8b2aad31b845122fd115ae30ec9c53e134aeeed4
[shibboleth/cpp-xmltooling.git] / xmltooling / soap / impl / CURLSOAPTransport.cpp
1 /*
2  *  Copyright 2001-2006 Internet2
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /**
18  * CURLSOAPTransport.cpp
19  * 
20  * libcurl-based SOAPTransport implementation
21  */
22
23 #include "internal.h"
24 #include "exceptions.h"
25 #include "security/OpenSSLTrustEngine.h"
26 #include "signature/OpenSSLCredentialResolver.h"
27 #include "soap/HTTPSOAPTransport.h"
28 #include "soap/OpenSSLSOAPTransport.h"
29 #include "util/NDC.h"
30 #include "util/Threads.h"
31
32 #include <list>
33 #include <curl/curl.h>
34 #include <log4cpp/Category.hh>
35 #include <openssl/x509_vfy.h>
36
37 using namespace xmlsignature;
38 using namespace xmltooling;
39 using namespace log4cpp;
40 using namespace std;
41
42 namespace xmltooling {
43
44     // Manages cache of socket connections via CURL handles.
45     class XMLTOOL_DLLLOCAL CURLPool
46     {
47     public:
48         CURLPool() : m_size(256), m_lock(Mutex::create()),
49             m_log(Category::getInstance(XMLTOOLING_LOGCAT".SOAPTransport.CURLPool")) {}
50         ~CURLPool();
51         
52         CURL* get(const string& to, const char* endpoint);
53         void put(const string& to, const char* endpoint, CURL* handle);
54     
55     private:    
56         typedef map<string,vector<CURL*> > poolmap_t;
57         poolmap_t m_bindingMap;
58         list< vector<CURL*>* > m_pools;
59         long m_size;
60         Mutex* m_lock;
61         Category& m_log;
62     };
63     
64     static XMLTOOL_DLLLOCAL CURLPool* g_CURLPool = NULL;
65     
66     class XMLTOOL_DLLLOCAL CURLSOAPTransport : public HTTPSOAPTransport, public OpenSSLSOAPTransport
67     {
68     public:
69         CURLSOAPTransport(const KeyInfoSource& peer, const char* endpoint)
70                 : m_peer(peer), m_endpoint(endpoint), m_handle(NULL), m_headers(NULL),
71                     m_credResolver(NULL), m_trustEngine(NULL), m_keyResolver(NULL),
72                     m_ssl_callback(NULL), m_ssl_userptr(NULL) {
73             m_handle = g_CURLPool->get(peer.getName(), endpoint);
74             curl_easy_setopt(m_handle,CURLOPT_URL,endpoint);
75             curl_easy_setopt(m_handle,CURLOPT_CONNECTTIMEOUT,15);
76             curl_easy_setopt(m_handle,CURLOPT_TIMEOUT,30);
77             curl_easy_setopt(m_handle,CURLOPT_HTTPAUTH,0);
78             curl_easy_setopt(m_handle,CURLOPT_USERPWD,NULL);
79             curl_easy_setopt(m_handle,CURLOPT_HEADERDATA,this);
80         }
81         
82         virtual ~CURLSOAPTransport() {
83             curl_slist_free_all(m_headers);
84             curl_easy_setopt(m_handle,CURLOPT_ERRORBUFFER,NULL);
85             g_CURLPool->put(m_peer.getName(), m_endpoint.c_str(), m_handle);
86         }
87
88         bool setConnectTimeout(long timeout) const {
89             return (curl_easy_setopt(m_handle,CURLOPT_CONNECTTIMEOUT,timeout)==CURLE_OK);
90         }
91         
92         bool setTimeout(long timeout) const {
93             return (curl_easy_setopt(m_handle,CURLOPT_TIMEOUT,timeout)==CURLE_OK);
94         }
95         
96         bool setAuth(transport_auth_t authType, const char* username=NULL, const char* password=NULL) const;
97         
98         bool setCredentialResolver(const CredentialResolver* credResolver) const {
99             const OpenSSLCredentialResolver* down = dynamic_cast<const OpenSSLCredentialResolver*>(credResolver);
100             if (!down) {
101                 m_credResolver = NULL;
102                 return (credResolver==NULL);
103             }
104             m_credResolver = down;
105             return true;
106         }
107         
108         bool setTrustEngine(const X509TrustEngine* trustEngine, const KeyResolver* keyResolver=NULL) const {
109             const OpenSSLTrustEngine* down = dynamic_cast<const OpenSSLTrustEngine*>(trustEngine);
110             if (!down) {
111                 m_trustEngine = NULL;
112                 m_keyResolver = NULL;
113                 return (trustEngine==NULL);
114             }
115             m_trustEngine = down;
116             m_keyResolver = keyResolver;
117             return true;
118         }
119         
120         size_t send(istream& in, ostream& out);
121         
122         string getContentType() const;
123         
124         bool setRequestHeader(const char* name, const char* val) const {
125             string temp(name);
126             temp=temp + ": " + val;
127             m_headers=curl_slist_append(m_headers,temp.c_str());
128             return true;
129         }
130         
131         const vector<string>& getResponseHeader(const char* val) const;
132         
133         bool setSSLCallback(ssl_ctx_callback_fn fn, void* userptr=NULL) const {
134             m_ssl_callback=fn;
135             m_ssl_userptr=userptr;
136             return true;
137         }
138
139     private:        
140         // per-call state
141         const KeyInfoSource& m_peer;
142         string m_endpoint;
143         CURL* m_handle;
144         mutable struct curl_slist* m_headers;
145         map<string,vector<string> > m_response_headers;
146         mutable const OpenSSLCredentialResolver* m_credResolver;
147         mutable const OpenSSLTrustEngine* m_trustEngine;
148         mutable const KeyResolver* m_keyResolver;
149         mutable ssl_ctx_callback_fn m_ssl_callback;
150         mutable void* m_ssl_userptr;
151         
152         friend size_t XMLTOOL_DLLLOCAL curl_header_hook(void* ptr, size_t size, size_t nmemb, void* stream);
153         friend CURLcode XMLTOOL_DLLLOCAL xml_ssl_ctx_callback(CURL* curl, SSL_CTX* ssl_ctx, void* userptr);
154         friend int XMLTOOL_DLLLOCAL verify_callback(X509_STORE_CTX* x509_ctx, void* arg);
155     };
156
157     // libcurl callback functions
158     size_t XMLTOOL_DLLLOCAL curl_header_hook(void* ptr, size_t size, size_t nmemb, void* stream);
159     size_t XMLTOOL_DLLLOCAL curl_write_hook(void* ptr, size_t size, size_t nmemb, void* stream);
160     size_t XMLTOOL_DLLLOCAL curl_read_hook( void *ptr, size_t size, size_t nmemb, void *stream);
161     int XMLTOOL_DLLLOCAL curl_debug_hook(CURL* handle, curl_infotype type, char* data, size_t len, void* ptr);
162     CURLcode XMLTOOL_DLLLOCAL xml_ssl_ctx_callback(CURL* curl, SSL_CTX* ssl_ctx, void* userptr);
163     int XMLTOOL_DLLLOCAL verify_callback(X509_STORE_CTX* x509_ctx, void* arg);
164
165     SOAPTransport* CURLSOAPTransportFactory(const pair<const KeyInfoSource*,const char*>& dest)
166     {
167         return new CURLSOAPTransport(*dest.first, dest.second);
168     }
169 };
170
171 void xmltooling::registerSOAPTransports()
172 {
173     XMLToolingConfig& conf=XMLToolingConfig::getConfig();
174     conf.SOAPTransportManager.registerFactory("http", CURLSOAPTransportFactory);
175     conf.SOAPTransportManager.registerFactory("https", CURLSOAPTransportFactory);
176 }
177
178 void xmltooling::initSOAPTransports()
179 {
180     g_CURLPool=new CURLPool();
181 }
182
183 void xmltooling::termSOAPTransports()
184 {
185     delete g_CURLPool;
186     g_CURLPool = NULL;
187 }
188
189 CURLPool::~CURLPool()
190 {
191     for (poolmap_t::iterator i=m_bindingMap.begin(); i!=m_bindingMap.end(); i++) {
192         for (vector<CURL*>::iterator j=i->second.begin(); j!=i->second.end(); j++)
193             curl_easy_cleanup(*j);
194     }
195     delete m_lock;
196 }
197
198 CURL* CURLPool::get(const string& to, const char* endpoint)
199 {
200 #ifdef _DEBUG
201     xmltooling::NDC("get");
202 #endif
203     m_log.debug("getting connection handle to %s", endpoint);
204     m_lock->lock();
205     poolmap_t::iterator i=m_bindingMap.find(to + "|" + endpoint);
206     
207     if (i!=m_bindingMap.end()) {
208         // Move this pool to the front of the list.
209         m_pools.remove(&(i->second));
210         m_pools.push_front(&(i->second));
211         
212         // If a free connection exists, return it.
213         if (!(i->second.empty())) {
214             CURL* handle=i->second.back();
215             i->second.pop_back();
216             m_size--;
217             m_lock->unlock();
218             m_log.debug("returning existing connection handle from pool");
219             return handle;
220         }
221     }
222     
223     m_lock->unlock();
224     m_log.debug("nothing free in pool, returning new connection handle");
225     
226     // Create a new connection and set non-varying options.
227     CURL* handle=curl_easy_init();
228     if (!handle)
229         return NULL;
230     curl_easy_setopt(handle,CURLOPT_NOPROGRESS,1);
231     curl_easy_setopt(handle,CURLOPT_NOSIGNAL,1);
232     curl_easy_setopt(handle,CURLOPT_FAILONERROR,1);
233     curl_easy_setopt(handle,CURLOPT_SSLVERSION,3);
234     curl_easy_setopt(handle,CURLOPT_SSL_VERIFYHOST,2);
235     curl_easy_setopt(handle,CURLOPT_HEADERFUNCTION,&curl_header_hook);
236     curl_easy_setopt(handle,CURLOPT_READFUNCTION,&curl_read_hook);
237     curl_easy_setopt(handle,CURLOPT_WRITEFUNCTION,&curl_write_hook);
238     curl_easy_setopt(handle,CURLOPT_DEBUGFUNCTION,&curl_debug_hook);
239
240     return handle;
241 }
242
243 void CURLPool::put(const string& to, const char* endpoint, CURL* handle)
244 {
245     string key = to + "|" + endpoint;
246     m_lock->lock();
247     poolmap_t::iterator i=m_bindingMap.find(key);
248     if (i==m_bindingMap.end())
249         m_pools.push_front(&(m_bindingMap.insert(poolmap_t::value_type(key,vector<CURL*>(1,handle))).first->second));
250     else
251         i->second.push_back(handle);
252     
253     CURL* killit=NULL;
254     if (++m_size > 256) {
255         // Kick a handle out from the back of the bus.
256         while (true) {
257             vector<CURL*>* corpse=m_pools.back();
258             if (!corpse->empty()) {
259                 killit=corpse->back();
260                 corpse->pop_back();
261                 m_size--;
262                 break;
263             }
264             
265             // Move an empty pool up to the front so we don't keep hitting it.
266             m_pools.pop_back();
267             m_pools.push_front(corpse);
268         }
269     }
270     m_lock->unlock();
271     if (killit) {
272         curl_easy_cleanup(killit);
273 #ifdef _DEBUG
274         xmltooling::NDC("put");
275 #endif
276         m_log.info("conn_pool_max limit reached, dropping an old connection");
277     }
278 }
279
280 bool CURLSOAPTransport::setAuth(transport_auth_t authType, const char* username, const char* password) const
281 {
282     if (authType==transport_auth_none) {
283         if (curl_easy_setopt(m_handle,CURLOPT_HTTPAUTH,0)!=CURLE_OK)
284             return false;
285         return (curl_easy_setopt(m_handle,CURLOPT_USERPWD,NULL)==CURLE_OK);
286     }
287     long flag=0;
288     switch (authType) {
289         case transport_auth_basic:    flag = CURLAUTH_BASIC; break;
290         case transport_auth_digest:   flag = CURLAUTH_DIGEST; break;
291         case transport_auth_ntlm:     flag = CURLAUTH_NTLM; break;
292         case transport_auth_gss:      flag = CURLAUTH_GSSNEGOTIATE; break;
293         default:            return false;
294     }
295     if (curl_easy_setopt(m_handle,CURLOPT_HTTPAUTH,flag)!=CURLE_OK)
296         return false;
297     string creds = string(username ? username : "") + ':' + (password ? password : "");
298     return (curl_easy_setopt(m_handle,CURLOPT_USERPWD,creds.c_str())==CURLE_OK);
299 }
300
301 const vector<string>& CURLSOAPTransport::getResponseHeader(const char* name) const
302 {
303     static vector<string> emptyVector;
304
305     map<string,vector<string> >::const_iterator i=m_response_headers.find(name);
306     if (i!=m_response_headers.end())
307         return i->second;
308     
309     for (map<string,vector<string> >::const_iterator j=m_response_headers.begin(); j!=m_response_headers.end(); j++) {
310 #ifdef HAVE_STRCASECMP
311         if (!strcasecmp(j->first.c_str(), name))
312 #else
313         if (!stricmp(j->first.c_str(), name))
314 #endif
315             return j->second;
316     }
317     
318     return emptyVector;
319 }
320
321 string CURLSOAPTransport::getContentType() const
322 {
323     char* content_type=NULL;
324     curl_easy_getinfo(m_handle,CURLINFO_CONTENT_TYPE,&content_type);
325     return content_type ? content_type : "";
326 }
327
328 size_t CURLSOAPTransport::send(istream& in, ostream& out)
329 {
330 #ifdef _DEBUG
331     xmltooling::NDC ndc("send");
332 #endif
333     Category& log=Category::getInstance(XMLTOOLING_LOGCAT".SOAPTransport");
334     Category& log_curl=Category::getInstance(XMLTOOLING_LOGCAT".libcurl");
335
336     // By this time, the handle has been prepared with the URL to use and the
337     // caller should have executed any set functions to manipulate it.
338
339     // Setup standard per-call curl properties.
340     size_t content_length=0;
341     pair<ostream*,size_t*> output = make_pair(&out,&content_length); 
342     curl_easy_setopt(m_handle,CURLOPT_POST,1);
343     curl_easy_setopt(m_handle,CURLOPT_READDATA,&in);
344     curl_easy_setopt(m_handle,CURLOPT_FILE,&output);
345     curl_easy_setopt(m_handle,CURLOPT_DEBUGDATA,&log_curl);
346
347     char curl_errorbuf[CURL_ERROR_SIZE];
348     curl_errorbuf[0]=0;
349     curl_easy_setopt(m_handle,CURLOPT_ERRORBUFFER,curl_errorbuf);
350     if (log_curl.isDebugEnabled())
351         curl_easy_setopt(m_handle,CURLOPT_VERBOSE,1);
352
353     // Set request headers (possibly appended by hooks).
354     curl_easy_setopt(m_handle,CURLOPT_HTTPHEADER,m_headers);
355
356     if (m_ssl_callback || m_credResolver || m_trustEngine) {
357         curl_easy_setopt(m_handle,CURLOPT_SSL_CTX_FUNCTION,xml_ssl_ctx_callback);
358         curl_easy_setopt(m_handle,CURLOPT_SSL_CTX_DATA,this);
359     }
360     else {
361         curl_easy_setopt(m_handle,CURLOPT_SSL_CTX_FUNCTION,NULL);
362         curl_easy_setopt(m_handle,CURLOPT_SSL_CTX_DATA,NULL);
363     }
364     
365     // Verification of the peer is via TrustEngine only.
366     curl_easy_setopt(m_handle,CURLOPT_SSL_VERIFYPEER,0);
367
368     // Make the call.
369     log.info("sending SOAP message to %s", m_endpoint.c_str());
370     if (curl_easy_perform(m_handle) != CURLE_OK) {
371         log.error("failed communicating with SOAP endpoint: %s",
372             (curl_errorbuf[0] ? curl_errorbuf : "no further information available"));
373         throw IOException(
374             string("CURLSOAPTransport::send() failed while contacting SOAP responder: ") +
375                 (curl_errorbuf[0] ? curl_errorbuf : "no further information available"));
376     }
377     
378     return content_length;
379 }
380
381 // callback to buffer headers from server
382 size_t xmltooling::curl_header_hook(void* ptr, size_t size, size_t nmemb, void* stream)
383 {
384     // only handle single-byte data
385     if (size!=1)
386         return 0;
387     CURLSOAPTransport* ctx = reinterpret_cast<CURLSOAPTransport*>(stream);
388     char* buf = (char*)malloc(nmemb + 1);
389     if (buf) {
390         memset(buf,0,nmemb + 1);
391         memcpy(buf,ptr,nmemb);
392         char* sep=(char*)strchr(buf,':');
393         if (sep) {
394             *(sep++)=0;
395             while (*sep==' ')
396                 *(sep++)=0;
397             char* white=buf+nmemb-1;
398             while (isspace(*white))
399                 *(white--)=0;
400             ctx->m_response_headers[buf].push_back(sep);
401         }
402         free(buf);
403         return nmemb;
404     }
405     return 0;
406 }
407
408 // callback to send data to server
409 size_t xmltooling::curl_read_hook(void* ptr, size_t size, size_t nmemb, void* stream)
410 {
411     // *stream is actually an istream object
412     istream& buf=*(reinterpret_cast<istream*>(stream));
413     buf.read(reinterpret_cast<char*>(ptr),size*nmemb);
414     return buf.gcount();
415 }
416
417 // callback to buffer data from server
418 size_t xmltooling::curl_write_hook(void* ptr, size_t size, size_t nmemb, void* stream)
419 {
420     pair<ostream*,size_t*>* output = reinterpret_cast<pair<ostream*,size_t*>*>(stream); 
421     size_t len = size*nmemb;
422     output->first->write(reinterpret_cast<const char*>(ptr),len);
423     *(output->second) += len;
424     return len;
425 }
426
427 // callback for curl debug data
428 int xmltooling::curl_debug_hook(CURL* handle, curl_infotype type, char* data, size_t len, void* ptr)
429 {
430     // *ptr is actually a logging object
431     if (!ptr) return 0;
432     CategoryStream log=reinterpret_cast<Category*>(ptr)->debugStream();
433     for (char* ch=data; len && (isprint(*ch) || isspace(*ch)); len--)
434         log << *ch++;
435     log << CategoryStream::ENDLINE;
436     return 0;
437 }
438
439 int xmltooling::verify_callback(X509_STORE_CTX* x509_ctx, void* arg)
440 {
441     Category::getInstance("OpenSSL").debug("invoking X509 verify callback");
442 #if (OPENSSL_VERSION_NUMBER >= 0x00907000L)
443     CURLSOAPTransport* ctx = reinterpret_cast<CURLSOAPTransport*>(arg);
444 #else
445     // Yes, this sucks. I'd use TLS, but there's no really obvious spot to put the thread key
446     // and global variables suck too. We can't access the X509_STORE_CTX depth directly because
447     // OpenSSL only copies it into the context if it's >=0, and the unsigned pointer may be
448     // negative in the SSL structure's int member.
449     CURLSOAPTransport* ctx = reinterpret_cast<CURLSOAPTransport*>(
450         SSL_get_verify_depth(
451             reinterpret_cast<SSL*>(X509_STORE_CTX_get_ex_data(x509_ctx,SSL_get_ex_data_X509_STORE_CTX_idx()))
452             )
453         );
454 #endif
455
456      // Bypass name check (handled for us by curl).
457     if (!ctx->m_trustEngine->validate(x509_ctx->cert,x509_ctx->untrusted,ctx->m_peer,false,ctx->m_keyResolver)) {
458         x509_ctx->error=X509_V_ERR_APPLICATION_VERIFICATION;     // generic error, check log for plugin specifics
459         return 0;
460     }
461     
462     // Signal success. Hopefully it doesn't matter what's actually in the structure now.
463     return 1;
464 }
465
466 // callback to invoke a caller-defined SSL callback
467 CURLcode xmltooling::xml_ssl_ctx_callback(CURL* curl, SSL_CTX* ssl_ctx, void* userptr)
468 {
469     CURLSOAPTransport* conf = reinterpret_cast<CURLSOAPTransport*>(userptr);
470     if (conf->m_credResolver)
471         conf->m_credResolver->attach(ssl_ctx);
472
473     if (conf->m_trustEngine) {
474         SSL_CTX_set_verify(ssl_ctx,SSL_VERIFY_PEER,NULL);
475 #if (OPENSSL_VERSION_NUMBER >= 0x00907000L)
476         // With 0.9.7, we can pass a callback argument directly.
477         SSL_CTX_set_cert_verify_callback(ssl_ctx,verify_callback,userptr);
478 #else
479         // With 0.9.6, there's no argument, so we're going to use a really embarrassing hack and
480         // stuff the argument in the depth property where it will get copied to the context object
481         // that's handed to the callback.
482         SSL_CTX_set_cert_verify_callback(ssl_ctx,reinterpret_cast<int (*)()>(verify_callback),NULL);
483         SSL_CTX_set_verify_depth(ssl_ctx,reinterpret_cast<int>(userptr));
484 #endif
485     }
486         
487     if (!conf->m_ssl_callback(ssl_ctx,conf->m_ssl_userptr))
488         return CURLE_SSL_CERTPROBLEM;
489         
490     return CURLE_OK;
491 }