Add a 'request_id' to TID requests and responses
[trust_router.git] / tid / tidc.c
1 /*
2  * Copyright (c) 2012, 2014-2015, 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 #include <talloc.h>
38
39 #include <trust_router/tr_dh.h>
40 #include <tid_internal.h>
41 #include <tr_msg.h>
42 #include <gsscon.h>
43 #include <tr_debug.h>
44 #include <tr_rand_id.h>
45
46
47 int tmp_len = 32;
48
49 static int tidc_destructor(void *obj)
50 {
51   TIDC_INSTANCE *tidc=talloc_get_type_abort(obj, TIDC_INSTANCE);
52   if (NULL!=tidc) {
53     if (NULL!=tidc->client_dh)
54       tr_destroy_dh_params(tidc->client_dh);
55   }
56   return 0;
57 }
58
59 /* creates struct in talloc null context */
60 TIDC_INSTANCE *tidc_create(void)
61 {
62   TIDC_INSTANCE *tidc=talloc(NULL, TIDC_INSTANCE);
63   if (tidc!=NULL) {
64     tidc->client_dh=NULL;
65     talloc_set_destructor((void *)tidc, tidc_destructor);
66   }
67   return tidc;
68 }
69
70 void tidc_destroy(TIDC_INSTANCE *tidc)
71 {
72   talloc_free(tidc);
73 }
74
75 int tidc_open_connection (TIDC_INSTANCE *tidc, 
76                           const char *server,
77                           unsigned int port,
78                           gss_ctx_id_t *gssctx)
79 {
80   int err = 0;
81   int conn = -1;
82   unsigned int use_port = 0;
83
84   if (0 == port)
85     use_port = TID_PORT;
86   else
87     use_port = port;
88
89   tr_debug("tidc_open_connection: opening tidc connection to %s:%d", server, port);
90   err = gsscon_connect(server, use_port, "trustidentity", &conn, gssctx);
91
92   if (!err)
93     return conn;
94   else
95     return -1;
96 }
97
98 int tidc_send_request (TIDC_INSTANCE *tidc,
99                        int conn,
100                        gss_ctx_id_t gssctx,
101                        const char *rp_realm,
102                        const char *realm, 
103                        const char *comm,
104                        TIDC_RESP_FUNC *resp_handler,
105                        void *cookie)
106 {
107   TID_REQ *tid_req = NULL;
108   char *request_id = NULL;
109   int rc;
110
111   /* Create and populate a TID req structure */
112   if (!(tid_req = tid_req_new()))
113     return -1;
114
115   tid_req->conn = conn;
116   tid_req->gssctx = gssctx;
117
118   if ((NULL == (tid_req->rp_realm = tr_new_name(rp_realm))) ||
119       (NULL == (tid_req->realm = tr_new_name(realm))) ||
120       (NULL == (tid_req->comm = tr_new_name(comm)))) {
121     tr_err ( "tidc_send_request: Error duplicating names.\n");
122     goto error;
123   }
124
125   tid_req->tidc_dh = tr_dh_dup(tidc->client_dh);
126
127   /* generate an ID */
128   request_id = tr_random_id(NULL);
129   if (request_id) {
130     if (tid_req->request_id = tr_new_name(request_id))
131       tr_debug("tidc_send_request: Created TID request ID: %s", request_id);
132     else
133       tr_debug("tidc_send_request: Unable to set request ID, proceeding without one");
134     talloc_free(request_id);
135   } else
136     tr_debug("tidc_send_request: Failed to generate a TID request ID, proceeding without one");
137
138   rc = tidc_fwd_request(tidc, tid_req, resp_handler, cookie);
139   goto cleanup;
140  error:
141   rc = -1;
142  cleanup:
143   tid_req_free(tid_req);
144   return rc;
145 }
146
147 int tidc_fwd_request(TIDC_INSTANCE *tidc,
148                      TID_REQ *tid_req,
149                      TIDC_RESP_FUNC *resp_handler,
150                      void *cookie)
151 {
152   char *req_buf = NULL;
153   char *resp_buf = NULL;
154   size_t resp_buflen = 0;
155   TR_MSG *msg = NULL;
156   TR_MSG *resp_msg = NULL;
157   TID_RESP *tid_resp = NULL;
158   int err;
159   int rc = 0;
160
161   /* Create and populate a TID msg structure */
162   if (!(msg = talloc_zero(tid_req, TR_MSG)))
163     goto error;
164
165   msg->msg_type = TID_REQUEST;
166   tr_msg_set_req(msg, tid_req);
167
168   /* store the response function and cookie */
169   // tid_req->resp_func = resp_handler;
170   // tid_req->cookie = cookie;
171
172
173   /* Encode the request into a json string */
174   if (!(req_buf = tr_msg_encode(msg))) {
175     tr_err("tidc_fwd_request: Error encoding TID request.\n");
176     goto error;
177   }
178
179   tr_debug( "tidc_fwd_request: Sending TID request:\n");
180   tr_debug( "%s\n", req_buf);
181
182   /* Send the request over the connection */
183   if (err = gsscon_write_encrypted_token (tid_req->conn, tid_req->gssctx, req_buf,
184                                           strlen(req_buf))) {
185     tr_err( "tidc_fwd_request: Error sending request over connection.\n");
186     goto error;
187   }
188
189   /* TBD -- queue request on instance, read resps in separate thread */
190
191   /* Read the response from the connection */
192   /* TBD -- timeout? */
193   if (err = gsscon_read_encrypted_token(tid_req->conn, tid_req->gssctx, &resp_buf, &resp_buflen)) {
194     if (resp_buf)
195       free(resp_buf);
196     goto error;
197   }
198
199   tr_debug( "tidc_fwd_request: Response Received (%u bytes).\n", (unsigned) resp_buflen);
200   tr_debug( "%s\n", resp_buf);
201
202   if (NULL == (resp_msg = tr_msg_decode(resp_buf, resp_buflen))) {
203     tr_err( "tidc_fwd_request: Error decoding response.\n");
204     goto error;
205   }
206
207   /* TBD -- Check if this is actually a valid response */
208   tid_resp = tr_msg_get_resp(resp_msg);
209   if (tid_resp == NULL) {
210     tr_err( "tidc_fwd_request: Error, no response in the response!\n");
211     goto error;
212   }
213
214   /* Check whether the request IDs matched and warn if not. Do nothing if we don't get
215    * an ID on the return - it is not mandatory to preserve that field. */
216   if (tid_req->request_id) {
217     if ((tid_resp->request_id)
218         && (tr_name_cmp(tid_resp->request_id, tid_req->request_id) != 0)) {
219       /* Requests present but do not match */
220       tr_warning("tidc_fwd_request: Sent request ID %.*s, received response for %.*s",
221                  tid_req->request_id->len, tid_req->request_id->buf,
222                  tid_resp->request_id->len, tid_resp->request_id->buf);
223     }
224   } else if (tid_resp->request_id) {
225     tr_warning("tidc_fwd_request: Sent request without ID, received response for %.*s",
226                tid_resp->request_id->len, tid_resp->request_id->buf);
227   }
228
229   if (resp_handler) {
230     /* Call the caller's response function. It must copy any data it needs before returning. */
231     tr_debug("tidc_fwd_request: calling response callback function.");
232     (*resp_handler)(tidc, tid_req, tr_msg_get_resp(resp_msg), cookie);
233   }
234
235   goto cleanup;
236
237  error:
238   rc = -1;
239  cleanup:
240   if (msg)
241     talloc_free(msg);
242   if (req_buf)
243     free(req_buf);
244   if (resp_buf)
245     free(resp_buf);
246   if (resp_msg)
247     tr_msg_free_decoded(resp_msg);
248   return rc;
249 }
250
251
252 DH * tidc_get_dh(TIDC_INSTANCE *inst)
253 {
254   return inst->client_dh;
255 }
256
257 DH *tidc_set_dh(TIDC_INSTANCE *inst, DH *dh)
258 {
259   inst->client_dh = dh;
260   return dh;
261 }