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=talloc_get_type_abort(cookie_in, struct tr_tids_event_cookie);
62 TR_CFG_MGR *cfg_mgr=cookie->cfg_mgr;
63 TRPS_INSTANCE *trps=cookie->trps;
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 tr_debug("tr_tids_req_handler: looking up route.");
155 route=trps_get_selected_route(trps, orig_req->comm, orig_req->realm);
157 tr_notice("tr_tids_req_handler: no route table entry found for realm (%s) in community (%s).",
158 orig_req->realm->buf, orig_req->comm->buf);
159 tids_send_err_response(tids, orig_req, "Missing trust route error");
163 tr_debug("tr_tids_req_handler: found route.");
164 if (trp_route_is_local(route)) {
165 tr_debug("tr_tids_req_handler: route is local.");
166 aaa_servers = tr_idp_aaa_server_lookup(cfg_mgr->active->idp_realms,
170 tr_debug("tr_tids_req_handler: route not local.");
171 aaa_servers = tr_aaa_server_new(tmp_ctx, trp_route_get_next_hop(route));
174 /* Find the AAA server(s) for this request */
175 if (NULL == aaa_servers) {
176 tr_debug("tr_tids_req_handler: No AAA Servers for realm %s, defaulting.", orig_req->realm->buf);
177 if (NULL == (aaa_servers = tr_default_server_lookup (cfg_mgr->active->default_servers,
179 tr_notice("tr_tids_req_handler: No default AAA servers, discarded.");
180 tids_send_err_response(tids, orig_req, "No path to AAA Server(s) for realm");
185 /* if we aren't defaulting, check idp coi and apc membership */
186 if (NULL == (tr_find_comm_idp(cfg_comm, fwd_req->realm))) {
187 tr_notice("tr_tids_req_handler: IDP Realm (%s) not member of community (%s).", orig_req->realm->buf, orig_req->comm->buf);
188 tids_send_err_response(tids, orig_req, "IDP community membership error");
192 if ( cfg_apc && (NULL == (tr_find_comm_idp(cfg_apc, fwd_req->realm)))) {
193 tr_notice("tr_tids_req_handler: IDP Realm (%s) not member of APC (%s).", orig_req->realm->buf, orig_req->comm->buf);
194 tids_send_err_response(tids, orig_req, "IDP APC membership error");
200 /* send a TID request to the AAA server(s), and get the answer(s) */
201 /* TBD -- Handle multiple servers */
204 expiration_interval = cfg_apc->expiration_interval;
205 else expiration_interval = cfg_comm->expiration_interval;
206 if (fwd_req->expiration_interval)
207 fwd_req->expiration_interval = (expiration_interval < fwd_req->expiration_interval) ? expiration_interval : fwd_req->expiration_interval;
208 else fwd_req->expiration_interval = expiration_interval;
209 /* Create a TID client instance */
210 if (NULL == (tidc = tidc_create())) {
211 tr_crit("tr_tids_req_hander: Unable to allocate TIDC instance.");
212 tids_send_err_response(tids, orig_req, "Memory allocation failure");
216 /* Use the DH parameters from the original request */
217 /* TBD -- this needs to be fixed when we handle more than one req per conn */
218 tidc->client_dh = orig_req->tidc_dh;
220 /* Save information about this request for the response */
221 resp_cookie.tids = tids;
222 resp_cookie.orig_req = orig_req;
224 /* Set-up TID connection */
225 if (-1 == (fwd_req->conn = tidc_open_connection(tidc,
226 aaa_servers->hostname->buf,
228 &(fwd_req->gssctx)))) {
229 tr_notice("tr_tids_req_handler: Error in tidc_open_connection.");
230 tids_send_err_response(tids, orig_req, "Can't open connection to next hop TIDS");
235 /* Send a TID request */
236 if (0 > (rc = tidc_fwd_request(tidc, fwd_req, &tr_tidc_resp_handler, (void *)&resp_cookie))) {
237 tr_notice("Error from tidc_fwd_request, rc = %d.", rc);
238 tids_send_err_response(tids, orig_req, "Can't forward request to next hop TIDS");
247 talloc_free(tmp_ctx);
251 static int tr_tids_gss_handler(gss_name_t client_name, TR_NAME *gss_name,
255 struct tr_tids_event_cookie *cookie=talloc_get_type_abort(data, struct tr_tids_event_cookie);
256 TIDS_INSTANCE *tids = cookie->tids;
257 TR_CFG_MGR *cfg_mgr = cookie->cfg_mgr;
259 if ((!client_name) || (!gss_name) || (!tids) || (!cfg_mgr)) {
260 tr_debug("tr_tidc_gss_handler: Bad parameters.");
264 /* look up the RP client matching the GSS name */
265 if ((NULL == (rp = tr_rp_client_lookup(cfg_mgr->active->rp_clients, gss_name)))) {
266 tr_debug("tr_tids_gss_handler: Unknown GSS name %s", gss_name->buf);
270 /* Store the rp client */
272 tr_debug("Client's GSS Name: %s", gss_name->buf);
278 /***** TIDS event handling *****/
280 /* called when a connection to the TIDS port is received */
281 static void tr_tids_event_cb(int listener, short event, void *arg)
283 TIDS_INSTANCE *tids = (TIDS_INSTANCE *)arg;
285 if (0==(event & EV_READ))
286 tr_debug("tr_tids_event_cb: unexpected event on TIDS socket (event=0x%X)", event);
288 tids_accept(tids, listener);
291 /* Configure the tids instance and set up its event handler.
292 * Returns 0 on success, nonzero on failure. Fills in
293 * *tids_event (which should be allocated by caller). */
294 int tr_tids_event_init(struct event_base *base,
298 struct tr_socket_event *tids_ev)
300 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
301 struct tr_tids_event_cookie *cookie=NULL;
304 if (tids_ev == NULL) {
305 tr_debug("tr_tids_event_init: Null tids_ev.");
310 /* Create the cookie for callbacks. We'll put it in the tids context, so it will
311 * be cleaned up when tids is freed by talloc_free. */
312 cookie=talloc(tmp_ctx, struct tr_tids_event_cookie);
313 if (cookie == NULL) {
314 tr_debug("tr_tids_event_init: Unable to allocate cookie.");
319 cookie->cfg_mgr=cfg_mgr;
321 talloc_steal(tids, cookie);
323 /* get a tids listener */
324 tids_ev->sock_fd=tids_get_listener(tids,
327 cfg_mgr->active->internal->hostname,
328 cfg_mgr->active->internal->tids_port,
330 if (tids_ev->sock_fd < 0) {
331 tr_crit("Error opening TID server socket.");
337 tids_ev->ev=event_new(base,
342 event_add(tids_ev->ev, NULL);
345 talloc_free(tmp_ctx);