https://bugs.internet2.edu/jira/browse/SSPCPP-293
[shibboleth/sp.git] / shibsp / SPConfig.cpp
index 52ceecd..f89ee17 100644 (file)
@@ -1,7 +1,7 @@
 
 /*
- *  Copyright 2001-2007 Internet2
- * 
+ *  Copyright 2001-2010 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
@@ -17,8 +17,8 @@
 
 /**
  * SPConfig.cpp
- * 
- * Library configuration 
+ *
+ * Library configuration.
  */
 
 #include "internal.h"
@@ -41,6 +41,7 @@
 #include "ServiceProvider.h"
 #include "SessionCache.h"
 #include "SPConfig.h"
+#include "TransactionLog.h"
 #include "attribute/Attribute.h"
 #include "handler/SessionInitiator.h"
 #include "remoting/ListenerService.h"
 # include "binding/ArtifactResolver.h"
 # include "metadata/MetadataExt.h"
 # include "security/PKIXTrustEngine.h"
+# include "security/SecurityPolicyProvider.h"
 # include <saml/SAMLConfig.h>
-# include <xmltooling/util/CurlNetAccessor.hpp>
-#else
-# include <xmltooling/XMLToolingConfig.h>
 #endif
 
+#include <ctime>
+#include <xercesc/util/XMLUniDefs.hpp>
+#include <xmltooling/XMLToolingConfig.h>
 #include <xmltooling/util/NDC.h>
+#include <xmltooling/util/ParserPool.h>
 #include <xmltooling/util/PathResolver.h>
 #include <xmltooling/util/TemplateEngine.h>
+#include <xmltooling/util/Threads.h>
+#include <xmltooling/util/XMLHelper.h>
 
 using namespace shibsp;
 using namespace opensaml;
 using namespace xmltooling;
+using namespace std;
 
 DECL_XMLTOOLING_EXCEPTION_FACTORY(AttributeException,shibsp);
 DECL_XMLTOOLING_EXCEPTION_FACTORY(AttributeExtractionException,shibsp);
@@ -93,12 +99,52 @@ SPConfig& SPConfig::getConfig()
     return g_config;
 }
 
+SPConfig::SPConfig() : attribute_value_delimeter(';'), m_serviceProvider(nullptr),
+#ifndef SHIBSP_LITE
+    m_artifactResolver(nullptr),
+#endif
+    m_features(0), m_configDoc(nullptr)
+{
+}
+
+SPConfig::~SPConfig()
+{
+}
+
+void SPConfig::setFeatures(unsigned long enabled)
+{
+    m_features = enabled;
+}
+
+bool SPConfig::isEnabled(components_t feature)
+{
+    return (m_features & feature)>0;
+}
+
+ServiceProvider* SPConfig::getServiceProvider() const
+{
+    return m_serviceProvider;
+}
+
 void SPConfig::setServiceProvider(ServiceProvider* serviceProvider)
 {
     delete m_serviceProvider;
     m_serviceProvider = serviceProvider;
 }
 
+#ifndef SHIBSP_LITE
+void SPConfig::setArtifactResolver(MessageDecoder::ArtifactResolver* artifactResolver)
+{
+    delete m_artifactResolver;
+    m_artifactResolver = artifactResolver;
+}
+
+const MessageDecoder::ArtifactResolver* SPConfig::getArtifactResolver() const
+{
+    return m_artifactResolver;
+}
+#endif
+
 bool SPConfig::init(const char* catalog_path, const char* inst_prefix)
 {
 #ifdef _DEBUG
@@ -108,13 +154,22 @@ bool SPConfig::init(const char* catalog_path, const char* 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;
     std::string ll(loglevel);
     PathResolver localpr;
-    localpr.setDefaultPrefix(inst_prefix);
+    localpr.setDefaultPrefix(inst_prefix2.c_str());
+    inst_prefix = getenv("SHIBSP_CFGDIR");
+    if (!inst_prefix)
+        inst_prefix = SHIBSP_CFGDIR;
+    localpr.setCfgDir(inst_prefix);
     XMLToolingConfig::getConfig().log_config(localpr.resolve(ll, PathResolver::XMLTOOLING_CFG_FILE, PACKAGE_NAME).c_str());
 
     Category& log=Category::getInstance(SHIBSP_LOGCAT".Config");
@@ -131,18 +186,36 @@ bool SPConfig::init(const char* catalog_path, const char* inst_prefix)
         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_prefix);
+#endif
+    PathResolver* pr = XMLToolingConfig::getConfig().getPathResolver();
+    pr->setDefaultPackageName(PACKAGE_NAME);
+    pr->setDefaultPrefix(inst_prefix2.c_str());
+    pr->setCfgDir(inst_prefix);
+    inst_prefix = getenv("SHIBSP_LIBDIR");
+    if (!inst_prefix)
+        inst_prefix = SHIBSP_LIBDIR;
+    pr->setLibDir(inst_prefix);
+    inst_prefix = getenv("SHIBSP_LOGDIR");
+    if (!inst_prefix)
+        inst_prefix = SHIBSP_LOGDIR;
+    pr->setLogDir(inst_prefix);
+    inst_prefix = getenv("SHIBSP_RUNDIR");
+    if (!inst_prefix)
+        inst_prefix = SHIBSP_RUNDIR;
+    pr->setRunDir(inst_prefix);
+    inst_prefix = getenv("SHIBSP_XMLDIR");
+    if (!inst_prefix)
+        inst_prefix = SHIBSP_XMLDIR;
+    pr->setXMLDir(inst_prefix);
+
     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);
@@ -179,6 +252,7 @@ bool SPConfig::init(const char* catalog_path, const char* inst_prefix)
         registerAttributeFilters();
         registerMatchFunctors();
     }
+    registerSecurityPolicyProviders();
 #endif
 
     if (isEnabled(Listener))
@@ -196,6 +270,7 @@ bool SPConfig::init(const char* catalog_path, const char* inst_prefix)
     if (isEnabled(OutOfProcess))
         m_artifactResolver = new ArtifactResolver();
 #endif
+    srand(static_cast<unsigned int>(std::time(nullptr)));
 
     log.info("%s library initialization complete", PACKAGE_STRING);
     return true;
@@ -209,9 +284,12 @@ void SPConfig::term()
     Category& log=Category::getInstance(SHIBSP_LOGCAT".Config");
     log.info("%s library shutting down", PACKAGE_STRING);
 
-    setServiceProvider(NULL);
+    setServiceProvider(nullptr);
+    if (m_configDoc)
+        m_configDoc->release();
+    m_configDoc = nullptr;
 #ifndef SHIBSP_LITE
-    setArtifactResolver(NULL);
+    setArtifactResolver(nullptr);
 #endif
 
     ArtifactResolutionServiceManager.deregisterFactories();
@@ -225,6 +303,7 @@ void SPConfig::term()
     Attribute::deregisterFactories();
 
 #ifndef SHIBSP_LITE
+    SecurityPolicyProviderManager.deregisterFactories();
     if (isEnabled(AttributeResolution)) {
         MatchFunctorManager.deregisterFactories();
         AttributeFilterManager.deregisterFactories();
@@ -252,3 +331,79 @@ void SPConfig::term()
 #endif
     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()));
+            if (m_configDoc)
+                m_configDoc->release();
+            m_configDoc = docjanitor.release();
+        }
+        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(nullptr,_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.");
+            if (m_configDoc)
+                m_configDoc->release();
+            m_configDoc = docjanitor.release();
+        }
+
+        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;
+}
+
+TransactionLog::TransactionLog() : log(logging::Category::getInstance(SHIBSP_TX_LOGCAT)), m_lock(Mutex::create())
+{
+}
+
+TransactionLog::~TransactionLog()
+{
+    delete m_lock;
+}
+
+Lockable* TransactionLog::lock()
+{
+    m_lock->lock();
+    return this;
+}
+
+void TransactionLog::unlock()
+{
+    m_lock->unlock();
+}