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