Peer organizations now parsed and added to peer table.
[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_gss.h>
45 #include <tr_debug.h>
46 #include <tr_filter.h>
47 #include <trust_router/tr_constraint.h>
48 #include <tr_idp.h>
49 #include <tr.h>
50 #include <trust_router/trp.h>
51
52 void tr_print_config (TR_CFG *cfg) {
53   tr_notice("tr_print_config: Logging running trust router configuration.");
54   tr_print_comms(cfg->comms);
55 }
56
57 void tr_print_comms (TR_COMM *comm_list) {
58   TR_COMM *comm = NULL;
59
60   for (comm = comm_list; NULL != comm; comm = comm->next) {
61     tr_notice("tr_print_config: Community %s:", comm->id->buf);
62
63     tr_notice("tr_print_config:  - Member IdPs:");
64     tr_print_comm_idps(comm->idp_realms);
65
66     tr_notice("tr_print_config:  - Member RPs:");
67     tr_print_comm_rps(comm->rp_realms);
68   }
69 }
70
71 void tr_print_comm_idps (TR_IDP_REALM *idp_list) {
72   TR_IDP_REALM *idp = NULL;
73
74   for (idp = idp_list; NULL != idp; idp = idp->comm_next) {
75     tr_notice("tr_print_config:    - @%s", idp->realm_id->buf);
76   }
77 }
78
79 void tr_print_comm_rps(TR_RP_REALM *rp_list) {
80   TR_RP_REALM *rp = NULL;
81
82   for (rp = rp_list; NULL != rp; rp = rp->next) {
83     tr_notice("tr_print_config:    - %s", rp->realm_name->buf);
84   }
85 }
86
87 TR_CFG *tr_cfg_new(TALLOC_CTX *mem_ctx)
88 {
89   return talloc_zero(mem_ctx, TR_CFG);
90 }
91
92 void tr_cfg_free (TR_CFG *cfg) {
93   talloc_free(cfg);
94 }
95
96 TR_CFG_MGR *tr_cfg_mgr_new(TALLOC_CTX *mem_ctx)
97 {
98   return talloc_zero(mem_ctx, TR_CFG_MGR);
99 }
100
101 void tr_cfg_mgr_free (TR_CFG_MGR *cfg_mgr) {
102   talloc_free(cfg_mgr);
103 }
104
105 TR_CFG_RC tr_apply_new_config (TR_CFG_MGR *cfg_mgr)
106 {
107   /* cfg_mgr->active is allowed to be null, but new cannot be */
108   if ((cfg_mgr==NULL) || (cfg_mgr->new==NULL))
109     return TR_CFG_BAD_PARAMS;
110
111   if (cfg_mgr->active != NULL)
112     tr_cfg_free(cfg_mgr->active);
113
114   cfg_mgr->active = cfg_mgr->new;
115   cfg_mgr->new=NULL; /* only keep a single handle on the new configuration */
116
117   tr_log_threshold(cfg_mgr->active->internal->log_threshold);
118   tr_console_threshold(cfg_mgr->active->internal->console_threshold);
119
120   return TR_CFG_SUCCESS;
121 }
122
123 static TR_CFG_RC tr_cfg_parse_internal(TR_CFG *trc, json_t *jcfg)
124 {
125   json_t *jint = NULL;
126   json_t *jmtd = NULL;
127   json_t *jtidsp = NULL;
128   json_t *jtrpsp = NULL;
129   json_t *jhname = NULL;
130   json_t *jlog = NULL;
131   json_t *jconthres = NULL;
132   json_t *jlogthres = NULL;
133   json_t *jcfgpoll = NULL;
134   json_t *jcfgsettle = NULL;
135   json_t *jroutesweep = NULL;
136   json_t *jrouteupdate = NULL;
137   json_t *jrouteconnect = NULL;
138
139   if ((!trc) || (!jcfg))
140     return TR_CFG_BAD_PARAMS;
141
142   if (NULL == trc->internal) {
143     if (NULL == (trc->internal = talloc_zero(trc, TR_CFG_INTERNAL)))
144       return TR_CFG_NOMEM;
145   }
146
147   if (NULL != (jint = json_object_get(jcfg, "tr_internal"))) {
148     if (NULL != (jmtd = json_object_get(jint, "max_tree_depth"))) {
149       if (json_is_number(jmtd)) {
150         trc->internal->max_tree_depth = json_integer_value(jmtd);
151       } else {
152         tr_debug("tr_cfg_parse_internal: Parsing error, max_tree_depth is not a number.");
153         return TR_CFG_NOPARSE;
154       }
155     } else {
156       /* If not configured, use the default */
157       trc->internal->max_tree_depth = TR_DEFAULT_MAX_TREE_DEPTH;
158     }
159     if (NULL != (jtidsp = json_object_get(jint, "tids_port"))) {
160       if (json_is_number(jtidsp)) {
161         trc->internal->tids_port = json_integer_value(jtidsp);
162       } else {
163         tr_debug("tr_cfg_parse_internal: Parsing error, tids_port is not a number.");
164         return TR_CFG_NOPARSE;
165       }
166     } else {
167       /* If not configured, use the default */
168       trc->internal->tids_port = TR_DEFAULT_TIDS_PORT;
169     }
170     if (NULL != (jtrpsp = json_object_get(jint, "trps_port"))) {
171       if (json_is_number(jtrpsp)) {
172         trc->internal->trps_port = json_integer_value(jtrpsp);
173       } else {
174         tr_debug("tr_cfg_parse_internal: Parsing error, trps_port is not a number.");
175         return TR_CFG_NOPARSE;
176       }
177     } else {
178       /* If not configured, use the default */
179       trc->internal->trps_port = TR_DEFAULT_TRPS_PORT;
180     }
181     if (NULL != (jhname = json_object_get(jint, "hostname"))) {
182       if (json_is_string(jhname)) {
183         trc->internal->hostname = json_string_value(jhname);
184       } else {
185         tr_debug("tr_cfg_parse_internal: Parsing error, hostname is not a string.");
186         return TR_CFG_NOPARSE;
187       }
188     }
189     if (NULL != (jcfgpoll = json_object_get(jint, "cfg_poll_interval"))) {
190       if (json_is_number(jcfgpoll)) {
191         trc->internal->cfg_poll_interval = json_integer_value(jcfgpoll);
192       } else {
193         tr_debug("tr_cfg_parse_internal: Parsing error, cfg_poll_interval is not a number.");
194         return TR_CFG_NOPARSE;
195       }
196     } else {
197       trc->internal->cfg_poll_interval = TR_CFGWATCH_DEFAULT_POLL;
198     }
199
200     if (NULL != (jcfgsettle = json_object_get(jint, "cfg_settling_time"))) {
201       if (json_is_number(jcfgsettle)) {
202         trc->internal->cfg_settling_time = json_integer_value(jcfgsettle);
203       } else {
204         tr_debug("tr_cfg_parse_internal: Parsing error, cfg_settling_time is not a number.");
205         return TR_CFG_NOPARSE;
206       }
207     } else {
208       trc->internal->cfg_settling_time = TR_CFGWATCH_DEFAULT_SETTLE;
209     }
210
211     if (NULL != (jrouteconnect = json_object_get(jint, "trp_connect_interval"))) {
212       if (json_is_number(jrouteconnect)) {
213         trc->internal->trp_connect_interval = json_integer_value(jrouteconnect);
214       } else {
215         tr_debug("tr_cfg_parse_internal: Parsing error, trp_connect_interval is not a number.");
216         return TR_CFG_NOPARSE;
217       }
218     } else {
219       /* if not configured, use the default */
220       trc->internal->trp_connect_interval=TR_DEFAULT_TRP_CONNECT_INTERVAL;
221     }
222
223     if (NULL != (jroutesweep = json_object_get(jint, "trp_sweep_interval"))) {
224       if (json_is_number(jroutesweep)) {
225         trc->internal->trp_sweep_interval = json_integer_value(jroutesweep);
226       } else {
227         tr_debug("tr_cfg_parse_internal: Parsing error, trp_sweep_interval is not a number.");
228         return TR_CFG_NOPARSE;
229       }
230     } else {
231       /* if not configured, use the default */
232       trc->internal->trp_sweep_interval=TR_DEFAULT_TRP_SWEEP_INTERVAL;
233     }
234
235     if (NULL != (jrouteupdate = json_object_get(jint, "trp_update_interval"))) {
236       if (json_is_number(jrouteupdate)) {
237         trc->internal->trp_update_interval = json_integer_value(jrouteupdate);
238       } else {
239         tr_debug("tr_cfg_parse_internal: Parsing error, trp_update_interval is not a number.");
240         return TR_CFG_NOPARSE;
241       }
242     } else {
243       /* if not configured, use the default */
244       trc->internal->trp_update_interval=TR_DEFAULT_TRP_UPDATE_INTERVAL;
245     }
246
247     if (NULL != (jlog = json_object_get(jint, "logging"))) {
248       if (NULL != (jlogthres = json_object_get(jlog, "log_threshold"))) {
249         if (json_is_string(jlogthres)) {
250           trc->internal->log_threshold = str2sev(json_string_value(jlogthres));
251         } else {
252           tr_debug("tr_cfg_parse_internal: Parsing error, log_threshold is not a string.");
253           return TR_CFG_NOPARSE;
254         }
255       } else {
256         /* If not configured, use the default */
257         trc->internal->log_threshold = TR_DEFAULT_LOG_THRESHOLD;
258       }
259
260       if (NULL != (jconthres = json_object_get(jlog, "console_threshold"))) {
261         if (json_is_string(jconthres)) {
262             trc->internal->console_threshold = str2sev(json_string_value(jconthres));
263         } else {
264           tr_debug("tr_cfg_parse_internal: Parsing error, console_threshold is not a string.");
265           return TR_CFG_NOPARSE;
266         }
267       } else {
268         /* If not configured, use the default */
269         trc->internal->console_threshold = TR_DEFAULT_CONSOLE_THRESHOLD;
270       }
271     } else {
272         /* If not configured, use the default */
273         trc->internal->console_threshold = TR_DEFAULT_CONSOLE_THRESHOLD;
274         trc->internal->log_threshold = TR_DEFAULT_LOG_THRESHOLD;
275     }
276
277     tr_debug("tr_cfg_parse_internal: Internal config parsed.");
278     return TR_CFG_SUCCESS;
279   }
280   return TR_CFG_SUCCESS;
281 }
282
283 static TR_CONSTRAINT *tr_cfg_parse_one_constraint(TALLOC_CTX *mem_ctx, char *ctype, json_t *jc, TR_CFG_RC *rc)
284 {
285   TR_CONSTRAINT *cons=NULL;
286   int i=0;
287
288   if ((!ctype) || (!jc) || (!rc) ||
289       (!json_is_array(jc)) ||
290       (0 >= json_array_size(jc)) ||
291       (TR_MAX_CONST_MATCHES < json_array_size(jc)) ||
292       (!json_is_string(json_array_get(jc, 0)))) {
293     tr_err("tr_cfg_parse_one_constraint: config error.");
294     *rc=TR_CFG_NOPARSE;
295     return NULL;
296   }
297
298   if (NULL==(cons=tr_constraint_new(mem_ctx))) {
299     tr_debug("tr_cfg_parse_one_constraint: Out of memory (cons).");
300     *rc=TR_CFG_NOMEM;
301     return NULL;
302   }
303
304   if (NULL==(cons->type=tr_new_name(ctype))) {
305     tr_err("tr_cfg_parse_one_constraint: Out of memory (type).");
306     *rc=TR_CFG_NOMEM;
307     tr_constraint_free(cons);
308     return NULL;
309   }
310
311   for (i=0; i < json_array_size(jc); i++) {
312     cons->matches[i]=tr_new_name(json_string_value(json_array_get(jc, i)));
313     if (cons->matches[i]==NULL) {
314       tr_err("tr_cfg_parse_one_constraint: Out of memory (match %d).", i+1);
315       *rc=TR_CFG_NOMEM;
316       tr_constraint_free(cons);
317       return NULL;
318     }
319   }
320
321   return cons;
322 }
323
324 static TR_FILTER *tr_cfg_parse_one_filter(TALLOC_CTX *mem_ctx, json_t *jfilt, TR_FILTER_TYPE ftype, TR_CFG_RC *rc)
325 {
326   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
327   TR_FILTER *filt=NULL;
328   json_t *jfaction=NULL;
329   json_t *jfspecs=NULL;
330   json_t *jffield=NULL;
331   json_t *jfmatches=NULL;
332   json_t *jfmatch=NULL;
333   json_t *jrc=NULL;
334   json_t *jdc=NULL;
335   TR_NAME *name=NULL;
336   int i=0, j=0, k=0;
337
338   *rc=TR_CFG_ERROR;
339
340   if ((jfilt==NULL) || (rc==NULL)) {
341     tr_err("tr_cfg_parse_one_filter: null argument");
342     *rc=TR_CFG_BAD_PARAMS;
343     goto cleanup;
344   }
345     
346   if (NULL==(filt=tr_filter_new(tmp_ctx))) {
347     tr_err("tr_cfg_parse_one_filter: Out of memory.");
348     *rc=TR_CFG_NOMEM;
349     goto cleanup;
350   }
351   tr_filter_set_type(filt, ftype);
352
353   /* make sure we have space to represent the filter */
354   if (json_array_size(jfilt) > TR_MAX_FILTER_LINES) {
355     tr_err("tr_cfg_parse_one_filter: Filter has too many lines, maximum of %d.", TR_MAX_FILTER_LINES);
356     *rc=TR_CFG_NOPARSE;
357     goto cleanup;
358   }
359
360   /* For each entry in the filter... */
361   for (i=0; i < json_array_size(jfilt); i++) {
362     if ((NULL==(jfaction=json_object_get(json_array_get(jfilt, i), "action"))) ||
363         (!json_is_string(jfaction))) {
364       tr_debug("tr_cfg_parse_one_filter: Error parsing filter action.");
365       *rc=TR_CFG_NOPARSE;
366       goto cleanup;
367     }
368  
369     if ((NULL==(jfspecs=json_object_get(json_array_get(jfilt, i), "specs"))) ||
370         (!json_is_array(jfspecs)) ||
371         (0==json_array_size(jfspecs))) {
372       tr_debug("tr_cfg_parse_one_filter: Error parsing filter specs.");
373       *rc=TR_CFG_NOPARSE;
374       goto cleanup;
375     }
376   
377     if (TR_MAX_FILTER_SPECS < json_array_size(jfspecs)) {
378       tr_debug("tr_cfg_parse_one_filter: Filter has too many specs, maximimum of %d.", TR_MAX_FILTER_SPECS);
379       *rc=TR_CFG_NOPARSE;
380       goto cleanup;
381     }
382
383     if (NULL==(filt->lines[i]=tr_fline_new(filt))) {
384       tr_debug("tr_cfg_parse_one_filter: Out of memory allocating filter line %d.", i+1);
385       *rc=TR_CFG_NOMEM;
386       goto cleanup;
387     }
388
389     if (!strcmp(json_string_value(jfaction), "accept")) {
390       filt->lines[i]->action=TR_FILTER_ACTION_ACCEPT;
391     }
392     else if (!strcmp(json_string_value(jfaction), "reject")) {
393       filt->lines[i]->action=TR_FILTER_ACTION_REJECT;
394     }
395     else {
396       tr_debug("tr_cfg_parse_one_filter: Error parsing filter action, unknown action' %s'.", json_string_value(jfaction));
397       *rc=TR_CFG_NOPARSE;
398       goto cleanup;
399     }
400
401     if (NULL!=(jrc=json_object_get(json_array_get(jfilt, i), "realm_constraints"))) {
402       if (!json_is_array(jrc)) {
403         tr_err("tr_cfg_parse_one_filter: cannot parse realm_constraints, not an array.");
404         *rc=TR_CFG_NOPARSE;
405         goto cleanup;
406       } else if (json_array_size(jrc)>TR_MAX_CONST_MATCHES) {
407         tr_err("tr_cfg_parse_one_filter: realm_constraints has too many entries, maximum of %d.",
408                TR_MAX_CONST_MATCHES);
409         *rc=TR_CFG_NOPARSE;
410         goto cleanup;
411       } else if (json_array_size(jrc)>0) {
412         /* ok we actually have entries to process */
413         if (NULL==(filt->lines[i]->realm_cons=tr_cfg_parse_one_constraint(filt->lines[i], "realm", jrc, rc))) {
414           tr_debug("tr_cfg_parse_one_filter: Error parsing realm constraint");
415           *rc=TR_CFG_NOPARSE;
416           goto cleanup;
417         }
418       }
419     }
420
421     if (NULL!=(jdc=json_object_get(json_array_get(jfilt, i), "domain_constraints"))) {
422       if (!json_is_array(jdc)) {
423         tr_err("tr_cfg_parse_one_filter: cannot parse domain_constraints, not an array.");
424         *rc=TR_CFG_NOPARSE;
425         goto cleanup;
426       } else if (json_array_size(jdc)>TR_MAX_CONST_MATCHES) {
427         tr_err("tr_cfg_parse_one_filter: domain_constraints has too many entries, maximum of %d.",
428                TR_MAX_CONST_MATCHES);
429         *rc=TR_CFG_NOPARSE;
430         goto cleanup;
431       } else if (json_array_size(jdc)>0) {
432         if (NULL==(filt->lines[i]->domain_cons=tr_cfg_parse_one_constraint(filt->lines[i], "domain", jdc, rc))) {
433           tr_debug("tr_cfg_parse_one_filter: Error parsing domain constraint");
434           *rc=TR_CFG_NOPARSE;
435           goto cleanup;
436         }
437       }
438     }
439
440     /*For each filter spec within the filter line... */
441     for (j=0; j <json_array_size(jfspecs); j++) {
442       if ((NULL==(jffield=json_object_get(json_array_get(jfspecs, j), "field"))) ||
443           (!json_is_string(jffield))) {
444         tr_debug("tr_cfg_parse_one_filter: Error parsing filter: missing field for filer spec %d, filter line %d.", i, j);
445         *rc=TR_CFG_NOPARSE;
446         goto cleanup;
447       }
448
449       /* check that we have a match attribute */
450       if (NULL==(jfmatches=json_object_get(json_array_get(jfspecs, j), "match"))) {
451         tr_debug("tr_cfg_parse_one_filter: Error parsing filter: missing match for filer spec %d, filter line %d.", i, j);
452         *rc=TR_CFG_NOPARSE;
453         goto cleanup;
454       }
455
456       /* check that match is an array */
457       if (!json_is_array(jfmatches)) {
458         tr_debug("tr_cfg_parse_one_filter: Error parsing filter: match not an array for filter spec %d, filter line %d.", i, j);
459         *rc=TR_CFG_NOPARSE;
460         goto cleanup;
461       }
462
463       /* allocate the filter spec */
464       if (NULL==(filt->lines[i]->specs[j]=tr_fspec_new(filt->lines[i]))) {
465         tr_debug("tr_cfg_parse_one_filter: Out of memory.");
466         *rc=TR_CFG_NOMEM;
467         goto cleanup;
468       }
469
470       /* fill in the field */
471       if (NULL==(filt->lines[i]->specs[j]->field=tr_new_name(json_string_value(jffield)))) {
472         tr_debug("tr_cfg_parse_one_filter: Out of memory.");
473         *rc=TR_CFG_NOMEM;
474         goto cleanup;
475       }
476
477       /* fill in the matches */
478       for (k=0; k<json_array_size(jfmatches); k++) {
479         if (NULL==(jfmatch=json_array_get(jfmatches, k))) {
480           tr_debug("tr_cfg_parse_one_filter: Error parsing filter: unable to load match %d for filter spec %d, filter line %d.", k, i, j); 
481           *rc=TR_CFG_NOPARSE;
482           goto cleanup;
483         }
484         if (NULL==(name=tr_new_name(json_string_value(jfmatch)))) {
485           tr_debug("tr_cfg_parse_one_filter: Out of memory.");
486           *rc=TR_CFG_NOMEM;
487           goto cleanup;
488         }
489         if (0!=tr_fspec_add_match(filt->lines[i]->specs[j], name)) {
490           tr_debug("tr_cfg_parse_one_filter: Could not add match %d to filter spec %d, filter line %d.", k, i, j);
491           tr_free_name(name);
492           *rc=TR_CFG_ERROR;
493           goto cleanup;
494         }
495       }
496
497     }
498   }
499   *rc=TR_CFG_SUCCESS;
500   talloc_steal(mem_ctx, filt);
501   
502  cleanup:
503   talloc_free(tmp_ctx);
504   if (*rc!=TR_CFG_SUCCESS)
505     filt=NULL;
506   return filt;
507 }
508
509 static TR_FILTER *tr_cfg_parse_filters(TALLOC_CTX *mem_ctx, json_t *jfilts, TR_CFG_RC *rc)
510 {
511   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
512   json_t *jfilt;
513   TR_FILTER *filt=NULL;
514
515   *rc=TR_CFG_ERROR;
516
517   /* no filters */
518   if (jfilts==NULL) {
519     *rc=TR_CFG_SUCCESS;
520     goto cleanup;
521   }
522
523   jfilt=json_object_get(jfilts, "tid_inbound");
524   if (jfilt!=NULL) {
525     filt=tr_cfg_parse_one_filter(tmp_ctx, jfilt, TR_FILTER_TYPE_TID_INCOMING, rc);
526     if (*rc!=TR_CFG_SUCCESS) {
527       tr_debug("tr_cfg_parse_filters: Error parsing tid_inbound filter.");
528       *rc=TR_CFG_NOPARSE;
529       goto cleanup;
530     }
531   } else {
532     tr_debug("tr_cfg_parse_filters: Unknown filter types in filter block.");
533     *rc=TR_CFG_NOPARSE;
534     goto cleanup;
535   }
536   
537   *rc=TR_CFG_SUCCESS;
538
539  cleanup:
540   if (*rc==TR_CFG_SUCCESS)
541     talloc_steal(mem_ctx, filt);
542   else if (filt!=NULL) {
543     talloc_free(filt);
544     filt=NULL;
545   }
546
547   talloc_free(tmp_ctx);
548   return filt;
549 }
550
551 static TR_AAA_SERVER *tr_cfg_parse_one_aaa_server(TALLOC_CTX *mem_ctx, json_t *jaddr, TR_CFG_RC *rc)
552 {
553   TR_AAA_SERVER *aaa = NULL;
554   TR_NAME *name=NULL;
555
556   if ((!jaddr) || (!json_is_string(jaddr))) {
557     tr_debug("tr_cfg_parse_one_aaa_server: Bad parameters.");
558     *rc = TR_CFG_BAD_PARAMS;
559     return NULL;
560   }
561
562   name=tr_new_name(json_string_value(jaddr));
563   if (name==NULL) {
564     tr_debug("tr_cfg_parse_one_aaa_server: Out of memory allocating hostname.");
565     *rc = TR_CFG_NOMEM;
566     return NULL;
567   }
568
569   aaa=tr_aaa_server_new(mem_ctx, name);
570   if (aaa==NULL) {
571     tr_free_name(name);
572     tr_debug("tr_cfg_parse_one_aaa_server: Out of memory allocating AAA server.");
573     *rc = TR_CFG_NOMEM;
574     return NULL;
575   }
576
577   return aaa;
578 }
579
580 static TR_AAA_SERVER *tr_cfg_parse_aaa_servers(TALLOC_CTX *mem_ctx, json_t *jaaas, TR_CFG_RC *rc) 
581 {
582   TALLOC_CTX *tmp_ctx=NULL;
583   TR_AAA_SERVER *aaa = NULL;
584   TR_AAA_SERVER *temp_aaa = NULL;
585   int i = 0;
586
587   for (i = 0; i < json_array_size(jaaas); i++) {
588     /* rc gets set in here */
589     if (NULL == (temp_aaa = tr_cfg_parse_one_aaa_server(tmp_ctx, json_array_get(jaaas, i), rc))) {
590       talloc_free(tmp_ctx);
591       return NULL;
592     }
593     /* TBD -- IPv6 addresses */
594     //    tr_debug("tr_cfg_parse_aaa_servers: Configuring AAA Server: ip_addr = %s.", inet_ntoa(temp_aaa->aaa_server_addr));
595     temp_aaa->next = aaa;
596     aaa = temp_aaa;
597   }
598   tr_debug("tr_cfg_parse_aaa_servers: Finished (rc=%d)", *rc);
599
600   for (temp_aaa=aaa; temp_aaa!=NULL; temp_aaa=temp_aaa->next)
601     talloc_steal(mem_ctx, temp_aaa);
602   talloc_free(tmp_ctx);
603   return aaa;
604 }
605
606 static TR_APC *tr_cfg_parse_one_apc(TALLOC_CTX *mem_ctx, json_t *japc, TR_CFG_RC *rc)
607 {
608   TR_APC *apc=NULL;
609   TR_NAME *name=NULL;
610   
611   *rc = TR_CFG_SUCCESS;         /* presume success */
612
613   if ((!japc) || (!rc) || (!json_is_string(japc))) {
614     tr_debug("tr_cfg_parse_one_apc: Bad parameters.");
615     if (rc) 
616       *rc = TR_CFG_BAD_PARAMS;
617     return NULL;
618   }
619
620   apc=tr_apc_new(mem_ctx);
621   if (apc==NULL) {
622     tr_debug("tr_cfg_parse_one_apc: Out of memory.");
623     *rc = TR_CFG_NOMEM;
624     return NULL;
625   }
626
627   name=tr_new_name(json_string_value(japc));
628   if (name==NULL) {
629     tr_debug("tr_cfg_parse_one_apc: No memory for APC name.");
630     tr_apc_free(apc);
631     *rc = TR_CFG_NOMEM;
632     return NULL;
633   }
634   tr_apc_set_id(apc, name); /* apc is now responsible for freeing the name */
635
636   return apc;
637 }
638
639 static TR_APC *tr_cfg_parse_apcs(TALLOC_CTX *mem_ctx, json_t *japcs, TR_CFG_RC *rc)
640 {
641   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
642   TR_APC *apcs=NULL;
643   TR_APC *new_apc=NULL;
644   int ii=0;
645   TR_CFG_RC call_rc=TR_CFG_ERROR;
646   
647   *rc = TR_CFG_SUCCESS;         /* presume success */
648
649   if ((!japcs) || (!rc) || (!json_is_array(japcs))) {
650     tr_debug("tr_cfg_parse_one_apc: Bad parameters.");
651     if (rc) 
652       *rc = TR_CFG_BAD_PARAMS;
653     return NULL;
654   }
655
656   for (ii=0; ii<json_array_size(japcs); ii++) {
657     new_apc=tr_cfg_parse_one_apc(tmp_ctx, json_array_get(japcs, ii), &call_rc);
658     if ((call_rc!=TR_CFG_SUCCESS) || (new_apc==NULL)) {
659       tr_debug("tr_cfg_parse_apcs: Error parsing APC %d.", ii+1);
660       *rc=TR_CFG_NOPARSE;
661       goto cleanup;
662     }
663     apcs=tr_apc_add(apcs, new_apc);
664   }
665
666   talloc_steal(mem_ctx, apcs);
667   *rc=TR_CFG_SUCCESS;
668
669  cleanup:
670   talloc_free(tmp_ctx);
671   return apcs;
672 }
673
674 static TR_NAME *tr_cfg_parse_name(TALLOC_CTX *mem_ctx, json_t *jname, TR_CFG_RC *rc)
675 {
676   TR_NAME *name=NULL;
677   *rc=TR_CFG_ERROR;
678
679   if ((jname==NULL) || (!json_is_string(jname))) {
680     tr_err("tr_cfg_parse_name: name missing or not a string");
681     *rc=TR_CFG_BAD_PARAMS;
682     name=NULL;
683   } else {
684     name=tr_new_name(json_string_value(jname));
685     if (name==NULL) {
686       tr_err("tr_cfg_parse_name: could not allocate name");
687       *rc=TR_CFG_NOMEM;
688     } else {
689       *rc=TR_CFG_SUCCESS;
690     }
691   }
692   return name;  
693 }
694
695 static int tr_cfg_parse_shared_config(json_t *jsc, TR_CFG_RC *rc)
696 {
697   const char *shared=NULL;
698   *rc=TR_CFG_SUCCESS;
699   
700   if ((jsc==NULL) ||
701       (!json_is_string(jsc)) ||
702       (NULL==(shared=json_string_value(jsc)))) {
703     *rc=TR_CFG_BAD_PARAMS;
704     return -1;
705   }
706
707   if (0==strcmp(shared, "no"))
708     return 0;
709   else if (0==strcmp(shared, "yes"))
710     return 1;
711
712   /* any other value is an error */
713   tr_debug("tr_cfg_parse_shared_config: invalid shared_config value \"%s\" (should be \"yes\" or \"no\")",
714            shared);
715   *rc=TR_CFG_NOPARSE;
716   return -1;
717 }
718
719 /* Parse the identity provider object from a realm and fill in the given TR_IDP_REALM. */
720 static TR_CFG_RC tr_cfg_parse_idp(TR_IDP_REALM *idp, json_t *jidp)
721 {
722   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
723   TR_APC *apcs=NULL;
724   TR_AAA_SERVER *aaa=NULL;
725   TR_CFG_RC rc=TR_CFG_ERROR;
726
727   if (jidp==NULL)
728     goto cleanup;
729
730   idp->origin=TR_REALM_LOCAL; /* if we're parsing it from a config file, it's local */
731   idp->shared_config=tr_cfg_parse_shared_config(json_object_get(jidp, "shared_config"), &rc);
732   if (rc!=TR_CFG_SUCCESS) {
733     tr_err("tr_cfg_parse_idp: missing or malformed shared_config specification");
734     rc=TR_CFG_NOPARSE;
735     goto cleanup;
736   }
737
738   apcs=tr_cfg_parse_apcs(tmp_ctx, json_object_get(jidp, "apcs"), &rc);
739   if ((rc!=TR_CFG_SUCCESS) || (apcs==NULL)) {
740     tr_err("tr_cfg_parse_idp: unable to parse APC");
741     rc=TR_CFG_NOPARSE;
742     goto cleanup;
743   }
744   tr_debug("tr_cfg_parse_idp: APC=\"%.*s\"",
745            apcs->id->len,
746            apcs->id->buf);
747
748   aaa=tr_cfg_parse_aaa_servers(idp, json_object_get(jidp, "aaa_servers"), &rc);
749   if (rc!=TR_CFG_SUCCESS) {
750     tr_err("tr_cfg_parse_idp: unable to parse AAA servers");
751     rc=TR_CFG_NOPARSE;
752     goto cleanup;
753   }
754
755   /* done, fill in the idp structures */
756   idp->apcs=apcs;
757   talloc_steal(idp, apcs);
758   idp->aaa_servers=aaa;
759   rc=TR_CFG_SUCCESS;
760
761  cleanup:
762   if (rc!=TR_CFG_SUCCESS) {
763     if (apcs!=NULL)
764       tr_apc_free(apcs);
765     if (aaa!=NULL)
766       tr_aaa_server_free(aaa);
767   }
768
769   talloc_free(tmp_ctx);
770   return rc;
771 }
772
773 /* parses idp realm */
774 static TR_IDP_REALM *tr_cfg_parse_one_idp_realm(TALLOC_CTX *mem_ctx, json_t *jrealm, TR_CFG_RC *rc)
775 {
776   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
777   TR_IDP_REALM *realm=NULL;
778   TR_CFG_RC call_rc=TR_CFG_ERROR;
779
780   *rc=TR_CFG_ERROR; /* default to error if not set */
781
782   if ((!jrealm) || (!rc)) {
783     tr_err("tr_cfg_parse_one_idp_realm: Bad parameters.");
784     if (rc)
785       *rc=TR_CFG_BAD_PARAMS;
786     goto cleanup;
787   }
788
789   if (NULL==(realm=tr_idp_realm_new(tmp_ctx))) {
790     tr_err("tr_cfg_parse_one_idp_realm: could not allocate idp realm.");
791     *rc=TR_CFG_NOMEM;
792     goto cleanup;
793   }
794
795   /* must have a name */
796   realm->realm_id=tr_cfg_parse_name(realm,
797                                     json_object_get(jrealm, "realm"),
798                                    &call_rc);
799   if ((call_rc!=TR_CFG_SUCCESS) || (realm->realm_id==NULL)) {
800     tr_err("tr_cfg_parse_one_idp_realm: could not parse realm name");
801     *rc=TR_CFG_NOPARSE;
802     goto cleanup;
803   }
804   tr_debug("tr_cfg_parse_one_idp_realm: realm_id=\"%.*s\"",
805            realm->realm_id->len,
806            realm->realm_id->buf);
807         
808   call_rc=tr_cfg_parse_idp(realm, json_object_get(jrealm, "identity_provider"));
809   if (call_rc!=TR_CFG_SUCCESS) {
810     tr_err("tr_cfg_parse_one_idp_realm: could not parse identity_provider.");
811     *rc=TR_CFG_NOPARSE;
812     goto cleanup;
813   }
814
815   *rc=TR_CFG_SUCCESS;
816
817   cleanup:
818     if (*rc==TR_CFG_SUCCESS)
819       talloc_steal(mem_ctx, realm);
820     else {
821       talloc_free(realm);
822       realm=NULL;
823     }
824
825     talloc_free(tmp_ctx);
826     return realm;
827   }
828
829   /* Determine whether the realm is an IDP realm */
830 static int tr_cfg_is_idp_realm(json_t *jrealm)
831 {
832   /* If a realm spec contains an identity_provider, it's an IDP realm. */
833   if (NULL != json_object_get(jrealm, "identity_provider"))
834     return 1;
835   else
836     return 0;
837 }
838
839 /* Parse any idp realms in the j_realms object. Ignores other realm types. */
840 static TR_IDP_REALM *tr_cfg_parse_idp_realms(TALLOC_CTX *mem_ctx, json_t *jrealms, TR_CFG_RC *rc)
841 {
842   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
843   TR_IDP_REALM *realms=NULL;
844   TR_IDP_REALM *new_realm=NULL;
845   json_t *this_jrealm=NULL;
846   int ii=0;
847
848   *rc=TR_CFG_ERROR;
849   if ((jrealms==NULL) || (!json_is_array(jrealms))) {
850     tr_err("tr_cfg_parse_idp_realms: realms not an array");
851     *rc=TR_CFG_BAD_PARAMS;
852     goto cleanup;
853   }
854
855   for (ii=0; ii<json_array_size(jrealms); ii++) {
856     this_jrealm=json_array_get(jrealms, ii);
857     if (tr_cfg_is_idp_realm(this_jrealm)) {
858       new_realm=tr_cfg_parse_one_idp_realm(tmp_ctx, this_jrealm, rc);
859       if ((*rc)!=TR_CFG_SUCCESS) {
860         tr_err("tr_cfg_parse_idp_realms: error decoding realm entry %d", ii+1);
861         *rc=TR_CFG_NOPARSE;
862         goto cleanup;
863       }
864       realms=tr_idp_realm_add(realms, new_realm);
865     }
866   }
867   
868   *rc=TR_CFG_SUCCESS;
869   talloc_steal(mem_ctx, realms);
870
871 cleanup:
872   talloc_free(tmp_ctx);
873   return realms;
874 }
875
876 static TR_GSS_NAMES *tr_cfg_parse_gss_names(TALLOC_CTX *mem_ctx, json_t *jgss_names, TR_CFG_RC *rc)
877 {
878   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
879   TR_GSS_NAMES *gn=NULL;
880   json_t *jname=NULL;
881   int ii=0;
882   TR_NAME *name=NULL;
883
884   if ((rc==NULL) || (jgss_names==NULL)) {
885     tr_err("tr_cfg_parse_gss_names: Bad parameters.");
886     *rc=TR_CFG_BAD_PARAMS;
887
888   }
889
890   if (!json_is_array(jgss_names)) {
891     tr_err("tr_cfg_parse_gss_names: gss_names not an array.");
892     *rc=TR_CFG_NOPARSE;
893     goto cleanup;
894   }
895
896   gn=tr_gss_names_new(tmp_ctx);
897   for (ii=0; ii<json_array_size(jgss_names); ii++) {
898     jname=json_array_get(jgss_names, ii);
899     if (!json_is_string(jname)) {
900       tr_err("tr_cfg_parse_gss_names: Encountered non-string gss name.");
901       *rc=TR_CFG_NOPARSE;
902       goto cleanup;
903     }
904
905     name=tr_new_name(json_string_value(jname));
906     if (name==NULL) {
907       tr_err("tr_cfg_parse_gss_names: Out of memory allocating gss name.");
908       *rc=TR_CFG_NOMEM;
909       goto cleanup;
910     }
911
912     if (tr_gss_names_add(gn, name)!=0) {
913       tr_free_name(name);
914       tr_err("tr_cfg_parse_gss_names: Unable to add gss name to RP client.");
915       *rc=TR_CFG_ERROR;
916       goto cleanup;
917     }
918   }
919
920   talloc_steal(mem_ctx, gn);
921   *rc=TR_CFG_SUCCESS;
922
923  cleanup:
924   talloc_free(tmp_ctx);
925   if ((*rc!=TR_CFG_SUCCESS) && (gn!=NULL))
926     gn=NULL;
927   return gn;
928 }
929
930 /* parses rp client */
931 static TR_RP_CLIENT *tr_cfg_parse_one_rp_client(TALLOC_CTX *mem_ctx, json_t *jrealm, TR_CFG_RC *rc)
932 {
933   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
934   TR_RP_CLIENT *client=NULL;
935   TR_CFG_RC call_rc=TR_CFG_ERROR;
936   TR_FILTER *new_filt=NULL;
937
938   *rc=TR_CFG_ERROR; /* default to error if not set */
939
940   if ((!jrealm) || (!rc)) {
941     tr_err("tr_cfg_parse_one_rp_client: Bad parameters.");
942     if (rc)
943       *rc=TR_CFG_BAD_PARAMS;
944     goto cleanup;
945   }
946
947   if (NULL==(client=tr_rp_client_new(tmp_ctx))) {
948     tr_err("tr_cfg_parse_one_rp_client: could not allocate rp client.");
949     *rc=TR_CFG_NOMEM;
950     goto cleanup;
951   }
952
953   client->gss_names=tr_cfg_parse_gss_names(client, json_object_get(jrealm, "gss_names"), &call_rc);
954
955   if (call_rc!=TR_CFG_SUCCESS) {
956     tr_err("tr_cfg_parse_one_rp_client: could not parse gss_names.");
957     *rc=TR_CFG_NOPARSE;
958     goto cleanup;
959   }
960
961   /* parse filters */
962   new_filt=tr_cfg_parse_filters(tmp_ctx, json_object_get(jrealm, "filters"), &call_rc);
963   if (call_rc!=TR_CFG_SUCCESS) {
964     tr_err("tr_cfg_parse_one_rp_client: could not parse filters.");
965     *rc=TR_CFG_NOPARSE;
966     goto cleanup;
967   }
968
969   tr_rp_client_set_filter(client, new_filt);
970   *rc=TR_CFG_SUCCESS;
971
972   cleanup:
973     if (*rc==TR_CFG_SUCCESS)
974       talloc_steal(mem_ctx, client);
975     else {
976       talloc_free(client);
977       client=NULL;
978     }
979
980     talloc_free(tmp_ctx);
981     return client;
982   }
983
984   /* Determine whether the realm is an RP realm */
985 static int tr_cfg_is_rp_realm(json_t *jrealm)
986 {
987   /* Check that we have a gss name. */
988   if (NULL != json_object_get(jrealm, "gss_names"))
989     return 1;
990   else
991     return 0;
992 }
993
994 /* Parse any rp clients in the j_realms object. Ignores other realms. */
995 static TR_RP_CLIENT *tr_cfg_parse_rp_clients(TALLOC_CTX *mem_ctx, json_t *jrealms, TR_CFG_RC *rc)
996 {
997   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
998   TR_RP_CLIENT *clients=NULL;
999   TR_RP_CLIENT *new_client=NULL;
1000   json_t *this_jrealm=NULL;
1001   int ii=0;
1002
1003   *rc=TR_CFG_ERROR;
1004   if ((jrealms==NULL) || (!json_is_array(jrealms))) {
1005     tr_err("tr_cfg_parse_rp_clients: realms not an array");
1006     *rc=TR_CFG_BAD_PARAMS;
1007     goto cleanup;
1008   }
1009
1010   for (ii=0; ii<json_array_size(jrealms); ii++) {
1011     this_jrealm=json_array_get(jrealms, ii);
1012     if (tr_cfg_is_rp_realm(this_jrealm)) {
1013       new_client=tr_cfg_parse_one_rp_client(tmp_ctx, this_jrealm, rc);
1014       if ((*rc)!=TR_CFG_SUCCESS) {
1015         tr_err("tr_cfg_parse_rp_clients: error decoding realm entry %d", ii+1);
1016         *rc=TR_CFG_NOPARSE;
1017         goto cleanup;
1018       }
1019       clients=tr_rp_client_add(clients, new_client);
1020     }
1021   }
1022   
1023   *rc=TR_CFG_SUCCESS;
1024   talloc_steal(mem_ctx, clients);
1025
1026 cleanup:
1027   talloc_free(tmp_ctx);
1028   return clients;
1029 }
1030
1031 /* takes a talloc context, but currently does not use it */
1032 static TR_NAME *tr_cfg_parse_org_name(TALLOC_CTX *mem_ctx, json_t *j_org, TR_CFG_RC *rc)
1033 {
1034   TR_NAME *name=NULL;
1035
1036   if ((j_org==NULL) || (rc==NULL) || (!json_is_string(j_org))) {
1037     tr_debug("tr_cfg_parse_org_name: Bad parameters.");
1038     if (rc!=NULL)
1039       *rc = TR_CFG_BAD_PARAMS; /* fill in return value if we can */
1040     return NULL;
1041   }
1042
1043   name=tr_new_name(json_string_value(j_org));
1044   if (name==NULL)
1045     *rc=TR_CFG_NOMEM;
1046   else
1047     *rc=TR_CFG_SUCCESS;
1048   return name;
1049 }
1050
1051 #if 0
1052 /* Update the community information with data from a new batch of IDP realms.
1053  * May partially add realms if there is a failure, no guarantees.
1054  * Call like comms=tr_comm_idp_update(comms, new_realms, &rc) */
1055 static TR_COMM *tr_cfg_comm_idp_update(TALLOC_CTX *mem_ctx, TR_COMM *comms, TR_IDP_REALM *new_realms, TR_CFG_RC *rc)
1056 {
1057   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1058   TR_COMM *comm=NULL; /* community looked up in comms table */
1059   TR_COMM *new_comms=NULL; /* new communities as we create them */
1060   TR_IDP_REALM *realm=NULL;
1061   TR_APC *apc=NULL; /* apc of one realm */
1062
1063   if (rc==NULL) {
1064     *rc=TR_CFG_BAD_PARAMS;
1065     goto cleanup;
1066   }
1067
1068   /* start with an empty list communities, then fill that in */
1069   for (realm=new_realms; realm!=NULL; realm=realm->next) {
1070     for (apc=realm->apcs; apc!=NULL; apc=apc->next) {
1071       comm=tr_comm_lookup(comms, apc->id);
1072       if (comm==NULL) {
1073         comm=tr_comm_new(tmp_ctx);
1074         if (comm==NULL) {
1075           tr_debug("tr_cfg_comm_idp_update: unable to allocate new community.");
1076           *rc=TR_CFG_NOMEM;
1077           goto cleanup;
1078         }
1079         /* fill in the community with info */
1080         comm->type=TR_COMM_APC; /* realms added this way are in APCs */
1081         comm->expiration_interval=TR_DEFAULT_APC_EXPIRATION_INTERVAL;
1082         comm->id=tr_dup_name(apc->id);
1083         tr_comm_add_idp_realm(comm, realm);
1084         new_comms=tr_comm_add(new_comms, comm);
1085       } else {
1086         /* add this realm to the comm */
1087         tr_comm_add_idp_realm(comm, realm);
1088       }
1089     }
1090   }
1091
1092   /* we successfully built a list, add it to the other list */
1093   comms=tr_comm_add(comms, new_comms);
1094   talloc_steal(mem_ctx, comms);
1095  cleanup:
1096   talloc_free(tmp_ctx);
1097   return comms;
1098 }
1099 #endif
1100
1101 static TR_CFG_RC tr_cfg_parse_one_local_org(TR_CFG *trc, json_t *jlorg)
1102 {
1103   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1104   TR_CFG_RC retval=TR_CFG_ERROR; /* our return code */
1105   TR_CFG_RC rc=TR_CFG_ERROR; /* return code from functions we call */
1106   TR_NAME *org_name=NULL;
1107   json_t *j_org=NULL;
1108   json_t *j_realms=NULL;
1109   TR_IDP_REALM *new_idp_realms=NULL;
1110   TR_RP_CLIENT *new_rp_clients=NULL;
1111
1112   tr_debug("tr_cfg_parse_one_local_org: parsing local organization");
1113
1114   /* get organization_name (optional) */
1115   if (NULL==(j_org=json_object_get(jlorg, "organization_name"))) {
1116     tr_debug("tr_cfg_parse_one_local_org: organization_name unspecified");
1117   } else {
1118     org_name=tr_cfg_parse_org_name(tmp_ctx, j_org, &rc);
1119     if (rc==TR_CFG_SUCCESS) {
1120       tr_debug("tr_cfg_parse_one_local_org: organization_name=\"%.*s\"",
1121                org_name->len,
1122                org_name->buf);
1123       /* we don't actually do anything with this, but we could */
1124       tr_free_name(org_name);
1125       org_name=NULL; 
1126     }
1127   }
1128
1129   /* Now get realms. Allow this to be missing; even though that is a pointless organization entry,
1130    * it's harmless. Report a warning because that might be unintentional. */
1131   if (NULL==(j_realms=json_object_get(jlorg, "realms"))) {
1132     tr_warning("tr_cfg_parse_one_local_org: warning - no realms in this local organization");
1133   } else {
1134     /* Allocate in the tmp_ctx so these will be cleaned up if we do not complete successfully. */
1135     new_idp_realms=tr_cfg_parse_idp_realms(tmp_ctx, j_realms, &rc);
1136     if (rc!=TR_CFG_SUCCESS)
1137       goto cleanup;
1138
1139     new_rp_clients=tr_cfg_parse_rp_clients(tmp_ctx, j_realms, &rc);
1140     if (rc!=TR_CFG_SUCCESS)
1141       goto cleanup;
1142   }
1143   retval=TR_CFG_SUCCESS;
1144   
1145 cleanup:
1146   /* if we succeeded, link things to the configuration and move out of tmp context */
1147   if (retval==TR_CFG_SUCCESS) {
1148     if (new_idp_realms!=NULL) {
1149       trc->idp_realms=tr_idp_realm_add(trc->idp_realms, new_idp_realms); /* fixes talloc contexts except for head*/
1150       talloc_steal(trc, trc->idp_realms); /* make sure the head is in the right context */
1151     }
1152
1153     if (new_rp_clients!=NULL) {
1154       trc->rp_clients=tr_rp_client_add(trc->rp_clients, new_rp_clients); /* fixes talloc contexts */
1155       talloc_steal(trc, trc->rp_clients); /* make sure head is in the right context */
1156     }
1157   }
1158
1159   talloc_free(tmp_ctx);
1160   return rc;
1161 }
1162
1163 /* Parse local organizations if present. Returns success if there are none. On failure, the configuration is unreliable. */
1164 static TR_CFG_RC tr_cfg_parse_local_orgs(TR_CFG *trc, json_t *jcfg)
1165 {
1166   json_t *jlocorgs=NULL;
1167   int ii=0;
1168
1169   jlocorgs=json_object_get(jcfg, "local_organizations");
1170   if (jlocorgs==NULL)
1171     return TR_CFG_SUCCESS;
1172
1173   if (!json_is_array(jlocorgs)) {
1174     tr_err("tr_cfg_parse_local_orgs: local_organizations is not an array.");
1175     return TR_CFG_NOPARSE;
1176   }
1177
1178   for (ii=0; ii<json_array_size(jlocorgs); ii++) {
1179     if (tr_cfg_parse_one_local_org(trc, json_array_get(jlocorgs, ii))!=TR_CFG_SUCCESS) {
1180       tr_err("tr_cfg_parse_local_orgs: error parsing local_organization %d.", ii+1);
1181       return TR_CFG_NOPARSE;
1182     }
1183   }
1184
1185   return TR_CFG_SUCCESS;
1186 }
1187
1188 static TR_CFG_RC tr_cfg_parse_one_peer_org(TR_CFG *trc, json_t *jporg)
1189 {
1190   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1191   json_t *jhost=NULL;
1192   json_t *jport=NULL;
1193   json_t *jgss=NULL;
1194   TRP_PEER *new_peer=NULL;
1195   TR_GSS_NAMES *names=NULL;
1196   TR_CFG_RC rc=TR_CFG_ERROR;
1197
1198   jhost=json_object_get(jporg, "hostname");
1199   jport=json_object_get(jporg, "port");
1200   jgss=json_object_get(jporg, "gss_names");
1201
1202   if ((jhost==NULL) || (!json_is_string(jhost))) {
1203     tr_err("tr_cfg_parse_one_peer_org: hostname not specified or not a string.");
1204     rc=TR_CFG_NOPARSE;
1205     goto cleanup;
1206   }
1207
1208   if ((jport!=NULL) && (!json_is_number(jport))) {
1209     /* note that not specifying the port is allowed, but if set it must be a number */
1210     tr_err("tr_cfg_parse_one_peer_org: port is not a number.");
1211     rc=TR_CFG_NOPARSE;
1212     goto cleanup;
1213   }
1214   
1215   if ((jgss==NULL) || (!json_is_array(jgss))) {
1216     tr_err("tr_cfg_parse_one_peer_org: gss_names not specified or not an array.");
1217     rc=TR_CFG_NOPARSE;
1218     goto cleanup;
1219   }
1220
1221   new_peer=trp_peer_new(tmp_ctx);
1222   if (new_peer==NULL) {
1223     tr_err("tr_cfg_parse_one_peer_org: could not allocate new peer.");
1224     rc=TR_CFG_NOMEM;
1225     goto cleanup;
1226   }
1227
1228   trp_peer_set_server(new_peer, json_string_value(jhost));
1229   if (jport==NULL)
1230     trp_peer_set_port(new_peer, TRP_PORT);
1231   else
1232     trp_peer_set_port(new_peer, json_integer_value(jport));
1233
1234   names=tr_cfg_parse_gss_names(tmp_ctx, jgss, &rc);
1235   if (rc!=TR_CFG_SUCCESS) {
1236     tr_err("tr_cfg_parse_one_peer_org: unable to parse gss names.");
1237     rc=TR_CFG_NOPARSE;
1238     goto cleanup;
1239   }
1240   trp_peer_set_gss_names(new_peer, names);
1241
1242   /* success! */
1243   trp_ptable_add(trc->peers, new_peer);
1244   rc=TR_CFG_SUCCESS;
1245
1246  cleanup:
1247   talloc_free(tmp_ctx);
1248   return rc;
1249 }
1250
1251 /* Parse peer organizations, if present. Returns success if there are none. */
1252 static TR_CFG_RC tr_cfg_parse_peer_orgs(TR_CFG *trc, json_t *jcfg)
1253 {
1254   json_t *jpeerorgs=NULL;
1255   int ii=0;
1256
1257   jpeerorgs=json_object_get(jcfg, "peer_organizations");
1258   if (jpeerorgs==NULL)
1259     return TR_CFG_SUCCESS;
1260
1261   if (!json_is_array(jpeerorgs)) {
1262     tr_err("tr_cfg_parse_peer_orgs: peer_organizations is not an array.");
1263     return TR_CFG_NOPARSE;
1264   }
1265
1266   for (ii=0; ii<json_array_size(jpeerorgs); ii++) {
1267     if (tr_cfg_parse_one_peer_org(trc, json_array_get(jpeerorgs, ii))!=TR_CFG_SUCCESS) {
1268       tr_err("tr_cfg_parse_peer_orgs: error parsing peer_organization %d.", ii+1);
1269       return TR_CFG_NOPARSE;
1270     }
1271   }
1272
1273   return TR_CFG_SUCCESS;
1274 }
1275              
1276 static TR_CFG_RC tr_cfg_parse_default_servers (TR_CFG *trc, json_t *jcfg) 
1277 {
1278   json_t *jdss = NULL;
1279   TR_CFG_RC rc = TR_CFG_SUCCESS;
1280   TR_AAA_SERVER *ds = NULL;
1281   int i = 0;
1282
1283   if ((!trc) || (!jcfg))
1284     return TR_CFG_BAD_PARAMS;
1285
1286   /* If there are default servers, store them */
1287   if ((NULL != (jdss = json_object_get(jcfg, "default_servers"))) &&
1288       (json_is_array(jdss)) &&
1289       (0 < json_array_size(jdss))) {
1290
1291     for (i = 0; i < json_array_size(jdss); i++) {
1292       if (NULL == (ds = tr_cfg_parse_one_aaa_server(trc,
1293                                                     json_array_get(jdss, i), 
1294                                                    &rc))) {
1295         return rc;
1296       }
1297       tr_debug("tr_cfg_parse_default_servers: Default server configured: %s", ds->hostname->buf);
1298       ds->next = trc->default_servers;
1299       trc->default_servers = ds;
1300     }
1301   } 
1302
1303   tr_debug("tr_cfg_parse_default_servers: Finished (rc=%d)", rc);
1304   return rc;
1305 }
1306
1307 static TR_IDP_REALM *tr_cfg_parse_comm_idps (TR_CFG *trc, json_t *jidps, TR_CFG_RC *rc)
1308 {
1309   TR_IDP_REALM *idp = NULL;
1310   TR_IDP_REALM *found_idp = NULL;
1311   TR_IDP_REALM *temp_idp = NULL;
1312   int i = 0;
1313
1314   if ((!trc) ||
1315       (!jidps) ||
1316       (!json_is_array(jidps))) {
1317     if (rc)
1318       *rc = TR_CFG_BAD_PARAMS;
1319     return NULL;
1320   }
1321
1322   for (i = 0; i < json_array_size(jidps); i++) {
1323     if (NULL == (temp_idp = talloc(trc, TR_IDP_REALM))) {
1324       tr_debug("tr_cfg_parse_comm_idps: Can't allocate memory for IdP Realm.");
1325       if (rc)
1326         *rc = TR_CFG_NOMEM;
1327       return NULL;
1328     }
1329     memset (temp_idp, 0, sizeof(TR_IDP_REALM));
1330     
1331     if (NULL == (found_idp = (tr_cfg_find_idp(trc, 
1332                                               tr_new_name((char *)json_string_value(json_array_get(jidps, i))), 
1333                                               rc)))) {
1334       tr_debug("tr_cfg_parse_comm_idps: Unknown IDP %s.", 
1335                (char *)json_string_value(json_array_get(jidps, i)));
1336       return NULL;
1337     }
1338
1339     // We *MUST* do a dereferenced copy here or the second community will corrupt the linked list we create here.
1340     *temp_idp = *found_idp;
1341
1342     temp_idp->comm_next = idp;
1343     idp = temp_idp;
1344   }
1345
1346   return idp;
1347 }
1348
1349 static TR_RP_REALM *tr_cfg_parse_comm_rps (TR_CFG *trc, json_t *jrps, TR_CFG_RC *rc)
1350 {
1351   TR_RP_REALM *rp = NULL;
1352   TR_RP_REALM *temp_rp = NULL;
1353   int i = 0;
1354
1355   if ((!trc) ||
1356       (!jrps) ||
1357       (!json_is_array(jrps))) {
1358     if (rc)
1359       *rc = TR_CFG_BAD_PARAMS;
1360     return NULL;
1361   }
1362
1363   for (i = (json_array_size(jrps)-1); i >= 0; i--) {
1364     if (NULL == (temp_rp = talloc_zero(trc, TR_RP_REALM))) {
1365       tr_debug("tr_cfg_parse_comm_rps: Can't allocate memory for RP Realm.");
1366       if (rc)
1367         *rc = TR_CFG_NOMEM;
1368       return NULL;
1369     }
1370
1371     if (NULL == (temp_rp->realm_name = tr_new_name((char *)json_string_value(json_array_get(jrps, i))))) {
1372       tr_debug("tr_cfg_parse_comm_rps: No memory for RP Realm Name.");
1373       if (rc)
1374         *rc = TR_CFG_NOMEM;
1375       return NULL;
1376     }
1377
1378     temp_rp->next = rp;
1379     rp = temp_rp;
1380   }
1381
1382   return rp;
1383 }
1384
1385 static TR_COMM *tr_cfg_parse_one_comm (TR_CFG *trc, json_t *jcomm, TR_CFG_RC *rc) {
1386   TR_COMM *comm = NULL;
1387   json_t *jid = NULL;
1388   json_t *jtype = NULL;
1389   json_t *japcs = NULL;
1390   json_t *jidps = NULL;
1391   json_t *jrps = NULL;
1392
1393   if ((!trc) || (!jcomm) || (!rc)) {
1394     tr_debug("tr_cfg_parse_one_comm: Bad parameters.");
1395     if (rc)
1396       *rc = TR_CFG_BAD_PARAMS;
1397     return NULL;
1398   }
1399
1400   if (NULL == (comm = talloc_zero(trc, TR_COMM))) {
1401     tr_crit("tr_cfg_parse_one_comm: Out of memory.");
1402     *rc = TR_CFG_NOMEM;
1403     return NULL;
1404   }
1405
1406
1407   if ((NULL == (jid = json_object_get(jcomm, "community_id"))) ||
1408       (!json_is_string(jid)) ||
1409       (NULL == (jtype = json_object_get(jcomm, "type"))) ||
1410       (!json_is_string(jtype)) ||
1411       (NULL == (japcs = json_object_get(jcomm, "apcs"))) ||
1412       (!json_is_array(japcs)) ||
1413       (NULL == (jidps = json_object_get(jcomm, "idp_realms"))) ||
1414       (!json_is_array(jidps)) ||
1415       (NULL == (jrps = json_object_get(jcomm, "rp_realms"))) ||
1416       (!json_is_array(jrps))) {
1417     tr_debug("tr_cfg_parse_one_comm: Error parsing Communities configuration.");
1418     *rc = TR_CFG_NOPARSE;
1419     return NULL;
1420   }
1421
1422   if (NULL == (comm->id = tr_new_name((char *)json_string_value(jid)))) {
1423     tr_debug("tr_cfg_parse_one_comm: No memory for community id.");
1424     *rc = TR_CFG_NOMEM;
1425     return NULL;
1426   }
1427
1428   if (0 == strcmp(json_string_value(jtype), "apc")) {
1429     comm->type = TR_COMM_APC;
1430   } else if (0 == strcmp(json_string_value(jtype), "coi")) {
1431     comm->type = TR_COMM_COI;
1432     if (NULL == (comm->apcs = tr_cfg_parse_apcs(trc, japcs, rc))) {
1433       tr_debug("tr_cfg_parse_one_comm: Can't parse APCs for COI %s.", comm->id->buf);
1434       tr_free_name(comm->id);
1435       return NULL;
1436     }
1437   } else {
1438     tr_debug("tr_cfg_parse_one_comm: Invalid community type, comm = %s, type = %s", comm->id->buf, json_string_value(jtype));
1439     tr_free_name(comm->id);
1440     *rc = TR_CFG_NOPARSE;
1441     return NULL;
1442   }
1443
1444   comm->idp_realms = tr_cfg_parse_comm_idps(trc, jidps, rc);
1445   if (TR_CFG_SUCCESS != *rc) {
1446     tr_debug("tr_cfg_parse_one_comm: Can't parse IDP realms for comm %s.", comm->id->buf);
1447     tr_free_name(comm->id);
1448     return NULL;
1449   }
1450
1451   comm->rp_realms = tr_cfg_parse_comm_rps(trc, jrps, rc);
1452   if (TR_CFG_SUCCESS != *rc) {
1453     tr_debug("tr_cfg_parse_comm: Can't parse RP realms for comm %s .", comm->id->buf);
1454     tr_free_name(comm->id);
1455     return NULL;
1456   }
1457
1458   if (TR_COMM_APC == comm->type) {
1459     json_t *jexpire  = json_object_get(jcomm, "expiration_interval");
1460     comm->expiration_interval = 43200; /*30 days*/
1461     if (jexpire) {
1462         if (!json_is_integer(jexpire)) {
1463           fprintf(stderr, "tr_parse_comm: expirae_interval is not an integer\n");
1464           return NULL;
1465         }
1466         comm->expiration_interval = json_integer_value(jexpire);
1467         if (comm->expiration_interval <= 10)
1468           comm->expiration_interval = 11; /* Freeradius waits 10 minutes between successful TR queries*/
1469         if (comm->expiration_interval > 129600) /* 90 days*/
1470         comm->expiration_interval = 129600;
1471     }
1472   }
1473   
1474   return comm;
1475 }
1476
1477 static TR_CFG_RC tr_cfg_parse_comms (TR_CFG *trc, json_t *jcfg) 
1478 {
1479   json_t *jcomms = NULL;
1480   TR_CFG_RC rc = TR_CFG_SUCCESS;
1481   TR_COMM *comm = NULL;
1482   int i = 0;
1483
1484   if ((!trc) || (!jcfg)) {
1485     tr_debug("tr_cfg_parse_comms: Bad Parameters.");
1486     return TR_CFG_BAD_PARAMS;
1487   }
1488
1489   if (NULL != (jcomms = json_object_get(jcfg, "communities"))) {
1490     if (!json_is_array(jcomms)) {
1491       return TR_CFG_NOPARSE;
1492     }
1493
1494     for (i = 0; i < json_array_size(jcomms); i++) {
1495       if (NULL == (comm = tr_cfg_parse_one_comm(trc, 
1496                                                 json_array_get(jcomms, i), 
1497                                                 &rc))) {
1498         return rc;
1499       }
1500       tr_debug("tr_cfg_parse_comms: Community configured: %s.", comm->id->buf);
1501       comm->next = trc->comms;
1502       trc->comms = comm;
1503     }
1504   }
1505   tr_debug("tr_cfg_parse_comms: Finished (rc=%d)", rc);
1506   return rc;
1507 }
1508
1509 TR_CFG_RC tr_cfg_validate(TR_CFG *trc)
1510 {
1511   TR_CFG_RC rc = TR_CFG_SUCCESS;
1512
1513   if (!trc)
1514     return TR_CFG_BAD_PARAMS;
1515
1516   if ((NULL == trc->internal)||
1517       (NULL == trc->internal->hostname)) {
1518     tr_debug("tr_cfg_validate: Error: No internal configuration, or no hostname.");
1519     rc = TR_CFG_ERROR;
1520   }
1521
1522   if (NULL == trc->rp_clients) {
1523     tr_debug("tr_cfg_validate: Error: No RP Clients configured");
1524     rc = TR_CFG_ERROR;
1525   }
1526
1527   if (NULL == trc->comms) {
1528     tr_debug("tr_cfg_validate: Error: No Communities configured");
1529     rc = TR_CFG_ERROR;
1530   }
1531
1532   if ((NULL == trc->default_servers) && (NULL == trc->idp_realms)) {
1533     tr_debug("tr_cfg_validate: Error: No default servers or IDPs configured.");
1534     rc = TR_CFG_ERROR;
1535   }
1536   
1537   return rc;
1538 }
1539
1540 /* Join two paths and return a pointer to the result. This should be freed
1541  * via talloc_free. Returns NULL on failure. */
1542 static char *join_paths(TALLOC_CTX *mem_ctx, const char *p1, const char *p2)
1543 {
1544   return talloc_asprintf(mem_ctx, "%s/%s", p1, p2); /* returns NULL on a failure */
1545 }
1546
1547 TR_CFG_RC tr_cfg_parse_one_config_file(TR_CFG *cfg, const char *file_with_path)
1548 {
1549   json_t *jcfg=NULL;
1550   json_t *jser=NULL;
1551   json_error_t rc;
1552
1553   if (NULL==(jcfg=json_load_file(file_with_path, 
1554                                  JSON_DISABLE_EOF_CHECK, &rc))) {
1555     tr_debug("tr_parse_one_config_file: Error parsing config file %s.", 
1556              file_with_path);
1557     return TR_CFG_NOPARSE;
1558   }
1559
1560   // Look for serial number and log it if it exists
1561   if (NULL!=(jser=json_object_get(jcfg, "serial_number"))) {
1562     if (json_is_number(jser)) {
1563       tr_notice("tr_parse_one_config_file: Attempting to load revision %" JSON_INTEGER_FORMAT " of '%s'.",
1564                 json_integer_value(jser),
1565                 file_with_path);
1566     }
1567   }
1568
1569   if ((TR_CFG_SUCCESS != tr_cfg_parse_internal(cfg, jcfg)) ||
1570       (TR_CFG_SUCCESS != tr_cfg_parse_local_orgs(cfg, jcfg)) ||
1571       (TR_CFG_SUCCESS != tr_cfg_parse_peer_orgs(cfg, jcfg)) ||
1572       (TR_CFG_SUCCESS != tr_cfg_parse_default_servers(cfg, jcfg)) ||
1573       (TR_CFG_SUCCESS != tr_cfg_parse_comms(cfg, jcfg)))
1574     return TR_CFG_ERROR;
1575
1576   return TR_CFG_SUCCESS;
1577 }
1578
1579 /* Reads configuration files in config_dir ("" or "./" will use the current directory). */
1580 TR_CFG_RC tr_parse_config(TR_CFG_MGR *cfg_mgr, const char *config_dir, int n, struct dirent **cfg_files)
1581 {
1582   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1583   char *file_with_path;
1584   int ii;
1585   TR_CFG_RC cfg_rc=TR_CFG_ERROR;
1586
1587   if ((!cfg_mgr) || (!cfg_files) || (n<=0)) {
1588     cfg_rc=TR_CFG_BAD_PARAMS;
1589     goto cleanup;
1590   }
1591
1592   if (cfg_mgr->new != NULL)
1593     tr_cfg_free(cfg_mgr->new);
1594   cfg_mgr->new=tr_cfg_new(tmp_ctx); /* belongs to the temporary context for now */
1595   if (cfg_mgr->new == NULL) {
1596     cfg_rc=TR_CFG_NOMEM;
1597     goto cleanup;
1598   }
1599
1600   cfg_mgr->new->peers=trp_ptable_new(cfg_mgr);
1601
1602   /* Parse configuration information from each config file */
1603   for (ii=0; ii<n; ii++) {
1604     file_with_path=join_paths(tmp_ctx, config_dir, cfg_files[ii]->d_name); /* must free result with talloc_free */
1605     if(file_with_path == NULL) {
1606       tr_crit("tr_parse_config: error joining path.");
1607       cfg_rc=TR_CFG_NOMEM;
1608       goto cleanup;
1609     }
1610     tr_debug("tr_parse_config: Parsing %s.", cfg_files[ii]->d_name); /* print the filename without the path */
1611     cfg_rc=tr_cfg_parse_one_config_file(cfg_mgr->new, file_with_path);
1612     if (cfg_rc!=TR_CFG_SUCCESS) {
1613       tr_crit("tr_parse_config: Error parsing %s", file_with_path);
1614       goto cleanup;
1615     }
1616     talloc_free(file_with_path); /* done with filename */
1617   }
1618
1619   /* make sure we got a complete, consistent configuration */
1620   if (TR_CFG_SUCCESS != tr_cfg_validate(cfg_mgr->new)) {
1621     tr_err("tr_parse_config: Error: INVALID CONFIGURATION");
1622     cfg_rc=TR_CFG_ERROR;
1623     goto cleanup;
1624   }
1625
1626   /* success! */
1627   talloc_steal(cfg_mgr, cfg_mgr->new); /* hand this over to the cfg_mgr context */
1628   cfg_rc=TR_CFG_SUCCESS;
1629
1630 cleanup:
1631   talloc_free(tmp_ctx);
1632   return cfg_rc;
1633 }
1634
1635 TR_IDP_REALM *tr_cfg_find_idp (TR_CFG *tr_cfg, TR_NAME *idp_id, TR_CFG_RC *rc)
1636 {
1637
1638   TR_IDP_REALM *cfg_idp;
1639
1640   if ((!tr_cfg) || (!idp_id)) {
1641     if (rc)
1642       *rc = TR_CFG_BAD_PARAMS;
1643     return NULL;
1644   }
1645
1646   for (cfg_idp = tr_cfg->idp_realms; NULL != cfg_idp; cfg_idp = cfg_idp->next) {
1647     if (!tr_name_cmp (idp_id, cfg_idp->realm_id)) {
1648       tr_debug("tr_cfg_find_idp: Found %s.", idp_id->buf);
1649       return cfg_idp;
1650     }
1651   }
1652   /* if we didn't find one, return NULL */ 
1653   return NULL;
1654 }
1655
1656 TR_RP_CLIENT *tr_cfg_find_rp (TR_CFG *tr_cfg, TR_NAME *rp_gss, TR_CFG_RC *rc)
1657 {
1658   TR_RP_CLIENT *cfg_rp;
1659
1660   if ((!tr_cfg) || (!rp_gss)) {
1661     if (rc)
1662       *rc = TR_CFG_BAD_PARAMS;
1663     return NULL;
1664   }
1665
1666   for (cfg_rp = tr_cfg->rp_clients; NULL != cfg_rp; cfg_rp = cfg_rp->next) {
1667     if (tr_gss_names_matches(cfg_rp->gss_names, rp_gss)) {
1668       tr_debug("tr_cfg_find_rp: Found %s.", rp_gss->buf);
1669       return cfg_rp;
1670     }
1671   }
1672   /* if we didn't find one, return NULL */ 
1673   return NULL;
1674 }
1675
1676 static int is_cfg_file(const struct dirent *dent) {
1677   int n;
1678
1679   /* Only accept filenames ending in ".cfg" and starting with a character
1680    * other than an ASCII '.' */
1681
1682   /* filename must be at least 4 characters long to be acceptable */
1683   n=strlen(dent->d_name);
1684   if (n < 4) {
1685     return 0;
1686   }
1687
1688   /* filename must not start with '.' */
1689   if ('.' == dent->d_name[0]) {
1690     return 0;
1691   }
1692
1693   /* If the above passed and the last four characters of the filename are .cfg, accept.
1694    * (n.b., assumes an earlier test checked that the name is >= 4 chars long.) */
1695   if (0 == strcmp(&(dent->d_name[n-4]), ".cfg")) {
1696     return 1;
1697   }
1698
1699   /* otherwise, return false. */
1700   return 0;
1701 }
1702
1703 /* Find configuration files in a particular directory. Returns the
1704  * number of entries found, 0 if none are found, or <0 for some
1705  * errors. If n>=0, the cfg_files parameter will contain a newly
1706  * allocated array of pointers to struct dirent entries, as returned
1707  * by scandir(). These can be freed with tr_free_config_file_list().
1708  */
1709 int tr_find_config_files (const char *config_dir, struct dirent ***cfg_files) {
1710   int n = 0;
1711   
1712   n = scandir(config_dir, cfg_files, is_cfg_file, alphasort);
1713
1714   if (n < 0) {
1715     perror("scandir");
1716     tr_debug("tr_find_config: scandir error trying to scan %s.", config_dir);
1717   } 
1718
1719   return n;
1720 }
1721
1722 /* Free memory allocated for configuration file list returned from tr_find_config_files().
1723  * This can be called regardless of the return value of tr_find_config_values(). */
1724 void tr_free_config_file_list(int n, struct dirent ***cfg_files) {
1725   int ii;
1726
1727   /* if n < 0, then scandir did not allocate anything because it failed */
1728   if((n>=0) && (*cfg_files != NULL)) {
1729     for(ii=0; ii<n; ii++) {
1730       free((*cfg_files)[ii]);
1731     }
1732     free(*cfg_files); /* safe even if n==0 */
1733     *cfg_files=NULL; /* this will help prevent accidentally freeing twice */
1734   }
1735 }