2 * Copyright (c) 2012, JANET(UK)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
38 #include <tr_aaa_server.h>
39 #include <tr_name_internal.h>
41 #include <tr_config.h>
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)
47 TR_IDP_REALM *idp = NULL;
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 */
57 *shared_out=idp->shared_config;
58 return idp->aaa_servers;
63 TR_AAA_SERVER *tr_default_server_lookup(TR_AAA_SERVER *default_servers, TR_NAME *comm)
68 return(default_servers);
71 static int tr_idp_realm_destructor(void *obj)
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);
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)
85 TR_IDP_REALM *idp=talloc(mem_ctx, TR_IDP_REALM);
91 idp->aaa_servers=NULL;
93 idp->origin=TR_REALM_LOCAL;
95 talloc_set_destructor((void *)idp, tr_idp_realm_destructor);
100 void tr_idp_realm_free(TR_IDP_REALM *idp)
105 TR_NAME *tr_idp_realm_get_id(TR_IDP_REALM *idp)
110 return idp->realm_id;
113 TR_NAME *tr_idp_realm_dup_id(TR_IDP_REALM *idp)
118 return tr_dup_name(tr_idp_realm_get_id(idp));
121 void tr_idp_realm_set_id(TR_IDP_REALM *idp, TR_NAME *id)
123 if (idp->realm_id!=NULL)
124 tr_free_name(idp->realm_id);
128 void tr_idp_realm_set_apcs(TR_IDP_REALM *idp, TR_APC *apc)
131 tr_apc_free(idp->apcs);
133 talloc_steal(idp, apc);
136 TR_APC *tr_idp_realm_get_apcs(TR_IDP_REALM *idp)
141 TR_IDP_REALM *tr_idp_realm_lookup(TR_IDP_REALM *idp_realms, TR_NAME *idp_name)
143 TR_IDP_REALM *idp = NULL;
146 tr_debug("tr_idp_realm_lookup: Bad parameters.");
150 for (idp=idp_realms; NULL!=idp; idp=idp->next) {
151 if (0==tr_name_cmp(tr_idp_realm_get_id(idp), idp_name))
158 static TR_IDP_REALM *tr_idp_realm_tail(TR_IDP_REALM *idp)
163 while (idp->next!=NULL)
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)
174 tr_idp_realm_tail(head)->next=new;
176 talloc_steal(head, new); /* put it in the right context */
184 TR_IDP_REALM *tr_idp_realm_remove_func(TR_IDP_REALM *head, TR_IDP_REALM *remove)
186 TALLOC_CTX *list_ctx=talloc_parent(head);
187 TR_IDP_REALM *this=NULL;
193 /* if we're removing the head, put the next element (if present) into the context
194 * the list head was in. */
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);
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;
214 int tr_idp_realm_apc_count(TR_IDP_REALM *idp)
217 TR_APC *apc=idp->apcs;
225 int tr_idp_realm_aaa_server_count(TR_IDP_REALM *idp)
228 TR_AAA_SERVER *aaa=idp->aaa_servers;
236 void tr_idp_realm_incref(TR_IDP_REALM *realm)
241 void tr_idp_realm_decref(TR_IDP_REALM *realm)
243 if (realm->refcount>0)
247 /* remove any with zero refcount
249 TR_IDP_REALM *tr_idp_realm_sweep_func(TR_IDP_REALM *head)
251 TR_IDP_REALM *idp=NULL;
252 TR_IDP_REALM *old_next=NULL;
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);
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) {
270 tr_idp_realm_remove(head, idp->next); /* changes idp->next, may make it NULL */
271 tr_idp_realm_free(old_next);