Configuration of key expiration for APC
[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     *rc = TR_CFG_NOPARSE;
371     return NULL;
372   }
373     
374   for (i = 0; i < json_array_size(jgns); i++) {
375     if (NULL == (rp->gss_names[i] = tr_new_name ((char *)json_string_value(json_array_get(jgns, i))))) {
376       fprintf(stderr, "tr_cfg_parse_one_rp_client: No memory for GSS Name.\n");
377       *rc = TR_CFG_NOMEM;
378       return NULL;
379     }
380   }
381   
382   return rp;
383 }
384
385 static TR_CFG_RC tr_cfg_parse_rp_clients (TR_CFG *trc, json_t *jcfg) {
386   json_t *jrps = NULL;
387   TR_RP_CLIENT *rp = NULL;
388   TR_CFG_RC rc = TR_CFG_SUCCESS;
389   int i = 0;
390
391   if ((!trc) || (!jcfg))
392     return TR_CFG_BAD_PARAMS;
393
394   if (NULL != (jrps = json_object_get(jcfg, "rp_clients"))) {
395
396     if (!json_is_array(jrps)) {
397       return TR_CFG_NOPARSE;
398     }
399
400     for (i = 0; i < json_array_size(jrps); i++) {
401       if (NULL == (rp = tr_cfg_parse_one_rp_client(trc, 
402                                                    json_array_get(jrps, i), 
403                                                    &rc))) {
404         return rc;
405       }
406       fprintf(stderr, "tr_cfg_parse_rp_clients: RP client configured -- first gss: %s\n", rp->gss_names[0]->buf);
407       rp->next = trc->rp_clients;
408       trc->rp_clients = rp;
409     }
410   }
411   return rc;
412 }
413
414 static TR_AAA_SERVER *tr_cfg_parse_one_aaa_server (TR_CFG *trc, json_t *jaddr, TR_CFG_RC *rc) {
415   TR_AAA_SERVER *aaa = NULL;
416
417   if ((!trc) || (!jaddr) || (!json_is_string(jaddr))) {
418     fprintf(stderr, "tr_cfg_parse_one_aaa_server: Bad parameters.\n");
419     *rc = TR_CFG_BAD_PARAMS;
420     return NULL;
421   }
422
423   if (NULL == (aaa = talloc(trc, TR_AAA_SERVER))) {
424     fprintf(stderr, "tr_cfg_parse_one_aaa_server: Out of memory.\n");
425     *rc = TR_CFG_NOMEM;
426     return NULL;
427   }
428
429   memset(aaa, 0, sizeof(TR_AAA_SERVER));
430
431   aaa->hostname = tr_new_name((char *)(json_string_value(jaddr)));
432
433   return aaa;
434 }
435
436 static TR_AAA_SERVER *tr_cfg_parse_aaa_servers (TR_CFG *trc, json_t *jaaas, TR_CFG_RC *rc) 
437 {
438   TR_AAA_SERVER *aaa = NULL;
439   TR_AAA_SERVER *temp_aaa = NULL;
440   int i = 0;
441
442   for (i = 0; i < json_array_size(jaaas); i++) {
443     if (NULL == (temp_aaa = tr_cfg_parse_one_aaa_server(trc, json_array_get(jaaas, i), rc))) {
444       return NULL;
445     }
446     /* TBD -- IPv6 addresses */
447     //    fprintf(stderr, "tr_cfg_parse_aaa_servers: Configuring AAA Server: ip_addr = %s.\n", inet_ntoa(temp_aaa->aaa_server_addr));
448     temp_aaa->next = aaa;
449     aaa = temp_aaa;
450   }
451   return aaa;
452 }
453
454 static TR_APC *tr_cfg_parse_apcs (TR_CFG *trc, json_t *japcs, TR_CFG_RC *rc)
455 {
456   TR_APC *apc;
457
458   *rc = TR_CFG_SUCCESS;         /* presume success */
459
460   if ((!trc) || (!japcs) || (!rc)) {
461     fprintf(stderr, "tr_cfg_parse_apcs: Bad parameters.\n");
462     if (rc) 
463       *rc = TR_CFG_BAD_PARAMS;
464     return NULL;
465   }
466
467   if (NULL == (apc = talloc(trc, TR_APC))) {
468     fprintf (stderr, "tr_cfg_parse_apcs: Out of memory.\n");
469     *rc = TR_CFG_NOMEM;
470     return NULL;
471   }
472
473   memset(apc, 0, sizeof(TR_APC));
474
475   /* TBD, deal with more than one APC.  In the meantime, though...                */
476   /* Only parse the first APC, because we only know how to deal with one, anyway. */
477   if (0 == json_array_size(japcs))
478     return NULL;
479
480   if (NULL == (apc->id = tr_new_name((char *)json_string_value(json_array_get(japcs, 0))))) {
481     fprintf(stderr, "tr_cfg_parse_apcs: No memory for APC name.\n");
482     *rc = TR_CFG_NOMEM;
483     return NULL;
484   }
485
486   return apc;
487 }
488
489 static TR_IDP_REALM *tr_cfg_parse_one_idp_realm (TR_CFG *trc, json_t *jidp, TR_CFG_RC *rc) {
490   TR_IDP_REALM *idp = NULL;
491   json_t *jrid = NULL;
492   json_t *jscfg = NULL;
493   json_t *jsrvrs = NULL;
494   json_t *japcs = NULL;
495
496   if ((!trc) || (!jidp) || (!rc)) {
497     fprintf(stderr, "tr_cfg_parse_one_idp_realm: Bad parameters.\n");
498     if (rc)
499       *rc = TR_CFG_BAD_PARAMS;
500     return NULL;
501   }
502
503   if (NULL == (idp = talloc(trc, TR_IDP_REALM))) {
504     fprintf(stderr, "tr_cfg_parse_one_idp_realm: Out of memory.\n");
505     *rc = TR_CFG_NOMEM;
506     return NULL;
507   }
508
509   memset(idp, 0, sizeof(TR_IDP_REALM));
510
511   if ((NULL == (jrid = json_object_get(jidp, "realm_id"))) ||
512       (!json_is_string(jrid)) ||
513       (NULL == (jscfg = json_object_get(jidp, "shared_config"))) ||
514       (!json_is_string(jscfg)) ||
515       (NULL == (jsrvrs = json_object_get(jidp, "aaa_servers"))) ||
516       (!json_is_array(jsrvrs))) {
517     fprintf(stderr, "tr_cfg_parse_one_idp_realm: Error parsing IDP realm configuration.\n");
518     *rc = TR_CFG_NOPARSE;
519     return NULL;
520   }
521
522   if (0 == strcmp(json_string_value(jscfg), "no")) {
523     idp->shared_config = 0;
524   } else {
525     idp->shared_config = 1;
526   }
527
528   if (NULL == (idp->realm_id = tr_new_name((char *)json_string_value(jrid)))) {
529     fprintf(stderr, "tr_cfg_parse_one_idp_realm: No memory for realm id.\n");
530     *rc = TR_CFG_NOMEM;
531     return NULL;
532   }
533
534   if (NULL == (idp->aaa_servers = tr_cfg_parse_aaa_servers(trc, jsrvrs, rc))) {
535     fprintf(stderr, "tr_cfg_parse_one_idp_realm: Can't parse AAA servers for realm %s.\n", idp->realm_id->buf);
536     tr_free_name(idp->realm_id);
537     return NULL;
538   }
539
540   if ((NULL != (japcs = json_object_get(jidp, "apcs"))) &&
541       (json_is_array(japcs))) {
542     if (NULL == (idp->apcs = tr_cfg_parse_apcs(trc, japcs, rc))) {
543       fprintf(stderr, "tr_cfg_parse_one_idp_realm: Can't parse APCs for realm %s .\n", idp->realm_id->buf);
544       tr_free_name(idp->realm_id);
545       /* TBD -- free aaa_servers */;
546       return NULL;
547     }
548   } 
549   return idp;
550 }
551
552 static TR_CFG_RC tr_cfg_parse_default_servers (TR_CFG *trc, json_t *jcfg) 
553 {
554   json_t *jdss = NULL;
555   TR_CFG_RC rc = TR_CFG_SUCCESS;
556   TR_AAA_SERVER *ds = NULL;
557   int i = 0;
558
559   if ((!trc) || (!jcfg))
560     return TR_CFG_BAD_PARAMS;
561
562   /* If there are default servers, store them */
563   if ((NULL != (jdss = json_object_get(jcfg, "default_servers"))) &&
564       (json_is_array(jdss)) &&
565       (0 < json_array_size(jdss))) {
566
567     for (i = 0; i < json_array_size(jdss); i++) {
568       if (NULL == (ds = tr_cfg_parse_one_aaa_server(trc, 
569                                                   json_array_get(jdss, i), 
570                                                   &rc))) {
571         return rc;
572       }
573       fprintf(stderr, "tr_cfg_parse_default_servers: Default server configured: %s\n", ds->hostname->buf);
574       ds->next = trc->default_servers;
575       trc->default_servers = ds;
576     }
577   } 
578
579   return rc;
580 }
581
582 static TR_CFG_RC tr_cfg_parse_idp_realms (TR_CFG *trc, json_t *jcfg) 
583 {
584   json_t *jidps = NULL;
585   TR_CFG_RC rc = TR_CFG_SUCCESS;
586   TR_IDP_REALM *idp = NULL;
587   int i = 0;
588
589   if ((!trc) || (!jcfg))
590     return TR_CFG_BAD_PARAMS;
591
592   /* If there are any IDP Realms, parse them */
593   if ((NULL != (jidps = json_object_get(jcfg, "idp_realms"))) &&
594       (json_is_array(jidps))) {
595     for (i = 0; i < json_array_size(jidps); i++) {
596       if (NULL == (idp = tr_cfg_parse_one_idp_realm(trc,
597                                                     json_array_get(jidps, i), 
598                                                     &rc))) {
599         return rc;
600       }
601       fprintf(stderr, "tr_cfg_parse_idp_realms: IDP realm configured: %s.\n", idp->realm_id->buf);
602       idp->next = trc->idp_realms;
603       trc->idp_realms = idp;
604     }
605   }
606
607   return rc;
608 }
609
610 static TR_IDP_REALM *tr_cfg_parse_comm_idps (TR_CFG *trc, json_t *jidps, TR_CFG_RC *rc)
611 {
612   TR_IDP_REALM *idp = NULL;
613   TR_IDP_REALM *temp_idp = NULL;
614   int i = 0;
615
616   if ((!trc) ||
617       (!jidps) ||
618       (!json_is_array(jidps))) {
619     if (rc)
620       *rc = TR_CFG_BAD_PARAMS;
621     return NULL;
622   }
623
624   for (i = 0; i < json_array_size(jidps); i++) {
625     if (NULL == (temp_idp = (tr_cfg_find_idp(trc, 
626                                              tr_new_name((char *)json_string_value(json_array_get(jidps, i))), 
627                                              rc)))) {
628       fprintf(stderr, "tr_cfg_parse_comm_idps: Unknown IDP %s.\n", 
629               (char *)json_string_value(json_array_get(jidps, i)));
630       return NULL;
631     }
632
633     temp_idp->comm_next = idp;
634     idp = temp_idp;
635   }
636
637   return idp;
638 }
639
640 static TR_RP_REALM *tr_cfg_parse_comm_rps (TR_CFG *trc, json_t *jrps, TR_CFG_RC *rc)
641 {
642   TR_RP_REALM *rp = NULL;
643   TR_RP_REALM *temp_rp = NULL;
644   int i = 0;
645
646   if ((!trc) ||
647       (!jrps) ||
648       (!json_is_array(jrps))) {
649     if (rc)
650       *rc = TR_CFG_BAD_PARAMS;
651     return NULL;
652   }
653
654   for (i = (json_array_size(jrps)-1); i >= 0; i--) {
655     if (NULL == (temp_rp = talloc(trc, TR_RP_REALM))) {
656       fprintf(stderr, "tr_cfg_parse_comm_rps: Can't allocate memory for RP Realm.\n");
657       if (rc)
658         *rc = TR_CFG_NOMEM;
659       return NULL;
660     }
661     memset (temp_rp, 0, sizeof(TR_RP_REALM));
662
663     if (NULL == (temp_rp->realm_name = tr_new_name((char *)json_string_value(json_array_get(jrps, i))))) {
664       fprintf(stderr, "tr_cfg_parse_comm_rps: No memory for RP Realm Name.\n");
665       if (rc)
666         *rc = TR_CFG_NOMEM;
667       return NULL;
668     }
669
670     temp_rp->next = rp;
671     rp = temp_rp;
672   }
673
674   return rp;
675 }
676
677 static TR_COMM *tr_cfg_parse_one_comm (TR_CFG *trc, json_t *jcomm, TR_CFG_RC *rc) {
678   TR_COMM *comm = NULL;
679   json_t *jid = NULL;
680   json_t *jtype = NULL;
681   json_t *japcs = NULL;
682   json_t *jidps = NULL;
683   json_t *jrps = NULL;
684
685   if ((!trc) || (!jcomm) || (!rc)) {
686     fprintf(stderr, "tr_cfg_parse_one_comm: Bad parameters.\n");
687     if (rc)
688       *rc = TR_CFG_BAD_PARAMS;
689     return NULL;
690   }
691
692   if (NULL == (comm = talloc_zero(trc, TR_COMM))) {
693     fprintf(stderr, "tr_cfg_parse_one_comm: Out of memory.\n");
694     *rc = TR_CFG_NOMEM;
695     return NULL;
696   }
697
698
699   if ((NULL == (jid = json_object_get(jcomm, "community_id"))) ||
700       (!json_is_string(jid)) ||
701       (NULL == (jtype = json_object_get(jcomm, "type"))) ||
702       (!json_is_string(jtype)) ||
703       (NULL == (japcs = json_object_get(jcomm, "apcs"))) ||
704       (!json_is_array(japcs)) ||
705       (NULL == (jidps = json_object_get(jcomm, "idp_realms"))) ||
706       (!json_is_array(jidps)) ||
707       (NULL == (jrps = json_object_get(jcomm, "rp_realms"))) ||
708       (!json_is_array(jrps))) {
709     fprintf(stderr, "tr_cfg_parse_one_comm: Error parsing Communities configuration.\n");
710     *rc = TR_CFG_NOPARSE;
711     return NULL;
712   }
713
714   if (NULL == (comm->id = tr_new_name((char *)json_string_value(jid)))) {
715     fprintf(stderr, "tr_cfg_parse_one_comm: No memory for community id.\n");
716     *rc = TR_CFG_NOMEM;
717     return NULL;
718   }
719
720   if (0 == strcmp(json_string_value(jtype), "apc")) {
721     comm->type = TR_COMM_APC;
722   } else if (0 == strcmp(json_string_value(jtype), "coi")) {
723     comm->type = TR_COMM_COI;
724     if (NULL == (comm->apcs = tr_cfg_parse_apcs(trc, japcs, rc))) {
725       fprintf(stderr, "tr_cfg_parse_one_comm: Can't parse APCs for COI %s.\n", comm->id->buf);
726       tr_free_name(comm->id);
727       return NULL;
728     }
729   } else {
730     fprintf(stderr, "tr_cfg_parse_one_comm: Invalid community type, comm = %s, type = %s\n", comm->id->buf, json_string_value(jtype));
731     tr_free_name(comm->id);
732     *rc = TR_CFG_NOPARSE;
733     return NULL;
734   }
735
736   comm->idp_realms = tr_cfg_parse_comm_idps(trc, jidps, rc);
737   if (TR_CFG_SUCCESS != *rc) {
738     fprintf(stderr, "tr_cfg_parse_one_comm: Can't parse IDP realms for comm %s.\n", comm->id->buf);
739     tr_free_name(comm->id);
740     return NULL;
741   }
742
743   comm->rp_realms = tr_cfg_parse_comm_rps(trc, jrps, rc);
744   if (TR_CFG_SUCCESS != *rc) {
745     fprintf(stderr, "tr_cfg_parse_comm: Can't parse RP realms for comm %s .\n", comm->id->buf);
746     tr_free_name(comm->id);
747     return NULL;
748   }
749
750   if (TR_COMM_APC == comm->type) {
751     json_t *jexpire  = json_object_get(jcomm, "expiration_interval");
752     comm->expiration_interval = 43200; /*30 days*/
753     if (jexpire) {
754         if (!json_is_integer(jexpire)) {
755           fprintf(stderr, "tr_parse_comm: expirae_interval is not an integer\n");
756           return NULL;
757         }
758         comm->expiration_interval = json_integer_value(jexpire);
759         if (comm->expiration_interval <= 10)
760           comm->expiration_interval = 11; /* Freeradius waits 10 minutes between successful TR queries*/
761         if (comm->expiration_interval > 129600) /* 90 days*/
762         comm->expiration_interval = 129600;
763     }
764   }
765   
766   return comm;
767 }
768
769 static TR_CFG_RC tr_cfg_parse_comms (TR_CFG *trc, json_t *jcfg) 
770 {
771   json_t *jcomms = NULL;
772   TR_CFG_RC rc = TR_CFG_SUCCESS;
773   TR_COMM *comm = NULL;
774   int i = 0;
775
776   if ((!trc) || (!jcfg)) {
777     fprintf(stderr, "tr_cfg_parse_comms: Bad Parameters.\n");
778     return TR_CFG_BAD_PARAMS;
779   }
780
781   if (NULL != (jcomms = json_object_get(jcfg, "communities"))) {
782     if (!json_is_array(jcomms)) {
783       return TR_CFG_NOPARSE;
784     }
785
786     for (i = 0; i < json_array_size(jcomms); i++) {
787       if (NULL == (comm = tr_cfg_parse_one_comm(trc, 
788                                                 json_array_get(jcomms, i), 
789                                                 &rc))) {
790         return rc;
791       }
792       fprintf(stderr, "tr_cfg_parse_comms: Community configured: %s.\n", comm->id->buf);
793       comm->next = trc->comms;
794       trc->comms = comm;
795     }
796   }
797   return rc;
798 }
799
800 TR_CFG_RC tr_cfg_validate (TR_CFG *trc) {
801   TR_CFG_RC rc = TR_CFG_SUCCESS;
802
803   if (!trc)
804     return TR_CFG_BAD_PARAMS;
805
806   if ((NULL == trc->internal)||
807       (NULL == trc->internal->hostname)) {
808     fprintf(stderr, "tr_cfg_validate: Error: No internal configuration, or no hostname.\n");
809     rc = TR_CFG_ERROR;
810   }
811
812   if (NULL == trc->rp_clients) {
813     fprintf(stderr, "tr_cfg_validate: Error: No RP Clients configured\n");
814     rc = TR_CFG_ERROR;
815   }
816
817   if (NULL == trc->comms) {
818     fprintf(stderr, "tr_cfg_validate: Error: No Communities configured\n");
819     rc = TR_CFG_ERROR;
820   }
821
822   if ((NULL == trc->default_servers) && (NULL == trc->idp_realms)) {
823     fprintf(stderr, "tr_cfg_validate: Error: No default servers or IDPs configured.\n");
824     rc = TR_CFG_ERROR;
825   }
826   
827   return rc;
828 }
829
830 TR_CFG_RC tr_parse_config (TR_INSTANCE *tr, int n, struct dirent **cfg_files) {
831   json_t *jcfg;
832   json_error_t rc;
833
834   if ((!tr) || (!cfg_files))
835     return TR_CFG_BAD_PARAMS;
836
837   /* If there is a partial/abandoned config lying around, free it */
838   if (tr->new_cfg) 
839     tr_cfg_free(tr->new_cfg);
840   
841   if (NULL == (tr->new_cfg = talloc(NULL, TR_CFG)))
842     return TR_CFG_NOMEM;
843
844   memset(tr->new_cfg, 0, sizeof(TR_CFG));
845
846   /* Parse configuration information from each config file */
847   while (n--) {
848     fprintf(stderr, "tr_read_config: Parsing %s.\n", cfg_files[n]->d_name);
849     if (NULL == (jcfg = json_load_file(cfg_files[n]->d_name, 
850                                        JSON_DISABLE_EOF_CHECK, &rc))) {
851       fprintf (stderr, "tr_read_config: Error parsing config file %s.\n", 
852                cfg_files[n]->d_name);
853       return TR_CFG_NOPARSE;
854     }
855         
856     if ((TR_CFG_SUCCESS != tr_cfg_parse_internal(tr->new_cfg, jcfg)) ||
857         (TR_CFG_SUCCESS != tr_cfg_parse_rp_clients(tr->new_cfg, jcfg)) ||
858         (TR_CFG_SUCCESS != tr_cfg_parse_idp_realms(tr->new_cfg, jcfg)) ||
859         (TR_CFG_SUCCESS != tr_cfg_parse_default_servers(tr->new_cfg, jcfg)) ||
860         (TR_CFG_SUCCESS != tr_cfg_parse_comms(tr->new_cfg, jcfg))) {
861       tr_cfg_free(tr->new_cfg);
862       return TR_CFG_ERROR;
863     }
864   }
865
866   /* make sure we got a complete, consistent configuration */
867   if (TR_CFG_SUCCESS != tr_cfg_validate(tr->new_cfg)) {
868     fprintf(stderr, "tr_parse_config: Error: INVALID CONFIGURATION, EXITING\n");
869     return TR_CFG_ERROR;
870   }
871
872   return TR_CFG_SUCCESS;
873 }
874
875 TR_IDP_REALM *tr_cfg_find_idp (TR_CFG *tr_cfg, TR_NAME *idp_id, TR_CFG_RC *rc)
876 {
877
878   TR_IDP_REALM *cfg_idp;
879
880   if ((!tr_cfg) || (!idp_id)) {
881     if (rc)
882       *rc = TR_CFG_BAD_PARAMS;
883     return NULL;
884   }
885
886   for (cfg_idp = tr_cfg->idp_realms; NULL != cfg_idp; cfg_idp = cfg_idp->next) {
887     if (!tr_name_cmp (idp_id, cfg_idp->realm_id)) {
888       fprintf(stderr, "tr_cfg_find_idp: Found %s.\n", idp_id->buf);
889       return cfg_idp;
890     }
891   }
892   /* if we didn't find one, return NULL */ 
893   return NULL;
894 }
895
896 TR_RP_CLIENT *tr_cfg_find_rp (TR_CFG *tr_cfg, TR_NAME *rp_gss, TR_CFG_RC *rc)
897 {
898   TR_RP_CLIENT *cfg_rp;
899   int i;
900
901   if ((!tr_cfg) || (!rp_gss)) {
902     if (rc)
903       *rc = TR_CFG_BAD_PARAMS;
904     return NULL;
905   }
906
907   for (cfg_rp = tr_cfg->rp_clients; NULL != cfg_rp; cfg_rp = cfg_rp->next) {
908     for (i = 0; i < TR_MAX_GSS_NAMES; i++) {
909       if (!tr_name_cmp (rp_gss, cfg_rp->gss_names[i])) {
910         fprintf(stderr, "tr_cfg_find_rp: Found %s.\n", rp_gss->buf);
911         return cfg_rp;
912       }
913     }
914   }
915   /* if we didn't find one, return NULL */ 
916   return NULL;
917 }
918
919 static int is_cfg_file(const struct dirent *dent) {
920   int n;
921
922   /* if the last four letters of the filename are .cfg, return true. */
923   if ((4 <= (n = strlen(dent->d_name))) &&
924       (0 == strcmp(&(dent->d_name[n-4]), ".cfg"))) {
925     return 1;
926   }
927
928   /* otherwise, return false. */
929   return 0;
930 }
931
932 int tr_find_config_files (struct dirent ***cfg_files) {
933   int n = 0, i = 0;
934   
935   n = scandir(".", cfg_files, &is_cfg_file, 0);
936
937   if (n < 0) {
938     perror("scandir");
939     fprintf(stderr, "tr_find_config: scandir error.\n");
940     return 0;
941   }
942
943   if (n == 0) {
944     fprintf (stderr, "tr_find_config: No config files found.\n");
945     return 0;
946   }
947
948   i = n;
949   while(i--) {
950     fprintf(stderr, "tr_find_config: Config file found (%s).\n", (*cfg_files)[i]->d_name);
951   }
952     
953   return n;
954 }