Parse hostname/port for AAA server addresses
[trust_router.git] / common / tr_config_realms.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 <stdlib.h>
36 #include <string.h>
37 #include <jansson.h>
38 #include <dirent.h>
39 #include <talloc.h>
40
41 #include <tr_cfgwatch.h>
42 #include <tr_comm.h>
43 #include <tr_config.h>
44 #include <tr_gss_names.h>
45 #include <tr_debug.h>
46 #include <tr_filter.h>
47 #include <trust_router/tr_constraint.h>
48 #include <tr_idp.h>
49 #include <tr.h>
50 #include <trust_router/trp.h>
51
52 #if JANSSON_VERSION_HEX < 0x020500
53 #include "jansson_iterators.h"
54 #endif
55
56 TR_AAA_SERVER *tr_cfg_parse_one_aaa_server(TALLOC_CTX *mem_ctx, json_t *jaddr, TR_CFG_RC *rc)
57 {
58   TALLOC_CTX *tmp_ctx = talloc_new(NULL);
59   TR_AAA_SERVER *aaa = NULL;
60
61   if ((!jaddr) || (!json_is_string(jaddr))) {
62     tr_debug("tr_cfg_parse_one_aaa_server: Bad parameters.");
63     *rc = TR_CFG_BAD_PARAMS;
64     goto cleanup;
65   }
66
67   aaa = tr_aaa_server_from_string(mem_ctx, json_string_value(jaddr));
68   if (aaa == NULL) {
69     tr_debug("tr_cfg_parse_one_aaa_server: Out of memory allocating AAA server.");
70     *rc = TR_CFG_NOMEM;
71     goto cleanup
72   }
73
74   if (tr_aaa_server_get_hostname(aaa)->len == 0) {
75     tr_debug("tr_cfg_parse_one_aaa_server: Empty hostname for AAA server not allowed");
76     *rc = TR_CFG_NOPARSE;
77     goto cleanup;
78   }
79
80   if ((tr_aaa_server_get_port(aaa) <= 0)
81       || (tr_aaa_server_get_port(aaa) > 65535)) {
82     tr_debug("tr_cfg_parse_one_aaa_server: Invalid AAA server port");
83     *rc = TR_CFG_NOPARSE;
84     goto cleanup;
85   }
86
87   /* success ! */
88   *rc = TR_CFG_SUCCESS;
89   talloc_steal(mem_ctx, aaa);
90
91 cleanup:
92   if (*rc != TR_CFG_SUCCESS)
93     aaa = NULL;
94   talloc_free(tmp_ctx);
95   return aaa;
96 }
97
98 static TR_AAA_SERVER *tr_cfg_parse_aaa_servers(TALLOC_CTX *mem_ctx, json_t *jaaas, TR_CFG_RC *rc)
99 {
100   TALLOC_CTX *tmp_ctx=NULL;
101   TR_AAA_SERVER *aaa = NULL;
102   TR_AAA_SERVER *temp_aaa = NULL;
103   int i = 0;
104
105   for (i = 0; i < json_array_size(jaaas); i++) {
106     /* rc gets set in here */
107     if (NULL == (temp_aaa = tr_cfg_parse_one_aaa_server(tmp_ctx, json_array_get(jaaas, i), rc))) {
108       talloc_free(tmp_ctx);
109       return NULL;
110     }
111     /* TBD -- IPv6 addresses */
112     //    tr_debug("tr_cfg_parse_aaa_servers: Configuring AAA Server: ip_addr = %s.", inet_ntoa(temp_aaa->aaa_server_addr));
113     temp_aaa->next = aaa;
114     aaa = temp_aaa;
115   }
116   tr_debug("tr_cfg_parse_aaa_servers: Finished (rc=%d)", *rc);
117
118   for (temp_aaa=aaa; temp_aaa!=NULL; temp_aaa=temp_aaa->next)
119     talloc_steal(mem_ctx, temp_aaa);
120   talloc_free(tmp_ctx);
121   return aaa;
122 }
123
124 static TR_APC *tr_cfg_parse_one_apc(TALLOC_CTX *mem_ctx, json_t *japc, TR_CFG_RC *rc)
125 {
126   TR_APC *apc=NULL;
127   TR_NAME *name=NULL;
128
129   *rc = TR_CFG_SUCCESS;         /* presume success */
130
131   if ((!japc) || (!rc) || (!json_is_string(japc))) {
132     tr_debug("tr_cfg_parse_one_apc: Bad parameters.");
133     if (rc)
134       *rc = TR_CFG_BAD_PARAMS;
135     return NULL;
136   }
137
138   apc=tr_apc_new(mem_ctx);
139   if (apc==NULL) {
140     tr_debug("tr_cfg_parse_one_apc: Out of memory.");
141     *rc = TR_CFG_NOMEM;
142     return NULL;
143   }
144
145   name=tr_new_name(json_string_value(japc));
146   if (name==NULL) {
147     tr_debug("tr_cfg_parse_one_apc: No memory for APC name.");
148     tr_apc_free(apc);
149     *rc = TR_CFG_NOMEM;
150     return NULL;
151   }
152   tr_apc_set_id(apc, name); /* apc is now responsible for freeing the name */
153
154   return apc;
155 }
156
157 TR_APC *tr_cfg_parse_apcs(TALLOC_CTX *mem_ctx, json_t *japcs, TR_CFG_RC *rc)
158 {
159   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
160   TR_APC *apcs=NULL;
161   TR_APC *new_apc=NULL;
162   int ii=0;
163   TR_CFG_RC call_rc=TR_CFG_ERROR;
164
165   *rc = TR_CFG_SUCCESS;         /* presume success */
166
167   if ((!japcs) || (!rc) || (!json_is_array(japcs))) {
168     tr_debug("tr_cfg_parse_one_apc: Bad parameters.");
169     if (rc)
170       *rc = TR_CFG_BAD_PARAMS;
171     return NULL;
172   }
173
174   for (ii=0; ii<json_array_size(japcs); ii++) {
175     new_apc=tr_cfg_parse_one_apc(tmp_ctx, json_array_get(japcs, ii), &call_rc);
176     if ((call_rc!=TR_CFG_SUCCESS) || (new_apc==NULL)) {
177       tr_debug("tr_cfg_parse_apcs: Error parsing APC %d.", ii+1);
178       *rc=TR_CFG_NOPARSE;
179       goto cleanup;
180     }
181     tr_apc_add(apcs, new_apc);
182   }
183
184   talloc_steal(mem_ctx, apcs);
185   *rc=TR_CFG_SUCCESS;
186
187 cleanup:
188   talloc_free(tmp_ctx);
189   return apcs;
190 }
191
192 static TR_NAME *tr_cfg_parse_name(TALLOC_CTX *mem_ctx, json_t *jname, TR_CFG_RC *rc)
193 {
194   TR_NAME *name=NULL;
195   *rc=TR_CFG_ERROR;
196
197   if ((jname==NULL) || (!json_is_string(jname))) {
198     tr_err("tr_cfg_parse_name: name missing or not a string");
199     *rc=TR_CFG_BAD_PARAMS;
200     name=NULL;
201   } else {
202     name=tr_new_name(json_string_value(jname));
203     if (name==NULL) {
204       tr_err("tr_cfg_parse_name: could not allocate name");
205       *rc=TR_CFG_NOMEM;
206     } else {
207       *rc=TR_CFG_SUCCESS;
208     }
209   }
210   return name;
211 }
212
213 static int tr_cfg_parse_shared_config(json_t *jsc, TR_CFG_RC *rc)
214 {
215   const char *shared=NULL;
216   *rc=TR_CFG_SUCCESS;
217
218   if ((jsc==NULL) ||
219       (!json_is_string(jsc)) ||
220       (NULL==(shared=json_string_value(jsc)))) {
221     *rc=TR_CFG_BAD_PARAMS;
222     return -1;
223   }
224
225   if (0==strcmp(shared, "no"))
226     return 0;
227   else if (0==strcmp(shared, "yes"))
228     return 1;
229
230   /* any other value is an error */
231   tr_debug("tr_cfg_parse_shared_config: invalid shared_config value \"%s\" (should be \"yes\" or \"no\")",
232            shared);
233   *rc=TR_CFG_NOPARSE;
234   return -1;
235 }
236
237 static TR_REALM_ORIGIN tr_cfg_realm_origin(json_t *jrealm)
238 {
239   json_t *jremote=json_object_get(jrealm, "remote");
240   const char *s=NULL;
241
242   if (jremote==NULL)
243     return TR_REALM_LOCAL;
244   if (!json_is_string(jremote)) {
245     tr_warning("tr_cfg_realm_origin: \"remote\" is not a string, assuming this is a local realm.");
246     return TR_REALM_LOCAL;
247   }
248   s=json_string_value(jremote);
249   if (strcasecmp(s, "yes")==0)
250     return TR_REALM_REMOTE_INCOMPLETE;
251   else if (strcasecmp(s, "no")!=0)
252     tr_warning("tr_cfg_realm_origin: \"remote\" is neither 'yes' nor 'no', assuming this is a local realm.");
253
254   return TR_REALM_LOCAL;
255 }
256
257 /* Parse the identity provider object from a realm and fill in the given TR_IDP_REALM. */
258 static TR_CFG_RC tr_cfg_parse_idp(TR_IDP_REALM *idp, json_t *jidp)
259 {
260   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
261   TR_APC *apcs=NULL;
262   TR_AAA_SERVER *aaa=NULL;
263   TR_CFG_RC rc=TR_CFG_ERROR;
264
265   if (jidp==NULL)
266     goto cleanup;
267
268   idp->shared_config=tr_cfg_parse_shared_config(json_object_get(jidp, "shared_config"), &rc);
269   if (rc!=TR_CFG_SUCCESS) {
270     tr_err("tr_cfg_parse_idp: missing or malformed shared_config specification");
271     rc=TR_CFG_NOPARSE;
272     goto cleanup;
273   }
274
275   apcs=tr_cfg_parse_apcs(tmp_ctx, json_object_get(jidp, "apcs"), &rc);
276   if ((rc!=TR_CFG_SUCCESS) || (apcs==NULL)) {
277     tr_err("tr_cfg_parse_idp: unable to parse APC");
278     rc=TR_CFG_NOPARSE;
279     goto cleanup;
280   }
281
282   aaa=tr_cfg_parse_aaa_servers(idp, json_object_get(jidp, "aaa_servers"), &rc);
283   if (rc!=TR_CFG_SUCCESS) {
284     tr_err("tr_cfg_parse_idp: unable to parse AAA servers");
285     rc=TR_CFG_NOPARSE;
286     goto cleanup;
287   }
288
289   tr_debug("tr_cfg_parse_idp: APC=\"%.*s\"",
290            apcs->id->len,
291            apcs->id->buf);
292
293   /* done, fill in the idp structures */
294   idp->apcs=apcs;
295   talloc_steal(idp, apcs);
296   idp->aaa_servers=aaa;
297   rc=TR_CFG_SUCCESS;
298
299 cleanup:
300   if (rc!=TR_CFG_SUCCESS) {
301     if (apcs!=NULL)
302       tr_apc_free(apcs);
303     if (aaa!=NULL)
304       tr_aaa_server_free(aaa);
305   }
306
307   talloc_free(tmp_ctx);
308   return rc;
309 }
310
311 /* parses idp realm */
312 static TR_IDP_REALM *tr_cfg_parse_one_idp_realm(TALLOC_CTX *mem_ctx, json_t *jrealm, TR_CFG_RC *rc)
313 {
314   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
315   TR_IDP_REALM *realm=NULL;
316   TR_CFG_RC call_rc=TR_CFG_ERROR;
317
318   *rc=TR_CFG_ERROR; /* default to error if not set */
319
320   if ((!jrealm) || (!rc)) {
321     tr_err("tr_cfg_parse_one_idp_realm: Bad parameters.");
322     if (rc)
323       *rc=TR_CFG_BAD_PARAMS;
324     goto cleanup;
325   }
326
327   if (NULL==(realm=tr_idp_realm_new(tmp_ctx))) {
328     tr_err("tr_cfg_parse_one_idp_realm: could not allocate idp realm.");
329     *rc=TR_CFG_NOMEM;
330     goto cleanup;
331   }
332
333   realm->origin=tr_cfg_realm_origin(jrealm);
334   if (realm->origin!=TR_REALM_LOCAL) {
335     tr_debug("tr_cfg_parse_one_idp_realm: realm is remote, should not have full IdP info.");
336     *rc=TR_CFG_NOPARSE;
337     goto cleanup;
338   }
339
340   /* must have a name */
341   realm->realm_id=tr_cfg_parse_name(realm,
342                                     json_object_get(jrealm, "realm"),
343                                     &call_rc);
344   if ((call_rc!=TR_CFG_SUCCESS) || (realm->realm_id==NULL)) {
345     tr_err("tr_cfg_parse_one_idp_realm: could not parse realm name");
346     *rc=TR_CFG_NOPARSE;
347     goto cleanup;
348   }
349   tr_debug("tr_cfg_parse_one_idp_realm: realm_id=\"%.*s\"",
350            realm->realm_id->len,
351            realm->realm_id->buf);
352
353   call_rc=tr_cfg_parse_idp(realm, json_object_get(jrealm, "identity_provider"));
354   if (call_rc!=TR_CFG_SUCCESS) {
355     tr_err("tr_cfg_parse_one_idp_realm: could not parse identity_provider.");
356     *rc=TR_CFG_NOPARSE;
357     goto cleanup;
358   }
359
360   *rc=TR_CFG_SUCCESS;
361
362 cleanup:
363   if (*rc==TR_CFG_SUCCESS)
364     talloc_steal(mem_ctx, realm);
365   else {
366     talloc_free(realm);
367     realm=NULL;
368   }
369
370   talloc_free(tmp_ctx);
371   return realm;
372 }
373
374 /* Determine whether the realm is an IDP realm */
375 static int tr_cfg_is_idp_realm(json_t *jrealm)
376 {
377   /* If a realm spec contains an identity_provider, it's an IDP realm. */
378   if (NULL != json_object_get(jrealm, "identity_provider"))
379     return 1;
380   else
381     return 0;
382 }
383
384 static TR_IDP_REALM *tr_cfg_parse_one_remote_realm(TALLOC_CTX *mem_ctx, json_t *jrealm, TR_CFG_RC *rc)
385 {
386   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
387   TR_IDP_REALM *realm=talloc(mem_ctx, TR_IDP_REALM);
388
389   *rc=TR_CFG_ERROR; /* default to error if not set */
390
391   if ((!jrealm) || (!rc)) {
392     tr_err("tr_cfg_parse_one_remote_realm: Bad parameters.");
393     if (rc)
394       *rc=TR_CFG_BAD_PARAMS;
395     goto cleanup;
396   }
397
398   if (NULL==(realm=tr_idp_realm_new(tmp_ctx))) {
399     tr_err("tr_cfg_parse_one_remote_realm: could not allocate idp realm.");
400     *rc=TR_CFG_NOMEM;
401     goto cleanup;
402   }
403
404   /* must have a name */
405   realm->realm_id=tr_cfg_parse_name(realm,
406                                     json_object_get(jrealm, "realm"),
407                                     rc);
408   if ((*rc!=TR_CFG_SUCCESS) || (realm->realm_id==NULL)) {
409     tr_err("tr_cfg_parse_one_remote_realm: could not parse realm name");
410     *rc=TR_CFG_NOPARSE;
411     goto cleanup;
412   }
413   tr_debug("tr_cfg_parse_one_remote_realm: realm_id=\"%.*s\"",
414            realm->realm_id->len,
415            realm->realm_id->buf);
416
417   realm->origin=tr_cfg_realm_origin(jrealm);
418   *rc=TR_CFG_SUCCESS;
419
420 cleanup:
421   if (*rc==TR_CFG_SUCCESS)
422     talloc_steal(mem_ctx, realm);
423   else {
424     talloc_free(realm);
425     realm=NULL;
426   }
427
428   talloc_free(tmp_ctx);
429   return realm;
430 }
431
432 static int tr_cfg_is_remote_realm(json_t *jrealm)
433 {
434   return (tr_cfg_realm_origin(jrealm)!=TR_REALM_LOCAL);
435 }
436
437 /* Parse any idp realms in the j_realms object. Ignores other realm types. */
438 TR_IDP_REALM *tr_cfg_parse_idp_realms(TALLOC_CTX *mem_ctx, json_t *jrealms, TR_CFG_RC *rc)
439 {
440   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
441   TR_IDP_REALM *realms=NULL;
442   TR_IDP_REALM *new_realm=NULL;
443   json_t *this_jrealm=NULL;
444   int ii=0;
445
446   *rc=TR_CFG_ERROR;
447   if ((jrealms==NULL) || (!json_is_array(jrealms))) {
448     tr_err("tr_cfg_parse_idp_realms: realms not an array");
449     *rc=TR_CFG_BAD_PARAMS;
450     goto cleanup;
451   }
452
453   for (ii=0; ii<json_array_size(jrealms); ii++) {
454     this_jrealm=json_array_get(jrealms, ii);
455     if (tr_cfg_is_idp_realm(this_jrealm)) {
456       new_realm=tr_cfg_parse_one_idp_realm(tmp_ctx, this_jrealm, rc);
457       if ((*rc)!=TR_CFG_SUCCESS) {
458         tr_err("tr_cfg_parse_idp_realms: error decoding realm entry %d", ii+1);
459         *rc=TR_CFG_NOPARSE;
460         goto cleanup;
461       }
462       tr_idp_realm_add(realms, new_realm);
463     } else if (tr_cfg_is_remote_realm(this_jrealm)) {
464       new_realm=tr_cfg_parse_one_remote_realm(tmp_ctx, this_jrealm, rc);
465       if ((*rc)!=TR_CFG_SUCCESS) {
466         tr_err("tr_cfg_parse_idp_realms: error decoding remote realm entry %d", ii+1);
467         *rc=TR_CFG_NOPARSE;
468         goto cleanup;
469       }
470       tr_idp_realm_add(realms, new_realm);
471     }
472   }
473
474   *rc=TR_CFG_SUCCESS;
475   talloc_steal(mem_ctx, realms);
476
477 cleanup:
478   talloc_free(tmp_ctx);
479   return realms;
480 }