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 /* parses rp client */
931 static TR_RP_CLIENT *tr_cfg_parse_one_rp_client(TALLOC_CTX *mem_ctx, json_t *jrealm, TR_CFG_RC *rc)
933 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
934 TR_RP_CLIENT *client=NULL;
935 TR_CFG_RC call_rc=TR_CFG_ERROR;
936 TR_FILTER *new_filt=NULL;
938 *rc=TR_CFG_ERROR; /* default to error if not set */
940 if ((!jrealm) || (!rc)) {
941 tr_err("tr_cfg_parse_one_rp_client: Bad parameters.");
943 *rc=TR_CFG_BAD_PARAMS;
947 if (NULL==(client=tr_rp_client_new(tmp_ctx))) {
948 tr_err("tr_cfg_parse_one_rp_client: could not allocate rp client.");
953 client->gss_names=tr_cfg_parse_gss_names(client, json_object_get(jrealm, "gss_names"), &call_rc);
955 if (call_rc!=TR_CFG_SUCCESS) {
956 tr_err("tr_cfg_parse_one_rp_client: could not parse gss_names.");
962 new_filt=tr_cfg_parse_filters(tmp_ctx, json_object_get(jrealm, "filters"), &call_rc);
963 if (call_rc!=TR_CFG_SUCCESS) {
964 tr_err("tr_cfg_parse_one_rp_client: could not parse filters.");
969 tr_rp_client_set_filter(client, new_filt);
973 if (*rc==TR_CFG_SUCCESS)
974 talloc_steal(mem_ctx, client);
980 talloc_free(tmp_ctx);
984 /* Determine whether the realm is an RP realm */
985 static int tr_cfg_is_rp_realm(json_t *jrealm)
987 /* Check that we have a gss name. */
988 if (NULL != json_object_get(jrealm, "gss_names"))
994 /* Parse any rp clients in the j_realms object. Ignores other realms. */
995 static TR_RP_CLIENT *tr_cfg_parse_rp_clients(TALLOC_CTX *mem_ctx, json_t *jrealms, TR_CFG_RC *rc)
997 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
998 TR_RP_CLIENT *clients=NULL;
999 TR_RP_CLIENT *new_client=NULL;
1000 json_t *this_jrealm=NULL;
1004 if ((jrealms==NULL) || (!json_is_array(jrealms))) {
1005 tr_err("tr_cfg_parse_rp_clients: realms not an array");
1006 *rc=TR_CFG_BAD_PARAMS;
1010 for (ii=0; ii<json_array_size(jrealms); ii++) {
1011 this_jrealm=json_array_get(jrealms, ii);
1012 if (tr_cfg_is_rp_realm(this_jrealm)) {
1013 new_client=tr_cfg_parse_one_rp_client(tmp_ctx, this_jrealm, rc);
1014 if ((*rc)!=TR_CFG_SUCCESS) {
1015 tr_err("tr_cfg_parse_rp_clients: error decoding realm entry %d", ii+1);
1019 clients=tr_rp_client_add(clients, new_client);
1024 talloc_steal(mem_ctx, clients);
1027 talloc_free(tmp_ctx);
1031 /* takes a talloc context, but currently does not use it */
1032 static TR_NAME *tr_cfg_parse_org_name(TALLOC_CTX *mem_ctx, json_t *j_org, TR_CFG_RC *rc)
1036 if ((j_org==NULL) || (rc==NULL) || (!json_is_string(j_org))) {
1037 tr_debug("tr_cfg_parse_org_name: Bad parameters.");
1039 *rc = TR_CFG_BAD_PARAMS; /* fill in return value if we can */
1043 name=tr_new_name(json_string_value(j_org));
1052 /* Update the community information with data from a new batch of IDP realms.
1053 * May partially add realms if there is a failure, no guarantees.
1054 * Call like comms=tr_comm_idp_update(comms, new_realms, &rc) */
1055 static TR_COMM *tr_cfg_comm_idp_update(TALLOC_CTX *mem_ctx, TR_COMM *comms, TR_IDP_REALM *new_realms, TR_CFG_RC *rc)
1057 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1058 TR_COMM *comm=NULL; /* community looked up in comms table */
1059 TR_COMM *new_comms=NULL; /* new communities as we create them */
1060 TR_IDP_REALM *realm=NULL;
1061 TR_APC *apc=NULL; /* apc of one realm */
1064 *rc=TR_CFG_BAD_PARAMS;
1068 /* start with an empty list communities, then fill that in */
1069 for (realm=new_realms; realm!=NULL; realm=realm->next) {
1070 for (apc=realm->apcs; apc!=NULL; apc=apc->next) {
1071 comm=tr_comm_lookup(comms, apc->id);
1073 comm=tr_comm_new(tmp_ctx);
1075 tr_debug("tr_cfg_comm_idp_update: unable to allocate new community.");
1079 /* fill in the community with info */
1080 comm->type=TR_COMM_APC; /* realms added this way are in APCs */
1081 comm->expiration_interval=TR_DEFAULT_APC_EXPIRATION_INTERVAL;
1082 comm->id=tr_dup_name(apc->id);
1083 tr_comm_add_idp_realm(comm, realm);
1084 new_comms=tr_comm_add(new_comms, comm);
1086 /* add this realm to the comm */
1087 tr_comm_add_idp_realm(comm, realm);
1092 /* we successfully built a list, add it to the other list */
1093 comms=tr_comm_add(comms, new_comms);
1094 talloc_steal(mem_ctx, comms);
1096 talloc_free(tmp_ctx);
1101 static TR_CFG_RC tr_cfg_parse_one_local_org(TR_CFG *trc, json_t *jlorg)
1103 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1104 TR_CFG_RC retval=TR_CFG_ERROR; /* our return code */
1105 TR_CFG_RC rc=TR_CFG_ERROR; /* return code from functions we call */
1106 TR_NAME *org_name=NULL;
1108 json_t *j_realms=NULL;
1109 TR_IDP_REALM *new_idp_realms=NULL;
1110 TR_RP_CLIENT *new_rp_clients=NULL;
1112 tr_debug("tr_cfg_parse_one_local_org: parsing local organization");
1114 /* get organization_name (optional) */
1115 if (NULL==(j_org=json_object_get(jlorg, "organization_name"))) {
1116 tr_debug("tr_cfg_parse_one_local_org: organization_name unspecified");
1118 org_name=tr_cfg_parse_org_name(tmp_ctx, j_org, &rc);
1119 if (rc==TR_CFG_SUCCESS) {
1120 tr_debug("tr_cfg_parse_one_local_org: organization_name=\"%.*s\"",
1123 /* we don't actually do anything with this, but we could */
1124 tr_free_name(org_name);
1129 /* Now get realms. Allow this to be missing; even though that is a pointless organization entry,
1130 * it's harmless. Report a warning because that might be unintentional. */
1131 if (NULL==(j_realms=json_object_get(jlorg, "realms"))) {
1132 tr_warning("tr_cfg_parse_one_local_org: warning - no realms in this local organization");
1134 /* Allocate in the tmp_ctx so these will be cleaned up if we do not complete successfully. */
1135 new_idp_realms=tr_cfg_parse_idp_realms(tmp_ctx, j_realms, &rc);
1136 if (rc!=TR_CFG_SUCCESS)
1139 new_rp_clients=tr_cfg_parse_rp_clients(tmp_ctx, j_realms, &rc);
1140 if (rc!=TR_CFG_SUCCESS)
1143 retval=TR_CFG_SUCCESS;
1146 /* if we succeeded, link things to the configuration and move out of tmp context */
1147 if (retval==TR_CFG_SUCCESS) {
1148 if (new_idp_realms!=NULL) {
1149 trc->idp_realms=tr_idp_realm_add(trc->idp_realms, new_idp_realms); /* fixes talloc contexts except for head*/
1150 talloc_steal(trc, trc->idp_realms); /* make sure the head is in the right context */
1153 if (new_rp_clients!=NULL) {
1154 trc->rp_clients=tr_rp_client_add(trc->rp_clients, new_rp_clients); /* fixes talloc contexts */
1155 talloc_steal(trc, trc->rp_clients); /* make sure head is in the right context */
1159 talloc_free(tmp_ctx);
1163 /* Parse local organizations if present. Returns success if there are none. On failure, the configuration is unreliable. */
1164 static TR_CFG_RC tr_cfg_parse_local_orgs(TR_CFG *trc, json_t *jcfg)
1166 json_t *jlocorgs=NULL;
1169 jlocorgs=json_object_get(jcfg, "local_organizations");
1171 return TR_CFG_SUCCESS;
1173 if (!json_is_array(jlocorgs)) {
1174 tr_err("tr_cfg_parse_local_orgs: local_organizations is not an array.");
1175 return TR_CFG_NOPARSE;
1178 for (ii=0; ii<json_array_size(jlocorgs); ii++) {
1179 if (tr_cfg_parse_one_local_org(trc, json_array_get(jlocorgs, ii))!=TR_CFG_SUCCESS) {
1180 tr_err("tr_cfg_parse_local_orgs: error parsing local_organization %d.", ii+1);
1181 return TR_CFG_NOPARSE;
1185 return TR_CFG_SUCCESS;
1188 static TR_CFG_RC tr_cfg_parse_one_peer_org(TR_CFG *trc, json_t *jporg)
1190 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1194 TRP_PEER *new_peer=NULL;
1195 TR_GSS_NAMES *names=NULL;
1196 TR_CFG_RC rc=TR_CFG_ERROR;
1198 jhost=json_object_get(jporg, "hostname");
1199 jport=json_object_get(jporg, "port");
1200 jgss=json_object_get(jporg, "gss_names");
1202 if ((jhost==NULL) || (!json_is_string(jhost))) {
1203 tr_err("tr_cfg_parse_one_peer_org: hostname not specified or not a string.");
1208 if ((jport!=NULL) && (!json_is_number(jport))) {
1209 /* note that not specifying the port is allowed, but if set it must be a number */
1210 tr_err("tr_cfg_parse_one_peer_org: port is not a number.");
1215 if ((jgss==NULL) || (!json_is_array(jgss))) {
1216 tr_err("tr_cfg_parse_one_peer_org: gss_names not specified or not an array.");
1221 new_peer=trp_peer_new(tmp_ctx);
1222 if (new_peer==NULL) {
1223 tr_err("tr_cfg_parse_one_peer_org: could not allocate new peer.");
1228 trp_peer_set_server(new_peer, json_string_value(jhost));
1230 trp_peer_set_port(new_peer, TRP_PORT);
1232 trp_peer_set_port(new_peer, json_integer_value(jport));
1234 names=tr_cfg_parse_gss_names(tmp_ctx, jgss, &rc);
1235 if (rc!=TR_CFG_SUCCESS) {
1236 tr_err("tr_cfg_parse_one_peer_org: unable to parse gss names.");
1240 trp_peer_set_gss_names(new_peer, names);
1243 trp_ptable_add(trc->peers, new_peer);
1247 talloc_free(tmp_ctx);
1251 /* Parse peer organizations, if present. Returns success if there are none. */
1252 static TR_CFG_RC tr_cfg_parse_peer_orgs(TR_CFG *trc, json_t *jcfg)
1254 json_t *jpeerorgs=NULL;
1257 jpeerorgs=json_object_get(jcfg, "peer_organizations");
1258 if (jpeerorgs==NULL)
1259 return TR_CFG_SUCCESS;
1261 if (!json_is_array(jpeerorgs)) {
1262 tr_err("tr_cfg_parse_peer_orgs: peer_organizations is not an array.");
1263 return TR_CFG_NOPARSE;
1266 for (ii=0; ii<json_array_size(jpeerorgs); ii++) {
1267 if (tr_cfg_parse_one_peer_org(trc, json_array_get(jpeerorgs, ii))!=TR_CFG_SUCCESS) {
1268 tr_err("tr_cfg_parse_peer_orgs: error parsing peer_organization %d.", ii+1);
1269 return TR_CFG_NOPARSE;
1273 return TR_CFG_SUCCESS;
1276 static TR_CFG_RC tr_cfg_parse_default_servers (TR_CFG *trc, json_t *jcfg)
1278 json_t *jdss = NULL;
1279 TR_CFG_RC rc = TR_CFG_SUCCESS;
1280 TR_AAA_SERVER *ds = NULL;
1283 if ((!trc) || (!jcfg))
1284 return TR_CFG_BAD_PARAMS;
1286 /* If there are default servers, store them */
1287 if ((NULL != (jdss = json_object_get(jcfg, "default_servers"))) &&
1288 (json_is_array(jdss)) &&
1289 (0 < json_array_size(jdss))) {
1291 for (i = 0; i < json_array_size(jdss); i++) {
1292 if (NULL == (ds = tr_cfg_parse_one_aaa_server(trc,
1293 json_array_get(jdss, i),
1297 tr_debug("tr_cfg_parse_default_servers: Default server configured: %s", ds->hostname->buf);
1298 ds->next = trc->default_servers;
1299 trc->default_servers = ds;
1303 tr_debug("tr_cfg_parse_default_servers: Finished (rc=%d)", rc);
1307 static TR_IDP_REALM *tr_cfg_parse_comm_idps (TR_CFG *trc, json_t *jidps, TR_CFG_RC *rc)
1309 TR_IDP_REALM *idp = NULL;
1310 TR_IDP_REALM *found_idp = NULL;
1311 TR_IDP_REALM *temp_idp = NULL;
1316 (!json_is_array(jidps))) {
1318 *rc = TR_CFG_BAD_PARAMS;
1322 for (i = 0; i < json_array_size(jidps); i++) {
1323 if (NULL == (temp_idp = talloc(trc, TR_IDP_REALM))) {
1324 tr_debug("tr_cfg_parse_comm_idps: Can't allocate memory for IdP Realm.");
1329 memset (temp_idp, 0, sizeof(TR_IDP_REALM));
1331 if (NULL == (found_idp = (tr_cfg_find_idp(trc,
1332 tr_new_name((char *)json_string_value(json_array_get(jidps, i))),
1334 tr_debug("tr_cfg_parse_comm_idps: Unknown IDP %s.",
1335 (char *)json_string_value(json_array_get(jidps, i)));
1339 // We *MUST* do a dereferenced copy here or the second community will corrupt the linked list we create here.
1340 *temp_idp = *found_idp;
1342 temp_idp->comm_next = idp;
1349 static TR_RP_REALM *tr_cfg_parse_comm_rps (TR_CFG *trc, json_t *jrps, TR_CFG_RC *rc)
1351 TR_RP_REALM *rp = NULL;
1352 TR_RP_REALM *temp_rp = NULL;
1357 (!json_is_array(jrps))) {
1359 *rc = TR_CFG_BAD_PARAMS;
1363 for (i = (json_array_size(jrps)-1); i >= 0; i--) {
1364 if (NULL == (temp_rp = talloc_zero(trc, TR_RP_REALM))) {
1365 tr_debug("tr_cfg_parse_comm_rps: Can't allocate memory for RP Realm.");
1371 if (NULL == (temp_rp->realm_name = tr_new_name((char *)json_string_value(json_array_get(jrps, i))))) {
1372 tr_debug("tr_cfg_parse_comm_rps: No memory for RP Realm Name.");
1385 static TR_COMM *tr_cfg_parse_one_comm (TR_CFG *trc, json_t *jcomm, TR_CFG_RC *rc) {
1386 TR_COMM *comm = NULL;
1388 json_t *jtype = NULL;
1389 json_t *japcs = NULL;
1390 json_t *jidps = NULL;
1391 json_t *jrps = NULL;
1393 if ((!trc) || (!jcomm) || (!rc)) {
1394 tr_debug("tr_cfg_parse_one_comm: Bad parameters.");
1396 *rc = TR_CFG_BAD_PARAMS;
1400 if (NULL == (comm = talloc_zero(trc, TR_COMM))) {
1401 tr_crit("tr_cfg_parse_one_comm: Out of memory.");
1407 if ((NULL == (jid = json_object_get(jcomm, "community_id"))) ||
1408 (!json_is_string(jid)) ||
1409 (NULL == (jtype = json_object_get(jcomm, "type"))) ||
1410 (!json_is_string(jtype)) ||
1411 (NULL == (japcs = json_object_get(jcomm, "apcs"))) ||
1412 (!json_is_array(japcs)) ||
1413 (NULL == (jidps = json_object_get(jcomm, "idp_realms"))) ||
1414 (!json_is_array(jidps)) ||
1415 (NULL == (jrps = json_object_get(jcomm, "rp_realms"))) ||
1416 (!json_is_array(jrps))) {
1417 tr_debug("tr_cfg_parse_one_comm: Error parsing Communities configuration.");
1418 *rc = TR_CFG_NOPARSE;
1422 if (NULL == (comm->id = tr_new_name((char *)json_string_value(jid)))) {
1423 tr_debug("tr_cfg_parse_one_comm: No memory for community id.");
1428 if (0 == strcmp(json_string_value(jtype), "apc")) {
1429 comm->type = TR_COMM_APC;
1430 } else if (0 == strcmp(json_string_value(jtype), "coi")) {
1431 comm->type = TR_COMM_COI;
1432 if (NULL == (comm->apcs = tr_cfg_parse_apcs(trc, japcs, rc))) {
1433 tr_debug("tr_cfg_parse_one_comm: Can't parse APCs for COI %s.", comm->id->buf);
1434 tr_free_name(comm->id);
1438 tr_debug("tr_cfg_parse_one_comm: Invalid community type, comm = %s, type = %s", comm->id->buf, json_string_value(jtype));
1439 tr_free_name(comm->id);
1440 *rc = TR_CFG_NOPARSE;
1444 comm->idp_realms = tr_cfg_parse_comm_idps(trc, jidps, rc);
1445 if (TR_CFG_SUCCESS != *rc) {
1446 tr_debug("tr_cfg_parse_one_comm: Can't parse IDP realms for comm %s.", comm->id->buf);
1447 tr_free_name(comm->id);
1451 comm->rp_realms = tr_cfg_parse_comm_rps(trc, jrps, rc);
1452 if (TR_CFG_SUCCESS != *rc) {
1453 tr_debug("tr_cfg_parse_comm: Can't parse RP realms for comm %s .", comm->id->buf);
1454 tr_free_name(comm->id);
1458 if (TR_COMM_APC == comm->type) {
1459 json_t *jexpire = json_object_get(jcomm, "expiration_interval");
1460 comm->expiration_interval = 43200; /*30 days*/
1462 if (!json_is_integer(jexpire)) {
1463 fprintf(stderr, "tr_parse_comm: expirae_interval is not an integer\n");
1466 comm->expiration_interval = json_integer_value(jexpire);
1467 if (comm->expiration_interval <= 10)
1468 comm->expiration_interval = 11; /* Freeradius waits 10 minutes between successful TR queries*/
1469 if (comm->expiration_interval > 129600) /* 90 days*/
1470 comm->expiration_interval = 129600;
1477 static TR_CFG_RC tr_cfg_parse_comms (TR_CFG *trc, json_t *jcfg)
1479 json_t *jcomms = NULL;
1480 TR_CFG_RC rc = TR_CFG_SUCCESS;
1481 TR_COMM *comm = NULL;
1484 if ((!trc) || (!jcfg)) {
1485 tr_debug("tr_cfg_parse_comms: Bad Parameters.");
1486 return TR_CFG_BAD_PARAMS;
1489 if (NULL != (jcomms = json_object_get(jcfg, "communities"))) {
1490 if (!json_is_array(jcomms)) {
1491 return TR_CFG_NOPARSE;
1494 for (i = 0; i < json_array_size(jcomms); i++) {
1495 if (NULL == (comm = tr_cfg_parse_one_comm(trc,
1496 json_array_get(jcomms, i),
1500 tr_debug("tr_cfg_parse_comms: Community configured: %s.", comm->id->buf);
1501 comm->next = trc->comms;
1505 tr_debug("tr_cfg_parse_comms: Finished (rc=%d)", rc);
1509 TR_CFG_RC tr_cfg_validate(TR_CFG *trc)
1511 TR_CFG_RC rc = TR_CFG_SUCCESS;
1514 return TR_CFG_BAD_PARAMS;
1516 if ((NULL == trc->internal)||
1517 (NULL == trc->internal->hostname)) {
1518 tr_debug("tr_cfg_validate: Error: No internal configuration, or no hostname.");
1522 if (NULL == trc->rp_clients) {
1523 tr_debug("tr_cfg_validate: Error: No RP Clients configured");
1527 if (NULL == trc->comms) {
1528 tr_debug("tr_cfg_validate: Error: No Communities configured");
1532 if ((NULL == trc->default_servers) && (NULL == trc->idp_realms)) {
1533 tr_debug("tr_cfg_validate: Error: No default servers or IDPs configured.");
1540 /* Join two paths and return a pointer to the result. This should be freed
1541 * via talloc_free. Returns NULL on failure. */
1542 static char *join_paths(TALLOC_CTX *mem_ctx, const char *p1, const char *p2)
1544 return talloc_asprintf(mem_ctx, "%s/%s", p1, p2); /* returns NULL on a failure */
1547 TR_CFG_RC tr_cfg_parse_one_config_file(TR_CFG *cfg, const char *file_with_path)
1553 if (NULL==(jcfg=json_load_file(file_with_path,
1554 JSON_DISABLE_EOF_CHECK, &rc))) {
1555 tr_debug("tr_parse_one_config_file: Error parsing config file %s.",
1557 return TR_CFG_NOPARSE;
1560 // Look for serial number and log it if it exists
1561 if (NULL!=(jser=json_object_get(jcfg, "serial_number"))) {
1562 if (json_is_number(jser)) {
1563 tr_notice("tr_parse_one_config_file: Attempting to load revision %" JSON_INTEGER_FORMAT " of '%s'.",
1564 json_integer_value(jser),
1569 if ((TR_CFG_SUCCESS != tr_cfg_parse_internal(cfg, jcfg)) ||
1570 (TR_CFG_SUCCESS != tr_cfg_parse_local_orgs(cfg, jcfg)) ||
1571 (TR_CFG_SUCCESS != tr_cfg_parse_peer_orgs(cfg, jcfg)) ||
1572 (TR_CFG_SUCCESS != tr_cfg_parse_default_servers(cfg, jcfg)) ||
1573 (TR_CFG_SUCCESS != tr_cfg_parse_comms(cfg, jcfg)))
1574 return TR_CFG_ERROR;
1576 return TR_CFG_SUCCESS;
1579 /* Reads configuration files in config_dir ("" or "./" will use the current directory). */
1580 TR_CFG_RC tr_parse_config(TR_CFG_MGR *cfg_mgr, const char *config_dir, int n, struct dirent **cfg_files)
1582 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1583 char *file_with_path;
1585 TR_CFG_RC cfg_rc=TR_CFG_ERROR;
1587 if ((!cfg_mgr) || (!cfg_files) || (n<=0)) {
1588 cfg_rc=TR_CFG_BAD_PARAMS;
1592 if (cfg_mgr->new != NULL)
1593 tr_cfg_free(cfg_mgr->new);
1594 cfg_mgr->new=tr_cfg_new(tmp_ctx); /* belongs to the temporary context for now */
1595 if (cfg_mgr->new == NULL) {
1596 cfg_rc=TR_CFG_NOMEM;
1600 cfg_mgr->new->peers=trp_ptable_new(cfg_mgr);
1602 /* Parse configuration information from each config file */
1603 for (ii=0; ii<n; ii++) {
1604 file_with_path=join_paths(tmp_ctx, config_dir, cfg_files[ii]->d_name); /* must free result with talloc_free */
1605 if(file_with_path == NULL) {
1606 tr_crit("tr_parse_config: error joining path.");
1607 cfg_rc=TR_CFG_NOMEM;
1610 tr_debug("tr_parse_config: Parsing %s.", cfg_files[ii]->d_name); /* print the filename without the path */
1611 cfg_rc=tr_cfg_parse_one_config_file(cfg_mgr->new, file_with_path);
1612 if (cfg_rc!=TR_CFG_SUCCESS) {
1613 tr_crit("tr_parse_config: Error parsing %s", file_with_path);
1616 talloc_free(file_with_path); /* done with filename */
1619 /* make sure we got a complete, consistent configuration */
1620 if (TR_CFG_SUCCESS != tr_cfg_validate(cfg_mgr->new)) {
1621 tr_err("tr_parse_config: Error: INVALID CONFIGURATION");
1622 cfg_rc=TR_CFG_ERROR;
1627 talloc_steal(cfg_mgr, cfg_mgr->new); /* hand this over to the cfg_mgr context */
1628 cfg_rc=TR_CFG_SUCCESS;
1631 talloc_free(tmp_ctx);
1635 TR_IDP_REALM *tr_cfg_find_idp (TR_CFG *tr_cfg, TR_NAME *idp_id, TR_CFG_RC *rc)
1638 TR_IDP_REALM *cfg_idp;
1640 if ((!tr_cfg) || (!idp_id)) {
1642 *rc = TR_CFG_BAD_PARAMS;
1646 for (cfg_idp = tr_cfg->idp_realms; NULL != cfg_idp; cfg_idp = cfg_idp->next) {
1647 if (!tr_name_cmp (idp_id, cfg_idp->realm_id)) {
1648 tr_debug("tr_cfg_find_idp: Found %s.", idp_id->buf);
1652 /* if we didn't find one, return NULL */
1656 TR_RP_CLIENT *tr_cfg_find_rp (TR_CFG *tr_cfg, TR_NAME *rp_gss, TR_CFG_RC *rc)
1658 TR_RP_CLIENT *cfg_rp;
1660 if ((!tr_cfg) || (!rp_gss)) {
1662 *rc = TR_CFG_BAD_PARAMS;
1666 for (cfg_rp = tr_cfg->rp_clients; NULL != cfg_rp; cfg_rp = cfg_rp->next) {
1667 if (tr_gss_names_matches(cfg_rp->gss_names, rp_gss)) {
1668 tr_debug("tr_cfg_find_rp: Found %s.", rp_gss->buf);
1672 /* if we didn't find one, return NULL */
1676 static int is_cfg_file(const struct dirent *dent) {
1679 /* Only accept filenames ending in ".cfg" and starting with a character
1680 * other than an ASCII '.' */
1682 /* filename must be at least 4 characters long to be acceptable */
1683 n=strlen(dent->d_name);
1688 /* filename must not start with '.' */
1689 if ('.' == dent->d_name[0]) {
1693 /* If the above passed and the last four characters of the filename are .cfg, accept.
1694 * (n.b., assumes an earlier test checked that the name is >= 4 chars long.) */
1695 if (0 == strcmp(&(dent->d_name[n-4]), ".cfg")) {
1699 /* otherwise, return false. */
1703 /* Find configuration files in a particular directory. Returns the
1704 * number of entries found, 0 if none are found, or <0 for some
1705 * errors. If n>=0, the cfg_files parameter will contain a newly
1706 * allocated array of pointers to struct dirent entries, as returned
1707 * by scandir(). These can be freed with tr_free_config_file_list().
1709 int tr_find_config_files (const char *config_dir, struct dirent ***cfg_files) {
1712 n = scandir(config_dir, cfg_files, is_cfg_file, alphasort);
1716 tr_debug("tr_find_config: scandir error trying to scan %s.", config_dir);
1722 /* Free memory allocated for configuration file list returned from tr_find_config_files().
1723 * This can be called regardless of the return value of tr_find_config_values(). */
1724 void tr_free_config_file_list(int n, struct dirent ***cfg_files) {
1727 /* if n < 0, then scandir did not allocate anything because it failed */
1728 if((n>=0) && (*cfg_files != NULL)) {
1729 for(ii=0; ii<n; ii++) {
1730 free((*cfg_files)[ii]);
1732 free(*cfg_files); /* safe even if n==0 */
1733 *cfg_files=NULL; /* this will help prevent accidentally freeing twice */