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/ReplayCache.h"
34 #include "util/StorageService.h"
35 #include "util/XMLConstants.h"
36 #include "validation/ValidatorSuite.h"
43 #include <log4cpp/Category.hh>
44 #include <log4cpp/PropertyConfigurator.hh>
45 #include <log4cpp/OstreamAppender.hh>
46 #include <xercesc/util/PlatformUtils.hpp>
47 #ifndef XMLTOOLING_NO_XMLSEC
48 #include <xsec/framework/XSECProvider.hpp>
49 #include <openssl/err.h>
52 using namespace soap11;
53 using namespace xmlencryption;
54 using namespace xmlsignature;
55 using namespace xmltooling;
56 using namespace log4cpp;
59 DECL_EXCEPTION_FACTORY(XMLParserException,xmltooling);
60 DECL_EXCEPTION_FACTORY(XMLObjectException,xmltooling);
61 DECL_EXCEPTION_FACTORY(MarshallingException,xmltooling);
62 DECL_EXCEPTION_FACTORY(UnmarshallingException,xmltooling);
63 DECL_EXCEPTION_FACTORY(UnknownElementException,xmltooling);
64 DECL_EXCEPTION_FACTORY(UnknownAttributeException,xmltooling);
65 DECL_EXCEPTION_FACTORY(UnknownExtensionException,xmltooling);
66 DECL_EXCEPTION_FACTORY(ValidationException,xmltooling);
67 DECL_EXCEPTION_FACTORY(XMLSecurityException,xmltooling);
68 DECL_EXCEPTION_FACTORY(IOException,xmltooling);
70 #ifndef XMLTOOLING_NO_XMLSEC
71 DECL_EXCEPTION_FACTORY(SignatureException,xmlsignature);
74 namespace xmltooling {
75 XMLToolingInternalConfig g_config;
78 XMLToolingConfig& XMLToolingConfig::getConfig()
83 XMLToolingInternalConfig& XMLToolingInternalConfig::getInternalConfig()
88 bool XMLToolingInternalConfig::log_config(const char* config)
91 if (!config || !*config)
92 config=getenv("XMLTOOLING_LOG_CONFIG");
93 if (!config || !*config)
97 Category& root = Category::getRoot();
98 if (!strcmp(config,"DEBUG")) {
99 root.setPriority(Priority::DEBUG);
102 else if (!strcmp(config,"INFO")) {
103 root.setPriority(Priority::INFO);
106 else if (!strcmp(config,"NOTICE")) {
107 root.setPriority(Priority::NOTICE);
110 else if (!strcmp(config,"WARN")) {
111 root.setPriority(Priority::WARN);
114 else if (!strcmp(config,"ERROR")) {
115 root.setPriority(Priority::ERROR);
118 else if (!strcmp(config,"CRIT")) {
119 root.setPriority(Priority::CRIT);
122 else if (!strcmp(config,"ALERT")) {
123 root.setPriority(Priority::ALERT);
126 else if (!strcmp(config,"EMERG")) {
127 root.setPriority(Priority::EMERG);
130 else if (!strcmp(config,"FATAL")) {
131 root.setPriority(Priority::FATAL);
135 root.setAppender(new OstreamAppender("default",&cerr));
137 PropertyConfigurator::configure(config);
139 catch (const ConfigureFailure& e) {
140 Category::getInstance(XMLTOOLING_LOGCAT".Logging").crit("failed to initialize log4cpp: %s", e.what());
147 void XMLToolingConfig::setReplayCache(ReplayCache* replayCache)
149 delete m_replayCache;
150 m_replayCache = replayCache;
153 bool XMLToolingInternalConfig::init()
156 xmltooling::NDC ndc("init");
158 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".XMLToolingConfig");
160 log.debug("library initialization started");
162 xercesc::XMLPlatformUtils::Initialize();
163 log.debug("Xerces initialization complete");
165 #ifndef XMLTOOLING_NO_XMLSEC
166 XSECPlatformUtils::Initialise();
167 m_xsecProvider=new XSECProvider();
168 log.debug("XMLSec initialization complete");
171 m_parserPool=new ParserPool();
172 m_validatingPool=new ParserPool(true,true);
173 m_lock=xercesc::XMLPlatformUtils::makeMutex();
175 // Load catalogs from path.
176 if (!catalog_path.empty()) {
177 char* catpath=strdup(catalog_path.c_str());
180 while (start && *start) {
181 sep=strchr(start,PATH_SEPARATOR_CHAR);
184 auto_ptr_XMLCh temp(start);
185 m_validatingPool->loadCatalog(temp.get());
186 start = sep ? sep + 1 : NULL;
191 // default registrations
192 XMLObjectBuilder::registerDefaultBuilder(new UnknownElementBuilder());
194 registerKeyInfoClasses();
195 registerEncryptionClasses();
196 registerSOAPClasses();
198 REGISTER_EXCEPTION_FACTORY(XMLParserException,xmltooling);
199 REGISTER_EXCEPTION_FACTORY(XMLObjectException,xmltooling);
200 REGISTER_EXCEPTION_FACTORY(MarshallingException,xmltooling);
201 REGISTER_EXCEPTION_FACTORY(UnmarshallingException,xmltooling);
202 REGISTER_EXCEPTION_FACTORY(UnknownElementException,xmltooling);
203 REGISTER_EXCEPTION_FACTORY(UnknownAttributeException,xmltooling);
204 REGISTER_EXCEPTION_FACTORY(ValidationException,xmltooling);
205 REGISTER_EXCEPTION_FACTORY(XMLSecurityException,xmltooling);
206 REGISTER_EXCEPTION_FACTORY(IOException,xmltooling);
208 #ifndef XMLTOOLING_NO_XMLSEC
209 XMLObjectBuilder::registerBuilder(QName(XMLConstants::XMLSIG_NS,Signature::LOCAL_NAME),new SignatureBuilder());
210 REGISTER_EXCEPTION_FACTORY(SignatureException,xmlsignature);
211 registerKeyResolvers();
212 registerCredentialResolvers();
213 registerTrustEngines();
215 registerStorageServices();
217 // Register xml:id as an ID attribute.
218 static const XMLCh xmlid[] = UNICODE_LITERAL_2(i,d);
219 AttributeExtensibleXMLObject::registerIDAttribute(QName(XMLConstants::XML_NS, xmlid));
221 catch (const xercesc::XMLException&) {
222 log.fatal("caught exception while initializing Xerces");
226 log.info("library initialization complete");
230 void XMLToolingInternalConfig::term()
232 SchemaValidators.destroyValidators();
233 XMLObjectBuilder::destroyBuilders();
234 XMLToolingException::deregisterFactories();
235 AttributeExtensibleXMLObject::deregisterIDAttributes();
237 #ifndef XMLTOOLING_NO_XMLSEC
238 TrustEngineManager.deregisterFactories();
239 CredentialResolverManager.deregisterFactories();
240 KeyResolverManager.deregisterFactories();
243 delete m_replayCache;
244 m_replayCache = NULL;
246 for (vector<void*>::reverse_iterator i=m_libhandles.rbegin(); i!=m_libhandles.rend(); i++) {
248 FARPROC fn=GetProcAddress(static_cast<HMODULE>(*i),"xmltooling_extension_term");
251 FreeLibrary(static_cast<HMODULE>(*i));
252 #elif defined(HAVE_DLFCN_H)
253 void (*fn)()=(void (*)())dlsym(*i,"xmltooling_extension_term");
258 # error "Don't know about dynamic loading on this platform!"
261 m_libhandles.clear();
265 delete m_validatingPool;
266 m_validatingPool=NULL;
268 #ifndef XMLTOOLING_NO_XMLSEC
269 delete m_xsecProvider;
271 XSECPlatformUtils::Terminate();
274 xercesc::XMLPlatformUtils::closeMutex(m_lock);
276 xercesc::XMLPlatformUtils::Terminate();
279 xmltooling::NDC ndc("term");
281 Category::getInstance(XMLTOOLING_LOGCAT".XMLToolingConfig").info("library shutdown complete");
284 Lockable* XMLToolingInternalConfig::lock()
286 xercesc::XMLPlatformUtils::lockMutex(m_lock);
290 void XMLToolingInternalConfig::unlock()
292 xercesc::XMLPlatformUtils::unlockMutex(m_lock);
295 bool XMLToolingInternalConfig::load_library(const char* path, void* context)
298 xmltooling::NDC ndc("LoadLibrary");
300 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".XMLToolingConfig");
301 log.info("loading extension: %s", path);
307 char* fixed=const_cast<char*>(path);
308 if (strchr(fixed,'/')) {
311 while (p=strchr(p,'/'))
315 UINT em=SetErrorMode(SEM_FAILCRITICALERRORS);
317 handle=LoadLibraryEx(fixed,NULL,LOAD_WITH_ALTERED_SEARCH_PATH);
319 handle=LoadLibraryEx(fixed,NULL,0);
321 throw runtime_error(string("unable to load extension library: ") + fixed);
322 FARPROC fn=GetProcAddress(handle,"xmltooling_extension_init");
324 throw runtime_error(string("unable to locate xmltooling_extension_init entry point: ") + fixed);
325 if (reinterpret_cast<int(*)(void*)>(fn)(context)!=0)
326 throw runtime_error(string("detected error in xmltooling_extension_init: ") + fixed);
331 catch(runtime_error& e) {
341 #elif defined(HAVE_DLFCN_H)
342 void* handle=dlopen(path,RTLD_LAZY);
344 throw runtime_error(string("unable to load extension library '") + path + "': " + dlerror());
345 int (*fn)(void*)=(int (*)(void*))(dlsym(handle,"xmltooling_extension_init"));
349 string("unable to locate xmltooling_extension_init entry point in '") + path + "': " +
350 (dlerror() ? dlerror() : "unknown error")
355 throw runtime_error(string("detected error in xmltooling_extension_init in ") + path);
357 catch(runtime_error& e) {
364 # error "Don't know about dynamic loading on this platform!"
366 m_libhandles.push_back(handle);
367 log.info("loaded extension: %s", path);
371 #ifndef XMLTOOLING_NO_XMLSEC
372 void xmltooling::log_openssl()
378 unsigned long code=ERR_get_error_line_data(&file,&line,&data,&flags);
380 Category& log=Category::getInstance("OpenSSL");
381 log.errorStream() << "error code: " << code << " in " << file << ", line " << line << CategoryStream::ENDLINE;
382 if (data && (flags & ERR_TXT_STRING))
383 log.errorStream() << "error data: " << data << CategoryStream::ENDLINE;
384 code=ERR_get_error_line_data(&file,&line,&data,&flags);
388 XSECCryptoX509CRL* XMLToolingInternalConfig::X509CRL() const
390 return new OpenSSLCryptoX509CRL();