tr_constraint_set_get_match_strings
[trust_router.git] / tid / tidc.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 <trust_router/tr_dh.h>
39 #include <trust_router/tid.h>
40 #include <tr_msg.h>
41 #include <gsscon.h>
42
43 int tmp_len = 32;
44
45 TIDC_INSTANCE *tidc_create ()
46 {
47   TIDC_INSTANCE *tidc = NULL;
48
49   if (tidc = malloc(sizeof(TIDC_INSTANCE))) 
50     memset(tidc, 0, sizeof(TIDC_INSTANCE));
51   else
52     return NULL;
53
54   return tidc;
55 }
56
57 void tidc_destroy (TIDC_INSTANCE *tidc)
58 {
59   if (tidc)
60     free(tidc);
61 }
62
63 int tidc_open_connection (TIDC_INSTANCE *tidc, 
64                           char *server,
65                           unsigned int port,
66                           gss_ctx_id_t *gssctx)
67 {
68   int err = 0;
69   int conn = -1;
70   unsigned int use_port = 0;
71
72   if (0 == port)
73     use_port = TID_PORT;
74   else 
75     use_port = port;
76
77   fprintf(stderr, "tidc_open_connection: Opening GSS connection to %s:%u.", server, use_port);  
78   err = gsscon_connect(server, use_port, "trustidentity", &conn, gssctx);
79
80   if (!err)
81     return conn;
82   else
83     return -1;
84 }
85
86 int tidc_send_request (TIDC_INSTANCE *tidc, 
87                        int conn, 
88                        gss_ctx_id_t gssctx,
89                        char *rp_realm,
90                        char *realm, 
91                        char *comm,
92                        TIDC_RESP_FUNC *resp_handler,
93                        void *cookie)
94
95 {
96   TID_REQ *tid_req = NULL;
97
98   /* Create and populate a TID req structure */
99   if (!(tid_req = malloc(sizeof(TID_REQ))))
100     return -1;
101
102   memset(tid_req, 0, sizeof(TID_REQ));
103
104   tid_req->conn = conn;
105   tid_req->gssctx = gssctx;
106
107   if ((NULL == (tid_req->rp_realm = tr_new_name(rp_realm))) ||
108       (NULL == (tid_req->realm = tr_new_name(realm))) ||
109       (NULL == (tid_req->comm = tr_new_name(comm)))) {
110     fprintf (stderr, "tidc_send_request: Error duplicating names.\n");
111     return -1;
112   }
113
114   tid_req->tidc_dh = tidc->client_dh;
115
116   return (tidc_fwd_request(tidc, tid_req, resp_handler, cookie));
117 }
118
119 int tidc_fwd_request (TIDC_INSTANCE *tidc, 
120                       TID_REQ *tid_req, 
121                       TIDC_RESP_FUNC *resp_handler,
122                       void *cookie)
123
124 {
125   char *req_buf = NULL;
126   char *resp_buf = NULL;
127   size_t resp_buflen = 0;
128   TR_MSG *msg = NULL;
129   TR_MSG *resp_msg = NULL;
130   int err;
131
132   /* Create and populate a TID msg structure */
133   if (!(msg = malloc(sizeof(TR_MSG))))
134     return -1;
135
136   msg->msg_type = TID_REQUEST;
137   msg->tid_req = tid_req;
138
139   /* store the response function and cookie */
140   // tid_req->resp_func = resp_handler;
141   // tid_req->cookie = cookie;
142   
143
144   /* Encode the request into a json string */
145   if (!(req_buf = tr_msg_encode(msg))) {
146     fprintf(stderr, "tidc_fwd_request: Error encoding TID request.\n");
147     return -1;
148   }
149
150   fprintf (stderr, "tidc_fwd_request: Sending TID request:\n");
151   fprintf (stderr, "%s\n", req_buf);
152
153   /* Send the request over the connection */
154   if (err = gsscon_write_encrypted_token (tid_req->conn, tid_req->gssctx, req_buf, 
155                                           strlen(req_buf))) {
156     fprintf(stderr, "tidc_fwd_request: Error sending request over connection.\n");
157     return -1;
158   }
159
160   /* TBD -- queue request on instance, read resps in separate thread */
161
162   /* Read the response from the connection */
163   /* TBD -- timeout? */
164   if (err = gsscon_read_encrypted_token(tid_req->conn, tid_req->gssctx, &resp_buf, &resp_buflen)) {
165     if (resp_buf)
166       free(resp_buf);
167     return -1;
168   }
169
170   fprintf(stdout, "tidc_fwd_request: Response Received (%u bytes).\n", (unsigned) resp_buflen);
171   fprintf(stdout, "%s\n", resp_buf);
172
173   if (NULL == (resp_msg = tr_msg_decode(resp_buf, resp_buflen))) {
174     fprintf(stderr, "tidc_fwd_request: Error decoding response.\n");
175     return -1;
176   }
177
178   /* TBD -- Check if this is actually a valid response */
179   if (!resp_msg->tid_resp) {
180     fprintf(stderr, "tidc_fwd_request: Error, no response in the response!\n");
181     return -1;
182   }
183   
184   if (resp_handler)
185     /* Call the caller's response function */
186     (*resp_handler)(tidc, tid_req, resp_msg->tid_resp, cookie);
187   else
188     fprintf(stderr, "tidc_fwd_request: NULL response function.\n");
189
190   if (msg)
191     free(msg);
192   if (tid_req)
193     tid_req_free(tid_req);
194   if (req_buf)
195     free(req_buf);
196   if (resp_buf)
197     free(resp_buf);
198
199   /* TBD -- free the decoded response */
200
201   return 0;
202 }
203