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