Merge branch 'milestone/monitoring' into jennifer/request_id
[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   if (req->rp_realm!=NULL)
59     tr_free_name(req->rp_realm);
60   if (req->realm!=NULL)
61     tr_free_name(req->realm);
62   if (req->comm!=NULL)
63     tr_free_name(req->comm);
64   if (req->orig_coi!=NULL)
65     tr_free_name(req->orig_coi);
66   if (req->request_id!=NULL)
67     tr_free_name(req->request_id);
68   return 0;
69 }
70
71 TID_REQ *tid_req_new()
72 {
73   TID_REQ *req = talloc_zero(NULL, TID_REQ);
74   if(!req)
75     return NULL;
76   talloc_set_destructor(req, destroy_tid_req);
77   req->json_references = json_array();
78   assert(req->json_references);
79   req->conn = -1;
80   req->free_conn = 1;
81   req->request_id = NULL;
82   return req;
83 }
84
85 TID_REQ *tid_req_get_next_req(TID_REQ *req)
86 {
87   return(req->next_req);
88 }
89
90 void tid_req_set_next_req(TID_REQ *req, TID_REQ *next_req)
91 {
92   req->next_req = next_req;
93 }
94
95 int tid_req_get_resp_sent(TID_REQ *req)
96 {
97   return(req->resp_sent);
98 }
99
100 void tid_req_set_resp_sent(TID_REQ *req, int resp_sent)
101 {
102   req->resp_sent = resp_sent;
103 }
104
105 int tid_req_get_conn(TID_REQ *req)
106 {
107   return(req->conn);
108 }
109
110 void tid_req_set_conn(TID_REQ *req, int conn)
111 {
112   req->conn = conn;
113 }
114
115 gss_ctx_id_t tid_req_get_gssctx(TID_REQ *req)
116 {
117   return(req->gssctx);
118 }
119
120 void tid_req_set_gssctx(TID_REQ *req, gss_ctx_id_t gssctx)
121 {
122   req->gssctx = gssctx;
123 }
124
125 int tid_req_get_resp_rcvd(TID_REQ *req)
126 {
127   return(req->resp_rcvd);
128 }
129
130 void tid_req_set_resp_rcvd(TID_REQ *req, int resp_rcvd)
131 {
132   req->resp_rcvd = resp_rcvd;
133 }
134
135 TR_NAME *tid_req_get_rp_realm(TID_REQ *req)
136 {
137   return(req->rp_realm);
138 }
139
140 void tid_req_set_rp_realm(TID_REQ *req, TR_NAME *rp_realm)
141 {
142   req->rp_realm = rp_realm;
143 }
144
145 TR_NAME *tid_req_get_realm(TID_REQ *req)
146 {
147   return(req->realm);
148 }
149
150 void tid_req_set_realm(TID_REQ *req, TR_NAME *realm)
151 {
152   req->realm = realm;
153 }
154
155 TR_NAME *tid_req_get_comm(TID_REQ *req)
156 {
157   return(req->comm);
158 }
159
160 void tid_req_set_comm(TID_REQ *req, TR_NAME *comm)
161 {
162   req->comm = comm;
163 }
164
165 TR_NAME *tid_req_get_orig_coi(TID_REQ *req)
166 {
167   return(req->orig_coi);
168 }
169
170 void tid_req_set_orig_coi(TID_REQ *req, TR_NAME *orig_coi)
171 {
172   req->orig_coi = orig_coi;
173 }
174
175 void tid_req_set_request_id(TID_REQ *req, TR_NAME *request_id)
176 {
177   req->request_id = request_id;
178 }
179
180 TR_NAME *tid_req_get_request_id(TID_REQ *req)
181 {
182   return(req->request_id);
183 }
184
185 TIDC_RESP_FUNC *tid_req_get_resp_func(TID_REQ *req)
186 {
187   return(req->resp_func);
188 }
189
190 void tid_req_set_resp_func(TID_REQ *req, TIDC_RESP_FUNC *resp_func)
191 {
192   req->resp_func = resp_func;
193 }
194
195 void *tid_req_get_cookie(TID_REQ *req)
196 {
197   return(req->cookie);
198 }
199
200 void tid_req_set_cookie(TID_REQ *req, void *cookie)
201 {
202   req->cookie = cookie;
203 }
204
205 /* struct is allocated in talloc null context */
206 TID_REQ *tid_dup_req (TID_REQ *orig_req) 
207 {
208   TID_REQ *new_req = NULL;
209
210   if (NULL == (new_req = talloc_zero(NULL, TID_REQ))) {
211     tr_crit("tid_dup_req: Can't allocated duplicate request.");
212     return NULL;
213   }
214
215   /* Memcpy for flat fields, not valid until names are duped. */
216   memcpy(new_req, orig_req, sizeof(TID_REQ));
217   json_incref(new_req->json_references);
218   new_req->free_conn = 0;
219   
220   if ((NULL == (new_req->rp_realm = tr_dup_name(orig_req->rp_realm))) ||
221       (NULL == (new_req->realm = tr_dup_name(orig_req->realm))) ||
222       (NULL == (new_req->comm = tr_dup_name(orig_req->comm)))) {
223         tr_crit("tid_dup_req: Can't duplicate request (names).");
224   }
225
226   if (orig_req->orig_coi) {
227     if (NULL == (new_req->orig_coi = tr_dup_name(orig_req->orig_coi))) {
228       tr_crit("tid_dup_req: Can't duplicate request (orig_coi).");
229     }
230   }
231
232   if (orig_req->request_id) {
233     if (NULL == (new_req->request_id = tr_dup_name(orig_req->request_id))) {
234       tr_crit("tid_dup_req: Can't duplicate request (request_id).");
235     }
236   }
237
238   return new_req;
239 }
240
241
242 /* Adds the JSON object ref to req's list of objects to release when the
243  * req is freed.
244  */
245 void tid_req_cleanup_json( TID_REQ *req, json_t *ref)
246 {
247   (void) json_array_append_new(req->json_references, ref);
248 }
249
250 void tid_req_free(TID_REQ *req)
251 {
252   talloc_free(req);
253 }
254
255 int tid_req_add_path(TID_REQ *req,
256                      const char *this_system, unsigned port)
257 {
258   char *path_element = talloc_asprintf(req, "%s:%u",
259                                        this_system, port);
260   if (!req->path) {
261     req->path = json_array();
262     if (!req->path)
263       return -1;
264     tid_req_cleanup_json(req, req->path);
265   }
266   return json_array_append( req->path, json_string(path_element));
267 }
268
269
270
271 void tid_srvr_get_address(const TID_SRVR_BLK *blk,
272                           const struct sockaddr **out_addr,
273                           size_t *out_len)
274 {
275   struct sockaddr_in *sa = NULL;
276     assert(blk);
277     sa = talloc_zero(blk, struct sockaddr_in);
278     sa->sin_family = AF_INET;
279     inet_aton(blk->aaa_server_addr, &(sa->sin_addr));
280     sa->sin_port = htons(2083); /* radsec port */
281     *out_addr = (struct sockaddr *) sa;
282     *out_len = sizeof( struct sockaddr_in);
283 }
284
285 DH *tid_srvr_get_dh( TID_SRVR_BLK *blk)
286 {
287   assert(blk);
288   return blk->aaa_server_dh;
289 }
290
291 const TR_NAME *tid_srvr_get_key_name(
292                                     const TID_SRVR_BLK *blk)
293 {
294   assert(blk);
295   return blk->key_name;
296 }