Merge branch 'master' of moonshot.suchdamage.org:/srv/git/trust_router
[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   /* Set-up TID connection */
96   /* TBD -- version of open_connection that takes an inaddr */
97   if (-1 == (conn = tidc_open_connection(tidc, inet_ntoa(aaa_servers->aaa_server_addr), &gssctx))) {
98     printf("tr_tids_req_handler: Error in tidc_open_connection.\n");
99     return -1;
100   };
101
102   /* Send a TID request */
103   resp_cookie.tr = tr;
104   resp_cookie.orig_req = req;
105
106   /* TBD -- version of send request that takes TR_NAMES */
107   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))) {
108     printf("Error in tidc_send_request, rc = %d.\n", rc);
109     return -1;
110   }
111     
112   return 0;
113 }
114
115 int main (int argc, const char *argv[])
116 {
117   TR_INSTANCE *tr = NULL;
118   struct dirent **cfg_files = NULL;
119   json_t *jcfg = NULL;
120   TR_CFG_RC rc = TR_CFG_SUCCESS;        /* presume success */
121   int err = 0, n = 0;;
122
123   /* parse command-line arguments -- TBD */
124
125   /* create a Trust Router instance */
126   if (NULL == (tr = tr_create())) {
127     fprintf(stderr, "Unable to create Trust Router instance, exiting.\n");
128     return 1;
129   }
130
131   /* find the configuration files */
132   if (0 == (n = tr_find_config_files(&cfg_files))) {
133     fprintf (stderr, "Can't locate configuration files, exiting.\n");
134     exit(1);
135   }
136
137   /* read and parse initial configuration */
138   if (NULL == (jcfg = tr_read_config (n, cfg_files))) {
139     fprintf (stderr, "Error reading or parsing configuration files, exiting.\n");
140     exit(1);
141   }
142   if (TR_CFG_SUCCESS != tr_parse_config(tr, jcfg)) {
143     fprintf (stderr, "Error decoding configuration information, exiting.\n");
144     exit(1);
145   }
146
147   /* apply initial configuration */
148   if (TR_CFG_SUCCESS != (rc = tr_apply_new_config(tr))) {
149     fprintf (stderr, "Error applying configuration, rc = %d.\n", rc);
150     exit(1);
151   }
152
153   /* initialize the trust path query server instance */
154   if (0 == (tr->tids = tids_create ())) {
155     printf ("Error initializing Trust Path Query Server instance.\n");
156     exit(1);
157   }
158
159   /* start the trust path query server, won't return unless error. */
160   if (0 != (err = tids_start(tr->tids, &tr_tids_req_handler, (void *)tr))) {
161     printf ("Error starting Trust Path Query Server, err = %d.\n", err);
162     exit(err);
163   }
164
165   tids_destroy(tr->tids);
166   tr_destroy(tr);
167   exit(0);
168 }