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