generate reason strings and remediation instructions for improper OS settings
[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 char non_market_apps_str[] = "install_non_market_apps";
154
155 os_state = (imv_os_state_t*)state;
156
157 /* parse received PA-TNC message and handle local and remote errors */
158 result = in_msg->receive(in_msg, &fatal_error);
159 if (result != TNC_RESULT_SUCCESS)
160 {
161 return result;
162 }
163
164 out_msg = imv_msg_create_as_reply(in_msg);
165
166 /* analyze PA-TNC attributes */
167 enumerator = in_msg->create_attribute_enumerator(in_msg);
168 while (enumerator->enumerate(enumerator, &attr))
169 {
170 type = attr->get_type(attr);
171
172 if (type.vendor_id == PEN_IETF)
173 {
174 switch (type.type)
175 {
176 case IETF_ATTR_PRODUCT_INFORMATION:
177 {
178 ietf_attr_product_info_t *attr_cast;
179 pen_t vendor_id;
180
181 attr_cast = (ietf_attr_product_info_t*)attr;
182 os_name = attr_cast->get_info(attr_cast, &vendor_id, NULL);
183 if (vendor_id != PEN_IETF)
184 {
185 DBG1(DBG_IMV, "operating system name is '%.*s' "
186 "from vendor %N", os_name.len, os_name.ptr,
187 pen_names, vendor_id);
188 }
189 else
190 {
191 DBG1(DBG_IMV, "operating system name is '%.*s'",
192 os_name.len, os_name.ptr);
193 }
194 break;
195 }
196 case IETF_ATTR_STRING_VERSION:
197 {
198 ietf_attr_string_version_t *attr_cast;
199
200 attr_cast = (ietf_attr_string_version_t*)attr;
201 os_version = attr_cast->get_version(attr_cast, NULL, NULL);
202 if (os_version.len)
203 {
204 DBG1(DBG_IMV, "operating system version is '%.*s'",
205 os_version.len, os_version.ptr);
206 }
207 break;
208 }
209 case IETF_ATTR_NUMERIC_VERSION:
210 {
211 ietf_attr_numeric_version_t *attr_cast;
212 u_int32_t major, minor;
213
214 attr_cast = (ietf_attr_numeric_version_t*)attr;
215 attr_cast->get_version(attr_cast, &major, &minor);
216 DBG1(DBG_IMV, "operating system numeric version is %d.%d",
217 major, minor);
218 break;
219 }
220 case IETF_ATTR_OPERATIONAL_STATUS:
221 {
222 ietf_attr_op_status_t *attr_cast;
223 op_status_t op_status;
224 op_result_t op_result;
225 time_t last_boot;
226
227 attr_cast = (ietf_attr_op_status_t*)attr;
228 op_status = attr_cast->get_status(attr_cast);
229 op_result = attr_cast->get_result(attr_cast);
230 last_boot = attr_cast->get_last_use(attr_cast);
231 DBG1(DBG_IMV, "operational status: %N, result: %N",
232 op_status_names, op_status, op_result_names, op_result);
233 DBG1(DBG_IMV, "last boot: %T", &last_boot, TRUE);
234 break;
235 }
236 case IETF_ATTR_FORWARDING_ENABLED:
237 {
238 ietf_attr_fwd_enabled_t *attr_cast;
239 os_fwd_status_t fwd_status;
240
241 attr_cast = (ietf_attr_fwd_enabled_t*)attr;
242 fwd_status = attr_cast->get_status(attr_cast);
243 DBG1(DBG_IMV, "IPv4 forwarding status: %N",
244 os_fwd_status_names, fwd_status);
245 if (fwd_status == OS_FWD_ENABLED)
246 {
247 os_state->set_os_settings(os_state,
248 OS_SETTINGS_FWD_ENABLED);
249 }
250 break;
251 }
252 case IETF_ATTR_FACTORY_DEFAULT_PWD_ENABLED:
253 {
254 ietf_attr_default_pwd_enabled_t *attr_cast;
255 bool default_pwd_status;
256
257 attr_cast = (ietf_attr_default_pwd_enabled_t*)attr;
258 default_pwd_status = attr_cast->get_status(attr_cast);
259 DBG1(DBG_IMV, "factory default password: %sabled",
260 default_pwd_status ? "en":"dis");
261 if (default_pwd_status)
262 {
263 os_state->set_os_settings(os_state,
264 OS_SETTINGS_DEFAULT_PWD_ENABLED);
265 }
266 break;
267 }
268 case IETF_ATTR_INSTALLED_PACKAGES:
269 {
270 ietf_attr_installed_packages_t *attr_cast;
271 enumerator_t *e;
272 status_t status;
273
274 /* Received at least one Installed Packages attribute */
275 os_state->set_package_request(os_state, FALSE);
276
277 if (!os_db)
278 {
279 break;
280 }
281 attr_cast = (ietf_attr_installed_packages_t*)attr;
282
283 e = attr_cast->create_enumerator(attr_cast);
284 status = os_db->check_packages(os_db, os_state, e);
285 e->destroy(e);
286
287 if (status == FAILED)
288 {
289 state->set_recommendation(state,
290 TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION,
291 TNC_IMV_EVALUATION_RESULT_ERROR);
292 assessment = TRUE;
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 if (streq(name, non_market_apps_str) &&
316 chunk_equals(value, chunk_from_chars('1')))
317 {
318 os_state->set_os_settings(os_state,
319 OS_SETTINGS_NON_MARKET_APPS);
320 }
321 DBG1(DBG_IMV, "setting '%s'", name);
322 dbg_imv_multi_line(value);
323 }
324 e->destroy(e);
325 break;
326 }
327 case ITA_ATTR_START_ANGEL:
328 os_state->set_angel_count(os_state, TRUE);
329 break;
330 case ITA_ATTR_STOP_ANGEL:
331 os_state->set_angel_count(os_state, FALSE);
332 break;
333 default:
334 break;
335 }
336 }
337 }
338 enumerator->destroy(enumerator);
339
340 if (os_name.len && os_version.len)
341 {
342 os_type_t os_type;
343 ita_attr_get_settings_t *attr_cast;
344
345 /* set the OS type, name and version */
346 os_type = os_type_from_name(os_name);
347 os_state->set_info(os_state,os_type, os_name, os_version);
348
349 /* requesting installed packages */
350 os_state->set_package_request(os_state, TRUE);
351 attr = ietf_attr_attr_request_create(PEN_IETF,
352 IETF_ATTR_INSTALLED_PACKAGES);
353 out_msg->add_attribute(out_msg, attr);
354
355 /* requesting Android or Linux settings */
356 attr = ita_attr_get_settings_create();
357 attr_cast = (ita_attr_get_settings_t*)attr;
358
359 if (os_type == OS_TYPE_ANDROID)
360 {
361 attr_cast->add(attr_cast, "android_id");
362 attr_cast->add(attr_cast, non_market_apps_str);
363 }
364 else
365 {
366 attr_cast->add(attr_cast, "/proc/sys/kernel/random/boot_id");
367 attr_cast->add(attr_cast, "/proc/sys/kernel/tainted");
368 }
369 out_msg->add_attribute(out_msg, attr);
370 }
371
372 if (fatal_error)
373 {
374 state->set_recommendation(state,
375 TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION,
376 TNC_IMV_EVALUATION_RESULT_ERROR);
377 assessment = TRUE;
378 }
379
380 /* If all Installed Packages attributes were received, go to assessment */
381 if (!assessment &&
382 !os_state->get_package_request(os_state) &&
383 !os_state->get_angel_count(os_state))
384 {
385 int count, count_update, count_blacklist, count_ok;
386
387 os_state->get_count(os_state, &count, &count_update, &count_blacklist,
388 &count_ok);
389 DBG1(DBG_IMV, "processed %d packages: %d not updated, %d blacklisted, "
390 "%d ok, %d not found", count, count_update, count_blacklist,
391 count_ok, count - count_update - count_blacklist - count_ok);
392
393 if (count_update || count_blacklist ||
394 os_state->get_os_settings(os_state))
395 {
396 state->set_recommendation(state,
397 TNC_IMV_ACTION_RECOMMENDATION_ISOLATE,
398 TNC_IMV_EVALUATION_RESULT_NONCOMPLIANT_MINOR);
399 }
400 else
401 {
402 state->set_recommendation(state,
403 TNC_IMV_ACTION_RECOMMENDATION_ALLOW,
404 TNC_IMV_EVALUATION_RESULT_COMPLIANT);
405 }
406 assessment = TRUE;
407 }
408
409 if (assessment)
410 {
411 result = out_msg->send_assessment(out_msg);
412 out_msg->destroy(out_msg);
413 if (result != TNC_RESULT_SUCCESS)
414 {
415 return result;
416 }
417 return imv_os->provide_recommendation(imv_os, state);
418 }
419
420 /* send PA-TNC message with excl flag set */
421 result = out_msg->send(out_msg, TRUE);
422 out_msg->destroy(out_msg);
423
424 return result;
425 }
426
427 /**
428 * see section 3.8.4 of TCG TNC IF-IMV Specification 1.3
429 */
430 TNC_Result TNC_IMV_ReceiveMessage(TNC_IMVID imv_id,
431 TNC_ConnectionID connection_id,
432 TNC_BufferReference msg,
433 TNC_UInt32 msg_len,
434 TNC_MessageType msg_type)
435 {
436 imv_state_t *state;
437 imv_msg_t *in_msg;
438 TNC_Result result;
439
440 if (!imv_os)
441 {
442 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
443 return TNC_RESULT_NOT_INITIALIZED;
444 }
445 if (!imv_os->get_state(imv_os, connection_id, &state))
446 {
447 return TNC_RESULT_FATAL;
448 }
449 in_msg = imv_msg_create_from_data(imv_os, state, connection_id, msg_type,
450 chunk_create(msg, msg_len));
451 result = receive_message(state, in_msg);
452 in_msg->destroy(in_msg);
453
454 return result;
455 }
456
457 /**
458 * see section 3.8.6 of TCG TNC IF-IMV Specification 1.3
459 */
460 TNC_Result TNC_IMV_ReceiveMessageLong(TNC_IMVID imv_id,
461 TNC_ConnectionID connection_id,
462 TNC_UInt32 msg_flags,
463 TNC_BufferReference msg,
464 TNC_UInt32 msg_len,
465 TNC_VendorID msg_vid,
466 TNC_MessageSubtype msg_subtype,
467 TNC_UInt32 src_imc_id,
468 TNC_UInt32 dst_imv_id)
469 {
470 imv_state_t *state;
471 imv_msg_t *in_msg;
472 TNC_Result result;
473
474 if (!imv_os)
475 {
476 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
477 return TNC_RESULT_NOT_INITIALIZED;
478 }
479 if (!imv_os->get_state(imv_os, connection_id, &state))
480 {
481 return TNC_RESULT_FATAL;
482 }
483 in_msg = imv_msg_create_from_long_data(imv_os, state, connection_id,
484 src_imc_id, dst_imv_id, msg_vid, msg_subtype,
485 chunk_create(msg, msg_len));
486 result =receive_message(state, in_msg);
487 in_msg->destroy(in_msg);
488
489 return result;
490 }
491
492 /**
493 * see section 3.8.7 of TCG TNC IF-IMV Specification 1.3
494 */
495 TNC_Result TNC_IMV_SolicitRecommendation(TNC_IMVID imv_id,
496 TNC_ConnectionID connection_id)
497 {
498 imv_state_t *state;
499
500 if (!imv_os)
501 {
502 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
503 return TNC_RESULT_NOT_INITIALIZED;
504 }
505 if (!imv_os->get_state(imv_os, connection_id, &state))
506 {
507 return TNC_RESULT_FATAL;
508 }
509 return imv_os->provide_recommendation(imv_os, state);
510 }
511
512 /**
513 * see section 3.8.8 of TCG TNC IF-IMV Specification 1.3
514 */
515 TNC_Result TNC_IMV_BatchEnding(TNC_IMVID imv_id,
516 TNC_ConnectionID connection_id)
517 {
518 imv_state_t *state;
519 imv_os_state_t *os_state;
520 TNC_Result result = TNC_RESULT_SUCCESS;
521
522 if (!imv_os)
523 {
524 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
525 return TNC_RESULT_NOT_INITIALIZED;
526 }
527 if (!imv_os->get_state(imv_os, connection_id, &state))
528 {
529 return TNC_RESULT_FATAL;
530 }
531 os_state = (imv_os_state_t*)state;
532
533 if (os_state->get_info(os_state, NULL, NULL, NULL) == NULL)
534 {
535 imv_msg_t *out_msg;
536 pa_tnc_attr_t *attr;
537 ietf_attr_attr_request_t *attr_cast;
538
539 out_msg = imv_msg_create(imv_os, state, connection_id, imv_id,
540 TNC_IMCID_ANY, msg_types[0]);
541 attr = ietf_attr_attr_request_create(PEN_IETF,
542 IETF_ATTR_PRODUCT_INFORMATION);
543 attr_cast = (ietf_attr_attr_request_t*)attr;
544 attr_cast->add(attr_cast, PEN_IETF, IETF_ATTR_STRING_VERSION);
545 attr_cast->add(attr_cast, PEN_IETF, IETF_ATTR_NUMERIC_VERSION);
546 attr_cast->add(attr_cast, PEN_IETF, IETF_ATTR_OPERATIONAL_STATUS);
547 attr_cast->add(attr_cast, PEN_IETF, IETF_ATTR_FORWARDING_ENABLED);
548 attr_cast->add(attr_cast, PEN_IETF, IETF_ATTR_FACTORY_DEFAULT_PWD_ENABLED);
549 out_msg->add_attribute(out_msg, attr);
550
551 /* send PA-TNC message with excl flag not set */
552 result = out_msg->send(out_msg, FALSE);
553 out_msg->destroy(out_msg);
554 }
555
556 return result;
557 }
558
559 /**
560 * see section 3.8.9 of TCG TNC IF-IMV Specification 1.3
561 */
562 TNC_Result TNC_IMV_Terminate(TNC_IMVID imv_id)
563 {
564 if (!imv_os)
565 {
566 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
567 return TNC_RESULT_NOT_INITIALIZED;
568 }
569 DESTROY_IF(os_db);
570
571 imv_os->destroy(imv_os);
572 imv_os = NULL;
573
574 return TNC_RESULT_SUCCESS;
575 }
576
577 /**
578 * see section 4.2.8.1 of TCG TNC IF-IMV Specification 1.3
579 */
580 TNC_Result TNC_IMV_ProvideBindFunction(TNC_IMVID imv_id,
581 TNC_TNCS_BindFunctionPointer bind_function)
582 {
583 if (!imv_os)
584 {
585 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
586 return TNC_RESULT_NOT_INITIALIZED;
587 }
588 return imv_os->bind_functions(imv_os, bind_function);
589 }