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