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