2 * Copyright (C) 2011 Andreas Steffen, HSR Hochschule fuer Technik Rapperswil
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>.
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
15 #include "imv_scanner_state.h"
17 #include <imv/imv_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>
23 #include <tncif_names.h>
24 #include <tncif_pa_subtypes.h>
27 #include <utils/linked_list.h>
28 #include <utils/lexparser.h>
33 static const char imv_name
[] = "Scanner";
35 #define IMV_VENDOR_ID PEN_ITA
36 #define IMV_SUBTYPE PA_SUBTYPE_ITA_SCANNER
38 static imv_agent_t
*imv_scanner
;
40 typedef struct port_range_t port_range_t
;
43 u_int16_t start
, stop
;
50 * TRUE: all server ports on the TNC client must be closed
51 * FALSE: any server port on the TNC client is allowed to be open
53 static bool closed_port_policy
= TRUE
;
56 * List of TCP and UDP port ranges
58 * TRUE: server ports on the TNC client that are allowed to be open
59 * FALSE: server ports on the TNC client that must be closed
61 static linked_list_t
*tcp_ports
, *udp_ports
;
64 * Get a TCP or UDP port list from strongswan.conf
66 static linked_list_t
* get_port_list(char *label
)
70 chunk_t port_list
, port_item
, port_stop
;
71 port_range_t
*port_range
;
73 list
= linked_list_create();
75 snprintf(key
, sizeof(key
), "libimcv.plugins.imv-scanner.%s_ports", label
);
76 value
= lib
->settings
->get_str(lib
->settings
, key
, NULL
);
79 DBG1(DBG_IMV
, "%s not defined", key
);
82 port_list
= chunk_create(value
, strlen(value
));
83 DBG2(DBG_IMV
, "list of %s ports that %s:", label
,
84 closed_port_policy ?
"are allowed to be open" : "must be closed");
86 while (eat_whitespace(&port_list
))
88 if (!extract_token(&port_item
, ' ', &port_list
))
90 /* reached last port item */
91 port_item
= port_list
;
92 port_list
= chunk_empty
;
94 port_range
= malloc_thing(port_range_t
);
95 port_range
->start
= atoi(port_item
.ptr
);;
97 if (extract_token(&port_stop
, '-', &port_item
))
99 port_range
->stop
= atoi(port_stop
.ptr
);
103 port_range
->stop
= port_range
->start
;
105 DBG2(DBG_IMV
, "%5u - %5u", port_range
->start
, port_range
->stop
);
106 list
->insert_last(list
, port_range
);
114 * see section 3.8.1 of TCG TNC IF-IMV Specification 1.3
116 TNC_Result
TNC_IMV_Initialize(TNC_IMVID imv_id
,
117 TNC_Version min_version
,
118 TNC_Version max_version
,
119 TNC_Version
*actual_version
)
123 DBG1(DBG_IMV
, "IMV \"%s\" has already been initialized", imv_name
);
124 return TNC_RESULT_ALREADY_INITIALIZED
;
126 imv_scanner
= imv_agent_create(imv_name
, IMV_VENDOR_ID
, IMV_SUBTYPE
,
127 imv_id
, actual_version
);
130 return TNC_RESULT_FATAL
;
132 if (min_version
> TNC_IFIMV_VERSION_1
|| max_version
< TNC_IFIMV_VERSION_1
)
134 DBG1(DBG_IMV
, "no common IF-IMV version");
135 return TNC_RESULT_NO_COMMON_VERSION
;
138 /* set the default port policy to closed (TRUE) or open (FALSE) */
139 closed_port_policy
= lib
->settings
->get_bool(lib
->settings
,
140 "libimcv.plugins.imv-scanner.closed_port_policy", TRUE
);
141 DBG2(DBG_IMV
, "default port policy is %s ports",
142 closed_port_policy ?
"closed" : "open");
144 /* get the list of open|closed ports */
145 tcp_ports
= get_port_list("tcp");
146 udp_ports
= get_port_list("udp");
148 return TNC_RESULT_SUCCESS
;
152 * see section 3.8.2 of TCG TNC IF-IMV Specification 1.3
154 TNC_Result
TNC_IMV_NotifyConnectionChange(TNC_IMVID imv_id
,
155 TNC_ConnectionID connection_id
,
156 TNC_ConnectionState new_state
)
162 DBG1(DBG_IMV
, "IMV \"%s\" has not been initialized", imv_name
);
163 return TNC_RESULT_NOT_INITIALIZED
;
167 case TNC_CONNECTION_STATE_CREATE
:
168 state
= imv_scanner_state_create(connection_id
);
169 return imv_scanner
->create_state(imv_scanner
, state
);
170 case TNC_CONNECTION_STATE_DELETE
:
171 return imv_scanner
->delete_state(imv_scanner
, connection_id
);
173 return imv_scanner
->change_state(imv_scanner
, connection_id
,
178 static TNC_Result
receive_message(TNC_IMVID imv_id
,
179 TNC_ConnectionID connection_id
,
180 TNC_UInt32 msg_flags
,
182 TNC_VendorID msg_vid
,
183 TNC_MessageSubtype msg_subtype
,
184 TNC_UInt32 src_imc_id
,
185 TNC_UInt32 dst_imv_id
)
187 pa_tnc_msg_t
*pa_tnc_msg
;
190 enumerator_t
*enumerator
;
192 bool fatal_error
= FALSE
;
196 DBG1(DBG_IMV
, "IMV \"%s\" has not been initialized", imv_name
);
197 return TNC_RESULT_NOT_INITIALIZED
;
200 /* get current IMV state */
201 if (!imv_scanner
->get_state(imv_scanner
, connection_id
, &state
))
203 return TNC_RESULT_FATAL
;
206 /* parse received PA-TNC message and automatically handle any errors */
207 result
= imv_scanner
->receive_message(imv_scanner
, state
, msg
, msg_vid
,
208 msg_subtype
, src_imc_id
, dst_imv_id
, &pa_tnc_msg
);
210 /* no parsed PA-TNC attributes available if an error occurred */
216 /* analyze PA-TNC attributes */
217 enumerator
= pa_tnc_msg
->create_attribute_enumerator(pa_tnc_msg
);
218 while (enumerator
->enumerate(enumerator
, &attr
))
220 if (attr
->get_vendor_id(attr
) != PEN_IETF
)
225 if (attr
->get_type(attr
) == IETF_ATTR_PA_TNC_ERROR
)
227 ietf_attr_pa_tnc_error_t
*error_attr
;
228 pa_tnc_error_code_t error_code
;
229 chunk_t msg_info
, attr_info
;
232 error_attr
= (ietf_attr_pa_tnc_error_t
*)attr
;
233 error_code
= error_attr
->get_error_code(error_attr
);
234 msg_info
= error_attr
->get_msg_info(error_attr
);
235 DBG1(DBG_IMV
, "received PA-TNC error '%N' concerning message %#B",
236 pa_tnc_error_code_names
, error_code
, &msg_info
);
240 case PA_ERROR_INVALID_PARAMETER
:
241 offset
= error_attr
->get_offset(error_attr
);
242 DBG1(DBG_IMV
, " occurred at offset of %u bytes", offset
);
244 case PA_ERROR_ATTR_TYPE_NOT_SUPPORTED
:
245 attr_info
= error_attr
->get_attr_info(error_attr
);
246 DBG1(DBG_IMV
, " unsupported attribute %#B", &attr_info
);
253 else if (attr
->get_type(attr
) == IETF_ATTR_PORT_FILTER
)
255 ietf_attr_port_filter_t
*attr_port_filter
;
256 enumerator_t
*enumerator
;
259 char buf
[BUF_LEN
], *pos
= buf
;
260 size_t len
= BUF_LEN
;
261 bool blocked
, compliant
= TRUE
;
263 attr_port_filter
= (ietf_attr_port_filter_t
*)attr
;
264 enumerator
= attr_port_filter
->create_port_enumerator(attr_port_filter
);
265 while (enumerator
->enumerate(enumerator
, &blocked
, &protocol
, &port
))
268 port_range_t
*port_range
;
269 bool passed
, found
= FALSE
;
274 /* ignore closed ports */
278 e
= (protocol
== IPPROTO_TCP
) ?
279 tcp_ports
->create_enumerator(tcp_ports
) :
280 udp_ports
->create_enumerator(udp_ports
);
281 while (e
->enumerate(e
, &port_range
))
283 if (port
>= port_range
->start
&& port
<= port_range
->stop
)
291 passed
= (closed_port_policy
== found
);
292 DBG2(DBG_IMV
, "%s port %5u %s: %s",
293 (protocol
== IPPROTO_TCP
) ?
"tcp" : "udp", port
,
294 blocked ?
"closed" : "open", passed ?
"ok" : "fatal");
298 written
= snprintf(pos
, len
, " %s/%u",
299 (protocol
== IPPROTO_TCP
) ?
"tcp" : "udp",
301 if (written
< 0 || written
>= len
)
309 enumerator
->destroy(enumerator
);
313 state
->set_recommendation(state
,
314 TNC_IMV_ACTION_RECOMMENDATION_ALLOW
,
315 TNC_IMV_EVALUATION_RESULT_COMPLIANT
);
319 imv_scanner_state_t
*imv_scanner_state
;
321 imv_scanner_state
= (imv_scanner_state_t
*)state
;
322 imv_scanner_state
->set_violating_ports(imv_scanner_state
, buf
);
323 state
->set_recommendation(state
,
324 TNC_IMV_ACTION_RECOMMENDATION_NO_ACCESS
,
325 TNC_IMV_EVALUATION_RESULT_NONCOMPLIANT_MAJOR
);
329 enumerator
->destroy(enumerator
);
330 pa_tnc_msg
->destroy(pa_tnc_msg
);
334 state
->set_recommendation(state
,
335 TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION
,
336 TNC_IMV_EVALUATION_RESULT_ERROR
);
337 return imv_scanner
->provide_recommendation(imv_scanner
, connection_id
);
340 return imv_scanner
->provide_recommendation(imv_scanner
, connection_id
);
344 * see section 3.8.4 of TCG TNC IF-IMV Specification 1.3
346 TNC_Result
TNC_IMV_ReceiveMessage(TNC_IMVID imv_id
,
347 TNC_ConnectionID connection_id
,
348 TNC_BufferReference msg
,
350 TNC_MessageType msg_type
)
352 TNC_VendorID msg_vid
;
353 TNC_MessageSubtype msg_subtype
;
355 msg_vid
= msg_type
>> 8;
356 msg_subtype
= msg_type
& TNC_SUBTYPE_ANY
;
358 return receive_message(imv_id
, connection_id
, 0, chunk_create(msg
, msg_len
),
359 msg_vid
, msg_subtype
, 0, TNC_IMVID_ANY
);
363 * see section 3.8.6 of TCG TNC IF-IMV Specification 1.3
365 TNC_Result
TNC_IMV_ReceiveMessageLong(TNC_IMVID imv_id
,
366 TNC_ConnectionID connection_id
,
367 TNC_UInt32 msg_flags
,
368 TNC_BufferReference msg
,
370 TNC_VendorID msg_vid
,
371 TNC_MessageSubtype msg_subtype
,
372 TNC_UInt32 src_imc_id
,
373 TNC_UInt32 dst_imv_id
)
375 return receive_message(imv_id
, connection_id
, msg_flags
,
376 chunk_create(msg
, msg_len
), msg_vid
, msg_subtype
,
377 src_imc_id
, dst_imv_id
);
381 * see section 3.8.7 of TCG TNC IF-IMV Specification 1.3
383 TNC_Result
TNC_IMV_SolicitRecommendation(TNC_IMVID imv_id
,
384 TNC_ConnectionID connection_id
)
388 DBG1(DBG_IMV
, "IMV \"%s\" has not been initialized", imv_name
);
389 return TNC_RESULT_NOT_INITIALIZED
;
391 return imv_scanner
->provide_recommendation(imv_scanner
, connection_id
);
395 * see section 3.8.8 of TCG TNC IF-IMV Specification 1.3
397 TNC_Result
TNC_IMV_BatchEnding(TNC_IMVID imv_id
,
398 TNC_ConnectionID connection_id
)
402 DBG1(DBG_IMV
, "IMV \"%s\" has not been initialized", imv_name
);
403 return TNC_RESULT_NOT_INITIALIZED
;
405 return TNC_RESULT_SUCCESS
;
409 * see section 3.8.9 of TCG TNC IF-IMV Specification 1.3
411 TNC_Result
TNC_IMV_Terminate(TNC_IMVID imv_id
)
415 DBG1(DBG_IMV
, "IMV \"%s\" has not been initialized", imv_name
);
416 return TNC_RESULT_NOT_INITIALIZED
;
418 tcp_ports
->destroy_function(tcp_ports
, free
);
419 udp_ports
->destroy_function(udp_ports
, free
);
420 imv_scanner
->destroy(imv_scanner
);
423 return TNC_RESULT_SUCCESS
;
427 * see section 4.2.8.1 of TCG TNC IF-IMV Specification 1.3
429 TNC_Result
TNC_IMV_ProvideBindFunction(TNC_IMVID imv_id
,
430 TNC_TNCS_BindFunctionPointer bind_function
)
434 DBG1(DBG_IMV
, "IMV \"%s\" has not been initialized", imv_name
);
435 return TNC_RESULT_NOT_INITIALIZED
;
437 return imv_scanner
->bind_functions(imv_scanner
, bind_function
);