Properly set expiry on new community inforecs.
[trust_router.git] / common / tr_config.c
index bfe8fe8..b451860 100644 (file)
 #include <tr_cfgwatch.h>
 #include <tr_comm.h>
 #include <tr_config.h>
+#include <tr_gss.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 (TR_CFG *cfg) {
   tr_notice("tr_print_config: Logging running trust router configuration.");
-  tr_print_comms(cfg->comms);
+  tr_print_comms(cfg->ctable);
 }
 
-void tr_print_comms (TR_COMM *comm_list) {
+void tr_print_comms (TR_COMM_TABLE *ctab)
+{
   TR_COMM *comm = NULL;
 
-  for (comm = comm_list; NULL != comm; comm = comm->next) {
+  for (comm = ctab->comms; NULL != comm; comm = comm->next) {
     tr_notice("tr_print_config: Community %s:", comm->id->buf);
 
     tr_notice("tr_print_config:  - Member IdPs:");
-    tr_print_comm_idps(comm->idp_realms);
+    tr_print_comm_idps(ctab, comm);
 
     tr_notice("tr_print_config:  - Member RPs:");
-    tr_print_comm_rps(comm->rp_realms);
+    tr_print_comm_rps(ctab, comm);
   }
 }
 
-void tr_print_comm_idps (TR_IDP_REALM *idp_list) {
+void tr_print_comm_idps(TR_COMM_TABLE *ctab, TR_COMM *comm)
+{
+  TALLOC_CTX *tmp_ctx=talloc_new(NULL);
+  TR_COMM_ITER *iter=NULL;
   TR_IDP_REALM *idp = NULL;
+  char *s=NULL;
 
-  for (idp = idp_list; NULL != idp; idp = idp->comm_next) {
-    tr_notice("tr_print_config:    - @%s", idp->realm_id->buf);
+  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 (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);
 }
 
-void tr_print_comm_rps(TR_RP_REALM *rp_list) {
+void tr_print_comm_rps(TR_COMM_TABLE *ctab, TR_COMM *comm)
+{
+  TALLOC_CTX *tmp_ctx=talloc_new(NULL);
+  TR_COMM_ITER *iter=NULL;
   TR_RP_REALM *rp = NULL;
+  char *s=NULL;
 
-  for (rp = rp_list; NULL != rp; rp = rp->next) {
-    tr_notice("tr_print_config:    - %s", rp->realm_name->buf);
+  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;
   }
+  
+  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);
 }
 
 TR_CFG *tr_cfg_new(TALLOC_CTX *mem_ctx)
 {
-  return talloc_zero(mem_ctx, TR_CFG);
+  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);
+      cfg=NULL;
+    }
+  }
+  return cfg;
 }
 
-void tr_cfg_free (TR_CFG *cfg) {
+void tr_cfg_free (TR_CFG *cfg)
+{
   talloc_free(cfg);
 }
 
@@ -132,6 +184,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))
@@ -242,6 +297,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)) {
@@ -329,6 +420,7 @@ static TR_FILTER *tr_cfg_parse_one_filter(TALLOC_CTX *mem_ctx, json_t *jfilt, TR
   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;
@@ -436,26 +528,47 @@ static TR_FILTER *tr_cfg_parse_one_filter(TALLOC_CTX *mem_ctx, json_t *jfilt, TR
     /*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))) {
-        tr_debug("tr_cfg_parse_one_filter: Error parsing filter field and match for filter spec %d, filter line %d.", i, j);
+          (!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;
+        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;
         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;
+        goto cleanup;
+      }
+
+      /* allocate the filter spec */
       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;
         goto cleanup;
       }
 
-      if ((NULL==(filt->lines[i]->specs[j]->field=tr_new_name(json_string_value(jffield)))) ||
-          (NULL==(filt->lines[i]->specs[j]->match=tr_new_name(json_string_value(jfmatch))))) {
+      /* fill in the field */
+      if (NULL==(filt->lines[i]->specs[j]->field=tr_new_name(json_string_value(jffield)))) {
+        tr_debug("tr_cfg_parse_one_filter: Out of memory.");
+        *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;
         goto cleanup;
       }
+      tr_fspec_set_match(filt->lines[i]->specs[j], name);
     }
   }
   *rc=TR_CFG_SUCCESS;
@@ -463,6 +576,8 @@ static TR_FILTER *tr_cfg_parse_one_filter(TALLOC_CTX *mem_ctx, json_t *jfilt, TR
   
  cleanup:
   talloc_free(tmp_ctx);
+  if (*rc!=TR_CFG_SUCCESS)
+    filt=NULL;
   return filt;
 }
 
@@ -471,7 +586,6 @@ static TR_FILTER *tr_cfg_parse_filters(TALLOC_CTX *mem_ctx, json_t *jfilts, TR_C
   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
   json_t *jfilt;
   TR_FILTER *filt=NULL;
-  int ii=0;
 
   *rc=TR_CFG_ERROR;
 
@@ -500,7 +614,7 @@ static TR_FILTER *tr_cfg_parse_filters(TALLOC_CTX *mem_ctx, json_t *jfilts, TR_C
  cleanup:
   if (*rc==TR_CFG_SUCCESS)
     talloc_steal(mem_ctx, filt);
-  else {
+  else if (filt!=NULL) {
     talloc_free(filt);
     filt=NULL;
   }
@@ -564,7 +678,7 @@ static TR_AAA_SERVER *tr_cfg_parse_aaa_servers(TALLOC_CTX *mem_ctx, json_t *jaaa
   return aaa;
 }
 
-static TR_APC *tr_cfg_parse_apc(TALLOC_CTX *mem_ctx, json_t *japc, TR_CFG_RC *rc)
+static TR_APC *tr_cfg_parse_one_apc(TALLOC_CTX *mem_ctx, json_t *japc, TR_CFG_RC *rc)
 {
   TR_APC *apc=NULL;
   TR_NAME *name=NULL;
@@ -572,7 +686,7 @@ static TR_APC *tr_cfg_parse_apc(TALLOC_CTX *mem_ctx, json_t *japc, TR_CFG_RC *rc
   *rc = TR_CFG_SUCCESS;         /* presume success */
 
   if ((!japc) || (!rc) || (!json_is_string(japc))) {
-    tr_debug("tr_cfg_parse_apc: Bad parameters.");
+    tr_debug("tr_cfg_parse_one_apc: Bad parameters.");
     if (rc) 
       *rc = TR_CFG_BAD_PARAMS;
     return NULL;
@@ -580,14 +694,14 @@ static TR_APC *tr_cfg_parse_apc(TALLOC_CTX *mem_ctx, json_t *japc, TR_CFG_RC *rc
 
   apc=tr_apc_new(mem_ctx);
   if (apc==NULL) {
-    tr_debug("tr_cfg_parse_apc: Out of memory.");
+    tr_debug("tr_cfg_parse_one_apc: Out of memory.");
     *rc = TR_CFG_NOMEM;
     return NULL;
   }
 
   name=tr_new_name(json_string_value(japc));
   if (name==NULL) {
-    tr_debug("tr_cfg_parse_apc: No memory for APC name.");
+    tr_debug("tr_cfg_parse_one_apc: No memory for APC name.");
     tr_apc_free(apc);
     *rc = TR_CFG_NOMEM;
     return NULL;
@@ -597,6 +711,41 @@ static TR_APC *tr_cfg_parse_apc(TALLOC_CTX *mem_ctx, json_t *japc, TR_CFG_RC *rc
   return apc;
 }
 
+static TR_APC *tr_cfg_parse_apcs(TALLOC_CTX *mem_ctx, json_t *japcs, TR_CFG_RC *rc)
+{
+  TALLOC_CTX *tmp_ctx=talloc_new(NULL);
+  TR_APC *apcs=NULL;
+  TR_APC *new_apc=NULL;
+  int ii=0;
+  TR_CFG_RC call_rc=TR_CFG_ERROR;
+  
+  *rc = TR_CFG_SUCCESS;         /* presume success */
+
+  if ((!japcs) || (!rc) || (!json_is_array(japcs))) {
+    tr_debug("tr_cfg_parse_one_apc: Bad parameters.");
+    if (rc) 
+      *rc = TR_CFG_BAD_PARAMS;
+    return NULL;
+  }
+
+  for (ii=0; ii<json_array_size(japcs); ii++) {
+    new_apc=tr_cfg_parse_one_apc(tmp_ctx, json_array_get(japcs, ii), &call_rc);
+    if ((call_rc!=TR_CFG_SUCCESS) || (new_apc==NULL)) {
+      tr_debug("tr_cfg_parse_apcs: Error parsing APC %d.", ii+1);
+      *rc=TR_CFG_NOPARSE;
+      goto cleanup;
+    }
+    tr_apc_add(apcs, new_apc);
+  }
+
+  talloc_steal(mem_ctx, apcs);
+  *rc=TR_CFG_SUCCESS;
+
+ cleanup:
+  talloc_free(tmp_ctx);
+  return apcs;
+}
+
 static TR_NAME *tr_cfg_parse_name(TALLOC_CTX *mem_ctx, json_t *jname, TR_CFG_RC *rc)
 {
   TR_NAME *name=NULL;
@@ -642,6 +791,26 @@ static int tr_cfg_parse_shared_config(json_t *jsc, TR_CFG_RC *rc)
   return -1;
 }
 
+static TR_REALM_ORIGIN tr_cfg_realm_origin(json_t *jrealm)
+{
+  json_t *jremote=json_object_get(jrealm, "remote");
+  const char *s=NULL;
+
+  if (jremote==NULL)
+    return TR_REALM_LOCAL;
+  if (!json_is_string(jremote)) {
+    tr_warning("tr_cfg_realm_origin: \"remote\" is not a string, assuming this is a local realm.");
+    return TR_REALM_LOCAL;
+  }
+  s=json_string_value(jremote);
+  if (strcasecmp(s, "yes")==0)
+    return TR_REALM_REMOTE_INCOMPLETE;
+  else if (strcasecmp(s, "no")!=0)
+    tr_warning("tr_cfg_realm_origin: \"remote\" is neither 'yes' nor 'no', assuming this is a local realm.");
+
+  return TR_REALM_LOCAL;
+}
+
 /* Parse the identity provider object from a realm and fill in the given TR_IDP_REALM. */
 static TR_CFG_RC tr_cfg_parse_idp(TR_IDP_REALM *idp, json_t *jidp)
 {
@@ -649,11 +818,10 @@ static TR_CFG_RC tr_cfg_parse_idp(TR_IDP_REALM *idp, json_t *jidp)
   TR_APC *apcs=NULL;
   TR_AAA_SERVER *aaa=NULL;
   TR_CFG_RC rc=TR_CFG_ERROR;
-
+  
   if (jidp==NULL)
     goto cleanup;
 
-  idp->origin=TR_REALM_LOCAL; /* if we're parsing it from a config file, it's local */
   idp->shared_config=tr_cfg_parse_shared_config(json_object_get(jidp, "shared_config"), &rc);
   if (rc!=TR_CFG_SUCCESS) {
     tr_err("tr_cfg_parse_idp: missing or malformed shared_config specification");
@@ -661,37 +829,38 @@ static TR_CFG_RC tr_cfg_parse_idp(TR_IDP_REALM *idp, json_t *jidp)
     goto cleanup;
   }
 
-  apcs=tr_cfg_parse_apc(tmp_ctx, json_object_get(jidp, "apc"), &rc);
+  apcs=tr_cfg_parse_apcs(tmp_ctx, json_object_get(jidp, "apcs"), &rc);
   if ((rc!=TR_CFG_SUCCESS) || (apcs==NULL)) {
     tr_err("tr_cfg_parse_idp: unable to parse APC");
     rc=TR_CFG_NOPARSE;
     goto cleanup;
   }
-  tr_debug("tr_cfg_parse_idp: APC=\"%.*s\"",
-           apcs->id->len,
-           apcs->id->buf);
-
+  
   aaa=tr_cfg_parse_aaa_servers(idp, json_object_get(jidp, "aaa_servers"), &rc);
-  if ((rc!=TR_CFG_SUCCESS) || (aaa==NULL)) {
+  if (rc!=TR_CFG_SUCCESS) {
     tr_err("tr_cfg_parse_idp: unable to parse AAA servers");
     rc=TR_CFG_NOPARSE;
     goto cleanup;
   }
-
+  
+  tr_debug("tr_cfg_parse_idp: APC=\"%.*s\"",
+           apcs->id->len,
+           apcs->id->buf);
+  
   /* done, fill in the idp structures */
   idp->apcs=apcs;
   talloc_steal(idp, apcs);
   idp->aaa_servers=aaa;
   rc=TR_CFG_SUCCESS;
-
- cleanup:
+  
+cleanup:
   if (rc!=TR_CFG_SUCCESS) {
     if (apcs!=NULL)
       tr_apc_free(apcs);
     if (aaa!=NULL)
       tr_aaa_server_free(aaa);
   }
-
+  
   talloc_free(tmp_ctx);
   return rc;
 }
@@ -718,6 +887,13 @@ static TR_IDP_REALM *tr_cfg_parse_one_idp_realm(TALLOC_CTX *mem_ctx, json_t *jre
     goto cleanup;
   }
 
+  realm->origin=tr_cfg_realm_origin(jrealm);
+  if (realm->origin!=TR_REALM_LOCAL) {
+    tr_debug("tr_cfg_parse_one_idp_realm: realm is remote, should not have full IdP info.");
+    *rc=TR_CFG_NOPARSE;
+    goto cleanup;
+  }
+
   /* must have a name */
   realm->realm_id=tr_cfg_parse_name(realm,
                                     json_object_get(jrealm, "realm"),
@@ -737,20 +913,20 @@ static TR_IDP_REALM *tr_cfg_parse_one_idp_realm(TALLOC_CTX *mem_ctx, json_t *jre
     *rc=TR_CFG_NOPARSE;
     goto cleanup;
   }
-
+  
   *rc=TR_CFG_SUCCESS;
 
-  cleanup:
-    if (*rc==TR_CFG_SUCCESS)
-      talloc_steal(mem_ctx, realm);
-    else {
-      talloc_free(realm);
-      realm=NULL;
-    }
-
-    talloc_free(tmp_ctx);
-    return realm;
+cleanup:
+  if (*rc==TR_CFG_SUCCESS)
+    talloc_steal(mem_ctx, realm);
+  else {
+    talloc_free(realm);
+    realm=NULL;
   }
+  
+  talloc_free(tmp_ctx);
+  return realm;
+}
 
   /* Determine whether the realm is an IDP realm */
 static int tr_cfg_is_idp_realm(json_t *jrealm)
@@ -762,6 +938,59 @@ static int tr_cfg_is_idp_realm(json_t *jrealm)
     return 0;
 }
 
+static TR_IDP_REALM *tr_cfg_parse_one_remote_realm(TALLOC_CTX *mem_ctx, json_t *jrealm, TR_CFG_RC *rc)
+{
+  TALLOC_CTX *tmp_ctx=talloc_new(NULL);
+  TR_IDP_REALM *realm=talloc(mem_ctx, TR_IDP_REALM);
+  
+  *rc=TR_CFG_ERROR; /* default to error if not set */
+
+  if ((!jrealm) || (!rc)) {
+    tr_err("tr_cfg_parse_one_remote_realm: Bad parameters.");
+    if (rc)
+      *rc=TR_CFG_BAD_PARAMS;
+    goto cleanup;
+  }
+
+  if (NULL==(realm=tr_idp_realm_new(tmp_ctx))) {
+    tr_err("tr_cfg_parse_one_remote_realm: could not allocate idp realm.");
+    *rc=TR_CFG_NOMEM;
+    goto cleanup;
+  }
+
+  /* must have a name */
+  realm->realm_id=tr_cfg_parse_name(realm,
+                                    json_object_get(jrealm, "realm"),
+                                    rc);
+  if ((*rc!=TR_CFG_SUCCESS) || (realm->realm_id==NULL)) {
+    tr_err("tr_cfg_parse_one_remote_realm: could not parse realm name");
+    *rc=TR_CFG_NOPARSE;
+    goto cleanup;
+  }
+  tr_debug("tr_cfg_parse_one_remote_realm: realm_id=\"%.*s\"",
+           realm->realm_id->len,
+           realm->realm_id->buf);
+
+  realm->origin=tr_cfg_realm_origin(jrealm);
+  *rc=TR_CFG_SUCCESS;
+
+cleanup:
+  if (*rc==TR_CFG_SUCCESS)
+    talloc_steal(mem_ctx, realm);
+  else {
+    talloc_free(realm);
+    realm=NULL;
+  }
+  
+  talloc_free(tmp_ctx);
+  return realm;
+}
+
+static int tr_cfg_is_remote_realm(json_t *jrealm)
+{
+  return (tr_cfg_realm_origin(jrealm)!=TR_REALM_LOCAL);
+}
+
 /* Parse any idp realms in the j_realms object. Ignores other realm types. */
 static TR_IDP_REALM *tr_cfg_parse_idp_realms(TALLOC_CTX *mem_ctx, json_t *jrealms, TR_CFG_RC *rc)
 {
@@ -787,7 +1016,15 @@ static TR_IDP_REALM *tr_cfg_parse_idp_realms(TALLOC_CTX *mem_ctx, json_t *jrealm
         *rc=TR_CFG_NOPARSE;
         goto cleanup;
       }
-      realms=tr_idp_realm_add(realms, new_realm);
+      tr_idp_realm_add(realms, new_realm);
+    } else if (tr_cfg_is_remote_realm(this_jrealm)) {
+      new_realm=tr_cfg_parse_one_remote_realm(tmp_ctx, this_jrealm, rc);
+      if ((*rc)!=TR_CFG_SUCCESS) {
+        tr_err("tr_cfg_parse_idp_realms: error decoding remote realm entry %d", ii+1);
+        *rc=TR_CFG_NOPARSE;
+        goto cleanup;
+      }
+      tr_idp_realm_add(realms, new_realm);
     }
   }
   
@@ -799,41 +1036,207 @@ cleanup:
   return realms;
 }
 
-static TR_CFG_RC tr_cfg_parse_gss_names(TR_RP_CLIENT *client, json_t *jgss_names)
+static TR_GSS_NAMES *tr_cfg_parse_gss_names(TALLOC_CTX *mem_ctx, json_t *jgss_names, TR_CFG_RC *rc)
 {
+  TALLOC_CTX *tmp_ctx=talloc_new(NULL);
+  TR_GSS_NAMES *gn=NULL;
   json_t *jname=NULL;
   int ii=0;
   TR_NAME *name=NULL;
 
-  if ((client==NULL) || (jgss_names==NULL)) {
+  if ((rc==NULL) || (jgss_names==NULL)) {
     tr_err("tr_cfg_parse_gss_names: Bad parameters.");
-    return TR_CFG_BAD_PARAMS;
+    *rc=TR_CFG_BAD_PARAMS;
+
   }
 
   if (!json_is_array(jgss_names)) {
     tr_err("tr_cfg_parse_gss_names: gss_names not an array.");
-    return TR_CFG_NOPARSE;
+    *rc=TR_CFG_NOPARSE;
+    goto cleanup;
   }
 
+  gn=tr_gss_names_new(tmp_ctx);
   for (ii=0; ii<json_array_size(jgss_names); ii++) {
     jname=json_array_get(jgss_names, ii);
     if (!json_is_string(jname)) {
       tr_err("tr_cfg_parse_gss_names: Encountered non-string gss name.");
-      return TR_CFG_NOPARSE;
+      *rc=TR_CFG_NOPARSE;
+      goto cleanup;
     }
+
     name=tr_new_name(json_string_value(jname));
     if (name==NULL) {
       tr_err("tr_cfg_parse_gss_names: Out of memory allocating gss name.");
-      return TR_CFG_NOMEM;
+      *rc=TR_CFG_NOMEM;
+      goto cleanup;
     }
-    if (tr_rp_client_add_gss_name(client, name)!=0) {
+
+    if (tr_gss_names_add(gn, name)!=0) {
       tr_free_name(name);
       tr_err("tr_cfg_parse_gss_names: Unable to add gss name to RP client.");
-      return TR_CFG_ERROR;
+      *rc=TR_CFG_ERROR;
+      goto cleanup;
     }
   }
 
-  return TR_CFG_SUCCESS;
+  talloc_steal(mem_ctx, gn);
+  *rc=TR_CFG_SUCCESS;
+
+ cleanup:
+  talloc_free(tmp_ctx);
+  if ((*rc!=TR_CFG_SUCCESS) && (gn!=NULL))
+    gn=NULL;
+  return gn;
+}
+
+/* default filter accepts realm and *.realm */
+static TR_FILTER *tr_cfg_default_filter(TALLOC_CTX *mem_ctx, TR_NAME *realm, TR_CFG_RC *rc)
+{
+  TALLOC_CTX *tmp_ctx=talloc_new(NULL);
+  TR_FILTER *filt=NULL;
+  TR_CONSTRAINT *cons=NULL;
+  TR_NAME *name=NULL;
+  TR_NAME *n_prefix=tr_new_name("*.");
+  TR_NAME *n_rp_realm_1=tr_new_name("rp_realm");
+  TR_NAME *n_rp_realm_2=tr_new_name("rp_realm");
+  TR_NAME *n_domain=tr_new_name("domain");
+  TR_NAME *n_realm=tr_new_name("realm");
+  
+
+  if ((realm==NULL) || (rc==NULL)) {
+    tr_debug("tr_cfg_default_filter: invalid arguments.");
+    if (rc!=NULL)
+      *rc=TR_CFG_BAD_PARAMS;
+    goto cleanup;
+  }
+
+  if ((n_prefix==NULL) ||
+      (n_rp_realm_1==NULL) ||
+      (n_rp_realm_2==NULL) ||
+      (n_domain==NULL) ||
+      (n_realm==NULL)) {
+    tr_debug("tr_cfg_default_filter: 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.");
+    *rc=TR_CFG_NOMEM;
+    goto cleanup;
+  }
+  tr_filter_set_type(filt, TR_FILTER_TYPE_TID_INCOMING);
+  filt->lines[0]=tr_fline_new(filt);
+  if (filt->lines[0]==NULL) {
+    tr_debug("tr_cfg_default_filter: could not allocate filter line.");
+    *rc=TR_CFG_NOMEM;
+    goto cleanup;
+  }
+
+  filt->lines[0]->action=TR_FILTER_ACTION_ACCEPT;
+  filt->lines[0]->specs[0]=tr_fspec_new(filt->lines[0]);
+  filt->lines[0]->specs[0]->field=n_rp_realm_1;
+  n_rp_realm_1=NULL; /* we don't own this name any more */
+
+  name=tr_dup_name(realm);
+  if (name==NULL) {
+    tr_debug("tr_cfg_default_filter: could not allocate realm name.");
+    *rc=TR_CFG_NOMEM;
+    goto cleanup;
+  }
+  tr_fspec_set_match(filt->lines[0]->specs[0], name);
+  name=NULL; /* we no longer own the name */
+
+  /* now do the wildcard name */
+  filt->lines[0]->specs[1]=tr_fspec_new(filt->lines[0]);
+  filt->lines[0]->specs[1]->field=n_rp_realm_2;
+  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.");
+    *rc=TR_CFG_NOMEM;
+    goto cleanup;
+  }
+
+  tr_fspec_set_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.");
+    *rc=TR_CFG_NOMEM;
+    goto cleanup;
+  }
+
+  cons->type=n_domain;
+  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.");
+    *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.");
+    *rc=TR_CFG_NOMEM;
+    goto cleanup;
+  }
+  cons->matches[1]=name;
+  name=NULL;
+  filt->lines[0]->domain_cons=cons;
+
+
+  /* realm constraint */
+  if (NULL==(cons=tr_constraint_new(filt->lines[0]))) {
+    tr_debug("tr_cfg_default_filter: could not allocate realm constraint.");
+    *rc=TR_CFG_NOMEM;
+    goto cleanup;
+  }
+
+  cons->type=n_realm;
+  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.");
+    *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.");
+    *rc=TR_CFG_NOMEM;
+    goto cleanup;
+  }
+  cons->matches[1]=name;
+  name=NULL;
+  filt->lines[0]->realm_cons=cons;
+
+  talloc_steal(mem_ctx, filt);
+cleanup:
+  talloc_free(tmp_ctx);
+
+  if (*rc!=TR_CFG_SUCCESS)
+    filt=NULL;
+
+  if (n_prefix!=NULL)
+    tr_free_name(n_prefix);
+  if (n_rp_realm_1!=NULL)
+    tr_free_name(n_rp_realm_1);
+  if (n_rp_realm_2!=NULL)
+    tr_free_name(n_rp_realm_2);
+  if (n_realm!=NULL)
+    tr_free_name(n_realm);
+  if (n_domain!=NULL)
+    tr_free_name(n_domain);
+  if (name!=NULL)
+    tr_free_name(name);
+
+  return filt;
 }
 
 /* parses rp client */
@@ -843,6 +1246,9 @@ static TR_RP_CLIENT *tr_cfg_parse_one_rp_client(TALLOC_CTX *mem_ctx, json_t *jre
   TR_RP_CLIENT *client=NULL;
   TR_CFG_RC call_rc=TR_CFG_ERROR;
   TR_FILTER *new_filt=NULL;
+  TR_NAME *realm=NULL;
+  json_t *jfilt=NULL;
+  json_t *jrealm_id=NULL;
 
   *rc=TR_CFG_ERROR; /* default to error if not set */
 
@@ -853,13 +1259,28 @@ static TR_RP_CLIENT *tr_cfg_parse_one_rp_client(TALLOC_CTX *mem_ctx, json_t *jre
     goto cleanup;
   }
 
+  if ((NULL==(jrealm_id=json_object_get(jrealm, "realm"))) || (!json_is_string(jrealm_id))) {
+    tr_debug("tr_cfg_parse_one_rp_client: no realm ID found.");
+    *rc=TR_CFG_BAD_PARAMS;
+    goto cleanup;
+  } 
+
+  tr_debug("tr_cfg_parse_one_rp_client: realm_id=\"%s\"", json_string_value(jrealm_id));
+  realm=tr_new_name(json_string_value(jrealm_id));
+  if (realm==NULL) {
+    tr_err("tr_cfg_parse_one_rp_client: could not allocate realm ID.");
+    *rc=TR_CFG_NOMEM;
+    goto cleanup;
+  }
+
   if (NULL==(client=tr_rp_client_new(tmp_ctx))) {
     tr_err("tr_cfg_parse_one_rp_client: could not allocate rp client.");
     *rc=TR_CFG_NOMEM;
     goto cleanup;
   }
 
-  call_rc=tr_cfg_parse_gss_names(client, json_object_get(jrealm, "gss_names"));
+  client->gss_names=tr_cfg_parse_gss_names(client, json_object_get(jrealm, "gss_names"), &call_rc);
+
   if (call_rc!=TR_CFG_SUCCESS) {
     tr_err("tr_cfg_parse_one_rp_client: could not parse gss_names.");
     *rc=TR_CFG_NOPARSE;
@@ -867,17 +1288,31 @@ static TR_RP_CLIENT *tr_cfg_parse_one_rp_client(TALLOC_CTX *mem_ctx, json_t *jre
   }
 
   /* parse filters */
-  new_filt=tr_cfg_parse_filters(tmp_ctx, json_object_get(jrealm, "filters"), &call_rc);
-  if (call_rc!=TR_CFG_SUCCESS) {
-    tr_err("tr_cfg_parse_one_rp_client: could not parse filters.");
-    *rc=TR_CFG_NOPARSE;
-    goto cleanup;
+  jfilt=json_object_get(jrealm, "filters");
+  if (jfilt!=NULL) {
+    new_filt=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;
+      goto cleanup;
+    }
+  } 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);
+    if (call_rc!=TR_CFG_SUCCESS) {
+      tr_err("tr_cfg_parse_one_rp_client: could not set default filters.");
+      *rc=TR_CFG_NOPARSE;
+      goto cleanup;
+    }
   }
 
   tr_rp_client_set_filter(client, new_filt);
   *rc=TR_CFG_SUCCESS;
 
   cleanup:
+  if (realm!=NULL)
+    tr_free_name(realm);
+
     if (*rc==TR_CFG_SUCCESS)
       talloc_steal(mem_ctx, client);
     else {
@@ -924,7 +1359,7 @@ static TR_RP_CLIENT *tr_cfg_parse_rp_clients(TALLOC_CTX *mem_ctx, json_t *jrealm
         *rc=TR_CFG_NOPARSE;
         goto cleanup;
       }
-      clients=tr_rp_client_add(clients, new_client);
+      tr_rp_client_add(clients, new_client);
     }
   }
   
@@ -956,10 +1391,15 @@ 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 *comms, TR_IDP_REALM *new_realms, TR_CFG_RC *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 */
@@ -986,9 +1426,9 @@ static TR_COMM *tr_cfg_comm_idp_update(TALLOC_CTX *mem_ctx, TR_COMM *comms, TR_I
         /* 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;
-        comm->id=tr_dup_name(apc->id);
+        tr_comm_set_id(comm, tr_dup_name(apc->id));
         tr_comm_add_idp_realm(comm, realm);
-        new_comms=tr_comm_add(new_comms, comm);
+        tr_comm_add(new_comms, comm);
       } else {
         /* add this realm to the comm */
         tr_comm_add_idp_realm(comm, realm);
@@ -997,13 +1437,13 @@ static TR_COMM *tr_cfg_comm_idp_update(TALLOC_CTX *mem_ctx, TR_COMM *comms, TR_I
   }
 
   /* we successfully built a list, add it to the other list */
-  comms=tr_comm_add(comms, new_comms);
+  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)
 {
@@ -1053,13 +1493,12 @@ cleanup:
   /* if we succeeded, link things to the configuration and move out of tmp context */
   if (retval==TR_CFG_SUCCESS) {
     if (new_idp_realms!=NULL) {
-      trc->idp_realms=tr_idp_realm_add(trc->idp_realms, new_idp_realms); /* fixes talloc contexts except for head*/
-      talloc_steal(trc, trc->idp_realms); /* make sure the head is in the right context */
-      trc->comms=tr_cfg_comm_idp_update(trc, trc->comms, new_idp_realms, &rc); /* put realm info in community table */
+      tr_idp_realm_add(trc->ctable->idp_realms, new_idp_realms); /* fixes talloc contexts except for head*/
+      talloc_steal(trc, trc->ctable->idp_realms); /* make sure the head is in the right context */
     }
 
     if (new_rp_clients!=NULL) {
-      trc->rp_clients=tr_rp_client_add(trc->rp_clients, new_rp_clients); /* fixes talloc contexts */
+      tr_rp_client_add(trc->rp_clients, new_rp_clients); /* fixes talloc contexts */
       talloc_steal(trc, trc->rp_clients); /* make sure head is in the right context */
     }
   }
@@ -1092,7 +1531,358 @@ static TR_CFG_RC tr_cfg_parse_local_orgs(TR_CFG *trc, json_t *jcfg)
 
   return TR_CFG_SUCCESS;
 }
+
+static TR_CFG_RC tr_cfg_parse_one_peer_org(TR_CFG *trc, json_t *jporg)
+{
+  TALLOC_CTX *tmp_ctx=talloc_new(NULL);
+  json_t *jhost=NULL;
+  json_t *jport=NULL;
+  json_t *jgss=NULL;
+  TRP_PEER *new_peer=NULL;
+  TR_GSS_NAMES *names=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");
+
+  if ((jhost==NULL) || (!json_is_string(jhost))) {
+    tr_err("tr_cfg_parse_one_peer_org: hostname not specified or not a string.");
+    rc=TR_CFG_NOPARSE;
+    goto cleanup;
+  }
+
+  if ((jport!=NULL) && (!json_is_number(jport))) {
+    /* note that not specifying the port is allowed, but if set it must be a number */
+    tr_err("tr_cfg_parse_one_peer_org: port is not a number.");
+    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;
+  }
+
+  new_peer=trp_peer_new(tmp_ctx);
+  if (new_peer==NULL) {
+    tr_err("tr_cfg_parse_one_peer_org: could not allocate new peer.");
+    rc=TR_CFG_NOMEM;
+    goto cleanup;
+  }
+
+  trp_peer_set_server(new_peer, json_string_value(jhost));
+  if (jport==NULL)
+    trp_peer_set_port(new_peer, TRP_PORT);
+  else
+    trp_peer_set_port(new_peer, json_integer_value(jport));
+
+  names=tr_cfg_parse_gss_names(tmp_ctx, jgss, &rc);
+  if (rc!=TR_CFG_SUCCESS) {
+    tr_err("tr_cfg_parse_one_peer_org: unable to parse gss names.");
+    rc=TR_CFG_NOPARSE;
+    goto cleanup;
+  }
+  trp_peer_set_gss_names(new_peer, names);
+
+  /* success! */
+  trp_ptable_add(trc->peers, new_peer);
+  rc=TR_CFG_SUCCESS;
+
+ cleanup:
+  talloc_free(tmp_ctx);
+  return rc;
+}
+
+/* Parse peer organizations, if present. Returns success if there are none. */
+static TR_CFG_RC tr_cfg_parse_peer_orgs(TR_CFG *trc, json_t *jcfg)
+{
+  json_t *jpeerorgs=NULL;
+  int ii=0;
+
+  jpeerorgs=json_object_get(jcfg, "peer_organizations");
+  if (jpeerorgs==NULL)
+    return TR_CFG_SUCCESS;
+
+  if (!json_is_array(jpeerorgs)) {
+    tr_err("tr_cfg_parse_peer_orgs: peer_organizations is not an array.");
+    return TR_CFG_NOPARSE;
+  }
+
+  for (ii=0; ii<json_array_size(jpeerorgs); ii++) {
+    if (tr_cfg_parse_one_peer_org(trc, json_array_get(jpeerorgs, ii))!=TR_CFG_SUCCESS) {
+      tr_err("tr_cfg_parse_peer_orgs: error parsing peer_organization %d.", ii+1);
+      return TR_CFG_NOPARSE;
+    }
+  }
+
+  return TR_CFG_SUCCESS;
+}
              
+static TR_CFG_RC tr_cfg_parse_default_servers (TR_CFG *trc, json_t *jcfg) 
+{
+  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;
+      }
+      tr_debug("tr_cfg_parse_default_servers: Default server configured: %s", ds->hostname->buf);
+      ds->next = trc->default_servers;
+      trc->default_servers = ds;
+    }
+  } 
+
+  tr_debug("tr_cfg_parse_default_servers: Finished (rc=%d)", rc);
+  return rc;
+}
+
+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;
+
+  if ((!trc) ||
+      (!jidps) ||
+      (!json_is_array(jidps))) {
+    if (rc)
+      *rc = TR_CFG_BAD_PARAMS;
+    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);
+    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)));
+      *rc=TR_CFG_ERROR;
+      return;
+    }
+    tr_comm_add_idp_realm(trc->ctable, comm, found_idp, 0, NULL, NULL); /* no provenance, never expires */
+  }
+
+  *rc=TR_CFG_SUCCESS;
+  return;
+}
+
+static void tr_cfg_parse_comm_rps(TR_CFG *trc, json_t *jrps, TR_COMM *comm, TR_CFG_RC *rc)
+{
+  TR_RP_REALM *found_rp=NULL;
+  TR_RP_REALM *new_rp=NULL;
+  TR_NAME *rp_name=NULL;
+  const char *s=NULL;
+  int ii=0;
+
+  if ((!trc) ||
+      (!jrps) ||
+      (!json_is_array(jrps))) {
+    if (rc)
+      *rc = TR_CFG_BAD_PARAMS;
+    return;
+  }
+
+  for (ii=0; ii<json_array_size(jrps); ii++) {
+    /* get the RP name as a string */
+    s=json_string_value(json_array_get(jrps, ii));
+    if (s==NULL) {
+      tr_notice("tr_cfg_parse_comm_rps: null RP found in community %.*s, ignoring.",
+                tr_comm_get_id(comm)->len, tr_comm_get_id(comm)->buf);
+      continue;
+    }
+
+    /* convert string to TR_NAME */
+    rp_name=tr_new_name(s);
+    if (rp_name==NULL) {
+      tr_err("tr_cfg_parse_comm_rps: unable to allocate RP name for %s in community %.*s.",
+             s, tr_comm_get_id(comm)->len, tr_comm_get_id(comm)->buf);
+    }
+
+    /* see if we already have this RP in this community */
+    found_rp=tr_comm_find_rp(trc->ctable, comm, rp_name);
+    if (found_rp!=NULL) {
+      tr_notice("tr_cfg_parse_comm_rps: RP %s repeated in community %.*s.",
+                s, tr_comm_get_id(comm)->len, tr_comm_get_id(comm)->buf);
+      tr_free_name(rp_name);
+      continue;
+    }
+
+    /* Add the RP to the community, first see if we have the RP in any community */
+    found_rp=tr_rp_realm_lookup(trc->ctable->rp_realms, rp_name);
+    if (found_rp!=NULL) {
+      tr_debug("tr_cfg_parse_comm_rps: RP realm %s already exists.", s);
+      new_rp=found_rp; /* use it rather than creating a new realm record */
+    } else {
+      new_rp=tr_rp_realm_new(NULL);
+      if (new_rp==NULL) {
+        tr_err("tr_cfg_parse_comm_rps: unable to allocate RP record for %s in community %.*s.",
+               s, tr_comm_get_id(comm)->len, tr_comm_get_id(comm)->buf);
+      }
+      tr_debug("tr_cfg_parse_comm_rps: setting name to %s", rp_name->buf);
+      tr_rp_realm_set_id(new_rp, rp_name);
+      rp_name=NULL; /* rp_name no longer belongs to us */
+      tr_rp_realm_add(trc->ctable->rp_realms, new_rp);
+      talloc_steal(trc->ctable, trc->ctable->rp_realms); /* make sure head is in the right context */
+    }
+    tr_comm_add_rp_realm(trc->ctable, comm, new_rp, 0, NULL, NULL);
+  }
+}
+
+static TR_COMM *tr_cfg_parse_one_comm (TALLOC_CTX *mem_ctx, TR_CFG *trc, json_t *jcomm, TR_CFG_RC *rc)
+{
+  TALLOC_CTX *tmp_ctx=talloc_new(NULL);
+  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)) {
+    tr_debug("tr_cfg_parse_one_comm: Bad parameters.");
+    if (rc)
+      *rc = TR_CFG_BAD_PARAMS;
+    goto cleanup;
+  }
+
+  comm=tr_comm_new(tmp_ctx);
+  if (comm==NULL) {
+    tr_crit("tr_cfg_parse_one_comm: Out of memory.");
+    *rc = TR_CFG_NOMEM;
+    goto cleanup;
+  }
+
+
+  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))) {
+    tr_debug("tr_cfg_parse_one_comm: Error parsing Communities configuration.");
+    *rc = TR_CFG_NOPARSE;
+    comm=NULL;
+    goto cleanup;
+  }
+
+  tr_comm_set_id(comm, tr_new_name(json_string_value(jid)));
+  if (NULL == tr_comm_get_id(comm)) {
+    tr_debug("tr_cfg_parse_one_comm: No memory for community id.");
+    *rc = TR_CFG_NOMEM;
+    comm=NULL;
+    goto cleanup;
+  }
+
+  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))) {
+      tr_debug("tr_cfg_parse_one_comm: Can't parse APCs for COI %s.",
+               tr_comm_get_id(comm)->buf);
+      comm=NULL;
+      goto cleanup;
+    }
+  } else {
+    tr_debug("tr_cfg_parse_one_comm: Invalid community type, comm = %s, type = %s",
+             tr_comm_get_id(comm)->buf, json_string_value(jtype));
+    *rc = TR_CFG_NOPARSE;
+    comm=NULL;
+    goto cleanup;
+  }
+
+  tr_cfg_parse_comm_idps(trc, jidps, comm, rc);
+  if (TR_CFG_SUCCESS != *rc) {
+    tr_debug("tr_cfg_parse_one_comm: Can't parse IDP realms for comm %s.",
+             tr_comm_get_id(comm)->buf);
+    comm=NULL;
+    goto cleanup;
+  }
+
+  tr_cfg_parse_comm_rps(trc, jrps, comm, rc);
+  if (TR_CFG_SUCCESS != *rc) {
+    tr_debug("tr_cfg_parse_one_comm: Can't parse RP realms for comm %s .",
+             tr_comm_get_id(comm)->buf);
+    comm=NULL;
+    goto cleanup;
+  }
+
+  if (TR_COMM_APC == comm->type) {
+    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;
+    }
+  }
+
+cleanup:
+  if (comm!=NULL)
+    talloc_steal(mem_ctx, comm);
+  talloc_free(tmp_ctx);
+  return comm;
+}
+
+static TR_CFG_RC tr_cfg_parse_comms (TR_CFG *trc, json_t *jcfg) 
+{
+  json_t *jcomms = NULL;
+  TR_CFG_RC rc = TR_CFG_SUCCESS;
+  TR_COMM *comm = NULL;
+  int i = 0;
+
+  if ((!trc) || (!jcfg)) {
+    tr_debug("tr_cfg_parse_comms: Bad Parameters.");
+    return TR_CFG_BAD_PARAMS;
+  }
+
+  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(NULL, /* TODO: use a talloc context */
+                                                trc, 
+                                                json_array_get(jcomms, i), 
+                                               &rc))) {
+        return rc;
+      }
+      tr_debug("tr_cfg_parse_comms: Community configured: %s.",
+               tr_comm_get_id(comm)->buf);
+
+      tr_comm_table_add_comm(trc->ctable, comm);
+    }
+  }
+  tr_debug("tr_cfg_parse_comms: Finished (rc=%d)", rc);
+  return rc;
+}
 
 TR_CFG_RC tr_cfg_validate(TR_CFG *trc)
 {
@@ -1112,12 +1902,12 @@ TR_CFG_RC tr_cfg_validate(TR_CFG *trc)
     rc = TR_CFG_ERROR;
   }
 
-  if (NULL == trc->comms) {
+  if (0==tr_comm_table_size(trc->ctable)) {
     tr_debug("tr_cfg_validate: Error: No Communities configured");
     rc = TR_CFG_ERROR;
   }
 
-  if ((NULL == trc->default_servers) && (NULL == trc->idp_realms)) {
+  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;
   }
@@ -1132,6 +1922,14 @@ 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",
+          label,
+          rc->line,
+          rc->text);
+}
+
 TR_CFG_RC tr_cfg_parse_one_config_file(TR_CFG *cfg, const char *file_with_path)
 {
   json_t *jcfg=NULL;
@@ -1140,31 +1938,26 @@ TR_CFG_RC tr_cfg_parse_one_config_file(TR_CFG *cfg, const char *file_with_path)
 
   if (NULL==(jcfg=json_load_file(file_with_path, 
                                  JSON_DISABLE_EOF_CHECK, &rc))) {
-    tr_debug("tr_parse_config: Error parsing config file %s.", 
+    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;
   }
 
   // Look for serial number and log it if it exists
   if (NULL!=(jser=json_object_get(jcfg, "serial_number"))) {
     if (json_is_number(jser)) {
-      tr_notice("tr_read_config: Attempting to load revision %" JSON_INTEGER_FORMAT " of '%s'.",
+      tr_notice("tr_parse_one_config_file: Attempting to load revision %" JSON_INTEGER_FORMAT " of '%s'.",
                 json_integer_value(jser),
                 file_with_path);
     }
   }
 
-  /* TODO: parse using the new functions */
-#if 0
   if ((TR_CFG_SUCCESS != tr_cfg_parse_internal(cfg, jcfg)) ||
-      (TR_CFG_SUCCESS != tr_cfg_parse_rp_clients(cfg, jcfg)) ||
-      (TR_CFG_SUCCESS != tr_cfg_parse_idp_realms(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))) {
-
-  }
-#endif
-  if (TR_CFG_SUCCESS != tr_cfg_parse_local_orgs(cfg, jcfg))
+      (TR_CFG_SUCCESS != tr_cfg_parse_comms(cfg, jcfg)))
     return TR_CFG_ERROR;
 
   return TR_CFG_SUCCESS;
@@ -1174,9 +1967,6 @@ TR_CFG_RC tr_cfg_parse_one_config_file(TR_CFG *cfg, const char *file_with_path)
 TR_CFG_RC tr_parse_config(TR_CFG_MGR *cfg_mgr, const char *config_dir, int n, struct dirent **cfg_files)
 {
   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
-  json_t *jcfg;
-  json_t *jser;
-  json_error_t rc;
   char *file_with_path;
   int ii;
   TR_CFG_RC cfg_rc=TR_CFG_ERROR;
@@ -1194,6 +1984,8 @@ 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 */
@@ -1205,7 +1997,7 @@ TR_CFG_RC tr_parse_config(TR_CFG_MGR *cfg_mgr, const char *config_dir, int n, st
     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);
+      tr_crit("tr_parse_config: Error parsing %s", file_with_path);
       goto cleanup;
     }
     talloc_free(file_with_path); /* done with filename */
@@ -1238,7 +2030,7 @@ TR_IDP_REALM *tr_cfg_find_idp (TR_CFG *tr_cfg, TR_NAME *idp_id, TR_CFG_RC *rc)
     return NULL;
   }
 
-  for (cfg_idp = tr_cfg->idp_realms; NULL != cfg_idp; cfg_idp = cfg_idp->next) {
+  for (cfg_idp = tr_cfg->ctable->idp_realms; NULL != cfg_idp; cfg_idp = cfg_idp->next) {
     if (!tr_name_cmp (idp_id, cfg_idp->realm_id)) {
       tr_debug("tr_cfg_find_idp: Found %s.", idp_id->buf);
       return cfg_idp;
@@ -1251,7 +2043,6 @@ TR_IDP_REALM *tr_cfg_find_idp (TR_CFG *tr_cfg, TR_NAME *idp_id, TR_CFG_RC *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;
 
   if ((!tr_cfg) || (!rp_gss)) {
     if (rc)
@@ -1260,11 +2051,9 @@ TR_RP_CLIENT *tr_cfg_find_rp (TR_CFG *tr_cfg, TR_NAME *rp_gss, TR_CFG_RC *rc)
   }
 
   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])) {
-        tr_debug("tr_cfg_find_rp: Found %s.", rp_gss->buf);
-        return cfg_rp;
-      }
+    if (tr_gss_names_matches(cfg_rp->gss_names, rp_gss)) {
+      tr_debug("tr_cfg_find_rp: Found %s.", rp_gss->buf);
+      return cfg_rp;
     }
   }
   /* if we didn't find one, return NULL */