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_op_status.h>
26 #include <ietf/ietf_attr_product_info.h>
27 #include <ietf/ietf_attr_remediation_instr.h>
28 #include <ietf/ietf_attr_string_version.h>
29 #include <os_info/os_info.h>
31 #include <tncif_pa_subtypes.h>
38 static const char imc_name
[] = "OS";
40 static pen_type_t msg_types
[] = {
41 { PEN_IETF
, PA_SUBTYPE_IETF_OPERATING_SYSTEM
}
44 static imc_agent_t
*imc_os
;
48 * see section 3.8.1 of TCG TNC IF-IMC Specification 1.3
50 TNC_Result
TNC_IMC_Initialize(TNC_IMCID imc_id
,
51 TNC_Version min_version
,
52 TNC_Version max_version
,
53 TNC_Version
*actual_version
)
57 DBG1(DBG_IMC
, "IMC \"%s\" has already been initialized", imc_name
);
58 return TNC_RESULT_ALREADY_INITIALIZED
;
60 imc_os
= imc_agent_create(imc_name
, msg_types
, 1, imc_id
, actual_version
);
63 return TNC_RESULT_FATAL
;
66 os
= os_info_create();
69 imc_os
->destroy(imc_os
);
72 return TNC_RESULT_FATAL
;
75 if (min_version
> TNC_IFIMC_VERSION_1
|| max_version
< TNC_IFIMC_VERSION_1
)
77 DBG1(DBG_IMC
, "no common IF-IMC version");
78 return TNC_RESULT_NO_COMMON_VERSION
;
80 return TNC_RESULT_SUCCESS
;
84 * see section 3.8.2 of TCG TNC IF-IMC Specification 1.3
86 TNC_Result
TNC_IMC_NotifyConnectionChange(TNC_IMCID imc_id
,
87 TNC_ConnectionID connection_id
,
88 TNC_ConnectionState new_state
)
94 DBG1(DBG_IMC
, "IMC \"%s\" has not been initialized", imc_name
);
95 return TNC_RESULT_NOT_INITIALIZED
;
99 case TNC_CONNECTION_STATE_CREATE
:
100 state
= imc_os_state_create(connection_id
);
101 return imc_os
->create_state(imc_os
, state
);
102 case TNC_CONNECTION_STATE_HANDSHAKE
:
103 if (imc_os
->change_state(imc_os
, connection_id
, new_state
,
104 &state
) != TNC_RESULT_SUCCESS
)
106 return TNC_RESULT_FATAL
;
108 state
->set_result(state
, imc_id
,
109 TNC_IMV_EVALUATION_RESULT_DONT_KNOW
);
110 return TNC_RESULT_SUCCESS
;
111 case TNC_CONNECTION_STATE_DELETE
:
112 return imc_os
->delete_state(imc_os
, connection_id
);
114 return imc_os
->change_state(imc_os
, connection_id
,
120 * Add IETF Product Information attribute to the send queue
122 static void add_product_info(imc_msg_t
*msg
)
126 attr
= ietf_attr_product_info_create(PEN_IETF
, 0, os
->get_name(os
));
127 msg
->add_attribute(msg
, attr
);
131 * Add IETF String Version attribute to the send queue
133 static void add_string_version(imc_msg_t
*msg
)
137 attr
= ietf_attr_string_version_create(os
->get_version(os
),
138 chunk_empty
, chunk_empty
);
139 msg
->add_attribute(msg
, attr
);
143 * Add IETF Operational Status attribute to the send queue
145 static void add_op_status(imc_msg_t
*msg
)
148 time_t uptime
, last_boot
;
150 uptime
= os
->get_uptime(os
);
151 last_boot
= uptime ?
time(NULL
) - uptime
: UNDEFINED_TIME
;
152 if (last_boot
!= UNDEFINED_TIME
)
154 DBG1(DBG_IMC
, "last boot: %T, %u s ago", &last_boot
, TRUE
, uptime
);
156 attr
= ietf_attr_op_status_create(OP_STATUS_OPERATIONAL
,
157 OP_RESULT_SUCCESSFUL
, last_boot
);
158 msg
->add_attribute(msg
, attr
);
162 * Add IETF Forwarding Enabled attribute to the send queue
164 static void add_fwd_enabled(imc_msg_t
*msg
)
167 os_fwd_status_t fwd_status
;
169 fwd_status
= os
->get_fwd_status(os
);
170 DBG1(DBG_IMC
, "IPv4 forwarding status: %N",
171 os_fwd_status_names
, fwd_status
);
172 attr
= ietf_attr_fwd_enabled_create(fwd_status
);
173 msg
->add_attribute(msg
, attr
);
177 * Add IETF Factory Default Password Enabled attribute to the send queue
179 static void add_default_pwd_enabled(imc_msg_t
*msg
)
183 DBG1(DBG_IMC
, "factory default password: disabled");
184 attr
= ietf_attr_default_pwd_enabled_create(FALSE
);
185 msg
->add_attribute(msg
, attr
);
189 * Add an IETF Installed Packages attribute to the send queue
191 static void add_installed_packages(imc_msg_t
*msg
)
194 ietf_attr_installed_packages_t
*attr_cast
;
195 chunk_t libc_name
= { "libc-bin", 8 };
196 chunk_t libc_version
= { "2.15-0ubuntu10.2", 16 };
197 chunk_t selinux_name
= { "selinux", 7 };
198 chunk_t selinux_version
= { "1:0.11", 6 };
200 attr
= ietf_attr_installed_packages_create();
201 attr_cast
= (ietf_attr_installed_packages_t
*)attr
;
202 attr_cast
->add(attr_cast
, libc_name
, libc_version
);
203 attr_cast
->add(attr_cast
, selinux_name
, selinux_version
);
204 msg
->add_attribute(msg
, attr
);
208 * see section 3.8.3 of TCG TNC IF-IMC Specification 1.3
210 TNC_Result
TNC_IMC_BeginHandshake(TNC_IMCID imc_id
,
211 TNC_ConnectionID connection_id
)
215 TNC_Result result
= TNC_RESULT_SUCCESS
;
219 DBG1(DBG_IMC
, "IMC \"%s\" has not been initialized", imc_name
);
220 return TNC_RESULT_NOT_INITIALIZED
;
222 if (!imc_os
->get_state(imc_os
, connection_id
, &state
))
224 return TNC_RESULT_FATAL
;
226 if (lib
->settings
->get_bool(lib
->settings
,
227 "libimcv.plugins.imc-os.send_info", TRUE
))
229 out_msg
= imc_msg_create(imc_os
, state
, connection_id
, imc_id
,
230 TNC_IMVID_ANY
, msg_types
[0]);
231 add_product_info(out_msg
);
232 add_string_version(out_msg
);
233 add_op_status(out_msg
);
234 add_fwd_enabled(out_msg
);
235 add_default_pwd_enabled(out_msg
);
237 /* send PA-TNC message with the excl flag not set */
238 result
= out_msg
->send(out_msg
, FALSE
);
239 out_msg
->destroy(out_msg
);
245 static TNC_Result
receive_message(imc_msg_t
*in_msg
)
248 enumerator_t
*enumerator
;
250 pen_type_t attr_type
;
252 bool fatal_error
= FALSE
;
254 /* parse received PA-TNC message and handle local and remote errors */
255 result
= in_msg
->receive(in_msg
, &fatal_error
);
256 if (result
!= TNC_RESULT_SUCCESS
)
260 out_msg
= imc_msg_create_as_reply(in_msg
);
262 /* analyze PA-TNC attributes */
263 enumerator
= in_msg
->create_attribute_enumerator(in_msg
);
264 while (enumerator
->enumerate(enumerator
, &attr
))
266 attr_type
= attr
->get_type(attr
);
268 if (attr_type
.vendor_id
!= PEN_IETF
)
272 if (attr_type
.type
== IETF_ATTR_ATTRIBUTE_REQUEST
)
274 ietf_attr_attr_request_t
*attr_cast
;
278 attr_cast
= (ietf_attr_attr_request_t
*)attr
;
280 e
= attr_cast
->create_enumerator(attr_cast
);
281 while (e
->enumerate(e
, &entry
))
283 if (entry
->vendor_id
!= PEN_IETF
)
289 case IETF_ATTR_PRODUCT_INFORMATION
:
290 add_product_info(out_msg
);
292 case IETF_ATTR_STRING_VERSION
:
293 add_string_version(out_msg
);
295 case IETF_ATTR_OPERATIONAL_STATUS
:
296 add_op_status(out_msg
);
298 case IETF_ATTR_FORWARDING_ENABLED
:
299 add_fwd_enabled(out_msg
);
301 case IETF_ATTR_FACTORY_DEFAULT_PWD_ENABLED
:
302 add_default_pwd_enabled(out_msg
);
304 case IETF_ATTR_INSTALLED_PACKAGES
:
305 add_installed_packages(out_msg
);
313 else if (attr_type
.type
== IETF_ATTR_REMEDIATION_INSTRUCTIONS
)
315 ietf_attr_remediation_instr_t
*attr_cast
;
316 pen_type_t parameters_type
;
317 chunk_t parameters
, string
, lang_code
;
319 attr_cast
= (ietf_attr_remediation_instr_t
*)attr
;
320 parameters_type
= attr_cast
->get_parameters_type(attr_cast
);
321 parameters
= attr_cast
->get_parameters(attr_cast
);
323 if (parameters_type
.vendor_id
== PEN_IETF
)
325 switch (parameters_type
.type
)
327 case IETF_REMEDIATION_PARAMETERS_URI
:
328 DBG1(DBG_IMC
, "remediation uri: '%.*s'",
329 parameters
.len
, parameters
.ptr
);
331 case IETF_REMEDIATION_PARAMETERS_STRING
:
332 string
= attr_cast
->get_string(attr_cast
, &lang_code
);
333 DBG1(DBG_IMC
, "remediation string: '%.*s' [%.*s]",
334 string
.len
, string
.ptr
,
335 lang_code
.len
, lang_code
.ptr
);
338 DBG1(DBG_IMC
, "remediation parameters %B", ¶meters
);
343 DBG1(DBG_IMC
, "remediation parameters %B", ¶meters
);
347 enumerator
->destroy(enumerator
);
351 result
= TNC_RESULT_FATAL
;
355 result
= out_msg
->send(out_msg
, TRUE
);
357 out_msg
->destroy(out_msg
);
363 * see section 3.8.4 of TCG TNC IF-IMC Specification 1.3
366 TNC_Result
TNC_IMC_ReceiveMessage(TNC_IMCID imc_id
,
367 TNC_ConnectionID connection_id
,
368 TNC_BufferReference msg
,
370 TNC_MessageType msg_type
)
378 DBG1(DBG_IMC
, "IMC \"%s\" has not been initialized", imc_name
);
379 return TNC_RESULT_NOT_INITIALIZED
;
381 if (!imc_os
->get_state(imc_os
, connection_id
, &state
))
383 return TNC_RESULT_FATAL
;
385 in_msg
= imc_msg_create_from_data(imc_os
, state
, connection_id
, msg_type
,
386 chunk_create(msg
, msg_len
));
387 result
= receive_message(in_msg
);
388 in_msg
->destroy(in_msg
);
394 * see section 3.8.6 of TCG TNC IF-IMV Specification 1.3
396 TNC_Result
TNC_IMC_ReceiveMessageLong(TNC_IMCID imc_id
,
397 TNC_ConnectionID connection_id
,
398 TNC_UInt32 msg_flags
,
399 TNC_BufferReference msg
,
401 TNC_VendorID msg_vid
,
402 TNC_MessageSubtype msg_subtype
,
403 TNC_UInt32 src_imv_id
,
404 TNC_UInt32 dst_imc_id
)
412 DBG1(DBG_IMC
, "IMC \"%s\" has not been initialized", imc_name
);
413 return TNC_RESULT_NOT_INITIALIZED
;
415 if (!imc_os
->get_state(imc_os
, connection_id
, &state
))
417 return TNC_RESULT_FATAL
;
419 in_msg
= imc_msg_create_from_long_data(imc_os
, state
, connection_id
,
420 src_imv_id
, dst_imc_id
,msg_vid
, msg_subtype
,
421 chunk_create(msg
, msg_len
));
422 result
=receive_message(in_msg
);
423 in_msg
->destroy(in_msg
);
429 * see section 3.8.7 of TCG TNC IF-IMC Specification 1.3
431 TNC_Result
TNC_IMC_BatchEnding(TNC_IMCID imc_id
,
432 TNC_ConnectionID connection_id
)
436 DBG1(DBG_IMC
, "IMC \"%s\" has not been initialized", imc_name
);
437 return TNC_RESULT_NOT_INITIALIZED
;
439 return TNC_RESULT_SUCCESS
;
443 * see section 3.8.8 of TCG TNC IF-IMC Specification 1.3
445 TNC_Result
TNC_IMC_Terminate(TNC_IMCID imc_id
)
449 DBG1(DBG_IMC
, "IMC \"%s\" has not been initialized", imc_name
);
450 return TNC_RESULT_NOT_INITIALIZED
;
452 imc_os
->destroy(imc_os
);
458 return TNC_RESULT_SUCCESS
;
462 * see section 4.2.8.1 of TCG TNC IF-IMC Specification 1.3
464 TNC_Result
TNC_IMC_ProvideBindFunction(TNC_IMCID imc_id
,
465 TNC_TNCC_BindFunctionPointer bind_function
)
469 DBG1(DBG_IMC
, "IMC \"%s\" has not been initialized", imc_name
);
470 return TNC_RESULT_NOT_INITIALIZED
;
472 return imc_os
->bind_functions(imc_os
, bind_function
);