2 * The Shibboleth License, Version 1.
4 * University Corporation for Advanced Internet Development, Inc.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
11 * Redistributions of source code must retain the above copyright notice, this
12 * list of conditions and the following disclaimer.
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.
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
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.
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.
51 * shibrpc-server.cpp -- SHIBRPC Server implementation. Originally created
52 * as shibrpc-server-stubs.c; make sure that the function
53 * prototypes here match those in shibrpc.x.
55 * Created by: Derek Atkins <derek@ihtfp.com>
66 #ifdef HAVE_LIBDMALLOCXX
70 static string get_threadid (const char* proc)
72 static u_long counter = 0;
74 buf << "[" << counter++ << "] " << proc;
78 static Category& get_category (void)
80 string ctx = "shibtarget.rpc-server";
81 return Category::getInstance(ctx);
85 shibrpc_ping_1_svc(int *argp, int *result, struct svc_req *rqstp)
91 void set_rpc_status(ShibRpcError *error, ShibRpcStatus status,
92 const char* msg, const char* origin)
94 error->status = status;
96 error->ShibRpcError_u.e.error = strdup(msg);
97 error->ShibRpcError_u.e.origin = strdup(origin);
101 void set_rpc_status_x(ShibRpcError *error, ShibRpcStatus status,
102 const char* msg, const XMLCh* origin)
105 set_rpc_status(error, status, NULL, NULL);
108 auto_ptr<char> orig(XMLString::transcode(origin));
109 set_rpc_status(error, status, msg, orig.get());
113 shibrpc_session_is_valid_1_svc(shibrpc_session_is_valid_args_1 *argp,
114 shibrpc_session_is_valid_ret_1 *result,
115 struct svc_req *rqstp)
117 Category& log = get_category();
118 string ctx = get_threadid("session_is_valid");
121 if (!argp || !result) {
122 log.error ("RPC Argument Error");
126 memset (result, 0, sizeof (*result));
128 log.debug ("checking: %s@%s (checkAddr=%s)",
129 argp->cookie.cookie, argp->cookie.client_addr,
130 argp->checkIPAddress ? "true" : "false");
132 // See if the cookie exists...
133 CCacheEntry *entry = g_shibTargetCCache->find(argp->cookie.cookie);
135 // If not, leave now..
137 log.debug ("Not found");
138 set_rpc_status(&result->status, SHIBRPC_NO_SESSION,
139 "No session exists for this cookie", "");
143 // TEST the session...
147 const XMLCh* origin = entry->getStatement()->getSubject()->getNameQualifier();
149 // Verify the address is the same
150 if (argp->checkIPAddress) {
151 log.debug ("Checking address against %s", entry->getClientAddress());
152 if (strcmp (argp->cookie.client_addr, entry->getClientAddress())) {
153 log.debug ("IP Address mismatch");
155 throw ShibTargetException(SHIBRPC_IPADDR_MISMATCH,
156 "Your IP address does not match the address in the original authentication.",
161 // and that the session is still valid...
162 if (!entry->isSessionValid(argp->lifetime, argp->timeout)) {
163 log.debug ("Session expired");
164 throw ShibTargetException(SHIBRPC_SESSION_EXPIRED,
165 "Your session has expired. Re-authenticate.",
169 // and now try to prefetch the attributes .. this could cause an
170 // "error", which is why we call it here.
172 log.debug ("resource: %s", argp->url);
173 Resource r(argp->url);
174 entry->preFetch(r,15); // give a 15-second window for the RM
176 } catch (SAMLException &e) {
177 log.debug ("prefetch failed with a SAML Exception: %s", e.what());
180 throw ShibTargetException(SHIBRPC_SAML_EXCEPTION, os.str(), origin);
183 log.error ("prefetch caught an unknown exception");
184 throw ShibTargetException(SHIBRPC_UNKNOWN_ERROR,
185 "An unknown error occured while pre-fetching attributes.",
189 } catch (ShibTargetException &e) {
191 g_shibTargetCCache->remove (argp->cookie.cookie);
192 set_rpc_status_x(&result->status, e.which(), e.what(), e.where());
196 // Ok, just release it.
199 // ok, we've succeeded..
200 set_rpc_status(&result->status, SHIBRPC_OK, NULL, NULL);
201 log.debug ("session ok");
206 shibrpc_new_session_1_svc(shibrpc_new_session_args_1 *argp,
207 shibrpc_new_session_ret_1 *result, struct svc_req *rqstp)
209 Category& log = get_category();
210 string ctx = get_threadid("new_session");
213 if (!argp || !result) {
214 log.error ("Invalid RPC Arguments");
218 // Initialize the result structure
219 memset (result, 0, sizeof(*result));
220 result->cookie = strdup ("");
222 log.debug ("creating session for %s", argp->client_addr);
223 log.debug ("shire location: %s", argp->shire_location);
225 XMLByte* post=reinterpret_cast<XMLByte*>(argp->saml_post);
226 auto_ptr<XMLCh> location(XMLString::transcode(argp->shire_location));
228 // Pull in the Policies
229 Iterator<const XMLCh*> policies=ShibTargetConfig::getConfig().getPolicies();
231 // And grab the Profile
232 // XXX: Create a "Global" POSTProfile instance per location...
233 log.debug ("create the POST profile (%d policies)", policies.size());
234 ShibPOSTProfile *profile =
235 ShibPOSTProfileFactory::getInstance(policies,
239 SAMLResponse* r = NULL;
240 const SAMLAuthenticationStatement* auth_st = NULL;
241 XMLCh* origin = NULL;
247 // Make sure we've got a profile
249 throw ShibTargetException(SHIBRPC_INTERNAL_ERROR,
250 "Failed to obtain the profile");
252 // Try and accept the response...
253 log.debug ("Trying to accept the post");
254 r = profile->accept(post, &origin);
256 // Make sure we got a response
258 throw ShibTargetException(SHIBRPC_RESPONSE_MISSING,
259 "Failed to accept the response.",
262 // Find the SSO Assertion
263 log.debug ("Get the SSOAssertion");
264 const SAMLAssertion* ssoAssertion = profile->getSSOAssertion(*r);
266 // Check against the replay cache
267 log.debug ("check replay cache");
268 if (profile->checkReplayCache(*ssoAssertion) == false)
269 throw ShibTargetException(SHIBRPC_ASSERTION_REPLAYED,
270 "Duplicate assertion found.",
273 // Get the authentication statement we need.
274 log.debug ("get SSOStatement");
275 auth_st = profile->getSSOStatement(*ssoAssertion);
277 // Maybe verify the origin address....
278 if (argp->checkIPAddress) {
279 log.debug ("check IP Address");
281 // Verify the client address exists
282 const XMLCh* ip = auth_st->getSubjectIP();
284 throw ShibTargetException(SHIBRPC_IPADDR_MISSING,
285 "The IP Address provided by your origin site was missing.",
288 log.debug ("verify client address");
289 // Verify the client address matches authentication
290 auto_ptr<char> this_ip(XMLString::transcode(ip));
291 if (strcmp (argp->client_addr, this_ip.get()))
292 throw ShibTargetException(SHIBRPC_IPADDR_MISMATCH,
293 "The IP address provided by your origin site did not match "
294 "your current address. "
295 "To correct this problem you may need to bypass a local proxy server.",
299 catch (SAMLException &e) // XXX refine this handler to catch and log different profile exceptions
301 log.error ("received SAML exception: %s", e.what());
304 throw ShibTargetException (SHIBRPC_SAML_EXCEPTION, os.str(), origin);
306 catch (XMLException &e)
308 log.error ("received XML exception");
309 auto_ptr<char> msg(XMLString::transcode(e.getMessage()));
310 throw ShibTargetException (SHIBRPC_XML_EXCEPTION, msg.get(), origin);
313 catch (ShibTargetException &e)
315 log.info ("FAILED: %s", e.what());
317 if (origin) delete origin;
318 set_rpc_status_x(&result->status, e.which(), e.what(), e.where());
324 log.error ("Unknown error");
326 if (origin) delete origin;
327 set_rpc_status(&result->status, SHIBRPC_UNKNOWN_ERROR,
328 "An unknown exception occurred", "");
333 // It passes all our tests -- create a new session.
334 log.info ("Creating new session");
336 SAMLAuthenticationStatement* as=static_cast<SAMLAuthenticationStatement*>(auth_st->clone());
338 // Create a new cookie
340 auto_ptr<char> c(XMLString::transcode(id));
341 char *cookie = c.get();
343 // Cache this session with the cookie
344 g_shibTargetCCache->insert(cookie, as, argp->client_addr);
346 // Delete the response...
349 // Delete the origin...
350 XMLString::release(&origin);
352 // And let the user know.
353 if (result->cookie) free(result->cookie);
354 result->cookie = strdup(cookie);
355 set_rpc_status(&result->status, SHIBRPC_OK, NULL, NULL);
357 log.debug("new session id: %s", cookie);
362 shibrpc_get_assertions_1_svc(shibrpc_get_assertions_args_1 *argp,
363 shibrpc_get_assertions_ret_1 *result, struct svc_req *rqstp)
365 Category& log = get_category();
366 string ctx = get_threadid("get_assertions");
369 if (!argp || !result) {
370 log.error ("Invalid RPC arguments");
374 memset (result, 0, sizeof (*result));
376 log.debug ("get attrs for client at %s", argp->cookie.client_addr);
377 log.debug ("cookie: %s", argp->cookie.cookie);
378 log.debug ("resource: %s", argp->url);
381 CCacheEntry* entry = g_shibTargetCCache->find(argp->cookie.cookie);
383 // If it does not exist, leave now..
385 log.error ("No Session");
386 set_rpc_status(&result->status, SHIBRPC_NO_SESSION,
387 "getattrs Internal error: no session", "");
392 const XMLCh* origin = entry->getStatement()->getSubject()->getNameQualifier();
394 // Validate the client address (again?)
395 if (argp->checkIPAddress &&
396 strcmp (argp->cookie.client_addr, entry->getClientAddress())) {
397 log.error ("IP Mismatch");
398 set_rpc_status_x(&result->status, SHIBRPC_IPADDR_MISMATCH,
399 "Your IP address does not match the address in the original authentication.",
406 // grab the attributes for this resource
407 Resource resource(argp->url);
408 Iterator<SAMLAssertion*> iter = entry->getAssertions(resource);
409 u_int size = iter.size();
410 result->assertions.assertions_len = size;
412 // if we have assertions...
415 // Build the response section
417 (ShibRpcXML*) malloc (size * sizeof (ShibRpcXML));
418 result->assertions.assertions_val = av;
420 // and then serialize them all...
422 while (iter.hasNext()) {
423 SAMLAssertion* as = iter.next();
426 av[i++].xml_string = strdup(os.str().c_str());
429 } catch (SAMLException& e) {
430 log.error ("received SAML exception: %s", e.what());
433 set_rpc_status_x(&result->status, SHIBRPC_SAML_EXCEPTION,
434 strdup(os.str().c_str()), origin);
439 // Now grab the serialized authentication statement
440 result->auth_statement.xml_string = strdup(entry->getSerializedStatement());
445 set_rpc_status(&result->status, SHIBRPC_OK, NULL, NULL);
447 log.debug ("returning");
452 shibrpc_prog_1_freeresult (SVCXPRT *transp, xdrproc_t xdr_result, caddr_t result)
454 xdr_free (xdr_result, result);
457 * Insert additional freeing code here, if needed