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