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 char *aaa_hostname = tr_name_strdup( tr_aaa_server_get_hostname(aaa) );
50 if (aaa_hostname == NULL)
53 result = talloc_asprintf(mem_ctx,
56 tr_aaa_server_get_port(aaa));
62 char *tr_idp_realm_to_str(TALLOC_CTX *mem_ctx, TR_IDP_REALM *idp)
64 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
65 char **s_aaa=NULL, *aaa_servers=NULL;
66 char **s_apc=NULL, *apcs=NULL;
67 int ii=0, aaa_servers_strlen=0, apcs_strlen=0;
68 int n_aaa_servers=tr_idp_realm_aaa_server_count(idp);
69 int n_apcs=tr_idp_realm_apc_count(idp);
70 TR_AAA_SERVER *aaa=NULL;
74 /* get the AAA servers */
76 aaa_servers=talloc_strdup(tmp_ctx, "");
78 s_aaa=talloc_array(tmp_ctx, char *, n_aaa_servers);
79 for (aaa=idp->aaa_servers,ii=0; aaa!=NULL; aaa=aaa->next,ii++) {
80 s_aaa[ii]=tr_aaa_server_to_str(s_aaa, aaa);
81 aaa_servers_strlen+=strlen(s_aaa[ii]);
84 /* add space for comma-space separators */
85 aaa_servers_strlen+=2*(n_aaa_servers-1);
87 aaa_servers=talloc_array(tmp_ctx, char, aaa_servers_strlen+1);
89 for (ii=0; ii<n_aaa_servers; ii++) {
90 strcat(aaa_servers, s_aaa[ii]);
91 if (ii<(n_aaa_servers-1))
92 strcat(aaa_servers, ", ");
99 apcs=talloc_strdup(tmp_ctx, "");
101 s_apc=talloc_array(tmp_ctx, char *, n_apcs);
102 for (apc=idp->apcs,ii=0; apc!=NULL; apc=apc->next,ii++) {
103 s_apc[ii]=tr_apc_to_str(s_apc, apc);
104 apcs_strlen+=strlen(s_apc[ii]);
107 /* add space for comma-space separators */
108 apcs_strlen+=2*(n_apcs-1);
110 apcs=talloc_array(tmp_ctx, char, apcs_strlen+1);
112 for (ii=0; ii<n_apcs; ii++) {
113 strcat(apcs, s_apc[ii]);
120 result=talloc_asprintf(mem_ctx,
121 "IDP realm: \"%.*s\""
126 idp->realm_id->len, idp->realm_id->buf,
127 (idp->shared_config)?"yes":"no",
128 (idp->origin==TR_REALM_LOCAL)?"yes":"no",
131 talloc_free(tmp_ctx);
136 static json_t *tr_apcs_to_json(TR_APC *apcs)
138 json_t *jarray = json_array();
139 json_t *retval = NULL;
140 TR_APC_ITER *iter = tr_apc_iter_new(NULL);
143 if ((jarray == NULL) || (iter == NULL))
146 for (apc = tr_apc_iter_first(iter, apcs);
148 apc = tr_apc_iter_next(iter)) {
149 ARRAY_APPEND_OR_FAIL(jarray, tr_name_to_json_string(tr_apc_get_id(apc)));
163 static json_t *tr_aaa_server_to_json(TR_AAA_SERVER *aaa)
165 char *s = tr_aaa_server_to_str(NULL, aaa);
169 jstr = json_string(s);
175 static json_t *tr_aaa_servers_to_json(TR_AAA_SERVER *aaas)
177 json_t *jarray = json_array();
178 json_t *retval = NULL;
179 TR_AAA_SERVER_ITER *iter = tr_aaa_server_iter_new(NULL);
180 TR_AAA_SERVER *aaa = NULL;
182 if ((jarray == NULL) || (iter == NULL))
185 for (aaa = tr_aaa_server_iter_first(iter, aaas);
187 aaa = tr_aaa_server_iter_next(iter)) {
188 ARRAY_APPEND_OR_FAIL(jarray, tr_aaa_server_to_json(aaa));
202 static json_t *tr_idp_realm_to_json(TR_IDP_REALM *idp)
204 json_t *idp_json = json_object();
205 json_t *retval = NULL;
207 if (idp_json == NULL)
215 OBJECT_SET_OR_FAIL(idp_json, "realm",
216 tr_name_to_json_string(tr_idp_realm_get_id(idp)));
217 OBJECT_SET_OR_FAIL(idp_json, "discovered",
218 json_boolean(idp->origin == TR_REALM_DISCOVERED));
219 OBJECT_SET_OR_FAIL(idp_json, "apcs",
220 tr_apcs_to_json(tr_idp_realm_get_apcs(idp)));
221 OBJECT_SET_OR_FAIL(idp_json, "aaa_servers",
222 tr_aaa_servers_to_json(idp->aaa_servers));
223 OBJECT_SET_OR_FAIL(idp_json, "shared_config",
224 json_boolean(idp->shared_config));
227 json_decref(idp_json);
232 json_t *tr_idp_realms_to_json(TR_IDP_REALM *idps)
235 json_t *jarray = json_array();
236 json_t *retval = NULL;
237 TR_IDP_REALM *this = NULL;
242 for (this=idps; this != NULL; this=this->next)
243 ARRAY_APPEND_OR_FAIL(jarray, tr_idp_realm_to_json(this));