Tagging 2.4RC1 release.
[shibboleth/sp.git] / shibsp / ServiceProvider.cpp
index c4b70b8..b358948 100644 (file)
@@ -1,6 +1,6 @@
 /*
- *  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
@@ -16,7 +16,7 @@
 
 /**
  * ServiceProvider.cpp
- * 
+ *
  * Interface to a Shibboleth ServiceProvider instance.
  */
 
 
 #include <fstream>
 #include <sstream>
+#ifndef SHIBSP_LITE
+# include <saml/exceptions.h>
+# include <saml/saml2/metadata/MetadataProvider.h>
+#endif
 #include <xmltooling/XMLToolingConfig.h>
 #include <xmltooling/util/NDC.h>
+#include <xmltooling/util/PathResolver.h>
 #include <xmltooling/util/URLEncoder.h>
 #include <xmltooling/util/XMLHelper.h>
 
 using namespace shibsp;
+using namespace xmltooling::logging;
 using namespace xmltooling;
 using namespace std;
 
 namespace shibsp {
     SHIBSP_DLLLOCAL PluginManager<ServiceProvider,string,const DOMElement*>::Factory XMLServiceProviderFactory;
 
-    long SHIBSP_DLLLOCAL sendError(SPRequest& request, const Application* app, const char* page, TemplateParameters& tp, bool mayRedirect=false)
+    long SHIBSP_DLLLOCAL sendError(
+        Category& log, SPRequest& request, const Application* app, const char* page, TemplateParameters& tp, bool mayRedirect=true
+        )
     {
+        // The properties we need can be set in the RequestMap, or the Errors element.
+        bool mderror = dynamic_cast<const opensaml::saml2md::MetadataException*>(tp.getRichException())!=nullptr;
+        bool accesserror = (strcmp(page, "access")==0);
+        pair<bool,const char*> redirectErrors = pair<bool,const char*>(false,nullptr);
+        pair<bool,const char*> pathname = pair<bool,const char*>(false,nullptr);
+
+        // Strictly for error handling, detect a nullptr application and point at the default.
+        if (!app)
+            app = request.getServiceProvider().getApplication(nullptr);
+
+        const PropertySet* props=app->getPropertySet("Errors");
+
+        // First look for settings in the request map of the form pageError.
+        try {
+            RequestMapper::Settings settings = request.getRequestSettings();
+            if (mderror)
+                pathname = settings.first->getString("metadataError");
+            if (!pathname.first) {
+                string pagename(page);
+                pagename += "Error";
+                pathname = settings.first->getString(pagename.c_str());
+            }
+            if (mayRedirect)
+                redirectErrors = settings.first->getString("redirectErrors");
+        }
+        catch (exception& ex) {
+            log.error(ex.what());
+        }
+
+        // Check for redirection on errors instead of template.
         if (mayRedirect) {
-            // Check for redirection on errors instead of template.
-            pair<bool,const char*> redirectErrors = app ? app->getString("redirectErrors") : pair<bool,const char*>(false,NULL);
+            if (!redirectErrors.first && props)
+                redirectErrors = props->getString("redirectErrors");
             if (redirectErrors.first) {
                 string loc(redirectErrors.second);
                 loc = loc + '?' + tp.toQueryString();
@@ -60,55 +98,61 @@ namespace shibsp {
         request.setContentType("text/html");
         request.setResponseHeader("Expires","01-Jan-1997 12:00:00 GMT");
         request.setResponseHeader("Cache-Control","private,no-store,no-cache");
-    
-        // Error templates come from the request's settings or from the Errors property set.
-        pair<bool,const char*> pathname = pair<bool,const char*>(false,NULL);
-        try {
-            RequestMapper::Settings settings = request.getRequestSettings();
-            string pagename(page);
-            pagename += "Error";
-            pathname = settings.first->getString(pagename.c_str());
-        }
-        catch (exception& ex) {
-            request.log(SPRequest::SPError, ex.what());
+
+        // Nothing in the request map, so check for a property named "page" in the Errors property set.
+        if (!pathname.first && props) {
+            if (mderror)
+                pathname=props->getString("metadata");
+            if (!pathname.first)
+                pathname=props->getString(page);
         }
 
-        // Nothing for request, so check app properties.
-        const PropertySet* props=app ? app->getPropertySet("Errors") : NULL;
-        if (!pathname.first && props)
-            pathname=props->getString(page);
+        // If there's still no template to use, just use pageError.html unless it's an access issue.
+        string fname;
+        if (!pathname.first) {
+            if (!accesserror) {
+                fname = string(page) + "Error.html";
+                pathname.second = fname.c_str();
+            }
+        }
+        else {
+            fname = pathname.second;
+        }
 
-        if (pathname.first) {
-            ifstream infile(pathname.second);
+        // If we have a template to use, use it.
+        if (!fname.empty()) {
+            ifstream infile(XMLToolingConfig::getConfig().getPathResolver()->resolve(fname, PathResolver::XMLTOOLING_CFG_FILE).c_str());
             if (infile) {
                 tp.setPropertySet(props);
                 stringstream str;
                 XMLToolingConfig::getConfig().getTemplateEngine()->run(infile, str, tp, tp.getRichException());
-                return request.sendResponse(str);
+                return request.sendError(str);
             }
         }
-        
-        if (!strcmp(page,"access")) {
+
+        // If we got here, then either it's an access error or a template failed.
+        if (accesserror) {
             istringstream msg("Access Denied");
             return request.sendResponse(msg, HTTPResponse::XMLTOOLING_HTTP_STATUS_FORBIDDEN);
         }
-    
-        string errstr = string("sendError could not process error template (") + page + ")";
-        request.log(SPRequest::SPError, errstr);
+
+        log.error("sendError could not process error template (%s)", pathname.second);
         istringstream msg("Internal Server Error. Please contact the site administrator.");
         return request.sendError(msg);
     }
-    
+
     void SHIBSP_DLLLOCAL clearHeaders(SPRequest& request) {
-        request.clearHeader("Shib-Session-ID", "HTTP_SHIB_SESSION_ID");
-        request.clearHeader("Shib-Identity-Provider", "HTTP_SHIB_IDENTITY_PROVIDER");
-        request.clearHeader("Shib-Authentication-Method", "HTTP_SHIB_AUTHENTICATION_METHOD");
-        request.clearHeader("Shib-AuthnContext-Class", "HTTP_SHIB_AUTHNCONTEXT_CLASS");
-        request.clearHeader("Shib-AuthnContext-Decl", "HTTP_SHIB_AUTHNCONTEXT_DECL");
-        request.clearHeader("Shib-Assertion-Count", "HTTP_SHIB_ASSERTION_COUNT");
+        const Application& app = request.getApplication();
+        app.clearHeader(request, "Shib-Session-ID", "HTTP_SHIB_SESSION_ID");
+        app.clearHeader(request, "Shib-Session-Index", "HTTP_SHIB_SESSION_INDEX");
+        app.clearHeader(request, "Shib-Identity-Provider", "HTTP_SHIB_IDENTITY_PROVIDER");
+        app.clearHeader(request, "Shib-Authentication-Method", "HTTP_SHIB_AUTHENTICATION_METHOD");
+        app.clearHeader(request, "Shib-Authentication-Instant", "HTTP_SHIB_AUTHENTICATION_INSTANT");
+        app.clearHeader(request, "Shib-AuthnContext-Class", "HTTP_SHIB_AUTHNCONTEXT_CLASS");
+        app.clearHeader(request, "Shib-AuthnContext-Decl", "HTTP_SHIB_AUTHNCONTEXT_DECL");
+        app.clearHeader(request, "Shib-Assertion-Count", "HTTP_SHIB_ASSERTION_COUNT");
+        app.clearAttributeHeaders(request);
         request.clearHeader("REMOTE_USER", "HTTP_REMOTE_USER");
-        //request.clearHeader("Shib-Application-ID");   handle inside app method
-        request.getApplication().clearAttributeHeaders(request);
     }
 };
 
@@ -117,13 +161,63 @@ void SHIBSP_API shibsp::registerServiceProviders()
     SPConfig::getConfig().ServiceProviderManager.registerFactory(XML_SERVICE_PROVIDER, XMLServiceProviderFactory);
 }
 
+ServiceProvider::ServiceProvider()
+{
+    m_authTypes.insert("shibboleth");
+}
+
+ServiceProvider::~ServiceProvider()
+{
+}
+
+#ifndef SHIBSP_LITE
+SecurityPolicyProvider* ServiceProvider::getSecurityPolicyProvider(bool required) const
+{
+    if (required)
+        throw ConfigurationException("No SecurityPolicyProvider available.");
+    return nullptr;
+}
+#endif
+
+Remoted* ServiceProvider::regListener(const char* address, Remoted* listener)
+{
+    Remoted* ret=nullptr;
+    map<string,Remoted*>::const_iterator i=m_listenerMap.find(address);
+    if (i!=m_listenerMap.end())
+        ret=i->second;
+    m_listenerMap[address]=listener;
+    Category::getInstance(SHIBSP_LOGCAT".ServiceProvider").info("registered remoted message endpoint (%s)",address);
+    return ret;
+}
+
+bool ServiceProvider::unregListener(const char* address, Remoted* current, Remoted* restore)
+{
+    map<string,Remoted*>::const_iterator i=m_listenerMap.find(address);
+    if (i!=m_listenerMap.end() && i->second==current) {
+        if (restore)
+            m_listenerMap[address]=restore;
+        else
+            m_listenerMap.erase(address);
+        Category::getInstance(SHIBSP_LOGCAT".ServiceProvider").info("unregistered remoted message endpoint (%s)",address);
+        return true;
+    }
+    return false;
+}
+
+Remoted* ServiceProvider::lookupListener(const char *address) const
+{
+    map<string,Remoted*>::const_iterator i=m_listenerMap.find(address);
+    return (i==m_listenerMap.end()) ? nullptr : i->second;
+}
+
 pair<bool,long> ServiceProvider::doAuthentication(SPRequest& request, bool handler) const
 {
 #ifdef _DEBUG
     xmltooling::NDC ndc("doAuthentication");
 #endif
+    Category& log = Category::getInstance(SHIBSP_LOGCAT".ServiceProvider");
 
-    const Application* app=NULL;
+    const Application* app=nullptr;
     string targetURL = request.getRequestURL();
 
     try {
@@ -150,11 +244,11 @@ pair<bool,long> ServiceProvider::doAuthentication(SPRequest& request, bool handl
                 else {
                     TemplateParameters tp;
                     tp.m_map["requestURL"] = targetURL.substr(0,targetURL.find('?'));
-                    return make_pair(true,sendError(request, app, "ssl", tp));
+                    return make_pair(true,sendError(log, request, app, "ssl", tp, false));
                 }
             }
         }
-        
+
         const char* handlerURL=request.getHandlerURL(targetURL.c_str());
         if (!handlerURL)
             throw ConfigurationException("Cannot determine handler from resource URL, check configuration.");
@@ -173,28 +267,30 @@ pair<bool,long> ServiceProvider::doAuthentication(SPRequest& request, bool handl
         pair<bool,bool> requireSession = settings.first->getBool("requireSession");
         pair<bool,const char*> requireSessionWith = settings.first->getString("requireSessionWith");
 
-        // If no session is required AND the AuthType (an Apache-derived concept) isn't shibboleth,
+        string lcAuthType;
+        if (authType.first) {
+            while (*authType.second)
+                lcAuthType += tolower(*authType.second++);
+        }
+
+        // If no session is required AND the AuthType (an Apache-derived concept) isn't recognized,
         // then we ignore this request and consider it unprotected. Apache might lie to us if
         // ShibBasicHijack is on, but that's up to it.
         if ((!requireSession.first || !requireSession.second) && !requireSessionWith.first &&
-#ifdef HAVE_STRCASECMP
-                (!authType.first || strcasecmp(authType.second,"shibboleth")))
-#else
-                (!authType.first || _stricmp(authType.second,"shibboleth")))
-#endif
-            return make_pair(true,request.returnDecline());
+                (!authType.first || m_authTypes.find(lcAuthType) == m_authTypes.end()))
+            return make_pair(true, request.returnDecline());
 
         // Fix for secadv 20050901
         clearHeaders(request);
 
-        Session* session = NULL;
+        Session* session = nullptr;
         try {
             session = request.getSession();
         }
         catch (exception& e) {
-            request.log(SPRequest::SPWarn, string("error during session lookup: ") + e.what());
+            log.warn("error during session lookup: %s", e.what());
             // If it's not a retryable session failure, we throw to the outer handler for reporting.
-            if (dynamic_cast<opensaml::RetryableProfileException*>(&e)==NULL)
+            if (dynamic_cast<opensaml::RetryableProfileException*>(&e)==nullptr)
                 throw;
         }
 
@@ -204,14 +300,14 @@ pair<bool,long> ServiceProvider::doAuthentication(SPRequest& request, bool handl
                 return make_pair(true,request.returnOK());
 
             // No session, but we require one. Initiate a new session using the indicated method.
-            const Handler* initiator=NULL;
+            const SessionInitiator* initiator=nullptr;
             if (requireSessionWith.first) {
                 initiator=app->getSessionInitiatorById(requireSessionWith.second);
-                if (!initiator)
+                if (!initiator) {
                     throw ConfigurationException(
-                        "No session initiator found with id ($1), check requireSessionWith command.",
-                        params(1,requireSessionWith.second)
+                        "No session initiator found with id ($1), check requireSessionWith command.", params(1,requireSessionWith.second)
                         );
+                }
             }
             else {
                 initiator=app->getDefaultSessionInitiator();
@@ -222,25 +318,19 @@ pair<bool,long> ServiceProvider::doAuthentication(SPRequest& request, bool handl
             return initiator->run(request,false);
         }
 
+        request.setAuthType(lcAuthType.c_str());
+
         // We're done.  Everything is okay.  Nothing to report.  Nothing to do..
         // Let the caller decide how to proceed.
-        request.log(SPRequest::SPDebug, "doAuthentication succeeded");
-        return make_pair(false,0);
+        log.debug("doAuthentication succeeded");
+        return make_pair(false,0L);
     }
     catch (exception& e) {
+        request.log(SPRequest::SPError, e.what());
         TemplateParameters tp(&e);
         tp.m_map["requestURL"] = targetURL.substr(0,targetURL.find('?'));
-        return make_pair(true,sendError(request, app, "session", tp));
+        return make_pair(true,sendError(log, request, app, "session", tp));
     }
-#ifndef _DEBUG
-    catch (...) {
-        TemplateParameters tp;
-        tp.m_map["errorType"] = "Unexpected Error";
-        tp.m_map["errorText"] = "Caught an unknown exception.";
-        tp.m_map["requestURL"] = targetURL.substr(0,targetURL.find('?'));
-        return make_pair(true,sendError(request, app, "session", tp));
-    }
-#endif
 }
 
 pair<bool,long> ServiceProvider::doAuthorization(SPRequest& request) const
@@ -248,8 +338,9 @@ pair<bool,long> ServiceProvider::doAuthorization(SPRequest& request) const
 #ifdef _DEBUG
     xmltooling::NDC ndc("doAuthorization");
 #endif
+    Category& log = Category::getInstance(SHIBSP_LOGCAT".ServiceProvider");
 
-    const Application* app=NULL;
+    const Application* app=nullptr;
     string targetURL = request.getRequestURL();
 
     try {
@@ -261,58 +352,58 @@ pair<bool,long> ServiceProvider::doAuthorization(SPRequest& request) const
         pair<bool,bool> requireSession = settings.first->getBool("requireSession");
         pair<bool,const char*> requireSessionWith = settings.first->getString("requireSessionWith");
 
-        // If no session is required AND the AuthType (an Apache-derived concept) isn't shibboleth,
+        string lcAuthType;
+        if (authType.first) {
+            while (*authType.second)
+                lcAuthType += tolower(*authType.second++);
+        }
+
+        // If no session is required AND the AuthType (an Apache-derived concept) isn't recognized,
         // then we ignore this request and consider it unprotected. Apache might lie to us if
         // ShibBasicHijack is on, but that's up to it.
         if ((!requireSession.first || !requireSession.second) && !requireSessionWith.first &&
-#ifdef HAVE_STRCASECMP
-                (!authType.first || strcasecmp(authType.second,"shibboleth")))
-#else
-                (!authType.first || _stricmp(authType.second,"shibboleth")))
-#endif
-            return make_pair(true,request.returnDecline());
+                (!authType.first || m_authTypes.find(lcAuthType) == m_authTypes.end()))
+            return make_pair(true, request.returnDecline());
 
         // Do we have an access control plugin?
         if (settings.second) {
-            const Session* session = NULL;
+            const Session* session = nullptr;
             try {
                 session = request.getSession(false);
             }
             catch (exception& e) {
-                request.log(SPRequest::SPWarn, string("unable to obtain session to pass to access control provider: ") + e.what());
+                log.warn("unable to obtain session to pass to access control provider: %s", e.what());
             }
-       
+
             Locker acllock(settings.second);
-            if (settings.second->authorized(request,session)) {
-                // Let the caller decide how to proceed.
-                request.log(SPRequest::SPDebug, "access control provider granted access");
-                return make_pair(false,0);
-            }
-            else {
-                request.log(SPRequest::SPWarn, "access control provider denied access");
-                TemplateParameters tp;
-                tp.m_map["requestURL"] = targetURL;
-                return make_pair(true,sendError(request, app, "access", tp));
+            switch (settings.second->authorized(request,session)) {
+                case AccessControl::shib_acl_true:
+                    log.debug("access control provider granted access");
+                    return make_pair(true,request.returnOK());
+
+                case AccessControl::shib_acl_false:
+                {
+                    log.warn("access control provider denied access");
+                    TemplateParameters tp;
+                    tp.m_map["requestURL"] = targetURL;
+                    return make_pair(true,sendError(log, request, app, "access", tp, false));
+                }
+
+                default:
+                    // Use the "DECLINE" interface to signal we don't know what to do.
+                    return make_pair(true,request.returnDecline());
             }
-            return make_pair(false,0);
         }
-        else
+        else {
             return make_pair(true,request.returnDecline());
+        }
     }
     catch (exception& e) {
+        request.log(SPRequest::SPError, e.what());
         TemplateParameters tp(&e);
         tp.m_map["requestURL"] = targetURL.substr(0,targetURL.find('?'));
-        return make_pair(true,sendError(request, app, "access", tp));
-    }
-#ifndef _DEBUG
-    catch (...) {
-        TemplateParameters tp;
-        tp.m_map["errorType"] = "Unexpected Error";
-        tp.m_map["errorText"] = "Caught an unknown exception.";
-        tp.m_map["requestURL"] = targetURL.substr(0,targetURL.find('?'));
-        return make_pair(true,sendError(request, app, "access", tp));
+        return make_pair(true,sendError(log, request, app, "access", tp));
     }
-#endif
 }
 
 pair<bool,long> ServiceProvider::doExport(SPRequest& request, bool requireSession) const
@@ -320,20 +411,21 @@ pair<bool,long> ServiceProvider::doExport(SPRequest& request, bool requireSessio
 #ifdef _DEBUG
     xmltooling::NDC ndc("doExport");
 #endif
+    Category& log = Category::getInstance(SHIBSP_LOGCAT".ServiceProvider");
 
-    const Application* app=NULL;
+    const Application* app=nullptr;
     string targetURL = request.getRequestURL();
 
     try {
         RequestMapper::Settings settings = request.getRequestSettings();
         app = &(request.getApplication());
 
-        const Session* session = NULL;
+        const Session* session = nullptr;
         try {
             session = request.getSession(false);
         }
         catch (exception& e) {
-            request.log(SPRequest::SPWarn, string("unable to obtain session to export to request: ") +  e.what());
+            log.warn("unable to obtain session to export to request: %s", e.what());
                // If we have to have a session, then this is a fatal error.
                if (requireSession)
                        throw;
@@ -344,32 +436,38 @@ pair<bool,long> ServiceProvider::doExport(SPRequest& request, bool requireSessio
                if (requireSession)
                 throw opensaml::RetryableProfileException("Unable to obtain session to export to request.");
                else
-                       return make_pair(false,0);      // just bail silently
+                       return make_pair(false,0L);     // just bail silently
         }
-        
-        request.setHeader("Shib-Application-ID", app->getId());
-        request.setHeader("Shib-Session-ID", session->getID());
+
+        app->setHeader(request, "Shib-Application-ID", app->getId());
+        app->setHeader(request, "Shib-Session-ID", session->getID());
 
         // Export the IdP name and Authn method/context info.
         const char* hval = session->getEntityID();
         if (hval)
-            request.setHeader("Shib-Identity-Provider", hval);
+            app->setHeader(request, "Shib-Identity-Provider", hval);
+        hval = session->getAuthnInstant();
+        if (hval)
+            app->setHeader(request, "Shib-Authentication-Instant", hval);
         hval = session->getAuthnContextClassRef();
         if (hval) {
-            request.setHeader("Shib-Authentication-Method", hval);
-            request.setHeader("Shib-AuthnContext-Class", hval);
+            app->setHeader(request, "Shib-Authentication-Method", hval);
+            app->setHeader(request, "Shib-AuthnContext-Class", hval);
         }
         hval = session->getAuthnContextDeclRef();
         if (hval)
-            request.setHeader("Shib-AuthnContext-Decl", hval);
-        
+            app->setHeader(request, "Shib-AuthnContext-Decl", hval);
+        hval = session->getSessionIndex();
+        if (hval)
+            app->setHeader(request, "Shib-Session-Index", hval);
+
         // Maybe export the assertion keys.
         pair<bool,bool> exp=settings.first->getBool("exportAssertion");
         if (exp.first && exp.second) {
             const PropertySet* sessions=app->getPropertySet("Sessions");
-            pair<bool,const char*> exportLocation = sessions ? sessions->getString("exportLocation") : pair<bool,const char*>(false,NULL);
+            pair<bool,const char*> exportLocation = sessions ? sessions->getString("exportLocation") : pair<bool,const char*>(false,nullptr);
             if (!exportLocation.first)
-                request.log(SPRequest::SPWarn, "can't export assertions without an exportLocation Sessions property");
+                log.warn("can't export assertions without an exportLocation Sessions property");
             else {
                 const URLEncoder* encoder = XMLToolingConfig::getConfig().getURLEncoder();
                 string exportName = "Shib-Assertion-00";
@@ -386,26 +484,19 @@ pair<bool,long> ServiceProvider::doExport(SPRequest& request, bool requireSessio
                     *(exportName.rbegin()) = '0' + (count%10);
                     *(++exportName.rbegin()) = '0' + (count/10);
                     string fullURL = baseURL + encoder->encode(*tokenids);
-                    request.setHeader(exportName.c_str(), fullURL.c_str());
+                    app->setHeader(request, exportName.c_str(), fullURL.c_str());
                 }
-                request.setHeader("Shib-Assertion-Count", exportName.c_str() + 15);
+                app->setHeader(request, "Shib-Assertion-Count", exportName.c_str() + 15);
             }
         }
 
         // Export the attributes.
-        bool remoteUserSet = false;
         const multimap<string,const Attribute*>& attributes = session->getIndexedAttributes();
         for (multimap<string,const Attribute*>::const_iterator a = attributes.begin(); a!=attributes.end(); ++a) {
+            if (a->second->isInternal())
+                continue;
+            string header(app->getSecureHeader(request, a->first.c_str()));
             const vector<string>& vals = a->second->getSerializedValues();
-
-            // See if this needs to be set as the REMOTE_USER value.
-            if (!remoteUserSet && !vals.empty() && app->getRemoteUserAttributeIds().count(a->first)) {
-                request.setRemoteUser(vals.front().c_str());
-                remoteUserSet = true;
-            }
-
-            // Handle the normal export case.
-            string header(request.getSecureHeader(a->first.c_str()));
             for (vector<string>::const_iterator v = vals.begin(); v!=vals.end(); ++v) {
                 if (!header.empty())
                     header += ";";
@@ -422,25 +513,33 @@ pair<bool,long> ServiceProvider::doExport(SPRequest& request, bool requireSessio
                     header += (*v);
                 }
             }
-            request.setHeader(a->first.c_str(), header.c_str());
+            app->setHeader(request, a->first.c_str(), header.c_str());
+        }
+
+        // Check for REMOTE_USER.
+        bool remoteUserSet = false;
+        const vector<string>& rmids = app->getRemoteUserAttributeIds();
+        for (vector<string>::const_iterator rmid = rmids.begin(); !remoteUserSet && rmid != rmids.end(); ++rmid) {
+            pair<multimap<string,const Attribute*>::const_iterator,multimap<string,const Attribute*>::const_iterator> matches =
+                attributes.equal_range(*rmid);
+            for (; matches.first != matches.second; ++matches.first) {
+                const vector<string>& vals = matches.first->second->getSerializedValues();
+                if (!vals.empty()) {
+                    request.setRemoteUser(vals.front().c_str());
+                    remoteUserSet = true;
+                    break;
+                }
+            }
         }
-    
-        return make_pair(false,0);
+
+        return make_pair(false,0L);
     }
     catch (exception& e) {
+        request.log(SPRequest::SPError, e.what());
         TemplateParameters tp(&e);
         tp.m_map["requestURL"] = targetURL.substr(0,targetURL.find('?'));
-        return make_pair(true,sendError(request, app, "session", tp));
-    }
-#ifndef _DEBUG
-    catch (...) {
-        TemplateParameters tp;
-        tp.m_map["errorType"] = "Unexpected Error";
-        tp.m_map["errorText"] = "Caught an unknown exception.";
-        tp.m_map["requestURL"] = targetURL.substr(0,targetURL.find('?'));
-        return make_pair(true,sendError(request, app, "session", tp));
+        return make_pair(true,sendError(log, request, app, "session", tp));
     }
-#endif
 }
 
 pair<bool,long> ServiceProvider::doHandler(SPRequest& request) const
@@ -448,14 +547,40 @@ pair<bool,long> ServiceProvider::doHandler(SPRequest& request) const
 #ifdef _DEBUG
     xmltooling::NDC ndc("doHandler");
 #endif
+    Category& log = Category::getInstance(SHIBSP_LOGCAT".ServiceProvider");
 
-    const Application* app=NULL;
+    const Application* app=nullptr;
     string targetURL = request.getRequestURL();
 
     try {
         RequestMapper::Settings settings = request.getRequestSettings();
         app = &(request.getApplication());
 
+        // If not SSL, check to see if we should block or redirect it.
+        if (!request.isSecure()) {
+            pair<bool,const char*> redirectToSSL = settings.first->getString("redirectToSSL");
+            if (redirectToSSL.first) {
+#ifdef HAVE_STRCASECMP
+                if (!strcasecmp("GET",request.getMethod()) || !strcasecmp("HEAD",request.getMethod())) {
+#else
+                if (!stricmp("GET",request.getMethod()) || !stricmp("HEAD",request.getMethod())) {
+#endif
+                    // Compute the new target URL
+                    string redirectURL = string("https://") + request.getHostname();
+                    if (strcmp(redirectToSSL.second,"443")) {
+                        redirectURL = redirectURL + ':' + redirectToSSL.second;
+                    }
+                    redirectURL += request.getRequestURI();
+                    return make_pair(true, request.sendRedirect(redirectURL.c_str()));
+                }
+                else {
+                    TemplateParameters tp;
+                    tp.m_map["requestURL"] = targetURL.substr(0,targetURL.find('?'));
+                    return make_pair(true,sendError(log, request, app, "ssl", tp, false));
+                }
+            }
+        }
+
         const char* handlerURL=request.getHandlerURL(targetURL.c_str());
         if (!handlerURL)
             throw ConfigurationException("Cannot determine handler from resource URL, check configuration.");
@@ -470,7 +595,7 @@ pair<bool,long> ServiceProvider::doHandler(SPRequest& request) const
 
         // Process incoming request.
         pair<bool,bool> handlerSSL=sessionProps->getBool("handlerSSL");
-      
+
         // Make sure this is SSL, if it should be
         if ((!handlerSSL.first || handlerSSL.second) && !request.isSecure())
             throw opensaml::FatalProfileException("Blocked non-SSL access to Shibboleth handler.");
@@ -486,33 +611,14 @@ pair<bool,long> ServiceProvider::doHandler(SPRequest& request) const
         // Did the handler run successfully?
         if (hret.first)
             return hret;
-       
+
         throw ConfigurationException("Configured Shibboleth handler failed to process the request.");
     }
-    catch (opensaml::saml2md::MetadataException& e) {
-        TemplateParameters tp(&e);
-        tp.m_map["requestURL"] = targetURL.substr(0,targetURL.find('?'));
-        // See if a metadata error page is installed.
-        const PropertySet* props=app ? app->getPropertySet("Errors") : NULL;
-        if (props) {
-            pair<bool,const char*> p=props->getString("metadata");
-            if (p.first)
-                return make_pair(true,sendError(request, app, "metadata", tp, true));
-        }
-        return make_pair(true,sendError(request, app, "session", tp, true));
-    }
     catch (exception& e) {
+        request.log(SPRequest::SPError, e.what());
         TemplateParameters tp(&e);
         tp.m_map["requestURL"] = targetURL.substr(0,targetURL.find('?'));
-        return make_pair(true,sendError(request, app, "session", tp, true));
+        tp.m_request = &request;
+        return make_pair(true,sendError(log, request, app, "session", tp));
     }
-#ifndef _DEBUG
-    catch (...) {
-        TemplateParameters tp;
-        tp.m_map["errorType"] = "Unexpected Error";
-        tp.m_map["errorText"] = "Caught an unknown exception.";
-        tp.m_map["requestURL"] = targetURL.substr(0,targetURL.find('?'));
-        return make_pair(true,sendError(request, app, "session", tp, true));
-    }
-#endif
 }