97133e57529f0190a842fcc5035daf80e3756bed
[strongswan.git] / src / libimcv / plugins / imc_scanner / imc_scanner.c
1 /*
2 * Copyright (C) 2011 Andreas Steffen, HSR Hochschule fuer Technik Rapperswil
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * for more details.
13 */
14
15 #include "imc_scanner_state.h"
16
17 #include <imc/imc_agent.h>
18 #include <pa_tnc/pa_tnc_msg.h>
19 #include <ietf/ietf_attr.h>
20 #include <ietf/ietf_attr_pa_tnc_error.h>
21 #include <ietf/ietf_attr_port_filter.h>
22
23 #include <tncif_names.h>
24 #include <tncif_pa_subtypes.h>
25
26 #include <pen/pen.h>
27 #include <utils/lexparser.h>
28 #include <debug.h>
29
30 #include <stdio.h>
31
32 /* IMC definitions */
33
34 static const char imc_name[] = "Scanner";
35
36 #define IMC_VENDOR_ID PEN_ITA
37 #define IMC_SUBTYPE PA_SUBTYPE_ITA_SCANNER
38
39 static imc_agent_t *imc_scanner;
40
41 /**
42 * see section 3.7.1 of TCG TNC IF-IMC Specification 1.2
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, IMC_VENDOR_ID, IMC_SUBTYPE,
55 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.7.2 of TCG TNC IF-IMC Specification 1.2
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_DELETE:
88 return imc_scanner->delete_state(imc_scanner, connection_id);
89 default:
90 return imc_scanner->change_state(imc_scanner, connection_id,
91 new_state, NULL);
92 }
93 }
94
95 /**
96 * Determine all TCP and UDP server sockets listening on physical interfaces
97 */
98 static bool do_netstat(ietf_attr_port_filter_t *attr)
99 {
100 FILE *file;
101 char buf[BUF_LEN];
102 chunk_t line, token;
103 int n = 0;
104 bool success = FALSE;
105
106 /* Open a pipe stream for reading the output of the netstat commmand */
107 file = popen("/bin/netstat -n -l -4 -6 --inet", "r");
108 if (!file)
109 {
110 DBG1(DBG_IMC, "Failed to run netstat command");
111 return FALSE;
112 }
113
114 /* Read the output a line at a time */
115 while (fgets(buf, BUF_LEN-1, file))
116 {
117 u_char *pos;
118 u_int8_t new_protocol, protocol;
119 u_int16_t new_port, port;
120 int i;
121 enumerator_t *enumerator;
122 bool allowed, found = FALSE;
123
124 DBG2(DBG_IMC, "%.*s", strlen(buf)-1, buf);
125
126 if (n++ < 2)
127 {
128 /* skip the first two header lines */
129 continue;
130 }
131 line = chunk_create(buf, strlen(buf));
132
133 /* Extract the IP protocol type */
134 if (!extract_token(&token, ' ', &line))
135 {
136 DBG1(DBG_IMC, "Protocol field in netstat output not found");
137 goto end;
138 }
139 if (match("tcp", &token) || match("tcp6", &token))
140 {
141 new_protocol = IPPROTO_TCP;
142 }
143 else if (match("udp", &token) || match("udp6", &token))
144 {
145 new_protocol = IPPROTO_UDP;
146 }
147 else
148 {
149 DBG1(DBG_IMC, "Skipped unknown IP protocol in netstat output");
150 continue;
151 }
152
153 /* Skip the Recv-Q and Send-Q fields */
154 for (i = 0; i < 3; i++)
155 {
156 if (!eat_whitespace(&line) || !extract_token(&token, ' ', &line))
157 {
158 token = chunk_empty;
159 break;
160 }
161 }
162 if (token.len == 0)
163 {
164 DBG1(DBG_IMC, "Local Address field in netstat output not found");
165 goto end;
166 }
167
168 /* Find the local port appended to the local address */
169 pos = token.ptr + token.len;
170 while (*--pos != ':' && --token.len);
171 if (*pos != ':')
172 {
173 DBG1(DBG_IMC, "Local port field in netstat output not found");
174 goto end;
175 }
176
177 /* convert the port string to an integer */
178 new_port = atoi(pos+1);
179
180 /* check if the there is already a port entry */
181 enumerator = attr->create_port_enumerator(attr);
182 while (enumerator->enumerate(enumerator, &allowed, &protocol, &port))
183 {
184 if (new_port == port && new_protocol == protocol)
185 {
186 found = TRUE;
187 }
188 }
189 enumerator->destroy(enumerator);
190
191 /* Skip the duplicate port entry */
192 if (found)
193 {
194 continue;
195 }
196
197 /* Add new port entry */
198 attr->add_port(attr, FALSE, new_protocol, new_port);
199 }
200
201 /* Successfully completed the parsing of the netstat output */
202 success = TRUE;
203
204 end:
205 /* Close the pipe stream */
206 pclose(file);
207 return success;
208 }
209
210 static TNC_Result send_message(TNC_ConnectionID connection_id)
211 {
212 pa_tnc_msg_t *msg;
213 pa_tnc_attr_t *attr;
214 ietf_attr_port_filter_t *attr_port_filter;
215 TNC_Result result;
216
217 attr = ietf_attr_port_filter_create();
218 attr->set_noskip_flag(attr, TRUE);
219 attr_port_filter = (ietf_attr_port_filter_t*)attr;
220 if (!do_netstat(attr_port_filter))
221 {
222 attr->destroy(attr);
223 return TNC_RESULT_FATAL;
224 }
225 msg = pa_tnc_msg_create();
226 msg->add_attribute(msg, attr);
227 msg->build(msg);
228 result = imc_scanner->send_message(imc_scanner, connection_id,
229 msg->get_encoding(msg));
230 msg->destroy(msg);
231
232 return result;
233 }
234
235 /**
236 * see section 3.7.3 of TCG TNC IF-IMC Specification 1.2
237 */
238 TNC_Result TNC_IMC_BeginHandshake(TNC_IMCID imc_id,
239 TNC_ConnectionID connection_id)
240 {
241 if (!imc_scanner)
242 {
243 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
244 return TNC_RESULT_NOT_INITIALIZED;
245 }
246 return send_message(connection_id);
247 }
248
249 /**
250 * see section 3.7.4 of TCG TNC IF-IMC Specification 1.2
251 */
252 TNC_Result TNC_IMC_ReceiveMessage(TNC_IMCID imc_id,
253 TNC_ConnectionID connection_id,
254 TNC_BufferReference msg,
255 TNC_UInt32 msg_len,
256 TNC_MessageType msg_type)
257 {
258 pa_tnc_msg_t *pa_tnc_msg;
259 pa_tnc_attr_t *attr;
260 enumerator_t *enumerator;
261 TNC_Result result;
262 bool fatal_error = FALSE;
263
264 if (!imc_scanner)
265 {
266 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
267 return TNC_RESULT_NOT_INITIALIZED;
268 }
269
270 /* parse received PA-TNC message and automatically handle any errors */
271 result = imc_scanner->receive_message(imc_scanner, connection_id,
272 chunk_create(msg, msg_len), msg_type,
273 &pa_tnc_msg);
274
275 /* no parsed PA-TNC attributes available if an error occurred */
276 if (!pa_tnc_msg)
277 {
278 return result;
279 }
280
281 /* analyze PA-TNC attributes */
282 enumerator = pa_tnc_msg->create_attribute_enumerator(pa_tnc_msg);
283 while (enumerator->enumerate(enumerator, &attr))
284 {
285 ietf_attr_pa_tnc_error_t *error_attr;
286 pa_tnc_error_code_t error_code;
287 chunk_t msg_info, attr_info;
288
289 if (attr->get_vendor_id(attr) != PEN_IETF &&
290 attr->get_type(attr) != IETF_ATTR_PA_TNC_ERROR)
291 {
292 continue;
293 }
294
295 error_attr = (ietf_attr_pa_tnc_error_t*)attr;
296 error_code = error_attr->get_error_code(error_attr);
297 msg_info = error_attr->get_msg_info(error_attr);
298 DBG1(DBG_IMC, "received PA-TNC error '%N' concerning message %#B",
299 pa_tnc_error_code_names, error_code, &msg_info);
300
301 switch (error_code)
302 {
303 case PA_ERROR_ATTR_TYPE_NOT_SUPPORTED:
304 attr_info = error_attr->get_attr_info(error_attr);
305 DBG1(DBG_IMC, " unsupported attribute %#B", &attr_info);
306 break;
307 default:
308 break;
309 }
310 fatal_error = TRUE;
311 }
312 enumerator->destroy(enumerator);
313 pa_tnc_msg->destroy(pa_tnc_msg);
314
315 /* if no error occurred then always return the same response */
316 return fatal_error ? TNC_RESULT_FATAL : send_message(connection_id);
317 }
318
319 /**
320 * see section 3.7.5 of TCG TNC IF-IMC Specification 1.2
321 */
322 TNC_Result TNC_IMC_BatchEnding(TNC_IMCID imc_id,
323 TNC_ConnectionID connection_id)
324 {
325 if (!imc_scanner)
326 {
327 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
328 return TNC_RESULT_NOT_INITIALIZED;
329 }
330 return TNC_RESULT_SUCCESS;
331 }
332
333 /**
334 * see section 3.7.6 of TCG TNC IF-IMC Specification 1.2
335 */
336 TNC_Result TNC_IMC_Terminate(TNC_IMCID imc_id)
337 {
338 if (!imc_scanner)
339 {
340 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
341 return TNC_RESULT_NOT_INITIALIZED;
342 }
343 imc_scanner->destroy(imc_scanner);
344 imc_scanner = NULL;
345
346 return TNC_RESULT_SUCCESS;
347 }
348
349 /**
350 * see section 4.2.8.1 of TCG TNC IF-IMC Specification 1.2
351 */
352 TNC_Result TNC_IMC_ProvideBindFunction(TNC_IMCID imc_id,
353 TNC_TNCC_BindFunctionPointer bind_function)
354 {
355 if (!imc_scanner)
356 {
357 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
358 return TNC_RESULT_NOT_INITIALIZED;
359 }
360 return imc_scanner->bind_functions(imc_scanner, bind_function);
361 }