3df7a26164015c157fb992f491f2330fb8105e00
[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 #include "AccessControl.h"
26 #include "exceptions.h"
27 #include "RequestMapper.h"
28 #include "ServiceProvider.h"
29 #include "SessionCache.h"
30 #include "SPConfig.h"
31 #include "attribute/Attribute.h"
32 #include "handler/SessionInitiator.h"
33 #include "remoting/ListenerService.h"
34
35 #ifndef SHIBSP_LITE
36 # include "attribute/AttributeDecoder.h"
37 # include "attribute/filtering/AttributeFilter.h"
38 # include "attribute/filtering/MatchFunctor.h"
39 # include "attribute/resolver/AttributeExtractor.h"
40 # include "attribute/resolver/AttributeResolver.h"
41 # include "binding/ArtifactResolver.h"
42 # include "metadata/MetadataExt.h"
43 # include "security/PKIXTrustEngine.h"
44 # include <saml/SAMLConfig.h>
45 #else
46 # include <xmltooling/XMLToolingConfig.h>
47 #endif
48
49 #include <log4cpp/Category.hh>
50 #include <xmltooling/util/NDC.h>
51 #include <xmltooling/util/TemplateEngine.h>
52
53 using namespace shibsp;
54 using namespace opensaml;
55 using namespace xmltooling;
56 using namespace log4cpp;
57
58 DECL_XMLTOOLING_EXCEPTION_FACTORY(AttributeException,shibsp);
59 DECL_XMLTOOLING_EXCEPTION_FACTORY(AttributeExtractionException,shibsp);
60 DECL_XMLTOOLING_EXCEPTION_FACTORY(AttributeFilteringException,shibsp);
61 DECL_XMLTOOLING_EXCEPTION_FACTORY(AttributeResolutionException,shibsp);
62 DECL_XMLTOOLING_EXCEPTION_FACTORY(ConfigurationException,shibsp);
63 DECL_XMLTOOLING_EXCEPTION_FACTORY(ListenerException,shibsp);
64
65 #ifdef SHIBSP_LITE
66 DECL_XMLTOOLING_EXCEPTION_FACTORY(MetadataException,opensaml::saml2md);
67 DECL_XMLTOOLING_EXCEPTION_FACTORY(SecurityPolicyException,opensaml);
68 DECL_XMLTOOLING_EXCEPTION_FACTORY(ProfileException,opensaml);
69 DECL_XMLTOOLING_EXCEPTION_FACTORY(FatalProfileException,opensaml);
70 DECL_XMLTOOLING_EXCEPTION_FACTORY(RetryableProfileException,opensaml);
71 #endif
72
73 namespace shibsp {
74    SPInternalConfig g_config;
75 }
76
77 SPConfig& SPConfig::getConfig()
78 {
79     return g_config;
80 }
81
82 SPInternalConfig& SPInternalConfig::getInternalConfig()
83 {
84     return g_config;
85 }
86
87 void SPConfig::setServiceProvider(ServiceProvider* serviceProvider)
88 {
89     delete m_serviceProvider;
90     m_serviceProvider = serviceProvider;
91 }
92
93 bool SPInternalConfig::init(const char* catalog_path)
94 {
95 #ifdef _DEBUG
96     NDC ndc("init");
97 #endif
98     Category& log=Category::getInstance(SHIBSP_LOGCAT".Config");
99     log.debug("library initialization started");
100
101     const char* loglevel=getenv("SHIBSP_LOGGING");
102     if (!loglevel)
103         loglevel = SHIBSP_LOGGING;
104     XMLToolingConfig::getConfig().log_config(loglevel);
105
106     if (!catalog_path)
107         catalog_path = getenv("SHIBSP_SCHEMAS");
108     if (!catalog_path)
109         catalog_path = SHIBSP_SCHEMAS;
110     XMLToolingConfig::getConfig().catalog_path = catalog_path;
111
112 #ifndef SHIBSP_LITE
113     if (!SAMLConfig::getConfig().init()) {
114         log.fatal("failed to initialize OpenSAML library");
115         return false;
116     }
117 #else
118     if (!XMLToolingConfig::getConfig().init()) {
119         log.fatal("failed to initialize XMLTooling library");
120         return false;
121     }
122 #endif
123
124     XMLToolingConfig::getConfig().setTemplateEngine(new TemplateEngine());
125     XMLToolingConfig::getConfig().getTemplateEngine()->setTagPrefix("shibmlp");
126     
127     REGISTER_XMLTOOLING_EXCEPTION_FACTORY(AttributeException,shibsp);
128     REGISTER_XMLTOOLING_EXCEPTION_FACTORY(AttributeExtractionException,shibsp);
129     REGISTER_XMLTOOLING_EXCEPTION_FACTORY(AttributeFilteringException,shibsp);
130     REGISTER_XMLTOOLING_EXCEPTION_FACTORY(AttributeResolutionException,shibsp);
131     REGISTER_XMLTOOLING_EXCEPTION_FACTORY(ConfigurationException,shibsp);
132     REGISTER_XMLTOOLING_EXCEPTION_FACTORY(ListenerException,shibsp);
133
134 #ifdef SHIBSP_LITE
135     REGISTER_XMLTOOLING_EXCEPTION_FACTORY(SecurityPolicyException,opensaml);
136     REGISTER_XMLTOOLING_EXCEPTION_FACTORY(ProfileException,opensaml);
137     REGISTER_XMLTOOLING_EXCEPTION_FACTORY(FatalProfileException,opensaml);
138     REGISTER_XMLTOOLING_EXCEPTION_FACTORY(RetryableProfileException,opensaml);
139     REGISTER_XMLTOOLING_EXCEPTION_FACTORY(MetadataException,opensaml::saml2md);
140 #endif
141
142 #ifndef SHIBSP_LITE
143     if (isEnabled(Metadata))
144         registerMetadataExtClasses();
145     if (isEnabled(Trust))
146         registerPKIXTrustEngine();
147 #endif
148
149     registerAttributeFactories();
150     registerHandlers();
151     registerSessionInitiators();
152     registerServiceProviders();
153
154 #ifndef SHIBSP_LITE
155     if (isEnabled(AttributeResolution)) {
156         registerAttributeExtractors();
157         registerAttributeDecoders();
158         registerAttributeResolvers();
159         registerAttributeFilters();
160         registerMatchFunctors();
161     }
162 #endif
163
164     if (isEnabled(Listener))
165         registerListenerServices();
166
167     if (isEnabled(RequestMapping)) {
168         registerAccessControls();
169         registerRequestMappers();
170     }
171
172     if (isEnabled(Caching))
173         registerSessionCaches();
174
175 #ifndef SHIBSP_LITE
176     if (isEnabled(OutOfProcess))
177         m_artifactResolver = new ArtifactResolver();
178 #endif
179
180     log.info("library initialization complete");
181     return true;
182 }
183
184 void SPInternalConfig::term()
185 {
186 #ifdef _DEBUG
187     NDC ndc("term");
188 #endif
189     Category& log=Category::getInstance(SHIBSP_LOGCAT".Config");
190     log.info("shutting down the library");
191
192     setServiceProvider(NULL);
193 #ifndef SHIBSP_LITE
194     setArtifactResolver(NULL);
195 #endif
196
197     AssertionConsumerServiceManager.deregisterFactories();
198     ManageNameIDServiceManager.deregisterFactories();
199     SessionInitiatorManager.deregisterFactories();
200     SingleLogoutServiceManager.deregisterFactories();
201     HandlerManager.deregisterFactories();
202     ServiceProviderManager.deregisterFactories();
203     Attribute::deregisterFactories();
204
205 #ifndef SHIBSP_LITE
206     if (isEnabled(AttributeResolution)) {
207         MatchFunctorManager.deregisterFactories();
208         AttributeFilterManager.deregisterFactories();
209         AttributeDecoderManager.deregisterFactories();
210         AttributeExtractorManager.deregisterFactories();
211         AttributeResolverManager.deregisterFactories();
212     }
213 #endif
214
215     if (isEnabled(Listener))
216         ListenerServiceManager.deregisterFactories();
217
218     if (isEnabled(RequestMapping)) {
219         AccessControlManager.deregisterFactories();
220         RequestMapperManager.deregisterFactories();
221     }
222
223     if (isEnabled(Caching))
224         SessionCacheManager.deregisterFactories();
225
226 #ifndef SHIBSP_LITE
227     SAMLConfig::getConfig().term();
228 #else
229     XMLToolingConfig::getConfig().term();
230 #endif
231     log.info("library shutdown complete");
232 }