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>
43 #include <tr_json_util.h>
45 static char *tr_aaa_server_to_str(TALLOC_CTX *mem_ctx, TR_AAA_SERVER *aaa)
47 return talloc_strndup(mem_ctx, aaa->hostname->buf, aaa->hostname->len);
51 char *tr_idp_realm_to_str(TALLOC_CTX *mem_ctx, TR_IDP_REALM *idp)
53 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
54 char **s_aaa=NULL, *aaa_servers=NULL;
55 char **s_apc=NULL, *apcs=NULL;
56 int ii=0, aaa_servers_strlen=0, apcs_strlen=0;
57 int n_aaa_servers=tr_idp_realm_aaa_server_count(idp);
58 int n_apcs=tr_idp_realm_apc_count(idp);
59 TR_AAA_SERVER *aaa=NULL;
63 /* get the AAA servers */
65 aaa_servers=talloc_strdup(tmp_ctx, "");
67 s_aaa=talloc_array(tmp_ctx, char *, n_aaa_servers);
68 for (aaa=idp->aaa_servers,ii=0; aaa!=NULL; aaa=aaa->next,ii++) {
69 s_aaa[ii]=tr_aaa_server_to_str(s_aaa, aaa);
70 aaa_servers_strlen+=strlen(s_aaa[ii]);
73 /* add space for comma-space separators */
74 aaa_servers_strlen+=2*(n_aaa_servers-1);
76 aaa_servers=talloc_array(tmp_ctx, char, aaa_servers_strlen+1);
78 for (ii=0; ii<n_aaa_servers; ii++) {
79 strcat(aaa_servers, s_aaa[ii]);
80 if (ii<(n_aaa_servers-1))
81 strcat(aaa_servers, ", ");
88 apcs=talloc_strdup(tmp_ctx, "");
90 s_apc=talloc_array(tmp_ctx, char *, n_apcs);
91 for (apc=idp->apcs,ii=0; apc!=NULL; apc=apc->next,ii++) {
92 s_apc[ii]=tr_apc_to_str(s_apc, apc);
93 apcs_strlen+=strlen(s_apc[ii]);
96 /* add space for comma-space separators */
97 apcs_strlen+=2*(n_apcs-1);
99 apcs=talloc_array(tmp_ctx, char, apcs_strlen+1);
101 for (ii=0; ii<n_apcs; ii++) {
102 strcat(apcs, s_apc[ii]);
109 result=talloc_asprintf(mem_ctx,
110 "IDP realm: \"%.*s\""
115 idp->realm_id->len, idp->realm_id->buf,
116 (idp->shared_config)?"yes":"no",
117 (idp->origin==TR_REALM_LOCAL)?"yes":"no",
120 talloc_free(tmp_ctx);
125 static json_t *tr_apcs_to_json(TR_APC *apcs)
127 json_t *jarray = json_array();
128 json_t *retval = NULL;
129 TR_APC_ITER *iter = tr_apc_iter_new(NULL);
132 if ((jarray == NULL) || (iter == NULL))
135 for (apc = tr_apc_iter_first(iter, apcs);
137 apc = tr_apc_iter_next(iter)) {
138 ARRAY_APPEND_OR_FAIL(jarray, tr_name_to_json_string(tr_apc_get_id(apc)));
152 static json_t *tr_aaa_server_to_json(TR_AAA_SERVER *aaa)
154 char *hostname = tr_name_strdup(aaa->hostname);
158 if (hostname == NULL)
161 s = talloc_asprintf(NULL, "%s:%d", hostname, TID_PORT);
163 jstr = json_string(s);
169 static json_t *tr_aaa_servers_to_json(TR_AAA_SERVER *aaas)
171 json_t *jarray = json_array();
172 json_t *retval = NULL;
173 TR_AAA_SERVER_ITER *iter = tr_aaa_server_iter_new(NULL);
174 TR_AAA_SERVER *aaa = NULL;
176 if ((jarray == NULL) || (iter == NULL))
179 for (aaa = tr_aaa_server_iter_first(iter, aaas);
181 aaa = tr_aaa_server_iter_next(iter)) {
182 ARRAY_APPEND_OR_FAIL(jarray, tr_aaa_server_to_json(aaa));
196 static json_t *tr_idp_realm_to_json(TR_IDP_REALM *idp)
198 json_t *idp_json = json_object();
199 json_t *retval = NULL;
201 if (idp_json == NULL)
209 OBJECT_SET_OR_FAIL(idp_json, "realm",
210 tr_name_to_json_string(tr_idp_realm_get_id(idp)));
211 OBJECT_SET_OR_FAIL(idp_json, "discovered",
212 json_boolean(idp->origin == TR_REALM_DISCOVERED));
213 OBJECT_SET_OR_FAIL(idp_json, "apcs",
214 tr_apcs_to_json(tr_idp_realm_get_apcs(idp)));
215 OBJECT_SET_OR_FAIL(idp_json, "aaa_servers",
216 tr_aaa_servers_to_json(idp->aaa_servers));
217 OBJECT_SET_OR_FAIL(idp_json, "shared_config",
218 json_boolean(idp->shared_config));
221 json_decref(idp_json);
226 json_t *tr_idp_realms_to_json(TR_IDP_REALM *idps)
229 json_t *jarray = json_array();
230 json_t *retval = NULL;
231 TR_IDP_REALM *this = NULL;
236 for (this=idps; this != NULL; this=this->next)
237 ARRAY_APPEND_OR_FAIL(jarray, tr_idp_realm_to_json(this));