Fixes for disabling xmlsec
[shibboleth/cpp-xmltooling.git] / xmltooling / XMLToolingConfig.cpp
1 /*
2  *  Copyright 2001-2006 Internet2
3  * 
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 /**
18  * XMLToolingConfig.cpp
19  * 
20  * Library configuration 
21  */
22
23 #include "internal.h"
24 #include "XMLToolingConfig.h"
25 #include "impl/UnknownElement.h"
26 #include "signature/impl/XMLSecSignature.h"
27 #include "util/NDC.h"
28
29 #ifdef HAVE_DLFCN_H
30 # include <dlfcn.h>
31 #endif
32
33 #include <log4cpp/Category.hh>
34 #include <log4cpp/PropertyConfigurator.hh>
35 #include <log4cpp/OstreamAppender.hh>
36 #include <xercesc/util/PlatformUtils.hpp>
37 #ifndef XMLTOOLING_NO_XMLSEC
38     #include <xsec/framework/XSECProvider.hpp>
39 #endif
40
41 #include <stdexcept>
42
43 using namespace log4cpp;
44 using namespace xmltooling;
45 using namespace std;
46
47 namespace {
48    XMLToolingInternalConfig g_config;
49 }
50
51 XMLToolingConfig& XMLToolingConfig::getConfig()
52 {
53     return g_config;
54 }
55
56 XMLToolingInternalConfig& XMLToolingInternalConfig::getInternalConfig()
57 {
58     return g_config;
59 }
60
61 bool XMLToolingInternalConfig::log_config(const char* config)
62 {
63     try {
64         if (!config || !*config)
65             config=getenv("XMLTOOLING_LOG_CONFIG");
66         if (!config || !*config)
67             config="WARN";
68         
69         bool level=false;
70         Category& root = Category::getRoot();
71         if (!strcmp(config,"DEBUG")) {
72             root.setPriority(Priority::DEBUG);
73             level=true;
74         }
75         else if (!strcmp(config,"INFO")) {
76             root.setPriority(Priority::INFO);
77             level=true;
78         }
79         else if (!strcmp(config,"NOTICE")) {
80             root.setPriority(Priority::NOTICE);
81             level=true;
82         }
83         else if (!strcmp(config,"WARN")) {
84             root.setPriority(Priority::WARN);
85             level=true;
86         }
87         else if (!strcmp(config,"ERROR")) {
88             root.setPriority(Priority::ERROR);
89             level=true;
90         }
91         else if (!strcmp(config,"CRIT")) {
92             root.setPriority(Priority::CRIT);
93             level=true;
94         }
95         else if (!strcmp(config,"ALERT")) {
96             root.setPriority(Priority::ALERT);
97             level=true;
98         }
99         else if (!strcmp(config,"EMERG")) {
100             root.setPriority(Priority::EMERG);
101             level=true;
102         }
103         else if (!strcmp(config,"FATAL")) {
104             root.setPriority(Priority::FATAL);
105             level=true;
106         }
107         if (level)
108             root.setAppender(new OstreamAppender("default",&cerr));
109         else
110             PropertyConfigurator::configure(config);
111     }
112     catch (const ConfigureFailure& e) {
113         Category::getInstance(XMLTOOLING_LOGCAT".Logging").crit("failed to initialize log4cpp: %s", e.what());
114         return false;
115     }
116     
117     return true;
118 }
119
120 bool XMLToolingInternalConfig::init()
121 {
122 #ifdef _DEBUG
123     xmltooling::NDC ndc("init");
124 #endif
125     Category& log=Category::getInstance(XMLTOOLING_LOGCAT".XMLToolingConfig");
126     try {
127         log.debug("library initialization started");
128
129         xercesc::XMLPlatformUtils::Initialize();
130         log.debug("Xerces initialization complete");
131
132 #ifndef XMLTOOLING_NO_XMLSEC
133         XSECPlatformUtils::Initialise();
134         m_xsecProvider=new XSECProvider();
135         log.debug("XMLSec initialization complete");
136 #endif
137
138         m_parserPool=new ParserPool();
139         m_lock=xercesc::XMLPlatformUtils::makeMutex();
140
141         // default registrations
142         XMLObjectBuilder::registerDefaultBuilder(new UnknownElementBuilder());
143         Marshaller::registerDefaultMarshaller(new UnknownElementMarshaller());
144         Unmarshaller::registerDefaultUnmarshaller(new UnknownElementUnmarshaller());
145 #ifndef XMLTOOLING_NO_XMLSEC
146         QName dsig(XMLConstants::XMLSIG_NS,Signature::LOCAL_NAME);
147         XMLObjectBuilder::registerBuilder(dsig,new XMLSecSignatureBuilder());
148         Marshaller::registerMarshaller(dsig,new XMLSecSignatureMarshaller());
149         Unmarshaller::registerUnmarshaller(dsig,new XMLSecSignatureUnmarshaller());
150 #endif
151     }
152     catch (const xercesc::XMLException&) {
153         log.fatal("caught exception while initializing Xerces");
154         return false;
155     }
156
157     log.info("library initialization complete");
158     return true;
159 }
160
161 void XMLToolingInternalConfig::term()
162 {
163     XMLObjectBuilder::destroyBuilders();
164     Marshaller::destroyMarshallers();
165     Unmarshaller::destroyUnmarshallers();
166
167     for (vector<void*>::reverse_iterator i=m_libhandles.rbegin(); i!=m_libhandles.rend(); i++) {
168 #if defined(WIN32)
169         FARPROC fn=GetProcAddress(static_cast<HMODULE>(*i),"xmltooling_extension_term");
170         if (fn)
171             fn();
172         FreeLibrary(static_cast<HMODULE>(*i));
173 #elif defined(HAVE_DLFCN_H)
174         void (*fn)()=(void (*)())dlsym(*i,"xmltooling_extension_term");
175         if (fn)
176             fn();
177         dlclose(*i);
178 #else
179 # error "Don't know about dynamic loading on this platform!"
180 #endif
181     }
182     m_libhandles.clear();
183     
184     delete m_parserPool;
185     m_parserPool=NULL;
186
187 #ifndef XMLTOOLING_NO_XMLSEC
188     delete m_xsecProvider;
189     m_xsecProvider=NULL;
190     XSECPlatformUtils::Terminate();
191 #endif
192
193     xercesc::XMLPlatformUtils::closeMutex(m_lock);
194     m_lock=NULL;
195     xercesc::XMLPlatformUtils::Terminate();
196
197  #ifdef _DEBUG
198     xmltooling::NDC ndc("term");
199 #endif
200    Category::getInstance(XMLTOOLING_LOGCAT".XMLToolingConfig").info("library shutdown complete");
201 }
202
203 ILockable& XMLToolingInternalConfig::lock()
204 {
205     xercesc::XMLPlatformUtils::lockMutex(m_lock);
206     return *this;
207 }
208
209 void XMLToolingInternalConfig::unlock()
210 {
211     xercesc::XMLPlatformUtils::unlockMutex(m_lock);
212 }
213
214 bool XMLToolingInternalConfig::load_library(const char* path, void* context)
215 {
216 #ifdef _DEBUG
217     xmltooling::NDC ndc("LoadLibrary");
218 #endif
219     Category& log=Category::getInstance(XMLTOOLING_LOGCAT".XMLToolingConfig");
220     log.info("loading extension: %s", path);
221
222     Locker locker(this);
223
224 #if defined(WIN32)
225     HMODULE handle=NULL;
226     char* fixed=const_cast<char*>(path);
227     if (strchr(fixed,'/')) {
228         fixed=strdup(path);
229         char* p=fixed;
230         while (p=strchr(p,'/'))
231             *p='\\';
232     }
233
234     UINT em=SetErrorMode(SEM_FAILCRITICALERRORS);
235     try {
236         handle=LoadLibraryEx(fixed,NULL,LOAD_WITH_ALTERED_SEARCH_PATH);
237         if (!handle)
238              handle=LoadLibraryEx(fixed,NULL,0);
239         if (!handle)
240             throw runtime_error(string("unable to load extension library: ") + fixed);
241         FARPROC fn=GetProcAddress(handle,"xmltooling_extension_init");
242         if (!fn)
243             throw runtime_error(string("unable to locate xmltooling_extension_init entry point: ") + fixed);
244         if (reinterpret_cast<int(*)(void*)>(fn)(context)!=0)
245             throw runtime_error(string("detected error in xmltooling_extension_init: ") + fixed);
246         if (fixed!=path)
247             free(fixed);
248         SetErrorMode(em);
249     }
250     catch(runtime_error& e) {
251         log.error(e.what());
252         if (handle)
253             FreeLibrary(handle);
254         SetErrorMode(em);
255         if (fixed!=path)
256             free(fixed);
257         return false;
258     }
259
260 #elif defined(HAVE_DLFCN_H)
261     void* handle=dlopen(path,RTLD_LAZY);
262     if (!handle)
263         throw runtime_error(string("unable to load extension library '") + path + "': " + dlerror());
264     int (*fn)(void*)=(int (*)(void*))(dlsym(handle,"xmltooling_extension_init"));
265     if (!fn) {
266         dlclose(handle);
267         throw runtime_error(
268             string("unable to locate xmltooling_extension_init entry point in '") + path + "': " +
269                 (dlerror() ? dlerror() : "unknown error")
270             );
271     }
272     try {
273         if (fn(context)!=0)
274             throw runtime_error(string("detected error in xmltooling_extension_init in ") + path);
275     }
276     catch(runtime_error& e) {
277         log.error(e.what());
278         if (handle)
279             dlclose(handle);
280         return false;
281     }
282 #else
283 # error "Don't know about dynamic loading on this platform!"
284 #endif
285     m_libhandles.push_back(handle);
286     log.info("loaded extension: %s", path);
287     return true;
288 }