created tnc-ifmap plugin
[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 * Axis2c environment
43 */
44 axutil_env_t *env;
45
46 /**
47 * Axis2c service client
48 */
49 axis2_svc_client_t* svc_client;
50
51 };
52
53 static axiom_node_t* build_request(private_tnc_ifmap_listener_t *this)
54 {
55 axiom_node_t *node = NULL;
56 axiom_element_t *el;
57 axiom_namespace_t *ns;
58
59 ns = axiom_namespace_create(this->env, IFMAP_NAMESPACE, "ifmap");
60 el = axiom_element_create(this->env, NULL, "newSession", ns, &node);
61
62 return node;
63 }
64
65 METHOD(listener_t, child_updown, bool,
66 private_tnc_ifmap_listener_t *this, ike_sa_t *ike_sa, child_sa_t *child_sa,
67 bool up)
68 {
69 traffic_selector_t *my_ts, *other_ts;
70 enumerator_t *enumerator;
71 child_cfg_t *config;
72 host_t *vip, *me, *other;
73
74 config = child_sa->get_config(child_sa);
75 vip = ike_sa->get_virtual_ip(ike_sa, TRUE);
76 me = ike_sa->get_my_host(ike_sa);
77 other = ike_sa->get_other_host(ike_sa);
78
79 return TRUE;
80 }
81
82 METHOD(tnc_ifmap_listener_t, destroy, void,
83 private_tnc_ifmap_listener_t *this)
84 {
85 if (this->svc_client)
86 {
87 axis2_svc_client_free(this->svc_client, this->env);
88 }
89 if (this->env)
90 {
91 axutil_env_free(this->env);
92 }
93 free(this);
94 }
95
96 /**
97 * See header
98 */
99 tnc_ifmap_listener_t *tnc_ifmap_listener_create()
100 {
101 private_tnc_ifmap_listener_t *this;
102 axis2_char_t *server, *client_home, *username, *password, *auth_type;
103 axis2_endpoint_ref_t* endpoint_ref = NULL;
104 axis2_options_t *options = NULL;
105 axiom_node_t *request, *response, *node;
106 axiom_text_t *text;
107
108 client_home = lib->settings->get_str(lib->settings,
109 "charon.plugins.tnc-ifmap.client_home",
110 AXIS2_GETENV("AXIS2C_HOME"));
111 server = lib->settings->get_str(lib->settings,
112 "charon.plugins.tnc-ifmap.server", IFMAP_SERVER);
113 auth_type = lib->settings->get_str(lib->settings,
114 "charon.plugins.tnc-ifmap.auth_type", "Basic");
115 username = lib->settings->get_str(lib->settings,
116 "charon.plugins.tnc-ifmap.username", NULL);
117 password = lib->settings->get_str(lib->settings,
118 "charon.plugins.tnc-ifmap.password", NULL);
119
120 if (!username || !password)
121 {
122 DBG1(DBG_TNC, "IF-MAP client %s%s%s not defined",
123 (!username) ? "username" : "",
124 (!username && ! password) ? " and " : "",
125 (!password) ? "password" : "");
126 }
127
128 INIT(this,
129 .public = {
130 .listener = {
131 .child_updown = _child_updown,
132 },
133 .destroy = _destroy,
134 },
135 );
136
137 /* Create Axis2/C environment and options */
138 this->env = axutil_env_create_all(IFMAP_LOGFILE, AXIS2_LOG_LEVEL_TRACE);
139 options = axis2_options_create(this->env);
140
141 /* Define the IF-MAP server as the to endpoint reference */
142 endpoint_ref = axis2_endpoint_ref_create(this->env, server);
143 axis2_options_set_to(options, this->env, endpoint_ref);
144
145 /* Create the axis2 service client */
146 this->svc_client = axis2_svc_client_create(this->env, client_home);
147 if (!this->svc_client)
148 {
149 DBG1(DBG_TNC, "Error creating axis2 service client");
150 AXIS2_LOG_ERROR(this->env->log, AXIS2_LOG_SI,
151 "Stub invoke FAILED: Error code: %d :: %s",
152 this->env->error->error_number,
153 AXIS2_ERROR_GET_MESSAGE(this->env->error));
154 destroy(this);
155 return NULL;
156 }
157
158 axis2_svc_client_set_options(this->svc_client, this->env, options);
159 axis2_options_set_http_auth_info(options, this->env, username, password,
160 auth_type);
161
162 request = build_request(this);
163 response = axis2_svc_client_send_receive(this->svc_client, this->env, request);
164 if (!response)
165 {
166 DBG1(DBG_TNC, "Session setup with IF-MAP server failed");
167 destroy(this);
168 return NULL;
169 }
170 node = axiom_node_get_first_child(response, this->env);
171 if (node && axiom_node_get_node_type(node, this->env) == AXIOM_TEXT)
172 {
173 text = (axiom_text_t *)axiom_node_get_data_element(node, this->env);
174 if (text)
175 {
176 DBG1(DBG_TNC, "response = '%s'",
177 axiom_text_get_value(text, this->env));
178 }
179 }
180 axiom_node_free_tree(response, this->env);
181
182 return &this->public;
183 }
184