Starting migration to new SP library
[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 <saml/util/SAMLConstants.h>
27 #include <shibsp/SPConfig.h>
28 #include <xmltooling/XMLToolingConfig.h>
29 #include <xmltooling/util/NDC.h>
30
31 #include <log4cpp/OstreamAppender.hh>
32
33 using namespace shibsp;
34 using namespace shibtarget;
35 using namespace shibboleth;
36 using namespace saml;
37 using namespace log4cpp;
38 using namespace std;
39
40 using xmltooling::XMLToolingConfig;
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 SAML_EXCEPTION_FACTORY(ListenerException);
62 SAML_EXCEPTION_FACTORY(ConfigurationException);
63
64 ShibTargetConfig& ShibTargetConfig::getConfig()
65 {
66     return g_Config;
67 }
68
69 bool STConfig::init(const char* schemadir)
70 {
71 #ifdef _DEBUG
72     xmltooling::NDC ndc("init");
73 #endif
74     Category& log = Category::getInstance("shibtarget.Config");
75
76     if (!schemadir) {
77         log.fatal("XML schema directory not supplied");
78         return false;
79     }
80
81     // This will cause some extra console logging, but for now,
82     // initialize the underlying libraries.
83     SAMLConfig& samlConf=SAMLConfig::getConfig();
84     if (schemadir)
85         samlConf.schema_dir = schemadir;
86     if (!samlConf.init() || !SPConfig::getConfig().init(NULL)) {
87         log.fatal("failed to initialize SP library");
88         return false;
89     }
90
91     ShibConfig& shibConf=ShibConfig::getConfig();
92     if (!shibConf.init()) {
93         log.fatal("Failed to initialize Shib library");
94         samlConf.term();
95         return false;
96     }
97
98     // Register built-in plugin types.
99     REGISTER_EXCEPTION_FACTORY(ListenerException);
100     REGISTER_EXCEPTION_FACTORY(ConfigurationException);
101 #ifndef WIN32
102     samlConf.getPlugMgr().regFactory(shibtarget::XML::UnixListenerType,&UnixListenerFactory);
103 #endif
104     samlConf.getPlugMgr().regFactory(shibtarget::XML::TCPListenerType,&TCPListenerFactory);
105     //samlConf.getPlugMgr().regFactory(shibtarget::XML::MemoryListenerType,&MemoryListenerFactory);
106     samlConf.getPlugMgr().regFactory(shibtarget::XML::MemorySessionCacheType,&MemoryCacheFactory);
107     samlConf.getPlugMgr().regFactory(shibtarget::XML::LegacyRequestMapType,&XMLRequestMapFactory);
108     samlConf.getPlugMgr().regFactory(shibtarget::XML::XMLRequestMapType,&XMLRequestMapFactory);
109     samlConf.getPlugMgr().regFactory(shibtarget::XML::NativeRequestMapType,&XMLRequestMapFactory);
110     
111     auto_ptr_char temp1(Constants::SHIB_SESSIONINIT_PROFILE_URI);
112     samlConf.getPlugMgr().regFactory(temp1.get(),&ShibSessionInitiatorFactory);
113     samlConf.getPlugMgr().regFactory(samlconstants::SAML1_PROFILE_BROWSER_POST,&SAML1POSTFactory);
114     samlConf.getPlugMgr().regFactory(samlconstants::SAML1_PROFILE_BROWSER_ARTIFACT,&SAML1ArtifactFactory);
115     auto_ptr_char temp4(Constants::SHIB_LOGOUT_PROFILE_URI);
116     samlConf.getPlugMgr().regFactory(temp4.get(),&ShibLogoutFactory);
117     
118     saml::XML::registerSchema(shibtarget::XML::SHIBTARGET_NS,shibtarget::XML::SHIBTARGET_SCHEMA_ID,NULL,false);
119     saml::XML::registerSchema(samlconstants::SAML20MD_NS,shibtarget::XML::SAML2META_SCHEMA_ID,NULL,false);
120     saml::XML::registerSchema(samlconstants::SAML20_NS,shibtarget::XML::SAML2ASSERT_SCHEMA_ID,NULL,false);
121     saml::XML::registerSchema(xmlconstants::XMLENC_NS,shibtarget::XML::XMLENC_SCHEMA_ID,NULL,false);
122     
123     log.info("finished initializing");
124     return true;
125 }
126
127 bool STConfig::load(const char* config)
128 {
129 #ifdef _DEBUG
130     xmltooling::NDC ndc("load");
131 #endif
132     Category& log = Category::getInstance("shibtarget.Config");
133
134     if (!config) {
135         log.fatal("path to configuration file not supplied");
136         shutdown();
137         return false;
138     }
139
140     try {
141         log.info("loading configuration file: %s", config);
142         static const XMLCh uri[] = { chLatin_u, chLatin_r, chLatin_i, chNull };
143         DOMImplementation* impl=DOMImplementationRegistry::getDOMImplementation(NULL);
144         DOMDocument* dummydoc=impl->createDocument();
145         DOMElement* dummy = dummydoc->createElementNS(NULL,XML::Literals::ShibbolethTargetConfig);
146         auto_ptr_XMLCh src(config);
147         dummy->setAttributeNS(NULL,uri,src.get());
148         m_ini=ShibTargetConfigFactory(dummy);
149         dummydoc->release();
150         m_ini->init();
151         
152         pair<bool,unsigned int> skew=m_ini->getUnsignedInt("clockSkew");
153         SAMLConfig::getConfig().clock_skew_secs=skew.first ? skew.second : 180;
154         if (skew.first)
155             XMLToolingConfig::getConfig().clock_skew_secs=skew.second;
156         
157         m_tranLog=new FixedContextCategory(SHIBTRAN_LOGCAT);
158         m_tranLog->info("opened transaction log");
159         m_tranLogLock = xmltooling::Mutex::create();
160     }
161     catch (SAMLException& ex) {
162         log.fatal("caught exception while loading/initializing configuration: %s",ex.what());
163         shutdown();
164         return false;
165     }
166 #ifndef _DEBUG
167     catch (...) {
168         log.fatal("caught exception while loading/initializing configuration");
169         shutdown();
170         return false;
171     }
172 #endif
173
174     log.info("finished loading configuration");
175     return true;
176 }
177
178 void STConfig::shutdown()
179 {
180 #ifdef _DEBUG
181     xmltooling::NDC ndc("shutdown");
182 #endif
183     Category& log = Category::getInstance("shibtarget.Config");
184     log.info("shutting down the library");
185     delete m_tranLogLock;
186     m_tranLogLock = NULL;
187     //delete m_tranLog; // This is crashing for some reason, but we're shutting down anyway.
188     delete m_ini;
189     m_ini = NULL;
190     ShibConfig::getConfig().term();
191     SAMLConfig::getConfig().term();
192     SPConfig::getConfig().term();
193     log.info("library shutdown complete");
194 }