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"
32 #include "util/XMLConstants.h"
33 #include "validation/Validator.h"
40 #include <log4cpp/Category.hh>
41 #include <log4cpp/PropertyConfigurator.hh>
42 #include <log4cpp/OstreamAppender.hh>
43 #include <xercesc/util/PlatformUtils.hpp>
44 #ifndef XMLTOOLING_NO_XMLSEC
45 #include <xsec/framework/XSECProvider.hpp>
46 #include <openssl/err.h>
49 using namespace xmlencryption;
50 using namespace xmlsignature;
51 using namespace xmltooling;
52 using namespace log4cpp;
55 DECL_EXCEPTION_FACTORY(XMLParserException,xmltooling);
56 DECL_EXCEPTION_FACTORY(XMLObjectException,xmltooling);
57 DECL_EXCEPTION_FACTORY(MarshallingException,xmltooling);
58 DECL_EXCEPTION_FACTORY(UnmarshallingException,xmltooling);
59 DECL_EXCEPTION_FACTORY(UnknownElementException,xmltooling);
60 DECL_EXCEPTION_FACTORY(UnknownAttributeException,xmltooling);
61 DECL_EXCEPTION_FACTORY(UnknownExtensionException,xmltooling);
62 DECL_EXCEPTION_FACTORY(ValidationException,xmltooling);
64 #ifndef XMLTOOLING_NO_XMLSEC
65 DECL_EXCEPTION_FACTORY(SignatureException,xmlsignature);
68 namespace xmltooling {
69 XMLToolingInternalConfig g_config;
72 XMLToolingConfig& XMLToolingConfig::getConfig()
77 XMLToolingInternalConfig& XMLToolingInternalConfig::getInternalConfig()
82 bool XMLToolingInternalConfig::log_config(const char* config)
85 if (!config || !*config)
86 config=getenv("XMLTOOLING_LOG_CONFIG");
87 if (!config || !*config)
91 Category& root = Category::getRoot();
92 if (!strcmp(config,"DEBUG")) {
93 root.setPriority(Priority::DEBUG);
96 else if (!strcmp(config,"INFO")) {
97 root.setPriority(Priority::INFO);
100 else if (!strcmp(config,"NOTICE")) {
101 root.setPriority(Priority::NOTICE);
104 else if (!strcmp(config,"WARN")) {
105 root.setPriority(Priority::WARN);
108 else if (!strcmp(config,"ERROR")) {
109 root.setPriority(Priority::ERROR);
112 else if (!strcmp(config,"CRIT")) {
113 root.setPriority(Priority::CRIT);
116 else if (!strcmp(config,"ALERT")) {
117 root.setPriority(Priority::ALERT);
120 else if (!strcmp(config,"EMERG")) {
121 root.setPriority(Priority::EMERG);
124 else if (!strcmp(config,"FATAL")) {
125 root.setPriority(Priority::FATAL);
129 root.setAppender(new OstreamAppender("default",&cerr));
131 PropertyConfigurator::configure(config);
133 catch (const ConfigureFailure& e) {
134 Category::getInstance(XMLTOOLING_LOGCAT".Logging").crit("failed to initialize log4cpp: %s", e.what());
141 bool XMLToolingInternalConfig::init()
144 xmltooling::NDC ndc("init");
146 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".XMLToolingConfig");
148 log.debug("library initialization started");
150 xercesc::XMLPlatformUtils::Initialize();
151 log.debug("Xerces initialization complete");
153 #ifndef XMLTOOLING_NO_XMLSEC
154 XSECPlatformUtils::Initialise();
155 m_xsecProvider=new XSECProvider();
156 log.debug("XMLSec initialization complete");
159 m_parserPool=new ParserPool();
160 m_validatingPool=new ParserPool(true,true);
161 m_lock=xercesc::XMLPlatformUtils::makeMutex();
163 // default registrations
164 XMLObjectBuilder::registerDefaultBuilder(new UnknownElementBuilder());
166 registerKeyInfoClasses();
167 registerEncryptionClasses();
169 REGISTER_EXCEPTION_FACTORY(XMLParserException,xmltooling);
170 REGISTER_EXCEPTION_FACTORY(XMLObjectException,xmltooling);
171 REGISTER_EXCEPTION_FACTORY(MarshallingException,xmltooling);
172 REGISTER_EXCEPTION_FACTORY(UnmarshallingException,xmltooling);
173 REGISTER_EXCEPTION_FACTORY(UnknownElementException,xmltooling);
174 REGISTER_EXCEPTION_FACTORY(UnknownAttributeException,xmltooling);
175 REGISTER_EXCEPTION_FACTORY(ValidationException,xmltooling);
177 #ifndef XMLTOOLING_NO_XMLSEC
178 XMLObjectBuilder::registerBuilder(QName(XMLConstants::XMLSIG_NS,Signature::LOCAL_NAME),new SignatureBuilder());
179 REGISTER_EXCEPTION_FACTORY(SignatureException,xmlsignature);
180 registerKeyResolvers();
181 registerCredentialResolvers();
182 registerTrustEngines();
185 catch (const xercesc::XMLException&) {
186 log.fatal("caught exception while initializing Xerces");
190 log.info("library initialization complete");
194 void XMLToolingInternalConfig::term()
196 XMLObjectBuilder::destroyBuilders();
197 KeyInfoSchemaValidators.destroyValidators();
198 EncryptionSchemaValidators.destroyValidators();
199 XMLToolingException::deregisterFactories();
201 #ifndef XMLTOOLING_NO_XMLSEC
202 TrustEngineManager.deregisterFactories();
203 CredentialResolverManager.deregisterFactories();
204 KeyResolverManager.deregisterFactories();
207 for (vector<void*>::reverse_iterator i=m_libhandles.rbegin(); i!=m_libhandles.rend(); i++) {
209 FARPROC fn=GetProcAddress(static_cast<HMODULE>(*i),"xmltooling_extension_term");
212 FreeLibrary(static_cast<HMODULE>(*i));
213 #elif defined(HAVE_DLFCN_H)
214 void (*fn)()=(void (*)())dlsym(*i,"xmltooling_extension_term");
219 # error "Don't know about dynamic loading on this platform!"
222 m_libhandles.clear();
226 delete m_validatingPool;
227 m_validatingPool=NULL;
229 #ifndef XMLTOOLING_NO_XMLSEC
230 delete m_xsecProvider;
232 XSECPlatformUtils::Terminate();
235 xercesc::XMLPlatformUtils::closeMutex(m_lock);
237 xercesc::XMLPlatformUtils::Terminate();
240 xmltooling::NDC ndc("term");
242 Category::getInstance(XMLTOOLING_LOGCAT".XMLToolingConfig").info("library shutdown complete");
245 Lockable* XMLToolingInternalConfig::lock()
247 xercesc::XMLPlatformUtils::lockMutex(m_lock);
251 void XMLToolingInternalConfig::unlock()
253 xercesc::XMLPlatformUtils::unlockMutex(m_lock);
256 bool XMLToolingInternalConfig::load_library(const char* path, void* context)
259 xmltooling::NDC ndc("LoadLibrary");
261 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".XMLToolingConfig");
262 log.info("loading extension: %s", path);
268 char* fixed=const_cast<char*>(path);
269 if (strchr(fixed,'/')) {
272 while (p=strchr(p,'/'))
276 UINT em=SetErrorMode(SEM_FAILCRITICALERRORS);
278 handle=LoadLibraryEx(fixed,NULL,LOAD_WITH_ALTERED_SEARCH_PATH);
280 handle=LoadLibraryEx(fixed,NULL,0);
282 throw runtime_error(string("unable to load extension library: ") + fixed);
283 FARPROC fn=GetProcAddress(handle,"xmltooling_extension_init");
285 throw runtime_error(string("unable to locate xmltooling_extension_init entry point: ") + fixed);
286 if (reinterpret_cast<int(*)(void*)>(fn)(context)!=0)
287 throw runtime_error(string("detected error in xmltooling_extension_init: ") + fixed);
292 catch(runtime_error& e) {
302 #elif defined(HAVE_DLFCN_H)
303 void* handle=dlopen(path,RTLD_LAZY);
305 throw runtime_error(string("unable to load extension library '") + path + "': " + dlerror());
306 int (*fn)(void*)=(int (*)(void*))(dlsym(handle,"xmltooling_extension_init"));
310 string("unable to locate xmltooling_extension_init entry point in '") + path + "': " +
311 (dlerror() ? dlerror() : "unknown error")
316 throw runtime_error(string("detected error in xmltooling_extension_init in ") + path);
318 catch(runtime_error& e) {
325 # error "Don't know about dynamic loading on this platform!"
327 m_libhandles.push_back(handle);
328 log.info("loaded extension: %s", path);
332 #ifndef XMLTOOLING_NO_XMLSEC
333 void xmltooling::log_openssl()
339 unsigned long code=ERR_get_error_line_data(&file,&line,&data,&flags);
341 Category& log=Category::getInstance("OpenSSL");
342 log.errorStream() << "error code: " << code << " in " << file << ", line " << line << CategoryStream::ENDLINE;
343 if (data && (flags & ERR_TXT_STRING))
344 log.errorStream() << "error data: " << data << CategoryStream::ENDLINE;
345 code=ERR_get_error_line_data(&file,&line,&data,&flags);
349 XSECCryptoX509CRL* XMLToolingInternalConfig::X509CRL() const
351 return new OpenSSLCryptoX509CRL();