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