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