f2e00da5a7b138c91accafe6236b7a7324fe3b35
[strongswan.git] / src / libimcv / plugins / imv_os / imv_os.c
1 /*
2 * Copyright (C) 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 "imv_os_state.h"
17 #include "imv_os_database.h"
18
19 #include <imv/imv_agent.h>
20 #include <imv/imv_msg.h>
21 #include <ietf/ietf_attr.h>
22 #include <ietf/ietf_attr_attr_request.h>
23 #include <ietf/ietf_attr_default_pwd_enabled.h>
24 #include <ietf/ietf_attr_fwd_enabled.h>
25 #include <ietf/ietf_attr_installed_packages.h>
26 #include <ietf/ietf_attr_numeric_version.h>
27 #include <ietf/ietf_attr_op_status.h>
28 #include <ietf/ietf_attr_pa_tnc_error.h>
29 #include <ietf/ietf_attr_product_info.h>
30 #include <ietf/ietf_attr_remediation_instr.h>
31 #include <ietf/ietf_attr_string_version.h>
32 #include <ita/ita_attr.h>
33 #include <ita/ita_attr_get_settings.h>
34 #include <ita/ita_attr_settings.h>
35 #include <ita/ita_attr_angel.h>
36
37 #include <tncif_names.h>
38 #include <tncif_pa_subtypes.h>
39
40 #include <pen/pen.h>
41 #include <collections/linked_list.h>
42 #include <utils/debug.h>
43 #include <utils/lexparser.h>
44
45 /* IMV definitions */
46
47 static const char imv_name[] = "OS";
48
49 static pen_type_t msg_types[] = {
50 { PEN_IETF, PA_SUBTYPE_IETF_OPERATING_SYSTEM }
51 };
52
53 static imv_agent_t *imv_os;
54
55 /**
56 * IMV OS database
57 */
58 static imv_os_database_t *os_db;
59
60 /*
61 * see section 3.8.1 of TCG TNC IF-IMV Specification 1.3
62 */
63 TNC_Result TNC_IMV_Initialize(TNC_IMVID imv_id,
64 TNC_Version min_version,
65 TNC_Version max_version,
66 TNC_Version *actual_version)
67 {
68 char *uri;
69
70 if (imv_os)
71 {
72 DBG1(DBG_IMV, "IMV \"%s\" has already been initialized", imv_name);
73 return TNC_RESULT_ALREADY_INITIALIZED;
74 }
75 imv_os = imv_agent_create(imv_name, msg_types, countof(msg_types),
76 imv_id, actual_version);
77 if (!imv_os)
78 {
79 return TNC_RESULT_FATAL;
80 }
81 if (min_version > TNC_IFIMV_VERSION_1 || max_version < TNC_IFIMV_VERSION_1)
82 {
83 DBG1(DBG_IMV, "no common IF-IMV version");
84 return TNC_RESULT_NO_COMMON_VERSION;
85 }
86
87 /* attach OS database */
88 uri = lib->settings->get_str(lib->settings,
89 "libimcv.plugins.imv-os.database", NULL);
90 if (uri)
91 {
92 os_db = imv_os_database_create(uri);
93 }
94
95 return TNC_RESULT_SUCCESS;
96 }
97
98 /**
99 * see section 3.8.2 of TCG TNC IF-IMV Specification 1.3
100 */
101 TNC_Result TNC_IMV_NotifyConnectionChange(TNC_IMVID imv_id,
102 TNC_ConnectionID connection_id,
103 TNC_ConnectionState new_state)
104 {
105 imv_state_t *state;
106
107 if (!imv_os)
108 {
109 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
110 return TNC_RESULT_NOT_INITIALIZED;
111 }
112 switch (new_state)
113 {
114 case TNC_CONNECTION_STATE_CREATE:
115 state = imv_os_state_create(connection_id);
116 return imv_os->create_state(imv_os, state);
117 case TNC_CONNECTION_STATE_DELETE:
118 return imv_os->delete_state(imv_os, connection_id);
119 default:
120 return imv_os->change_state(imv_os, connection_id,
121 new_state, NULL);
122 }
123 }
124
125 /**
126 * print multi-line values to debug output
127 */
128 static void dbg_imv_multi_line(chunk_t value)
129 {
130 chunk_t line;
131
132 while (extract_token(&line, '\n', &value))
133 {
134 DBG2(DBG_IMV, " %.*s", line.len, line.ptr);
135 }
136 if (value.len)
137 {
138 DBG2(DBG_IMV, " %.*s", value.len, value.ptr);
139 }
140 }
141
142 static TNC_Result receive_message(imv_state_t *state, imv_msg_t *in_msg)
143 {
144 imv_msg_t *out_msg;
145 imv_os_state_t *os_state;
146 enumerator_t *enumerator;
147 pa_tnc_attr_t *attr;
148 pen_type_t type;
149 TNC_Result result;
150 chunk_t os_name = chunk_empty;
151 chunk_t os_version = chunk_empty;
152 bool fatal_error = FALSE, assessment = FALSE;
153
154 os_state = (imv_os_state_t*)state;
155
156 /* parse received PA-TNC message and handle local and remote errors */
157 result = in_msg->receive(in_msg, &fatal_error);
158 if (result != TNC_RESULT_SUCCESS)
159 {
160 return result;
161 }
162
163 out_msg = imv_msg_create_as_reply(in_msg);
164
165 /* analyze PA-TNC attributes */
166 enumerator = in_msg->create_attribute_enumerator(in_msg);
167 while (enumerator->enumerate(enumerator, &attr))
168 {
169 type = attr->get_type(attr);
170
171 if (type.vendor_id == PEN_IETF)
172 {
173 switch (type.type)
174 {
175 case IETF_ATTR_PRODUCT_INFORMATION:
176 {
177 ietf_attr_product_info_t *attr_cast;
178 pen_t vendor_id;
179
180 attr_cast = (ietf_attr_product_info_t*)attr;
181 os_name = attr_cast->get_info(attr_cast, &vendor_id, NULL);
182 if (vendor_id != PEN_IETF)
183 {
184 DBG1(DBG_IMV, "operating system name is '%.*s' "
185 "from vendor %N", os_name.len, os_name.ptr,
186 pen_names, vendor_id);
187 }
188 else
189 {
190 DBG1(DBG_IMV, "operating system name is '%.*s'",
191 os_name.len, os_name.ptr);
192 }
193 break;
194 }
195 case IETF_ATTR_STRING_VERSION:
196 {
197 ietf_attr_string_version_t *attr_cast;
198
199 attr_cast = (ietf_attr_string_version_t*)attr;
200 os_version = attr_cast->get_version(attr_cast, NULL, NULL);
201 if (os_version.len)
202 {
203 DBG1(DBG_IMV, "operating system version is '%.*s'",
204 os_version.len, os_version.ptr);
205 }
206 break;
207 }
208 case IETF_ATTR_NUMERIC_VERSION:
209 {
210 ietf_attr_numeric_version_t *attr_cast;
211 u_int32_t major, minor;
212
213 attr_cast = (ietf_attr_numeric_version_t*)attr;
214 attr_cast->get_version(attr_cast, &major, &minor);
215 DBG1(DBG_IMV, "operating system numeric version is %d.%d",
216 major, minor);
217 break;
218 }
219 case IETF_ATTR_OPERATIONAL_STATUS:
220 {
221 ietf_attr_op_status_t *attr_cast;
222 op_status_t op_status;
223 op_result_t op_result;
224 time_t last_boot;
225
226 attr_cast = (ietf_attr_op_status_t*)attr;
227 op_status = attr_cast->get_status(attr_cast);
228 op_result = attr_cast->get_result(attr_cast);
229 last_boot = attr_cast->get_last_use(attr_cast);
230 DBG1(DBG_IMV, "operational status: %N, result: %N",
231 op_status_names, op_status, op_result_names, op_result);
232 DBG1(DBG_IMV, "last boot: %T", &last_boot, TRUE);
233 break;
234 }
235 case IETF_ATTR_FORWARDING_ENABLED:
236 {
237 ietf_attr_fwd_enabled_t *attr_cast;
238 os_fwd_status_t fwd_status;
239
240 attr_cast = (ietf_attr_fwd_enabled_t*)attr;
241 fwd_status = attr_cast->get_status(attr_cast);
242 DBG1(DBG_IMV, "IPv4 forwarding status: %N",
243 os_fwd_status_names, fwd_status);
244 break;
245 }
246 case IETF_ATTR_FACTORY_DEFAULT_PWD_ENABLED:
247 {
248 ietf_attr_default_pwd_enabled_t *attr_cast;
249 bool default_pwd_status;
250
251 attr_cast = (ietf_attr_default_pwd_enabled_t*)attr;
252 default_pwd_status = attr_cast->get_status(attr_cast);
253 DBG1(DBG_IMV, "factory default password: %sabled",
254 default_pwd_status ? "en":"dis");
255 break;
256 }
257 case IETF_ATTR_INSTALLED_PACKAGES:
258 {
259 ietf_attr_installed_packages_t *attr_cast;
260 enumerator_t *e;
261 status_t status;
262
263 /* Received at least one Installed Packages attribute */
264 os_state->set_package_request(os_state, FALSE);
265
266 if (!os_db)
267 {
268 break;
269 }
270 attr_cast = (ietf_attr_installed_packages_t*)attr;
271
272 e = attr_cast->create_enumerator(attr_cast);
273 status = os_db->check_packages(os_db,
274 os_state->get_info(os_state), e);
275 e->destroy(e);
276
277 switch (status)
278 {
279 case VERIFY_ERROR:
280 state->set_recommendation(state,
281 TNC_IMV_ACTION_RECOMMENDATION_ISOLATE,
282 TNC_IMV_EVALUATION_RESULT_NONCOMPLIANT_MINOR);
283 assessment = TRUE;
284 break;
285 case FAILED:
286 state->set_recommendation(state,
287 TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION,
288 TNC_IMV_EVALUATION_RESULT_ERROR);
289 assessment = TRUE;
290 break;
291 default:
292 break;
293 }
294 break;
295 }
296 default:
297 break;
298 }
299 }
300 else if (type.vendor_id == PEN_ITA)
301 {
302 switch (type.type)
303 {
304 case ITA_ATTR_SETTINGS:
305 {
306 ita_attr_settings_t *attr_cast;
307 enumerator_t *e;
308 char *name;
309 chunk_t value;
310
311 attr_cast = (ita_attr_settings_t*)attr;
312 e = attr_cast->create_enumerator(attr_cast);
313 while (e->enumerate(e, &name, &value))
314 {
315 DBG1(DBG_IMV, "setting '%s'", name);
316 dbg_imv_multi_line(value);
317 }
318 e->destroy(e);
319 break;
320 }
321 case ITA_ATTR_START_ANGEL:
322 os_state->set_angel_count(os_state, TRUE);
323 break;
324 case ITA_ATTR_STOP_ANGEL:
325 os_state->set_angel_count(os_state, FALSE);
326 break;
327 default:
328 break;
329 }
330 }
331 }
332 enumerator->destroy(enumerator);
333
334 if (os_name.len && os_version.len)
335 {
336 char *product_info;
337 char *uri = "http://remediation.strongswan.org/fix-it/";
338 char *string = "use a Linux operating system instead of Windows 1.2.3";
339 char *lang_code = "en";
340
341 os_state->set_info(os_state, os_name, os_version);
342 product_info = os_state->get_info(os_state);
343
344 if (streq(product_info, "Windows 1.2.3"))
345 {
346 DBG1(DBG_IMV, "OS '%s' is not supported", product_info);
347
348 attr = ietf_attr_remediation_instr_create_from_string(
349 chunk_create(string, strlen(string)),
350 chunk_create(lang_code, strlen(lang_code)));
351 out_msg->add_attribute(out_msg, attr);
352 attr = ietf_attr_remediation_instr_create_from_uri(
353 chunk_create(uri, strlen(uri)));
354 out_msg->add_attribute(out_msg, attr);
355
356 state->set_recommendation(state,
357 TNC_IMV_ACTION_RECOMMENDATION_ISOLATE,
358 TNC_IMV_EVALUATION_RESULT_NONCOMPLIANT_MINOR);
359 assessment = TRUE;
360 }
361 else
362 {
363 ita_attr_get_settings_t *attr_cast;
364
365 DBG1(DBG_IMV, "requesting installed packages for '%s'",
366 product_info);
367 os_state->set_package_request(os_state, TRUE);
368 attr = ietf_attr_attr_request_create(PEN_IETF,
369 IETF_ATTR_INSTALLED_PACKAGES);
370 out_msg->add_attribute(out_msg, attr);
371
372 /* requesting Android or Linux settings */
373 attr = ita_attr_get_settings_create();
374 attr_cast = (ita_attr_get_settings_t*)attr;
375
376 if (chunk_equals(os_name, chunk_create("Android", 7)))
377 {
378 attr_cast->add(attr_cast, "android_id");
379 attr_cast->add(attr_cast, "install_non_market_apps");
380 }
381 else
382 {
383 attr_cast->add(attr_cast, "/proc/sys/kernel/random/boot_id");
384 attr_cast->add(attr_cast, "/proc/sys/kernel/tainted");
385 }
386 out_msg->add_attribute(out_msg, attr);
387 }
388 }
389
390 if (fatal_error)
391 {
392 state->set_recommendation(state,
393 TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION,
394 TNC_IMV_EVALUATION_RESULT_ERROR);
395 assessment = TRUE;
396 }
397
398 /* If all Installed Packages attributes were received, go to assessment */
399 if (!assessment &&
400 !os_state->get_package_request(os_state) &&
401 !os_state->get_angel_count(os_state))
402 {
403 state->set_recommendation(state, TNC_IMV_ACTION_RECOMMENDATION_ALLOW,
404 TNC_IMV_EVALUATION_RESULT_COMPLIANT);
405 assessment = TRUE;
406 }
407
408 if (assessment)
409 {
410 result = out_msg->send_assessment(out_msg);
411 out_msg->destroy(out_msg);
412 if (result != TNC_RESULT_SUCCESS)
413 {
414 return result;
415 }
416 return imv_os->provide_recommendation(imv_os, state);
417 }
418
419 /* send PA-TNC message with excl flag set */
420 result = out_msg->send(out_msg, TRUE);
421 out_msg->destroy(out_msg);
422
423 return result;
424 }
425
426 /**
427 * see section 3.8.4 of TCG TNC IF-IMV Specification 1.3
428 */
429 TNC_Result TNC_IMV_ReceiveMessage(TNC_IMVID imv_id,
430 TNC_ConnectionID connection_id,
431 TNC_BufferReference msg,
432 TNC_UInt32 msg_len,
433 TNC_MessageType msg_type)
434 {
435 imv_state_t *state;
436 imv_msg_t *in_msg;
437 TNC_Result result;
438
439 if (!imv_os)
440 {
441 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
442 return TNC_RESULT_NOT_INITIALIZED;
443 }
444 if (!imv_os->get_state(imv_os, connection_id, &state))
445 {
446 return TNC_RESULT_FATAL;
447 }
448 in_msg = imv_msg_create_from_data(imv_os, state, connection_id, msg_type,
449 chunk_create(msg, msg_len));
450 result = receive_message(state, in_msg);
451 in_msg->destroy(in_msg);
452
453 return result;
454 }
455
456 /**
457 * see section 3.8.6 of TCG TNC IF-IMV Specification 1.3
458 */
459 TNC_Result TNC_IMV_ReceiveMessageLong(TNC_IMVID imv_id,
460 TNC_ConnectionID connection_id,
461 TNC_UInt32 msg_flags,
462 TNC_BufferReference msg,
463 TNC_UInt32 msg_len,
464 TNC_VendorID msg_vid,
465 TNC_MessageSubtype msg_subtype,
466 TNC_UInt32 src_imc_id,
467 TNC_UInt32 dst_imv_id)
468 {
469 imv_state_t *state;
470 imv_msg_t *in_msg;
471 TNC_Result result;
472
473 if (!imv_os)
474 {
475 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
476 return TNC_RESULT_NOT_INITIALIZED;
477 }
478 if (!imv_os->get_state(imv_os, connection_id, &state))
479 {
480 return TNC_RESULT_FATAL;
481 }
482 in_msg = imv_msg_create_from_long_data(imv_os, state, connection_id,
483 src_imc_id, dst_imv_id, msg_vid, msg_subtype,
484 chunk_create(msg, msg_len));
485 result =receive_message(state, in_msg);
486 in_msg->destroy(in_msg);
487
488 return result;
489 }
490
491 /**
492 * see section 3.8.7 of TCG TNC IF-IMV Specification 1.3
493 */
494 TNC_Result TNC_IMV_SolicitRecommendation(TNC_IMVID imv_id,
495 TNC_ConnectionID connection_id)
496 {
497 imv_state_t *state;
498
499 if (!imv_os)
500 {
501 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
502 return TNC_RESULT_NOT_INITIALIZED;
503 }
504 if (!imv_os->get_state(imv_os, connection_id, &state))
505 {
506 return TNC_RESULT_FATAL;
507 }
508 return imv_os->provide_recommendation(imv_os, state);
509 }
510
511 /**
512 * see section 3.8.8 of TCG TNC IF-IMV Specification 1.3
513 */
514 TNC_Result TNC_IMV_BatchEnding(TNC_IMVID imv_id,
515 TNC_ConnectionID connection_id)
516 {
517 imv_state_t *state;
518 imv_os_state_t *os_state;
519 TNC_Result result = TNC_RESULT_SUCCESS;
520
521 if (!imv_os)
522 {
523 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
524 return TNC_RESULT_NOT_INITIALIZED;
525 }
526 if (!imv_os->get_state(imv_os, connection_id, &state))
527 {
528 return TNC_RESULT_FATAL;
529 }
530 os_state = (imv_os_state_t*)state;
531
532 if (os_state->get_info(os_state) == NULL)
533 {
534 imv_msg_t *out_msg;
535 pa_tnc_attr_t *attr;
536 ietf_attr_attr_request_t *attr_cast;
537
538 out_msg = imv_msg_create(imv_os, state, connection_id, imv_id,
539 TNC_IMCID_ANY, msg_types[0]);
540 attr = ietf_attr_attr_request_create(PEN_IETF,
541 IETF_ATTR_PRODUCT_INFORMATION);
542 attr_cast = (ietf_attr_attr_request_t*)attr;
543 attr_cast->add(attr_cast, PEN_IETF, IETF_ATTR_STRING_VERSION);
544 attr_cast->add(attr_cast, PEN_IETF, IETF_ATTR_NUMERIC_VERSION);
545 attr_cast->add(attr_cast, PEN_IETF, IETF_ATTR_OPERATIONAL_STATUS);
546 attr_cast->add(attr_cast, PEN_IETF, IETF_ATTR_FORWARDING_ENABLED);
547 attr_cast->add(attr_cast, PEN_IETF, IETF_ATTR_FACTORY_DEFAULT_PWD_ENABLED);
548 out_msg->add_attribute(out_msg, attr);
549
550 /* send PA-TNC message with excl flag not set */
551 result = out_msg->send(out_msg, FALSE);
552 out_msg->destroy(out_msg);
553 }
554
555 return result;
556 }
557
558 /**
559 * see section 3.8.9 of TCG TNC IF-IMV Specification 1.3
560 */
561 TNC_Result TNC_IMV_Terminate(TNC_IMVID imv_id)
562 {
563 if (!imv_os)
564 {
565 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
566 return TNC_RESULT_NOT_INITIALIZED;
567 }
568 DESTROY_IF(os_db);
569
570 imv_os->destroy(imv_os);
571 imv_os = NULL;
572
573 return TNC_RESULT_SUCCESS;
574 }
575
576 /**
577 * see section 4.2.8.1 of TCG TNC IF-IMV Specification 1.3
578 */
579 TNC_Result TNC_IMV_ProvideBindFunction(TNC_IMVID imv_id,
580 TNC_TNCS_BindFunctionPointer bind_function)
581 {
582 if (!imv_os)
583 {
584 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
585 return TNC_RESULT_NOT_INITIALIZED;
586 }
587 return imv_os->bind_functions(imv_os, bind_function);
588 }