Full support for rp_permitted filters using new filter structures, etc.
[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
40 #include <tr_config.h>
41 #include <tr.h>
42 #include <tr_filter.h>
43
44 void tr_print_config (FILE *stream, TR_CFG *cfg) {
45   fprintf(stream, "tr_print_config: Not yet implemented.\n");
46   return;
47 }
48
49 void tr_cfg_free (TR_CFG *cfg) {
50   /* TBD -- need to deallocate memory as part of dynamic config */
51   return;
52 }
53
54 TR_CFG_RC tr_apply_new_config (TR_INSTANCE *tr) {
55
56   if (!tr)
57     return TR_CFG_BAD_PARAMS;
58
59   if (tr->active_cfg)
60     tr_cfg_free(tr->active_cfg);
61
62   tr->active_cfg = tr->new_cfg;
63   return TR_CFG_SUCCESS;
64 }
65
66 static TR_CFG_RC tr_cfg_parse_internal (TR_INSTANCE *tr, json_t *jcfg) {
67   json_t *jint = NULL;
68   json_t *jmtd = NULL;
69
70   if ((!tr) || (!tr->new_cfg) || (!jcfg))
71     return TR_CFG_BAD_PARAMS;
72
73   if (NULL == (tr->new_cfg->internal = malloc(sizeof(TR_CFG_INTERNAL))))
74     return TR_CFG_NOMEM;
75
76   memset(tr->new_cfg->internal, 0, sizeof(TR_CFG_INTERNAL));
77
78   if ((NULL != (jint = json_object_get(jcfg, "tr_internal"))) &&
79       (NULL != (jmtd = json_object_get(jint, "max_tree_depth")))) {
80     if (json_is_number(jmtd)) {
81       tr->new_cfg->internal->max_tree_depth = json_integer_value(jmtd);
82     } else {
83       fprintf(stderr,"tr_cfg_parse_internal: Parsing error, max_tree_depth is not a number.\n");
84       return TR_CFG_NOPARSE;
85     }
86   } else {
87     /* If not configured, use the default */
88     tr->new_cfg->internal->max_tree_depth = TR_DEFAULT_MAX_TREE_DEPTH;
89   }
90   fprintf(stderr, "tr_cfg_parse_internal: Internal config parsed.\n");
91   return TR_CFG_SUCCESS;
92 }
93
94 static TR_FILTER *tr_cfg_parse_one_filter (TR_INSTANCE *tr, json_t *jfilt, TR_CFG_RC *rc)
95 {
96   TR_FILTER *filt = NULL;
97   json_t *jftype = NULL;
98   json_t *jfls = NULL;
99   json_t *jfaction = NULL;
100   json_t *jfspecs = NULL;
101   json_t *jffield = NULL;
102   json_t *jfmatch = NULL;
103   int i = 0, j = 0;
104
105   if ((NULL == (jftype = json_object_get(jfilt, "type"))) ||
106       (!json_is_string(jftype))) {
107     fprintf(stderr, "tr_cfg_parse_one_filter: Error parsing filter type.\n");
108     *rc = TR_CFG_NOPARSE;
109     return NULL;
110   }
111
112   if ((NULL == (jfls = json_object_get(jfilt, "filter_lines"))) ||
113       (!json_is_array(jfls))) {
114     fprintf(stderr, "tr_cfg_parse_one_filter: Error parsing filter type.\n");
115     *rc = TR_CFG_NOPARSE;
116     return NULL;
117   }
118
119   if (TR_MAX_FILTER_LINES < json_array_size(jfls)) {
120     fprintf(stderr, "tr_cfg_parse_one_filter: Filter has too many filter_lines, maximimum of %d.\n", TR_MAX_FILTER_LINES);
121     *rc = TR_CFG_NOPARSE;
122     return NULL;
123   }
124
125   /* TBD -- optionally parse realm and domain constraints */
126
127   if (NULL == (filt = malloc(sizeof(TR_FILTER)))) {
128     fprintf(stderr, "tr_config_parse_one_filter: Out of memory.\n");
129     *rc = TR_CFG_NOMEM;
130     return NULL;
131   }
132
133   memset(filt, 0, sizeof(TR_FILTER));
134
135   if (!strcmp(json_string_value(jftype), "rp_permitted")) {
136     filt->type = TR_FILTER_TYPE_RP_PERMITTED;
137   }
138   else {
139     fprintf(stderr, "tr_cfg_parse_one_filter: Error parsing filter type, unknown type '%s'.\n", json_string_value(jftype));
140     *rc = TR_CFG_NOPARSE;
141     tr_filter_free(filt);
142     return NULL;
143   }
144   
145   /* For each filter line... */
146   for (i = 0; i < json_array_size(jfls); i++) {
147
148     if ((NULL == (jfaction = json_object_get(json_array_get(jfls, i), "action"))) ||
149         (!json_is_string(jfaction))) {
150       fprintf(stderr, "tr_cfg_parse_one_filter: Error parsing filter action.\n");
151       *rc = TR_CFG_NOPARSE;
152       tr_filter_free(filt);
153       return NULL;
154     }
155  
156     /* TBD -- parse constraints */
157
158     if ((NULL == (jfspecs = json_object_get(json_array_get(jfls, i), "filter_specs"))) ||
159         (!json_is_array(jfspecs)) ||
160         (0 == json_array_size(jfspecs))) {
161       fprintf(stderr, "tr_cfg_parse_one_filter: Error parsing filter specs.\n");
162       *rc = TR_CFG_NOPARSE;
163       tr_filter_free(filt);
164       return NULL;
165     }
166   
167     if (TR_MAX_FILTER_SPECS < json_array_size(jfspecs)) {
168       fprintf(stderr, "tr_cfg_parse_one_filter: Filter has too many filter_specs, maximimum of %d.\n", TR_MAX_FILTER_SPECS);
169       *rc = TR_CFG_NOPARSE;
170       tr_filter_free(filt);
171       return NULL;
172     }
173
174     if (NULL == (filt->lines[i] = malloc(sizeof(TR_FLINE)))) {
175       fprintf(stderr, "tr_config_parse_one_filter: Out of memory.\n");
176       *rc = TR_CFG_NOMEM;
177       tr_filter_free(filt);
178       return NULL;
179     }
180
181     memset(filt->lines[i], 0, sizeof(TR_FLINE));
182
183     if (!strcmp(json_string_value(jfaction), "accept")) {
184         filt->lines[i]->action = TR_FILTER_ACTION_ACCEPT;
185     }
186     else if (!strcmp(json_string_value(jfaction), "reject")) {
187       filt->lines[i]->action = TR_FILTER_ACTION_REJECT;
188     }
189     else {
190       fprintf(stderr, "tr_cfg_parse_one_filter: Error parsing filter action, unknown action' %s'.\n", json_string_value(jfaction));
191       *rc = TR_CFG_NOPARSE;
192       tr_filter_free(filt);
193       return NULL;
194     }
195       
196     /*For each filter spec within the filter line... */
197     for (j = 0; j <json_array_size(jfspecs); j++) {
198       
199       if ((NULL == (jffield = json_object_get(json_array_get(jfspecs, j), "field"))) ||
200           (!json_is_string(jffield)) ||
201           (NULL == (jfmatch = json_object_get(json_array_get(jfspecs, j), "match"))) ||
202           (!json_is_string(jfmatch))) {
203         fprintf (stderr, "tr_cfg_parse_one_filter: Error parsing filter field and match for filter spec %d, filter line %d.\n", i, j);
204         *rc = TR_CFG_NOPARSE;
205         tr_filter_free(filt);
206         return NULL;
207       }
208
209       if (NULL == (filt->lines[i]->specs[j] = malloc(sizeof(TR_FSPEC)))) {
210         fprintf(stderr, "tr_config_parse_one_filter: Out of memory.\n");
211         *rc = TR_CFG_NOMEM;
212         tr_filter_free(filt);
213         return NULL;
214       }
215
216       memset(filt->lines[i]->specs[j], 0, sizeof(TR_FSPEC));
217     
218       if ((NULL == (filt->lines[i]->specs[j]->field = tr_new_name((char *)json_string_value(jffield)))) ||
219           (NULL == (filt->lines[i]->specs[j]->match = tr_new_name((char *)json_string_value(jfmatch))))) {
220         fprintf(stderr, "tr_config_parse_one_filter: Out of memory.\n");
221         *rc = TR_CFG_NOMEM;
222         tr_filter_free(filt);
223         return NULL;
224       }
225     }
226   }
227   return filt;
228 }
229
230 static TR_RP_CLIENT *tr_cfg_parse_one_rp_client (TR_INSTANCE *tr, json_t *jrp, TR_CFG_RC *rc)
231 {
232   TR_RP_CLIENT *rp = NULL;
233   json_t *jgns = NULL;
234   json_t *jfilt = NULL;
235   json_t *jftype = NULL;
236   int i = 0;
237
238   if ((!jrp) || (!rc)) {
239     fprintf(stderr, "tr_cfg_parse_one_rp_realm: Bad parameters.\n");
240     if (rc)
241       *rc = TR_CFG_BAD_PARAMS;
242     return NULL;
243   }
244
245   if ((NULL == (jgns = json_object_get(jrp, "gss_names"))) ||
246       (!json_is_array(jgns))) {
247     fprintf(stderr, "tr_cfg_parse_one_rp_client: Error parsing RP client configuration, no GSS names.\n");
248     *rc = TR_CFG_NOPARSE;
249     return NULL;
250   }
251
252   /* TBD -- Support more than one filter per RP client? */
253   if (NULL == (jfilt = json_object_get(jrp, "filter"))) {
254     fprintf(stderr, "tr_cfg_parse_one_rp_client: Error parsing RP client configuration, no filter.\n");
255     *rc = TR_CFG_NOPARSE;
256     return NULL;
257   }
258
259   /* We only support rp_permitted filters for RP clients */
260   if ((NULL == (jftype = json_object_get(jfilt, "type"))) ||
261       (!json_is_string(jftype)) ||
262       (strcmp(json_string_value(jftype), "rp_permitted"))) {
263     fprintf(stderr, "tr_cfg_parse_one_rp_client: Error parsing RP client filter type.\n");
264     *rc = TR_CFG_NOPARSE;
265     return NULL;
266   }
267
268   if (TR_MAX_GSS_NAMES < json_array_size(jgns)) {
269     fprintf(stderr, "tr_cfg_parse_one_rp_client: RP Client has too many GSS Names.\n");
270     *rc = TR_CFG_NOPARSE;
271     return NULL;
272   }
273
274   if (NULL == (rp = malloc(sizeof(TR_RP_CLIENT)))) {
275     fprintf(stderr, "tr_config_parse_one_rp_realm: Out of memory.\n");
276     *rc = TR_CFG_NOMEM;
277     return NULL;
278   }
279   
280   memset(rp, 0, sizeof(TR_RP_CLIENT));
281
282   /* TBD -- support more than one filter entry per RP Client? */
283   if (NULL == (rp->filter = tr_cfg_parse_one_filter(tr, jfilt, rc))) {
284     fprintf(stderr, "tr_cfg_parse_one_rp_client: Error parsing filter.\n");
285     free(rp);
286     *rc = TR_CFG_NOPARSE;
287     return NULL;
288   }
289     
290   for (i = 0; i < json_array_size(jgns); i++) {
291     if (NULL == (rp->gss_names[i] = tr_new_name ((char *)json_string_value(json_array_get(jgns, i))))) {
292       fprintf(stderr, "tr_cfg_parse_one_rp_client: No memory for GSS Name.\n");
293       free(rp);
294       *rc = TR_CFG_NOMEM;
295       return NULL;
296     }
297   }
298   
299   return rp;
300 }
301
302 static TR_CFG_RC tr_cfg_parse_rp_clients (TR_INSTANCE *tr, json_t *jcfg) {
303   json_t *jrps = NULL;
304   TR_RP_CLIENT *rp = NULL;
305   TR_CFG_RC rc = TR_CFG_SUCCESS;
306   int i = 0;
307
308   if ((!tr) || (!tr->new_cfg) || (!jcfg))
309     return TR_CFG_BAD_PARAMS;
310
311   if ((NULL == (jrps = json_object_get(jcfg, "rp_clients"))) ||
312       (!json_is_array(jrps))) {
313     return TR_CFG_NOPARSE;
314   }
315
316   for (i = 0; i < json_array_size(jrps); i++) {
317     if (NULL == (rp = tr_cfg_parse_one_rp_client(tr, 
318                                                  json_array_get(jrps, i), 
319                                                  &rc))) {
320        return rc;
321     }
322     fprintf(stderr, "tr_cfg_parse_rp_clients: RP client configured -- first gss: %s", rp->gss_names[0]->buf);
323     rp->next = tr->new_cfg->rp_clients;
324     tr->new_cfg->rp_clients = rp;
325   }
326   return rc;
327 }
328
329 static TR_AAA_SERVER *tr_cfg_parse_one_aaa_server (TR_INSTANCE *tr, json_t *jaddr, TR_CFG_RC *rc) {
330   TR_AAA_SERVER *aaa = NULL;
331
332   if ((!tr) || (!tr->new_cfg) || (!jaddr) || (!json_is_string(jaddr))) {
333     fprintf(stderr, "tr_cfg_parse_one_aaa_server: Bad parameters.\n");
334     *rc = TR_CFG_BAD_PARAMS;
335     return NULL;
336   }
337
338   if (NULL == (aaa = malloc(sizeof(TR_AAA_SERVER)))) {
339     fprintf(stderr, "tr_config_parse_one_aaa_server: Out of memory.\n");
340     *rc = TR_CFG_NOMEM;
341     return NULL;
342   }
343
344   memset(aaa, 0, sizeof(TR_AAA_SERVER));
345
346   /* TBD -- Handle IPv6 addresses */
347   inet_aton(json_string_value(jaddr), &(aaa->aaa_server_addr));
348
349   return aaa;
350 }
351
352
353 static TR_AAA_SERVER *tr_cfg_parse_aaa_servers (TR_INSTANCE *tr, json_t *jaaas, TR_CFG_RC *rc) 
354 {
355   TR_AAA_SERVER *aaa = NULL;
356   TR_AAA_SERVER *temp_aaa = NULL;
357   int i = 0;
358
359   for (i = 0; i < json_array_size(jaaas); i++) {
360     if (NULL == (temp_aaa = tr_cfg_parse_one_aaa_server(tr, json_array_get(jaaas, i), rc))) {
361       return NULL;
362     }
363     /* TBD -- IPv6 addresses */
364     //    fprintf(stderr, "tr_cfg_parse_aaa_servers: Configuring AAA Server: ip_addr = %s.\n", inet_ntoa(temp_aaa->aaa_server_addr));
365     temp_aaa->next = aaa;
366     aaa = temp_aaa;
367   }
368   return aaa;
369 }
370
371 static TR_APC *tr_cfg_parse_apcs (TR_INSTANCE *tr, json_t *japcs, TR_CFG_RC *rc)
372 {
373   TR_APC *apc;
374
375   *rc = TR_CFG_SUCCESS;         /* presume success */
376
377   if ((!japcs) || (!rc)) {
378     fprintf(stderr, "tr_cfg_parse_apcs: Bad parameters.\n");
379     if (rc) 
380       *rc = TR_CFG_BAD_PARAMS;
381     return NULL;
382   }
383
384   if (NULL == (apc = malloc(sizeof(TR_APC)))) {
385     fprintf (stderr, "tr_cfg_parse_apcs: Out of memory.\n");
386     *rc = TR_CFG_NOMEM;
387     return NULL;
388   }
389
390   memset(apc, 0, sizeof(TR_APC));
391
392   /* TBD, deal with more than one APC.  In the meantime, though...                */
393   /* Only parse the first APC, because we only know how to deal with one, anyway. */
394   if (0 == json_array_size(japcs))
395     return NULL;
396
397   if (NULL == (apc->id = tr_new_name((char *)json_string_value(json_array_get(japcs, 0))))) {
398     fprintf(stderr, "tr_cfg_parse_apcs: No memory for APC name.\n");
399     *rc = TR_CFG_NOMEM;
400     return NULL;
401   }
402
403   return apc;
404 }
405
406 static TR_IDP_REALM *tr_cfg_parse_one_idp_realm (TR_INSTANCE *tr, json_t *jidp, TR_CFG_RC *rc) {
407   TR_IDP_REALM *idp = NULL;
408   json_t *jrid = NULL;
409   json_t *jscfg = NULL;
410   json_t *jsrvrs = NULL;
411   json_t *japcs = NULL;
412
413   if ((!jidp) || (!rc)) {
414     fprintf(stderr, "tr_cfg_parse_one_idp_realm: Bad parameters.\n");
415     if (rc)
416       *rc = TR_CFG_BAD_PARAMS;
417     return NULL;
418   }
419
420   if (NULL == (idp = malloc(sizeof(TR_IDP_REALM)))) {
421     fprintf(stderr, "tr_config_parse_one_idp_realm: Out of memory.\n");
422     *rc = TR_CFG_NOMEM;
423     return NULL;
424   }
425
426   memset(idp, 0, sizeof(TR_IDP_REALM));
427
428   if ((NULL == (jrid = json_object_get(jidp, "realm_id"))) ||
429       (!json_is_string(jrid)) ||
430       (NULL == (jscfg = json_object_get(jidp, "shared_config"))) ||
431       (!json_is_string(jscfg)) ||
432       (NULL == (jsrvrs = json_object_get(jidp, "aaa_servers"))) ||
433       (!json_is_array(jsrvrs))) {
434     fprintf(stderr, "tr_cfg_parse_one_idp_realm: Error parsing IDP realm configuration.\n");
435     free(idp);
436     *rc = TR_CFG_NOPARSE;
437     return NULL;
438   }
439
440   if (0 == strcmp(json_string_value(jscfg), "no")) {
441     idp->shared_config = 0;
442   } else {
443     idp->shared_config = 1;
444   }
445
446   if (NULL == (idp->realm_id = tr_new_name((char *)json_string_value(jrid)))) {
447     free(idp);
448     fprintf(stderr, "tr_cfg_parse_one_idp_realm: No memory for realm id.\n");
449     *rc = TR_CFG_NOMEM;
450     return NULL;
451   }
452
453   if (NULL == (idp->aaa_servers = tr_cfg_parse_aaa_servers(tr, jsrvrs, rc))) {
454     fprintf(stderr, "tr_cfg_parse_one_idp_realm: Can't parse AAA servers for realm %s.\n", idp->realm_id->buf);
455     tr_free_name(idp->realm_id);
456     free(idp);
457     return NULL;
458   }
459
460   if ((NULL != (japcs = json_object_get(jidp, "apcs"))) &&
461       (json_is_array(japcs))) {
462     if (NULL == (idp->apcs = tr_cfg_parse_apcs(tr, japcs, rc))) {
463       fprintf(stderr, "tr_cfg_parse_one_idp_realm: Can't parse APCs for realm %s .\n", idp->realm_id->buf);
464       tr_free_name(idp->realm_id);
465       /* TBD -- free aaa_servers */;
466       free(idp);
467       return NULL;
468     }
469   } 
470   return idp;
471 }
472
473 static TR_CFG_RC tr_cfg_parse_idp_realms (TR_INSTANCE *tr, json_t *jcfg) 
474 {
475   json_t *jidps = NULL;
476   TR_CFG_RC rc = TR_CFG_SUCCESS;
477   TR_IDP_REALM *idp = NULL;
478   int i = 0;
479
480   if ((!tr) || (!tr->new_cfg) || (!jcfg))
481     return TR_CFG_BAD_PARAMS;
482
483   if ((NULL == (jidps = json_object_get(jcfg, "idp_realms"))) ||
484       (!json_is_array(jidps))) {
485     return TR_CFG_NOPARSE;
486   }
487
488   for (i = 0; i < json_array_size(jidps); i++) {
489     if (NULL == (idp = tr_cfg_parse_one_idp_realm(tr, 
490                                                   json_array_get(jidps, i), 
491                                                   &rc))) {
492        return rc;
493     }
494     fprintf(stderr, "tr_cfg_parse_idp_realms: IDP realm configured: %s.\n", idp->realm_id->buf);
495     idp->next = tr->new_cfg->idp_realms;
496     tr->new_cfg->idp_realms = idp;
497   }
498   return rc;
499 }
500
501 static TR_IDP_REALM *tr_cfg_parse_comm_idps (TR_INSTANCE *tr, json_t *jidps, TR_CFG_RC *rc)
502 {
503   TR_IDP_REALM *idp = NULL;
504   TR_IDP_REALM *temp_idp = NULL;
505   int i = 0;
506
507   if ((!tr) ||
508       (!jidps) ||
509       (!json_is_array(jidps))) {
510     if (rc)
511       *rc = TR_CFG_BAD_PARAMS;
512     return NULL;
513   }
514
515   for (i = 0; i < json_array_size(jidps); i++) {
516     if (NULL == (temp_idp = (tr_cfg_find_idp(tr->new_cfg, 
517                                              tr_new_name((char *)json_string_value(json_array_get(jidps, i))), 
518                                              rc)))) {
519       fprintf(stderr, "tr_cfg_parse_comm_idps: Unknown IDP %s.\n", 
520               (char *)json_string_value(json_array_get(jidps, i)));
521       return NULL;
522     }
523
524     temp_idp->comm_next = idp;
525     idp = temp_idp;
526   }
527
528   return idp;
529 }
530
531 static TR_RP_REALM *tr_cfg_parse_comm_rps (TR_INSTANCE *tr, json_t *jrps, TR_CFG_RC *rc)
532 {
533   TR_RP_REALM *rp = NULL;
534   TR_RP_REALM *temp_rp = NULL;
535   int i = 0;
536
537   if ((!tr) ||
538       (!jrps) ||
539       (!json_is_array(jrps))) {
540     if (rc)
541       *rc = TR_CFG_BAD_PARAMS;
542     return NULL;
543   }
544
545   for (i = (json_array_size(jrps)-1); i >= 0; i--) {
546     if (NULL == (temp_rp = malloc(sizeof(TR_RP_REALM)))) {
547       fprintf(stderr, "tr_cfg_parse_comm_rps: Can't allocate memory for RP Realm.\n");
548       if (rc)
549         *rc = TR_CFG_NOMEM;
550       return NULL;
551     }
552     memset (temp_rp, 0, sizeof(TR_RP_REALM));
553
554     if (NULL == (temp_rp->realm_name = tr_new_name((char *)json_string_value(json_array_get(jrps, i))))) {
555       fprintf(stderr, "tr_cfg_parse_comm_rps: No memory for RP Realm Name.\n");
556       if (rc)
557         *rc = TR_CFG_NOMEM;
558       return NULL;
559     }
560
561     temp_rp->next = rp;
562     rp = temp_rp;
563   }
564
565   return rp;
566 }
567
568 static TR_COMM *tr_cfg_parse_one_comm (TR_INSTANCE *tr, json_t *jcomm, TR_CFG_RC *rc) {
569   TR_COMM *comm = NULL;
570   json_t *jid = NULL;
571   json_t *jtype = NULL;
572   json_t *japcs = NULL;
573   json_t *jidps = NULL;
574   json_t *jrps = NULL;
575
576   if ((!jcomm) || (!rc)) {
577     fprintf(stderr, "tr_cfg_parse_one_comm: Bad parameters.\n");
578     if (rc)
579       *rc = TR_CFG_BAD_PARAMS;
580     return NULL;
581   }
582
583   if (NULL == (comm = malloc(sizeof(TR_COMM)))) {
584     fprintf(stderr, "tr_config_parse_one_comm: Out of memory.\n");
585     *rc = TR_CFG_NOMEM;
586     return NULL;
587   }
588
589   memset(comm, 0, sizeof(TR_COMM));
590
591   if ((NULL == (jid = json_object_get(jcomm, "community_id"))) ||
592       (!json_is_string(jid)) ||
593       (NULL == (jtype = json_object_get(jcomm, "type"))) ||
594       (!json_is_string(jtype)) ||
595       (NULL == (japcs = json_object_get(jcomm, "apcs"))) ||
596       (!json_is_array(japcs)) ||
597       (NULL == (jidps = json_object_get(jcomm, "idp_realms"))) ||
598       (!json_is_array(jidps)) ||
599       (NULL == (jrps = json_object_get(jcomm, "rp_realms"))) ||
600       (!json_is_array(jrps))) {
601     fprintf(stderr, "tr_cfg_parse_one_comm: Error parsing Communities configuration.\n");
602     free(comm);
603     *rc = TR_CFG_NOPARSE;
604     return NULL;
605   }
606
607   if (NULL == (comm->id = tr_new_name((char *)json_string_value(jid)))) {
608     free(comm);
609     fprintf(stderr, "tr_cfg_parse_one_comm: No memory for community id.\n");
610     *rc = TR_CFG_NOMEM;
611     return NULL;
612   }
613
614   if (0 == strcmp(json_string_value(jtype), "apc")) {
615     comm->type = TR_COMM_APC;
616   } else if (0 == strcmp(json_string_value(jtype), "coi")) {
617     comm->type = TR_COMM_COI;
618     if (NULL == (comm->apcs = tr_cfg_parse_apcs(tr, japcs, rc))) {
619       fprintf(stderr, "tr_cfg_parse_one_comm: Can't parse APCs for COI %s.\n", comm->id->buf);
620       tr_free_name(comm->id);
621       free(comm);
622       return NULL;
623     }
624   } else {
625     fprintf(stderr, "tr_cfg_parse_one_comm: Invalid community type, comm = %s, type = %s\n", comm->id->buf, json_string_value(jtype));
626     tr_free_name(comm->id);
627     free(comm);
628     *rc = TR_CFG_NOPARSE;
629     return NULL;
630   }
631
632   comm->idp_realms = tr_cfg_parse_comm_idps(tr, jidps, rc);
633   if (TR_CFG_SUCCESS != *rc) {
634     fprintf(stderr, "tr_cfg_parse_one_comm: Can't parse IDP realms for comm %s.\n", comm->id->buf);
635     tr_free_name(comm->id);
636     free(comm);
637     return NULL;
638   }
639
640   comm->rp_realms = tr_cfg_parse_comm_rps(tr, jrps, rc);
641   if (TR_CFG_SUCCESS != *rc) {
642     fprintf(stderr, "tr_cfg_parse_comm: Can't parse RP realms for comm %s .\n", comm->id->buf);
643     tr_free_name(comm->id);
644     /* TBD -- free idps? */;
645     free(comm);
646     return NULL;
647   }
648
649   return comm;
650 }
651
652 static TR_CFG_RC tr_cfg_parse_comms (TR_INSTANCE *tr, json_t *jcfg) 
653 {
654   json_t *jcomms = NULL;
655   TR_CFG_RC rc = TR_CFG_SUCCESS;
656   TR_COMM *comm = NULL;
657   int i = 0;
658
659   if ((!tr) || (!tr->new_cfg) || (!jcfg)) {
660     fprintf(stderr, "tr_config_parse_comms: Bad Parameters.\n");
661     return TR_CFG_BAD_PARAMS;
662   }
663
664   if (NULL == (comm = malloc(sizeof(TR_COMM)))) {
665     fprintf(stderr, "tr_cfg_parse_comms: Out of Memory\n");
666     return TR_CFG_NOMEM;
667   }
668   memset (comm, 0, sizeof(TR_COMM));
669
670   if ((NULL == (jcomms = json_object_get(jcfg, "communities"))) ||
671       (!json_is_array(jcomms))) {
672     return TR_CFG_NOPARSE;
673   }
674
675   for (i = 0; i < json_array_size(jcomms); i++) {
676     if (NULL == (comm = tr_cfg_parse_one_comm(tr, 
677                                               json_array_get(jcomms, i), 
678                                               &rc))) {
679        return rc;
680     }
681     fprintf(stderr, "tr_cfg_parse_comms: Community configured: %s.\n", comm->id->buf);
682     comm->next = tr->new_cfg->comms;
683     tr->new_cfg->comms = comm;
684   }
685   return rc;
686 }
687
688 TR_CFG_RC tr_parse_config (TR_INSTANCE *tr, json_t *jcfg) {
689
690   /* If there is a partial/abandoned config lying around, free it */
691   if (tr->new_cfg) {
692     tr_cfg_free(tr->new_cfg);
693   }
694   
695   if (NULL == (tr->new_cfg = malloc(sizeof(TR_CFG))))
696     return TR_CFG_NOMEM;
697
698   memset(tr->new_cfg, 0, sizeof(TR_CFG));
699
700   if ((TR_CFG_SUCCESS != tr_cfg_parse_internal(tr, jcfg)) ||
701       (TR_CFG_SUCCESS != tr_cfg_parse_rp_clients(tr, jcfg)) ||
702       (TR_CFG_SUCCESS != tr_cfg_parse_idp_realms(tr, jcfg)) ||
703       (TR_CFG_SUCCESS != tr_cfg_parse_comms(tr, jcfg))) {
704     tr_cfg_free(tr->new_cfg);
705     return TR_CFG_ERROR;
706   }
707   return TR_CFG_SUCCESS;
708 }
709
710 TR_IDP_REALM *tr_cfg_find_idp (TR_CFG *tr_cfg, TR_NAME *idp_id, TR_CFG_RC *rc)
711 {
712
713   TR_IDP_REALM *cfg_idp;
714
715   if ((!tr_cfg) || (!idp_id)) {
716     if (rc)
717       *rc = TR_CFG_BAD_PARAMS;
718     return NULL;
719   }
720
721   for (cfg_idp = tr_cfg->idp_realms; NULL != cfg_idp; cfg_idp = cfg_idp->next) {
722     if (!tr_name_cmp (idp_id, cfg_idp->realm_id)) {
723       fprintf(stderr, "tr_cfg_find_idp: Found %s.\n", idp_id->buf);
724       return cfg_idp;
725     }
726   }
727   /* if we didn't find one, return NULL */ 
728   return NULL;
729 }
730
731 TR_RP_CLIENT *tr_cfg_find_rp (TR_CFG *tr_cfg, TR_NAME *rp_gss, TR_CFG_RC *rc)
732 {
733   TR_RP_CLIENT *cfg_rp;
734   int i;
735
736   if ((!tr_cfg) || (!rp_gss)) {
737     if (rc)
738       *rc = TR_CFG_BAD_PARAMS;
739     return NULL;
740   }
741
742   for (cfg_rp = tr_cfg->rp_clients; NULL != cfg_rp; cfg_rp = cfg_rp->next) {
743     for (i = 0; i < TR_MAX_GSS_NAMES; i++) {
744       if (!tr_name_cmp (rp_gss, cfg_rp->gss_names[i])) {
745         fprintf(stderr, "tr_cfg_find_rp: Found %s.\n", rp_gss->buf);
746         return cfg_rp;
747       }
748     }
749   }
750   /* if we didn't find one, return NULL */ 
751   return NULL;
752 }
753
754 json_t *tr_read_config (int n, struct dirent **cfg_files) {
755   json_t *jcfg = NULL;
756   json_t *temp = NULL;
757   json_error_t err;
758
759   if (!cfg_files)
760     return NULL;
761
762   while (n--) {
763     fprintf(stderr, "tr_read_config: Parsing %s.\n", cfg_files[n]->d_name);
764     if (NULL == (temp = json_load_file(cfg_files[n]->d_name, JSON_DISABLE_EOF_CHECK, &err))) {
765       fprintf (stderr, "tr_read_config: Error parsing config file %s.\n", cfg_files[n]->d_name);
766       return NULL;
767     }
768
769     /* TBD -- instead of using json_object_update, iterate through files for non-overlap config? */
770     if (!jcfg) {
771       jcfg = temp;
772     }else {
773       if (-1 == json_object_update(jcfg, temp)) {
774         fprintf(stderr, "tr_read_config: Error merging config information.\n");
775         return NULL;
776       }
777     }
778   }
779
780   //  fprintf(stderr, "tr_read_config: Merged configuration complete:\n%s\n", json_dumps(jcfg, 0));
781
782   return jcfg;
783 }
784
785 static int is_cfg_file(const struct dirent *dent) {
786   int n;
787
788   /* if the last four letters of the filename are .cfg, return true. */
789   if ((4 <= (n = strlen(dent->d_name))) &&
790       (0 == strcmp(&(dent->d_name[n-4]), ".cfg"))) {
791     return 1;
792   }
793
794   /* otherwise, return false. */
795   return 0;
796 }
797
798 int tr_find_config_files (struct dirent ***cfg_files) {
799   int n = 0, i = 0;
800   
801   n = scandir(".", cfg_files, &is_cfg_file, 0);
802
803   if (n < 0) {
804     perror("scandir");
805     fprintf(stderr, "tr_find_config: scandir error.\n");
806     return 0;
807   }
808
809   if (n == 0) {
810     fprintf (stderr, "tr_find_config: No config files found.\n");
811     return 0;
812   }
813
814   i = n;
815   while(i--) {
816     fprintf(stderr, "tr_find_config: Config file found (%s).\n", (*cfg_files)[i]->d_name);
817   }
818     
819   return n;
820 }