10bb2c3cd4c725f51064e5cddf39c249056feb27
[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 <trust_router/tr_name.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     talloc_set_destructor((void *)idp, tr_idp_realm_destructor);
143   }
144   return idp;
145 }
146
147 void tr_idp_realm_free(TR_IDP_REALM *idp)
148 {
149   talloc_free(idp);
150 }
151
152 TR_NAME *tr_idp_realm_get_id(TR_IDP_REALM *idp)
153 {
154   if (idp==NULL)
155     return NULL;
156   
157   return idp->realm_id;
158 }
159
160 TR_NAME *tr_idp_realm_dup_id(TR_IDP_REALM *idp)
161 {
162   if (idp==NULL)
163     return NULL;
164   
165   return tr_dup_name(tr_idp_realm_get_id(idp));
166 }
167
168 void tr_idp_realm_set_id(TR_IDP_REALM *idp, TR_NAME *id)
169 {
170   if (idp->realm_id!=NULL)
171     tr_free_name(idp->realm_id);
172   idp->realm_id=id;
173 }
174
175 void tr_idp_realm_set_apcs(TR_IDP_REALM *idp, TR_APC *apc)
176 {
177   if (idp->apcs!=NULL)
178     tr_apc_free(idp->apcs);
179   idp->apcs=apc;
180   talloc_steal(idp, apc);
181 }
182
183 TR_APC *tr_idp_realm_get_apcs(TR_IDP_REALM *idp)
184 {
185   return idp->apcs;
186 }
187
188 TR_IDP_REALM *tr_idp_realm_lookup(TR_IDP_REALM *idp_realms, TR_NAME *idp_name)
189 {
190   TR_IDP_REALM *idp = NULL;
191
192   if (!idp_name) {
193     tr_debug("tr_idp_realm_lookup: Bad parameters.");
194     return NULL;
195   }
196
197   for (idp=idp_realms; NULL!=idp; idp=idp->next) {
198     if (0==tr_name_cmp(tr_idp_realm_get_id(idp), idp_name))
199       return idp;
200   } 
201   return NULL;
202 }
203
204
205 static TR_IDP_REALM *tr_idp_realm_tail(TR_IDP_REALM *idp)
206 {
207   if (idp==NULL)
208     return NULL;
209
210   while (idp->next!=NULL)
211     idp=idp->next;
212   return idp;
213 }
214
215 /* do not call directly, use the tr_idp_realm_add() macro */
216 TR_IDP_REALM *tr_idp_realm_add_func(TR_IDP_REALM *head, TR_IDP_REALM *new)
217 {
218   if (head==NULL)
219     head=new;
220   else {
221     tr_idp_realm_tail(head)->next=new;
222     while (new!=NULL) {
223       talloc_steal(head, new); /* put it in the right context */
224       new=new->next;
225     }
226   }
227   return head;
228 }
229
230 /* use the macro */
231 TR_IDP_REALM *tr_idp_realm_remove_func(TR_IDP_REALM *head, TR_IDP_REALM *remove)
232 {
233   TALLOC_CTX *list_ctx=talloc_parent(head);
234   TR_IDP_REALM *this=NULL;
235
236   if (head==NULL)
237     return NULL;
238
239   if (head==remove) {
240     /* if we're removing the head, put the next element (if present) into the context
241      * the list head was in. */
242     head=head->next;
243     if (head!=NULL) {
244       talloc_steal(list_ctx, head);
245       /* now put all the other elements in the context of the list head */
246       for (this=head->next; this!=NULL; this=this->next)
247         talloc_steal(head, this);
248     }
249   } else {
250     /* not removing the head; no need to play with contexts */
251     for (this=head; this->next!=NULL; this=this->next) {
252       if (this->next==remove) {
253         this->next=remove->next;
254         break;
255       }
256     }
257   }
258   return head;
259 }
260
261 static int tr_idp_realm_apc_count(TR_IDP_REALM *idp)
262 {
263   int ii=0;
264   TR_APC *apc=idp->apcs;
265   while (apc!=NULL) {
266     apc=apc->next;
267     ii++;
268   }
269   return ii;
270 }
271
272 static int tr_idp_realm_aaa_server_count(TR_IDP_REALM *idp)
273 {
274   int ii=0;
275   TR_AAA_SERVER *aaa=idp->aaa_servers;
276   while (aaa!=NULL) {
277     aaa=aaa->next;
278     ii++;
279   }
280   return ii;
281 }
282
283 static char *tr_aaa_server_to_str(TALLOC_CTX *mem_ctx, TR_AAA_SERVER *aaa)
284 {
285   return talloc_strndup(mem_ctx, aaa->hostname->buf, aaa->hostname->len);
286 }
287
288 char *tr_idp_realm_to_str(TALLOC_CTX *mem_ctx, TR_IDP_REALM *idp)
289 {
290   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
291   char **s_aaa=NULL, *aaa_servers=NULL;
292   char **s_apc=NULL, *apcs=NULL;
293   int ii=0, aaa_servers_strlen=0, apcs_strlen=0;
294   int n_aaa_servers=tr_idp_realm_aaa_server_count(idp);
295   int n_apcs=tr_idp_realm_apc_count(idp);
296   TR_AAA_SERVER *aaa=NULL;
297   TR_APC *apc=NULL;
298   char *result=NULL;
299
300   /* get the AAA servers */
301   if (n_aaa_servers<=0)
302     aaa_servers=talloc_strdup(tmp_ctx, "");
303   else {
304     s_aaa=talloc_array(tmp_ctx, char *, n_aaa_servers);
305     for (aaa=idp->aaa_servers,ii=0; aaa!=NULL; aaa=aaa->next,ii++) {
306       s_aaa[ii]=tr_aaa_server_to_str(s_aaa, aaa);
307       aaa_servers_strlen+=strlen(s_aaa[ii]);
308     }
309
310     /* add space for comma-space separators */
311     aaa_servers_strlen+=2*(n_aaa_servers-1);
312
313     aaa_servers=talloc_array(tmp_ctx, char, aaa_servers_strlen+1);
314     aaa_servers[0]='\0';
315     for (ii=0; ii<n_aaa_servers; ii++) {
316       strcat(aaa_servers, s_aaa[ii]);
317       if (ii<(n_aaa_servers-1))
318         strcat(aaa_servers, ", ");
319     }
320     talloc_free(s_aaa);
321   }
322
323   /* get the APCs */
324   if (n_apcs<=0)
325     apcs=talloc_strdup(tmp_ctx, "");
326   else {
327     s_apc=talloc_array(tmp_ctx, char *, n_apcs);
328     for (apc=idp->apcs,ii=0; apc!=NULL; apc=apc->next,ii++) {
329       s_apc[ii]=tr_apc_to_str(s_apc, apc);
330       apcs_strlen+=strlen(s_apc[ii]);
331     }
332
333     /* add space for comma-space separators */
334     apcs_strlen+=2*(n_apcs-1);
335
336     apcs=talloc_array(tmp_ctx, char, apcs_strlen+1);
337     apcs[0]='\0';
338     for (ii=0; ii<n_apcs; ii++) {
339       strcat(apcs, s_apc[ii]);
340       if (ii<(n_apcs-1))
341         strcat(apcs, ", ");
342     }
343     talloc_free(s_apc);
344   }
345
346   result=talloc_asprintf(mem_ctx,
347                          "IDP realm: \"%.*s\"\n"
348                          "  shared: %s\n"
349                          "  local: %s\n"
350                          "  AAA servers: %s\n"
351                          "  APCs: %s\n",
352                          idp->realm_id->len, idp->realm_id->buf,
353                          (idp->shared_config)?"yes":"no",
354                          (idp->origin==TR_REALM_LOCAL)?"yes":"no",
355                          aaa_servers,
356                          apcs);
357   talloc_free(tmp_ctx);
358   return result;
359 }
360
361 void tr_idp_realm_incref(TR_IDP_REALM *realm)
362 {
363   realm->refcount++;
364 }
365
366 void tr_idp_realm_decref(TR_IDP_REALM *realm)
367 {
368   if (realm->refcount>0)
369     realm->refcount--;
370 }
371
372 /* remove any with zero refcount 
373  * Call via macro. */
374 TR_IDP_REALM *tr_idp_realm_sweep_func(TR_IDP_REALM *head)
375 {
376   TR_IDP_REALM *idp=NULL;
377   TR_IDP_REALM *old_next=NULL;
378
379   if (head==NULL)
380     return NULL;
381
382   while ((head!=NULL) && (head->refcount==0)) {
383     idp=head; /* keep a pointer so we can remove it */
384     tr_idp_realm_remove(head, idp); /* use this to get talloc contexts right */
385     tr_idp_realm_free(idp);
386   }
387
388   if (head==NULL)
389     return NULL;
390
391   /* will not remove the head here, that has already been done */
392   for (idp=head; idp->next!=NULL; idp=idp->next) {
393     if (idp->next->refcount==0) {
394       old_next=idp->next;
395       tr_idp_realm_remove(head, idp->next); /* changes idp->next */
396       tr_idp_realm_free(old_next);
397     }
398   }
399
400   return head;
401 }
402