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 <ita/ita_attr.h>
32 #include <ita/ita_attr_get_settings.h>
33 #include <ita/ita_attr_settings.h>
35 #include <tncif_names.h>
36 #include <tncif_pa_subtypes.h>
39 #include <collections/linked_list.h>
40 #include <utils/debug.h>
41 #include <utils/lexparser.h>
45 static const char imv_name
[] = "OS";
47 static pen_type_t msg_types
[] = {
48 { PEN_IETF
, PA_SUBTYPE_IETF_OPERATING_SYSTEM
}
51 static imv_agent_t
*imv_os
;
54 * see section 3.8.1 of TCG TNC IF-IMV Specification 1.3
56 TNC_Result
TNC_IMV_Initialize(TNC_IMVID imv_id
,
57 TNC_Version min_version
,
58 TNC_Version max_version
,
59 TNC_Version
*actual_version
)
63 DBG1(DBG_IMV
, "IMV \"%s\" has already been initialized", imv_name
);
64 return TNC_RESULT_ALREADY_INITIALIZED
;
66 imv_os
= imv_agent_create(imv_name
, msg_types
, countof(msg_types
),
67 imv_id
, actual_version
);
70 return TNC_RESULT_FATAL
;
72 if (min_version
> TNC_IFIMV_VERSION_1
|| max_version
< TNC_IFIMV_VERSION_1
)
74 DBG1(DBG_IMV
, "no common IF-IMV version");
75 return TNC_RESULT_NO_COMMON_VERSION
;
78 return TNC_RESULT_SUCCESS
;
82 * see section 3.8.2 of TCG TNC IF-IMV Specification 1.3
84 TNC_Result
TNC_IMV_NotifyConnectionChange(TNC_IMVID imv_id
,
85 TNC_ConnectionID connection_id
,
86 TNC_ConnectionState new_state
)
92 DBG1(DBG_IMV
, "IMV \"%s\" has not been initialized", imv_name
);
93 return TNC_RESULT_NOT_INITIALIZED
;
97 case TNC_CONNECTION_STATE_CREATE
:
98 state
= imv_os_state_create(connection_id
);
99 return imv_os
->create_state(imv_os
, state
);
100 case TNC_CONNECTION_STATE_DELETE
:
101 return imv_os
->delete_state(imv_os
, connection_id
);
103 return imv_os
->change_state(imv_os
, connection_id
,
109 * print multi-line values to debug output
111 static void dbg_imv_multi_line(chunk_t value
)
115 while (extract_token(&line
, '\n', &value
))
117 DBG2(DBG_IMV
, " %.*s", line
.len
, line
.ptr
);
121 DBG2(DBG_IMV
, " %.*s", value
.len
, value
.ptr
);
125 static TNC_Result
receive_message(imv_state_t
*state
, imv_msg_t
*in_msg
)
128 imv_os_state_t
*os_state
;
129 enumerator_t
*enumerator
;
133 chunk_t os_name
= chunk_empty
;
134 chunk_t os_version
= chunk_empty
;
135 bool fatal_error
= FALSE
, assessment
= FALSE
;
138 /* parse received PA-TNC message and handle local and remote errors */
139 result
= in_msg
->receive(in_msg
, &fatal_error
);
140 if (result
!= TNC_RESULT_SUCCESS
)
145 out_msg
= imv_msg_create_as_reply(in_msg
);
147 /* analyze PA-TNC attributes */
148 enumerator
= in_msg
->create_attribute_enumerator(in_msg
);
149 while (enumerator
->enumerate(enumerator
, &attr
))
151 type
= attr
->get_type(attr
);
153 if (type
.vendor_id
== PEN_IETF
)
157 case IETF_ATTR_PRODUCT_INFORMATION
:
159 ietf_attr_product_info_t
*attr_cast
;
162 attr_cast
= (ietf_attr_product_info_t
*)attr
;
163 os_name
= attr_cast
->get_info(attr_cast
, &vendor_id
, NULL
);
164 if (vendor_id
!= PEN_IETF
)
166 DBG1(DBG_IMV
, "operating system name is '%.*s' "
167 "from vendor %N", os_name
.len
, os_name
.ptr
,
168 pen_names
, vendor_id
);
172 DBG1(DBG_IMV
, "operating system name is '%.*s'",
173 os_name
.len
, os_name
.ptr
);
177 case IETF_ATTR_STRING_VERSION
:
179 ietf_attr_string_version_t
*attr_cast
;
181 attr_cast
= (ietf_attr_string_version_t
*)attr
;
182 os_version
= attr_cast
->get_version(attr_cast
, NULL
, NULL
);
185 DBG1(DBG_IMV
, "operating system version is '%.*s'",
186 os_version
.len
, os_version
.ptr
);
190 case IETF_ATTR_NUMERIC_VERSION
:
192 ietf_attr_numeric_version_t
*attr_cast
;
193 u_int32_t major
, minor
;
195 attr_cast
= (ietf_attr_numeric_version_t
*)attr
;
196 attr_cast
->get_version(attr_cast
, &major
, &minor
);
197 DBG1(DBG_IMV
, "operating system numeric version is %d.%d",
201 case IETF_ATTR_OPERATIONAL_STATUS
:
203 ietf_attr_op_status_t
*attr_cast
;
204 op_status_t op_status
;
205 op_result_t op_result
;
208 attr_cast
= (ietf_attr_op_status_t
*)attr
;
209 op_status
= attr_cast
->get_status(attr_cast
);
210 op_result
= attr_cast
->get_result(attr_cast
);
211 last_boot
= attr_cast
->get_last_use(attr_cast
);
212 DBG1(DBG_IMV
, "operational status: %N, result: %N",
213 op_status_names
, op_status
, op_result_names
, op_result
);
214 DBG1(DBG_IMV
, "last boot: %T", &last_boot
, TRUE
);
217 case IETF_ATTR_FORWARDING_ENABLED
:
219 ietf_attr_fwd_enabled_t
*attr_cast
;
220 os_fwd_status_t fwd_status
;
222 attr_cast
= (ietf_attr_fwd_enabled_t
*)attr
;
223 fwd_status
= attr_cast
->get_status(attr_cast
);
224 DBG1(DBG_IMV
, "IPv4 forwarding status: %N",
225 os_fwd_status_names
, fwd_status
);
228 case IETF_ATTR_FACTORY_DEFAULT_PWD_ENABLED
:
230 ietf_attr_default_pwd_enabled_t
*attr_cast
;
231 bool default_pwd_status
;
233 attr_cast
= (ietf_attr_default_pwd_enabled_t
*)attr
;
234 default_pwd_status
= attr_cast
->get_status(attr_cast
);
235 DBG1(DBG_IMV
, "factory default password: %sabled",
236 default_pwd_status ?
"en":"dis");
239 case IETF_ATTR_INSTALLED_PACKAGES
:
241 ietf_attr_installed_packages_t
*attr_cast
;
243 chunk_t name
, version
;
245 attr_cast
= (ietf_attr_installed_packages_t
*)attr
;
246 e
= attr_cast
->create_enumerator(attr_cast
);
247 while (e
->enumerate(e
, &name
, &version
))
249 DBG1(DBG_IMV
, "package '%.*s' %.*s", name
.len
, name
.ptr
,
250 version
.len
, version
.ptr
);
254 state
->set_recommendation(state
,
255 TNC_IMV_ACTION_RECOMMENDATION_ALLOW
,
256 TNC_IMV_EVALUATION_RESULT_COMPLIANT
);
264 else if (type
.vendor_id
== PEN_ITA
&& type
.type
== ITA_ATTR_SETTINGS
)
266 ita_attr_settings_t
*attr_cast
;
271 attr_cast
= (ita_attr_settings_t
*)attr
;
272 e
= attr_cast
->create_enumerator(attr_cast
);
273 while (e
->enumerate(e
, &name
, &value
))
275 DBG1(DBG_IMV
, "setting '%s'", name
);
276 dbg_imv_multi_line(value
);
281 enumerator
->destroy(enumerator
);
283 if (os_name
.len
&& os_version
.len
)
286 char *uri
= "http://remediation.strongswan.org/fix-it/";
287 char *string
= "use a Linux operating system instead of Windows 1.2.3";
288 char *lang_code
= "en";
290 os_state
= (imv_os_state_t
*)state
;
291 os_state
->set_info(os_state
, os_name
, os_version
);
292 product_info
= os_state
->get_info(os_state
);
294 if (streq(product_info
, "Windows 1.2.3"))
296 DBG1(DBG_IMV
, "OS '%s' is not supported", product_info
);
298 attr
= ietf_attr_remediation_instr_create_from_string(
299 chunk_create(string
, strlen(string
)),
300 chunk_create(lang_code
, strlen(lang_code
)));
301 out_msg
->add_attribute(out_msg
, attr
);
302 attr
= ietf_attr_remediation_instr_create_from_uri(
303 chunk_create(uri
, strlen(uri
)));
304 out_msg
->add_attribute(out_msg
, attr
);
306 state
->set_recommendation(state
,
307 TNC_IMV_ACTION_RECOMMENDATION_ISOLATE
,
308 TNC_IMV_EVALUATION_RESULT_NONCOMPLIANT_MINOR
);
313 ita_attr_get_settings_t
*attr_cast
;
315 DBG1(DBG_IMV
, "requesting installed packages for '%s'",
317 attr
= ietf_attr_attr_request_create(PEN_IETF
,
318 IETF_ATTR_INSTALLED_PACKAGES
);
319 out_msg
->add_attribute(out_msg
, attr
);
321 /* requesting Android or Linux settings */
322 attr
= ita_attr_get_settings_create();
323 attr_cast
= (ita_attr_get_settings_t
*)attr
;
325 if (chunk_equals(os_name
, chunk_create("Android", 7)))
327 attr_cast
->add(attr_cast
, "android_id");
328 attr_cast
->add(attr_cast
, "install_non_market_apps");
332 attr_cast
->add(attr_cast
, "/proc/sys/kernel/random/boot_id");
333 attr_cast
->add(attr_cast
, "/proc/sys/kernel/tainted");
335 out_msg
->add_attribute(out_msg
, attr
);
341 state
->set_recommendation(state
,
342 TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION
,
343 TNC_IMV_EVALUATION_RESULT_ERROR
);
349 result
= out_msg
->send_assessment(out_msg
);
350 out_msg
->destroy(out_msg
);
351 if (result
!= TNC_RESULT_SUCCESS
)
355 return imv_os
->provide_recommendation(imv_os
, state
);
358 /* send PA-TNC message with excl flag set */
359 result
= out_msg
->send(out_msg
, TRUE
);
360 out_msg
->destroy(out_msg
);
366 * see section 3.8.4 of TCG TNC IF-IMV Specification 1.3
368 TNC_Result
TNC_IMV_ReceiveMessage(TNC_IMVID imv_id
,
369 TNC_ConnectionID connection_id
,
370 TNC_BufferReference msg
,
372 TNC_MessageType msg_type
)
380 DBG1(DBG_IMV
, "IMV \"%s\" has not been initialized", imv_name
);
381 return TNC_RESULT_NOT_INITIALIZED
;
383 if (!imv_os
->get_state(imv_os
, connection_id
, &state
))
385 return TNC_RESULT_FATAL
;
387 in_msg
= imv_msg_create_from_data(imv_os
, state
, connection_id
, msg_type
,
388 chunk_create(msg
, msg_len
));
389 result
= receive_message(state
, in_msg
);
390 in_msg
->destroy(in_msg
);
396 * see section 3.8.6 of TCG TNC IF-IMV Specification 1.3
398 TNC_Result
TNC_IMV_ReceiveMessageLong(TNC_IMVID imv_id
,
399 TNC_ConnectionID connection_id
,
400 TNC_UInt32 msg_flags
,
401 TNC_BufferReference msg
,
403 TNC_VendorID msg_vid
,
404 TNC_MessageSubtype msg_subtype
,
405 TNC_UInt32 src_imc_id
,
406 TNC_UInt32 dst_imv_id
)
414 DBG1(DBG_IMV
, "IMV \"%s\" has not been initialized", imv_name
);
415 return TNC_RESULT_NOT_INITIALIZED
;
417 if (!imv_os
->get_state(imv_os
, connection_id
, &state
))
419 return TNC_RESULT_FATAL
;
421 in_msg
= imv_msg_create_from_long_data(imv_os
, state
, connection_id
,
422 src_imc_id
, dst_imv_id
, msg_vid
, msg_subtype
,
423 chunk_create(msg
, msg_len
));
424 result
=receive_message(state
, in_msg
);
425 in_msg
->destroy(in_msg
);
431 * see section 3.8.7 of TCG TNC IF-IMV Specification 1.3
433 TNC_Result
TNC_IMV_SolicitRecommendation(TNC_IMVID imv_id
,
434 TNC_ConnectionID connection_id
)
440 DBG1(DBG_IMV
, "IMV \"%s\" has not been initialized", imv_name
);
441 return TNC_RESULT_NOT_INITIALIZED
;
443 if (!imv_os
->get_state(imv_os
, connection_id
, &state
))
445 return TNC_RESULT_FATAL
;
447 return imv_os
->provide_recommendation(imv_os
, state
);
451 * see section 3.8.8 of TCG TNC IF-IMV Specification 1.3
453 TNC_Result
TNC_IMV_BatchEnding(TNC_IMVID imv_id
,
454 TNC_ConnectionID connection_id
)
457 imv_os_state_t
*os_state
;
458 TNC_Result result
= TNC_RESULT_SUCCESS
;
462 DBG1(DBG_IMV
, "IMV \"%s\" has not been initialized", imv_name
);
463 return TNC_RESULT_NOT_INITIALIZED
;
465 if (!imv_os
->get_state(imv_os
, connection_id
, &state
))
467 return TNC_RESULT_FATAL
;
469 os_state
= (imv_os_state_t
*)state
;
471 if (os_state
->get_info(os_state
) == NULL
)
475 ietf_attr_attr_request_t
*attr_cast
;
477 out_msg
= imv_msg_create(imv_os
, state
, connection_id
, imv_id
,
478 TNC_IMCID_ANY
, msg_types
[0]);
479 attr
= ietf_attr_attr_request_create(PEN_IETF
,
480 IETF_ATTR_PRODUCT_INFORMATION
);
481 attr_cast
= (ietf_attr_attr_request_t
*)attr
;
482 attr_cast
->add(attr_cast
, PEN_IETF
, IETF_ATTR_STRING_VERSION
);
483 attr_cast
->add(attr_cast
, PEN_IETF
, IETF_ATTR_NUMERIC_VERSION
);
484 attr_cast
->add(attr_cast
, PEN_IETF
, IETF_ATTR_OPERATIONAL_STATUS
);
485 attr_cast
->add(attr_cast
, PEN_IETF
, IETF_ATTR_FORWARDING_ENABLED
);
486 attr_cast
->add(attr_cast
, PEN_IETF
, IETF_ATTR_FACTORY_DEFAULT_PWD_ENABLED
);
487 out_msg
->add_attribute(out_msg
, attr
);
489 /* send PA-TNC message with excl flag not set */
490 result
= out_msg
->send(out_msg
, FALSE
);
491 out_msg
->destroy(out_msg
);
498 * see section 3.8.9 of TCG TNC IF-IMV Specification 1.3
500 TNC_Result
TNC_IMV_Terminate(TNC_IMVID imv_id
)
504 DBG1(DBG_IMV
, "IMV \"%s\" has not been initialized", imv_name
);
505 return TNC_RESULT_NOT_INITIALIZED
;
507 imv_os
->destroy(imv_os
);
510 return TNC_RESULT_SUCCESS
;
514 * see section 4.2.8.1 of TCG TNC IF-IMV Specification 1.3
516 TNC_Result
TNC_IMV_ProvideBindFunction(TNC_IMVID imv_id
,
517 TNC_TNCS_BindFunctionPointer bind_function
)
521 DBG1(DBG_IMV
, "IMV \"%s\" has not been initialized", imv_name
);
522 return TNC_RESULT_NOT_INITIALIZED
;
524 return imv_os
->bind_functions(imv_os
, bind_function
);