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