use countof()
[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, countof(msg_types),
62 imc_id, actual_version);
63 if (!imc_os)
64 {
65 return TNC_RESULT_FATAL;
66 }
67
68 os = os_info_create();
69 if (!os)
70 {
71 imc_os->destroy(imc_os);
72 imc_os = NULL;
73
74 return TNC_RESULT_FATAL;
75 }
76
77 if (min_version > TNC_IFIMC_VERSION_1 || max_version < TNC_IFIMC_VERSION_1)
78 {
79 DBG1(DBG_IMC, "no common IF-IMC version");
80 return TNC_RESULT_NO_COMMON_VERSION;
81 }
82 return TNC_RESULT_SUCCESS;
83 }
84
85 /**
86 * see section 3.8.2 of TCG TNC IF-IMC Specification 1.3
87 */
88 TNC_Result TNC_IMC_NotifyConnectionChange(TNC_IMCID imc_id,
89 TNC_ConnectionID connection_id,
90 TNC_ConnectionState new_state)
91 {
92 imc_state_t *state;
93
94 if (!imc_os)
95 {
96 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
97 return TNC_RESULT_NOT_INITIALIZED;
98 }
99 switch (new_state)
100 {
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)
107 {
108 return TNC_RESULT_FATAL;
109 }
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);
115 default:
116 return imc_os->change_state(imc_os, connection_id,
117 new_state, NULL);
118 }
119 }
120
121 /**
122 * Add IETF Product Information attribute to the send queue
123 */
124 static void add_product_info(imc_msg_t *msg)
125 {
126 pa_tnc_attr_t *attr;
127 chunk_t os_name;
128 pen_t vendor_id = PEN_IETF;
129 char *vendor;
130 int i;
131
132 typedef struct vendor_pen_t {
133 char *vendor;
134 pen_t pen;
135 } vendor_pen_t;
136
137 vendor_pen_t vendor_pens[] = {
138 { "Debian", PEN_DEBIAN },
139 { "Ubuntu", PEN_CANONICAL }
140 };
141
142 os_name = os->get_name(os);
143 for (i = 0; i < countof(vendor_pens); i++)
144 {
145 vendor = vendor_pens[i].vendor;
146 if (chunk_equals(os_name, chunk_create(vendor, strlen(vendor))))
147 {
148 vendor_id = vendor_pens[i].pen;
149 break;
150 }
151 }
152 attr = ietf_attr_product_info_create(vendor_id, 0, os_name);
153 msg->add_attribute(msg, attr);
154 }
155
156 /**
157 * Add IETF Numeric Version attribute to the send queue
158 */
159 static void add_numeric_version(imc_msg_t *msg)
160 {
161 pa_tnc_attr_t *attr;
162 u_int32_t major, minor;
163
164 os->get_numeric_version(os, &major, &minor);
165 DBG1(DBG_IMC, "operating system numeric version is %d.%d",
166 major, minor);
167
168 attr = ietf_attr_numeric_version_create(major, minor, 0, 0, 0);
169 msg->add_attribute(msg, attr);
170 }
171
172 /**
173 * Add IETF String Version attribute to the send queue
174 */
175 static void add_string_version(imc_msg_t *msg)
176 {
177 pa_tnc_attr_t *attr;
178
179 attr = ietf_attr_string_version_create(os->get_version(os),
180 chunk_empty, chunk_empty);
181 msg->add_attribute(msg, attr);
182 }
183
184 /**
185 * Add IETF Operational Status attribute to the send queue
186 */
187 static void add_op_status(imc_msg_t *msg)
188 {
189 pa_tnc_attr_t *attr;
190 time_t uptime, last_boot;
191
192 uptime = os->get_uptime(os);
193 last_boot = uptime ? time(NULL) - uptime : UNDEFINED_TIME;
194 if (last_boot != UNDEFINED_TIME)
195 {
196 DBG1(DBG_IMC, "last boot: %T, %u s ago", &last_boot, TRUE, uptime);
197 }
198 attr = ietf_attr_op_status_create(OP_STATUS_OPERATIONAL,
199 OP_RESULT_SUCCESSFUL, last_boot);
200 msg->add_attribute(msg, attr);
201 }
202
203 /**
204 * Add IETF Forwarding Enabled attribute to the send queue
205 */
206 static void add_fwd_enabled(imc_msg_t *msg)
207 {
208 pa_tnc_attr_t *attr;
209 os_fwd_status_t fwd_status;
210
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);
216 }
217
218 /**
219 * Add IETF Factory Default Password Enabled attribute to the send queue
220 */
221 static void add_default_pwd_enabled(imc_msg_t *msg)
222 {
223 pa_tnc_attr_t *attr;
224
225 DBG1(DBG_IMC, "factory default password: disabled");
226 attr = ietf_attr_default_pwd_enabled_create(FALSE);
227 msg->add_attribute(msg, attr);
228 }
229
230 /**
231 * Add an IETF Installed Packages attribute to the send queue
232 */
233 static void add_installed_packages(imc_msg_t *msg)
234 {
235 pa_tnc_attr_t *attr;
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 };
241
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);
247 }
248
249 /**
250 * see section 3.8.3 of TCG TNC IF-IMC Specification 1.3
251 */
252 TNC_Result TNC_IMC_BeginHandshake(TNC_IMCID imc_id,
253 TNC_ConnectionID connection_id)
254 {
255 imc_state_t *state;
256 imc_msg_t *out_msg;
257 TNC_Result result = TNC_RESULT_SUCCESS;
258
259 if (!imc_os)
260 {
261 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
262 return TNC_RESULT_NOT_INITIALIZED;
263 }
264 if (!imc_os->get_state(imc_os, connection_id, &state))
265 {
266 return TNC_RESULT_FATAL;
267 }
268 if (lib->settings->get_bool(lib->settings,
269 "libimcv.plugins.imc-os.send_info", TRUE))
270 {
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);
279
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);
283 }
284
285 return result;
286 }
287
288 static TNC_Result receive_message(imc_msg_t *in_msg)
289 {
290 imc_msg_t *out_msg;
291 enumerator_t *enumerator;
292 pa_tnc_attr_t *attr;
293 pen_type_t attr_type;
294 TNC_Result result;
295 bool fatal_error = FALSE;
296
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)
300 {
301 return result;
302 }
303 out_msg = imc_msg_create_as_reply(in_msg);
304
305 /* analyze PA-TNC attributes */
306 enumerator = in_msg->create_attribute_enumerator(in_msg);
307 while (enumerator->enumerate(enumerator, &attr))
308 {
309 attr_type = attr->get_type(attr);
310
311 if (attr_type.vendor_id != PEN_IETF)
312 {
313 continue;
314 }
315 if (attr_type.type == IETF_ATTR_ATTRIBUTE_REQUEST)
316 {
317 ietf_attr_attr_request_t *attr_cast;
318 pen_type_t *entry;
319 enumerator_t *e;
320
321 attr_cast = (ietf_attr_attr_request_t*)attr;
322
323 e = attr_cast->create_enumerator(attr_cast);
324 while (e->enumerate(e, &entry))
325 {
326 if (entry->vendor_id != PEN_IETF)
327 {
328 continue;
329 }
330 switch (entry->type)
331 {
332 case IETF_ATTR_PRODUCT_INFORMATION:
333 add_product_info(out_msg);
334 break;
335 case IETF_ATTR_STRING_VERSION:
336 add_string_version(out_msg);
337 break;
338 case IETF_ATTR_NUMERIC_VERSION:
339 add_numeric_version(out_msg);
340 break;
341 case IETF_ATTR_OPERATIONAL_STATUS:
342 add_op_status(out_msg);
343 break;
344 case IETF_ATTR_FORWARDING_ENABLED:
345 add_fwd_enabled(out_msg);
346 break;
347 case IETF_ATTR_FACTORY_DEFAULT_PWD_ENABLED:
348 add_default_pwd_enabled(out_msg);
349 break;
350 case IETF_ATTR_INSTALLED_PACKAGES:
351 add_installed_packages(out_msg);
352 break;
353 default:
354 break;
355 }
356 }
357 e->destroy(e);
358 }
359 else if (attr_type.type == IETF_ATTR_REMEDIATION_INSTRUCTIONS)
360 {
361 ietf_attr_remediation_instr_t *attr_cast;
362 pen_type_t parameters_type;
363 chunk_t parameters, string, lang_code;
364
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);
368
369 if (parameters_type.vendor_id == PEN_IETF)
370 {
371 switch (parameters_type.type)
372 {
373 case IETF_REMEDIATION_PARAMETERS_URI:
374 DBG1(DBG_IMC, "remediation uri: '%.*s'",
375 parameters.len, parameters.ptr);
376 break;
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);
382 break;
383 default:
384 DBG1(DBG_IMC, "remediation parameters %B", &parameters);
385 }
386 }
387 else
388 {
389 DBG1(DBG_IMC, "remediation parameters %B", &parameters);
390 }
391 }
392 }
393 enumerator->destroy(enumerator);
394
395 if (fatal_error)
396 {
397 result = TNC_RESULT_FATAL;
398 }
399 else
400 {
401 result = out_msg->send(out_msg, TRUE);
402 }
403 out_msg->destroy(out_msg);
404
405 return result;
406 }
407
408 /**
409 * see section 3.8.4 of TCG TNC IF-IMC Specification 1.3
410
411 */
412 TNC_Result TNC_IMC_ReceiveMessage(TNC_IMCID imc_id,
413 TNC_ConnectionID connection_id,
414 TNC_BufferReference msg,
415 TNC_UInt32 msg_len,
416 TNC_MessageType msg_type)
417 {
418 imc_state_t *state;
419 imc_msg_t *in_msg;
420 TNC_Result result;
421
422 if (!imc_os)
423 {
424 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
425 return TNC_RESULT_NOT_INITIALIZED;
426 }
427 if (!imc_os->get_state(imc_os, connection_id, &state))
428 {
429 return TNC_RESULT_FATAL;
430 }
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);
435
436 return result;
437 }
438
439 /**
440 * see section 3.8.6 of TCG TNC IF-IMV Specification 1.3
441 */
442 TNC_Result TNC_IMC_ReceiveMessageLong(TNC_IMCID imc_id,
443 TNC_ConnectionID connection_id,
444 TNC_UInt32 msg_flags,
445 TNC_BufferReference msg,
446 TNC_UInt32 msg_len,
447 TNC_VendorID msg_vid,
448 TNC_MessageSubtype msg_subtype,
449 TNC_UInt32 src_imv_id,
450 TNC_UInt32 dst_imc_id)
451 {
452 imc_state_t *state;
453 imc_msg_t *in_msg;
454 TNC_Result result;
455
456 if (!imc_os)
457 {
458 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
459 return TNC_RESULT_NOT_INITIALIZED;
460 }
461 if (!imc_os->get_state(imc_os, connection_id, &state))
462 {
463 return TNC_RESULT_FATAL;
464 }
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);
470
471 return result;
472 }
473
474 /**
475 * see section 3.8.7 of TCG TNC IF-IMC Specification 1.3
476 */
477 TNC_Result TNC_IMC_BatchEnding(TNC_IMCID imc_id,
478 TNC_ConnectionID connection_id)
479 {
480 if (!imc_os)
481 {
482 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
483 return TNC_RESULT_NOT_INITIALIZED;
484 }
485 return TNC_RESULT_SUCCESS;
486 }
487
488 /**
489 * see section 3.8.8 of TCG TNC IF-IMC Specification 1.3
490 */
491 TNC_Result TNC_IMC_Terminate(TNC_IMCID imc_id)
492 {
493 if (!imc_os)
494 {
495 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
496 return TNC_RESULT_NOT_INITIALIZED;
497 }
498 imc_os->destroy(imc_os);
499 imc_os = NULL;
500
501 os->destroy(os);
502 os = NULL;
503
504 return TNC_RESULT_SUCCESS;
505 }
506
507 /**
508 * see section 4.2.8.1 of TCG TNC IF-IMC Specification 1.3
509 */
510 TNC_Result TNC_IMC_ProvideBindFunction(TNC_IMCID imc_id,
511 TNC_TNCC_BindFunctionPointer bind_function)
512 {
513 if (!imc_os)
514 {
515 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
516 return TNC_RESULT_NOT_INITIALIZED;
517 }
518 return imc_os->bind_functions(imc_os, bind_function);
519 }