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.
41 static json_t *expiry_to_json_string(TR_COMM_MEMB *memb)
43 struct timespec ts_zero = {0, 0};
47 if (tr_cmp_timespec(tr_comm_memb_get_expiry(memb), &ts_zero) == 0) {
50 s = timespec_to_str(tr_comm_memb_get_expiry(memb));
54 jstr = json_string(s);
62 * Get the provenance from the member, handling empty provenance safely
64 static json_t *provenance_to_json(TR_COMM_MEMB *memb)
66 json_t *prov = tr_comm_memb_get_provenance(memb);
76 /* helper for below */
77 #define OBJECT_SET_OR_FAIL(jobj, key, val) \
80 json_object_set_new((jobj),(key),(val)); \
85 #define ARRAY_APPEND_OR_FAIL(jary, val) \
88 json_array_append_new((jary),(val)); \
93 static json_t *tr_comm_memb_to_json(TR_COMM_MEMB *memb)
95 json_t *memb_json = NULL;
96 json_t *retval = NULL;
98 memb_json = json_object();
99 if (memb_json == NULL)
102 if (tr_comm_memb_get_origin(memb) == NULL) {
103 OBJECT_SET_OR_FAIL(memb_json, "origin", json_string("file"));
105 OBJECT_SET_OR_FAIL(memb_json, "origin",
106 tr_name_to_json_string(tr_comm_memb_get_origin(memb)));
107 OBJECT_SET_OR_FAIL(memb_json, "provenance",
108 provenance_to_json(memb));
109 OBJECT_SET_OR_FAIL(memb_json, "expires",
110 expiry_to_json_string(memb));
111 OBJECT_SET_OR_FAIL(memb_json, "announce_interval",
112 json_integer(tr_comm_memb_get_interval(memb)));
113 OBJECT_SET_OR_FAIL(memb_json, "times_expired",
114 json_integer(tr_comm_memb_get_times_expired(memb)));
117 /* succeeded - set the return value and increment the reference count */
123 json_decref(memb_json);
128 * Summarize the different reasons we believe a realm belongs to a community
130 static json_t *tr_comm_memb_sources_to_json(TR_COMM_MEMB *first_memb)
132 json_t *jarray = NULL;
133 json_t *retval = NULL;
134 TR_COMM_ITER *iter = NULL;
135 TR_COMM_MEMB *memb = NULL;
137 jarray = json_array();
141 iter = tr_comm_iter_new(NULL);
145 /* Iterate over all the memberships for this realm/comm pair that come from different origins */
146 for (memb = tr_comm_memb_iter_first(iter, first_memb);
148 memb = tr_comm_memb_iter_next(iter)) {
149 ARRAY_APPEND_OR_FAIL(jarray, tr_comm_memb_to_json(memb));
164 static json_t *tr_comm_realms_to_json(TR_COMM_TABLE *ctable, TR_NAME *comm_name, TR_REALM_ROLE role)
166 json_t *jarray = json_array();
167 json_t *realm_json = NULL;
168 json_t *retval = NULL;
169 TR_COMM_ITER *iter = NULL;
170 TR_REALM *realm = NULL;
171 TR_COMM_MEMB *memb = NULL;
173 iter = tr_comm_iter_new(NULL);
176 /* Do not display the full realm json here, only the name and info relevant to the community listing */
177 for (realm = tr_realm_iter_first(iter, ctable, comm_name);
179 realm = tr_realm_iter_next(iter)) {
180 if (realm->role == role) {
181 realm_json = json_object();
182 OBJECT_SET_OR_FAIL(realm_json, "realm",
183 tr_name_to_json_string(tr_realm_get_id(realm)));
184 memb = tr_comm_table_find_memb(ctable,
185 tr_realm_get_id(realm),
188 /* This should not happen - there must be a matching membership if we
189 * believed the realm was in the community in the first place! */
192 OBJECT_SET_OR_FAIL(realm_json, "sources",
193 tr_comm_memb_sources_to_json(memb));
194 json_array_append_new(jarray, realm_json);
195 realm_json = NULL; /* so we don't free this twice during cleanup */
199 /* Success - increment the reference count so return value survives */
208 json_decref(realm_json);
211 tr_comm_iter_free(iter);
216 static json_t *tr_comm_to_json(TR_COMM_TABLE *ctable, TR_COMM *comm)
218 json_t *comm_json = NULL;
219 json_t *retval = NULL;
221 comm_json = json_object();
222 if (comm_json == NULL)
225 OBJECT_SET_OR_FAIL(comm_json, "type",
226 json_string(tr_comm_type_to_str(tr_comm_get_type(comm))));
227 if (tr_comm_get_type(comm) == TR_COMM_APC) {
228 OBJECT_SET_OR_FAIL(comm_json, "expiration_interval",
229 json_integer(comm->expiration_interval));
231 /* just get the first apc */
232 OBJECT_SET_OR_FAIL(comm_json, "apc",
233 tr_name_to_json_string(
235 tr_comm_get_apcs(comm))));
237 OBJECT_SET_OR_FAIL(comm_json, "name",
238 tr_name_to_json_string(tr_comm_get_id(comm)));
239 if (tr_comm_get_owner_realm(comm)) {
240 OBJECT_SET_OR_FAIL(comm_json, "owner_realm",
241 tr_name_to_json_string(tr_comm_get_owner_realm(comm)));
243 if (tr_comm_get_owner_contact(comm)) {
244 OBJECT_SET_OR_FAIL(comm_json, "owner_contact",
245 tr_name_to_json_string(tr_comm_get_owner_contact(comm)));
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));
252 /* succeeded - set the return value and increment the reference count */
258 json_decref(comm_json);
262 json_t *tr_comm_table_to_json(TR_COMM_TABLE *ctable)
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;
270 ctable_json = json_array();
271 if (ctable_json == NULL)
274 iter = tr_comm_iter_new(NULL);
278 /* Iterate over communities in the table */
279 for (comm = tr_comm_table_iter_first(iter, ctable);
281 comm = tr_comm_table_iter_next(iter)) {
282 comm_json = tr_comm_to_json(ctable, comm);
284 if (comm_json == NULL)
287 json_array_append_new(ctable_json, comm_json);
290 /* succeeded - set the return value and increment the reference count */
291 retval = ctable_json;
296 tr_comm_iter_free(iter);
299 json_decref(ctable_json);