Use default set of filters for RP clients with none specified.
[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 /* default filter accepts realm and *.realm */
931 static TR_FILTER *tr_cfg_default_filter(TALLOC_CTX *mem_ctx, TR_NAME *realm, TR_CFG_RC *rc)
932 {
933   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
934   TR_FILTER *filt=NULL;
935   TR_CONSTRAINT *cons=NULL;
936   TR_NAME *name=NULL;
937   TR_NAME *n_prefix=tr_new_name("*.");
938   TR_NAME *n_rp_realm=tr_new_name("rp_realm");
939   TR_NAME *n_domain=tr_new_name("domain");
940   TR_NAME *n_realm=tr_new_name("realm");
941   
942
943   if ((realm==NULL) || (rc==NULL)) {
944     tr_debug("tr_cfg_default_filter: invalid arguments.");
945     if (rc!=NULL)
946       *rc=TR_CFG_BAD_PARAMS;
947     goto cleanup;
948   }
949
950   if ((n_prefix==NULL) || (n_rp_realm==NULL) || (n_domain==NULL) || (n_realm==NULL)) {
951     tr_debug("tr_cfg_default_filter: unable to allocate names.");
952     *rc=TR_CFG_NOMEM;
953     goto cleanup;
954   }
955
956   filt=tr_filter_new(tmp_ctx);
957   if (filt==NULL) {
958     tr_debug("tr_cfg_default_filter: could not allocate filter.");
959     *rc=TR_CFG_NOMEM;
960     goto cleanup;
961   }
962   tr_filter_set_type(filt, TR_FILTER_TYPE_TID_INCOMING);
963   filt->lines[0]=tr_fline_new(filt);
964   if (filt->lines[0]==NULL) {
965     tr_debug("tr_cfg_default_filter: could not allocate filter line.");
966     *rc=TR_CFG_NOMEM;
967     goto cleanup;
968   }
969
970   filt->lines[0]->action=TR_FILTER_ACTION_ACCEPT;
971   filt->lines[0]->specs[0]=tr_fspec_new(filt->lines[0]);
972   filt->lines[0]->specs[0]->field=n_rp_realm;
973   n_rp_realm=NULL; /* we don't own this name any more */
974
975   name=tr_dup_name(realm);
976   if (name==NULL) {
977     tr_debug("tr_cfg_default_filter: could not allocate realm name.");
978     *rc=TR_CFG_NOMEM;
979     goto cleanup;
980   }
981   if (0!=tr_fspec_add_match(filt->lines[0]->specs[0], name)) {
982     tr_debug("tr_cfg_default_filter: could not add realm name to filter spec.");
983     *rc=TR_CFG_NOMEM;
984     goto cleanup;
985   }
986   name=NULL; /* we no longer own the name */
987
988   if (NULL==(name=tr_name_cat(n_prefix, realm))) {
989     tr_debug("tr_cfg_default_filter: could not allocate wildcard realm name.");
990     *rc=TR_CFG_NOMEM;
991     goto cleanup;
992   }
993
994   if (0!=tr_fspec_add_match(filt->lines[0]->specs[0], name)) {
995     tr_debug("tr_cfg_default_filter: could not add wildcard realm name to filter spec.");
996     *rc=TR_CFG_NOMEM;
997     goto cleanup;
998   }
999   name=NULL; /* we no longer own the name */
1000
1001   /* domain constraint */
1002   if (NULL==(cons=tr_constraint_new(filt->lines[0]))) {
1003     tr_debug("tr_cfg_default_filter: could not allocate domain constraint.");
1004     *rc=TR_CFG_NOMEM;
1005     goto cleanup;
1006   }
1007
1008   cons->type=n_domain;
1009   n_domain=NULL; /* belongs to the constraint now */
1010   name=tr_dup_name(realm);
1011   if (name==NULL) {
1012     tr_debug("tr_cfg_default_filter: could not allocate realm name for domain constraint.");
1013     *rc=TR_CFG_NOMEM;
1014     goto cleanup;
1015   }
1016   cons->matches[0]=name;
1017   name=tr_name_cat(n_prefix, realm);
1018   if (name==NULL) {
1019     tr_debug("tr_cfg_default_filter: could not allocate wildcard realm name for domain constraint.");
1020     *rc=TR_CFG_NOMEM;
1021     goto cleanup;
1022   }
1023   cons->matches[1]=name;
1024   name=NULL;
1025   filt->lines[0]->domain_cons=cons;
1026
1027
1028   /* realm constraint */
1029   if (NULL==(cons=tr_constraint_new(filt->lines[0]))) {
1030     tr_debug("tr_cfg_default_filter: could not allocate realm constraint.");
1031     *rc=TR_CFG_NOMEM;
1032     goto cleanup;
1033   }
1034
1035   cons->type=n_realm;
1036   n_realm=NULL; /* belongs to the constraint now */
1037   name=tr_dup_name(realm);
1038   if (name==NULL) {
1039     tr_debug("tr_cfg_default_filter: could not allocate realm name for realm constraint.");
1040     *rc=TR_CFG_NOMEM;
1041     goto cleanup;
1042   }
1043   cons->matches[0]=name;
1044   name=tr_name_cat(n_prefix, realm);
1045   if (name==NULL) {
1046     tr_debug("tr_cfg_default_filter: could not allocate wildcard realm name for realm constraint.");
1047     *rc=TR_CFG_NOMEM;
1048     goto cleanup;
1049   }
1050   cons->matches[1]=name;
1051   name=NULL;
1052   filt->lines[0]->realm_cons=cons;
1053
1054   talloc_steal(mem_ctx, filt);
1055 cleanup:
1056   talloc_free(tmp_ctx);
1057
1058   if (*rc!=TR_CFG_SUCCESS)
1059     filt=NULL;
1060
1061   if (n_prefix!=NULL)
1062     tr_free_name(n_prefix);
1063   if (n_rp_realm!=NULL)
1064     tr_free_name(n_rp_realm);
1065   if (n_realm!=NULL)
1066     tr_free_name(n_realm);
1067   if (n_domain!=NULL)
1068     tr_free_name(n_domain);
1069   if (name!=NULL)
1070     tr_free_name(name);
1071
1072   return filt;
1073 }
1074
1075 /* parses rp client */
1076 static TR_RP_CLIENT *tr_cfg_parse_one_rp_client(TALLOC_CTX *mem_ctx, json_t *jrealm, TR_CFG_RC *rc)
1077 {
1078   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1079   TR_RP_CLIENT *client=NULL;
1080   TR_CFG_RC call_rc=TR_CFG_ERROR;
1081   TR_FILTER *new_filt=NULL;
1082   TR_NAME *realm=NULL;
1083   json_t *jfilt=NULL;
1084   json_t *jrealm_id=NULL;
1085
1086   *rc=TR_CFG_ERROR; /* default to error if not set */
1087
1088   if ((!jrealm) || (!rc)) {
1089     tr_err("tr_cfg_parse_one_rp_client: Bad parameters.");
1090     if (rc)
1091       *rc=TR_CFG_BAD_PARAMS;
1092     goto cleanup;
1093   }
1094
1095   if ((NULL==(jrealm_id=json_object_get(jrealm, "realm"))) || (!json_is_string(jrealm_id))) {
1096     tr_debug("tr_cfg_parse_one_rp_client: no realm ID found.");
1097     *rc=TR_CFG_BAD_PARAMS;
1098     goto cleanup;
1099   } 
1100
1101   tr_debug("tr_cfg_parse_one_rp_client: realm_id=\"%s\"", json_string_value(jrealm_id));
1102   realm=tr_new_name(json_string_value(jrealm_id));
1103   if (realm==NULL) {
1104     tr_err("tr_cfg_parse_one_rp_client: could not allocate realm ID.");
1105     *rc=TR_CFG_NOMEM;
1106     goto cleanup;
1107   }
1108
1109   if (NULL==(client=tr_rp_client_new(tmp_ctx))) {
1110     tr_err("tr_cfg_parse_one_rp_client: could not allocate rp client.");
1111     *rc=TR_CFG_NOMEM;
1112     goto cleanup;
1113   }
1114
1115   client->gss_names=tr_cfg_parse_gss_names(client, json_object_get(jrealm, "gss_names"), &call_rc);
1116
1117   if (call_rc!=TR_CFG_SUCCESS) {
1118     tr_err("tr_cfg_parse_one_rp_client: could not parse gss_names.");
1119     *rc=TR_CFG_NOPARSE;
1120     goto cleanup;
1121   }
1122
1123   /* parse filters */
1124   jfilt=json_object_get(jrealm, "filters");
1125   if (jfilt!=NULL) {
1126     new_filt=tr_cfg_parse_filters(tmp_ctx, jfilt, &call_rc);
1127     if (call_rc!=TR_CFG_SUCCESS) {
1128       tr_err("tr_cfg_parse_one_rp_client: could not parse filters.");
1129       *rc=TR_CFG_NOPARSE;
1130       goto cleanup;
1131     }
1132   } else {
1133     tr_debug("tr_cfg_parse_one_rp_client: no filters specified, using default filters.");
1134     new_filt=tr_cfg_default_filter(tmp_ctx, realm, &call_rc);
1135     if (call_rc!=TR_CFG_SUCCESS) {
1136       tr_err("tr_cfg_parse_one_rp_client: could not set default filters.");
1137       *rc=TR_CFG_NOPARSE;
1138       goto cleanup;
1139     }
1140   }
1141
1142   tr_rp_client_set_filter(client, new_filt);
1143   *rc=TR_CFG_SUCCESS;
1144
1145   cleanup:
1146   if (realm!=NULL)
1147     tr_free_name(realm);
1148
1149     if (*rc==TR_CFG_SUCCESS)
1150       talloc_steal(mem_ctx, client);
1151     else {
1152       talloc_free(client);
1153       client=NULL;
1154     }
1155
1156     talloc_free(tmp_ctx);
1157     return client;
1158   }
1159
1160   /* Determine whether the realm is an RP realm */
1161 static int tr_cfg_is_rp_realm(json_t *jrealm)
1162 {
1163   /* Check that we have a gss name. */
1164   if (NULL != json_object_get(jrealm, "gss_names"))
1165     return 1;
1166   else
1167     return 0;
1168 }
1169
1170 /* Parse any rp clients in the j_realms object. Ignores other realms. */
1171 static TR_RP_CLIENT *tr_cfg_parse_rp_clients(TALLOC_CTX *mem_ctx, json_t *jrealms, TR_CFG_RC *rc)
1172 {
1173   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1174   TR_RP_CLIENT *clients=NULL;
1175   TR_RP_CLIENT *new_client=NULL;
1176   json_t *this_jrealm=NULL;
1177   int ii=0;
1178
1179   *rc=TR_CFG_ERROR;
1180   if ((jrealms==NULL) || (!json_is_array(jrealms))) {
1181     tr_err("tr_cfg_parse_rp_clients: realms not an array");
1182     *rc=TR_CFG_BAD_PARAMS;
1183     goto cleanup;
1184   }
1185
1186   for (ii=0; ii<json_array_size(jrealms); ii++) {
1187     this_jrealm=json_array_get(jrealms, ii);
1188     if (tr_cfg_is_rp_realm(this_jrealm)) {
1189       new_client=tr_cfg_parse_one_rp_client(tmp_ctx, this_jrealm, rc);
1190       if ((*rc)!=TR_CFG_SUCCESS) {
1191         tr_err("tr_cfg_parse_rp_clients: error decoding realm entry %d", ii+1);
1192         *rc=TR_CFG_NOPARSE;
1193         goto cleanup;
1194       }
1195       clients=tr_rp_client_add(clients, new_client);
1196     }
1197   }
1198   
1199   *rc=TR_CFG_SUCCESS;
1200   talloc_steal(mem_ctx, clients);
1201
1202 cleanup:
1203   talloc_free(tmp_ctx);
1204   return clients;
1205 }
1206
1207 /* takes a talloc context, but currently does not use it */
1208 static TR_NAME *tr_cfg_parse_org_name(TALLOC_CTX *mem_ctx, json_t *j_org, TR_CFG_RC *rc)
1209 {
1210   TR_NAME *name=NULL;
1211
1212   if ((j_org==NULL) || (rc==NULL) || (!json_is_string(j_org))) {
1213     tr_debug("tr_cfg_parse_org_name: Bad parameters.");
1214     if (rc!=NULL)
1215       *rc = TR_CFG_BAD_PARAMS; /* fill in return value if we can */
1216     return NULL;
1217   }
1218
1219   name=tr_new_name(json_string_value(j_org));
1220   if (name==NULL)
1221     *rc=TR_CFG_NOMEM;
1222   else
1223     *rc=TR_CFG_SUCCESS;
1224   return name;
1225 }
1226
1227 #if 0
1228 /* Update the community information with data from a new batch of IDP realms.
1229  * May partially add realms if there is a failure, no guarantees.
1230  * Call like comms=tr_comm_idp_update(comms, new_realms, &rc) */
1231 static TR_COMM *tr_cfg_comm_idp_update(TALLOC_CTX *mem_ctx, TR_COMM *comms, TR_IDP_REALM *new_realms, TR_CFG_RC *rc)
1232 {
1233   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1234   TR_COMM *comm=NULL; /* community looked up in comms table */
1235   TR_COMM *new_comms=NULL; /* new communities as we create them */
1236   TR_IDP_REALM *realm=NULL;
1237   TR_APC *apc=NULL; /* apc of one realm */
1238
1239   if (rc==NULL) {
1240     *rc=TR_CFG_BAD_PARAMS;
1241     goto cleanup;
1242   }
1243
1244   /* start with an empty list communities, then fill that in */
1245   for (realm=new_realms; realm!=NULL; realm=realm->next) {
1246     for (apc=realm->apcs; apc!=NULL; apc=apc->next) {
1247       comm=tr_comm_lookup(comms, apc->id);
1248       if (comm==NULL) {
1249         comm=tr_comm_new(tmp_ctx);
1250         if (comm==NULL) {
1251           tr_debug("tr_cfg_comm_idp_update: unable to allocate new community.");
1252           *rc=TR_CFG_NOMEM;
1253           goto cleanup;
1254         }
1255         /* fill in the community with info */
1256         comm->type=TR_COMM_APC; /* realms added this way are in APCs */
1257         comm->expiration_interval=TR_DEFAULT_APC_EXPIRATION_INTERVAL;
1258         comm->id=tr_dup_name(apc->id);
1259         tr_comm_add_idp_realm(comm, realm);
1260         new_comms=tr_comm_add(new_comms, comm);
1261       } else {
1262         /* add this realm to the comm */
1263         tr_comm_add_idp_realm(comm, realm);
1264       }
1265     }
1266   }
1267
1268   /* we successfully built a list, add it to the other list */
1269   comms=tr_comm_add(comms, new_comms);
1270   talloc_steal(mem_ctx, comms);
1271  cleanup:
1272   talloc_free(tmp_ctx);
1273   return comms;
1274 }
1275 #endif
1276
1277 static TR_CFG_RC tr_cfg_parse_one_local_org(TR_CFG *trc, json_t *jlorg)
1278 {
1279   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1280   TR_CFG_RC retval=TR_CFG_ERROR; /* our return code */
1281   TR_CFG_RC rc=TR_CFG_ERROR; /* return code from functions we call */
1282   TR_NAME *org_name=NULL;
1283   json_t *j_org=NULL;
1284   json_t *j_realms=NULL;
1285   TR_IDP_REALM *new_idp_realms=NULL;
1286   TR_RP_CLIENT *new_rp_clients=NULL;
1287
1288   tr_debug("tr_cfg_parse_one_local_org: parsing local organization");
1289
1290   /* get organization_name (optional) */
1291   if (NULL==(j_org=json_object_get(jlorg, "organization_name"))) {
1292     tr_debug("tr_cfg_parse_one_local_org: organization_name unspecified");
1293   } else {
1294     org_name=tr_cfg_parse_org_name(tmp_ctx, j_org, &rc);
1295     if (rc==TR_CFG_SUCCESS) {
1296       tr_debug("tr_cfg_parse_one_local_org: organization_name=\"%.*s\"",
1297                org_name->len,
1298                org_name->buf);
1299       /* we don't actually do anything with this, but we could */
1300       tr_free_name(org_name);
1301       org_name=NULL; 
1302     }
1303   }
1304
1305   /* Now get realms. Allow this to be missing; even though that is a pointless organization entry,
1306    * it's harmless. Report a warning because that might be unintentional. */
1307   if (NULL==(j_realms=json_object_get(jlorg, "realms"))) {
1308     tr_warning("tr_cfg_parse_one_local_org: warning - no realms in this local organization");
1309   } else {
1310     /* Allocate in the tmp_ctx so these will be cleaned up if we do not complete successfully. */
1311     new_idp_realms=tr_cfg_parse_idp_realms(tmp_ctx, j_realms, &rc);
1312     if (rc!=TR_CFG_SUCCESS)
1313       goto cleanup;
1314
1315     new_rp_clients=tr_cfg_parse_rp_clients(tmp_ctx, j_realms, &rc);
1316     if (rc!=TR_CFG_SUCCESS)
1317       goto cleanup;
1318   }
1319   retval=TR_CFG_SUCCESS;
1320   
1321 cleanup:
1322   /* if we succeeded, link things to the configuration and move out of tmp context */
1323   if (retval==TR_CFG_SUCCESS) {
1324     if (new_idp_realms!=NULL) {
1325       trc->idp_realms=tr_idp_realm_add(trc->idp_realms, new_idp_realms); /* fixes talloc contexts except for head*/
1326       talloc_steal(trc, trc->idp_realms); /* make sure the head is in the right context */
1327     }
1328
1329     if (new_rp_clients!=NULL) {
1330       trc->rp_clients=tr_rp_client_add(trc->rp_clients, new_rp_clients); /* fixes talloc contexts */
1331       talloc_steal(trc, trc->rp_clients); /* make sure head is in the right context */
1332     }
1333   }
1334
1335   talloc_free(tmp_ctx);
1336   return rc;
1337 }
1338
1339 /* Parse local organizations if present. Returns success if there are none. On failure, the configuration is unreliable. */
1340 static TR_CFG_RC tr_cfg_parse_local_orgs(TR_CFG *trc, json_t *jcfg)
1341 {
1342   json_t *jlocorgs=NULL;
1343   int ii=0;
1344
1345   jlocorgs=json_object_get(jcfg, "local_organizations");
1346   if (jlocorgs==NULL)
1347     return TR_CFG_SUCCESS;
1348
1349   if (!json_is_array(jlocorgs)) {
1350     tr_err("tr_cfg_parse_local_orgs: local_organizations is not an array.");
1351     return TR_CFG_NOPARSE;
1352   }
1353
1354   for (ii=0; ii<json_array_size(jlocorgs); ii++) {
1355     if (tr_cfg_parse_one_local_org(trc, json_array_get(jlocorgs, ii))!=TR_CFG_SUCCESS) {
1356       tr_err("tr_cfg_parse_local_orgs: error parsing local_organization %d.", ii+1);
1357       return TR_CFG_NOPARSE;
1358     }
1359   }
1360
1361   return TR_CFG_SUCCESS;
1362 }
1363
1364 static TR_CFG_RC tr_cfg_parse_one_peer_org(TR_CFG *trc, json_t *jporg)
1365 {
1366   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1367   json_t *jhost=NULL;
1368   json_t *jport=NULL;
1369   json_t *jgss=NULL;
1370   TRP_PEER *new_peer=NULL;
1371   TR_GSS_NAMES *names=NULL;
1372   TR_CFG_RC rc=TR_CFG_ERROR;
1373
1374   jhost=json_object_get(jporg, "hostname");
1375   jport=json_object_get(jporg, "port");
1376   jgss=json_object_get(jporg, "gss_names");
1377
1378   if ((jhost==NULL) || (!json_is_string(jhost))) {
1379     tr_err("tr_cfg_parse_one_peer_org: hostname not specified or not a string.");
1380     rc=TR_CFG_NOPARSE;
1381     goto cleanup;
1382   }
1383
1384   if ((jport!=NULL) && (!json_is_number(jport))) {
1385     /* note that not specifying the port is allowed, but if set it must be a number */
1386     tr_err("tr_cfg_parse_one_peer_org: port is not a number.");
1387     rc=TR_CFG_NOPARSE;
1388     goto cleanup;
1389   }
1390   
1391   if ((jgss==NULL) || (!json_is_array(jgss))) {
1392     tr_err("tr_cfg_parse_one_peer_org: gss_names not specified or not an array.");
1393     rc=TR_CFG_NOPARSE;
1394     goto cleanup;
1395   }
1396
1397   new_peer=trp_peer_new(tmp_ctx);
1398   if (new_peer==NULL) {
1399     tr_err("tr_cfg_parse_one_peer_org: could not allocate new peer.");
1400     rc=TR_CFG_NOMEM;
1401     goto cleanup;
1402   }
1403
1404   trp_peer_set_server(new_peer, json_string_value(jhost));
1405   if (jport==NULL)
1406     trp_peer_set_port(new_peer, TRP_PORT);
1407   else
1408     trp_peer_set_port(new_peer, json_integer_value(jport));
1409
1410   names=tr_cfg_parse_gss_names(tmp_ctx, jgss, &rc);
1411   if (rc!=TR_CFG_SUCCESS) {
1412     tr_err("tr_cfg_parse_one_peer_org: unable to parse gss names.");
1413     rc=TR_CFG_NOPARSE;
1414     goto cleanup;
1415   }
1416   trp_peer_set_gss_names(new_peer, names);
1417
1418   /* success! */
1419   trp_ptable_add(trc->peers, new_peer);
1420   rc=TR_CFG_SUCCESS;
1421
1422  cleanup:
1423   talloc_free(tmp_ctx);
1424   return rc;
1425 }
1426
1427 /* Parse peer organizations, if present. Returns success if there are none. */
1428 static TR_CFG_RC tr_cfg_parse_peer_orgs(TR_CFG *trc, json_t *jcfg)
1429 {
1430   json_t *jpeerorgs=NULL;
1431   int ii=0;
1432
1433   jpeerorgs=json_object_get(jcfg, "peer_organizations");
1434   if (jpeerorgs==NULL)
1435     return TR_CFG_SUCCESS;
1436
1437   if (!json_is_array(jpeerorgs)) {
1438     tr_err("tr_cfg_parse_peer_orgs: peer_organizations is not an array.");
1439     return TR_CFG_NOPARSE;
1440   }
1441
1442   for (ii=0; ii<json_array_size(jpeerorgs); ii++) {
1443     if (tr_cfg_parse_one_peer_org(trc, json_array_get(jpeerorgs, ii))!=TR_CFG_SUCCESS) {
1444       tr_err("tr_cfg_parse_peer_orgs: error parsing peer_organization %d.", ii+1);
1445       return TR_CFG_NOPARSE;
1446     }
1447   }
1448
1449   return TR_CFG_SUCCESS;
1450 }
1451              
1452 static TR_CFG_RC tr_cfg_parse_default_servers (TR_CFG *trc, json_t *jcfg) 
1453 {
1454   json_t *jdss = NULL;
1455   TR_CFG_RC rc = TR_CFG_SUCCESS;
1456   TR_AAA_SERVER *ds = NULL;
1457   int i = 0;
1458
1459   if ((!trc) || (!jcfg))
1460     return TR_CFG_BAD_PARAMS;
1461
1462   /* If there are default servers, store them */
1463   if ((NULL != (jdss = json_object_get(jcfg, "default_servers"))) &&
1464       (json_is_array(jdss)) &&
1465       (0 < json_array_size(jdss))) {
1466
1467     for (i = 0; i < json_array_size(jdss); i++) {
1468       if (NULL == (ds = tr_cfg_parse_one_aaa_server(trc,
1469                                                     json_array_get(jdss, i), 
1470                                                    &rc))) {
1471         return rc;
1472       }
1473       tr_debug("tr_cfg_parse_default_servers: Default server configured: %s", ds->hostname->buf);
1474       ds->next = trc->default_servers;
1475       trc->default_servers = ds;
1476     }
1477   } 
1478
1479   tr_debug("tr_cfg_parse_default_servers: Finished (rc=%d)", rc);
1480   return rc;
1481 }
1482
1483 static TR_IDP_REALM *tr_cfg_parse_comm_idps (TR_CFG *trc, json_t *jidps, TR_CFG_RC *rc)
1484 {
1485   TR_IDP_REALM *idp = NULL;
1486   TR_IDP_REALM *found_idp = NULL;
1487   TR_IDP_REALM *temp_idp = NULL;
1488   int i = 0;
1489
1490   if ((!trc) ||
1491       (!jidps) ||
1492       (!json_is_array(jidps))) {
1493     if (rc)
1494       *rc = TR_CFG_BAD_PARAMS;
1495     return NULL;
1496   }
1497
1498   for (i = 0; i < json_array_size(jidps); i++) {
1499     if (NULL == (temp_idp = talloc(trc, TR_IDP_REALM))) {
1500       tr_debug("tr_cfg_parse_comm_idps: Can't allocate memory for IdP Realm.");
1501       if (rc)
1502         *rc = TR_CFG_NOMEM;
1503       return NULL;
1504     }
1505     memset (temp_idp, 0, sizeof(TR_IDP_REALM));
1506     
1507     if (NULL == (found_idp = (tr_cfg_find_idp(trc, 
1508                                               tr_new_name((char *)json_string_value(json_array_get(jidps, i))), 
1509                                               rc)))) {
1510       tr_debug("tr_cfg_parse_comm_idps: Unknown IDP %s.", 
1511                (char *)json_string_value(json_array_get(jidps, i)));
1512       return NULL;
1513     }
1514
1515     // We *MUST* do a dereferenced copy here or the second community will corrupt the linked list we create here.
1516     *temp_idp = *found_idp;
1517
1518     temp_idp->comm_next = idp;
1519     idp = temp_idp;
1520   }
1521
1522   return idp;
1523 }
1524
1525 static TR_RP_REALM *tr_cfg_parse_comm_rps (TR_CFG *trc, json_t *jrps, TR_CFG_RC *rc)
1526 {
1527   TR_RP_REALM *rp = NULL;
1528   TR_RP_REALM *temp_rp = NULL;
1529   int i = 0;
1530
1531   if ((!trc) ||
1532       (!jrps) ||
1533       (!json_is_array(jrps))) {
1534     if (rc)
1535       *rc = TR_CFG_BAD_PARAMS;
1536     return NULL;
1537   }
1538
1539   for (i = (json_array_size(jrps)-1); i >= 0; i--) {
1540     if (NULL == (temp_rp = talloc_zero(trc, TR_RP_REALM))) {
1541       tr_debug("tr_cfg_parse_comm_rps: Can't allocate memory for RP Realm.");
1542       if (rc)
1543         *rc = TR_CFG_NOMEM;
1544       return NULL;
1545     }
1546
1547     if (NULL == (temp_rp->realm_name = tr_new_name((char *)json_string_value(json_array_get(jrps, i))))) {
1548       tr_debug("tr_cfg_parse_comm_rps: No memory for RP Realm Name.");
1549       if (rc)
1550         *rc = TR_CFG_NOMEM;
1551       return NULL;
1552     }
1553
1554     temp_rp->next = rp;
1555     rp = temp_rp;
1556   }
1557
1558   return rp;
1559 }
1560
1561 static TR_COMM *tr_cfg_parse_one_comm (TR_CFG *trc, json_t *jcomm, TR_CFG_RC *rc) {
1562   TR_COMM *comm = NULL;
1563   json_t *jid = NULL;
1564   json_t *jtype = NULL;
1565   json_t *japcs = NULL;
1566   json_t *jidps = NULL;
1567   json_t *jrps = NULL;
1568
1569   if ((!trc) || (!jcomm) || (!rc)) {
1570     tr_debug("tr_cfg_parse_one_comm: Bad parameters.");
1571     if (rc)
1572       *rc = TR_CFG_BAD_PARAMS;
1573     return NULL;
1574   }
1575
1576   if (NULL == (comm = talloc_zero(trc, TR_COMM))) {
1577     tr_crit("tr_cfg_parse_one_comm: Out of memory.");
1578     *rc = TR_CFG_NOMEM;
1579     return NULL;
1580   }
1581
1582
1583   if ((NULL == (jid = json_object_get(jcomm, "community_id"))) ||
1584       (!json_is_string(jid)) ||
1585       (NULL == (jtype = json_object_get(jcomm, "type"))) ||
1586       (!json_is_string(jtype)) ||
1587       (NULL == (japcs = json_object_get(jcomm, "apcs"))) ||
1588       (!json_is_array(japcs)) ||
1589       (NULL == (jidps = json_object_get(jcomm, "idp_realms"))) ||
1590       (!json_is_array(jidps)) ||
1591       (NULL == (jrps = json_object_get(jcomm, "rp_realms"))) ||
1592       (!json_is_array(jrps))) {
1593     tr_debug("tr_cfg_parse_one_comm: Error parsing Communities configuration.");
1594     *rc = TR_CFG_NOPARSE;
1595     return NULL;
1596   }
1597
1598   if (NULL == (comm->id = tr_new_name((char *)json_string_value(jid)))) {
1599     tr_debug("tr_cfg_parse_one_comm: No memory for community id.");
1600     *rc = TR_CFG_NOMEM;
1601     return NULL;
1602   }
1603
1604   if (0 == strcmp(json_string_value(jtype), "apc")) {
1605     comm->type = TR_COMM_APC;
1606   } else if (0 == strcmp(json_string_value(jtype), "coi")) {
1607     comm->type = TR_COMM_COI;
1608     if (NULL == (comm->apcs = tr_cfg_parse_apcs(trc, japcs, rc))) {
1609       tr_debug("tr_cfg_parse_one_comm: Can't parse APCs for COI %s.", comm->id->buf);
1610       tr_free_name(comm->id);
1611       return NULL;
1612     }
1613   } else {
1614     tr_debug("tr_cfg_parse_one_comm: Invalid community type, comm = %s, type = %s", comm->id->buf, json_string_value(jtype));
1615     tr_free_name(comm->id);
1616     *rc = TR_CFG_NOPARSE;
1617     return NULL;
1618   }
1619
1620   comm->idp_realms = tr_cfg_parse_comm_idps(trc, jidps, rc);
1621   if (TR_CFG_SUCCESS != *rc) {
1622     tr_debug("tr_cfg_parse_one_comm: Can't parse IDP realms for comm %s.", comm->id->buf);
1623     tr_free_name(comm->id);
1624     return NULL;
1625   }
1626
1627   comm->rp_realms = tr_cfg_parse_comm_rps(trc, jrps, rc);
1628   if (TR_CFG_SUCCESS != *rc) {
1629     tr_debug("tr_cfg_parse_comm: Can't parse RP realms for comm %s .", comm->id->buf);
1630     tr_free_name(comm->id);
1631     return NULL;
1632   }
1633
1634   if (TR_COMM_APC == comm->type) {
1635     json_t *jexpire  = json_object_get(jcomm, "expiration_interval");
1636     comm->expiration_interval = 43200; /*30 days*/
1637     if (jexpire) {
1638         if (!json_is_integer(jexpire)) {
1639           fprintf(stderr, "tr_parse_comm: expirae_interval is not an integer\n");
1640           return NULL;
1641         }
1642         comm->expiration_interval = json_integer_value(jexpire);
1643         if (comm->expiration_interval <= 10)
1644           comm->expiration_interval = 11; /* Freeradius waits 10 minutes between successful TR queries*/
1645         if (comm->expiration_interval > 129600) /* 90 days*/
1646         comm->expiration_interval = 129600;
1647     }
1648   }
1649   
1650   return comm;
1651 }
1652
1653 static TR_CFG_RC tr_cfg_parse_comms (TR_CFG *trc, json_t *jcfg) 
1654 {
1655   json_t *jcomms = NULL;
1656   TR_CFG_RC rc = TR_CFG_SUCCESS;
1657   TR_COMM *comm = NULL;
1658   int i = 0;
1659
1660   if ((!trc) || (!jcfg)) {
1661     tr_debug("tr_cfg_parse_comms: Bad Parameters.");
1662     return TR_CFG_BAD_PARAMS;
1663   }
1664
1665   if (NULL != (jcomms = json_object_get(jcfg, "communities"))) {
1666     if (!json_is_array(jcomms)) {
1667       return TR_CFG_NOPARSE;
1668     }
1669
1670     for (i = 0; i < json_array_size(jcomms); i++) {
1671       if (NULL == (comm = tr_cfg_parse_one_comm(trc, 
1672                                                 json_array_get(jcomms, i), 
1673                                                 &rc))) {
1674         return rc;
1675       }
1676       tr_debug("tr_cfg_parse_comms: Community configured: %s.", comm->id->buf);
1677       comm->next = trc->comms;
1678       trc->comms = comm;
1679     }
1680   }
1681   tr_debug("tr_cfg_parse_comms: Finished (rc=%d)", rc);
1682   return rc;
1683 }
1684
1685 TR_CFG_RC tr_cfg_validate(TR_CFG *trc)
1686 {
1687   TR_CFG_RC rc = TR_CFG_SUCCESS;
1688
1689   if (!trc)
1690     return TR_CFG_BAD_PARAMS;
1691
1692   if ((NULL == trc->internal)||
1693       (NULL == trc->internal->hostname)) {
1694     tr_debug("tr_cfg_validate: Error: No internal configuration, or no hostname.");
1695     rc = TR_CFG_ERROR;
1696   }
1697
1698   if (NULL == trc->rp_clients) {
1699     tr_debug("tr_cfg_validate: Error: No RP Clients configured");
1700     rc = TR_CFG_ERROR;
1701   }
1702
1703   if (NULL == trc->comms) {
1704     tr_debug("tr_cfg_validate: Error: No Communities configured");
1705     rc = TR_CFG_ERROR;
1706   }
1707
1708   if ((NULL == trc->default_servers) && (NULL == trc->idp_realms)) {
1709     tr_debug("tr_cfg_validate: Error: No default servers or IDPs configured.");
1710     rc = TR_CFG_ERROR;
1711   }
1712   
1713   return rc;
1714 }
1715
1716 /* Join two paths and return a pointer to the result. This should be freed
1717  * via talloc_free. Returns NULL on failure. */
1718 static char *join_paths(TALLOC_CTX *mem_ctx, const char *p1, const char *p2)
1719 {
1720   return talloc_asprintf(mem_ctx, "%s/%s", p1, p2); /* returns NULL on a failure */
1721 }
1722
1723 TR_CFG_RC tr_cfg_parse_one_config_file(TR_CFG *cfg, const char *file_with_path)
1724 {
1725   json_t *jcfg=NULL;
1726   json_t *jser=NULL;
1727   json_error_t rc;
1728
1729   if (NULL==(jcfg=json_load_file(file_with_path, 
1730                                  JSON_DISABLE_EOF_CHECK, &rc))) {
1731     tr_debug("tr_parse_one_config_file: Error parsing config file %s.", 
1732              file_with_path);
1733     return TR_CFG_NOPARSE;
1734   }
1735
1736   // Look for serial number and log it if it exists
1737   if (NULL!=(jser=json_object_get(jcfg, "serial_number"))) {
1738     if (json_is_number(jser)) {
1739       tr_notice("tr_parse_one_config_file: Attempting to load revision %" JSON_INTEGER_FORMAT " of '%s'.",
1740                 json_integer_value(jser),
1741                 file_with_path);
1742     }
1743   }
1744
1745   if ((TR_CFG_SUCCESS != tr_cfg_parse_internal(cfg, jcfg)) ||
1746       (TR_CFG_SUCCESS != tr_cfg_parse_local_orgs(cfg, jcfg)) ||
1747       (TR_CFG_SUCCESS != tr_cfg_parse_peer_orgs(cfg, jcfg)) ||
1748       (TR_CFG_SUCCESS != tr_cfg_parse_default_servers(cfg, jcfg)) ||
1749       (TR_CFG_SUCCESS != tr_cfg_parse_comms(cfg, jcfg)))
1750     return TR_CFG_ERROR;
1751
1752   return TR_CFG_SUCCESS;
1753 }
1754
1755 /* Reads configuration files in config_dir ("" or "./" will use the current directory). */
1756 TR_CFG_RC tr_parse_config(TR_CFG_MGR *cfg_mgr, const char *config_dir, int n, struct dirent **cfg_files)
1757 {
1758   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1759   char *file_with_path;
1760   int ii;
1761   TR_CFG_RC cfg_rc=TR_CFG_ERROR;
1762
1763   if ((!cfg_mgr) || (!cfg_files) || (n<=0)) {
1764     cfg_rc=TR_CFG_BAD_PARAMS;
1765     goto cleanup;
1766   }
1767
1768   if (cfg_mgr->new != NULL)
1769     tr_cfg_free(cfg_mgr->new);
1770   cfg_mgr->new=tr_cfg_new(tmp_ctx); /* belongs to the temporary context for now */
1771   if (cfg_mgr->new == NULL) {
1772     cfg_rc=TR_CFG_NOMEM;
1773     goto cleanup;
1774   }
1775
1776   cfg_mgr->new->peers=trp_ptable_new(cfg_mgr);
1777
1778   /* Parse configuration information from each config file */
1779   for (ii=0; ii<n; ii++) {
1780     file_with_path=join_paths(tmp_ctx, config_dir, cfg_files[ii]->d_name); /* must free result with talloc_free */
1781     if(file_with_path == NULL) {
1782       tr_crit("tr_parse_config: error joining path.");
1783       cfg_rc=TR_CFG_NOMEM;
1784       goto cleanup;
1785     }
1786     tr_debug("tr_parse_config: Parsing %s.", cfg_files[ii]->d_name); /* print the filename without the path */
1787     cfg_rc=tr_cfg_parse_one_config_file(cfg_mgr->new, file_with_path);
1788     if (cfg_rc!=TR_CFG_SUCCESS) {
1789       tr_crit("tr_parse_config: Error parsing %s", file_with_path);
1790       goto cleanup;
1791     }
1792     talloc_free(file_with_path); /* done with filename */
1793   }
1794
1795   /* make sure we got a complete, consistent configuration */
1796   if (TR_CFG_SUCCESS != tr_cfg_validate(cfg_mgr->new)) {
1797     tr_err("tr_parse_config: Error: INVALID CONFIGURATION");
1798     cfg_rc=TR_CFG_ERROR;
1799     goto cleanup;
1800   }
1801
1802   /* success! */
1803   talloc_steal(cfg_mgr, cfg_mgr->new); /* hand this over to the cfg_mgr context */
1804   cfg_rc=TR_CFG_SUCCESS;
1805
1806 cleanup:
1807   talloc_free(tmp_ctx);
1808   return cfg_rc;
1809 }
1810
1811 TR_IDP_REALM *tr_cfg_find_idp (TR_CFG *tr_cfg, TR_NAME *idp_id, TR_CFG_RC *rc)
1812 {
1813
1814   TR_IDP_REALM *cfg_idp;
1815
1816   if ((!tr_cfg) || (!idp_id)) {
1817     if (rc)
1818       *rc = TR_CFG_BAD_PARAMS;
1819     return NULL;
1820   }
1821
1822   for (cfg_idp = tr_cfg->idp_realms; NULL != cfg_idp; cfg_idp = cfg_idp->next) {
1823     if (!tr_name_cmp (idp_id, cfg_idp->realm_id)) {
1824       tr_debug("tr_cfg_find_idp: Found %s.", idp_id->buf);
1825       return cfg_idp;
1826     }
1827   }
1828   /* if we didn't find one, return NULL */ 
1829   return NULL;
1830 }
1831
1832 TR_RP_CLIENT *tr_cfg_find_rp (TR_CFG *tr_cfg, TR_NAME *rp_gss, TR_CFG_RC *rc)
1833 {
1834   TR_RP_CLIENT *cfg_rp;
1835
1836   if ((!tr_cfg) || (!rp_gss)) {
1837     if (rc)
1838       *rc = TR_CFG_BAD_PARAMS;
1839     return NULL;
1840   }
1841
1842   for (cfg_rp = tr_cfg->rp_clients; NULL != cfg_rp; cfg_rp = cfg_rp->next) {
1843     if (tr_gss_names_matches(cfg_rp->gss_names, rp_gss)) {
1844       tr_debug("tr_cfg_find_rp: Found %s.", rp_gss->buf);
1845       return cfg_rp;
1846     }
1847   }
1848   /* if we didn't find one, return NULL */ 
1849   return NULL;
1850 }
1851
1852 static int is_cfg_file(const struct dirent *dent) {
1853   int n;
1854
1855   /* Only accept filenames ending in ".cfg" and starting with a character
1856    * other than an ASCII '.' */
1857
1858   /* filename must be at least 4 characters long to be acceptable */
1859   n=strlen(dent->d_name);
1860   if (n < 4) {
1861     return 0;
1862   }
1863
1864   /* filename must not start with '.' */
1865   if ('.' == dent->d_name[0]) {
1866     return 0;
1867   }
1868
1869   /* If the above passed and the last four characters of the filename are .cfg, accept.
1870    * (n.b., assumes an earlier test checked that the name is >= 4 chars long.) */
1871   if (0 == strcmp(&(dent->d_name[n-4]), ".cfg")) {
1872     return 1;
1873   }
1874
1875   /* otherwise, return false. */
1876   return 0;
1877 }
1878
1879 /* Find configuration files in a particular directory. Returns the
1880  * number of entries found, 0 if none are found, or <0 for some
1881  * errors. If n>=0, the cfg_files parameter will contain a newly
1882  * allocated array of pointers to struct dirent entries, as returned
1883  * by scandir(). These can be freed with tr_free_config_file_list().
1884  */
1885 int tr_find_config_files (const char *config_dir, struct dirent ***cfg_files) {
1886   int n = 0;
1887   
1888   n = scandir(config_dir, cfg_files, is_cfg_file, alphasort);
1889
1890   if (n < 0) {
1891     perror("scandir");
1892     tr_debug("tr_find_config: scandir error trying to scan %s.", config_dir);
1893   } 
1894
1895   return n;
1896 }
1897
1898 /* Free memory allocated for configuration file list returned from tr_find_config_files().
1899  * This can be called regardless of the return value of tr_find_config_values(). */
1900 void tr_free_config_file_list(int n, struct dirent ***cfg_files) {
1901   int ii;
1902
1903   /* if n < 0, then scandir did not allocate anything because it failed */
1904   if((n>=0) && (*cfg_files != NULL)) {
1905     for(ii=0; ii<n; ii++) {
1906       free((*cfg_files)[ii]);
1907     }
1908     free(*cfg_files); /* safe even if n==0 */
1909     *cfg_files=NULL; /* this will help prevent accidentally freeing twice */
1910   }
1911 }