refactored reason string and remediation instructions framework
[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 * String with list of ports that should be closed
73 */
74 char *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 * Table of reason strings
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 * Table of "ports" remediation instruction title strings
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 /**
116 * Table of "ports" remediation instruction descriptions strings
117 */
118 static imv_lang_string_t instr_ports_descr[] = {
119 { "en", "Please close the following server ports" },
120 { "de", "Bitte schliessen Sie die folgenden Serverports" },
121 { "fr", "Fermez les ports du serveur suivants s'il vous plait" },
122 { "pl", "Proszę zamknąć następujące porty serwera" },
123 { NULL, NULL }
124 };
125
126 METHOD(imv_state_t, get_connection_id, TNC_ConnectionID,
127 private_imv_scanner_state_t *this)
128 {
129 return this->connection_id;
130 }
131
132 METHOD(imv_state_t, has_long, bool,
133 private_imv_scanner_state_t *this)
134 {
135 return this->has_long;
136 }
137
138 METHOD(imv_state_t, has_excl, bool,
139 private_imv_scanner_state_t *this)
140 {
141 return this->has_excl;
142 }
143
144 METHOD(imv_state_t, set_flags, void,
145 private_imv_scanner_state_t *this, bool has_long, bool has_excl)
146 {
147 this->has_long = has_long;
148 this->has_excl = has_excl;
149 }
150
151 METHOD(imv_state_t, set_max_msg_len, void,
152 private_imv_scanner_state_t *this, u_int32_t max_msg_len)
153 {
154 this->max_msg_len = max_msg_len;
155 }
156
157 METHOD(imv_state_t, get_max_msg_len, u_int32_t,
158 private_imv_scanner_state_t *this)
159 {
160 return this->max_msg_len;
161 }
162
163 METHOD(imv_state_t, change_state, void,
164 private_imv_scanner_state_t *this, TNC_ConnectionState new_state)
165 {
166 this->state = new_state;
167 }
168
169 METHOD(imv_state_t, get_recommendation, void,
170 private_imv_scanner_state_t *this, TNC_IMV_Action_Recommendation *rec,
171 TNC_IMV_Evaluation_Result *eval)
172 {
173 *rec = this->rec;
174 *eval = this->eval;
175 }
176
177 METHOD(imv_state_t, set_recommendation, void,
178 private_imv_scanner_state_t *this, TNC_IMV_Action_Recommendation rec,
179 TNC_IMV_Evaluation_Result eval)
180 {
181 this->rec = rec;
182 this->eval = eval;
183 }
184
185 METHOD(imv_state_t, get_reason_string, bool,
186 private_imv_scanner_state_t *this, enumerator_t *language_enumerator,
187 chunk_t *reason_string, char **reason_language)
188 {
189 if (!this->violating_ports)
190 {
191 return FALSE;
192 }
193 *reason_language = imv_lang_string_select_lang(language_enumerator,
194 languages, countof(languages));
195
196 /* Instantiate a TNC Reason String object */
197 DESTROY_IF(this->reason_string);
198 this->reason_string = imv_reason_string_create(*reason_language);
199 this->reason_string->add_reason(this->reason_string, reasons);
200 *reason_string = this->reason_string->get_encoding(this->reason_string);
201
202 return TRUE;
203 }
204
205 METHOD(imv_state_t, get_remediation_instructions, bool,
206 private_imv_scanner_state_t *this, enumerator_t *language_enumerator,
207 chunk_t *string, char **lang_code, char **uri)
208 {
209 if (!this->violating_ports)
210 {
211 return FALSE;
212 }
213 *lang_code = imv_lang_string_select_lang(language_enumerator,
214 languages, countof(languages));
215
216 /* Instantiate an IETF Remediation Instructions String object */
217 DESTROY_IF(this->remediation_string);
218 this->remediation_string = imv_remediation_string_create(
219 TRUE, *lang_code); /* TODO get os_type */
220
221 this->remediation_string->add_instruction(this->remediation_string,
222 instr_ports_title, instr_ports_descr, NULL, NULL);
223
224 *string = this->remediation_string->get_encoding(this->remediation_string);
225 *uri = lib->settings->get_str(lib->settings,
226 "libimcv.plugins.imv-scanner.remediation_uri", NULL);
227
228 return TRUE;
229 }
230
231 METHOD(imv_state_t, destroy, void,
232 private_imv_scanner_state_t *this)
233 {
234 DESTROY_IF(this->reason_string);
235 DESTROY_IF(this->remediation_string);
236 free(this->violating_ports);
237 free(this);
238 }
239
240 METHOD(imv_scanner_state_t, set_violating_ports, void,
241 private_imv_scanner_state_t *this, char *ports)
242 {
243 this->violating_ports = strdup(ports);
244 }
245
246 /**
247 * Described in header.
248 */
249 imv_state_t *imv_scanner_state_create(TNC_ConnectionID connection_id)
250 {
251 private_imv_scanner_state_t *this;
252
253 INIT(this,
254 .public = {
255 .interface = {
256 .get_connection_id = _get_connection_id,
257 .has_long = _has_long,
258 .has_excl = _has_excl,
259 .set_flags = _set_flags,
260 .set_max_msg_len = _set_max_msg_len,
261 .get_max_msg_len = _get_max_msg_len,
262 .change_state = _change_state,
263 .get_recommendation = _get_recommendation,
264 .set_recommendation = _set_recommendation,
265 .get_reason_string = _get_reason_string,
266 .get_remediation_instructions = _get_remediation_instructions,
267 .destroy = _destroy,
268 },
269 .set_violating_ports = _set_violating_ports,
270 },
271 .state = TNC_CONNECTION_STATE_CREATE,
272 .rec = TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION,
273 .eval = TNC_IMV_EVALUATION_RESULT_DONT_KNOW,
274 .connection_id = connection_id,
275 );
276
277 return &this->public.interface;
278 }
279
280