Handle community updates when received. Builds, not tested.
[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     tr_debug("tr_tids_req_handler: Community was a COI, switching.");
158     /* TBD -- In theory there can be more than one?  How would that work? */
159     if ((!cfg_comm->apcs) || (!cfg_comm->apcs->id)) {
160       tr_notice("No valid APC for COI %s.", orig_req->comm->buf);
161       tids_send_err_response(tids, orig_req, "No valid APC for community");
162       retval=-1;
163       goto cleanup;
164     }
165     apc = tr_dup_name(cfg_comm->apcs->id);
166
167     /* Check that the APC is configured */
168     if (NULL == (cfg_apc = tr_comm_table_find_comm(cfg_mgr->active->ctable, apc))) {
169       tr_notice("tr_tids_req_hander: Request for unknown comm: %s.", apc->buf);
170       tids_send_err_response(tids, orig_req, "Unknown APC");
171       retval=-1;
172       goto cleanup;
173     }
174
175     fwd_req->comm = apc;
176     fwd_req->orig_coi = orig_req->comm;
177
178     /* Check that rp_realm is a  member of this APC */
179     if (NULL == (tr_comm_find_rp(cfg_mgr->active->ctable, cfg_apc, orig_req->rp_realm))) {
180       tr_notice("tr_tids_req_hander: RP Realm (%s) not member of community (%s).", orig_req->rp_realm->buf, orig_req->comm->buf);
181       tids_send_err_response(tids, orig_req, "RP APC membership error");
182       retval=-1;
183       goto cleanup;
184     }
185   }
186
187   /* Look up the route for this community/realm. */
188   tr_debug("tr_tids_req_handler: looking up route.");
189   route=trps_get_selected_route(trps, orig_req->comm, orig_req->realm);
190   if (route==NULL) {
191     tr_notice("tr_tids_req_handler: no route table entry found for realm (%s) in community (%s).",
192               orig_req->realm->buf, orig_req->comm->buf);
193     tids_send_err_response(tids, orig_req, "Missing trust route error");
194     retval=-1;
195     goto cleanup;
196   }
197   tr_debug("tr_tids_req_handler: found route.");
198   if (trp_route_is_local(route)) {
199   tr_debug("tr_tids_req_handler: route is local.");
200     aaa_servers = tr_idp_aaa_server_lookup(cfg_mgr->active->ctable->idp_realms, 
201                                            orig_req->realm, 
202                                            orig_req->comm);
203   } else {
204     tr_debug("tr_tids_req_handler: route not local.");
205     aaa_servers = tr_aaa_server_new(tmp_ctx, trp_route_get_next_hop(route));
206   }
207
208   /* Find the AAA server(s) for this request */
209   if (NULL == aaa_servers) {
210     tr_debug("tr_tids_req_handler: No AAA Servers for realm %s, defaulting.", orig_req->realm->buf);
211     if (NULL == (aaa_servers = tr_default_server_lookup (cfg_mgr->active->default_servers,
212                                                          orig_req->comm))) {
213       tr_notice("tr_tids_req_handler: No default AAA servers, discarded.");
214       tids_send_err_response(tids, orig_req, "No path to AAA Server(s) for realm");
215       retval=-1;
216       goto cleanup;
217     }
218   } else {
219     /* if we aren't defaulting, check idp coi and apc membership */
220     if (NULL == (tr_comm_find_idp(cfg_mgr->active->ctable, cfg_comm, fwd_req->realm))) {
221       tr_notice("tr_tids_req_handler: IDP Realm (%s) not member of community (%s).", orig_req->realm->buf, orig_req->comm->buf);
222       tids_send_err_response(tids, orig_req, "IDP community membership error");
223       retval=-1;
224       goto cleanup;
225     }
226     if ( cfg_apc && (NULL == (tr_comm_find_idp(cfg_mgr->active->ctable, cfg_apc, fwd_req->realm)))) {
227       tr_notice("tr_tids_req_handler: IDP Realm (%s) not member of APC (%s).", orig_req->realm->buf, orig_req->comm->buf);
228       tids_send_err_response(tids, orig_req, "IDP APC membership error");
229       retval=-1;
230       goto cleanup;
231     }
232   }
233
234   /* send a TID request to the AAA server(s), and get the answer(s) */
235   /* TBD -- Handle multiple servers */
236
237   if (cfg_apc)
238     expiration_interval = cfg_apc->expiration_interval;
239   else expiration_interval = cfg_comm->expiration_interval;
240   if (fwd_req->expiration_interval)
241     fwd_req->expiration_interval =  (expiration_interval < fwd_req->expiration_interval) ? expiration_interval : fwd_req->expiration_interval;
242   else fwd_req->expiration_interval = expiration_interval;
243   /* Create a TID client instance */
244   if (NULL == (tidc = tidc_create())) {
245     tr_crit("tr_tids_req_hander: Unable to allocate TIDC instance.");
246     tids_send_err_response(tids, orig_req, "Memory allocation failure");
247     retval=-1;
248     goto cleanup;
249   }
250   /* Use the DH parameters from the original request */
251   /* TBD -- this needs to be fixed when we handle more than one req per conn */
252   tidc->client_dh = orig_req->tidc_dh;
253
254   /* Save information about this request for the response */
255   resp_cookie.tids = tids;
256   resp_cookie.orig_req = orig_req;
257
258   /* Set-up TID connection */
259   if (-1 == (fwd_req->conn = tidc_open_connection(tidc, 
260                                                   aaa_servers->hostname->buf,
261                                                   TID_PORT,
262                                                  &(fwd_req->gssctx)))) {
263     tr_notice("tr_tids_req_handler: Error in tidc_open_connection.");
264     tids_send_err_response(tids, orig_req, "Can't open connection to next hop TIDS");
265     retval=-1;
266     goto cleanup;
267   };
268
269   /* Send a TID request */
270   if (0 > (rc = tidc_fwd_request(tidc, fwd_req, &tr_tidc_resp_handler, (void *)&resp_cookie))) {
271     tr_notice("Error from tidc_fwd_request, rc = %d.", rc);
272     tids_send_err_response(tids, orig_req, "Can't forward request to next hop TIDS");
273     retval=-1;
274     goto cleanup;
275   }
276
277   /* success! */
278   retval=0;
279     
280 cleanup:
281   talloc_free(tmp_ctx);
282   return retval;
283 }
284
285 static int tr_tids_gss_handler(gss_name_t client_name, TR_NAME *gss_name,
286                                void *data)
287 {
288   TR_RP_CLIENT *rp;
289   struct tr_tids_event_cookie *cookie=talloc_get_type_abort(data, struct tr_tids_event_cookie);
290   TIDS_INSTANCE *tids = cookie->tids;
291   TR_CFG_MGR *cfg_mgr = cookie->cfg_mgr;
292
293   if ((!client_name) || (!gss_name) || (!tids) || (!cfg_mgr)) {
294     tr_debug("tr_tidc_gss_handler: Bad parameters.");
295     return -1;
296   }
297
298   /* look up the RP client matching the GSS name */
299   if ((NULL == (rp = tr_rp_client_lookup(cfg_mgr->active->rp_clients, gss_name)))) {
300     tr_debug("tr_tids_gss_handler: Unknown GSS name %s", gss_name->buf);
301     return -1;
302   }
303
304   /* Store the rp client */
305   tids->rp_gss = rp;
306   tr_debug("Client's GSS Name: %s", gss_name->buf);
307
308   return 0;
309 }
310
311
312 /***** TIDS event handling *****/
313
314 /* called when a connection to the TIDS port is received */
315 static void tr_tids_event_cb(int listener, short event, void *arg)
316 {
317   TIDS_INSTANCE *tids = (TIDS_INSTANCE *)arg;
318
319   if (0==(event & EV_READ))
320     tr_debug("tr_tids_event_cb: unexpected event on TIDS socket (event=0x%X)", event);
321   else 
322     tids_accept(tids, listener);
323 }
324
325 /* Configure the tids instance and set up its event handler.
326  * Returns 0 on success, nonzero on failure. Fills in
327  * *tids_event (which should be allocated by caller). */
328 int tr_tids_event_init(struct event_base *base,
329                        TIDS_INSTANCE *tids,
330                        TR_CFG_MGR *cfg_mgr,
331                        TRPS_INSTANCE *trps,
332                        struct tr_socket_event *tids_ev)
333 {
334   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
335   struct tr_tids_event_cookie *cookie=NULL;
336   int retval=0;
337
338   if (tids_ev == NULL) {
339     tr_debug("tr_tids_event_init: Null tids_ev.");
340     retval=1;
341     goto cleanup;
342   }
343
344   /* Create the cookie for callbacks. We'll put it in the tids context, so it will
345    * be cleaned up when tids is freed by talloc_free. */
346   cookie=talloc(tmp_ctx, struct tr_tids_event_cookie);
347   if (cookie == NULL) {
348     tr_debug("tr_tids_event_init: Unable to allocate cookie.");
349     retval=1;
350     goto cleanup;
351   }
352   cookie->tids=tids;
353   cookie->cfg_mgr=cfg_mgr;
354   cookie->trps=trps;
355   talloc_steal(tids, cookie);
356
357   /* get a tids listener */
358   tids_ev->sock_fd=tids_get_listener(tids,
359                                      tr_tids_req_handler,
360                                      tr_tids_gss_handler,
361                                      cfg_mgr->active->internal->hostname,
362                                      cfg_mgr->active->internal->tids_port,
363                                      (void *)cookie);
364   if (tids_ev->sock_fd < 0) {
365     tr_crit("Error opening TID server socket.");
366     retval=1;
367     goto cleanup;
368   }
369
370   /* and its event */
371   tids_ev->ev=event_new(base,
372                         tids_ev->sock_fd,
373                         EV_READ|EV_PERSIST,
374                         tr_tids_event_cb,
375                         (void *)tids);
376   event_add(tids_ev->ev, NULL);
377
378 cleanup:
379   talloc_free(tmp_ctx);
380   return retval;
381 }