977d657979503ca424880f53b0ccfd3747321835
[strongswan.git] / src / libimcv / plugins / imv_scanner / imv_scanner_agent.c
1 /*
2 * Copyright (C) 2013 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_agent.h"
17 #include "imv_scanner_state.h"
18
19 #include <imv/imv_agent.h>
20 #include <imv/imv_msg.h>
21 #include <ietf/ietf_attr.h>
22 #include <ietf/ietf_attr_attr_request.h>
23 #include <ietf/ietf_attr_pa_tnc_error.h>
24 #include <ietf/ietf_attr_port_filter.h>
25
26 #include <tncif_names.h>
27 #include <tncif_pa_subtypes.h>
28
29 #include <pen/pen.h>
30 #include <utils/debug.h>
31 #include <utils/lexparser.h>
32
33 typedef struct private_imv_scanner_agent_t private_imv_scanner_agent_t;
34
35 /* Subscribed PA-TNC message subtypes */
36 static pen_type_t msg_types[] = {
37 { PEN_IETF, PA_SUBTYPE_IETF_VPN }
38 };
39
40 /**
41 * Flag set when corresponding attribute has been received
42 */
43 typedef enum imv_scanner_attr_t imv_scanner_attr_t;
44
45 enum imv_scanner_attr_t {
46 IMV_SCANNER_ATTR_PORT_FILTER = (1<<0)
47 };
48
49 typedef struct port_range_t port_range_t;
50
51 struct port_range_t {
52 u_int16_t start, stop;
53 };
54
55 /**
56 * Default port policy
57 *
58 * TRUE: all server ports on the TNC client must be closed
59 * FALSE: any server port on the TNC client is allowed to be open
60 */
61 static bool closed_port_policy = TRUE;
62
63 /**
64 * List of TCP and UDP port ranges
65 *
66 * TRUE: server ports on the TNC client that are allowed to be open
67 * FALSE: server ports on the TNC client that must be closed
68 */
69 static linked_list_t *tcp_ports, *udp_ports;
70
71 /**
72 * Get a TCP or UDP port list from strongswan.conf
73 */
74 static linked_list_t* get_port_list(char *label)
75 {
76 char key[40], *value;
77 linked_list_t *list;
78 chunk_t port_list, port_item, port_start;
79 port_range_t *port_range;
80
81 list = linked_list_create();
82
83 snprintf(key, sizeof(key), "libimcv.plugins.imv-scanner.%s_ports", label);
84 value = lib->settings->get_str(lib->settings, key, NULL);
85 if (!value)
86 {
87 DBG1(DBG_IMV, "%s not defined", key);
88 return list;
89 }
90 port_list = chunk_create(value, strlen(value));
91 DBG2(DBG_IMV, "list of %s ports that %s:", label,
92 closed_port_policy ? "are allowed to be open" : "must be closed");
93
94 while (eat_whitespace(&port_list))
95 {
96 if (!extract_token(&port_item, ' ', &port_list))
97 {
98 /* reached last port item */
99 port_item = port_list;
100 port_list = chunk_empty;
101 }
102 port_range = malloc_thing(port_range_t);
103 port_range->start = atoi(port_item.ptr);
104
105 if (extract_token(&port_start, '-', &port_item) && port_item.len)
106 {
107 port_range->stop = atoi(port_item.ptr);
108 }
109 else
110 {
111 port_range->stop = port_range->start;
112 }
113 DBG2(DBG_IMV, "%5u - %5u", port_range->start, port_range->stop);
114 list->insert_last(list, port_range);
115 }
116
117 return list;
118 }
119
120 /**
121 * Private data of an imv_scanner_agent_t object.
122 */
123 struct private_imv_scanner_agent_t {
124
125 /**
126 * Public members of imv_scanner_agent_t
127 */
128 imv_agent_if_t public;
129
130 /**
131 * IMV agent responsible for generic functions
132 */
133 imv_agent_t *agent;
134
135 };
136
137 METHOD(imv_agent_if_t, bind_functions, TNC_Result,
138 private_imv_scanner_agent_t *this, TNC_TNCS_BindFunctionPointer bind_function)
139 {
140 return this->agent->bind_functions(this->agent, bind_function);
141 }
142
143 METHOD(imv_agent_if_t, notify_connection_change, TNC_Result,
144 private_imv_scanner_agent_t *this, TNC_ConnectionID id,
145 TNC_ConnectionState new_state)
146 {
147 imv_state_t *state;
148
149 switch (new_state)
150 {
151 case TNC_CONNECTION_STATE_CREATE:
152 state = imv_scanner_state_create(id);
153 return this->agent->create_state(this->agent, state);
154 case TNC_CONNECTION_STATE_DELETE:
155 return this->agent->delete_state(this->agent, id);
156 default:
157 return this->agent->change_state(this->agent, id, new_state, NULL);
158 }
159 }
160
161 /**
162 * Process a received message
163 */
164 static TNC_Result receive_msg(private_imv_scanner_agent_t *this,
165 imv_state_t *state, imv_msg_t *in_msg)
166 {
167 imv_msg_t *out_msg;
168 enumerator_t *enumerator;
169 pa_tnc_attr_t *attr;
170 pen_type_t type;
171 TNC_Result result;
172 bool fatal_error = FALSE;
173
174 /* parse received PA-TNC message and handle local and remote errors */
175 result = in_msg->receive(in_msg, &fatal_error);
176 if (result != TNC_RESULT_SUCCESS)
177 {
178 return result;
179 }
180
181 /* analyze PA-TNC attributes */
182 enumerator = in_msg->create_attribute_enumerator(in_msg);
183 while (enumerator->enumerate(enumerator, &attr))
184 {
185 type = attr->get_type(attr);
186
187 if (type.vendor_id == PEN_IETF && type.type == IETF_ATTR_PORT_FILTER)
188 {
189 imv_scanner_state_t *scanner_state;
190 ietf_attr_port_filter_t *attr_port_filter;
191 enumerator_t *enumerator;
192 u_int8_t protocol;
193 u_int16_t port;
194 bool blocked, compliant = TRUE;
195
196
197 scanner_state = (imv_scanner_state_t*)state;
198 scanner_state->set_received(scanner_state,
199 IMV_SCANNER_ATTR_PORT_FILTER);
200 attr_port_filter = (ietf_attr_port_filter_t*)attr;
201
202 enumerator = attr_port_filter->create_port_enumerator(attr_port_filter);
203 while (enumerator->enumerate(enumerator, &blocked, &protocol, &port))
204 {
205 enumerator_t *e;
206 port_range_t *port_range;
207 bool passed, found = FALSE;
208 char buf[20];
209
210 if (blocked)
211 {
212 /* ignore closed ports */
213 continue;
214 }
215
216 e = (protocol == IPPROTO_TCP) ?
217 tcp_ports->create_enumerator(tcp_ports) :
218 udp_ports->create_enumerator(udp_ports);
219 while (e->enumerate(e, &port_range))
220 {
221 if (port >= port_range->start && port <= port_range->stop)
222 {
223 found = TRUE;
224 break;
225 }
226 }
227 e->destroy(e);
228
229 passed = (closed_port_policy == found);
230 DBG2(DBG_IMV, "%s port %5u %s: %s",
231 (protocol == IPPROTO_TCP) ? "tcp" : "udp", port,
232 blocked ? "closed" : "open", passed ? "ok" : "fatal");
233 if (!passed)
234 {
235 compliant = FALSE;
236 snprintf(buf, sizeof(buf), "%s/%u",
237 (protocol == IPPROTO_TCP) ? "tcp" : "udp", port);
238 scanner_state->add_violating_port(scanner_state, strdup(buf));
239 }
240 }
241 enumerator->destroy(enumerator);
242
243 if (compliant)
244 {
245 state->set_recommendation(state,
246 TNC_IMV_ACTION_RECOMMENDATION_ALLOW,
247 TNC_IMV_EVALUATION_RESULT_COMPLIANT);
248 }
249 else
250 {
251 state->set_recommendation(state,
252 TNC_IMV_ACTION_RECOMMENDATION_NO_ACCESS,
253 TNC_IMV_EVALUATION_RESULT_NONCOMPLIANT_MAJOR);
254 }
255 }
256 }
257 enumerator->destroy(enumerator);
258
259 if (fatal_error)
260 {
261 state->set_recommendation(state,
262 TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION,
263 TNC_IMV_EVALUATION_RESULT_ERROR);
264 }
265
266 out_msg = imv_msg_create_as_reply(in_msg);
267 result = out_msg->send_assessment(out_msg);
268 out_msg->destroy(out_msg);
269 if (result != TNC_RESULT_SUCCESS)
270 {
271 return result;
272 }
273 return this->agent->provide_recommendation(this->agent, state);
274 }
275
276 METHOD(imv_agent_if_t, receive_message, TNC_Result,
277 private_imv_scanner_agent_t *this, TNC_ConnectionID id,
278 TNC_MessageType msg_type, chunk_t msg)
279 {
280 imv_state_t *state;
281 imv_msg_t *in_msg;
282 TNC_Result result;
283
284 if (!this->agent->get_state(this->agent, id, &state))
285 {
286 return TNC_RESULT_FATAL;
287 }
288 in_msg = imv_msg_create_from_data(this->agent, state, id, msg_type, msg);
289 result = receive_msg(this, state, in_msg);
290 in_msg->destroy(in_msg);
291
292 return result;
293 }
294
295 METHOD(imv_agent_if_t, receive_message_long, TNC_Result,
296 private_imv_scanner_agent_t *this, TNC_ConnectionID id,
297 TNC_UInt32 src_imc_id, TNC_UInt32 dst_imv_id,
298 TNC_VendorID msg_vid, TNC_MessageSubtype msg_subtype, chunk_t msg)
299 {
300 imv_state_t *state;
301 imv_msg_t *in_msg;
302 TNC_Result result;
303
304 if (!this->agent->get_state(this->agent, id, &state))
305 {
306 return TNC_RESULT_FATAL;
307 }
308 in_msg = imv_msg_create_from_long_data(this->agent, state, id,
309 src_imc_id, dst_imv_id, msg_vid, msg_subtype, msg);
310 result = receive_msg(this, state, in_msg);
311 in_msg->destroy(in_msg);
312
313 return result;
314
315 }
316
317 METHOD(imv_agent_if_t, batch_ending, TNC_Result,
318 private_imv_scanner_agent_t *this, TNC_ConnectionID id)
319 {
320 imv_state_t *state;
321 imv_msg_t *out_msg;
322 pa_tnc_attr_t *attr;
323 TNC_IMV_Action_Recommendation rec;
324 TNC_IMV_Evaluation_Result eval;
325 TNC_Result result = TNC_RESULT_SUCCESS;
326
327 if (!this->agent->get_state(this->agent, id, &state))
328 {
329 return TNC_RESULT_FATAL;
330 }
331 state->get_recommendation(state, &rec, &eval);
332 if (rec == TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION)
333 {
334 out_msg = imv_msg_create(this->agent, state, id,
335 this->agent->get_id(this->agent),
336 TNC_IMCID_ANY, msg_types[0]);
337 attr = ietf_attr_attr_request_create(PEN_IETF, IETF_ATTR_PORT_FILTER);
338 out_msg->add_attribute(out_msg, attr);
339
340 /* send PA-TNC message with excl flag not set */
341 result = out_msg->send(out_msg, FALSE);
342 out_msg->destroy(out_msg);
343 }
344 return result;
345 }
346
347 METHOD(imv_agent_if_t, solicit_recommendation, TNC_Result,
348 private_imv_scanner_agent_t *this, TNC_ConnectionID id)
349 {
350 imv_state_t *state;
351
352 if (!this->agent->get_state(this->agent, id, &state))
353 {
354 return TNC_RESULT_FATAL;
355 }
356 return this->agent->provide_recommendation(this->agent, state);
357 }
358
359 METHOD(imv_agent_if_t, destroy, void,
360 private_imv_scanner_agent_t *this)
361 {
362 tcp_ports->destroy_function(tcp_ports, free);
363 udp_ports->destroy_function(udp_ports, free);
364 this->agent->destroy(this->agent);
365 free(this);
366 }
367
368 /**
369 * Described in header.
370 */
371 imv_agent_if_t *imv_scanner_agent_create(const char *name, TNC_IMVID id,
372 TNC_Version *actual_version)
373 {
374 private_imv_scanner_agent_t *this;
375 imv_agent_t *agent;
376
377 agent = imv_agent_create(name, msg_types, countof(msg_types), id,
378 actual_version);
379 if (!agent)
380 {
381 return NULL;
382 }
383
384 INIT(this,
385 .public = {
386 .bind_functions = _bind_functions,
387 .notify_connection_change = _notify_connection_change,
388 .receive_message = _receive_message,
389 .receive_message_long = _receive_message_long,
390 .batch_ending = _batch_ending,
391 .solicit_recommendation = _solicit_recommendation,
392 .destroy = _destroy,
393 },
394 .agent = agent,
395 );
396
397 /* set the default port policy to closed (TRUE) or open (FALSE) */
398 closed_port_policy = lib->settings->get_bool(lib->settings,
399 "libimcv.plugins.imv-scanner.closed_port_policy", TRUE);
400 DBG2(DBG_IMV, "default port policy is %s ports",
401 closed_port_policy ? "closed" : "open");
402
403 /* get the list of open|closed ports */
404 tcp_ports = get_port_list("tcp");
405 udp_ports = get_port_list("udp");
406
407 return &this->public;
408 }
409