2 * Copyright (c) 2012, JANET(UK)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
40 #include <tr_config.h>
42 #include <tr_filter.h>
44 void tr_print_config (FILE *stream, TR_CFG *cfg) {
45 fprintf(stream, "tr_print_config: Not yet implemented.\n");
49 void tr_cfg_free (TR_CFG *cfg) {
50 /* TBD -- need to deallocate memory as part of dynamic config */
54 TR_CFG_RC tr_apply_new_config (TR_INSTANCE *tr) {
57 return TR_CFG_BAD_PARAMS;
60 tr_cfg_free(tr->active_cfg);
62 tr->active_cfg = tr->new_cfg;
63 return TR_CFG_SUCCESS;
66 static TR_CFG_RC tr_cfg_parse_internal (TR_INSTANCE *tr, json_t *jcfg) {
70 if ((!tr) || (!tr->new_cfg) || (!jcfg))
71 return TR_CFG_BAD_PARAMS;
73 if (NULL == (tr->new_cfg->internal = malloc(sizeof(TR_CFG_INTERNAL))))
76 memset(tr->new_cfg->internal, 0, sizeof(TR_CFG_INTERNAL));
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);
83 fprintf(stderr,"tr_cfg_parse_internal: Parsing error, max_tree_depth is not a number.\n");
84 return TR_CFG_NOPARSE;
87 /* If not configured, use the default */
88 tr->new_cfg->internal->max_tree_depth = TR_DEFAULT_MAX_TREE_DEPTH;
90 fprintf(stderr, "tr_cfg_parse_internal: Internal config parsed.\n");
91 return TR_CFG_SUCCESS;
94 static TR_FILTER *tr_cfg_parse_one_filter (TR_INSTANCE *tr, json_t *jfilt, TR_CFG_RC *rc)
96 TR_FILTER *filt = NULL;
97 json_t *jftype = NULL;
99 json_t *jfaction = NULL;
100 json_t *jfspecs = NULL;
101 json_t *jffield = NULL;
102 json_t *jfmatch = NULL;
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;
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;
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;
125 /* TBD -- optionally parse realm and domain constraints */
127 if (NULL == (filt = malloc(sizeof(TR_FILTER)))) {
128 fprintf(stderr, "tr_config_parse_one_filter: Out of memory.\n");
133 memset(filt, 0, sizeof(TR_FILTER));
135 if (!strcmp(json_string_value(jftype), "rp_permitted")) {
136 filt->type = TR_FILTER_TYPE_RP_PERMITTED;
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);
145 /* For each filter line... */
146 for (i = 0; i < json_array_size(jfls); i++) {
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);
156 /* TBD -- parse constraints */
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);
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);
174 if (NULL == (filt->lines[i] = malloc(sizeof(TR_FLINE)))) {
175 fprintf(stderr, "tr_config_parse_one_filter: Out of memory.\n");
177 tr_filter_free(filt);
181 memset(filt->lines[i], 0, sizeof(TR_FLINE));
183 if (!strcmp(json_string_value(jfaction), "accept")) {
184 filt->lines[i]->action = TR_FILTER_ACTION_ACCEPT;
186 else if (!strcmp(json_string_value(jfaction), "reject")) {
187 filt->lines[i]->action = TR_FILTER_ACTION_REJECT;
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);
196 /*For each filter spec within the filter line... */
197 for (j = 0; j <json_array_size(jfspecs); j++) {
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);
209 if (NULL == (filt->lines[i]->specs[j] = malloc(sizeof(TR_FSPEC)))) {
210 fprintf(stderr, "tr_config_parse_one_filter: Out of memory.\n");
212 tr_filter_free(filt);
216 memset(filt->lines[i]->specs[j], 0, sizeof(TR_FSPEC));
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");
222 tr_filter_free(filt);
230 static TR_RP_CLIENT *tr_cfg_parse_one_rp_client (TR_INSTANCE *tr, json_t *jrp, TR_CFG_RC *rc)
232 TR_RP_CLIENT *rp = NULL;
234 json_t *jfilt = NULL;
235 json_t *jftype = NULL;
238 if ((!jrp) || (!rc)) {
239 fprintf(stderr, "tr_cfg_parse_one_rp_realm: Bad parameters.\n");
241 *rc = TR_CFG_BAD_PARAMS;
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;
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;
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;
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;
274 if (NULL == (rp = malloc(sizeof(TR_RP_CLIENT)))) {
275 fprintf(stderr, "tr_config_parse_one_rp_realm: Out of memory.\n");
280 memset(rp, 0, sizeof(TR_RP_CLIENT));
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");
286 *rc = TR_CFG_NOPARSE;
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");
302 static TR_CFG_RC tr_cfg_parse_rp_clients (TR_INSTANCE *tr, json_t *jcfg) {
304 TR_RP_CLIENT *rp = NULL;
305 TR_CFG_RC rc = TR_CFG_SUCCESS;
308 if ((!tr) || (!tr->new_cfg) || (!jcfg))
309 return TR_CFG_BAD_PARAMS;
311 if ((NULL == (jrps = json_object_get(jcfg, "rp_clients"))) ||
312 (!json_is_array(jrps))) {
313 return TR_CFG_NOPARSE;
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),
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;
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;
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;
338 if (NULL == (aaa = malloc(sizeof(TR_AAA_SERVER)))) {
339 fprintf(stderr, "tr_config_parse_one_aaa_server: Out of memory.\n");
344 memset(aaa, 0, sizeof(TR_AAA_SERVER));
346 /* TBD -- Handle IPv6 addresses */
347 inet_aton(json_string_value(jaddr), &(aaa->aaa_server_addr));
353 static TR_AAA_SERVER *tr_cfg_parse_aaa_servers (TR_INSTANCE *tr, json_t *jaaas, TR_CFG_RC *rc)
355 TR_AAA_SERVER *aaa = NULL;
356 TR_AAA_SERVER *temp_aaa = NULL;
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))) {
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;
371 static TR_APC *tr_cfg_parse_apcs (TR_INSTANCE *tr, json_t *japcs, TR_CFG_RC *rc)
375 *rc = TR_CFG_SUCCESS; /* presume success */
377 if ((!japcs) || (!rc)) {
378 fprintf(stderr, "tr_cfg_parse_apcs: Bad parameters.\n");
380 *rc = TR_CFG_BAD_PARAMS;
384 if (NULL == (apc = malloc(sizeof(TR_APC)))) {
385 fprintf (stderr, "tr_cfg_parse_apcs: Out of memory.\n");
390 memset(apc, 0, sizeof(TR_APC));
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))
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");
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;
409 json_t *jscfg = NULL;
410 json_t *jsrvrs = NULL;
411 json_t *japcs = NULL;
413 if ((!jidp) || (!rc)) {
414 fprintf(stderr, "tr_cfg_parse_one_idp_realm: Bad parameters.\n");
416 *rc = TR_CFG_BAD_PARAMS;
420 if (NULL == (idp = malloc(sizeof(TR_IDP_REALM)))) {
421 fprintf(stderr, "tr_config_parse_one_idp_realm: Out of memory.\n");
426 memset(idp, 0, sizeof(TR_IDP_REALM));
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");
436 *rc = TR_CFG_NOPARSE;
440 if (0 == strcmp(json_string_value(jscfg), "no")) {
441 idp->shared_config = 0;
443 idp->shared_config = 1;
446 if (NULL == (idp->realm_id = tr_new_name((char *)json_string_value(jrid)))) {
448 fprintf(stderr, "tr_cfg_parse_one_idp_realm: No memory for realm id.\n");
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);
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 */;
473 static TR_CFG_RC tr_cfg_parse_idp_realms (TR_INSTANCE *tr, json_t *jcfg)
475 json_t *jidps = NULL;
476 TR_CFG_RC rc = TR_CFG_SUCCESS;
477 TR_IDP_REALM *idp = NULL;
480 if ((!tr) || (!tr->new_cfg) || (!jcfg))
481 return TR_CFG_BAD_PARAMS;
483 if ((NULL == (jidps = json_object_get(jcfg, "idp_realms"))) ||
484 (!json_is_array(jidps))) {
485 return TR_CFG_NOPARSE;
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),
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;
501 static TR_IDP_REALM *tr_cfg_parse_comm_idps (TR_INSTANCE *tr, json_t *jidps, TR_CFG_RC *rc)
503 TR_IDP_REALM *idp = NULL;
504 TR_IDP_REALM *temp_idp = NULL;
509 (!json_is_array(jidps))) {
511 *rc = TR_CFG_BAD_PARAMS;
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))),
519 fprintf(stderr, "tr_cfg_parse_comm_idps: Unknown IDP %s.\n",
520 (char *)json_string_value(json_array_get(jidps, i)));
524 temp_idp->comm_next = idp;
531 static TR_RP_REALM *tr_cfg_parse_comm_rps (TR_INSTANCE *tr, json_t *jrps, TR_CFG_RC *rc)
533 TR_RP_REALM *rp = NULL;
534 TR_RP_REALM *temp_rp = NULL;
539 (!json_is_array(jrps))) {
541 *rc = TR_CFG_BAD_PARAMS;
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");
552 memset (temp_rp, 0, sizeof(TR_RP_REALM));
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");
568 static TR_COMM *tr_cfg_parse_one_comm (TR_INSTANCE *tr, json_t *jcomm, TR_CFG_RC *rc) {
569 TR_COMM *comm = NULL;
571 json_t *jtype = NULL;
572 json_t *japcs = NULL;
573 json_t *jidps = NULL;
576 if ((!jcomm) || (!rc)) {
577 fprintf(stderr, "tr_cfg_parse_one_comm: Bad parameters.\n");
579 *rc = TR_CFG_BAD_PARAMS;
583 if (NULL == (comm = malloc(sizeof(TR_COMM)))) {
584 fprintf(stderr, "tr_config_parse_one_comm: Out of memory.\n");
589 memset(comm, 0, sizeof(TR_COMM));
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");
603 *rc = TR_CFG_NOPARSE;
607 if (NULL == (comm->id = tr_new_name((char *)json_string_value(jid)))) {
609 fprintf(stderr, "tr_cfg_parse_one_comm: No memory for community id.\n");
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);
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);
628 *rc = TR_CFG_NOPARSE;
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);
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? */;
652 static TR_CFG_RC tr_cfg_parse_comms (TR_INSTANCE *tr, json_t *jcfg)
654 json_t *jcomms = NULL;
655 TR_CFG_RC rc = TR_CFG_SUCCESS;
656 TR_COMM *comm = NULL;
659 if ((!tr) || (!tr->new_cfg) || (!jcfg)) {
660 fprintf(stderr, "tr_config_parse_comms: Bad Parameters.\n");
661 return TR_CFG_BAD_PARAMS;
664 if (NULL == (comm = malloc(sizeof(TR_COMM)))) {
665 fprintf(stderr, "tr_cfg_parse_comms: Out of Memory\n");
668 memset (comm, 0, sizeof(TR_COMM));
670 if ((NULL == (jcomms = json_object_get(jcfg, "communities"))) ||
671 (!json_is_array(jcomms))) {
672 return TR_CFG_NOPARSE;
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),
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;
688 TR_CFG_RC tr_parse_config (TR_INSTANCE *tr, json_t *jcfg) {
690 /* If there is a partial/abandoned config lying around, free it */
692 tr_cfg_free(tr->new_cfg);
695 if (NULL == (tr->new_cfg = malloc(sizeof(TR_CFG))))
698 memset(tr->new_cfg, 0, sizeof(TR_CFG));
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);
707 return TR_CFG_SUCCESS;
710 TR_IDP_REALM *tr_cfg_find_idp (TR_CFG *tr_cfg, TR_NAME *idp_id, TR_CFG_RC *rc)
713 TR_IDP_REALM *cfg_idp;
715 if ((!tr_cfg) || (!idp_id)) {
717 *rc = TR_CFG_BAD_PARAMS;
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);
727 /* if we didn't find one, return NULL */
731 TR_RP_CLIENT *tr_cfg_find_rp (TR_CFG *tr_cfg, TR_NAME *rp_gss, TR_CFG_RC *rc)
733 TR_RP_CLIENT *cfg_rp;
736 if ((!tr_cfg) || (!rp_gss)) {
738 *rc = TR_CFG_BAD_PARAMS;
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);
750 /* if we didn't find one, return NULL */
754 json_t *tr_read_config (int n, struct dirent **cfg_files) {
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);
769 /* TBD -- instead of using json_object_update, iterate through files for non-overlap config? */
773 if (-1 == json_object_update(jcfg, temp)) {
774 fprintf(stderr, "tr_read_config: Error merging config information.\n");
780 // fprintf(stderr, "tr_read_config: Merged configuration complete:\n%s\n", json_dumps(jcfg, 0));
785 static int is_cfg_file(const struct dirent *dent) {
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"))) {
794 /* otherwise, return false. */
798 int tr_find_config_files (struct dirent ***cfg_files) {
801 n = scandir(".", cfg_files, &is_cfg_file, 0);
805 fprintf(stderr, "tr_find_config: scandir error.\n");
810 fprintf (stderr, "tr_find_config: No config files found.\n");
816 fprintf(stderr, "tr_find_config: Config file found (%s).\n", (*cfg_files)[i]->d_name);