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