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