https://issues.shibboleth.net/jira/browse/SSPCPP-128
[shibboleth/cpp-sp.git] / shibsp / SPConfig.cpp
index 3df7a26..7aaaf98 100644 (file)
@@ -1,7 +1,7 @@
 
 /*
  *  Copyright 2001-2007 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
 
 /**
  * SPConfig.cpp
- * 
- * Library configuration 
+ *
+ * Library configuration
  */
 
 #include "internal.h"
+
+#if defined(XMLTOOLING_LOG4SHIB)
+# ifndef SHIBSP_LOG4SHIB
+#  error "Logging library mismatch (XMLTooling is using log4shib)."
+# endif
+#elif defined(XMLTOOLING_LOG4CPP)
+# ifndef SHIBSP_LOG4CPP
+#  error "Logging library mismatch (XMLTooling is using log4cpp)."
+# endif
+#else
+# error "No supported logging library."
+#endif
+
 #include "AccessControl.h"
 #include "exceptions.h"
 #include "RequestMapper.h"
 # include "metadata/MetadataExt.h"
 # include "security/PKIXTrustEngine.h"
 # include <saml/SAMLConfig.h>
+# include <xmltooling/util/CurlNetAccessor.hpp>
 #else
 # include <xmltooling/XMLToolingConfig.h>
 #endif
 
-#include <log4cpp/Category.hh>
+#include <ctime>
+#include <xercesc/util/XMLUniDefs.hpp>
 #include <xmltooling/util/NDC.h>
+#include <xmltooling/util/PathResolver.h>
 #include <xmltooling/util/TemplateEngine.h>
+#include <xmltooling/util/XMLHelper.h>
 
 using namespace shibsp;
 using namespace opensaml;
 using namespace xmltooling;
-using namespace log4cpp;
+using namespace std;
 
 DECL_XMLTOOLING_EXCEPTION_FACTORY(AttributeException,shibsp);
 DECL_XMLTOOLING_EXCEPTION_FACTORY(AttributeExtractionException,shibsp);
@@ -63,15 +80,16 @@ DECL_XMLTOOLING_EXCEPTION_FACTORY(ConfigurationException,shibsp);
 DECL_XMLTOOLING_EXCEPTION_FACTORY(ListenerException,shibsp);
 
 #ifdef SHIBSP_LITE
-DECL_XMLTOOLING_EXCEPTION_FACTORY(MetadataException,opensaml::saml2md);
+DECL_XMLTOOLING_EXCEPTION_FACTORY(BindingException,opensaml);
 DECL_XMLTOOLING_EXCEPTION_FACTORY(SecurityPolicyException,opensaml);
 DECL_XMLTOOLING_EXCEPTION_FACTORY(ProfileException,opensaml);
 DECL_XMLTOOLING_EXCEPTION_FACTORY(FatalProfileException,opensaml);
 DECL_XMLTOOLING_EXCEPTION_FACTORY(RetryableProfileException,opensaml);
+DECL_XMLTOOLING_EXCEPTION_FACTORY(MetadataException,opensaml::saml2md);
 #endif
 
 namespace shibsp {
-   SPInternalConfig g_config;
+   SPConfig g_config;
 }
 
 SPConfig& SPConfig::getConfig()
@@ -79,29 +97,37 @@ SPConfig& SPConfig::getConfig()
     return g_config;
 }
 
-SPInternalConfig& SPInternalConfig::getInternalConfig()
-{
-    return g_config;
-}
-
 void SPConfig::setServiceProvider(ServiceProvider* serviceProvider)
 {
     delete m_serviceProvider;
     m_serviceProvider = serviceProvider;
 }
 
-bool SPInternalConfig::init(const char* catalog_path)
+bool SPConfig::init(const char* catalog_path, const char* inst_prefix)
 {
 #ifdef _DEBUG
     NDC ndc("init");
 #endif
-    Category& log=Category::getInstance(SHIBSP_LOGCAT".Config");
-    log.debug("library initialization started");
+    if (!inst_prefix)
+        inst_prefix = getenv("SHIBSP_PREFIX");
+    if (!inst_prefix)
+        inst_prefix = SHIBSP_PREFIX;
+    std::string inst_prefix2;
+    while (*inst_prefix) {
+        inst_prefix2.push_back((*inst_prefix=='\\') ? ('/') : (*inst_prefix));
+        ++inst_prefix;
+    }
 
     const char* loglevel=getenv("SHIBSP_LOGGING");
     if (!loglevel)
         loglevel = SHIBSP_LOGGING;
-    XMLToolingConfig::getConfig().log_config(loglevel);
+    std::string ll(loglevel);
+    PathResolver localpr;
+    localpr.setDefaultPrefix(inst_prefix2.c_str());
+    XMLToolingConfig::getConfig().log_config(localpr.resolve(ll, PathResolver::XMLTOOLING_CFG_FILE, PACKAGE_NAME).c_str());
+
+    Category& log=Category::getInstance(SHIBSP_LOGCAT".Config");
+    log.debug("%s library initialization started", PACKAGE_STRING);
 
     if (!catalog_path)
         catalog_path = getenv("SHIBSP_SCHEMAS");
@@ -114,16 +140,18 @@ bool SPInternalConfig::init(const char* catalog_path)
         log.fatal("failed to initialize OpenSAML library");
         return false;
     }
+    XMLPlatformUtils::fgNetAccessor = new CurlNetAccessor();
 #else
     if (!XMLToolingConfig::getConfig().init()) {
         log.fatal("failed to initialize XMLTooling library");
         return false;
     }
 #endif
-
+    XMLToolingConfig::getConfig().getPathResolver()->setDefaultPackageName(PACKAGE_NAME);
+    XMLToolingConfig::getConfig().getPathResolver()->setDefaultPrefix(inst_prefix2.c_str());
     XMLToolingConfig::getConfig().setTemplateEngine(new TemplateEngine());
     XMLToolingConfig::getConfig().getTemplateEngine()->setTagPrefix("shibmlp");
-    
+
     REGISTER_XMLTOOLING_EXCEPTION_FACTORY(AttributeException,shibsp);
     REGISTER_XMLTOOLING_EXCEPTION_FACTORY(AttributeExtractionException,shibsp);
     REGISTER_XMLTOOLING_EXCEPTION_FACTORY(AttributeFilteringException,shibsp);
@@ -132,6 +160,7 @@ bool SPInternalConfig::init(const char* catalog_path)
     REGISTER_XMLTOOLING_EXCEPTION_FACTORY(ListenerException,shibsp);
 
 #ifdef SHIBSP_LITE
+    REGISTER_XMLTOOLING_EXCEPTION_FACTORY(BindingException,opensaml);
     REGISTER_XMLTOOLING_EXCEPTION_FACTORY(SecurityPolicyException,opensaml);
     REGISTER_XMLTOOLING_EXCEPTION_FACTORY(ProfileException,opensaml);
     REGISTER_XMLTOOLING_EXCEPTION_FACTORY(FatalProfileException,opensaml);
@@ -176,25 +205,28 @@ bool SPInternalConfig::init(const char* catalog_path)
     if (isEnabled(OutOfProcess))
         m_artifactResolver = new ArtifactResolver();
 #endif
+    srand(static_cast<unsigned int>(std::time(NULL)));
 
-    log.info("library initialization complete");
+    log.info("%s library initialization complete", PACKAGE_STRING);
     return true;
 }
 
-void SPInternalConfig::term()
+void SPConfig::term()
 {
 #ifdef _DEBUG
     NDC ndc("term");
 #endif
     Category& log=Category::getInstance(SHIBSP_LOGCAT".Config");
-    log.info("shutting down the library");
+    log.info("%s library shutting down", PACKAGE_STRING);
 
     setServiceProvider(NULL);
 #ifndef SHIBSP_LITE
     setArtifactResolver(NULL);
 #endif
 
+    ArtifactResolutionServiceManager.deregisterFactories();
     AssertionConsumerServiceManager.deregisterFactories();
+    LogoutInitiatorManager.deregisterFactories();
     ManageNameIDServiceManager.deregisterFactories();
     SessionInitiatorManager.deregisterFactories();
     SingleLogoutServiceManager.deregisterFactories();
@@ -228,5 +260,55 @@ void SPInternalConfig::term()
 #else
     XMLToolingConfig::getConfig().term();
 #endif
-    log.info("library shutdown complete");
+    log.info("%s library shutdown complete", PACKAGE_STRING);
+}
+
+bool SPConfig::instantiate(const char* config, bool rethrow)
+{
+#ifdef _DEBUG
+    NDC ndc("instantiate");
+#endif
+    if (!config)
+        config = getenv("SHIBSP_CONFIG");
+    if (!config)
+        config = SHIBSP_CONFIG;
+    try {
+        xercesc::DOMDocument* dummydoc;
+        if (*config == '"' || *config == '\'') {
+            throw ConfigurationException("The value of SHIBSP_CONFIG started with a quote.");
+        }
+        else if (*config != '<') {
+
+            // Mock up some XML.
+            string resolved(config);
+            stringstream snippet;
+            snippet
+                << "<Dummy path='"
+                << XMLToolingConfig::getConfig().getPathResolver()->resolve(resolved, PathResolver::XMLTOOLING_CFG_FILE)
+                << "' validate='1'/>";
+            dummydoc = XMLToolingConfig::getConfig().getParser().parse(snippet);
+            XercesJanitor<xercesc::DOMDocument> docjanitor(dummydoc);
+            setServiceProvider(ServiceProviderManager.newPlugin(XML_SERVICE_PROVIDER, dummydoc->getDocumentElement()));
+        }
+        else {
+            stringstream snippet(config);
+            dummydoc = XMLToolingConfig::getConfig().getParser().parse(snippet);
+            XercesJanitor<xercesc::DOMDocument> docjanitor(dummydoc);
+            static const XMLCh _type[] = UNICODE_LITERAL_4(t,y,p,e);
+            auto_ptr_char type(dummydoc->getDocumentElement()->getAttributeNS(NULL,_type));
+            if (type.get() && *type.get())
+                setServiceProvider(ServiceProviderManager.newPlugin(type.get(), dummydoc->getDocumentElement()));
+            else
+                throw ConfigurationException("The supplied XML bootstrapping configuration did not include a type attribute.");
+        }
+
+        getServiceProvider()->init();
+        return true;
+    }
+    catch (exception& ex) {
+        if (rethrow)
+            throw;
+        Category::getInstance(SHIBSP_LOGCAT".Config").fatal("caught exception while loading configuration: %s", ex.what());
+    }
+    return false;
 }