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