Code for TR to change a COI to an APC in forwarded reqs, also some reorg of request...
[trust_router.git] / tr / tr_main.c
1 /*
2  * Copyright (c) 2012, 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 <stdio.h>
36 #include <jansson.h>
37
38 #include <tr.h>
39 #include <trust_router/tid.h>
40 #include <tr_config.h>
41 #include <tr_comm.h>
42 #include <tr_idp.h>
43
44 /* Structure to hold TR instance and original request in one cookie */
45 typedef struct tr_resp_cookie {
46   TR_INSTANCE *tr;
47   TID_REQ *orig_req;
48 } TR_RESP_COOKIE;
49
50 static void tr_tidc_resp_handler (TIDC_INSTANCE *tidc, 
51                         TID_REQ *req,
52                         TID_RESP *resp, 
53                         void *resp_cookie) 
54 {
55   fprintf(stderr, "tr_tidc_resp_handler: Response received! Realm = %s, Community = %s.\n", resp->realm->buf, resp->comm->buf);
56   req->resp_rcvd = 1;
57
58   /* TBD -- handle concatentation of multiple responses to single req */
59   tids_send_response(((TR_RESP_COOKIE *)resp_cookie)->tr->tids, ((TR_RESP_COOKIE *)resp_cookie)->orig_req->conn, &((TR_RESP_COOKIE *)resp_cookie)->orig_req->gssctx, resp);
60   
61   return;
62 }
63
64 static int tr_tids_req_handler (TIDS_INSTANCE * tids,
65                       TID_REQ *orig_req, 
66                       TID_RESP **resp,
67                       void *tr)
68 {
69   TIDC_INSTANCE *tidc = NULL;
70   TR_RESP_COOKIE resp_cookie;
71   TR_AAA_SERVER *aaa_servers = NULL;
72   TR_NAME *apc = NULL;
73   TID_REQ *req = NULL;
74   TR_COMM *cfg_comm = NULL;
75   int rc;
76
77   if ((!tids) || (!orig_req) || (!resp) || (!(*resp))) {
78     printf("tids_req_handler: Bad parameters\n");
79     return -1;
80   }
81
82   printf("Request received! Realm = %s, Comm = %s\n", orig_req->realm->buf, orig_req->comm->buf);
83   if (tids)
84     tids->req_count++;
85
86   /* Save tr and request info for the response */
87   resp_cookie.tr = tr;
88   resp_cookie.orig_req = req;
89
90   /* Duplicate the request, so we can modify and forward it */
91   if (NULL == (req = tid_dup_req(orig_req))) {
92     fprintf(stderr, "tr_tids_req_handler: Unable to duplicate request.\n");
93     return -1;
94   }
95
96   /* Map the comm in the request from a COI to an APC, if needed */
97   if (NULL == (cfg_comm = tr_comm_lookup((TR_INSTANCE *)tids->cookie, req->comm))) {
98     fprintf(stderr, "tr_tids_req_hander: Request for unknown comm: %s.\n", req->comm->buf);
99   }
100
101   /* TBD -- check that the rp_realm is a member of the original community */
102
103   if (TR_COMM_COI == cfg_comm->type) {
104     /* TBD -- In theory there can be more than one?  How would that work? */
105     apc = tr_dup_name(cfg_comm->apcs->id);
106     req->orig_coi = req->comm;
107     req->comm = apc;
108   }
109
110   /* Find the AAA server(s) for this request */
111   aaa_servers = tr_idp_aaa_server_lookup((TR_INSTANCE *)tids->cookie, req->realm, apc);
112   /* send a TID request to the AAA server(s), and get the answer(s) */
113   /* TBD -- Handle multiple servers */
114
115   /* Create a TID client instance */
116   if (NULL == (tidc = tidc_create())) {
117     fprintf(stderr, "tr_tids_req_hander: Unable to allocate TIDC instance.\n");
118     return -1;
119   }
120
121   /* Use the DH parameters from the original request */
122   /* TBD -- this needs to be fixed when we handle more than one req per conn */
123   tidc->client_dh = req->tidc_dh;
124
125   /* Save information about this request for the response */
126   resp_cookie.tr = tr;
127   resp_cookie.orig_req = req;
128
129   /* Set-up TID connection */
130   /* TBD -- handle IPv6 Addresses */
131   if (-1 == (req->conn = tidc_open_connection(tidc, inet_ntoa(aaa_servers->aaa_server_addr), &(req->gssctx)))) {
132     printf("tr_tids_req_handler: Error in tidc_open_connection.\n");
133     return -1;
134   };
135
136   /* Send a TID request */
137
138   if (0 > (rc = tidc_fwd_request(tidc, req, &tr_tidc_resp_handler, (void *)&resp_cookie))) {
139     printf("Error from tidc_fwd_request, rc = %d.\n", rc);
140     return -1;
141   }
142     
143   return 0;
144 }
145
146 int main (int argc, const char *argv[])
147 {
148   TR_INSTANCE *tr = NULL;
149   struct dirent **cfg_files = NULL;
150   json_t *jcfg = NULL;
151   TR_CFG_RC rc = TR_CFG_SUCCESS;        /* presume success */
152   int err = 0, n = 0;;
153
154   /* parse command-line arguments? -- TBD */
155
156   /* create a Trust Router instance */
157   if (NULL == (tr = tr_create())) {
158     fprintf(stderr, "Unable to create Trust Router instance, exiting.\n");
159     return 1;
160   }
161
162   /* find the configuration files */
163   if (0 == (n = tr_find_config_files(&cfg_files))) {
164     fprintf (stderr, "Can't locate configuration files, exiting.\n");
165     exit(1);
166   }
167
168   /* read and parse initial configuration */
169   if (NULL == (jcfg = tr_read_config (n, cfg_files))) {
170     fprintf (stderr, "Error reading or parsing configuration files, exiting.\n");
171     exit(1);
172   }
173   if (TR_CFG_SUCCESS != tr_parse_config(tr, jcfg)) {
174     fprintf (stderr, "Error decoding configuration information, exiting.\n");
175     exit(1);
176   }
177
178   /* apply initial configuration */
179   if (TR_CFG_SUCCESS != (rc = tr_apply_new_config(tr))) {
180     fprintf (stderr, "Error applying configuration, rc = %d.\n", rc);
181     exit(1);
182   }
183
184   /* initialize the trust path query server instance */
185   if (0 == (tr->tids = tids_create ())) {
186     printf ("Error initializing Trust Path Query Server instance.\n");
187     exit(1);
188   }
189
190   /* start the trust path query server, won't return unless fatal error. */
191   if (0 != (err = tids_start(tr->tids, &tr_tids_req_handler, (void *)tr))) {
192     printf ("Error from Trust Path Query Server, err = %d.\n", err);
193     exit(err);
194   }
195
196   tids_destroy(tr->tids);
197   tr_destroy(tr);
198   exit(0);
199 }