Refactor to move task code out of tr_main.c.
[trust_router.git] / tr / tr_tid.c
1 #include <tr.h>
2 #include <tid_internal.h>
3 #include <tr_filter.h>
4 #include <tr_comm.h>
5 #include <tr_idp.h>
6 #include <tr_rp.h>
7 #include <tr_event.h>
8 #include <tr_debug.h>
9 #include <gsscon.h>
10 #include <tr_tid.h>
11
12 /* Structure to hold TR instance and original request in one cookie */
13 typedef struct tr_resp_cookie {
14   TR_INSTANCE *tr;
15   TID_REQ *orig_req;
16 } TR_RESP_COOKIE;
17
18
19 static void tr_tidc_resp_handler (TIDC_INSTANCE *tidc, 
20                                   TID_REQ *req,
21                                   TID_RESP *resp, 
22                                   void *resp_cookie)
23 {
24   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);
25   req->resp_rcvd = 1;
26
27   /* TBD -- handle concatentation of multiple responses to single req */
28   tids_send_response(((TR_RESP_COOKIE *)resp_cookie)->tr->tids, 
29                      ((TR_RESP_COOKIE *)resp_cookie)->orig_req, 
30                      resp);
31   
32   return;
33 }
34
35 static int tr_tids_req_handler (TIDS_INSTANCE *tids,
36                       TID_REQ *orig_req, 
37                       TID_RESP *resp,
38                       void *tr_in)
39 {
40   TIDC_INSTANCE *tidc = NULL;
41   TR_RESP_COOKIE resp_cookie;
42   TR_AAA_SERVER *aaa_servers = NULL;
43   TR_NAME *apc = NULL;
44   TID_REQ *fwd_req = NULL;
45   TR_COMM *cfg_comm = NULL;
46   TR_COMM *cfg_apc = NULL;
47   TR_INSTANCE *tr = (TR_INSTANCE *) tr_in;
48   int oaction = TR_FILTER_ACTION_REJECT;
49   int rc = 0;
50   time_t expiration_interval;
51
52   if ((!tids) || (!orig_req) || (!resp) ||  (!tr)) {
53     tr_debug("tr_tids_req_handler: Bad parameters");
54     return -1;
55   }
56
57   tr_debug("tr_tids_req_handler: Request received (conn = %d)! Realm = %s, Comm = %s", orig_req->conn, 
58          orig_req->realm->buf, orig_req->comm->buf);
59   if (tids)
60     tids->req_count++;
61
62   /* Duplicate the request, so we can modify and forward it */
63   if (NULL == (fwd_req = tid_dup_req(orig_req))) {
64     tr_debug("tr_tids_req_handler: Unable to duplicate request.");
65     return -1;
66   }
67
68   if (NULL == (cfg_comm = tr_comm_lookup(tids->cookie, orig_req->comm))) {
69     tr_notice("tr_tids_req_hander: Request for unknown comm: %s.", orig_req->comm->buf);
70     tids_send_err_response(tids, orig_req, "Unknown community");
71     return -1;
72   }
73
74   /* Check that the rp_realm matches the filter for the GSS name that 
75    * was received. */
76
77   if ((!(tr)->rp_gss) || 
78       (!(tr)->rp_gss->filter)) {
79     tr_notice("tr_tids_req_handler: No GSS name for incoming request.");
80     tids_send_err_response(tids, orig_req, "No GSS name for request");
81     return -1;
82   }
83
84   if ((TR_FILTER_NO_MATCH == tr_filter_process_rp_permitted(orig_req->rp_realm, (tr)->rp_gss->filter, orig_req->cons, &fwd_req->cons, &oaction)) ||
85       (TR_FILTER_ACTION_REJECT == oaction)) {
86     tr_notice("tr_tids_req_handler: RP realm (%s) does not match RP Realm filter for GSS name", orig_req->rp_realm->buf);
87     tids_send_err_response(tids, orig_req, "RP Realm filter error");
88     return -1;
89   }
90   /* Check that the rp_realm is a member of the community in the request */
91   if (NULL == (tr_find_comm_rp(cfg_comm, orig_req->rp_realm))) {
92     tr_notice("tr_tids_req_handler: RP Realm (%s) not member of community (%s).", orig_req->rp_realm->buf, orig_req->comm->buf);
93     tids_send_err_response(tids, orig_req, "RP COI membership error");
94     return -1;
95   }
96
97   /* Map the comm in the request from a COI to an APC, if needed */
98   if (TR_COMM_COI == cfg_comm->type) {
99     tr_debug("tr_tids_req_handler: Community was a COI, switching.");
100     /* TBD -- In theory there can be more than one?  How would that work? */
101     if ((!cfg_comm->apcs) || (!cfg_comm->apcs->id)) {
102       tr_notice("No valid APC for COI %s.", orig_req->comm->buf);
103       tids_send_err_response(tids, orig_req, "No valid APC for community");
104       return -1;
105     }
106     apc = tr_dup_name(cfg_comm->apcs->id);
107
108     /* Check that the APC is configured */
109     if (NULL == (cfg_apc = tr_comm_lookup(tids->cookie, apc))) {
110       tr_notice("tr_tids_req_hander: Request for unknown comm: %s.", apc->buf);
111       tids_send_err_response(tids, orig_req, "Unknown APC");
112       return -1;
113     }
114
115     fwd_req->comm = apc;
116     fwd_req->orig_coi = orig_req->comm;
117
118     /* Check that rp_realm is a  member of this APC */
119     if (NULL == (tr_find_comm_rp(cfg_apc, orig_req->rp_realm))) {
120       tr_notice("tr_tids_req_hander: RP Realm (%s) not member of community (%s).", orig_req->rp_realm->buf, orig_req->comm->buf);
121       tids_send_err_response(tids, orig_req, "RP APC membership error");
122       return -1;
123     }
124   }
125
126   /* Find the AAA server(s) for this request */
127   if (NULL == (aaa_servers = tr_idp_aaa_server_lookup(((TR_INSTANCE *)tids->cookie)->active_cfg->idp_realms, 
128                                                       orig_req->realm, 
129                                                       orig_req->comm))) {
130       tr_debug("tr_tids_req_handler: No AAA Servers for realm %s, defaulting.", orig_req->realm->buf);
131       if (NULL == (aaa_servers = tr_default_server_lookup (((TR_INSTANCE *)tids->cookie)->active_cfg->default_servers,
132                                                            orig_req->comm))) {
133         tr_notice("tr_tids_req_handler: No default AAA servers, discarded.");
134         tids_send_err_response(tids, orig_req, "No path to AAA Server(s) for realm");
135         return -1;
136       }
137   } else {
138     /* if we aren't defaulting, check idp coi and apc membership */
139     if (NULL == (tr_find_comm_idp(cfg_comm, fwd_req->realm))) {
140       tr_notice("tr_tids_req_handler: IDP Realm (%s) not member of community (%s).", orig_req->realm->buf, orig_req->comm->buf);
141       tids_send_err_response(tids, orig_req, "IDP community membership error");
142       return -1;
143     }
144     if ( cfg_apc && (NULL == (tr_find_comm_idp(cfg_apc, fwd_req->realm)))) {
145       tr_notice("tr_tids_req_handler: IDP Realm (%s) not member of APC (%s).", orig_req->realm->buf, orig_req->comm->buf);
146       tids_send_err_response(tids, orig_req, "IDP APC membership error");
147       return -1;
148     }
149   }
150
151   /* send a TID request to the AAA server(s), and get the answer(s) */
152   /* TBD -- Handle multiple servers */
153
154   if (cfg_apc)
155     expiration_interval = cfg_apc->expiration_interval;
156   else expiration_interval = cfg_comm->expiration_interval;
157   if (fwd_req->expiration_interval)
158     fwd_req->expiration_interval =  (expiration_interval < fwd_req->expiration_interval) ? expiration_interval : fwd_req->expiration_interval;
159   else fwd_req->expiration_interval = expiration_interval;
160   /* Create a TID client instance */
161   if (NULL == (tidc = tidc_create())) {
162     tr_crit("tr_tids_req_hander: Unable to allocate TIDC instance.");
163     tids_send_err_response(tids, orig_req, "Memory allocation failure");
164     return -1;
165   }
166   /* Use the DH parameters from the original request */
167   /* TBD -- this needs to be fixed when we handle more than one req per conn */
168   tidc->client_dh = orig_req->tidc_dh;
169
170   /* Save information about this request for the response */
171   resp_cookie.tr = tr;
172   resp_cookie.orig_req = orig_req;
173
174   /* Set-up TID connection */
175   if (-1 == (fwd_req->conn = tidc_open_connection(tidc, 
176                                                   aaa_servers->hostname->buf,
177                                                   TID_PORT,
178                                               &(fwd_req->gssctx)))) {
179     tr_notice("tr_tids_req_handler: Error in tidc_open_connection.");
180     tids_send_err_response(tids, orig_req, "Can't open connection to next hop TIDS");
181     return -1;
182   };
183
184   /* Send a TID request */
185   if (0 > (rc = tidc_fwd_request(tidc, fwd_req, &tr_tidc_resp_handler, (void *)&resp_cookie))) {
186     tr_notice("Error from tidc_fwd_request, rc = %d.", rc);
187     tids_send_err_response(tids, orig_req, "Can't forward request to next hop TIDS");
188     tid_req_free(orig_req);
189     return -1;
190   }
191     
192   tid_req_free(orig_req);
193   return 0;
194 }
195
196 static int tr_tids_gss_handler(gss_name_t client_name, TR_NAME *gss_name,
197                                void *tr_in)
198 {
199   TR_RP_CLIENT *rp;
200   TR_INSTANCE *tr = (TR_INSTANCE *) tr_in;
201
202   if ((!client_name) || (!gss_name) || (!tr)) {
203     tr_debug("tr_tidc_gss_handler: Bad parameters.");
204     return -1;
205   }
206   
207   /* look up the RP client matching the GSS name */
208   if ((NULL == (rp = tr_rp_client_lookup(tr->active_cfg->rp_clients, gss_name)))) {
209     tr_debug("tr_tids_gss_handler: Unknown GSS name %s", gss_name->buf);
210     return -1;
211   }
212
213   /* Store the rp client in the TR_INSTANCE structure for now... 
214    * TBD -- fix me for new tasking model. */
215   (tr)->rp_gss = rp;
216   tr_debug("Client's GSS Name: %s", gss_name->buf);
217
218   return 0;
219 }
220
221
222 /***** TIDS event handling *****/
223
224 /* called when a connection to the TIDS port is received */
225 static void tr_tids_event_cb(int listener, short event, void *arg)
226 {
227   TIDS_INSTANCE *tids = (TIDS_INSTANCE *)arg;
228
229   if (0==(event & EV_READ))
230     tr_debug("tr_tids_event_cb: unexpected event on TIDS socket (event=0x%X)", event);
231   else 
232     tids_accept(tids, listener);
233 }
234
235 /* Configure the tids instance and set up its event handler.
236  * Returns 0 on success, nonzero on failure. Fills in
237  * *tids_event (which should be allocated). */
238 int tr_tids_event_init(struct event_base *base,
239                        TR_INSTANCE *tr,
240                        struct tr_socket_event *tids_ev)
241 {
242   if (tids_ev == NULL) {
243     tr_debug("tr_tids_event_init: Null tids_ev.");
244     return 1;
245   }
246
247   /* get a tids listener */
248   tids_ev->sock_fd=tids_get_listener(tr->tids,
249                                      tr_tids_req_handler,
250                                      tr_tids_gss_handler,
251                                      tr->active_cfg->internal->hostname,
252                                      tr->active_cfg->internal->tids_port,
253                                      (void *)tr);
254   if (tids_ev->sock_fd < 0) {
255     tr_crit("Error opening TID server socket.");
256     return 1;
257   }
258
259   /* and its event */
260   tids_ev->ev=event_new(base,
261                         tids_ev->sock_fd,
262                         EV_READ|EV_PERSIST,
263                         tr_tids_event_cb,
264                         (void *)tr->tids);
265   event_add(tids_ev->ev, NULL);
266
267   return 0;
268 }