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_remediation_instr.h>
29 #include <ietf/ietf_attr_string_version.h>
30 #include <ita/ita_attr.h>
31 #include <ita/ita_attr_get_settings.h>
32 #include <ita/ita_attr_settings.h>
33 #include <os_info/os_info.h>
35 #include <tncif_pa_subtypes.h>
38 #include <utils/debug.h>
42 static const char imc_name
[] = "OS";
44 static pen_type_t msg_types
[] = {
45 { PEN_IETF
, PA_SUBTYPE_IETF_OPERATING_SYSTEM
}
48 static imc_agent_t
*imc_os
;
52 * see section 3.8.1 of TCG TNC IF-IMC Specification 1.3
54 TNC_Result
TNC_IMC_Initialize(TNC_IMCID imc_id
,
55 TNC_Version min_version
,
56 TNC_Version max_version
,
57 TNC_Version
*actual_version
)
61 DBG1(DBG_IMC
, "IMC \"%s\" has already been initialized", imc_name
);
62 return TNC_RESULT_ALREADY_INITIALIZED
;
64 imc_os
= imc_agent_create(imc_name
, msg_types
, countof(msg_types
),
65 imc_id
, actual_version
);
68 return TNC_RESULT_FATAL
;
71 os
= os_info_create();
74 imc_os
->destroy(imc_os
);
77 return TNC_RESULT_FATAL
;
80 if (min_version
> TNC_IFIMC_VERSION_1
|| max_version
< TNC_IFIMC_VERSION_1
)
82 DBG1(DBG_IMC
, "no common IF-IMC version");
83 return TNC_RESULT_NO_COMMON_VERSION
;
85 return TNC_RESULT_SUCCESS
;
89 * see section 3.8.2 of TCG TNC IF-IMC Specification 1.3
91 TNC_Result
TNC_IMC_NotifyConnectionChange(TNC_IMCID imc_id
,
92 TNC_ConnectionID connection_id
,
93 TNC_ConnectionState new_state
)
99 DBG1(DBG_IMC
, "IMC \"%s\" has not been initialized", imc_name
);
100 return TNC_RESULT_NOT_INITIALIZED
;
104 case TNC_CONNECTION_STATE_CREATE
:
105 state
= imc_os_state_create(connection_id
);
106 return imc_os
->create_state(imc_os
, state
);
107 case TNC_CONNECTION_STATE_HANDSHAKE
:
108 if (imc_os
->change_state(imc_os
, connection_id
, new_state
,
109 &state
) != TNC_RESULT_SUCCESS
)
111 return TNC_RESULT_FATAL
;
113 state
->set_result(state
, imc_id
,
114 TNC_IMV_EVALUATION_RESULT_DONT_KNOW
);
115 return TNC_RESULT_SUCCESS
;
116 case TNC_CONNECTION_STATE_DELETE
:
117 return imc_os
->delete_state(imc_os
, connection_id
);
119 return imc_os
->change_state(imc_os
, connection_id
,
125 * Add IETF Product Information attribute to the send queue
127 static void add_product_info(imc_msg_t
*msg
)
131 pen_t vendor_id
= PEN_IETF
;
135 typedef struct vendor_pen_t
{
140 vendor_pen_t vendor_pens
[] = {
141 { "Debian", PEN_DEBIAN
},
142 { "Ubuntu", PEN_CANONICAL
}
145 os_name
= os
->get_name(os
);
146 for (i
= 0; i
< countof(vendor_pens
); i
++)
148 vendor
= vendor_pens
[i
].vendor
;
149 if (chunk_equals(os_name
, chunk_create(vendor
, strlen(vendor
))))
151 vendor_id
= vendor_pens
[i
].pen
;
155 attr
= ietf_attr_product_info_create(vendor_id
, 0, os_name
);
156 msg
->add_attribute(msg
, attr
);
160 * Add IETF Numeric Version attribute to the send queue
162 static void add_numeric_version(imc_msg_t
*msg
)
165 u_int32_t major
, minor
;
167 os
->get_numeric_version(os
, &major
, &minor
);
168 DBG1(DBG_IMC
, "operating system numeric version is %d.%d",
171 attr
= ietf_attr_numeric_version_create(major
, minor
, 0, 0, 0);
172 msg
->add_attribute(msg
, attr
);
176 * Add IETF String Version attribute to the send queue
178 static void add_string_version(imc_msg_t
*msg
)
182 attr
= ietf_attr_string_version_create(os
->get_version(os
),
183 chunk_empty
, chunk_empty
);
184 msg
->add_attribute(msg
, attr
);
188 * Add IETF Operational Status attribute to the send queue
190 static void add_op_status(imc_msg_t
*msg
)
193 time_t uptime
, last_boot
;
195 uptime
= os
->get_uptime(os
);
196 last_boot
= uptime ?
time(NULL
) - uptime
: UNDEFINED_TIME
;
197 if (last_boot
!= UNDEFINED_TIME
)
199 DBG1(DBG_IMC
, "last boot: %T, %u s ago", &last_boot
, TRUE
, uptime
);
201 attr
= ietf_attr_op_status_create(OP_STATUS_OPERATIONAL
,
202 OP_RESULT_SUCCESSFUL
, last_boot
);
203 msg
->add_attribute(msg
, attr
);
207 * Add IETF Forwarding Enabled attribute to the send queue
209 static void add_fwd_enabled(imc_msg_t
*msg
)
212 os_fwd_status_t fwd_status
;
214 fwd_status
= os
->get_fwd_status(os
);
215 DBG1(DBG_IMC
, "IPv4 forwarding status: %N",
216 os_fwd_status_names
, fwd_status
);
217 attr
= ietf_attr_fwd_enabled_create(fwd_status
);
218 msg
->add_attribute(msg
, attr
);
222 * Add IETF Factory Default Password Enabled attribute to the send queue
224 static void add_default_pwd_enabled(imc_msg_t
*msg
)
228 DBG1(DBG_IMC
, "factory default password: disabled");
229 attr
= ietf_attr_default_pwd_enabled_create(FALSE
);
230 msg
->add_attribute(msg
, attr
);
234 * Add an IETF Installed Packages attribute to the send queue
236 static void add_installed_packages(imc_msg_t
*msg
)
239 ietf_attr_installed_packages_t
*attr_cast
;
240 chunk_t libc_name
= { "libc-bin", 8 };
241 chunk_t libc_version
= { "2.15-0ubuntu10.2", 16 };
242 chunk_t selinux_name
= { "selinux", 7 };
243 chunk_t selinux_version
= { "1:0.11", 6 };
245 attr
= ietf_attr_installed_packages_create();
246 attr_cast
= (ietf_attr_installed_packages_t
*)attr
;
247 attr_cast
->add(attr_cast
, libc_name
, libc_version
);
248 attr_cast
->add(attr_cast
, selinux_name
, selinux_version
);
249 msg
->add_attribute(msg
, attr
);
253 * Add ITA Settings attribute to the send queue
255 static void add_settings(enumerator_t
*enumerator
, imc_msg_t
*msg
)
257 pa_tnc_attr_t
*attr
= NULL
;
258 ita_attr_settings_t
*attr_cast
;
263 while (enumerator
->enumerate(enumerator
, &name
))
265 DBG1(DBG_IMC
, "setting '%s'", name
);
267 value
= os
->get_setting(os
, name
);
270 DBG1(DBG_IMC
, " failed to get setting");
275 attr
= ita_attr_settings_create();
278 attr_cast
= (ita_attr_settings_t
*)attr
;
279 attr_cast
->add(attr_cast
, name
, value
);
285 msg
->add_attribute(msg
, attr
);
290 * see section 3.8.3 of TCG TNC IF-IMC Specification 1.3
292 TNC_Result
TNC_IMC_BeginHandshake(TNC_IMCID imc_id
,
293 TNC_ConnectionID connection_id
)
297 TNC_Result result
= TNC_RESULT_SUCCESS
;
301 DBG1(DBG_IMC
, "IMC \"%s\" has not been initialized", imc_name
);
302 return TNC_RESULT_NOT_INITIALIZED
;
304 if (!imc_os
->get_state(imc_os
, connection_id
, &state
))
306 return TNC_RESULT_FATAL
;
308 if (lib
->settings
->get_bool(lib
->settings
,
309 "libimcv.plugins.imc-os.send_info", TRUE
))
311 out_msg
= imc_msg_create(imc_os
, state
, connection_id
, imc_id
,
312 TNC_IMVID_ANY
, msg_types
[0]);
313 add_product_info(out_msg
);
314 add_string_version(out_msg
);
315 add_numeric_version(out_msg
);
316 add_op_status(out_msg
);
317 add_fwd_enabled(out_msg
);
318 add_default_pwd_enabled(out_msg
);
320 /* send PA-TNC message with the excl flag not set */
321 result
= out_msg
->send(out_msg
, FALSE
);
322 out_msg
->destroy(out_msg
);
328 static TNC_Result
receive_message(imc_msg_t
*in_msg
)
331 enumerator_t
*enumerator
;
335 bool fatal_error
= FALSE
;
337 /* parse received PA-TNC message and handle local and remote errors */
338 result
= in_msg
->receive(in_msg
, &fatal_error
);
339 if (result
!= TNC_RESULT_SUCCESS
)
343 out_msg
= imc_msg_create_as_reply(in_msg
);
345 /* analyze PA-TNC attributes */
346 enumerator
= in_msg
->create_attribute_enumerator(in_msg
);
347 while (enumerator
->enumerate(enumerator
, &attr
))
349 type
= attr
->get_type(attr
);
351 if (type
.vendor_id
== PEN_IETF
)
353 if (type
.type
== IETF_ATTR_ATTRIBUTE_REQUEST
)
355 ietf_attr_attr_request_t
*attr_cast
;
359 attr_cast
= (ietf_attr_attr_request_t
*)attr
;
361 e
= attr_cast
->create_enumerator(attr_cast
);
362 while (e
->enumerate(e
, &entry
))
364 if (entry
->vendor_id
!= PEN_IETF
)
370 case IETF_ATTR_PRODUCT_INFORMATION
:
371 add_product_info(out_msg
);
373 case IETF_ATTR_STRING_VERSION
:
374 add_string_version(out_msg
);
376 case IETF_ATTR_NUMERIC_VERSION
:
377 add_numeric_version(out_msg
);
379 case IETF_ATTR_OPERATIONAL_STATUS
:
380 add_op_status(out_msg
);
382 case IETF_ATTR_FORWARDING_ENABLED
:
383 add_fwd_enabled(out_msg
);
385 case IETF_ATTR_FACTORY_DEFAULT_PWD_ENABLED
:
386 add_default_pwd_enabled(out_msg
);
388 case IETF_ATTR_INSTALLED_PACKAGES
:
389 add_installed_packages(out_msg
);
397 else if (type
.type
== IETF_ATTR_REMEDIATION_INSTRUCTIONS
)
399 ietf_attr_remediation_instr_t
*attr_cast
;
400 pen_type_t parameters_type
;
401 chunk_t parameters
, string
, lang_code
;
403 attr_cast
= (ietf_attr_remediation_instr_t
*)attr
;
404 parameters_type
= attr_cast
->get_parameters_type(attr_cast
);
405 parameters
= attr_cast
->get_parameters(attr_cast
);
407 if (parameters_type
.vendor_id
== PEN_IETF
)
409 switch (parameters_type
.type
)
411 case IETF_REMEDIATION_PARAMETERS_URI
:
412 DBG1(DBG_IMC
, "remediation uri: '%.*s'",
413 parameters
.len
, parameters
.ptr
);
415 case IETF_REMEDIATION_PARAMETERS_STRING
:
416 string
= attr_cast
->get_string(attr_cast
, &lang_code
);
417 DBG1(DBG_IMC
, "remediation string: '%.*s' [%.*s]",
418 string
.len
, string
.ptr
,
419 lang_code
.len
, lang_code
.ptr
);
422 DBG1(DBG_IMC
, "remediation parameters %B", ¶meters
);
427 DBG1(DBG_IMC
, "remediation parameters %B", ¶meters
);
431 else if (type
.vendor_id
== PEN_ITA
&& type
.type
== ITA_ATTR_GET_SETTINGS
)
433 ita_attr_get_settings_t
*attr_cast
;
436 attr_cast
= (ita_attr_get_settings_t
*)attr
;
438 e
= attr_cast
->create_enumerator(attr_cast
);
439 add_settings(e
, out_msg
);
443 enumerator
->destroy(enumerator
);
447 result
= TNC_RESULT_FATAL
;
451 result
= out_msg
->send(out_msg
, TRUE
);
453 out_msg
->destroy(out_msg
);
459 * see section 3.8.4 of TCG TNC IF-IMC Specification 1.3
462 TNC_Result
TNC_IMC_ReceiveMessage(TNC_IMCID imc_id
,
463 TNC_ConnectionID connection_id
,
464 TNC_BufferReference msg
,
466 TNC_MessageType msg_type
)
474 DBG1(DBG_IMC
, "IMC \"%s\" has not been initialized", imc_name
);
475 return TNC_RESULT_NOT_INITIALIZED
;
477 if (!imc_os
->get_state(imc_os
, connection_id
, &state
))
479 return TNC_RESULT_FATAL
;
481 in_msg
= imc_msg_create_from_data(imc_os
, state
, connection_id
, msg_type
,
482 chunk_create(msg
, msg_len
));
483 result
= receive_message(in_msg
);
484 in_msg
->destroy(in_msg
);
490 * see section 3.8.6 of TCG TNC IF-IMV Specification 1.3
492 TNC_Result
TNC_IMC_ReceiveMessageLong(TNC_IMCID imc_id
,
493 TNC_ConnectionID connection_id
,
494 TNC_UInt32 msg_flags
,
495 TNC_BufferReference msg
,
497 TNC_VendorID msg_vid
,
498 TNC_MessageSubtype msg_subtype
,
499 TNC_UInt32 src_imv_id
,
500 TNC_UInt32 dst_imc_id
)
508 DBG1(DBG_IMC
, "IMC \"%s\" has not been initialized", imc_name
);
509 return TNC_RESULT_NOT_INITIALIZED
;
511 if (!imc_os
->get_state(imc_os
, connection_id
, &state
))
513 return TNC_RESULT_FATAL
;
515 in_msg
= imc_msg_create_from_long_data(imc_os
, state
, connection_id
,
516 src_imv_id
, dst_imc_id
,msg_vid
, msg_subtype
,
517 chunk_create(msg
, msg_len
));
518 result
=receive_message(in_msg
);
519 in_msg
->destroy(in_msg
);
525 * see section 3.8.7 of TCG TNC IF-IMC Specification 1.3
527 TNC_Result
TNC_IMC_BatchEnding(TNC_IMCID imc_id
,
528 TNC_ConnectionID connection_id
)
532 DBG1(DBG_IMC
, "IMC \"%s\" has not been initialized", imc_name
);
533 return TNC_RESULT_NOT_INITIALIZED
;
535 return TNC_RESULT_SUCCESS
;
539 * see section 3.8.8 of TCG TNC IF-IMC Specification 1.3
541 TNC_Result
TNC_IMC_Terminate(TNC_IMCID imc_id
)
545 DBG1(DBG_IMC
, "IMC \"%s\" has not been initialized", imc_name
);
546 return TNC_RESULT_NOT_INITIALIZED
;
548 imc_os
->destroy(imc_os
);
554 return TNC_RESULT_SUCCESS
;
558 * see section 4.2.8.1 of TCG TNC IF-IMC Specification 1.3
560 TNC_Result
TNC_IMC_ProvideBindFunction(TNC_IMCID imc_id
,
561 TNC_TNCC_BindFunctionPointer bind_function
)
565 DBG1(DBG_IMC
, "IMC \"%s\" has not been initialized", imc_name
);
566 return TNC_RESULT_NOT_INITIALIZED
;
568 return imc_os
->bind_functions(imc_os
, bind_function
);