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