d1e09313762e2d97198ace554c4b3223a0396360
[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 <imcv.h>
20 #include <imv/imv_agent.h>
21 #include <imv/imv_msg.h>
22 #include <ietf/ietf_attr.h>
23 #include <ietf/ietf_attr_attr_request.h>
24 #include <ietf/ietf_attr_pa_tnc_error.h>
25 #include <ietf/ietf_attr_port_filter.h>
26
27 #include <tncif_names.h>
28 #include <tncif_pa_subtypes.h>
29
30 #include <pen/pen.h>
31 #include <utils/debug.h>
32 #include <utils/lexparser.h>
33
34 typedef struct private_imv_scanner_agent_t private_imv_scanner_agent_t;
35
36 /* Subscribed PA-TNC message subtypes */
37 static pen_type_t msg_types[] = {
38 { PEN_IETF, PA_SUBTYPE_IETF_VPN }
39 };
40
41 /**
42 * Private data of an imv_scanner_agent_t object.
43 */
44 struct private_imv_scanner_agent_t {
45
46 /**
47 * Public members of imv_scanner_agent_t
48 */
49 imv_agent_if_t public;
50
51 /**
52 * IMV agent responsible for generic functions
53 */
54 imv_agent_t *agent;
55
56 };
57
58 METHOD(imv_agent_if_t, bind_functions, TNC_Result,
59 private_imv_scanner_agent_t *this, TNC_TNCS_BindFunctionPointer bind_function)
60 {
61 return this->agent->bind_functions(this->agent, bind_function);
62 }
63
64 METHOD(imv_agent_if_t, notify_connection_change, TNC_Result,
65 private_imv_scanner_agent_t *this, TNC_ConnectionID id,
66 TNC_ConnectionState new_state)
67 {
68 imv_state_t *state;
69
70 switch (new_state)
71 {
72 case TNC_CONNECTION_STATE_CREATE:
73 state = imv_scanner_state_create(id);
74 return this->agent->create_state(this->agent, state);
75 case TNC_CONNECTION_STATE_DELETE:
76 return this->agent->delete_state(this->agent, id);
77 default:
78 return this->agent->change_state(this->agent, id, new_state, NULL);
79 }
80 }
81
82 /**
83 * Process a received message
84 */
85 static TNC_Result receive_msg(private_imv_scanner_agent_t *this,
86 imv_state_t *state, imv_msg_t *in_msg)
87 {
88 imv_msg_t *out_msg;
89 imv_scanner_state_t *scanner_state;
90 enumerator_t *enumerator;
91 pa_tnc_attr_t *attr;
92 pen_type_t type;
93 TNC_Result result;
94 ietf_attr_port_filter_t *port_filter_attr;
95 bool fatal_error = FALSE;
96
97 /* parse received PA-TNC message and handle local and remote errors */
98 result = in_msg->receive(in_msg, &fatal_error);
99 if (result != TNC_RESULT_SUCCESS)
100 {
101 return result;
102 }
103
104 /* analyze PA-TNC attributes */
105 enumerator = in_msg->create_attribute_enumerator(in_msg);
106 while (enumerator->enumerate(enumerator, &attr))
107 {
108 type = attr->get_type(attr);
109
110 if (type.vendor_id == PEN_IETF && type.type == IETF_ATTR_PORT_FILTER)
111 {
112 scanner_state = (imv_scanner_state_t*)state;
113 port_filter_attr = (ietf_attr_port_filter_t*)attr->get_ref(attr);
114 scanner_state->set_port_filter_attr(scanner_state, port_filter_attr);
115 }
116 }
117 enumerator->destroy(enumerator);
118
119 if (fatal_error)
120 {
121 state->set_recommendation(state,
122 TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION,
123 TNC_IMV_EVALUATION_RESULT_ERROR);
124 out_msg = imv_msg_create_as_reply(in_msg);
125 result = out_msg->send_assessment(out_msg);
126 out_msg->destroy(out_msg);
127 if (result != TNC_RESULT_SUCCESS)
128 {
129 return result;
130 }
131 return this->agent->provide_recommendation(this->agent, state);
132 }
133
134 return TNC_RESULT_SUCCESS;
135 }
136
137 METHOD(imv_agent_if_t, receive_message, TNC_Result,
138 private_imv_scanner_agent_t *this, TNC_ConnectionID id,
139 TNC_MessageType msg_type, chunk_t msg)
140 {
141 imv_state_t *state;
142 imv_msg_t *in_msg;
143 TNC_Result result;
144
145 if (!this->agent->get_state(this->agent, id, &state))
146 {
147 return TNC_RESULT_FATAL;
148 }
149 in_msg = imv_msg_create_from_data(this->agent, state, id, msg_type, msg);
150 result = receive_msg(this, state, in_msg);
151 in_msg->destroy(in_msg);
152
153 return result;
154 }
155
156 METHOD(imv_agent_if_t, receive_message_long, TNC_Result,
157 private_imv_scanner_agent_t *this, TNC_ConnectionID id,
158 TNC_UInt32 src_imc_id, TNC_UInt32 dst_imv_id,
159 TNC_VendorID msg_vid, TNC_MessageSubtype msg_subtype, chunk_t msg)
160 {
161 imv_state_t *state;
162 imv_msg_t *in_msg;
163 TNC_Result result;
164
165 if (!this->agent->get_state(this->agent, id, &state))
166 {
167 return TNC_RESULT_FATAL;
168 }
169 in_msg = imv_msg_create_from_long_data(this->agent, state, id,
170 src_imc_id, dst_imv_id, msg_vid, msg_subtype, msg);
171 result = receive_msg(this, state, in_msg);
172 in_msg->destroy(in_msg);
173
174 return result;
175
176 }
177
178 typedef struct port_range_t port_range_t;
179
180 struct port_range_t {
181 u_int16_t start, stop;
182 };
183
184 /**
185 * Parse a TCP or UDP port list from an argument string
186 */
187 static linked_list_t* get_port_list(u_int8_t protocol_family,
188 bool closed_port_policy, char *arg_str)
189 {
190 chunk_t port_list, port_item, port_start;
191 port_range_t *port_range;
192 linked_list_t *list;
193
194 list = linked_list_create();
195
196 port_list = chunk_from_str(arg_str);
197 DBG2(DBG_IMV, "list of %s ports that %s:",
198 (protocol_family == IPPROTO_TCP) ? "tcp" : "udp",
199 closed_port_policy ? "are allowed to be open" : "must be closed");
200
201 while (eat_whitespace(&port_list))
202 {
203 if (!extract_token(&port_item, ' ', &port_list))
204 {
205 /* reached last port item */
206 port_item = port_list;
207 port_list = chunk_empty;
208 }
209 port_range = malloc_thing(port_range_t);
210 port_range->start = atoi(port_item.ptr);
211
212 if (extract_token(&port_start, '-', &port_item) && port_item.len)
213 {
214 port_range->stop = atoi(port_item.ptr);
215 }
216 else
217 {
218 port_range->stop = port_range->start;
219 }
220 DBG2(DBG_IMV, "%5u - %5u", port_range->start, port_range->stop);
221 list->insert_last(list, port_range);
222 }
223
224 return list;
225 }
226
227 METHOD(imv_agent_if_t, batch_ending, TNC_Result,
228 private_imv_scanner_agent_t *this, TNC_ConnectionID id)
229 {
230 imv_msg_t *out_msg;
231 imv_state_t *state;
232 imv_session_t *session;
233 imv_workitem_t *workitem;
234 imv_scanner_state_t *scanner_state;
235 imv_scanner_handshake_state_t handshake_state;
236 pa_tnc_attr_t *attr;
237 ietf_attr_port_filter_t *port_filter_attr;
238 TNC_IMVID imv_id;
239 TNC_Result result = TNC_RESULT_SUCCESS;
240 bool no_workitems = TRUE;
241 enumerator_t *enumerator;
242
243 if (!this->agent->get_state(this->agent, id, &state))
244 {
245 return TNC_RESULT_FATAL;
246 }
247 scanner_state = (imv_scanner_state_t*)state;
248 handshake_state = scanner_state->get_handshake_state(scanner_state);
249 port_filter_attr = scanner_state->get_port_filter_attr(scanner_state);
250 session = state->get_session(state);
251 imv_id = this->agent->get_id(this->agent);
252
253 if (handshake_state == IMV_SCANNER_STATE_END)
254 {
255 return TNC_RESULT_SUCCESS;
256 }
257
258 /* create an empty out message - we might need it */
259 out_msg = imv_msg_create(this->agent, state, id, imv_id, TNC_IMCID_ANY,
260 msg_types[0]);
261
262 if (!session)
263 {
264 DBG2(DBG_IMV, "no workitems available - no evaluation possible");
265 state->set_recommendation(state,
266 TNC_IMV_ACTION_RECOMMENDATION_ALLOW,
267 TNC_IMV_EVALUATION_RESULT_DONT_KNOW);
268 result = out_msg->send_assessment(out_msg);
269 out_msg->destroy(out_msg);
270 scanner_state->set_handshake_state(scanner_state, IMV_SCANNER_STATE_END);
271
272 if (result != TNC_RESULT_SUCCESS)
273 {
274 return result;
275 }
276 return this->agent->provide_recommendation(this->agent, state);
277 }
278
279 if (handshake_state == IMV_SCANNER_STATE_INIT)
280 {
281 enumerator = session->create_workitem_enumerator(session);
282 if (enumerator)
283 {
284 while (enumerator->enumerate(enumerator, &workitem))
285 {
286 if (workitem->get_imv_id(workitem) != TNC_IMVID_ANY)
287 {
288 continue;
289 }
290
291 switch (workitem->get_type(workitem))
292 {
293 case IMV_WORKITEM_TCP_PORT_OPEN:
294 case IMV_WORKITEM_TCP_PORT_BLOCK:
295 case IMV_WORKITEM_UDP_PORT_OPEN:
296 case IMV_WORKITEM_UDP_PORT_BLOCK:
297 if (!port_filter_attr &&
298 handshake_state != IMV_SCANNER_STATE_ATTR_REQ)
299 {
300 attr = ietf_attr_attr_request_create(PEN_IETF,
301 IETF_ATTR_PORT_FILTER);
302 out_msg->add_attribute(out_msg, attr);
303 handshake_state = IMV_SCANNER_STATE_ATTR_REQ;
304 }
305 break;
306 default:
307 continue;
308 }
309 workitem->set_imv_id(workitem, imv_id);
310 no_workitems = FALSE;
311 }
312 enumerator->destroy(enumerator);
313
314 if (no_workitems)
315 {
316 DBG2(DBG_IMV, "IMV %d has no workitems - "
317 "no evaluation requested", imv_id);
318 state->set_recommendation(state,
319 TNC_IMV_ACTION_RECOMMENDATION_ALLOW,
320 TNC_IMV_EVALUATION_RESULT_DONT_KNOW);
321 }
322 handshake_state = IMV_SCANNER_STATE_WORKITEMS;
323 scanner_state->set_handshake_state(scanner_state, handshake_state);
324 }
325 }
326
327 if (handshake_state == IMV_SCANNER_STATE_WORKITEMS && port_filter_attr)
328 {
329 TNC_IMV_Evaluation_Result eval;
330 TNC_IMV_Action_Recommendation rec;
331 u_int8_t protocol_family, protocol;
332 u_int16_t port;
333 bool closed_port_policy, blocked, first;
334 char result_str[BUF_LEN], *pos, *protocol_str;
335 size_t len;
336 int written;
337 linked_list_t *port_list;
338 enumerator_t *e1, *e2;
339
340 enumerator = session->create_workitem_enumerator(session);
341 while (enumerator->enumerate(enumerator, &workitem))
342 {
343 if (workitem->get_imv_id(workitem) != imv_id)
344 {
345 continue;
346 }
347 eval = TNC_IMV_EVALUATION_RESULT_COMPLIANT;
348
349 switch (workitem->get_type(workitem))
350 {
351 case IMV_WORKITEM_TCP_PORT_OPEN:
352 protocol_family = IPPROTO_TCP;
353 closed_port_policy = TRUE;
354 break;
355 case IMV_WORKITEM_TCP_PORT_BLOCK:
356 protocol_family = IPPROTO_TCP;
357 closed_port_policy = FALSE;
358 break;
359 case IMV_WORKITEM_UDP_PORT_OPEN:
360 protocol_family = IPPROTO_UDP;
361 closed_port_policy = TRUE;
362 break;
363 case IMV_WORKITEM_UDP_PORT_BLOCK:
364 protocol_family = IPPROTO_UDP;
365 closed_port_policy = FALSE;
366 break;
367 default:
368 continue;
369 }
370 port_list = get_port_list(protocol_family, closed_port_policy,
371 workitem->get_arg_str(workitem));
372 protocol_str = (protocol_family == IPPROTO_TCP) ? "tcp" : "udp";
373 result_str[0] = '\0';
374 pos = result_str;
375 len = BUF_LEN;
376 first = TRUE;
377
378 e1 = port_filter_attr->create_port_enumerator(port_filter_attr);
379 while (e1->enumerate(e1, &blocked, &protocol, &port))
380 {
381 port_range_t *port_range;
382 bool passed, found = FALSE;
383 char buf[20];
384
385 if (blocked || protocol != protocol_family)
386 {
387 /* ignore closed ports or non-matching protocols */
388 continue;
389 }
390
391 e2 = port_list->create_enumerator(port_list);
392 while (e2->enumerate(e2, &port_range))
393 {
394 if (port >= port_range->start && port <= port_range->stop)
395 {
396 found = TRUE;
397 break;
398 }
399 }
400 e2->destroy(e2);
401
402 passed = (closed_port_policy == found);
403 DBG2(DBG_IMV, "%s port %5u open: %s", protocol_str, port,
404 passed ? "ok" : "fatal");
405 if (!passed)
406 {
407 eval = TNC_IMV_EVALUATION_RESULT_NONCOMPLIANT_MINOR;
408 snprintf(buf, sizeof(buf), "%s/%u", protocol_str, port);
409 scanner_state->add_violating_port(scanner_state, strdup(buf));
410 if (first)
411 {
412 written = snprintf(pos, len, "violating %s ports:",
413 protocol_str);
414 if (written > 0 && written < len)
415 {
416 pos += written;
417 len -= written;
418 }
419 first = FALSE;
420 }
421 written = snprintf(pos, len, " %u", port);
422 if (written < 0 || written >= len)
423 {
424 pos += len - 1;
425 *pos = '\0';
426 }
427 else
428 {
429 pos += written;
430 len -= written;
431 }
432 }
433 }
434 e1->destroy(e1);
435
436 if (first)
437 {
438 snprintf(pos, len, "no violating %s ports", protocol_str);
439 }
440 port_list->destroy(port_list);
441
442 session->remove_workitem(session, enumerator);
443 rec = workitem->set_result(workitem, result_str, eval);
444 state->update_recommendation(state, rec, eval);
445 imcv_db->finalize_workitem(imcv_db, workitem);
446 workitem->destroy(workitem);
447 }
448 enumerator->destroy(enumerator);
449 }
450
451 /* finalized all workitems ? */
452 if (handshake_state == IMV_SCANNER_STATE_WORKITEMS &&
453 session->get_workitem_count(session, imv_id) == 0)
454 {
455 result = out_msg->send_assessment(out_msg);
456 out_msg->destroy(out_msg);
457 scanner_state->set_handshake_state(scanner_state, IMV_SCANNER_STATE_END);
458
459 if (result != TNC_RESULT_SUCCESS)
460 {
461 return result;
462 }
463 return this->agent->provide_recommendation(this->agent, state);
464 }
465
466 /* send non-empty PA-TNC message with excl flag not set */
467 if (out_msg->get_attribute_count(out_msg))
468 {
469 result = out_msg->send(out_msg, FALSE);
470 }
471 out_msg->destroy(out_msg);
472
473 return result;
474 }
475
476 METHOD(imv_agent_if_t, solicit_recommendation, TNC_Result,
477 private_imv_scanner_agent_t *this, TNC_ConnectionID id)
478 {
479 imv_state_t *state;
480
481 if (!this->agent->get_state(this->agent, id, &state))
482 {
483 return TNC_RESULT_FATAL;
484 }
485 return this->agent->provide_recommendation(this->agent, state);
486 }
487
488 METHOD(imv_agent_if_t, destroy, void,
489 private_imv_scanner_agent_t *this)
490 {
491 this->agent->destroy(this->agent);
492 free(this);
493 }
494
495 /**
496 * Described in header.
497 */
498 imv_agent_if_t *imv_scanner_agent_create(const char *name, TNC_IMVID id,
499 TNC_Version *actual_version)
500 {
501 private_imv_scanner_agent_t *this;
502 imv_agent_t *agent;
503
504 agent = imv_agent_create(name, msg_types, countof(msg_types), id,
505 actual_version);
506 if (!agent)
507 {
508 return NULL;
509 }
510
511 INIT(this,
512 .public = {
513 .bind_functions = _bind_functions,
514 .notify_connection_change = _notify_connection_change,
515 .receive_message = _receive_message,
516 .receive_message_long = _receive_message_long,
517 .batch_ending = _batch_ending,
518 .solicit_recommendation = _solicit_recommendation,
519 .destroy = _destroy,
520 },
521 .agent = agent,
522 );
523
524 return &this->public;
525 }
526