implemented output of item lists in remediation instructions
[strongswan.git] / src / libimcv / plugins / imv_scanner / imv_scanner_state.c
1 /*
2 * Copyright (C) 2011-2012 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 #include "imv_scanner_state.h"
17 #include "imv/imv_lang_string.h"
18 #include "imv/imv_reason_string.h"
19 #include "imv/imv_remediation_string.h"
20
21 #include <utils/lexparser.h>
22 #include <utils/debug.h>
23
24 typedef struct private_imv_scanner_state_t private_imv_scanner_state_t;
25
26 /**
27 * Private data of an imv_scanner_state_t object.
28 */
29 struct private_imv_scanner_state_t {
30
31 /**
32 * Public members of imv_scanner_state_t
33 */
34 imv_scanner_state_t public;
35
36 /**
37 * TNCCS connection ID
38 */
39 TNC_ConnectionID connection_id;
40
41 /**
42 * TNCCS connection state
43 */
44 TNC_ConnectionState state;
45
46 /**
47 * Does the TNCCS connection support long message types?
48 */
49 bool has_long;
50
51 /**
52 * Does the TNCCS connection support exclusive delivery?
53 */
54 bool has_excl;
55
56 /**
57 * Maximum PA-TNC message size for this TNCCS connection
58 */
59 u_int32_t max_msg_len;
60
61 /**
62 * IMV action recommendation
63 */
64 TNC_IMV_Action_Recommendation rec;
65
66 /**
67 * IMV evaluation result
68 */
69 TNC_IMV_Evaluation_Result eval;
70
71 /**
72 * List with ports that should be closed
73 */
74 linked_list_t *violating_ports;
75
76 /**
77 * TNC Reason String
78 */
79 imv_reason_string_t *reason_string;
80
81 /**
82 * IETF Remediation Instructions String
83 */
84 imv_remediation_string_t *remediation_string;
85
86 };
87
88 /**
89 * Supported languages
90 */
91 static char* languages[] = { "en", "de", "fr", "pl" };
92
93 /**
94 * Reason strings for "Port Filter"
95 */
96 static imv_lang_string_t reasons[] = {
97 { "en", "Open server ports were detected" },
98 { "de", "Offene Serverports wurden festgestellt" },
99 { "fr", "Il y a des ports du serveur ouverts" },
100 { "pl", "Są otwarte porty serwera" },
101 { NULL, NULL }
102 };
103
104 /**
105 * Instruction strings for "Port Filters"
106 */
107 static imv_lang_string_t instr_ports_title[] = {
108 { "en", "Open Server Ports" },
109 { "de", "Offene Server Ports" },
110 { "fr", "Ports ouverts du serveur" },
111 { "pl", "Otwarte Porty Serwera" },
112 { NULL, NULL }
113 };
114
115 static imv_lang_string_t instr_ports_descr[] = {
116 { "en", "Open Internet ports have been detected" },
117 { "de", "Offenen Internet-Ports wurden festgestellt" },
118 { "fr", "Il y'a des ports Internet ouverts" },
119 { "pl", "Porty internetowe są otwarte" },
120 { NULL, NULL }
121 };
122
123 static imv_lang_string_t instr_ports_header[] = {
124 { "en", "Please close the following server ports:" },
125 { "de", "Bitte schliessen Sie die folgenden Serverports:" },
126 { "fr", "Fermez les ports du serveur suivants s'il vous plait:" },
127 { "pl", "Proszę zamknąć następujące porty serwera:" },
128 { NULL, NULL }
129 };
130
131 METHOD(imv_state_t, get_connection_id, TNC_ConnectionID,
132 private_imv_scanner_state_t *this)
133 {
134 return this->connection_id;
135 }
136
137 METHOD(imv_state_t, has_long, bool,
138 private_imv_scanner_state_t *this)
139 {
140 return this->has_long;
141 }
142
143 METHOD(imv_state_t, has_excl, bool,
144 private_imv_scanner_state_t *this)
145 {
146 return this->has_excl;
147 }
148
149 METHOD(imv_state_t, set_flags, void,
150 private_imv_scanner_state_t *this, bool has_long, bool has_excl)
151 {
152 this->has_long = has_long;
153 this->has_excl = has_excl;
154 }
155
156 METHOD(imv_state_t, set_max_msg_len, void,
157 private_imv_scanner_state_t *this, u_int32_t max_msg_len)
158 {
159 this->max_msg_len = max_msg_len;
160 }
161
162 METHOD(imv_state_t, get_max_msg_len, u_int32_t,
163 private_imv_scanner_state_t *this)
164 {
165 return this->max_msg_len;
166 }
167
168 METHOD(imv_state_t, change_state, void,
169 private_imv_scanner_state_t *this, TNC_ConnectionState new_state)
170 {
171 this->state = new_state;
172 }
173
174 METHOD(imv_state_t, get_recommendation, void,
175 private_imv_scanner_state_t *this, TNC_IMV_Action_Recommendation *rec,
176 TNC_IMV_Evaluation_Result *eval)
177 {
178 *rec = this->rec;
179 *eval = this->eval;
180 }
181
182 METHOD(imv_state_t, set_recommendation, void,
183 private_imv_scanner_state_t *this, TNC_IMV_Action_Recommendation rec,
184 TNC_IMV_Evaluation_Result eval)
185 {
186 this->rec = rec;
187 this->eval = eval;
188 }
189
190 METHOD(imv_state_t, get_reason_string, bool,
191 private_imv_scanner_state_t *this, enumerator_t *language_enumerator,
192 chunk_t *reason_string, char **reason_language)
193 {
194 if (!this->violating_ports)
195 {
196 return FALSE;
197 }
198 *reason_language = imv_lang_string_select_lang(language_enumerator,
199 languages, countof(languages));
200
201 /* Instantiate a TNC Reason String object */
202 DESTROY_IF(this->reason_string);
203 this->reason_string = imv_reason_string_create(*reason_language);
204 this->reason_string->add_reason(this->reason_string, reasons);
205 *reason_string = this->reason_string->get_encoding(this->reason_string);
206
207 return TRUE;
208 }
209
210 METHOD(imv_state_t, get_remediation_instructions, bool,
211 private_imv_scanner_state_t *this, enumerator_t *language_enumerator,
212 chunk_t *string, char **lang_code, char **uri)
213 {
214 if (!this->violating_ports)
215 {
216 return FALSE;
217 }
218 *lang_code = imv_lang_string_select_lang(language_enumerator,
219 languages, countof(languages));
220
221 /* Instantiate an IETF Remediation Instructions String object */
222 DESTROY_IF(this->remediation_string);
223 this->remediation_string = imv_remediation_string_create(
224 TRUE, *lang_code); /* TODO get os_type */
225
226 this->remediation_string->add_instruction(this->remediation_string,
227 instr_ports_title,
228 instr_ports_descr,
229 instr_ports_header,
230 this->violating_ports);
231 *string = this->remediation_string->get_encoding(this->remediation_string);
232 *uri = lib->settings->get_str(lib->settings,
233 "libimcv.plugins.imv-scanner.remediation_uri", NULL);
234
235 return TRUE;
236 }
237
238 METHOD(imv_state_t, destroy, void,
239 private_imv_scanner_state_t *this)
240 {
241 DESTROY_IF(this->reason_string);
242 DESTROY_IF(this->remediation_string);
243 this->violating_ports->destroy_function(this->violating_ports, free);
244 free(this);
245 }
246
247 METHOD(imv_scanner_state_t, add_violating_port, void,
248 private_imv_scanner_state_t *this, char *port)
249 {
250 this->violating_ports->insert_last(this->violating_ports, port);
251 }
252
253 /**
254 * Described in header.
255 */
256 imv_state_t *imv_scanner_state_create(TNC_ConnectionID connection_id)
257 {
258 private_imv_scanner_state_t *this;
259
260 INIT(this,
261 .public = {
262 .interface = {
263 .get_connection_id = _get_connection_id,
264 .has_long = _has_long,
265 .has_excl = _has_excl,
266 .set_flags = _set_flags,
267 .set_max_msg_len = _set_max_msg_len,
268 .get_max_msg_len = _get_max_msg_len,
269 .change_state = _change_state,
270 .get_recommendation = _get_recommendation,
271 .set_recommendation = _set_recommendation,
272 .get_reason_string = _get_reason_string,
273 .get_remediation_instructions = _get_remediation_instructions,
274 .destroy = _destroy,
275 },
276 .add_violating_port = _add_violating_port,
277 },
278 .state = TNC_CONNECTION_STATE_CREATE,
279 .rec = TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION,
280 .eval = TNC_IMV_EVALUATION_RESULT_DONT_KNOW,
281 .connection_id = connection_id,
282 .violating_ports = linked_list_create(),
283 );
284
285 return &this->public.interface;
286 }
287
288