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