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.
41 #include <tr_config.h>
43 #include <tr_filter.h>
44 #include <trust_router/tr_constraint.h>
46 void tr_print_config (FILE *stream, TR_CFG *cfg) {
47 fprintf(stream, "tr_print_config: Not yet implemented.\n");
51 void tr_cfg_free (TR_CFG *cfg) {
56 TR_CFG_RC tr_apply_new_config (TR_INSTANCE *tr) {
58 return TR_CFG_BAD_PARAMS;
61 tr_cfg_free(tr->active_cfg);
63 tr->active_cfg = tr->new_cfg;
64 return TR_CFG_SUCCESS;
67 static TR_CFG_RC tr_cfg_parse_internal (TR_CFG *trc, json_t *jcfg) {
71 json_t *jhname = NULL;
73 if ((!trc) || (!jcfg))
74 return TR_CFG_BAD_PARAMS;
76 if (NULL == trc->internal) {
77 if (NULL == (trc->internal = talloc(trc, TR_CFG_INTERNAL)))
80 memset(trc->internal, 0, sizeof(TR_CFG_INTERNAL));
83 if (NULL != (jint = json_object_get(jcfg, "tr_internal"))) {
84 if (NULL != (jmtd = json_object_get(jint, "max_tree_depth"))) {
85 if (json_is_number(jmtd)) {
86 trc->internal->max_tree_depth = json_integer_value(jmtd);
88 fprintf(stderr,"tr_cfg_parse_internal: Parsing error, max_tree_depth is not a number.\n");
89 return TR_CFG_NOPARSE;
92 /* If not configured, use the default */
93 trc->internal->max_tree_depth = TR_DEFAULT_MAX_TREE_DEPTH;
95 if (NULL != (jtp = json_object_get(jint, "tids_port"))) {
96 if (json_is_number(jtp)) {
97 trc->internal->tids_port = json_integer_value(jtp);
99 fprintf(stderr,"tr_cfg_parse_internal: Parsing error, port is not a number.\n");
100 return TR_CFG_NOPARSE;
103 /* If not configured, use the default */
104 trc->internal->tids_port = TR_DEFAULT_TIDS_PORT;
106 if (NULL != (jhname = json_object_get(jint, "hostname"))) {
107 if (json_is_string(jhname)) {
108 trc->internal->hostname = json_string_value(jhname);
110 fprintf(stderr,"tr_cfg_parse_internal: Parsing error, hostname is not a string.\n");
111 return TR_CFG_NOPARSE;
114 fprintf(stderr, "tr_cfg_parse_internal: Internal config parsed.\n");
115 return TR_CFG_SUCCESS;
117 return TR_CFG_SUCCESS;
120 static TR_CONSTRAINT *tr_cfg_parse_one_constraint (TR_CFG *trc, char *ctype, json_t *jc, TR_CFG_RC *rc)
125 if ((!trc) || (!ctype) || (!jc) || (!rc) ||
126 (!json_is_array(jc)) ||
127 (0 >= json_array_size(jc)) ||
128 (TR_MAX_CONST_MATCHES < json_array_size(jc)) ||
129 (!json_is_string(json_array_get(jc, 0)))) {
130 fprintf(stderr, "tr_cfg_parse_one_constraint: config error.\n");
131 *rc = TR_CFG_NOPARSE;
135 if (NULL == (cons = talloc(trc, TR_CONSTRAINT))) {
136 fprintf(stderr, "tr_cfg_parse_one_constraint: Out of memory (cons).\n");
141 memset(cons, 0, sizeof(TR_CONSTRAINT));
143 if (NULL == (cons->type = tr_new_name(ctype))) {
144 fprintf(stderr, "tr_cfg_parse_one_constraint: Out of memory (type).\n");
149 for (i = 0; i < json_array_size(jc); i++) {
150 cons->matches[i] = tr_new_name((char *)(json_string_value(json_array_get(jc, i))));
156 static TR_FILTER *tr_cfg_parse_one_filter (TR_CFG *trc, json_t *jfilt, TR_CFG_RC *rc)
158 TR_FILTER *filt = NULL;
159 json_t *jftype = NULL;
161 json_t *jfaction = NULL;
162 json_t *jfspecs = NULL;
163 json_t *jffield = NULL;
164 json_t *jfmatch = NULL;
169 if ((NULL == (jftype = json_object_get(jfilt, "type"))) ||
170 (!json_is_string(jftype))) {
171 fprintf(stderr, "tr_cfg_parse_one_filter: Error parsing filter type.\n");
172 *rc = TR_CFG_NOPARSE;
176 if ((NULL == (jfls = json_object_get(jfilt, "filter_lines"))) ||
177 (!json_is_array(jfls))) {
178 fprintf(stderr, "tr_cfg_parse_one_filter: Error parsing filter type.\n");
179 *rc = TR_CFG_NOPARSE;
183 if (TR_MAX_FILTER_LINES < json_array_size(jfls)) {
184 fprintf(stderr, "tr_cfg_parse_one_filter: Filter has too many filter_lines, maximimum of %d.\n", TR_MAX_FILTER_LINES);
185 *rc = TR_CFG_NOPARSE;
189 if (NULL == (filt = talloc(trc, TR_FILTER))) {
190 fprintf(stderr, "tr_cfg_parse_one_filter: Out of memory.\n");
195 memset(filt, 0, sizeof(TR_FILTER));
197 if (!strcmp(json_string_value(jftype), "rp_permitted")) {
198 filt->type = TR_FILTER_TYPE_RP_PERMITTED;
201 fprintf(stderr, "tr_cfg_parse_one_filter: Error parsing filter type, unknown type '%s'.\n", json_string_value(jftype));
202 *rc = TR_CFG_NOPARSE;
203 tr_filter_free(filt);
207 /* For each filter line... */
208 for (i = 0; i < json_array_size(jfls); i++) {
210 if ((NULL == (jfaction = json_object_get(json_array_get(jfls, i), "action"))) ||
211 (!json_is_string(jfaction))) {
212 fprintf(stderr, "tr_cfg_parse_one_filter: Error parsing filter action.\n");
213 *rc = TR_CFG_NOPARSE;
214 tr_filter_free(filt);
218 if ((NULL == (jfspecs = json_object_get(json_array_get(jfls, i), "filter_specs"))) ||
219 (!json_is_array(jfspecs)) ||
220 (0 == json_array_size(jfspecs))) {
221 fprintf(stderr, "tr_cfg_parse_one_filter: Error parsing filter specs.\n");
222 *rc = TR_CFG_NOPARSE;
223 tr_filter_free(filt);
227 if (TR_MAX_FILTER_SPECS < json_array_size(jfspecs)) {
228 fprintf(stderr, "tr_cfg_parse_one_filter: Filter has too many filter_specs, maximimum of %d.\n", TR_MAX_FILTER_SPECS);
229 *rc = TR_CFG_NOPARSE;
230 tr_filter_free(filt);
234 if (NULL == (filt->lines[i] = talloc(trc, TR_FLINE))) {
235 fprintf(stderr, "tr_cfg_parse_one_filter: Out of memory (fline).\n");
237 tr_filter_free(filt);
241 memset(filt->lines[i], 0, sizeof(TR_FLINE));
243 if (!strcmp(json_string_value(jfaction), "accept")) {
244 filt->lines[i]->action = TR_FILTER_ACTION_ACCEPT;
246 else if (!strcmp(json_string_value(jfaction), "reject")) {
247 filt->lines[i]->action = TR_FILTER_ACTION_REJECT;
250 fprintf(stderr, "tr_cfg_parse_one_filter: Error parsing filter action, unknown action' %s'.\n", json_string_value(jfaction));
251 *rc = TR_CFG_NOPARSE;
252 tr_filter_free(filt);
256 if ((NULL != (jrc = json_object_get(json_array_get(jfls, i), "realm_constraints"))) &&
257 (json_is_array(jrc)) &&
258 (0 != json_array_size(jrc)) &&
259 (TR_MAX_CONST_MATCHES >= json_array_size(jrc))) {
261 if (NULL == (filt->lines[i]->realm_cons = tr_cfg_parse_one_constraint(trc, "realm", jrc, rc))) {
262 fprintf(stderr, "tr_cfg_parse_one_filter: Error parsing realm constraint");
263 tr_filter_free(filt);
268 if ((NULL != (jdc = json_object_get(json_array_get(jfls, i), "domain_constraints"))) &&
269 (json_is_array(jdc)) &&
270 (0 != json_array_size(jdc)) &&
271 (TR_MAX_CONST_MATCHES >= json_array_size(jdc))) {
273 if (NULL == (filt->lines[i]->domain_cons = tr_cfg_parse_one_constraint(trc, "domain", jdc, rc))) {
274 fprintf(stderr, "tr_cfg_parse_one_filter: Error parsing domain constraint");
275 tr_filter_free(filt);
280 /*For each filter spec within the filter line... */
281 for (j = 0; j <json_array_size(jfspecs); j++) {
283 if ((NULL == (jffield = json_object_get(json_array_get(jfspecs, j), "field"))) ||
284 (!json_is_string(jffield)) ||
285 (NULL == (jfmatch = json_object_get(json_array_get(jfspecs, j), "match"))) ||
286 (!json_is_string(jfmatch))) {
287 fprintf (stderr, "tr_cfg_parse_one_filter: Error parsing filter field and match for filter spec %d, filter line %d.\n", i, j);
288 *rc = TR_CFG_NOPARSE;
289 tr_filter_free(filt);
293 if (NULL == (filt->lines[i]->specs[j] = talloc(trc, TR_FSPEC))) {
294 fprintf(stderr, "tr_cfg_parse_one_filter: Out of memory.\n");
296 tr_filter_free(filt);
300 memset(filt->lines[i]->specs[j], 0, sizeof(TR_FSPEC));
302 if ((NULL == (filt->lines[i]->specs[j]->field = tr_new_name((char *)json_string_value(jffield)))) ||
303 (NULL == (filt->lines[i]->specs[j]->match = tr_new_name((char *)json_string_value(jfmatch))))) {
304 fprintf(stderr, "tr_cfg_parse_one_filter: Out of memory.\n");
306 tr_filter_free(filt);
315 static TR_RP_CLIENT *tr_cfg_parse_one_rp_client (TR_CFG *trc, json_t *jrp, TR_CFG_RC *rc)
317 TR_RP_CLIENT *rp = NULL;
319 json_t *jfilt = NULL;
320 json_t *jftype = NULL;
323 if ((!trc) || (!jrp) || (!rc)) {
324 fprintf(stderr, "tr_cfg_parse_one_rp_realm: Bad parameters.\n");
326 *rc = TR_CFG_BAD_PARAMS;
330 if ((NULL == (jgns = json_object_get(jrp, "gss_names"))) ||
331 (!json_is_array(jgns))) {
332 fprintf(stderr, "tr_cfg_parse_one_rp_client: Error parsing RP client configuration, no GSS names.\n");
333 *rc = TR_CFG_NOPARSE;
337 /* TBD -- Support more than one filter per RP client? */
338 if (NULL == (jfilt = json_object_get(jrp, "filter"))) {
339 fprintf(stderr, "tr_cfg_parse_one_rp_client: Error parsing RP client configuration, no filter.\n");
340 *rc = TR_CFG_NOPARSE;
344 /* We only support rp_permitted filters for RP clients */
345 if ((NULL == (jftype = json_object_get(jfilt, "type"))) ||
346 (!json_is_string(jftype)) ||
347 (strcmp(json_string_value(jftype), "rp_permitted"))) {
348 fprintf(stderr, "tr_cfg_parse_one_rp_client: Error parsing RP client filter type.\n");
349 *rc = TR_CFG_NOPARSE;
353 if (TR_MAX_GSS_NAMES < json_array_size(jgns)) {
354 fprintf(stderr, "tr_cfg_parse_one_rp_client: RP Client has too many GSS Names.\n");
355 *rc = TR_CFG_NOPARSE;
359 if (NULL == (rp = talloc(trc, TR_RP_CLIENT))) {
360 fprintf(stderr, "tr_cfg_parse_one_rp_realm: Out of memory.\n");
365 memset(rp, 0, sizeof(TR_RP_CLIENT));
367 /* TBD -- support more than one filter entry per RP Client? */
368 if (NULL == (rp->filter = tr_cfg_parse_one_filter(trc, jfilt, rc))) {
369 fprintf(stderr, "tr_cfg_parse_one_rp_client: Error parsing filter.\n");
371 *rc = TR_CFG_NOPARSE;
375 for (i = 0; i < json_array_size(jgns); i++) {
376 if (NULL == (rp->gss_names[i] = tr_new_name ((char *)json_string_value(json_array_get(jgns, i))))) {
377 fprintf(stderr, "tr_cfg_parse_one_rp_client: No memory for GSS Name.\n");
387 static TR_CFG_RC tr_cfg_parse_rp_clients (TR_CFG *trc, json_t *jcfg) {
389 TR_RP_CLIENT *rp = NULL;
390 TR_CFG_RC rc = TR_CFG_SUCCESS;
393 if ((!trc) || (!jcfg))
394 return TR_CFG_BAD_PARAMS;
396 if (NULL != (jrps = json_object_get(jcfg, "rp_clients"))) {
398 if (!json_is_array(jrps)) {
399 return TR_CFG_NOPARSE;
402 for (i = 0; i < json_array_size(jrps); i++) {
403 if (NULL == (rp = tr_cfg_parse_one_rp_client(trc,
404 json_array_get(jrps, i),
408 fprintf(stderr, "tr_cfg_parse_rp_clients: RP client configured -- first gss: %s", rp->gss_names[0]->buf);
409 rp->next = trc->rp_clients;
410 trc->rp_clients = rp;
416 static TR_AAA_SERVER *tr_cfg_parse_one_aaa_server (TR_CFG *trc, json_t *jaddr, TR_CFG_RC *rc) {
417 TR_AAA_SERVER *aaa = NULL;
419 if ((!trc) || (!jaddr) || (!json_is_string(jaddr))) {
420 fprintf(stderr, "tr_cfg_parse_one_aaa_server: Bad parameters.\n");
421 *rc = TR_CFG_BAD_PARAMS;
425 if (NULL == (aaa = talloc(trc, TR_AAA_SERVER))) {
426 fprintf(stderr, "tr_cfg_parse_one_aaa_server: Out of memory.\n");
431 memset(aaa, 0, sizeof(TR_AAA_SERVER));
433 aaa->hostname = tr_new_name((char *)(json_string_value(jaddr)));
438 static TR_AAA_SERVER *tr_cfg_parse_aaa_servers (TR_CFG *trc, json_t *jaaas, TR_CFG_RC *rc)
440 TR_AAA_SERVER *aaa = NULL;
441 TR_AAA_SERVER *temp_aaa = NULL;
444 for (i = 0; i < json_array_size(jaaas); i++) {
445 if (NULL == (temp_aaa = tr_cfg_parse_one_aaa_server(trc, json_array_get(jaaas, i), rc))) {
448 /* TBD -- IPv6 addresses */
449 // fprintf(stderr, "tr_cfg_parse_aaa_servers: Configuring AAA Server: ip_addr = %s.\n", inet_ntoa(temp_aaa->aaa_server_addr));
450 temp_aaa->next = aaa;
456 static TR_APC *tr_cfg_parse_apcs (TR_CFG *trc, json_t *japcs, TR_CFG_RC *rc)
460 *rc = TR_CFG_SUCCESS; /* presume success */
462 if ((!trc) || (!japcs) || (!rc)) {
463 fprintf(stderr, "tr_cfg_parse_apcs: Bad parameters.\n");
465 *rc = TR_CFG_BAD_PARAMS;
469 if (NULL == (apc = talloc(trc, TR_APC))) {
470 fprintf (stderr, "tr_cfg_parse_apcs: Out of memory.\n");
475 memset(apc, 0, sizeof(TR_APC));
477 /* TBD, deal with more than one APC. In the meantime, though... */
478 /* Only parse the first APC, because we only know how to deal with one, anyway. */
479 if (0 == json_array_size(japcs))
482 if (NULL == (apc->id = tr_new_name((char *)json_string_value(json_array_get(japcs, 0))))) {
483 fprintf(stderr, "tr_cfg_parse_apcs: No memory for APC name.\n");
491 static TR_IDP_REALM *tr_cfg_parse_one_idp_realm (TR_CFG *trc, json_t *jidp, TR_CFG_RC *rc) {
492 TR_IDP_REALM *idp = NULL;
494 json_t *jscfg = NULL;
495 json_t *jsrvrs = NULL;
496 json_t *japcs = NULL;
498 if ((!trc) || (!jidp) || (!rc)) {
499 fprintf(stderr, "tr_cfg_parse_one_idp_realm: Bad parameters.\n");
501 *rc = TR_CFG_BAD_PARAMS;
505 if (NULL == (idp = talloc(trc, TR_IDP_REALM))) {
506 fprintf(stderr, "tr_cfg_parse_one_idp_realm: Out of memory.\n");
511 memset(idp, 0, sizeof(TR_IDP_REALM));
513 if ((NULL == (jrid = json_object_get(jidp, "realm_id"))) ||
514 (!json_is_string(jrid)) ||
515 (NULL == (jscfg = json_object_get(jidp, "shared_config"))) ||
516 (!json_is_string(jscfg)) ||
517 (NULL == (jsrvrs = json_object_get(jidp, "aaa_servers"))) ||
518 (!json_is_array(jsrvrs))) {
519 fprintf(stderr, "tr_cfg_parse_one_idp_realm: Error parsing IDP realm configuration.\n");
521 *rc = TR_CFG_NOPARSE;
525 if (0 == strcmp(json_string_value(jscfg), "no")) {
526 idp->shared_config = 0;
528 idp->shared_config = 1;
531 if (NULL == (idp->realm_id = tr_new_name((char *)json_string_value(jrid)))) {
533 fprintf(stderr, "tr_cfg_parse_one_idp_realm: No memory for realm id.\n");
538 if (NULL == (idp->aaa_servers = tr_cfg_parse_aaa_servers(trc, jsrvrs, rc))) {
539 fprintf(stderr, "tr_cfg_parse_one_idp_realm: Can't parse AAA servers for realm %s.\n", idp->realm_id->buf);
540 tr_free_name(idp->realm_id);
545 if ((NULL != (japcs = json_object_get(jidp, "apcs"))) &&
546 (json_is_array(japcs))) {
547 if (NULL == (idp->apcs = tr_cfg_parse_apcs(trc, japcs, rc))) {
548 fprintf(stderr, "tr_cfg_parse_one_idp_realm: Can't parse APCs for realm %s .\n", idp->realm_id->buf);
549 tr_free_name(idp->realm_id);
550 /* TBD -- free aaa_servers */;
558 static TR_CFG_RC tr_cfg_parse_default_servers (TR_CFG *trc, json_t *jcfg)
561 TR_CFG_RC rc = TR_CFG_SUCCESS;
562 TR_AAA_SERVER *ds = NULL;
565 if ((!trc) || (!jcfg))
566 return TR_CFG_BAD_PARAMS;
568 /* If there are default servers, store them */
569 if ((NULL != (jdss = json_object_get(jcfg, "default_servers"))) &&
570 (json_is_array(jdss)) &&
571 (0 < json_array_size(jdss))) {
573 for (i = 0; i < json_array_size(jdss); i++) {
574 if (NULL == (ds = tr_cfg_parse_one_aaa_server(trc,
575 json_array_get(jdss, i),
579 fprintf(stderr, "tr_cfg_parse_default_servers: Default server configured.\n");
580 ds->next = trc->default_servers;
581 trc->default_servers = ds;
588 static TR_CFG_RC tr_cfg_parse_idp_realms (TR_CFG *trc, json_t *jcfg)
590 json_t *jidps = NULL;
591 TR_CFG_RC rc = TR_CFG_SUCCESS;
592 TR_IDP_REALM *idp = NULL;
595 if ((!trc) || (!jcfg))
596 return TR_CFG_BAD_PARAMS;
598 /* If there are any IDP Realms, parse them */
599 if ((NULL != (jidps = json_object_get(jcfg, "idp_realms"))) &&
600 (json_is_array(jidps))) {
601 for (i = 0; i < json_array_size(jidps); i++) {
602 if (NULL == (idp = tr_cfg_parse_one_idp_realm(trc,
603 json_array_get(jidps, i),
607 fprintf(stderr, "tr_cfg_parse_idp_realms: IDP realm configured: %s.\n", idp->realm_id->buf);
608 idp->next = trc->idp_realms;
609 trc->idp_realms = idp;
616 static TR_IDP_REALM *tr_cfg_parse_comm_idps (TR_CFG *trc, json_t *jidps, TR_CFG_RC *rc)
618 TR_IDP_REALM *idp = NULL;
619 TR_IDP_REALM *temp_idp = NULL;
624 (!json_is_array(jidps))) {
626 *rc = TR_CFG_BAD_PARAMS;
630 for (i = 0; i < json_array_size(jidps); i++) {
631 if (NULL == (temp_idp = (tr_cfg_find_idp(trc,
632 tr_new_name((char *)json_string_value(json_array_get(jidps, i))),
634 fprintf(stderr, "tr_cfg_parse_comm_idps: Unknown IDP %s.\n",
635 (char *)json_string_value(json_array_get(jidps, i)));
639 temp_idp->comm_next = idp;
646 static TR_RP_REALM *tr_cfg_parse_comm_rps (TR_CFG *trc, json_t *jrps, TR_CFG_RC *rc)
648 TR_RP_REALM *rp = NULL;
649 TR_RP_REALM *temp_rp = NULL;
654 (!json_is_array(jrps))) {
656 *rc = TR_CFG_BAD_PARAMS;
660 for (i = (json_array_size(jrps)-1); i >= 0; i--) {
661 if (NULL == (temp_rp = talloc(trc, TR_RP_REALM))) {
662 fprintf(stderr, "tr_cfg_parse_comm_rps: Can't allocate memory for RP Realm.\n");
667 memset (temp_rp, 0, sizeof(TR_RP_REALM));
669 if (NULL == (temp_rp->realm_name = tr_new_name((char *)json_string_value(json_array_get(jrps, i))))) {
670 fprintf(stderr, "tr_cfg_parse_comm_rps: No memory for RP Realm Name.\n");
683 static TR_COMM *tr_cfg_parse_one_comm (TR_CFG *trc, json_t *jcomm, TR_CFG_RC *rc) {
684 TR_COMM *comm = NULL;
686 json_t *jtype = NULL;
687 json_t *japcs = NULL;
688 json_t *jidps = NULL;
691 if ((!trc) || (!jcomm) || (!rc)) {
692 fprintf(stderr, "tr_cfg_parse_one_comm: Bad parameters.\n");
694 *rc = TR_CFG_BAD_PARAMS;
698 if (NULL == (comm = talloc(trc, TR_COMM))) {
699 fprintf(stderr, "tr_cfg_parse_one_comm: Out of memory.\n");
704 memset(comm, 0, sizeof(TR_COMM));
706 if ((NULL == (jid = json_object_get(jcomm, "community_id"))) ||
707 (!json_is_string(jid)) ||
708 (NULL == (jtype = json_object_get(jcomm, "type"))) ||
709 (!json_is_string(jtype)) ||
710 (NULL == (japcs = json_object_get(jcomm, "apcs"))) ||
711 (!json_is_array(japcs)) ||
712 (NULL == (jidps = json_object_get(jcomm, "idp_realms"))) ||
713 (!json_is_array(jidps)) ||
714 (NULL == (jrps = json_object_get(jcomm, "rp_realms"))) ||
715 (!json_is_array(jrps))) {
716 fprintf(stderr, "tr_cfg_parse_one_comm: Error parsing Communities configuration.\n");
718 *rc = TR_CFG_NOPARSE;
722 if (NULL == (comm->id = tr_new_name((char *)json_string_value(jid)))) {
724 fprintf(stderr, "tr_cfg_parse_one_comm: No memory for community id.\n");
729 if (0 == strcmp(json_string_value(jtype), "apc")) {
730 comm->type = TR_COMM_APC;
731 } else if (0 == strcmp(json_string_value(jtype), "coi")) {
732 comm->type = TR_COMM_COI;
733 if (NULL == (comm->apcs = tr_cfg_parse_apcs(trc, japcs, rc))) {
734 fprintf(stderr, "tr_cfg_parse_one_comm: Can't parse APCs for COI %s.\n", comm->id->buf);
735 tr_free_name(comm->id);
740 fprintf(stderr, "tr_cfg_parse_one_comm: Invalid community type, comm = %s, type = %s\n", comm->id->buf, json_string_value(jtype));
741 tr_free_name(comm->id);
743 *rc = TR_CFG_NOPARSE;
747 comm->idp_realms = tr_cfg_parse_comm_idps(trc, jidps, rc);
748 if (TR_CFG_SUCCESS != *rc) {
749 fprintf(stderr, "tr_cfg_parse_one_comm: Can't parse IDP realms for comm %s.\n", comm->id->buf);
750 tr_free_name(comm->id);
755 comm->rp_realms = tr_cfg_parse_comm_rps(trc, jrps, rc);
756 if (TR_CFG_SUCCESS != *rc) {
757 fprintf(stderr, "tr_cfg_parse_comm: Can't parse RP realms for comm %s .\n", comm->id->buf);
758 tr_free_name(comm->id);
759 /* TBD -- free idps? */;
767 static TR_CFG_RC tr_cfg_parse_comms (TR_CFG *trc, json_t *jcfg)
769 json_t *jcomms = NULL;
770 TR_CFG_RC rc = TR_CFG_SUCCESS;
771 TR_COMM *comm = NULL;
774 if ((!trc) || (!jcfg)) {
775 fprintf(stderr, "tr_cfg_parse_comms: Bad Parameters.\n");
776 return TR_CFG_BAD_PARAMS;
779 if (NULL != (jcomms = json_object_get(jcfg, "communities"))) {
780 if (!json_is_array(jcomms)) {
781 return TR_CFG_NOPARSE;
784 for (i = 0; i < json_array_size(jcomms); i++) {
785 if (NULL == (comm = tr_cfg_parse_one_comm(trc,
786 json_array_get(jcomms, i),
790 fprintf(stderr, "tr_cfg_parse_comms: Community configured: %s.\n", comm->id->buf);
791 comm->next = trc->comms;
798 TR_CFG_RC tr_cfg_validate (TR_CFG *trc) {
799 TR_CFG_RC rc = TR_CFG_SUCCESS;
802 return TR_CFG_BAD_PARAMS;
804 if ((NULL == trc->internal)||
805 (NULL == trc->internal->hostname)) {
806 fprintf(stderr, "tr_cfg_validate: Error: No internal configuration, or no hostname.\n");
810 if (NULL == trc->rp_clients) {
811 fprintf(stderr, "tr_cfg_validate: Error: No RP Clients configured\n");
815 if (NULL == trc->comms) {
816 fprintf(stderr, "tr_cfg_validate: Error: No Communities configured\n");
820 if ((NULL == trc->default_servers) && (NULL == trc->idp_realms)) {
821 fprintf(stderr, "tr_cfg_validate: Error: No default servers or IDPs configured.\n");
828 TR_CFG_RC tr_parse_config (TR_INSTANCE *tr, struct dirent **cfg_files) {
833 if ((!tr) || (!cfg_files))
834 return TR_CFG_BAD_PARAMS;
836 /* If there is a partial/abandoned config lying around, free it */
838 tr_cfg_free(tr->new_cfg);
840 if (NULL == (tr->new_cfg = talloc(NULL, TR_CFG)))
843 memset(tr->new_cfg, 0, sizeof(TR_CFG));
845 /* Parse configuration information from each config file */
847 fprintf(stderr, "tr_read_config: Parsing %s.\n", cfg_files[n]->d_name);
848 if (NULL == (jcfg = json_load_file(cfg_files[n]->d_name,
849 JSON_DISABLE_EOF_CHECK, &rc))) {
850 fprintf (stderr, "tr_read_config: Error parsing config file %s.\n",
851 cfg_files[n]->d_name);
852 return TR_CFG_NOPARSE;
855 if ((TR_CFG_SUCCESS != tr_cfg_parse_internal(tr->new_cfg, jcfg)) ||
856 (TR_CFG_SUCCESS != tr_cfg_parse_rp_clients(tr->new_cfg, jcfg)) ||
857 (TR_CFG_SUCCESS != tr_cfg_parse_idp_realms(tr->new_cfg, jcfg)) ||
858 (TR_CFG_SUCCESS != tr_cfg_parse_default_servers(tr->new_cfg, jcfg)) ||
859 (TR_CFG_SUCCESS != tr_cfg_parse_comms(tr->new_cfg, jcfg))) {
860 tr_cfg_free(tr->new_cfg);
865 /* make sure we got a complete, consistent configuration */
866 if (TR_CFG_SUCCESS != tr_cfg_validate(tr->new_cfg)) {
867 fprintf(stderr, "tr_parse_config: Error: INVALID CONFIGURATION, EXITING\n");
871 return TR_CFG_SUCCESS;
874 TR_IDP_REALM *tr_cfg_find_idp (TR_CFG *tr_cfg, TR_NAME *idp_id, TR_CFG_RC *rc)
877 TR_IDP_REALM *cfg_idp;
879 if ((!tr_cfg) || (!idp_id)) {
881 *rc = TR_CFG_BAD_PARAMS;
885 for (cfg_idp = tr_cfg->idp_realms; NULL != cfg_idp; cfg_idp = cfg_idp->next) {
886 if (!tr_name_cmp (idp_id, cfg_idp->realm_id)) {
887 fprintf(stderr, "tr_cfg_find_idp: Found %s.\n", idp_id->buf);
891 /* if we didn't find one, return NULL */
895 TR_RP_CLIENT *tr_cfg_find_rp (TR_CFG *tr_cfg, TR_NAME *rp_gss, TR_CFG_RC *rc)
897 TR_RP_CLIENT *cfg_rp;
900 if ((!tr_cfg) || (!rp_gss)) {
902 *rc = TR_CFG_BAD_PARAMS;
906 for (cfg_rp = tr_cfg->rp_clients; NULL != cfg_rp; cfg_rp = cfg_rp->next) {
907 for (i = 0; i < TR_MAX_GSS_NAMES; i++) {
908 if (!tr_name_cmp (rp_gss, cfg_rp->gss_names[i])) {
909 fprintf(stderr, "tr_cfg_find_rp: Found %s.\n", rp_gss->buf);
914 /* if we didn't find one, return NULL */
919 json_t *tr_read_config (int n, struct dirent **cfg_files) {
928 fprintf(stderr, "tr_read_config: Parsing %s.\n", cfg_files[n]->d_name);
929 if (NULL == (temp = json_load_file(cfg_files[n]->d_name, JSON_DISABLE_EOF_CHECK, &err))) {
930 fprintf (stderr, "tr_read_config: Error parsing config file %s.\n", cfg_files[n]->d_name);
937 if (-1 == json_object_update(jcfg, temp)) {
938 fprintf(stderr, "tr_read_config: Error merging config information.\n");
944 fprintf(stderr, "tr_read_config: Merged configuration complete:\n%s\n", json_dumps(jcfg, 0));
950 static int is_cfg_file(const struct dirent *dent) {
953 /* if the last four letters of the filename are .cfg, return true. */
954 if ((4 <= (n = strlen(dent->d_name))) &&
955 (0 == strcmp(&(dent->d_name[n-4]), ".cfg"))) {
959 /* otherwise, return false. */
963 int tr_find_config_files (struct dirent ***cfg_files) {
966 n = scandir(".", cfg_files, &is_cfg_file, 0);
970 fprintf(stderr, "tr_find_config: scandir error.\n");
975 fprintf (stderr, "tr_find_config: No config files found.\n");
981 fprintf(stderr, "tr_find_config: Config file found (%s).\n", (*cfg_files)[i]->d_name);