4b0c6787abcdb7560211fd330c090b6d240c3cd8
[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 <stdexcept>
51 #include <boost/lexical_cast.hpp>
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 boost;
77 using namespace std;
78
79 // macros to output text to client
80 #define NET_WRITE(str) \
81     if (IO_ERROR==net_write(sn->csd,str,strlen(str))) return REQ_EXIT
82
83 namespace {
84     SPConfig* g_Config=nullptr;
85     string g_ServerName;
86     string g_unsetHeaderValue;
87     string g_spoofKey;
88     bool g_checkSpoofing = true;
89     bool g_catchAll = false;
90
91     static const XMLCh path[] =     UNICODE_LITERAL_4(p,a,t,h);
92     static const XMLCh validate[] = UNICODE_LITERAL_8(v,a,l,i,d,a,t,e);
93
94     void _my_invalid_parameter_handler(
95        const wchar_t * expression,
96        const wchar_t * function,
97        const wchar_t * file,
98        unsigned int line,
99        uintptr_t pReserved
100        ) {
101         return;
102     }
103 }
104
105 PluginManager<RequestMapper,string,const xercesc::DOMElement*>::Factory SunRequestMapFactory;
106
107 extern "C" NSAPI_PUBLIC void nsapi_shib_exit(void*)
108 {
109     if (g_Config)
110         g_Config->term();
111     g_Config = nullptr;
112 }
113
114 extern "C" NSAPI_PUBLIC int nsapi_shib_init(pblock* pb, ::Session* sn, Request* rq)
115 {
116     // Save off a default hostname for this virtual server.
117     char* name=pblock_findval("server-name",pb);
118     if (name)
119         g_ServerName=name;
120     else {
121         name=server_hostname;
122         if (name)
123             g_ServerName=name;
124         else {
125             name=util_hostname();
126             if (name) {
127                 g_ServerName=name;
128                 FREE(name);
129             }
130             else {
131                 pblock_nvinsert("error","unable to determine web server hostname",pb);
132                 return REQ_ABORTED;
133             }
134         }
135     }
136
137     log_error(LOG_INFORM,"nsapi_shib_init",sn,rq,"nsapi_shib loaded for host (%s)",g_ServerName.c_str());
138
139     const char* schemadir=pblock_findval("shib-schemas",pb);
140     const char* prefix=pblock_findval("shib-prefix",pb);
141
142     g_Config=&SPConfig::getConfig();
143     g_Config->setFeatures(
144         SPConfig::Listener |
145         SPConfig::Caching |
146         SPConfig::RequestMapping |
147         SPConfig::InProcess |
148         SPConfig::Logging |
149         SPConfig::Handlers
150         );
151     if (!g_Config->init(schemadir,prefix)) {
152         g_Config=nullptr;
153         pblock_nvinsert("error","unable to initialize Shibboleth libraries",pb);
154         return REQ_ABORTED;
155     }
156
157     g_Config->RequestMapperManager.registerFactory(NATIVE_REQUEST_MAPPER,&SunRequestMapFactory);
158
159     try {
160         if (!g_Config->instantiate(pblock_findval("shib-config",pb), true))
161             throw runtime_error("unknown error");
162     }
163     catch (std::exception& ex) {
164         pblock_nvinsert("error",ex.what(),pb);
165         g_Config->term();
166         g_Config=nullptr;
167         return REQ_ABORTED;
168     }
169
170     daemon_atrestart(nsapi_shib_exit,nullptr);
171
172     ServiceProvider* sp=g_Config->getServiceProvider();
173     Locker locker(sp);
174     const PropertySet* props=sp->getPropertySet("InProcess");
175     if (props) {
176         pair<bool,bool> flag=props->getBool("checkSpoofing");
177         g_checkSpoofing = !flag.first || flag.second;
178         flag=props->getBool("catchAll");
179         g_catchAll = flag.first && flag.second;
180
181         pair<bool,const char*> unsetValue=props->getString("unsetHeaderValue");
182         if (unsetValue.first)
183             g_unsetHeaderValue = unsetValue.second;
184         if (g_checkSpoofing) {
185             unsetValue=props->getString("spoofKey");
186             if (unsetValue.first)
187                 g_spoofKey = unsetValue.second;
188 #ifdef WIN32
189             else {
190                 _invalid_parameter_handler old = _set_invalid_parameter_handler(_my_invalid_parameter_handler);
191                 unsigned int randkey=0,randkey2=0,randkey3=0,randkey4=0;
192                 if (rand_s(&randkey) == 0 && rand_s(&randkey2) == 0 && rand_s(&randkey3) == 0 && rand_s(&randkey4) == 0) {
193                     _set_invalid_parameter_handler(old);
194                     g_spoofKey = lexical_cast<string>(randkey) + lexical_cast<string>(randkey2) +
195                         lexical_cast<string>(randkey3) + lexical_cast<string>(randkey4);
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     if (name) {
431         pblock_nvinsert(name, value, m_rq->srvhdrs);
432     }
433   }
434
435   long sendResponse(istream& in, long status) {
436     string msg;
437     char buf[1024];
438     while (in) {
439         in.read(buf,1024);
440         msg.append(buf,in.gcount());
441     }
442     pblock_nvinsert("connection","close",m_rq->srvhdrs);
443     pblock_nninsert("content-length", msg.length(), m_rq->srvhdrs);
444     protocol_status(m_sn, m_rq, status, nullptr);
445     protocol_start_response(m_sn, m_rq);
446     net_write(m_sn->csd,const_cast<char*>(msg.c_str()),msg.length());
447     return REQ_EXIT;
448   }
449   long sendRedirect(const char* url) {
450     HTTPResponse::sendRedirect(url);
451     param_free(pblock_remove("content-type", m_rq->srvhdrs));
452     pblock_nninsert("content-length", 0, m_rq->srvhdrs);
453     pblock_nvinsert("expires", "Wed, 01 Jan 1997 12:00:00 GMT", m_rq->srvhdrs);
454     pblock_nvinsert("cache-control", "private,no-store,no-cache,max-age=0", m_rq->srvhdrs);
455     pblock_nvinsert("location", url, m_rq->srvhdrs);
456     pblock_nvinsert("connection","close",m_rq->srvhdrs);
457     protocol_status(m_sn, m_rq, PROTOCOL_REDIRECT, nullptr);
458     protocol_start_response(m_sn, m_rq);
459     return REQ_ABORTED;
460   }
461   long returnDecline() { return REQ_NOACTION; }
462   long returnOK() { return REQ_PROCEED; }
463   const vector<string>& getClientCertificates() const {
464       if (m_certs.empty()) {
465           const char* cert = pblock_findval("auth-cert", m_rq->vars);
466           if (cert)
467               m_certs.push_back(cert);
468       }
469       return m_certs;
470   }
471 };
472
473 /********************************************************************************/
474
475 int WriteClientError(::Session* sn, Request* rq, char* func, char* msg)
476 {
477     log_error(LOG_FAILURE,func,sn,rq,msg);
478     protocol_status(sn,rq,PROTOCOL_SERVER_ERROR,msg);
479     return REQ_ABORTED;
480 }
481
482 #undef FUNC
483 #define FUNC "shibboleth"
484 extern "C" NSAPI_PUBLIC int nsapi_shib(pblock* pb, ::Session* sn, Request* rq)
485 {
486   string threadid("[");
487   threadid += lexical_cast<string>(getpid()) + "] nsapi_shib";
488   xmltooling::NDC ndc(threadid.c_str());
489
490   try {
491     ShibTargetNSAPI stn(pb, sn, rq);
492
493     // Check user authentication
494     pair<bool,long> res = stn.getServiceProvider().doAuthentication(stn);
495     // If directed, install a spoof key to recognize when we've already cleared headers.
496     if (!g_spoofKey.empty()) {
497       param_free(pblock_remove("Shib-Spoof-Check", rq->headers));
498       pblock_nvinsert("Shib-Spoof-Check", g_spoofKey.c_str(), rq->headers);
499     }
500     if (res.first) return (int)res.second;
501
502     // user authN was okay -- export the assertions now
503     param_free(pblock_remove("auth-user",rq->vars));
504
505     res = stn.getServiceProvider().doExport(stn);
506     if (res.first) return (int)res.second;
507
508     // Check the Authorization
509     res = stn.getServiceProvider().doAuthorization(stn);
510     if (res.first) return (int)res.second;
511
512     // this user is ok.
513     return REQ_PROCEED;
514   }
515   catch (std::exception& e) {
516     log_error(LOG_FAILURE,FUNC,sn,rq,const_cast<char*>(e.what()));
517     return WriteClientError(sn, rq, FUNC, "Shibboleth module threw an exception, see web server log for error.");
518   }
519   catch (...) {
520     log_error(LOG_FAILURE,FUNC,sn,rq,const_cast<char*>("Shibboleth module threw an unknown exception."));
521     if (g_catchAll)
522         return WriteClientError(sn, rq, FUNC, "Shibboleth module threw an unknown exception.");
523     throw;
524   }
525 }
526
527
528 #undef FUNC
529 #define FUNC "shib_handler"
530 extern "C" NSAPI_PUBLIC int shib_handler(pblock* pb, ::Session* sn, Request* rq)
531 {
532   string threadid("[");
533   threadid += lexical_cast<string>(getpid()) + "] shib_handler";
534   xmltooling::NDC ndc(threadid.c_str());
535
536   try {
537     ShibTargetNSAPI stn(pb, sn, rq);
538
539     pair<bool,long> res = stn.getServiceProvider().doHandler(stn);
540     if (res.first) return (int)res.second;
541
542     return WriteClientError(sn, rq, FUNC, "Shibboleth handler did not do anything.");
543   }
544   catch (std::exception& e) {
545     log_error(LOG_FAILURE,FUNC,sn,rq,const_cast<char*>(e.what()));
546     return WriteClientError(sn, rq, FUNC, "Shibboleth handler threw an exception, see web server log for error.");
547   }
548   catch (...) {
549     log_error(LOG_FAILURE,FUNC,sn,rq,"unknown exception caught in Shibboleth handler");
550     if (g_catchAll)
551         return WriteClientError(sn, rq, FUNC, "Shibboleth handler threw an unknown exception.");
552     throw;
553   }
554 }
555
556
557 class SunRequestMapper : public virtual RequestMapper, public virtual PropertySet
558 {
559 public:
560     SunRequestMapper(const xercesc::DOMElement* e);
561     ~SunRequestMapper() {}
562     Lockable* lock() { return m_mapper->lock(); }
563     void unlock() { m_stKey->setData(nullptr); m_propsKey->setData(nullptr); m_mapper->unlock(); }
564     Settings getSettings(const HTTPRequest& request) const;
565
566     const PropertySet* getParent() const { return nullptr; }
567     void setParent(const PropertySet*) {}
568     pair<bool,bool> getBool(const char* name, const char* ns=nullptr) const;
569     pair<bool,const char*> getString(const char* name, const char* ns=nullptr) const;
570     pair<bool,const XMLCh*> getXMLString(const char* name, const char* ns=nullptr) const;
571     pair<bool,unsigned int> getUnsignedInt(const char* name, const char* ns=nullptr) const;
572     pair<bool,int> getInt(const char* name, const char* ns=nullptr) const;
573     void getAll(map<string,const char*>& properties) const;
574     const PropertySet* getPropertySet(const char* name, const char* ns=shibspconstants::ASCII_SHIB2SPCONFIG_NS) const;
575     const xercesc::DOMElement* getElement() const;
576
577 private:
578     scoped_ptr<RequestMapper> m_mapper;
579     scoped_ptr<ThreadKey> m_stKey, m_propsKey;
580 };
581
582 RequestMapper* SunRequestMapFactory(const xercesc::DOMElement* const & e)
583 {
584     return new SunRequestMapper(e);
585 }
586
587 SunRequestMapper::SunRequestMapper(const xercesc::DOMElement* e)
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
594 RequestMapper::Settings SunRequestMapper::getSettings(const HTTPRequest& request) const
595 {
596     Settings s=m_mapper->getSettings(request);
597     m_stKey->setData((void*)dynamic_cast<const ShibTargetNSAPI*>(&request));
598     m_propsKey->setData((void*)s.first);
599     return pair<const PropertySet*,AccessControl*>(this,s.second);
600 }
601
602 pair<bool,bool> SunRequestMapper::getBool(const char* name, const char* ns) const
603 {
604     const ShibTargetNSAPI* stn=reinterpret_cast<const ShibTargetNSAPI*>(m_stKey->getData());
605     const PropertySet* s=reinterpret_cast<const PropertySet*>(m_propsKey->getData());
606     if (stn && !ns && name) {
607         // Override boolean properties.
608         const char* param=pblock_findval(name,stn->m_pb);
609         if (param && (!strcmp(param,"1") || !strcasecmp(param,"true")))
610             return make_pair(true,true);
611     }
612     return s ? s->getBool(name,ns) : make_pair(false,false);
613 }
614
615 pair<bool,const char*> SunRequestMapper::getString(const char* name, const char* ns) const
616 {
617     const ShibTargetNSAPI* stn=reinterpret_cast<const ShibTargetNSAPI*>(m_stKey->getData());
618     const PropertySet* s=reinterpret_cast<const PropertySet*>(m_propsKey->getData());
619     if (stn && !ns && name) {
620         // Override string properties.
621         if (!strcmp(name,"authType"))
622             return pair<bool,const char*>(true,"shibboleth");
623         else {
624             const char* param=pblock_findval(name,stn->m_pb);
625             if (param)
626                 return make_pair(true,param);
627         }
628     }
629     return s ? s->getString(name,ns) : pair<bool,const char*>(false,nullptr);
630 }
631
632 pair<bool,const XMLCh*> SunRequestMapper::getXMLString(const char* name, const char* ns) const
633 {
634     const PropertySet* s=reinterpret_cast<const PropertySet*>(m_propsKey->getData());
635     return s ? s->getXMLString(name,ns) : pair<bool,const XMLCh*>(false,nullptr);
636 }
637
638 pair<bool,unsigned int> SunRequestMapper::getUnsignedInt(const char* name, const char* ns) const
639 {
640     const ShibTargetNSAPI* stn=reinterpret_cast<const ShibTargetNSAPI*>(m_stKey->getData());
641     const PropertySet* s=reinterpret_cast<const PropertySet*>(m_propsKey->getData());
642     if (stn && !ns && name) {
643         // Override int properties.
644         const char* param=pblock_findval(name,stn->m_pb);
645         if (param) {
646             try {
647                 return pair<bool,unsigned int>(true,lexical_cast<unsigned int>(param));
648             }
649             catch (bad_lexical_cast&) {
650                 return pair<bool,unsigned int>(false,0);
651             }
652         }
653     }
654     return s ? s->getUnsignedInt(name,ns) : pair<bool,unsigned int>(false,0);
655 }
656
657 pair<bool,int> SunRequestMapper::getInt(const char* name, const char* ns) const
658 {
659     const ShibTargetNSAPI* stn=reinterpret_cast<const ShibTargetNSAPI*>(m_stKey->getData());
660     const PropertySet* s=reinterpret_cast<const PropertySet*>(m_propsKey->getData());
661     if (stn && !ns && name) {
662         // Override int properties.
663         const char* param=pblock_findval(name,stn->m_pb);
664         if (param)
665             return pair<bool,int>(true,atoi(param));
666     }
667     return s ? s->getInt(name,ns) : pair<bool,int>(false,0);
668 }
669
670 void SunRequestMapper::getAll(map<string,const char*>& properties) const
671 {
672     const ShibTargetNSAPI* stn=reinterpret_cast<const ShibTargetNSAPI*>(m_stKey->getData());
673     const PropertySet* s=reinterpret_cast<const PropertySet*>(m_propsKey->getData());
674     if (s)
675         s->getAll(properties);
676     if (!stn)
677         return;
678     properties["authType"] = "shibboleth";
679     const pb_entry* entry;
680     for (int i=0; i<stn->m_pb->hsize; ++i) {
681         entry = stn->m_pb->ht[i];
682         while (entry) {
683             properties[entry->param->name] = entry->param->value;
684             entry = entry->next;
685         }
686     }
687 }
688
689 const PropertySet* SunRequestMapper::getPropertySet(const char* name, const char* ns) const
690 {
691     const PropertySet* s=reinterpret_cast<const PropertySet*>(m_propsKey->getData());
692     return s ? s->getPropertySet(name,ns) : nullptr;
693 }
694
695 const xercesc::DOMElement* SunRequestMapper::getElement() const
696 {
697     const PropertySet* s=reinterpret_cast<const PropertySet*>(m_propsKey->getData());
698     return s ? s->getElement() : nullptr;
699 }