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
, *node4
;
178 axiom_namespace_t
*ns
, *ns_meta
;
179 axiom_attribute_t
*attr
;
181 axutil_qname_t
*qname
;
182 char buf
[BUF_LEN
], *id_type
;
183 bool success
= FALSE
;
185 /* build publish request */
186 ns
= axiom_namespace_create(this->env
, IFMAP_NS
, "ifmap");
187 el
= axiom_element_create(this->env
, NULL
, "publish", ns
, &request
);
188 attr
= axiom_attribute_create(this->env
, "session-id", this->session_id
,
190 axiom_element_add_attribute(el
, this->env
, attr
, request
);
193 * update or delete authenticated-as metadata
197 el
= axiom_element_create(this->env
, NULL
, "update", NULL
, &node
);
198 axiom_node_add_child(request
, this->env
, node
);
202 el
= axiom_element_create(this->env
, NULL
, "delete", NULL
, &node
);
203 axiom_node_add_child(request
, this->env
, node
);
206 attr
= axiom_attribute_create(this->env
, "filter",
207 "authenticated-as", NULL
);
208 axiom_element_add_attribute(el
, this->env
, attr
, node
);
211 /* add access-request */
212 el
= axiom_element_create(this->env
, NULL
, "access-request", NULL
, &node2
);
213 axiom_node_add_child(node
, this->env
, node2
);
215 snprintf(buf
, BUF_LEN
, "%s:%d", this->ifmap_publisher_id
, ike_sa_id
);
216 attr
= axiom_attribute_create(this->env
, "name", buf
, NULL
);
217 axiom_element_add_attribute(el
, this->env
, attr
, node2
);
220 el
= axiom_element_create(this->env
, NULL
, "identity", NULL
, &node2
);
221 axiom_node_add_child(node
, this->env
, node2
);
223 snprintf(buf
, BUF_LEN
, "%Y", id
);
224 attr
= axiom_attribute_create(this->env
, "name", buf
, NULL
);
225 axiom_element_add_attribute(el
, this->env
, attr
, node2
);
227 switch (id
->get_type(id
))
230 id_type
= "dns-name";
233 id_type
= "email-address";
236 id_type
= "distinguished-name";
241 attr
= axiom_attribute_create(this->env
, "type", id_type
, NULL
);
242 axiom_element_add_attribute(el
, this->env
, attr
, node2
);
247 el
= axiom_element_create(this->env
, NULL
, "metadata", NULL
, &node2
);
248 axiom_node_add_child(node
, this->env
, node2
);
250 ns_meta
= axiom_namespace_create(this->env
, IFMAP_META_NS
, "meta");
252 el
= axiom_element_create(this->env
, NULL
, "authenticated-as", ns_meta
,
254 axiom_node_add_child(node2
, this->env
, node3
);
255 attr
= axiom_attribute_create(this->env
, "ifmap-cardinality",
256 "singleValue", NULL
);
257 axiom_element_add_attribute(el
, this->env
, attr
, node3
);
261 * update or delete access-request-ip metadata
265 el
= axiom_element_create(this->env
, NULL
, "update", NULL
, &node
);
266 axiom_node_add_child(request
, this->env
, node
);
270 el
= axiom_element_create(this->env
, NULL
, "delete", NULL
, &node
);
271 axiom_node_add_child(request
, this->env
, node
);
274 attr
= axiom_attribute_create(this->env
, "filter",
275 "access-request-ip", NULL
);
276 axiom_element_add_attribute(el
, this->env
, attr
, node
);
279 /* add access-request */
280 el
= axiom_element_create(this->env
, NULL
, "access-request", NULL
, &node2
);
281 axiom_node_add_child(node
, this->env
, node2
);
283 snprintf(buf
, BUF_LEN
, "%s:%d", this->ifmap_publisher_id
, ike_sa_id
);
284 attr
= axiom_attribute_create(this->env
, "name", buf
, NULL
);
285 axiom_element_add_attribute(el
, this->env
, attr
, node2
);
288 el
= axiom_element_create(this->env
, NULL
, "ip-address", NULL
, &node2
);
289 axiom_node_add_child(node
, this->env
, node2
);
291 snprintf(buf
, BUF_LEN
, "%H", host
);
292 attr
= axiom_attribute_create(this->env
, "value", buf
, NULL
);
293 axiom_element_add_attribute(el
, this->env
, attr
, node2
);
295 attr
= axiom_attribute_create(this->env
, "type",
296 host
->get_family(host
) == AF_INET ?
"IPv4" : "IPv6", NULL
);
297 axiom_element_add_attribute(el
, this->env
, attr
, node2
);
302 el
= axiom_element_create(this->env
, NULL
, "metadata", NULL
, &node2
);
303 axiom_node_add_child(node
, this->env
, node2
);
304 ns_meta
= axiom_namespace_create(this->env
, IFMAP_META_NS
, "meta");
305 el
= axiom_element_create(this->env
, NULL
, "access-request-ip", ns_meta
,
307 axiom_node_add_child(node2
, this->env
, node3
);
308 attr
= axiom_attribute_create(this->env
, "ifmap-cardinality",
309 "singleValue", NULL
);
310 axiom_element_add_attribute(el
, this->env
, attr
, node3
);
314 * update or delete authenticated-by metadata
318 el
= axiom_element_create(this->env
, NULL
, "update", NULL
, &node
);
319 axiom_node_add_child(request
, this->env
, node
);
323 el
= axiom_element_create(this->env
, NULL
, "delete", NULL
, &node
);
324 axiom_node_add_child(request
, this->env
, node
);
327 attr
= axiom_attribute_create(this->env
, "filter",
328 "authenticated-by", NULL
);
329 axiom_element_add_attribute(el
, this->env
, attr
, node
);
332 /* add access-request */
333 el
= axiom_element_create(this->env
, NULL
, "access-request", NULL
, &node2
);
334 axiom_node_add_child(node
, this->env
, node2
);
336 snprintf(buf
, BUF_LEN
, "%s:%d", this->ifmap_publisher_id
, ike_sa_id
);
337 attr
= axiom_attribute_create(this->env
, "name", buf
, NULL
);
338 axiom_element_add_attribute(el
, this->env
, attr
, node2
);
341 el
= axiom_element_create(this->env
, NULL
, "device", NULL
, &node2
);
342 axiom_node_add_child(node
, this->env
, node2
);
343 el
= axiom_element_create(this->env
, NULL
, "name", NULL
, &node3
);
344 axiom_node_add_child(node2
, this->env
, node3
);
345 text
= axiom_text_create(this->env
, node3
, this->ifmap_publisher_id
, &node4
);
350 el
= axiom_element_create(this->env
, NULL
, "metadata", NULL
, &node2
);
351 axiom_node_add_child(node
, this->env
, node2
);
352 ns_meta
= axiom_namespace_create(this->env
, IFMAP_META_NS
, "meta");
353 el
= axiom_element_create(this->env
, NULL
, "authenticated-by", ns_meta
,
355 axiom_node_add_child(node2
, this->env
, node3
);
356 attr
= axiom_attribute_create(this->env
, "ifmap-cardinality",
357 "singleValue", NULL
);
358 axiom_element_add_attribute(el
, this->env
, attr
, node3
);
361 /* send publish request */
362 result
= axis2_svc_client_send_receive(this->svc_client
, this->env
, request
);
368 /* process publishReceived */
369 node
= axiom_node_get_first_child(result
, this->env
);
370 if (node
&& axiom_node_get_node_type(node
, this->env
) == AXIOM_ELEMENT
)
372 el
= (axiom_element_t
*)axiom_node_get_data_element(node
, this->env
);
373 qname
= axiom_element_get_qname(el
, this->env
, node
);
374 success
= streq("publishReceived",
375 axutil_qname_to_string(qname
, this->env
));
378 DBG1(DBG_TNC
, "%s", axiom_element_to_string(el
, this->env
, node
));
381 axiom_node_free_tree(result
, this->env
);
386 static bool endSession(private_tnc_ifmap_listener_t
*this)
388 axiom_node_t
*request
, *result
, *node
;
390 axiom_namespace_t
*ns
;
391 axiom_attribute_t
*attr
;
392 axutil_qname_t
*qname
;
393 bool success
= FALSE
;
395 /* build endSession request */
396 ns
= axiom_namespace_create(this->env
, IFMAP_NS
, "ifmap");
397 el
= axiom_element_create(this->env
, NULL
, "endSession", ns
, &request
);
398 attr
= axiom_attribute_create(this->env
, "session-id", this->session_id
, NULL
);
399 axiom_element_add_attribute(el
, this->env
, attr
, request
);
401 /* send endSession request */
402 result
= axis2_svc_client_send_receive(this->svc_client
, this->env
, request
);
408 /* process endSessionResult */
409 node
= axiom_node_get_first_child(result
, this->env
);
410 if (node
&& axiom_node_get_node_type(node
, this->env
) == AXIOM_ELEMENT
)
412 el
= (axiom_element_t
*)axiom_node_get_data_element(node
, this->env
);
413 qname
= axiom_element_get_qname(el
, this->env
, node
);
414 success
= streq("endSessionResult",
415 axutil_qname_to_string(qname
, this->env
));
418 DBG1(DBG_TNC
, "%s", axiom_element_to_string(el
, this->env
, node
));
421 axiom_node_free_tree(result
, this->env
);
428 METHOD(listener_t
, ike_updown
, bool,
429 private_tnc_ifmap_listener_t
*this, ike_sa_t
*ike_sa
, bool up
)
432 identification_t
*id
;
435 ike_sa_id
= ike_sa
->get_unique_id(ike_sa
);
436 id
= ike_sa
->get_other_id(ike_sa
);
437 host
= ike_sa
->get_other_host(ike_sa
);
439 DBG2(DBG_TNC
, "sending publish");
440 if (!publish(this, ike_sa_id
, id
, host
, up
))
442 DBG1(DBG_TNC
, "publish with MAP server failed");
448 METHOD(tnc_ifmap_listener_t
, destroy
, void,
449 private_tnc_ifmap_listener_t
*this)
451 if (this->session_id
)
453 DBG2(DBG_TNC
, "sending endSession");
454 if (!endSession(this))
456 DBG1(DBG_TNC
, "endSession with MAP server failed");
458 free(this->session_id
);
459 free(this->ifmap_publisher_id
);
461 if (this->svc_client
)
463 axis2_svc_client_free(this->svc_client
, this->env
);
467 axutil_env_free(this->env
);
475 tnc_ifmap_listener_t
*tnc_ifmap_listener_create()
477 private_tnc_ifmap_listener_t
*this;
478 axis2_char_t
*server
, *client_home
, *username
, *password
, *auth_type
;
479 axis2_endpoint_ref_t
* endpoint_ref
= NULL
;
480 axis2_options_t
*options
= NULL
;
482 client_home
= lib
->settings
->get_str(lib
->settings
,
483 "charon.plugins.tnc-ifmap.client_home",
484 AXIS2_GETENV("AXIS2C_HOME"));
485 server
= lib
->settings
->get_str(lib
->settings
,
486 "charon.plugins.tnc-ifmap.server", IFMAP_SERVER
);
487 auth_type
= lib
->settings
->get_str(lib
->settings
,
488 "charon.plugins.tnc-ifmap.auth_type", "Basic");
489 username
= lib
->settings
->get_str(lib
->settings
,
490 "charon.plugins.tnc-ifmap.username", NULL
);
491 password
= lib
->settings
->get_str(lib
->settings
,
492 "charon.plugins.tnc-ifmap.password", NULL
);
494 if (!username
|| !password
)
496 DBG1(DBG_TNC
, "MAP client %s%s%s not defined",
497 (!username
) ?
"username" : "",
498 (!username
&& ! password
) ?
" and " : "",
499 (!password
) ?
"password" : "");
505 .ike_updown
= _ike_updown
,
511 /* Create Axis2/C environment and options */
512 this->env
= axutil_env_create_all(IFMAP_LOGFILE
, AXIS2_LOG_LEVEL_TRACE
);
513 options
= axis2_options_create(this->env
);
515 /* Define the IF-MAP server as the to endpoint reference */
516 endpoint_ref
= axis2_endpoint_ref_create(this->env
, server
);
517 axis2_options_set_to(options
, this->env
, endpoint_ref
);
519 /* Create the axis2 service client */
520 this->svc_client
= axis2_svc_client_create(this->env
, client_home
);
521 if (!this->svc_client
)
523 DBG1(DBG_TNC
, "Error creating axis2 service client");
524 AXIS2_LOG_ERROR(this->env
->log
, AXIS2_LOG_SI
,
525 "Stub invoke FAILED: Error code: %d :: %s",
526 this->env
->error
->error_number
,
527 AXIS2_ERROR_GET_MESSAGE(this->env
->error
));
532 axis2_svc_client_set_options(this->svc_client
, this->env
, options
);
533 axis2_options_set_http_auth_info(options
, this->env
, username
, password
,
535 DBG1(DBG_TNC
, "connecting as MAP client '%s' to MAP server at '%s'",
538 DBG2(DBG_TNC
, "sending newSession");
539 if (!newSession(this))
541 DBG1(DBG_TNC
, "newSession with MAP server failed");
545 DBG2(DBG_TNC
, "sending purgePublisher");
546 if (!purgePublisher(this))
548 DBG1(DBG_TNC
, "purgePublisher with MAP server failed");
553 return &this->public;