Merge pull request #86 from painless-security/jennifer/aaa_server_port
[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   char *aaa_hostname = tr_name_strdup( tr_aaa_server_get_hostname(aaa) );
48   char *result = NULL;
49
50   if (aaa_hostname == NULL)
51     return NULL;
52
53   result = talloc_asprintf(mem_ctx,
54                            "%s:%d",
55                            aaa_hostname,
56                            tr_aaa_server_get_port(aaa));
57   free(aaa_hostname);
58   return result;
59 }
60
61
62 char *tr_idp_realm_to_str(TALLOC_CTX *mem_ctx, TR_IDP_REALM *idp)
63 {
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;
71   TR_APC *apc=NULL;
72   char *result=NULL;
73
74   /* get the AAA servers */
75   if (n_aaa_servers<=0)
76     aaa_servers=talloc_strdup(tmp_ctx, "");
77   else {
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]);
82     }
83
84     /* add space for comma-space separators */
85     aaa_servers_strlen+=2*(n_aaa_servers-1);
86
87     aaa_servers=talloc_array(tmp_ctx, char, aaa_servers_strlen+1);
88     aaa_servers[0]='\0';
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, ", ");
93     }
94     talloc_free(s_aaa);
95   }
96
97   /* get the APCs */
98   if (n_apcs<=0)
99     apcs=talloc_strdup(tmp_ctx, "");
100   else {
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]);
105     }
106
107     /* add space for comma-space separators */
108     apcs_strlen+=2*(n_apcs-1);
109
110     apcs=talloc_array(tmp_ctx, char, apcs_strlen+1);
111     apcs[0]='\0';
112     for (ii=0; ii<n_apcs; ii++) {
113       strcat(apcs, s_apc[ii]);
114       if (ii<(n_apcs-1))
115         strcat(apcs, ", ");
116     }
117     talloc_free(s_apc);
118   }
119
120   result=talloc_asprintf(mem_ctx,
121                          "IDP realm: \"%.*s\""
122                          "  shared: %s"
123                          "  local: %s"
124                          "  AAA servers: %s"
125                          "  APCs: %s",
126                          idp->realm_id->len, idp->realm_id->buf,
127                          (idp->shared_config)?"yes":"no",
128                          (idp->origin==TR_REALM_LOCAL)?"yes":"no",
129                          aaa_servers,
130                          apcs);
131   talloc_free(tmp_ctx);
132   return result;
133 }
134
135
136 static json_t *tr_apcs_to_json(TR_APC *apcs)
137 {
138   json_t *jarray = json_array();
139   json_t *retval = NULL;
140   TR_APC_ITER *iter = tr_apc_iter_new(NULL);
141   TR_APC *apc = NULL;
142
143   if ((jarray == NULL) || (iter == NULL))
144     goto cleanup;
145
146   for (apc = tr_apc_iter_first(iter, apcs);
147        apc != NULL;
148        apc = tr_apc_iter_next(iter)) {
149     ARRAY_APPEND_OR_FAIL(jarray, tr_name_to_json_string(tr_apc_get_id(apc)));
150   }
151
152   /* success */
153   retval = jarray;
154   json_incref(retval);
155
156 cleanup:
157   if (jarray)
158     json_decref(jarray);
159
160   return retval;
161 }
162
163 static json_t *tr_aaa_server_to_json(TR_AAA_SERVER *aaa)
164 {
165   char *s = tr_aaa_server_to_str(NULL, aaa);
166   json_t *jstr = NULL;
167
168   if (s) {
169     jstr = json_string(s);
170     talloc_free(s);
171   }
172   return jstr;
173 }
174
175 static json_t *tr_aaa_servers_to_json(TR_AAA_SERVER *aaas)
176 {
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;
181
182   if ((jarray == NULL) || (iter == NULL))
183     goto cleanup;
184
185   for (aaa = tr_aaa_server_iter_first(iter, aaas);
186        aaa != NULL;
187        aaa = tr_aaa_server_iter_next(iter)) {
188     ARRAY_APPEND_OR_FAIL(jarray, tr_aaa_server_to_json(aaa));
189   }
190
191   /* success */
192   retval = jarray;
193   json_incref(retval);
194
195 cleanup:
196   if (jarray)
197     json_decref(jarray);
198
199   return retval;
200 }
201
202 static json_t *tr_idp_realm_to_json(TR_IDP_REALM *idp)
203 {
204   json_t *idp_json = json_object();
205   json_t *retval = NULL;
206
207   if (idp_json == NULL)
208     goto cleanup;
209
210
211   /* success */
212   retval = idp_json;
213   json_incref(retval);
214
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));
225 cleanup:
226   if (idp_json)
227     json_decref(idp_json);
228
229   return retval;
230 }
231
232 json_t *tr_idp_realms_to_json(TR_IDP_REALM *idps)
233 {
234   {
235     json_t *jarray = json_array();
236     json_t *retval = NULL;
237     TR_IDP_REALM *this = NULL;
238
239     if (jarray == NULL)
240       goto cleanup;
241
242     for (this=idps; this != NULL; this=this->next)
243       ARRAY_APPEND_OR_FAIL(jarray, tr_idp_realm_to_json(this));
244
245     /* success */
246     retval = jarray;
247     json_incref(retval);
248
249   cleanup:
250     if (jarray)
251       json_decref(jarray);
252
253     return retval;
254   }
255
256 }