46b7624a9d71332a1f230d03660d99868172d762
[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
102     samlConf.getPlugMgr().regFactory(shibtarget::XML::MemorySessionCacheType,&MemoryCacheFactory);
103     samlConf.getPlugMgr().regFactory(shibtarget::XML::LegacyRequestMapType,&XMLRequestMapFactory);
104     samlConf.getPlugMgr().regFactory(shibtarget::XML::XMLRequestMapType,&XMLRequestMapFactory);
105     samlConf.getPlugMgr().regFactory(shibtarget::XML::NativeRequestMapType,&XMLRequestMapFactory);
106     
107     auto_ptr_char temp1(Constants::SHIB_SESSIONINIT_PROFILE_URI);
108     samlConf.getPlugMgr().regFactory(temp1.get(),&ShibSessionInitiatorFactory);
109     samlConf.getPlugMgr().regFactory(samlconstants::SAML1_PROFILE_BROWSER_POST,&SAML1POSTFactory);
110     samlConf.getPlugMgr().regFactory(samlconstants::SAML1_PROFILE_BROWSER_ARTIFACT,&SAML1ArtifactFactory);
111     auto_ptr_char temp4(Constants::SHIB_LOGOUT_PROFILE_URI);
112     samlConf.getPlugMgr().regFactory(temp4.get(),&ShibLogoutFactory);
113     
114     saml::XML::registerSchema(shibtarget::XML::SHIBTARGET_NS,shibtarget::XML::SHIBTARGET_SCHEMA_ID,NULL,false);
115     saml::XML::registerSchema(samlconstants::SAML20MD_NS,shibtarget::XML::SAML2META_SCHEMA_ID,NULL,false);
116     saml::XML::registerSchema(samlconstants::SAML20_NS,shibtarget::XML::SAML2ASSERT_SCHEMA_ID,NULL,false);
117     saml::XML::registerSchema(xmlconstants::XMLENC_NS,shibtarget::XML::XMLENC_SCHEMA_ID,NULL,false);
118     
119     log.info("finished initializing");
120     return true;
121 }
122
123 bool STConfig::load(const char* config)
124 {
125 #ifdef _DEBUG
126     xmltooling::NDC ndc("load");
127 #endif
128     Category& log = Category::getInstance("shibtarget.Config");
129
130     if (!config) {
131         log.fatal("path to configuration file not supplied");
132         shutdown();
133         return false;
134     }
135
136     try {
137         log.info("loading configuration file: %s", config);
138         static const XMLCh uri[] = { chLatin_u, chLatin_r, chLatin_i, chNull };
139         DOMImplementation* impl=DOMImplementationRegistry::getDOMImplementation(NULL);
140         DOMDocument* dummydoc=impl->createDocument();
141         DOMElement* dummy = dummydoc->createElementNS(NULL,XML::Literals::ShibbolethTargetConfig);
142         auto_ptr_XMLCh src(config);
143         dummy->setAttributeNS(NULL,uri,src.get());
144         m_ini=ShibTargetConfigFactory(dummy);
145         dummydoc->release();
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("shibtarget.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 }