2 * Copyright 2001-2006 Internet2
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * XMLToolingConfig.cpp
20 * Library configuration
24 #include "exceptions.h"
25 #include "XMLToolingConfig.h"
26 #include "encryption/Encryption.h"
27 #include "impl/UnknownElement.h"
28 #include "security/TrustEngine.h"
29 #include "security/OpenSSLCryptoX509CRL.h"
30 #include "signature/CredentialResolver.h"
31 #include "soap/SOAP.h"
33 #include "util/XMLConstants.h"
34 #include "validation/Validator.h"
41 #include <log4cpp/Category.hh>
42 #include <log4cpp/PropertyConfigurator.hh>
43 #include <log4cpp/OstreamAppender.hh>
44 #include <xercesc/util/PlatformUtils.hpp>
45 #ifndef XMLTOOLING_NO_XMLSEC
46 #include <xsec/framework/XSECProvider.hpp>
47 #include <openssl/err.h>
50 using namespace soap11;
51 using namespace xmlencryption;
52 using namespace xmlsignature;
53 using namespace xmltooling;
54 using namespace log4cpp;
57 DECL_EXCEPTION_FACTORY(XMLParserException,xmltooling);
58 DECL_EXCEPTION_FACTORY(XMLObjectException,xmltooling);
59 DECL_EXCEPTION_FACTORY(MarshallingException,xmltooling);
60 DECL_EXCEPTION_FACTORY(UnmarshallingException,xmltooling);
61 DECL_EXCEPTION_FACTORY(UnknownElementException,xmltooling);
62 DECL_EXCEPTION_FACTORY(UnknownAttributeException,xmltooling);
63 DECL_EXCEPTION_FACTORY(UnknownExtensionException,xmltooling);
64 DECL_EXCEPTION_FACTORY(ValidationException,xmltooling);
66 #ifndef XMLTOOLING_NO_XMLSEC
67 DECL_EXCEPTION_FACTORY(SignatureException,xmlsignature);
70 namespace xmltooling {
71 XMLToolingInternalConfig g_config;
74 XMLToolingConfig& XMLToolingConfig::getConfig()
79 XMLToolingInternalConfig& XMLToolingInternalConfig::getInternalConfig()
84 bool XMLToolingInternalConfig::log_config(const char* config)
87 if (!config || !*config)
88 config=getenv("XMLTOOLING_LOG_CONFIG");
89 if (!config || !*config)
93 Category& root = Category::getRoot();
94 if (!strcmp(config,"DEBUG")) {
95 root.setPriority(Priority::DEBUG);
98 else if (!strcmp(config,"INFO")) {
99 root.setPriority(Priority::INFO);
102 else if (!strcmp(config,"NOTICE")) {
103 root.setPriority(Priority::NOTICE);
106 else if (!strcmp(config,"WARN")) {
107 root.setPriority(Priority::WARN);
110 else if (!strcmp(config,"ERROR")) {
111 root.setPriority(Priority::ERROR);
114 else if (!strcmp(config,"CRIT")) {
115 root.setPriority(Priority::CRIT);
118 else if (!strcmp(config,"ALERT")) {
119 root.setPriority(Priority::ALERT);
122 else if (!strcmp(config,"EMERG")) {
123 root.setPriority(Priority::EMERG);
126 else if (!strcmp(config,"FATAL")) {
127 root.setPriority(Priority::FATAL);
131 root.setAppender(new OstreamAppender("default",&cerr));
133 PropertyConfigurator::configure(config);
135 catch (const ConfigureFailure& e) {
136 Category::getInstance(XMLTOOLING_LOGCAT".Logging").crit("failed to initialize log4cpp: %s", e.what());
143 bool XMLToolingInternalConfig::init()
146 xmltooling::NDC ndc("init");
148 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".XMLToolingConfig");
150 log.debug("library initialization started");
152 xercesc::XMLPlatformUtils::Initialize();
153 log.debug("Xerces initialization complete");
155 #ifndef XMLTOOLING_NO_XMLSEC
156 XSECPlatformUtils::Initialise();
157 m_xsecProvider=new XSECProvider();
158 log.debug("XMLSec initialization complete");
161 m_parserPool=new ParserPool();
162 m_validatingPool=new ParserPool(true,true);
163 m_lock=xercesc::XMLPlatformUtils::makeMutex();
165 // default registrations
166 XMLObjectBuilder::registerDefaultBuilder(new UnknownElementBuilder());
168 registerKeyInfoClasses();
169 registerEncryptionClasses();
170 registerSOAPClasses();
172 REGISTER_EXCEPTION_FACTORY(XMLParserException,xmltooling);
173 REGISTER_EXCEPTION_FACTORY(XMLObjectException,xmltooling);
174 REGISTER_EXCEPTION_FACTORY(MarshallingException,xmltooling);
175 REGISTER_EXCEPTION_FACTORY(UnmarshallingException,xmltooling);
176 REGISTER_EXCEPTION_FACTORY(UnknownElementException,xmltooling);
177 REGISTER_EXCEPTION_FACTORY(UnknownAttributeException,xmltooling);
178 REGISTER_EXCEPTION_FACTORY(ValidationException,xmltooling);
180 #ifndef XMLTOOLING_NO_XMLSEC
181 XMLObjectBuilder::registerBuilder(QName(XMLConstants::XMLSIG_NS,Signature::LOCAL_NAME),new SignatureBuilder());
182 REGISTER_EXCEPTION_FACTORY(SignatureException,xmlsignature);
183 registerKeyResolvers();
184 registerCredentialResolvers();
185 registerTrustEngines();
188 catch (const xercesc::XMLException&) {
189 log.fatal("caught exception while initializing Xerces");
193 log.info("library initialization complete");
197 void XMLToolingInternalConfig::term()
199 XMLObjectBuilder::destroyBuilders();
200 KeyInfoSchemaValidators.destroyValidators();
201 EncryptionSchemaValidators.destroyValidators();
202 XMLToolingException::deregisterFactories();
204 #ifndef XMLTOOLING_NO_XMLSEC
205 TrustEngineManager.deregisterFactories();
206 CredentialResolverManager.deregisterFactories();
207 KeyResolverManager.deregisterFactories();
210 for (vector<void*>::reverse_iterator i=m_libhandles.rbegin(); i!=m_libhandles.rend(); i++) {
212 FARPROC fn=GetProcAddress(static_cast<HMODULE>(*i),"xmltooling_extension_term");
215 FreeLibrary(static_cast<HMODULE>(*i));
216 #elif defined(HAVE_DLFCN_H)
217 void (*fn)()=(void (*)())dlsym(*i,"xmltooling_extension_term");
222 # error "Don't know about dynamic loading on this platform!"
225 m_libhandles.clear();
229 delete m_validatingPool;
230 m_validatingPool=NULL;
232 #ifndef XMLTOOLING_NO_XMLSEC
233 delete m_xsecProvider;
235 XSECPlatformUtils::Terminate();
238 xercesc::XMLPlatformUtils::closeMutex(m_lock);
240 xercesc::XMLPlatformUtils::Terminate();
243 xmltooling::NDC ndc("term");
245 Category::getInstance(XMLTOOLING_LOGCAT".XMLToolingConfig").info("library shutdown complete");
248 Lockable* XMLToolingInternalConfig::lock()
250 xercesc::XMLPlatformUtils::lockMutex(m_lock);
254 void XMLToolingInternalConfig::unlock()
256 xercesc::XMLPlatformUtils::unlockMutex(m_lock);
259 bool XMLToolingInternalConfig::load_library(const char* path, void* context)
262 xmltooling::NDC ndc("LoadLibrary");
264 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".XMLToolingConfig");
265 log.info("loading extension: %s", path);
271 char* fixed=const_cast<char*>(path);
272 if (strchr(fixed,'/')) {
275 while (p=strchr(p,'/'))
279 UINT em=SetErrorMode(SEM_FAILCRITICALERRORS);
281 handle=LoadLibraryEx(fixed,NULL,LOAD_WITH_ALTERED_SEARCH_PATH);
283 handle=LoadLibraryEx(fixed,NULL,0);
285 throw runtime_error(string("unable to load extension library: ") + fixed);
286 FARPROC fn=GetProcAddress(handle,"xmltooling_extension_init");
288 throw runtime_error(string("unable to locate xmltooling_extension_init entry point: ") + fixed);
289 if (reinterpret_cast<int(*)(void*)>(fn)(context)!=0)
290 throw runtime_error(string("detected error in xmltooling_extension_init: ") + fixed);
295 catch(runtime_error& e) {
305 #elif defined(HAVE_DLFCN_H)
306 void* handle=dlopen(path,RTLD_LAZY);
308 throw runtime_error(string("unable to load extension library '") + path + "': " + dlerror());
309 int (*fn)(void*)=(int (*)(void*))(dlsym(handle,"xmltooling_extension_init"));
313 string("unable to locate xmltooling_extension_init entry point in '") + path + "': " +
314 (dlerror() ? dlerror() : "unknown error")
319 throw runtime_error(string("detected error in xmltooling_extension_init in ") + path);
321 catch(runtime_error& e) {
328 # error "Don't know about dynamic loading on this platform!"
330 m_libhandles.push_back(handle);
331 log.info("loaded extension: %s", path);
335 #ifndef XMLTOOLING_NO_XMLSEC
336 void xmltooling::log_openssl()
342 unsigned long code=ERR_get_error_line_data(&file,&line,&data,&flags);
344 Category& log=Category::getInstance("OpenSSL");
345 log.errorStream() << "error code: " << code << " in " << file << ", line " << line << CategoryStream::ENDLINE;
346 if (data && (flags & ERR_TXT_STRING))
347 log.errorStream() << "error data: " << data << CategoryStream::ENDLINE;
348 code=ERR_get_error_line_data(&file,&line,&data,&flags);
352 XSECCryptoX509CRL* XMLToolingInternalConfig::X509CRL() const
354 return new OpenSSLCryptoX509CRL();