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_cfgwatch.h>
43 #include <tr_config.h>
46 #include <tr_filter.h>
47 #include <trust_router/tr_constraint.h>
50 #include <trust_router/trp.h>
52 void tr_print_config (TR_CFG *cfg) {
53 tr_notice("tr_print_config: Logging running trust router configuration.");
54 tr_print_comms(cfg->comms);
57 void tr_print_comms (TR_COMM *comm_list) {
60 for (comm = comm_list; NULL != comm; comm = comm->next) {
61 tr_notice("tr_print_config: Community %s:", comm->id->buf);
63 tr_notice("tr_print_config: - Member IdPs:");
64 tr_print_comm_idps(comm->idp_realms);
66 tr_notice("tr_print_config: - Member RPs:");
67 tr_print_comm_rps(comm->rp_realms);
71 void tr_print_comm_idps (TR_IDP_REALM *idp_list) {
72 TR_IDP_REALM *idp = NULL;
75 for (idp = idp_list; NULL != idp; idp = idp->comm_next) {
76 s=tr_idp_realm_to_str(NULL, idp);
78 tr_notice("tr_print_config: - @%s", s);
80 tr_notice("tr_print_config: unable to allocate idp output string.");
84 void tr_print_comm_rps(TR_RP_REALM *rp_list) {
85 TR_RP_REALM *rp = NULL;
87 for (rp = rp_list; NULL != rp; rp = rp->next) {
88 tr_notice("tr_print_config: - %s", rp->realm_name->buf);
92 TR_CFG *tr_cfg_new(TALLOC_CTX *mem_ctx)
94 return talloc_zero(mem_ctx, TR_CFG);
97 void tr_cfg_free (TR_CFG *cfg) {
101 TR_CFG_MGR *tr_cfg_mgr_new(TALLOC_CTX *mem_ctx)
103 return talloc_zero(mem_ctx, TR_CFG_MGR);
106 void tr_cfg_mgr_free (TR_CFG_MGR *cfg_mgr) {
107 talloc_free(cfg_mgr);
110 TR_CFG_RC tr_apply_new_config (TR_CFG_MGR *cfg_mgr)
112 /* cfg_mgr->active is allowed to be null, but new cannot be */
113 if ((cfg_mgr==NULL) || (cfg_mgr->new==NULL))
114 return TR_CFG_BAD_PARAMS;
116 if (cfg_mgr->active != NULL)
117 tr_cfg_free(cfg_mgr->active);
119 cfg_mgr->active = cfg_mgr->new;
120 cfg_mgr->new=NULL; /* only keep a single handle on the new configuration */
122 tr_log_threshold(cfg_mgr->active->internal->log_threshold);
123 tr_console_threshold(cfg_mgr->active->internal->console_threshold);
125 return TR_CFG_SUCCESS;
128 static TR_CFG_RC tr_cfg_parse_internal(TR_CFG *trc, json_t *jcfg)
132 json_t *jtidsp = NULL;
133 json_t *jtrpsp = NULL;
134 json_t *jhname = NULL;
136 json_t *jconthres = NULL;
137 json_t *jlogthres = NULL;
138 json_t *jcfgpoll = NULL;
139 json_t *jcfgsettle = NULL;
140 json_t *jroutesweep = NULL;
141 json_t *jrouteupdate = NULL;
142 json_t *jrouteconnect = NULL;
144 if ((!trc) || (!jcfg))
145 return TR_CFG_BAD_PARAMS;
147 if (NULL == trc->internal) {
148 if (NULL == (trc->internal = talloc_zero(trc, TR_CFG_INTERNAL)))
152 if (NULL != (jint = json_object_get(jcfg, "tr_internal"))) {
153 if (NULL != (jmtd = json_object_get(jint, "max_tree_depth"))) {
154 if (json_is_number(jmtd)) {
155 trc->internal->max_tree_depth = json_integer_value(jmtd);
157 tr_debug("tr_cfg_parse_internal: Parsing error, max_tree_depth is not a number.");
158 return TR_CFG_NOPARSE;
161 /* If not configured, use the default */
162 trc->internal->max_tree_depth = TR_DEFAULT_MAX_TREE_DEPTH;
164 if (NULL != (jtidsp = json_object_get(jint, "tids_port"))) {
165 if (json_is_number(jtidsp)) {
166 trc->internal->tids_port = json_integer_value(jtidsp);
168 tr_debug("tr_cfg_parse_internal: Parsing error, tids_port is not a number.");
169 return TR_CFG_NOPARSE;
172 /* If not configured, use the default */
173 trc->internal->tids_port = TR_DEFAULT_TIDS_PORT;
175 if (NULL != (jtrpsp = json_object_get(jint, "trps_port"))) {
176 if (json_is_number(jtrpsp)) {
177 trc->internal->trps_port = json_integer_value(jtrpsp);
179 tr_debug("tr_cfg_parse_internal: Parsing error, trps_port is not a number.");
180 return TR_CFG_NOPARSE;
183 /* If not configured, use the default */
184 trc->internal->trps_port = TR_DEFAULT_TRPS_PORT;
186 if (NULL != (jhname = json_object_get(jint, "hostname"))) {
187 if (json_is_string(jhname)) {
188 trc->internal->hostname = json_string_value(jhname);
190 tr_debug("tr_cfg_parse_internal: Parsing error, hostname is not a string.");
191 return TR_CFG_NOPARSE;
194 if (NULL != (jcfgpoll = json_object_get(jint, "cfg_poll_interval"))) {
195 if (json_is_number(jcfgpoll)) {
196 trc->internal->cfg_poll_interval = json_integer_value(jcfgpoll);
198 tr_debug("tr_cfg_parse_internal: Parsing error, cfg_poll_interval is not a number.");
199 return TR_CFG_NOPARSE;
202 trc->internal->cfg_poll_interval = TR_CFGWATCH_DEFAULT_POLL;
205 if (NULL != (jcfgsettle = json_object_get(jint, "cfg_settling_time"))) {
206 if (json_is_number(jcfgsettle)) {
207 trc->internal->cfg_settling_time = json_integer_value(jcfgsettle);
209 tr_debug("tr_cfg_parse_internal: Parsing error, cfg_settling_time is not a number.");
210 return TR_CFG_NOPARSE;
213 trc->internal->cfg_settling_time = TR_CFGWATCH_DEFAULT_SETTLE;
216 if (NULL != (jrouteconnect = json_object_get(jint, "trp_connect_interval"))) {
217 if (json_is_number(jrouteconnect)) {
218 trc->internal->trp_connect_interval = json_integer_value(jrouteconnect);
220 tr_debug("tr_cfg_parse_internal: Parsing error, trp_connect_interval is not a number.");
221 return TR_CFG_NOPARSE;
224 /* if not configured, use the default */
225 trc->internal->trp_connect_interval=TR_DEFAULT_TRP_CONNECT_INTERVAL;
228 if (NULL != (jroutesweep = json_object_get(jint, "trp_sweep_interval"))) {
229 if (json_is_number(jroutesweep)) {
230 trc->internal->trp_sweep_interval = json_integer_value(jroutesweep);
232 tr_debug("tr_cfg_parse_internal: Parsing error, trp_sweep_interval is not a number.");
233 return TR_CFG_NOPARSE;
236 /* if not configured, use the default */
237 trc->internal->trp_sweep_interval=TR_DEFAULT_TRP_SWEEP_INTERVAL;
240 if (NULL != (jrouteupdate = json_object_get(jint, "trp_update_interval"))) {
241 if (json_is_number(jrouteupdate)) {
242 trc->internal->trp_update_interval = json_integer_value(jrouteupdate);
244 tr_debug("tr_cfg_parse_internal: Parsing error, trp_update_interval is not a number.");
245 return TR_CFG_NOPARSE;
248 /* if not configured, use the default */
249 trc->internal->trp_update_interval=TR_DEFAULT_TRP_UPDATE_INTERVAL;
252 if (NULL != (jlog = json_object_get(jint, "logging"))) {
253 if (NULL != (jlogthres = json_object_get(jlog, "log_threshold"))) {
254 if (json_is_string(jlogthres)) {
255 trc->internal->log_threshold = str2sev(json_string_value(jlogthres));
257 tr_debug("tr_cfg_parse_internal: Parsing error, log_threshold is not a string.");
258 return TR_CFG_NOPARSE;
261 /* If not configured, use the default */
262 trc->internal->log_threshold = TR_DEFAULT_LOG_THRESHOLD;
265 if (NULL != (jconthres = json_object_get(jlog, "console_threshold"))) {
266 if (json_is_string(jconthres)) {
267 trc->internal->console_threshold = str2sev(json_string_value(jconthres));
269 tr_debug("tr_cfg_parse_internal: Parsing error, console_threshold is not a string.");
270 return TR_CFG_NOPARSE;
273 /* If not configured, use the default */
274 trc->internal->console_threshold = TR_DEFAULT_CONSOLE_THRESHOLD;
277 /* If not configured, use the default */
278 trc->internal->console_threshold = TR_DEFAULT_CONSOLE_THRESHOLD;
279 trc->internal->log_threshold = TR_DEFAULT_LOG_THRESHOLD;
282 tr_debug("tr_cfg_parse_internal: Internal config parsed.");
283 return TR_CFG_SUCCESS;
285 return TR_CFG_SUCCESS;
288 static TR_CONSTRAINT *tr_cfg_parse_one_constraint(TALLOC_CTX *mem_ctx, char *ctype, json_t *jc, TR_CFG_RC *rc)
290 TR_CONSTRAINT *cons=NULL;
293 if ((!ctype) || (!jc) || (!rc) ||
294 (!json_is_array(jc)) ||
295 (0 >= json_array_size(jc)) ||
296 (TR_MAX_CONST_MATCHES < json_array_size(jc)) ||
297 (!json_is_string(json_array_get(jc, 0)))) {
298 tr_err("tr_cfg_parse_one_constraint: config error.");
303 if (NULL==(cons=tr_constraint_new(mem_ctx))) {
304 tr_debug("tr_cfg_parse_one_constraint: Out of memory (cons).");
309 if (NULL==(cons->type=tr_new_name(ctype))) {
310 tr_err("tr_cfg_parse_one_constraint: Out of memory (type).");
312 tr_constraint_free(cons);
316 for (i=0; i < json_array_size(jc); i++) {
317 cons->matches[i]=tr_new_name(json_string_value(json_array_get(jc, i)));
318 if (cons->matches[i]==NULL) {
319 tr_err("tr_cfg_parse_one_constraint: Out of memory (match %d).", i+1);
321 tr_constraint_free(cons);
329 static TR_FILTER *tr_cfg_parse_one_filter(TALLOC_CTX *mem_ctx, json_t *jfilt, TR_FILTER_TYPE ftype, TR_CFG_RC *rc)
331 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
332 TR_FILTER *filt=NULL;
333 json_t *jfaction=NULL;
334 json_t *jfspecs=NULL;
335 json_t *jffield=NULL;
336 json_t *jfmatch=NULL;
344 if ((jfilt==NULL) || (rc==NULL)) {
345 tr_err("tr_cfg_parse_one_filter: null argument");
346 *rc=TR_CFG_BAD_PARAMS;
350 if (NULL==(filt=tr_filter_new(tmp_ctx))) {
351 tr_err("tr_cfg_parse_one_filter: Out of memory.");
355 tr_filter_set_type(filt, ftype);
357 /* make sure we have space to represent the filter */
358 if (json_array_size(jfilt) > TR_MAX_FILTER_LINES) {
359 tr_err("tr_cfg_parse_one_filter: Filter has too many lines, maximum of %d.", TR_MAX_FILTER_LINES);
364 /* For each entry in the filter... */
365 for (i=0; i < json_array_size(jfilt); i++) {
366 if ((NULL==(jfaction=json_object_get(json_array_get(jfilt, i), "action"))) ||
367 (!json_is_string(jfaction))) {
368 tr_debug("tr_cfg_parse_one_filter: Error parsing filter action.");
373 if ((NULL==(jfspecs=json_object_get(json_array_get(jfilt, i), "specs"))) ||
374 (!json_is_array(jfspecs)) ||
375 (0==json_array_size(jfspecs))) {
376 tr_debug("tr_cfg_parse_one_filter: Error parsing filter specs.");
381 if (TR_MAX_FILTER_SPECS < json_array_size(jfspecs)) {
382 tr_debug("tr_cfg_parse_one_filter: Filter has too many specs, maximimum of %d.", TR_MAX_FILTER_SPECS);
387 if (NULL==(filt->lines[i]=tr_fline_new(filt))) {
388 tr_debug("tr_cfg_parse_one_filter: Out of memory allocating filter line %d.", i+1);
393 if (!strcmp(json_string_value(jfaction), "accept")) {
394 filt->lines[i]->action=TR_FILTER_ACTION_ACCEPT;
396 else if (!strcmp(json_string_value(jfaction), "reject")) {
397 filt->lines[i]->action=TR_FILTER_ACTION_REJECT;
400 tr_debug("tr_cfg_parse_one_filter: Error parsing filter action, unknown action' %s'.", json_string_value(jfaction));
405 if (NULL!=(jrc=json_object_get(json_array_get(jfilt, i), "realm_constraints"))) {
406 if (!json_is_array(jrc)) {
407 tr_err("tr_cfg_parse_one_filter: cannot parse realm_constraints, not an array.");
410 } else if (json_array_size(jrc)>TR_MAX_CONST_MATCHES) {
411 tr_err("tr_cfg_parse_one_filter: realm_constraints has too many entries, maximum of %d.",
412 TR_MAX_CONST_MATCHES);
415 } else if (json_array_size(jrc)>0) {
416 /* ok we actually have entries to process */
417 if (NULL==(filt->lines[i]->realm_cons=tr_cfg_parse_one_constraint(filt->lines[i], "realm", jrc, rc))) {
418 tr_debug("tr_cfg_parse_one_filter: Error parsing realm constraint");
425 if (NULL!=(jdc=json_object_get(json_array_get(jfilt, i), "domain_constraints"))) {
426 if (!json_is_array(jdc)) {
427 tr_err("tr_cfg_parse_one_filter: cannot parse domain_constraints, not an array.");
430 } else if (json_array_size(jdc)>TR_MAX_CONST_MATCHES) {
431 tr_err("tr_cfg_parse_one_filter: domain_constraints has too many entries, maximum of %d.",
432 TR_MAX_CONST_MATCHES);
435 } else if (json_array_size(jdc)>0) {
436 if (NULL==(filt->lines[i]->domain_cons=tr_cfg_parse_one_constraint(filt->lines[i], "domain", jdc, rc))) {
437 tr_debug("tr_cfg_parse_one_filter: Error parsing domain constraint");
444 /*For each filter spec within the filter line... */
445 for (j=0; j <json_array_size(jfspecs); j++) {
446 if ((NULL==(jffield=json_object_get(json_array_get(jfspecs, j), "field"))) ||
447 (!json_is_string(jffield))) {
448 tr_debug("tr_cfg_parse_one_filter: Error parsing filter: missing field for filer spec %d, filter line %d.", i, j);
453 /* check that we have a match attribute */
454 if (NULL==(jfmatch=json_object_get(json_array_get(jfspecs, j), "match"))) {
455 tr_debug("tr_cfg_parse_one_filter: Error parsing filter: missing match for filer spec %d, filter line %d.", i, j);
460 /* check that match is a string */
461 if (!json_is_string(jfmatch)) {
462 tr_debug("tr_cfg_parse_one_filter: Error parsing filter: match not a string for filter spec %d, filter line %d.", i, j);
467 /* allocate the filter spec */
468 if (NULL==(filt->lines[i]->specs[j]=tr_fspec_new(filt->lines[i]))) {
469 tr_debug("tr_cfg_parse_one_filter: Out of memory.");
474 /* fill in the field */
475 if (NULL==(filt->lines[i]->specs[j]->field=tr_new_name(json_string_value(jffield)))) {
476 tr_debug("tr_cfg_parse_one_filter: Out of memory.");
481 /* fill in the matches */
482 if (NULL==(name=tr_new_name(json_string_value(jfmatch)))) {
483 tr_debug("tr_cfg_parse_one_filter: Out of memory.");
487 tr_fspec_set_match(filt->lines[i]->specs[j], name);
491 talloc_steal(mem_ctx, filt);
494 talloc_free(tmp_ctx);
495 if (*rc!=TR_CFG_SUCCESS)
500 static TR_FILTER *tr_cfg_parse_filters(TALLOC_CTX *mem_ctx, json_t *jfilts, TR_CFG_RC *rc)
502 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
504 TR_FILTER *filt=NULL;
514 jfilt=json_object_get(jfilts, "tid_inbound");
516 filt=tr_cfg_parse_one_filter(tmp_ctx, jfilt, TR_FILTER_TYPE_TID_INCOMING, rc);
517 if (*rc!=TR_CFG_SUCCESS) {
518 tr_debug("tr_cfg_parse_filters: Error parsing tid_inbound filter.");
523 tr_debug("tr_cfg_parse_filters: Unknown filter types in filter block.");
531 if (*rc==TR_CFG_SUCCESS)
532 talloc_steal(mem_ctx, filt);
533 else if (filt!=NULL) {
538 talloc_free(tmp_ctx);
542 static TR_AAA_SERVER *tr_cfg_parse_one_aaa_server(TALLOC_CTX *mem_ctx, json_t *jaddr, TR_CFG_RC *rc)
544 TR_AAA_SERVER *aaa = NULL;
547 if ((!jaddr) || (!json_is_string(jaddr))) {
548 tr_debug("tr_cfg_parse_one_aaa_server: Bad parameters.");
549 *rc = TR_CFG_BAD_PARAMS;
553 name=tr_new_name(json_string_value(jaddr));
555 tr_debug("tr_cfg_parse_one_aaa_server: Out of memory allocating hostname.");
560 aaa=tr_aaa_server_new(mem_ctx, name);
563 tr_debug("tr_cfg_parse_one_aaa_server: Out of memory allocating AAA server.");
571 static TR_AAA_SERVER *tr_cfg_parse_aaa_servers(TALLOC_CTX *mem_ctx, json_t *jaaas, TR_CFG_RC *rc)
573 TALLOC_CTX *tmp_ctx=NULL;
574 TR_AAA_SERVER *aaa = NULL;
575 TR_AAA_SERVER *temp_aaa = NULL;
578 for (i = 0; i < json_array_size(jaaas); i++) {
579 /* rc gets set in here */
580 if (NULL == (temp_aaa = tr_cfg_parse_one_aaa_server(tmp_ctx, json_array_get(jaaas, i), rc))) {
581 talloc_free(tmp_ctx);
584 /* TBD -- IPv6 addresses */
585 // tr_debug("tr_cfg_parse_aaa_servers: Configuring AAA Server: ip_addr = %s.", inet_ntoa(temp_aaa->aaa_server_addr));
586 temp_aaa->next = aaa;
589 tr_debug("tr_cfg_parse_aaa_servers: Finished (rc=%d)", *rc);
591 for (temp_aaa=aaa; temp_aaa!=NULL; temp_aaa=temp_aaa->next)
592 talloc_steal(mem_ctx, temp_aaa);
593 talloc_free(tmp_ctx);
597 static TR_APC *tr_cfg_parse_one_apc(TALLOC_CTX *mem_ctx, json_t *japc, TR_CFG_RC *rc)
602 *rc = TR_CFG_SUCCESS; /* presume success */
604 if ((!japc) || (!rc) || (!json_is_string(japc))) {
605 tr_debug("tr_cfg_parse_one_apc: Bad parameters.");
607 *rc = TR_CFG_BAD_PARAMS;
611 apc=tr_apc_new(mem_ctx);
613 tr_debug("tr_cfg_parse_one_apc: Out of memory.");
618 name=tr_new_name(json_string_value(japc));
620 tr_debug("tr_cfg_parse_one_apc: No memory for APC name.");
625 tr_apc_set_id(apc, name); /* apc is now responsible for freeing the name */
630 static TR_APC *tr_cfg_parse_apcs(TALLOC_CTX *mem_ctx, json_t *japcs, TR_CFG_RC *rc)
632 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
634 TR_APC *new_apc=NULL;
636 TR_CFG_RC call_rc=TR_CFG_ERROR;
638 *rc = TR_CFG_SUCCESS; /* presume success */
640 if ((!japcs) || (!rc) || (!json_is_array(japcs))) {
641 tr_debug("tr_cfg_parse_one_apc: Bad parameters.");
643 *rc = TR_CFG_BAD_PARAMS;
647 for (ii=0; ii<json_array_size(japcs); ii++) {
648 new_apc=tr_cfg_parse_one_apc(tmp_ctx, json_array_get(japcs, ii), &call_rc);
649 if ((call_rc!=TR_CFG_SUCCESS) || (new_apc==NULL)) {
650 tr_debug("tr_cfg_parse_apcs: Error parsing APC %d.", ii+1);
654 tr_apc_add(apcs, new_apc);
657 talloc_steal(mem_ctx, apcs);
661 talloc_free(tmp_ctx);
665 static TR_NAME *tr_cfg_parse_name(TALLOC_CTX *mem_ctx, json_t *jname, TR_CFG_RC *rc)
670 if ((jname==NULL) || (!json_is_string(jname))) {
671 tr_err("tr_cfg_parse_name: name missing or not a string");
672 *rc=TR_CFG_BAD_PARAMS;
675 name=tr_new_name(json_string_value(jname));
677 tr_err("tr_cfg_parse_name: could not allocate name");
686 static int tr_cfg_parse_shared_config(json_t *jsc, TR_CFG_RC *rc)
688 const char *shared=NULL;
692 (!json_is_string(jsc)) ||
693 (NULL==(shared=json_string_value(jsc)))) {
694 *rc=TR_CFG_BAD_PARAMS;
698 if (0==strcmp(shared, "no"))
700 else if (0==strcmp(shared, "yes"))
703 /* any other value is an error */
704 tr_debug("tr_cfg_parse_shared_config: invalid shared_config value \"%s\" (should be \"yes\" or \"no\")",
710 static TR_REALM_ORIGIN tr_cfg_realm_origin(json_t *jrealm)
712 json_t *jremote=json_object_get(jrealm, "remote");
716 return TR_REALM_LOCAL;
717 if (!json_is_string(jremote)) {
718 tr_warning("tr_cfg_realm_origin: \"remote\" is not a string, assuming this is a local realm.");
719 return TR_REALM_LOCAL;
721 s=json_string_value(jremote);
722 if (strcasecmp(s, "yes")==0)
723 return TR_REALM_REMOTE_INCOMPLETE;
724 else if (strcasecmp(s, "no")!=0)
725 tr_warning("tr_cfg_realm_origin: \"remote\" is neither 'yes' nor 'no', assuming this is a local realm.");
727 return TR_REALM_LOCAL;
730 /* Parse the identity provider object from a realm and fill in the given TR_IDP_REALM. */
731 static TR_CFG_RC tr_cfg_parse_idp(TR_IDP_REALM *idp, json_t *jidp)
733 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
735 TR_AAA_SERVER *aaa=NULL;
736 TR_CFG_RC rc=TR_CFG_ERROR;
741 idp->shared_config=tr_cfg_parse_shared_config(json_object_get(jidp, "shared_config"), &rc);
742 if (rc!=TR_CFG_SUCCESS) {
743 tr_err("tr_cfg_parse_idp: missing or malformed shared_config specification");
748 apcs=tr_cfg_parse_apcs(tmp_ctx, json_object_get(jidp, "apcs"), &rc);
749 if ((rc!=TR_CFG_SUCCESS) || (apcs==NULL)) {
750 tr_err("tr_cfg_parse_idp: unable to parse APC");
755 aaa=tr_cfg_parse_aaa_servers(idp, json_object_get(jidp, "aaa_servers"), &rc);
756 if (rc!=TR_CFG_SUCCESS) {
757 tr_err("tr_cfg_parse_idp: unable to parse AAA servers");
762 tr_debug("tr_cfg_parse_idp: APC=\"%.*s\"",
766 /* done, fill in the idp structures */
768 talloc_steal(idp, apcs);
769 idp->aaa_servers=aaa;
773 if (rc!=TR_CFG_SUCCESS) {
777 tr_aaa_server_free(aaa);
780 talloc_free(tmp_ctx);
784 /* parses idp realm */
785 static TR_IDP_REALM *tr_cfg_parse_one_idp_realm(TALLOC_CTX *mem_ctx, json_t *jrealm, TR_CFG_RC *rc)
787 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
788 TR_IDP_REALM *realm=NULL;
789 TR_CFG_RC call_rc=TR_CFG_ERROR;
791 *rc=TR_CFG_ERROR; /* default to error if not set */
793 if ((!jrealm) || (!rc)) {
794 tr_err("tr_cfg_parse_one_idp_realm: Bad parameters.");
796 *rc=TR_CFG_BAD_PARAMS;
800 if (NULL==(realm=tr_idp_realm_new(tmp_ctx))) {
801 tr_err("tr_cfg_parse_one_idp_realm: could not allocate idp realm.");
806 realm->origin=tr_cfg_realm_origin(jrealm);
807 if (realm->origin!=TR_REALM_LOCAL) {
808 tr_debug("tr_cfg_parse_one_idp_realm: realm is remote, should not have full IdP info.");
813 /* must have a name */
814 realm->realm_id=tr_cfg_parse_name(realm,
815 json_object_get(jrealm, "realm"),
817 if ((call_rc!=TR_CFG_SUCCESS) || (realm->realm_id==NULL)) {
818 tr_err("tr_cfg_parse_one_idp_realm: could not parse realm name");
822 tr_debug("tr_cfg_parse_one_idp_realm: realm_id=\"%.*s\"",
823 realm->realm_id->len,
824 realm->realm_id->buf);
826 call_rc=tr_cfg_parse_idp(realm, json_object_get(jrealm, "identity_provider"));
827 if (call_rc!=TR_CFG_SUCCESS) {
828 tr_err("tr_cfg_parse_one_idp_realm: could not parse identity_provider.");
836 if (*rc==TR_CFG_SUCCESS)
837 talloc_steal(mem_ctx, realm);
843 talloc_free(tmp_ctx);
847 /* Determine whether the realm is an IDP realm */
848 static int tr_cfg_is_idp_realm(json_t *jrealm)
850 /* If a realm spec contains an identity_provider, it's an IDP realm. */
851 if (NULL != json_object_get(jrealm, "identity_provider"))
857 static TR_IDP_REALM *tr_cfg_parse_one_remote_realm(TALLOC_CTX *mem_ctx, json_t *jrealm, TR_CFG_RC *rc)
859 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
860 TR_IDP_REALM *realm=talloc(mem_ctx, TR_IDP_REALM);
862 *rc=TR_CFG_ERROR; /* default to error if not set */
864 if ((!jrealm) || (!rc)) {
865 tr_err("tr_cfg_parse_one_remote_realm: Bad parameters.");
867 *rc=TR_CFG_BAD_PARAMS;
871 if (NULL==(realm=tr_idp_realm_new(tmp_ctx))) {
872 tr_err("tr_cfg_parse_one_remote_realm: could not allocate idp realm.");
877 /* must have a name */
878 realm->realm_id=tr_cfg_parse_name(realm,
879 json_object_get(jrealm, "realm"),
881 if ((*rc!=TR_CFG_SUCCESS) || (realm->realm_id==NULL)) {
882 tr_err("tr_cfg_parse_one_remote_realm: could not parse realm name");
886 tr_debug("tr_cfg_parse_one_remote_realm: realm_id=\"%.*s\"",
887 realm->realm_id->len,
888 realm->realm_id->buf);
890 realm->origin=tr_cfg_realm_origin(jrealm);
894 if (*rc==TR_CFG_SUCCESS)
895 talloc_steal(mem_ctx, realm);
901 talloc_free(tmp_ctx);
905 static int tr_cfg_is_remote_realm(json_t *jrealm)
907 return (tr_cfg_realm_origin(jrealm)!=TR_REALM_LOCAL);
910 /* Parse any idp realms in the j_realms object. Ignores other realm types. */
911 static TR_IDP_REALM *tr_cfg_parse_idp_realms(TALLOC_CTX *mem_ctx, json_t *jrealms, TR_CFG_RC *rc)
913 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
914 TR_IDP_REALM *realms=NULL;
915 TR_IDP_REALM *new_realm=NULL;
916 json_t *this_jrealm=NULL;
920 if ((jrealms==NULL) || (!json_is_array(jrealms))) {
921 tr_err("tr_cfg_parse_idp_realms: realms not an array");
922 *rc=TR_CFG_BAD_PARAMS;
926 for (ii=0; ii<json_array_size(jrealms); ii++) {
927 this_jrealm=json_array_get(jrealms, ii);
928 if (tr_cfg_is_idp_realm(this_jrealm)) {
929 new_realm=tr_cfg_parse_one_idp_realm(tmp_ctx, this_jrealm, rc);
930 if ((*rc)!=TR_CFG_SUCCESS) {
931 tr_err("tr_cfg_parse_idp_realms: error decoding realm entry %d", ii+1);
935 tr_idp_realm_add(realms, new_realm);
936 } else if (tr_cfg_is_remote_realm(this_jrealm)) {
937 new_realm=tr_cfg_parse_one_remote_realm(tmp_ctx, this_jrealm, rc);
938 if ((*rc)!=TR_CFG_SUCCESS) {
939 tr_err("tr_cfg_parse_idp_realms: error decoding remote realm entry %d", ii+1);
943 tr_idp_realm_add(realms, new_realm);
948 talloc_steal(mem_ctx, realms);
951 talloc_free(tmp_ctx);
956 static TR_IDP_REALM *tr_cfg_parse_remote_realms(TALLOC_CTX *mem_ctx, json_t *jrealms, TR_CFG_RC *rc)
958 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
959 TR_IDP_REALM *realms=NULL;
960 TR_IDP_REALM *new_realm=NULL;
961 json_t *this_jrealm=NULL;
965 if ((jrealms==NULL) || (!json_is_array(jrealms))) {
966 tr_err("tr_cfg_parse_remote_realms: realms not an array");
967 *rc=TR_CFG_BAD_PARAMS;
971 for (ii=0; ii<json_array_size(jrealms); ii++) {
972 this_jrealm=json_array_get(jrealms, ii);
973 if (tr_cfg_is_remote_realm(this_jrealm)) {
974 new_realm=tr_cfg_parse_one_remote_realm(tmp_ctx, this_jrealm, rc);
975 if ((*rc)!=TR_CFG_SUCCESS) {
976 tr_err("tr_cfg_parse_remote_realms: error decoding remote realm entry %d", ii+1);
980 realms=tr_idp_realm_add(realms, new_realm);
985 talloc_steal(mem_ctx, realms);
988 talloc_free(tmp_ctx);
993 static TR_GSS_NAMES *tr_cfg_parse_gss_names(TALLOC_CTX *mem_ctx, json_t *jgss_names, TR_CFG_RC *rc)
995 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
996 TR_GSS_NAMES *gn=NULL;
1001 if ((rc==NULL) || (jgss_names==NULL)) {
1002 tr_err("tr_cfg_parse_gss_names: Bad parameters.");
1003 *rc=TR_CFG_BAD_PARAMS;
1007 if (!json_is_array(jgss_names)) {
1008 tr_err("tr_cfg_parse_gss_names: gss_names not an array.");
1013 gn=tr_gss_names_new(tmp_ctx);
1014 for (ii=0; ii<json_array_size(jgss_names); ii++) {
1015 jname=json_array_get(jgss_names, ii);
1016 if (!json_is_string(jname)) {
1017 tr_err("tr_cfg_parse_gss_names: Encountered non-string gss name.");
1022 name=tr_new_name(json_string_value(jname));
1024 tr_err("tr_cfg_parse_gss_names: Out of memory allocating gss name.");
1029 if (tr_gss_names_add(gn, name)!=0) {
1031 tr_err("tr_cfg_parse_gss_names: Unable to add gss name to RP client.");
1037 talloc_steal(mem_ctx, gn);
1041 talloc_free(tmp_ctx);
1042 if ((*rc!=TR_CFG_SUCCESS) && (gn!=NULL))
1047 /* default filter accepts realm and *.realm */
1048 static TR_FILTER *tr_cfg_default_filter(TALLOC_CTX *mem_ctx, TR_NAME *realm, TR_CFG_RC *rc)
1050 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1051 TR_FILTER *filt=NULL;
1052 TR_CONSTRAINT *cons=NULL;
1054 TR_NAME *n_prefix=tr_new_name("*.");
1055 TR_NAME *n_rp_realm_1=tr_new_name("rp_realm");
1056 TR_NAME *n_rp_realm_2=tr_new_name("rp_realm");
1057 TR_NAME *n_domain=tr_new_name("domain");
1058 TR_NAME *n_realm=tr_new_name("realm");
1061 if ((realm==NULL) || (rc==NULL)) {
1062 tr_debug("tr_cfg_default_filter: invalid arguments.");
1064 *rc=TR_CFG_BAD_PARAMS;
1068 if ((n_prefix==NULL) ||
1069 (n_rp_realm_1==NULL) ||
1070 (n_rp_realm_2==NULL) ||
1073 tr_debug("tr_cfg_default_filter: unable to allocate names.");
1078 filt=tr_filter_new(tmp_ctx);
1080 tr_debug("tr_cfg_default_filter: could not allocate filter.");
1084 tr_filter_set_type(filt, TR_FILTER_TYPE_TID_INCOMING);
1085 filt->lines[0]=tr_fline_new(filt);
1086 if (filt->lines[0]==NULL) {
1087 tr_debug("tr_cfg_default_filter: could not allocate filter line.");
1092 filt->lines[0]->action=TR_FILTER_ACTION_ACCEPT;
1093 filt->lines[0]->specs[0]=tr_fspec_new(filt->lines[0]);
1094 filt->lines[0]->specs[0]->field=n_rp_realm_1;
1095 n_rp_realm_1=NULL; /* we don't own this name any more */
1097 name=tr_dup_name(realm);
1099 tr_debug("tr_cfg_default_filter: could not allocate realm name.");
1103 tr_fspec_set_match(filt->lines[0]->specs[0], name);
1104 name=NULL; /* we no longer own the name */
1106 /* now do the wildcard name */
1107 filt->lines[0]->specs[1]=tr_fspec_new(filt->lines[0]);
1108 filt->lines[0]->specs[1]->field=n_rp_realm_2;
1109 n_rp_realm_2=NULL; /* we don't own this name any more */
1111 if (NULL==(name=tr_name_cat(n_prefix, realm))) {
1112 tr_debug("tr_cfg_default_filter: could not allocate wildcard realm name.");
1117 tr_fspec_set_match(filt->lines[0]->specs[1], name);
1118 name=NULL; /* we no longer own the name */
1120 /* domain constraint */
1121 if (NULL==(cons=tr_constraint_new(filt->lines[0]))) {
1122 tr_debug("tr_cfg_default_filter: could not allocate domain constraint.");
1127 cons->type=n_domain;
1128 n_domain=NULL; /* belongs to the constraint now */
1129 name=tr_dup_name(realm);
1131 tr_debug("tr_cfg_default_filter: could not allocate realm name for domain constraint.");
1135 cons->matches[0]=name;
1136 name=tr_name_cat(n_prefix, realm);
1138 tr_debug("tr_cfg_default_filter: could not allocate wildcard realm name for domain constraint.");
1142 cons->matches[1]=name;
1144 filt->lines[0]->domain_cons=cons;
1147 /* realm constraint */
1148 if (NULL==(cons=tr_constraint_new(filt->lines[0]))) {
1149 tr_debug("tr_cfg_default_filter: could not allocate realm constraint.");
1155 n_realm=NULL; /* belongs to the constraint now */
1156 name=tr_dup_name(realm);
1158 tr_debug("tr_cfg_default_filter: could not allocate realm name for realm constraint.");
1162 cons->matches[0]=name;
1163 name=tr_name_cat(n_prefix, realm);
1165 tr_debug("tr_cfg_default_filter: could not allocate wildcard realm name for realm constraint.");
1169 cons->matches[1]=name;
1171 filt->lines[0]->realm_cons=cons;
1173 talloc_steal(mem_ctx, filt);
1175 talloc_free(tmp_ctx);
1177 if (*rc!=TR_CFG_SUCCESS)
1181 tr_free_name(n_prefix);
1182 if (n_rp_realm_1!=NULL)
1183 tr_free_name(n_rp_realm_1);
1184 if (n_rp_realm_2!=NULL)
1185 tr_free_name(n_rp_realm_2);
1187 tr_free_name(n_realm);
1189 tr_free_name(n_domain);
1196 /* parses rp client */
1197 static TR_RP_CLIENT *tr_cfg_parse_one_rp_client(TALLOC_CTX *mem_ctx, json_t *jrealm, TR_CFG_RC *rc)
1199 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1200 TR_RP_CLIENT *client=NULL;
1201 TR_CFG_RC call_rc=TR_CFG_ERROR;
1202 TR_FILTER *new_filt=NULL;
1203 TR_NAME *realm=NULL;
1205 json_t *jrealm_id=NULL;
1207 *rc=TR_CFG_ERROR; /* default to error if not set */
1209 if ((!jrealm) || (!rc)) {
1210 tr_err("tr_cfg_parse_one_rp_client: Bad parameters.");
1212 *rc=TR_CFG_BAD_PARAMS;
1216 if ((NULL==(jrealm_id=json_object_get(jrealm, "realm"))) || (!json_is_string(jrealm_id))) {
1217 tr_debug("tr_cfg_parse_one_rp_client: no realm ID found.");
1218 *rc=TR_CFG_BAD_PARAMS;
1222 tr_debug("tr_cfg_parse_one_rp_client: realm_id=\"%s\"", json_string_value(jrealm_id));
1223 realm=tr_new_name(json_string_value(jrealm_id));
1225 tr_err("tr_cfg_parse_one_rp_client: could not allocate realm ID.");
1230 if (NULL==(client=tr_rp_client_new(tmp_ctx))) {
1231 tr_err("tr_cfg_parse_one_rp_client: could not allocate rp client.");
1236 client->gss_names=tr_cfg_parse_gss_names(client, json_object_get(jrealm, "gss_names"), &call_rc);
1238 if (call_rc!=TR_CFG_SUCCESS) {
1239 tr_err("tr_cfg_parse_one_rp_client: could not parse gss_names.");
1245 jfilt=json_object_get(jrealm, "filters");
1247 new_filt=tr_cfg_parse_filters(tmp_ctx, jfilt, &call_rc);
1248 if (call_rc!=TR_CFG_SUCCESS) {
1249 tr_err("tr_cfg_parse_one_rp_client: could not parse filters.");
1254 tr_debug("tr_cfg_parse_one_rp_client: no filters specified, using default filters.");
1255 new_filt=tr_cfg_default_filter(tmp_ctx, realm, &call_rc);
1256 if (call_rc!=TR_CFG_SUCCESS) {
1257 tr_err("tr_cfg_parse_one_rp_client: could not set default filters.");
1263 tr_rp_client_set_filter(client, new_filt);
1268 tr_free_name(realm);
1270 if (*rc==TR_CFG_SUCCESS)
1271 talloc_steal(mem_ctx, client);
1273 talloc_free(client);
1277 talloc_free(tmp_ctx);
1281 /* Determine whether the realm is an RP realm */
1282 static int tr_cfg_is_rp_realm(json_t *jrealm)
1284 /* Check that we have a gss name. */
1285 if (NULL != json_object_get(jrealm, "gss_names"))
1291 /* Parse any rp clients in the j_realms object. Ignores other realms. */
1292 static TR_RP_CLIENT *tr_cfg_parse_rp_clients(TALLOC_CTX *mem_ctx, json_t *jrealms, TR_CFG_RC *rc)
1294 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1295 TR_RP_CLIENT *clients=NULL;
1296 TR_RP_CLIENT *new_client=NULL;
1297 json_t *this_jrealm=NULL;
1301 if ((jrealms==NULL) || (!json_is_array(jrealms))) {
1302 tr_err("tr_cfg_parse_rp_clients: realms not an array");
1303 *rc=TR_CFG_BAD_PARAMS;
1307 for (ii=0; ii<json_array_size(jrealms); ii++) {
1308 this_jrealm=json_array_get(jrealms, ii);
1309 if (tr_cfg_is_rp_realm(this_jrealm)) {
1310 new_client=tr_cfg_parse_one_rp_client(tmp_ctx, this_jrealm, rc);
1311 if ((*rc)!=TR_CFG_SUCCESS) {
1312 tr_err("tr_cfg_parse_rp_clients: error decoding realm entry %d", ii+1);
1316 tr_rp_client_add(clients, new_client);
1321 talloc_steal(mem_ctx, clients);
1324 talloc_free(tmp_ctx);
1328 /* takes a talloc context, but currently does not use it */
1329 static TR_NAME *tr_cfg_parse_org_name(TALLOC_CTX *mem_ctx, json_t *j_org, TR_CFG_RC *rc)
1333 if ((j_org==NULL) || (rc==NULL) || (!json_is_string(j_org))) {
1334 tr_debug("tr_cfg_parse_org_name: Bad parameters.");
1336 *rc = TR_CFG_BAD_PARAMS; /* fill in return value if we can */
1340 name=tr_new_name(json_string_value(j_org));
1349 /* Update the community information with data from a new batch of IDP realms.
1350 * May partially add realms if there is a failure, no guarantees.
1351 * Call like comms=tr_comm_idp_update(comms, new_realms, &rc) */
1352 static TR_COMM *tr_cfg_comm_idp_update(TALLOC_CTX *mem_ctx, TR_COMM *comms, TR_IDP_REALM *new_realms, TR_CFG_RC *rc)
1354 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1355 TR_COMM *comm=NULL; /* community looked up in comms table */
1356 TR_COMM *new_comms=NULL; /* new communities as we create them */
1357 TR_IDP_REALM *realm=NULL;
1358 TR_APC *apc=NULL; /* apc of one realm */
1361 *rc=TR_CFG_BAD_PARAMS;
1365 /* start with an empty list communities, then fill that in */
1366 for (realm=new_realms; realm!=NULL; realm=realm->next) {
1367 for (apc=realm->apcs; apc!=NULL; apc=apc->next) {
1368 comm=tr_comm_lookup(comms, apc->id);
1370 comm=tr_comm_new(tmp_ctx);
1372 tr_debug("tr_cfg_comm_idp_update: unable to allocate new community.");
1376 /* fill in the community with info */
1377 comm->type=TR_COMM_APC; /* realms added this way are in APCs */
1378 comm->expiration_interval=TR_DEFAULT_APC_EXPIRATION_INTERVAL;
1379 comm->id=tr_dup_name(apc->id);
1380 tr_comm_add_idp_realm(comm, realm);
1381 tr_comm_add(new_comms, comm);
1383 /* add this realm to the comm */
1384 tr_comm_add_idp_realm(comm, realm);
1389 /* we successfully built a list, add it to the other list */
1390 tr_comm_add(comms, new_comms);
1391 talloc_steal(mem_ctx, comms);
1393 talloc_free(tmp_ctx);
1398 static TR_CFG_RC tr_cfg_parse_one_local_org(TR_CFG *trc, json_t *jlorg)
1400 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1401 TR_CFG_RC retval=TR_CFG_ERROR; /* our return code */
1402 TR_CFG_RC rc=TR_CFG_ERROR; /* return code from functions we call */
1403 TR_NAME *org_name=NULL;
1405 json_t *j_realms=NULL;
1406 TR_IDP_REALM *new_idp_realms=NULL;
1407 TR_RP_CLIENT *new_rp_clients=NULL;
1409 tr_debug("tr_cfg_parse_one_local_org: parsing local organization");
1411 /* get organization_name (optional) */
1412 if (NULL==(j_org=json_object_get(jlorg, "organization_name"))) {
1413 tr_debug("tr_cfg_parse_one_local_org: organization_name unspecified");
1415 org_name=tr_cfg_parse_org_name(tmp_ctx, j_org, &rc);
1416 if (rc==TR_CFG_SUCCESS) {
1417 tr_debug("tr_cfg_parse_one_local_org: organization_name=\"%.*s\"",
1420 /* we don't actually do anything with this, but we could */
1421 tr_free_name(org_name);
1426 /* Now get realms. Allow this to be missing; even though that is a pointless organization entry,
1427 * it's harmless. Report a warning because that might be unintentional. */
1428 if (NULL==(j_realms=json_object_get(jlorg, "realms"))) {
1429 tr_warning("tr_cfg_parse_one_local_org: warning - no realms in this local organization");
1431 /* Allocate in the tmp_ctx so these will be cleaned up if we do not complete successfully. */
1432 new_idp_realms=tr_cfg_parse_idp_realms(tmp_ctx, j_realms, &rc);
1433 if (rc!=TR_CFG_SUCCESS)
1436 new_rp_clients=tr_cfg_parse_rp_clients(tmp_ctx, j_realms, &rc);
1437 if (rc!=TR_CFG_SUCCESS)
1440 retval=TR_CFG_SUCCESS;
1443 /* if we succeeded, link things to the configuration and move out of tmp context */
1444 if (retval==TR_CFG_SUCCESS) {
1445 if (new_idp_realms!=NULL) {
1446 tr_idp_realm_add(trc->idp_realms, new_idp_realms); /* fixes talloc contexts except for head*/
1447 talloc_steal(trc, trc->idp_realms); /* make sure the head is in the right context */
1450 if (new_rp_clients!=NULL) {
1451 tr_rp_client_add(trc->rp_clients, new_rp_clients); /* fixes talloc contexts */
1452 talloc_steal(trc, trc->rp_clients); /* make sure head is in the right context */
1456 talloc_free(tmp_ctx);
1460 /* Parse local organizations if present. Returns success if there are none. On failure, the configuration is unreliable. */
1461 static TR_CFG_RC tr_cfg_parse_local_orgs(TR_CFG *trc, json_t *jcfg)
1463 json_t *jlocorgs=NULL;
1466 jlocorgs=json_object_get(jcfg, "local_organizations");
1468 return TR_CFG_SUCCESS;
1470 if (!json_is_array(jlocorgs)) {
1471 tr_err("tr_cfg_parse_local_orgs: local_organizations is not an array.");
1472 return TR_CFG_NOPARSE;
1475 for (ii=0; ii<json_array_size(jlocorgs); ii++) {
1476 if (tr_cfg_parse_one_local_org(trc, json_array_get(jlocorgs, ii))!=TR_CFG_SUCCESS) {
1477 tr_err("tr_cfg_parse_local_orgs: error parsing local_organization %d.", ii+1);
1478 return TR_CFG_NOPARSE;
1482 return TR_CFG_SUCCESS;
1485 static TR_CFG_RC tr_cfg_parse_one_peer_org(TR_CFG *trc, json_t *jporg)
1487 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1491 TRP_PEER *new_peer=NULL;
1492 TR_GSS_NAMES *names=NULL;
1493 TR_CFG_RC rc=TR_CFG_ERROR;
1495 jhost=json_object_get(jporg, "hostname");
1496 jport=json_object_get(jporg, "port");
1497 jgss=json_object_get(jporg, "gss_names");
1499 if ((jhost==NULL) || (!json_is_string(jhost))) {
1500 tr_err("tr_cfg_parse_one_peer_org: hostname not specified or not a string.");
1505 if ((jport!=NULL) && (!json_is_number(jport))) {
1506 /* note that not specifying the port is allowed, but if set it must be a number */
1507 tr_err("tr_cfg_parse_one_peer_org: port is not a number.");
1512 if ((jgss==NULL) || (!json_is_array(jgss))) {
1513 tr_err("tr_cfg_parse_one_peer_org: gss_names not specified or not an array.");
1518 new_peer=trp_peer_new(tmp_ctx);
1519 if (new_peer==NULL) {
1520 tr_err("tr_cfg_parse_one_peer_org: could not allocate new peer.");
1525 trp_peer_set_server(new_peer, json_string_value(jhost));
1527 trp_peer_set_port(new_peer, TRP_PORT);
1529 trp_peer_set_port(new_peer, json_integer_value(jport));
1531 names=tr_cfg_parse_gss_names(tmp_ctx, jgss, &rc);
1532 if (rc!=TR_CFG_SUCCESS) {
1533 tr_err("tr_cfg_parse_one_peer_org: unable to parse gss names.");
1537 trp_peer_set_gss_names(new_peer, names);
1540 trp_ptable_add(trc->peers, new_peer);
1544 talloc_free(tmp_ctx);
1548 /* Parse peer organizations, if present. Returns success if there are none. */
1549 static TR_CFG_RC tr_cfg_parse_peer_orgs(TR_CFG *trc, json_t *jcfg)
1551 json_t *jpeerorgs=NULL;
1554 jpeerorgs=json_object_get(jcfg, "peer_organizations");
1555 if (jpeerorgs==NULL)
1556 return TR_CFG_SUCCESS;
1558 if (!json_is_array(jpeerorgs)) {
1559 tr_err("tr_cfg_parse_peer_orgs: peer_organizations is not an array.");
1560 return TR_CFG_NOPARSE;
1563 for (ii=0; ii<json_array_size(jpeerorgs); ii++) {
1564 if (tr_cfg_parse_one_peer_org(trc, json_array_get(jpeerorgs, ii))!=TR_CFG_SUCCESS) {
1565 tr_err("tr_cfg_parse_peer_orgs: error parsing peer_organization %d.", ii+1);
1566 return TR_CFG_NOPARSE;
1570 return TR_CFG_SUCCESS;
1573 static TR_CFG_RC tr_cfg_parse_default_servers (TR_CFG *trc, json_t *jcfg)
1575 json_t *jdss = NULL;
1576 TR_CFG_RC rc = TR_CFG_SUCCESS;
1577 TR_AAA_SERVER *ds = NULL;
1580 if ((!trc) || (!jcfg))
1581 return TR_CFG_BAD_PARAMS;
1583 /* If there are default servers, store them */
1584 if ((NULL != (jdss = json_object_get(jcfg, "default_servers"))) &&
1585 (json_is_array(jdss)) &&
1586 (0 < json_array_size(jdss))) {
1588 for (i = 0; i < json_array_size(jdss); i++) {
1589 if (NULL == (ds = tr_cfg_parse_one_aaa_server(trc,
1590 json_array_get(jdss, i),
1594 tr_debug("tr_cfg_parse_default_servers: Default server configured: %s", ds->hostname->buf);
1595 ds->next = trc->default_servers;
1596 trc->default_servers = ds;
1600 tr_debug("tr_cfg_parse_default_servers: Finished (rc=%d)", rc);
1604 static TR_IDP_REALM *tr_cfg_parse_comm_idps (TR_CFG *trc, json_t *jidps, TR_CFG_RC *rc)
1606 TR_IDP_REALM *idp = NULL;
1607 TR_IDP_REALM *found_idp = NULL;
1608 TR_IDP_REALM *temp_idp = NULL;
1613 (!json_is_array(jidps))) {
1615 *rc = TR_CFG_BAD_PARAMS;
1619 for (i = 0; i < json_array_size(jidps); i++) {
1620 if (NULL == (temp_idp = talloc(trc, TR_IDP_REALM))) {
1621 tr_debug("tr_cfg_parse_comm_idps: Can't allocate memory for IdP Realm.");
1626 memset (temp_idp, 0, sizeof(TR_IDP_REALM));
1628 if (NULL == (found_idp = (tr_cfg_find_idp(trc,
1629 tr_new_name((char *)json_string_value(json_array_get(jidps, i))),
1631 tr_debug("tr_cfg_parse_comm_idps: Unknown IDP %s.",
1632 (char *)json_string_value(json_array_get(jidps, i)));
1636 // We *MUST* do a dereferenced copy here or the second community will corrupt the linked list we create here.
1637 *temp_idp = *found_idp;
1639 temp_idp->comm_next = idp;
1646 static TR_RP_REALM *tr_cfg_parse_comm_rps (TR_CFG *trc, json_t *jrps, TR_CFG_RC *rc)
1648 TR_RP_REALM *rp = NULL;
1649 TR_RP_REALM *temp_rp = NULL;
1654 (!json_is_array(jrps))) {
1656 *rc = TR_CFG_BAD_PARAMS;
1660 for (i = (json_array_size(jrps)-1); i >= 0; i--) {
1661 if (NULL == (temp_rp = talloc_zero(trc, TR_RP_REALM))) {
1662 tr_debug("tr_cfg_parse_comm_rps: Can't allocate memory for RP Realm.");
1668 if (NULL == (temp_rp->realm_name = tr_new_name((char *)json_string_value(json_array_get(jrps, i))))) {
1669 tr_debug("tr_cfg_parse_comm_rps: No memory for RP Realm Name.");
1682 static TR_COMM *tr_cfg_parse_one_comm (TR_CFG *trc, json_t *jcomm, TR_CFG_RC *rc) {
1683 TR_COMM *comm = NULL;
1685 json_t *jtype = NULL;
1686 json_t *japcs = NULL;
1687 json_t *jidps = NULL;
1688 json_t *jrps = NULL;
1690 if ((!trc) || (!jcomm) || (!rc)) {
1691 tr_debug("tr_cfg_parse_one_comm: Bad parameters.");
1693 *rc = TR_CFG_BAD_PARAMS;
1697 if (NULL == (comm = talloc_zero(trc, TR_COMM))) {
1698 tr_crit("tr_cfg_parse_one_comm: Out of memory.");
1704 if ((NULL == (jid = json_object_get(jcomm, "community_id"))) ||
1705 (!json_is_string(jid)) ||
1706 (NULL == (jtype = json_object_get(jcomm, "type"))) ||
1707 (!json_is_string(jtype)) ||
1708 (NULL == (japcs = json_object_get(jcomm, "apcs"))) ||
1709 (!json_is_array(japcs)) ||
1710 (NULL == (jidps = json_object_get(jcomm, "idp_realms"))) ||
1711 (!json_is_array(jidps)) ||
1712 (NULL == (jrps = json_object_get(jcomm, "rp_realms"))) ||
1713 (!json_is_array(jrps))) {
1714 tr_debug("tr_cfg_parse_one_comm: Error parsing Communities configuration.");
1715 *rc = TR_CFG_NOPARSE;
1719 if (NULL == (comm->id = tr_new_name((char *)json_string_value(jid)))) {
1720 tr_debug("tr_cfg_parse_one_comm: No memory for community id.");
1725 if (0 == strcmp(json_string_value(jtype), "apc")) {
1726 comm->type = TR_COMM_APC;
1727 } else if (0 == strcmp(json_string_value(jtype), "coi")) {
1728 comm->type = TR_COMM_COI;
1729 if (NULL == (comm->apcs = tr_cfg_parse_apcs(trc, japcs, rc))) {
1730 tr_debug("tr_cfg_parse_one_comm: Can't parse APCs for COI %s.", comm->id->buf);
1731 tr_free_name(comm->id);
1735 tr_debug("tr_cfg_parse_one_comm: Invalid community type, comm = %s, type = %s", comm->id->buf, json_string_value(jtype));
1736 tr_free_name(comm->id);
1737 *rc = TR_CFG_NOPARSE;
1741 comm->idp_realms = tr_cfg_parse_comm_idps(trc, jidps, rc);
1742 if (TR_CFG_SUCCESS != *rc) {
1743 tr_debug("tr_cfg_parse_one_comm: Can't parse IDP realms for comm %s.", comm->id->buf);
1744 tr_free_name(comm->id);
1748 comm->rp_realms = tr_cfg_parse_comm_rps(trc, jrps, rc);
1749 if (TR_CFG_SUCCESS != *rc) {
1750 tr_debug("tr_cfg_parse_comm: Can't parse RP realms for comm %s .", comm->id->buf);
1751 tr_free_name(comm->id);
1755 if (TR_COMM_APC == comm->type) {
1756 json_t *jexpire = json_object_get(jcomm, "expiration_interval");
1757 comm->expiration_interval = 43200; /*30 days*/
1759 if (!json_is_integer(jexpire)) {
1760 fprintf(stderr, "tr_parse_comm: expirae_interval is not an integer\n");
1763 comm->expiration_interval = json_integer_value(jexpire);
1764 if (comm->expiration_interval <= 10)
1765 comm->expiration_interval = 11; /* Freeradius waits 10 minutes between successful TR queries*/
1766 if (comm->expiration_interval > 129600) /* 90 days*/
1767 comm->expiration_interval = 129600;
1774 static TR_CFG_RC tr_cfg_parse_comms (TR_CFG *trc, json_t *jcfg)
1776 json_t *jcomms = NULL;
1777 TR_CFG_RC rc = TR_CFG_SUCCESS;
1778 TR_COMM *comm = NULL;
1781 if ((!trc) || (!jcfg)) {
1782 tr_debug("tr_cfg_parse_comms: Bad Parameters.");
1783 return TR_CFG_BAD_PARAMS;
1786 if (NULL != (jcomms = json_object_get(jcfg, "communities"))) {
1787 if (!json_is_array(jcomms)) {
1788 return TR_CFG_NOPARSE;
1791 for (i = 0; i < json_array_size(jcomms); i++) {
1792 if (NULL == (comm = tr_cfg_parse_one_comm(trc,
1793 json_array_get(jcomms, i),
1797 tr_debug("tr_cfg_parse_comms: Community configured: %s.", comm->id->buf);
1798 comm->next = trc->comms;
1802 tr_debug("tr_cfg_parse_comms: Finished (rc=%d)", rc);
1806 TR_CFG_RC tr_cfg_validate(TR_CFG *trc)
1808 TR_CFG_RC rc = TR_CFG_SUCCESS;
1811 return TR_CFG_BAD_PARAMS;
1813 if ((NULL == trc->internal)||
1814 (NULL == trc->internal->hostname)) {
1815 tr_debug("tr_cfg_validate: Error: No internal configuration, or no hostname.");
1819 if (NULL == trc->rp_clients) {
1820 tr_debug("tr_cfg_validate: Error: No RP Clients configured");
1824 if (NULL == trc->comms) {
1825 tr_debug("tr_cfg_validate: Error: No Communities configured");
1829 if ((NULL == trc->default_servers) && (NULL == trc->idp_realms)) {
1830 tr_debug("tr_cfg_validate: Error: No default servers or IDPs configured.");
1837 /* Join two paths and return a pointer to the result. This should be freed
1838 * via talloc_free. Returns NULL on failure. */
1839 static char *join_paths(TALLOC_CTX *mem_ctx, const char *p1, const char *p2)
1841 return talloc_asprintf(mem_ctx, "%s/%s", p1, p2); /* returns NULL on a failure */
1844 static void tr_cfg_log_json_error(const char *label, json_error_t *rc)
1846 tr_debug("%s: JSON parse error on line %d: %s",
1852 TR_CFG_RC tr_cfg_parse_one_config_file(TR_CFG *cfg, const char *file_with_path)
1858 if (NULL==(jcfg=json_load_file(file_with_path,
1859 JSON_DISABLE_EOF_CHECK, &rc))) {
1860 tr_debug("tr_cfg_parse_one_config_file: Error parsing config file %s.",
1862 tr_cfg_log_json_error("tr_cfg_parse_one_config_file", &rc);
1863 return TR_CFG_NOPARSE;
1866 // Look for serial number and log it if it exists
1867 if (NULL!=(jser=json_object_get(jcfg, "serial_number"))) {
1868 if (json_is_number(jser)) {
1869 tr_notice("tr_parse_one_config_file: Attempting to load revision %" JSON_INTEGER_FORMAT " of '%s'.",
1870 json_integer_value(jser),
1875 if ((TR_CFG_SUCCESS != tr_cfg_parse_internal(cfg, jcfg)) ||
1876 (TR_CFG_SUCCESS != tr_cfg_parse_local_orgs(cfg, jcfg)) ||
1877 (TR_CFG_SUCCESS != tr_cfg_parse_peer_orgs(cfg, jcfg)) ||
1878 (TR_CFG_SUCCESS != tr_cfg_parse_default_servers(cfg, jcfg)) ||
1879 (TR_CFG_SUCCESS != tr_cfg_parse_comms(cfg, jcfg)))
1880 return TR_CFG_ERROR;
1882 return TR_CFG_SUCCESS;
1885 /* Reads configuration files in config_dir ("" or "./" will use the current directory). */
1886 TR_CFG_RC tr_parse_config(TR_CFG_MGR *cfg_mgr, const char *config_dir, int n, struct dirent **cfg_files)
1888 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1889 char *file_with_path;
1891 TR_CFG_RC cfg_rc=TR_CFG_ERROR;
1893 if ((!cfg_mgr) || (!cfg_files) || (n<=0)) {
1894 cfg_rc=TR_CFG_BAD_PARAMS;
1898 if (cfg_mgr->new != NULL)
1899 tr_cfg_free(cfg_mgr->new);
1900 cfg_mgr->new=tr_cfg_new(tmp_ctx); /* belongs to the temporary context for now */
1901 if (cfg_mgr->new == NULL) {
1902 cfg_rc=TR_CFG_NOMEM;
1906 cfg_mgr->new->peers=trp_ptable_new(cfg_mgr);
1908 /* Parse configuration information from each config file */
1909 for (ii=0; ii<n; ii++) {
1910 file_with_path=join_paths(tmp_ctx, config_dir, cfg_files[ii]->d_name); /* must free result with talloc_free */
1911 if(file_with_path == NULL) {
1912 tr_crit("tr_parse_config: error joining path.");
1913 cfg_rc=TR_CFG_NOMEM;
1916 tr_debug("tr_parse_config: Parsing %s.", cfg_files[ii]->d_name); /* print the filename without the path */
1917 cfg_rc=tr_cfg_parse_one_config_file(cfg_mgr->new, file_with_path);
1918 if (cfg_rc!=TR_CFG_SUCCESS) {
1919 tr_crit("tr_parse_config: Error parsing %s", file_with_path);
1922 talloc_free(file_with_path); /* done with filename */
1925 /* make sure we got a complete, consistent configuration */
1926 if (TR_CFG_SUCCESS != tr_cfg_validate(cfg_mgr->new)) {
1927 tr_err("tr_parse_config: Error: INVALID CONFIGURATION");
1928 cfg_rc=TR_CFG_ERROR;
1933 talloc_steal(cfg_mgr, cfg_mgr->new); /* hand this over to the cfg_mgr context */
1934 cfg_rc=TR_CFG_SUCCESS;
1937 talloc_free(tmp_ctx);
1941 TR_IDP_REALM *tr_cfg_find_idp (TR_CFG *tr_cfg, TR_NAME *idp_id, TR_CFG_RC *rc)
1944 TR_IDP_REALM *cfg_idp;
1946 if ((!tr_cfg) || (!idp_id)) {
1948 *rc = TR_CFG_BAD_PARAMS;
1952 for (cfg_idp = tr_cfg->idp_realms; NULL != cfg_idp; cfg_idp = cfg_idp->next) {
1953 if (!tr_name_cmp (idp_id, cfg_idp->realm_id)) {
1954 tr_debug("tr_cfg_find_idp: Found %s.", idp_id->buf);
1958 /* if we didn't find one, return NULL */
1962 TR_RP_CLIENT *tr_cfg_find_rp (TR_CFG *tr_cfg, TR_NAME *rp_gss, TR_CFG_RC *rc)
1964 TR_RP_CLIENT *cfg_rp;
1966 if ((!tr_cfg) || (!rp_gss)) {
1968 *rc = TR_CFG_BAD_PARAMS;
1972 for (cfg_rp = tr_cfg->rp_clients; NULL != cfg_rp; cfg_rp = cfg_rp->next) {
1973 if (tr_gss_names_matches(cfg_rp->gss_names, rp_gss)) {
1974 tr_debug("tr_cfg_find_rp: Found %s.", rp_gss->buf);
1978 /* if we didn't find one, return NULL */
1982 static int is_cfg_file(const struct dirent *dent) {
1985 /* Only accept filenames ending in ".cfg" and starting with a character
1986 * other than an ASCII '.' */
1988 /* filename must be at least 4 characters long to be acceptable */
1989 n=strlen(dent->d_name);
1994 /* filename must not start with '.' */
1995 if ('.' == dent->d_name[0]) {
1999 /* If the above passed and the last four characters of the filename are .cfg, accept.
2000 * (n.b., assumes an earlier test checked that the name is >= 4 chars long.) */
2001 if (0 == strcmp(&(dent->d_name[n-4]), ".cfg")) {
2005 /* otherwise, return false. */
2009 /* Find configuration files in a particular directory. Returns the
2010 * number of entries found, 0 if none are found, or <0 for some
2011 * errors. If n>=0, the cfg_files parameter will contain a newly
2012 * allocated array of pointers to struct dirent entries, as returned
2013 * by scandir(). These can be freed with tr_free_config_file_list().
2015 int tr_find_config_files (const char *config_dir, struct dirent ***cfg_files) {
2018 n = scandir(config_dir, cfg_files, is_cfg_file, alphasort);
2022 tr_debug("tr_find_config: scandir error trying to scan %s.", config_dir);
2028 /* Free memory allocated for configuration file list returned from tr_find_config_files().
2029 * This can be called regardless of the return value of tr_find_config_values(). */
2030 void tr_free_config_file_list(int n, struct dirent ***cfg_files) {
2033 /* if n < 0, then scandir did not allocate anything because it failed */
2034 if((n>=0) && (*cfg_files != NULL)) {
2035 for(ii=0; ii<n; ii++) {
2036 free((*cfg_files)[ii]);
2038 free(*cfg_files); /* safe even if n==0 */
2039 *cfg_files=NULL; /* this will help prevent accidentally freeing twice */