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=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;
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   tr_debug("tr_tids_req_handler: looking up route.");
155   route=trps_get_selected_route(trps, orig_req->comm, orig_req->realm);
156   if (route==NULL) {
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");
160     retval=-1;
161     goto cleanup;
162   }
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, 
167                                            orig_req->realm, 
168                                            orig_req->comm);
169   } else {
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));
172   }
173
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,
178                                                          orig_req->comm))) {
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");
181       retval=-1;
182       goto cleanup;
183     }
184   } else {
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");
189       retval=-1;
190       goto cleanup;
191     }
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");
195       retval=-1;
196       goto cleanup;
197     }
198   }
199
200   /* send a TID request to the AAA server(s), and get the answer(s) */
201   /* TBD -- Handle multiple servers */
202
203   if (cfg_apc)
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");
213     retval=-1;
214     goto cleanup;
215   }
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;
219
220   /* Save information about this request for the response */
221   resp_cookie.tids = tids;
222   resp_cookie.orig_req = orig_req;
223
224   /* Set-up TID connection */
225   if (-1 == (fwd_req->conn = tidc_open_connection(tidc, 
226                                                   aaa_servers->hostname->buf,
227                                                   TID_PORT,
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");
231     retval=-1;
232     goto cleanup;
233   };
234
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");
239     retval=-1;
240     goto cleanup;
241   }
242
243   /* success! */
244   retval=0;
245     
246 cleanup:
247   talloc_free(tmp_ctx);
248   return retval;
249 }
250
251 static int tr_tids_gss_handler(gss_name_t client_name, TR_NAME *gss_name,
252                                void *data)
253 {
254   TR_RP_CLIENT *rp;
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;
258
259   if ((!client_name) || (!gss_name) || (!tids) || (!cfg_mgr)) {
260     tr_debug("tr_tidc_gss_handler: Bad parameters.");
261     return -1;
262   }
263
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);
267     return -1;
268   }
269
270   /* Store the rp client */
271   tids->rp_gss = rp;
272   tr_debug("Client's GSS Name: %s", gss_name->buf);
273
274   return 0;
275 }
276
277
278 /***** TIDS event handling *****/
279
280 /* called when a connection to the TIDS port is received */
281 static void tr_tids_event_cb(int listener, short event, void *arg)
282 {
283   TIDS_INSTANCE *tids = (TIDS_INSTANCE *)arg;
284
285   if (0==(event & EV_READ))
286     tr_debug("tr_tids_event_cb: unexpected event on TIDS socket (event=0x%X)", event);
287   else 
288     tids_accept(tids, listener);
289 }
290
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,
295                        TIDS_INSTANCE *tids,
296                        TR_CFG_MGR *cfg_mgr,
297                        TRPS_INSTANCE *trps,
298                        struct tr_socket_event *tids_ev)
299 {
300   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
301   struct tr_tids_event_cookie *cookie=NULL;
302   int retval=0;
303
304   if (tids_ev == NULL) {
305     tr_debug("tr_tids_event_init: Null tids_ev.");
306     retval=1;
307     goto cleanup;
308   }
309
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.");
315     retval=1;
316     goto cleanup;
317   }
318   cookie->tids=tids;
319   cookie->cfg_mgr=cfg_mgr;
320   cookie->trps=trps;
321   talloc_steal(tids, cookie);
322
323   /* get a tids listener */
324   tids_ev->sock_fd=tids_get_listener(tids,
325                                      tr_tids_req_handler,
326                                      tr_tids_gss_handler,
327                                      cfg_mgr->active->internal->hostname,
328                                      cfg_mgr->active->internal->tids_port,
329                                      (void *)cookie);
330   if (tids_ev->sock_fd < 0) {
331     tr_crit("Error opening TID server socket.");
332     retval=1;
333     goto cleanup;
334   }
335
336   /* and its event */
337   tids_ev->ev=event_new(base,
338                         tids_ev->sock_fd,
339                         EV_READ|EV_PERSIST,
340                         tr_tids_event_cb,
341                         (void *)tids);
342   event_add(tids_ev->ev, NULL);
343
344 cleanup:
345   talloc_free(tmp_ctx);
346   return retval;
347 }