3e79778cda1ed04aa84bcf3fbaf0a8310c629b45
[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_find_idp (TR_INSTANCE *tr, TR_NAME *idp_id, TR_CFG_RC *rc)
346 {
347
348   TR_IDP_REALM *cfg_idp;
349
350   if ((!tr) || (!idp_id)) {
351     if (rc)
352       *rc = TR_CFG_BAD_PARAMS;
353     return NULL;
354   }
355
356   for (cfg_idp = tr->active_cfg->idp_realms; NULL != cfg_idp; cfg_idp = cfg_idp->next) {
357     if (!tr_name_cmp (idp_id, cfg_idp->realm_id)) {
358       fprintf(stderr, "tr_cfg_find_idp: Found %s.\n", idp_id->buf);
359       return cfg_idp;
360     }
361   }
362   /* if we didn't find one, return NULL */ 
363   return NULL;
364 }
365
366 static TR_IDP_REALM *tr_cfg_parse_comm_idps (TR_INSTANCE *tr, json_t *jidps, TR_CFG_RC *rc)
367 {
368   TR_IDP_REALM *idp = NULL;
369   TR_IDP_REALM *temp_idp = NULL;
370   int i = 0;
371
372   if ((!tr) ||
373       (!jidps) ||
374       (!json_is_array(jidps))) {
375     if (rc)
376       *rc = TR_CFG_BAD_PARAMS;
377     return NULL;
378   }
379
380   for (i = 0; i < json_array_size(jidps); i++) {
381     if (NULL == (temp_idp = (tr_cfg_find_idp(tr, 
382                                              tr_new_name((char *)json_string_value(json_array_get(jidps, i))), 
383                                              rc)))) {
384       fprintf(stderr, "tr_cfg_parse_comm_idps: Unknown IDP %s.\n", 
385               (char *)json_string_value(json_array_get(jidps, i)));
386       return NULL;
387     }
388
389     temp_idp->comm_next = idp;
390     idp = temp_idp;
391   }
392
393   return idp;
394 }
395
396 static TR_RP_REALM *tr_cfg_parse_comm_rps (TR_INSTANCE *tr, json_t *jrps, TR_CFG_RC *rc)
397 {
398   TR_RP_REALM *rp = NULL;
399   TR_RP_REALM *temp_rp = NULL;
400   int i = 0;
401
402   if ((!tr) ||
403       (!jrps) ||
404       (!json_is_array(jrps))) {
405     if (rc)
406       *rc = TR_CFG_BAD_PARAMS;
407     return NULL;
408   }
409
410   for (i = (json_array_size(jrps)-1); i >= 0; i++) {
411     if (NULL == (temp_rp = malloc(sizeof(TR_RP_REALM)))) {
412       fprintf(stderr, "tr_cfg_parse_comm_rps: Can't allocate memory for RP Realm.\n");
413       if (rc)
414         *rc = TR_CFG_NOMEM;
415       return NULL;
416     }
417     memset (temp_rp, 0, sizeof(TR_RP_REALM));
418
419     if (NULL == (temp_rp->realm_name = tr_new_name((char *)json_string_value(json_array_get(jrps, i))))) {
420       fprintf(stderr, "tr_cfg_parse_comm_rps: No memory for RP Realm Name.\n");
421       if (rc)
422         *rc = TR_CFG_NOMEM;
423       return NULL;
424     }
425
426     temp_rp->next = rp;
427     rp = temp_rp;
428   }
429
430   return rp;
431 }
432
433 static TR_COMM *tr_cfg_parse_one_comm (TR_INSTANCE *tr, json_t *jcomm, TR_CFG_RC *rc) {
434   TR_COMM *comm = NULL;
435   json_t *jid = NULL;
436   json_t *jtype = NULL;
437   json_t *japcs = NULL;
438   json_t *jidps = NULL;
439   json_t *jrps = NULL;
440
441   if ((!jcomm) || (!rc)) {
442     fprintf(stderr, "tr_cfg_parse_one_comm: Bad parameters.\n");
443     if (rc)
444       *rc = TR_CFG_BAD_PARAMS;
445     return NULL;
446   }
447
448   if (NULL == (comm = malloc(sizeof(TR_COMM)))) {
449     fprintf(stderr, "tr_config_parse_one_comm: Out of memory.\n");
450     *rc = TR_CFG_NOMEM;
451     return NULL;
452   }
453
454   memset(comm, 0, sizeof(TR_COMM));
455
456   if ((NULL == (jid = json_object_get(jcomm, "community_id"))) ||
457       (!json_is_string(jid)) ||
458       (NULL == (jtype = json_object_get(jcomm, "type"))) ||
459       (!json_is_string(jtype)) ||
460       (NULL == (japcs = json_object_get(jcomm, "apcs"))) ||
461       (!json_is_array(japcs)) ||
462       (NULL == (jidps = json_object_get(jcomm, "idp_realms"))) ||
463       (!json_is_array(jidps)) ||
464       (NULL == (jrps = json_object_get(jcomm, "rp_realms"))) ||
465       (!json_is_array(jrps))) {
466     fprintf(stderr, "tr_cfg_parse_one_comm: Error parsing Communities configuration.\n");
467     free(comm);
468     *rc = TR_CFG_NOPARSE;
469     return NULL;
470   }
471
472   if (NULL == (comm->id = tr_new_name((char *)json_string_value(jid)))) {
473     free(comm);
474     fprintf(stderr, "tr_cfg_parse_one_comm: No memory for community id.\n");
475     *rc = TR_CFG_NOMEM;
476     return NULL;
477   }
478
479   if (0 == strcmp(json_string_value(jtype), "apc")) {
480     comm->type = TR_COMM_APC;
481   } else if (0 == strcmp(json_string_value(jtype), "coi")) {
482     comm->type = TR_COMM_COI;
483     if (NULL == (comm->apcs = tr_cfg_parse_apcs(tr, japcs, rc))) {
484       fprintf(stderr, "tr_cfg_parse_one_comm: Can't parse APCs for COI %s.\n", comm->id->buf);
485       tr_free_name(comm->id);
486       free(comm);
487       return NULL;
488     }
489   } else {
490     fprintf(stderr, "tr_cfg_parse_one_comm: Invalid community type, comm = %s, type = %s\n", comm->id->buf, json_string_value(jtype));
491     tr_free_name(comm->id);
492     free(comm);
493     *rc = TR_CFG_NOPARSE;
494     return NULL;
495   }
496
497   comm->idp_realms = tr_cfg_parse_comm_idps(tr, jidps, rc);
498   if (TR_CFG_SUCCESS != *rc) {
499     fprintf(stderr, "tr_cfg_parse_one_comm: Can't parse IDP realms for comm %s.\n", comm->id->buf);
500     tr_free_name(comm->id);
501     free(comm);
502     return NULL;
503   }
504
505   comm->rp_realms = tr_cfg_parse_comm_rps(tr, jrps, rc);
506   if (TR_CFG_SUCCESS != *rc) {
507     fprintf(stderr, "tr_cfg_parse_comm: Can't parse RP realms for comm %s .\n", comm->id->buf);
508     tr_free_name(comm->id);
509     /* TBD -- free idps? */;
510     free(comm);
511     return NULL;
512   }
513
514   return comm;
515 }
516
517 static TR_CFG_RC tr_cfg_parse_comms (TR_INSTANCE *tr, json_t *jcfg) 
518 {
519   json_t *jcomms = NULL;
520   TR_CFG_RC rc = TR_CFG_SUCCESS;
521   TR_COMM *comm = NULL;
522   int i = 0;
523
524   if ((!tr) || (!tr->new_cfg) || (!jcfg)) {
525     fprintf(stderr, "tr_config_parse_comms: Bad Parameters.\n");
526     return TR_CFG_BAD_PARAMS;
527   }
528
529   if (NULL == (comm = malloc(sizeof(TR_COMM)))) {
530     fprintf(stderr, "tr_cfg_parse_comms: Out of Memory\n");
531     return TR_CFG_NOMEM;
532   }
533   memset (comm, 0, sizeof(TR_COMM));
534
535   if ((NULL == (jcomms = json_object_get(jcfg, "communities"))) ||
536       (!json_is_array(jcomms))) {
537     return TR_CFG_NOPARSE;
538   }
539
540   for (i = 0; i < json_array_size(jcomms); i++) {
541     if (NULL == (comm = tr_cfg_parse_one_comm(tr, 
542                                               json_array_get(jcomms, i), 
543                                               &rc))) {
544        return rc;
545     }
546     fprintf(stderr, "tr_cfg_parse_comms: Community configured: %s.\n", comm->id->buf);
547     comm->next = tr->new_cfg->comms;
548     tr->new_cfg->comms = comm;
549   }
550   return rc;
551 }
552
553 TR_CFG_RC tr_parse_config (TR_INSTANCE *tr, json_t *jcfg) {
554
555   /* If there is a partial/abandoned config lying around, free it */
556   if (tr->new_cfg) {
557     tr_cfg_free(tr->new_cfg);
558   }
559   
560   if (NULL == (tr->new_cfg = malloc(sizeof(TR_CFG))))
561     return TR_CFG_NOMEM;
562
563   memset(tr->new_cfg, 0, sizeof(TR_CFG));
564
565   if ((TR_CFG_SUCCESS != tr_cfg_parse_internal(tr, jcfg)) ||
566       (TR_CFG_SUCCESS != tr_cfg_parse_rp_clients(tr, jcfg)) ||
567       (TR_CFG_SUCCESS != tr_cfg_parse_idp_realms(tr, jcfg)) ||
568       (TR_CFG_SUCCESS != tr_cfg_parse_comms(tr, jcfg))) {
569     tr_cfg_free(tr->new_cfg);
570     return TR_CFG_ERROR;
571   }
572   return TR_CFG_SUCCESS;
573 }
574
575 TR_RP_CLIENT *tr_cfg_find_rp (TR_INSTANCE *tr, TR_NAME *rp_gss, TR_CFG_RC *rc)
576 {
577   TR_RP_CLIENT *cfg_rp;
578   int i;
579
580   if ((!tr) || (!rp_gss)) {
581     if (rc)
582       *rc = TR_CFG_BAD_PARAMS;
583     return NULL;
584   }
585
586   for (cfg_rp = tr->active_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 json_t *tr_read_config (int n, struct dirent **cfg_files) {
599   json_t *jcfg = NULL;
600   json_t *temp = NULL;
601   json_error_t err;
602
603   if (!cfg_files)
604     return NULL;
605
606   while (n--) {
607     fprintf(stderr, "tr_read_config: Parsing %s.\n", cfg_files[n]->d_name);
608     if (NULL == (temp = json_load_file(cfg_files[n]->d_name, JSON_DISABLE_EOF_CHECK, &err))) {
609       fprintf (stderr, "tr_read_config: Error parsing config file %s.\n", cfg_files[n]->d_name);
610       return NULL;
611     }
612
613     if (!jcfg) {
614       jcfg = temp;
615     }else {
616       if (-1 == json_object_update(jcfg, temp)) {
617         fprintf(stderr, "tr_read_config: Error merging config information.\n");
618         return NULL;
619       }
620     }
621   }
622
623   //  fprintf(stderr, "tr_read_config: Merged configuration complete:\n%s\n", json_dumps(jcfg, 0));
624
625   return jcfg;
626 }
627
628 static int is_cfg_file(const struct dirent *dent) {
629   int n;
630
631   /* if the last four letters of the filename are .cfg, return true. */
632   if ((4 <= (n = strlen(dent->d_name))) &&
633       (0 == strcmp(&(dent->d_name[n-4]), ".cfg"))) {
634     return 1;
635   }
636
637   /* otherwise, return false. */
638   return 0;
639 }
640
641 int tr_find_config_files (struct dirent ***cfg_files) {
642   int n = 0, i = 0;
643   
644   n = scandir(".", cfg_files, &is_cfg_file, 0);
645
646   if (n < 0) {
647     perror("scandir");
648     fprintf(stderr, "tr_find_config: scandir error.\n");
649     return 0;
650   }
651
652   if (n == 0) {
653     fprintf (stderr, "tr_find_config: No config files found.\n");
654     return 0;
655   }
656
657   i = n;
658   while(i--) {
659     fprintf(stderr, "tr_find_config: Config file found (%s).\n", (*cfg_files)[i]->d_name);
660   }
661     
662   return n;
663 }