Shell of new SP object interface to replace old IConfig layer.
[shibboleth/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 <shibsp/SPConstants.h>
28 #include <xmltooling/XMLToolingConfig.h>
29
30 #include <log4cpp/OstreamAppender.hh>
31
32 using namespace shibsp;
33 using namespace shibtarget;
34 using namespace shibboleth;
35 using namespace saml;
36 using namespace log4cpp;
37 using namespace std;
38
39 using xmltooling::XMLToolingConfig;
40 using xmltooling::PluginManager;
41
42 namespace {
43     STConfig g_Config;
44 }
45
46 // Factories for built-in plugins we can manufacture. Actual definitions
47 // will be with the actual object implementation.
48 #ifndef WIN32
49 PlugManager::Factory UnixListenerFactory;
50 #endif
51 PlugManager::Factory TCPListenerFactory;
52 //PlugManager::Factory MemoryListenerFactory;
53 PlugManager::Factory MemoryCacheFactory;
54 PlugManager::Factory XMLRequestMapFactory;
55 PlugManager::Factory ShibSessionInitiatorFactory;
56 PlugManager::Factory SAML1POSTFactory;
57 PlugManager::Factory SAML1ArtifactFactory;
58 PlugManager::Factory ShibLogoutFactory;
59 //PlugManager::Factory htaccessFactory;
60
61 ShibTargetConfig& ShibTargetConfig::getConfig()
62 {
63     return g_Config;
64 }
65
66 bool STConfig::init(const char* schemadir)
67 {
68 #ifdef _DEBUG
69     xmltooling::NDC ndc("init");
70 #endif
71     Category& log = Category::getInstance(SHIBT_LOGCAT".Config");
72
73     if (!schemadir) {
74         log.fatal("XML schema directory not supplied");
75         return false;
76     }
77
78     // This will cause some extra console logging, but for now,
79     // initialize the underlying libraries.
80     SAMLConfig& samlConf=SAMLConfig::getConfig();
81     if (schemadir)
82         samlConf.schema_dir = schemadir;
83     if (!samlConf.init()) {
84         log.fatal("failed to initialize OpenSAML1 library");
85         return false;
86     }
87
88     ShibConfig& shibConf=ShibConfig::getConfig();
89     if (!shibConf.init()) {
90         log.fatal("Failed to initialize Shib library");
91         samlConf.term();
92         return false;
93     }
94     
95     if (!SPConfig::getConfig().init(NULL)) {
96         log.fatal("Failed to initialize SP library");
97         shibConf.term();
98         samlConf.term();
99         return false;
100     }
101
102     // Register built-in plugin types.
103     SPConfig::getConfig().ServiceProviderManager.registerFactory(XML_SERVICE_PROVIDER, XMLServiceProviderFactory);
104
105     samlConf.getPlugMgr().regFactory(MEMORY_SESSIONCACHE,&MemoryCacheFactory);
106     samlConf.getPlugMgr().regFactory(LEGACY_REQUESTMAP_PROVIDER,&XMLRequestMapFactory);
107     samlConf.getPlugMgr().regFactory(XML_REQUESTMAP_PROVIDER,&XMLRequestMapFactory);
108     samlConf.getPlugMgr().regFactory(NATIVE_REQUESTMAP_PROVIDER,&XMLRequestMapFactory);
109     
110     auto_ptr_char temp1(shibspconstants::SHIB1_SESSIONINIT_PROFILE_URI);
111     samlConf.getPlugMgr().regFactory(temp1.get(),&ShibSessionInitiatorFactory);
112     samlConf.getPlugMgr().regFactory(samlconstants::SAML1_PROFILE_BROWSER_POST,&SAML1POSTFactory);
113     samlConf.getPlugMgr().regFactory(samlconstants::SAML1_PROFILE_BROWSER_ARTIFACT,&SAML1ArtifactFactory);
114     auto_ptr_char temp4(shibspconstants::SHIB1_LOGOUT_PROFILE_URI);
115     samlConf.getPlugMgr().regFactory(temp4.get(),&ShibLogoutFactory);
116     
117     log.info("finished initializing");
118     return true;
119 }
120
121 bool STConfig::load(const char* config)
122 {
123 #ifdef _DEBUG
124     xmltooling::NDC ndc("load");
125 #endif
126     Category& log = Category::getInstance(SHIBT_LOGCAT".Config");
127
128     if (!config) {
129         log.fatal("path to configuration file not supplied");
130         shutdown();
131         return false;
132     }
133
134     try {
135         log.info("loading configuration file: %s", config);
136         static const XMLCh path[] = UNICODE_LITERAL_4(p,a,t,h);
137         DOMImplementation* impl=DOMImplementationRegistry::getDOMImplementation(NULL);
138         DOMDocument* dummydoc=impl->createDocument();
139         xmltooling::XercesJanitor<DOMDocument> docjanitor(dummydoc);
140         DOMElement* dummy = dummydoc->createElementNS(NULL,path);
141
142         auto_ptr_XMLCh src(config);
143         dummy->setAttributeNS(NULL,path,src.get());
144
145         m_ini=dynamic_cast<IConfig*>(SPConfig::getConfig().ServiceProviderManager.newPlugin(XML_SERVICE_PROVIDER,dummy));
146         m_ini->init();
147         
148         pair<bool,unsigned int> skew=m_ini->getUnsignedInt("clockSkew");
149         SAMLConfig::getConfig().clock_skew_secs=skew.first ? skew.second : 180;
150         if (skew.first)
151             XMLToolingConfig::getConfig().clock_skew_secs=skew.second;
152         
153         m_tranLog=new FixedContextCategory(SHIBTRAN_LOGCAT);
154         m_tranLog->info("opened transaction log");
155         m_tranLogLock = xmltooling::Mutex::create();
156     }
157     catch (SAMLException& ex) {
158         log.fatal("caught exception while loading/initializing configuration: %s",ex.what());
159         shutdown();
160         return false;
161     }
162 #ifndef _DEBUG
163     catch (...) {
164         log.fatal("caught exception while loading/initializing configuration");
165         shutdown();
166         return false;
167     }
168 #endif
169
170     log.info("finished loading configuration");
171     return true;
172 }
173
174 void STConfig::shutdown()
175 {
176 #ifdef _DEBUG
177     xmltooling::NDC ndc("shutdown");
178 #endif
179     Category& log = Category::getInstance(SHIBT_LOGCAT".Config");
180     log.info("shutting down the library");
181     delete m_tranLogLock;
182     m_tranLogLock = NULL;
183     //delete m_tranLog; // This is crashing for some reason, but we're shutting down anyway.
184     delete m_ini;
185     m_ini = NULL;
186     ShibConfig::getConfig().term();
187     SAMLConfig::getConfig().term();
188     SPConfig::getConfig().term();
189     log.info("library shutdown complete");
190 }