Connection clean up
[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
45
46 int tmp_len = 32;
47
48 TIDC_INSTANCE *tidc_create ()
49 {
50   TIDC_INSTANCE *tidc = NULL;
51
52   if (NULL == (tidc = talloc_zero(NULL, TIDC_INSTANCE))) 
53     return NULL;
54
55   return tidc;
56 }
57
58 void tidc_destroy (TIDC_INSTANCE *tidc)
59 {
60   talloc_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   err = gsscon_connect(server, use_port, "trustidentity", &conn, gssctx);
78
79   if (!err)
80     return conn;
81   else
82     return -1;
83 }
84
85 int tidc_send_request (TIDC_INSTANCE *tidc, 
86                        int conn, 
87                        gss_ctx_id_t gssctx,
88                        char *rp_realm,
89                        char *realm, 
90                        char *comm,
91                        TIDC_RESP_FUNC *resp_handler,
92                        void *cookie)
93 {
94   TID_REQ *tid_req = NULL;
95   int rc;
96
97   /* Create and populate a TID req structure */
98   if (!(tid_req = tid_req_new()))
99     return -1;
100
101   tid_req->conn = conn;
102   tid_req->gssctx = gssctx;
103
104   if ((NULL == (tid_req->rp_realm = tr_new_name(rp_realm))) ||
105       (NULL == (tid_req->realm = tr_new_name(realm))) ||
106       (NULL == (tid_req->comm = tr_new_name(comm)))) {
107     tr_err ( "tidc_send_request: Error duplicating names.\n");
108     goto error;
109   }
110
111   tid_req->tidc_dh = tidc->client_dh;
112
113   rc = tidc_fwd_request(tidc, tid_req, resp_handler, cookie);
114   goto cleanup;
115  error:
116   rc = -1;
117  cleanup:
118   tid_req_free(tid_req);
119   return rc;
120 }
121
122 int tidc_fwd_request (TIDC_INSTANCE *tidc, 
123                       TID_REQ *tid_req, 
124                       TIDC_RESP_FUNC *resp_handler,
125                       void *cookie)
126 {
127   char *req_buf = NULL;
128   char *resp_buf = NULL;
129   size_t resp_buflen = 0;
130   TR_MSG *msg = NULL;
131   TR_MSG *resp_msg = NULL;
132   int err;
133   int rc = 0;
134
135   /* Create and populate a TID msg structure */
136   if (!(msg = talloc_zero(tid_req, TR_MSG)))
137     goto error;
138
139   msg->msg_type = TID_REQUEST;
140   tr_msg_set_req(msg, tid_req);
141
142   /* store the response function and cookie */
143   // tid_req->resp_func = resp_handler;
144   // tid_req->cookie = cookie;
145   
146
147   /* Encode the request into a json string */
148   if (!(req_buf = tr_msg_encode(msg))) {
149     tr_err("tidc_fwd_request: Error encoding TID request.\n");
150     goto error;
151   }
152
153   tr_debug( "tidc_fwd_request: Sending TID request:\n");
154   tr_debug( "%s\n", req_buf);
155
156   /* Send the request over the connection */
157   if (err = gsscon_write_encrypted_token (tid_req->conn, tid_req->gssctx, req_buf, 
158                                           strlen(req_buf))) {
159     tr_err( "tidc_fwd_request: Error sending request over connection.\n");
160     goto error;
161   }
162
163   /* TBD -- queue request on instance, read resps in separate thread */
164
165   /* Read the response from the connection */
166   /* TBD -- timeout? */
167   if (err = gsscon_read_encrypted_token(tid_req->conn, tid_req->gssctx, &resp_buf, &resp_buflen)) {
168     if (resp_buf)
169       free(resp_buf);
170     goto error;
171   }
172
173   tr_debug( "tidc_fwd_request: Response Received (%u bytes).\n", (unsigned) resp_buflen);
174   tr_debug( "%s\n", resp_buf);
175
176   if (NULL == (resp_msg = tr_msg_decode(resp_buf, resp_buflen))) {
177     tr_err( "tidc_fwd_request: Error decoding response.\n");
178     goto error;
179   }
180
181   /* TBD -- Check if this is actually a valid response */
182   if (TID_RESPONSE != tr_msg_get_msg_type(resp_msg)) {
183     tr_err( "tidc_fwd_request: Error, no response in the response!\n");
184     goto error;
185   }
186   
187   if (resp_handler)
188     /* Call the caller's response function */
189     (*resp_handler)(tidc, tid_req, tr_msg_get_resp(resp_msg), cookie);
190   goto cleanup;
191
192  error:
193   rc = -1;
194  cleanup:
195   if (msg)
196     talloc_free(msg);
197   if (req_buf)
198     free(req_buf);
199   if (resp_buf)
200     free(resp_buf);
201
202   /* TBD -- free the decoded response */
203
204   return rc;
205 }
206
207
208 DH * tidc_get_dh(TIDC_INSTANCE *inst)
209 {
210   return inst->client_dh;
211 }
212
213 DH *tidc_set_dh(TIDC_INSTANCE *inst, DH *dh)
214 {
215   inst->client_dh = dh;
216   return dh;
217 }