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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
|
/******************************************************************
* $Id: soap-server.c,v 1.35 2006/11/29 11:04:25 m0gg Exp $
*
* CSOAP Project: A SOAP client/server library in C
* Copyright (C) 2003 Ferhat Ayaz
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*
* Email: ayaz@jprogrammer.net
******************************************************************/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#ifdef HAVE_STDIO_H
#include <stdio.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
#include <libxml/tree.h>
#include <libxml/uri.h>
#include <nanohttp/nanohttp-error.h>
#include <nanohttp/nanohttp-logging.h>
#include "soap-fault.h"
#include "soap-env.h"
#include "soap-ctx.h"
#include "soap-service.h"
#include "soap-router.h"
#include "soap-addressing.h"
#include "soap-transport.h"
#ifdef HAVE_XMLSEC1
#include "soap-xmlsec.h"
#endif
#include "soap-server.h"
static SoapRouterNode *head = NULL;
static SoapRouterNode *tail = NULL;
static SoapRouterNode *
router_node_new(struct SoapRouter * router, const char *context, SoapRouterNode * next)
{
const char *noname = "/lost_found";
SoapRouterNode *node;
if (!(node = (SoapRouterNode *) malloc(sizeof(SoapRouterNode))))
{
log_error2("malloc failed (%s)", strerror(errno));
return NULL;
}
if (context)
{
node->context = strdup(context);
}
else
{
log_warn2("context is null, using '%s'", noname);
node->context = strdup(noname);
}
node->router = router;
node->next = next;
return node;
}
static herror_t
_soap_server_env_new_with_fault(const char *fault_string, const char *detail, struct SoapEnv **out)
{
return soap_env_new_with_fault(SOAP_FAULT_RECEIVER, fault_string, soap_server_get_name(), detail, out);
}
static void
_soap_server_fillup_header(struct SoapEnv *envelope)
{
xmlURI *uri;
log_verbose1(__FUNCTION__);
if (!(uri = soap_addressing_get_message_id(envelope)))
soap_addressing_set_message_id_string(envelope, NULL);
else
xmlFreeURI(uri);
if (!(uri = soap_addressing_get_from_address(envelope)))
soap_addressing_set_from_address_string(envelope, soap_server_get_name());
else
xmlFreeURI(uri);
return;
}
struct SoapRouter *
soap_server_find_router(const char *context)
{
SoapRouterNode *node;
for (node = head; node; node = node->next)
{
if (!strcmp(node->context, context))
return node->router;
}
return NULL;
}
/* XXX: rewrite this function */
herror_t
soap_server_process(struct SoapCtx *request, struct SoapCtx **response)
{
char buffer[1054];
const char *urn;
xmlURI *uri;
const char *method;
struct SoapRouter *router;
SoapService *service;
herror_t err;
log_verbose1("**** processing ****");
xmlDocDump(stdout, request->env->root->doc);
log_verbose1("********************");
*response = soap_ctx_new(NULL);
#ifdef HAVE_XMLSEC1
if ((err = soap_xmlsec_verify(request)) != H_OK)
{
log_error2("soap_xmlsec_verify failed (%s)", herror_message(err));
sprintf(buffer, "Verification of message signature failed (%s)", herror_message(err));
_soap_server_env_new_with_fault("Internal server error", buffer, &((*response)->env));
return H_OK;
}
if ((err = soap_xmlsec_decrypt(request)) != H_OK)
{
log_error2("soap_xmlsec_decrypt failed (%s)", herror_message(err));
sprintf(buffer, "Decryption of message body failed (%s)", herror_message(err));
_soap_server_env_new_with_fault("Internal server error", buffer, &((*response)->env));
return H_OK;
}
#endif
if ((method = soap_env_find_methodname(request->env)))
{
log_verbose2("method: \"%s\"", method);
if ((urn = soap_env_find_urn(request->env)))
{
log_verbose2("urn: \"%s\"", urn);
if ((uri = soap_addressing_get_to_address(request->env)))
{
log_verbose2("searching router for \"%s\"", uri->path);
if ((router = soap_server_find_router(uri->path)))
{
log_verbose2("router: %p", router);
if ((service = soap_router_find_service(router, urn, method)))
{
log_verbose3("service (%p) found, function (%p)", service, service->func);
if ((err = service->func(request, *response)) == H_OK)
{
if ((*response)->env == NULL)
{
sprintf(buffer, "Service for \"%s\" returned no envelope", urn);
_soap_server_env_new_with_fault("Internal service error", buffer, &((*response)->env));
}
}
else
{
sprintf(buffer, "Service returned following error message: \"%s\"", herror_message(err));
herror_release(err);
_soap_server_env_new_with_fault("Internal service error", buffer, &((*response)->env));
}
}
else
{
sprintf(buffer, "no service for URN \"%s\" found", urn);
_soap_server_env_new_with_fault(buffer, "The URN is not known by the server", &((*response)->env));
}
}
else
{
sprintf(buffer, "no router for context \"%s\" found", uri->path);
_soap_server_env_new_with_fault(buffer, "The method is unknown by the server", &((*response)->env));
}
xmlFreeURI(uri);
}
else
{
_soap_server_env_new_with_fault(buffer, "The destination address is missing", &((*response)->env));
}
}
else
{
_soap_server_env_new_with_fault("No method found", "The method is missing in the SOAP envelope", &((*response)->env));
}
}
else
{
_soap_server_env_new_with_fault("No URN found", "The URN is missing in the SOAP envelope", &((*response)->env));
}
_soap_server_fillup_header((*response)->env);
#ifdef HAVE_XMLSEC1
soap_xmlsec_encrypt(*response);
soap_xmlsec_sign(*response);
#endif
return H_OK;
}
herror_t
soap_server_init_args(int argc, char **argv)
{
herror_t status;
if ((status = soap_transport_server_init_args(argc, argv)) != H_OK)
{
log_error2("soap_transport_server_init_args failed (%s)", herror_message(status));
return status;
}
#ifdef HAVE_XMLSEC1
if ((status = soap_xmlsec_server_init_args(argc, argv)) != H_OK)
{
log_error2("soap_xmlsec_server_init_args failed (%s)", herror_message(status));
return status;
}
#endif
return H_OK;
}
const char *
soap_server_get_name(void)
{
return soap_transport_get_name();
}
herror_t
soap_server_register_router(struct SoapRouter *router, const char *context)
{
herror_t status;
if ((status = soap_transport_register(context)) != H_OK)
{
log_error2("soap_transport_register failed (%s)", herror_message(status));
return status;
}
if (tail == NULL)
{
head = tail = router_node_new(router, context, NULL);
}
else
{
tail->next = router_node_new(router, context, NULL);
tail = tail->next;
}
return H_OK;
}
SoapRouterNode *
soap_server_get_routers(void)
{
return head;
}
herror_t
soap_server_run(void)
{
herror_t status;
if ((status = soap_transport_server_run()) != H_OK)
return status;
return H_OK;
}
void
soap_server_destroy(void)
{
SoapRouterNode *node = head;
SoapRouterNode *tmp;
while (node != NULL)
{
tmp = node->next;
log_verbose2("soap_router_free(%p)", node->router);
soap_router_free(node->router);
free(node->context);
free(node);
node = tmp;
}
soap_transport_server_destroy();
return;
}
|