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