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_CONSTRAINT *tr_cfg_parse_one_constraint (TR_INSTANCE *tr, const char *ctype, json_t *jdc, TR_CFG_RC *rc)
114 if (NULL == (cons = malloc(sizeof(TR_CONSTRAINT)))) {
115 fprintf(stderr, "tr_cfg_parse_one_constraint: Out of memory.\n");
120 memset(filt, 0, sizeof(TR_FILTER));
125 static TR_FILTER *tr_cfg_parse_one_filter (TR_INSTANCE *tr, json_t *jfilt, TR_CFG_RC *rc)
127 TR_FILTER *filt = NULL;
128 json_t *jftype = NULL;
130 json_t *jfaction = NULL;
131 json_t *jfspecs = NULL;
132 json_t *jffield = NULL;
133 json_t *jfmatch = NULL;
138 if ((NULL == (jftype = json_object_get(jfilt, "type"))) ||
139 (!json_is_string(jftype))) {
140 fprintf(stderr, "tr_cfg_parse_one_filter: Error parsing filter type.\n");
141 *rc = TR_CFG_NOPARSE;
145 if ((NULL == (jfls = json_object_get(jfilt, "filter_lines"))) ||
146 (!json_is_array(jfls))) {
147 fprintf(stderr, "tr_cfg_parse_one_filter: Error parsing filter type.\n");
148 *rc = TR_CFG_NOPARSE;
152 if (TR_MAX_FILTER_LINES < json_array_size(jfls)) {
153 fprintf(stderr, "tr_cfg_parse_one_filter: Filter has too many filter_lines, maximimum of %d.\n", TR_MAX_FILTER_LINES);
154 *rc = TR_CFG_NOPARSE;
158 if (NULL == (filt = malloc(sizeof(TR_FILTER)))) {
159 fprintf(stderr, "tr_config_parse_one_filter: Out of memory.\n");
164 memset(filt, 0, sizeof(TR_FILTER));
166 if (!strcmp(json_string_value(jftype), "rp_permitted")) {
167 filt->type = TR_FILTER_TYPE_RP_PERMITTED;
170 fprintf(stderr, "tr_cfg_parse_one_filter: Error parsing filter type, unknown type '%s'.\n", json_string_value(jftype));
171 *rc = TR_CFG_NOPARSE;
172 tr_filter_free(filt);
176 /* For each filter line... */
177 for (i = 0; i < json_array_size(jfls); i++) {
179 if ((NULL == (jfaction = json_object_get(json_array_get(jfls, i), "action"))) ||
180 (!json_is_string(jfaction))) {
181 fprintf(stderr, "tr_cfg_parse_one_filter: Error parsing filter action.\n");
182 *rc = TR_CFG_NOPARSE;
183 tr_filter_free(filt);
187 if ((NULL != (jrc = json_object_get(json_array_get(jfls, i), "realm_constraints"))) &&
188 (json_is_array(jrc)) &&
189 (0 != json_array_size(jrc)) &&
190 (TR_MAX_CONST_MATCHES >= json_array_size(jrc))) {
191 if (NULL == (filt->realm_cons = tr_cfg_parse_one_constraint(tr, "realm", jrc, rc)))
192 fprintf(stderr, "tr_cfg_parse_one_filter: Error parsing realm constraint");
193 tr_filter_free(filt);
197 if ((NULL != (jdc = json_object_get(json_array_get(jfls, i), "domain_constraints"))) &&
198 (json_is_array(jdc)) &&
199 (0 != json_array_size(jdc)) &&
200 (TR_MAX_CONST_MATCHES >= json_array_size(jdc))) {
201 if (NULL == (filt->realm_cons = tr_cfg_parse_one_constraint(tr, "domain", jdc, rc)))
202 fprintf(stderr, "tr_cfg_parse_one_filter: Error parsing domain constraint");
203 tr_filter_free(filt);
207 if ((NULL == (jfspecs = json_object_get(json_array_get(jfls, i), "filter_specs"))) ||
208 (!json_is_array(jfspecs)) ||
209 (0 == json_array_size(jfspecs))) {
210 fprintf(stderr, "tr_cfg_parse_one_filter: Error parsing filter specs.\n");
211 *rc = TR_CFG_NOPARSE;
212 tr_filter_free(filt);
216 if (TR_MAX_FILTER_SPECS < json_array_size(jfspecs)) {
217 fprintf(stderr, "tr_cfg_parse_one_filter: Filter has too many filter_specs, maximimum of %d.\n", TR_MAX_FILTER_SPECS);
218 *rc = TR_CFG_NOPARSE;
219 tr_filter_free(filt);
223 if (NULL == (filt->lines[i] = malloc(sizeof(TR_FLINE)))) {
224 fprintf(stderr, "tr_config_parse_one_filter: Out of memory.\n");
226 tr_filter_free(filt);
230 memset(filt->lines[i], 0, sizeof(TR_FLINE));
232 if (!strcmp(json_string_value(jfaction), "accept")) {
233 filt->lines[i]->action = TR_FILTER_ACTION_ACCEPT;
235 else if (!strcmp(json_string_value(jfaction), "reject")) {
236 filt->lines[i]->action = TR_FILTER_ACTION_REJECT;
239 fprintf(stderr, "tr_cfg_parse_one_filter: Error parsing filter action, unknown action' %s'.\n", json_string_value(jfaction));
240 *rc = TR_CFG_NOPARSE;
241 tr_filter_free(filt);
245 /*For each filter spec within the filter line... */
246 for (j = 0; j <json_array_size(jfspecs); j++) {
248 if ((NULL == (jffield = json_object_get(json_array_get(jfspecs, j), "field"))) ||
249 (!json_is_string(jffield)) ||
250 (NULL == (jfmatch = json_object_get(json_array_get(jfspecs, j), "match"))) ||
251 (!json_is_string(jfmatch))) {
252 fprintf (stderr, "tr_cfg_parse_one_filter: Error parsing filter field and match for filter spec %d, filter line %d.\n", i, j);
253 *rc = TR_CFG_NOPARSE;
254 tr_filter_free(filt);
258 if (NULL == (filt->lines[i]->specs[j] = malloc(sizeof(TR_FSPEC)))) {
259 fprintf(stderr, "tr_config_parse_one_filter: Out of memory.\n");
261 tr_filter_free(filt);
265 memset(filt->lines[i]->specs[j], 0, sizeof(TR_FSPEC));
267 if ((NULL == (filt->lines[i]->specs[j]->field = tr_new_name((char *)json_string_value(jffield)))) ||
268 (NULL == (filt->lines[i]->specs[j]->match = tr_new_name((char *)json_string_value(jfmatch))))) {
269 fprintf(stderr, "tr_config_parse_one_filter: Out of memory.\n");
271 tr_filter_free(filt);
279 static TR_RP_CLIENT *tr_cfg_parse_one_rp_client (TR_INSTANCE *tr, json_t *jrp, TR_CFG_RC *rc)
281 TR_RP_CLIENT *rp = NULL;
283 json_t *jfilt = NULL;
284 json_t *jftype = NULL;
287 if ((!jrp) || (!rc)) {
288 fprintf(stderr, "tr_cfg_parse_one_rp_realm: Bad parameters.\n");
290 *rc = TR_CFG_BAD_PARAMS;
294 if ((NULL == (jgns = json_object_get(jrp, "gss_names"))) ||
295 (!json_is_array(jgns))) {
296 fprintf(stderr, "tr_cfg_parse_one_rp_client: Error parsing RP client configuration, no GSS names.\n");
297 *rc = TR_CFG_NOPARSE;
301 /* TBD -- Support more than one filter per RP client? */
302 if (NULL == (jfilt = json_object_get(jrp, "filter"))) {
303 fprintf(stderr, "tr_cfg_parse_one_rp_client: Error parsing RP client configuration, no filter.\n");
304 *rc = TR_CFG_NOPARSE;
308 /* We only support rp_permitted filters for RP clients */
309 if ((NULL == (jftype = json_object_get(jfilt, "type"))) ||
310 (!json_is_string(jftype)) ||
311 (strcmp(json_string_value(jftype), "rp_permitted"))) {
312 fprintf(stderr, "tr_cfg_parse_one_rp_client: Error parsing RP client filter type.\n");
313 *rc = TR_CFG_NOPARSE;
317 if (TR_MAX_GSS_NAMES < json_array_size(jgns)) {
318 fprintf(stderr, "tr_cfg_parse_one_rp_client: RP Client has too many GSS Names.\n");
319 *rc = TR_CFG_NOPARSE;
323 if (NULL == (rp = malloc(sizeof(TR_RP_CLIENT)))) {
324 fprintf(stderr, "tr_config_parse_one_rp_realm: Out of memory.\n");
329 memset(rp, 0, sizeof(TR_RP_CLIENT));
331 /* TBD -- support more than one filter entry per RP Client? */
332 if (NULL == (rp->filter = tr_cfg_parse_one_filter(tr, jfilt, rc))) {
333 fprintf(stderr, "tr_cfg_parse_one_rp_client: Error parsing filter.\n");
335 *rc = TR_CFG_NOPARSE;
339 for (i = 0; i < json_array_size(jgns); i++) {
340 if (NULL == (rp->gss_names[i] = tr_new_name ((char *)json_string_value(json_array_get(jgns, i))))) {
341 fprintf(stderr, "tr_cfg_parse_one_rp_client: No memory for GSS Name.\n");
351 static TR_CFG_RC tr_cfg_parse_rp_clients (TR_INSTANCE *tr, json_t *jcfg) {
353 TR_RP_CLIENT *rp = NULL;
354 TR_CFG_RC rc = TR_CFG_SUCCESS;
357 if ((!tr) || (!tr->new_cfg) || (!jcfg))
358 return TR_CFG_BAD_PARAMS;
360 if ((NULL == (jrps = json_object_get(jcfg, "rp_clients"))) ||
361 (!json_is_array(jrps))) {
362 return TR_CFG_NOPARSE;
365 for (i = 0; i < json_array_size(jrps); i++) {
366 if (NULL == (rp = tr_cfg_parse_one_rp_client(tr,
367 json_array_get(jrps, i),
371 fprintf(stderr, "tr_cfg_parse_rp_clients: RP client configured -- first gss: %s", rp->gss_names[0]->buf);
372 rp->next = tr->new_cfg->rp_clients;
373 tr->new_cfg->rp_clients = rp;
378 static TR_AAA_SERVER *tr_cfg_parse_one_aaa_server (TR_INSTANCE *tr, json_t *jaddr, TR_CFG_RC *rc) {
379 TR_AAA_SERVER *aaa = NULL;
381 if ((!tr) || (!tr->new_cfg) || (!jaddr) || (!json_is_string(jaddr))) {
382 fprintf(stderr, "tr_cfg_parse_one_aaa_server: Bad parameters.\n");
383 *rc = TR_CFG_BAD_PARAMS;
387 if (NULL == (aaa = malloc(sizeof(TR_AAA_SERVER)))) {
388 fprintf(stderr, "tr_config_parse_one_aaa_server: Out of memory.\n");
393 memset(aaa, 0, sizeof(TR_AAA_SERVER));
395 /* TBD -- Handle IPv6 addresses */
396 inet_aton(json_string_value(jaddr), &(aaa->aaa_server_addr));
402 static TR_AAA_SERVER *tr_cfg_parse_aaa_servers (TR_INSTANCE *tr, json_t *jaaas, TR_CFG_RC *rc)
404 TR_AAA_SERVER *aaa = NULL;
405 TR_AAA_SERVER *temp_aaa = NULL;
408 for (i = 0; i < json_array_size(jaaas); i++) {
409 if (NULL == (temp_aaa = tr_cfg_parse_one_aaa_server(tr, json_array_get(jaaas, i), rc))) {
412 /* TBD -- IPv6 addresses */
413 // fprintf(stderr, "tr_cfg_parse_aaa_servers: Configuring AAA Server: ip_addr = %s.\n", inet_ntoa(temp_aaa->aaa_server_addr));
414 temp_aaa->next = aaa;
420 static TR_APC *tr_cfg_parse_apcs (TR_INSTANCE *tr, json_t *japcs, TR_CFG_RC *rc)
424 *rc = TR_CFG_SUCCESS; /* presume success */
426 if ((!japcs) || (!rc)) {
427 fprintf(stderr, "tr_cfg_parse_apcs: Bad parameters.\n");
429 *rc = TR_CFG_BAD_PARAMS;
433 if (NULL == (apc = malloc(sizeof(TR_APC)))) {
434 fprintf (stderr, "tr_cfg_parse_apcs: Out of memory.\n");
439 memset(apc, 0, sizeof(TR_APC));
441 /* TBD, deal with more than one APC. In the meantime, though... */
442 /* Only parse the first APC, because we only know how to deal with one, anyway. */
443 if (0 == json_array_size(japcs))
446 if (NULL == (apc->id = tr_new_name((char *)json_string_value(json_array_get(japcs, 0))))) {
447 fprintf(stderr, "tr_cfg_parse_apcs: No memory for APC name.\n");
455 static TR_IDP_REALM *tr_cfg_parse_one_idp_realm (TR_INSTANCE *tr, json_t *jidp, TR_CFG_RC *rc) {
456 TR_IDP_REALM *idp = NULL;
458 json_t *jscfg = NULL;
459 json_t *jsrvrs = NULL;
460 json_t *japcs = NULL;
462 if ((!jidp) || (!rc)) {
463 fprintf(stderr, "tr_cfg_parse_one_idp_realm: Bad parameters.\n");
465 *rc = TR_CFG_BAD_PARAMS;
469 if (NULL == (idp = malloc(sizeof(TR_IDP_REALM)))) {
470 fprintf(stderr, "tr_config_parse_one_idp_realm: Out of memory.\n");
475 memset(idp, 0, sizeof(TR_IDP_REALM));
477 if ((NULL == (jrid = json_object_get(jidp, "realm_id"))) ||
478 (!json_is_string(jrid)) ||
479 (NULL == (jscfg = json_object_get(jidp, "shared_config"))) ||
480 (!json_is_string(jscfg)) ||
481 (NULL == (jsrvrs = json_object_get(jidp, "aaa_servers"))) ||
482 (!json_is_array(jsrvrs))) {
483 fprintf(stderr, "tr_cfg_parse_one_idp_realm: Error parsing IDP realm configuration.\n");
485 *rc = TR_CFG_NOPARSE;
489 if (0 == strcmp(json_string_value(jscfg), "no")) {
490 idp->shared_config = 0;
492 idp->shared_config = 1;
495 if (NULL == (idp->realm_id = tr_new_name((char *)json_string_value(jrid)))) {
497 fprintf(stderr, "tr_cfg_parse_one_idp_realm: No memory for realm id.\n");
502 if (NULL == (idp->aaa_servers = tr_cfg_parse_aaa_servers(tr, jsrvrs, rc))) {
503 fprintf(stderr, "tr_cfg_parse_one_idp_realm: Can't parse AAA servers for realm %s.\n", idp->realm_id->buf);
504 tr_free_name(idp->realm_id);
509 if ((NULL != (japcs = json_object_get(jidp, "apcs"))) &&
510 (json_is_array(japcs))) {
511 if (NULL == (idp->apcs = tr_cfg_parse_apcs(tr, japcs, rc))) {
512 fprintf(stderr, "tr_cfg_parse_one_idp_realm: Can't parse APCs for realm %s .\n", idp->realm_id->buf);
513 tr_free_name(idp->realm_id);
514 /* TBD -- free aaa_servers */;
522 static TR_CFG_RC tr_cfg_parse_idp_realms (TR_INSTANCE *tr, json_t *jcfg)
524 json_t *jidps = NULL;
525 TR_CFG_RC rc = TR_CFG_SUCCESS;
526 TR_IDP_REALM *idp = NULL;
529 if ((!tr) || (!tr->new_cfg) || (!jcfg))
530 return TR_CFG_BAD_PARAMS;
532 if ((NULL == (jidps = json_object_get(jcfg, "idp_realms"))) ||
533 (!json_is_array(jidps))) {
534 return TR_CFG_NOPARSE;
537 for (i = 0; i < json_array_size(jidps); i++) {
538 if (NULL == (idp = tr_cfg_parse_one_idp_realm(tr,
539 json_array_get(jidps, i),
543 fprintf(stderr, "tr_cfg_parse_idp_realms: IDP realm configured: %s.\n", idp->realm_id->buf);
544 idp->next = tr->new_cfg->idp_realms;
545 tr->new_cfg->idp_realms = idp;
550 static TR_IDP_REALM *tr_cfg_parse_comm_idps (TR_INSTANCE *tr, json_t *jidps, TR_CFG_RC *rc)
552 TR_IDP_REALM *idp = NULL;
553 TR_IDP_REALM *temp_idp = NULL;
558 (!json_is_array(jidps))) {
560 *rc = TR_CFG_BAD_PARAMS;
564 for (i = 0; i < json_array_size(jidps); i++) {
565 if (NULL == (temp_idp = (tr_cfg_find_idp(tr->new_cfg,
566 tr_new_name((char *)json_string_value(json_array_get(jidps, i))),
568 fprintf(stderr, "tr_cfg_parse_comm_idps: Unknown IDP %s.\n",
569 (char *)json_string_value(json_array_get(jidps, i)));
573 temp_idp->comm_next = idp;
580 static TR_RP_REALM *tr_cfg_parse_comm_rps (TR_INSTANCE *tr, json_t *jrps, TR_CFG_RC *rc)
582 TR_RP_REALM *rp = NULL;
583 TR_RP_REALM *temp_rp = NULL;
588 (!json_is_array(jrps))) {
590 *rc = TR_CFG_BAD_PARAMS;
594 for (i = (json_array_size(jrps)-1); i >= 0; i--) {
595 if (NULL == (temp_rp = malloc(sizeof(TR_RP_REALM)))) {
596 fprintf(stderr, "tr_cfg_parse_comm_rps: Can't allocate memory for RP Realm.\n");
601 memset (temp_rp, 0, sizeof(TR_RP_REALM));
603 if (NULL == (temp_rp->realm_name = tr_new_name((char *)json_string_value(json_array_get(jrps, i))))) {
604 fprintf(stderr, "tr_cfg_parse_comm_rps: No memory for RP Realm Name.\n");
617 static TR_COMM *tr_cfg_parse_one_comm (TR_INSTANCE *tr, json_t *jcomm, TR_CFG_RC *rc) {
618 TR_COMM *comm = NULL;
620 json_t *jtype = NULL;
621 json_t *japcs = NULL;
622 json_t *jidps = NULL;
625 if ((!jcomm) || (!rc)) {
626 fprintf(stderr, "tr_cfg_parse_one_comm: Bad parameters.\n");
628 *rc = TR_CFG_BAD_PARAMS;
632 if (NULL == (comm = malloc(sizeof(TR_COMM)))) {
633 fprintf(stderr, "tr_config_parse_one_comm: Out of memory.\n");
638 memset(comm, 0, sizeof(TR_COMM));
640 if ((NULL == (jid = json_object_get(jcomm, "community_id"))) ||
641 (!json_is_string(jid)) ||
642 (NULL == (jtype = json_object_get(jcomm, "type"))) ||
643 (!json_is_string(jtype)) ||
644 (NULL == (japcs = json_object_get(jcomm, "apcs"))) ||
645 (!json_is_array(japcs)) ||
646 (NULL == (jidps = json_object_get(jcomm, "idp_realms"))) ||
647 (!json_is_array(jidps)) ||
648 (NULL == (jrps = json_object_get(jcomm, "rp_realms"))) ||
649 (!json_is_array(jrps))) {
650 fprintf(stderr, "tr_cfg_parse_one_comm: Error parsing Communities configuration.\n");
652 *rc = TR_CFG_NOPARSE;
656 if (NULL == (comm->id = tr_new_name((char *)json_string_value(jid)))) {
658 fprintf(stderr, "tr_cfg_parse_one_comm: No memory for community id.\n");
663 if (0 == strcmp(json_string_value(jtype), "apc")) {
664 comm->type = TR_COMM_APC;
665 } else if (0 == strcmp(json_string_value(jtype), "coi")) {
666 comm->type = TR_COMM_COI;
667 if (NULL == (comm->apcs = tr_cfg_parse_apcs(tr, japcs, rc))) {
668 fprintf(stderr, "tr_cfg_parse_one_comm: Can't parse APCs for COI %s.\n", comm->id->buf);
669 tr_free_name(comm->id);
674 fprintf(stderr, "tr_cfg_parse_one_comm: Invalid community type, comm = %s, type = %s\n", comm->id->buf, json_string_value(jtype));
675 tr_free_name(comm->id);
677 *rc = TR_CFG_NOPARSE;
681 comm->idp_realms = tr_cfg_parse_comm_idps(tr, jidps, rc);
682 if (TR_CFG_SUCCESS != *rc) {
683 fprintf(stderr, "tr_cfg_parse_one_comm: Can't parse IDP realms for comm %s.\n", comm->id->buf);
684 tr_free_name(comm->id);
689 comm->rp_realms = tr_cfg_parse_comm_rps(tr, jrps, rc);
690 if (TR_CFG_SUCCESS != *rc) {
691 fprintf(stderr, "tr_cfg_parse_comm: Can't parse RP realms for comm %s .\n", comm->id->buf);
692 tr_free_name(comm->id);
693 /* TBD -- free idps? */;
701 static TR_CFG_RC tr_cfg_parse_comms (TR_INSTANCE *tr, json_t *jcfg)
703 json_t *jcomms = NULL;
704 TR_CFG_RC rc = TR_CFG_SUCCESS;
705 TR_COMM *comm = NULL;
708 if ((!tr) || (!tr->new_cfg) || (!jcfg)) {
709 fprintf(stderr, "tr_config_parse_comms: Bad Parameters.\n");
710 return TR_CFG_BAD_PARAMS;
713 if (NULL == (comm = malloc(sizeof(TR_COMM)))) {
714 fprintf(stderr, "tr_cfg_parse_comms: Out of Memory\n");
717 memset (comm, 0, sizeof(TR_COMM));
719 if ((NULL == (jcomms = json_object_get(jcfg, "communities"))) ||
720 (!json_is_array(jcomms))) {
721 return TR_CFG_NOPARSE;
724 for (i = 0; i < json_array_size(jcomms); i++) {
725 if (NULL == (comm = tr_cfg_parse_one_comm(tr,
726 json_array_get(jcomms, i),
730 fprintf(stderr, "tr_cfg_parse_comms: Community configured: %s.\n", comm->id->buf);
731 comm->next = tr->new_cfg->comms;
732 tr->new_cfg->comms = comm;
737 TR_CFG_RC tr_parse_config (TR_INSTANCE *tr, json_t *jcfg) {
739 /* If there is a partial/abandoned config lying around, free it */
741 tr_cfg_free(tr->new_cfg);
744 if (NULL == (tr->new_cfg = malloc(sizeof(TR_CFG))))
747 memset(tr->new_cfg, 0, sizeof(TR_CFG));
749 if ((TR_CFG_SUCCESS != tr_cfg_parse_internal(tr, jcfg)) ||
750 (TR_CFG_SUCCESS != tr_cfg_parse_rp_clients(tr, jcfg)) ||
751 (TR_CFG_SUCCESS != tr_cfg_parse_idp_realms(tr, jcfg)) ||
752 (TR_CFG_SUCCESS != tr_cfg_parse_comms(tr, jcfg))) {
753 tr_cfg_free(tr->new_cfg);
756 return TR_CFG_SUCCESS;
759 TR_IDP_REALM *tr_cfg_find_idp (TR_CFG *tr_cfg, TR_NAME *idp_id, TR_CFG_RC *rc)
762 TR_IDP_REALM *cfg_idp;
764 if ((!tr_cfg) || (!idp_id)) {
766 *rc = TR_CFG_BAD_PARAMS;
770 for (cfg_idp = tr_cfg->idp_realms; NULL != cfg_idp; cfg_idp = cfg_idp->next) {
771 if (!tr_name_cmp (idp_id, cfg_idp->realm_id)) {
772 fprintf(stderr, "tr_cfg_find_idp: Found %s.\n", idp_id->buf);
776 /* if we didn't find one, return NULL */
780 TR_RP_CLIENT *tr_cfg_find_rp (TR_CFG *tr_cfg, TR_NAME *rp_gss, TR_CFG_RC *rc)
782 TR_RP_CLIENT *cfg_rp;
785 if ((!tr_cfg) || (!rp_gss)) {
787 *rc = TR_CFG_BAD_PARAMS;
791 for (cfg_rp = tr_cfg->rp_clients; NULL != cfg_rp; cfg_rp = cfg_rp->next) {
792 for (i = 0; i < TR_MAX_GSS_NAMES; i++) {
793 if (!tr_name_cmp (rp_gss, cfg_rp->gss_names[i])) {
794 fprintf(stderr, "tr_cfg_find_rp: Found %s.\n", rp_gss->buf);
799 /* if we didn't find one, return NULL */
803 json_t *tr_read_config (int n, struct dirent **cfg_files) {
812 fprintf(stderr, "tr_read_config: Parsing %s.\n", cfg_files[n]->d_name);
813 if (NULL == (temp = json_load_file(cfg_files[n]->d_name, JSON_DISABLE_EOF_CHECK, &err))) {
814 fprintf (stderr, "tr_read_config: Error parsing config file %s.\n", cfg_files[n]->d_name);
818 /* TBD -- instead of using json_object_update, iterate through files for non-overlap config? */
822 if (-1 == json_object_update(jcfg, temp)) {
823 fprintf(stderr, "tr_read_config: Error merging config information.\n");
829 // fprintf(stderr, "tr_read_config: Merged configuration complete:\n%s\n", json_dumps(jcfg, 0));
834 static int is_cfg_file(const struct dirent *dent) {
837 /* if the last four letters of the filename are .cfg, return true. */
838 if ((4 <= (n = strlen(dent->d_name))) &&
839 (0 == strcmp(&(dent->d_name[n-4]), ".cfg"))) {
843 /* otherwise, return false. */
847 int tr_find_config_files (struct dirent ***cfg_files) {
850 n = scandir(".", cfg_files, &is_cfg_file, 0);
854 fprintf(stderr, "tr_find_config: scandir error.\n");
859 fprintf (stderr, "tr_find_config: No config files found.\n");
865 fprintf(stderr, "tr_find_config: Config file found (%s).\n", (*cfg_files)[i]->d_name);