803a031de9a4afd9408c6b5cc8a895c6adc94579
[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
24 #include <tncif_names.h>
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 #define IMC_VENDOR_ID PEN_ITA
38 #define IMC_SUBTYPE PA_SUBTYPE_ITA_SCANNER
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, IMC_VENDOR_ID, IMC_SUBTYPE,
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_DELETE:
89 return imc_scanner->delete_state(imc_scanner, connection_id);
90 default:
91 return imc_scanner->change_state(imc_scanner, connection_id,
92 new_state, NULL);
93 }
94 }
95
96 /**
97 * Determine all TCP and UDP server sockets listening on physical interfaces
98 */
99 static bool do_netstat(ietf_attr_port_filter_t *attr)
100 {
101 FILE *file;
102 char buf[BUF_LEN];
103 chunk_t line, token;
104 int n = 0;
105 bool success = FALSE;
106 const char loopback_v4[] = "127.0.0.1";
107 const char loopback_v6[] = "::1";
108
109 /* Open a pipe stream for reading the output of the netstat commmand */
110 file = popen("/bin/netstat -n -l -p -4 -6 --inet", "r");
111 if (!file)
112 {
113 DBG1(DBG_IMC, "Failed to run netstat command");
114 return FALSE;
115 }
116
117 /* Read the output a line at a time */
118 while (fgets(buf, BUF_LEN-1, file))
119 {
120 u_char *pos;
121 u_int8_t new_protocol, protocol;
122 u_int16_t new_port, port;
123 int i;
124 enumerator_t *enumerator;
125 bool allowed, found = FALSE;
126
127 DBG2(DBG_IMC, "%.*s", strlen(buf)-1, buf);
128
129 if (n++ < 2)
130 {
131 /* skip the first two header lines */
132 continue;
133 }
134 line = chunk_create(buf, strlen(buf));
135
136 /* Extract the IP protocol type */
137 if (!extract_token(&token, ' ', &line))
138 {
139 DBG1(DBG_IMC, "Protocol field in netstat output not found");
140 goto end;
141 }
142 if (match("tcp", &token) || match("tcp6", &token))
143 {
144 new_protocol = IPPROTO_TCP;
145 }
146 else if (match("udp", &token) || match("udp6", &token))
147 {
148 new_protocol = IPPROTO_UDP;
149 }
150 else
151 {
152 DBG1(DBG_IMC, "Skipped unknown IP protocol in netstat output");
153 continue;
154 }
155
156 /* Skip the Recv-Q and Send-Q fields */
157 for (i = 0; i < 3; i++)
158 {
159 if (!eat_whitespace(&line) || !extract_token(&token, ' ', &line))
160 {
161 token = chunk_empty;
162 break;
163 }
164 }
165 if (token.len == 0)
166 {
167 DBG1(DBG_IMC, "Local Address field in netstat output not found");
168 goto end;
169 }
170
171 /* Find the local port appended to the local address */
172 pos = token.ptr + token.len;
173 while (*--pos != ':' && --token.len);
174 if (*pos != ':')
175 {
176 DBG1(DBG_IMC, "Local port field in netstat output not found");
177 goto end;
178 }
179 token.len--;
180
181 /* ignore ports of IPv4 and IPv6 loopback interfaces */
182 if ((token.len == strlen(loopback_v4) &&
183 memeq(loopback_v4, token.ptr, token.len)) ||
184 (token.len == strlen(loopback_v6) &&
185 memeq(loopback_v6, token.ptr, token.len)))
186 {
187 continue;
188 }
189
190 /* convert the port string to an integer */
191 new_port = atoi(pos+1);
192
193 /* check if the there is already a port entry */
194 enumerator = attr->create_port_enumerator(attr);
195 while (enumerator->enumerate(enumerator, &allowed, &protocol, &port))
196 {
197 if (new_port == port && new_protocol == protocol)
198 {
199 found = TRUE;
200 }
201 }
202 enumerator->destroy(enumerator);
203
204 /* Skip the duplicate port entry */
205 if (found)
206 {
207 continue;
208 }
209
210 /* Add new port entry */
211 attr->add_port(attr, FALSE, new_protocol, new_port);
212 }
213
214 /* Successfully completed the parsing of the netstat output */
215 success = TRUE;
216
217 end:
218 /* Close the pipe stream */
219 pclose(file);
220 return success;
221 }
222
223 static TNC_Result send_message(TNC_ConnectionID connection_id)
224 {
225 linked_list_t *attr_list;
226 pa_tnc_attr_t *attr;
227 ietf_attr_port_filter_t *attr_port_filter;
228 TNC_Result result;
229
230 attr = ietf_attr_port_filter_create();
231 attr->set_noskip_flag(attr, TRUE);
232 attr_port_filter = (ietf_attr_port_filter_t*)attr;
233 if (!do_netstat(attr_port_filter))
234 {
235 attr->destroy(attr);
236 return TNC_RESULT_FATAL;
237 }
238 attr_list = linked_list_create();
239 attr_list->insert_last(attr_list, attr);
240 result = imc_scanner->send_message(imc_scanner, connection_id, FALSE, 0,
241 TNC_IMVID_ANY, attr_list);
242 attr_list->destroy(attr_list);
243
244 return result;
245 }
246
247 /**
248 * see section 3.8.3 of TCG TNC IF-IMC Specification 1.3
249 */
250 TNC_Result TNC_IMC_BeginHandshake(TNC_IMCID imc_id,
251 TNC_ConnectionID connection_id)
252 {
253 if (!imc_scanner)
254 {
255 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
256 return TNC_RESULT_NOT_INITIALIZED;
257 }
258 return send_message(connection_id);
259 }
260
261 static TNC_Result receive_message(TNC_IMCID imc_id,
262 TNC_ConnectionID connection_id,
263 TNC_UInt32 msg_flags,
264 chunk_t msg,
265 TNC_VendorID msg_vid,
266 TNC_MessageSubtype msg_subtype,
267 TNC_UInt32 src_imv_id,
268 TNC_UInt32 dst_imc_id)
269 {
270 pa_tnc_msg_t *pa_tnc_msg;
271 imc_state_t *state;
272 TNC_Result result;
273 bool fatal_error;
274
275 if (!imc_scanner)
276 {
277 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
278 return TNC_RESULT_NOT_INITIALIZED;
279 }
280
281 /* get current IMC state */
282 if (!imc_scanner->get_state(imc_scanner, connection_id, &state))
283 {
284 return TNC_RESULT_FATAL;
285 }
286
287 /* parse received PA-TNC message and automatically handle any errors */
288 result = imc_scanner->receive_message(imc_scanner, state, msg, msg_vid,
289 msg_subtype, src_imv_id, dst_imc_id, &pa_tnc_msg);
290
291 /* no parsed PA-TNC attributes available if an error occurred */
292 if (!pa_tnc_msg)
293 {
294 return result;
295 }
296
297 /* preprocess any IETF standard error attributes */
298 fatal_error = pa_tnc_msg->process_ietf_std_errors(pa_tnc_msg);
299 pa_tnc_msg->destroy(pa_tnc_msg);
300
301 /* if no error occurred then always return the same response */
302 return fatal_error ? TNC_RESULT_FATAL : send_message(connection_id);
303 }
304
305 /**
306 * see section 3.8.4 of TCG TNC IF-IMC Specification 1.3
307 */
308 TNC_Result TNC_IMC_ReceiveMessage(TNC_IMCID imc_id,
309 TNC_ConnectionID connection_id,
310 TNC_BufferReference msg,
311 TNC_UInt32 msg_len,
312 TNC_MessageType msg_type)
313 {
314 TNC_VendorID msg_vid;
315 TNC_MessageSubtype msg_subtype;
316
317 msg_vid = msg_type >> 8;
318 msg_subtype = msg_type & TNC_SUBTYPE_ANY;
319
320 return receive_message(imc_id, connection_id, 0, chunk_create(msg, msg_len),
321 msg_vid, msg_subtype, 0, TNC_IMCID_ANY);
322 }
323
324 /**
325 * see section 3.8.6 of TCG TNC IF-IMV Specification 1.3
326 */
327 TNC_Result TNC_IMC_ReceiveMessageLong(TNC_IMCID imc_id,
328 TNC_ConnectionID connection_id,
329 TNC_UInt32 msg_flags,
330 TNC_BufferReference msg,
331 TNC_UInt32 msg_len,
332 TNC_VendorID msg_vid,
333 TNC_MessageSubtype msg_subtype,
334 TNC_UInt32 src_imv_id,
335 TNC_UInt32 dst_imc_id)
336 {
337 return receive_message(imc_id, connection_id, msg_flags,
338 chunk_create(msg, msg_len), msg_vid, msg_subtype,
339 src_imv_id, dst_imc_id);
340 }
341
342 /**
343 * see section 3.8.7 of TCG TNC IF-IMC Specification 1.3
344 */
345 TNC_Result TNC_IMC_BatchEnding(TNC_IMCID imc_id,
346 TNC_ConnectionID connection_id)
347 {
348 if (!imc_scanner)
349 {
350 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
351 return TNC_RESULT_NOT_INITIALIZED;
352 }
353 return TNC_RESULT_SUCCESS;
354 }
355
356 /**
357 * see section 3.8.8 of TCG TNC IF-IMC Specification 1.3
358 */
359 TNC_Result TNC_IMC_Terminate(TNC_IMCID imc_id)
360 {
361 if (!imc_scanner)
362 {
363 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
364 return TNC_RESULT_NOT_INITIALIZED;
365 }
366 imc_scanner->destroy(imc_scanner);
367 imc_scanner = NULL;
368
369 return TNC_RESULT_SUCCESS;
370 }
371
372 /**
373 * see section 4.2.8.1 of TCG TNC IF-IMC Specification 1.3
374 */
375 TNC_Result TNC_IMC_ProvideBindFunction(TNC_IMCID imc_id,
376 TNC_TNCC_BindFunctionPointer bind_function)
377 {
378 if (!imc_scanner)
379 {
380 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
381 return TNC_RESULT_NOT_INITIALIZED;
382 }
383 return imc_scanner->bind_functions(imc_scanner, bind_function);
384 }