Ensure outgoing errors are logged.
[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
27 #if defined(HAVE_LOG4SHIB)
28 # include <log4shib/OstreamAppender.hh>
29 #elif defined(HAVE_LOG4CPP)
30 # include <log4cpp/OstreamAppender.hh>
31 #else
32 # error "Supported logging library not available."
33 #endif
34
35 using namespace std;
36 using namespace saml;
37 using namespace shibboleth;
38 using namespace shibtarget;
39 using namespace shibtarget::logging;
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 PlugManager::Factory MemoryCacheFactory;
53 PlugManager::Factory XMLRequestMapFactory;
54 PlugManager::Factory ShibSessionInitiatorFactory;
55 PlugManager::Factory SAML1POSTFactory;
56 PlugManager::Factory SAML1ArtifactFactory;
57 PlugManager::Factory ShibLogoutFactory;
58 //PlugManager::Factory htaccessFactory;
59
60 SAML_EXCEPTION_FACTORY(ListenerException);
61 SAML_EXCEPTION_FACTORY(ConfigurationException);
62
63 ShibTargetConfig& ShibTargetConfig::getConfig()
64 {
65     return g_Config;
66 }
67
68 bool STConfig::init(const char* schemadir)
69 {
70     // With new build of log4cpp, we need to establish a "default"
71     // logging appender to stderr up front.
72     const char* loglevel=getenv("SHIB_LOGGING");
73     if (!loglevel)
74         loglevel = SHIB_LOGGING;    
75     Category& root = Category::getRoot();
76     if (!strcmp(loglevel,"DEBUG"))
77         root.setPriority(Priority::DEBUG);
78     else if (!strcmp(loglevel,"INFO"))
79         root.setPriority(Priority::INFO);
80     else if (!strcmp(loglevel,"NOTICE"))
81         root.setPriority(Priority::NOTICE);
82     else if (!strcmp(loglevel,"WARN"))
83         root.setPriority(Priority::WARN);
84     else if (!strcmp(loglevel,"ERROR"))
85         root.setPriority(Priority::ERROR);
86     else if (!strcmp(loglevel,"CRIT"))
87         root.setPriority(Priority::CRIT);
88     else if (!strcmp(loglevel,"ALERT"))
89         root.setPriority(Priority::ALERT);
90     else if (!strcmp(loglevel,"EMERG"))
91         root.setPriority(Priority::EMERG);
92     else if (!strcmp(loglevel,"FATAL"))
93         root.setPriority(Priority::FATAL);
94     root.setAppender(new OstreamAppender("default",&cerr));
95  
96 #ifdef _DEBUG
97     saml::NDC ndc("init");
98 #endif
99     Category& log = Category::getInstance("shibtarget.Config");
100
101     if (!schemadir) {
102         log.fatal("XML schema directory not supplied");
103         return false;
104     }
105
106     // This will cause some extra console logging, but for now,
107     // initialize the underlying libraries.
108     SAMLConfig& samlConf=SAMLConfig::getConfig();
109     if (schemadir)
110         samlConf.schema_dir = schemadir;
111     try {
112         if (!samlConf.init()) {
113             log.fatal("Failed to initialize SAML Library");
114             return false;
115         }
116     }
117     catch (...) {
118         log.fatal("Died initializing SAML Library");
119         return false;
120     }
121     
122     ShibConfig& shibConf=ShibConfig::getConfig();
123     try { 
124         if (!shibConf.init()) {
125             log.fatal("Failed to initialize Shib library");
126             samlConf.term();
127             return false;
128         }
129     }
130     catch (...) {
131         log.fatal("Died initializing Shib library.");
132         samlConf.term();
133         return false;
134     }
135
136     // Register built-in plugin types.
137     REGISTER_EXCEPTION_FACTORY(ListenerException);
138     REGISTER_EXCEPTION_FACTORY(ConfigurationException);
139 #ifndef WIN32
140     samlConf.getPlugMgr().regFactory(shibtarget::XML::UnixListenerType,&UnixListenerFactory);
141 #endif
142     samlConf.getPlugMgr().regFactory(shibtarget::XML::TCPListenerType,&TCPListenerFactory);
143     samlConf.getPlugMgr().regFactory(shibtarget::XML::MemoryListenerType,&MemoryListenerFactory);
144     samlConf.getPlugMgr().regFactory(shibtarget::XML::MemorySessionCacheType,&MemoryCacheFactory);
145     samlConf.getPlugMgr().regFactory(shibtarget::XML::LegacyRequestMapType,&XMLRequestMapFactory);
146     samlConf.getPlugMgr().regFactory(shibtarget::XML::XMLRequestMapType,&XMLRequestMapFactory);
147     samlConf.getPlugMgr().regFactory(shibtarget::XML::NativeRequestMapType,&XMLRequestMapFactory);
148     
149     auto_ptr_char temp1(Constants::SHIB_SESSIONINIT_PROFILE_URI);
150     samlConf.getPlugMgr().regFactory(temp1.get(),&ShibSessionInitiatorFactory);
151     auto_ptr_char temp2(SAMLBrowserProfile::BROWSER_POST);
152     samlConf.getPlugMgr().regFactory(temp2.get(),&SAML1POSTFactory);
153     auto_ptr_char temp3(SAMLBrowserProfile::BROWSER_ARTIFACT);
154     samlConf.getPlugMgr().regFactory(temp3.get(),&SAML1ArtifactFactory);
155     auto_ptr_char temp4(Constants::SHIB_LOGOUT_PROFILE_URI);
156     samlConf.getPlugMgr().regFactory(temp4.get(),&ShibLogoutFactory);
157     
158     saml::XML::registerSchema(shibtarget::XML::SHIBTARGET_NS,shibtarget::XML::SHIBTARGET_SCHEMA_ID,NULL,false);
159     saml::XML::registerSchema(shibtarget::XML::SAML2META_NS,shibtarget::XML::SAML2META_SCHEMA_ID,NULL,false);
160     saml::XML::registerSchema(shibtarget::XML::SAML2ASSERT_NS,shibtarget::XML::SAML2ASSERT_SCHEMA_ID,NULL,false);
161     saml::XML::registerSchema(shibtarget::XML::XMLENC_NS,shibtarget::XML::XMLENC_SCHEMA_ID,NULL,false);
162     
163     log.info("finished initializing");
164     return true;
165 }
166
167 bool STConfig::load(const char* config)
168 {
169 #ifdef _DEBUG
170     saml::NDC ndc("load");
171 #endif
172     Category& log = Category::getInstance("shibtarget.Config");
173
174     if (!config) {
175         log.fatal("path to configuration file not supplied");
176         shutdown();
177         return false;
178     }
179
180     try {
181         log.info("loading configuration file: %s", config);
182         static const XMLCh uri[] = { chLatin_u, chLatin_r, chLatin_i, chNull };
183         DOMImplementation* impl=DOMImplementationRegistry::getDOMImplementation(NULL);
184         DOMDocument* dummydoc=impl->createDocument();
185         DOMElement* dummy = dummydoc->createElementNS(NULL,XML::Literals::ShibbolethTargetConfig);
186         auto_ptr_XMLCh src(config);
187         dummy->setAttributeNS(NULL,uri,src.get());
188         m_ini=ShibTargetConfigFactory(dummy);
189         dummydoc->release();
190         
191         pair<bool,unsigned int> skew=m_ini->getUnsignedInt("clockSkew");
192         SAMLConfig::getConfig().clock_skew_secs=skew.first ? skew.second : 180;
193         
194         m_tranLog=new FixedContextCategory(SHIBTRAN_LOGCAT);
195         m_tranLog->info("opened transaction log");
196         m_tranLogLock = Mutex::create();
197     }
198     catch (SAMLException& ex) {
199         log.fatal("caught exception while loading/initializing configuration: %s",ex.what());
200         shutdown();
201         return false;
202     }
203 #ifndef _DEBUG
204     catch (...) {
205         log.fatal("caught exception while loading/initializing configuration");
206         shutdown();
207         return false;
208     }
209 #endif
210
211     log.info("finished loading configuration");
212     return true;
213 }
214
215 void STConfig::shutdown()
216 {
217 #ifdef _DEBUG
218     saml::NDC ndc("shutdown");
219 #endif
220     Category& log = Category::getInstance("shibtarget.Config");
221     log.info("shutting down the library");
222     delete m_tranLogLock;
223     m_tranLogLock = NULL;
224     //delete m_tranLog; // This is crashing for some reason, but we're shutting down anyway.
225     delete m_ini;
226     m_ini = NULL;
227     ShibConfig::getConfig().term();
228     SAMLConfig::getConfig().term();
229     log.info("library shutdown complete");
230 }