Moved data structures to new collections subfolder
[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_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 <collections/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 static pen_type_t msg_types[] = {
37 { PEN_ITA, PA_SUBTYPE_ITA_SCANNER }
38 };
39
40 static imv_agent_t *imv_scanner;
41
42 typedef struct port_range_t port_range_t;
43
44 struct port_range_t {
45 u_int16_t start, stop;
46 };
47
48
49 /**
50 * Default port policy
51 *
52 * TRUE: all server ports on the TNC client must be closed
53 * FALSE: any server port on the TNC client is allowed to be open
54 */
55 static bool closed_port_policy = TRUE;
56
57 /**
58 * List of TCP and UDP port ranges
59 *
60 * TRUE: server ports on the TNC client that are allowed to be open
61 * FALSE: server ports on the TNC client that must be closed
62 */
63 static linked_list_t *tcp_ports, *udp_ports;
64
65 /**
66 * Get a TCP or UDP port list from strongswan.conf
67 */
68 static linked_list_t* get_port_list(char *label)
69 {
70 char key[40], *value;
71 linked_list_t *list;
72 chunk_t port_list, port_item, port_start;
73 port_range_t *port_range;
74
75 list = linked_list_create();
76
77 snprintf(key, sizeof(key), "libimcv.plugins.imv-scanner.%s_ports", label);
78 value = lib->settings->get_str(lib->settings, key, NULL);
79 if (!value)
80 {
81 DBG1(DBG_IMV, "%s not defined", key);
82 return list;
83 }
84 port_list = chunk_create(value, strlen(value));
85 DBG2(DBG_IMV, "list of %s ports that %s:", label,
86 closed_port_policy ? "are allowed to be open" : "must be closed");
87
88 while (eat_whitespace(&port_list))
89 {
90 if (!extract_token(&port_item, ' ', &port_list))
91 {
92 /* reached last port item */
93 port_item = port_list;
94 port_list = chunk_empty;
95 }
96 port_range = malloc_thing(port_range_t);
97 port_range->start = atoi(port_item.ptr);
98
99 if (extract_token(&port_start, '-', &port_item) && port_item.len)
100 {
101 port_range->stop = atoi(port_item.ptr);
102 }
103 else
104 {
105 port_range->stop = port_range->start;
106 }
107 DBG2(DBG_IMV, "%5u - %5u", port_range->start, port_range->stop);
108 list->insert_last(list, port_range);
109 }
110
111 return list;
112 }
113
114
115 /*
116 * see section 3.8.1 of TCG TNC IF-IMV Specification 1.3
117 */
118 TNC_Result TNC_IMV_Initialize(TNC_IMVID imv_id,
119 TNC_Version min_version,
120 TNC_Version max_version,
121 TNC_Version *actual_version)
122 {
123 if (imv_scanner)
124 {
125 DBG1(DBG_IMV, "IMV \"%s\" has already been initialized", imv_name);
126 return TNC_RESULT_ALREADY_INITIALIZED;
127 }
128 imv_scanner = imv_agent_create(imv_name, msg_types, 1, 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(imv_state_t *state, imv_msg_t *in_msg)
180 {
181 imv_msg_t *out_msg;
182 enumerator_t *enumerator;
183 pa_tnc_attr_t *attr;
184 pen_type_t type;
185 TNC_Result result;
186 bool fatal_error = FALSE;
187
188 /* parse received PA-TNC message and handle local and remote errors */
189 result = in_msg->receive(in_msg, &fatal_error);
190 if (result != TNC_RESULT_SUCCESS)
191 {
192 return result;
193 }
194
195 /* analyze PA-TNC attributes */
196 enumerator = in_msg->create_attribute_enumerator(in_msg);
197 while (enumerator->enumerate(enumerator, &attr))
198 {
199 type = attr->get_type(attr);
200
201 if (type.vendor_id == PEN_IETF && type.type == IETF_ATTR_PORT_FILTER)
202 {
203 ietf_attr_port_filter_t *attr_port_filter;
204 enumerator_t *enumerator;
205 u_int8_t protocol;
206 u_int16_t port;
207 char buf[BUF_LEN], *pos = buf;
208 size_t len = BUF_LEN;
209 bool blocked, compliant = TRUE;
210
211 attr_port_filter = (ietf_attr_port_filter_t*)attr;
212 enumerator = attr_port_filter->create_port_enumerator(attr_port_filter);
213 while (enumerator->enumerate(enumerator, &blocked, &protocol, &port))
214 {
215 enumerator_t *e;
216 port_range_t *port_range;
217 bool passed, found = FALSE;
218 int written = 0;
219
220 if (blocked)
221 {
222 /* ignore closed ports */
223 continue;
224 }
225
226 e = (protocol == IPPROTO_TCP) ?
227 tcp_ports->create_enumerator(tcp_ports) :
228 udp_ports->create_enumerator(udp_ports);
229 while (e->enumerate(e, &port_range))
230 {
231 if (port >= port_range->start && port <= port_range->stop)
232 {
233 found = TRUE;
234 break;
235 }
236 }
237 e->destroy(e);
238
239 passed = (closed_port_policy == found);
240 DBG2(DBG_IMV, "%s port %5u %s: %s",
241 (protocol == IPPROTO_TCP) ? "tcp" : "udp", port,
242 blocked ? "closed" : "open", passed ? "ok" : "fatal");
243 if (!passed)
244 {
245 compliant = FALSE;
246 written = snprintf(pos, len, " %s/%u",
247 (protocol == IPPROTO_TCP) ? "tcp" : "udp",
248 port);
249 if (written < 0 || written >= len)
250 {
251 break;
252 }
253 pos += written;
254 len -= written;
255 }
256 }
257 enumerator->destroy(enumerator);
258
259 if (compliant)
260 {
261 state->set_recommendation(state,
262 TNC_IMV_ACTION_RECOMMENDATION_ALLOW,
263 TNC_IMV_EVALUATION_RESULT_COMPLIANT);
264 }
265 else
266 {
267 imv_scanner_state_t *imv_scanner_state;
268
269 imv_scanner_state = (imv_scanner_state_t*)state;
270 imv_scanner_state->set_violating_ports(imv_scanner_state, buf);
271 state->set_recommendation(state,
272 TNC_IMV_ACTION_RECOMMENDATION_NO_ACCESS,
273 TNC_IMV_EVALUATION_RESULT_NONCOMPLIANT_MAJOR);
274 }
275 }
276 }
277 enumerator->destroy(enumerator);
278
279 if (fatal_error)
280 {
281 state->set_recommendation(state,
282 TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION,
283 TNC_IMV_EVALUATION_RESULT_ERROR);
284 }
285
286 out_msg = imv_msg_create_as_reply(in_msg);
287 result = out_msg->send_assessment(out_msg);
288 out_msg->destroy(out_msg);
289 if (result != TNC_RESULT_SUCCESS)
290 {
291 return result;
292 }
293 return imv_scanner->provide_recommendation(imv_scanner, state);
294 }
295
296 /**
297 * see section 3.8.4 of TCG TNC IF-IMV Specification 1.3
298 */
299 TNC_Result TNC_IMV_ReceiveMessage(TNC_IMVID imv_id,
300 TNC_ConnectionID connection_id,
301 TNC_BufferReference msg,
302 TNC_UInt32 msg_len,
303 TNC_MessageType msg_type)
304 {
305 imv_state_t *state;
306 imv_msg_t *in_msg;
307 TNC_Result result;
308
309 if (!imv_scanner)
310 {
311 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
312 return TNC_RESULT_NOT_INITIALIZED;
313 }
314 if (!imv_scanner->get_state(imv_scanner, connection_id, &state))
315 {
316 return TNC_RESULT_FATAL;
317 }
318
319 in_msg = imv_msg_create_from_data(imv_scanner, state, connection_id, msg_type,
320 chunk_create(msg, msg_len));
321 result = receive_message(state, in_msg);
322 in_msg->destroy(in_msg);
323
324 return result;
325 }
326
327 /**
328 * see section 3.8.6 of TCG TNC IF-IMV Specification 1.3
329 */
330 TNC_Result TNC_IMV_ReceiveMessageLong(TNC_IMVID imv_id,
331 TNC_ConnectionID connection_id,
332 TNC_UInt32 msg_flags,
333 TNC_BufferReference msg,
334 TNC_UInt32 msg_len,
335 TNC_VendorID msg_vid,
336 TNC_MessageSubtype msg_subtype,
337 TNC_UInt32 src_imc_id,
338 TNC_UInt32 dst_imv_id)
339 {
340 imv_state_t *state;
341 imv_msg_t *in_msg;
342 TNC_Result result;
343
344 if (!imv_scanner)
345 {
346 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
347 return TNC_RESULT_NOT_INITIALIZED;
348 }
349 if (!imv_scanner->get_state(imv_scanner, connection_id, &state))
350 {
351 return TNC_RESULT_FATAL;
352 }
353 in_msg = imv_msg_create_from_long_data(imv_scanner, state, connection_id,
354 src_imc_id, dst_imv_id, msg_vid, msg_subtype,
355 chunk_create(msg, msg_len));
356 result =receive_message(state, in_msg);
357 in_msg->destroy(in_msg);
358
359 return result;
360 }
361
362 /**
363 * see section 3.8.7 of TCG TNC IF-IMV Specification 1.3
364 */
365 TNC_Result TNC_IMV_SolicitRecommendation(TNC_IMVID imv_id,
366 TNC_ConnectionID connection_id)
367 {
368 imv_state_t *state;
369
370 if (!imv_scanner)
371 {
372 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
373 return TNC_RESULT_NOT_INITIALIZED;
374 }
375 if (!imv_scanner->get_state(imv_scanner, connection_id, &state))
376 {
377 return TNC_RESULT_FATAL;
378 }
379 return imv_scanner->provide_recommendation(imv_scanner, state);
380 }
381
382 /**
383 * see section 3.8.8 of TCG TNC IF-IMV Specification 1.3
384 */
385 TNC_Result TNC_IMV_BatchEnding(TNC_IMVID imv_id,
386 TNC_ConnectionID connection_id)
387 {
388 if (!imv_scanner)
389 {
390 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
391 return TNC_RESULT_NOT_INITIALIZED;
392 }
393 return TNC_RESULT_SUCCESS;
394 }
395
396 /**
397 * see section 3.8.9 of TCG TNC IF-IMV Specification 1.3
398 */
399 TNC_Result TNC_IMV_Terminate(TNC_IMVID imv_id)
400 {
401 if (!imv_scanner)
402 {
403 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
404 return TNC_RESULT_NOT_INITIALIZED;
405 }
406 tcp_ports->destroy_function(tcp_ports, free);
407 udp_ports->destroy_function(udp_ports, free);
408 imv_scanner->destroy(imv_scanner);
409 imv_scanner = NULL;
410
411 return TNC_RESULT_SUCCESS;
412 }
413
414 /**
415 * see section 4.2.8.1 of TCG TNC IF-IMV Specification 1.3
416 */
417 TNC_Result TNC_IMV_ProvideBindFunction(TNC_IMVID imv_id,
418 TNC_TNCS_BindFunctionPointer bind_function)
419 {
420 if (!imv_scanner)
421 {
422 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
423 return TNC_RESULT_NOT_INITIALIZED;
424 }
425 return imv_scanner->bind_functions(imv_scanner, bind_function);
426 }