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