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