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.
39 #include <tr_name_internal.h>
41 #include <tr_config.h>
44 static char *tr_aaa_server_to_str(TALLOC_CTX *mem_ctx, TR_AAA_SERVER *aaa)
46 return talloc_strndup(mem_ctx, aaa->hostname->buf, aaa->hostname->len);
50 char *tr_idp_realm_to_str(TALLOC_CTX *mem_ctx, TR_IDP_REALM *idp)
52 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
53 char **s_aaa=NULL, *aaa_servers=NULL;
54 char **s_apc=NULL, *apcs=NULL;
55 int ii=0, aaa_servers_strlen=0, apcs_strlen=0;
56 int n_aaa_servers=tr_idp_realm_aaa_server_count(idp);
57 int n_apcs=tr_idp_realm_apc_count(idp);
58 TR_AAA_SERVER *aaa=NULL;
62 /* get the AAA servers */
64 aaa_servers=talloc_strdup(tmp_ctx, "");
66 s_aaa=talloc_array(tmp_ctx, char *, n_aaa_servers);
67 for (aaa=idp->aaa_servers,ii=0; aaa!=NULL; aaa=aaa->next,ii++) {
68 s_aaa[ii]=tr_aaa_server_to_str(s_aaa, aaa);
69 aaa_servers_strlen+=strlen(s_aaa[ii]);
72 /* add space for comma-space separators */
73 aaa_servers_strlen+=2*(n_aaa_servers-1);
75 aaa_servers=talloc_array(tmp_ctx, char, aaa_servers_strlen+1);
77 for (ii=0; ii<n_aaa_servers; ii++) {
78 strcat(aaa_servers, s_aaa[ii]);
79 if (ii<(n_aaa_servers-1))
80 strcat(aaa_servers, ", ");
87 apcs=talloc_strdup(tmp_ctx, "");
89 s_apc=talloc_array(tmp_ctx, char *, n_apcs);
90 for (apc=idp->apcs,ii=0; apc!=NULL; apc=apc->next,ii++) {
91 s_apc[ii]=tr_apc_to_str(s_apc, apc);
92 apcs_strlen+=strlen(s_apc[ii]);
95 /* add space for comma-space separators */
96 apcs_strlen+=2*(n_apcs-1);
98 apcs=talloc_array(tmp_ctx, char, apcs_strlen+1);
100 for (ii=0; ii<n_apcs; ii++) {
101 strcat(apcs, s_apc[ii]);
108 result=talloc_asprintf(mem_ctx,
109 "IDP realm: \"%.*s\""
114 idp->realm_id->len, idp->realm_id->buf,
115 (idp->shared_config)?"yes":"no",
116 (idp->origin==TR_REALM_LOCAL)?"yes":"no",
119 talloc_free(tmp_ctx);
124 /* helper for below */
125 #define OBJECT_SET_OR_FAIL(jobj, key, val) \
128 json_object_set_new((jobj),(key),(val)); \
133 #define ARRAY_APPEND_OR_FAIL(jary, val) \
136 json_array_append_new((jary),(val)); \
141 static json_t *tr_apcs_to_json(TR_APC *apcs)
143 json_t *jarray = json_array();
144 json_t *retval = NULL;
145 TR_APC_ITER *iter = tr_apc_iter_new(NULL);
148 if ((jarray == NULL) || (iter == NULL))
151 apc = tr_apc_iter_first(iter, apcs);
153 ARRAY_APPEND_OR_FAIL(jarray, tr_name_to_json_string(tr_apc_get_id(apc)));
154 apc = tr_apc_iter_next(iter);
168 static json_t *tr_aaa_server_to_json(TR_AAA_SERVER *aaa)
170 char *hostname = tr_name_strdup(aaa->hostname);
174 if (hostname == NULL)
177 s = talloc_asprintf(NULL, "%s:%d", hostname, TID_PORT);
179 jstr = json_string(s);
185 static json_t *tr_aaa_servers_to_json(TR_AAA_SERVER *aaas)
187 json_t *jarray = json_array();
188 json_t *retval = NULL;
189 TR_AAA_SERVER_ITER *iter = tr_aaa_server_iter_new(NULL);
190 TR_AAA_SERVER *aaa = NULL;
192 if ((jarray == NULL) || (iter == NULL))
195 aaa = tr_aaa_server_iter_first(iter, aaas);
197 ARRAY_APPEND_OR_FAIL(jarray, tr_aaa_server_to_json(aaa));
198 aaa = tr_aaa_server_iter_next(iter);
212 static json_t *tr_idp_realm_to_json(TR_IDP_REALM *idp)
214 json_t *idp_json = json_object();
215 json_t *retval = NULL;
217 if (idp_json == NULL)
225 OBJECT_SET_OR_FAIL(idp_json, "realm",
226 tr_name_to_json_string(tr_idp_realm_get_id(idp)));
227 OBJECT_SET_OR_FAIL(idp_json, "discovered",
228 json_boolean(idp->origin == TR_REALM_DISCOVERED));
229 OBJECT_SET_OR_FAIL(idp_json, "apcs",
230 tr_apcs_to_json(tr_idp_realm_get_apcs(idp)));
231 OBJECT_SET_OR_FAIL(idp_json, "aaa_servers",
232 tr_aaa_servers_to_json(idp->aaa_servers));
233 OBJECT_SET_OR_FAIL(idp_json, "shared_config",
234 json_boolean(idp->shared_config));
237 json_decref(idp_json);
242 json_t *tr_idp_realms_to_json(TR_IDP_REALM *idps)
245 json_t *jarray = json_array();
246 json_t *retval = NULL;
247 TR_IDP_REALM *this = NULL;
252 for (this=idps; this != NULL; this=this->next)
253 ARRAY_APPEND_OR_FAIL(jarray, tr_idp_realm_to_json(this));