Store device with product ID
[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 result = out_msg->send_assessment(out_msg);
400 out_msg->destroy(out_msg);
401 if (result != TNC_RESULT_SUCCESS)
402 {
403 return result;
404 }
405 return this->agent->provide_recommendation(this->agent, state);
406 }
407
408 /* send PA-TNC message with excl flag set */
409 result = out_msg->send(out_msg, TRUE);
410 out_msg->destroy(out_msg);
411
412 return result;
413 }
414
415 METHOD(imv_agent_if_t, receive_message, TNC_Result,
416 private_imv_os_agent_t *this, TNC_ConnectionID id,
417 TNC_MessageType msg_type, chunk_t msg)
418 {
419 imv_state_t *state;
420 imv_msg_t *in_msg;
421 TNC_Result result;
422
423 if (!this->agent->get_state(this->agent, id, &state))
424 {
425 return TNC_RESULT_FATAL;
426 }
427 in_msg = imv_msg_create_from_data(this->agent, state, id, msg_type, msg);
428 result = receive_msg(this, state, in_msg);
429 in_msg->destroy(in_msg);
430
431 return result;
432 }
433
434 METHOD(imv_agent_if_t, receive_message_long, TNC_Result,
435 private_imv_os_agent_t *this, TNC_ConnectionID id,
436 TNC_UInt32 src_imc_id, TNC_UInt32 dst_imv_id,
437 TNC_VendorID msg_vid, TNC_MessageSubtype msg_subtype, chunk_t msg)
438 {
439 imv_state_t *state;
440 imv_msg_t *in_msg;
441 TNC_Result result;
442
443 if (!this->agent->get_state(this->agent, id, &state))
444 {
445 return TNC_RESULT_FATAL;
446 }
447 in_msg = imv_msg_create_from_long_data(this->agent, state, id,
448 src_imc_id, dst_imv_id, msg_vid, msg_subtype, msg);
449 result = receive_msg(this, state, in_msg);
450 in_msg->destroy(in_msg);
451
452 return result;
453
454 }
455
456 /**
457 * Build an IETF Attribute Request attribute for missing attributes
458 */
459 static pa_tnc_attr_t* build_attr_request(u_int32_t received)
460 {
461 pa_tnc_attr_t *attr;
462 ietf_attr_attr_request_t *attr_cast;
463
464 attr = ietf_attr_attr_request_create(PEN_RESERVED, 0);
465 attr_cast = (ietf_attr_attr_request_t*)attr;
466
467 if (!(received & IMV_OS_ATTR_PRODUCT_INFORMATION) ||
468 !(received & IMV_OS_ATTR_STRING_VERSION))
469 {
470 attr_cast->add(attr_cast, PEN_IETF, IETF_ATTR_PRODUCT_INFORMATION);
471 attr_cast->add(attr_cast, PEN_IETF, IETF_ATTR_STRING_VERSION);
472 }
473 if (!(received & IMV_OS_ATTR_NUMERIC_VERSION))
474 {
475 attr_cast->add(attr_cast, PEN_IETF, IETF_ATTR_NUMERIC_VERSION);
476 }
477 if (!(received & IMV_OS_ATTR_OPERATIONAL_STATUS))
478 {
479 attr_cast->add(attr_cast, PEN_IETF, IETF_ATTR_OPERATIONAL_STATUS);
480 }
481 if (!(received & IMV_OS_ATTR_FORWARDING_ENABLED))
482 {
483 attr_cast->add(attr_cast, PEN_IETF, IETF_ATTR_FORWARDING_ENABLED);
484 }
485 if (!(received & IMV_OS_ATTR_FACTORY_DEFAULT_PWD_ENABLED))
486 {
487 attr_cast->add(attr_cast, PEN_IETF,
488 IETF_ATTR_FACTORY_DEFAULT_PWD_ENABLED);
489 }
490 if (!(received & IMV_OS_ATTR_DEVICE_ID))
491 {
492 attr_cast->add(attr_cast, PEN_ITA, ITA_ATTR_DEVICE_ID);
493 }
494
495 return attr;
496 }
497
498 METHOD(imv_agent_if_t, batch_ending, TNC_Result,
499 private_imv_os_agent_t *this, TNC_ConnectionID id)
500 {
501 imv_msg_t *out_msg;
502 imv_state_t *state;
503 imv_session_t *session;
504 imv_workitem_t *workitem;
505 imv_os_state_t *os_state;
506 imv_os_handshake_state_t handshake_state;
507 pa_tnc_attr_t *attr;
508 TNC_IMVID imv_id;
509 TNC_Result result = TNC_RESULT_SUCCESS;
510 bool no_workitems = TRUE;
511 enumerator_t *enumerator;
512 u_int32_t received;
513
514 if (!this->agent->get_state(this->agent, id, &state))
515 {
516 return TNC_RESULT_FATAL;
517 }
518 os_state = (imv_os_state_t*)state;
519 handshake_state = os_state->get_handshake_state(os_state);
520 received = state->get_action_flags(state);
521 session = state->get_session(state);
522 imv_id = this->agent->get_id(this->agent);
523
524 if (handshake_state == IMV_OS_STATE_END)
525 {
526 return TNC_RESULT_SUCCESS;
527 }
528
529 /* create an empty out message - we might need it */
530 out_msg = imv_msg_create(this->agent, state, id, imv_id, TNC_IMCID_ANY,
531 msg_types[0]);
532
533 if (handshake_state == IMV_OS_STATE_INIT)
534 {
535 if ((received & IMV_OS_ATTR_MUST) != IMV_OS_ATTR_MUST)
536 {
537 /* create attribute request for missing mandatory attributes */
538 out_msg->add_attribute(out_msg, build_attr_request(received));
539 }
540 }
541
542 if (handshake_state < IMV_OS_STATE_POLICY_START)
543 {
544 if (((received & IMV_OS_ATTR_PRODUCT_INFORMATION) &&
545 (received & IMV_OS_ATTR_STRING_VERSION)) &&
546 ((received & IMV_OS_ATTR_DEVICE_ID) ||
547 (handshake_state == IMV_OS_STATE_ATTR_REQ)))
548 {
549 if (imcv_db)
550 {
551 imcv_db->add_device(imcv_db, session,
552 os_state->get_device_id(os_state));
553
554 /* trigger the policy manager */
555 imcv_db->policy_script(imcv_db, session, TRUE);
556 }
557 else
558 {
559 DBG2(DBG_IMV, "no workitems available - no evaluation possible");
560 state->set_recommendation(state,
561 TNC_IMV_ACTION_RECOMMENDATION_ALLOW,
562 TNC_IMV_EVALUATION_RESULT_DONT_KNOW);
563 }
564 handshake_state = IMV_OS_STATE_POLICY_START;
565 }
566 else if (handshake_state == IMV_OS_STATE_ATTR_REQ)
567 {
568 /**
569 * both the IETF Product Information and IETF String Version
570 * attribute should have been present
571 */
572 state->set_recommendation(state,
573 TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION,
574 TNC_IMV_EVALUATION_RESULT_ERROR);
575
576 /* send assessment */
577 result = out_msg->send_assessment(out_msg);
578 out_msg->destroy(out_msg);
579
580 if (result != TNC_RESULT_SUCCESS)
581 {
582 return result;
583 }
584 return this->agent->provide_recommendation(this->agent, state);
585 }
586 else
587 {
588 handshake_state = IMV_OS_STATE_ATTR_REQ;
589 }
590 os_state->set_handshake_state(os_state, handshake_state);
591 }
592
593 if (handshake_state == IMV_OS_STATE_POLICY_START && session)
594 {
595 enumerator = session->create_workitem_enumerator(session);
596 if (enumerator)
597 {
598 while (enumerator->enumerate(enumerator, &workitem))
599 {
600 if (workitem->get_imv_id(workitem) != TNC_IMVID_ANY)
601 {
602 continue;
603 }
604 no_workitems = FALSE;
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 }
625 enumerator->destroy(enumerator);
626
627 if (no_workitems)
628 {
629 DBG2(DBG_IMV, "IMV %d has no workitems - "
630 "no evaluation requested", imv_id);
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 os_state->set_handshake_state(os_state, IMV_OS_STATE_END);
731
732 result = out_msg->send_assessment(out_msg);
733 out_msg->destroy(out_msg);
734 if (result != TNC_RESULT_SUCCESS)
735 {
736 return result;
737 }
738 return this->agent->provide_recommendation(this->agent, state);
739 }
740 }
741
742 /* send non-empty PA-TNC message with excl flag not set */
743 if (out_msg->get_attribute_count(out_msg))
744 {
745 result = out_msg->send(out_msg, FALSE);
746 }
747 out_msg->destroy(out_msg);
748
749 return result;
750 }
751
752 METHOD(imv_agent_if_t, solicit_recommendation, TNC_Result,
753 private_imv_os_agent_t *this, TNC_ConnectionID id)
754 {
755 imv_state_t *state;
756
757 if (!this->agent->get_state(this->agent, id, &state))
758 {
759 return TNC_RESULT_FATAL;
760 }
761 return this->agent->provide_recommendation(this->agent, state);
762 }
763
764 METHOD(imv_agent_if_t, destroy, void,
765 private_imv_os_agent_t *this)
766 {
767 DESTROY_IF(this->agent);
768 DESTROY_IF(this->db);
769 free(this);
770 }
771
772 /**
773 * Described in header.
774 */
775 imv_agent_if_t *imv_os_agent_create(const char *name, TNC_IMVID id,
776 TNC_Version *actual_version)
777 {
778 private_imv_os_agent_t *this;
779
780 INIT(this,
781 .public = {
782 .bind_functions = _bind_functions,
783 .notify_connection_change = _notify_connection_change,
784 .receive_message = _receive_message,
785 .receive_message_long = _receive_message_long,
786 .batch_ending = _batch_ending,
787 .solicit_recommendation = _solicit_recommendation,
788 .destroy = _destroy,
789 },
790 .agent = imv_agent_create(name, msg_types, countof(msg_types), id,
791 actual_version),
792 .db = imv_os_database_create(imcv_db),
793 );
794
795 if (!this->agent)
796 {
797 destroy(this);
798 return NULL;
799 }
800 return &this->public;
801 }
802