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