Miscellaneous minor code cleanup for MRW's review comments
[trust_router.git] / common / tr_config.c
index 66eda35..2ddda12 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2012, JANET(UK)
+ * Copyright (c) 2012-2018, JANET(UK)
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
 #include <dirent.h>
 #include <talloc.h>
 
+#include <tr_cfgwatch.h>
+#include <tr_comm.h>
 #include <tr_config.h>
-#include <tr.h>
+#include <tr_gss_names.h>
+#include <tr_debug.h>
 #include <tr_filter.h>
 #include <trust_router/tr_constraint.h>
+#include <tr_idp.h>
+#include <tr.h>
+#include <trust_router/trp.h>
 
-void tr_print_config (FILE *stream, TR_CFG *cfg) {
-  fprintf(stream, "tr_print_config: Not yet implemented.\n");
-  return;
-}
-
-void tr_cfg_free (TR_CFG *cfg) {
-  talloc_free(cfg);
-  return;
-}
-
-TR_CFG_RC tr_apply_new_config (TR_INSTANCE *tr) {
-  if (!tr)
-    return TR_CFG_BAD_PARAMS;
-
-  if (tr->active_cfg)
-    tr_cfg_free(tr->active_cfg);
+#if JANSSON_VERSION_HEX < 0x020500
+#include "jansson_iterators.h"
+#endif
 
-  tr->active_cfg = tr->new_cfg;
-  return TR_CFG_SUCCESS;
+void tr_print_config (TR_CFG *cfg) {
+  tr_notice("tr_print_config: Logging running trust router configuration.");
+  tr_print_comms(cfg->ctable);
 }
 
-static TR_CFG_RC tr_cfg_parse_internal (TR_CFG *trc, json_t *jcfg) {
-  json_t *jint = NULL;
-  json_t *jmtd = NULL;
-  json_t *jtp = NULL;
-  json_t *jhname = NULL;
-
-  if ((!trc) || (!jcfg))
-    return TR_CFG_BAD_PARAMS;
-
-  if (NULL == trc->internal) {
-    if (NULL == (trc->internal = talloc(trc, TR_CFG_INTERNAL)))
-      return TR_CFG_NOMEM;
-
-    memset(trc->internal, 0, sizeof(TR_CFG_INTERNAL));
-  }
-
-  if (NULL != (jint = json_object_get(jcfg, "tr_internal"))) {
-    if (NULL != (jmtd = json_object_get(jint, "max_tree_depth"))) {
-      if (json_is_number(jmtd)) {
-       trc->internal->max_tree_depth = json_integer_value(jmtd);
-      } else {
-       fprintf(stderr,"tr_cfg_parse_internal: Parsing error, max_tree_depth is not a number.\n");
-       return TR_CFG_NOPARSE;
-      }
-    } else {
-      /* If not configured, use the default */
-      trc->internal->max_tree_depth = TR_DEFAULT_MAX_TREE_DEPTH;
-    }
-    if (NULL != (jtp = json_object_get(jint, "tids_port"))) {
-      if (json_is_number(jtp)) {
-       trc->internal->tids_port = json_integer_value(jtp);
-      } else {
-       fprintf(stderr,"tr_cfg_parse_internal: Parsing error, port is not a number.\n");
-       return TR_CFG_NOPARSE;
-      }
-    } else {
-      /* If not configured, use the default */
-      trc->internal->tids_port = TR_DEFAULT_TIDS_PORT;
-    }
-    if (NULL != (jhname = json_object_get(jint, "hostname"))) {
-      if (json_is_string(jhname)) {
-       trc->internal->hostname = json_string_value(jhname);
-      } else {
-       fprintf(stderr,"tr_cfg_parse_internal: Parsing error, hostname is not a string.\n");
-       return TR_CFG_NOPARSE;
-      }
-    }
-    fprintf(stderr, "tr_cfg_parse_internal: Internal config parsed.\n");
-    return TR_CFG_SUCCESS;
-  }
-  return TR_CFG_SUCCESS;
-}
-
-static TR_CONSTRAINT *tr_cfg_parse_one_constraint (TR_CFG *trc, char *ctype, json_t *jc, TR_CFG_RC *rc)
+void tr_print_comms (TR_COMM_TABLE *ctab)
 {
-  TR_CONSTRAINT *cons;
-  int i;
-
-  if ((!trc) || (!ctype) || (!jc) || (!rc) ||
-      (!json_is_array(jc)) ||
-      (0 >= json_array_size(jc)) ||
-      (TR_MAX_CONST_MATCHES < json_array_size(jc)) ||
-      (!json_is_string(json_array_get(jc, 0)))) {
-    fprintf(stderr, "tr_cfg_parse_one_constraint: config error.\n");
-    *rc = TR_CFG_NOPARSE;
-    return NULL;
-  }
-
-  if (NULL == (cons = talloc(trc, TR_CONSTRAINT))) {
-    fprintf(stderr, "tr_cfg_parse_one_constraint: Out of memory (cons).\n");
-    *rc = TR_CFG_NOMEM;
-    return NULL;
-  }
+  TR_COMM *comm = NULL;
 
-  memset(cons, 0, sizeof(TR_CONSTRAINT));
+  for (comm = ctab->comms; NULL != comm; comm = comm->next) {
+    tr_notice("tr_print_config: Community %s:", comm->id->buf);
 
-  if (NULL == (cons->type = tr_new_name(ctype))) {
-    fprintf(stderr, "tr_cfg_parse_one_constraint: Out of memory (type).\n");
-    *rc = TR_CFG_NOMEM;
-    return NULL;
-  }
+    tr_notice("tr_print_config:  - Member IdPs:");
+    tr_print_comm_idps(ctab, comm);
 
-  for (i = 0; i < json_array_size(jc); i++) {
-    cons->matches[i] = tr_new_name((char *)(json_string_value(json_array_get(jc, i))));
+    tr_notice("tr_print_config:  - Member RPs:");
+    tr_print_comm_rps(ctab, comm);
   }
-
-  return cons;
 }
 
-static TR_FILTER *tr_cfg_parse_one_filter (TR_CFG *trc, json_t *jfilt, TR_CFG_RC *rc)
+void tr_print_comm_idps(TR_COMM_TABLE *ctab, TR_COMM *comm)
 {
-  TR_FILTER *filt = NULL;
-  json_t *jftype = NULL;
-  json_t *jfls = 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;
-  int i = 0, j = 0;
-
-  if ((NULL == (jftype = json_object_get(jfilt, "type"))) ||
-      (!json_is_string(jftype))) {
-    fprintf(stderr, "tr_cfg_parse_one_filter: Error parsing filter type.\n");
-    *rc = TR_CFG_NOPARSE;
-    return NULL;
-  }
-
-  if ((NULL == (jfls = json_object_get(jfilt, "filter_lines"))) ||
-      (!json_is_array(jfls))) {
-    fprintf(stderr, "tr_cfg_parse_one_filter: Error parsing filter type.\n");
-    *rc = TR_CFG_NOPARSE;
-    return NULL;
-  }
-
-  if (TR_MAX_FILTER_LINES < json_array_size(jfls)) {
-    fprintf(stderr, "tr_cfg_parse_one_filter: Filter has too many filter_lines, maximimum of %d.\n", TR_MAX_FILTER_LINES);
-    *rc = TR_CFG_NOPARSE;
-    return NULL;
-  }
-
-  if (NULL == (filt = talloc(trc, TR_FILTER))) {
-    fprintf(stderr, "tr_cfg_parse_one_filter: Out of memory.\n");
-    *rc = TR_CFG_NOMEM;
-    return NULL;
-  }
-
-  memset(filt, 0, sizeof(TR_FILTER));
+  TALLOC_CTX *tmp_ctx=talloc_new(NULL);
+  TR_COMM_ITER *iter=NULL;
+  TR_IDP_REALM *idp = NULL;
+  char *s=NULL;
 
-  if (!strcmp(json_string_value(jftype), "rp_permitted")) {
-    filt->type = TR_FILTER_TYPE_RP_PERMITTED;
-  }
-  else {
-    fprintf(stderr, "tr_cfg_parse_one_filter: Error parsing filter type, unknown type '%s'.\n", json_string_value(jftype));
-    *rc = TR_CFG_NOPARSE;
-    tr_filter_free(filt);
-    return NULL;
+  iter=tr_comm_iter_new(tmp_ctx);
+  if (iter==NULL) {
+    tr_notice("tr_print_config: unable to allocate IdP iterator.");
+    talloc_free(tmp_ctx);
+    return;
   }
-
-  /* For each filter line... */
-  for (i = 0; i < json_array_size(jfls); i++) {
-
-    if ((NULL == (jfaction = json_object_get(json_array_get(jfls, i), "action"))) ||
-       (!json_is_string(jfaction))) {
-      fprintf(stderr, "tr_cfg_parse_one_filter: Error parsing filter action.\n");
-      *rc = TR_CFG_NOPARSE;
-      tr_filter_free(filt);
-      return NULL;
-    }
-    if ((NULL == (jfspecs = json_object_get(json_array_get(jfls, i), "filter_specs"))) ||
-       (!json_is_array(jfspecs)) ||
-       (0 == json_array_size(jfspecs))) {
-      fprintf(stderr, "tr_cfg_parse_one_filter: Error parsing filter specs.\n");
-      *rc = TR_CFG_NOPARSE;
-      tr_filter_free(filt);
-      return NULL;
-    }
   
-    if (TR_MAX_FILTER_SPECS < json_array_size(jfspecs)) {
-      fprintf(stderr, "tr_cfg_parse_one_filter: Filter has too many filter_specs, maximimum of %d.\n", TR_MAX_FILTER_SPECS);
-      *rc = TR_CFG_NOPARSE;
-      tr_filter_free(filt);
-      return NULL;
-    }
-
-    if (NULL == (filt->lines[i] = talloc(trc, TR_FLINE))) {
-      fprintf(stderr, "tr_cfg_parse_one_filter: Out of memory (fline).\n");
-      *rc = TR_CFG_NOMEM;
-      tr_filter_free(filt);
-      return NULL;
-    }
-
-    memset(filt->lines[i], 0, sizeof(TR_FLINE));
-
-    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 {
-      fprintf(stderr, "tr_cfg_parse_one_filter: Error parsing filter action, unknown action' %s'.\n", json_string_value(jfaction));
-      *rc = TR_CFG_NOPARSE;
-      tr_filter_free(filt);
-      return NULL;
-    }
-
-    if ((NULL != (jrc = json_object_get(json_array_get(jfls, i), "realm_constraints"))) &&
-       (json_is_array(jrc)) &&
-       (0 != json_array_size(jrc)) &&
-       (TR_MAX_CONST_MATCHES >= json_array_size(jrc))) {
-
-      if (NULL == (filt->lines[i]->realm_cons = tr_cfg_parse_one_constraint(trc, "realm", jrc, rc))) {
-       fprintf(stderr, "tr_cfg_parse_one_filter: Error parsing realm constraint");
-      tr_filter_free(filt);
-      return NULL;
-      }
-    }
-
-    if ((NULL != (jdc = json_object_get(json_array_get(jfls, i), "domain_constraints"))) &&
-       (json_is_array(jdc)) &&
-       (0 != json_array_size(jdc)) &&
-       (TR_MAX_CONST_MATCHES >= json_array_size(jdc))) {
-
-      if (NULL == (filt->lines[i]->domain_cons = tr_cfg_parse_one_constraint(trc, "domain", jdc, rc))) {
-       fprintf(stderr, "tr_cfg_parse_one_filter: Error parsing domain constraint");
-      tr_filter_free(filt);
-      return NULL;
-      }
-    }
-
-    /*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)) ||
-         (NULL == (jfmatch = json_object_get(json_array_get(jfspecs, j), "match"))) ||
-         (!json_is_string(jfmatch))) {
-       fprintf (stderr, "tr_cfg_parse_one_filter: Error parsing filter field and match for filter spec %d, filter line %d.\n", i, j);
-       *rc = TR_CFG_NOPARSE;
-       tr_filter_free(filt);
-       return NULL;
-      }
-
-      if (NULL == (filt->lines[i]->specs[j] = talloc(trc, TR_FSPEC))) {
-       fprintf(stderr, "tr_cfg_parse_one_filter: Out of memory.\n");
-       *rc = TR_CFG_NOMEM;
-       tr_filter_free(filt);
-       return NULL;
-      }
-
-      memset(filt->lines[i]->specs[j], 0, sizeof(TR_FSPEC));
-    
-      if ((NULL == (filt->lines[i]->specs[j]->field = tr_new_name((char *)json_string_value(jffield)))) ||
-         (NULL == (filt->lines[i]->specs[j]->match = tr_new_name((char *)json_string_value(jfmatch))))) {
-       fprintf(stderr, "tr_cfg_parse_one_filter: Out of memory.\n");
-       *rc = TR_CFG_NOMEM;
-       tr_filter_free(filt);
-       return NULL;
-      }
-    }
-  }
-
-  return filt;
+  for (idp=tr_idp_realm_iter_first(iter, ctab, tr_comm_get_id(comm));
+       NULL!=idp;
+       idp=tr_idp_realm_iter_next(iter)) {
+    s=tr_idp_realm_to_str(tmp_ctx, idp);
+    if (s!=NULL)
+      tr_notice("tr_print_config:    - @%s", s);
+    else
+      tr_notice("tr_print_config: unable to allocate IdP output string.");
+  }
+  talloc_free(tmp_ctx);
 }
 
-static TR_RP_CLIENT *tr_cfg_parse_one_rp_client (TR_CFG *trc, json_t *jrp, TR_CFG_RC *rc)
+void tr_print_comm_rps(TR_COMM_TABLE *ctab, TR_COMM *comm)
 {
-  TR_RP_CLIENT *rp = NULL;
-  json_t *jgns = NULL;
-  json_t *jfilt = NULL;
-  json_t *jftype = NULL;
-  int i = 0;
-
-  if ((!trc) || (!jrp) || (!rc)) {
-    fprintf(stderr, "tr_cfg_parse_one_rp_realm: Bad parameters.\n");
-    if (rc)
-      *rc = TR_CFG_BAD_PARAMS;
-    return NULL;
-  }
-
-  if ((NULL == (jgns = json_object_get(jrp, "gss_names"))) ||
-      (!json_is_array(jgns))) {
-    fprintf(stderr, "tr_cfg_parse_one_rp_client: Error parsing RP client configuration, no GSS names.\n");
-    *rc = TR_CFG_NOPARSE;
-    return NULL;
-  }
-
-  /* TBD -- Support more than one filter per RP client? */
-  if (NULL == (jfilt = json_object_get(jrp, "filter"))) {
-    fprintf(stderr, "tr_cfg_parse_one_rp_client: Error parsing RP client configuration, no filter.\n");
-    *rc = TR_CFG_NOPARSE;
-    return NULL;
-  }
-
-  /* We only support rp_permitted filters for RP clients */
-  if ((NULL == (jftype = json_object_get(jfilt, "type"))) ||
-      (!json_is_string(jftype)) ||
-      (strcmp(json_string_value(jftype), "rp_permitted"))) {
-    fprintf(stderr, "tr_cfg_parse_one_rp_client: Error parsing RP client filter type.\n");
-    *rc = TR_CFG_NOPARSE;
-    return NULL;
-  }
-
-  if (TR_MAX_GSS_NAMES < json_array_size(jgns)) {
-    fprintf(stderr, "tr_cfg_parse_one_rp_client: RP Client has too many GSS Names.\n");
-    *rc = TR_CFG_NOPARSE;
-    return NULL;
-  }
-
-  if (NULL == (rp = talloc(trc, TR_RP_CLIENT))) {
-    fprintf(stderr, "tr_cfg_parse_one_rp_realm: Out of memory.\n");
-    *rc = TR_CFG_NOMEM;
-    return NULL;
-  }
-  
-  memset(rp, 0, sizeof(TR_RP_CLIENT));
+  TALLOC_CTX *tmp_ctx=talloc_new(NULL);
+  TR_COMM_ITER *iter=NULL;
+  TR_RP_REALM *rp = NULL;
+  char *s=NULL;
 
-  /* TBD -- support more than one filter entry per RP Client? */
-  if (NULL == (rp->filter = tr_cfg_parse_one_filter(trc, jfilt, rc))) {
-    fprintf(stderr, "tr_cfg_parse_one_rp_client: Error parsing filter.\n");
-    *rc = TR_CFG_NOPARSE;
-    return NULL;
-  }
-    
-  for (i = 0; i < json_array_size(jgns); i++) {
-    if (NULL == (rp->gss_names[i] = tr_new_name ((char *)json_string_value(json_array_get(jgns, i))))) {
-      fprintf(stderr, "tr_cfg_parse_one_rp_client: No memory for GSS Name.\n");
-      *rc = TR_CFG_NOMEM;
-      return NULL;
-    }
+  iter=tr_comm_iter_new(tmp_ctx);
+  if (iter==NULL) {
+    tr_notice("tr_print_config: unable to allocate RP iterator.");
+    talloc_free(tmp_ctx);
+    return;
   }
   
-  return rp;
+  for (rp=tr_rp_realm_iter_first(iter, ctab, tr_comm_get_id(comm));
+       NULL!=rp;
+       rp=tr_rp_realm_iter_next(iter)) {
+    s=tr_rp_realm_to_str(tmp_ctx, rp);
+    if (s!=NULL)
+      tr_notice("tr_print_config:    - @%s", s);
+    else
+      tr_notice("tr_print_config: unable to allocate RP output string.");
+  }
+  talloc_free(tmp_ctx);
 }
 
-static TR_CFG_RC tr_cfg_parse_rp_clients (TR_CFG *trc, json_t *jcfg) {
-  json_t *jrps = NULL;
-  TR_RP_CLIENT *rp = NULL;
-  TR_CFG_RC rc = TR_CFG_SUCCESS;
-  int i = 0;
-
-  if ((!trc) || (!jcfg))
-    return TR_CFG_BAD_PARAMS;
-
-  if (NULL != (jrps = json_object_get(jcfg, "rp_clients"))) {
-
-    if (!json_is_array(jrps)) {
-      return TR_CFG_NOPARSE;
+static int tr_cfg_destructor(void *object)
+{
+  TR_CFG *cfg = talloc_get_type_abort(object, TR_CFG);
+  if (cfg->files)
+    g_array_unref(cfg->files);
+  return 0;
+}
+TR_CFG *tr_cfg_new(TALLOC_CTX *mem_ctx)
+{
+  TR_CFG *cfg=talloc(mem_ctx, TR_CFG);
+  if (cfg!=NULL) {
+    cfg->internal=NULL;
+    cfg->rp_clients=NULL;
+    cfg->peers=NULL;
+    cfg->default_servers=NULL;
+    cfg->ctable=tr_comm_table_new(cfg);
+    if (cfg->ctable==NULL) {
+      talloc_free(cfg);
+      return NULL;
     }
-
-    for (i = 0; i < json_array_size(jrps); i++) {
-      if (NULL == (rp = tr_cfg_parse_one_rp_client(trc, 
-                                                  json_array_get(jrps, i), 
-                                                  &rc))) {
-       return rc;
-      }
-      fprintf(stderr, "tr_cfg_parse_rp_clients: RP client configured -- first gss: %s\n", rp->gss_names[0]->buf);
-      rp->next = trc->rp_clients;
-      trc->rp_clients = rp;
+    cfg->files = g_array_new(FALSE, FALSE, sizeof(TR_CFG_FILE));
+    if (cfg->files == NULL) {
+      talloc_free(cfg);
+      return NULL;
     }
+    talloc_set_destructor((void *)cfg, tr_cfg_destructor);
   }
-  return rc;
+  return cfg;
 }
 
-static TR_AAA_SERVER *tr_cfg_parse_one_aaa_server (TR_CFG *trc, json_t *jaddr, TR_CFG_RC *rc) {
-  TR_AAA_SERVER *aaa = NULL;
-
-  if ((!trc) || (!jaddr) || (!json_is_string(jaddr))) {
-    fprintf(stderr, "tr_cfg_parse_one_aaa_server: Bad parameters.\n");
-    *rc = TR_CFG_BAD_PARAMS;
-    return NULL;
-  }
-
-  if (NULL == (aaa = talloc(trc, TR_AAA_SERVER))) {
-    fprintf(stderr, "tr_cfg_parse_one_aaa_server: Out of memory.\n");
-    *rc = TR_CFG_NOMEM;
-    return NULL;
-  }
-
-  memset(aaa, 0, sizeof(TR_AAA_SERVER));
-
-  aaa->hostname = tr_new_name((char *)(json_string_value(jaddr)));
-
-  return aaa;
+void tr_cfg_free (TR_CFG *cfg)
+{
+  talloc_free(cfg);
 }
 
-static TR_AAA_SERVER *tr_cfg_parse_aaa_servers (TR_CFG *trc, json_t *jaaas, TR_CFG_RC *rc) 
+TR_CFG_MGR *tr_cfg_mgr_new(TALLOC_CTX *mem_ctx)
 {
-  TR_AAA_SERVER *aaa = NULL;
-  TR_AAA_SERVER *temp_aaa = NULL;
-  int i = 0;
+  return talloc_zero(mem_ctx, TR_CFG_MGR);
+}
 
-  for (i = 0; i < json_array_size(jaaas); i++) {
-    if (NULL == (temp_aaa = tr_cfg_parse_one_aaa_server(trc, json_array_get(jaaas, i), rc))) {
-      return NULL;
-    }
-    /* TBD -- IPv6 addresses */
-    //    fprintf(stderr, "tr_cfg_parse_aaa_servers: Configuring AAA Server: ip_addr = %s.\n", inet_ntoa(temp_aaa->aaa_server_addr));
-    temp_aaa->next = aaa;
-    aaa = temp_aaa;
-  }
-  return aaa;
+void tr_cfg_mgr_free (TR_CFG_MGR *cfg_mgr) {
+  talloc_free(cfg_mgr);
 }
 
-static TR_APC *tr_cfg_parse_apcs (TR_CFG *trc, json_t *japcs, TR_CFG_RC *rc)
+TR_CFG_RC tr_apply_new_config (TR_CFG_MGR *cfg_mgr)
 {
-  TR_APC *apc;
-
-  *rc = TR_CFG_SUCCESS;                /* presume success */
-
-  if ((!trc) || (!japcs) || (!rc)) {
-    fprintf(stderr, "tr_cfg_parse_apcs: Bad parameters.\n");
-    if (rc) 
-      *rc = TR_CFG_BAD_PARAMS;
-    return NULL;
-  }
-
-  if (NULL == (apc = talloc(trc, TR_APC))) {
-    fprintf (stderr, "tr_cfg_parse_apcs: Out of memory.\n");
-    *rc = TR_CFG_NOMEM;
-    return NULL;
-  }
+  /* cfg_mgr->active is allowed to be null, but new cannot be */
+  if ((cfg_mgr==NULL) || (cfg_mgr->new==NULL))
+    return TR_CFG_BAD_PARAMS;
 
-  memset(apc, 0, sizeof(TR_APC));
+  if (cfg_mgr->active != NULL)
+    tr_cfg_free(cfg_mgr->active);
 
-  /* TBD, deal with more than one APC.  In the meantime, though...                */
-  /* Only parse the first APC, because we only know how to deal with one, anyway. */
-  if (0 == json_array_size(japcs))
-    return NULL;
+  cfg_mgr->active = cfg_mgr->new;
+  cfg_mgr->new=NULL; /* only keep a single handle on the new configuration */
 
-  if (NULL == (apc->id = tr_new_name((char *)json_string_value(json_array_get(japcs, 0))))) {
-    fprintf(stderr, "tr_cfg_parse_apcs: No memory for APC name.\n");
-    *rc = TR_CFG_NOMEM;
-    return NULL;
-  }
+  tr_log_threshold(cfg_mgr->active->internal->log_threshold);
+  tr_console_threshold(cfg_mgr->active->internal->console_threshold);
 
-  return apc;
+  return TR_CFG_SUCCESS;
 }
 
-static TR_IDP_REALM *tr_cfg_parse_one_idp_realm (TR_CFG *trc, json_t *jidp, TR_CFG_RC *rc) {
-  TR_IDP_REALM *idp = NULL;
-  json_t *jrid = NULL;
-  json_t *jscfg = NULL;
-  json_t *jsrvrs = NULL;
-  json_t *japcs = NULL;
-
-  if ((!trc) || (!jidp) || (!rc)) {
-    fprintf(stderr, "tr_cfg_parse_one_idp_realm: Bad parameters.\n");
-    if (rc)
-      *rc = TR_CFG_BAD_PARAMS;
-    return NULL;
-  }
 
-  if (NULL == (idp = talloc(trc, TR_IDP_REALM))) {
-    fprintf(stderr, "tr_cfg_parse_one_idp_realm: Out of memory.\n");
-    *rc = TR_CFG_NOMEM;
-    return NULL;
-  }
+TR_CFG_RC tr_cfg_validate(TR_CFG *trc)
+{
+  TR_CFG_RC rc = TR_CFG_SUCCESS;
 
-  memset(idp, 0, sizeof(TR_IDP_REALM));
+  if (!trc)
+    return TR_CFG_BAD_PARAMS;
 
-  if ((NULL == (jrid = json_object_get(jidp, "realm_id"))) ||
-      (!json_is_string(jrid)) ||
-      (NULL == (jscfg = json_object_get(jidp, "shared_config"))) ||
-      (!json_is_string(jscfg)) ||
-      (NULL == (jsrvrs = json_object_get(jidp, "aaa_servers"))) ||
-      (!json_is_array(jsrvrs))) {
-    fprintf(stderr, "tr_cfg_parse_one_idp_realm: Error parsing IDP realm configuration.\n");
-    *rc = TR_CFG_NOPARSE;
-    return NULL;
+  if ((NULL == trc->internal)||
+      (NULL == trc->internal->hostname)) {
+    tr_debug("tr_cfg_validate: Error: No internal configuration, or no hostname.");
+    rc = TR_CFG_ERROR;
   }
 
-  if (0 == strcmp(json_string_value(jscfg), "no")) {
-    idp->shared_config = 0;
-  } else {
-    idp->shared_config = 1;
+  if (NULL == trc->rp_clients) {
+    tr_debug("tr_cfg_validate: Error: No RP Clients configured");
+    rc = TR_CFG_ERROR;
   }
 
-  if (NULL == (idp->realm_id = tr_new_name((char *)json_string_value(jrid)))) {
-    fprintf(stderr, "tr_cfg_parse_one_idp_realm: No memory for realm id.\n");
-    *rc = TR_CFG_NOMEM;
-    return NULL;
+  if (0==tr_comm_table_size(trc->ctable)) {
+    tr_debug("tr_cfg_validate: Error: No Communities configured");
+    rc = TR_CFG_ERROR;
   }
 
-  if (NULL == (idp->aaa_servers = tr_cfg_parse_aaa_servers(trc, jsrvrs, rc))) {
-    fprintf(stderr, "tr_cfg_parse_one_idp_realm: Can't parse AAA servers for realm %s.\n", idp->realm_id->buf);
-    tr_free_name(idp->realm_id);
-    return NULL;
+  if ((NULL == trc->default_servers) && (NULL == trc->ctable->idp_realms)) {
+    tr_debug("tr_cfg_validate: Error: No default servers or IDPs configured.");
+    rc = TR_CFG_ERROR;
   }
-
-  if ((NULL != (japcs = json_object_get(jidp, "apcs"))) &&
-      (json_is_array(japcs))) {
-    if (NULL == (idp->apcs = tr_cfg_parse_apcs(trc, japcs, rc))) {
-      fprintf(stderr, "tr_cfg_parse_one_idp_realm: Can't parse APCs for realm %s .\n", idp->realm_id->buf);
-      tr_free_name(idp->realm_id);
-      /* TBD -- free aaa_servers */;
-      return NULL;
-    }
-  } 
-  return idp;
+  
+  return rc;
 }
 
-static TR_CFG_RC tr_cfg_parse_default_servers (TR_CFG *trc, json_t *jcfg) 
+static void tr_cfg_log_json_error(const char *label, json_error_t *rc)
 {
-  json_t *jdss = NULL;
-  TR_CFG_RC rc = TR_CFG_SUCCESS;
-  TR_AAA_SERVER *ds = NULL;
-  int i = 0;
-
-  if ((!trc) || (!jcfg))
-    return TR_CFG_BAD_PARAMS;
-
-  /* If there are default servers, store them */
-  if ((NULL != (jdss = json_object_get(jcfg, "default_servers"))) &&
-      (json_is_array(jdss)) &&
-      (0 < json_array_size(jdss))) {
-
-    for (i = 0; i < json_array_size(jdss); i++) {
-      if (NULL == (ds = tr_cfg_parse_one_aaa_server(trc, 
-                                                 json_array_get(jdss, i), 
-                                                 &rc))) {
-       return rc;
-      }
-      fprintf(stderr, "tr_cfg_parse_default_servers: Default server configured: %s.\n", ds->hostname->buf);
-      ds->next = trc->default_servers;
-      trc->default_servers = ds;
-    }
-  } 
-
-  return rc;
+  tr_debug("%s: JSON parse error on line %d: %s",
+          label,
+          rc->line,
+          rc->text);
 }
 
-static TR_CFG_RC tr_cfg_parse_idp_realms (TR_CFG *trc, json_t *jcfg) 
+/**
+ * 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 *jidps = NULL;
-  TR_CFG_RC rc = TR_CFG_SUCCESS;
-  TR_IDP_REALM *idp = NULL;
-  int i = 0;
-
-  if ((!trc) || (!jcfg))
-    return TR_CFG_BAD_PARAMS;
+  json_t *jcfg=NULL;
+  json_error_t rc;
 
-  /* If there are any IDP Realms, parse them */
-  if ((NULL != (jidps = json_object_get(jcfg, "idp_realms"))) &&
-      (json_is_array(jidps))) {
-    for (i = 0; i < json_array_size(jidps); i++) {
-      if (NULL == (idp = tr_cfg_parse_one_idp_realm(trc,
-                                                   json_array_get(jidps, i), 
-                                                   &rc))) {
-       return rc;
-      }
-      fprintf(stderr, "tr_cfg_parse_idp_realms: IDP realm configured: %s.\n", idp->realm_id->buf);
-      idp->next = trc->idp_realms;
-      trc->idp_realms = idp;
-    }
+  if (NULL==(jcfg=json_load_file(file_with_path, 
+                                 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 NULL;
   }
 
-  return rc;
+  return jcfg;
 }
 
-static TR_IDP_REALM *tr_cfg_parse_comm_idps (TR_CFG *trc, json_t *jidps, TR_CFG_RC *rc)
+/* extract serial number */
+static json_int_t get_cfg_serial(json_t *jcfg)
 {
-  TR_IDP_REALM *idp = NULL;
-  TR_IDP_REALM *temp_idp = NULL;
-  int i = 0;
-
-  if ((!trc) ||
-      (!jidps) ||
-      (!json_is_array(jidps))) {
-    if (rc)
-      *rc = TR_CFG_BAD_PARAMS;
-    return NULL;
-  }
+  json_t *jser=NULL;
 
-  for (i = 0; i < json_array_size(jidps); i++) {
-    if (NULL == (temp_idp = (tr_cfg_find_idp(trc, 
-                                            tr_new_name((char *)json_string_value(json_array_get(jidps, i))), 
-                                            rc)))) {
-      fprintf(stderr, "tr_cfg_parse_comm_idps: Unknown IDP %s.\n", 
-             (char *)json_string_value(json_array_get(jidps, i)));
-      return NULL;
+  if (NULL != (jser = json_object_get(jcfg, "serial_number"))) {
+    if (json_is_integer(jser)) {
+      return json_integer_value(jser);
     }
-
-    temp_idp->comm_next = idp;
-    idp = temp_idp;
   }
-
-  return idp;
+  return TR_CFG_INVALID_SERIAL;
 }
 
-static TR_RP_REALM *tr_cfg_parse_comm_rps (TR_CFG *trc, json_t *jrps, TR_CFG_RC *rc)
+/**
+ * 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)
 {
-  TR_RP_REALM *rp = NULL;
-  TR_RP_REALM *temp_rp = NULL;
-  int i = 0;
-
-  if ((!trc) ||
-      (!jrps) ||
-      (!json_is_array(jrps))) {
-    if (rc)
-      *rc = TR_CFG_BAD_PARAMS;
-    return NULL;
-  }
+  int ii=0;
+  for (ii=0; ii<n_jcfgs; ii++)
+    json_decref(jcfgs[ii]);
+  talloc_free(jcfgs);
+}
 
-  for (i = (json_array_size(jrps)-1); i >= 0; i--) {
-    if (NULL == (temp_rp = talloc(trc, TR_RP_REALM))) {
-      fprintf(stderr, "tr_cfg_parse_comm_rps: Can't allocate memory for RP Realm.\n");
-      if (rc)
-       *rc = TR_CFG_NOMEM;
-      return NULL;
+/**
+ * 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, GArray *files)
+{
+  TALLOC_CTX *tmp_ctx=talloc_new(NULL);
+  unsigned int ii=0;
+  json_t **jcfgs=NULL;
+  TR_CFG_FILE *this_file = 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++) {
+    this_file = &g_array_index(files, TR_CFG_FILE, ii);
+    jcfgs[ii]=tr_cfg_parse_one_config_file(this_file->name);
+    if (jcfgs[ii]==NULL) {
+      tr_err("tr_parse_config: Error parsing JSON in %s", this_file->name);
+      tr_cfg_parse_free_jcfgs(ii, jcfgs); /* frees the JSON objects and the jcfgs array */
+      jcfgs=NULL;
+      goto cleanup;
     }
-    memset (temp_rp, 0, sizeof(TR_RP_REALM));
 
-    if (NULL == (temp_rp->realm_name = tr_new_name((char *)json_string_value(json_array_get(jrps, i))))) {
-      fprintf(stderr, "tr_cfg_parse_comm_rps: No memory for RP Realm Name.\n");
-      if (rc)
-       *rc = TR_CFG_NOMEM;
-      return NULL;
+    this_file->serial = get_cfg_serial(jcfgs[ii]);
+    if (this_file->serial != TR_CFG_INVALID_SERIAL) {
+      tr_notice("tr_parse_config_files: Attempting to load revision %"
+                    JSON_INTEGER_FORMAT
+                    " of '%s'.",
+                this_file->serial,
+                this_file->name);
+    } else {
+      tr_notice("tr_parse_config_files: Attempting to load '%s'.",
+                this_file->name);
     }
-
-    temp_rp->next = rp;
-    rp = temp_rp;
   }
 
-  return rp;
+cleanup:
+  if (jcfgs)
+    talloc_steal(mem_ctx, jcfgs); /* give this to the caller's context if we succeeded */
+  talloc_free(tmp_ctx);
+  return jcfgs;
 }
 
-static TR_COMM *tr_cfg_parse_one_comm (TR_CFG *trc, json_t *jcomm, TR_CFG_RC *rc) {
-  TR_COMM *comm = NULL;
-  json_t *jid = NULL;
-  json_t *jtype = NULL;
-  json_t *japcs = NULL;
-  json_t *jidps = NULL;
-  json_t *jrps = NULL;
-
-  if ((!trc) || (!jcomm) || (!rc)) {
-    fprintf(stderr, "tr_cfg_parse_one_comm: Bad parameters.\n");
-    if (rc)
-      *rc = TR_CFG_BAD_PARAMS;
-    return NULL;
-  }
+/* 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 parse_fn Function to apply
+ * @param n_jcfg Number of JSON structures in the array
+ * @param jcfgs Pointer to an array of decoded JSON structures
+ * @param key Key to extract from each jcfg before parsing, or NULL to use the object itself
+ * @return TR_CFG_SUCCESS on success, _FAIL or an error code on failure
+ */
+static TR_CFG_RC tr_cfg_parse_helper(TR_CFG *cfg,
+                                     TR_CFG_PARSE_FN parse_fn,
+                                     unsigned int n_jcfg,
+                                     json_t **jcfgs,
+                                     const char *key)
+{
+  size_t ii=0;
+  json_t *this_jcfg=NULL;
+  TR_CFG_RC ret=TR_CFG_ERROR;
 
-  if (NULL == (comm = talloc(trc, TR_COMM))) {
-    fprintf(stderr, "tr_cfg_parse_one_comm: Out of memory.\n");
-    *rc = TR_CFG_NOMEM;
-    return NULL;
-  }
+  if ((cfg==NULL) || (jcfgs==NULL) || (parse_fn==NULL))
+    return TR_CFG_ERROR;
 
-  memset(comm, 0, sizeof(TR_COMM));
-
-  if ((NULL == (jid = json_object_get(jcomm, "community_id"))) ||
-      (!json_is_string(jid)) ||
-      (NULL == (jtype = json_object_get(jcomm, "type"))) ||
-      (!json_is_string(jtype)) ||
-      (NULL == (japcs = json_object_get(jcomm, "apcs"))) ||
-      (!json_is_array(japcs)) ||
-      (NULL == (jidps = json_object_get(jcomm, "idp_realms"))) ||
-      (!json_is_array(jidps)) ||
-      (NULL == (jrps = json_object_get(jcomm, "rp_realms"))) ||
-      (!json_is_array(jrps))) {
-    fprintf(stderr, "tr_cfg_parse_one_comm: Error parsing Communities configuration.\n");
-    *rc = TR_CFG_NOPARSE;
-    return NULL;
-  }
+  for (ii=0; ii<n_jcfg; ii++) {
+    if (key)
+      this_jcfg = json_object_get(jcfgs[ii], key);
+    else
+      this_jcfg = jcfgs[ii];
 
-  if (NULL == (comm->id = tr_new_name((char *)json_string_value(jid)))) {
-    fprintf(stderr, "tr_cfg_parse_one_comm: No memory for community id.\n");
-    *rc = TR_CFG_NOMEM;
-    return NULL;
-  }
+    /* do not try to parse a missing jcfg */
+    if (this_jcfg == NULL)
+      continue;
 
-  if (0 == strcmp(json_string_value(jtype), "apc")) {
-    comm->type = TR_COMM_APC;
-  } else if (0 == strcmp(json_string_value(jtype), "coi")) {
-    comm->type = TR_COMM_COI;
-    if (NULL == (comm->apcs = tr_cfg_parse_apcs(trc, japcs, rc))) {
-      fprintf(stderr, "tr_cfg_parse_one_comm: Can't parse APCs for COI %s.\n", comm->id->buf);
-      tr_free_name(comm->id);
-      return NULL;
-    }
-  } else {
-    fprintf(stderr, "tr_cfg_parse_one_comm: Invalid community type, comm = %s, type = %s\n", comm->id->buf, json_string_value(jtype));
-    tr_free_name(comm->id);
-    *rc = TR_CFG_NOPARSE;
-    return NULL;
+    ret=parse_fn(cfg, this_jcfg);
+    if (ret!=TR_CFG_SUCCESS)
+      break;
   }
-
-  comm->idp_realms = tr_cfg_parse_comm_idps(trc, jidps, rc);
-  if (TR_CFG_SUCCESS != *rc) {
-    fprintf(stderr, "tr_cfg_parse_one_comm: Can't parse IDP realms for comm %s.\n", comm->id->buf);
-    tr_free_name(comm->id);
-    return NULL;
-  }
-
-  comm->rp_realms = tr_cfg_parse_comm_rps(trc, jrps, rc);
-  if (TR_CFG_SUCCESS != *rc) {
-    fprintf(stderr, "tr_cfg_parse_comm: Can't parse RP realms for comm %s .\n", comm->id->buf);
-    tr_free_name(comm->id);
-    /* TBD -- free idps? */;
-    return NULL;
-  }
-
-  return comm;
+  return ret;
 }
 
-static TR_CFG_RC tr_cfg_parse_comms (TR_CFG *trc, json_t *jcfg) 
+static void add_files(TR_CFG *cfg, unsigned int n, char **filenames)
 {
-  json_t *jcomms = NULL;
-  TR_CFG_RC rc = TR_CFG_SUCCESS;
-  TR_COMM *comm = NULL;
-  int i = 0;
+  TR_CFG_FILE frec = {0};
 
-  if ((!trc) || (!jcfg)) {
-    fprintf(stderr, "tr_cfg_parse_comms: Bad Parameters.\n");
-    return TR_CFG_BAD_PARAMS;
-  }
+  while ((n--) > 0) {
+    frec.name = talloc_strdup(cfg, filenames[n]);
+    frec.serial = TR_CFG_INVALID_SERIAL;
 
-  if (NULL != (jcomms = json_object_get(jcfg, "communities"))) {
-    if (!json_is_array(jcomms)) {
-      return TR_CFG_NOPARSE;
-    }
-
-    for (i = 0; i < json_array_size(jcomms); i++) {
-      if (NULL == (comm = tr_cfg_parse_one_comm(trc, 
-                                               json_array_get(jcomms, i), 
-                                               &rc))) {
-       return rc;
-      }
-      fprintf(stderr, "tr_cfg_parse_comms: Community configured: %s.\n", comm->id->buf);
-      comm->next = trc->comms;
-      trc->comms = comm;
-    }
+    g_array_append_val(cfg->files, frec);
   }
-  return rc;
 }
 
-TR_CFG_RC tr_cfg_validate (TR_CFG *trc) {
-  TR_CFG_RC rc = TR_CFG_SUCCESS;
-
-  if (!trc)
-    return TR_CFG_BAD_PARAMS;
+/**
+ *  Read a list of configuration files
+ *
+ * @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);
+  json_t **jcfgs=NULL;
+  TR_CFG_RC cfg_rc=TR_CFG_ERROR;
 
-  if ((NULL == trc->internal)||
-      (NULL == trc->internal->hostname)) {
-    fprintf(stderr, "tr_cfg_validate: Error: No internal configuration, or no hostname.\n");
-    rc = TR_CFG_ERROR;
+  if ((!cfg_mgr) || (!files_with_paths)) {
+    cfg_rc=TR_CFG_BAD_PARAMS;
+    goto cleanup;
   }
 
-  if (NULL == trc->rp_clients) {
-    fprintf(stderr, "tr_cfg_validate: Error: No RP Clients configured\n");
-    rc = TR_CFG_ERROR;
+  /* 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 */
+  if (cfg_mgr->new == NULL) {
+    cfg_rc=TR_CFG_NOMEM;
+    goto cleanup;
   }
 
-  if (NULL == trc->comms) {
-    fprintf(stderr, "tr_cfg_validate: Error: No Communities configured\n");
-    rc = TR_CFG_ERROR;
-  }
+  /* add the list of files to the config */
+  add_files(cfg_mgr->new, n_files, files_with_paths);
 
-  if ((NULL == trc->default_servers) && (NULL == trc->idp_realms)) {
-    fprintf(stderr, "tr_cfg_validate: Error: No default servers or IDPs configured.\n");
-    rc = TR_CFG_ERROR;
+  /* first parse the json */
+  jcfgs=tr_cfg_parse_config_files(tmp_ctx, n_files, cfg_mgr->new->files);
+  if (jcfgs==NULL) {
+    cfg_rc=TR_CFG_NOPARSE;
+    goto cleanup;
   }
-  
-  return rc;
-}
-
-TR_CFG_RC tr_parse_config (TR_INSTANCE *tr, int n, struct dirent **cfg_files) {
-  json_t *jcfg;
-  json_error_t rc;
 
-  if ((!tr) || (!cfg_files))
-    return TR_CFG_BAD_PARAMS;
+  cfg_mgr->new->peers=trp_ptable_new(cfg_mgr); /* not sure why this isn't in cfg_mgr->new's context */
 
-  /* If there is a partial/abandoned config lying around, free it */
-  if (tr->new_cfg) 
-    tr_cfg_free(tr->new_cfg);
-  
-  if (NULL == (tr->new_cfg = talloc(NULL, TR_CFG)))
-    return TR_CFG_NOMEM;
-
-  memset(tr->new_cfg, 0, sizeof(TR_CFG));
-
-  /* Parse configuration information from each config file */
-  while (n--) {
-    fprintf(stderr, "tr_read_config: Parsing %s.\n", cfg_files[n]->d_name);
-    if (NULL == (jcfg = json_load_file(cfg_files[n]->d_name, 
-                                      JSON_DISABLE_EOF_CHECK, &rc))) {
-      fprintf (stderr, "tr_read_config: Error parsing config file %s.\n", 
-              cfg_files[n]->d_name);
-      return TR_CFG_NOPARSE;
-    }
-       
-    if ((TR_CFG_SUCCESS != tr_cfg_parse_internal(tr->new_cfg, jcfg)) ||
-       (TR_CFG_SUCCESS != tr_cfg_parse_rp_clients(tr->new_cfg, jcfg)) ||
-       (TR_CFG_SUCCESS != tr_cfg_parse_idp_realms(tr->new_cfg, jcfg)) ||
-       (TR_CFG_SUCCESS != tr_cfg_parse_default_servers(tr->new_cfg, jcfg)) ||
-       (TR_CFG_SUCCESS != tr_cfg_parse_comms(tr->new_cfg, jcfg))) {
-      tr_cfg_free(tr->new_cfg);
-      return TR_CFG_ERROR;
-    }
-  }
+  /* now run through the parsers on the JSON */
+  if ((TR_CFG_SUCCESS != (cfg_rc= tr_cfg_parse_helper(cfg_mgr->new, tr_cfg_parse_internal, n_files, jcfgs, "tr_internal"))) ||
+      (TR_CFG_SUCCESS != (cfg_rc= tr_cfg_parse_helper(cfg_mgr->new, tr_cfg_parse_local_orgs, n_files, jcfgs, NULL))) ||
+      (TR_CFG_SUCCESS != (cfg_rc= tr_cfg_parse_helper(cfg_mgr->new, tr_cfg_parse_peer_orgs, n_files, jcfgs, NULL))) ||
+      (TR_CFG_SUCCESS != (cfg_rc= tr_cfg_parse_helper(cfg_mgr->new, tr_cfg_parse_default_servers, n_files, jcfgs,
+                                                      NULL))) ||
+      (TR_CFG_SUCCESS != (cfg_rc= tr_cfg_parse_helper(cfg_mgr->new, tr_cfg_parse_comms, n_files, jcfgs, NULL))))
+    goto cleanup; /* cfg_rc was set above */
 
   /* make sure we got a complete, consistent configuration */
-  if (TR_CFG_SUCCESS != tr_cfg_validate(tr->new_cfg)) {
-    fprintf(stderr, "tr_parse_config: Error: INVALID CONFIGURATION, EXITING\n");
-    return TR_CFG_ERROR;
+  if (TR_CFG_SUCCESS != tr_cfg_validate(cfg_mgr->new)) {
+    tr_err("tr_parse_config: Error: INVALID CONFIGURATION");
+    cfg_rc=TR_CFG_ERROR;
+    goto cleanup;
   }
 
-  return TR_CFG_SUCCESS;
-}
-
-TR_IDP_REALM *tr_cfg_find_idp (TR_CFG *tr_cfg, TR_NAME *idp_id, TR_CFG_RC *rc)
-{
-
-  TR_IDP_REALM *cfg_idp;
+  /* success! */
+  talloc_steal(cfg_mgr, cfg_mgr->new); /* hand this over to the cfg_mgr context */
+  cfg_rc=TR_CFG_SUCCESS;
 
-  if ((!tr_cfg) || (!idp_id)) {
-    if (rc)
-      *rc = TR_CFG_BAD_PARAMS;
-    return NULL;
-  }
-
-  for (cfg_idp = tr_cfg->idp_realms; NULL != cfg_idp; cfg_idp = cfg_idp->next) {
-    if (!tr_name_cmp (idp_id, cfg_idp->realm_id)) {
-      fprintf(stderr, "tr_cfg_find_idp: Found %s.\n", idp_id->buf);
-      return cfg_idp;
-    }
-  }
-  /* if we didn't find one, return NULL */ 
-  return NULL;
+cleanup:
+  if (jcfgs!=NULL)
+    tr_cfg_parse_free_jcfgs(n_files, jcfgs);
+  talloc_free(tmp_ctx);
+  return cfg_rc;
 }
 
-TR_RP_CLIENT *tr_cfg_find_rp (TR_CFG *tr_cfg, TR_NAME *rp_gss, TR_CFG_RC *rc)
-{
-  TR_RP_CLIENT *cfg_rp;
-  int i;
+static int is_cfg_file(const struct dirent *dent) {
+  size_t n;
 
-  if ((!tr_cfg) || (!rp_gss)) {
-    if (rc)
-      *rc = TR_CFG_BAD_PARAMS;
-    return NULL;
-  }
+  /* Only accept filenames ending in ".cfg" and starting with a character
+   * other than an ASCII '.' */
 
-  for (cfg_rp = tr_cfg->rp_clients; NULL != cfg_rp; cfg_rp = cfg_rp->next) {
-    for (i = 0; i < TR_MAX_GSS_NAMES; i++) {
-      if (!tr_name_cmp (rp_gss, cfg_rp->gss_names[i])) {
-       fprintf(stderr, "tr_cfg_find_rp: Found %s.\n", rp_gss->buf);
-       return cfg_rp;
-      }
-    }
+  /* filename must be at least 4 characters long to be acceptable */
+  n=strlen(dent->d_name);
+  if (n < 4) {
+    return 0;
   }
-  /* if we didn't find one, return NULL */ 
-  return NULL;
-}
 
-static int is_cfg_file(const struct dirent *dent) {
-  int n;
+  /* filename must not start with '.' */
+  if ('.' == dent->d_name[0]) {
+    return 0;
+  }
 
-  /* if the last four letters of the filename are .cfg, return true. */
-  if ((4 <= (n = strlen(dent->d_name))) &&
-      (0 == strcmp(&(dent->d_name[n-4]), ".cfg"))) {
+  /* If the above passed and the last four characters of the filename are .cfg, accept.
+   * (n.b., assumes an earlier test checked that the name is >= 4 chars long.) */
+  if (0 == strcmp(&(dent->d_name[n-4]), ".cfg")) {
     return 1;
   }
 
@@ -915,26 +476,36 @@ static int is_cfg_file(const struct dirent *dent) {
   return 0;
 }
 
-int tr_find_config_files (struct dirent ***cfg_files) {
-  int n = 0, i = 0;
+/* Find configuration files in a particular directory. Returns the
+ * number of entries found, 0 if none are found, or <0 for some
+ * errors. If n>=0, the cfg_files parameter will contain a newly
+ * 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 n = 0;
   
-  n = scandir(".", cfg_files, &is_cfg_file, 0);
+  n = scandir(config_dir, cfg_files, is_cfg_file, alphasort);
 
   if (n < 0) {
     perror("scandir");
-    fprintf(stderr, "tr_find_config: scandir error.\n");
-    return 0;
-  }
+    tr_debug("tr_find_config: scandir error trying to scan %s.", config_dir);
+  } 
 
-  if (n == 0) {
-    fprintf (stderr, "tr_find_config: No config files found.\n");
-    return 0;
-  }
+  return n;
+}
+
+/* Free memory allocated for configuration file list returned from tr_find_config_files().
+ * This can be called regardless of the return value of tr_find_config_values(). */
+void tr_free_config_file_list(int n, struct dirent ***cfg_files) {
+  int ii;
 
-  i = n;
-  while(i--) {
-    fprintf(stderr, "tr_find_config: Config file found (%s).\n", (*cfg_files)[i]->d_name);
+  /* if n < 0, then scandir did not allocate anything because it failed */
+  if((n>=0) && (*cfg_files != NULL)) {
+    for(ii=0; ii<n; ii++) {
+      free((*cfg_files)[ii]);
+    }
+    free(*cfg_files); /* safe even if n==0 */
+    *cfg_files=NULL; /* this will help prevent accidentally freeing twice */
   }
-    
-  return n;
 }