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