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 (TR_CFG *cfg) {
48 tr_notice("tr_print_config: Logging running trust router configuration.");
49 tr_print_comms(cfg->comms);
52 void tr_print_comms (TR_COMM *comm_list) {
55 for (comm = comm_list; NULL != comm; comm = comm->next) {
56 tr_notice("tr_print_config: Community %s:", comm->id->buf);
58 tr_notice("tr_print_config: - Member IdPs:");
59 tr_print_comm_idps(comm->idp_realms);
61 tr_notice("tr_print_config: - Member RPs:");
62 tr_print_comm_rps(comm->rp_realms);
66 void tr_print_comm_idps (TR_IDP_REALM *idp_list) {
67 TR_IDP_REALM *idp = NULL;
69 for (idp = idp_list; NULL != idp; idp = idp->comm_next) {
70 tr_notice("tr_print_config: - @%s", idp->realm_id->buf);
74 void tr_print_comm_rps(TR_RP_REALM *rp_list) {
75 TR_RP_REALM *rp = NULL;
77 for (rp = rp_list; NULL != rp; rp = rp->next) {
78 tr_notice("tr_print_config: - %s", rp->realm_name->buf);
82 void tr_cfg_free (TR_CFG *cfg) {
87 TR_CFG_RC tr_apply_new_config (TR_INSTANCE *tr) {
89 return TR_CFG_BAD_PARAMS;
92 tr_cfg_free(tr->active_cfg);
94 tr->active_cfg = tr->new_cfg;
96 tr_log_threshold(tr->active_cfg->internal->log_threshold);
97 tr_console_threshold(tr->active_cfg->internal->console_threshold);
99 return TR_CFG_SUCCESS;
102 static TR_CFG_RC tr_cfg_parse_internal (TR_CFG *trc, json_t *jcfg) {
106 json_t *jhname = NULL;
108 json_t *jconthres = NULL;
109 json_t *jlogthres = NULL;
111 if ((!trc) || (!jcfg))
112 return TR_CFG_BAD_PARAMS;
114 if (NULL == trc->internal) {
115 if (NULL == (trc->internal = talloc(trc, TR_CFG_INTERNAL)))
118 memset(trc->internal, 0, sizeof(TR_CFG_INTERNAL));
121 if (NULL != (jint = json_object_get(jcfg, "tr_internal"))) {
122 if (NULL != (jmtd = json_object_get(jint, "max_tree_depth"))) {
123 if (json_is_number(jmtd)) {
124 trc->internal->max_tree_depth = json_integer_value(jmtd);
126 tr_debug("tr_cfg_parse_internal: Parsing error, max_tree_depth is not a number.");
127 return TR_CFG_NOPARSE;
130 /* If not configured, use the default */
131 trc->internal->max_tree_depth = TR_DEFAULT_MAX_TREE_DEPTH;
133 if (NULL != (jtp = json_object_get(jint, "tids_port"))) {
134 if (json_is_number(jtp)) {
135 trc->internal->tids_port = json_integer_value(jtp);
137 tr_debug("tr_cfg_parse_internal: Parsing error, port is not a number.");
138 return TR_CFG_NOPARSE;
141 /* If not configured, use the default */
142 trc->internal->tids_port = TR_DEFAULT_TIDS_PORT;
144 if (NULL != (jhname = json_object_get(jint, "hostname"))) {
145 if (json_is_string(jhname)) {
146 trc->internal->hostname = json_string_value(jhname);
148 tr_debug("tr_cfg_parse_internal: Parsing error, hostname is not a string.");
149 return TR_CFG_NOPARSE;
153 if (NULL != (jlog = json_object_get(jint, "logging"))) {
154 if (NULL != (jlogthres = json_object_get(jlog, "log_threshold"))) {
155 if (json_is_string(jlogthres)) {
156 trc->internal->log_threshold = str2sev(json_string_value(jlogthres));
158 tr_debug("tr_cfg_parse_internal: Parsing error, log_threshold is not a string.");
159 return TR_CFG_NOPARSE;
162 /* If not configured, use the default */
163 trc->internal->log_threshold = TR_DEFAULT_LOG_THRESHOLD;
166 if (NULL != (jconthres = json_object_get(jlog, "console_threshold"))) {
167 if (json_is_string(jconthres)) {
168 trc->internal->console_threshold = str2sev(json_string_value(jconthres));
170 tr_debug("tr_cfg_parse_internal: Parsing error, console_threshold is not a string.");
171 return TR_CFG_NOPARSE;
174 /* If not configured, use the default */
175 trc->internal->console_threshold = TR_DEFAULT_CONSOLE_THRESHOLD;
178 /* If not configured, use the default */
179 trc->internal->console_threshold = TR_DEFAULT_CONSOLE_THRESHOLD;
180 trc->internal->log_threshold = TR_DEFAULT_LOG_THRESHOLD;
183 tr_debug("tr_cfg_parse_internal: Internal config parsed.");
184 return TR_CFG_SUCCESS;
186 return TR_CFG_SUCCESS;
189 static TR_CONSTRAINT *tr_cfg_parse_one_constraint (TR_CFG *trc, char *ctype, json_t *jc, TR_CFG_RC *rc)
194 if ((!trc) || (!ctype) || (!jc) || (!rc) ||
195 (!json_is_array(jc)) ||
196 (0 >= json_array_size(jc)) ||
197 (TR_MAX_CONST_MATCHES < json_array_size(jc)) ||
198 (!json_is_string(json_array_get(jc, 0)))) {
199 tr_debug("tr_cfg_parse_one_constraint: config error.");
200 *rc = TR_CFG_NOPARSE;
204 if (NULL == (cons = talloc(trc, TR_CONSTRAINT))) {
205 tr_debug("tr_cfg_parse_one_constraint: Out of memory (cons).");
210 memset(cons, 0, sizeof(TR_CONSTRAINT));
212 if (NULL == (cons->type = tr_new_name(ctype))) {
213 tr_debug("tr_cfg_parse_one_constraint: Out of memory (type).");
218 for (i = 0; i < json_array_size(jc); i++) {
219 cons->matches[i] = tr_new_name((char *)(json_string_value(json_array_get(jc, i))));
225 static TR_FILTER *tr_cfg_parse_one_filter (TR_CFG *trc, json_t *jfilt, TR_CFG_RC *rc)
227 TR_FILTER *filt = NULL;
228 json_t *jftype = NULL;
230 json_t *jfaction = NULL;
231 json_t *jfspecs = NULL;
232 json_t *jffield = NULL;
233 json_t *jfmatch = NULL;
238 if ((NULL == (jftype = json_object_get(jfilt, "type"))) ||
239 (!json_is_string(jftype))) {
240 tr_debug("tr_cfg_parse_one_filter: Error parsing filter type.");
241 *rc = TR_CFG_NOPARSE;
245 if ((NULL == (jfls = json_object_get(jfilt, "filter_lines"))) ||
246 (!json_is_array(jfls))) {
247 tr_debug("tr_cfg_parse_one_filter: Error parsing filter type.");
248 *rc = TR_CFG_NOPARSE;
252 if (TR_MAX_FILTER_LINES < json_array_size(jfls)) {
253 tr_debug("tr_cfg_parse_one_filter: Filter has too many filter_lines, maximimum of %d.", TR_MAX_FILTER_LINES);
254 *rc = TR_CFG_NOPARSE;
258 if (NULL == (filt = talloc(trc, TR_FILTER))) {
259 tr_debug("tr_cfg_parse_one_filter: Out of memory.");
264 memset(filt, 0, sizeof(TR_FILTER));
266 if (!strcmp(json_string_value(jftype), "rp_permitted")) {
267 filt->type = TR_FILTER_TYPE_RP_PERMITTED;
270 tr_debug("tr_cfg_parse_one_filter: Error parsing filter type, unknown type '%s'.", json_string_value(jftype));
271 *rc = TR_CFG_NOPARSE;
272 tr_filter_free(filt);
276 /* For each filter line... */
277 for (i = 0; i < json_array_size(jfls); i++) {
279 if ((NULL == (jfaction = json_object_get(json_array_get(jfls, i), "action"))) ||
280 (!json_is_string(jfaction))) {
281 tr_debug("tr_cfg_parse_one_filter: Error parsing filter action.");
282 *rc = TR_CFG_NOPARSE;
283 tr_filter_free(filt);
287 if ((NULL == (jfspecs = json_object_get(json_array_get(jfls, i), "filter_specs"))) ||
288 (!json_is_array(jfspecs)) ||
289 (0 == json_array_size(jfspecs))) {
290 tr_debug("tr_cfg_parse_one_filter: Error parsing filter specs.");
291 *rc = TR_CFG_NOPARSE;
292 tr_filter_free(filt);
296 if (TR_MAX_FILTER_SPECS < json_array_size(jfspecs)) {
297 tr_debug("tr_cfg_parse_one_filter: Filter has too many filter_specs, maximimum of %d.", TR_MAX_FILTER_SPECS);
298 *rc = TR_CFG_NOPARSE;
299 tr_filter_free(filt);
303 if (NULL == (filt->lines[i] = talloc(trc, TR_FLINE))) {
304 tr_debug("tr_cfg_parse_one_filter: Out of memory (fline).");
306 tr_filter_free(filt);
310 memset(filt->lines[i], 0, sizeof(TR_FLINE));
312 if (!strcmp(json_string_value(jfaction), "accept")) {
313 filt->lines[i]->action = TR_FILTER_ACTION_ACCEPT;
315 else if (!strcmp(json_string_value(jfaction), "reject")) {
316 filt->lines[i]->action = TR_FILTER_ACTION_REJECT;
319 tr_debug("tr_cfg_parse_one_filter: Error parsing filter action, unknown action' %s'.", json_string_value(jfaction));
320 *rc = TR_CFG_NOPARSE;
321 tr_filter_free(filt);
325 if ((NULL != (jrc = json_object_get(json_array_get(jfls, i), "realm_constraints"))) &&
326 (json_is_array(jrc)) &&
327 (0 != json_array_size(jrc)) &&
328 (TR_MAX_CONST_MATCHES >= json_array_size(jrc))) {
330 if (NULL == (filt->lines[i]->realm_cons = tr_cfg_parse_one_constraint(trc, "realm", jrc, rc))) {
331 tr_debug("tr_cfg_parse_one_filter: Error parsing realm constraint");
332 tr_filter_free(filt);
337 if ((NULL != (jdc = json_object_get(json_array_get(jfls, i), "domain_constraints"))) &&
338 (json_is_array(jdc)) &&
339 (0 != json_array_size(jdc)) &&
340 (TR_MAX_CONST_MATCHES >= json_array_size(jdc))) {
342 if (NULL == (filt->lines[i]->domain_cons = tr_cfg_parse_one_constraint(trc, "domain", jdc, rc))) {
343 tr_debug("tr_cfg_parse_one_filter: Error parsing domain constraint");
344 tr_filter_free(filt);
349 /*For each filter spec within the filter line... */
350 for (j = 0; j <json_array_size(jfspecs); j++) {
352 if ((NULL == (jffield = json_object_get(json_array_get(jfspecs, j), "field"))) ||
353 (!json_is_string(jffield)) ||
354 (NULL == (jfmatch = json_object_get(json_array_get(jfspecs, j), "match"))) ||
355 (!json_is_string(jfmatch))) {
356 tr_debug("tr_cfg_parse_one_filter: Error parsing filter field and match for filter spec %d, filter line %d.", i, j);
357 *rc = TR_CFG_NOPARSE;
358 tr_filter_free(filt);
362 if (NULL == (filt->lines[i]->specs[j] = talloc(trc, TR_FSPEC))) {
363 tr_debug("tr_cfg_parse_one_filter: Out of memory.");
365 tr_filter_free(filt);
369 memset(filt->lines[i]->specs[j], 0, sizeof(TR_FSPEC));
371 if ((NULL == (filt->lines[i]->specs[j]->field = tr_new_name((char *)json_string_value(jffield)))) ||
372 (NULL == (filt->lines[i]->specs[j]->match = tr_new_name((char *)json_string_value(jfmatch))))) {
373 tr_debug("tr_cfg_parse_one_filter: Out of memory.");
375 tr_filter_free(filt);
384 static TR_RP_CLIENT *tr_cfg_parse_one_rp_client (TR_CFG *trc, json_t *jrp, TR_CFG_RC *rc)
386 TR_RP_CLIENT *rp = NULL;
388 json_t *jfilt = NULL;
389 json_t *jftype = NULL;
392 if ((!trc) || (!jrp) || (!rc)) {
393 tr_debug("tr_cfg_parse_one_rp_realm: Bad parameters.");
395 *rc = TR_CFG_BAD_PARAMS;
399 if ((NULL == (jgns = json_object_get(jrp, "gss_names"))) ||
400 (!json_is_array(jgns))) {
401 tr_debug("tr_cfg_parse_one_rp_client: Error parsing RP client configuration, no GSS names.");
402 *rc = TR_CFG_NOPARSE;
406 /* TBD -- Support more than one filter per RP client? */
407 if (NULL == (jfilt = json_object_get(jrp, "filter"))) {
408 tr_debug("tr_cfg_parse_one_rp_client: Error parsing RP client configuration, no filter.");
409 *rc = TR_CFG_NOPARSE;
413 /* We only support rp_permitted filters for RP clients */
414 if ((NULL == (jftype = json_object_get(jfilt, "type"))) ||
415 (!json_is_string(jftype)) ||
416 (strcmp(json_string_value(jftype), "rp_permitted"))) {
417 tr_debug("tr_cfg_parse_one_rp_client: Error parsing RP client filter type.");
418 *rc = TR_CFG_NOPARSE;
422 if (TR_MAX_GSS_NAMES < json_array_size(jgns)) {
423 tr_debug("tr_cfg_parse_one_rp_client: RP Client has too many GSS Names.");
424 *rc = TR_CFG_NOPARSE;
428 if (NULL == (rp = talloc(trc, TR_RP_CLIENT))) {
429 tr_debug("tr_cfg_parse_one_rp_realm: Out of memory.");
434 memset(rp, 0, sizeof(TR_RP_CLIENT));
436 /* TBD -- support more than one filter entry per RP Client? */
437 if (NULL == (rp->filter = tr_cfg_parse_one_filter(trc, jfilt, rc))) {
438 tr_debug("tr_cfg_parse_one_rp_client: Error parsing filter.");
439 *rc = TR_CFG_NOPARSE;
443 for (i = 0; i < json_array_size(jgns); i++) {
444 if (NULL == (rp->gss_names[i] = tr_new_name ((char *)json_string_value(json_array_get(jgns, i))))) {
445 tr_debug("tr_cfg_parse_one_rp_client: No memory for GSS Name.");
454 static TR_CFG_RC tr_cfg_parse_rp_clients (TR_CFG *trc, json_t *jcfg) {
456 TR_RP_CLIENT *rp = NULL;
457 TR_CFG_RC rc = TR_CFG_SUCCESS;
460 if ((!trc) || (!jcfg))
461 return TR_CFG_BAD_PARAMS;
463 if (NULL != (jrps = json_object_get(jcfg, "rp_clients"))) {
465 if (!json_is_array(jrps)) {
466 return TR_CFG_NOPARSE;
469 for (i = 0; i < json_array_size(jrps); i++) {
470 if (NULL == (rp = tr_cfg_parse_one_rp_client(trc,
471 json_array_get(jrps, i),
475 tr_debug("tr_cfg_parse_rp_clients: RP client configured -- first gss: %s", rp->gss_names[0]->buf);
476 rp->next = trc->rp_clients;
477 trc->rp_clients = rp;
483 static TR_AAA_SERVER *tr_cfg_parse_one_aaa_server (TR_CFG *trc, json_t *jaddr, TR_CFG_RC *rc) {
484 TR_AAA_SERVER *aaa = NULL;
486 if ((!trc) || (!jaddr) || (!json_is_string(jaddr))) {
487 tr_debug("tr_cfg_parse_one_aaa_server: Bad parameters.");
488 *rc = TR_CFG_BAD_PARAMS;
492 if (NULL == (aaa = talloc(trc, TR_AAA_SERVER))) {
493 tr_debug("tr_cfg_parse_one_aaa_server: Out of memory.");
498 memset(aaa, 0, sizeof(TR_AAA_SERVER));
500 aaa->hostname = tr_new_name((char *)(json_string_value(jaddr)));
505 static TR_AAA_SERVER *tr_cfg_parse_aaa_servers (TR_CFG *trc, json_t *jaaas, TR_CFG_RC *rc)
507 TR_AAA_SERVER *aaa = NULL;
508 TR_AAA_SERVER *temp_aaa = NULL;
511 for (i = 0; i < json_array_size(jaaas); i++) {
512 if (NULL == (temp_aaa = tr_cfg_parse_one_aaa_server(trc, json_array_get(jaaas, i), rc))) {
515 /* TBD -- IPv6 addresses */
516 // tr_debug("tr_cfg_parse_aaa_servers: Configuring AAA Server: ip_addr = %s.", inet_ntoa(temp_aaa->aaa_server_addr));
517 temp_aaa->next = aaa;
523 static TR_APC *tr_cfg_parse_apcs (TR_CFG *trc, json_t *japcs, TR_CFG_RC *rc)
527 *rc = TR_CFG_SUCCESS; /* presume success */
529 if ((!trc) || (!japcs) || (!rc)) {
530 tr_debug("tr_cfg_parse_apcs: Bad parameters.");
532 *rc = TR_CFG_BAD_PARAMS;
536 if (NULL == (apc = talloc(trc, TR_APC))) {
537 tr_debug("tr_cfg_parse_apcs: Out of memory.");
542 memset(apc, 0, sizeof(TR_APC));
544 /* TBD, deal with more than one APC. In the meantime, though... */
545 /* Only parse the first APC, because we only know how to deal with one, anyway. */
546 if (0 == json_array_size(japcs))
549 if (NULL == (apc->id = tr_new_name((char *)json_string_value(json_array_get(japcs, 0))))) {
550 tr_debug("tr_cfg_parse_apcs: No memory for APC name.");
558 static TR_IDP_REALM *tr_cfg_parse_one_idp_realm (TR_CFG *trc, json_t *jidp, TR_CFG_RC *rc) {
559 TR_IDP_REALM *idp = NULL;
561 json_t *jscfg = NULL;
562 json_t *jsrvrs = NULL;
563 json_t *japcs = NULL;
565 if ((!trc) || (!jidp) || (!rc)) {
566 tr_debug("tr_cfg_parse_one_idp_realm: Bad parameters.");
568 *rc = TR_CFG_BAD_PARAMS;
572 if (NULL == (idp = talloc(trc, TR_IDP_REALM))) {
573 tr_debug("tr_cfg_parse_one_idp_realm: Out of memory.");
578 memset(idp, 0, sizeof(TR_IDP_REALM));
580 if ((NULL == (jrid = json_object_get(jidp, "realm_id"))) ||
581 (!json_is_string(jrid)) ||
582 (NULL == (jscfg = json_object_get(jidp, "shared_config"))) ||
583 (!json_is_string(jscfg)) ||
584 (NULL == (jsrvrs = json_object_get(jidp, "aaa_servers"))) ||
585 (!json_is_array(jsrvrs))) {
586 tr_debug("tr_cfg_parse_one_idp_realm: Error parsing IDP realm configuration.");
587 *rc = TR_CFG_NOPARSE;
591 if (0 == strcmp(json_string_value(jscfg), "no")) {
592 idp->shared_config = 0;
594 idp->shared_config = 1;
597 if (NULL == (idp->realm_id = tr_new_name((char *)json_string_value(jrid)))) {
598 tr_debug("tr_cfg_parse_one_idp_realm: No memory for realm id.");
603 if (NULL == (idp->aaa_servers = tr_cfg_parse_aaa_servers(trc, jsrvrs, rc))) {
604 tr_debug("tr_cfg_parse_one_idp_realm: Can't parse AAA servers for realm %s.", idp->realm_id->buf);
605 tr_free_name(idp->realm_id);
609 if ((NULL != (japcs = json_object_get(jidp, "apcs"))) &&
610 (json_is_array(japcs))) {
611 if (NULL == (idp->apcs = tr_cfg_parse_apcs(trc, japcs, rc))) {
612 tr_debug("tr_cfg_parse_one_idp_realm: Can't parse APCs for realm %s .", idp->realm_id->buf);
613 tr_free_name(idp->realm_id);
614 /* TBD -- free aaa_servers */;
621 static TR_CFG_RC tr_cfg_parse_default_servers (TR_CFG *trc, json_t *jcfg)
624 TR_CFG_RC rc = TR_CFG_SUCCESS;
625 TR_AAA_SERVER *ds = NULL;
628 if ((!trc) || (!jcfg))
629 return TR_CFG_BAD_PARAMS;
631 /* If there are default servers, store them */
632 if ((NULL != (jdss = json_object_get(jcfg, "default_servers"))) &&
633 (json_is_array(jdss)) &&
634 (0 < json_array_size(jdss))) {
636 for (i = 0; i < json_array_size(jdss); i++) {
637 if (NULL == (ds = tr_cfg_parse_one_aaa_server(trc,
638 json_array_get(jdss, i),
642 tr_debug("tr_cfg_parse_default_servers: Default server configured: %s", ds->hostname->buf);
643 ds->next = trc->default_servers;
644 trc->default_servers = ds;
651 static TR_CFG_RC tr_cfg_parse_idp_realms (TR_CFG *trc, json_t *jcfg)
653 json_t *jidps = NULL;
654 TR_CFG_RC rc = TR_CFG_SUCCESS;
655 TR_IDP_REALM *idp = NULL;
658 if ((!trc) || (!jcfg))
659 return TR_CFG_BAD_PARAMS;
661 /* If there are any IDP Realms, parse them */
662 if ((NULL != (jidps = json_object_get(jcfg, "idp_realms"))) &&
663 (json_is_array(jidps))) {
664 for (i = 0; i < json_array_size(jidps); i++) {
665 if (NULL == (idp = tr_cfg_parse_one_idp_realm(trc,
666 json_array_get(jidps, i),
670 tr_debug("tr_cfg_parse_idp_realms: IDP realm configured: %s.", idp->realm_id->buf);
671 idp->next = trc->idp_realms;
672 trc->idp_realms = idp;
679 static TR_IDP_REALM *tr_cfg_parse_comm_idps (TR_CFG *trc, json_t *jidps, TR_CFG_RC *rc)
681 TR_IDP_REALM *idp = NULL;
682 TR_IDP_REALM *found_idp = NULL;
683 TR_IDP_REALM *temp_idp = NULL;
688 (!json_is_array(jidps))) {
690 *rc = TR_CFG_BAD_PARAMS;
694 for (i = 0; i < json_array_size(jidps); i++) {
695 if (NULL == (temp_idp = talloc(trc, TR_IDP_REALM))) {
696 tr_debug("tr_cfg_parse_comm_idps: Can't allocate memory for IdP Realm.");
701 memset (temp_idp, 0, sizeof(TR_IDP_REALM));
703 if (NULL == (found_idp = (tr_cfg_find_idp(trc,
704 tr_new_name((char *)json_string_value(json_array_get(jidps, i))),
706 tr_debug("tr_cfg_parse_comm_idps: Unknown IDP %s.",
707 (char *)json_string_value(json_array_get(jidps, i)));
711 // We *MUST* do a dereferenced copy here or the second community will corrupt the linked list we create here.
712 *temp_idp = *found_idp;
714 temp_idp->comm_next = idp;
721 static TR_RP_REALM *tr_cfg_parse_comm_rps (TR_CFG *trc, json_t *jrps, TR_CFG_RC *rc)
723 TR_RP_REALM *rp = NULL;
724 TR_RP_REALM *temp_rp = NULL;
729 (!json_is_array(jrps))) {
731 *rc = TR_CFG_BAD_PARAMS;
735 for (i = (json_array_size(jrps)-1); i >= 0; i--) {
736 if (NULL == (temp_rp = talloc(trc, TR_RP_REALM))) {
737 tr_debug("tr_cfg_parse_comm_rps: Can't allocate memory for RP Realm.");
742 memset (temp_rp, 0, sizeof(TR_RP_REALM));
744 if (NULL == (temp_rp->realm_name = tr_new_name((char *)json_string_value(json_array_get(jrps, i))))) {
745 tr_debug("tr_cfg_parse_comm_rps: No memory for RP Realm Name.");
758 static TR_COMM *tr_cfg_parse_one_comm (TR_CFG *trc, json_t *jcomm, TR_CFG_RC *rc) {
759 TR_COMM *comm = NULL;
761 json_t *jtype = NULL;
762 json_t *japcs = NULL;
763 json_t *jidps = NULL;
766 if ((!trc) || (!jcomm) || (!rc)) {
767 tr_debug("tr_cfg_parse_one_comm: Bad parameters.");
769 *rc = TR_CFG_BAD_PARAMS;
773 if (NULL == (comm = talloc_zero(trc, TR_COMM))) {
774 tr_crit("tr_cfg_parse_one_comm: Out of memory.");
780 if ((NULL == (jid = json_object_get(jcomm, "community_id"))) ||
781 (!json_is_string(jid)) ||
782 (NULL == (jtype = json_object_get(jcomm, "type"))) ||
783 (!json_is_string(jtype)) ||
784 (NULL == (japcs = json_object_get(jcomm, "apcs"))) ||
785 (!json_is_array(japcs)) ||
786 (NULL == (jidps = json_object_get(jcomm, "idp_realms"))) ||
787 (!json_is_array(jidps)) ||
788 (NULL == (jrps = json_object_get(jcomm, "rp_realms"))) ||
789 (!json_is_array(jrps))) {
790 tr_debug("tr_cfg_parse_one_comm: Error parsing Communities configuration.");
791 *rc = TR_CFG_NOPARSE;
795 if (NULL == (comm->id = tr_new_name((char *)json_string_value(jid)))) {
796 tr_debug("tr_cfg_parse_one_comm: No memory for community id.");
801 if (0 == strcmp(json_string_value(jtype), "apc")) {
802 comm->type = TR_COMM_APC;
803 } else if (0 == strcmp(json_string_value(jtype), "coi")) {
804 comm->type = TR_COMM_COI;
805 if (NULL == (comm->apcs = tr_cfg_parse_apcs(trc, japcs, rc))) {
806 tr_debug("tr_cfg_parse_one_comm: Can't parse APCs for COI %s.", comm->id->buf);
807 tr_free_name(comm->id);
811 tr_debug("tr_cfg_parse_one_comm: Invalid community type, comm = %s, type = %s", comm->id->buf, json_string_value(jtype));
812 tr_free_name(comm->id);
813 *rc = TR_CFG_NOPARSE;
817 comm->idp_realms = tr_cfg_parse_comm_idps(trc, jidps, rc);
818 if (TR_CFG_SUCCESS != *rc) {
819 tr_debug("tr_cfg_parse_one_comm: Can't parse IDP realms for comm %s.", comm->id->buf);
820 tr_free_name(comm->id);
824 comm->rp_realms = tr_cfg_parse_comm_rps(trc, jrps, rc);
825 if (TR_CFG_SUCCESS != *rc) {
826 tr_debug("tr_cfg_parse_comm: Can't parse RP realms for comm %s .", comm->id->buf);
827 tr_free_name(comm->id);
831 if (TR_COMM_APC == comm->type) {
832 json_t *jexpire = json_object_get(jcomm, "expiration_interval");
833 comm->expiration_interval = 43200; /*30 days*/
835 if (!json_is_integer(jexpire)) {
836 fprintf(stderr, "tr_parse_comm: expirae_interval is not an integer\n");
839 comm->expiration_interval = json_integer_value(jexpire);
840 if (comm->expiration_interval <= 10)
841 comm->expiration_interval = 11; /* Freeradius waits 10 minutes between successful TR queries*/
842 if (comm->expiration_interval > 129600) /* 90 days*/
843 comm->expiration_interval = 129600;
850 static TR_CFG_RC tr_cfg_parse_comms (TR_CFG *trc, json_t *jcfg)
852 json_t *jcomms = NULL;
853 TR_CFG_RC rc = TR_CFG_SUCCESS;
854 TR_COMM *comm = NULL;
857 if ((!trc) || (!jcfg)) {
858 tr_debug("tr_cfg_parse_comms: Bad Parameters.");
859 return TR_CFG_BAD_PARAMS;
862 if (NULL != (jcomms = json_object_get(jcfg, "communities"))) {
863 if (!json_is_array(jcomms)) {
864 return TR_CFG_NOPARSE;
867 for (i = 0; i < json_array_size(jcomms); i++) {
868 if (NULL == (comm = tr_cfg_parse_one_comm(trc,
869 json_array_get(jcomms, i),
873 tr_debug("tr_cfg_parse_comms: Community configured: %s.", comm->id->buf);
874 comm->next = trc->comms;
881 TR_CFG_RC tr_cfg_validate (TR_CFG *trc) {
882 TR_CFG_RC rc = TR_CFG_SUCCESS;
885 return TR_CFG_BAD_PARAMS;
887 if ((NULL == trc->internal)||
888 (NULL == trc->internal->hostname)) {
889 tr_debug("tr_cfg_validate: Error: No internal configuration, or no hostname.");
893 if (NULL == trc->rp_clients) {
894 tr_debug("tr_cfg_validate: Error: No RP Clients configured");
898 if (NULL == trc->comms) {
899 tr_debug("tr_cfg_validate: Error: No Communities configured");
903 if ((NULL == trc->default_servers) && (NULL == trc->idp_realms)) {
904 tr_debug("tr_cfg_validate: Error: No default servers or IDPs configured.");
911 /* Join two paths and return a pointer to the result. This should be freed
912 * via talloc_free. Returns NULL on failure. */
913 static char *join_paths(const char *p1, const char *p2) {
914 return talloc_asprintf(NULL, "%s/%s", p1, p2); /* returns NULL on a failure */
917 /* Reads configuration files in config_dir ("" or "./" will use the current directory) */
918 TR_CFG_RC tr_parse_config (TR_INSTANCE *tr, const char *config_dir, int n, struct dirent **cfg_files) {
922 char *file_with_path;
924 if ((!tr) || (!cfg_files) || (n<=0))
925 return TR_CFG_BAD_PARAMS;
927 /* If there is a partial/abandoned config lying around, free it */
929 tr_cfg_free(tr->new_cfg);
931 if (NULL == (tr->new_cfg = talloc(NULL, TR_CFG)))
934 memset(tr->new_cfg, 0, sizeof(TR_CFG));
936 /* Parse configuration information from each config file */
938 file_with_path=join_paths(config_dir, cfg_files[n]->d_name); /* must free result with talloc_free */
939 if(file_with_path == NULL) {
940 tr_crit("tr_parse_config: error joining path.");
943 tr_debug("tr_parse_config: Parsing %s.", cfg_files[n]->d_name); /* print the filename without the path */
944 if (NULL == (jcfg = json_load_file(file_with_path,
945 JSON_DISABLE_EOF_CHECK, &rc))) {
946 tr_debug("tr_parse_config: Error parsing config file %s.",
947 cfg_files[n]->d_name);
948 talloc_free(file_with_path);
949 return TR_CFG_NOPARSE;
951 talloc_free(file_with_path); /* done with filename */
953 // Look for serial number and log it if it exists
954 if (NULL != (jser = json_object_get(jcfg, "serial_number"))) {
955 if (json_is_number(jser)) {
956 tr_notice("tr_read_config: Attempting to load revision %" JSON_INTEGER_FORMAT " of '%s'.",
957 json_integer_value(jser),
958 cfg_files[n]->d_name);
962 if ((TR_CFG_SUCCESS != tr_cfg_parse_internal(tr->new_cfg, jcfg)) ||
963 (TR_CFG_SUCCESS != tr_cfg_parse_rp_clients(tr->new_cfg, jcfg)) ||
964 (TR_CFG_SUCCESS != tr_cfg_parse_idp_realms(tr->new_cfg, jcfg)) ||
965 (TR_CFG_SUCCESS != tr_cfg_parse_default_servers(tr->new_cfg, jcfg)) ||
966 (TR_CFG_SUCCESS != tr_cfg_parse_comms(tr->new_cfg, jcfg))) {
967 tr_cfg_free(tr->new_cfg);
972 /* make sure we got a complete, consistent configuration */
973 if (TR_CFG_SUCCESS != tr_cfg_validate(tr->new_cfg)) {
974 tr_debug("tr_parse_config: Error: INVALID CONFIGURATION, EXITING");
978 return TR_CFG_SUCCESS;
981 TR_IDP_REALM *tr_cfg_find_idp (TR_CFG *tr_cfg, TR_NAME *idp_id, TR_CFG_RC *rc)
984 TR_IDP_REALM *cfg_idp;
986 if ((!tr_cfg) || (!idp_id)) {
988 *rc = TR_CFG_BAD_PARAMS;
992 for (cfg_idp = tr_cfg->idp_realms; NULL != cfg_idp; cfg_idp = cfg_idp->next) {
993 if (!tr_name_cmp (idp_id, cfg_idp->realm_id)) {
994 tr_debug("tr_cfg_find_idp: Found %s.", idp_id->buf);
998 /* if we didn't find one, return NULL */
1002 TR_RP_CLIENT *tr_cfg_find_rp (TR_CFG *tr_cfg, TR_NAME *rp_gss, TR_CFG_RC *rc)
1004 TR_RP_CLIENT *cfg_rp;
1007 if ((!tr_cfg) || (!rp_gss)) {
1009 *rc = TR_CFG_BAD_PARAMS;
1013 for (cfg_rp = tr_cfg->rp_clients; NULL != cfg_rp; cfg_rp = cfg_rp->next) {
1014 for (i = 0; i < TR_MAX_GSS_NAMES; i++) {
1015 if (!tr_name_cmp (rp_gss, cfg_rp->gss_names[i])) {
1016 tr_debug("tr_cfg_find_rp: Found %s.", rp_gss->buf);
1021 /* if we didn't find one, return NULL */
1025 static int is_cfg_file(const struct dirent *dent) {
1028 /* Only accept filenames ending in ".cfg" and starting with a character
1029 * other than an ASCII '.' */
1031 /* filename must be at least 4 characters long to be acceptable */
1032 n=strlen(dent->d_name);
1037 /* filename must not start with '.' */
1038 if ('.' == dent->d_name[0]) {
1042 /* If the above passed and the last four characters of the filename are .cfg, accept.
1043 * (n.b., assumes an earlier test checked that the name is >= 4 chars long.) */
1044 if (0 == strcmp(&(dent->d_name[n-4]), ".cfg")) {
1048 /* otherwise, return false. */
1052 /* Find configuration files in a particular directory. Returns the
1053 * number of entries found, 0 if none are found, or <0 for some
1054 * errors. If n>=0, the cfg_files parameter will contain a newly
1055 * allocated array of pointers to struct dirent entries, as returned
1056 * by scandir(). These can be freed with tr_free_config_file_list().
1058 int tr_find_config_files (const char *config_dir, struct dirent ***cfg_files) {
1061 n = scandir(config_dir, cfg_files, &is_cfg_file, 0);
1065 tr_debug("tr_find_config: scandir error trying to scan %s.", config_dir);
1066 } else if (n == 0) {
1067 tr_debug("tr_find_config: No config files found.");
1071 tr_debug("tr_find_config: Config file found (%s).", (*cfg_files)[i]->d_name);
1078 /* Free memory allocated for configuration file list returned from tr_find_config_files().
1079 * This can be called regardless of the return value of tr_find_config_values(). */
1080 void tr_free_config_file_list(int n, struct dirent ***cfg_files) {
1083 /* if n < 0, then scandir did not allocate anything because it failed */
1084 if((n>=0) && (*cfg_files != NULL)) {
1085 for(ii=0; ii<n; ii++) {
1086 free((*cfg_files)[ii]);
1088 free(*cfg_files); /* safe even if n==0 */
1089 *cfg_files=NULL; /* this will help prevent accidentally freeing twice */