Starting to refactor session cache, eliminated IConfig class.
[shibboleth/cpp-sp.git] / shib-target / shib-config.cpp
1 /*
2  *  Copyright 2001-2005 Internet2
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /*
18  * shib-config.cpp -- ShibTarget initialization and finalization routines
19  *
20  * Created By:  Derek Atkins <derek@ihtfp.com>
21  *
22  * $Id$
23  */
24
25 #include "internal.h"
26 #include <shibsp/SPConfig.h>
27 #include <xmltooling/XMLToolingConfig.h>
28
29 #include <log4cpp/OstreamAppender.hh>
30
31 using namespace shibsp;
32 using namespace shibtarget;
33 using namespace shibboleth;
34 using namespace saml;
35 using namespace log4cpp;
36 using namespace std;
37
38 using xmltooling::XMLToolingConfig;
39 using xmltooling::PluginManager;
40
41 namespace {
42     STConfig g_Config;
43 }
44
45 // Factories for built-in plugins we can manufacture. Actual definitions
46 // will be with the actual object implementation.
47 #ifndef WIN32
48 PlugManager::Factory UnixListenerFactory;
49 #endif
50 PlugManager::Factory TCPListenerFactory;
51 //PlugManager::Factory MemoryListenerFactory;
52
53 PluginManager<SessionCache,const DOMElement*>::Factory MemoryCacheFactory;
54 PluginManager<Handler,const DOMElement*>::Factory ShibSessionInitiatorFactory;
55 PluginManager<Handler,const DOMElement*>::Factory SAML1POSTFactory;
56 PluginManager<Handler,const DOMElement*>::Factory SAML1ArtifactFactory;
57 PluginManager<Handler,const DOMElement*>::Factory ShibLogoutFactory;
58
59 ShibTargetConfig& ShibTargetConfig::getConfig()
60 {
61     return g_Config;
62 }
63
64 bool STConfig::init(const char* schemadir)
65 {
66 #ifdef _DEBUG
67     xmltooling::NDC ndc("init");
68 #endif
69     Category& log = Category::getInstance(SHIBT_LOGCAT".Config");
70
71     if (!schemadir) {
72         log.fatal("XML schema directory not supplied");
73         return false;
74     }
75
76     // This will cause some extra console logging, but for now,
77     // initialize the underlying libraries.
78     SAMLConfig& samlConf=SAMLConfig::getConfig();
79     if (schemadir)
80         samlConf.schema_dir = schemadir;
81     if (!samlConf.init()) {
82         log.fatal("failed to initialize OpenSAML1 library");
83         return false;
84     }
85
86     ShibConfig& shibConf=ShibConfig::getConfig();
87     if (!shibConf.init()) {
88         log.fatal("Failed to initialize Shib library");
89         samlConf.term();
90         return false;
91     }
92     
93     SPConfig& conf=SPConfig::getConfig();
94     if (!SPConfig::getConfig().init(NULL)) {
95         log.fatal("Failed to initialize SP library");
96         shibConf.term();
97         samlConf.term();
98         return false;
99     }
100
101     // Register built-in plugin types.
102     conf.ServiceProviderManager.registerFactory(XML_SERVICE_PROVIDER, XMLServiceProviderFactory);
103
104     conf.SessionInitiatorManager.registerFactory(shibspconstants::SHIB1_SESSIONINIT_PROFILE_URI,&ShibSessionInitiatorFactory);
105     conf.AssertionConsumerServiceManager.registerFactory(samlconstants::SAML1_PROFILE_BROWSER_POST,&SAML1POSTFactory);
106     conf.AssertionConsumerServiceManager.registerFactory(samlconstants::SAML1_PROFILE_BROWSER_ARTIFACT,&SAML1ArtifactFactory);
107     conf.SingleLogoutServiceManager.registerFactory(shibspconstants::SHIB1_LOGOUT_PROFILE_URI,&ShibLogoutFactory);
108     
109     conf.SessionCacheManager.registerFactory(MEMORY_SESSIONCACHE,&MemoryCacheFactory);
110     
111     log.info("finished initializing");
112     return true;
113 }
114
115 bool STConfig::load(const char* config)
116 {
117 #ifdef _DEBUG
118     xmltooling::NDC ndc("load");
119 #endif
120     Category& log = Category::getInstance(SHIBT_LOGCAT".Config");
121
122     if (!config) {
123         log.fatal("path to configuration file not supplied");
124         shutdown();
125         return false;
126     }
127
128     try {
129         log.info("loading configuration file: %s", config);
130         static const XMLCh path[] = UNICODE_LITERAL_4(p,a,t,h);
131         DOMImplementation* impl=DOMImplementationRegistry::getDOMImplementation(NULL);
132         DOMDocument* dummydoc=impl->createDocument();
133         xmltooling::XercesJanitor<DOMDocument> docjanitor(dummydoc);
134         DOMElement* dummy = dummydoc->createElementNS(NULL,path);
135
136         auto_ptr_XMLCh src(config);
137         dummy->setAttributeNS(NULL,path,src.get());
138
139         auto_ptr<ServiceProvider> sp(SPConfig::getConfig().ServiceProviderManager.newPlugin(XML_SERVICE_PROVIDER,dummy));
140         sp->init();
141         
142         pair<bool,unsigned int> skew=sp->getUnsignedInt("clockSkew");
143         SAMLConfig::getConfig().clock_skew_secs=skew.first ? skew.second : 180;
144         if (skew.first)
145             XMLToolingConfig::getConfig().clock_skew_secs=skew.second;
146         SPConfig::getConfig().setServiceProvider(sp.release());
147         
148         m_tranLog=new FixedContextCategory(SHIBTRAN_LOGCAT);
149         m_tranLog->info("opened transaction log");
150         m_tranLogLock = xmltooling::Mutex::create();
151     }
152     catch (SAMLException& ex) {
153         log.fatal("caught exception while loading/initializing configuration: %s",ex.what());
154         shutdown();
155         return false;
156     }
157 #ifndef _DEBUG
158     catch (...) {
159         log.fatal("caught exception while loading/initializing configuration");
160         shutdown();
161         return false;
162     }
163 #endif
164
165     log.info("finished loading configuration");
166     return true;
167 }
168
169 void STConfig::shutdown()
170 {
171 #ifdef _DEBUG
172     xmltooling::NDC ndc("shutdown");
173 #endif
174     Category& log = Category::getInstance(SHIBT_LOGCAT".Config");
175     log.info("shutting down the library");
176     delete m_tranLogLock;
177     m_tranLogLock = NULL;
178     //delete m_tranLog; // This is crashing for some reason, but we're shutting down anyway.
179     SPConfig::getConfig().term();
180     ShibConfig::getConfig().term();
181     SAMLConfig::getConfig().term();
182     log.info("library shutdown complete");
183 }