allow registration of multiple message type
[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 <pa_tnc/pa_tnc_msg.h>
20 #include <ietf/ietf_attr.h>
21 #include <ietf/ietf_attr_pa_tnc_error.h>
22 #include <ietf/ietf_attr_port_filter.h>
23 #include <ietf/ietf_attr_assess_result.h>
24
25 #include <tncif_pa_subtypes.h>
26
27 #include <pen/pen.h>
28 #include <utils/lexparser.h>
29 #include <debug.h>
30
31 #include <stdio.h>
32
33 /* IMC definitions */
34
35 static const char imc_name[] = "Scanner";
36
37 static pen_type_t msg_types[] = {
38 { PEN_ITA, PA_SUBTYPE_ITA_SCANNER }
39 };
40
41 static imc_agent_t *imc_scanner;
42
43 /**
44 * see section 3.8.1 of TCG TNC IF-IMC Specification 1.3
45 */
46 TNC_Result TNC_IMC_Initialize(TNC_IMCID imc_id,
47 TNC_Version min_version,
48 TNC_Version max_version,
49 TNC_Version *actual_version)
50 {
51 if (imc_scanner)
52 {
53 DBG1(DBG_IMC, "IMC \"%s\" has already been initialized", imc_name);
54 return TNC_RESULT_ALREADY_INITIALIZED;
55 }
56 imc_scanner = imc_agent_create(imc_name, msg_types, 1, 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 loopback_v4[] = "127.0.0.1";
116 const char loopback_v6[] = "::1";
117
118 /* Open a pipe stream for reading the output of the netstat commmand */
119 file = popen("/bin/netstat -n -l -p -4 -6 --inet", "r");
120 if (!file)
121 {
122 DBG1(DBG_IMC, "Failed to run netstat command");
123 return FALSE;
124 }
125
126 /* Read the output a line at a time */
127 while (fgets(buf, BUF_LEN-1, file))
128 {
129 u_char *pos;
130 u_int8_t new_protocol, protocol;
131 u_int16_t new_port, port;
132 int i;
133 enumerator_t *enumerator;
134 bool allowed, found = FALSE;
135
136 DBG2(DBG_IMC, "%.*s", (int)(strlen(buf)-1), buf);
137
138 if (n++ < 2)
139 {
140 /* skip the first two header lines */
141 continue;
142 }
143 line = chunk_create(buf, strlen(buf));
144
145 /* Extract the IP protocol type */
146 if (!extract_token(&token, ' ', &line))
147 {
148 DBG1(DBG_IMC, "Protocol field in netstat output not found");
149 goto end;
150 }
151 if (match("tcp", &token) || match("tcp6", &token))
152 {
153 new_protocol = IPPROTO_TCP;
154 }
155 else if (match("udp", &token) || match("udp6", &token))
156 {
157 new_protocol = IPPROTO_UDP;
158 }
159 else
160 {
161 DBG1(DBG_IMC, "Skipped unknown IP protocol in netstat output");
162 continue;
163 }
164
165 /* Skip the Recv-Q and Send-Q fields */
166 for (i = 0; i < 3; i++)
167 {
168 if (!eat_whitespace(&line) || !extract_token(&token, ' ', &line))
169 {
170 token = chunk_empty;
171 break;
172 }
173 }
174 if (token.len == 0)
175 {
176 DBG1(DBG_IMC, "Local Address field in netstat output not found");
177 goto end;
178 }
179
180 /* Find the local port appended to the local address */
181 pos = token.ptr + token.len;
182 while (*--pos != ':' && --token.len);
183 if (*pos != ':')
184 {
185 DBG1(DBG_IMC, "Local port field in netstat output not found");
186 goto end;
187 }
188 token.len--;
189
190 /* ignore ports of IPv4 and IPv6 loopback interfaces */
191 if ((token.len == strlen(loopback_v4) &&
192 memeq(loopback_v4, token.ptr, token.len)) ||
193 (token.len == strlen(loopback_v6) &&
194 memeq(loopback_v6, token.ptr, token.len)))
195 {
196 continue;
197 }
198
199 /* convert the port string to an integer */
200 new_port = atoi(pos+1);
201
202 /* check if the there is already a port entry */
203 enumerator = attr->create_port_enumerator(attr);
204 while (enumerator->enumerate(enumerator, &allowed, &protocol, &port))
205 {
206 if (new_port == port && new_protocol == protocol)
207 {
208 found = TRUE;
209 }
210 }
211 enumerator->destroy(enumerator);
212
213 /* Skip the duplicate port entry */
214 if (found)
215 {
216 continue;
217 }
218
219 /* Add new port entry */
220 attr->add_port(attr, FALSE, new_protocol, new_port);
221 }
222
223 /* Successfully completed the parsing of the netstat output */
224 success = TRUE;
225
226 end:
227 /* Close the pipe stream */
228 pclose(file);
229 return success;
230 }
231
232 static TNC_Result send_message(TNC_ConnectionID connection_id)
233 {
234 linked_list_t *attr_list;
235 pa_tnc_attr_t *attr;
236 ietf_attr_port_filter_t *attr_port_filter;
237 TNC_Result result;
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 attr_list = linked_list_create();
248 attr_list->insert_last(attr_list, attr);
249 result = imc_scanner->send_message(imc_scanner, connection_id, FALSE, 0,
250 TNC_IMVID_ANY, PEN_ITA, PA_SUBTYPE_ITA_SCANNER,
251 attr_list);
252 attr_list->destroy(attr_list);
253
254 return result;
255 }
256
257 /**
258 * see section 3.8.3 of TCG TNC IF-IMC Specification 1.3
259 */
260 TNC_Result TNC_IMC_BeginHandshake(TNC_IMCID imc_id,
261 TNC_ConnectionID connection_id)
262 {
263 if (!imc_scanner)
264 {
265 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
266 return TNC_RESULT_NOT_INITIALIZED;
267 }
268 return send_message(connection_id);
269 }
270
271 static TNC_Result receive_message(TNC_IMCID imc_id,
272 TNC_ConnectionID connection_id,
273 TNC_UInt32 msg_flags,
274 chunk_t msg,
275 TNC_VendorID msg_vid,
276 TNC_MessageSubtype msg_subtype,
277 TNC_UInt32 src_imv_id,
278 TNC_UInt32 dst_imc_id)
279 {
280 pa_tnc_msg_t *pa_tnc_msg;
281 pa_tnc_attr_t *attr;
282 pen_type_t attr_type;
283 imc_state_t *state;
284 enumerator_t *enumerator;
285 TNC_Result result;
286 TNC_UInt32 target_imc_id;
287 bool fatal_error;
288
289 if (!imc_scanner)
290 {
291 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
292 return TNC_RESULT_NOT_INITIALIZED;
293 }
294
295 /* get current IMC state */
296 if (!imc_scanner->get_state(imc_scanner, connection_id, &state))
297 {
298 return TNC_RESULT_FATAL;
299 }
300
301 /* parse received PA-TNC message and automatically handle any errors */
302 result = imc_scanner->receive_message(imc_scanner, state, msg, msg_vid,
303 msg_subtype, src_imv_id, dst_imc_id, &pa_tnc_msg);
304
305 /* no parsed PA-TNC attributes available if an error occurred */
306 if (!pa_tnc_msg)
307 {
308 return result;
309 }
310 target_imc_id = (dst_imc_id == TNC_IMCID_ANY) ? imc_id : dst_imc_id;
311
312 /* preprocess any IETF standard error attributes */
313 fatal_error = pa_tnc_msg->process_ietf_std_errors(pa_tnc_msg);
314
315 /* analyze PA-TNC attributes */
316 enumerator = pa_tnc_msg->create_attribute_enumerator(pa_tnc_msg);
317 while (enumerator->enumerate(enumerator, &attr))
318 {
319 attr_type = attr->get_type(attr);
320
321 if (attr_type.vendor_id == PEN_IETF &&
322 attr_type.type == IETF_ATTR_ASSESSMENT_RESULT)
323 {
324 ietf_attr_assess_result_t *ietf_attr;
325
326 ietf_attr = (ietf_attr_assess_result_t*)attr;
327 state->set_result(state, target_imc_id,
328 ietf_attr->get_result(ietf_attr));
329 }
330 }
331 enumerator->destroy(enumerator);
332 pa_tnc_msg->destroy(pa_tnc_msg);
333
334 if (fatal_error)
335 {
336 return TNC_RESULT_FATAL;
337 }
338
339 /* if no assessment result is known then repeat the measurement */
340 return state->get_result(state, target_imc_id, NULL) ?
341 TNC_RESULT_SUCCESS : send_message(connection_id);
342 }
343
344 /**
345 * see section 3.8.4 of TCG TNC IF-IMC Specification 1.3
346
347 */
348 TNC_Result TNC_IMC_ReceiveMessage(TNC_IMCID imc_id,
349 TNC_ConnectionID connection_id,
350 TNC_BufferReference msg,
351 TNC_UInt32 msg_len,
352 TNC_MessageType msg_type)
353 {
354 TNC_VendorID msg_vid;
355 TNC_MessageSubtype msg_subtype;
356
357 msg_vid = msg_type >> 8;
358 msg_subtype = msg_type & TNC_SUBTYPE_ANY;
359
360 return receive_message(imc_id, connection_id, 0, chunk_create(msg, msg_len),
361 msg_vid, msg_subtype, 0, TNC_IMCID_ANY);
362 }
363
364 /**
365 * see section 3.8.6 of TCG TNC IF-IMV Specification 1.3
366 */
367 TNC_Result TNC_IMC_ReceiveMessageLong(TNC_IMCID imc_id,
368 TNC_ConnectionID connection_id,
369 TNC_UInt32 msg_flags,
370 TNC_BufferReference msg,
371 TNC_UInt32 msg_len,
372 TNC_VendorID msg_vid,
373 TNC_MessageSubtype msg_subtype,
374 TNC_UInt32 src_imv_id,
375 TNC_UInt32 dst_imc_id)
376 {
377 return receive_message(imc_id, connection_id, msg_flags,
378 chunk_create(msg, msg_len), msg_vid, msg_subtype,
379 src_imv_id, dst_imc_id);
380 }
381
382 /**
383 * see section 3.8.7 of TCG TNC IF-IMC Specification 1.3
384 */
385 TNC_Result TNC_IMC_BatchEnding(TNC_IMCID imc_id,
386 TNC_ConnectionID connection_id)
387 {
388 if (!imc_scanner)
389 {
390 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
391 return TNC_RESULT_NOT_INITIALIZED;
392 }
393 return TNC_RESULT_SUCCESS;
394 }
395
396 /**
397 * see section 3.8.8 of TCG TNC IF-IMC Specification 1.3
398 */
399 TNC_Result TNC_IMC_Terminate(TNC_IMCID imc_id)
400 {
401 if (!imc_scanner)
402 {
403 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
404 return TNC_RESULT_NOT_INITIALIZED;
405 }
406 imc_scanner->destroy(imc_scanner);
407 imc_scanner = NULL;
408
409 return TNC_RESULT_SUCCESS;
410 }
411
412 /**
413 * see section 4.2.8.1 of TCG TNC IF-IMC Specification 1.3
414 */
415 TNC_Result TNC_IMC_ProvideBindFunction(TNC_IMCID imc_id,
416 TNC_TNCC_BindFunctionPointer bind_function)
417 {
418 if (!imc_scanner)
419 {
420 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
421 return TNC_RESULT_NOT_INITIALIZED;
422 }
423 return imc_scanner->bind_functions(imc_scanner, bind_function);
424 }