OS IMV proposes IF-M segmentation contract
[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_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 - PA_TNC_ATTR_HEADER_SIZE
547 - TCG_SEG_ATTR_MAX_SIZE_SIZE;
548
549 /* Announce support of PA-TNC segmentation to IMC */
550 contract = seg_contract_create(msg_types[0], max_attr_size,
551 max_seg_size, TRUE, imv_id, FALSE);
552 contract->get_info_string(contract, buf, BUF_LEN, TRUE);
553 DBG2(DBG_IMV, "%s", buf);
554 contracts = state->get_contracts(state);
555 contracts->add_contract(contracts, contract);
556 attr = tcg_seg_attr_max_size_create(max_attr_size, max_seg_size, TRUE);
557 out_msg->add_attribute(out_msg, attr);
558
559 if ((received & IMV_OS_ATTR_MUST) != IMV_OS_ATTR_MUST)
560 {
561 /* create attribute request for missing mandatory attributes */
562 out_msg->add_attribute(out_msg, build_attr_request(received));
563 }
564 }
565
566 if (handshake_state < IMV_OS_STATE_POLICY_START)
567 {
568 if (session->get_policy_started(session))
569 {
570 /* the policy script has already been started by another IMV */
571 handshake_state = IMV_OS_STATE_POLICY_START;
572 }
573 else
574 {
575 if (((received & IMV_OS_ATTR_PRODUCT_INFORMATION) &&
576 (received & IMV_OS_ATTR_STRING_VERSION)) &&
577 ((received & IMV_OS_ATTR_DEVICE_ID) ||
578 (handshake_state == IMV_OS_STATE_ATTR_REQ)))
579 {
580 if (!session->get_device_id(session, NULL))
581 {
582 session->set_device_id(session, chunk_empty);
583 }
584 if (imcv_db)
585 {
586 /* start the policy script */
587 if (!imcv_db->policy_script(imcv_db, session, TRUE))
588 {
589 DBG1(DBG_IMV, "error in policy script start");
590 }
591 }
592 else
593 {
594 DBG2(DBG_IMV, "no workitems available - "
595 "no evaluation possible");
596 state->set_recommendation(state,
597 TNC_IMV_ACTION_RECOMMENDATION_ALLOW,
598 TNC_IMV_EVALUATION_RESULT_DONT_KNOW);
599 session->set_policy_started(session, TRUE);
600 }
601 handshake_state = IMV_OS_STATE_POLICY_START;
602 }
603 else if (handshake_state == IMV_OS_STATE_ATTR_REQ)
604 {
605 /**
606 * both the IETF Product Information and IETF String Version
607 * attribute should have been present
608 */
609 state->set_recommendation(state,
610 TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION,
611 TNC_IMV_EVALUATION_RESULT_ERROR);
612
613 /* send assessment */
614 result = out_msg->send_assessment(out_msg);
615 out_msg->destroy(out_msg);
616
617 if (result != TNC_RESULT_SUCCESS)
618 {
619 return result;
620 }
621 return this->agent->provide_recommendation(this->agent, state);
622 }
623 else
624 {
625 handshake_state = IMV_OS_STATE_ATTR_REQ;
626 }
627 }
628 os_state->set_handshake_state(os_state, handshake_state);
629 }
630
631 if (handshake_state == IMV_OS_STATE_POLICY_START)
632 {
633 enumerator = session->create_workitem_enumerator(session);
634 if (enumerator)
635 {
636 while (enumerator->enumerate(enumerator, &workitem))
637 {
638 if (workitem->get_imv_id(workitem) != TNC_IMVID_ANY)
639 {
640 continue;
641 }
642
643 switch (workitem->get_type(workitem))
644 {
645 case IMV_WORKITEM_PACKAGES:
646 attr = ietf_attr_attr_request_create(PEN_IETF,
647 IETF_ATTR_INSTALLED_PACKAGES);
648 out_msg->add_attribute(out_msg, attr);
649 break;
650 case IMV_WORKITEM_UNKNOWN_SOURCE:
651 attr = ita_attr_get_settings_create(unknown_source_str);
652 out_msg->add_attribute(out_msg, attr);
653 break;
654 case IMV_WORKITEM_FORWARDING:
655 case IMV_WORKITEM_DEFAULT_PWD:
656 break;
657 default:
658 continue;
659 }
660 workitem->set_imv_id(workitem, imv_id);
661 no_workitems = FALSE;
662 }
663 enumerator->destroy(enumerator);
664
665 if (no_workitems)
666 {
667 DBG2(DBG_IMV, "IMV %d has no workitems - "
668 "no evaluation requested", imv_id);
669 state->set_recommendation(state,
670 TNC_IMV_ACTION_RECOMMENDATION_ALLOW,
671 TNC_IMV_EVALUATION_RESULT_DONT_KNOW);
672 }
673 handshake_state = IMV_OS_STATE_WORKITEMS;
674 os_state->set_handshake_state(os_state, handshake_state);
675 }
676 }
677
678 if (handshake_state == IMV_OS_STATE_WORKITEMS)
679 {
680 TNC_IMV_Evaluation_Result eval;
681 TNC_IMV_Action_Recommendation rec;
682 char result_str[BUF_LEN];
683 bool fail;
684
685 enumerator = session->create_workitem_enumerator(session);
686 while (enumerator->enumerate(enumerator, &workitem))
687 {
688 if (workitem->get_imv_id(workitem) != imv_id)
689 {
690 continue;
691 }
692 eval = TNC_IMV_EVALUATION_RESULT_DONT_KNOW;
693
694 switch (workitem->get_type(workitem))
695 {
696 case IMV_WORKITEM_PACKAGES:
697 {
698 int count, count_update, count_blacklist, count_ok;
699
700 if (!(received & IMV_OS_ATTR_INSTALLED_PACKAGES) ||
701 os_state->get_missing(os_state) > 0)
702 {
703 continue;
704 }
705 os_state->get_count(os_state, &count, &count_update,
706 &count_blacklist, &count_ok);
707 fail = count_update || count_blacklist;
708 eval = fail ? TNC_IMV_EVALUATION_RESULT_NONCOMPLIANT_MINOR :
709 TNC_IMV_EVALUATION_RESULT_COMPLIANT;
710 snprintf(result_str, BUF_LEN, "processed %d packages: "
711 "%d not updated, %d blacklisted, %d ok, "
712 "%d unknown",
713 count, count_update, count_blacklist, count_ok,
714 count - count_update - count_blacklist - count_ok);
715 break;
716 }
717 case IMV_WORKITEM_UNKNOWN_SOURCE:
718 if (!(received & IMV_OS_ATTR_SETTINGS))
719 {
720 continue;
721 }
722 fail = os_state->get_os_settings(os_state) &
723 OS_SETTINGS_UNKNOWN_SOURCE;
724 eval = fail ? TNC_IMV_EVALUATION_RESULT_NONCOMPLIANT_MINOR :
725 TNC_IMV_EVALUATION_RESULT_COMPLIANT;
726 snprintf(result_str, BUF_LEN, "unknown sources%s enabled",
727 fail ? "" : " not");
728 break;
729 case IMV_WORKITEM_FORWARDING:
730 if (!(received & IMV_OS_ATTR_FORWARDING_ENABLED))
731 {
732 continue;
733 }
734 fail = os_state->get_os_settings(os_state) &
735 OS_SETTINGS_FWD_ENABLED;
736 eval = fail ? TNC_IMV_EVALUATION_RESULT_NONCOMPLIANT_MAJOR :
737 TNC_IMV_EVALUATION_RESULT_COMPLIANT;
738 snprintf(result_str, BUF_LEN, "forwarding%s enabled",
739 fail ? "" : " not");
740 break;
741 case IMV_WORKITEM_DEFAULT_PWD:
742 if (!(received & IMV_OS_ATTR_FACTORY_DEFAULT_PWD_ENABLED))
743 {
744 continue;
745 }
746 fail = os_state->get_os_settings(os_state) &
747 OS_SETTINGS_DEFAULT_PWD_ENABLED;
748 eval = fail ? TNC_IMV_EVALUATION_RESULT_NONCOMPLIANT_MAJOR :
749 TNC_IMV_EVALUATION_RESULT_COMPLIANT;
750 snprintf(result_str, BUF_LEN, "factory default password%s enabled",
751 fail ? "" : " not");
752 break;
753 default:
754 continue;
755 }
756 if (eval != TNC_IMV_EVALUATION_RESULT_DONT_KNOW)
757 {
758 session->remove_workitem(session, enumerator);
759 rec = workitem->set_result(workitem, result_str, eval);
760 state->update_recommendation(state, rec, eval);
761 imcv_db->finalize_workitem(imcv_db, workitem);
762 workitem->destroy(workitem);
763 }
764 }
765 enumerator->destroy(enumerator);
766
767 /* finalized all workitems ? */
768 if (session->get_workitem_count(session, imv_id) == 0)
769 {
770 os_state->set_handshake_state(os_state, IMV_OS_STATE_END);
771
772 result = out_msg->send_assessment(out_msg);
773 out_msg->destroy(out_msg);
774 if (result != TNC_RESULT_SUCCESS)
775 {
776 return result;
777 }
778 return this->agent->provide_recommendation(this->agent, state);
779 }
780 }
781
782 /* send non-empty PA-TNC message with excl flag not set */
783 if (out_msg->get_attribute_count(out_msg))
784 {
785 result = out_msg->send(out_msg, FALSE);
786 }
787 out_msg->destroy(out_msg);
788
789 return result;
790 }
791
792 METHOD(imv_agent_if_t, solicit_recommendation, TNC_Result,
793 private_imv_os_agent_t *this, TNC_ConnectionID id)
794 {
795 imv_state_t *state;
796
797 if (!this->agent->get_state(this->agent, id, &state))
798 {
799 return TNC_RESULT_FATAL;
800 }
801 return this->agent->provide_recommendation(this->agent, state);
802 }
803
804 METHOD(imv_agent_if_t, destroy, void,
805 private_imv_os_agent_t *this)
806 {
807 DESTROY_IF(this->agent);
808 DESTROY_IF(this->db);
809 free(this);
810 }
811
812 /**
813 * Described in header.
814 */
815 imv_agent_if_t *imv_os_agent_create(const char *name, TNC_IMVID id,
816 TNC_Version *actual_version)
817 {
818 private_imv_os_agent_t *this;
819 imv_agent_t *agent;
820
821 agent = imv_agent_create(name, msg_types, countof(msg_types), id,
822 actual_version);
823 if (!agent)
824 {
825 return NULL;
826 }
827
828 INIT(this,
829 .public = {
830 .bind_functions = _bind_functions,
831 .notify_connection_change = _notify_connection_change,
832 .receive_message = _receive_message,
833 .receive_message_long = _receive_message_long,
834 .batch_ending = _batch_ending,
835 .solicit_recommendation = _solicit_recommendation,
836 .destroy = _destroy,
837 },
838 .agent = agent,
839 .db = imv_os_database_create(imcv_db),
840 );
841
842 return &this->public;
843 }
844