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_NAMESPACE "http://www.trustedcomputinggroup.org/2010/IFMAP/2"
26 #define IFMAP_LOGFILE "strongswan_ifmap.log"
27 #define IFMAP_SERVER "https://localhost:8443/"
29 typedef struct private_tnc_ifmap_listener_t private_tnc_ifmap_listener_t
;
32 * Private data of an tnc_ifmap_listener_t object.
34 struct private_tnc_ifmap_listener_t
{
37 * Public tnc_ifmap_listener_t interface.
39 tnc_ifmap_listener_t
public;
47 * Axis2 service client
49 axis2_svc_client_t
* svc_client
;
59 char *ifmap_publisher_id
;
63 static axiom_node_t
* newSession(private_tnc_ifmap_listener_t
*this)
65 axiom_node_t
*node
= NULL
;
67 axiom_namespace_t
*ns
;
69 ns
= axiom_namespace_create(this->env
, IFMAP_NAMESPACE
, "ifmap");
70 el
= axiom_element_create(this->env
, NULL
, "newSession", ns
, &node
);
75 static bool newSessionResult(private_tnc_ifmap_listener_t
*this,
83 node
= axiom_node_get_first_child(result
, this->env
);
84 if (node
&& axiom_node_get_node_type(node
, this->env
) == AXIOM_ELEMENT
)
86 el
= (axiom_element_t
*)axiom_node_get_data_element(node
, this->env
);
87 attr
= axiom_element_get_attribute_value_by_name(el
, this->env
,
89 this->session_id
= strdup(attr
);
90 attr
= axiom_element_get_attribute_value_by_name(el
, this->env
,
91 "ifmap-publisher-id");
92 this->ifmap_publisher_id
= strdup(attr
);
94 DBG1(DBG_TNC
, "session-id: %s, ifmap-publisher-id: %s",
95 this->session_id
, this->ifmap_publisher_id
);
96 success
= this->session_id
&& this->ifmap_publisher_id
;
98 axiom_node_free_tree(result
, this->env
);
103 static axiom_node_t
* endSession(private_tnc_ifmap_listener_t
*this)
105 axiom_node_t
*node
= NULL
;
107 axiom_namespace_t
*ns
;
108 axiom_attribute_t
*attr
;
110 ns
= axiom_namespace_create(this->env
, IFMAP_NAMESPACE
, "ifmap");
111 el
= axiom_element_create(this->env
, NULL
, "endSession", ns
, &node
);
112 attr
= axiom_attribute_create(this->env
, "session-id", this->session_id
, NULL
);
113 axiom_element_add_attribute(el
, this->env
, attr
, node
);
118 METHOD(listener_t
, child_updown
, bool,
119 private_tnc_ifmap_listener_t
*this, ike_sa_t
*ike_sa
, child_sa_t
*child_sa
,
122 traffic_selector_t
*my_ts
, *other_ts
;
123 enumerator_t
*enumerator
;
125 host_t
*vip
, *me
, *other
;
127 config
= child_sa
->get_config(child_sa
);
128 vip
= ike_sa
->get_virtual_ip(ike_sa
, TRUE
);
129 me
= ike_sa
->get_my_host(ike_sa
);
130 other
= ike_sa
->get_other_host(ike_sa
);
135 METHOD(tnc_ifmap_listener_t
, destroy
, void,
136 private_tnc_ifmap_listener_t
*this)
138 if (this->session_id
)
140 axiom_node_t
*request
, *result
;
142 DBG2(DBG_TNC
, "sending endSession");
143 request
= endSession(this);
144 result
= axis2_svc_client_send_receive(this->svc_client
, this->env
,
148 DBG1(DBG_TNC
, "endSession with MAP server failed");
150 free(this->session_id
);
151 free(this->ifmap_publisher_id
);
153 if (this->svc_client
)
155 axis2_svc_client_free(this->svc_client
, this->env
);
159 axutil_env_free(this->env
);
167 tnc_ifmap_listener_t
*tnc_ifmap_listener_create()
169 private_tnc_ifmap_listener_t
*this;
170 axis2_char_t
*server
, *client_home
, *username
, *password
, *auth_type
;
171 axis2_endpoint_ref_t
* endpoint_ref
= NULL
;
172 axis2_options_t
*options
= NULL
;
173 axiom_node_t
*request
, *result
;
175 client_home
= lib
->settings
->get_str(lib
->settings
,
176 "charon.plugins.tnc-ifmap.client_home",
177 AXIS2_GETENV("AXIS2C_HOME"));
178 server
= lib
->settings
->get_str(lib
->settings
,
179 "charon.plugins.tnc-ifmap.server", IFMAP_SERVER
);
180 auth_type
= lib
->settings
->get_str(lib
->settings
,
181 "charon.plugins.tnc-ifmap.auth_type", "Basic");
182 username
= lib
->settings
->get_str(lib
->settings
,
183 "charon.plugins.tnc-ifmap.username", NULL
);
184 password
= lib
->settings
->get_str(lib
->settings
,
185 "charon.plugins.tnc-ifmap.password", NULL
);
187 if (!username
|| !password
)
189 DBG1(DBG_TNC
, "MAP client %s%s%s not defined",
190 (!username
) ?
"username" : "",
191 (!username
&& ! password
) ?
" and " : "",
192 (!password
) ?
"password" : "");
198 .child_updown
= _child_updown
,
204 /* Create Axis2/C environment and options */
205 this->env
= axutil_env_create_all(IFMAP_LOGFILE
, AXIS2_LOG_LEVEL_TRACE
);
206 options
= axis2_options_create(this->env
);
208 /* Define the IF-MAP server as the to endpoint reference */
209 endpoint_ref
= axis2_endpoint_ref_create(this->env
, server
);
210 axis2_options_set_to(options
, this->env
, endpoint_ref
);
212 /* Create the axis2 service client */
213 this->svc_client
= axis2_svc_client_create(this->env
, client_home
);
214 if (!this->svc_client
)
216 DBG1(DBG_TNC
, "Error creating axis2 service client");
217 AXIS2_LOG_ERROR(this->env
->log
, AXIS2_LOG_SI
,
218 "Stub invoke FAILED: Error code: %d :: %s",
219 this->env
->error
->error_number
,
220 AXIS2_ERROR_GET_MESSAGE(this->env
->error
));
225 axis2_svc_client_set_options(this->svc_client
, this->env
, options
);
226 axis2_options_set_http_auth_info(options
, this->env
, username
, password
,
228 DBG1(DBG_TNC
, "connecting as MAP client '%s' to MAP server at '%s'",
231 DBG2(DBG_TNC
, "sending newSession");
232 request
= newSession(this);
233 result
= axis2_svc_client_send_receive(this->svc_client
, this->env
, request
);
234 if (!result
|| !newSessionResult(this, result
))
236 DBG1(DBG_TNC
, "newSession with MAP server failed");
241 return &this->public;