Further work on tids and monitoring, tids appears to work again
[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 /**
84  * Allocate a new copy of a TID_RESP
85  *
86  * @param mem_ctx
87  * @param resp
88  * @return
89  */
90 TID_RESP *tid_resp_dup(TALLOC_CTX *mem_ctx, TID_RESP *resp)
91 {
92   TID_RESP *newresp=NULL;
93
94   if (resp==NULL)
95     return NULL;
96
97   newresp=tid_resp_new(mem_ctx);
98
99   if (NULL!=newresp) {
100     tid_resp_cpy(newresp, resp);
101   }
102   return newresp;
103 }
104
105 /**
106  * Copy contents of one TID_RESP to an existing TID_RESP
107  *
108  * @param dst
109  * @param src
110  * @return TID_SUCCESS on success, error code on error
111  */
112 TID_RC tid_resp_cpy(TID_RESP *dst, TID_RESP *src)
113 {
114   tid_resp_set_result(dst, tid_resp_get_result(src));
115   tid_resp_set_err_msg(dst,
116                        tr_dup_name(tid_resp_get_err_msg(src)));
117   tid_resp_set_rp_realm(dst,
118                         tr_dup_name(tid_resp_get_rp_realm(src)));
119   tid_resp_set_realm(dst,
120                      tr_dup_name(tid_resp_get_realm(src)));
121   tid_resp_set_comm(dst,
122                     tr_dup_name(tid_resp_get_comm(src)));
123   tid_resp_set_cons(dst, src->cons);
124   tid_resp_set_orig_coi(dst,
125                         tr_dup_name(tid_resp_get_orig_coi(src)));
126   dst->servers = tid_srvr_blk_dup(dst, src->servers);
127   tid_resp_set_error_path(dst, src->error_path);
128
129   return TID_SUCCESS;
130 }
131
132 TR_EXPORT int tid_resp_get_result(TID_RESP *resp)
133 {
134   return(resp->result);
135 }
136
137 void tid_resp_set_result(TID_RESP *resp, int result)
138 {
139   resp->result = result;
140 }
141
142 TR_EXPORT TR_NAME *tid_resp_get_err_msg(TID_RESP *resp)
143 {
144   return(resp->err_msg);
145 }
146
147 void tid_resp_set_err_msg(TID_RESP *resp, TR_NAME *err_msg)
148 {
149   if (resp->err_msg!=NULL)
150     tr_free_name(resp->err_msg);
151
152   resp->err_msg = err_msg;
153 }
154
155 TR_EXPORT TR_NAME *tid_resp_get_rp_realm(TID_RESP *resp)
156 {
157   return(resp->rp_realm);
158 }
159
160 void tid_resp_set_rp_realm(TID_RESP *resp, TR_NAME *rp_realm)
161 {
162   resp->rp_realm = rp_realm;
163 }
164
165 TR_EXPORT TR_NAME *tid_resp_get_realm(TID_RESP *resp)
166 {
167   return(resp->realm);
168 }
169
170 void tid_resp_set_realm(TID_RESP *resp, TR_NAME *realm)
171 {
172   resp->realm = realm;
173 }
174
175 TR_EXPORT TR_NAME *tid_resp_get_comm(TID_RESP *resp)
176 {
177   return(resp->comm);
178 }
179
180 void tid_resp_set_comm(TID_RESP *resp, TR_NAME *comm)
181 {
182   resp->comm = comm;
183 }
184
185 TR_EXPORT TR_NAME *tid_resp_get_orig_coi(TID_RESP *resp)
186 {
187   return(resp->orig_coi);
188 }
189
190 void tid_resp_set_orig_coi(TID_RESP *resp, TR_NAME *orig_coi)
191 {
192   resp->orig_coi = orig_coi;
193 }
194
195 TR_EXPORT TID_SRVR_BLK *tid_resp_get_server(TID_RESP *resp,
196                                             size_t index)
197 {
198   TID_SRVR_BLK *this=NULL;
199   assert(resp);
200
201   for (this=resp->servers; index>0; index--, this=this->next) {}
202
203   return this;
204 }
205
206 size_t tid_resp_get_num_servers(const TID_RESP *resp)
207 {
208   size_t count=0;
209   TID_SRVR_BLK *this=NULL;
210
211   assert(resp!=NULL);
212   for (count=0, this=resp->servers; this!=NULL; count++, this=this->next) {}
213   return count;
214 }
215
216 static int tid_srvr_blk_destructor(void *obj)
217 {
218   TID_SRVR_BLK *srvr=talloc_get_type_abort(obj, TID_SRVR_BLK);
219
220   if (srvr->key_name!=NULL)
221     tr_free_name(srvr->key_name);
222   if (srvr->aaa_server_dh!=NULL)
223     tr_destroy_dh_params(srvr->aaa_server_dh);
224   if (srvr->path!=NULL)
225     json_decref((json_t *)(srvr->path));
226   return 0;
227 }
228
229 TR_EXPORT TID_SRVR_BLK *tid_srvr_blk_new(TALLOC_CTX *mem_ctx)
230 {
231   TID_SRVR_BLK *srvr=talloc(mem_ctx, TID_SRVR_BLK);
232
233   if (srvr!=NULL) {
234     srvr->next=NULL;
235     srvr->aaa_server_addr=NULL;
236     srvr->key_name=NULL;
237     srvr->aaa_server_dh=NULL;
238     srvr->key_expiration=(GTimeVal){0};
239     srvr->path=NULL;
240     talloc_set_destructor((void *)srvr, tid_srvr_blk_destructor);
241   }
242   return srvr;
243 }
244
245 TR_EXPORT void tid_srvr_blk_free(TID_SRVR_BLK *srvr)
246 {
247   talloc_free(srvr);
248 }
249
250 TR_EXPORT TID_SRVR_BLK *tid_srvr_blk_dup(TALLOC_CTX *mem_ctx, TID_SRVR_BLK *srvr)
251 {
252   TID_SRVR_BLK *new=NULL;
253
254   if (srvr==NULL)
255     return NULL;
256
257   new=tid_srvr_blk_new(mem_ctx);
258   if (new!=NULL) {
259     if (srvr->aaa_server_addr!=NULL)
260       new->aaa_server_addr=talloc_strdup(new, srvr->aaa_server_addr);
261     new->key_name=tr_dup_name(srvr->key_name);
262     new->aaa_server_dh=tr_dh_dup(srvr->aaa_server_dh);
263     new->key_expiration=srvr->key_expiration;
264     
265     tid_srvr_blk_set_path(new, srvr->path);
266
267     tid_srvr_blk_add(new->next, tid_srvr_blk_dup(mem_ctx, srvr->next));
268   }
269   return new;
270 }
271
272 /* use the macro */
273 TR_EXPORT TID_SRVR_BLK *tid_srvr_blk_add_func(TID_SRVR_BLK *head, TID_SRVR_BLK *new)
274 {
275   TID_SRVR_BLK *this=head;
276
277   if (head==NULL)
278     return new;
279
280   while (this->next!=NULL)
281     this=this->next;
282   
283   this->next=new;
284   while (this!=NULL) {
285     talloc_steal(head, this);
286     this=this->next;
287   }
288   return head;
289 }
290
291 TR_EXPORT void tid_srvr_blk_set_path(TID_SRVR_BLK *block, TID_PATH *path)
292 {
293   if (block->path!=NULL)
294     json_decref((json_t *)(block->path));
295   block->path=path;
296   if (block->path!=NULL)
297     json_incref((json_t *)(block->path));
298 }
299
300 TR_EXPORT const TID_PATH *tid_srvr_get_path( const TID_SRVR_BLK *block)
301 {
302   if (!block)
303     return NULL;
304   return block->path;
305 }
306
307 TR_EXPORT int tid_srvr_get_key_expiration(const TID_SRVR_BLK *block, struct timeval *tv_out)
308 {
309   if ((block==NULL) || (tv_out==NULL))
310     return -1; /* error */
311
312   tv_out->tv_sec=block->key_expiration.tv_sec;
313   tv_out->tv_usec=block->key_expiration.tv_usec;
314   return 0;
315 }
316
317
318 TR_EXPORT void tid_resp_set_cons(TID_RESP *resp, TR_CONSTRAINT_SET *cons)
319 {
320   json_t *jc=(json_t *)cons;
321
322   if (resp->cons!=NULL)
323     json_decref((json_t *) (resp->cons));
324
325   resp->cons=(TR_CONSTRAINT_SET *)jc;
326   if (jc!=NULL)
327     json_incref(jc);
328 }
329
330 TR_EXPORT void tid_resp_set_error_path(TID_RESP *resp, json_t *ep)
331 {
332   if (resp->error_path!=NULL)
333     json_decref(resp->error_path);
334   resp->error_path=ep;
335   if (resp->error_path!=NULL)
336     json_incref(resp->error_path);
337 }
338
339 TR_EXPORT const TID_PATH *tid_resp_get_error_path(const TID_RESP *resp)
340 {
341   if (!resp)
342     return NULL;
343   return (const TID_PATH *)(resp->error_path);
344 }
345
346 TR_EXPORT const TID_PATH *tid_resp_get_a_path(const TID_RESP *const_resp)
347 {
348   size_t index;
349   TID_SRVR_BLK *server;
350   TID_RESP *resp = (TID_RESP *) const_resp;
351   if (!resp)
352     return NULL;
353
354   if (resp->error_path)
355     return (const TID_PATH *)(resp->error_path);
356   tid_resp_servers_foreach( resp, server, index) {
357     if (server->path)
358       return server->path;
359   }
360   return NULL;
361   
362 }