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