use countof()
[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 ietf_attr_port_filter_t *attr_port_filter;
206 enumerator_t *enumerator;
207 u_int8_t protocol;
208 u_int16_t port;
209 char buf[BUF_LEN], *pos = buf;
210 size_t len = BUF_LEN;
211 bool blocked, compliant = TRUE;
212
213 attr_port_filter = (ietf_attr_port_filter_t*)attr;
214 enumerator = attr_port_filter->create_port_enumerator(attr_port_filter);
215 while (enumerator->enumerate(enumerator, &blocked, &protocol, &port))
216 {
217 enumerator_t *e;
218 port_range_t *port_range;
219 bool passed, found = FALSE;
220 int written = 0;
221
222 if (blocked)
223 {
224 /* ignore closed ports */
225 continue;
226 }
227
228 e = (protocol == IPPROTO_TCP) ?
229 tcp_ports->create_enumerator(tcp_ports) :
230 udp_ports->create_enumerator(udp_ports);
231 while (e->enumerate(e, &port_range))
232 {
233 if (port >= port_range->start && port <= port_range->stop)
234 {
235 found = TRUE;
236 break;
237 }
238 }
239 e->destroy(e);
240
241 passed = (closed_port_policy == found);
242 DBG2(DBG_IMV, "%s port %5u %s: %s",
243 (protocol == IPPROTO_TCP) ? "tcp" : "udp", port,
244 blocked ? "closed" : "open", passed ? "ok" : "fatal");
245 if (!passed)
246 {
247 compliant = FALSE;
248 written = snprintf(pos, len, " %s/%u",
249 (protocol == IPPROTO_TCP) ? "tcp" : "udp",
250 port);
251 if (written < 0 || written >= len)
252 {
253 break;
254 }
255 pos += written;
256 len -= written;
257 }
258 }
259 enumerator->destroy(enumerator);
260
261 if (compliant)
262 {
263 state->set_recommendation(state,
264 TNC_IMV_ACTION_RECOMMENDATION_ALLOW,
265 TNC_IMV_EVALUATION_RESULT_COMPLIANT);
266 }
267 else
268 {
269 imv_scanner_state_t *imv_scanner_state;
270
271 imv_scanner_state = (imv_scanner_state_t*)state;
272 imv_scanner_state->set_violating_ports(imv_scanner_state, buf);
273 state->set_recommendation(state,
274 TNC_IMV_ACTION_RECOMMENDATION_NO_ACCESS,
275 TNC_IMV_EVALUATION_RESULT_NONCOMPLIANT_MAJOR);
276 }
277 }
278 }
279 enumerator->destroy(enumerator);
280
281 if (fatal_error)
282 {
283 state->set_recommendation(state,
284 TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION,
285 TNC_IMV_EVALUATION_RESULT_ERROR);
286 }
287
288 out_msg = imv_msg_create_as_reply(in_msg);
289 result = out_msg->send_assessment(out_msg);
290 out_msg->destroy(out_msg);
291 if (result != TNC_RESULT_SUCCESS)
292 {
293 return result;
294 }
295 return imv_scanner->provide_recommendation(imv_scanner, state);
296 }
297
298 /**
299 * see section 3.8.4 of TCG TNC IF-IMV Specification 1.3
300 */
301 TNC_Result TNC_IMV_ReceiveMessage(TNC_IMVID imv_id,
302 TNC_ConnectionID connection_id,
303 TNC_BufferReference msg,
304 TNC_UInt32 msg_len,
305 TNC_MessageType msg_type)
306 {
307 imv_state_t *state;
308 imv_msg_t *in_msg;
309 TNC_Result result;
310
311 if (!imv_scanner)
312 {
313 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
314 return TNC_RESULT_NOT_INITIALIZED;
315 }
316 if (!imv_scanner->get_state(imv_scanner, connection_id, &state))
317 {
318 return TNC_RESULT_FATAL;
319 }
320
321 in_msg = imv_msg_create_from_data(imv_scanner, state, connection_id, msg_type,
322 chunk_create(msg, msg_len));
323 result = receive_message(state, in_msg);
324 in_msg->destroy(in_msg);
325
326 return result;
327 }
328
329 /**
330 * see section 3.8.6 of TCG TNC IF-IMV Specification 1.3
331 */
332 TNC_Result TNC_IMV_ReceiveMessageLong(TNC_IMVID imv_id,
333 TNC_ConnectionID connection_id,
334 TNC_UInt32 msg_flags,
335 TNC_BufferReference msg,
336 TNC_UInt32 msg_len,
337 TNC_VendorID msg_vid,
338 TNC_MessageSubtype msg_subtype,
339 TNC_UInt32 src_imc_id,
340 TNC_UInt32 dst_imv_id)
341 {
342 imv_state_t *state;
343 imv_msg_t *in_msg;
344 TNC_Result result;
345
346 if (!imv_scanner)
347 {
348 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
349 return TNC_RESULT_NOT_INITIALIZED;
350 }
351 if (!imv_scanner->get_state(imv_scanner, connection_id, &state))
352 {
353 return TNC_RESULT_FATAL;
354 }
355 in_msg = imv_msg_create_from_long_data(imv_scanner, state, connection_id,
356 src_imc_id, dst_imv_id, msg_vid, msg_subtype,
357 chunk_create(msg, msg_len));
358 result =receive_message(state, in_msg);
359 in_msg->destroy(in_msg);
360
361 return result;
362 }
363
364 /**
365 * see section 3.8.7 of TCG TNC IF-IMV Specification 1.3
366 */
367 TNC_Result TNC_IMV_SolicitRecommendation(TNC_IMVID imv_id,
368 TNC_ConnectionID connection_id)
369 {
370 imv_state_t *state;
371
372 if (!imv_scanner)
373 {
374 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
375 return TNC_RESULT_NOT_INITIALIZED;
376 }
377 if (!imv_scanner->get_state(imv_scanner, connection_id, &state))
378 {
379 return TNC_RESULT_FATAL;
380 }
381 return imv_scanner->provide_recommendation(imv_scanner, state);
382 }
383
384 /**
385 * see section 3.8.8 of TCG TNC IF-IMV Specification 1.3
386 */
387 TNC_Result TNC_IMV_BatchEnding(TNC_IMVID imv_id,
388 TNC_ConnectionID connection_id)
389 {
390 imv_state_t *state;
391 imv_msg_t *out_msg;
392 pa_tnc_attr_t *attr;
393 TNC_IMV_Action_Recommendation rec;
394 TNC_IMV_Evaluation_Result eval;
395 TNC_Result result = TNC_RESULT_SUCCESS;
396
397 if (!imv_scanner)
398 {
399 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
400 return TNC_RESULT_NOT_INITIALIZED;
401 }
402 if (!imv_scanner->get_state(imv_scanner, connection_id, &state))
403 {
404 return TNC_RESULT_FATAL;
405 }
406 state->get_recommendation(state, &rec, &eval);
407 if (rec == TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION)
408 {
409 out_msg = imv_msg_create(imv_scanner, state, connection_id, imv_id,
410 TNC_IMCID_ANY, msg_types[0]);
411 attr = ietf_attr_attr_request_create(PEN_IETF, IETF_ATTR_PORT_FILTER);
412 out_msg->add_attribute(out_msg, attr);
413
414 /* send PA-TNC message with excl flag not set */
415 result = out_msg->send(out_msg, FALSE);
416 out_msg->destroy(out_msg);
417
418 }
419 return result;
420 }
421
422 /**
423 * see section 3.8.9 of TCG TNC IF-IMV Specification 1.3
424 */
425 TNC_Result TNC_IMV_Terminate(TNC_IMVID imv_id)
426 {
427 if (!imv_scanner)
428 {
429 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
430 return TNC_RESULT_NOT_INITIALIZED;
431 }
432 tcp_ports->destroy_function(tcp_ports, free);
433 udp_ports->destroy_function(udp_ports, free);
434 imv_scanner->destroy(imv_scanner);
435 imv_scanner = NULL;
436
437 return TNC_RESULT_SUCCESS;
438 }
439
440 /**
441 * see section 4.2.8.1 of TCG TNC IF-IMV Specification 1.3
442 */
443 TNC_Result TNC_IMV_ProvideBindFunction(TNC_IMVID imv_id,
444 TNC_TNCS_BindFunctionPointer bind_function)
445 {
446 if (!imv_scanner)
447 {
448 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
449 return TNC_RESULT_NOT_INITIALIZED;
450 }
451 return imv_scanner->bind_functions(imv_scanner, bind_function);
452 }