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