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