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