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 <os_info/os_info.h>
32 #include <tncif_pa_subtypes.h>
35 #include <utils/debug.h>
39 static const char imc_name
[] = "OS";
41 static pen_type_t msg_types
[] = {
42 { PEN_IETF
, PA_SUBTYPE_IETF_OPERATING_SYSTEM
}
45 static imc_agent_t
*imc_os
;
49 * see section 3.8.1 of TCG TNC IF-IMC Specification 1.3
51 TNC_Result
TNC_IMC_Initialize(TNC_IMCID imc_id
,
52 TNC_Version min_version
,
53 TNC_Version max_version
,
54 TNC_Version
*actual_version
)
58 DBG1(DBG_IMC
, "IMC \"%s\" has already been initialized", imc_name
);
59 return TNC_RESULT_ALREADY_INITIALIZED
;
61 imc_os
= imc_agent_create(imc_name
, msg_types
, countof(msg_types
),
62 imc_id
, actual_version
);
65 return TNC_RESULT_FATAL
;
68 os
= os_info_create();
71 imc_os
->destroy(imc_os
);
74 return TNC_RESULT_FATAL
;
77 if (min_version
> TNC_IFIMC_VERSION_1
|| max_version
< TNC_IFIMC_VERSION_1
)
79 DBG1(DBG_IMC
, "no common IF-IMC version");
80 return TNC_RESULT_NO_COMMON_VERSION
;
82 return TNC_RESULT_SUCCESS
;
86 * see section 3.8.2 of TCG TNC IF-IMC Specification 1.3
88 TNC_Result
TNC_IMC_NotifyConnectionChange(TNC_IMCID imc_id
,
89 TNC_ConnectionID connection_id
,
90 TNC_ConnectionState new_state
)
96 DBG1(DBG_IMC
, "IMC \"%s\" has not been initialized", imc_name
);
97 return TNC_RESULT_NOT_INITIALIZED
;
101 case TNC_CONNECTION_STATE_CREATE
:
102 state
= imc_os_state_create(connection_id
);
103 return imc_os
->create_state(imc_os
, state
);
104 case TNC_CONNECTION_STATE_HANDSHAKE
:
105 if (imc_os
->change_state(imc_os
, connection_id
, new_state
,
106 &state
) != TNC_RESULT_SUCCESS
)
108 return TNC_RESULT_FATAL
;
110 state
->set_result(state
, imc_id
,
111 TNC_IMV_EVALUATION_RESULT_DONT_KNOW
);
112 return TNC_RESULT_SUCCESS
;
113 case TNC_CONNECTION_STATE_DELETE
:
114 return imc_os
->delete_state(imc_os
, connection_id
);
116 return imc_os
->change_state(imc_os
, connection_id
,
122 * Add IETF Product Information attribute to the send queue
124 static void add_product_info(imc_msg_t
*msg
)
128 pen_t vendor_id
= PEN_IETF
;
132 typedef struct vendor_pen_t
{
137 vendor_pen_t vendor_pens
[] = {
138 { "Debian", PEN_DEBIAN
},
139 { "Ubuntu", PEN_CANONICAL
}
142 os_name
= os
->get_name(os
);
143 for (i
= 0; i
< countof(vendor_pens
); i
++)
145 vendor
= vendor_pens
[i
].vendor
;
146 if (chunk_equals(os_name
, chunk_create(vendor
, strlen(vendor
))))
148 vendor_id
= vendor_pens
[i
].pen
;
152 attr
= ietf_attr_product_info_create(vendor_id
, 0, os_name
);
153 msg
->add_attribute(msg
, attr
);
157 * Add IETF Numeric Version attribute to the send queue
159 static void add_numeric_version(imc_msg_t
*msg
)
162 u_int32_t major
, minor
;
164 os
->get_numeric_version(os
, &major
, &minor
);
165 DBG1(DBG_IMC
, "operating system numeric version is %d.%d",
168 attr
= ietf_attr_numeric_version_create(major
, minor
, 0, 0, 0);
169 msg
->add_attribute(msg
, attr
);
173 * Add IETF String Version attribute to the send queue
175 static void add_string_version(imc_msg_t
*msg
)
179 attr
= ietf_attr_string_version_create(os
->get_version(os
),
180 chunk_empty
, chunk_empty
);
181 msg
->add_attribute(msg
, attr
);
185 * Add IETF Operational Status attribute to the send queue
187 static void add_op_status(imc_msg_t
*msg
)
190 time_t uptime
, last_boot
;
192 uptime
= os
->get_uptime(os
);
193 last_boot
= uptime ?
time(NULL
) - uptime
: UNDEFINED_TIME
;
194 if (last_boot
!= UNDEFINED_TIME
)
196 DBG1(DBG_IMC
, "last boot: %T, %u s ago", &last_boot
, TRUE
, uptime
);
198 attr
= ietf_attr_op_status_create(OP_STATUS_OPERATIONAL
,
199 OP_RESULT_SUCCESSFUL
, last_boot
);
200 msg
->add_attribute(msg
, attr
);
204 * Add IETF Forwarding Enabled attribute to the send queue
206 static void add_fwd_enabled(imc_msg_t
*msg
)
209 os_fwd_status_t fwd_status
;
211 fwd_status
= os
->get_fwd_status(os
);
212 DBG1(DBG_IMC
, "IPv4 forwarding status: %N",
213 os_fwd_status_names
, fwd_status
);
214 attr
= ietf_attr_fwd_enabled_create(fwd_status
);
215 msg
->add_attribute(msg
, attr
);
219 * Add IETF Factory Default Password Enabled attribute to the send queue
221 static void add_default_pwd_enabled(imc_msg_t
*msg
)
225 DBG1(DBG_IMC
, "factory default password: disabled");
226 attr
= ietf_attr_default_pwd_enabled_create(FALSE
);
227 msg
->add_attribute(msg
, attr
);
231 * Add an IETF Installed Packages attribute to the send queue
233 static void add_installed_packages(imc_msg_t
*msg
)
236 ietf_attr_installed_packages_t
*attr_cast
;
237 chunk_t libc_name
= { "libc-bin", 8 };
238 chunk_t libc_version
= { "2.15-0ubuntu10.2", 16 };
239 chunk_t selinux_name
= { "selinux", 7 };
240 chunk_t selinux_version
= { "1:0.11", 6 };
242 attr
= ietf_attr_installed_packages_create();
243 attr_cast
= (ietf_attr_installed_packages_t
*)attr
;
244 attr_cast
->add(attr_cast
, libc_name
, libc_version
);
245 attr_cast
->add(attr_cast
, selinux_name
, selinux_version
);
246 msg
->add_attribute(msg
, attr
);
250 * see section 3.8.3 of TCG TNC IF-IMC Specification 1.3
252 TNC_Result
TNC_IMC_BeginHandshake(TNC_IMCID imc_id
,
253 TNC_ConnectionID connection_id
)
257 TNC_Result result
= TNC_RESULT_SUCCESS
;
261 DBG1(DBG_IMC
, "IMC \"%s\" has not been initialized", imc_name
);
262 return TNC_RESULT_NOT_INITIALIZED
;
264 if (!imc_os
->get_state(imc_os
, connection_id
, &state
))
266 return TNC_RESULT_FATAL
;
268 if (lib
->settings
->get_bool(lib
->settings
,
269 "libimcv.plugins.imc-os.send_info", TRUE
))
271 out_msg
= imc_msg_create(imc_os
, state
, connection_id
, imc_id
,
272 TNC_IMVID_ANY
, msg_types
[0]);
273 add_product_info(out_msg
);
274 add_string_version(out_msg
);
275 add_numeric_version(out_msg
);
276 add_op_status(out_msg
);
277 add_fwd_enabled(out_msg
);
278 add_default_pwd_enabled(out_msg
);
280 /* send PA-TNC message with the excl flag not set */
281 result
= out_msg
->send(out_msg
, FALSE
);
282 out_msg
->destroy(out_msg
);
288 static TNC_Result
receive_message(imc_msg_t
*in_msg
)
291 enumerator_t
*enumerator
;
293 pen_type_t attr_type
;
295 bool fatal_error
= FALSE
;
297 /* parse received PA-TNC message and handle local and remote errors */
298 result
= in_msg
->receive(in_msg
, &fatal_error
);
299 if (result
!= TNC_RESULT_SUCCESS
)
303 out_msg
= imc_msg_create_as_reply(in_msg
);
305 /* analyze PA-TNC attributes */
306 enumerator
= in_msg
->create_attribute_enumerator(in_msg
);
307 while (enumerator
->enumerate(enumerator
, &attr
))
309 attr_type
= attr
->get_type(attr
);
311 if (attr_type
.vendor_id
!= PEN_IETF
)
315 if (attr_type
.type
== IETF_ATTR_ATTRIBUTE_REQUEST
)
317 ietf_attr_attr_request_t
*attr_cast
;
321 attr_cast
= (ietf_attr_attr_request_t
*)attr
;
323 e
= attr_cast
->create_enumerator(attr_cast
);
324 while (e
->enumerate(e
, &entry
))
326 if (entry
->vendor_id
!= PEN_IETF
)
332 case IETF_ATTR_PRODUCT_INFORMATION
:
333 add_product_info(out_msg
);
335 case IETF_ATTR_STRING_VERSION
:
336 add_string_version(out_msg
);
338 case IETF_ATTR_NUMERIC_VERSION
:
339 add_numeric_version(out_msg
);
341 case IETF_ATTR_OPERATIONAL_STATUS
:
342 add_op_status(out_msg
);
344 case IETF_ATTR_FORWARDING_ENABLED
:
345 add_fwd_enabled(out_msg
);
347 case IETF_ATTR_FACTORY_DEFAULT_PWD_ENABLED
:
348 add_default_pwd_enabled(out_msg
);
350 case IETF_ATTR_INSTALLED_PACKAGES
:
351 add_installed_packages(out_msg
);
359 else if (attr_type
.type
== IETF_ATTR_REMEDIATION_INSTRUCTIONS
)
361 ietf_attr_remediation_instr_t
*attr_cast
;
362 pen_type_t parameters_type
;
363 chunk_t parameters
, string
, lang_code
;
365 attr_cast
= (ietf_attr_remediation_instr_t
*)attr
;
366 parameters_type
= attr_cast
->get_parameters_type(attr_cast
);
367 parameters
= attr_cast
->get_parameters(attr_cast
);
369 if (parameters_type
.vendor_id
== PEN_IETF
)
371 switch (parameters_type
.type
)
373 case IETF_REMEDIATION_PARAMETERS_URI
:
374 DBG1(DBG_IMC
, "remediation uri: '%.*s'",
375 parameters
.len
, parameters
.ptr
);
377 case IETF_REMEDIATION_PARAMETERS_STRING
:
378 string
= attr_cast
->get_string(attr_cast
, &lang_code
);
379 DBG1(DBG_IMC
, "remediation string: '%.*s' [%.*s]",
380 string
.len
, string
.ptr
,
381 lang_code
.len
, lang_code
.ptr
);
384 DBG1(DBG_IMC
, "remediation parameters %B", ¶meters
);
389 DBG1(DBG_IMC
, "remediation parameters %B", ¶meters
);
393 enumerator
->destroy(enumerator
);
397 result
= TNC_RESULT_FATAL
;
401 result
= out_msg
->send(out_msg
, TRUE
);
403 out_msg
->destroy(out_msg
);
409 * see section 3.8.4 of TCG TNC IF-IMC Specification 1.3
412 TNC_Result
TNC_IMC_ReceiveMessage(TNC_IMCID imc_id
,
413 TNC_ConnectionID connection_id
,
414 TNC_BufferReference msg
,
416 TNC_MessageType msg_type
)
424 DBG1(DBG_IMC
, "IMC \"%s\" has not been initialized", imc_name
);
425 return TNC_RESULT_NOT_INITIALIZED
;
427 if (!imc_os
->get_state(imc_os
, connection_id
, &state
))
429 return TNC_RESULT_FATAL
;
431 in_msg
= imc_msg_create_from_data(imc_os
, state
, connection_id
, msg_type
,
432 chunk_create(msg
, msg_len
));
433 result
= receive_message(in_msg
);
434 in_msg
->destroy(in_msg
);
440 * see section 3.8.6 of TCG TNC IF-IMV Specification 1.3
442 TNC_Result
TNC_IMC_ReceiveMessageLong(TNC_IMCID imc_id
,
443 TNC_ConnectionID connection_id
,
444 TNC_UInt32 msg_flags
,
445 TNC_BufferReference msg
,
447 TNC_VendorID msg_vid
,
448 TNC_MessageSubtype msg_subtype
,
449 TNC_UInt32 src_imv_id
,
450 TNC_UInt32 dst_imc_id
)
458 DBG1(DBG_IMC
, "IMC \"%s\" has not been initialized", imc_name
);
459 return TNC_RESULT_NOT_INITIALIZED
;
461 if (!imc_os
->get_state(imc_os
, connection_id
, &state
))
463 return TNC_RESULT_FATAL
;
465 in_msg
= imc_msg_create_from_long_data(imc_os
, state
, connection_id
,
466 src_imv_id
, dst_imc_id
,msg_vid
, msg_subtype
,
467 chunk_create(msg
, msg_len
));
468 result
=receive_message(in_msg
);
469 in_msg
->destroy(in_msg
);
475 * see section 3.8.7 of TCG TNC IF-IMC Specification 1.3
477 TNC_Result
TNC_IMC_BatchEnding(TNC_IMCID imc_id
,
478 TNC_ConnectionID connection_id
)
482 DBG1(DBG_IMC
, "IMC \"%s\" has not been initialized", imc_name
);
483 return TNC_RESULT_NOT_INITIALIZED
;
485 return TNC_RESULT_SUCCESS
;
489 * see section 3.8.8 of TCG TNC IF-IMC Specification 1.3
491 TNC_Result
TNC_IMC_Terminate(TNC_IMCID imc_id
)
495 DBG1(DBG_IMC
, "IMC \"%s\" has not been initialized", imc_name
);
496 return TNC_RESULT_NOT_INITIALIZED
;
498 imc_os
->destroy(imc_os
);
504 return TNC_RESULT_SUCCESS
;
508 * see section 4.2.8.1 of TCG TNC IF-IMC Specification 1.3
510 TNC_Result
TNC_IMC_ProvideBindFunction(TNC_IMCID imc_id
,
511 TNC_TNCC_BindFunctionPointer bind_function
)
515 DBG1(DBG_IMC
, "IMC \"%s\" has not been initialized", imc_name
);
516 return TNC_RESULT_NOT_INITIALIZED
;
518 return imc_os
->bind_functions(imc_os
, bind_function
);