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.
6 * Created by: Derek Atkins <derek@ihtfp.com>
12 #include "shib-target.h"
14 #include <log4cpp/Category.hh>
19 using namespace shibboleth;
20 using namespace shibtarget;
23 static std::string get_threadid (const char* proc)
25 static u_long counter = 0;
27 buf << "[" << counter++ << "] " << proc;
31 static log4cpp::Category& get_category (void)
33 string ctx = "shibtarget.rpc-server";
34 return log4cpp::Category::getInstance(ctx);
38 shibrpc_ping_1_svc(int *argp, int *result, struct svc_req *rqstp)
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)
49 log4cpp::Category& log = get_category();
50 string ctx = get_threadid("session_is_valid");
53 if (!argp || !result) {
54 log.error ("RPC Argument Error");
58 memset (result, 0, sizeof (*result));
60 log.debug ("checking: %s@%s (checkAddr=%s)",
61 argp->cookie.cookie, argp->cookie.client_addr,
62 argp->checkIPAddress ? "true" : "false");
64 // See if the cookie exists...
65 CCacheEntry *entry = g_shibTargetCCache->find(argp->cookie.cookie);
67 // If not, leave now..
69 log.debug ("Not found");
70 result->status = SHIBRPC_NO_SESSION;
71 result->error_msg = strdup("No session exists for this cookie");
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;
82 strdup ("Your IP address does not match the address in the original authentication.");
83 g_shibTargetCCache->remove (argp->cookie.cookie);
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);
97 // ok, we've succeeded..
98 result->status = SHIBRPC_OK;
99 result->error_msg = strdup("");
100 log.debug ("session ok");
105 shibrpc_new_session_1_svc(shibrpc_new_session_args_1 *argp,
106 shibrpc_new_session_ret_1 *result, struct svc_req *rqstp)
108 log4cpp::Category& log = get_category();
109 string ctx = get_threadid("new_session");
112 if (!argp || !result) {
113 log.error ("Invalid RPC Arguments");
117 // Initialize the result structure
118 memset (result, 0, sizeof(*result));
119 result->cookie = strdup ("");
121 log.debug ("creating session for %s", argp->client_addr);
122 log.debug ("shire location: %s", argp->shire_location);
124 XMLByte* post=reinterpret_cast<XMLByte*>(argp->saml_post);
125 auto_ptr<XMLCh> location(XMLString::transcode(argp->shire_location));
127 // Pull in the Policies
128 static const XMLCh* clubShib[] = {shibboleth::Constants::POLICY_CLUBSHIB};
129 ArrayIterator<const XMLCh*> policies(clubShib);
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,
139 SAMLResponse* r = NULL;
140 SAMLAuthenticationStatement* auth_st = NULL;
146 // Make sure we've got a profile
148 throw ShibTargetException(SHIBRPC_INTERNAL_ERROR,
149 "Failed to obtain the profile");
151 // Try and accept the response...
152 log.debug ("Trying to accept the post");
153 r = profile->accept(post);
155 // Make sure we got a response
157 throw ShibTargetException(SHIBRPC_RESPONSE_MISSING,
158 "Failed to accept the response.");
160 // Find the SSO Assertion
161 log.debug ("Get the SSOAssertion");
162 SAMLAssertion* ssoAssertion = profile->getSSOAssertion(*r);
164 // verify that we obtained an assertion
166 throw ShibTargetException(SHIBRPC_ASSERTION_MISSING,
167 "Cannot find SSO Assertion");
169 // Check against the replay cache
170 log.debug ("check replay cache");
171 if (profile->checkReplayCache(*ssoAssertion) == false)
172 throw ShibTargetException(SHIBRPC_ASSERTION_REPLAYED,
173 "Duplicate assertion found.");
175 // Get the authentication statement we need.
176 log.debug ("get SSOStatement");
177 auth_st = profile->getSSOStatement(*ssoAssertion);
179 // Verify we obtained an authentication statement
181 throw ShibTargetException(SHIBRPC_AUTHSTATEMENT_MISSING,
182 "Processing was successful but no authentication statement was found.");
184 // Maybe verify the origin address....
185 if (argp->checkIPAddress) {
186 log.debug ("check IP Address");
188 // Verify the client address exists
189 const XMLCh* ip = auth_st->getSubjectIP();
191 throw ShibTargetException(SHIBRPC_IPADDR_MISSING,
192 "The IP Address provided by your origin site was missing.");
194 log.debug ("verify client address");
195 // Verify the client address matches authentication
196 auto_ptr<char> this_ip(XMLString::transcode(ip));
197 if (strcmp (argp->client_addr, this_ip.get()))
198 throw ShibTargetException(SHIBRPC_IPADDR_MISMATCH,
199 "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.");
202 catch (SAMLException &e)
204 log.error ("received SAML exception: %s", e.what());
207 throw ShibTargetException (SHIBRPC_SAML_EXCEPTION, os.str());
209 catch (XMLException &e)
211 log.error ("received XML exception");
212 auto_ptr<char> msg(XMLString::transcode(e.getMessage()));
213 throw ShibTargetException (SHIBRPC_XML_EXCEPTION, msg.get());
216 catch (ShibTargetException &e)
218 log.info ("FAILED: %s", e.what());
220 result->status = e.which();
221 result->error_msg = strdup(e.what());
227 log.error ("Unknown error");
229 result->status = SHIBRPC_UNKNOWN_ERROR;
230 result->error_msg = strdup("An unknown exception occurred");
235 // It passes all our tests -- create a new session.
236 log.info ("Creating new session");
238 SAMLAuthenticationStatement* as=static_cast<SAMLAuthenticationStatement*>(auth_st->clone());
239 CCacheEntry* session = CCacheEntry::getInstance (as, argp->client_addr);
241 // Create a new cookie
243 auto_ptr<char> c(XMLString::transcode(id));
244 char *cookie = c.get();
246 // Cache this session with the cookie
247 g_shibTargetCCache->insert(cookie, session);
249 // Delete the response...
252 // And let the user know.
253 free (result->cookie);
254 result->cookie = strdup(cookie);
255 result->status = SHIBRPC_OK;
256 result->error_msg = strdup("");
258 log.debug ("new session id: %s", cookie);
263 shibrpc_get_assertions_1_svc(shibrpc_get_assertions_args_1 *argp,
264 shibrpc_get_assertions_ret_1 *result, struct svc_req *rqstp)
266 log4cpp::Category& log = get_category();
267 string ctx = get_threadid("get_assertions");
270 if (!argp || !result) {
271 log.error ("Invalid RPC arguments");
275 memset (result, 0, sizeof (*result));
277 log.debug ("get attrs for client at %s", argp->cookie.client_addr);
278 log.debug ("cookie: %s", argp->cookie.cookie);
279 log.debug ("resource: %s", argp->url);
282 CCacheEntry* entry = g_shibTargetCCache->find(argp->cookie.cookie);
284 // If it does not exist, leave now..
286 log.error ("No Session");
287 result->status = SHIBRPC_NO_SESSION;
288 result->error_msg = strdup("getattrs Internal error: no session");
292 // Validate the client address (again?)
293 if (argp->checkIPAddress &&
294 strcmp (argp->cookie.client_addr, entry->getClientAddress())) {
295 log.error ("IP Mismatch");
296 result->status = SHIBRPC_IPADDR_MISMATCH;
298 strdup("Your IP address does not match the address in the original authentication.");
303 // grab the attributes for this resource
304 Resource resource(argp->url);
305 Iterator<SAMLAssertion*> iter = entry->getAssertions(resource);
306 u_int size = iter.size();
307 result->assertions.assertions_len = size;
309 // if we have assertions...
312 // Build the response section
313 ShibRpcAssertion_1* av =
314 (ShibRpcAssertion_1*) malloc (size * sizeof (ShibRpcAssertion_1));
315 result->assertions.assertions_val = av;
317 // and then serialize them all...
319 while (iter.hasNext()) {
320 SAMLAssertion* as = iter.next();
323 av[i++].assertion = strdup(os.str().c_str());
326 } catch (SAMLException& e) {
327 log.error ("received SAML exception: %s", e.what());
330 result->status = SHIBRPC_SAML_EXCEPTION;
331 result->error_msg = strdup(os.str().c_str());
336 result->status = SHIBRPC_OK;
337 result->error_msg = strdup("");
339 log.debug ("returning");
344 shibrpc_prog_1_freeresult (SVCXPRT *transp, xdrproc_t xdr_result, caddr_t result)
346 xdr_free (xdr_result, result);
349 * Insert additional freeing code here, if needed