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