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>
44 #include <tr_filter.h>
45 #include <trust_router/tr_constraint.h>
47 void tr_print_config (FILE *stream, TR_CFG *cfg) {
48 fprintf(stream, "tr_print_config: Not yet implemented.");
52 TR_CFG *tr_cfg_new(TALLOC_CTX *mem_ctx)
54 return talloc_zero(mem_ctx, TR_CFG);
57 void tr_cfg_free (TR_CFG *cfg) {
62 TR_CFG_RC tr_apply_new_config (TR_CFG **active_cfg,
65 if ((active_cfg==NULL) || (new_cfg==NULL))
66 return TR_CFG_BAD_PARAMS;
68 if (*active_cfg != NULL)
69 tr_cfg_free(*active_cfg);
71 *active_cfg = *new_cfg;
72 *new_cfg=NULL; /* only keep a single handle on the new configuration */
74 tr_log_threshold((*active_cfg)->internal->log_threshold);
75 tr_console_threshold((*active_cfg)->internal->console_threshold);
77 return TR_CFG_SUCCESS;
80 static TR_CFG_RC tr_cfg_parse_internal (TR_CFG *trc, json_t *jcfg) {
84 json_t *jhname = NULL;
86 json_t *jconthres = NULL;
87 json_t *jlogthres = NULL;
88 json_t *jcfgpoll = NULL;
89 json_t *jcfgsettle = NULL;
91 if ((!trc) || (!jcfg))
92 return TR_CFG_BAD_PARAMS;
94 if (NULL == trc->internal) {
95 if (NULL == (trc->internal = talloc_zero(trc, TR_CFG_INTERNAL)))
99 if (NULL != (jint = json_object_get(jcfg, "tr_internal"))) {
100 if (NULL != (jmtd = json_object_get(jint, "max_tree_depth"))) {
101 if (json_is_number(jmtd)) {
102 trc->internal->max_tree_depth = json_integer_value(jmtd);
104 tr_debug("tr_cfg_parse_internal: Parsing error, max_tree_depth is not a number.");
105 return TR_CFG_NOPARSE;
108 /* If not configured, use the default */
109 trc->internal->max_tree_depth = TR_DEFAULT_MAX_TREE_DEPTH;
111 if (NULL != (jtp = json_object_get(jint, "tids_port"))) {
112 if (json_is_number(jtp)) {
113 trc->internal->tids_port = json_integer_value(jtp);
115 tr_debug("tr_cfg_parse_internal: Parsing error, port is not a number.");
116 return TR_CFG_NOPARSE;
119 /* If not configured, use the default */
120 trc->internal->tids_port = TR_DEFAULT_TIDS_PORT;
122 if (NULL != (jhname = json_object_get(jint, "hostname"))) {
123 if (json_is_string(jhname)) {
124 trc->internal->hostname = json_string_value(jhname);
126 tr_debug("tr_cfg_parse_internal: Parsing error, hostname is not a string.");
127 return TR_CFG_NOPARSE;
130 if (NULL != (jcfgpoll = json_object_get(jint, "cfg_poll_interval"))) {
131 if (json_is_number(jcfgpoll)) {
132 trc->internal->cfg_poll_interval = json_integer_value(jcfgpoll);
134 tr_debug("tr_cfg_parse_internal: Parsing error, cfg_poll_interval is not a number.");
135 return TR_CFG_NOPARSE;
138 if (NULL != (jcfgsettle = json_object_get(jint, "cfg_settle_count"))) {
139 if (json_is_number(jcfgsettle)) {
140 trc->internal->cfg_settle_count = json_integer_value(jcfgsettle);
142 tr_debug("tr_cfg_parse_internal: Parsing error, cfg_settle_count is not a number.");
143 return TR_CFG_NOPARSE;
147 if (NULL != (jlog = json_object_get(jint, "logging"))) {
148 if (NULL != (jlogthres = json_object_get(jlog, "log_threshold"))) {
149 if (json_is_string(jlogthres)) {
150 trc->internal->log_threshold = str2sev(json_string_value(jlogthres));
152 tr_debug("tr_cfg_parse_internal: Parsing error, log_threshold is not a string.");
153 return TR_CFG_NOPARSE;
156 /* If not configured, use the default */
157 trc->internal->log_threshold = TR_DEFAULT_LOG_THRESHOLD;
160 if (NULL != (jconthres = json_object_get(jlog, "console_threshold"))) {
161 if (json_is_string(jconthres)) {
162 trc->internal->console_threshold = str2sev(json_string_value(jconthres));
164 tr_debug("tr_cfg_parse_internal: Parsing error, console_threshold is not a string.");
165 return TR_CFG_NOPARSE;
168 /* If not configured, use the default */
169 trc->internal->console_threshold = TR_DEFAULT_CONSOLE_THRESHOLD;
172 /* If not configured, use the default */
173 trc->internal->console_threshold = TR_DEFAULT_CONSOLE_THRESHOLD;
174 trc->internal->log_threshold = TR_DEFAULT_LOG_THRESHOLD;
177 tr_debug("tr_cfg_parse_internal: Internal config parsed.");
178 return TR_CFG_SUCCESS;
180 return TR_CFG_SUCCESS;
183 static TR_CONSTRAINT *tr_cfg_parse_one_constraint (TR_CFG *trc, char *ctype, json_t *jc, TR_CFG_RC *rc)
188 if ((!trc) || (!ctype) || (!jc) || (!rc) ||
189 (!json_is_array(jc)) ||
190 (0 >= json_array_size(jc)) ||
191 (TR_MAX_CONST_MATCHES < json_array_size(jc)) ||
192 (!json_is_string(json_array_get(jc, 0)))) {
193 tr_debug("tr_cfg_parse_one_constraint: config error.");
194 *rc = TR_CFG_NOPARSE;
198 if (NULL == (cons = talloc_zero(trc, TR_CONSTRAINT))) {
199 tr_debug("tr_cfg_parse_one_constraint: Out of memory (cons).");
204 if (NULL == (cons->type = tr_new_name(ctype))) {
205 tr_debug("tr_cfg_parse_one_constraint: Out of memory (type).");
210 for (i = 0; i < json_array_size(jc); i++) {
211 cons->matches[i] = tr_new_name((char *)(json_string_value(json_array_get(jc, i))));
217 static TR_FILTER *tr_cfg_parse_one_filter (TR_CFG *trc, json_t *jfilt, TR_CFG_RC *rc)
219 TR_FILTER *filt = NULL;
220 json_t *jftype = NULL;
222 json_t *jfaction = NULL;
223 json_t *jfspecs = NULL;
224 json_t *jffield = NULL;
225 json_t *jfmatch = NULL;
230 if ((NULL == (jftype = json_object_get(jfilt, "type"))) ||
231 (!json_is_string(jftype))) {
232 tr_debug("tr_cfg_parse_one_filter: Error parsing filter type.");
233 *rc = TR_CFG_NOPARSE;
237 if ((NULL == (jfls = json_object_get(jfilt, "filter_lines"))) ||
238 (!json_is_array(jfls))) {
239 tr_debug("tr_cfg_parse_one_filter: Error parsing filter type.");
240 *rc = TR_CFG_NOPARSE;
244 if (TR_MAX_FILTER_LINES < json_array_size(jfls)) {
245 tr_debug("tr_cfg_parse_one_filter: Filter has too many filter_lines, maximimum of %d.", TR_MAX_FILTER_LINES);
246 *rc = TR_CFG_NOPARSE;
250 if (NULL == (filt = talloc_zero(trc, TR_FILTER))) {
251 tr_debug("tr_cfg_parse_one_filter: Out of memory.");
256 if (!strcmp(json_string_value(jftype), "rp_permitted")) {
257 filt->type = TR_FILTER_TYPE_RP_PERMITTED;
260 tr_debug("tr_cfg_parse_one_filter: Error parsing filter type, unknown type '%s'.", json_string_value(jftype));
261 *rc = TR_CFG_NOPARSE;
262 tr_filter_free(filt);
266 /* For each filter line... */
267 for (i = 0; i < json_array_size(jfls); i++) {
269 if ((NULL == (jfaction = json_object_get(json_array_get(jfls, i), "action"))) ||
270 (!json_is_string(jfaction))) {
271 tr_debug("tr_cfg_parse_one_filter: Error parsing filter action.");
272 *rc = TR_CFG_NOPARSE;
273 tr_filter_free(filt);
277 if ((NULL == (jfspecs = json_object_get(json_array_get(jfls, i), "filter_specs"))) ||
278 (!json_is_array(jfspecs)) ||
279 (0 == json_array_size(jfspecs))) {
280 tr_debug("tr_cfg_parse_one_filter: Error parsing filter specs.");
281 *rc = TR_CFG_NOPARSE;
282 tr_filter_free(filt);
286 if (TR_MAX_FILTER_SPECS < json_array_size(jfspecs)) {
287 tr_debug("tr_cfg_parse_one_filter: Filter has too many filter_specs, maximimum of %d.", TR_MAX_FILTER_SPECS);
288 *rc = TR_CFG_NOPARSE;
289 tr_filter_free(filt);
293 if (NULL == (filt->lines[i] = talloc_zero(trc, TR_FLINE))) {
294 tr_debug("tr_cfg_parse_one_filter: Out of memory (fline).");
296 tr_filter_free(filt);
300 if (!strcmp(json_string_value(jfaction), "accept")) {
301 filt->lines[i]->action = TR_FILTER_ACTION_ACCEPT;
303 else if (!strcmp(json_string_value(jfaction), "reject")) {
304 filt->lines[i]->action = TR_FILTER_ACTION_REJECT;
307 tr_debug("tr_cfg_parse_one_filter: Error parsing filter action, unknown action' %s'.", json_string_value(jfaction));
308 *rc = TR_CFG_NOPARSE;
309 tr_filter_free(filt);
313 if ((NULL != (jrc = json_object_get(json_array_get(jfls, i), "realm_constraints"))) &&
314 (json_is_array(jrc)) &&
315 (0 != json_array_size(jrc)) &&
316 (TR_MAX_CONST_MATCHES >= json_array_size(jrc))) {
318 if (NULL == (filt->lines[i]->realm_cons = tr_cfg_parse_one_constraint(trc, "realm", jrc, rc))) {
319 tr_debug("tr_cfg_parse_one_filter: Error parsing realm constraint");
320 tr_filter_free(filt);
325 if ((NULL != (jdc = json_object_get(json_array_get(jfls, i), "domain_constraints"))) &&
326 (json_is_array(jdc)) &&
327 (0 != json_array_size(jdc)) &&
328 (TR_MAX_CONST_MATCHES >= json_array_size(jdc))) {
330 if (NULL == (filt->lines[i]->domain_cons = tr_cfg_parse_one_constraint(trc, "domain", jdc, rc))) {
331 tr_debug("tr_cfg_parse_one_filter: Error parsing domain constraint");
332 tr_filter_free(filt);
337 /*For each filter spec within the filter line... */
338 for (j = 0; j <json_array_size(jfspecs); j++) {
340 if ((NULL == (jffield = json_object_get(json_array_get(jfspecs, j), "field"))) ||
341 (!json_is_string(jffield)) ||
342 (NULL == (jfmatch = json_object_get(json_array_get(jfspecs, j), "match"))) ||
343 (!json_is_string(jfmatch))) {
344 tr_debug("tr_cfg_parse_one_filter: Error parsing filter field and match for filter spec %d, filter line %d.", i, j);
345 *rc = TR_CFG_NOPARSE;
346 tr_filter_free(filt);
350 if (NULL == (filt->lines[i]->specs[j] = talloc_zero(trc, TR_FSPEC))) {
351 tr_debug("tr_cfg_parse_one_filter: Out of memory.");
353 tr_filter_free(filt);
357 if ((NULL == (filt->lines[i]->specs[j]->field = tr_new_name((char *)json_string_value(jffield)))) ||
358 (NULL == (filt->lines[i]->specs[j]->match = tr_new_name((char *)json_string_value(jfmatch))))) {
359 tr_debug("tr_cfg_parse_one_filter: Out of memory.");
361 tr_filter_free(filt);
370 static TR_RP_CLIENT *tr_cfg_parse_one_rp_client (TR_CFG *trc, json_t *jrp, TR_CFG_RC *rc)
372 TR_RP_CLIENT *rp = NULL;
374 json_t *jfilt = NULL;
375 json_t *jftype = NULL;
378 if ((!trc) || (!jrp) || (!rc)) {
379 tr_debug("tr_cfg_parse_one_rp_realm: Bad parameters.");
381 *rc = TR_CFG_BAD_PARAMS;
385 if ((NULL == (jgns = json_object_get(jrp, "gss_names"))) ||
386 (!json_is_array(jgns))) {
387 tr_debug("tr_cfg_parse_one_rp_client: Error parsing RP client configuration, no GSS names.");
388 *rc = TR_CFG_NOPARSE;
392 /* TBD -- Support more than one filter per RP client? */
393 if (NULL == (jfilt = json_object_get(jrp, "filter"))) {
394 tr_debug("tr_cfg_parse_one_rp_client: Error parsing RP client configuration, no filter.");
395 *rc = TR_CFG_NOPARSE;
399 /* We only support rp_permitted filters for RP clients */
400 if ((NULL == (jftype = json_object_get(jfilt, "type"))) ||
401 (!json_is_string(jftype)) ||
402 (strcmp(json_string_value(jftype), "rp_permitted"))) {
403 tr_debug("tr_cfg_parse_one_rp_client: Error parsing RP client filter type.");
404 *rc = TR_CFG_NOPARSE;
408 if (TR_MAX_GSS_NAMES < json_array_size(jgns)) {
409 tr_debug("tr_cfg_parse_one_rp_client: RP Client has too many GSS Names.");
410 *rc = TR_CFG_NOPARSE;
414 if (NULL == (rp = talloc_zero(trc, TR_RP_CLIENT))) {
415 tr_debug("tr_cfg_parse_one_rp_realm: Out of memory.");
420 /* TBD -- support more than one filter entry per RP Client? */
421 if (NULL == (rp->filter = tr_cfg_parse_one_filter(trc, jfilt, rc))) {
422 tr_debug("tr_cfg_parse_one_rp_client: Error parsing filter.");
423 *rc = TR_CFG_NOPARSE;
427 for (i = 0; i < json_array_size(jgns); i++) {
428 if (NULL == (rp->gss_names[i] = tr_new_name ((char *)json_string_value(json_array_get(jgns, i))))) {
429 tr_debug("tr_cfg_parse_one_rp_client: No memory for GSS Name.");
438 static TR_CFG_RC tr_cfg_parse_rp_clients (TR_CFG *trc, json_t *jcfg) {
440 TR_RP_CLIENT *rp = NULL;
441 TR_CFG_RC rc = TR_CFG_SUCCESS;
444 if ((!trc) || (!jcfg))
445 return TR_CFG_BAD_PARAMS;
447 if (NULL != (jrps = json_object_get(jcfg, "rp_clients"))) {
449 if (!json_is_array(jrps)) {
450 return TR_CFG_NOPARSE;
453 for (i = 0; i < json_array_size(jrps); i++) {
454 if (NULL == (rp = tr_cfg_parse_one_rp_client(trc,
455 json_array_get(jrps, i),
459 tr_debug("tr_cfg_parse_rp_clients: RP client configured -- first gss: %s", rp->gss_names[0]->buf);
460 rp->next = trc->rp_clients;
461 trc->rp_clients = rp;
464 tr_debug("tr_cfg_parse_rp_clients: Finished (rc=%d)", rc);
468 static TR_AAA_SERVER *tr_cfg_parse_one_aaa_server (TR_CFG *trc, json_t *jaddr, TR_CFG_RC *rc) {
469 TR_AAA_SERVER *aaa = NULL;
471 if ((!trc) || (!jaddr) || (!json_is_string(jaddr))) {
472 tr_debug("tr_cfg_parse_one_aaa_server: Bad parameters.");
473 *rc = TR_CFG_BAD_PARAMS;
477 if (NULL == (aaa = talloc_zero(trc, TR_AAA_SERVER))) {
478 tr_debug("tr_cfg_parse_one_aaa_server: Out of memory.");
483 aaa->hostname = tr_new_name((char *)(json_string_value(jaddr)));
488 static TR_AAA_SERVER *tr_cfg_parse_aaa_servers (TR_CFG *trc, json_t *jaaas, TR_CFG_RC *rc)
490 TR_AAA_SERVER *aaa = NULL;
491 TR_AAA_SERVER *temp_aaa = NULL;
494 for (i = 0; i < json_array_size(jaaas); i++) {
495 if (NULL == (temp_aaa = tr_cfg_parse_one_aaa_server(trc, json_array_get(jaaas, i), rc))) {
498 /* TBD -- IPv6 addresses */
499 // tr_debug("tr_cfg_parse_aaa_servers: Configuring AAA Server: ip_addr = %s.", inet_ntoa(temp_aaa->aaa_server_addr));
500 temp_aaa->next = aaa;
503 tr_debug("tr_cfg_parse_aaa_servers: Finished (rc=%d)", *rc);
507 static TR_APC *tr_cfg_parse_apcs (TR_CFG *trc, json_t *japcs, TR_CFG_RC *rc)
511 *rc = TR_CFG_SUCCESS; /* presume success */
513 if ((!trc) || (!japcs) || (!rc)) {
514 tr_debug("tr_cfg_parse_apcs: Bad parameters.");
516 *rc = TR_CFG_BAD_PARAMS;
520 if (NULL == (apc = talloc_zero(trc, TR_APC))) {
521 tr_debug("tr_cfg_parse_apcs: Out of memory.");
526 /* TBD, deal with more than one APC. In the meantime, though... */
527 /* Only parse the first APC, because we only know how to deal with one, anyway. */
528 if (0 == json_array_size(japcs))
531 if (NULL == (apc->id = tr_new_name((char *)json_string_value(json_array_get(japcs, 0))))) {
532 tr_debug("tr_cfg_parse_apcs: No memory for APC name.");
537 tr_debug("tr_cfg_parse_apcs: Finished (rc=%d)", *rc);
541 static TR_IDP_REALM *tr_cfg_parse_one_idp_realm (TR_CFG *trc, json_t *jidp, TR_CFG_RC *rc) {
542 TR_IDP_REALM *idp = NULL;
544 json_t *jscfg = NULL;
545 json_t *jsrvrs = NULL;
546 json_t *japcs = NULL;
548 if ((!trc) || (!jidp) || (!rc)) {
549 tr_debug("tr_cfg_parse_one_idp_realm: Bad parameters.");
551 *rc = TR_CFG_BAD_PARAMS;
555 if (NULL == (idp = talloc_zero(trc, TR_IDP_REALM))) {
556 tr_debug("tr_cfg_parse_one_idp_realm: Out of memory.");
561 if ((NULL == (jrid = json_object_get(jidp, "realm_id"))) ||
562 (!json_is_string(jrid)) ||
563 (NULL == (jscfg = json_object_get(jidp, "shared_config"))) ||
564 (!json_is_string(jscfg)) ||
565 (NULL == (jsrvrs = json_object_get(jidp, "aaa_servers"))) ||
566 (!json_is_array(jsrvrs))) {
567 tr_debug("tr_cfg_parse_one_idp_realm: Error parsing IDP realm configuration.");
568 *rc = TR_CFG_NOPARSE;
572 if (0 == strcmp(json_string_value(jscfg), "no")) {
573 idp->shared_config = 0;
575 idp->shared_config = 1;
578 if (NULL == (idp->realm_id = tr_new_name((char *)json_string_value(jrid)))) {
579 tr_debug("tr_cfg_parse_one_idp_realm: No memory for realm id.");
584 if (NULL == (idp->aaa_servers = tr_cfg_parse_aaa_servers(trc, jsrvrs, rc))) {
585 tr_debug("tr_cfg_parse_one_idp_realm: Can't parse AAA servers for realm %s.", idp->realm_id->buf);
586 tr_free_name(idp->realm_id);
590 if ((NULL != (japcs = json_object_get(jidp, "apcs"))) &&
591 (json_is_array(japcs))) {
592 if (NULL == (idp->apcs = tr_cfg_parse_apcs(trc, japcs, rc))) {
593 tr_debug("tr_cfg_parse_one_idp_realm: Can't parse APCs for realm %s .", idp->realm_id->buf);
594 tr_free_name(idp->realm_id);
595 /* TBD -- free aaa_servers */;
602 static TR_CFG_RC tr_cfg_parse_default_servers (TR_CFG *trc, json_t *jcfg)
605 TR_CFG_RC rc = TR_CFG_SUCCESS;
606 TR_AAA_SERVER *ds = NULL;
609 if ((!trc) || (!jcfg))
610 return TR_CFG_BAD_PARAMS;
612 /* If there are default servers, store them */
613 if ((NULL != (jdss = json_object_get(jcfg, "default_servers"))) &&
614 (json_is_array(jdss)) &&
615 (0 < json_array_size(jdss))) {
617 for (i = 0; i < json_array_size(jdss); i++) {
618 if (NULL == (ds = tr_cfg_parse_one_aaa_server(trc,
619 json_array_get(jdss, i),
623 tr_debug("tr_cfg_parse_default_servers: Default server configured: %s", ds->hostname->buf);
624 ds->next = trc->default_servers;
625 trc->default_servers = ds;
629 tr_debug("tr_cfg_parse_default_servers: Finished (rc=%d)", rc);
633 static TR_CFG_RC tr_cfg_parse_idp_realms (TR_CFG *trc, json_t *jcfg)
635 json_t *jidps = NULL;
636 TR_CFG_RC rc = TR_CFG_SUCCESS;
637 TR_IDP_REALM *idp = NULL;
640 if ((!trc) || (!jcfg))
641 return TR_CFG_BAD_PARAMS;
643 /* If there are any IDP Realms, parse them */
644 if ((NULL != (jidps = json_object_get(jcfg, "idp_realms"))) &&
645 (json_is_array(jidps))) {
646 for (i = 0; i < json_array_size(jidps); i++) {
647 if (NULL == (idp = tr_cfg_parse_one_idp_realm(trc,
648 json_array_get(jidps, i),
652 tr_debug("tr_cfg_parse_idp_realms: IDP realm configured: %s.", idp->realm_id->buf);
653 idp->next = trc->idp_realms;
654 trc->idp_realms = idp;
658 tr_debug("tr_cfg_parse_idp_realms: Finished (rc=%d)", rc);
662 static TR_IDP_REALM *tr_cfg_parse_comm_idps (TR_CFG *trc, json_t *jidps, TR_CFG_RC *rc)
664 TR_IDP_REALM *idp = NULL;
665 TR_IDP_REALM *temp_idp = NULL;
670 (!json_is_array(jidps))) {
672 *rc = TR_CFG_BAD_PARAMS;
676 for (i = 0; i < json_array_size(jidps); i++) {
677 if (NULL == (temp_idp = (tr_cfg_find_idp(trc,
678 tr_new_name((char *)json_string_value(json_array_get(jidps, i))),
680 tr_debug("tr_cfg_parse_comm_idps: Unknown IDP %s.",
681 (char *)json_string_value(json_array_get(jidps, i)));
685 temp_idp->comm_next = idp;
692 static TR_RP_REALM *tr_cfg_parse_comm_rps (TR_CFG *trc, json_t *jrps, TR_CFG_RC *rc)
694 TR_RP_REALM *rp = NULL;
695 TR_RP_REALM *temp_rp = NULL;
700 (!json_is_array(jrps))) {
702 *rc = TR_CFG_BAD_PARAMS;
706 for (i = (json_array_size(jrps)-1); i >= 0; i--) {
707 if (NULL == (temp_rp = talloc_zero(trc, TR_RP_REALM))) {
708 tr_debug("tr_cfg_parse_comm_rps: Can't allocate memory for RP Realm.");
714 if (NULL == (temp_rp->realm_name = tr_new_name((char *)json_string_value(json_array_get(jrps, i))))) {
715 tr_debug("tr_cfg_parse_comm_rps: No memory for RP Realm Name.");
728 static TR_COMM *tr_cfg_parse_one_comm (TR_CFG *trc, json_t *jcomm, TR_CFG_RC *rc) {
729 TR_COMM *comm = NULL;
731 json_t *jtype = NULL;
732 json_t *japcs = NULL;
733 json_t *jidps = NULL;
736 if ((!trc) || (!jcomm) || (!rc)) {
737 tr_debug("tr_cfg_parse_one_comm: Bad parameters.");
739 *rc = TR_CFG_BAD_PARAMS;
743 if (NULL == (comm = talloc_zero(trc, TR_COMM))) {
744 tr_crit("tr_cfg_parse_one_comm: Out of memory.");
750 if ((NULL == (jid = json_object_get(jcomm, "community_id"))) ||
751 (!json_is_string(jid)) ||
752 (NULL == (jtype = json_object_get(jcomm, "type"))) ||
753 (!json_is_string(jtype)) ||
754 (NULL == (japcs = json_object_get(jcomm, "apcs"))) ||
755 (!json_is_array(japcs)) ||
756 (NULL == (jidps = json_object_get(jcomm, "idp_realms"))) ||
757 (!json_is_array(jidps)) ||
758 (NULL == (jrps = json_object_get(jcomm, "rp_realms"))) ||
759 (!json_is_array(jrps))) {
760 tr_debug("tr_cfg_parse_one_comm: Error parsing Communities configuration.");
761 *rc = TR_CFG_NOPARSE;
765 if (NULL == (comm->id = tr_new_name((char *)json_string_value(jid)))) {
766 tr_debug("tr_cfg_parse_one_comm: No memory for community id.");
771 if (0 == strcmp(json_string_value(jtype), "apc")) {
772 comm->type = TR_COMM_APC;
773 } else if (0 == strcmp(json_string_value(jtype), "coi")) {
774 comm->type = TR_COMM_COI;
775 if (NULL == (comm->apcs = tr_cfg_parse_apcs(trc, japcs, rc))) {
776 tr_debug("tr_cfg_parse_one_comm: Can't parse APCs for COI %s.", comm->id->buf);
777 tr_free_name(comm->id);
781 tr_debug("tr_cfg_parse_one_comm: Invalid community type, comm = %s, type = %s", comm->id->buf, json_string_value(jtype));
782 tr_free_name(comm->id);
783 *rc = TR_CFG_NOPARSE;
787 comm->idp_realms = tr_cfg_parse_comm_idps(trc, jidps, rc);
788 if (TR_CFG_SUCCESS != *rc) {
789 tr_debug("tr_cfg_parse_one_comm: Can't parse IDP realms for comm %s.", comm->id->buf);
790 tr_free_name(comm->id);
794 comm->rp_realms = tr_cfg_parse_comm_rps(trc, jrps, rc);
795 if (TR_CFG_SUCCESS != *rc) {
796 tr_debug("tr_cfg_parse_comm: Can't parse RP realms for comm %s .", comm->id->buf);
797 tr_free_name(comm->id);
801 if (TR_COMM_APC == comm->type) {
802 json_t *jexpire = json_object_get(jcomm, "expiration_interval");
803 comm->expiration_interval = 43200; /*30 days*/
805 if (!json_is_integer(jexpire)) {
806 fprintf(stderr, "tr_parse_comm: expirae_interval is not an integer\n");
809 comm->expiration_interval = json_integer_value(jexpire);
810 if (comm->expiration_interval <= 10)
811 comm->expiration_interval = 11; /* Freeradius waits 10 minutes between successful TR queries*/
812 if (comm->expiration_interval > 129600) /* 90 days*/
813 comm->expiration_interval = 129600;
820 static TR_CFG_RC tr_cfg_parse_comms (TR_CFG *trc, json_t *jcfg)
822 json_t *jcomms = NULL;
823 TR_CFG_RC rc = TR_CFG_SUCCESS;
824 TR_COMM *comm = NULL;
827 if ((!trc) || (!jcfg)) {
828 tr_debug("tr_cfg_parse_comms: Bad Parameters.");
829 return TR_CFG_BAD_PARAMS;
832 if (NULL != (jcomms = json_object_get(jcfg, "communities"))) {
833 if (!json_is_array(jcomms)) {
834 return TR_CFG_NOPARSE;
837 for (i = 0; i < json_array_size(jcomms); i++) {
838 if (NULL == (comm = tr_cfg_parse_one_comm(trc,
839 json_array_get(jcomms, i),
843 tr_debug("tr_cfg_parse_comms: Community configured: %s.", comm->id->buf);
844 comm->next = trc->comms;
848 tr_debug("tr_cfg_parse_comms: Finished (rc=%d)", rc);
852 TR_CFG_RC tr_cfg_validate (TR_CFG *trc) {
853 TR_CFG_RC rc = TR_CFG_SUCCESS;
856 return TR_CFG_BAD_PARAMS;
858 if ((NULL == trc->internal)||
859 (NULL == trc->internal->hostname)) {
860 tr_debug("tr_cfg_validate: Error: No internal configuration, or no hostname.");
864 if (NULL == trc->rp_clients) {
865 tr_debug("tr_cfg_validate: Error: No RP Clients configured");
869 if (NULL == trc->comms) {
870 tr_debug("tr_cfg_validate: Error: No Communities configured");
874 if ((NULL == trc->default_servers) && (NULL == trc->idp_realms)) {
875 tr_debug("tr_cfg_validate: Error: No default servers or IDPs configured.");
882 /* Join two paths and return a pointer to the result. This should be freed
883 * via talloc_free. Returns NULL on failure. */
884 static char *join_paths(const char *p1, const char *p2) {
885 return talloc_asprintf(NULL, "%s/%s", p1, p2); /* returns NULL on a failure */
888 /* Reads configuration files in config_dir ("" or "./" will use the current directory). */
889 TR_CFG_RC tr_parse_config (TR_CFG *new_cfg, const char *config_dir, int n, struct dirent **cfg_files) {
892 char *file_with_path;
895 if ((!new_cfg) || (!cfg_files) || (n<=0))
896 return TR_CFG_BAD_PARAMS;
898 /* Parse configuration information from each config file */
899 for (ii=0; ii<n; ii++) {
900 file_with_path=join_paths(config_dir, cfg_files[ii]->d_name); /* must free result with talloc_free */
901 if(file_with_path == NULL) {
902 tr_crit("tr_parse_config: error joining path.");
905 tr_debug("tr_parse_config: Parsing %s.", cfg_files[ii]->d_name); /* print the filename without the path */
906 if (NULL == (jcfg = json_load_file(file_with_path,
907 JSON_DISABLE_EOF_CHECK, &rc))) {
908 tr_debug("tr_parse_config: Error parsing config file %s.",
909 cfg_files[ii]->d_name);
910 talloc_free(file_with_path);
911 return TR_CFG_NOPARSE;
913 talloc_free(file_with_path); /* done with filename */
915 if ((TR_CFG_SUCCESS != tr_cfg_parse_internal(new_cfg, jcfg)) ||
916 (TR_CFG_SUCCESS != tr_cfg_parse_rp_clients(new_cfg, jcfg)) ||
917 (TR_CFG_SUCCESS != tr_cfg_parse_idp_realms(new_cfg, jcfg)) ||
918 (TR_CFG_SUCCESS != tr_cfg_parse_default_servers(new_cfg, jcfg)) ||
919 (TR_CFG_SUCCESS != tr_cfg_parse_comms(new_cfg, jcfg))) {
924 /* make sure we got a complete, consistent configuration */
925 if (TR_CFG_SUCCESS != tr_cfg_validate(new_cfg)) {
926 tr_err("tr_parse_config: Error: INVALID CONFIGURATION");
930 return TR_CFG_SUCCESS;
933 TR_IDP_REALM *tr_cfg_find_idp (TR_CFG *tr_cfg, TR_NAME *idp_id, TR_CFG_RC *rc)
936 TR_IDP_REALM *cfg_idp;
938 if ((!tr_cfg) || (!idp_id)) {
940 *rc = TR_CFG_BAD_PARAMS;
944 for (cfg_idp = tr_cfg->idp_realms; NULL != cfg_idp; cfg_idp = cfg_idp->next) {
945 if (!tr_name_cmp (idp_id, cfg_idp->realm_id)) {
946 tr_debug("tr_cfg_find_idp: Found %s.", idp_id->buf);
950 /* if we didn't find one, return NULL */
954 TR_RP_CLIENT *tr_cfg_find_rp (TR_CFG *tr_cfg, TR_NAME *rp_gss, TR_CFG_RC *rc)
956 TR_RP_CLIENT *cfg_rp;
959 if ((!tr_cfg) || (!rp_gss)) {
961 *rc = TR_CFG_BAD_PARAMS;
965 for (cfg_rp = tr_cfg->rp_clients; NULL != cfg_rp; cfg_rp = cfg_rp->next) {
966 for (i = 0; i < TR_MAX_GSS_NAMES; i++) {
967 if (!tr_name_cmp (rp_gss, cfg_rp->gss_names[i])) {
968 tr_debug("tr_cfg_find_rp: Found %s.", rp_gss->buf);
973 /* if we didn't find one, return NULL */
977 static int is_cfg_file(const struct dirent *dent) {
980 /* Only accept filenames ending in ".cfg" and starting with a character
981 * other than an ASCII '.' */
983 /* filename must be at least 4 characters long to be acceptable */
984 n=strlen(dent->d_name);
989 /* filename must not start with '.' */
990 if ('.' == dent->d_name[0]) {
994 /* If the above passed and the last four characters of the filename are .cfg, accept.
995 * (n.b., assumes an earlier test checked that the name is >= 4 chars long.) */
996 if (0 == strcmp(&(dent->d_name[n-4]), ".cfg")) {
1000 /* otherwise, return false. */
1004 /* Find configuration files in a particular directory. Returns the
1005 * number of entries found, 0 if none are found, or <0 for some
1006 * errors. If n>=0, the cfg_files parameter will contain a newly
1007 * allocated array of pointers to struct dirent entries, as returned
1008 * by scandir(). These can be freed with tr_free_config_file_list().
1010 int tr_find_config_files (const char *config_dir, struct dirent ***cfg_files) {
1013 n = scandir(config_dir, cfg_files, is_cfg_file, alphasort);
1017 tr_debug("tr_find_config: scandir error trying to scan %s.", config_dir);
1018 } else if (n == 0) {
1019 tr_debug("tr_find_config: No config files found.");
1021 for (ii=0; ii<n; ii++) {
1022 tr_debug("tr_find_config: Config file found (%s).", (*cfg_files)[ii]->d_name);
1029 /* Free memory allocated for configuration file list returned from tr_find_config_files().
1030 * This can be called regardless of the return value of tr_find_config_values(). */
1031 void tr_free_config_file_list(int n, struct dirent ***cfg_files) {
1034 /* if n < 0, then scandir did not allocate anything because it failed */
1035 if((n>=0) && (*cfg_files != NULL)) {
1036 for(ii=0; ii<n; ii++) {
1037 free((*cfg_files)[ii]);
1039 free(*cfg_files); /* safe even if n==0 */
1040 *cfg_files=NULL; /* this will help prevent accidentally freeing twice */