Enforce single COI->APC mapping.
[trust_router.git] / tr / tr_tid.c
1 /*
2  * Copyright (c) 2016, JANET(UK)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * 3. Neither the name of JANET(UK) nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31  * OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  */
34
35 #include <talloc.h>
36
37 #include <tid_internal.h>
38 #include <tr_filter.h>
39 #include <tr_comm.h>
40 #include <tr_idp.h>
41 #include <tr_rp.h>
42 #include <tr_event.h>
43 #include <tr_debug.h>
44 #include <gsscon.h>
45 #include <trp_internal.h>
46 #include <tr_config.h>
47 #include <tr_tid.h>
48
49 /* Structure to hold TR instance and original request in one cookie */
50 typedef struct tr_resp_cookie {
51   TIDS_INSTANCE *tids;
52   TID_REQ *orig_req;
53 } TR_RESP_COOKIE;
54
55 /* hold a tids instance and a config manager */
56 struct tr_tids_event_cookie {
57   TIDS_INSTANCE *tids;
58   TR_CFG_MGR *cfg_mgr;
59   TRPS_INSTANCE *trps;
60 };
61
62
63 static void tr_tidc_resp_handler (TIDC_INSTANCE *tidc, 
64                                   TID_REQ *req,
65                                   TID_RESP *resp, 
66                                   void *resp_cookie)
67 {
68   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);
69   req->resp_rcvd = 1;
70
71   /* TBD -- handle concatentation of multiple responses to single req */
72   tids_send_response(((TR_RESP_COOKIE *)resp_cookie)->tids, 
73                      ((TR_RESP_COOKIE *)resp_cookie)->orig_req, 
74                      resp);
75   
76   return;
77 }
78
79 static int tr_tids_req_handler (TIDS_INSTANCE *tids,
80                                 TID_REQ *orig_req, 
81                                 TID_RESP *resp,
82                                 void *cookie_in)
83 {
84   TALLOC_CTX *tmp_ctx=talloc_new(NULL);;
85   TIDC_INSTANCE *tidc = NULL;
86   TR_RESP_COOKIE resp_cookie;
87   TR_AAA_SERVER *aaa_servers = NULL;
88   TR_NAME *apc = NULL;
89   TID_REQ *fwd_req = NULL;
90   TR_COMM *cfg_comm = NULL;
91   TR_COMM *cfg_apc = NULL;
92   int oaction = TR_FILTER_ACTION_REJECT;
93   int rc = 0;
94   time_t expiration_interval=0;
95   struct tr_tids_event_cookie *cookie=talloc_get_type_abort(cookie_in, struct tr_tids_event_cookie);
96   TR_CFG_MGR *cfg_mgr=cookie->cfg_mgr;
97   TRPS_INSTANCE *trps=cookie->trps;
98   TRP_ROUTE *route=NULL;
99   int retval=-1;
100
101   if ((!tids) || (!orig_req) || (!resp)) {
102     tr_debug("tr_tids_req_handler: Bad parameters");
103     retval=-1;
104     goto cleanup;
105   }
106
107   tr_debug("tr_tids_req_handler: Request received (conn = %d)! Realm = %s, Comm = %s", orig_req->conn, 
108            orig_req->realm->buf, orig_req->comm->buf);
109   tids->req_count++;
110
111   /* Duplicate the request, so we can modify and forward it */
112   if (NULL == (fwd_req = tid_dup_req(orig_req))) {
113     tr_debug("tr_tids_req_handler: Unable to duplicate request.");
114     retval=-1;
115     goto cleanup;
116   }
117
118   if (NULL == (cfg_comm = tr_comm_table_find_comm(cfg_mgr->active->ctable, orig_req->comm))) {
119     tr_notice("tr_tids_req_hander: Request for unknown comm: %s.", orig_req->comm->buf);
120     tids_send_err_response(tids, orig_req, "Unknown community");
121     retval=-1;
122     goto cleanup;
123   }
124
125   /* Check that the rp_realm matches the filter for the GSS name that 
126    * was received. */
127
128   if ((!tids->rp_gss) || 
129       (!tids->rp_gss->filter)) {
130     tr_notice("tr_tids_req_handler: No GSS name for incoming request.");
131     tids_send_err_response(tids, orig_req, "No GSS name for request");
132     retval=-1;
133     goto cleanup;
134   }
135
136   if ((TR_FILTER_NO_MATCH == tr_filter_process_rp_permitted(orig_req->rp_realm,
137                                                             tids->rp_gss->filter,
138                                                             orig_req->cons,
139                                                            &fwd_req->cons,
140                                                            &oaction)) ||
141       (TR_FILTER_ACTION_REJECT == oaction)) {
142     tr_notice("tr_tids_req_handler: RP realm (%s) does not match RP Realm filter for GSS name", orig_req->rp_realm->buf);
143     tids_send_err_response(tids, orig_req, "RP Realm filter error");
144     retval=-1;
145     goto cleanup;
146   }
147   /* Check that the rp_realm is a member of the community in the request */
148   if (NULL == (tr_comm_find_rp(cfg_mgr->active->ctable, cfg_comm, orig_req->rp_realm))) {
149     tr_notice("tr_tids_req_handler: RP Realm (%s) not member of community (%s).", orig_req->rp_realm->buf, orig_req->comm->buf);
150     tids_send_err_response(tids, orig_req, "RP COI membership error");
151     retval=-1;
152     goto cleanup;
153   }
154
155   /* Map the comm in the request from a COI to an APC, if needed */
156   if (TR_COMM_COI == cfg_comm->type) {
157     if (orig_req->orig_coi!=NULL) {
158       tr_notice("tr_tids_req_handler: community %s is COI but COI to APC mapping already occurred. Dropping request.",
159                orig_req->comm->buf);
160       tids_send_err_response(tids, orig_req, "Second COI to APC mapping would result, permitted only once.");
161       retval=-1;
162       goto cleanup;
163     }
164     tr_debug("tr_tids_req_handler: Community was a COI, switching.");
165     /* TBD -- In theory there can be more than one?  How would that work? */
166     if ((!cfg_comm->apcs) || (!cfg_comm->apcs->id)) {
167       tr_notice("No valid APC for COI %s.", orig_req->comm->buf);
168       tids_send_err_response(tids, orig_req, "No valid APC for community");
169       retval=-1;
170       goto cleanup;
171     }
172     apc = tr_dup_name(cfg_comm->apcs->id);
173
174     /* Check that the APC is configured */
175     if (NULL == (cfg_apc = tr_comm_table_find_comm(cfg_mgr->active->ctable, apc))) {
176       tr_notice("tr_tids_req_hander: Request for unknown comm: %s.", apc->buf);
177       tids_send_err_response(tids, orig_req, "Unknown APC");
178       retval=-1;
179       goto cleanup;
180     }
181
182     fwd_req->comm = apc;
183     fwd_req->orig_coi = orig_req->comm;
184
185     /* Check that rp_realm is a  member of this APC */
186     if (NULL == (tr_comm_find_rp(cfg_mgr->active->ctable, cfg_apc, orig_req->rp_realm))) {
187       tr_notice("tr_tids_req_hander: RP Realm (%s) not member of community (%s).", orig_req->rp_realm->buf, orig_req->comm->buf);
188       tids_send_err_response(tids, orig_req, "RP APC membership error");
189       retval=-1;
190       goto cleanup;
191     }
192   }
193
194   /* Look up the route for this community/realm. */
195   tr_debug("tr_tids_req_handler: looking up route.");
196   route=trps_get_selected_route(trps, orig_req->comm, orig_req->realm);
197   if (route==NULL) {
198     tr_notice("tr_tids_req_handler: no route table entry found for realm (%s) in community (%s).",
199               orig_req->realm->buf, orig_req->comm->buf);
200     tids_send_err_response(tids, orig_req, "Missing trust route error");
201     retval=-1;
202     goto cleanup;
203   }
204   tr_debug("tr_tids_req_handler: found route.");
205   if (trp_route_is_local(route)) {
206     tr_debug("tr_tids_req_handler: route is local.");
207     aaa_servers = tr_idp_aaa_server_lookup(cfg_mgr->active->ctable->idp_realms, 
208                                            orig_req->realm, 
209                                            orig_req->comm);
210   } else {
211     tr_debug("tr_tids_req_handler: route not local.");
212     aaa_servers = tr_aaa_server_new(tmp_ctx, trp_route_get_next_hop(route));
213   }
214
215   /* Find the AAA server(s) for this request */
216   if (NULL == aaa_servers) {
217     tr_debug("tr_tids_req_handler: No AAA Servers for realm %s, defaulting.", orig_req->realm->buf);
218     if (NULL == (aaa_servers = tr_default_server_lookup (cfg_mgr->active->default_servers,
219                                                          orig_req->comm))) {
220       tr_notice("tr_tids_req_handler: No default AAA servers, discarded.");
221       tids_send_err_response(tids, orig_req, "No path to AAA Server(s) for realm");
222       retval=-1;
223       goto cleanup;
224     }
225   } else {
226     /* if we aren't defaulting, check idp coi and apc membership */
227     if (NULL == (tr_comm_find_idp(cfg_mgr->active->ctable, cfg_comm, fwd_req->realm))) {
228       tr_notice("tr_tids_req_handler: IDP Realm (%s) not member of community (%s).", orig_req->realm->buf, orig_req->comm->buf);
229       tids_send_err_response(tids, orig_req, "IDP community membership error");
230       retval=-1;
231       goto cleanup;
232     }
233     if ( cfg_apc && (NULL == (tr_comm_find_idp(cfg_mgr->active->ctable, cfg_apc, fwd_req->realm)))) {
234       tr_notice("tr_tids_req_handler: IDP Realm (%s) not member of APC (%s).", orig_req->realm->buf, orig_req->comm->buf);
235       tids_send_err_response(tids, orig_req, "IDP APC membership error");
236       retval=-1;
237       goto cleanup;
238     }
239   }
240
241   /* send a TID request to the AAA server(s), and get the answer(s) */
242   /* TBD -- Handle multiple servers */
243
244   if (cfg_apc)
245     expiration_interval = cfg_apc->expiration_interval;
246   else expiration_interval = cfg_comm->expiration_interval;
247   if (fwd_req->expiration_interval)
248     fwd_req->expiration_interval =  (expiration_interval < fwd_req->expiration_interval) ? expiration_interval : fwd_req->expiration_interval;
249   else fwd_req->expiration_interval = expiration_interval;
250   /* Create a TID client instance */
251   if (NULL == (tidc = tidc_create())) {
252     tr_crit("tr_tids_req_hander: Unable to allocate TIDC instance.");
253     tids_send_err_response(tids, orig_req, "Memory allocation failure");
254     retval=-1;
255     goto cleanup;
256   }
257   /* Use the DH parameters from the original request */
258   /* TBD -- this needs to be fixed when we handle more than one req per conn */
259   tidc->client_dh = orig_req->tidc_dh;
260
261   /* Save information about this request for the response */
262   resp_cookie.tids = tids;
263   resp_cookie.orig_req = orig_req;
264
265   /* Set-up TID connection */
266   if (-1 == (fwd_req->conn = tidc_open_connection(tidc, 
267                                                   aaa_servers->hostname->buf,
268                                                   TID_PORT,
269                                                  &(fwd_req->gssctx)))) {
270     tr_notice("tr_tids_req_handler: Error in tidc_open_connection.");
271     tids_send_err_response(tids, orig_req, "Can't open connection to next hop TIDS");
272     retval=-1;
273     goto cleanup;
274   };
275
276   /* Send a TID request */
277   if (0 > (rc = tidc_fwd_request(tidc, fwd_req, &tr_tidc_resp_handler, (void *)&resp_cookie))) {
278     tr_notice("Error from tidc_fwd_request, rc = %d.", rc);
279     tids_send_err_response(tids, orig_req, "Can't forward request to next hop TIDS");
280     retval=-1;
281     goto cleanup;
282   }
283
284   /* success! */
285   retval=0;
286     
287 cleanup:
288   talloc_free(tmp_ctx);
289   return retval;
290 }
291
292 static int tr_tids_gss_handler(gss_name_t client_name, TR_NAME *gss_name,
293                                void *data)
294 {
295   TR_RP_CLIENT *rp;
296   struct tr_tids_event_cookie *cookie=talloc_get_type_abort(data, struct tr_tids_event_cookie);
297   TIDS_INSTANCE *tids = cookie->tids;
298   TR_CFG_MGR *cfg_mgr = cookie->cfg_mgr;
299
300   if ((!client_name) || (!gss_name) || (!tids) || (!cfg_mgr)) {
301     tr_debug("tr_tidc_gss_handler: Bad parameters.");
302     return -1;
303   }
304
305   /* look up the RP client matching the GSS name */
306   if ((NULL == (rp = tr_rp_client_lookup(cfg_mgr->active->rp_clients, gss_name)))) {
307     tr_debug("tr_tids_gss_handler: Unknown GSS name %s", gss_name->buf);
308     return -1;
309   }
310
311   /* Store the rp client */
312   tids->rp_gss = rp;
313   tr_debug("Client's GSS Name: %s", gss_name->buf);
314
315   return 0;
316 }
317
318
319 /***** TIDS event handling *****/
320
321 /* called when a connection to the TIDS port is received */
322 static void tr_tids_event_cb(int listener, short event, void *arg)
323 {
324   TIDS_INSTANCE *tids = (TIDS_INSTANCE *)arg;
325
326   if (0==(event & EV_READ))
327     tr_debug("tr_tids_event_cb: unexpected event on TIDS socket (event=0x%X)", event);
328   else 
329     tids_accept(tids, listener);
330 }
331
332 /* Configure the tids instance and set up its event handler.
333  * Returns 0 on success, nonzero on failure. Fills in
334  * *tids_event (which should be allocated by caller). */
335 int tr_tids_event_init(struct event_base *base,
336                        TIDS_INSTANCE *tids,
337                        TR_CFG_MGR *cfg_mgr,
338                        TRPS_INSTANCE *trps,
339                        struct tr_socket_event *tids_ev)
340 {
341   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
342   struct tr_tids_event_cookie *cookie=NULL;
343   int retval=0;
344
345   if (tids_ev == NULL) {
346     tr_debug("tr_tids_event_init: Null tids_ev.");
347     retval=1;
348     goto cleanup;
349   }
350
351   /* Create the cookie for callbacks. We'll put it in the tids context, so it will
352    * be cleaned up when tids is freed by talloc_free. */
353   cookie=talloc(tmp_ctx, struct tr_tids_event_cookie);
354   if (cookie == NULL) {
355     tr_debug("tr_tids_event_init: Unable to allocate cookie.");
356     retval=1;
357     goto cleanup;
358   }
359   cookie->tids=tids;
360   cookie->cfg_mgr=cfg_mgr;
361   cookie->trps=trps;
362   talloc_steal(tids, cookie);
363
364   /* get a tids listener */
365   tids_ev->sock_fd=tids_get_listener(tids,
366                                      tr_tids_req_handler,
367                                      tr_tids_gss_handler,
368                                      cfg_mgr->active->internal->hostname,
369                                      cfg_mgr->active->internal->tids_port,
370                                      (void *)cookie);
371   if (tids_ev->sock_fd < 0) {
372     tr_crit("Error opening TID server socket.");
373     retval=1;
374     goto cleanup;
375   }
376
377   /* and its event */
378   tids_ev->ev=event_new(base,
379                         tids_ev->sock_fd,
380                         EV_READ|EV_PERSIST,
381                         tr_tids_event_cb,
382                         (void *)tids);
383   event_add(tids_ev->ev, NULL);
384
385 cleanup:
386   talloc_free(tmp_ctx);
387   return retval;
388 }