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