2 * Copyright 2001-2007 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/Encrypter.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"
32 #include "soap/SOAPTransport.h"
34 #include "util/ReplayCache.h"
35 #include "util/StorageService.h"
36 #include "util/TemplateEngine.h"
37 #include "util/XMLConstants.h"
38 #include "validation/ValidatorSuite.h"
45 #include <log4cpp/Category.hh>
46 #include <log4cpp/PropertyConfigurator.hh>
47 #include <log4cpp/OstreamAppender.hh>
48 #include <xercesc/util/PlatformUtils.hpp>
49 #ifndef XMLTOOLING_NO_XMLSEC
50 #include <xsec/framework/XSECProvider.hpp>
51 #include <openssl/err.h>
54 using namespace soap11;
55 using namespace xmlencryption;
56 using namespace xmlsignature;
57 using namespace xmltooling;
58 using namespace log4cpp;
61 DECL_XMLTOOLING_EXCEPTION_FACTORY(XMLParserException,xmltooling);
62 DECL_XMLTOOLING_EXCEPTION_FACTORY(XMLObjectException,xmltooling);
63 DECL_XMLTOOLING_EXCEPTION_FACTORY(MarshallingException,xmltooling);
64 DECL_XMLTOOLING_EXCEPTION_FACTORY(UnmarshallingException,xmltooling);
65 DECL_XMLTOOLING_EXCEPTION_FACTORY(UnknownElementException,xmltooling);
66 DECL_XMLTOOLING_EXCEPTION_FACTORY(UnknownAttributeException,xmltooling);
67 DECL_XMLTOOLING_EXCEPTION_FACTORY(UnknownExtensionException,xmltooling);
68 DECL_XMLTOOLING_EXCEPTION_FACTORY(ValidationException,xmltooling);
69 DECL_XMLTOOLING_EXCEPTION_FACTORY(IOException,xmltooling);
71 #ifndef XMLTOOLING_NO_XMLSEC
72 DECL_XMLTOOLING_EXCEPTION_FACTORY(XMLSecurityException,xmltooling);
73 DECL_XMLTOOLING_EXCEPTION_FACTORY(SignatureException,xmlsignature);
74 DECL_XMLTOOLING_EXCEPTION_FACTORY(EncryptionException,xmlencryption);
77 namespace xmltooling {
78 XMLToolingInternalConfig g_config;
81 XMLToolingConfig& XMLToolingConfig::getConfig()
86 XMLToolingInternalConfig& XMLToolingInternalConfig::getInternalConfig()
91 bool XMLToolingInternalConfig::log_config(const char* config)
94 if (!config || !*config)
95 config=getenv("XMLTOOLING_LOG_CONFIG");
96 if (!config || !*config)
100 Category& root = Category::getRoot();
101 if (!strcmp(config,"DEBUG")) {
102 root.setPriority(Priority::DEBUG);
105 else if (!strcmp(config,"INFO")) {
106 root.setPriority(Priority::INFO);
109 else if (!strcmp(config,"NOTICE")) {
110 root.setPriority(Priority::NOTICE);
113 else if (!strcmp(config,"WARN")) {
114 root.setPriority(Priority::WARN);
117 else if (!strcmp(config,"ERROR")) {
118 root.setPriority(Priority::ERROR);
121 else if (!strcmp(config,"CRIT")) {
122 root.setPriority(Priority::CRIT);
125 else if (!strcmp(config,"ALERT")) {
126 root.setPriority(Priority::ALERT);
129 else if (!strcmp(config,"EMERG")) {
130 root.setPriority(Priority::EMERG);
133 else if (!strcmp(config,"FATAL")) {
134 root.setPriority(Priority::FATAL);
138 root.setAppender(new OstreamAppender("default",&cerr));
140 PropertyConfigurator::configure(config);
142 catch (const ConfigureFailure& e) {
143 Category::getInstance(XMLTOOLING_LOGCAT".Logging").crit("failed to initialize log4cpp: %s", e.what());
150 void XMLToolingConfig::setReplayCache(ReplayCache* replayCache)
152 delete m_replayCache;
153 m_replayCache = replayCache;
156 void XMLToolingConfig::setTemplateEngine(TemplateEngine* templateEngine)
158 delete m_templateEngine;
159 m_templateEngine = templateEngine;
162 bool XMLToolingInternalConfig::init()
165 xmltooling::NDC ndc("init");
167 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".XMLToolingConfig");
169 log.debug("library initialization started");
171 xercesc::XMLPlatformUtils::Initialize();
172 log.debug("Xerces initialization complete");
174 #ifndef XMLTOOLING_NO_XMLSEC
175 XSECPlatformUtils::Initialise();
176 m_xsecProvider=new XSECProvider();
177 log.debug("XMLSec initialization complete");
180 m_parserPool=new ParserPool();
181 m_validatingPool=new ParserPool(true,true);
182 m_lock=xercesc::XMLPlatformUtils::makeMutex();
184 // Load catalogs from path.
185 if (!catalog_path.empty()) {
186 char* catpath=strdup(catalog_path.c_str());
189 while (start && *start) {
190 sep=strchr(start,PATH_SEPARATOR_CHAR);
193 auto_ptr_XMLCh temp(start);
194 m_validatingPool->loadCatalog(temp.get());
195 start = sep ? sep + 1 : NULL;
200 // default registrations
201 XMLObjectBuilder::registerDefaultBuilder(new UnknownElementBuilder());
203 registerKeyInfoClasses();
204 registerEncryptionClasses();
205 registerSOAPClasses();
207 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(XMLParserException,xmltooling);
208 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(XMLObjectException,xmltooling);
209 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(MarshallingException,xmltooling);
210 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(UnmarshallingException,xmltooling);
211 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(UnknownElementException,xmltooling);
212 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(UnknownAttributeException,xmltooling);
213 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(ValidationException,xmltooling);
214 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(IOException,xmltooling);
216 #ifndef XMLTOOLING_NO_XMLSEC
217 XMLObjectBuilder::registerBuilder(QName(xmlconstants::XMLSIG_NS,Signature::LOCAL_NAME),new SignatureBuilder());
218 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(XMLSecurityException,xmltooling);
219 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(SignatureException,xmlsignature);
220 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(EncryptionException,xmlencryption);
221 registerKeyResolvers();
222 registerCredentialResolvers();
223 registerTrustEngines();
225 registerSOAPTransports();
226 initSOAPTransports();
227 registerStorageServices();
229 // Register xml:id as an ID attribute.
230 static const XMLCh xmlid[] = UNICODE_LITERAL_2(i,d);
231 AttributeExtensibleXMLObject::registerIDAttribute(QName(xmlconstants::XML_NS, xmlid));
233 catch (const xercesc::XMLException&) {
234 log.fatal("caught exception while initializing Xerces");
238 log.info("library initialization complete");
242 void XMLToolingInternalConfig::term()
244 SchemaValidators.destroyValidators();
245 XMLObjectBuilder::destroyBuilders();
246 XMLToolingException::deregisterFactories();
247 AttributeExtensibleXMLObject::deregisterIDAttributes();
249 StorageServiceManager.deregisterFactories();
250 termSOAPTransports();
251 SOAPTransportManager.deregisterFactories();
252 #ifndef XMLTOOLING_NO_XMLSEC
253 TrustEngineManager.deregisterFactories();
254 CredentialResolverManager.deregisterFactories();
255 KeyResolverManager.deregisterFactories();
258 delete m_replayCache;
259 m_replayCache = NULL;
261 delete m_templateEngine;
262 m_templateEngine = NULL;
264 for (vector<void*>::reverse_iterator i=m_libhandles.rbegin(); i!=m_libhandles.rend(); i++) {
266 FARPROC fn=GetProcAddress(static_cast<HMODULE>(*i),"xmltooling_extension_term");
269 FreeLibrary(static_cast<HMODULE>(*i));
270 #elif defined(HAVE_DLFCN_H)
271 void (*fn)()=(void (*)())dlsym(*i,"xmltooling_extension_term");
276 # error "Don't know about dynamic loading on this platform!"
279 m_libhandles.clear();
283 delete m_validatingPool;
284 m_validatingPool=NULL;
286 #ifndef XMLTOOLING_NO_XMLSEC
287 delete m_xsecProvider;
289 XSECPlatformUtils::Terminate();
292 xercesc::XMLPlatformUtils::closeMutex(m_lock);
294 xercesc::XMLPlatformUtils::Terminate();
297 xmltooling::NDC ndc("term");
299 Category::getInstance(XMLTOOLING_LOGCAT".XMLToolingConfig").info("library shutdown complete");
302 Lockable* XMLToolingInternalConfig::lock()
304 xercesc::XMLPlatformUtils::lockMutex(m_lock);
308 void XMLToolingInternalConfig::unlock()
310 xercesc::XMLPlatformUtils::unlockMutex(m_lock);
313 bool XMLToolingInternalConfig::load_library(const char* path, void* context)
316 xmltooling::NDC ndc("LoadLibrary");
318 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".XMLToolingConfig");
319 log.info("loading extension: %s", path);
325 char* fixed=const_cast<char*>(path);
326 if (strchr(fixed,'/')) {
329 while (p=strchr(p,'/'))
333 UINT em=SetErrorMode(SEM_FAILCRITICALERRORS);
335 handle=LoadLibraryEx(fixed,NULL,LOAD_WITH_ALTERED_SEARCH_PATH);
337 handle=LoadLibraryEx(fixed,NULL,0);
339 throw runtime_error(string("unable to load extension library: ") + fixed);
340 FARPROC fn=GetProcAddress(handle,"xmltooling_extension_init");
342 throw runtime_error(string("unable to locate xmltooling_extension_init entry point: ") + fixed);
343 if (reinterpret_cast<int(*)(void*)>(fn)(context)!=0)
344 throw runtime_error(string("detected error in xmltooling_extension_init: ") + fixed);
349 catch(runtime_error& e) {
359 #elif defined(HAVE_DLFCN_H)
360 void* handle=dlopen(path,RTLD_LAZY);
362 throw runtime_error(string("unable to load extension library '") + path + "': " + dlerror());
363 int (*fn)(void*)=(int (*)(void*))(dlsym(handle,"xmltooling_extension_init"));
367 string("unable to locate xmltooling_extension_init entry point in '") + path + "': " +
368 (dlerror() ? dlerror() : "unknown error")
373 throw runtime_error(string("detected error in xmltooling_extension_init in ") + path);
375 catch(runtime_error& e) {
382 # error "Don't know about dynamic loading on this platform!"
384 m_libhandles.push_back(handle);
385 log.info("loaded extension: %s", path);
389 #ifndef XMLTOOLING_NO_XMLSEC
390 void xmltooling::log_openssl()
396 unsigned long code=ERR_get_error_line_data(&file,&line,&data,&flags);
398 Category& log=Category::getInstance("OpenSSL");
399 log.errorStream() << "error code: " << code << " in " << file << ", line " << line << CategoryStream::ENDLINE;
400 if (data && (flags & ERR_TXT_STRING))
401 log.errorStream() << "error data: " << data << CategoryStream::ENDLINE;
402 code=ERR_get_error_line_data(&file,&line,&data,&flags);
406 XSECCryptoX509CRL* XMLToolingInternalConfig::X509CRL() const
408 return new OpenSSLCryptoX509CRL();