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