82dbc8db80c1436e01f583f9c440a8e395691b1a
[shibboleth/xmltooling.git] / xmltooling / XMLToolingConfig.cpp
1 /*
2  *  Copyright 2001-2007 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  * XMLToolingConfig.cpp
19  * 
20  * Library configuration 
21  */
22
23 #include "internal.h"
24 #include "exceptions.h"
25 #include "logging.h"
26 #include "XMLToolingConfig.h"
27 #include "encryption/Encryption.h"
28 #include "encryption/Encrypter.h"
29 #include "io/HTTPRequest.h"
30 #include "io/HTTPResponse.h"
31 #include "impl/UnknownElement.h"
32 #include "security/TrustEngine.h"
33 #include "security/OpenSSLCryptoX509CRL.h"
34 #include "security/CredentialResolver.h"
35 #include "security/KeyInfoResolver.h"
36 #include "signature/Signature.h"
37 #include "soap/SOAP.h"
38 #include "soap/SOAPTransport.h"
39 #include "util/NDC.h"
40 #include "util/PathResolver.h"
41 #include "util/ReplayCache.h"
42 #include "util/StorageService.h"
43 #include "util/TemplateEngine.h"
44 #include "util/URLEncoder.h"
45 #include "util/XMLConstants.h"
46 #include "validation/ValidatorSuite.h"
47
48 #ifdef HAVE_DLFCN_H
49 # include <dlfcn.h>
50 #endif
51
52 #include <stdexcept>
53 #if defined(XMLTOOLING_LOG4SHIB)
54 # include <log4shib/PropertyConfigurator.hh>
55 # include <log4shib/OstreamAppender.hh>
56 #elif defined(XMLTOOLING_LOG4CPP)
57 # include <log4cpp/PropertyConfigurator.hh>
58 # include <log4cpp/OstreamAppender.hh>
59 #endif
60 #include <xercesc/util/PlatformUtils.hpp>
61 #ifndef XMLTOOLING_NO_XMLSEC
62 # include <curl/curl.h>
63 # include <openssl/err.h>
64 # include <xsec/framework/XSECProvider.hpp>
65 #endif
66
67 using namespace soap11;
68 using namespace xmlencryption;
69 using namespace xmlsignature;
70 using namespace xmltooling::logging;
71 using namespace xmltooling;
72 using namespace std;
73
74 DECL_XMLTOOLING_EXCEPTION_FACTORY(XMLParserException,xmltooling);
75 DECL_XMLTOOLING_EXCEPTION_FACTORY(XMLObjectException,xmltooling);
76 DECL_XMLTOOLING_EXCEPTION_FACTORY(MarshallingException,xmltooling);
77 DECL_XMLTOOLING_EXCEPTION_FACTORY(UnmarshallingException,xmltooling);
78 DECL_XMLTOOLING_EXCEPTION_FACTORY(UnknownElementException,xmltooling);
79 DECL_XMLTOOLING_EXCEPTION_FACTORY(UnknownAttributeException,xmltooling);
80 DECL_XMLTOOLING_EXCEPTION_FACTORY(UnknownExtensionException,xmltooling);
81 DECL_XMLTOOLING_EXCEPTION_FACTORY(ValidationException,xmltooling);
82 DECL_XMLTOOLING_EXCEPTION_FACTORY(IOException,xmltooling);
83
84 #ifndef XMLTOOLING_NO_XMLSEC
85     DECL_XMLTOOLING_EXCEPTION_FACTORY(XMLSecurityException,xmltooling);
86     DECL_XMLTOOLING_EXCEPTION_FACTORY(SignatureException,xmlsignature);
87     DECL_XMLTOOLING_EXCEPTION_FACTORY(EncryptionException,xmlencryption);
88 #endif
89
90 namespace xmltooling {
91     static XMLToolingInternalConfig g_config;
92 #ifndef XMLTOOLING_NO_XMLSEC
93     static vector<Mutex*> g_openssl_locks;
94
95     extern "C" void openssl_locking_callback(int mode,int n,const char *file,int line)
96     {
97         if (mode & CRYPTO_LOCK)
98             g_openssl_locks[n]->lock();
99         else
100             g_openssl_locks[n]->unlock();
101     }
102     
103 # ifndef WIN32
104     extern "C" unsigned long openssl_thread_id(void)
105     {
106         return (unsigned long)(pthread_self());
107     }
108 # endif
109 #endif
110 }
111
112 XMLToolingConfig& XMLToolingConfig::getConfig()
113 {
114     return g_config;
115 }
116
117 XMLToolingInternalConfig& XMLToolingInternalConfig::getInternalConfig()
118 {
119     return g_config;
120 }
121
122 bool XMLToolingInternalConfig::log_config(const char* config)
123 {
124     try {
125         if (!config || !*config)
126             config=getenv("XMLTOOLING_LOG_CONFIG");
127         if (!config || !*config)
128             config="WARN";
129         
130         bool level=false;
131         Category& root = Category::getRoot();
132         if (!strcmp(config,"DEBUG")) {
133             root.setPriority(Priority::DEBUG);
134             level=true;
135         }
136         else if (!strcmp(config,"INFO")) {
137             root.setPriority(Priority::INFO);
138             level=true;
139         }
140         else if (!strcmp(config,"NOTICE")) {
141             root.setPriority(Priority::NOTICE);
142             level=true;
143         }
144         else if (!strcmp(config,"WARN")) {
145             root.setPriority(Priority::WARN);
146             level=true;
147         }
148         else if (!strcmp(config,"ERROR")) {
149             root.setPriority(Priority::ERROR);
150             level=true;
151         }
152         else if (!strcmp(config,"CRIT")) {
153             root.setPriority(Priority::CRIT);
154             level=true;
155         }
156         else if (!strcmp(config,"ALERT")) {
157             root.setPriority(Priority::ALERT);
158             level=true;
159         }
160         else if (!strcmp(config,"EMERG")) {
161             root.setPriority(Priority::EMERG);
162             level=true;
163         }
164         else if (!strcmp(config,"FATAL")) {
165             root.setPriority(Priority::FATAL);
166             level=true;
167         }
168         if (level) {
169             root.setAppender(new OstreamAppender("default",&cerr));
170         }
171         else {
172             string path(config);
173             PropertyConfigurator::configure(m_pathResolver ? m_pathResolver->resolve(path, PathResolver::XMLTOOLING_CFG_FILE).c_str() : config);
174         }
175     }
176     catch (const ConfigureFailure& e) {
177         Category::getInstance(XMLTOOLING_LOGCAT".Logging").crit("failed to initialize log4cpp: %s", e.what());
178         return false;
179     }
180     
181     return true;
182 }
183
184 #ifndef XMLTOOLING_LITE
185 void XMLToolingConfig::setReplayCache(ReplayCache* replayCache)
186 {
187     delete m_replayCache;
188     m_replayCache = replayCache;
189 }
190 #endif
191
192 void XMLToolingConfig::setPathResolver(PathResolver* pathResolver)
193 {
194     delete m_pathResolver;
195     m_pathResolver = pathResolver;
196 }
197
198 void XMLToolingConfig::setTemplateEngine(TemplateEngine* templateEngine)
199 {
200     delete m_templateEngine;
201     m_templateEngine = templateEngine;
202 }
203
204 void XMLToolingConfig::setURLEncoder(URLEncoder* urlEncoder)
205 {
206     delete m_urlEncoder;
207     m_urlEncoder = urlEncoder;
208 }
209
210 bool XMLToolingInternalConfig::init()
211 {
212 #ifdef _DEBUG
213     xmltooling::NDC ndc("init");
214 #endif
215     Category& log=Category::getInstance(XMLTOOLING_LOGCAT".XMLToolingConfig");
216     try {
217         log.debug("library initialization started");
218
219 #ifndef XMLTOOLING_NO_XMLSEC
220         if (curl_global_init(CURL_GLOBAL_ALL)) {
221             log.fatal("failed to initialize libcurl, OpenSSL, or Winsock");
222             return false;
223         }
224         log.debug("libcurl %s initialization complete", LIBCURL_VERSION);
225 #endif
226
227         XMLPlatformUtils::Initialize();
228         log.debug("Xerces %s initialization complete", XERCES_FULLVERSIONDOT);
229
230 #ifndef XMLTOOLING_NO_XMLSEC
231         XSECPlatformUtils::Initialise();
232         m_xsecProvider=new XSECProvider();
233         log.debug("XML-Security %s initialization complete", XSEC_FULLVERSIONDOT);
234 #endif
235
236         m_parserPool=new ParserPool();
237         m_validatingPool=new ParserPool(true,true);
238         m_lock=XMLPlatformUtils::makeMutex();
239         
240         // Load catalogs from path.
241         if (!catalog_path.empty()) {
242             char* catpath=strdup(catalog_path.c_str());
243             char* sep=NULL;
244             char* start=catpath;
245             while (start && *start) {
246                 sep=strchr(start,PATH_SEPARATOR_CHAR);
247                 if (sep)
248                     *sep=0;
249                 auto_ptr_XMLCh temp(start);
250                 m_validatingPool->loadCatalog(temp.get());
251                 start = sep ? sep + 1 : NULL;
252             }
253             free(catpath);
254         }
255
256         // default registrations
257         XMLObjectBuilder::registerDefaultBuilder(new UnknownElementBuilder());
258
259         registerKeyInfoClasses();
260         registerEncryptionClasses();
261         registerSOAPClasses();
262
263         REGISTER_XMLTOOLING_EXCEPTION_FACTORY(XMLParserException,xmltooling);
264         REGISTER_XMLTOOLING_EXCEPTION_FACTORY(XMLObjectException,xmltooling);
265         REGISTER_XMLTOOLING_EXCEPTION_FACTORY(MarshallingException,xmltooling);
266         REGISTER_XMLTOOLING_EXCEPTION_FACTORY(UnmarshallingException,xmltooling);
267         REGISTER_XMLTOOLING_EXCEPTION_FACTORY(UnknownElementException,xmltooling);
268         REGISTER_XMLTOOLING_EXCEPTION_FACTORY(UnknownAttributeException,xmltooling);
269         REGISTER_XMLTOOLING_EXCEPTION_FACTORY(ValidationException,xmltooling);
270         REGISTER_XMLTOOLING_EXCEPTION_FACTORY(IOException,xmltooling);
271         
272 #ifndef XMLTOOLING_NO_XMLSEC
273         XMLObjectBuilder::registerBuilder(QName(xmlconstants::XMLSIG_NS,Signature::LOCAL_NAME),new SignatureBuilder());
274         REGISTER_XMLTOOLING_EXCEPTION_FACTORY(XMLSecurityException,xmltooling);
275         REGISTER_XMLTOOLING_EXCEPTION_FACTORY(SignatureException,xmlsignature);
276         REGISTER_XMLTOOLING_EXCEPTION_FACTORY(EncryptionException,xmlencryption);
277         registerKeyInfoResolvers();
278         registerCredentialResolvers();
279         registerTrustEngines();
280         registerXMLAlgorithms();
281         registerSOAPTransports();
282         initSOAPTransports();
283         registerStorageServices();
284         m_keyInfoResolver = KeyInfoResolverManager.newPlugin(INLINE_KEYINFO_RESOLVER,NULL);
285 #endif
286
287         m_pathResolver = new PathResolver();
288         m_urlEncoder = new URLEncoder();
289         
290         // Register xml:id as an ID attribute.        
291         static const XMLCh xmlid[] = UNICODE_LITERAL_2(i,d);
292         AttributeExtensibleXMLObject::registerIDAttribute(QName(xmlconstants::XML_NS, xmlid)); 
293     }
294     catch (const xercesc::XMLException&) {
295         log.fatal("caught exception while initializing Xerces");
296 #ifndef XMLTOOLING_NO_XMLSEC
297         curl_global_cleanup();
298 #endif
299         return false;
300     }
301
302 #ifndef XMLTOOLING_NO_XMLSEC
303     // Set up OpenSSL locking.
304     for (int i=0; i<CRYPTO_num_locks(); i++)
305         g_openssl_locks.push_back(Mutex::create());
306     CRYPTO_set_locking_callback(openssl_locking_callback);
307 # ifndef WIN32
308     CRYPTO_set_id_callback(openssl_thread_id);
309 # endif
310 #endif
311
312     log.info("%s library initialization complete", PACKAGE_STRING);
313     return true;
314 }
315
316 void XMLToolingInternalConfig::term()
317 {
318 #ifndef XMLTOOLING_NO_XMLSEC
319     CRYPTO_set_locking_callback(NULL);
320     for_each(g_openssl_locks.begin(), g_openssl_locks.end(), xmltooling::cleanup<Mutex>());
321     g_openssl_locks.clear();
322 #endif
323
324     SchemaValidators.destroyValidators();
325     XMLObjectBuilder::destroyBuilders();
326     XMLToolingException::deregisterFactories();
327     AttributeExtensibleXMLObject::deregisterIDAttributes();
328
329 #ifndef XMLTOOLING_NO_XMLSEC
330     StorageServiceManager.deregisterFactories();
331     termSOAPTransports();
332     SOAPTransportManager.deregisterFactories();
333     TrustEngineManager.deregisterFactories();
334     CredentialResolverManager.deregisterFactories();
335     KeyInfoResolverManager.deregisterFactories();
336     m_algorithmMap.clear();
337
338     delete m_keyInfoResolver;
339     m_keyInfoResolver = NULL;
340
341     delete m_replayCache;
342     m_replayCache = NULL;
343 #endif
344
345     delete m_pathResolver;
346     m_pathResolver = NULL;
347     
348     delete m_templateEngine;
349     m_templateEngine = NULL;
350
351     delete m_urlEncoder;
352     m_urlEncoder = NULL;
353
354     for (vector<void*>::reverse_iterator i=m_libhandles.rbegin(); i!=m_libhandles.rend(); i++) {
355 #if defined(WIN32)
356         FARPROC fn=GetProcAddress(static_cast<HMODULE>(*i),"xmltooling_extension_term");
357         if (fn)
358             fn();
359         FreeLibrary(static_cast<HMODULE>(*i));
360 #elif defined(HAVE_DLFCN_H)
361         void (*fn)()=(void (*)())dlsym(*i,"xmltooling_extension_term");
362         if (fn)
363             fn();
364         dlclose(*i);
365 #else
366 # error "Don't know about dynamic loading on this platform!"
367 #endif
368     }
369     m_libhandles.clear();
370     
371     delete m_parserPool;
372     m_parserPool=NULL;
373     delete m_validatingPool;
374     m_validatingPool=NULL;
375
376 #ifndef XMLTOOLING_NO_XMLSEC
377     delete m_xsecProvider;
378     m_xsecProvider=NULL;
379     XSECPlatformUtils::Terminate();
380 #endif
381
382     XMLPlatformUtils::closeMutex(m_lock);
383     m_lock=NULL;
384     XMLPlatformUtils::Terminate();
385
386 #ifndef XMLTOOLING_NO_XMLSEC
387     curl_global_cleanup();
388 #endif   
389 #ifdef _DEBUG
390     xmltooling::NDC ndc("term");
391 #endif
392    Category::getInstance(XMLTOOLING_LOGCAT".XMLToolingConfig").info("%s library shutdown complete", PACKAGE_STRING);
393 }
394
395 Lockable* XMLToolingInternalConfig::lock()
396 {
397     xercesc::XMLPlatformUtils::lockMutex(m_lock);
398     return this;
399 }
400
401 void XMLToolingInternalConfig::unlock()
402 {
403     xercesc::XMLPlatformUtils::unlockMutex(m_lock);
404 }
405
406 bool XMLToolingInternalConfig::load_library(const char* path, void* context)
407 {
408 #ifdef _DEBUG
409     xmltooling::NDC ndc("LoadLibrary");
410 #endif
411     Category& log=Category::getInstance(XMLTOOLING_LOGCAT".XMLToolingConfig");
412     log.info("loading extension: %s", path);
413
414     Locker locker(this);
415
416     string resolved(path);
417     m_pathResolver->resolve(resolved, PathResolver::XMLTOOLING_LIB_FILE);
418     
419 #if defined(WIN32)
420     HMODULE handle=NULL;
421     for (string::iterator i = resolved.begin(); i != resolved.end(); ++i)
422         if (*i == '/')
423             *i = '\\';
424     
425     UINT em=SetErrorMode(SEM_FAILCRITICALERRORS);
426     try {
427         handle=LoadLibraryEx(resolved.c_str(),NULL,LOAD_WITH_ALTERED_SEARCH_PATH);
428         if (!handle)
429              handle=LoadLibraryEx(resolved.c_str(),NULL,0);
430         if (!handle)
431             throw runtime_error(string("unable to load extension library: ") + resolved);
432         FARPROC fn=GetProcAddress(handle,"xmltooling_extension_init");
433         if (!fn)
434             throw runtime_error(string("unable to locate xmltooling_extension_init entry point: ") + resolved);
435         if (reinterpret_cast<int(*)(void*)>(fn)(context)!=0)
436             throw runtime_error(string("detected error in xmltooling_extension_init: ") + resolved);
437         SetErrorMode(em);
438     }
439     catch(runtime_error& e) {
440         log.error(e.what());
441         if (handle)
442             FreeLibrary(handle);
443         SetErrorMode(em);
444         return false;
445     }
446
447 #elif defined(HAVE_DLFCN_H)
448     void* handle=dlopen(resolved.c_str(),RTLD_LAZY);
449     if (!handle)
450         throw runtime_error(string("unable to load extension library '") + resolved + "': " + dlerror());
451     int (*fn)(void*)=(int (*)(void*))(dlsym(handle,"xmltooling_extension_init"));
452     if (!fn) {
453         dlclose(handle);
454         throw runtime_error(
455             string("unable to locate xmltooling_extension_init entry point in '") + resolved + "': " +
456                 (dlerror() ? dlerror() : "unknown error")
457             );
458     }
459     try {
460         if (fn(context)!=0)
461             throw runtime_error(string("detected error in xmltooling_extension_init in ") + resolved);
462     }
463     catch(runtime_error& e) {
464         log.error(e.what());
465         if (handle)
466             dlclose(handle);
467         return false;
468     }
469 #else
470 # error "Don't know about dynamic loading on this platform!"
471 #endif
472     m_libhandles.push_back(handle);
473     log.info("loaded extension: %s", resolved.c_str());
474     return true;
475 }
476
477 #ifndef XMLTOOLING_NO_XMLSEC
478 void xmltooling::log_openssl()
479 {
480     const char* file;
481     const char* data;
482     int flags,line;
483
484     unsigned long code=ERR_get_error_line_data(&file,&line,&data,&flags);
485     while (code) {
486         Category& log=Category::getInstance("OpenSSL");
487         log.errorStream() << "error code: " << code << " in " << file << ", line " << line << logging::eol;
488         if (data && (flags & ERR_TXT_STRING))
489             log.errorStream() << "error data: " << data << logging::eol;
490         code=ERR_get_error_line_data(&file,&line,&data,&flags);
491     }
492 }
493
494 XSECCryptoX509CRL* XMLToolingInternalConfig::X509CRL() const
495 {
496     return new OpenSSLCryptoX509CRL();
497 }
498
499 void XMLToolingInternalConfig::registerXMLAlgorithms()
500 {
501     registerXMLAlgorithm(DSIGConstants::s_unicodeStrURIRSA_MD5, "RSA", 0);
502     registerXMLAlgorithm(DSIGConstants::s_unicodeStrURIRSA_SHA1, "RSA", 0);
503     registerXMLAlgorithm(DSIGConstants::s_unicodeStrURIRSA_SHA224, "RSA", 0);
504     registerXMLAlgorithm(DSIGConstants::s_unicodeStrURIRSA_SHA256, "RSA", 0);
505     registerXMLAlgorithm(DSIGConstants::s_unicodeStrURIRSA_SHA384, "RSA", 0);
506     registerXMLAlgorithm(DSIGConstants::s_unicodeStrURIRSA_SHA512, "RSA", 0);
507
508     registerXMLAlgorithm(DSIGConstants::s_unicodeStrURIRSA_1_5, "RSA", 0);
509     registerXMLAlgorithm(DSIGConstants::s_unicodeStrURIRSA_OAEP_MGFP1, "RSA", 0);
510
511     registerXMLAlgorithm(DSIGConstants::s_unicodeStrURIDSA_SHA1, "DSA", 0);
512
513     registerXMLAlgorithm(DSIGConstants::s_unicodeStrURIHMAC_SHA1, "HMAC", 0);
514     registerXMLAlgorithm(DSIGConstants::s_unicodeStrURIHMAC_SHA224, "HMAC", 0);
515     registerXMLAlgorithm(DSIGConstants::s_unicodeStrURIHMAC_SHA256, "HMAC", 0);
516     registerXMLAlgorithm(DSIGConstants::s_unicodeStrURIHMAC_SHA384, "HMAC", 0);
517     registerXMLAlgorithm(DSIGConstants::s_unicodeStrURIHMAC_SHA512, "HMAC", 0);
518
519     registerXMLAlgorithm(DSIGConstants::s_unicodeStrURI3DES_CBC, "DESede", 192);
520     registerXMLAlgorithm(DSIGConstants::s_unicodeStrURIKW_3DES, "DESede", 192);
521
522     registerXMLAlgorithm(DSIGConstants::s_unicodeStrURIAES128_CBC, "AES", 128);
523     registerXMLAlgorithm(DSIGConstants::s_unicodeStrURIKW_AES128, "AES", 128);
524
525     registerXMLAlgorithm(DSIGConstants::s_unicodeStrURIAES192_CBC, "AES", 192);
526     registerXMLAlgorithm(DSIGConstants::s_unicodeStrURIKW_AES192, "AES", 192);
527
528     registerXMLAlgorithm(DSIGConstants::s_unicodeStrURIAES256_CBC, "AES", 256);
529     registerXMLAlgorithm(DSIGConstants::s_unicodeStrURIKW_AES256, "AES", 256);
530 }
531 #endif