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_op_status.h>
26 #include <ietf/ietf_attr_pa_tnc_error.h>
27 #include <ietf/ietf_attr_product_info.h>
28 #include <ietf/ietf_attr_string_version.h>
29 #include <os_info/os_info.h>
31 #include <tncif_names.h>
32 #include <tncif_pa_subtypes.h>
35 #include <utils/linked_list.h>
40 static const char imv_name
[] = "OS";
42 static pen_type_t msg_types
[] = {
43 { PEN_IETF
, PA_SUBTYPE_IETF_OPERATING_SYSTEM
}
46 static imv_agent_t
*imv_os
;
49 * see section 3.8.1 of TCG TNC IF-IMV Specification 1.3
51 TNC_Result
TNC_IMV_Initialize(TNC_IMVID imv_id
,
52 TNC_Version min_version
,
53 TNC_Version max_version
,
54 TNC_Version
*actual_version
)
58 DBG1(DBG_IMV
, "IMV \"%s\" has already been initialized", imv_name
);
59 return TNC_RESULT_ALREADY_INITIALIZED
;
61 imv_os
= imv_agent_create(imv_name
, msg_types
, 1, imv_id
, actual_version
);
64 return TNC_RESULT_FATAL
;
66 if (min_version
> TNC_IFIMV_VERSION_1
|| max_version
< TNC_IFIMV_VERSION_1
)
68 DBG1(DBG_IMV
, "no common IF-IMV version");
69 return TNC_RESULT_NO_COMMON_VERSION
;
72 return TNC_RESULT_SUCCESS
;
76 * see section 3.8.2 of TCG TNC IF-IMV Specification 1.3
78 TNC_Result
TNC_IMV_NotifyConnectionChange(TNC_IMVID imv_id
,
79 TNC_ConnectionID connection_id
,
80 TNC_ConnectionState new_state
)
86 DBG1(DBG_IMV
, "IMV \"%s\" has not been initialized", imv_name
);
87 return TNC_RESULT_NOT_INITIALIZED
;
91 case TNC_CONNECTION_STATE_CREATE
:
92 state
= imv_os_state_create(connection_id
);
93 return imv_os
->create_state(imv_os
, state
);
94 case TNC_CONNECTION_STATE_DELETE
:
95 return imv_os
->delete_state(imv_os
, connection_id
);
97 return imv_os
->change_state(imv_os
, connection_id
,
102 static TNC_Result
receive_message(imv_state_t
*state
, imv_msg_t
*in_msg
)
105 imv_os_state_t
*os_state
;
106 enumerator_t
*enumerator
;
110 chunk_t os_name
= chunk_empty
;
111 chunk_t os_version
= chunk_empty
;
112 bool fatal_error
= FALSE
, assessment
= FALSE
;
115 /* parse received PA-TNC message and handle local and remote errors */
116 result
= in_msg
->receive(in_msg
, &fatal_error
);
117 if (result
!= TNC_RESULT_SUCCESS
)
122 out_msg
= imv_msg_create_as_reply(in_msg
);
124 /* analyze PA-TNC attributes */
125 enumerator
= in_msg
->create_attribute_enumerator(in_msg
);
126 while (enumerator
->enumerate(enumerator
, &attr
))
128 type
= attr
->get_type(attr
);
130 if (type
.vendor_id
!= PEN_IETF
)
136 case IETF_ATTR_PRODUCT_INFORMATION
:
138 ietf_attr_product_info_t
*attr_cast
;
140 attr_cast
= (ietf_attr_product_info_t
*)attr
;
141 os_name
= attr_cast
->get_info(attr_cast
, NULL
, NULL
);
142 DBG1(DBG_IMV
, "operating system name is '%.*s'",
143 os_name
.len
, os_name
.ptr
);
146 case IETF_ATTR_STRING_VERSION
:
148 ietf_attr_string_version_t
*attr_cast
;
150 attr_cast
= (ietf_attr_string_version_t
*)attr
;
151 os_version
= attr_cast
->get_version(attr_cast
, NULL
, NULL
);
154 DBG1(DBG_IMV
, "operating system version is '%.*s'",
155 os_version
.len
, os_version
.ptr
);
159 case IETF_ATTR_OPERATIONAL_STATUS
:
161 ietf_attr_op_status_t
*attr_cast
;
162 op_status_t op_status
;
163 op_result_t op_result
;
166 attr_cast
= (ietf_attr_op_status_t
*)attr
;
167 op_status
= attr_cast
->get_status(attr_cast
);
168 op_result
= attr_cast
->get_result(attr_cast
);
169 last_boot
= attr_cast
->get_last_use(attr_cast
);
170 DBG1(DBG_IMV
, "operational status: %N, result: %N",
171 op_status_names
, op_status
, op_result_names
, op_result
);
172 DBG1(DBG_IMV
, "last boot: %T", &last_boot
, TRUE
);
175 case IETF_ATTR_FORWARDING_ENABLED
:
177 ietf_attr_fwd_enabled_t
*attr_cast
;
178 os_fwd_status_t fwd_status
;
180 attr_cast
= (ietf_attr_fwd_enabled_t
*)attr
;
181 fwd_status
= attr_cast
->get_status(attr_cast
);
182 DBG1(DBG_IMV
, "IPv4 forwarding status: %N",
183 os_fwd_status_names
, fwd_status
);
186 case IETF_ATTR_FACTORY_DEFAULT_PWD_ENABLED
:
188 ietf_attr_default_pwd_enabled_t
*attr_cast
;
189 bool default_pwd_status
;
191 attr_cast
= (ietf_attr_default_pwd_enabled_t
*)attr
;
192 default_pwd_status
= attr_cast
->get_status(attr_cast
);
193 DBG1(DBG_IMV
, "factory default password: %sabled",
194 default_pwd_status ?
"en":"dis");
197 case IETF_ATTR_INSTALLED_PACKAGES
:
199 ietf_attr_installed_packages_t
*attr_cast
;
201 chunk_t name
, version
;
203 attr_cast
= (ietf_attr_installed_packages_t
*)attr
;
204 e
= attr_cast
->create_enumerator(attr_cast
);
205 while (e
->enumerate(e
, &name
, &version
))
207 DBG1(DBG_IMV
, "package '%.*s' %.*s", name
.len
, name
.ptr
,
208 version
.len
, version
.ptr
);
212 state
->set_recommendation(state
,
213 TNC_IMV_ACTION_RECOMMENDATION_ALLOW
,
214 TNC_IMV_EVALUATION_RESULT_COMPLIANT
);
222 enumerator
->destroy(enumerator
);
224 if (os_name
.len
&& os_version
.len
)
228 os_state
= (imv_os_state_t
*)state
;
229 os_state
->set_info(os_state
, os_name
, os_version
);
230 product_info
= os_state
->get_info(os_state
);
232 if (streq(product_info
, "Windows 1.2.3"))
234 DBG1(DBG_IMV
, "OS '%s' is not supported", product_info
);
236 state
->set_recommendation(state
,
237 TNC_IMV_ACTION_RECOMMENDATION_ISOLATE
,
238 TNC_IMV_EVALUATION_RESULT_NONCOMPLIANT_MINOR
);
243 DBG1(DBG_IMV
, "requesting installed packages for '%s'",
245 attr
= ietf_attr_attr_request_create(PEN_IETF
,
246 IETF_ATTR_INSTALLED_PACKAGES
);
247 out_msg
->add_attribute(out_msg
, attr
);
253 state
->set_recommendation(state
,
254 TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION
,
255 TNC_IMV_EVALUATION_RESULT_ERROR
);
261 result
= out_msg
->send_assessment(out_msg
);
262 out_msg
->destroy(out_msg
);
263 if (result
!= TNC_RESULT_SUCCESS
)
267 return imv_os
->provide_recommendation(imv_os
, state
);
270 /* send PA-TNC message with excl flag set */
271 result
= out_msg
->send(out_msg
, TRUE
);
272 out_msg
->destroy(out_msg
);
278 * see section 3.8.4 of TCG TNC IF-IMV Specification 1.3
280 TNC_Result
TNC_IMV_ReceiveMessage(TNC_IMVID imv_id
,
281 TNC_ConnectionID connection_id
,
282 TNC_BufferReference msg
,
284 TNC_MessageType msg_type
)
292 DBG1(DBG_IMV
, "IMV \"%s\" has not been initialized", imv_name
);
293 return TNC_RESULT_NOT_INITIALIZED
;
295 if (!imv_os
->get_state(imv_os
, connection_id
, &state
))
297 return TNC_RESULT_FATAL
;
299 in_msg
= imv_msg_create_from_data(imv_os
, state
, connection_id
, msg_type
,
300 chunk_create(msg
, msg_len
));
301 result
= receive_message(state
, in_msg
);
302 in_msg
->destroy(in_msg
);
308 * see section 3.8.6 of TCG TNC IF-IMV Specification 1.3
310 TNC_Result
TNC_IMV_ReceiveMessageLong(TNC_IMVID imv_id
,
311 TNC_ConnectionID connection_id
,
312 TNC_UInt32 msg_flags
,
313 TNC_BufferReference msg
,
315 TNC_VendorID msg_vid
,
316 TNC_MessageSubtype msg_subtype
,
317 TNC_UInt32 src_imc_id
,
318 TNC_UInt32 dst_imv_id
)
326 DBG1(DBG_IMV
, "IMV \"%s\" has not been initialized", imv_name
);
327 return TNC_RESULT_NOT_INITIALIZED
;
329 if (!imv_os
->get_state(imv_os
, connection_id
, &state
))
331 return TNC_RESULT_FATAL
;
333 in_msg
= imv_msg_create_from_long_data(imv_os
, state
, connection_id
,
334 src_imc_id
, dst_imv_id
, msg_vid
, msg_subtype
,
335 chunk_create(msg
, msg_len
));
336 result
=receive_message(state
, in_msg
);
337 in_msg
->destroy(in_msg
);
343 * see section 3.8.7 of TCG TNC IF-IMV Specification 1.3
345 TNC_Result
TNC_IMV_SolicitRecommendation(TNC_IMVID imv_id
,
346 TNC_ConnectionID connection_id
)
352 DBG1(DBG_IMV
, "IMV \"%s\" has not been initialized", imv_name
);
353 return TNC_RESULT_NOT_INITIALIZED
;
355 if (!imv_os
->get_state(imv_os
, connection_id
, &state
))
357 return TNC_RESULT_FATAL
;
359 return imv_os
->provide_recommendation(imv_os
, state
);
363 * see section 3.8.8 of TCG TNC IF-IMV Specification 1.3
365 TNC_Result
TNC_IMV_BatchEnding(TNC_IMVID imv_id
,
366 TNC_ConnectionID connection_id
)
369 imv_os_state_t
*os_state
;
370 TNC_Result result
= TNC_RESULT_SUCCESS
;
374 DBG1(DBG_IMV
, "IMV \"%s\" has not been initialized", imv_name
);
375 return TNC_RESULT_NOT_INITIALIZED
;
377 if (!imv_os
->get_state(imv_os
, connection_id
, &state
))
379 return TNC_RESULT_FATAL
;
381 os_state
= (imv_os_state_t
*)state
;
383 if (os_state
->get_info(os_state
) == NULL
)
387 ietf_attr_attr_request_t
*attr_cast
;
389 out_msg
= imv_msg_create(imv_os
, state
, connection_id
, imv_id
,
390 TNC_IMCID_ANY
, msg_types
[0]);
391 attr
= ietf_attr_attr_request_create(PEN_IETF
,
392 IETF_ATTR_PRODUCT_INFORMATION
);
393 attr_cast
= (ietf_attr_attr_request_t
*)attr
;
394 attr_cast
->add(attr_cast
, PEN_IETF
, IETF_ATTR_STRING_VERSION
);
395 attr_cast
->add(attr_cast
, PEN_IETF
, IETF_ATTR_OPERATIONAL_STATUS
);
396 attr_cast
->add(attr_cast
, PEN_IETF
, IETF_ATTR_FORWARDING_ENABLED
);
397 attr_cast
->add(attr_cast
, PEN_IETF
, IETF_ATTR_FACTORY_DEFAULT_PWD_ENABLED
);
398 out_msg
->add_attribute(out_msg
, attr
);
400 /* send PA-TNC message with excl flag not set */
401 result
= out_msg
->send(out_msg
, FALSE
);
402 out_msg
->destroy(out_msg
);
409 * see section 3.8.9 of TCG TNC IF-IMV Specification 1.3
411 TNC_Result
TNC_IMV_Terminate(TNC_IMVID imv_id
)
415 DBG1(DBG_IMV
, "IMV \"%s\" has not been initialized", imv_name
);
416 return TNC_RESULT_NOT_INITIALIZED
;
418 imv_os
->destroy(imv_os
);
421 return TNC_RESULT_SUCCESS
;
425 * see section 4.2.8.1 of TCG TNC IF-IMV Specification 1.3
427 TNC_Result
TNC_IMV_ProvideBindFunction(TNC_IMVID imv_id
,
428 TNC_TNCS_BindFunctionPointer bind_function
)
432 DBG1(DBG_IMV
, "IMV \"%s\" has not been initialized", imv_name
);
433 return TNC_RESULT_NOT_INITIALIZED
;
435 return imv_os
->bind_functions(imv_os
, bind_function
);