Factor entityID into SessionInitiator subinterface, move WAYF logic out of Shib handler.
[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 #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/AttributeDecoder.h"
32 #include "attribute/resolver/AttributeResolver.h"
33 #include "binding/ArtifactResolver.h"
34 #include "handler/SessionInitiator.h"
35 #include "metadata/MetadataExt.h"
36 #include "remoting/ListenerService.h"
37 #include "security/PKIXTrustEngine.h"
38
39 #include <log4cpp/Category.hh>
40 #include <saml/SAMLConfig.h>
41 #include <xmltooling/util/NDC.h>
42 #include <xmltooling/util/TemplateEngine.h>
43
44 using namespace shibsp;
45 using namespace opensaml;
46 using namespace xmltooling;
47 using namespace log4cpp;
48
49 DECL_XMLTOOLING_EXCEPTION_FACTORY(AttributeException,shibsp);
50 DECL_XMLTOOLING_EXCEPTION_FACTORY(AttributeResolutionException,shibsp);
51 DECL_XMLTOOLING_EXCEPTION_FACTORY(ConfigurationException,shibsp);
52 DECL_XMLTOOLING_EXCEPTION_FACTORY(ListenerException,shibsp);
53
54 namespace shibsp {
55    SPInternalConfig g_config;
56 }
57
58 SPConfig& SPConfig::getConfig()
59 {
60     return g_config;
61 }
62
63 SPInternalConfig& SPInternalConfig::getInternalConfig()
64 {
65     return g_config;
66 }
67
68 void SPConfig::setServiceProvider(ServiceProvider* serviceProvider)
69 {
70     delete m_serviceProvider;
71     m_serviceProvider = serviceProvider;
72 }
73
74 bool SPInternalConfig::init(const char* catalog_path)
75 {
76 #ifdef _DEBUG
77     NDC ndc("init");
78 #endif
79     Category& log=Category::getInstance(SHIBSP_LOGCAT".Config");
80     log.debug("library initialization started");
81
82     const char* loglevel=getenv("SHIBSP_LOGGING");
83     if (!loglevel)
84         loglevel = SHIBSP_LOGGING;
85     XMLToolingConfig::getConfig().log_config(loglevel);
86
87     if (!catalog_path)
88         catalog_path = getenv("SHIBSP_SCHEMAS");
89     if (!catalog_path)
90         catalog_path = SHIBSP_SCHEMAS;
91     XMLToolingConfig::getConfig().catalog_path = catalog_path;
92
93     if (!SAMLConfig::getConfig().init()) {
94         log.fatal("failed to initialize OpenSAML library");
95         return false;
96     }
97
98     XMLToolingConfig::getConfig().setTemplateEngine(new TemplateEngine());
99     XMLToolingConfig::getConfig().getTemplateEngine()->setTagPrefix("shibmlp");
100     
101     REGISTER_XMLTOOLING_EXCEPTION_FACTORY(AttributeException,shibsp);
102     REGISTER_XMLTOOLING_EXCEPTION_FACTORY(AttributeResolutionException,shibsp);
103     REGISTER_XMLTOOLING_EXCEPTION_FACTORY(ConfigurationException,shibsp);
104     REGISTER_XMLTOOLING_EXCEPTION_FACTORY(ListenerException,shibsp);
105     
106     registerMetadataExtClasses();
107     registerPKIXTrustEngine();
108
109     registerAccessControls();
110     registerAttributeDecoders();
111     registerAttributeFactories();
112     registerAttributeResolvers();
113     registerHandlers();
114     registerSessionInitiators();
115     registerListenerServices();
116     registerRequestMappers();
117     registerSessionCaches();
118     registerServiceProviders();
119
120     if (isEnabled(OutOfProcess))
121         m_artifactResolver = new ArtifactResolver();
122     
123     log.info("library initialization complete");
124     return true;
125 }
126
127 void SPInternalConfig::term()
128 {
129 #ifdef _DEBUG
130     NDC ndc("term");
131 #endif
132     Category& log=Category::getInstance(SHIBSP_LOGCAT".Config");
133     log.info("shutting down the library");
134
135     setServiceProvider(NULL);
136     setArtifactResolver(NULL);
137
138     AssertionConsumerServiceManager.deregisterFactories();
139     ManageNameIDServiceManager.deregisterFactories();
140     SessionInitiatorManager.deregisterFactories();
141     SingleLogoutServiceManager.deregisterFactories();
142     
143     ServiceProviderManager.deregisterFactories();
144     SessionCacheManager.deregisterFactories();
145     RequestMapperManager.deregisterFactories();
146     ListenerServiceManager.deregisterFactories();
147     HandlerManager.deregisterFactories();
148     AttributeResolverManager.deregisterFactories();
149     AttributeDecoderManager.deregisterFactories();
150     Attribute::deregisterFactories();
151     AccessControlManager.deregisterFactories();
152
153     SAMLConfig::getConfig().term();
154     log.info("library shutdown complete");
155 }