#include <tr_cfgwatch.h>
#include <tr_comm.h>
#include <tr_config.h>
-#include <tr_gss.h>
+#include <tr_gss_names.h>
#include <tr_debug.h>
#include <tr_filter.h>
#include <trust_router/tr_constraint.h>
}
if (NULL != (jhname = json_object_get(jint, "hostname"))) {
if (json_is_string(jhname)) {
- trc->internal->hostname = json_string_value(jhname);
+ trc->internal->hostname = talloc_strdup(trc->internal, json_string_value(jhname));
} else {
tr_debug("tr_cfg_parse_internal: Parsing error, hostname is not a string.");
return TR_CFG_NOPARSE;
return name;
}
-#if 0
-/* TODO: are we using this? JLR */
-/* Update the community information with data from a new batch of IDP realms.
- * May partially add realms if there is a failure, no guarantees.
- * Call like comms=tr_comm_idp_update(comms, new_realms, &rc) */
-static TR_COMM *tr_cfg_comm_idp_update(TALLOC_CTX *mem_ctx,
- TR_COMM_TABLE *ctab,
- TR_IDP_REALM *new_realms,
- TR_CFG_RC *rc)
-{
- TALLOC_CTX *tmp_ctx=talloc_new(NULL);
- TR_COMM *comm=NULL; /* community looked up in comms table */
- TR_COMM *new_comms=NULL; /* new communities as we create them */
- TR_IDP_REALM *realm=NULL;
- TR_APC *apc=NULL; /* apc of one realm */
-
- if (rc==NULL) {
- *rc=TR_CFG_BAD_PARAMS;
- goto cleanup;
- }
-
- /* start with an empty list communities, then fill that in */
- for (realm=new_realms; realm!=NULL; realm=realm->next) {
- for (apc=realm->apcs; apc!=NULL; apc=apc->next) {
- comm=tr_comm_lookup(comms, apc->id);
- if (comm==NULL) {
- comm=tr_comm_new(tmp_ctx);
- if (comm==NULL) {
- tr_debug("tr_cfg_comm_idp_update: unable to allocate new community.");
- *rc=TR_CFG_NOMEM;
- goto cleanup;
- }
- /* fill in the community with info */
- comm->type=TR_COMM_APC; /* realms added this way are in APCs */
- comm->expiration_interval=TR_DEFAULT_APC_EXPIRATION_INTERVAL;
- tr_comm_set_id(comm, tr_dup_name(apc->id));
- tr_comm_add_idp_realm(comm, realm);
- tr_comm_add(new_comms, comm);
- } else {
- /* add this realm to the comm */
- tr_comm_add_idp_realm(comm, realm);
- }
- }
- }
-
- /* we successfully built a list, add it to the other list */
- tr_comm_add(comms, new_comms);
- talloc_steal(mem_ctx, comms);
- cleanup:
- talloc_free(tmp_ctx);
- return comms;
-}
-#endif
-
static TR_CFG_RC tr_cfg_parse_one_local_org(TR_CFG *trc, json_t *jlorg)
{
TALLOC_CTX *tmp_ctx=talloc_new(NULL);
goto cleanup;
}
- if ((jfilt!=NULL) && (!json_is_array(jfilt))) {
- tr_err("tr_cfg_parse_one_peer_org: filters is not an array.");
+ if ((jfilt!=NULL) && (!json_is_object(jfilt))) {
+ tr_err("tr_cfg_parse_one_peer_org: filters is not an object.");
rc=TR_CFG_NOPARSE;
goto cleanup;
}
goto cleanup;
}
- trp_peer_set_server(new_peer, json_string_value(jhost));
+ trp_peer_set_server(new_peer, json_string_value(jhost)); /* string is strdup'ed in _set_server() */
if (jport==NULL)
trp_peer_set_port(new_peer, TRP_PORT);
else
static void tr_cfg_parse_comm_idps(TR_CFG *trc, json_t *jidps, TR_COMM *comm, TR_CFG_RC *rc)
{
TR_IDP_REALM *found_idp=NULL;
- int i = 0;
+ json_t *jidp_name=NULL;
+ TR_NAME *idp_name=NULL;
+ size_t ii = 0;
if ((!trc) ||
(!jidps) ||
return;
}
- for (i=0; i < json_array_size(jidps); i++) {
- found_idp=tr_cfg_find_idp(trc,
- tr_new_name((char *)json_string_value(json_array_get(jidps, i))),
- rc);
+ json_array_foreach(jidps, ii, jidp_name) {
+ idp_name=tr_new_name(json_string_value(jidp_name));
+ if (idp_name==NULL) {
+ *rc = TR_CFG_NOMEM;
+ return;
+ }
+ found_idp=tr_cfg_find_idp(trc, idp_name, rc);
+ tr_free_name(idp_name);
+
if ((found_idp==NULL) || (*rc!=TR_CFG_SUCCESS)) {
- tr_debug("tr_cfg_parse_comm_idps: Unknown IDP %s.",
- (char *)json_string_value(json_array_get(jidps, i)));
+ tr_debug("tr_cfg_parse_comm_idps: Unknown IDP %s.", json_string_value(jidp_name));
*rc=TR_CFG_ERROR;
return;
}
json_t *jexpire = json_object_get(jcomm, "expiration_interval");
comm->expiration_interval = 43200; /*30 days*/
if (jexpire) {
- if (!json_is_integer(jexpire)) {
- fprintf(stderr, "tr_parse_one_comm: expiration_interval is not an integer\n");
- comm=NULL;
- goto cleanup;
- }
- comm->expiration_interval = json_integer_value(jexpire);
- if (comm->expiration_interval <= 10)
- comm->expiration_interval = 11; /* Freeradius waits 10 minutes between successful TR queries*/
- if (comm->expiration_interval > 129600) /* 90 days*/
- comm->expiration_interval = 129600;
+ if (!json_is_integer(jexpire)) {
+ tr_err("tr_parse_one_comm: expiration_interval is not an integer for comm %.*s",
+ tr_comm_get_id(comm)->len, tr_comm_get_id(comm)->buf);
+ comm=NULL;
+ goto cleanup;
+ }
+ comm->expiration_interval = json_integer_value(jexpire);
+ if (comm->expiration_interval <= 10) {
+ comm->expiration_interval = 11; /* Freeradius waits 10 minutes between successful TR queries*/
+ tr_notice(
+ "tr_parse_one_comm: expiration interval for %.*s less than minimum of 11 minutes; using 11 minutes instead.",
+ tr_comm_get_id(comm)->len, tr_comm_get_id(comm)->buf);
+ }
+ if (comm->expiration_interval > 129600) {
+ /* > 90 days*/
+ comm->expiration_interval = 129600;
+ tr_notice(
+ "tr_parse_one_comm: expiration interval for %.*s exceeds maximum of 90 days; using 90 days instead.",
+ tr_comm_get_id(comm)->len, tr_comm_get_id(comm)->buf);
+ }
}
}
return rc;
}
-/* Join two paths and return a pointer to the result. This should be freed
- * via talloc_free. Returns NULL on failure. */
-static char *join_paths(TALLOC_CTX *mem_ctx, const char *p1, const char *p2)
-{
- return talloc_asprintf(mem_ctx, "%s/%s", p1, p2); /* returns NULL on a failure */
-}
-
static void tr_cfg_log_json_error(const char *label, json_error_t *rc)
{
tr_debug("%s: JSON parse error on line %d: %s",
rc->text);
}
-TR_CFG_RC tr_cfg_parse_one_config_file(TR_CFG *cfg, const char *file_with_path)
+/**
+ * Parse a config file and return its JSON structure. Also emits a serial number to the log
+ * if one is present.
+ *
+ * @param file_with_path The file (with path!) to parse
+ * @return Pointer to the result of parsing, or null on error
+ */
+static json_t *tr_cfg_parse_one_config_file(const char *file_with_path)
{
json_t *jcfg=NULL;
json_t *jser=NULL;
tr_debug("tr_cfg_parse_one_config_file: Error parsing config file %s.",
file_with_path);
tr_cfg_log_json_error("tr_cfg_parse_one_config_file", &rc);
- return TR_CFG_NOPARSE;
+ return NULL;
}
- // Look for serial number and log it if it exists
+ // Look for serial number and log it if it exists (borrowed reference, so no need to free it later)
if (NULL!=(jser=json_object_get(jcfg, "serial_number"))) {
if (json_is_number(jser)) {
tr_notice("tr_parse_one_config_file: Attempting to load revision %" JSON_INTEGER_FORMAT " of '%s'.",
}
}
- if ((TR_CFG_SUCCESS != tr_cfg_parse_internal(cfg, jcfg)) ||
- (TR_CFG_SUCCESS != tr_cfg_parse_local_orgs(cfg, jcfg)) ||
- (TR_CFG_SUCCESS != tr_cfg_parse_peer_orgs(cfg, jcfg)) ||
- (TR_CFG_SUCCESS != tr_cfg_parse_default_servers(cfg, jcfg)) ||
- (TR_CFG_SUCCESS != tr_cfg_parse_comms(cfg, jcfg)))
+ return jcfg;
+}
+
+/**
+ * Helper to free an array returned by tr_cfg_parse_config_files
+ * @param n_jcfgs
+ * @param jcfgs
+ */
+static void tr_cfg_parse_free_jcfgs(unsigned int n_jcfgs, json_t **jcfgs)
+{
+ int ii=0;
+ for (ii=0; ii<n_jcfgs; ii++)
+ json_decref(jcfgs[ii]);
+ talloc_free(jcfgs);
+}
+
+/**
+ * Parse a list of configuration files. Returns an array of JSON objects, free this with
+ * tr_cfg_parse_free_jcfgs(), a helper function
+ *
+ * @param config_dir
+ * @param n_files
+ * @param cfg_files
+ * @return
+ */
+static json_t **tr_cfg_parse_config_files(TALLOC_CTX *mem_ctx, unsigned int n_files, char **files_with_paths)
+{
+ TALLOC_CTX *tmp_ctx=talloc_new(NULL);
+ unsigned int ii=0;
+ json_t **jcfgs=NULL;
+
+ /* first allocate the jcfgs */
+ jcfgs=talloc_array(NULL, json_t *, n_files);
+ if (jcfgs==NULL) {
+ tr_crit("tr_parse_config_files: cannot allocate JSON structure array");
+ goto cleanup;
+ }
+ for (ii=0; ii<n_files; ii++) {
+ jcfgs[ii]=tr_cfg_parse_one_config_file(files_with_paths[ii]);
+ if (jcfgs[ii]==NULL) {
+ tr_err("tr_parse_config: Error parsing JSON in %s", files_with_paths[ii]);
+ tr_cfg_parse_free_jcfgs(ii, jcfgs); /* frees the JSON objects and the jcfgs array */
+ jcfgs=NULL;
+ goto cleanup;
+ }
+ }
+cleanup:
+ if (jcfgs)
+ talloc_steal(mem_ctx, jcfgs); /* give this to the caller's context if we succeeded */
+ talloc_free(tmp_ctx);
+ return jcfgs;
+}
+
+/* define a type for config parse functions */
+typedef TR_CFG_RC (TR_CFG_PARSE_FN)(TR_CFG *, json_t *);
+/**
+ * Helper function to parse a collection of JSON structures using a generic parse function.
+ *
+ * @param cfg Config structure to receive results
+ * @param jcfgs Pointer to an array of decoded JSON structures
+ * @param n_jcfg Number of JSON structures in the array
+ * @param parse_fn Function to apply
+ * @return TR_CFG_SUCCESS on success, _FAIL or an error code on failure
+ */
+static TR_CFG_RC tr_cfg_parse_helper(TR_CFG *cfg, unsigned int n_jcfg, json_t **jcfgs, TR_CFG_PARSE_FN parse_fn)
+{
+ size_t ii=0;
+ json_t *this_jcfg=NULL;
+ TR_CFG_RC ret=TR_CFG_ERROR;
+
+ if ((cfg==NULL) || (jcfgs==NULL) || (parse_fn==NULL))
return TR_CFG_ERROR;
- return TR_CFG_SUCCESS;
+ for (ii=0; ii<n_jcfg; ii++) {
+ this_jcfg=jcfgs[ii];
+ ret=parse_fn(cfg, this_jcfg);
+ if (ret!=TR_CFG_SUCCESS)
+ break;
+ }
+ return ret;
}
-/* Reads configuration files in config_dir ("" or "./" will use the current directory). */
-TR_CFG_RC tr_parse_config(TR_CFG_MGR *cfg_mgr, const char *config_dir, int n, struct dirent **cfg_files)
+
+/**
+ * Reads configuration files in config_dir ("" or "./" will use the current directory).
+ *
+ * @param cfg_mgr Configuration manager
+ * @param n_files Number of entries in cfg_files
+ * @param files_with_paths Array of filenames with path to load
+ * @return TR_CFG_SUCCESS on success, TR_CFG_ERROR or a more specific error on failure
+ */
+TR_CFG_RC tr_parse_config(TR_CFG_MGR *cfg_mgr, unsigned int n_files, char **files_with_paths)
{
TALLOC_CTX *tmp_ctx=talloc_new(NULL);
- char *file_with_path;
- int ii;
+ json_t **jcfgs=NULL;
TR_CFG_RC cfg_rc=TR_CFG_ERROR;
- if ((!cfg_mgr) || (!cfg_files) || (n<=0)) {
+ if ((!cfg_mgr) || (!files_with_paths)) {
cfg_rc=TR_CFG_BAD_PARAMS;
goto cleanup;
}
+ /* get a fresh config to fill in, freeing old one if needed */
if (cfg_mgr->new != NULL)
tr_cfg_free(cfg_mgr->new);
cfg_mgr->new=tr_cfg_new(tmp_ctx); /* belongs to the temporary context for now */
goto cleanup;
}
- cfg_mgr->new->peers=trp_ptable_new(cfg_mgr);
-
- /* Parse configuration information from each config file */
- for (ii=0; ii<n; ii++) {
- file_with_path=join_paths(tmp_ctx, config_dir, cfg_files[ii]->d_name); /* must free result with talloc_free */
- if(file_with_path == NULL) {
- tr_crit("tr_parse_config: error joining path.");
- cfg_rc=TR_CFG_NOMEM;
- goto cleanup;
- }
- tr_debug("tr_parse_config: Parsing %s.", cfg_files[ii]->d_name); /* print the filename without the path */
- cfg_rc=tr_cfg_parse_one_config_file(cfg_mgr->new, file_with_path);
- if (cfg_rc!=TR_CFG_SUCCESS) {
- tr_crit("tr_parse_config: Error parsing %s", file_with_path);
- goto cleanup;
- }
- talloc_free(file_with_path); /* done with filename */
+ /* first parse the json */
+ jcfgs=tr_cfg_parse_config_files(tmp_ctx, n_files, files_with_paths);
+ if (jcfgs==NULL) {
+ cfg_rc=TR_CFG_NOPARSE;
+ goto cleanup;
}
+ cfg_mgr->new->peers=trp_ptable_new(cfg_mgr); /* not sure why this isn't in cfg_mgr->new's context */
+
+ /* now run through the parsers on the JSON */
+ if ((TR_CFG_SUCCESS != (cfg_rc=tr_cfg_parse_helper(cfg_mgr->new, n_files, jcfgs, tr_cfg_parse_internal))) ||
+ (TR_CFG_SUCCESS != (cfg_rc=tr_cfg_parse_helper(cfg_mgr->new, n_files, jcfgs, tr_cfg_parse_local_orgs))) ||
+ (TR_CFG_SUCCESS != (cfg_rc=tr_cfg_parse_helper(cfg_mgr->new, n_files, jcfgs, tr_cfg_parse_peer_orgs))) ||
+ (TR_CFG_SUCCESS != (cfg_rc=tr_cfg_parse_helper(cfg_mgr->new, n_files, jcfgs, tr_cfg_parse_default_servers))) ||
+ (TR_CFG_SUCCESS != (cfg_rc=tr_cfg_parse_helper(cfg_mgr->new, n_files, jcfgs, tr_cfg_parse_comms))))
+ goto cleanup; /* cfg_rc was set above */
+
/* make sure we got a complete, consistent configuration */
if (TR_CFG_SUCCESS != tr_cfg_validate(cfg_mgr->new)) {
tr_err("tr_parse_config: Error: INVALID CONFIGURATION");
cfg_rc=TR_CFG_SUCCESS;
cleanup:
+ if (jcfgs!=NULL)
+ tr_cfg_parse_free_jcfgs(n_files, jcfgs);
talloc_free(tmp_ctx);
return cfg_rc;
}
* allocated array of pointers to struct dirent entries, as returned
* by scandir(). These can be freed with tr_free_config_file_list().
*/
-int tr_find_config_files (const char *config_dir, struct dirent ***cfg_files) {
+int tr_find_config_files(const char *config_dir, struct dirent ***cfg_files) {
int n = 0;
n = scandir(config_dir, cfg_files, is_cfg_file, alphasort);