2 * Copyright (C) 2012 Andreas Steffen
3 * HSR Hochschule fuer Technik Rapperswil
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 #include "imv_os_state.h"
18 #include <imv/imv_agent.h>
19 #include <imv/imv_msg.h>
20 #include <ietf/ietf_attr.h>
21 #include <ietf/ietf_attr_attr_request.h>
22 #include <ietf/ietf_attr_default_pwd_enabled.h>
23 #include <ietf/ietf_attr_fwd_enabled.h>
24 #include <ietf/ietf_attr_installed_packages.h>
25 #include <ietf/ietf_attr_numeric_version.h>
26 #include <ietf/ietf_attr_op_status.h>
27 #include <ietf/ietf_attr_pa_tnc_error.h>
28 #include <ietf/ietf_attr_product_info.h>
29 #include <ietf/ietf_attr_remediation_instr.h>
30 #include <ietf/ietf_attr_string_version.h>
31 #include <os_info/os_info.h>
33 #include <tncif_names.h>
34 #include <tncif_pa_subtypes.h>
37 #include <collections/linked_list.h>
38 #include <utils/debug.h>
42 static const char imv_name
[] = "OS";
44 static pen_type_t msg_types
[] = {
45 { PEN_IETF
, PA_SUBTYPE_IETF_OPERATING_SYSTEM
}
48 static imv_agent_t
*imv_os
;
51 * see section 3.8.1 of TCG TNC IF-IMV Specification 1.3
53 TNC_Result
TNC_IMV_Initialize(TNC_IMVID imv_id
,
54 TNC_Version min_version
,
55 TNC_Version max_version
,
56 TNC_Version
*actual_version
)
60 DBG1(DBG_IMV
, "IMV \"%s\" has already been initialized", imv_name
);
61 return TNC_RESULT_ALREADY_INITIALIZED
;
63 imv_os
= imv_agent_create(imv_name
, msg_types
, countof(msg_types
),
64 imv_id
, actual_version
);
67 return TNC_RESULT_FATAL
;
69 if (min_version
> TNC_IFIMV_VERSION_1
|| max_version
< TNC_IFIMV_VERSION_1
)
71 DBG1(DBG_IMV
, "no common IF-IMV version");
72 return TNC_RESULT_NO_COMMON_VERSION
;
75 return TNC_RESULT_SUCCESS
;
79 * see section 3.8.2 of TCG TNC IF-IMV Specification 1.3
81 TNC_Result
TNC_IMV_NotifyConnectionChange(TNC_IMVID imv_id
,
82 TNC_ConnectionID connection_id
,
83 TNC_ConnectionState new_state
)
89 DBG1(DBG_IMV
, "IMV \"%s\" has not been initialized", imv_name
);
90 return TNC_RESULT_NOT_INITIALIZED
;
94 case TNC_CONNECTION_STATE_CREATE
:
95 state
= imv_os_state_create(connection_id
);
96 return imv_os
->create_state(imv_os
, state
);
97 case TNC_CONNECTION_STATE_DELETE
:
98 return imv_os
->delete_state(imv_os
, connection_id
);
100 return imv_os
->change_state(imv_os
, connection_id
,
105 static TNC_Result
receive_message(imv_state_t
*state
, imv_msg_t
*in_msg
)
108 imv_os_state_t
*os_state
;
109 enumerator_t
*enumerator
;
113 chunk_t os_name
= chunk_empty
;
114 chunk_t os_version
= chunk_empty
;
115 bool fatal_error
= FALSE
, assessment
= FALSE
;
118 /* parse received PA-TNC message and handle local and remote errors */
119 result
= in_msg
->receive(in_msg
, &fatal_error
);
120 if (result
!= TNC_RESULT_SUCCESS
)
125 out_msg
= imv_msg_create_as_reply(in_msg
);
127 /* analyze PA-TNC attributes */
128 enumerator
= in_msg
->create_attribute_enumerator(in_msg
);
129 while (enumerator
->enumerate(enumerator
, &attr
))
131 type
= attr
->get_type(attr
);
133 if (type
.vendor_id
!= PEN_IETF
)
139 case IETF_ATTR_PRODUCT_INFORMATION
:
141 ietf_attr_product_info_t
*attr_cast
;
144 attr_cast
= (ietf_attr_product_info_t
*)attr
;
145 os_name
= attr_cast
->get_info(attr_cast
, &vendor_id
, NULL
);
146 if (vendor_id
!= PEN_IETF
)
148 DBG1(DBG_IMV
, "operating system name is '%.*s' "
149 "from vendor %N", os_name
.len
, os_name
.ptr
,
150 pen_names
, vendor_id
);
154 DBG1(DBG_IMV
, "operating system name is '%.*s'",
155 os_name
.len
, os_name
.ptr
);
159 case IETF_ATTR_STRING_VERSION
:
161 ietf_attr_string_version_t
*attr_cast
;
163 attr_cast
= (ietf_attr_string_version_t
*)attr
;
164 os_version
= attr_cast
->get_version(attr_cast
, NULL
, NULL
);
167 DBG1(DBG_IMV
, "operating system version is '%.*s'",
168 os_version
.len
, os_version
.ptr
);
172 case IETF_ATTR_NUMERIC_VERSION
:
174 ietf_attr_numeric_version_t
*attr_cast
;
175 u_int32_t major
, minor
;
177 attr_cast
= (ietf_attr_numeric_version_t
*)attr
;
178 attr_cast
->get_version(attr_cast
, &major
, &minor
);
179 DBG1(DBG_IMV
, "operating system numeric version is %d.%d",
183 case IETF_ATTR_OPERATIONAL_STATUS
:
185 ietf_attr_op_status_t
*attr_cast
;
186 op_status_t op_status
;
187 op_result_t op_result
;
190 attr_cast
= (ietf_attr_op_status_t
*)attr
;
191 op_status
= attr_cast
->get_status(attr_cast
);
192 op_result
= attr_cast
->get_result(attr_cast
);
193 last_boot
= attr_cast
->get_last_use(attr_cast
);
194 DBG1(DBG_IMV
, "operational status: %N, result: %N",
195 op_status_names
, op_status
, op_result_names
, op_result
);
196 DBG1(DBG_IMV
, "last boot: %T", &last_boot
, TRUE
);
199 case IETF_ATTR_FORWARDING_ENABLED
:
201 ietf_attr_fwd_enabled_t
*attr_cast
;
202 os_fwd_status_t fwd_status
;
204 attr_cast
= (ietf_attr_fwd_enabled_t
*)attr
;
205 fwd_status
= attr_cast
->get_status(attr_cast
);
206 DBG1(DBG_IMV
, "IPv4 forwarding status: %N",
207 os_fwd_status_names
, fwd_status
);
210 case IETF_ATTR_FACTORY_DEFAULT_PWD_ENABLED
:
212 ietf_attr_default_pwd_enabled_t
*attr_cast
;
213 bool default_pwd_status
;
215 attr_cast
= (ietf_attr_default_pwd_enabled_t
*)attr
;
216 default_pwd_status
= attr_cast
->get_status(attr_cast
);
217 DBG1(DBG_IMV
, "factory default password: %sabled",
218 default_pwd_status ?
"en":"dis");
221 case IETF_ATTR_INSTALLED_PACKAGES
:
223 ietf_attr_installed_packages_t
*attr_cast
;
225 chunk_t name
, version
;
227 attr_cast
= (ietf_attr_installed_packages_t
*)attr
;
228 e
= attr_cast
->create_enumerator(attr_cast
);
229 while (e
->enumerate(e
, &name
, &version
))
231 DBG1(DBG_IMV
, "package '%.*s' %.*s", name
.len
, name
.ptr
,
232 version
.len
, version
.ptr
);
236 state
->set_recommendation(state
,
237 TNC_IMV_ACTION_RECOMMENDATION_ALLOW
,
238 TNC_IMV_EVALUATION_RESULT_COMPLIANT
);
246 enumerator
->destroy(enumerator
);
248 if (os_name
.len
&& os_version
.len
)
251 char *uri
= "http://remediation.strongswan.org/fix-it/";
252 char *string
= "use a Linux operating system instead of Windows 1.2.3";
253 char *lang_code
= "en";
255 os_state
= (imv_os_state_t
*)state
;
256 os_state
->set_info(os_state
, os_name
, os_version
);
257 product_info
= os_state
->get_info(os_state
);
259 if (streq(product_info
, "Windows 1.2.3"))
261 DBG1(DBG_IMV
, "OS '%s' is not supported", product_info
);
263 attr
= ietf_attr_remediation_instr_create_from_string(
264 chunk_create(string
, strlen(string
)),
265 chunk_create(lang_code
, strlen(lang_code
)));
266 out_msg
->add_attribute(out_msg
, attr
);
267 attr
= ietf_attr_remediation_instr_create_from_uri(
268 chunk_create(uri
, strlen(uri
)));
269 out_msg
->add_attribute(out_msg
, attr
);
271 state
->set_recommendation(state
,
272 TNC_IMV_ACTION_RECOMMENDATION_ISOLATE
,
273 TNC_IMV_EVALUATION_RESULT_NONCOMPLIANT_MINOR
);
278 DBG1(DBG_IMV
, "requesting installed packages for '%s'",
280 attr
= ietf_attr_attr_request_create(PEN_IETF
,
281 IETF_ATTR_INSTALLED_PACKAGES
);
282 out_msg
->add_attribute(out_msg
, attr
);
288 state
->set_recommendation(state
,
289 TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION
,
290 TNC_IMV_EVALUATION_RESULT_ERROR
);
296 result
= out_msg
->send_assessment(out_msg
);
297 out_msg
->destroy(out_msg
);
298 if (result
!= TNC_RESULT_SUCCESS
)
302 return imv_os
->provide_recommendation(imv_os
, state
);
305 /* send PA-TNC message with excl flag set */
306 result
= out_msg
->send(out_msg
, TRUE
);
307 out_msg
->destroy(out_msg
);
313 * see section 3.8.4 of TCG TNC IF-IMV Specification 1.3
315 TNC_Result
TNC_IMV_ReceiveMessage(TNC_IMVID imv_id
,
316 TNC_ConnectionID connection_id
,
317 TNC_BufferReference msg
,
319 TNC_MessageType msg_type
)
327 DBG1(DBG_IMV
, "IMV \"%s\" has not been initialized", imv_name
);
328 return TNC_RESULT_NOT_INITIALIZED
;
330 if (!imv_os
->get_state(imv_os
, connection_id
, &state
))
332 return TNC_RESULT_FATAL
;
334 in_msg
= imv_msg_create_from_data(imv_os
, state
, connection_id
, msg_type
,
335 chunk_create(msg
, msg_len
));
336 result
= receive_message(state
, in_msg
);
337 in_msg
->destroy(in_msg
);
343 * see section 3.8.6 of TCG TNC IF-IMV Specification 1.3
345 TNC_Result
TNC_IMV_ReceiveMessageLong(TNC_IMVID imv_id
,
346 TNC_ConnectionID connection_id
,
347 TNC_UInt32 msg_flags
,
348 TNC_BufferReference msg
,
350 TNC_VendorID msg_vid
,
351 TNC_MessageSubtype msg_subtype
,
352 TNC_UInt32 src_imc_id
,
353 TNC_UInt32 dst_imv_id
)
361 DBG1(DBG_IMV
, "IMV \"%s\" has not been initialized", imv_name
);
362 return TNC_RESULT_NOT_INITIALIZED
;
364 if (!imv_os
->get_state(imv_os
, connection_id
, &state
))
366 return TNC_RESULT_FATAL
;
368 in_msg
= imv_msg_create_from_long_data(imv_os
, state
, connection_id
,
369 src_imc_id
, dst_imv_id
, msg_vid
, msg_subtype
,
370 chunk_create(msg
, msg_len
));
371 result
=receive_message(state
, in_msg
);
372 in_msg
->destroy(in_msg
);
378 * see section 3.8.7 of TCG TNC IF-IMV Specification 1.3
380 TNC_Result
TNC_IMV_SolicitRecommendation(TNC_IMVID imv_id
,
381 TNC_ConnectionID connection_id
)
387 DBG1(DBG_IMV
, "IMV \"%s\" has not been initialized", imv_name
);
388 return TNC_RESULT_NOT_INITIALIZED
;
390 if (!imv_os
->get_state(imv_os
, connection_id
, &state
))
392 return TNC_RESULT_FATAL
;
394 return imv_os
->provide_recommendation(imv_os
, state
);
398 * see section 3.8.8 of TCG TNC IF-IMV Specification 1.3
400 TNC_Result
TNC_IMV_BatchEnding(TNC_IMVID imv_id
,
401 TNC_ConnectionID connection_id
)
404 imv_os_state_t
*os_state
;
405 TNC_Result result
= TNC_RESULT_SUCCESS
;
409 DBG1(DBG_IMV
, "IMV \"%s\" has not been initialized", imv_name
);
410 return TNC_RESULT_NOT_INITIALIZED
;
412 if (!imv_os
->get_state(imv_os
, connection_id
, &state
))
414 return TNC_RESULT_FATAL
;
416 os_state
= (imv_os_state_t
*)state
;
418 if (os_state
->get_info(os_state
) == NULL
)
422 ietf_attr_attr_request_t
*attr_cast
;
424 out_msg
= imv_msg_create(imv_os
, state
, connection_id
, imv_id
,
425 TNC_IMCID_ANY
, msg_types
[0]);
426 attr
= ietf_attr_attr_request_create(PEN_IETF
,
427 IETF_ATTR_PRODUCT_INFORMATION
);
428 attr_cast
= (ietf_attr_attr_request_t
*)attr
;
429 attr_cast
->add(attr_cast
, PEN_IETF
, IETF_ATTR_STRING_VERSION
);
430 attr_cast
->add(attr_cast
, PEN_IETF
, IETF_ATTR_NUMERIC_VERSION
);
431 attr_cast
->add(attr_cast
, PEN_IETF
, IETF_ATTR_OPERATIONAL_STATUS
);
432 attr_cast
->add(attr_cast
, PEN_IETF
, IETF_ATTR_FORWARDING_ENABLED
);
433 attr_cast
->add(attr_cast
, PEN_IETF
, IETF_ATTR_FACTORY_DEFAULT_PWD_ENABLED
);
434 out_msg
->add_attribute(out_msg
, attr
);
436 /* send PA-TNC message with excl flag not set */
437 result
= out_msg
->send(out_msg
, FALSE
);
438 out_msg
->destroy(out_msg
);
445 * see section 3.8.9 of TCG TNC IF-IMV Specification 1.3
447 TNC_Result
TNC_IMV_Terminate(TNC_IMVID imv_id
)
451 DBG1(DBG_IMV
, "IMV \"%s\" has not been initialized", imv_name
);
452 return TNC_RESULT_NOT_INITIALIZED
;
454 imv_os
->destroy(imv_os
);
457 return TNC_RESULT_SUCCESS
;
461 * see section 4.2.8.1 of TCG TNC IF-IMV Specification 1.3
463 TNC_Result
TNC_IMV_ProvideBindFunction(TNC_IMVID imv_id
,
464 TNC_TNCS_BindFunctionPointer bind_function
)
468 DBG1(DBG_IMV
, "IMV \"%s\" has not been initialized", imv_name
);
469 return TNC_RESULT_NOT_INITIALIZED
;
471 return imv_os
->bind_functions(imv_os
, bind_function
);