Next integration phase, metadata and trust conversion.
[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 <shibsp/SPConstants.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 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("shibtarget.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() || !SPConfig::getConfig().init(NULL)) {
84         log.fatal("failed to initialize SP 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     // Register built-in plugin types.
96
97     samlConf.getPlugMgr().regFactory(shibtarget::XML::MemorySessionCacheType,&MemoryCacheFactory);
98     samlConf.getPlugMgr().regFactory(shibtarget::XML::LegacyRequestMapType,&XMLRequestMapFactory);
99     samlConf.getPlugMgr().regFactory(shibtarget::XML::XMLRequestMapType,&XMLRequestMapFactory);
100     samlConf.getPlugMgr().regFactory(shibtarget::XML::NativeRequestMapType,&XMLRequestMapFactory);
101     
102     auto_ptr_char temp1(shibspconstants::SHIB1_SESSIONINIT_PROFILE_URI);
103     samlConf.getPlugMgr().regFactory(temp1.get(),&ShibSessionInitiatorFactory);
104     samlConf.getPlugMgr().regFactory(samlconstants::SAML1_PROFILE_BROWSER_POST,&SAML1POSTFactory);
105     samlConf.getPlugMgr().regFactory(samlconstants::SAML1_PROFILE_BROWSER_ARTIFACT,&SAML1ArtifactFactory);
106     auto_ptr_char temp4(shibspconstants::SHIB1_LOGOUT_PROFILE_URI);
107     samlConf.getPlugMgr().regFactory(temp4.get(),&ShibLogoutFactory);
108     
109     saml::XML::registerSchema(shibtarget::XML::SHIBTARGET_NS,shibtarget::XML::SHIBTARGET_SCHEMA_ID,NULL,false);
110     saml::XML::registerSchema(samlconstants::SAML20MD_NS,shibtarget::XML::SAML2META_SCHEMA_ID,NULL,false);
111     saml::XML::registerSchema(samlconstants::SAML20_NS,shibtarget::XML::SAML2ASSERT_SCHEMA_ID,NULL,false);
112     saml::XML::registerSchema(xmlconstants::XMLENC_NS,shibtarget::XML::XMLENC_SCHEMA_ID,NULL,false);
113     
114     log.info("finished initializing");
115     return true;
116 }
117
118 bool STConfig::load(const char* config)
119 {
120 #ifdef _DEBUG
121     xmltooling::NDC ndc("load");
122 #endif
123     Category& log = Category::getInstance("shibtarget.Config");
124
125     if (!config) {
126         log.fatal("path to configuration file not supplied");
127         shutdown();
128         return false;
129     }
130
131     try {
132         log.info("loading configuration file: %s", config);
133         static const XMLCh uri[] = { chLatin_u, chLatin_r, chLatin_i, chNull };
134         DOMImplementation* impl=DOMImplementationRegistry::getDOMImplementation(NULL);
135         DOMDocument* dummydoc=impl->createDocument();
136         DOMElement* dummy = dummydoc->createElementNS(NULL,XML::Literals::ShibbolethTargetConfig);
137         auto_ptr_XMLCh src(config);
138         dummy->setAttributeNS(NULL,uri,src.get());
139         m_ini=ShibTargetConfigFactory(dummy);
140         dummydoc->release();
141         m_ini->init();
142         
143         pair<bool,unsigned int> skew=m_ini->getUnsignedInt("clockSkew");
144         SAMLConfig::getConfig().clock_skew_secs=skew.first ? skew.second : 180;
145         if (skew.first)
146             XMLToolingConfig::getConfig().clock_skew_secs=skew.second;
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("shibtarget.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     delete m_ini;
180     m_ini = NULL;
181     ShibConfig::getConfig().term();
182     SAMLConfig::getConfig().term();
183     SPConfig::getConfig().term();
184     log.info("library shutdown complete");
185 }