support of error_offset in PA-TNC INVALID PARAMETER error messages
[strongswan.git] / src / libimcv / plugins / imv_scanner / imv_scanner.c
1 /*
2 * Copyright (C) 2011 Andreas Steffen, HSR Hochschule fuer Technik Rapperswil
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * for more details.
13 */
14
15 #include "imv_scanner_state.h"
16
17 #include <imv/imv_agent.h>
18 #include <pa_tnc/pa_tnc_msg.h>
19 #include <ietf/ietf_attr.h>
20 #include <ietf/ietf_attr_pa_tnc_error.h>
21 #include <ietf/ietf_attr_port_filter.h>
22
23 #include <tncif_names.h>
24 #include <tncif_pa_subtypes.h>
25
26 #include <pen/pen.h>
27 #include <utils/linked_list.h>
28 #include <utils/lexparser.h>
29 #include <debug.h>
30
31 /* IMV definitions */
32
33 static const char imv_name[] = "Scanner";
34
35 #define IMV_VENDOR_ID PEN_ITA
36 #define IMV_SUBTYPE PA_SUBTYPE_ITA_SCANNER
37
38 static imv_agent_t *imv_scanner;
39
40 typedef struct port_range_t port_range_t;
41
42 struct port_range_t {
43 u_int16_t start, stop;
44 };
45
46
47 /**
48 * Default port policy
49 *
50 * TRUE: all server ports on the TNC client must be closed
51 * FALSE: any server port on the TNC client is allowed to be open
52 */
53 static bool closed_port_policy = TRUE;
54
55 /**
56 * List of TCP and UDP port ranges
57 *
58 * TRUE: server ports on the TNC client that are allowed to be open
59 * FALSE: server ports on the TNC client that must be closed
60 */
61 static linked_list_t *tcp_ports, *udp_ports;
62
63 /**
64 * Get a TCP or UDP port list from strongswan.conf
65 */
66 static linked_list_t* get_port_list(char *label)
67 {
68 char key[40], *value;
69 linked_list_t *list;
70 chunk_t port_list, port_item, port_stop;
71 port_range_t *port_range;
72
73 list = linked_list_create();
74
75 snprintf(key, sizeof(key), "libimcv.plugins.imv-scanner.%s_ports", label);
76 value = lib->settings->get_str(lib->settings, key, NULL);
77 if (!value)
78 {
79 DBG1(DBG_IMV, "%s not defined", key);
80 return list;
81 }
82 port_list = chunk_create(value, strlen(value));
83 DBG2(DBG_IMV, "list of %s ports that %s:", label,
84 closed_port_policy ? "are allowed to be open" : "must be closed");
85
86 while (eat_whitespace(&port_list))
87 {
88 if (!extract_token(&port_item, ' ', &port_list))
89 {
90 /* reached last port item */
91 port_item = port_list;
92 port_list = chunk_empty;
93 }
94 port_range = malloc_thing(port_range_t);
95 port_range->start = atoi(port_item.ptr);;
96
97 if (extract_token(&port_stop, '-', &port_item))
98 {
99 port_range->stop = atoi(port_stop.ptr);
100 }
101 else
102 {
103 port_range->stop = port_range->start;
104 }
105 DBG2(DBG_IMV, "%5u - %5u", port_range->start, port_range->stop);
106 list->insert_last(list, port_range);
107 }
108
109 return list;
110 }
111
112
113 /*
114 * see section 3.7.1 of TCG TNC IF-IMV Specification 1.2
115 */
116 TNC_Result TNC_IMV_Initialize(TNC_IMVID imv_id,
117 TNC_Version min_version,
118 TNC_Version max_version,
119 TNC_Version *actual_version)
120 {
121 if (imv_scanner)
122 {
123 DBG1(DBG_IMV, "IMV \"%s\" has already been initialized", imv_name);
124 return TNC_RESULT_ALREADY_INITIALIZED;
125 }
126 imv_scanner = imv_agent_create(imv_name, IMV_VENDOR_ID, IMV_SUBTYPE,
127 imv_id, actual_version);
128 if (!imv_scanner)
129 {
130 return TNC_RESULT_FATAL;
131 }
132 if (min_version > TNC_IFIMV_VERSION_1 || max_version < TNC_IFIMV_VERSION_1)
133 {
134 DBG1(DBG_IMV, "no common IF-IMV version");
135 return TNC_RESULT_NO_COMMON_VERSION;
136 }
137
138 /* set the default port policy to closed (TRUE) or open (FALSE) */
139 closed_port_policy = lib->settings->get_bool(lib->settings,
140 "libimcv.plugins.imv-scanner.closed_port_policy", TRUE);
141 DBG2(DBG_IMV, "default port policy is %s ports",
142 closed_port_policy ? "closed" : "open");
143
144 /* get the list of open|closed ports */
145 tcp_ports = get_port_list("tcp");
146 udp_ports = get_port_list("udp");
147
148 return TNC_RESULT_SUCCESS;
149 }
150
151 /**
152 * see section 3.7.2 of TCG TNC IF-IMV Specification 1.2
153 */
154 TNC_Result TNC_IMV_NotifyConnectionChange(TNC_IMVID imv_id,
155 TNC_ConnectionID connection_id,
156 TNC_ConnectionState new_state)
157 {
158 imv_state_t *state;
159
160 if (!imv_scanner)
161 {
162 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
163 return TNC_RESULT_NOT_INITIALIZED;
164 }
165 switch (new_state)
166 {
167 case TNC_CONNECTION_STATE_CREATE:
168 state = imv_scanner_state_create(connection_id);
169 return imv_scanner->create_state(imv_scanner, state);
170 case TNC_CONNECTION_STATE_DELETE:
171 return imv_scanner->delete_state(imv_scanner, connection_id);
172 default:
173 return imv_scanner->change_state(imv_scanner, connection_id,
174 new_state, NULL);
175 }
176 }
177
178 /**
179 * see section 3.7.3 of TCG TNC IF-IMV Specification 1.2
180 */
181 TNC_Result TNC_IMV_ReceiveMessage(TNC_IMVID imv_id,
182 TNC_ConnectionID connection_id,
183 TNC_BufferReference msg,
184 TNC_UInt32 msg_len,
185 TNC_MessageType msg_type)
186 {
187 pa_tnc_msg_t *pa_tnc_msg;
188 pa_tnc_attr_t *attr;
189 imv_state_t *state;
190 enumerator_t *enumerator;
191 TNC_Result result;
192 bool fatal_error = FALSE;
193
194 if (!imv_scanner)
195 {
196 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
197 return TNC_RESULT_NOT_INITIALIZED;
198 }
199
200 /* get current IMV state */
201 if (!imv_scanner->get_state(imv_scanner, connection_id, &state))
202 {
203 return TNC_RESULT_FATAL;
204 }
205
206 /* parse received PA-TNC message and automatically handle any errors */
207 result = imv_scanner->receive_message(imv_scanner, connection_id,
208 chunk_create(msg, msg_len), msg_type,
209 &pa_tnc_msg);
210
211 /* no parsed PA-TNC attributes available if an error occurred */
212 if (!pa_tnc_msg)
213 {
214 return result;
215 }
216
217 /* analyze PA-TNC attributes */
218 enumerator = pa_tnc_msg->create_attribute_enumerator(pa_tnc_msg);
219 while (enumerator->enumerate(enumerator, &attr))
220 {
221 if (attr->get_vendor_id(attr) != PEN_IETF)
222 {
223 continue;
224 }
225
226 if (attr->get_type(attr) == IETF_ATTR_PA_TNC_ERROR)
227 {
228 ietf_attr_pa_tnc_error_t *error_attr;
229 pa_tnc_error_code_t error_code;
230 chunk_t msg_info, attr_info;
231 u_int32_t offset;
232
233 error_attr = (ietf_attr_pa_tnc_error_t*)attr;
234 error_code = error_attr->get_error_code(error_attr);
235 msg_info = error_attr->get_msg_info(error_attr);
236 DBG1(DBG_IMV, "received PA-TNC error '%N' concerning message %#B",
237 pa_tnc_error_code_names, error_code, &msg_info);
238
239 switch (error_code)
240 {
241 case PA_ERROR_INVALID_PARAMETER:
242 offset = error_attr->get_offset(error_attr);
243 DBG1(DBG_IMV, " occurred at offset of %u bytes", offset);
244 break;
245 case PA_ERROR_ATTR_TYPE_NOT_SUPPORTED:
246 attr_info = error_attr->get_attr_info(error_attr);
247 DBG1(DBG_IMV, " unsupported attribute %#B", &attr_info);
248 break;
249 default:
250 break;
251 }
252 fatal_error = TRUE;
253 }
254 else if (attr->get_type(attr) == IETF_ATTR_PORT_FILTER)
255 {
256 ietf_attr_port_filter_t *attr_port_filter;
257 enumerator_t *enumerator;
258 u_int8_t protocol;
259 u_int16_t port;
260 char buf[BUF_LEN], *pos = buf;
261 size_t len = BUF_LEN;
262 bool blocked, compliant = TRUE;
263
264 attr_port_filter = (ietf_attr_port_filter_t*)attr;
265 enumerator = attr_port_filter->create_port_enumerator(attr_port_filter);
266 while (enumerator->enumerate(enumerator, &blocked, &protocol, &port))
267 {
268 enumerator_t *e;
269 port_range_t *port_range;
270 bool passed, found = FALSE;
271 int written = 0;
272
273 if (blocked)
274 {
275 /* ignore closed ports */
276 continue;
277 }
278
279 e = (protocol == IPPROTO_TCP) ?
280 tcp_ports->create_enumerator(tcp_ports) :
281 udp_ports->create_enumerator(udp_ports);
282 while (e->enumerate(e, &port_range))
283 {
284 if (port >= port_range->start && port <= port_range->stop)
285 {
286 found = TRUE;
287 break;
288 }
289 }
290 e->destroy(e);
291
292 passed = (closed_port_policy == found);
293 DBG2(DBG_IMV, "%s port %5u %s: %s",
294 (protocol == IPPROTO_TCP) ? "tcp" : "udp", port,
295 blocked ? "closed" : "open", passed ? "ok" : "fatal");
296 if (!passed)
297 {
298 compliant = FALSE;
299 written = snprintf(pos, len, " %s/%u",
300 (protocol == IPPROTO_TCP) ? "tcp" : "udp",
301 port);
302 if (written < 0 || written >= len)
303 {
304 break;
305 }
306 pos += written;
307 len -= written;
308 }
309 }
310 enumerator->destroy(enumerator);
311
312 if (compliant)
313 {
314 state->set_recommendation(state,
315 TNC_IMV_ACTION_RECOMMENDATION_ALLOW,
316 TNC_IMV_EVALUATION_RESULT_COMPLIANT);
317 }
318 else
319 {
320 imv_scanner_state_t *imv_scanner_state;
321
322 imv_scanner_state = (imv_scanner_state_t*)state;
323 imv_scanner_state->set_violating_ports(imv_scanner_state, buf);
324 state->set_recommendation(state,
325 TNC_IMV_ACTION_RECOMMENDATION_NO_ACCESS,
326 TNC_IMV_EVALUATION_RESULT_NONCOMPLIANT_MAJOR);
327 }
328 }
329 }
330 enumerator->destroy(enumerator);
331 pa_tnc_msg->destroy(pa_tnc_msg);
332
333 if (fatal_error)
334 {
335 state->set_recommendation(state,
336 TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION,
337 TNC_IMV_EVALUATION_RESULT_ERROR);
338 return imv_scanner->provide_recommendation(imv_scanner, connection_id);
339 }
340
341 return imv_scanner->provide_recommendation(imv_scanner, connection_id);
342 }
343
344 /**
345 * see section 3.7.4 of TCG TNC IF-IMV Specification 1.2
346 */
347 TNC_Result TNC_IMV_SolicitRecommendation(TNC_IMVID imv_id,
348 TNC_ConnectionID connection_id)
349 {
350 if (!imv_scanner)
351 {
352 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
353 return TNC_RESULT_NOT_INITIALIZED;
354 }
355 return imv_scanner->provide_recommendation(imv_scanner, connection_id);
356 }
357
358 /**
359 * see section 3.7.5 of TCG TNC IF-IMV Specification 1.2
360 */
361 TNC_Result TNC_IMV_BatchEnding(TNC_IMVID imv_id,
362 TNC_ConnectionID connection_id)
363 {
364 if (!imv_scanner)
365 {
366 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
367 return TNC_RESULT_NOT_INITIALIZED;
368 }
369 return TNC_RESULT_SUCCESS;
370 }
371
372 /**
373 * see section 3.7.6 of TCG TNC IF-IMV Specification 1.2
374 */
375 TNC_Result TNC_IMV_Terminate(TNC_IMVID imv_id)
376 {
377 if (!imv_scanner)
378 {
379 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
380 return TNC_RESULT_NOT_INITIALIZED;
381 }
382 tcp_ports->destroy_function(tcp_ports, free);
383 udp_ports->destroy_function(udp_ports, free);
384 imv_scanner->destroy(imv_scanner);
385 imv_scanner = NULL;
386
387 return TNC_RESULT_SUCCESS;
388 }
389
390 /**
391 * see section 4.2.8.1 of TCG TNC IF-IMV Specification 1.2
392 */
393 TNC_Result TNC_IMV_ProvideBindFunction(TNC_IMVID imv_id,
394 TNC_TNCS_BindFunctionPointer bind_function)
395 {
396 if (!imv_scanner)
397 {
398 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
399 return TNC_RESULT_NOT_INITIALIZED;
400 }
401 return imv_scanner->bind_functions(imv_scanner, bind_function);
402 }