scanner imc/imv pair uses IETF VPN PA-TNC message subtype
[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, 1, imv_id, actual_version);
130 if (!imv_scanner)
131 {
132 return TNC_RESULT_FATAL;
133 }
134 if (min_version > TNC_IFIMV_VERSION_1 || max_version < TNC_IFIMV_VERSION_1)
135 {
136 DBG1(DBG_IMV, "no common IF-IMV version");
137 return TNC_RESULT_NO_COMMON_VERSION;
138 }
139
140 /* set the default port policy to closed (TRUE) or open (FALSE) */
141 closed_port_policy = lib->settings->get_bool(lib->settings,
142 "libimcv.plugins.imv-scanner.closed_port_policy", TRUE);
143 DBG2(DBG_IMV, "default port policy is %s ports",
144 closed_port_policy ? "closed" : "open");
145
146 /* get the list of open|closed ports */
147 tcp_ports = get_port_list("tcp");
148 udp_ports = get_port_list("udp");
149
150 return TNC_RESULT_SUCCESS;
151 }
152
153 /**
154 * see section 3.8.2 of TCG TNC IF-IMV Specification 1.3
155 */
156 TNC_Result TNC_IMV_NotifyConnectionChange(TNC_IMVID imv_id,
157 TNC_ConnectionID connection_id,
158 TNC_ConnectionState new_state)
159 {
160 imv_state_t *state;
161
162 if (!imv_scanner)
163 {
164 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
165 return TNC_RESULT_NOT_INITIALIZED;
166 }
167 switch (new_state)
168 {
169 case TNC_CONNECTION_STATE_CREATE:
170 state = imv_scanner_state_create(connection_id);
171 return imv_scanner->create_state(imv_scanner, state);
172 case TNC_CONNECTION_STATE_DELETE:
173 return imv_scanner->delete_state(imv_scanner, connection_id);
174 default:
175 return imv_scanner->change_state(imv_scanner, connection_id,
176 new_state, NULL);
177 }
178 }
179
180 static TNC_Result receive_message(imv_state_t *state, imv_msg_t *in_msg)
181 {
182 imv_msg_t *out_msg;
183 enumerator_t *enumerator;
184 pa_tnc_attr_t *attr;
185 pen_type_t type;
186 TNC_Result result;
187 bool fatal_error = FALSE;
188
189 /* parse received PA-TNC message and handle local and remote errors */
190 result = in_msg->receive(in_msg, &fatal_error);
191 if (result != TNC_RESULT_SUCCESS)
192 {
193 return result;
194 }
195
196 /* analyze PA-TNC attributes */
197 enumerator = in_msg->create_attribute_enumerator(in_msg);
198 while (enumerator->enumerate(enumerator, &attr))
199 {
200 type = attr->get_type(attr);
201
202 if (type.vendor_id == PEN_IETF && type.type == IETF_ATTR_PORT_FILTER)
203 {
204 ietf_attr_port_filter_t *attr_port_filter;
205 enumerator_t *enumerator;
206 u_int8_t protocol;
207 u_int16_t port;
208 char buf[BUF_LEN], *pos = buf;
209 size_t len = BUF_LEN;
210 bool blocked, compliant = TRUE;
211
212 attr_port_filter = (ietf_attr_port_filter_t*)attr;
213 enumerator = attr_port_filter->create_port_enumerator(attr_port_filter);
214 while (enumerator->enumerate(enumerator, &blocked, &protocol, &port))
215 {
216 enumerator_t *e;
217 port_range_t *port_range;
218 bool passed, found = FALSE;
219 int written = 0;
220
221 if (blocked)
222 {
223 /* ignore closed ports */
224 continue;
225 }
226
227 e = (protocol == IPPROTO_TCP) ?
228 tcp_ports->create_enumerator(tcp_ports) :
229 udp_ports->create_enumerator(udp_ports);
230 while (e->enumerate(e, &port_range))
231 {
232 if (port >= port_range->start && port <= port_range->stop)
233 {
234 found = TRUE;
235 break;
236 }
237 }
238 e->destroy(e);
239
240 passed = (closed_port_policy == found);
241 DBG2(DBG_IMV, "%s port %5u %s: %s",
242 (protocol == IPPROTO_TCP) ? "tcp" : "udp", port,
243 blocked ? "closed" : "open", passed ? "ok" : "fatal");
244 if (!passed)
245 {
246 compliant = FALSE;
247 written = snprintf(pos, len, " %s/%u",
248 (protocol == IPPROTO_TCP) ? "tcp" : "udp",
249 port);
250 if (written < 0 || written >= len)
251 {
252 break;
253 }
254 pos += written;
255 len -= written;
256 }
257 }
258 enumerator->destroy(enumerator);
259
260 if (compliant)
261 {
262 state->set_recommendation(state,
263 TNC_IMV_ACTION_RECOMMENDATION_ALLOW,
264 TNC_IMV_EVALUATION_RESULT_COMPLIANT);
265 }
266 else
267 {
268 imv_scanner_state_t *imv_scanner_state;
269
270 imv_scanner_state = (imv_scanner_state_t*)state;
271 imv_scanner_state->set_violating_ports(imv_scanner_state, buf);
272 state->set_recommendation(state,
273 TNC_IMV_ACTION_RECOMMENDATION_NO_ACCESS,
274 TNC_IMV_EVALUATION_RESULT_NONCOMPLIANT_MAJOR);
275 }
276 }
277 }
278 enumerator->destroy(enumerator);
279
280 if (fatal_error)
281 {
282 state->set_recommendation(state,
283 TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION,
284 TNC_IMV_EVALUATION_RESULT_ERROR);
285 }
286
287 out_msg = imv_msg_create_as_reply(in_msg);
288 result = out_msg->send_assessment(out_msg);
289 out_msg->destroy(out_msg);
290 if (result != TNC_RESULT_SUCCESS)
291 {
292 return result;
293 }
294 return imv_scanner->provide_recommendation(imv_scanner, state);
295 }
296
297 /**
298 * see section 3.8.4 of TCG TNC IF-IMV Specification 1.3
299 */
300 TNC_Result TNC_IMV_ReceiveMessage(TNC_IMVID imv_id,
301 TNC_ConnectionID connection_id,
302 TNC_BufferReference msg,
303 TNC_UInt32 msg_len,
304 TNC_MessageType msg_type)
305 {
306 imv_state_t *state;
307 imv_msg_t *in_msg;
308 TNC_Result result;
309
310 if (!imv_scanner)
311 {
312 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
313 return TNC_RESULT_NOT_INITIALIZED;
314 }
315 if (!imv_scanner->get_state(imv_scanner, connection_id, &state))
316 {
317 return TNC_RESULT_FATAL;
318 }
319
320 in_msg = imv_msg_create_from_data(imv_scanner, state, connection_id, msg_type,
321 chunk_create(msg, msg_len));
322 result = receive_message(state, in_msg);
323 in_msg->destroy(in_msg);
324
325 return result;
326 }
327
328 /**
329 * see section 3.8.6 of TCG TNC IF-IMV Specification 1.3
330 */
331 TNC_Result TNC_IMV_ReceiveMessageLong(TNC_IMVID imv_id,
332 TNC_ConnectionID connection_id,
333 TNC_UInt32 msg_flags,
334 TNC_BufferReference msg,
335 TNC_UInt32 msg_len,
336 TNC_VendorID msg_vid,
337 TNC_MessageSubtype msg_subtype,
338 TNC_UInt32 src_imc_id,
339 TNC_UInt32 dst_imv_id)
340 {
341 imv_state_t *state;
342 imv_msg_t *in_msg;
343 TNC_Result result;
344
345 if (!imv_scanner)
346 {
347 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
348 return TNC_RESULT_NOT_INITIALIZED;
349 }
350 if (!imv_scanner->get_state(imv_scanner, connection_id, &state))
351 {
352 return TNC_RESULT_FATAL;
353 }
354 in_msg = imv_msg_create_from_long_data(imv_scanner, state, connection_id,
355 src_imc_id, dst_imv_id, msg_vid, msg_subtype,
356 chunk_create(msg, msg_len));
357 result =receive_message(state, in_msg);
358 in_msg->destroy(in_msg);
359
360 return result;
361 }
362
363 /**
364 * see section 3.8.7 of TCG TNC IF-IMV Specification 1.3
365 */
366 TNC_Result TNC_IMV_SolicitRecommendation(TNC_IMVID imv_id,
367 TNC_ConnectionID connection_id)
368 {
369 imv_state_t *state;
370
371 if (!imv_scanner)
372 {
373 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
374 return TNC_RESULT_NOT_INITIALIZED;
375 }
376 if (!imv_scanner->get_state(imv_scanner, connection_id, &state))
377 {
378 return TNC_RESULT_FATAL;
379 }
380 return imv_scanner->provide_recommendation(imv_scanner, state);
381 }
382
383 /**
384 * see section 3.8.8 of TCG TNC IF-IMV Specification 1.3
385 */
386 TNC_Result TNC_IMV_BatchEnding(TNC_IMVID imv_id,
387 TNC_ConnectionID connection_id)
388 {
389 imv_state_t *state;
390 imv_msg_t *out_msg;
391 pa_tnc_attr_t *attr;
392 TNC_IMV_Action_Recommendation rec;
393 TNC_IMV_Evaluation_Result eval;
394 TNC_Result result = TNC_RESULT_SUCCESS;
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 if (!imv_scanner->get_state(imv_scanner, connection_id, &state))
402 {
403 return TNC_RESULT_FATAL;
404 }
405 state->get_recommendation(state, &rec, &eval);
406 if (rec == TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION)
407 {
408 out_msg = imv_msg_create(imv_scanner, state, connection_id, imv_id,
409 TNC_IMCID_ANY, msg_types[0]);
410 attr = ietf_attr_attr_request_create(PEN_IETF, IETF_ATTR_PORT_FILTER);
411 out_msg->add_attribute(out_msg, attr);
412
413 /* send PA-TNC message with excl flag not set */
414 result = out_msg->send(out_msg, FALSE);
415 out_msg->destroy(out_msg);
416
417 }
418 return result;
419 }
420
421 /**
422 * see section 3.8.9 of TCG TNC IF-IMV Specification 1.3
423 */
424 TNC_Result TNC_IMV_Terminate(TNC_IMVID imv_id)
425 {
426 if (!imv_scanner)
427 {
428 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
429 return TNC_RESULT_NOT_INITIALIZED;
430 }
431 tcp_ports->destroy_function(tcp_ports, free);
432 udp_ports->destroy_function(udp_ports, free);
433 imv_scanner->destroy(imv_scanner);
434 imv_scanner = NULL;
435
436 return TNC_RESULT_SUCCESS;
437 }
438
439 /**
440 * see section 4.2.8.1 of TCG TNC IF-IMV Specification 1.3
441 */
442 TNC_Result TNC_IMV_ProvideBindFunction(TNC_IMVID imv_id,
443 TNC_TNCS_BindFunctionPointer bind_function)
444 {
445 if (!imv_scanner)
446 {
447 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
448 return TNC_RESULT_NOT_INITIALIZED;
449 }
450 return imv_scanner->bind_functions(imv_scanner, bind_function);
451 }