Support multiple AAA servers. Compiles but untested.
[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 static int tidc_destructor(void *obj)
49 {
50   TIDC_INSTANCE *tidc=talloc_get_type_abort(obj, TIDC_INSTANCE);
51   if (NULL!=tidc) {
52     if (NULL!=tidc->client_dh)
53       tr_destroy_dh_params(tidc->client_dh);
54   }
55   return 0;
56 }
57
58 TIDC_INSTANCE *tidc_create(TALLOC_CTX *mem_ctx)
59 {
60   TIDC_INSTANCE *tidc=talloc(mem_ctx, TIDC_INSTANCE);
61   if (tidc!=NULL) {
62     tidc->client_dh=NULL;
63     talloc_set_destructor((void *)tidc, tidc_destructor);
64   }
65   return tidc;
66 }
67
68 void tidc_free(TIDC_INSTANCE *tidc)
69 {
70   talloc_free(tidc);
71 }
72
73 int tidc_open_connection (TIDC_INSTANCE *tidc, 
74                           const char *server,
75                           unsigned int port,
76                           gss_ctx_id_t *gssctx)
77 {
78   int err = 0;
79   int conn = -1;
80   unsigned int use_port = 0;
81
82   if (0 == port)
83     use_port = TID_PORT;
84   else
85     use_port = port;
86
87   tr_debug("tidc_open_connection: opening tidc connection to %s:%d", server, port);
88   err = gsscon_connect(server, use_port, "trustidentity", &conn, gssctx);
89
90   if (!err)
91     return conn;
92   else
93     return -1;
94 }
95
96 int tidc_send_request (TIDC_INSTANCE *tidc,
97                        int conn,
98                        gss_ctx_id_t gssctx,
99                        const char *rp_realm,
100                        const char *realm, 
101                        const char *comm,
102                        TIDC_RESP_FUNC *resp_handler,
103                        void *cookie)
104 {
105   TID_REQ *tid_req = NULL;
106   int rc;
107
108   /* Create and populate a TID req structure */
109   if (!(tid_req = tid_req_new()))
110     return -1;
111
112   tid_req->conn = conn;
113   tid_req->gssctx = gssctx;
114
115   if ((NULL == (tid_req->rp_realm = tr_new_name(rp_realm))) ||
116       (NULL == (tid_req->realm = tr_new_name(realm))) ||
117       (NULL == (tid_req->comm = tr_new_name(comm)))) {
118     tr_err ( "tidc_send_request: Error duplicating names.\n");
119     goto error;
120   }
121
122   tid_req->tidc_dh = tidc->client_dh;
123
124   rc = tidc_fwd_request(tidc, tid_req, resp_handler, cookie);
125   goto cleanup;
126  error:
127   rc = -1;
128  cleanup:
129   tid_req_free(tid_req);
130   return rc;
131 }
132
133 int tidc_fwd_request(TIDC_INSTANCE *tidc,
134                      TID_REQ *tid_req,
135                      TIDC_RESP_FUNC *resp_handler,
136                      void *cookie)
137 {
138   char *req_buf = NULL;
139   char *resp_buf = NULL;
140   size_t resp_buflen = 0;
141   TR_MSG *msg = NULL;
142   TR_MSG *resp_msg = NULL;
143   int err;
144   int rc = 0;
145
146   /* Create and populate a TID msg structure */
147   if (!(msg = talloc_zero(tid_req, TR_MSG)))
148     goto error;
149
150   msg->msg_type = TID_REQUEST;
151   tr_msg_set_req(msg, tid_req);
152
153   /* store the response function and cookie */
154   // tid_req->resp_func = resp_handler;
155   // tid_req->cookie = cookie;
156
157
158   /* Encode the request into a json string */
159   if (!(req_buf = tr_msg_encode(msg))) {
160     tr_err("tidc_fwd_request: Error encoding TID request.\n");
161     goto error;
162   }
163
164   tr_debug( "tidc_fwd_request: Sending TID request:\n");
165   tr_debug( "%s\n", req_buf);
166
167   /* Send the request over the connection */
168   if (err = gsscon_write_encrypted_token (tid_req->conn, tid_req->gssctx, req_buf,
169                                           strlen(req_buf))) {
170     tr_err( "tidc_fwd_request: Error sending request over connection.\n");
171     goto error;
172   }
173
174   /* TBD -- queue request on instance, read resps in separate thread */
175
176   /* Read the response from the connection */
177   /* TBD -- timeout? */
178   if (err = gsscon_read_encrypted_token(tid_req->conn, tid_req->gssctx, &resp_buf, &resp_buflen)) {
179     if (resp_buf)
180       free(resp_buf);
181     goto error;
182   }
183
184   tr_debug( "tidc_fwd_request: Response Received (%u bytes).\n", (unsigned) resp_buflen);
185   tr_debug( "%s\n", resp_buf);
186
187   if (NULL == (resp_msg = tr_msg_decode(resp_buf, resp_buflen))) {
188     tr_err( "tidc_fwd_request: Error decoding response.\n");
189     goto error;
190   }
191
192   /* TBD -- Check if this is actually a valid response */
193   if (TID_RESPONSE != tr_msg_get_msg_type(resp_msg)) {
194     tr_err( "tidc_fwd_request: Error, no response in the response!\n");
195     goto error;
196   }
197
198   if (resp_handler) {
199     /* Call the caller's response function. It must copy any data it needs before returning. */
200     tr_debug("tidc_fwd_request: calling response callback function.");
201     (*resp_handler)(tidc, tid_req, tr_msg_get_resp(resp_msg), cookie);
202   }
203
204   goto cleanup;
205
206  error:
207   rc = -1;
208  cleanup:
209   if (msg)
210     talloc_free(msg);
211   if (req_buf)
212     free(req_buf);
213   if (resp_buf)
214     free(resp_buf);
215   if (resp_msg)
216     tr_msg_free_decoded(resp_msg);
217   return rc;
218 }
219
220
221 DH * tidc_get_dh(TIDC_INSTANCE *inst)
222 {
223   return inst->client_dh;
224 }
225
226 DH *tidc_set_dh(TIDC_INSTANCE *inst, DH *dh)
227 {
228   inst->client_dh = dh;
229   return dh;
230 }