84a24b48f85b9d0cea523ebf7e48e935b429270d
[strongswan.git] / src / libimcv / plugins / imv_os / imv_os_agent.c
1 /*
2 * Copyright (C) 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
16 #define _GNU_SOURCE
17 #include <stdio.h>
18
19 #include "imv_os_agent.h"
20 #include "imv_os_state.h"
21 #include "imv_os_database.h"
22
23 #include <imcv.h>
24 #include <imv/imv_agent.h>
25 #include <imv/imv_msg.h>
26 #include <ietf/ietf_attr.h>
27 #include <ietf/ietf_attr_attr_request.h>
28 #include <ietf/ietf_attr_default_pwd_enabled.h>
29 #include <ietf/ietf_attr_fwd_enabled.h>
30 #include <ietf/ietf_attr_installed_packages.h>
31 #include <ietf/ietf_attr_numeric_version.h>
32 #include <ietf/ietf_attr_op_status.h>
33 #include <ietf/ietf_attr_pa_tnc_error.h>
34 #include <ietf/ietf_attr_product_info.h>
35 #include <ietf/ietf_attr_remediation_instr.h>
36 #include <ietf/ietf_attr_string_version.h>
37 #include <ita/ita_attr.h>
38 #include <ita/ita_attr_get_settings.h>
39 #include <ita/ita_attr_settings.h>
40 #include <ita/ita_attr_angel.h>
41 #include <ita/ita_attr_device_id.h>
42
43 #include <tncif_names.h>
44 #include <tncif_pa_subtypes.h>
45
46 #include <pen/pen.h>
47 #include <utils/debug.h>
48
49 typedef struct private_imv_os_agent_t private_imv_os_agent_t;
50 typedef enum imv_os_attr_t imv_os_attr_t;
51
52 /* Subscribed PA-TNC message subtypes */
53 static pen_type_t msg_types[] = {
54 { PEN_IETF, PA_SUBTYPE_IETF_OPERATING_SYSTEM }
55 };
56
57 static char unknown_source_str[] = "install_non_market_apps";
58
59 /**
60 * Flag set when corresponding attribute has been received
61 */
62 enum imv_os_attr_t {
63 IMV_OS_ATTR_PRODUCT_INFORMATION = (1<<0),
64 IMV_OS_ATTR_STRING_VERSION = (1<<1),
65 IMV_OS_ATTR_NUMERIC_VERSION = (1<<2),
66 IMV_OS_ATTR_OPERATIONAL_STATUS = (1<<3),
67 IMV_OS_ATTR_FORWARDING_ENABLED = (1<<4),
68 IMV_OS_ATTR_FACTORY_DEFAULT_PWD_ENABLED = (1<<5),
69 IMV_OS_ATTR_DEVICE_ID = (1<<6),
70 IMV_OS_ATTR_MUST = (1<<7)-1,
71 IMV_OS_ATTR_INSTALLED_PACKAGES = (1<<7),
72 IMV_OS_ATTR_SETTINGS = (1<<8)
73 };
74
75 /**
76 * Private data of an imv_os_agent_t object.
77 */
78 struct private_imv_os_agent_t {
79
80 /**
81 * Public members of imv_os_agent_t
82 */
83 imv_agent_if_t public;
84
85 /**
86 * IMV agent responsible for generic functions
87 */
88 imv_agent_t *agent;
89
90 /**
91 * IMV OS database
92 */
93 imv_os_database_t *db;
94
95 };
96
97 METHOD(imv_agent_if_t, bind_functions, TNC_Result,
98 private_imv_os_agent_t *this, TNC_TNCS_BindFunctionPointer bind_function)
99 {
100 return this->agent->bind_functions(this->agent, bind_function);
101 }
102
103 METHOD(imv_agent_if_t, notify_connection_change, TNC_Result,
104 private_imv_os_agent_t *this, TNC_ConnectionID id,
105 TNC_ConnectionState new_state)
106 {
107 TNC_IMV_Action_Recommendation rec;
108 imv_state_t *state;
109 imv_session_t *session;
110
111 switch (new_state)
112 {
113 case TNC_CONNECTION_STATE_CREATE:
114 state = imv_os_state_create(id);
115 return this->agent->create_state(this->agent, state);
116 case TNC_CONNECTION_STATE_DELETE:
117 return this->agent->delete_state(this->agent, id);
118 case TNC_CONNECTION_STATE_ACCESS_ALLOWED:
119 case TNC_CONNECTION_STATE_ACCESS_ISOLATED:
120 case TNC_CONNECTION_STATE_ACCESS_NONE:
121 if (imcv_db && this->agent->get_state(this->agent, id, &state))
122 {
123 switch (new_state)
124 {
125 case TNC_CONNECTION_STATE_ACCESS_ALLOWED:
126 rec = TNC_IMV_ACTION_RECOMMENDATION_ALLOW;
127 break;
128 case TNC_CONNECTION_STATE_ACCESS_ISOLATED:
129 rec = TNC_IMV_ACTION_RECOMMENDATION_ISOLATE;
130 break;
131 case TNC_CONNECTION_STATE_ACCESS_NONE:
132 default:
133 rec = TNC_IMV_ACTION_RECOMMENDATION_NO_ACCESS;
134 }
135 session = state->get_session(state);
136 imcv_db->add_recommendation(imcv_db, session, rec);
137 imcv_db->policy_script(imcv_db, session, FALSE);
138 }
139 /* fall through to default state */
140 default:
141 return this->agent->change_state(this->agent, id, new_state, NULL);
142 }
143 }
144
145 /**
146 * Process a received message
147 */
148 static TNC_Result receive_msg(private_imv_os_agent_t *this, imv_state_t *state,
149 imv_msg_t *in_msg)
150 {
151 imv_msg_t *out_msg;
152 imv_os_state_t *os_state;
153 enumerator_t *enumerator;
154 pa_tnc_attr_t *attr;
155 pen_type_t type;
156 TNC_Result result;
157 chunk_t os_name = chunk_empty;
158 chunk_t os_version = chunk_empty;
159 bool fatal_error = FALSE, assessment = FALSE;
160
161 os_state = (imv_os_state_t*)state;
162
163 /* parse received PA-TNC message and handle local and remote errors */
164 result = in_msg->receive(in_msg, &fatal_error);
165 if (result != TNC_RESULT_SUCCESS)
166 {
167 return result;
168 }
169
170 out_msg = imv_msg_create_as_reply(in_msg);
171
172 /* analyze PA-TNC attributes */
173 enumerator = in_msg->create_attribute_enumerator(in_msg);
174 while (enumerator->enumerate(enumerator, &attr))
175 {
176 type = attr->get_type(attr);
177
178 if (type.vendor_id == PEN_IETF)
179 {
180 switch (type.type)
181 {
182 case IETF_ATTR_PRODUCT_INFORMATION:
183 {
184 ietf_attr_product_info_t *attr_cast;
185 pen_t vendor_id;
186
187 state->set_action_flags(state,
188 IMV_OS_ATTR_PRODUCT_INFORMATION);
189 attr_cast = (ietf_attr_product_info_t*)attr;
190 os_name = attr_cast->get_info(attr_cast, &vendor_id, NULL);
191 if (vendor_id != PEN_IETF)
192 {
193 DBG1(DBG_IMV, "operating system name is '%.*s' "
194 "from vendor %N", os_name.len, os_name.ptr,
195 pen_names, vendor_id);
196 }
197 else
198 {
199 DBG1(DBG_IMV, "operating system name is '%.*s'",
200 os_name.len, os_name.ptr);
201 }
202 break;
203 }
204 case IETF_ATTR_STRING_VERSION:
205 {
206 ietf_attr_string_version_t *attr_cast;
207
208 state->set_action_flags(state,
209 IMV_OS_ATTR_STRING_VERSION);
210 attr_cast = (ietf_attr_string_version_t*)attr;
211 os_version = attr_cast->get_version(attr_cast, NULL, NULL);
212 if (os_version.len)
213 {
214 DBG1(DBG_IMV, "operating system version is '%.*s'",
215 os_version.len, os_version.ptr);
216 }
217 break;
218 }
219 case IETF_ATTR_NUMERIC_VERSION:
220 {
221 ietf_attr_numeric_version_t *attr_cast;
222 u_int32_t major, minor;
223
224 state->set_action_flags(state,
225 IMV_OS_ATTR_NUMERIC_VERSION);
226 attr_cast = (ietf_attr_numeric_version_t*)attr;
227 attr_cast->get_version(attr_cast, &major, &minor);
228 DBG1(DBG_IMV, "operating system numeric version is %d.%d",
229 major, minor);
230 break;
231 }
232 case IETF_ATTR_OPERATIONAL_STATUS:
233 {
234 ietf_attr_op_status_t *attr_cast;
235 op_status_t op_status;
236 op_result_t op_result;
237 time_t last_boot;
238
239 state->set_action_flags(state,
240 IMV_OS_ATTR_OPERATIONAL_STATUS);
241 attr_cast = (ietf_attr_op_status_t*)attr;
242 op_status = attr_cast->get_status(attr_cast);
243 op_result = attr_cast->get_result(attr_cast);
244 last_boot = attr_cast->get_last_use(attr_cast);
245 DBG1(DBG_IMV, "operational status: %N, result: %N",
246 op_status_names, op_status, op_result_names, op_result);
247 DBG1(DBG_IMV, "last boot: %T", &last_boot, TRUE);
248 break;
249 }
250 case IETF_ATTR_FORWARDING_ENABLED:
251 {
252 ietf_attr_fwd_enabled_t *attr_cast;
253 os_fwd_status_t fwd_status;
254
255 state->set_action_flags(state,
256 IMV_OS_ATTR_FORWARDING_ENABLED);
257 attr_cast = (ietf_attr_fwd_enabled_t*)attr;
258 fwd_status = attr_cast->get_status(attr_cast);
259 DBG1(DBG_IMV, "IPv4 forwarding is %N",
260 os_fwd_status_names, fwd_status);
261 if (fwd_status == OS_FWD_ENABLED)
262 {
263 os_state->set_os_settings(os_state,
264 OS_SETTINGS_FWD_ENABLED);
265 }
266 break;
267 }
268 case IETF_ATTR_FACTORY_DEFAULT_PWD_ENABLED:
269 {
270 ietf_attr_default_pwd_enabled_t *attr_cast;
271 bool default_pwd_status;
272
273 state->set_action_flags(state,
274 IMV_OS_ATTR_FACTORY_DEFAULT_PWD_ENABLED);
275 attr_cast = (ietf_attr_default_pwd_enabled_t*)attr;
276 default_pwd_status = attr_cast->get_status(attr_cast);
277 DBG1(DBG_IMV, "factory default password is %sabled",
278 default_pwd_status ? "en":"dis");
279 if (default_pwd_status)
280 {
281 os_state->set_os_settings(os_state,
282 OS_SETTINGS_DEFAULT_PWD_ENABLED);
283 }
284 break;
285 }
286 case IETF_ATTR_INSTALLED_PACKAGES:
287 {
288 ietf_attr_installed_packages_t *attr_cast;
289 enumerator_t *e;
290 status_t status;
291
292 state->set_action_flags(state,
293 IMV_OS_ATTR_INSTALLED_PACKAGES);
294 if (!this->db)
295 {
296 break;
297 }
298 attr_cast = (ietf_attr_installed_packages_t*)attr;
299
300 e = attr_cast->create_enumerator(attr_cast);
301 status = this->db->check_packages(this->db, os_state, e);
302 e->destroy(e);
303
304 if (status == FAILED)
305 {
306 state->set_recommendation(state,
307 TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION,
308 TNC_IMV_EVALUATION_RESULT_ERROR);
309 assessment = TRUE;
310 }
311 break;
312 }
313 default:
314 break;
315 }
316 }
317 else if (type.vendor_id == PEN_ITA)
318 {
319 switch (type.type)
320 {
321 case ITA_ATTR_SETTINGS:
322 {
323 ita_attr_settings_t *attr_cast;
324 enumerator_t *e;
325 char *name;
326 chunk_t value;
327
328 state->set_action_flags(state, IMV_OS_ATTR_SETTINGS);
329
330 attr_cast = (ita_attr_settings_t*)attr;
331 e = attr_cast->create_enumerator(attr_cast);
332 while (e->enumerate(e, &name, &value))
333 {
334 if (streq(name, unknown_source_str) &&
335 chunk_equals(value, chunk_from_chars('1')))
336 {
337 os_state->set_os_settings(os_state,
338 OS_SETTINGS_UNKNOWN_SOURCE);
339 }
340 DBG1(DBG_IMV, "setting '%s'\n %.*s",
341 name, value.len, value.ptr);
342 }
343 e->destroy(e);
344 break;
345 }
346 case ITA_ATTR_DEVICE_ID:
347 {
348 chunk_t value;
349
350 state->set_action_flags(state, IMV_OS_ATTR_DEVICE_ID);
351
352 value = attr->get_value(attr);
353 os_state->set_device_id(os_state, value);
354 DBG1(DBG_IMV, "device ID is %.*s", value.len, value.ptr);
355 break;
356 }
357 case ITA_ATTR_START_ANGEL:
358 os_state->set_angel_count(os_state, TRUE);
359 break;
360 case ITA_ATTR_STOP_ANGEL:
361 os_state->set_angel_count(os_state, FALSE);
362 break;
363 default:
364 break;
365 }
366 }
367 }
368 enumerator->destroy(enumerator);
369
370 /**
371 * The IETF Product Information and String Version attributes
372 * are supposed to arrive in the same PA-TNC message
373 */
374 if (os_name.len && os_version.len)
375 {
376 os_type_t os_type;
377
378 /* set the OS type, name and version */
379 os_type = os_type_from_name(os_name);
380 os_state->set_info(os_state,os_type, os_name, os_version);
381
382 if (imcv_db)
383 {
384 imcv_db->add_product(imcv_db, state->get_session(state),
385 os_state->get_info(os_state, NULL, NULL, NULL));
386 }
387 }
388
389 if (fatal_error)
390 {
391 state->set_recommendation(state,
392 TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION,
393 TNC_IMV_EVALUATION_RESULT_ERROR);
394 assessment = TRUE;
395 }
396
397 if (assessment)
398 {
399 os_state->set_handshake_state(os_state, IMV_OS_STATE_END);
400 result = out_msg->send_assessment(out_msg);
401 out_msg->destroy(out_msg);
402 if (result != TNC_RESULT_SUCCESS)
403 {
404 return result;
405 }
406 return this->agent->provide_recommendation(this->agent, state);
407 }
408
409 /* send PA-TNC message with excl flag set */
410 result = out_msg->send(out_msg, TRUE);
411 out_msg->destroy(out_msg);
412
413 return result;
414 }
415
416 METHOD(imv_agent_if_t, receive_message, TNC_Result,
417 private_imv_os_agent_t *this, TNC_ConnectionID id,
418 TNC_MessageType msg_type, chunk_t msg)
419 {
420 imv_state_t *state;
421 imv_msg_t *in_msg;
422 TNC_Result result;
423
424 if (!this->agent->get_state(this->agent, id, &state))
425 {
426 return TNC_RESULT_FATAL;
427 }
428 in_msg = imv_msg_create_from_data(this->agent, state, id, msg_type, msg);
429 result = receive_msg(this, state, in_msg);
430 in_msg->destroy(in_msg);
431
432 return result;
433 }
434
435 METHOD(imv_agent_if_t, receive_message_long, TNC_Result,
436 private_imv_os_agent_t *this, TNC_ConnectionID id,
437 TNC_UInt32 src_imc_id, TNC_UInt32 dst_imv_id,
438 TNC_VendorID msg_vid, TNC_MessageSubtype msg_subtype, chunk_t msg)
439 {
440 imv_state_t *state;
441 imv_msg_t *in_msg;
442 TNC_Result result;
443
444 if (!this->agent->get_state(this->agent, id, &state))
445 {
446 return TNC_RESULT_FATAL;
447 }
448 in_msg = imv_msg_create_from_long_data(this->agent, state, id,
449 src_imc_id, dst_imv_id, msg_vid, msg_subtype, msg);
450 result = receive_msg(this, state, in_msg);
451 in_msg->destroy(in_msg);
452
453 return result;
454
455 }
456
457 /**
458 * Build an IETF Attribute Request attribute for missing attributes
459 */
460 static pa_tnc_attr_t* build_attr_request(u_int32_t received)
461 {
462 pa_tnc_attr_t *attr;
463 ietf_attr_attr_request_t *attr_cast;
464
465 attr = ietf_attr_attr_request_create(PEN_RESERVED, 0);
466 attr_cast = (ietf_attr_attr_request_t*)attr;
467
468 if (!(received & IMV_OS_ATTR_PRODUCT_INFORMATION) ||
469 !(received & IMV_OS_ATTR_STRING_VERSION))
470 {
471 attr_cast->add(attr_cast, PEN_IETF, IETF_ATTR_PRODUCT_INFORMATION);
472 attr_cast->add(attr_cast, PEN_IETF, IETF_ATTR_STRING_VERSION);
473 }
474 if (!(received & IMV_OS_ATTR_NUMERIC_VERSION))
475 {
476 attr_cast->add(attr_cast, PEN_IETF, IETF_ATTR_NUMERIC_VERSION);
477 }
478 if (!(received & IMV_OS_ATTR_OPERATIONAL_STATUS))
479 {
480 attr_cast->add(attr_cast, PEN_IETF, IETF_ATTR_OPERATIONAL_STATUS);
481 }
482 if (!(received & IMV_OS_ATTR_FORWARDING_ENABLED))
483 {
484 attr_cast->add(attr_cast, PEN_IETF, IETF_ATTR_FORWARDING_ENABLED);
485 }
486 if (!(received & IMV_OS_ATTR_FACTORY_DEFAULT_PWD_ENABLED))
487 {
488 attr_cast->add(attr_cast, PEN_IETF,
489 IETF_ATTR_FACTORY_DEFAULT_PWD_ENABLED);
490 }
491 if (!(received & IMV_OS_ATTR_DEVICE_ID))
492 {
493 attr_cast->add(attr_cast, PEN_ITA, ITA_ATTR_DEVICE_ID);
494 }
495
496 return attr;
497 }
498
499 METHOD(imv_agent_if_t, batch_ending, TNC_Result,
500 private_imv_os_agent_t *this, TNC_ConnectionID id)
501 {
502 imv_msg_t *out_msg;
503 imv_state_t *state;
504 imv_session_t *session;
505 imv_workitem_t *workitem;
506 imv_os_state_t *os_state;
507 imv_os_handshake_state_t handshake_state;
508 pa_tnc_attr_t *attr;
509 TNC_IMVID imv_id;
510 TNC_Result result = TNC_RESULT_SUCCESS;
511 bool no_workitems = TRUE;
512 enumerator_t *enumerator;
513 u_int32_t received;
514
515 if (!this->agent->get_state(this->agent, id, &state))
516 {
517 return TNC_RESULT_FATAL;
518 }
519 os_state = (imv_os_state_t*)state;
520 handshake_state = os_state->get_handshake_state(os_state);
521 received = state->get_action_flags(state);
522 session = state->get_session(state);
523 imv_id = this->agent->get_id(this->agent);
524
525 if (handshake_state == IMV_OS_STATE_END)
526 {
527 return TNC_RESULT_SUCCESS;
528 }
529
530 /* create an empty out message - we might need it */
531 out_msg = imv_msg_create(this->agent, state, id, imv_id, TNC_IMCID_ANY,
532 msg_types[0]);
533
534 if (handshake_state == IMV_OS_STATE_INIT)
535 {
536 if ((received & IMV_OS_ATTR_MUST) != IMV_OS_ATTR_MUST)
537 {
538 /* create attribute request for missing mandatory attributes */
539 out_msg->add_attribute(out_msg, build_attr_request(received));
540 }
541 }
542
543 if (handshake_state < IMV_OS_STATE_POLICY_START)
544 {
545 if (((received & IMV_OS_ATTR_PRODUCT_INFORMATION) &&
546 (received & IMV_OS_ATTR_STRING_VERSION)) &&
547 ((received & IMV_OS_ATTR_DEVICE_ID) ||
548 (handshake_state == IMV_OS_STATE_ATTR_REQ)))
549 {
550 if (imcv_db)
551 {
552 imcv_db->add_device(imcv_db, session,
553 os_state->get_device_id(os_state));
554
555 /* trigger the policy manager */
556 imcv_db->policy_script(imcv_db, session, TRUE);
557 }
558 else
559 {
560 DBG2(DBG_IMV, "no workitems available - no evaluation possible");
561 state->set_recommendation(state,
562 TNC_IMV_ACTION_RECOMMENDATION_ALLOW,
563 TNC_IMV_EVALUATION_RESULT_DONT_KNOW);
564 }
565 handshake_state = IMV_OS_STATE_POLICY_START;
566 }
567 else if (handshake_state == IMV_OS_STATE_ATTR_REQ)
568 {
569 /**
570 * both the IETF Product Information and IETF String Version
571 * attribute should have been present
572 */
573 state->set_recommendation(state,
574 TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION,
575 TNC_IMV_EVALUATION_RESULT_ERROR);
576
577 /* send assessment */
578 result = out_msg->send_assessment(out_msg);
579 out_msg->destroy(out_msg);
580
581 if (result != TNC_RESULT_SUCCESS)
582 {
583 return result;
584 }
585 return this->agent->provide_recommendation(this->agent, state);
586 }
587 else
588 {
589 handshake_state = IMV_OS_STATE_ATTR_REQ;
590 }
591 os_state->set_handshake_state(os_state, handshake_state);
592 }
593
594 if (handshake_state == IMV_OS_STATE_POLICY_START && session)
595 {
596 enumerator = session->create_workitem_enumerator(session);
597 if (enumerator)
598 {
599 while (enumerator->enumerate(enumerator, &workitem))
600 {
601 if (workitem->get_imv_id(workitem) != TNC_IMVID_ANY)
602 {
603 continue;
604 }
605
606 switch (workitem->get_type(workitem))
607 {
608 case IMV_WORKITEM_PACKAGES:
609 attr = ietf_attr_attr_request_create(PEN_IETF,
610 IETF_ATTR_INSTALLED_PACKAGES);
611 out_msg->add_attribute(out_msg, attr);
612 break;
613 case IMV_WORKITEM_UNKNOWN_SOURCE:
614 attr = ita_attr_get_settings_create(unknown_source_str);
615 out_msg->add_attribute(out_msg, attr);
616 break;
617 case IMV_WORKITEM_FORWARDING:
618 case IMV_WORKITEM_DEFAULT_PWD:
619 break;
620 default:
621 continue;
622 }
623 workitem->set_imv_id(workitem, imv_id);
624 no_workitems = FALSE;
625 }
626 enumerator->destroy(enumerator);
627
628 if (no_workitems)
629 {
630 DBG2(DBG_IMV, "IMV %d has no workitems - "
631 "no evaluation requested", imv_id);
632 state->set_recommendation(state,
633 TNC_IMV_ACTION_RECOMMENDATION_ALLOW,
634 TNC_IMV_EVALUATION_RESULT_DONT_KNOW);
635 }
636 handshake_state = IMV_OS_STATE_WORKITEMS;
637 os_state->set_handshake_state(os_state, handshake_state);
638 }
639 }
640
641 if (handshake_state == IMV_OS_STATE_WORKITEMS && session)
642 {
643 TNC_IMV_Evaluation_Result eval;
644 TNC_IMV_Action_Recommendation rec;
645 char result_str[BUF_LEN];
646 bool fail;
647
648 enumerator = session->create_workitem_enumerator(session);
649 while (enumerator->enumerate(enumerator, &workitem))
650 {
651 if (workitem->get_imv_id(workitem) != imv_id)
652 {
653 continue;
654 }
655 eval = TNC_IMV_EVALUATION_RESULT_DONT_KNOW;
656
657 switch (workitem->get_type(workitem))
658 {
659 case IMV_WORKITEM_PACKAGES:
660 {
661 int count, count_update, count_blacklist, count_ok;
662
663 if (!(received & IMV_OS_ATTR_INSTALLED_PACKAGES) ||
664 os_state->get_angel_count(os_state))
665 {
666 continue;
667 }
668 os_state->get_count(os_state, &count, &count_update,
669 &count_blacklist, &count_ok);
670 fail = count_update || count_blacklist;
671 eval = fail ? TNC_IMV_EVALUATION_RESULT_NONCOMPLIANT_MINOR :
672 TNC_IMV_EVALUATION_RESULT_COMPLIANT;
673 snprintf(result_str, BUF_LEN, "processed %d packages: "
674 "%d not updated, %d blacklisted, %d ok, "
675 "%d not found",
676 count, count_update, count_blacklist, count_ok,
677 count - count_update - count_blacklist - count_ok);
678 break;
679 }
680 case IMV_WORKITEM_UNKNOWN_SOURCE:
681 if (!(received & IMV_OS_ATTR_SETTINGS))
682 {
683 continue;
684 }
685 fail = os_state->get_os_settings(os_state) &
686 OS_SETTINGS_UNKNOWN_SOURCE;
687 eval = fail ? TNC_IMV_EVALUATION_RESULT_NONCOMPLIANT_MINOR :
688 TNC_IMV_EVALUATION_RESULT_COMPLIANT;
689 snprintf(result_str, BUF_LEN, "unknown sources%s enabled",
690 fail ? "" : " not");
691 break;
692 case IMV_WORKITEM_FORWARDING:
693 if (!(received & IMV_OS_ATTR_FORWARDING_ENABLED))
694 {
695 continue;
696 }
697 fail = os_state->get_os_settings(os_state) &
698 OS_SETTINGS_FWD_ENABLED;
699 eval = fail ? TNC_IMV_EVALUATION_RESULT_NONCOMPLIANT_MAJOR :
700 TNC_IMV_EVALUATION_RESULT_COMPLIANT;
701 snprintf(result_str, BUF_LEN, "forwarding%s enabled",
702 fail ? "" : " not");
703 break;
704 case IMV_WORKITEM_DEFAULT_PWD:
705 if (!(received & IMV_OS_ATTR_FACTORY_DEFAULT_PWD_ENABLED))
706 {
707 continue;
708 }
709 fail = os_state->get_os_settings(os_state) &
710 OS_SETTINGS_DEFAULT_PWD_ENABLED;
711 eval = fail ? TNC_IMV_EVALUATION_RESULT_NONCOMPLIANT_MAJOR :
712 TNC_IMV_EVALUATION_RESULT_COMPLIANT;
713 snprintf(result_str, BUF_LEN, "factory default password%s enabled",
714 fail ? "" : " not");
715 break;
716 default:
717 continue;
718 }
719 if (eval != TNC_IMV_EVALUATION_RESULT_DONT_KNOW)
720 {
721 session->remove_workitem(session, enumerator);
722 rec = workitem->set_result(workitem, result_str, eval);
723 state->update_recommendation(state, rec, eval);
724 imcv_db->finalize_workitem(imcv_db, workitem);
725 workitem->destroy(workitem);
726 }
727 }
728 enumerator->destroy(enumerator);
729
730 /* finalized all workitems ? */
731 if (session->get_workitem_count(session, imv_id) == 0)
732 {
733 os_state->set_handshake_state(os_state, IMV_OS_STATE_END);
734
735 result = out_msg->send_assessment(out_msg);
736 out_msg->destroy(out_msg);
737 if (result != TNC_RESULT_SUCCESS)
738 {
739 return result;
740 }
741 return this->agent->provide_recommendation(this->agent, state);
742 }
743 }
744
745 /* send non-empty PA-TNC message with excl flag not set */
746 if (out_msg->get_attribute_count(out_msg))
747 {
748 result = out_msg->send(out_msg, FALSE);
749 }
750 out_msg->destroy(out_msg);
751
752 return result;
753 }
754
755 METHOD(imv_agent_if_t, solicit_recommendation, TNC_Result,
756 private_imv_os_agent_t *this, TNC_ConnectionID id)
757 {
758 imv_state_t *state;
759
760 if (!this->agent->get_state(this->agent, id, &state))
761 {
762 return TNC_RESULT_FATAL;
763 }
764 return this->agent->provide_recommendation(this->agent, state);
765 }
766
767 METHOD(imv_agent_if_t, destroy, void,
768 private_imv_os_agent_t *this)
769 {
770 DESTROY_IF(this->agent);
771 DESTROY_IF(this->db);
772 free(this);
773 }
774
775 /**
776 * Described in header.
777 */
778 imv_agent_if_t *imv_os_agent_create(const char *name, TNC_IMVID id,
779 TNC_Version *actual_version)
780 {
781 private_imv_os_agent_t *this;
782 imv_agent_t *agent;
783
784 agent = imv_agent_create(name, msg_types, countof(msg_types), id,
785 actual_version);
786 if (!agent)
787 {
788 return NULL;
789 }
790
791 INIT(this,
792 .public = {
793 .bind_functions = _bind_functions,
794 .notify_connection_change = _notify_connection_change,
795 .receive_message = _receive_message,
796 .receive_message_long = _receive_message_long,
797 .batch_ending = _batch_ending,
798 .solicit_recommendation = _solicit_recommendation,
799 .destroy = _destroy,
800 },
801 .agent = agent,
802 .db = imv_os_database_create(imcv_db),
803 );
804
805 return &this->public;
806 }
807