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 <pa_tnc/pa_tnc_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 #define IMV_VENDOR_ID PEN_IETF
43 #define IMV_SUBTYPE PA_SUBTYPE_IETF_OPERATING_SYSTEM
45 static imv_agent_t
*imv_os
;
48 * see section 3.8.1 of TCG TNC IF-IMV Specification 1.3
50 TNC_Result
TNC_IMV_Initialize(TNC_IMVID imv_id
,
51 TNC_Version min_version
,
52 TNC_Version max_version
,
53 TNC_Version
*actual_version
)
57 DBG1(DBG_IMV
, "IMV \"%s\" has already been initialized", imv_name
);
58 return TNC_RESULT_ALREADY_INITIALIZED
;
60 imv_os
= imv_agent_create(imv_name
, IMV_VENDOR_ID
, IMV_SUBTYPE
,
61 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(TNC_IMVID imv_id
,
103 TNC_ConnectionID connection_id
,
104 TNC_UInt32 msg_flags
,
106 TNC_VendorID msg_vid
,
107 TNC_MessageSubtype msg_subtype
,
108 TNC_UInt32 src_imc_id
,
109 TNC_UInt32 dst_imv_id
)
111 pa_tnc_msg_t
*pa_tnc_msg
;
114 linked_list_t
*attr_list
;
116 imv_os_state_t
*os_state
;
117 enumerator_t
*enumerator
;
119 chunk_t os_name
= chunk_empty
;
120 chunk_t os_version
= chunk_empty
;
121 bool fatal_error
, assessment
= FALSE
;
125 DBG1(DBG_IMV
, "IMV \"%s\" has not been initialized", imv_name
);
126 return TNC_RESULT_NOT_INITIALIZED
;
129 /* get current IMV state */
130 if (!imv_os
->get_state(imv_os
, connection_id
, &state
))
132 return TNC_RESULT_FATAL
;
134 os_state
= (imv_os_state_t
*)state
;
136 /* parse received PA-TNC message and automatically handle any errors */
137 result
= imv_os
->receive_message(imv_os
, state
, msg
, msg_vid
,
138 msg_subtype
, src_imc_id
, dst_imv_id
, &pa_tnc_msg
);
140 /* no parsed PA-TNC attributes available if an error occurred */
146 /* preprocess any IETF standard error attributes */
147 fatal_error
= pa_tnc_msg
->process_ietf_std_errors(pa_tnc_msg
);
149 /* analyze PA-TNC attributes */
150 attr_list
= linked_list_create();
151 enumerator
= pa_tnc_msg
->create_attribute_enumerator(pa_tnc_msg
);
152 while (enumerator
->enumerate(enumerator
, &attr
))
154 type
= attr
->get_type(attr
);
156 if (type
.vendor_id
!= PEN_IETF
)
162 case IETF_ATTR_PRODUCT_INFORMATION
:
164 ietf_attr_product_info_t
*attr_cast
;
166 attr_cast
= (ietf_attr_product_info_t
*)attr
;
167 os_name
= attr_cast
->get_info(attr_cast
, NULL
, NULL
);
168 DBG1(DBG_IMV
, "operating system name is '%.*s'",
169 os_name
.len
, os_name
.ptr
);
172 case IETF_ATTR_STRING_VERSION
:
174 ietf_attr_string_version_t
*attr_cast
;
176 attr_cast
= (ietf_attr_string_version_t
*)attr
;
177 os_version
= attr_cast
->get_version(attr_cast
, NULL
, NULL
);
180 DBG1(DBG_IMV
, "operating system version is '%.*s'",
181 os_version
.len
, os_version
.ptr
);
185 case IETF_ATTR_OPERATIONAL_STATUS
:
187 ietf_attr_op_status_t
*attr_cast
;
188 op_status_t op_status
;
189 op_result_t op_result
;
192 attr_cast
= (ietf_attr_op_status_t
*)attr
;
193 op_status
= attr_cast
->get_status(attr_cast
);
194 op_result
= attr_cast
->get_result(attr_cast
);
195 last_boot
= attr_cast
->get_last_use(attr_cast
);
196 DBG1(DBG_IMV
, "operational status: %N, result: %N",
197 op_status_names
, op_status
, op_result_names
, op_result
);
198 DBG1(DBG_IMV
, "last boot: %T", &last_boot
, TRUE
);
201 case IETF_ATTR_FORWARDING_ENABLED
:
203 ietf_attr_fwd_enabled_t
*attr_cast
;
204 os_fwd_status_t fwd_status
;
206 attr_cast
= (ietf_attr_fwd_enabled_t
*)attr
;
207 fwd_status
= attr_cast
->get_status(attr_cast
);
208 DBG1(DBG_IMV
, "IPv4 forwarding status: %N",
209 os_fwd_status_names
, fwd_status
);
212 case IETF_ATTR_FACTORY_DEFAULT_PWD_ENABLED
:
214 ietf_attr_default_pwd_enabled_t
*attr_cast
;
215 bool default_pwd_status
;
217 attr_cast
= (ietf_attr_default_pwd_enabled_t
*)attr
;
218 default_pwd_status
= attr_cast
->get_status(attr_cast
);
219 DBG1(DBG_IMV
, "factory default password: %sabled",
220 default_pwd_status ?
"en":"dis");
223 case IETF_ATTR_INSTALLED_PACKAGES
:
225 ietf_attr_installed_packages_t
*attr_cast
;
227 chunk_t name
, version
;
229 attr_cast
= (ietf_attr_installed_packages_t
*)attr
;
230 e
= attr_cast
->create_enumerator(attr_cast
);
231 while (e
->enumerate(e
, &name
, &version
))
233 DBG1(DBG_IMV
, "package '%.*s' %.*s", name
.len
, name
.ptr
,
234 version
.len
, version
.ptr
);
238 state
->set_recommendation(state
,
239 TNC_IMV_ACTION_RECOMMENDATION_ALLOW
,
240 TNC_IMV_EVALUATION_RESULT_COMPLIANT
);
248 enumerator
->destroy(enumerator
);
250 if (os_name
.len
&& os_version
.len
)
254 os_state
->set_info(os_state
, os_name
, os_version
);
255 product_info
= os_state
->get_info(os_state
);
257 if (streq(product_info
, "Windows 1.2.3"))
259 DBG1(DBG_IMV
, "OS '%s' is not supported", product_info
);
261 state
->set_recommendation(state
,
262 TNC_IMV_ACTION_RECOMMENDATION_ISOLATE
,
263 TNC_IMV_EVALUATION_RESULT_NONCOMPLIANT_MINOR
);
268 DBG1(DBG_IMV
, "requesting installed packages for '%s'",
270 attr
= ietf_attr_attr_request_create(PEN_IETF
,
271 IETF_ATTR_INSTALLED_PACKAGES
);
272 attr_list
->insert_last(attr_list
, attr
);
275 pa_tnc_msg
->destroy(pa_tnc_msg
);
279 state
->set_recommendation(state
,
280 TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION
,
281 TNC_IMV_EVALUATION_RESULT_ERROR
);
287 attr_list
->destroy_offset(attr_list
, offsetof(pa_tnc_attr_t
, destroy
));
288 return imv_os
->provide_recommendation(imv_os
, connection_id
, src_imc_id
);
291 result
= imv_os
->send_message(imv_os
, connection_id
, TRUE
, imv_id
,
292 src_imc_id
, attr_list
);
293 attr_list
->destroy(attr_list
);
299 * see section 3.8.4 of TCG TNC IF-IMV Specification 1.3
301 TNC_Result
TNC_IMV_ReceiveMessage(TNC_IMVID imv_id
,
302 TNC_ConnectionID connection_id
,
303 TNC_BufferReference msg
,
305 TNC_MessageType msg_type
)
307 TNC_VendorID msg_vid
;
308 TNC_MessageSubtype msg_subtype
;
310 msg_vid
= msg_type
>> 8;
311 msg_subtype
= msg_type
& TNC_SUBTYPE_ANY
;
313 return receive_message(imv_id
, connection_id
, 0, chunk_create(msg
, msg_len
),
314 msg_vid
, msg_subtype
, 0, TNC_IMVID_ANY
);
318 * see section 3.8.6 of TCG TNC IF-IMV Specification 1.3
320 TNC_Result
TNC_IMV_ReceiveMessageLong(TNC_IMVID imv_id
,
321 TNC_ConnectionID connection_id
,
322 TNC_UInt32 msg_flags
,
323 TNC_BufferReference msg
,
325 TNC_VendorID msg_vid
,
326 TNC_MessageSubtype msg_subtype
,
327 TNC_UInt32 src_imc_id
,
328 TNC_UInt32 dst_imv_id
)
330 return receive_message(imv_id
, connection_id
, msg_flags
,
331 chunk_create(msg
, msg_len
), msg_vid
, msg_subtype
,
332 src_imc_id
, dst_imv_id
);
336 * see section 3.8.7 of TCG TNC IF-IMV Specification 1.3
338 TNC_Result
TNC_IMV_SolicitRecommendation(TNC_IMVID imv_id
,
339 TNC_ConnectionID connection_id
)
343 DBG1(DBG_IMV
, "IMV \"%s\" has not been initialized", imv_name
);
344 return TNC_RESULT_NOT_INITIALIZED
;
346 return imv_os
->provide_recommendation(imv_os
, connection_id
,
351 * see section 3.8.8 of TCG TNC IF-IMV Specification 1.3
353 TNC_Result
TNC_IMV_BatchEnding(TNC_IMVID imv_id
,
354 TNC_ConnectionID connection_id
)
357 imv_os_state_t
*os_state
;
358 TNC_Result result
= TNC_RESULT_SUCCESS
;
362 DBG1(DBG_IMV
, "IMV \"%s\" has not been initialized", imv_name
);
363 return TNC_RESULT_NOT_INITIALIZED
;
366 /* get current IMV state */
367 if (!imv_os
->get_state(imv_os
, connection_id
, &state
))
369 return TNC_RESULT_FATAL
;
371 os_state
= (imv_os_state_t
*)state
;
373 if (os_state
->get_info(os_state
) == NULL
)
376 linked_list_t
*attr_list
;
377 ietf_attr_attr_request_t
*attr_cast
;
379 attr_list
= linked_list_create();
380 attr
= ietf_attr_attr_request_create(PEN_IETF
,
381 IETF_ATTR_PRODUCT_INFORMATION
);
382 attr_cast
= (ietf_attr_attr_request_t
*)attr
;
383 attr_cast
->add(attr_cast
, PEN_IETF
, IETF_ATTR_STRING_VERSION
);
384 attr_cast
->add(attr_cast
, PEN_IETF
, IETF_ATTR_OPERATIONAL_STATUS
);
385 attr_cast
->add(attr_cast
, PEN_IETF
, IETF_ATTR_FORWARDING_ENABLED
);
386 attr_cast
->add(attr_cast
, PEN_IETF
, IETF_ATTR_FACTORY_DEFAULT_PWD_ENABLED
);
387 attr_list
->insert_last(attr_list
, attr
);
388 result
= imv_os
->send_message(imv_os
, connection_id
, FALSE
, imv_id
,
389 TNC_IMCID_ANY
, attr_list
);
390 attr_list
->destroy(attr_list
);
397 * see section 3.8.9 of TCG TNC IF-IMV Specification 1.3
399 TNC_Result
TNC_IMV_Terminate(TNC_IMVID imv_id
)
403 DBG1(DBG_IMV
, "IMV \"%s\" has not been initialized", imv_name
);
404 return TNC_RESULT_NOT_INITIALIZED
;
406 imv_os
->destroy(imv_os
);
409 return TNC_RESULT_SUCCESS
;
413 * see section 4.2.8.1 of TCG TNC IF-IMV Specification 1.3
415 TNC_Result
TNC_IMV_ProvideBindFunction(TNC_IMVID imv_id
,
416 TNC_TNCS_BindFunctionPointer bind_function
)
420 DBG1(DBG_IMV
, "IMV \"%s\" has not been initialized", imv_name
);
421 return TNC_RESULT_NOT_INITIALIZED
;
423 return imv_os
->bind_functions(imv_os
, bind_function
);