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.
41 #include <tr_cfgwatch.h>
43 #include <tr_config.h>
44 #include <tr_gss_names.h>
46 #include <tr_filter.h>
47 #include <trust_router/tr_constraint.h>
50 #include <trust_router/trp.h>
52 #if JANSSON_VERSION_HEX < 0x020500
53 #include "jansson_iterators.h"
56 TR_AAA_SERVER *tr_cfg_parse_one_aaa_server(TALLOC_CTX *mem_ctx, json_t *jaddr, TR_CFG_RC *rc)
58 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
59 TR_AAA_SERVER *aaa = NULL;
61 if ((!jaddr) || (!json_is_string(jaddr))) {
62 tr_debug("tr_cfg_parse_one_aaa_server: Bad parameters.");
63 *rc = TR_CFG_BAD_PARAMS;
67 aaa = tr_aaa_server_from_string(mem_ctx, json_string_value(jaddr));
69 tr_debug("tr_cfg_parse_one_aaa_server: Out of memory allocating AAA server.");
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");
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");
89 talloc_steal(mem_ctx, aaa);
92 if (*rc != TR_CFG_SUCCESS)
98 static TR_AAA_SERVER *tr_cfg_parse_aaa_servers(TALLOC_CTX *mem_ctx, json_t *jaaas, TR_CFG_RC *rc)
100 TALLOC_CTX *tmp_ctx=NULL;
101 TR_AAA_SERVER *aaa = NULL;
102 TR_AAA_SERVER *temp_aaa = NULL;
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);
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;
116 tr_debug("tr_cfg_parse_aaa_servers: Finished (rc=%d)", *rc);
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);
124 static TR_APC *tr_cfg_parse_one_apc(TALLOC_CTX *mem_ctx, json_t *japc, TR_CFG_RC *rc)
129 *rc = TR_CFG_SUCCESS; /* presume success */
131 if ((!japc) || (!rc) || (!json_is_string(japc))) {
132 tr_debug("tr_cfg_parse_one_apc: Bad parameters.");
134 *rc = TR_CFG_BAD_PARAMS;
138 apc=tr_apc_new(mem_ctx);
140 tr_debug("tr_cfg_parse_one_apc: Out of memory.");
145 name=tr_new_name(json_string_value(japc));
147 tr_debug("tr_cfg_parse_one_apc: No memory for APC name.");
152 tr_apc_set_id(apc, name); /* apc is now responsible for freeing the name */
157 TR_APC *tr_cfg_parse_apcs(TALLOC_CTX *mem_ctx, json_t *japcs, TR_CFG_RC *rc)
159 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
161 TR_APC *new_apc=NULL;
163 TR_CFG_RC call_rc=TR_CFG_ERROR;
165 *rc = TR_CFG_SUCCESS; /* presume success */
167 if ((!japcs) || (!rc) || (!json_is_array(japcs))) {
168 tr_debug("tr_cfg_parse_one_apc: Bad parameters.");
170 *rc = TR_CFG_BAD_PARAMS;
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);
181 tr_apc_add(apcs, new_apc);
184 talloc_steal(mem_ctx, apcs);
188 talloc_free(tmp_ctx);
192 static TR_NAME *tr_cfg_parse_name(TALLOC_CTX *mem_ctx, json_t *jname, TR_CFG_RC *rc)
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;
202 name=tr_new_name(json_string_value(jname));
204 tr_err("tr_cfg_parse_name: could not allocate name");
213 static int tr_cfg_parse_shared_config(json_t *jsc, TR_CFG_RC *rc)
215 const char *shared=NULL;
219 (!json_is_string(jsc)) ||
220 (NULL==(shared=json_string_value(jsc)))) {
221 *rc=TR_CFG_BAD_PARAMS;
225 if (0==strcmp(shared, "no"))
227 else if (0==strcmp(shared, "yes"))
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\")",
237 static TR_REALM_ORIGIN tr_cfg_realm_origin(json_t *jrealm)
239 json_t *jremote=json_object_get(jrealm, "remote");
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;
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.");
254 return TR_REALM_LOCAL;
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)
260 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
262 TR_AAA_SERVER *aaa=NULL;
263 TR_CFG_RC rc=TR_CFG_ERROR;
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");
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");
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");
289 tr_debug("tr_cfg_parse_idp: APC=\"%.*s\"",
293 /* done, fill in the idp structures */
295 talloc_steal(idp, apcs);
296 idp->aaa_servers=aaa;
300 if (rc!=TR_CFG_SUCCESS) {
304 tr_aaa_server_free(aaa);
307 talloc_free(tmp_ctx);
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)
314 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
315 TR_IDP_REALM *realm=NULL;
316 TR_CFG_RC call_rc=TR_CFG_ERROR;
318 *rc=TR_CFG_ERROR; /* default to error if not set */
320 if ((!jrealm) || (!rc)) {
321 tr_err("tr_cfg_parse_one_idp_realm: Bad parameters.");
323 *rc=TR_CFG_BAD_PARAMS;
327 if (NULL==(realm=tr_idp_realm_new(tmp_ctx))) {
328 tr_err("tr_cfg_parse_one_idp_realm: could not allocate idp realm.");
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.");
340 /* must have a name */
341 realm->realm_id=tr_cfg_parse_name(realm,
342 json_object_get(jrealm, "realm"),
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");
349 tr_debug("tr_cfg_parse_one_idp_realm: realm_id=\"%.*s\"",
350 realm->realm_id->len,
351 realm->realm_id->buf);
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.");
363 if (*rc==TR_CFG_SUCCESS)
364 talloc_steal(mem_ctx, realm);
370 talloc_free(tmp_ctx);
374 /* Determine whether the realm is an IDP realm */
375 static int tr_cfg_is_idp_realm(json_t *jrealm)
377 /* If a realm spec contains an identity_provider, it's an IDP realm. */
378 if (NULL != json_object_get(jrealm, "identity_provider"))
384 static TR_IDP_REALM *tr_cfg_parse_one_remote_realm(TALLOC_CTX *mem_ctx, json_t *jrealm, TR_CFG_RC *rc)
386 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
387 TR_IDP_REALM *realm=talloc(mem_ctx, TR_IDP_REALM);
389 *rc=TR_CFG_ERROR; /* default to error if not set */
391 if ((!jrealm) || (!rc)) {
392 tr_err("tr_cfg_parse_one_remote_realm: Bad parameters.");
394 *rc=TR_CFG_BAD_PARAMS;
398 if (NULL==(realm=tr_idp_realm_new(tmp_ctx))) {
399 tr_err("tr_cfg_parse_one_remote_realm: could not allocate idp realm.");
404 /* must have a name */
405 realm->realm_id=tr_cfg_parse_name(realm,
406 json_object_get(jrealm, "realm"),
408 if ((*rc!=TR_CFG_SUCCESS) || (realm->realm_id==NULL)) {
409 tr_err("tr_cfg_parse_one_remote_realm: could not parse realm name");
413 tr_debug("tr_cfg_parse_one_remote_realm: realm_id=\"%.*s\"",
414 realm->realm_id->len,
415 realm->realm_id->buf);
417 realm->origin=tr_cfg_realm_origin(jrealm);
421 if (*rc==TR_CFG_SUCCESS)
422 talloc_steal(mem_ctx, realm);
428 talloc_free(tmp_ctx);
432 static int tr_cfg_is_remote_realm(json_t *jrealm)
434 return (tr_cfg_realm_origin(jrealm)!=TR_REALM_LOCAL);
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)
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;
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;
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);
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);
470 tr_idp_realm_add(realms, new_realm);
475 talloc_steal(mem_ctx, realms);
478 talloc_free(tmp_ctx);