Added ITA Start/Stop Angel attributes to split bulk data into multiple attributes
[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 (!os_state->get_package_request(os_state) &&
364 !os_state->get_angel_count(os_state))
365 {
366 state->set_recommendation(state, TNC_IMV_ACTION_RECOMMENDATION_ALLOW,
367 TNC_IMV_EVALUATION_RESULT_COMPLIANT);
368 assessment = TRUE;
369 }
370
371 if (assessment)
372 {
373 result = out_msg->send_assessment(out_msg);
374 out_msg->destroy(out_msg);
375 if (result != TNC_RESULT_SUCCESS)
376 {
377 return result;
378 }
379 return imv_os->provide_recommendation(imv_os, state);
380 }
381
382 /* send PA-TNC message with excl flag set */
383 result = out_msg->send(out_msg, TRUE);
384 out_msg->destroy(out_msg);
385
386 return result;
387 }
388
389 /**
390 * see section 3.8.4 of TCG TNC IF-IMV Specification 1.3
391 */
392 TNC_Result TNC_IMV_ReceiveMessage(TNC_IMVID imv_id,
393 TNC_ConnectionID connection_id,
394 TNC_BufferReference msg,
395 TNC_UInt32 msg_len,
396 TNC_MessageType msg_type)
397 {
398 imv_state_t *state;
399 imv_msg_t *in_msg;
400 TNC_Result result;
401
402 if (!imv_os)
403 {
404 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
405 return TNC_RESULT_NOT_INITIALIZED;
406 }
407 if (!imv_os->get_state(imv_os, connection_id, &state))
408 {
409 return TNC_RESULT_FATAL;
410 }
411 in_msg = imv_msg_create_from_data(imv_os, state, connection_id, msg_type,
412 chunk_create(msg, msg_len));
413 result = receive_message(state, in_msg);
414 in_msg->destroy(in_msg);
415
416 return result;
417 }
418
419 /**
420 * see section 3.8.6 of TCG TNC IF-IMV Specification 1.3
421 */
422 TNC_Result TNC_IMV_ReceiveMessageLong(TNC_IMVID imv_id,
423 TNC_ConnectionID connection_id,
424 TNC_UInt32 msg_flags,
425 TNC_BufferReference msg,
426 TNC_UInt32 msg_len,
427 TNC_VendorID msg_vid,
428 TNC_MessageSubtype msg_subtype,
429 TNC_UInt32 src_imc_id,
430 TNC_UInt32 dst_imv_id)
431 {
432 imv_state_t *state;
433 imv_msg_t *in_msg;
434 TNC_Result result;
435
436 if (!imv_os)
437 {
438 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
439 return TNC_RESULT_NOT_INITIALIZED;
440 }
441 if (!imv_os->get_state(imv_os, connection_id, &state))
442 {
443 return TNC_RESULT_FATAL;
444 }
445 in_msg = imv_msg_create_from_long_data(imv_os, state, connection_id,
446 src_imc_id, dst_imv_id, msg_vid, msg_subtype,
447 chunk_create(msg, msg_len));
448 result =receive_message(state, in_msg);
449 in_msg->destroy(in_msg);
450
451 return result;
452 }
453
454 /**
455 * see section 3.8.7 of TCG TNC IF-IMV Specification 1.3
456 */
457 TNC_Result TNC_IMV_SolicitRecommendation(TNC_IMVID imv_id,
458 TNC_ConnectionID connection_id)
459 {
460 imv_state_t *state;
461
462 if (!imv_os)
463 {
464 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
465 return TNC_RESULT_NOT_INITIALIZED;
466 }
467 if (!imv_os->get_state(imv_os, connection_id, &state))
468 {
469 return TNC_RESULT_FATAL;
470 }
471 return imv_os->provide_recommendation(imv_os, state);
472 }
473
474 /**
475 * see section 3.8.8 of TCG TNC IF-IMV Specification 1.3
476 */
477 TNC_Result TNC_IMV_BatchEnding(TNC_IMVID imv_id,
478 TNC_ConnectionID connection_id)
479 {
480 imv_state_t *state;
481 imv_os_state_t *os_state;
482 TNC_Result result = TNC_RESULT_SUCCESS;
483
484 if (!imv_os)
485 {
486 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
487 return TNC_RESULT_NOT_INITIALIZED;
488 }
489 if (!imv_os->get_state(imv_os, connection_id, &state))
490 {
491 return TNC_RESULT_FATAL;
492 }
493 os_state = (imv_os_state_t*)state;
494
495 if (os_state->get_info(os_state) == NULL)
496 {
497 imv_msg_t *out_msg;
498 pa_tnc_attr_t *attr;
499 ietf_attr_attr_request_t *attr_cast;
500
501 out_msg = imv_msg_create(imv_os, state, connection_id, imv_id,
502 TNC_IMCID_ANY, msg_types[0]);
503 attr = ietf_attr_attr_request_create(PEN_IETF,
504 IETF_ATTR_PRODUCT_INFORMATION);
505 attr_cast = (ietf_attr_attr_request_t*)attr;
506 attr_cast->add(attr_cast, PEN_IETF, IETF_ATTR_STRING_VERSION);
507 attr_cast->add(attr_cast, PEN_IETF, IETF_ATTR_NUMERIC_VERSION);
508 attr_cast->add(attr_cast, PEN_IETF, IETF_ATTR_OPERATIONAL_STATUS);
509 attr_cast->add(attr_cast, PEN_IETF, IETF_ATTR_FORWARDING_ENABLED);
510 attr_cast->add(attr_cast, PEN_IETF, IETF_ATTR_FACTORY_DEFAULT_PWD_ENABLED);
511 out_msg->add_attribute(out_msg, attr);
512
513 /* send PA-TNC message with excl flag not set */
514 result = out_msg->send(out_msg, FALSE);
515 out_msg->destroy(out_msg);
516 }
517
518 return result;
519 }
520
521 /**
522 * see section 3.8.9 of TCG TNC IF-IMV Specification 1.3
523 */
524 TNC_Result TNC_IMV_Terminate(TNC_IMVID imv_id)
525 {
526 if (!imv_os)
527 {
528 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
529 return TNC_RESULT_NOT_INITIALIZED;
530 }
531 imv_os->destroy(imv_os);
532 imv_os = NULL;
533
534 return TNC_RESULT_SUCCESS;
535 }
536
537 /**
538 * see section 4.2.8.1 of TCG TNC IF-IMV Specification 1.3
539 */
540 TNC_Result TNC_IMV_ProvideBindFunction(TNC_IMVID imv_id,
541 TNC_TNCS_BindFunctionPointer bind_function)
542 {
543 if (!imv_os)
544 {
545 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
546 return TNC_RESULT_NOT_INITIALIZED;
547 }
548 return imv_os->bind_functions(imv_os, bind_function);
549 }