50a9330d534669b29a377189022b00034896b0f3
[trust_router.git] / common / tr_idp.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 <talloc.h>
36 #include <time.h>
37
38 #include <tr_name_internal.h>
39 #include <tr_idp.h>
40 #include <tr_config.h>
41 #include <tr_debug.h>
42
43 static int tr_aaa_server_destructor(void *obj)
44 {
45   TR_AAA_SERVER *aaa=talloc_get_type_abort(obj, TR_AAA_SERVER);
46   if (aaa->hostname!=NULL)
47     tr_free_name(aaa->hostname);
48   return 0;
49 }
50
51 TR_AAA_SERVER *tr_aaa_server_new(TALLOC_CTX *mem_ctx, TR_NAME *hostname)
52 {
53   TR_AAA_SERVER *aaa=talloc(mem_ctx, TR_AAA_SERVER);
54   if (aaa!=NULL) {
55     aaa->next=NULL;
56     aaa->hostname=hostname;
57     talloc_set_destructor((void *)aaa, tr_aaa_server_destructor);
58   }
59   return aaa;
60 }
61
62 void tr_aaa_server_free(TR_AAA_SERVER *aaa)
63 {
64   talloc_free(aaa);
65 }
66
67 TR_AAA_SERVER_ITER *tr_aaa_server_iter_new(TALLOC_CTX *mem_ctx)
68 {
69   return talloc(mem_ctx, TR_AAA_SERVER_ITER);
70 }
71
72 void tr_aaa_server_iter_free(TR_AAA_SERVER_ITER *iter)
73 {
74   talloc_free(iter);
75 }
76
77 TR_AAA_SERVER *tr_aaa_server_iter_first(TR_AAA_SERVER_ITER *iter, TR_AAA_SERVER *aaa)
78 {
79   iter->this=aaa;
80   return iter->this;
81 }
82
83 TR_AAA_SERVER *tr_aaa_server_iter_next(TR_AAA_SERVER_ITER *iter)
84 {
85   if (iter->this!=NULL) {
86     iter->this=iter->this->next;
87   }
88   return iter->this;
89 }
90
91
92 /* fills in shared if pointer not null */
93 TR_AAA_SERVER *tr_idp_aaa_server_lookup(TR_IDP_REALM *idp_realms, TR_NAME *idp_realm_name, TR_NAME *comm, int *shared_out)
94 {
95   TR_IDP_REALM *idp = NULL;
96
97   for (idp = idp_realms; idp != NULL; idp = idp->next) {
98     if (!tr_name_cmp(idp_realm_name, idp->realm_id)) {
99       /* TBD -- check that the community is one of the APCs for the IDP */
100       break;
101     }
102   }
103   if (idp) {
104     if (shared_out!=NULL)
105       *shared_out=idp->shared_config;
106     return idp->aaa_servers;
107   } else 
108     return NULL;
109 }
110
111 TR_AAA_SERVER *tr_default_server_lookup(TR_AAA_SERVER *default_servers, TR_NAME *comm)
112 {
113   if (!default_servers)
114     return NULL;
115
116   return(default_servers);
117 }
118
119 static int tr_idp_realm_destructor(void *obj)
120 {
121   TR_IDP_REALM *idp=talloc_get_type_abort(obj, TR_IDP_REALM);
122   if (idp->realm_id!=NULL)
123     tr_free_name(idp->realm_id);
124   return 0;
125 }
126
127 /* talloc note: lists of idp realms should be assembled using
128  * tr_idp_realm_add(). This will put all of the elements in the
129  * list, other than the head, as children of the head context.
130  * The head can then be placed in whatever context is desired. */
131 TR_IDP_REALM *tr_idp_realm_new(TALLOC_CTX *mem_ctx)
132 {
133   TR_IDP_REALM *idp=talloc(mem_ctx, TR_IDP_REALM);
134   if (idp!=NULL) {
135     idp->next=NULL;
136     idp->comm_next=NULL;
137     idp->realm_id=NULL;
138     idp->shared_config=0;
139     idp->aaa_servers=NULL;
140     idp->apcs=NULL;
141     idp->origin=TR_REALM_LOCAL;
142     idp->refcount=0;
143     talloc_set_destructor((void *)idp, tr_idp_realm_destructor);
144   }
145   return idp;
146 }
147
148 void tr_idp_realm_free(TR_IDP_REALM *idp)
149 {
150   talloc_free(idp);
151 }
152
153 TR_NAME *tr_idp_realm_get_id(TR_IDP_REALM *idp)
154 {
155   if (idp==NULL)
156     return NULL;
157   
158   return idp->realm_id;
159 }
160
161 TR_NAME *tr_idp_realm_dup_id(TR_IDP_REALM *idp)
162 {
163   if (idp==NULL)
164     return NULL;
165   
166   return tr_dup_name(tr_idp_realm_get_id(idp));
167 }
168
169 void tr_idp_realm_set_id(TR_IDP_REALM *idp, TR_NAME *id)
170 {
171   if (idp->realm_id!=NULL)
172     tr_free_name(idp->realm_id);
173   idp->realm_id=id;
174 }
175
176 void tr_idp_realm_set_apcs(TR_IDP_REALM *idp, TR_APC *apc)
177 {
178   if (idp->apcs!=NULL)
179     tr_apc_free(idp->apcs);
180   idp->apcs=apc;
181   talloc_steal(idp, apc);
182 }
183
184 TR_APC *tr_idp_realm_get_apcs(TR_IDP_REALM *idp)
185 {
186   return idp->apcs;
187 }
188
189 TR_IDP_REALM *tr_idp_realm_lookup(TR_IDP_REALM *idp_realms, TR_NAME *idp_name)
190 {
191   TR_IDP_REALM *idp = NULL;
192
193   if (!idp_name) {
194     tr_debug("tr_idp_realm_lookup: Bad parameters.");
195     return NULL;
196   }
197
198   for (idp=idp_realms; NULL!=idp; idp=idp->next) {
199     if (0==tr_name_cmp(tr_idp_realm_get_id(idp), idp_name))
200       return idp;
201   } 
202   return NULL;
203 }
204
205
206 static TR_IDP_REALM *tr_idp_realm_tail(TR_IDP_REALM *idp)
207 {
208   if (idp==NULL)
209     return NULL;
210
211   while (idp->next!=NULL)
212     idp=idp->next;
213   return idp;
214 }
215
216 /* do not call directly, use the tr_idp_realm_add() macro */
217 TR_IDP_REALM *tr_idp_realm_add_func(TR_IDP_REALM *head, TR_IDP_REALM *new)
218 {
219   if (head==NULL)
220     head=new;
221   else {
222     tr_idp_realm_tail(head)->next=new;
223     while (new!=NULL) {
224       talloc_steal(head, new); /* put it in the right context */
225       new=new->next;
226     }
227   }
228   return head;
229 }
230
231 /* use the macro */
232 TR_IDP_REALM *tr_idp_realm_remove_func(TR_IDP_REALM *head, TR_IDP_REALM *remove)
233 {
234   TALLOC_CTX *list_ctx=talloc_parent(head);
235   TR_IDP_REALM *this=NULL;
236
237   if (head==NULL)
238     return NULL;
239
240   if (head==remove) {
241     /* if we're removing the head, put the next element (if present) into the context
242      * the list head was in. */
243     head=head->next;
244     if (head!=NULL) {
245       talloc_steal(list_ctx, head);
246       /* now put all the other elements in the context of the list head */
247       for (this=head->next; this!=NULL; this=this->next)
248         talloc_steal(head, this);
249     }
250   } else {
251     /* not removing the head; no need to play with contexts */
252     for (this=head; this->next!=NULL; this=this->next) {
253       if (this->next==remove) {
254         this->next=remove->next;
255         break;
256       }
257     }
258   }
259   return head;
260 }
261
262 int tr_idp_realm_apc_count(TR_IDP_REALM *idp)
263 {
264   int ii=0;
265   TR_APC *apc=idp->apcs;
266   while (apc!=NULL) {
267     apc=apc->next;
268     ii++;
269   }
270   return ii;
271 }
272
273 int tr_idp_realm_aaa_server_count(TR_IDP_REALM *idp)
274 {
275   int ii=0;
276   TR_AAA_SERVER *aaa=idp->aaa_servers;
277   while (aaa!=NULL) {
278     aaa=aaa->next;
279     ii++;
280   }
281   return ii;
282 }
283
284 void tr_idp_realm_incref(TR_IDP_REALM *realm)
285 {
286   realm->refcount++;
287 }
288
289 void tr_idp_realm_decref(TR_IDP_REALM *realm)
290 {
291   if (realm->refcount>0)
292     realm->refcount--;
293 }
294
295 /* remove any with zero refcount 
296  * Call via macro. */
297 TR_IDP_REALM *tr_idp_realm_sweep_func(TR_IDP_REALM *head)
298 {
299   TR_IDP_REALM *idp=NULL;
300   TR_IDP_REALM *old_next=NULL;
301
302   if (head==NULL)
303     return NULL;
304
305   while ((head!=NULL) && (head->refcount==0)) {
306     idp=head; /* keep a pointer so we can remove it */
307     tr_idp_realm_remove(head, idp); /* use this to get talloc contexts right */
308     tr_idp_realm_free(idp);
309   }
310
311   if (head==NULL)
312     return NULL;
313
314   /* Will not remove the head here, that has already been done.*/
315   for (idp=head; (idp!=NULL) && (idp->next!=NULL); idp=idp->next) {
316     if (idp->next->refcount==0) {
317       old_next=idp->next;
318       tr_idp_realm_remove(head, idp->next); /* changes idp->next, may make it NULL */
319       tr_idp_realm_free(old_next);
320     }
321   }
322
323   return head;
324 }
325