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.
42 #include <trust_router/tr_name.h>
47 static int tr_comm_destructor(void *obj)
49 TR_COMM *comm=talloc_get_type_abort(obj, TR_COMM);
51 tr_free_name(comm->id);
52 if (comm->owner_realm!=NULL)
53 tr_free_name(comm->owner_realm);
54 if (comm->owner_contact!=NULL)
55 tr_free_name(comm->owner_contact);
59 TR_COMM *tr_comm_new(TALLOC_CTX *mem_ctx)
61 TR_COMM *comm=talloc(mem_ctx, TR_COMM);
65 comm->type=TR_COMM_UNKNOWN;
67 comm->owner_realm=NULL;
68 comm->owner_contact=NULL;
69 comm->expiration_interval=0;
71 talloc_set_destructor((void *)comm, tr_comm_destructor);
76 void tr_comm_free(TR_COMM *comm)
81 void tr_comm_set_id(TR_COMM *comm, TR_NAME *id)
84 tr_free_name(comm->id);
88 void tr_comm_incref(TR_COMM *comm)
93 void tr_comm_decref(TR_COMM *comm)
99 void tr_comm_set_apcs(TR_COMM *comm, TR_APC *apc)
101 if (comm->apcs!=NULL)
102 tr_apc_free(comm->apcs);
104 talloc_steal(comm, apc);
107 TR_APC *tr_comm_get_apcs(TR_COMM *comm)
112 TR_NAME *tr_comm_get_id(TR_COMM *comm)
117 TR_NAME *tr_comm_dup_id(TR_COMM *comm)
119 return tr_dup_name(comm->id);
122 void tr_comm_set_type(TR_COMM *comm, TR_COMM_TYPE type)
127 TR_COMM_TYPE tr_comm_get_type(TR_COMM *comm)
132 void tr_comm_set_owner_realm(TR_COMM *comm, TR_NAME *realm)
134 if (comm->owner_realm!=NULL)
135 tr_free_name(comm->owner_realm);
136 comm->owner_realm=realm;
139 TR_NAME *tr_comm_get_owner_realm(TR_COMM *comm)
141 return comm->owner_realm;
144 TR_NAME *tr_comm_dup_owner_realm(TR_COMM *comm)
146 return tr_dup_name(comm->owner_realm);
149 void tr_comm_set_owner_contact(TR_COMM *comm, TR_NAME *contact)
151 if (comm->owner_contact != NULL)
152 tr_free_name(comm->owner_contact);
153 comm->owner_contact=contact;
156 TR_NAME *tr_comm_get_owner_contact(TR_COMM *comm)
158 return comm->owner_contact;
161 TR_NAME *tr_comm_dup_owner_contact(TR_COMM *comm)
163 return tr_dup_name(comm->owner_contact);
166 unsigned int tr_comm_get_refcount(TR_COMM *comm)
168 return comm->refcount;
171 /* 0 if equivalent, nonzero if different, only considers
172 * nhops last hops (nhops==0 means consider all, nhops==1
173 * only considers last hop) */
174 static int tr_comm_memb_provenance_cmp(TR_COMM_MEMB *m1, TR_COMM_MEMB *m2, int nhops)
178 if ((m1->provenance==NULL) || (m2->provenance==NULL))
179 return m1->provenance!=m2->provenance; /* return 0 if both null, 1 if only one null */
181 if (json_array_size(m1->provenance)!=json_array_size(m2->provenance))
185 nhops=json_array_size(m1->provenance); /* same as size(m2->provenance) */
187 for (ii=0; ii<json_array_size(m1->provenance); ii++) {
188 if (0==strcmp(json_string_value(json_array_get(m1->provenance, ii)),
189 json_string_value(json_array_get(m2->provenance, ii)))) {
196 /* Accepts an update that either came from the same peer as the previous
197 * origin, has a shorter provenance list, or can replace an expired
198 * membership. Otherwise keeps the existing one.
199 * On replacement, frees the old member and moves new member to ctab's
200 * context. Caller should not free newmemb except by freeing its original
202 static void tr_comm_add_if_shorter(TR_COMM_TABLE *ctab, TR_COMM_MEMB *existing, TR_COMM_MEMB *newmemb)
206 if (existing==NULL) {
207 /* not in the table */
208 tr_comm_table_add_memb(ctab, newmemb);
210 if (0==tr_comm_memb_provenance_cmp(existing, newmemb, 1))
211 accept=1; /* always accept a replacement from the same peer */
212 else if (tr_comm_memb_provenance_len(newmemb) < tr_comm_memb_provenance_len(existing))
213 accept=1; /* accept a shorter provenance */
214 else if (existing->times_expired>0)
220 tr_comm_table_remove_memb(ctab, existing);
221 tr_comm_memb_free(existing);
222 tr_comm_table_add_memb(ctab, newmemb);
227 /* does not take responsibility for freeing IDP realm */
228 void tr_comm_add_idp_realm(TR_COMM_TABLE *ctab,
231 unsigned int interval,
233 struct timespec *expiry)
235 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
236 TR_COMM_MEMB *newmemb=tr_comm_memb_new(tmp_ctx);
237 TR_COMM_MEMB *existing=NULL;
240 tr_err("tr_comm_add_idp_realm: unable to allocate new membership record.");
241 talloc_free(tmp_ctx);
245 tr_comm_memb_set_idp_realm(newmemb, realm);
246 tr_comm_memb_set_comm(newmemb, comm);
247 tr_comm_memb_set_interval(newmemb, interval);
248 tr_comm_memb_set_provenance(newmemb, provenance);
250 existing=tr_comm_table_find_idp_memb_origin(ctab,
251 tr_idp_realm_get_id(realm),
252 tr_comm_get_id(comm),
253 tr_comm_memb_get_origin(newmemb));
254 tr_comm_add_if_shorter(ctab, existing, newmemb); /* takes newmemb out of tmp_ctx if needed */
256 talloc_free(tmp_ctx);
259 /* does not take responsibility for freeing RP realm */
260 void tr_comm_add_rp_realm(TR_COMM_TABLE *ctab,
263 unsigned int interval,
265 struct timespec *expiry)
267 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
268 TR_COMM_MEMB *newmemb=tr_comm_memb_new(tmp_ctx);
269 TR_COMM_MEMB *existing=NULL;
272 tr_err("tr_comm_add_idp_realm: unable to allocate new membership record.");
273 talloc_free(tmp_ctx);
277 tr_comm_memb_set_rp_realm(newmemb, realm);
278 tr_comm_memb_set_comm(newmemb, comm);
279 tr_comm_memb_set_interval(newmemb, interval);
280 tr_comm_memb_set_provenance(newmemb, provenance);
282 existing=tr_comm_table_find_rp_memb_origin(ctab,
283 tr_rp_realm_get_id(realm),
284 tr_comm_get_id(comm),
285 tr_comm_memb_get_origin(newmemb));
286 tr_comm_add_if_shorter(ctab, existing, newmemb); /* takes newmemb out of tmp_ctx if needed */
287 talloc_free(tmp_ctx);
290 static TR_COMM *tr_comm_tail(TR_COMM *comm)
295 while (comm->next!=NULL)
300 /* All list members are in the talloc context of the head.
301 * This will require careful thought if entries are ever removed
302 * Call like comms=tr_comm_add_func(comms, new_comm);
303 * or just use the tr_comm_add(comms, new) macro. */
304 #define tr_comm_add(comms, new) ((comms)=tr_comm_add_func((comms), (new)))
305 static TR_COMM *tr_comm_add_func(TR_COMM *comms, TR_COMM *new)
310 tr_comm_tail(comms)->next=new;
312 talloc_steal(comms, new);
319 /* Guarantees comm is not in the list, not an error if it was't there.
320 * Does not free the removed element, nor change its talloc context. */
321 #define tr_comm_remove(comms, c) ((comms)=tr_comm_remove_func((comms), (c)))
322 static TR_COMM *tr_comm_remove_func(TR_COMM *comms, TR_COMM *remove)
324 TALLOC_CTX *list_ctx=talloc_parent(comms); /* in case we need to remove the head */
331 /* if we're removing the head, put the next element (if present) into the context
332 * the list head was in. */
335 talloc_steal(list_ctx, comms);
336 /* now put all the other elements in the context of the list head */
337 for (this=comms->next; this!=NULL; this=this->next)
338 talloc_steal(comms, this);
341 /* not removing the head; no need to play with contexts */
342 for (this=comms; this->next!=NULL; this=this->next) {
343 if (this->next==remove) {
344 this->next=remove->next;
352 /* remove any with zero refcount
354 #define tr_comm_sweep(head) ((head)=tr_comm_sweep_func((head)))
355 static TR_COMM *tr_comm_sweep_func(TR_COMM *head)
358 TR_COMM *old_next=NULL;
363 while ((head!=NULL) && (head->refcount==0)) {
364 comm=head; /* keep a pointer so we can remove it */
365 tr_comm_remove(head, comm); /* use this to get talloc contexts right */
372 /* will not remove the head here, that has already been done */
373 for (comm=head; comm->next!=NULL; comm=comm->next) {
374 if (comm->next->refcount==0) {
376 tr_comm_remove(head, comm->next); /* changes comm->next */
377 tr_comm_free(old_next);
384 TR_IDP_REALM *tr_comm_find_idp(TR_COMM_TABLE *ctab, TR_COMM *comm, TR_NAME *idp_realm)
386 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
387 TR_COMM_ITER *iter=NULL;
388 TR_IDP_REALM *this_idp=NULL;
390 if ((NULL==ctab) || (NULL==comm) || (NULL==idp_realm)) {
391 talloc_free(tmp_ctx);
395 iter=tr_comm_iter_new(tmp_ctx);
396 for (this_idp=tr_idp_realm_iter_first(iter, ctab, tr_comm_get_id(comm));
398 this_idp=tr_idp_realm_iter_next(iter)) {
399 if (0==tr_name_cmp(idp_realm, tr_idp_realm_get_id(this_idp))) {
400 tr_debug("tr_comm_find_idp: Found IdP %s in community %s.", idp_realm->buf, tr_comm_get_id(comm)->buf);
401 talloc_free(tmp_ctx);
405 tr_debug("tr_comm_find_idp: Unable to find IdP %s in community %s.", idp_realm->buf, tr_comm_get_id(comm)->buf);
406 talloc_free(tmp_ctx);
410 TR_RP_REALM *tr_comm_find_rp (TR_COMM_TABLE *ctab, TR_COMM *comm, TR_NAME *rp_realm)
412 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
413 TR_COMM_ITER *iter=NULL;
414 TR_RP_REALM *this_rp=NULL;
416 if ((NULL==ctab) || (NULL==comm) || (NULL==rp_realm)) {
417 talloc_free(tmp_ctx);
421 iter=tr_comm_iter_new(tmp_ctx);
422 for (this_rp=tr_rp_realm_iter_first(iter, ctab, tr_comm_get_id(comm));
424 this_rp=tr_rp_realm_iter_next(iter)) {
425 if (0==tr_name_cmp(rp_realm, tr_rp_realm_get_id(this_rp))) {
426 tr_debug("tr_comm_find_rp: Found RP %s in community %s.", rp_realm->buf, tr_comm_get_id(comm)->buf);
427 talloc_free(tmp_ctx);
431 tr_debug("tr_comm_find_rp: Unable to find RP %s in community %s.", rp_realm->buf, tr_comm_get_id(comm)->buf);
432 talloc_free(tmp_ctx);
436 static TR_COMM *tr_comm_lookup(TR_COMM *comms, TR_NAME *comm_name)
438 TR_COMM *cfg_comm = NULL;
440 for (cfg_comm = comms; NULL != cfg_comm; cfg_comm = cfg_comm->next) {
441 if (0==tr_name_cmp(cfg_comm->id, comm_name))
447 TR_COMM_ITER *tr_comm_iter_new(TALLOC_CTX *mem_ctx)
449 TR_COMM_ITER *iter=talloc(mem_ctx, TR_COMM_ITER);
453 iter->cur_orig_head=NULL;
460 void tr_comm_iter_free(TR_COMM_ITER *iter)
466 TR_COMM *tr_comm_iter_first(TR_COMM_ITER *iter, TR_COMM_TABLE *ctab, TR_NAME *realm)
470 /* find memberships for this realm */
471 for (iter->cur_memb=ctab->memberships;
472 iter->cur_memb!=NULL;
473 iter->cur_memb=iter->cur_memb->next) {
474 if (0==tr_name_cmp(iter->match, tr_comm_memb_get_realm_id(iter->cur_memb)))
475 return tr_comm_memb_get_comm(iter->cur_memb);
480 TR_COMM *tr_comm_iter_next(TR_COMM_ITER *iter)
482 for (iter->cur_memb=iter->cur_memb->next;
483 iter->cur_memb!=NULL;
484 iter->cur_memb=iter->cur_memb->next) {
485 if (0==tr_name_cmp(iter->match, tr_comm_memb_get_realm_id(iter->cur_memb)))
486 return tr_comm_memb_get_comm(iter->cur_memb);
491 /* iterate only over RPs */
492 TR_COMM *tr_comm_iter_first_rp(TR_COMM_ITER *iter, TR_COMM_TABLE *ctab, TR_NAME *realm)
496 /* find memberships for this realm */
497 for (iter->cur_memb=ctab->memberships;
498 iter->cur_memb!=NULL;
499 iter->cur_memb=iter->cur_memb->next) {
500 if ((tr_comm_memb_get_rp_realm(iter->cur_memb)!=NULL) &&
501 (0==tr_name_cmp(iter->match, tr_comm_memb_get_realm_id(iter->cur_memb))))
502 return tr_comm_memb_get_comm(iter->cur_memb);
507 TR_COMM *tr_comm_iter_next_rp(TR_COMM_ITER *iter)
509 for (iter->cur_memb=iter->cur_memb->next;
510 iter->cur_memb!=NULL;
511 iter->cur_memb=iter->cur_memb->next) {
512 if ((tr_comm_memb_get_rp_realm(iter->cur_memb)!=NULL) &&
513 (0==tr_name_cmp(iter->match, tr_comm_memb_get_realm_id(iter->cur_memb))))
514 return tr_comm_memb_get_comm(iter->cur_memb);
519 /* iterate only over IDPs */
520 TR_COMM *tr_comm_iter_first_idp(TR_COMM_ITER *iter, TR_COMM_TABLE *ctab, TR_NAME *realm)
524 /* find memberships for this realm */
525 for (iter->cur_memb=ctab->memberships;
526 iter->cur_memb!=NULL;
527 iter->cur_memb=iter->cur_memb->next) {
528 if ((tr_comm_memb_get_idp_realm(iter->cur_memb)!=NULL) &&
529 (0==tr_name_cmp(iter->match, tr_comm_memb_get_realm_id(iter->cur_memb))))
530 return tr_comm_memb_get_comm(iter->cur_memb);
535 TR_COMM *tr_comm_iter_next_idp(TR_COMM_ITER *iter)
537 for (iter->cur_memb=iter->cur_memb->next;
538 iter->cur_memb!=NULL;
539 iter->cur_memb=iter->cur_memb->next) {
540 if ((tr_comm_memb_get_idp_realm(iter->cur_memb)!=NULL) &&
541 (0==tr_name_cmp(iter->match, tr_comm_memb_get_realm_id(iter->cur_memb))))
542 return tr_comm_memb_get_comm(iter->cur_memb);
547 static TR_REALM *tr_realm_new(TALLOC_CTX *mem_ctx)
549 TR_REALM *realm=talloc(mem_ctx, TR_REALM);
551 realm->role=TR_ROLE_UNKNOWN;
558 static void tr_realm_free(TR_REALM *realm)
563 static void tr_realm_set_rp(TR_REALM *realm, TR_RP_REALM *rp)
565 if (realm->idp!=NULL)
567 realm->role=TR_ROLE_RP;
571 static void tr_realm_set_idp(TR_REALM *realm, TR_IDP_REALM *idp)
575 realm->role=TR_ROLE_IDP;
579 TR_NAME *tr_realm_get_id(TR_REALM *realm)
581 switch (realm->role) {
583 return tr_rp_realm_get_id(realm->rp);
585 return tr_idp_realm_get_id(realm->idp);
592 TR_NAME *tr_realm_dup_id(TR_REALM *realm)
594 return tr_dup_name(tr_realm_get_id(realm));
597 /* Iterate over either sort of realm. Do not free the TR_REALM returned. It becomes
598 * undefined/invalid after the next operation affecting the iterator. */
599 TR_REALM *tr_realm_iter_first(TR_COMM_ITER *iter, TR_COMM_TABLE *ctab, TR_NAME *comm)
602 if (iter->realm==NULL)
603 iter->realm=tr_realm_new(iter);
604 if (iter->realm==NULL)
607 /* find memberships for this comm */
608 for (iter->cur_memb=ctab->memberships;
609 iter->cur_memb!=NULL;
610 iter->cur_memb=iter->cur_memb->next) {
611 if (0==tr_name_cmp(iter->match,
612 tr_comm_get_id(tr_comm_memb_get_comm(iter->cur_memb)))) {
613 /* found a match, determine whether it's an rp realm or an idp realm */
614 if (tr_comm_memb_get_rp_realm(iter->cur_memb)!=NULL)
615 tr_realm_set_rp(iter->realm, tr_comm_memb_get_rp_realm(iter->cur_memb));
616 else if (tr_comm_memb_get_idp_realm(iter->cur_memb)!=NULL)
617 tr_realm_set_idp(iter->realm, tr_comm_memb_get_idp_realm(iter->cur_memb));
619 if (iter->realm!=NULL)
620 tr_realm_free(iter->realm);
626 if (iter->realm!=NULL)
627 tr_realm_free(iter->realm);
632 TR_REALM *tr_realm_iter_next(TR_COMM_ITER *iter)
634 if (iter->realm==NULL)
637 /* find memberships for this comm */
638 for (iter->cur_memb=iter->cur_memb->next;
639 iter->cur_memb!=NULL;
640 iter->cur_memb=iter->cur_memb->next) {
641 if (0==tr_name_cmp(iter->match,
642 tr_comm_get_id(tr_comm_memb_get_comm(iter->cur_memb)))) {
643 /* found a match, determine whether it's an rp realm or an idp realm */
644 if (tr_comm_memb_get_rp_realm(iter->cur_memb)!=NULL)
645 tr_realm_set_rp(iter->realm, tr_comm_memb_get_rp_realm(iter->cur_memb));
646 else if (tr_comm_memb_get_idp_realm(iter->cur_memb)!=NULL)
647 tr_realm_set_idp(iter->realm, tr_comm_memb_get_idp_realm(iter->cur_memb));
649 if (iter->realm!=NULL)
650 tr_realm_free(iter->realm);
656 if (iter->realm!=NULL)
657 tr_realm_free(iter->realm);
662 TR_RP_REALM *tr_rp_realm_iter_first(TR_COMM_ITER *iter, TR_COMM_TABLE *ctab, TR_NAME *comm)
666 /* find memberships for this comm */
667 for (iter->cur_memb=ctab->memberships;
668 iter->cur_memb!=NULL;
669 iter->cur_memb=iter->cur_memb->next) {
670 if ((tr_comm_memb_get_rp_realm(iter->cur_memb)!=NULL) &&
671 (0==tr_name_cmp(iter->match, tr_comm_get_id(tr_comm_memb_get_comm(iter->cur_memb)))))
672 return tr_comm_memb_get_rp_realm(iter->cur_memb);
677 TR_RP_REALM *tr_rp_realm_iter_next(TR_COMM_ITER *iter)
679 for (iter->cur_memb=iter->cur_memb->next;
680 iter->cur_memb!=NULL;
681 iter->cur_memb=iter->cur_memb->next) {
682 if ((tr_comm_memb_get_rp_realm(iter->cur_memb)!=NULL) &&
683 (0==tr_name_cmp(iter->match, tr_comm_get_id(tr_comm_memb_get_comm(iter->cur_memb)))))
684 return tr_comm_memb_get_rp_realm(iter->cur_memb);
689 TR_IDP_REALM *tr_idp_realm_iter_first(TR_COMM_ITER *iter, TR_COMM_TABLE *ctab, TR_NAME *comm)
693 /* find memberships for this comm */
694 for (iter->cur_memb=ctab->memberships;
695 iter->cur_memb!=NULL;
696 iter->cur_memb=iter->cur_memb->next) {
697 if ((tr_comm_memb_get_idp_realm(iter->cur_memb)!=NULL) &&
698 (0==tr_name_cmp(iter->match, tr_comm_get_id(tr_comm_memb_get_comm(iter->cur_memb)))))
699 return tr_comm_memb_get_idp_realm(iter->cur_memb);
704 TR_IDP_REALM *tr_idp_realm_iter_next(TR_COMM_ITER *iter)
706 for (iter->cur_memb=iter->cur_memb->next;
707 iter->cur_memb!=NULL;
708 iter->cur_memb=iter->cur_memb->next) {
709 if ((tr_comm_memb_get_idp_realm(iter->cur_memb)!=NULL) &&
710 (0==tr_name_cmp(iter->match, tr_comm_get_id(tr_comm_memb_get_comm(iter->cur_memb)))))
711 return tr_comm_memb_get_idp_realm(iter->cur_memb);
716 /* iterators for all communities in a table */
717 TR_COMM *tr_comm_table_iter_first(TR_COMM_ITER *iter, TR_COMM_TABLE *ctab)
719 iter->cur_comm=ctab->comms;
720 return iter->cur_comm;
723 TR_COMM *tr_comm_table_iter_next(TR_COMM_ITER *iter)
725 return iter->cur_comm=iter->cur_comm->next;
728 const char *tr_comm_type_to_str(TR_COMM_TYPE type)
732 case TR_COMM_UNKNOWN:
747 /* iterate along the origin list for this member */
748 TR_COMM_MEMB *tr_comm_memb_iter_first(TR_COMM_ITER *iter, TR_COMM_MEMB *memb)
751 return iter->cur_memb;
754 TR_COMM_MEMB *tr_comm_memb_iter_next(TR_COMM_ITER *iter)
756 if (iter->cur_memb!=NULL)
757 iter->cur_memb=iter->cur_memb->origin_next;
758 return iter->cur_memb;
762 /* iterate over all memberships in the table */
763 TR_COMM_MEMB *tr_comm_memb_iter_all_first(TR_COMM_ITER *iter, TR_COMM_TABLE *ctab)
765 iter->cur_memb=ctab->memberships;
766 iter->cur_orig_head=ctab->memberships;
767 return iter->cur_memb;
770 TR_COMM_MEMB *tr_comm_memb_iter_all_next(TR_COMM_ITER *iter)
772 if (iter->cur_memb->next==NULL) {
773 if (iter->cur_orig_head->next==NULL) {
777 iter->cur_memb=iter->cur_orig_head->next;
778 iter->cur_orig_head=iter->cur_orig_head->next;
781 iter->cur_memb=iter->cur_memb->origin_next;
783 return iter->cur_memb;
787 TR_COMM_TYPE tr_comm_type_from_str(const char *s)
789 if (strcmp(s, "apc")==0)
791 if (strcmp(s,"coi")==0)
793 return TR_COMM_UNKNOWN;
797 static int tr_comm_memb_destructor(void *obj)
799 TR_COMM_MEMB *memb=talloc_get_type_abort(obj, TR_COMM_MEMB);
800 if (memb->origin!=NULL)
801 tr_free_name(memb->origin);
804 tr_rp_realm_decref(memb->rp);
806 tr_idp_realm_decref(memb->idp);
807 if (memb->comm!=NULL)
808 tr_comm_decref(memb->comm);
809 if (memb->provenance!=NULL)
810 json_decref(memb->provenance);
814 TR_COMM_MEMB *tr_comm_memb_new(TALLOC_CTX *mem_ctx)
816 TR_COMM_MEMB *memb=talloc(mem_ctx, TR_COMM_MEMB);
819 memb->origin_next=NULL;
824 memb->provenance=NULL;
827 memb->times_expired=0;
828 memb->expiry=talloc(memb, struct timespec);
829 if (memb->expiry==NULL) {
833 *(memb->expiry)=(struct timespec){0,0};
834 talloc_set_destructor(memb, tr_comm_memb_destructor);
839 void tr_comm_memb_free(TR_COMM_MEMB *memb)
844 /* Returns 0 if they are the same, nonzero if they differ.
845 * Ignores expiry, triggered, and times_expired, next pointers */
846 int tr_comm_memb_cmp(TR_COMM_MEMB *m1, TR_COMM_MEMB *m2)
848 if ((m1->idp==m2->idp) &&
850 (m1->comm==m2->comm) &&
851 (tr_comm_memb_provenance_cmp(m1, m2, 0)==0) &&
852 (m1->interval==m2->interval))
857 TR_REALM_ROLE tr_comm_memb_get_role(TR_COMM_MEMB *memb)
863 return TR_ROLE_UNKNOWN;
866 void tr_comm_memb_set_rp_realm(TR_COMM_MEMB *memb, TR_RP_REALM *realm)
868 if (memb->idp!=NULL) {
869 tr_idp_realm_decref(memb->idp);
873 tr_rp_realm_decref(memb->rp);
877 tr_rp_realm_incref(realm);
880 TR_RP_REALM *tr_comm_memb_get_rp_realm(TR_COMM_MEMB *memb)
885 void tr_comm_memb_set_idp_realm(TR_COMM_MEMB *memb, TR_IDP_REALM *realm)
887 if (memb->rp!=NULL) {
888 tr_rp_realm_decref(memb->rp);
892 tr_idp_realm_decref(memb->idp);
895 tr_idp_realm_incref(realm);
898 TR_IDP_REALM *tr_comm_memb_get_idp_realm(TR_COMM_MEMB *memb)
903 void tr_comm_memb_set_comm(TR_COMM_MEMB *memb, TR_COMM *comm)
905 if (memb->comm!=NULL)
906 tr_comm_decref(memb->comm);
908 tr_comm_incref(comm);
911 TR_COMM *tr_comm_memb_get_comm(TR_COMM_MEMB *memb)
916 static void tr_comm_memb_set_origin(TR_COMM_MEMB *memb, TR_NAME *origin)
918 if (memb->origin!=NULL)
919 tr_free_name(memb->origin);
923 TR_NAME *tr_comm_memb_get_origin(TR_COMM_MEMB *memb)
928 TR_NAME *tr_comm_memb_dup_origin(TR_COMM_MEMB *memb)
930 if (memb->origin!=NULL)
931 return tr_dup_name(memb->origin);
935 json_t *tr_comm_memb_get_provenance(TR_COMM_MEMB *memb)
938 return memb->provenance;
942 void tr_comm_memb_set_provenance(TR_COMM_MEMB *memb, json_t *prov)
946 if (memb->provenance)
947 json_decref(memb->provenance);
949 memb->provenance=prov;
953 /* next line sets origin to NULL if provenance is empty because jansson
954 * routines return NULL on error */
955 s=json_string_value(json_array_get(prov, 0));
957 tr_comm_memb_set_origin(memb, NULL);
959 memb->origin=tr_new_name(s);
961 tr_comm_memb_set_origin(memb, NULL);
965 void tr_comm_memb_add_to_provenance(TR_COMM_MEMB *memb, TR_NAME *hop)
967 if (memb->provenance==NULL) {
968 memb->provenance=json_array();
969 if (memb->provenance==NULL) {
970 tr_err("tr_comm_memb_add_to_provenance: unable to allocate provenance list.");
973 /* this is the first entry in the provenance, so it is the origin */
974 tr_comm_memb_set_origin(memb,tr_dup_name(hop));
975 if (memb->origin==NULL) {
976 tr_err("tr_comm_memb_add_to_provenance: unable to allocate origin.");
977 json_decref(memb->provenance);
978 memb->provenance=NULL;
982 if (0!=json_array_append_new(memb->provenance, tr_name_to_json_string(hop)))
983 tr_err("tr_comm_memb_add_to_provenance: unable to extend provenance list.");
986 size_t tr_comm_memb_provenance_len(TR_COMM_MEMB *memb)
988 if (memb->provenance==NULL)
990 return json_array_size(memb->provenance);
993 void tr_comm_memb_set_interval(TR_COMM_MEMB *memb, unsigned int interval)
995 memb->interval=interval;
998 unsigned int tr_comm_memb_get_interval(TR_COMM_MEMB *memb)
1000 return memb->interval;
1003 void tr_comm_memb_set_expiry(TR_COMM_MEMB *memb, struct timespec *time)
1006 *(memb->expiry)=(struct timespec){0,0};
1008 memb->expiry->tv_sec=time->tv_sec;
1009 memb->expiry->tv_nsec=time->tv_nsec;
1013 struct timespec *tr_comm_memb_get_expiry(TR_COMM_MEMB *memb)
1015 return memb->expiry;
1018 int tr_comm_memb_is_expired(TR_COMM_MEMB *memb, struct timespec *curtime)
1020 return ((curtime->tv_sec > memb->expiry->tv_sec)
1021 || ((curtime->tv_sec == memb->expiry->tv_sec)
1022 &&(curtime->tv_nsec >= memb->expiry->tv_nsec)));
1025 void tr_comm_memb_set_triggered(TR_COMM_MEMB *memb, int trig)
1027 memb->triggered=trig;
1030 int tr_comm_memb_is_triggered(TR_COMM_MEMB *memb)
1032 return memb->triggered;
1035 void tr_comm_memb_reset_times_expired(TR_COMM_MEMB *memb)
1037 memb->times_expired=0;
1040 /* bumps the expiration count */
1041 void tr_comm_memb_expire(TR_COMM_MEMB *memb)
1043 /* avoid overflow */
1044 if (memb->times_expired+1>memb->times_expired)
1045 memb->times_expired++;
1048 unsigned int tr_comm_memb_get_times_expired(TR_COMM_MEMB *memb)
1050 return memb->times_expired;
1053 TR_COMM_TABLE *tr_comm_table_new(TALLOC_CTX *mem_ctx)
1055 TR_COMM_TABLE *ctab=talloc(mem_ctx, TR_COMM_TABLE);
1058 ctab->memberships=NULL;
1059 ctab->idp_realms=NULL;
1060 ctab->rp_realms=NULL;
1065 void tr_comm_table_free(TR_COMM_TABLE *ctab)
1070 static TR_REALM_ROLE tr_comm_memb_role(TR_COMM_MEMB *memb)
1074 if (memb->idp!=NULL)
1077 return TR_ROLE_UNKNOWN;
1080 void tr_comm_table_add_memb(TR_COMM_TABLE *ctab, TR_COMM_MEMB *new)
1082 TR_COMM_MEMB *cur=NULL;
1084 /* TODO: further validate the member (must have valid comm and realm) */
1085 if ((new->next!=NULL) || (new->origin_next!=NULL)) {
1086 tr_debug("tr_comm_table_add_memb: attempting to add member already in a list.");
1089 /* handle the empty list case */
1090 if (ctab->memberships==NULL) {
1091 ctab->memberships=new;
1092 talloc_steal(ctab, new);
1096 /* The list was not empty. See if we already have a membership for this realm/comm/role */
1097 switch (tr_comm_memb_role(new)) {
1099 cur=tr_comm_table_find_rp_memb(ctab,
1100 tr_rp_realm_get_id(tr_comm_memb_get_rp_realm(new)),
1101 tr_comm_get_id(tr_comm_memb_get_comm(new)));
1104 cur=tr_comm_table_find_idp_memb(ctab,
1105 tr_idp_realm_get_id(tr_comm_memb_get_idp_realm(new)),
1106 tr_comm_get_id(tr_comm_memb_get_comm(new)));
1108 case TR_ROLE_UNKNOWN:
1110 tr_err("tr_comm_table_add_memb: realm with unknown role added.");
1115 /* no entry for this realm/comm/role, tack it on the end */
1116 for (cur=ctab->memberships; cur->next!=NULL; cur=cur->next) { }
1119 /* Found an entry. Add to the end of its same-origin list. */
1120 while (cur->origin_next!=NULL) {
1121 cur=cur->origin_next;
1123 cur->origin_next=new;
1126 talloc_steal(ctab, new);
1129 /* Remove memb from ctab. Do not free anything. Do nothing if memb not in ctab. */
1130 void tr_comm_table_remove_memb(TR_COMM_TABLE *ctab, TR_COMM_MEMB *memb)
1132 TR_COMM_MEMB *cur=NULL; /* for walking the main list */
1133 TR_COMM_MEMB *orig_cur=NULL; /* for walking the origin list */
1135 if ((memb==NULL) || (ctab->memberships==NULL))
1138 /* see if it's the first member */
1139 if (ctab->memberships==memb) {
1140 if (memb->origin_next!=NULL) {
1141 memb->origin_next->next=memb->next;
1142 ctab->memberships=memb->origin_next;
1144 ctab->memberships=memb->next;
1149 /* see if it's in first member's origin list */
1150 for (orig_cur=ctab->memberships;
1151 orig_cur->origin_next!=NULL;
1152 orig_cur=orig_cur->origin_next) {
1153 if (orig_cur->origin_next==memb) {
1154 orig_cur->origin_next=memb->origin_next;
1159 /* now we have to walk the rest of the tree */
1160 for (cur=ctab->memberships; cur->next!=NULL; cur=cur->next) {
1161 if (cur->next==memb) {
1162 /* it matched an entry on the main list */
1163 if (memb->origin_next==NULL)
1164 cur->next=memb->next; /* no origin list, just drop memb */
1166 /* replace the entry in the main list with the next element on the origin list */
1167 memb->origin_next->next=memb->next;
1168 cur->next=memb->origin_next;
1172 /* it was not on the main list, walk the origin list */
1173 for (orig_cur=cur; orig_cur->origin_next!=NULL; orig_cur=orig_cur->origin_next) {
1174 if (orig_cur->origin_next==memb) {
1175 orig_cur->origin_next=memb->origin_next;
1176 return; /* just drop the element from the origin list */
1181 /* if we got here, cur->next was null. Still have to check the origin_next list */
1182 for (orig_cur=cur; orig_cur->origin_next!=NULL; orig_cur=orig_cur->origin_next) {
1183 if (orig_cur->origin_next==memb) {
1184 orig_cur->origin_next=memb->origin_next;
1185 return; /* just drop the element from the origin list */
1190 TR_NAME *tr_comm_memb_get_realm_id(TR_COMM_MEMB *memb)
1193 return tr_rp_realm_get_id(memb->rp);
1195 return tr_idp_realm_get_id(memb->idp);
1198 /* find a membership from any origin */
1199 TR_COMM_MEMB *tr_comm_table_find_memb(TR_COMM_TABLE *ctab, TR_NAME *realm, TR_NAME *comm)
1201 TR_COMM_MEMB *cur=NULL;
1202 TR_NAME *cur_realm_name=NULL;
1204 for (cur=ctab->memberships; cur!=NULL; cur=cur->next) {
1205 cur_realm_name=tr_comm_memb_get_realm_id(cur);
1206 if (cur_realm_name==NULL) {
1207 tr_warning("tr_comm_table_find: encountered realm with no name.");
1210 if ((0==tr_name_cmp(realm, cur_realm_name)) &&
1211 (0==tr_name_cmp(comm, tr_comm_get_id(tr_comm_memb_get_comm(cur))))) {
1218 /* find a membership from a particular origin */
1219 TR_COMM_MEMB *tr_comm_table_find_memb_origin(TR_COMM_TABLE *ctab, TR_NAME *realm, TR_NAME *comm, TR_NAME *origin)
1221 TR_NAME *cur_orig=NULL;
1222 TR_COMM_MEMB *cur=tr_comm_table_find_memb(ctab, realm, comm);
1224 return NULL; /* no match */
1226 /* had a match for comm/realm; find origin match */
1228 if (((origin==NULL) && (cur_orig==NULL)) ||
1229 ((origin!=NULL) && (cur_orig!=NULL) && (0==tr_name_cmp(origin, cur_orig))))
1230 return cur; /* found a match */
1231 cur=cur->origin_next;
1233 return NULL; /* no match */
1237 /* find an idp membership regardless of its origin */
1238 TR_COMM_MEMB *tr_comm_table_find_idp_memb(TR_COMM_TABLE *ctab, TR_NAME *realm, TR_NAME *comm)
1240 TR_COMM_MEMB *cur=NULL;
1241 TR_IDP_REALM *idp_realm=NULL;
1243 for (cur=ctab->memberships; cur!=NULL; cur=cur->next) {
1244 idp_realm=tr_comm_memb_get_idp_realm(cur);
1245 if (idp_realm==NULL)
1246 continue; /* was not an idp */
1248 if ((0==tr_name_cmp(realm, idp_realm->realm_id)) &&
1249 (0==tr_name_cmp(comm, tr_comm_get_id(tr_comm_memb_get_comm(cur))))) {
1256 /* find an idp membership from a particular origin */
1257 TR_COMM_MEMB *tr_comm_table_find_idp_memb_origin(TR_COMM_TABLE *ctab, TR_NAME *realm, TR_NAME *comm, TR_NAME *origin)
1259 TR_NAME *cur_orig=NULL;
1260 TR_COMM_MEMB *cur=tr_comm_table_find_idp_memb(ctab, realm, comm);
1262 return NULL; /* no match */
1264 /* had a match for comm/realm; find origin match */
1266 cur_orig=tr_comm_memb_get_origin(cur);
1267 if (((origin==NULL) && (cur_orig==NULL)) ||
1268 ((origin!=NULL) && (cur_orig!=NULL) && (0==tr_name_cmp(origin, cur_orig))))
1269 return cur; /* found a match */
1270 cur=cur->origin_next;
1272 return NULL; /* no match */
1275 /* find an rp membership from any origin */
1276 TR_COMM_MEMB *tr_comm_table_find_rp_memb(TR_COMM_TABLE *ctab, TR_NAME *realm, TR_NAME *comm)
1278 TR_COMM_MEMB *cur=NULL;
1279 TR_RP_REALM *rp_realm=NULL;
1281 for (cur=ctab->memberships; cur!=NULL; cur=cur->next) {
1282 rp_realm=tr_comm_memb_get_rp_realm(cur);
1284 continue; /* was not an rp */
1286 if ((0==tr_name_cmp(realm, tr_rp_realm_get_id(rp_realm))) &&
1287 (0==tr_name_cmp(comm, tr_comm_get_id(tr_comm_memb_get_comm(cur))))) {
1294 /* find an rp membership from a particular origin */
1295 TR_COMM_MEMB *tr_comm_table_find_rp_memb_origin(TR_COMM_TABLE *ctab, TR_NAME *realm, TR_NAME *comm, TR_NAME *origin)
1297 TR_NAME *cur_orig=NULL;
1298 TR_COMM_MEMB *cur=tr_comm_table_find_rp_memb(ctab, realm, comm);
1300 return NULL; /* no match */
1302 /* had a match for comm/realm; find origin match */
1304 cur_orig=tr_comm_memb_get_origin(cur);
1305 if (((origin==NULL) && (cur_orig==NULL)) ||
1306 ((origin!=NULL) && (cur_orig!=NULL) && (0==tr_name_cmp(origin, cur_orig))))
1307 return cur; /* found a match */
1308 cur=cur->origin_next;
1310 return NULL; /* no match */
1313 TR_COMM *tr_comm_table_find_comm(TR_COMM_TABLE *ctab, TR_NAME *comm_id)
1315 return tr_comm_lookup(ctab->comms, comm_id);
1318 void tr_comm_table_add_comm(TR_COMM_TABLE *ctab, TR_COMM *new)
1320 tr_comm_add(ctab->comms, new);
1321 if (ctab->comms!=NULL)
1322 talloc_steal(ctab, ctab->comms); /* make sure it's in the right context */
1325 void tr_comm_table_remove_comm(TR_COMM_TABLE *ctab, TR_COMM *comm)
1327 tr_comm_remove(ctab->comms, comm);
1330 TR_RP_REALM *tr_comm_table_find_rp_realm(TR_COMM_TABLE *ctab, TR_NAME *realm_id)
1332 return tr_rp_realm_lookup(ctab->rp_realms, realm_id);
1335 void tr_comm_table_add_rp_realm(TR_COMM_TABLE *ctab, TR_RP_REALM *new)
1337 tr_rp_realm_add(ctab->rp_realms, new);
1338 if (ctab->rp_realms!=NULL)
1339 talloc_steal(ctab, ctab->rp_realms); /* make sure it's in the right context */
1342 void tr_comm_table_remove_rp_realm(TR_COMM_TABLE *ctab, TR_RP_REALM *realm)
1344 tr_rp_realm_remove(ctab->rp_realms, realm);
1347 TR_IDP_REALM *tr_comm_table_find_idp_realm(TR_COMM_TABLE *ctab, TR_NAME *realm_id)
1349 return tr_idp_realm_lookup(ctab->idp_realms, realm_id);
1352 void tr_comm_table_add_idp_realm(TR_COMM_TABLE *ctab, TR_IDP_REALM *new)
1354 tr_idp_realm_add(ctab->idp_realms, new);
1355 if (ctab->idp_realms!=NULL)
1356 talloc_steal(ctab, ctab->idp_realms); /* make sure it's in the right context */
1359 void tr_comm_table_remove_idp_realm(TR_COMM_TABLE *ctab, TR_IDP_REALM *realm)
1361 tr_idp_realm_remove(ctab->idp_realms, realm);
1365 /* how many communities in the table? */
1366 size_t tr_comm_table_size(TR_COMM_TABLE *ctab)
1369 TR_COMM *this=ctab->comms;
1377 /* clean up unreferenced realms, etc */
1378 void tr_comm_table_sweep(TR_COMM_TABLE *ctab)
1380 tr_rp_realm_sweep(ctab->rp_realms);
1381 tr_idp_realm_sweep(ctab->idp_realms);
1382 tr_comm_sweep(ctab->comms);
1386 const char *tr_realm_role_to_str(TR_REALM_ROLE role)
1398 TR_REALM_ROLE tr_realm_role_from_str(const char *s)
1400 if (strcmp(s, "idp")==0)
1402 if (strcmp(s, "rp")==0)
1404 return TR_ROLE_UNKNOWN;
1407 void tr_comm_table_print(FILE *f, TR_COMM_TABLE *ctab)
1409 TR_COMM_MEMB *p1=NULL; /* for walking the main list */
1410 TR_COMM_MEMB *p2=NULL; /* for walking the same-origin lists */
1412 fprintf(f, ">> Membership table start <<\n");
1413 for (p1=ctab->memberships; p1!=NULL; p1=p1->next) {
1414 fprintf(f, "* %s %s/%s\n %s (%p)\n",
1415 tr_realm_role_to_str(tr_comm_memb_get_role(p1)),
1416 tr_comm_memb_get_realm_id(p1)->buf,
1417 tr_comm_get_id(tr_comm_memb_get_comm(p1))->buf,
1418 (tr_comm_memb_get_origin(p1)==NULL)?"null origin":(tr_comm_memb_get_origin(p1)->buf),
1420 for (p2=p1->origin_next; p2!=NULL; p2=p2->origin_next) {
1421 fprintf(f, " %s (%p)\n",
1422 (tr_comm_memb_get_origin(p2)==NULL)?"null origin":(tr_comm_memb_get_origin(p2)->buf),