/*
+ * 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
+ *
+ * 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.
+ */
+
+/*
* shib-config.cpp -- ShibTarget initialization and finalization routines
*
* Created By: Derek Atkins <derek@ihtfp.com>
* $Id$
*/
-#include "shib-target.h"
-#include "ccache-utils.h"
-#include <shib/shib-threads.h>
+#include "internal.h"
+#include <shibsp/SPConfig.h>
+#include <xmltooling/XMLToolingConfig.h>
-#include <log4cpp/PropertyConfigurator.hh>
-#include <log4cpp/Category.hh>
+#include <log4cpp/OstreamAppender.hh>
-using namespace saml;
-using namespace shibboleth;
+using namespace shibsp;
using namespace shibtarget;
-using namespace std;
+using namespace shibboleth;
+using namespace saml;
using namespace log4cpp;
+using namespace std;
-#ifndef SHIBTARGET_INIFILE
-#define SHIBTARGET_INIFILE "/opt/shibboleth/etc/shibboleth/shibboleth.ini"
-#endif
-
-class STConfig : public ShibTargetConfig
-{
-public:
- STConfig(const char* app_name, const char* inifile);
- ~STConfig();
- void shutdown();
- void init();
- ShibINI& getINI() { return *ini; }
-
- Iterator<const XMLCh*> getPolicies() { return Iterator<const XMLCh*>(policies); }
-
- void ref();
-private:
- SAMLConfig& samlConf;
- ShibConfig& shibConf;
- ShibINI* ini;
- string m_app_name;
- int refcount;
- vector<const XMLCh*> policies;
-};
+using xmltooling::XMLToolingConfig;
+using xmltooling::PluginManager;
namespace {
- STConfig * g_Config = NULL;
- Mutex * g_lock = NULL;
+ STConfig g_Config;
}
-CCache* shibtarget::g_shibTargetCCache = NULL;
-
-/****************************************************************************/
-// External Interface
-
-
-void ShibTargetConfig::preinit()
-{
- if (g_lock) return;
- g_lock = Mutex::create();
-}
-
-ShibTargetConfig& ShibTargetConfig::init(const char* app_name, const char* inifile)
-{
- if (!g_lock)
- throw runtime_error ("ShibTargetConfig not pre-initialized");
-
- if (!app_name)
- throw runtime_error ("No Application name");
- Lock lock(g_lock);
-
- if (g_Config) {
- g_Config->ref();
- return *g_Config;
- }
+// 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 MemoryListenerFactory;
- g_Config = new STConfig(app_name, inifile);
- g_Config->init();
- return *g_Config;
-}
+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()
{
- if (!g_Config)
- throw SAMLException("ShibTargetConfig::getConfig() called with NULL configuration");
- return *g_Config;
+ return g_Config;
}
-ShibTargetConfig::~ShibTargetConfig()
+bool STConfig::init(const char* schemadir)
{
-#ifdef WIN32
-#else
- if (m_SocketName) free(m_SocketName);
+#ifdef _DEBUG
+ xmltooling::NDC ndc("init");
#endif
-}
-
-/****************************************************************************/
-// STConfig
-
-STConfig::STConfig(const char* app_name, const char* inifile)
- : samlConf(SAMLConfig::getConfig()), shibConf(ShibConfig::getConfig()),
- m_app_name(app_name)
-{
- try {
- ini = new ShibINI((inifile ? inifile : SHIBTARGET_INIFILE));
- } catch (...) {
- cerr << "Unable to load the INI file: " <<
- (inifile ? inifile : SHIBTARGET_INIFILE) << endl;
- throw;
- }
-}
-
-extern "C" SAMLAttribute* ScopedFactory(DOMElement* e)
-{
- return new ScopedAttribute(e);
-}
+ Category& log = Category::getInstance(SHIBT_LOGCAT".Config");
-extern "C" SAMLAttribute* SimpleFactory(DOMElement* e)
-{
- return new SimpleAttribute(e);
-}
-
-void STConfig::init()
-{
- string app = m_app_name;
- string tag;
-
- // Initialize Log4cpp
- if (ini->get_tag (app, SHIBTARGET_TAG_LOGGER, true, &tag)) {
- cerr << "Trying to load logger configuration: " << tag << "\n";
- try {
- PropertyConfigurator::configure(tag);
- } catch (ConfigureFailure& e) {
- cerr << "Error reading configuration: " << e.what() << "\n";
+ if (!schemadir) {
+ log.fatal("XML schema directory not supplied");
+ return false;
}
- } else {
- Category& category = Category::getRoot();
- category.setPriority(log4cpp::Priority::DEBUG);
- cerr << "No logger configuration found\n";
- }
-
- Category& log = Category::getInstance("shibtarget.STConfig");
-
- saml::NDC ndc("STConfig::init");
-
- // Init SAML Configuration
- if (ini->get_tag (app, SHIBTARGET_TAG_SAMLCOMPAT, true, &tag))
- samlConf.compatibility_mode = ShibINI::boolean(tag);
- if (ini->get_tag (app, SHIBTARGET_TAG_SCHEMAS, true, &tag))
- samlConf.schema_dir = tag;
- // Init SAML Binding Configuration
- if (ini->get_tag (app, SHIBTARGET_TAG_AATIMEOUT, true, &tag))
- samlConf.binding_defaults.timeout = atoi(tag.c_str());
- if (ini->get_tag (app, SHIBTARGET_TAG_AACONNECTTO, true, &tag))
- samlConf.binding_defaults.conn_timeout = atoi(tag.c_str());
- if (ini->get_tag (app, SHIBTARGET_TAG_CERTFILE, true, &tag))
- samlConf.binding_defaults.ssl_certfile = tag;
- if (ini->get_tag (app, SHIBTARGET_TAG_KEYFILE, true, &tag))
- samlConf.binding_defaults.ssl_keyfile = tag;
- if (ini->get_tag (app, SHIBTARGET_TAG_KEYPASS, true, &tag))
- samlConf.binding_defaults.ssl_keypass = tag;
- if (ini->get_tag (app, SHIBTARGET_TAG_CALIST, true, &tag))
- samlConf.binding_defaults.ssl_calist = tag;
-
- try {
+ // This will cause some extra console logging, but for now,
+ // initialize the underlying libraries.
+ SAMLConfig& samlConf=SAMLConfig::getConfig();
+ if (schemadir)
+ samlConf.schema_dir = schemadir;
if (!samlConf.init()) {
- log.fatal ("Failed to initialize SAML Library");
- throw runtime_error ("Failed to initialize SAML Library");
- } else
- log.debug ("SAML Initialized");
- } catch (...) {
- log.crit ("Died initializing SAML Library");
- throw;
- }
-
- // Init Shib
- if (ini->get_tag(app, SHIBTARGET_TAG_AAP, true, &tag))
- shibConf.aapFile=tag;
-
- if (! ini->get_tag (app, SHIBTARGET_TAG_SITES, true, &tag)) {
- log.fatal("No Sites File found in configuration");
- throw runtime_error ("No Sites File found in configuration");
- }
- shibConf.mapperFile=tag;
-
- try {
- if (!shibConf.init()) {
- log.fatal ("Failed to initialize Shib library");
- throw runtime_error ("Failed to initialize Shib Library");
- } else
- log.debug ("Shib Initialized");
- } catch (...) {
- log.crit ("Failed initializing Shib library.");
- throw;
- }
-
- // Load any SAML extensions
- string ext = "extensions:saml";
- if (ini->exists(ext)) {
- saml::NDC ndc("load_extensions");
- ShibINI::Iterator* iter = ini->tag_iterator(ext);
-
- for (const string* str = iter->begin(); str; str = iter->next()) {
- string file = ini->get(ext, *str);
- try
- {
- samlConf.saml_register_extension(file.c_str(),ini);
- log.debug("%s: loading %s", str->c_str(), file.c_str());
- }
- catch (SAMLException& e)
- {
- log.crit("%s: %s", str->c_str(), e.what());
- }
+ log.fatal("failed to initialize OpenSAML1 library");
+ return false;
}
- delete iter;
- }
- // Register attributes based on built-in classes.
- if (ini->exists("attributes")) {
- log.info("registering attributes");
- ShibINI::Iterator* iter=ini->tag_iterator("attributes");
- for (const string* attrname=iter->begin(); attrname; attrname=iter->next())
- {
- const string factory=ini->get("attributes",*attrname);
- if (factory=="scoped")
- {
- auto_ptr<XMLCh> temp(XMLString::transcode(attrname->c_str()));
- SAMLAttribute::regFactory(temp.get(),shibboleth::Constants::SHIB_ATTRIBUTE_NAMESPACE_URI,&ScopedFactory);
- log.info("registered scoped attribute (%s)",attrname->c_str());
- }
- else if (factory=="simple")
- {
- auto_ptr<XMLCh> temp(XMLString::transcode(attrname->c_str()));
- SAMLAttribute::regFactory(temp.get(),shibboleth::Constants::SHIB_ATTRIBUTE_NAMESPACE_URI,&SimpleFactory);
- log.info("registered simple attribute (%s)",attrname->c_str());
- }
+ ShibConfig& shibConf=ShibConfig::getConfig();
+ if (!shibConf.init()) {
+ log.fatal("Failed to initialize Shib library");
+ samlConf.term();
+ return false;
}
- delete iter;
- }
-
- // Load SAML policies.
- if (ini->exists(SHIBTARGET_POLICIES)) {
- log.info("loading SAML policies");
- ShibINI::Iterator* iter = ini->tag_iterator(SHIBTARGET_POLICIES);
-
- for (const string* str = iter->begin(); str; str = iter->next()) {
- policies.push_back(XMLString::transcode(ini->get(SHIBTARGET_POLICIES, *str).c_str()));
+
+ SPConfig& conf=SPConfig::getConfig();
+ if (!SPConfig::getConfig().init(NULL)) {
+ log.fatal("Failed to initialize SP library");
+ shibConf.term();
+ samlConf.term();
+ return false;
}
- delete iter;
- }
-
- // Initialize the SHAR Cache
- if (!strcmp (app.c_str(), SHIBTARGET_SHAR)) {
- const char * cache_type = NULL;
- if (ini->get_tag (app, SHIBTARGET_TAG_CACHETYPE, true, &tag))
- cache_type = tag.c_str();
-
- g_shibTargetCCache = CCache::getInstance(cache_type);
- }
- string sockname=ini->get(SHIBTARGET_GENERAL, "sharsocket");
-#ifdef WIN32
- if (sockname.length()>0)
- m_SocketName=atoi(sockname.c_str());
- else
- m_SocketName=SHIB_SHAR_SOCKET;
-#else
- if (sockname.length()>0)
- m_SocketName=strdup(sockname.c_str());
- else
- m_SocketName=strdup(SHIB_SHAR_SOCKET);
-#endif
+ // Register built-in plugin types.
+ conf.ServiceProviderManager.registerFactory(XML_SERVICE_PROVIDER, XMLServiceProviderFactory);
- ref();
- log.debug("finished");
+ 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;
}
-STConfig::~STConfig()
+bool STConfig::load(const char* config)
{
- for (vector<const XMLCh*>::iterator i=policies.begin(); i!=policies.end(); i++)
- delete const_cast<XMLCh*>(*i);
-
- // Unregister attributes based on built-in classes.
- if (ini && ini->exists("attributes")) {
- ShibINI::Iterator* iter=ini->tag_iterator("attributes");
- for (const string* attrname=iter->begin(); attrname; attrname=iter->next())
- {
- const string factory=ini->get("attributes",*attrname);
- if (factory=="scoped")
- {
- auto_ptr<XMLCh> temp(XMLString::transcode(attrname->c_str()));
- SAMLAttribute::unregFactory(temp.get(),shibboleth::Constants::SHIB_ATTRIBUTE_NAMESPACE_URI);
- }
- else if (factory=="simple")
- {
- auto_ptr<XMLCh> temp(XMLString::transcode(attrname->c_str()));
- SAMLAttribute::unregFactory(temp.get(),shibboleth::Constants::SHIB_ATTRIBUTE_NAMESPACE_URI);
- }
- }
- delete iter;
- }
+#ifdef _DEBUG
+ xmltooling::NDC ndc("load");
+#endif
+ Category& log = Category::getInstance(SHIBT_LOGCAT".Config");
- if (ini) delete ini;
-
- if (g_shibTargetCCache)
- delete g_shibTargetCCache;
+ if (!config) {
+ log.fatal("path to configuration file not supplied");
+ shutdown();
+ return false;
+ }
- shibConf.term();
- samlConf.term();
-}
+ try {
+ log.info("loading configuration file: %s", config);
+ static const XMLCh path[] = UNICODE_LITERAL_4(p,a,t,h);
+ DOMImplementation* impl=DOMImplementationRegistry::getDOMImplementation(NULL);
+ DOMDocument* dummydoc=impl->createDocument();
+ xmltooling::XercesJanitor<DOMDocument> docjanitor(dummydoc);
+ DOMElement* dummy = dummydoc->createElementNS(NULL,path);
+
+ auto_ptr_XMLCh src(config);
+ 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=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 = xmltooling::Mutex::create();
+ }
+ catch (SAMLException& ex) {
+ log.fatal("caught exception while loading/initializing configuration: %s",ex.what());
+ shutdown();
+ return false;
+ }
+#ifndef _DEBUG
+ catch (...) {
+ log.fatal("caught exception while loading/initializing configuration");
+ shutdown();
+ return false;
+ }
+#endif
-void STConfig::ref()
-{
- refcount++;
+ log.info("finished loading configuration");
+ return true;
}
void STConfig::shutdown()
{
- refcount--;
- if (!refcount) {
- delete g_Config;
- g_Config = NULL;
- }
+#ifdef _DEBUG
+ xmltooling::NDC ndc("shutdown");
+#endif
+ Category& log = Category::getInstance(SHIBT_LOGCAT".Config");
+ log.info("shutting down the library");
+ delete m_tranLogLock;
+ m_tranLogLock = NULL;
+ //delete m_tranLog; // This is crashing for some reason, but we're shutting down anyway.
+ SPConfig::getConfig().term();
+ ShibConfig::getConfig().term();
+ SAMLConfig::getConfig().term();
+ log.info("library shutdown complete");
}