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