File: ServiceProvider.cpp

package info (click to toggle)
shibboleth-sp2 2.6.0%2Bdfsg1-4%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 7,896 kB
  • sloc: cpp: 39,404; sh: 11,726; makefile: 866; xml: 371; ansic: 35
file content (753 lines) | stat: -rw-r--r-- 33,265 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
/**
 * 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.
 *
 * 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
 *
 * 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.
 */

/**
 * ServiceProvider.cpp
 *
 * Interface to a Shibboleth ServiceProvider instance.
 */

#include "internal.h"
#include "exceptions.h"
#include "AccessControl.h"
#include "Application.h"
#include "ServiceProvider.h"
#include "SessionCache.h"
#include "SPRequest.h"
#include "attribute/Attribute.h"
#include "handler/SessionInitiator.h"
#include "util/TemplateParameters.h"

#include <fstream>
#include <sstream>
#include <boost/algorithm/string.hpp>
#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(
        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) {
            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","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");
            if (!pathname.first)
                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 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 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);
        }

        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) {
        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");
        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.clearHeader(request, "Shib-Handler", "HTTP_SHIB_HANDLER");
        app.clearAttributeHeaders(request);
        request.clearHeader("REMOTE_USER", "HTTP_REMOTE_USER");
    }

    void SHIBSP_DLLLOCAL exportAttributes(SPRequest& request, const Session* session, RequestMapper::Settings settings) {

        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();

        pair<bool,bool> exportDups = settings.first->getBool("exportDuplicateValues");
        const multimap<string,const Attribute*>& attributes = session->getIndexedAttributes();

        // Default export strategy will include duplicates.
        if (!exportDups.first || exportDups.second) {
            for (multimap<string,const Attribute*>::const_iterator a = attributes.begin(); a != attributes.end(); ++a) {
                if (a->second->isInternal())
                    continue;
                string header(request.getApplication().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) {
                    if (!header.empty())
                        header += ';';
                    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);
                        }
                    }
                }
                request.getApplication().setHeader(request, a->first.c_str(), header.c_str());
            }
        }
        else {
            // Capture values in a map of sets to check for duplicates on the fly.
            map< string,set<string> > valueMap;
            for (multimap<string,const Attribute*>::const_iterator a = attributes.begin(); a != attributes.end(); ++a) {
                if (a->second->isInternal())
                    continue;
                const vector<string>& vals = a->second->getSerializedValues();
                valueMap[a->first].insert(vals.begin(), vals.end());
            }

            // Export the mapped sets to the headers.
            for (map< string,set<string> >::const_iterator deduped = valueMap.begin(); deduped != valueMap.end(); ++deduped) {
                string header;
                for (set<string>::const_iterator v = deduped->second.begin(); v != deduped->second.end(); ++v) {
                    if (!header.empty())
                        header += ';';
                    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);
                        }
                    }
                }
                request.getApplication().setHeader(request, deduped->first.c_str(), header.c_str());
            }
        }

        // Check for REMOTE_USER.
        bool remoteUserSet = false;
        const vector<string>& rmids = request.getApplication().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()) {
                    if (enc.first)
                        request.setRemoteUser(encoder->encode(vals.front().c_str()).c_str());
                    else
                        request.setRemoteUser(vals.front().c_str());
                    remoteUserSet = true;
                    break;
                }
            }
        }
    }
};

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 = 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.");

        // 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 (boost::contains(targetURL, handlerURL)) {
            if (handler)
                return doHandler(request);
            else
                return make_pair(true, request.returnOK());
        }

        // 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 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 &&
                (!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 = nullptr;
        try {
            session = request.getSession(true, false, false);   // don't cache it
        }
        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) == nullptr)
                throw;
        }

        Locker slocker(session, false); // pop existing lock on exit
        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 += '&';
                    else
                        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()));
                }
            }
            app->setHeader(request, "Shib-Handler", handlerURL);
        }
        else {
            // No session.  Maybe that's acceptable?
            if ((!requireSession.first || !requireSession.second) && !requireSessionWith.first) {
                app->setHeader(request, "Shib-Handler", handlerURL);
                return make_pair(true, request.returnOK());
            }

            // No session, but we require one. Initiate a new session using the indicated method.
            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)
                        );
                }
            }
            else {
                initiator=app->getDefaultSessionInitiator();
                if (!initiator)
                    throw ConfigurationException("No default session initiator found, check configuration.");
            }

            // Dispatch to SessionInitiator. This MUST handle the request, or we want to fail here.
            // Used to fall through into doExport, but this is a cleaner exit path.
            try {
                pair<bool, long> ret = initiator->run(request, false);
                if (ret.first)
                    return ret;
                throw ConfigurationException("Session initiator did not handle request for a new session, check configuration.");
            }
            catch (XMLToolingException& ex) {
                if (!ex.getProperty("eventType") && initiator->getEventType())
                    ex.addProperty("eventType", initiator->getEventType());
                throw;
            }
        }

        request.setAuthType(authType.second);

        // We're done.  Everything is okay.  Nothing to report.  Nothing to do..
        // Let the caller decide how to proceed.
        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(log, request, app, "session", tp));
    }
}

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 = nullptr;
    Session* session = nullptr;
    Locker slocker;
    string targetURL = request.getRequestURL();

    try {
        RequestMapper::Settings settings = request.getRequestSettings();
        app = &(request.getApplication());

        // Three 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");

        // 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 &&
                (!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) {
            try {
                session = request.getSession(false, false, false);  // ignore timeout and do not cache
                if (session)
                    slocker.assign(session, false); // assign to lock popper
            }
            catch (exception& e) {
                log.warn("unable to obtain session to pass to access control provider: %s", e.what());
            }

            Locker acllock(settings.second);
            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(nullptr, nullptr, session);
                    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());
            }
        }
        else {
            return make_pair(true, request.returnDecline());
        }
    }
    catch (exception& e) {
        request.log(SPRequest::SPError, e.what());
        TemplateParameters tp(&e, nullptr, session);
        tp.m_map["requestURL"] = targetURL.substr(0,targetURL.find('?'));
        return make_pair(true, sendError(log, request, app, "access", tp));
    }
}

pair<bool,long> ServiceProvider::doExport(SPRequest& request, bool requireSession) const
{
#ifdef _DEBUG
    xmltooling::NDC ndc("doExport");
#endif
    Category& log = Category::getInstance(SHIBSP_LOGCAT ".ServiceProvider");

    const Application* app = nullptr;
    Session* session = nullptr;
    Locker slocker;
    string targetURL = request.getRequestURL();

    try {
        RequestMapper::Settings settings = request.getRequestSettings();
        app = &(request.getApplication());

        try {
            session = request.getSession(false, false, false);  // ignore timeout and do not cache
            if (session)
                slocker.assign(session, false); // assign to lock popper
        }
        catch (exception& e) {
            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;
        }

		// Still no data?
        if (!session) {
        	if (requireSession)
                throw opensaml::RetryableProfileException("Unable to obtain session to export to request.");
        	else
        		return make_pair(false, 0L);	// just bail silently
        }

        app->setHeader(request, "Shib-Application-ID", app->getId());
        app->setHeader(request, "Shib-Session-ID", session->getID());

        // 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());
        }

        // 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,nullptr);
            if (!exportLocation.first)
                log.warn("can't export assertions without an exportLocation Sessions property");
            else {
                string exportName = "Shib-Assertion-00";
                string baseURL;
                if (!strncmp(exportLocation.second, "http", 4))
                    baseURL = exportLocation.second;
                else
                    baseURL = string(request.getHandlerURL(targetURL.c_str())) + exportLocation.second;
                baseURL = baseURL + "?key=" + session->getID() + "&ID=";
                const vector<const char*>& tokens = session->getAssertionIDs();
                vector<const char*>::size_type count = 0;
                for (vector<const char*>::const_iterator tokenids = tokens.begin(); tokenids!=tokens.end(); ++tokenids) {
                    count++;
                    *(exportName.rbegin()) = '0' + (count%10);
                    *(++exportName.rbegin()) = '0' + (count/10);
                    string fullURL = baseURL + XMLToolingConfig::getConfig().getURLEncoder()->encode(*tokenids);
                    app->setHeader(request, exportName.c_str(), fullURL.c_str());
                }
                app->setHeader(request, "Shib-Assertion-Count", exportName.c_str() + 15);
            }
        }

        // Export the attributes.
        exportAttributes(request, session, settings);

        return make_pair(false,0L);
    }
    catch (exception& e) {
        request.log(SPRequest::SPError, e.what());
        TemplateParameters tp(&e, nullptr, session);
        tp.m_map["requestURL"] = targetURL.substr(0,targetURL.find('?'));
        return make_pair(true, sendError(log, request, app, "session", tp));
    }
}

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 = 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.");

        // Make sure we only process handler requests.
        if (!boost::contains(targetURL, handlerURL))
            return make_pair(true, request.returnDecline());

        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");

        // 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.");

        // 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));
        if (!handler)
            throw ConfigurationException("Shibboleth handler invoked at an unconfigured location.");

        try {
            pair<bool, long> hret = handler->run(request);
            // Did the handler run successfully?
            if (hret.first)
                return hret;
            throw ConfigurationException("Configured Shibboleth handler failed to process the request.");
        }
        catch (XMLToolingException& ex) {
            if (!ex.getProperty("eventType") && handler->getEventType())
                ex.addProperty("eventType", handler->getEventType());
            throw;
        }
    }
    catch (exception& e) {
        request.log(SPRequest::SPError, e.what());
        Session* session = nullptr;
        try {
            session = request.getSession(false, true, false);   // do not cache
        }
        catch (exception&) {
        }
        Locker slocker(session, false); // pop existing lock on exit
        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));
    }
}