Change spec "match" field back to a single string instead of array.
[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_cfgwatch.h>
42 #include <tr_comm.h>
43 #include <tr_config.h>
44 #include <tr_gss.h>
45 #include <tr_debug.h>
46 #include <tr_filter.h>
47 #include <trust_router/tr_constraint.h>
48 #include <tr_idp.h>
49 #include <tr.h>
50 #include <trust_router/trp.h>
51
52 void tr_print_config (TR_CFG *cfg) {
53   tr_notice("tr_print_config: Logging running trust router configuration.");
54   tr_print_comms(cfg->comms);
55 }
56
57 void tr_print_comms (TR_COMM *comm_list) {
58   TR_COMM *comm = NULL;
59
60   for (comm = comm_list; NULL != comm; comm = comm->next) {
61     tr_notice("tr_print_config: Community %s:", comm->id->buf);
62
63     tr_notice("tr_print_config:  - Member IdPs:");
64     tr_print_comm_idps(comm->idp_realms);
65
66     tr_notice("tr_print_config:  - Member RPs:");
67     tr_print_comm_rps(comm->rp_realms);
68   }
69 }
70
71 void tr_print_comm_idps (TR_IDP_REALM *idp_list) {
72   TR_IDP_REALM *idp = NULL;
73   char *s=NULL;
74
75   for (idp = idp_list; NULL != idp; idp = idp->comm_next) {
76     s=tr_idp_realm_to_str(NULL, idp);
77     if (s!=NULL)
78       tr_notice("tr_print_config:    - @%s", s);
79     else
80       tr_notice("tr_print_config: unable to allocate idp output string.");
81   }
82 }
83
84 void tr_print_comm_rps(TR_RP_REALM *rp_list) {
85   TR_RP_REALM *rp = NULL;
86
87   for (rp = rp_list; NULL != rp; rp = rp->next) {
88     tr_notice("tr_print_config:    - %s", rp->realm_name->buf);
89   }
90 }
91
92 TR_CFG *tr_cfg_new(TALLOC_CTX *mem_ctx)
93 {
94   return talloc_zero(mem_ctx, TR_CFG);
95 }
96
97 void tr_cfg_free (TR_CFG *cfg) {
98   talloc_free(cfg);
99 }
100
101 TR_CFG_MGR *tr_cfg_mgr_new(TALLOC_CTX *mem_ctx)
102 {
103   return talloc_zero(mem_ctx, TR_CFG_MGR);
104 }
105
106 void tr_cfg_mgr_free (TR_CFG_MGR *cfg_mgr) {
107   talloc_free(cfg_mgr);
108 }
109
110 TR_CFG_RC tr_apply_new_config (TR_CFG_MGR *cfg_mgr)
111 {
112   /* cfg_mgr->active is allowed to be null, but new cannot be */
113   if ((cfg_mgr==NULL) || (cfg_mgr->new==NULL))
114     return TR_CFG_BAD_PARAMS;
115
116   if (cfg_mgr->active != NULL)
117     tr_cfg_free(cfg_mgr->active);
118
119   cfg_mgr->active = cfg_mgr->new;
120   cfg_mgr->new=NULL; /* only keep a single handle on the new configuration */
121
122   tr_log_threshold(cfg_mgr->active->internal->log_threshold);
123   tr_console_threshold(cfg_mgr->active->internal->console_threshold);
124
125   return TR_CFG_SUCCESS;
126 }
127
128 static TR_CFG_RC tr_cfg_parse_internal(TR_CFG *trc, json_t *jcfg)
129 {
130   json_t *jint = NULL;
131   json_t *jmtd = NULL;
132   json_t *jtidsp = NULL;
133   json_t *jtrpsp = NULL;
134   json_t *jhname = NULL;
135   json_t *jlog = NULL;
136   json_t *jconthres = NULL;
137   json_t *jlogthres = NULL;
138   json_t *jcfgpoll = NULL;
139   json_t *jcfgsettle = NULL;
140   json_t *jroutesweep = NULL;
141   json_t *jrouteupdate = NULL;
142   json_t *jrouteconnect = NULL;
143
144   if ((!trc) || (!jcfg))
145     return TR_CFG_BAD_PARAMS;
146
147   if (NULL == trc->internal) {
148     if (NULL == (trc->internal = talloc_zero(trc, TR_CFG_INTERNAL)))
149       return TR_CFG_NOMEM;
150   }
151
152   if (NULL != (jint = json_object_get(jcfg, "tr_internal"))) {
153     if (NULL != (jmtd = json_object_get(jint, "max_tree_depth"))) {
154       if (json_is_number(jmtd)) {
155         trc->internal->max_tree_depth = json_integer_value(jmtd);
156       } else {
157         tr_debug("tr_cfg_parse_internal: Parsing error, max_tree_depth is not a number.");
158         return TR_CFG_NOPARSE;
159       }
160     } else {
161       /* If not configured, use the default */
162       trc->internal->max_tree_depth = TR_DEFAULT_MAX_TREE_DEPTH;
163     }
164     if (NULL != (jtidsp = json_object_get(jint, "tids_port"))) {
165       if (json_is_number(jtidsp)) {
166         trc->internal->tids_port = json_integer_value(jtidsp);
167       } else {
168         tr_debug("tr_cfg_parse_internal: Parsing error, tids_port is not a number.");
169         return TR_CFG_NOPARSE;
170       }
171     } else {
172       /* If not configured, use the default */
173       trc->internal->tids_port = TR_DEFAULT_TIDS_PORT;
174     }
175     if (NULL != (jtrpsp = json_object_get(jint, "trps_port"))) {
176       if (json_is_number(jtrpsp)) {
177         trc->internal->trps_port = json_integer_value(jtrpsp);
178       } else {
179         tr_debug("tr_cfg_parse_internal: Parsing error, trps_port is not a number.");
180         return TR_CFG_NOPARSE;
181       }
182     } else {
183       /* If not configured, use the default */
184       trc->internal->trps_port = TR_DEFAULT_TRPS_PORT;
185     }
186     if (NULL != (jhname = json_object_get(jint, "hostname"))) {
187       if (json_is_string(jhname)) {
188         trc->internal->hostname = json_string_value(jhname);
189       } else {
190         tr_debug("tr_cfg_parse_internal: Parsing error, hostname is not a string.");
191         return TR_CFG_NOPARSE;
192       }
193     }
194     if (NULL != (jcfgpoll = json_object_get(jint, "cfg_poll_interval"))) {
195       if (json_is_number(jcfgpoll)) {
196         trc->internal->cfg_poll_interval = json_integer_value(jcfgpoll);
197       } else {
198         tr_debug("tr_cfg_parse_internal: Parsing error, cfg_poll_interval is not a number.");
199         return TR_CFG_NOPARSE;
200       }
201     } else {
202       trc->internal->cfg_poll_interval = TR_CFGWATCH_DEFAULT_POLL;
203     }
204
205     if (NULL != (jcfgsettle = json_object_get(jint, "cfg_settling_time"))) {
206       if (json_is_number(jcfgsettle)) {
207         trc->internal->cfg_settling_time = json_integer_value(jcfgsettle);
208       } else {
209         tr_debug("tr_cfg_parse_internal: Parsing error, cfg_settling_time is not a number.");
210         return TR_CFG_NOPARSE;
211       }
212     } else {
213       trc->internal->cfg_settling_time = TR_CFGWATCH_DEFAULT_SETTLE;
214     }
215
216     if (NULL != (jrouteconnect = json_object_get(jint, "trp_connect_interval"))) {
217       if (json_is_number(jrouteconnect)) {
218         trc->internal->trp_connect_interval = json_integer_value(jrouteconnect);
219       } else {
220         tr_debug("tr_cfg_parse_internal: Parsing error, trp_connect_interval is not a number.");
221         return TR_CFG_NOPARSE;
222       }
223     } else {
224       /* if not configured, use the default */
225       trc->internal->trp_connect_interval=TR_DEFAULT_TRP_CONNECT_INTERVAL;
226     }
227
228     if (NULL != (jroutesweep = json_object_get(jint, "trp_sweep_interval"))) {
229       if (json_is_number(jroutesweep)) {
230         trc->internal->trp_sweep_interval = json_integer_value(jroutesweep);
231       } else {
232         tr_debug("tr_cfg_parse_internal: Parsing error, trp_sweep_interval is not a number.");
233         return TR_CFG_NOPARSE;
234       }
235     } else {
236       /* if not configured, use the default */
237       trc->internal->trp_sweep_interval=TR_DEFAULT_TRP_SWEEP_INTERVAL;
238     }
239
240     if (NULL != (jrouteupdate = json_object_get(jint, "trp_update_interval"))) {
241       if (json_is_number(jrouteupdate)) {
242         trc->internal->trp_update_interval = json_integer_value(jrouteupdate);
243       } else {
244         tr_debug("tr_cfg_parse_internal: Parsing error, trp_update_interval is not a number.");
245         return TR_CFG_NOPARSE;
246       }
247     } else {
248       /* if not configured, use the default */
249       trc->internal->trp_update_interval=TR_DEFAULT_TRP_UPDATE_INTERVAL;
250     }
251
252     if (NULL != (jlog = json_object_get(jint, "logging"))) {
253       if (NULL != (jlogthres = json_object_get(jlog, "log_threshold"))) {
254         if (json_is_string(jlogthres)) {
255           trc->internal->log_threshold = str2sev(json_string_value(jlogthres));
256         } else {
257           tr_debug("tr_cfg_parse_internal: Parsing error, log_threshold is not a string.");
258           return TR_CFG_NOPARSE;
259         }
260       } else {
261         /* If not configured, use the default */
262         trc->internal->log_threshold = TR_DEFAULT_LOG_THRESHOLD;
263       }
264
265       if (NULL != (jconthres = json_object_get(jlog, "console_threshold"))) {
266         if (json_is_string(jconthres)) {
267             trc->internal->console_threshold = str2sev(json_string_value(jconthres));
268         } else {
269           tr_debug("tr_cfg_parse_internal: Parsing error, console_threshold is not a string.");
270           return TR_CFG_NOPARSE;
271         }
272       } else {
273         /* If not configured, use the default */
274         trc->internal->console_threshold = TR_DEFAULT_CONSOLE_THRESHOLD;
275       }
276     } else {
277         /* If not configured, use the default */
278         trc->internal->console_threshold = TR_DEFAULT_CONSOLE_THRESHOLD;
279         trc->internal->log_threshold = TR_DEFAULT_LOG_THRESHOLD;
280     }
281
282     tr_debug("tr_cfg_parse_internal: Internal config parsed.");
283     return TR_CFG_SUCCESS;
284   }
285   return TR_CFG_SUCCESS;
286 }
287
288 static TR_CONSTRAINT *tr_cfg_parse_one_constraint(TALLOC_CTX *mem_ctx, char *ctype, json_t *jc, TR_CFG_RC *rc)
289 {
290   TR_CONSTRAINT *cons=NULL;
291   int i=0;
292
293   if ((!ctype) || (!jc) || (!rc) ||
294       (!json_is_array(jc)) ||
295       (0 >= json_array_size(jc)) ||
296       (TR_MAX_CONST_MATCHES < json_array_size(jc)) ||
297       (!json_is_string(json_array_get(jc, 0)))) {
298     tr_err("tr_cfg_parse_one_constraint: config error.");
299     *rc=TR_CFG_NOPARSE;
300     return NULL;
301   }
302
303   if (NULL==(cons=tr_constraint_new(mem_ctx))) {
304     tr_debug("tr_cfg_parse_one_constraint: Out of memory (cons).");
305     *rc=TR_CFG_NOMEM;
306     return NULL;
307   }
308
309   if (NULL==(cons->type=tr_new_name(ctype))) {
310     tr_err("tr_cfg_parse_one_constraint: Out of memory (type).");
311     *rc=TR_CFG_NOMEM;
312     tr_constraint_free(cons);
313     return NULL;
314   }
315
316   for (i=0; i < json_array_size(jc); i++) {
317     cons->matches[i]=tr_new_name(json_string_value(json_array_get(jc, i)));
318     if (cons->matches[i]==NULL) {
319       tr_err("tr_cfg_parse_one_constraint: Out of memory (match %d).", i+1);
320       *rc=TR_CFG_NOMEM;
321       tr_constraint_free(cons);
322       return NULL;
323     }
324   }
325
326   return cons;
327 }
328
329 static TR_FILTER *tr_cfg_parse_one_filter(TALLOC_CTX *mem_ctx, json_t *jfilt, TR_FILTER_TYPE ftype, TR_CFG_RC *rc)
330 {
331   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
332   TR_FILTER *filt=NULL;
333   json_t *jfaction=NULL;
334   json_t *jfspecs=NULL;
335   json_t *jffield=NULL;
336   json_t *jfmatch=NULL;
337   json_t *jrc=NULL;
338   json_t *jdc=NULL;
339   TR_NAME *name=NULL;
340   int i=0, j=0;
341
342   *rc=TR_CFG_ERROR;
343
344   if ((jfilt==NULL) || (rc==NULL)) {
345     tr_err("tr_cfg_parse_one_filter: null argument");
346     *rc=TR_CFG_BAD_PARAMS;
347     goto cleanup;
348   }
349     
350   if (NULL==(filt=tr_filter_new(tmp_ctx))) {
351     tr_err("tr_cfg_parse_one_filter: Out of memory.");
352     *rc=TR_CFG_NOMEM;
353     goto cleanup;
354   }
355   tr_filter_set_type(filt, ftype);
356
357   /* make sure we have space to represent the filter */
358   if (json_array_size(jfilt) > TR_MAX_FILTER_LINES) {
359     tr_err("tr_cfg_parse_one_filter: Filter has too many lines, maximum of %d.", TR_MAX_FILTER_LINES);
360     *rc=TR_CFG_NOPARSE;
361     goto cleanup;
362   }
363
364   /* For each entry in the filter... */
365   for (i=0; i < json_array_size(jfilt); i++) {
366     if ((NULL==(jfaction=json_object_get(json_array_get(jfilt, i), "action"))) ||
367         (!json_is_string(jfaction))) {
368       tr_debug("tr_cfg_parse_one_filter: Error parsing filter action.");
369       *rc=TR_CFG_NOPARSE;
370       goto cleanup;
371     }
372  
373     if ((NULL==(jfspecs=json_object_get(json_array_get(jfilt, i), "specs"))) ||
374         (!json_is_array(jfspecs)) ||
375         (0==json_array_size(jfspecs))) {
376       tr_debug("tr_cfg_parse_one_filter: Error parsing filter specs.");
377       *rc=TR_CFG_NOPARSE;
378       goto cleanup;
379     }
380   
381     if (TR_MAX_FILTER_SPECS < json_array_size(jfspecs)) {
382       tr_debug("tr_cfg_parse_one_filter: Filter has too many specs, maximimum of %d.", TR_MAX_FILTER_SPECS);
383       *rc=TR_CFG_NOPARSE;
384       goto cleanup;
385     }
386
387     if (NULL==(filt->lines[i]=tr_fline_new(filt))) {
388       tr_debug("tr_cfg_parse_one_filter: Out of memory allocating filter line %d.", i+1);
389       *rc=TR_CFG_NOMEM;
390       goto cleanup;
391     }
392
393     if (!strcmp(json_string_value(jfaction), "accept")) {
394       filt->lines[i]->action=TR_FILTER_ACTION_ACCEPT;
395     }
396     else if (!strcmp(json_string_value(jfaction), "reject")) {
397       filt->lines[i]->action=TR_FILTER_ACTION_REJECT;
398     }
399     else {
400       tr_debug("tr_cfg_parse_one_filter: Error parsing filter action, unknown action' %s'.", json_string_value(jfaction));
401       *rc=TR_CFG_NOPARSE;
402       goto cleanup;
403     }
404
405     if (NULL!=(jrc=json_object_get(json_array_get(jfilt, i), "realm_constraints"))) {
406       if (!json_is_array(jrc)) {
407         tr_err("tr_cfg_parse_one_filter: cannot parse realm_constraints, not an array.");
408         *rc=TR_CFG_NOPARSE;
409         goto cleanup;
410       } else if (json_array_size(jrc)>TR_MAX_CONST_MATCHES) {
411         tr_err("tr_cfg_parse_one_filter: realm_constraints has too many entries, maximum of %d.",
412                TR_MAX_CONST_MATCHES);
413         *rc=TR_CFG_NOPARSE;
414         goto cleanup;
415       } else if (json_array_size(jrc)>0) {
416         /* ok we actually have entries to process */
417         if (NULL==(filt->lines[i]->realm_cons=tr_cfg_parse_one_constraint(filt->lines[i], "realm", jrc, rc))) {
418           tr_debug("tr_cfg_parse_one_filter: Error parsing realm constraint");
419           *rc=TR_CFG_NOPARSE;
420           goto cleanup;
421         }
422       }
423     }
424
425     if (NULL!=(jdc=json_object_get(json_array_get(jfilt, i), "domain_constraints"))) {
426       if (!json_is_array(jdc)) {
427         tr_err("tr_cfg_parse_one_filter: cannot parse domain_constraints, not an array.");
428         *rc=TR_CFG_NOPARSE;
429         goto cleanup;
430       } else if (json_array_size(jdc)>TR_MAX_CONST_MATCHES) {
431         tr_err("tr_cfg_parse_one_filter: domain_constraints has too many entries, maximum of %d.",
432                TR_MAX_CONST_MATCHES);
433         *rc=TR_CFG_NOPARSE;
434         goto cleanup;
435       } else if (json_array_size(jdc)>0) {
436         if (NULL==(filt->lines[i]->domain_cons=tr_cfg_parse_one_constraint(filt->lines[i], "domain", jdc, rc))) {
437           tr_debug("tr_cfg_parse_one_filter: Error parsing domain constraint");
438           *rc=TR_CFG_NOPARSE;
439           goto cleanup;
440         }
441       }
442     }
443
444     /*For each filter spec within the filter line... */
445     for (j=0; j <json_array_size(jfspecs); j++) {
446       if ((NULL==(jffield=json_object_get(json_array_get(jfspecs, j), "field"))) ||
447           (!json_is_string(jffield))) {
448         tr_debug("tr_cfg_parse_one_filter: Error parsing filter: missing field for filer spec %d, filter line %d.", i, j);
449         *rc=TR_CFG_NOPARSE;
450         goto cleanup;
451       }
452
453       /* check that we have a match attribute */
454       if (NULL==(jfmatch=json_object_get(json_array_get(jfspecs, j), "match"))) {
455         tr_debug("tr_cfg_parse_one_filter: Error parsing filter: missing match for filer spec %d, filter line %d.", i, j);
456         *rc=TR_CFG_NOPARSE;
457         goto cleanup;
458       }
459
460       /* check that match is a string */
461       if (!json_is_string(jfmatch)) {
462         tr_debug("tr_cfg_parse_one_filter: Error parsing filter: match not a string for filter spec %d, filter line %d.", i, j);
463         *rc=TR_CFG_NOPARSE;
464         goto cleanup;
465       }
466
467       /* allocate the filter spec */
468       if (NULL==(filt->lines[i]->specs[j]=tr_fspec_new(filt->lines[i]))) {
469         tr_debug("tr_cfg_parse_one_filter: Out of memory.");
470         *rc=TR_CFG_NOMEM;
471         goto cleanup;
472       }
473
474       /* fill in the field */
475       if (NULL==(filt->lines[i]->specs[j]->field=tr_new_name(json_string_value(jffield)))) {
476         tr_debug("tr_cfg_parse_one_filter: Out of memory.");
477         *rc=TR_CFG_NOMEM;
478         goto cleanup;
479       }
480
481       /* fill in the matches */
482       if (NULL==(name=tr_new_name(json_string_value(jfmatch)))) {
483         tr_debug("tr_cfg_parse_one_filter: Out of memory.");
484         *rc=TR_CFG_NOMEM;
485         goto cleanup;
486       }
487       tr_fspec_set_match(filt->lines[i]->specs[j], name);
488     }
489   }
490   *rc=TR_CFG_SUCCESS;
491   talloc_steal(mem_ctx, filt);
492   
493  cleanup:
494   talloc_free(tmp_ctx);
495   if (*rc!=TR_CFG_SUCCESS)
496     filt=NULL;
497   return filt;
498 }
499
500 static TR_FILTER *tr_cfg_parse_filters(TALLOC_CTX *mem_ctx, json_t *jfilts, TR_CFG_RC *rc)
501 {
502   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
503   json_t *jfilt;
504   TR_FILTER *filt=NULL;
505
506   *rc=TR_CFG_ERROR;
507
508   /* no filters */
509   if (jfilts==NULL) {
510     *rc=TR_CFG_SUCCESS;
511     goto cleanup;
512   }
513
514   jfilt=json_object_get(jfilts, "tid_inbound");
515   if (jfilt!=NULL) {
516     filt=tr_cfg_parse_one_filter(tmp_ctx, jfilt, TR_FILTER_TYPE_TID_INCOMING, rc);
517     if (*rc!=TR_CFG_SUCCESS) {
518       tr_debug("tr_cfg_parse_filters: Error parsing tid_inbound filter.");
519       *rc=TR_CFG_NOPARSE;
520       goto cleanup;
521     }
522   } else {
523     tr_debug("tr_cfg_parse_filters: Unknown filter types in filter block.");
524     *rc=TR_CFG_NOPARSE;
525     goto cleanup;
526   }
527   
528   *rc=TR_CFG_SUCCESS;
529
530  cleanup:
531   if (*rc==TR_CFG_SUCCESS)
532     talloc_steal(mem_ctx, filt);
533   else if (filt!=NULL) {
534     talloc_free(filt);
535     filt=NULL;
536   }
537
538   talloc_free(tmp_ctx);
539   return filt;
540 }
541
542 static TR_AAA_SERVER *tr_cfg_parse_one_aaa_server(TALLOC_CTX *mem_ctx, json_t *jaddr, TR_CFG_RC *rc)
543 {
544   TR_AAA_SERVER *aaa = NULL;
545   TR_NAME *name=NULL;
546
547   if ((!jaddr) || (!json_is_string(jaddr))) {
548     tr_debug("tr_cfg_parse_one_aaa_server: Bad parameters.");
549     *rc = TR_CFG_BAD_PARAMS;
550     return NULL;
551   }
552
553   name=tr_new_name(json_string_value(jaddr));
554   if (name==NULL) {
555     tr_debug("tr_cfg_parse_one_aaa_server: Out of memory allocating hostname.");
556     *rc = TR_CFG_NOMEM;
557     return NULL;
558   }
559
560   aaa=tr_aaa_server_new(mem_ctx, name);
561   if (aaa==NULL) {
562     tr_free_name(name);
563     tr_debug("tr_cfg_parse_one_aaa_server: Out of memory allocating AAA server.");
564     *rc = TR_CFG_NOMEM;
565     return NULL;
566   }
567
568   return aaa;
569 }
570
571 static TR_AAA_SERVER *tr_cfg_parse_aaa_servers(TALLOC_CTX *mem_ctx, json_t *jaaas, TR_CFG_RC *rc) 
572 {
573   TALLOC_CTX *tmp_ctx=NULL;
574   TR_AAA_SERVER *aaa = NULL;
575   TR_AAA_SERVER *temp_aaa = NULL;
576   int i = 0;
577
578   for (i = 0; i < json_array_size(jaaas); i++) {
579     /* rc gets set in here */
580     if (NULL == (temp_aaa = tr_cfg_parse_one_aaa_server(tmp_ctx, json_array_get(jaaas, i), rc))) {
581       talloc_free(tmp_ctx);
582       return NULL;
583     }
584     /* TBD -- IPv6 addresses */
585     //    tr_debug("tr_cfg_parse_aaa_servers: Configuring AAA Server: ip_addr = %s.", inet_ntoa(temp_aaa->aaa_server_addr));
586     temp_aaa->next = aaa;
587     aaa = temp_aaa;
588   }
589   tr_debug("tr_cfg_parse_aaa_servers: Finished (rc=%d)", *rc);
590
591   for (temp_aaa=aaa; temp_aaa!=NULL; temp_aaa=temp_aaa->next)
592     talloc_steal(mem_ctx, temp_aaa);
593   talloc_free(tmp_ctx);
594   return aaa;
595 }
596
597 static TR_APC *tr_cfg_parse_one_apc(TALLOC_CTX *mem_ctx, json_t *japc, TR_CFG_RC *rc)
598 {
599   TR_APC *apc=NULL;
600   TR_NAME *name=NULL;
601   
602   *rc = TR_CFG_SUCCESS;         /* presume success */
603
604   if ((!japc) || (!rc) || (!json_is_string(japc))) {
605     tr_debug("tr_cfg_parse_one_apc: Bad parameters.");
606     if (rc) 
607       *rc = TR_CFG_BAD_PARAMS;
608     return NULL;
609   }
610
611   apc=tr_apc_new(mem_ctx);
612   if (apc==NULL) {
613     tr_debug("tr_cfg_parse_one_apc: Out of memory.");
614     *rc = TR_CFG_NOMEM;
615     return NULL;
616   }
617
618   name=tr_new_name(json_string_value(japc));
619   if (name==NULL) {
620     tr_debug("tr_cfg_parse_one_apc: No memory for APC name.");
621     tr_apc_free(apc);
622     *rc = TR_CFG_NOMEM;
623     return NULL;
624   }
625   tr_apc_set_id(apc, name); /* apc is now responsible for freeing the name */
626
627   return apc;
628 }
629
630 static TR_APC *tr_cfg_parse_apcs(TALLOC_CTX *mem_ctx, json_t *japcs, TR_CFG_RC *rc)
631 {
632   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
633   TR_APC *apcs=NULL;
634   TR_APC *new_apc=NULL;
635   int ii=0;
636   TR_CFG_RC call_rc=TR_CFG_ERROR;
637   
638   *rc = TR_CFG_SUCCESS;         /* presume success */
639
640   if ((!japcs) || (!rc) || (!json_is_array(japcs))) {
641     tr_debug("tr_cfg_parse_one_apc: Bad parameters.");
642     if (rc) 
643       *rc = TR_CFG_BAD_PARAMS;
644     return NULL;
645   }
646
647   for (ii=0; ii<json_array_size(japcs); ii++) {
648     new_apc=tr_cfg_parse_one_apc(tmp_ctx, json_array_get(japcs, ii), &call_rc);
649     if ((call_rc!=TR_CFG_SUCCESS) || (new_apc==NULL)) {
650       tr_debug("tr_cfg_parse_apcs: Error parsing APC %d.", ii+1);
651       *rc=TR_CFG_NOPARSE;
652       goto cleanup;
653     }
654     apcs=tr_apc_add(apcs, new_apc);
655   }
656
657   talloc_steal(mem_ctx, apcs);
658   *rc=TR_CFG_SUCCESS;
659
660  cleanup:
661   talloc_free(tmp_ctx);
662   return apcs;
663 }
664
665 static TR_NAME *tr_cfg_parse_name(TALLOC_CTX *mem_ctx, json_t *jname, TR_CFG_RC *rc)
666 {
667   TR_NAME *name=NULL;
668   *rc=TR_CFG_ERROR;
669
670   if ((jname==NULL) || (!json_is_string(jname))) {
671     tr_err("tr_cfg_parse_name: name missing or not a string");
672     *rc=TR_CFG_BAD_PARAMS;
673     name=NULL;
674   } else {
675     name=tr_new_name(json_string_value(jname));
676     if (name==NULL) {
677       tr_err("tr_cfg_parse_name: could not allocate name");
678       *rc=TR_CFG_NOMEM;
679     } else {
680       *rc=TR_CFG_SUCCESS;
681     }
682   }
683   return name;  
684 }
685
686 static int tr_cfg_parse_shared_config(json_t *jsc, TR_CFG_RC *rc)
687 {
688   const char *shared=NULL;
689   *rc=TR_CFG_SUCCESS;
690   
691   if ((jsc==NULL) ||
692       (!json_is_string(jsc)) ||
693       (NULL==(shared=json_string_value(jsc)))) {
694     *rc=TR_CFG_BAD_PARAMS;
695     return -1;
696   }
697
698   if (0==strcmp(shared, "no"))
699     return 0;
700   else if (0==strcmp(shared, "yes"))
701     return 1;
702
703   /* any other value is an error */
704   tr_debug("tr_cfg_parse_shared_config: invalid shared_config value \"%s\" (should be \"yes\" or \"no\")",
705            shared);
706   *rc=TR_CFG_NOPARSE;
707   return -1;
708 }
709
710 static TR_REALM_ORIGIN tr_cfg_realm_origin(json_t *jrealm)
711 {
712   json_t *jremote=json_object_get(jrealm, "remote");
713   const char *s=NULL;
714
715   if (jremote==NULL)
716     return TR_REALM_LOCAL;
717   if (!json_is_string(jremote)) {
718     tr_warning("tr_cfg_realm_origin: \"remote\" is not a string, assuming this is a local realm.");
719     return TR_REALM_LOCAL;
720   }
721   s=json_string_value(jremote);
722   if (strcasecmp(s, "yes")==0)
723     return TR_REALM_REMOTE_INCOMPLETE;
724   else if (strcasecmp(s, "no")!=0)
725     tr_warning("tr_cfg_realm_origin: \"remote\" is neither 'yes' nor 'no', assuming this is a local realm.");
726
727   return TR_REALM_LOCAL;
728 }
729
730 /* Parse the identity provider object from a realm and fill in the given TR_IDP_REALM. */
731 static TR_CFG_RC tr_cfg_parse_idp(TR_IDP_REALM *idp, json_t *jidp)
732 {
733   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
734   TR_APC *apcs=NULL;
735   TR_AAA_SERVER *aaa=NULL;
736   TR_CFG_RC rc=TR_CFG_ERROR;
737   
738   if (jidp==NULL)
739     goto cleanup;
740
741   idp->shared_config=tr_cfg_parse_shared_config(json_object_get(jidp, "shared_config"), &rc);
742   if (rc!=TR_CFG_SUCCESS) {
743     tr_err("tr_cfg_parse_idp: missing or malformed shared_config specification");
744     rc=TR_CFG_NOPARSE;
745     goto cleanup;
746   }
747
748   apcs=tr_cfg_parse_apcs(tmp_ctx, json_object_get(jidp, "apcs"), &rc);
749   if ((rc!=TR_CFG_SUCCESS) || (apcs==NULL)) {
750     tr_err("tr_cfg_parse_idp: unable to parse APC");
751     rc=TR_CFG_NOPARSE;
752     goto cleanup;
753   }
754   
755   aaa=tr_cfg_parse_aaa_servers(idp, json_object_get(jidp, "aaa_servers"), &rc);
756   if (rc!=TR_CFG_SUCCESS) {
757     tr_err("tr_cfg_parse_idp: unable to parse AAA servers");
758     rc=TR_CFG_NOPARSE;
759     goto cleanup;
760   }
761   
762   tr_debug("tr_cfg_parse_idp: APC=\"%.*s\"",
763            apcs->id->len,
764            apcs->id->buf);
765   
766   /* done, fill in the idp structures */
767   idp->apcs=apcs;
768   talloc_steal(idp, apcs);
769   idp->aaa_servers=aaa;
770   rc=TR_CFG_SUCCESS;
771   
772 cleanup:
773   if (rc!=TR_CFG_SUCCESS) {
774     if (apcs!=NULL)
775       tr_apc_free(apcs);
776     if (aaa!=NULL)
777       tr_aaa_server_free(aaa);
778   }
779   
780   talloc_free(tmp_ctx);
781   return rc;
782 }
783
784 /* parses idp realm */
785 static TR_IDP_REALM *tr_cfg_parse_one_idp_realm(TALLOC_CTX *mem_ctx, json_t *jrealm, TR_CFG_RC *rc)
786 {
787   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
788   TR_IDP_REALM *realm=NULL;
789   TR_CFG_RC call_rc=TR_CFG_ERROR;
790
791   *rc=TR_CFG_ERROR; /* default to error if not set */
792
793   if ((!jrealm) || (!rc)) {
794     tr_err("tr_cfg_parse_one_idp_realm: Bad parameters.");
795     if (rc)
796       *rc=TR_CFG_BAD_PARAMS;
797     goto cleanup;
798   }
799
800   if (NULL==(realm=tr_idp_realm_new(tmp_ctx))) {
801     tr_err("tr_cfg_parse_one_idp_realm: could not allocate idp realm.");
802     *rc=TR_CFG_NOMEM;
803     goto cleanup;
804   }
805
806   realm->origin=tr_cfg_realm_origin(jrealm);
807   if (realm->origin!=TR_REALM_LOCAL) {
808     tr_debug("tr_cfg_parse_one_idp_realm: realm is remote, should not have full IdP info.");
809     *rc=TR_CFG_NOPARSE;
810     goto cleanup;
811   }
812
813   /* must have a name */
814   realm->realm_id=tr_cfg_parse_name(realm,
815                                     json_object_get(jrealm, "realm"),
816                                    &call_rc);
817   if ((call_rc!=TR_CFG_SUCCESS) || (realm->realm_id==NULL)) {
818     tr_err("tr_cfg_parse_one_idp_realm: could not parse realm name");
819     *rc=TR_CFG_NOPARSE;
820     goto cleanup;
821   }
822   tr_debug("tr_cfg_parse_one_idp_realm: realm_id=\"%.*s\"",
823            realm->realm_id->len,
824            realm->realm_id->buf);
825         
826   call_rc=tr_cfg_parse_idp(realm, json_object_get(jrealm, "identity_provider"));
827   if (call_rc!=TR_CFG_SUCCESS) {
828     tr_err("tr_cfg_parse_one_idp_realm: could not parse identity_provider.");
829     *rc=TR_CFG_NOPARSE;
830     goto cleanup;
831   }
832   
833   *rc=TR_CFG_SUCCESS;
834
835 cleanup:
836   if (*rc==TR_CFG_SUCCESS)
837     talloc_steal(mem_ctx, realm);
838   else {
839     talloc_free(realm);
840     realm=NULL;
841   }
842   
843   talloc_free(tmp_ctx);
844   return realm;
845 }
846
847   /* Determine whether the realm is an IDP realm */
848 static int tr_cfg_is_idp_realm(json_t *jrealm)
849 {
850   /* If a realm spec contains an identity_provider, it's an IDP realm. */
851   if (NULL != json_object_get(jrealm, "identity_provider"))
852     return 1;
853   else
854     return 0;
855 }
856
857 static TR_IDP_REALM *tr_cfg_parse_one_remote_realm(TALLOC_CTX *mem_ctx, json_t *jrealm, TR_CFG_RC *rc)
858 {
859   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
860   TR_IDP_REALM *realm=talloc(mem_ctx, TR_IDP_REALM);
861   
862   *rc=TR_CFG_ERROR; /* default to error if not set */
863
864   if ((!jrealm) || (!rc)) {
865     tr_err("tr_cfg_parse_one_remote_realm: Bad parameters.");
866     if (rc)
867       *rc=TR_CFG_BAD_PARAMS;
868     goto cleanup;
869   }
870
871   if (NULL==(realm=tr_idp_realm_new(tmp_ctx))) {
872     tr_err("tr_cfg_parse_one_remote_realm: could not allocate idp realm.");
873     *rc=TR_CFG_NOMEM;
874     goto cleanup;
875   }
876
877   /* must have a name */
878   realm->realm_id=tr_cfg_parse_name(realm,
879                                     json_object_get(jrealm, "realm"),
880                                     rc);
881   if ((*rc!=TR_CFG_SUCCESS) || (realm->realm_id==NULL)) {
882     tr_err("tr_cfg_parse_one_remote_realm: could not parse realm name");
883     *rc=TR_CFG_NOPARSE;
884     goto cleanup;
885   }
886   tr_debug("tr_cfg_parse_one_remote_realm: realm_id=\"%.*s\"",
887            realm->realm_id->len,
888            realm->realm_id->buf);
889
890   realm->origin=tr_cfg_realm_origin(jrealm);
891   *rc=TR_CFG_SUCCESS;
892
893 cleanup:
894   if (*rc==TR_CFG_SUCCESS)
895     talloc_steal(mem_ctx, realm);
896   else {
897     talloc_free(realm);
898     realm=NULL;
899   }
900   
901   talloc_free(tmp_ctx);
902   return realm;
903 }
904
905 static int tr_cfg_is_remote_realm(json_t *jrealm)
906 {
907   return (tr_cfg_realm_origin(jrealm)!=TR_REALM_LOCAL);
908 }
909
910 /* Parse any idp realms in the j_realms object. Ignores other realm types. */
911 static TR_IDP_REALM *tr_cfg_parse_idp_realms(TALLOC_CTX *mem_ctx, json_t *jrealms, TR_CFG_RC *rc)
912 {
913   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
914   TR_IDP_REALM *realms=NULL;
915   TR_IDP_REALM *new_realm=NULL;
916   json_t *this_jrealm=NULL;
917   int ii=0;
918
919   *rc=TR_CFG_ERROR;
920   if ((jrealms==NULL) || (!json_is_array(jrealms))) {
921     tr_err("tr_cfg_parse_idp_realms: realms not an array");
922     *rc=TR_CFG_BAD_PARAMS;
923     goto cleanup;
924   }
925
926   for (ii=0; ii<json_array_size(jrealms); ii++) {
927     this_jrealm=json_array_get(jrealms, ii);
928     if (tr_cfg_is_idp_realm(this_jrealm)) {
929       new_realm=tr_cfg_parse_one_idp_realm(tmp_ctx, this_jrealm, rc);
930       if ((*rc)!=TR_CFG_SUCCESS) {
931         tr_err("tr_cfg_parse_idp_realms: error decoding realm entry %d", ii+1);
932         *rc=TR_CFG_NOPARSE;
933         goto cleanup;
934       }
935       realms=tr_idp_realm_add(realms, new_realm);
936     } else if (tr_cfg_is_remote_realm(this_jrealm)) {
937       new_realm=tr_cfg_parse_one_remote_realm(tmp_ctx, this_jrealm, rc);
938       if ((*rc)!=TR_CFG_SUCCESS) {
939         tr_err("tr_cfg_parse_idp_realms: error decoding remote realm entry %d", ii+1);
940         *rc=TR_CFG_NOPARSE;
941         goto cleanup;
942       }
943       realms=tr_idp_realm_add(realms, new_realm);
944     }
945   }
946   
947   *rc=TR_CFG_SUCCESS;
948   talloc_steal(mem_ctx, realms);
949
950 cleanup:
951   talloc_free(tmp_ctx);
952   return realms;
953 }
954
955 #if 0
956 static TR_IDP_REALM *tr_cfg_parse_remote_realms(TALLOC_CTX *mem_ctx, json_t *jrealms, TR_CFG_RC *rc)
957 {
958   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
959   TR_IDP_REALM *realms=NULL;
960   TR_IDP_REALM *new_realm=NULL;
961   json_t *this_jrealm=NULL;
962   int ii=0;
963
964   *rc=TR_CFG_ERROR;
965   if ((jrealms==NULL) || (!json_is_array(jrealms))) {
966     tr_err("tr_cfg_parse_remote_realms: realms not an array");
967     *rc=TR_CFG_BAD_PARAMS;
968     goto cleanup;
969   }
970
971   for (ii=0; ii<json_array_size(jrealms); ii++) {
972     this_jrealm=json_array_get(jrealms, ii);
973     if (tr_cfg_is_remote_realm(this_jrealm)) {
974       new_realm=tr_cfg_parse_one_remote_realm(tmp_ctx, this_jrealm, rc);
975       if ((*rc)!=TR_CFG_SUCCESS) {
976         tr_err("tr_cfg_parse_remote_realms: error decoding remote realm entry %d", ii+1);
977         *rc=TR_CFG_NOPARSE;
978         goto cleanup;
979       }
980       realms=tr_idp_realm_add(realms, new_realm);
981     }
982   }
983   
984   *rc=TR_CFG_SUCCESS;
985   talloc_steal(mem_ctx, realms);
986
987 cleanup:
988   talloc_free(tmp_ctx);
989   return realms;
990 }
991 #endif /* 0 */
992
993 static TR_GSS_NAMES *tr_cfg_parse_gss_names(TALLOC_CTX *mem_ctx, json_t *jgss_names, TR_CFG_RC *rc)
994 {
995   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
996   TR_GSS_NAMES *gn=NULL;
997   json_t *jname=NULL;
998   int ii=0;
999   TR_NAME *name=NULL;
1000
1001   if ((rc==NULL) || (jgss_names==NULL)) {
1002     tr_err("tr_cfg_parse_gss_names: Bad parameters.");
1003     *rc=TR_CFG_BAD_PARAMS;
1004
1005   }
1006
1007   if (!json_is_array(jgss_names)) {
1008     tr_err("tr_cfg_parse_gss_names: gss_names not an array.");
1009     *rc=TR_CFG_NOPARSE;
1010     goto cleanup;
1011   }
1012
1013   gn=tr_gss_names_new(tmp_ctx);
1014   for (ii=0; ii<json_array_size(jgss_names); ii++) {
1015     jname=json_array_get(jgss_names, ii);
1016     if (!json_is_string(jname)) {
1017       tr_err("tr_cfg_parse_gss_names: Encountered non-string gss name.");
1018       *rc=TR_CFG_NOPARSE;
1019       goto cleanup;
1020     }
1021
1022     name=tr_new_name(json_string_value(jname));
1023     if (name==NULL) {
1024       tr_err("tr_cfg_parse_gss_names: Out of memory allocating gss name.");
1025       *rc=TR_CFG_NOMEM;
1026       goto cleanup;
1027     }
1028
1029     if (tr_gss_names_add(gn, name)!=0) {
1030       tr_free_name(name);
1031       tr_err("tr_cfg_parse_gss_names: Unable to add gss name to RP client.");
1032       *rc=TR_CFG_ERROR;
1033       goto cleanup;
1034     }
1035   }
1036
1037   talloc_steal(mem_ctx, gn);
1038   *rc=TR_CFG_SUCCESS;
1039
1040  cleanup:
1041   talloc_free(tmp_ctx);
1042   if ((*rc!=TR_CFG_SUCCESS) && (gn!=NULL))
1043     gn=NULL;
1044   return gn;
1045 }
1046
1047 /* default filter accepts realm and *.realm */
1048 static TR_FILTER *tr_cfg_default_filter(TALLOC_CTX *mem_ctx, TR_NAME *realm, TR_CFG_RC *rc)
1049 {
1050   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1051   TR_FILTER *filt=NULL;
1052   TR_CONSTRAINT *cons=NULL;
1053   TR_NAME *name=NULL;
1054   TR_NAME *n_prefix=tr_new_name("*.");
1055   TR_NAME *n_rp_realm_1=tr_new_name("rp_realm");
1056   TR_NAME *n_rp_realm_2=tr_new_name("rp_realm");
1057   TR_NAME *n_domain=tr_new_name("domain");
1058   TR_NAME *n_realm=tr_new_name("realm");
1059   
1060
1061   if ((realm==NULL) || (rc==NULL)) {
1062     tr_debug("tr_cfg_default_filter: invalid arguments.");
1063     if (rc!=NULL)
1064       *rc=TR_CFG_BAD_PARAMS;
1065     goto cleanup;
1066   }
1067
1068   if ((n_prefix==NULL) ||
1069       (n_rp_realm_1==NULL) ||
1070       (n_rp_realm_2==NULL) ||
1071       (n_domain==NULL) ||
1072       (n_realm==NULL)) {
1073     tr_debug("tr_cfg_default_filter: unable to allocate names.");
1074     *rc=TR_CFG_NOMEM;
1075     goto cleanup;
1076   }
1077
1078   filt=tr_filter_new(tmp_ctx);
1079   if (filt==NULL) {
1080     tr_debug("tr_cfg_default_filter: could not allocate filter.");
1081     *rc=TR_CFG_NOMEM;
1082     goto cleanup;
1083   }
1084   tr_filter_set_type(filt, TR_FILTER_TYPE_TID_INCOMING);
1085   filt->lines[0]=tr_fline_new(filt);
1086   if (filt->lines[0]==NULL) {
1087     tr_debug("tr_cfg_default_filter: could not allocate filter line.");
1088     *rc=TR_CFG_NOMEM;
1089     goto cleanup;
1090   }
1091
1092   filt->lines[0]->action=TR_FILTER_ACTION_ACCEPT;
1093   filt->lines[0]->specs[0]=tr_fspec_new(filt->lines[0]);
1094   filt->lines[0]->specs[0]->field=n_rp_realm_1;
1095   n_rp_realm_1=NULL; /* we don't own this name any more */
1096
1097   name=tr_dup_name(realm);
1098   if (name==NULL) {
1099     tr_debug("tr_cfg_default_filter: could not allocate realm name.");
1100     *rc=TR_CFG_NOMEM;
1101     goto cleanup;
1102   }
1103   tr_fspec_set_match(filt->lines[0]->specs[0], name);
1104   name=NULL; /* we no longer own the name */
1105
1106   /* now do the wildcard name */
1107   filt->lines[0]->specs[1]=tr_fspec_new(filt->lines[0]);
1108   filt->lines[0]->specs[1]->field=n_rp_realm_2;
1109   n_rp_realm_2=NULL; /* we don't own this name any more */
1110
1111   if (NULL==(name=tr_name_cat(n_prefix, realm))) {
1112     tr_debug("tr_cfg_default_filter: could not allocate wildcard realm name.");
1113     *rc=TR_CFG_NOMEM;
1114     goto cleanup;
1115   }
1116
1117   tr_fspec_set_match(filt->lines[0]->specs[1], name);
1118   name=NULL; /* we no longer own the name */
1119
1120   /* domain constraint */
1121   if (NULL==(cons=tr_constraint_new(filt->lines[0]))) {
1122     tr_debug("tr_cfg_default_filter: could not allocate domain constraint.");
1123     *rc=TR_CFG_NOMEM;
1124     goto cleanup;
1125   }
1126
1127   cons->type=n_domain;
1128   n_domain=NULL; /* belongs to the constraint now */
1129   name=tr_dup_name(realm);
1130   if (name==NULL) {
1131     tr_debug("tr_cfg_default_filter: could not allocate realm name for domain constraint.");
1132     *rc=TR_CFG_NOMEM;
1133     goto cleanup;
1134   }
1135   cons->matches[0]=name;
1136   name=tr_name_cat(n_prefix, realm);
1137   if (name==NULL) {
1138     tr_debug("tr_cfg_default_filter: could not allocate wildcard realm name for domain constraint.");
1139     *rc=TR_CFG_NOMEM;
1140     goto cleanup;
1141   }
1142   cons->matches[1]=name;
1143   name=NULL;
1144   filt->lines[0]->domain_cons=cons;
1145
1146
1147   /* realm constraint */
1148   if (NULL==(cons=tr_constraint_new(filt->lines[0]))) {
1149     tr_debug("tr_cfg_default_filter: could not allocate realm constraint.");
1150     *rc=TR_CFG_NOMEM;
1151     goto cleanup;
1152   }
1153
1154   cons->type=n_realm;
1155   n_realm=NULL; /* belongs to the constraint now */
1156   name=tr_dup_name(realm);
1157   if (name==NULL) {
1158     tr_debug("tr_cfg_default_filter: could not allocate realm name for realm constraint.");
1159     *rc=TR_CFG_NOMEM;
1160     goto cleanup;
1161   }
1162   cons->matches[0]=name;
1163   name=tr_name_cat(n_prefix, realm);
1164   if (name==NULL) {
1165     tr_debug("tr_cfg_default_filter: could not allocate wildcard realm name for realm constraint.");
1166     *rc=TR_CFG_NOMEM;
1167     goto cleanup;
1168   }
1169   cons->matches[1]=name;
1170   name=NULL;
1171   filt->lines[0]->realm_cons=cons;
1172
1173   talloc_steal(mem_ctx, filt);
1174 cleanup:
1175   talloc_free(tmp_ctx);
1176
1177   if (*rc!=TR_CFG_SUCCESS)
1178     filt=NULL;
1179
1180   if (n_prefix!=NULL)
1181     tr_free_name(n_prefix);
1182   if (n_rp_realm_1!=NULL)
1183     tr_free_name(n_rp_realm_1);
1184   if (n_rp_realm_2!=NULL)
1185     tr_free_name(n_rp_realm_2);
1186   if (n_realm!=NULL)
1187     tr_free_name(n_realm);
1188   if (n_domain!=NULL)
1189     tr_free_name(n_domain);
1190   if (name!=NULL)
1191     tr_free_name(name);
1192
1193   return filt;
1194 }
1195
1196 /* parses rp client */
1197 static TR_RP_CLIENT *tr_cfg_parse_one_rp_client(TALLOC_CTX *mem_ctx, json_t *jrealm, TR_CFG_RC *rc)
1198 {
1199   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1200   TR_RP_CLIENT *client=NULL;
1201   TR_CFG_RC call_rc=TR_CFG_ERROR;
1202   TR_FILTER *new_filt=NULL;
1203   TR_NAME *realm=NULL;
1204   json_t *jfilt=NULL;
1205   json_t *jrealm_id=NULL;
1206
1207   *rc=TR_CFG_ERROR; /* default to error if not set */
1208
1209   if ((!jrealm) || (!rc)) {
1210     tr_err("tr_cfg_parse_one_rp_client: Bad parameters.");
1211     if (rc)
1212       *rc=TR_CFG_BAD_PARAMS;
1213     goto cleanup;
1214   }
1215
1216   if ((NULL==(jrealm_id=json_object_get(jrealm, "realm"))) || (!json_is_string(jrealm_id))) {
1217     tr_debug("tr_cfg_parse_one_rp_client: no realm ID found.");
1218     *rc=TR_CFG_BAD_PARAMS;
1219     goto cleanup;
1220   } 
1221
1222   tr_debug("tr_cfg_parse_one_rp_client: realm_id=\"%s\"", json_string_value(jrealm_id));
1223   realm=tr_new_name(json_string_value(jrealm_id));
1224   if (realm==NULL) {
1225     tr_err("tr_cfg_parse_one_rp_client: could not allocate realm ID.");
1226     *rc=TR_CFG_NOMEM;
1227     goto cleanup;
1228   }
1229
1230   if (NULL==(client=tr_rp_client_new(tmp_ctx))) {
1231     tr_err("tr_cfg_parse_one_rp_client: could not allocate rp client.");
1232     *rc=TR_CFG_NOMEM;
1233     goto cleanup;
1234   }
1235
1236   client->gss_names=tr_cfg_parse_gss_names(client, json_object_get(jrealm, "gss_names"), &call_rc);
1237
1238   if (call_rc!=TR_CFG_SUCCESS) {
1239     tr_err("tr_cfg_parse_one_rp_client: could not parse gss_names.");
1240     *rc=TR_CFG_NOPARSE;
1241     goto cleanup;
1242   }
1243
1244   /* parse filters */
1245   jfilt=json_object_get(jrealm, "filters");
1246   if (jfilt!=NULL) {
1247     new_filt=tr_cfg_parse_filters(tmp_ctx, jfilt, &call_rc);
1248     if (call_rc!=TR_CFG_SUCCESS) {
1249       tr_err("tr_cfg_parse_one_rp_client: could not parse filters.");
1250       *rc=TR_CFG_NOPARSE;
1251       goto cleanup;
1252     }
1253   } else {
1254     tr_debug("tr_cfg_parse_one_rp_client: no filters specified, using default filters.");
1255     new_filt=tr_cfg_default_filter(tmp_ctx, realm, &call_rc);
1256     if (call_rc!=TR_CFG_SUCCESS) {
1257       tr_err("tr_cfg_parse_one_rp_client: could not set default filters.");
1258       *rc=TR_CFG_NOPARSE;
1259       goto cleanup;
1260     }
1261   }
1262
1263   tr_rp_client_set_filter(client, new_filt);
1264   *rc=TR_CFG_SUCCESS;
1265
1266   cleanup:
1267   if (realm!=NULL)
1268     tr_free_name(realm);
1269
1270     if (*rc==TR_CFG_SUCCESS)
1271       talloc_steal(mem_ctx, client);
1272     else {
1273       talloc_free(client);
1274       client=NULL;
1275     }
1276
1277     talloc_free(tmp_ctx);
1278     return client;
1279   }
1280
1281   /* Determine whether the realm is an RP realm */
1282 static int tr_cfg_is_rp_realm(json_t *jrealm)
1283 {
1284   /* Check that we have a gss name. */
1285   if (NULL != json_object_get(jrealm, "gss_names"))
1286     return 1;
1287   else
1288     return 0;
1289 }
1290
1291 /* Parse any rp clients in the j_realms object. Ignores other realms. */
1292 static TR_RP_CLIENT *tr_cfg_parse_rp_clients(TALLOC_CTX *mem_ctx, json_t *jrealms, TR_CFG_RC *rc)
1293 {
1294   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1295   TR_RP_CLIENT *clients=NULL;
1296   TR_RP_CLIENT *new_client=NULL;
1297   json_t *this_jrealm=NULL;
1298   int ii=0;
1299
1300   *rc=TR_CFG_ERROR;
1301   if ((jrealms==NULL) || (!json_is_array(jrealms))) {
1302     tr_err("tr_cfg_parse_rp_clients: realms not an array");
1303     *rc=TR_CFG_BAD_PARAMS;
1304     goto cleanup;
1305   }
1306
1307   for (ii=0; ii<json_array_size(jrealms); ii++) {
1308     this_jrealm=json_array_get(jrealms, ii);
1309     if (tr_cfg_is_rp_realm(this_jrealm)) {
1310       new_client=tr_cfg_parse_one_rp_client(tmp_ctx, this_jrealm, rc);
1311       if ((*rc)!=TR_CFG_SUCCESS) {
1312         tr_err("tr_cfg_parse_rp_clients: error decoding realm entry %d", ii+1);
1313         *rc=TR_CFG_NOPARSE;
1314         goto cleanup;
1315       }
1316       clients=tr_rp_client_add(clients, new_client);
1317     }
1318   }
1319   
1320   *rc=TR_CFG_SUCCESS;
1321   talloc_steal(mem_ctx, clients);
1322
1323 cleanup:
1324   talloc_free(tmp_ctx);
1325   return clients;
1326 }
1327
1328 /* takes a talloc context, but currently does not use it */
1329 static TR_NAME *tr_cfg_parse_org_name(TALLOC_CTX *mem_ctx, json_t *j_org, TR_CFG_RC *rc)
1330 {
1331   TR_NAME *name=NULL;
1332
1333   if ((j_org==NULL) || (rc==NULL) || (!json_is_string(j_org))) {
1334     tr_debug("tr_cfg_parse_org_name: Bad parameters.");
1335     if (rc!=NULL)
1336       *rc = TR_CFG_BAD_PARAMS; /* fill in return value if we can */
1337     return NULL;
1338   }
1339
1340   name=tr_new_name(json_string_value(j_org));
1341   if (name==NULL)
1342     *rc=TR_CFG_NOMEM;
1343   else
1344     *rc=TR_CFG_SUCCESS;
1345   return name;
1346 }
1347
1348 #if 0
1349 /* Update the community information with data from a new batch of IDP realms.
1350  * May partially add realms if there is a failure, no guarantees.
1351  * Call like comms=tr_comm_idp_update(comms, new_realms, &rc) */
1352 static TR_COMM *tr_cfg_comm_idp_update(TALLOC_CTX *mem_ctx, TR_COMM *comms, TR_IDP_REALM *new_realms, TR_CFG_RC *rc)
1353 {
1354   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1355   TR_COMM *comm=NULL; /* community looked up in comms table */
1356   TR_COMM *new_comms=NULL; /* new communities as we create them */
1357   TR_IDP_REALM *realm=NULL;
1358   TR_APC *apc=NULL; /* apc of one realm */
1359
1360   if (rc==NULL) {
1361     *rc=TR_CFG_BAD_PARAMS;
1362     goto cleanup;
1363   }
1364
1365   /* start with an empty list communities, then fill that in */
1366   for (realm=new_realms; realm!=NULL; realm=realm->next) {
1367     for (apc=realm->apcs; apc!=NULL; apc=apc->next) {
1368       comm=tr_comm_lookup(comms, apc->id);
1369       if (comm==NULL) {
1370         comm=tr_comm_new(tmp_ctx);
1371         if (comm==NULL) {
1372           tr_debug("tr_cfg_comm_idp_update: unable to allocate new community.");
1373           *rc=TR_CFG_NOMEM;
1374           goto cleanup;
1375         }
1376         /* fill in the community with info */
1377         comm->type=TR_COMM_APC; /* realms added this way are in APCs */
1378         comm->expiration_interval=TR_DEFAULT_APC_EXPIRATION_INTERVAL;
1379         comm->id=tr_dup_name(apc->id);
1380         tr_comm_add_idp_realm(comm, realm);
1381         new_comms=tr_comm_add(new_comms, comm);
1382       } else {
1383         /* add this realm to the comm */
1384         tr_comm_add_idp_realm(comm, realm);
1385       }
1386     }
1387   }
1388
1389   /* we successfully built a list, add it to the other list */
1390   comms=tr_comm_add(comms, new_comms);
1391   talloc_steal(mem_ctx, comms);
1392  cleanup:
1393   talloc_free(tmp_ctx);
1394   return comms;
1395 }
1396 #endif
1397
1398 static TR_CFG_RC tr_cfg_parse_one_local_org(TR_CFG *trc, json_t *jlorg)
1399 {
1400   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1401   TR_CFG_RC retval=TR_CFG_ERROR; /* our return code */
1402   TR_CFG_RC rc=TR_CFG_ERROR; /* return code from functions we call */
1403   TR_NAME *org_name=NULL;
1404   json_t *j_org=NULL;
1405   json_t *j_realms=NULL;
1406   TR_IDP_REALM *new_idp_realms=NULL;
1407   TR_RP_CLIENT *new_rp_clients=NULL;
1408
1409   tr_debug("tr_cfg_parse_one_local_org: parsing local organization");
1410
1411   /* get organization_name (optional) */
1412   if (NULL==(j_org=json_object_get(jlorg, "organization_name"))) {
1413     tr_debug("tr_cfg_parse_one_local_org: organization_name unspecified");
1414   } else {
1415     org_name=tr_cfg_parse_org_name(tmp_ctx, j_org, &rc);
1416     if (rc==TR_CFG_SUCCESS) {
1417       tr_debug("tr_cfg_parse_one_local_org: organization_name=\"%.*s\"",
1418                org_name->len,
1419                org_name->buf);
1420       /* we don't actually do anything with this, but we could */
1421       tr_free_name(org_name);
1422       org_name=NULL; 
1423     }
1424   }
1425
1426   /* Now get realms. Allow this to be missing; even though that is a pointless organization entry,
1427    * it's harmless. Report a warning because that might be unintentional. */
1428   if (NULL==(j_realms=json_object_get(jlorg, "realms"))) {
1429     tr_warning("tr_cfg_parse_one_local_org: warning - no realms in this local organization");
1430   } else {
1431     /* Allocate in the tmp_ctx so these will be cleaned up if we do not complete successfully. */
1432     new_idp_realms=tr_cfg_parse_idp_realms(tmp_ctx, j_realms, &rc);
1433     if (rc!=TR_CFG_SUCCESS)
1434       goto cleanup;
1435
1436     new_rp_clients=tr_cfg_parse_rp_clients(tmp_ctx, j_realms, &rc);
1437     if (rc!=TR_CFG_SUCCESS)
1438       goto cleanup;
1439   }
1440   retval=TR_CFG_SUCCESS;
1441   
1442 cleanup:
1443   /* if we succeeded, link things to the configuration and move out of tmp context */
1444   if (retval==TR_CFG_SUCCESS) {
1445     if (new_idp_realms!=NULL) {
1446       trc->idp_realms=tr_idp_realm_add(trc->idp_realms, new_idp_realms); /* fixes talloc contexts except for head*/
1447       talloc_steal(trc, trc->idp_realms); /* make sure the head is in the right context */
1448     }
1449
1450     if (new_rp_clients!=NULL) {
1451       trc->rp_clients=tr_rp_client_add(trc->rp_clients, new_rp_clients); /* fixes talloc contexts */
1452       talloc_steal(trc, trc->rp_clients); /* make sure head is in the right context */
1453     }
1454   }
1455
1456   talloc_free(tmp_ctx);
1457   return rc;
1458 }
1459
1460 /* Parse local organizations if present. Returns success if there are none. On failure, the configuration is unreliable. */
1461 static TR_CFG_RC tr_cfg_parse_local_orgs(TR_CFG *trc, json_t *jcfg)
1462 {
1463   json_t *jlocorgs=NULL;
1464   int ii=0;
1465
1466   jlocorgs=json_object_get(jcfg, "local_organizations");
1467   if (jlocorgs==NULL)
1468     return TR_CFG_SUCCESS;
1469
1470   if (!json_is_array(jlocorgs)) {
1471     tr_err("tr_cfg_parse_local_orgs: local_organizations is not an array.");
1472     return TR_CFG_NOPARSE;
1473   }
1474
1475   for (ii=0; ii<json_array_size(jlocorgs); ii++) {
1476     if (tr_cfg_parse_one_local_org(trc, json_array_get(jlocorgs, ii))!=TR_CFG_SUCCESS) {
1477       tr_err("tr_cfg_parse_local_orgs: error parsing local_organization %d.", ii+1);
1478       return TR_CFG_NOPARSE;
1479     }
1480   }
1481
1482   return TR_CFG_SUCCESS;
1483 }
1484
1485 static TR_CFG_RC tr_cfg_parse_one_peer_org(TR_CFG *trc, json_t *jporg)
1486 {
1487   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1488   json_t *jhost=NULL;
1489   json_t *jport=NULL;
1490   json_t *jgss=NULL;
1491   TRP_PEER *new_peer=NULL;
1492   TR_GSS_NAMES *names=NULL;
1493   TR_CFG_RC rc=TR_CFG_ERROR;
1494
1495   jhost=json_object_get(jporg, "hostname");
1496   jport=json_object_get(jporg, "port");
1497   jgss=json_object_get(jporg, "gss_names");
1498
1499   if ((jhost==NULL) || (!json_is_string(jhost))) {
1500     tr_err("tr_cfg_parse_one_peer_org: hostname not specified or not a string.");
1501     rc=TR_CFG_NOPARSE;
1502     goto cleanup;
1503   }
1504
1505   if ((jport!=NULL) && (!json_is_number(jport))) {
1506     /* note that not specifying the port is allowed, but if set it must be a number */
1507     tr_err("tr_cfg_parse_one_peer_org: port is not a number.");
1508     rc=TR_CFG_NOPARSE;
1509     goto cleanup;
1510   }
1511   
1512   if ((jgss==NULL) || (!json_is_array(jgss))) {
1513     tr_err("tr_cfg_parse_one_peer_org: gss_names not specified or not an array.");
1514     rc=TR_CFG_NOPARSE;
1515     goto cleanup;
1516   }
1517
1518   new_peer=trp_peer_new(tmp_ctx);
1519   if (new_peer==NULL) {
1520     tr_err("tr_cfg_parse_one_peer_org: could not allocate new peer.");
1521     rc=TR_CFG_NOMEM;
1522     goto cleanup;
1523   }
1524
1525   trp_peer_set_server(new_peer, json_string_value(jhost));
1526   if (jport==NULL)
1527     trp_peer_set_port(new_peer, TRP_PORT);
1528   else
1529     trp_peer_set_port(new_peer, json_integer_value(jport));
1530
1531   names=tr_cfg_parse_gss_names(tmp_ctx, jgss, &rc);
1532   if (rc!=TR_CFG_SUCCESS) {
1533     tr_err("tr_cfg_parse_one_peer_org: unable to parse gss names.");
1534     rc=TR_CFG_NOPARSE;
1535     goto cleanup;
1536   }
1537   trp_peer_set_gss_names(new_peer, names);
1538
1539   /* success! */
1540   trp_ptable_add(trc->peers, new_peer);
1541   rc=TR_CFG_SUCCESS;
1542
1543  cleanup:
1544   talloc_free(tmp_ctx);
1545   return rc;
1546 }
1547
1548 /* Parse peer organizations, if present. Returns success if there are none. */
1549 static TR_CFG_RC tr_cfg_parse_peer_orgs(TR_CFG *trc, json_t *jcfg)
1550 {
1551   json_t *jpeerorgs=NULL;
1552   int ii=0;
1553
1554   jpeerorgs=json_object_get(jcfg, "peer_organizations");
1555   if (jpeerorgs==NULL)
1556     return TR_CFG_SUCCESS;
1557
1558   if (!json_is_array(jpeerorgs)) {
1559     tr_err("tr_cfg_parse_peer_orgs: peer_organizations is not an array.");
1560     return TR_CFG_NOPARSE;
1561   }
1562
1563   for (ii=0; ii<json_array_size(jpeerorgs); ii++) {
1564     if (tr_cfg_parse_one_peer_org(trc, json_array_get(jpeerorgs, ii))!=TR_CFG_SUCCESS) {
1565       tr_err("tr_cfg_parse_peer_orgs: error parsing peer_organization %d.", ii+1);
1566       return TR_CFG_NOPARSE;
1567     }
1568   }
1569
1570   return TR_CFG_SUCCESS;
1571 }
1572              
1573 static TR_CFG_RC tr_cfg_parse_default_servers (TR_CFG *trc, json_t *jcfg) 
1574 {
1575   json_t *jdss = NULL;
1576   TR_CFG_RC rc = TR_CFG_SUCCESS;
1577   TR_AAA_SERVER *ds = NULL;
1578   int i = 0;
1579
1580   if ((!trc) || (!jcfg))
1581     return TR_CFG_BAD_PARAMS;
1582
1583   /* If there are default servers, store them */
1584   if ((NULL != (jdss = json_object_get(jcfg, "default_servers"))) &&
1585       (json_is_array(jdss)) &&
1586       (0 < json_array_size(jdss))) {
1587
1588     for (i = 0; i < json_array_size(jdss); i++) {
1589       if (NULL == (ds = tr_cfg_parse_one_aaa_server(trc,
1590                                                     json_array_get(jdss, i), 
1591                                                    &rc))) {
1592         return rc;
1593       }
1594       tr_debug("tr_cfg_parse_default_servers: Default server configured: %s", ds->hostname->buf);
1595       ds->next = trc->default_servers;
1596       trc->default_servers = ds;
1597     }
1598   } 
1599
1600   tr_debug("tr_cfg_parse_default_servers: Finished (rc=%d)", rc);
1601   return rc;
1602 }
1603
1604 static TR_IDP_REALM *tr_cfg_parse_comm_idps (TR_CFG *trc, json_t *jidps, TR_CFG_RC *rc)
1605 {
1606   TR_IDP_REALM *idp = NULL;
1607   TR_IDP_REALM *found_idp = NULL;
1608   TR_IDP_REALM *temp_idp = NULL;
1609   int i = 0;
1610
1611   if ((!trc) ||
1612       (!jidps) ||
1613       (!json_is_array(jidps))) {
1614     if (rc)
1615       *rc = TR_CFG_BAD_PARAMS;
1616     return NULL;
1617   }
1618
1619   for (i = 0; i < json_array_size(jidps); i++) {
1620     if (NULL == (temp_idp = talloc(trc, TR_IDP_REALM))) {
1621       tr_debug("tr_cfg_parse_comm_idps: Can't allocate memory for IdP Realm.");
1622       if (rc)
1623         *rc = TR_CFG_NOMEM;
1624       return NULL;
1625     }
1626     memset (temp_idp, 0, sizeof(TR_IDP_REALM));
1627     
1628     if (NULL == (found_idp = (tr_cfg_find_idp(trc, 
1629                                               tr_new_name((char *)json_string_value(json_array_get(jidps, i))), 
1630                                               rc)))) {
1631       tr_debug("tr_cfg_parse_comm_idps: Unknown IDP %s.", 
1632                (char *)json_string_value(json_array_get(jidps, i)));
1633       return NULL;
1634     }
1635
1636     // We *MUST* do a dereferenced copy here or the second community will corrupt the linked list we create here.
1637     *temp_idp = *found_idp;
1638
1639     temp_idp->comm_next = idp;
1640     idp = temp_idp;
1641   }
1642
1643   return idp;
1644 }
1645
1646 static TR_RP_REALM *tr_cfg_parse_comm_rps (TR_CFG *trc, json_t *jrps, TR_CFG_RC *rc)
1647 {
1648   TR_RP_REALM *rp = NULL;
1649   TR_RP_REALM *temp_rp = NULL;
1650   int i = 0;
1651
1652   if ((!trc) ||
1653       (!jrps) ||
1654       (!json_is_array(jrps))) {
1655     if (rc)
1656       *rc = TR_CFG_BAD_PARAMS;
1657     return NULL;
1658   }
1659
1660   for (i = (json_array_size(jrps)-1); i >= 0; i--) {
1661     if (NULL == (temp_rp = talloc_zero(trc, TR_RP_REALM))) {
1662       tr_debug("tr_cfg_parse_comm_rps: Can't allocate memory for RP Realm.");
1663       if (rc)
1664         *rc = TR_CFG_NOMEM;
1665       return NULL;
1666     }
1667
1668     if (NULL == (temp_rp->realm_name = tr_new_name((char *)json_string_value(json_array_get(jrps, i))))) {
1669       tr_debug("tr_cfg_parse_comm_rps: No memory for RP Realm Name.");
1670       if (rc)
1671         *rc = TR_CFG_NOMEM;
1672       return NULL;
1673     }
1674
1675     temp_rp->next = rp;
1676     rp = temp_rp;
1677   }
1678
1679   return rp;
1680 }
1681
1682 static TR_COMM *tr_cfg_parse_one_comm (TR_CFG *trc, json_t *jcomm, TR_CFG_RC *rc) {
1683   TR_COMM *comm = NULL;
1684   json_t *jid = NULL;
1685   json_t *jtype = NULL;
1686   json_t *japcs = NULL;
1687   json_t *jidps = NULL;
1688   json_t *jrps = NULL;
1689
1690   if ((!trc) || (!jcomm) || (!rc)) {
1691     tr_debug("tr_cfg_parse_one_comm: Bad parameters.");
1692     if (rc)
1693       *rc = TR_CFG_BAD_PARAMS;
1694     return NULL;
1695   }
1696
1697   if (NULL == (comm = talloc_zero(trc, TR_COMM))) {
1698     tr_crit("tr_cfg_parse_one_comm: Out of memory.");
1699     *rc = TR_CFG_NOMEM;
1700     return NULL;
1701   }
1702
1703
1704   if ((NULL == (jid = json_object_get(jcomm, "community_id"))) ||
1705       (!json_is_string(jid)) ||
1706       (NULL == (jtype = json_object_get(jcomm, "type"))) ||
1707       (!json_is_string(jtype)) ||
1708       (NULL == (japcs = json_object_get(jcomm, "apcs"))) ||
1709       (!json_is_array(japcs)) ||
1710       (NULL == (jidps = json_object_get(jcomm, "idp_realms"))) ||
1711       (!json_is_array(jidps)) ||
1712       (NULL == (jrps = json_object_get(jcomm, "rp_realms"))) ||
1713       (!json_is_array(jrps))) {
1714     tr_debug("tr_cfg_parse_one_comm: Error parsing Communities configuration.");
1715     *rc = TR_CFG_NOPARSE;
1716     return NULL;
1717   }
1718
1719   if (NULL == (comm->id = tr_new_name((char *)json_string_value(jid)))) {
1720     tr_debug("tr_cfg_parse_one_comm: No memory for community id.");
1721     *rc = TR_CFG_NOMEM;
1722     return NULL;
1723   }
1724
1725   if (0 == strcmp(json_string_value(jtype), "apc")) {
1726     comm->type = TR_COMM_APC;
1727   } else if (0 == strcmp(json_string_value(jtype), "coi")) {
1728     comm->type = TR_COMM_COI;
1729     if (NULL == (comm->apcs = tr_cfg_parse_apcs(trc, japcs, rc))) {
1730       tr_debug("tr_cfg_parse_one_comm: Can't parse APCs for COI %s.", comm->id->buf);
1731       tr_free_name(comm->id);
1732       return NULL;
1733     }
1734   } else {
1735     tr_debug("tr_cfg_parse_one_comm: Invalid community type, comm = %s, type = %s", comm->id->buf, json_string_value(jtype));
1736     tr_free_name(comm->id);
1737     *rc = TR_CFG_NOPARSE;
1738     return NULL;
1739   }
1740
1741   comm->idp_realms = tr_cfg_parse_comm_idps(trc, jidps, rc);
1742   if (TR_CFG_SUCCESS != *rc) {
1743     tr_debug("tr_cfg_parse_one_comm: Can't parse IDP realms for comm %s.", comm->id->buf);
1744     tr_free_name(comm->id);
1745     return NULL;
1746   }
1747
1748   comm->rp_realms = tr_cfg_parse_comm_rps(trc, jrps, rc);
1749   if (TR_CFG_SUCCESS != *rc) {
1750     tr_debug("tr_cfg_parse_comm: Can't parse RP realms for comm %s .", comm->id->buf);
1751     tr_free_name(comm->id);
1752     return NULL;
1753   }
1754
1755   if (TR_COMM_APC == comm->type) {
1756     json_t *jexpire  = json_object_get(jcomm, "expiration_interval");
1757     comm->expiration_interval = 43200; /*30 days*/
1758     if (jexpire) {
1759         if (!json_is_integer(jexpire)) {
1760           fprintf(stderr, "tr_parse_comm: expirae_interval is not an integer\n");
1761           return NULL;
1762         }
1763         comm->expiration_interval = json_integer_value(jexpire);
1764         if (comm->expiration_interval <= 10)
1765           comm->expiration_interval = 11; /* Freeradius waits 10 minutes between successful TR queries*/
1766         if (comm->expiration_interval > 129600) /* 90 days*/
1767         comm->expiration_interval = 129600;
1768     }
1769   }
1770   
1771   return comm;
1772 }
1773
1774 static TR_CFG_RC tr_cfg_parse_comms (TR_CFG *trc, json_t *jcfg) 
1775 {
1776   json_t *jcomms = NULL;
1777   TR_CFG_RC rc = TR_CFG_SUCCESS;
1778   TR_COMM *comm = NULL;
1779   int i = 0;
1780
1781   if ((!trc) || (!jcfg)) {
1782     tr_debug("tr_cfg_parse_comms: Bad Parameters.");
1783     return TR_CFG_BAD_PARAMS;
1784   }
1785
1786   if (NULL != (jcomms = json_object_get(jcfg, "communities"))) {
1787     if (!json_is_array(jcomms)) {
1788       return TR_CFG_NOPARSE;
1789     }
1790
1791     for (i = 0; i < json_array_size(jcomms); i++) {
1792       if (NULL == (comm = tr_cfg_parse_one_comm(trc, 
1793                                                 json_array_get(jcomms, i), 
1794                                                 &rc))) {
1795         return rc;
1796       }
1797       tr_debug("tr_cfg_parse_comms: Community configured: %s.", comm->id->buf);
1798       comm->next = trc->comms;
1799       trc->comms = comm;
1800     }
1801   }
1802   tr_debug("tr_cfg_parse_comms: Finished (rc=%d)", rc);
1803   return rc;
1804 }
1805
1806 TR_CFG_RC tr_cfg_validate(TR_CFG *trc)
1807 {
1808   TR_CFG_RC rc = TR_CFG_SUCCESS;
1809
1810   if (!trc)
1811     return TR_CFG_BAD_PARAMS;
1812
1813   if ((NULL == trc->internal)||
1814       (NULL == trc->internal->hostname)) {
1815     tr_debug("tr_cfg_validate: Error: No internal configuration, or no hostname.");
1816     rc = TR_CFG_ERROR;
1817   }
1818
1819   if (NULL == trc->rp_clients) {
1820     tr_debug("tr_cfg_validate: Error: No RP Clients configured");
1821     rc = TR_CFG_ERROR;
1822   }
1823
1824   if (NULL == trc->comms) {
1825     tr_debug("tr_cfg_validate: Error: No Communities configured");
1826     rc = TR_CFG_ERROR;
1827   }
1828
1829   if ((NULL == trc->default_servers) && (NULL == trc->idp_realms)) {
1830     tr_debug("tr_cfg_validate: Error: No default servers or IDPs configured.");
1831     rc = TR_CFG_ERROR;
1832   }
1833   
1834   return rc;
1835 }
1836
1837 /* Join two paths and return a pointer to the result. This should be freed
1838  * via talloc_free. Returns NULL on failure. */
1839 static char *join_paths(TALLOC_CTX *mem_ctx, const char *p1, const char *p2)
1840 {
1841   return talloc_asprintf(mem_ctx, "%s/%s", p1, p2); /* returns NULL on a failure */
1842 }
1843
1844 static void tr_cfg_log_json_error(const char *label, json_error_t *rc)
1845 {
1846   tr_debug("%s: JSON parse error on line %d: %s",
1847            label,
1848            rc->line,
1849            rc->text);
1850 }
1851
1852 TR_CFG_RC tr_cfg_parse_one_config_file(TR_CFG *cfg, const char *file_with_path)
1853 {
1854   json_t *jcfg=NULL;
1855   json_t *jser=NULL;
1856   json_error_t rc;
1857
1858   if (NULL==(jcfg=json_load_file(file_with_path, 
1859                                  JSON_DISABLE_EOF_CHECK, &rc))) {
1860     tr_debug("tr_cfg_parse_one_config_file: Error parsing config file %s.", 
1861              file_with_path);
1862     tr_cfg_log_json_error("tr_cfg_parse_one_config_file", &rc);
1863     return TR_CFG_NOPARSE;
1864   }
1865
1866   // Look for serial number and log it if it exists
1867   if (NULL!=(jser=json_object_get(jcfg, "serial_number"))) {
1868     if (json_is_number(jser)) {
1869       tr_notice("tr_parse_one_config_file: Attempting to load revision %" JSON_INTEGER_FORMAT " of '%s'.",
1870                 json_integer_value(jser),
1871                 file_with_path);
1872     }
1873   }
1874
1875   if ((TR_CFG_SUCCESS != tr_cfg_parse_internal(cfg, jcfg)) ||
1876       (TR_CFG_SUCCESS != tr_cfg_parse_local_orgs(cfg, jcfg)) ||
1877       (TR_CFG_SUCCESS != tr_cfg_parse_peer_orgs(cfg, jcfg)) ||
1878       (TR_CFG_SUCCESS != tr_cfg_parse_default_servers(cfg, jcfg)) ||
1879       (TR_CFG_SUCCESS != tr_cfg_parse_comms(cfg, jcfg)))
1880     return TR_CFG_ERROR;
1881
1882   return TR_CFG_SUCCESS;
1883 }
1884
1885 /* Reads configuration files in config_dir ("" or "./" will use the current directory). */
1886 TR_CFG_RC tr_parse_config(TR_CFG_MGR *cfg_mgr, const char *config_dir, int n, struct dirent **cfg_files)
1887 {
1888   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1889   char *file_with_path;
1890   int ii;
1891   TR_CFG_RC cfg_rc=TR_CFG_ERROR;
1892
1893   if ((!cfg_mgr) || (!cfg_files) || (n<=0)) {
1894     cfg_rc=TR_CFG_BAD_PARAMS;
1895     goto cleanup;
1896   }
1897
1898   if (cfg_mgr->new != NULL)
1899     tr_cfg_free(cfg_mgr->new);
1900   cfg_mgr->new=tr_cfg_new(tmp_ctx); /* belongs to the temporary context for now */
1901   if (cfg_mgr->new == NULL) {
1902     cfg_rc=TR_CFG_NOMEM;
1903     goto cleanup;
1904   }
1905
1906   cfg_mgr->new->peers=trp_ptable_new(cfg_mgr);
1907
1908   /* Parse configuration information from each config file */
1909   for (ii=0; ii<n; ii++) {
1910     file_with_path=join_paths(tmp_ctx, config_dir, cfg_files[ii]->d_name); /* must free result with talloc_free */
1911     if(file_with_path == NULL) {
1912       tr_crit("tr_parse_config: error joining path.");
1913       cfg_rc=TR_CFG_NOMEM;
1914       goto cleanup;
1915     }
1916     tr_debug("tr_parse_config: Parsing %s.", cfg_files[ii]->d_name); /* print the filename without the path */
1917     cfg_rc=tr_cfg_parse_one_config_file(cfg_mgr->new, file_with_path);
1918     if (cfg_rc!=TR_CFG_SUCCESS) {
1919       tr_crit("tr_parse_config: Error parsing %s", file_with_path);
1920       goto cleanup;
1921     }
1922     talloc_free(file_with_path); /* done with filename */
1923   }
1924
1925   /* make sure we got a complete, consistent configuration */
1926   if (TR_CFG_SUCCESS != tr_cfg_validate(cfg_mgr->new)) {
1927     tr_err("tr_parse_config: Error: INVALID CONFIGURATION");
1928     cfg_rc=TR_CFG_ERROR;
1929     goto cleanup;
1930   }
1931
1932   /* success! */
1933   talloc_steal(cfg_mgr, cfg_mgr->new); /* hand this over to the cfg_mgr context */
1934   cfg_rc=TR_CFG_SUCCESS;
1935
1936 cleanup:
1937   talloc_free(tmp_ctx);
1938   return cfg_rc;
1939 }
1940
1941 TR_IDP_REALM *tr_cfg_find_idp (TR_CFG *tr_cfg, TR_NAME *idp_id, TR_CFG_RC *rc)
1942 {
1943
1944   TR_IDP_REALM *cfg_idp;
1945
1946   if ((!tr_cfg) || (!idp_id)) {
1947     if (rc)
1948       *rc = TR_CFG_BAD_PARAMS;
1949     return NULL;
1950   }
1951
1952   for (cfg_idp = tr_cfg->idp_realms; NULL != cfg_idp; cfg_idp = cfg_idp->next) {
1953     if (!tr_name_cmp (idp_id, cfg_idp->realm_id)) {
1954       tr_debug("tr_cfg_find_idp: Found %s.", idp_id->buf);
1955       return cfg_idp;
1956     }
1957   }
1958   /* if we didn't find one, return NULL */ 
1959   return NULL;
1960 }
1961
1962 TR_RP_CLIENT *tr_cfg_find_rp (TR_CFG *tr_cfg, TR_NAME *rp_gss, TR_CFG_RC *rc)
1963 {
1964   TR_RP_CLIENT *cfg_rp;
1965
1966   if ((!tr_cfg) || (!rp_gss)) {
1967     if (rc)
1968       *rc = TR_CFG_BAD_PARAMS;
1969     return NULL;
1970   }
1971
1972   for (cfg_rp = tr_cfg->rp_clients; NULL != cfg_rp; cfg_rp = cfg_rp->next) {
1973     if (tr_gss_names_matches(cfg_rp->gss_names, rp_gss)) {
1974       tr_debug("tr_cfg_find_rp: Found %s.", rp_gss->buf);
1975       return cfg_rp;
1976     }
1977   }
1978   /* if we didn't find one, return NULL */ 
1979   return NULL;
1980 }
1981
1982 static int is_cfg_file(const struct dirent *dent) {
1983   int n;
1984
1985   /* Only accept filenames ending in ".cfg" and starting with a character
1986    * other than an ASCII '.' */
1987
1988   /* filename must be at least 4 characters long to be acceptable */
1989   n=strlen(dent->d_name);
1990   if (n < 4) {
1991     return 0;
1992   }
1993
1994   /* filename must not start with '.' */
1995   if ('.' == dent->d_name[0]) {
1996     return 0;
1997   }
1998
1999   /* If the above passed and the last four characters of the filename are .cfg, accept.
2000    * (n.b., assumes an earlier test checked that the name is >= 4 chars long.) */
2001   if (0 == strcmp(&(dent->d_name[n-4]), ".cfg")) {
2002     return 1;
2003   }
2004
2005   /* otherwise, return false. */
2006   return 0;
2007 }
2008
2009 /* Find configuration files in a particular directory. Returns the
2010  * number of entries found, 0 if none are found, or <0 for some
2011  * errors. If n>=0, the cfg_files parameter will contain a newly
2012  * allocated array of pointers to struct dirent entries, as returned
2013  * by scandir(). These can be freed with tr_free_config_file_list().
2014  */
2015 int tr_find_config_files (const char *config_dir, struct dirent ***cfg_files) {
2016   int n = 0;
2017   
2018   n = scandir(config_dir, cfg_files, is_cfg_file, alphasort);
2019
2020   if (n < 0) {
2021     perror("scandir");
2022     tr_debug("tr_find_config: scandir error trying to scan %s.", config_dir);
2023   } 
2024
2025   return n;
2026 }
2027
2028 /* Free memory allocated for configuration file list returned from tr_find_config_files().
2029  * This can be called regardless of the return value of tr_find_config_values(). */
2030 void tr_free_config_file_list(int n, struct dirent ***cfg_files) {
2031   int ii;
2032
2033   /* if n < 0, then scandir did not allocate anything because it failed */
2034   if((n>=0) && (*cfg_files != NULL)) {
2035     for(ii=0; ii<n; ii++) {
2036       free((*cfg_files)[ii]);
2037     }
2038     free(*cfg_files); /* safe even if n==0 */
2039     *cfg_files=NULL; /* this will help prevent accidentally freeing twice */
2040   }
2041 }