2 * Copyright (C) 2011-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 "imc_os_state.h"
18 #include <imc/imc_agent.h>
19 #include <imc/imc_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_product_info.h>
28 #include <ietf/ietf_attr_string_version.h>
29 #include <ita/ita_attr.h>
30 #include <ita/ita_attr_get_settings.h>
31 #include <ita/ita_attr_settings.h>
32 #include <ita/ita_attr_angel.h>
33 #include <ita/ita_attr_device_id.h>
34 #include <os_info/os_info.h>
36 #include <tncif_pa_subtypes.h>
39 #include <utils/debug.h>
43 static const char imc_name
[] = "OS";
45 static pen_type_t msg_types
[] = {
46 { PEN_IETF
, PA_SUBTYPE_IETF_OPERATING_SYSTEM
}
49 static imc_agent_t
*imc_os
;
53 * see section 3.8.1 of TCG TNC IF-IMC Specification 1.3
55 TNC_Result
TNC_IMC_Initialize(TNC_IMCID imc_id
,
56 TNC_Version min_version
,
57 TNC_Version max_version
,
58 TNC_Version
*actual_version
)
62 DBG1(DBG_IMC
, "IMC \"%s\" has already been initialized", imc_name
);
63 return TNC_RESULT_ALREADY_INITIALIZED
;
65 imc_os
= imc_agent_create(imc_name
, msg_types
, countof(msg_types
),
66 imc_id
, actual_version
);
69 return TNC_RESULT_FATAL
;
72 os
= os_info_create();
75 imc_os
->destroy(imc_os
);
78 return TNC_RESULT_FATAL
;
81 if (min_version
> TNC_IFIMC_VERSION_1
|| max_version
< TNC_IFIMC_VERSION_1
)
83 DBG1(DBG_IMC
, "no common IF-IMC version");
84 return TNC_RESULT_NO_COMMON_VERSION
;
86 return TNC_RESULT_SUCCESS
;
90 * see section 3.8.2 of TCG TNC IF-IMC Specification 1.3
92 TNC_Result
TNC_IMC_NotifyConnectionChange(TNC_IMCID imc_id
,
93 TNC_ConnectionID connection_id
,
94 TNC_ConnectionState new_state
)
100 DBG1(DBG_IMC
, "IMC \"%s\" has not been initialized", imc_name
);
101 return TNC_RESULT_NOT_INITIALIZED
;
105 case TNC_CONNECTION_STATE_CREATE
:
106 state
= imc_os_state_create(connection_id
);
107 return imc_os
->create_state(imc_os
, state
);
108 case TNC_CONNECTION_STATE_HANDSHAKE
:
109 if (imc_os
->change_state(imc_os
, connection_id
, new_state
,
110 &state
) != TNC_RESULT_SUCCESS
)
112 return TNC_RESULT_FATAL
;
114 state
->set_result(state
, imc_id
,
115 TNC_IMV_EVALUATION_RESULT_DONT_KNOW
);
116 return TNC_RESULT_SUCCESS
;
117 case TNC_CONNECTION_STATE_DELETE
:
118 return imc_os
->delete_state(imc_os
, connection_id
);
120 return imc_os
->change_state(imc_os
, connection_id
,
126 * Add IETF Product Information attribute to the send queue
128 static void add_product_info(imc_msg_t
*msg
)
132 pen_t vendor_id
= PEN_IETF
;
135 typedef struct vendor_pen_t
{
140 vendor_pen_t vendor_pens
[] = {
141 { OS_TYPE_DEBIAN
, PEN_DEBIAN
},
142 { OS_TYPE_UBUNTU
, PEN_CANONICAL
},
143 { OS_TYPE_FEDORA
, PEN_FEDORA
},
144 { OS_TYPE_REDHAT
, PEN_REDHAT
},
145 { OS_TYPE_ANDROID
, PEN_GOOGLE
}
148 os_type
= os
->get_type(os
);
149 for (i
= 0; i
< countof(vendor_pens
); i
++)
151 if (os_type
== vendor_pens
[i
].os_type
)
153 vendor_id
= vendor_pens
[i
].pen
;
157 attr
= ietf_attr_product_info_create(vendor_id
, 0, os
->get_name(os
));
158 msg
->add_attribute(msg
, attr
);
162 * Add IETF Numeric Version attribute to the send queue
164 static void add_numeric_version(imc_msg_t
*msg
)
167 u_int32_t major
, minor
;
169 os
->get_numeric_version(os
, &major
, &minor
);
170 DBG1(DBG_IMC
, "operating system numeric version is %d.%d",
173 attr
= ietf_attr_numeric_version_create(major
, minor
, 0, 0, 0);
174 msg
->add_attribute(msg
, attr
);
178 * Add IETF String Version attribute to the send queue
180 static void add_string_version(imc_msg_t
*msg
)
184 attr
= ietf_attr_string_version_create(os
->get_version(os
),
185 chunk_empty
, chunk_empty
);
186 msg
->add_attribute(msg
, attr
);
190 * Add IETF Operational Status attribute to the send queue
192 static void add_op_status(imc_msg_t
*msg
)
195 time_t uptime
, last_boot
;
197 uptime
= os
->get_uptime(os
);
198 last_boot
= uptime ?
time(NULL
) - uptime
: UNDEFINED_TIME
;
199 if (last_boot
!= UNDEFINED_TIME
)
201 DBG1(DBG_IMC
, "last boot: %T, %u s ago", &last_boot
, TRUE
, uptime
);
203 attr
= ietf_attr_op_status_create(OP_STATUS_OPERATIONAL
,
204 OP_RESULT_SUCCESSFUL
, last_boot
);
205 msg
->add_attribute(msg
, attr
);
209 * Add IETF Forwarding Enabled attribute to the send queue
211 static void add_fwd_enabled(imc_msg_t
*msg
)
214 os_fwd_status_t fwd_status
;
216 fwd_status
= os
->get_fwd_status(os
);
217 DBG1(DBG_IMC
, "IPv4 forwarding is %N",
218 os_fwd_status_names
, fwd_status
);
219 attr
= ietf_attr_fwd_enabled_create(fwd_status
);
220 msg
->add_attribute(msg
, attr
);
224 * Add IETF Factory Default Password Enabled attribute to the send queue
226 static void add_default_pwd_enabled(imc_msg_t
*msg
)
230 DBG1(DBG_IMC
, "factory default password is disabled");
231 attr
= ietf_attr_default_pwd_enabled_create(FALSE
);
232 msg
->add_attribute(msg
, attr
);
236 * Add ITA Device ID attribute to the send queue
238 static void add_device_id(imc_msg_t
*msg
)
244 name
= os
->get_type(os
) == OS_TYPE_ANDROID ?
245 "android_id" : "/var/lib/dbus/machine-id";
246 value
= os
->get_setting(os
, name
);
250 DBG1(DBG_IMC
, "no device ID available");
254 /* trim trailing newline character */
255 if (value
.ptr
[value
.len
- 1] == '\n')
260 DBG1(DBG_IMC
, "device ID is %.*s", value
.len
, value
.ptr
);
261 attr
= ita_attr_device_id_create(value
);
262 msg
->add_attribute(msg
, attr
);
267 * Add an IETF Installed Packages attribute to the send queue
269 static void add_installed_packages(imc_state_t
*state
, imc_msg_t
*msg
)
271 pa_tnc_attr_t
*attr
= NULL
, *attr_angel
;
272 ietf_attr_installed_packages_t
*attr_cast
;
273 enumerator_t
*enumerator
;
274 chunk_t name
, version
;
275 size_t max_attr_size
, attr_size
, entry_size
;
279 * Compute the maximum IETF Installed Packages attribute size
280 * leaving space for an additional ITA Angel attribute
282 max_attr_size
= state
->get_max_msg_len(state
) - 8 - 12;
284 /* At least one IETF Installed Packages attribute is sent */
285 attr
= ietf_attr_installed_packages_create();
288 enumerator
= os
->create_package_enumerator(os
);
291 while (enumerator
->enumerate(enumerator
, &name
, &version
))
293 DBG2(DBG_IMC
, "package '%.*s' (%.*s)",
294 name
.len
, name
.ptr
, version
.len
, version
.ptr
);
296 entry_size
= 2 + name
.len
+ version
.len
;
297 if (attr_size
+ entry_size
> max_attr_size
)
302 * Send an ITA Start Angel attribute to the IMV signalling
303 * that multiple ITA Installed Package attributes follow.
305 attr_angel
= ita_attr_angel_create(TRUE
);
306 msg
->add_attribute(msg
, attr_angel
);
309 msg
->add_attribute(msg
, attr
);
311 /* create the next IETF Installed Packages attribute */
312 attr
= ietf_attr_installed_packages_create();
315 attr_cast
= (ietf_attr_installed_packages_t
*)attr
;
316 attr_cast
->add(attr_cast
, name
, version
);
317 attr_size
+= entry_size
;
319 enumerator
->destroy(enumerator
);
321 msg
->add_attribute(msg
, attr
);
326 * If we sent an ITA Start Angel attribute in the first place,
327 * terminate by appending a matching ITA Stop Angel attribute.
329 attr_angel
= ita_attr_angel_create(FALSE
);
330 msg
->add_attribute(msg
, attr_angel
);
335 * Add ITA Settings attribute to the send queue
337 static void add_settings(enumerator_t
*enumerator
, imc_msg_t
*msg
)
339 pa_tnc_attr_t
*attr
= NULL
;
340 ita_attr_settings_t
*attr_cast
;
345 while (enumerator
->enumerate(enumerator
, &name
))
347 DBG1(DBG_IMC
, "setting '%s'", name
);
349 value
= os
->get_setting(os
, name
);
356 attr
= ita_attr_settings_create();
359 attr_cast
= (ita_attr_settings_t
*)attr
;
360 attr_cast
->add(attr_cast
, name
, value
);
366 msg
->add_attribute(msg
, attr
);
371 * see section 3.8.3 of TCG TNC IF-IMC Specification 1.3
373 TNC_Result
TNC_IMC_BeginHandshake(TNC_IMCID imc_id
,
374 TNC_ConnectionID connection_id
)
378 TNC_Result result
= TNC_RESULT_SUCCESS
;
382 DBG1(DBG_IMC
, "IMC \"%s\" has not been initialized", imc_name
);
383 return TNC_RESULT_NOT_INITIALIZED
;
385 if (!imc_os
->get_state(imc_os
, connection_id
, &state
))
387 return TNC_RESULT_FATAL
;
389 if (lib
->settings
->get_bool(lib
->settings
,
390 "libimcv.plugins.imc-os.push_info", TRUE
))
392 out_msg
= imc_msg_create(imc_os
, state
, connection_id
, imc_id
,
393 TNC_IMVID_ANY
, msg_types
[0]);
394 add_product_info(out_msg
);
395 add_string_version(out_msg
);
396 add_numeric_version(out_msg
);
397 add_op_status(out_msg
);
398 add_fwd_enabled(out_msg
);
399 add_default_pwd_enabled(out_msg
);
400 add_device_id(out_msg
);
402 /* send PA-TNC message with the excl flag not set */
403 result
= out_msg
->send(out_msg
, FALSE
);
404 out_msg
->destroy(out_msg
);
410 static TNC_Result
receive_message(imc_state_t
*state
, imc_msg_t
*in_msg
)
413 enumerator_t
*enumerator
;
417 bool fatal_error
= FALSE
;
419 /* parse received PA-TNC message and handle local and remote errors */
420 result
= in_msg
->receive(in_msg
, &fatal_error
);
421 if (result
!= TNC_RESULT_SUCCESS
)
425 out_msg
= imc_msg_create_as_reply(in_msg
);
427 /* analyze PA-TNC attributes */
428 enumerator
= in_msg
->create_attribute_enumerator(in_msg
);
429 while (enumerator
->enumerate(enumerator
, &attr
))
431 type
= attr
->get_type(attr
);
433 if (type
.vendor_id
== PEN_IETF
)
435 if (type
.type
== IETF_ATTR_ATTRIBUTE_REQUEST
)
437 ietf_attr_attr_request_t
*attr_cast
;
441 attr_cast
= (ietf_attr_attr_request_t
*)attr
;
443 e
= attr_cast
->create_enumerator(attr_cast
);
444 while (e
->enumerate(e
, &entry
))
446 if (entry
->vendor_id
== PEN_IETF
)
450 case IETF_ATTR_PRODUCT_INFORMATION
:
451 add_product_info(out_msg
);
453 case IETF_ATTR_STRING_VERSION
:
454 add_string_version(out_msg
);
456 case IETF_ATTR_NUMERIC_VERSION
:
457 add_numeric_version(out_msg
);
459 case IETF_ATTR_OPERATIONAL_STATUS
:
460 add_op_status(out_msg
);
462 case IETF_ATTR_FORWARDING_ENABLED
:
463 add_fwd_enabled(out_msg
);
465 case IETF_ATTR_FACTORY_DEFAULT_PWD_ENABLED
:
466 add_default_pwd_enabled(out_msg
);
468 case IETF_ATTR_INSTALLED_PACKAGES
:
469 add_installed_packages(state
, out_msg
);
475 else if (entry
->vendor_id
== PEN_ITA
)
479 case ITA_ATTR_DEVICE_ID
:
480 add_device_id(out_msg
);
490 else if (type
.vendor_id
== PEN_ITA
&& type
.type
== ITA_ATTR_GET_SETTINGS
)
492 ita_attr_get_settings_t
*attr_cast
;
495 attr_cast
= (ita_attr_get_settings_t
*)attr
;
497 e
= attr_cast
->create_enumerator(attr_cast
);
498 add_settings(e
, out_msg
);
502 enumerator
->destroy(enumerator
);
506 result
= TNC_RESULT_FATAL
;
510 result
= out_msg
->send(out_msg
, TRUE
);
512 out_msg
->destroy(out_msg
);
518 * see section 3.8.4 of TCG TNC IF-IMC Specification 1.3
521 TNC_Result
TNC_IMC_ReceiveMessage(TNC_IMCID imc_id
,
522 TNC_ConnectionID connection_id
,
523 TNC_BufferReference msg
,
525 TNC_MessageType msg_type
)
533 DBG1(DBG_IMC
, "IMC \"%s\" has not been initialized", imc_name
);
534 return TNC_RESULT_NOT_INITIALIZED
;
536 if (!imc_os
->get_state(imc_os
, connection_id
, &state
))
538 return TNC_RESULT_FATAL
;
540 in_msg
= imc_msg_create_from_data(imc_os
, state
, connection_id
, msg_type
,
541 chunk_create(msg
, msg_len
));
542 result
= receive_message(state
, in_msg
);
543 in_msg
->destroy(in_msg
);
549 * see section 3.8.6 of TCG TNC IF-IMV Specification 1.3
551 TNC_Result
TNC_IMC_ReceiveMessageLong(TNC_IMCID imc_id
,
552 TNC_ConnectionID connection_id
,
553 TNC_UInt32 msg_flags
,
554 TNC_BufferReference msg
,
556 TNC_VendorID msg_vid
,
557 TNC_MessageSubtype msg_subtype
,
558 TNC_UInt32 src_imv_id
,
559 TNC_UInt32 dst_imc_id
)
567 DBG1(DBG_IMC
, "IMC \"%s\" has not been initialized", imc_name
);
568 return TNC_RESULT_NOT_INITIALIZED
;
570 if (!imc_os
->get_state(imc_os
, connection_id
, &state
))
572 return TNC_RESULT_FATAL
;
574 in_msg
= imc_msg_create_from_long_data(imc_os
, state
, connection_id
,
575 src_imv_id
, dst_imc_id
,msg_vid
, msg_subtype
,
576 chunk_create(msg
, msg_len
));
577 result
=receive_message(state
, in_msg
);
578 in_msg
->destroy(in_msg
);
584 * see section 3.8.7 of TCG TNC IF-IMC Specification 1.3
586 TNC_Result
TNC_IMC_BatchEnding(TNC_IMCID imc_id
,
587 TNC_ConnectionID connection_id
)
591 DBG1(DBG_IMC
, "IMC \"%s\" has not been initialized", imc_name
);
592 return TNC_RESULT_NOT_INITIALIZED
;
594 return TNC_RESULT_SUCCESS
;
598 * see section 3.8.8 of TCG TNC IF-IMC Specification 1.3
600 TNC_Result
TNC_IMC_Terminate(TNC_IMCID imc_id
)
604 DBG1(DBG_IMC
, "IMC \"%s\" has not been initialized", imc_name
);
605 return TNC_RESULT_NOT_INITIALIZED
;
607 imc_os
->destroy(imc_os
);
613 return TNC_RESULT_SUCCESS
;
617 * see section 4.2.8.1 of TCG TNC IF-IMC Specification 1.3
619 TNC_Result
TNC_IMC_ProvideBindFunction(TNC_IMCID imc_id
,
620 TNC_TNCC_BindFunctionPointer bind_function
)
624 DBG1(DBG_IMC
, "IMC \"%s\" has not been initialized", imc_name
);
625 return TNC_RESULT_NOT_INITIALIZED
;
627 return imc_os
->bind_functions(imc_os
, bind_function
);