7972cf8ad6138a30062e3ea7b9c0a5bbe4c938fd
[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
40 #include <tr_config.h>
41 #include <tr.h>
42
43 void tr_print_config (FILE *stream, TR_CFG *cfg) {
44   fprintf(stream, "tr_print_config: Not yet implemented.\n");
45   return;
46 }
47
48 void tr_cfg_free (TR_CFG *cfg) {
49   /* TBD -- need to deallocate memory as part of dynamic config */
50   return;
51 }
52
53 TR_CFG_RC tr_apply_new_config (TR_INSTANCE *tr) {
54
55   if (!tr)
56     return TR_CFG_BAD_PARAMS;
57
58   if (tr->active_cfg)
59     tr_cfg_free(tr->active_cfg);
60
61   tr->active_cfg = tr->new_cfg;
62   return TR_CFG_SUCCESS;
63 }
64
65 static TR_CFG_RC tr_cfg_parse_internal (TR_INSTANCE *tr, json_t *jcfg) {
66   json_t *jint = NULL;
67   json_t *jmtd = NULL;
68
69   if ((!tr) || (!tr->new_cfg) || (!jcfg))
70     return TR_CFG_BAD_PARAMS;
71
72   if (NULL == (tr->new_cfg->internal = malloc(sizeof(TR_CFG_INTERNAL))))
73     return TR_CFG_NOMEM;
74
75   memset(tr->new_cfg->internal, 0, sizeof(TR_CFG_INTERNAL));
76
77   if ((NULL != (jint = json_object_get(jcfg, "tr_internal"))) &&
78       (NULL != (jmtd = json_object_get(jint, "max_tree_depth")))) {
79     if (json_is_number(jmtd)) {
80       tr->new_cfg->internal->max_tree_depth = json_integer_value(jmtd);
81     } else {
82       fprintf(stderr,"tr_cfg_parse_internal: Parsing error, max_tree_depth is not a number.\n");
83       return TR_CFG_NOPARSE;
84     }
85   } else {
86     /* If not configured, use the default */
87     tr->new_cfg->internal->max_tree_depth = TR_DEFAULT_MAX_TREE_DEPTH;
88   }
89   fprintf(stderr, "tr_cfg_parse_internal: Internal config parsed.\n");
90   return TR_CFG_SUCCESS;
91 }
92
93 static TR_RP_CLIENT *tr_cfg_parse_one_rp_client (TR_INSTANCE *tr, json_t *jrp, TR_CFG_RC *rc) 
94 {
95   TR_RP_CLIENT *rp = NULL;
96   json_t *jgns = NULL;
97   json_t *jfilt = NULL;
98   json_t *jfls = NULL;
99   json_t *jftype = NULL;
100   json_t *jfact = NULL;
101   json_t *jfspecs = NULL;
102   json_t *jffield = NULL;
103   json_t *jfrealm = NULL;
104   int i = 0;
105
106   if ((!jrp) || (!rc)) {
107     fprintf(stderr, "tr_cfg_parse_one_rp_realm: Bad parameters.\n");
108     if (rc)
109       *rc = TR_CFG_BAD_PARAMS;
110     return NULL;
111   }
112
113   if (NULL == (rp = malloc(sizeof(TR_RP_CLIENT)))) {
114     fprintf(stderr, "tr_config_parse_one_rp_realm: Out of memory.\n");
115     *rc = TR_CFG_NOMEM;
116     return NULL;
117   }
118   
119   memset(rp, 0, sizeof(TR_RP_CLIENT));
120
121   if ((NULL == (jfilt = json_object_get(jrp, "filter"))) || 
122       (NULL == (jfls = json_object_get(jfilt, "filter_lines"))) ||
123       (!json_is_array(jfls)) ||
124       (NULL == (jgns = json_object_get(jrp, "gss_names"))) ||
125       (!json_is_array(jgns))) {
126     fprintf(stderr, "tr_cfg_parse_one_rp_client: Error parsing RP client configuration.\n");
127     free(rp);
128     *rc = TR_CFG_NOPARSE;
129     return NULL;
130   }
131
132   if (0 == json_array_size(jfls)) {
133     fprintf(stderr, "tr_cfg_parse_one_rp_client: RP Client has no filter lines.\n");
134     *rc = TR_CFG_NOPARSE;
135     return NULL;
136   }
137
138   if ((NULL == (jftype = json_object_get(jfilt, "type"))) ||
139       (!json_is_string(jftype)) ||
140       (strcmp(json_string_value(jftype), "rp_permitted"))) {
141     fprintf(stderr, "tr_cfg_parse_one_rp_client: Error parsing RP client filter type.\n");
142     *rc = TR_CFG_NOPARSE;
143     return NULL;
144   }
145
146
147   /* Right now, we only accept one type of filter, and we only care
148    * about one per rp_client. */
149   if ((NULL == (jfact = json_object_get(json_array_get(jfls, 0), "action"))) ||
150       (!json_is_string(jfact)) ||
151       (strcmp(json_string_value(jfact), "accept"))) {
152     fprintf(stderr, "tr_cfg_parse_one_rp_client: Error parsing RP client filter action.\n");
153     *rc = TR_CFG_NOPARSE;
154     return NULL;
155       }
156
157   if ((NULL == (jfspecs = json_object_get(json_array_get(jfls, 0), "filter_specs"))) ||
158       (!json_is_array(jfspecs)) ||
159       (0 == json_array_size(jfspecs))) {
160       fprintf(stderr, "tr_cfg_parse_one_rp_client: Error parsing RP client filter specs.\n");
161       *rc = TR_CFG_NOPARSE;
162       return NULL;
163   }
164
165   if ((NULL == (jffield = json_object_get(json_array_get(jfspecs, 0), "field"))) ||
166       (!json_is_string(jffield)) ||
167       (strcmp(json_string_value(jffield), "rp_realm")) ||
168       (NULL == (jfrealm = json_object_get(json_array_get(jfspecs, 0), "match"))) ||
169       (!json_is_string(jfrealm))) {
170       fprintf(stderr, "tr_cfg_parse_one_rp_client: Error parsing RP client filter field and match.\n");
171       *rc = TR_CFG_NOPARSE;
172       return NULL;
173   }
174
175   rp->rp_match = tr_new_name((char *)json_string_value(jfrealm));
176
177   if (0 == json_array_size(jgns)) {
178     fprintf(stderr, "tr_cfg_parse_one_rp_client: RP Client has no GSS Names.\n");
179     *rc = TR_CFG_NOPARSE;
180     return NULL;
181   }
182
183   if (TR_MAX_GSS_NAMES < json_array_size(jgns)) {
184     fprintf(stderr, "tr_cfg_parse_one_rp_client: RP Client has too many GSS Names.\n");
185     *rc = TR_CFG_NOPARSE;
186     return NULL;
187   }
188
189   for (i = 0; i < json_array_size(jgns); i++) {
190     if (NULL == (rp->gss_names[i] = tr_new_name ((char *)json_string_value(json_array_get(jgns, i))))) {
191       fprintf(stderr, "tr_cfg_parse_one_rp_client: No memory for GSS Name.\n");
192       *rc = TR_CFG_NOMEM;
193       return NULL;
194     }
195   }
196   
197   return rp;
198 }
199
200 static TR_CFG_RC tr_cfg_parse_rp_clients (TR_INSTANCE *tr, json_t *jcfg) {
201   json_t *jrps = NULL;
202   TR_RP_CLIENT *rp = NULL;
203   TR_CFG_RC rc = TR_CFG_SUCCESS;
204   int i = 0;
205
206   if ((!tr) || (!tr->new_cfg) || (!jcfg))
207     return TR_CFG_BAD_PARAMS;
208
209   if ((NULL == (jrps = json_object_get(jcfg, "rp_clients"))) ||
210       (!json_is_array(jrps))) {
211     return TR_CFG_NOPARSE;
212   }
213
214   for (i = 0; i < json_array_size(jrps); i++) {
215     if (NULL == (rp = tr_cfg_parse_one_rp_client(tr, 
216                                                  json_array_get(jrps, i), 
217                                                  &rc))) {
218        return rc;
219     }
220     fprintf(stderr, "tr_cfg_parse_rp_clients: RP client configured -- first gss: %s, rp_realm: %s\n", rp->gss_names[0]->buf, rp->rp_match->buf);
221     rp->next = tr->new_cfg->rp_clients;
222     tr->new_cfg->rp_clients = rp;
223   }
224   return rc;
225 }
226
227 static TR_AAA_SERVER *tr_cfg_parse_one_aaa_server (TR_INSTANCE *tr, json_t *jaddr, TR_CFG_RC *rc) {
228   TR_AAA_SERVER *aaa = NULL;
229
230   if ((!tr) || (!tr->new_cfg) || (!jaddr) || (!json_is_string(jaddr))) {
231     fprintf(stderr, "tr_cfg_parse_one_aaa_server: Bad parameters.\n");
232     *rc = TR_CFG_BAD_PARAMS;
233     return NULL;
234   }
235
236   if (NULL == (aaa = malloc(sizeof(TR_AAA_SERVER)))) {
237     fprintf(stderr, "tr_config_parse_one_aaa_server: Out of memory.\n");
238     *rc = TR_CFG_NOMEM;
239     return NULL;
240   }
241
242   memset(aaa, 0, sizeof(TR_AAA_SERVER));
243
244   /* TBD -- Handle IPv6 addresses */
245   inet_aton(json_string_value(jaddr), &(aaa->aaa_server_addr));
246
247   return aaa;
248 }
249
250
251 static TR_AAA_SERVER *tr_cfg_parse_aaa_servers (TR_INSTANCE *tr, json_t *jaaas, TR_CFG_RC *rc) 
252 {
253   TR_AAA_SERVER *aaa = NULL;
254   TR_AAA_SERVER *temp_aaa = NULL;
255   int i = 0;
256
257   for (i = 0; i < json_array_size(jaaas); i++) {
258     if (NULL == (temp_aaa = tr_cfg_parse_one_aaa_server(tr, json_array_get(jaaas, i), rc))) {
259       return NULL;
260     }
261     /* TBD -- IPv6 addresses */
262     //    fprintf(stderr, "tr_cfg_parse_aaa_servers: Configuring AAA Server: ip_addr = %s.\n", inet_ntoa(temp_aaa->aaa_server_addr));
263     temp_aaa->next = aaa;
264     aaa = temp_aaa;
265   }
266   return aaa;
267 }
268
269 static TR_APC *tr_cfg_parse_apcs (TR_INSTANCE *tr, json_t *japcs, TR_CFG_RC *rc)
270 {
271   TR_APC *apc;
272
273   *rc = TR_CFG_SUCCESS;         /* presume success */
274
275   if ((!japcs) || (!rc)) {
276     fprintf(stderr, "tr_cfg_parse_apcs: Bad parameters.\n");
277     if (rc) 
278       *rc = TR_CFG_BAD_PARAMS;
279     return NULL;
280   }
281
282   if (NULL == (apc = malloc(sizeof(TR_APC)))) {
283     fprintf (stderr, "tr_cfg_parse_apcs: Out of memory.\n");
284     *rc = TR_CFG_NOMEM;
285     return NULL;
286   }
287
288   memset(apc, 0, sizeof(TR_APC));
289
290   /* TBD, deal with more than one APC.  In the meantime, though...                */
291   /* Only parse the first APC, because we only know how to deal with one, anyway. */
292   if (0 == json_array_size(japcs))
293     return NULL;
294
295   if (NULL == (apc->id = tr_new_name((char *)json_string_value(json_array_get(japcs, 0))))) {
296     fprintf(stderr, "tr_cfg_parse_apcs: No memory for APC name.\n");
297     *rc = TR_CFG_NOMEM;
298     return NULL;
299   }
300
301   return apc;
302 }
303
304 static TR_IDP_REALM *tr_cfg_parse_one_idp_realm (TR_INSTANCE *tr, json_t *jidp, TR_CFG_RC *rc) {
305   TR_IDP_REALM *idp = NULL;
306   json_t *jrid = NULL;
307   json_t *jscfg = NULL;
308   json_t *jsrvrs = NULL;
309   json_t *japcs = NULL;
310
311   if ((!jidp) || (!rc)) {
312     fprintf(stderr, "tr_cfg_parse_one_idp_realm: Bad parameters.\n");
313     if (rc)
314       *rc = TR_CFG_BAD_PARAMS;
315     return NULL;
316   }
317
318   if (NULL == (idp = malloc(sizeof(TR_IDP_REALM)))) {
319     fprintf(stderr, "tr_config_parse_one_idp_realm: Out of memory.\n");
320     *rc = TR_CFG_NOMEM;
321     return NULL;
322   }
323
324   memset(idp, 0, sizeof(TR_IDP_REALM));
325
326   if ((NULL == (jrid = json_object_get(jidp, "realm_id"))) ||
327       (!json_is_string(jrid)) ||
328       (NULL == (jscfg = json_object_get(jidp, "shared_config"))) ||
329       (!json_is_string(jscfg)) ||
330       (NULL == (jsrvrs = json_object_get(jidp, "aaa_servers"))) ||
331       (!json_is_array(jsrvrs))) {
332     fprintf(stderr, "tr_cfg_parse_one_idp_realm: Error parsing IDP realm configuration.\n");
333     free(idp);
334     *rc = TR_CFG_NOPARSE;
335     return NULL;
336   }
337
338   if (0 == strcmp(json_string_value(jscfg), "no")) {
339     idp->shared_config = 0;
340   } else {
341     idp->shared_config = 1;
342   }
343
344   if (NULL == (idp->realm_id = tr_new_name((char *)json_string_value(jrid)))) {
345     free(idp);
346     fprintf(stderr, "tr_cfg_parse_one_idp_realm: No memory for realm id.\n");
347     *rc = TR_CFG_NOMEM;
348     return NULL;
349   }
350
351   if (NULL == (idp->aaa_servers = tr_cfg_parse_aaa_servers(tr, jsrvrs, rc))) {
352     fprintf(stderr, "tr_cfg_parse_one_idp_realm: Can't parse AAA servers for realm %s.\n", idp->realm_id->buf);
353     tr_free_name(idp->realm_id);
354     free(idp);
355     return NULL;
356   }
357
358   if ((NULL != (japcs = json_object_get(jidp, "apcs"))) &&
359       (json_is_array(japcs))) {
360     if (NULL != (idp->apcs = tr_cfg_parse_apcs(tr, japcs, rc))) {
361       fprintf(stderr, "tr_cfg_parse_one_idp_realm: Can't parse APCs for realm %s .\n", idp->realm_id->buf);
362       tr_free_name(idp->realm_id);
363       /* TBD -- free aaa_servers */;
364       free(idp);
365       return NULL;
366     }
367   } 
368   return idp;
369 }
370
371 static TR_CFG_RC tr_cfg_parse_idp_realms (TR_INSTANCE *tr, json_t *jcfg) 
372 {
373   json_t *jidps = NULL;
374   TR_CFG_RC rc = TR_CFG_SUCCESS;
375   TR_IDP_REALM *idp = NULL;
376   int i = 0;
377
378   if ((!tr) || (!tr->new_cfg) || (!jcfg))
379     return TR_CFG_BAD_PARAMS;
380
381   if ((NULL == (jidps = json_object_get(jcfg, "idp_realms"))) ||
382       (!json_is_array(jidps))) {
383     return TR_CFG_NOPARSE;
384   }
385
386   for (i = 0; i < json_array_size(jidps); i++) {
387     if (NULL == (idp = tr_cfg_parse_one_idp_realm(tr, 
388                                                   json_array_get(jidps, i), 
389                                                   &rc))) {
390        return rc;
391     }
392     fprintf(stderr, "tr_cfg_parse_idp_realms: IDP realm configured: %s.\n", idp->realm_id->buf);
393     idp->next = tr->new_cfg->idp_realms;
394     tr->new_cfg->idp_realms = idp;
395   }
396   return rc;
397 }
398
399 static TR_IDP_REALM *tr_cfg_parse_comm_idps (TR_INSTANCE *tr, json_t *jidps, TR_CFG_RC *rc)
400 {
401   TR_IDP_REALM *idp = NULL;
402   TR_IDP_REALM *temp_idp = NULL;
403   int i = 0;
404
405   if ((!tr) ||
406       (!jidps) ||
407       (!json_is_array(jidps))) {
408     if (rc)
409       *rc = TR_CFG_BAD_PARAMS;
410     return NULL;
411   }
412
413   for (i = 0; i < json_array_size(jidps); i++) {
414     if (NULL == (temp_idp = (tr_cfg_find_idp(tr->new_cfg, 
415                                              tr_new_name((char *)json_string_value(json_array_get(jidps, i))), 
416                                              rc)))) {
417       fprintf(stderr, "tr_cfg_parse_comm_idps: Unknown IDP %s.\n", 
418               (char *)json_string_value(json_array_get(jidps, i)));
419       return NULL;
420     }
421
422     temp_idp->comm_next = idp;
423     idp = temp_idp;
424   }
425
426   return idp;
427 }
428
429 static TR_RP_REALM *tr_cfg_parse_comm_rps (TR_INSTANCE *tr, json_t *jrps, TR_CFG_RC *rc)
430 {
431   TR_RP_REALM *rp = NULL;
432   TR_RP_REALM *temp_rp = NULL;
433   int i = 0;
434
435   if ((!tr) ||
436       (!jrps) ||
437       (!json_is_array(jrps))) {
438     if (rc)
439       *rc = TR_CFG_BAD_PARAMS;
440     return NULL;
441   }
442
443   for (i = (json_array_size(jrps)-1); i >= 0; i--) {
444     if (NULL == (temp_rp = malloc(sizeof(TR_RP_REALM)))) {
445       fprintf(stderr, "tr_cfg_parse_comm_rps: Can't allocate memory for RP Realm.\n");
446       if (rc)
447         *rc = TR_CFG_NOMEM;
448       return NULL;
449     }
450     memset (temp_rp, 0, sizeof(TR_RP_REALM));
451
452     if (NULL == (temp_rp->realm_name = tr_new_name((char *)json_string_value(json_array_get(jrps, i))))) {
453       fprintf(stderr, "tr_cfg_parse_comm_rps: No memory for RP Realm Name.\n");
454       if (rc)
455         *rc = TR_CFG_NOMEM;
456       return NULL;
457     }
458
459     temp_rp->next = rp;
460     rp = temp_rp;
461   }
462
463   return rp;
464 }
465
466 static TR_COMM *tr_cfg_parse_one_comm (TR_INSTANCE *tr, json_t *jcomm, TR_CFG_RC *rc) {
467   TR_COMM *comm = NULL;
468   json_t *jid = NULL;
469   json_t *jtype = NULL;
470   json_t *japcs = NULL;
471   json_t *jidps = NULL;
472   json_t *jrps = NULL;
473
474   if ((!jcomm) || (!rc)) {
475     fprintf(stderr, "tr_cfg_parse_one_comm: Bad parameters.\n");
476     if (rc)
477       *rc = TR_CFG_BAD_PARAMS;
478     return NULL;
479   }
480
481   if (NULL == (comm = malloc(sizeof(TR_COMM)))) {
482     fprintf(stderr, "tr_config_parse_one_comm: Out of memory.\n");
483     *rc = TR_CFG_NOMEM;
484     return NULL;
485   }
486
487   memset(comm, 0, sizeof(TR_COMM));
488
489   if ((NULL == (jid = json_object_get(jcomm, "community_id"))) ||
490       (!json_is_string(jid)) ||
491       (NULL == (jtype = json_object_get(jcomm, "type"))) ||
492       (!json_is_string(jtype)) ||
493       (NULL == (japcs = json_object_get(jcomm, "apcs"))) ||
494       (!json_is_array(japcs)) ||
495       (NULL == (jidps = json_object_get(jcomm, "idp_realms"))) ||
496       (!json_is_array(jidps)) ||
497       (NULL == (jrps = json_object_get(jcomm, "rp_realms"))) ||
498       (!json_is_array(jrps))) {
499     fprintf(stderr, "tr_cfg_parse_one_comm: Error parsing Communities configuration.\n");
500     free(comm);
501     *rc = TR_CFG_NOPARSE;
502     return NULL;
503   }
504
505   if (NULL == (comm->id = tr_new_name((char *)json_string_value(jid)))) {
506     free(comm);
507     fprintf(stderr, "tr_cfg_parse_one_comm: No memory for community id.\n");
508     *rc = TR_CFG_NOMEM;
509     return NULL;
510   }
511
512   if (0 == strcmp(json_string_value(jtype), "apc")) {
513     comm->type = TR_COMM_APC;
514   } else if (0 == strcmp(json_string_value(jtype), "coi")) {
515     comm->type = TR_COMM_COI;
516     if (NULL == (comm->apcs = tr_cfg_parse_apcs(tr, japcs, rc))) {
517       fprintf(stderr, "tr_cfg_parse_one_comm: Can't parse APCs for COI %s.\n", comm->id->buf);
518       tr_free_name(comm->id);
519       free(comm);
520       return NULL;
521     }
522   } else {
523     fprintf(stderr, "tr_cfg_parse_one_comm: Invalid community type, comm = %s, type = %s\n", comm->id->buf, json_string_value(jtype));
524     tr_free_name(comm->id);
525     free(comm);
526     *rc = TR_CFG_NOPARSE;
527     return NULL;
528   }
529
530   comm->idp_realms = tr_cfg_parse_comm_idps(tr, jidps, rc);
531   if (TR_CFG_SUCCESS != *rc) {
532     fprintf(stderr, "tr_cfg_parse_one_comm: Can't parse IDP realms for comm %s.\n", comm->id->buf);
533     tr_free_name(comm->id);
534     free(comm);
535     return NULL;
536   }
537
538   comm->rp_realms = tr_cfg_parse_comm_rps(tr, jrps, rc);
539   if (TR_CFG_SUCCESS != *rc) {
540     fprintf(stderr, "tr_cfg_parse_comm: Can't parse RP realms for comm %s .\n", comm->id->buf);
541     tr_free_name(comm->id);
542     /* TBD -- free idps? */;
543     free(comm);
544     return NULL;
545   }
546
547   return comm;
548 }
549
550 static TR_CFG_RC tr_cfg_parse_comms (TR_INSTANCE *tr, json_t *jcfg) 
551 {
552   json_t *jcomms = NULL;
553   TR_CFG_RC rc = TR_CFG_SUCCESS;
554   TR_COMM *comm = NULL;
555   int i = 0;
556
557   if ((!tr) || (!tr->new_cfg) || (!jcfg)) {
558     fprintf(stderr, "tr_config_parse_comms: Bad Parameters.\n");
559     return TR_CFG_BAD_PARAMS;
560   }
561
562   if (NULL == (comm = malloc(sizeof(TR_COMM)))) {
563     fprintf(stderr, "tr_cfg_parse_comms: Out of Memory\n");
564     return TR_CFG_NOMEM;
565   }
566   memset (comm, 0, sizeof(TR_COMM));
567
568   if ((NULL == (jcomms = json_object_get(jcfg, "communities"))) ||
569       (!json_is_array(jcomms))) {
570     return TR_CFG_NOPARSE;
571   }
572
573   for (i = 0; i < json_array_size(jcomms); i++) {
574     if (NULL == (comm = tr_cfg_parse_one_comm(tr, 
575                                               json_array_get(jcomms, i), 
576                                               &rc))) {
577        return rc;
578     }
579     fprintf(stderr, "tr_cfg_parse_comms: Community configured: %s.\n", comm->id->buf);
580     comm->next = tr->new_cfg->comms;
581     tr->new_cfg->comms = comm;
582   }
583   return rc;
584 }
585
586 TR_CFG_RC tr_parse_config (TR_INSTANCE *tr, json_t *jcfg) {
587
588   /* If there is a partial/abandoned config lying around, free it */
589   if (tr->new_cfg) {
590     tr_cfg_free(tr->new_cfg);
591   }
592   
593   if (NULL == (tr->new_cfg = malloc(sizeof(TR_CFG))))
594     return TR_CFG_NOMEM;
595
596   memset(tr->new_cfg, 0, sizeof(TR_CFG));
597
598   if ((TR_CFG_SUCCESS != tr_cfg_parse_internal(tr, jcfg)) ||
599       (TR_CFG_SUCCESS != tr_cfg_parse_rp_clients(tr, jcfg)) ||
600       (TR_CFG_SUCCESS != tr_cfg_parse_idp_realms(tr, jcfg)) ||
601       (TR_CFG_SUCCESS != tr_cfg_parse_comms(tr, jcfg))) {
602     tr_cfg_free(tr->new_cfg);
603     return TR_CFG_ERROR;
604   }
605   return TR_CFG_SUCCESS;
606 }
607
608 TR_IDP_REALM *tr_cfg_find_idp (TR_CFG *tr_cfg, TR_NAME *idp_id, TR_CFG_RC *rc)
609 {
610
611   TR_IDP_REALM *cfg_idp;
612
613   if ((!tr_cfg) || (!idp_id)) {
614     if (rc)
615       *rc = TR_CFG_BAD_PARAMS;
616     return NULL;
617   }
618
619   for (cfg_idp = tr_cfg->idp_realms; NULL != cfg_idp; cfg_idp = cfg_idp->next) {
620     if (!tr_name_cmp (idp_id, cfg_idp->realm_id)) {
621       fprintf(stderr, "tr_cfg_find_idp: Found %s.\n", idp_id->buf);
622       return cfg_idp;
623     }
624   }
625   /* if we didn't find one, return NULL */ 
626   return NULL;
627 }
628
629 TR_RP_CLIENT *tr_cfg_find_rp (TR_CFG *tr_cfg, TR_NAME *rp_gss, TR_CFG_RC *rc)
630 {
631   TR_RP_CLIENT *cfg_rp;
632   int i;
633
634   if ((!tr_cfg) || (!rp_gss)) {
635     if (rc)
636       *rc = TR_CFG_BAD_PARAMS;
637     return NULL;
638   }
639
640   for (cfg_rp = tr_cfg->rp_clients; NULL != cfg_rp; cfg_rp = cfg_rp->next) {
641     for (i = 0; i < TR_MAX_GSS_NAMES; i++) {
642       if (!tr_name_cmp (rp_gss, cfg_rp->gss_names[i])) {
643         fprintf(stderr, "tr_cfg_find_rp: Found %s.\n", rp_gss->buf);
644         return cfg_rp;
645       }
646     }
647   }
648   /* if we didn't find one, return NULL */ 
649   return NULL;
650 }
651
652 json_t *tr_read_config (int n, struct dirent **cfg_files) {
653   json_t *jcfg = NULL;
654   json_t *temp = NULL;
655   json_error_t err;
656
657   if (!cfg_files)
658     return NULL;
659
660   while (n--) {
661     fprintf(stderr, "tr_read_config: Parsing %s.\n", cfg_files[n]->d_name);
662     if (NULL == (temp = json_load_file(cfg_files[n]->d_name, JSON_DISABLE_EOF_CHECK, &err))) {
663       fprintf (stderr, "tr_read_config: Error parsing config file %s.\n", cfg_files[n]->d_name);
664       return NULL;
665     }
666
667     /* TBD -- instead of using json_object_update, iterate through files for non-overlap config? */
668     if (!jcfg) {
669       jcfg = temp;
670     }else {
671       if (-1 == json_object_update(jcfg, temp)) {
672         fprintf(stderr, "tr_read_config: Error merging config information.\n");
673         return NULL;
674       }
675     }
676   }
677
678   //  fprintf(stderr, "tr_read_config: Merged configuration complete:\n%s\n", json_dumps(jcfg, 0));
679
680   return jcfg;
681 }
682
683 static int is_cfg_file(const struct dirent *dent) {
684   int n;
685
686   /* if the last four letters of the filename are .cfg, return true. */
687   if ((4 <= (n = strlen(dent->d_name))) &&
688       (0 == strcmp(&(dent->d_name[n-4]), ".cfg"))) {
689     return 1;
690   }
691
692   /* otherwise, return false. */
693   return 0;
694 }
695
696 int tr_find_config_files (struct dirent ***cfg_files) {
697   int n = 0, i = 0;
698   
699   n = scandir(".", cfg_files, &is_cfg_file, 0);
700
701   if (n < 0) {
702     perror("scandir");
703     fprintf(stderr, "tr_find_config: scandir error.\n");
704     return 0;
705   }
706
707   if (n == 0) {
708     fprintf (stderr, "tr_find_config: No config files found.\n");
709     return 0;
710   }
711
712   i = n;
713   while(i--) {
714     fprintf(stderr, "tr_find_config: Config file found (%s).\n", (*cfg_files)[i]->d_name);
715   }
716     
717   return n;
718 }