implemented IMC/IMV ReceiveMessageLong functions
[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.8.1 of TCG TNC IF-IMV Specification 1.3
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.8.2 of TCG TNC IF-IMV Specification 1.3
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 static TNC_Result receive_message(TNC_IMVID imv_id,
179 TNC_ConnectionID connection_id,
180 TNC_UInt32 msg_flags,
181 chunk_t msg,
182 TNC_VendorID msg_vid,
183 TNC_MessageSubtype msg_subtype,
184 TNC_UInt32 src_imc_id,
185 TNC_UInt32 dst_imv_id)
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, state, msg, msg_vid,
208 msg_subtype, src_imc_id, dst_imv_id, &pa_tnc_msg);
209
210 /* no parsed PA-TNC attributes available if an error occurred */
211 if (!pa_tnc_msg)
212 {
213 return result;
214 }
215
216 /* analyze PA-TNC attributes */
217 enumerator = pa_tnc_msg->create_attribute_enumerator(pa_tnc_msg);
218 while (enumerator->enumerate(enumerator, &attr))
219 {
220 if (attr->get_vendor_id(attr) != PEN_IETF)
221 {
222 continue;
223 }
224
225 if (attr->get_type(attr) == IETF_ATTR_PA_TNC_ERROR)
226 {
227 ietf_attr_pa_tnc_error_t *error_attr;
228 pa_tnc_error_code_t error_code;
229 chunk_t msg_info, attr_info;
230 u_int32_t offset;
231
232 error_attr = (ietf_attr_pa_tnc_error_t*)attr;
233 error_code = error_attr->get_error_code(error_attr);
234 msg_info = error_attr->get_msg_info(error_attr);
235 DBG1(DBG_IMV, "received PA-TNC error '%N' concerning message %#B",
236 pa_tnc_error_code_names, error_code, &msg_info);
237
238 switch (error_code)
239 {
240 case PA_ERROR_INVALID_PARAMETER:
241 offset = error_attr->get_offset(error_attr);
242 DBG1(DBG_IMV, " occurred at offset of %u bytes", offset);
243 break;
244 case PA_ERROR_ATTR_TYPE_NOT_SUPPORTED:
245 attr_info = error_attr->get_attr_info(error_attr);
246 DBG1(DBG_IMV, " unsupported attribute %#B", &attr_info);
247 break;
248 default:
249 break;
250 }
251 fatal_error = TRUE;
252 }
253 else if (attr->get_type(attr) == IETF_ATTR_PORT_FILTER)
254 {
255 ietf_attr_port_filter_t *attr_port_filter;
256 enumerator_t *enumerator;
257 u_int8_t protocol;
258 u_int16_t port;
259 char buf[BUF_LEN], *pos = buf;
260 size_t len = BUF_LEN;
261 bool blocked, compliant = TRUE;
262
263 attr_port_filter = (ietf_attr_port_filter_t*)attr;
264 enumerator = attr_port_filter->create_port_enumerator(attr_port_filter);
265 while (enumerator->enumerate(enumerator, &blocked, &protocol, &port))
266 {
267 enumerator_t *e;
268 port_range_t *port_range;
269 bool passed, found = FALSE;
270 int written = 0;
271
272 if (blocked)
273 {
274 /* ignore closed ports */
275 continue;
276 }
277
278 e = (protocol == IPPROTO_TCP) ?
279 tcp_ports->create_enumerator(tcp_ports) :
280 udp_ports->create_enumerator(udp_ports);
281 while (e->enumerate(e, &port_range))
282 {
283 if (port >= port_range->start && port <= port_range->stop)
284 {
285 found = TRUE;
286 break;
287 }
288 }
289 e->destroy(e);
290
291 passed = (closed_port_policy == found);
292 DBG2(DBG_IMV, "%s port %5u %s: %s",
293 (protocol == IPPROTO_TCP) ? "tcp" : "udp", port,
294 blocked ? "closed" : "open", passed ? "ok" : "fatal");
295 if (!passed)
296 {
297 compliant = FALSE;
298 written = snprintf(pos, len, " %s/%u",
299 (protocol == IPPROTO_TCP) ? "tcp" : "udp",
300 port);
301 if (written < 0 || written >= len)
302 {
303 break;
304 }
305 pos += written;
306 len -= written;
307 }
308 }
309 enumerator->destroy(enumerator);
310
311 if (compliant)
312 {
313 state->set_recommendation(state,
314 TNC_IMV_ACTION_RECOMMENDATION_ALLOW,
315 TNC_IMV_EVALUATION_RESULT_COMPLIANT);
316 }
317 else
318 {
319 imv_scanner_state_t *imv_scanner_state;
320
321 imv_scanner_state = (imv_scanner_state_t*)state;
322 imv_scanner_state->set_violating_ports(imv_scanner_state, buf);
323 state->set_recommendation(state,
324 TNC_IMV_ACTION_RECOMMENDATION_NO_ACCESS,
325 TNC_IMV_EVALUATION_RESULT_NONCOMPLIANT_MAJOR);
326 }
327 }
328 }
329 enumerator->destroy(enumerator);
330 pa_tnc_msg->destroy(pa_tnc_msg);
331
332 if (fatal_error)
333 {
334 state->set_recommendation(state,
335 TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION,
336 TNC_IMV_EVALUATION_RESULT_ERROR);
337 return imv_scanner->provide_recommendation(imv_scanner, connection_id);
338 }
339
340 return imv_scanner->provide_recommendation(imv_scanner, connection_id);
341 }
342
343 /**
344 * see section 3.8.4 of TCG TNC IF-IMV Specification 1.3
345 */
346 TNC_Result TNC_IMV_ReceiveMessage(TNC_IMVID imv_id,
347 TNC_ConnectionID connection_id,
348 TNC_BufferReference msg,
349 TNC_UInt32 msg_len,
350 TNC_MessageType msg_type)
351 {
352 TNC_VendorID msg_vid;
353 TNC_MessageSubtype msg_subtype;
354
355 msg_vid = msg_type >> 8;
356 msg_subtype = msg_type & TNC_SUBTYPE_ANY;
357
358 return receive_message(imv_id, connection_id, 0, chunk_create(msg, msg_len),
359 msg_vid, msg_subtype, 0, TNC_IMVID_ANY);
360 }
361
362 /**
363 * see section 3.8.6 of TCG TNC IF-IMV Specification 1.3
364 */
365 TNC_Result TNC_IMV_ReceiveMessageLong(TNC_IMVID imv_id,
366 TNC_ConnectionID connection_id,
367 TNC_UInt32 msg_flags,
368 TNC_BufferReference msg,
369 TNC_UInt32 msg_len,
370 TNC_VendorID msg_vid,
371 TNC_MessageSubtype msg_subtype,
372 TNC_UInt32 src_imc_id,
373 TNC_UInt32 dst_imv_id)
374 {
375 return receive_message(imv_id, connection_id, msg_flags,
376 chunk_create(msg, msg_len), msg_vid, msg_subtype,
377 src_imc_id, dst_imv_id);
378 }
379
380 /**
381 * see section 3.8.7 of TCG TNC IF-IMV Specification 1.3
382 */
383 TNC_Result TNC_IMV_SolicitRecommendation(TNC_IMVID imv_id,
384 TNC_ConnectionID connection_id)
385 {
386 if (!imv_scanner)
387 {
388 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
389 return TNC_RESULT_NOT_INITIALIZED;
390 }
391 return imv_scanner->provide_recommendation(imv_scanner, connection_id);
392 }
393
394 /**
395 * see section 3.8.8 of TCG TNC IF-IMV Specification 1.3
396 */
397 TNC_Result TNC_IMV_BatchEnding(TNC_IMVID imv_id,
398 TNC_ConnectionID connection_id)
399 {
400 if (!imv_scanner)
401 {
402 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
403 return TNC_RESULT_NOT_INITIALIZED;
404 }
405 return TNC_RESULT_SUCCESS;
406 }
407
408 /**
409 * see section 3.8.9 of TCG TNC IF-IMV Specification 1.3
410 */
411 TNC_Result TNC_IMV_Terminate(TNC_IMVID imv_id)
412 {
413 if (!imv_scanner)
414 {
415 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
416 return TNC_RESULT_NOT_INITIALIZED;
417 }
418 tcp_ports->destroy_function(tcp_ports, free);
419 udp_ports->destroy_function(udp_ports, free);
420 imv_scanner->destroy(imv_scanner);
421 imv_scanner = NULL;
422
423 return TNC_RESULT_SUCCESS;
424 }
425
426 /**
427 * see section 4.2.8.1 of TCG TNC IF-IMV Specification 1.3
428 */
429 TNC_Result TNC_IMV_ProvideBindFunction(TNC_IMVID imv_id,
430 TNC_TNCS_BindFunctionPointer bind_function)
431 {
432 if (!imv_scanner)
433 {
434 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
435 return TNC_RESULT_NOT_INITIALIZED;
436 }
437 return imv_scanner->bind_functions(imv_scanner, bind_function);
438 }