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 <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) {
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, char *ctype, json_t *jc, TR_CFG_RC *rc)
117 if ((!tr) || (!ctype) || (!jc) || (!rc) ||
118 (!json_is_array(jc)) ||
119 (0 >= json_array_size(jc)) ||
120 (TR_MAX_CONST_MATCHES < json_array_size(jc)) ||
121 (!json_is_string(json_array_get(jc, 0)))) {
122 fprintf(stderr, "tr_cfg_parse_one_constraint: config error.\n");
123 *rc = TR_CFG_NOPARSE;
127 if (NULL == (cons = malloc(sizeof(TR_CONSTRAINT)))) {
128 fprintf(stderr, "tr_cfg_parse_one_constraint: Out of memory (cons).\n");
133 memset(cons, 0, sizeof(TR_CONSTRAINT));
135 if (NULL == (cons->type = tr_new_name(ctype))) {
136 fprintf(stderr, "tr_cfg_parse_one_constraint: Out of memory (type).\n");
141 for (i = 0; i < json_array_size(jc); i++) {
142 cons->matches[i] = tr_new_name((char *)(json_string_value(json_array_get(jc, i))));
148 static TR_FILTER *tr_cfg_parse_one_filter (TR_INSTANCE *tr, json_t *jfilt, TR_CFG_RC *rc)
150 TR_FILTER *filt = NULL;
151 json_t *jftype = NULL;
153 json_t *jfaction = NULL;
154 json_t *jfspecs = NULL;
155 json_t *jffield = NULL;
156 json_t *jfmatch = NULL;
161 if ((NULL == (jftype = json_object_get(jfilt, "type"))) ||
162 (!json_is_string(jftype))) {
163 fprintf(stderr, "tr_cfg_parse_one_filter: Error parsing filter type.\n");
164 *rc = TR_CFG_NOPARSE;
168 if ((NULL == (jfls = json_object_get(jfilt, "filter_lines"))) ||
169 (!json_is_array(jfls))) {
170 fprintf(stderr, "tr_cfg_parse_one_filter: Error parsing filter type.\n");
171 *rc = TR_CFG_NOPARSE;
175 if (TR_MAX_FILTER_LINES < json_array_size(jfls)) {
176 fprintf(stderr, "tr_cfg_parse_one_filter: Filter has too many filter_lines, maximimum of %d.\n", TR_MAX_FILTER_LINES);
177 *rc = TR_CFG_NOPARSE;
181 if (NULL == (filt = malloc(sizeof(TR_FILTER)))) {
182 fprintf(stderr, "tr_config_parse_one_filter: Out of memory.\n");
187 memset(filt, 0, sizeof(TR_FILTER));
189 if (!strcmp(json_string_value(jftype), "rp_permitted")) {
190 filt->type = TR_FILTER_TYPE_RP_PERMITTED;
193 fprintf(stderr, "tr_cfg_parse_one_filter: Error parsing filter type, unknown type '%s'.\n", json_string_value(jftype));
194 *rc = TR_CFG_NOPARSE;
195 tr_filter_free(filt);
199 /* For each filter line... */
200 for (i = 0; i < json_array_size(jfls); i++) {
202 if ((NULL == (jfaction = json_object_get(json_array_get(jfls, i), "action"))) ||
203 (!json_is_string(jfaction))) {
204 fprintf(stderr, "tr_cfg_parse_one_filter: Error parsing filter action.\n");
205 *rc = TR_CFG_NOPARSE;
206 tr_filter_free(filt);
210 if ((NULL == (jfspecs = json_object_get(json_array_get(jfls, i), "filter_specs"))) ||
211 (!json_is_array(jfspecs)) ||
212 (0 == json_array_size(jfspecs))) {
213 fprintf(stderr, "tr_cfg_parse_one_filter: Error parsing filter specs.\n");
214 *rc = TR_CFG_NOPARSE;
215 tr_filter_free(filt);
219 if (TR_MAX_FILTER_SPECS < json_array_size(jfspecs)) {
220 fprintf(stderr, "tr_cfg_parse_one_filter: Filter has too many filter_specs, maximimum of %d.\n", TR_MAX_FILTER_SPECS);
221 *rc = TR_CFG_NOPARSE;
222 tr_filter_free(filt);
226 if (NULL == (filt->lines[i] = malloc(sizeof(TR_FLINE)))) {
227 fprintf(stderr, "tr_config_parse_one_filter: Out of memory (fline).\n");
229 tr_filter_free(filt);
233 memset(filt->lines[i], 0, sizeof(TR_FLINE));
235 if (!strcmp(json_string_value(jfaction), "accept")) {
236 filt->lines[i]->action = TR_FILTER_ACTION_ACCEPT;
238 else if (!strcmp(json_string_value(jfaction), "reject")) {
239 filt->lines[i]->action = TR_FILTER_ACTION_REJECT;
242 fprintf(stderr, "tr_cfg_parse_one_filter: Error parsing filter action, unknown action' %s'.\n", json_string_value(jfaction));
243 *rc = TR_CFG_NOPARSE;
244 tr_filter_free(filt);
248 if ((NULL != (jrc = json_object_get(json_array_get(jfls, i), "realm_constraints"))) &&
249 (json_is_array(jrc)) &&
250 (0 != json_array_size(jrc)) &&
251 (TR_MAX_CONST_MATCHES >= json_array_size(jrc))) {
253 if (NULL == (filt->lines[i]->realm_cons = tr_cfg_parse_one_constraint(tr, "realm", jrc, rc))) {
254 fprintf(stderr, "tr_cfg_parse_one_filter: Error parsing realm constraint");
255 tr_filter_free(filt);
260 if ((NULL != (jdc = json_object_get(json_array_get(jfls, i), "domain_constraints"))) &&
261 (json_is_array(jdc)) &&
262 (0 != json_array_size(jdc)) &&
263 (TR_MAX_CONST_MATCHES >= json_array_size(jdc))) {
265 if (NULL == (filt->lines[i]->domain_cons = tr_cfg_parse_one_constraint(tr, "domain", jdc, rc))) {
266 fprintf(stderr, "tr_cfg_parse_one_filter: Error parsing domain constraint");
267 tr_filter_free(filt);
272 /*For each filter spec within the filter line... */
273 for (j = 0; j <json_array_size(jfspecs); j++) {
275 if ((NULL == (jffield = json_object_get(json_array_get(jfspecs, j), "field"))) ||
276 (!json_is_string(jffield)) ||
277 (NULL == (jfmatch = json_object_get(json_array_get(jfspecs, j), "match"))) ||
278 (!json_is_string(jfmatch))) {
279 fprintf (stderr, "tr_cfg_parse_one_filter: Error parsing filter field and match for filter spec %d, filter line %d.\n", i, j);
280 *rc = TR_CFG_NOPARSE;
281 tr_filter_free(filt);
285 if (NULL == (filt->lines[i]->specs[j] = malloc(sizeof(TR_FSPEC)))) {
286 fprintf(stderr, "tr_config_parse_one_filter: Out of memory.\n");
288 tr_filter_free(filt);
292 memset(filt->lines[i]->specs[j], 0, sizeof(TR_FSPEC));
294 if ((NULL == (filt->lines[i]->specs[j]->field = tr_new_name((char *)json_string_value(jffield)))) ||
295 (NULL == (filt->lines[i]->specs[j]->match = tr_new_name((char *)json_string_value(jfmatch))))) {
296 fprintf(stderr, "tr_config_parse_one_filter: Out of memory.\n");
298 tr_filter_free(filt);
307 static TR_RP_CLIENT *tr_cfg_parse_one_rp_client (TR_INSTANCE *tr, json_t *jrp, TR_CFG_RC *rc)
309 TR_RP_CLIENT *rp = NULL;
311 json_t *jfilt = NULL;
312 json_t *jftype = NULL;
315 if ((!jrp) || (!rc)) {
316 fprintf(stderr, "tr_cfg_parse_one_rp_realm: Bad parameters.\n");
318 *rc = TR_CFG_BAD_PARAMS;
322 if ((NULL == (jgns = json_object_get(jrp, "gss_names"))) ||
323 (!json_is_array(jgns))) {
324 fprintf(stderr, "tr_cfg_parse_one_rp_client: Error parsing RP client configuration, no GSS names.\n");
325 *rc = TR_CFG_NOPARSE;
329 /* TBD -- Support more than one filter per RP client? */
330 if (NULL == (jfilt = json_object_get(jrp, "filter"))) {
331 fprintf(stderr, "tr_cfg_parse_one_rp_client: Error parsing RP client configuration, no filter.\n");
332 *rc = TR_CFG_NOPARSE;
336 /* We only support rp_permitted filters for RP clients */
337 if ((NULL == (jftype = json_object_get(jfilt, "type"))) ||
338 (!json_is_string(jftype)) ||
339 (strcmp(json_string_value(jftype), "rp_permitted"))) {
340 fprintf(stderr, "tr_cfg_parse_one_rp_client: Error parsing RP client filter type.\n");
341 *rc = TR_CFG_NOPARSE;
345 if (TR_MAX_GSS_NAMES < json_array_size(jgns)) {
346 fprintf(stderr, "tr_cfg_parse_one_rp_client: RP Client has too many GSS Names.\n");
347 *rc = TR_CFG_NOPARSE;
351 if (NULL == (rp = malloc(sizeof(TR_RP_CLIENT)))) {
352 fprintf(stderr, "tr_config_parse_one_rp_realm: Out of memory.\n");
357 memset(rp, 0, sizeof(TR_RP_CLIENT));
359 /* TBD -- support more than one filter entry per RP Client? */
360 if (NULL == (rp->filter = tr_cfg_parse_one_filter(tr, jfilt, rc))) {
361 fprintf(stderr, "tr_cfg_parse_one_rp_client: Error parsing filter.\n");
363 *rc = TR_CFG_NOPARSE;
367 for (i = 0; i < json_array_size(jgns); i++) {
368 if (NULL == (rp->gss_names[i] = tr_new_name ((char *)json_string_value(json_array_get(jgns, i))))) {
369 fprintf(stderr, "tr_cfg_parse_one_rp_client: No memory for GSS Name.\n");
379 static TR_CFG_RC tr_cfg_parse_rp_clients (TR_INSTANCE *tr, json_t *jcfg) {
381 TR_RP_CLIENT *rp = NULL;
382 TR_CFG_RC rc = TR_CFG_SUCCESS;
385 if ((!tr) || (!tr->new_cfg) || (!jcfg))
386 return TR_CFG_BAD_PARAMS;
388 if ((NULL == (jrps = json_object_get(jcfg, "rp_clients"))) ||
389 (!json_is_array(jrps))) {
390 return TR_CFG_NOPARSE;
393 for (i = 0; i < json_array_size(jrps); i++) {
394 if (NULL == (rp = tr_cfg_parse_one_rp_client(tr,
395 json_array_get(jrps, i),
399 fprintf(stderr, "tr_cfg_parse_rp_clients: RP client configured -- first gss: %s", rp->gss_names[0]->buf);
400 rp->next = tr->new_cfg->rp_clients;
401 tr->new_cfg->rp_clients = rp;
406 static TR_AAA_SERVER *tr_cfg_parse_one_aaa_server (TR_INSTANCE *tr, json_t *jaddr, TR_CFG_RC *rc) {
407 TR_AAA_SERVER *aaa = NULL;
409 if ((!tr) || (!tr->new_cfg) || (!jaddr) || (!json_is_string(jaddr))) {
410 fprintf(stderr, "tr_cfg_parse_one_aaa_server: Bad parameters.\n");
411 *rc = TR_CFG_BAD_PARAMS;
415 if (NULL == (aaa = malloc(sizeof(TR_AAA_SERVER)))) {
416 fprintf(stderr, "tr_config_parse_one_aaa_server: Out of memory.\n");
421 memset(aaa, 0, sizeof(TR_AAA_SERVER));
423 aaa->hostname = tr_new_name((char *)(json_string_value(jaddr)));
429 static TR_AAA_SERVER *tr_cfg_parse_aaa_servers (TR_INSTANCE *tr, json_t *jaaas, TR_CFG_RC *rc)
431 TR_AAA_SERVER *aaa = NULL;
432 TR_AAA_SERVER *temp_aaa = NULL;
435 for (i = 0; i < json_array_size(jaaas); i++) {
436 if (NULL == (temp_aaa = tr_cfg_parse_one_aaa_server(tr, json_array_get(jaaas, i), rc))) {
439 /* TBD -- IPv6 addresses */
440 // fprintf(stderr, "tr_cfg_parse_aaa_servers: Configuring AAA Server: ip_addr = %s.\n", inet_ntoa(temp_aaa->aaa_server_addr));
441 temp_aaa->next = aaa;
447 static TR_APC *tr_cfg_parse_apcs (TR_INSTANCE *tr, json_t *japcs, TR_CFG_RC *rc)
451 *rc = TR_CFG_SUCCESS; /* presume success */
453 if ((!japcs) || (!rc)) {
454 fprintf(stderr, "tr_cfg_parse_apcs: Bad parameters.\n");
456 *rc = TR_CFG_BAD_PARAMS;
460 if (NULL == (apc = malloc(sizeof(TR_APC)))) {
461 fprintf (stderr, "tr_cfg_parse_apcs: Out of memory.\n");
466 memset(apc, 0, sizeof(TR_APC));
468 /* TBD, deal with more than one APC. In the meantime, though... */
469 /* Only parse the first APC, because we only know how to deal with one, anyway. */
470 if (0 == json_array_size(japcs))
473 if (NULL == (apc->id = tr_new_name((char *)json_string_value(json_array_get(japcs, 0))))) {
474 fprintf(stderr, "tr_cfg_parse_apcs: No memory for APC name.\n");
482 static TR_IDP_REALM *tr_cfg_parse_one_idp_realm (TR_INSTANCE *tr, json_t *jidp, TR_CFG_RC *rc) {
483 TR_IDP_REALM *idp = NULL;
485 json_t *jscfg = NULL;
486 json_t *jsrvrs = NULL;
487 json_t *japcs = NULL;
489 if ((!jidp) || (!rc)) {
490 fprintf(stderr, "tr_cfg_parse_one_idp_realm: Bad parameters.\n");
492 *rc = TR_CFG_BAD_PARAMS;
496 if (NULL == (idp = malloc(sizeof(TR_IDP_REALM)))) {
497 fprintf(stderr, "tr_config_parse_one_idp_realm: Out of memory.\n");
502 memset(idp, 0, sizeof(TR_IDP_REALM));
504 if ((NULL == (jrid = json_object_get(jidp, "realm_id"))) ||
505 (!json_is_string(jrid)) ||
506 (NULL == (jscfg = json_object_get(jidp, "shared_config"))) ||
507 (!json_is_string(jscfg)) ||
508 (NULL == (jsrvrs = json_object_get(jidp, "aaa_servers"))) ||
509 (!json_is_array(jsrvrs))) {
510 fprintf(stderr, "tr_cfg_parse_one_idp_realm: Error parsing IDP realm configuration.\n");
512 *rc = TR_CFG_NOPARSE;
516 if (0 == strcmp(json_string_value(jscfg), "no")) {
517 idp->shared_config = 0;
519 idp->shared_config = 1;
522 if (NULL == (idp->realm_id = tr_new_name((char *)json_string_value(jrid)))) {
524 fprintf(stderr, "tr_cfg_parse_one_idp_realm: No memory for realm id.\n");
529 if (NULL == (idp->aaa_servers = tr_cfg_parse_aaa_servers(tr, jsrvrs, rc))) {
530 fprintf(stderr, "tr_cfg_parse_one_idp_realm: Can't parse AAA servers for realm %s.\n", idp->realm_id->buf);
531 tr_free_name(idp->realm_id);
536 if ((NULL != (japcs = json_object_get(jidp, "apcs"))) &&
537 (json_is_array(japcs))) {
538 if (NULL == (idp->apcs = tr_cfg_parse_apcs(tr, japcs, rc))) {
539 fprintf(stderr, "tr_cfg_parse_one_idp_realm: Can't parse APCs for realm %s .\n", idp->realm_id->buf);
540 tr_free_name(idp->realm_id);
541 /* TBD -- free aaa_servers */;
549 static TR_CFG_RC tr_cfg_parse_idp_realms (TR_INSTANCE *tr, json_t *jcfg)
551 json_t *jidps = NULL;
552 TR_CFG_RC rc = TR_CFG_SUCCESS;
553 TR_IDP_REALM *idp = NULL;
556 if ((!tr) || (!tr->new_cfg) || (!jcfg))
557 return TR_CFG_BAD_PARAMS;
559 if ((NULL == (jidps = json_object_get(jcfg, "idp_realms"))) ||
560 (!json_is_array(jidps))) {
561 return TR_CFG_NOPARSE;
564 for (i = 0; i < json_array_size(jidps); i++) {
565 if (NULL == (idp = tr_cfg_parse_one_idp_realm(tr,
566 json_array_get(jidps, i),
570 fprintf(stderr, "tr_cfg_parse_idp_realms: IDP realm configured: %s.\n", idp->realm_id->buf);
571 idp->next = tr->new_cfg->idp_realms;
572 tr->new_cfg->idp_realms = idp;
577 static TR_IDP_REALM *tr_cfg_parse_comm_idps (TR_INSTANCE *tr, json_t *jidps, TR_CFG_RC *rc)
579 TR_IDP_REALM *idp = NULL;
580 TR_IDP_REALM *temp_idp = NULL;
585 (!json_is_array(jidps))) {
587 *rc = TR_CFG_BAD_PARAMS;
591 for (i = 0; i < json_array_size(jidps); i++) {
592 if (NULL == (temp_idp = (tr_cfg_find_idp(tr->new_cfg,
593 tr_new_name((char *)json_string_value(json_array_get(jidps, i))),
595 fprintf(stderr, "tr_cfg_parse_comm_idps: Unknown IDP %s.\n",
596 (char *)json_string_value(json_array_get(jidps, i)));
600 temp_idp->comm_next = idp;
607 static TR_RP_REALM *tr_cfg_parse_comm_rps (TR_INSTANCE *tr, json_t *jrps, TR_CFG_RC *rc)
609 TR_RP_REALM *rp = NULL;
610 TR_RP_REALM *temp_rp = NULL;
615 (!json_is_array(jrps))) {
617 *rc = TR_CFG_BAD_PARAMS;
621 for (i = (json_array_size(jrps)-1); i >= 0; i--) {
622 if (NULL == (temp_rp = malloc(sizeof(TR_RP_REALM)))) {
623 fprintf(stderr, "tr_cfg_parse_comm_rps: Can't allocate memory for RP Realm.\n");
628 memset (temp_rp, 0, sizeof(TR_RP_REALM));
630 if (NULL == (temp_rp->realm_name = tr_new_name((char *)json_string_value(json_array_get(jrps, i))))) {
631 fprintf(stderr, "tr_cfg_parse_comm_rps: No memory for RP Realm Name.\n");
644 static TR_COMM *tr_cfg_parse_one_comm (TR_INSTANCE *tr, json_t *jcomm, TR_CFG_RC *rc) {
645 TR_COMM *comm = NULL;
647 json_t *jtype = NULL;
648 json_t *japcs = NULL;
649 json_t *jidps = NULL;
652 if ((!jcomm) || (!rc)) {
653 fprintf(stderr, "tr_cfg_parse_one_comm: Bad parameters.\n");
655 *rc = TR_CFG_BAD_PARAMS;
659 if (NULL == (comm = malloc(sizeof(TR_COMM)))) {
660 fprintf(stderr, "tr_config_parse_one_comm: Out of memory.\n");
665 memset(comm, 0, sizeof(TR_COMM));
667 if ((NULL == (jid = json_object_get(jcomm, "community_id"))) ||
668 (!json_is_string(jid)) ||
669 (NULL == (jtype = json_object_get(jcomm, "type"))) ||
670 (!json_is_string(jtype)) ||
671 (NULL == (japcs = json_object_get(jcomm, "apcs"))) ||
672 (!json_is_array(japcs)) ||
673 (NULL == (jidps = json_object_get(jcomm, "idp_realms"))) ||
674 (!json_is_array(jidps)) ||
675 (NULL == (jrps = json_object_get(jcomm, "rp_realms"))) ||
676 (!json_is_array(jrps))) {
677 fprintf(stderr, "tr_cfg_parse_one_comm: Error parsing Communities configuration.\n");
679 *rc = TR_CFG_NOPARSE;
683 if (NULL == (comm->id = tr_new_name((char *)json_string_value(jid)))) {
685 fprintf(stderr, "tr_cfg_parse_one_comm: No memory for community id.\n");
690 if (0 == strcmp(json_string_value(jtype), "apc")) {
691 comm->type = TR_COMM_APC;
692 } else if (0 == strcmp(json_string_value(jtype), "coi")) {
693 comm->type = TR_COMM_COI;
694 if (NULL == (comm->apcs = tr_cfg_parse_apcs(tr, japcs, rc))) {
695 fprintf(stderr, "tr_cfg_parse_one_comm: Can't parse APCs for COI %s.\n", comm->id->buf);
696 tr_free_name(comm->id);
701 fprintf(stderr, "tr_cfg_parse_one_comm: Invalid community type, comm = %s, type = %s\n", comm->id->buf, json_string_value(jtype));
702 tr_free_name(comm->id);
704 *rc = TR_CFG_NOPARSE;
708 comm->idp_realms = tr_cfg_parse_comm_idps(tr, jidps, rc);
709 if (TR_CFG_SUCCESS != *rc) {
710 fprintf(stderr, "tr_cfg_parse_one_comm: Can't parse IDP realms for comm %s.\n", comm->id->buf);
711 tr_free_name(comm->id);
716 comm->rp_realms = tr_cfg_parse_comm_rps(tr, jrps, rc);
717 if (TR_CFG_SUCCESS != *rc) {
718 fprintf(stderr, "tr_cfg_parse_comm: Can't parse RP realms for comm %s .\n", comm->id->buf);
719 tr_free_name(comm->id);
720 /* TBD -- free idps? */;
728 static TR_CFG_RC tr_cfg_parse_comms (TR_INSTANCE *tr, json_t *jcfg)
730 json_t *jcomms = NULL;
731 TR_CFG_RC rc = TR_CFG_SUCCESS;
732 TR_COMM *comm = NULL;
735 if ((!tr) || (!tr->new_cfg) || (!jcfg)) {
736 fprintf(stderr, "tr_config_parse_comms: Bad Parameters.\n");
737 return TR_CFG_BAD_PARAMS;
740 if (NULL == (comm = malloc(sizeof(TR_COMM)))) {
741 fprintf(stderr, "tr_cfg_parse_comms: Out of Memory\n");
744 memset (comm, 0, sizeof(TR_COMM));
746 if ((NULL == (jcomms = json_object_get(jcfg, "communities"))) ||
747 (!json_is_array(jcomms))) {
748 return TR_CFG_NOPARSE;
751 for (i = 0; i < json_array_size(jcomms); i++) {
752 if (NULL == (comm = tr_cfg_parse_one_comm(tr,
753 json_array_get(jcomms, i),
757 fprintf(stderr, "tr_cfg_parse_comms: Community configured: %s.\n", comm->id->buf);
758 comm->next = tr->new_cfg->comms;
759 tr->new_cfg->comms = comm;
764 TR_CFG_RC tr_parse_config (TR_INSTANCE *tr, json_t *jcfg) {
766 /* If there is a partial/abandoned config lying around, free it */
768 tr_cfg_free(tr->new_cfg);
771 if (NULL == (tr->new_cfg = malloc(sizeof(TR_CFG))))
774 memset(tr->new_cfg, 0, sizeof(TR_CFG));
776 if ((TR_CFG_SUCCESS != tr_cfg_parse_internal(tr, jcfg)) ||
777 (TR_CFG_SUCCESS != tr_cfg_parse_rp_clients(tr, jcfg)) ||
778 (TR_CFG_SUCCESS != tr_cfg_parse_idp_realms(tr, jcfg)) ||
779 (TR_CFG_SUCCESS != tr_cfg_parse_comms(tr, jcfg))) {
780 tr_cfg_free(tr->new_cfg);
783 return TR_CFG_SUCCESS;
786 TR_IDP_REALM *tr_cfg_find_idp (TR_CFG *tr_cfg, TR_NAME *idp_id, TR_CFG_RC *rc)
789 TR_IDP_REALM *cfg_idp;
791 if ((!tr_cfg) || (!idp_id)) {
793 *rc = TR_CFG_BAD_PARAMS;
797 for (cfg_idp = tr_cfg->idp_realms; NULL != cfg_idp; cfg_idp = cfg_idp->next) {
798 if (!tr_name_cmp (idp_id, cfg_idp->realm_id)) {
799 fprintf(stderr, "tr_cfg_find_idp: Found %s.\n", idp_id->buf);
803 /* if we didn't find one, return NULL */
807 TR_RP_CLIENT *tr_cfg_find_rp (TR_CFG *tr_cfg, TR_NAME *rp_gss, TR_CFG_RC *rc)
809 TR_RP_CLIENT *cfg_rp;
812 if ((!tr_cfg) || (!rp_gss)) {
814 *rc = TR_CFG_BAD_PARAMS;
818 for (cfg_rp = tr_cfg->rp_clients; NULL != cfg_rp; cfg_rp = cfg_rp->next) {
819 for (i = 0; i < TR_MAX_GSS_NAMES; i++) {
820 if (!tr_name_cmp (rp_gss, cfg_rp->gss_names[i])) {
821 fprintf(stderr, "tr_cfg_find_rp: Found %s.\n", rp_gss->buf);
826 /* if we didn't find one, return NULL */
830 json_t *tr_read_config (int n, struct dirent **cfg_files) {
839 fprintf(stderr, "tr_read_config: Parsing %s.\n", cfg_files[n]->d_name);
840 if (NULL == (temp = json_load_file(cfg_files[n]->d_name, JSON_DISABLE_EOF_CHECK, &err))) {
841 fprintf (stderr, "tr_read_config: Error parsing config file %s.\n", cfg_files[n]->d_name);
845 /* TBD -- instead of using json_object_update, iterate through files for non-overlap config? */
849 if (-1 == json_object_update(jcfg, temp)) {
850 fprintf(stderr, "tr_read_config: Error merging config information.\n");
856 fprintf(stderr, "tr_read_config: Merged configuration complete:\n%s\n", json_dumps(jcfg, 0));
861 static int is_cfg_file(const struct dirent *dent) {
864 /* if the last four letters of the filename are .cfg, return true. */
865 if ((4 <= (n = strlen(dent->d_name))) &&
866 (0 == strcmp(&(dent->d_name[n-4]), ".cfg"))) {
870 /* otherwise, return false. */
874 int tr_find_config_files (struct dirent ***cfg_files) {
877 n = scandir(".", cfg_files, &is_cfg_file, 0);
881 fprintf(stderr, "tr_find_config: scandir error.\n");
886 fprintf (stderr, "tr_find_config: No config files found.\n");
892 fprintf(stderr, "tr_find_config: Config file found (%s).\n", (*cfg_files)[i]->d_name);