Move the SHIRE and RM code into ShibTarget.
[shibboleth/cpp-sp.git] / shib-target / shib-target.cpp
1 /*
2  * The Shibboleth License, Version 1.
3  * Copyright (c) 2002
4  * University Corporation for Advanced Internet Development, Inc.
5  * All rights reserved
6  *
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
11  * Redistributions of source code must retain the above copyright notice, this
12  * list of conditions and the following disclaimer.
13  *
14  * Redistributions in binary form must reproduce the above copyright notice,
15  * this list of conditions and the following disclaimer in the documentation
16  * and/or other materials provided with the distribution, if any, must include
17  * the following acknowledgment: "This product includes software developed by
18  * the University Corporation for Advanced Internet Development
19  * <http://www.ucaid.edu>Internet2 Project. Alternately, this acknowledegement
20  * may appear in the software itself, if and wherever such third-party
21  * acknowledgments normally appear.
22  *
23  * Neither the name of Shibboleth nor the names of its contributors, nor
24  * Internet2, nor the University Corporation for Advanced Internet Development,
25  * Inc., nor UCAID may be used to endorse or promote products derived from this
26  * software without specific prior written permission. For written permission,
27  * please contact shibboleth@shibboleth.org
28  *
29  * Products derived from this software may not be called Shibboleth, Internet2,
30  * UCAID, or the University Corporation for Advanced Internet Development, nor
31  * may Shibboleth appear in their name, without prior written permission of the
32  * University Corporation for Advanced Internet Development.
33  *
34  *
35  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
36  * AND WITH ALL FAULTS. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
38  * PARTICULAR PURPOSE, AND NON-INFRINGEMENT ARE DISCLAIMED AND THE ENTIRE RISK
39  * OF SATISFACTORY QUALITY, PERFORMANCE, ACCURACY, AND EFFORT IS WITH LICENSEE.
40  * IN NO EVENT SHALL THE COPYRIGHT OWNER, CONTRIBUTORS OR THE UNIVERSITY
41  * CORPORATION FOR ADVANCED INTERNET DEVELOPMENT, INC. BE LIABLE FOR ANY DIRECT,
42  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
43  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
44  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
46  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
47  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48  */
49
50 /*
51  * shib-target.cpp -- The ShibTarget class, a superclass for general
52  *                    target code
53  *
54  * Created by:  Derek Atkins <derek@ihtfp.com>
55  *
56  * $Id$
57  */
58
59 #include "internal.h"
60
61 #ifdef HAVE_UNISTD_H
62 # include <unistd.h>
63 #endif
64
65 #include <sstream>
66 #include <stdexcept>
67
68 #include <shib/shib-threads.h>
69 #include <log4cpp/Category.hh>
70 #include <log4cpp/PropertyConfigurator.hh>
71 #include <xercesc/util/Base64.hpp>
72
73 using namespace std;
74 using namespace saml;
75 using namespace shibboleth;
76 using namespace shibtarget;
77 using namespace log4cpp;
78
79 namespace shibtarget {
80   class CgiParse
81   {
82   public:
83     CgiParse(const char* data, unsigned int len);
84     ~CgiParse();
85     const char* get_value(const char* name) const;
86     
87   private:
88     char * fmakeword(char stop, unsigned int *cl, const char** ppch);
89     char * makeword(char *line, char stop);
90     void plustospace(char *str);
91     char x2c(char *what);
92     void url_decode(char *url);
93
94     map<string,char*> kvp_map;
95   };
96
97   class ShibTargetPriv
98   {
99   public:
100     ShibTargetPriv();
101     ~ShibTargetPriv();
102
103     string url_encode(const char* s);
104
105     const IApplication *m_app;
106     string m_cookieName;
107     string m_shireURL;
108     string m_authnRequest;
109     CgiParse* m_parser;
110   };
111 }
112
113
114 /*************************************************************************
115  * Shib Target implementation
116  */
117
118 ShibTarget::ShibTarget(void) : m_priv(NULL)
119 {
120   m_total_bytes = 0;
121   m_priv = new ShibTargetPriv();
122 }
123
124 ShibTarget::ShibTarget(const IApplication *app) : m_priv(NULL)
125 {
126   m_total_bytes = 0;
127   m_priv = new ShibTargetPriv();
128   m_priv->m_app = app;
129 }
130
131 ShibTarget::~ShibTarget(void)
132 {
133   if (m_priv) delete m_priv;
134 }
135
136
137 // These functions implement the server-agnostic shibboleth engine
138 // The web server modules implement a subclass and then call into 
139 // these methods once they instantiate their request object.
140 void*
141 ShibTarget::doCheckAuthN(void)
142 {
143   return NULL;
144 }
145
146 void*
147 ShibTarget::doHandlePOST(void)
148 {
149   return NULL;
150 }
151
152 void*
153 ShibTarget::doCheckAuthZ(void)
154 {
155   return NULL;
156 }
157
158
159 // SHIRE APIs
160
161 // Get the session cookie name and properties for the application
162 std::pair<const char*,const char*>
163 ShibTarget::getCookieNameProps() const
164 {
165     static const char* defProps="; path=/";
166     static const char* defName="_shibsession_";
167     
168     // XXX: What to do if m_app isn't set?
169
170     const IPropertySet* props=m_priv->m_app->getPropertySet("Sessions");
171     if (props) {
172         pair<bool,const char*> p=props->getString("cookieProps");
173         if (!p.first)
174             p.second=defProps;
175         if (!m_priv->m_cookieName.empty())
176             return pair<const char*,const char*>(m_priv->m_cookieName.c_str(),
177                                                  p.second);
178         pair<bool,const char*> p2=props->getString("cookieName");
179         if (p2.first) {
180             m_priv->m_cookieName=p2.second;
181             return pair<const char*,const char*>(p2.second,p.second);
182         }
183         m_priv->m_cookieName=defName;
184         m_priv->m_cookieName+=m_priv->m_app->getId();
185         return pair<const char*,const char*>(m_priv->m_cookieName.c_str(),p.second);
186     }
187     m_priv->m_cookieName=defName;
188     m_priv->m_cookieName+=m_priv->m_app->getId();
189     return pair<const char*,const char*>(m_priv->m_cookieName.c_str(),defProps);
190 }
191         
192 // Find the default assertion consumer service for the resource
193 const char*
194 ShibTarget::getShireURL(const char* resource) const
195 {
196     if (!m_priv->m_shireURL.empty())
197         return m_priv->m_shireURL.c_str();
198
199     // XXX: what to do is m_app is NULL?
200
201     bool shire_ssl_only=false;
202     const char* shire=NULL;
203     const IPropertySet* props=m_priv->m_app->getPropertySet("Sessions");
204     if (props) {
205         pair<bool,bool> p=props->getBool("shireSSL");
206         if (p.first)
207             shire_ssl_only=p.second;
208         pair<bool,const char*> p2=props->getString("shireURL");
209         if (p2.first)
210             shire=p2.second;
211     }
212     
213     // Should never happen...
214     if (!shire || (*shire!='/' && strncmp(shire,"http:",5) && strncmp(shire,"https:",6)))
215         return NULL;
216
217     // The "shireURL" property can be in one of three formats:
218     //
219     // 1) a full URI:       http://host/foo/bar
220     // 2) a hostless URI:   http:///foo/bar
221     // 3) a relative path:  /foo/bar
222     //
223     // #  Protocol  Host        Path
224     // 1  shire     shire       shire
225     // 2  shire     resource    shire
226     // 3  resource  resource    shire
227     //
228     // note: if shire_ssl_only is true, make sure the protocol is https
229
230     const char* path = NULL;
231
232     // Decide whether to use the shire or the resource for the "protocol"
233     const char* prot;
234     if (*shire != '/') {
235         prot = shire;
236     }
237     else {
238         prot = resource;
239         path = shire;
240     }
241
242     // break apart the "protocol" string into protocol, host, and "the rest"
243     const char* colon=strchr(prot,':');
244     colon += 3;
245     const char* slash=strchr(colon,'/');
246     if (!path)
247         path = slash;
248
249     // Compute the actual protocol and store in member.
250     if (shire_ssl_only)
251         m_priv->m_shireURL.assign("https://");
252     else
253         m_priv->m_shireURL.assign(prot, colon-prot);
254
255     // create the "host" from either the colon/slash or from the target string
256     // If prot == shire then we're in either #1 or #2, else #3.
257     // If slash == colon then we're in #2.
258     if (prot != shire || slash == colon) {
259         colon = strchr(resource, ':');
260         colon += 3;      // Get past the ://
261         slash = strchr(colon, '/');
262     }
263     string host(colon, slash-colon);
264
265     // Build the shire URL
266     m_priv->m_shireURL+=host + path;
267     return m_priv->m_shireURL.c_str();
268 }
269         
270 // Generate a Shib 1.x AuthnRequest redirect URL for the resource
271 const char*
272 ShibTarget::getAuthnRequest(const char* resource) const
273 {
274     if (!m_priv->m_authnRequest.empty())
275         return m_priv->m_authnRequest.c_str();
276         
277     // XXX: what to do if m_app is NULL?
278
279     char timebuf[16];
280     sprintf(timebuf,"%u",time(NULL));
281     
282     const IPropertySet* props=m_priv->m_app->getPropertySet("Sessions");
283     if (props) {
284         pair<bool,const char*> wayf=props->getString("wayfURL");
285         if (wayf.first) {
286             m_priv->m_authnRequest=m_priv->m_authnRequest + wayf.second + "?shire=" + m_priv->url_encode(getShireURL(resource)) +
287                 "&target=" + m_priv->url_encode(resource) + "&time=" + timebuf;
288             pair<bool,bool> old=m_priv->m_app->getBool("oldAuthnRequest");
289             if (!old.first || !old.second) {
290                 wayf=m_priv->m_app->getString("providerId");
291                 if (wayf.first)
292                     m_priv->m_authnRequest=m_priv->m_authnRequest + "&providerId=" + m_priv->url_encode(wayf.second);
293             }
294         }
295     }
296     return m_priv->m_authnRequest.c_str();
297 }
298         
299 // Process a lazy session setup request and turn it into an AuthnRequest
300 const char*
301 ShibTarget::getLazyAuthnRequest(const char* query_string) const
302 {
303     CgiParse parser(query_string,strlen(query_string));
304     const char* target=parser.get_value("target");
305     if (!target || !*target)
306         return NULL;
307     return getAuthnRequest(target);
308 }
309         
310 // Process a POST profile submission, and return (SAMLResponse,TARGET) pair.
311 std::pair<const char*,const char*>
312 ShibTarget::getFormSubmission(const char* post, unsigned int len) const
313 {
314     m_priv->m_parser = new CgiParse(post,len);
315     return pair<const char*,const char*>(m_priv->m_parser->get_value("SAMLResponse"),m_priv->m_parser->get_value("TARGET"));
316 }
317         
318 RPCError* 
319 ShibTarget::sessionCreate(const char* response, const char* ip, std::string &cookie)
320   const
321 {
322   saml::NDC ndc("sessionCreate");
323   Category& log = Category::getInstance("shibtarget.SHIRE");
324
325   if (!response || !*response) {
326     log.error ("Empty SAML response content");
327     return new RPCError(-1,  "Empty SAML response content");
328   }
329
330   if (!ip || !*ip) {
331     log.error ("Invalid IP address");
332     return new RPCError(-1, "Invalid IP address");
333   }
334   
335   shibrpc_new_session_args_1 arg;
336   arg.shire_location = (char*) m_priv->m_shireURL.c_str();
337   arg.application_id = (char*) m_priv->m_app->getId();
338   arg.saml_post = (char*)response;
339   arg.client_addr = (char*)ip;
340   arg.checkIPAddress = true;
341
342   log.info ("create session for user at %s for application %s", ip, arg.application_id);
343
344   const IPropertySet* props=m_priv->m_app->getPropertySet("Sessions");
345   if (props) {
346       pair<bool,bool> pcheck=props->getBool("checkAddress");
347       if (pcheck.first)
348           arg.checkIPAddress = pcheck.second;
349   }
350
351   shibrpc_new_session_ret_1 ret;
352   memset (&ret, 0, sizeof(ret));
353
354   // Loop on the RPC in case we lost contact the first time through
355   int retry = 1;
356   CLIENT* clnt;
357   RPC rpc;
358   do {
359     clnt = rpc->connect();
360     clnt_stat status = shibrpc_new_session_1 (&arg, &ret, clnt);
361     if (status != RPC_SUCCESS) {
362       // FAILED.  Release, disconnect, and retry
363       log.error("RPC Failure: %p (%p) (%d): %s", this, clnt, status, clnt_spcreateerror("shibrpc_new_session_1"));
364       rpc->disconnect();
365       if (retry)
366         retry--;
367       else
368         return new RPCError(-1, "RPC Failure");
369     }
370     else {
371       // SUCCESS.  Pool and continue
372       retry = -1;
373     }
374   } while (retry>=0);
375
376   log.debug("RPC completed with status %d (%p)", ret.status.status, this);
377
378   RPCError* retval;
379   if (ret.status.status)
380     retval = new RPCError(&ret.status);
381   else {
382     log.debug ("new cookie: %s", ret.cookie);
383     cookie = ret.cookie;
384     retval = new RPCError();
385   }
386
387   clnt_freeres(clnt, (xdrproc_t)xdr_shibrpc_new_session_ret_1, (caddr_t)&ret);
388   rpc.pool();
389
390   log.debug("returning");
391   return retval;
392 }
393
394 RPCError*
395 ShibTarget::sessionIsValid(const char* session_id, const char* ip) const
396 {
397   saml::NDC ndc("sessionIsValid");
398   Category& log = Category::getInstance("shibtarget.SHIRE");
399
400   if (!session_id || !*session_id) {
401     log.error ("No cookie value was provided");
402     return new RPCError(SHIBRPC_NO_SESSION, "No cookie value was provided");
403   }
404   else if (strchr(session_id,'=')) {
405     log.error ("The cookie value wasn't extracted successfully, use a more unique cookie name for your installation.");
406     return new RPCError(SHIBRPC_INTERNAL_ERROR, "The cookie value wasn't extracted successfully, use a more unique cookie name for your installation.");
407   }
408
409   if (!ip || !*ip) {
410     log.error ("Invalid IP Address");
411     return new RPCError(SHIBRPC_IPADDR_MISSING, "Invalid IP Address");
412   }
413
414   log.info ("is session valid: %s", ip);
415   log.debug ("session cookie: %s", session_id);
416
417   shibrpc_session_is_valid_args_1 arg;
418
419   arg.cookie.cookie = (char*)session_id;
420   arg.cookie.client_addr = (char *)ip;
421   arg.application_id = (char *)m_priv->m_app->getId();
422   
423   // Get rest of input from the application Session properties.
424   arg.lifetime = 3600;
425   arg.timeout = 1800;
426   arg.checkIPAddress = true;
427   const IPropertySet* props=m_priv->m_app->getPropertySet("Sessions");
428   if (props) {
429       pair<bool,unsigned int> p=props->getUnsignedInt("lifetime");
430       if (p.first)
431           arg.lifetime = p.second;
432       p=props->getUnsignedInt("timeout");
433       if (p.first)
434           arg.timeout = p.second;
435       pair<bool,bool> pcheck=props->getBool("checkAddress");
436       if (pcheck.first)
437           arg.checkIPAddress = pcheck.second;
438   }
439   
440   shibrpc_session_is_valid_ret_1 ret;
441   memset (&ret, 0, sizeof(ret));
442
443   // Loop on the RPC in case we lost contact the first time through
444   int retry = 1;
445   CLIENT *clnt;
446   RPC rpc;
447   do {
448     clnt = rpc->connect();
449     clnt_stat status = shibrpc_session_is_valid_1(&arg, &ret, clnt);
450     if (status != RPC_SUCCESS) {
451       // FAILED.  Release, disconnect, and try again...
452       log.error("RPC Failure: %p (%p) (%d) %s", this, clnt, status, clnt_spcreateerror("shibrpc_session_is_valid_1"));
453       rpc->disconnect();
454       if (retry)
455           retry--;
456       else
457           return new RPCError(-1, "RPC Failure");
458     }
459     else {
460       // SUCCESS
461       retry = -1;
462     }
463   } while (retry>=0);
464
465   log.debug("RPC completed with status %d, %p", ret.status.status, this);
466
467   RPCError* retval;
468   if (ret.status.status)
469     retval = new RPCError(&ret.status);
470   else
471     retval = new RPCError();
472
473   clnt_freeres (clnt, (xdrproc_t)xdr_shibrpc_session_is_valid_ret_1, (caddr_t)&ret);
474   rpc.pool();
475
476   log.debug("returning");
477   return retval;
478 }
479
480 // RM APIS
481
482 RPCError*
483 ShibTarget::getAssertions(const char* cookie, const char* ip,
484                           std::vector<saml::SAMLAssertion*>& assertions,
485                           saml::SAMLAuthenticationStatement **statement
486                           ) const
487 {
488   saml::NDC ndc("getAssertions");
489   Category& log=Category::getInstance("shibtarget.RM");
490   log.info("get assertions...");
491
492   if (!cookie || !*cookie) {
493     log.error ("No cookie value provided.");
494     return new RPCError(-1, "No cookie value provided.");
495   }
496
497   if (!ip || !*ip) {
498     log.error ("Invalid ip address");
499     return new RPCError(-1, "Invalid IP address");
500   }
501
502   log.debug("session cookie: %s", cookie);
503
504   shibrpc_get_assertions_args_1 arg;
505   arg.cookie.cookie = (char*)cookie;
506   arg.cookie.client_addr = (char*)ip;
507   arg.checkIPAddress = true;
508   arg.application_id = (char *)m_priv->m_app->getId();
509
510   log.info("request from %s for \"%s\"", ip, arg.application_id);
511
512   const IPropertySet* props=m_priv->m_app->getPropertySet("Sessions");
513   if (props) {
514       pair<bool,bool> pcheck=props->getBool("checkAddress");
515       if (pcheck.first)
516           arg.checkIPAddress = pcheck.second;
517   }
518
519   shibrpc_get_assertions_ret_1 ret;
520   memset (&ret, 0, sizeof(ret));
521
522   // Loop on the RPC in case we lost contact the first time through
523   int retry = 1;
524   CLIENT *clnt;
525   RPC rpc;
526   do {
527     clnt = rpc->connect();
528     clnt_stat status = shibrpc_get_assertions_1(&arg, &ret, clnt);
529     if (status != RPC_SUCCESS) {
530       // FAILED.  Release, disconnect, and try again.
531       log.debug("RPC Failure: %p (%p) (%d): %s", this, clnt, status, clnt_spcreateerror("shibrpc_get_assertions_1"));
532       rpc->disconnect();
533       if (retry)
534         retry--;
535       else
536         return new RPCError(-1, "RPC Failure");
537     }
538     else {
539       // SUCCESS.  Release back into pool
540       retry = -1;
541     }
542   } while (retry>=0);
543
544   log.debug("RPC completed with status %d (%p)", ret.status.status, this);
545
546   RPCError* retval = NULL;
547   if (ret.status.status)
548     retval = new RPCError(&ret.status);
549   else {
550     try {
551       try {
552         for (u_int i = 0; i < ret.assertions.assertions_len; i++) {
553           istringstream attrstream(ret.assertions.assertions_val[i].xml_string);
554           SAMLAssertion *as = NULL;
555           log.debugStream() << "Trying to decode assertion " << i << ": " <<
556                 ret.assertions.assertions_val[i].xml_string << CategoryStream::ENDLINE;
557           assertions.push_back(new SAMLAssertion(attrstream));
558         }
559
560         // return the Authentication Statement
561         if (statement) {
562           istringstream authstream(ret.auth_statement.xml_string);
563           SAMLAuthenticationStatement *auth = NULL;
564           
565           log.debugStream() << "Trying to decode authentication statement: " <<
566                 ret.auth_statement.xml_string << CategoryStream::ENDLINE;
567             auth = new SAMLAuthenticationStatement(authstream);
568         
569             // Save off the statement
570             *statement = auth;
571         }
572       }
573       catch (SAMLException& e) {
574         log.error ("SAML Exception: %s", e.what());
575         ostringstream os;
576         os << e;
577         throw ShibTargetException(SHIBRPC_SAML_EXCEPTION, os.str().c_str());
578       }
579       catch (XMLException& e) {
580         log.error ("XML Exception: %s", e.getMessage());
581         auto_ptr_char msg(e.getMessage());
582         throw ShibTargetException (SHIBRPC_XML_EXCEPTION, msg.get());
583       }
584     }
585     catch (ShibTargetException &e) {
586       retval = new RPCError(e);
587     }
588
589     if (!retval)
590       retval = new RPCError();
591   }
592
593   clnt_freeres(clnt, (xdrproc_t)xdr_shibrpc_get_assertions_ret_1, (caddr_t)&ret);
594   rpc.pool();
595
596   log.debug ("returning..");
597   return retval;
598 }
599
600 void
601 ShibTarget::serialize(saml::SAMLAssertion &assertion, std::string &result)
602 {
603   saml::NDC ndc("serialize");
604   Category& log=Category::getInstance("shibtarget.RM");
605
606   ostringstream os;
607   os << assertion;
608   unsigned int outlen;
609   char* assn = (char*) os.str().c_str();
610   XMLByte* serialized = Base64::encode(reinterpret_cast<XMLByte*>(assn), os.str().length(), &outlen);
611   result = (char*) serialized;
612   XMLString::release(&serialized);
613 }
614
615
616 /*************************************************************************
617  * Shib Target Private implementation
618  */
619
620 ShibTargetPriv::ShibTargetPriv() : m_parser(NULL), m_app(NULL)
621 {
622 }
623
624 ShibTargetPriv::~ShibTargetPriv()
625 {
626   if (m_parser) delete m_parser;
627   //if (m_app) delete m_app;
628 }
629
630 static inline char hexchar(unsigned short s)
631 {
632     return (s<=9) ? ('0' + s) : ('A' + s - 10);
633 }
634
635 string
636 ShibTargetPriv::url_encode(const char* s)
637 {
638     static char badchars[]="\"\\+<>#%{}|^~[]`;/?:@=&";
639
640     string ret;
641     for (; *s; s++) {
642         if (strchr(badchars,*s) || *s<=0x1F || *s>=0x7F) {
643             ret+='%';
644         ret+=hexchar(*s >> 4);
645         ret+=hexchar(*s & 0x0F);
646         }
647         else
648             ret+=*s;
649     }
650     return ret;
651 }
652
653 /*************************************************************************
654  * CGI Parser implementation
655  */
656
657 CgiParse::CgiParse(const char* data, unsigned int len)
658 {
659     const char* pch = data;
660     unsigned int cl = len;
661         
662     while (cl && pch) {
663         char *name;
664         char *value;
665         value=fmakeword('&',&cl,&pch);
666         plustospace(value);
667         url_decode(value);
668         name=makeword(value,'=');
669         kvp_map[name]=value;
670         free(name);
671     }
672 }
673
674 CgiParse::~CgiParse()
675 {
676     for (map<string,char*>::iterator i=kvp_map.begin(); i!=kvp_map.end(); i++)
677         free(i->second);
678 }
679
680 const char*
681 CgiParse::get_value(const char* name) const
682 {
683     map<string,char*>::const_iterator i=kvp_map.find(name);
684     if (i==kvp_map.end())
685         return NULL;
686     return i->second;
687 }
688
689 /* Parsing routines modified from NCSA source. */
690 char *
691 CgiParse::makeword(char *line, char stop)
692 {
693     int x = 0,y;
694     char *word = (char *) malloc(sizeof(char) * (strlen(line) + 1));
695
696     for(x=0;((line[x]) && (line[x] != stop));x++)
697         word[x] = line[x];
698
699     word[x] = '\0';
700     if(line[x])
701         ++x;
702     y=0;
703
704     while(line[x])
705       line[y++] = line[x++];
706     line[y] = '\0';
707     return word;
708 }
709
710 char *
711 CgiParse::fmakeword(char stop, unsigned int *cl, const char** ppch)
712 {
713     int wsize;
714     char *word;
715     int ll;
716
717     wsize = 1024;
718     ll=0;
719     word = (char *) malloc(sizeof(char) * (wsize + 1));
720
721     while(1)
722     {
723         word[ll] = *((*ppch)++);
724         if(ll==wsize-1)
725         {
726             word[ll+1] = '\0';
727             wsize+=1024;
728             word = (char *)realloc(word,sizeof(char)*(wsize+1));
729         }
730         --(*cl);
731         if((word[ll] == stop) || word[ll] == EOF || (!(*cl)))
732         {
733             if(word[ll] != stop)
734                 ll++;
735             word[ll] = '\0';
736             return word;
737         }
738         ++ll;
739     }
740 }
741
742 void
743 CgiParse::plustospace(char *str)
744 {
745     register int x;
746
747     for(x=0;str[x];x++)
748         if(str[x] == '+') str[x] = ' ';
749 }
750
751 char
752 CgiParse::x2c(char *what)
753 {
754     register char digit;
755
756     digit = (what[0] >= 'A' ? ((what[0] & 0xdf) - 'A')+10 : (what[0] - '0'));
757     digit *= 16;
758     digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A')+10 : (what[1] - '0'));
759     return(digit);
760 }
761
762 void
763 CgiParse::url_decode(char *url)
764 {
765     register int x,y;
766
767     for(x=0,y=0;url[y];++x,++y)
768     {
769         if((url[x] = url[y]) == '%')
770         {
771             url[x] = x2c(&url[y+1]);
772             y+=2;
773         }
774     }
775     url[x] = '\0';
776 }
777
778 /*
779  * We need to implement this so the SHIRE (and RM) recodes work
780  * in terms of the ShibTarget
781  */
782 void ShibTarget::log(ShibLogLevel level, string &msg)
783 {
784   throw runtime_error("Invalid Usage");
785 }
786 string ShibTarget::getCookie(std::string &name)
787 {
788   throw runtime_error("Invalid Usage");
789 }
790 void ShibTarget::setCookie(string &name, string &value)
791 {
792   throw runtime_error("Invalid Usage");
793 }
794 string ShibTarget::getPostData(void)
795 {
796   throw runtime_error("Invalid Usage");
797 }
798 string ShibTarget::getAuthType(void)
799 {
800   throw runtime_error("Invalid Usage");
801 }
802 void ShibTarget::setAuthType(std::string)
803 {
804   throw runtime_error("Invalid Usage");
805 }
806 //virtual HTAccessInfo& getAccessInfo(void);
807 void* ShibTarget::sendPage(string &msg, pair<string,string> headers[], int code)
808 {
809   throw runtime_error("Invalid Usage");
810 }
811 void* ShibTarget::sendRedirect(std::string url)
812 {
813   throw runtime_error("Invalid Usage");
814 }