combined newSession and newSessionResult
[strongswan.git] / src / libcharon / plugins / tnc_ifmap / tnc_ifmap_listener.c
1 /*
2 * Copyright (C) 2011 Andreas Steffen
3 * HSR Hochschule fuer Technik Rapperswil
4 *
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>.
9 *
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
13 * for more details.
14 */
15
16 #include "tnc_ifmap_listener.h"
17
18 #include <daemon.h>
19 #include <config/child_cfg.h>
20
21 #include <axis2_util.h>
22 #include <axis2_client.h>
23 #include <axiom_soap.h>
24
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/"
28
29 typedef struct private_tnc_ifmap_listener_t private_tnc_ifmap_listener_t;
30
31 /**
32 * Private data of an tnc_ifmap_listener_t object.
33 */
34 struct private_tnc_ifmap_listener_t {
35
36 /**
37 * Public tnc_ifmap_listener_t interface.
38 */
39 tnc_ifmap_listener_t public;
40
41 /**
42 * Axis2/C environment
43 */
44 axutil_env_t *env;
45
46 /**
47 * Axis2 service client
48 */
49 axis2_svc_client_t* svc_client;
50
51 /**
52 * SOAP Session ID
53 */
54 char *session_id;
55
56 /**
57 * IF-MAP Publisher ID
58 */
59 char *ifmap_publisher_id;
60
61 };
62
63 static bool newSession(private_tnc_ifmap_listener_t *this)
64 {
65 axiom_node_t *request, *result, *node;
66 axiom_element_t *el;
67 axiom_namespace_t *ns;
68 axis2_char_t *value;
69 bool success = FALSE;
70
71 /* build newSession request */
72 ns = axiom_namespace_create(this->env, IFMAP_NAMESPACE, "ifmap");
73 el = axiom_element_create(this->env, NULL, "newSession", ns, &request);
74
75 /* send newSession request */
76 result = axis2_svc_client_send_receive(this->svc_client, this->env, request);
77 if (!result)
78 {
79 return FALSE;
80 }
81
82 /* process newSessionResult */
83 node = axiom_node_get_first_child(result, this->env);
84 if (node && axiom_node_get_node_type(node, this->env) == AXIOM_ELEMENT)
85 {
86 el = (axiom_element_t *)axiom_node_get_data_element(node, this->env);
87 value = axiom_element_get_attribute_value_by_name(el, this->env,
88 "session-id");
89 this->session_id = strdup(value);
90 value = axiom_element_get_attribute_value_by_name(el, this->env,
91 "ifmap-publisher-id");
92 this->ifmap_publisher_id = strdup(value);
93
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;
97 }
98 axiom_node_free_tree(result, this->env);
99
100 return success;
101 }
102
103 static bool endSession(private_tnc_ifmap_listener_t *this)
104 {
105 axiom_node_t *request, *result, *node;
106 axiom_element_t *el;
107 axiom_namespace_t *ns;
108 axiom_attribute_t *attr;
109
110 /* build endSession request */
111 ns = axiom_namespace_create(this->env, IFMAP_NAMESPACE, "ifmap");
112 el = axiom_element_create(this->env, NULL, "endSession", ns, &request);
113 attr = axiom_attribute_create(this->env, "session-id", this->session_id, NULL);
114 axiom_element_add_attribute(el, this->env, attr, request);
115
116 /* send endSession request */
117 result = axis2_svc_client_send_receive(this->svc_client, this->env, request);
118 if (!result)
119 {
120 return FALSE;
121 }
122
123 /* process newSessionResult */
124 node = axiom_node_get_first_child(result, this->env);
125 axiom_node_free_tree(result, this->env);
126
127 return TRUE;
128 }
129
130 METHOD(listener_t, child_updown, bool,
131 private_tnc_ifmap_listener_t *this, ike_sa_t *ike_sa, child_sa_t *child_sa,
132 bool up)
133 {
134 traffic_selector_t *my_ts, *other_ts;
135 enumerator_t *enumerator;
136 child_cfg_t *config;
137 host_t *vip, *me, *other;
138
139 config = child_sa->get_config(child_sa);
140 vip = ike_sa->get_virtual_ip(ike_sa, TRUE);
141 me = ike_sa->get_my_host(ike_sa);
142 other = ike_sa->get_other_host(ike_sa);
143
144 return TRUE;
145 }
146
147 METHOD(tnc_ifmap_listener_t, destroy, void,
148 private_tnc_ifmap_listener_t *this)
149 {
150 if (this->session_id)
151 {
152 DBG2(DBG_TNC, "sending endSession");
153 if (!endSession(this))
154 {
155 DBG1(DBG_TNC, "endSession with MAP server failed");
156 }
157 free(this->session_id);
158 free(this->ifmap_publisher_id);
159 }
160 if (this->svc_client)
161 {
162 axis2_svc_client_free(this->svc_client, this->env);
163 }
164 if (this->env)
165 {
166 axutil_env_free(this->env);
167 }
168 free(this);
169 }
170
171 /**
172 * See header
173 */
174 tnc_ifmap_listener_t *tnc_ifmap_listener_create()
175 {
176 private_tnc_ifmap_listener_t *this;
177 axis2_char_t *server, *client_home, *username, *password, *auth_type;
178 axis2_endpoint_ref_t* endpoint_ref = NULL;
179 axis2_options_t *options = NULL;
180
181 client_home = lib->settings->get_str(lib->settings,
182 "charon.plugins.tnc-ifmap.client_home",
183 AXIS2_GETENV("AXIS2C_HOME"));
184 server = lib->settings->get_str(lib->settings,
185 "charon.plugins.tnc-ifmap.server", IFMAP_SERVER);
186 auth_type = lib->settings->get_str(lib->settings,
187 "charon.plugins.tnc-ifmap.auth_type", "Basic");
188 username = lib->settings->get_str(lib->settings,
189 "charon.plugins.tnc-ifmap.username", NULL);
190 password = lib->settings->get_str(lib->settings,
191 "charon.plugins.tnc-ifmap.password", NULL);
192
193 if (!username || !password)
194 {
195 DBG1(DBG_TNC, "MAP client %s%s%s not defined",
196 (!username) ? "username" : "",
197 (!username && ! password) ? " and " : "",
198 (!password) ? "password" : "");
199 }
200
201 INIT(this,
202 .public = {
203 .listener = {
204 .child_updown = _child_updown,
205 },
206 .destroy = _destroy,
207 },
208 );
209
210 /* Create Axis2/C environment and options */
211 this->env = axutil_env_create_all(IFMAP_LOGFILE, AXIS2_LOG_LEVEL_TRACE);
212 options = axis2_options_create(this->env);
213
214 /* Define the IF-MAP server as the to endpoint reference */
215 endpoint_ref = axis2_endpoint_ref_create(this->env, server);
216 axis2_options_set_to(options, this->env, endpoint_ref);
217
218 /* Create the axis2 service client */
219 this->svc_client = axis2_svc_client_create(this->env, client_home);
220 if (!this->svc_client)
221 {
222 DBG1(DBG_TNC, "Error creating axis2 service client");
223 AXIS2_LOG_ERROR(this->env->log, AXIS2_LOG_SI,
224 "Stub invoke FAILED: Error code: %d :: %s",
225 this->env->error->error_number,
226 AXIS2_ERROR_GET_MESSAGE(this->env->error));
227 destroy(this);
228 return NULL;
229 }
230
231 axis2_svc_client_set_options(this->svc_client, this->env, options);
232 axis2_options_set_http_auth_info(options, this->env, username, password,
233 auth_type);
234 DBG1(DBG_TNC, "connecting as MAP client '%s' to MAP server at '%s'",
235 username, server);
236
237 DBG2(DBG_TNC, "sending newSession");
238 if (!newSession(this))
239 {
240 DBG1(DBG_TNC, "newSession with MAP server failed");
241 destroy(this);
242 return NULL;
243 }
244
245 return &this->public;
246 }
247