https://bugs.internet2.edu/jira/browse/SSPCPP-282
[shibboleth/sp.git] / shibsp / handler / impl / SAML2NameIDMgmt.cpp
index 0f90abb..12c0ed6 100644 (file)
@@ -1,6 +1,6 @@
 /*
- *  Copyright 2001-2007 Internet2
- * 
+ *  Copyright 2001-2009 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
@@ -16,7 +16,7 @@
 
 /**
  * SAML2NameIDMgmt.cpp
- * 
+ *
  * Handles SAML 2.0 NameID management protocol messages.
  */
 
@@ -24,6 +24,7 @@
 #include "exceptions.h"
 #include "Application.h"
 #include "ServiceProvider.h"
+#include "SPRequest.h"
 #include "handler/AbstractHandler.h"
 #include "handler/RemotedHandler.h"
 #include "util/SPConstants.h"
 # include "security/SecurityPolicy.h"
 # include "util/TemplateParameters.h"
 # include <fstream>
+# include <saml/exceptions.h>
 # include <saml/SAMLConfig.h>
 # include <saml/saml2/core/Protocols.h>
 # include <saml/saml2/metadata/EndpointManager.h>
+# include <saml/saml2/metadata/Metadata.h>
 # include <saml/saml2/metadata/MetadataCredentialCriteria.h>
 # include <xmltooling/util/URLEncoder.h>
 using namespace opensaml::saml2;
@@ -54,7 +57,7 @@ namespace shibsp {
     #pragma warning( push )
     #pragma warning( disable : 4250 )
 #endif
-    
+
     class SHIBSP_DLLLOCAL SAML2NameIDMgmt : public AbstractHandler, public RemotedHandler
     {
     public:
@@ -68,7 +71,7 @@ namespace shibsp {
             }
 #endif
         }
-        
+
         void receive(DDF& in, ostream& out);
         pair<bool,long> run(SPRequest& request, bool isHandler=true) const;
 
@@ -93,12 +96,9 @@ namespace shibsp {
 #endif
 
     private:
-        pair<bool,long> doRequest(
-            const Application& application, const char* session_id, const HTTPRequest& httpRequest, HTTPResponse& httpResponse
-            ) const;
+        pair<bool,long> doRequest(const Application& application, const HTTPRequest& httpRequest, HTTPResponse& httpResponse) const;
 
 #ifndef SHIBSP_LITE
-        bool stronglyMatches(const XMLCh* idp, const XMLCh* sp, const saml2::NameID& n1, const saml2::NameID& n2) const;
         bool notifyBackChannel(const Application& application, const char* requestURL, const NameID& nameid, const NewID* newid) const;
 
         pair<bool,long> sendResponse(
@@ -113,7 +113,7 @@ namespace shibsp {
             bool front
             ) const;
 
-        QName m_role;
+        xmltooling::QName m_role;
         MessageDecoder* m_decoder;
         XMLCh* m_outgoing;
         vector<const XMLCh*> m_bindings;
@@ -142,7 +142,9 @@ SAML2NameIDMgmt::SAML2NameIDMgmt(const DOMElement* e, const char* appId)
         SAMLConfig& conf = SAMLConfig::getConfig();
 
         // Handle incoming binding.
-        m_decoder = conf.MessageDecoderManager.newPlugin(getString("Binding").second,make_pair(e,shibspconstants::SHIB2SPCONFIG_NS));
+        m_decoder = conf.MessageDecoderManager.newPlugin(
+            getString("Binding").second, pair<const DOMElement*,const XMLCh*>(e,shibspconstants::SHIB2SPCONFIG_NS)
+            );
         m_decoder->setArtifactResolver(SPConfig::getConfig().getArtifactResolver());
 
         if (m_decoder->isUserAgentPresent()) {
@@ -168,9 +170,17 @@ SAML2NameIDMgmt::SAML2NameIDMgmt(const DOMElement* e, const char* appId)
                 m_bindings.push_back(start);
                 try {
                     auto_ptr_char b(start);
-                    MessageEncoder * encoder = conf.MessageEncoderManager.newPlugin(b.get(),make_pair(e,shibspconstants::SHIB2SPCONFIG_NS));
-                    m_encoders[start] = encoder;
-                    m_log.debug("supporting outgoing front-channel binding (%s)", b.get());
+                    MessageEncoder * encoder = conf.MessageEncoderManager.newPlugin(
+                        b.get(), pair<const DOMElement*,const XMLCh*>(e,shibspconstants::SHIB2SPCONFIG_NS)
+                        );
+                    if (encoder->isUserAgentPresent()) {
+                        m_encoders[start] = encoder;
+                        m_log.debug("supporting outgoing binding (%s)", b.get());
+                    }
+                    else {
+                        delete encoder;
+                        m_log.warn("skipping outgoing binding (%s), not a front-channel mechanism", b.get());
+                    }
                 }
                 catch (exception& ex) {
                     m_log.error("error building MessageEncoder: %s", ex.what());
@@ -183,7 +193,7 @@ SAML2NameIDMgmt::SAML2NameIDMgmt(const DOMElement* e, const char* appId)
         }
         else {
             MessageEncoder* encoder = conf.MessageEncoderManager.newPlugin(
-                getString("Binding").second,make_pair(e,shibspconstants::SHIB2SPCONFIG_NS)
+                getString("Binding").second, pair<const DOMElement*,const XMLCh*>(e,shibspconstants::SHIB2SPCONFIG_NS)
                 );
             m_encoders.insert(pair<const XMLCh*,MessageEncoder*>(NULL, encoder));
         }
@@ -197,21 +207,16 @@ SAML2NameIDMgmt::SAML2NameIDMgmt(const DOMElement* e, const char* appId)
 
 pair<bool,long> SAML2NameIDMgmt::run(SPRequest& request, bool isHandler) const
 {
-    // Get the session_id in case this is a front-channel request.
-    pair<string,const char*> shib_cookie = request.getApplication().getCookieNameProps("_shibsession_");
-    const char* session_id = request.getCookie(shib_cookie.first.c_str());
-
     SPConfig& conf = SPConfig::getConfig();
     if (conf.isEnabled(SPConfig::OutOfProcess)) {
         // When out of process, we run natively and directly process the message.
-        return doRequest(request.getApplication(), session_id, request, request);
+        return doRequest(request.getApplication(), request, request);
     }
     else {
         // When not out of process, we remote all the message processing.
-        DDF out,in = wrap(request, NULL, true);
+        vector<string> headers(1,"Cookie");
+        DDF out,in = wrap(request, &headers, true);
         DDFJanitor jin(in), jout(out);
-        if (session_id)
-            in.addmember("session_id").string(session_id);
         out=request.getServiceProvider().getListenerService()->send(in);
         return unwrap(request, out);
     }
@@ -227,22 +232,24 @@ void SAML2NameIDMgmt::receive(DDF& in, ostream& out)
         m_log.error("couldn't find application (%s) for NameID mgmt", aid ? aid : "(missing)");
         throw ConfigurationException("Unable to locate application for NameID mgmt, deleted?");
     }
-    
+
     // Unpack the request.
+    auto_ptr<HTTPRequest> req(getRequest(in));
+
+    // Wrap a response shim.
     DDF ret(NULL);
     DDFJanitor jout(ret);
-    auto_ptr<HTTPRequest> req(getRequest(in));
     auto_ptr<HTTPResponse> resp(getResponse(ret));
-    
+
     // Since we're remoted, the result should either be a throw, which we pass on,
     // a false/0 return, which we just return as an empty structure, or a response/redirect,
     // which we capture in the facade and send back.
-    doRequest(*app, in["session_id"].string(), *req.get(), *resp.get());
+    doRequest(*app, *req.get(), *resp.get());
     out << ret;
 }
 
 pair<bool,long> SAML2NameIDMgmt::doRequest(
-    const Application& application, const char* session_id, const HTTPRequest& request, HTTPResponse& response
+    const Application& application, const HTTPRequest& request, HTTPResponse& response
     ) const
 {
 #ifndef SHIBSP_LITE
@@ -252,7 +259,7 @@ pair<bool,long> SAML2NameIDMgmt::doRequest(
     pair<bool,const char*> policyId = getString("policyId", m_configNS.get());  // namespace-qualified if inside handler element
     if (!policyId.first)
         policyId = application.getString("policyId");   // unqualified in Application(s) element
-        
+
     // Access policy properties.
     const PropertySet* settings = application.getServiceProvider().getPolicySettings(policyId.second);
     pair<bool,bool> validate = settings->getBool("validate");
@@ -261,8 +268,8 @@ pair<bool,long> SAML2NameIDMgmt::doRequest(
     Locker metadataLocker(application.getMetadataProvider());
 
     // Create the policy.
-    shibsp::SecurityPolicy policy(application, &m_role, validate.first && validate.second);
-    
+    shibsp::SecurityPolicy policy(application, &m_role, validate.first && validate.second, policyId.second);
+
     // Decode the message.
     string relayState;
     auto_ptr<XMLObject> msg(m_decoder->decode(relayState, request, policy));
@@ -272,9 +279,10 @@ pair<bool,long> SAML2NameIDMgmt::doRequest(
             throw SecurityPolicyException("Security of ManageNameIDRequest not established.");
 
         // Message from IdP to change or terminate a NameID.
-        
+
         // If this is front-channel, we have to have a session_id to use already.
-        if (m_decoder->isUserAgentPresent() && !session_id) {
+        string session_id = cache->active(application, request);
+        if (m_decoder->isUserAgentPresent() && session_id.empty()) {
             m_log.error("no active session");
             return sendResponse(
                 mgmtRequest->getID(),
@@ -287,6 +295,8 @@ pair<bool,long> SAML2NameIDMgmt::doRequest(
                 );
         }
 
+        EntityDescriptor* entity = policy.getIssuerMetadata() ? dynamic_cast<EntityDescriptor*>(policy.getIssuerMetadata()->getParent()) : NULL;
+
         bool ownedName = false;
         NameID* nameid = mgmtRequest->getNameID();
         if (!nameid) {
@@ -302,7 +312,7 @@ pair<bool,long> SAML2NameIDMgmt::doRequest(
                         policy.getIssuerMetadata() ? new MetadataCredentialCriteria(*policy.getIssuerMetadata()) : NULL
                         );
                     try {
-                        auto_ptr<XMLObject> decryptedID(encname->decrypt(*cr,application.getXMLString("entityID").second,mcc.get()));
+                        auto_ptr<XMLObject> decryptedID(encname->decrypt(*cr,application.getRelyingParty(entity)->getXMLString("entityID").second,mcc.get()));
                         nameid = dynamic_cast<NameID*>(decryptedID.get());
                         if (nameid) {
                             ownedName = true;
@@ -333,9 +343,8 @@ pair<bool,long> SAML2NameIDMgmt::doRequest(
 
         // For a front-channel request, we have to match the information in the request
         // against the current session.
-        EntityDescriptor* entity = policy.getIssuerMetadata() ? dynamic_cast<EntityDescriptor*>(policy.getIssuerMetadata()->getParent()) : NULL;
-        if (session_id) {
-            if (!cache->matches(session_id, entity, *nameid, NULL, application)) {
+        if (!session_id.empty()) {
+            if (!cache->matches(application, request, entity, *nameid, NULL)) {
                 return sendResponse(
                     mgmtRequest->getID(),
                     StatusCode::REQUESTER, StatusCode::REQUEST_DENIED, "Active session did not match NameID mgmt request.",
@@ -368,7 +377,7 @@ pair<bool,long> SAML2NameIDMgmt::doRequest(
                             policy.getIssuerMetadata() ? new MetadataCredentialCriteria(*policy.getIssuerMetadata()) : NULL
                             );
                         try {
-                            auto_ptr<XMLObject> decryptedID(encnewid->decrypt(*cr,application.getXMLString("entityID").second,mcc.get()));
+                            auto_ptr<XMLObject> decryptedID(encnewid->decrypt(*cr,application.getRelyingParty(entity)->getXMLString("entityID").second,mcc.get()));
                             newid = dynamic_cast<NewID*>(decryptedID.get());
                             if (newid) {
                                 ownedNewID = true;
@@ -464,7 +473,7 @@ pair<bool,long> SAML2NameIDMgmt::doRequest(
     if (policy.getIssuerMetadata())
         annotateException(&ex, policy.getIssuerMetadata()); // throws it
     ex.raise();
-    return make_pair(false,0);  // never happen, satisfies compiler
+    return make_pair(false,0L);  // never happen, satisfies compiler
 #else
     throw ConfigurationException("Cannot process NameID mgmt message using lite version of shibsp library.");
 #endif
@@ -472,41 +481,6 @@ pair<bool,long> SAML2NameIDMgmt::doRequest(
 
 #ifndef SHIBSP_LITE
 
-bool SAML2NameIDMgmt::stronglyMatches(const XMLCh* idp, const XMLCh* sp, const saml2::NameID& n1, const saml2::NameID& n2) const
-{
-    if (!XMLString::equals(n1.getName(), n2.getName()))
-        return false;
-    
-    const XMLCh* s1 = n1.getFormat();
-    const XMLCh* s2 = n2.getFormat();
-    if (!s1 || !*s1)
-        s1 = saml2::NameID::UNSPECIFIED;
-    if (!s2 || !*s2)
-        s2 = saml2::NameID::UNSPECIFIED;
-    if (!XMLString::equals(s1,s2))
-        return false;
-    
-    s1 = n1.getNameQualifier();
-    s2 = n2.getNameQualifier();
-    if (!s1 || !*s1)
-        s1 = idp;
-    if (!s2 || !*s2)
-        s2 = idp;
-    if (!XMLString::equals(s1,s2))
-        return false;
-
-    s1 = n1.getSPNameQualifier();
-    s2 = n2.getSPNameQualifier();
-    if (!s1 || !*s1)
-        s1 = sp;
-    if (!s2 || !*s2)
-        s2 = sp;
-    if (!XMLString::equals(s1,s2))
-        return false;
-
-    return true;
-}
-
 pair<bool,long> SAML2NameIDMgmt::sendResponse(
     const XMLCh* requestID,
     const XMLCh* code,
@@ -554,7 +528,7 @@ pair<bool,long> SAML2NameIDMgmt::sendResponse(
     }
     Issuer* issuer = IssuerBuilder::buildIssuer();
     nim->setIssuer(issuer);
-    issuer->setName(application.getXMLString("entityID").second);
+    issuer->setName(application.getRelyingParty(dynamic_cast<EntityDescriptor*>(role->getParent()))->getXMLString("entityID").second);
     fillStatus(*nim.get(), code, subcode, msg);
 
     auto_ptr_char dest(nim->getDestination());
@@ -568,6 +542,7 @@ pair<bool,long> SAML2NameIDMgmt::sendResponse(
 #include <xmltooling/impl/AnyElement.h>
 #include <xmltooling/soap/SOAP.h>
 #include <xmltooling/soap/SOAPClient.h>
+#include <xmltooling/soap/HTTPSOAPTransport.h>
 using namespace soap11;
 namespace {
     static const XMLCh NameIDNotification[] =   UNICODE_LITERAL_18(N,a,m,e,I,D,N,o,t,i,f,i,c,a,t,i,o,n);
@@ -580,6 +555,12 @@ namespace {
     private:
         void prepareTransport(SOAPTransport& transport) {
             transport.setVerifyHost(false);
+            HTTPSOAPTransport* http = dynamic_cast<HTTPSOAPTransport*>(&transport);
+            if (http) {
+                http->useChunkedEncoding(false);
+                http->setRequestHeader("User-Agent", PACKAGE_NAME);
+                http->setRequestHeader(PACKAGE_NAME, PACKAGE_VERSION);
+            }
         }
     };
 };