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