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 // Load catalogs from path.
166 if (!catalog_path.empty()) {
167 char* catpath=strdup(catalog_path.c_str());
170 while (start && *start) {
171 sep=strchr(start,PATH_SEPARATOR_CHAR);
174 auto_ptr_XMLCh temp(start);
175 m_validatingPool->loadCatalog(temp.get());
176 start = sep ? sep + 1 : NULL;
181 // default registrations
182 XMLObjectBuilder::registerDefaultBuilder(new UnknownElementBuilder());
184 registerKeyInfoClasses();
185 registerEncryptionClasses();
186 registerSOAPClasses();
188 REGISTER_EXCEPTION_FACTORY(XMLParserException,xmltooling);
189 REGISTER_EXCEPTION_FACTORY(XMLObjectException,xmltooling);
190 REGISTER_EXCEPTION_FACTORY(MarshallingException,xmltooling);
191 REGISTER_EXCEPTION_FACTORY(UnmarshallingException,xmltooling);
192 REGISTER_EXCEPTION_FACTORY(UnknownElementException,xmltooling);
193 REGISTER_EXCEPTION_FACTORY(UnknownAttributeException,xmltooling);
194 REGISTER_EXCEPTION_FACTORY(ValidationException,xmltooling);
196 #ifndef XMLTOOLING_NO_XMLSEC
197 XMLObjectBuilder::registerBuilder(QName(XMLConstants::XMLSIG_NS,Signature::LOCAL_NAME),new SignatureBuilder());
198 REGISTER_EXCEPTION_FACTORY(SignatureException,xmlsignature);
199 registerKeyResolvers();
200 registerCredentialResolvers();
201 registerTrustEngines();
204 // Register xml:id as an ID attribute.
205 static const XMLCh xmlid[] = UNICODE_LITERAL_2(i,d);
206 AttributeExtensibleXMLObject::registerIDAttribute(QName(XMLConstants::XML_NS, xmlid));
208 catch (const xercesc::XMLException&) {
209 log.fatal("caught exception while initializing Xerces");
213 log.info("library initialization complete");
217 void XMLToolingInternalConfig::term()
219 XMLObjectBuilder::destroyBuilders();
220 KeyInfoSchemaValidators.destroyValidators();
221 EncryptionSchemaValidators.destroyValidators();
222 XMLToolingException::deregisterFactories();
223 AttributeExtensibleXMLObject::deregisterIDAttributes();
225 #ifndef XMLTOOLING_NO_XMLSEC
226 TrustEngineManager.deregisterFactories();
227 CredentialResolverManager.deregisterFactories();
228 KeyResolverManager.deregisterFactories();
231 for (vector<void*>::reverse_iterator i=m_libhandles.rbegin(); i!=m_libhandles.rend(); i++) {
233 FARPROC fn=GetProcAddress(static_cast<HMODULE>(*i),"xmltooling_extension_term");
236 FreeLibrary(static_cast<HMODULE>(*i));
237 #elif defined(HAVE_DLFCN_H)
238 void (*fn)()=(void (*)())dlsym(*i,"xmltooling_extension_term");
243 # error "Don't know about dynamic loading on this platform!"
246 m_libhandles.clear();
250 delete m_validatingPool;
251 m_validatingPool=NULL;
253 #ifndef XMLTOOLING_NO_XMLSEC
254 delete m_xsecProvider;
256 XSECPlatformUtils::Terminate();
259 xercesc::XMLPlatformUtils::closeMutex(m_lock);
261 xercesc::XMLPlatformUtils::Terminate();
264 xmltooling::NDC ndc("term");
266 Category::getInstance(XMLTOOLING_LOGCAT".XMLToolingConfig").info("library shutdown complete");
269 Lockable* XMLToolingInternalConfig::lock()
271 xercesc::XMLPlatformUtils::lockMutex(m_lock);
275 void XMLToolingInternalConfig::unlock()
277 xercesc::XMLPlatformUtils::unlockMutex(m_lock);
280 bool XMLToolingInternalConfig::load_library(const char* path, void* context)
283 xmltooling::NDC ndc("LoadLibrary");
285 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".XMLToolingConfig");
286 log.info("loading extension: %s", path);
292 char* fixed=const_cast<char*>(path);
293 if (strchr(fixed,'/')) {
296 while (p=strchr(p,'/'))
300 UINT em=SetErrorMode(SEM_FAILCRITICALERRORS);
302 handle=LoadLibraryEx(fixed,NULL,LOAD_WITH_ALTERED_SEARCH_PATH);
304 handle=LoadLibraryEx(fixed,NULL,0);
306 throw runtime_error(string("unable to load extension library: ") + fixed);
307 FARPROC fn=GetProcAddress(handle,"xmltooling_extension_init");
309 throw runtime_error(string("unable to locate xmltooling_extension_init entry point: ") + fixed);
310 if (reinterpret_cast<int(*)(void*)>(fn)(context)!=0)
311 throw runtime_error(string("detected error in xmltooling_extension_init: ") + fixed);
316 catch(runtime_error& e) {
326 #elif defined(HAVE_DLFCN_H)
327 void* handle=dlopen(path,RTLD_LAZY);
329 throw runtime_error(string("unable to load extension library '") + path + "': " + dlerror());
330 int (*fn)(void*)=(int (*)(void*))(dlsym(handle,"xmltooling_extension_init"));
334 string("unable to locate xmltooling_extension_init entry point in '") + path + "': " +
335 (dlerror() ? dlerror() : "unknown error")
340 throw runtime_error(string("detected error in xmltooling_extension_init in ") + path);
342 catch(runtime_error& e) {
349 # error "Don't know about dynamic loading on this platform!"
351 m_libhandles.push_back(handle);
352 log.info("loaded extension: %s", path);
356 #ifndef XMLTOOLING_NO_XMLSEC
357 void xmltooling::log_openssl()
363 unsigned long code=ERR_get_error_line_data(&file,&line,&data,&flags);
365 Category& log=Category::getInstance("OpenSSL");
366 log.errorStream() << "error code: " << code << " in " << file << ", line " << line << CategoryStream::ENDLINE;
367 if (data && (flags & ERR_TXT_STRING))
368 log.errorStream() << "error data: " << data << CategoryStream::ENDLINE;
369 code=ERR_get_error_line_data(&file,&line,&data,&flags);
373 XSECCryptoX509CRL* XMLToolingInternalConfig::X509CRL() const
375 return new OpenSSLCryptoX509CRL();