Move DH parameter generation outside of TIDC Instance creation.
[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_idp.h>
42
43 /* Structure to hold TR instance and original request in one cookie */
44 typedef struct tr_resp_cookie {
45   TR_INSTANCE *tr;
46   TID_REQ *orig_req;
47 } TR_RESP_COOKIE;
48
49 static void tr_tidc_resp_handler (TIDC_INSTANCE *tidc, 
50                         TID_REQ *req,
51                         TID_RESP *resp, 
52                         void *resp_cookie) 
53 {
54   fprintf(stderr, "tr_tidc_resp_handler: Response received! Realm = %s, Community = %s.\n", resp->realm->buf, resp->comm->buf);
55   req->resp_rcvd = 1;
56
57   /* TBD -- handle concatentation of multiple responses to single req */
58   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);
59   
60   return;
61 }
62
63 static int tr_tids_req_handler (TIDS_INSTANCE * tids,
64                       TID_REQ *req, 
65                       TID_RESP **resp,
66                       void *tr)
67 {
68   gss_ctx_id_t gssctx;
69   TIDC_INSTANCE *tidc = NULL;
70   TR_RESP_COOKIE resp_cookie;
71   TR_AAA_SERVER *aaa_servers = NULL;
72   int conn = 0;
73   int rc;
74
75   if ((!tids) || (!req) || (!resp) || (!(*resp))) {
76     printf("tids_req_handler: Bad parameters\n");
77     return -1;
78   }
79
80   printf("Request received! Realm = %s, Comm = %s\n", req->realm->buf, req->comm->buf);
81   if (tids)
82     tids->req_count++;
83
84   /* find the AAA server(s) for this request */
85   aaa_servers = tr_idp_aaa_server_lookup((TR_INSTANCE *)tids->cookie, req->realm, req->comm);
86   /* send a TID request to the AAA server(s), and get the answer(s) */
87   /* TBD -- Handle multiple servers */
88
89   /* Create a TID client instance */
90   if (NULL == (tidc = tidc_create())) {
91     fprintf(stderr, "tr_tids_req_hander: Unable to allocate TIDC instance.\n");
92     return -1;
93   }
94
95   /* Use the DH parameters from the original request */
96   tidc->client_dh = req->tidc_dh;
97
98   /* Set-up TID connection */
99   /* TBD -- version of open_connection that takes an inaddr */
100   if (-1 == (conn = tidc_open_connection(tidc, inet_ntoa(aaa_servers->aaa_server_addr), &gssctx))) {
101     printf("tr_tids_req_handler: Error in tidc_open_connection.\n");
102     return -1;
103   };
104
105   /* Send a TID request */
106   resp_cookie.tr = tr;
107   resp_cookie.orig_req = req;
108
109   /* TBD -- version of send request that takes TR_NAMES? */
110   if (0 > (rc = tidc_send_request(tidc, conn, gssctx, req->rp_realm->buf, req->realm->buf, req->comm->buf, &tr_tidc_resp_handler, (void *)&resp_cookie))) {
111     printf("Error in tidc_send_request, rc = %d.\n", rc);
112     return -1;
113   }
114     
115   return 0;
116 }
117
118 int main (int argc, const char *argv[])
119 {
120   TR_INSTANCE *tr = NULL;
121   struct dirent **cfg_files = NULL;
122   json_t *jcfg = NULL;
123   TR_CFG_RC rc = TR_CFG_SUCCESS;        /* presume success */
124   int err = 0, n = 0;;
125
126   /* parse command-line arguments? -- TBD */
127
128   /* create a Trust Router instance */
129   if (NULL == (tr = tr_create())) {
130     fprintf(stderr, "Unable to create Trust Router instance, exiting.\n");
131     return 1;
132   }
133
134   /* find the configuration files */
135   if (0 == (n = tr_find_config_files(&cfg_files))) {
136     fprintf (stderr, "Can't locate configuration files, exiting.\n");
137     exit(1);
138   }
139
140   /* read and parse initial configuration */
141   if (NULL == (jcfg = tr_read_config (n, cfg_files))) {
142     fprintf (stderr, "Error reading or parsing configuration files, exiting.\n");
143     exit(1);
144   }
145   if (TR_CFG_SUCCESS != tr_parse_config(tr, jcfg)) {
146     fprintf (stderr, "Error decoding configuration information, exiting.\n");
147     exit(1);
148   }
149
150   /* apply initial configuration */
151   if (TR_CFG_SUCCESS != (rc = tr_apply_new_config(tr))) {
152     fprintf (stderr, "Error applying configuration, rc = %d.\n", rc);
153     exit(1);
154   }
155
156   /* initialize the trust path query server instance */
157   if (0 == (tr->tids = tids_create ())) {
158     printf ("Error initializing Trust Path Query Server instance.\n");
159     exit(1);
160   }
161
162   /* start the trust path query server, won't return unless fatal error. */
163   if (0 != (err = tids_start(tr->tids, &tr_tids_req_handler, (void *)tr))) {
164     printf ("Error from Trust Path Query Server, err = %d.\n", err);
165     exit(err);
166   }
167
168   tids_destroy(tr->tids);
169   tr_destroy(tr);
170   exit(0);
171 }