Change license header.
[shibboleth/cpp-sp.git] / nsapi_shib / nsapi_shib.cpp
1 /**
2  * Licensed to the University Corporation for Advanced Internet
3  * Development, Inc. (UCAID) under one or more contributor license
4  * agreements. See the NOTICE file distributed with this work for
5  * additional information regarding copyright ownership.
6  *
7  * UCAID licenses this file to you under the Apache License,
8  * Version 2.0 (the "License"); you may not use this file except
9  * in compliance with the License. You may obtain a copy of the
10  * License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing,
15  * software distributed under the License is distributed on an
16  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17  * either express or implied. See the License for the specific
18  * language governing permissions and limitations under the License.
19  */
20
21 /**
22  * nsapi_shib.cpp
23  *
24  * Shibboleth NSAPI filter.
25  */
26
27 #define SHIBSP_LITE
28
29 #if defined (_MSC_VER) || defined(__BORLANDC__)
30 # include "config_win32.h"
31 #else
32 # include "config.h"
33 #endif
34
35 #ifdef WIN32
36 # define _CRT_NONSTDC_NO_DEPRECATE 1
37 # define _CRT_SECURE_NO_DEPRECATE 1
38 # define _CRT_RAND_S
39 #endif
40
41 #include <shibsp/exceptions.h>
42 #include <shibsp/AbstractSPRequest.h>
43 #include <shibsp/RequestMapper.h>
44 #include <shibsp/SPConfig.h>
45 #include <shibsp/ServiceProvider.h>
46
47 #include <set>
48 #include <memory>
49 #include <fstream>
50 #include <sstream>
51 #include <stdexcept>
52 #include <xmltooling/XMLToolingConfig.h>
53 #include <xmltooling/util/NDC.h>
54 #include <xmltooling/util/Threads.h>
55 #include <xmltooling/util/XMLConstants.h>
56 #include <xmltooling/util/XMLHelper.h>
57 #include <xercesc/util/XMLUniDefs.hpp>
58
59 #ifdef WIN32
60 # include <process.h>
61 # define XP_WIN32
62 #else
63 # define XP_UNIX
64 #endif
65
66 #define MCC_HTTPD
67 #define NET_SSL
68
69 extern "C"
70 {
71 #include <nsapi.h>
72 }
73
74 using namespace shibsp;
75 using namespace xmltooling;
76 using namespace std;
77
78 // macros to output text to client
79 #define NET_WRITE(str) \
80     if (IO_ERROR==net_write(sn->csd,str,strlen(str))) return REQ_EXIT
81
82 namespace {
83     SPConfig* g_Config=nullptr;
84     string g_ServerName;
85     string g_unsetHeaderValue;
86     string g_spoofKey;
87     bool g_checkSpoofing = true;
88     bool g_catchAll = false;
89
90     static const XMLCh path[] =     UNICODE_LITERAL_4(p,a,t,h);
91     static const XMLCh validate[] = UNICODE_LITERAL_8(v,a,l,i,d,a,t,e);
92
93     void _my_invalid_parameter_handler(
94        const wchar_t * expression,
95        const wchar_t * function,
96        const wchar_t * file,
97        unsigned int line,
98        uintptr_t pReserved
99        ) {
100         return;
101     }
102 }
103
104 PluginManager<RequestMapper,string,const xercesc::DOMElement*>::Factory SunRequestMapFactory;
105
106 extern "C" NSAPI_PUBLIC void nsapi_shib_exit(void*)
107 {
108     if (g_Config)
109         g_Config->term();
110     g_Config = nullptr;
111 }
112
113 extern "C" NSAPI_PUBLIC int nsapi_shib_init(pblock* pb, ::Session* sn, Request* rq)
114 {
115     // Save off a default hostname for this virtual server.
116     char* name=pblock_findval("server-name",pb);
117     if (name)
118         g_ServerName=name;
119     else {
120         name=server_hostname;
121         if (name)
122             g_ServerName=name;
123         else {
124             name=util_hostname();
125             if (name) {
126                 g_ServerName=name;
127                 FREE(name);
128             }
129             else {
130                 pblock_nvinsert("error","unable to determine web server hostname",pb);
131                 return REQ_ABORTED;
132             }
133         }
134     }
135
136     log_error(LOG_INFORM,"nsapi_shib_init",sn,rq,"nsapi_shib loaded for host (%s)",g_ServerName.c_str());
137
138     const char* schemadir=pblock_findval("shib-schemas",pb);
139     const char* prefix=pblock_findval("shib-prefix",pb);
140
141     g_Config=&SPConfig::getConfig();
142     g_Config->setFeatures(
143         SPConfig::Listener |
144         SPConfig::Caching |
145         SPConfig::RequestMapping |
146         SPConfig::InProcess |
147         SPConfig::Logging |
148         SPConfig::Handlers
149         );
150     if (!g_Config->init(schemadir,prefix)) {
151         g_Config=nullptr;
152         pblock_nvinsert("error","unable to initialize Shibboleth libraries",pb);
153         return REQ_ABORTED;
154     }
155
156     g_Config->RequestMapperManager.registerFactory(NATIVE_REQUEST_MAPPER,&SunRequestMapFactory);
157
158     try {
159         if (!g_Config->instantiate(pblock_findval("shib-config",pb), true))
160             throw runtime_error("unknown error");
161     }
162     catch (exception& ex) {
163         pblock_nvinsert("error",ex.what(),pb);
164         g_Config->term();
165         g_Config=nullptr;
166         return REQ_ABORTED;
167     }
168
169     daemon_atrestart(nsapi_shib_exit,nullptr);
170
171     ServiceProvider* sp=g_Config->getServiceProvider();
172     Locker locker(sp);
173     const PropertySet* props=sp->getPropertySet("InProcess");
174     if (props) {
175         pair<bool,bool> flag=props->getBool("checkSpoofing");
176         g_checkSpoofing = !flag.first || flag.second;
177         flag=props->getBool("catchAll");
178         g_catchAll = flag.first && flag.second;
179
180         pair<bool,const char*> unsetValue=props->getString("unsetHeaderValue");
181         if (unsetValue.first)
182             g_unsetHeaderValue = unsetValue.second;
183         if (g_checkSpoofing) {
184             unsetValue=props->getString("spoofKey");
185             if (unsetValue.first)
186                 g_spoofKey = unsetValue.second;
187 #ifdef WIN32
188             else {
189                 _invalid_parameter_handler old = _set_invalid_parameter_handler(_my_invalid_parameter_handler);
190                 unsigned int randkey=0,randkey2=0,randkey3=0,randkey4=0;
191                 if (rand_s(&randkey) == 0 && rand_s(&randkey2) == 0 && rand_s(&randkey3) == 0 && rand_s(&randkey4) == 0) {
192                     _set_invalid_parameter_handler(old);
193                     ostringstream keystr;
194                     keystr << randkey << randkey2 << randkey3 << randkey4;
195                     g_spoofKey = keystr.str();
196                 }
197                 else {
198                     _set_invalid_parameter_handler(old);
199                     pblock_nvinsert("error", "module failed to generate a random anti-spoofing key (if this is Windows 2000 set one manually)", pb);
200                     locker.assign(); // pops lock on SP config
201                     g_Config->term();
202                     g_Config=nullptr;
203                     return REQ_ABORTED;
204                 }
205             }
206 #endif
207         }
208     }
209     return REQ_PROCEED;
210 }
211
212 /********************************************************************************/
213 // NSAPI Shib Target Subclass
214
215 class ShibTargetNSAPI : public AbstractSPRequest
216 {
217   mutable string m_body;
218   mutable bool m_gotBody,m_firsttime;
219   bool m_security_active;
220   int m_server_portnum;
221   mutable vector<string> m_certs;
222   set<string> m_allhttp;
223
224 public:
225   pblock* m_pb;
226   ::Session* m_sn;
227   Request* m_rq;
228
229   ShibTargetNSAPI(pblock* pb, ::Session* sn, Request* rq)
230       : AbstractSPRequest(SHIBSP_LOGCAT".NSAPI"),
231         m_gotBody(false), m_firsttime(true), m_security_active(false), m_server_portnum(0), m_pb(pb), m_sn(sn), m_rq(rq) {
232
233     // To determine whether SSL is active or not, we're supposed to rely
234     // on the security_active macro. For iPlanet 4.x, this works.
235     // For Sun 7.x, it's useless and appears to be on or off based
236     // on whether ANY SSL support is enabled for a vhost. Sun 6.x is unknown.
237     // As a fix, there's a conf variable called $security that can be mapped
238     // into a function parameter: security_active="$security"
239     // We check for this parameter, and rely on the macro if it isn't set.
240     // This doubles as a scheme virtualizer for load balanced scenarios
241     // since you can set the parameter to 1 or 0 as needed.
242     const char* sa = pblock_findval("security_active", m_pb);
243     if (sa)
244         m_security_active = (*sa == '1');
245     else if (security_active)
246         m_security_active = true;
247     else
248         m_security_active = false;
249
250     // A similar issue exists for the port. server_portnum is no longer
251     // working on at least Sun 7.x, and returns the first listener's port
252     // rather than whatever port is actually used for the request. Nice job, Sun.
253     sa = pblock_findval("server_portnum", m_pb);
254     m_server_portnum = (sa && *sa) ? atoi(sa) : server_portnum;
255
256     const char* uri = pblock_findval("uri", rq->reqpb);
257     const char* qstr = pblock_findval("query", rq->reqpb);
258
259     if (qstr) {
260         string temp = string(uri) + '?' + qstr;
261         setRequestURI(temp.c_str());
262     }
263     else {
264         setRequestURI(uri);
265     }
266
267     // See if this is the first time we've run.
268     if (!g_spoofKey.empty()) {
269         qstr = pblock_findval("Shib-Spoof-Check", rq->headers);
270         if (qstr && g_spoofKey == qstr)
271             m_firsttime = false;
272     }
273     if (!m_firsttime || rq->orig_rq)
274         log(SPDebug, "nsapi_shib function running more than once");
275   }
276   ~ShibTargetNSAPI() { }
277
278   const char* getScheme() const {
279     return m_security_active ? "https" : "http";
280   }
281   const char* getHostname() const {
282 #ifdef vs_is_default_vs
283     // This is 6.0 or later, so we can distinguish requests to name-based vhosts.
284     if (!vs_is_default_vs(request_get_vs(m_rq)))
285         // The beauty here is, a non-default vhost can *only* be accessed if the client
286         // specified the exact name in the Host header. So we can trust the Host header.
287         return pblock_findval("host", m_rq->headers);
288     else
289 #endif
290     // In other cases, we're going to rely on the initialization process...
291     return g_ServerName.c_str();
292   }
293   int getPort() const {
294     return m_server_portnum;
295   }
296   const char* getMethod() const {
297     return pblock_findval("method", m_rq->reqpb);
298   }
299   string getContentType() const {
300     char* content_type = nullptr;
301     if (request_header("content-type", &content_type, m_sn, m_rq) != REQ_PROCEED)
302         return "";
303     return content_type ? content_type : "";
304   }
305   long getContentLength() const {
306     if (m_gotBody)
307         return m_body.length();
308     char* content_length=nullptr;
309     if (request_header("content-length", &content_length, m_sn, m_rq) != REQ_PROCEED)
310         return 0;
311     return content_length ? atoi(content_length) : 0;
312   }
313   string getRemoteAddr() const {
314     string ret = AbstractSPRequest::getRemoteAddr();
315     return ret.empty() ? pblock_findval("ip", m_sn->client) : ret;
316   }
317   void log(SPLogLevel level, const string& msg) const {
318     AbstractSPRequest::log(level,msg);
319     if (level>=SPError)
320         log_error(LOG_FAILURE, "nsapi_shib", m_sn, m_rq, const_cast<char*>(msg.c_str()));
321   }
322   const char* getQueryString() const {
323     return pblock_findval("query", m_rq->reqpb);
324   }
325   const char* getRequestBody() const {
326     if (m_gotBody)
327         return m_body.c_str();
328     char* content_length=nullptr;
329     if (request_header("content-length", &content_length, m_sn, m_rq) != REQ_PROCEED || !content_length) {
330         m_gotBody = true;
331         return nullptr;
332     }
333     else if (atoi(content_length) > 1024*1024) // 1MB?
334       throw opensaml::SecurityPolicyException("Blocked request body exceeding 1M size limit.");
335     else {
336       char ch=IO_EOF+1;
337       int cl=atoi(content_length);
338       m_gotBody=true;
339       while (cl && ch != IO_EOF) {
340         ch=netbuf_getc(m_sn->inbuf);
341         // Check for error.
342         if(ch==IO_ERROR)
343           break;
344         m_body += ch;
345         cl--;
346       }
347       if (cl)
348         throw IOException("Error reading request body from browser.");
349       return m_body.c_str();
350     }
351   }
352   void clearHeader(const char* rawname, const char* cginame) {
353     if (g_checkSpoofing && m_firsttime && !m_rq->orig_rq) {
354         if (m_allhttp.empty()) {
355             // Populate the set of client-supplied headers for spoof checking.
356             const pb_entry* entry;
357             for (int i=0; i<m_rq->headers->hsize; ++i) {
358                 entry = m_rq->headers->ht[i];
359                 while (entry) {
360                     string cgiversion("HTTP_");
361                     const char* pch = entry->param->name;
362                     while (*pch) {
363                         cgiversion += (isalnum(*pch) ? toupper(*pch) : '_');
364                         pch++;
365                     }
366                     m_allhttp.insert(cgiversion);
367                     entry = entry->next;
368                 }
369             }
370         }
371         if (m_allhttp.count(cginame) > 0)
372             throw opensaml::SecurityPolicyException("Attempt to spoof header ($1) was detected.", params(1, rawname));
373     }
374     if (strcmp(rawname, "REMOTE_USER") == 0) {
375         param_free(pblock_remove("remote-user", m_rq->headers));
376         pblock_nvinsert("remote-user", g_unsetHeaderValue.c_str(), m_rq->headers);
377     }
378     else {
379         param_free(pblock_remove(rawname, m_rq->headers));
380         pblock_nvinsert(rawname, g_unsetHeaderValue.c_str(), m_rq->headers);
381     }
382   }
383   void setHeader(const char* name, const char* value) {
384     param_free(pblock_remove(name, m_rq->headers));
385     pblock_nvinsert(name, value, m_rq->headers);
386   }
387   string getHeader(const char* name) const {
388     // NSAPI headers tend to be lower case. We'll special case "cookie" since it's used a lot.
389     char* hdr = nullptr;
390     int cookie = strcmp(name, "Cookie");
391     if (cookie == 0)
392         name = "cookie";
393     if (request_header(const_cast<char*>(name), &hdr, m_sn, m_rq) != REQ_PROCEED) {
394       // We didn't get a hit, so we'll try a lower-casing operation, unless we already did...
395       if (cookie == 0)
396           return "";
397       string n;
398       while (*name)
399           n += tolower(*(name++));
400       if (request_header(const_cast<char*>(n.c_str()), &hdr, m_sn, m_rq) != REQ_PROCEED)
401           return "";
402     }
403     return string(hdr ? hdr : "");
404   }
405   void setRemoteUser(const char* user) {
406     pblock_nvinsert("auth-user", user, m_rq->vars);
407     param_free(pblock_remove("remote-user", m_rq->headers));
408     pblock_nvinsert("remote-user", user, m_rq->headers);
409   }
410   string getRemoteUser() const {
411     const char* ru = pblock_findval("auth-user", m_rq->vars);
412     return ru ? ru : "";
413   }
414   void setAuthType(const char* authtype) {
415     param_free(pblock_remove("auth-type", m_rq->vars));
416     if (authtype)
417         pblock_nvinsert("auth-type", authtype, m_rq->vars);
418   }
419   string getAuthType() const {
420     const char* at = pblock_findval("auth-type", m_rq->vars);
421     return at ? at : "";
422   }
423   void setContentType(const char* type) {
424       // iPlanet seems to have a case folding problem.
425       param_free(pblock_remove("content-type", m_rq->srvhdrs));
426       setResponseHeader("Content-Type", type);
427   }
428   void setResponseHeader(const char* name, const char* value) {
429     HTTPResponse::setResponseHeader(name, value);
430     pblock_nvinsert(name, value, m_rq->srvhdrs);
431   }
432
433   long sendResponse(istream& in, long status) {
434     string msg;
435     char buf[1024];
436     while (in) {
437         in.read(buf,1024);
438         msg.append(buf,in.gcount());
439     }
440     pblock_nvinsert("connection","close",m_rq->srvhdrs);
441     pblock_nninsert("content-length", msg.length(), m_rq->srvhdrs);
442     protocol_status(m_sn, m_rq, status, nullptr);
443     protocol_start_response(m_sn, m_rq);
444     net_write(m_sn->csd,const_cast<char*>(msg.c_str()),msg.length());
445     return REQ_EXIT;
446   }
447   long sendRedirect(const char* url) {
448     HTTPResponse::sendRedirect(url);
449     param_free(pblock_remove("content-type", m_rq->srvhdrs));
450     pblock_nninsert("content-length", 0, m_rq->srvhdrs);
451     pblock_nvinsert("expires", "01-Jan-1997 12:00:00 GMT", m_rq->srvhdrs);
452     pblock_nvinsert("cache-control", "private,no-store,no-cache", m_rq->srvhdrs);
453     pblock_nvinsert("location", url, m_rq->srvhdrs);
454     pblock_nvinsert("connection","close",m_rq->srvhdrs);
455     protocol_status(m_sn, m_rq, PROTOCOL_REDIRECT, nullptr);
456     protocol_start_response(m_sn, m_rq);
457     return REQ_ABORTED;
458   }
459   long returnDecline() { return REQ_NOACTION; }
460   long returnOK() { return REQ_PROCEED; }
461   const vector<string>& getClientCertificates() const {
462       if (m_certs.empty()) {
463           const char* cert = pblock_findval("auth-cert", m_rq->vars);
464           if (cert)
465               m_certs.push_back(cert);
466       }
467       return m_certs;
468   }
469 };
470
471 /********************************************************************************/
472
473 int WriteClientError(::Session* sn, Request* rq, char* func, char* msg)
474 {
475     log_error(LOG_FAILURE,func,sn,rq,msg);
476     protocol_status(sn,rq,PROTOCOL_SERVER_ERROR,msg);
477     return REQ_ABORTED;
478 }
479
480 #undef FUNC
481 #define FUNC "shibboleth"
482 extern "C" NSAPI_PUBLIC int nsapi_shib(pblock* pb, ::Session* sn, Request* rq)
483 {
484   ostringstream threadid;
485   threadid << "[" << getpid() << "] nsapi_shib" << '\0';
486   xmltooling::NDC ndc(threadid.str().c_str());
487
488   try {
489     ShibTargetNSAPI stn(pb, sn, rq);
490
491     // Check user authentication
492     pair<bool,long> res = stn.getServiceProvider().doAuthentication(stn);
493     // If directed, install a spoof key to recognize when we've already cleared headers.
494     if (!g_spoofKey.empty()) {
495       param_free(pblock_remove("Shib-Spoof-Check", rq->headers));
496       pblock_nvinsert("Shib-Spoof-Check", g_spoofKey.c_str(), rq->headers);
497     }
498     if (res.first) return (int)res.second;
499
500     // user authN was okay -- export the assertions now
501     param_free(pblock_remove("auth-user",rq->vars));
502
503     res = stn.getServiceProvider().doExport(stn);
504     if (res.first) return (int)res.second;
505
506     // Check the Authorization
507     res = stn.getServiceProvider().doAuthorization(stn);
508     if (res.first) return (int)res.second;
509
510     // this user is ok.
511     return REQ_PROCEED;
512   }
513   catch (exception& e) {
514     log_error(LOG_FAILURE,FUNC,sn,rq,const_cast<char*>(e.what()));
515     return WriteClientError(sn, rq, FUNC, "Shibboleth module threw an exception, see web server log for error.");
516   }
517   catch (...) {
518     log_error(LOG_FAILURE,FUNC,sn,rq,const_cast<char*>("Shibboleth module threw an unknown exception."));
519     if (g_catchAll)
520         return WriteClientError(sn, rq, FUNC, "Shibboleth module threw an unknown exception.");
521     throw;
522   }
523 }
524
525
526 #undef FUNC
527 #define FUNC "shib_handler"
528 extern "C" NSAPI_PUBLIC int shib_handler(pblock* pb, ::Session* sn, Request* rq)
529 {
530   ostringstream threadid;
531   threadid << "[" << getpid() << "] shib_handler" << '\0';
532   xmltooling::NDC ndc(threadid.str().c_str());
533
534   try {
535     ShibTargetNSAPI stn(pb, sn, rq);
536
537     pair<bool,long> res = stn.getServiceProvider().doHandler(stn);
538     if (res.first) return (int)res.second;
539
540     return WriteClientError(sn, rq, FUNC, "Shibboleth handler did not do anything.");
541   }
542   catch (exception& e) {
543     log_error(LOG_FAILURE,FUNC,sn,rq,const_cast<char*>(e.what()));
544     return WriteClientError(sn, rq, FUNC, "Shibboleth handler threw an exception, see web server log for error.");
545   }
546   catch (...) {
547     log_error(LOG_FAILURE,FUNC,sn,rq,"unknown exception caught in Shibboleth handler");
548     if (g_catchAll)
549         return WriteClientError(sn, rq, FUNC, "Shibboleth handler threw an unknown exception.");
550     throw;
551   }
552 }
553
554
555 class SunRequestMapper : public virtual RequestMapper, public virtual PropertySet
556 {
557 public:
558     SunRequestMapper(const xercesc::DOMElement* e);
559     ~SunRequestMapper() { delete m_mapper; delete m_stKey; delete m_propsKey; }
560     Lockable* lock() { return m_mapper->lock(); }
561     void unlock() { m_stKey->setData(nullptr); m_propsKey->setData(nullptr); m_mapper->unlock(); }
562     Settings getSettings(const HTTPRequest& request) const;
563
564     const PropertySet* getParent() const { return nullptr; }
565     void setParent(const PropertySet*) {}
566     pair<bool,bool> getBool(const char* name, const char* ns=nullptr) const;
567     pair<bool,const char*> getString(const char* name, const char* ns=nullptr) const;
568     pair<bool,const XMLCh*> getXMLString(const char* name, const char* ns=nullptr) const;
569     pair<bool,unsigned int> getUnsignedInt(const char* name, const char* ns=nullptr) const;
570     pair<bool,int> getInt(const char* name, const char* ns=nullptr) const;
571     void getAll(map<string,const char*>& properties) const;
572     const PropertySet* getPropertySet(const char* name, const char* ns=shibspconstants::ASCII_SHIB2SPCONFIG_NS) const;
573     const xercesc::DOMElement* getElement() const;
574
575 private:
576     RequestMapper* m_mapper;
577     ThreadKey* m_stKey;
578     ThreadKey* m_propsKey;
579 };
580
581 RequestMapper* SunRequestMapFactory(const xercesc::DOMElement* const & e)
582 {
583     return new SunRequestMapper(e);
584 }
585
586 SunRequestMapper::SunRequestMapper(const xercesc::DOMElement* e) : m_mapper(nullptr), m_stKey(nullptr), m_propsKey(nullptr)
587 {
588     m_mapper = SPConfig::getConfig().RequestMapperManager.newPlugin(XML_REQUEST_MAPPER,e);
589     m_stKey=ThreadKey::create(nullptr);
590     m_propsKey=ThreadKey::create(nullptr);
591 }
592
593 RequestMapper::Settings SunRequestMapper::getSettings(const HTTPRequest& request) const
594 {
595     Settings s=m_mapper->getSettings(request);
596     m_stKey->setData((void*)dynamic_cast<const ShibTargetNSAPI*>(&request));
597     m_propsKey->setData((void*)s.first);
598     return pair<const PropertySet*,AccessControl*>(this,s.second);
599 }
600
601 pair<bool,bool> SunRequestMapper::getBool(const char* name, const char* ns) const
602 {
603     const ShibTargetNSAPI* stn=reinterpret_cast<const ShibTargetNSAPI*>(m_stKey->getData());
604     const PropertySet* s=reinterpret_cast<const PropertySet*>(m_propsKey->getData());
605     if (stn && !ns && name) {
606         // Override boolean properties.
607         const char* param=pblock_findval(name,stn->m_pb);
608         if (param && (!strcmp(param,"1") || !strcasecmp(param,"true")))
609             return make_pair(true,true);
610     }
611     return s ? s->getBool(name,ns) : make_pair(false,false);
612 }
613
614 pair<bool,const char*> SunRequestMapper::getString(const char* name, const char* ns) const
615 {
616     const ShibTargetNSAPI* stn=reinterpret_cast<const ShibTargetNSAPI*>(m_stKey->getData());
617     const PropertySet* s=reinterpret_cast<const PropertySet*>(m_propsKey->getData());
618     if (stn && !ns && name) {
619         // Override string properties.
620         if (!strcmp(name,"authType"))
621             return pair<bool,const char*>(true,"shibboleth");
622         else {
623             const char* param=pblock_findval(name,stn->m_pb);
624             if (param)
625                 return make_pair(true,param);
626         }
627     }
628     return s ? s->getString(name,ns) : pair<bool,const char*>(false,nullptr);
629 }
630
631 pair<bool,const XMLCh*> SunRequestMapper::getXMLString(const char* name, const char* ns) const
632 {
633     const PropertySet* s=reinterpret_cast<const PropertySet*>(m_propsKey->getData());
634     return s ? s->getXMLString(name,ns) : pair<bool,const XMLCh*>(false,nullptr);
635 }
636
637 pair<bool,unsigned int> SunRequestMapper::getUnsignedInt(const char* name, const char* ns) const
638 {
639     const ShibTargetNSAPI* stn=reinterpret_cast<const ShibTargetNSAPI*>(m_stKey->getData());
640     const PropertySet* s=reinterpret_cast<const PropertySet*>(m_propsKey->getData());
641     if (stn && !ns && name) {
642         // Override int properties.
643         const char* param=pblock_findval(name,stn->m_pb);
644         if (param)
645             return pair<bool,unsigned int>(true,strtol(param,nullptr,10));
646     }
647     return s ? s->getUnsignedInt(name,ns) : pair<bool,unsigned int>(false,0);
648 }
649
650 pair<bool,int> SunRequestMapper::getInt(const char* name, const char* ns) const
651 {
652     const ShibTargetNSAPI* stn=reinterpret_cast<const ShibTargetNSAPI*>(m_stKey->getData());
653     const PropertySet* s=reinterpret_cast<const PropertySet*>(m_propsKey->getData());
654     if (stn && !ns && name) {
655         // Override int properties.
656         const char* param=pblock_findval(name,stn->m_pb);
657         if (param)
658             return pair<bool,int>(true,atoi(param));
659     }
660     return s ? s->getInt(name,ns) : pair<bool,int>(false,0);
661 }
662
663 void SunRequestMapper::getAll(map<string,const char*>& properties) const
664 {
665     const ShibTargetNSAPI* stn=reinterpret_cast<const ShibTargetNSAPI*>(m_stKey->getData());
666     const PropertySet* s=reinterpret_cast<const PropertySet*>(m_propsKey->getData());
667     if (s)
668         s->getAll(properties);
669     if (!stn)
670         return;
671     properties["authType"] = "shibboleth";
672     const pb_entry* entry;
673     for (int i=0; i<stn->m_pb->hsize; ++i) {
674         entry = stn->m_pb->ht[i];
675         while (entry) {
676             properties[entry->param->name] = entry->param->value;
677             entry = entry->next;
678         }
679     }
680 }
681
682 const PropertySet* SunRequestMapper::getPropertySet(const char* name, const char* ns) const
683 {
684     const PropertySet* s=reinterpret_cast<const PropertySet*>(m_propsKey->getData());
685     return s ? s->getPropertySet(name,ns) : nullptr;
686 }
687
688 const xercesc::DOMElement* SunRequestMapper::getElement() const
689 {
690     const PropertySet* s=reinterpret_cast<const PropertySet*>(m_propsKey->getData());
691     return s ? s->getElement() : nullptr;
692 }