Updated example configuration files to match new config module
[shibboleth/sp.git] / shib-target / shibrpc-server.cpp
1 /*
2  * shibrpc-server.cpp -- SHIBRPC Server implementation.  Originally created
3  *                       as shibrpc-server-stubs.c; make sure that the function
4  *                       prototypes here match those in shibrpc.x.
5  *
6  * Created by:  Derek Atkins <derek@ihtfp.com>
7  *
8  * $Id$
9  */
10
11 #include "shibrpc.h"
12 #include "shib-target.h"
13
14 #include <log4cpp/Category.hh>
15 #include <strstream>
16
17 using namespace std;
18 using namespace saml;
19 using namespace shibboleth;
20 using namespace shibtarget;
21
22
23 static std::string get_threadid (const char* proc)
24 {
25   static u_long counter = 0;
26   ostringstream buf;
27   buf << "[" << counter++ << "] " << proc;
28   return buf.str();
29 }
30
31 static log4cpp::Category& get_category (void)
32 {
33   string ctx = "shibtarget.rpc-server";
34   return log4cpp::Category::getInstance(ctx);
35 }
36
37 extern "C" bool_t
38 shibrpc_ping_1_svc(int *argp, int *result, struct svc_req *rqstp)
39 {
40   *result = (*argp)+1;
41   return TRUE;
42 }
43
44 extern "C" bool_t
45 shibrpc_session_is_valid_1_svc(shibrpc_session_is_valid_args_1 *argp,
46                                shibrpc_session_is_valid_ret_1 *result,
47                                struct svc_req *rqstp)
48 {
49   log4cpp::Category& log = get_category();
50   string ctx = get_threadid("session_is_valid");
51   saml::NDC ndc(ctx);
52
53   if (!argp || !result) {
54     log.error ("RPC Argument Error");
55     return FALSE;
56   }
57
58   memset (result, 0, sizeof (*result));
59   
60   log.debug ("checking: %s@%s (checkAddr=%s)",
61              argp->cookie.cookie, argp->cookie.client_addr,
62              argp->checkIPAddress ? "true" : "false");
63
64   // See if the cookie exists...
65   CCacheEntry *entry = g_shibTargetCCache->find(argp->cookie.cookie);
66
67   // If not, leave now..
68   if (!entry) {
69     log.debug ("Not found");
70     result->status = SHIBRPC_NO_SESSION;
71     result->error_msg = strdup("No session exists for this cookie");
72     return TRUE;
73   }
74
75   // Verify the address is the same
76   if (argp->checkIPAddress) {
77     log.debug ("Checking address against %s", entry->getClientAddress());
78     if (strcmp (argp->cookie.client_addr, entry->getClientAddress())) {
79       log.debug ("IP Address mismatch");
80       result->status = SHIBRPC_IPADDR_MISMATCH;
81       result->error_msg = 
82         strdup ("Your IP address does not match the address in the original authentication.");
83       g_shibTargetCCache->remove (argp->cookie.cookie);
84       return TRUE;
85     }
86   }
87
88   // and that the session is still valid...
89   if (!entry->isSessionValid(argp->lifetime, argp->timeout)) {
90     log.debug ("Session expired");
91     result->status = SHIBRPC_SESSION_EXPIRED;
92     result->error_msg = strdup ("Your session has expired.  Re-authenticate.");
93     g_shibTargetCCache->remove (argp->cookie.cookie);
94     return TRUE;
95   }
96
97   // ok, we've succeeded..
98   result->status = SHIBRPC_OK;
99   result->error_msg = strdup("");
100   log.debug ("session ok");
101   return TRUE;
102 }
103
104 extern "C" bool_t
105 shibrpc_new_session_1_svc(shibrpc_new_session_args_1 *argp,
106                           shibrpc_new_session_ret_1 *result, struct svc_req *rqstp)
107 {
108   log4cpp::Category& log = get_category();
109   string ctx = get_threadid("new_session");
110   saml::NDC ndc(ctx);
111
112   if (!argp || !result) {
113     log.error ("Invalid RPC Arguments");
114     return FALSE;
115   }
116
117   // Initialize the result structure
118   memset (result, 0, sizeof(*result));
119   result->cookie = strdup ("");
120
121   log.debug ("creating session for %s", argp->client_addr);
122   log.debug ("shire location: %s", argp->shire_location);
123
124   XMLByte* post=reinterpret_cast<XMLByte*>(argp->saml_post);
125   auto_ptr<XMLCh> location(XMLString::transcode(argp->shire_location));
126
127   // Pull in the Policies
128   static const XMLCh* clubShib[] = {shibboleth::Constants::POLICY_CLUBSHIB};
129   ArrayIterator<const XMLCh*> policies(clubShib);
130
131   // And grab the Profile
132   // XXX: Create a "Global" POSTProfile instance per location...
133   log.debug ("create the POST profile (%d policies)", policies.size());
134   ShibPOSTProfile *profile =
135     ShibPOSTProfileFactory::getInstance(policies,
136                                         ShibConfig::getConfig().origin_mapper,
137                                         location.get(),
138                                         3600);
139
140   SAMLResponse* r = NULL;
141   SAMLAuthenticationStatement* auth_st = NULL;
142
143   try
144   {
145     try
146     {
147       // Make sure we've got a profile
148       if (!profile)
149         throw ShibTargetException(SHIBRPC_INTERNAL_ERROR,
150                                   "Failed to obtain the profile");
151
152       // Try and accept the response...
153       log.debug ("Trying to accept the post");
154       r = profile->accept(post);
155
156       // Make sure we got a response
157       if (!r)
158         throw ShibTargetException(SHIBRPC_RESPONSE_MISSING,
159                                   "Failed to accept the response.");
160
161       // Find the SSO Assertion
162       log.debug ("Get the SSOAssertion");
163       SAMLAssertion* ssoAssertion = profile->getSSOAssertion(*r);
164
165       // verify that we obtained an assertion
166       if (!ssoAssertion)
167         throw ShibTargetException(SHIBRPC_ASSERTION_MISSING,
168                                   "Cannot find SSO Assertion");
169
170       // Check against the replay cache
171       log.debug ("check replay cache");
172       if (profile->checkReplayCache(*ssoAssertion) == false)
173         throw ShibTargetException(SHIBRPC_ASSERTION_REPLAYED,
174                                   "Duplicate assertion found.");
175
176       // Get the authentication statement we need.
177       log.debug ("get SSOStatement");
178       auth_st = profile->getSSOStatement(*ssoAssertion);
179
180       // Verify we obtained an authentication statement
181       if (!auth_st)
182         throw ShibTargetException(SHIBRPC_AUTHSTATEMENT_MISSING,
183                                   "Processing was successful but no authentication statement was found.");
184   
185       // Maybe verify the origin address....
186       if (argp->checkIPAddress) {
187         log.debug ("check IP Address");
188
189         // Verify the client address exists
190         const XMLCh* ip = auth_st->getSubjectIP();
191         if (!ip)
192           throw ShibTargetException(SHIBRPC_IPADDR_MISSING,
193                                     "The IP Address provided by your origin site was missing.");
194         
195         log.debug ("verify client address");
196         // Verify the client address matches authentication
197         auto_ptr<char> this_ip(XMLString::transcode(ip));
198         if (strcmp (argp->client_addr, this_ip.get()))
199           throw ShibTargetException(SHIBRPC_IPADDR_MISMATCH,
200                                     "The IP address provided by your origin site did not match your current address.  To correct this problem you may need to bypass a local proxy server.");
201       }
202     }
203     catch (SAMLException &e)
204     {
205       log.error ("received SAML exception");
206       ostringstream os;
207       os << e;
208       throw ShibTargetException (SHIBRPC_SAML_EXCEPTION, os.str());
209     }
210     catch (XMLException &e)
211     {
212       log.error ("received XML exception");
213       auto_ptr<char> msg(XMLString::transcode(e.getMessage()));
214       throw ShibTargetException (SHIBRPC_XML_EXCEPTION, msg.get());
215     }
216   }
217   catch (ShibTargetException &e)
218   {
219     log.info ("FAILED: %s", e.what());
220     if (r) delete r;
221     result->status = e.which();
222     result->error_msg = strdup(e.what());
223     return TRUE;
224   }
225 #if 0
226   catch (...)
227   {
228     log.error ("Unknown error");
229     if (r) delete r;
230     result->status = SHIBRPC_UNKNOWN_ERROR;
231     result->error_msg = strdup("An unknown exception occurred");
232     return TRUE;
233   }
234 #endif
235
236   // It passes all our tests -- create a new session.
237   log.info ("Creating new session");
238
239   SAMLAuthenticationStatement* as=static_cast<SAMLAuthenticationStatement*>(auth_st->clone());
240   CCacheEntry* session = CCacheEntry::getInstance (as, argp->client_addr);
241
242   // Create a new cookie
243   SAMLIdentifier id;
244   auto_ptr<char> c(XMLString::transcode(id));
245   char *cookie = c.get();
246
247   // Cache this session with the cookie
248   g_shibTargetCCache->insert(cookie, session);
249
250   // Delete the response...
251   delete r;
252
253   // And let the user know.
254   free (result->cookie);
255   result->cookie = strdup(cookie);
256   result->status = SHIBRPC_OK;
257   result->error_msg = strdup("");
258
259   log.debug ("new session id: %s", cookie);
260   return TRUE;
261 }
262
263 extern "C" bool_t
264 shibrpc_get_assertions_1_svc(shibrpc_get_assertions_args_1 *argp,
265                         shibrpc_get_assertions_ret_1 *result, struct svc_req *rqstp)
266 {
267   log4cpp::Category& log = get_category();
268   string ctx = get_threadid("get_assertions");
269   saml::NDC ndc(ctx);
270
271   if (!argp || !result) {
272     log.error ("Invalid RPC arguments");
273     return FALSE;
274   }
275
276   memset (result, 0, sizeof (*result));
277
278   log.debug ("get attrs for client at %s", argp->cookie.client_addr);
279   log.debug ("cookie: %s", argp->cookie.cookie);
280   log.debug ("resource: %s", argp->url);
281
282   // Find this session
283   CCacheEntry* entry = g_shibTargetCCache->find(argp->cookie.cookie);
284
285   // If it does not exist, leave now..
286   if (!entry) {
287     log.error ("No Session");
288     result->status = SHIBRPC_NO_SESSION;
289     result->error_msg = strdup("getattrs Internal error: no session");
290     return TRUE;
291   }
292
293   // Validate the client address (again?)
294   if (argp->checkIPAddress &&
295       strcmp (argp->cookie.client_addr, entry->getClientAddress())) {
296     log.error ("IP Mismatch");
297     result->status = SHIBRPC_IPADDR_MISMATCH;
298     result->error_msg =
299       strdup("Your IP address does not match the address in the original authentication.");
300     return TRUE;
301   }
302
303   try {
304     // grab the attributes for this resource
305     Resource resource(argp->url);
306     Iterator<SAMLAssertion*> iter = entry->getAssertions(resource);
307     u_int size = iter.size();
308     result->assertions.assertions_len = size;
309
310     // if we have assertions...
311     if (size) {
312
313       // Build the response section
314       ShibRpcAssertion_1* av =
315         (ShibRpcAssertion_1*) malloc (size * sizeof (ShibRpcAssertion_1));
316       result->assertions.assertions_val = av;
317
318       // and then serialize them all...
319       u_int i = 0;
320       while (iter.hasNext()) {
321         SAMLAssertion* as = iter.next();
322         ostringstream os;
323         os << *as;
324         av[i++].assertion = strdup(os.str().c_str());
325       }
326     }
327   } catch (SAMLException& e) {
328     log.error ("received SAML exception");
329     ostringstream os;
330     os << e;
331     result->status = SHIBRPC_SAML_EXCEPTION;
332     result->error_msg = strdup(os.str().c_str());
333     return TRUE;
334   }
335
336   // and let it fly
337   result->status = SHIBRPC_OK;
338   result->error_msg = strdup("");
339
340   log.debug ("returning");
341   return TRUE;
342 }
343
344 extern "C" int
345 shibrpc_prog_1_freeresult (SVCXPRT *transp, xdrproc_t xdr_result, caddr_t result)
346 {
347         xdr_free (xdr_result, result);
348
349         /*
350          * Insert additional freeing code here, if needed
351          */
352
353         return 1;
354 }