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