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