Code for TR to change a COI to an APC in forwarded reqs, also some reorg of request...
[trust_router.git] / tid / tidc.c
1 /*
2  * Copyright (c) 2012, 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 <stdlib.h>
37 #include <jansson.h>
38 #include <gsscon.h>
39
40 #include <trust_router/tr_dh.h>
41 #include <trust_router/tid.h>
42 #include <tr_msg.h>
43 #include <gsscon.h>
44
45 int tmp_len = 32;
46
47 TIDC_INSTANCE *tidc_create ()
48 {
49   TIDC_INSTANCE *tidc = NULL;
50
51   if (tidc = malloc(sizeof(TIDC_INSTANCE))) 
52     memset(tidc, 0, sizeof(TIDC_INSTANCE));
53   else
54     return NULL;
55
56   return tidc;
57 }
58
59 void tidc_destroy (TIDC_INSTANCE *tidc)
60 {
61   if (tidc)
62     free(tidc);
63 }
64
65 TID_REQ *tid_dup_req (TID_REQ *orig_req) 
66 {
67   TID_REQ *new_req;
68
69   if (NULL == (new_req = malloc(sizeof(TID_REQ))))
70     return NULL;
71
72   /* Memcpy for flat fields, not valid until names are duped. */
73   memcpy(new_req, orig_req, sizeof(TID_REQ));
74   
75   new_req->rp_realm = tr_dup_name(orig_req->rp_realm);
76   new_req->realm = tr_dup_name(orig_req->realm);
77   new_req->comm = tr_dup_name(orig_req->comm);
78   new_req->orig_coi = tr_dup_name(orig_req->orig_coi);
79
80   return new_req;
81 }
82
83 int tidc_open_connection (TIDC_INSTANCE *tidc, 
84                           char *server,
85                           gss_ctx_id_t *gssctx)
86 {
87   int err = 0;
88   int conn = -1;
89
90   err = gsscon_connect(server, TID_PORT, &conn);
91
92   if (!err)
93     err = gsscon_active_authenticate(conn, NULL, "trustidentity", gssctx);
94
95   if (!err)
96     return conn;
97   else
98     return -1;
99 }
100
101 int tidc_send_request (TIDC_INSTANCE *tidc, 
102                        int conn, 
103                        gss_ctx_id_t gssctx,
104                        char *rp_realm,
105                        char *realm, 
106                        char *comm,
107                        TIDC_RESP_FUNC *resp_handler,
108                        void *cookie)
109
110 {
111   TID_REQ *tid_req = NULL;
112
113   /* Create and populate a TID req structure */
114   if (!(tid_req = malloc(sizeof(TID_REQ))))
115     return -1;
116
117   memset(tid_req, 0, sizeof(tid_req));
118
119   tid_req->conn = conn;
120   tid_req->gssctx = gssctx;
121
122   if ((NULL == (tid_req->rp_realm = tr_new_name(rp_realm))) ||
123       (NULL == (tid_req->realm = tr_new_name(realm))) ||
124       (NULL == (tid_req->comm = tr_new_name(comm)))) {
125     fprintf (stderr, "tidc_send_request: Error duplicating names.\n");
126     return -1;
127   }
128
129   tid_req->tidc_dh = tidc->client_dh;
130   tid_req->resp_func = resp_handler;
131   tid_req->cookie = cookie;
132   
133   return (tidc_fwd_request(tidc, tid_req, resp_handler, cookie));
134 }
135
136 int tidc_fwd_request (TIDC_INSTANCE *tidc, 
137                       TID_REQ *tid_req, 
138                       TIDC_RESP_FUNC *resp_handler,
139                       void *cookie)
140
141 {
142   char *req_buf = NULL;
143   char *resp_buf = NULL;
144   size_t resp_buflen = 0;
145   TR_MSG *msg = NULL;
146   TR_MSG *resp_msg = NULL;
147   int err;
148
149   /* Create and populate a TID msg structure */
150   if (!(msg = malloc(sizeof(TR_MSG))))
151     return -1;
152
153   msg->msg_type = TID_REQUEST;
154   msg->tid_req = tid_req;
155
156   /* Encode the request into a json string */
157   if (!(req_buf = tr_msg_encode(msg))) {
158     printf("Error encoding TID request.\n");
159     return -1;
160   }
161
162   printf ("Sending TID request:\n");
163   printf ("%s\n", req_buf);
164
165   /* Send the request over the connection */
166   if (err = gsscon_write_encrypted_token (tid_req->conn, tid_req->gssctx, req_buf, 
167                                           strlen(req_buf))) {
168     fprintf(stderr, "Error sending request over connection.\n");
169     return -1;
170   }
171
172   /* TBD -- queue request on instance, read resps in separate thread */
173
174   /* Read the response from the connection */
175   /* TBD -- timeout? */
176   if (err = gsscon_read_encrypted_token(tid_req->conn, tid_req->gssctx, &resp_buf, &resp_buflen)) {
177     if (resp_buf)
178       free(resp_buf);
179     return -1;
180   }
181
182   fprintf(stdout, "Response Received (%d bytes).\n", resp_buflen);
183   fprintf(stdout, "%s\n", resp_buf);
184
185   if (NULL == (resp_msg = tr_msg_decode(resp_buf, resp_buflen))) {
186     fprintf(stderr, "Error decoding response.\n");
187     return -1;
188   }
189
190   /* TBD -- Check if this is actually a valid response */
191   if (!resp_msg->tid_resp) {
192     fprintf(stderr, "Error: No response in the response!\n");
193     return -1;
194   }
195   
196   /* Call the caller's response function */
197   (*tid_req->resp_func)(tidc, tid_req, resp_msg->tid_resp, cookie);
198
199
200   if (msg)
201     free(msg);
202   if (tid_req)
203     free(tid_req);
204   if (req_buf)
205     free(req_buf);
206   if (resp_buf)
207     free(resp_buf);
208
209   /* TBD -- free the decoded response */
210
211   return 0;
212 }
213
214
215
216
217