7aaaf9800ff65696117988729d31d0f5dfd97f3d
[shibboleth/sp.git] / shibsp / SPConfig.cpp
1
2 /*
3  *  Copyright 2001-2007 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 # include <xmltooling/util/CurlNetAccessor.hpp>
59 #else
60 # include <xmltooling/XMLToolingConfig.h>
61 #endif
62
63 #include <ctime>
64 #include <xercesc/util/XMLUniDefs.hpp>
65 #include <xmltooling/util/NDC.h>
66 #include <xmltooling/util/PathResolver.h>
67 #include <xmltooling/util/TemplateEngine.h>
68 #include <xmltooling/util/XMLHelper.h>
69
70 using namespace shibsp;
71 using namespace opensaml;
72 using namespace xmltooling;
73 using namespace std;
74
75 DECL_XMLTOOLING_EXCEPTION_FACTORY(AttributeException,shibsp);
76 DECL_XMLTOOLING_EXCEPTION_FACTORY(AttributeExtractionException,shibsp);
77 DECL_XMLTOOLING_EXCEPTION_FACTORY(AttributeFilteringException,shibsp);
78 DECL_XMLTOOLING_EXCEPTION_FACTORY(AttributeResolutionException,shibsp);
79 DECL_XMLTOOLING_EXCEPTION_FACTORY(ConfigurationException,shibsp);
80 DECL_XMLTOOLING_EXCEPTION_FACTORY(ListenerException,shibsp);
81
82 #ifdef SHIBSP_LITE
83 DECL_XMLTOOLING_EXCEPTION_FACTORY(BindingException,opensaml);
84 DECL_XMLTOOLING_EXCEPTION_FACTORY(SecurityPolicyException,opensaml);
85 DECL_XMLTOOLING_EXCEPTION_FACTORY(ProfileException,opensaml);
86 DECL_XMLTOOLING_EXCEPTION_FACTORY(FatalProfileException,opensaml);
87 DECL_XMLTOOLING_EXCEPTION_FACTORY(RetryableProfileException,opensaml);
88 DECL_XMLTOOLING_EXCEPTION_FACTORY(MetadataException,opensaml::saml2md);
89 #endif
90
91 namespace shibsp {
92    SPConfig g_config;
93 }
94
95 SPConfig& SPConfig::getConfig()
96 {
97     return g_config;
98 }
99
100 void SPConfig::setServiceProvider(ServiceProvider* serviceProvider)
101 {
102     delete m_serviceProvider;
103     m_serviceProvider = serviceProvider;
104 }
105
106 bool SPConfig::init(const char* catalog_path, const char* inst_prefix)
107 {
108 #ifdef _DEBUG
109     NDC ndc("init");
110 #endif
111     if (!inst_prefix)
112         inst_prefix = getenv("SHIBSP_PREFIX");
113     if (!inst_prefix)
114         inst_prefix = SHIBSP_PREFIX;
115     std::string inst_prefix2;
116     while (*inst_prefix) {
117         inst_prefix2.push_back((*inst_prefix=='\\') ? ('/') : (*inst_prefix));
118         ++inst_prefix;
119     }
120
121     const char* loglevel=getenv("SHIBSP_LOGGING");
122     if (!loglevel)
123         loglevel = SHIBSP_LOGGING;
124     std::string ll(loglevel);
125     PathResolver localpr;
126     localpr.setDefaultPrefix(inst_prefix2.c_str());
127     XMLToolingConfig::getConfig().log_config(localpr.resolve(ll, PathResolver::XMLTOOLING_CFG_FILE, PACKAGE_NAME).c_str());
128
129     Category& log=Category::getInstance(SHIBSP_LOGCAT".Config");
130     log.debug("%s library initialization started", PACKAGE_STRING);
131
132     if (!catalog_path)
133         catalog_path = getenv("SHIBSP_SCHEMAS");
134     if (!catalog_path)
135         catalog_path = SHIBSP_SCHEMAS;
136     XMLToolingConfig::getConfig().catalog_path = catalog_path;
137
138 #ifndef SHIBSP_LITE
139     if (!SAMLConfig::getConfig().init()) {
140         log.fatal("failed to initialize OpenSAML library");
141         return false;
142     }
143     XMLPlatformUtils::fgNetAccessor = new CurlNetAccessor();
144 #else
145     if (!XMLToolingConfig::getConfig().init()) {
146         log.fatal("failed to initialize XMLTooling library");
147         return false;
148     }
149 #endif
150     XMLToolingConfig::getConfig().getPathResolver()->setDefaultPackageName(PACKAGE_NAME);
151     XMLToolingConfig::getConfig().getPathResolver()->setDefaultPrefix(inst_prefix2.c_str());
152     XMLToolingConfig::getConfig().setTemplateEngine(new TemplateEngine());
153     XMLToolingConfig::getConfig().getTemplateEngine()->setTagPrefix("shibmlp");
154
155     REGISTER_XMLTOOLING_EXCEPTION_FACTORY(AttributeException,shibsp);
156     REGISTER_XMLTOOLING_EXCEPTION_FACTORY(AttributeExtractionException,shibsp);
157     REGISTER_XMLTOOLING_EXCEPTION_FACTORY(AttributeFilteringException,shibsp);
158     REGISTER_XMLTOOLING_EXCEPTION_FACTORY(AttributeResolutionException,shibsp);
159     REGISTER_XMLTOOLING_EXCEPTION_FACTORY(ConfigurationException,shibsp);
160     REGISTER_XMLTOOLING_EXCEPTION_FACTORY(ListenerException,shibsp);
161
162 #ifdef SHIBSP_LITE
163     REGISTER_XMLTOOLING_EXCEPTION_FACTORY(BindingException,opensaml);
164     REGISTER_XMLTOOLING_EXCEPTION_FACTORY(SecurityPolicyException,opensaml);
165     REGISTER_XMLTOOLING_EXCEPTION_FACTORY(ProfileException,opensaml);
166     REGISTER_XMLTOOLING_EXCEPTION_FACTORY(FatalProfileException,opensaml);
167     REGISTER_XMLTOOLING_EXCEPTION_FACTORY(RetryableProfileException,opensaml);
168     REGISTER_XMLTOOLING_EXCEPTION_FACTORY(MetadataException,opensaml::saml2md);
169 #endif
170
171 #ifndef SHIBSP_LITE
172     if (isEnabled(Metadata))
173         registerMetadataExtClasses();
174     if (isEnabled(Trust))
175         registerPKIXTrustEngine();
176 #endif
177
178     registerAttributeFactories();
179     registerHandlers();
180     registerSessionInitiators();
181     registerServiceProviders();
182
183 #ifndef SHIBSP_LITE
184     if (isEnabled(AttributeResolution)) {
185         registerAttributeExtractors();
186         registerAttributeDecoders();
187         registerAttributeResolvers();
188         registerAttributeFilters();
189         registerMatchFunctors();
190     }
191 #endif
192
193     if (isEnabled(Listener))
194         registerListenerServices();
195
196     if (isEnabled(RequestMapping)) {
197         registerAccessControls();
198         registerRequestMappers();
199     }
200
201     if (isEnabled(Caching))
202         registerSessionCaches();
203
204 #ifndef SHIBSP_LITE
205     if (isEnabled(OutOfProcess))
206         m_artifactResolver = new ArtifactResolver();
207 #endif
208     srand(static_cast<unsigned int>(std::time(NULL)));
209
210     log.info("%s library initialization complete", PACKAGE_STRING);
211     return true;
212 }
213
214 void SPConfig::term()
215 {
216 #ifdef _DEBUG
217     NDC ndc("term");
218 #endif
219     Category& log=Category::getInstance(SHIBSP_LOGCAT".Config");
220     log.info("%s library shutting down", PACKAGE_STRING);
221
222     setServiceProvider(NULL);
223 #ifndef SHIBSP_LITE
224     setArtifactResolver(NULL);
225 #endif
226
227     ArtifactResolutionServiceManager.deregisterFactories();
228     AssertionConsumerServiceManager.deregisterFactories();
229     LogoutInitiatorManager.deregisterFactories();
230     ManageNameIDServiceManager.deregisterFactories();
231     SessionInitiatorManager.deregisterFactories();
232     SingleLogoutServiceManager.deregisterFactories();
233     HandlerManager.deregisterFactories();
234     ServiceProviderManager.deregisterFactories();
235     Attribute::deregisterFactories();
236
237 #ifndef SHIBSP_LITE
238     if (isEnabled(AttributeResolution)) {
239         MatchFunctorManager.deregisterFactories();
240         AttributeFilterManager.deregisterFactories();
241         AttributeDecoderManager.deregisterFactories();
242         AttributeExtractorManager.deregisterFactories();
243         AttributeResolverManager.deregisterFactories();
244     }
245 #endif
246
247     if (isEnabled(Listener))
248         ListenerServiceManager.deregisterFactories();
249
250     if (isEnabled(RequestMapping)) {
251         AccessControlManager.deregisterFactories();
252         RequestMapperManager.deregisterFactories();
253     }
254
255     if (isEnabled(Caching))
256         SessionCacheManager.deregisterFactories();
257
258 #ifndef SHIBSP_LITE
259     SAMLConfig::getConfig().term();
260 #else
261     XMLToolingConfig::getConfig().term();
262 #endif
263     log.info("%s library shutdown complete", PACKAGE_STRING);
264 }
265
266 bool SPConfig::instantiate(const char* config, bool rethrow)
267 {
268 #ifdef _DEBUG
269     NDC ndc("instantiate");
270 #endif
271     if (!config)
272         config = getenv("SHIBSP_CONFIG");
273     if (!config)
274         config = SHIBSP_CONFIG;
275     try {
276         xercesc::DOMDocument* dummydoc;
277         if (*config == '"' || *config == '\'') {
278             throw ConfigurationException("The value of SHIBSP_CONFIG started with a quote.");
279         }
280         else if (*config != '<') {
281
282             // Mock up some XML.
283             string resolved(config);
284             stringstream snippet;
285             snippet
286                 << "<Dummy path='"
287                 << XMLToolingConfig::getConfig().getPathResolver()->resolve(resolved, PathResolver::XMLTOOLING_CFG_FILE)
288                 << "' validate='1'/>";
289             dummydoc = XMLToolingConfig::getConfig().getParser().parse(snippet);
290             XercesJanitor<xercesc::DOMDocument> docjanitor(dummydoc);
291             setServiceProvider(ServiceProviderManager.newPlugin(XML_SERVICE_PROVIDER, dummydoc->getDocumentElement()));
292         }
293         else {
294             stringstream snippet(config);
295             dummydoc = XMLToolingConfig::getConfig().getParser().parse(snippet);
296             XercesJanitor<xercesc::DOMDocument> docjanitor(dummydoc);
297             static const XMLCh _type[] = UNICODE_LITERAL_4(t,y,p,e);
298             auto_ptr_char type(dummydoc->getDocumentElement()->getAttributeNS(NULL,_type));
299             if (type.get() && *type.get())
300                 setServiceProvider(ServiceProviderManager.newPlugin(type.get(), dummydoc->getDocumentElement()));
301             else
302                 throw ConfigurationException("The supplied XML bootstrapping configuration did not include a type attribute.");
303         }
304
305         getServiceProvider()->init();
306         return true;
307     }
308     catch (exception& ex) {
309         if (rethrow)
310             throw;
311         Category::getInstance(SHIBSP_LOGCAT".Config").fatal("caught exception while loading configuration: %s", ex.what());
312     }
313     return false;
314 }