Parsing new-style config files partially working.
[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   return 0;
48 }
49
50 TR_COMM *tr_comm_new(TALLOC_CTX *mem_ctx)
51 {
52   TR_COMM *comm=talloc(mem_ctx, TR_COMM);
53   if (comm!=NULL) {
54     comm->next=NULL;
55     comm->id=NULL;
56     comm->type=TR_COMM_UNKNOWN;
57     comm->apcs=NULL;
58     comm->idp_realms=NULL;
59     comm->rp_realms=NULL;
60     talloc_set_destructor((void *)comm, tr_comm_destructor);
61   }
62   return comm;
63 }
64
65 void tr_comm_free(TR_COMM *comm)
66 {
67   talloc_free(comm);
68 }
69
70 /* does not take responsibility for freeing IDP realm */
71 void tr_comm_add_idp_realm(TR_COMM *comm, TR_IDP_REALM *realm)
72 {
73   TR_IDP_REALM *cur=NULL;
74
75   if (comm->idp_realms==NULL)
76     comm->idp_realms=realm;
77   else {
78     for (cur=comm->idp_realms; cur->comm_next!=NULL; cur=cur->comm_next) { }
79     cur->comm_next=realm;
80   }
81 }
82
83 /* does not take responsibility for freeing RP realm */
84 void tr_comm_add_rp_realm(TR_COMM *comm, TR_RP_REALM *realm)
85 {
86   TR_RP_REALM *cur=NULL;
87
88   if (comm->rp_realms==NULL)
89     comm->rp_realms=realm;
90   else {
91     for (cur=comm->rp_realms; cur->next!=NULL; cur=cur->next) { }
92     cur->next=realm;
93   }
94 }
95
96 static TR_COMM *tr_comm_tail(TR_COMM *comm)
97 {
98   if (comm==NULL)
99     return comm;
100
101   while (comm->next!=NULL)
102     comm=comm->next;
103   return comm;
104 }
105
106 /* All list members are in the talloc context of the head.
107  * This will require careful thought if entries are ever removed
108  * or shuffled between lists. 
109  * Call like comms=tr_comm_add(comms, new_comm); */
110 TR_COMM *tr_comm_add(TR_COMM *comms, TR_COMM *new)
111 {
112   if (comms==NULL)
113     comms=new;
114   else {
115     tr_comm_tail(comms)->next=new;
116     while(new!=NULL) {
117       talloc_steal(comms, new);
118       new=new->next;
119     }
120   }
121   return comms;
122 }
123
124 TR_IDP_REALM *tr_find_comm_idp (TR_COMM *comm, TR_NAME *idp_realm)
125 {
126   TR_IDP_REALM *idp;
127
128   if ((!comm) || (!idp_realm)) {
129     return NULL;
130   }
131
132   for (idp = comm->idp_realms; NULL != idp; idp = idp->comm_next) {
133     if (!tr_name_cmp (idp_realm, idp->realm_id)) {
134       tr_debug("tr_find_comm_idp: Found IdP %s in community %s.", idp_realm->buf, comm->id->buf);
135       return idp;
136     }
137   }
138   /* if we didn't find one, return NULL */ 
139   return NULL;
140 }
141
142 TR_RP_REALM *tr_find_comm_rp (TR_COMM *comm, TR_NAME *rp_realm)
143 {
144   TR_RP_REALM *rp;
145
146   if ((!comm) || (!rp_realm)) {
147     return NULL;
148   }
149
150   for (rp = comm->rp_realms; NULL != rp; rp = rp->next) {
151     if (!tr_name_cmp (rp_realm, rp->realm_name)) {
152       tr_debug("tr_find_comm_rp: Found RP %s in community %s.", rp_realm->buf, comm->id->buf);
153       return rp;
154     }
155   }
156   /* if we didn't find one, return NULL */ 
157   return NULL;
158 }
159
160 TR_COMM *tr_comm_lookup(TR_COMM *comms, TR_NAME *comm_name) 
161 {
162   TR_COMM *cfg_comm = NULL;
163
164   for (cfg_comm = comms; NULL != cfg_comm; cfg_comm = cfg_comm->next) {
165     if ((cfg_comm->id->len == comm_name->len) &&
166         (!strncmp(cfg_comm->id->buf, comm_name->buf, comm_name->len)))
167       return cfg_comm;
168   }
169   return NULL;
170 }