implemented IETF Numeric Version attribute
[strongswan.git] / src / libimcv / plugins / imc_os / imc_os.c
1 /*
2 * Copyright (C) 2011-2012 Andreas Steffen
3 * HSR Hochschule fuer Technik Rapperswil
4 *
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>.
9 *
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
13 * for more details.
14 */
15
16 #include "imc_os_state.h"
17
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>
31
32 #include <tncif_pa_subtypes.h>
33
34 #include <pen/pen.h>
35 #include <debug.h>
36
37 /* IMC definitions */
38
39 static const char imc_name[] = "OS";
40
41 static pen_type_t msg_types[] = {
42 { PEN_IETF, PA_SUBTYPE_IETF_OPERATING_SYSTEM }
43 };
44
45 static imc_agent_t *imc_os;
46 static os_info_t *os;
47
48 /**
49 * see section 3.8.1 of TCG TNC IF-IMC Specification 1.3
50 */
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)
55 {
56 if (imc_os)
57 {
58 DBG1(DBG_IMC, "IMC \"%s\" has already been initialized", imc_name);
59 return TNC_RESULT_ALREADY_INITIALIZED;
60 }
61 imc_os = imc_agent_create(imc_name, msg_types, 1, imc_id, actual_version);
62 if (!imc_os)
63 {
64 return TNC_RESULT_FATAL;
65 }
66
67 os = os_info_create();
68 if (!os)
69 {
70 imc_os->destroy(imc_os);
71 imc_os = NULL;
72
73 return TNC_RESULT_FATAL;
74 }
75
76 if (min_version > TNC_IFIMC_VERSION_1 || max_version < TNC_IFIMC_VERSION_1)
77 {
78 DBG1(DBG_IMC, "no common IF-IMC version");
79 return TNC_RESULT_NO_COMMON_VERSION;
80 }
81 return TNC_RESULT_SUCCESS;
82 }
83
84 /**
85 * see section 3.8.2 of TCG TNC IF-IMC Specification 1.3
86 */
87 TNC_Result TNC_IMC_NotifyConnectionChange(TNC_IMCID imc_id,
88 TNC_ConnectionID connection_id,
89 TNC_ConnectionState new_state)
90 {
91 imc_state_t *state;
92
93 if (!imc_os)
94 {
95 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
96 return TNC_RESULT_NOT_INITIALIZED;
97 }
98 switch (new_state)
99 {
100 case TNC_CONNECTION_STATE_CREATE:
101 state = imc_os_state_create(connection_id);
102 return imc_os->create_state(imc_os, state);
103 case TNC_CONNECTION_STATE_HANDSHAKE:
104 if (imc_os->change_state(imc_os, connection_id, new_state,
105 &state) != TNC_RESULT_SUCCESS)
106 {
107 return TNC_RESULT_FATAL;
108 }
109 state->set_result(state, imc_id,
110 TNC_IMV_EVALUATION_RESULT_DONT_KNOW);
111 return TNC_RESULT_SUCCESS;
112 case TNC_CONNECTION_STATE_DELETE:
113 return imc_os->delete_state(imc_os, connection_id);
114 default:
115 return imc_os->change_state(imc_os, connection_id,
116 new_state, NULL);
117 }
118 }
119
120 /**
121 * Add IETF Product Information attribute to the send queue
122 */
123 static void add_product_info(imc_msg_t *msg)
124 {
125 pa_tnc_attr_t *attr;
126
127 attr = ietf_attr_product_info_create(PEN_IETF, 0, os->get_name(os));
128 msg->add_attribute(msg, attr);
129 }
130
131 /**
132 * Add IETF Numeric Version attribute to the send queue
133 */
134 static void add_numeric_version(imc_msg_t *msg)
135 {
136 pa_tnc_attr_t *attr;
137 u_int32_t major, minor;
138
139 os->get_numeric_version(os, &major, &minor);
140 DBG1(DBG_IMC, "operating system numeric version is %d.%d",
141 major, minor);
142
143 attr = ietf_attr_numeric_version_create(major, minor, 0, 0, 0);
144 msg->add_attribute(msg, attr);
145 }
146
147 /**
148 * Add IETF String Version attribute to the send queue
149 */
150 static void add_string_version(imc_msg_t *msg)
151 {
152 pa_tnc_attr_t *attr;
153
154 attr = ietf_attr_string_version_create(os->get_version(os),
155 chunk_empty, chunk_empty);
156 msg->add_attribute(msg, attr);
157 }
158
159 /**
160 * Add IETF Operational Status attribute to the send queue
161 */
162 static void add_op_status(imc_msg_t *msg)
163 {
164 pa_tnc_attr_t *attr;
165 time_t uptime, last_boot;
166
167 uptime = os->get_uptime(os);
168 last_boot = uptime ? time(NULL) - uptime : UNDEFINED_TIME;
169 if (last_boot != UNDEFINED_TIME)
170 {
171 DBG1(DBG_IMC, "last boot: %T, %u s ago", &last_boot, TRUE, uptime);
172 }
173 attr = ietf_attr_op_status_create(OP_STATUS_OPERATIONAL,
174 OP_RESULT_SUCCESSFUL, last_boot);
175 msg->add_attribute(msg, attr);
176 }
177
178 /**
179 * Add IETF Forwarding Enabled attribute to the send queue
180 */
181 static void add_fwd_enabled(imc_msg_t *msg)
182 {
183 pa_tnc_attr_t *attr;
184 os_fwd_status_t fwd_status;
185
186 fwd_status = os->get_fwd_status(os);
187 DBG1(DBG_IMC, "IPv4 forwarding status: %N",
188 os_fwd_status_names, fwd_status);
189 attr = ietf_attr_fwd_enabled_create(fwd_status);
190 msg->add_attribute(msg, attr);
191 }
192
193 /**
194 * Add IETF Factory Default Password Enabled attribute to the send queue
195 */
196 static void add_default_pwd_enabled(imc_msg_t *msg)
197 {
198 pa_tnc_attr_t *attr;
199
200 DBG1(DBG_IMC, "factory default password: disabled");
201 attr = ietf_attr_default_pwd_enabled_create(FALSE);
202 msg->add_attribute(msg, attr);
203 }
204
205 /**
206 * Add an IETF Installed Packages attribute to the send queue
207 */
208 static void add_installed_packages(imc_msg_t *msg)
209 {
210 pa_tnc_attr_t *attr;
211 ietf_attr_installed_packages_t *attr_cast;
212 chunk_t libc_name = { "libc-bin", 8 };
213 chunk_t libc_version = { "2.15-0ubuntu10.2", 16 };
214 chunk_t selinux_name = { "selinux", 7 };
215 chunk_t selinux_version = { "1:0.11", 6 };
216
217 attr = ietf_attr_installed_packages_create();
218 attr_cast = (ietf_attr_installed_packages_t*)attr;
219 attr_cast->add(attr_cast, libc_name, libc_version);
220 attr_cast->add(attr_cast, selinux_name, selinux_version);
221 msg->add_attribute(msg, attr);
222 }
223
224 /**
225 * see section 3.8.3 of TCG TNC IF-IMC Specification 1.3
226 */
227 TNC_Result TNC_IMC_BeginHandshake(TNC_IMCID imc_id,
228 TNC_ConnectionID connection_id)
229 {
230 imc_state_t *state;
231 imc_msg_t *out_msg;
232 TNC_Result result = TNC_RESULT_SUCCESS;
233
234 if (!imc_os)
235 {
236 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
237 return TNC_RESULT_NOT_INITIALIZED;
238 }
239 if (!imc_os->get_state(imc_os, connection_id, &state))
240 {
241 return TNC_RESULT_FATAL;
242 }
243 if (lib->settings->get_bool(lib->settings,
244 "libimcv.plugins.imc-os.send_info", TRUE))
245 {
246 out_msg = imc_msg_create(imc_os, state, connection_id, imc_id,
247 TNC_IMVID_ANY, msg_types[0]);
248 add_product_info(out_msg);
249 add_string_version(out_msg);
250 add_numeric_version(out_msg);
251 add_op_status(out_msg);
252 add_fwd_enabled(out_msg);
253 add_default_pwd_enabled(out_msg);
254
255 /* send PA-TNC message with the excl flag not set */
256 result = out_msg->send(out_msg, FALSE);
257 out_msg->destroy(out_msg);
258 }
259
260 return result;
261 }
262
263 static TNC_Result receive_message(imc_msg_t *in_msg)
264 {
265 imc_msg_t *out_msg;
266 enumerator_t *enumerator;
267 pa_tnc_attr_t *attr;
268 pen_type_t attr_type;
269 TNC_Result result;
270 bool fatal_error = FALSE;
271
272 /* parse received PA-TNC message and handle local and remote errors */
273 result = in_msg->receive(in_msg, &fatal_error);
274 if (result != TNC_RESULT_SUCCESS)
275 {
276 return result;
277 }
278 out_msg = imc_msg_create_as_reply(in_msg);
279
280 /* analyze PA-TNC attributes */
281 enumerator = in_msg->create_attribute_enumerator(in_msg);
282 while (enumerator->enumerate(enumerator, &attr))
283 {
284 attr_type = attr->get_type(attr);
285
286 if (attr_type.vendor_id != PEN_IETF)
287 {
288 continue;
289 }
290 if (attr_type.type == IETF_ATTR_ATTRIBUTE_REQUEST)
291 {
292 ietf_attr_attr_request_t *attr_cast;
293 pen_type_t *entry;
294 enumerator_t *e;
295
296 attr_cast = (ietf_attr_attr_request_t*)attr;
297
298 e = attr_cast->create_enumerator(attr_cast);
299 while (e->enumerate(e, &entry))
300 {
301 if (entry->vendor_id != PEN_IETF)
302 {
303 continue;
304 }
305 switch (entry->type)
306 {
307 case IETF_ATTR_PRODUCT_INFORMATION:
308 add_product_info(out_msg);
309 break;
310 case IETF_ATTR_STRING_VERSION:
311 add_string_version(out_msg);
312 break;
313 case IETF_ATTR_NUMERIC_VERSION:
314 add_numeric_version(out_msg);
315 break;
316 case IETF_ATTR_OPERATIONAL_STATUS:
317 add_op_status(out_msg);
318 break;
319 case IETF_ATTR_FORWARDING_ENABLED:
320 add_fwd_enabled(out_msg);
321 break;
322 case IETF_ATTR_FACTORY_DEFAULT_PWD_ENABLED:
323 add_default_pwd_enabled(out_msg);
324 break;
325 case IETF_ATTR_INSTALLED_PACKAGES:
326 add_installed_packages(out_msg);
327 break;
328 default:
329 break;
330 }
331 }
332 e->destroy(e);
333 }
334 else if (attr_type.type == IETF_ATTR_REMEDIATION_INSTRUCTIONS)
335 {
336 ietf_attr_remediation_instr_t *attr_cast;
337 pen_type_t parameters_type;
338 chunk_t parameters, string, lang_code;
339
340 attr_cast = (ietf_attr_remediation_instr_t*)attr;
341 parameters_type = attr_cast->get_parameters_type(attr_cast);
342 parameters = attr_cast->get_parameters(attr_cast);
343
344 if (parameters_type.vendor_id == PEN_IETF)
345 {
346 switch (parameters_type.type)
347 {
348 case IETF_REMEDIATION_PARAMETERS_URI:
349 DBG1(DBG_IMC, "remediation uri: '%.*s'",
350 parameters.len, parameters.ptr);
351 break;
352 case IETF_REMEDIATION_PARAMETERS_STRING:
353 string = attr_cast->get_string(attr_cast, &lang_code);
354 DBG1(DBG_IMC, "remediation string: '%.*s' [%.*s]",
355 string.len, string.ptr,
356 lang_code.len, lang_code.ptr);
357 break;
358 default:
359 DBG1(DBG_IMC, "remediation parameters %B", &parameters);
360 }
361 }
362 else
363 {
364 DBG1(DBG_IMC, "remediation parameters %B", &parameters);
365 }
366 }
367 }
368 enumerator->destroy(enumerator);
369
370 if (fatal_error)
371 {
372 result = TNC_RESULT_FATAL;
373 }
374 else
375 {
376 result = out_msg->send(out_msg, TRUE);
377 }
378 out_msg->destroy(out_msg);
379
380 return result;
381 }
382
383 /**
384 * see section 3.8.4 of TCG TNC IF-IMC Specification 1.3
385
386 */
387 TNC_Result TNC_IMC_ReceiveMessage(TNC_IMCID imc_id,
388 TNC_ConnectionID connection_id,
389 TNC_BufferReference msg,
390 TNC_UInt32 msg_len,
391 TNC_MessageType msg_type)
392 {
393 imc_state_t *state;
394 imc_msg_t *in_msg;
395 TNC_Result result;
396
397 if (!imc_os)
398 {
399 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
400 return TNC_RESULT_NOT_INITIALIZED;
401 }
402 if (!imc_os->get_state(imc_os, connection_id, &state))
403 {
404 return TNC_RESULT_FATAL;
405 }
406 in_msg = imc_msg_create_from_data(imc_os, state, connection_id, msg_type,
407 chunk_create(msg, msg_len));
408 result = receive_message(in_msg);
409 in_msg->destroy(in_msg);
410
411 return result;
412 }
413
414 /**
415 * see section 3.8.6 of TCG TNC IF-IMV Specification 1.3
416 */
417 TNC_Result TNC_IMC_ReceiveMessageLong(TNC_IMCID imc_id,
418 TNC_ConnectionID connection_id,
419 TNC_UInt32 msg_flags,
420 TNC_BufferReference msg,
421 TNC_UInt32 msg_len,
422 TNC_VendorID msg_vid,
423 TNC_MessageSubtype msg_subtype,
424 TNC_UInt32 src_imv_id,
425 TNC_UInt32 dst_imc_id)
426 {
427 imc_state_t *state;
428 imc_msg_t *in_msg;
429 TNC_Result result;
430
431 if (!imc_os)
432 {
433 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
434 return TNC_RESULT_NOT_INITIALIZED;
435 }
436 if (!imc_os->get_state(imc_os, connection_id, &state))
437 {
438 return TNC_RESULT_FATAL;
439 }
440 in_msg = imc_msg_create_from_long_data(imc_os, state, connection_id,
441 src_imv_id, dst_imc_id,msg_vid, msg_subtype,
442 chunk_create(msg, msg_len));
443 result =receive_message(in_msg);
444 in_msg->destroy(in_msg);
445
446 return result;
447 }
448
449 /**
450 * see section 3.8.7 of TCG TNC IF-IMC Specification 1.3
451 */
452 TNC_Result TNC_IMC_BatchEnding(TNC_IMCID imc_id,
453 TNC_ConnectionID connection_id)
454 {
455 if (!imc_os)
456 {
457 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
458 return TNC_RESULT_NOT_INITIALIZED;
459 }
460 return TNC_RESULT_SUCCESS;
461 }
462
463 /**
464 * see section 3.8.8 of TCG TNC IF-IMC Specification 1.3
465 */
466 TNC_Result TNC_IMC_Terminate(TNC_IMCID imc_id)
467 {
468 if (!imc_os)
469 {
470 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
471 return TNC_RESULT_NOT_INITIALIZED;
472 }
473 imc_os->destroy(imc_os);
474 imc_os = NULL;
475
476 os->destroy(os);
477 os = NULL;
478
479 return TNC_RESULT_SUCCESS;
480 }
481
482 /**
483 * see section 4.2.8.1 of TCG TNC IF-IMC Specification 1.3
484 */
485 TNC_Result TNC_IMC_ProvideBindFunction(TNC_IMCID imc_id,
486 TNC_TNCC_BindFunctionPointer bind_function)
487 {
488 if (!imc_os)
489 {
490 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
491 return TNC_RESULT_NOT_INITIALIZED;
492 }
493 return imc_os->bind_functions(imc_os, bind_function);
494 }