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 void tr_cfg_free (TR_CFG *cfg) {
57 TR_CFG_RC tr_apply_new_config (TR_INSTANCE *tr) {
59 return TR_CFG_BAD_PARAMS;
62 tr_cfg_free(tr->active_cfg);
64 tr->active_cfg = tr->new_cfg;
65 tr->new_cfg=NULL; /* only keep a single handle on the new configuration */
67 tr_log_threshold(tr->active_cfg->internal->log_threshold);
68 tr_console_threshold(tr->active_cfg->internal->console_threshold);
70 return TR_CFG_SUCCESS;
73 static TR_CFG_RC tr_cfg_parse_internal (TR_CFG *trc, json_t *jcfg) {
77 json_t *jhname = NULL;
79 json_t *jconthres = NULL;
80 json_t *jlogthres = NULL;
82 if ((!trc) || (!jcfg))
83 return TR_CFG_BAD_PARAMS;
85 if (NULL == trc->internal) {
86 if (NULL == (trc->internal = talloc(trc, TR_CFG_INTERNAL)))
89 memset(trc->internal, 0, sizeof(TR_CFG_INTERNAL));
92 if (NULL != (jint = json_object_get(jcfg, "tr_internal"))) {
93 if (NULL != (jmtd = json_object_get(jint, "max_tree_depth"))) {
94 if (json_is_number(jmtd)) {
95 trc->internal->max_tree_depth = json_integer_value(jmtd);
97 tr_debug("tr_cfg_parse_internal: Parsing error, max_tree_depth is not a number.");
98 return TR_CFG_NOPARSE;
101 /* If not configured, use the default */
102 trc->internal->max_tree_depth = TR_DEFAULT_MAX_TREE_DEPTH;
104 if (NULL != (jtp = json_object_get(jint, "tids_port"))) {
105 if (json_is_number(jtp)) {
106 trc->internal->tids_port = json_integer_value(jtp);
108 tr_debug("tr_cfg_parse_internal: Parsing error, port is not a number.");
109 return TR_CFG_NOPARSE;
112 /* If not configured, use the default */
113 trc->internal->tids_port = TR_DEFAULT_TIDS_PORT;
115 if (NULL != (jhname = json_object_get(jint, "hostname"))) {
116 if (json_is_string(jhname)) {
117 trc->internal->hostname = json_string_value(jhname);
119 tr_debug("tr_cfg_parse_internal: Parsing error, hostname is not a string.");
120 return TR_CFG_NOPARSE;
124 if (NULL != (jlog = json_object_get(jint, "logging"))) {
125 if (NULL != (jlogthres = json_object_get(jlog, "log_threshold"))) {
126 if (json_is_string(jlogthres)) {
127 trc->internal->log_threshold = str2sev(json_string_value(jlogthres));
129 tr_debug("tr_cfg_parse_internal: Parsing error, log_threshold is not a string.");
130 return TR_CFG_NOPARSE;
133 /* If not configured, use the default */
134 trc->internal->log_threshold = TR_DEFAULT_LOG_THRESHOLD;
137 if (NULL != (jconthres = json_object_get(jlog, "console_threshold"))) {
138 if (json_is_string(jconthres)) {
139 trc->internal->console_threshold = str2sev(json_string_value(jconthres));
141 tr_debug("tr_cfg_parse_internal: Parsing error, console_threshold is not a string.");
142 return TR_CFG_NOPARSE;
145 /* If not configured, use the default */
146 trc->internal->console_threshold = TR_DEFAULT_CONSOLE_THRESHOLD;
149 /* If not configured, use the default */
150 trc->internal->console_threshold = TR_DEFAULT_CONSOLE_THRESHOLD;
151 trc->internal->log_threshold = TR_DEFAULT_LOG_THRESHOLD;
154 tr_debug("tr_cfg_parse_internal: Internal config parsed.");
155 return TR_CFG_SUCCESS;
157 return TR_CFG_SUCCESS;
160 static TR_CONSTRAINT *tr_cfg_parse_one_constraint (TR_CFG *trc, char *ctype, json_t *jc, TR_CFG_RC *rc)
165 if ((!trc) || (!ctype) || (!jc) || (!rc) ||
166 (!json_is_array(jc)) ||
167 (0 >= json_array_size(jc)) ||
168 (TR_MAX_CONST_MATCHES < json_array_size(jc)) ||
169 (!json_is_string(json_array_get(jc, 0)))) {
170 tr_debug("tr_cfg_parse_one_constraint: config error.");
171 *rc = TR_CFG_NOPARSE;
175 if (NULL == (cons = talloc(trc, TR_CONSTRAINT))) {
176 tr_debug("tr_cfg_parse_one_constraint: Out of memory (cons).");
181 memset(cons, 0, sizeof(TR_CONSTRAINT));
183 if (NULL == (cons->type = tr_new_name(ctype))) {
184 tr_debug("tr_cfg_parse_one_constraint: Out of memory (type).");
189 for (i = 0; i < json_array_size(jc); i++) {
190 cons->matches[i] = tr_new_name((char *)(json_string_value(json_array_get(jc, i))));
196 static TR_FILTER *tr_cfg_parse_one_filter (TR_CFG *trc, json_t *jfilt, TR_CFG_RC *rc)
198 TR_FILTER *filt = NULL;
199 json_t *jftype = NULL;
201 json_t *jfaction = NULL;
202 json_t *jfspecs = NULL;
203 json_t *jffield = NULL;
204 json_t *jfmatch = NULL;
209 if ((NULL == (jftype = json_object_get(jfilt, "type"))) ||
210 (!json_is_string(jftype))) {
211 tr_debug("tr_cfg_parse_one_filter: Error parsing filter type.");
212 *rc = TR_CFG_NOPARSE;
216 if ((NULL == (jfls = json_object_get(jfilt, "filter_lines"))) ||
217 (!json_is_array(jfls))) {
218 tr_debug("tr_cfg_parse_one_filter: Error parsing filter type.");
219 *rc = TR_CFG_NOPARSE;
223 if (TR_MAX_FILTER_LINES < json_array_size(jfls)) {
224 tr_debug("tr_cfg_parse_one_filter: Filter has too many filter_lines, maximimum of %d.", TR_MAX_FILTER_LINES);
225 *rc = TR_CFG_NOPARSE;
229 if (NULL == (filt = talloc(trc, TR_FILTER))) {
230 tr_debug("tr_cfg_parse_one_filter: Out of memory.");
235 memset(filt, 0, sizeof(TR_FILTER));
237 if (!strcmp(json_string_value(jftype), "rp_permitted")) {
238 filt->type = TR_FILTER_TYPE_RP_PERMITTED;
241 tr_debug("tr_cfg_parse_one_filter: Error parsing filter type, unknown type '%s'.", json_string_value(jftype));
242 *rc = TR_CFG_NOPARSE;
243 tr_filter_free(filt);
247 /* For each filter line... */
248 for (i = 0; i < json_array_size(jfls); i++) {
250 if ((NULL == (jfaction = json_object_get(json_array_get(jfls, i), "action"))) ||
251 (!json_is_string(jfaction))) {
252 tr_debug("tr_cfg_parse_one_filter: Error parsing filter action.");
253 *rc = TR_CFG_NOPARSE;
254 tr_filter_free(filt);
258 if ((NULL == (jfspecs = json_object_get(json_array_get(jfls, i), "filter_specs"))) ||
259 (!json_is_array(jfspecs)) ||
260 (0 == json_array_size(jfspecs))) {
261 tr_debug("tr_cfg_parse_one_filter: Error parsing filter specs.");
262 *rc = TR_CFG_NOPARSE;
263 tr_filter_free(filt);
267 if (TR_MAX_FILTER_SPECS < json_array_size(jfspecs)) {
268 tr_debug("tr_cfg_parse_one_filter: Filter has too many filter_specs, maximimum of %d.", TR_MAX_FILTER_SPECS);
269 *rc = TR_CFG_NOPARSE;
270 tr_filter_free(filt);
274 if (NULL == (filt->lines[i] = talloc(trc, TR_FLINE))) {
275 tr_debug("tr_cfg_parse_one_filter: Out of memory (fline).");
277 tr_filter_free(filt);
281 memset(filt->lines[i], 0, sizeof(TR_FLINE));
283 if (!strcmp(json_string_value(jfaction), "accept")) {
284 filt->lines[i]->action = TR_FILTER_ACTION_ACCEPT;
286 else if (!strcmp(json_string_value(jfaction), "reject")) {
287 filt->lines[i]->action = TR_FILTER_ACTION_REJECT;
290 tr_debug("tr_cfg_parse_one_filter: Error parsing filter action, unknown action' %s'.", json_string_value(jfaction));
291 *rc = TR_CFG_NOPARSE;
292 tr_filter_free(filt);
296 if ((NULL != (jrc = json_object_get(json_array_get(jfls, i), "realm_constraints"))) &&
297 (json_is_array(jrc)) &&
298 (0 != json_array_size(jrc)) &&
299 (TR_MAX_CONST_MATCHES >= json_array_size(jrc))) {
301 if (NULL == (filt->lines[i]->realm_cons = tr_cfg_parse_one_constraint(trc, "realm", jrc, rc))) {
302 tr_debug("tr_cfg_parse_one_filter: Error parsing realm constraint");
303 tr_filter_free(filt);
308 if ((NULL != (jdc = json_object_get(json_array_get(jfls, i), "domain_constraints"))) &&
309 (json_is_array(jdc)) &&
310 (0 != json_array_size(jdc)) &&
311 (TR_MAX_CONST_MATCHES >= json_array_size(jdc))) {
313 if (NULL == (filt->lines[i]->domain_cons = tr_cfg_parse_one_constraint(trc, "domain", jdc, rc))) {
314 tr_debug("tr_cfg_parse_one_filter: Error parsing domain constraint");
315 tr_filter_free(filt);
320 /*For each filter spec within the filter line... */
321 for (j = 0; j <json_array_size(jfspecs); j++) {
323 if ((NULL == (jffield = json_object_get(json_array_get(jfspecs, j), "field"))) ||
324 (!json_is_string(jffield)) ||
325 (NULL == (jfmatch = json_object_get(json_array_get(jfspecs, j), "match"))) ||
326 (!json_is_string(jfmatch))) {
327 tr_debug("tr_cfg_parse_one_filter: Error parsing filter field and match for filter spec %d, filter line %d.", i, j);
328 *rc = TR_CFG_NOPARSE;
329 tr_filter_free(filt);
333 if (NULL == (filt->lines[i]->specs[j] = talloc(trc, TR_FSPEC))) {
334 tr_debug("tr_cfg_parse_one_filter: Out of memory.");
336 tr_filter_free(filt);
340 memset(filt->lines[i]->specs[j], 0, sizeof(TR_FSPEC));
342 if ((NULL == (filt->lines[i]->specs[j]->field = tr_new_name((char *)json_string_value(jffield)))) ||
343 (NULL == (filt->lines[i]->specs[j]->match = tr_new_name((char *)json_string_value(jfmatch))))) {
344 tr_debug("tr_cfg_parse_one_filter: Out of memory.");
346 tr_filter_free(filt);
355 static TR_RP_CLIENT *tr_cfg_parse_one_rp_client (TR_CFG *trc, json_t *jrp, TR_CFG_RC *rc)
357 TR_RP_CLIENT *rp = NULL;
359 json_t *jfilt = NULL;
360 json_t *jftype = NULL;
363 if ((!trc) || (!jrp) || (!rc)) {
364 tr_debug("tr_cfg_parse_one_rp_realm: Bad parameters.");
366 *rc = TR_CFG_BAD_PARAMS;
370 if ((NULL == (jgns = json_object_get(jrp, "gss_names"))) ||
371 (!json_is_array(jgns))) {
372 tr_debug("tr_cfg_parse_one_rp_client: Error parsing RP client configuration, no GSS names.");
373 *rc = TR_CFG_NOPARSE;
377 /* TBD -- Support more than one filter per RP client? */
378 if (NULL == (jfilt = json_object_get(jrp, "filter"))) {
379 tr_debug("tr_cfg_parse_one_rp_client: Error parsing RP client configuration, no filter.");
380 *rc = TR_CFG_NOPARSE;
384 /* We only support rp_permitted filters for RP clients */
385 if ((NULL == (jftype = json_object_get(jfilt, "type"))) ||
386 (!json_is_string(jftype)) ||
387 (strcmp(json_string_value(jftype), "rp_permitted"))) {
388 tr_debug("tr_cfg_parse_one_rp_client: Error parsing RP client filter type.");
389 *rc = TR_CFG_NOPARSE;
393 if (TR_MAX_GSS_NAMES < json_array_size(jgns)) {
394 tr_debug("tr_cfg_parse_one_rp_client: RP Client has too many GSS Names.");
395 *rc = TR_CFG_NOPARSE;
399 if (NULL == (rp = talloc(trc, TR_RP_CLIENT))) {
400 tr_debug("tr_cfg_parse_one_rp_realm: Out of memory.");
405 memset(rp, 0, sizeof(TR_RP_CLIENT));
407 /* TBD -- support more than one filter entry per RP Client? */
408 if (NULL == (rp->filter = tr_cfg_parse_one_filter(trc, jfilt, rc))) {
409 tr_debug("tr_cfg_parse_one_rp_client: Error parsing filter.");
410 *rc = TR_CFG_NOPARSE;
414 for (i = 0; i < json_array_size(jgns); i++) {
415 if (NULL == (rp->gss_names[i] = tr_new_name ((char *)json_string_value(json_array_get(jgns, i))))) {
416 tr_debug("tr_cfg_parse_one_rp_client: No memory for GSS Name.");
425 static TR_CFG_RC tr_cfg_parse_rp_clients (TR_CFG *trc, json_t *jcfg) {
427 TR_RP_CLIENT *rp = NULL;
428 TR_CFG_RC rc = TR_CFG_SUCCESS;
431 if ((!trc) || (!jcfg))
432 return TR_CFG_BAD_PARAMS;
434 if (NULL != (jrps = json_object_get(jcfg, "rp_clients"))) {
436 if (!json_is_array(jrps)) {
437 return TR_CFG_NOPARSE;
440 for (i = 0; i < json_array_size(jrps); i++) {
441 if (NULL == (rp = tr_cfg_parse_one_rp_client(trc,
442 json_array_get(jrps, i),
446 tr_debug("tr_cfg_parse_rp_clients: RP client configured -- first gss: %s", rp->gss_names[0]->buf);
447 rp->next = trc->rp_clients;
448 trc->rp_clients = rp;
454 static TR_AAA_SERVER *tr_cfg_parse_one_aaa_server (TR_CFG *trc, json_t *jaddr, TR_CFG_RC *rc) {
455 TR_AAA_SERVER *aaa = NULL;
457 if ((!trc) || (!jaddr) || (!json_is_string(jaddr))) {
458 tr_debug("tr_cfg_parse_one_aaa_server: Bad parameters.");
459 *rc = TR_CFG_BAD_PARAMS;
463 if (NULL == (aaa = talloc(trc, TR_AAA_SERVER))) {
464 tr_debug("tr_cfg_parse_one_aaa_server: Out of memory.");
469 memset(aaa, 0, sizeof(TR_AAA_SERVER));
471 aaa->hostname = tr_new_name((char *)(json_string_value(jaddr)));
476 static TR_AAA_SERVER *tr_cfg_parse_aaa_servers (TR_CFG *trc, json_t *jaaas, TR_CFG_RC *rc)
478 TR_AAA_SERVER *aaa = NULL;
479 TR_AAA_SERVER *temp_aaa = NULL;
482 for (i = 0; i < json_array_size(jaaas); i++) {
483 if (NULL == (temp_aaa = tr_cfg_parse_one_aaa_server(trc, json_array_get(jaaas, i), rc))) {
486 /* TBD -- IPv6 addresses */
487 // tr_debug("tr_cfg_parse_aaa_servers: Configuring AAA Server: ip_addr = %s.", inet_ntoa(temp_aaa->aaa_server_addr));
488 temp_aaa->next = aaa;
494 static TR_APC *tr_cfg_parse_apcs (TR_CFG *trc, json_t *japcs, TR_CFG_RC *rc)
498 *rc = TR_CFG_SUCCESS; /* presume success */
500 if ((!trc) || (!japcs) || (!rc)) {
501 tr_debug("tr_cfg_parse_apcs: Bad parameters.");
503 *rc = TR_CFG_BAD_PARAMS;
507 if (NULL == (apc = talloc(trc, TR_APC))) {
508 tr_debug("tr_cfg_parse_apcs: Out of memory.");
513 memset(apc, 0, sizeof(TR_APC));
515 /* TBD, deal with more than one APC. In the meantime, though... */
516 /* Only parse the first APC, because we only know how to deal with one, anyway. */
517 if (0 == json_array_size(japcs))
520 if (NULL == (apc->id = tr_new_name((char *)json_string_value(json_array_get(japcs, 0))))) {
521 tr_debug("tr_cfg_parse_apcs: No memory for APC name.");
529 static TR_IDP_REALM *tr_cfg_parse_one_idp_realm (TR_CFG *trc, json_t *jidp, TR_CFG_RC *rc) {
530 TR_IDP_REALM *idp = NULL;
532 json_t *jscfg = NULL;
533 json_t *jsrvrs = NULL;
534 json_t *japcs = NULL;
536 if ((!trc) || (!jidp) || (!rc)) {
537 tr_debug("tr_cfg_parse_one_idp_realm: Bad parameters.");
539 *rc = TR_CFG_BAD_PARAMS;
543 if (NULL == (idp = talloc(trc, TR_IDP_REALM))) {
544 tr_debug("tr_cfg_parse_one_idp_realm: Out of memory.");
549 memset(idp, 0, sizeof(TR_IDP_REALM));
551 if ((NULL == (jrid = json_object_get(jidp, "realm_id"))) ||
552 (!json_is_string(jrid)) ||
553 (NULL == (jscfg = json_object_get(jidp, "shared_config"))) ||
554 (!json_is_string(jscfg)) ||
555 (NULL == (jsrvrs = json_object_get(jidp, "aaa_servers"))) ||
556 (!json_is_array(jsrvrs))) {
557 tr_debug("tr_cfg_parse_one_idp_realm: Error parsing IDP realm configuration.");
558 *rc = TR_CFG_NOPARSE;
562 if (0 == strcmp(json_string_value(jscfg), "no")) {
563 idp->shared_config = 0;
565 idp->shared_config = 1;
568 if (NULL == (idp->realm_id = tr_new_name((char *)json_string_value(jrid)))) {
569 tr_debug("tr_cfg_parse_one_idp_realm: No memory for realm id.");
574 if (NULL == (idp->aaa_servers = tr_cfg_parse_aaa_servers(trc, jsrvrs, rc))) {
575 tr_debug("tr_cfg_parse_one_idp_realm: Can't parse AAA servers for realm %s.", idp->realm_id->buf);
576 tr_free_name(idp->realm_id);
580 if ((NULL != (japcs = json_object_get(jidp, "apcs"))) &&
581 (json_is_array(japcs))) {
582 if (NULL == (idp->apcs = tr_cfg_parse_apcs(trc, japcs, rc))) {
583 tr_debug("tr_cfg_parse_one_idp_realm: Can't parse APCs for realm %s .", idp->realm_id->buf);
584 tr_free_name(idp->realm_id);
585 /* TBD -- free aaa_servers */;
592 static TR_CFG_RC tr_cfg_parse_default_servers (TR_CFG *trc, json_t *jcfg)
595 TR_CFG_RC rc = TR_CFG_SUCCESS;
596 TR_AAA_SERVER *ds = NULL;
599 if ((!trc) || (!jcfg))
600 return TR_CFG_BAD_PARAMS;
602 /* If there are default servers, store them */
603 if ((NULL != (jdss = json_object_get(jcfg, "default_servers"))) &&
604 (json_is_array(jdss)) &&
605 (0 < json_array_size(jdss))) {
607 for (i = 0; i < json_array_size(jdss); i++) {
608 if (NULL == (ds = tr_cfg_parse_one_aaa_server(trc,
609 json_array_get(jdss, i),
613 tr_debug("tr_cfg_parse_default_servers: Default server configured: %s", ds->hostname->buf);
614 ds->next = trc->default_servers;
615 trc->default_servers = ds;
622 static TR_CFG_RC tr_cfg_parse_idp_realms (TR_CFG *trc, json_t *jcfg)
624 json_t *jidps = NULL;
625 TR_CFG_RC rc = TR_CFG_SUCCESS;
626 TR_IDP_REALM *idp = NULL;
629 if ((!trc) || (!jcfg))
630 return TR_CFG_BAD_PARAMS;
632 /* If there are any IDP Realms, parse them */
633 if ((NULL != (jidps = json_object_get(jcfg, "idp_realms"))) &&
634 (json_is_array(jidps))) {
635 for (i = 0; i < json_array_size(jidps); i++) {
636 if (NULL == (idp = tr_cfg_parse_one_idp_realm(trc,
637 json_array_get(jidps, i),
641 tr_debug("tr_cfg_parse_idp_realms: IDP realm configured: %s.", idp->realm_id->buf);
642 idp->next = trc->idp_realms;
643 trc->idp_realms = idp;
650 static TR_IDP_REALM *tr_cfg_parse_comm_idps (TR_CFG *trc, json_t *jidps, TR_CFG_RC *rc)
652 TR_IDP_REALM *idp = NULL;
653 TR_IDP_REALM *temp_idp = NULL;
658 (!json_is_array(jidps))) {
660 *rc = TR_CFG_BAD_PARAMS;
664 for (i = 0; i < json_array_size(jidps); i++) {
665 if (NULL == (temp_idp = (tr_cfg_find_idp(trc,
666 tr_new_name((char *)json_string_value(json_array_get(jidps, i))),
668 tr_debug("tr_cfg_parse_comm_idps: Unknown IDP %s.",
669 (char *)json_string_value(json_array_get(jidps, i)));
673 temp_idp->comm_next = idp;
680 static TR_RP_REALM *tr_cfg_parse_comm_rps (TR_CFG *trc, json_t *jrps, TR_CFG_RC *rc)
682 TR_RP_REALM *rp = NULL;
683 TR_RP_REALM *temp_rp = NULL;
688 (!json_is_array(jrps))) {
690 *rc = TR_CFG_BAD_PARAMS;
694 for (i = (json_array_size(jrps)-1); i >= 0; i--) {
695 if (NULL == (temp_rp = talloc(trc, TR_RP_REALM))) {
696 tr_debug("tr_cfg_parse_comm_rps: Can't allocate memory for RP Realm.");
701 memset (temp_rp, 0, sizeof(TR_RP_REALM));
703 if (NULL == (temp_rp->realm_name = tr_new_name((char *)json_string_value(json_array_get(jrps, i))))) {
704 tr_debug("tr_cfg_parse_comm_rps: No memory for RP Realm Name.");
717 static TR_COMM *tr_cfg_parse_one_comm (TR_CFG *trc, json_t *jcomm, TR_CFG_RC *rc) {
718 TR_COMM *comm = NULL;
720 json_t *jtype = NULL;
721 json_t *japcs = NULL;
722 json_t *jidps = NULL;
725 if ((!trc) || (!jcomm) || (!rc)) {
726 tr_debug("tr_cfg_parse_one_comm: Bad parameters.");
728 *rc = TR_CFG_BAD_PARAMS;
732 if (NULL == (comm = talloc_zero(trc, TR_COMM))) {
733 tr_crit("tr_cfg_parse_one_comm: Out of memory.");
739 if ((NULL == (jid = json_object_get(jcomm, "community_id"))) ||
740 (!json_is_string(jid)) ||
741 (NULL == (jtype = json_object_get(jcomm, "type"))) ||
742 (!json_is_string(jtype)) ||
743 (NULL == (japcs = json_object_get(jcomm, "apcs"))) ||
744 (!json_is_array(japcs)) ||
745 (NULL == (jidps = json_object_get(jcomm, "idp_realms"))) ||
746 (!json_is_array(jidps)) ||
747 (NULL == (jrps = json_object_get(jcomm, "rp_realms"))) ||
748 (!json_is_array(jrps))) {
749 tr_debug("tr_cfg_parse_one_comm: Error parsing Communities configuration.");
750 *rc = TR_CFG_NOPARSE;
754 if (NULL == (comm->id = tr_new_name((char *)json_string_value(jid)))) {
755 tr_debug("tr_cfg_parse_one_comm: No memory for community id.");
760 if (0 == strcmp(json_string_value(jtype), "apc")) {
761 comm->type = TR_COMM_APC;
762 } else if (0 == strcmp(json_string_value(jtype), "coi")) {
763 comm->type = TR_COMM_COI;
764 if (NULL == (comm->apcs = tr_cfg_parse_apcs(trc, japcs, rc))) {
765 tr_debug("tr_cfg_parse_one_comm: Can't parse APCs for COI %s.", comm->id->buf);
766 tr_free_name(comm->id);
770 tr_debug("tr_cfg_parse_one_comm: Invalid community type, comm = %s, type = %s", comm->id->buf, json_string_value(jtype));
771 tr_free_name(comm->id);
772 *rc = TR_CFG_NOPARSE;
776 comm->idp_realms = tr_cfg_parse_comm_idps(trc, jidps, rc);
777 if (TR_CFG_SUCCESS != *rc) {
778 tr_debug("tr_cfg_parse_one_comm: Can't parse IDP realms for comm %s.", comm->id->buf);
779 tr_free_name(comm->id);
783 comm->rp_realms = tr_cfg_parse_comm_rps(trc, jrps, rc);
784 if (TR_CFG_SUCCESS != *rc) {
785 tr_debug("tr_cfg_parse_comm: Can't parse RP realms for comm %s .", comm->id->buf);
786 tr_free_name(comm->id);
790 if (TR_COMM_APC == comm->type) {
791 json_t *jexpire = json_object_get(jcomm, "expiration_interval");
792 comm->expiration_interval = 43200; /*30 days*/
794 if (!json_is_integer(jexpire)) {
795 fprintf(stderr, "tr_parse_comm: expirae_interval is not an integer\n");
798 comm->expiration_interval = json_integer_value(jexpire);
799 if (comm->expiration_interval <= 10)
800 comm->expiration_interval = 11; /* Freeradius waits 10 minutes between successful TR queries*/
801 if (comm->expiration_interval > 129600) /* 90 days*/
802 comm->expiration_interval = 129600;
809 static TR_CFG_RC tr_cfg_parse_comms (TR_CFG *trc, json_t *jcfg)
811 json_t *jcomms = NULL;
812 TR_CFG_RC rc = TR_CFG_SUCCESS;
813 TR_COMM *comm = NULL;
816 if ((!trc) || (!jcfg)) {
817 tr_debug("tr_cfg_parse_comms: Bad Parameters.");
818 return TR_CFG_BAD_PARAMS;
821 if (NULL != (jcomms = json_object_get(jcfg, "communities"))) {
822 if (!json_is_array(jcomms)) {
823 return TR_CFG_NOPARSE;
826 for (i = 0; i < json_array_size(jcomms); i++) {
827 if (NULL == (comm = tr_cfg_parse_one_comm(trc,
828 json_array_get(jcomms, i),
832 tr_debug("tr_cfg_parse_comms: Community configured: %s.", comm->id->buf);
833 comm->next = trc->comms;
840 TR_CFG_RC tr_cfg_validate (TR_CFG *trc) {
841 TR_CFG_RC rc = TR_CFG_SUCCESS;
844 return TR_CFG_BAD_PARAMS;
846 if ((NULL == trc->internal)||
847 (NULL == trc->internal->hostname)) {
848 tr_debug("tr_cfg_validate: Error: No internal configuration, or no hostname.");
852 if (NULL == trc->rp_clients) {
853 tr_debug("tr_cfg_validate: Error: No RP Clients configured");
857 if (NULL == trc->comms) {
858 tr_debug("tr_cfg_validate: Error: No Communities configured");
862 if ((NULL == trc->default_servers) && (NULL == trc->idp_realms)) {
863 tr_debug("tr_cfg_validate: Error: No default servers or IDPs configured.");
870 /* Join two paths and return a pointer to the result. This should be freed
871 * via talloc_free. Returns NULL on failure. */
872 static char *join_paths(const char *p1, const char *p2) {
873 return talloc_asprintf(NULL, "%s/%s", p1, p2); /* returns NULL on a failure */
876 /* Reads configuration files in config_dir ("" or "./" will use the current directory) */
877 TR_CFG_RC tr_parse_config (TR_INSTANCE *tr, const char *config_dir, int n, struct dirent **cfg_files) {
880 char *file_with_path;
882 if ((!tr) || (!cfg_files) || (n<=0))
883 return TR_CFG_BAD_PARAMS;
885 /* If there is a partial/abandoned config lying around, free it */
887 tr_cfg_free(tr->new_cfg);
889 if (NULL == (tr->new_cfg = talloc(NULL, TR_CFG)))
892 memset(tr->new_cfg, 0, sizeof(TR_CFG));
894 /* Parse configuration information from each config file */
896 file_with_path=join_paths(config_dir, cfg_files[n]->d_name); /* must free result with talloc_free */
897 if(file_with_path == NULL) {
898 tr_crit("tr_parse_config: error joining path.");
901 tr_debug("tr_parse_config: Parsing %s.", cfg_files[n]->d_name); /* print the filename without the path */
902 if (NULL == (jcfg = json_load_file(file_with_path,
903 JSON_DISABLE_EOF_CHECK, &rc))) {
904 tr_debug("tr_parse_config: Error parsing config file %s.",
905 cfg_files[n]->d_name);
906 talloc_free(file_with_path);
907 return TR_CFG_NOPARSE;
909 talloc_free(file_with_path); /* done with filename */
911 if ((TR_CFG_SUCCESS != tr_cfg_parse_internal(tr->new_cfg, jcfg)) ||
912 (TR_CFG_SUCCESS != tr_cfg_parse_rp_clients(tr->new_cfg, jcfg)) ||
913 (TR_CFG_SUCCESS != tr_cfg_parse_idp_realms(tr->new_cfg, jcfg)) ||
914 (TR_CFG_SUCCESS != tr_cfg_parse_default_servers(tr->new_cfg, jcfg)) ||
915 (TR_CFG_SUCCESS != tr_cfg_parse_comms(tr->new_cfg, jcfg))) {
916 tr_cfg_free(tr->new_cfg);
921 /* make sure we got a complete, consistent configuration */
922 if (TR_CFG_SUCCESS != tr_cfg_validate(tr->new_cfg)) {
923 tr_err("tr_parse_config: Error: INVALID CONFIGURATION");
927 return TR_CFG_SUCCESS;
930 TR_IDP_REALM *tr_cfg_find_idp (TR_CFG *tr_cfg, TR_NAME *idp_id, TR_CFG_RC *rc)
933 TR_IDP_REALM *cfg_idp;
935 if ((!tr_cfg) || (!idp_id)) {
937 *rc = TR_CFG_BAD_PARAMS;
941 for (cfg_idp = tr_cfg->idp_realms; NULL != cfg_idp; cfg_idp = cfg_idp->next) {
942 if (!tr_name_cmp (idp_id, cfg_idp->realm_id)) {
943 tr_debug("tr_cfg_find_idp: Found %s.", idp_id->buf);
947 /* if we didn't find one, return NULL */
951 TR_RP_CLIENT *tr_cfg_find_rp (TR_CFG *tr_cfg, TR_NAME *rp_gss, TR_CFG_RC *rc)
953 TR_RP_CLIENT *cfg_rp;
956 if ((!tr_cfg) || (!rp_gss)) {
958 *rc = TR_CFG_BAD_PARAMS;
962 for (cfg_rp = tr_cfg->rp_clients; NULL != cfg_rp; cfg_rp = cfg_rp->next) {
963 for (i = 0; i < TR_MAX_GSS_NAMES; i++) {
964 if (!tr_name_cmp (rp_gss, cfg_rp->gss_names[i])) {
965 tr_debug("tr_cfg_find_rp: Found %s.", rp_gss->buf);
970 /* if we didn't find one, return NULL */
974 static int is_cfg_file(const struct dirent *dent) {
977 /* Only accept filenames ending in ".cfg" and starting with a character
978 * other than an ASCII '.' */
980 /* filename must be at least 4 characters long to be acceptable */
981 n=strlen(dent->d_name);
986 /* filename must not start with '.' */
987 if ('.' == dent->d_name[0]) {
991 /* If the above passed and the last four characters of the filename are .cfg, accept.
992 * (n.b., assumes an earlier test checked that the name is >= 4 chars long.) */
993 if (0 == strcmp(&(dent->d_name[n-4]), ".cfg")) {
997 /* otherwise, return false. */
1001 /* Find configuration files in a particular directory. Returns the
1002 * number of entries found, 0 if none are found, or <0 for some
1003 * errors. If n>=0, the cfg_files parameter will contain a newly
1004 * allocated array of pointers to struct dirent entries, as returned
1005 * by scandir(). These can be freed with tr_free_config_file_list().
1007 int tr_find_config_files (const char *config_dir, struct dirent ***cfg_files) {
1010 n = scandir(config_dir, cfg_files, &is_cfg_file, 0);
1014 tr_debug("tr_find_config: scandir error trying to scan %s.", config_dir);
1015 } else if (n == 0) {
1016 tr_debug("tr_find_config: No config files found.");
1020 tr_debug("tr_find_config: Config file found (%s).", (*cfg_files)[i]->d_name);
1027 /* Free memory allocated for configuration file list returned from tr_find_config_files().
1028 * This can be called regardless of the return value of tr_find_config_values(). */
1029 void tr_free_config_file_list(int n, struct dirent ***cfg_files) {
1032 /* if n < 0, then scandir did not allocate anything because it failed */
1033 if((n>=0) && (*cfg_files != NULL)) {
1034 for(ii=0; ii<n; ii++) {
1035 free((*cfg_files)[ii]);
1037 free(*cfg_files); /* safe even if n==0 */
1038 *cfg_files=NULL; /* this will help prevent accidentally freeing twice */