1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
|
/*
* Copyright (C) 2013 Andreas Steffen
* HSR Hochschule fuer Technik Rapperswil
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*/
#include "tnc_ifmap_soap_msg.h"
#include "tnc_ifmap_http.h"
#include <utils/debug.h>
#define SOAP_NS "http://www.w3.org/2003/05/soap-envelope"
typedef struct private_tnc_ifmap_soap_msg_t private_tnc_ifmap_soap_msg_t;
/**
* Private data of an tnc_ifmap_soap_msg_t object.
*/
struct private_tnc_ifmap_soap_msg_t {
/**
* Public tnc_ifmap_soap_msg_t interface.
*/
tnc_ifmap_soap_msg_t public;
/**
* HTTP POST request builder and response processing
*/
tnc_ifmap_http_t *http;
/**
* TLS socket
*/
tls_socket_t *tls;
/**
* XML Document
*/
xmlDocPtr doc;
};
/**
* Find a child node with a given name
*/
static xmlNodePtr find_child(xmlNodePtr parent, const xmlChar* name)
{
xmlNodePtr child;
child = parent->xmlChildrenNode;
while (child)
{
if (xmlStrcmp(child->name, name) == 0)
{
return child;
}
child = child->next;
}
DBG1(DBG_TNC, "child node \"%s\" not found", name);
return NULL;
}
METHOD(tnc_ifmap_soap_msg_t, post, bool,
private_tnc_ifmap_soap_msg_t *this, xmlNodePtr request, char *result_name,
xmlNodePtr *result)
{
xmlDocPtr doc;
xmlNodePtr env, body, cur, response;
xmlNsPtr ns;
xmlChar *xml_str, *errorCode, *errorString;
int xml_len, len, written;
chunk_t xml, http;
char buf[4096] = { 0 };
status_t status;
DBG2(DBG_TNC, "sending ifmap %s", request->name);
/* Generate XML Document containing SOAP Envelope */
doc = xmlNewDoc("1.0");
env =xmlNewNode(NULL, "Envelope");
ns = xmlNewNs(env, SOAP_NS, "env");
xmlSetNs(env, ns);
xmlDocSetRootElement(doc, env);
/* Add SOAP Body containing IF-MAP request */
body = xmlNewNode(ns, "Body");
xmlAddChild(body, request);
xmlAddChild(env, body);
/* Convert XML Document into a character string */
xmlDocDumpFormatMemory(doc, &xml_str, &xml_len, 1);
xmlFreeDoc(doc);
DBG3(DBG_TNC, "%.*s", xml_len, xml_str);
xml = chunk_create(xml_str, xml_len);
/* Send SOAP-XML request via HTTPS POST */
do
{
status = this->http->build(this->http, &xml, &http);
if (status == FAILED)
{
break;
}
written = this->tls->write(this->tls, http.ptr, http.len);
free(http.ptr);
if (written != http.len)
{
status = FAILED;
break;
}
}
while (status == NEED_MORE);
xmlFree(xml_str);
if (status != SUCCESS)
{
return FALSE;
}
/* Receive SOAP-XML response via [chunked] HTTPS */
xml = chunk_empty;
do
{
/* reduce size so the buffer is null-terminated */
len = this->tls->read(this->tls, buf, sizeof(buf)-1, TRUE);
if (len <= 0)
{
return FALSE;
}
http = chunk_create(buf, len);
status = this->http->process(this->http, &http, &xml);
if (status == FAILED)
{
free(xml.ptr);
return FALSE;
}
}
while (status == NEED_MORE);
DBG3(DBG_TNC, "parsing XML message %B", &xml);
this->doc = xmlParseMemory(xml.ptr, xml.len);
free(xml.ptr);
if (!this->doc)
{
DBG1(DBG_TNC, "failed to parse XML message");
return FALSE;
}
/* check out XML document */
cur = xmlDocGetRootElement(this->doc);
if (!cur)
{
DBG1(DBG_TNC, "empty XML message");
return FALSE;
}
/* get XML Document type is a SOAP Envelope */
if (xmlStrcmp(cur->name, "Envelope"))
{
DBG1(DBG_TNC, "XML message does not contain a SOAP Envelope");
return FALSE;
}
/* get SOAP Body */
cur = find_child(cur, "Body");
if (!cur)
{
return FALSE;
}
/* get IF-MAP response */
response = find_child(cur, "response");
if (!response)
{
return FALSE;
}
/* get IF-MAP result */
cur = find_child(response, result_name);
if (!cur)
{
cur = find_child(response, "errorResult");
if (cur)
{
DBG1(DBG_TNC, "received errorResult");
errorCode = xmlGetProp(cur, "errorCode");
if (errorCode)
{
DBG1(DBG_TNC, " %s", errorCode);
xmlFree(errorCode);
}
cur = find_child(cur, "errorString");
if (cur)
{
errorString = xmlNodeGetContent(cur);
if (errorString)
{
DBG1(DBG_TNC, " %s", errorString);
xmlFree(errorString);
}
}
}
return FALSE;
}
if (result)
{
*result = cur;
}
return TRUE;
}
METHOD(tnc_ifmap_soap_msg_t, destroy, void,
private_tnc_ifmap_soap_msg_t *this)
{
this->http->destroy(this->http);
if (this->doc)
{
xmlFreeDoc(this->doc);
}
free(this);
}
/**
* See header
*/
tnc_ifmap_soap_msg_t *tnc_ifmap_soap_msg_create(char *uri, chunk_t user_pass,
tls_socket_t *tls)
{
private_tnc_ifmap_soap_msg_t *this;
INIT(this,
.public = {
.post = _post,
.destroy = _destroy,
},
.http = tnc_ifmap_http_create(uri, user_pass),
.tls = tls,
);
return &this->public;
}
|