2 * Copyright (C) 2012-2014 Andreas Steffen
3 * HSR Hochschule fuer Technik Rapperswil
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>.
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
16 #include "imv_os_state.h"
18 #include "imv/imv_lang_string.h"
19 #include "imv/imv_reason_string.h"
20 #include "imv/imv_remediation_string.h"
21 #include "imv/imv_os_info.h"
23 #include <tncif_policy.h>
25 #include <utils/debug.h>
26 #include <collections/linked_list.h>
28 typedef struct private_imv_os_state_t private_imv_os_state_t
;
29 typedef struct package_entry_t package_entry_t
;
30 typedef struct entry_t entry_t
;
31 typedef struct instruction_entry_t instruction_entry_t
;
34 * Private data of an imv_os_state_t object.
36 struct private_imv_os_state_t
{
39 * Public members of imv_os_state_t
41 imv_os_state_t
public;
46 TNC_ConnectionID connection_id
;
49 * TNCCS connection state
51 TNC_ConnectionState state
;
54 * Does the TNCCS connection support long message types?
59 * Does the TNCCS connection support exclusive delivery?
64 * Maximum PA-TNC message size for this TNCCS connection
69 * Flags set for completed actions
71 uint32_t action_flags
;
74 * IMV database session associated with TNCCS connection
76 imv_session_t
*session
;
79 * IMV action recommendation
81 TNC_IMV_Action_Recommendation rec
;
84 * IMV evaluation result
86 TNC_IMV_Evaluation_Result eval
;
89 * IMV OS handshake state
91 imv_os_handshake_state_t handshake_state
;
94 * List of blacklisted packages to be removed
96 linked_list_t
*remove_packages
;
99 h* List of vulnerable packages to be updated
101 linked_list_t
*update_packages
;
106 imv_reason_string_t
*reason_string
;
109 * IETF Remediation Instructions String
111 imv_remediation_string_t
*remediation_string
;
114 * Number of processed packages
119 * Number of not updated packages
124 * Number of blacklisted packages
129 * Number of whitelisted packages
146 * Supported languages
148 static char* languages
[] = { "en", "de", "pl" };
151 * Reason strings for "OS settings"
153 static imv_lang_string_t reason_settings
[] = {
154 { "en", "Improper OS settings were detected" },
155 { "de", "Unzulässige OS Einstellungen wurden festgestellt" },
156 { "pl", "Stwierdzono niewłaściwe ustawienia OS" },
161 * Reason strings for "installed software packages"
163 static imv_lang_string_t reason_packages
[] = {
164 { "en", "Vulnerable or blacklisted software packages were found" },
165 { "de", "Schwachstellenbehaftete oder gesperrte Softwarepakete wurden gefunden" },
166 { "pl", "Znaleziono pakiety podatne na atak lub będące na czarnej liście" },
171 * Instruction strings for "Software Security Updates"
173 static imv_lang_string_t instr_update_packages_title
[] = {
174 { "en", "Software Security Updates" },
175 { "de", "Software Sicherheitsupdates" },
176 { "pl", "Aktualizacja softwaru zabezpieczającego" },
180 static imv_lang_string_t instr_update_packages_descr
[] = {
181 { "en", "Packages with security vulnerabilities were found" },
182 { "de", "Softwarepakete mit Sicherheitsschwachstellen wurden gefunden" },
183 { "pl", "Znaleziono pakiety podatne na atak" },
187 static imv_lang_string_t instr_update_packages_header
[] = {
188 { "en", "Please update the following software packages:" },
189 { "de", "Bitte updaten Sie die folgenden Softwarepakete:" },
190 { "pl", "Proszę zaktualizować następujące pakiety:" },
195 * Instruction strings for "Blacklisted Software Packages"
197 static imv_lang_string_t instr_remove_packages_title
[] = {
198 { "en", "Blacklisted Software Packages" },
199 { "de", "Gesperrte Softwarepakete" },
200 { "pl", "Pakiety będące na czarnej liście" },
204 static imv_lang_string_t instr_remove_packages_descr
[] = {
205 { "en", "Dangerous software packages were found" },
206 { "de", "Gefährliche Softwarepakete wurden gefunden" },
207 { "pl", "Znaleziono niebezpieczne pakiety" },
211 static imv_lang_string_t instr_remove_packages_header
[] = {
212 { "en", "Please remove the following software packages:" },
213 { "de", "Bitte entfernen Sie die folgenden Softwarepakete:" },
214 { "pl", "Proszę usunąć następujące pakiety:" },
219 * Instruction strings for "Forwarding Enabled"
221 static imv_lang_string_t instr_fwd_enabled_title
[] = {
222 { "en", "IP Packet Forwarding" },
223 { "de", "Weiterleitung von IP Paketen" },
224 { "pl", "Przekazywanie pakietów IP" },
228 static imv_lang_string_t instr_fwd_enabled_descr
[] = {
229 { "en", "Please disable the forwarding of IP packets" },
230 { "de", "Bitte deaktivieren Sie das Forwarding von IP Paketen" },
231 { "pl", "Proszę zdezaktywować przekazywanie pakietów IP" },
236 * Instruction strings for "Default Password Enabled"
238 static imv_lang_string_t instr_default_pwd_enabled_title
[] = {
239 { "en", "Default Password" },
240 { "de", "Default Passwort" },
241 { "pl", "Hasło domyślne" },
245 static imv_lang_string_t instr_default_pwd_enabled_descr
[] = {
246 { "en", "Please change the default password" },
247 { "de", "Bitte ändern Sie das Default Passwort" },
248 { "pl", "Proszę zmienić domyślne hasło" },
253 * Instruction strings for "Unknown Source"
255 static imv_lang_string_t instr_unknown_source_title
[] = {
256 { "en", "Unknown Software Origin" },
257 { "de", "Unbekannte Softwareherkunft" },
258 { "pl", "Nieznane pochodzenie softwaru" },
262 static imv_lang_string_t instr_unknown_source_descr
[] = {
263 { "en", "Do not allow the installation of apps from unknown sources" },
264 { "de", "Erlauben Sie nicht die Installation von Apps aus unbekannten Quellen" },
265 { "pl", "Proszę nie dopuszczać do instalacji Apps z nieznanych źródeł" },
269 METHOD(imv_state_t
, get_connection_id
, TNC_ConnectionID
,
270 private_imv_os_state_t
*this)
272 return this->connection_id
;
275 METHOD(imv_state_t
, has_long
, bool,
276 private_imv_os_state_t
*this)
278 return this->has_long
;
281 METHOD(imv_state_t
, has_excl
, bool,
282 private_imv_os_state_t
*this)
284 return this->has_excl
;
287 METHOD(imv_state_t
, set_flags
, void,
288 private_imv_os_state_t
*this, bool has_long
, bool has_excl
)
290 this->has_long
= has_long
;
291 this->has_excl
= has_excl
;
294 METHOD(imv_state_t
, set_max_msg_len
, void,
295 private_imv_os_state_t
*this, uint32_t max_msg_len
)
297 this->max_msg_len
= max_msg_len
;
300 METHOD(imv_state_t
, get_max_msg_len
, uint32_t,
301 private_imv_os_state_t
*this)
303 return this->max_msg_len
;
306 METHOD(imv_state_t
, set_action_flags
, void,
307 private_imv_os_state_t
*this, uint32_t flags
)
309 this->action_flags
|= flags
;
312 METHOD(imv_state_t
, get_action_flags
, uint32_t,
313 private_imv_os_state_t
*this)
315 return this->action_flags
;
318 METHOD(imv_state_t
, set_session
, void,
319 private_imv_os_state_t
*this, imv_session_t
*session
)
321 this->session
= session
;
324 METHOD(imv_state_t
, get_session
, imv_session_t
*,
325 private_imv_os_state_t
*this)
327 return this->session
;
330 METHOD(imv_state_t
, get_recommendation
, void,
331 private_imv_os_state_t
*this, TNC_IMV_Action_Recommendation
*rec
,
332 TNC_IMV_Evaluation_Result
*eval
)
338 METHOD(imv_state_t
, set_recommendation
, void,
339 private_imv_os_state_t
*this, TNC_IMV_Action_Recommendation rec
,
340 TNC_IMV_Evaluation_Result eval
)
346 METHOD(imv_state_t
, update_recommendation
, void,
347 private_imv_os_state_t
*this, TNC_IMV_Action_Recommendation rec
,
348 TNC_IMV_Evaluation_Result eval
)
350 this->rec
= tncif_policy_update_recommendation(this->rec
, rec
);
351 this->eval
= tncif_policy_update_evaluation(this->eval
, eval
);
354 METHOD(imv_state_t
, change_state
, void,
355 private_imv_os_state_t
*this, TNC_ConnectionState new_state
)
357 this->state
= new_state
;
360 METHOD(imv_state_t
, get_reason_string
, bool,
361 private_imv_os_state_t
*this, enumerator_t
*language_enumerator
,
362 chunk_t
*reason_string
, char **reason_language
)
364 if (!this->count_update
&& !this->count_blacklist
& !this->os_settings
)
368 *reason_language
= imv_lang_string_select_lang(language_enumerator
,
369 languages
, countof(languages
));
371 /* Instantiate a TNC Reason String object */
372 DESTROY_IF(this->reason_string
);
373 this->reason_string
= imv_reason_string_create(*reason_language
, "\n");
375 if (this->count_update
|| this->count_blacklist
)
377 this->reason_string
->add_reason(this->reason_string
, reason_packages
);
379 if (this->os_settings
)
381 this->reason_string
->add_reason(this->reason_string
, reason_settings
);
383 *reason_string
= this->reason_string
->get_encoding(this->reason_string
);
388 METHOD(imv_state_t
, get_remediation_instructions
, bool,
389 private_imv_os_state_t
*this, enumerator_t
*language_enumerator
,
390 chunk_t
*string
, char **lang_code
, char **uri
)
392 imv_os_info_t
*os_info
;
395 if (!this->count_update
&& !this->count_blacklist
& !this->os_settings
)
399 *lang_code
= imv_lang_string_select_lang(language_enumerator
,
400 languages
, countof(languages
));
402 /* Instantiate an IETF Remediation Instructions String object */
403 DESTROY_IF(this->remediation_string
);
406 os_info
= this->session
->get_os_info(this->session
);
407 as_xml
= os_info
->get_type(os_info
) == OS_TYPE_ANDROID
;
409 this->remediation_string
= imv_remediation_string_create(as_xml
, *lang_code
);
411 /* List of blacklisted packages to be removed, if any */
412 if (this->count_blacklist
)
414 this->remediation_string
->add_instruction(this->remediation_string
,
415 instr_remove_packages_title
,
416 instr_remove_packages_descr
,
417 instr_remove_packages_header
,
418 this->remove_packages
);
421 /* List of packages in need of an update, if any */
422 if (this->count_update
)
424 this->remediation_string
->add_instruction(this->remediation_string
,
425 instr_update_packages_title
,
426 instr_update_packages_descr
,
427 instr_update_packages_header
,
428 this->update_packages
);
431 /* Add instructions concerning improper OS settings */
432 if (this->os_settings
& OS_SETTINGS_FWD_ENABLED
)
434 this->remediation_string
->add_instruction(this->remediation_string
,
435 instr_fwd_enabled_title
,
436 instr_fwd_enabled_descr
, NULL
, NULL
);
438 if (this->os_settings
& OS_SETTINGS_DEFAULT_PWD_ENABLED
)
440 this->remediation_string
->add_instruction(this->remediation_string
,
441 instr_default_pwd_enabled_title
,
442 instr_default_pwd_enabled_descr
, NULL
, NULL
);
444 if (this->os_settings
& OS_SETTINGS_UNKNOWN_SOURCE
)
446 this->remediation_string
->add_instruction(this->remediation_string
,
447 instr_unknown_source_title
,
448 instr_unknown_source_descr
, NULL
, NULL
);
451 *string
= this->remediation_string
->get_encoding(this->remediation_string
);
452 *uri
= lib
->settings
->get_str(lib
->settings
,
453 "%s.plugins.imv-os.remediation_uri", NULL
, lib
->ns
);
458 METHOD(imv_state_t
, destroy
, void,
459 private_imv_os_state_t
*this)
461 DESTROY_IF(this->session
);
462 DESTROY_IF(this->reason_string
);
463 DESTROY_IF(this->remediation_string
);
464 this->update_packages
->destroy_function(this->update_packages
, free
);
465 this->remove_packages
->destroy_function(this->remove_packages
, free
);
469 METHOD(imv_os_state_t
, set_handshake_state
, void,
470 private_imv_os_state_t
*this, imv_os_handshake_state_t new_state
)
472 this->handshake_state
= new_state
;
475 METHOD(imv_os_state_t
, get_handshake_state
, imv_os_handshake_state_t
,
476 private_imv_os_state_t
*this)
478 return this->handshake_state
;
482 METHOD(imv_os_state_t
, set_count
, void,
483 private_imv_os_state_t
*this, int count
, int count_update
,
484 int count_blacklist
, int count_ok
)
486 this->count
+= count
;
487 this->count_update
+= count_update
;
488 this->count_blacklist
+= count_blacklist
;
489 this->count_ok
+= count_ok
;
492 METHOD(imv_os_state_t
, get_count
, void,
493 private_imv_os_state_t
*this, int *count
, int *count_update
,
494 int *count_blacklist
, int *count_ok
)
498 *count
= this->count
;
502 *count_update
= this->count_update
;
506 *count_blacklist
= this->count_blacklist
;
510 *count_ok
= this->count_ok
;
514 METHOD(imv_os_state_t
, set_os_settings
, void,
515 private_imv_os_state_t
*this, u_int settings
)
517 this->os_settings
|= settings
;
520 METHOD(imv_os_state_t
, get_os_settings
, u_int
,
521 private_imv_os_state_t
*this)
523 return this->os_settings
;
526 METHOD(imv_os_state_t
, set_angel_count
, void,
527 private_imv_os_state_t
*this, bool start
)
529 this->angel_count
+= start ?
1 : -1;
532 METHOD(imv_os_state_t
, get_angel_count
, int,
533 private_imv_os_state_t
*this)
535 return this->angel_count
;
538 METHOD(imv_os_state_t
, add_bad_package
, void,
539 private_imv_os_state_t
*this, char *package
,
540 os_package_state_t package_state
)
542 package
= strdup(package
);
544 if (package_state
== OS_PACKAGE_STATE_BLACKLIST
)
546 this->remove_packages
->insert_last(this->remove_packages
, package
);
550 this->update_packages
->insert_last(this->update_packages
, package
);
555 * Described in header.
557 imv_state_t
*imv_os_state_create(TNC_ConnectionID connection_id
)
559 private_imv_os_state_t
*this;
564 .get_connection_id
= _get_connection_id
,
565 .has_long
= _has_long
,
566 .has_excl
= _has_excl
,
567 .set_flags
= _set_flags
,
568 .set_max_msg_len
= _set_max_msg_len
,
569 .get_max_msg_len
= _get_max_msg_len
,
570 .set_action_flags
= _set_action_flags
,
571 .get_action_flags
= _get_action_flags
,
572 .set_session
= _set_session
,
573 .get_session
= _get_session
,
574 .change_state
= _change_state
,
575 .get_recommendation
= _get_recommendation
,
576 .set_recommendation
= _set_recommendation
,
577 .update_recommendation
= _update_recommendation
,
578 .get_reason_string
= _get_reason_string
,
579 .get_remediation_instructions
= _get_remediation_instructions
,
582 .set_handshake_state
= _set_handshake_state
,
583 .get_handshake_state
= _get_handshake_state
,
584 .set_count
= _set_count
,
585 .get_count
= _get_count
,
586 .set_os_settings
= _set_os_settings
,
587 .get_os_settings
= _get_os_settings
,
588 .set_angel_count
= _set_angel_count
,
589 .get_angel_count
= _get_angel_count
,
590 .add_bad_package
= _add_bad_package
,
592 .state
= TNC_CONNECTION_STATE_CREATE
,
593 .rec
= TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION
,
594 .eval
= TNC_IMV_EVALUATION_RESULT_DONT_KNOW
,
595 .connection_id
= connection_id
,
596 .update_packages
= linked_list_create(),
597 .remove_packages
= linked_list_create(),
600 return &this->public.interface
;