2 * Copyright (C) 2011 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 "tnc_ifmap_listener.h"
19 #include <config/child_cfg.h>
21 #include <axis2_util.h>
22 #include <axis2_client.h>
23 #include <axiom_soap.h>
25 #define IFMAP_NS "http://www.trustedcomputinggroup.org/2010/IFMAP/2"
26 #define IFMAP_META_NS "http://www.trustedcomputinggroup.org/2010/IFMAP-METADATA/2"
27 #define IFMAP_LOGFILE "strongswan_ifmap.log"
28 #define IFMAP_SERVER "https://localhost:8443/"
30 typedef struct private_tnc_ifmap_listener_t private_tnc_ifmap_listener_t
;
33 * Private data of an tnc_ifmap_listener_t object.
35 struct private_tnc_ifmap_listener_t
{
38 * Public tnc_ifmap_listener_t interface.
40 tnc_ifmap_listener_t
public;
48 * Axis2 service client
50 axis2_svc_client_t
* svc_client
;
60 char *ifmap_publisher_id
;
64 static bool newSession(private_tnc_ifmap_listener_t
*this)
66 axiom_node_t
*request
, *result
, *node
;
68 axiom_namespace_t
*ns
;
69 axiom_attribute_t
*attr
;
71 axutil_qname_t
*qname
;
75 /* build newSession request */
76 ns
= axiom_namespace_create(this->env
, IFMAP_NS
, "ifmap");
77 el
= axiom_element_create(this->env
, NULL
, "newSession", ns
, &request
);
78 attr
= axiom_attribute_create(this->env
, "max-poll-result-size", "1000000", NULL
);
79 axiom_element_add_attribute(el
, this->env
, attr
, request
);
81 /* send newSession request */
82 result
= axis2_svc_client_send_receive(this->svc_client
, this->env
, request
);
88 /* process newSessionResult */
89 node
= axiom_node_get_first_child(result
, this->env
);
90 if (node
&& axiom_node_get_node_type(node
, this->env
) == AXIOM_ELEMENT
)
92 el
= (axiom_element_t
*)axiom_node_get_data_element(node
, this->env
);
93 qname
= axiom_element_get_qname(el
, this->env
, node
);
94 success
= streq("newSessionResult",
95 axutil_qname_to_string(qname
, this->env
));
97 /* process the attributes */
100 value
= axiom_element_get_attribute_value_by_name(el
, this->env
,
102 this->session_id
= strdup(value
);
103 value
= axiom_element_get_attribute_value_by_name(el
, this->env
,
104 "ifmap-publisher-id");
105 this->ifmap_publisher_id
= strdup(value
);
107 DBG1(DBG_TNC
, "session-id: %s, ifmap-publisher-id: %s",
108 this->session_id
, this->ifmap_publisher_id
);
109 success
= this->session_id
&& this->ifmap_publisher_id
;
111 value
= axiom_element_get_attribute_value_by_name(el
, this->env
,
112 "max-poll-result-size");
115 DBG1(DBG_TNC
, "max-poll-result-size: %s", value
);
120 DBG1(DBG_TNC
, "%s", axiom_element_to_string(el
, this->env
, node
));
123 axiom_node_free_tree(result
, this->env
);
128 static bool purgePublisher(private_tnc_ifmap_listener_t
*this)
130 axiom_node_t
*request
, *result
, *node
;
132 axiom_namespace_t
*ns
;
133 axiom_attribute_t
*attr
;
134 axutil_qname_t
*qname
;
135 bool success
= FALSE
;
137 /* build purgePublisher request */
138 ns
= axiom_namespace_create(this->env
, IFMAP_NS
, "ifmap");
139 el
= axiom_element_create(this->env
, NULL
, "purgePublisher", ns
,
141 attr
= axiom_attribute_create(this->env
, "session-id",
142 this->session_id
, NULL
);
143 axiom_element_add_attribute(el
, this->env
, attr
, request
);
144 attr
= axiom_attribute_create(this->env
, "ifmap-publisher-id",
145 this->ifmap_publisher_id
, NULL
);
146 axiom_element_add_attribute(el
, this->env
, attr
, request
);
148 /* send purgePublisher request */
149 result
= axis2_svc_client_send_receive(this->svc_client
, this->env
, request
);
155 /* process purgePublisherReceived */
156 node
= axiom_node_get_first_child(result
, this->env
);
157 if (node
&& axiom_node_get_node_type(node
, this->env
) == AXIOM_ELEMENT
)
159 el
= (axiom_element_t
*)axiom_node_get_data_element(node
, this->env
);
160 qname
= axiom_element_get_qname(el
, this->env
, node
);
161 success
= streq("purgePublisherReceived",
162 axutil_qname_to_string(qname
, this->env
));
165 DBG1(DBG_TNC
, "%s", axiom_element_to_string(el
, this->env
, node
));
168 axiom_node_free_tree(result
, this->env
);
173 static bool publish(private_tnc_ifmap_listener_t
*this, u_int32_t ike_sa_id
,
174 identification_t
*id
, host_t
*host
, bool up
)
176 axiom_node_t
*request
, *result
, *node
, *node2
, *node3
;
178 axiom_namespace_t
*ns
, *ns_meta
;
179 axiom_attribute_t
*attr
;
180 char buf
[BUF_LEN
], *id_type
;
182 /* build publish request */
183 ns
= axiom_namespace_create(this->env
, IFMAP_NS
, "ifmap");
184 el
= axiom_element_create(this->env
, NULL
, "publish", ns
, &request
);
185 attr
= axiom_attribute_create(this->env
, "session-id", this->session_id
,
187 axiom_element_add_attribute(el
, this->env
, attr
, request
);
189 /* update or delete IKE_SA information */
192 el
= axiom_element_create(this->env
, NULL
, "update", NULL
, &node
);
193 axiom_node_add_child(request
, this->env
, node
);
197 el
= axiom_element_create(this->env
, NULL
, "delete", NULL
, &node
);
198 axiom_node_add_child(request
, this->env
, node
);
201 attr
= axiom_attribute_create(this->env
, "filter",
202 "authenticated-as", NULL
);
203 axiom_element_add_attribute(el
, this->env
, attr
, node
);
206 /* add access-request */
207 el
= axiom_element_create(this->env
, NULL
, "access-request", NULL
, &node2
);
208 axiom_node_add_child(node
, this->env
, node2
);
210 snprintf(buf
, BUF_LEN
, "%s:%d", this->ifmap_publisher_id
, ike_sa_id
);
211 attr
= axiom_attribute_create(this->env
, "name", buf
, NULL
);
212 axiom_element_add_attribute(el
, this->env
, attr
, node2
);
215 el
= axiom_element_create(this->env
, NULL
, "identity", NULL
, &node2
);
216 axiom_node_add_child(node
, this->env
, node2
);
218 snprintf(buf
, BUF_LEN
, "%Y", id
);
219 attr
= axiom_attribute_create(this->env
, "name", buf
, NULL
);
220 axiom_element_add_attribute(el
, this->env
, attr
, node2
);
222 switch (id
->get_type(id
))
225 id_type
= "dns-name";
228 id_type
= "email-address";
231 id_type
= "distinguished-name";
236 attr
= axiom_attribute_create(this->env
, "type", id_type
, NULL
);
237 axiom_element_add_attribute(el
, this->env
, attr
, node2
);
242 el
= axiom_element_create(this->env
, NULL
, "metadata", NULL
, &node2
);
243 axiom_node_add_child(node
, this->env
, node2
);
245 ns_meta
= axiom_namespace_create(this->env
, IFMAP_META_NS
, "meta");
247 el
= axiom_element_create(this->env
, NULL
, "authenticated-as", ns_meta
,
249 axiom_node_add_child(node2
, this->env
, node3
);
250 attr
= axiom_attribute_create(this->env
, "ifmap-cardinality",
251 "singleValue", NULL
);
252 axiom_element_add_attribute(el
, this->env
, attr
, node3
);
255 /* update or delete IKE_SA information */
258 el
= axiom_element_create(this->env
, NULL
, "update", NULL
, &node
);
259 axiom_node_add_child(request
, this->env
, node
);
263 el
= axiom_element_create(this->env
, NULL
, "delete", NULL
, &node
);
264 axiom_node_add_child(request
, this->env
, node
);
267 attr
= axiom_attribute_create(this->env
, "filter",
268 "acces-request-ip", NULL
);
269 axiom_element_add_attribute(el
, this->env
, attr
, node
);
273 el
= axiom_element_create(this->env
, NULL
, "ip-address", NULL
, &node2
);
274 axiom_node_add_child(node
, this->env
, node2
);
276 snprintf(buf
, BUF_LEN
, "%H", host
);
277 attr
= axiom_attribute_create(this->env
, "value", buf
, NULL
);
278 axiom_element_add_attribute(el
, this->env
, attr
, node2
);
280 attr
= axiom_attribute_create(this->env
, "type",
281 host
->get_family(host
) == AF_INET ?
"IPv4" : "IPv6", NULL
);
282 axiom_element_add_attribute(el
, this->env
, attr
, node2
);
284 /* add access-request */
285 el
= axiom_element_create(this->env
, NULL
, "access-request", NULL
, &node2
);
286 axiom_node_add_child(node
, this->env
, node2
);
288 snprintf(buf
, BUF_LEN
, "%s:%d", this->ifmap_publisher_id
, ike_sa_id
);
289 attr
= axiom_attribute_create(this->env
, "name", buf
, NULL
);
290 axiom_element_add_attribute(el
, this->env
, attr
, node2
);
295 el
= axiom_element_create(this->env
, NULL
, "metadata", NULL
, &node2
);
296 axiom_node_add_child(node
, this->env
, node2
);
297 ns_meta
= axiom_namespace_create(this->env
, IFMAP_META_NS
, "meta");
298 el
= axiom_element_create(this->env
, NULL
, "access-request-ip", ns_meta
,
300 axiom_node_add_child(node2
, this->env
, node3
);
301 attr
= axiom_attribute_create(this->env
, "ifmap-cardinality",
302 "singleValue", NULL
);
303 axiom_element_add_attribute(el
, this->env
, attr
, node3
);
306 /* send publish request */
307 result
= axis2_svc_client_send_receive(this->svc_client
, this->env
, request
);
313 /* process publishReceived */
314 node
= axiom_node_get_first_child(result
, this->env
);
315 axiom_node_free_tree(result
, this->env
);
320 static bool endSession(private_tnc_ifmap_listener_t
*this)
322 axiom_node_t
*request
, *result
, *node
;
324 axiom_namespace_t
*ns
;
325 axiom_attribute_t
*attr
;
326 axutil_qname_t
*qname
;
327 bool success
= FALSE
;
329 /* build endSession request */
330 ns
= axiom_namespace_create(this->env
, IFMAP_NS
, "ifmap");
331 el
= axiom_element_create(this->env
, NULL
, "endSession", ns
, &request
);
332 attr
= axiom_attribute_create(this->env
, "session-id", this->session_id
, NULL
);
333 axiom_element_add_attribute(el
, this->env
, attr
, request
);
335 /* send endSession request */
336 result
= axis2_svc_client_send_receive(this->svc_client
, this->env
, request
);
342 /* process endSessionResult */
343 node
= axiom_node_get_first_child(result
, this->env
);
344 if (node
&& axiom_node_get_node_type(node
, this->env
) == AXIOM_ELEMENT
)
346 el
= (axiom_element_t
*)axiom_node_get_data_element(node
, this->env
);
347 qname
= axiom_element_get_qname(el
, this->env
, node
);
348 success
= streq("endSessionResult",
349 axutil_qname_to_string(qname
, this->env
));
352 DBG1(DBG_TNC
, "%s", axiom_element_to_string(el
, this->env
, node
));
355 axiom_node_free_tree(result
, this->env
);
362 METHOD(listener_t
, ike_updown
, bool,
363 private_tnc_ifmap_listener_t
*this, ike_sa_t
*ike_sa
, bool up
)
366 identification_t
*id
;
369 ike_sa_id
= ike_sa
->get_unique_id(ike_sa
);
370 id
= ike_sa
->get_other_id(ike_sa
);
371 host
= ike_sa
->get_other_host(ike_sa
);
373 DBG2(DBG_TNC
, "sending publish");
374 if (!publish(this, ike_sa_id
, id
, host
, up
))
376 DBG1(DBG_TNC
, "publish with MAP server failed");
382 METHOD(tnc_ifmap_listener_t
, destroy
, void,
383 private_tnc_ifmap_listener_t
*this)
385 if (this->session_id
)
387 DBG2(DBG_TNC
, "sending endSession");
388 if (!endSession(this))
390 DBG1(DBG_TNC
, "endSession with MAP server failed");
392 free(this->session_id
);
393 free(this->ifmap_publisher_id
);
395 if (this->svc_client
)
397 axis2_svc_client_free(this->svc_client
, this->env
);
401 axutil_env_free(this->env
);
409 tnc_ifmap_listener_t
*tnc_ifmap_listener_create()
411 private_tnc_ifmap_listener_t
*this;
412 axis2_char_t
*server
, *client_home
, *username
, *password
, *auth_type
;
413 axis2_endpoint_ref_t
* endpoint_ref
= NULL
;
414 axis2_options_t
*options
= NULL
;
416 client_home
= lib
->settings
->get_str(lib
->settings
,
417 "charon.plugins.tnc-ifmap.client_home",
418 AXIS2_GETENV("AXIS2C_HOME"));
419 server
= lib
->settings
->get_str(lib
->settings
,
420 "charon.plugins.tnc-ifmap.server", IFMAP_SERVER
);
421 auth_type
= lib
->settings
->get_str(lib
->settings
,
422 "charon.plugins.tnc-ifmap.auth_type", "Basic");
423 username
= lib
->settings
->get_str(lib
->settings
,
424 "charon.plugins.tnc-ifmap.username", NULL
);
425 password
= lib
->settings
->get_str(lib
->settings
,
426 "charon.plugins.tnc-ifmap.password", NULL
);
428 if (!username
|| !password
)
430 DBG1(DBG_TNC
, "MAP client %s%s%s not defined",
431 (!username
) ?
"username" : "",
432 (!username
&& ! password
) ?
" and " : "",
433 (!password
) ?
"password" : "");
439 .ike_updown
= _ike_updown
,
445 /* Create Axis2/C environment and options */
446 this->env
= axutil_env_create_all(IFMAP_LOGFILE
, AXIS2_LOG_LEVEL_TRACE
);
447 options
= axis2_options_create(this->env
);
449 /* Define the IF-MAP server as the to endpoint reference */
450 endpoint_ref
= axis2_endpoint_ref_create(this->env
, server
);
451 axis2_options_set_to(options
, this->env
, endpoint_ref
);
453 /* Create the axis2 service client */
454 this->svc_client
= axis2_svc_client_create(this->env
, client_home
);
455 if (!this->svc_client
)
457 DBG1(DBG_TNC
, "Error creating axis2 service client");
458 AXIS2_LOG_ERROR(this->env
->log
, AXIS2_LOG_SI
,
459 "Stub invoke FAILED: Error code: %d :: %s",
460 this->env
->error
->error_number
,
461 AXIS2_ERROR_GET_MESSAGE(this->env
->error
));
466 axis2_svc_client_set_options(this->svc_client
, this->env
, options
);
467 axis2_options_set_http_auth_info(options
, this->env
, username
, password
,
469 DBG1(DBG_TNC
, "connecting as MAP client '%s' to MAP server at '%s'",
472 DBG2(DBG_TNC
, "sending newSession");
473 if (!newSession(this))
475 DBG1(DBG_TNC
, "newSession with MAP server failed");
479 DBG2(DBG_TNC
, "sending purgePublisher");
480 if (!purgePublisher(this))
482 DBG1(DBG_TNC
, "purgePublisher with MAP server failed");
487 return &this->public;