Merge branch 'master' into jennifer/trp-devel
[trust_router.git] / tr / tr_tid.c
1 #include <talloc.h>
2
3 #include <tid_internal.h>
4 #include <tr_filter.h>
5 #include <tr_comm.h>
6 #include <tr_idp.h>
7 #include <tr_rp.h>
8 #include <tr_event.h>
9 #include <tr_debug.h>
10 #include <gsscon.h>
11 #include <trp_internal.h>
12 #include <tr_config.h>
13 #include <tr_tid.h>
14
15 /* Structure to hold TR instance and original request in one cookie */
16 typedef struct tr_resp_cookie {
17   TIDS_INSTANCE *tids;
18   TID_REQ *orig_req;
19 } TR_RESP_COOKIE;
20
21 /* hold a tids instance and a config manager */
22 struct tr_tids_event_cookie {
23   TIDS_INSTANCE *tids;
24   TR_CFG_MGR *cfg_mgr;
25   TRPS_INSTANCE *trps;
26 };
27
28
29 static void tr_tidc_resp_handler (TIDC_INSTANCE *tidc, 
30                                   TID_REQ *req,
31                                   TID_RESP *resp, 
32                                   void *resp_cookie)
33 {
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);
35   req->resp_rcvd = 1;
36
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, 
40                      resp);
41   
42   return;
43 }
44
45 static int tr_tids_req_handler (TIDS_INSTANCE *tids,
46                                 TID_REQ *orig_req, 
47                                 TID_RESP *resp,
48                                 void *cookie_in)
49 {
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;
54   TR_NAME *apc = 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;
59   int rc = 0;
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;
65   int retval=-1;
66
67   if ((!tids) || (!orig_req) || (!resp)) {
68     tr_debug("tr_tids_req_handler: Bad parameters");
69     retval=-1;
70     goto cleanup;
71   }
72
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);
75   tids->req_count++;
76
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.");
80     retval=-1;
81     goto cleanup;
82   }
83
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");
87     retval=-1;
88     goto cleanup;
89   }
90
91   /* Check that the rp_realm matches the filter for the GSS name that 
92    * was received. */
93
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");
98     retval=-1;
99     goto cleanup;
100   }
101
102   if ((TR_FILTER_NO_MATCH == tr_filter_process_rp_permitted(orig_req->rp_realm,
103                                                             tids->rp_gss->filter,
104                                                             orig_req->cons,
105                                                            &fwd_req->cons,
106                                                            &oaction)) ||
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");
110     retval=-1;
111     goto cleanup;
112   }
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");
117     retval=-1;
118     goto cleanup;
119   }
120
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");
128       retval=-1;
129       goto cleanup;
130     }
131     apc = tr_dup_name(cfg_comm->apcs->id);
132
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");
137       retval=-1;
138       goto cleanup;
139     }
140
141     fwd_req->comm = apc;
142     fwd_req->orig_coi = orig_req->comm;
143
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");
148       retval=-1;
149       goto cleanup;
150     }
151   }
152
153   /* Look up the route for this community/realm. */
154   route=trps_get_selected_route(trps, orig_req->comm, orig_req->realm);
155   if (route==NULL) {
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");
159     retval=-1;
160     goto cleanup;
161   }
162   if (trp_route_is_local(route)) {
163     aaa_servers = tr_idp_aaa_server_lookup(cfg_mgr->active->idp_realms, 
164                                            orig_req->realm, 
165                                            orig_req->comm);
166   } else {
167     aaa_servers = tr_aaa_server_new(tmp_ctx, trp_route_get_next_hop(route));
168   }
169
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,
174                                                          orig_req->comm))) {
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");
177       retval=-1;
178       goto cleanup;
179     }
180   } else {
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");
185       retval=-1;
186       goto cleanup;
187     }
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");
191       retval=-1;
192       goto cleanup;
193     }
194   }
195
196   /* send a TID request to the AAA server(s), and get the answer(s) */
197   /* TBD -- Handle multiple servers */
198
199   if (cfg_apc)
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");
209     retval=-1;
210     goto cleanup;
211   }
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;
215
216   /* Save information about this request for the response */
217   resp_cookie.tids = tids;
218   resp_cookie.orig_req = orig_req;
219
220   /* Set-up TID connection */
221   if (-1 == (fwd_req->conn = tidc_open_connection(tidc, 
222                                                   aaa_servers->hostname->buf,
223                                                   TID_PORT,
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");
227     retval=-1;
228     goto cleanup;
229   };
230
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");
235     retval=-1;
236     goto cleanup;
237   }
238     
239 cleanup:
240   talloc_free(tmp_ctx);
241   return retval;
242 }
243
244 static int tr_tids_gss_handler(gss_name_t client_name, TR_NAME *gss_name,
245                                void *data)
246 {
247   TR_RP_CLIENT *rp;
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;
251
252   if ((!client_name) || (!gss_name) || (!tids) || (!cfg_mgr)) {
253     tr_debug("tr_tidc_gss_handler: Bad parameters.");
254     return -1;
255   }
256
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);
260     return -1;
261   }
262
263   /* Store the rp client */
264   tids->rp_gss = rp;
265   tr_debug("Client's GSS Name: %s", gss_name->buf);
266
267   return 0;
268 }
269
270
271 /***** TIDS event handling *****/
272
273 /* called when a connection to the TIDS port is received */
274 static void tr_tids_event_cb(int listener, short event, void *arg)
275 {
276   TIDS_INSTANCE *tids = (TIDS_INSTANCE *)arg;
277
278   if (0==(event & EV_READ))
279     tr_debug("tr_tids_event_cb: unexpected event on TIDS socket (event=0x%X)", event);
280   else 
281     tids_accept(tids, listener);
282 }
283
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,
288                        TIDS_INSTANCE *tids,
289                        TR_CFG_MGR *cfg_mgr,
290                        struct tr_socket_event *tids_ev)
291 {
292   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
293   struct tr_tids_event_cookie *cookie=NULL;
294   int retval=0;
295
296   if (tids_ev == NULL) {
297     tr_debug("tr_tids_event_init: Null tids_ev.");
298     retval=1;
299     goto cleanup;
300   }
301
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.");
307     retval=1;
308     goto cleanup;
309   }
310   cookie->tids=tids;
311   cookie->cfg_mgr=cfg_mgr;
312   talloc_steal(tids, cookie);
313
314   /* get a tids listener */
315   tids_ev->sock_fd=tids_get_listener(tids,
316                                      tr_tids_req_handler,
317                                      tr_tids_gss_handler,
318                                      cfg_mgr->active->internal->hostname,
319                                      cfg_mgr->active->internal->tids_port,
320                                      (void *)cookie);
321   if (tids_ev->sock_fd < 0) {
322     tr_crit("Error opening TID server socket.");
323     retval=1;
324     goto cleanup;
325   }
326
327   /* and its event */
328   tids_ev->ev=event_new(base,
329                         tids_ev->sock_fd,
330                         EV_READ|EV_PERSIST,
331                         tr_tids_event_cb,
332                         (void *)tids);
333   event_add(tids_ev->ev, NULL);
334
335 cleanup:
336   talloc_free(tmp_ctx);
337   return retval;
338 }