transmit Product Vendor ID if known
[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 <utils/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 chunk_t os_name;
127 pen_t vendor_id = PEN_IETF;
128 char *vendor;
129 int i;
130
131 typedef struct vendor_pen_t {
132 char *vendor;
133 pen_t pen;
134 } vendor_pen_t;
135
136 vendor_pen_t vendor_pens[] = {
137 { "Debian", PEN_DEBIAN },
138 { "Ubuntu", PEN_CANONICAL }
139 };
140
141 os_name = os->get_name(os);
142 for (i = 0; i < countof(vendor_pens); i++)
143 {
144 vendor = vendor_pens[i].vendor;
145 if (chunk_equals(os_name, chunk_create(vendor, strlen(vendor))))
146 {
147 vendor_id = vendor_pens[i].pen;
148 break;
149 }
150 }
151 attr = ietf_attr_product_info_create(vendor_id, 0, os_name);
152 msg->add_attribute(msg, attr);
153 }
154
155 /**
156 * Add IETF Numeric Version attribute to the send queue
157 */
158 static void add_numeric_version(imc_msg_t *msg)
159 {
160 pa_tnc_attr_t *attr;
161 u_int32_t major, minor;
162
163 os->get_numeric_version(os, &major, &minor);
164 DBG1(DBG_IMC, "operating system numeric version is %d.%d",
165 major, minor);
166
167 attr = ietf_attr_numeric_version_create(major, minor, 0, 0, 0);
168 msg->add_attribute(msg, attr);
169 }
170
171 /**
172 * Add IETF String Version attribute to the send queue
173 */
174 static void add_string_version(imc_msg_t *msg)
175 {
176 pa_tnc_attr_t *attr;
177
178 attr = ietf_attr_string_version_create(os->get_version(os),
179 chunk_empty, chunk_empty);
180 msg->add_attribute(msg, attr);
181 }
182
183 /**
184 * Add IETF Operational Status attribute to the send queue
185 */
186 static void add_op_status(imc_msg_t *msg)
187 {
188 pa_tnc_attr_t *attr;
189 time_t uptime, last_boot;
190
191 uptime = os->get_uptime(os);
192 last_boot = uptime ? time(NULL) - uptime : UNDEFINED_TIME;
193 if (last_boot != UNDEFINED_TIME)
194 {
195 DBG1(DBG_IMC, "last boot: %T, %u s ago", &last_boot, TRUE, uptime);
196 }
197 attr = ietf_attr_op_status_create(OP_STATUS_OPERATIONAL,
198 OP_RESULT_SUCCESSFUL, last_boot);
199 msg->add_attribute(msg, attr);
200 }
201
202 /**
203 * Add IETF Forwarding Enabled attribute to the send queue
204 */
205 static void add_fwd_enabled(imc_msg_t *msg)
206 {
207 pa_tnc_attr_t *attr;
208 os_fwd_status_t fwd_status;
209
210 fwd_status = os->get_fwd_status(os);
211 DBG1(DBG_IMC, "IPv4 forwarding status: %N",
212 os_fwd_status_names, fwd_status);
213 attr = ietf_attr_fwd_enabled_create(fwd_status);
214 msg->add_attribute(msg, attr);
215 }
216
217 /**
218 * Add IETF Factory Default Password Enabled attribute to the send queue
219 */
220 static void add_default_pwd_enabled(imc_msg_t *msg)
221 {
222 pa_tnc_attr_t *attr;
223
224 DBG1(DBG_IMC, "factory default password: disabled");
225 attr = ietf_attr_default_pwd_enabled_create(FALSE);
226 msg->add_attribute(msg, attr);
227 }
228
229 /**
230 * Add an IETF Installed Packages attribute to the send queue
231 */
232 static void add_installed_packages(imc_msg_t *msg)
233 {
234 pa_tnc_attr_t *attr;
235 ietf_attr_installed_packages_t *attr_cast;
236 chunk_t libc_name = { "libc-bin", 8 };
237 chunk_t libc_version = { "2.15-0ubuntu10.2", 16 };
238 chunk_t selinux_name = { "selinux", 7 };
239 chunk_t selinux_version = { "1:0.11", 6 };
240
241 attr = ietf_attr_installed_packages_create();
242 attr_cast = (ietf_attr_installed_packages_t*)attr;
243 attr_cast->add(attr_cast, libc_name, libc_version);
244 attr_cast->add(attr_cast, selinux_name, selinux_version);
245 msg->add_attribute(msg, attr);
246 }
247
248 /**
249 * see section 3.8.3 of TCG TNC IF-IMC Specification 1.3
250 */
251 TNC_Result TNC_IMC_BeginHandshake(TNC_IMCID imc_id,
252 TNC_ConnectionID connection_id)
253 {
254 imc_state_t *state;
255 imc_msg_t *out_msg;
256 TNC_Result result = TNC_RESULT_SUCCESS;
257
258 if (!imc_os)
259 {
260 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
261 return TNC_RESULT_NOT_INITIALIZED;
262 }
263 if (!imc_os->get_state(imc_os, connection_id, &state))
264 {
265 return TNC_RESULT_FATAL;
266 }
267 if (lib->settings->get_bool(lib->settings,
268 "libimcv.plugins.imc-os.send_info", TRUE))
269 {
270 out_msg = imc_msg_create(imc_os, state, connection_id, imc_id,
271 TNC_IMVID_ANY, msg_types[0]);
272 add_product_info(out_msg);
273 add_string_version(out_msg);
274 add_numeric_version(out_msg);
275 add_op_status(out_msg);
276 add_fwd_enabled(out_msg);
277 add_default_pwd_enabled(out_msg);
278
279 /* send PA-TNC message with the excl flag not set */
280 result = out_msg->send(out_msg, FALSE);
281 out_msg->destroy(out_msg);
282 }
283
284 return result;
285 }
286
287 static TNC_Result receive_message(imc_msg_t *in_msg)
288 {
289 imc_msg_t *out_msg;
290 enumerator_t *enumerator;
291 pa_tnc_attr_t *attr;
292 pen_type_t attr_type;
293 TNC_Result result;
294 bool fatal_error = FALSE;
295
296 /* parse received PA-TNC message and handle local and remote errors */
297 result = in_msg->receive(in_msg, &fatal_error);
298 if (result != TNC_RESULT_SUCCESS)
299 {
300 return result;
301 }
302 out_msg = imc_msg_create_as_reply(in_msg);
303
304 /* analyze PA-TNC attributes */
305 enumerator = in_msg->create_attribute_enumerator(in_msg);
306 while (enumerator->enumerate(enumerator, &attr))
307 {
308 attr_type = attr->get_type(attr);
309
310 if (attr_type.vendor_id != PEN_IETF)
311 {
312 continue;
313 }
314 if (attr_type.type == IETF_ATTR_ATTRIBUTE_REQUEST)
315 {
316 ietf_attr_attr_request_t *attr_cast;
317 pen_type_t *entry;
318 enumerator_t *e;
319
320 attr_cast = (ietf_attr_attr_request_t*)attr;
321
322 e = attr_cast->create_enumerator(attr_cast);
323 while (e->enumerate(e, &entry))
324 {
325 if (entry->vendor_id != PEN_IETF)
326 {
327 continue;
328 }
329 switch (entry->type)
330 {
331 case IETF_ATTR_PRODUCT_INFORMATION:
332 add_product_info(out_msg);
333 break;
334 case IETF_ATTR_STRING_VERSION:
335 add_string_version(out_msg);
336 break;
337 case IETF_ATTR_NUMERIC_VERSION:
338 add_numeric_version(out_msg);
339 break;
340 case IETF_ATTR_OPERATIONAL_STATUS:
341 add_op_status(out_msg);
342 break;
343 case IETF_ATTR_FORWARDING_ENABLED:
344 add_fwd_enabled(out_msg);
345 break;
346 case IETF_ATTR_FACTORY_DEFAULT_PWD_ENABLED:
347 add_default_pwd_enabled(out_msg);
348 break;
349 case IETF_ATTR_INSTALLED_PACKAGES:
350 add_installed_packages(out_msg);
351 break;
352 default:
353 break;
354 }
355 }
356 e->destroy(e);
357 }
358 else if (attr_type.type == IETF_ATTR_REMEDIATION_INSTRUCTIONS)
359 {
360 ietf_attr_remediation_instr_t *attr_cast;
361 pen_type_t parameters_type;
362 chunk_t parameters, string, lang_code;
363
364 attr_cast = (ietf_attr_remediation_instr_t*)attr;
365 parameters_type = attr_cast->get_parameters_type(attr_cast);
366 parameters = attr_cast->get_parameters(attr_cast);
367
368 if (parameters_type.vendor_id == PEN_IETF)
369 {
370 switch (parameters_type.type)
371 {
372 case IETF_REMEDIATION_PARAMETERS_URI:
373 DBG1(DBG_IMC, "remediation uri: '%.*s'",
374 parameters.len, parameters.ptr);
375 break;
376 case IETF_REMEDIATION_PARAMETERS_STRING:
377 string = attr_cast->get_string(attr_cast, &lang_code);
378 DBG1(DBG_IMC, "remediation string: '%.*s' [%.*s]",
379 string.len, string.ptr,
380 lang_code.len, lang_code.ptr);
381 break;
382 default:
383 DBG1(DBG_IMC, "remediation parameters %B", &parameters);
384 }
385 }
386 else
387 {
388 DBG1(DBG_IMC, "remediation parameters %B", &parameters);
389 }
390 }
391 }
392 enumerator->destroy(enumerator);
393
394 if (fatal_error)
395 {
396 result = TNC_RESULT_FATAL;
397 }
398 else
399 {
400 result = out_msg->send(out_msg, TRUE);
401 }
402 out_msg->destroy(out_msg);
403
404 return result;
405 }
406
407 /**
408 * see section 3.8.4 of TCG TNC IF-IMC Specification 1.3
409
410 */
411 TNC_Result TNC_IMC_ReceiveMessage(TNC_IMCID imc_id,
412 TNC_ConnectionID connection_id,
413 TNC_BufferReference msg,
414 TNC_UInt32 msg_len,
415 TNC_MessageType msg_type)
416 {
417 imc_state_t *state;
418 imc_msg_t *in_msg;
419 TNC_Result result;
420
421 if (!imc_os)
422 {
423 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
424 return TNC_RESULT_NOT_INITIALIZED;
425 }
426 if (!imc_os->get_state(imc_os, connection_id, &state))
427 {
428 return TNC_RESULT_FATAL;
429 }
430 in_msg = imc_msg_create_from_data(imc_os, state, connection_id, msg_type,
431 chunk_create(msg, msg_len));
432 result = receive_message(in_msg);
433 in_msg->destroy(in_msg);
434
435 return result;
436 }
437
438 /**
439 * see section 3.8.6 of TCG TNC IF-IMV Specification 1.3
440 */
441 TNC_Result TNC_IMC_ReceiveMessageLong(TNC_IMCID imc_id,
442 TNC_ConnectionID connection_id,
443 TNC_UInt32 msg_flags,
444 TNC_BufferReference msg,
445 TNC_UInt32 msg_len,
446 TNC_VendorID msg_vid,
447 TNC_MessageSubtype msg_subtype,
448 TNC_UInt32 src_imv_id,
449 TNC_UInt32 dst_imc_id)
450 {
451 imc_state_t *state;
452 imc_msg_t *in_msg;
453 TNC_Result result;
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 if (!imc_os->get_state(imc_os, connection_id, &state))
461 {
462 return TNC_RESULT_FATAL;
463 }
464 in_msg = imc_msg_create_from_long_data(imc_os, state, connection_id,
465 src_imv_id, dst_imc_id,msg_vid, msg_subtype,
466 chunk_create(msg, msg_len));
467 result =receive_message(in_msg);
468 in_msg->destroy(in_msg);
469
470 return result;
471 }
472
473 /**
474 * see section 3.8.7 of TCG TNC IF-IMC Specification 1.3
475 */
476 TNC_Result TNC_IMC_BatchEnding(TNC_IMCID imc_id,
477 TNC_ConnectionID connection_id)
478 {
479 if (!imc_os)
480 {
481 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
482 return TNC_RESULT_NOT_INITIALIZED;
483 }
484 return TNC_RESULT_SUCCESS;
485 }
486
487 /**
488 * see section 3.8.8 of TCG TNC IF-IMC Specification 1.3
489 */
490 TNC_Result TNC_IMC_Terminate(TNC_IMCID imc_id)
491 {
492 if (!imc_os)
493 {
494 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
495 return TNC_RESULT_NOT_INITIALIZED;
496 }
497 imc_os->destroy(imc_os);
498 imc_os = NULL;
499
500 os->destroy(os);
501 os = NULL;
502
503 return TNC_RESULT_SUCCESS;
504 }
505
506 /**
507 * see section 4.2.8.1 of TCG TNC IF-IMC Specification 1.3
508 */
509 TNC_Result TNC_IMC_ProvideBindFunction(TNC_IMCID imc_id,
510 TNC_TNCC_BindFunctionPointer bind_function)
511 {
512 if (!imc_os)
513 {
514 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
515 return TNC_RESULT_NOT_INITIALIZED;
516 }
517 return imc_os->bind_functions(imc_os, bind_function);
518 }