2 * Copyright (C) 2011-2012 Andreas Steffen
3 * HSR Hochschule fuer Technik Rapperswil
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>.
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
16 #include "imc_scanner_state.h"
18 #include <imc/imc_agent.h>
19 #include <imc/imc_msg.h>
20 #include <ietf/ietf_attr.h>
21 #include <ietf/ietf_attr_attr_request.h>
22 #include <ietf/ietf_attr_port_filter.h>
23 #include <ietf/ietf_attr_remediation_instr.h>
25 #include <tncif_pa_subtypes.h>
28 #include <utils/lexparser.h>
29 #include <utils/debug.h>
35 static const char imc_name
[] = "Scanner";
37 static pen_type_t msg_types
[] = {
38 { PEN_IETF
, PA_SUBTYPE_IETF_VPN
}
41 static imc_agent_t
*imc_scanner
;
44 * see section 3.8.1 of TCG TNC IF-IMC Specification 1.3
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
)
53 DBG1(DBG_IMC
, "IMC \"%s\" has already been initialized", imc_name
);
54 return TNC_RESULT_ALREADY_INITIALIZED
;
56 imc_scanner
= imc_agent_create(imc_name
, msg_types
, countof(msg_types
),
57 imc_id
, actual_version
);
60 return TNC_RESULT_FATAL
;
62 if (min_version
> TNC_IFIMC_VERSION_1
|| max_version
< TNC_IFIMC_VERSION_1
)
64 DBG1(DBG_IMC
, "no common IF-IMC version");
65 return TNC_RESULT_NO_COMMON_VERSION
;
67 return TNC_RESULT_SUCCESS
;
71 * see section 3.8.2 of TCG TNC IF-IMC Specification 1.3
73 TNC_Result
TNC_IMC_NotifyConnectionChange(TNC_IMCID imc_id
,
74 TNC_ConnectionID connection_id
,
75 TNC_ConnectionState new_state
)
81 DBG1(DBG_IMC
, "IMC \"%s\" has not been initialized", imc_name
);
82 return TNC_RESULT_NOT_INITIALIZED
;
86 case TNC_CONNECTION_STATE_CREATE
:
87 state
= imc_scanner_state_create(connection_id
);
88 return imc_scanner
->create_state(imc_scanner
, state
);
89 case TNC_CONNECTION_STATE_HANDSHAKE
:
90 if (imc_scanner
->change_state(imc_scanner
, connection_id
, new_state
,
91 &state
) != TNC_RESULT_SUCCESS
)
93 return TNC_RESULT_FATAL
;
95 state
->set_result(state
, imc_id
,
96 TNC_IMV_EVALUATION_RESULT_DONT_KNOW
);
97 return TNC_RESULT_SUCCESS
;
98 case TNC_CONNECTION_STATE_DELETE
:
99 return imc_scanner
->delete_state(imc_scanner
, connection_id
);
101 return imc_scanner
->change_state(imc_scanner
, connection_id
,
107 * Determine all TCP and UDP server sockets listening on physical interfaces
109 static bool do_netstat(ietf_attr_port_filter_t
*attr
)
115 bool success
= FALSE
;
116 const char loopback_v4
[] = "127.0.0.1";
117 const char loopback_v6
[] = "::1";
119 /* Open a pipe stream for reading the output of the netstat commmand */
120 file
= popen("/bin/netstat -n -l -p -4 -6 --inet", "r");
123 DBG1(DBG_IMC
, "failed to run netstat command");
127 /* Read the output a line at a time */
128 while (fgets(buf
, sizeof(buf
), file
))
131 u_int8_t new_protocol
, protocol
;
132 u_int16_t new_port
, port
;
134 enumerator_t
*enumerator
;
135 bool allowed
, found
= FALSE
;
137 DBG2(DBG_IMC
, "%.*s", (int)(strlen(buf
)-1), buf
);
141 /* skip the first two header lines */
144 line
= chunk_create(buf
, strlen(buf
));
146 /* Extract the IP protocol type */
147 if (!extract_token(&token
, ' ', &line
))
149 DBG1(DBG_IMC
, "protocol field in netstat output not found");
152 if (match("tcp", &token
) || match("tcp6", &token
))
154 new_protocol
= IPPROTO_TCP
;
156 else if (match("udp", &token
) || match("udp6", &token
))
158 new_protocol
= IPPROTO_UDP
;
162 DBG1(DBG_IMC
, "skipped unknown IP protocol in netstat output");
166 /* Skip the Recv-Q and Send-Q fields */
167 for (i
= 0; i
< 3; i
++)
169 if (!eat_whitespace(&line
) || !extract_token(&token
, ' ', &line
))
177 DBG1(DBG_IMC
, "local address field in netstat output not found");
181 /* Find the local port appended to the local address */
182 pos
= token
.ptr
+ token
.len
;
183 while (*--pos
!= ':' && --token
.len
);
186 DBG1(DBG_IMC
, "local port field in netstat output not found");
191 /* ignore ports of IPv4 and IPv6 loopback interfaces */
192 if ((token
.len
== strlen(loopback_v4
) &&
193 memeq(loopback_v4
, token
.ptr
, token
.len
)) ||
194 (token
.len
== strlen(loopback_v6
) &&
195 memeq(loopback_v6
, token
.ptr
, token
.len
)))
200 /* convert the port string to an integer */
201 new_port
= atoi(pos
+1);
203 /* check if the there is already a port entry */
204 enumerator
= attr
->create_port_enumerator(attr
);
205 while (enumerator
->enumerate(enumerator
, &allowed
, &protocol
, &port
))
207 if (new_port
== port
&& new_protocol
== protocol
)
212 enumerator
->destroy(enumerator
);
214 /* Skip the duplicate port entry */
220 /* Add new port entry */
221 attr
->add_port(attr
, FALSE
, new_protocol
, new_port
);
224 /* Successfully completed the parsing of the netstat output */
228 /* Close the pipe stream */
234 * Add IETF Port Filter attribute to the send queue
236 static TNC_Result
add_port_filter(imc_msg_t
*msg
)
239 ietf_attr_port_filter_t
*attr_port_filter
;
241 attr
= ietf_attr_port_filter_create();
242 attr
->set_noskip_flag(attr
, TRUE
);
243 attr_port_filter
= (ietf_attr_port_filter_t
*)attr
;
244 if (!do_netstat(attr_port_filter
))
247 return TNC_RESULT_FATAL
;
249 msg
->add_attribute(msg
, attr
);
251 return TNC_RESULT_SUCCESS
;
255 * see section 3.8.3 of TCG TNC IF-IMC Specification 1.3
257 TNC_Result
TNC_IMC_BeginHandshake(TNC_IMCID imc_id
,
258 TNC_ConnectionID connection_id
)
262 TNC_Result result
= TNC_RESULT_SUCCESS
;
266 DBG1(DBG_IMC
, "IMC \"%s\" has not been initialized", imc_name
);
267 return TNC_RESULT_NOT_INITIALIZED
;
269 if (!imc_scanner
->get_state(imc_scanner
, connection_id
, &state
))
271 return TNC_RESULT_FATAL
;
273 if (lib
->settings
->get_bool(lib
->settings
,
274 "libimcv.plugins.imc-scanner.send_ports", TRUE
))
276 out_msg
= imc_msg_create(imc_scanner
, state
, connection_id
, imc_id
,
277 TNC_IMVID_ANY
, msg_types
[0]);
278 result
= add_port_filter(out_msg
);
279 if (result
== TNC_RESULT_SUCCESS
)
281 /* send PA-TNC message with the excl flag not set */
282 result
= out_msg
->send(out_msg
, FALSE
);
284 out_msg
->destroy(out_msg
);
290 static TNC_Result
receive_message(imc_msg_t
*in_msg
)
293 enumerator_t
*enumerator
;
295 pen_type_t attr_type
;
296 TNC_Result result
= TNC_RESULT_SUCCESS
;
297 bool fatal_error
= FALSE
;
299 /* parse received PA-TNC message and handle local and remote errors */
300 result
= in_msg
->receive(in_msg
, &fatal_error
);
301 if (result
!= TNC_RESULT_SUCCESS
)
305 out_msg
= imc_msg_create_as_reply(in_msg
);
307 /* analyze PA-TNC attributes */
308 enumerator
= in_msg
->create_attribute_enumerator(in_msg
);
309 while (enumerator
->enumerate(enumerator
, &attr
))
311 attr_type
= attr
->get_type(attr
);
313 if (attr_type
.vendor_id
!= PEN_IETF
)
317 if (attr_type
.type
== IETF_ATTR_ATTRIBUTE_REQUEST
)
319 ietf_attr_attr_request_t
*attr_cast
;
323 attr_cast
= (ietf_attr_attr_request_t
*)attr
;
325 e
= attr_cast
->create_enumerator(attr_cast
);
326 while (e
->enumerate(e
, &entry
))
328 if (entry
->vendor_id
!= PEN_IETF
)
334 case IETF_ATTR_PORT_FILTER
:
335 result
= add_port_filter(out_msg
);
343 else if (attr_type
.type
== IETF_ATTR_REMEDIATION_INSTRUCTIONS
)
345 ietf_attr_remediation_instr_t
*attr_cast
;
346 pen_type_t parameters_type
;
347 chunk_t parameters
, string
, lang_code
;
349 attr_cast
= (ietf_attr_remediation_instr_t
*)attr
;
350 parameters_type
= attr_cast
->get_parameters_type(attr_cast
);
351 parameters
= attr_cast
->get_parameters(attr_cast
);
353 if (parameters_type
.vendor_id
== PEN_IETF
)
355 switch (parameters_type
.type
)
357 case IETF_REMEDIATION_PARAMETERS_URI
:
358 DBG1(DBG_IMC
, "remediation uri: '%.*s'",
359 parameters
.len
, parameters
.ptr
);
361 case IETF_REMEDIATION_PARAMETERS_STRING
:
362 string
= attr_cast
->get_string(attr_cast
, &lang_code
);
363 DBG1(DBG_IMC
, "remediation string: '%.*s' [%.*s]",
364 string
.len
, string
.ptr
,
365 lang_code
.len
, lang_code
.ptr
);
368 DBG1(DBG_IMC
, "remediation parameters %B", ¶meters
);
373 DBG1(DBG_IMC
, "remediation parameters %B", ¶meters
);
377 enumerator
->destroy(enumerator
);
381 result
= TNC_RESULT_FATAL
;
383 else if (result
== TNC_RESULT_SUCCESS
)
385 result
= out_msg
->send(out_msg
, TRUE
);
387 out_msg
->destroy(out_msg
);
393 * see section 3.8.4 of TCG TNC IF-IMC Specification 1.3
396 TNC_Result
TNC_IMC_ReceiveMessage(TNC_IMCID imc_id
,
397 TNC_ConnectionID connection_id
,
398 TNC_BufferReference msg
,
400 TNC_MessageType msg_type
)
408 DBG1(DBG_IMC
, "IMC \"%s\" has not been initialized", imc_name
);
409 return TNC_RESULT_NOT_INITIALIZED
;
411 if (!imc_scanner
->get_state(imc_scanner
, connection_id
, &state
))
413 return TNC_RESULT_FATAL
;
416 in_msg
= imc_msg_create_from_data(imc_scanner
, state
, connection_id
,
417 msg_type
, chunk_create(msg
, msg_len
));
418 result
= receive_message(in_msg
);
419 in_msg
->destroy(in_msg
);
425 * see section 3.8.6 of TCG TNC IF-IMV Specification 1.3
427 TNC_Result
TNC_IMC_ReceiveMessageLong(TNC_IMCID imc_id
,
428 TNC_ConnectionID connection_id
,
429 TNC_UInt32 msg_flags
,
430 TNC_BufferReference msg
,
432 TNC_VendorID msg_vid
,
433 TNC_MessageSubtype msg_subtype
,
434 TNC_UInt32 src_imv_id
,
435 TNC_UInt32 dst_imc_id
)
443 DBG1(DBG_IMC
, "IMC \"%s\" has not been initialized", imc_name
);
444 return TNC_RESULT_NOT_INITIALIZED
;
446 if (!imc_scanner
->get_state(imc_scanner
, connection_id
, &state
))
448 return TNC_RESULT_FATAL
;
450 in_msg
= imc_msg_create_from_long_data(imc_scanner
, state
, connection_id
,
451 src_imv_id
, dst_imc_id
, msg_vid
, msg_subtype
,
452 chunk_create(msg
, msg_len
));
453 result
= receive_message(in_msg
);
454 in_msg
->destroy(in_msg
);
460 * see section 3.8.7 of TCG TNC IF-IMC Specification 1.3
462 TNC_Result
TNC_IMC_BatchEnding(TNC_IMCID imc_id
,
463 TNC_ConnectionID connection_id
)
467 DBG1(DBG_IMC
, "IMC \"%s\" has not been initialized", imc_name
);
468 return TNC_RESULT_NOT_INITIALIZED
;
470 return TNC_RESULT_SUCCESS
;
474 * see section 3.8.8 of TCG TNC IF-IMC Specification 1.3
476 TNC_Result
TNC_IMC_Terminate(TNC_IMCID imc_id
)
480 DBG1(DBG_IMC
, "IMC \"%s\" has not been initialized", imc_name
);
481 return TNC_RESULT_NOT_INITIALIZED
;
483 imc_scanner
->destroy(imc_scanner
);
486 return TNC_RESULT_SUCCESS
;
490 * see section 4.2.8.1 of TCG TNC IF-IMC Specification 1.3
492 TNC_Result
TNC_IMC_ProvideBindFunction(TNC_IMCID imc_id
,
493 TNC_TNCC_BindFunctionPointer bind_function
)
497 DBG1(DBG_IMC
, "IMC \"%s\" has not been initialized", imc_name
);
498 return TNC_RESULT_NOT_INITIALIZED
;
500 return imc_scanner
->bind_functions(imc_scanner
, bind_function
);