2 * Copyright (c) 2012-2018, 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 * Convert TR_NAME n to a JSON string, returning the empty string if n is null
44 static json_t *safe_name_to_json_string(TR_NAME *n)
47 return tr_name_to_json_string(n);
49 return json_string("");
52 static json_t *expiry_to_json_string(TR_COMM_MEMB *memb)
54 struct timespec ts_zero = {0, 0};
58 if (tr_cmp_timespec(tr_comm_memb_get_expiry(memb), &ts_zero) == 0) {
61 s = timespec_to_str(tr_comm_memb_get_expiry(memb));
65 jstr = json_string(s);
73 * Get the provenance from the member, handling empty provenance safely
75 static json_t *provenance_to_json(TR_COMM_MEMB *memb)
77 json_t *prov = tr_comm_memb_get_provenance(memb);
87 /* helper for below */
88 #define OBJECT_SET_OR_FAIL(jobj, key, val) \
91 json_object_set_new((jobj),(key),(val)); \
96 #define ARRAY_APPEND_OR_FAIL(jary, val) \
99 json_array_append_new((jary),(val)); \
104 static json_t *tr_comm_memb_to_json(TR_COMM_MEMB *memb)
106 json_t *memb_json = NULL;
107 json_t *retval = NULL;
109 memb_json = json_object();
110 if (memb_json == NULL)
113 if (tr_comm_memb_get_origin(memb) == NULL) {
114 OBJECT_SET_OR_FAIL(memb_json, "origin", json_string("file"));
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)));
128 /* succeeded - set the return value and increment the reference count */
134 json_decref(memb_json);
139 * Summarize the different reasons we believe a realm belongs to a community
141 static json_t *tr_comm_memb_sources_to_json(TR_COMM_MEMB *first_memb)
143 json_t *jarray = NULL;
144 json_t *retval = NULL;
145 TR_COMM_ITER *iter = NULL;
146 TR_COMM_MEMB *memb = NULL;
148 jarray = json_array();
152 iter = tr_comm_iter_new(NULL);
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);
159 ARRAY_APPEND_OR_FAIL(jarray, tr_comm_memb_to_json(memb));
160 memb = tr_comm_memb_iter_next(iter);
175 static json_t *tr_comm_realms_to_json(TR_COMM_TABLE *ctable, TR_NAME *comm_name, TR_REALM_ROLE role)
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;
184 iter = tr_comm_iter_new(NULL);
185 realm = tr_realm_iter_first(iter, ctable, comm_name);
187 /* Do not display the full realm json here, only the name and info relevant to the community listing */
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),
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 */
201 realm = tr_realm_iter_next(iter);
204 /* Success - increment the reference count so return value survives */
213 json_decref(realm_json);
216 tr_comm_iter_free(iter);
221 static json_t *tr_comm_to_json(TR_COMM_TABLE *ctable, TR_COMM *comm)
223 json_t *comm_json = NULL;
224 json_t *retval = NULL;
226 comm_json = json_object();
227 if (comm_json == NULL)
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));
236 /* just get the first apc */
237 OBJECT_SET_OR_FAIL(comm_json, "apc",
238 tr_name_to_json_string(
240 tr_comm_get_apcs(comm))));
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)));
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));
254 /* succeeded - set the return value and increment the reference count */
260 json_decref(comm_json);
264 json_t *tr_comm_table_to_json(TR_COMM_TABLE *ctable)
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;
272 ctable_json = json_array();
273 if (ctable_json == NULL)
276 iter = tr_comm_iter_new(NULL);
280 /* Iterate over communities in the table */
281 comm = tr_comm_table_iter_first(iter, ctable);
283 comm_json = tr_comm_to_json(ctable, comm);
285 if (comm_json == NULL)
288 json_array_append_new(ctable_json, comm_json);
289 comm = tr_comm_table_iter_next(iter);
292 /* succeeded - set the return value and increment the reference count */
293 retval = ctable_json;
298 tr_comm_iter_free(iter);
301 json_decref(ctable_json);