Separated IMV session management from IMV policy database
[strongswan.git] / src / libimcv / plugins / imv_scanner / imv_scanner_state.c
1 /*
2 * Copyright (C) 2011-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 #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 <tncif_policy.h>
22
23 #include <utils/lexparser.h>
24 #include <utils/debug.h>
25
26 typedef struct private_imv_scanner_state_t private_imv_scanner_state_t;
27
28 /**
29 * Private data of an imv_scanner_state_t object.
30 */
31 struct private_imv_scanner_state_t {
32
33 /**
34 * Public members of imv_scanner_state_t
35 */
36 imv_scanner_state_t public;
37
38 /**
39 * TNCCS connection ID
40 */
41 TNC_ConnectionID connection_id;
42
43 /**
44 * TNCCS connection state
45 */
46 TNC_ConnectionState state;
47
48 /**
49 * Does the TNCCS connection support long message types?
50 */
51 bool has_long;
52
53 /**
54 * Does the TNCCS connection support exclusive delivery?
55 */
56 bool has_excl;
57
58 /**
59 * Maximum PA-TNC message size for this TNCCS connection
60 */
61 uint32_t max_msg_len;
62
63 /**
64 * Flags set for completed actions
65 */
66 uint32_t action_flags;
67
68 /**
69 * IMV database session associatied with TNCCS connection
70 */
71 imv_session_t *session;
72
73 /**
74 * IMV action recommendation
75 */
76 TNC_IMV_Action_Recommendation rec;
77
78 /**
79 * IMV evaluation result
80 */
81 TNC_IMV_Evaluation_Result eval;
82
83 /**
84 * IMV Scanner handshake state
85 */
86 imv_scanner_handshake_state_t handshake_state;
87
88 /**
89 * Copy of the received IEEE Port Filter attribute
90 */
91 ietf_attr_port_filter_t *port_filter_attr;
92
93 /**
94 * List with ports that should be closed
95 */
96 linked_list_t *violating_ports;
97
98 /**
99 * TNC Reason String
100 */
101 imv_reason_string_t *reason_string;
102
103 /**
104 * IETF Remediation Instructions String
105 */
106 imv_remediation_string_t *remediation_string;
107
108 };
109
110 /**
111 * Supported languages
112 */
113 static char* languages[] = { "en", "de", "fr", "pl" };
114
115 /**
116 * Reason strings for "Port Filter"
117 */
118 static imv_lang_string_t reasons[] = {
119 { "en", "Open server ports were detected" },
120 { "de", "Offene Serverports wurden festgestellt" },
121 { "fr", "Il y a des ports du serveur ouverts" },
122 { "pl", "Są otwarte porty serwera" },
123 { NULL, NULL }
124 };
125
126 /**
127 * Instruction strings for "Port Filters"
128 */
129 static imv_lang_string_t instr_ports_title[] = {
130 { "en", "Open Server Ports" },
131 { "de", "Offene Server Ports" },
132 { "fr", "Ports ouverts du serveur" },
133 { "pl", "Otwarte Porty Serwera" },
134 { NULL, NULL }
135 };
136
137 static imv_lang_string_t instr_ports_descr[] = {
138 { "en", "Open Internet ports have been detected" },
139 { "de", "Offenen Internet-Ports wurden festgestellt" },
140 { "fr", "Il y'a des ports Internet ouverts" },
141 { "pl", "Porty internetowe są otwarte" },
142 { NULL, NULL }
143 };
144
145 static imv_lang_string_t instr_ports_header[] = {
146 { "en", "Please close the following server ports:" },
147 { "de", "Bitte schliessen Sie die folgenden Serverports:" },
148 { "fr", "Fermez les ports du serveur suivants s'il vous plait:" },
149 { "pl", "Proszę zamknąć następujące porty serwera:" },
150 { NULL, NULL }
151 };
152
153 METHOD(imv_state_t, get_connection_id, TNC_ConnectionID,
154 private_imv_scanner_state_t *this)
155 {
156 return this->connection_id;
157 }
158
159 METHOD(imv_state_t, has_long, bool,
160 private_imv_scanner_state_t *this)
161 {
162 return this->has_long;
163 }
164
165 METHOD(imv_state_t, has_excl, bool,
166 private_imv_scanner_state_t *this)
167 {
168 return this->has_excl;
169 }
170
171 METHOD(imv_state_t, set_flags, void,
172 private_imv_scanner_state_t *this, bool has_long, bool has_excl)
173 {
174 this->has_long = has_long;
175 this->has_excl = has_excl;
176 }
177
178 METHOD(imv_state_t, set_max_msg_len, void,
179 private_imv_scanner_state_t *this, uint32_t max_msg_len)
180 {
181 this->max_msg_len = max_msg_len;
182 }
183
184 METHOD(imv_state_t, get_max_msg_len, uint32_t,
185 private_imv_scanner_state_t *this)
186 {
187 return this->max_msg_len;
188 }
189
190 METHOD(imv_state_t, set_action_flags, void,
191 private_imv_scanner_state_t *this, uint32_t flags)
192 {
193 this->action_flags |= flags;
194 }
195
196 METHOD(imv_state_t, get_action_flags, uint32_t,
197 private_imv_scanner_state_t *this)
198 {
199 return this->action_flags;
200 }
201
202 METHOD(imv_state_t, set_session, void,
203 private_imv_scanner_state_t *this, imv_session_t *session)
204 {
205 this->session = session;
206 }
207
208 METHOD(imv_state_t, get_session, imv_session_t*,
209 private_imv_scanner_state_t *this)
210 {
211 return this->session;
212 }
213
214 METHOD(imv_state_t, change_state, void,
215 private_imv_scanner_state_t *this, TNC_ConnectionState new_state)
216 {
217 this->state = new_state;
218 }
219
220 METHOD(imv_state_t, get_recommendation, void,
221 private_imv_scanner_state_t *this, TNC_IMV_Action_Recommendation *rec,
222 TNC_IMV_Evaluation_Result *eval)
223 {
224 *rec = this->rec;
225 *eval = this->eval;
226 }
227
228 METHOD(imv_state_t, set_recommendation, void,
229 private_imv_scanner_state_t *this, TNC_IMV_Action_Recommendation rec,
230 TNC_IMV_Evaluation_Result eval)
231 {
232 this->rec = rec;
233 this->eval = eval;
234 }
235
236 METHOD(imv_state_t, update_recommendation, void,
237 private_imv_scanner_state_t *this, TNC_IMV_Action_Recommendation rec,
238 TNC_IMV_Evaluation_Result eval)
239 {
240 this->rec = tncif_policy_update_recommendation(this->rec, rec);
241 this->eval = tncif_policy_update_evaluation(this->eval, eval);
242 }
243
244 METHOD(imv_state_t, get_reason_string, bool,
245 private_imv_scanner_state_t *this, enumerator_t *language_enumerator,
246 chunk_t *reason_string, char **reason_language)
247 {
248 if (this->violating_ports->get_count(this->violating_ports) == 0)
249 {
250 return FALSE;
251 }
252 *reason_language = imv_lang_string_select_lang(language_enumerator,
253 languages, countof(languages));
254
255 /* Instantiate a TNC Reason String object */
256 DESTROY_IF(this->reason_string);
257 this->reason_string = imv_reason_string_create(*reason_language, "\n");
258 if (this->rec != TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION)
259 {
260 this->reason_string->add_reason(this->reason_string, reasons);
261 }
262 *reason_string = this->reason_string->get_encoding(this->reason_string);
263
264 return TRUE;
265 }
266
267 METHOD(imv_state_t, get_remediation_instructions, bool,
268 private_imv_scanner_state_t *this, enumerator_t *language_enumerator,
269 chunk_t *string, char **lang_code, char **uri)
270 {
271 if (this->violating_ports->get_count(this->violating_ports) == 0)
272 {
273 return FALSE;
274 }
275 *lang_code = imv_lang_string_select_lang(language_enumerator,
276 languages, countof(languages));
277
278 /* Instantiate an IETF Remediation Instructions String object */
279 DESTROY_IF(this->remediation_string);
280 this->remediation_string = imv_remediation_string_create(
281 TRUE, *lang_code); /* TODO get os_type */
282
283 this->remediation_string->add_instruction(this->remediation_string,
284 instr_ports_title,
285 instr_ports_descr,
286 instr_ports_header,
287 this->violating_ports);
288 *string = this->remediation_string->get_encoding(this->remediation_string);
289 *uri = lib->settings->get_str(lib->settings,
290 "%s.plugins.imv-scanner.remediation_uri", NULL, lib->ns);
291
292 return TRUE;
293 }
294
295 METHOD(imv_state_t, destroy, void,
296 private_imv_scanner_state_t *this)
297 {
298 DESTROY_IF(this->session);
299 DESTROY_IF(this->reason_string);
300 DESTROY_IF(this->remediation_string);
301 DESTROY_IF(&this->port_filter_attr->pa_tnc_attribute);
302 this->violating_ports->destroy_function(this->violating_ports, free);
303 free(this);
304 }
305
306 METHOD(imv_scanner_state_t, set_handshake_state, void,
307 private_imv_scanner_state_t *this, imv_scanner_handshake_state_t new_state)
308 {
309 this->handshake_state = new_state;
310 }
311
312 METHOD(imv_scanner_state_t, get_handshake_state, imv_scanner_handshake_state_t,
313 private_imv_scanner_state_t *this)
314 {
315 return this->handshake_state;
316 }
317
318 METHOD(imv_scanner_state_t, set_port_filter_attr, void,
319 private_imv_scanner_state_t *this, ietf_attr_port_filter_t *attr)
320 {
321 DESTROY_IF(&this->port_filter_attr->pa_tnc_attribute);
322 this->port_filter_attr = attr;
323 }
324
325 METHOD(imv_scanner_state_t, get_port_filter_attr, ietf_attr_port_filter_t*,
326 private_imv_scanner_state_t *this)
327 {
328 return this->port_filter_attr;
329 }
330
331 METHOD(imv_scanner_state_t, add_violating_port, void,
332 private_imv_scanner_state_t *this, char *port)
333 {
334 this->violating_ports->insert_last(this->violating_ports, port);
335 }
336
337 /**
338 * Described in header.
339 */
340 imv_state_t *imv_scanner_state_create(TNC_ConnectionID connection_id)
341 {
342 private_imv_scanner_state_t *this;
343
344 INIT(this,
345 .public = {
346 .interface = {
347 .get_connection_id = _get_connection_id,
348 .has_long = _has_long,
349 .has_excl = _has_excl,
350 .set_flags = _set_flags,
351 .set_max_msg_len = _set_max_msg_len,
352 .get_max_msg_len = _get_max_msg_len,
353 .set_action_flags = _set_action_flags,
354 .get_action_flags = _get_action_flags,
355 .set_session = _set_session,
356 .get_session= _get_session,
357 .change_state = _change_state,
358 .get_recommendation = _get_recommendation,
359 .set_recommendation = _set_recommendation,
360 .update_recommendation = _update_recommendation,
361 .get_reason_string = _get_reason_string,
362 .get_remediation_instructions = _get_remediation_instructions,
363 .destroy = _destroy,
364 },
365 .set_handshake_state = _set_handshake_state,
366 .get_handshake_state = _get_handshake_state,
367 .set_port_filter_attr = _set_port_filter_attr,
368 .get_port_filter_attr = _get_port_filter_attr,
369 .add_violating_port = _add_violating_port,
370 },
371 .state = TNC_CONNECTION_STATE_CREATE,
372 .rec = TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION,
373 .eval = TNC_IMV_EVALUATION_RESULT_DONT_KNOW,
374 .connection_id = connection_id,
375 .violating_ports = linked_list_create(),
376 );
377
378 return &this->public.interface;
379 }
380
381