Move repeated #defines into tr_json_util.h and add documentation
[trust_router.git] / common / tr_idp_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 <talloc.h>
36 #include <time.h>
37 #include <jansson.h>
38
39 #include <tr_name_internal.h>
40 #include <tr_idp.h>
41 #include <tr_config.h>
42 #include <tr_debug.h>
43 #include <tr_json_util.h>
44
45 static char *tr_aaa_server_to_str(TALLOC_CTX *mem_ctx, TR_AAA_SERVER *aaa)
46 {
47   return talloc_strndup(mem_ctx, aaa->hostname->buf, aaa->hostname->len);
48 }
49
50
51 char *tr_idp_realm_to_str(TALLOC_CTX *mem_ctx, TR_IDP_REALM *idp)
52 {
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;
60   TR_APC *apc=NULL;
61   char *result=NULL;
62
63   /* get the AAA servers */
64   if (n_aaa_servers<=0)
65     aaa_servers=talloc_strdup(tmp_ctx, "");
66   else {
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]);
71     }
72
73     /* add space for comma-space separators */
74     aaa_servers_strlen+=2*(n_aaa_servers-1);
75
76     aaa_servers=talloc_array(tmp_ctx, char, aaa_servers_strlen+1);
77     aaa_servers[0]='\0';
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, ", ");
82     }
83     talloc_free(s_aaa);
84   }
85
86   /* get the APCs */
87   if (n_apcs<=0)
88     apcs=talloc_strdup(tmp_ctx, "");
89   else {
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]);
94     }
95
96     /* add space for comma-space separators */
97     apcs_strlen+=2*(n_apcs-1);
98
99     apcs=talloc_array(tmp_ctx, char, apcs_strlen+1);
100     apcs[0]='\0';
101     for (ii=0; ii<n_apcs; ii++) {
102       strcat(apcs, s_apc[ii]);
103       if (ii<(n_apcs-1))
104         strcat(apcs, ", ");
105     }
106     talloc_free(s_apc);
107   }
108
109   result=talloc_asprintf(mem_ctx,
110                          "IDP realm: \"%.*s\""
111                          "  shared: %s"
112                          "  local: %s"
113                          "  AAA servers: %s"
114                          "  APCs: %s",
115                          idp->realm_id->len, idp->realm_id->buf,
116                          (idp->shared_config)?"yes":"no",
117                          (idp->origin==TR_REALM_LOCAL)?"yes":"no",
118                          aaa_servers,
119                          apcs);
120   talloc_free(tmp_ctx);
121   return result;
122 }
123
124
125 static json_t *tr_apcs_to_json(TR_APC *apcs)
126 {
127   json_t *jarray = json_array();
128   json_t *retval = NULL;
129   TR_APC_ITER *iter = tr_apc_iter_new(NULL);
130   TR_APC *apc = NULL;
131
132   if ((jarray == NULL) || (iter == NULL))
133     goto cleanup;
134
135   for (apc = tr_apc_iter_first(iter, apcs);
136        apc != NULL;
137        apc = tr_apc_iter_next(iter)) {
138     ARRAY_APPEND_OR_FAIL(jarray, tr_name_to_json_string(tr_apc_get_id(apc)));
139   }
140
141   /* success */
142   retval = jarray;
143   json_incref(retval);
144
145 cleanup:
146   if (jarray)
147     json_decref(jarray);
148
149   return retval;
150 }
151
152 static json_t *tr_aaa_server_to_json(TR_AAA_SERVER *aaa)
153 {
154   char *hostname = tr_name_strdup(aaa->hostname);
155   char *s = NULL;
156   json_t *jstr = NULL;
157
158   if (hostname == NULL)
159     return NULL;
160
161   s = talloc_asprintf(NULL, "%s:%d", hostname, TID_PORT);
162   if (s) {
163     jstr = json_string(s);
164     talloc_free(s);
165   }
166   return jstr;
167 }
168
169 static json_t *tr_aaa_servers_to_json(TR_AAA_SERVER *aaas)
170 {
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;
175
176   if ((jarray == NULL) || (iter == NULL))
177     goto cleanup;
178
179   for (aaa = tr_aaa_server_iter_first(iter, aaas);
180        aaa != NULL;
181        aaa = tr_aaa_server_iter_next(iter)) {
182     ARRAY_APPEND_OR_FAIL(jarray, tr_aaa_server_to_json(aaa));
183   }
184
185   /* success */
186   retval = jarray;
187   json_incref(retval);
188
189 cleanup:
190   if (jarray)
191     json_decref(jarray);
192
193   return retval;
194 }
195
196 static json_t *tr_idp_realm_to_json(TR_IDP_REALM *idp)
197 {
198   json_t *idp_json = json_object();
199   json_t *retval = NULL;
200
201   if (idp_json == NULL)
202     goto cleanup;
203
204
205   /* success */
206   retval = idp_json;
207   json_incref(retval);
208
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));
219 cleanup:
220   if (idp_json)
221     json_decref(idp_json);
222
223   return retval;
224 }
225
226 json_t *tr_idp_realms_to_json(TR_IDP_REALM *idps)
227 {
228   {
229     json_t *jarray = json_array();
230     json_t *retval = NULL;
231     TR_IDP_REALM *this = NULL;
232
233     if (jarray == NULL)
234       goto cleanup;
235
236     for (this=idps; this != NULL; this=this->next)
237       ARRAY_APPEND_OR_FAIL(jarray, tr_idp_realm_to_json(this));
238
239     /* success */
240     retval = jarray;
241     json_incref(retval);
242
243   cleanup:
244     if (jarray)
245       json_decref(jarray);
246
247     return retval;
248   }
249
250 }