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