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 #if JANSSON_VERSION_HEX < 0x020500
53 #include "jansson_iterators.h"
56 void tr_print_config (TR_CFG *cfg) {
57 tr_notice("tr_print_config: Logging running trust router configuration.");
58 tr_print_comms(cfg->ctable);
61 void tr_print_comms (TR_COMM_TABLE *ctab)
65 for (comm = ctab->comms; NULL != comm; comm = comm->next) {
66 tr_notice("tr_print_config: Community %s:", comm->id->buf);
68 tr_notice("tr_print_config: - Member IdPs:");
69 tr_print_comm_idps(ctab, comm);
71 tr_notice("tr_print_config: - Member RPs:");
72 tr_print_comm_rps(ctab, comm);
76 void tr_print_comm_idps(TR_COMM_TABLE *ctab, TR_COMM *comm)
78 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
79 TR_COMM_ITER *iter=NULL;
80 TR_IDP_REALM *idp = NULL;
83 iter=tr_comm_iter_new(tmp_ctx);
85 tr_notice("tr_print_config: unable to allocate IdP iterator.");
90 for (idp=tr_idp_realm_iter_first(iter, ctab, tr_comm_get_id(comm));
92 idp=tr_idp_realm_iter_next(iter)) {
93 s=tr_idp_realm_to_str(tmp_ctx, idp);
95 tr_notice("tr_print_config: - @%s", s);
97 tr_notice("tr_print_config: unable to allocate IdP output string.");
102 void tr_print_comm_rps(TR_COMM_TABLE *ctab, TR_COMM *comm)
104 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
105 TR_COMM_ITER *iter=NULL;
106 TR_RP_REALM *rp = NULL;
109 iter=tr_comm_iter_new(tmp_ctx);
111 tr_notice("tr_print_config: unable to allocate RP iterator.");
112 talloc_free(tmp_ctx);
116 for (rp=tr_rp_realm_iter_first(iter, ctab, tr_comm_get_id(comm));
118 rp=tr_rp_realm_iter_next(iter)) {
119 s=tr_rp_realm_to_str(tmp_ctx, rp);
121 tr_notice("tr_print_config: - @%s", s);
123 tr_notice("tr_print_config: unable to allocate RP output string.");
125 talloc_free(tmp_ctx);
128 TR_CFG *tr_cfg_new(TALLOC_CTX *mem_ctx)
130 TR_CFG *cfg=talloc(mem_ctx, TR_CFG);
133 cfg->rp_clients=NULL;
135 cfg->default_servers=NULL;
136 cfg->ctable=tr_comm_table_new(cfg);
137 if (cfg->ctable==NULL) {
145 void tr_cfg_free (TR_CFG *cfg)
150 TR_CFG_MGR *tr_cfg_mgr_new(TALLOC_CTX *mem_ctx)
152 return talloc_zero(mem_ctx, TR_CFG_MGR);
155 void tr_cfg_mgr_free (TR_CFG_MGR *cfg_mgr) {
156 talloc_free(cfg_mgr);
159 TR_CFG_RC tr_apply_new_config (TR_CFG_MGR *cfg_mgr)
161 /* cfg_mgr->active is allowed to be null, but new cannot be */
162 if ((cfg_mgr==NULL) || (cfg_mgr->new==NULL))
163 return TR_CFG_BAD_PARAMS;
165 if (cfg_mgr->active != NULL)
166 tr_cfg_free(cfg_mgr->active);
168 cfg_mgr->active = cfg_mgr->new;
169 cfg_mgr->new=NULL; /* only keep a single handle on the new configuration */
171 tr_log_threshold(cfg_mgr->active->internal->log_threshold);
172 tr_console_threshold(cfg_mgr->active->internal->console_threshold);
174 return TR_CFG_SUCCESS;
177 static TR_CFG_RC tr_cfg_parse_internal(TR_CFG *trc, json_t *jcfg)
181 json_t *jtidsp = NULL;
182 json_t *jtrpsp = NULL;
183 json_t *jhname = NULL;
185 json_t *jconthres = NULL;
186 json_t *jlogthres = NULL;
187 json_t *jcfgpoll = NULL;
188 json_t *jcfgsettle = NULL;
189 json_t *jroutesweep = NULL;
190 json_t *jrouteupdate = NULL;
191 json_t *jtidreq_timeout = NULL;
192 json_t *jtidresp_numer = NULL;
193 json_t *jtidresp_denom = NULL;
194 json_t *jrouteconnect = NULL;
196 if ((!trc) || (!jcfg))
197 return TR_CFG_BAD_PARAMS;
199 if (NULL == trc->internal) {
200 if (NULL == (trc->internal = talloc_zero(trc, TR_CFG_INTERNAL)))
204 if (NULL != (jint = json_object_get(jcfg, "tr_internal"))) {
205 if (NULL != (jmtd = json_object_get(jint, "max_tree_depth"))) {
206 if (json_is_number(jmtd)) {
207 trc->internal->max_tree_depth = json_integer_value(jmtd);
209 tr_debug("tr_cfg_parse_internal: Parsing error, max_tree_depth is not a number.");
210 return TR_CFG_NOPARSE;
213 /* If not configured, use the default */
214 trc->internal->max_tree_depth = TR_DEFAULT_MAX_TREE_DEPTH;
216 if (NULL != (jtidsp = json_object_get(jint, "tids_port"))) {
217 if (json_is_number(jtidsp)) {
218 trc->internal->tids_port = json_integer_value(jtidsp);
220 tr_debug("tr_cfg_parse_internal: Parsing error, tids_port is not a number.");
221 return TR_CFG_NOPARSE;
224 /* If not configured, use the default */
225 trc->internal->tids_port = TR_DEFAULT_TIDS_PORT;
227 if (NULL != (jtrpsp = json_object_get(jint, "trps_port"))) {
228 if (json_is_number(jtrpsp)) {
229 trc->internal->trps_port = json_integer_value(jtrpsp);
231 tr_debug("tr_cfg_parse_internal: Parsing error, trps_port is not a number.");
232 return TR_CFG_NOPARSE;
235 /* If not configured, use the default */
236 trc->internal->trps_port = TR_DEFAULT_TRPS_PORT;
238 if (NULL != (jhname = json_object_get(jint, "hostname"))) {
239 if (json_is_string(jhname)) {
240 trc->internal->hostname = talloc_strdup(trc->internal, json_string_value(jhname));
242 tr_debug("tr_cfg_parse_internal: Parsing error, hostname is not a string.");
243 return TR_CFG_NOPARSE;
246 if (NULL != (jcfgpoll = json_object_get(jint, "cfg_poll_interval"))) {
247 if (json_is_number(jcfgpoll)) {
248 trc->internal->cfg_poll_interval = json_integer_value(jcfgpoll);
250 tr_debug("tr_cfg_parse_internal: Parsing error, cfg_poll_interval is not a number.");
251 return TR_CFG_NOPARSE;
254 trc->internal->cfg_poll_interval = TR_CFGWATCH_DEFAULT_POLL;
257 if (NULL != (jcfgsettle = json_object_get(jint, "cfg_settling_time"))) {
258 if (json_is_number(jcfgsettle)) {
259 trc->internal->cfg_settling_time = json_integer_value(jcfgsettle);
261 tr_debug("tr_cfg_parse_internal: Parsing error, cfg_settling_time is not a number.");
262 return TR_CFG_NOPARSE;
265 trc->internal->cfg_settling_time = TR_CFGWATCH_DEFAULT_SETTLE;
268 if (NULL != (jrouteconnect = json_object_get(jint, "trp_connect_interval"))) {
269 if (json_is_number(jrouteconnect)) {
270 trc->internal->trp_connect_interval = json_integer_value(jrouteconnect);
272 tr_debug("tr_cfg_parse_internal: Parsing error, trp_connect_interval is not a number.");
273 return TR_CFG_NOPARSE;
276 /* if not configured, use the default */
277 trc->internal->trp_connect_interval=TR_DEFAULT_TRP_CONNECT_INTERVAL;
280 if (NULL != (jroutesweep = json_object_get(jint, "trp_sweep_interval"))) {
281 if (json_is_number(jroutesweep)) {
282 trc->internal->trp_sweep_interval = json_integer_value(jroutesweep);
284 tr_debug("tr_cfg_parse_internal: Parsing error, trp_sweep_interval is not a number.");
285 return TR_CFG_NOPARSE;
288 /* if not configured, use the default */
289 trc->internal->trp_sweep_interval=TR_DEFAULT_TRP_SWEEP_INTERVAL;
292 if (NULL != (jrouteupdate = json_object_get(jint, "trp_update_interval"))) {
293 if (json_is_number(jrouteupdate)) {
294 trc->internal->trp_update_interval = json_integer_value(jrouteupdate);
296 tr_debug("tr_cfg_parse_internal: Parsing error, trp_update_interval is not a number.");
297 return TR_CFG_NOPARSE;
300 /* if not configured, use the default */
301 trc->internal->trp_update_interval=TR_DEFAULT_TRP_UPDATE_INTERVAL;
304 if (NULL != (jtidreq_timeout = json_object_get(jint, "tid_request_timeout"))) {
305 if (json_is_number(jtidreq_timeout)) {
306 trc->internal->tid_req_timeout = json_integer_value(jtidreq_timeout);
308 tr_debug("tr_cfg_parse_internal: Parsing error, tid_request_timeout is not a number.");
309 return TR_CFG_NOPARSE;
312 /* if not configured, use the default */
313 trc->internal->tid_req_timeout=TR_DEFAULT_TID_REQ_TIMEOUT;
316 if (NULL != (jtidresp_numer = json_object_get(jint, "tid_response_numerator"))) {
317 if (json_is_number(jtidresp_numer)) {
318 trc->internal->tid_resp_numer = json_integer_value(jtidresp_numer);
320 tr_debug("tr_cfg_parse_internal: Parsing error, tid_response_numerator is not a number.");
321 return TR_CFG_NOPARSE;
324 /* if not configured, use the default */
325 trc->internal->tid_resp_numer=TR_DEFAULT_TID_RESP_NUMER;
328 if (NULL != (jtidresp_denom = json_object_get(jint, "tid_response_denominator"))) {
329 if (json_is_number(jtidresp_denom)) {
330 trc->internal->tid_resp_denom = json_integer_value(jtidresp_denom);
332 tr_debug("tr_cfg_parse_internal: Parsing error, tid_response_denominator is not a number.");
333 return TR_CFG_NOPARSE;
336 /* if not configured, use the default */
337 trc->internal->tid_resp_denom=TR_DEFAULT_TID_RESP_DENOM;
340 if (NULL != (jlog = json_object_get(jint, "logging"))) {
341 if (NULL != (jlogthres = json_object_get(jlog, "log_threshold"))) {
342 if (json_is_string(jlogthres)) {
343 trc->internal->log_threshold = str2sev(json_string_value(jlogthres));
345 tr_debug("tr_cfg_parse_internal: Parsing error, log_threshold is not a string.");
346 return TR_CFG_NOPARSE;
349 /* If not configured, use the default */
350 trc->internal->log_threshold = TR_DEFAULT_LOG_THRESHOLD;
353 if (NULL != (jconthres = json_object_get(jlog, "console_threshold"))) {
354 if (json_is_string(jconthres)) {
355 trc->internal->console_threshold = str2sev(json_string_value(jconthres));
357 tr_debug("tr_cfg_parse_internal: Parsing error, console_threshold is not a string.");
358 return TR_CFG_NOPARSE;
361 /* If not configured, use the default */
362 trc->internal->console_threshold = TR_DEFAULT_CONSOLE_THRESHOLD;
365 /* If not configured, use the default */
366 trc->internal->console_threshold = TR_DEFAULT_CONSOLE_THRESHOLD;
367 trc->internal->log_threshold = TR_DEFAULT_LOG_THRESHOLD;
370 tr_debug("tr_cfg_parse_internal: Internal config parsed.");
371 return TR_CFG_SUCCESS;
373 return TR_CFG_SUCCESS;
376 static TR_CONSTRAINT *tr_cfg_parse_one_constraint(TALLOC_CTX *mem_ctx, char *ctype, json_t *jc, TR_CFG_RC *rc)
378 TR_CONSTRAINT *cons=NULL;
381 if ((!ctype) || (!jc) || (!rc) ||
382 (!json_is_array(jc)) ||
383 (0 >= json_array_size(jc)) ||
384 (TR_MAX_CONST_MATCHES < json_array_size(jc)) ||
385 (!json_is_string(json_array_get(jc, 0)))) {
386 tr_err("tr_cfg_parse_one_constraint: config error.");
391 if (NULL==(cons=tr_constraint_new(mem_ctx))) {
392 tr_debug("tr_cfg_parse_one_constraint: Out of memory (cons).");
397 if (NULL==(cons->type=tr_new_name(ctype))) {
398 tr_err("tr_cfg_parse_one_constraint: Out of memory (type).");
400 tr_constraint_free(cons);
404 for (i=0; i < json_array_size(jc); i++) {
405 cons->matches[i]=tr_new_name(json_string_value(json_array_get(jc, i)));
406 if (cons->matches[i]==NULL) {
407 tr_err("tr_cfg_parse_one_constraint: Out of memory (match %d).", i+1);
409 tr_constraint_free(cons);
417 static TR_FILTER *tr_cfg_parse_one_filter(TALLOC_CTX *mem_ctx, json_t *jfilt, TR_FILTER_TYPE ftype, TR_CFG_RC *rc)
419 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
420 TR_FILTER *filt = NULL;
421 json_t *jfaction = NULL;
422 json_t *jfline = NULL;
423 json_t *jfspecs = NULL;
424 json_t *this_jfspec = NULL;
425 json_t *jfield = NULL;
426 json_t *jmatch = NULL;
429 json_t *this_jmatch = NULL;
430 TR_NAME *name = NULL;
431 size_t i = 0, j = 0, k = 0;
435 if ((jfilt == NULL) || (rc == NULL)) {
436 tr_err("tr_cfg_parse_one_filter: null argument");
437 *rc = TR_CFG_BAD_PARAMS;
441 if (NULL == (filt = tr_filter_new(tmp_ctx))) {
442 tr_err("tr_cfg_parse_one_filter: Out of memory.");
446 tr_filter_set_type(filt, ftype);
448 /* make sure we have space to represent the filter */
449 if (json_array_size(jfilt) > TR_MAX_FILTER_LINES) {
450 tr_err("tr_cfg_parse_one_filter: Filter has too many lines, maximum of %d.", TR_MAX_FILTER_LINES);
451 *rc = TR_CFG_NOPARSE;
455 /* For each entry in the filter... */
456 json_array_foreach(jfilt, i, jfline) {
457 if ((NULL == (jfaction = json_object_get(jfline, "action"))) ||
458 (!json_is_string(jfaction))) {
459 tr_debug("tr_cfg_parse_one_filter: Error parsing filter action.");
460 *rc = TR_CFG_NOPARSE;
464 if ((NULL == (jfspecs = json_object_get(jfline, "specs"))) ||
465 (!json_is_array(jfspecs)) ||
466 (0 == json_array_size(jfspecs))) {
467 tr_debug("tr_cfg_parse_one_filter: Error parsing filter specs.");
468 *rc = TR_CFG_NOPARSE;
472 if (TR_MAX_FILTER_SPECS < json_array_size(jfspecs)) {
473 tr_debug("tr_cfg_parse_one_filter: Filter has too many specs, maximimum of %d.", TR_MAX_FILTER_SPECS);
474 *rc = TR_CFG_NOPARSE;
478 if (NULL == (filt->lines[i] = tr_fline_new(filt))) {
479 tr_debug("tr_cfg_parse_one_filter: Out of memory allocating filter line %d.", i + 1);
484 if (!strcmp(json_string_value(jfaction), "accept")) {
485 filt->lines[i]->action = TR_FILTER_ACTION_ACCEPT;
486 } else if (!strcmp(json_string_value(jfaction), "reject")) {
487 filt->lines[i]->action = TR_FILTER_ACTION_REJECT;
489 tr_debug("tr_cfg_parse_one_filter: Error parsing filter action, unknown action' %s'.",
490 json_string_value(jfaction));
491 *rc = TR_CFG_NOPARSE;
495 if (NULL != (jrc = json_object_get(jfline, "realm_constraints"))) {
496 if (!json_is_array(jrc)) {
497 tr_err("tr_cfg_parse_one_filter: cannot parse realm_constraints, not an array.");
498 *rc = TR_CFG_NOPARSE;
500 } else if (json_array_size(jrc) > TR_MAX_CONST_MATCHES) {
501 tr_err("tr_cfg_parse_one_filter: realm_constraints has too many entries, maximum of %d.",
502 TR_MAX_CONST_MATCHES);
503 *rc = TR_CFG_NOPARSE;
505 } else if (json_array_size(jrc) > 0) {
506 /* ok we actually have entries to process */
507 if (NULL == (filt->lines[i]->realm_cons = tr_cfg_parse_one_constraint(filt->lines[i], "realm", jrc, rc))) {
508 tr_debug("tr_cfg_parse_one_filter: Error parsing realm constraint");
509 *rc = TR_CFG_NOPARSE;
515 if (NULL != (jdc = json_object_get(jfline, "domain_constraints"))) {
516 if (!json_is_array(jdc)) {
517 tr_err("tr_cfg_parse_one_filter: cannot parse domain_constraints, not an array.");
518 *rc = TR_CFG_NOPARSE;
520 } else if (json_array_size(jdc) > TR_MAX_CONST_MATCHES) {
521 tr_err("tr_cfg_parse_one_filter: domain_constraints has too many entries, maximum of %d.",
522 TR_MAX_CONST_MATCHES);
523 *rc = TR_CFG_NOPARSE;
525 } else if (json_array_size(jdc) > 0) {
526 if (NULL == (filt->lines[i]->domain_cons = tr_cfg_parse_one_constraint(filt->lines[i], "domain", jdc, rc))) {
527 tr_debug("tr_cfg_parse_one_filter: Error parsing domain constraint");
528 *rc = TR_CFG_NOPARSE;
534 /*For each filter spec within the filter line... */
535 json_array_foreach(jfspecs, j, this_jfspec) {
536 if ((NULL == (jfield = json_object_get(this_jfspec, "field"))) ||
537 (!json_is_string(jfield))) {
538 tr_debug("tr_cfg_parse_one_filter: Error parsing filter: missing field for filer spec %d, filter line %d.", i,
540 *rc = TR_CFG_NOPARSE;
544 /* check that we have a match attribute */
545 if (NULL == (jmatch = json_object_get(this_jfspec, "match"))) {
546 tr_debug("tr_cfg_parse_one_filter: Error parsing filter: missing match for filer spec %d, filter line %d.", i,
548 *rc = TR_CFG_NOPARSE;
552 /* check that match is a string or an array */
553 if ((!json_is_string(jmatch)) && (!json_is_array(jmatch))) {
555 "tr_cfg_parse_one_filter: Error parsing filter: match not a string or array for filter spec %d, filter line %d.",
557 *rc = TR_CFG_NOPARSE;
561 /* allocate the filter spec */
562 if (NULL == (filt->lines[i]->specs[j] = tr_fspec_new(filt->lines[i]))) {
563 tr_debug("tr_cfg_parse_one_filter: Out of memory.");
568 /* fill in the field */
569 if (NULL == (filt->lines[i]->specs[j]->field = tr_new_name(json_string_value(jfield)))) {
570 tr_debug("tr_cfg_parse_one_filter: Out of memory.");
575 /* fill in the matches */
576 if (json_is_string(jmatch)) {
577 if (NULL == (name = tr_new_name(json_string_value(jmatch)))) {
578 tr_debug("tr_cfg_parse_one_filter: Out of memory.");
582 tr_fspec_add_match(filt->lines[i]->specs[j], name);
584 /* jmatch is an array (we checked earlier) */
585 json_array_foreach(jmatch, k, this_jmatch) {
586 if (NULL == (name = tr_new_name(json_string_value(this_jmatch)))) {
587 tr_debug("tr_cfg_parse_one_filter: Out of memory.");
591 tr_fspec_add_match(filt->lines[i]->specs[j], name);
594 if (!tr_filter_validate_spec_field(ftype, filt->lines[i]->specs[j])){
595 tr_debug("tr_cfg_parse_one_filter: Invalid filter field \"%.*s\" for %s filter, spec %d, filter %d.",
596 filt->lines[i]->specs[j]->field->len,
597 filt->lines[i]->specs[j]->field->buf,
598 tr_filter_type_to_string(filt->type),
606 /* check that the filter is valid */
607 if (!tr_filter_validate(filt)) {
610 *rc = TR_CFG_SUCCESS;
611 talloc_steal(mem_ctx, filt);
615 talloc_free(tmp_ctx);
616 if (*rc!=TR_CFG_SUCCESS)
621 static TR_FILTER_SET *tr_cfg_parse_filters(TALLOC_CTX *mem_ctx, json_t *jfilts, TR_CFG_RC *rc)
623 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
625 const char *filt_label=NULL;
626 TR_FILTER *filt=NULL;
627 TR_FILTER_SET *filt_set=NULL;
628 TR_FILTER_TYPE filt_type=TR_FILTER_TYPE_UNKNOWN;
638 filt_set=tr_filter_set_new(tmp_ctx);
639 if (filt_set==NULL) {
640 tr_debug("tr_cfg_parse_filters: Unable to allocate filter set.");
645 json_object_foreach(jfilts, filt_label, jfilt) {
646 /* check that we got a filter */
648 tr_debug("tr_cfg_parse_filters: Definition for %s filter is missing.", filt_label);
649 *rc = TR_CFG_NOPARSE;
653 /* check that we recognize the filter type */
654 filt_type=tr_filter_type_from_string(filt_label);
655 if (filt_type==TR_FILTER_TYPE_UNKNOWN) {
656 tr_debug("tr_cfg_parse_filters: Unrecognized filter (%s) defined.", filt_label);
657 *rc = TR_CFG_NOPARSE;
661 /* finally, parse the filter */
662 tr_debug("tr_cfg_parse_filters: Found %s filter.", filt_label);
663 filt = tr_cfg_parse_one_filter(tmp_ctx, jfilt, filt_type, rc);
664 tr_filter_set_add(filt_set, filt);
665 if (*rc != TR_CFG_SUCCESS) {
666 tr_debug("tr_cfg_parse_filters: Error parsing %s filter.", filt_label);
667 *rc = TR_CFG_NOPARSE;
675 if (*rc==TR_CFG_SUCCESS)
676 talloc_steal(mem_ctx, filt_set);
677 else if (filt_set!=NULL) {
678 talloc_free(filt_set);
682 talloc_free(tmp_ctx);
686 static TR_AAA_SERVER *tr_cfg_parse_one_aaa_server(TALLOC_CTX *mem_ctx, json_t *jaddr, TR_CFG_RC *rc)
688 TR_AAA_SERVER *aaa = NULL;
691 if ((!jaddr) || (!json_is_string(jaddr))) {
692 tr_debug("tr_cfg_parse_one_aaa_server: Bad parameters.");
693 *rc = TR_CFG_BAD_PARAMS;
697 name=tr_new_name(json_string_value(jaddr));
699 tr_debug("tr_cfg_parse_one_aaa_server: Out of memory allocating hostname.");
704 aaa=tr_aaa_server_new(mem_ctx, name);
707 tr_debug("tr_cfg_parse_one_aaa_server: Out of memory allocating AAA server.");
715 static TR_AAA_SERVER *tr_cfg_parse_aaa_servers(TALLOC_CTX *mem_ctx, json_t *jaaas, TR_CFG_RC *rc)
717 TALLOC_CTX *tmp_ctx=NULL;
718 TR_AAA_SERVER *aaa = NULL;
719 TR_AAA_SERVER *temp_aaa = NULL;
722 for (i = 0; i < json_array_size(jaaas); i++) {
723 /* rc gets set in here */
724 if (NULL == (temp_aaa = tr_cfg_parse_one_aaa_server(tmp_ctx, json_array_get(jaaas, i), rc))) {
725 talloc_free(tmp_ctx);
728 /* TBD -- IPv6 addresses */
729 // tr_debug("tr_cfg_parse_aaa_servers: Configuring AAA Server: ip_addr = %s.", inet_ntoa(temp_aaa->aaa_server_addr));
730 temp_aaa->next = aaa;
733 tr_debug("tr_cfg_parse_aaa_servers: Finished (rc=%d)", *rc);
735 for (temp_aaa=aaa; temp_aaa!=NULL; temp_aaa=temp_aaa->next)
736 talloc_steal(mem_ctx, temp_aaa);
737 talloc_free(tmp_ctx);
741 static TR_APC *tr_cfg_parse_one_apc(TALLOC_CTX *mem_ctx, json_t *japc, TR_CFG_RC *rc)
746 *rc = TR_CFG_SUCCESS; /* presume success */
748 if ((!japc) || (!rc) || (!json_is_string(japc))) {
749 tr_debug("tr_cfg_parse_one_apc: Bad parameters.");
751 *rc = TR_CFG_BAD_PARAMS;
755 apc=tr_apc_new(mem_ctx);
757 tr_debug("tr_cfg_parse_one_apc: Out of memory.");
762 name=tr_new_name(json_string_value(japc));
764 tr_debug("tr_cfg_parse_one_apc: No memory for APC name.");
769 tr_apc_set_id(apc, name); /* apc is now responsible for freeing the name */
774 static TR_APC *tr_cfg_parse_apcs(TALLOC_CTX *mem_ctx, json_t *japcs, TR_CFG_RC *rc)
776 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
778 TR_APC *new_apc=NULL;
780 TR_CFG_RC call_rc=TR_CFG_ERROR;
782 *rc = TR_CFG_SUCCESS; /* presume success */
784 if ((!japcs) || (!rc) || (!json_is_array(japcs))) {
785 tr_debug("tr_cfg_parse_one_apc: Bad parameters.");
787 *rc = TR_CFG_BAD_PARAMS;
791 for (ii=0; ii<json_array_size(japcs); ii++) {
792 new_apc=tr_cfg_parse_one_apc(tmp_ctx, json_array_get(japcs, ii), &call_rc);
793 if ((call_rc!=TR_CFG_SUCCESS) || (new_apc==NULL)) {
794 tr_debug("tr_cfg_parse_apcs: Error parsing APC %d.", ii+1);
798 tr_apc_add(apcs, new_apc);
801 talloc_steal(mem_ctx, apcs);
805 talloc_free(tmp_ctx);
809 static TR_NAME *tr_cfg_parse_name(TALLOC_CTX *mem_ctx, json_t *jname, TR_CFG_RC *rc)
814 if ((jname==NULL) || (!json_is_string(jname))) {
815 tr_err("tr_cfg_parse_name: name missing or not a string");
816 *rc=TR_CFG_BAD_PARAMS;
819 name=tr_new_name(json_string_value(jname));
821 tr_err("tr_cfg_parse_name: could not allocate name");
830 static int tr_cfg_parse_shared_config(json_t *jsc, TR_CFG_RC *rc)
832 const char *shared=NULL;
836 (!json_is_string(jsc)) ||
837 (NULL==(shared=json_string_value(jsc)))) {
838 *rc=TR_CFG_BAD_PARAMS;
842 if (0==strcmp(shared, "no"))
844 else if (0==strcmp(shared, "yes"))
847 /* any other value is an error */
848 tr_debug("tr_cfg_parse_shared_config: invalid shared_config value \"%s\" (should be \"yes\" or \"no\")",
854 static TR_REALM_ORIGIN tr_cfg_realm_origin(json_t *jrealm)
856 json_t *jremote=json_object_get(jrealm, "remote");
860 return TR_REALM_LOCAL;
861 if (!json_is_string(jremote)) {
862 tr_warning("tr_cfg_realm_origin: \"remote\" is not a string, assuming this is a local realm.");
863 return TR_REALM_LOCAL;
865 s=json_string_value(jremote);
866 if (strcasecmp(s, "yes")==0)
867 return TR_REALM_REMOTE_INCOMPLETE;
868 else if (strcasecmp(s, "no")!=0)
869 tr_warning("tr_cfg_realm_origin: \"remote\" is neither 'yes' nor 'no', assuming this is a local realm.");
871 return TR_REALM_LOCAL;
874 /* Parse the identity provider object from a realm and fill in the given TR_IDP_REALM. */
875 static TR_CFG_RC tr_cfg_parse_idp(TR_IDP_REALM *idp, json_t *jidp)
877 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
879 TR_AAA_SERVER *aaa=NULL;
880 TR_CFG_RC rc=TR_CFG_ERROR;
885 idp->shared_config=tr_cfg_parse_shared_config(json_object_get(jidp, "shared_config"), &rc);
886 if (rc!=TR_CFG_SUCCESS) {
887 tr_err("tr_cfg_parse_idp: missing or malformed shared_config specification");
892 apcs=tr_cfg_parse_apcs(tmp_ctx, json_object_get(jidp, "apcs"), &rc);
893 if ((rc!=TR_CFG_SUCCESS) || (apcs==NULL)) {
894 tr_err("tr_cfg_parse_idp: unable to parse APC");
899 aaa=tr_cfg_parse_aaa_servers(idp, json_object_get(jidp, "aaa_servers"), &rc);
900 if (rc!=TR_CFG_SUCCESS) {
901 tr_err("tr_cfg_parse_idp: unable to parse AAA servers");
906 tr_debug("tr_cfg_parse_idp: APC=\"%.*s\"",
910 /* done, fill in the idp structures */
912 talloc_steal(idp, apcs);
913 idp->aaa_servers=aaa;
917 if (rc!=TR_CFG_SUCCESS) {
921 tr_aaa_server_free(aaa);
924 talloc_free(tmp_ctx);
928 /* parses idp realm */
929 static TR_IDP_REALM *tr_cfg_parse_one_idp_realm(TALLOC_CTX *mem_ctx, json_t *jrealm, TR_CFG_RC *rc)
931 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
932 TR_IDP_REALM *realm=NULL;
933 TR_CFG_RC call_rc=TR_CFG_ERROR;
935 *rc=TR_CFG_ERROR; /* default to error if not set */
937 if ((!jrealm) || (!rc)) {
938 tr_err("tr_cfg_parse_one_idp_realm: Bad parameters.");
940 *rc=TR_CFG_BAD_PARAMS;
944 if (NULL==(realm=tr_idp_realm_new(tmp_ctx))) {
945 tr_err("tr_cfg_parse_one_idp_realm: could not allocate idp realm.");
950 realm->origin=tr_cfg_realm_origin(jrealm);
951 if (realm->origin!=TR_REALM_LOCAL) {
952 tr_debug("tr_cfg_parse_one_idp_realm: realm is remote, should not have full IdP info.");
957 /* must have a name */
958 realm->realm_id=tr_cfg_parse_name(realm,
959 json_object_get(jrealm, "realm"),
961 if ((call_rc!=TR_CFG_SUCCESS) || (realm->realm_id==NULL)) {
962 tr_err("tr_cfg_parse_one_idp_realm: could not parse realm name");
966 tr_debug("tr_cfg_parse_one_idp_realm: realm_id=\"%.*s\"",
967 realm->realm_id->len,
968 realm->realm_id->buf);
970 call_rc=tr_cfg_parse_idp(realm, json_object_get(jrealm, "identity_provider"));
971 if (call_rc!=TR_CFG_SUCCESS) {
972 tr_err("tr_cfg_parse_one_idp_realm: could not parse identity_provider.");
980 if (*rc==TR_CFG_SUCCESS)
981 talloc_steal(mem_ctx, realm);
987 talloc_free(tmp_ctx);
991 /* Determine whether the realm is an IDP realm */
992 static int tr_cfg_is_idp_realm(json_t *jrealm)
994 /* If a realm spec contains an identity_provider, it's an IDP realm. */
995 if (NULL != json_object_get(jrealm, "identity_provider"))
1001 static TR_IDP_REALM *tr_cfg_parse_one_remote_realm(TALLOC_CTX *mem_ctx, json_t *jrealm, TR_CFG_RC *rc)
1003 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1004 TR_IDP_REALM *realm=talloc(mem_ctx, TR_IDP_REALM);
1006 *rc=TR_CFG_ERROR; /* default to error if not set */
1008 if ((!jrealm) || (!rc)) {
1009 tr_err("tr_cfg_parse_one_remote_realm: Bad parameters.");
1011 *rc=TR_CFG_BAD_PARAMS;
1015 if (NULL==(realm=tr_idp_realm_new(tmp_ctx))) {
1016 tr_err("tr_cfg_parse_one_remote_realm: could not allocate idp realm.");
1021 /* must have a name */
1022 realm->realm_id=tr_cfg_parse_name(realm,
1023 json_object_get(jrealm, "realm"),
1025 if ((*rc!=TR_CFG_SUCCESS) || (realm->realm_id==NULL)) {
1026 tr_err("tr_cfg_parse_one_remote_realm: could not parse realm name");
1030 tr_debug("tr_cfg_parse_one_remote_realm: realm_id=\"%.*s\"",
1031 realm->realm_id->len,
1032 realm->realm_id->buf);
1034 realm->origin=tr_cfg_realm_origin(jrealm);
1038 if (*rc==TR_CFG_SUCCESS)
1039 talloc_steal(mem_ctx, realm);
1045 talloc_free(tmp_ctx);
1049 static int tr_cfg_is_remote_realm(json_t *jrealm)
1051 return (tr_cfg_realm_origin(jrealm)!=TR_REALM_LOCAL);
1054 /* Parse any idp realms in the j_realms object. Ignores other realm types. */
1055 static TR_IDP_REALM *tr_cfg_parse_idp_realms(TALLOC_CTX *mem_ctx, json_t *jrealms, TR_CFG_RC *rc)
1057 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1058 TR_IDP_REALM *realms=NULL;
1059 TR_IDP_REALM *new_realm=NULL;
1060 json_t *this_jrealm=NULL;
1064 if ((jrealms==NULL) || (!json_is_array(jrealms))) {
1065 tr_err("tr_cfg_parse_idp_realms: realms not an array");
1066 *rc=TR_CFG_BAD_PARAMS;
1070 for (ii=0; ii<json_array_size(jrealms); ii++) {
1071 this_jrealm=json_array_get(jrealms, ii);
1072 if (tr_cfg_is_idp_realm(this_jrealm)) {
1073 new_realm=tr_cfg_parse_one_idp_realm(tmp_ctx, this_jrealm, rc);
1074 if ((*rc)!=TR_CFG_SUCCESS) {
1075 tr_err("tr_cfg_parse_idp_realms: error decoding realm entry %d", ii+1);
1079 tr_idp_realm_add(realms, new_realm);
1080 } else if (tr_cfg_is_remote_realm(this_jrealm)) {
1081 new_realm=tr_cfg_parse_one_remote_realm(tmp_ctx, this_jrealm, rc);
1082 if ((*rc)!=TR_CFG_SUCCESS) {
1083 tr_err("tr_cfg_parse_idp_realms: error decoding remote realm entry %d", ii+1);
1087 tr_idp_realm_add(realms, new_realm);
1092 talloc_steal(mem_ctx, realms);
1095 talloc_free(tmp_ctx);
1099 static TR_GSS_NAMES *tr_cfg_parse_gss_names(TALLOC_CTX *mem_ctx, json_t *jgss_names, TR_CFG_RC *rc)
1101 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1102 TR_GSS_NAMES *gn=NULL;
1107 if ((rc==NULL) || (jgss_names==NULL)) {
1108 tr_err("tr_cfg_parse_gss_names: Bad parameters.");
1109 *rc=TR_CFG_BAD_PARAMS;
1113 if (!json_is_array(jgss_names)) {
1114 tr_err("tr_cfg_parse_gss_names: gss_names not an array.");
1119 gn=tr_gss_names_new(tmp_ctx);
1120 for (ii=0; ii<json_array_size(jgss_names); ii++) {
1121 jname=json_array_get(jgss_names, ii);
1122 if (!json_is_string(jname)) {
1123 tr_err("tr_cfg_parse_gss_names: Encountered non-string gss name.");
1128 name=tr_new_name(json_string_value(jname));
1130 tr_err("tr_cfg_parse_gss_names: Out of memory allocating gss name.");
1135 if (tr_gss_names_add(gn, name)!=0) {
1137 tr_err("tr_cfg_parse_gss_names: Unable to add gss name to RP client.");
1143 talloc_steal(mem_ctx, gn);
1147 talloc_free(tmp_ctx);
1148 if ((*rc!=TR_CFG_SUCCESS) && (gn!=NULL))
1153 /* default filter accepts realm and *.realm */
1154 static TR_FILTER_SET *tr_cfg_default_filters(TALLOC_CTX *mem_ctx, TR_NAME *realm, TR_CFG_RC *rc)
1156 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1157 TR_FILTER *filt=NULL;
1158 TR_FILTER_SET *filt_set=NULL;
1159 TR_CONSTRAINT *cons=NULL;
1161 TR_NAME *n_prefix=tr_new_name("*.");
1162 TR_NAME *n_rp_realm_1=tr_new_name("rp_realm");
1163 TR_NAME *n_rp_realm_2=tr_new_name("rp_realm");
1164 TR_NAME *n_domain=tr_new_name("domain");
1165 TR_NAME *n_realm=tr_new_name("realm");
1168 if ((realm==NULL) || (rc==NULL)) {
1169 tr_debug("tr_cfg_default_filters: invalid arguments.");
1171 *rc=TR_CFG_BAD_PARAMS;
1175 if ((n_prefix==NULL) ||
1176 (n_rp_realm_1==NULL) ||
1177 (n_rp_realm_2==NULL) ||
1180 tr_debug("tr_cfg_default_filters: unable to allocate names.");
1185 filt=tr_filter_new(tmp_ctx);
1187 tr_debug("tr_cfg_default_filters: could not allocate filter.");
1191 tr_filter_set_type(filt, TR_FILTER_TYPE_TID_INBOUND);
1192 filt->lines[0]=tr_fline_new(filt);
1193 if (filt->lines[0]==NULL) {
1194 tr_debug("tr_cfg_default_filters: could not allocate filter line.");
1199 filt->lines[0]->action=TR_FILTER_ACTION_ACCEPT;
1200 filt->lines[0]->specs[0]=tr_fspec_new(filt->lines[0]);
1201 filt->lines[0]->specs[0]->field=n_rp_realm_1;
1202 n_rp_realm_1=NULL; /* we don't own this name any more */
1204 name=tr_dup_name(realm);
1206 tr_debug("tr_cfg_default_filters: could not allocate realm name.");
1210 tr_fspec_add_match(filt->lines[0]->specs[0], name);
1211 name=NULL; /* we no longer own the name */
1213 /* now do the wildcard name */
1214 filt->lines[0]->specs[1]=tr_fspec_new(filt->lines[0]);
1215 filt->lines[0]->specs[1]->field=n_rp_realm_2;
1216 n_rp_realm_2=NULL; /* we don't own this name any more */
1218 if (NULL==(name=tr_name_cat(n_prefix, realm))) {
1219 tr_debug("tr_cfg_default_filters: could not allocate wildcard realm name.");
1224 tr_fspec_add_match(filt->lines[0]->specs[1], name);
1225 name=NULL; /* we no longer own the name */
1227 /* domain constraint */
1228 if (NULL==(cons=tr_constraint_new(filt->lines[0]))) {
1229 tr_debug("tr_cfg_default_filters: could not allocate domain constraint.");
1234 cons->type=n_domain;
1235 n_domain=NULL; /* belongs to the constraint now */
1236 name=tr_dup_name(realm);
1238 tr_debug("tr_cfg_default_filters: could not allocate realm name for domain constraint.");
1242 cons->matches[0]=name;
1243 name=tr_name_cat(n_prefix, realm);
1245 tr_debug("tr_cfg_default_filters: could not allocate wildcard realm name for domain constraint.");
1249 cons->matches[1]=name;
1251 filt->lines[0]->domain_cons=cons;
1254 /* realm constraint */
1255 if (NULL==(cons=tr_constraint_new(filt->lines[0]))) {
1256 tr_debug("tr_cfg_default_filters: could not allocate realm constraint.");
1262 n_realm=NULL; /* belongs to the constraint now */
1263 name=tr_dup_name(realm);
1265 tr_debug("tr_cfg_default_filters: could not allocate realm name for realm constraint.");
1269 cons->matches[0]=name;
1270 name=tr_name_cat(n_prefix, realm);
1272 tr_debug("tr_cfg_default_filters: could not allocate wildcard realm name for realm constraint.");
1276 cons->matches[1]=name;
1278 filt->lines[0]->realm_cons=cons;
1280 /* put the filter in a set */
1281 filt_set=tr_filter_set_new(tmp_ctx);
1282 if ((filt_set==NULL)||(0!=tr_filter_set_add(filt_set, filt))) {
1283 tr_debug("tr_cfg_default_filters: could not allocate filter set.");
1287 talloc_steal(mem_ctx, filt_set);
1290 talloc_free(tmp_ctx);
1292 if (*rc!=TR_CFG_SUCCESS)
1296 tr_free_name(n_prefix);
1297 if (n_rp_realm_1!=NULL)
1298 tr_free_name(n_rp_realm_1);
1299 if (n_rp_realm_2!=NULL)
1300 tr_free_name(n_rp_realm_2);
1302 tr_free_name(n_realm);
1304 tr_free_name(n_domain);
1311 /* parses rp client */
1312 static TR_RP_CLIENT *tr_cfg_parse_one_rp_client(TALLOC_CTX *mem_ctx, json_t *jrealm, TR_CFG_RC *rc)
1314 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1315 TR_RP_CLIENT *client=NULL;
1316 TR_CFG_RC call_rc=TR_CFG_ERROR;
1317 TR_FILTER_SET *new_filts=NULL;
1318 TR_NAME *realm=NULL;
1320 json_t *jrealm_id=NULL;
1322 *rc=TR_CFG_ERROR; /* default to error if not set */
1324 if ((!jrealm) || (!rc)) {
1325 tr_err("tr_cfg_parse_one_rp_client: Bad parameters.");
1327 *rc=TR_CFG_BAD_PARAMS;
1331 if ((NULL==(jrealm_id=json_object_get(jrealm, "realm"))) || (!json_is_string(jrealm_id))) {
1332 tr_debug("tr_cfg_parse_one_rp_client: no realm ID found.");
1333 *rc=TR_CFG_BAD_PARAMS;
1337 tr_debug("tr_cfg_parse_one_rp_client: realm_id=\"%s\"", json_string_value(jrealm_id));
1338 realm=tr_new_name(json_string_value(jrealm_id));
1340 tr_err("tr_cfg_parse_one_rp_client: could not allocate realm ID.");
1345 if (NULL==(client=tr_rp_client_new(tmp_ctx))) {
1346 tr_err("tr_cfg_parse_one_rp_client: could not allocate rp client.");
1351 client->gss_names=tr_cfg_parse_gss_names(client, json_object_get(jrealm, "gss_names"), &call_rc);
1353 if (call_rc!=TR_CFG_SUCCESS) {
1354 tr_err("tr_cfg_parse_one_rp_client: could not parse gss_names.");
1360 jfilt=json_object_get(jrealm, "filters");
1362 new_filts=tr_cfg_parse_filters(tmp_ctx, jfilt, &call_rc);
1363 if (call_rc!=TR_CFG_SUCCESS) {
1364 tr_err("tr_cfg_parse_one_rp_client: could not parse filters.");
1369 tr_debug("tr_cfg_parse_one_rp_client: no filters specified, using default filters.");
1370 new_filts= tr_cfg_default_filters(tmp_ctx, realm, &call_rc);
1371 if (call_rc!=TR_CFG_SUCCESS) {
1372 tr_err("tr_cfg_parse_one_rp_client: could not set default filters.");
1378 tr_rp_client_set_filters(client, new_filts);
1383 tr_free_name(realm);
1385 if (*rc==TR_CFG_SUCCESS)
1386 talloc_steal(mem_ctx, client);
1388 talloc_free(client);
1392 talloc_free(tmp_ctx);
1396 /* Determine whether the realm is an RP realm */
1397 static int tr_cfg_is_rp_realm(json_t *jrealm)
1399 /* Check that we have a gss name. */
1400 if (NULL != json_object_get(jrealm, "gss_names"))
1406 /* Parse any rp clients in the j_realms object. Ignores other realms. */
1407 static TR_RP_CLIENT *tr_cfg_parse_rp_clients(TALLOC_CTX *mem_ctx, json_t *jrealms, TR_CFG_RC *rc)
1409 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1410 TR_RP_CLIENT *clients=NULL;
1411 TR_RP_CLIENT *new_client=NULL;
1412 json_t *this_jrealm=NULL;
1416 if ((jrealms==NULL) || (!json_is_array(jrealms))) {
1417 tr_err("tr_cfg_parse_rp_clients: realms not an array");
1418 *rc=TR_CFG_BAD_PARAMS;
1422 for (ii=0; ii<json_array_size(jrealms); ii++) {
1423 this_jrealm=json_array_get(jrealms, ii);
1424 if (tr_cfg_is_rp_realm(this_jrealm)) {
1425 new_client=tr_cfg_parse_one_rp_client(tmp_ctx, this_jrealm, rc);
1426 if ((*rc)!=TR_CFG_SUCCESS) {
1427 tr_err("tr_cfg_parse_rp_clients: error decoding realm entry %d", ii+1);
1431 tr_rp_client_add(clients, new_client);
1436 talloc_steal(mem_ctx, clients);
1439 talloc_free(tmp_ctx);
1443 /* takes a talloc context, but currently does not use it */
1444 static TR_NAME *tr_cfg_parse_org_name(TALLOC_CTX *mem_ctx, json_t *j_org, TR_CFG_RC *rc)
1448 if ((j_org==NULL) || (rc==NULL) || (!json_is_string(j_org))) {
1449 tr_debug("tr_cfg_parse_org_name: Bad parameters.");
1451 *rc = TR_CFG_BAD_PARAMS; /* fill in return value if we can */
1455 name=tr_new_name(json_string_value(j_org));
1463 static TR_CFG_RC tr_cfg_parse_one_local_org(TR_CFG *trc, json_t *jlorg)
1465 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1466 TR_CFG_RC retval=TR_CFG_ERROR; /* our return code */
1467 TR_CFG_RC rc=TR_CFG_ERROR; /* return code from functions we call */
1468 TR_NAME *org_name=NULL;
1470 json_t *j_realms=NULL;
1471 TR_IDP_REALM *new_idp_realms=NULL;
1472 TR_RP_CLIENT *new_rp_clients=NULL;
1474 tr_debug("tr_cfg_parse_one_local_org: parsing local organization");
1476 /* get organization_name (optional) */
1477 if (NULL==(j_org=json_object_get(jlorg, "organization_name"))) {
1478 tr_debug("tr_cfg_parse_one_local_org: organization_name unspecified");
1480 org_name=tr_cfg_parse_org_name(tmp_ctx, j_org, &rc);
1481 if (rc==TR_CFG_SUCCESS) {
1482 tr_debug("tr_cfg_parse_one_local_org: organization_name=\"%.*s\"",
1485 /* we don't actually do anything with this, but we could */
1486 tr_free_name(org_name);
1491 /* Now get realms. Allow this to be missing; even though that is a pointless organization entry,
1492 * it's harmless. Report a warning because that might be unintentional. */
1493 if (NULL==(j_realms=json_object_get(jlorg, "realms"))) {
1494 tr_warning("tr_cfg_parse_one_local_org: warning - no realms in this local organization");
1496 /* Allocate in the tmp_ctx so these will be cleaned up if we do not complete successfully. */
1497 new_idp_realms=tr_cfg_parse_idp_realms(tmp_ctx, j_realms, &rc);
1498 if (rc!=TR_CFG_SUCCESS)
1501 new_rp_clients=tr_cfg_parse_rp_clients(tmp_ctx, j_realms, &rc);
1502 if (rc!=TR_CFG_SUCCESS)
1505 retval=TR_CFG_SUCCESS;
1508 /* if we succeeded, link things to the configuration and move out of tmp context */
1509 if (retval==TR_CFG_SUCCESS) {
1510 if (new_idp_realms!=NULL) {
1511 tr_idp_realm_add(trc->ctable->idp_realms, new_idp_realms); /* fixes talloc contexts except for head*/
1512 talloc_steal(trc, trc->ctable->idp_realms); /* make sure the head is in the right context */
1515 if (new_rp_clients!=NULL) {
1516 tr_rp_client_add(trc->rp_clients, new_rp_clients); /* fixes talloc contexts */
1517 talloc_steal(trc, trc->rp_clients); /* make sure head is in the right context */
1521 talloc_free(tmp_ctx);
1525 /* Parse local organizations if present. Returns success if there are none. On failure, the configuration is unreliable. */
1526 static TR_CFG_RC tr_cfg_parse_local_orgs(TR_CFG *trc, json_t *jcfg)
1528 json_t *jlocorgs=NULL;
1531 jlocorgs=json_object_get(jcfg, "local_organizations");
1533 return TR_CFG_SUCCESS;
1535 if (!json_is_array(jlocorgs)) {
1536 tr_err("tr_cfg_parse_local_orgs: local_organizations is not an array.");
1537 return TR_CFG_NOPARSE;
1540 for (ii=0; ii<json_array_size(jlocorgs); ii++) {
1541 if (tr_cfg_parse_one_local_org(trc, json_array_get(jlocorgs, ii))!=TR_CFG_SUCCESS) {
1542 tr_err("tr_cfg_parse_local_orgs: error parsing local_organization %d.", ii+1);
1543 return TR_CFG_NOPARSE;
1547 return TR_CFG_SUCCESS;
1550 static TR_CFG_RC tr_cfg_parse_one_peer_org(TR_CFG *trc, json_t *jporg)
1552 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1557 TRP_PEER *new_peer=NULL;
1558 TR_GSS_NAMES *names=NULL;
1559 TR_FILTER_SET *filt_set=NULL;
1560 TR_CFG_RC rc=TR_CFG_ERROR;
1562 jhost=json_object_get(jporg, "hostname");
1563 jport=json_object_get(jporg, "port");
1564 jgss=json_object_get(jporg, "gss_names");
1565 jfilt=json_object_get(jporg, "filters");
1567 if ((jhost==NULL) || (!json_is_string(jhost))) {
1568 tr_err("tr_cfg_parse_one_peer_org: hostname not specified or not a string.");
1573 if ((jport!=NULL) && (!json_is_number(jport))) {
1574 /* note that not specifying the port is allowed, but if set it must be a number */
1575 tr_err("tr_cfg_parse_one_peer_org: port is not a number.");
1580 if ((jgss==NULL) || (!json_is_array(jgss))) {
1581 tr_err("tr_cfg_parse_one_peer_org: gss_names not specified or not an array.");
1586 if ((jfilt!=NULL) && (!json_is_object(jfilt))) {
1587 tr_err("tr_cfg_parse_one_peer_org: filters is not an object.");
1592 new_peer=trp_peer_new(tmp_ctx);
1593 if (new_peer==NULL) {
1594 tr_err("tr_cfg_parse_one_peer_org: could not allocate new peer.");
1599 trp_peer_set_server(new_peer, json_string_value(jhost)); /* string is strdup'ed in _set_server() */
1601 trp_peer_set_port(new_peer, TRP_PORT);
1603 trp_peer_set_port(new_peer, json_integer_value(jport));
1605 names=tr_cfg_parse_gss_names(tmp_ctx, jgss, &rc);
1606 if (rc!=TR_CFG_SUCCESS) {
1607 tr_err("tr_cfg_parse_one_peer_org: unable to parse gss names.");
1611 trp_peer_set_gss_names(new_peer, names);
1614 filt_set=tr_cfg_parse_filters(tmp_ctx, jfilt, &rc);
1615 if (rc!=TR_CFG_SUCCESS) {
1616 tr_err("tr_cfg_parse_one_peer_org: unable to parse filters.");
1620 trp_peer_set_filters(new_peer, filt_set);
1624 trp_ptable_add(trc->peers, new_peer);
1628 talloc_free(tmp_ctx);
1632 /* Parse peer organizations, if present. Returns success if there are none. */
1633 static TR_CFG_RC tr_cfg_parse_peer_orgs(TR_CFG *trc, json_t *jcfg)
1635 json_t *jpeerorgs=NULL;
1638 jpeerorgs=json_object_get(jcfg, "peer_organizations");
1639 if (jpeerorgs==NULL)
1640 return TR_CFG_SUCCESS;
1642 if (!json_is_array(jpeerorgs)) {
1643 tr_err("tr_cfg_parse_peer_orgs: peer_organizations is not an array.");
1644 return TR_CFG_NOPARSE;
1647 for (ii=0; ii<json_array_size(jpeerorgs); ii++) {
1648 if (tr_cfg_parse_one_peer_org(trc, json_array_get(jpeerorgs, ii))!=TR_CFG_SUCCESS) {
1649 tr_err("tr_cfg_parse_peer_orgs: error parsing peer_organization %d.", ii+1);
1650 return TR_CFG_NOPARSE;
1654 return TR_CFG_SUCCESS;
1657 static TR_CFG_RC tr_cfg_parse_default_servers (TR_CFG *trc, json_t *jcfg)
1659 json_t *jdss = NULL;
1660 TR_CFG_RC rc = TR_CFG_SUCCESS;
1661 TR_AAA_SERVER *ds = NULL;
1664 if ((!trc) || (!jcfg))
1665 return TR_CFG_BAD_PARAMS;
1667 /* If there are default servers, store them */
1668 if ((NULL != (jdss = json_object_get(jcfg, "default_servers"))) &&
1669 (json_is_array(jdss)) &&
1670 (0 < json_array_size(jdss))) {
1672 for (i = 0; i < json_array_size(jdss); i++) {
1673 if (NULL == (ds = tr_cfg_parse_one_aaa_server(trc,
1674 json_array_get(jdss, i),
1678 tr_debug("tr_cfg_parse_default_servers: Default server configured: %s", ds->hostname->buf);
1679 ds->next = trc->default_servers;
1680 trc->default_servers = ds;
1684 tr_debug("tr_cfg_parse_default_servers: Finished (rc=%d)", rc);
1688 static void tr_cfg_parse_comm_idps(TR_CFG *trc, json_t *jidps, TR_COMM *comm, TR_CFG_RC *rc)
1690 TR_IDP_REALM *found_idp=NULL;
1691 json_t *jidp_name=NULL;
1692 TR_NAME *idp_name=NULL;
1697 (!json_is_array(jidps))) {
1699 *rc = TR_CFG_BAD_PARAMS;
1703 json_array_foreach(jidps, ii, jidp_name) {
1704 idp_name=tr_new_name(json_string_value(jidp_name));
1705 if (idp_name==NULL) {
1709 found_idp=tr_cfg_find_idp(trc, idp_name, rc);
1710 tr_free_name(idp_name);
1712 if ((found_idp==NULL) || (*rc!=TR_CFG_SUCCESS)) {
1713 tr_debug("tr_cfg_parse_comm_idps: Unknown IDP %s.", json_string_value(jidp_name));
1717 tr_comm_add_idp_realm(trc->ctable, comm, found_idp, 0, NULL, NULL); /* no provenance, never expires */
1724 static void tr_cfg_parse_comm_rps(TR_CFG *trc, json_t *jrps, TR_COMM *comm, TR_CFG_RC *rc)
1726 TR_RP_REALM *found_rp=NULL;
1727 TR_RP_REALM *new_rp=NULL;
1728 TR_NAME *rp_name=NULL;
1734 (!json_is_array(jrps))) {
1736 *rc = TR_CFG_BAD_PARAMS;
1740 for (ii=0; ii<json_array_size(jrps); ii++) {
1741 /* get the RP name as a string */
1742 s=json_string_value(json_array_get(jrps, ii));
1744 tr_notice("tr_cfg_parse_comm_rps: null RP found in community %.*s, ignoring.",
1745 tr_comm_get_id(comm)->len, tr_comm_get_id(comm)->buf);
1749 /* convert string to TR_NAME */
1750 rp_name=tr_new_name(s);
1751 if (rp_name==NULL) {
1752 tr_err("tr_cfg_parse_comm_rps: unable to allocate RP name for %s in community %.*s.",
1753 s, tr_comm_get_id(comm)->len, tr_comm_get_id(comm)->buf);
1756 /* see if we already have this RP in this community */
1757 found_rp=tr_comm_find_rp(trc->ctable, comm, rp_name);
1758 if (found_rp!=NULL) {
1759 tr_notice("tr_cfg_parse_comm_rps: RP %s repeated in community %.*s.",
1760 s, tr_comm_get_id(comm)->len, tr_comm_get_id(comm)->buf);
1761 tr_free_name(rp_name);
1765 /* Add the RP to the community, first see if we have the RP in any community */
1766 found_rp=tr_rp_realm_lookup(trc->ctable->rp_realms, rp_name);
1767 if (found_rp!=NULL) {
1768 tr_debug("tr_cfg_parse_comm_rps: RP realm %s already exists.", s);
1769 new_rp=found_rp; /* use it rather than creating a new realm record */
1771 new_rp=tr_rp_realm_new(NULL);
1773 tr_err("tr_cfg_parse_comm_rps: unable to allocate RP record for %s in community %.*s.",
1774 s, tr_comm_get_id(comm)->len, tr_comm_get_id(comm)->buf);
1776 tr_debug("tr_cfg_parse_comm_rps: setting name to %s", rp_name->buf);
1777 tr_rp_realm_set_id(new_rp, rp_name);
1778 rp_name=NULL; /* rp_name no longer belongs to us */
1779 tr_rp_realm_add(trc->ctable->rp_realms, new_rp);
1780 talloc_steal(trc->ctable, trc->ctable->rp_realms); /* make sure head is in the right context */
1782 tr_comm_add_rp_realm(trc->ctable, comm, new_rp, 0, NULL, NULL);
1786 static TR_COMM *tr_cfg_parse_one_comm (TALLOC_CTX *mem_ctx, TR_CFG *trc, json_t *jcomm, TR_CFG_RC *rc)
1788 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1789 TR_COMM *comm = NULL;
1791 json_t *jtype = NULL;
1792 json_t *japcs = NULL;
1793 json_t *jidps = NULL;
1794 json_t *jrps = NULL;
1796 if ((!trc) || (!jcomm) || (!rc)) {
1797 tr_debug("tr_cfg_parse_one_comm: Bad parameters.");
1799 *rc = TR_CFG_BAD_PARAMS;
1803 comm=tr_comm_new(tmp_ctx);
1805 tr_crit("tr_cfg_parse_one_comm: Out of memory.");
1811 if ((NULL == (jid = json_object_get(jcomm, "community_id"))) ||
1812 (!json_is_string(jid)) ||
1813 (NULL == (jtype = json_object_get(jcomm, "type"))) ||
1814 (!json_is_string(jtype)) ||
1815 (NULL == (japcs = json_object_get(jcomm, "apcs"))) ||
1816 (!json_is_array(japcs)) ||
1817 (NULL == (jidps = json_object_get(jcomm, "idp_realms"))) ||
1818 (!json_is_array(jidps)) ||
1819 (NULL == (jrps = json_object_get(jcomm, "rp_realms"))) ||
1820 (!json_is_array(jrps))) {
1821 tr_debug("tr_cfg_parse_one_comm: Error parsing Communities configuration.");
1822 *rc = TR_CFG_NOPARSE;
1827 tr_comm_set_id(comm, tr_new_name(json_string_value(jid)));
1828 if (NULL == tr_comm_get_id(comm)) {
1829 tr_debug("tr_cfg_parse_one_comm: No memory for community id.");
1835 if (0 == strcmp(json_string_value(jtype), "apc")) {
1836 comm->type = TR_COMM_APC;
1837 } else if (0 == strcmp(json_string_value(jtype), "coi")) {
1838 comm->type = TR_COMM_COI;
1839 if (NULL == (comm->apcs = tr_cfg_parse_apcs(trc, japcs, rc))) {
1840 tr_debug("tr_cfg_parse_one_comm: Can't parse APCs for COI %s.",
1841 tr_comm_get_id(comm)->buf);
1846 tr_debug("tr_cfg_parse_one_comm: Invalid community type, comm = %s, type = %s",
1847 tr_comm_get_id(comm)->buf, json_string_value(jtype));
1848 *rc = TR_CFG_NOPARSE;
1853 tr_cfg_parse_comm_idps(trc, jidps, comm, rc);
1854 if (TR_CFG_SUCCESS != *rc) {
1855 tr_debug("tr_cfg_parse_one_comm: Can't parse IDP realms for comm %s.",
1856 tr_comm_get_id(comm)->buf);
1861 tr_cfg_parse_comm_rps(trc, jrps, comm, rc);
1862 if (TR_CFG_SUCCESS != *rc) {
1863 tr_debug("tr_cfg_parse_one_comm: Can't parse RP realms for comm %s .",
1864 tr_comm_get_id(comm)->buf);
1869 if (TR_COMM_APC == comm->type) {
1870 json_t *jexpire = json_object_get(jcomm, "expiration_interval");
1871 comm->expiration_interval = 43200; /*30 days*/
1873 if (!json_is_integer(jexpire)) {
1874 tr_err("tr_parse_one_comm: expiration_interval is not an integer for comm %.*s",
1875 tr_comm_get_id(comm)->len, tr_comm_get_id(comm)->buf);
1879 comm->expiration_interval = json_integer_value(jexpire);
1880 if (comm->expiration_interval <= 10) {
1881 comm->expiration_interval = 11; /* Freeradius waits 10 minutes between successful TR queries*/
1883 "tr_parse_one_comm: expiration interval for %.*s less than minimum of 11 minutes; using 11 minutes instead.",
1884 tr_comm_get_id(comm)->len, tr_comm_get_id(comm)->buf);
1886 if (comm->expiration_interval > 129600) {
1888 comm->expiration_interval = 129600;
1890 "tr_parse_one_comm: expiration interval for %.*s exceeds maximum of 90 days; using 90 days instead.",
1891 tr_comm_get_id(comm)->len, tr_comm_get_id(comm)->buf);
1898 talloc_steal(mem_ctx, comm);
1899 talloc_free(tmp_ctx);
1903 static TR_CFG_RC tr_cfg_parse_comms (TR_CFG *trc, json_t *jcfg)
1905 json_t *jcomms = NULL;
1906 TR_CFG_RC rc = TR_CFG_SUCCESS;
1907 TR_COMM *comm = NULL;
1910 if ((!trc) || (!jcfg)) {
1911 tr_debug("tr_cfg_parse_comms: Bad Parameters.");
1912 return TR_CFG_BAD_PARAMS;
1915 if (NULL != (jcomms = json_object_get(jcfg, "communities"))) {
1916 if (!json_is_array(jcomms)) {
1917 return TR_CFG_NOPARSE;
1920 for (i = 0; i < json_array_size(jcomms); i++) {
1921 if (NULL == (comm = tr_cfg_parse_one_comm(NULL, /* TODO: use a talloc context */
1923 json_array_get(jcomms, i),
1927 tr_debug("tr_cfg_parse_comms: Community configured: %s.",
1928 tr_comm_get_id(comm)->buf);
1930 tr_comm_table_add_comm(trc->ctable, comm);
1933 tr_debug("tr_cfg_parse_comms: Finished (rc=%d)", rc);
1937 TR_CFG_RC tr_cfg_validate(TR_CFG *trc)
1939 TR_CFG_RC rc = TR_CFG_SUCCESS;
1942 return TR_CFG_BAD_PARAMS;
1944 if ((NULL == trc->internal)||
1945 (NULL == trc->internal->hostname)) {
1946 tr_debug("tr_cfg_validate: Error: No internal configuration, or no hostname.");
1950 if (NULL == trc->rp_clients) {
1951 tr_debug("tr_cfg_validate: Error: No RP Clients configured");
1955 if (0==tr_comm_table_size(trc->ctable)) {
1956 tr_debug("tr_cfg_validate: Error: No Communities configured");
1960 if ((NULL == trc->default_servers) && (NULL == trc->ctable->idp_realms)) {
1961 tr_debug("tr_cfg_validate: Error: No default servers or IDPs configured.");
1968 static void tr_cfg_log_json_error(const char *label, json_error_t *rc)
1970 tr_debug("%s: JSON parse error on line %d: %s",
1977 * Parse a config file and return its JSON structure. Also emits a serial number to the log
1978 * if one is present.
1980 * @param file_with_path The file (with path!) to parse
1981 * @return Pointer to the result of parsing, or null on error
1983 static json_t *tr_cfg_parse_one_config_file(const char *file_with_path)
1989 if (NULL==(jcfg=json_load_file(file_with_path,
1990 JSON_DISABLE_EOF_CHECK|JSON_REJECT_DUPLICATES, &rc))) {
1991 tr_debug("tr_cfg_parse_one_config_file: Error parsing config file %s.",
1993 tr_cfg_log_json_error("tr_cfg_parse_one_config_file", &rc);
1997 // Look for serial number and log it if it exists (borrowed reference, so no need to free it later)
1998 if (NULL!=(jser=json_object_get(jcfg, "serial_number"))) {
1999 if (json_is_number(jser)) {
2000 tr_notice("tr_parse_one_config_file: Attempting to load revision %" JSON_INTEGER_FORMAT " of '%s'.",
2001 json_integer_value(jser),
2010 * Helper to free an array returned by tr_cfg_parse_config_files
2014 static void tr_cfg_parse_free_jcfgs(unsigned int n_jcfgs, json_t **jcfgs)
2017 for (ii=0; ii<n_jcfgs; ii++)
2018 json_decref(jcfgs[ii]);
2023 * Parse a list of configuration files. Returns an array of JSON objects, free this with
2024 * tr_cfg_parse_free_jcfgs(), a helper function
2031 static json_t **tr_cfg_parse_config_files(TALLOC_CTX *mem_ctx, unsigned int n_files, char **files_with_paths)
2033 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
2035 json_t **jcfgs=NULL;
2037 /* first allocate the jcfgs */
2038 jcfgs=talloc_array(NULL, json_t *, n_files);
2040 tr_crit("tr_parse_config_files: cannot allocate JSON structure array");
2043 for (ii=0; ii<n_files; ii++) {
2044 jcfgs[ii]=tr_cfg_parse_one_config_file(files_with_paths[ii]);
2045 if (jcfgs[ii]==NULL) {
2046 tr_err("tr_parse_config: Error parsing JSON in %s", files_with_paths[ii]);
2047 tr_cfg_parse_free_jcfgs(ii, jcfgs); /* frees the JSON objects and the jcfgs array */
2054 talloc_steal(mem_ctx, jcfgs); /* give this to the caller's context if we succeeded */
2055 talloc_free(tmp_ctx);
2059 /* define a type for config parse functions */
2060 typedef TR_CFG_RC (TR_CFG_PARSE_FN)(TR_CFG *, json_t *);
2062 * Helper function to parse a collection of JSON structures using a generic parse function.
2064 * @param cfg Config structure to receive results
2065 * @param jcfgs Pointer to an array of decoded JSON structures
2066 * @param n_jcfg Number of JSON structures in the array
2067 * @param parse_fn Function to apply
2068 * @return TR_CFG_SUCCESS on success, _FAIL or an error code on failure
2070 static TR_CFG_RC tr_cfg_parse_helper(TR_CFG *cfg, unsigned int n_jcfg, json_t **jcfgs, TR_CFG_PARSE_FN parse_fn)
2073 json_t *this_jcfg=NULL;
2074 TR_CFG_RC ret=TR_CFG_ERROR;
2076 if ((cfg==NULL) || (jcfgs==NULL) || (parse_fn==NULL))
2077 return TR_CFG_ERROR;
2079 for (ii=0; ii<n_jcfg; ii++) {
2080 this_jcfg=jcfgs[ii];
2081 ret=parse_fn(cfg, this_jcfg);
2082 if (ret!=TR_CFG_SUCCESS)
2090 * Reads configuration files in config_dir ("" or "./" will use the current directory).
2092 * @param cfg_mgr Configuration manager
2093 * @param n_files Number of entries in cfg_files
2094 * @param files_with_paths Array of filenames with path to load
2095 * @return TR_CFG_SUCCESS on success, TR_CFG_ERROR or a more specific error on failure
2097 TR_CFG_RC tr_parse_config(TR_CFG_MGR *cfg_mgr, unsigned int n_files, char **files_with_paths)
2099 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
2100 json_t **jcfgs=NULL;
2101 TR_CFG_RC cfg_rc=TR_CFG_ERROR;
2103 if ((!cfg_mgr) || (!files_with_paths)) {
2104 cfg_rc=TR_CFG_BAD_PARAMS;
2108 /* get a fresh config to fill in, freeing old one if needed */
2109 if (cfg_mgr->new != NULL)
2110 tr_cfg_free(cfg_mgr->new);
2111 cfg_mgr->new=tr_cfg_new(tmp_ctx); /* belongs to the temporary context for now */
2112 if (cfg_mgr->new == NULL) {
2113 cfg_rc=TR_CFG_NOMEM;
2117 /* first parse the json */
2118 jcfgs=tr_cfg_parse_config_files(tmp_ctx, n_files, files_with_paths);
2120 cfg_rc=TR_CFG_NOPARSE;
2124 cfg_mgr->new->peers=trp_ptable_new(cfg_mgr); /* not sure why this isn't in cfg_mgr->new's context */
2126 /* now run through the parsers on the JSON */
2127 if ((TR_CFG_SUCCESS != (cfg_rc=tr_cfg_parse_helper(cfg_mgr->new, n_files, jcfgs, tr_cfg_parse_internal))) ||
2128 (TR_CFG_SUCCESS != (cfg_rc=tr_cfg_parse_helper(cfg_mgr->new, n_files, jcfgs, tr_cfg_parse_local_orgs))) ||
2129 (TR_CFG_SUCCESS != (cfg_rc=tr_cfg_parse_helper(cfg_mgr->new, n_files, jcfgs, tr_cfg_parse_peer_orgs))) ||
2130 (TR_CFG_SUCCESS != (cfg_rc=tr_cfg_parse_helper(cfg_mgr->new, n_files, jcfgs, tr_cfg_parse_default_servers))) ||
2131 (TR_CFG_SUCCESS != (cfg_rc=tr_cfg_parse_helper(cfg_mgr->new, n_files, jcfgs, tr_cfg_parse_comms))))
2132 goto cleanup; /* cfg_rc was set above */
2134 /* make sure we got a complete, consistent configuration */
2135 if (TR_CFG_SUCCESS != tr_cfg_validate(cfg_mgr->new)) {
2136 tr_err("tr_parse_config: Error: INVALID CONFIGURATION");
2137 cfg_rc=TR_CFG_ERROR;
2142 talloc_steal(cfg_mgr, cfg_mgr->new); /* hand this over to the cfg_mgr context */
2143 cfg_rc=TR_CFG_SUCCESS;
2147 tr_cfg_parse_free_jcfgs(n_files, jcfgs);
2148 talloc_free(tmp_ctx);
2152 TR_IDP_REALM *tr_cfg_find_idp (TR_CFG *tr_cfg, TR_NAME *idp_id, TR_CFG_RC *rc)
2155 TR_IDP_REALM *cfg_idp;
2157 if ((!tr_cfg) || (!idp_id)) {
2159 *rc = TR_CFG_BAD_PARAMS;
2163 for (cfg_idp = tr_cfg->ctable->idp_realms; NULL != cfg_idp; cfg_idp = cfg_idp->next) {
2164 if (!tr_name_cmp (idp_id, cfg_idp->realm_id)) {
2165 tr_debug("tr_cfg_find_idp: Found %s.", idp_id->buf);
2169 /* if we didn't find one, return NULL */
2173 TR_RP_CLIENT *tr_cfg_find_rp (TR_CFG *tr_cfg, TR_NAME *rp_gss, TR_CFG_RC *rc)
2175 TR_RP_CLIENT *cfg_rp;
2177 if ((!tr_cfg) || (!rp_gss)) {
2179 *rc = TR_CFG_BAD_PARAMS;
2183 for (cfg_rp = tr_cfg->rp_clients; NULL != cfg_rp; cfg_rp = cfg_rp->next) {
2184 if (tr_gss_names_matches(cfg_rp->gss_names, rp_gss)) {
2185 tr_debug("tr_cfg_find_rp: Found %s.", rp_gss->buf);
2189 /* if we didn't find one, return NULL */
2193 static int is_cfg_file(const struct dirent *dent) {
2196 /* Only accept filenames ending in ".cfg" and starting with a character
2197 * other than an ASCII '.' */
2199 /* filename must be at least 4 characters long to be acceptable */
2200 n=strlen(dent->d_name);
2205 /* filename must not start with '.' */
2206 if ('.' == dent->d_name[0]) {
2210 /* If the above passed and the last four characters of the filename are .cfg, accept.
2211 * (n.b., assumes an earlier test checked that the name is >= 4 chars long.) */
2212 if (0 == strcmp(&(dent->d_name[n-4]), ".cfg")) {
2216 /* otherwise, return false. */
2220 /* Find configuration files in a particular directory. Returns the
2221 * number of entries found, 0 if none are found, or <0 for some
2222 * errors. If n>=0, the cfg_files parameter will contain a newly
2223 * allocated array of pointers to struct dirent entries, as returned
2224 * by scandir(). These can be freed with tr_free_config_file_list().
2226 int tr_find_config_files(const char *config_dir, struct dirent ***cfg_files) {
2229 n = scandir(config_dir, cfg_files, is_cfg_file, alphasort);
2233 tr_debug("tr_find_config: scandir error trying to scan %s.", config_dir);
2239 /* Free memory allocated for configuration file list returned from tr_find_config_files().
2240 * This can be called regardless of the return value of tr_find_config_values(). */
2241 void tr_free_config_file_list(int n, struct dirent ***cfg_files) {
2244 /* if n < 0, then scandir did not allocate anything because it failed */
2245 if((n>=0) && (*cfg_files != NULL)) {
2246 for(ii=0; ii<n; ii++) {
2247 free((*cfg_files)[ii]);
2249 free(*cfg_files); /* safe even if n==0 */
2250 *cfg_files=NULL; /* this will help prevent accidentally freeing twice */