centralized processing of assessment information
[strongswan.git] / src / libimcv / plugins / imv_test / imv_test_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_test_state.h"
17
18 #include <utils/lexparser.h>
19 #include <collections/linked_list.h>
20 #include <utils/debug.h>
21
22 typedef struct private_imv_test_state_t private_imv_test_state_t;
23
24 /**
25 * Private data of an imv_test_state_t object.
26 */
27 struct private_imv_test_state_t {
28
29 /**
30 * Public members of imv_test_state_t
31 */
32 imv_test_state_t public;
33
34 /**
35 * TNCCS connection ID
36 */
37 TNC_ConnectionID connection_id;
38
39 /**
40 * TNCCS connection state
41 */
42 TNC_ConnectionState state;
43
44 /**
45 * Does the TNCCS connection support long message types?
46 */
47 bool has_long;
48
49 /**
50 * Does the TNCCS connection support exclusive delivery?
51 */
52 bool has_excl;
53
54 /**
55 * Maximum PA-TNC message size for this TNCCS connection
56 */
57 u_int32_t max_msg_len;
58
59 /**
60 * IMV action recommendation
61 */
62 TNC_IMV_Action_Recommendation rec;
63
64 /**
65 * IMV evaluation result
66 */
67 TNC_IMV_Evaluation_Result eval;
68
69 /**
70 * List of IMCs
71 */
72 linked_list_t *imcs;
73
74 };
75
76 typedef struct imc_entry_t imc_entry_t;
77
78 /**
79 * Define an internal IMC entry
80 */
81 struct imc_entry_t {
82 TNC_UInt32 imc_id;
83 int rounds;
84 };
85
86 typedef struct entry_t entry_t;
87
88 /**
89 * Define an internal reason string entry
90 */
91 struct entry_t {
92 char *lang;
93 char *string;
94 };
95
96 /**
97 * Table of multi-lingual reason string entries
98 */
99 static entry_t reasons[] = {
100 { "en", "IMC Test was not configured with \"command = allow\"" },
101 { "de", "IMC Test wurde nicht mit \"command = allow\" konfiguriert" },
102 { "fr", "IMC Test n'etait pas configuré avec \"command = allow\"" },
103 { "pl", "IMC Test nie zostało skonfigurowany z \"command = allow\"" }
104 };
105
106 METHOD(imv_state_t, get_connection_id, TNC_ConnectionID,
107 private_imv_test_state_t *this)
108 {
109 return this->connection_id;
110 }
111
112 METHOD(imv_state_t, has_long, bool,
113 private_imv_test_state_t *this)
114 {
115 return this->has_long;
116 }
117
118 METHOD(imv_state_t, has_excl, bool,
119 private_imv_test_state_t *this)
120 {
121 return this->has_excl;
122 }
123
124 METHOD(imv_state_t, set_flags, void,
125 private_imv_test_state_t *this, bool has_long, bool has_excl)
126 {
127 this->has_long = has_long;
128 this->has_excl = has_excl;
129 }
130
131 METHOD(imv_state_t, set_max_msg_len, void,
132 private_imv_test_state_t *this, u_int32_t max_msg_len)
133 {
134 this->max_msg_len = max_msg_len;
135 }
136
137 METHOD(imv_state_t, get_max_msg_len, u_int32_t,
138 private_imv_test_state_t *this)
139 {
140 return this->max_msg_len;
141 }
142
143 METHOD(imv_state_t, change_state, void,
144 private_imv_test_state_t *this, TNC_ConnectionState new_state)
145 {
146 this->state = new_state;
147 }
148
149 METHOD(imv_state_t, get_recommendation, void,
150 private_imv_test_state_t *this, TNC_IMV_Action_Recommendation *rec,
151 TNC_IMV_Evaluation_Result *eval)
152 {
153 *rec = this->rec;
154 *eval = this->eval;
155 }
156
157 METHOD(imv_state_t, set_recommendation, void,
158 private_imv_test_state_t *this, TNC_IMV_Action_Recommendation rec,
159 TNC_IMV_Evaluation_Result eval)
160 {
161 this->rec = rec;
162 this->eval = eval;
163 }
164
165 METHOD(imv_state_t, get_reason_string, bool,
166 private_imv_test_state_t *this, enumerator_t *language_enumerator,
167 char **reason_string, char **reason_language)
168 {
169 bool match = FALSE;
170 char *lang;
171 int i;
172
173 /* set the default language */
174 *reason_language = reasons[0].lang;
175 *reason_string = reasons[0].string;
176
177 while (language_enumerator->enumerate(language_enumerator, &lang))
178 {
179 for (i = 0 ; i < countof(reasons); i++)
180 {
181 if (streq(lang, reasons[i].lang))
182 {
183 match = TRUE;
184 *reason_language = reasons[i].lang;
185 *reason_string = reasons[i].string;
186 break;
187 }
188 }
189 if (match)
190 {
191 break;
192 }
193 }
194
195 return TRUE;
196 }
197
198 METHOD(imv_state_t, get_remediation_instructions, bool,
199 private_imv_test_state_t *this, enumerator_t *language_enumerator,
200 char **string, char **lang_code, char **uri)
201 {
202 return FALSE;
203 }
204
205 METHOD(imv_state_t, destroy, void,
206 private_imv_test_state_t *this)
207 {
208 this->imcs->destroy_function(this->imcs, free);
209 free(this);
210 }
211
212 METHOD(imv_test_state_t, add_imc, void,
213 private_imv_test_state_t *this, TNC_UInt32 imc_id, int rounds)
214 {
215 enumerator_t *enumerator;
216 imc_entry_t *imc_entry;
217 bool found = FALSE;
218
219 enumerator = this->imcs->create_enumerator(this->imcs);
220 while (enumerator->enumerate(enumerator, &imc_entry))
221 {
222 if (imc_entry->imc_id == imc_id)
223 {
224 found = TRUE;
225 break;
226 }
227 }
228 enumerator->destroy(enumerator);
229
230 if (!found)
231 {
232 imc_entry = malloc_thing(imc_entry_t);
233 imc_entry->imc_id = imc_id;
234 imc_entry->rounds = rounds;
235 this->imcs->insert_last(this->imcs, imc_entry);
236 }
237 }
238
239 METHOD(imv_test_state_t, set_rounds, void,
240 private_imv_test_state_t *this, int rounds)
241 {
242 enumerator_t *enumerator;
243 imc_entry_t *imc_entry;
244
245 enumerator = this->imcs->create_enumerator(this->imcs);
246 while (enumerator->enumerate(enumerator, &imc_entry))
247 {
248 imc_entry->rounds = rounds;
249 }
250 enumerator->destroy(enumerator);
251 }
252
253 METHOD(imv_test_state_t, another_round, bool,
254 private_imv_test_state_t *this, TNC_UInt32 imc_id)
255 {
256 enumerator_t *enumerator;
257 imc_entry_t *imc_entry;
258 bool not_finished = FALSE;
259
260 enumerator = this->imcs->create_enumerator(this->imcs);
261 while (enumerator->enumerate(enumerator, &imc_entry))
262 {
263 if (imc_entry->rounds > 0)
264 {
265 not_finished = TRUE;
266 }
267 if (imc_entry->imc_id == imc_id)
268 {
269 imc_entry->rounds--;
270 }
271 }
272 enumerator->destroy(enumerator);
273
274 return not_finished;
275 }
276
277 /**
278 * Described in header.
279 */
280 imv_state_t *imv_test_state_create(TNC_ConnectionID connection_id)
281 {
282 private_imv_test_state_t *this;
283
284 INIT(this,
285 .public = {
286 .interface = {
287 .get_connection_id = _get_connection_id,
288 .has_long = _has_long,
289 .has_excl = _has_excl,
290 .set_flags = _set_flags,
291 .set_max_msg_len = _set_max_msg_len,
292 .get_max_msg_len = _get_max_msg_len,
293 .change_state = _change_state,
294 .get_recommendation = _get_recommendation,
295 .set_recommendation = _set_recommendation,
296 .get_reason_string = _get_reason_string,
297 .get_remediation_instructions = _get_remediation_instructions,
298 .destroy = _destroy,
299 },
300 .add_imc = _add_imc,
301 .set_rounds = _set_rounds,
302 .another_round = _another_round,
303 },
304 .state = TNC_CONNECTION_STATE_CREATE,
305 .rec = TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION,
306 .eval = TNC_IMV_EVALUATION_RESULT_DONT_KNOW,
307 .connection_id = connection_id,
308 .imcs = linked_list_create(),
309 );
310
311 return &this->public.interface;
312 }
313
314