0415f52d498f3b07d643920640fb054a99167bb4
[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 state->set_action_flags(state, IMV_SCANNER_ATTR_PORT_FILTER);
197 scanner_state = (imv_scanner_state_t*)state;
198 attr_port_filter = (ietf_attr_port_filter_t*)attr;
199
200 enumerator = attr_port_filter->create_port_enumerator(attr_port_filter);
201 while (enumerator->enumerate(enumerator, &blocked, &protocol, &port))
202 {
203 enumerator_t *e;
204 port_range_t *port_range;
205 bool passed, found = FALSE;
206 char buf[20];
207
208 if (blocked)
209 {
210 /* ignore closed ports */
211 continue;
212 }
213
214 e = (protocol == IPPROTO_TCP) ?
215 tcp_ports->create_enumerator(tcp_ports) :
216 udp_ports->create_enumerator(udp_ports);
217 while (e->enumerate(e, &port_range))
218 {
219 if (port >= port_range->start && port <= port_range->stop)
220 {
221 found = TRUE;
222 break;
223 }
224 }
225 e->destroy(e);
226
227 passed = (closed_port_policy == found);
228 DBG2(DBG_IMV, "%s port %5u %s: %s",
229 (protocol == IPPROTO_TCP) ? "tcp" : "udp", port,
230 blocked ? "closed" : "open", passed ? "ok" : "fatal");
231 if (!passed)
232 {
233 compliant = FALSE;
234 snprintf(buf, sizeof(buf), "%s/%u",
235 (protocol == IPPROTO_TCP) ? "tcp" : "udp", port);
236 scanner_state->add_violating_port(scanner_state, strdup(buf));
237 }
238 }
239 enumerator->destroy(enumerator);
240
241 if (compliant)
242 {
243 state->set_recommendation(state,
244 TNC_IMV_ACTION_RECOMMENDATION_ALLOW,
245 TNC_IMV_EVALUATION_RESULT_COMPLIANT);
246 }
247 else
248 {
249 state->set_recommendation(state,
250 TNC_IMV_ACTION_RECOMMENDATION_NO_ACCESS,
251 TNC_IMV_EVALUATION_RESULT_NONCOMPLIANT_MAJOR);
252 }
253 }
254 }
255 enumerator->destroy(enumerator);
256
257 if (fatal_error)
258 {
259 state->set_recommendation(state,
260 TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION,
261 TNC_IMV_EVALUATION_RESULT_ERROR);
262 }
263
264 out_msg = imv_msg_create_as_reply(in_msg);
265 result = out_msg->send_assessment(out_msg);
266 out_msg->destroy(out_msg);
267 if (result != TNC_RESULT_SUCCESS)
268 {
269 return result;
270 }
271 return this->agent->provide_recommendation(this->agent, state);
272 }
273
274 METHOD(imv_agent_if_t, receive_message, TNC_Result,
275 private_imv_scanner_agent_t *this, TNC_ConnectionID id,
276 TNC_MessageType msg_type, chunk_t msg)
277 {
278 imv_state_t *state;
279 imv_msg_t *in_msg;
280 TNC_Result result;
281
282 if (!this->agent->get_state(this->agent, id, &state))
283 {
284 return TNC_RESULT_FATAL;
285 }
286 in_msg = imv_msg_create_from_data(this->agent, state, id, msg_type, msg);
287 result = receive_msg(this, state, in_msg);
288 in_msg->destroy(in_msg);
289
290 return result;
291 }
292
293 METHOD(imv_agent_if_t, receive_message_long, TNC_Result,
294 private_imv_scanner_agent_t *this, TNC_ConnectionID id,
295 TNC_UInt32 src_imc_id, TNC_UInt32 dst_imv_id,
296 TNC_VendorID msg_vid, TNC_MessageSubtype msg_subtype, chunk_t msg)
297 {
298 imv_state_t *state;
299 imv_msg_t *in_msg;
300 TNC_Result result;
301
302 if (!this->agent->get_state(this->agent, id, &state))
303 {
304 return TNC_RESULT_FATAL;
305 }
306 in_msg = imv_msg_create_from_long_data(this->agent, state, id,
307 src_imc_id, dst_imv_id, msg_vid, msg_subtype, msg);
308 result = receive_msg(this, state, in_msg);
309 in_msg->destroy(in_msg);
310
311 return result;
312
313 }
314
315 METHOD(imv_agent_if_t, batch_ending, TNC_Result,
316 private_imv_scanner_agent_t *this, TNC_ConnectionID id)
317 {
318 imv_state_t *state;
319 imv_msg_t *out_msg;
320 pa_tnc_attr_t *attr;
321 TNC_IMV_Action_Recommendation rec;
322 TNC_IMV_Evaluation_Result eval;
323 TNC_Result result = TNC_RESULT_SUCCESS;
324
325 if (!this->agent->get_state(this->agent, id, &state))
326 {
327 return TNC_RESULT_FATAL;
328 }
329 state->get_recommendation(state, &rec, &eval);
330 if (rec == TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION)
331 {
332 out_msg = imv_msg_create(this->agent, state, id,
333 this->agent->get_id(this->agent),
334 TNC_IMCID_ANY, msg_types[0]);
335 attr = ietf_attr_attr_request_create(PEN_IETF, IETF_ATTR_PORT_FILTER);
336 out_msg->add_attribute(out_msg, attr);
337
338 /* send PA-TNC message with excl flag not set */
339 result = out_msg->send(out_msg, FALSE);
340 out_msg->destroy(out_msg);
341 }
342 return result;
343 }
344
345 METHOD(imv_agent_if_t, solicit_recommendation, TNC_Result,
346 private_imv_scanner_agent_t *this, TNC_ConnectionID id)
347 {
348 imv_state_t *state;
349
350 if (!this->agent->get_state(this->agent, id, &state))
351 {
352 return TNC_RESULT_FATAL;
353 }
354 return this->agent->provide_recommendation(this->agent, state);
355 }
356
357 METHOD(imv_agent_if_t, destroy, void,
358 private_imv_scanner_agent_t *this)
359 {
360 tcp_ports->destroy_function(tcp_ports, free);
361 udp_ports->destroy_function(udp_ports, free);
362 this->agent->destroy(this->agent);
363 free(this);
364 }
365
366 /**
367 * Described in header.
368 */
369 imv_agent_if_t *imv_scanner_agent_create(const char *name, TNC_IMVID id,
370 TNC_Version *actual_version)
371 {
372 private_imv_scanner_agent_t *this;
373 imv_agent_t *agent;
374
375 agent = imv_agent_create(name, msg_types, countof(msg_types), id,
376 actual_version);
377 if (!agent)
378 {
379 return NULL;
380 }
381
382 INIT(this,
383 .public = {
384 .bind_functions = _bind_functions,
385 .notify_connection_change = _notify_connection_change,
386 .receive_message = _receive_message,
387 .receive_message_long = _receive_message_long,
388 .batch_ending = _batch_ending,
389 .solicit_recommendation = _solicit_recommendation,
390 .destroy = _destroy,
391 },
392 .agent = agent,
393 );
394
395 /* set the default port policy to closed (TRUE) or open (FALSE) */
396 closed_port_policy = lib->settings->get_bool(lib->settings,
397 "libimcv.plugins.imv-scanner.closed_port_policy", TRUE);
398 DBG2(DBG_IMV, "default port policy is %s ports",
399 closed_port_policy ? "closed" : "open");
400
401 /* get the list of open|closed ports */
402 tcp_ports = get_port_list("tcp");
403 udp_ports = get_port_list("udp");
404
405 return &this->public;
406 }
407