fc325dce1fe8b47d9c1b4d31fdf19544353aa265
[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->hostname=hostname;
56     talloc_set_destructor((void *)aaa, tr_aaa_server_destructor);
57   }
58   return aaa;
59 }
60
61 void tr_aaa_server_free(TR_AAA_SERVER *aaa)
62 {
63   talloc_free(aaa);
64 }
65
66 TR_AAA_SERVER_ITER *tr_aaa_server_iter_new(TALLOC_CTX *mem_ctx)
67 {
68   return talloc(mem_ctx, TR_AAA_SERVER_ITER);
69 }
70
71 void tr_aaa_server_iter_free(TR_AAA_SERVER_ITER *iter)
72 {
73   talloc_free(iter);
74 }
75
76 TR_AAA_SERVER *tr_aaa_server_iter_first(TR_AAA_SERVER_ITER *iter, TR_AAA_SERVER *aaa)
77 {
78   iter->this=aaa;
79   return iter->this;
80 }
81
82 TR_AAA_SERVER *tr_aaa_server_iter_next(TR_AAA_SERVER_ITER *iter)
83 {
84   if (iter->this!=NULL) {
85     iter->this=iter->this->next;
86   }
87   return iter->this;
88 }
89
90
91 /* fills in shared if pointer not null */
92 TR_AAA_SERVER *tr_idp_aaa_server_lookup(TR_IDP_REALM *idp_realms, TR_NAME *idp_realm_name, TR_NAME *comm, int *shared_out)
93 {
94   TR_IDP_REALM *idp = NULL;
95
96   for (idp = idp_realms; idp != NULL; idp = idp->next) {
97     if (!tr_name_cmp(idp_realm_name, idp->realm_id)) {
98       /* TBD -- check that the community is one of the APCs for the IDP */
99       break;
100     }
101   }
102   if (idp) {
103     if (shared_out!=NULL)
104       *shared_out=idp->shared_config;
105     return idp->aaa_servers;
106   } else 
107     return NULL;
108 }
109
110 TR_AAA_SERVER *tr_default_server_lookup(TR_AAA_SERVER *default_servers, TR_NAME *comm)
111 {
112   if (!default_servers)
113     return NULL;
114
115   return(default_servers);
116 }
117
118 static int tr_idp_realm_destructor(void *obj)
119 {
120   TR_IDP_REALM *idp=talloc_get_type_abort(obj, TR_IDP_REALM);
121   if (idp->realm_id!=NULL)
122     tr_free_name(idp->realm_id);
123   return 0;
124 }
125
126 /* talloc note: lists of idp realms should be assembled using
127  * tr_idp_realm_add(). This will put all of the elements in the
128  * list, other than the head, as children of the head context.
129  * The head can then be placed in whatever context is desired. */
130 TR_IDP_REALM *tr_idp_realm_new(TALLOC_CTX *mem_ctx)
131 {
132   TR_IDP_REALM *idp=talloc(mem_ctx, TR_IDP_REALM);
133   if (idp!=NULL) {
134     idp->next=NULL;
135     idp->comm_next=NULL;
136     idp->realm_id=NULL;
137     idp->shared_config=0;
138     idp->aaa_servers=NULL;
139     idp->apcs=NULL;
140     idp->origin=TR_REALM_LOCAL;
141     talloc_set_destructor((void *)idp, tr_idp_realm_destructor);
142   }
143   return idp;
144 }
145
146 void tr_idp_realm_free(TR_IDP_REALM *idp)
147 {
148   talloc_free(idp);
149 }
150
151 TR_NAME *tr_idp_realm_get_id(TR_IDP_REALM *idp)
152 {
153   if (idp==NULL)
154     return NULL;
155   
156   return idp->realm_id;
157 }
158
159 TR_NAME *tr_idp_realm_dup_id(TR_IDP_REALM *idp)
160 {
161   if (idp==NULL)
162     return NULL;
163   
164   return tr_dup_name(tr_idp_realm_get_id(idp));
165 }
166
167 void tr_idp_realm_set_id(TR_IDP_REALM *idp, TR_NAME *id)
168 {
169   if (idp->realm_id!=NULL)
170     tr_free_name(idp->realm_id);
171   idp->realm_id=id;
172 }
173
174 void tr_idp_realm_set_apcs(TR_IDP_REALM *idp, TR_APC *apc)
175 {
176   if (idp->apcs!=NULL)
177     tr_apc_free(idp->apcs);
178   idp->apcs=apc;
179   talloc_steal(idp, apc);
180 }
181
182 TR_APC *tr_idp_realm_get_apcs(TR_IDP_REALM *idp)
183 {
184   return idp->apcs;
185 }
186
187 TR_IDP_REALM *tr_idp_realm_lookup(TR_IDP_REALM *idp_realms, TR_NAME *idp_name)
188 {
189   TR_IDP_REALM *idp = NULL;
190
191   if (!idp_name) {
192     tr_debug("tr_idp_realm_lookup: Bad parameters.");
193     return NULL;
194   }
195
196   for (idp=idp_realms; NULL!=idp; idp=idp->next) {
197     if (0==tr_name_cmp(tr_idp_realm_get_id(idp), idp_name))
198       return idp;
199   } 
200   return NULL;
201 }
202
203
204 static TR_IDP_REALM *tr_idp_realm_tail(TR_IDP_REALM *idp)
205 {
206   if (idp==NULL)
207     return NULL;
208
209   while (idp->next!=NULL)
210     idp=idp->next;
211   return idp;
212 }
213
214 /* do not call directly, use the tr_idp_realm_add() macro */
215 TR_IDP_REALM *tr_idp_realm_add_func(TR_IDP_REALM *head, TR_IDP_REALM *new)
216 {
217   if (head==NULL)
218     head=new;
219   else {
220     tr_idp_realm_tail(head)->next=new;
221     while (new!=NULL) {
222       talloc_steal(head, new); /* put it in the right context */
223       new=new->next;
224     }
225   }
226   return head;
227 }
228
229 /* use the macro */
230 TR_IDP_REALM *tr_idp_realm_remove_func(TR_IDP_REALM *head, TR_IDP_REALM *remove)
231 {
232   TALLOC_CTX *list_ctx=talloc_parent(head);
233   TR_IDP_REALM *this=NULL;
234
235   if (head==NULL)
236     return NULL;
237
238   if (head==remove) {
239     /* if we're removing the head, put the next element (if present) into the context
240      * the list head was in. */
241     head=head->next;
242     if (head!=NULL) {
243       talloc_steal(list_ctx, head);
244       /* now put all the other elements in the context of the list head */
245       for (this=head->next; this!=NULL; this=this->next)
246         talloc_steal(head, this);
247     }
248   } else {
249     /* not removing the head; no need to play with contexts */
250     for (this=head; this->next!=NULL; this=this->next) {
251       if (this->next==remove) {
252         this->next=remove->next;
253         break;
254       }
255     }
256   }
257   return head;
258 }
259
260 static int tr_idp_realm_apc_count(TR_IDP_REALM *idp)
261 {
262   int ii=0;
263   TR_APC *apc=idp->apcs;
264   while (apc!=NULL) {
265     apc=apc->next;
266     ii++;
267   }
268   return ii;
269 }
270
271 static int tr_idp_realm_aaa_server_count(TR_IDP_REALM *idp)
272 {
273   int ii=0;
274   TR_AAA_SERVER *aaa=idp->aaa_servers;
275   while (aaa!=NULL) {
276     aaa=aaa->next;
277     ii++;
278   }
279   return ii;
280 }
281
282 static char *tr_aaa_server_to_str(TALLOC_CTX *mem_ctx, TR_AAA_SERVER *aaa)
283 {
284   return talloc_strndup(mem_ctx, aaa->hostname->buf, aaa->hostname->len);
285 }
286
287 char *tr_idp_realm_to_str(TALLOC_CTX *mem_ctx, TR_IDP_REALM *idp)
288 {
289   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
290   char **s_aaa=NULL, *aaa_servers=NULL;
291   char **s_apc=NULL, *apcs=NULL;
292   int ii=0, aaa_servers_strlen=0, apcs_strlen=0;
293   int n_aaa_servers=tr_idp_realm_aaa_server_count(idp);
294   int n_apcs=tr_idp_realm_apc_count(idp);
295   TR_AAA_SERVER *aaa=NULL;
296   TR_APC *apc=NULL;
297   char *result=NULL;
298
299   /* get the AAA servers */
300   if (n_aaa_servers<=0)
301     aaa_servers=talloc_strdup(tmp_ctx, "");
302   else {
303     s_aaa=talloc_array(tmp_ctx, char *, n_aaa_servers);
304     for (aaa=idp->aaa_servers,ii=0; aaa!=NULL; aaa=aaa->next,ii++) {
305       s_aaa[ii]=tr_aaa_server_to_str(s_aaa, aaa);
306       aaa_servers_strlen+=strlen(s_aaa[ii]);
307     }
308
309     /* add space for comma-space separators */
310     aaa_servers_strlen+=2*(n_aaa_servers-1);
311
312     aaa_servers=talloc_array(tmp_ctx, char, aaa_servers_strlen+1);
313     aaa_servers[0]='\0';
314     for (ii=0; ii<n_aaa_servers; ii++) {
315       strcat(aaa_servers, s_aaa[ii]);
316       if (ii<(n_aaa_servers-1))
317         strcat(aaa_servers, ", ");
318     }
319     talloc_free(s_aaa);
320   }
321
322   /* get the APCs */
323   if (n_apcs<=0)
324     apcs=talloc_strdup(tmp_ctx, "");
325   else {
326     s_apc=talloc_array(tmp_ctx, char *, n_apcs);
327     for (apc=idp->apcs,ii=0; apc!=NULL; apc=apc->next,ii++) {
328       s_apc[ii]=tr_apc_to_str(s_apc, apc);
329       apcs_strlen+=strlen(s_apc[ii]);
330     }
331
332     /* add space for comma-space separators */
333     apcs_strlen+=2*(n_apcs-1);
334
335     apcs=talloc_array(tmp_ctx, char, apcs_strlen+1);
336     apcs[0]='\0';
337     for (ii=0; ii<n_apcs; ii++) {
338       strcat(apcs, s_apc[ii]);
339       if (ii<(n_apcs-1))
340         strcat(apcs, ", ");
341     }
342     talloc_free(s_apc);
343   }
344
345   result=talloc_asprintf(mem_ctx,
346                          "IDP realm: \"%.*s\"\n"
347                          "  shared: %s\n"
348                          "  local: %s\n"
349                          "  AAA servers: %s\n"
350                          "  APCs: %s\n",
351                          idp->realm_id->len, idp->realm_id->buf,
352                          (idp->shared_config)?"yes":"no",
353                          (idp->origin==TR_REALM_LOCAL)?"yes":"no",
354                          aaa_servers,
355                          apcs);
356   talloc_free(tmp_ctx);
357   return result;
358 }
359
360 void tr_idp_realm_incref(TR_IDP_REALM *realm)
361 {
362   realm->refcount++;
363 }
364
365 void tr_idp_realm_decref(TR_IDP_REALM *realm)
366 {
367   if (realm->refcount>0)
368     realm->refcount--;
369 }
370
371 /* remove any with zero refcount 
372  * Call via macro. */
373 TR_IDP_REALM *tr_idp_realm_sweep_func(TR_IDP_REALM *head)
374 {
375   TR_IDP_REALM *idp=NULL;
376   TR_IDP_REALM *old_next=NULL;
377
378   if (head==NULL)
379     return NULL;
380
381   while ((head!=NULL) && (head->refcount==0)) {
382     idp=head; /* keep a pointer so we can remove it */
383     tr_idp_realm_remove(head, idp); /* use this to get talloc contexts right */
384     tr_idp_realm_free(idp);
385   }
386
387   if (head==NULL)
388     return NULL;
389
390   /* will not remove the head here, that has already been done */
391   for (idp=head; idp->next!=NULL; idp=idp->next) {
392     if (idp->next->refcount==0) {
393       old_next=idp->next;
394       tr_idp_realm_remove(head, idp->next); /* changes idp->next */
395       tr_idp_realm_free(old_next);
396     }
397   }
398
399   return head;
400 }
401