X-Git-Url: http://www.project-moonshot.org/gitweb/?a=blobdiff_plain;f=xmltooling%2FXMLToolingConfig.cpp;h=fd6ce97cffb6ac5b48d26ac8860426fb8de965cd;hb=f9beeedeecae5da6e9d8b3b85aa2c9a68cc388e9;hp=3c0a9c5b4122ca721f5fbd3c4c07f76cd0c84d13;hpb=420effca96f38dfa5f2a6549679a5d455a3945a7;p=shibboleth%2Fcpp-xmltooling.git diff --git a/xmltooling/XMLToolingConfig.cpp b/xmltooling/XMLToolingConfig.cpp index 3c0a9c5..fd6ce97 100644 --- a/xmltooling/XMLToolingConfig.cpp +++ b/xmltooling/XMLToolingConfig.cpp @@ -1,17 +1,21 @@ -/* - * Copyright 2001-2010 Internet2 +/** + * Licensed to the University Corporation for Advanced Internet + * Development, Inc. (UCAID) under one or more contributor license + * agreements. See the NOTICE file distributed with this work for + * additional information regarding copyright ownership. * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at + * UCAID licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the + * License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + * either express or implied. See the License for the specific + * language governing permissions and limitations under the License. */ /** @@ -32,9 +36,11 @@ #include "security/OpenSSLCryptoX509CRL.h" #include "security/CredentialResolver.h" #include "security/KeyInfoResolver.h" +#include "security/PathValidator.h" #include "signature/KeyInfo.h" #include "signature/Signature.h" #include "soap/SOAP.h" +#include "soap/SOAPTransport.h" #include "util/NDC.h" #include "util/PathResolver.h" #include "util/ReplayCache.h" @@ -49,6 +55,10 @@ #endif #include +#include +#include +#include + #if defined(XMLTOOLING_LOG4SHIB) # include # include @@ -72,11 +82,13 @@ using namespace soap11; using namespace xmltooling::logging; using namespace xmltooling; using namespace xercesc; +using namespace boost::lambda; +using namespace boost; using namespace std; #ifdef WIN32 # if (OPENSSL_VERSION_NUMBER >= 0x00908000) -# define XMLTOOLING_OPENSSL_HAVE_SHA2 +# define XMLTOOLING_OPENSSL_HAVE_SHA2 1 # endif #endif @@ -218,6 +230,76 @@ XMLToolingConfig::~XMLToolingConfig() { } +#ifndef XMLTOOLING_LITE +const KeyInfoResolver* XMLToolingConfig::getKeyInfoResolver() const +{ + return m_keyInfoResolver; +} + +ReplayCache* XMLToolingConfig::getReplayCache() const +{ + return m_replayCache; +} + +void XMLToolingConfig::setKeyInfoResolver(xmltooling::KeyInfoResolver *keyInfoResolver) +{ + delete m_keyInfoResolver; + m_keyInfoResolver = keyInfoResolver; +} + +void XMLToolingConfig::setReplayCache(ReplayCache* replayCache) +{ + delete m_replayCache; + m_replayCache = replayCache; +} +#endif + +PathResolver* XMLToolingConfig::getPathResolver() const +{ + return m_pathResolver; +} + +TemplateEngine* XMLToolingConfig::getTemplateEngine() const +{ + return m_templateEngine; +} + +const URLEncoder* XMLToolingConfig::getURLEncoder() const +{ + return m_urlEncoder; +} + +void XMLToolingConfig::setPathResolver(PathResolver* pathResolver) +{ + delete m_pathResolver; + m_pathResolver = pathResolver; +} + +void XMLToolingConfig::setTemplateEngine(TemplateEngine* templateEngine) +{ + delete m_templateEngine; + m_templateEngine = templateEngine; +} + +void XMLToolingConfig::setURLEncoder(URLEncoder* urlEncoder) +{ + delete m_urlEncoder; + m_urlEncoder = urlEncoder; +} + +XMLToolingInternalConfig::XMLToolingInternalConfig() : +#ifndef XMLTOOLING_NO_XMLSEC + m_xsecProvider(nullptr), +#endif + m_initCount(0), m_lock(Mutex::create()), m_parserPool(nullptr), m_validatingPool(nullptr) +{ +} + +XMLToolingInternalConfig::~XMLToolingInternalConfig() +{ + delete m_lock; +} + bool XMLToolingInternalConfig::log_config(const char* config) { try { @@ -271,7 +353,11 @@ bool XMLToolingInternalConfig::log_config(const char* config) string path(config); PropertyConfigurator::configure(m_pathResolver ? m_pathResolver->resolve(path, PathResolver::XMLTOOLING_CFG_FILE) : path); } - } + +#ifndef XMLTOOLING_NO_XMLSEC + Category::getInstance(XMLTOOLING_LOGCAT".Signature.Debugger").setAdditivity(false); +#endif + } catch (const ConfigureFailure& e) { string msg = string("failed to configure logging: ") + e.what(); Category::getInstance(XMLTOOLING_LOGCAT".Logging").crit(msg); @@ -284,69 +370,25 @@ bool XMLToolingInternalConfig::log_config(const char* config) return true; } -#ifndef XMLTOOLING_LITE -const KeyInfoResolver* XMLToolingConfig::getKeyInfoResolver() const -{ - return m_keyInfoResolver; -} - -ReplayCache* XMLToolingConfig::getReplayCache() const -{ - return m_replayCache; -} - -void XMLToolingConfig::setKeyInfoResolver(xmltooling::KeyInfoResolver *keyInfoResolver) -{ - delete m_keyInfoResolver; - m_keyInfoResolver = keyInfoResolver; -} - -void XMLToolingConfig::setReplayCache(ReplayCache* replayCache) +bool XMLToolingInternalConfig::init() { - delete m_replayCache; - m_replayCache = replayCache; -} +#ifdef _DEBUG + xmltooling::NDC ndc("init"); #endif + Category& log=Category::getInstance(XMLTOOLING_LOGCAT".Config"); -PathResolver* XMLToolingConfig::getPathResolver() const -{ - return m_pathResolver; -} - -TemplateEngine* XMLToolingConfig::getTemplateEngine() const -{ - return m_templateEngine; -} + Lock initLock(m_lock); -const URLEncoder* XMLToolingConfig::getURLEncoder() const -{ - return m_urlEncoder; -} - -void XMLToolingConfig::setPathResolver(PathResolver* pathResolver) -{ - delete m_pathResolver; - m_pathResolver = pathResolver; -} - -void XMLToolingConfig::setTemplateEngine(TemplateEngine* templateEngine) -{ - delete m_templateEngine; - m_templateEngine = templateEngine; -} + if (m_initCount == INT_MAX) { + log.crit("library initialized too many times"); + return false; + } -void XMLToolingConfig::setURLEncoder(URLEncoder* urlEncoder) -{ - delete m_urlEncoder; - m_urlEncoder = urlEncoder; -} + if (m_initCount >= 1) { + ++m_initCount; + return true; + } -bool XMLToolingInternalConfig::init() -{ -#ifdef _DEBUG - xmltooling::NDC ndc("init"); -#endif - Category& log=Category::getInstance(XMLTOOLING_LOGCAT".XMLToolingConfig"); try { log.debug("library initialization started"); @@ -355,7 +397,16 @@ bool XMLToolingInternalConfig::init() log.fatal("failed to initialize libcurl, OpenSSL, or Winsock"); return false; } - log.debug("libcurl %s initialization complete", LIBCURL_VERSION); + curl_version_info_data* curlver = curl_version_info(CURLVERSION_NOW); + if (curlver) { + log.debug("libcurl %s initialization complete", curlver->version); + if (!(curlver->features & CURL_VERSION_SSL)) { + log.warn("libcurl lacks TLS/SSL support, this will greatly limit functionality"); + } + } + else { + log.debug("libcurl %s initialization complete", LIBCURL_VERSION); + } #endif XMLPlatformUtils::Initialize(); @@ -372,22 +423,16 @@ bool XMLToolingInternalConfig::init() m_parserPool=new ParserPool(); m_validatingPool=new ParserPool(true,true); - m_lock=XMLPlatformUtils::makeMutex(); // Load catalogs from path. if (!catalog_path.empty()) { - char* catpath=strdup(catalog_path.c_str()); - char* sep=nullptr; - char* start=catpath; - while (start && *start) { - sep=strchr(start,PATH_SEPARATOR_CHAR); - if (sep) - *sep=0; - auto_ptr_XMLCh temp(start); - m_validatingPool->loadCatalog(temp.get()); - start = sep ? sep + 1 : nullptr; - } - free(catpath); + vector catpaths; + split(catpaths, catalog_path, is_any_of(PATH_SEPARATOR_STR), algorithm::token_compress_on); + for_each( + catpaths.begin(), catpaths.end(), + // Call loadCatalog with an inner call to s->c_str() on each entry. + lambda::bind(static_cast(&ParserPool::loadCatalog), m_validatingPool, lambda::bind(&string::c_str,_1)) + ); } // default registrations @@ -411,16 +456,20 @@ bool XMLToolingInternalConfig::init() REGISTER_XMLTOOLING_EXCEPTION_FACTORY(EncryptionException,xmlencryption); registerKeyInfoClasses(); registerEncryptionClasses(); - registerKeyInfoResolvers(); registerCredentialResolvers(); + registerKeyInfoResolvers(); + registerPathValidators(); registerTrustEngines(); registerXMLAlgorithms(); - registerSOAPTransports(); - initSOAPTransports(); - registerStorageServices(); m_keyInfoResolver = KeyInfoResolverManager.newPlugin(INLINE_KEYINFO_RESOLVER,nullptr); #endif +#ifndef XMLTOOLING_LITE + registerStorageServices(); +#endif + registerSOAPTransports(); + initSOAPTransports(); + m_pathResolver = new PathResolver(); m_urlEncoder = new URLEncoder(); @@ -450,11 +499,25 @@ bool XMLToolingInternalConfig::init() #endif log.info("%s library initialization complete", PACKAGE_STRING); + ++m_initCount; return true; } void XMLToolingInternalConfig::term() { +#ifdef _DEBUG + xmltooling::NDC ndc("term"); +#endif + + Lock initLock(m_lock); + if (m_initCount == 0) { + Category::getInstance(XMLTOOLING_LOGCAT".Config").crit("term without corresponding init"); + return; + } + else if (--m_initCount > 0) { + return; + } + #ifndef XMLTOOLING_NO_XMLSEC CRYPTO_set_locking_callback(nullptr); for_each(g_openssl_locks.begin(), g_openssl_locks.end(), xmltooling::cleanup()); @@ -466,10 +529,14 @@ void XMLToolingInternalConfig::term() XMLToolingException::deregisterFactories(); AttributeExtensibleXMLObject::deregisterIDAttributes(); -#ifndef XMLTOOLING_NO_XMLSEC - StorageServiceManager.deregisterFactories(); termSOAPTransports(); SOAPTransportManager.deregisterFactories(); + +#ifndef XMLTOOLING_LITE + StorageServiceManager.deregisterFactories(); +#endif + +#ifndef XMLTOOLING_NO_XMLSEC TrustEngineManager.deregisterFactories(); CredentialResolverManager.deregisterFactories(); KeyInfoResolverManager.deregisterFactories(); @@ -513,34 +580,43 @@ void XMLToolingInternalConfig::term() delete m_validatingPool; m_validatingPool=nullptr; + for_each(m_namedLocks.begin(), m_namedLocks.end(), cleanup_pair()); + m_namedLocks.clear(); + #ifndef XMLTOOLING_NO_XMLSEC delete m_xsecProvider; m_xsecProvider=nullptr; XSECPlatformUtils::Terminate(); #endif - XMLPlatformUtils::closeMutex(m_lock); - m_lock=nullptr; XMLPlatformUtils::Terminate(); #ifndef XMLTOOLING_NO_XMLSEC curl_global_cleanup(); #endif -#ifdef _DEBUG - xmltooling::NDC ndc("term"); -#endif - Category::getInstance(XMLTOOLING_LOGCAT".XMLToolingConfig").info("%s library shutdown complete", PACKAGE_STRING); + Category::getInstance(XMLTOOLING_LOGCAT".Config").info("%s library shutdown complete", PACKAGE_STRING); } Lockable* XMLToolingInternalConfig::lock() { - xercesc::XMLPlatformUtils::lockMutex(m_lock); + m_lock->lock(); return this; } void XMLToolingInternalConfig::unlock() { - xercesc::XMLPlatformUtils::unlockMutex(m_lock); + m_lock->unlock(); +} + +Mutex& XMLToolingInternalConfig::getNamedMutex(const char* name) +{ + Locker glock(this); + map::const_iterator m = m_namedLocks.find(name); + if (m != m_namedLocks.end()) + return *(m->second); + Mutex* newlock = Mutex::create(); + m_namedLocks[name] = newlock; + return *newlock; } bool XMLToolingInternalConfig::load_library(const char* path, void* context) @@ -548,7 +624,7 @@ bool XMLToolingInternalConfig::load_library(const char* path, void* context) #ifdef _DEBUG xmltooling::NDC ndc("LoadLibrary"); #endif - Category& log=Category::getInstance(XMLTOOLING_LOGCAT".XMLToolingConfig"); + Category& log=Category::getInstance(XMLTOOLING_LOGCAT".Config"); log.info("loading extension: %s", path); Locker locker(this); @@ -576,7 +652,7 @@ bool XMLToolingInternalConfig::load_library(const char* path, void* context) throw runtime_error(string("detected error in xmltooling_extension_init: ") + resolved); SetErrorMode(em); } - catch(exception&) { + catch(std::exception&) { if (handle) FreeLibrary(handle); SetErrorMode(em); @@ -599,7 +675,7 @@ bool XMLToolingInternalConfig::load_library(const char* path, void* context) if (fn(context)!=0) throw runtime_error(string("detected error in xmltooling_extension_init in ") + resolved); } - catch(exception&) { + catch(std::exception&) { if (handle) dlclose(handle); throw;