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/StorageService.h"
34 #include "util/XMLConstants.h"
35 #include "validation/Validator.h"
42 #include <log4cpp/Category.hh>
43 #include <log4cpp/PropertyConfigurator.hh>
44 #include <log4cpp/OstreamAppender.hh>
45 #include <xercesc/util/PlatformUtils.hpp>
46 #ifndef XMLTOOLING_NO_XMLSEC
47 #include <xsec/framework/XSECProvider.hpp>
48 #include <openssl/err.h>
51 using namespace soap11;
52 using namespace xmlencryption;
53 using namespace xmlsignature;
54 using namespace xmltooling;
55 using namespace log4cpp;
58 DECL_EXCEPTION_FACTORY(XMLParserException,xmltooling);
59 DECL_EXCEPTION_FACTORY(XMLObjectException,xmltooling);
60 DECL_EXCEPTION_FACTORY(MarshallingException,xmltooling);
61 DECL_EXCEPTION_FACTORY(UnmarshallingException,xmltooling);
62 DECL_EXCEPTION_FACTORY(UnknownElementException,xmltooling);
63 DECL_EXCEPTION_FACTORY(UnknownAttributeException,xmltooling);
64 DECL_EXCEPTION_FACTORY(UnknownExtensionException,xmltooling);
65 DECL_EXCEPTION_FACTORY(ValidationException,xmltooling);
66 DECL_EXCEPTION_FACTORY(XMLSecurityException,xmltooling);
67 DECL_EXCEPTION_FACTORY(IOException,xmltooling);
69 #ifndef XMLTOOLING_NO_XMLSEC
70 DECL_EXCEPTION_FACTORY(SignatureException,xmlsignature);
73 namespace xmltooling {
74 XMLToolingInternalConfig g_config;
77 XMLToolingConfig& XMLToolingConfig::getConfig()
82 XMLToolingInternalConfig& XMLToolingInternalConfig::getInternalConfig()
87 bool XMLToolingInternalConfig::log_config(const char* config)
90 if (!config || !*config)
91 config=getenv("XMLTOOLING_LOG_CONFIG");
92 if (!config || !*config)
96 Category& root = Category::getRoot();
97 if (!strcmp(config,"DEBUG")) {
98 root.setPriority(Priority::DEBUG);
101 else if (!strcmp(config,"INFO")) {
102 root.setPriority(Priority::INFO);
105 else if (!strcmp(config,"NOTICE")) {
106 root.setPriority(Priority::NOTICE);
109 else if (!strcmp(config,"WARN")) {
110 root.setPriority(Priority::WARN);
113 else if (!strcmp(config,"ERROR")) {
114 root.setPriority(Priority::ERROR);
117 else if (!strcmp(config,"CRIT")) {
118 root.setPriority(Priority::CRIT);
121 else if (!strcmp(config,"ALERT")) {
122 root.setPriority(Priority::ALERT);
125 else if (!strcmp(config,"EMERG")) {
126 root.setPriority(Priority::EMERG);
129 else if (!strcmp(config,"FATAL")) {
130 root.setPriority(Priority::FATAL);
134 root.setAppender(new OstreamAppender("default",&cerr));
136 PropertyConfigurator::configure(config);
138 catch (const ConfigureFailure& e) {
139 Category::getInstance(XMLTOOLING_LOGCAT".Logging").crit("failed to initialize log4cpp: %s", e.what());
146 bool XMLToolingInternalConfig::init()
149 xmltooling::NDC ndc("init");
151 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".XMLToolingConfig");
153 log.debug("library initialization started");
155 xercesc::XMLPlatformUtils::Initialize();
156 log.debug("Xerces initialization complete");
158 #ifndef XMLTOOLING_NO_XMLSEC
159 XSECPlatformUtils::Initialise();
160 m_xsecProvider=new XSECProvider();
161 log.debug("XMLSec initialization complete");
164 m_parserPool=new ParserPool();
165 m_validatingPool=new ParserPool(true,true);
166 m_lock=xercesc::XMLPlatformUtils::makeMutex();
168 // Load catalogs from path.
169 if (!catalog_path.empty()) {
170 char* catpath=strdup(catalog_path.c_str());
173 while (start && *start) {
174 sep=strchr(start,PATH_SEPARATOR_CHAR);
177 auto_ptr_XMLCh temp(start);
178 m_validatingPool->loadCatalog(temp.get());
179 start = sep ? sep + 1 : NULL;
184 // default registrations
185 XMLObjectBuilder::registerDefaultBuilder(new UnknownElementBuilder());
187 registerKeyInfoClasses();
188 registerEncryptionClasses();
189 registerSOAPClasses();
191 REGISTER_EXCEPTION_FACTORY(XMLParserException,xmltooling);
192 REGISTER_EXCEPTION_FACTORY(XMLObjectException,xmltooling);
193 REGISTER_EXCEPTION_FACTORY(MarshallingException,xmltooling);
194 REGISTER_EXCEPTION_FACTORY(UnmarshallingException,xmltooling);
195 REGISTER_EXCEPTION_FACTORY(UnknownElementException,xmltooling);
196 REGISTER_EXCEPTION_FACTORY(UnknownAttributeException,xmltooling);
197 REGISTER_EXCEPTION_FACTORY(ValidationException,xmltooling);
198 REGISTER_EXCEPTION_FACTORY(XMLSecurityException,xmltooling);
199 REGISTER_EXCEPTION_FACTORY(IOException,xmltooling);
201 #ifndef XMLTOOLING_NO_XMLSEC
202 XMLObjectBuilder::registerBuilder(QName(XMLConstants::XMLSIG_NS,Signature::LOCAL_NAME),new SignatureBuilder());
203 REGISTER_EXCEPTION_FACTORY(SignatureException,xmlsignature);
204 registerKeyResolvers();
205 registerCredentialResolvers();
206 registerTrustEngines();
208 registerStorageServices();
210 // Register xml:id as an ID attribute.
211 static const XMLCh xmlid[] = UNICODE_LITERAL_2(i,d);
212 AttributeExtensibleXMLObject::registerIDAttribute(QName(XMLConstants::XML_NS, xmlid));
214 catch (const xercesc::XMLException&) {
215 log.fatal("caught exception while initializing Xerces");
219 log.info("library initialization complete");
223 void XMLToolingInternalConfig::term()
225 SchemaValidators.destroyValidators();
226 XMLObjectBuilder::destroyBuilders();
227 XMLToolingException::deregisterFactories();
228 AttributeExtensibleXMLObject::deregisterIDAttributes();
230 #ifndef XMLTOOLING_NO_XMLSEC
231 TrustEngineManager.deregisterFactories();
232 CredentialResolverManager.deregisterFactories();
233 KeyResolverManager.deregisterFactories();
236 for (vector<void*>::reverse_iterator i=m_libhandles.rbegin(); i!=m_libhandles.rend(); i++) {
238 FARPROC fn=GetProcAddress(static_cast<HMODULE>(*i),"xmltooling_extension_term");
241 FreeLibrary(static_cast<HMODULE>(*i));
242 #elif defined(HAVE_DLFCN_H)
243 void (*fn)()=(void (*)())dlsym(*i,"xmltooling_extension_term");
248 # error "Don't know about dynamic loading on this platform!"
251 m_libhandles.clear();
255 delete m_validatingPool;
256 m_validatingPool=NULL;
258 #ifndef XMLTOOLING_NO_XMLSEC
259 delete m_xsecProvider;
261 XSECPlatformUtils::Terminate();
264 xercesc::XMLPlatformUtils::closeMutex(m_lock);
266 xercesc::XMLPlatformUtils::Terminate();
269 xmltooling::NDC ndc("term");
271 Category::getInstance(XMLTOOLING_LOGCAT".XMLToolingConfig").info("library shutdown complete");
274 Lockable* XMLToolingInternalConfig::lock()
276 xercesc::XMLPlatformUtils::lockMutex(m_lock);
280 void XMLToolingInternalConfig::unlock()
282 xercesc::XMLPlatformUtils::unlockMutex(m_lock);
285 bool XMLToolingInternalConfig::load_library(const char* path, void* context)
288 xmltooling::NDC ndc("LoadLibrary");
290 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".XMLToolingConfig");
291 log.info("loading extension: %s", path);
297 char* fixed=const_cast<char*>(path);
298 if (strchr(fixed,'/')) {
301 while (p=strchr(p,'/'))
305 UINT em=SetErrorMode(SEM_FAILCRITICALERRORS);
307 handle=LoadLibraryEx(fixed,NULL,LOAD_WITH_ALTERED_SEARCH_PATH);
309 handle=LoadLibraryEx(fixed,NULL,0);
311 throw runtime_error(string("unable to load extension library: ") + fixed);
312 FARPROC fn=GetProcAddress(handle,"xmltooling_extension_init");
314 throw runtime_error(string("unable to locate xmltooling_extension_init entry point: ") + fixed);
315 if (reinterpret_cast<int(*)(void*)>(fn)(context)!=0)
316 throw runtime_error(string("detected error in xmltooling_extension_init: ") + fixed);
321 catch(runtime_error& e) {
331 #elif defined(HAVE_DLFCN_H)
332 void* handle=dlopen(path,RTLD_LAZY);
334 throw runtime_error(string("unable to load extension library '") + path + "': " + dlerror());
335 int (*fn)(void*)=(int (*)(void*))(dlsym(handle,"xmltooling_extension_init"));
339 string("unable to locate xmltooling_extension_init entry point in '") + path + "': " +
340 (dlerror() ? dlerror() : "unknown error")
345 throw runtime_error(string("detected error in xmltooling_extension_init in ") + path);
347 catch(runtime_error& e) {
354 # error "Don't know about dynamic loading on this platform!"
356 m_libhandles.push_back(handle);
357 log.info("loaded extension: %s", path);
361 #ifndef XMLTOOLING_NO_XMLSEC
362 void xmltooling::log_openssl()
368 unsigned long code=ERR_get_error_line_data(&file,&line,&data,&flags);
370 Category& log=Category::getInstance("OpenSSL");
371 log.errorStream() << "error code: " << code << " in " << file << ", line " << line << CategoryStream::ENDLINE;
372 if (data && (flags & ERR_TXT_STRING))
373 log.errorStream() << "error data: " << data << CategoryStream::ENDLINE;
374 code=ERR_get_error_line_data(&file,&line,&data,&flags);
378 XSECCryptoX509CRL* XMLToolingInternalConfig::X509CRL() const
380 return new OpenSSLCryptoX509CRL();