Move DH record from TR_GSSC_INSTANCE to TIDC_INSTANCE, where it belongs
[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 <gsscon.h>
40 #include <trust_router/tr_dh.h>
41 #include <tid_internal.h>
42 #include <tr_msg.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
60 /* creates struct in talloc null context */
61 TIDC_INSTANCE *tidc_create(void)
62 {
63   TIDC_INSTANCE *tidc=talloc(NULL, TIDC_INSTANCE);
64   if (tidc!=NULL) {
65     tidc->gssc = tr_gssc_instance_new(tidc);
66     if (tidc->gssc == NULL) {
67       talloc_free(tidc);
68       return NULL;
69     }
70     tidc->gssc->service_name = "trustidentity";
71     tidc->client_dh = NULL;
72     talloc_set_destructor((void *)tidc, tidc_destructor);
73   }
74   return tidc;
75 }
76
77 void tidc_destroy(TIDC_INSTANCE *tidc)
78 {
79   talloc_free(tidc);
80 }
81
82 int tidc_open_connection (TIDC_INSTANCE *tidc, 
83                           const char *server,
84                           unsigned int port,
85                           gss_ctx_id_t *gssctx)
86 {
87   unsigned int use_port = 0;
88   tidc->gssc->gss_ctx = gssctx;
89
90   if (0 == port)
91     use_port = TID_PORT;
92   else
93     use_port = port;
94
95   tr_debug("tidc_open_connection: opening tidc connection to %s:%d", server, use_port);
96   if (0 == tr_gssc_open_connection(tidc->gssc, server, use_port))
97     return tidc->gssc->conn;
98   else
99     return -1;
100 }
101
102 int tidc_send_request (TIDC_INSTANCE *tidc,
103                        int conn,
104                        gss_ctx_id_t gssctx,
105                        const char *rp_realm,
106                        const char *realm,
107                        const char *comm,
108                        TIDC_RESP_FUNC *resp_handler,
109                        void *cookie)
110 {
111   TID_REQ *tid_req = NULL;
112   char *request_id = NULL;
113   int rc;
114   int orig_conn = 0;
115   gss_ctx_id_t *orig_gss_ctx = NULL;
116
117   /* For ABI compatibility, replace the generic GSS client parameters
118    * with the arguments we were passed. */
119   orig_conn = tidc->gssc->conn; /* save to restore later */
120   if (conn != tidc->gssc->conn) {
121     tr_warning("tidc_send_request: WARNING: socket connection FD does not match FD opened by tidc_open_connection()");
122     tidc->gssc->conn = conn;
123   }
124   orig_gss_ctx = tidc->gssc->gss_ctx; /* save to restore later */
125   if (gssctx != *(tidc->gssc->gss_ctx)) {
126     tr_warning("tidc_send_request: WARNING: sending request with different GSS context than used for tidc_open_connection()");
127     *tidc->gssc->gss_ctx = gssctx;
128   }
129
130   /* Create and populate a TID req structure */
131   if (!(tid_req = tid_req_new()))
132     goto error;
133
134   tid_req->conn = conn;
135   tid_req->gssctx = gssctx;
136
137   if ((NULL == (tid_req->rp_realm = tr_new_name(rp_realm))) ||
138       (NULL == (tid_req->realm = tr_new_name(realm))) ||
139       (NULL == (tid_req->comm = tr_new_name(comm)))) {
140     tr_err ( "tidc_send_request: Error duplicating names.\n");
141     goto error;
142   }
143
144   tid_req->tidc_dh = tr_dh_dup(tidc_get_dh(tidc));
145
146   /* generate an ID */
147   request_id = tr_random_id(NULL);
148   if (request_id) {
149     if (tid_req->request_id = tr_new_name(request_id))
150       tr_debug("tidc_send_request: Created TID request ID: %s", request_id);
151     else
152       tr_debug("tidc_send_request: Unable to set request ID, proceeding without one");
153     talloc_free(request_id);
154   } else
155     tr_debug("tidc_send_request: Failed to generate a TID request ID, proceeding without one");
156
157   rc = tidc_fwd_request(tidc, tid_req, resp_handler, cookie);
158   goto cleanup;
159  error:
160   rc = -1;
161  cleanup:
162   if (tid_req)
163     tid_req_free(tid_req);
164
165   tidc->gssc->conn = orig_conn;
166   tidc->gssc->gss_ctx = orig_gss_ctx;
167   return rc;
168 }
169
170 int tidc_fwd_request(TIDC_INSTANCE *tidc,
171                      TID_REQ *tid_req,
172                      TIDC_RESP_FUNC *resp_handler,
173                      void *cookie)
174 {
175   TALLOC_CTX *tmp_ctx = talloc_new(NULL);
176   TR_MSG *msg = NULL;
177   TR_MSG *resp_msg = NULL;
178   TID_RESP *tid_resp = NULL;
179   int rc = 0;
180
181   /* Create and populate a TID msg structure */
182   if (!(msg = talloc_zero(tmp_ctx, TR_MSG)))
183     goto error;
184
185   msg->msg_type = TID_REQUEST;
186   tr_msg_set_req(msg, tid_req);
187
188
189   tr_debug( "tidc_fwd_request: Sending TID request\n");
190
191   /* Send the request over the connection */
192   resp_msg = tr_gssc_exchange_msgs(tmp_ctx, tidc->gssc, msg);
193   if (resp_msg == NULL)
194     goto error;
195
196   /* TBD -- Check if this is actually a valid response */
197   tid_resp = tr_msg_get_resp(resp_msg);
198   if (tid_resp == NULL) {
199     tr_err( "tidc_fwd_request: Error, no response in the response!\n");
200     goto error;
201   }
202
203   /* Check whether the request IDs matched and warn if not. Do nothing if we don't get
204    * an ID on the return - it is not mandatory to preserve that field. */
205   if (tid_req->request_id) {
206     if ((tid_resp->request_id)
207         && (tr_name_cmp(tid_resp->request_id, tid_req->request_id) != 0)) {
208       /* Requests present but do not match */
209       tr_warning("tidc_fwd_request: Sent request ID %.*s, received response for %.*s",
210                  tid_req->request_id->len, tid_req->request_id->buf,
211                  tid_resp->request_id->len, tid_resp->request_id->buf);
212     }
213   } else if (tid_resp->request_id) {
214     tr_warning("tidc_fwd_request: Sent request without ID, received response for %.*s",
215                tid_resp->request_id->len, tid_resp->request_id->buf);
216   }
217
218   if (resp_handler) {
219     /* Call the caller's response function. It must copy any data it needs before returning. */
220     tr_debug("tidc_fwd_request: calling response callback function.");
221     (*resp_handler)(tidc, tid_req, tr_msg_get_resp(resp_msg), cookie);
222   }
223
224   goto cleanup;
225
226  error:
227   rc = -1;
228  cleanup:
229   talloc_free(tmp_ctx);
230   return rc;
231 }
232
233
234 DH *tidc_get_dh(TIDC_INSTANCE *inst)
235 {
236   return inst->client_dh;
237 }
238
239 DH *tidc_set_dh(TIDC_INSTANCE *inst, DH *dh)
240 {
241   inst->client_dh = dh;
242   return dh;
243 }