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