2 #include <tid_internal.h>
12 /* Structure to hold TR instance and original request in one cookie */
13 typedef struct tr_resp_cookie {
19 static void tr_tidc_resp_handler (TIDC_INSTANCE *tidc,
24 tr_debug("tr_tidc_resp_handler: Response received (conn = %d)! Realm = %s, Community = %s.", ((TR_RESP_COOKIE *)resp_cookie)->orig_req->conn, resp->realm->buf, resp->comm->buf);
27 /* TBD -- handle concatentation of multiple responses to single req */
28 tids_send_response(((TR_RESP_COOKIE *)resp_cookie)->tr->tids,
29 ((TR_RESP_COOKIE *)resp_cookie)->orig_req,
35 static int tr_tids_req_handler (TIDS_INSTANCE *tids,
40 TIDC_INSTANCE *tidc = NULL;
41 TR_RESP_COOKIE resp_cookie;
42 TR_AAA_SERVER *aaa_servers = NULL;
44 TID_REQ *fwd_req = NULL;
45 TR_COMM *cfg_comm = NULL;
46 TR_COMM *cfg_apc = NULL;
47 TR_INSTANCE *tr = (TR_INSTANCE *) tr_in;
48 int oaction = TR_FILTER_ACTION_REJECT;
50 time_t expiration_interval;
52 if ((!tids) || (!orig_req) || (!resp) || (!tr)) {
53 tr_debug("tr_tids_req_handler: Bad parameters");
57 tr_debug("tr_tids_req_handler: Request received (conn = %d)! Realm = %s, Comm = %s", orig_req->conn,
58 orig_req->realm->buf, orig_req->comm->buf);
62 /* Duplicate the request, so we can modify and forward it */
63 if (NULL == (fwd_req = tid_dup_req(orig_req))) {
64 tr_debug("tr_tids_req_handler: Unable to duplicate request.");
68 if (NULL == (cfg_comm = tr_comm_lookup(tids->cookie, orig_req->comm))) {
69 tr_notice("tr_tids_req_hander: Request for unknown comm: %s.", orig_req->comm->buf);
70 tids_send_err_response(tids, orig_req, "Unknown community");
74 /* Check that the rp_realm matches the filter for the GSS name that
77 if ((!(tr)->rp_gss) ||
78 (!(tr)->rp_gss->filter)) {
79 tr_notice("tr_tids_req_handler: No GSS name for incoming request.");
80 tids_send_err_response(tids, orig_req, "No GSS name for request");
84 if ((TR_FILTER_NO_MATCH == tr_filter_process_rp_permitted(orig_req->rp_realm, (tr)->rp_gss->filter, orig_req->cons, &fwd_req->cons, &oaction)) ||
85 (TR_FILTER_ACTION_REJECT == oaction)) {
86 tr_notice("tr_tids_req_handler: RP realm (%s) does not match RP Realm filter for GSS name", orig_req->rp_realm->buf);
87 tids_send_err_response(tids, orig_req, "RP Realm filter error");
90 /* Check that the rp_realm is a member of the community in the request */
91 if (NULL == (tr_find_comm_rp(cfg_comm, orig_req->rp_realm))) {
92 tr_notice("tr_tids_req_handler: RP Realm (%s) not member of community (%s).", orig_req->rp_realm->buf, orig_req->comm->buf);
93 tids_send_err_response(tids, orig_req, "RP COI membership error");
97 /* Map the comm in the request from a COI to an APC, if needed */
98 if (TR_COMM_COI == cfg_comm->type) {
99 tr_debug("tr_tids_req_handler: Community was a COI, switching.");
100 /* TBD -- In theory there can be more than one? How would that work? */
101 if ((!cfg_comm->apcs) || (!cfg_comm->apcs->id)) {
102 tr_notice("No valid APC for COI %s.", orig_req->comm->buf);
103 tids_send_err_response(tids, orig_req, "No valid APC for community");
106 apc = tr_dup_name(cfg_comm->apcs->id);
108 /* Check that the APC is configured */
109 if (NULL == (cfg_apc = tr_comm_lookup(tids->cookie, apc))) {
110 tr_notice("tr_tids_req_hander: Request for unknown comm: %s.", apc->buf);
111 tids_send_err_response(tids, orig_req, "Unknown APC");
116 fwd_req->orig_coi = orig_req->comm;
118 /* Check that rp_realm is a member of this APC */
119 if (NULL == (tr_find_comm_rp(cfg_apc, orig_req->rp_realm))) {
120 tr_notice("tr_tids_req_hander: RP Realm (%s) not member of community (%s).", orig_req->rp_realm->buf, orig_req->comm->buf);
121 tids_send_err_response(tids, orig_req, "RP APC membership error");
126 /* Find the AAA server(s) for this request */
127 if (NULL == (aaa_servers = tr_idp_aaa_server_lookup(((TR_INSTANCE *)tids->cookie)->active_cfg->idp_realms,
130 tr_debug("tr_tids_req_handler: No AAA Servers for realm %s, defaulting.", orig_req->realm->buf);
131 if (NULL == (aaa_servers = tr_default_server_lookup (((TR_INSTANCE *)tids->cookie)->active_cfg->default_servers,
133 tr_notice("tr_tids_req_handler: No default AAA servers, discarded.");
134 tids_send_err_response(tids, orig_req, "No path to AAA Server(s) for realm");
138 /* if we aren't defaulting, check idp coi and apc membership */
139 if (NULL == (tr_find_comm_idp(cfg_comm, fwd_req->realm))) {
140 tr_notice("tr_tids_req_handler: IDP Realm (%s) not member of community (%s).", orig_req->realm->buf, orig_req->comm->buf);
141 tids_send_err_response(tids, orig_req, "IDP community membership error");
144 if ( cfg_apc && (NULL == (tr_find_comm_idp(cfg_apc, fwd_req->realm)))) {
145 tr_notice("tr_tids_req_handler: IDP Realm (%s) not member of APC (%s).", orig_req->realm->buf, orig_req->comm->buf);
146 tids_send_err_response(tids, orig_req, "IDP APC membership error");
151 /* send a TID request to the AAA server(s), and get the answer(s) */
152 /* TBD -- Handle multiple servers */
155 expiration_interval = cfg_apc->expiration_interval;
156 else expiration_interval = cfg_comm->expiration_interval;
157 if (fwd_req->expiration_interval)
158 fwd_req->expiration_interval = (expiration_interval < fwd_req->expiration_interval) ? expiration_interval : fwd_req->expiration_interval;
159 else fwd_req->expiration_interval = expiration_interval;
160 /* Create a TID client instance */
161 if (NULL == (tidc = tidc_create())) {
162 tr_crit("tr_tids_req_hander: Unable to allocate TIDC instance.");
163 tids_send_err_response(tids, orig_req, "Memory allocation failure");
166 /* Use the DH parameters from the original request */
167 /* TBD -- this needs to be fixed when we handle more than one req per conn */
168 tidc->client_dh = orig_req->tidc_dh;
170 /* Save information about this request for the response */
172 resp_cookie.orig_req = orig_req;
174 /* Set-up TID connection */
175 if (-1 == (fwd_req->conn = tidc_open_connection(tidc,
176 aaa_servers->hostname->buf,
178 &(fwd_req->gssctx)))) {
179 tr_notice("tr_tids_req_handler: Error in tidc_open_connection.");
180 tids_send_err_response(tids, orig_req, "Can't open connection to next hop TIDS");
184 /* Send a TID request */
185 if (0 > (rc = tidc_fwd_request(tidc, fwd_req, &tr_tidc_resp_handler, (void *)&resp_cookie))) {
186 tr_notice("Error from tidc_fwd_request, rc = %d.", rc);
187 tids_send_err_response(tids, orig_req, "Can't forward request to next hop TIDS");
188 tid_req_free(orig_req);
192 tid_req_free(orig_req);
196 static int tr_tids_gss_handler(gss_name_t client_name, TR_NAME *gss_name,
200 TR_INSTANCE *tr = (TR_INSTANCE *) tr_in;
202 if ((!client_name) || (!gss_name) || (!tr)) {
203 tr_debug("tr_tidc_gss_handler: Bad parameters.");
207 /* look up the RP client matching the GSS name */
208 if ((NULL == (rp = tr_rp_client_lookup(tr->active_cfg->rp_clients, gss_name)))) {
209 tr_debug("tr_tids_gss_handler: Unknown GSS name %s", gss_name->buf);
213 /* Store the rp client in the TR_INSTANCE structure for now...
214 * TBD -- fix me for new tasking model. */
216 tr_debug("Client's GSS Name: %s", gss_name->buf);
222 /***** TIDS event handling *****/
224 /* called when a connection to the TIDS port is received */
225 static void tr_tids_event_cb(int listener, short event, void *arg)
227 TIDS_INSTANCE *tids = (TIDS_INSTANCE *)arg;
229 if (0==(event & EV_READ))
230 tr_debug("tr_tids_event_cb: unexpected event on TIDS socket (event=0x%X)", event);
232 tids_accept(tids, listener);
235 /* Configure the tids instance and set up its event handler.
236 * Returns 0 on success, nonzero on failure. Fills in
237 * *tids_event (which should be allocated). */
238 int tr_tids_event_init(struct event_base *base,
240 struct tr_socket_event *tids_ev)
242 if (tids_ev == NULL) {
243 tr_debug("tr_tids_event_init: Null tids_ev.");
247 /* get a tids listener */
248 tids_ev->sock_fd=tids_get_listener(tr->tids,
251 tr->active_cfg->internal->hostname,
252 tr->active_cfg->internal->tids_port,
254 if (tids_ev->sock_fd < 0) {
255 tr_crit("Error opening TID server socket.");
260 tids_ev->ev=event_new(base,
265 event_add(tids_ev->ev, NULL);