2 * The Shibboleth License, Version 1.
4 * University Corporation for Advanced Internet Development, Inc.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
11 * Redistributions of source code must retain the above copyright notice, this
12 * list of conditions and the following disclaimer.
14 * Redistributions in binary form must reproduce the above copyright notice,
15 * this list of conditions and the following disclaimer in the documentation
16 * and/or other materials provided with the distribution, if any, must include
17 * the following acknowledgment: "This product includes software developed by
18 * the University Corporation for Advanced Internet Development
19 * <http://www.ucaid.edu>Internet2 Project. Alternately, this acknowledegement
20 * may appear in the software itself, if and wherever such third-party
21 * acknowledgments normally appear.
23 * Neither the name of Shibboleth nor the names of its contributors, nor
24 * Internet2, nor the University Corporation for Advanced Internet Development,
25 * Inc., nor UCAID may be used to endorse or promote products derived from this
26 * software without specific prior written permission. For written permission,
27 * please contact shibboleth@shibboleth.org
29 * Products derived from this software may not be called Shibboleth, Internet2,
30 * UCAID, or the University Corporation for Advanced Internet Development, nor
31 * may Shibboleth appear in their name, without prior written permission of the
32 * University Corporation for Advanced Internet Development.
35 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
36 * AND WITH ALL FAULTS. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
38 * PARTICULAR PURPOSE, AND NON-INFRINGEMENT ARE DISCLAIMED AND THE ENTIRE RISK
39 * OF SATISFACTORY QUALITY, PERFORMANCE, ACCURACY, AND EFFORT IS WITH LICENSEE.
40 * IN NO EVENT SHALL THE COPYRIGHT OWNER, CONTRIBUTORS OR THE UNIVERSITY
41 * CORPORATION FOR ADVANCED INTERNET DEVELOPMENT, INC. BE LIABLE FOR ANY DIRECT,
42 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
43 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
44 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
46 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
47 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
50 /* nsapi_shib.cpp - Shibboleth NSAPI filter
56 #include "config_win32.h"
59 #include <saml/saml.h>
60 #include <shib/shib.h>
61 #include <shib/shib-threads.h>
62 #include <shib-target/shib-target.h>
64 #include <log4cpp/Category.hh>
86 using namespace log4cpp;
88 using namespace shibboleth;
89 using namespace shibtarget;
91 // macros to output text to client
92 #define NET_WRITE(str) \
93 if (IO_ERROR==net_write(sn->csd,str,strlen(str))) return REQ_EXIT
96 /**************************************************************************/
97 /* This isn't used anywhere -- why have it? */
98 #define NET_WRITE1(buf,fmstr,param) \
99 do { sprintf(buf,fmstr,param); NET_WRITE(buf); } while(0)
101 #define NET_WRITE2(buf,fmstr,param1,param2) \
102 do { sprintf(buf,fmstr,param1,param2); NET_WRITE(buf); } while(0)
104 #define NET_WRITE3(buf,fmstr,param1,param2,param3) \
105 do { sprintf(buf,fmstr,param1,param2,param3); NET_WRITE(buf); } while(0)
107 #define NET_WRITE4(buf,fmstr,param1,param2,param3,param4) \
108 do { sprintf(buf,fmstr,param1,param2,param3,param4); NET_WRITE(buf); } while(0)
109 /**************************************************************************/
113 ShibTargetConfig* g_Config=NULL;
115 string g_ServerScheme;
118 extern "C" NSAPI_PUBLIC void nsapi_shib_exit(void*)
121 g_Config->shutdown();
125 extern "C" NSAPI_PUBLIC int nsapi_shib_init(pblock* pb, Session* sn, Request* rq)
127 // Save off a default hostname for this virtual server.
128 char* name=pblock_findval("server-name",pb);
132 name=server_hostname;
136 name=util_hostname();
142 pblock_nvinsert("error","unable to determine web server hostname",pb);
147 name=pblock_findval("server-scheme",pb);
151 log_error(LOG_INFORM,"nsapi_shib_init",sn,rq,"nsapi_shib loaded for host (%s)",g_ServerName.c_str());
155 const char* schemadir=pblock_findval("shib-schemas",pb);
157 schemadir=getenv("SHIBSCHEMAS");
159 schemadir=SHIB_SCHEMAS;
160 const char* config=pblock_findval("shib-config",pb);
162 config=getenv("SHIBCONFIG");
165 g_Config=&ShibTargetConfig::getConfig();
166 g_Config->setFeatures(
167 ShibTargetConfig::Listener |
168 ShibTargetConfig::Metadata |
169 ShibTargetConfig::AAP |
170 ShibTargetConfig::RequestMapper |
171 ShibTargetConfig::SHIREExtensions |
172 ShibTargetConfig::Logging
174 if (!g_Config->init(schemadir,config)) {
176 pblock_nvinsert("error","unable to initialize Shibboleth libraries",pb);
180 daemon_atrestart(nsapi_shib_exit,NULL);
188 pblock_nvinsert("error","caught exception, unable to initialize Shibboleth libraries",pb);
194 /********************************************************************************/
195 // NSAPI Shib Target Subclass
197 class ShibTargetNSAPI : public ShibTarget
200 ShibTargetNSAPI(pblock* pb, Session* sn, Request* rq) {
201 // Get everything but hostname...
202 const char* uri=pblock_findval("uri", rq->reqpb);
203 const char* qstr=pblock_findval("query", rq->reqpb);
204 int port=server_portnum;
205 const char* scheme=security_active ? "https" : "http";
206 const char* host=NULL;
212 url=url + '?' + qstr;
214 #ifdef vs_is_default_vs
215 // This is 6.0 or later, so we can distinguish requests to name-based vhosts.
216 if (!vs_is_default_vs)
217 // The beauty here is, a non-default vhost can *only* be accessed if the client
218 // specified the exact name in the Host header. So we can trust the Host header.
219 host=pblock_findval("host", rq->headers);
222 // In other cases, we're going to rely on the initialization process...
223 host=g_ServerName.c_str();
225 char *content_type = NULL;
226 if (request_header("content-type", &content_type, sn, rq) != REQ_PROCEED)
227 throw("Bad Content Type");
229 const char *remote_ip = pblock_findval("ip", sn->client);
230 const char *method = pblock_findval("method", rq->reqpb);
233 g_Config, scheme, host, port, url.c_str(), content_type,
241 ~ShibTargetNSAPI() { }
243 virtual void log(ShibLogLevel level, const string &msg) {
244 log_error((level == LogLevelDebug ? LOG_INFORM :
245 (level == LogLevelInfo ? LOG_INFORM :
246 (level == LogLevelWarn ? LOG_FAILURE :
248 "NSAPI_SHIB", m_sn, m_rq, msg.c_str());
250 virtual string getCookies(void) {
251 char *cookies = NULL;
252 if (request_header("cookie", &cookies, m_sn, m_rq) == REQ_ABORTED)
253 throw("error accessing cookie header");
254 return string(cookies ? cookies : "");
256 virtual void setCookie(const string &name, const string &value) {
257 string cookie = name + '=' + value;
258 pblock_nvinsert("Set-Cookie", cookie.c_str(), m_rq->srvhdrs);
260 virtual string getArgs(void) {
261 const char *q = pblock_findval("query", m_rq->reqpb);
262 return string(q ? q : "");
264 virtual string getPostData(void) {
265 char* content_length=NULL;
266 if (request_header("content-length", &content_length, m_sn, m_rq)
267 !=REQ_PROCEED || atoi(content_length) > 1024*1024) // 1MB?
268 throw ShibTargetException(SHIBRPC_OK,
269 "blocked too-large a post to Shibboleth session processor");
272 int cl=atoi(content_length);
274 while (cl && ch != IO_EOF) {
275 ch=netbuf_getc(m_sn->inbuf);
284 throw ShibTargetException(SHIBRPC_OK,"error reading POST data from browser");
288 virtual void clearHeader(const string &name) {
289 // srvhdrs or headers?
290 param_free(pblock_remove(name.c_str(), m_rq->headers));
292 virtual void setHeader(const string &name, const string &value) {
293 // srvhdrs or headers?
294 pblock_nvinsert(name.c_str(), value.c_str() ,m_rq->srvhdrs);
296 virtual string getHeader(const string &name) {
297 const char *hdr = NULL;
298 if (request_header(name.c_str(), &hdr, m_sn, m_rq) != REQ_PROCEED)
299 hdr = NULL; // XXX: throw an exception here?
300 return string(hdr ? hdr : "");
302 virtual void setRemoteUser(const string &user) {
303 pblock_nvinsert("remote-user", user.c_str(), m_rq->headers);
304 pblock_nvinsert("auth-user", user.c_str(), m_rq->vars);
306 virtual string getRemoteUser(void) {
307 return getHeader("remote-user");
309 // Override this function because we want to add the NSAPI Directory override
310 virtual pair<bool,bool> getRequireSession(IRequestMapper::Settings &settings) {
311 pair<bool,bool> requireSession=settings.first->getBool("requireSession");
312 if (!requireSession.first || !requireSession.second) {
313 const char* param=pblock_findval("require-session",pb);
314 if (param && (!strcmp(param,"1") || !strcasecmp(param,"true")))
315 requireSession.second=true;
317 return requireSession;
320 virtual void* sendPage(
322 const string& content_type,
323 const saml::Iterator<header_t>& headers=EMPTY(header_t),
326 pblock_nvinsert("Content-Type", content_type.c_str(), m_rq->srvhdrs);
327 // XXX: Do we need content-length: or connection: close headers?
328 while (headers.hasNext()) {
329 const header_t& h=headers.next();
330 pblock_nvinsert(h.first.c_str(), h.second.c_str(), m_rq->srvhdrs);
332 protocol_status(m_sn, m_rq, PROTOCOL_OK, NULL);
333 NET_WRITE(const_cast<char*>(msg.c_str()));
334 return (VOID*)REQ_EXIT;
336 virtual void* sendRedirect(const string& url) {
337 pblock_nvinsert("Content-Type", "text/html", m_rq->srvhdrs);
338 pblock_nvinsert("Content-Length", "40", m_rq->srvhdrs);
339 pblock_nvinsert("Expires", "01-Jan-1997 12:00:00 GMT", m_rq->srvhdrs);
340 pblock_nvinsert("Cache-Control", "private,no-store,no-cache", m_rq->srvhdrs);
341 pblock_nvinsert("Location", url.c_str(), m_rq->srvhdrs);
342 protocol_status(m_sn, m_rq, PROTOCOL_REDIRECT, "302 Please wait");
343 protocol_start_response(m_sn, m_rq);
344 NET_WRITE("<HTML><BODY>Redirecting...</BODY></HTML>");
345 return (void*)REQ_EXIT;
347 virtual void* returnDecline(void) { return (void*)REQ_PROCEED; } // XXX?
348 virtual void* returnOK(void) { return (void*)REQ_PROCEED; }
355 /********************************************************************************/
357 int WriteClientError(Session* sn, Request* rq, char* func, char* msg)
359 log_error(LOG_FAILURE,func,sn,rq,msg);
360 protocol_status(sn,rq,PROTOCOL_SERVER_ERROR,msg);
365 #define FUNC "shibboleth"
366 extern "C" NSAPI_PUBLIC int nsapi_shib(pblock* pb, Session* sn, Request* rq)
368 ostringstream threadid;
369 threadid << "[" << getpid() << "] nsapi_shib" << '\0';
370 saml::NDC ndc(threadid.str().c_str());
375 ShibTargetNSAPI stn(pb, sn, rq);
377 // Check user authentication
378 pair<bool,void*> res = stn.doCheckAuthN();
379 if (res.first) return (int)res.second;
381 // user authN was okay -- export the assertions now
382 const char* param=pblock_findval("export-assertion", pb);
383 bool doExportAssn = false;
384 if (param && (!strcmp(param,"1") || !strcasecmp(param,"true")))
386 res = stn.doExportAssertions(doExportAssn);
387 if (res.first) return (int)res.second;
389 // Check the Authorization
390 res = stf.doCheckAuthZ();
391 if (res.first) return (int)res.second;
398 return WriteClientError(sn, rq, FUNC, "threw an uncaught exception.");
405 #define FUNC "shib_handler"
406 extern "C" NSAPI_PUBLIC int shib_handler(pblock* pb, Session* sn, Request* rq)
408 ostringstream threadid;
409 threadid << "[" << getpid() << "] shib_handler" << '\0';
410 saml::NDC ndc(threadid.str().c_str());
415 ShibTargetNSAPI stn(pb, sn, rq);
417 pair<bool,void*> res = stn.doHandleProfile();
418 if (res.first) return (int)res.second;
420 return WriteClientError(sn, rq, FUNC, "doHandleProfile() did not do anything.")
424 return WriteClientError(sn, rq, FUNC, "threw an uncaught exception.");
433 IRequestMapper::Settings map_request(pblock* pb, Session* sn, Request* rq, IRequestMapper* mapper, string& target)
435 // Get everything but hostname...
436 const char* uri=pblock_findval("uri",rq->reqpb);
437 const char* qstr=pblock_findval("query",rq->reqpb);
438 int port=server_portnum;
439 const char* scheme=security_active ? "https" : "http";
440 const char* host=NULL;
446 url=url + '?' + qstr;
448 #ifdef vs_is_default_vs
449 // This is 6.0 or later, so we can distinguish requests to name-based vhosts.
450 if (!vs_is_default_vs)
451 // The beauty here is, a non-default vhost can *only* be accessed if the client
452 // specified the exact name in the Host header. So we can trust the Host header.
453 host=pblock_findval("host", rq->headers);
456 // In other cases, we're going to rely on the initialization process...
457 host=g_ServerName.c_str();
459 target=(g_ServerScheme.empty() ? string(scheme) : g_ServerScheme) + "://" + host;
461 // If port is non-default, append it.
462 if ((!security_active && port!=80) || (security_active && port!=443)) {
464 util_snprintf(portbuf,9,"%d",port);
465 target = target + ':' + portbuf;
470 return mapper->getSettingsFromParsedURL(scheme,host,port,url.c_str());
473 int WriteClientError(Session* sn, Request* rq, const IApplication* app, const char* page, ShibMLP& mlp)
475 const IPropertySet* props=app->getPropertySet("Errors");
477 pair<bool,const char*> p=props->getString(page);
479 ifstream infile(p.second);
480 if (!infile.fail()) {
481 const char* res = mlp.run(infile,props);
483 pblock_nvinsert("Content-Type","text/html",rq->srvhdrs);
484 pblock_nninsert("Content-Length",strlen(res),rq->srvhdrs);
485 pblock_nvinsert("Connection","close",rq->srvhdrs);
486 protocol_status(sn,rq,PROTOCOL_OK,NULL);
487 NET_WRITE(const_cast<char*>(res));
494 log_error(LOG_FAILURE,"WriteClientError",sn,rq,"Unable to open error template, check settings.");
495 protocol_status(sn,rq,PROTOCOL_SERVER_ERROR,"Unable to open error template, check settings.");
499 int WriteRedirectPage(Session* sn, Request* rq, const IApplication* app, const char* file, ShibMLP& mlp)
501 ifstream infile(file);
502 if (!infile.fail()) {
503 const char* res = mlp.run(infile,app->getPropertySet("Errors"));
505 pblock_nvinsert("Content-Type","text/html",rq->srvhdrs);
506 pblock_nninsert("Content-Length",strlen(res),rq->srvhdrs);
507 protocol_status(sn,rq,PROTOCOL_OK,NULL);
508 NET_WRITE(const_cast<char*>(res));
512 log_error(LOG_FAILURE,"WriteRedirectPage",sn,rq,"Unable to open redirect template, check settings.");
513 protocol_status(sn,rq,PROTOCOL_SERVER_ERROR,"Unable to open redirect template, check settings.");
518 #define FUNC "shibboleth"
519 extern "C" NSAPI_PUBLIC int nsapi_shib(pblock* pb, Session* sn, Request* rq)
523 ostringstream threadid;
524 threadid << "[" << getpid() << "] nsapi_shib" << '\0';
525 saml::NDC ndc(threadid.str().c_str());
527 // We lock the configuration system for the duration.
528 IConfig* conf=g_Config->getINI();
531 // Map request to application and content settings.
533 IRequestMapper* mapper=conf->getRequestMapper();
534 Locker locker2(mapper);
535 IRequestMapper::Settings settings=map_request(pb,sn,rq,mapper,targeturl);
536 pair<bool,const char*> application_id=settings.first->getString("applicationId");
537 const IApplication* application=conf->getApplication(application_id.second);
539 return WriteClientError(sn,rq,FUNC,"Unable to map request to application settings, check configuration.");
541 // Declare SHIRE object for this request.
542 SHIRE shire(application);
544 const char* shireURL=shire.getShireURL(targeturl.c_str());
546 return WriteClientError(sn,rq,FUNC,"Unable to map request to proper shireURL setting, check configuration.");
548 // If the user is accessing the SHIRE acceptance point, pass it on.
549 if (targeturl.find(shireURL)!=string::npos)
552 // Now check the policy for this request.
553 pair<bool,bool> requireSession=settings.first->getBool("requireSession");
554 if (!requireSession.first || !requireSession.second) {
555 const char* param=pblock_findval("require-session",pb);
556 if (param && (!strcmp(param,"1") || !strcasecmp(param,"true")))
557 requireSession.second=true;
559 pair<const char*,const char*> shib_cookie=shire.getCookieNameProps();
560 pair<bool,bool> httpRedirects=application->getPropertySet("Sessions")->getBool("httpRedirects");
561 pair<bool,const char*> redirectPage=application->getPropertySet("Sessions")->getString("redirectPage");
562 if (httpRedirects.first && !httpRedirects.second && !redirectPage.first)
563 return WriteClientError(sn,rq,FUNC,"HTML-based redirection requires a redirectPage property.");
565 // Check for session cookie.
566 const char* session_id=NULL;
568 if (request_header("cookie",(char**)&session_id,sn,rq)==REQ_ABORTED)
569 return WriteClientError(sn,rq,FUNC,"error accessing cookie header");
571 Category::getInstance("nsapi_shib."FUNC).debug("cookie header is {%s}",session_id ? session_id : "NULL");
572 if (session_id && (session_id=strstr(session_id,shib_cookie.first))) {
573 session_id+=strlen(shib_cookie.first) + 1; /* Skip over the '=' */
574 char* cookieend=strchr(session_id,';');
576 // Chop out just the value portion.
577 cookie.assign(session_id,cookieend-session_id-1);
578 session_id=cookie.c_str();
582 if (!session_id || !*session_id) {
583 // If no session required, bail now.
584 if (!requireSession.second)
587 // No acceptable cookie, and we require a session. Generate an AuthnRequest.
588 const char* areq = shire.getAuthnRequest(targeturl.c_str());
589 if (!httpRedirects.first || httpRedirects.second) {
590 pblock_nvinsert("Content-Type","text/html",rq->srvhdrs);
591 pblock_nvinsert("Content-Length","40",rq->srvhdrs);
592 pblock_nvinsert("Expires","01-Jan-1997 12:00:00 GMT",rq->srvhdrs);
593 pblock_nvinsert("Cache-Control","private,no-store,no-cache",rq->srvhdrs);
594 pblock_nvinsert("Location",areq,rq->srvhdrs);
595 protocol_status(sn,rq,PROTOCOL_REDIRECT,"302 Please wait");
596 protocol_start_response(sn,rq);
597 NET_WRITE("<HTML><BODY>Redirecting...</BODY></HTML>");
601 ShibMLP markupProcessor;
602 markupProcessor.insert("requestURL",areq);
603 return WriteRedirectPage(sn, rq, application, redirectPage.second, markupProcessor);
607 // Make sure this session is still valid.
608 RPCError* status = NULL;
609 ShibMLP markupProcessor;
610 markupProcessor.insert("requestURL", targeturl);
613 status = shire.sessionIsValid(session_id, pblock_findval("ip",sn->client));
615 catch (ShibTargetException &e) {
616 markupProcessor.insert("errorType", "Session Processing Error");
617 markupProcessor.insert("errorText", e.what());
618 markupProcessor.insert("errorDesc", "An error occurred while processing your request.");
619 return WriteClientError(sn, rq, application, "shire", markupProcessor);
623 markupProcessor.insert("errorType", "Session Processing Error");
624 markupProcessor.insert("errorText", "Unexpected Exception");
625 markupProcessor.insert("errorDesc", "An error occurred while processing your request.");
626 return WriteClientError(sn, rq, application, "shire", markupProcessor);
631 if (status->isError()) {
632 if (!requireSession.second)
634 else if (status->isRetryable()) {
635 // Oops, session is invalid. Generate AuthnRequest.
637 const char* areq = shire.getAuthnRequest(targeturl.c_str());
638 if (!httpRedirects.first || httpRedirects.second) {
639 pblock_nvinsert("Content-Type","text/html",rq->srvhdrs);
640 pblock_nvinsert("Content-Length","40",rq->srvhdrs);
641 pblock_nvinsert("Expires","01-Jan-1997 12:00:00 GMT",rq->srvhdrs);
642 pblock_nvinsert("Cache-Control","private,no-store,no-cache",rq->srvhdrs);
643 pblock_nvinsert("Location",areq,rq->srvhdrs);
644 protocol_status(sn,rq,PROTOCOL_REDIRECT,"302 Please wait");
645 protocol_start_response(sn,rq);
646 NET_WRITE("<HTML><BODY>Redirecting...</BODY></HTML>");
650 markupProcessor.insert("requestURL",areq);
651 return WriteRedirectPage(sn, rq, application, redirectPage.second, markupProcessor);
655 // return the error page to the user
656 markupProcessor.insert(*status);
658 return WriteClientError(sn, rq, application, "shire", markupProcessor);
665 vector<SAMLAssertion*> assertions;
666 SAMLAuthenticationStatement* sso_statement=NULL;
669 status = rm.getAssertions(session_id, pblock_findval("ip",sn->client), assertions, &sso_statement);
671 catch (ShibTargetException &e) {
672 markupProcessor.insert("errorType", "Attribute Processing Error");
673 markupProcessor.insert("errorText", e.what());
674 markupProcessor.insert("errorDesc", "An error occurred while processing your request.");
675 return WriteClientError(sn, rq, application, "rm", markupProcessor);
679 markupProcessor.insert("errorType", "Attribute Processing Error");
680 markupProcessor.insert("errorText", "Unexpected Exception");
681 markupProcessor.insert("errorDesc", "An error occurred while processing your request.");
682 return WriteClientError(sn, rq, application, "rm", markupProcessor);
686 if (status->isError()) {
687 markupProcessor.insert(*status);
689 return WriteClientError(sn, rq, application, "rm", markupProcessor);
693 // Do we have an access control plugin?
694 if (settings.second) {
695 Locker acllock(settings.second);
696 if (!settings.second->authorized(*sso_statement,assertions)) {
697 for (int k = 0; k < assertions.size(); k++)
698 delete assertions[k];
699 delete sso_statement;
700 return WriteClientError(sn, rq, application, "access", markupProcessor);
704 // Get the AAP providers, which contain the attribute policy info.
705 Iterator<IAAP*> provs=application->getAAPProviders();
707 // Clear out the list of mapped attributes
708 while (provs.hasNext()) {
709 IAAP* aap=provs.next();
712 Iterator<const IAttributeRule*> rules=aap->getAttributeRules();
713 while (rules.hasNext()) {
714 const char* header=rules.next()->getHeader();
716 param_free(pblock_remove(header,rq->headers));
721 for (int k = 0; k < assertions.size(); k++)
722 delete assertions[k];
723 delete sso_statement;
724 markupProcessor.insert("errorType", "Attribute Processing Error");
725 markupProcessor.insert("errorText", "Unexpected Exception");
726 markupProcessor.insert("errorDesc", "An error occurred while processing your request.");
727 return WriteClientError(sn, rq, application, "rm", markupProcessor);
733 // Maybe export the first assertion.
734 param_free(pblock_remove("remote-user",rq->headers));
735 param_free(pblock_remove("auth-user",rq->vars));
736 param_free(pblock_remove("Shib-Attributes",rq->headers));
737 pair<bool,bool> exp=settings.first->getBool("exportAssertion");
738 if (!exp.first || !exp.second) {
739 const char* param=pblock_findval("export-assertion",pb);
740 if (param && (!strcmp(param,"1") || !strcasecmp(param,"true")))
743 if (exp.second && assertions.size()) {
745 RM::serialize(*(assertions[0]), assertion);
746 string::size_type lfeed;
747 while ((lfeed=assertion.find('\n'))!=string::npos)
748 assertion.erase(lfeed,1);
749 pblock_nvinsert("Shib-Attributes",assertion.c_str(),rq->headers);
752 pblock_nvinsert("auth-type","shibboleth",rq->vars);
753 param_free(pblock_remove("Shib-Origin-Site",rq->headers));
754 param_free(pblock_remove("Shib-Authentication-Method",rq->headers));
755 param_free(pblock_remove("Shib-NameIdentifier-Format",rq->headers));
757 // Export the SAML AuthnMethod and the origin site name.
758 auto_ptr_char os(sso_statement->getSubject()->getNameIdentifier()->getNameQualifier());
759 auto_ptr_char am(sso_statement->getAuthMethod());
760 pblock_nvinsert("Shib-Origin-Site",os.get(),rq->headers);
761 pblock_nvinsert("Shib-Authentication-Method",am.get(),rq->headers);
764 AAP wrapper(provs,sso_statement->getSubject()->getNameIdentifier()->getFormat(),Constants::SHIB_ATTRIBUTE_NAMESPACE_URI);
765 if (!wrapper.fail() && wrapper->getHeader()) {
766 auto_ptr_char form(sso_statement->getSubject()->getNameIdentifier()->getFormat());
767 auto_ptr_char nameid(sso_statement->getSubject()->getNameIdentifier()->getName());
768 pblock_nvinsert("Shib-NameIdentifier-Format",form.get(),pb);
769 if (!strcmp(wrapper->getHeader(),"REMOTE_USER")) {
770 pblock_nvinsert("remote-user",nameid.get(),rq->headers);
771 pblock_nvinsert("auth-user",nameid.get(),rq->vars);
774 pblock_nvinsert(wrapper->getHeader(),nameid.get(),rq->headers);
778 param_free(pblock_remove("Shib-Application-ID",rq->headers));
779 pblock_nvinsert("Shib-Application-ID",application_id.second,rq->headers);
781 // Export the attributes.
782 Iterator<SAMLAssertion*> a_iter(assertions);
783 while (a_iter.hasNext()) {
784 SAMLAssertion* assert=a_iter.next();
785 Iterator<SAMLStatement*> statements=assert->getStatements();
786 while (statements.hasNext()) {
787 SAMLAttributeStatement* astate=dynamic_cast<SAMLAttributeStatement*>(statements.next());
790 Iterator<SAMLAttribute*> attrs=astate->getAttributes();
791 while (attrs.hasNext()) {
792 SAMLAttribute* attr=attrs.next();
794 // Are we supposed to export it?
795 AAP wrapper(provs,attr->getName(),attr->getNamespace());
796 if (wrapper.fail() || !wrapper->getHeader())
799 Iterator<string> vals=attr->getSingleByteValues();
800 if (!strcmp(wrapper->getHeader(),"REMOTE_USER") && vals.hasNext()) {
801 char* principal=const_cast<char*>(vals.next().c_str());
802 pblock_nvinsert("remote-user",principal,rq->headers);
803 pblock_nvinsert("auth-user",principal,rq->vars);
808 const char* h=pblock_findval(wrapper->getHeader(),rq->headers);
811 param_free(pblock_remove(wrapper->getHeader(),rq->headers));
814 for (; vals.hasNext(); it++) {
815 string value = vals.next();
816 for (string::size_type pos = value.find_first_of(";", string::size_type(0));
818 pos = value.find_first_of(";", pos)) {
819 value.insert(pos, "\\");
825 header=header + ';' + value;
827 pblock_nvinsert(wrapper->getHeader(),header.c_str(),rq->headers);
834 for (int k = 0; k < assertions.size(); k++)
835 delete assertions[k];
836 delete sso_statement;
841 return WriteClientError(sn, rq, FUNC,"Out of Memory");
845 return WriteClientError(sn, rq, FUNC,"Server caught an unknown exception.");
849 return WriteClientError(sn, rq, FUNC,"Server reached unreachable code, save my walrus!");
853 #define FUNC "shib_handler"
854 extern "C" NSAPI_PUBLIC int shib_handler(pblock* pb, Session* sn, Request* rq)
857 const IApplication* application=NULL;
860 ostringstream threadid;
861 threadid << "[" << getpid() << "] shib_handler" << '\0';
862 saml::NDC ndc(threadid.str().c_str());
864 // We lock the configuration system for the duration.
865 IConfig* conf=g_Config->getINI();
868 // Map request to application and content settings.
869 IRequestMapper* mapper=conf->getRequestMapper();
870 Locker locker2(mapper);
871 IRequestMapper::Settings settings=map_request(pb,sn,rq,mapper,targeturl);
872 pair<bool,const char*> application_id=settings.first->getString("applicationId");
873 application=conf->getApplication(application_id.second);
874 const IPropertySet* sessionProps=application ? application->getPropertySet("Sessions") : NULL;
875 if (!application || !sessionProps)
876 return WriteClientError(sn,rq,FUNC,"Unable to map request to application settings, check configuration.");
878 SHIRE shire(application);
880 const char* shireURL=shire.getShireURL(targeturl.c_str());
882 return WriteClientError(sn,rq,FUNC,"Unable to map request to proper shireURL setting, check configuration.");
884 // Make sure we only process the SHIRE requests.
885 if (!strstr(targeturl.c_str(),shireURL))
886 return WriteClientError(sn,rq,FUNC,"NSAPI service function can only be invoked to process incoming sessions."
887 "Make sure the mapped file extension or URL doesn't match actual content.");
889 pair<const char*,const char*> shib_cookie=shire.getCookieNameProps();
891 // Make sure this is SSL, if it should be
892 pair<bool,bool> shireSSL=sessionProps->getBool("shireSSL");
893 if (!shireSSL.first || shireSSL.second) {
894 if (!security_active)
895 throw ShibTargetException(SHIBRPC_OK,"blocked non-SSL access to Shibboleth session processor");
898 pair<bool,bool> httpRedirects=sessionProps->getBool("httpRedirects");
899 pair<bool,const char*> redirectPage=sessionProps->getString("redirectPage");
900 if (httpRedirects.first && !httpRedirects.second && !redirectPage.first)
901 return WriteClientError(sn,rq,FUNC,"HTML-based redirection requires a redirectPage property.");
903 // If this is a GET, we manufacture an AuthnRequest.
904 if (!strcasecmp(pblock_findval("method",rq->reqpb),"GET")) {
905 const char* areq=pblock_findval("query",rq->reqpb) ? shire.getLazyAuthnRequest(pblock_findval("query",rq->reqpb)) : NULL;
907 throw ShibTargetException(SHIBRPC_OK, "malformed arguments to request a new session");
908 if (!httpRedirects.first || httpRedirects.second) {
909 pblock_nvinsert("Content-Type","text/html",rq->srvhdrs);
910 pblock_nvinsert("Content-Length","40",rq->srvhdrs);
911 pblock_nvinsert("Expires","01-Jan-1997 12:00:00 GMT",rq->srvhdrs);
912 pblock_nvinsert("Cache-Control","private,no-store,no-cache",rq->srvhdrs);
913 pblock_nvinsert("Location",areq,rq->srvhdrs);
914 protocol_status(sn,rq,PROTOCOL_REDIRECT,"302 Please wait");
915 protocol_start_response(sn,rq);
916 NET_WRITE("<HTML><BODY>Redirecting...</BODY></HTML>");
920 ShibMLP markupProcessor;
921 markupProcessor.insert("requestURL",areq);
922 return WriteRedirectPage(sn, rq, application, redirectPage.second, markupProcessor);
925 else if (strcasecmp(pblock_findval("method",rq->reqpb),"POST"))
926 throw ShibTargetException(SHIBRPC_OK,"blocked non-POST to Shibboleth session processor");
928 // Make sure this POST is an appropriate content type
929 char* content_type=NULL;
930 if (request_header("content-type",&content_type,sn,rq)!=REQ_PROCEED ||
931 !content_type || strcasecmp(content_type,"application/x-www-form-urlencoded"))
932 throw ShibTargetException(SHIBRPC_OK,"blocked bad content-type to Shibboleth session processor");
935 pair<const char*,const char*> elements=pair<const char*,const char*>(NULL,NULL);
936 char* content_length=NULL;
937 if (request_header("content-length",&content_length,sn,rq)!=REQ_PROCEED ||
938 atoi(content_length) > 1024*1024) // 1MB?
939 throw ShibTargetException(SHIBRPC_OK,"blocked too-large a post to Shibboleth session processor");
942 int cl=atoi(content_length);
944 while (cl && ch!=IO_EOF) {
945 ch=netbuf_getc(sn->inbuf);
954 throw ShibTargetException(SHIBRPC_OK,"error reading POST data from browser");
955 elements=shire.getFormSubmission(cgistr.c_str(),cgistr.length());
958 // Make sure the SAML Response parameter exists
959 if (!elements.first || !*elements.first)
960 throw ShibTargetException(SHIBRPC_OK, "Shibboleth POST failed to find SAMLResponse form element");
962 // Make sure the target parameter exists
963 if (!elements.second || !*elements.second)
964 throw ShibTargetException(SHIBRPC_OK, "Shibboleth POST failed to find TARGET form element");
968 RPCError* status=NULL;
969 ShibMLP markupProcessor;
970 markupProcessor.insert("requestURL", targeturl.c_str());
972 status = shire.sessionCreate(elements.first,pblock_findval("ip",sn->client),cookie);
974 catch (ShibTargetException &e) {
975 markupProcessor.insert("errorType", "Session Creation Service Error");
976 markupProcessor.insert("errorText", e.what());
977 markupProcessor.insert("errorDesc", "An error occurred while processing your request.");
978 return WriteClientError(sn, rq, application, "shire", markupProcessor);
982 markupProcessor.insert("errorType", "Session Creation Service Error");
983 markupProcessor.insert("errorText", "Unexpected Exception");
984 markupProcessor.insert("errorDesc", "An error occurred while processing your request.");
985 return WriteClientError(sn, rq, application, "shire", markupProcessor);
989 if (status->isError()) {
990 if (status->isRetryable()) {
992 const char* loc=shire.getAuthnRequest(elements.second);
993 if (!httpRedirects.first || httpRedirects.second) {
994 pblock_nvinsert("Content-Type","text/html",rq->srvhdrs);
995 pblock_nvinsert("Content-Length","40",rq->srvhdrs);
996 pblock_nvinsert("Expires","01-Jan-1997 12:00:00 GMT",rq->srvhdrs);
997 pblock_nvinsert("Cache-Control","private,no-store,no-cache",rq->srvhdrs);
998 pblock_nvinsert("Location",loc,rq->srvhdrs);
999 protocol_status(sn,rq,PROTOCOL_REDIRECT,"302 Please wait");
1000 protocol_start_response(sn,rq);
1001 NET_WRITE("<HTML><BODY>Redirecting...</BODY></HTML>");
1005 markupProcessor.insert("requestURL",loc);
1006 return WriteRedirectPage(sn, rq, application, redirectPage.second, markupProcessor);
1010 // Return this error to the user.
1011 markupProcessor.insert(*status);
1013 return WriteClientError(sn,rq,application,"shire",markupProcessor);
1017 // We've got a good session, set the cookie and redirect to target.
1018 cookie = string(shib_cookie.first) + '=' + cookie + shib_cookie.second;
1019 pblock_nvinsert("Set-Cookie",cookie.c_str(),rq->srvhdrs);
1020 if (!httpRedirects.first || httpRedirects.second) {
1021 pblock_nvinsert("Content-Type","text/html",rq->srvhdrs);
1022 pblock_nvinsert("Content-Length","40",rq->srvhdrs);
1023 pblock_nvinsert("Expires","01-Jan-1997 12:00:00 GMT",rq->srvhdrs);
1024 pblock_nvinsert("Cache-Control","private,no-store,no-cache",rq->srvhdrs);
1025 pblock_nvinsert("Location",elements.second,rq->srvhdrs);
1026 protocol_status(sn,rq,PROTOCOL_REDIRECT,"302 Please wait");
1027 protocol_start_response(sn,rq);
1028 NET_WRITE("<HTML><BODY>Redirecting...</BODY></HTML>");
1032 markupProcessor.insert("requestURL",elements.second);
1033 return WriteRedirectPage(sn, rq, application, redirectPage.second, markupProcessor);
1036 catch (ShibTargetException &e) {
1038 ShibMLP markupProcessor;
1039 markupProcessor.insert("requestURL", targeturl.c_str());
1040 markupProcessor.insert("errorType", "Session Creation Service Error");
1041 markupProcessor.insert("errorText", e.what());
1042 markupProcessor.insert("errorDesc", "An error occurred while processing your request.");
1043 return WriteClientError(sn,rq,application,"shire",markupProcessor);
1049 ShibMLP markupProcessor;
1050 markupProcessor.insert("requestURL", targeturl.c_str());
1051 markupProcessor.insert("errorType", "Session Creation Service Error");
1052 markupProcessor.insert("errorText", "Unexpected Exception");
1053 markupProcessor.insert("errorDesc", "An error occurred while processing your request.");
1054 return WriteClientError(sn,rq,application,"shire",markupProcessor);