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) {
69 json_t *jhname = NULL;
71 if ((!tr) || (!tr->new_cfg) || (!jcfg))
72 return TR_CFG_BAD_PARAMS;
74 if (NULL == (tr->new_cfg->internal = malloc(sizeof(TR_CFG_INTERNAL))))
77 memset(tr->new_cfg->internal, 0, sizeof(TR_CFG_INTERNAL));
79 if (NULL != (jint = json_object_get(jcfg, "tr_internal"))) {
80 if (NULL != (jmtd = json_object_get(jint, "max_tree_depth"))) {
81 if (json_is_number(jmtd)) {
82 tr->new_cfg->internal->max_tree_depth = json_integer_value(jmtd);
84 fprintf(stderr,"tr_cfg_parse_internal: Parsing error, max_tree_depth is not a number.\n");
85 return TR_CFG_NOPARSE;
88 /* If not configured, use the default */
89 tr->new_cfg->internal->max_tree_depth = TR_DEFAULT_MAX_TREE_DEPTH;
91 if (NULL != (jhname = json_object_get(jint, "hostname"))) {
92 if (json_is_string(jhname)) {
93 tr->new_cfg->internal->hostname = json_string_value(jhname);
95 fprintf(stderr,"tr_cfg_parse_internal: Parsing error, hostname is not a string.\n");
96 return TR_CFG_NOPARSE;
100 fprintf(stderr, "tr_cfg_parse_internal: Parsing error, hostname is not found.\n");
101 return TR_CFG_NOPARSE;
103 fprintf(stderr, "tr_cfg_parse_internal: Internal config parsed.\n");
104 return TR_CFG_SUCCESS;
107 fprintf(stderr, "tr_cfg_parse_internal: Parsing error, tr_internal configuration section not found.\n");
108 return TR_CFG_NOPARSE;
112 static TR_FILTER *tr_cfg_parse_one_filter (TR_INSTANCE *tr, json_t *jfilt, TR_CFG_RC *rc)
114 TR_FILTER *filt = NULL;
115 json_t *jftype = NULL;
117 json_t *jfaction = NULL;
118 json_t *jfspecs = NULL;
119 json_t *jffield = NULL;
120 json_t *jfmatch = NULL;
123 if ((NULL == (jftype = json_object_get(jfilt, "type"))) ||
124 (!json_is_string(jftype))) {
125 fprintf(stderr, "tr_cfg_parse_one_filter: Error parsing filter type.\n");
126 *rc = TR_CFG_NOPARSE;
130 if ((NULL == (jfls = json_object_get(jfilt, "filter_lines"))) ||
131 (!json_is_array(jfls))) {
132 fprintf(stderr, "tr_cfg_parse_one_filter: Error parsing filter type.\n");
133 *rc = TR_CFG_NOPARSE;
137 if (TR_MAX_FILTER_LINES < json_array_size(jfls)) {
138 fprintf(stderr, "tr_cfg_parse_one_filter: Filter has too many filter_lines, maximimum of %d.\n", TR_MAX_FILTER_LINES);
139 *rc = TR_CFG_NOPARSE;
143 /* TBD -- optionally parse realm and domain constraints */
145 if (NULL == (filt = malloc(sizeof(TR_FILTER)))) {
146 fprintf(stderr, "tr_config_parse_one_filter: Out of memory.\n");
151 memset(filt, 0, sizeof(TR_FILTER));
153 if (!strcmp(json_string_value(jftype), "rp_permitted")) {
154 filt->type = TR_FILTER_TYPE_RP_PERMITTED;
157 fprintf(stderr, "tr_cfg_parse_one_filter: Error parsing filter type, unknown type '%s'.\n", json_string_value(jftype));
158 *rc = TR_CFG_NOPARSE;
159 tr_filter_free(filt);
163 /* For each filter line... */
164 for (i = 0; i < json_array_size(jfls); i++) {
166 if ((NULL == (jfaction = json_object_get(json_array_get(jfls, i), "action"))) ||
167 (!json_is_string(jfaction))) {
168 fprintf(stderr, "tr_cfg_parse_one_filter: Error parsing filter action.\n");
169 *rc = TR_CFG_NOPARSE;
170 tr_filter_free(filt);
174 /* TBD -- parse constraints */
176 if ((NULL == (jfspecs = json_object_get(json_array_get(jfls, i), "filter_specs"))) ||
177 (!json_is_array(jfspecs)) ||
178 (0 == json_array_size(jfspecs))) {
179 fprintf(stderr, "tr_cfg_parse_one_filter: Error parsing filter specs.\n");
180 *rc = TR_CFG_NOPARSE;
181 tr_filter_free(filt);
185 if (TR_MAX_FILTER_SPECS < json_array_size(jfspecs)) {
186 fprintf(stderr, "tr_cfg_parse_one_filter: Filter has too many filter_specs, maximimum of %d.\n", TR_MAX_FILTER_SPECS);
187 *rc = TR_CFG_NOPARSE;
188 tr_filter_free(filt);
192 if (NULL == (filt->lines[i] = malloc(sizeof(TR_FLINE)))) {
193 fprintf(stderr, "tr_config_parse_one_filter: Out of memory.\n");
195 tr_filter_free(filt);
199 memset(filt->lines[i], 0, sizeof(TR_FLINE));
201 if (!strcmp(json_string_value(jfaction), "accept")) {
202 filt->lines[i]->action = TR_FILTER_ACTION_ACCEPT;
204 else if (!strcmp(json_string_value(jfaction), "reject")) {
205 filt->lines[i]->action = TR_FILTER_ACTION_REJECT;
208 fprintf(stderr, "tr_cfg_parse_one_filter: Error parsing filter action, unknown action' %s'.\n", json_string_value(jfaction));
209 *rc = TR_CFG_NOPARSE;
210 tr_filter_free(filt);
214 /*For each filter spec within the filter line... */
215 for (j = 0; j <json_array_size(jfspecs); j++) {
217 if ((NULL == (jffield = json_object_get(json_array_get(jfspecs, j), "field"))) ||
218 (!json_is_string(jffield)) ||
219 (NULL == (jfmatch = json_object_get(json_array_get(jfspecs, j), "match"))) ||
220 (!json_is_string(jfmatch))) {
221 fprintf (stderr, "tr_cfg_parse_one_filter: Error parsing filter field and match for filter spec %d, filter line %d.\n", i, j);
222 *rc = TR_CFG_NOPARSE;
223 tr_filter_free(filt);
227 if (NULL == (filt->lines[i]->specs[j] = malloc(sizeof(TR_FSPEC)))) {
228 fprintf(stderr, "tr_config_parse_one_filter: Out of memory.\n");
230 tr_filter_free(filt);
234 memset(filt->lines[i]->specs[j], 0, sizeof(TR_FSPEC));
236 if ((NULL == (filt->lines[i]->specs[j]->field = tr_new_name((char *)json_string_value(jffield)))) ||
237 (NULL == (filt->lines[i]->specs[j]->match = tr_new_name((char *)json_string_value(jfmatch))))) {
238 fprintf(stderr, "tr_config_parse_one_filter: Out of memory.\n");
240 tr_filter_free(filt);
248 static TR_RP_CLIENT *tr_cfg_parse_one_rp_client (TR_INSTANCE *tr, json_t *jrp, TR_CFG_RC *rc)
250 TR_RP_CLIENT *rp = NULL;
252 json_t *jfilt = NULL;
253 json_t *jftype = NULL;
256 if ((!jrp) || (!rc)) {
257 fprintf(stderr, "tr_cfg_parse_one_rp_realm: Bad parameters.\n");
259 *rc = TR_CFG_BAD_PARAMS;
263 if ((NULL == (jgns = json_object_get(jrp, "gss_names"))) ||
264 (!json_is_array(jgns))) {
265 fprintf(stderr, "tr_cfg_parse_one_rp_client: Error parsing RP client configuration, no GSS names.\n");
266 *rc = TR_CFG_NOPARSE;
270 /* TBD -- Support more than one filter per RP client? */
271 if (NULL == (jfilt = json_object_get(jrp, "filter"))) {
272 fprintf(stderr, "tr_cfg_parse_one_rp_client: Error parsing RP client configuration, no filter.\n");
273 *rc = TR_CFG_NOPARSE;
277 /* We only support rp_permitted filters for RP clients */
278 if ((NULL == (jftype = json_object_get(jfilt, "type"))) ||
279 (!json_is_string(jftype)) ||
280 (strcmp(json_string_value(jftype), "rp_permitted"))) {
281 fprintf(stderr, "tr_cfg_parse_one_rp_client: Error parsing RP client filter type.\n");
282 *rc = TR_CFG_NOPARSE;
286 if (TR_MAX_GSS_NAMES < json_array_size(jgns)) {
287 fprintf(stderr, "tr_cfg_parse_one_rp_client: RP Client has too many GSS Names.\n");
288 *rc = TR_CFG_NOPARSE;
292 if (NULL == (rp = malloc(sizeof(TR_RP_CLIENT)))) {
293 fprintf(stderr, "tr_config_parse_one_rp_realm: Out of memory.\n");
298 memset(rp, 0, sizeof(TR_RP_CLIENT));
300 /* TBD -- support more than one filter entry per RP Client? */
301 if (NULL == (rp->filter = tr_cfg_parse_one_filter(tr, jfilt, rc))) {
302 fprintf(stderr, "tr_cfg_parse_one_rp_client: Error parsing filter.\n");
304 *rc = TR_CFG_NOPARSE;
308 for (i = 0; i < json_array_size(jgns); i++) {
309 if (NULL == (rp->gss_names[i] = tr_new_name ((char *)json_string_value(json_array_get(jgns, i))))) {
310 fprintf(stderr, "tr_cfg_parse_one_rp_client: No memory for GSS Name.\n");
320 static TR_CFG_RC tr_cfg_parse_rp_clients (TR_INSTANCE *tr, json_t *jcfg) {
322 TR_RP_CLIENT *rp = NULL;
323 TR_CFG_RC rc = TR_CFG_SUCCESS;
326 if ((!tr) || (!tr->new_cfg) || (!jcfg))
327 return TR_CFG_BAD_PARAMS;
329 if ((NULL == (jrps = json_object_get(jcfg, "rp_clients"))) ||
330 (!json_is_array(jrps))) {
331 return TR_CFG_NOPARSE;
334 for (i = 0; i < json_array_size(jrps); i++) {
335 if (NULL == (rp = tr_cfg_parse_one_rp_client(tr,
336 json_array_get(jrps, i),
340 fprintf(stderr, "tr_cfg_parse_rp_clients: RP client configured -- first gss: %s", rp->gss_names[0]->buf);
341 rp->next = tr->new_cfg->rp_clients;
342 tr->new_cfg->rp_clients = rp;
347 static TR_AAA_SERVER *tr_cfg_parse_one_aaa_server (TR_INSTANCE *tr, json_t *jaddr, TR_CFG_RC *rc) {
348 TR_AAA_SERVER *aaa = NULL;
350 if ((!tr) || (!tr->new_cfg) || (!jaddr) || (!json_is_string(jaddr))) {
351 fprintf(stderr, "tr_cfg_parse_one_aaa_server: Bad parameters.\n");
352 *rc = TR_CFG_BAD_PARAMS;
356 if (NULL == (aaa = malloc(sizeof(TR_AAA_SERVER)))) {
357 fprintf(stderr, "tr_config_parse_one_aaa_server: Out of memory.\n");
362 memset(aaa, 0, sizeof(TR_AAA_SERVER));
364 /* TBD -- Handle IPv6 addresses */
365 inet_aton(json_string_value(jaddr), &(aaa->aaa_server_addr));
371 static TR_AAA_SERVER *tr_cfg_parse_aaa_servers (TR_INSTANCE *tr, json_t *jaaas, TR_CFG_RC *rc)
373 TR_AAA_SERVER *aaa = NULL;
374 TR_AAA_SERVER *temp_aaa = NULL;
377 for (i = 0; i < json_array_size(jaaas); i++) {
378 if (NULL == (temp_aaa = tr_cfg_parse_one_aaa_server(tr, json_array_get(jaaas, i), rc))) {
381 /* TBD -- IPv6 addresses */
382 // fprintf(stderr, "tr_cfg_parse_aaa_servers: Configuring AAA Server: ip_addr = %s.\n", inet_ntoa(temp_aaa->aaa_server_addr));
383 temp_aaa->next = aaa;
389 static TR_APC *tr_cfg_parse_apcs (TR_INSTANCE *tr, json_t *japcs, TR_CFG_RC *rc)
393 *rc = TR_CFG_SUCCESS; /* presume success */
395 if ((!japcs) || (!rc)) {
396 fprintf(stderr, "tr_cfg_parse_apcs: Bad parameters.\n");
398 *rc = TR_CFG_BAD_PARAMS;
402 if (NULL == (apc = malloc(sizeof(TR_APC)))) {
403 fprintf (stderr, "tr_cfg_parse_apcs: Out of memory.\n");
408 memset(apc, 0, sizeof(TR_APC));
410 /* TBD, deal with more than one APC. In the meantime, though... */
411 /* Only parse the first APC, because we only know how to deal with one, anyway. */
412 if (0 == json_array_size(japcs))
415 if (NULL == (apc->id = tr_new_name((char *)json_string_value(json_array_get(japcs, 0))))) {
416 fprintf(stderr, "tr_cfg_parse_apcs: No memory for APC name.\n");
424 static TR_IDP_REALM *tr_cfg_parse_one_idp_realm (TR_INSTANCE *tr, json_t *jidp, TR_CFG_RC *rc) {
425 TR_IDP_REALM *idp = NULL;
427 json_t *jscfg = NULL;
428 json_t *jsrvrs = NULL;
429 json_t *japcs = NULL;
431 if ((!jidp) || (!rc)) {
432 fprintf(stderr, "tr_cfg_parse_one_idp_realm: Bad parameters.\n");
434 *rc = TR_CFG_BAD_PARAMS;
438 if (NULL == (idp = malloc(sizeof(TR_IDP_REALM)))) {
439 fprintf(stderr, "tr_config_parse_one_idp_realm: Out of memory.\n");
444 memset(idp, 0, sizeof(TR_IDP_REALM));
446 if ((NULL == (jrid = json_object_get(jidp, "realm_id"))) ||
447 (!json_is_string(jrid)) ||
448 (NULL == (jscfg = json_object_get(jidp, "shared_config"))) ||
449 (!json_is_string(jscfg)) ||
450 (NULL == (jsrvrs = json_object_get(jidp, "aaa_servers"))) ||
451 (!json_is_array(jsrvrs))) {
452 fprintf(stderr, "tr_cfg_parse_one_idp_realm: Error parsing IDP realm configuration.\n");
454 *rc = TR_CFG_NOPARSE;
458 if (0 == strcmp(json_string_value(jscfg), "no")) {
459 idp->shared_config = 0;
461 idp->shared_config = 1;
464 if (NULL == (idp->realm_id = tr_new_name((char *)json_string_value(jrid)))) {
466 fprintf(stderr, "tr_cfg_parse_one_idp_realm: No memory for realm id.\n");
471 if (NULL == (idp->aaa_servers = tr_cfg_parse_aaa_servers(tr, jsrvrs, rc))) {
472 fprintf(stderr, "tr_cfg_parse_one_idp_realm: Can't parse AAA servers for realm %s.\n", idp->realm_id->buf);
473 tr_free_name(idp->realm_id);
478 if ((NULL != (japcs = json_object_get(jidp, "apcs"))) &&
479 (json_is_array(japcs))) {
480 if (NULL == (idp->apcs = tr_cfg_parse_apcs(tr, japcs, rc))) {
481 fprintf(stderr, "tr_cfg_parse_one_idp_realm: Can't parse APCs for realm %s .\n", idp->realm_id->buf);
482 tr_free_name(idp->realm_id);
483 /* TBD -- free aaa_servers */;
491 static TR_CFG_RC tr_cfg_parse_idp_realms (TR_INSTANCE *tr, json_t *jcfg)
493 json_t *jidps = NULL;
494 TR_CFG_RC rc = TR_CFG_SUCCESS;
495 TR_IDP_REALM *idp = NULL;
498 if ((!tr) || (!tr->new_cfg) || (!jcfg))
499 return TR_CFG_BAD_PARAMS;
501 if ((NULL == (jidps = json_object_get(jcfg, "idp_realms"))) ||
502 (!json_is_array(jidps))) {
503 return TR_CFG_NOPARSE;
506 for (i = 0; i < json_array_size(jidps); i++) {
507 if (NULL == (idp = tr_cfg_parse_one_idp_realm(tr,
508 json_array_get(jidps, i),
512 fprintf(stderr, "tr_cfg_parse_idp_realms: IDP realm configured: %s.\n", idp->realm_id->buf);
513 idp->next = tr->new_cfg->idp_realms;
514 tr->new_cfg->idp_realms = idp;
519 static TR_IDP_REALM *tr_cfg_parse_comm_idps (TR_INSTANCE *tr, json_t *jidps, TR_CFG_RC *rc)
521 TR_IDP_REALM *idp = NULL;
522 TR_IDP_REALM *temp_idp = NULL;
527 (!json_is_array(jidps))) {
529 *rc = TR_CFG_BAD_PARAMS;
533 for (i = 0; i < json_array_size(jidps); i++) {
534 if (NULL == (temp_idp = (tr_cfg_find_idp(tr->new_cfg,
535 tr_new_name((char *)json_string_value(json_array_get(jidps, i))),
537 fprintf(stderr, "tr_cfg_parse_comm_idps: Unknown IDP %s.\n",
538 (char *)json_string_value(json_array_get(jidps, i)));
542 temp_idp->comm_next = idp;
549 static TR_RP_REALM *tr_cfg_parse_comm_rps (TR_INSTANCE *tr, json_t *jrps, TR_CFG_RC *rc)
551 TR_RP_REALM *rp = NULL;
552 TR_RP_REALM *temp_rp = NULL;
557 (!json_is_array(jrps))) {
559 *rc = TR_CFG_BAD_PARAMS;
563 for (i = (json_array_size(jrps)-1); i >= 0; i--) {
564 if (NULL == (temp_rp = malloc(sizeof(TR_RP_REALM)))) {
565 fprintf(stderr, "tr_cfg_parse_comm_rps: Can't allocate memory for RP Realm.\n");
570 memset (temp_rp, 0, sizeof(TR_RP_REALM));
572 if (NULL == (temp_rp->realm_name = tr_new_name((char *)json_string_value(json_array_get(jrps, i))))) {
573 fprintf(stderr, "tr_cfg_parse_comm_rps: No memory for RP Realm Name.\n");
586 static TR_COMM *tr_cfg_parse_one_comm (TR_INSTANCE *tr, json_t *jcomm, TR_CFG_RC *rc) {
587 TR_COMM *comm = NULL;
589 json_t *jtype = NULL;
590 json_t *japcs = NULL;
591 json_t *jidps = NULL;
594 if ((!jcomm) || (!rc)) {
595 fprintf(stderr, "tr_cfg_parse_one_comm: Bad parameters.\n");
597 *rc = TR_CFG_BAD_PARAMS;
601 if (NULL == (comm = malloc(sizeof(TR_COMM)))) {
602 fprintf(stderr, "tr_config_parse_one_comm: Out of memory.\n");
607 memset(comm, 0, sizeof(TR_COMM));
609 if ((NULL == (jid = json_object_get(jcomm, "community_id"))) ||
610 (!json_is_string(jid)) ||
611 (NULL == (jtype = json_object_get(jcomm, "type"))) ||
612 (!json_is_string(jtype)) ||
613 (NULL == (japcs = json_object_get(jcomm, "apcs"))) ||
614 (!json_is_array(japcs)) ||
615 (NULL == (jidps = json_object_get(jcomm, "idp_realms"))) ||
616 (!json_is_array(jidps)) ||
617 (NULL == (jrps = json_object_get(jcomm, "rp_realms"))) ||
618 (!json_is_array(jrps))) {
619 fprintf(stderr, "tr_cfg_parse_one_comm: Error parsing Communities configuration.\n");
621 *rc = TR_CFG_NOPARSE;
625 if (NULL == (comm->id = tr_new_name((char *)json_string_value(jid)))) {
627 fprintf(stderr, "tr_cfg_parse_one_comm: No memory for community id.\n");
632 if (0 == strcmp(json_string_value(jtype), "apc")) {
633 comm->type = TR_COMM_APC;
634 } else if (0 == strcmp(json_string_value(jtype), "coi")) {
635 comm->type = TR_COMM_COI;
636 if (NULL == (comm->apcs = tr_cfg_parse_apcs(tr, japcs, rc))) {
637 fprintf(stderr, "tr_cfg_parse_one_comm: Can't parse APCs for COI %s.\n", comm->id->buf);
638 tr_free_name(comm->id);
643 fprintf(stderr, "tr_cfg_parse_one_comm: Invalid community type, comm = %s, type = %s\n", comm->id->buf, json_string_value(jtype));
644 tr_free_name(comm->id);
646 *rc = TR_CFG_NOPARSE;
650 comm->idp_realms = tr_cfg_parse_comm_idps(tr, jidps, rc);
651 if (TR_CFG_SUCCESS != *rc) {
652 fprintf(stderr, "tr_cfg_parse_one_comm: Can't parse IDP realms for comm %s.\n", comm->id->buf);
653 tr_free_name(comm->id);
658 comm->rp_realms = tr_cfg_parse_comm_rps(tr, jrps, rc);
659 if (TR_CFG_SUCCESS != *rc) {
660 fprintf(stderr, "tr_cfg_parse_comm: Can't parse RP realms for comm %s .\n", comm->id->buf);
661 tr_free_name(comm->id);
662 /* TBD -- free idps? */;
670 static TR_CFG_RC tr_cfg_parse_comms (TR_INSTANCE *tr, json_t *jcfg)
672 json_t *jcomms = NULL;
673 TR_CFG_RC rc = TR_CFG_SUCCESS;
674 TR_COMM *comm = NULL;
677 if ((!tr) || (!tr->new_cfg) || (!jcfg)) {
678 fprintf(stderr, "tr_config_parse_comms: Bad Parameters.\n");
679 return TR_CFG_BAD_PARAMS;
682 if (NULL == (comm = malloc(sizeof(TR_COMM)))) {
683 fprintf(stderr, "tr_cfg_parse_comms: Out of Memory\n");
686 memset (comm, 0, sizeof(TR_COMM));
688 if ((NULL == (jcomms = json_object_get(jcfg, "communities"))) ||
689 (!json_is_array(jcomms))) {
690 return TR_CFG_NOPARSE;
693 for (i = 0; i < json_array_size(jcomms); i++) {
694 if (NULL == (comm = tr_cfg_parse_one_comm(tr,
695 json_array_get(jcomms, i),
699 fprintf(stderr, "tr_cfg_parse_comms: Community configured: %s.\n", comm->id->buf);
700 comm->next = tr->new_cfg->comms;
701 tr->new_cfg->comms = comm;
706 TR_CFG_RC tr_parse_config (TR_INSTANCE *tr, json_t *jcfg) {
708 /* If there is a partial/abandoned config lying around, free it */
710 tr_cfg_free(tr->new_cfg);
713 if (NULL == (tr->new_cfg = malloc(sizeof(TR_CFG))))
716 memset(tr->new_cfg, 0, sizeof(TR_CFG));
718 if ((TR_CFG_SUCCESS != tr_cfg_parse_internal(tr, jcfg)) ||
719 (TR_CFG_SUCCESS != tr_cfg_parse_rp_clients(tr, jcfg)) ||
720 (TR_CFG_SUCCESS != tr_cfg_parse_idp_realms(tr, jcfg)) ||
721 (TR_CFG_SUCCESS != tr_cfg_parse_comms(tr, jcfg))) {
722 tr_cfg_free(tr->new_cfg);
725 return TR_CFG_SUCCESS;
728 TR_IDP_REALM *tr_cfg_find_idp (TR_CFG *tr_cfg, TR_NAME *idp_id, TR_CFG_RC *rc)
731 TR_IDP_REALM *cfg_idp;
733 if ((!tr_cfg) || (!idp_id)) {
735 *rc = TR_CFG_BAD_PARAMS;
739 for (cfg_idp = tr_cfg->idp_realms; NULL != cfg_idp; cfg_idp = cfg_idp->next) {
740 if (!tr_name_cmp (idp_id, cfg_idp->realm_id)) {
741 fprintf(stderr, "tr_cfg_find_idp: Found %s.\n", idp_id->buf);
745 /* if we didn't find one, return NULL */
749 TR_RP_CLIENT *tr_cfg_find_rp (TR_CFG *tr_cfg, TR_NAME *rp_gss, TR_CFG_RC *rc)
751 TR_RP_CLIENT *cfg_rp;
754 if ((!tr_cfg) || (!rp_gss)) {
756 *rc = TR_CFG_BAD_PARAMS;
760 for (cfg_rp = tr_cfg->rp_clients; NULL != cfg_rp; cfg_rp = cfg_rp->next) {
761 for (i = 0; i < TR_MAX_GSS_NAMES; i++) {
762 if (!tr_name_cmp (rp_gss, cfg_rp->gss_names[i])) {
763 fprintf(stderr, "tr_cfg_find_rp: Found %s.\n", rp_gss->buf);
768 /* if we didn't find one, return NULL */
772 json_t *tr_read_config (int n, struct dirent **cfg_files) {
781 fprintf(stderr, "tr_read_config: Parsing %s.\n", cfg_files[n]->d_name);
782 if (NULL == (temp = json_load_file(cfg_files[n]->d_name, JSON_DISABLE_EOF_CHECK, &err))) {
783 fprintf (stderr, "tr_read_config: Error parsing config file %s.\n", cfg_files[n]->d_name);
787 /* TBD -- instead of using json_object_update, iterate through files for non-overlap config? */
791 if (-1 == json_object_update(jcfg, temp)) {
792 fprintf(stderr, "tr_read_config: Error merging config information.\n");
798 // fprintf(stderr, "tr_read_config: Merged configuration complete:\n%s\n", json_dumps(jcfg, 0));
803 static int is_cfg_file(const struct dirent *dent) {
806 /* if the last four letters of the filename are .cfg, return true. */
807 if ((4 <= (n = strlen(dent->d_name))) &&
808 (0 == strcmp(&(dent->d_name[n-4]), ".cfg"))) {
812 /* otherwise, return false. */
816 int tr_find_config_files (struct dirent ***cfg_files) {
819 n = scandir(".", cfg_files, &is_cfg_file, 0);
823 fprintf(stderr, "tr_find_config: scandir error.\n");
828 fprintf (stderr, "tr_find_config: No config files found.\n");
834 fprintf(stderr, "tr_find_config: Config file found (%s).\n", (*cfg_files)[i]->d_name);