2 * Copyright (c) 2012, 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.
39 #include <trust_router/tid.h>
40 #include <tr_config.h>
43 /* Structure to hold TR instance and original request in one cookie */
44 typedef struct tr_resp_cookie {
49 static void tr_tidc_resp_handler (TIDC_INSTANCE *tidc,
54 fprintf(stderr, "tr_tidc_resp_handler: Response received! Realm = %s, Community = %s.\n", resp->realm->buf, resp->comm->buf);
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);
63 static int tr_tids_req_handler (TIDS_INSTANCE * tids,
69 TIDC_INSTANCE *tidc = NULL;
70 TR_RESP_COOKIE resp_cookie;
71 TR_AAA_SERVER *aaa_servers = NULL;
75 if ((!tids) || (!req) || (!resp) || (!(*resp))) {
76 printf("tids_req_handler: Bad parameters\n");
80 printf("Request received! Realm = %s, Comm = %s\n", req->realm->buf, req->comm->buf);
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 */
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");
95 /* Use the DH parameters from the original request */
96 tidc->client_dh = req->tidc_dh;
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");
105 /* Send a TID request */
107 resp_cookie.orig_req = req;
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);
118 int main (int argc, const char *argv[])
120 TR_INSTANCE *tr = NULL;
121 struct dirent **cfg_files = NULL;
123 TR_CFG_RC rc = TR_CFG_SUCCESS; /* presume success */
126 /* parse command-line arguments? -- TBD */
128 /* create a Trust Router instance */
129 if (NULL == (tr = tr_create())) {
130 fprintf(stderr, "Unable to create Trust Router instance, exiting.\n");
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");
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");
145 if (TR_CFG_SUCCESS != tr_parse_config(tr, jcfg)) {
146 fprintf (stderr, "Error decoding configuration information, exiting.\n");
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);
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");
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);
168 tids_destroy(tr->tids);