1 #define _XOPEN_SOURCE 500 /* strdup from string.h */
8 /* Private structure for write_body() call back */
15 /* Close connection to server and destroy CURL handle associated
17 _hidden isds_error
close_connection(struct isds_ctx
*context
) {
18 if (!context
) return IE_INVALID_CONTEXT
;
21 curl_easy_cleanup(context
->curl
);
23 isds_log(ILF_HTTP
, ILL_DEBUG
, _("Connection to server %s closed\n"),
27 return IE_CONNECTION_CLOSED
;
32 /* CURL call back function called when chunk of HTTP reponse body is available.
33 * @buffer points to new data
34 * @size * @nmemb is length of the chunk in bytes. Zero means empty body.
35 * @userp is private structure.
36 * Must reuturn the length of the chunk, otherwise CURL will signal
37 * CURL_WRITE_ERROR. */
38 static size_t write_body(void *buffer
, size_t size
, size_t nmemb
, void *userp
) {
39 struct soap_body
*body
= (struct soap_body
*) userp
;
42 /* FIXME: Check for (size * nmemb + body->lengt) !> SIZE_T_MAX.
43 * Precompute the product then. */
45 if (!body
) return 0; /* This should never happen */
46 if (0 == (size
* nmemb
)) return 0; /* Empty body */
48 new_data
= realloc(body
->data
, body
->length
+ size
* nmemb
);
49 if (!new_data
) return 0;
51 memcpy(new_data
+ body
->length
, buffer
, size
* nmemb
);
53 body
->data
= new_data
;
54 body
->length
+= size
* nmemb
;
56 return (size
* nmemb
);
60 /* CURL progress callback proxy to rearrange arguments.
61 * @curl_data is session context */
62 static int progress_proxy(void *curl_data
, double download_total
,
63 double download_current
, double upload_total
, double upload_current
) {
64 struct isds_ctx
*context
= (struct isds_ctx
*) curl_data
;
67 if (context
&& context
->progress_callback
) {
68 abort
= context
->progress_callback(
69 upload_total
, upload_current
,
70 download_total
, download_current
,
71 context
->progress_callback_data
);
73 isds_log(ILF_HTTP
, ILL_INFO
,
74 _("Application aborted HTTP transfer"));
83 * @context holds the base URL,
84 * @url is a (CGI) file of SOAP URL,
85 * @request is body for POST request
86 * @request_length is length of @request in bytes
87 * @reponse is automatically reallocated() buffer to fit HTTP response with
88 * @response_length (does not need to match allocatef memory exactly). You must
89 * free() the @response.
90 * @mime_type is automatically allocated MIME type send by server (*NULL if not
91 * sent). Set NULL if you don't care.
92 * @charset is charset of the body signaled by server. The same constrains
93 * like on @mime_type apply.
94 * @http_code is final HTTP code returned by server. This can be 200, 401, 500
95 * or any other one. Pass NULL if you don't interrest.
96 * In case of error, the response memory, MIME type, charset and lenght will be
97 * deallocated and zerod automatically. Thus be sure they are preallocated or
98 * they points to NULL.
99 * Be ware that successful return value does not mean the HTTP request has
100 * been accepted by the server. You must cosult @http_code. OTOH, failure
101 * return value means the request could not been sent (e.g. SSL error).
102 * Side effect: message buffer */
103 static isds_error
http(struct isds_ctx
*context
, const char *url
,
104 const void *request
, const size_t request_length
,
105 void **response
, size_t *response_length
,
106 char **mime_type
, char **charset
, long *http_code
) {
109 isds_error err
= IE_SUCCESS
;
110 struct soap_body body
;
112 struct curl_slist
*headers
= NULL
;
115 if (!context
) return IE_INVALID_CONTEXT
;
116 if (!url
) return IE_INVAL
;
117 if (request_length
> 0 && !request
) return IE_INVAL
;
118 if (!response
|| !response_length
) return IE_INVAL
;
120 /* Set the body here to allow deallocataion in leave block */
121 body
.data
= *response
;
124 /* Set Request-URI */
125 curl_err
= curl_easy_setopt(context
->curl
, CURLOPT_URL
, url
);
127 /* Set TLS options */
128 if (!curl_err
&& context
->tls_verify_server
) {
129 if (!*context
->tls_verify_server
)
130 isds_log(ILF_SEC
, ILL_WARNING
,
131 _("Disabling server identity verification. "
132 "That was your decision.\n"));
133 curl_err
= curl_easy_setopt(context
->curl
, CURLOPT_SSL_VERIFYPEER
,
134 (*context
->tls_verify_server
)? 1L : 0L);
136 curl_err
= curl_easy_setopt(context
->curl
, CURLOPT_SSL_VERIFYHOST
,
137 (*context
->tls_verify_server
)? 2L : 0L);
140 if (!curl_err
&& context
->tls_ca_file
) {
141 isds_log(ILF_SEC
, ILL_INFO
,
142 _("CA certificates will be searched in `%s' file since now\n"),
143 context
->tls_ca_file
);
144 curl_err
= curl_easy_setopt(context
->curl
, CURLOPT_CAINFO
,
145 context
->tls_ca_file
);
147 if (!curl_err
&& context
->tls_ca_dir
) {
148 isds_log(ILF_SEC
, ILL_INFO
,
149 _("CA certificates will be searched in `%s' directory "
150 "since now\n"), context
->tls_ca_file
);
151 curl_err
= curl_easy_setopt(context
->curl
, CURLOPT_CAINFO
,
152 context
->tls_ca_file
);
156 /* Set credentials */
157 #if HAVE_DECL_CURLOPT_USERNAME /* Since curl-7.19.1 */
158 if (!curl_err
&& context
->username
) {
159 curl_err
= curl_easy_setopt(context
->curl
, CURLOPT_USERNAME
,
162 if (!curl_err
&& context
->password
) {
163 curl_err
= curl_easy_setopt(context
->curl
, CURLOPT_PASSWORD
,
167 if (!curl_err
&& (context
->username
|| context
->password
)) {
168 char *userpwd
= astrcat3(context
->username
, ":", context
->password
);
170 isds_log_message(context
, _("Could not pass credentials to CURL"));
174 curl_err
= curl_easy_setopt(context
->curl
, CURLOPT_USERPWD
, userpwd
);
177 #endif /* not HAVE_DECL_CURLOPT_USERNAME */
181 curl_err
= curl_easy_setopt(context
->curl
, CURLOPT_NOSIGNAL
, 1);
183 if (!curl_err
&& context
->timeout
) {
184 #if HAVE_DECL_CURLOPT_TIMEOUT_MS /* Since curl-7.16.2 */
185 curl_err
= curl_easy_setopt(context
->curl
, CURLOPT_TIMEOUT_MS
,
188 curl_err
= curl_easy_setopt(context
->curl
, CURLOPT_TIMEOUT
,
189 context
->timeout
/ 1000);
190 #endif /* not HAVE_DECL_CURLOPT_TIMEOUT_MS */
193 /* Register callback */
194 if (context
->progress_callback
) {
196 curl_err
= curl_easy_setopt(context
->curl
, CURLOPT_NOPROGRESS
, 0);
199 curl_err
= curl_easy_setopt(context
->curl
,
200 CURLOPT_PROGRESSFUNCTION
, progress_proxy
);
203 curl_err
= curl_easy_setopt(context
->curl
, CURLOPT_PROGRESSDATA
,
208 /* Set other CURL features */
210 curl_err
= curl_easy_setopt(context
->curl
, CURLOPT_FAILONERROR
, 0);
213 curl_err
= curl_easy_setopt(context
->curl
, CURLOPT_FOLLOWLOCATION
, 1);
216 /* TODO: Make the redirect depth configurable */
217 curl_err
= curl_easy_setopt(context
->curl
, CURLOPT_MAXREDIRS
, 8);
220 curl_err
= curl_easy_setopt(context
->curl
,
221 CURLOPT_UNRESTRICTED_AUTH
, 1);
224 curl_err
= curl_easy_setopt(context
->curl
, CURLOPT_COOKIEFILE
, "");
227 /* Set get-response function */
229 curl_err
= curl_easy_setopt(context
->curl
, CURLOPT_WRITEFUNCTION
,
233 curl_err
= curl_easy_setopt(context
->curl
, CURLOPT_WRITEDATA
, &body
);
236 /* Set MIME types and headers requires by SOAP 1.1.
237 * SOAP 1.1 requires text/xml, SOAP 1.2 requires application/soap+xml */
239 headers
= curl_slist_append(headers
,
240 "Accept: application/soap+xml,application/xml,text/xml");
245 headers
= curl_slist_append(headers
, "Content-Type: text/xml");
250 headers
= curl_slist_append(headers
, "SOAPAction: ");
255 curl_err
= curl_easy_setopt(context
->curl
, CURLOPT_HTTPHEADER
, headers
);
258 /* Set user agent identification */
259 /* TODO: Present library version, curl etc. in User-Agent */
260 curl_err
= curl_easy_setopt(context
->curl
, CURLOPT_USERAGENT
, "libisds");
263 /* Set POST request body */
265 curl_err
= curl_easy_setopt(context
->curl
, CURLOPT_POST
, 1);
268 curl_err
= curl_easy_setopt(context
->curl
, CURLOPT_POSTFIELDS
, request
);
271 curl_err
= curl_easy_setopt(context
->curl
, CURLOPT_POSTFIELDSIZE
,
275 /* Check for errors so far */
277 isds_log_message(context
, curl_easy_strerror(curl_err
));
282 isds_log(ILF_HTTP
, ILL_DEBUG
, _("Sending POST request to %s\n"), url
);
283 isds_log(ILF_HTTP
, ILL_DEBUG
,
284 _("POST body length: %zu, content follows:\n"), request_length
);
285 isds_log(ILF_HTTP
, ILL_DEBUG
, "%.*s\n", request_length
, request
);
286 isds_log(ILF_HTTP
, ILL_DEBUG
, _("End of POST body\n"));
287 if ((log_facilities
& ILF_HTTP
) && (log_level
>= ILL_DEBUG
) ) {
288 curl_easy_setopt(context
->curl
, CURLOPT_VERBOSE
, 1);
293 curl_err
= curl_easy_perform(context
->curl
);
295 /* Wipe credentials out of the handler */
296 #if HAVE_DECL_CURLOPT_USERNAME /* Since curl-7.19.1 */
297 if (context
->username
) {
298 curl_easy_setopt(context
->curl
, CURLOPT_USERNAME
, NULL
);
300 if (context
->password
) {
301 curl_easy_setopt(context
->curl
, CURLOPT_PASSWORD
, NULL
);
304 if (context
->username
|| context
->password
) {
305 curl_easy_setopt(context
->curl
, CURLOPT_USERPWD
, NULL
);
307 #endif /* not HAVE_DECL_CURLOPT_USERNAME */
310 curl_err
= curl_easy_getinfo(context
->curl
, CURLINFO_CONTENT_TYPE
,
314 /* TODO: CURL is not internationalized yet. Collect CURL messages for
316 isds_printf_message(context
,
317 _("%s: %s"), url
, _(curl_easy_strerror(curl_err
)));
322 isds_log(ILF_HTTP
, ILL_DEBUG
, _("Final response to %s received\n"), url
);
323 isds_log(ILF_HTTP
, ILL_DEBUG
,
324 _("Response body length: %zu, content follows:\n"),
326 isds_log(ILF_HTTP
, ILL_DEBUG
, "%.*s\n", body
.length
, body
.data
);
327 isds_log(ILF_HTTP
, ILL_DEBUG
, _("End of response body\n"));
330 /* Extract MIME type and charset */
335 sep
= strchr(content_type
, ';');
336 if (sep
) offset
= (size_t) (sep
- content_type
);
337 else offset
= strlen(content_type
);
340 *mime_type
= malloc(offset
+ 1);
345 memcpy(*mime_type
, content_type
, offset
);
346 (*mime_type
)[offset
] = '\0';
353 sep
= strstr(sep
, "charset=");
357 *charset
= strdup(sep
+ 8);
367 /* Get HTTP response code */
369 curl_err
= curl_easy_getinfo(context
->curl
,
370 CURLINFO_RESPONSE_CODE
, http_code
);
378 curl_slist_free_all(headers
);
394 close_connection(context
);
397 *response
= body
.data
;
398 *response_length
= body
.length
;
405 * @context holds the base URL,
406 * @file is a (CGI) file of SOAP URL,
407 * @request is XML node set with SOAP request body.
408 * @file must be NULL, @request should be NULL rather than empty, if they should
409 * not be signaled in the SOAP request.
410 * @reponse is automatically allocated() node set with SOAP response body.
411 * You must xmlFreeNodeList() it. This is literal body, empty (NULL), one node
412 * or more nodes can be returned.
413 * @raw_response is automatically allocated bitstream with response body. Use
414 * NULL if you don't care
415 * @raw_response_length is size of @raw_response in bytes
416 * In case of error the response will be deallocated automatically.
417 * Side effect: message buffer */
418 _hidden isds_error
soap(struct isds_ctx
*context
, const char *file
,
419 const xmlNodePtr request
, xmlNodePtr
*response
,
420 void **raw_response
, size_t *raw_response_length
) {
422 isds_error err
= IE_SUCCESS
;
424 char *mime_type
= NULL
;
426 xmlBufferPtr http_request
= NULL
;
427 xmlSaveCtxtPtr save_ctx
= NULL
;
428 xmlDocPtr request_soap_doc
= NULL
;
429 xmlNodePtr request_soap_envelope
= NULL
, request_soap_body
= NULL
;
430 xmlNsPtr soap_ns
= NULL
;
431 void *http_response
= NULL
;
432 size_t response_length
= 0;
433 xmlDocPtr response_soap_doc
= NULL
;
434 xmlNodePtr response_root
= NULL
;
435 xmlXPathContextPtr xpath_ctx
= NULL
;
436 xmlXPathObjectPtr response_soap_headers
= NULL
, response_soap_body
= NULL
,
437 response_soap_fault
= NULL
;
440 if (!context
) return IE_INVALID_CONTEXT
;
441 if (!response
) return IE_INVAL
;
442 if (!raw_response_length
&& raw_response
) return IE_INVAL
;
444 xmlFreeNodeList(*response
);
446 if (raw_response
) *raw_response
= NULL
;
448 url
= astrcat(context
->url
, file
);
449 if (!url
) return IE_NOMEM
;
451 /* Build SOAP request envelope */
452 request_soap_doc
= xmlNewDoc(BAD_CAST
"1.0");
453 if (!request_soap_doc
) {
454 isds_log_message(context
, _("Could not build SOAP request document"));
458 request_soap_envelope
= xmlNewNode(NULL
, BAD_CAST
"Envelope");
459 if (!request_soap_envelope
) {
460 isds_log_message(context
, _("Could not build SOAP request envelope"));
464 xmlDocSetRootElement(request_soap_doc
, request_soap_envelope
);
465 /* Only this way we get namespace definition as @xmlns:soap,
466 * otherwise we get namespace prefix without definition */
467 soap_ns
= xmlNewNs(request_soap_envelope
, BAD_CAST SOAP_NS
, NULL
);
469 isds_log_message(context
, _("Could not create SOAP name space"));
473 xmlSetNs(request_soap_envelope
, soap_ns
);
474 request_soap_body
= xmlNewChild(request_soap_envelope
, NULL
,
475 BAD_CAST
"Body", NULL
);
476 if (!request_soap_body
) {
477 isds_log_message(context
,
478 _("Could not add Body to SOAP request envelope"));
483 /* Append request XML node set to SOAP body if request is not empty */
484 /* XXX: Copy of request must be used, otherwise xmlFreeDoc(request_soap_doc)
485 * would destroy this outer structure. */
487 xmlNodePtr request_copy
= xmlCopyNodeList(request
);
489 isds_log_message(context
,
490 _("Could not copy request content"));
494 if (!xmlAddChildList(request_soap_body
, request_copy
)) {
495 xmlFreeNodeList(request_copy
);
496 isds_log_message(context
,
497 _("Could not add request content to SOAP "
498 "request envelope"));
505 /* Serialize the SOAP request into HTTP request body */
506 http_request
= xmlBufferCreate();
508 isds_log_message(context
,
509 _("Could not create xmlBuffer for HTTP request body"));
513 /* Last argument 1 means format the XML tree. This is pretty but it breaks
514 * digital signatures probably because ISDS abandoned XMLDSig */
515 save_ctx
= xmlSaveToBuffer(http_request
, "UTF-8", 1);
517 isds_log_message(context
,
518 _("Could not create XML serializer"));
522 /* XXX: According LibXML documentation, this function does not return
523 * meaningfull value yet */
524 xmlSaveDoc(save_ctx
, request_soap_doc
);
525 if (-1 == xmlSaveFlush(save_ctx
)) {
526 isds_log_message(context
,
527 _("Could not serialize SOAP request to HTTP request bddy"));
532 isds_log(ILF_SOAP
, ILL_DEBUG
,
533 _("SOAP request to sent to %s:\n%.*s\nEnd of SOAP request\n"),
534 url
, http_request
->use
, http_request
->content
);
536 err
= http(context
, url
, http_request
->content
, http_request
->use
,
537 &http_response
, &response_length
,
538 &mime_type
, NULL
, &http_code
);
540 /* TODO: HTTP binding for SOAP prescribes non-200 HTTP return codes
541 * to be processes too. */
547 /* Check for HTTP return code */
548 isds_log(ILF_SOAP
, ILL_DEBUG
, _("Server returned %ld HTTP code\n"),
551 /* XXX: We must see which code is used for not permitted ISDS
552 * operation like downloading message without proper user
553 * permissions. In that cat we should keep connection opened. */
555 err
= IE_NOT_LOGGED_IN
;
556 isds_log_message(context
, _("Authentication failed"));
561 isds_log_message(context
,
562 _("Code 404: Document not found on server"));
565 /* 500 should return standard SOAP message */
568 /* Check for Content-Type: text/xml.
569 * Do it after HTTP code check because 401 Unauthorized returns HTML web
570 * page for browsers. */
571 if (mime_type
&& strcmp(mime_type
, "text/xml")
572 && strcmp(mime_type
, "application/soap+xml")
573 && strcmp(mime_type
, "application/xml")) {
574 char *mime_type_locale
= utf82locale(mime_type
);
575 isds_printf_message(context
,
576 _("%s: bad MIME type sent by server: %s"), url
,
578 free(mime_type_locale
);
583 /* TODO: Convert returned body into XML default encoding */
585 /* Parse the HTTP body as XML */
586 response_soap_doc
= xmlParseMemory(http_response
, response_length
);
587 if (!response_soap_doc
) {
592 xpath_ctx
= xmlXPathNewContext(response_soap_doc
);
598 if (register_namespaces(xpath_ctx
, MESSAGE_NS_UNSIGNED
)) {
603 isds_log(ILF_SOAP
, ILL_DEBUG
,
604 _("SOAP response received:\n%.*s\nEnd of SOAP response\n"),
605 response_length
, http_response
);
608 /* Check for SOAP version */
609 response_root
= xmlDocGetRootElement(response_soap_doc
);
610 if (!response_root
) {
611 isds_log_message(context
, "SOAP response has no root element");
615 if (xmlStrcmp(response_root
->name
, BAD_CAST
"Envelope") ||
616 xmlStrcmp(response_root
->ns
->href
, BAD_CAST SOAP_NS
)) {
617 isds_log_message(context
, "SOAP response is not SOAP 1.1 document");
622 /* Check for SOAP Headers */
623 response_soap_headers
= xmlXPathEvalExpression(
624 BAD_CAST
"/soap:Envelope/soap:Header/"
625 "*[@soap:mustUnderstand/text() = true()]", xpath_ctx
);
626 if (!response_soap_headers
) {
630 if (!xmlXPathNodeSetIsEmpty(response_soap_headers
->nodesetval
)) {
631 isds_log_message(context
,
632 _("SOAP response requires unsupported feature"));
633 /* TODO: log the headers
634 * xmlChar *fragment = NULL;
635 * fragment = xmlXPathCastNodeSetToSting(response_soap_headers->nodesetval);*/
641 response_soap_body
= xmlXPathEvalExpression(
642 BAD_CAST
"/soap:Envelope/soap:Body", xpath_ctx
);
643 if (!response_soap_body
) {
647 if (xmlXPathNodeSetIsEmpty(response_soap_body
->nodesetval
)) {
648 isds_log_message(context
,
649 _("SOAP response does not contain SOAP Body element"));
653 if (response_soap_body
->nodesetval
->nodeNr
> 1) {
654 isds_log_message(context
,
655 _("SOAP response has more than one Body element"));
660 /* Check for SOAP Fault */
661 response_soap_fault
= xmlXPathEvalExpression(
662 BAD_CAST
"/soap:Envelope/soap:Body/soap:Fault", xpath_ctx
);
663 if (!response_soap_fault
) {
667 if (!xmlXPathNodeSetIsEmpty(response_soap_fault
->nodesetval
)) {
668 /* TODO: log the faultcode and faultstring */
669 isds_log_message(context
, _("SOAP response signals Fault"));
675 /* Extract XML Tree with ISDS response from SOAP envelope and return it.
676 * XXX: response_soap_body is Body, we need children which may not exist
677 * (i.e. empty Body). */
678 /* TODO: Destroy SOAP response but Body childern. This is more memory
679 * friendly than copying (potentialy) fat body */
680 if (response_soap_body
->nodesetval
->nodeTab
[0]->children
) {
681 *response
= xmlDocCopyNodeList(response_soap_doc
,
682 response_soap_body
->nodesetval
->nodeTab
[0]->children
);
687 } else *response
= NULL
;
689 /* Save raw response */
691 *raw_response
= http_response
;
692 *raw_response_length
= response_length
;
693 http_response
= NULL
;
699 xmlFreeNodeList(*response
);
703 xmlXPathFreeObject(response_soap_fault
);
704 xmlXPathFreeObject(response_soap_body
);
705 xmlXPathFreeObject(response_soap_headers
);
706 xmlXPathFreeContext(xpath_ctx
);
707 xmlFreeDoc(response_soap_doc
);
710 xmlSaveClose(save_ctx
);
711 xmlBufferFree(http_request
);
712 xmlFreeDoc(request_soap_doc
); /* recursive, frees request_body, soap_ns*/
721 * void xmlInitParser(void)
722 * Initialization function for the XML parser. This is not reentrant. Call
723 * once before processing in case of use in multithreaded programs.
725 * int xmlInitParserCtxt(xmlParserCtxtPtr ctxt)
726 * Initialize a parser context
728 * xmlDocPtr xmlCtxtReadDoc(xmlParserCtxtPtr ctxt, const xmlChar * cur,
729 * const * char URL, const char * encoding, int options);
730 * Parse in-memory NULL-terminated document @cur.
732 * xmlDocPtr xmlParseMemory(const char * buffer, int size)
733 * Parse an XML in-memory block and build a tree.
735 * xmlParserCtxtPtr xmlCreateMemoryParserCtxt(const char * buffer, int
737 * Create a parser context for an XML in-memory document.
739 * xmlParserCtxtPtr xmlCreateDocParserCtxt(const xmlChar * cur)
740 * Creates a parser context for an XML in-memory document.
742 * xmlDocPtr xmlCtxtReadMemory(xmlParserCtxtPtr ctxt,
743 * const char * buffer, int size, const char * URL, const char * encoding,
745 * Parse an XML in-memory document and build a tree. This reuses the existing
746 * @ctxt parser context.
748 * void xmlCleanupParser(void)
749 * Cleanup function for the XML library. It tries to reclaim all parsing
750 * related glob document related memory. Calling this function should not
751 * prevent reusing the libr finished using the library or XML document built
754 * void xmlClearParserCtxt(xmlParserCtxtPtr ctxt)
755 * Clear (release owned resources) and reinitialize a parser context.
757 * void xmlCtxtReset(xmlParserCtxtPtr ctxt)
758 * Reset a parser context
760 * void xmlFreeParserCtxt(xmlParserCtxtPtr ctxt)
761 * Free all the memory used by a parser context. However the parsed document
762 * in ctxt->myDoc is not freed.
764 * void xmlFreeDoc(xmlDocPtr cur)
765 * Free up all the structures used by a document, tree included.