New-style configuration file loading partially implemented.
[trust_router.git] / common / tr_config.c
1 /*
2  * Copyright (c) 2012, JANET(UK)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
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.
15  *
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.
19  *
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.
32  *
33  */
34
35 #include <stdlib.h>
36 #include <string.h>
37 #include <jansson.h>
38 #include <dirent.h>
39 #include <talloc.h>
40
41 #include <tr_cfgwatch.h>
42 #include <tr_comm.h>
43 #include <tr_config.h>
44 #include <tr_debug.h>
45 #include <tr_filter.h>
46 #include <trust_router/tr_constraint.h>
47 #include <tr_idp.h>
48 #include <tr.h>
49
50 void tr_print_config (TR_CFG *cfg) {
51   tr_notice("tr_print_config: Logging running trust router configuration.");
52   tr_print_comms(cfg->comms);
53 }
54
55 void tr_print_comms (TR_COMM *comm_list) {
56   TR_COMM *comm = NULL;
57
58   for (comm = comm_list; NULL != comm; comm = comm->next) {
59     tr_notice("tr_print_config: Community %s:", comm->id->buf);
60
61     tr_notice("tr_print_config:  - Member IdPs:");
62     tr_print_comm_idps(comm->idp_realms);
63
64     tr_notice("tr_print_config:  - Member RPs:");
65     tr_print_comm_rps(comm->rp_realms);
66   }
67 }
68
69 void tr_print_comm_idps (TR_IDP_REALM *idp_list) {
70   TR_IDP_REALM *idp = NULL;
71
72   for (idp = idp_list; NULL != idp; idp = idp->comm_next) {
73     tr_notice("tr_print_config:    - @%s", idp->realm_id->buf);
74   }
75 }
76
77 void tr_print_comm_rps(TR_RP_REALM *rp_list) {
78   TR_RP_REALM *rp = NULL;
79
80   for (rp = rp_list; NULL != rp; rp = rp->next) {
81     tr_notice("tr_print_config:    - %s", rp->realm_name->buf);
82   }
83 }
84
85 TR_CFG *tr_cfg_new(TALLOC_CTX *mem_ctx)
86 {
87   return talloc_zero(mem_ctx, TR_CFG);
88 }
89
90 void tr_cfg_free (TR_CFG *cfg) {
91   talloc_free(cfg);
92 }
93
94 TR_CFG_MGR *tr_cfg_mgr_new(TALLOC_CTX *mem_ctx)
95 {
96   return talloc_zero(mem_ctx, TR_CFG_MGR);
97 }
98
99 void tr_cfg_mgr_free (TR_CFG_MGR *cfg_mgr) {
100   talloc_free(cfg_mgr);
101 }
102
103 TR_CFG_RC tr_apply_new_config (TR_CFG_MGR *cfg_mgr)
104 {
105   /* cfg_mgr->active is allowed to be null, but new cannot be */
106   if ((cfg_mgr==NULL) || (cfg_mgr->new==NULL))
107     return TR_CFG_BAD_PARAMS;
108
109   if (cfg_mgr->active != NULL)
110     tr_cfg_free(cfg_mgr->active);
111
112   cfg_mgr->active = cfg_mgr->new;
113   cfg_mgr->new=NULL; /* only keep a single handle on the new configuration */
114
115   tr_log_threshold(cfg_mgr->active->internal->log_threshold);
116   tr_console_threshold(cfg_mgr->active->internal->console_threshold);
117
118   return TR_CFG_SUCCESS;
119 }
120
121 static TR_CFG_RC tr_cfg_parse_internal(TR_CFG *trc, json_t *jcfg)
122 {
123   json_t *jint = NULL;
124   json_t *jmtd = NULL;
125   json_t *jtidsp = NULL;
126   json_t *jtrpsp = NULL;
127   json_t *jhname = NULL;
128   json_t *jlog = NULL;
129   json_t *jconthres = NULL;
130   json_t *jlogthres = NULL;
131   json_t *jcfgpoll = NULL;
132   json_t *jcfgsettle = NULL;
133   json_t *jroutesweep = NULL;
134   json_t *jrouteupdate = NULL;
135   json_t *jrouteconnect = NULL;
136
137   if ((!trc) || (!jcfg))
138     return TR_CFG_BAD_PARAMS;
139
140   if (NULL == trc->internal) {
141     if (NULL == (trc->internal = talloc_zero(trc, TR_CFG_INTERNAL)))
142       return TR_CFG_NOMEM;
143   }
144
145   if (NULL != (jint = json_object_get(jcfg, "tr_internal"))) {
146     if (NULL != (jmtd = json_object_get(jint, "max_tree_depth"))) {
147       if (json_is_number(jmtd)) {
148         trc->internal->max_tree_depth = json_integer_value(jmtd);
149       } else {
150         tr_debug("tr_cfg_parse_internal: Parsing error, max_tree_depth is not a number.");
151         return TR_CFG_NOPARSE;
152       }
153     } else {
154       /* If not configured, use the default */
155       trc->internal->max_tree_depth = TR_DEFAULT_MAX_TREE_DEPTH;
156     }
157     if (NULL != (jtidsp = json_object_get(jint, "tids_port"))) {
158       if (json_is_number(jtidsp)) {
159         trc->internal->tids_port = json_integer_value(jtidsp);
160       } else {
161         tr_debug("tr_cfg_parse_internal: Parsing error, tids_port is not a number.");
162         return TR_CFG_NOPARSE;
163       }
164     } else {
165       /* If not configured, use the default */
166       trc->internal->tids_port = TR_DEFAULT_TIDS_PORT;
167     }
168     if (NULL != (jtrpsp = json_object_get(jint, "trps_port"))) {
169       if (json_is_number(jtrpsp)) {
170         trc->internal->trps_port = json_integer_value(jtrpsp);
171       } else {
172         tr_debug("tr_cfg_parse_internal: Parsing error, trps_port is not a number.");
173         return TR_CFG_NOPARSE;
174       }
175     } else {
176       /* If not configured, use the default */
177       trc->internal->trps_port = TR_DEFAULT_TRPS_PORT;
178     }
179     if (NULL != (jhname = json_object_get(jint, "hostname"))) {
180       if (json_is_string(jhname)) {
181         trc->internal->hostname = json_string_value(jhname);
182       } else {
183         tr_debug("tr_cfg_parse_internal: Parsing error, hostname is not a string.");
184         return TR_CFG_NOPARSE;
185       }
186     }
187     if (NULL != (jcfgpoll = json_object_get(jint, "cfg_poll_interval"))) {
188       if (json_is_number(jcfgpoll)) {
189         trc->internal->cfg_poll_interval = json_integer_value(jcfgpoll);
190       } else {
191         tr_debug("tr_cfg_parse_internal: Parsing error, cfg_poll_interval is not a number.");
192         return TR_CFG_NOPARSE;
193       }
194     } else {
195       trc->internal->cfg_poll_interval = TR_CFGWATCH_DEFAULT_POLL;
196     }
197
198     if (NULL != (jcfgsettle = json_object_get(jint, "cfg_settling_time"))) {
199       if (json_is_number(jcfgsettle)) {
200         trc->internal->cfg_settling_time = json_integer_value(jcfgsettle);
201       } else {
202         tr_debug("tr_cfg_parse_internal: Parsing error, cfg_settling_time is not a number.");
203         return TR_CFG_NOPARSE;
204       }
205     } else {
206       trc->internal->cfg_settling_time = TR_CFGWATCH_DEFAULT_SETTLE;
207     }
208
209     if (NULL != (jrouteconnect = json_object_get(jint, "trp_connect_interval"))) {
210       if (json_is_number(jrouteconnect)) {
211         trc->internal->trp_connect_interval = json_integer_value(jrouteconnect);
212       } else {
213         tr_debug("tr_cfg_parse_internal: Parsing error, trp_connect_interval is not a number.");
214         return TR_CFG_NOPARSE;
215       }
216     } else {
217       /* if not configured, use the default */
218       trc->internal->trp_connect_interval=TR_DEFAULT_TRP_CONNECT_INTERVAL;
219     }
220
221     if (NULL != (jroutesweep = json_object_get(jint, "trp_sweep_interval"))) {
222       if (json_is_number(jroutesweep)) {
223         trc->internal->trp_sweep_interval = json_integer_value(jroutesweep);
224       } else {
225         tr_debug("tr_cfg_parse_internal: Parsing error, trp_sweep_interval is not a number.");
226         return TR_CFG_NOPARSE;
227       }
228     } else {
229       /* if not configured, use the default */
230       trc->internal->trp_sweep_interval=TR_DEFAULT_TRP_SWEEP_INTERVAL;
231     }
232
233     if (NULL != (jrouteupdate = json_object_get(jint, "trp_update_interval"))) {
234       if (json_is_number(jrouteupdate)) {
235         trc->internal->trp_update_interval = json_integer_value(jrouteupdate);
236       } else {
237         tr_debug("tr_cfg_parse_internal: Parsing error, trp_update_interval is not a number.");
238         return TR_CFG_NOPARSE;
239       }
240     } else {
241       /* if not configured, use the default */
242       trc->internal->trp_update_interval=TR_DEFAULT_TRP_UPDATE_INTERVAL;
243     }
244
245     if (NULL != (jlog = json_object_get(jint, "logging"))) {
246       if (NULL != (jlogthres = json_object_get(jlog, "log_threshold"))) {
247         if (json_is_string(jlogthres)) {
248           trc->internal->log_threshold = str2sev(json_string_value(jlogthres));
249         } else {
250           tr_debug("tr_cfg_parse_internal: Parsing error, log_threshold is not a string.");
251           return TR_CFG_NOPARSE;
252         }
253       } else {
254         /* If not configured, use the default */
255         trc->internal->log_threshold = TR_DEFAULT_LOG_THRESHOLD;
256       }
257
258       if (NULL != (jconthres = json_object_get(jlog, "console_threshold"))) {
259         if (json_is_string(jconthres)) {
260             trc->internal->console_threshold = str2sev(json_string_value(jconthres));
261         } else {
262           tr_debug("tr_cfg_parse_internal: Parsing error, console_threshold is not a string.");
263           return TR_CFG_NOPARSE;
264         }
265       } else {
266         /* If not configured, use the default */
267         trc->internal->console_threshold = TR_DEFAULT_CONSOLE_THRESHOLD;
268       }
269     } else {
270         /* If not configured, use the default */
271         trc->internal->console_threshold = TR_DEFAULT_CONSOLE_THRESHOLD;
272         trc->internal->log_threshold = TR_DEFAULT_LOG_THRESHOLD;
273     }
274
275     tr_debug("tr_cfg_parse_internal: Internal config parsed.");
276     return TR_CFG_SUCCESS;
277   }
278   return TR_CFG_SUCCESS;
279 }
280
281 static TR_CONSTRAINT *tr_cfg_parse_one_constraint(TALLOC_CTX *mem_ctx, char *ctype, json_t *jc, TR_CFG_RC *rc)
282 {
283   TR_CONSTRAINT *cons=NULL;
284   int i=0;
285
286   if ((!ctype) || (!jc) || (!rc) ||
287       (!json_is_array(jc)) ||
288       (0 >= json_array_size(jc)) ||
289       (TR_MAX_CONST_MATCHES < json_array_size(jc)) ||
290       (!json_is_string(json_array_get(jc, 0)))) {
291     tr_err("tr_cfg_parse_one_constraint: config error.");
292     *rc=TR_CFG_NOPARSE;
293     return NULL;
294   }
295
296   if (NULL==(cons=tr_constraint_new(mem_ctx))) {
297     tr_debug("tr_cfg_parse_one_constraint: Out of memory (cons).");
298     *rc=TR_CFG_NOMEM;
299     return NULL;
300   }
301
302   if (NULL==(cons->type=tr_new_name(ctype))) {
303     tr_err("tr_cfg_parse_one_constraint: Out of memory (type).");
304     *rc=TR_CFG_NOMEM;
305     tr_constraint_free(cons);
306     return NULL;
307   }
308
309   for (i=0; i < json_array_size(jc); i++) {
310     cons->matches[i]=tr_new_name(json_string_value(json_array_get(jc, i)));
311     if (cons->matches[i]==NULL) {
312       tr_err("tr_cfg_parse_one_constraint: Out of memory (match %d).", i+1);
313       *rc=TR_CFG_NOMEM;
314       tr_constraint_free(cons);
315       return NULL;
316     }
317   }
318
319   return cons;
320 }
321
322 static TR_FILTER *tr_cfg_parse_one_filter(TALLOC_CTX *mem_ctx, json_t *jfilt, TR_FILTER_TYPE ftype, TR_CFG_RC *rc)
323 {
324   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
325   TR_FILTER *filt=NULL;
326   json_t *jfaction=NULL;
327   json_t *jfspecs=NULL;
328   json_t *jffield=NULL;
329   json_t *jfmatch=NULL;
330   json_t *jrc=NULL;
331   json_t *jdc=NULL;
332   int i=0, j=0;
333
334   *rc=TR_CFG_ERROR;
335
336   if ((jfilt==NULL) || (rc==NULL)) {
337     tr_err("tr_cfg_parse_one_filter: null argument");
338     *rc=TR_CFG_BAD_PARAMS;
339     goto cleanup;
340   }
341     
342   if (NULL==(filt=tr_filter_new(tmp_ctx))) {
343     tr_err("tr_cfg_parse_one_filter: Out of memory.");
344     *rc=TR_CFG_NOMEM;
345     goto cleanup;
346   }
347   tr_filter_set_type(filt, ftype);
348
349   /* make sure we have space to represent the filter */
350   if (json_array_size(jfilt) > TR_MAX_FILTER_LINES) {
351     tr_err("tr_cfg_parse_one_filter: Filter has too many lines, maximum of %d.", TR_MAX_FILTER_LINES);
352     *rc=TR_CFG_NOPARSE;
353     goto cleanup;
354   }
355
356   /* For each entry in the filter... */
357   for (i=0; i < json_array_size(jfilt); i++) {
358     if ((NULL==(jfaction=json_object_get(json_array_get(jfilt, i), "action"))) ||
359         (!json_is_string(jfaction))) {
360       tr_debug("tr_cfg_parse_one_filter: Error parsing filter action.");
361       *rc=TR_CFG_NOPARSE;
362       goto cleanup;
363     }
364  
365     if ((NULL==(jfspecs=json_object_get(json_array_get(jfilt, i), "filter_specs"))) ||
366         (!json_is_array(jfspecs)) ||
367         (0==json_array_size(jfspecs))) {
368       tr_debug("tr_cfg_parse_one_filter: Error parsing filter specs.");
369       *rc=TR_CFG_NOPARSE;
370       goto cleanup;
371     }
372   
373     if (TR_MAX_FILTER_SPECS < json_array_size(jfspecs)) {
374       tr_debug("tr_cfg_parse_one_filter: Filter has too many filter_specs, maximimum of %d.", TR_MAX_FILTER_SPECS);
375       *rc=TR_CFG_NOPARSE;
376       goto cleanup;
377     }
378
379     if (NULL==(filt->lines[i]=tr_fline_new(filt))) {
380       tr_debug("tr_cfg_parse_one_filter: Out of memory allocating filter line %d.", i+1);
381       *rc=TR_CFG_NOMEM;
382       goto cleanup;
383     }
384
385     if (!strcmp(json_string_value(jfaction), "accept")) {
386       filt->lines[i]->action=TR_FILTER_ACTION_ACCEPT;
387     }
388     else if (!strcmp(json_string_value(jfaction), "reject")) {
389       filt->lines[i]->action=TR_FILTER_ACTION_REJECT;
390     }
391     else {
392       tr_debug("tr_cfg_parse_one_filter: Error parsing filter action, unknown action' %s'.", json_string_value(jfaction));
393       *rc=TR_CFG_NOPARSE;
394       goto cleanup;
395     }
396
397     if (NULL!=(jrc=json_object_get(json_array_get(jfilt, i), "realm_constraints"))) {
398       if (!json_is_array(jrc)) {
399         tr_err("tr_cfg_parse_one_filter: cannot parse realm_constraints, not an array.");
400         *rc=TR_CFG_NOPARSE;
401         goto cleanup;
402       } else if (json_array_size(jrc)>TR_MAX_CONST_MATCHES) {
403         tr_err("tr_cfg_parse_one_filter: realm_constraints has too many entries, maximum of %d.",
404                TR_MAX_CONST_MATCHES);
405         *rc=TR_CFG_NOPARSE;
406         goto cleanup;
407       } else if (json_array_size(jrc)>0) {
408         /* ok we actually have entries to process */
409         if (NULL==(filt->lines[i]->realm_cons=tr_cfg_parse_one_constraint(filt->lines[i], "realm", jrc, rc))) {
410           tr_debug("tr_cfg_parse_one_filter: Error parsing realm constraint");
411           *rc=TR_CFG_NOPARSE;
412           goto cleanup;
413         }
414       }
415     }
416   }
417
418   if (NULL!=(jdc=json_object_get(json_array_get(jfilt, i), "domain_constraints"))) {
419     if (!json_is_array(jdc)) {
420       tr_err("tr_cfg_parse_one_filter: cannot parse domain_constraints, not an array.");
421       *rc=TR_CFG_NOPARSE;
422       goto cleanup;
423     } else if (json_array_size(jdc)>TR_MAX_CONST_MATCHES) {
424       tr_err("tr_cfg_parse_one_filter: domain_constraints has too many entries, maximum of %d.",
425              TR_MAX_CONST_MATCHES);
426       *rc=TR_CFG_NOPARSE;
427       goto cleanup;
428     } else if (json_array_size(jdc)>0) {
429       if (NULL==(filt->lines[i]->domain_cons=tr_cfg_parse_one_constraint(filt->lines[i], "domain", jdc, rc))) {
430         tr_debug("tr_cfg_parse_one_filter: Error parsing domain constraint");
431         *rc=TR_CFG_NOPARSE;
432         goto cleanup;
433       }
434     }
435   }
436
437   /*For each filter spec within the filter line... */
438   for (j=0; j <json_array_size(jfspecs); j++) {
439     if ((NULL==(jffield=json_object_get(json_array_get(jfspecs, j), "field"))) ||
440         (!json_is_string(jffield)) ||
441         (NULL==(jfmatch=json_object_get(json_array_get(jfspecs, j), "match"))) ||
442         (!json_is_string(jfmatch))) {
443       tr_debug("tr_cfg_parse_one_filter: Error parsing filter field and match for filter spec %d, filter line %d.", i, j);
444       *rc=TR_CFG_NOPARSE;
445       goto cleanup;
446     }
447
448     if (NULL==(filt->lines[i]->specs[j]=tr_fspec_new(filt->lines[i]))) {
449       tr_debug("tr_cfg_parse_one_filter: Out of memory.");
450       *rc=TR_CFG_NOMEM;
451       goto cleanup;
452     }
453
454     if ((NULL==(filt->lines[i]->specs[j]->field=tr_new_name(json_string_value(jffield)))) ||
455           (NULL==(filt->lines[i]->specs[j]->match=tr_new_name(json_string_value(jfmatch))))) {
456       tr_debug("tr_cfg_parse_one_filter: Out of memory.");
457       *rc=TR_CFG_NOMEM;
458       goto cleanup;
459     }
460   }
461   
462   *rc=TR_CFG_SUCCESS;
463   talloc_steal(mem_ctx, filt);
464   
465  cleanup:
466   talloc_free(tmp_ctx);
467   return filt;
468 }
469
470 static TR_AAA_SERVER *tr_cfg_parse_one_aaa_server(TALLOC_CTX *mem_ctx, json_t *jaddr, TR_CFG_RC *rc)
471 {
472   TR_AAA_SERVER *aaa = NULL;
473   TR_NAME *name=NULL;
474
475   if ((!jaddr) || (!json_is_string(jaddr))) {
476     tr_debug("tr_cfg_parse_one_aaa_server: Bad parameters.");
477     *rc = TR_CFG_BAD_PARAMS;
478     return NULL;
479   }
480
481   name=tr_new_name(json_string_value(jaddr));
482   if (name==NULL) {
483     tr_debug("tr_cfg_parse_one_aaa_server: Out of memory allocating hostname.");
484     *rc = TR_CFG_NOMEM;
485     return NULL;
486   }
487
488   aaa=tr_aaa_server_new(mem_ctx, name);
489   if (aaa==NULL) {
490     tr_free_name(name);
491     tr_debug("tr_cfg_parse_one_aaa_server: Out of memory allocating AAA server.");
492     *rc = TR_CFG_NOMEM;
493     return NULL;
494   }
495
496   return aaa;
497 }
498
499 static TR_AAA_SERVER *tr_cfg_parse_aaa_servers(TALLOC_CTX *mem_ctx, json_t *jaaas, TR_CFG_RC *rc) 
500 {
501   TALLOC_CTX *tmp_ctx=NULL;
502   TR_AAA_SERVER *aaa = NULL;
503   TR_AAA_SERVER *temp_aaa = NULL;
504   int i = 0;
505
506   for (i = 0; i < json_array_size(jaaas); i++) {
507     /* rc gets set in here */
508     if (NULL == (temp_aaa = tr_cfg_parse_one_aaa_server(tmp_ctx, json_array_get(jaaas, i), rc))) {
509       talloc_free(tmp_ctx);
510       return NULL;
511     }
512     /* TBD -- IPv6 addresses */
513     //    tr_debug("tr_cfg_parse_aaa_servers: Configuring AAA Server: ip_addr = %s.", inet_ntoa(temp_aaa->aaa_server_addr));
514     temp_aaa->next = aaa;
515     aaa = temp_aaa;
516   }
517   tr_debug("tr_cfg_parse_aaa_servers: Finished (rc=%d)", *rc);
518
519   for (temp_aaa=aaa; temp_aaa!=NULL; temp_aaa=temp_aaa->next)
520     talloc_steal(mem_ctx, temp_aaa);
521   talloc_free(tmp_ctx);
522   return aaa;
523 }
524
525 static TR_APC *tr_cfg_parse_apc(TALLOC_CTX *mem_ctx, json_t *japc, TR_CFG_RC *rc)
526 {
527   TR_APC *apc=NULL;
528   TR_NAME *name=NULL;
529   
530   *rc = TR_CFG_SUCCESS;         /* presume success */
531
532   if ((!japc) || (!rc) || (!json_is_string(japc))) {
533     tr_debug("tr_cfg_parse_apc: Bad parameters.");
534     if (rc) 
535       *rc = TR_CFG_BAD_PARAMS;
536     return NULL;
537   }
538
539   apc=tr_apc_new(mem_ctx);
540   if (apc==NULL) {
541     tr_debug("tr_cfg_parse_apc: Out of memory.");
542     *rc = TR_CFG_NOMEM;
543     return NULL;
544   }
545
546   name=tr_new_name(json_string_value(japc));
547   if (name==NULL) {
548     tr_debug("tr_cfg_parse_apc: No memory for APC name.");
549     tr_apc_free(apc);
550     *rc = TR_CFG_NOMEM;
551     return NULL;
552   }
553   tr_apc_set_id(apc, name); /* apc is now responsible for freeing the name */
554
555   return apc;
556 }
557
558 static TR_NAME *tr_cfg_parse_name(TALLOC_CTX *mem_ctx, json_t *jname, TR_CFG_RC *rc)
559 {
560   TR_NAME *name=NULL;
561   *rc=TR_CFG_ERROR;
562
563   if ((jname==NULL) || (!json_is_string(jname))) {
564     tr_err("tr_cfg_parse_name: name missing or not a string");
565     *rc=TR_CFG_BAD_PARAMS;
566     name=NULL;
567   } else {
568     name=tr_new_name(json_string_value(jname));
569     if (name==NULL) {
570       tr_err("tr_cfg_parse_name: could not allocate name");
571       *rc=TR_CFG_NOMEM;
572     } else {
573       *rc=TR_CFG_SUCCESS;
574     }
575   }
576   return name;  
577 }
578
579 static int tr_cfg_parse_shared_config(json_t *jsc, TR_CFG_RC *rc)
580 {
581   const char *shared=NULL;
582   *rc=TR_CFG_SUCCESS;
583   
584   if ((jsc==NULL) ||
585       (!json_is_string(jsc)) ||
586       (NULL==(shared=json_string_value(jsc)))) {
587     *rc=TR_CFG_BAD_PARAMS;
588     return -1;
589   }
590
591   if (0==strcmp(shared, "no"))
592     return 0;
593   else if (0==strcmp(shared, "yes"))
594     return 1;
595
596   /* any other value is an error */
597   tr_debug("tr_cfg_parse_shared_config: invalid shared_config value \"%s\" (should be \"yes\" or \"no\")",
598            shared);
599   *rc=TR_CFG_NOPARSE;
600   return -1;
601 }
602
603 /* Parse the identity provider object from a realm and fill in the given TR_IDP_REALM. */
604 static TR_CFG_RC tr_cfg_parse_idp(TR_IDP_REALM *idp, json_t *jidp)
605 {
606   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
607   TR_APC *apcs=NULL;
608   TR_AAA_SERVER *aaa=NULL;
609   TR_CFG_RC rc=TR_CFG_ERROR;
610
611   if (jidp==NULL)
612     goto cleanup;
613
614   idp->origin=TR_REALM_LOCAL; /* if we're parsing it from a config file, it's local */
615   idp->shared_config=tr_cfg_parse_shared_config(json_object_get(jidp, "shared_config"), &rc);
616   if (rc!=TR_CFG_SUCCESS) {
617     tr_err("tr_cfg_parse_idp: missing or malformed shared_config specification");
618     rc=TR_CFG_NOPARSE;
619     goto cleanup;
620   }
621
622   apcs=tr_cfg_parse_apc(tmp_ctx, json_object_get(jidp, "apc"), &rc);
623   if ((rc!=TR_CFG_SUCCESS) || (apcs==NULL)) {
624     tr_err("tr_cfg_parse_idp: unable to parse APC");
625     rc=TR_CFG_NOPARSE;
626     goto cleanup;
627   }
628   tr_debug("tr_cfg_parse_idp: APC=\"%.*s\"",
629            apcs->id->len,
630            apcs->id->buf);
631
632   aaa=tr_cfg_parse_aaa_servers(idp, json_object_get(jidp, "aaa_servers"), &rc);
633   if ((rc!=TR_CFG_SUCCESS) || (aaa==NULL)) {
634     tr_err("tr_cfg_parse_idp: unable to parse AAA servers");
635     rc=TR_CFG_NOPARSE;
636     goto cleanup;
637   }
638
639   /* done, fill in the idp structures */
640   idp->apcs=apcs;
641   talloc_steal(idp, apcs);
642   idp->aaa_servers=aaa;
643   rc=TR_CFG_SUCCESS;
644
645  cleanup:
646   if (rc!=TR_CFG_SUCCESS) {
647     if (apcs!=NULL)
648       tr_apc_free(apcs);
649     if (aaa!=NULL)
650       tr_aaa_server_free(aaa);
651   }
652
653   talloc_free(tmp_ctx);
654   return rc;
655 }
656
657 /* parses idp realm */
658 static TR_IDP_REALM *tr_cfg_parse_one_idp_realm(TALLOC_CTX *mem_ctx, json_t *jrealm, TR_CFG_RC *rc)
659 {
660   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
661   TR_IDP_REALM *realm=NULL;
662   json_t *jremote=NULL;
663   json_t *jscfg=NULL;
664   json_t *jsrvrs=NULL;
665   json_t *japcs=NULL;
666   TR_CFG_RC call_rc=TR_CFG_ERROR;
667
668   *rc=TR_CFG_ERROR; /* default to error if not set */
669
670   if ((!jrealm) || (!rc)) {
671     tr_err("tr_cfg_parse_one_idp_realm: Bad parameters.");
672     if (rc)
673       *rc=TR_CFG_BAD_PARAMS;
674     goto cleanup;
675   }
676
677   if (NULL==(realm=tr_idp_realm_new(tmp_ctx))) {
678     tr_err("tr_cfg_parse_one_idp_realm: could not allocate idp realm.");
679     *rc=TR_CFG_NOMEM;
680     goto cleanup;
681   }
682
683   /* must have a name */
684   realm->realm_id=tr_cfg_parse_name(realm,
685                                     json_object_get(jrealm, "realm"),
686                                    &call_rc);
687   if ((call_rc!=TR_CFG_SUCCESS) || (realm->realm_id==NULL)) {
688     tr_err("tr_cfg_parse_one_idp_realm: could not parse realm name");
689     *rc=TR_CFG_NOPARSE;
690     goto cleanup;
691   }
692   tr_debug("tr_cfg_parse_one_idp_realm: realm_id=\"%.*s\"",
693            realm->realm_id->len,
694            realm->realm_id->buf);
695         
696   call_rc=tr_cfg_parse_idp(realm, json_object_get(jrealm, "identity_provider"));
697   if (call_rc!=TR_CFG_SUCCESS) {
698     tr_err("tr_cfg_parse_one_idp_realm: could not parse identity_provider.");
699     *rc=TR_CFG_NOPARSE;
700     goto cleanup;
701   }
702
703   *rc=TR_CFG_SUCCESS;
704
705   cleanup:
706     if (*rc==TR_CFG_SUCCESS)
707       talloc_steal(mem_ctx, realm);
708     else {
709       talloc_free(realm);
710       realm=NULL;
711     }
712
713     talloc_free(tmp_ctx);
714     return realm;
715   }
716
717   /* Determine whether the realm is an IDP realm */
718 static int tr_cfg_is_idp_realm(json_t *jrealm)
719 {
720   /* If a realm spec contains an identity_provider, it's an IDP realm. */
721   if (NULL != json_object_get(jrealm, "identity_provider"))
722     return 1;
723   else
724     return 0;
725 }
726
727 /* Parse any idp realms in the j_realms object. Ignores other realm types. */
728 static TR_IDP_REALM *tr_cfg_parse_idp_realms(TALLOC_CTX *mem_ctx, json_t *jrealms, TR_CFG_RC *rc)
729 {
730   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
731   TR_IDP_REALM *realms=NULL;
732   TR_IDP_REALM *new_realm=NULL;
733   json_t *this_jrealm=NULL;
734   int ii=0;
735
736   *rc=TR_CFG_ERROR;
737   if ((jrealms==NULL) || (!json_is_array(jrealms))) {
738     tr_err("tr_cfg_parse_idp_realms: realms not an array");
739     *rc=TR_CFG_BAD_PARAMS;
740     goto cleanup;
741   }
742
743   for (ii=0; ii<json_array_size(jrealms); ii++) {
744     this_jrealm=json_array_get(jrealms, ii);
745     if (tr_cfg_is_idp_realm(this_jrealm)) {
746       new_realm=tr_cfg_parse_one_idp_realm(tmp_ctx, this_jrealm, rc);
747       if ((*rc)!=TR_CFG_SUCCESS) {
748         tr_err("tr_cfg_parse_idp_realms: error decoding realm entry %d", ii+1);
749         *rc=TR_CFG_NOPARSE;
750         goto cleanup;
751       }
752       realms=tr_idp_realm_add(realms, new_realm);
753     }
754   }
755   
756   *rc=TR_CFG_SUCCESS;
757   talloc_steal(mem_ctx, realms);
758
759 cleanup:
760   talloc_free(tmp_ctx);
761   return realms;
762 }
763
764 /* takes a talloc context, but currently does not use it */
765 static TR_NAME *tr_cfg_parse_org_name(TALLOC_CTX *mem_ctx, json_t *j_org, TR_CFG_RC *rc)
766 {
767   TR_NAME *name=NULL;
768
769   if ((j_org==NULL) || (rc==NULL) || (!json_is_string(j_org))) {
770     tr_debug("tr_cfg_parse_org_name: Bad parameters.");
771     if (rc!=NULL)
772       *rc = TR_CFG_BAD_PARAMS; /* fill in return value if we can */
773     return NULL;
774   }
775
776   name=tr_new_name(json_string_value(j_org));
777   if (name==NULL)
778     *rc=TR_CFG_NOMEM;
779   else
780     *rc=TR_CFG_SUCCESS;
781   return name;
782 }
783
784 /* Update the community information with data from a new batch of IDP realms.
785  * May partially add realms if there is a failure, no guarantees.
786  * Call like comms=tr_comm_idp_update(comms, new_realms, &rc) */
787 static TR_COMM *tr_cfg_comm_idp_update(TALLOC_CTX *mem_ctx, TR_COMM *comms, TR_IDP_REALM *new_realms, TR_CFG_RC *rc)
788 {
789   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
790   TR_COMM *comm=NULL; /* community looked up in comms table */
791   TR_COMM *new_comms=NULL; /* new communities as we create them */
792   TR_IDP_REALM *realm=NULL;
793   TR_APC *apc=NULL; /* apc of one realm */
794
795   if (rc==NULL) {
796     *rc=TR_CFG_BAD_PARAMS;
797     goto cleanup;
798   }
799
800   /* start with an empty list communities, then fill that in */
801   for (realm=new_realms; realm!=NULL; realm=realm->next) {
802     for (apc=realm->apcs; apc!=NULL; apc=apc->next) {
803       comm=tr_comm_lookup(comms, apc->id);
804       if (comm==NULL) {
805         comm=tr_comm_new(tmp_ctx);
806         if (comm==NULL) {
807           tr_debug("tr_cfg_comm_idp_update: unable to allocate new community.");
808           *rc=TR_CFG_NOMEM;
809           goto cleanup;
810         }
811         /* fill in the community with info */
812         comm->type=TR_COMM_APC; /* realms added this way are in APCs */
813         comm->expiration_interval=TR_DEFAULT_APC_EXPIRATION_INTERVAL;
814         comm->id=tr_dup_name(apc->id);
815         tr_comm_add_idp_realm(comm, realm);
816         new_comms=tr_comm_add(new_comms, comm);
817       } else {
818         /* add this realm to the comm */
819         tr_comm_add_idp_realm(comm, realm);
820       }
821     }
822   }
823
824   /* we successfully built a list, add it to the other list */
825   comms=tr_comm_add(comms, new_comms);
826   talloc_steal(mem_ctx, comms);
827  cleanup:
828   talloc_free(tmp_ctx);
829   return comms;
830 }
831
832
833 static TR_CFG_RC tr_cfg_parse_one_local_org(TR_CFG *trc, json_t *jlorg)
834 {
835   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
836   TR_CFG_RC retval=TR_CFG_ERROR; /* our return code */
837   TR_CFG_RC rc=TR_CFG_ERROR; /* return code from functions we call */
838   TR_NAME *org_name=NULL;
839   json_t *j_org=NULL;
840   json_t *j_realms=NULL;
841   TR_IDP_REALM *new_idp_realms=NULL;
842   TR_RP_REALM *new_rp_realms=NULL;
843
844   tr_debug("tr_cfg_parse_one_local_org: parsing local organization");
845
846   /* get organization_name (optional) */
847   if (NULL==(j_org=json_object_get(jlorg, "organization_name"))) {
848     tr_debug("tr_cfg_parse_one_local_org: organization_name unspecified");
849   } else {
850     org_name=tr_cfg_parse_org_name(tmp_ctx, j_org, &rc);
851     if (rc==TR_CFG_SUCCESS) {
852       tr_debug("tr_cfg_parse_one_local_org: organization_name=\"%.*s\"",
853                org_name->len,
854                org_name->buf);
855       /* we don't actually do anything with this, but we could */
856       tr_free_name(org_name);
857       org_name=NULL; 
858     }
859   }
860
861   /* Now get realms. Allow this to be missing; even though that is a pointless organization entry,
862    * it's harmless. Report a warning because that might be unintentional. */
863   if (NULL==(j_realms=json_object_get(jlorg, "realms"))) {
864     tr_warning("tr_cfg_parse_one_local_org: warning - no realms in this local organization");
865   } else {
866     /* Allocate in the tmp_ctx so these will be cleaned up if we do not complete successfully. */
867     new_idp_realms=tr_cfg_parse_idp_realms(tmp_ctx, j_realms, &rc);
868     if (rc!=TR_CFG_SUCCESS)
869       goto cleanup;
870
871 #if 0
872     new_rp_realms=tr_cfg_parse_rp_realms(tmp_ctx, j_realms, &rc);
873     if (rc!=TR_CFG_SUCCESS)
874       goto cleanup;
875 #endif
876   }
877   retval=TR_CFG_SUCCESS;
878   
879 cleanup:
880   /* if we succeeded, link things to the configuration and move out of tmp context */
881   if (retval==TR_CFG_SUCCESS) {
882     if (new_idp_realms!=NULL) {
883       trc->idp_realms=tr_idp_realm_add(trc->idp_realms, new_idp_realms); /* fixes talloc contexts except for head*/
884       talloc_steal(trc, trc->idp_realms); /* make sure the head is in the right context */
885       trc->comms=tr_cfg_comm_idp_update(trc, trc->comms, new_idp_realms, &rc); /* put realm info in community table */
886     }
887 #if 0
888     if (new_rp_realms!=NULL)
889       trc->rp_realms=tr_rp_realm_add(trc->rp_realms, new_rp_realms); /* fixes talloc contexts */
890 #endif
891   }
892
893   talloc_free(tmp_ctx);
894   return rc;
895 }
896
897 /* Parse local organizations if present. Returns success if there are none. On failure, the configuration is unreliable. */
898 static TR_CFG_RC tr_cfg_parse_local_orgs(TR_CFG *trc, json_t *jcfg)
899 {
900   json_t *jlocorgs=NULL;
901   int ii=0;
902
903   jlocorgs=json_object_get(jcfg, "local_organizations");
904   if (jlocorgs==NULL)
905     return TR_CFG_SUCCESS;
906
907   if (!json_is_array(jlocorgs)) {
908     tr_err("tr_cfg_parse_local_orgs: local_organizations is not an array.");
909     return TR_CFG_NOPARSE;
910   }
911
912   for (ii=0; ii<json_array_size(jlocorgs); ii++) {
913     if (tr_cfg_parse_one_local_org(trc, json_array_get(jlocorgs, ii))!=TR_CFG_SUCCESS) {
914       tr_err("tr_cfg_parse_local_orgs: error parsing local_organization %d.", ii+1);
915       return TR_CFG_NOPARSE;
916     }
917   }
918
919   return TR_CFG_SUCCESS;
920 }
921              
922
923 TR_CFG_RC tr_cfg_validate(TR_CFG *trc)
924 {
925   TR_CFG_RC rc = TR_CFG_SUCCESS;
926
927   if (!trc)
928     return TR_CFG_BAD_PARAMS;
929
930   if ((NULL == trc->internal)||
931       (NULL == trc->internal->hostname)) {
932     tr_debug("tr_cfg_validate: Error: No internal configuration, or no hostname.");
933     rc = TR_CFG_ERROR;
934   }
935
936   if (NULL == trc->rp_clients) {
937     tr_debug("tr_cfg_validate: Error: No RP Clients configured");
938     rc = TR_CFG_ERROR;
939   }
940
941   if (NULL == trc->comms) {
942     tr_debug("tr_cfg_validate: Error: No Communities configured");
943     rc = TR_CFG_ERROR;
944   }
945
946   if ((NULL == trc->default_servers) && (NULL == trc->idp_realms)) {
947     tr_debug("tr_cfg_validate: Error: No default servers or IDPs configured.");
948     rc = TR_CFG_ERROR;
949   }
950   
951   return rc;
952 }
953
954 /* Join two paths and return a pointer to the result. This should be freed
955  * via talloc_free. Returns NULL on failure. */
956 static char *join_paths(TALLOC_CTX *mem_ctx, const char *p1, const char *p2)
957 {
958   return talloc_asprintf(mem_ctx, "%s/%s", p1, p2); /* returns NULL on a failure */
959 }
960
961 TR_CFG_RC tr_cfg_parse_one_config_file(TR_CFG *cfg, const char *file_with_path)
962 {
963   json_t *jcfg=NULL;
964   json_t *jser=NULL;
965   json_error_t rc;
966
967   if (NULL==(jcfg=json_load_file(file_with_path, 
968                                  JSON_DISABLE_EOF_CHECK, &rc))) {
969     tr_debug("tr_parse_config: Error parsing config file %s.", 
970              file_with_path);
971     return TR_CFG_NOPARSE;
972   }
973
974   // Look for serial number and log it if it exists
975   if (NULL!=(jser=json_object_get(jcfg, "serial_number"))) {
976     if (json_is_number(jser)) {
977       tr_notice("tr_read_config: Attempting to load revision %" JSON_INTEGER_FORMAT " of '%s'.",
978                 json_integer_value(jser),
979                 file_with_path);
980     }
981   }
982
983   /* TODO: parse using the new functions */
984 #if 0
985   if ((TR_CFG_SUCCESS != tr_cfg_parse_internal(cfg, jcfg)) ||
986       (TR_CFG_SUCCESS != tr_cfg_parse_rp_clients(cfg, jcfg)) ||
987       (TR_CFG_SUCCESS != tr_cfg_parse_idp_realms(cfg, jcfg)) ||
988       (TR_CFG_SUCCESS != tr_cfg_parse_default_servers(cfg, jcfg)) ||
989       (TR_CFG_SUCCESS != tr_cfg_parse_comms(cfg, jcfg))) {
990
991   }
992 #endif
993   if (TR_CFG_SUCCESS != tr_cfg_parse_local_orgs(cfg, jcfg))
994     return TR_CFG_ERROR;
995
996   return TR_CFG_SUCCESS;
997 }
998
999 /* Reads configuration files in config_dir ("" or "./" will use the current directory). */
1000 TR_CFG_RC tr_parse_config(TR_CFG_MGR *cfg_mgr, const char *config_dir, int n, struct dirent **cfg_files)
1001 {
1002   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1003   json_t *jcfg;
1004   json_t *jser;
1005   json_error_t rc;
1006   char *file_with_path;
1007   int ii;
1008   TR_CFG_RC cfg_rc=TR_CFG_ERROR;
1009
1010   if ((!cfg_mgr) || (!cfg_files) || (n<=0)) {
1011     cfg_rc=TR_CFG_BAD_PARAMS;
1012     goto cleanup;
1013   }
1014
1015   if (cfg_mgr->new != NULL)
1016     tr_cfg_free(cfg_mgr->new);
1017   cfg_mgr->new=tr_cfg_new(tmp_ctx); /* belongs to the temporary context for now */
1018   if (cfg_mgr->new == NULL) {
1019     cfg_rc=TR_CFG_NOMEM;
1020     goto cleanup;
1021   }
1022
1023   /* Parse configuration information from each config file */
1024   for (ii=0; ii<n; ii++) {
1025     file_with_path=join_paths(tmp_ctx, config_dir, cfg_files[ii]->d_name); /* must free result with talloc_free */
1026     if(file_with_path == NULL) {
1027       tr_crit("tr_parse_config: error joining path.");
1028       cfg_rc=TR_CFG_NOMEM;
1029       goto cleanup;
1030     }
1031     tr_debug("tr_parse_config: Parsing %s.", cfg_files[ii]->d_name); /* print the filename without the path */
1032     cfg_rc=tr_cfg_parse_one_config_file(cfg_mgr->new, file_with_path);
1033     if (cfg_rc!=TR_CFG_SUCCESS) {
1034       tr_crit("tr_parse_config: error parsing %s", file_with_path);
1035       goto cleanup;
1036     }
1037     talloc_free(file_with_path); /* done with filename */
1038   }
1039
1040   /* make sure we got a complete, consistent configuration */
1041   if (TR_CFG_SUCCESS != tr_cfg_validate(cfg_mgr->new)) {
1042     tr_err("tr_parse_config: Error: INVALID CONFIGURATION");
1043     cfg_rc=TR_CFG_ERROR;
1044     goto cleanup;
1045   }
1046
1047   /* success! */
1048   talloc_steal(cfg_mgr, cfg_mgr->new); /* hand this over to the cfg_mgr context */
1049   cfg_rc=TR_CFG_SUCCESS;
1050
1051 cleanup:
1052   talloc_free(tmp_ctx);
1053   return cfg_rc;
1054 }
1055
1056 TR_IDP_REALM *tr_cfg_find_idp (TR_CFG *tr_cfg, TR_NAME *idp_id, TR_CFG_RC *rc)
1057 {
1058
1059   TR_IDP_REALM *cfg_idp;
1060
1061   if ((!tr_cfg) || (!idp_id)) {
1062     if (rc)
1063       *rc = TR_CFG_BAD_PARAMS;
1064     return NULL;
1065   }
1066
1067   for (cfg_idp = tr_cfg->idp_realms; NULL != cfg_idp; cfg_idp = cfg_idp->next) {
1068     if (!tr_name_cmp (idp_id, cfg_idp->realm_id)) {
1069       tr_debug("tr_cfg_find_idp: Found %s.", idp_id->buf);
1070       return cfg_idp;
1071     }
1072   }
1073   /* if we didn't find one, return NULL */ 
1074   return NULL;
1075 }
1076
1077 TR_RP_CLIENT *tr_cfg_find_rp (TR_CFG *tr_cfg, TR_NAME *rp_gss, TR_CFG_RC *rc)
1078 {
1079   TR_RP_CLIENT *cfg_rp;
1080   int i;
1081
1082   if ((!tr_cfg) || (!rp_gss)) {
1083     if (rc)
1084       *rc = TR_CFG_BAD_PARAMS;
1085     return NULL;
1086   }
1087
1088   for (cfg_rp = tr_cfg->rp_clients; NULL != cfg_rp; cfg_rp = cfg_rp->next) {
1089     for (i = 0; i < TR_MAX_GSS_NAMES; i++) {
1090       if (!tr_name_cmp (rp_gss, cfg_rp->gss_names[i])) {
1091         tr_debug("tr_cfg_find_rp: Found %s.", rp_gss->buf);
1092         return cfg_rp;
1093       }
1094     }
1095   }
1096   /* if we didn't find one, return NULL */ 
1097   return NULL;
1098 }
1099
1100 static int is_cfg_file(const struct dirent *dent) {
1101   int n;
1102
1103   /* Only accept filenames ending in ".cfg" and starting with a character
1104    * other than an ASCII '.' */
1105
1106   /* filename must be at least 4 characters long to be acceptable */
1107   n=strlen(dent->d_name);
1108   if (n < 4) {
1109     return 0;
1110   }
1111
1112   /* filename must not start with '.' */
1113   if ('.' == dent->d_name[0]) {
1114     return 0;
1115   }
1116
1117   /* If the above passed and the last four characters of the filename are .cfg, accept.
1118    * (n.b., assumes an earlier test checked that the name is >= 4 chars long.) */
1119   if (0 == strcmp(&(dent->d_name[n-4]), ".cfg")) {
1120     return 1;
1121   }
1122
1123   /* otherwise, return false. */
1124   return 0;
1125 }
1126
1127 /* Find configuration files in a particular directory. Returns the
1128  * number of entries found, 0 if none are found, or <0 for some
1129  * errors. If n>=0, the cfg_files parameter will contain a newly
1130  * allocated array of pointers to struct dirent entries, as returned
1131  * by scandir(). These can be freed with tr_free_config_file_list().
1132  */
1133 int tr_find_config_files (const char *config_dir, struct dirent ***cfg_files) {
1134   int n = 0;
1135   
1136   n = scandir(config_dir, cfg_files, is_cfg_file, alphasort);
1137
1138   if (n < 0) {
1139     perror("scandir");
1140     tr_debug("tr_find_config: scandir error trying to scan %s.", config_dir);
1141   } 
1142
1143   return n;
1144 }
1145
1146 /* Free memory allocated for configuration file list returned from tr_find_config_files().
1147  * This can be called regardless of the return value of tr_find_config_values(). */
1148 void tr_free_config_file_list(int n, struct dirent ***cfg_files) {
1149   int ii;
1150
1151   /* if n < 0, then scandir did not allocate anything because it failed */
1152   if((n>=0) && (*cfg_files != NULL)) {
1153     for(ii=0; ii<n; ii++) {
1154       free((*cfg_files)[ii]);
1155     }
1156     free(*cfg_files); /* safe even if n==0 */
1157     *cfg_files=NULL; /* this will help prevent accidentally freeing twice */
1158   }
1159 }