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