Checkpoint commit: refactoring the request code in TIDS for better reuse
[trust_router.git] / common / tr_config.c
index bbbda1a..06d15a6 100644 (file)
@@ -41,7 +41,7 @@
 #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>
 #include <tr.h>
 #include <trust_router/trp.h>
 
+#if JANSSON_VERSION_HEX < 0x020500
+#include "jansson_iterators.h"
+#endif
+
 void tr_print_config (TR_CFG *cfg) {
   tr_notice("tr_print_config: Logging running trust router configuration.");
   tr_print_comms(cfg->ctable);
@@ -184,6 +188,9 @@ static TR_CFG_RC tr_cfg_parse_internal(TR_CFG *trc, json_t *jcfg)
   json_t *jcfgsettle = NULL;
   json_t *jroutesweep = NULL;
   json_t *jrouteupdate = NULL;
+  json_t *jtidreq_timeout = NULL;
+  json_t *jtidresp_numer = NULL;
+  json_t *jtidresp_denom = NULL;
   json_t *jrouteconnect = NULL;
 
   if ((!trc) || (!jcfg))
@@ -230,7 +237,7 @@ static TR_CFG_RC tr_cfg_parse_internal(TR_CFG *trc, json_t *jcfg)
     }
     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;
@@ -294,6 +301,42 @@ static TR_CFG_RC tr_cfg_parse_internal(TR_CFG *trc, json_t *jcfg)
       trc->internal->trp_update_interval=TR_DEFAULT_TRP_UPDATE_INTERVAL;
     }
 
+    if (NULL != (jtidreq_timeout = json_object_get(jint, "tid_request_timeout"))) {
+      if (json_is_number(jtidreq_timeout)) {
+        trc->internal->tid_req_timeout = json_integer_value(jtidreq_timeout);
+      } else {
+        tr_debug("tr_cfg_parse_internal: Parsing error, tid_request_timeout is not a number.");
+        return TR_CFG_NOPARSE;
+      }
+    } else {
+      /* if not configured, use the default */
+      trc->internal->tid_req_timeout=TR_DEFAULT_TID_REQ_TIMEOUT;
+    }
+
+    if (NULL != (jtidresp_numer = json_object_get(jint, "tid_response_numerator"))) {
+      if (json_is_number(jtidresp_numer)) {
+        trc->internal->tid_resp_numer = json_integer_value(jtidresp_numer);
+      } else {
+        tr_debug("tr_cfg_parse_internal: Parsing error, tid_response_numerator is not a number.");
+        return TR_CFG_NOPARSE;
+      }
+    } else {
+      /* if not configured, use the default */
+      trc->internal->tid_resp_numer=TR_DEFAULT_TID_RESP_NUMER;
+    }
+
+    if (NULL != (jtidresp_denom = json_object_get(jint, "tid_response_denominator"))) {
+      if (json_is_number(jtidresp_denom)) {
+        trc->internal->tid_resp_denom = json_integer_value(jtidresp_denom);
+      } else {
+        tr_debug("tr_cfg_parse_internal: Parsing error, tid_response_denominator is not a number.");
+        return TR_CFG_NOPARSE;
+      }
+    } else {
+      /* if not configured, use the default */
+      trc->internal->tid_resp_denom=TR_DEFAULT_TID_RESP_DENOM;
+    }
+
     if (NULL != (jlog = json_object_get(jint, "logging"))) {
       if (NULL != (jlogthres = json_object_get(jlog, "log_threshold"))) {
         if (json_is_string(jlogthres)) {
@@ -373,28 +416,31 @@ static TR_CONSTRAINT *tr_cfg_parse_one_constraint(TALLOC_CTX *mem_ctx, char *cty
 
 static TR_FILTER *tr_cfg_parse_one_filter(TALLOC_CTX *mem_ctx, json_t *jfilt, TR_FILTER_TYPE ftype, TR_CFG_RC *rc)
 {
-  TALLOC_CTX *tmp_ctx=talloc_new(NULL);
-  TR_FILTER *filt=NULL;
-  json_t *jfaction=NULL;
-  json_t *jfspecs=NULL;
-  json_t *jffield=NULL;
-  json_t *jfmatch=NULL;
-  json_t *jrc=NULL;
-  json_t *jdc=NULL;
-  TR_NAME *name=NULL;
-  int i=0, j=0;
-
-  *rc=TR_CFG_ERROR;
-
-  if ((jfilt==NULL) || (rc==NULL)) {
+  TALLOC_CTX *tmp_ctx = talloc_new(NULL);
+  TR_FILTER *filt = NULL;
+  json_t *jfaction = NULL;
+  json_t *jfline = NULL;
+  json_t *jfspecs = NULL;
+  json_t *this_jfspec = NULL;
+  json_t *jfield = NULL;
+  json_t *jmatch = NULL;
+  json_t *jrc = NULL;
+  json_t *jdc = NULL;
+  json_t *this_jmatch = NULL;
+  TR_NAME *name = NULL;
+  size_t i = 0, j = 0, k = 0;
+
+  *rc = TR_CFG_ERROR;
+
+  if ((jfilt == NULL) || (rc == NULL)) {
     tr_err("tr_cfg_parse_one_filter: null argument");
-    *rc=TR_CFG_BAD_PARAMS;
+    *rc = TR_CFG_BAD_PARAMS;
     goto cleanup;
   }
-    
-  if (NULL==(filt=tr_filter_new(tmp_ctx))) {
+
+  if (NULL == (filt = tr_filter_new(tmp_ctx))) {
     tr_err("tr_cfg_parse_one_filter: Out of memory.");
-    *rc=TR_CFG_NOMEM;
+    *rc = TR_CFG_NOMEM;
     goto cleanup;
   }
   tr_filter_set_type(filt, ftype);
@@ -402,139 +448,169 @@ static TR_FILTER *tr_cfg_parse_one_filter(TALLOC_CTX *mem_ctx, json_t *jfilt, TR
   /* make sure we have space to represent the filter */
   if (json_array_size(jfilt) > TR_MAX_FILTER_LINES) {
     tr_err("tr_cfg_parse_one_filter: Filter has too many lines, maximum of %d.", TR_MAX_FILTER_LINES);
-    *rc=TR_CFG_NOPARSE;
+    *rc = TR_CFG_NOPARSE;
     goto cleanup;
   }
 
   /* For each entry in the filter... */
-  for (i=0; i < json_array_size(jfilt); i++) {
-    if ((NULL==(jfaction=json_object_get(json_array_get(jfilt, i), "action"))) ||
+  json_array_foreach(jfilt, i, jfline) {
+    if ((NULL == (jfaction = json_object_get(jfline, "action"))) ||
         (!json_is_string(jfaction))) {
       tr_debug("tr_cfg_parse_one_filter: Error parsing filter action.");
-      *rc=TR_CFG_NOPARSE;
+      *rc = TR_CFG_NOPARSE;
       goto cleanup;
     }
-    if ((NULL==(jfspecs=json_object_get(json_array_get(jfilt, i), "specs"))) ||
+
+    if ((NULL == (jfspecs = json_object_get(jfline, "specs"))) ||
         (!json_is_array(jfspecs)) ||
-        (0==json_array_size(jfspecs))) {
+        (0 == json_array_size(jfspecs))) {
       tr_debug("tr_cfg_parse_one_filter: Error parsing filter specs.");
-      *rc=TR_CFG_NOPARSE;
+      *rc = TR_CFG_NOPARSE;
       goto cleanup;
     }
-  
+
     if (TR_MAX_FILTER_SPECS < json_array_size(jfspecs)) {
       tr_debug("tr_cfg_parse_one_filter: Filter has too many specs, maximimum of %d.", TR_MAX_FILTER_SPECS);
-      *rc=TR_CFG_NOPARSE;
+      *rc = TR_CFG_NOPARSE;
       goto cleanup;
     }
 
-    if (NULL==(filt->lines[i]=tr_fline_new(filt))) {
-      tr_debug("tr_cfg_parse_one_filter: Out of memory allocating filter line %d.", i+1);
-      *rc=TR_CFG_NOMEM;
+    if (NULL == (filt->lines[i] = tr_fline_new(filt))) {
+      tr_debug("tr_cfg_parse_one_filter: Out of memory allocating filter line %d.", i + 1);
+      *rc = TR_CFG_NOMEM;
       goto cleanup;
     }
 
     if (!strcmp(json_string_value(jfaction), "accept")) {
-      filt->lines[i]->action=TR_FILTER_ACTION_ACCEPT;
-    }
-    else if (!strcmp(json_string_value(jfaction), "reject")) {
-      filt->lines[i]->action=TR_FILTER_ACTION_REJECT;
-    }
-    else {
-      tr_debug("tr_cfg_parse_one_filter: Error parsing filter action, unknown action' %s'.", json_string_value(jfaction));
-      *rc=TR_CFG_NOPARSE;
+      filt->lines[i]->action = TR_FILTER_ACTION_ACCEPT;
+    } else if (!strcmp(json_string_value(jfaction), "reject")) {
+      filt->lines[i]->action = TR_FILTER_ACTION_REJECT;
+    } else {
+      tr_debug("tr_cfg_parse_one_filter: Error parsing filter action, unknown action' %s'.",
+               json_string_value(jfaction));
+      *rc = TR_CFG_NOPARSE;
       goto cleanup;
     }
 
-    if (NULL!=(jrc=json_object_get(json_array_get(jfilt, i), "realm_constraints"))) {
+    if (NULL != (jrc = json_object_get(jfline, "realm_constraints"))) {
       if (!json_is_array(jrc)) {
         tr_err("tr_cfg_parse_one_filter: cannot parse realm_constraints, not an array.");
-        *rc=TR_CFG_NOPARSE;
+        *rc = TR_CFG_NOPARSE;
         goto cleanup;
-      } else if (json_array_size(jrc)>TR_MAX_CONST_MATCHES) {
+      } else if (json_array_size(jrc) > TR_MAX_CONST_MATCHES) {
         tr_err("tr_cfg_parse_one_filter: realm_constraints has too many entries, maximum of %d.",
                TR_MAX_CONST_MATCHES);
-        *rc=TR_CFG_NOPARSE;
+        *rc = TR_CFG_NOPARSE;
         goto cleanup;
-      } else if (json_array_size(jrc)>0) {
+      } else if (json_array_size(jrc) > 0) {
         /* ok we actually have entries to process */
-        if (NULL==(filt->lines[i]->realm_cons=tr_cfg_parse_one_constraint(filt->lines[i], "realm", jrc, rc))) {
+        if (NULL == (filt->lines[i]->realm_cons = tr_cfg_parse_one_constraint(filt->lines[i], "realm", jrc, rc))) {
           tr_debug("tr_cfg_parse_one_filter: Error parsing realm constraint");
-          *rc=TR_CFG_NOPARSE;
+          *rc = TR_CFG_NOPARSE;
           goto cleanup;
         }
       }
     }
 
-    if (NULL!=(jdc=json_object_get(json_array_get(jfilt, i), "domain_constraints"))) {
+    if (NULL != (jdc = json_object_get(jfline, "domain_constraints"))) {
       if (!json_is_array(jdc)) {
         tr_err("tr_cfg_parse_one_filter: cannot parse domain_constraints, not an array.");
-        *rc=TR_CFG_NOPARSE;
+        *rc = TR_CFG_NOPARSE;
         goto cleanup;
-      } else if (json_array_size(jdc)>TR_MAX_CONST_MATCHES) {
+      } else if (json_array_size(jdc) > TR_MAX_CONST_MATCHES) {
         tr_err("tr_cfg_parse_one_filter: domain_constraints has too many entries, maximum of %d.",
                TR_MAX_CONST_MATCHES);
-        *rc=TR_CFG_NOPARSE;
+        *rc = TR_CFG_NOPARSE;
         goto cleanup;
-      } else if (json_array_size(jdc)>0) {
-        if (NULL==(filt->lines[i]->domain_cons=tr_cfg_parse_one_constraint(filt->lines[i], "domain", jdc, rc))) {
+      } else if (json_array_size(jdc) > 0) {
+        if (NULL == (filt->lines[i]->domain_cons = tr_cfg_parse_one_constraint(filt->lines[i], "domain", jdc, rc))) {
           tr_debug("tr_cfg_parse_one_filter: Error parsing domain constraint");
-          *rc=TR_CFG_NOPARSE;
+          *rc = TR_CFG_NOPARSE;
           goto cleanup;
         }
       }
     }
 
     /*For each filter spec within the filter line... */
-    for (j=0; j <json_array_size(jfspecs); j++) {
-      if ((NULL==(jffield=json_object_get(json_array_get(jfspecs, j), "field"))) ||
-          (!json_is_string(jffield))) {
-        tr_debug("tr_cfg_parse_one_filter: Error parsing filter: missing field for filer spec %d, filter line %d.", i, j);
-        *rc=TR_CFG_NOPARSE;
+    json_array_foreach(jfspecs, j, this_jfspec) {
+      if ((NULL == (jfield = json_object_get(this_jfspec, "field"))) ||
+          (!json_is_string(jfield))) {
+        tr_debug("tr_cfg_parse_one_filter: Error parsing filter: missing field for filer spec %d, filter line %d.", i,
+                 j);
+        *rc = TR_CFG_NOPARSE;
         goto cleanup;
       }
 
       /* check that we have a match attribute */
-      if (NULL==(jfmatch=json_object_get(json_array_get(jfspecs, j), "match"))) {
-        tr_debug("tr_cfg_parse_one_filter: Error parsing filter: missing match for filer spec %d, filter line %d.", i, j);
-        *rc=TR_CFG_NOPARSE;
+      if (NULL == (jmatch = json_object_get(this_jfspec, "match"))) {
+        tr_debug("tr_cfg_parse_one_filter: Error parsing filter: missing match for filer spec %d, filter line %d.", i,
+                 j);
+        *rc = TR_CFG_NOPARSE;
         goto cleanup;
       }
 
-      /* check that match is a string */
-      if (!json_is_string(jfmatch)) {
-        tr_debug("tr_cfg_parse_one_filter: Error parsing filter: match not a string for filter spec %d, filter line %d.", i, j);
-        *rc=TR_CFG_NOPARSE;
+      /* check that match is a string or an array */
+      if ((!json_is_string(jmatch)) && (!json_is_array(jmatch))) {
+        tr_debug(
+            "tr_cfg_parse_one_filter: Error parsing filter: match not a string or array for filter spec %d, filter line %d.",
+            i, j);
+        *rc = TR_CFG_NOPARSE;
         goto cleanup;
       }
 
       /* allocate the filter spec */
-      if (NULL==(filt->lines[i]->specs[j]=tr_fspec_new(filt->lines[i]))) {
+      if (NULL == (filt->lines[i]->specs[j] = tr_fspec_new(filt->lines[i]))) {
         tr_debug("tr_cfg_parse_one_filter: Out of memory.");
-        *rc=TR_CFG_NOMEM;
+        *rc = TR_CFG_NOMEM;
         goto cleanup;
       }
 
       /* fill in the field */
-      if (NULL==(filt->lines[i]->specs[j]->field=tr_new_name(json_string_value(jffield)))) {
+      if (NULL == (filt->lines[i]->specs[j]->field = tr_new_name(json_string_value(jfield)))) {
         tr_debug("tr_cfg_parse_one_filter: Out of memory.");
-        *rc=TR_CFG_NOMEM;
+        *rc = TR_CFG_NOMEM;
         goto cleanup;
       }
 
       /* fill in the matches */
-      if (NULL==(name=tr_new_name(json_string_value(jfmatch)))) {
-        tr_debug("tr_cfg_parse_one_filter: Out of memory.");
-        *rc=TR_CFG_NOMEM;
+      if (json_is_string(jmatch)) {
+        if (NULL == (name = tr_new_name(json_string_value(jmatch)))) {
+          tr_debug("tr_cfg_parse_one_filter: Out of memory.");
+          *rc = TR_CFG_NOMEM;
+          goto cleanup;
+        }
+        tr_fspec_add_match(filt->lines[i]->specs[j], name);
+      } else {
+        /* jmatch is an array (we checked earlier) */
+        json_array_foreach(jmatch, k, this_jmatch) {
+          if (NULL == (name = tr_new_name(json_string_value(this_jmatch)))) {
+            tr_debug("tr_cfg_parse_one_filter: Out of memory.");
+            *rc = TR_CFG_NOMEM;
+            goto cleanup;
+          }
+          tr_fspec_add_match(filt->lines[i]->specs[j], name);
+        }
+      }
+      if (!tr_filter_validate_spec_field(ftype, filt->lines[i]->specs[j])){
+        tr_debug("tr_cfg_parse_one_filter: Invalid filter field \"%.*s\" for %s filter, spec %d, filter %d.",
+                 filt->lines[i]->specs[j]->field->len,
+                 filt->lines[i]->specs[j]->field->buf,
+                 tr_filter_type_to_string(filt->type),
+                 i, j);
+        *rc = TR_CFG_ERROR;
         goto cleanup;
       }
-      tr_fspec_set_match(filt->lines[i]->specs[j], name);
     }
   }
-  *rc=TR_CFG_SUCCESS;
-  talloc_steal(mem_ctx, filt);
-  
+
+  /* check that the filter is valid */
+  if (!tr_filter_validate(filt)) {
+    *rc = TR_CFG_ERROR;
+  } else {
+    *rc = TR_CFG_SUCCESS;
+    talloc_steal(mem_ctx, filt);
+  }
+
  cleanup:
   talloc_free(tmp_ctx);
   if (*rc!=TR_CFG_SUCCESS)
@@ -542,11 +618,14 @@ static TR_FILTER *tr_cfg_parse_one_filter(TALLOC_CTX *mem_ctx, json_t *jfilt, TR
   return filt;
 }
 
-static TR_FILTER *tr_cfg_parse_filters(TALLOC_CTX *mem_ctx, json_t *jfilts, TR_CFG_RC *rc)
+static TR_FILTER_SET *tr_cfg_parse_filters(TALLOC_CTX *mem_ctx, json_t *jfilts, TR_CFG_RC *rc)
 {
   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
   json_t *jfilt;
+  const char *filt_label=NULL;
   TR_FILTER *filt=NULL;
+  TR_FILTER_SET *filt_set=NULL;
+  TR_FILTER_TYPE filt_type=TR_FILTER_TYPE_UNKNOWN;
 
   *rc=TR_CFG_ERROR;
 
@@ -556,32 +635,52 @@ static TR_FILTER *tr_cfg_parse_filters(TALLOC_CTX *mem_ctx, json_t *jfilts, TR_C
     goto cleanup;
   }
 
-  jfilt=json_object_get(jfilts, "tid_inbound");
-  if (jfilt!=NULL) {
-    filt=tr_cfg_parse_one_filter(tmp_ctx, jfilt, TR_FILTER_TYPE_TID_INCOMING, rc);
-    if (*rc!=TR_CFG_SUCCESS) {
-      tr_debug("tr_cfg_parse_filters: Error parsing tid_inbound filter.");
-      *rc=TR_CFG_NOPARSE;
+  filt_set=tr_filter_set_new(tmp_ctx);
+  if (filt_set==NULL) {
+    tr_debug("tr_cfg_parse_filters: Unable to allocate filter set.");
+    *rc = TR_CFG_NOMEM;
+    goto cleanup;
+  }
+
+  json_object_foreach(jfilts, filt_label, jfilt) {
+    /* check that we got a filter */
+    if (jfilt == NULL) {
+      tr_debug("tr_cfg_parse_filters: Definition for %s filter is missing.", filt_label);
+      *rc = TR_CFG_NOPARSE;
+      goto cleanup;
+    }
+
+    /* check that we recognize the filter type */
+    filt_type=tr_filter_type_from_string(filt_label);
+    if (filt_type==TR_FILTER_TYPE_UNKNOWN) {
+      tr_debug("tr_cfg_parse_filters: Unrecognized filter (%s) defined.", filt_label);
+      *rc = TR_CFG_NOPARSE;
+      goto cleanup;
+    }
+
+    /* finally, parse the filter */
+    tr_debug("tr_cfg_parse_filters: Found %s filter.", filt_label);
+    filt = tr_cfg_parse_one_filter(tmp_ctx, jfilt, filt_type, rc);
+    tr_filter_set_add(filt_set, filt);
+    if (*rc != TR_CFG_SUCCESS) {
+      tr_debug("tr_cfg_parse_filters: Error parsing %s filter.", filt_label);
+      *rc = TR_CFG_NOPARSE;
       goto cleanup;
     }
-  } else {
-    tr_debug("tr_cfg_parse_filters: Unknown filter types in filter block.");
-    *rc=TR_CFG_NOPARSE;
-    goto cleanup;
   }
-  
+
   *rc=TR_CFG_SUCCESS;
 
  cleanup:
   if (*rc==TR_CFG_SUCCESS)
-    talloc_steal(mem_ctx, filt);
-  else if (filt!=NULL) {
-    talloc_free(filt);
-    filt=NULL;
+    talloc_steal(mem_ctx, filt_set);
+  else if (filt_set!=NULL) {
+    talloc_free(filt_set);
+    filt_set=NULL;
   }
 
   talloc_free(tmp_ctx);
-  return filt;
+  return filt_set;
 }
 
 static TR_AAA_SERVER *tr_cfg_parse_one_aaa_server(TALLOC_CTX *mem_ctx, json_t *jaddr, TR_CFG_RC *rc)
@@ -1052,10 +1151,11 @@ static TR_GSS_NAMES *tr_cfg_parse_gss_names(TALLOC_CTX *mem_ctx, json_t *jgss_na
 }
 
 /* default filter accepts realm and *.realm */
-static TR_FILTER *tr_cfg_default_filter(TALLOC_CTX *mem_ctx, TR_NAME *realm, TR_CFG_RC *rc)
+static TR_FILTER_SET *tr_cfg_default_filters(TALLOC_CTX *mem_ctx, TR_NAME *realm, TR_CFG_RC *rc)
 {
   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
   TR_FILTER *filt=NULL;
+  TR_FILTER_SET *filt_set=NULL;
   TR_CONSTRAINT *cons=NULL;
   TR_NAME *name=NULL;
   TR_NAME *n_prefix=tr_new_name("*.");
@@ -1066,7 +1166,7 @@ static TR_FILTER *tr_cfg_default_filter(TALLOC_CTX *mem_ctx, TR_NAME *realm, TR_
   
 
   if ((realm==NULL) || (rc==NULL)) {
-    tr_debug("tr_cfg_default_filter: invalid arguments.");
+    tr_debug("tr_cfg_default_filters: invalid arguments.");
     if (rc!=NULL)
       *rc=TR_CFG_BAD_PARAMS;
     goto cleanup;
@@ -1077,21 +1177,21 @@ static TR_FILTER *tr_cfg_default_filter(TALLOC_CTX *mem_ctx, TR_NAME *realm, TR_
       (n_rp_realm_2==NULL) ||
       (n_domain==NULL) ||
       (n_realm==NULL)) {
-    tr_debug("tr_cfg_default_filter: unable to allocate names.");
+    tr_debug("tr_cfg_default_filters: unable to allocate names.");
     *rc=TR_CFG_NOMEM;
     goto cleanup;
   }
 
   filt=tr_filter_new(tmp_ctx);
   if (filt==NULL) {
-    tr_debug("tr_cfg_default_filter: could not allocate filter.");
+    tr_debug("tr_cfg_default_filters: could not allocate filter.");
     *rc=TR_CFG_NOMEM;
     goto cleanup;
   }
-  tr_filter_set_type(filt, TR_FILTER_TYPE_TID_INCOMING);
+  tr_filter_set_type(filt, TR_FILTER_TYPE_TID_INBOUND);
   filt->lines[0]=tr_fline_new(filt);
   if (filt->lines[0]==NULL) {
-    tr_debug("tr_cfg_default_filter: could not allocate filter line.");
+    tr_debug("tr_cfg_default_filters: could not allocate filter line.");
     *rc=TR_CFG_NOMEM;
     goto cleanup;
   }
@@ -1103,11 +1203,11 @@ static TR_FILTER *tr_cfg_default_filter(TALLOC_CTX *mem_ctx, TR_NAME *realm, TR_
 
   name=tr_dup_name(realm);
   if (name==NULL) {
-    tr_debug("tr_cfg_default_filter: could not allocate realm name.");
+    tr_debug("tr_cfg_default_filters: could not allocate realm name.");
     *rc=TR_CFG_NOMEM;
     goto cleanup;
   }
-  tr_fspec_set_match(filt->lines[0]->specs[0], name);
+  tr_fspec_add_match(filt->lines[0]->specs[0], name);
   name=NULL; /* we no longer own the name */
 
   /* now do the wildcard name */
@@ -1116,17 +1216,17 @@ static TR_FILTER *tr_cfg_default_filter(TALLOC_CTX *mem_ctx, TR_NAME *realm, TR_
   n_rp_realm_2=NULL; /* we don't own this name any more */
 
   if (NULL==(name=tr_name_cat(n_prefix, realm))) {
-    tr_debug("tr_cfg_default_filter: could not allocate wildcard realm name.");
+    tr_debug("tr_cfg_default_filters: could not allocate wildcard realm name.");
     *rc=TR_CFG_NOMEM;
     goto cleanup;
   }
 
-  tr_fspec_set_match(filt->lines[0]->specs[1], name);
+  tr_fspec_add_match(filt->lines[0]->specs[1], name);
   name=NULL; /* we no longer own the name */
 
   /* domain constraint */
   if (NULL==(cons=tr_constraint_new(filt->lines[0]))) {
-    tr_debug("tr_cfg_default_filter: could not allocate domain constraint.");
+    tr_debug("tr_cfg_default_filters: could not allocate domain constraint.");
     *rc=TR_CFG_NOMEM;
     goto cleanup;
   }
@@ -1135,14 +1235,14 @@ static TR_FILTER *tr_cfg_default_filter(TALLOC_CTX *mem_ctx, TR_NAME *realm, TR_
   n_domain=NULL; /* belongs to the constraint now */
   name=tr_dup_name(realm);
   if (name==NULL) {
-    tr_debug("tr_cfg_default_filter: could not allocate realm name for domain constraint.");
+    tr_debug("tr_cfg_default_filters: could not allocate realm name for domain constraint.");
     *rc=TR_CFG_NOMEM;
     goto cleanup;
   }
   cons->matches[0]=name;
   name=tr_name_cat(n_prefix, realm);
   if (name==NULL) {
-    tr_debug("tr_cfg_default_filter: could not allocate wildcard realm name for domain constraint.");
+    tr_debug("tr_cfg_default_filters: could not allocate wildcard realm name for domain constraint.");
     *rc=TR_CFG_NOMEM;
     goto cleanup;
   }
@@ -1153,7 +1253,7 @@ static TR_FILTER *tr_cfg_default_filter(TALLOC_CTX *mem_ctx, TR_NAME *realm, TR_
 
   /* realm constraint */
   if (NULL==(cons=tr_constraint_new(filt->lines[0]))) {
-    tr_debug("tr_cfg_default_filter: could not allocate realm constraint.");
+    tr_debug("tr_cfg_default_filters: could not allocate realm constraint.");
     *rc=TR_CFG_NOMEM;
     goto cleanup;
   }
@@ -1162,14 +1262,14 @@ static TR_FILTER *tr_cfg_default_filter(TALLOC_CTX *mem_ctx, TR_NAME *realm, TR_
   n_realm=NULL; /* belongs to the constraint now */
   name=tr_dup_name(realm);
   if (name==NULL) {
-    tr_debug("tr_cfg_default_filter: could not allocate realm name for realm constraint.");
+    tr_debug("tr_cfg_default_filters: could not allocate realm name for realm constraint.");
     *rc=TR_CFG_NOMEM;
     goto cleanup;
   }
   cons->matches[0]=name;
   name=tr_name_cat(n_prefix, realm);
   if (name==NULL) {
-    tr_debug("tr_cfg_default_filter: could not allocate wildcard realm name for realm constraint.");
+    tr_debug("tr_cfg_default_filters: could not allocate wildcard realm name for realm constraint.");
     *rc=TR_CFG_NOMEM;
     goto cleanup;
   }
@@ -1177,7 +1277,15 @@ static TR_FILTER *tr_cfg_default_filter(TALLOC_CTX *mem_ctx, TR_NAME *realm, TR_
   name=NULL;
   filt->lines[0]->realm_cons=cons;
 
-  talloc_steal(mem_ctx, filt);
+  /* put the filter in a set */
+  filt_set=tr_filter_set_new(tmp_ctx);
+  if ((filt_set==NULL)||(0!=tr_filter_set_add(filt_set, filt))) {
+    tr_debug("tr_cfg_default_filters: could not allocate filter set.");
+    *rc=TR_CFG_NOMEM;
+    goto cleanup;
+  }
+  talloc_steal(mem_ctx, filt_set);
+
 cleanup:
   talloc_free(tmp_ctx);
 
@@ -1197,7 +1305,7 @@ cleanup:
   if (name!=NULL)
     tr_free_name(name);
 
-  return filt;
+  return filt_set;
 }
 
 /* parses rp client */
@@ -1206,7 +1314,7 @@ static TR_RP_CLIENT *tr_cfg_parse_one_rp_client(TALLOC_CTX *mem_ctx, json_t *jre
   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
   TR_RP_CLIENT *client=NULL;
   TR_CFG_RC call_rc=TR_CFG_ERROR;
-  TR_FILTER *new_filt=NULL;
+  TR_FILTER_SET *new_filts=NULL;
   TR_NAME *realm=NULL;
   json_t *jfilt=NULL;
   json_t *jrealm_id=NULL;
@@ -1251,7 +1359,7 @@ static TR_RP_CLIENT *tr_cfg_parse_one_rp_client(TALLOC_CTX *mem_ctx, json_t *jre
   /* parse filters */
   jfilt=json_object_get(jrealm, "filters");
   if (jfilt!=NULL) {
-    new_filt=tr_cfg_parse_filters(tmp_ctx, jfilt, &call_rc);
+    new_filts=tr_cfg_parse_filters(tmp_ctx, jfilt, &call_rc);
     if (call_rc!=TR_CFG_SUCCESS) {
       tr_err("tr_cfg_parse_one_rp_client: could not parse filters.");
       *rc=TR_CFG_NOPARSE;
@@ -1259,7 +1367,7 @@ static TR_RP_CLIENT *tr_cfg_parse_one_rp_client(TALLOC_CTX *mem_ctx, json_t *jre
     }
   } else {
     tr_debug("tr_cfg_parse_one_rp_client: no filters specified, using default filters.");
-    new_filt=tr_cfg_default_filter(tmp_ctx, realm, &call_rc);
+    new_filts= tr_cfg_default_filters(tmp_ctx, realm, &call_rc);
     if (call_rc!=TR_CFG_SUCCESS) {
       tr_err("tr_cfg_parse_one_rp_client: could not set default filters.");
       *rc=TR_CFG_NOPARSE;
@@ -1267,24 +1375,24 @@ static TR_RP_CLIENT *tr_cfg_parse_one_rp_client(TALLOC_CTX *mem_ctx, json_t *jre
     }
   }
 
-  tr_rp_client_set_filter(client, new_filt);
+  tr_rp_client_set_filters(client, new_filts);
   *rc=TR_CFG_SUCCESS;
 
   cleanup:
   if (realm!=NULL)
     tr_free_name(realm);
 
-    if (*rc==TR_CFG_SUCCESS)
-      talloc_steal(mem_ctx, client);
-    else {
-      talloc_free(client);
-      client=NULL;
-    }
-
-    talloc_free(tmp_ctx);
-    return client;
+  if (*rc==TR_CFG_SUCCESS)
+    talloc_steal(mem_ctx, client);
+  else {
+    talloc_free(client);
+    client=NULL;
   }
 
+  talloc_free(tmp_ctx);
+  return client;
+}
+
   /* Determine whether the realm is an RP realm */
 static int tr_cfg_is_rp_realm(json_t *jrealm)
 {
@@ -1352,60 +1460,6 @@ static TR_NAME *tr_cfg_parse_org_name(TALLOC_CTX *mem_ctx, json_t *j_org, TR_CFG
   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);
@@ -1472,7 +1526,7 @@ cleanup:
 static TR_CFG_RC tr_cfg_parse_local_orgs(TR_CFG *trc, json_t *jcfg)
 {
   json_t *jlocorgs=NULL;
-  int ii=0;
+  size_t ii=0;
 
   jlocorgs=json_object_get(jcfg, "local_organizations");
   if (jlocorgs==NULL)
@@ -1499,13 +1553,16 @@ static TR_CFG_RC tr_cfg_parse_one_peer_org(TR_CFG *trc, json_t *jporg)
   json_t *jhost=NULL;
   json_t *jport=NULL;
   json_t *jgss=NULL;
+  json_t *jfilt=NULL;
   TRP_PEER *new_peer=NULL;
   TR_GSS_NAMES *names=NULL;
+  TR_FILTER_SET *filt_set=NULL;
   TR_CFG_RC rc=TR_CFG_ERROR;
 
   jhost=json_object_get(jporg, "hostname");
   jport=json_object_get(jporg, "port");
   jgss=json_object_get(jporg, "gss_names");
+  jfilt=json_object_get(jporg, "filters");
 
   if ((jhost==NULL) || (!json_is_string(jhost))) {
     tr_err("tr_cfg_parse_one_peer_org: hostname not specified or not a string.");
@@ -1519,13 +1576,19 @@ static TR_CFG_RC tr_cfg_parse_one_peer_org(TR_CFG *trc, json_t *jporg)
     rc=TR_CFG_NOPARSE;
     goto cleanup;
   }
-  
+
   if ((jgss==NULL) || (!json_is_array(jgss))) {
     tr_err("tr_cfg_parse_one_peer_org: gss_names not specified or not an array.");
     rc=TR_CFG_NOPARSE;
     goto cleanup;
   }
 
+  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;
+  }
+
   new_peer=trp_peer_new(tmp_ctx);
   if (new_peer==NULL) {
     tr_err("tr_cfg_parse_one_peer_org: could not allocate new peer.");
@@ -1533,7 +1596,7 @@ static TR_CFG_RC tr_cfg_parse_one_peer_org(TR_CFG *trc, json_t *jporg)
     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
@@ -1547,6 +1610,16 @@ static TR_CFG_RC tr_cfg_parse_one_peer_org(TR_CFG *trc, json_t *jporg)
   }
   trp_peer_set_gss_names(new_peer, names);
 
+  if (jfilt) {
+    filt_set=tr_cfg_parse_filters(tmp_ctx, jfilt, &rc);
+    if (rc!=TR_CFG_SUCCESS) {
+      tr_err("tr_cfg_parse_one_peer_org: unable to parse filters.");
+      rc=TR_CFG_NOPARSE;
+      goto cleanup;
+    }
+    trp_peer_set_filters(new_peer, filt_set);
+  }
+
   /* success! */
   trp_ptable_add(trc->peers, new_peer);
   rc=TR_CFG_SUCCESS;
@@ -1615,7 +1688,9 @@ static TR_CFG_RC tr_cfg_parse_default_servers (TR_CFG *trc, json_t *jcfg)
 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) ||
@@ -1625,13 +1700,17 @@ static void tr_cfg_parse_comm_idps(TR_CFG *trc, json_t *jidps, TR_COMM *comm, TR
     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;
     }
@@ -1791,16 +1870,26 @@ static TR_COMM *tr_cfg_parse_one_comm (TALLOC_CTX *mem_ctx, TR_CFG *trc, json_t
     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);
+      }
     }
   }
 
@@ -1876,13 +1965,6 @@ TR_CFG_RC tr_cfg_validate(TR_CFG *trc)
   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",
@@ -1891,21 +1973,28 @@ static void tr_cfg_log_json_error(const char *label, json_error_t *rc)
           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;
   json_error_t rc;
 
   if (NULL==(jcfg=json_load_file(file_with_path, 
-                                 JSON_DISABLE_EOF_CHECK, &rc))) {
+                                 JSON_DISABLE_EOF_CHECK|JSON_REJECT_DUPLICATES, &rc))) {
     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'.",
@@ -1914,29 +2003,109 @@ TR_CFG_RC tr_cfg_parse_one_config_file(TR_CFG *cfg, const char *file_with_path)
     }
   }
 
-  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 */
@@ -1945,25 +2114,23 @@ TR_CFG_RC tr_parse_config(TR_CFG_MGR *cfg_mgr, const char *config_dir, int n, st
     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");
@@ -1976,6 +2143,8 @@ TR_CFG_RC tr_parse_config(TR_CFG_MGR *cfg_mgr, const char *config_dir, int n, st
   cfg_rc=TR_CFG_SUCCESS;
 
 cleanup:
+  if (jcfgs!=NULL)
+    tr_cfg_parse_free_jcfgs(n_files, jcfgs);
   talloc_free(tmp_ctx);
   return cfg_rc;
 }
@@ -2054,7 +2223,7 @@ static int is_cfg_file(const struct dirent *dent) {
  * 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);