updated Copyright info
[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
18 #include <utils/lexparser.h>
19 #include <debug.h>
20
21 typedef struct private_imv_scanner_state_t private_imv_scanner_state_t;
22
23 /**
24 * Private data of an imv_scanner_state_t object.
25 */
26 struct private_imv_scanner_state_t {
27
28 /**
29 * Public members of imv_scanner_state_t
30 */
31 imv_scanner_state_t public;
32
33 /**
34 * TNCCS connection ID
35 */
36 TNC_ConnectionID connection_id;
37
38 /**
39 * TNCCS connection state
40 */
41 TNC_ConnectionState state;
42
43 /**
44 * Does the TNCCS connection support long message types?
45 */
46 bool has_long;
47
48 /**
49 * Does the TNCCS connection support exclusive delivery?
50 */
51 bool has_excl;
52
53 /**
54 * Maximum PA-TNC message size for this TNCCS connection
55 */
56 u_int32_t max_msg_len;
57
58 /**
59 * IMV action recommendation
60 */
61 TNC_IMV_Action_Recommendation rec;
62
63 /**
64 * IMV evaluation result
65 */
66 TNC_IMV_Evaluation_Result eval;
67
68 /**
69 * String with list of ports that should be closed
70 */
71 char *violating_ports;
72
73 /**
74 * Local copy of the reason string
75 */
76 chunk_t reason_string;
77 };
78
79 typedef struct entry_t entry_t;
80
81 /**
82 * Define an internal reason string entry
83 */
84 struct entry_t {
85 char *lang;
86 char *string;
87 };
88
89 /**
90 * Table of multi-lingual reason string entries
91 */
92 static entry_t reasons[] = {
93 { "en", "The following ports are open:" },
94 { "de", "Die folgenden Ports sind offen" },
95 { "fr", "Les ports suivants sont ouverts:" },
96 { "pl", "Następujące porty sa otwarte:" }
97 };
98
99 METHOD(imv_state_t, get_connection_id, TNC_ConnectionID,
100 private_imv_scanner_state_t *this)
101 {
102 return this->connection_id;
103 }
104
105 METHOD(imv_state_t, has_long, bool,
106 private_imv_scanner_state_t *this)
107 {
108 return this->has_long;
109 }
110
111 METHOD(imv_state_t, has_excl, bool,
112 private_imv_scanner_state_t *this)
113 {
114 return this->has_excl;
115 }
116
117 METHOD(imv_state_t, set_flags, void,
118 private_imv_scanner_state_t *this, bool has_long, bool has_excl)
119 {
120 this->has_long = has_long;
121 this->has_excl = has_excl;
122 }
123
124 METHOD(imv_state_t, set_max_msg_len, void,
125 private_imv_scanner_state_t *this, u_int32_t max_msg_len)
126 {
127 this->max_msg_len = max_msg_len;
128 }
129
130 METHOD(imv_state_t, get_max_msg_len, u_int32_t,
131 private_imv_scanner_state_t *this)
132 {
133 return this->max_msg_len;
134 }
135
136 METHOD(imv_state_t, change_state, void,
137 private_imv_scanner_state_t *this, TNC_ConnectionState new_state)
138 {
139 this->state = new_state;
140 }
141
142 METHOD(imv_state_t, get_recommendation, void,
143 private_imv_scanner_state_t *this, TNC_IMV_Action_Recommendation *rec,
144 TNC_IMV_Evaluation_Result *eval)
145 {
146 *rec = this->rec;
147 *eval = this->eval;
148 }
149
150 METHOD(imv_state_t, set_recommendation, void,
151 private_imv_scanner_state_t *this, TNC_IMV_Action_Recommendation rec,
152 TNC_IMV_Evaluation_Result eval)
153 {
154 this->rec = rec;
155 this->eval = eval;
156 }
157
158 METHOD(imv_state_t, get_reason_string, bool,
159 private_imv_scanner_state_t *this, chunk_t preferred_language,
160 chunk_t *reason_string, chunk_t *reason_language)
161 {
162 chunk_t pref_lang, lang;
163 u_char *pos;
164 int i;
165
166 if (!this->violating_ports)
167 {
168 return FALSE;
169 }
170
171 while (eat_whitespace(&preferred_language))
172 {
173 if (!extract_token(&pref_lang, ',', &preferred_language))
174 {
175 /* last entry in a comma-separated list or single entry */
176 pref_lang = preferred_language;
177 }
178
179 /* eat trailing whitespace */
180 pos = pref_lang.ptr + pref_lang.len - 1;
181 while (pref_lang.len && *pos-- == ' ')
182 {
183 pref_lang.len--;
184 }
185
186 for (i = 0 ; i < countof(reasons); i++)
187 {
188 lang = chunk_create(reasons[i].lang, strlen(reasons[i].lang));
189 if (chunk_equals(lang, pref_lang))
190 {
191 this->reason_string = chunk_cat("cc",
192 chunk_create(reasons[i].string,
193 strlen(reasons[i].string)),
194 chunk_create(this->violating_ports,
195 strlen(this->violating_ports)));
196 *reason_string = this->reason_string;
197 *reason_language = lang;
198 return TRUE;
199 }
200 }
201 }
202
203 /* no preferred language match found - use the default language */
204
205 this->reason_string = chunk_cat("cc",
206 chunk_create(reasons[0].string,
207 strlen(reasons[0].string)),
208 chunk_create(this->violating_ports,
209 strlen(this->violating_ports)));
210 *reason_string = this->reason_string;
211 *reason_language = chunk_create(reasons[0].lang,
212 strlen(reasons[0].lang));
213 return TRUE;
214 }
215
216 METHOD(imv_state_t, destroy, void,
217 private_imv_scanner_state_t *this)
218 {
219 free(this->violating_ports);
220 free(this->reason_string.ptr);
221 free(this);
222 }
223
224 METHOD(imv_scanner_state_t, set_violating_ports, void,
225 private_imv_scanner_state_t *this, char *ports)
226 {
227 this->violating_ports = strdup(ports);
228 }
229
230 /**
231 * Described in header.
232 */
233 imv_state_t *imv_scanner_state_create(TNC_ConnectionID connection_id)
234 {
235 private_imv_scanner_state_t *this;
236
237 INIT(this,
238 .public = {
239 .interface = {
240 .get_connection_id = _get_connection_id,
241 .has_long = _has_long,
242 .has_excl = _has_excl,
243 .set_flags = _set_flags,
244 .set_max_msg_len = _set_max_msg_len,
245 .get_max_msg_len = _get_max_msg_len,
246 .change_state = _change_state,
247 .get_recommendation = _get_recommendation,
248 .set_recommendation = _set_recommendation,
249 .get_reason_string = _get_reason_string,
250 .destroy = _destroy,
251 },
252 .set_violating_ports = _set_violating_ports,
253 },
254 .state = TNC_CONNECTION_STATE_CREATE,
255 .rec = TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION,
256 .eval = TNC_IMV_EVALUATION_RESULT_DONT_KNOW,
257 .connection_id = connection_id,
258 );
259
260 return &this->public.interface;
261 }
262
263