-/*
- * Copyright 2001-2009 Internet2
+/**
+ * Licensed to the University Corporation for Advanced Internet
+ * Development, Inc. (UCAID) under one or more contributor license
+ * agreements. See the NOTICE file distributed with this work for
+ * additional information regarding copyright ownership.
*
- * 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
+ * UCAID licenses this file to you under the Apache License,
+ * Version 2.0 (the "License"); you may not use this file except
+ * in compliance with the License. You may obtain a copy of the
+ * License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
+ * either express or implied. See the License for the specific
+ * language governing permissions and limitations under the License.
*/
/**
#include <fstream>
#include <sstream>
+#include <boost/algorithm/string.hpp>
#ifndef SHIBSP_LITE
# include <saml/exceptions.h>
# include <saml/saml2/metadata/MetadataProvider.h>
)
{
// The properties we need can be set in the RequestMap, or the Errors element.
- bool mderror = dynamic_cast<const opensaml::saml2md::MetadataException*>(tp.getRichException())!=NULL;
- pair<bool,const char*> redirectErrors = pair<bool,const char*>(false,NULL);
- pair<bool,const char*> pathname = pair<bool,const char*>(false,NULL);
+ 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 NULL application and point at the default.
+ // Strictly for error handling, detect a nullptr application and point at the default.
if (!app)
- app = request.getServiceProvider().getApplication("default");
+ 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)
log.error(ex.what());
}
+ // Check for redirection on errors instead of template.
if (mayRedirect) {
- // Check for redirection on errors instead of template.
if (!redirectErrors.first && props)
redirectErrors = props->getString("redirectErrors");
if (redirectErrors.first) {
string loc(redirectErrors.second);
+ request.absolutize(loc);
loc = loc + '?' + tp.toQueryString();
return request.sendRedirect(loc.c_str());
}
}
request.setContentType("text/html");
- request.setResponseHeader("Expires","01-Jan-1997 12:00:00 GMT");
- request.setResponseHeader("Cache-Control","private,no-store,no-cache");
+ request.setResponseHeader("Expires","Wed, 01 Jan 1997 12:00:00 GMT");
+ request.setResponseHeader("Cache-Control","private,no-store,no-cache,max-age=0");
+ // 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");
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) {
- fname = string(page) + "Error.html";
- pathname.second = fname.c_str();
+ if (!accesserror) {
+ fname = string(page) + "Error.html";
+ pathname.second = fname.c_str();
+ }
}
else {
fname = pathname.second;
}
- 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.sendError(str);
+ // 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.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);
}
void SHIBSP_DLLLOCAL clearHeaders(SPRequest& request) {
const Application& app = request.getApplication();
+ app.clearHeader(request, "Shib-Cookie-Name", "HTTP_SHIB_COOKIE_NAME");
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");
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
#endif
Category& log = Category::getInstance(SHIBSP_LOGCAT".ServiceProvider");
- const Application* app=NULL;
+ const Application* app = nullptr;
string targetURL = request.getRequestURL();
try {
else {
TemplateParameters tp;
tp.m_map["requestURL"] = targetURL.substr(0,targetURL.find('?'));
- return make_pair(true,sendError(log, request, app, "ssl", tp, false));
+ return make_pair(true, sendError(log, request, app, "ssl", tp, false));
}
}
}
// If the request URL contains the handler base URL for this application, either dispatch
// directly (mainly Apache 2.0) or just pass back control.
- if (strstr(targetURL.c_str(),handlerURL)) {
+ if (boost::contains(targetURL, handlerURL)) {
if (handler)
return doHandler(request);
else
return make_pair(true, request.returnOK());
}
- // Three settings dictate how to proceed.
+ // These settings dictate how to proceed.
pair<bool,const char*> authType = settings.first->getString("authType");
pair<bool,bool> requireSession = settings.first->getBool("requireSession");
pair<bool,const char*> requireSessionWith = settings.first->getString("requireSessionWith");
+ pair<bool,const char*> requireLogoutWith = settings.first->getString("requireLogoutWith");
- // If no session is required AND the AuthType (an Apache-derived concept) isn't shibboleth,
+ // 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(boost::to_lower_copy(string(authType.second))) == 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) {
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;
}
- if (!session) {
+ if (session) {
+ // Check for logout interception.
+ if (requireLogoutWith.first) {
+ // Check for a completion parameter on the query string.
+ const char* qstr = request.getQueryString();
+ if (!qstr || !strstr(qstr, "shiblogoutdone=1")) {
+ // First leg of circuit, so we redirect to the logout endpoint specified with this URL as a return location.
+ string selfurl = request.getRequestURL();
+ if (!qstr)
+ selfurl += '?';
+ selfurl += "shiblogoutdone=1";
+ string loc = requireLogoutWith.second;
+ request.absolutize(loc);
+ if (loc.find('?') != string::npos)
+ loc += '&';
+ else
+ loc += '?';
+ loc += "return=" + XMLToolingConfig::getConfig().getURLEncoder()->encode(selfurl.c_str());
+ return make_pair(true, request.sendRedirect(loc.c_str()));
+ }
+ }
+ }
+ else {
// No session. Maybe that's acceptable?
if ((!requireSession.first || !requireSession.second) && !requireSessionWith.first)
- return make_pair(true,request.returnOK());
+ return make_pair(true, request.returnOK());
// No session, but we require one. Initiate a new session using the indicated method.
- const SessionInitiator* initiator=NULL;
+ const SessionInitiator* initiator=nullptr;
if (requireSessionWith.first) {
initiator=app->getSessionInitiatorById(requireSessionWith.second);
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)
);
}
}
throw ConfigurationException("No default session initiator found, check configuration.");
}
- return initiator->run(request,false);
+ return initiator->run(request, false);
}
- request.setAuthType("shibboleth");
+ request.setAuthType(authType.second);
// We're done. Everything is okay. Nothing to report. Nothing to do..
// Let the caller decide how to proceed.
request.log(SPRequest::SPError, e.what());
TemplateParameters tp(&e);
tp.m_map["requestURL"] = targetURL.substr(0,targetURL.find('?'));
- return make_pair(true,sendError(log, request, app, "session", tp));
+ return make_pair(true, sendError(log, request, app, "session", tp));
}
}
#endif
Category& log = Category::getInstance(SHIBSP_LOGCAT".ServiceProvider");
- const Application* app=NULL;
+ const Application* app = nullptr;
+ const Session* session = nullptr;
string targetURL = request.getRequestURL();
try {
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,
+ // 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(boost::to_lower_copy(string(authType.second))) == m_authTypes.end()))
+ return make_pair(true, request.returnDecline());
// Do we have an access control plugin?
if (settings.second) {
- const Session* session = NULL;
try {
session = request.getSession(false);
}
}
Locker acllock(settings.second);
- switch (settings.second->authorized(request,session)) {
+ switch (settings.second->authorized(request, session)) {
case AccessControl::shib_acl_true:
log.debug("access control provider granted access");
- return make_pair(true,request.returnOK());
+ return make_pair(true, request.returnOK());
case AccessControl::shib_acl_false:
{
log.warn("access control provider denied access");
- TemplateParameters tp;
+ TemplateParameters tp(nullptr, nullptr, session);
tp.m_map["requestURL"] = targetURL;
- return make_pair(true,sendError(log, request, app, "access", tp, false));
+ 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(true, request.returnDecline());
}
}
else {
- return make_pair(true,request.returnDecline());
+ return make_pair(true, request.returnDecline());
}
}
catch (exception& e) {
request.log(SPRequest::SPError, e.what());
- TemplateParameters tp(&e);
+ TemplateParameters tp(&e, nullptr, session);
tp.m_map["requestURL"] = targetURL.substr(0,targetURL.find('?'));
- return make_pair(true,sendError(log, request, app, "access", tp));
+ return make_pair(true, sendError(log, request, app, "access", tp));
}
}
#endif
Category& log = Category::getInstance(SHIBSP_LOGCAT".ServiceProvider");
- const Application* app=NULL;
+ const Application* app = nullptr;
+ const Session* session = nullptr;
string targetURL = request.getRequestURL();
try {
RequestMapper::Settings settings = request.getRequestSettings();
app = &(request.getApplication());
- const Session* session = NULL;
try {
session = request.getSession(false);
}
if (requireSession)
throw opensaml::RetryableProfileException("Unable to obtain session to export to request.");
else
- return make_pair(false,0L); // just bail silently
+ return make_pair(false, 0L); // just bail silently
}
+ pair<bool,const char*> enc = settings.first->getString("encoding");
+ if (enc.first && strcmp(enc.second, "URL"))
+ throw ConfigurationException("Unsupported value for 'encoding' content setting ($1).", params(1,enc.second));
+
+ const URLEncoder* encoder = XMLToolingConfig::getConfig().getURLEncoder();
+
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)
- app->setHeader(request, "Shib-Identity-Provider", hval);
- hval = session->getAuthnInstant();
- if (hval)
- app->setHeader(request, "Shib-Authentication-Instant", hval);
- hval = session->getAuthnContextClassRef();
- if (hval) {
- app->setHeader(request, "Shib-Authentication-Method", hval);
- app->setHeader(request, "Shib-AuthnContext-Class", hval);
+ // Check for export of "standard" variables.
+ // A 3.0 release would switch this default to false and rely solely on the
+ // Assertion extractor plugin and ship out of the box with the same defaults.
+ pair<bool,bool> stdvars = settings.first->getBool("exportStdVars");
+ if (!stdvars.first || stdvars.second) {
+ const char* hval = session->getEntityID();
+ if (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) {
+ app->setHeader(request, "Shib-Authentication-Method", hval);
+ app->setHeader(request, "Shib-AuthnContext-Class", hval);
+ }
+ hval = session->getAuthnContextDeclRef();
+ if (hval)
+ app->setHeader(request, "Shib-AuthnContext-Decl", hval);
+ hval = session->getSessionIndex();
+ if (hval)
+ app->setHeader(request, "Shib-Session-Index", hval);
+ }
+
+ // Check for export of algorithmically-derived portion of cookie names.
+ stdvars = settings.first->getBool("exportCookie");
+ if (stdvars.first && stdvars.second) {
+ pair<string,const char*> cookieprops = app->getCookieNameProps(nullptr);
+ app->setHeader(request, "Shib-Cookie-Name", cookieprops.first.c_str());
}
- hval = session->getAuthnContextDeclRef();
- if (hval)
- app->setHeader(request, "Shib-AuthnContext-Decl", hval);
// Maybe export the assertion keys.
- pair<bool,bool> exp=settings.first->getBool("exportAssertion");
+ 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);
+ const PropertySet* sessions = app->getPropertySet("Sessions");
+ pair<bool,const char*> exportLocation = sessions ? sessions->getString("exportLocation") : pair<bool,const char*>(false,nullptr);
if (!exportLocation.first)
log.warn("can't export assertions without an exportLocation Sessions property");
else {
- const URLEncoder* encoder = XMLToolingConfig::getConfig().getURLEncoder();
string exportName = "Shib-Assertion-00";
string baseURL;
if (!strncmp(exportLocation.second, "http", 4))
// Export the attributes.
const multimap<string,const Attribute*>& attributes = session->getIndexedAttributes();
- for (multimap<string,const Attribute*>::const_iterator a = attributes.begin(); a!=attributes.end(); ++a) {
+ 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();
- for (vector<string>::const_iterator v = vals.begin(); v!=vals.end(); ++v) {
+ for (vector<string>::const_iterator v = vals.begin(); v != vals.end(); ++v) {
if (!header.empty())
header += ";";
- string::size_type pos = v->find_first_of(';',string::size_type(0));
- if (pos!=string::npos) {
- string value(*v);
- for (; pos != string::npos; pos = value.find_first_of(';',pos)) {
- value.insert(pos, "\\");
- pos += 2;
- }
- header += value;
- }
- else {
- header += (*v);
- }
+ if (enc.first) {
+ // If URL-encoding, any semicolons will get escaped anyway.
+ header += encoder->encode(v->c_str());
+ }
+ else {
+ string::size_type pos = v->find_first_of(';', string::size_type(0));
+ if (pos != string::npos) {
+ string value(*v);
+ for (; pos != string::npos; pos = value.find_first_of(';', pos)) {
+ value.insert(pos, "\\");
+ pos += 2;
+ }
+ header += value;
+ }
+ else {
+ header += (*v);
+ }
+ }
}
app->setHeader(request, a->first.c_str(), header.c_str());
}
for (; matches.first != matches.second; ++matches.first) {
const vector<string>& vals = matches.first->second->getSerializedValues();
if (!vals.empty()) {
- request.setRemoteUser(vals.front().c_str());
+ if (enc.first)
+ request.setRemoteUser(encoder->encode(vals.front().c_str()).c_str());
+ else
+ request.setRemoteUser(vals.front().c_str());
remoteUserSet = true;
break;
}
}
catch (exception& e) {
request.log(SPRequest::SPError, e.what());
- TemplateParameters tp(&e);
+ TemplateParameters tp(&e, nullptr, session);
tp.m_map["requestURL"] = targetURL.substr(0,targetURL.find('?'));
- return make_pair(true,sendError(log, request, app, "session", tp));
+ return make_pair(true, sendError(log, request, app, "session", tp));
}
}
#endif
Category& log = Category::getInstance(SHIBSP_LOGCAT".ServiceProvider");
- const Application* app=NULL;
+ const Application* app = nullptr;
string targetURL = request.getRequestURL();
try {
}
}
- const char* handlerURL=request.getHandlerURL(targetURL.c_str());
+ const char* handlerURL = request.getHandlerURL(targetURL.c_str());
if (!handlerURL)
throw ConfigurationException("Cannot determine handler from resource URL, check configuration.");
// Make sure we only process handler requests.
- if (!strstr(targetURL.c_str(),handlerURL))
+ if (!boost::contains(targetURL, handlerURL))
return make_pair(true, request.returnDecline());
- const PropertySet* sessionProps=app->getPropertySet("Sessions");
+ const PropertySet* sessionProps = app->getPropertySet("Sessions");
if (!sessionProps)
throw ConfigurationException("Unable to map request to application session settings, check configuration.");
// Process incoming request.
- pair<bool,bool> handlerSSL=sessionProps->getBool("handlerSSL");
+ pair<bool,bool> handlerSSL = sessionProps->getBool("handlerSSL");
// Make sure this is SSL, if it should be
if ((!handlerSSL.first || handlerSSL.second) && !request.isSecure())
// We dispatch based on our path info. We know the request URL begins with or equals the handler URL,
// so the path info is the next character (or null).
- const Handler* handler=app->getHandler(targetURL.c_str() + strlen(handlerURL));
+ const Handler* handler = app->getHandler(targetURL.c_str() + strlen(handlerURL));
if (!handler)
throw ConfigurationException("Shibboleth handler invoked at an unconfigured location.");
- pair<bool,long> hret=handler->run(request);
+ pair<bool,long> hret = handler->run(request);
// Did the handler run successfully?
if (hret.first)
}
catch (exception& e) {
request.log(SPRequest::SPError, e.what());
- TemplateParameters tp(&e);
- tp.m_map["requestURL"] = targetURL.substr(0,targetURL.find('?'));
+ const Session* session = nullptr;
+ try {
+ session = request.getSession(false, true);
+ }
+ catch (exception&) {
+ }
+ TemplateParameters tp(&e, nullptr, session);
+ tp.m_map["requestURL"] = targetURL.substr(0, targetURL.find('?'));
tp.m_request = &request;
- return make_pair(true,sendError(log, request, app, "session", tp));
+ return make_pair(true, sendError(log, request, app, "session", tp));
}
}