772c4040c48c66e0c5fb15057a75eaf6e9db7631
[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 <xmltooling/util/NDC.h>
64 #include <xmltooling/util/PathResolver.h>
65 #include <xmltooling/util/TemplateEngine.h>
66
67 using namespace shibsp;
68 using namespace opensaml;
69 using namespace xmltooling;
70
71 DECL_XMLTOOLING_EXCEPTION_FACTORY(AttributeException,shibsp);
72 DECL_XMLTOOLING_EXCEPTION_FACTORY(AttributeExtractionException,shibsp);
73 DECL_XMLTOOLING_EXCEPTION_FACTORY(AttributeFilteringException,shibsp);
74 DECL_XMLTOOLING_EXCEPTION_FACTORY(AttributeResolutionException,shibsp);
75 DECL_XMLTOOLING_EXCEPTION_FACTORY(ConfigurationException,shibsp);
76 DECL_XMLTOOLING_EXCEPTION_FACTORY(ListenerException,shibsp);
77
78 #ifdef SHIBSP_LITE
79 DECL_XMLTOOLING_EXCEPTION_FACTORY(BindingException,opensaml);
80 DECL_XMLTOOLING_EXCEPTION_FACTORY(SecurityPolicyException,opensaml);
81 DECL_XMLTOOLING_EXCEPTION_FACTORY(ProfileException,opensaml);
82 DECL_XMLTOOLING_EXCEPTION_FACTORY(FatalProfileException,opensaml);
83 DECL_XMLTOOLING_EXCEPTION_FACTORY(RetryableProfileException,opensaml);
84 DECL_XMLTOOLING_EXCEPTION_FACTORY(MetadataException,opensaml::saml2md);
85 #endif
86
87 namespace shibsp {
88    SPConfig g_config;
89 }
90
91 SPConfig& SPConfig::getConfig()
92 {
93     return g_config;
94 }
95
96 void SPConfig::setServiceProvider(ServiceProvider* serviceProvider)
97 {
98     delete m_serviceProvider;
99     m_serviceProvider = serviceProvider;
100 }
101
102 bool SPConfig::init(const char* catalog_path, const char* inst_prefix)
103 {
104 #ifdef _DEBUG
105     NDC ndc("init");
106 #endif
107     Category& log=Category::getInstance(SHIBSP_LOGCAT".Config");
108     log.debug("%s library initialization started", PACKAGE_STRING);
109
110     const char* loglevel=getenv("SHIBSP_LOGGING");
111     if (!loglevel)
112         loglevel = SHIBSP_LOGGING;
113     XMLToolingConfig::getConfig().log_config(loglevel);
114
115     if (!inst_prefix)
116         inst_prefix = getenv("SHIBSP_PREFIX");
117     if (!inst_prefix)
118         inst_prefix = SHIBSP_PREFIX;
119     
120     if (!catalog_path)
121         catalog_path = getenv("SHIBSP_SCHEMAS");
122     if (!catalog_path)
123         catalog_path = SHIBSP_SCHEMAS;
124     XMLToolingConfig::getConfig().catalog_path = catalog_path;
125
126 #ifndef SHIBSP_LITE
127     if (!SAMLConfig::getConfig().init()) {
128         log.fatal("failed to initialize OpenSAML library");
129         return false;
130     }
131     XMLPlatformUtils::fgNetAccessor = new CurlNetAccessor();
132 #else
133     if (!XMLToolingConfig::getConfig().init()) {
134         log.fatal("failed to initialize XMLTooling library");
135         return false;
136     }
137 #endif    
138     XMLToolingConfig::getConfig().getPathResolver()->setDefaultPackageName(PACKAGE_NAME);
139     XMLToolingConfig::getConfig().getPathResolver()->setDefaultPrefix(inst_prefix);
140     XMLToolingConfig::getConfig().setTemplateEngine(new TemplateEngine());
141     XMLToolingConfig::getConfig().getTemplateEngine()->setTagPrefix("shibmlp");
142     
143     REGISTER_XMLTOOLING_EXCEPTION_FACTORY(AttributeException,shibsp);
144     REGISTER_XMLTOOLING_EXCEPTION_FACTORY(AttributeExtractionException,shibsp);
145     REGISTER_XMLTOOLING_EXCEPTION_FACTORY(AttributeFilteringException,shibsp);
146     REGISTER_XMLTOOLING_EXCEPTION_FACTORY(AttributeResolutionException,shibsp);
147     REGISTER_XMLTOOLING_EXCEPTION_FACTORY(ConfigurationException,shibsp);
148     REGISTER_XMLTOOLING_EXCEPTION_FACTORY(ListenerException,shibsp);
149
150 #ifdef SHIBSP_LITE
151     REGISTER_XMLTOOLING_EXCEPTION_FACTORY(BindingException,opensaml);
152     REGISTER_XMLTOOLING_EXCEPTION_FACTORY(SecurityPolicyException,opensaml);
153     REGISTER_XMLTOOLING_EXCEPTION_FACTORY(ProfileException,opensaml);
154     REGISTER_XMLTOOLING_EXCEPTION_FACTORY(FatalProfileException,opensaml);
155     REGISTER_XMLTOOLING_EXCEPTION_FACTORY(RetryableProfileException,opensaml);
156     REGISTER_XMLTOOLING_EXCEPTION_FACTORY(MetadataException,opensaml::saml2md);
157 #endif
158
159 #ifndef SHIBSP_LITE
160     if (isEnabled(Metadata))
161         registerMetadataExtClasses();
162     if (isEnabled(Trust))
163         registerPKIXTrustEngine();
164 #endif
165
166     registerAttributeFactories();
167     registerHandlers();
168     registerSessionInitiators();
169     registerServiceProviders();
170
171 #ifndef SHIBSP_LITE
172     if (isEnabled(AttributeResolution)) {
173         registerAttributeExtractors();
174         registerAttributeDecoders();
175         registerAttributeResolvers();
176         registerAttributeFilters();
177         registerMatchFunctors();
178     }
179 #endif
180
181     if (isEnabled(Listener))
182         registerListenerServices();
183
184     if (isEnabled(RequestMapping)) {
185         registerAccessControls();
186         registerRequestMappers();
187     }
188
189     if (isEnabled(Caching))
190         registerSessionCaches();
191
192 #ifndef SHIBSP_LITE
193     if (isEnabled(OutOfProcess))
194         m_artifactResolver = new ArtifactResolver();
195 #endif
196
197     log.info("%s library initialization complete", PACKAGE_STRING);
198     return true;
199 }
200
201 void SPConfig::term()
202 {
203 #ifdef _DEBUG
204     NDC ndc("term");
205 #endif
206     Category& log=Category::getInstance(SHIBSP_LOGCAT".Config");
207     log.info("%s library shutting down", PACKAGE_STRING);
208
209     setServiceProvider(NULL);
210 #ifndef SHIBSP_LITE
211     setArtifactResolver(NULL);
212 #endif
213
214     ArtifactResolutionServiceManager.deregisterFactories();
215     AssertionConsumerServiceManager.deregisterFactories();
216     LogoutInitiatorManager.deregisterFactories();
217     ManageNameIDServiceManager.deregisterFactories();
218     SessionInitiatorManager.deregisterFactories();
219     SingleLogoutServiceManager.deregisterFactories();
220     HandlerManager.deregisterFactories();
221     ServiceProviderManager.deregisterFactories();
222     Attribute::deregisterFactories();
223
224 #ifndef SHIBSP_LITE
225     if (isEnabled(AttributeResolution)) {
226         MatchFunctorManager.deregisterFactories();
227         AttributeFilterManager.deregisterFactories();
228         AttributeDecoderManager.deregisterFactories();
229         AttributeExtractorManager.deregisterFactories();
230         AttributeResolverManager.deregisterFactories();
231     }
232 #endif
233
234     if (isEnabled(Listener))
235         ListenerServiceManager.deregisterFactories();
236
237     if (isEnabled(RequestMapping)) {
238         AccessControlManager.deregisterFactories();
239         RequestMapperManager.deregisterFactories();
240     }
241
242     if (isEnabled(Caching))
243         SessionCacheManager.deregisterFactories();
244
245 #ifndef SHIBSP_LITE
246     SAMLConfig::getConfig().term();
247 #else
248     XMLToolingConfig::getConfig().term();
249 #endif
250     log.info("%s library shutdown complete", PACKAGE_STRING);
251 }