Merge pull request #62 from painless-security/jennifer/report_incoming_ipaddr
[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
44 static char *tr_aaa_server_to_str(TALLOC_CTX *mem_ctx, TR_AAA_SERVER *aaa)
45 {
46   return talloc_strndup(mem_ctx, aaa->hostname->buf, aaa->hostname->len);
47 }
48
49
50 char *tr_idp_realm_to_str(TALLOC_CTX *mem_ctx, TR_IDP_REALM *idp)
51 {
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;
59   TR_APC *apc=NULL;
60   char *result=NULL;
61
62   /* get the AAA servers */
63   if (n_aaa_servers<=0)
64     aaa_servers=talloc_strdup(tmp_ctx, "");
65   else {
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]);
70     }
71
72     /* add space for comma-space separators */
73     aaa_servers_strlen+=2*(n_aaa_servers-1);
74
75     aaa_servers=talloc_array(tmp_ctx, char, aaa_servers_strlen+1);
76     aaa_servers[0]='\0';
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, ", ");
81     }
82     talloc_free(s_aaa);
83   }
84
85   /* get the APCs */
86   if (n_apcs<=0)
87     apcs=talloc_strdup(tmp_ctx, "");
88   else {
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]);
93     }
94
95     /* add space for comma-space separators */
96     apcs_strlen+=2*(n_apcs-1);
97
98     apcs=talloc_array(tmp_ctx, char, apcs_strlen+1);
99     apcs[0]='\0';
100     for (ii=0; ii<n_apcs; ii++) {
101       strcat(apcs, s_apc[ii]);
102       if (ii<(n_apcs-1))
103         strcat(apcs, ", ");
104     }
105     talloc_free(s_apc);
106   }
107
108   result=talloc_asprintf(mem_ctx,
109                          "IDP realm: \"%.*s\""
110                          "  shared: %s"
111                          "  local: %s"
112                          "  AAA servers: %s"
113                          "  APCs: %s",
114                          idp->realm_id->len, idp->realm_id->buf,
115                          (idp->shared_config)?"yes":"no",
116                          (idp->origin==TR_REALM_LOCAL)?"yes":"no",
117                          aaa_servers,
118                          apcs);
119   talloc_free(tmp_ctx);
120   return result;
121 }
122
123
124 /* helper for below */
125 #define OBJECT_SET_OR_FAIL(jobj, key, val)     \
126 do {                                           \
127   if (val)                                     \
128     json_object_set_new((jobj),(key),(val));   \
129   else                                         \
130     goto cleanup;                              \
131 } while (0)
132
133 #define ARRAY_APPEND_OR_FAIL(jary, val)        \
134 do {                                           \
135   if (val)                                     \
136     json_array_append_new((jary),(val));       \
137   else                                         \
138     goto cleanup;                              \
139 } while (0)
140
141 static json_t *tr_apcs_to_json(TR_APC *apcs)
142 {
143   json_t *jarray = json_array();
144   json_t *retval = NULL;
145   TR_APC_ITER *iter = tr_apc_iter_new(NULL);
146   TR_APC *apc = NULL;
147
148   if ((jarray == NULL) || (iter == NULL))
149     goto cleanup;
150
151   for (apc = tr_apc_iter_first(iter, apcs);
152        apc != NULL;
153        apc = tr_apc_iter_next(iter)) {
154     ARRAY_APPEND_OR_FAIL(jarray, tr_name_to_json_string(tr_apc_get_id(apc)));
155   }
156
157   /* success */
158   retval = jarray;
159   json_incref(retval);
160
161 cleanup:
162   if (jarray)
163     json_decref(jarray);
164
165   return retval;
166 }
167
168 static json_t *tr_aaa_server_to_json(TR_AAA_SERVER *aaa)
169 {
170   char *hostname = tr_name_strdup(aaa->hostname);
171   char *s = NULL;
172   json_t *jstr = NULL;
173
174   if (hostname == NULL)
175     return NULL;
176
177   s = talloc_asprintf(NULL, "%s:%d", hostname, TID_PORT);
178   if (s) {
179     jstr = json_string(s);
180     talloc_free(s);
181   }
182   return jstr;
183 }
184
185 static json_t *tr_aaa_servers_to_json(TR_AAA_SERVER *aaas)
186 {
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;
191
192   if ((jarray == NULL) || (iter == NULL))
193     goto cleanup;
194
195   for (aaa = tr_aaa_server_iter_first(iter, aaas);
196        aaa != NULL;
197        aaa = tr_aaa_server_iter_next(iter)) {
198     ARRAY_APPEND_OR_FAIL(jarray, tr_aaa_server_to_json(aaa));
199   }
200
201   /* success */
202   retval = jarray;
203   json_incref(retval);
204
205 cleanup:
206   if (jarray)
207     json_decref(jarray);
208
209   return retval;
210 }
211
212 static json_t *tr_idp_realm_to_json(TR_IDP_REALM *idp)
213 {
214   json_t *idp_json = json_object();
215   json_t *retval = NULL;
216
217   if (idp_json == NULL)
218     goto cleanup;
219
220
221   /* success */
222   retval = idp_json;
223   json_incref(retval);
224
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));
235 cleanup:
236   if (idp_json)
237     json_decref(idp_json);
238
239   return retval;
240 }
241
242 json_t *tr_idp_realms_to_json(TR_IDP_REALM *idps)
243 {
244   {
245     json_t *jarray = json_array();
246     json_t *retval = NULL;
247     TR_IDP_REALM *this = NULL;
248
249     if (jarray == NULL)
250       goto cleanup;
251
252     for (this=idps; this != NULL; this=this->next)
253       ARRAY_APPEND_OR_FAIL(jarray, tr_idp_realm_to_json(this));
254
255     /* success */
256     retval = jarray;
257     json_incref(retval);
258
259   cleanup:
260     if (jarray)
261       json_decref(jarray);
262
263     return retval;
264   }
265
266 }