Refactor tidc/monc to better share code
[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
45
46 int tmp_len = 32;
47
48 /* creates struct in talloc null context */
49 TIDC_INSTANCE *tidc_create(void)
50 {
51   TIDC_INSTANCE *tidc=talloc(NULL, TIDC_INSTANCE);
52   if (tidc!=NULL) {
53     tidc->gssc = tr_gssc_instance_new(tidc);
54     if (tidc->gssc == NULL) {
55       talloc_free(tidc);
56       return NULL;
57     }
58
59     tidc->gssc->service_name = "trustidentity";
60   }
61   return tidc;
62 }
63
64 void tidc_destroy(TIDC_INSTANCE *tidc)
65 {
66   talloc_free(tidc);
67 }
68
69 int tidc_open_connection (TIDC_INSTANCE *tidc, 
70                           const char *server,
71                           unsigned int port,
72                           gss_ctx_id_t *gssctx)
73 {
74   unsigned int use_port = 0;
75   tidc->gssc->gss_ctx = gssctx;
76
77   if (0 == port)
78     use_port = TID_PORT;
79   else
80     use_port = port;
81
82   tr_debug("tidc_open_connection: opening tidc connection to %s:%d", server, use_port);
83   if (0 == tr_gssc_open_connection(tidc->gssc, server, use_port))
84     return tidc->gssc->conn;
85   else
86     return -1;
87 }
88
89 int tidc_send_request (TIDC_INSTANCE *tidc,
90                        int conn,
91                        gss_ctx_id_t gssctx,
92                        const char *rp_realm,
93                        const char *realm,
94                        const char *comm,
95                        TIDC_RESP_FUNC *resp_handler,
96                        void *cookie)
97 {
98   TID_REQ *tid_req = NULL;
99   int rc;
100   int orig_conn = 0;
101   gss_ctx_id_t *orig_gss_ctx = NULL;
102
103   /* For ABI compatibility, replace the generic GSS client parameters
104    * with the arguments we were passed. */
105   orig_conn = tidc->gssc->conn; /* save to restore later */
106   if (conn != tidc->gssc->conn) {
107     tr_warning("tidc_send_request: WARNING: socket connection FD does not match FD opened by tidc_open_connection()");
108     tidc->gssc->conn = conn;
109   }
110   orig_gss_ctx = tidc->gssc->gss_ctx; /* save to restore later */
111   if (gssctx != *(tidc->gssc->gss_ctx)) {
112     tr_warning("tidc_send_request: WARNING: sending request with different GSS context than used for tidc_open_connection()");
113     *tidc->gssc->gss_ctx = gssctx;
114   }
115
116   /* Create and populate a TID req structure */
117   if (!(tid_req = tid_req_new()))
118     goto error;
119
120   tid_req->conn = conn;
121   tid_req->gssctx = gssctx;
122
123   if ((NULL == (tid_req->rp_realm = tr_new_name(rp_realm))) ||
124       (NULL == (tid_req->realm = tr_new_name(realm))) ||
125       (NULL == (tid_req->comm = tr_new_name(comm)))) {
126     tr_err ( "tidc_send_request: Error duplicating names.\n");
127     goto error;
128   }
129
130   tid_req->tidc_dh = tr_dh_dup(tidc->gssc->client_dh);
131
132   rc = tidc_fwd_request(tidc, tid_req, resp_handler, cookie);
133   goto cleanup;
134  error:
135   rc = -1;
136  cleanup:
137   if (tid_req)
138     tid_req_free(tid_req);
139
140   tidc->gssc->conn = orig_conn;
141   tidc->gssc->gss_ctx = orig_gss_ctx;
142   return rc;
143 }
144
145 int tidc_fwd_request(TIDC_INSTANCE *tidc,
146                      TID_REQ *tid_req,
147                      TIDC_RESP_FUNC *resp_handler,
148                      void *cookie)
149 {
150   TALLOC_CTX *tmp_ctx = talloc_new(NULL);
151   TR_MSG *msg = NULL;
152   TR_MSG *resp_msg = NULL;
153   int rc = 0;
154
155   /* Create and populate a TID msg structure */
156   if (!(msg = talloc_zero(tmp_ctx, TR_MSG)))
157     goto error;
158
159   msg->msg_type = TID_REQUEST;
160   tr_msg_set_req(msg, tid_req);
161
162
163   tr_debug( "tidc_fwd_request: Sending TID request\n");
164
165   /* Send the request over the connection */
166   resp_msg = tr_gssc_exchange_msgs(tmp_ctx, tidc->gssc, msg);
167   if (resp_msg == NULL)
168     goto error;
169
170   /* TBD -- Check if this is actually a valid response */
171   if (TID_RESPONSE != tr_msg_get_msg_type(resp_msg)) {
172     tr_err( "tidc_fwd_request: Error, no response in the response!\n");
173     goto error;
174   }
175
176   if (resp_handler) {
177     /* Call the caller's response function. It must copy any data it needs before returning. */
178     tr_debug("tidc_fwd_request: calling response callback function.");
179     (*resp_handler)(tidc, tid_req, tr_msg_get_resp(resp_msg), cookie);
180   }
181
182   goto cleanup;
183
184  error:
185   rc = -1;
186  cleanup:
187   talloc_free(tmp_ctx);
188   return rc;
189 }
190
191
192 DH * tidc_get_dh(TIDC_INSTANCE *inst)
193 {
194   return tr_gssc_get_dh(inst->gssc);
195 }
196
197 DH *tidc_set_dh(TIDC_INSTANCE *inst, DH *dh)
198 {
199   return tr_gssc_set_dh(inst->gssc, dh);
200 }