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 if (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 if (NULL != (jrname = json_object_get(jint, "realm_name"))) {
91 if (json_is_string(jrname)) {
92 tr->new_cfg->internal->realm_name = json_integer_value(jrname);
94 fprintf(stderr,"tr_cfg_parse_internal: Parsing error, realm_name is not a string.\n");
95 return TR_CFG_NOPARSE;
99 fprintf(stderr, "tr_cfg_parse_internal: Parsing error, realm_name is not found.\n");
100 return TR_CFG_NOPARSE;
102 fprintf(stderr, "tr_cfg_parse_internal: Internal config parsed.\n");
103 return TR_CFG_SUCCESS;
106 fprintf(stderr, "tr_cfg_parse_internal: Parsing error, tr_internal configuration section not found.\n");
107 return TR_CFG_NOPARSE;
110 static TR_FILTER *tr_cfg_parse_one_filter (TR_INSTANCE *tr, json_t *jfilt, TR_CFG_RC *rc)
112 TR_FILTER *filt = NULL;
113 json_t *jftype = NULL;
115 json_t *jfaction = NULL;
116 json_t *jfspecs = NULL;
117 json_t *jffield = NULL;
118 json_t *jfmatch = NULL;
121 if ((NULL == (jftype = json_object_get(jfilt, "type"))) ||
122 (!json_is_string(jftype))) {
123 fprintf(stderr, "tr_cfg_parse_one_filter: Error parsing filter type.\n");
124 *rc = TR_CFG_NOPARSE;
128 if ((NULL == (jfls = json_object_get(jfilt, "filter_lines"))) ||
129 (!json_is_array(jfls))) {
130 fprintf(stderr, "tr_cfg_parse_one_filter: Error parsing filter type.\n");
131 *rc = TR_CFG_NOPARSE;
135 if (TR_MAX_FILTER_LINES < json_array_size(jfls)) {
136 fprintf(stderr, "tr_cfg_parse_one_filter: Filter has too many filter_lines, maximimum of %d.\n", TR_MAX_FILTER_LINES);
137 *rc = TR_CFG_NOPARSE;
141 /* TBD -- optionally parse realm and domain constraints */
143 if (NULL == (filt = malloc(sizeof(TR_FILTER)))) {
144 fprintf(stderr, "tr_config_parse_one_filter: Out of memory.\n");
149 memset(filt, 0, sizeof(TR_FILTER));
151 if (!strcmp(json_string_value(jftype), "rp_permitted")) {
152 filt->type = TR_FILTER_TYPE_RP_PERMITTED;
155 fprintf(stderr, "tr_cfg_parse_one_filter: Error parsing filter type, unknown type '%s'.\n", json_string_value(jftype));
156 *rc = TR_CFG_NOPARSE;
157 tr_filter_free(filt);
161 /* For each filter line... */
162 for (i = 0; i < json_array_size(jfls); i++) {
164 if ((NULL == (jfaction = json_object_get(json_array_get(jfls, i), "action"))) ||
165 (!json_is_string(jfaction))) {
166 fprintf(stderr, "tr_cfg_parse_one_filter: Error parsing filter action.\n");
167 *rc = TR_CFG_NOPARSE;
168 tr_filter_free(filt);
172 /* TBD -- parse constraints */
174 if ((NULL == (jfspecs = json_object_get(json_array_get(jfls, i), "filter_specs"))) ||
175 (!json_is_array(jfspecs)) ||
176 (0 == json_array_size(jfspecs))) {
177 fprintf(stderr, "tr_cfg_parse_one_filter: Error parsing filter specs.\n");
178 *rc = TR_CFG_NOPARSE;
179 tr_filter_free(filt);
183 if (TR_MAX_FILTER_SPECS < json_array_size(jfspecs)) {
184 fprintf(stderr, "tr_cfg_parse_one_filter: Filter has too many filter_specs, maximimum of %d.\n", TR_MAX_FILTER_SPECS);
185 *rc = TR_CFG_NOPARSE;
186 tr_filter_free(filt);
190 if (NULL == (filt->lines[i] = malloc(sizeof(TR_FLINE)))) {
191 fprintf(stderr, "tr_config_parse_one_filter: Out of memory.\n");
193 tr_filter_free(filt);
197 memset(filt->lines[i], 0, sizeof(TR_FLINE));
199 if (!strcmp(json_string_value(jfaction), "accept")) {
200 filt->lines[i]->action = TR_FILTER_ACTION_ACCEPT;
202 else if (!strcmp(json_string_value(jfaction), "reject")) {
203 filt->lines[i]->action = TR_FILTER_ACTION_REJECT;
206 fprintf(stderr, "tr_cfg_parse_one_filter: Error parsing filter action, unknown action' %s'.\n", json_string_value(jfaction));
207 *rc = TR_CFG_NOPARSE;
208 tr_filter_free(filt);
212 /*For each filter spec within the filter line... */
213 for (j = 0; j <json_array_size(jfspecs); j++) {
215 if ((NULL == (jffield = json_object_get(json_array_get(jfspecs, j), "field"))) ||
216 (!json_is_string(jffield)) ||
217 (NULL == (jfmatch = json_object_get(json_array_get(jfspecs, j), "match"))) ||
218 (!json_is_string(jfmatch))) {
219 fprintf (stderr, "tr_cfg_parse_one_filter: Error parsing filter field and match for filter spec %d, filter line %d.\n", i, j);
220 *rc = TR_CFG_NOPARSE;
221 tr_filter_free(filt);
225 if (NULL == (filt->lines[i]->specs[j] = malloc(sizeof(TR_FSPEC)))) {
226 fprintf(stderr, "tr_config_parse_one_filter: Out of memory.\n");
228 tr_filter_free(filt);
232 memset(filt->lines[i]->specs[j], 0, sizeof(TR_FSPEC));
234 if ((NULL == (filt->lines[i]->specs[j]->field = tr_new_name((char *)json_string_value(jffield)))) ||
235 (NULL == (filt->lines[i]->specs[j]->match = tr_new_name((char *)json_string_value(jfmatch))))) {
236 fprintf(stderr, "tr_config_parse_one_filter: Out of memory.\n");
238 tr_filter_free(filt);
246 static TR_RP_CLIENT *tr_cfg_parse_one_rp_client (TR_INSTANCE *tr, json_t *jrp, TR_CFG_RC *rc)
248 TR_RP_CLIENT *rp = NULL;
250 json_t *jfilt = NULL;
251 json_t *jftype = NULL;
254 if ((!jrp) || (!rc)) {
255 fprintf(stderr, "tr_cfg_parse_one_rp_realm: Bad parameters.\n");
257 *rc = TR_CFG_BAD_PARAMS;
261 if ((NULL == (jgns = json_object_get(jrp, "gss_names"))) ||
262 (!json_is_array(jgns))) {
263 fprintf(stderr, "tr_cfg_parse_one_rp_client: Error parsing RP client configuration, no GSS names.\n");
264 *rc = TR_CFG_NOPARSE;
268 /* TBD -- Support more than one filter per RP client? */
269 if (NULL == (jfilt = json_object_get(jrp, "filter"))) {
270 fprintf(stderr, "tr_cfg_parse_one_rp_client: Error parsing RP client configuration, no filter.\n");
271 *rc = TR_CFG_NOPARSE;
275 /* We only support rp_permitted filters for RP clients */
276 if ((NULL == (jftype = json_object_get(jfilt, "type"))) ||
277 (!json_is_string(jftype)) ||
278 (strcmp(json_string_value(jftype), "rp_permitted"))) {
279 fprintf(stderr, "tr_cfg_parse_one_rp_client: Error parsing RP client filter type.\n");
280 *rc = TR_CFG_NOPARSE;
284 if (TR_MAX_GSS_NAMES < json_array_size(jgns)) {
285 fprintf(stderr, "tr_cfg_parse_one_rp_client: RP Client has too many GSS Names.\n");
286 *rc = TR_CFG_NOPARSE;
290 if (NULL == (rp = malloc(sizeof(TR_RP_CLIENT)))) {
291 fprintf(stderr, "tr_config_parse_one_rp_realm: Out of memory.\n");
296 memset(rp, 0, sizeof(TR_RP_CLIENT));
298 /* TBD -- support more than one filter entry per RP Client? */
299 if (NULL == (rp->filter = tr_cfg_parse_one_filter(tr, jfilt, rc))) {
300 fprintf(stderr, "tr_cfg_parse_one_rp_client: Error parsing filter.\n");
302 *rc = TR_CFG_NOPARSE;
306 for (i = 0; i < json_array_size(jgns); i++) {
307 if (NULL == (rp->gss_names[i] = tr_new_name ((char *)json_string_value(json_array_get(jgns, i))))) {
308 fprintf(stderr, "tr_cfg_parse_one_rp_client: No memory for GSS Name.\n");
318 static TR_CFG_RC tr_cfg_parse_rp_clients (TR_INSTANCE *tr, json_t *jcfg) {
320 TR_RP_CLIENT *rp = NULL;
321 TR_CFG_RC rc = TR_CFG_SUCCESS;
324 if ((!tr) || (!tr->new_cfg) || (!jcfg))
325 return TR_CFG_BAD_PARAMS;
327 if ((NULL == (jrps = json_object_get(jcfg, "rp_clients"))) ||
328 (!json_is_array(jrps))) {
329 return TR_CFG_NOPARSE;
332 for (i = 0; i < json_array_size(jrps); i++) {
333 if (NULL == (rp = tr_cfg_parse_one_rp_client(tr,
334 json_array_get(jrps, i),
338 fprintf(stderr, "tr_cfg_parse_rp_clients: RP client configured -- first gss: %s", rp->gss_names[0]->buf);
339 rp->next = tr->new_cfg->rp_clients;
340 tr->new_cfg->rp_clients = rp;
345 static TR_AAA_SERVER *tr_cfg_parse_one_aaa_server (TR_INSTANCE *tr, json_t *jaddr, TR_CFG_RC *rc) {
346 TR_AAA_SERVER *aaa = NULL;
348 if ((!tr) || (!tr->new_cfg) || (!jaddr) || (!json_is_string(jaddr))) {
349 fprintf(stderr, "tr_cfg_parse_one_aaa_server: Bad parameters.\n");
350 *rc = TR_CFG_BAD_PARAMS;
354 if (NULL == (aaa = malloc(sizeof(TR_AAA_SERVER)))) {
355 fprintf(stderr, "tr_config_parse_one_aaa_server: Out of memory.\n");
360 memset(aaa, 0, sizeof(TR_AAA_SERVER));
362 /* TBD -- Handle IPv6 addresses */
363 inet_aton(json_string_value(jaddr), &(aaa->aaa_server_addr));
369 static TR_AAA_SERVER *tr_cfg_parse_aaa_servers (TR_INSTANCE *tr, json_t *jaaas, TR_CFG_RC *rc)
371 TR_AAA_SERVER *aaa = NULL;
372 TR_AAA_SERVER *temp_aaa = NULL;
375 for (i = 0; i < json_array_size(jaaas); i++) {
376 if (NULL == (temp_aaa = tr_cfg_parse_one_aaa_server(tr, json_array_get(jaaas, i), rc))) {
379 /* TBD -- IPv6 addresses */
380 // fprintf(stderr, "tr_cfg_parse_aaa_servers: Configuring AAA Server: ip_addr = %s.\n", inet_ntoa(temp_aaa->aaa_server_addr));
381 temp_aaa->next = aaa;
387 static TR_APC *tr_cfg_parse_apcs (TR_INSTANCE *tr, json_t *japcs, TR_CFG_RC *rc)
391 *rc = TR_CFG_SUCCESS; /* presume success */
393 if ((!japcs) || (!rc)) {
394 fprintf(stderr, "tr_cfg_parse_apcs: Bad parameters.\n");
396 *rc = TR_CFG_BAD_PARAMS;
400 if (NULL == (apc = malloc(sizeof(TR_APC)))) {
401 fprintf (stderr, "tr_cfg_parse_apcs: Out of memory.\n");
406 memset(apc, 0, sizeof(TR_APC));
408 /* TBD, deal with more than one APC. In the meantime, though... */
409 /* Only parse the first APC, because we only know how to deal with one, anyway. */
410 if (0 == json_array_size(japcs))
413 if (NULL == (apc->id = tr_new_name((char *)json_string_value(json_array_get(japcs, 0))))) {
414 fprintf(stderr, "tr_cfg_parse_apcs: No memory for APC name.\n");
422 static TR_IDP_REALM *tr_cfg_parse_one_idp_realm (TR_INSTANCE *tr, json_t *jidp, TR_CFG_RC *rc) {
423 TR_IDP_REALM *idp = NULL;
425 json_t *jscfg = NULL;
426 json_t *jsrvrs = NULL;
427 json_t *japcs = NULL;
429 if ((!jidp) || (!rc)) {
430 fprintf(stderr, "tr_cfg_parse_one_idp_realm: Bad parameters.\n");
432 *rc = TR_CFG_BAD_PARAMS;
436 if (NULL == (idp = malloc(sizeof(TR_IDP_REALM)))) {
437 fprintf(stderr, "tr_config_parse_one_idp_realm: Out of memory.\n");
442 memset(idp, 0, sizeof(TR_IDP_REALM));
444 if ((NULL == (jrid = json_object_get(jidp, "realm_id"))) ||
445 (!json_is_string(jrid)) ||
446 (NULL == (jscfg = json_object_get(jidp, "shared_config"))) ||
447 (!json_is_string(jscfg)) ||
448 (NULL == (jsrvrs = json_object_get(jidp, "aaa_servers"))) ||
449 (!json_is_array(jsrvrs))) {
450 fprintf(stderr, "tr_cfg_parse_one_idp_realm: Error parsing IDP realm configuration.\n");
452 *rc = TR_CFG_NOPARSE;
456 if (0 == strcmp(json_string_value(jscfg), "no")) {
457 idp->shared_config = 0;
459 idp->shared_config = 1;
462 if (NULL == (idp->realm_id = tr_new_name((char *)json_string_value(jrid)))) {
464 fprintf(stderr, "tr_cfg_parse_one_idp_realm: No memory for realm id.\n");
469 if (NULL == (idp->aaa_servers = tr_cfg_parse_aaa_servers(tr, jsrvrs, rc))) {
470 fprintf(stderr, "tr_cfg_parse_one_idp_realm: Can't parse AAA servers for realm %s.\n", idp->realm_id->buf);
471 tr_free_name(idp->realm_id);
476 if ((NULL != (japcs = json_object_get(jidp, "apcs"))) &&
477 (json_is_array(japcs))) {
478 if (NULL == (idp->apcs = tr_cfg_parse_apcs(tr, japcs, rc))) {
479 fprintf(stderr, "tr_cfg_parse_one_idp_realm: Can't parse APCs for realm %s .\n", idp->realm_id->buf);
480 tr_free_name(idp->realm_id);
481 /* TBD -- free aaa_servers */;
489 static TR_CFG_RC tr_cfg_parse_idp_realms (TR_INSTANCE *tr, json_t *jcfg)
491 json_t *jidps = NULL;
492 TR_CFG_RC rc = TR_CFG_SUCCESS;
493 TR_IDP_REALM *idp = NULL;
496 if ((!tr) || (!tr->new_cfg) || (!jcfg))
497 return TR_CFG_BAD_PARAMS;
499 if ((NULL == (jidps = json_object_get(jcfg, "idp_realms"))) ||
500 (!json_is_array(jidps))) {
501 return TR_CFG_NOPARSE;
504 for (i = 0; i < json_array_size(jidps); i++) {
505 if (NULL == (idp = tr_cfg_parse_one_idp_realm(tr,
506 json_array_get(jidps, i),
510 fprintf(stderr, "tr_cfg_parse_idp_realms: IDP realm configured: %s.\n", idp->realm_id->buf);
511 idp->next = tr->new_cfg->idp_realms;
512 tr->new_cfg->idp_realms = idp;
517 static TR_IDP_REALM *tr_cfg_parse_comm_idps (TR_INSTANCE *tr, json_t *jidps, TR_CFG_RC *rc)
519 TR_IDP_REALM *idp = NULL;
520 TR_IDP_REALM *temp_idp = NULL;
525 (!json_is_array(jidps))) {
527 *rc = TR_CFG_BAD_PARAMS;
531 for (i = 0; i < json_array_size(jidps); i++) {
532 if (NULL == (temp_idp = (tr_cfg_find_idp(tr->new_cfg,
533 tr_new_name((char *)json_string_value(json_array_get(jidps, i))),
535 fprintf(stderr, "tr_cfg_parse_comm_idps: Unknown IDP %s.\n",
536 (char *)json_string_value(json_array_get(jidps, i)));
540 temp_idp->comm_next = idp;
547 static TR_RP_REALM *tr_cfg_parse_comm_rps (TR_INSTANCE *tr, json_t *jrps, TR_CFG_RC *rc)
549 TR_RP_REALM *rp = NULL;
550 TR_RP_REALM *temp_rp = NULL;
555 (!json_is_array(jrps))) {
557 *rc = TR_CFG_BAD_PARAMS;
561 for (i = (json_array_size(jrps)-1); i >= 0; i--) {
562 if (NULL == (temp_rp = malloc(sizeof(TR_RP_REALM)))) {
563 fprintf(stderr, "tr_cfg_parse_comm_rps: Can't allocate memory for RP Realm.\n");
568 memset (temp_rp, 0, sizeof(TR_RP_REALM));
570 if (NULL == (temp_rp->realm_name = tr_new_name((char *)json_string_value(json_array_get(jrps, i))))) {
571 fprintf(stderr, "tr_cfg_parse_comm_rps: No memory for RP Realm Name.\n");
584 static TR_COMM *tr_cfg_parse_one_comm (TR_INSTANCE *tr, json_t *jcomm, TR_CFG_RC *rc) {
585 TR_COMM *comm = NULL;
587 json_t *jtype = NULL;
588 json_t *japcs = NULL;
589 json_t *jidps = NULL;
592 if ((!jcomm) || (!rc)) {
593 fprintf(stderr, "tr_cfg_parse_one_comm: Bad parameters.\n");
595 *rc = TR_CFG_BAD_PARAMS;
599 if (NULL == (comm = malloc(sizeof(TR_COMM)))) {
600 fprintf(stderr, "tr_config_parse_one_comm: Out of memory.\n");
605 memset(comm, 0, sizeof(TR_COMM));
607 if ((NULL == (jid = json_object_get(jcomm, "community_id"))) ||
608 (!json_is_string(jid)) ||
609 (NULL == (jtype = json_object_get(jcomm, "type"))) ||
610 (!json_is_string(jtype)) ||
611 (NULL == (japcs = json_object_get(jcomm, "apcs"))) ||
612 (!json_is_array(japcs)) ||
613 (NULL == (jidps = json_object_get(jcomm, "idp_realms"))) ||
614 (!json_is_array(jidps)) ||
615 (NULL == (jrps = json_object_get(jcomm, "rp_realms"))) ||
616 (!json_is_array(jrps))) {
617 fprintf(stderr, "tr_cfg_parse_one_comm: Error parsing Communities configuration.\n");
619 *rc = TR_CFG_NOPARSE;
623 if (NULL == (comm->id = tr_new_name((char *)json_string_value(jid)))) {
625 fprintf(stderr, "tr_cfg_parse_one_comm: No memory for community id.\n");
630 if (0 == strcmp(json_string_value(jtype), "apc")) {
631 comm->type = TR_COMM_APC;
632 } else if (0 == strcmp(json_string_value(jtype), "coi")) {
633 comm->type = TR_COMM_COI;
634 if (NULL == (comm->apcs = tr_cfg_parse_apcs(tr, japcs, rc))) {
635 fprintf(stderr, "tr_cfg_parse_one_comm: Can't parse APCs for COI %s.\n", comm->id->buf);
636 tr_free_name(comm->id);
641 fprintf(stderr, "tr_cfg_parse_one_comm: Invalid community type, comm = %s, type = %s\n", comm->id->buf, json_string_value(jtype));
642 tr_free_name(comm->id);
644 *rc = TR_CFG_NOPARSE;
648 comm->idp_realms = tr_cfg_parse_comm_idps(tr, jidps, rc);
649 if (TR_CFG_SUCCESS != *rc) {
650 fprintf(stderr, "tr_cfg_parse_one_comm: Can't parse IDP realms for comm %s.\n", comm->id->buf);
651 tr_free_name(comm->id);
656 comm->rp_realms = tr_cfg_parse_comm_rps(tr, jrps, rc);
657 if (TR_CFG_SUCCESS != *rc) {
658 fprintf(stderr, "tr_cfg_parse_comm: Can't parse RP realms for comm %s .\n", comm->id->buf);
659 tr_free_name(comm->id);
660 /* TBD -- free idps? */;
668 static TR_CFG_RC tr_cfg_parse_comms (TR_INSTANCE *tr, json_t *jcfg)
670 json_t *jcomms = NULL;
671 TR_CFG_RC rc = TR_CFG_SUCCESS;
672 TR_COMM *comm = NULL;
675 if ((!tr) || (!tr->new_cfg) || (!jcfg)) {
676 fprintf(stderr, "tr_config_parse_comms: Bad Parameters.\n");
677 return TR_CFG_BAD_PARAMS;
680 if (NULL == (comm = malloc(sizeof(TR_COMM)))) {
681 fprintf(stderr, "tr_cfg_parse_comms: Out of Memory\n");
684 memset (comm, 0, sizeof(TR_COMM));
686 if ((NULL == (jcomms = json_object_get(jcfg, "communities"))) ||
687 (!json_is_array(jcomms))) {
688 return TR_CFG_NOPARSE;
691 for (i = 0; i < json_array_size(jcomms); i++) {
692 if (NULL == (comm = tr_cfg_parse_one_comm(tr,
693 json_array_get(jcomms, i),
697 fprintf(stderr, "tr_cfg_parse_comms: Community configured: %s.\n", comm->id->buf);
698 comm->next = tr->new_cfg->comms;
699 tr->new_cfg->comms = comm;
704 TR_CFG_RC tr_parse_config (TR_INSTANCE *tr, json_t *jcfg) {
706 /* If there is a partial/abandoned config lying around, free it */
708 tr_cfg_free(tr->new_cfg);
711 if (NULL == (tr->new_cfg = malloc(sizeof(TR_CFG))))
714 memset(tr->new_cfg, 0, sizeof(TR_CFG));
716 if ((TR_CFG_SUCCESS != tr_cfg_parse_internal(tr, jcfg)) ||
717 (TR_CFG_SUCCESS != tr_cfg_parse_rp_clients(tr, jcfg)) ||
718 (TR_CFG_SUCCESS != tr_cfg_parse_idp_realms(tr, jcfg)) ||
719 (TR_CFG_SUCCESS != tr_cfg_parse_comms(tr, jcfg))) {
720 tr_cfg_free(tr->new_cfg);
723 return TR_CFG_SUCCESS;
726 TR_IDP_REALM *tr_cfg_find_idp (TR_CFG *tr_cfg, TR_NAME *idp_id, TR_CFG_RC *rc)
729 TR_IDP_REALM *cfg_idp;
731 if ((!tr_cfg) || (!idp_id)) {
733 *rc = TR_CFG_BAD_PARAMS;
737 for (cfg_idp = tr_cfg->idp_realms; NULL != cfg_idp; cfg_idp = cfg_idp->next) {
738 if (!tr_name_cmp (idp_id, cfg_idp->realm_id)) {
739 fprintf(stderr, "tr_cfg_find_idp: Found %s.\n", idp_id->buf);
743 /* if we didn't find one, return NULL */
747 TR_RP_CLIENT *tr_cfg_find_rp (TR_CFG *tr_cfg, TR_NAME *rp_gss, TR_CFG_RC *rc)
749 TR_RP_CLIENT *cfg_rp;
752 if ((!tr_cfg) || (!rp_gss)) {
754 *rc = TR_CFG_BAD_PARAMS;
758 for (cfg_rp = tr_cfg->rp_clients; NULL != cfg_rp; cfg_rp = cfg_rp->next) {
759 for (i = 0; i < TR_MAX_GSS_NAMES; i++) {
760 if (!tr_name_cmp (rp_gss, cfg_rp->gss_names[i])) {
761 fprintf(stderr, "tr_cfg_find_rp: Found %s.\n", rp_gss->buf);
766 /* if we didn't find one, return NULL */
770 json_t *tr_read_config (int n, struct dirent **cfg_files) {
779 fprintf(stderr, "tr_read_config: Parsing %s.\n", cfg_files[n]->d_name);
780 if (NULL == (temp = json_load_file(cfg_files[n]->d_name, JSON_DISABLE_EOF_CHECK, &err))) {
781 fprintf (stderr, "tr_read_config: Error parsing config file %s.\n", cfg_files[n]->d_name);
785 /* TBD -- instead of using json_object_update, iterate through files for non-overlap config? */
789 if (-1 == json_object_update(jcfg, temp)) {
790 fprintf(stderr, "tr_read_config: Error merging config information.\n");
796 // fprintf(stderr, "tr_read_config: Merged configuration complete:\n%s\n", json_dumps(jcfg, 0));
801 static int is_cfg_file(const struct dirent *dent) {
804 /* if the last four letters of the filename are .cfg, return true. */
805 if ((4 <= (n = strlen(dent->d_name))) &&
806 (0 == strcmp(&(dent->d_name[n-4]), ".cfg"))) {
810 /* otherwise, return false. */
814 int tr_find_config_files (struct dirent ***cfg_files) {
817 n = scandir(".", cfg_files, &is_cfg_file, 0);
821 fprintf(stderr, "tr_find_config: scandir error.\n");
826 fprintf (stderr, "tr_find_config: No config files found.\n");
832 fprintf(stderr, "tr_find_config: Config file found (%s).\n", (*cfg_files)[i]->d_name);