8a5a9b66136bf4b5b39ff73c451f1c371ccde429
[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   while(realm) {
187     if (realm->role == role) {
188       realm_json = json_object();
189       OBJECT_SET_OR_FAIL(realm_json, "realm",
190                          tr_name_to_json_string(tr_realm_get_id(realm)));
191       memb = tr_comm_table_find_idp_memb(ctable,
192                                          tr_realm_get_id(realm),
193                                          comm_name);
194       OBJECT_SET_OR_FAIL(realm_json, "sources",
195                          tr_comm_memb_sources_to_json(memb));
196       json_array_append_new(jarray, realm_json);
197       realm_json = NULL; /* so we don't free this twice during cleanup */
198     }
199     realm = tr_realm_iter_next(iter);
200   }
201
202   /* Success - increment the reference count so return value survives */
203   retval = jarray;
204   json_incref(retval);
205
206 cleanup:
207   if (jarray)
208     json_decref(jarray);
209
210   if (realm_json)
211     json_decref(realm_json);
212
213   if (iter)
214     tr_comm_iter_free(iter);
215
216   return retval;
217 }
218
219 static json_t *tr_comm_to_json(TR_COMM_TABLE *ctable, TR_COMM *comm)
220 {
221   json_t *comm_json = NULL;
222   json_t *retval = NULL;
223
224   comm_json = json_object();
225   if (comm_json == NULL)
226     goto cleanup;
227
228   OBJECT_SET_OR_FAIL(comm_json, "type",
229                      json_string(tr_comm_type_to_str(tr_comm_get_type(comm))));
230   if (tr_comm_get_type(comm) == TR_COMM_APC) {
231     OBJECT_SET_OR_FAIL(comm_json, "expiration_interval",
232                        json_integer(comm->expiration_interval));
233   } else {
234     /* just get the first apc */
235     OBJECT_SET_OR_FAIL(comm_json, "apc",
236                        tr_name_to_json_string(
237                            tr_apc_get_id(
238                                tr_comm_get_apcs(comm))));
239   }
240   OBJECT_SET_OR_FAIL(comm_json, "name",
241                      tr_name_to_json_string(tr_comm_get_id(comm)));
242   OBJECT_SET_OR_FAIL(comm_json, "owner_realm",
243                      safe_name_to_json_string(tr_comm_get_owner_realm(comm)));
244   OBJECT_SET_OR_FAIL(comm_json, "owner_contact",
245                      safe_name_to_json_string(tr_comm_get_owner_contact(comm)));
246
247   OBJECT_SET_OR_FAIL(comm_json, "idp_realms",
248                      tr_comm_realms_to_json(ctable, tr_comm_get_id(comm), TR_ROLE_IDP));
249   OBJECT_SET_OR_FAIL(comm_json, "rp_realms",
250                      tr_comm_realms_to_json(ctable, tr_comm_get_id(comm), TR_ROLE_RP));
251
252   /* succeeded - set the return value and increment the reference count */
253   retval = comm_json;
254   json_incref(retval);
255
256   cleanup:
257   if (comm_json)
258     json_decref(comm_json);
259   return retval;
260 }
261
262 json_t *tr_comm_table_to_json(TR_COMM_TABLE *ctable)
263 {
264   json_t *ctable_json = NULL;
265   json_t *retval = NULL;
266   json_t *comm_json = NULL;
267   TR_COMM_ITER *iter = NULL;
268   TR_COMM *comm = NULL;
269
270   ctable_json = json_array();
271   if (ctable_json == NULL)
272     goto cleanup;
273
274   iter = tr_comm_iter_new(NULL);
275   if (iter == NULL)
276     goto cleanup;
277
278   /* Iterate over communities in the table */
279   comm = tr_comm_table_iter_first(iter, ctable);
280   while (comm) {
281     comm_json = tr_comm_to_json(ctable, comm);
282
283     if (comm_json == NULL)
284       goto cleanup;
285
286     json_array_append_new(ctable_json, comm_json);
287     comm = tr_comm_table_iter_next(iter);
288   }
289
290   /* succeeded - set the return value and increment the reference count */
291   retval = ctable_json;
292   json_incref(retval);
293
294 cleanup:
295   if (iter)
296     tr_comm_iter_free(iter);
297
298   if (ctable_json)
299     json_decref(ctable_json);
300
301   return retval;
302  
303 }