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.7.1 of TCG TNC IF-IMV Specification 1.2
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.7.2 of TCG TNC IF-IMV Specification 1.2
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
,
179 * see section 3.7.3 of TCG TNC IF-IMV Specification 1.2
181 TNC_Result
TNC_IMV_ReceiveMessage(TNC_IMVID imv_id
,
182 TNC_ConnectionID connection_id
,
183 TNC_BufferReference msg
,
185 TNC_MessageType msg_type
)
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
, connection_id
,
208 chunk_create(msg
, msg_len
), msg_type
,
211 /* no parsed PA-TNC attributes available if an error occurred */
217 /* analyze PA-TNC attributes */
218 enumerator
= pa_tnc_msg
->create_attribute_enumerator(pa_tnc_msg
);
219 while (enumerator
->enumerate(enumerator
, &attr
))
221 if (attr
->get_vendor_id(attr
) != PEN_IETF
)
226 if (attr
->get_type(attr
) == IETF_ATTR_PA_TNC_ERROR
)
228 ietf_attr_pa_tnc_error_t
*error_attr
;
229 pa_tnc_error_code_t error_code
;
230 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_ATTR_TYPE_NOT_SUPPORTED
:
241 attr_info
= error_attr
->get_attr_info(error_attr
);
242 DBG1(DBG_IMV
, " unsupported attribute %#B", &attr_info
);
249 else if (attr
->get_type(attr
) == IETF_ATTR_PORT_FILTER
)
251 ietf_attr_port_filter_t
*attr_port_filter
;
252 enumerator_t
*enumerator
;
255 char buf
[BUF_LEN
], *pos
= buf
;
256 size_t len
= BUF_LEN
;
257 bool blocked
, compliant
= TRUE
;
259 attr_port_filter
= (ietf_attr_port_filter_t
*)attr
;
260 enumerator
= attr_port_filter
->create_port_enumerator(attr_port_filter
);
261 while (enumerator
->enumerate(enumerator
, &blocked
, &protocol
, &port
))
264 port_range_t
*port_range
;
265 bool passed
, found
= FALSE
;
270 /* ignore closed ports */
274 e
= (protocol
== IPPROTO_TCP
) ?
275 tcp_ports
->create_enumerator(tcp_ports
) :
276 udp_ports
->create_enumerator(udp_ports
);
277 while (e
->enumerate(e
, &port_range
))
279 if (port
>= port_range
->start
&& port
<= port_range
->stop
)
287 passed
= (closed_port_policy
== found
);
288 DBG2(DBG_IMV
, "%s port %5u %s: %s",
289 (protocol
== IPPROTO_TCP
) ?
"tcp" : "udp", port
,
290 blocked ?
"closed" : "open", passed ?
"ok" : "fatal");
294 written
= snprintf(pos
, len
, " %s/%u",
295 (protocol
== IPPROTO_TCP
) ?
"tcp" : "udp",
297 if (written
< 0 || written
>= len
)
305 enumerator
->destroy(enumerator
);
309 state
->set_recommendation(state
,
310 TNC_IMV_ACTION_RECOMMENDATION_ALLOW
,
311 TNC_IMV_EVALUATION_RESULT_COMPLIANT
);
315 imv_scanner_state_t
*imv_scanner_state
;
317 imv_scanner_state
= (imv_scanner_state_t
*)state
;
318 imv_scanner_state
->set_violating_ports(imv_scanner_state
, buf
);
319 state
->set_recommendation(state
,
320 TNC_IMV_ACTION_RECOMMENDATION_NO_ACCESS
,
321 TNC_IMV_EVALUATION_RESULT_NONCOMPLIANT_MAJOR
);
325 enumerator
->destroy(enumerator
);
326 pa_tnc_msg
->destroy(pa_tnc_msg
);
330 state
->set_recommendation(state
,
331 TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION
,
332 TNC_IMV_EVALUATION_RESULT_ERROR
);
333 return imv_scanner
->provide_recommendation(imv_scanner
, connection_id
);
336 return imv_scanner
->provide_recommendation(imv_scanner
, connection_id
);
340 * see section 3.7.4 of TCG TNC IF-IMV Specification 1.2
342 TNC_Result
TNC_IMV_SolicitRecommendation(TNC_IMVID imv_id
,
343 TNC_ConnectionID connection_id
)
347 DBG1(DBG_IMV
, "IMV \"%s\" has not been initialized", imv_name
);
348 return TNC_RESULT_NOT_INITIALIZED
;
350 return imv_scanner
->provide_recommendation(imv_scanner
, connection_id
);
354 * see section 3.7.5 of TCG TNC IF-IMV Specification 1.2
356 TNC_Result
TNC_IMV_BatchEnding(TNC_IMVID imv_id
,
357 TNC_ConnectionID connection_id
)
361 DBG1(DBG_IMV
, "IMV \"%s\" has not been initialized", imv_name
);
362 return TNC_RESULT_NOT_INITIALIZED
;
364 return TNC_RESULT_SUCCESS
;
368 * see section 3.7.6 of TCG TNC IF-IMV Specification 1.2
370 TNC_Result
TNC_IMV_Terminate(TNC_IMVID imv_id
)
374 DBG1(DBG_IMV
, "IMV \"%s\" has not been initialized", imv_name
);
375 return TNC_RESULT_NOT_INITIALIZED
;
377 tcp_ports
->destroy_function(tcp_ports
, free
);
378 udp_ports
->destroy_function(udp_ports
, free
);
379 imv_scanner
->destroy(imv_scanner
);
382 return TNC_RESULT_SUCCESS
;
386 * see section 4.2.8.1 of TCG TNC IF-IMV Specification 1.2
388 TNC_Result
TNC_IMV_ProvideBindFunction(TNC_IMVID imv_id
,
389 TNC_TNCS_BindFunctionPointer bind_function
)
393 DBG1(DBG_IMV
, "IMV \"%s\" has not been initialized", imv_name
);
394 return TNC_RESULT_NOT_INITIALIZED
;
396 return imv_scanner
->bind_functions(imv_scanner
, bind_function
);