allow registration of multiple message type
[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 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(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 pen_type_t type;
191 imv_state_t *state;
192 enumerator_t *enumerator;
193 TNC_Result result;
194 bool fatal_error;
195
196 if (!imv_scanner)
197 {
198 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
199 return TNC_RESULT_NOT_INITIALIZED;
200 }
201
202 /* get current IMV state */
203 if (!imv_scanner->get_state(imv_scanner, connection_id, &state))
204 {
205 return TNC_RESULT_FATAL;
206 }
207
208 /* parse received PA-TNC message and automatically handle any errors */
209 result = imv_scanner->receive_message(imv_scanner, state, msg, msg_vid,
210 msg_subtype, src_imc_id, dst_imv_id, &pa_tnc_msg);
211
212 /* no parsed PA-TNC attributes available if an error occurred */
213 if (!pa_tnc_msg)
214 {
215 return result;
216 }
217
218 /* preprocess any IETF standard error attributes */
219 fatal_error = pa_tnc_msg->process_ietf_std_errors(pa_tnc_msg);
220
221 /* analyze PA-TNC attributes */
222 enumerator = pa_tnc_msg->create_attribute_enumerator(pa_tnc_msg);
223 while (enumerator->enumerate(enumerator, &attr))
224 {
225 type = attr->get_type(attr);
226
227 if (type.vendor_id == PEN_IETF && type.type == IETF_ATTR_PORT_FILTER)
228 {
229 ietf_attr_port_filter_t *attr_port_filter;
230 enumerator_t *enumerator;
231 u_int8_t protocol;
232 u_int16_t port;
233 char buf[BUF_LEN], *pos = buf;
234 size_t len = BUF_LEN;
235 bool blocked, compliant = TRUE;
236
237 attr_port_filter = (ietf_attr_port_filter_t*)attr;
238 enumerator = attr_port_filter->create_port_enumerator(attr_port_filter);
239 while (enumerator->enumerate(enumerator, &blocked, &protocol, &port))
240 {
241 enumerator_t *e;
242 port_range_t *port_range;
243 bool passed, found = FALSE;
244 int written = 0;
245
246 if (blocked)
247 {
248 /* ignore closed ports */
249 continue;
250 }
251
252 e = (protocol == IPPROTO_TCP) ?
253 tcp_ports->create_enumerator(tcp_ports) :
254 udp_ports->create_enumerator(udp_ports);
255 while (e->enumerate(e, &port_range))
256 {
257 if (port >= port_range->start && port <= port_range->stop)
258 {
259 found = TRUE;
260 break;
261 }
262 }
263 e->destroy(e);
264
265 passed = (closed_port_policy == found);
266 DBG2(DBG_IMV, "%s port %5u %s: %s",
267 (protocol == IPPROTO_TCP) ? "tcp" : "udp", port,
268 blocked ? "closed" : "open", passed ? "ok" : "fatal");
269 if (!passed)
270 {
271 compliant = FALSE;
272 written = snprintf(pos, len, " %s/%u",
273 (protocol == IPPROTO_TCP) ? "tcp" : "udp",
274 port);
275 if (written < 0 || written >= len)
276 {
277 break;
278 }
279 pos += written;
280 len -= written;
281 }
282 }
283 enumerator->destroy(enumerator);
284
285 if (compliant)
286 {
287 state->set_recommendation(state,
288 TNC_IMV_ACTION_RECOMMENDATION_ALLOW,
289 TNC_IMV_EVALUATION_RESULT_COMPLIANT);
290 }
291 else
292 {
293 imv_scanner_state_t *imv_scanner_state;
294
295 imv_scanner_state = (imv_scanner_state_t*)state;
296 imv_scanner_state->set_violating_ports(imv_scanner_state, buf);
297 state->set_recommendation(state,
298 TNC_IMV_ACTION_RECOMMENDATION_NO_ACCESS,
299 TNC_IMV_EVALUATION_RESULT_NONCOMPLIANT_MAJOR);
300 }
301 }
302 }
303 enumerator->destroy(enumerator);
304 pa_tnc_msg->destroy(pa_tnc_msg);
305
306 if (fatal_error)
307 {
308 state->set_recommendation(state,
309 TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION,
310 TNC_IMV_EVALUATION_RESULT_ERROR);
311 }
312 return imv_scanner->provide_recommendation(imv_scanner, connection_id,
313 src_imc_id, PEN_ITA, PA_SUBTYPE_ITA_SCANNER);
314 }
315
316 /**
317 * see section 3.8.4 of TCG TNC IF-IMV Specification 1.3
318 */
319 TNC_Result TNC_IMV_ReceiveMessage(TNC_IMVID imv_id,
320 TNC_ConnectionID connection_id,
321 TNC_BufferReference msg,
322 TNC_UInt32 msg_len,
323 TNC_MessageType msg_type)
324 {
325 TNC_VendorID msg_vid;
326 TNC_MessageSubtype msg_subtype;
327
328 msg_vid = msg_type >> 8;
329 msg_subtype = msg_type & TNC_SUBTYPE_ANY;
330
331 return receive_message(imv_id, connection_id, 0, chunk_create(msg, msg_len),
332 msg_vid, msg_subtype, 0, TNC_IMVID_ANY);
333 }
334
335 /**
336 * see section 3.8.6 of TCG TNC IF-IMV Specification 1.3
337 */
338 TNC_Result TNC_IMV_ReceiveMessageLong(TNC_IMVID imv_id,
339 TNC_ConnectionID connection_id,
340 TNC_UInt32 msg_flags,
341 TNC_BufferReference msg,
342 TNC_UInt32 msg_len,
343 TNC_VendorID msg_vid,
344 TNC_MessageSubtype msg_subtype,
345 TNC_UInt32 src_imc_id,
346 TNC_UInt32 dst_imv_id)
347 {
348 return receive_message(imv_id, connection_id, msg_flags,
349 chunk_create(msg, msg_len), msg_vid, msg_subtype,
350 src_imc_id, dst_imv_id);
351 }
352
353 /**
354 * see section 3.8.7 of TCG TNC IF-IMV Specification 1.3
355 */
356 TNC_Result TNC_IMV_SolicitRecommendation(TNC_IMVID imv_id,
357 TNC_ConnectionID connection_id)
358 {
359 if (!imv_scanner)
360 {
361 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
362 return TNC_RESULT_NOT_INITIALIZED;
363 }
364 return imv_scanner->provide_recommendation(imv_scanner, connection_id,
365 TNC_IMCID_ANY, PEN_ITA, PA_SUBTYPE_ITA_SCANNER);
366 }
367
368 /**
369 * see section 3.8.8 of TCG TNC IF-IMV Specification 1.3
370 */
371 TNC_Result TNC_IMV_BatchEnding(TNC_IMVID imv_id,
372 TNC_ConnectionID connection_id)
373 {
374 if (!imv_scanner)
375 {
376 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
377 return TNC_RESULT_NOT_INITIALIZED;
378 }
379 return TNC_RESULT_SUCCESS;
380 }
381
382 /**
383 * see section 3.8.9 of TCG TNC IF-IMV Specification 1.3
384 */
385 TNC_Result TNC_IMV_Terminate(TNC_IMVID imv_id)
386 {
387 if (!imv_scanner)
388 {
389 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
390 return TNC_RESULT_NOT_INITIALIZED;
391 }
392 tcp_ports->destroy_function(tcp_ports, free);
393 udp_ports->destroy_function(udp_ports, free);
394 imv_scanner->destroy(imv_scanner);
395 imv_scanner = NULL;
396
397 return TNC_RESULT_SUCCESS;
398 }
399
400 /**
401 * see section 4.2.8.1 of TCG TNC IF-IMV Specification 1.3
402 */
403 TNC_Result TNC_IMV_ProvideBindFunction(TNC_IMVID imv_id,
404 TNC_TNCS_BindFunctionPointer bind_function)
405 {
406 if (!imv_scanner)
407 {
408 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
409 return TNC_RESULT_NOT_INITIALIZED;
410 }
411 return imv_scanner->bind_functions(imv_scanner, bind_function);
412 }