register received scanner attributes
[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 /**
44 * Flag set when corresponding attribute has been received
45 */
46 typedef enum imv_scanner_attr_t imv_scanner_attr_t;
47
48 enum imv_scanner_attr_t {
49 IMV_SCANNER_ATTR_PORT_FILTER = (1<<0)
50 };
51
52 typedef struct port_range_t port_range_t;
53
54 struct port_range_t {
55 u_int16_t start, stop;
56 };
57
58
59 /**
60 * Default port policy
61 *
62 * TRUE: all server ports on the TNC client must be closed
63 * FALSE: any server port on the TNC client is allowed to be open
64 */
65 static bool closed_port_policy = TRUE;
66
67 /**
68 * List of TCP and UDP port ranges
69 *
70 * TRUE: server ports on the TNC client that are allowed to be open
71 * FALSE: server ports on the TNC client that must be closed
72 */
73 static linked_list_t *tcp_ports, *udp_ports;
74
75 /**
76 * Get a TCP or UDP port list from strongswan.conf
77 */
78 static linked_list_t* get_port_list(char *label)
79 {
80 char key[40], *value;
81 linked_list_t *list;
82 chunk_t port_list, port_item, port_start;
83 port_range_t *port_range;
84
85 list = linked_list_create();
86
87 snprintf(key, sizeof(key), "libimcv.plugins.imv-scanner.%s_ports", label);
88 value = lib->settings->get_str(lib->settings, key, NULL);
89 if (!value)
90 {
91 DBG1(DBG_IMV, "%s not defined", key);
92 return list;
93 }
94 port_list = chunk_create(value, strlen(value));
95 DBG2(DBG_IMV, "list of %s ports that %s:", label,
96 closed_port_policy ? "are allowed to be open" : "must be closed");
97
98 while (eat_whitespace(&port_list))
99 {
100 if (!extract_token(&port_item, ' ', &port_list))
101 {
102 /* reached last port item */
103 port_item = port_list;
104 port_list = chunk_empty;
105 }
106 port_range = malloc_thing(port_range_t);
107 port_range->start = atoi(port_item.ptr);
108
109 if (extract_token(&port_start, '-', &port_item) && port_item.len)
110 {
111 port_range->stop = atoi(port_item.ptr);
112 }
113 else
114 {
115 port_range->stop = port_range->start;
116 }
117 DBG2(DBG_IMV, "%5u - %5u", port_range->start, port_range->stop);
118 list->insert_last(list, port_range);
119 }
120
121 return list;
122 }
123
124
125 /*
126 * see section 3.8.1 of TCG TNC IF-IMV Specification 1.3
127 */
128 TNC_Result TNC_IMV_Initialize(TNC_IMVID imv_id,
129 TNC_Version min_version,
130 TNC_Version max_version,
131 TNC_Version *actual_version)
132 {
133 if (imv_scanner)
134 {
135 DBG1(DBG_IMV, "IMV \"%s\" has already been initialized", imv_name);
136 return TNC_RESULT_ALREADY_INITIALIZED;
137 }
138 imv_scanner = imv_agent_create(imv_name, msg_types, countof(msg_types),
139 imv_id, actual_version);
140 if (!imv_scanner)
141 {
142 return TNC_RESULT_FATAL;
143 }
144 if (min_version > TNC_IFIMV_VERSION_1 || max_version < TNC_IFIMV_VERSION_1)
145 {
146 DBG1(DBG_IMV, "no common IF-IMV version");
147 return TNC_RESULT_NO_COMMON_VERSION;
148 }
149
150 /* set the default port policy to closed (TRUE) or open (FALSE) */
151 closed_port_policy = lib->settings->get_bool(lib->settings,
152 "libimcv.plugins.imv-scanner.closed_port_policy", TRUE);
153 DBG2(DBG_IMV, "default port policy is %s ports",
154 closed_port_policy ? "closed" : "open");
155
156 /* get the list of open|closed ports */
157 tcp_ports = get_port_list("tcp");
158 udp_ports = get_port_list("udp");
159
160 return TNC_RESULT_SUCCESS;
161 }
162
163 /**
164 * see section 3.8.2 of TCG TNC IF-IMV Specification 1.3
165 */
166 TNC_Result TNC_IMV_NotifyConnectionChange(TNC_IMVID imv_id,
167 TNC_ConnectionID connection_id,
168 TNC_ConnectionState new_state)
169 {
170 imv_state_t *state;
171
172 if (!imv_scanner)
173 {
174 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
175 return TNC_RESULT_NOT_INITIALIZED;
176 }
177 switch (new_state)
178 {
179 case TNC_CONNECTION_STATE_CREATE:
180 state = imv_scanner_state_create(connection_id);
181 return imv_scanner->create_state(imv_scanner, state);
182 case TNC_CONNECTION_STATE_DELETE:
183 return imv_scanner->delete_state(imv_scanner, connection_id);
184 default:
185 return imv_scanner->change_state(imv_scanner, connection_id,
186 new_state, NULL);
187 }
188 }
189
190 static TNC_Result receive_message(imv_state_t *state, imv_msg_t *in_msg)
191 {
192 imv_msg_t *out_msg;
193 enumerator_t *enumerator;
194 pa_tnc_attr_t *attr;
195 pen_type_t type;
196 TNC_Result result;
197 bool fatal_error = FALSE;
198
199 /* parse received PA-TNC message and handle local and remote errors */
200 result = in_msg->receive(in_msg, &fatal_error);
201 if (result != TNC_RESULT_SUCCESS)
202 {
203 return result;
204 }
205
206 /* analyze PA-TNC attributes */
207 enumerator = in_msg->create_attribute_enumerator(in_msg);
208 while (enumerator->enumerate(enumerator, &attr))
209 {
210 type = attr->get_type(attr);
211
212 if (type.vendor_id == PEN_IETF && type.type == IETF_ATTR_PORT_FILTER)
213 {
214 imv_scanner_state_t *scanner_state;
215 ietf_attr_port_filter_t *attr_port_filter;
216 enumerator_t *enumerator;
217 u_int8_t protocol;
218 u_int16_t port;
219 bool blocked, compliant = TRUE;
220
221
222 scanner_state = (imv_scanner_state_t*)state;
223 scanner_state->set_received(scanner_state,
224 IMV_SCANNER_ATTR_PORT_FILTER);
225 attr_port_filter = (ietf_attr_port_filter_t*)attr;
226
227 enumerator = attr_port_filter->create_port_enumerator(attr_port_filter);
228 while (enumerator->enumerate(enumerator, &blocked, &protocol, &port))
229 {
230 enumerator_t *e;
231 port_range_t *port_range;
232 bool passed, found = FALSE;
233 char buf[20];
234
235 if (blocked)
236 {
237 /* ignore closed ports */
238 continue;
239 }
240
241 e = (protocol == IPPROTO_TCP) ?
242 tcp_ports->create_enumerator(tcp_ports) :
243 udp_ports->create_enumerator(udp_ports);
244 while (e->enumerate(e, &port_range))
245 {
246 if (port >= port_range->start && port <= port_range->stop)
247 {
248 found = TRUE;
249 break;
250 }
251 }
252 e->destroy(e);
253
254 passed = (closed_port_policy == found);
255 DBG2(DBG_IMV, "%s port %5u %s: %s",
256 (protocol == IPPROTO_TCP) ? "tcp" : "udp", port,
257 blocked ? "closed" : "open", passed ? "ok" : "fatal");
258 if (!passed)
259 {
260 compliant = FALSE;
261 snprintf(buf, sizeof(buf), "%s/%u",
262 (protocol == IPPROTO_TCP) ? "tcp" : "udp", port);
263 scanner_state->add_violating_port(scanner_state, strdup(buf));
264 }
265 }
266 enumerator->destroy(enumerator);
267
268 if (compliant)
269 {
270 state->set_recommendation(state,
271 TNC_IMV_ACTION_RECOMMENDATION_ALLOW,
272 TNC_IMV_EVALUATION_RESULT_COMPLIANT);
273 }
274 else
275 {
276 state->set_recommendation(state,
277 TNC_IMV_ACTION_RECOMMENDATION_NO_ACCESS,
278 TNC_IMV_EVALUATION_RESULT_NONCOMPLIANT_MAJOR);
279 }
280 }
281 }
282 enumerator->destroy(enumerator);
283
284 if (fatal_error)
285 {
286 state->set_recommendation(state,
287 TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION,
288 TNC_IMV_EVALUATION_RESULT_ERROR);
289 }
290
291 out_msg = imv_msg_create_as_reply(in_msg);
292 result = out_msg->send_assessment(out_msg);
293 out_msg->destroy(out_msg);
294 if (result != TNC_RESULT_SUCCESS)
295 {
296 return result;
297 }
298 return imv_scanner->provide_recommendation(imv_scanner, state);
299 }
300
301 /**
302 * see section 3.8.4 of TCG TNC IF-IMV Specification 1.3
303 */
304 TNC_Result TNC_IMV_ReceiveMessage(TNC_IMVID imv_id,
305 TNC_ConnectionID connection_id,
306 TNC_BufferReference msg,
307 TNC_UInt32 msg_len,
308 TNC_MessageType msg_type)
309 {
310 imv_state_t *state;
311 imv_msg_t *in_msg;
312 TNC_Result result;
313
314 if (!imv_scanner)
315 {
316 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
317 return TNC_RESULT_NOT_INITIALIZED;
318 }
319 if (!imv_scanner->get_state(imv_scanner, connection_id, &state))
320 {
321 return TNC_RESULT_FATAL;
322 }
323
324 in_msg = imv_msg_create_from_data(imv_scanner, state, connection_id, msg_type,
325 chunk_create(msg, msg_len));
326 result = receive_message(state, in_msg);
327 in_msg->destroy(in_msg);
328
329 return result;
330 }
331
332 /**
333 * see section 3.8.6 of TCG TNC IF-IMV Specification 1.3
334 */
335 TNC_Result TNC_IMV_ReceiveMessageLong(TNC_IMVID imv_id,
336 TNC_ConnectionID connection_id,
337 TNC_UInt32 msg_flags,
338 TNC_BufferReference msg,
339 TNC_UInt32 msg_len,
340 TNC_VendorID msg_vid,
341 TNC_MessageSubtype msg_subtype,
342 TNC_UInt32 src_imc_id,
343 TNC_UInt32 dst_imv_id)
344 {
345 imv_state_t *state;
346 imv_msg_t *in_msg;
347 TNC_Result result;
348
349 if (!imv_scanner)
350 {
351 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
352 return TNC_RESULT_NOT_INITIALIZED;
353 }
354 if (!imv_scanner->get_state(imv_scanner, connection_id, &state))
355 {
356 return TNC_RESULT_FATAL;
357 }
358 in_msg = imv_msg_create_from_long_data(imv_scanner, state, connection_id,
359 src_imc_id, dst_imv_id, msg_vid, msg_subtype,
360 chunk_create(msg, msg_len));
361 result =receive_message(state, in_msg);
362 in_msg->destroy(in_msg);
363
364 return result;
365 }
366
367 /**
368 * see section 3.8.7 of TCG TNC IF-IMV Specification 1.3
369 */
370 TNC_Result TNC_IMV_SolicitRecommendation(TNC_IMVID imv_id,
371 TNC_ConnectionID connection_id)
372 {
373 imv_state_t *state;
374
375 if (!imv_scanner)
376 {
377 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
378 return TNC_RESULT_NOT_INITIALIZED;
379 }
380 if (!imv_scanner->get_state(imv_scanner, connection_id, &state))
381 {
382 return TNC_RESULT_FATAL;
383 }
384 return imv_scanner->provide_recommendation(imv_scanner, state);
385 }
386
387 /**
388 * see section 3.8.8 of TCG TNC IF-IMV Specification 1.3
389 */
390 TNC_Result TNC_IMV_BatchEnding(TNC_IMVID imv_id,
391 TNC_ConnectionID connection_id)
392 {
393 imv_state_t *state;
394 imv_msg_t *out_msg;
395 pa_tnc_attr_t *attr;
396 TNC_IMV_Action_Recommendation rec;
397 TNC_IMV_Evaluation_Result eval;
398 TNC_Result result = TNC_RESULT_SUCCESS;
399
400 if (!imv_scanner)
401 {
402 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
403 return TNC_RESULT_NOT_INITIALIZED;
404 }
405 if (!imv_scanner->get_state(imv_scanner, connection_id, &state))
406 {
407 return TNC_RESULT_FATAL;
408 }
409 state->get_recommendation(state, &rec, &eval);
410 if (rec == TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION)
411 {
412 out_msg = imv_msg_create(imv_scanner, state, connection_id, imv_id,
413 TNC_IMCID_ANY, msg_types[0]);
414 attr = ietf_attr_attr_request_create(PEN_IETF, IETF_ATTR_PORT_FILTER);
415 out_msg->add_attribute(out_msg, attr);
416
417 /* send PA-TNC message with excl flag not set */
418 result = out_msg->send(out_msg, FALSE);
419 out_msg->destroy(out_msg);
420
421 }
422 return result;
423 }
424
425 /**
426 * see section 3.8.9 of TCG TNC IF-IMV Specification 1.3
427 */
428 TNC_Result TNC_IMV_Terminate(TNC_IMVID imv_id)
429 {
430 if (!imv_scanner)
431 {
432 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
433 return TNC_RESULT_NOT_INITIALIZED;
434 }
435 tcp_ports->destroy_function(tcp_ports, free);
436 udp_ports->destroy_function(udp_ports, free);
437 imv_scanner->destroy(imv_scanner);
438 imv_scanner = NULL;
439
440 return TNC_RESULT_SUCCESS;
441 }
442
443 /**
444 * see section 4.2.8.1 of TCG TNC IF-IMV Specification 1.3
445 */
446 TNC_Result TNC_IMV_ProvideBindFunction(TNC_IMVID imv_id,
447 TNC_TNCS_BindFunctionPointer bind_function)
448 {
449 if (!imv_scanner)
450 {
451 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
452 return TNC_RESULT_NOT_INITIALIZED;
453 }
454 return imv_scanner->bind_functions(imv_scanner, bind_function);
455 }