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