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>
43 #include <trust_router/tr_constraint.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 json_t *jhname = NULL;
72 if ((!tr) || (!tr->new_cfg) || (!jcfg))
73 return TR_CFG_BAD_PARAMS;
75 if (NULL == (tr->new_cfg->internal = malloc(sizeof(TR_CFG_INTERNAL))))
78 memset(tr->new_cfg->internal, 0, sizeof(TR_CFG_INTERNAL));
80 if (NULL != (jint = json_object_get(jcfg, "tr_internal"))) {
81 if (NULL != (jmtd = json_object_get(jint, "max_tree_depth"))) {
82 if (json_is_number(jmtd)) {
83 tr->new_cfg->internal->max_tree_depth = json_integer_value(jmtd);
85 fprintf(stderr,"tr_cfg_parse_internal: Parsing error, max_tree_depth is not a number.\n");
86 return TR_CFG_NOPARSE;
89 /* If not configured, use the default */
90 tr->new_cfg->internal->max_tree_depth = TR_DEFAULT_MAX_TREE_DEPTH;
92 if (NULL != (jtp = json_object_get(jint, "tids_port"))) {
93 if (json_is_number(jtp)) {
94 tr->new_cfg->internal->tids_port = json_integer_value(jtp);
96 fprintf(stderr,"tr_cfg_parse_internal: Parsing error, port is not a number.\n");
97 return TR_CFG_NOPARSE;
100 /* If not configured, use the default */
101 tr->new_cfg->internal->tids_port = TR_DEFAULT_TIDS_PORT;
103 if (NULL != (jhname = json_object_get(jint, "hostname"))) {
104 if (json_is_string(jhname)) {
105 tr->new_cfg->internal->hostname = json_string_value(jhname);
107 fprintf(stderr,"tr_cfg_parse_internal: Parsing error, hostname is not a string.\n");
108 return TR_CFG_NOPARSE;
112 fprintf(stderr, "tr_cfg_parse_internal: Parsing error, hostname is not found.\n");
113 return TR_CFG_NOPARSE;
115 fprintf(stderr, "tr_cfg_parse_internal: Internal config parsed.\n");
116 return TR_CFG_SUCCESS;
119 fprintf(stderr, "tr_cfg_parse_internal: Parsing error, tr_internal configuration section not found.\n");
120 return TR_CFG_NOPARSE;
124 static TR_CONSTRAINT *tr_cfg_parse_one_constraint (TR_INSTANCE *tr, char *ctype, json_t *jc, TR_CFG_RC *rc)
129 if ((!tr) || (!ctype) || (!jc) || (!rc) ||
130 (!json_is_array(jc)) ||
131 (0 >= json_array_size(jc)) ||
132 (TR_MAX_CONST_MATCHES < json_array_size(jc)) ||
133 (!json_is_string(json_array_get(jc, 0)))) {
134 fprintf(stderr, "tr_cfg_parse_one_constraint: config error.\n");
135 *rc = TR_CFG_NOPARSE;
139 if (NULL == (cons = malloc(sizeof(TR_CONSTRAINT)))) {
140 fprintf(stderr, "tr_cfg_parse_one_constraint: Out of memory (cons).\n");
145 memset(cons, 0, sizeof(TR_CONSTRAINT));
147 if (NULL == (cons->type = tr_new_name(ctype))) {
148 fprintf(stderr, "tr_cfg_parse_one_constraint: Out of memory (type).\n");
153 for (i = 0; i < json_array_size(jc); i++) {
154 cons->matches[i] = tr_new_name((char *)(json_string_value(json_array_get(jc, i))));
160 static TR_FILTER *tr_cfg_parse_one_filter (TR_INSTANCE *tr, json_t *jfilt, TR_CFG_RC *rc)
162 TR_FILTER *filt = NULL;
163 json_t *jftype = NULL;
165 json_t *jfaction = NULL;
166 json_t *jfspecs = NULL;
167 json_t *jffield = NULL;
168 json_t *jfmatch = NULL;
173 if ((NULL == (jftype = json_object_get(jfilt, "type"))) ||
174 (!json_is_string(jftype))) {
175 fprintf(stderr, "tr_cfg_parse_one_filter: Error parsing filter type.\n");
176 *rc = TR_CFG_NOPARSE;
180 if ((NULL == (jfls = json_object_get(jfilt, "filter_lines"))) ||
181 (!json_is_array(jfls))) {
182 fprintf(stderr, "tr_cfg_parse_one_filter: Error parsing filter type.\n");
183 *rc = TR_CFG_NOPARSE;
187 if (TR_MAX_FILTER_LINES < json_array_size(jfls)) {
188 fprintf(stderr, "tr_cfg_parse_one_filter: Filter has too many filter_lines, maximimum of %d.\n", TR_MAX_FILTER_LINES);
189 *rc = TR_CFG_NOPARSE;
193 if (NULL == (filt = malloc(sizeof(TR_FILTER)))) {
194 fprintf(stderr, "tr_config_parse_one_filter: Out of memory.\n");
199 memset(filt, 0, sizeof(TR_FILTER));
201 if (!strcmp(json_string_value(jftype), "rp_permitted")) {
202 filt->type = TR_FILTER_TYPE_RP_PERMITTED;
205 fprintf(stderr, "tr_cfg_parse_one_filter: Error parsing filter type, unknown type '%s'.\n", json_string_value(jftype));
206 *rc = TR_CFG_NOPARSE;
207 tr_filter_free(filt);
211 /* For each filter line... */
212 for (i = 0; i < json_array_size(jfls); i++) {
214 if ((NULL == (jfaction = json_object_get(json_array_get(jfls, i), "action"))) ||
215 (!json_is_string(jfaction))) {
216 fprintf(stderr, "tr_cfg_parse_one_filter: Error parsing filter action.\n");
217 *rc = TR_CFG_NOPARSE;
218 tr_filter_free(filt);
222 if ((NULL == (jfspecs = json_object_get(json_array_get(jfls, i), "filter_specs"))) ||
223 (!json_is_array(jfspecs)) ||
224 (0 == json_array_size(jfspecs))) {
225 fprintf(stderr, "tr_cfg_parse_one_filter: Error parsing filter specs.\n");
226 *rc = TR_CFG_NOPARSE;
227 tr_filter_free(filt);
231 if (TR_MAX_FILTER_SPECS < json_array_size(jfspecs)) {
232 fprintf(stderr, "tr_cfg_parse_one_filter: Filter has too many filter_specs, maximimum of %d.\n", TR_MAX_FILTER_SPECS);
233 *rc = TR_CFG_NOPARSE;
234 tr_filter_free(filt);
238 if (NULL == (filt->lines[i] = malloc(sizeof(TR_FLINE)))) {
239 fprintf(stderr, "tr_config_parse_one_filter: Out of memory (fline).\n");
241 tr_filter_free(filt);
245 memset(filt->lines[i], 0, sizeof(TR_FLINE));
247 if (!strcmp(json_string_value(jfaction), "accept")) {
248 filt->lines[i]->action = TR_FILTER_ACTION_ACCEPT;
250 else if (!strcmp(json_string_value(jfaction), "reject")) {
251 filt->lines[i]->action = TR_FILTER_ACTION_REJECT;
254 fprintf(stderr, "tr_cfg_parse_one_filter: Error parsing filter action, unknown action' %s'.\n", json_string_value(jfaction));
255 *rc = TR_CFG_NOPARSE;
256 tr_filter_free(filt);
260 if ((NULL != (jrc = json_object_get(json_array_get(jfls, i), "realm_constraints"))) &&
261 (json_is_array(jrc)) &&
262 (0 != json_array_size(jrc)) &&
263 (TR_MAX_CONST_MATCHES >= json_array_size(jrc))) {
265 if (NULL == (filt->lines[i]->realm_cons = tr_cfg_parse_one_constraint(tr, "realm", jrc, rc))) {
266 fprintf(stderr, "tr_cfg_parse_one_filter: Error parsing realm constraint");
267 tr_filter_free(filt);
272 if ((NULL != (jdc = json_object_get(json_array_get(jfls, i), "domain_constraints"))) &&
273 (json_is_array(jdc)) &&
274 (0 != json_array_size(jdc)) &&
275 (TR_MAX_CONST_MATCHES >= json_array_size(jdc))) {
277 if (NULL == (filt->lines[i]->domain_cons = tr_cfg_parse_one_constraint(tr, "domain", jdc, rc))) {
278 fprintf(stderr, "tr_cfg_parse_one_filter: Error parsing domain constraint");
279 tr_filter_free(filt);
284 /*For each filter spec within the filter line... */
285 for (j = 0; j <json_array_size(jfspecs); j++) {
287 if ((NULL == (jffield = json_object_get(json_array_get(jfspecs, j), "field"))) ||
288 (!json_is_string(jffield)) ||
289 (NULL == (jfmatch = json_object_get(json_array_get(jfspecs, j), "match"))) ||
290 (!json_is_string(jfmatch))) {
291 fprintf (stderr, "tr_cfg_parse_one_filter: Error parsing filter field and match for filter spec %d, filter line %d.\n", i, j);
292 *rc = TR_CFG_NOPARSE;
293 tr_filter_free(filt);
297 if (NULL == (filt->lines[i]->specs[j] = malloc(sizeof(TR_FSPEC)))) {
298 fprintf(stderr, "tr_config_parse_one_filter: Out of memory.\n");
300 tr_filter_free(filt);
304 memset(filt->lines[i]->specs[j], 0, sizeof(TR_FSPEC));
306 if ((NULL == (filt->lines[i]->specs[j]->field = tr_new_name((char *)json_string_value(jffield)))) ||
307 (NULL == (filt->lines[i]->specs[j]->match = tr_new_name((char *)json_string_value(jfmatch))))) {
308 fprintf(stderr, "tr_config_parse_one_filter: Out of memory.\n");
310 tr_filter_free(filt);
319 static TR_RP_CLIENT *tr_cfg_parse_one_rp_client (TR_INSTANCE *tr, json_t *jrp, TR_CFG_RC *rc)
321 TR_RP_CLIENT *rp = NULL;
323 json_t *jfilt = NULL;
324 json_t *jftype = NULL;
327 if ((!jrp) || (!rc)) {
328 fprintf(stderr, "tr_cfg_parse_one_rp_realm: Bad parameters.\n");
330 *rc = TR_CFG_BAD_PARAMS;
334 if ((NULL == (jgns = json_object_get(jrp, "gss_names"))) ||
335 (!json_is_array(jgns))) {
336 fprintf(stderr, "tr_cfg_parse_one_rp_client: Error parsing RP client configuration, no GSS names.\n");
337 *rc = TR_CFG_NOPARSE;
341 /* TBD -- Support more than one filter per RP client? */
342 if (NULL == (jfilt = json_object_get(jrp, "filter"))) {
343 fprintf(stderr, "tr_cfg_parse_one_rp_client: Error parsing RP client configuration, no filter.\n");
344 *rc = TR_CFG_NOPARSE;
348 /* We only support rp_permitted filters for RP clients */
349 if ((NULL == (jftype = json_object_get(jfilt, "type"))) ||
350 (!json_is_string(jftype)) ||
351 (strcmp(json_string_value(jftype), "rp_permitted"))) {
352 fprintf(stderr, "tr_cfg_parse_one_rp_client: Error parsing RP client filter type.\n");
353 *rc = TR_CFG_NOPARSE;
357 if (TR_MAX_GSS_NAMES < json_array_size(jgns)) {
358 fprintf(stderr, "tr_cfg_parse_one_rp_client: RP Client has too many GSS Names.\n");
359 *rc = TR_CFG_NOPARSE;
363 if (NULL == (rp = malloc(sizeof(TR_RP_CLIENT)))) {
364 fprintf(stderr, "tr_config_parse_one_rp_realm: Out of memory.\n");
369 memset(rp, 0, sizeof(TR_RP_CLIENT));
371 /* TBD -- support more than one filter entry per RP Client? */
372 if (NULL == (rp->filter = tr_cfg_parse_one_filter(tr, jfilt, rc))) {
373 fprintf(stderr, "tr_cfg_parse_one_rp_client: Error parsing filter.\n");
375 *rc = TR_CFG_NOPARSE;
379 for (i = 0; i < json_array_size(jgns); i++) {
380 if (NULL == (rp->gss_names[i] = tr_new_name ((char *)json_string_value(json_array_get(jgns, i))))) {
381 fprintf(stderr, "tr_cfg_parse_one_rp_client: No memory for GSS Name.\n");
391 static TR_CFG_RC tr_cfg_parse_rp_clients (TR_INSTANCE *tr, json_t *jcfg) {
393 TR_RP_CLIENT *rp = NULL;
394 TR_CFG_RC rc = TR_CFG_SUCCESS;
397 if ((!tr) || (!tr->new_cfg) || (!jcfg))
398 return TR_CFG_BAD_PARAMS;
400 if ((NULL == (jrps = json_object_get(jcfg, "rp_clients"))) ||
401 (!json_is_array(jrps))) {
402 return TR_CFG_NOPARSE;
405 for (i = 0; i < json_array_size(jrps); i++) {
406 if (NULL == (rp = tr_cfg_parse_one_rp_client(tr,
407 json_array_get(jrps, i),
411 fprintf(stderr, "tr_cfg_parse_rp_clients: RP client configured -- first gss: %s", rp->gss_names[0]->buf);
412 rp->next = tr->new_cfg->rp_clients;
413 tr->new_cfg->rp_clients = rp;
418 static TR_AAA_SERVER *tr_cfg_parse_one_aaa_server (TR_INSTANCE *tr, json_t *jaddr, TR_CFG_RC *rc) {
419 TR_AAA_SERVER *aaa = NULL;
421 if ((!tr) || (!tr->new_cfg) || (!jaddr) || (!json_is_string(jaddr))) {
422 fprintf(stderr, "tr_cfg_parse_one_aaa_server: Bad parameters.\n");
423 *rc = TR_CFG_BAD_PARAMS;
427 if (NULL == (aaa = malloc(sizeof(TR_AAA_SERVER)))) {
428 fprintf(stderr, "tr_config_parse_one_aaa_server: Out of memory.\n");
433 memset(aaa, 0, sizeof(TR_AAA_SERVER));
435 aaa->hostname = tr_new_name((char *)(json_string_value(jaddr)));
441 static TR_AAA_SERVER *tr_cfg_parse_aaa_servers (TR_INSTANCE *tr, json_t *jaaas, TR_CFG_RC *rc)
443 TR_AAA_SERVER *aaa = NULL;
444 TR_AAA_SERVER *temp_aaa = NULL;
447 for (i = 0; i < json_array_size(jaaas); i++) {
448 if (NULL == (temp_aaa = tr_cfg_parse_one_aaa_server(tr, json_array_get(jaaas, i), rc))) {
451 /* TBD -- IPv6 addresses */
452 // fprintf(stderr, "tr_cfg_parse_aaa_servers: Configuring AAA Server: ip_addr = %s.\n", inet_ntoa(temp_aaa->aaa_server_addr));
453 temp_aaa->next = aaa;
459 static TR_APC *tr_cfg_parse_apcs (TR_INSTANCE *tr, json_t *japcs, TR_CFG_RC *rc)
463 *rc = TR_CFG_SUCCESS; /* presume success */
465 if ((!japcs) || (!rc)) {
466 fprintf(stderr, "tr_cfg_parse_apcs: Bad parameters.\n");
468 *rc = TR_CFG_BAD_PARAMS;
472 if (NULL == (apc = malloc(sizeof(TR_APC)))) {
473 fprintf (stderr, "tr_cfg_parse_apcs: Out of memory.\n");
478 memset(apc, 0, sizeof(TR_APC));
480 /* TBD, deal with more than one APC. In the meantime, though... */
481 /* Only parse the first APC, because we only know how to deal with one, anyway. */
482 if (0 == json_array_size(japcs))
485 if (NULL == (apc->id = tr_new_name((char *)json_string_value(json_array_get(japcs, 0))))) {
486 fprintf(stderr, "tr_cfg_parse_apcs: No memory for APC name.\n");
494 static TR_IDP_REALM *tr_cfg_parse_one_idp_realm (TR_INSTANCE *tr, json_t *jidp, TR_CFG_RC *rc) {
495 TR_IDP_REALM *idp = NULL;
497 json_t *jscfg = NULL;
498 json_t *jsrvrs = NULL;
499 json_t *japcs = NULL;
501 if ((!jidp) || (!rc)) {
502 fprintf(stderr, "tr_cfg_parse_one_idp_realm: Bad parameters.\n");
504 *rc = TR_CFG_BAD_PARAMS;
508 if (NULL == (idp = malloc(sizeof(TR_IDP_REALM)))) {
509 fprintf(stderr, "tr_config_parse_one_idp_realm: Out of memory.\n");
514 memset(idp, 0, sizeof(TR_IDP_REALM));
516 if ((NULL == (jrid = json_object_get(jidp, "realm_id"))) ||
517 (!json_is_string(jrid)) ||
518 (NULL == (jscfg = json_object_get(jidp, "shared_config"))) ||
519 (!json_is_string(jscfg)) ||
520 (NULL == (jsrvrs = json_object_get(jidp, "aaa_servers"))) ||
521 (!json_is_array(jsrvrs))) {
522 fprintf(stderr, "tr_cfg_parse_one_idp_realm: Error parsing IDP realm configuration.\n");
524 *rc = TR_CFG_NOPARSE;
528 if (0 == strcmp(json_string_value(jscfg), "no")) {
529 idp->shared_config = 0;
531 idp->shared_config = 1;
534 if (NULL == (idp->realm_id = tr_new_name((char *)json_string_value(jrid)))) {
536 fprintf(stderr, "tr_cfg_parse_one_idp_realm: No memory for realm id.\n");
541 if (NULL == (idp->aaa_servers = tr_cfg_parse_aaa_servers(tr, jsrvrs, rc))) {
542 fprintf(stderr, "tr_cfg_parse_one_idp_realm: Can't parse AAA servers for realm %s.\n", idp->realm_id->buf);
543 tr_free_name(idp->realm_id);
548 if ((NULL != (japcs = json_object_get(jidp, "apcs"))) &&
549 (json_is_array(japcs))) {
550 if (NULL == (idp->apcs = tr_cfg_parse_apcs(tr, japcs, rc))) {
551 fprintf(stderr, "tr_cfg_parse_one_idp_realm: Can't parse APCs for realm %s .\n", idp->realm_id->buf);
552 tr_free_name(idp->realm_id);
553 /* TBD -- free aaa_servers */;
561 static TR_CFG_RC tr_cfg_parse_idp_realms (TR_INSTANCE *tr, json_t *jcfg)
563 json_t *jidps = NULL;
564 TR_CFG_RC rc = TR_CFG_SUCCESS;
565 TR_IDP_REALM *idp = NULL;
568 if ((!tr) || (!tr->new_cfg) || (!jcfg))
569 return TR_CFG_BAD_PARAMS;
571 if ((NULL == (jidps = json_object_get(jcfg, "idp_realms"))) ||
572 (!json_is_array(jidps))) {
573 return TR_CFG_NOPARSE;
576 for (i = 0; i < json_array_size(jidps); i++) {
577 if (NULL == (idp = tr_cfg_parse_one_idp_realm(tr,
578 json_array_get(jidps, i),
582 fprintf(stderr, "tr_cfg_parse_idp_realms: IDP realm configured: %s.\n", idp->realm_id->buf);
583 idp->next = tr->new_cfg->idp_realms;
584 tr->new_cfg->idp_realms = idp;
589 static TR_IDP_REALM *tr_cfg_parse_comm_idps (TR_INSTANCE *tr, json_t *jidps, TR_CFG_RC *rc)
591 TR_IDP_REALM *idp = NULL;
592 TR_IDP_REALM *temp_idp = NULL;
597 (!json_is_array(jidps))) {
599 *rc = TR_CFG_BAD_PARAMS;
603 for (i = 0; i < json_array_size(jidps); i++) {
604 if (NULL == (temp_idp = (tr_cfg_find_idp(tr->new_cfg,
605 tr_new_name((char *)json_string_value(json_array_get(jidps, i))),
607 fprintf(stderr, "tr_cfg_parse_comm_idps: Unknown IDP %s.\n",
608 (char *)json_string_value(json_array_get(jidps, i)));
612 temp_idp->comm_next = idp;
619 static TR_RP_REALM *tr_cfg_parse_comm_rps (TR_INSTANCE *tr, json_t *jrps, TR_CFG_RC *rc)
621 TR_RP_REALM *rp = NULL;
622 TR_RP_REALM *temp_rp = NULL;
627 (!json_is_array(jrps))) {
629 *rc = TR_CFG_BAD_PARAMS;
633 for (i = (json_array_size(jrps)-1); i >= 0; i--) {
634 if (NULL == (temp_rp = malloc(sizeof(TR_RP_REALM)))) {
635 fprintf(stderr, "tr_cfg_parse_comm_rps: Can't allocate memory for RP Realm.\n");
640 memset (temp_rp, 0, sizeof(TR_RP_REALM));
642 if (NULL == (temp_rp->realm_name = tr_new_name((char *)json_string_value(json_array_get(jrps, i))))) {
643 fprintf(stderr, "tr_cfg_parse_comm_rps: No memory for RP Realm Name.\n");
656 static TR_COMM *tr_cfg_parse_one_comm (TR_INSTANCE *tr, json_t *jcomm, TR_CFG_RC *rc) {
657 TR_COMM *comm = NULL;
659 json_t *jtype = NULL;
660 json_t *japcs = NULL;
661 json_t *jidps = NULL;
664 if ((!jcomm) || (!rc)) {
665 fprintf(stderr, "tr_cfg_parse_one_comm: Bad parameters.\n");
667 *rc = TR_CFG_BAD_PARAMS;
671 if (NULL == (comm = malloc(sizeof(TR_COMM)))) {
672 fprintf(stderr, "tr_config_parse_one_comm: Out of memory.\n");
677 memset(comm, 0, sizeof(TR_COMM));
679 if ((NULL == (jid = json_object_get(jcomm, "community_id"))) ||
680 (!json_is_string(jid)) ||
681 (NULL == (jtype = json_object_get(jcomm, "type"))) ||
682 (!json_is_string(jtype)) ||
683 (NULL == (japcs = json_object_get(jcomm, "apcs"))) ||
684 (!json_is_array(japcs)) ||
685 (NULL == (jidps = json_object_get(jcomm, "idp_realms"))) ||
686 (!json_is_array(jidps)) ||
687 (NULL == (jrps = json_object_get(jcomm, "rp_realms"))) ||
688 (!json_is_array(jrps))) {
689 fprintf(stderr, "tr_cfg_parse_one_comm: Error parsing Communities configuration.\n");
691 *rc = TR_CFG_NOPARSE;
695 if (NULL == (comm->id = tr_new_name((char *)json_string_value(jid)))) {
697 fprintf(stderr, "tr_cfg_parse_one_comm: No memory for community id.\n");
702 if (0 == strcmp(json_string_value(jtype), "apc")) {
703 comm->type = TR_COMM_APC;
704 } else if (0 == strcmp(json_string_value(jtype), "coi")) {
705 comm->type = TR_COMM_COI;
706 if (NULL == (comm->apcs = tr_cfg_parse_apcs(tr, japcs, rc))) {
707 fprintf(stderr, "tr_cfg_parse_one_comm: Can't parse APCs for COI %s.\n", comm->id->buf);
708 tr_free_name(comm->id);
713 fprintf(stderr, "tr_cfg_parse_one_comm: Invalid community type, comm = %s, type = %s\n", comm->id->buf, json_string_value(jtype));
714 tr_free_name(comm->id);
716 *rc = TR_CFG_NOPARSE;
720 comm->idp_realms = tr_cfg_parse_comm_idps(tr, jidps, rc);
721 if (TR_CFG_SUCCESS != *rc) {
722 fprintf(stderr, "tr_cfg_parse_one_comm: Can't parse IDP realms for comm %s.\n", comm->id->buf);
723 tr_free_name(comm->id);
728 comm->rp_realms = tr_cfg_parse_comm_rps(tr, jrps, rc);
729 if (TR_CFG_SUCCESS != *rc) {
730 fprintf(stderr, "tr_cfg_parse_comm: Can't parse RP realms for comm %s .\n", comm->id->buf);
731 tr_free_name(comm->id);
732 /* TBD -- free idps? */;
740 static TR_CFG_RC tr_cfg_parse_comms (TR_INSTANCE *tr, json_t *jcfg)
742 json_t *jcomms = NULL;
743 TR_CFG_RC rc = TR_CFG_SUCCESS;
744 TR_COMM *comm = NULL;
747 if ((!tr) || (!tr->new_cfg) || (!jcfg)) {
748 fprintf(stderr, "tr_config_parse_comms: Bad Parameters.\n");
749 return TR_CFG_BAD_PARAMS;
752 if (NULL == (comm = malloc(sizeof(TR_COMM)))) {
753 fprintf(stderr, "tr_cfg_parse_comms: Out of Memory\n");
756 memset (comm, 0, sizeof(TR_COMM));
758 if ((NULL == (jcomms = json_object_get(jcfg, "communities"))) ||
759 (!json_is_array(jcomms))) {
760 return TR_CFG_NOPARSE;
763 for (i = 0; i < json_array_size(jcomms); i++) {
764 if (NULL == (comm = tr_cfg_parse_one_comm(tr,
765 json_array_get(jcomms, i),
769 fprintf(stderr, "tr_cfg_parse_comms: Community configured: %s.\n", comm->id->buf);
770 comm->next = tr->new_cfg->comms;
771 tr->new_cfg->comms = comm;
776 TR_CFG_RC tr_parse_config (TR_INSTANCE *tr, json_t *jcfg) {
778 /* If there is a partial/abandoned config lying around, free it */
780 tr_cfg_free(tr->new_cfg);
783 if (NULL == (tr->new_cfg = malloc(sizeof(TR_CFG))))
786 memset(tr->new_cfg, 0, sizeof(TR_CFG));
788 if ((TR_CFG_SUCCESS != tr_cfg_parse_internal(tr, jcfg)) ||
789 (TR_CFG_SUCCESS != tr_cfg_parse_rp_clients(tr, jcfg)) ||
790 (TR_CFG_SUCCESS != tr_cfg_parse_idp_realms(tr, jcfg)) ||
791 (TR_CFG_SUCCESS != tr_cfg_parse_comms(tr, jcfg))) {
792 tr_cfg_free(tr->new_cfg);
795 return TR_CFG_SUCCESS;
798 TR_IDP_REALM *tr_cfg_find_idp (TR_CFG *tr_cfg, TR_NAME *idp_id, TR_CFG_RC *rc)
801 TR_IDP_REALM *cfg_idp;
803 if ((!tr_cfg) || (!idp_id)) {
805 *rc = TR_CFG_BAD_PARAMS;
809 for (cfg_idp = tr_cfg->idp_realms; NULL != cfg_idp; cfg_idp = cfg_idp->next) {
810 if (!tr_name_cmp (idp_id, cfg_idp->realm_id)) {
811 fprintf(stderr, "tr_cfg_find_idp: Found %s.\n", idp_id->buf);
815 /* if we didn't find one, return NULL */
819 TR_RP_CLIENT *tr_cfg_find_rp (TR_CFG *tr_cfg, TR_NAME *rp_gss, TR_CFG_RC *rc)
821 TR_RP_CLIENT *cfg_rp;
824 if ((!tr_cfg) || (!rp_gss)) {
826 *rc = TR_CFG_BAD_PARAMS;
830 for (cfg_rp = tr_cfg->rp_clients; NULL != cfg_rp; cfg_rp = cfg_rp->next) {
831 for (i = 0; i < TR_MAX_GSS_NAMES; i++) {
832 if (!tr_name_cmp (rp_gss, cfg_rp->gss_names[i])) {
833 fprintf(stderr, "tr_cfg_find_rp: Found %s.\n", rp_gss->buf);
838 /* if we didn't find one, return NULL */
842 json_t *tr_read_config (int n, struct dirent **cfg_files) {
851 fprintf(stderr, "tr_read_config: Parsing %s.\n", cfg_files[n]->d_name);
852 if (NULL == (temp = json_load_file(cfg_files[n]->d_name, JSON_DISABLE_EOF_CHECK, &err))) {
853 fprintf (stderr, "tr_read_config: Error parsing config file %s.\n", cfg_files[n]->d_name);
857 /* TBD -- instead of using json_object_update, iterate through files for non-overlap config? */
861 if (-1 == json_object_update(jcfg, temp)) {
862 fprintf(stderr, "tr_read_config: Error merging config information.\n");
868 fprintf(stderr, "tr_read_config: Merged configuration complete:\n%s\n", json_dumps(jcfg, 0));
873 static int is_cfg_file(const struct dirent *dent) {
876 /* if the last four letters of the filename are .cfg, return true. */
877 if ((4 <= (n = strlen(dent->d_name))) &&
878 (0 == strcmp(&(dent->d_name[n-4]), ".cfg"))) {
882 /* otherwise, return false. */
886 int tr_find_config_files (struct dirent ***cfg_files) {
889 n = scandir(".", cfg_files, &is_cfg_file, 0);
893 fprintf(stderr, "tr_find_config: scandir error.\n");
898 fprintf (stderr, "tr_find_config: No config files found.\n");
904 fprintf(stderr, "tr_find_config: Config file found (%s).\n", (*cfg_files)[i]->d_name);