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