2 * Copyright (c) 2016, JANET(UK)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
37 #include <tid_internal.h>
38 #include <tr_filter.h>
45 #include <trp_internal.h>
46 #include <tr_config.h>
49 /* Structure to hold TR instance and original request in one cookie */
50 typedef struct tr_resp_cookie {
55 /* hold a tids instance and a config manager */
56 struct tr_tids_event_cookie {
63 static void tr_tidc_resp_handler (TIDC_INSTANCE *tidc,
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);
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,
79 static int tr_tids_req_handler (TIDS_INSTANCE *tids,
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;
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;
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;
101 if ((!tids) || (!orig_req) || (!resp)) {
102 tr_debug("tr_tids_req_handler: Bad parameters");
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);
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.");
118 if (NULL == (cfg_comm = tr_comm_lookup(cfg_mgr->active->comms, 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");
125 /* Check that the rp_realm matches the filter for the GSS name that
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");
136 if ((TR_FILTER_NO_MATCH == tr_filter_process_rp_permitted(orig_req->rp_realm,
137 tids->rp_gss->filter,
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");
147 /* Check that the rp_realm is a member of the community in the request */
148 if (NULL == (tr_find_comm_rp(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");
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");
165 apc = tr_dup_name(cfg_comm->apcs->id);
167 /* Check that the APC is configured */
168 if (NULL == (cfg_apc = tr_comm_lookup(cfg_mgr->active->comms, 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");
176 fwd_req->orig_coi = orig_req->comm;
178 /* Check that rp_realm is a member of this APC */
179 if (NULL == (tr_find_comm_rp(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");
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);
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");
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->idp_realms,
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));
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,
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");
219 /* if we aren't defaulting, check idp coi and apc membership */
220 if (NULL == (tr_find_comm_idp(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");
226 if ( cfg_apc && (NULL == (tr_find_comm_idp(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");
234 /* send a TID request to the AAA server(s), and get the answer(s) */
235 /* TBD -- Handle multiple servers */
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");
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;
254 /* Save information about this request for the response */
255 resp_cookie.tids = tids;
256 resp_cookie.orig_req = orig_req;
258 /* Set-up TID connection */
259 if (-1 == (fwd_req->conn = tidc_open_connection(tidc,
260 aaa_servers->hostname->buf,
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");
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");
281 talloc_free(tmp_ctx);
285 static int tr_tids_gss_handler(gss_name_t client_name, TR_NAME *gss_name,
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;
293 if ((!client_name) || (!gss_name) || (!tids) || (!cfg_mgr)) {
294 tr_debug("tr_tidc_gss_handler: Bad parameters.");
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);
304 /* Store the rp client */
306 tr_debug("Client's GSS Name: %s", gss_name->buf);
312 /***** TIDS event handling *****/
314 /* called when a connection to the TIDS port is received */
315 static void tr_tids_event_cb(int listener, short event, void *arg)
317 TIDS_INSTANCE *tids = (TIDS_INSTANCE *)arg;
319 if (0==(event & EV_READ))
320 tr_debug("tr_tids_event_cb: unexpected event on TIDS socket (event=0x%X)", event);
322 tids_accept(tids, listener);
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,
332 struct tr_socket_event *tids_ev)
334 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
335 struct tr_tids_event_cookie *cookie=NULL;
338 if (tids_ev == NULL) {
339 tr_debug("tr_tids_event_init: Null tids_ev.");
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.");
353 cookie->cfg_mgr=cfg_mgr;
355 talloc_steal(tids, cookie);
357 /* get a tids listener */
358 tids_ev->sock_fd=tids_get_listener(tids,
361 cfg_mgr->active->internal->hostname,
362 cfg_mgr->active->internal->tids_port,
364 if (tids_ev->sock_fd < 0) {
365 tr_crit("Error opening TID server socket.");
371 tids_ev->ev=event_new(base,
376 event_add(tids_ev->ev, NULL);
379 talloc_free(tmp_ctx);