Reducing header overuse, non-inlining selected methods (CPPOST-35).
[shibboleth/cpp-sp.git] / shibsp / SPConfig.cpp
1
2 /*
3  *  Copyright 2001-2009 Internet2
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 /**
19  * SPConfig.cpp
20  *
21  * Library configuration
22  */
23
24 #include "internal.h"
25
26 #if defined(XMLTOOLING_LOG4SHIB)
27 # ifndef SHIBSP_LOG4SHIB
28 #  error "Logging library mismatch (XMLTooling is using log4shib)."
29 # endif
30 #elif defined(XMLTOOLING_LOG4CPP)
31 # ifndef SHIBSP_LOG4CPP
32 #  error "Logging library mismatch (XMLTooling is using log4cpp)."
33 # endif
34 #else
35 # error "No supported logging library."
36 #endif
37
38 #include "AccessControl.h"
39 #include "exceptions.h"
40 #include "RequestMapper.h"
41 #include "ServiceProvider.h"
42 #include "SessionCache.h"
43 #include "SPConfig.h"
44 #include "attribute/Attribute.h"
45 #include "handler/SessionInitiator.h"
46 #include "remoting/ListenerService.h"
47
48 #ifndef SHIBSP_LITE
49 # include "attribute/AttributeDecoder.h"
50 # include "attribute/filtering/AttributeFilter.h"
51 # include "attribute/filtering/MatchFunctor.h"
52 # include "attribute/resolver/AttributeExtractor.h"
53 # include "attribute/resolver/AttributeResolver.h"
54 # include "binding/ArtifactResolver.h"
55 # include "metadata/MetadataExt.h"
56 # include "security/PKIXTrustEngine.h"
57 # include <saml/SAMLConfig.h>
58 #endif
59
60 #include <ctime>
61 #include <xercesc/util/XMLUniDefs.hpp>
62 #include <xmltooling/XMLToolingConfig.h>
63 #include <xmltooling/util/NDC.h>
64 #include <xmltooling/util/PathResolver.h>
65 #include <xmltooling/util/TemplateEngine.h>
66 #include <xmltooling/util/XMLHelper.h>
67
68 using namespace shibsp;
69 using namespace opensaml;
70 using namespace xmltooling;
71 using namespace std;
72
73 DECL_XMLTOOLING_EXCEPTION_FACTORY(AttributeException,shibsp);
74 DECL_XMLTOOLING_EXCEPTION_FACTORY(AttributeExtractionException,shibsp);
75 DECL_XMLTOOLING_EXCEPTION_FACTORY(AttributeFilteringException,shibsp);
76 DECL_XMLTOOLING_EXCEPTION_FACTORY(AttributeResolutionException,shibsp);
77 DECL_XMLTOOLING_EXCEPTION_FACTORY(ConfigurationException,shibsp);
78 DECL_XMLTOOLING_EXCEPTION_FACTORY(ListenerException,shibsp);
79
80 #ifdef SHIBSP_LITE
81 DECL_XMLTOOLING_EXCEPTION_FACTORY(BindingException,opensaml);
82 DECL_XMLTOOLING_EXCEPTION_FACTORY(SecurityPolicyException,opensaml);
83 DECL_XMLTOOLING_EXCEPTION_FACTORY(ProfileException,opensaml);
84 DECL_XMLTOOLING_EXCEPTION_FACTORY(FatalProfileException,opensaml);
85 DECL_XMLTOOLING_EXCEPTION_FACTORY(RetryableProfileException,opensaml);
86 DECL_XMLTOOLING_EXCEPTION_FACTORY(MetadataException,opensaml::saml2md);
87 #endif
88
89 namespace shibsp {
90    SPConfig g_config;
91 }
92
93 SPConfig& SPConfig::getConfig()
94 {
95     return g_config;
96 }
97
98 void SPConfig::setServiceProvider(ServiceProvider* serviceProvider)
99 {
100     delete m_serviceProvider;
101     m_serviceProvider = serviceProvider;
102 }
103
104 bool SPConfig::init(const char* catalog_path, const char* inst_prefix)
105 {
106 #ifdef _DEBUG
107     NDC ndc("init");
108 #endif
109     if (!inst_prefix)
110         inst_prefix = getenv("SHIBSP_PREFIX");
111     if (!inst_prefix)
112         inst_prefix = SHIBSP_PREFIX;
113     std::string inst_prefix2;
114     while (*inst_prefix) {
115         inst_prefix2.push_back((*inst_prefix=='\\') ? ('/') : (*inst_prefix));
116         ++inst_prefix;
117     }
118
119     const char* loglevel=getenv("SHIBSP_LOGGING");
120     if (!loglevel)
121         loglevel = SHIBSP_LOGGING;
122     std::string ll(loglevel);
123     PathResolver localpr;
124     localpr.setDefaultPrefix(inst_prefix2.c_str());
125     inst_prefix = getenv("SHIBSP_CFGDIR");
126     if (!inst_prefix)
127         inst_prefix = SHIBSP_CFGDIR;
128     localpr.setCfgDir(inst_prefix);
129     XMLToolingConfig::getConfig().log_config(localpr.resolve(ll, PathResolver::XMLTOOLING_CFG_FILE, PACKAGE_NAME).c_str());
130
131     Category& log=Category::getInstance(SHIBSP_LOGCAT".Config");
132     log.debug("%s library initialization started", PACKAGE_STRING);
133
134     if (!catalog_path)
135         catalog_path = getenv("SHIBSP_SCHEMAS");
136     if (!catalog_path)
137         catalog_path = SHIBSP_SCHEMAS;
138     XMLToolingConfig::getConfig().catalog_path = catalog_path;
139
140 #ifndef SHIBSP_LITE
141     if (!SAMLConfig::getConfig().init()) {
142         log.fatal("failed to initialize OpenSAML library");
143         return false;
144     }
145 #else
146     if (!XMLToolingConfig::getConfig().init()) {
147         log.fatal("failed to initialize XMLTooling library");
148         return false;
149     }
150 #endif
151     PathResolver* pr = XMLToolingConfig::getConfig().getPathResolver();
152     pr->setDefaultPackageName(PACKAGE_NAME);
153     pr->setDefaultPrefix(inst_prefix2.c_str());
154     pr->setCfgDir(inst_prefix);
155     inst_prefix = getenv("SHIBSP_LIBDIR");
156     if (!inst_prefix)
157         inst_prefix = SHIBSP_LIBDIR;
158     pr->setLibDir(inst_prefix);
159     inst_prefix = getenv("SHIBSP_LOGDIR");
160     if (!inst_prefix)
161         inst_prefix = SHIBSP_LOGDIR;
162     pr->setLogDir(inst_prefix);
163     inst_prefix = getenv("SHIBSP_RUNDIR");
164     if (!inst_prefix)
165         inst_prefix = SHIBSP_RUNDIR;
166     pr->setRunDir(inst_prefix);
167     inst_prefix = getenv("SHIBSP_XMLDIR");
168     if (!inst_prefix)
169         inst_prefix = SHIBSP_XMLDIR;
170     pr->setXMLDir(inst_prefix);
171
172     XMLToolingConfig::getConfig().setTemplateEngine(new TemplateEngine());
173     XMLToolingConfig::getConfig().getTemplateEngine()->setTagPrefix("shibmlp");
174
175     REGISTER_XMLTOOLING_EXCEPTION_FACTORY(AttributeException,shibsp);
176     REGISTER_XMLTOOLING_EXCEPTION_FACTORY(AttributeExtractionException,shibsp);
177     REGISTER_XMLTOOLING_EXCEPTION_FACTORY(AttributeFilteringException,shibsp);
178     REGISTER_XMLTOOLING_EXCEPTION_FACTORY(AttributeResolutionException,shibsp);
179     REGISTER_XMLTOOLING_EXCEPTION_FACTORY(ConfigurationException,shibsp);
180     REGISTER_XMLTOOLING_EXCEPTION_FACTORY(ListenerException,shibsp);
181
182 #ifdef SHIBSP_LITE
183     REGISTER_XMLTOOLING_EXCEPTION_FACTORY(BindingException,opensaml);
184     REGISTER_XMLTOOLING_EXCEPTION_FACTORY(SecurityPolicyException,opensaml);
185     REGISTER_XMLTOOLING_EXCEPTION_FACTORY(ProfileException,opensaml);
186     REGISTER_XMLTOOLING_EXCEPTION_FACTORY(FatalProfileException,opensaml);
187     REGISTER_XMLTOOLING_EXCEPTION_FACTORY(RetryableProfileException,opensaml);
188     REGISTER_XMLTOOLING_EXCEPTION_FACTORY(MetadataException,opensaml::saml2md);
189 #endif
190
191 #ifndef SHIBSP_LITE
192     if (isEnabled(Metadata))
193         registerMetadataExtClasses();
194     if (isEnabled(Trust))
195         registerPKIXTrustEngine();
196 #endif
197
198     registerAttributeFactories();
199     registerHandlers();
200     registerSessionInitiators();
201     registerServiceProviders();
202
203 #ifndef SHIBSP_LITE
204     if (isEnabled(AttributeResolution)) {
205         registerAttributeExtractors();
206         registerAttributeDecoders();
207         registerAttributeResolvers();
208         registerAttributeFilters();
209         registerMatchFunctors();
210     }
211 #endif
212
213     if (isEnabled(Listener))
214         registerListenerServices();
215
216     if (isEnabled(RequestMapping)) {
217         registerAccessControls();
218         registerRequestMappers();
219     }
220
221     if (isEnabled(Caching))
222         registerSessionCaches();
223
224 #ifndef SHIBSP_LITE
225     if (isEnabled(OutOfProcess))
226         m_artifactResolver = new ArtifactResolver();
227 #endif
228     srand(static_cast<unsigned int>(std::time(NULL)));
229
230     log.info("%s library initialization complete", PACKAGE_STRING);
231     return true;
232 }
233
234 void SPConfig::term()
235 {
236 #ifdef _DEBUG
237     NDC ndc("term");
238 #endif
239     Category& log=Category::getInstance(SHIBSP_LOGCAT".Config");
240     log.info("%s library shutting down", PACKAGE_STRING);
241
242     setServiceProvider(NULL);
243     if (m_configDoc)
244         m_configDoc->release();
245     m_configDoc = NULL;
246 #ifndef SHIBSP_LITE
247     setArtifactResolver(NULL);
248 #endif
249
250     ArtifactResolutionServiceManager.deregisterFactories();
251     AssertionConsumerServiceManager.deregisterFactories();
252     LogoutInitiatorManager.deregisterFactories();
253     ManageNameIDServiceManager.deregisterFactories();
254     SessionInitiatorManager.deregisterFactories();
255     SingleLogoutServiceManager.deregisterFactories();
256     HandlerManager.deregisterFactories();
257     ServiceProviderManager.deregisterFactories();
258     Attribute::deregisterFactories();
259
260 #ifndef SHIBSP_LITE
261     if (isEnabled(AttributeResolution)) {
262         MatchFunctorManager.deregisterFactories();
263         AttributeFilterManager.deregisterFactories();
264         AttributeDecoderManager.deregisterFactories();
265         AttributeExtractorManager.deregisterFactories();
266         AttributeResolverManager.deregisterFactories();
267     }
268 #endif
269
270     if (isEnabled(Listener))
271         ListenerServiceManager.deregisterFactories();
272
273     if (isEnabled(RequestMapping)) {
274         AccessControlManager.deregisterFactories();
275         RequestMapperManager.deregisterFactories();
276     }
277
278     if (isEnabled(Caching))
279         SessionCacheManager.deregisterFactories();
280
281 #ifndef SHIBSP_LITE
282     SAMLConfig::getConfig().term();
283 #else
284     XMLToolingConfig::getConfig().term();
285 #endif
286     log.info("%s library shutdown complete", PACKAGE_STRING);
287 }
288
289 bool SPConfig::instantiate(const char* config, bool rethrow)
290 {
291 #ifdef _DEBUG
292     NDC ndc("instantiate");
293 #endif
294     if (!config)
295         config = getenv("SHIBSP_CONFIG");
296     if (!config)
297         config = SHIBSP_CONFIG;
298     try {
299         xercesc::DOMDocument* dummydoc;
300         if (*config == '"' || *config == '\'') {
301             throw ConfigurationException("The value of SHIBSP_CONFIG started with a quote.");
302         }
303         else if (*config != '<') {
304
305             // Mock up some XML.
306             string resolved(config);
307             stringstream snippet;
308             snippet
309                 << "<Dummy path='"
310                 << XMLToolingConfig::getConfig().getPathResolver()->resolve(resolved, PathResolver::XMLTOOLING_CFG_FILE)
311                 << "' validate='1'/>";
312             dummydoc = XMLToolingConfig::getConfig().getParser().parse(snippet);
313             XercesJanitor<xercesc::DOMDocument> docjanitor(dummydoc);
314             setServiceProvider(ServiceProviderManager.newPlugin(XML_SERVICE_PROVIDER, dummydoc->getDocumentElement()));
315             if (m_configDoc)
316                 m_configDoc->release();
317             m_configDoc = docjanitor.release();
318         }
319         else {
320             stringstream snippet(config);
321             dummydoc = XMLToolingConfig::getConfig().getParser().parse(snippet);
322             XercesJanitor<xercesc::DOMDocument> docjanitor(dummydoc);
323             static const XMLCh _type[] = UNICODE_LITERAL_4(t,y,p,e);
324             auto_ptr_char type(dummydoc->getDocumentElement()->getAttributeNS(NULL,_type));
325             if (type.get() && *type.get())
326                 setServiceProvider(ServiceProviderManager.newPlugin(type.get(), dummydoc->getDocumentElement()));
327             else
328                 throw ConfigurationException("The supplied XML bootstrapping configuration did not include a type attribute.");
329             if (m_configDoc)
330                 m_configDoc->release();
331             m_configDoc = docjanitor.release();
332         }
333
334         getServiceProvider()->init();
335         return true;
336     }
337     catch (exception& ex) {
338         if (rethrow)
339             throw;
340         Category::getInstance(SHIBSP_LOGCAT".Config").fatal("caught exception while loading configuration: %s", ex.what());
341     }
342     return false;
343 }