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 <utils/linked_list.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
, 1, imv_id
, actual_version
);
66 return TNC_RESULT_FATAL
;
68 if (min_version
> TNC_IFIMV_VERSION_1
|| max_version
< TNC_IFIMV_VERSION_1
)
70 DBG1(DBG_IMV
, "no common IF-IMV version");
71 return TNC_RESULT_NO_COMMON_VERSION
;
74 return TNC_RESULT_SUCCESS
;
78 * see section 3.8.2 of TCG TNC IF-IMV Specification 1.3
80 TNC_Result
TNC_IMV_NotifyConnectionChange(TNC_IMVID imv_id
,
81 TNC_ConnectionID connection_id
,
82 TNC_ConnectionState new_state
)
88 DBG1(DBG_IMV
, "IMV \"%s\" has not been initialized", imv_name
);
89 return TNC_RESULT_NOT_INITIALIZED
;
93 case TNC_CONNECTION_STATE_CREATE
:
94 state
= imv_os_state_create(connection_id
);
95 return imv_os
->create_state(imv_os
, state
);
96 case TNC_CONNECTION_STATE_DELETE
:
97 return imv_os
->delete_state(imv_os
, connection_id
);
99 return imv_os
->change_state(imv_os
, connection_id
,
104 static TNC_Result
receive_message(imv_state_t
*state
, imv_msg_t
*in_msg
)
107 imv_os_state_t
*os_state
;
108 enumerator_t
*enumerator
;
112 chunk_t os_name
= chunk_empty
;
113 chunk_t os_version
= chunk_empty
;
114 bool fatal_error
= FALSE
, assessment
= FALSE
;
117 /* parse received PA-TNC message and handle local and remote errors */
118 result
= in_msg
->receive(in_msg
, &fatal_error
);
119 if (result
!= TNC_RESULT_SUCCESS
)
124 out_msg
= imv_msg_create_as_reply(in_msg
);
126 /* analyze PA-TNC attributes */
127 enumerator
= in_msg
->create_attribute_enumerator(in_msg
);
128 while (enumerator
->enumerate(enumerator
, &attr
))
130 type
= attr
->get_type(attr
);
132 if (type
.vendor_id
!= PEN_IETF
)
138 case IETF_ATTR_PRODUCT_INFORMATION
:
140 ietf_attr_product_info_t
*attr_cast
;
142 attr_cast
= (ietf_attr_product_info_t
*)attr
;
143 os_name
= attr_cast
->get_info(attr_cast
, NULL
, NULL
);
144 DBG1(DBG_IMV
, "operating system name is '%.*s'",
145 os_name
.len
, os_name
.ptr
);
148 case IETF_ATTR_STRING_VERSION
:
150 ietf_attr_string_version_t
*attr_cast
;
152 attr_cast
= (ietf_attr_string_version_t
*)attr
;
153 os_version
= attr_cast
->get_version(attr_cast
, NULL
, NULL
);
156 DBG1(DBG_IMV
, "operating system version is '%.*s'",
157 os_version
.len
, os_version
.ptr
);
161 case IETF_ATTR_NUMERIC_VERSION
:
163 ietf_attr_numeric_version_t
*attr_cast
;
164 u_int32_t major
, minor
;
166 attr_cast
= (ietf_attr_numeric_version_t
*)attr
;
167 attr_cast
->get_version(attr_cast
, &major
, &minor
);
168 DBG1(DBG_IMV
, "operating system numeric version is %d.%d",
172 case IETF_ATTR_OPERATIONAL_STATUS
:
174 ietf_attr_op_status_t
*attr_cast
;
175 op_status_t op_status
;
176 op_result_t op_result
;
179 attr_cast
= (ietf_attr_op_status_t
*)attr
;
180 op_status
= attr_cast
->get_status(attr_cast
);
181 op_result
= attr_cast
->get_result(attr_cast
);
182 last_boot
= attr_cast
->get_last_use(attr_cast
);
183 DBG1(DBG_IMV
, "operational status: %N, result: %N",
184 op_status_names
, op_status
, op_result_names
, op_result
);
185 DBG1(DBG_IMV
, "last boot: %T", &last_boot
, TRUE
);
188 case IETF_ATTR_FORWARDING_ENABLED
:
190 ietf_attr_fwd_enabled_t
*attr_cast
;
191 os_fwd_status_t fwd_status
;
193 attr_cast
= (ietf_attr_fwd_enabled_t
*)attr
;
194 fwd_status
= attr_cast
->get_status(attr_cast
);
195 DBG1(DBG_IMV
, "IPv4 forwarding status: %N",
196 os_fwd_status_names
, fwd_status
);
199 case IETF_ATTR_FACTORY_DEFAULT_PWD_ENABLED
:
201 ietf_attr_default_pwd_enabled_t
*attr_cast
;
202 bool default_pwd_status
;
204 attr_cast
= (ietf_attr_default_pwd_enabled_t
*)attr
;
205 default_pwd_status
= attr_cast
->get_status(attr_cast
);
206 DBG1(DBG_IMV
, "factory default password: %sabled",
207 default_pwd_status ?
"en":"dis");
210 case IETF_ATTR_INSTALLED_PACKAGES
:
212 ietf_attr_installed_packages_t
*attr_cast
;
214 chunk_t name
, version
;
216 attr_cast
= (ietf_attr_installed_packages_t
*)attr
;
217 e
= attr_cast
->create_enumerator(attr_cast
);
218 while (e
->enumerate(e
, &name
, &version
))
220 DBG1(DBG_IMV
, "package '%.*s' %.*s", name
.len
, name
.ptr
,
221 version
.len
, version
.ptr
);
225 state
->set_recommendation(state
,
226 TNC_IMV_ACTION_RECOMMENDATION_ALLOW
,
227 TNC_IMV_EVALUATION_RESULT_COMPLIANT
);
235 enumerator
->destroy(enumerator
);
237 if (os_name
.len
&& os_version
.len
)
240 char *uri
= "http://remediation.strongswan.org/fix-it/";
241 char *string
= "use a Linux operating system instead of Windows 1.2.3";
242 char *lang_code
= "en";
244 os_state
= (imv_os_state_t
*)state
;
245 os_state
->set_info(os_state
, os_name
, os_version
);
246 product_info
= os_state
->get_info(os_state
);
248 if (streq(product_info
, "Windows 1.2.3"))
250 DBG1(DBG_IMV
, "OS '%s' is not supported", product_info
);
252 attr
= ietf_attr_remediation_instr_create_from_string(
253 chunk_create(string
, strlen(string
)),
254 chunk_create(lang_code
, strlen(lang_code
)));
255 out_msg
->add_attribute(out_msg
, attr
);
256 attr
= ietf_attr_remediation_instr_create_from_uri(
257 chunk_create(uri
, strlen(uri
)));
258 out_msg
->add_attribute(out_msg
, attr
);
260 state
->set_recommendation(state
,
261 TNC_IMV_ACTION_RECOMMENDATION_ISOLATE
,
262 TNC_IMV_EVALUATION_RESULT_NONCOMPLIANT_MINOR
);
267 DBG1(DBG_IMV
, "requesting installed packages for '%s'",
269 attr
= ietf_attr_attr_request_create(PEN_IETF
,
270 IETF_ATTR_INSTALLED_PACKAGES
);
271 out_msg
->add_attribute(out_msg
, attr
);
277 state
->set_recommendation(state
,
278 TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION
,
279 TNC_IMV_EVALUATION_RESULT_ERROR
);
285 result
= out_msg
->send_assessment(out_msg
);
286 out_msg
->destroy(out_msg
);
287 if (result
!= TNC_RESULT_SUCCESS
)
291 return imv_os
->provide_recommendation(imv_os
, state
);
294 /* send PA-TNC message with excl flag set */
295 result
= out_msg
->send(out_msg
, TRUE
);
296 out_msg
->destroy(out_msg
);
302 * see section 3.8.4 of TCG TNC IF-IMV Specification 1.3
304 TNC_Result
TNC_IMV_ReceiveMessage(TNC_IMVID imv_id
,
305 TNC_ConnectionID connection_id
,
306 TNC_BufferReference msg
,
308 TNC_MessageType msg_type
)
316 DBG1(DBG_IMV
, "IMV \"%s\" has not been initialized", imv_name
);
317 return TNC_RESULT_NOT_INITIALIZED
;
319 if (!imv_os
->get_state(imv_os
, connection_id
, &state
))
321 return TNC_RESULT_FATAL
;
323 in_msg
= imv_msg_create_from_data(imv_os
, state
, connection_id
, msg_type
,
324 chunk_create(msg
, msg_len
));
325 result
= receive_message(state
, in_msg
);
326 in_msg
->destroy(in_msg
);
332 * see section 3.8.6 of TCG TNC IF-IMV Specification 1.3
334 TNC_Result
TNC_IMV_ReceiveMessageLong(TNC_IMVID imv_id
,
335 TNC_ConnectionID connection_id
,
336 TNC_UInt32 msg_flags
,
337 TNC_BufferReference msg
,
339 TNC_VendorID msg_vid
,
340 TNC_MessageSubtype msg_subtype
,
341 TNC_UInt32 src_imc_id
,
342 TNC_UInt32 dst_imv_id
)
350 DBG1(DBG_IMV
, "IMV \"%s\" has not been initialized", imv_name
);
351 return TNC_RESULT_NOT_INITIALIZED
;
353 if (!imv_os
->get_state(imv_os
, connection_id
, &state
))
355 return TNC_RESULT_FATAL
;
357 in_msg
= imv_msg_create_from_long_data(imv_os
, state
, connection_id
,
358 src_imc_id
, dst_imv_id
, msg_vid
, msg_subtype
,
359 chunk_create(msg
, msg_len
));
360 result
=receive_message(state
, in_msg
);
361 in_msg
->destroy(in_msg
);
367 * see section 3.8.7 of TCG TNC IF-IMV Specification 1.3
369 TNC_Result
TNC_IMV_SolicitRecommendation(TNC_IMVID imv_id
,
370 TNC_ConnectionID connection_id
)
376 DBG1(DBG_IMV
, "IMV \"%s\" has not been initialized", imv_name
);
377 return TNC_RESULT_NOT_INITIALIZED
;
379 if (!imv_os
->get_state(imv_os
, connection_id
, &state
))
381 return TNC_RESULT_FATAL
;
383 return imv_os
->provide_recommendation(imv_os
, state
);
387 * see section 3.8.8 of TCG TNC IF-IMV Specification 1.3
389 TNC_Result
TNC_IMV_BatchEnding(TNC_IMVID imv_id
,
390 TNC_ConnectionID connection_id
)
393 imv_os_state_t
*os_state
;
394 TNC_Result result
= TNC_RESULT_SUCCESS
;
398 DBG1(DBG_IMV
, "IMV \"%s\" has not been initialized", imv_name
);
399 return TNC_RESULT_NOT_INITIALIZED
;
401 if (!imv_os
->get_state(imv_os
, connection_id
, &state
))
403 return TNC_RESULT_FATAL
;
405 os_state
= (imv_os_state_t
*)state
;
407 if (os_state
->get_info(os_state
) == NULL
)
411 ietf_attr_attr_request_t
*attr_cast
;
413 out_msg
= imv_msg_create(imv_os
, state
, connection_id
, imv_id
,
414 TNC_IMCID_ANY
, msg_types
[0]);
415 attr
= ietf_attr_attr_request_create(PEN_IETF
,
416 IETF_ATTR_PRODUCT_INFORMATION
);
417 attr_cast
= (ietf_attr_attr_request_t
*)attr
;
418 attr_cast
->add(attr_cast
, PEN_IETF
, IETF_ATTR_STRING_VERSION
);
419 attr_cast
->add(attr_cast
, PEN_IETF
, IETF_ATTR_NUMERIC_VERSION
);
420 attr_cast
->add(attr_cast
, PEN_IETF
, IETF_ATTR_OPERATIONAL_STATUS
);
421 attr_cast
->add(attr_cast
, PEN_IETF
, IETF_ATTR_FORWARDING_ENABLED
);
422 attr_cast
->add(attr_cast
, PEN_IETF
, IETF_ATTR_FACTORY_DEFAULT_PWD_ENABLED
);
423 out_msg
->add_attribute(out_msg
, attr
);
425 /* send PA-TNC message with excl flag not set */
426 result
= out_msg
->send(out_msg
, FALSE
);
427 out_msg
->destroy(out_msg
);
434 * see section 3.8.9 of TCG TNC IF-IMV Specification 1.3
436 TNC_Result
TNC_IMV_Terminate(TNC_IMVID imv_id
)
440 DBG1(DBG_IMV
, "IMV \"%s\" has not been initialized", imv_name
);
441 return TNC_RESULT_NOT_INITIALIZED
;
443 imv_os
->destroy(imv_os
);
446 return TNC_RESULT_SUCCESS
;
450 * see section 4.2.8.1 of TCG TNC IF-IMV Specification 1.3
452 TNC_Result
TNC_IMV_ProvideBindFunction(TNC_IMVID imv_id
,
453 TNC_TNCS_BindFunctionPointer bind_function
)
457 DBG1(DBG_IMV
, "IMV \"%s\" has not been initialized", imv_name
);
458 return TNC_RESULT_NOT_INITIALIZED
;
460 return imv_os
->bind_functions(imv_os
, bind_function
);