Fix address checking if none supplied.
[shibboleth/sp.git] / shibsp / handler / impl / AssertionConsumerService.cpp
index 169636d..02c7f97 100644 (file)
 # include <saml/saml1/core/Assertions.h>
 # include <saml/util/CommonDomainCookie.h>
 using namespace samlconstants;
+using opensaml::saml2md::MetadataProvider;
+using opensaml::saml2md::RoleDescriptor;
 using opensaml::saml2md::EntityDescriptor;
 using opensaml::saml2md::IDPSSODescriptor;
 using opensaml::saml2md::SPSSODescriptor;
-using opensaml::saml2md::isValidForProtocol;
 #else
 # include "lite/CommonDomainCookie.h"
 #endif
@@ -68,7 +69,7 @@ AssertionConsumerService::AssertionConsumerService(const DOMElement* e, const ch
 #ifndef SHIBSP_LITE
     if (SPConfig::getConfig().isEnabled(SPConfig::OutOfProcess)) {
         m_decoder = SAMLConfig::getConfig().MessageDecoderManager.newPlugin(
-            getString("Binding").second,make_pair(e,shibspconstants::SHIB2SPCONFIG_NS)
+            getString("Binding").second, pair<const DOMElement*,const XMLCh*>(e,shibspconstants::SHIB2SPCONFIG_NS)
             );
         m_decoder->setArtifactResolver(SPConfig::getConfig().getArtifactResolver());
     }
@@ -87,53 +88,17 @@ pair<bool,long> AssertionConsumerService::run(SPRequest& request, bool isHandler
     string relayState;
     SPConfig& conf = SPConfig::getConfig();
     
-    try {
-        if (conf.isEnabled(SPConfig::OutOfProcess)) {
-            // When out of process, we run natively and directly process the message.
-            // RelayState will be fully handled during message processing.
-            string entityID;
-            string key = processMessage(request.getApplication(), request, entityID, relayState);
-            return sendRedirect(request, key.c_str(), entityID.c_str(), relayState.c_str());
-        }
-        else {
-            // When not out of process, we remote all the message processing.
-            DDF out,in = wrap(request);
-            DDFJanitor jin(in), jout(out);
-            
-            try {
-                out=request.getServiceProvider().getListenerService()->send(in);
-            }
-            catch (XMLToolingException& ex) {
-                // Try for RelayState recovery.
-                if (ex.getProperty("RelayState"))
-                    relayState = ex.getProperty("RelayState");
-                try {
-                    recoverRelayState(request.getApplication(), request, relayState);
-                }
-                catch (exception& ex2) {
-                    m_log.error("trapped an error during RelayState recovery while handling an error: %s", ex2.what());
-                }
-                throw;
-            }
-                
-            // We invoke RelayState recovery one last time on this side of the boundary.
-            if (out["RelayState"].isstring())
-                relayState = out["RelayState"].string(); 
-            recoverRelayState(request.getApplication(), request, relayState);
-    
-            // If it worked, we have a session key.
-            if (!out["key"].isstring())
-                throw FatalProfileException("Remote processing of SSO profile did not return a usable session key.");
-            
-            // Take care of cookie business and wrap it up.
-            return sendRedirect(request, out["key"].string(), out["entity_id"].string(), relayState.c_str());
-        }
+    if (conf.isEnabled(SPConfig::OutOfProcess)) {
+        // When out of process, we run natively and directly process the message.
+        return processMessage(request.getApplication(), request, request);
     }
-    catch (XMLToolingException& ex) {
-        // Try and preserve RelayState.
-        if (!relayState.empty())
-            ex.addProperty("RelayState", relayState.c_str());
-        throw;
+    else {
+        // When not out of process, we remote all the message processing.
+        vector<string> headers(1, "Cookie");
+        DDF out,in = wrap(request, &headers);
+        DDFJanitor jin(in), jout(out);
+        out=request.getServiceProvider().getListenerService()->send(in);
+        return unwrap(request, out);
     }
 }
 
@@ -149,33 +114,22 @@ void AssertionConsumerService::receive(DDF& in, ostream& out)
     }
     
     // Unpack the request.
-    auto_ptr<HTTPRequest> http(getRequest(in));
-    
-    // Do the work.
-    string relayState, entityID;
-    try {
-        string key = processMessage(*app, *http.get(), entityID, relayState);
-
-        // Repack for return to caller.
-        DDF ret=DDF(NULL).structure();
-        DDFJanitor jret(ret);
-        ret.addmember("key").string(key.c_str());
-        if (!entityID.empty())
-            ret.addmember("entity_id").string(entityID.c_str());
-        if (!relayState.empty())
-            ret.addmember("RelayState").string(relayState.c_str());
-        out << ret;
-    }
-    catch (XMLToolingException& ex) {
-        // Try and preserve RelayState if we can.
-        if (!relayState.empty())
-            ex.addProperty("RelayState", relayState.c_str());
-        throw;
-    }
+    auto_ptr<HTTPRequest> req(getRequest(in));
+
+    // Wrap a response shim.
+    DDF ret(NULL);
+    DDFJanitor jout(ret);
+    auto_ptr<HTTPResponse> resp(getResponse(ret));
+
+    // Since we're remoted, the result should either be a throw, 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.
+    processMessage(*app, *req.get(), *resp.get());
+    out << ret;
 }
 
-string AssertionConsumerService::processMessage(
-    const Application& application, HTTPRequest& httpRequest, string& entityID, string& relayState
+pair<bool,long> AssertionConsumerService::processMessage(
+    const Application& application, const HTTPRequest& httpRequest, HTTPResponse& httpResponse
     ) const
 {
 #ifndef SHIBSP_LITE
@@ -194,44 +148,40 @@ string AssertionConsumerService::processMessage(
     // Create the policy.
     shibsp::SecurityPolicy policy(application, &m_role, validate.first && validate.second);
     
-    // Decode the message and process it in a protocol-specific way.
-    auto_ptr<XMLObject> msg(m_decoder->decode(relayState, httpRequest, policy));
-    if (!msg.get())
-        throw BindingException("Failed to decode an SSO protocol response.");
-    recoverRelayState(application, httpRequest, relayState);
-    string key = implementProtocol(application, httpRequest, policy, settings, *msg.get());
-
-    auto_ptr_char issuer(policy.getIssuer() ? policy.getIssuer()->getName() : NULL);
-    if (issuer.get())
-        entityID = issuer.get();
-    
-    return key;
+    string relayState;
+
+    try {
+        // Decode the message and process it in a protocol-specific way.
+        auto_ptr<XMLObject> msg(m_decoder->decode(relayState, httpRequest, policy));
+        if (!msg.get())
+            throw BindingException("Failed to decode an SSO protocol response.");
+        recoverRelayState(application, httpRequest, httpResponse, relayState);
+        implementProtocol(application, httpRequest, httpResponse, policy, settings, *msg.get());
+
+        auto_ptr_char issuer(policy.getIssuer() ? policy.getIssuer()->getName() : NULL);
+        
+        // History cookie.
+        if (issuer.get() && *issuer.get())
+            maintainHistory(application, httpRequest, httpResponse, issuer.get());
+
+        // Now redirect to the state value. By now, it should be set to *something* usable.
+        return make_pair(true, httpResponse.sendRedirect(relayState.c_str()));
+    }
+    catch (XMLToolingException& ex) {
+        if (!relayState.empty())
+            ex.addProperty("RelayState", relayState.c_str());
+        throw;
+    }
 #else
     throw ConfigurationException("Cannot process message using lite version of shibsp library.");
 #endif
 }
 
-pair<bool,long> AssertionConsumerService::sendRedirect(
-    SPRequest& request, const char* key, const char* entityID, const char* relayState
-    ) const
-{
-    // We've got a good session, so set the session cookie.
-    pair<string,const char*> shib_cookie=request.getApplication().getCookieNameProps("_shibsession_");
-    string k(key);
-    k += shib_cookie.second;
-    request.setCookie(shib_cookie.first.c_str(), k.c_str());
-
-    // History cookie.
-    maintainHistory(request, entityID, shib_cookie.second);
-
-    // Now redirect to the state value. By now, it should be set to *something* usable.
-    return make_pair(true, request.sendRedirect(relayState));
-}
-
-void AssertionConsumerService::checkAddress(
-    const Application& application, const HTTPRequest& httpRequest, const char* issuedTo
-    ) const
+void AssertionConsumerService::checkAddress(const Application& application, const HTTPRequest& httpRequest, const char* issuedTo) const
 {
+    if (!issuedTo || !*issuedTo)
+        return;
+    
     const PropertySet* props=application.getPropertySet("Sessions");
     pair<bool,bool> checkAddress = props ? props->getBool("checkAddress") : make_pair(false,true);
     if (!checkAddress.first)
@@ -309,15 +259,15 @@ ResolutionContext* AssertionConsumerService::resolveAttributes(
     if (extractor) {
         Locker extlocker(extractor);
         if (entity) {
-            pair<bool,const char*> prefix = application.getString("metadataAttributePrefix");
-            if (prefix.first) {
+            pair<bool,const char*> mprefix = application.getString("metadataAttributePrefix");
+            if (mprefix.first) {
                 m_log.debug("extracting metadata-derived attributes...");
                 try {
                     extractor->extractAttributes(application, issuer, *entity, resolvedAttributes);
                     for (vector<Attribute*>::iterator a = resolvedAttributes.begin(); a != resolvedAttributes.end(); ++a) {
                         vector<string>& ids = (*a)->getAliases();
                         for (vector<string>::iterator id = ids.begin(); id != ids.end(); ++id)
-                            *id = prefix.second + *id;
+                            *id = mprefix.second + *id;
                     }
                 }
                 catch (exception& ex) {
@@ -391,6 +341,17 @@ ResolutionContext* AssertionConsumerService::resolveAttributes(
             // Copy over any pushed attributes.
             if (!resolvedAttributes.empty())
                 ctx->getResolvedAttributes().insert(ctx->getResolvedAttributes().end(), resolvedAttributes.begin(), resolvedAttributes.end());
+
+            // Attach global prefix if needed.
+            pair<bool,const char*> prefix = application.getString("attributePrefix");
+            if (prefix.first) {
+                for (vector<Attribute*>::iterator a = ctx->getResolvedAttributes().begin(); a != ctx->getResolvedAttributes().end(); ++a) {
+                    vector<string>& ids = (*a)->getAliases();
+                    for (vector<string>::iterator id = ids.begin(); id != ids.end(); ++id)
+                        *id = prefix.second + *id;
+                }
+            }
+
             return ctx.release();
         }
     }
@@ -398,8 +359,19 @@ ResolutionContext* AssertionConsumerService::resolveAttributes(
         m_log.error("attribute resolution failed: %s", ex.what());
     }
     
-    if (!resolvedAttributes.empty())
+    if (!resolvedAttributes.empty()) {
+        // Attach global prefix if needed.
+        pair<bool,const char*> prefix = application.getString("attributePrefix");
+        if (prefix.first) {
+            for (vector<Attribute*>::iterator a = resolvedAttributes.begin(); a != resolvedAttributes.end(); ++a) {
+                vector<string>& ids = (*a)->getAliases();
+                for (vector<string>::iterator id = ids.begin(); id != ids.end(); ++id)
+                    *id = prefix.second + *id;
+            }
+        }
+
         return new DummyContext(resolvedAttributes);
+    }
     return NULL;
 }
 
@@ -408,7 +380,7 @@ void AssertionConsumerService::extractMessageDetails(const Assertion& assertion,
     policy.setMessageID(assertion.getID());
     policy.setIssueInstant(assertion.getIssueInstantEpoch());
 
-    if (XMLString::equals(assertion.getElementQName().getNamespaceURI(), samlconstants::SAML20P_NS)) {
+    if (XMLString::equals(assertion.getElementQName().getNamespaceURI(), samlconstants::SAML20_NS)) {
         const saml2::Assertion* a2 = dynamic_cast<const saml2::Assertion*>(&assertion);
         if (a2) {
             m_log.debug("extracting issuer from SAML 2.0 assertion");
@@ -424,41 +396,50 @@ void AssertionConsumerService::extractMessageDetails(const Assertion& assertion,
     }
 
     if (policy.getIssuer() && !policy.getIssuerMetadata() && policy.getMetadataProvider()) {
-        m_log.debug("searching metadata for assertion issuer...");
-        const EntityDescriptor* entity = policy.getMetadataProvider()->getEntityDescriptor(policy.getIssuer()->getName());
-        if (entity) {
-            m_log.debug("matched assertion issuer against metadata, searching for applicable role...");
-            const IDPSSODescriptor* idp=find_if(entity->getIDPSSODescriptors(), isValidForProtocol(protocol));
-            if (idp)
-                policy.setIssuerMetadata(idp);
-            else if (m_log.isWarnEnabled())
-                m_log.warn("unable to find compatible IdP role in metadata");
+        if (policy.getIssuer()->getFormat() && !XMLString::equals(policy.getIssuer()->getFormat(), saml2::NameIDType::ENTITY)) {
+            m_log.warn("non-system entity issuer, skipping metadata lookup");
+            return;
         }
-        else if (m_log.isWarnEnabled()) {
+        m_log.debug("searching metadata for assertion issuer...");
+        MetadataProvider::Criteria mc(policy.getIssuer()->getName(), &IDPSSODescriptor::ELEMENT_QNAME, protocol);
+        pair<const EntityDescriptor*,const RoleDescriptor*> entity = policy.getMetadataProvider()->getEntityDescriptor(mc);
+        if (!entity.first) {
             auto_ptr_char iname(policy.getIssuer()->getName());
             m_log.warn("no metadata found, can't establish identity of issuer (%s)", iname.get());
         }
+        else if (!entity.second) {
+            m_log.warn("unable to find compatible IdP role in metadata");
+        }
+        else {
+            policy.setIssuerMetadata(entity.second);
+        }
     }
 }
 
 #endif
 
-void AssertionConsumerService::maintainHistory(SPRequest& request, const char* entityID, const char* cookieProps) const
+void AssertionConsumerService::maintainHistory(
+    const Application& application, const HTTPRequest& request, HTTPResponse& response, const char* entityID
+    ) const
 {
-    if (!entityID)
-        return;
-        
-    const PropertySet* sessionProps=request.getApplication().getPropertySet("Sessions");
+    static const char* defProps="; path=/";
+
+    const PropertySet* sessionProps=application.getPropertySet("Sessions");
     pair<bool,bool> idpHistory=sessionProps->getBool("idpHistory");
+
     if (!idpHistory.first || idpHistory.second) {
+        pair<bool,const char*> cookieProps=sessionProps->getString("cookieProps");
+        if (!cookieProps.first)
+            cookieProps.second=defProps;
+
         // Set an IdP history cookie locally (essentially just a CDC).
         CommonDomainCookie cdc(request.getCookie(CommonDomainCookie::CDCName));
 
         // Either leave in memory or set an expiration.
         pair<bool,unsigned int> days=sessionProps->getUnsignedInt("idpHistoryDays");
         if (!days.first || days.second==0) {
-            string c = string(cdc.set(entityID)) + cookieProps;
-            request.setCookie(CommonDomainCookie::CDCName, c.c_str());
+            string c = string(cdc.set(entityID)) + cookieProps.second;
+            response.setCookie(CommonDomainCookie::CDCName, c.c_str());
         }
         else {
             time_t now=time(NULL) + (days.second * 24 * 60 * 60);
@@ -470,8 +451,8 @@ void AssertionConsumerService::maintainHistory(SPRequest& request, const char* e
 #endif
             char timebuf[64];
             strftime(timebuf,64,"%a, %d %b %Y %H:%M:%S GMT",ptime);
-            string c = string(cdc.set(entityID)) + cookieProps + "; expires=" + timebuf;
-            request.setCookie(CommonDomainCookie::CDCName, c.c_str());
+            string c = string(cdc.set(entityID)) + cookieProps.second + "; expires=" + timebuf;
+            response.setCookie(CommonDomainCookie::CDCName, c.c_str());
         }
     }
 }