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