Initial path and expiration utilities
[trust_router.git] / tid / tid_req.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 <stdlib.h>
37 #include <assert.h>
38 #include <talloc.h>
39
40 #include <tid_internal.h>
41 #include <jansson.h>
42
43 static int destroy_tid_req(TID_REQ *req)
44 {
45   OM_uint32 minor;
46   if (req->json_references)
47     json_decref(req->json_references);
48   if (req->gssctx)
49     gss_delete_sec_context( &minor, &req->gssctx, NULL);
50   return 0;
51 }
52
53 TID_REQ *tid_req_new()
54 {
55   TID_REQ *req = talloc_zero(NULL, TID_REQ);
56   if(!req)
57     return NULL;
58   talloc_set_destructor(req, destroy_tid_req);
59   req->json_references = json_array();
60   assert(req->json_references);
61   req->conn = -1;
62   return req;
63 }
64
65 TID_REQ *tid_req_get_next_req(TID_REQ *req)
66 {
67   return(req->next_req);
68 }
69
70 void tid_req_set_next_req(TID_REQ *req, TID_REQ *next_req)
71 {
72   req->next_req = next_req;
73 }
74
75 int tid_req_get_resp_sent(TID_REQ *req)
76 {
77   return(req->resp_sent);
78 }
79
80 void tid_req_set_resp_sent(TID_REQ *req, int resp_sent)
81 {
82   req->resp_sent = resp_sent;
83 }
84
85 int tid_req_get_conn(TID_REQ *req)
86 {
87   return(req->conn);
88 }
89
90 void tid_req_set_conn(TID_REQ *req, int conn)
91 {
92   req->conn = conn;
93 }
94
95 gss_ctx_id_t tid_req_get_gssctx(TID_REQ *req)
96 {
97   return(req->gssctx);
98 }
99
100 void tid_req_set_gssctx(TID_REQ *req, gss_ctx_id_t gssctx)
101 {
102   req->gssctx = gssctx;
103 }
104
105 int tid_req_get_resp_rcvd(TID_REQ *req)
106 {
107   return(req->resp_rcvd);
108 }
109
110 void tid_req_set_resp_rcvd(TID_REQ *req, int resp_rcvd)
111 {
112   req->resp_rcvd = resp_rcvd;
113 }
114
115 TR_NAME *tid_req_get_rp_realm(TID_REQ *req)
116 {
117   return(req->rp_realm);
118 }
119
120 void tid_req_set_rp_realm(TID_REQ *req, TR_NAME *rp_realm)
121 {
122   req->rp_realm = rp_realm;
123 }
124
125 TR_NAME *tid_req_get_realm(TID_REQ *req)
126 {
127   return(req->realm);
128 }
129
130 void tid_req_set_realm(TID_REQ *req, TR_NAME *realm)
131 {
132   req->realm = realm;
133 }
134
135 TR_NAME *tid_req_get_comm(TID_REQ *req)
136 {
137   return(req->comm);
138 }
139
140 void tid_req_set_comm(TID_REQ *req, TR_NAME *comm)
141 {
142   req->comm = comm;
143 }
144
145 TR_NAME *tid_req_get_orig_coi(TID_REQ *req)
146 {
147   return(req->orig_coi);
148 }
149
150 void tid_req_set_rp_orig_coi(TID_REQ *req, TR_NAME *orig_coi)
151 {
152   req->orig_coi = orig_coi;
153 }
154
155 TIDC_RESP_FUNC *tid_req_get_resp_func(TID_REQ *req)
156 {
157   return(req->resp_func);
158 }
159
160 void tid_req_set_resp_func(TID_REQ *req, TIDC_RESP_FUNC *resp_func)
161 {
162   req->resp_func = resp_func;
163 }
164
165 void *tid_req_get_cookie(TID_REQ *req)
166 {
167   return(req->cookie);
168 }
169
170 void tid_req_set_cookie(TID_REQ *req, void *cookie)
171 {
172   req->cookie = cookie;
173 }
174
175 TID_REQ *tid_dup_req (TID_REQ *orig_req) 
176 {
177   TID_REQ *new_req = NULL;
178
179   if (NULL == (new_req = malloc(sizeof(TID_REQ)))) {
180     fprintf(stderr, "tid_dup_req: Can't allocated duplicate request.\n");
181     return NULL;
182   }
183
184   /* Memcpy for flat fields, not valid until names are duped. */
185   memcpy(new_req, orig_req, sizeof(TID_REQ));
186   json_incref(new_req->json_references);
187   
188   if ((NULL == (new_req->rp_realm = tr_dup_name(orig_req->rp_realm))) ||
189       (NULL == (new_req->realm = tr_dup_name(orig_req->realm))) ||
190       (NULL == (new_req->comm = tr_dup_name(orig_req->comm)))) {
191         fprintf(stderr, "tid_dup_req: Can't duplicate request (names).\n");
192   }
193
194   if (orig_req->orig_coi) {
195     if (NULL == (new_req->orig_coi = tr_dup_name(orig_req->orig_coi))) {
196       fprintf(stderr, "tid_dup_req: Can't duplicate request (orig_coi).\n");
197     }
198   }
199   
200   return new_req;
201 }
202
203
204 void tid_req_cleanup_json( TID_REQ *req, json_t *ref)
205 {
206   (void) json_array_append_new(req->json_references, ref);
207 }
208
209 void tid_req_free(TID_REQ *req)
210 {
211   talloc_free(req);
212 }
213
214 int tid_req_add_path(TID_REQ *req,
215                      const char *this_system, unsigned port)
216 {
217   char *path_element = talloc_asprintf(req, "%s:%u",
218                                        this_system, port);
219   if (!req->path) {
220     req->path = json_array();
221     if (!req->path)
222       return -1;
223     tid_req_cleanup_json(req, req->path);
224   }
225   return json_array_append( req->path, json_string(path_element));
226 }
227
228
229
230 void tid_srvr_get_address(const TID_SRVR_BLK *blk,
231                           const struct sockaddr **out_addr,
232                           size_t *out_len)
233 {
234   struct sockaddr_in *sa = NULL;
235     assert(blk);
236     sa = talloc_zero(blk, struct sockaddr_in);
237     sa->sin_family = AF_INET;
238     sa->sin_addr = blk->aaa_server_addr;
239     sa->sin_port = htons(2083);
240     *out_addr = (struct sockaddr *) sa;
241     *out_len = sizeof( struct sockaddr_in);
242 }
243
244 DH *tid_srvr_get_dh( TID_SRVR_BLK *blk)
245 {
246   assert(blk);
247   return blk->aaa_server_dh;
248 }
249
250 const TR_NAME *tid_srvr_get_key_name(
251                                     const TID_SRVR_BLK *blk)
252 {
253   assert(blk);
254   return blk->key_name;
255 }