3 #include <tid_internal.h>
11 #include <trp_internal.h>
12 #include <tr_config.h>
15 /* Structure to hold TR instance and original request in one cookie */
16 typedef struct tr_resp_cookie {
21 /* hold a tids instance and a config manager */
22 struct tr_tids_event_cookie {
29 static void tr_tidc_resp_handler (TIDC_INSTANCE *tidc,
34 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);
37 /* TBD -- handle concatentation of multiple responses to single req */
38 tids_send_response(((TR_RESP_COOKIE *)resp_cookie)->tids,
39 ((TR_RESP_COOKIE *)resp_cookie)->orig_req,
45 static int tr_tids_req_handler (TIDS_INSTANCE *tids,
50 TALLOC_CTX *tmp_ctx=talloc_new(NULL);;
51 TIDC_INSTANCE *tidc = NULL;
52 TR_RESP_COOKIE resp_cookie;
53 TR_AAA_SERVER *aaa_servers = NULL;
55 TID_REQ *fwd_req = NULL;
56 TR_COMM *cfg_comm = NULL;
57 TR_COMM *cfg_apc = NULL;
58 int oaction = TR_FILTER_ACTION_REJECT;
60 time_t expiration_interval=0;
61 struct tr_tids_event_cookie *cookie=(struct tr_tids_event_cookie *)cookie_in;
62 TR_CFG_MGR *cfg_mgr=talloc_get_type_abort(cookie, TR_CFG_MGR);
63 TRPS_INSTANCE *trps=talloc_get_type_abort(cookie, TRPS_INSTANCE);
64 TRP_ROUTE *route=NULL;
67 if ((!tids) || (!orig_req) || (!resp)) {
68 tr_debug("tr_tids_req_handler: Bad parameters");
73 tr_debug("tr_tids_req_handler: Request received (conn = %d)! Realm = %s, Comm = %s", orig_req->conn,
74 orig_req->realm->buf, orig_req->comm->buf);
77 /* Duplicate the request, so we can modify and forward it */
78 if (NULL == (fwd_req = tid_dup_req(orig_req))) {
79 tr_debug("tr_tids_req_handler: Unable to duplicate request.");
84 if (NULL == (cfg_comm = tr_comm_lookup(cfg_mgr->active->comms, orig_req->comm))) {
85 tr_notice("tr_tids_req_hander: Request for unknown comm: %s.", orig_req->comm->buf);
86 tids_send_err_response(tids, orig_req, "Unknown community");
91 /* Check that the rp_realm matches the filter for the GSS name that
94 if ((!tids->rp_gss) ||
95 (!tids->rp_gss->filter)) {
96 tr_notice("tr_tids_req_handler: No GSS name for incoming request.");
97 tids_send_err_response(tids, orig_req, "No GSS name for request");
102 if ((TR_FILTER_NO_MATCH == tr_filter_process_rp_permitted(orig_req->rp_realm,
103 tids->rp_gss->filter,
107 (TR_FILTER_ACTION_REJECT == oaction)) {
108 tr_notice("tr_tids_req_handler: RP realm (%s) does not match RP Realm filter for GSS name", orig_req->rp_realm->buf);
109 tids_send_err_response(tids, orig_req, "RP Realm filter error");
113 /* Check that the rp_realm is a member of the community in the request */
114 if (NULL == (tr_find_comm_rp(cfg_comm, orig_req->rp_realm))) {
115 tr_notice("tr_tids_req_handler: RP Realm (%s) not member of community (%s).", orig_req->rp_realm->buf, orig_req->comm->buf);
116 tids_send_err_response(tids, orig_req, "RP COI membership error");
121 /* Map the comm in the request from a COI to an APC, if needed */
122 if (TR_COMM_COI == cfg_comm->type) {
123 tr_debug("tr_tids_req_handler: Community was a COI, switching.");
124 /* TBD -- In theory there can be more than one? How would that work? */
125 if ((!cfg_comm->apcs) || (!cfg_comm->apcs->id)) {
126 tr_notice("No valid APC for COI %s.", orig_req->comm->buf);
127 tids_send_err_response(tids, orig_req, "No valid APC for community");
131 apc = tr_dup_name(cfg_comm->apcs->id);
133 /* Check that the APC is configured */
134 if (NULL == (cfg_apc = tr_comm_lookup(cfg_mgr->active->comms, apc))) {
135 tr_notice("tr_tids_req_hander: Request for unknown comm: %s.", apc->buf);
136 tids_send_err_response(tids, orig_req, "Unknown APC");
142 fwd_req->orig_coi = orig_req->comm;
144 /* Check that rp_realm is a member of this APC */
145 if (NULL == (tr_find_comm_rp(cfg_apc, orig_req->rp_realm))) {
146 tr_notice("tr_tids_req_hander: RP Realm (%s) not member of community (%s).", orig_req->rp_realm->buf, orig_req->comm->buf);
147 tids_send_err_response(tids, orig_req, "RP APC membership error");
153 /* Look up the route for this community/realm. */
154 route=trps_get_selected_route(trps, orig_req->comm, orig_req->realm);
156 tr_notice("tr_tids_req_handler: no route table entry found for realm (%s) in community (%s).",
157 orig_req->realm->buf, orig_req->comm->buf);
158 tids_send_err_response(tids, orig_req, "Missing trust route error");
162 if (trp_route_is_local(route)) {
163 aaa_servers = tr_idp_aaa_server_lookup(cfg_mgr->active->idp_realms,
167 aaa_servers = tr_aaa_server_new(tmp_ctx, trp_route_get_next_hop(route));
170 /* Find the AAA server(s) for this request */
171 if (NULL == aaa_servers) {
172 tr_debug("tr_tids_req_handler: No AAA Servers for realm %s, defaulting.", orig_req->realm->buf);
173 if (NULL == (aaa_servers = tr_default_server_lookup (cfg_mgr->active->default_servers,
175 tr_notice("tr_tids_req_handler: No default AAA servers, discarded.");
176 tids_send_err_response(tids, orig_req, "No path to AAA Server(s) for realm");
181 /* if we aren't defaulting, check idp coi and apc membership */
182 if (NULL == (tr_find_comm_idp(cfg_comm, fwd_req->realm))) {
183 tr_notice("tr_tids_req_handler: IDP Realm (%s) not member of community (%s).", orig_req->realm->buf, orig_req->comm->buf);
184 tids_send_err_response(tids, orig_req, "IDP community membership error");
188 if ( cfg_apc && (NULL == (tr_find_comm_idp(cfg_apc, fwd_req->realm)))) {
189 tr_notice("tr_tids_req_handler: IDP Realm (%s) not member of APC (%s).", orig_req->realm->buf, orig_req->comm->buf);
190 tids_send_err_response(tids, orig_req, "IDP APC membership error");
196 /* send a TID request to the AAA server(s), and get the answer(s) */
197 /* TBD -- Handle multiple servers */
200 expiration_interval = cfg_apc->expiration_interval;
201 else expiration_interval = cfg_comm->expiration_interval;
202 if (fwd_req->expiration_interval)
203 fwd_req->expiration_interval = (expiration_interval < fwd_req->expiration_interval) ? expiration_interval : fwd_req->expiration_interval;
204 else fwd_req->expiration_interval = expiration_interval;
205 /* Create a TID client instance */
206 if (NULL == (tidc = tidc_create())) {
207 tr_crit("tr_tids_req_hander: Unable to allocate TIDC instance.");
208 tids_send_err_response(tids, orig_req, "Memory allocation failure");
212 /* Use the DH parameters from the original request */
213 /* TBD -- this needs to be fixed when we handle more than one req per conn */
214 tidc->client_dh = orig_req->tidc_dh;
216 /* Save information about this request for the response */
217 resp_cookie.tids = tids;
218 resp_cookie.orig_req = orig_req;
220 /* Set-up TID connection */
221 if (-1 == (fwd_req->conn = tidc_open_connection(tidc,
222 aaa_servers->hostname->buf,
224 &(fwd_req->gssctx)))) {
225 tr_notice("tr_tids_req_handler: Error in tidc_open_connection.");
226 tids_send_err_response(tids, orig_req, "Can't open connection to next hop TIDS");
231 /* Send a TID request */
232 if (0 > (rc = tidc_fwd_request(tidc, fwd_req, &tr_tidc_resp_handler, (void *)&resp_cookie))) {
233 tr_notice("Error from tidc_fwd_request, rc = %d.", rc);
234 tids_send_err_response(tids, orig_req, "Can't forward request to next hop TIDS");
240 talloc_free(tmp_ctx);
244 static int tr_tids_gss_handler(gss_name_t client_name, TR_NAME *gss_name,
248 struct tr_tids_event_cookie *cookie=(struct tr_tids_event_cookie *)data;
249 TIDS_INSTANCE *tids = cookie->tids;
250 TR_CFG_MGR *cfg_mgr = cookie->cfg_mgr;
252 if ((!client_name) || (!gss_name) || (!tids) || (!cfg_mgr)) {
253 tr_debug("tr_tidc_gss_handler: Bad parameters.");
257 /* look up the RP client matching the GSS name */
258 if ((NULL == (rp = tr_rp_client_lookup(cfg_mgr->active->rp_clients, gss_name)))) {
259 tr_debug("tr_tids_gss_handler: Unknown GSS name %s", gss_name->buf);
263 /* Store the rp client */
265 tr_debug("Client's GSS Name: %s", gss_name->buf);
271 /***** TIDS event handling *****/
273 /* called when a connection to the TIDS port is received */
274 static void tr_tids_event_cb(int listener, short event, void *arg)
276 TIDS_INSTANCE *tids = (TIDS_INSTANCE *)arg;
278 if (0==(event & EV_READ))
279 tr_debug("tr_tids_event_cb: unexpected event on TIDS socket (event=0x%X)", event);
281 tids_accept(tids, listener);
284 /* Configure the tids instance and set up its event handler.
285 * Returns 0 on success, nonzero on failure. Fills in
286 * *tids_event (which should be allocated by caller). */
287 int tr_tids_event_init(struct event_base *base,
290 struct tr_socket_event *tids_ev)
292 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
293 struct tr_tids_event_cookie *cookie=NULL;
296 if (tids_ev == NULL) {
297 tr_debug("tr_tids_event_init: Null tids_ev.");
302 /* Create the cookie for callbacks. We'll put it in the tids context, so it will
303 * be cleaned up when tids is freed by talloc_free. */
304 cookie=talloc(tmp_ctx, struct tr_tids_event_cookie);
305 if (cookie == NULL) {
306 tr_debug("tr_tids_event_init: Unable to allocate cookie.");
311 cookie->cfg_mgr=cfg_mgr;
312 talloc_steal(tids, cookie);
314 /* get a tids listener */
315 tids_ev->sock_fd=tids_get_listener(tids,
318 cfg_mgr->active->internal->hostname,
319 cfg_mgr->active->internal->tids_port,
321 if (tids_ev->sock_fd < 0) {
322 tr_crit("Error opening TID server socket.");
328 tids_ev->ev=event_new(base,
333 event_add(tids_ev->ev, NULL);
336 talloc_free(tmp_ctx);