2 * Copyright (c) 2012, 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>
46 #include <tr_filter.h>
47 #include <trust_router/tr_constraint.h>
50 #include <trust_router/trp.h>
52 void tr_print_config (TR_CFG *cfg) {
53 tr_notice("tr_print_config: Logging running trust router configuration.");
54 tr_print_comms(cfg->comms);
57 void tr_print_comms (TR_COMM *comm_list) {
60 for (comm = comm_list; NULL != comm; comm = comm->next) {
61 tr_notice("tr_print_config: Community %s:", comm->id->buf);
63 tr_notice("tr_print_config: - Member IdPs:");
64 tr_print_comm_idps(comm->idp_realms);
66 tr_notice("tr_print_config: - Member RPs:");
67 tr_print_comm_rps(comm->rp_realms);
71 void tr_print_comm_idps (TR_IDP_REALM *idp_list) {
72 TR_IDP_REALM *idp = NULL;
74 for (idp = idp_list; NULL != idp; idp = idp->comm_next) {
75 tr_notice("tr_print_config: - @%s", idp->realm_id->buf);
79 void tr_print_comm_rps(TR_RP_REALM *rp_list) {
80 TR_RP_REALM *rp = NULL;
82 for (rp = rp_list; NULL != rp; rp = rp->next) {
83 tr_notice("tr_print_config: - %s", rp->realm_name->buf);
87 TR_CFG *tr_cfg_new(TALLOC_CTX *mem_ctx)
89 return talloc_zero(mem_ctx, TR_CFG);
92 void tr_cfg_free (TR_CFG *cfg) {
96 TR_CFG_MGR *tr_cfg_mgr_new(TALLOC_CTX *mem_ctx)
98 return talloc_zero(mem_ctx, TR_CFG_MGR);
101 void tr_cfg_mgr_free (TR_CFG_MGR *cfg_mgr) {
102 talloc_free(cfg_mgr);
105 TR_CFG_RC tr_apply_new_config (TR_CFG_MGR *cfg_mgr)
107 /* cfg_mgr->active is allowed to be null, but new cannot be */
108 if ((cfg_mgr==NULL) || (cfg_mgr->new==NULL))
109 return TR_CFG_BAD_PARAMS;
111 if (cfg_mgr->active != NULL)
112 tr_cfg_free(cfg_mgr->active);
114 cfg_mgr->active = cfg_mgr->new;
115 cfg_mgr->new=NULL; /* only keep a single handle on the new configuration */
117 tr_log_threshold(cfg_mgr->active->internal->log_threshold);
118 tr_console_threshold(cfg_mgr->active->internal->console_threshold);
120 return TR_CFG_SUCCESS;
123 static TR_CFG_RC tr_cfg_parse_internal(TR_CFG *trc, json_t *jcfg)
127 json_t *jtidsp = NULL;
128 json_t *jtrpsp = NULL;
129 json_t *jhname = NULL;
131 json_t *jconthres = NULL;
132 json_t *jlogthres = NULL;
133 json_t *jcfgpoll = NULL;
134 json_t *jcfgsettle = NULL;
135 json_t *jroutesweep = NULL;
136 json_t *jrouteupdate = NULL;
137 json_t *jrouteconnect = NULL;
139 if ((!trc) || (!jcfg))
140 return TR_CFG_BAD_PARAMS;
142 if (NULL == trc->internal) {
143 if (NULL == (trc->internal = talloc_zero(trc, TR_CFG_INTERNAL)))
147 if (NULL != (jint = json_object_get(jcfg, "tr_internal"))) {
148 if (NULL != (jmtd = json_object_get(jint, "max_tree_depth"))) {
149 if (json_is_number(jmtd)) {
150 trc->internal->max_tree_depth = json_integer_value(jmtd);
152 tr_debug("tr_cfg_parse_internal: Parsing error, max_tree_depth is not a number.");
153 return TR_CFG_NOPARSE;
156 /* If not configured, use the default */
157 trc->internal->max_tree_depth = TR_DEFAULT_MAX_TREE_DEPTH;
159 if (NULL != (jtidsp = json_object_get(jint, "tids_port"))) {
160 if (json_is_number(jtidsp)) {
161 trc->internal->tids_port = json_integer_value(jtidsp);
163 tr_debug("tr_cfg_parse_internal: Parsing error, tids_port is not a number.");
164 return TR_CFG_NOPARSE;
167 /* If not configured, use the default */
168 trc->internal->tids_port = TR_DEFAULT_TIDS_PORT;
170 if (NULL != (jtrpsp = json_object_get(jint, "trps_port"))) {
171 if (json_is_number(jtrpsp)) {
172 trc->internal->trps_port = json_integer_value(jtrpsp);
174 tr_debug("tr_cfg_parse_internal: Parsing error, trps_port is not a number.");
175 return TR_CFG_NOPARSE;
178 /* If not configured, use the default */
179 trc->internal->trps_port = TR_DEFAULT_TRPS_PORT;
181 if (NULL != (jhname = json_object_get(jint, "hostname"))) {
182 if (json_is_string(jhname)) {
183 trc->internal->hostname = json_string_value(jhname);
185 tr_debug("tr_cfg_parse_internal: Parsing error, hostname is not a string.");
186 return TR_CFG_NOPARSE;
189 if (NULL != (jcfgpoll = json_object_get(jint, "cfg_poll_interval"))) {
190 if (json_is_number(jcfgpoll)) {
191 trc->internal->cfg_poll_interval = json_integer_value(jcfgpoll);
193 tr_debug("tr_cfg_parse_internal: Parsing error, cfg_poll_interval is not a number.");
194 return TR_CFG_NOPARSE;
197 trc->internal->cfg_poll_interval = TR_CFGWATCH_DEFAULT_POLL;
200 if (NULL != (jcfgsettle = json_object_get(jint, "cfg_settling_time"))) {
201 if (json_is_number(jcfgsettle)) {
202 trc->internal->cfg_settling_time = json_integer_value(jcfgsettle);
204 tr_debug("tr_cfg_parse_internal: Parsing error, cfg_settling_time is not a number.");
205 return TR_CFG_NOPARSE;
208 trc->internal->cfg_settling_time = TR_CFGWATCH_DEFAULT_SETTLE;
211 if (NULL != (jrouteconnect = json_object_get(jint, "trp_connect_interval"))) {
212 if (json_is_number(jrouteconnect)) {
213 trc->internal->trp_connect_interval = json_integer_value(jrouteconnect);
215 tr_debug("tr_cfg_parse_internal: Parsing error, trp_connect_interval is not a number.");
216 return TR_CFG_NOPARSE;
219 /* if not configured, use the default */
220 trc->internal->trp_connect_interval=TR_DEFAULT_TRP_CONNECT_INTERVAL;
223 if (NULL != (jroutesweep = json_object_get(jint, "trp_sweep_interval"))) {
224 if (json_is_number(jroutesweep)) {
225 trc->internal->trp_sweep_interval = json_integer_value(jroutesweep);
227 tr_debug("tr_cfg_parse_internal: Parsing error, trp_sweep_interval is not a number.");
228 return TR_CFG_NOPARSE;
231 /* if not configured, use the default */
232 trc->internal->trp_sweep_interval=TR_DEFAULT_TRP_SWEEP_INTERVAL;
235 if (NULL != (jrouteupdate = json_object_get(jint, "trp_update_interval"))) {
236 if (json_is_number(jrouteupdate)) {
237 trc->internal->trp_update_interval = json_integer_value(jrouteupdate);
239 tr_debug("tr_cfg_parse_internal: Parsing error, trp_update_interval is not a number.");
240 return TR_CFG_NOPARSE;
243 /* if not configured, use the default */
244 trc->internal->trp_update_interval=TR_DEFAULT_TRP_UPDATE_INTERVAL;
247 if (NULL != (jlog = json_object_get(jint, "logging"))) {
248 if (NULL != (jlogthres = json_object_get(jlog, "log_threshold"))) {
249 if (json_is_string(jlogthres)) {
250 trc->internal->log_threshold = str2sev(json_string_value(jlogthres));
252 tr_debug("tr_cfg_parse_internal: Parsing error, log_threshold is not a string.");
253 return TR_CFG_NOPARSE;
256 /* If not configured, use the default */
257 trc->internal->log_threshold = TR_DEFAULT_LOG_THRESHOLD;
260 if (NULL != (jconthres = json_object_get(jlog, "console_threshold"))) {
261 if (json_is_string(jconthres)) {
262 trc->internal->console_threshold = str2sev(json_string_value(jconthres));
264 tr_debug("tr_cfg_parse_internal: Parsing error, console_threshold is not a string.");
265 return TR_CFG_NOPARSE;
268 /* If not configured, use the default */
269 trc->internal->console_threshold = TR_DEFAULT_CONSOLE_THRESHOLD;
272 /* If not configured, use the default */
273 trc->internal->console_threshold = TR_DEFAULT_CONSOLE_THRESHOLD;
274 trc->internal->log_threshold = TR_DEFAULT_LOG_THRESHOLD;
277 tr_debug("tr_cfg_parse_internal: Internal config parsed.");
278 return TR_CFG_SUCCESS;
280 return TR_CFG_SUCCESS;
283 static TR_CONSTRAINT *tr_cfg_parse_one_constraint(TALLOC_CTX *mem_ctx, char *ctype, json_t *jc, TR_CFG_RC *rc)
285 TR_CONSTRAINT *cons=NULL;
288 if ((!ctype) || (!jc) || (!rc) ||
289 (!json_is_array(jc)) ||
290 (0 >= json_array_size(jc)) ||
291 (TR_MAX_CONST_MATCHES < json_array_size(jc)) ||
292 (!json_is_string(json_array_get(jc, 0)))) {
293 tr_err("tr_cfg_parse_one_constraint: config error.");
298 if (NULL==(cons=tr_constraint_new(mem_ctx))) {
299 tr_debug("tr_cfg_parse_one_constraint: Out of memory (cons).");
304 if (NULL==(cons->type=tr_new_name(ctype))) {
305 tr_err("tr_cfg_parse_one_constraint: Out of memory (type).");
307 tr_constraint_free(cons);
311 for (i=0; i < json_array_size(jc); i++) {
312 cons->matches[i]=tr_new_name(json_string_value(json_array_get(jc, i)));
313 if (cons->matches[i]==NULL) {
314 tr_err("tr_cfg_parse_one_constraint: Out of memory (match %d).", i+1);
316 tr_constraint_free(cons);
324 static TR_FILTER *tr_cfg_parse_one_filter(TALLOC_CTX *mem_ctx, json_t *jfilt, TR_FILTER_TYPE ftype, TR_CFG_RC *rc)
326 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
327 TR_FILTER *filt=NULL;
328 json_t *jfaction=NULL;
329 json_t *jfspecs=NULL;
330 json_t *jffield=NULL;
331 json_t *jfmatches=NULL;
332 json_t *jfmatch=NULL;
340 if ((jfilt==NULL) || (rc==NULL)) {
341 tr_err("tr_cfg_parse_one_filter: null argument");
342 *rc=TR_CFG_BAD_PARAMS;
346 if (NULL==(filt=tr_filter_new(tmp_ctx))) {
347 tr_err("tr_cfg_parse_one_filter: Out of memory.");
351 tr_filter_set_type(filt, ftype);
353 /* make sure we have space to represent the filter */
354 if (json_array_size(jfilt) > TR_MAX_FILTER_LINES) {
355 tr_err("tr_cfg_parse_one_filter: Filter has too many lines, maximum of %d.", TR_MAX_FILTER_LINES);
360 /* For each entry in the filter... */
361 for (i=0; i < json_array_size(jfilt); i++) {
362 if ((NULL==(jfaction=json_object_get(json_array_get(jfilt, i), "action"))) ||
363 (!json_is_string(jfaction))) {
364 tr_debug("tr_cfg_parse_one_filter: Error parsing filter action.");
369 if ((NULL==(jfspecs=json_object_get(json_array_get(jfilt, i), "specs"))) ||
370 (!json_is_array(jfspecs)) ||
371 (0==json_array_size(jfspecs))) {
372 tr_debug("tr_cfg_parse_one_filter: Error parsing filter specs.");
377 if (TR_MAX_FILTER_SPECS < json_array_size(jfspecs)) {
378 tr_debug("tr_cfg_parse_one_filter: Filter has too many specs, maximimum of %d.", TR_MAX_FILTER_SPECS);
383 if (NULL==(filt->lines[i]=tr_fline_new(filt))) {
384 tr_debug("tr_cfg_parse_one_filter: Out of memory allocating filter line %d.", i+1);
389 if (!strcmp(json_string_value(jfaction), "accept")) {
390 filt->lines[i]->action=TR_FILTER_ACTION_ACCEPT;
392 else if (!strcmp(json_string_value(jfaction), "reject")) {
393 filt->lines[i]->action=TR_FILTER_ACTION_REJECT;
396 tr_debug("tr_cfg_parse_one_filter: Error parsing filter action, unknown action' %s'.", json_string_value(jfaction));
401 if (NULL!=(jrc=json_object_get(json_array_get(jfilt, i), "realm_constraints"))) {
402 if (!json_is_array(jrc)) {
403 tr_err("tr_cfg_parse_one_filter: cannot parse realm_constraints, not an array.");
406 } else if (json_array_size(jrc)>TR_MAX_CONST_MATCHES) {
407 tr_err("tr_cfg_parse_one_filter: realm_constraints has too many entries, maximum of %d.",
408 TR_MAX_CONST_MATCHES);
411 } else if (json_array_size(jrc)>0) {
412 /* ok we actually have entries to process */
413 if (NULL==(filt->lines[i]->realm_cons=tr_cfg_parse_one_constraint(filt->lines[i], "realm", jrc, rc))) {
414 tr_debug("tr_cfg_parse_one_filter: Error parsing realm constraint");
421 if (NULL!=(jdc=json_object_get(json_array_get(jfilt, i), "domain_constraints"))) {
422 if (!json_is_array(jdc)) {
423 tr_err("tr_cfg_parse_one_filter: cannot parse domain_constraints, not an array.");
426 } else if (json_array_size(jdc)>TR_MAX_CONST_MATCHES) {
427 tr_err("tr_cfg_parse_one_filter: domain_constraints has too many entries, maximum of %d.",
428 TR_MAX_CONST_MATCHES);
431 } else if (json_array_size(jdc)>0) {
432 if (NULL==(filt->lines[i]->domain_cons=tr_cfg_parse_one_constraint(filt->lines[i], "domain", jdc, rc))) {
433 tr_debug("tr_cfg_parse_one_filter: Error parsing domain constraint");
440 /*For each filter spec within the filter line... */
441 for (j=0; j <json_array_size(jfspecs); j++) {
442 if ((NULL==(jffield=json_object_get(json_array_get(jfspecs, j), "field"))) ||
443 (!json_is_string(jffield))) {
444 tr_debug("tr_cfg_parse_one_filter: Error parsing filter: missing field for filer spec %d, filter line %d.", i, j);
449 /* check that we have a match attribute */
450 if (NULL==(jfmatches=json_object_get(json_array_get(jfspecs, j), "match"))) {
451 tr_debug("tr_cfg_parse_one_filter: Error parsing filter: missing match for filer spec %d, filter line %d.", i, j);
456 /* check that match is an array */
457 if (!json_is_array(jfmatches)) {
458 tr_debug("tr_cfg_parse_one_filter: Error parsing filter: match not an array for filter spec %d, filter line %d.", i, j);
463 /* allocate the filter spec */
464 if (NULL==(filt->lines[i]->specs[j]=tr_fspec_new(filt->lines[i]))) {
465 tr_debug("tr_cfg_parse_one_filter: Out of memory.");
470 /* fill in the field */
471 if (NULL==(filt->lines[i]->specs[j]->field=tr_new_name(json_string_value(jffield)))) {
472 tr_debug("tr_cfg_parse_one_filter: Out of memory.");
477 /* fill in the matches */
478 for (k=0; k<json_array_size(jfmatches); k++) {
479 if (NULL==(jfmatch=json_array_get(jfmatches, k))) {
480 tr_debug("tr_cfg_parse_one_filter: Error parsing filter: unable to load match %d for filter spec %d, filter line %d.", k, i, j);
484 if (NULL==(name=tr_new_name(json_string_value(jfmatch)))) {
485 tr_debug("tr_cfg_parse_one_filter: Out of memory.");
489 if (0!=tr_fspec_add_match(filt->lines[i]->specs[j], name)) {
490 tr_debug("tr_cfg_parse_one_filter: Could not add match %d to filter spec %d, filter line %d.", k, i, j);
500 talloc_steal(mem_ctx, filt);
503 talloc_free(tmp_ctx);
504 if (*rc!=TR_CFG_SUCCESS)
509 static TR_FILTER *tr_cfg_parse_filters(TALLOC_CTX *mem_ctx, json_t *jfilts, TR_CFG_RC *rc)
511 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
513 TR_FILTER *filt=NULL;
523 jfilt=json_object_get(jfilts, "tid_inbound");
525 filt=tr_cfg_parse_one_filter(tmp_ctx, jfilt, TR_FILTER_TYPE_TID_INCOMING, rc);
526 if (*rc!=TR_CFG_SUCCESS) {
527 tr_debug("tr_cfg_parse_filters: Error parsing tid_inbound filter.");
532 tr_debug("tr_cfg_parse_filters: Unknown filter types in filter block.");
540 if (*rc==TR_CFG_SUCCESS)
541 talloc_steal(mem_ctx, filt);
542 else if (filt!=NULL) {
547 talloc_free(tmp_ctx);
551 static TR_AAA_SERVER *tr_cfg_parse_one_aaa_server(TALLOC_CTX *mem_ctx, json_t *jaddr, TR_CFG_RC *rc)
553 TR_AAA_SERVER *aaa = NULL;
556 if ((!jaddr) || (!json_is_string(jaddr))) {
557 tr_debug("tr_cfg_parse_one_aaa_server: Bad parameters.");
558 *rc = TR_CFG_BAD_PARAMS;
562 name=tr_new_name(json_string_value(jaddr));
564 tr_debug("tr_cfg_parse_one_aaa_server: Out of memory allocating hostname.");
569 aaa=tr_aaa_server_new(mem_ctx, name);
572 tr_debug("tr_cfg_parse_one_aaa_server: Out of memory allocating AAA server.");
580 static TR_AAA_SERVER *tr_cfg_parse_aaa_servers(TALLOC_CTX *mem_ctx, json_t *jaaas, TR_CFG_RC *rc)
582 TALLOC_CTX *tmp_ctx=NULL;
583 TR_AAA_SERVER *aaa = NULL;
584 TR_AAA_SERVER *temp_aaa = NULL;
587 for (i = 0; i < json_array_size(jaaas); i++) {
588 /* rc gets set in here */
589 if (NULL == (temp_aaa = tr_cfg_parse_one_aaa_server(tmp_ctx, json_array_get(jaaas, i), rc))) {
590 talloc_free(tmp_ctx);
593 /* TBD -- IPv6 addresses */
594 // tr_debug("tr_cfg_parse_aaa_servers: Configuring AAA Server: ip_addr = %s.", inet_ntoa(temp_aaa->aaa_server_addr));
595 temp_aaa->next = aaa;
598 tr_debug("tr_cfg_parse_aaa_servers: Finished (rc=%d)", *rc);
600 for (temp_aaa=aaa; temp_aaa!=NULL; temp_aaa=temp_aaa->next)
601 talloc_steal(mem_ctx, temp_aaa);
602 talloc_free(tmp_ctx);
606 static TR_APC *tr_cfg_parse_one_apc(TALLOC_CTX *mem_ctx, json_t *japc, TR_CFG_RC *rc)
611 *rc = TR_CFG_SUCCESS; /* presume success */
613 if ((!japc) || (!rc) || (!json_is_string(japc))) {
614 tr_debug("tr_cfg_parse_one_apc: Bad parameters.");
616 *rc = TR_CFG_BAD_PARAMS;
620 apc=tr_apc_new(mem_ctx);
622 tr_debug("tr_cfg_parse_one_apc: Out of memory.");
627 name=tr_new_name(json_string_value(japc));
629 tr_debug("tr_cfg_parse_one_apc: No memory for APC name.");
634 tr_apc_set_id(apc, name); /* apc is now responsible for freeing the name */
639 static TR_APC *tr_cfg_parse_apcs(TALLOC_CTX *mem_ctx, json_t *japcs, TR_CFG_RC *rc)
641 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
643 TR_APC *new_apc=NULL;
645 TR_CFG_RC call_rc=TR_CFG_ERROR;
647 *rc = TR_CFG_SUCCESS; /* presume success */
649 if ((!japcs) || (!rc) || (!json_is_array(japcs))) {
650 tr_debug("tr_cfg_parse_one_apc: Bad parameters.");
652 *rc = TR_CFG_BAD_PARAMS;
656 for (ii=0; ii<json_array_size(japcs); ii++) {
657 new_apc=tr_cfg_parse_one_apc(tmp_ctx, json_array_get(japcs, ii), &call_rc);
658 if ((call_rc!=TR_CFG_SUCCESS) || (new_apc==NULL)) {
659 tr_debug("tr_cfg_parse_apcs: Error parsing APC %d.", ii+1);
663 apcs=tr_apc_add(apcs, new_apc);
666 talloc_steal(mem_ctx, apcs);
670 talloc_free(tmp_ctx);
674 static TR_NAME *tr_cfg_parse_name(TALLOC_CTX *mem_ctx, json_t *jname, TR_CFG_RC *rc)
679 if ((jname==NULL) || (!json_is_string(jname))) {
680 tr_err("tr_cfg_parse_name: name missing or not a string");
681 *rc=TR_CFG_BAD_PARAMS;
684 name=tr_new_name(json_string_value(jname));
686 tr_err("tr_cfg_parse_name: could not allocate name");
695 static int tr_cfg_parse_shared_config(json_t *jsc, TR_CFG_RC *rc)
697 const char *shared=NULL;
701 (!json_is_string(jsc)) ||
702 (NULL==(shared=json_string_value(jsc)))) {
703 *rc=TR_CFG_BAD_PARAMS;
707 if (0==strcmp(shared, "no"))
709 else if (0==strcmp(shared, "yes"))
712 /* any other value is an error */
713 tr_debug("tr_cfg_parse_shared_config: invalid shared_config value \"%s\" (should be \"yes\" or \"no\")",
719 /* Parse the identity provider object from a realm and fill in the given TR_IDP_REALM. */
720 static TR_CFG_RC tr_cfg_parse_idp(TR_IDP_REALM *idp, json_t *jidp)
722 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
724 TR_AAA_SERVER *aaa=NULL;
725 TR_CFG_RC rc=TR_CFG_ERROR;
730 idp->origin=TR_REALM_LOCAL; /* if we're parsing it from a config file, it's local */
731 idp->shared_config=tr_cfg_parse_shared_config(json_object_get(jidp, "shared_config"), &rc);
732 if (rc!=TR_CFG_SUCCESS) {
733 tr_err("tr_cfg_parse_idp: missing or malformed shared_config specification");
738 apcs=tr_cfg_parse_apcs(tmp_ctx, json_object_get(jidp, "apcs"), &rc);
739 if ((rc!=TR_CFG_SUCCESS) || (apcs==NULL)) {
740 tr_err("tr_cfg_parse_idp: unable to parse APC");
744 tr_debug("tr_cfg_parse_idp: APC=\"%.*s\"",
748 aaa=tr_cfg_parse_aaa_servers(idp, json_object_get(jidp, "aaa_servers"), &rc);
749 if (rc!=TR_CFG_SUCCESS) {
750 tr_err("tr_cfg_parse_idp: unable to parse AAA servers");
755 /* done, fill in the idp structures */
757 talloc_steal(idp, apcs);
758 idp->aaa_servers=aaa;
762 if (rc!=TR_CFG_SUCCESS) {
766 tr_aaa_server_free(aaa);
769 talloc_free(tmp_ctx);
773 /* parses idp realm */
774 static TR_IDP_REALM *tr_cfg_parse_one_idp_realm(TALLOC_CTX *mem_ctx, json_t *jrealm, TR_CFG_RC *rc)
776 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
777 TR_IDP_REALM *realm=NULL;
778 TR_CFG_RC call_rc=TR_CFG_ERROR;
780 *rc=TR_CFG_ERROR; /* default to error if not set */
782 if ((!jrealm) || (!rc)) {
783 tr_err("tr_cfg_parse_one_idp_realm: Bad parameters.");
785 *rc=TR_CFG_BAD_PARAMS;
789 if (NULL==(realm=tr_idp_realm_new(tmp_ctx))) {
790 tr_err("tr_cfg_parse_one_idp_realm: could not allocate idp realm.");
795 /* must have a name */
796 realm->realm_id=tr_cfg_parse_name(realm,
797 json_object_get(jrealm, "realm"),
799 if ((call_rc!=TR_CFG_SUCCESS) || (realm->realm_id==NULL)) {
800 tr_err("tr_cfg_parse_one_idp_realm: could not parse realm name");
804 tr_debug("tr_cfg_parse_one_idp_realm: realm_id=\"%.*s\"",
805 realm->realm_id->len,
806 realm->realm_id->buf);
808 call_rc=tr_cfg_parse_idp(realm, json_object_get(jrealm, "identity_provider"));
809 if (call_rc!=TR_CFG_SUCCESS) {
810 tr_err("tr_cfg_parse_one_idp_realm: could not parse identity_provider.");
818 if (*rc==TR_CFG_SUCCESS)
819 talloc_steal(mem_ctx, realm);
825 talloc_free(tmp_ctx);
829 /* Determine whether the realm is an IDP realm */
830 static int tr_cfg_is_idp_realm(json_t *jrealm)
832 /* If a realm spec contains an identity_provider, it's an IDP realm. */
833 if (NULL != json_object_get(jrealm, "identity_provider"))
839 /* Parse any idp realms in the j_realms object. Ignores other realm types. */
840 static TR_IDP_REALM *tr_cfg_parse_idp_realms(TALLOC_CTX *mem_ctx, json_t *jrealms, TR_CFG_RC *rc)
842 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
843 TR_IDP_REALM *realms=NULL;
844 TR_IDP_REALM *new_realm=NULL;
845 json_t *this_jrealm=NULL;
849 if ((jrealms==NULL) || (!json_is_array(jrealms))) {
850 tr_err("tr_cfg_parse_idp_realms: realms not an array");
851 *rc=TR_CFG_BAD_PARAMS;
855 for (ii=0; ii<json_array_size(jrealms); ii++) {
856 this_jrealm=json_array_get(jrealms, ii);
857 if (tr_cfg_is_idp_realm(this_jrealm)) {
858 new_realm=tr_cfg_parse_one_idp_realm(tmp_ctx, this_jrealm, rc);
859 if ((*rc)!=TR_CFG_SUCCESS) {
860 tr_err("tr_cfg_parse_idp_realms: error decoding realm entry %d", ii+1);
864 realms=tr_idp_realm_add(realms, new_realm);
869 talloc_steal(mem_ctx, realms);
872 talloc_free(tmp_ctx);
876 static TR_GSS_NAMES *tr_cfg_parse_gss_names(TALLOC_CTX *mem_ctx, json_t *jgss_names, TR_CFG_RC *rc)
878 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
879 TR_GSS_NAMES *gn=NULL;
884 if ((rc==NULL) || (jgss_names==NULL)) {
885 tr_err("tr_cfg_parse_gss_names: Bad parameters.");
886 *rc=TR_CFG_BAD_PARAMS;
890 if (!json_is_array(jgss_names)) {
891 tr_err("tr_cfg_parse_gss_names: gss_names not an array.");
896 gn=tr_gss_names_new(tmp_ctx);
897 for (ii=0; ii<json_array_size(jgss_names); ii++) {
898 jname=json_array_get(jgss_names, ii);
899 if (!json_is_string(jname)) {
900 tr_err("tr_cfg_parse_gss_names: Encountered non-string gss name.");
905 name=tr_new_name(json_string_value(jname));
907 tr_err("tr_cfg_parse_gss_names: Out of memory allocating gss name.");
912 if (tr_gss_names_add(gn, name)!=0) {
914 tr_err("tr_cfg_parse_gss_names: Unable to add gss name to RP client.");
920 talloc_steal(mem_ctx, gn);
924 talloc_free(tmp_ctx);
925 if ((*rc!=TR_CFG_SUCCESS) && (gn!=NULL))
930 /* default filter accepts realm and *.realm */
931 static TR_FILTER *tr_cfg_default_filter(TALLOC_CTX *mem_ctx, TR_NAME *realm, TR_CFG_RC *rc)
933 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
934 TR_FILTER *filt=NULL;
935 TR_CONSTRAINT *cons=NULL;
937 TR_NAME *n_prefix=tr_new_name("*.");
938 TR_NAME *n_rp_realm=tr_new_name("rp_realm");
939 TR_NAME *n_domain=tr_new_name("domain");
940 TR_NAME *n_realm=tr_new_name("realm");
943 if ((realm==NULL) || (rc==NULL)) {
944 tr_debug("tr_cfg_default_filter: invalid arguments.");
946 *rc=TR_CFG_BAD_PARAMS;
950 if ((n_prefix==NULL) || (n_rp_realm==NULL) || (n_domain==NULL) || (n_realm==NULL)) {
951 tr_debug("tr_cfg_default_filter: unable to allocate names.");
956 filt=tr_filter_new(tmp_ctx);
958 tr_debug("tr_cfg_default_filter: could not allocate filter.");
962 tr_filter_set_type(filt, TR_FILTER_TYPE_TID_INCOMING);
963 filt->lines[0]=tr_fline_new(filt);
964 if (filt->lines[0]==NULL) {
965 tr_debug("tr_cfg_default_filter: could not allocate filter line.");
970 filt->lines[0]->action=TR_FILTER_ACTION_ACCEPT;
971 filt->lines[0]->specs[0]=tr_fspec_new(filt->lines[0]);
972 filt->lines[0]->specs[0]->field=n_rp_realm;
973 n_rp_realm=NULL; /* we don't own this name any more */
975 name=tr_dup_name(realm);
977 tr_debug("tr_cfg_default_filter: could not allocate realm name.");
981 if (0!=tr_fspec_add_match(filt->lines[0]->specs[0], name)) {
982 tr_debug("tr_cfg_default_filter: could not add realm name to filter spec.");
986 name=NULL; /* we no longer own the name */
988 if (NULL==(name=tr_name_cat(n_prefix, realm))) {
989 tr_debug("tr_cfg_default_filter: could not allocate wildcard realm name.");
994 if (0!=tr_fspec_add_match(filt->lines[0]->specs[0], name)) {
995 tr_debug("tr_cfg_default_filter: could not add wildcard realm name to filter spec.");
999 name=NULL; /* we no longer own the name */
1001 /* domain constraint */
1002 if (NULL==(cons=tr_constraint_new(filt->lines[0]))) {
1003 tr_debug("tr_cfg_default_filter: could not allocate domain constraint.");
1008 cons->type=n_domain;
1009 n_domain=NULL; /* belongs to the constraint now */
1010 name=tr_dup_name(realm);
1012 tr_debug("tr_cfg_default_filter: could not allocate realm name for domain constraint.");
1016 cons->matches[0]=name;
1017 name=tr_name_cat(n_prefix, realm);
1019 tr_debug("tr_cfg_default_filter: could not allocate wildcard realm name for domain constraint.");
1023 cons->matches[1]=name;
1025 filt->lines[0]->domain_cons=cons;
1028 /* realm constraint */
1029 if (NULL==(cons=tr_constraint_new(filt->lines[0]))) {
1030 tr_debug("tr_cfg_default_filter: could not allocate realm constraint.");
1036 n_realm=NULL; /* belongs to the constraint now */
1037 name=tr_dup_name(realm);
1039 tr_debug("tr_cfg_default_filter: could not allocate realm name for realm constraint.");
1043 cons->matches[0]=name;
1044 name=tr_name_cat(n_prefix, realm);
1046 tr_debug("tr_cfg_default_filter: could not allocate wildcard realm name for realm constraint.");
1050 cons->matches[1]=name;
1052 filt->lines[0]->realm_cons=cons;
1054 talloc_steal(mem_ctx, filt);
1056 talloc_free(tmp_ctx);
1058 if (*rc!=TR_CFG_SUCCESS)
1062 tr_free_name(n_prefix);
1063 if (n_rp_realm!=NULL)
1064 tr_free_name(n_rp_realm);
1066 tr_free_name(n_realm);
1068 tr_free_name(n_domain);
1075 /* parses rp client */
1076 static TR_RP_CLIENT *tr_cfg_parse_one_rp_client(TALLOC_CTX *mem_ctx, json_t *jrealm, TR_CFG_RC *rc)
1078 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1079 TR_RP_CLIENT *client=NULL;
1080 TR_CFG_RC call_rc=TR_CFG_ERROR;
1081 TR_FILTER *new_filt=NULL;
1082 TR_NAME *realm=NULL;
1084 json_t *jrealm_id=NULL;
1086 *rc=TR_CFG_ERROR; /* default to error if not set */
1088 if ((!jrealm) || (!rc)) {
1089 tr_err("tr_cfg_parse_one_rp_client: Bad parameters.");
1091 *rc=TR_CFG_BAD_PARAMS;
1095 if ((NULL==(jrealm_id=json_object_get(jrealm, "realm"))) || (!json_is_string(jrealm_id))) {
1096 tr_debug("tr_cfg_parse_one_rp_client: no realm ID found.");
1097 *rc=TR_CFG_BAD_PARAMS;
1101 tr_debug("tr_cfg_parse_one_rp_client: realm_id=\"%s\"", json_string_value(jrealm_id));
1102 realm=tr_new_name(json_string_value(jrealm_id));
1104 tr_err("tr_cfg_parse_one_rp_client: could not allocate realm ID.");
1109 if (NULL==(client=tr_rp_client_new(tmp_ctx))) {
1110 tr_err("tr_cfg_parse_one_rp_client: could not allocate rp client.");
1115 client->gss_names=tr_cfg_parse_gss_names(client, json_object_get(jrealm, "gss_names"), &call_rc);
1117 if (call_rc!=TR_CFG_SUCCESS) {
1118 tr_err("tr_cfg_parse_one_rp_client: could not parse gss_names.");
1124 jfilt=json_object_get(jrealm, "filters");
1126 new_filt=tr_cfg_parse_filters(tmp_ctx, jfilt, &call_rc);
1127 if (call_rc!=TR_CFG_SUCCESS) {
1128 tr_err("tr_cfg_parse_one_rp_client: could not parse filters.");
1133 tr_debug("tr_cfg_parse_one_rp_client: no filters specified, using default filters.");
1134 new_filt=tr_cfg_default_filter(tmp_ctx, realm, &call_rc);
1135 if (call_rc!=TR_CFG_SUCCESS) {
1136 tr_err("tr_cfg_parse_one_rp_client: could not set default filters.");
1142 tr_rp_client_set_filter(client, new_filt);
1147 tr_free_name(realm);
1149 if (*rc==TR_CFG_SUCCESS)
1150 talloc_steal(mem_ctx, client);
1152 talloc_free(client);
1156 talloc_free(tmp_ctx);
1160 /* Determine whether the realm is an RP realm */
1161 static int tr_cfg_is_rp_realm(json_t *jrealm)
1163 /* Check that we have a gss name. */
1164 if (NULL != json_object_get(jrealm, "gss_names"))
1170 /* Parse any rp clients in the j_realms object. Ignores other realms. */
1171 static TR_RP_CLIENT *tr_cfg_parse_rp_clients(TALLOC_CTX *mem_ctx, json_t *jrealms, TR_CFG_RC *rc)
1173 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1174 TR_RP_CLIENT *clients=NULL;
1175 TR_RP_CLIENT *new_client=NULL;
1176 json_t *this_jrealm=NULL;
1180 if ((jrealms==NULL) || (!json_is_array(jrealms))) {
1181 tr_err("tr_cfg_parse_rp_clients: realms not an array");
1182 *rc=TR_CFG_BAD_PARAMS;
1186 for (ii=0; ii<json_array_size(jrealms); ii++) {
1187 this_jrealm=json_array_get(jrealms, ii);
1188 if (tr_cfg_is_rp_realm(this_jrealm)) {
1189 new_client=tr_cfg_parse_one_rp_client(tmp_ctx, this_jrealm, rc);
1190 if ((*rc)!=TR_CFG_SUCCESS) {
1191 tr_err("tr_cfg_parse_rp_clients: error decoding realm entry %d", ii+1);
1195 clients=tr_rp_client_add(clients, new_client);
1200 talloc_steal(mem_ctx, clients);
1203 talloc_free(tmp_ctx);
1207 /* takes a talloc context, but currently does not use it */
1208 static TR_NAME *tr_cfg_parse_org_name(TALLOC_CTX *mem_ctx, json_t *j_org, TR_CFG_RC *rc)
1212 if ((j_org==NULL) || (rc==NULL) || (!json_is_string(j_org))) {
1213 tr_debug("tr_cfg_parse_org_name: Bad parameters.");
1215 *rc = TR_CFG_BAD_PARAMS; /* fill in return value if we can */
1219 name=tr_new_name(json_string_value(j_org));
1228 /* Update the community information with data from a new batch of IDP realms.
1229 * May partially add realms if there is a failure, no guarantees.
1230 * Call like comms=tr_comm_idp_update(comms, new_realms, &rc) */
1231 static TR_COMM *tr_cfg_comm_idp_update(TALLOC_CTX *mem_ctx, TR_COMM *comms, TR_IDP_REALM *new_realms, TR_CFG_RC *rc)
1233 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1234 TR_COMM *comm=NULL; /* community looked up in comms table */
1235 TR_COMM *new_comms=NULL; /* new communities as we create them */
1236 TR_IDP_REALM *realm=NULL;
1237 TR_APC *apc=NULL; /* apc of one realm */
1240 *rc=TR_CFG_BAD_PARAMS;
1244 /* start with an empty list communities, then fill that in */
1245 for (realm=new_realms; realm!=NULL; realm=realm->next) {
1246 for (apc=realm->apcs; apc!=NULL; apc=apc->next) {
1247 comm=tr_comm_lookup(comms, apc->id);
1249 comm=tr_comm_new(tmp_ctx);
1251 tr_debug("tr_cfg_comm_idp_update: unable to allocate new community.");
1255 /* fill in the community with info */
1256 comm->type=TR_COMM_APC; /* realms added this way are in APCs */
1257 comm->expiration_interval=TR_DEFAULT_APC_EXPIRATION_INTERVAL;
1258 comm->id=tr_dup_name(apc->id);
1259 tr_comm_add_idp_realm(comm, realm);
1260 new_comms=tr_comm_add(new_comms, comm);
1262 /* add this realm to the comm */
1263 tr_comm_add_idp_realm(comm, realm);
1268 /* we successfully built a list, add it to the other list */
1269 comms=tr_comm_add(comms, new_comms);
1270 talloc_steal(mem_ctx, comms);
1272 talloc_free(tmp_ctx);
1277 static TR_CFG_RC tr_cfg_parse_one_local_org(TR_CFG *trc, json_t *jlorg)
1279 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1280 TR_CFG_RC retval=TR_CFG_ERROR; /* our return code */
1281 TR_CFG_RC rc=TR_CFG_ERROR; /* return code from functions we call */
1282 TR_NAME *org_name=NULL;
1284 json_t *j_realms=NULL;
1285 TR_IDP_REALM *new_idp_realms=NULL;
1286 TR_RP_CLIENT *new_rp_clients=NULL;
1288 tr_debug("tr_cfg_parse_one_local_org: parsing local organization");
1290 /* get organization_name (optional) */
1291 if (NULL==(j_org=json_object_get(jlorg, "organization_name"))) {
1292 tr_debug("tr_cfg_parse_one_local_org: organization_name unspecified");
1294 org_name=tr_cfg_parse_org_name(tmp_ctx, j_org, &rc);
1295 if (rc==TR_CFG_SUCCESS) {
1296 tr_debug("tr_cfg_parse_one_local_org: organization_name=\"%.*s\"",
1299 /* we don't actually do anything with this, but we could */
1300 tr_free_name(org_name);
1305 /* Now get realms. Allow this to be missing; even though that is a pointless organization entry,
1306 * it's harmless. Report a warning because that might be unintentional. */
1307 if (NULL==(j_realms=json_object_get(jlorg, "realms"))) {
1308 tr_warning("tr_cfg_parse_one_local_org: warning - no realms in this local organization");
1310 /* Allocate in the tmp_ctx so these will be cleaned up if we do not complete successfully. */
1311 new_idp_realms=tr_cfg_parse_idp_realms(tmp_ctx, j_realms, &rc);
1312 if (rc!=TR_CFG_SUCCESS)
1315 new_rp_clients=tr_cfg_parse_rp_clients(tmp_ctx, j_realms, &rc);
1316 if (rc!=TR_CFG_SUCCESS)
1319 retval=TR_CFG_SUCCESS;
1322 /* if we succeeded, link things to the configuration and move out of tmp context */
1323 if (retval==TR_CFG_SUCCESS) {
1324 if (new_idp_realms!=NULL) {
1325 trc->idp_realms=tr_idp_realm_add(trc->idp_realms, new_idp_realms); /* fixes talloc contexts except for head*/
1326 talloc_steal(trc, trc->idp_realms); /* make sure the head is in the right context */
1329 if (new_rp_clients!=NULL) {
1330 trc->rp_clients=tr_rp_client_add(trc->rp_clients, new_rp_clients); /* fixes talloc contexts */
1331 talloc_steal(trc, trc->rp_clients); /* make sure head is in the right context */
1335 talloc_free(tmp_ctx);
1339 /* Parse local organizations if present. Returns success if there are none. On failure, the configuration is unreliable. */
1340 static TR_CFG_RC tr_cfg_parse_local_orgs(TR_CFG *trc, json_t *jcfg)
1342 json_t *jlocorgs=NULL;
1345 jlocorgs=json_object_get(jcfg, "local_organizations");
1347 return TR_CFG_SUCCESS;
1349 if (!json_is_array(jlocorgs)) {
1350 tr_err("tr_cfg_parse_local_orgs: local_organizations is not an array.");
1351 return TR_CFG_NOPARSE;
1354 for (ii=0; ii<json_array_size(jlocorgs); ii++) {
1355 if (tr_cfg_parse_one_local_org(trc, json_array_get(jlocorgs, ii))!=TR_CFG_SUCCESS) {
1356 tr_err("tr_cfg_parse_local_orgs: error parsing local_organization %d.", ii+1);
1357 return TR_CFG_NOPARSE;
1361 return TR_CFG_SUCCESS;
1364 static TR_CFG_RC tr_cfg_parse_one_peer_org(TR_CFG *trc, json_t *jporg)
1366 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1370 TRP_PEER *new_peer=NULL;
1371 TR_GSS_NAMES *names=NULL;
1372 TR_CFG_RC rc=TR_CFG_ERROR;
1374 jhost=json_object_get(jporg, "hostname");
1375 jport=json_object_get(jporg, "port");
1376 jgss=json_object_get(jporg, "gss_names");
1378 if ((jhost==NULL) || (!json_is_string(jhost))) {
1379 tr_err("tr_cfg_parse_one_peer_org: hostname not specified or not a string.");
1384 if ((jport!=NULL) && (!json_is_number(jport))) {
1385 /* note that not specifying the port is allowed, but if set it must be a number */
1386 tr_err("tr_cfg_parse_one_peer_org: port is not a number.");
1391 if ((jgss==NULL) || (!json_is_array(jgss))) {
1392 tr_err("tr_cfg_parse_one_peer_org: gss_names not specified or not an array.");
1397 new_peer=trp_peer_new(tmp_ctx);
1398 if (new_peer==NULL) {
1399 tr_err("tr_cfg_parse_one_peer_org: could not allocate new peer.");
1404 trp_peer_set_server(new_peer, json_string_value(jhost));
1406 trp_peer_set_port(new_peer, TRP_PORT);
1408 trp_peer_set_port(new_peer, json_integer_value(jport));
1410 names=tr_cfg_parse_gss_names(tmp_ctx, jgss, &rc);
1411 if (rc!=TR_CFG_SUCCESS) {
1412 tr_err("tr_cfg_parse_one_peer_org: unable to parse gss names.");
1416 trp_peer_set_gss_names(new_peer, names);
1419 trp_ptable_add(trc->peers, new_peer);
1423 talloc_free(tmp_ctx);
1427 /* Parse peer organizations, if present. Returns success if there are none. */
1428 static TR_CFG_RC tr_cfg_parse_peer_orgs(TR_CFG *trc, json_t *jcfg)
1430 json_t *jpeerorgs=NULL;
1433 jpeerorgs=json_object_get(jcfg, "peer_organizations");
1434 if (jpeerorgs==NULL)
1435 return TR_CFG_SUCCESS;
1437 if (!json_is_array(jpeerorgs)) {
1438 tr_err("tr_cfg_parse_peer_orgs: peer_organizations is not an array.");
1439 return TR_CFG_NOPARSE;
1442 for (ii=0; ii<json_array_size(jpeerorgs); ii++) {
1443 if (tr_cfg_parse_one_peer_org(trc, json_array_get(jpeerorgs, ii))!=TR_CFG_SUCCESS) {
1444 tr_err("tr_cfg_parse_peer_orgs: error parsing peer_organization %d.", ii+1);
1445 return TR_CFG_NOPARSE;
1449 return TR_CFG_SUCCESS;
1452 static TR_CFG_RC tr_cfg_parse_default_servers (TR_CFG *trc, json_t *jcfg)
1454 json_t *jdss = NULL;
1455 TR_CFG_RC rc = TR_CFG_SUCCESS;
1456 TR_AAA_SERVER *ds = NULL;
1459 if ((!trc) || (!jcfg))
1460 return TR_CFG_BAD_PARAMS;
1462 /* If there are default servers, store them */
1463 if ((NULL != (jdss = json_object_get(jcfg, "default_servers"))) &&
1464 (json_is_array(jdss)) &&
1465 (0 < json_array_size(jdss))) {
1467 for (i = 0; i < json_array_size(jdss); i++) {
1468 if (NULL == (ds = tr_cfg_parse_one_aaa_server(trc,
1469 json_array_get(jdss, i),
1473 tr_debug("tr_cfg_parse_default_servers: Default server configured: %s", ds->hostname->buf);
1474 ds->next = trc->default_servers;
1475 trc->default_servers = ds;
1479 tr_debug("tr_cfg_parse_default_servers: Finished (rc=%d)", rc);
1483 static TR_IDP_REALM *tr_cfg_parse_comm_idps (TR_CFG *trc, json_t *jidps, TR_CFG_RC *rc)
1485 TR_IDP_REALM *idp = NULL;
1486 TR_IDP_REALM *found_idp = NULL;
1487 TR_IDP_REALM *temp_idp = NULL;
1492 (!json_is_array(jidps))) {
1494 *rc = TR_CFG_BAD_PARAMS;
1498 for (i = 0; i < json_array_size(jidps); i++) {
1499 if (NULL == (temp_idp = talloc(trc, TR_IDP_REALM))) {
1500 tr_debug("tr_cfg_parse_comm_idps: Can't allocate memory for IdP Realm.");
1505 memset (temp_idp, 0, sizeof(TR_IDP_REALM));
1507 if (NULL == (found_idp = (tr_cfg_find_idp(trc,
1508 tr_new_name((char *)json_string_value(json_array_get(jidps, i))),
1510 tr_debug("tr_cfg_parse_comm_idps: Unknown IDP %s.",
1511 (char *)json_string_value(json_array_get(jidps, i)));
1515 // We *MUST* do a dereferenced copy here or the second community will corrupt the linked list we create here.
1516 *temp_idp = *found_idp;
1518 temp_idp->comm_next = idp;
1525 static TR_RP_REALM *tr_cfg_parse_comm_rps (TR_CFG *trc, json_t *jrps, TR_CFG_RC *rc)
1527 TR_RP_REALM *rp = NULL;
1528 TR_RP_REALM *temp_rp = NULL;
1533 (!json_is_array(jrps))) {
1535 *rc = TR_CFG_BAD_PARAMS;
1539 for (i = (json_array_size(jrps)-1); i >= 0; i--) {
1540 if (NULL == (temp_rp = talloc_zero(trc, TR_RP_REALM))) {
1541 tr_debug("tr_cfg_parse_comm_rps: Can't allocate memory for RP Realm.");
1547 if (NULL == (temp_rp->realm_name = tr_new_name((char *)json_string_value(json_array_get(jrps, i))))) {
1548 tr_debug("tr_cfg_parse_comm_rps: No memory for RP Realm Name.");
1561 static TR_COMM *tr_cfg_parse_one_comm (TR_CFG *trc, json_t *jcomm, TR_CFG_RC *rc) {
1562 TR_COMM *comm = NULL;
1564 json_t *jtype = NULL;
1565 json_t *japcs = NULL;
1566 json_t *jidps = NULL;
1567 json_t *jrps = NULL;
1569 if ((!trc) || (!jcomm) || (!rc)) {
1570 tr_debug("tr_cfg_parse_one_comm: Bad parameters.");
1572 *rc = TR_CFG_BAD_PARAMS;
1576 if (NULL == (comm = talloc_zero(trc, TR_COMM))) {
1577 tr_crit("tr_cfg_parse_one_comm: Out of memory.");
1583 if ((NULL == (jid = json_object_get(jcomm, "community_id"))) ||
1584 (!json_is_string(jid)) ||
1585 (NULL == (jtype = json_object_get(jcomm, "type"))) ||
1586 (!json_is_string(jtype)) ||
1587 (NULL == (japcs = json_object_get(jcomm, "apcs"))) ||
1588 (!json_is_array(japcs)) ||
1589 (NULL == (jidps = json_object_get(jcomm, "idp_realms"))) ||
1590 (!json_is_array(jidps)) ||
1591 (NULL == (jrps = json_object_get(jcomm, "rp_realms"))) ||
1592 (!json_is_array(jrps))) {
1593 tr_debug("tr_cfg_parse_one_comm: Error parsing Communities configuration.");
1594 *rc = TR_CFG_NOPARSE;
1598 if (NULL == (comm->id = tr_new_name((char *)json_string_value(jid)))) {
1599 tr_debug("tr_cfg_parse_one_comm: No memory for community id.");
1604 if (0 == strcmp(json_string_value(jtype), "apc")) {
1605 comm->type = TR_COMM_APC;
1606 } else if (0 == strcmp(json_string_value(jtype), "coi")) {
1607 comm->type = TR_COMM_COI;
1608 if (NULL == (comm->apcs = tr_cfg_parse_apcs(trc, japcs, rc))) {
1609 tr_debug("tr_cfg_parse_one_comm: Can't parse APCs for COI %s.", comm->id->buf);
1610 tr_free_name(comm->id);
1614 tr_debug("tr_cfg_parse_one_comm: Invalid community type, comm = %s, type = %s", comm->id->buf, json_string_value(jtype));
1615 tr_free_name(comm->id);
1616 *rc = TR_CFG_NOPARSE;
1620 comm->idp_realms = tr_cfg_parse_comm_idps(trc, jidps, rc);
1621 if (TR_CFG_SUCCESS != *rc) {
1622 tr_debug("tr_cfg_parse_one_comm: Can't parse IDP realms for comm %s.", comm->id->buf);
1623 tr_free_name(comm->id);
1627 comm->rp_realms = tr_cfg_parse_comm_rps(trc, jrps, rc);
1628 if (TR_CFG_SUCCESS != *rc) {
1629 tr_debug("tr_cfg_parse_comm: Can't parse RP realms for comm %s .", comm->id->buf);
1630 tr_free_name(comm->id);
1634 if (TR_COMM_APC == comm->type) {
1635 json_t *jexpire = json_object_get(jcomm, "expiration_interval");
1636 comm->expiration_interval = 43200; /*30 days*/
1638 if (!json_is_integer(jexpire)) {
1639 fprintf(stderr, "tr_parse_comm: expirae_interval is not an integer\n");
1642 comm->expiration_interval = json_integer_value(jexpire);
1643 if (comm->expiration_interval <= 10)
1644 comm->expiration_interval = 11; /* Freeradius waits 10 minutes between successful TR queries*/
1645 if (comm->expiration_interval > 129600) /* 90 days*/
1646 comm->expiration_interval = 129600;
1653 static TR_CFG_RC tr_cfg_parse_comms (TR_CFG *trc, json_t *jcfg)
1655 json_t *jcomms = NULL;
1656 TR_CFG_RC rc = TR_CFG_SUCCESS;
1657 TR_COMM *comm = NULL;
1660 if ((!trc) || (!jcfg)) {
1661 tr_debug("tr_cfg_parse_comms: Bad Parameters.");
1662 return TR_CFG_BAD_PARAMS;
1665 if (NULL != (jcomms = json_object_get(jcfg, "communities"))) {
1666 if (!json_is_array(jcomms)) {
1667 return TR_CFG_NOPARSE;
1670 for (i = 0; i < json_array_size(jcomms); i++) {
1671 if (NULL == (comm = tr_cfg_parse_one_comm(trc,
1672 json_array_get(jcomms, i),
1676 tr_debug("tr_cfg_parse_comms: Community configured: %s.", comm->id->buf);
1677 comm->next = trc->comms;
1681 tr_debug("tr_cfg_parse_comms: Finished (rc=%d)", rc);
1685 TR_CFG_RC tr_cfg_validate(TR_CFG *trc)
1687 TR_CFG_RC rc = TR_CFG_SUCCESS;
1690 return TR_CFG_BAD_PARAMS;
1692 if ((NULL == trc->internal)||
1693 (NULL == trc->internal->hostname)) {
1694 tr_debug("tr_cfg_validate: Error: No internal configuration, or no hostname.");
1698 if (NULL == trc->rp_clients) {
1699 tr_debug("tr_cfg_validate: Error: No RP Clients configured");
1703 if (NULL == trc->comms) {
1704 tr_debug("tr_cfg_validate: Error: No Communities configured");
1708 if ((NULL == trc->default_servers) && (NULL == trc->idp_realms)) {
1709 tr_debug("tr_cfg_validate: Error: No default servers or IDPs configured.");
1716 /* Join two paths and return a pointer to the result. This should be freed
1717 * via talloc_free. Returns NULL on failure. */
1718 static char *join_paths(TALLOC_CTX *mem_ctx, const char *p1, const char *p2)
1720 return talloc_asprintf(mem_ctx, "%s/%s", p1, p2); /* returns NULL on a failure */
1723 TR_CFG_RC tr_cfg_parse_one_config_file(TR_CFG *cfg, const char *file_with_path)
1729 if (NULL==(jcfg=json_load_file(file_with_path,
1730 JSON_DISABLE_EOF_CHECK, &rc))) {
1731 tr_debug("tr_parse_one_config_file: Error parsing config file %s.",
1733 return TR_CFG_NOPARSE;
1736 // Look for serial number and log it if it exists
1737 if (NULL!=(jser=json_object_get(jcfg, "serial_number"))) {
1738 if (json_is_number(jser)) {
1739 tr_notice("tr_parse_one_config_file: Attempting to load revision %" JSON_INTEGER_FORMAT " of '%s'.",
1740 json_integer_value(jser),
1745 if ((TR_CFG_SUCCESS != tr_cfg_parse_internal(cfg, jcfg)) ||
1746 (TR_CFG_SUCCESS != tr_cfg_parse_local_orgs(cfg, jcfg)) ||
1747 (TR_CFG_SUCCESS != tr_cfg_parse_peer_orgs(cfg, jcfg)) ||
1748 (TR_CFG_SUCCESS != tr_cfg_parse_default_servers(cfg, jcfg)) ||
1749 (TR_CFG_SUCCESS != tr_cfg_parse_comms(cfg, jcfg)))
1750 return TR_CFG_ERROR;
1752 return TR_CFG_SUCCESS;
1755 /* Reads configuration files in config_dir ("" or "./" will use the current directory). */
1756 TR_CFG_RC tr_parse_config(TR_CFG_MGR *cfg_mgr, const char *config_dir, int n, struct dirent **cfg_files)
1758 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1759 char *file_with_path;
1761 TR_CFG_RC cfg_rc=TR_CFG_ERROR;
1763 if ((!cfg_mgr) || (!cfg_files) || (n<=0)) {
1764 cfg_rc=TR_CFG_BAD_PARAMS;
1768 if (cfg_mgr->new != NULL)
1769 tr_cfg_free(cfg_mgr->new);
1770 cfg_mgr->new=tr_cfg_new(tmp_ctx); /* belongs to the temporary context for now */
1771 if (cfg_mgr->new == NULL) {
1772 cfg_rc=TR_CFG_NOMEM;
1776 cfg_mgr->new->peers=trp_ptable_new(cfg_mgr);
1778 /* Parse configuration information from each config file */
1779 for (ii=0; ii<n; ii++) {
1780 file_with_path=join_paths(tmp_ctx, config_dir, cfg_files[ii]->d_name); /* must free result with talloc_free */
1781 if(file_with_path == NULL) {
1782 tr_crit("tr_parse_config: error joining path.");
1783 cfg_rc=TR_CFG_NOMEM;
1786 tr_debug("tr_parse_config: Parsing %s.", cfg_files[ii]->d_name); /* print the filename without the path */
1787 cfg_rc=tr_cfg_parse_one_config_file(cfg_mgr->new, file_with_path);
1788 if (cfg_rc!=TR_CFG_SUCCESS) {
1789 tr_crit("tr_parse_config: Error parsing %s", file_with_path);
1792 talloc_free(file_with_path); /* done with filename */
1795 /* make sure we got a complete, consistent configuration */
1796 if (TR_CFG_SUCCESS != tr_cfg_validate(cfg_mgr->new)) {
1797 tr_err("tr_parse_config: Error: INVALID CONFIGURATION");
1798 cfg_rc=TR_CFG_ERROR;
1803 talloc_steal(cfg_mgr, cfg_mgr->new); /* hand this over to the cfg_mgr context */
1804 cfg_rc=TR_CFG_SUCCESS;
1807 talloc_free(tmp_ctx);
1811 TR_IDP_REALM *tr_cfg_find_idp (TR_CFG *tr_cfg, TR_NAME *idp_id, TR_CFG_RC *rc)
1814 TR_IDP_REALM *cfg_idp;
1816 if ((!tr_cfg) || (!idp_id)) {
1818 *rc = TR_CFG_BAD_PARAMS;
1822 for (cfg_idp = tr_cfg->idp_realms; NULL != cfg_idp; cfg_idp = cfg_idp->next) {
1823 if (!tr_name_cmp (idp_id, cfg_idp->realm_id)) {
1824 tr_debug("tr_cfg_find_idp: Found %s.", idp_id->buf);
1828 /* if we didn't find one, return NULL */
1832 TR_RP_CLIENT *tr_cfg_find_rp (TR_CFG *tr_cfg, TR_NAME *rp_gss, TR_CFG_RC *rc)
1834 TR_RP_CLIENT *cfg_rp;
1836 if ((!tr_cfg) || (!rp_gss)) {
1838 *rc = TR_CFG_BAD_PARAMS;
1842 for (cfg_rp = tr_cfg->rp_clients; NULL != cfg_rp; cfg_rp = cfg_rp->next) {
1843 if (tr_gss_names_matches(cfg_rp->gss_names, rp_gss)) {
1844 tr_debug("tr_cfg_find_rp: Found %s.", rp_gss->buf);
1848 /* if we didn't find one, return NULL */
1852 static int is_cfg_file(const struct dirent *dent) {
1855 /* Only accept filenames ending in ".cfg" and starting with a character
1856 * other than an ASCII '.' */
1858 /* filename must be at least 4 characters long to be acceptable */
1859 n=strlen(dent->d_name);
1864 /* filename must not start with '.' */
1865 if ('.' == dent->d_name[0]) {
1869 /* If the above passed and the last four characters of the filename are .cfg, accept.
1870 * (n.b., assumes an earlier test checked that the name is >= 4 chars long.) */
1871 if (0 == strcmp(&(dent->d_name[n-4]), ".cfg")) {
1875 /* otherwise, return false. */
1879 /* Find configuration files in a particular directory. Returns the
1880 * number of entries found, 0 if none are found, or <0 for some
1881 * errors. If n>=0, the cfg_files parameter will contain a newly
1882 * allocated array of pointers to struct dirent entries, as returned
1883 * by scandir(). These can be freed with tr_free_config_file_list().
1885 int tr_find_config_files (const char *config_dir, struct dirent ***cfg_files) {
1888 n = scandir(config_dir, cfg_files, is_cfg_file, alphasort);
1892 tr_debug("tr_find_config: scandir error trying to scan %s.", config_dir);
1898 /* Free memory allocated for configuration file list returned from tr_find_config_files().
1899 * This can be called regardless of the return value of tr_find_config_values(). */
1900 void tr_free_config_file_list(int n, struct dirent ***cfg_files) {
1903 /* if n < 0, then scandir did not allocate anything because it failed */
1904 if((n>=0) && (*cfg_files != NULL)) {
1905 for(ii=0; ii<n; ii++) {
1906 free((*cfg_files)[ii]);
1908 free(*cfg_files); /* safe even if n==0 */
1909 *cfg_files=NULL; /* this will help prevent accidentally freeing twice */