Merge branch 'master' into jennifer/trp-devel
[trust_router.git] / tid / tid_resp.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 <assert.h>
38 #include <talloc.h>
39
40 #include <trust_router/tr_dh.h>
41 #include <tid_internal.h>
42
43 static int tid_resp_destructor(void *obj)
44 {
45   TID_RESP *resp=talloc_get_type_abort(obj, TID_RESP);
46   if (resp->err_msg!=NULL)
47     tr_free_name(resp->err_msg);
48   if (resp->rp_realm!=NULL)
49     tr_free_name(resp->rp_realm);
50   if (resp->realm!=NULL)
51     tr_free_name(resp->realm);
52   if (resp->comm!=NULL)
53     tr_free_name(resp->comm);
54   if (resp->orig_coi!=NULL)
55     tr_free_name(resp->orig_coi);
56   return 0;
57 }
58
59 TID_RESP *tid_resp_new(TALLOC_CTX *mem_ctx)
60 {
61   TID_RESP *resp=talloc(mem_ctx, TID_RESP);
62   if (resp!=NULL) {
63     resp->result=TID_ERROR;
64     resp->err_msg=NULL;
65     resp->rp_realm=NULL;
66     resp->realm=NULL;
67     resp->comm=NULL;
68     resp->cons=NULL;
69     resp->orig_coi=NULL;
70     resp->servers=NULL;
71     resp->error_path=NULL;
72     talloc_set_destructor((void *)resp, tid_resp_destructor);
73   }
74   return resp;
75 }
76
77 void tid_resp_free(TID_RESP *resp)
78 {
79   if (resp)
80     talloc_free(resp);
81 }
82
83 TID_RESP *tid_resp_dup(TALLOC_CTX *mem_ctx, TID_RESP *resp)
84 {
85   TID_RESP *newresp=NULL;
86
87   if (resp==NULL)
88     return NULL;
89
90   newresp=tid_resp_new(mem_ctx);
91
92   if (NULL!=newresp) {
93     newresp->result=resp->result;
94     newresp->err_msg=tr_dup_name(resp->err_msg);
95     newresp->rp_realm=tr_dup_name(resp->rp_realm);
96     newresp->realm=tr_dup_name(resp->realm);
97     newresp->comm=tr_dup_name(resp->comm);
98     newresp->orig_coi=tr_dup_name(resp->orig_coi);
99     newresp->servers=tid_srvr_blk_dup(newresp, resp->servers);
100     tid_resp_set_cons(newresp, resp->cons);
101     tid_resp_set_error_path(newresp, resp->error_path);
102   }
103   return newresp;
104 }
105
106 TR_EXPORT int tid_resp_get_result(TID_RESP *resp)
107 {
108   return(resp->result);
109 }
110
111 void tid_resp_set_result(TID_RESP *resp, int result)
112 {
113   resp->result = result;
114 }
115
116 TR_EXPORT TR_NAME *tid_resp_get_err_msg(TID_RESP *resp)
117 {
118   return(resp->err_msg);
119 }
120
121 void tid_resp_set_err_msg(TID_RESP *resp, TR_NAME *err_msg)
122 {
123   if (resp->err_msg!=NULL)
124     tr_free_name(resp->err_msg);
125
126   resp->err_msg = err_msg;
127 }
128
129 TR_EXPORT TR_NAME *tid_resp_get_rp_realm(TID_RESP *resp)
130 {
131   return(resp->rp_realm);
132 }
133
134 void tid_resp_set_rp_realm(TID_RESP *resp, TR_NAME *rp_realm)
135 {
136   resp->rp_realm = rp_realm;
137 }
138
139 TR_EXPORT TR_NAME *tid_resp_get_realm(TID_RESP *resp)
140 {
141   return(resp->realm);
142 }
143
144 void tid_resp_set_realm(TID_RESP *resp, TR_NAME *realm)
145 {
146   resp->realm = realm;
147 }
148
149 TR_EXPORT TR_NAME *tid_resp_get_comm(TID_RESP *resp)
150 {
151   return(resp->comm);
152 }
153
154 void tid_resp_set_comm(TID_RESP *resp, TR_NAME *comm)
155 {
156   resp->comm = comm;
157 }
158
159 TR_EXPORT TR_NAME *tid_resp_get_orig_coi(TID_RESP *resp)
160 {
161   return(resp->orig_coi);
162 }
163
164 void tid_resp_set_orig_coi(TID_RESP *resp, TR_NAME *orig_coi)
165 {
166   resp->orig_coi = orig_coi;
167 }
168
169 TR_EXPORT TID_SRVR_BLK *tid_resp_get_server(TID_RESP *resp,
170                                             size_t index)
171 {
172   TID_SRVR_BLK *this=NULL;
173   assert(resp);
174
175   for (this=resp->servers; index>0; index--, this=this->next) {}
176
177   return this;
178 }
179
180 size_t tid_resp_get_num_servers(const TID_RESP *resp)
181 {
182   size_t count=0;
183   TID_SRVR_BLK *this=NULL;
184
185   assert(resp!=NULL);
186   for (count=0, this=resp->servers; this!=NULL; count++, this=this->next) {}
187   return count;
188 }
189
190 static int tid_srvr_blk_destructor(void *obj)
191 {
192   TID_SRVR_BLK *srvr=talloc_get_type_abort(obj, TID_SRVR_BLK);
193
194   if (srvr->key_name!=NULL)
195     tr_free_name(srvr->key_name);
196   if (srvr->aaa_server_dh!=NULL)
197     tr_destroy_dh_params(srvr->aaa_server_dh);
198   if (srvr->path!=NULL)
199     json_decref((json_t *)(srvr->path));
200   return 0;
201 }
202
203 TR_EXPORT TID_SRVR_BLK *tid_srvr_blk_new(TALLOC_CTX *mem_ctx)
204 {
205   TID_SRVR_BLK *srvr=talloc(mem_ctx, TID_SRVR_BLK);
206
207   if (srvr!=NULL) {
208     srvr->next=NULL;
209     srvr->aaa_server_addr=NULL;
210     srvr->key_name=NULL;
211     srvr->aaa_server_dh=NULL;
212     srvr->key_expiration=(GTimeVal){0};
213     srvr->path=NULL;
214     talloc_set_destructor((void *)srvr, tid_srvr_blk_destructor);
215   }
216   return srvr;
217 }
218
219 TR_EXPORT void tid_srvr_blk_free(TID_SRVR_BLK *srvr)
220 {
221   talloc_free(srvr);
222 }
223
224 TR_EXPORT TID_SRVR_BLK *tid_srvr_blk_dup(TALLOC_CTX *mem_ctx, TID_SRVR_BLK *srvr)
225 {
226   TID_SRVR_BLK *new=NULL;
227
228   if (srvr==NULL)
229     return NULL;
230
231   new=tid_srvr_blk_new(mem_ctx);
232   if (new!=NULL) {
233     if (srvr->aaa_server_addr!=NULL)
234       new->aaa_server_addr=talloc_strdup(new, srvr->aaa_server_addr);
235     new->key_name=tr_dup_name(srvr->key_name);
236     new->aaa_server_dh=tr_dh_dup(srvr->aaa_server_dh);
237     new->key_expiration=srvr->key_expiration;
238     
239     tid_srvr_blk_set_path(new, srvr->path);
240
241     tid_srvr_blk_add(new->next, tid_srvr_blk_dup(mem_ctx, srvr->next));
242   }
243   return new;
244 }
245
246 /* use the macro */
247 TR_EXPORT TID_SRVR_BLK *tid_srvr_blk_add_func(TID_SRVR_BLK *head, TID_SRVR_BLK *new)
248 {
249   TID_SRVR_BLK *this=head;
250
251   if (head==NULL)
252     return new;
253
254   while (this->next!=NULL)
255     this=this->next;
256   
257   this->next=new;
258   while (this!=NULL) {
259     talloc_steal(head, this);
260     this=this->next;
261   }
262   return head;
263 }
264
265 TR_EXPORT void tid_srvr_blk_set_path(TID_SRVR_BLK *block, TID_PATH *path)
266 {
267   if (block->path!=NULL)
268     json_decref((json_t *)(block->path));
269   block->path=path;
270   if (block->path!=NULL)
271     json_incref((json_t *)(block->path));
272 }
273
274 TR_EXPORT const TID_PATH *tid_srvr_get_path( const TID_SRVR_BLK *block)
275 {
276   if (!block)
277     return NULL;
278   return block->path;
279 }
280
281 TR_EXPORT void tid_resp_set_cons(TID_RESP *resp, TR_CONSTRAINT_SET *cons)
282 {
283   json_t *jc=(json_t *)cons;
284
285   if (resp->cons!=NULL)
286     json_decref((json_t *) (resp->cons));
287
288   resp->cons=(TR_CONSTRAINT_SET *)jc;
289   if (jc!=NULL)
290     json_incref(jc);
291 }
292
293 TR_EXPORT void tid_resp_set_error_path(TID_RESP *resp, json_t *ep)
294 {
295   if (resp->error_path!=NULL)
296     json_decref(resp->error_path);
297   resp->error_path=ep;
298   if (resp->error_path!=NULL)
299     json_incref(resp->error_path);
300 }
301
302 TR_EXPORT const TID_PATH *tid_resp_get_error_path(const TID_RESP *resp)
303 {
304   if (!resp)
305     return NULL;
306   return (const TID_PATH *)(resp->error_path);
307 }
308
309 TR_EXPORT const TID_PATH *tid_resp_get_a_path(const TID_RESP *const_resp)
310 {
311   size_t index;
312   TID_SRVR_BLK *server;
313   TID_RESP *resp = (TID_RESP *) const_resp;
314   if (!resp)
315     return NULL;
316
317   if (resp->error_path)
318     return (const TID_PATH *)(resp->error_path);
319   tid_resp_servers_foreach( resp, server, index) {
320     if (server->path)
321       return server->path;
322   }
323   return NULL;
324   
325 }