Separated IMV session management from IMV policy database
[strongswan.git] / src / libimcv / plugins / imv_test / imv_test_state.c
1 /*
2 * Copyright (C) 2011-2013 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 #include "imv/imv_lang_string.h"
18 #include "imv/imv_reason_string.h"
19
20 #include <tncif_policy.h>
21
22 #include <utils/lexparser.h>
23 #include <collections/linked_list.h>
24 #include <utils/debug.h>
25
26 typedef struct private_imv_test_state_t private_imv_test_state_t;
27
28 /**
29 * Private data of an imv_test_state_t object.
30 */
31 struct private_imv_test_state_t {
32
33 /**
34 * Public members of imv_test_state_t
35 */
36 imv_test_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 * IMV database session associated with TNCCS connection
65 */
66 imv_session_t *session;
67
68 /**
69 * IMV action recommendation
70 */
71 TNC_IMV_Action_Recommendation rec;
72
73 /**
74 * IMV evaluation result
75 */
76 TNC_IMV_Evaluation_Result eval;
77
78 /**
79 * TNC Reason String
80 */
81 imv_reason_string_t *reason_string;
82
83 /**
84 * List of IMCs
85 */
86 linked_list_t *imcs;
87
88 };
89
90 typedef struct imc_entry_t imc_entry_t;
91
92 /**
93 * Define an internal IMC entry
94 */
95 struct imc_entry_t {
96 TNC_UInt32 imc_id;
97 int rounds;
98 };
99
100 /**
101 * Supported languages
102 */
103 static char* languages[] = { "en", "de", "fr", "pl" };
104
105 /**
106 * Table of reason strings
107 */
108 static imv_lang_string_t reasons[] = {
109 { "en", "IMC Test was not configured with \"command = allow\"" },
110 { "de", "IMC Test wurde nicht mit \"command = allow\" konfiguriert" },
111 { "fr", "IMC Test n'etait pas configuré avec \"command = allow\"" },
112 { "pl", "IMC Test nie zostało skonfigurowany z \"command = allow\"" },
113 { NULL, NULL }
114 };
115
116 METHOD(imv_state_t, get_connection_id, TNC_ConnectionID,
117 private_imv_test_state_t *this)
118 {
119 return this->connection_id;
120 }
121
122 METHOD(imv_state_t, has_long, bool,
123 private_imv_test_state_t *this)
124 {
125 return this->has_long;
126 }
127
128 METHOD(imv_state_t, has_excl, bool,
129 private_imv_test_state_t *this)
130 {
131 return this->has_excl;
132 }
133
134 METHOD(imv_state_t, set_flags, void,
135 private_imv_test_state_t *this, bool has_long, bool has_excl)
136 {
137 this->has_long = has_long;
138 this->has_excl = has_excl;
139 }
140
141 METHOD(imv_state_t, set_max_msg_len, void,
142 private_imv_test_state_t *this, uint32_t max_msg_len)
143 {
144 this->max_msg_len = max_msg_len;
145 }
146
147 METHOD(imv_state_t, get_max_msg_len, uint32_t,
148 private_imv_test_state_t *this)
149 {
150 return this->max_msg_len;
151 }
152
153 METHOD(imv_state_t, set_session, void,
154 private_imv_test_state_t *this, imv_session_t *session)
155 {
156 this->session = session;
157 }
158
159 METHOD(imv_state_t, get_session, imv_session_t*,
160 private_imv_test_state_t *this)
161 {
162 return this->session;
163 }
164
165 METHOD(imv_state_t, change_state, void,
166 private_imv_test_state_t *this, TNC_ConnectionState new_state)
167 {
168 this->state = new_state;
169 }
170
171 METHOD(imv_state_t, get_recommendation, void,
172 private_imv_test_state_t *this, TNC_IMV_Action_Recommendation *rec,
173 TNC_IMV_Evaluation_Result *eval)
174 {
175 *rec = this->rec;
176 *eval = this->eval;
177 }
178
179 METHOD(imv_state_t, set_recommendation, void,
180 private_imv_test_state_t *this, TNC_IMV_Action_Recommendation rec,
181 TNC_IMV_Evaluation_Result eval)
182 {
183 this->rec = rec;
184 this->eval = eval;
185 }
186
187 METHOD(imv_state_t, update_recommendation, void,
188 private_imv_test_state_t *this, TNC_IMV_Action_Recommendation rec,
189 TNC_IMV_Evaluation_Result eval)
190 {
191 this->rec = tncif_policy_update_recommendation(this->rec, rec);
192 this->eval = tncif_policy_update_evaluation(this->eval, eval);
193 }
194
195 METHOD(imv_state_t, get_reason_string, bool,
196 private_imv_test_state_t *this, enumerator_t *language_enumerator,
197 chunk_t *reason_string, char **reason_language)
198 {
199 *reason_language = imv_lang_string_select_lang(language_enumerator,
200 languages, countof(languages));
201
202 /* Instantiate a TNC Reason String object */
203 DESTROY_IF(this->reason_string);
204 this->reason_string = imv_reason_string_create(*reason_language, "\n");
205 this->reason_string->add_reason(this->reason_string, reasons);
206 *reason_string = this->reason_string->get_encoding(this->reason_string);
207
208 return TRUE;
209 }
210
211 METHOD(imv_state_t, get_remediation_instructions, bool,
212 private_imv_test_state_t *this, enumerator_t *language_enumerator,
213 chunk_t *string, char **lang_code, char **uri)
214 {
215 return FALSE;
216 }
217
218 METHOD(imv_state_t, destroy, void,
219 private_imv_test_state_t *this)
220 {
221 DESTROY_IF(this->session);
222 DESTROY_IF(this->reason_string);
223 this->imcs->destroy_function(this->imcs, free);
224 free(this);
225 }
226
227 METHOD(imv_test_state_t, add_imc, void,
228 private_imv_test_state_t *this, TNC_UInt32 imc_id, int rounds)
229 {
230 enumerator_t *enumerator;
231 imc_entry_t *imc_entry;
232 bool found = FALSE;
233
234 enumerator = this->imcs->create_enumerator(this->imcs);
235 while (enumerator->enumerate(enumerator, &imc_entry))
236 {
237 if (imc_entry->imc_id == imc_id)
238 {
239 found = TRUE;
240 break;
241 }
242 }
243 enumerator->destroy(enumerator);
244
245 if (!found)
246 {
247 imc_entry = malloc_thing(imc_entry_t);
248 imc_entry->imc_id = imc_id;
249 imc_entry->rounds = rounds;
250 this->imcs->insert_last(this->imcs, imc_entry);
251 }
252 }
253
254 METHOD(imv_test_state_t, set_rounds, void,
255 private_imv_test_state_t *this, int rounds)
256 {
257 enumerator_t *enumerator;
258 imc_entry_t *imc_entry;
259
260 enumerator = this->imcs->create_enumerator(this->imcs);
261 while (enumerator->enumerate(enumerator, &imc_entry))
262 {
263 imc_entry->rounds = rounds;
264 }
265 enumerator->destroy(enumerator);
266 }
267
268 METHOD(imv_test_state_t, another_round, bool,
269 private_imv_test_state_t *this, TNC_UInt32 imc_id)
270 {
271 enumerator_t *enumerator;
272 imc_entry_t *imc_entry;
273 bool not_finished = FALSE;
274
275 enumerator = this->imcs->create_enumerator(this->imcs);
276 while (enumerator->enumerate(enumerator, &imc_entry))
277 {
278 if (imc_entry->rounds > 0)
279 {
280 not_finished = TRUE;
281 }
282 if (imc_entry->imc_id == imc_id)
283 {
284 imc_entry->rounds--;
285 }
286 }
287 enumerator->destroy(enumerator);
288
289 return not_finished;
290 }
291
292 /**
293 * Described in header.
294 */
295 imv_state_t *imv_test_state_create(TNC_ConnectionID connection_id)
296 {
297 private_imv_test_state_t *this;
298
299 INIT(this,
300 .public = {
301 .interface = {
302 .get_connection_id = _get_connection_id,
303 .has_long = _has_long,
304 .has_excl = _has_excl,
305 .set_flags = _set_flags,
306 .set_max_msg_len = _set_max_msg_len,
307 .get_max_msg_len = _get_max_msg_len,
308 .set_session = _set_session,
309 .get_session = _get_session,
310 .change_state = _change_state,
311 .get_recommendation = _get_recommendation,
312 .set_recommendation = _set_recommendation,
313 .update_recommendation = _update_recommendation,
314 .get_reason_string = _get_reason_string,
315 .get_remediation_instructions = _get_remediation_instructions,
316 .destroy = _destroy,
317 },
318 .add_imc = _add_imc,
319 .set_rounds = _set_rounds,
320 .another_round = _another_round,
321 },
322 .state = TNC_CONNECTION_STATE_CREATE,
323 .rec = TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION,
324 .eval = TNC_IMV_EVALUATION_RESULT_DONT_KNOW,
325 .connection_id = connection_id,
326 .imcs = linked_list_create(),
327 );
328
329 return &this->public.interface;
330 }
331
332