Fix logic error in gss name comparison.
[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       (NULL == (japcs = json_object_get(jidp, "apcs"))) ||
333       (!json_is_array(japcs))) {
334     fprintf(stderr, "tr_cfg_parse_one_idp_realm: Error parsing IDP realm configuration.\n");
335     free(idp);
336     *rc = TR_CFG_NOPARSE;
337     return NULL;
338   }
339
340   if (0 == strcmp(json_string_value(jscfg), "no")) {
341     idp->shared_config = 0;
342   } else {
343     idp->shared_config = 1;
344   }
345
346   if (NULL == (idp->realm_id = tr_new_name((char *)json_string_value(jrid)))) {
347     free(idp);
348     fprintf(stderr, "tr_cfg_parse_one_idp_realm: No memory for realm id.\n");
349     *rc = TR_CFG_NOMEM;
350     return NULL;
351   }
352
353   if (NULL == (idp->aaa_servers = tr_cfg_parse_aaa_servers(tr, jsrvrs, rc))) {
354     fprintf(stderr, "tr_cfg_parse_one_idp_realm: Can't parse AAA servers for realm %s.\n", idp->realm_id->buf);
355     tr_free_name(idp->realm_id);
356     free(idp);
357     return NULL;
358   }
359   if (NULL == (idp->apcs = tr_cfg_parse_apcs(tr, japcs, rc))) {
360     fprintf(stderr, "tr_cfg_parse_one_idp_realm: Can't parse APCs for realm %s .\n", idp->realm_id->buf);
361     tr_free_name(idp->realm_id);
362     /* TBD -- free aaa_servers */;
363     free(idp);
364     return NULL;
365   }
366
367 return idp;
368 }
369
370 static TR_CFG_RC tr_cfg_parse_idp_realms (TR_INSTANCE *tr, json_t *jcfg) 
371 {
372   json_t *jidps = NULL;
373   TR_CFG_RC rc = TR_CFG_SUCCESS;
374   TR_IDP_REALM *idp = NULL;
375   int i = 0;
376
377   if ((!tr) || (!tr->new_cfg) || (!jcfg))
378     return TR_CFG_BAD_PARAMS;
379
380   if ((NULL == (jidps = json_object_get(jcfg, "idp_realms"))) ||
381       (!json_is_array(jidps))) {
382     return TR_CFG_NOPARSE;
383   }
384
385   for (i = 0; i < json_array_size(jidps); i++) {
386     if (NULL == (idp = tr_cfg_parse_one_idp_realm(tr, 
387                                                   json_array_get(jidps, i), 
388                                                   &rc))) {
389        return rc;
390     }
391     fprintf(stderr, "tr_cfg_parse_idp_realms: IDP realm configured: %s.\n", idp->realm_id->buf);
392     idp->next = tr->new_cfg->idp_realms;
393     tr->new_cfg->idp_realms = idp;
394   }
395   return rc;
396 }
397
398 static TR_IDP_REALM *tr_cfg_parse_comm_idps (TR_INSTANCE *tr, json_t *jidps, TR_CFG_RC *rc)
399 {
400   TR_IDP_REALM *idp = NULL;
401   TR_IDP_REALM *temp_idp = NULL;
402   int i = 0;
403
404   if ((!tr) ||
405       (!jidps) ||
406       (!json_is_array(jidps))) {
407     if (rc)
408       *rc = TR_CFG_BAD_PARAMS;
409     return NULL;
410   }
411
412   for (i = 0; i < json_array_size(jidps); i++) {
413     if (NULL == (temp_idp = (tr_cfg_find_idp(tr->new_cfg, 
414                                              tr_new_name((char *)json_string_value(json_array_get(jidps, i))), 
415                                              rc)))) {
416       fprintf(stderr, "tr_cfg_parse_comm_idps: Unknown IDP %s.\n", 
417               (char *)json_string_value(json_array_get(jidps, i)));
418       return NULL;
419     }
420
421     temp_idp->comm_next = idp;
422     idp = temp_idp;
423   }
424
425   return idp;
426 }
427
428 static TR_RP_REALM *tr_cfg_parse_comm_rps (TR_INSTANCE *tr, json_t *jrps, TR_CFG_RC *rc)
429 {
430   TR_RP_REALM *rp = NULL;
431   TR_RP_REALM *temp_rp = NULL;
432   int i = 0;
433
434   if ((!tr) ||
435       (!jrps) ||
436       (!json_is_array(jrps))) {
437     if (rc)
438       *rc = TR_CFG_BAD_PARAMS;
439     return NULL;
440   }
441
442   for (i = (json_array_size(jrps)-1); i >= 0; i--) {
443     if (NULL == (temp_rp = malloc(sizeof(TR_RP_REALM)))) {
444       fprintf(stderr, "tr_cfg_parse_comm_rps: Can't allocate memory for RP Realm.\n");
445       if (rc)
446         *rc = TR_CFG_NOMEM;
447       return NULL;
448     }
449     memset (temp_rp, 0, sizeof(TR_RP_REALM));
450
451     if (NULL == (temp_rp->realm_name = tr_new_name((char *)json_string_value(json_array_get(jrps, i))))) {
452       fprintf(stderr, "tr_cfg_parse_comm_rps: No memory for RP Realm Name.\n");
453       if (rc)
454         *rc = TR_CFG_NOMEM;
455       return NULL;
456     }
457
458     temp_rp->next = rp;
459     rp = temp_rp;
460   }
461
462   return rp;
463 }
464
465 static TR_COMM *tr_cfg_parse_one_comm (TR_INSTANCE *tr, json_t *jcomm, TR_CFG_RC *rc) {
466   TR_COMM *comm = NULL;
467   json_t *jid = NULL;
468   json_t *jtype = NULL;
469   json_t *japcs = NULL;
470   json_t *jidps = NULL;
471   json_t *jrps = NULL;
472
473   if ((!jcomm) || (!rc)) {
474     fprintf(stderr, "tr_cfg_parse_one_comm: Bad parameters.\n");
475     if (rc)
476       *rc = TR_CFG_BAD_PARAMS;
477     return NULL;
478   }
479
480   if (NULL == (comm = malloc(sizeof(TR_COMM)))) {
481     fprintf(stderr, "tr_config_parse_one_comm: Out of memory.\n");
482     *rc = TR_CFG_NOMEM;
483     return NULL;
484   }
485
486   memset(comm, 0, sizeof(TR_COMM));
487
488   if ((NULL == (jid = json_object_get(jcomm, "community_id"))) ||
489       (!json_is_string(jid)) ||
490       (NULL == (jtype = json_object_get(jcomm, "type"))) ||
491       (!json_is_string(jtype)) ||
492       (NULL == (japcs = json_object_get(jcomm, "apcs"))) ||
493       (!json_is_array(japcs)) ||
494       (NULL == (jidps = json_object_get(jcomm, "idp_realms"))) ||
495       (!json_is_array(jidps)) ||
496       (NULL == (jrps = json_object_get(jcomm, "rp_realms"))) ||
497       (!json_is_array(jrps))) {
498     fprintf(stderr, "tr_cfg_parse_one_comm: Error parsing Communities configuration.\n");
499     free(comm);
500     *rc = TR_CFG_NOPARSE;
501     return NULL;
502   }
503
504   if (NULL == (comm->id = tr_new_name((char *)json_string_value(jid)))) {
505     free(comm);
506     fprintf(stderr, "tr_cfg_parse_one_comm: No memory for community id.\n");
507     *rc = TR_CFG_NOMEM;
508     return NULL;
509   }
510
511   if (0 == strcmp(json_string_value(jtype), "apc")) {
512     comm->type = TR_COMM_APC;
513   } else if (0 == strcmp(json_string_value(jtype), "coi")) {
514     comm->type = TR_COMM_COI;
515     if (NULL == (comm->apcs = tr_cfg_parse_apcs(tr, japcs, rc))) {
516       fprintf(stderr, "tr_cfg_parse_one_comm: Can't parse APCs for COI %s.\n", comm->id->buf);
517       tr_free_name(comm->id);
518       free(comm);
519       return NULL;
520     }
521   } else {
522     fprintf(stderr, "tr_cfg_parse_one_comm: Invalid community type, comm = %s, type = %s\n", comm->id->buf, json_string_value(jtype));
523     tr_free_name(comm->id);
524     free(comm);
525     *rc = TR_CFG_NOPARSE;
526     return NULL;
527   }
528
529   comm->idp_realms = tr_cfg_parse_comm_idps(tr, jidps, rc);
530   if (TR_CFG_SUCCESS != *rc) {
531     fprintf(stderr, "tr_cfg_parse_one_comm: Can't parse IDP realms for comm %s.\n", comm->id->buf);
532     tr_free_name(comm->id);
533     free(comm);
534     return NULL;
535   }
536
537   comm->rp_realms = tr_cfg_parse_comm_rps(tr, jrps, rc);
538   if (TR_CFG_SUCCESS != *rc) {
539     fprintf(stderr, "tr_cfg_parse_comm: Can't parse RP realms for comm %s .\n", comm->id->buf);
540     tr_free_name(comm->id);
541     /* TBD -- free idps? */;
542     free(comm);
543     return NULL;
544   }
545
546   return comm;
547 }
548
549 static TR_CFG_RC tr_cfg_parse_comms (TR_INSTANCE *tr, json_t *jcfg) 
550 {
551   json_t *jcomms = NULL;
552   TR_CFG_RC rc = TR_CFG_SUCCESS;
553   TR_COMM *comm = NULL;
554   int i = 0;
555
556   if ((!tr) || (!tr->new_cfg) || (!jcfg)) {
557     fprintf(stderr, "tr_config_parse_comms: Bad Parameters.\n");
558     return TR_CFG_BAD_PARAMS;
559   }
560
561   if (NULL == (comm = malloc(sizeof(TR_COMM)))) {
562     fprintf(stderr, "tr_cfg_parse_comms: Out of Memory\n");
563     return TR_CFG_NOMEM;
564   }
565   memset (comm, 0, sizeof(TR_COMM));
566
567   if ((NULL == (jcomms = json_object_get(jcfg, "communities"))) ||
568       (!json_is_array(jcomms))) {
569     return TR_CFG_NOPARSE;
570   }
571
572   for (i = 0; i < json_array_size(jcomms); i++) {
573     if (NULL == (comm = tr_cfg_parse_one_comm(tr, 
574                                               json_array_get(jcomms, i), 
575                                               &rc))) {
576        return rc;
577     }
578     fprintf(stderr, "tr_cfg_parse_comms: Community configured: %s.\n", comm->id->buf);
579     comm->next = tr->new_cfg->comms;
580     tr->new_cfg->comms = comm;
581   }
582   return rc;
583 }
584
585 TR_CFG_RC tr_parse_config (TR_INSTANCE *tr, json_t *jcfg) {
586
587   /* If there is a partial/abandoned config lying around, free it */
588   if (tr->new_cfg) {
589     tr_cfg_free(tr->new_cfg);
590   }
591   
592   if (NULL == (tr->new_cfg = malloc(sizeof(TR_CFG))))
593     return TR_CFG_NOMEM;
594
595   memset(tr->new_cfg, 0, sizeof(TR_CFG));
596
597   if ((TR_CFG_SUCCESS != tr_cfg_parse_internal(tr, jcfg)) ||
598       (TR_CFG_SUCCESS != tr_cfg_parse_rp_clients(tr, jcfg)) ||
599       (TR_CFG_SUCCESS != tr_cfg_parse_idp_realms(tr, jcfg)) ||
600       (TR_CFG_SUCCESS != tr_cfg_parse_comms(tr, jcfg))) {
601     tr_cfg_free(tr->new_cfg);
602     return TR_CFG_ERROR;
603   }
604   return TR_CFG_SUCCESS;
605 }
606
607 TR_IDP_REALM *tr_cfg_find_idp (TR_CFG *tr_cfg, TR_NAME *idp_id, TR_CFG_RC *rc)
608 {
609
610   TR_IDP_REALM *cfg_idp;
611
612   if ((!tr_cfg) || (!idp_id)) {
613     if (rc)
614       *rc = TR_CFG_BAD_PARAMS;
615     return NULL;
616   }
617
618   for (cfg_idp = tr_cfg->idp_realms; NULL != cfg_idp; cfg_idp = cfg_idp->next) {
619     if (!tr_name_cmp (idp_id, cfg_idp->realm_id)) {
620       fprintf(stderr, "tr_cfg_find_idp: Found %s.\n", idp_id->buf);
621       return cfg_idp;
622     }
623   }
624   /* if we didn't find one, return NULL */ 
625   return NULL;
626 }
627
628 TR_RP_CLIENT *tr_cfg_find_rp (TR_CFG *tr_cfg, TR_NAME *rp_gss, TR_CFG_RC *rc)
629 {
630   TR_RP_CLIENT *cfg_rp;
631   int i;
632
633   if ((!tr_cfg) || (!rp_gss)) {
634     if (rc)
635       *rc = TR_CFG_BAD_PARAMS;
636     return NULL;
637   }
638
639   for (cfg_rp = tr_cfg->rp_clients; NULL != cfg_rp; cfg_rp = cfg_rp->next) {
640     for (i = 0; i < TR_MAX_GSS_NAMES; i++) {
641       if (!tr_name_cmp (rp_gss, cfg_rp->gss_names[i])) {
642         fprintf(stderr, "tr_cfg_find_rp: Found %s.\n", rp_gss->buf);
643         return cfg_rp;
644       }
645     }
646   }
647   /* if we didn't find one, return NULL */ 
648   return NULL;
649 }
650
651 json_t *tr_read_config (int n, struct dirent **cfg_files) {
652   json_t *jcfg = NULL;
653   json_t *temp = NULL;
654   json_error_t err;
655
656   if (!cfg_files)
657     return NULL;
658
659   while (n--) {
660     fprintf(stderr, "tr_read_config: Parsing %s.\n", cfg_files[n]->d_name);
661     if (NULL == (temp = json_load_file(cfg_files[n]->d_name, JSON_DISABLE_EOF_CHECK, &err))) {
662       fprintf (stderr, "tr_read_config: Error parsing config file %s.\n", cfg_files[n]->d_name);
663       return NULL;
664     }
665
666     /* TBD -- instead of using json_object_update, iterate through files for non-overlap config? */
667     if (!jcfg) {
668       jcfg = temp;
669     }else {
670       if (-1 == json_object_update(jcfg, temp)) {
671         fprintf(stderr, "tr_read_config: Error merging config information.\n");
672         return NULL;
673       }
674     }
675   }
676
677   //  fprintf(stderr, "tr_read_config: Merged configuration complete:\n%s\n", json_dumps(jcfg, 0));
678
679   return jcfg;
680 }
681
682 static int is_cfg_file(const struct dirent *dent) {
683   int n;
684
685   /* if the last four letters of the filename are .cfg, return true. */
686   if ((4 <= (n = strlen(dent->d_name))) &&
687       (0 == strcmp(&(dent->d_name[n-4]), ".cfg"))) {
688     return 1;
689   }
690
691   /* otherwise, return false. */
692   return 0;
693 }
694
695 int tr_find_config_files (struct dirent ***cfg_files) {
696   int n = 0, i = 0;
697   
698   n = scandir(".", cfg_files, &is_cfg_file, 0);
699
700   if (n < 0) {
701     perror("scandir");
702     fprintf(stderr, "tr_find_config: scandir error.\n");
703     return 0;
704   }
705
706   if (n == 0) {
707     fprintf (stderr, "tr_find_config: No config files found.\n");
708     return 0;
709   }
710
711   i = n;
712   while(i--) {
713     fprintf(stderr, "tr_find_config: Config file found (%s).\n", (*cfg_files)[i]->d_name);
714   }
715     
716   return n;
717 }