Starting to refactor session cache, eliminated IConfig class.
[shibboleth/cpp-sp.git] / shib-target / shib-config.cpp
index 7f46801..064c61a 100644 (file)
@@ -1,50 +1,17 @@
 /*
- * The Shibboleth License, Version 1.
- * Copyright (c) 2002
- * University Corporation for Advanced Internet Development, Inc.
- * All rights reserved
+ *  Copyright 2001-2005 Internet2
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
  *
+ *     http://www.apache.org/licenses/LICENSE-2.0
  *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * Redistributions of source code must retain the above copyright notice, this
- * list of conditions and the following disclaimer.
- *
- * Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution, if any, must include
- * the following acknowledgment: "This product includes software developed by
- * the University Corporation for Advanced Internet Development
- * <http://www.ucaid.edu>Internet2 Project. Alternately, this acknowledegement
- * may appear in the software itself, if and wherever such third-party
- * acknowledgments normally appear.
- *
- * Neither the name of Shibboleth nor the names of its contributors, nor
- * Internet2, nor the University Corporation for Advanced Internet Development,
- * Inc., nor UCAID may be used to endorse or promote products derived from this
- * software without specific prior written permission. For written permission,
- * please contact shibboleth@shibboleth.org
- *
- * Products derived from this software may not be called Shibboleth, Internet2,
- * UCAID, or the University Corporation for Advanced Internet Development, nor
- * may Shibboleth appear in their name, without prior written permission of the
- * University Corporation for Advanced Internet Development.
- *
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND WITH ALL FAULTS. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
- * PARTICULAR PURPOSE, AND NON-INFRINGEMENT ARE DISCLAIMED AND THE ENTIRE RISK
- * OF SATISFACTORY QUALITY, PERFORMANCE, ACCURACY, AND EFFORT IS WITH LICENSEE.
- * IN NO EVENT SHALL THE COPYRIGHT OWNER, CONTRIBUTORS OR THE UNIVERSITY
- * CORPORATION FOR ADVANCED INTERNET DEVELOPMENT, INC. BE LIABLE FOR ANY DIRECT,
- * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
- * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
  */
 
 /*
  */
 
 #include "internal.h"
+#include <shibsp/SPConfig.h>
+#include <xmltooling/XMLToolingConfig.h>
 
 #include <log4cpp/OstreamAppender.hh>
 
-using namespace std;
-using namespace log4cpp;
-using namespace saml;
-using namespace shibboleth;
+using namespace shibsp;
 using namespace shibtarget;
+using namespace shibboleth;
+using namespace saml;
+using namespace log4cpp;
+using namespace std;
+
+using xmltooling::XMLToolingConfig;
+using xmltooling::PluginManager;
 
 namespace {
     STConfig g_Config;
 }
 
-const XMLCh ShibTargetConfig::SHIBTARGET_NS[] = // urn:mace:shibboleth:target:config:1.0
-{ chLatin_u, chLatin_r, chLatin_n, chColon, chLatin_m, chLatin_a, chLatin_c, chLatin_e, chColon,
-  chLatin_s, chLatin_h, chLatin_i, chLatin_b, chLatin_b, chLatin_o, chLatin_l, chLatin_e, chLatin_t, chLatin_h, chColon,
-  chLatin_t, chLatin_a, chLatin_r, chLatin_g, chLatin_e, chLatin_t, chColon,
-  chLatin_c, chLatin_o, chLatin_n, chLatin_f, chLatin_i, chLatin_g, chColon,
-  chDigit_1, chPeriod, chDigit_0, chNull
-};
-
 // Factories for built-in plugins we can manufacture. Actual definitions
 // will be with the actual object implementation.
 #ifndef WIN32
 PlugManager::Factory UnixListenerFactory;
 #endif
 PlugManager::Factory TCPListenerFactory;
-PlugManager::Factory MemoryCacheFactory;
-PlugManager::Factory XMLRequestMapFactory;
-//PlugManager::Factory htaccessFactory;
+//PlugManager::Factory MemoryListenerFactory;
 
-SAML_EXCEPTION_FACTORY(ListenerException);
+PluginManager<SessionCache,const DOMElement*>::Factory MemoryCacheFactory;
+PluginManager<Handler,const DOMElement*>::Factory ShibSessionInitiatorFactory;
+PluginManager<Handler,const DOMElement*>::Factory SAML1POSTFactory;
+PluginManager<Handler,const DOMElement*>::Factory SAML1ArtifactFactory;
+PluginManager<Handler,const DOMElement*>::Factory ShibLogoutFactory;
 
 ShibTargetConfig& ShibTargetConfig::getConfig()
 {
     return g_Config;
 }
 
-bool STConfig::init(const char* schemadir, const char* config)
+bool STConfig::init(const char* schemadir)
 {
-    // With new build of log4cpp, we need to establish a "default"
-    // logging appender to stderr up front.
-    const char* loglevel=getenv("SHIB_LOGGING");
-    if (!loglevel)
-        loglevel = SHIB_LOGGING;    
-    Category& root = Category::getRoot();
-    if (!strcmp(loglevel,"DEBUG"))
-        root.setPriority(Priority::DEBUG);
-    else if (!strcmp(loglevel,"INFO"))
-        root.setPriority(Priority::INFO);
-    else if (!strcmp(loglevel,"NOTICE"))
-        root.setPriority(Priority::NOTICE);
-    else if (!strcmp(loglevel,"WARN"))
-        root.setPriority(Priority::WARN);
-    else if (!strcmp(loglevel,"ERROR"))
-        root.setPriority(Priority::ERROR);
-    else if (!strcmp(loglevel,"CRIT"))
-        root.setPriority(Priority::CRIT);
-    else if (!strcmp(loglevel,"ALERT"))
-        root.setPriority(Priority::ALERT);
-    else if (!strcmp(loglevel,"EMERG"))
-        root.setPriority(Priority::EMERG);
-    else if (!strcmp(loglevel,"FATAL"))
-        root.setPriority(Priority::FATAL);
-    root.setAppender(new OstreamAppender("default",&cerr));
 #ifdef _DEBUG
-    saml::NDC ndc("init");
+    xmltooling::NDC ndc("init");
 #endif
-    Category& log = Category::getInstance("shibtarget.STConfig");
+    Category& log = Category::getInstance(SHIBT_LOGCAT".Config");
 
-    if (!schemadir || !config) {
-        log.fatal("schema directory or config file not supplied");
+    if (!schemadir) {
+        log.fatal("XML schema directory not supplied");
         return false;
     }
 
@@ -137,65 +78,79 @@ bool STConfig::init(const char* schemadir, const char* config)
     SAMLConfig& samlConf=SAMLConfig::getConfig();
     if (schemadir)
         samlConf.schema_dir = schemadir;
-    try {
-        if (!samlConf.init()) {
-            log.fatal("Failed to initialize SAML Library");
-            return false;
-        }
-    }
-    catch (...) {
-        log.fatal("Died initializing SAML Library");
+    if (!samlConf.init()) {
+        log.fatal("failed to initialize OpenSAML1 library");
         return false;
     }
-    
+
     ShibConfig& shibConf=ShibConfig::getConfig();
-    try { 
-        if (!shibConf.init()) {
-            log.fatal("Failed to initialize Shib library");
-            samlConf.term();
-            return false;
-        }
+    if (!shibConf.init()) {
+        log.fatal("Failed to initialize Shib library");
+        samlConf.term();
+        return false;
     }
-    catch (...) {
-        log.fatal("Died initializing Shib library.");
+    
+    SPConfig& conf=SPConfig::getConfig();
+    if (!SPConfig::getConfig().init(NULL)) {
+        log.fatal("Failed to initialize SP library");
+        shibConf.term();
         samlConf.term();
         return false;
     }
 
-    try {
-        // Register plugin types.
-        REGISTER_EXCEPTION_FACTORY(ListenerException);
-#ifndef WIN32
-        samlConf.getPlugMgr().regFactory(shibtarget::XML::UnixListenerType,&UnixListenerFactory);
+    // Register built-in plugin types.
+    conf.ServiceProviderManager.registerFactory(XML_SERVICE_PROVIDER, XMLServiceProviderFactory);
+
+    conf.SessionInitiatorManager.registerFactory(shibspconstants::SHIB1_SESSIONINIT_PROFILE_URI,&ShibSessionInitiatorFactory);
+    conf.AssertionConsumerServiceManager.registerFactory(samlconstants::SAML1_PROFILE_BROWSER_POST,&SAML1POSTFactory);
+    conf.AssertionConsumerServiceManager.registerFactory(samlconstants::SAML1_PROFILE_BROWSER_ARTIFACT,&SAML1ArtifactFactory);
+    conf.SingleLogoutServiceManager.registerFactory(shibspconstants::SHIB1_LOGOUT_PROFILE_URI,&ShibLogoutFactory);
+    
+    conf.SessionCacheManager.registerFactory(MEMORY_SESSIONCACHE,&MemoryCacheFactory);
+    
+    log.info("finished initializing");
+    return true;
+}
+
+bool STConfig::load(const char* config)
+{
+#ifdef _DEBUG
+    xmltooling::NDC ndc("load");
 #endif
-        samlConf.getPlugMgr().regFactory(shibtarget::XML::TCPListenerType,&TCPListenerFactory);
-        samlConf.getPlugMgr().regFactory(shibtarget::XML::MemorySessionCacheType,&MemoryCacheFactory);
-        samlConf.getPlugMgr().regFactory(shibtarget::XML::LegacyRequestMapType,&XMLRequestMapFactory);
-        samlConf.getPlugMgr().regFactory(shibtarget::XML::RequestMapType,&XMLRequestMapFactory);
-        //shibConf.getPlugMgr().regFactory(shibtarget::XML::htaccessType,&htaccessFactory);
-        saml::XML::registerSchema(ShibTargetConfig::SHIBTARGET_NS,shibtarget::XML::SHIBTARGET_SCHEMA_ID);
-        
+    Category& log = Category::getInstance(SHIBT_LOGCAT".Config");
+
+    if (!config) {
+        log.fatal("path to configuration file not supplied");
+        shutdown();
+        return false;
+    }
+
+    try {
         log.info("loading configuration file: %s", config);
-        static const XMLCh uri[] = { chLatin_u, chLatin_r, chLatin_i, chNull };
+        static const XMLCh path[] = UNICODE_LITERAL_4(p,a,t,h);
         DOMImplementation* impl=DOMImplementationRegistry::getDOMImplementation(NULL);
         DOMDocument* dummydoc=impl->createDocument();
-        DOMElement* dummy = dummydoc->createElementNS(NULL,XML::Literals::ShibbolethTargetConfig);
+        xmltooling::XercesJanitor<DOMDocument> docjanitor(dummydoc);
+        DOMElement* dummy = dummydoc->createElementNS(NULL,path);
+
         auto_ptr_XMLCh src(config);
-        dummy->setAttributeNS(NULL,uri,src.get());
-        m_ini=ShibTargetConfigFactory(dummy);
-        dummydoc->release();
+        dummy->setAttributeNS(NULL,path,src.get());
+
+        auto_ptr<ServiceProvider> sp(SPConfig::getConfig().ServiceProviderManager.newPlugin(XML_SERVICE_PROVIDER,dummy));
+        sp->init();
         
-        pair<bool,unsigned int> skew=m_ini->getUnsignedInt("clockSkew");
-        samlConf.clock_skew_secs=skew.first ? skew.second : 180;
+        pair<bool,unsigned int> skew=sp->getUnsignedInt("clockSkew");
+        SAMLConfig::getConfig().clock_skew_secs=skew.first ? skew.second : 180;
+        if (skew.first)
+            XMLToolingConfig::getConfig().clock_skew_secs=skew.second;
+        SPConfig::getConfig().setServiceProvider(sp.release());
         
         m_tranLog=new FixedContextCategory(SHIBTRAN_LOGCAT);
         m_tranLog->info("opened transaction log");
-        m_tranLogLock = Mutex::create();
-
-        m_rpcpool = new RPCHandlePool;
+        m_tranLogLock = xmltooling::Mutex::create();
     }
     catch (SAMLException& ex) {
-        log.fatalStream() << "caught exception while loading/initializing configuration: " << ex.what() << CategoryStream::ENDLINE;
+        log.fatal("caught exception while loading/initializing configuration: %s",ex.what());
         shutdown();
         return false;
     }
@@ -206,26 +161,22 @@ bool STConfig::init(const char* schemadir, const char* config)
         return false;
     }
 #endif
-  
-    log.info("finished initializing");
 
+    log.info("finished loading configuration");
     return true;
 }
 
 void STConfig::shutdown()
 {
 #ifdef _DEBUG
-    saml::NDC ndc("shutdown");
+    xmltooling::NDC ndc("shutdown");
 #endif
-    Category& log = Category::getInstance("shibtarget.STConfig");
+    Category& log = Category::getInstance(SHIBT_LOGCAT".Config");
     log.info("shutting down the library");
-    delete m_rpcpool;
-    m_rpcpool = NULL;
     delete m_tranLogLock;
     m_tranLogLock = NULL;
     //delete m_tranLog; // This is crashing for some reason, but we're shutting down anyway.
-    delete m_ini;
-    m_ini = NULL;
+    SPConfig::getConfig().term();
     ShibConfig::getConfig().term();
     SAMLConfig::getConfig().term();
     log.info("library shutdown complete");