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