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