Draft of 2.0 config schema/file, removed legacy support, validate config.
[shibboleth/cpp-sp.git] / shibsp / impl / XMLServiceProvider.cpp
index b2b87fb..faa71d7 100644 (file)
@@ -51,7 +51,7 @@
 #include <xmltooling/util/ReplayCache.h>\r
 \r
 using namespace shibsp;\r
-using namespace opensaml::saml1;\r
+using namespace opensaml::saml2;\r
 using namespace opensaml::saml2md;\r
 using namespace opensaml;\r
 using namespace xmltooling;\r
@@ -59,7 +59,7 @@ using namespace log4cpp;
 using namespace std;\r
 using xmlsignature::CredentialResolver;\r
 \r
-namespace shibsp {\r
+namespace {\r
 \r
 #if defined (_MSC_VER)\r
     #pragma warning( push )\r
@@ -124,7 +124,7 @@ namespace shibsp {
 \r
         // maps binding strings to supporting consumer service(s)\r
 #ifdef HAVE_GOOD_STL\r
-        typedef map<xmltooling::xstring,vector<const Handler*> > ACSBindingMap;\r
+        typedef map<xstring,vector<const Handler*> > ACSBindingMap;\r
 #else\r
         typedef map<string,vector<const Handler*> > ACSBindingMap;\r
 #endif\r
@@ -138,7 +138,7 @@ namespace shibsp {
 \r
         DOMPropertySet* m_credDefault;\r
 #ifdef HAVE_GOOD_STL\r
-        map<xmltooling::xstring,PropertySet*> m_credMap;\r
+        map<xstring,PropertySet*> m_credMap;\r
 #else\r
         map<const XMLCh*,PropertySet*> m_credMap;\r
 #endif\r
@@ -155,6 +155,8 @@ namespace shibsp {
         RequestMapper* m_requestMapper;\r
         map<string,Application*> m_appmap;\r
         map<string,CredentialResolver*> m_credResolverMap;\r
+        map< string,vector<const SecurityPolicyRule*> > m_policyMap;\r
+        string m_policyDefault;\r
         \r
         // Provides filter to exclude special config elements.\r
         short acceptNode(const DOMNode* node) const;\r
@@ -174,7 +176,7 @@ namespace shibsp {
     {\r
     public:\r
         XMLConfig(const DOMElement* e)\r
-            : ReloadableXMLFile(e), m_impl(NULL), m_listener(NULL), m_sessionCache(NULL) {\r
+            : ReloadableXMLFile(e), m_impl(NULL), m_listener(NULL), m_sessionCache(NULL), m_tranLog(NULL) {\r
         }\r
         \r
         void init() {\r
@@ -196,7 +198,7 @@ namespace shibsp {
         pair<bool,const XMLCh*> getXMLString(const char* name, const char* ns=NULL) const {return m_impl->getXMLString(name,ns);}\r
         pair<bool,unsigned int> getUnsignedInt(const char* name, const char* ns=NULL) const {return m_impl->getUnsignedInt(name,ns);}\r
         pair<bool,int> getInt(const char* name, const char* ns=NULL) const {return m_impl->getInt(name,ns);}\r
-        const PropertySet* getPropertySet(const char* name, const char* ns="urn:mace:shibboleth:target:config:1.0") const {return m_impl->getPropertySet(name,ns);}\r
+        const PropertySet* getPropertySet(const char* name, const char* ns="urn:mace:shibboleth:sp:config:2.0") const {return m_impl->getPropertySet(name,ns);}\r
         const DOMElement* getElement() const {return m_impl->getElement();}\r
 \r
         // ServiceProvider\r
@@ -247,6 +249,15 @@ namespace shibsp {
             return NULL;\r
         }\r
 \r
+        vector<const SecurityPolicyRule*>& getPolicyRules(const Handler& handler) const {\r
+            pair<bool,const char*> pid = handler.getString("policyId", "urn:mace:shibboleth:sp:config:2.0");\r
+            if (!pid.first)\r
+                pid.second = m_impl->m_policyDefault.c_str();\r
+            if (m_impl->m_policyMap.count(pid.second))\r
+                return m_impl->m_policyMap[pid.second];\r
+            throw ConfigurationException("Security Policy ($1) not found, check <SecurityPolicies> element.", params(1,pid.second));\r
+        }\r
+\r
     protected:\r
         pair<bool,DOMElement*> load();\r
 \r
@@ -263,46 +274,45 @@ namespace shibsp {
     #pragma warning( pop )\r
 #endif\r
 \r
-    ServiceProvider* XMLServiceProviderFactory(const DOMElement* const & e)\r
-    {\r
-        return new XMLConfig(e);\r
-    }\r
-\r
-    static const XMLCh AAPProvider[] =          UNICODE_LITERAL_11(A,A,P,P,r,o,v,i,d,e,r);\r
     static const XMLCh _Application[] =         UNICODE_LITERAL_11(A,p,p,l,i,c,a,t,i,o,n);\r
     static const XMLCh Applications[] =         UNICODE_LITERAL_12(A,p,p,l,i,c,a,t,i,o,n,s);\r
-    static const XMLCh AttributeFactory[] =     UNICODE_LITERAL_16(A,t,t,r,i,b,u,t,e,F,a,c,t,o,r,y);\r
     static const XMLCh Credentials[] =          UNICODE_LITERAL_11(C,r,e,d,e,n,t,i,a,l,s);\r
-    static const XMLCh CredentialsProvider[] =  UNICODE_LITERAL_19(C,r,e,d,e,n,t,i,a,l,s,P,r,o,v,i,d,e,r);\r
     static const XMLCh CredentialUse[] =        UNICODE_LITERAL_13(C,r,e,d,e,n,t,i,a,l,U,s,e);\r
-    static const XMLCh DiagnosticService[] =    UNICODE_LITERAL_17(D,i,a,g,n,o,s,t,i,c,S,e,r,v,i,c,e);\r
+    static const XMLCh _default[] =             UNICODE_LITERAL_7(d,e,f,a,u,l,t);\r
     static const XMLCh fatal[] =                UNICODE_LITERAL_5(f,a,t,a,l);\r
-    static const XMLCh FileResolver[] =         UNICODE_LITERAL_12(F,i,l,e,R,e,s,o,l,v,e,r);\r
-    static const XMLCh Global[] =               UNICODE_LITERAL_6(G,l,o,b,a,l);\r
-    static const XMLCh Id[] =                   UNICODE_LITERAL_2(I,d);\r
+    static const XMLCh _Handler[] =             UNICODE_LITERAL_7(H,a,n,d,l,e,r);\r
+    static const XMLCh _id[] =                  UNICODE_LITERAL_2(i,d);\r
     static const XMLCh Implementation[] =       UNICODE_LITERAL_14(I,m,p,l,e,m,e,n,t,a,t,i,o,n);\r
     static const XMLCh InProcess[] =            UNICODE_LITERAL_9(I,n,P,r,o,c,e,s,s);\r
     static const XMLCh Library[] =              UNICODE_LITERAL_7(L,i,b,r,a,r,y);\r
     static const XMLCh Listener[] =             UNICODE_LITERAL_8(L,i,s,t,e,n,e,r);\r
-    static const XMLCh Local[] =                UNICODE_LITERAL_5(L,o,c,a,l);\r
     static const XMLCh logger[] =               UNICODE_LITERAL_6(l,o,g,g,e,r);\r
     static const XMLCh MemoryListener[] =       UNICODE_LITERAL_14(M,e,m,o,r,y,L,i,s,t,e,n,e,r);\r
-    static const XMLCh MemorySessionCache[] =   UNICODE_LITERAL_18(M,e,m,o,r,y,S,e,s,s,i,o,n,C,a,c,h,e);\r
+    static const XMLCh Policy[] =               UNICODE_LITERAL_6(P,o,l,i,c,y);\r
     static const XMLCh RelyingParty[] =         UNICODE_LITERAL_12(R,e,l,y,i,n,g,P,a,r,t,y);\r
     static const XMLCh _ReplayCache[] =         UNICODE_LITERAL_11(R,e,p,l,a,y,C,a,c,h,e);\r
-    static const XMLCh RequestMapProvider[] =   UNICODE_LITERAL_18(R,e,q,u,e,s,t,M,a,p,P,r,o,v,i,d,e,r);\r
+    static const XMLCh _RequestMapper[] =       UNICODE_LITERAL_13(R,e,q,u,e,s,t,M,a,p,p,e,r);\r
+    static const XMLCh Rule[] =                 UNICODE_LITERAL_4(R,u,l,e);\r
+    static const XMLCh SecurityPolicies[] =     UNICODE_LITERAL_16(S,e,c,u,r,i,t,y,P,o,l,i,c,i,e,s);\r
     static const XMLCh _SessionCache[] =        UNICODE_LITERAL_12(S,e,s,s,i,o,n,C,a,c,h,e);\r
     static const XMLCh SessionInitiator[] =     UNICODE_LITERAL_16(S,e,s,s,i,o,n,I,n,i,t,i,a,t,o,r);\r
     static const XMLCh _StorageService[] =      UNICODE_LITERAL_14(S,t,o,r,a,g,e,S,e,r,v,i,c,e);\r
     static const XMLCh OutOfProcess[] =         UNICODE_LITERAL_12(O,u,t,O,f,P,r,o,c,e,s,s);\r
     static const XMLCh TCPListener[] =          UNICODE_LITERAL_11(T,C,P,L,i,s,t,e,n,e,r);\r
-    static const XMLCh TrustProvider[] =        UNICODE_LITERAL_13(T,r,u,s,t,P,r,o,v,i,d,e,r);\r
+    static const XMLCh _TrustEngine[] =         UNICODE_LITERAL_11(T,r,u,s,t,E,n,g,i,n,e);\r
     static const XMLCh UnixListener[] =         UNICODE_LITERAL_12(U,n,i,x,L,i,s,t,e,n,e,r);\r
     static const XMLCh _MetadataProvider[] =    UNICODE_LITERAL_16(M,e,t,a,d,a,t,a,P,r,o,v,i,d,e,r);\r
     static const XMLCh _path[] =                UNICODE_LITERAL_4(p,a,t,h);\r
     static const XMLCh _type[] =                UNICODE_LITERAL_4(t,y,p,e);\r
 };\r
 \r
+namespace shibsp {\r
+    ServiceProvider* XMLServiceProviderFactory(const DOMElement* const & e)\r
+    {\r
+        return new XMLConfig(e);\r
+    }\r
+};\r
+\r
 XMLApplication::XMLApplication(\r
     const ServiceProvider* sp,\r
     const DOMElement* e,\r
@@ -317,18 +327,7 @@ XMLApplication::XMLApplication(
 \r
     try {\r
         // First load any property sets.\r
-        map<string,string> root_remap;\r
-        root_remap["shire"]="session";\r
-        root_remap["shireURL"]="handlerURL";\r
-        root_remap["shireSSL"]="handlerSSL";\r
-        load(e,log,this,&root_remap);\r
-\r
-        const PropertySet* propcheck=getPropertySet("Errors");\r
-        if (propcheck && !propcheck->getString("session").first)\r
-            throw ConfigurationException("<Errors> element requires 'session' (or deprecated 'shire') attribute");\r
-        propcheck=getPropertySet("Sessions");\r
-        if (propcheck && !propcheck->getString("handlerURL").first)\r
-            throw ConfigurationException("<Sessions> element requires 'handlerURL' (or deprecated 'shireURL') attribute");\r
+        load(e,log,this);\r
 \r
         SPConfig& conf=SPConfig::getConfig();\r
         SAMLConfig& samlConf=SAMLConfig::getConfig();\r
@@ -338,10 +337,12 @@ XMLApplication::XMLApplication(
         m_hash+=getString("providerId").second;\r
         m_hash=samlConf.hashSHA1(m_hash.c_str(), true);\r
 \r
+        const PropertySet* sessions = getPropertySet("Sessions");\r
+\r
         // Process handlers.\r
         Handler* handler=NULL;\r
         bool hardACS=false, hardSessionInit=false;\r
-        const DOMElement* child = XMLHelper::getFirstChildElement(propcheck->getElement());\r
+        const DOMElement* child = sessions ? XMLHelper::getFirstChildElement(sessions->getElement()) : NULL;\r
         while (child) {\r
             try {\r
                 // A handler is based on the Binding property in conjunction with the element name.\r
@@ -443,30 +444,7 @@ XMLApplication::XMLApplication(
             child = XMLHelper::getNextSiblingElement(child);\r
         }\r
 \r
-        // If no handlers defined at the root, assume a legacy configuration.\r
-        if (!m_base && m_handlers.empty()) {\r
-            try {\r
-                // A legacy config installs a SAML POST handler at the root handler location.\r
-                // We use the Sessions element itself as the PropertySet.\r
-                Handler* h1=conf.SessionInitiatorManager.newPlugin(\r
-                    shibspconstants::SHIB1_SESSIONINIT_PROFILE_URI,propcheck->getElement()\r
-                    );\r
-                m_handlers.push_back(h1);\r
-                m_sessionInitDefault=h1;\r
-\r
-                Handler* h2=conf.AssertionConsumerServiceManager.newPlugin(\r
-                    samlconstants::SAML1_PROFILE_BROWSER_POST,propcheck->getElement()\r
-                    );\r
-                m_handlers.push_back(h2);\r
-                m_handlerMap[""] = h2;\r
-                m_acsDefault=h2;\r
-            }\r
-            catch (exception& ex) {\r
-                log.crit("error installing legacy handler configuration: %s", ex.what());\r
-            }\r
-        }\r
-        \r
-        DOMNodeList* nlist=e->getElementsByTagNameNS(samlconstants::SAML1_NS,Audience::LOCAL_NAME);\r
+        DOMNodeList* nlist=e->getElementsByTagNameNS(samlconstants::SAML20_NS,Audience::LOCAL_NAME);\r
         for (XMLSize_t i=0; nlist && i<nlist->getLength(); i++)\r
             if (nlist->item(i)->getParentNode()->isSameNode(e) && nlist->item(i)->hasChildNodes())\r
                 m_audiences.push_back(nlist->item(i)->getFirstChild()->getNodeValue());\r
@@ -475,78 +453,36 @@ XMLApplication::XMLApplication(
         m_audiences.push_back(getXMLString("providerId").second);\r
 \r
         if (conf.isEnabled(SPConfig::AttributeResolver)) {\r
-            child = XMLHelper::getFirstChildElement(e,AAPProvider);\r
-            while (child) {\r
-                // TODO: some kind of compatibility\r
-                child = XMLHelper::getNextSiblingElement(child,AAPProvider);\r
-            }\r
+            // TODO\r
         }\r
 \r
         if (conf.isEnabled(SPConfig::Metadata)) {\r
-            vector<MetadataProvider*> os2providers;\r
             child = XMLHelper::getFirstChildElement(e,_MetadataProvider);\r
-            while (child) {\r
+            if (child) {\r
                 auto_ptr_char type(child->getAttributeNS(NULL,_type));\r
                 log.info("building metadata provider of type %s...",type.get());\r
                 try {\r
                     auto_ptr<MetadataProvider> mp(samlConf.MetadataProviderManager.newPlugin(type.get(),child));\r
                     mp->init();\r
-                    os2providers.push_back(mp.release());\r
+                    m_metadata = mp.release();\r
                 }\r
                 catch (exception& ex) {\r
                     log.crit("error building/initializing metadata provider: %s", ex.what());\r
                 }\r
-\r
-                child = XMLHelper::getNextSiblingElement(child,_MetadataProvider);\r
-            }\r
-            \r
-            if (os2providers.size()==1)\r
-                m_metadata=os2providers.front();\r
-            else if (os2providers.size()>1) {\r
-                try {\r
-                    m_metadata = samlConf.MetadataProviderManager.newPlugin(CHAINING_METADATA_PROVIDER,NULL);\r
-                    ChainingMetadataProvider* chainMeta = dynamic_cast<ChainingMetadataProvider*>(m_metadata);\r
-                    while (!os2providers.empty()) {\r
-                        chainMeta->addMetadataProvider(os2providers.back());\r
-                        os2providers.pop_back();\r
-                    }\r
-                }\r
-                catch (exception& ex) {\r
-                    log.crit("error building chaining metadata provider wrapper: %s",ex.what());\r
-                    for_each(os2providers.begin(), os2providers.end(), xmltooling::cleanup<MetadataProvider>());\r
-                }\r
             }\r
         }\r
 \r
         if (conf.isEnabled(SPConfig::Trust)) {\r
-            ChainingTrustEngine* chainTrust = NULL;\r
-            child = XMLHelper::getFirstChildElement(e,TrustProvider);\r
-            while (child) {\r
+            child = XMLHelper::getFirstChildElement(e,_TrustEngine);\r
+            if (child) {\r
                 auto_ptr_char type(child->getAttributeNS(NULL,_type));\r
-                log.info("building trust provider of type %s...",type.get());\r
+                log.info("building trust engine of type %s...",type.get());\r
                 try {\r
-                    if (!m_trust) {\r
-                        // For compatibility with old engine types, we're assuming a Shib engine is likely,\r
-                        // which requires chaining, so we'll build that regardless.\r
-                        m_trust = xmlConf.TrustEngineManager.newPlugin(CHAINING_TRUSTENGINE,NULL);\r
-                        chainTrust = dynamic_cast<ChainingTrustEngine*>(m_trust);\r
-                    }\r
-                    if (!strcmp(type.get(),"edu.internet2.middleware.shibboleth.common.provider.ShibbolethTrust")) {\r
-                        chainTrust->addTrustEngine(xmlConf.TrustEngineManager.newPlugin(EXPLICIT_KEY_TRUSTENGINE,child));\r
-                        chainTrust->addTrustEngine(xmlConf.TrustEngineManager.newPlugin(SHIBBOLETH_PKIX_TRUSTENGINE,child));\r
-                    }\r
-                    else if (!strcmp(type.get(),"edu.internet2.middleware.shibboleth.common.provider.BasicTrust")) {\r
-                        chainTrust->addTrustEngine(xmlConf.TrustEngineManager.newPlugin(EXPLICIT_KEY_TRUSTENGINE,child));\r
-                    }\r
-                    else {\r
-                        chainTrust->addTrustEngine(xmlConf.TrustEngineManager.newPlugin(type.get(),child));\r
-                    }\r
+                    m_trust = xmlConf.TrustEngineManager.newPlugin(type.get(),child);\r
                 }\r
                 catch (exception& ex) {\r
-                    log.crit("error building trust provider: %s",ex.what());\r
+                    log.crit("error building trust engine: %s",ex.what());\r
                 }\r
-    \r
-                child = XMLHelper::getNextSiblingElement(child,TrustProvider);\r
             }\r
         }\r
         \r
@@ -587,7 +523,7 @@ void XMLApplication::cleanup()
     \r
     delete m_credDefault;\r
 #ifdef HAVE_GOOD_STL\r
-    for_each(m_credMap.begin(),m_credMap.end(),cleanup_pair<xmltooling::xstring,PropertySet>());\r
+    for_each(m_credMap.begin(),m_credMap.end(),cleanup_pair<xstring,PropertySet>());\r
 #else\r
     for_each(m_credMap.begin(),m_credMap.end(),cleanup_pair<const XMLCh*,PropertySet>());\r
 #endif\r
@@ -598,23 +534,20 @@ void XMLApplication::cleanup()
 \r
 short XMLApplication::acceptNode(const DOMNode* node) const\r
 {\r
-    if (XMLHelper::isNodeNamed(node,samlconstants::SAML1_NS,AttributeDesignator::LOCAL_NAME))\r
+    if (XMLHelper::isNodeNamed(node,samlconstants::SAML20_NS,opensaml::saml2::Attribute::LOCAL_NAME))\r
         return FILTER_REJECT;\r
-    else if (XMLHelper::isNodeNamed(node,samlconstants::SAML20_NS,opensaml::saml1::Attribute::LOCAL_NAME))\r
-        return FILTER_REJECT;\r
-    else if (XMLHelper::isNodeNamed(node,samlconstants::SAML1_NS,Audience::LOCAL_NAME))\r
+    else if (XMLHelper::isNodeNamed(node,samlconstants::SAML20_NS,Audience::LOCAL_NAME))\r
         return FILTER_REJECT;\r
     const XMLCh* name=node->getLocalName();\r
     if (XMLString::equals(name,_Application) ||\r
         XMLString::equals(name,AssertionConsumerService::LOCAL_NAME) ||\r
         XMLString::equals(name,SingleLogoutService::LOCAL_NAME) ||\r
-        XMLString::equals(name,DiagnosticService) ||\r
+        XMLString::equals(name,ManageNameIDService::LOCAL_NAME) ||\r
         XMLString::equals(name,SessionInitiator) ||\r
-        XMLString::equals(name,AAPProvider) ||\r
         XMLString::equals(name,CredentialUse) ||\r
         XMLString::equals(name,RelyingParty) ||\r
         XMLString::equals(name,_MetadataProvider) ||\r
-        XMLString::equals(name,TrustProvider))\r
+        XMLString::equals(name,_TrustEngine))\r
         return FILTER_REJECT;\r
 \r
     return FILTER_ACCEPT;\r
@@ -689,7 +622,7 @@ const PropertySet* XMLApplication::getCredentialUse(const EntityDescriptor* prov
         return m_base->getCredentialUse(provider);\r
         \r
 #ifdef HAVE_GOOD_STL\r
-    map<xmltooling::xstring,PropertySet*>::const_iterator i=m_credMap.find(provider->getEntityID());\r
+    map<xstring,PropertySet*>::const_iterator i=m_credMap.find(provider->getEntityID());\r
     if (i!=m_credMap.end())\r
         return i->second;\r
     const EntitiesDescriptor* group=dynamic_cast<const EntitiesDescriptor*>(provider->getParent());\r
@@ -767,20 +700,17 @@ const Handler* XMLApplication::getHandler(const char* path) const
 \r
 short XMLConfigImpl::acceptNode(const DOMNode* node) const\r
 {\r
-    if (!XMLString::equals(node->getNamespaceURI(),shibspconstants::SHIB1SPCONFIG_NS) &&\r
-        !XMLString::equals(node->getNamespaceURI(),shibspconstants::SHIB2SPCONFIG_NS))\r
+    if (!XMLString::equals(node->getNamespaceURI(),shibspconstants::SHIB2SPCONFIG_NS))\r
         return FILTER_ACCEPT;\r
     const XMLCh* name=node->getLocalName();\r
     if (XMLString::equals(name,Applications) ||\r
-        XMLString::equals(name,AttributeFactory) ||\r
         XMLString::equals(name,Credentials) ||\r
-        XMLString::equals(name,CredentialsProvider) ||\r
         XMLString::equals(name,Extensions::LOCAL_NAME) ||\r
         XMLString::equals(name,Implementation) ||\r
         XMLString::equals(name,Listener) ||\r
         XMLString::equals(name,MemoryListener) ||\r
-        XMLString::equals(name,MemorySessionCache) ||\r
-        XMLString::equals(name,RequestMapProvider) ||\r
+        XMLString::equals(name,Policy) ||\r
+        XMLString::equals(name,_RequestMapper) ||\r
         XMLString::equals(name,_ReplayCache) ||\r
         XMLString::equals(name,_SessionCache) ||\r
         XMLString::equals(name,_StorageService) ||\r
@@ -819,7 +749,7 @@ void XMLConfigImpl::doExtensions(const DOMElement* e, const char* label, Categor
     }\r
 }\r
 \r
-XMLConfigImpl::XMLConfigImpl(const DOMElement* e, bool first, const XMLConfig* outer) : m_outer(outer), m_document(NULL), m_requestMapper(NULL)\r
+XMLConfigImpl::XMLConfigImpl(const DOMElement* e, bool first, const XMLConfig* outer) : m_requestMapper(NULL), m_outer(outer), m_document(NULL)\r
 {\r
 #ifdef _DEBUG\r
     xmltooling::NDC ndc("XMLConfigImpl");\r
@@ -828,13 +758,10 @@ XMLConfigImpl::XMLConfigImpl(const DOMElement* e, bool first, const XMLConfig* o
 \r
     try {\r
         SPConfig& conf=SPConfig::getConfig();\r
+        SAMLConfig& samlConf=SAMLConfig::getConfig();\r
         XMLToolingConfig& xmlConf=XMLToolingConfig::getConfig();\r
         const DOMElement* SHAR=XMLHelper::getFirstChildElement(e,OutOfProcess);\r
-        if (!SHAR)\r
-            SHAR=XMLHelper::getFirstChildElement(e,Global);\r
         const DOMElement* SHIRE=XMLHelper::getFirstChildElement(e,InProcess);\r
-        if (!SHIRE)\r
-            SHIRE=XMLHelper::getFirstChildElement(e,Local);\r
 \r
         // Initialize log4cpp manually in order to redirect log messages as soon as possible.\r
         if (conf.isEnabled(SPConfig::Logging)) {\r
@@ -856,10 +783,7 @@ XMLConfigImpl::XMLConfigImpl(const DOMElement* e, bool first, const XMLConfig* o
         }\r
         \r
         // First load any property sets.\r
-        map<string,string> root_remap;\r
-        root_remap["Global"]="OutOfProcess";\r
-        root_remap["Local"]="InProcess";\r
-        load(e,log,this,&root_remap);\r
+        load(e,log,this);\r
 \r
         const DOMElement* child;\r
         string plugtype;\r
@@ -913,17 +837,13 @@ XMLConfigImpl::XMLConfigImpl(const DOMElement* e, bool first, const XMLConfig* o
             }\r
 \r
             if (conf.isEnabled(SPConfig::Caching)) {\r
-                // TODO: This code's a mess, due to a very bad config layout for the caches...\r
-                // Needs rework with the new config file.\r
-                const DOMElement* container=conf.isEnabled(SPConfig::OutOfProcess) ? SHAR : SHIRE;\r
-\r
-                // First build any StorageServices.\r
-                string inmemID;\r
-                child=XMLHelper::getFirstChildElement(container,_StorageService);\r
-                while (child) {\r
-                    auto_ptr_char id(child->getAttributeNS(NULL,Id));\r
-                    auto_ptr_char type(child->getAttributeNS(NULL,_type));\r
-                    if (id.get() && type.get()) {\r
+                if (conf.isEnabled(SPConfig::OutOfProcess)) {\r
+                    // First build any StorageServices.\r
+                    string inmemID;\r
+                    child=XMLHelper::getFirstChildElement(SHAR,_StorageService);\r
+                    while (child) {\r
+                        auto_ptr_char id(child->getAttributeNS(NULL,_id));\r
+                        auto_ptr_char type(child->getAttributeNS(NULL,_type));\r
                         try {\r
                             log.info("building StorageService (%s) of type %s...", id.get(), type.get());\r
                             m_outer->m_storage[id.get()] = xmlConf.StorageServiceManager.newPlugin(type.get(),child);\r
@@ -933,117 +853,93 @@ XMLConfigImpl::XMLConfigImpl(const DOMElement* e, bool first, const XMLConfig* o
                         catch (exception& ex) {\r
                             log.crit("failed to instantiate StorageService (%s): %s", id.get(), ex.what());\r
                         }\r
+                        child=XMLHelper::getNextSiblingElement(child,_StorageService);\r
                     }\r
-                    child=XMLHelper::getNextSiblingElement(container,_StorageService);\r
-                }\r
                 \r
-                child=XMLHelper::getFirstChildElement(container,_SessionCache);\r
-                if (child) {\r
-                    auto_ptr_char type(child->getAttributeNS(NULL,_type));\r
-                    log.info("building Session Cache of type %s...",type.get());\r
-                    m_outer->m_sessionCache=conf.SessionCacheManager.newPlugin(type.get(),child);\r
-                }\r
-                else if (conf.isEnabled(SPConfig::OutOfProcess)) {\r
-                    log.warn("custom SessionCache unspecified or no longer supported, building SessionCache of type %s...",STORAGESERVICE_SESSION_CACHE);\r
-                    if (inmemID.empty()) {\r
-                        inmemID = "memory";\r
-                        log.info("no StorageServices configured, providing in-memory version for legacy config");\r
-                        m_outer->m_storage[inmemID] = xmlConf.StorageServiceManager.newPlugin(MEMORY_STORAGE_SERVICE,NULL);\r
+                    child=XMLHelper::getFirstChildElement(SHAR,_SessionCache);\r
+                    if (child) {\r
+                        auto_ptr_char type(child->getAttributeNS(NULL,_type));\r
+                        log.info("building SessionCache of type %s...",type.get());\r
+                        m_outer->m_sessionCache=conf.SessionCacheManager.newPlugin(type.get(),child);\r
+                    }\r
+                    else {\r
+                        log.warn("SessionCache unspecified, building SessionCache of type %s...",STORAGESERVICE_SESSION_CACHE);\r
+                        if (inmemID.empty()) {\r
+                            inmemID = "memory";\r
+                            log.info("no StorageServices configured, providing in-memory version for session cache");\r
+                            m_outer->m_storage[inmemID] = xmlConf.StorageServiceManager.newPlugin(MEMORY_STORAGE_SERVICE,NULL);\r
+                        }\r
+                        child = e->getOwnerDocument()->createElementNS(NULL,_SessionCache);\r
+                        auto_ptr_XMLCh ssid(inmemID.c_str());\r
+                        const_cast<DOMElement*>(child)->setAttributeNS(NULL,_StorageService,ssid.get());\r
+                        m_outer->m_sessionCache=conf.SessionCacheManager.newPlugin(STORAGESERVICE_SESSION_CACHE,child);\r
+                    }\r
+\r
+                    // Replay cache.\r
+                    StorageService* replaySS=NULL;\r
+                    child=XMLHelper::getFirstChildElement(SHAR,_ReplayCache);\r
+                    if (child) {\r
+                        auto_ptr_char ssid(child->getAttributeNS(NULL,_StorageService));\r
+                        if (ssid.get() && *ssid.get()) {\r
+                            if (m_outer->m_storage.count(ssid.get()))\r
+                                replaySS = m_outer->m_storage[ssid.get()];\r
+                            if (replaySS)\r
+                                log.info("building ReplayCache on top of StorageService (%s)...", ssid.get());\r
+                            else\r
+                                log.crit("unable to locate StorageService (%s) in configuration", ssid.get());\r
+                        }\r
+                    }\r
+                    if (!replaySS) {\r
+                        log.info("building ReplayCache using in-memory StorageService...");\r
+                        if (inmemID.empty()) {\r
+                            inmemID = "memory";\r
+                            log.info("no StorageServices configured, providing in-memory version for legacy config");\r
+                            m_outer->m_storage[inmemID] = xmlConf.StorageServiceManager.newPlugin(MEMORY_STORAGE_SERVICE,NULL);\r
+                        }\r
+                        replaySS = m_outer->m_storage[inmemID];\r
                     }\r
-                    child = container->getOwnerDocument()->createElementNS(NULL,_SessionCache);\r
-                    xmltooling::auto_ptr_XMLCh ssid(inmemID.c_str());\r
-                    const_cast<DOMElement*>(child)->setAttributeNS(NULL,_StorageService,ssid.get());\r
-                    m_outer->m_sessionCache=conf.SessionCacheManager.newPlugin(STORAGESERVICE_SESSION_CACHE,child);\r
+                    xmlConf.setReplayCache(new ReplayCache(replaySS));\r
                 }\r
                 else {\r
-                    log.warn("custom SessionCache unspecified or no longer supported, building SessionCache of type %s...",REMOTED_SESSION_CACHE);\r
+                    log.info("building in-process SessionCache of type %s...",REMOTED_SESSION_CACHE);\r
                     m_outer->m_sessionCache=conf.SessionCacheManager.newPlugin(REMOTED_SESSION_CACHE,NULL);\r
                 }\r
-                \r
-                // Replay cache.\r
-                StorageService* replaySS=NULL;\r
-                child=XMLHelper::getFirstChildElement(container,_ReplayCache);\r
-                if (child) {\r
-                    auto_ptr_char ssid(child->getAttributeNS(NULL,_StorageService));\r
-                    if (ssid.get() && *ssid.get()) {\r
-                        replaySS = m_outer->m_storage[ssid.get()];\r
-                        if (replaySS)\r
-                            log.info("building ReplayCache on top of StorageService (%s)...", ssid.get());\r
-                        else\r
-                            log.crit("unable to locate StorageService (%s) in configuration", ssid.get());\r
-                    }\r
-                }\r
-                if (!replaySS) {\r
-                    log.info("building ReplayCache using in-memory StorageService...");\r
-                    if (inmemID.empty()) {\r
-                        inmemID = "memory";\r
-                        log.info("no StorageServices configured, providing in-memory version for legacy config");\r
-                        m_outer->m_storage[inmemID] = xmlConf.StorageServiceManager.newPlugin(MEMORY_STORAGE_SERVICE,NULL);\r
-                    }\r
-                    replaySS = m_outer->m_storage[inmemID];\r
-                }\r
-                xmlConf.setReplayCache(new ReplayCache(replaySS));\r
             }\r
         } // end of first-time-only stuff\r
         \r
         // Back to the fully dynamic stuff...next up is the RequestMapper.\r
         if (conf.isEnabled(SPConfig::RequestMapping)) {\r
-            child=XMLHelper::getFirstChildElement(SHIRE,RequestMapProvider);\r
+            child=XMLHelper::getFirstChildElement(SHIRE,_RequestMapper);\r
             if (child) {\r
                 auto_ptr_char type(child->getAttributeNS(NULL,_type));\r
                 log.info("building RequestMapper of type %s...",type.get());\r
                 m_requestMapper=conf.RequestMapperManager.newPlugin(type.get(),child);\r
             }\r
-            else {\r
-                log.fatal("can't build RequestMapper, missing conf:RequestMapProvider element?");\r
-                throw ConfigurationException("can't build RequestMapper, missing conf:RequestMapProvider element?");\r
-            }\r
         }\r
         \r
         // Now we load the credentials map.\r
         if (conf.isEnabled(SPConfig::Credentials)) {\r
-            // Old format was to wrap it in a CredentialsProvider plugin, we're inlining that...\r
-            child = XMLHelper::getFirstChildElement(e,CredentialsProvider);\r
-            child = XMLHelper::getFirstChildElement(child ? child : e,Credentials);\r
+            child = XMLHelper::getLastChildElement(e,Credentials);\r
             if (child) {\r
                 // Step down and process resolvers.\r
                 child=XMLHelper::getFirstChildElement(child);\r
                 while (child) {\r
-                    auto_ptr_char id(child->getAttributeNS(NULL,Id));\r
-                    if (!id.get() || !*(id.get())) {\r
-                        log.warn("skipping CredentialResolver with no Id attribute");\r
-                        child = XMLHelper::getNextSiblingElement(child);\r
-                        continue;\r
-                    }\r
-                    \r
-                    if (XMLString::equals(child->getLocalName(),FileResolver))\r
-                        plugtype=FILESYSTEM_CREDENTIAL_RESOLVER;\r
-                    else {\r
-                        auto_ptr_char c(child->getAttributeNS(NULL,_type));\r
-                        plugtype=c.get();\r
-                    }\r
-                    \r
-                    if (!plugtype.empty()) {\r
-                        try {\r
-                            CredentialResolver* cr=\r
-                                XMLToolingConfig::getConfig().CredentialResolverManager.newPlugin(plugtype.c_str(),child);\r
-                            m_credResolverMap[id.get()] = cr;\r
-                        }\r
-                        catch (exception& ex) {\r
-                            log.crit("failed to instantiate CredentialResolver (%s): %s", id.get(), ex.what());\r
-                        }\r
+                    auto_ptr_char id(child->getAttributeNS(NULL,_id));\r
+                    auto_ptr_char type(child->getAttributeNS(NULL,_type));\r
+                    try {\r
+                        CredentialResolver* cr=xmlConf.CredentialResolverManager.newPlugin(type.get(),child);\r
+                        m_credResolverMap[id.get()] = cr;\r
                     }\r
-                    else {\r
-                        log.error("unknown type of CredentialResolver with Id (%s)", id.get());\r
+                    catch (exception& ex) {\r
+                        log.crit("failed to instantiate CredentialResolver (%s): %s", id.get(), ex.what());\r
                     }\r
-                    \r
                     child = XMLHelper::getNextSiblingElement(child);\r
                 }\r
             }\r
         }\r
 \r
         // Load the default application. This actually has a fixed ID of "default". ;-)\r
-        child=XMLHelper::getFirstChildElement(e,Applications);\r
+        child=XMLHelper::getLastChildElement(e,Applications);\r
         if (!child) {\r
             log.fatal("can't build default Application object, missing conf:Applications element?");\r
             throw ConfigurationException("can't build default Application object, missing conf:Applications element?");\r
@@ -1055,13 +951,39 @@ XMLConfigImpl::XMLConfigImpl(const DOMElement* e, bool first, const XMLConfig* o
         child = XMLHelper::getFirstChildElement(child,_Application);\r
         while (child) {\r
             auto_ptr<XMLApplication> iapp(new XMLApplication(m_outer,child,defapp));\r
-            if (m_appmap.find(iapp->getId())!=m_appmap.end())\r
-                log.crit("found conf:Application element with duplicate Id attribute (%s), skipping it", iapp->getId());\r
+            if (m_appmap.count(iapp->getId()))\r
+                log.crit("found conf:Application element with duplicate id attribute (%s), skipping it", iapp->getId());\r
             else\r
                 m_appmap[iapp->getId()]=iapp.release();\r
 \r
             child = XMLHelper::getNextSiblingElement(child,_Application);\r
         }\r
+\r
+        // Load security policies.\r
+        child = XMLHelper::getLastChildElement(e,SecurityPolicies);\r
+        if (child) {\r
+            auto_ptr_char def(child->getAttributeNS(NULL,_default));\r
+            m_policyDefault = def.get();\r
+            child = XMLHelper::getFirstChildElement(child,Policy);\r
+            while (child) {\r
+                auto_ptr_char id(child->getAttributeNS(NULL,_id));\r
+                vector<const SecurityPolicyRule*>& rules = m_policyMap[id.get()];\r
+                const DOMElement* rule = XMLHelper::getFirstChildElement(child,Rule);\r
+                while (rule) {\r
+                    auto_ptr_char type(rule->getAttributeNS(NULL,_type));\r
+                    try {\r
+                        rules.push_back(samlConf.SecurityPolicyRuleManager.newPlugin(type.get(),rule));\r
+                    }\r
+                    catch (exception& ex) {\r
+                        log.crit("error instantiating policy rule (%s) in policy (%s): %s", type.get(), id.get(), ex.what());\r
+                    }\r
+                    rule = XMLHelper::getNextSiblingElement(rule,Rule);\r
+                }\r
+                child = XMLHelper::getNextSiblingElement(child,Policy);\r
+            }\r
+            if (!m_policyMap.count(m_policyDefault))\r
+                throw ConfigurationException("Default security policy ($1) not found in conf:SecurityPolicies element.", params(1,m_policyDefault.c_str()));\r
+        }\r
     }\r
     catch (exception&) {\r
         this->~XMLConfigImpl();\r
@@ -1079,6 +1001,8 @@ XMLConfigImpl::~XMLConfigImpl()
 {\r
     for_each(m_appmap.begin(),m_appmap.end(),cleanup_pair<string,Application>());\r
     for_each(m_credResolverMap.begin(),m_credResolverMap.end(),cleanup_pair<string,CredentialResolver>());\r
+    for (map< string,vector<const SecurityPolicyRule*> >::iterator i=m_policyMap.begin(); i!=m_policyMap.end(); ++i)\r
+        for_each(i->second.begin(), i->second.end(), xmltooling::cleanup<SecurityPolicyRule>());\r
     delete m_requestMapper;\r
     if (m_document)\r
         m_document->release();\r