Don't overwrite minorStatus before printing error.
[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   aaa->hostname = tr_new_name((char *)(json_string_value(jaddr)));
424
425   return aaa;
426 }
427
428
429 static TR_AAA_SERVER *tr_cfg_parse_aaa_servers (TR_INSTANCE *tr, json_t *jaaas, TR_CFG_RC *rc) 
430 {
431   TR_AAA_SERVER *aaa = NULL;
432   TR_AAA_SERVER *temp_aaa = NULL;
433   int i = 0;
434
435   for (i = 0; i < json_array_size(jaaas); i++) {
436     if (NULL == (temp_aaa = tr_cfg_parse_one_aaa_server(tr, json_array_get(jaaas, i), rc))) {
437       return NULL;
438     }
439     /* TBD -- IPv6 addresses */
440     //    fprintf(stderr, "tr_cfg_parse_aaa_servers: Configuring AAA Server: ip_addr = %s.\n", inet_ntoa(temp_aaa->aaa_server_addr));
441     temp_aaa->next = aaa;
442     aaa = temp_aaa;
443   }
444   return aaa;
445 }
446
447 static TR_APC *tr_cfg_parse_apcs (TR_INSTANCE *tr, json_t *japcs, TR_CFG_RC *rc)
448 {
449   TR_APC *apc;
450
451   *rc = TR_CFG_SUCCESS;         /* presume success */
452
453   if ((!japcs) || (!rc)) {
454     fprintf(stderr, "tr_cfg_parse_apcs: Bad parameters.\n");
455     if (rc) 
456       *rc = TR_CFG_BAD_PARAMS;
457     return NULL;
458   }
459
460   if (NULL == (apc = malloc(sizeof(TR_APC)))) {
461     fprintf (stderr, "tr_cfg_parse_apcs: Out of memory.\n");
462     *rc = TR_CFG_NOMEM;
463     return NULL;
464   }
465
466   memset(apc, 0, sizeof(TR_APC));
467
468   /* TBD, deal with more than one APC.  In the meantime, though...                */
469   /* Only parse the first APC, because we only know how to deal with one, anyway. */
470   if (0 == json_array_size(japcs))
471     return NULL;
472
473   if (NULL == (apc->id = tr_new_name((char *)json_string_value(json_array_get(japcs, 0))))) {
474     fprintf(stderr, "tr_cfg_parse_apcs: No memory for APC name.\n");
475     *rc = TR_CFG_NOMEM;
476     return NULL;
477   }
478
479   return apc;
480 }
481
482 static TR_IDP_REALM *tr_cfg_parse_one_idp_realm (TR_INSTANCE *tr, json_t *jidp, TR_CFG_RC *rc) {
483   TR_IDP_REALM *idp = NULL;
484   json_t *jrid = NULL;
485   json_t *jscfg = NULL;
486   json_t *jsrvrs = NULL;
487   json_t *japcs = NULL;
488
489   if ((!jidp) || (!rc)) {
490     fprintf(stderr, "tr_cfg_parse_one_idp_realm: Bad parameters.\n");
491     if (rc)
492       *rc = TR_CFG_BAD_PARAMS;
493     return NULL;
494   }
495
496   if (NULL == (idp = malloc(sizeof(TR_IDP_REALM)))) {
497     fprintf(stderr, "tr_config_parse_one_idp_realm: Out of memory.\n");
498     *rc = TR_CFG_NOMEM;
499     return NULL;
500   }
501
502   memset(idp, 0, sizeof(TR_IDP_REALM));
503
504   if ((NULL == (jrid = json_object_get(jidp, "realm_id"))) ||
505       (!json_is_string(jrid)) ||
506       (NULL == (jscfg = json_object_get(jidp, "shared_config"))) ||
507       (!json_is_string(jscfg)) ||
508       (NULL == (jsrvrs = json_object_get(jidp, "aaa_servers"))) ||
509       (!json_is_array(jsrvrs))) {
510     fprintf(stderr, "tr_cfg_parse_one_idp_realm: Error parsing IDP realm configuration.\n");
511     free(idp);
512     *rc = TR_CFG_NOPARSE;
513     return NULL;
514   }
515
516   if (0 == strcmp(json_string_value(jscfg), "no")) {
517     idp->shared_config = 0;
518   } else {
519     idp->shared_config = 1;
520   }
521
522   if (NULL == (idp->realm_id = tr_new_name((char *)json_string_value(jrid)))) {
523     free(idp);
524     fprintf(stderr, "tr_cfg_parse_one_idp_realm: No memory for realm id.\n");
525     *rc = TR_CFG_NOMEM;
526     return NULL;
527   }
528
529   if (NULL == (idp->aaa_servers = tr_cfg_parse_aaa_servers(tr, jsrvrs, rc))) {
530     fprintf(stderr, "tr_cfg_parse_one_idp_realm: Can't parse AAA servers for realm %s.\n", idp->realm_id->buf);
531     tr_free_name(idp->realm_id);
532     free(idp);
533     return NULL;
534   }
535
536   if ((NULL != (japcs = json_object_get(jidp, "apcs"))) &&
537       (json_is_array(japcs))) {
538     if (NULL == (idp->apcs = tr_cfg_parse_apcs(tr, japcs, rc))) {
539       fprintf(stderr, "tr_cfg_parse_one_idp_realm: Can't parse APCs for realm %s .\n", idp->realm_id->buf);
540       tr_free_name(idp->realm_id);
541       /* TBD -- free aaa_servers */;
542       free(idp);
543       return NULL;
544     }
545   } 
546   return idp;
547 }
548
549 static TR_CFG_RC tr_cfg_parse_idp_realms (TR_INSTANCE *tr, json_t *jcfg) 
550 {
551   json_t *jidps = NULL;
552   TR_CFG_RC rc = TR_CFG_SUCCESS;
553   TR_IDP_REALM *idp = NULL;
554   int i = 0;
555
556   if ((!tr) || (!tr->new_cfg) || (!jcfg))
557     return TR_CFG_BAD_PARAMS;
558
559   if ((NULL == (jidps = json_object_get(jcfg, "idp_realms"))) ||
560       (!json_is_array(jidps))) {
561     return TR_CFG_NOPARSE;
562   }
563
564   for (i = 0; i < json_array_size(jidps); i++) {
565     if (NULL == (idp = tr_cfg_parse_one_idp_realm(tr, 
566                                                   json_array_get(jidps, i), 
567                                                   &rc))) {
568        return rc;
569     }
570     fprintf(stderr, "tr_cfg_parse_idp_realms: IDP realm configured: %s.\n", idp->realm_id->buf);
571     idp->next = tr->new_cfg->idp_realms;
572     tr->new_cfg->idp_realms = idp;
573   }
574   return rc;
575 }
576
577 static TR_IDP_REALM *tr_cfg_parse_comm_idps (TR_INSTANCE *tr, json_t *jidps, TR_CFG_RC *rc)
578 {
579   TR_IDP_REALM *idp = NULL;
580   TR_IDP_REALM *temp_idp = NULL;
581   int i = 0;
582
583   if ((!tr) ||
584       (!jidps) ||
585       (!json_is_array(jidps))) {
586     if (rc)
587       *rc = TR_CFG_BAD_PARAMS;
588     return NULL;
589   }
590
591   for (i = 0; i < json_array_size(jidps); i++) {
592     if (NULL == (temp_idp = (tr_cfg_find_idp(tr->new_cfg, 
593                                              tr_new_name((char *)json_string_value(json_array_get(jidps, i))), 
594                                              rc)))) {
595       fprintf(stderr, "tr_cfg_parse_comm_idps: Unknown IDP %s.\n", 
596               (char *)json_string_value(json_array_get(jidps, i)));
597       return NULL;
598     }
599
600     temp_idp->comm_next = idp;
601     idp = temp_idp;
602   }
603
604   return idp;
605 }
606
607 static TR_RP_REALM *tr_cfg_parse_comm_rps (TR_INSTANCE *tr, json_t *jrps, TR_CFG_RC *rc)
608 {
609   TR_RP_REALM *rp = NULL;
610   TR_RP_REALM *temp_rp = NULL;
611   int i = 0;
612
613   if ((!tr) ||
614       (!jrps) ||
615       (!json_is_array(jrps))) {
616     if (rc)
617       *rc = TR_CFG_BAD_PARAMS;
618     return NULL;
619   }
620
621   for (i = (json_array_size(jrps)-1); i >= 0; i--) {
622     if (NULL == (temp_rp = malloc(sizeof(TR_RP_REALM)))) {
623       fprintf(stderr, "tr_cfg_parse_comm_rps: Can't allocate memory for RP Realm.\n");
624       if (rc)
625         *rc = TR_CFG_NOMEM;
626       return NULL;
627     }
628     memset (temp_rp, 0, sizeof(TR_RP_REALM));
629
630     if (NULL == (temp_rp->realm_name = tr_new_name((char *)json_string_value(json_array_get(jrps, i))))) {
631       fprintf(stderr, "tr_cfg_parse_comm_rps: No memory for RP Realm Name.\n");
632       if (rc)
633         *rc = TR_CFG_NOMEM;
634       return NULL;
635     }
636
637     temp_rp->next = rp;
638     rp = temp_rp;
639   }
640
641   return rp;
642 }
643
644 static TR_COMM *tr_cfg_parse_one_comm (TR_INSTANCE *tr, json_t *jcomm, TR_CFG_RC *rc) {
645   TR_COMM *comm = NULL;
646   json_t *jid = NULL;
647   json_t *jtype = NULL;
648   json_t *japcs = NULL;
649   json_t *jidps = NULL;
650   json_t *jrps = NULL;
651
652   if ((!jcomm) || (!rc)) {
653     fprintf(stderr, "tr_cfg_parse_one_comm: Bad parameters.\n");
654     if (rc)
655       *rc = TR_CFG_BAD_PARAMS;
656     return NULL;
657   }
658
659   if (NULL == (comm = malloc(sizeof(TR_COMM)))) {
660     fprintf(stderr, "tr_config_parse_one_comm: Out of memory.\n");
661     *rc = TR_CFG_NOMEM;
662     return NULL;
663   }
664
665   memset(comm, 0, sizeof(TR_COMM));
666
667   if ((NULL == (jid = json_object_get(jcomm, "community_id"))) ||
668       (!json_is_string(jid)) ||
669       (NULL == (jtype = json_object_get(jcomm, "type"))) ||
670       (!json_is_string(jtype)) ||
671       (NULL == (japcs = json_object_get(jcomm, "apcs"))) ||
672       (!json_is_array(japcs)) ||
673       (NULL == (jidps = json_object_get(jcomm, "idp_realms"))) ||
674       (!json_is_array(jidps)) ||
675       (NULL == (jrps = json_object_get(jcomm, "rp_realms"))) ||
676       (!json_is_array(jrps))) {
677     fprintf(stderr, "tr_cfg_parse_one_comm: Error parsing Communities configuration.\n");
678     free(comm);
679     *rc = TR_CFG_NOPARSE;
680     return NULL;
681   }
682
683   if (NULL == (comm->id = tr_new_name((char *)json_string_value(jid)))) {
684     free(comm);
685     fprintf(stderr, "tr_cfg_parse_one_comm: No memory for community id.\n");
686     *rc = TR_CFG_NOMEM;
687     return NULL;
688   }
689
690   if (0 == strcmp(json_string_value(jtype), "apc")) {
691     comm->type = TR_COMM_APC;
692   } else if (0 == strcmp(json_string_value(jtype), "coi")) {
693     comm->type = TR_COMM_COI;
694     if (NULL == (comm->apcs = tr_cfg_parse_apcs(tr, japcs, rc))) {
695       fprintf(stderr, "tr_cfg_parse_one_comm: Can't parse APCs for COI %s.\n", comm->id->buf);
696       tr_free_name(comm->id);
697       free(comm);
698       return NULL;
699     }
700   } else {
701     fprintf(stderr, "tr_cfg_parse_one_comm: Invalid community type, comm = %s, type = %s\n", comm->id->buf, json_string_value(jtype));
702     tr_free_name(comm->id);
703     free(comm);
704     *rc = TR_CFG_NOPARSE;
705     return NULL;
706   }
707
708   comm->idp_realms = tr_cfg_parse_comm_idps(tr, jidps, rc);
709   if (TR_CFG_SUCCESS != *rc) {
710     fprintf(stderr, "tr_cfg_parse_one_comm: Can't parse IDP realms for comm %s.\n", comm->id->buf);
711     tr_free_name(comm->id);
712     free(comm);
713     return NULL;
714   }
715
716   comm->rp_realms = tr_cfg_parse_comm_rps(tr, jrps, rc);
717   if (TR_CFG_SUCCESS != *rc) {
718     fprintf(stderr, "tr_cfg_parse_comm: Can't parse RP realms for comm %s .\n", comm->id->buf);
719     tr_free_name(comm->id);
720     /* TBD -- free idps? */;
721     free(comm);
722     return NULL;
723   }
724
725   return comm;
726 }
727
728 static TR_CFG_RC tr_cfg_parse_comms (TR_INSTANCE *tr, json_t *jcfg) 
729 {
730   json_t *jcomms = NULL;
731   TR_CFG_RC rc = TR_CFG_SUCCESS;
732   TR_COMM *comm = NULL;
733   int i = 0;
734
735   if ((!tr) || (!tr->new_cfg) || (!jcfg)) {
736     fprintf(stderr, "tr_config_parse_comms: Bad Parameters.\n");
737     return TR_CFG_BAD_PARAMS;
738   }
739
740   if (NULL == (comm = malloc(sizeof(TR_COMM)))) {
741     fprintf(stderr, "tr_cfg_parse_comms: Out of Memory\n");
742     return TR_CFG_NOMEM;
743   }
744   memset (comm, 0, sizeof(TR_COMM));
745
746   if ((NULL == (jcomms = json_object_get(jcfg, "communities"))) ||
747       (!json_is_array(jcomms))) {
748     return TR_CFG_NOPARSE;
749   }
750
751   for (i = 0; i < json_array_size(jcomms); i++) {
752     if (NULL == (comm = tr_cfg_parse_one_comm(tr, 
753                                               json_array_get(jcomms, i), 
754                                               &rc))) {
755        return rc;
756     }
757     fprintf(stderr, "tr_cfg_parse_comms: Community configured: %s.\n", comm->id->buf);
758     comm->next = tr->new_cfg->comms;
759     tr->new_cfg->comms = comm;
760   }
761   return rc;
762 }
763
764 TR_CFG_RC tr_parse_config (TR_INSTANCE *tr, json_t *jcfg) {
765
766   /* If there is a partial/abandoned config lying around, free it */
767   if (tr->new_cfg) {
768     tr_cfg_free(tr->new_cfg);
769   }
770   
771   if (NULL == (tr->new_cfg = malloc(sizeof(TR_CFG))))
772     return TR_CFG_NOMEM;
773
774   memset(tr->new_cfg, 0, sizeof(TR_CFG));
775
776   if ((TR_CFG_SUCCESS != tr_cfg_parse_internal(tr, jcfg)) ||
777       (TR_CFG_SUCCESS != tr_cfg_parse_rp_clients(tr, jcfg)) ||
778       (TR_CFG_SUCCESS != tr_cfg_parse_idp_realms(tr, jcfg)) ||
779       (TR_CFG_SUCCESS != tr_cfg_parse_comms(tr, jcfg))) {
780     tr_cfg_free(tr->new_cfg);
781     return TR_CFG_ERROR;
782   }
783   return TR_CFG_SUCCESS;
784 }
785
786 TR_IDP_REALM *tr_cfg_find_idp (TR_CFG *tr_cfg, TR_NAME *idp_id, TR_CFG_RC *rc)
787 {
788
789   TR_IDP_REALM *cfg_idp;
790
791   if ((!tr_cfg) || (!idp_id)) {
792     if (rc)
793       *rc = TR_CFG_BAD_PARAMS;
794     return NULL;
795   }
796
797   for (cfg_idp = tr_cfg->idp_realms; NULL != cfg_idp; cfg_idp = cfg_idp->next) {
798     if (!tr_name_cmp (idp_id, cfg_idp->realm_id)) {
799       fprintf(stderr, "tr_cfg_find_idp: Found %s.\n", idp_id->buf);
800       return cfg_idp;
801     }
802   }
803   /* if we didn't find one, return NULL */ 
804   return NULL;
805 }
806
807 TR_RP_CLIENT *tr_cfg_find_rp (TR_CFG *tr_cfg, TR_NAME *rp_gss, TR_CFG_RC *rc)
808 {
809   TR_RP_CLIENT *cfg_rp;
810   int i;
811
812   if ((!tr_cfg) || (!rp_gss)) {
813     if (rc)
814       *rc = TR_CFG_BAD_PARAMS;
815     return NULL;
816   }
817
818   for (cfg_rp = tr_cfg->rp_clients; NULL != cfg_rp; cfg_rp = cfg_rp->next) {
819     for (i = 0; i < TR_MAX_GSS_NAMES; i++) {
820       if (!tr_name_cmp (rp_gss, cfg_rp->gss_names[i])) {
821         fprintf(stderr, "tr_cfg_find_rp: Found %s.\n", rp_gss->buf);
822         return cfg_rp;
823       }
824     }
825   }
826   /* if we didn't find one, return NULL */ 
827   return NULL;
828 }
829
830 json_t *tr_read_config (int n, struct dirent **cfg_files) {
831   json_t *jcfg = NULL;
832   json_t *temp = NULL;
833   json_error_t err;
834
835   if (!cfg_files)
836     return NULL;
837
838   while (n--) {
839     fprintf(stderr, "tr_read_config: Parsing %s.\n", cfg_files[n]->d_name);
840     if (NULL == (temp = json_load_file(cfg_files[n]->d_name, JSON_DISABLE_EOF_CHECK, &err))) {
841       fprintf (stderr, "tr_read_config: Error parsing config file %s.\n", cfg_files[n]->d_name);
842       return NULL;
843     }
844
845     /* TBD -- instead of using json_object_update, iterate through files for non-overlap config? */
846     if (!jcfg) {
847       jcfg = temp;
848     }else {
849       if (-1 == json_object_update(jcfg, temp)) {
850         fprintf(stderr, "tr_read_config: Error merging config information.\n");
851         return NULL;
852       }
853     }
854   }
855
856   fprintf(stderr, "tr_read_config: Merged configuration complete:\n%s\n", json_dumps(jcfg, 0));
857
858   return jcfg;
859 }
860
861 static int is_cfg_file(const struct dirent *dent) {
862   int n;
863
864   /* if the last four letters of the filename are .cfg, return true. */
865   if ((4 <= (n = strlen(dent->d_name))) &&
866       (0 == strcmp(&(dent->d_name[n-4]), ".cfg"))) {
867     return 1;
868   }
869
870   /* otherwise, return false. */
871   return 0;
872 }
873
874 int tr_find_config_files (struct dirent ***cfg_files) {
875   int n = 0, i = 0;
876   
877   n = scandir(".", cfg_files, &is_cfg_file, 0);
878
879   if (n < 0) {
880     perror("scandir");
881     fprintf(stderr, "tr_find_config: scandir error.\n");
882     return 0;
883   }
884
885   if (n == 0) {
886     fprintf (stderr, "tr_find_config: No config files found.\n");
887     return 0;
888   }
889
890   i = n;
891   while(i--) {
892     fprintf(stderr, "tr_find_config: Config file found (%s).\n", (*cfg_files)[i]->d_name);
893   }
894     
895   return n;
896 }