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