Add fields to TR_COMM records. Add accessor functions.
[trust_router.git] / common / tr_comm.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
37 #include <tr_rp.h>
38 #include <trust_router/tr_name.h>
39 #include <tr_comm.h>
40 #include <tr_debug.h>
41
42 static int tr_comm_destructor(void *obj)
43 {
44   TR_COMM *comm=talloc_get_type_abort(obj, TR_COMM);
45   if (comm->id!=NULL)
46     tr_free_name(comm->id);
47   if (comm->owner_realm!=NULL)
48     tr_free_name(comm->owner_realm);
49   if (comm->owner_email!=NULL)
50     tr_free_name(comm->owner_email);
51   return 0;
52 }
53
54 TR_COMM *tr_comm_new(TALLOC_CTX *mem_ctx)
55 {
56   TR_COMM *comm=talloc(mem_ctx, TR_COMM);
57   if (comm!=NULL) {
58     comm->next=NULL;
59     comm->id=NULL;
60     comm->type=TR_COMM_UNKNOWN;
61     comm->apcs=NULL;
62     comm->idp_realms=NULL;
63     comm->rp_realms=NULL;
64     comm->owner_realm=NULL;
65     comm->owner_contact=NULL;
66     talloc_set_destructor((void *)comm, tr_comm_destructor);
67   }
68   return comm;
69 }
70
71 void tr_comm_free(TR_COMM *comm)
72 {
73   talloc_free(comm);
74 }
75
76 void tr_comm_set_id(TR_COMM *comm, TR_NAME *id)
77 {
78   if (comm->id != NULL)
79     tr_free_name(comm->id);
80   comm->id=id;
81 }
82
83 TR_NAME *tr_comm_get_id(TR_COMM *comm)
84 {
85   return comm->id;
86 }
87
88 TR_NAME *tr_comm_dup_id(TR_COMM *comm)
89 {
90   return tr_dup_name(comm->id);
91 }
92
93 void tr_comm_set_type(TR_COMM *comm, TR_COMM_TYPE type)
94 {
95   comm->type=type;
96 }
97
98 TR_COMM_TYPE tr_comm_get_type(TR_COMM *comm)
99 {
100   return comm->type;
101 }
102
103 void tr_comm_set_owner_realm(TR_COMM *comm, TR_NAME *realm)
104 {
105   if (comm->owner_realm!=NULL)
106     tr_free_name(comm->owner_realm);
107   comm->owner_realm=realm;
108 }
109
110 TR_NAME *tr_comm_get_owner_realm(TR_COMM *comm)
111 {
112   return comm->owner_realm;
113 }
114
115 TR_NAME *tr_comm_dup_owner_realm(TR_COMM *comm)
116 {
117   return tr_dup_name(tr->owner_realm);
118 }
119
120 void tr_comm_set_owner_contact(TR_COMM *comm, TR_NAME *contact)
121 {
122   if (comm->owner_contact != NULL)
123     tr_free_name(comm->owner_contact);
124   comm->owner_contact=contact;
125 }
126
127 TR_NAME *tr_comm_get_owner_contact(TR_COMM *comm)
128 {
129   return comm->owner_contact;
130 }
131
132 TR_NAME *tr_comm_dup_owner_contact(TR_COMM *comm)
133 {
134   return tr_dup_name(tr->owner_contact);
135 }
136
137 /* does not take responsibility for freeing IDP realm */
138 void tr_comm_add_idp_realm(TR_COMM *comm, TR_IDP_REALM *realm)
139 {
140   TR_IDP_REALM *cur=NULL;
141
142   if (comm->idp_realms==NULL)
143     comm->idp_realms=realm;
144   else {
145     for (cur=comm->idp_realms; cur->comm_next!=NULL; cur=cur->comm_next) { }
146     cur->comm_next=realm;
147   }
148 }
149
150 /* does not take responsibility for freeing RP realm */
151 void tr_comm_add_rp_realm(TR_COMM *comm, TR_RP_REALM *realm)
152 {
153   TR_RP_REALM *cur=NULL;
154
155   if (comm->rp_realms==NULL)
156     comm->rp_realms=realm;
157   else {
158     for (cur=comm->rp_realms; cur->next!=NULL; cur=cur->next) { }
159     cur->next=realm;
160   }
161 }
162
163 static TR_COMM *tr_comm_tail(TR_COMM *comm)
164 {
165   if (comm==NULL)
166     return comm;
167
168   while (comm->next!=NULL)
169     comm=comm->next;
170   return comm;
171 }
172
173 /* All list members are in the talloc context of the head.
174  * This will require careful thought if entries are ever removed
175  * Call like comms=tr_comm_add_func(comms, new_comm);
176  * or just use the tr_comm_add(comms, new) macro. */
177 TR_COMM *tr_comm_add_func(TR_COMM *comms, TR_COMM *new)
178 {
179   if (comms==NULL)
180     comms=new;
181   else {
182     tr_comm_tail(comms)->next=new;
183     while(new!=NULL) {
184       talloc_steal(comms, new);
185       new=new->next;
186     }
187   }
188   return comms;
189 }
190
191 TR_IDP_REALM *tr_find_comm_idp (TR_COMM *comm, TR_NAME *idp_realm)
192 {
193   TR_IDP_REALM *idp;
194
195   if ((!comm) || (!idp_realm)) {
196     return NULL;
197   }
198
199   for (idp = comm->idp_realms; NULL != idp; idp = idp->comm_next) {
200     if (!tr_name_cmp (idp_realm, idp->realm_id)) {
201       tr_debug("tr_find_comm_idp: Found IdP %s in community %s.", idp_realm->buf, comm->id->buf);
202       return idp;
203     }
204   }
205   /* if we didn't find one, return NULL */ 
206   return NULL;
207 }
208
209 TR_RP_REALM *tr_find_comm_rp (TR_COMM *comm, TR_NAME *rp_realm)
210 {
211   TR_RP_REALM *rp;
212
213   if ((!comm) || (!rp_realm)) {
214     return NULL;
215   }
216
217   for (rp = comm->rp_realms; NULL != rp; rp = rp->next) {
218     if (!tr_name_cmp (rp_realm, rp->realm_name)) {
219       tr_debug("tr_find_comm_rp: Found RP %s in community %s.", rp_realm->buf, comm->id->buf);
220       return rp;
221     }
222   }
223   /* if we didn't find one, return NULL */ 
224   return NULL;
225 }
226
227 TR_COMM *tr_comm_lookup(TR_COMM *comms, TR_NAME *comm_name) 
228 {
229   TR_COMM *cfg_comm = NULL;
230
231   for (cfg_comm = comms; NULL != cfg_comm; cfg_comm = cfg_comm->next) {
232     if ((cfg_comm->id->len == comm_name->len) &&
233         (!strncmp(cfg_comm->id->buf, comm_name->buf, comm_name->len)))
234       return cfg_comm;
235   }
236   return NULL;
237 }