Return error when TID requests fail. (bug #1648118)
[trust_router.git] / tr / tr_tid.c
1 /*
2  * Copyright (c) 2016, JANET(UK)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * 3. Neither the name of JANET(UK) nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31  * OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  */
34
35 #include <talloc.h>
36
37 #include <trust_router/tr_dh.h>
38 #include <tid_internal.h>
39 #include <tr_filter.h>
40 #include <tr_comm.h>
41 #include <tr_idp.h>
42 #include <tr_rp.h>
43 #include <tr_event.h>
44 #include <tr_debug.h>
45 #include <gsscon.h>
46 #include <trp_internal.h>
47 #include <tr_config.h>
48 #include <tr_mq.h>
49 #include <tr_util.h>
50 #include <tr_tid.h>
51
52 /* Structure to hold data for the tid response callback */
53 typedef struct tr_resp_cookie {
54   int thread_id;
55   TID_RESP *resp;
56 } TR_RESP_COOKIE;
57
58 /* hold a tids instance and a config manager */
59 struct tr_tids_event_cookie {
60   TIDS_INSTANCE *tids;
61   TR_CFG_MGR *cfg_mgr;
62   TRPS_INSTANCE *trps;
63 };
64
65 static void tr_tidc_resp_handler(TIDC_INSTANCE *tidc, 
66                                  TID_REQ *req,
67                                  TID_RESP *resp, 
68                                  void *resp_cookie)
69 {
70   TR_RESP_COOKIE *cookie=talloc_get_type_abort(resp_cookie, TR_RESP_COOKIE);
71
72   tr_debug("tr_tidc_resp_handler: Response received! Realm = %s, Community = %s.",
73            resp->realm->buf,
74            resp->comm->buf);
75   
76   cookie->resp=tid_resp_dup(cookie, resp);
77 }
78
79 /* data for AAA req forwarding threads */
80 struct tr_tids_fwd_cookie {
81   int thread_id;
82   pthread_mutex_t mutex; /* lock on the mq (separate from the locking within the mq, see below) */
83   TR_MQ *mq; /* messages from thread to main process; set to NULL to disable response */
84   TR_NAME *aaa_hostname;
85   DH *dh_params;
86   TID_REQ *fwd_req; /* the req to duplicate */
87 };
88
89 static int tr_tids_fwd_cookie_destructor(void *obj)
90 {
91   struct tr_tids_fwd_cookie *c=talloc_get_type_abort(obj, struct tr_tids_fwd_cookie);
92   if (c->aaa_hostname!=NULL)
93     tr_free_name(c->aaa_hostname);
94   if (c->dh_params!=NULL)
95     tr_destroy_dh_params(c->dh_params);
96   return 0;
97 }
98
99 /* Block until we get the lock, returns 0 on success.
100  * The mutex is used to protect changes to the mq pointer in
101  * a thread's cookie. The master thread sets this to null to indicate
102  * that it has abandoned the thread and the message queue is no longer
103  * valid. This is unrelated to the locking in the message queue
104  * implementation itself. */
105 static int tr_tids_fwd_get_mutex(struct tr_tids_fwd_cookie *cookie)
106 {
107   if (cookie==NULL)
108     return -1;
109
110   return (pthread_mutex_lock(&(cookie->mutex)));
111 }
112
113 static int tr_tids_fwd_release_mutex(struct tr_tids_fwd_cookie *cookie)
114 {
115   if (cookie==NULL)
116     return -1;
117
118   return (pthread_mutex_unlock(&(cookie->mutex)));
119 }
120
121 /* values for messages */
122 #define TR_TID_MQMSG_SUCCESS "tid success"
123 #define TR_TID_MQMSG_FAILURE "tid failure"
124
125 /* Thread main for sending and receiving a request to a single AAA server */
126 static void *tr_tids_req_fwd_thread(void *arg)
127 {
128   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
129   struct tr_tids_fwd_cookie *args=talloc_get_type_abort(arg, struct tr_tids_fwd_cookie);
130   TIDC_INSTANCE *tidc=tidc_create(tmp_ctx);
131   TR_MQ_MSG *msg=NULL;
132   TR_RESP_COOKIE *cookie=NULL;
133   int rc=0;
134   int success=0;
135
136   talloc_steal(tmp_ctx, args); /* take responsibility for the cookie */
137
138   /* create the cookie we will use for our response */
139   cookie=talloc(tmp_ctx, TR_RESP_COOKIE);
140   if (cookie==NULL) {
141     tr_notice("tr_tids_req_fwd_thread: unable to allocate response cookie.");
142     success=0;
143     goto cleanup;
144   }
145   cookie->thread_id=args->thread_id;
146
147   /* Create a TID client instance */
148   if (tidc==NULL) {
149     tr_crit("tr_tids_req_fwd_thread: Unable to allocate TIDC instance.");
150     /*tids_send_err_response(tids, orig_req, "Memory allocation failure");*/
151     /* TODO: encode reason for failure */
152     success=0;
153     goto cleanup;
154   }
155
156   /* Set-up TID connection */
157   if (-1==(args->fwd_req->conn = tidc_open_connection(tidc, 
158                                                       args->aaa_hostname->buf,
159                                                       TID_PORT, /* TODO: make this configurable */
160                                                      &(args->fwd_req->gssctx)))) {
161     tr_notice("tr_tids_req_fwd_thread: Error in tidc_open_connection.");
162     /* tids_send_err_response(tids, orig_req, "Can't open connection to next hop TIDS"); */
163     /* TODO: encode reason for failure */
164     success=0;
165     goto cleanup;
166   };
167
168   /* Send a TID request. */
169   if (0 > (rc = tidc_fwd_request(tidc, args->fwd_req, tr_tidc_resp_handler, (void *)cookie))) {
170     tr_notice("Error from tidc_fwd_request, rc = %d.", rc);
171     success=0;
172     goto cleanup;
173   }
174   /* cookie->resp should now contain our copy of the response */
175   success=1;
176
177 cleanup:
178   /* Notify parent thread of the response, if it's still listening. */
179   if (0!=tr_tids_fwd_get_mutex(args)) {
180     tr_notice("tr_tids_req_fwd_thread: Error acquiring mutex.");
181   } else if (NULL!=args->mq) {
182     /* mq is still valid, so we can queue our response */
183     if (success)
184       msg=tr_mq_msg_new(tmp_ctx, TR_TID_MQMSG_SUCCESS, TR_MQ_PRIO_NORMAL);
185     else
186       msg=tr_mq_msg_new(tmp_ctx, TR_TID_MQMSG_FAILURE, TR_MQ_PRIO_NORMAL);
187
188     if (msg==NULL)
189       tr_notice("tr_tids_req_fwd_thread: unable to allocate response msg.");
190
191     tr_mq_msg_set_payload(msg, (void *)cookie, NULL);
192     if (NULL!=cookie)
193       talloc_steal(msg, cookie); /* attach this to the msg so we can forget about it */
194     tr_mq_add(args->mq, msg);
195     talloc_steal(NULL, args); /* take out of our tmp_ctx; master thread now responsible for freeing */
196     if (0!=tr_tids_fwd_release_mutex(args))
197       tr_notice("tr_tids_req_fwd_thread: Error releasing mutex.");
198   }
199
200   talloc_free(tmp_ctx);
201   return NULL;
202 }
203
204 /* Merges r2 into r1 if they are compatible. */
205 static TID_RC tr_tids_merge_resps(TID_RESP *r1, TID_RESP *r2)
206 {
207   /* ensure these are compatible replies */
208   if ((r1->result!=TID_SUCCESS) || (r2->result!=TID_SUCCESS))
209     return TID_ERROR;
210
211   if ((0!=tr_name_cmp(r1->rp_realm, r2->rp_realm)) ||
212       (0!=tr_name_cmp(r1->realm, r2->realm)) ||
213       (0!=tr_name_cmp(r1->comm, r2->comm)))
214     return TID_ERROR;
215
216   tid_srvr_blk_add(r1->servers, tid_srvr_blk_dup(r1, r2->servers));
217   return TID_SUCCESS;
218 }
219
220 static int tr_tids_req_handler(TIDS_INSTANCE *tids,
221                                TID_REQ *orig_req, 
222                                TID_RESP *resp,
223                                void *cookie_in)
224 {
225   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
226   TR_AAA_SERVER *aaa_servers=NULL, *this_aaa=NULL;
227   int n_aaa=0;
228   int idp_shared=0;
229   TR_AAA_SERVER_ITER *aaa_iter=NULL;
230   pthread_t aaa_thread[TR_TID_MAX_AAA_SERVERS];
231   struct tr_tids_fwd_cookie *aaa_cookie[TR_TID_MAX_AAA_SERVERS]={NULL};
232   TID_RESP *aaa_resp[TR_TID_MAX_AAA_SERVERS]={NULL};
233   TR_NAME *apc = NULL;
234   TID_REQ *fwd_req = NULL;
235   TR_COMM *cfg_comm = NULL;
236   TR_COMM *cfg_apc = NULL;
237   int oaction = TR_FILTER_ACTION_REJECT;
238   time_t expiration_interval=0;
239   struct tr_tids_event_cookie *cookie=talloc_get_type_abort(cookie_in, struct tr_tids_event_cookie);
240   TR_CFG_MGR *cfg_mgr=cookie->cfg_mgr;
241   TRPS_INSTANCE *trps=cookie->trps;
242   TRP_ROUTE *route=NULL;
243   TR_MQ *mq=NULL;
244   TR_MQ_MSG *msg=NULL;
245   unsigned int n_responses=0;
246   unsigned int n_failed=0;
247   struct timespec ts_abort={0};
248   unsigned int resp_frac_numer=cfg_mgr->active->internal->tid_resp_numer;
249   unsigned int resp_frac_denom=cfg_mgr->active->internal->tid_resp_denom;
250   TR_RESP_COOKIE *payload=NULL;
251   int ii=0;
252   int retval=-1;
253
254   if ((!tids) || (!orig_req) || (!resp)) {
255     tr_debug("tr_tids_req_handler: Bad parameters");
256     retval=-1;
257     goto cleanup;
258   }
259
260   tr_debug("tr_tids_req_handler: Request received (conn = %d)! Realm = %s, Comm = %s", orig_req->conn, 
261            orig_req->realm->buf, orig_req->comm->buf);
262   tids->req_count++;
263
264   /* Duplicate the request, so we can modify and forward it */
265   if (NULL == (fwd_req=tid_dup_req(tmp_ctx, orig_req))) {
266     tr_debug("tr_tids_req_handler: Unable to duplicate request.");
267     retval=-1;
268     goto cleanup;
269   }
270
271   if (NULL == (cfg_comm=tr_comm_table_find_comm(cfg_mgr->active->ctable, orig_req->comm))) {
272     tr_notice("tr_tids_req_hander: Request for unknown comm: %s.", orig_req->comm->buf);
273     tids_send_err_response(tids, orig_req, "Unknown community");
274     retval=-1;
275     goto cleanup;
276   }
277
278   /* Check that the rp_realm matches the filter for the GSS name that 
279    * was received. N.B. that tids->rp_gss was pointed at the correct
280    * rp_client when we received its GSS name. It is only set within
281    * the TIDS handler subprocess. */
282
283   if ((!tids->rp_gss) || 
284       (!tids->rp_gss->filter)) {
285     tr_notice("tr_tids_req_handler: No GSS name for incoming request.");
286     tids_send_err_response(tids, orig_req, "No GSS name for request");
287     retval=-1;
288     goto cleanup;
289   }
290
291   if ((TR_FILTER_NO_MATCH == tr_filter_process_rp_permitted(orig_req->rp_realm,
292                                                             tids->rp_gss->filter,
293                                                             orig_req->cons,
294                                                            &fwd_req->cons,
295                                                            &oaction)) ||
296       (TR_FILTER_ACTION_REJECT == oaction)) {
297     tr_notice("tr_tids_req_handler: RP realm (%s) does not match RP Realm filter for GSS name", orig_req->rp_realm->buf);
298     tids_send_err_response(tids, orig_req, "RP Realm filter error");
299     retval=-1;
300     goto cleanup;
301   }
302   /* Check that the rp_realm is a member of the community in the request */
303   if (NULL == tr_comm_find_rp(cfg_mgr->active->ctable, cfg_comm, orig_req->rp_realm)) {
304     tr_notice("tr_tids_req_handler: RP Realm (%s) not member of community (%s).", orig_req->rp_realm->buf, orig_req->comm->buf);
305     tids_send_err_response(tids, orig_req, "RP COI membership error");
306     retval=-1;
307     goto cleanup;
308   }
309
310   /* Map the comm in the request from a COI to an APC, if needed */
311   if (TR_COMM_COI == cfg_comm->type) {
312     if (orig_req->orig_coi!=NULL) {
313       tr_notice("tr_tids_req_handler: community %s is COI but COI to APC mapping already occurred. Dropping request.",
314                orig_req->comm->buf);
315       tids_send_err_response(tids, orig_req, "Second COI to APC mapping would result, permitted only once.");
316       retval=-1;
317       goto cleanup;
318     }
319     
320     tr_debug("tr_tids_req_handler: Community was a COI, switching.");
321     /* TBD -- In theory there can be more than one?  How would that work? */
322     if ((!cfg_comm->apcs) || (!cfg_comm->apcs->id)) {
323       tr_notice("No valid APC for COI %s.", orig_req->comm->buf);
324       tids_send_err_response(tids, orig_req, "No valid APC for community");
325       retval=-1;
326       goto cleanup;
327     }
328     apc = tr_dup_name(cfg_comm->apcs->id);
329
330     /* Check that the APC is configured */
331     if (NULL == (cfg_apc = tr_comm_table_find_comm(cfg_mgr->active->ctable, apc))) {
332       tr_notice("tr_tids_req_hander: Request for unknown comm: %s.", apc->buf);
333       tids_send_err_response(tids, orig_req, "Unknown APC");
334       retval=-1;
335       goto cleanup;
336     }
337
338     fwd_req->comm = apc;
339     fwd_req->orig_coi = orig_req->comm;
340
341     /* Check that rp_realm is a  member of this APC */
342     if (NULL == (tr_comm_find_rp(cfg_mgr->active->ctable, cfg_apc, orig_req->rp_realm))) {
343       tr_notice("tr_tids_req_hander: RP Realm (%s) not member of community (%s).", orig_req->rp_realm->buf, orig_req->comm->buf);
344       tids_send_err_response(tids, orig_req, "RP APC membership error");
345       retval=-1;
346       goto cleanup;
347     }
348   }
349
350   /* Look up the route for this community/realm. */
351   tr_debug("tr_tids_req_handler: looking up route.");
352   route=trps_get_selected_route(trps, orig_req->comm, orig_req->realm);
353   if (route==NULL) {
354     tr_notice("tr_tids_req_handler: no route table entry found for realm (%s) in community (%s).",
355               orig_req->realm->buf, orig_req->comm->buf);
356     tids_send_err_response(tids, orig_req, "Missing trust route error");
357     retval=-1;
358     goto cleanup;
359   }
360   tr_debug("tr_tids_req_handler: found route.");
361   if (trp_route_is_local(route)) {
362     tr_debug("tr_tids_req_handler: route is local.");
363     aaa_servers = tr_idp_aaa_server_lookup(cfg_mgr->active->ctable->idp_realms, 
364                                            orig_req->realm, 
365                                            orig_req->comm,
366                                           &idp_shared);
367   } else {
368     tr_debug("tr_tids_req_handler: route not local.");
369     aaa_servers = tr_aaa_server_new(tmp_ctx, trp_route_get_next_hop(route));
370     idp_shared=0;
371   }
372
373   /* Find the AAA server(s) for this request */
374   if (NULL == aaa_servers) {
375     tr_debug("tr_tids_req_handler: No AAA Servers for realm %s, defaulting.", orig_req->realm->buf);
376     if (NULL == (aaa_servers = tr_default_server_lookup (cfg_mgr->active->default_servers,
377                                                          orig_req->comm))) {
378       tr_notice("tr_tids_req_handler: No default AAA servers, discarded.");
379       tids_send_err_response(tids, orig_req, "No path to AAA Server(s) for realm");
380       retval=-1;
381       goto cleanup;
382     }
383     idp_shared=0;
384   } else {
385     /* if we aren't defaulting, check idp coi and apc membership */
386     if (NULL == (tr_comm_find_idp(cfg_mgr->active->ctable, cfg_comm, fwd_req->realm))) {
387       tr_notice("tr_tids_req_handler: IDP Realm (%s) not member of community (%s).", orig_req->realm->buf, orig_req->comm->buf);
388       tids_send_err_response(tids, orig_req, "IDP community membership error");
389       retval=-1;
390       goto cleanup;
391     }
392     if ( cfg_apc && (NULL == (tr_comm_find_idp(cfg_mgr->active->ctable, cfg_apc, fwd_req->realm)))) {
393       tr_notice("tr_tids_req_handler: IDP Realm (%s) not member of APC (%s).", orig_req->realm->buf, orig_req->comm->buf);
394       tids_send_err_response(tids, orig_req, "IDP APC membership error");
395       retval=-1;
396       goto cleanup;
397     }
398   }
399
400   /* send a TID request to the AAA server(s), and get the answer(s) */
401   if (cfg_apc)
402     expiration_interval = cfg_apc->expiration_interval;
403   else expiration_interval = cfg_comm->expiration_interval;
404   if (fwd_req->expiration_interval)
405     fwd_req->expiration_interval =  (expiration_interval < fwd_req->expiration_interval) ? expiration_interval : fwd_req->expiration_interval;
406   else fwd_req->expiration_interval = expiration_interval;
407
408   /* Set up message queue for replies from req forwarding threads */
409   mq=tr_mq_new(tmp_ctx);
410   if (mq==NULL) {
411     tr_notice("tr_tids_req_handler: unable to allocate message queue.");
412     retval=-1;
413     goto cleanup;
414   }
415
416   /* start threads */
417   aaa_iter=tr_aaa_server_iter_new(tmp_ctx);
418   if (aaa_iter==NULL) {
419     tr_notice("tr_tids_req_handler: unable to allocate AAA server iterator.");
420     retval=-1;
421     goto cleanup;
422   }
423   for (n_aaa=0, this_aaa=tr_aaa_server_iter_first(aaa_iter, aaa_servers);
424        this_aaa!=NULL;
425        n_aaa++, this_aaa=tr_aaa_server_iter_next(aaa_iter)) {
426     aaa_cookie[n_aaa]=talloc(tmp_ctx, struct tr_tids_fwd_cookie);
427     if (aaa_cookie[n_aaa]==NULL) {
428       tr_notice("tr_tids_req_handler: unable to allocate cookie for AAA thread %d.", n_aaa);
429       retval=-1;
430       goto cleanup;
431     }
432     talloc_set_destructor((void *)(aaa_cookie[n_aaa]), tr_tids_fwd_cookie_destructor);
433     /* fill in the cookie. To ensure the thread has valid data even if we exit first and
434      * abandon it, duplicate anything pointed to (except the mq). */
435     aaa_cookie[n_aaa]->thread_id=n_aaa;
436     if (0!=pthread_mutex_init(&(aaa_cookie[n_aaa]->mutex), NULL)) {
437       tr_notice("tr_tids_req_handler: unable to init mutex for AAA thread %d.", n_aaa);
438       retval=-1;
439       goto cleanup;
440     }
441     aaa_cookie[n_aaa]->mq=mq;
442     aaa_cookie[n_aaa]->aaa_hostname=tr_dup_name(this_aaa->hostname);
443     aaa_cookie[n_aaa]->dh_params=tr_dh_dup(orig_req->tidc_dh);
444     aaa_cookie[n_aaa]->fwd_req=tid_dup_req(aaa_cookie[n_aaa], fwd_req);
445
446     /* Take the cookie out of tmp_ctx before starting thread. If thread starts, it becomes
447      * responsible for freeing it until it queues a response. */
448     talloc_steal(NULL, aaa_cookie[n_aaa]);
449     if (0!=pthread_create(&(aaa_thread[n_aaa]), NULL, tr_tids_req_fwd_thread, aaa_cookie[n_aaa])) {
450       talloc_steal(tmp_ctx, aaa_cookie[n_aaa]); /* thread start failed; steal this back */
451       tr_notice("tr_tids_req_handler: unable to start AAA thread %d.", n_aaa);
452       retval=-1;
453       goto cleanup;
454     }
455   }
456  
457   /* determine expiration time */
458   if (0!=tr_mq_pop_timeout(cfg_mgr->active->internal->tid_req_timeout, &ts_abort)) {
459     tr_notice("tr_tids_req_handler: unable to read clock for timeout.");
460     retval=-1;
461     goto cleanup;
462   }
463
464   /* wait for responses */
465   n_responses=0;
466   n_failed=0;
467   while (((n_responses+n_failed)<n_aaa) &&
468          (NULL!=(msg=tr_mq_pop(mq, &ts_abort)))) {
469     /* process message */
470     if (0==strcmp(tr_mq_msg_get_message(msg), TR_TID_MQMSG_SUCCESS)) {
471       payload=talloc_get_type_abort(tr_mq_msg_get_payload(msg), TR_RESP_COOKIE);
472       aaa_resp[payload->thread_id]=payload->resp; /* save pointers to these */
473
474       if (payload->resp->result==TID_SUCCESS) {
475         tr_tids_merge_resps(resp, payload->resp);
476         n_responses++;
477       } else {
478         n_failed++;
479         tr_notice("tr_tids_req_handler: TID error received from AAA server %d: %.*s",
480                   payload->thread_id,
481                   payload->resp->err_msg->len,
482                   payload->resp->err_msg->buf);
483       }
484     } else if (0==strcmp(tr_mq_msg_get_message(msg), TR_TID_MQMSG_FAILURE)) {
485       /* failure */
486       n_failed++;
487       payload=talloc_get_type(tr_mq_msg_get_payload(msg), TR_RESP_COOKIE);
488       if (payload==NULL) {
489         /* this means the thread was unable to allocate a response cookie, and we thus cannot determine which thread it was. This is bad and should never happen in a working system.. Give up. */
490         tr_notice("tr_tids_req_handler: TID request thread sent invalid reply. Aborting!");
491         retval=-1;
492         goto cleanup;
493       }
494       tr_notice("tr_tids_req_handler: TID request for AAA server %d failed.",
495                 payload->thread_id);
496     } else {
497       /* unexpected message */
498       tr_err("tr_tids_req_handler: Unexpected message received. Aborting!");
499       retval=-1;
500       goto cleanup;
501     }
502     
503     /* Now free the cookie for this thread. Null it so we know we've dealt with it. */
504     talloc_free(aaa_cookie[payload->thread_id]);
505     aaa_cookie[payload->thread_id]=NULL;
506
507     tr_mq_msg_free(msg);
508
509     /* check whether we've received enough responses to exit */
510     if ((idp_shared && (n_responses>0)) ||
511         (resp_frac_denom*n_responses>=resp_frac_numer*n_aaa))
512       break;
513   }
514
515   /* Inform any remaining threads that we will no longer handle their responses. */
516   for (ii=0; ii<n_aaa; ii++) {
517     if (aaa_cookie[ii]!=NULL) {
518       if (0!=tr_tids_fwd_get_mutex(aaa_cookie[ii]))
519         tr_notice("tr_tids_req_handler: unable to get mutex for AAA thread %d.", ii);
520
521       aaa_cookie[ii]->mq=NULL; /* threads will not try to respond through a null mq */
522
523       if (0!=tr_tids_fwd_release_mutex(aaa_cookie[ii]))
524         tr_notice("tr_tids_req_handler: unable to release mutex for AAA thread %d.", ii);
525     }
526   }
527
528   /* Now all threads have either replied (and aaa_cookie[ii] is null) or have been told not to
529    * reply (by setting their mq pointer to null). However, some may have responded by placing
530    * a message on the mq after we last checked but before we set their mq pointer to null. These
531    * will not know that we gave up on them, so we must free their cookies for them. We can just
532    * go through any remaining messages on the mq to identify these threads. */
533   while (NULL!=(msg=tr_mq_pop(mq, NULL))) {
534     payload=(TR_RESP_COOKIE *)tr_mq_msg_get_payload(msg);
535     if (aaa_cookie[payload->thread_id]!=NULL)
536       talloc_free(aaa_cookie[payload->thread_id]);
537
538     tr_mq_msg_free(msg);
539   }
540
541   if (n_responses==0) {
542     /* No requests succeeded. Forward an error if we got any error responses. */
543     for (ii=0; ii<n_aaa; ii++) {
544       if (aaa_resp[ii]!=NULL)
545         tids_send_response(tids, orig_req, aaa_resp[ii]);
546       else
547         tids_send_err_response(tids, orig_req, "Unable to contact AAA server(s).");
548     }
549   }
550
551   /* success! */
552   retval=0;
553     
554 cleanup:
555   talloc_free(tmp_ctx);
556   return retval;
557 }
558
559 static int tr_tids_gss_handler(gss_name_t client_name, TR_NAME *gss_name,
560                                void *data)
561 {
562   TR_RP_CLIENT *rp;
563   struct tr_tids_event_cookie *cookie=talloc_get_type_abort(data, struct tr_tids_event_cookie);
564   TIDS_INSTANCE *tids = cookie->tids;
565   TR_CFG_MGR *cfg_mgr = cookie->cfg_mgr;
566
567   if ((!client_name) || (!gss_name) || (!tids) || (!cfg_mgr)) {
568     tr_debug("tr_tidc_gss_handler: Bad parameters.");
569     return -1;
570   }
571
572   /* look up the RP client matching the GSS name */
573   if ((NULL == (rp = tr_rp_client_lookup(cfg_mgr->active->rp_clients, gss_name)))) {
574     tr_debug("tr_tids_gss_handler: Unknown GSS name %s", gss_name->buf);
575     return -1;
576   }
577
578   /* Store the rp client */
579   tids->rp_gss = rp;
580   tr_debug("Client's GSS Name: %s", gss_name->buf);
581
582   return 0;
583 }
584
585
586 /***** TIDS event handling *****/
587
588 /* called when a connection to the TIDS port is received */
589 static void tr_tids_event_cb(int listener, short event, void *arg)
590 {
591   TIDS_INSTANCE *tids = (TIDS_INSTANCE *)arg;
592
593   if (0==(event & EV_READ))
594     tr_debug("tr_tids_event_cb: unexpected event on TIDS socket (event=0x%X)", event);
595   else 
596     tids_accept(tids, listener);
597 }
598
599 /* Configure the tids instance and set up its event handler.
600  * Returns 0 on success, nonzero on failure. Fills in
601  * *tids_event (which should be allocated by caller). */
602 int tr_tids_event_init(struct event_base *base,
603                        TIDS_INSTANCE *tids,
604                        TR_CFG_MGR *cfg_mgr,
605                        TRPS_INSTANCE *trps,
606                        struct tr_socket_event *tids_ev)
607 {
608   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
609   struct tr_tids_event_cookie *cookie=NULL;
610   int retval=0;
611   size_t ii=0;
612
613   if (tids_ev == NULL) {
614     tr_debug("tr_tids_event_init: Null tids_ev.");
615     retval=1;
616     goto cleanup;
617   }
618
619   /* Create the cookie for callbacks. We'll put it in the tids context, so it will
620    * be cleaned up when tids is freed by talloc_free. */
621   cookie=talloc(tmp_ctx, struct tr_tids_event_cookie);
622   if (cookie == NULL) {
623     tr_debug("tr_tids_event_init: Unable to allocate cookie.");
624     retval=1;
625     goto cleanup;
626   }
627   cookie->tids=tids;
628   cookie->cfg_mgr=cfg_mgr;
629   cookie->trps=trps;
630   talloc_steal(tids, cookie);
631
632   /* get a tids listener */
633   tids_ev->n_sock_fd=tids_get_listener(tids,
634                                        tr_tids_req_handler,
635                                        tr_tids_gss_handler,
636                                        cfg_mgr->active->internal->hostname,
637                                        cfg_mgr->active->internal->tids_port,
638                                        (void *)cookie,
639                                        tids_ev->sock_fd,
640                                        TR_MAX_SOCKETS);
641   if (tids_ev->n_sock_fd==0) {
642     tr_crit("Error opening TID server socket.");
643     retval=1;
644     goto cleanup;
645   }
646
647   /* Set up events */
648   for (ii=0; ii<tids_ev->n_sock_fd; ii++) {
649     tids_ev->ev[ii]=event_new(base,
650                               tids_ev->sock_fd[ii],
651                               EV_READ|EV_PERSIST,
652                               tr_tids_event_cb,
653                               (void *)tids);
654     event_add(tids_ev->ev[ii], NULL);
655   }
656
657 cleanup:
658   talloc_free(tmp_ctx);
659   return retval;
660 }