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->ctable);
57 void tr_print_comms (TR_COMM_TABLE *ctab)
61 for (comm = ctab->comms; NULL != comm; comm = comm->next) {
62 tr_notice("tr_print_config: Community %s:", comm->id->buf);
64 tr_notice("tr_print_config: - Member IdPs:");
65 tr_print_comm_idps(ctab, comm);
67 tr_notice("tr_print_config: - Member RPs:");
68 tr_print_comm_rps(ctab, comm);
72 void tr_print_comm_idps(TR_COMM_TABLE *ctab, TR_COMM *comm)
74 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
75 TR_COMM_ITER *iter=NULL;
76 TR_IDP_REALM *idp = NULL;
79 iter=tr_comm_iter_new(tmp_ctx);
81 tr_notice("tr_print_config: unable to allocate IdP iterator.");
86 for (idp=tr_idp_realm_iter_first(iter, ctab, tr_comm_get_id(comm));
88 idp=tr_idp_realm_iter_next(iter)) {
89 s=tr_idp_realm_to_str(tmp_ctx, idp);
91 tr_notice("tr_print_config: - @%s", s);
93 tr_notice("tr_print_config: unable to allocate IdP output string.");
98 void tr_print_comm_rps(TR_COMM_TABLE *ctab, TR_COMM *comm)
100 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
101 TR_COMM_ITER *iter=NULL;
102 TR_RP_REALM *rp = NULL;
105 iter=tr_comm_iter_new(tmp_ctx);
107 tr_notice("tr_print_config: unable to allocate RP iterator.");
108 talloc_free(tmp_ctx);
112 for (rp=tr_rp_realm_iter_first(iter, ctab, tr_comm_get_id(comm));
114 rp=tr_rp_realm_iter_next(iter)) {
115 s=tr_rp_realm_to_str(tmp_ctx, rp);
117 tr_notice("tr_print_config: - @%s", s);
119 tr_notice("tr_print_config: unable to allocate RP output string.");
121 talloc_free(tmp_ctx);
124 TR_CFG *tr_cfg_new(TALLOC_CTX *mem_ctx)
126 TR_CFG *cfg=talloc(mem_ctx, TR_CFG);
129 cfg->rp_clients=NULL;
131 cfg->default_servers=NULL;
132 cfg->ctable=tr_comm_table_new(cfg);
133 if (cfg->ctable==NULL) {
141 void tr_cfg_free (TR_CFG *cfg)
146 TR_CFG_MGR *tr_cfg_mgr_new(TALLOC_CTX *mem_ctx)
148 return talloc_zero(mem_ctx, TR_CFG_MGR);
151 void tr_cfg_mgr_free (TR_CFG_MGR *cfg_mgr) {
152 talloc_free(cfg_mgr);
155 TR_CFG_RC tr_apply_new_config (TR_CFG_MGR *cfg_mgr)
157 /* cfg_mgr->active is allowed to be null, but new cannot be */
158 if ((cfg_mgr==NULL) || (cfg_mgr->new==NULL))
159 return TR_CFG_BAD_PARAMS;
161 if (cfg_mgr->active != NULL)
162 tr_cfg_free(cfg_mgr->active);
164 cfg_mgr->active = cfg_mgr->new;
165 cfg_mgr->new=NULL; /* only keep a single handle on the new configuration */
167 tr_log_threshold(cfg_mgr->active->internal->log_threshold);
168 tr_console_threshold(cfg_mgr->active->internal->console_threshold);
170 return TR_CFG_SUCCESS;
173 static TR_CFG_RC tr_cfg_parse_internal(TR_CFG *trc, json_t *jcfg)
177 json_t *jtidsp = NULL;
178 json_t *jtrpsp = NULL;
179 json_t *jhname = NULL;
181 json_t *jconthres = NULL;
182 json_t *jlogthres = NULL;
183 json_t *jcfgpoll = NULL;
184 json_t *jcfgsettle = NULL;
185 json_t *jroutesweep = NULL;
186 json_t *jrouteupdate = NULL;
187 json_t *jtidreq_timeout = NULL;
188 json_t *jtidresp_numer = NULL;
189 json_t *jtidresp_denom = NULL;
190 json_t *jrouteconnect = NULL;
192 if ((!trc) || (!jcfg))
193 return TR_CFG_BAD_PARAMS;
195 if (NULL == trc->internal) {
196 if (NULL == (trc->internal = talloc_zero(trc, TR_CFG_INTERNAL)))
200 if (NULL != (jint = json_object_get(jcfg, "tr_internal"))) {
201 if (NULL != (jmtd = json_object_get(jint, "max_tree_depth"))) {
202 if (json_is_number(jmtd)) {
203 trc->internal->max_tree_depth = json_integer_value(jmtd);
205 tr_debug("tr_cfg_parse_internal: Parsing error, max_tree_depth is not a number.");
206 return TR_CFG_NOPARSE;
209 /* If not configured, use the default */
210 trc->internal->max_tree_depth = TR_DEFAULT_MAX_TREE_DEPTH;
212 if (NULL != (jtidsp = json_object_get(jint, "tids_port"))) {
213 if (json_is_number(jtidsp)) {
214 trc->internal->tids_port = json_integer_value(jtidsp);
216 tr_debug("tr_cfg_parse_internal: Parsing error, tids_port is not a number.");
217 return TR_CFG_NOPARSE;
220 /* If not configured, use the default */
221 trc->internal->tids_port = TR_DEFAULT_TIDS_PORT;
223 if (NULL != (jtrpsp = json_object_get(jint, "trps_port"))) {
224 if (json_is_number(jtrpsp)) {
225 trc->internal->trps_port = json_integer_value(jtrpsp);
227 tr_debug("tr_cfg_parse_internal: Parsing error, trps_port is not a number.");
228 return TR_CFG_NOPARSE;
231 /* If not configured, use the default */
232 trc->internal->trps_port = TR_DEFAULT_TRPS_PORT;
234 if (NULL != (jhname = json_object_get(jint, "hostname"))) {
235 if (json_is_string(jhname)) {
236 trc->internal->hostname = json_string_value(jhname);
238 tr_debug("tr_cfg_parse_internal: Parsing error, hostname is not a string.");
239 return TR_CFG_NOPARSE;
242 if (NULL != (jcfgpoll = json_object_get(jint, "cfg_poll_interval"))) {
243 if (json_is_number(jcfgpoll)) {
244 trc->internal->cfg_poll_interval = json_integer_value(jcfgpoll);
246 tr_debug("tr_cfg_parse_internal: Parsing error, cfg_poll_interval is not a number.");
247 return TR_CFG_NOPARSE;
250 trc->internal->cfg_poll_interval = TR_CFGWATCH_DEFAULT_POLL;
253 if (NULL != (jcfgsettle = json_object_get(jint, "cfg_settling_time"))) {
254 if (json_is_number(jcfgsettle)) {
255 trc->internal->cfg_settling_time = json_integer_value(jcfgsettle);
257 tr_debug("tr_cfg_parse_internal: Parsing error, cfg_settling_time is not a number.");
258 return TR_CFG_NOPARSE;
261 trc->internal->cfg_settling_time = TR_CFGWATCH_DEFAULT_SETTLE;
264 if (NULL != (jrouteconnect = json_object_get(jint, "trp_connect_interval"))) {
265 if (json_is_number(jrouteconnect)) {
266 trc->internal->trp_connect_interval = json_integer_value(jrouteconnect);
268 tr_debug("tr_cfg_parse_internal: Parsing error, trp_connect_interval is not a number.");
269 return TR_CFG_NOPARSE;
272 /* if not configured, use the default */
273 trc->internal->trp_connect_interval=TR_DEFAULT_TRP_CONNECT_INTERVAL;
276 if (NULL != (jroutesweep = json_object_get(jint, "trp_sweep_interval"))) {
277 if (json_is_number(jroutesweep)) {
278 trc->internal->trp_sweep_interval = json_integer_value(jroutesweep);
280 tr_debug("tr_cfg_parse_internal: Parsing error, trp_sweep_interval is not a number.");
281 return TR_CFG_NOPARSE;
284 /* if not configured, use the default */
285 trc->internal->trp_sweep_interval=TR_DEFAULT_TRP_SWEEP_INTERVAL;
288 if (NULL != (jrouteupdate = json_object_get(jint, "trp_update_interval"))) {
289 if (json_is_number(jrouteupdate)) {
290 trc->internal->trp_update_interval = json_integer_value(jrouteupdate);
292 tr_debug("tr_cfg_parse_internal: Parsing error, trp_update_interval is not a number.");
293 return TR_CFG_NOPARSE;
296 /* if not configured, use the default */
297 trc->internal->trp_update_interval=TR_DEFAULT_TRP_UPDATE_INTERVAL;
300 if (NULL != (jtidreq_timeout = json_object_get(jint, "tid_request_timeout"))) {
301 if (json_is_number(jtidreq_timeout)) {
302 trc->internal->tid_req_timeout = json_integer_value(jtidreq_timeout);
304 tr_debug("tr_cfg_parse_internal: Parsing error, tid_request_timeout is not a number.");
305 return TR_CFG_NOPARSE;
308 /* if not configured, use the default */
309 trc->internal->tid_req_timeout=TR_DEFAULT_TID_REQ_TIMEOUT;
312 if (NULL != (jtidresp_numer = json_object_get(jint, "tid_response_numerator"))) {
313 if (json_is_number(jtidresp_numer)) {
314 trc->internal->tid_resp_numer = json_integer_value(jtidresp_numer);
316 tr_debug("tr_cfg_parse_internal: Parsing error, tid_response_numerator is not a number.");
317 return TR_CFG_NOPARSE;
320 /* if not configured, use the default */
321 trc->internal->tid_resp_numer=TR_DEFAULT_TID_RESP_NUMER;
324 if (NULL != (jtidresp_denom = json_object_get(jint, "tid_response_denominator"))) {
325 if (json_is_number(jtidresp_denom)) {
326 trc->internal->tid_resp_denom = json_integer_value(jtidresp_denom);
328 tr_debug("tr_cfg_parse_internal: Parsing error, tid_response_denominator is not a number.");
329 return TR_CFG_NOPARSE;
332 /* if not configured, use the default */
333 trc->internal->tid_resp_denom=TR_DEFAULT_TID_RESP_DENOM;
336 if (NULL != (jlog = json_object_get(jint, "logging"))) {
337 if (NULL != (jlogthres = json_object_get(jlog, "log_threshold"))) {
338 if (json_is_string(jlogthres)) {
339 trc->internal->log_threshold = str2sev(json_string_value(jlogthres));
341 tr_debug("tr_cfg_parse_internal: Parsing error, log_threshold is not a string.");
342 return TR_CFG_NOPARSE;
345 /* If not configured, use the default */
346 trc->internal->log_threshold = TR_DEFAULT_LOG_THRESHOLD;
349 if (NULL != (jconthres = json_object_get(jlog, "console_threshold"))) {
350 if (json_is_string(jconthres)) {
351 trc->internal->console_threshold = str2sev(json_string_value(jconthres));
353 tr_debug("tr_cfg_parse_internal: Parsing error, console_threshold is not a string.");
354 return TR_CFG_NOPARSE;
357 /* If not configured, use the default */
358 trc->internal->console_threshold = TR_DEFAULT_CONSOLE_THRESHOLD;
361 /* If not configured, use the default */
362 trc->internal->console_threshold = TR_DEFAULT_CONSOLE_THRESHOLD;
363 trc->internal->log_threshold = TR_DEFAULT_LOG_THRESHOLD;
366 tr_debug("tr_cfg_parse_internal: Internal config parsed.");
367 return TR_CFG_SUCCESS;
369 return TR_CFG_SUCCESS;
372 static TR_CONSTRAINT *tr_cfg_parse_one_constraint(TALLOC_CTX *mem_ctx, char *ctype, json_t *jc, TR_CFG_RC *rc)
374 TR_CONSTRAINT *cons=NULL;
377 if ((!ctype) || (!jc) || (!rc) ||
378 (!json_is_array(jc)) ||
379 (0 >= json_array_size(jc)) ||
380 (TR_MAX_CONST_MATCHES < json_array_size(jc)) ||
381 (!json_is_string(json_array_get(jc, 0)))) {
382 tr_err("tr_cfg_parse_one_constraint: config error.");
387 if (NULL==(cons=tr_constraint_new(mem_ctx))) {
388 tr_debug("tr_cfg_parse_one_constraint: Out of memory (cons).");
393 if (NULL==(cons->type=tr_new_name(ctype))) {
394 tr_err("tr_cfg_parse_one_constraint: Out of memory (type).");
396 tr_constraint_free(cons);
400 for (i=0; i < json_array_size(jc); i++) {
401 cons->matches[i]=tr_new_name(json_string_value(json_array_get(jc, i)));
402 if (cons->matches[i]==NULL) {
403 tr_err("tr_cfg_parse_one_constraint: Out of memory (match %d).", i+1);
405 tr_constraint_free(cons);
413 static TR_FILTER *tr_cfg_parse_one_filter(TALLOC_CTX *mem_ctx, json_t *jfilt, TR_FILTER_TYPE ftype, TR_CFG_RC *rc)
415 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
416 TR_FILTER *filt = NULL;
417 json_t *jfaction = NULL;
418 json_t *jfline = NULL;
419 json_t *jfspecs = NULL;
420 json_t *this_jfspec = NULL;
421 json_t *jfield = NULL;
422 json_t *jmatch = NULL;
425 json_t *this_jmatch = NULL;
426 TR_NAME *name = NULL;
427 size_t i = 0, j = 0, k = 0;
431 if ((jfilt == NULL) || (rc == NULL)) {
432 tr_err("tr_cfg_parse_one_filter: null argument");
433 *rc = TR_CFG_BAD_PARAMS;
437 if (NULL == (filt = tr_filter_new(tmp_ctx))) {
438 tr_err("tr_cfg_parse_one_filter: Out of memory.");
442 tr_filter_set_type(filt, ftype);
444 /* make sure we have space to represent the filter */
445 if (json_array_size(jfilt) > TR_MAX_FILTER_LINES) {
446 tr_err("tr_cfg_parse_one_filter: Filter has too many lines, maximum of %d.", TR_MAX_FILTER_LINES);
447 *rc = TR_CFG_NOPARSE;
451 /* For each entry in the filter... */
452 json_array_foreach(jfilt, i, jfline) {
453 if ((NULL == (jfaction = json_object_get(jfline, "action"))) ||
454 (!json_is_string(jfaction))) {
455 tr_debug("tr_cfg_parse_one_filter: Error parsing filter action.");
456 *rc = TR_CFG_NOPARSE;
460 if ((NULL == (jfspecs = json_object_get(jfline, "specs"))) ||
461 (!json_is_array(jfspecs)) ||
462 (0 == json_array_size(jfspecs))) {
463 tr_debug("tr_cfg_parse_one_filter: Error parsing filter specs.");
464 *rc = TR_CFG_NOPARSE;
468 if (TR_MAX_FILTER_SPECS < json_array_size(jfspecs)) {
469 tr_debug("tr_cfg_parse_one_filter: Filter has too many specs, maximimum of %d.", TR_MAX_FILTER_SPECS);
470 *rc = TR_CFG_NOPARSE;
474 if (NULL == (filt->lines[i] = tr_fline_new(filt))) {
475 tr_debug("tr_cfg_parse_one_filter: Out of memory allocating filter line %d.", i + 1);
480 if (!strcmp(json_string_value(jfaction), "accept")) {
481 filt->lines[i]->action = TR_FILTER_ACTION_ACCEPT;
482 } else if (!strcmp(json_string_value(jfaction), "reject")) {
483 filt->lines[i]->action = TR_FILTER_ACTION_REJECT;
485 tr_debug("tr_cfg_parse_one_filter: Error parsing filter action, unknown action' %s'.",
486 json_string_value(jfaction));
487 *rc = TR_CFG_NOPARSE;
491 if (NULL != (jrc = json_object_get(jfline, "realm_constraints"))) {
492 if (!json_is_array(jrc)) {
493 tr_err("tr_cfg_parse_one_filter: cannot parse realm_constraints, not an array.");
494 *rc = TR_CFG_NOPARSE;
496 } else if (json_array_size(jrc) > TR_MAX_CONST_MATCHES) {
497 tr_err("tr_cfg_parse_one_filter: realm_constraints has too many entries, maximum of %d.",
498 TR_MAX_CONST_MATCHES);
499 *rc = TR_CFG_NOPARSE;
501 } else if (json_array_size(jrc) > 0) {
502 /* ok we actually have entries to process */
503 if (NULL == (filt->lines[i]->realm_cons = tr_cfg_parse_one_constraint(filt->lines[i], "realm", jrc, rc))) {
504 tr_debug("tr_cfg_parse_one_filter: Error parsing realm constraint");
505 *rc = TR_CFG_NOPARSE;
511 if (NULL != (jdc = json_object_get(jfline, "domain_constraints"))) {
512 if (!json_is_array(jdc)) {
513 tr_err("tr_cfg_parse_one_filter: cannot parse domain_constraints, not an array.");
514 *rc = TR_CFG_NOPARSE;
516 } else if (json_array_size(jdc) > TR_MAX_CONST_MATCHES) {
517 tr_err("tr_cfg_parse_one_filter: domain_constraints has too many entries, maximum of %d.",
518 TR_MAX_CONST_MATCHES);
519 *rc = TR_CFG_NOPARSE;
521 } else if (json_array_size(jdc) > 0) {
522 if (NULL == (filt->lines[i]->domain_cons = tr_cfg_parse_one_constraint(filt->lines[i], "domain", jdc, rc))) {
523 tr_debug("tr_cfg_parse_one_filter: Error parsing domain constraint");
524 *rc = TR_CFG_NOPARSE;
530 /*For each filter spec within the filter line... */
531 json_array_foreach(jfspecs, j, this_jfspec) {
532 if ((NULL == (jfield = json_object_get(this_jfspec, "field"))) ||
533 (!json_is_string(jfield))) {
534 tr_debug("tr_cfg_parse_one_filter: Error parsing filter: missing field for filer spec %d, filter line %d.", i,
536 *rc = TR_CFG_NOPARSE;
540 /* check that we have a match attribute */
541 if (NULL == (jmatch = json_object_get(this_jfspec, "match"))) {
542 tr_debug("tr_cfg_parse_one_filter: Error parsing filter: missing match for filer spec %d, filter line %d.", i,
544 *rc = TR_CFG_NOPARSE;
548 /* check that match is a string or an array */
549 if ((!json_is_string(jmatch)) && (!json_is_array(jmatch))) {
551 "tr_cfg_parse_one_filter: Error parsing filter: match not a string or array for filter spec %d, filter line %d.",
553 *rc = TR_CFG_NOPARSE;
557 /* allocate the filter spec */
558 if (NULL == (filt->lines[i]->specs[j] = tr_fspec_new(filt->lines[i]))) {
559 tr_debug("tr_cfg_parse_one_filter: Out of memory.");
564 /* fill in the field */
565 if (NULL == (filt->lines[i]->specs[j]->field = tr_new_name(json_string_value(jfield)))) {
566 tr_debug("tr_cfg_parse_one_filter: Out of memory.");
571 /* fill in the matches */
572 if (json_is_string(jmatch)) {
573 if (NULL == (name = tr_new_name(json_string_value(jmatch)))) {
574 tr_debug("tr_cfg_parse_one_filter: Out of memory.");
578 tr_fspec_add_match(filt->lines[i]->specs[j], name);
580 /* jmatch is an array (we checked earlier) */
581 json_array_foreach(jmatch, k, this_jmatch) {
582 if (NULL == (name = tr_new_name(json_string_value(this_jmatch)))) {
583 tr_debug("tr_cfg_parse_one_filter: Out of memory.");
587 tr_fspec_add_match(filt->lines[i]->specs[j], name);
590 if (!tr_filter_validate_spec_field(ftype, filt->lines[i]->specs[j])){
591 tr_debug("tr_cfg_parse_one_filter: Invalid filter field \"%.*s\" for %s filter, spec %d, filter %d.",
592 filt->lines[i]->specs[j]->field->len,
593 filt->lines[i]->specs[j]->field->buf,
594 tr_filter_type_to_string(filt->type),
602 /* check that the filter is valid */
603 if (!tr_filter_validate(filt)) {
606 *rc = TR_CFG_SUCCESS;
607 talloc_steal(mem_ctx, filt);
611 talloc_free(tmp_ctx);
612 if (*rc!=TR_CFG_SUCCESS)
617 static TR_FILTER_SET *tr_cfg_parse_filters(TALLOC_CTX *mem_ctx, json_t *jfilts, TR_CFG_RC *rc)
619 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
621 const char *filt_label=NULL;
622 TR_FILTER *filt=NULL;
623 TR_FILTER_SET *filt_set=NULL;
624 TR_FILTER_TYPE filt_type=TR_FILTER_TYPE_UNKNOWN;
634 filt_set=tr_filter_set_new(tmp_ctx);
635 if (filt_set==NULL) {
636 tr_debug("tr_cfg_parse_filters: Unable to allocate filter set.");
641 json_object_foreach(jfilts, filt_label, jfilt) {
642 /* check that we got a filter */
644 tr_debug("tr_cfg_parse_filters: Definition for %s filter is missing.", filt_label);
645 *rc = TR_CFG_NOPARSE;
649 /* check that we recognize the filter type */
650 filt_type=tr_filter_type_from_string(filt_label);
651 if (filt_type==TR_FILTER_TYPE_UNKNOWN) {
652 tr_debug("tr_cfg_parse_filters: Unrecognized filter (%s) defined.", filt_label);
653 *rc = TR_CFG_NOPARSE;
657 /* finally, parse the filter */
658 tr_debug("tr_cfg_parse_filters: Found %s filter.", filt_label);
659 filt = tr_cfg_parse_one_filter(tmp_ctx, jfilt, filt_type, rc);
660 tr_filter_set_add(filt_set, filt);
661 if (*rc != TR_CFG_SUCCESS) {
662 tr_debug("tr_cfg_parse_filters: Error parsing %s filter.", filt_label);
663 *rc = TR_CFG_NOPARSE;
671 if (*rc==TR_CFG_SUCCESS)
672 talloc_steal(mem_ctx, filt_set);
673 else if (filt_set!=NULL) {
674 talloc_free(filt_set);
678 talloc_free(tmp_ctx);
682 static TR_AAA_SERVER *tr_cfg_parse_one_aaa_server(TALLOC_CTX *mem_ctx, json_t *jaddr, TR_CFG_RC *rc)
684 TR_AAA_SERVER *aaa = NULL;
687 if ((!jaddr) || (!json_is_string(jaddr))) {
688 tr_debug("tr_cfg_parse_one_aaa_server: Bad parameters.");
689 *rc = TR_CFG_BAD_PARAMS;
693 name=tr_new_name(json_string_value(jaddr));
695 tr_debug("tr_cfg_parse_one_aaa_server: Out of memory allocating hostname.");
700 aaa=tr_aaa_server_new(mem_ctx, name);
703 tr_debug("tr_cfg_parse_one_aaa_server: Out of memory allocating AAA server.");
711 static TR_AAA_SERVER *tr_cfg_parse_aaa_servers(TALLOC_CTX *mem_ctx, json_t *jaaas, TR_CFG_RC *rc)
713 TALLOC_CTX *tmp_ctx=NULL;
714 TR_AAA_SERVER *aaa = NULL;
715 TR_AAA_SERVER *temp_aaa = NULL;
718 for (i = 0; i < json_array_size(jaaas); i++) {
719 /* rc gets set in here */
720 if (NULL == (temp_aaa = tr_cfg_parse_one_aaa_server(tmp_ctx, json_array_get(jaaas, i), rc))) {
721 talloc_free(tmp_ctx);
724 /* TBD -- IPv6 addresses */
725 // tr_debug("tr_cfg_parse_aaa_servers: Configuring AAA Server: ip_addr = %s.", inet_ntoa(temp_aaa->aaa_server_addr));
726 temp_aaa->next = aaa;
729 tr_debug("tr_cfg_parse_aaa_servers: Finished (rc=%d)", *rc);
731 for (temp_aaa=aaa; temp_aaa!=NULL; temp_aaa=temp_aaa->next)
732 talloc_steal(mem_ctx, temp_aaa);
733 talloc_free(tmp_ctx);
737 static TR_APC *tr_cfg_parse_one_apc(TALLOC_CTX *mem_ctx, json_t *japc, TR_CFG_RC *rc)
742 *rc = TR_CFG_SUCCESS; /* presume success */
744 if ((!japc) || (!rc) || (!json_is_string(japc))) {
745 tr_debug("tr_cfg_parse_one_apc: Bad parameters.");
747 *rc = TR_CFG_BAD_PARAMS;
751 apc=tr_apc_new(mem_ctx);
753 tr_debug("tr_cfg_parse_one_apc: Out of memory.");
758 name=tr_new_name(json_string_value(japc));
760 tr_debug("tr_cfg_parse_one_apc: No memory for APC name.");
765 tr_apc_set_id(apc, name); /* apc is now responsible for freeing the name */
770 static TR_APC *tr_cfg_parse_apcs(TALLOC_CTX *mem_ctx, json_t *japcs, TR_CFG_RC *rc)
772 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
774 TR_APC *new_apc=NULL;
776 TR_CFG_RC call_rc=TR_CFG_ERROR;
778 *rc = TR_CFG_SUCCESS; /* presume success */
780 if ((!japcs) || (!rc) || (!json_is_array(japcs))) {
781 tr_debug("tr_cfg_parse_one_apc: Bad parameters.");
783 *rc = TR_CFG_BAD_PARAMS;
787 for (ii=0; ii<json_array_size(japcs); ii++) {
788 new_apc=tr_cfg_parse_one_apc(tmp_ctx, json_array_get(japcs, ii), &call_rc);
789 if ((call_rc!=TR_CFG_SUCCESS) || (new_apc==NULL)) {
790 tr_debug("tr_cfg_parse_apcs: Error parsing APC %d.", ii+1);
794 tr_apc_add(apcs, new_apc);
797 talloc_steal(mem_ctx, apcs);
801 talloc_free(tmp_ctx);
805 static TR_NAME *tr_cfg_parse_name(TALLOC_CTX *mem_ctx, json_t *jname, TR_CFG_RC *rc)
810 if ((jname==NULL) || (!json_is_string(jname))) {
811 tr_err("tr_cfg_parse_name: name missing or not a string");
812 *rc=TR_CFG_BAD_PARAMS;
815 name=tr_new_name(json_string_value(jname));
817 tr_err("tr_cfg_parse_name: could not allocate name");
826 static int tr_cfg_parse_shared_config(json_t *jsc, TR_CFG_RC *rc)
828 const char *shared=NULL;
832 (!json_is_string(jsc)) ||
833 (NULL==(shared=json_string_value(jsc)))) {
834 *rc=TR_CFG_BAD_PARAMS;
838 if (0==strcmp(shared, "no"))
840 else if (0==strcmp(shared, "yes"))
843 /* any other value is an error */
844 tr_debug("tr_cfg_parse_shared_config: invalid shared_config value \"%s\" (should be \"yes\" or \"no\")",
850 static TR_REALM_ORIGIN tr_cfg_realm_origin(json_t *jrealm)
852 json_t *jremote=json_object_get(jrealm, "remote");
856 return TR_REALM_LOCAL;
857 if (!json_is_string(jremote)) {
858 tr_warning("tr_cfg_realm_origin: \"remote\" is not a string, assuming this is a local realm.");
859 return TR_REALM_LOCAL;
861 s=json_string_value(jremote);
862 if (strcasecmp(s, "yes")==0)
863 return TR_REALM_REMOTE_INCOMPLETE;
864 else if (strcasecmp(s, "no")!=0)
865 tr_warning("tr_cfg_realm_origin: \"remote\" is neither 'yes' nor 'no', assuming this is a local realm.");
867 return TR_REALM_LOCAL;
870 /* Parse the identity provider object from a realm and fill in the given TR_IDP_REALM. */
871 static TR_CFG_RC tr_cfg_parse_idp(TR_IDP_REALM *idp, json_t *jidp)
873 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
875 TR_AAA_SERVER *aaa=NULL;
876 TR_CFG_RC rc=TR_CFG_ERROR;
881 idp->shared_config=tr_cfg_parse_shared_config(json_object_get(jidp, "shared_config"), &rc);
882 if (rc!=TR_CFG_SUCCESS) {
883 tr_err("tr_cfg_parse_idp: missing or malformed shared_config specification");
888 apcs=tr_cfg_parse_apcs(tmp_ctx, json_object_get(jidp, "apcs"), &rc);
889 if ((rc!=TR_CFG_SUCCESS) || (apcs==NULL)) {
890 tr_err("tr_cfg_parse_idp: unable to parse APC");
895 aaa=tr_cfg_parse_aaa_servers(idp, json_object_get(jidp, "aaa_servers"), &rc);
896 if (rc!=TR_CFG_SUCCESS) {
897 tr_err("tr_cfg_parse_idp: unable to parse AAA servers");
902 tr_debug("tr_cfg_parse_idp: APC=\"%.*s\"",
906 /* done, fill in the idp structures */
908 talloc_steal(idp, apcs);
909 idp->aaa_servers=aaa;
913 if (rc!=TR_CFG_SUCCESS) {
917 tr_aaa_server_free(aaa);
920 talloc_free(tmp_ctx);
924 /* parses idp realm */
925 static TR_IDP_REALM *tr_cfg_parse_one_idp_realm(TALLOC_CTX *mem_ctx, json_t *jrealm, TR_CFG_RC *rc)
927 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
928 TR_IDP_REALM *realm=NULL;
929 TR_CFG_RC call_rc=TR_CFG_ERROR;
931 *rc=TR_CFG_ERROR; /* default to error if not set */
933 if ((!jrealm) || (!rc)) {
934 tr_err("tr_cfg_parse_one_idp_realm: Bad parameters.");
936 *rc=TR_CFG_BAD_PARAMS;
940 if (NULL==(realm=tr_idp_realm_new(tmp_ctx))) {
941 tr_err("tr_cfg_parse_one_idp_realm: could not allocate idp realm.");
946 realm->origin=tr_cfg_realm_origin(jrealm);
947 if (realm->origin!=TR_REALM_LOCAL) {
948 tr_debug("tr_cfg_parse_one_idp_realm: realm is remote, should not have full IdP info.");
953 /* must have a name */
954 realm->realm_id=tr_cfg_parse_name(realm,
955 json_object_get(jrealm, "realm"),
957 if ((call_rc!=TR_CFG_SUCCESS) || (realm->realm_id==NULL)) {
958 tr_err("tr_cfg_parse_one_idp_realm: could not parse realm name");
962 tr_debug("tr_cfg_parse_one_idp_realm: realm_id=\"%.*s\"",
963 realm->realm_id->len,
964 realm->realm_id->buf);
966 call_rc=tr_cfg_parse_idp(realm, json_object_get(jrealm, "identity_provider"));
967 if (call_rc!=TR_CFG_SUCCESS) {
968 tr_err("tr_cfg_parse_one_idp_realm: could not parse identity_provider.");
976 if (*rc==TR_CFG_SUCCESS)
977 talloc_steal(mem_ctx, realm);
983 talloc_free(tmp_ctx);
987 /* Determine whether the realm is an IDP realm */
988 static int tr_cfg_is_idp_realm(json_t *jrealm)
990 /* If a realm spec contains an identity_provider, it's an IDP realm. */
991 if (NULL != json_object_get(jrealm, "identity_provider"))
997 static TR_IDP_REALM *tr_cfg_parse_one_remote_realm(TALLOC_CTX *mem_ctx, json_t *jrealm, TR_CFG_RC *rc)
999 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1000 TR_IDP_REALM *realm=talloc(mem_ctx, TR_IDP_REALM);
1002 *rc=TR_CFG_ERROR; /* default to error if not set */
1004 if ((!jrealm) || (!rc)) {
1005 tr_err("tr_cfg_parse_one_remote_realm: Bad parameters.");
1007 *rc=TR_CFG_BAD_PARAMS;
1011 if (NULL==(realm=tr_idp_realm_new(tmp_ctx))) {
1012 tr_err("tr_cfg_parse_one_remote_realm: could not allocate idp realm.");
1017 /* must have a name */
1018 realm->realm_id=tr_cfg_parse_name(realm,
1019 json_object_get(jrealm, "realm"),
1021 if ((*rc!=TR_CFG_SUCCESS) || (realm->realm_id==NULL)) {
1022 tr_err("tr_cfg_parse_one_remote_realm: could not parse realm name");
1026 tr_debug("tr_cfg_parse_one_remote_realm: realm_id=\"%.*s\"",
1027 realm->realm_id->len,
1028 realm->realm_id->buf);
1030 realm->origin=tr_cfg_realm_origin(jrealm);
1034 if (*rc==TR_CFG_SUCCESS)
1035 talloc_steal(mem_ctx, realm);
1041 talloc_free(tmp_ctx);
1045 static int tr_cfg_is_remote_realm(json_t *jrealm)
1047 return (tr_cfg_realm_origin(jrealm)!=TR_REALM_LOCAL);
1050 /* Parse any idp realms in the j_realms object. Ignores other realm types. */
1051 static TR_IDP_REALM *tr_cfg_parse_idp_realms(TALLOC_CTX *mem_ctx, json_t *jrealms, TR_CFG_RC *rc)
1053 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1054 TR_IDP_REALM *realms=NULL;
1055 TR_IDP_REALM *new_realm=NULL;
1056 json_t *this_jrealm=NULL;
1060 if ((jrealms==NULL) || (!json_is_array(jrealms))) {
1061 tr_err("tr_cfg_parse_idp_realms: realms not an array");
1062 *rc=TR_CFG_BAD_PARAMS;
1066 for (ii=0; ii<json_array_size(jrealms); ii++) {
1067 this_jrealm=json_array_get(jrealms, ii);
1068 if (tr_cfg_is_idp_realm(this_jrealm)) {
1069 new_realm=tr_cfg_parse_one_idp_realm(tmp_ctx, this_jrealm, rc);
1070 if ((*rc)!=TR_CFG_SUCCESS) {
1071 tr_err("tr_cfg_parse_idp_realms: error decoding realm entry %d", ii+1);
1075 tr_idp_realm_add(realms, new_realm);
1076 } else if (tr_cfg_is_remote_realm(this_jrealm)) {
1077 new_realm=tr_cfg_parse_one_remote_realm(tmp_ctx, this_jrealm, rc);
1078 if ((*rc)!=TR_CFG_SUCCESS) {
1079 tr_err("tr_cfg_parse_idp_realms: error decoding remote realm entry %d", ii+1);
1083 tr_idp_realm_add(realms, new_realm);
1088 talloc_steal(mem_ctx, realms);
1091 talloc_free(tmp_ctx);
1095 static TR_GSS_NAMES *tr_cfg_parse_gss_names(TALLOC_CTX *mem_ctx, json_t *jgss_names, TR_CFG_RC *rc)
1097 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1098 TR_GSS_NAMES *gn=NULL;
1103 if ((rc==NULL) || (jgss_names==NULL)) {
1104 tr_err("tr_cfg_parse_gss_names: Bad parameters.");
1105 *rc=TR_CFG_BAD_PARAMS;
1109 if (!json_is_array(jgss_names)) {
1110 tr_err("tr_cfg_parse_gss_names: gss_names not an array.");
1115 gn=tr_gss_names_new(tmp_ctx);
1116 for (ii=0; ii<json_array_size(jgss_names); ii++) {
1117 jname=json_array_get(jgss_names, ii);
1118 if (!json_is_string(jname)) {
1119 tr_err("tr_cfg_parse_gss_names: Encountered non-string gss name.");
1124 name=tr_new_name(json_string_value(jname));
1126 tr_err("tr_cfg_parse_gss_names: Out of memory allocating gss name.");
1131 if (tr_gss_names_add(gn, name)!=0) {
1133 tr_err("tr_cfg_parse_gss_names: Unable to add gss name to RP client.");
1139 talloc_steal(mem_ctx, gn);
1143 talloc_free(tmp_ctx);
1144 if ((*rc!=TR_CFG_SUCCESS) && (gn!=NULL))
1149 /* default filter accepts realm and *.realm */
1150 static TR_FILTER_SET *tr_cfg_default_filters(TALLOC_CTX *mem_ctx, TR_NAME *realm, TR_CFG_RC *rc)
1152 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1153 TR_FILTER *filt=NULL;
1154 TR_FILTER_SET *filt_set=NULL;
1155 TR_CONSTRAINT *cons=NULL;
1157 TR_NAME *n_prefix=tr_new_name("*.");
1158 TR_NAME *n_rp_realm_1=tr_new_name("rp_realm");
1159 TR_NAME *n_rp_realm_2=tr_new_name("rp_realm");
1160 TR_NAME *n_domain=tr_new_name("domain");
1161 TR_NAME *n_realm=tr_new_name("realm");
1164 if ((realm==NULL) || (rc==NULL)) {
1165 tr_debug("tr_cfg_default_filters: invalid arguments.");
1167 *rc=TR_CFG_BAD_PARAMS;
1171 if ((n_prefix==NULL) ||
1172 (n_rp_realm_1==NULL) ||
1173 (n_rp_realm_2==NULL) ||
1176 tr_debug("tr_cfg_default_filters: unable to allocate names.");
1181 filt=tr_filter_new(tmp_ctx);
1183 tr_debug("tr_cfg_default_filters: could not allocate filter.");
1187 tr_filter_set_type(filt, TR_FILTER_TYPE_TID_INBOUND);
1188 filt->lines[0]=tr_fline_new(filt);
1189 if (filt->lines[0]==NULL) {
1190 tr_debug("tr_cfg_default_filters: could not allocate filter line.");
1195 filt->lines[0]->action=TR_FILTER_ACTION_ACCEPT;
1196 filt->lines[0]->specs[0]=tr_fspec_new(filt->lines[0]);
1197 filt->lines[0]->specs[0]->field=n_rp_realm_1;
1198 n_rp_realm_1=NULL; /* we don't own this name any more */
1200 name=tr_dup_name(realm);
1202 tr_debug("tr_cfg_default_filters: could not allocate realm name.");
1206 tr_fspec_add_match(filt->lines[0]->specs[0], name);
1207 name=NULL; /* we no longer own the name */
1209 /* now do the wildcard name */
1210 filt->lines[0]->specs[1]=tr_fspec_new(filt->lines[0]);
1211 filt->lines[0]->specs[1]->field=n_rp_realm_2;
1212 n_rp_realm_2=NULL; /* we don't own this name any more */
1214 if (NULL==(name=tr_name_cat(n_prefix, realm))) {
1215 tr_debug("tr_cfg_default_filters: could not allocate wildcard realm name.");
1220 tr_fspec_add_match(filt->lines[0]->specs[1], name);
1221 name=NULL; /* we no longer own the name */
1223 /* domain constraint */
1224 if (NULL==(cons=tr_constraint_new(filt->lines[0]))) {
1225 tr_debug("tr_cfg_default_filters: could not allocate domain constraint.");
1230 cons->type=n_domain;
1231 n_domain=NULL; /* belongs to the constraint now */
1232 name=tr_dup_name(realm);
1234 tr_debug("tr_cfg_default_filters: could not allocate realm name for domain constraint.");
1238 cons->matches[0]=name;
1239 name=tr_name_cat(n_prefix, realm);
1241 tr_debug("tr_cfg_default_filters: could not allocate wildcard realm name for domain constraint.");
1245 cons->matches[1]=name;
1247 filt->lines[0]->domain_cons=cons;
1250 /* realm constraint */
1251 if (NULL==(cons=tr_constraint_new(filt->lines[0]))) {
1252 tr_debug("tr_cfg_default_filters: could not allocate realm constraint.");
1258 n_realm=NULL; /* belongs to the constraint now */
1259 name=tr_dup_name(realm);
1261 tr_debug("tr_cfg_default_filters: could not allocate realm name for realm constraint.");
1265 cons->matches[0]=name;
1266 name=tr_name_cat(n_prefix, realm);
1268 tr_debug("tr_cfg_default_filters: could not allocate wildcard realm name for realm constraint.");
1272 cons->matches[1]=name;
1274 filt->lines[0]->realm_cons=cons;
1276 /* put the filter in a set */
1277 filt_set=tr_filter_set_new(tmp_ctx);
1278 if ((filt_set==NULL)||(0!=tr_filter_set_add(filt_set, filt))) {
1279 tr_debug("tr_cfg_default_filters: could not allocate filter set.");
1283 talloc_steal(mem_ctx, filt_set);
1286 talloc_free(tmp_ctx);
1288 if (*rc!=TR_CFG_SUCCESS)
1292 tr_free_name(n_prefix);
1293 if (n_rp_realm_1!=NULL)
1294 tr_free_name(n_rp_realm_1);
1295 if (n_rp_realm_2!=NULL)
1296 tr_free_name(n_rp_realm_2);
1298 tr_free_name(n_realm);
1300 tr_free_name(n_domain);
1307 /* parses rp client */
1308 static TR_RP_CLIENT *tr_cfg_parse_one_rp_client(TALLOC_CTX *mem_ctx, json_t *jrealm, TR_CFG_RC *rc)
1310 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1311 TR_RP_CLIENT *client=NULL;
1312 TR_CFG_RC call_rc=TR_CFG_ERROR;
1313 TR_FILTER_SET *new_filts=NULL;
1314 TR_NAME *realm=NULL;
1316 json_t *jrealm_id=NULL;
1318 *rc=TR_CFG_ERROR; /* default to error if not set */
1320 if ((!jrealm) || (!rc)) {
1321 tr_err("tr_cfg_parse_one_rp_client: Bad parameters.");
1323 *rc=TR_CFG_BAD_PARAMS;
1327 if ((NULL==(jrealm_id=json_object_get(jrealm, "realm"))) || (!json_is_string(jrealm_id))) {
1328 tr_debug("tr_cfg_parse_one_rp_client: no realm ID found.");
1329 *rc=TR_CFG_BAD_PARAMS;
1333 tr_debug("tr_cfg_parse_one_rp_client: realm_id=\"%s\"", json_string_value(jrealm_id));
1334 realm=tr_new_name(json_string_value(jrealm_id));
1336 tr_err("tr_cfg_parse_one_rp_client: could not allocate realm ID.");
1341 if (NULL==(client=tr_rp_client_new(tmp_ctx))) {
1342 tr_err("tr_cfg_parse_one_rp_client: could not allocate rp client.");
1347 client->gss_names=tr_cfg_parse_gss_names(client, json_object_get(jrealm, "gss_names"), &call_rc);
1349 if (call_rc!=TR_CFG_SUCCESS) {
1350 tr_err("tr_cfg_parse_one_rp_client: could not parse gss_names.");
1356 jfilt=json_object_get(jrealm, "filters");
1358 new_filts=tr_cfg_parse_filters(tmp_ctx, jfilt, &call_rc);
1359 if (call_rc!=TR_CFG_SUCCESS) {
1360 tr_err("tr_cfg_parse_one_rp_client: could not parse filters.");
1365 tr_debug("tr_cfg_parse_one_rp_client: no filters specified, using default filters.");
1366 new_filts= tr_cfg_default_filters(tmp_ctx, realm, &call_rc);
1367 if (call_rc!=TR_CFG_SUCCESS) {
1368 tr_err("tr_cfg_parse_one_rp_client: could not set default filters.");
1374 tr_rp_client_set_filters(client, new_filts);
1379 tr_free_name(realm);
1381 if (*rc==TR_CFG_SUCCESS)
1382 talloc_steal(mem_ctx, client);
1384 talloc_free(client);
1388 talloc_free(tmp_ctx);
1392 /* Determine whether the realm is an RP realm */
1393 static int tr_cfg_is_rp_realm(json_t *jrealm)
1395 /* Check that we have a gss name. */
1396 if (NULL != json_object_get(jrealm, "gss_names"))
1402 /* Parse any rp clients in the j_realms object. Ignores other realms. */
1403 static TR_RP_CLIENT *tr_cfg_parse_rp_clients(TALLOC_CTX *mem_ctx, json_t *jrealms, TR_CFG_RC *rc)
1405 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1406 TR_RP_CLIENT *clients=NULL;
1407 TR_RP_CLIENT *new_client=NULL;
1408 json_t *this_jrealm=NULL;
1412 if ((jrealms==NULL) || (!json_is_array(jrealms))) {
1413 tr_err("tr_cfg_parse_rp_clients: realms not an array");
1414 *rc=TR_CFG_BAD_PARAMS;
1418 for (ii=0; ii<json_array_size(jrealms); ii++) {
1419 this_jrealm=json_array_get(jrealms, ii);
1420 if (tr_cfg_is_rp_realm(this_jrealm)) {
1421 new_client=tr_cfg_parse_one_rp_client(tmp_ctx, this_jrealm, rc);
1422 if ((*rc)!=TR_CFG_SUCCESS) {
1423 tr_err("tr_cfg_parse_rp_clients: error decoding realm entry %d", ii+1);
1427 tr_rp_client_add(clients, new_client);
1432 talloc_steal(mem_ctx, clients);
1435 talloc_free(tmp_ctx);
1439 /* takes a talloc context, but currently does not use it */
1440 static TR_NAME *tr_cfg_parse_org_name(TALLOC_CTX *mem_ctx, json_t *j_org, TR_CFG_RC *rc)
1444 if ((j_org==NULL) || (rc==NULL) || (!json_is_string(j_org))) {
1445 tr_debug("tr_cfg_parse_org_name: Bad parameters.");
1447 *rc = TR_CFG_BAD_PARAMS; /* fill in return value if we can */
1451 name=tr_new_name(json_string_value(j_org));
1460 /* TODO: are we using this? JLR */
1461 /* Update the community information with data from a new batch of IDP realms.
1462 * May partially add realms if there is a failure, no guarantees.
1463 * Call like comms=tr_comm_idp_update(comms, new_realms, &rc) */
1464 static TR_COMM *tr_cfg_comm_idp_update(TALLOC_CTX *mem_ctx,
1465 TR_COMM_TABLE *ctab,
1466 TR_IDP_REALM *new_realms,
1469 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1470 TR_COMM *comm=NULL; /* community looked up in comms table */
1471 TR_COMM *new_comms=NULL; /* new communities as we create them */
1472 TR_IDP_REALM *realm=NULL;
1473 TR_APC *apc=NULL; /* apc of one realm */
1476 *rc=TR_CFG_BAD_PARAMS;
1480 /* start with an empty list communities, then fill that in */
1481 for (realm=new_realms; realm!=NULL; realm=realm->next) {
1482 for (apc=realm->apcs; apc!=NULL; apc=apc->next) {
1483 comm=tr_comm_lookup(comms, apc->id);
1485 comm=tr_comm_new(tmp_ctx);
1487 tr_debug("tr_cfg_comm_idp_update: unable to allocate new community.");
1491 /* fill in the community with info */
1492 comm->type=TR_COMM_APC; /* realms added this way are in APCs */
1493 comm->expiration_interval=TR_DEFAULT_APC_EXPIRATION_INTERVAL;
1494 tr_comm_set_id(comm, tr_dup_name(apc->id));
1495 tr_comm_add_idp_realm(comm, realm);
1496 tr_comm_add(new_comms, comm);
1498 /* add this realm to the comm */
1499 tr_comm_add_idp_realm(comm, realm);
1504 /* we successfully built a list, add it to the other list */
1505 tr_comm_add(comms, new_comms);
1506 talloc_steal(mem_ctx, comms);
1508 talloc_free(tmp_ctx);
1513 static TR_CFG_RC tr_cfg_parse_one_local_org(TR_CFG *trc, json_t *jlorg)
1515 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1516 TR_CFG_RC retval=TR_CFG_ERROR; /* our return code */
1517 TR_CFG_RC rc=TR_CFG_ERROR; /* return code from functions we call */
1518 TR_NAME *org_name=NULL;
1520 json_t *j_realms=NULL;
1521 TR_IDP_REALM *new_idp_realms=NULL;
1522 TR_RP_CLIENT *new_rp_clients=NULL;
1524 tr_debug("tr_cfg_parse_one_local_org: parsing local organization");
1526 /* get organization_name (optional) */
1527 if (NULL==(j_org=json_object_get(jlorg, "organization_name"))) {
1528 tr_debug("tr_cfg_parse_one_local_org: organization_name unspecified");
1530 org_name=tr_cfg_parse_org_name(tmp_ctx, j_org, &rc);
1531 if (rc==TR_CFG_SUCCESS) {
1532 tr_debug("tr_cfg_parse_one_local_org: organization_name=\"%.*s\"",
1535 /* we don't actually do anything with this, but we could */
1536 tr_free_name(org_name);
1541 /* Now get realms. Allow this to be missing; even though that is a pointless organization entry,
1542 * it's harmless. Report a warning because that might be unintentional. */
1543 if (NULL==(j_realms=json_object_get(jlorg, "realms"))) {
1544 tr_warning("tr_cfg_parse_one_local_org: warning - no realms in this local organization");
1546 /* Allocate in the tmp_ctx so these will be cleaned up if we do not complete successfully. */
1547 new_idp_realms=tr_cfg_parse_idp_realms(tmp_ctx, j_realms, &rc);
1548 if (rc!=TR_CFG_SUCCESS)
1551 new_rp_clients=tr_cfg_parse_rp_clients(tmp_ctx, j_realms, &rc);
1552 if (rc!=TR_CFG_SUCCESS)
1555 retval=TR_CFG_SUCCESS;
1558 /* if we succeeded, link things to the configuration and move out of tmp context */
1559 if (retval==TR_CFG_SUCCESS) {
1560 if (new_idp_realms!=NULL) {
1561 tr_idp_realm_add(trc->ctable->idp_realms, new_idp_realms); /* fixes talloc contexts except for head*/
1562 talloc_steal(trc, trc->ctable->idp_realms); /* make sure the head is in the right context */
1565 if (new_rp_clients!=NULL) {
1566 tr_rp_client_add(trc->rp_clients, new_rp_clients); /* fixes talloc contexts */
1567 talloc_steal(trc, trc->rp_clients); /* make sure head is in the right context */
1571 talloc_free(tmp_ctx);
1575 /* Parse local organizations if present. Returns success if there are none. On failure, the configuration is unreliable. */
1576 static TR_CFG_RC tr_cfg_parse_local_orgs(TR_CFG *trc, json_t *jcfg)
1578 json_t *jlocorgs=NULL;
1581 jlocorgs=json_object_get(jcfg, "local_organizations");
1583 return TR_CFG_SUCCESS;
1585 if (!json_is_array(jlocorgs)) {
1586 tr_err("tr_cfg_parse_local_orgs: local_organizations is not an array.");
1587 return TR_CFG_NOPARSE;
1590 for (ii=0; ii<json_array_size(jlocorgs); ii++) {
1591 if (tr_cfg_parse_one_local_org(trc, json_array_get(jlocorgs, ii))!=TR_CFG_SUCCESS) {
1592 tr_err("tr_cfg_parse_local_orgs: error parsing local_organization %d.", ii+1);
1593 return TR_CFG_NOPARSE;
1597 return TR_CFG_SUCCESS;
1600 static TR_CFG_RC tr_cfg_parse_one_peer_org(TR_CFG *trc, json_t *jporg)
1602 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1607 TRP_PEER *new_peer=NULL;
1608 TR_GSS_NAMES *names=NULL;
1609 TR_FILTER_SET *filt_set=NULL;
1610 TR_CFG_RC rc=TR_CFG_ERROR;
1612 jhost=json_object_get(jporg, "hostname");
1613 jport=json_object_get(jporg, "port");
1614 jgss=json_object_get(jporg, "gss_names");
1615 jfilt=json_object_get(jporg, "filters");
1617 if ((jhost==NULL) || (!json_is_string(jhost))) {
1618 tr_err("tr_cfg_parse_one_peer_org: hostname not specified or not a string.");
1623 if ((jport!=NULL) && (!json_is_number(jport))) {
1624 /* note that not specifying the port is allowed, but if set it must be a number */
1625 tr_err("tr_cfg_parse_one_peer_org: port is not a number.");
1630 if ((jgss==NULL) || (!json_is_array(jgss))) {
1631 tr_err("tr_cfg_parse_one_peer_org: gss_names not specified or not an array.");
1636 if ((jfilt!=NULL) && (!json_is_array(jfilt))) {
1637 tr_err("tr_cfg_parse_one_peer_org: filters is not an array.");
1642 new_peer=trp_peer_new(tmp_ctx);
1643 if (new_peer==NULL) {
1644 tr_err("tr_cfg_parse_one_peer_org: could not allocate new peer.");
1649 trp_peer_set_server(new_peer, json_string_value(jhost));
1651 trp_peer_set_port(new_peer, TRP_PORT);
1653 trp_peer_set_port(new_peer, json_integer_value(jport));
1655 names=tr_cfg_parse_gss_names(tmp_ctx, jgss, &rc);
1656 if (rc!=TR_CFG_SUCCESS) {
1657 tr_err("tr_cfg_parse_one_peer_org: unable to parse gss names.");
1661 trp_peer_set_gss_names(new_peer, names);
1664 filt_set=tr_cfg_parse_filters(tmp_ctx, jfilt, &rc);
1665 if (rc!=TR_CFG_SUCCESS) {
1666 tr_err("tr_cfg_parse_one_peer_org: unable to parse filters.");
1670 trp_peer_set_filters(new_peer, filt_set);
1674 trp_ptable_add(trc->peers, new_peer);
1678 talloc_free(tmp_ctx);
1682 /* Parse peer organizations, if present. Returns success if there are none. */
1683 static TR_CFG_RC tr_cfg_parse_peer_orgs(TR_CFG *trc, json_t *jcfg)
1685 json_t *jpeerorgs=NULL;
1688 jpeerorgs=json_object_get(jcfg, "peer_organizations");
1689 if (jpeerorgs==NULL)
1690 return TR_CFG_SUCCESS;
1692 if (!json_is_array(jpeerorgs)) {
1693 tr_err("tr_cfg_parse_peer_orgs: peer_organizations is not an array.");
1694 return TR_CFG_NOPARSE;
1697 for (ii=0; ii<json_array_size(jpeerorgs); ii++) {
1698 if (tr_cfg_parse_one_peer_org(trc, json_array_get(jpeerorgs, ii))!=TR_CFG_SUCCESS) {
1699 tr_err("tr_cfg_parse_peer_orgs: error parsing peer_organization %d.", ii+1);
1700 return TR_CFG_NOPARSE;
1704 return TR_CFG_SUCCESS;
1707 static TR_CFG_RC tr_cfg_parse_default_servers (TR_CFG *trc, json_t *jcfg)
1709 json_t *jdss = NULL;
1710 TR_CFG_RC rc = TR_CFG_SUCCESS;
1711 TR_AAA_SERVER *ds = NULL;
1714 if ((!trc) || (!jcfg))
1715 return TR_CFG_BAD_PARAMS;
1717 /* If there are default servers, store them */
1718 if ((NULL != (jdss = json_object_get(jcfg, "default_servers"))) &&
1719 (json_is_array(jdss)) &&
1720 (0 < json_array_size(jdss))) {
1722 for (i = 0; i < json_array_size(jdss); i++) {
1723 if (NULL == (ds = tr_cfg_parse_one_aaa_server(trc,
1724 json_array_get(jdss, i),
1728 tr_debug("tr_cfg_parse_default_servers: Default server configured: %s", ds->hostname->buf);
1729 ds->next = trc->default_servers;
1730 trc->default_servers = ds;
1734 tr_debug("tr_cfg_parse_default_servers: Finished (rc=%d)", rc);
1738 static void tr_cfg_parse_comm_idps(TR_CFG *trc, json_t *jidps, TR_COMM *comm, TR_CFG_RC *rc)
1740 TR_IDP_REALM *found_idp=NULL;
1745 (!json_is_array(jidps))) {
1747 *rc = TR_CFG_BAD_PARAMS;
1751 for (i=0; i < json_array_size(jidps); i++) {
1752 found_idp=tr_cfg_find_idp(trc,
1753 tr_new_name((char *)json_string_value(json_array_get(jidps, i))),
1755 if ((found_idp==NULL) || (*rc!=TR_CFG_SUCCESS)) {
1756 tr_debug("tr_cfg_parse_comm_idps: Unknown IDP %s.",
1757 (char *)json_string_value(json_array_get(jidps, i)));
1761 tr_comm_add_idp_realm(trc->ctable, comm, found_idp, 0, NULL, NULL); /* no provenance, never expires */
1768 static void tr_cfg_parse_comm_rps(TR_CFG *trc, json_t *jrps, TR_COMM *comm, TR_CFG_RC *rc)
1770 TR_RP_REALM *found_rp=NULL;
1771 TR_RP_REALM *new_rp=NULL;
1772 TR_NAME *rp_name=NULL;
1778 (!json_is_array(jrps))) {
1780 *rc = TR_CFG_BAD_PARAMS;
1784 for (ii=0; ii<json_array_size(jrps); ii++) {
1785 /* get the RP name as a string */
1786 s=json_string_value(json_array_get(jrps, ii));
1788 tr_notice("tr_cfg_parse_comm_rps: null RP found in community %.*s, ignoring.",
1789 tr_comm_get_id(comm)->len, tr_comm_get_id(comm)->buf);
1793 /* convert string to TR_NAME */
1794 rp_name=tr_new_name(s);
1795 if (rp_name==NULL) {
1796 tr_err("tr_cfg_parse_comm_rps: unable to allocate RP name for %s in community %.*s.",
1797 s, tr_comm_get_id(comm)->len, tr_comm_get_id(comm)->buf);
1800 /* see if we already have this RP in this community */
1801 found_rp=tr_comm_find_rp(trc->ctable, comm, rp_name);
1802 if (found_rp!=NULL) {
1803 tr_notice("tr_cfg_parse_comm_rps: RP %s repeated in community %.*s.",
1804 s, tr_comm_get_id(comm)->len, tr_comm_get_id(comm)->buf);
1805 tr_free_name(rp_name);
1809 /* Add the RP to the community, first see if we have the RP in any community */
1810 found_rp=tr_rp_realm_lookup(trc->ctable->rp_realms, rp_name);
1811 if (found_rp!=NULL) {
1812 tr_debug("tr_cfg_parse_comm_rps: RP realm %s already exists.", s);
1813 new_rp=found_rp; /* use it rather than creating a new realm record */
1815 new_rp=tr_rp_realm_new(NULL);
1817 tr_err("tr_cfg_parse_comm_rps: unable to allocate RP record for %s in community %.*s.",
1818 s, tr_comm_get_id(comm)->len, tr_comm_get_id(comm)->buf);
1820 tr_debug("tr_cfg_parse_comm_rps: setting name to %s", rp_name->buf);
1821 tr_rp_realm_set_id(new_rp, rp_name);
1822 rp_name=NULL; /* rp_name no longer belongs to us */
1823 tr_rp_realm_add(trc->ctable->rp_realms, new_rp);
1824 talloc_steal(trc->ctable, trc->ctable->rp_realms); /* make sure head is in the right context */
1826 tr_comm_add_rp_realm(trc->ctable, comm, new_rp, 0, NULL, NULL);
1830 static TR_COMM *tr_cfg_parse_one_comm (TALLOC_CTX *mem_ctx, TR_CFG *trc, json_t *jcomm, TR_CFG_RC *rc)
1832 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1833 TR_COMM *comm = NULL;
1835 json_t *jtype = NULL;
1836 json_t *japcs = NULL;
1837 json_t *jidps = NULL;
1838 json_t *jrps = NULL;
1840 if ((!trc) || (!jcomm) || (!rc)) {
1841 tr_debug("tr_cfg_parse_one_comm: Bad parameters.");
1843 *rc = TR_CFG_BAD_PARAMS;
1847 comm=tr_comm_new(tmp_ctx);
1849 tr_crit("tr_cfg_parse_one_comm: Out of memory.");
1855 if ((NULL == (jid = json_object_get(jcomm, "community_id"))) ||
1856 (!json_is_string(jid)) ||
1857 (NULL == (jtype = json_object_get(jcomm, "type"))) ||
1858 (!json_is_string(jtype)) ||
1859 (NULL == (japcs = json_object_get(jcomm, "apcs"))) ||
1860 (!json_is_array(japcs)) ||
1861 (NULL == (jidps = json_object_get(jcomm, "idp_realms"))) ||
1862 (!json_is_array(jidps)) ||
1863 (NULL == (jrps = json_object_get(jcomm, "rp_realms"))) ||
1864 (!json_is_array(jrps))) {
1865 tr_debug("tr_cfg_parse_one_comm: Error parsing Communities configuration.");
1866 *rc = TR_CFG_NOPARSE;
1871 tr_comm_set_id(comm, tr_new_name(json_string_value(jid)));
1872 if (NULL == tr_comm_get_id(comm)) {
1873 tr_debug("tr_cfg_parse_one_comm: No memory for community id.");
1879 if (0 == strcmp(json_string_value(jtype), "apc")) {
1880 comm->type = TR_COMM_APC;
1881 } else if (0 == strcmp(json_string_value(jtype), "coi")) {
1882 comm->type = TR_COMM_COI;
1883 if (NULL == (comm->apcs = tr_cfg_parse_apcs(trc, japcs, rc))) {
1884 tr_debug("tr_cfg_parse_one_comm: Can't parse APCs for COI %s.",
1885 tr_comm_get_id(comm)->buf);
1890 tr_debug("tr_cfg_parse_one_comm: Invalid community type, comm = %s, type = %s",
1891 tr_comm_get_id(comm)->buf, json_string_value(jtype));
1892 *rc = TR_CFG_NOPARSE;
1897 tr_cfg_parse_comm_idps(trc, jidps, comm, rc);
1898 if (TR_CFG_SUCCESS != *rc) {
1899 tr_debug("tr_cfg_parse_one_comm: Can't parse IDP realms for comm %s.",
1900 tr_comm_get_id(comm)->buf);
1905 tr_cfg_parse_comm_rps(trc, jrps, comm, rc);
1906 if (TR_CFG_SUCCESS != *rc) {
1907 tr_debug("tr_cfg_parse_one_comm: Can't parse RP realms for comm %s .",
1908 tr_comm_get_id(comm)->buf);
1913 if (TR_COMM_APC == comm->type) {
1914 json_t *jexpire = json_object_get(jcomm, "expiration_interval");
1915 comm->expiration_interval = 43200; /*30 days*/
1917 if (!json_is_integer(jexpire)) {
1918 fprintf(stderr, "tr_parse_one_comm: expiration_interval is not an integer\n");
1922 comm->expiration_interval = json_integer_value(jexpire);
1923 if (comm->expiration_interval <= 10)
1924 comm->expiration_interval = 11; /* Freeradius waits 10 minutes between successful TR queries*/
1925 if (comm->expiration_interval > 129600) /* 90 days*/
1926 comm->expiration_interval = 129600;
1932 talloc_steal(mem_ctx, comm);
1933 talloc_free(tmp_ctx);
1937 static TR_CFG_RC tr_cfg_parse_comms (TR_CFG *trc, json_t *jcfg)
1939 json_t *jcomms = NULL;
1940 TR_CFG_RC rc = TR_CFG_SUCCESS;
1941 TR_COMM *comm = NULL;
1944 if ((!trc) || (!jcfg)) {
1945 tr_debug("tr_cfg_parse_comms: Bad Parameters.");
1946 return TR_CFG_BAD_PARAMS;
1949 if (NULL != (jcomms = json_object_get(jcfg, "communities"))) {
1950 if (!json_is_array(jcomms)) {
1951 return TR_CFG_NOPARSE;
1954 for (i = 0; i < json_array_size(jcomms); i++) {
1955 if (NULL == (comm = tr_cfg_parse_one_comm(NULL, /* TODO: use a talloc context */
1957 json_array_get(jcomms, i),
1961 tr_debug("tr_cfg_parse_comms: Community configured: %s.",
1962 tr_comm_get_id(comm)->buf);
1964 tr_comm_table_add_comm(trc->ctable, comm);
1967 tr_debug("tr_cfg_parse_comms: Finished (rc=%d)", rc);
1971 TR_CFG_RC tr_cfg_validate(TR_CFG *trc)
1973 TR_CFG_RC rc = TR_CFG_SUCCESS;
1976 return TR_CFG_BAD_PARAMS;
1978 if ((NULL == trc->internal)||
1979 (NULL == trc->internal->hostname)) {
1980 tr_debug("tr_cfg_validate: Error: No internal configuration, or no hostname.");
1984 if (NULL == trc->rp_clients) {
1985 tr_debug("tr_cfg_validate: Error: No RP Clients configured");
1989 if (0==tr_comm_table_size(trc->ctable)) {
1990 tr_debug("tr_cfg_validate: Error: No Communities configured");
1994 if ((NULL == trc->default_servers) && (NULL == trc->ctable->idp_realms)) {
1995 tr_debug("tr_cfg_validate: Error: No default servers or IDPs configured.");
2002 /* Join two paths and return a pointer to the result. This should be freed
2003 * via talloc_free. Returns NULL on failure. */
2004 static char *join_paths(TALLOC_CTX *mem_ctx, const char *p1, const char *p2)
2006 return talloc_asprintf(mem_ctx, "%s/%s", p1, p2); /* returns NULL on a failure */
2009 static void tr_cfg_log_json_error(const char *label, json_error_t *rc)
2011 tr_debug("%s: JSON parse error on line %d: %s",
2017 TR_CFG_RC tr_cfg_parse_one_config_file(TR_CFG *cfg, const char *file_with_path)
2023 if (NULL==(jcfg=json_load_file(file_with_path,
2024 JSON_DISABLE_EOF_CHECK|JSON_REJECT_DUPLICATES, &rc))) {
2025 tr_debug("tr_cfg_parse_one_config_file: Error parsing config file %s.",
2027 tr_cfg_log_json_error("tr_cfg_parse_one_config_file", &rc);
2028 return TR_CFG_NOPARSE;
2031 // Look for serial number and log it if it exists
2032 if (NULL!=(jser=json_object_get(jcfg, "serial_number"))) {
2033 if (json_is_number(jser)) {
2034 tr_notice("tr_parse_one_config_file: Attempting to load revision %" JSON_INTEGER_FORMAT " of '%s'.",
2035 json_integer_value(jser),
2040 if ((TR_CFG_SUCCESS != tr_cfg_parse_internal(cfg, jcfg)) ||
2041 (TR_CFG_SUCCESS != tr_cfg_parse_local_orgs(cfg, jcfg)) ||
2042 (TR_CFG_SUCCESS != tr_cfg_parse_peer_orgs(cfg, jcfg)) ||
2043 (TR_CFG_SUCCESS != tr_cfg_parse_default_servers(cfg, jcfg)) ||
2044 (TR_CFG_SUCCESS != tr_cfg_parse_comms(cfg, jcfg)))
2045 return TR_CFG_ERROR;
2047 return TR_CFG_SUCCESS;
2050 /* Reads configuration files in config_dir ("" or "./" will use the current directory). */
2051 TR_CFG_RC tr_parse_config(TR_CFG_MGR *cfg_mgr, const char *config_dir, int n, struct dirent **cfg_files)
2053 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
2054 char *file_with_path;
2056 TR_CFG_RC cfg_rc=TR_CFG_ERROR;
2058 if ((!cfg_mgr) || (!cfg_files) || (n<=0)) {
2059 cfg_rc=TR_CFG_BAD_PARAMS;
2063 if (cfg_mgr->new != NULL)
2064 tr_cfg_free(cfg_mgr->new);
2065 cfg_mgr->new=tr_cfg_new(tmp_ctx); /* belongs to the temporary context for now */
2066 if (cfg_mgr->new == NULL) {
2067 cfg_rc=TR_CFG_NOMEM;
2071 cfg_mgr->new->peers=trp_ptable_new(cfg_mgr);
2073 /* Parse configuration information from each config file */
2074 for (ii=0; ii<n; ii++) {
2075 file_with_path=join_paths(tmp_ctx, config_dir, cfg_files[ii]->d_name); /* must free result with talloc_free */
2076 if(file_with_path == NULL) {
2077 tr_crit("tr_parse_config: error joining path.");
2078 cfg_rc=TR_CFG_NOMEM;
2081 tr_debug("tr_parse_config: Parsing %s.", cfg_files[ii]->d_name); /* print the filename without the path */
2082 cfg_rc=tr_cfg_parse_one_config_file(cfg_mgr->new, file_with_path);
2083 if (cfg_rc!=TR_CFG_SUCCESS) {
2084 tr_crit("tr_parse_config: Error parsing %s", file_with_path);
2087 talloc_free(file_with_path); /* done with filename */
2090 /* make sure we got a complete, consistent configuration */
2091 if (TR_CFG_SUCCESS != tr_cfg_validate(cfg_mgr->new)) {
2092 tr_err("tr_parse_config: Error: INVALID CONFIGURATION");
2093 cfg_rc=TR_CFG_ERROR;
2098 talloc_steal(cfg_mgr, cfg_mgr->new); /* hand this over to the cfg_mgr context */
2099 cfg_rc=TR_CFG_SUCCESS;
2102 talloc_free(tmp_ctx);
2106 TR_IDP_REALM *tr_cfg_find_idp (TR_CFG *tr_cfg, TR_NAME *idp_id, TR_CFG_RC *rc)
2109 TR_IDP_REALM *cfg_idp;
2111 if ((!tr_cfg) || (!idp_id)) {
2113 *rc = TR_CFG_BAD_PARAMS;
2117 for (cfg_idp = tr_cfg->ctable->idp_realms; NULL != cfg_idp; cfg_idp = cfg_idp->next) {
2118 if (!tr_name_cmp (idp_id, cfg_idp->realm_id)) {
2119 tr_debug("tr_cfg_find_idp: Found %s.", idp_id->buf);
2123 /* if we didn't find one, return NULL */
2127 TR_RP_CLIENT *tr_cfg_find_rp (TR_CFG *tr_cfg, TR_NAME *rp_gss, TR_CFG_RC *rc)
2129 TR_RP_CLIENT *cfg_rp;
2131 if ((!tr_cfg) || (!rp_gss)) {
2133 *rc = TR_CFG_BAD_PARAMS;
2137 for (cfg_rp = tr_cfg->rp_clients; NULL != cfg_rp; cfg_rp = cfg_rp->next) {
2138 if (tr_gss_names_matches(cfg_rp->gss_names, rp_gss)) {
2139 tr_debug("tr_cfg_find_rp: Found %s.", rp_gss->buf);
2143 /* if we didn't find one, return NULL */
2147 static int is_cfg_file(const struct dirent *dent) {
2150 /* Only accept filenames ending in ".cfg" and starting with a character
2151 * other than an ASCII '.' */
2153 /* filename must be at least 4 characters long to be acceptable */
2154 n=strlen(dent->d_name);
2159 /* filename must not start with '.' */
2160 if ('.' == dent->d_name[0]) {
2164 /* If the above passed and the last four characters of the filename are .cfg, accept.
2165 * (n.b., assumes an earlier test checked that the name is >= 4 chars long.) */
2166 if (0 == strcmp(&(dent->d_name[n-4]), ".cfg")) {
2170 /* otherwise, return false. */
2174 /* Find configuration files in a particular directory. Returns the
2175 * number of entries found, 0 if none are found, or <0 for some
2176 * errors. If n>=0, the cfg_files parameter will contain a newly
2177 * allocated array of pointers to struct dirent entries, as returned
2178 * by scandir(). These can be freed with tr_free_config_file_list().
2180 int tr_find_config_files (const char *config_dir, struct dirent ***cfg_files) {
2183 n = scandir(config_dir, cfg_files, is_cfg_file, alphasort);
2187 tr_debug("tr_find_config: scandir error trying to scan %s.", config_dir);
2193 /* Free memory allocated for configuration file list returned from tr_find_config_files().
2194 * This can be called regardless of the return value of tr_find_config_values(). */
2195 void tr_free_config_file_list(int n, struct dirent ***cfg_files) {
2198 /* if n < 0, then scandir did not allocate anything because it failed */
2199 if((n>=0) && (*cfg_files != NULL)) {
2200 for(ii=0; ii<n; ii++) {
2201 free((*cfg_files)[ii]);
2203 free(*cfg_files); /* safe even if n==0 */
2204 *cfg_files=NULL; /* this will help prevent accidentally freeing twice */