2 * Copyright 2001-2005 Internet2
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * shib-config.cpp -- ShibTarget initialization and finalization routines
20 * Created By: Derek Atkins <derek@ihtfp.com>
26 #include <saml/util/SAMLConstants.h>
27 #include <shibsp/SPConfig.h>
28 #include <xmltooling/XMLToolingConfig.h>
29 #include <xmltooling/util/NDC.h>
31 #include <log4cpp/OstreamAppender.hh>
33 using namespace shibsp;
34 using namespace shibtarget;
35 using namespace shibboleth;
37 using namespace log4cpp;
40 using xmltooling::XMLToolingConfig;
46 // Factories for built-in plugins we can manufacture. Actual definitions
47 // will be with the actual object implementation.
49 PlugManager::Factory UnixListenerFactory;
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;
61 SAML_EXCEPTION_FACTORY(ListenerException);
62 SAML_EXCEPTION_FACTORY(ConfigurationException);
64 ShibTargetConfig& ShibTargetConfig::getConfig()
69 bool STConfig::init(const char* schemadir)
72 xmltooling::NDC ndc("init");
74 Category& log = Category::getInstance("shibtarget.Config");
77 log.fatal("XML schema directory not supplied");
81 // This will cause some extra console logging, but for now,
82 // initialize the underlying libraries.
83 SAMLConfig& samlConf=SAMLConfig::getConfig();
85 samlConf.schema_dir = schemadir;
86 if (!samlConf.init() || !SPConfig::getConfig().init(NULL)) {
87 log.fatal("failed to initialize SP library");
91 ShibConfig& shibConf=ShibConfig::getConfig();
92 if (!shibConf.init()) {
93 log.fatal("Failed to initialize Shib library");
98 // Register built-in plugin types.
99 REGISTER_EXCEPTION_FACTORY(ListenerException);
100 REGISTER_EXCEPTION_FACTORY(ConfigurationException);
102 samlConf.getPlugMgr().regFactory(shibtarget::XML::UnixListenerType,&UnixListenerFactory);
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);
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);
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);
123 log.info("finished initializing");
127 bool STConfig::load(const char* config)
130 xmltooling::NDC ndc("load");
132 Category& log = Category::getInstance("shibtarget.Config");
135 log.fatal("path to configuration file not supplied");
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);
152 pair<bool,unsigned int> skew=m_ini->getUnsignedInt("clockSkew");
153 SAMLConfig::getConfig().clock_skew_secs=skew.first ? skew.second : 180;
155 XMLToolingConfig::getConfig().clock_skew_secs=skew.second;
157 m_tranLog=new FixedContextCategory(SHIBTRAN_LOGCAT);
158 m_tranLog->info("opened transaction log");
159 m_tranLogLock = xmltooling::Mutex::create();
161 catch (SAMLException& ex) {
162 log.fatal("caught exception while loading/initializing configuration: %s",ex.what());
168 log.fatal("caught exception while loading/initializing configuration");
174 log.info("finished loading configuration");
178 void STConfig::shutdown()
181 xmltooling::NDC ndc("shutdown");
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.
190 ShibConfig::getConfig().term();
191 SAMLConfig::getConfig().term();
192 SPConfig::getConfig().term();
193 log.info("library shutdown complete");