Redesigned target around URL->application mapping
[shibboleth/sp.git] / shib-target / shib-shire.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-shire.cpp -- Shibboleth SHIRE functions
52  *
53  * Created by:  Derek Atkins <derek@ihtfp.com>
54  *
55  * $Id$
56  */
57
58 #include "internal.h"
59
60 #ifdef HAVE_UNISTD_H
61 # include <unistd.h>
62 #endif
63
64 #include <stdexcept>
65
66 class shibtarget::SHIREPriv
67 {
68 public:
69   SHIREPriv(RPCHandle *rpc, SHIREConfig cfg, const char* shire_url);
70   ~SHIREPriv();
71
72   RPCHandle *   m_rpc;
73   SHIREConfig   m_config;
74   string            m_shire_url;
75
76   log4cpp::Category* log;
77 };
78
79 SHIREPriv::SHIREPriv(RPCHandle *rpc, SHIREConfig cfg, const char* shire_url)
80 {
81   string ctx = "shibtarget.SHIRE";
82   log = &(log4cpp::Category::getInstance(ctx));
83   m_rpc = rpc;
84   m_config = cfg;
85   m_shire_url = shire_url;
86 }
87
88 SHIREPriv::~SHIREPriv() {}
89
90
91 SHIRE::SHIRE(RPCHandle *rpc, SHIREConfig cfg, const char* shire_url)
92 {
93   m_priv = new SHIREPriv(rpc, cfg, shire_url);
94   m_priv->log->info ("New SHIRE handle created: %p", m_priv);
95 }
96
97 SHIRE::~SHIRE()
98 {
99   delete m_priv;
100 }
101
102
103 RPCError* SHIRE::sessionIsValid(const char* cookie, const char* ip, const char* application_id)
104 {
105   saml::NDC ndc("sessionIsValid");
106
107   if (!cookie || *cookie == '\0') {
108     m_priv->log->error ("No cookie value was provided");
109     return new RPCError(SHIBRPC_NO_SESSION, "No cookie value was provided");
110   }
111
112   if (!ip) {
113     m_priv->log->error ("No IP");
114     return new RPCError(-1, "Invalid IP Address");
115   }
116
117   // make sure we pass _something_ to the server
118   if (!application_id) application_id = "";
119
120   m_priv->log->info ("is session valid: %s", ip);
121   m_priv->log->debug ("session cookie: %s", cookie);
122
123   shibrpc_session_is_valid_args_1 arg;
124
125   arg.cookie.cookie = (char*)cookie;
126   arg.cookie.client_addr = (char *)ip;
127   arg.application_id = (char *)application_id;
128   arg.lifetime = m_priv->m_config.lifetime;
129   arg.timeout = m_priv->m_config.timeout;
130   arg.checkIPAddress = m_priv->m_config.checkIPAddress;
131
132   shibrpc_session_is_valid_ret_1 ret;
133   memset (&ret, 0, sizeof(ret));
134
135   // Loop on the RPC in case we lost contact the first time through
136   int retry = 1;
137   CLIENT *clnt;
138   do {
139     clnt = m_priv->m_rpc->connect();
140     if (shibrpc_session_is_valid_1 (&arg, &ret, clnt) != RPC_SUCCESS) {
141       // FAILED.  Release, disconnect, and try again...
142       m_priv->log->debug ("RPC Failure: %p (%p): %s", m_priv, clnt,
143                           clnt_spcreateerror (""));
144       m_priv->m_rpc->release();
145       m_priv->m_rpc->disconnect();
146       if (retry)
147         retry--;
148       else {
149         m_priv->log->error ("RPC Failure: %p (%p)", m_priv, clnt);
150         return new RPCError(-1, "RPC Failure");
151       }
152     } else {
153       // SUCCESS.  Release the lock.
154       m_priv->m_rpc->release();
155       retry = -1;
156     }
157   } while (retry >= 0);
158
159   m_priv->log->debug ("RPC completed with status %d, %p", ret.status.status, m_priv);
160
161   RPCError* retval;
162   if (ret.status.status)
163     retval = new RPCError(&ret.status);
164   else
165     retval = new RPCError();
166
167   clnt_freeres (clnt, (xdrproc_t)xdr_shibrpc_session_is_valid_ret_1, (caddr_t)&ret);
168
169   m_priv->log->debug ("returning");
170   return retval;
171 }
172
173 RPCError* SHIRE::sessionCreate(const char* post, const char* ip, const char* application_id, string& cookie)
174 {
175   saml::NDC ndc("sessionCreate");
176
177   if (!post || *post == '\0') {
178     m_priv->log->error ("No POST");
179     return new RPCError(-1,  "Invalid POST string");
180   }
181
182   if (!ip) {
183     m_priv->log->error ("No IP");
184     return new RPCError(-1, "Invalid IP Address");
185   }
186
187   // make sure we pass _something_ to the server
188   if (!application_id) application_id = "";
189
190   m_priv->log->info ("create session for user at %s", ip);
191
192   shibrpc_new_session_args_1 arg;
193   arg.shire_location = (char*) (m_priv->m_shire_url.c_str());
194   arg.application_id = (char*) application_id;
195   arg.saml_post = (char*)post;
196   arg.client_addr = (char*)ip;
197   arg.checkIPAddress = m_priv->m_config.checkIPAddress;
198
199   shibrpc_new_session_ret_1 ret;
200   memset (&ret, 0, sizeof(ret));
201
202   // Loop on the RPC in case we lost contact the first time through
203   int retry = 1;
204   CLIENT* clnt;
205   do {
206     clnt = m_priv->m_rpc->connect();
207     if (shibrpc_new_session_1 (&arg, &ret, clnt) != RPC_SUCCESS) {
208       // FAILED.  Release, disconnect, and retry
209       m_priv->log->debug ("RPC Failure: %p (%p): %s", m_priv, clnt,
210                           clnt_spcreateerror (""));
211       m_priv->m_rpc->release();
212       m_priv->m_rpc->disconnect();
213       if (retry)
214         retry--;
215       else {
216         m_priv->log->error ("RPC Failure: %p (%p)", m_priv, clnt);
217         return new RPCError(-1, "RPC Failure");
218       }
219     } else {
220       // SUCCESS.  Release and continue
221       m_priv->m_rpc->release();
222       retry = -1;
223     }
224   } while (retry >= 0);
225
226   m_priv->log->debug ("RPC completed with status %d (%p)", ret.status.status, m_priv);
227
228   RPCError* retval;
229   if (ret.status.status)
230     retval = new RPCError(&ret.status);
231   else {
232     m_priv->log->debug ("new cookie: %s", ret.cookie);
233     cookie = ret.cookie;
234     retval = new RPCError();
235   }
236
237   clnt_freeres (clnt, (xdrproc_t)xdr_shibrpc_new_session_ret_1, (caddr_t)&ret);
238
239   m_priv->log->debug ("returning");
240   return retval;
241 }