16ce0863f0f31c1e0ed7570ac609a07c75ba98eb
[strongswan.git] / src / libimcv / plugins / imv_scanner / imv_scanner.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 <imv/imv_agent.h>
19 #include <imv/imv_msg.h>
20 #include <ietf/ietf_attr.h>
21 #include <ietf/ietf_attr_attr_request.h>
22 #include <ietf/ietf_attr_pa_tnc_error.h>
23 #include <ietf/ietf_attr_port_filter.h>
24
25 #include <tncif_names.h>
26 #include <tncif_pa_subtypes.h>
27
28 #include <pen/pen.h>
29 #include <collections/linked_list.h>
30 #include <utils/lexparser.h>
31 #include <utils/debug.h>
32
33 /* IMV definitions */
34
35 static const char imv_name[] = "Scanner";
36
37 static pen_type_t msg_types[] = {
38 { PEN_IETF, PA_SUBTYPE_IETF_VPN }
39 };
40
41 static imv_agent_t *imv_scanner;
42
43 typedef struct port_range_t port_range_t;
44
45 struct port_range_t {
46 u_int16_t start, stop;
47 };
48
49
50 /**
51 * Default port policy
52 *
53 * TRUE: all server ports on the TNC client must be closed
54 * FALSE: any server port on the TNC client is allowed to be open
55 */
56 static bool closed_port_policy = TRUE;
57
58 /**
59 * List of TCP and UDP port ranges
60 *
61 * TRUE: server ports on the TNC client that are allowed to be open
62 * FALSE: server ports on the TNC client that must be closed
63 */
64 static linked_list_t *tcp_ports, *udp_ports;
65
66 /**
67 * Get a TCP or UDP port list from strongswan.conf
68 */
69 static linked_list_t* get_port_list(char *label)
70 {
71 char key[40], *value;
72 linked_list_t *list;
73 chunk_t port_list, port_item, port_start;
74 port_range_t *port_range;
75
76 list = linked_list_create();
77
78 snprintf(key, sizeof(key), "libimcv.plugins.imv-scanner.%s_ports", label);
79 value = lib->settings->get_str(lib->settings, key, NULL);
80 if (!value)
81 {
82 DBG1(DBG_IMV, "%s not defined", key);
83 return list;
84 }
85 port_list = chunk_create(value, strlen(value));
86 DBG2(DBG_IMV, "list of %s ports that %s:", label,
87 closed_port_policy ? "are allowed to be open" : "must be closed");
88
89 while (eat_whitespace(&port_list))
90 {
91 if (!extract_token(&port_item, ' ', &port_list))
92 {
93 /* reached last port item */
94 port_item = port_list;
95 port_list = chunk_empty;
96 }
97 port_range = malloc_thing(port_range_t);
98 port_range->start = atoi(port_item.ptr);
99
100 if (extract_token(&port_start, '-', &port_item) && port_item.len)
101 {
102 port_range->stop = atoi(port_item.ptr);
103 }
104 else
105 {
106 port_range->stop = port_range->start;
107 }
108 DBG2(DBG_IMV, "%5u - %5u", port_range->start, port_range->stop);
109 list->insert_last(list, port_range);
110 }
111
112 return list;
113 }
114
115
116 /*
117 * see section 3.8.1 of TCG TNC IF-IMV Specification 1.3
118 */
119 TNC_Result TNC_IMV_Initialize(TNC_IMVID imv_id,
120 TNC_Version min_version,
121 TNC_Version max_version,
122 TNC_Version *actual_version)
123 {
124 if (imv_scanner)
125 {
126 DBG1(DBG_IMV, "IMV \"%s\" has already been initialized", imv_name);
127 return TNC_RESULT_ALREADY_INITIALIZED;
128 }
129 imv_scanner = imv_agent_create(imv_name, msg_types, countof(msg_types),
130 imv_id, actual_version);
131 if (!imv_scanner)
132 {
133 return TNC_RESULT_FATAL;
134 }
135 if (min_version > TNC_IFIMV_VERSION_1 || max_version < TNC_IFIMV_VERSION_1)
136 {
137 DBG1(DBG_IMV, "no common IF-IMV version");
138 return TNC_RESULT_NO_COMMON_VERSION;
139 }
140
141 /* set the default port policy to closed (TRUE) or open (FALSE) */
142 closed_port_policy = lib->settings->get_bool(lib->settings,
143 "libimcv.plugins.imv-scanner.closed_port_policy", TRUE);
144 DBG2(DBG_IMV, "default port policy is %s ports",
145 closed_port_policy ? "closed" : "open");
146
147 /* get the list of open|closed ports */
148 tcp_ports = get_port_list("tcp");
149 udp_ports = get_port_list("udp");
150
151 return TNC_RESULT_SUCCESS;
152 }
153
154 /**
155 * see section 3.8.2 of TCG TNC IF-IMV Specification 1.3
156 */
157 TNC_Result TNC_IMV_NotifyConnectionChange(TNC_IMVID imv_id,
158 TNC_ConnectionID connection_id,
159 TNC_ConnectionState new_state)
160 {
161 imv_state_t *state;
162
163 if (!imv_scanner)
164 {
165 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
166 return TNC_RESULT_NOT_INITIALIZED;
167 }
168 switch (new_state)
169 {
170 case TNC_CONNECTION_STATE_CREATE:
171 state = imv_scanner_state_create(connection_id);
172 return imv_scanner->create_state(imv_scanner, state);
173 case TNC_CONNECTION_STATE_DELETE:
174 return imv_scanner->delete_state(imv_scanner, connection_id);
175 default:
176 return imv_scanner->change_state(imv_scanner, connection_id,
177 new_state, NULL);
178 }
179 }
180
181 static TNC_Result receive_message(imv_state_t *state, imv_msg_t *in_msg)
182 {
183 imv_msg_t *out_msg;
184 enumerator_t *enumerator;
185 pa_tnc_attr_t *attr;
186 pen_type_t type;
187 TNC_Result result;
188 bool fatal_error = FALSE;
189
190 /* parse received PA-TNC message and handle local and remote errors */
191 result = in_msg->receive(in_msg, &fatal_error);
192 if (result != TNC_RESULT_SUCCESS)
193 {
194 return result;
195 }
196
197 /* analyze PA-TNC attributes */
198 enumerator = in_msg->create_attribute_enumerator(in_msg);
199 while (enumerator->enumerate(enumerator, &attr))
200 {
201 type = attr->get_type(attr);
202
203 if (type.vendor_id == PEN_IETF && type.type == IETF_ATTR_PORT_FILTER)
204 {
205 imv_scanner_state_t *imv_scanner_state;
206 ietf_attr_port_filter_t *attr_port_filter;
207 enumerator_t *enumerator;
208 u_int8_t protocol;
209 u_int16_t port;
210 bool blocked, compliant = TRUE;
211
212
213 imv_scanner_state = (imv_scanner_state_t*)state;
214 attr_port_filter = (ietf_attr_port_filter_t*)attr;
215 enumerator = attr_port_filter->create_port_enumerator(attr_port_filter);
216 while (enumerator->enumerate(enumerator, &blocked, &protocol, &port))
217 {
218 enumerator_t *e;
219 port_range_t *port_range;
220 bool passed, found = FALSE;
221 char buf[20];
222
223 if (blocked)
224 {
225 /* ignore closed ports */
226 continue;
227 }
228
229 e = (protocol == IPPROTO_TCP) ?
230 tcp_ports->create_enumerator(tcp_ports) :
231 udp_ports->create_enumerator(udp_ports);
232 while (e->enumerate(e, &port_range))
233 {
234 if (port >= port_range->start && port <= port_range->stop)
235 {
236 found = TRUE;
237 break;
238 }
239 }
240 e->destroy(e);
241
242 passed = (closed_port_policy == found);
243 DBG2(DBG_IMV, "%s port %5u %s: %s",
244 (protocol == IPPROTO_TCP) ? "tcp" : "udp", port,
245 blocked ? "closed" : "open", passed ? "ok" : "fatal");
246 if (!passed)
247 {
248 compliant = FALSE;
249 snprintf(buf, sizeof(buf), "%s/%u",
250 (protocol == IPPROTO_TCP) ? "tcp" : "udp", port);
251 imv_scanner_state->add_violating_port(imv_scanner_state,
252 strdup(buf));
253 }
254 }
255 enumerator->destroy(enumerator);
256
257 if (compliant)
258 {
259 state->set_recommendation(state,
260 TNC_IMV_ACTION_RECOMMENDATION_ALLOW,
261 TNC_IMV_EVALUATION_RESULT_COMPLIANT);
262 }
263 else
264 {
265 state->set_recommendation(state,
266 TNC_IMV_ACTION_RECOMMENDATION_NO_ACCESS,
267 TNC_IMV_EVALUATION_RESULT_NONCOMPLIANT_MAJOR);
268 }
269 }
270 }
271 enumerator->destroy(enumerator);
272
273 if (fatal_error)
274 {
275 state->set_recommendation(state,
276 TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION,
277 TNC_IMV_EVALUATION_RESULT_ERROR);
278 }
279
280 out_msg = imv_msg_create_as_reply(in_msg);
281 result = out_msg->send_assessment(out_msg);
282 out_msg->destroy(out_msg);
283 if (result != TNC_RESULT_SUCCESS)
284 {
285 return result;
286 }
287 return imv_scanner->provide_recommendation(imv_scanner, state);
288 }
289
290 /**
291 * see section 3.8.4 of TCG TNC IF-IMV Specification 1.3
292 */
293 TNC_Result TNC_IMV_ReceiveMessage(TNC_IMVID imv_id,
294 TNC_ConnectionID connection_id,
295 TNC_BufferReference msg,
296 TNC_UInt32 msg_len,
297 TNC_MessageType msg_type)
298 {
299 imv_state_t *state;
300 imv_msg_t *in_msg;
301 TNC_Result result;
302
303 if (!imv_scanner)
304 {
305 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
306 return TNC_RESULT_NOT_INITIALIZED;
307 }
308 if (!imv_scanner->get_state(imv_scanner, connection_id, &state))
309 {
310 return TNC_RESULT_FATAL;
311 }
312
313 in_msg = imv_msg_create_from_data(imv_scanner, state, connection_id, msg_type,
314 chunk_create(msg, msg_len));
315 result = receive_message(state, in_msg);
316 in_msg->destroy(in_msg);
317
318 return result;
319 }
320
321 /**
322 * see section 3.8.6 of TCG TNC IF-IMV Specification 1.3
323 */
324 TNC_Result TNC_IMV_ReceiveMessageLong(TNC_IMVID imv_id,
325 TNC_ConnectionID connection_id,
326 TNC_UInt32 msg_flags,
327 TNC_BufferReference msg,
328 TNC_UInt32 msg_len,
329 TNC_VendorID msg_vid,
330 TNC_MessageSubtype msg_subtype,
331 TNC_UInt32 src_imc_id,
332 TNC_UInt32 dst_imv_id)
333 {
334 imv_state_t *state;
335 imv_msg_t *in_msg;
336 TNC_Result result;
337
338 if (!imv_scanner)
339 {
340 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
341 return TNC_RESULT_NOT_INITIALIZED;
342 }
343 if (!imv_scanner->get_state(imv_scanner, connection_id, &state))
344 {
345 return TNC_RESULT_FATAL;
346 }
347 in_msg = imv_msg_create_from_long_data(imv_scanner, state, connection_id,
348 src_imc_id, dst_imv_id, msg_vid, msg_subtype,
349 chunk_create(msg, msg_len));
350 result =receive_message(state, in_msg);
351 in_msg->destroy(in_msg);
352
353 return result;
354 }
355
356 /**
357 * see section 3.8.7 of TCG TNC IF-IMV Specification 1.3
358 */
359 TNC_Result TNC_IMV_SolicitRecommendation(TNC_IMVID imv_id,
360 TNC_ConnectionID connection_id)
361 {
362 imv_state_t *state;
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 if (!imv_scanner->get_state(imv_scanner, connection_id, &state))
370 {
371 return TNC_RESULT_FATAL;
372 }
373 return imv_scanner->provide_recommendation(imv_scanner, state);
374 }
375
376 /**
377 * see section 3.8.8 of TCG TNC IF-IMV Specification 1.3
378 */
379 TNC_Result TNC_IMV_BatchEnding(TNC_IMVID imv_id,
380 TNC_ConnectionID connection_id)
381 {
382 imv_state_t *state;
383 imv_msg_t *out_msg;
384 pa_tnc_attr_t *attr;
385 TNC_IMV_Action_Recommendation rec;
386 TNC_IMV_Evaluation_Result eval;
387 TNC_Result result = TNC_RESULT_SUCCESS;
388
389 if (!imv_scanner)
390 {
391 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
392 return TNC_RESULT_NOT_INITIALIZED;
393 }
394 if (!imv_scanner->get_state(imv_scanner, connection_id, &state))
395 {
396 return TNC_RESULT_FATAL;
397 }
398 state->get_recommendation(state, &rec, &eval);
399 if (rec == TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION)
400 {
401 out_msg = imv_msg_create(imv_scanner, state, connection_id, imv_id,
402 TNC_IMCID_ANY, msg_types[0]);
403 attr = ietf_attr_attr_request_create(PEN_IETF, IETF_ATTR_PORT_FILTER);
404 out_msg->add_attribute(out_msg, attr);
405
406 /* send PA-TNC message with excl flag not set */
407 result = out_msg->send(out_msg, FALSE);
408 out_msg->destroy(out_msg);
409
410 }
411 return result;
412 }
413
414 /**
415 * see section 3.8.9 of TCG TNC IF-IMV Specification 1.3
416 */
417 TNC_Result TNC_IMV_Terminate(TNC_IMVID imv_id)
418 {
419 if (!imv_scanner)
420 {
421 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
422 return TNC_RESULT_NOT_INITIALIZED;
423 }
424 tcp_ports->destroy_function(tcp_ports, free);
425 udp_ports->destroy_function(udp_ports, free);
426 imv_scanner->destroy(imv_scanner);
427 imv_scanner = NULL;
428
429 return TNC_RESULT_SUCCESS;
430 }
431
432 /**
433 * see section 4.2.8.1 of TCG TNC IF-IMV Specification 1.3
434 */
435 TNC_Result TNC_IMV_ProvideBindFunction(TNC_IMVID imv_id,
436 TNC_TNCS_BindFunctionPointer bind_function)
437 {
438 if (!imv_scanner)
439 {
440 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
441 return TNC_RESULT_NOT_INITIALIZED;
442 }
443 return imv_scanner->bind_functions(imv_scanner, bind_function);
444 }