2 * Copyright 2001-2009 Internet2
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
20 * Interface to a Shibboleth ServiceProvider instance.
24 #include "exceptions.h"
25 #include "AccessControl.h"
26 #include "Application.h"
27 #include "ServiceProvider.h"
28 #include "SessionCache.h"
29 #include "SPRequest.h"
30 #include "attribute/Attribute.h"
31 #include "handler/SessionInitiator.h"
32 #include "util/TemplateParameters.h"
37 # include <saml/exceptions.h>
38 # include <saml/saml2/metadata/MetadataProvider.h>
40 #include <xmltooling/XMLToolingConfig.h>
41 #include <xmltooling/util/NDC.h>
42 #include <xmltooling/util/PathResolver.h>
43 #include <xmltooling/util/URLEncoder.h>
44 #include <xmltooling/util/XMLHelper.h>
46 using namespace shibsp;
47 using namespace xmltooling::logging;
48 using namespace xmltooling;
52 SHIBSP_DLLLOCAL PluginManager<ServiceProvider,string,const DOMElement*>::Factory XMLServiceProviderFactory;
54 long SHIBSP_DLLLOCAL sendError(
55 Category& log, SPRequest& request, const Application* app, const char* page, TemplateParameters& tp, bool mayRedirect=true
58 // The properties we need can be set in the RequestMap, or the Errors element.
59 bool mderror = dynamic_cast<const opensaml::saml2md::MetadataException*>(tp.getRichException())!=NULL;
60 pair<bool,const char*> redirectErrors = pair<bool,const char*>(false,NULL);
61 pair<bool,const char*> pathname = pair<bool,const char*>(false,NULL);
63 // Strictly for error handling, detect a NULL application and point at the default.
65 app = request.getServiceProvider().getApplication("default");
67 const PropertySet* props=app->getPropertySet("Errors");
70 RequestMapper::Settings settings = request.getRequestSettings();
72 pathname = settings.first->getString("metadataError");
73 if (!pathname.first) {
74 string pagename(page);
76 pathname = settings.first->getString(pagename.c_str());
79 redirectErrors = settings.first->getString("redirectErrors");
81 catch (exception& ex) {
86 // Check for redirection on errors instead of template.
87 if (!redirectErrors.first && props)
88 redirectErrors = props->getString("redirectErrors");
89 if (redirectErrors.first) {
90 string loc(redirectErrors.second);
91 loc = loc + '?' + tp.toQueryString();
92 return request.sendRedirect(loc.c_str());
96 request.setContentType("text/html");
97 request.setResponseHeader("Expires","01-Jan-1997 12:00:00 GMT");
98 request.setResponseHeader("Cache-Control","private,no-store,no-cache");
100 if (!pathname.first && props) {
102 pathname=props->getString("metadata");
104 pathname=props->getString(page);
106 if (pathname.first) {
107 string fname(pathname.second);
108 ifstream infile(XMLToolingConfig::getConfig().getPathResolver()->resolve(fname, PathResolver::XMLTOOLING_CFG_FILE).c_str());
110 tp.setPropertySet(props);
112 XMLToolingConfig::getConfig().getTemplateEngine()->run(infile, str, tp, tp.getRichException());
113 return request.sendError(str);
117 if (!strcmp(page,"access")) {
118 istringstream msg("Access Denied");
119 return request.sendResponse(msg, HTTPResponse::XMLTOOLING_HTTP_STATUS_FORBIDDEN);
122 log.error("sendError could not process error template (%s)", page);
123 istringstream msg("Internal Server Error. Please contact the site administrator.");
124 return request.sendError(msg);
127 void SHIBSP_DLLLOCAL clearHeaders(SPRequest& request) {
128 const Application& app = request.getApplication();
129 app.clearHeader(request, "Shib-Session-ID", "HTTP_SHIB_SESSION_ID");
130 app.clearHeader(request, "Shib-Identity-Provider", "HTTP_SHIB_IDENTITY_PROVIDER");
131 app.clearHeader(request, "Shib-Authentication-Method", "HTTP_SHIB_AUTHENTICATION_METHOD");
132 app.clearHeader(request, "Shib-Authentication-Instant", "HTTP_SHIB_AUTHENTICATION_INSTANT");
133 app.clearHeader(request, "Shib-AuthnContext-Class", "HTTP_SHIB_AUTHNCONTEXT_CLASS");
134 app.clearHeader(request, "Shib-AuthnContext-Decl", "HTTP_SHIB_AUTHNCONTEXT_DECL");
135 app.clearHeader(request, "Shib-Assertion-Count", "HTTP_SHIB_ASSERTION_COUNT");
136 app.clearAttributeHeaders(request);
137 request.clearHeader("REMOTE_USER", "HTTP_REMOTE_USER");
141 void SHIBSP_API shibsp::registerServiceProviders()
143 SPConfig::getConfig().ServiceProviderManager.registerFactory(XML_SERVICE_PROVIDER, XMLServiceProviderFactory);
146 ServiceProvider::ServiceProvider()
150 ServiceProvider::~ServiceProvider()
154 pair<bool,long> ServiceProvider::doAuthentication(SPRequest& request, bool handler) const
157 xmltooling::NDC ndc("doAuthentication");
159 Category& log = Category::getInstance(SHIBSP_LOGCAT".ServiceProvider");
161 const Application* app=NULL;
162 string targetURL = request.getRequestURL();
165 RequestMapper::Settings settings = request.getRequestSettings();
166 app = &(request.getApplication());
168 // If not SSL, check to see if we should block or redirect it.
169 if (!request.isSecure()) {
170 pair<bool,const char*> redirectToSSL = settings.first->getString("redirectToSSL");
171 if (redirectToSSL.first) {
172 #ifdef HAVE_STRCASECMP
173 if (!strcasecmp("GET",request.getMethod()) || !strcasecmp("HEAD",request.getMethod())) {
175 if (!stricmp("GET",request.getMethod()) || !stricmp("HEAD",request.getMethod())) {
177 // Compute the new target URL
178 string redirectURL = string("https://") + request.getHostname();
179 if (strcmp(redirectToSSL.second,"443")) {
180 redirectURL = redirectURL + ':' + redirectToSSL.second;
182 redirectURL += request.getRequestURI();
183 return make_pair(true, request.sendRedirect(redirectURL.c_str()));
186 TemplateParameters tp;
187 tp.m_map["requestURL"] = targetURL.substr(0,targetURL.find('?'));
188 return make_pair(true,sendError(log, request, app, "ssl", tp, false));
193 const char* handlerURL=request.getHandlerURL(targetURL.c_str());
195 throw ConfigurationException("Cannot determine handler from resource URL, check configuration.");
197 // If the request URL contains the handler base URL for this application, either dispatch
198 // directly (mainly Apache 2.0) or just pass back control.
199 if (strstr(targetURL.c_str(),handlerURL)) {
201 return doHandler(request);
203 return make_pair(true, request.returnOK());
206 // Three settings dictate how to proceed.
207 pair<bool,const char*> authType = settings.first->getString("authType");
208 pair<bool,bool> requireSession = settings.first->getBool("requireSession");
209 pair<bool,const char*> requireSessionWith = settings.first->getString("requireSessionWith");
211 // If no session is required AND the AuthType (an Apache-derived concept) isn't shibboleth,
212 // then we ignore this request and consider it unprotected. Apache might lie to us if
213 // ShibBasicHijack is on, but that's up to it.
214 if ((!requireSession.first || !requireSession.second) && !requireSessionWith.first &&
215 #ifdef HAVE_STRCASECMP
216 (!authType.first || strcasecmp(authType.second,"shibboleth")))
218 (!authType.first || _stricmp(authType.second,"shibboleth")))
220 return make_pair(true,request.returnDecline());
222 // Fix for secadv 20050901
223 clearHeaders(request);
225 Session* session = NULL;
227 session = request.getSession();
229 catch (exception& e) {
230 log.warn("error during session lookup: %s", e.what());
231 // If it's not a retryable session failure, we throw to the outer handler for reporting.
232 if (dynamic_cast<opensaml::RetryableProfileException*>(&e)==NULL)
237 // No session. Maybe that's acceptable?
238 if ((!requireSession.first || !requireSession.second) && !requireSessionWith.first)
239 return make_pair(true,request.returnOK());
241 // No session, but we require one. Initiate a new session using the indicated method.
242 const SessionInitiator* initiator=NULL;
243 if (requireSessionWith.first) {
244 initiator=app->getSessionInitiatorById(requireSessionWith.second);
246 throw ConfigurationException(
247 "No session initiator found with id ($1), check requireSessionWith command.", params(1,requireSessionWith.second)
252 initiator=app->getDefaultSessionInitiator();
254 throw ConfigurationException("No default session initiator found, check configuration.");
257 return initiator->run(request,false);
260 request.setAuthType("shibboleth");
262 // We're done. Everything is okay. Nothing to report. Nothing to do..
263 // Let the caller decide how to proceed.
264 log.debug("doAuthentication succeeded");
265 return make_pair(false,0L);
267 catch (exception& e) {
268 request.log(SPRequest::SPError, e.what());
269 TemplateParameters tp(&e);
270 tp.m_map["requestURL"] = targetURL.substr(0,targetURL.find('?'));
271 return make_pair(true,sendError(log, request, app, "session", tp));
275 pair<bool,long> ServiceProvider::doAuthorization(SPRequest& request) const
278 xmltooling::NDC ndc("doAuthorization");
280 Category& log = Category::getInstance(SHIBSP_LOGCAT".ServiceProvider");
282 const Application* app=NULL;
283 string targetURL = request.getRequestURL();
286 RequestMapper::Settings settings = request.getRequestSettings();
287 app = &(request.getApplication());
289 // Three settings dictate how to proceed.
290 pair<bool,const char*> authType = settings.first->getString("authType");
291 pair<bool,bool> requireSession = settings.first->getBool("requireSession");
292 pair<bool,const char*> requireSessionWith = settings.first->getString("requireSessionWith");
294 // If no session is required AND the AuthType (an Apache-derived concept) isn't shibboleth,
295 // then we ignore this request and consider it unprotected. Apache might lie to us if
296 // ShibBasicHijack is on, but that's up to it.
297 if ((!requireSession.first || !requireSession.second) && !requireSessionWith.first &&
298 #ifdef HAVE_STRCASECMP
299 (!authType.first || strcasecmp(authType.second,"shibboleth")))
301 (!authType.first || _stricmp(authType.second,"shibboleth")))
303 return make_pair(true,request.returnDecline());
305 // Do we have an access control plugin?
306 if (settings.second) {
307 const Session* session = NULL;
309 session = request.getSession(false);
311 catch (exception& e) {
312 log.warn("unable to obtain session to pass to access control provider: %s", e.what());
315 Locker acllock(settings.second);
316 switch (settings.second->authorized(request,session)) {
317 case AccessControl::shib_acl_true:
318 log.debug("access control provider granted access");
319 return make_pair(true,request.returnOK());
321 case AccessControl::shib_acl_false:
323 log.warn("access control provider denied access");
324 TemplateParameters tp;
325 tp.m_map["requestURL"] = targetURL;
326 return make_pair(true,sendError(log, request, app, "access", tp, false));
330 // Use the "DECLINE" interface to signal we don't know what to do.
331 return make_pair(true,request.returnDecline());
335 return make_pair(true,request.returnDecline());
338 catch (exception& e) {
339 request.log(SPRequest::SPError, e.what());
340 TemplateParameters tp(&e);
341 tp.m_map["requestURL"] = targetURL.substr(0,targetURL.find('?'));
342 return make_pair(true,sendError(log, request, app, "access", tp));
346 pair<bool,long> ServiceProvider::doExport(SPRequest& request, bool requireSession) const
349 xmltooling::NDC ndc("doExport");
351 Category& log = Category::getInstance(SHIBSP_LOGCAT".ServiceProvider");
353 const Application* app=NULL;
354 string targetURL = request.getRequestURL();
357 RequestMapper::Settings settings = request.getRequestSettings();
358 app = &(request.getApplication());
360 const Session* session = NULL;
362 session = request.getSession(false);
364 catch (exception& e) {
365 log.warn("unable to obtain session to export to request: %s", e.what());
366 // If we have to have a session, then this is a fatal error.
374 throw opensaml::RetryableProfileException("Unable to obtain session to export to request.");
376 return make_pair(false,0L); // just bail silently
379 app->setHeader(request, "Shib-Application-ID", app->getId());
380 app->setHeader(request, "Shib-Session-ID", session->getID());
382 // Export the IdP name and Authn method/context info.
383 const char* hval = session->getEntityID();
385 app->setHeader(request, "Shib-Identity-Provider", hval);
386 hval = session->getAuthnInstant();
388 app->setHeader(request, "Shib-Authentication-Instant", hval);
389 hval = session->getAuthnContextClassRef();
391 app->setHeader(request, "Shib-Authentication-Method", hval);
392 app->setHeader(request, "Shib-AuthnContext-Class", hval);
394 hval = session->getAuthnContextDeclRef();
396 app->setHeader(request, "Shib-AuthnContext-Decl", hval);
398 // Maybe export the assertion keys.
399 pair<bool,bool> exp=settings.first->getBool("exportAssertion");
400 if (exp.first && exp.second) {
401 const PropertySet* sessions=app->getPropertySet("Sessions");
402 pair<bool,const char*> exportLocation = sessions ? sessions->getString("exportLocation") : pair<bool,const char*>(false,NULL);
403 if (!exportLocation.first)
404 log.warn("can't export assertions without an exportLocation Sessions property");
406 const URLEncoder* encoder = XMLToolingConfig::getConfig().getURLEncoder();
407 string exportName = "Shib-Assertion-00";
409 if (!strncmp(exportLocation.second, "http", 4))
410 baseURL = exportLocation.second;
412 baseURL = string(request.getHandlerURL(targetURL.c_str())) + exportLocation.second;
413 baseURL = baseURL + "?key=" + session->getID() + "&ID=";
414 const vector<const char*>& tokens = session->getAssertionIDs();
415 vector<const char*>::size_type count = 0;
416 for (vector<const char*>::const_iterator tokenids = tokens.begin(); tokenids!=tokens.end(); ++tokenids) {
418 *(exportName.rbegin()) = '0' + (count%10);
419 *(++exportName.rbegin()) = '0' + (count/10);
420 string fullURL = baseURL + encoder->encode(*tokenids);
421 app->setHeader(request, exportName.c_str(), fullURL.c_str());
423 app->setHeader(request, "Shib-Assertion-Count", exportName.c_str() + 15);
427 // Export the attributes.
428 const multimap<string,const Attribute*>& attributes = session->getIndexedAttributes();
429 for (multimap<string,const Attribute*>::const_iterator a = attributes.begin(); a!=attributes.end(); ++a) {
430 if (a->second->isInternal())
432 string header(app->getSecureHeader(request, a->first.c_str()));
433 const vector<string>& vals = a->second->getSerializedValues();
434 for (vector<string>::const_iterator v = vals.begin(); v!=vals.end(); ++v) {
437 string::size_type pos = v->find_first_of(';',string::size_type(0));
438 if (pos!=string::npos) {
440 for (; pos != string::npos; pos = value.find_first_of(';',pos)) {
441 value.insert(pos, "\\");
450 app->setHeader(request, a->first.c_str(), header.c_str());
453 // Check for REMOTE_USER.
454 bool remoteUserSet = false;
455 const vector<string>& rmids = app->getRemoteUserAttributeIds();
456 for (vector<string>::const_iterator rmid = rmids.begin(); !remoteUserSet && rmid != rmids.end(); ++rmid) {
457 pair<multimap<string,const Attribute*>::const_iterator,multimap<string,const Attribute*>::const_iterator> matches =
458 attributes.equal_range(*rmid);
459 for (; matches.first != matches.second; ++matches.first) {
460 const vector<string>& vals = matches.first->second->getSerializedValues();
462 request.setRemoteUser(vals.front().c_str());
463 remoteUserSet = true;
469 return make_pair(false,0L);
471 catch (exception& e) {
472 request.log(SPRequest::SPError, e.what());
473 TemplateParameters tp(&e);
474 tp.m_map["requestURL"] = targetURL.substr(0,targetURL.find('?'));
475 return make_pair(true,sendError(log, request, app, "session", tp));
479 pair<bool,long> ServiceProvider::doHandler(SPRequest& request) const
482 xmltooling::NDC ndc("doHandler");
484 Category& log = Category::getInstance(SHIBSP_LOGCAT".ServiceProvider");
486 const Application* app=NULL;
487 string targetURL = request.getRequestURL();
490 RequestMapper::Settings settings = request.getRequestSettings();
491 app = &(request.getApplication());
493 // If not SSL, check to see if we should block or redirect it.
494 if (!request.isSecure()) {
495 pair<bool,const char*> redirectToSSL = settings.first->getString("redirectToSSL");
496 if (redirectToSSL.first) {
497 #ifdef HAVE_STRCASECMP
498 if (!strcasecmp("GET",request.getMethod()) || !strcasecmp("HEAD",request.getMethod())) {
500 if (!stricmp("GET",request.getMethod()) || !stricmp("HEAD",request.getMethod())) {
502 // Compute the new target URL
503 string redirectURL = string("https://") + request.getHostname();
504 if (strcmp(redirectToSSL.second,"443")) {
505 redirectURL = redirectURL + ':' + redirectToSSL.second;
507 redirectURL += request.getRequestURI();
508 return make_pair(true, request.sendRedirect(redirectURL.c_str()));
511 TemplateParameters tp;
512 tp.m_map["requestURL"] = targetURL.substr(0,targetURL.find('?'));
513 return make_pair(true,sendError(log, request, app, "ssl", tp, false));
518 const char* handlerURL=request.getHandlerURL(targetURL.c_str());
520 throw ConfigurationException("Cannot determine handler from resource URL, check configuration.");
522 // Make sure we only process handler requests.
523 if (!strstr(targetURL.c_str(),handlerURL))
524 return make_pair(true, request.returnDecline());
526 const PropertySet* sessionProps=app->getPropertySet("Sessions");
528 throw ConfigurationException("Unable to map request to application session settings, check configuration.");
530 // Process incoming request.
531 pair<bool,bool> handlerSSL=sessionProps->getBool("handlerSSL");
533 // Make sure this is SSL, if it should be
534 if ((!handlerSSL.first || handlerSSL.second) && !request.isSecure())
535 throw opensaml::FatalProfileException("Blocked non-SSL access to Shibboleth handler.");
537 // We dispatch based on our path info. We know the request URL begins with or equals the handler URL,
538 // so the path info is the next character (or null).
539 const Handler* handler=app->getHandler(targetURL.c_str() + strlen(handlerURL));
541 throw ConfigurationException("Shibboleth handler invoked at an unconfigured location.");
543 pair<bool,long> hret=handler->run(request);
545 // Did the handler run successfully?
549 throw ConfigurationException("Configured Shibboleth handler failed to process the request.");
551 catch (exception& e) {
552 request.log(SPRequest::SPError, e.what());
553 TemplateParameters tp(&e);
554 tp.m_map["requestURL"] = targetURL.substr(0,targetURL.find('?'));
555 tp.m_request = &request;
556 return make_pair(true,sendError(log, request, app, "session", tp));