8e843a2ceafe1833c5f5ae6caba7108c4fb7cfdb
[strongswan.git] / src / libimcv / plugins / imc_scanner / imc_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 "imc_scanner_state.h"
17
18 #include <imc/imc_agent.h>
19 #include <imc/imc_msg.h>
20 #include <ietf/ietf_attr.h>
21 #include <ietf/ietf_attr_attr_request.h>
22 #include <ietf/ietf_attr_port_filter.h>
23
24 #include <tncif_pa_subtypes.h>
25
26 #include <pen/pen.h>
27 #include <utils/lexparser.h>
28 #include <utils/debug.h>
29
30 #include <stdio.h>
31
32 /* IMC definitions */
33
34 static const char imc_name[] = "Scanner";
35
36 static pen_type_t msg_types[] = {
37 { PEN_IETF, PA_SUBTYPE_IETF_VPN }
38 };
39
40 static imc_agent_t *imc_scanner;
41
42 /**
43 * see section 3.8.1 of TCG TNC IF-IMC Specification 1.3
44 */
45 TNC_Result TNC_IMC_Initialize(TNC_IMCID imc_id,
46 TNC_Version min_version,
47 TNC_Version max_version,
48 TNC_Version *actual_version)
49 {
50 if (imc_scanner)
51 {
52 DBG1(DBG_IMC, "IMC \"%s\" has already been initialized", imc_name);
53 return TNC_RESULT_ALREADY_INITIALIZED;
54 }
55 imc_scanner = imc_agent_create(imc_name, msg_types, 1, imc_id, actual_version);
56 if (!imc_scanner)
57 {
58 return TNC_RESULT_FATAL;
59 }
60 if (min_version > TNC_IFIMC_VERSION_1 || max_version < TNC_IFIMC_VERSION_1)
61 {
62 DBG1(DBG_IMC, "no common IF-IMC version");
63 return TNC_RESULT_NO_COMMON_VERSION;
64 }
65 return TNC_RESULT_SUCCESS;
66 }
67
68 /**
69 * see section 3.8.2 of TCG TNC IF-IMC Specification 1.3
70 */
71 TNC_Result TNC_IMC_NotifyConnectionChange(TNC_IMCID imc_id,
72 TNC_ConnectionID connection_id,
73 TNC_ConnectionState new_state)
74 {
75 imc_state_t *state;
76
77 if (!imc_scanner)
78 {
79 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
80 return TNC_RESULT_NOT_INITIALIZED;
81 }
82 switch (new_state)
83 {
84 case TNC_CONNECTION_STATE_CREATE:
85 state = imc_scanner_state_create(connection_id);
86 return imc_scanner->create_state(imc_scanner, state);
87 case TNC_CONNECTION_STATE_HANDSHAKE:
88 if (imc_scanner->change_state(imc_scanner, connection_id, new_state,
89 &state) != TNC_RESULT_SUCCESS)
90 {
91 return TNC_RESULT_FATAL;
92 }
93 state->set_result(state, imc_id,
94 TNC_IMV_EVALUATION_RESULT_DONT_KNOW);
95 return TNC_RESULT_SUCCESS;
96 case TNC_CONNECTION_STATE_DELETE:
97 return imc_scanner->delete_state(imc_scanner, connection_id);
98 default:
99 return imc_scanner->change_state(imc_scanner, connection_id,
100 new_state, NULL);
101 }
102 }
103
104 /**
105 * Determine all TCP and UDP server sockets listening on physical interfaces
106 */
107 static bool do_netstat(ietf_attr_port_filter_t *attr)
108 {
109 FILE *file;
110 char buf[BUF_LEN];
111 chunk_t line, token;
112 int n = 0;
113 bool success = FALSE;
114 const char loopback_v4[] = "127.0.0.1";
115 const char loopback_v6[] = "::1";
116
117 /* Open a pipe stream for reading the output of the netstat commmand */
118 file = popen("/bin/netstat -n -l -p -4 -6 --inet", "r");
119 if (!file)
120 {
121 DBG1(DBG_IMC, "Failed to run netstat command");
122 return FALSE;
123 }
124
125 /* Read the output a line at a time */
126 while (fgets(buf, BUF_LEN-1, file))
127 {
128 u_char *pos;
129 u_int8_t new_protocol, protocol;
130 u_int16_t new_port, port;
131 int i;
132 enumerator_t *enumerator;
133 bool allowed, found = FALSE;
134
135 DBG2(DBG_IMC, "%.*s", (int)(strlen(buf)-1), buf);
136
137 if (n++ < 2)
138 {
139 /* skip the first two header lines */
140 continue;
141 }
142 line = chunk_create(buf, strlen(buf));
143
144 /* Extract the IP protocol type */
145 if (!extract_token(&token, ' ', &line))
146 {
147 DBG1(DBG_IMC, "Protocol field in netstat output not found");
148 goto end;
149 }
150 if (match("tcp", &token) || match("tcp6", &token))
151 {
152 new_protocol = IPPROTO_TCP;
153 }
154 else if (match("udp", &token) || match("udp6", &token))
155 {
156 new_protocol = IPPROTO_UDP;
157 }
158 else
159 {
160 DBG1(DBG_IMC, "Skipped unknown IP protocol in netstat output");
161 continue;
162 }
163
164 /* Skip the Recv-Q and Send-Q fields */
165 for (i = 0; i < 3; i++)
166 {
167 if (!eat_whitespace(&line) || !extract_token(&token, ' ', &line))
168 {
169 token = chunk_empty;
170 break;
171 }
172 }
173 if (token.len == 0)
174 {
175 DBG1(DBG_IMC, "Local Address field in netstat output not found");
176 goto end;
177 }
178
179 /* Find the local port appended to the local address */
180 pos = token.ptr + token.len;
181 while (*--pos != ':' && --token.len);
182 if (*pos != ':')
183 {
184 DBG1(DBG_IMC, "Local port field in netstat output not found");
185 goto end;
186 }
187 token.len--;
188
189 /* ignore ports of IPv4 and IPv6 loopback interfaces */
190 if ((token.len == strlen(loopback_v4) &&
191 memeq(loopback_v4, token.ptr, token.len)) ||
192 (token.len == strlen(loopback_v6) &&
193 memeq(loopback_v6, token.ptr, token.len)))
194 {
195 continue;
196 }
197
198 /* convert the port string to an integer */
199 new_port = atoi(pos+1);
200
201 /* check if the there is already a port entry */
202 enumerator = attr->create_port_enumerator(attr);
203 while (enumerator->enumerate(enumerator, &allowed, &protocol, &port))
204 {
205 if (new_port == port && new_protocol == protocol)
206 {
207 found = TRUE;
208 }
209 }
210 enumerator->destroy(enumerator);
211
212 /* Skip the duplicate port entry */
213 if (found)
214 {
215 continue;
216 }
217
218 /* Add new port entry */
219 attr->add_port(attr, FALSE, new_protocol, new_port);
220 }
221
222 /* Successfully completed the parsing of the netstat output */
223 success = TRUE;
224
225 end:
226 /* Close the pipe stream */
227 pclose(file);
228 return success;
229 }
230
231 /**
232 * Add IETF Port Filter attribute to the send queue
233 */
234 static TNC_Result add_port_filter(imc_msg_t *msg)
235 {
236 pa_tnc_attr_t *attr;
237 ietf_attr_port_filter_t *attr_port_filter;
238
239 attr = ietf_attr_port_filter_create();
240 attr->set_noskip_flag(attr, TRUE);
241 attr_port_filter = (ietf_attr_port_filter_t*)attr;
242 if (!do_netstat(attr_port_filter))
243 {
244 attr->destroy(attr);
245 return TNC_RESULT_FATAL;
246 }
247 msg->add_attribute(msg, attr);
248
249 return TNC_RESULT_SUCCESS;
250 }
251
252 /**
253 * see section 3.8.3 of TCG TNC IF-IMC Specification 1.3
254 */
255 TNC_Result TNC_IMC_BeginHandshake(TNC_IMCID imc_id,
256 TNC_ConnectionID connection_id)
257 {
258 imc_state_t *state;
259 imc_msg_t *out_msg;
260 TNC_Result result = TNC_RESULT_SUCCESS;
261
262 if (!imc_scanner)
263 {
264 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
265 return TNC_RESULT_NOT_INITIALIZED;
266 }
267 if (!imc_scanner->get_state(imc_scanner, connection_id, &state))
268 {
269 return TNC_RESULT_FATAL;
270 }
271 if (lib->settings->get_bool(lib->settings,
272 "libimcv.plugins.imc-scanner.send_ports", TRUE))
273 {
274 out_msg = imc_msg_create(imc_scanner, state, connection_id, imc_id,
275 TNC_IMVID_ANY, msg_types[0]);
276 result = add_port_filter(out_msg);
277 if (result == TNC_RESULT_SUCCESS)
278 {
279 /* send PA-TNC message with the excl flag not set */
280 result = out_msg->send(out_msg, FALSE);
281 }
282 out_msg->destroy(out_msg);
283 }
284
285 return result;
286 }
287
288 static TNC_Result receive_message(imc_msg_t *in_msg)
289 {
290 imc_msg_t *out_msg;
291 enumerator_t *enumerator;
292 pa_tnc_attr_t *attr;
293 pen_type_t attr_type;
294 TNC_Result result = TNC_RESULT_SUCCESS;
295 bool fatal_error = FALSE;
296
297 /* parse received PA-TNC message and handle local and remote errors */
298 result = in_msg->receive(in_msg, &fatal_error);
299 if (result != TNC_RESULT_SUCCESS)
300 {
301 return result;
302 }
303 out_msg = imc_msg_create_as_reply(in_msg);
304
305 /* analyze PA-TNC attributes */
306 enumerator = in_msg->create_attribute_enumerator(in_msg);
307 while (enumerator->enumerate(enumerator, &attr))
308 {
309 attr_type = attr->get_type(attr);
310
311 if (attr_type.vendor_id != PEN_IETF)
312 {
313 continue;
314 }
315 if (attr_type.type == IETF_ATTR_ATTRIBUTE_REQUEST)
316 {
317 ietf_attr_attr_request_t *attr_cast;
318 pen_type_t *entry;
319 enumerator_t *e;
320
321 attr_cast = (ietf_attr_attr_request_t*)attr;
322
323 e = attr_cast->create_enumerator(attr_cast);
324 while (e->enumerate(e, &entry))
325 {
326 if (entry->vendor_id != PEN_IETF)
327 {
328 continue;
329 }
330 switch (entry->type)
331 {
332 case IETF_ATTR_PORT_FILTER:
333 result = add_port_filter(out_msg);
334 break;
335 default:
336 break;
337 }
338 }
339 e->destroy(e);
340 }
341 }
342 enumerator->destroy(enumerator);
343
344 if (fatal_error)
345 {
346 result = TNC_RESULT_FATAL;
347 }
348 else if (result == TNC_RESULT_SUCCESS)
349 {
350 result = out_msg->send(out_msg, TRUE);
351 }
352 out_msg->destroy(out_msg);
353
354 return result;
355 }
356
357 /**
358 * see section 3.8.4 of TCG TNC IF-IMC Specification 1.3
359
360 */
361 TNC_Result TNC_IMC_ReceiveMessage(TNC_IMCID imc_id,
362 TNC_ConnectionID connection_id,
363 TNC_BufferReference msg,
364 TNC_UInt32 msg_len,
365 TNC_MessageType msg_type)
366 {
367 imc_state_t *state;
368 imc_msg_t *in_msg;
369 TNC_Result result;
370
371 if (!imc_scanner)
372 {
373 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
374 return TNC_RESULT_NOT_INITIALIZED;
375 }
376 if (!imc_scanner->get_state(imc_scanner, connection_id, &state))
377 {
378 return TNC_RESULT_FATAL;
379 }
380
381 in_msg = imc_msg_create_from_data(imc_scanner, state, connection_id,
382 msg_type, chunk_create(msg, msg_len));
383 result = receive_message(in_msg);
384 in_msg->destroy(in_msg);
385
386 return result;
387 }
388
389 /**
390 * see section 3.8.6 of TCG TNC IF-IMV Specification 1.3
391 */
392 TNC_Result TNC_IMC_ReceiveMessageLong(TNC_IMCID imc_id,
393 TNC_ConnectionID connection_id,
394 TNC_UInt32 msg_flags,
395 TNC_BufferReference msg,
396 TNC_UInt32 msg_len,
397 TNC_VendorID msg_vid,
398 TNC_MessageSubtype msg_subtype,
399 TNC_UInt32 src_imv_id,
400 TNC_UInt32 dst_imc_id)
401 {
402 imc_state_t *state;
403 imc_msg_t *in_msg;
404 TNC_Result result;
405
406 if (!imc_scanner)
407 {
408 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
409 return TNC_RESULT_NOT_INITIALIZED;
410 }
411 if (!imc_scanner->get_state(imc_scanner, connection_id, &state))
412 {
413 return TNC_RESULT_FATAL;
414 }
415 in_msg = imc_msg_create_from_long_data(imc_scanner, state, connection_id,
416 src_imv_id, dst_imc_id, msg_vid, msg_subtype,
417 chunk_create(msg, msg_len));
418 result = receive_message(in_msg);
419 in_msg->destroy(in_msg);
420
421 return result;
422 }
423
424 /**
425 * see section 3.8.7 of TCG TNC IF-IMC Specification 1.3
426 */
427 TNC_Result TNC_IMC_BatchEnding(TNC_IMCID imc_id,
428 TNC_ConnectionID connection_id)
429 {
430 if (!imc_scanner)
431 {
432 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
433 return TNC_RESULT_NOT_INITIALIZED;
434 }
435 return TNC_RESULT_SUCCESS;
436 }
437
438 /**
439 * see section 3.8.8 of TCG TNC IF-IMC Specification 1.3
440 */
441 TNC_Result TNC_IMC_Terminate(TNC_IMCID imc_id)
442 {
443 if (!imc_scanner)
444 {
445 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
446 return TNC_RESULT_NOT_INITIALIZED;
447 }
448 imc_scanner->destroy(imc_scanner);
449 imc_scanner = NULL;
450
451 return TNC_RESULT_SUCCESS;
452 }
453
454 /**
455 * see section 4.2.8.1 of TCG TNC IF-IMC Specification 1.3
456 */
457 TNC_Result TNC_IMC_ProvideBindFunction(TNC_IMCID imc_id,
458 TNC_TNCC_BindFunctionPointer bind_function)
459 {
460 if (!imc_scanner)
461 {
462 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
463 return TNC_RESULT_NOT_INITIALIZED;
464 }
465 return imc_scanner->bind_functions(imc_scanner, bind_function);
466 }