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