Moved all functionality into imv_os_agent_t class turning imv_os_t into an IF-IMV...
[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_os_agent_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_os_agent_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_os_agent_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 os_state->set_received(os_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 os_state->set_received(os_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 os_state->set_received(os_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 os_state->set_received(os_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 os_state->set_received(os_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 os_state->set_received(os_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 os_state->set_received(os_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 os_state->set_received(os_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 imv_session_t *session;
349 int device_id;
350 chunk_t value;
351
352 os_state->set_received(os_state, IMV_OS_ATTR_DEVICE_ID);
353
354 value = attr->get_value(attr);
355 DBG1(DBG_IMV, "device ID is %.*s", value.len, value.ptr);
356
357 if (imcv_db)
358 {
359 session = state->get_session(state);
360 device_id = imcv_db->add_device(imcv_db, session, value);
361 os_state->set_device_id(os_state, device_id);
362 }
363 break;
364 }
365 case ITA_ATTR_START_ANGEL:
366 os_state->set_angel_count(os_state, TRUE);
367 break;
368 case ITA_ATTR_STOP_ANGEL:
369 os_state->set_angel_count(os_state, FALSE);
370 break;
371 default:
372 break;
373 }
374 }
375 }
376 enumerator->destroy(enumerator);
377
378 /**
379 * The IETF Product Information and String Version attributes
380 * are supposed to arrive in the same PA-TNC message
381 */
382 if (os_name.len && os_version.len)
383 {
384 os_type_t os_type;
385
386 /* set the OS type, name and version */
387 os_type = os_type_from_name(os_name);
388 os_state->set_info(os_state,os_type, os_name, os_version);
389
390 if (imcv_db)
391 {
392 imcv_db->add_product(imcv_db, state->get_session(state),
393 os_state->get_info(os_state, NULL, NULL, NULL));
394 }
395 }
396
397 if (fatal_error)
398 {
399 state->set_recommendation(state,
400 TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION,
401 TNC_IMV_EVALUATION_RESULT_ERROR);
402 assessment = TRUE;
403 }
404
405 if (assessment)
406 {
407 result = out_msg->send_assessment(out_msg);
408 out_msg->destroy(out_msg);
409 if (result != TNC_RESULT_SUCCESS)
410 {
411 return result;
412 }
413 return this->agent->provide_recommendation(this->agent, state);
414 }
415
416 /* send PA-TNC message with excl flag set */
417 result = out_msg->send(out_msg, TRUE);
418 out_msg->destroy(out_msg);
419
420 return result;
421 }
422
423 METHOD(imv_os_agent_t, receive_message, TNC_Result,
424 private_imv_os_agent_t *this, TNC_ConnectionID id,
425 TNC_MessageType msg_type, chunk_t msg)
426 {
427 imv_state_t *state;
428 imv_msg_t *in_msg;
429 TNC_Result result;
430
431 if (!this->agent->get_state(this->agent, id, &state))
432 {
433 return TNC_RESULT_FATAL;
434 }
435 in_msg = imv_msg_create_from_data(this->agent, state, id, msg_type, msg);
436 result = receive_msg(this, state, in_msg);
437 in_msg->destroy(in_msg);
438
439 return result;
440 }
441
442 METHOD(imv_os_agent_t, receive_message_long, TNC_Result,
443 private_imv_os_agent_t *this, TNC_ConnectionID id,
444 TNC_UInt32 src_imc_id, TNC_UInt32 dst_imv_id,
445 TNC_VendorID msg_vid, TNC_MessageSubtype msg_subtype, chunk_t msg)
446 {
447 imv_state_t *state;
448 imv_msg_t *in_msg;
449 TNC_Result result;
450
451 if (!this->agent->get_state(this->agent, id, &state))
452 {
453 return TNC_RESULT_FATAL;
454 }
455 in_msg = imv_msg_create_from_long_data(this->agent, state, id,
456 src_imc_id, dst_imv_id, msg_vid, msg_subtype, msg);
457 result = receive_msg(this, state, in_msg);
458 in_msg->destroy(in_msg);
459
460 return result;
461
462 }
463
464 /**
465 * Build an IETF Attribute Request attribute for missing attributes
466 */
467 static pa_tnc_attr_t* build_attr_request(u_int received)
468 {
469 pa_tnc_attr_t *attr;
470 ietf_attr_attr_request_t *attr_cast;
471
472 attr = ietf_attr_attr_request_create(PEN_RESERVED, 0);
473 attr_cast = (ietf_attr_attr_request_t*)attr;
474
475 if (!(received & IMV_OS_ATTR_PRODUCT_INFORMATION) ||
476 !(received & IMV_OS_ATTR_STRING_VERSION))
477 {
478 attr_cast->add(attr_cast, PEN_IETF, IETF_ATTR_PRODUCT_INFORMATION);
479 attr_cast->add(attr_cast, PEN_IETF, IETF_ATTR_STRING_VERSION);
480 }
481 if (!(received & IMV_OS_ATTR_NUMERIC_VERSION))
482 {
483 attr_cast->add(attr_cast, PEN_IETF, IETF_ATTR_NUMERIC_VERSION);
484 }
485 if (!(received & IMV_OS_ATTR_OPERATIONAL_STATUS))
486 {
487 attr_cast->add(attr_cast, PEN_IETF, IETF_ATTR_OPERATIONAL_STATUS);
488 }
489 if (!(received & IMV_OS_ATTR_FORWARDING_ENABLED))
490 {
491 attr_cast->add(attr_cast, PEN_IETF, IETF_ATTR_FORWARDING_ENABLED);
492 }
493 if (!(received & IMV_OS_ATTR_FACTORY_DEFAULT_PWD_ENABLED))
494 {
495 attr_cast->add(attr_cast, PEN_IETF,
496 IETF_ATTR_FACTORY_DEFAULT_PWD_ENABLED);
497 }
498 if (!(received & IMV_OS_ATTR_DEVICE_ID))
499 {
500 attr_cast->add(attr_cast, PEN_ITA, ITA_ATTR_DEVICE_ID);
501 }
502
503 return attr;
504 }
505
506 METHOD(imv_os_agent_t, batch_ending, TNC_Result,
507 private_imv_os_agent_t *this, TNC_ConnectionID id)
508 {
509 imv_msg_t *out_msg;
510 imv_state_t *state;
511 imv_session_t *session;
512 imv_workitem_t *workitem;
513 imv_os_state_t *os_state;
514 imv_os_handshake_state_t handshake_state;
515 pa_tnc_attr_t *attr;
516 TNC_IMVID imv_id;
517 TNC_Result result = TNC_RESULT_SUCCESS;
518 bool no_workitems = TRUE;
519 enumerator_t *enumerator;
520 u_int received;
521
522 if (!this->agent->get_state(this->agent, id, &state))
523 {
524 return TNC_RESULT_FATAL;
525 }
526 os_state = (imv_os_state_t*)state;
527 handshake_state = os_state->get_handshake_state(os_state);
528 received = os_state->get_received(os_state);
529 session = state->get_session(state);
530 imv_id = this->agent->get_id(this->agent);
531
532 /* create an empty out message - we might need it */
533 out_msg = imv_msg_create(this->agent, state, id,
534 this->agent->get_id(this->agent),
535 TNC_IMCID_ANY, msg_types[0]);
536
537 if (handshake_state == IMV_OS_STATE_INIT)
538 {
539 if ((received & IMV_OS_ATTR_MUST) != IMV_OS_ATTR_MUST)
540 {
541 /* create attribute request for missing mandatory attributes */
542 out_msg->add_attribute(out_msg, build_attr_request(received));
543 }
544 }
545
546 if (handshake_state < IMV_OS_STATE_POLICY_START)
547 {
548 if (((received & IMV_OS_ATTR_PRODUCT_INFORMATION) &&
549 (received & IMV_OS_ATTR_STRING_VERSION)) &&
550 ((received & IMV_OS_ATTR_DEVICE_ID) ||
551 (handshake_state == IMV_OS_STATE_ATTR_REQ)))
552 {
553 if (imcv_db)
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 no_workitems = FALSE;
606
607 switch (workitem->get_type(workitem))
608 {
609 case IMV_WORKITEM_PACKAGES:
610 attr = ietf_attr_attr_request_create(PEN_IETF,
611 IETF_ATTR_INSTALLED_PACKAGES);
612 out_msg->add_attribute(out_msg, attr);
613 break;
614 case IMV_WORKITEM_UNKNOWN_SOURCE:
615 attr = ita_attr_get_settings_create(unknown_source_str);
616 out_msg->add_attribute(out_msg, attr);
617 break;
618 case IMV_WORKITEM_FORWARDING:
619 case IMV_WORKITEM_DEFAULT_PWD:
620 break;
621 default:
622 continue;
623 }
624 workitem->set_imv_id(workitem, imv_id);
625 }
626 enumerator->destroy(enumerator);
627
628 if (no_workitems)
629 {
630 DBG2(DBG_IMV, "no workitems generated - no evaluation requested");
631 state->set_recommendation(state,
632 TNC_IMV_ACTION_RECOMMENDATION_ALLOW,
633 TNC_IMV_EVALUATION_RESULT_DONT_KNOW);
634 }
635 handshake_state = IMV_OS_STATE_WORKITEMS;
636 os_state->set_handshake_state(os_state, handshake_state);
637 }
638 }
639
640 if (handshake_state == IMV_OS_STATE_WORKITEMS && session)
641 {
642 TNC_IMV_Evaluation_Result eval;
643 TNC_IMV_Action_Recommendation rec;
644 char buf[BUF_LEN], *result_str;
645 bool fail;
646
647 enumerator = session->create_workitem_enumerator(session);
648 while (enumerator->enumerate(enumerator, &workitem))
649 {
650 if (workitem->get_imv_id(workitem) != imv_id)
651 {
652 continue;
653 }
654 eval = TNC_IMV_EVALUATION_RESULT_DONT_KNOW;
655
656 switch (workitem->get_type(workitem))
657 {
658 case IMV_WORKITEM_PACKAGES:
659 {
660 int count, count_update, count_blacklist, count_ok;
661
662 if (!(received & IMV_OS_ATTR_INSTALLED_PACKAGES) ||
663 os_state->get_angel_count(os_state))
664 {
665 continue;
666 }
667 os_state->get_count(os_state, &count, &count_update,
668 &count_blacklist, &count_ok);
669 fail = count_update || count_blacklist;
670 eval = fail ? TNC_IMV_EVALUATION_RESULT_NONCOMPLIANT_MINOR :
671 TNC_IMV_EVALUATION_RESULT_COMPLIANT;
672 snprintf(buf, BUF_LEN, "processed %d packages: "
673 "%d not updated, %d blacklisted, %d ok, "
674 "%d not found",
675 count, count_update, count_blacklist, count_ok,
676 count - count_update - count_blacklist - count_ok);
677 result_str = buf;
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 result_str = fail ? "unknown sources enabled" : "";
690 break;
691 case IMV_WORKITEM_FORWARDING:
692 if (!(received & IMV_OS_ATTR_FORWARDING_ENABLED))
693 {
694 continue;
695 }
696 fail = os_state->get_os_settings(os_state) &
697 OS_SETTINGS_FWD_ENABLED;
698 eval = fail ? TNC_IMV_EVALUATION_RESULT_NONCOMPLIANT_MAJOR :
699 TNC_IMV_EVALUATION_RESULT_COMPLIANT;
700 result_str = fail ? "forwarding enabled" : "";
701 break;
702 case IMV_WORKITEM_DEFAULT_PWD:
703 if (!(received & IMV_OS_ATTR_FACTORY_DEFAULT_PWD_ENABLED))
704 {
705 continue;
706 }
707 fail = os_state->get_os_settings(os_state) &
708 OS_SETTINGS_DEFAULT_PWD_ENABLED;
709 eval = fail ? TNC_IMV_EVALUATION_RESULT_NONCOMPLIANT_MAJOR :
710 TNC_IMV_EVALUATION_RESULT_COMPLIANT;
711 result_str = fail ? "default password enabled" : "";
712 break;
713 default:
714 continue;
715 }
716 if (eval != TNC_IMV_EVALUATION_RESULT_DONT_KNOW)
717 {
718 session->remove_workitem(session, enumerator);
719 rec = workitem->set_result(workitem, result_str, eval);
720 state->update_recommendation(state, rec, eval);
721 imcv_db->finalize_workitem(imcv_db, workitem);
722 workitem->destroy(workitem);
723 }
724 }
725 enumerator->destroy(enumerator);
726
727 /* finalized all workitems ? */
728 if (session->get_workitem_count(session, imv_id) == 0)
729 {
730 result = out_msg->send_assessment(out_msg);
731 out_msg->destroy(out_msg);
732 if (result != TNC_RESULT_SUCCESS)
733 {
734 return result;
735 }
736 return this->agent->provide_recommendation(this->agent, state);
737 }
738 }
739
740 /* send non-empty PA-TNC message with excl flag not set */
741 if (out_msg->get_attribute_count(out_msg))
742 {
743 result = out_msg->send(out_msg, FALSE);
744 }
745 out_msg->destroy(out_msg);
746
747 return result;
748 }
749
750 METHOD(imv_os_agent_t, solicit_recommendation, TNC_Result,
751 private_imv_os_agent_t *this, TNC_ConnectionID id)
752 {
753 imv_state_t *state;
754
755 if (!this->agent->get_state(this->agent, id, &state))
756 {
757 return TNC_RESULT_FATAL;
758 }
759 return this->agent->provide_recommendation(this->agent, state);
760 }
761
762 METHOD(imv_os_agent_t, destroy, void,
763 private_imv_os_agent_t *this)
764 {
765 DESTROY_IF(this->agent);
766 DESTROY_IF(this->db);
767 free(this);
768 }
769
770 /**
771 * Described in header.
772 */
773 imv_os_agent_t *imv_os_agent_create(const char *name, TNC_IMVID id,
774 TNC_Version *actual_version)
775 {
776 private_imv_os_agent_t *this;
777
778 INIT(this,
779 .public = {
780 .bind_functions = _bind_functions,
781 .notify_connection_change = _notify_connection_change,
782 .receive_message = _receive_message,
783 .receive_message_long = _receive_message_long,
784 .batch_ending = _batch_ending,
785 .solicit_recommendation = _solicit_recommendation,
786 .destroy = _destroy,
787 },
788 .agent = imv_agent_create(name, msg_types, countof(msg_types), id,
789 actual_version),
790 .db = imv_os_database_create(imcv_db),
791 );
792
793 if (!this->agent)
794 {
795 destroy(this);
796 return NULL;
797 }
798 return &this->public;
799 }
800