Rename acceptor_realm/name to _hostname/service, add some debug output
[trust_router.git] / common / tr_comm_encoders.c
1 /*
2  * Copyright (c) 2012-2018, 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 <jansson.h>
36 #include <tr_idp.h>
37 #include <tr_comm.h>
38 #include <tr_util.h>
39 #include <tr_debug.h>
40
41 /**
42  * Convert TR_NAME n to a JSON string, returning the empty string if n is null
43  */
44 static json_t *safe_name_to_json_string(TR_NAME *n)
45 {
46   if (n)
47     return tr_name_to_json_string(n);
48   else
49     return json_string("");
50 }
51
52 static json_t *expiry_to_json_string(TR_COMM_MEMB *memb)
53 {
54   struct timespec ts_zero = {0, 0};
55   char *s = NULL;
56   json_t *jstr = NULL;
57
58   if (tr_cmp_timespec(tr_comm_memb_get_expiry(memb), &ts_zero) == 0) {
59     s = strdup("");
60   } else {
61     s = timespec_to_str(tr_comm_memb_get_expiry(memb));
62   }
63
64   if (s) {
65     jstr = json_string(s);
66     free(s);
67   }
68
69   return jstr;
70 }
71
72 /**
73  * Get the provenance from the member, handling empty provenance safely
74  */
75 static json_t *provenance_to_json(TR_COMM_MEMB *memb)
76 {
77   json_t *prov = tr_comm_memb_get_provenance(memb);
78
79   if (prov) {
80     json_incref(prov);
81     return prov;
82   } else {
83     return json_array();
84   }
85 }
86
87 /* helper for below */
88 #define OBJECT_SET_OR_FAIL(jobj, key, val)     \
89 do {                                           \
90   if (val)                                     \
91     json_object_set_new((jobj),(key),(val));   \
92   else                                         \
93     goto cleanup;                              \
94 } while (0)
95
96 #define ARRAY_APPEND_OR_FAIL(jary, val)        \
97 do {                                           \
98   if (val)                                     \
99     json_array_append_new((jary),(val));       \
100   else                                         \
101     goto cleanup;                              \
102 } while (0)
103
104 static json_t *tr_comm_memb_to_json(TR_COMM_MEMB *memb)
105 {
106   json_t *memb_json = NULL;
107   json_t *retval = NULL;
108
109   memb_json = json_object();
110   if (memb_json == NULL)
111     goto cleanup;
112
113   if (tr_comm_memb_get_origin(memb) == NULL) {
114     OBJECT_SET_OR_FAIL(memb_json, "origin", json_string("file"));
115   } else {
116     OBJECT_SET_OR_FAIL(memb_json, "origin",
117                        safe_name_to_json_string(tr_comm_memb_get_origin(memb)));
118     OBJECT_SET_OR_FAIL(memb_json, "provenance",
119                        provenance_to_json(memb));
120     OBJECT_SET_OR_FAIL(memb_json, "expires",
121                        expiry_to_json_string(memb));
122     OBJECT_SET_OR_FAIL(memb_json, "announce_interval",
123                        json_integer(tr_comm_memb_get_interval(memb)));
124     OBJECT_SET_OR_FAIL(memb_json, "times_expired",
125                        json_integer(tr_comm_memb_get_times_expired(memb)));
126   }
127
128   /* succeeded - set the return value and increment the reference count */
129   retval = memb_json;
130   json_incref(retval);
131
132 cleanup:
133   if (memb_json)
134     json_decref(memb_json);
135   return retval;
136 }
137
138 /**
139  * Summarize the different reasons we believe a realm belongs to a community
140  */
141 static json_t *tr_comm_memb_sources_to_json(TR_COMM_MEMB *first_memb)
142 {
143   json_t *jarray = NULL;
144   json_t *retval = NULL;
145   TR_COMM_ITER *iter = NULL;
146   TR_COMM_MEMB *memb = NULL;
147
148   jarray = json_array();
149   if (jarray == NULL)
150     goto cleanup;
151
152   iter = tr_comm_iter_new(NULL);
153   if (iter == NULL)
154     goto cleanup;
155
156   /* Iterate over all the memberships for this realm/comm pair that come from different origins */
157   memb = tr_comm_memb_iter_first(iter, first_memb);
158   while (memb) {
159     ARRAY_APPEND_OR_FAIL(jarray, tr_comm_memb_to_json(memb));
160     memb = tr_comm_memb_iter_next(iter);
161   }
162
163   /* success */
164   retval = jarray;
165   json_incref(retval);
166
167 cleanup:
168   if (jarray)
169     json_decref(jarray);
170   if (iter)
171     talloc_free(iter);
172   return retval;
173 }
174
175 static json_t *tr_comm_realms_to_json(TR_COMM_TABLE *ctable, TR_NAME *comm_name, TR_REALM_ROLE role)
176 {
177   json_t *jarray = json_array();
178   json_t *realm_json = NULL;
179   json_t *retval = NULL;
180   TR_COMM_ITER *iter = NULL;
181   TR_REALM *realm = NULL;
182   TR_COMM_MEMB *memb = NULL;
183
184   iter = tr_comm_iter_new(NULL);
185   realm = tr_realm_iter_first(iter, ctable, comm_name);
186
187   /* Do not display the full realm json here, only the name and info relevant to the community listing */
188   while(realm) {
189     if (realm->role == role) {
190       realm_json = json_object();
191       OBJECT_SET_OR_FAIL(realm_json, "realm",
192                          tr_name_to_json_string(tr_realm_get_id(realm)));
193       memb = tr_comm_table_find_idp_memb(ctable,
194                                          tr_realm_get_id(realm),
195                                          comm_name);
196       OBJECT_SET_OR_FAIL(realm_json, "sources",
197                          tr_comm_memb_sources_to_json(memb));
198       json_array_append_new(jarray, realm_json);
199       realm_json = NULL; /* so we don't free this twice during cleanup */
200     }
201     realm = tr_realm_iter_next(iter);
202   }
203
204   /* Success - increment the reference count so return value survives */
205   retval = jarray;
206   json_incref(retval);
207
208 cleanup:
209   if (jarray)
210     json_decref(jarray);
211
212   if (realm_json)
213     json_decref(realm_json);
214
215   if (iter)
216     tr_comm_iter_free(iter);
217
218   return retval;
219 }
220
221 static json_t *tr_comm_to_json(TR_COMM_TABLE *ctable, TR_COMM *comm)
222 {
223   json_t *comm_json = NULL;
224   json_t *retval = NULL;
225
226   comm_json = json_object();
227   if (comm_json == NULL)
228     goto cleanup;
229
230   OBJECT_SET_OR_FAIL(comm_json, "type",
231                      json_string(tr_comm_type_to_str(tr_comm_get_type(comm))));
232   if (tr_comm_get_type(comm) == TR_COMM_APC) {
233     OBJECT_SET_OR_FAIL(comm_json, "expiration_interval",
234                        json_integer(comm->expiration_interval));
235   } else {
236     /* just get the first apc */
237     OBJECT_SET_OR_FAIL(comm_json, "apc",
238                        tr_name_to_json_string(
239                            tr_apc_get_id(
240                                tr_comm_get_apcs(comm))));
241   }
242   OBJECT_SET_OR_FAIL(comm_json, "name",
243                      tr_name_to_json_string(tr_comm_get_id(comm)));
244   OBJECT_SET_OR_FAIL(comm_json, "owner_realm",
245                      safe_name_to_json_string(tr_comm_get_owner_realm(comm)));
246   OBJECT_SET_OR_FAIL(comm_json, "owner_contact",
247                      safe_name_to_json_string(tr_comm_get_owner_contact(comm)));
248
249   OBJECT_SET_OR_FAIL(comm_json, "idp_realms",
250                      tr_comm_realms_to_json(ctable, tr_comm_get_id(comm), TR_ROLE_IDP));
251   OBJECT_SET_OR_FAIL(comm_json, "rp_realms",
252                      tr_comm_realms_to_json(ctable, tr_comm_get_id(comm), TR_ROLE_RP));
253
254   /* succeeded - set the return value and increment the reference count */
255   retval = comm_json;
256   json_incref(retval);
257
258   cleanup:
259   if (comm_json)
260     json_decref(comm_json);
261   return retval;
262 }
263
264 json_t *tr_comm_table_to_json(TR_COMM_TABLE *ctable)
265 {
266   json_t *ctable_json = NULL;
267   json_t *retval = NULL;
268   json_t *comm_json = NULL;
269   TR_COMM_ITER *iter = NULL;
270   TR_COMM *comm = NULL;
271
272   ctable_json = json_array();
273   if (ctable_json == NULL)
274     goto cleanup;
275
276   iter = tr_comm_iter_new(NULL);
277   if (iter == NULL)
278     goto cleanup;
279
280   /* Iterate over communities in the table */
281   comm = tr_comm_table_iter_first(iter, ctable);
282   while (comm) {
283     comm_json = tr_comm_to_json(ctable, comm);
284
285     if (comm_json == NULL)
286       goto cleanup;
287
288     json_array_append_new(ctable_json, comm_json);
289     comm = tr_comm_table_iter_next(iter);
290   }
291
292   /* succeeded - set the return value and increment the reference count */
293   retval = ctable_json;
294   json_incref(retval);
295
296 cleanup:
297   if (iter)
298     tr_comm_iter_free(iter);
299
300   if (ctable_json)
301     json_decref(ctable_json);
302
303   return retval;
304  
305 }