5eefb51ad98df7bc0c19b0f98a41afb332ddb525
[strongswan.git] / src / libimcv / plugins / imv_os / imv_os_agent.c
1 /*
2 * Copyright (C) 2013-2015 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_device_id.h>
41 #include "tcg/seg/tcg_seg_attr_max_size.h"
42 #include "tcg/seg/tcg_seg_attr_seg_env.h"
43
44 #include <tncif_names.h>
45 #include <tncif_pa_subtypes.h>
46
47 #include <pen/pen.h>
48 #include <utils/debug.h>
49
50 #define INSTALLED_PACKAGES_MAX_ATTR_SIZE 100000000
51
52 typedef struct private_imv_os_agent_t private_imv_os_agent_t;
53 typedef enum imv_os_attr_t imv_os_attr_t;
54
55 /* Subscribed PA-TNC message subtypes */
56 static pen_type_t msg_types[] = {
57 { PEN_IETF, PA_SUBTYPE_IETF_OPERATING_SYSTEM }
58 };
59
60 static char unknown_source_str[] = "install_non_market_apps";
61
62 /**
63 * Flag set when corresponding attribute has been received
64 */
65 enum imv_os_attr_t {
66 IMV_OS_ATTR_PRODUCT_INFORMATION = (1<<0),
67 IMV_OS_ATTR_STRING_VERSION = (1<<1),
68 IMV_OS_ATTR_NUMERIC_VERSION = (1<<2),
69 IMV_OS_ATTR_OPERATIONAL_STATUS = (1<<3),
70 IMV_OS_ATTR_FORWARDING_ENABLED = (1<<4),
71 IMV_OS_ATTR_FACTORY_DEFAULT_PWD_ENABLED = (1<<5),
72 IMV_OS_ATTR_DEVICE_ID = (1<<6),
73 IMV_OS_ATTR_MUST = (1<<7)-1,
74 IMV_OS_ATTR_INSTALLED_PACKAGES = (1<<7),
75 IMV_OS_ATTR_SETTINGS = (1<<8)
76 };
77
78 /**
79 * Private data of an imv_os_agent_t object.
80 */
81 struct private_imv_os_agent_t {
82
83 /**
84 * Public members of imv_os_agent_t
85 */
86 imv_agent_if_t public;
87
88 /**
89 * IMV agent responsible for generic functions
90 */
91 imv_agent_t *agent;
92
93 /**
94 * IMV OS database
95 */
96 imv_os_database_t *db;
97
98 };
99
100 METHOD(imv_agent_if_t, bind_functions, TNC_Result,
101 private_imv_os_agent_t *this, TNC_TNCS_BindFunctionPointer bind_function)
102 {
103 return this->agent->bind_functions(this->agent, bind_function);
104 }
105
106 METHOD(imv_agent_if_t, notify_connection_change, TNC_Result,
107 private_imv_os_agent_t *this, TNC_ConnectionID id,
108 TNC_ConnectionState new_state)
109 {
110 TNC_IMV_Action_Recommendation rec;
111 imv_state_t *state;
112 imv_session_t *session;
113
114 switch (new_state)
115 {
116 case TNC_CONNECTION_STATE_CREATE:
117 state = imv_os_state_create(id);
118 return this->agent->create_state(this->agent, state);
119 case TNC_CONNECTION_STATE_DELETE:
120 return this->agent->delete_state(this->agent, id);
121 case TNC_CONNECTION_STATE_ACCESS_ALLOWED:
122 case TNC_CONNECTION_STATE_ACCESS_ISOLATED:
123 case TNC_CONNECTION_STATE_ACCESS_NONE:
124 if (this->agent->get_state(this->agent, id, &state) && imcv_db)
125 {
126 session = state->get_session(state);
127
128 if (session->get_policy_started(session))
129 {
130 switch (new_state)
131 {
132 case TNC_CONNECTION_STATE_ACCESS_ALLOWED:
133 rec = TNC_IMV_ACTION_RECOMMENDATION_ALLOW;
134 break;
135 case TNC_CONNECTION_STATE_ACCESS_ISOLATED:
136 rec = TNC_IMV_ACTION_RECOMMENDATION_ISOLATE;
137 break;
138 case TNC_CONNECTION_STATE_ACCESS_NONE:
139 default:
140 rec = TNC_IMV_ACTION_RECOMMENDATION_NO_ACCESS;
141 }
142 imcv_db->add_recommendation(imcv_db, session, rec);
143 if (!imcv_db->policy_script(imcv_db, session, FALSE))
144 {
145 DBG1(DBG_IMV, "error in policy script stop");
146 }
147 }
148 }
149 /* fall through to default state */
150 default:
151 return this->agent->change_state(this->agent, id, new_state, NULL);
152 }
153 }
154
155 /**
156 * Process a received message
157 */
158 static TNC_Result receive_msg(private_imv_os_agent_t *this, imv_state_t *state,
159 imv_msg_t *in_msg)
160 {
161 imv_msg_t *out_msg;
162 imv_os_state_t *os_state;
163 imv_session_t *session;
164 imv_os_info_t *os_info = NULL;
165 enumerator_t *enumerator;
166 pa_tnc_attr_t *attr;
167 pen_type_t type;
168 TNC_Result result;
169 chunk_t os_name = chunk_empty;
170 chunk_t os_version = chunk_empty;
171 bool fatal_error = FALSE, assessment = FALSE;
172 uint16_t missing;
173
174 os_state = (imv_os_state_t*)state;
175 session = state->get_session(state);
176 os_info = session->get_os_info(session);
177
178 /* generate an outgoing PA-TNC message - we might need it */
179 out_msg = imv_msg_create_as_reply(in_msg);
180
181 /* parse received PA-TNC message and handle local and remote errors */
182 result = in_msg->receive(in_msg,out_msg, &fatal_error);
183 if (result != TNC_RESULT_SUCCESS)
184 {
185 out_msg->destroy(out_msg);
186 return result;
187 }
188
189 /* analyze PA-TNC attributes */
190 enumerator = in_msg->create_attribute_enumerator(in_msg);
191 while (enumerator->enumerate(enumerator, &attr))
192 {
193 type = attr->get_type(attr);
194
195 if (type.vendor_id == PEN_IETF)
196 {
197 switch (type.type)
198 {
199 case IETF_ATTR_PRODUCT_INFORMATION:
200 {
201 ietf_attr_product_info_t *attr_cast;
202 pen_t vendor_id;
203
204 state->set_action_flags(state,
205 IMV_OS_ATTR_PRODUCT_INFORMATION);
206 attr_cast = (ietf_attr_product_info_t*)attr;
207 os_name = attr_cast->get_info(attr_cast, &vendor_id, NULL);
208 os_info->set_name(os_info, os_name);
209
210 if (vendor_id != PEN_IETF)
211 {
212 DBG1(DBG_IMV, "operating system name is '%.*s' "
213 "from vendor %N", os_name.len, os_name.ptr,
214 pen_names, vendor_id);
215 }
216 else
217 {
218 DBG1(DBG_IMV, "operating system name is '%.*s'",
219 os_name.len, os_name.ptr);
220 }
221 break;
222 }
223 case IETF_ATTR_STRING_VERSION:
224 {
225 ietf_attr_string_version_t *attr_cast;
226
227 state->set_action_flags(state,
228 IMV_OS_ATTR_STRING_VERSION);
229 attr_cast = (ietf_attr_string_version_t*)attr;
230 os_version = attr_cast->get_version(attr_cast, NULL, NULL);
231 os_info->set_version(os_info, os_version);
232
233 if (os_version.len)
234 {
235 DBG1(DBG_IMV, "operating system version is '%.*s'",
236 os_version.len, os_version.ptr);
237 }
238 break;
239 }
240 case IETF_ATTR_NUMERIC_VERSION:
241 {
242 ietf_attr_numeric_version_t *attr_cast;
243 uint32_t major, minor;
244
245 state->set_action_flags(state,
246 IMV_OS_ATTR_NUMERIC_VERSION);
247 attr_cast = (ietf_attr_numeric_version_t*)attr;
248 attr_cast->get_version(attr_cast, &major, &minor);
249 DBG1(DBG_IMV, "operating system numeric version is %d.%d",
250 major, minor);
251 break;
252 }
253 case IETF_ATTR_OPERATIONAL_STATUS:
254 {
255 ietf_attr_op_status_t *attr_cast;
256 op_status_t op_status;
257 op_result_t op_result;
258 time_t last_boot;
259
260 state->set_action_flags(state,
261 IMV_OS_ATTR_OPERATIONAL_STATUS);
262 attr_cast = (ietf_attr_op_status_t*)attr;
263 op_status = attr_cast->get_status(attr_cast);
264 op_result = attr_cast->get_result(attr_cast);
265 last_boot = attr_cast->get_last_use(attr_cast);
266 DBG1(DBG_IMV, "operational status: %N, result: %N",
267 op_status_names, op_status, op_result_names, op_result);
268 DBG1(DBG_IMV, "last boot: %T", &last_boot, TRUE);
269 break;
270 }
271 case IETF_ATTR_FORWARDING_ENABLED:
272 {
273 ietf_attr_fwd_enabled_t *attr_cast;
274 os_fwd_status_t fwd_status;
275
276 state->set_action_flags(state,
277 IMV_OS_ATTR_FORWARDING_ENABLED);
278 attr_cast = (ietf_attr_fwd_enabled_t*)attr;
279 fwd_status = attr_cast->get_status(attr_cast);
280 DBG1(DBG_IMV, "IPv4 forwarding is %N",
281 os_fwd_status_names, fwd_status);
282 if (fwd_status == OS_FWD_ENABLED)
283 {
284 os_state->set_os_settings(os_state,
285 OS_SETTINGS_FWD_ENABLED);
286 }
287 break;
288 }
289 case IETF_ATTR_FACTORY_DEFAULT_PWD_ENABLED:
290 {
291 ietf_attr_default_pwd_enabled_t *attr_cast;
292 bool default_pwd_status;
293
294 state->set_action_flags(state,
295 IMV_OS_ATTR_FACTORY_DEFAULT_PWD_ENABLED);
296 attr_cast = (ietf_attr_default_pwd_enabled_t*)attr;
297 default_pwd_status = attr_cast->get_status(attr_cast);
298 DBG1(DBG_IMV, "factory default password is %sabled",
299 default_pwd_status ? "en":"dis");
300 if (default_pwd_status)
301 {
302 os_state->set_os_settings(os_state,
303 OS_SETTINGS_DEFAULT_PWD_ENABLED);
304 }
305 break;
306 }
307 case IETF_ATTR_INSTALLED_PACKAGES:
308 {
309 ietf_attr_installed_packages_t *attr_cast;
310 enumerator_t *e;
311 status_t status;
312
313 state->set_action_flags(state,
314 IMV_OS_ATTR_INSTALLED_PACKAGES);
315 if (!this->db)
316 {
317 break;
318 }
319 attr_cast = (ietf_attr_installed_packages_t*)attr;
320
321 e = attr_cast->create_enumerator(attr_cast);
322 status = this->db->check_packages(this->db, os_state, e);
323 e->destroy(e);
324
325 if (status == FAILED)
326 {
327 state->set_recommendation(state,
328 TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION,
329 TNC_IMV_EVALUATION_RESULT_ERROR);
330 assessment = TRUE;
331 }
332 missing = attr_cast->get_count(attr_cast);
333 os_state->set_missing(os_state, missing);
334 attr_cast->clear_packages(attr_cast);
335 break;
336 }
337 default:
338 break;
339 }
340 }
341 else if (type.vendor_id == PEN_ITA)
342 {
343 switch (type.type)
344 {
345 case ITA_ATTR_SETTINGS:
346 {
347 ita_attr_settings_t *attr_cast;
348 enumerator_t *e;
349 char *name;
350 chunk_t value;
351
352 state->set_action_flags(state, IMV_OS_ATTR_SETTINGS);
353
354 attr_cast = (ita_attr_settings_t*)attr;
355 e = attr_cast->create_enumerator(attr_cast);
356 while (e->enumerate(e, &name, &value))
357 {
358 if (streq(name, unknown_source_str) &&
359 chunk_equals(value, chunk_from_chars('1')))
360 {
361 os_state->set_os_settings(os_state,
362 OS_SETTINGS_UNKNOWN_SOURCE);
363 }
364 DBG1(DBG_IMV, "setting '%s'\n %.*s",
365 name, value.len, value.ptr);
366 }
367 e->destroy(e);
368 break;
369 }
370 case ITA_ATTR_DEVICE_ID:
371 {
372 chunk_t value;
373
374 state->set_action_flags(state, IMV_OS_ATTR_DEVICE_ID);
375
376 value = attr->get_value(attr);
377 DBG1(DBG_IMV, "device ID is %.*s", value.len, value.ptr);
378 session->set_device_id(session, value);
379 break;
380 }
381 default:
382 break;
383 }
384 }
385 }
386 enumerator->destroy(enumerator);
387
388 if (fatal_error)
389 {
390 state->set_recommendation(state,
391 TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION,
392 TNC_IMV_EVALUATION_RESULT_ERROR);
393 assessment = TRUE;
394 }
395
396 if (assessment)
397 {
398 os_state->set_handshake_state(os_state, IMV_OS_STATE_END);
399 result = out_msg->send_assessment(out_msg);
400 if (result == TNC_RESULT_SUCCESS)
401 {
402 result = this->agent->provide_recommendation(this->agent, state);
403 }
404 }
405 else
406 {
407 /* send PA-TNC message with the EXCL flag set */
408 result = out_msg->send(out_msg, TRUE);
409 }
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(uint32_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 uint32_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 size_t max_attr_size = INSTALLED_PACKAGES_MAX_ATTR_SIZE;
536 size_t max_seg_size;
537 seg_contract_t *contract;
538 seg_contract_manager_t *contracts;
539 char buf[BUF_LEN];
540
541 /* Determine maximum PA-TNC attribute segment size */
542 max_seg_size = state->get_max_msg_len(state)
543 - PA_TNC_HEADER_SIZE
544 - PA_TNC_ATTR_HEADER_SIZE
545 - TCG_SEG_ATTR_SEG_ENV_HEADER;
546
547 /* Announce support of PA-TNC segmentation to IMC */
548 contract = seg_contract_create(msg_types[0], max_attr_size,
549 max_seg_size, TRUE, imv_id, FALSE);
550 contract->get_info_string(contract, buf, BUF_LEN, TRUE);
551 DBG2(DBG_IMV, "%s", buf);
552 contracts = state->get_contracts(state);
553 contracts->add_contract(contracts, contract);
554 attr = tcg_seg_attr_max_size_create(max_attr_size, max_seg_size, TRUE);
555 out_msg->add_attribute(out_msg, attr);
556
557 if ((received & IMV_OS_ATTR_MUST) != IMV_OS_ATTR_MUST)
558 {
559 /* create attribute request for missing mandatory attributes */
560 out_msg->add_attribute(out_msg, build_attr_request(received));
561 }
562 }
563
564 if (handshake_state < IMV_OS_STATE_POLICY_START)
565 {
566 if (session->get_policy_started(session))
567 {
568 /* the policy script has already been started by another IMV */
569 handshake_state = IMV_OS_STATE_POLICY_START;
570 }
571 else
572 {
573 if (((received & IMV_OS_ATTR_PRODUCT_INFORMATION) &&
574 (received & IMV_OS_ATTR_STRING_VERSION)) &&
575 ((received & IMV_OS_ATTR_DEVICE_ID) ||
576 (handshake_state == IMV_OS_STATE_ATTR_REQ)))
577 {
578 if (!session->get_device_id(session, NULL))
579 {
580 session->set_device_id(session, chunk_empty);
581 }
582 if (imcv_db)
583 {
584 /* start the policy script */
585 if (!imcv_db->policy_script(imcv_db, session, TRUE))
586 {
587 DBG1(DBG_IMV, "error in policy script start");
588 }
589 }
590 else
591 {
592 DBG2(DBG_IMV, "no workitems available - "
593 "no evaluation possible");
594 state->set_recommendation(state,
595 TNC_IMV_ACTION_RECOMMENDATION_ALLOW,
596 TNC_IMV_EVALUATION_RESULT_DONT_KNOW);
597 session->set_policy_started(session, TRUE);
598 }
599 handshake_state = IMV_OS_STATE_POLICY_START;
600 }
601 else if (handshake_state == IMV_OS_STATE_ATTR_REQ)
602 {
603 /**
604 * both the IETF Product Information and IETF String Version
605 * attribute should have been present
606 */
607 state->set_recommendation(state,
608 TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION,
609 TNC_IMV_EVALUATION_RESULT_ERROR);
610
611 /* send assessment */
612 result = out_msg->send_assessment(out_msg);
613 out_msg->destroy(out_msg);
614
615 if (result != TNC_RESULT_SUCCESS)
616 {
617 return result;
618 }
619 return this->agent->provide_recommendation(this->agent, state);
620 }
621 else
622 {
623 handshake_state = IMV_OS_STATE_ATTR_REQ;
624 }
625 }
626 os_state->set_handshake_state(os_state, handshake_state);
627 }
628
629 if (handshake_state == IMV_OS_STATE_POLICY_START)
630 {
631 enumerator = session->create_workitem_enumerator(session);
632 if (enumerator)
633 {
634 while (enumerator->enumerate(enumerator, &workitem))
635 {
636 if (workitem->get_imv_id(workitem) != TNC_IMVID_ANY)
637 {
638 continue;
639 }
640
641 switch (workitem->get_type(workitem))
642 {
643 case IMV_WORKITEM_PACKAGES:
644 attr = ietf_attr_attr_request_create(PEN_IETF,
645 IETF_ATTR_INSTALLED_PACKAGES);
646 out_msg->add_attribute(out_msg, attr);
647 break;
648 case IMV_WORKITEM_UNKNOWN_SOURCE:
649 attr = ita_attr_get_settings_create(unknown_source_str);
650 out_msg->add_attribute(out_msg, attr);
651 break;
652 case IMV_WORKITEM_FORWARDING:
653 case IMV_WORKITEM_DEFAULT_PWD:
654 break;
655 default:
656 continue;
657 }
658 workitem->set_imv_id(workitem, imv_id);
659 no_workitems = FALSE;
660 }
661 enumerator->destroy(enumerator);
662
663 if (no_workitems)
664 {
665 DBG2(DBG_IMV, "IMV %d has no workitems - "
666 "no evaluation requested", imv_id);
667 state->set_recommendation(state,
668 TNC_IMV_ACTION_RECOMMENDATION_ALLOW,
669 TNC_IMV_EVALUATION_RESULT_DONT_KNOW);
670 }
671 handshake_state = IMV_OS_STATE_WORKITEMS;
672 os_state->set_handshake_state(os_state, handshake_state);
673 }
674 }
675
676 if (handshake_state == IMV_OS_STATE_WORKITEMS)
677 {
678 TNC_IMV_Evaluation_Result eval;
679 TNC_IMV_Action_Recommendation rec;
680 char result_str[BUF_LEN];
681 bool fail;
682
683 enumerator = session->create_workitem_enumerator(session);
684 while (enumerator->enumerate(enumerator, &workitem))
685 {
686 if (workitem->get_imv_id(workitem) != imv_id)
687 {
688 continue;
689 }
690 eval = TNC_IMV_EVALUATION_RESULT_DONT_KNOW;
691
692 switch (workitem->get_type(workitem))
693 {
694 case IMV_WORKITEM_PACKAGES:
695 {
696 int count, count_update, count_blacklist, count_ok;
697
698 if (!(received & IMV_OS_ATTR_INSTALLED_PACKAGES) ||
699 os_state->get_missing(os_state) > 0)
700 {
701 continue;
702 }
703 os_state->get_count(os_state, &count, &count_update,
704 &count_blacklist, &count_ok);
705 fail = count_update || count_blacklist;
706 eval = fail ? TNC_IMV_EVALUATION_RESULT_NONCOMPLIANT_MINOR :
707 TNC_IMV_EVALUATION_RESULT_COMPLIANT;
708 snprintf(result_str, BUF_LEN, "processed %d packages: "
709 "%d not updated, %d blacklisted, %d ok, "
710 "%d unknown",
711 count, count_update, count_blacklist, count_ok,
712 count - count_update - count_blacklist - count_ok);
713 break;
714 }
715 case IMV_WORKITEM_UNKNOWN_SOURCE:
716 if (!(received & IMV_OS_ATTR_SETTINGS))
717 {
718 continue;
719 }
720 fail = os_state->get_os_settings(os_state) &
721 OS_SETTINGS_UNKNOWN_SOURCE;
722 eval = fail ? TNC_IMV_EVALUATION_RESULT_NONCOMPLIANT_MINOR :
723 TNC_IMV_EVALUATION_RESULT_COMPLIANT;
724 snprintf(result_str, BUF_LEN, "unknown sources%s enabled",
725 fail ? "" : " not");
726 break;
727 case IMV_WORKITEM_FORWARDING:
728 if (!(received & IMV_OS_ATTR_FORWARDING_ENABLED))
729 {
730 continue;
731 }
732 fail = os_state->get_os_settings(os_state) &
733 OS_SETTINGS_FWD_ENABLED;
734 eval = fail ? TNC_IMV_EVALUATION_RESULT_NONCOMPLIANT_MAJOR :
735 TNC_IMV_EVALUATION_RESULT_COMPLIANT;
736 snprintf(result_str, BUF_LEN, "forwarding%s enabled",
737 fail ? "" : " not");
738 break;
739 case IMV_WORKITEM_DEFAULT_PWD:
740 if (!(received & IMV_OS_ATTR_FACTORY_DEFAULT_PWD_ENABLED))
741 {
742 continue;
743 }
744 fail = os_state->get_os_settings(os_state) &
745 OS_SETTINGS_DEFAULT_PWD_ENABLED;
746 eval = fail ? TNC_IMV_EVALUATION_RESULT_NONCOMPLIANT_MAJOR :
747 TNC_IMV_EVALUATION_RESULT_COMPLIANT;
748 snprintf(result_str, BUF_LEN, "factory default password%s enabled",
749 fail ? "" : " not");
750 break;
751 default:
752 continue;
753 }
754 if (eval != TNC_IMV_EVALUATION_RESULT_DONT_KNOW)
755 {
756 session->remove_workitem(session, enumerator);
757 rec = workitem->set_result(workitem, result_str, eval);
758 state->update_recommendation(state, rec, eval);
759 imcv_db->finalize_workitem(imcv_db, workitem);
760 workitem->destroy(workitem);
761 }
762 }
763 enumerator->destroy(enumerator);
764
765 /* finalized all workitems ? */
766 if (session->get_workitem_count(session, imv_id) == 0)
767 {
768 os_state->set_handshake_state(os_state, IMV_OS_STATE_END);
769
770 result = out_msg->send_assessment(out_msg);
771 out_msg->destroy(out_msg);
772 if (result != TNC_RESULT_SUCCESS)
773 {
774 return result;
775 }
776 return this->agent->provide_recommendation(this->agent, state);
777 }
778 }
779
780 /* send non-empty PA-TNC message with excl flag not set */
781 if (out_msg->get_attribute_count(out_msg))
782 {
783 result = out_msg->send(out_msg, FALSE);
784 }
785 out_msg->destroy(out_msg);
786
787 return result;
788 }
789
790 METHOD(imv_agent_if_t, solicit_recommendation, TNC_Result,
791 private_imv_os_agent_t *this, TNC_ConnectionID id)
792 {
793 imv_state_t *state;
794
795 if (!this->agent->get_state(this->agent, id, &state))
796 {
797 return TNC_RESULT_FATAL;
798 }
799 return this->agent->provide_recommendation(this->agent, state);
800 }
801
802 METHOD(imv_agent_if_t, destroy, void,
803 private_imv_os_agent_t *this)
804 {
805 DESTROY_IF(this->agent);
806 DESTROY_IF(this->db);
807 free(this);
808 }
809
810 /**
811 * Described in header.
812 */
813 imv_agent_if_t *imv_os_agent_create(const char *name, TNC_IMVID id,
814 TNC_Version *actual_version)
815 {
816 private_imv_os_agent_t *this;
817 imv_agent_t *agent;
818
819 agent = imv_agent_create(name, msg_types, countof(msg_types), id,
820 actual_version);
821 if (!agent)
822 {
823 return NULL;
824 }
825
826 INIT(this,
827 .public = {
828 .bind_functions = _bind_functions,
829 .notify_connection_change = _notify_connection_change,
830 .receive_message = _receive_message,
831 .receive_message_long = _receive_message_long,
832 .batch_ending = _batch_ending,
833 .solicit_recommendation = _solicit_recommendation,
834 .destroy = _destroy,
835 },
836 .agent = agent,
837 .db = imv_os_database_create(imcv_db),
838 );
839
840 return &this->public;
841 }
842