Merge pull request #48 from painless-security/jennifer/monitoring
authormrw42 <margaret@painless-security.com>
Thu, 3 May 2018 20:13:15 +0000 (16:13 -0400)
committerGitHub <noreply@github.com>
Thu, 3 May 2018 20:13:15 +0000 (16:13 -0400)
Monitoring interface and back end support (pull request 10)

33 files changed:
CMakeLists.txt
Makefile.am
common/tr_comm_encoders.c
common/tr_config.c
common/tr_config_comms.c [new file with mode: 0644]
common/tr_config_encoders.c [new file with mode: 0644]
common/tr_config_filters.c [new file with mode: 0644]
common/tr_config_internal.c
common/tr_config_orgs.c [new file with mode: 0644]
common/tr_config_realms.c [new file with mode: 0644]
common/tr_config_rp_clients.c [new file with mode: 0644]
common/tr_gss.c
common/tr_gss_names.c
common/tr_socket.c
configure.ac
include/mon_internal.h
include/tr_config.h
include/tr_gss.h
include/tr_gss_names.h
include/trust_router/tid.h
mon/mon_common.c
mon/mon_req_decode.c
mon/mon_resp_encode.c
mon/tests/test_mon_req_decode.c
mon/tests/test_mon_req_encode.c
mon/tests/test_mon_resp_encode.c
tid/tid_req.c
tr/tr_main.c
tr/tr_mon.c
tr/tr_tid.c
tr/tr_trp.c
trp/trp_route_encoders.c
trust_router.spec

index c031f68..4ae8dba 100644 (file)
@@ -96,7 +96,7 @@ set(SOURCE_FILES
     trp/trp_upd.c
     trp/trpc.c
     trp/trps.c include/tr_name_internal.h mon/mon_req.c mon/mon_req_encode.c mon/mon_req_decode.c
-        mon/mon_resp.c mon/mon_common.c mon/mon_resp_encode.c mon/mon_resp_decode.c tr/tr_mon.c mon/mons.c include/tr_socket.h common/tr_gss.c include/tr_gss.h common/tr_config_internal.c mon/mons_handlers.c include/mons_handlers.h tr/tr_tid_mons.c tr/tr_tid_mons.c trp/trp_route.c include/trp_route.h trp/trp_rtable_encoders.c trp/trp_route_encoders.c trp/trp_peer.c include/trp_peer.h trp/trp_peer_encoders.c trp/trp_ptable_encoders.c common/tr_idp_encoders.c common/tr_comm_encoders.c common/tr_rp_client.c include/tr_rp_client.h common/tr_rp_client_encoders.c common/tr_filter_encoders.c)
+        mon/mon_resp.c mon/mon_common.c mon/mon_resp_encode.c mon/mon_resp_decode.c tr/tr_mon.c mon/mons.c include/tr_socket.h common/tr_gss.c include/tr_gss.h common/tr_config_internal.c mon/mons_handlers.c include/mons_handlers.h tr/tr_tid_mons.c tr/tr_tid_mons.c trp/trp_route.c include/trp_route.h trp/trp_rtable_encoders.c trp/trp_route_encoders.c trp/trp_peer.c include/trp_peer.h trp/trp_peer_encoders.c trp/trp_ptable_encoders.c common/tr_idp_encoders.c common/tr_comm_encoders.c common/tr_rp_client.c include/tr_rp_client.h common/tr_rp_client_encoders.c common/tr_filter_encoders.c common/tr_config_encoders.c common/tr_config_filters.c common/tr_config_realms.c common/tr_config_rp_clients.c common/tr_config_orgs.c common/tr_config_comms.c)
 
 # Does not actually build!
 add_executable(trust_router ${SOURCE_FILES})
index 5ef3761..1c933d6 100644 (file)
@@ -47,9 +47,19 @@ trp/trp_rtable.c \
 trp/trp_rtable_encoders.c \
 trp/trp_req.c \
 trp/trp_upd.c \
-common/tr_config.c \
-common/tr_config_internal.c \
-common/tr_mq.c
+common/tr_mq.c \
+$(config_srcs)
+
+# configuration parsing sources
+config_srcs = \
+    common/tr_config.c \
+    common/tr_config_comms.c \
+    common/tr_config_encoders.c \
+    common/tr_config_filters.c \
+    common/tr_config_internal.c \
+    common/tr_config_orgs.c \
+    common/tr_config_realms.c \
+    common/tr_config_rp_clients.c
 
 # general monitoring message sources
 mon_srcs =                   \
index bd3278e..c33c604 100644 (file)
 #include <tr_util.h>
 #include <tr_debug.h>
 
-/**
- * Convert TR_NAME n to a JSON string, returning the empty string if n is null
- */
-static json_t *safe_name_to_json_string(TR_NAME *n)
-{
-  if (n)
-    return tr_name_to_json_string(n);
-  else
-    return json_string("");
-}
-
 static json_t *expiry_to_json_string(TR_COMM_MEMB *memb)
 {
   struct timespec ts_zero = {0, 0};
@@ -114,7 +103,7 @@ static json_t *tr_comm_memb_to_json(TR_COMM_MEMB *memb)
     OBJECT_SET_OR_FAIL(memb_json, "origin", json_string("file"));
   } else {
     OBJECT_SET_OR_FAIL(memb_json, "origin",
-                       safe_name_to_json_string(tr_comm_memb_get_origin(memb)));
+                       tr_name_to_json_string(tr_comm_memb_get_origin(memb)));
     OBJECT_SET_OR_FAIL(memb_json, "provenance",
                        provenance_to_json(memb));
     OBJECT_SET_OR_FAIL(memb_json, "expires",
@@ -190,9 +179,14 @@ static json_t *tr_comm_realms_to_json(TR_COMM_TABLE *ctable, TR_NAME *comm_name,
       realm_json = json_object();
       OBJECT_SET_OR_FAIL(realm_json, "realm",
                          tr_name_to_json_string(tr_realm_get_id(realm)));
-      memb = tr_comm_table_find_idp_memb(ctable,
-                                         tr_realm_get_id(realm),
-                                         comm_name);
+      memb = tr_comm_table_find_memb(ctable,
+                                     tr_realm_get_id(realm),
+                                     comm_name);
+      if (memb == NULL) {
+        /* This should not happen - there must be a matching membership if we
+         * believed the realm was in the community in the first place! */
+        goto cleanup;
+      }
       OBJECT_SET_OR_FAIL(realm_json, "sources",
                          tr_comm_memb_sources_to_json(memb));
       json_array_append_new(jarray, realm_json);
@@ -241,11 +235,14 @@ static json_t *tr_comm_to_json(TR_COMM_TABLE *ctable, TR_COMM *comm)
   }
   OBJECT_SET_OR_FAIL(comm_json, "name",
                      tr_name_to_json_string(tr_comm_get_id(comm)));
-  OBJECT_SET_OR_FAIL(comm_json, "owner_realm",
-                     safe_name_to_json_string(tr_comm_get_owner_realm(comm)));
-  OBJECT_SET_OR_FAIL(comm_json, "owner_contact",
-                     safe_name_to_json_string(tr_comm_get_owner_contact(comm)));
-
+  if (tr_comm_get_owner_realm(comm)) {
+    OBJECT_SET_OR_FAIL(comm_json, "owner_realm",
+                     tr_name_to_json_string(tr_comm_get_owner_realm(comm)));
+  }
+  if (tr_comm_get_owner_contact(comm)) {
+    OBJECT_SET_OR_FAIL(comm_json, "owner_contact",
+                       tr_name_to_json_string(tr_comm_get_owner_contact(comm)));
+  }
   OBJECT_SET_OR_FAIL(comm_json, "idp_realms",
                      tr_comm_realms_to_json(ctable, tr_comm_get_id(comm), TR_ROLE_IDP));
   OBJECT_SET_OR_FAIL(comm_json, "rp_realms",
index dc6f77b..8c0f577 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
@@ -125,1615 +125,68 @@ void tr_print_comm_rps(TR_COMM_TABLE *ctab, TR_COMM *comm)
   talloc_free(tmp_ctx);
 }
 
-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);
-      cfg=NULL;
-    }
-  }
-  return cfg;
-}
-
-void tr_cfg_free (TR_CFG *cfg)
-{
-  talloc_free(cfg);
-}
-
-TR_CFG_MGR *tr_cfg_mgr_new(TALLOC_CTX *mem_ctx)
-{
-  return talloc_zero(mem_ctx, TR_CFG_MGR);
-}
-
-void tr_cfg_mgr_free (TR_CFG_MGR *cfg_mgr) {
-  talloc_free(cfg_mgr);
-}
-
-TR_CFG_RC tr_apply_new_config (TR_CFG_MGR *cfg_mgr)
-{
-  /* 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;
-
-  if (cfg_mgr->active != NULL)
-    tr_cfg_free(cfg_mgr->active);
-
-  cfg_mgr->active = cfg_mgr->new;
-  cfg_mgr->new=NULL; /* only keep a single handle on the new configuration */
-
-  tr_log_threshold(cfg_mgr->active->internal->log_threshold);
-  tr_console_threshold(cfg_mgr->active->internal->console_threshold);
-
-  return TR_CFG_SUCCESS;
-}
-
-static TR_CONSTRAINT *tr_cfg_parse_one_constraint(TALLOC_CTX *mem_ctx, char *ctype, json_t *jc, TR_CFG_RC *rc)
-{
-  TR_CONSTRAINT *cons=NULL;
-  int i=0;
-
-  if ((!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)))) {
-    tr_err("tr_cfg_parse_one_constraint: config error.");
-    *rc=TR_CFG_NOPARSE;
-    return NULL;
-  }
-
-  if (NULL==(cons=tr_constraint_new(mem_ctx))) {
-    tr_debug("tr_cfg_parse_one_constraint: Out of memory (cons).");
-    *rc=TR_CFG_NOMEM;
-    return NULL;
-  }
-
-  if (NULL==(cons->type=tr_new_name(ctype))) {
-    tr_err("tr_cfg_parse_one_constraint: Out of memory (type).");
-    *rc=TR_CFG_NOMEM;
-    tr_constraint_free(cons);
-    return NULL;
-  }
-
-  for (i=0; i < json_array_size(jc); i++) {
-    cons->matches[i]=tr_new_name(json_string_value(json_array_get(jc, i)));
-    if (cons->matches[i]==NULL) {
-      tr_err("tr_cfg_parse_one_constraint: Out of memory (match %d).", i+1);
-      *rc=TR_CFG_NOMEM;
-      tr_constraint_free(cons);
-      return NULL;
-    }
-  }
-
-  return cons;
-}
-
-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 *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;
-    goto cleanup;
-  }
-
-  if (NULL == (filt = tr_filter_new(tmp_ctx))) {
-    tr_err("tr_cfg_parse_one_filter: Out of memory.");
-    *rc = TR_CFG_NOMEM;
-    goto cleanup;
-  }
-  tr_filter_set_type(filt, ftype);
-
-  /* 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;
-    goto cleanup;
-  }
-
-  /* For each entry in the filter... */
-  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;
-      goto cleanup;
-    }
-
-    if ((NULL == (jfspecs = json_object_get(jfline, "specs"))) ||
-        (!json_is_array(jfspecs)) ||
-        (0 == json_array_size(jfspecs))) {
-      tr_debug("tr_cfg_parse_one_filter: Error parsing filter specs.");
-      *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;
-      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;
-      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;
-      goto cleanup;
-    }
-
-    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;
-        goto cleanup;
-      } 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;
-        goto cleanup;
-      } 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))) {
-          tr_debug("tr_cfg_parse_one_filter: Error parsing realm constraint");
-          *rc = TR_CFG_NOPARSE;
-          goto cleanup;
-        }
-      }
-    }
-
-    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;
-        goto cleanup;
-      } 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;
-        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))) {
-          tr_debug("tr_cfg_parse_one_filter: Error parsing domain constraint");
-          *rc = TR_CFG_NOPARSE;
-          goto cleanup;
-        }
-      }
-    }
-
-    /*For each filter spec within the filter line... */
-    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 == (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 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]))) {
-        tr_debug("tr_cfg_parse_one_filter: Out of memory.");
-        *rc = TR_CFG_NOMEM;
-        goto cleanup;
-      }
-
-      /* fill in the field */
-      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;
-        goto cleanup;
-      }
-
-      /* fill in the matches */
-      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;
-      }
-    }
-  }
-
-  /* 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)
-    filt=NULL;
-  return filt;
-}
-
-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;
-
-  /* no filters */
-  if (jfilts==NULL) {
-    *rc=TR_CFG_SUCCESS;
-    goto cleanup;
-  }
-
-  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;
-    }
-  }
-
-  *rc=TR_CFG_SUCCESS;
-
- cleanup:
-  if (*rc==TR_CFG_SUCCESS)
-    talloc_steal(mem_ctx, filt_set);
-  else if (filt_set!=NULL) {
-    talloc_free(filt_set);
-    filt_set=NULL;
-  }
-
-  talloc_free(tmp_ctx);
-  return filt_set;
-}
-
-static TR_AAA_SERVER *tr_cfg_parse_one_aaa_server(TALLOC_CTX *mem_ctx, json_t *jaddr, TR_CFG_RC *rc)
-{
-  TR_AAA_SERVER *aaa = NULL;
-  TR_NAME *name=NULL;
-
-  if ((!jaddr) || (!json_is_string(jaddr))) {
-    tr_debug("tr_cfg_parse_one_aaa_server: Bad parameters.");
-    *rc = TR_CFG_BAD_PARAMS;
-    return NULL;
-  }
-
-  name=tr_new_name(json_string_value(jaddr));
-  if (name==NULL) {
-    tr_debug("tr_cfg_parse_one_aaa_server: Out of memory allocating hostname.");
-    *rc = TR_CFG_NOMEM;
-    return NULL;
-  }
-
-  aaa=tr_aaa_server_new(mem_ctx, name);
-  if (aaa==NULL) {
-    tr_free_name(name);
-    tr_debug("tr_cfg_parse_one_aaa_server: Out of memory allocating AAA server.");
-    *rc = TR_CFG_NOMEM;
-    return NULL;
-  }
-
-  return aaa;
-}
-
-static TR_AAA_SERVER *tr_cfg_parse_aaa_servers(TALLOC_CTX *mem_ctx, json_t *jaaas, TR_CFG_RC *rc) 
-{
-  TALLOC_CTX *tmp_ctx=NULL;
-  TR_AAA_SERVER *aaa = NULL;
-  TR_AAA_SERVER *temp_aaa = NULL;
-  int i = 0;
-
-  for (i = 0; i < json_array_size(jaaas); i++) {
-    /* rc gets set in here */
-    if (NULL == (temp_aaa = tr_cfg_parse_one_aaa_server(tmp_ctx, json_array_get(jaaas, i), rc))) {
-      talloc_free(tmp_ctx);
-      return NULL;
-    }
-    /* TBD -- IPv6 addresses */
-    //    tr_debug("tr_cfg_parse_aaa_servers: Configuring AAA Server: ip_addr = %s.", inet_ntoa(temp_aaa->aaa_server_addr));
-    temp_aaa->next = aaa;
-    aaa = temp_aaa;
-  }
-  tr_debug("tr_cfg_parse_aaa_servers: Finished (rc=%d)", *rc);
-
-  for (temp_aaa=aaa; temp_aaa!=NULL; temp_aaa=temp_aaa->next)
-    talloc_steal(mem_ctx, temp_aaa);
-  talloc_free(tmp_ctx);
-  return aaa;
-}
-
-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;
-  
-  *rc = TR_CFG_SUCCESS;         /* presume success */
-
-  if ((!japc) || (!rc) || (!json_is_string(japc))) {
-    tr_debug("tr_cfg_parse_one_apc: Bad parameters.");
-    if (rc) 
-      *rc = TR_CFG_BAD_PARAMS;
-    return NULL;
-  }
-
-  apc=tr_apc_new(mem_ctx);
-  if (apc==NULL) {
-    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_one_apc: No memory for APC name.");
-    tr_apc_free(apc);
-    *rc = TR_CFG_NOMEM;
-    return NULL;
-  }
-  tr_apc_set_id(apc, name); /* apc is now responsible for freeing the name */
-
-  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;
-  *rc=TR_CFG_ERROR;
-
-  if ((jname==NULL) || (!json_is_string(jname))) {
-    tr_err("tr_cfg_parse_name: name missing or not a string");
-    *rc=TR_CFG_BAD_PARAMS;
-    name=NULL;
-  } else {
-    name=tr_new_name(json_string_value(jname));
-    if (name==NULL) {
-      tr_err("tr_cfg_parse_name: could not allocate name");
-      *rc=TR_CFG_NOMEM;
-    } else {
-      *rc=TR_CFG_SUCCESS;
-    }
-  }
-  return name;  
-}
-
-static int tr_cfg_parse_shared_config(json_t *jsc, TR_CFG_RC *rc)
-{
-  const char *shared=NULL;
-  *rc=TR_CFG_SUCCESS;
-  
-  if ((jsc==NULL) ||
-      (!json_is_string(jsc)) ||
-      (NULL==(shared=json_string_value(jsc)))) {
-    *rc=TR_CFG_BAD_PARAMS;
-    return -1;
-  }
-
-  if (0==strcmp(shared, "no"))
-    return 0;
-  else if (0==strcmp(shared, "yes"))
-    return 1;
-
-  /* any other value is an error */
-  tr_debug("tr_cfg_parse_shared_config: invalid shared_config value \"%s\" (should be \"yes\" or \"no\")",
-           shared);
-  *rc=TR_CFG_NOPARSE;
-  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)
-{
-  TALLOC_CTX *tmp_ctx=talloc_new(NULL);
-  TR_APC *apcs=NULL;
-  TR_AAA_SERVER *aaa=NULL;
-  TR_CFG_RC rc=TR_CFG_ERROR;
-  
-  if (jidp==NULL)
-    goto cleanup;
-
-  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");
-    rc=TR_CFG_NOPARSE;
-    goto cleanup;
-  }
-
-  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;
-  }
-  
-  aaa=tr_cfg_parse_aaa_servers(idp, json_object_get(jidp, "aaa_servers"), &rc);
-  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:
-  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;
-}
-
-/* parses idp realm */
-static TR_IDP_REALM *tr_cfg_parse_one_idp_realm(TALLOC_CTX *mem_ctx, json_t *jrealm, TR_CFG_RC *rc)
-{
-  TALLOC_CTX *tmp_ctx=talloc_new(NULL);
-  TR_IDP_REALM *realm=NULL;
-  TR_CFG_RC call_rc=TR_CFG_ERROR;
-
-  *rc=TR_CFG_ERROR; /* default to error if not set */
-
-  if ((!jrealm) || (!rc)) {
-    tr_err("tr_cfg_parse_one_idp_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_idp_realm: could not allocate idp realm.");
-    *rc=TR_CFG_NOMEM;
-    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"),
-                                   &call_rc);
-  if ((call_rc!=TR_CFG_SUCCESS) || (realm->realm_id==NULL)) {
-    tr_err("tr_cfg_parse_one_idp_realm: could not parse realm name");
-    *rc=TR_CFG_NOPARSE;
-    goto cleanup;
-  }
-  tr_debug("tr_cfg_parse_one_idp_realm: realm_id=\"%.*s\"",
-           realm->realm_id->len,
-           realm->realm_id->buf);
-        
-  call_rc=tr_cfg_parse_idp(realm, json_object_get(jrealm, "identity_provider"));
-  if (call_rc!=TR_CFG_SUCCESS) {
-    tr_err("tr_cfg_parse_one_idp_realm: could not parse identity_provider.");
-    *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;
-}
-
-  /* Determine whether the realm is an IDP realm */
-static int tr_cfg_is_idp_realm(json_t *jrealm)
-{
-  /* If a realm spec contains an identity_provider, it's an IDP realm. */
-  if (NULL != json_object_get(jrealm, "identity_provider"))
-    return 1;
-  else
-    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)
-{
-  TALLOC_CTX *tmp_ctx=talloc_new(NULL);
-  TR_IDP_REALM *realms=NULL;
-  TR_IDP_REALM *new_realm=NULL;
-  json_t *this_jrealm=NULL;
-  int ii=0;
-
-  *rc=TR_CFG_ERROR;
-  if ((jrealms==NULL) || (!json_is_array(jrealms))) {
-    tr_err("tr_cfg_parse_idp_realms: realms not an array");
-    *rc=TR_CFG_BAD_PARAMS;
-    goto cleanup;
-  }
-
-  for (ii=0; ii<json_array_size(jrealms); ii++) {
-    this_jrealm=json_array_get(jrealms, ii);
-    if (tr_cfg_is_idp_realm(this_jrealm)) {
-      new_realm=tr_cfg_parse_one_idp_realm(tmp_ctx, this_jrealm, rc);
-      if ((*rc)!=TR_CFG_SUCCESS) {
-        tr_err("tr_cfg_parse_idp_realms: error decoding realm entry %d", ii+1);
-        *rc=TR_CFG_NOPARSE;
-        goto cleanup;
-      }
-      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);
-    }
-  }
-  
-  *rc=TR_CFG_SUCCESS;
-  talloc_steal(mem_ctx, realms);
-
-cleanup:
-  talloc_free(tmp_ctx);
-  return realms;
-}
-
-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 ((rc==NULL) || (jgss_names==NULL)) {
-    tr_err("tr_cfg_parse_gss_names: Bad parameters.");
-    *rc=TR_CFG_BAD_PARAMS;
-
-  }
-
-  if (!json_is_array(jgss_names)) {
-    tr_err("tr_cfg_parse_gss_names: gss_names not an array.");
-    *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.");
-      *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.");
-      *rc=TR_CFG_NOMEM;
-      goto cleanup;
-    }
-
-    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.");
-      *rc=TR_CFG_ERROR;
-      goto cleanup;
-    }
-  }
-
-  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_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("*.");
-  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_filters: 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_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_filters: could not allocate filter.");
-    *rc=TR_CFG_NOMEM;
-    goto cleanup;
-  }
-  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_filters: 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_filters: could not allocate realm name.");
-    *rc=TR_CFG_NOMEM;
-    goto cleanup;
-  }
-  tr_fspec_add_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_filters: could not allocate wildcard realm name.");
-    *rc=TR_CFG_NOMEM;
-    goto cleanup;
-  }
-
-  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_filters: 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_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_filters: 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_filters: 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_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_filters: 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;
-
-  /* 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);
-
-  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_set;
-}
-
-/* parses rp client */
-static TR_RP_CLIENT *tr_cfg_parse_one_rp_client(TALLOC_CTX *mem_ctx, json_t *jrealm, TR_CFG_RC *rc)
-{
-  TALLOC_CTX *tmp_ctx=talloc_new(NULL);
-  TR_RP_CLIENT *client=NULL;
-  TR_CFG_RC call_rc=TR_CFG_ERROR;
-  TR_FILTER_SET *new_filts=NULL;
-  TR_NAME *realm=NULL;
-  json_t *jfilt=NULL;
-  json_t *jrealm_id=NULL;
-
-  *rc=TR_CFG_ERROR; /* default to error if not set */
-
-  if ((!jrealm) || (!rc)) {
-    tr_err("tr_cfg_parse_one_rp_client: Bad parameters.");
-    if (rc)
-      *rc=TR_CFG_BAD_PARAMS;
-    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;
-  }
-
-  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;
-    goto cleanup;
-  }
-
-  /* parse filters */
-  jfilt=json_object_get(jrealm, "filters");
-  if (jfilt!=NULL) {
-    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;
-      goto cleanup;
-    }
-  } else {
-    tr_debug("tr_cfg_parse_one_rp_client: no filters specified, using default filters.");
-    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;
-      goto cleanup;
-    }
-  }
-
-  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;
-}
-
-  /* Determine whether the realm is an RP realm */
-static int tr_cfg_is_rp_realm(json_t *jrealm)
-{
-  /* Check that we have a gss name. */
-  if (NULL != json_object_get(jrealm, "gss_names"))
-    return 1;
-  else
-    return 0;
-}
-
-/* Parse any rp clients in the j_realms object. Ignores other realms. */
-static TR_RP_CLIENT *tr_cfg_parse_rp_clients(TALLOC_CTX *mem_ctx, json_t *jrealms, TR_CFG_RC *rc)
-{
-  TALLOC_CTX *tmp_ctx=talloc_new(NULL);
-  TR_RP_CLIENT *clients=NULL;
-  TR_RP_CLIENT *new_client=NULL;
-  json_t *this_jrealm=NULL;
-  int ii=0;
-
-  *rc=TR_CFG_ERROR;
-  if ((jrealms==NULL) || (!json_is_array(jrealms))) {
-    tr_err("tr_cfg_parse_rp_clients: realms not an array");
-    *rc=TR_CFG_BAD_PARAMS;
-    goto cleanup;
-  }
-
-  for (ii=0; ii<json_array_size(jrealms); ii++) {
-    this_jrealm=json_array_get(jrealms, ii);
-    if (tr_cfg_is_rp_realm(this_jrealm)) {
-      new_client=tr_cfg_parse_one_rp_client(tmp_ctx, this_jrealm, rc);
-      if ((*rc)!=TR_CFG_SUCCESS) {
-        tr_err("tr_cfg_parse_rp_clients: error decoding realm entry %d", ii+1);
-        *rc=TR_CFG_NOPARSE;
-        goto cleanup;
-      }
-      tr_rp_client_add(clients, new_client);
-    }
-  }
-  
-  *rc=TR_CFG_SUCCESS;
-  talloc_steal(mem_ctx, clients);
-
-cleanup:
-  talloc_free(tmp_ctx);
-  return clients;
-}
-
-/* takes a talloc context, but currently does not use it */
-static TR_NAME *tr_cfg_parse_org_name(TALLOC_CTX *mem_ctx, json_t *j_org, TR_CFG_RC *rc)
+static int tr_cfg_destructor(void *object)
 {
-  TR_NAME *name=NULL;
-
-  if ((j_org==NULL) || (rc==NULL) || (!json_is_string(j_org))) {
-    tr_debug("tr_cfg_parse_org_name: Bad parameters.");
-    if (rc!=NULL)
-      *rc = TR_CFG_BAD_PARAMS; /* fill in return value if we can */
-    return NULL;
-  }
-
-  name=tr_new_name(json_string_value(j_org));
-  if (name==NULL)
-    *rc=TR_CFG_NOMEM;
-  else
-    *rc=TR_CFG_SUCCESS;
-  return name;
-}
-
-static TR_CFG_RC tr_cfg_parse_one_local_org(TR_CFG *trc, json_t *jlorg)
-{
-  TALLOC_CTX *tmp_ctx=talloc_new(NULL);
-  TR_CFG_RC retval=TR_CFG_ERROR; /* our return code */
-  TR_CFG_RC rc=TR_CFG_ERROR; /* return code from functions we call */
-  TR_NAME *org_name=NULL;
-  json_t *j_org=NULL;
-  json_t *j_realms=NULL;
-  TR_IDP_REALM *new_idp_realms=NULL;
-  TR_RP_CLIENT *new_rp_clients=NULL;
-
-  tr_debug("tr_cfg_parse_one_local_org: parsing local organization");
-
-  /* get organization_name (optional) */
-  if (NULL==(j_org=json_object_get(jlorg, "organization_name"))) {
-    tr_debug("tr_cfg_parse_one_local_org: organization_name unspecified");
-  } else {
-    org_name=tr_cfg_parse_org_name(tmp_ctx, j_org, &rc);
-    if (rc==TR_CFG_SUCCESS) {
-      tr_debug("tr_cfg_parse_one_local_org: organization_name=\"%.*s\"",
-               org_name->len,
-               org_name->buf);
-      /* we don't actually do anything with this, but we could */
-      tr_free_name(org_name);
-      org_name=NULL; 
-    }
-  }
-
-  /* Now get realms. Allow this to be missing; even though that is a pointless organization entry,
-   * it's harmless. Report a warning because that might be unintentional. */
-  if (NULL==(j_realms=json_object_get(jlorg, "realms"))) {
-    tr_warning("tr_cfg_parse_one_local_org: warning - no realms in this local organization");
-  } else {
-    /* Allocate in the tmp_ctx so these will be cleaned up if we do not complete successfully. */
-    new_idp_realms=tr_cfg_parse_idp_realms(tmp_ctx, j_realms, &rc);
-    if (rc!=TR_CFG_SUCCESS)
-      goto cleanup;
-
-    new_rp_clients=tr_cfg_parse_rp_clients(tmp_ctx, j_realms, &rc);
-    if (rc!=TR_CFG_SUCCESS)
-      goto cleanup;
-  }
-  retval=TR_CFG_SUCCESS;
-  
-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) {
-      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) {
-      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 */
-    }
-  }
-
-  talloc_free(tmp_ctx);
-  return rc;
+  TR_CFG *cfg = talloc_get_type_abort(object, TR_CFG);
+  if (cfg->files)
+    g_array_unref(cfg->files);
+  return 0;
 }
-
-/* Parse local organizations if present. Returns success if there are none. On failure, the configuration is unreliable. */
-static TR_CFG_RC tr_cfg_parse_local_orgs(TR_CFG *trc, json_t *jcfg)
+TR_CFG *tr_cfg_new(TALLOC_CTX *mem_ctx)
 {
-  json_t *jlocorgs=NULL;
-  size_t ii=0;
-
-  jlocorgs=json_object_get(jcfg, "local_organizations");
-  if (jlocorgs==NULL)
-    return TR_CFG_SUCCESS;
-
-  if (!json_is_array(jlocorgs)) {
-    tr_err("tr_cfg_parse_local_orgs: local_organizations is not an array.");
-    return TR_CFG_NOPARSE;
-  }
-
-  for (ii=0; ii<json_array_size(jlocorgs); ii++) {
-    if (tr_cfg_parse_one_local_org(trc, json_array_get(jlocorgs, ii))!=TR_CFG_SUCCESS) {
-      tr_err("tr_cfg_parse_local_orgs: error parsing local_organization %d.", ii+1);
-      return TR_CFG_NOPARSE;
+  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;
     }
-  }
-
-  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;
-  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.");
-    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;
-  }
-
-  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.");
-    rc=TR_CFG_NOMEM;
-    goto cleanup;
-  }
-
-  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
-    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);
-
-  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;
+    cfg->files = g_array_new(FALSE, FALSE, sizeof(TR_CFG_FILE));
+    if (cfg->files == NULL) {
+      talloc_free(cfg);
+      return NULL;
     }
-    trp_peer_set_filters(new_peer, filt_set);
+    talloc_set_destructor((void *)cfg, tr_cfg_destructor);
   }
-
-  /* success! */
-  trp_ptable_add(trc->peers, new_peer);
-  rc=TR_CFG_SUCCESS;
-
- cleanup:
-  talloc_free(tmp_ctx);
-  return rc;
+  return cfg;
 }
 
-/* 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) 
+void tr_cfg_free (TR_CFG *cfg)
 {
-  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;
+  talloc_free(cfg);
 }
 
-static void tr_cfg_parse_comm_idps(TR_CFG *trc, json_t *jidps, TR_COMM *comm, TR_CFG_RC *rc)
+TR_CFG_MGR *tr_cfg_mgr_new(TALLOC_CTX *mem_ctx)
 {
-  TR_IDP_REALM *found_idp=NULL;
-  json_t *jidp_name=NULL;
-  TR_NAME *idp_name=NULL;
-  size_t ii = 0;
-
-  if ((!trc) ||
-      (!jidps) ||
-      (!json_is_array(jidps))) {
-    if (rc)
-      *rc = TR_CFG_BAD_PARAMS;
-    return;
-  }
-
-  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.", json_string_value(jidp_name));
-      *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;
+  return talloc_zero(mem_ctx, TR_CFG_MGR);
 }
 
-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);
-  }
+void tr_cfg_mgr_free (TR_CFG_MGR *cfg_mgr) {
+  talloc_free(cfg_mgr);
 }
 
-static TR_COMM *tr_cfg_parse_one_comm (TALLOC_CTX *mem_ctx, TR_CFG *trc, json_t *jcomm, TR_CFG_RC *rc)
+TR_CFG_RC tr_apply_new_config (TR_CFG_MGR *cfg_mgr)
 {
-  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;
-  }
+  /* 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;
 
-  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;
-  }
+  if (cfg_mgr->active != NULL)
+    tr_cfg_free(cfg_mgr->active);
 
-  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;
-  }
+  cfg_mgr->active = cfg_mgr->new;
+  cfg_mgr->new=NULL; /* only keep a single handle on the new configuration */
 
-  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)) {
-        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);
-      }
-    }
-  }
+  tr_log_threshold(cfg_mgr->active->internal->log_threshold);
+  tr_console_threshold(cfg_mgr->active->internal->console_threshold);
 
-cleanup:
-  if (comm!=NULL)
-    talloc_steal(mem_ctx, comm);
-  talloc_free(tmp_ctx);
-  return comm;
+  return TR_CFG_SUCCESS;
 }
 
-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)
 {
@@ -1784,7 +237,6 @@ static void tr_cfg_log_json_error(const char *label, json_error_t *rc)
 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, 
@@ -1795,16 +247,20 @@ static json_t *tr_cfg_parse_one_config_file(const char *file_with_path)
     return NULL;
   }
 
-  // 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'.",
-                json_integer_value(jser),
-                file_with_path);
+  return jcfg;
+}
+
+/* extract serial number */
+static json_int_t get_cfg_serial(json_t *jcfg)
+{
+  json_t *jser=NULL;
+
+  if (NULL != (jser = json_object_get(jcfg, "serial_number"))) {
+    if (json_is_integer(jser)) {
+      return json_integer_value(jser);
     }
   }
-
-  return jcfg;
+  return TR_CFG_INVALID_SERIAL;
 }
 
 /**
@@ -1829,11 +285,12 @@ static void tr_cfg_parse_free_jcfgs(unsigned int n_jcfgs, json_t **jcfgs)
  * @param cfg_files
  * @return
  */
-static json_t **tr_cfg_parse_config_files(TALLOC_CTX *mem_ctx, unsigned int n_files, char **files_with_paths)
+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);
@@ -1842,14 +299,28 @@ static json_t **tr_cfg_parse_config_files(TALLOC_CTX *mem_ctx, unsigned int n_fi
     goto cleanup;
   }
   for (ii=0; ii<n_files; ii++) {
-    jcfgs[ii]=tr_cfg_parse_one_config_file(files_with_paths[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", files_with_paths[ii]);
+      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;
     }
+
+    this_file->serial = get_cfg_serial(jcfgs[ii]);
+    if (this_file->serial != TR_CFG_INVALID_SERIAL) {
+      tr_notice("tr_parse_one_config_file: Attempting to load revision %"
+                    JSON_INTEGER_FORMAT
+                    " of '%s'.",
+                this_file->serial,
+                this_file->name);
+    } else {
+      tr_notice("tr_parse_one_config_file: Attempting to load '%s'.",
+                this_file->name);
+    }
   }
+
 cleanup:
   if (jcfgs)
     talloc_steal(mem_ctx, jcfgs); /* give this to the caller's context if we succeeded */
@@ -1899,9 +370,20 @@ static TR_CFG_RC tr_cfg_parse_helper(TR_CFG *cfg,
   return ret;
 }
 
+static void add_files(TR_CFG *cfg, unsigned int n, char **filenames)
+{
+  TR_CFG_FILE frec = {0};
+
+  while ((n--) > 0) {
+    frec.name = talloc_strdup(cfg, filenames[n]);
+    frec.serial = TR_CFG_INVALID_SERIAL;
+
+    g_array_append_val(cfg->files, frec);
+  }
+}
 
 /**
- *  Reads configuration files in config_dir ("" or "./" will use the current directory).
+ *  Read a list of configuration files
  *
  * @param cfg_mgr Configuration manager
  * @param n_files Number of entries in cfg_files
@@ -1928,8 +410,11 @@ TR_CFG_RC tr_parse_config(TR_CFG_MGR *cfg_mgr, unsigned int n_files, char **file
     goto cleanup;
   }
 
+  /* add the list of files to the config */
+  add_files(cfg_mgr->new, n_files, files_with_paths);
+
   /* first parse the json */
-  jcfgs=tr_cfg_parse_config_files(tmp_ctx, n_files, files_with_paths);
+  jcfgs=tr_cfg_parse_config_files(tmp_ctx, n_files, cfg_mgr->new->files);
   if (jcfgs==NULL) {
     cfg_rc=TR_CFG_NOPARSE;
     goto cleanup;
@@ -1964,49 +449,8 @@ cleanup:
   return cfg_rc;
 }
 
-TR_IDP_REALM *tr_cfg_find_idp (TR_CFG *tr_cfg, TR_NAME *idp_id, TR_CFG_RC *rc)
-{
-
-  TR_IDP_REALM *cfg_idp;
-
-  if ((!tr_cfg) || (!idp_id)) {
-    if (rc)
-      *rc = TR_CFG_BAD_PARAMS;
-    return NULL;
-  }
-
-  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;
-    }
-  }
-  /* if we didn't find one, return NULL */ 
-  return NULL;
-}
-
-TR_RP_CLIENT *tr_cfg_find_rp (TR_CFG *tr_cfg, TR_NAME *rp_gss, TR_CFG_RC *rc)
-{
-  TR_RP_CLIENT *cfg_rp;
-
-  if ((!tr_cfg) || (!rp_gss)) {
-    if (rc)
-      *rc = TR_CFG_BAD_PARAMS;
-    return NULL;
-  }
-
-  for (cfg_rp = tr_cfg->rp_clients; NULL != cfg_rp; cfg_rp = cfg_rp->next) {
-    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 */ 
-  return NULL;
-}
-
 static int is_cfg_file(const struct dirent *dent) {
-  int n;
+  size_t n;
 
   /* Only accept filenames ending in ".cfg" and starting with a character
    * other than an ASCII '.' */
diff --git a/common/tr_config_comms.c b/common/tr_config_comms.c
new file mode 100644 (file)
index 0000000..60b5f93
--- /dev/null
@@ -0,0 +1,375 @@
+/*
+ * Copyright (c) 2012-2018, JANET(UK)
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * 3. Neither the name of JANET(UK) nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#include <stdlib.h>
+#include <string.h>
+#include <jansson.h>
+#include <dirent.h>
+#include <talloc.h>
+
+#include <tr_cfgwatch.h>
+#include <tr_comm.h>
+#include <tr_config.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>
+
+#if JANSSON_VERSION_HEX < 0x020500
+#include "jansson_iterators.h"
+#endif
+
+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;
+  json_t *jidp_name=NULL;
+  TR_NAME *idp_name=NULL;
+  size_t ii = 0;
+
+  if ((!trc) ||
+      (!jidps) ||
+      (!json_is_array(jidps))) {
+    if (rc)
+      *rc = TR_CFG_BAD_PARAMS;
+    return;
+  }
+
+  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.", json_string_value(jidp_name));
+      *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)) {
+        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);
+      }
+    }
+  }
+
+cleanup:
+  if (comm!=NULL)
+    talloc_steal(mem_ctx, comm);
+  talloc_free(tmp_ctx);
+  return comm;
+}
+
+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_IDP_REALM *tr_cfg_find_idp (TR_CFG *tr_cfg, TR_NAME *idp_id, TR_CFG_RC *rc)
+{
+
+  TR_IDP_REALM *cfg_idp;
+
+  if ((!tr_cfg) || (!idp_id)) {
+    if (rc)
+      *rc = TR_CFG_BAD_PARAMS;
+    return NULL;
+  }
+
+  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;
+    }
+  }
+  /* if we didn't find one, return NULL */
+  return NULL;
+}
+
+TR_RP_CLIENT *tr_cfg_find_rp (TR_CFG *tr_cfg, TR_NAME *rp_gss, TR_CFG_RC *rc)
+{
+  TR_RP_CLIENT *cfg_rp;
+
+  if ((!tr_cfg) || (!rp_gss)) {
+    if (rc)
+      *rc = TR_CFG_BAD_PARAMS;
+    return NULL;
+  }
+
+  for (cfg_rp = tr_cfg->rp_clients; NULL != cfg_rp; cfg_rp = cfg_rp->next) {
+    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 */
+  return NULL;
+}
diff --git a/common/tr_config_encoders.c b/common/tr_config_encoders.c
new file mode 100644 (file)
index 0000000..47331d9
--- /dev/null
@@ -0,0 +1,103 @@
+/*
+ * Copyright (c) 2012, JANET(UK)
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * 3. Neither the name of JANET(UK) nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#include <jansson.h>
+#include <tr_config.h>
+
+/* helper for below */
+#define OBJECT_SET_OR_FAIL(jobj, key, val)     \
+do {                                           \
+  if (val)                                     \
+    json_object_set_new((jobj),(key),(val));   \
+  else                                         \
+    goto cleanup;                              \
+} while (0)
+
+#define ARRAY_APPEND_OR_FAIL(jary, val)        \
+do {                                           \
+  if (val)                                     \
+    json_array_append_new((jary),(val));       \
+  else                                         \
+    goto cleanup;                              \
+} while (0)
+
+static json_t *tr_cfg_file_to_json(TR_CFG_FILE *cfg_file)
+{
+  json_t *file_json = NULL;
+  json_t *retval = NULL;
+
+  file_json = json_object();
+  if (file_json == NULL)
+    goto cleanup;
+
+  OBJECT_SET_OR_FAIL(file_json, "name", json_string(cfg_file->name));
+  if (cfg_file->serial != TR_CFG_INVALID_SERIAL)
+    OBJECT_SET_OR_FAIL(file_json, "serial", json_integer(cfg_file->serial));
+
+  /* succeeded - set the return value and increment the reference count */
+  retval = file_json;
+  json_incref(retval);
+
+cleanup:
+  if (file_json)
+    json_decref(file_json);
+
+  return retval;
+}
+
+json_t *tr_cfg_files_to_json_array(TR_CFG *cfg)
+{
+  guint ii;
+  json_t *jarray = json_array();
+  json_t *retval = NULL;
+
+  if (jarray == NULL)
+    goto cleanup;
+
+  for (ii=0; ii<cfg->files->len; ii++) {
+    ARRAY_APPEND_OR_FAIL(jarray,
+                         tr_cfg_file_to_json(
+                             &g_array_index(cfg->files, TR_CFG_FILE, ii)));
+  }
+
+  /* success */
+  retval = jarray;
+  json_incref(retval);
+
+cleanup:
+  if (jarray)
+    json_decref(jarray);
+
+  return retval;
+}
diff --git a/common/tr_config_filters.c b/common/tr_config_filters.c
new file mode 100644 (file)
index 0000000..4035e68
--- /dev/null
@@ -0,0 +1,364 @@
+/*
+ * Copyright (c) 2012-2018, JANET(UK)
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * 3. Neither the name of JANET(UK) nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#include <stdlib.h>
+#include <string.h>
+#include <jansson.h>
+#include <dirent.h>
+#include <talloc.h>
+
+#include <tr_cfgwatch.h>
+#include <tr_comm.h>
+#include <tr_config.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>
+
+#if JANSSON_VERSION_HEX < 0x020500
+#include "jansson_iterators.h"
+#endif
+
+static TR_CONSTRAINT *tr_cfg_parse_one_constraint(TALLOC_CTX *mem_ctx, char *ctype, json_t *jc, TR_CFG_RC *rc)
+{
+  TR_CONSTRAINT *cons=NULL;
+  int i=0;
+
+  if ((!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)))) {
+    tr_err("tr_cfg_parse_one_constraint: config error.");
+    *rc=TR_CFG_NOPARSE;
+    return NULL;
+  }
+
+  if (NULL==(cons=tr_constraint_new(mem_ctx))) {
+    tr_debug("tr_cfg_parse_one_constraint: Out of memory (cons).");
+    *rc=TR_CFG_NOMEM;
+    return NULL;
+  }
+
+  if (NULL==(cons->type=tr_new_name(ctype))) {
+    tr_err("tr_cfg_parse_one_constraint: Out of memory (type).");
+    *rc=TR_CFG_NOMEM;
+    tr_constraint_free(cons);
+    return NULL;
+  }
+
+  for (i=0; i < json_array_size(jc); i++) {
+    cons->matches[i]=tr_new_name(json_string_value(json_array_get(jc, i)));
+    if (cons->matches[i]==NULL) {
+      tr_err("tr_cfg_parse_one_constraint: Out of memory (match %d).", i+1);
+      *rc=TR_CFG_NOMEM;
+      tr_constraint_free(cons);
+      return NULL;
+    }
+  }
+
+  return cons;
+}
+
+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 *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;
+    goto cleanup;
+  }
+
+  if (NULL == (filt = tr_filter_new(tmp_ctx))) {
+    tr_err("tr_cfg_parse_one_filter: Out of memory.");
+    *rc = TR_CFG_NOMEM;
+    goto cleanup;
+  }
+  tr_filter_set_type(filt, ftype);
+
+  /* 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;
+    goto cleanup;
+  }
+
+  /* For each entry in the filter... */
+  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;
+      goto cleanup;
+    }
+
+    if ((NULL == (jfspecs = json_object_get(jfline, "specs"))) ||
+        (!json_is_array(jfspecs)) ||
+        (0 == json_array_size(jfspecs))) {
+      tr_debug("tr_cfg_parse_one_filter: Error parsing filter specs.");
+      *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;
+      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;
+      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;
+      goto cleanup;
+    }
+
+    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;
+        goto cleanup;
+      } 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;
+        goto cleanup;
+      } 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))) {
+          tr_debug("tr_cfg_parse_one_filter: Error parsing realm constraint");
+          *rc = TR_CFG_NOPARSE;
+          goto cleanup;
+        }
+      }
+    }
+
+    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;
+        goto cleanup;
+      } 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;
+        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))) {
+          tr_debug("tr_cfg_parse_one_filter: Error parsing domain constraint");
+          *rc = TR_CFG_NOPARSE;
+          goto cleanup;
+        }
+      }
+    }
+
+    /*For each filter spec within the filter line... */
+    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 == (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 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]))) {
+        tr_debug("tr_cfg_parse_one_filter: Out of memory.");
+        *rc = TR_CFG_NOMEM;
+        goto cleanup;
+      }
+
+      /* fill in the field */
+      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;
+        goto cleanup;
+      }
+
+      /* fill in the matches */
+      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;
+      }
+    }
+  }
+
+  /* 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)
+    filt=NULL;
+  return filt;
+}
+
+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;
+
+  /* no filters */
+  if (jfilts==NULL) {
+    *rc=TR_CFG_SUCCESS;
+    goto cleanup;
+  }
+
+  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;
+    }
+  }
+
+  *rc=TR_CFG_SUCCESS;
+
+cleanup:
+  if (*rc==TR_CFG_SUCCESS)
+    talloc_steal(mem_ctx, filt_set);
+  else if (filt_set!=NULL) {
+    talloc_free(filt_set);
+    filt_set=NULL;
+  }
+
+  talloc_free(tmp_ctx);
+  return filt_set;
+}
index 82a57e2..d06dc51 100644 (file)
 #include <tr_cfgwatch.h>
 
 /**
+ * Parse a boolean
+ *
+ * If the key does not exist in the src object, returns success but does fill in *dest.
+ *
+ * @param src JSON object to pull a value from
+ * @param key key to pull
+ * @param dest (output) pointer to an allocated integer
+ * @return TR_CFG_SUCCESS or an error code
+ */
+static TR_CFG_RC tr_cfg_parse_boolean(json_t *src, const char *key, int *dest)
+{
+  json_t *jtmp;
+
+  /* Validate parameters */
+  if ((src == NULL) || (key == NULL) || (dest == NULL))
+    return TR_CFG_BAD_PARAMS;
+
+  /* See if we have a value for this key; do nothing if not */
+  jtmp = json_object_get(src, key);
+  if (jtmp) {
+    if (json_is_boolean(jtmp)) {
+      *dest = json_boolean_value(jtmp);
+    } else {
+      tr_debug("tr_cfg_parse_unsigned: Parsing error, %s is not a boolean.", key);
+      return TR_CFG_NOPARSE;
+    }
+  }
+
+  return TR_CFG_SUCCESS;
+}
+
+/**
  * Parse an unsigned integer
  *
  * If the key does not exist in the src object, returns success but does fill in *dest.
@@ -118,6 +150,7 @@ static void set_defaults(TR_CFG_INTERNAL *cfg)
   cfg->max_tree_depth = TR_DEFAULT_MAX_TREE_DEPTH;
   cfg->tids_port = TR_DEFAULT_TIDS_PORT;
   cfg->trps_port = TR_DEFAULT_TRPS_PORT;
+  cfg->monitoring_port = TR_DEFAULT_MONITORING_PORT;
   cfg->cfg_poll_interval = TR_CFGWATCH_DEFAULT_POLL;
   cfg->cfg_settling_time = TR_CFGWATCH_DEFAULT_SETTLE;
   cfg->trp_connect_interval = TR_DEFAULT_TRP_CONNECT_INTERVAL;
@@ -128,6 +161,7 @@ static void set_defaults(TR_CFG_INTERNAL *cfg)
   cfg->tid_resp_denom = TR_DEFAULT_TID_RESP_DENOM;
   cfg->log_threshold = TR_DEFAULT_LOG_THRESHOLD;
   cfg->console_threshold = TR_DEFAULT_CONSOLE_THRESHOLD;
+  cfg->monitoring_credentials = NULL;
 }
 
 /* Helper that checks return value of a parse fn and returns if it failed */
@@ -137,6 +171,21 @@ do {                         \
     return TR_CFG_NOPARSE;   \
 } while(0)
 
+static TR_CFG_RC tr_cfg_parse_monitoring(TR_CFG *trc, json_t *jmon)
+{
+  int enabled = 1; /* assume we are enabled unless we are told not to be */
+
+  NOPARSE_UNLESS(tr_cfg_parse_boolean(jmon, "enabled", &enabled));
+  if (enabled) {
+    NOPARSE_UNLESS(tr_cfg_parse_unsigned(jmon, "port", &(trc->internal->monitoring_port)));
+    NOPARSE_UNLESS(tr_cfg_parse_gss_names(trc->internal,
+                                          json_object_get(jmon, "authorized_credentials"),
+                                          &(trc->internal->monitoring_credentials)));
+  }
+
+  return TR_CFG_SUCCESS;
+}
+
 /**
  * Parse internal configuration JSON
  *
@@ -192,7 +241,7 @@ TR_CFG_RC tr_cfg_parse_internal(TR_CFG *trc, json_t *jint)
 
   /* Parse the monitoring section */
   if (NULL != (jtmp = json_object_get(jint, "monitoring"))) {
-    NOPARSE_UNLESS(tr_cfg_parse_unsigned(jtmp, "port", &(trc->internal->monitoring_port)));
+    NOPARSE_UNLESS(tr_cfg_parse_monitoring(trc, jtmp));
   }
 
   tr_debug("tr_cfg_parse_internal: Internal config parsed.");
diff --git a/common/tr_config_orgs.c b/common/tr_config_orgs.c
new file mode 100644 (file)
index 0000000..f6925ca
--- /dev/null
@@ -0,0 +1,269 @@
+/*
+ * Copyright (c) 2012-2018, JANET(UK)
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * 3. Neither the name of JANET(UK) nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#include <stdlib.h>
+#include <string.h>
+#include <jansson.h>
+#include <dirent.h>
+#include <talloc.h>
+
+#include <tr_cfgwatch.h>
+#include <tr_comm.h>
+#include <tr_config.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>
+
+#if JANSSON_VERSION_HEX < 0x020500
+#include "jansson_iterators.h"
+#endif
+
+/* takes a talloc context, but currently does not use it */
+static TR_NAME *tr_cfg_parse_org_name(TALLOC_CTX *mem_ctx, json_t *j_org, TR_CFG_RC *rc)
+{
+  TR_NAME *name=NULL;
+
+  if ((j_org==NULL) || (rc==NULL) || (!json_is_string(j_org))) {
+    tr_debug("tr_cfg_parse_org_name: Bad parameters.");
+    if (rc!=NULL)
+      *rc = TR_CFG_BAD_PARAMS; /* fill in return value if we can */
+    return NULL;
+  }
+
+  name=tr_new_name(json_string_value(j_org));
+  if (name==NULL)
+    *rc=TR_CFG_NOMEM;
+  else
+    *rc=TR_CFG_SUCCESS;
+  return name;
+}
+
+static TR_CFG_RC tr_cfg_parse_one_local_org(TR_CFG *trc, json_t *jlorg)
+{
+  TALLOC_CTX *tmp_ctx=talloc_new(NULL);
+  TR_CFG_RC retval=TR_CFG_ERROR; /* our return code */
+  TR_CFG_RC rc=TR_CFG_ERROR; /* return code from functions we call */
+  TR_NAME *org_name=NULL;
+  json_t *j_org=NULL;
+  json_t *j_realms=NULL;
+  TR_IDP_REALM *new_idp_realms=NULL;
+  TR_RP_CLIENT *new_rp_clients=NULL;
+
+  tr_debug("tr_cfg_parse_one_local_org: parsing local organization");
+
+  /* get organization_name (optional) */
+  if (NULL==(j_org=json_object_get(jlorg, "organization_name"))) {
+    tr_debug("tr_cfg_parse_one_local_org: organization_name unspecified");
+  } else {
+    org_name=tr_cfg_parse_org_name(tmp_ctx, j_org, &rc);
+    if (rc==TR_CFG_SUCCESS) {
+      tr_debug("tr_cfg_parse_one_local_org: organization_name=\"%.*s\"",
+               org_name->len,
+               org_name->buf);
+      /* we don't actually do anything with this, but we could */
+      tr_free_name(org_name);
+      org_name=NULL;
+    }
+  }
+
+  /* Now get realms. Allow this to be missing; even though that is a pointless organization entry,
+   * it's harmless. Report a warning because that might be unintentional. */
+  if (NULL==(j_realms=json_object_get(jlorg, "realms"))) {
+    tr_warning("tr_cfg_parse_one_local_org: warning - no realms in this local organization");
+  } else {
+    /* Allocate in the tmp_ctx so these will be cleaned up if we do not complete successfully. */
+    new_idp_realms=tr_cfg_parse_idp_realms(tmp_ctx, j_realms, &rc);
+    if (rc!=TR_CFG_SUCCESS)
+      goto cleanup;
+
+    new_rp_clients=tr_cfg_parse_rp_clients(tmp_ctx, j_realms, &rc);
+    if (rc!=TR_CFG_SUCCESS)
+      goto cleanup;
+  }
+  retval=TR_CFG_SUCCESS;
+
+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) {
+      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) {
+      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 */
+    }
+  }
+
+  talloc_free(tmp_ctx);
+  return rc;
+}
+
+/* Parse local organizations if present. Returns success if there are none. On failure, the configuration is unreliable. */
+TR_CFG_RC tr_cfg_parse_local_orgs(TR_CFG *trc, json_t *jcfg)
+{
+  json_t *jlocorgs=NULL;
+  size_t ii=0;
+
+  jlocorgs=json_object_get(jcfg, "local_organizations");
+  if (jlocorgs==NULL)
+    return TR_CFG_SUCCESS;
+
+  if (!json_is_array(jlocorgs)) {
+    tr_err("tr_cfg_parse_local_orgs: local_organizations is not an array.");
+    return TR_CFG_NOPARSE;
+  }
+
+  for (ii=0; ii<json_array_size(jlocorgs); ii++) {
+    if (tr_cfg_parse_one_local_org(trc, json_array_get(jlocorgs, ii))!=TR_CFG_SUCCESS) {
+      tr_err("tr_cfg_parse_local_orgs: error parsing local_organization %d.", ii+1);
+      return TR_CFG_NOPARSE;
+    }
+  }
+
+  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;
+  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.");
+    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;
+  }
+
+  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.");
+    rc=TR_CFG_NOMEM;
+    goto cleanup;
+  }
+
+  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
+    trp_peer_set_port(new_peer, json_integer_value(jport));
+
+  rc = tr_cfg_parse_gss_names(tmp_ctx, jgss, &names);
+  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);
+
+  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;
+
+cleanup:
+  talloc_free(tmp_ctx);
+  return rc;
+}
+
+/* Parse peer organizations, if present. Returns success if there are none. */
+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;
+}
+
diff --git a/common/tr_config_realms.c b/common/tr_config_realms.c
new file mode 100644 (file)
index 0000000..2f6fa09
--- /dev/null
@@ -0,0 +1,467 @@
+/*
+ * Copyright (c) 2012-2018, JANET(UK)
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * 3. Neither the name of JANET(UK) nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#include <stdlib.h>
+#include <string.h>
+#include <jansson.h>
+#include <dirent.h>
+#include <talloc.h>
+
+#include <tr_cfgwatch.h>
+#include <tr_comm.h>
+#include <tr_config.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>
+
+#if JANSSON_VERSION_HEX < 0x020500
+#include "jansson_iterators.h"
+#endif
+
+TR_AAA_SERVER *tr_cfg_parse_one_aaa_server(TALLOC_CTX *mem_ctx, json_t *jaddr, TR_CFG_RC *rc)
+{
+  TR_AAA_SERVER *aaa = NULL;
+  TR_NAME *name=NULL;
+
+  if ((!jaddr) || (!json_is_string(jaddr))) {
+    tr_debug("tr_cfg_parse_one_aaa_server: Bad parameters.");
+    *rc = TR_CFG_BAD_PARAMS;
+    return NULL;
+  }
+
+  name=tr_new_name(json_string_value(jaddr));
+  if (name==NULL) {
+    tr_debug("tr_cfg_parse_one_aaa_server: Out of memory allocating hostname.");
+    *rc = TR_CFG_NOMEM;
+    return NULL;
+  }
+
+  aaa=tr_aaa_server_new(mem_ctx, name);
+  if (aaa==NULL) {
+    tr_free_name(name);
+    tr_debug("tr_cfg_parse_one_aaa_server: Out of memory allocating AAA server.");
+    *rc = TR_CFG_NOMEM;
+    return NULL;
+  }
+
+  return aaa;
+}
+
+static TR_AAA_SERVER *tr_cfg_parse_aaa_servers(TALLOC_CTX *mem_ctx, json_t *jaaas, TR_CFG_RC *rc)
+{
+  TALLOC_CTX *tmp_ctx=NULL;
+  TR_AAA_SERVER *aaa = NULL;
+  TR_AAA_SERVER *temp_aaa = NULL;
+  int i = 0;
+
+  for (i = 0; i < json_array_size(jaaas); i++) {
+    /* rc gets set in here */
+    if (NULL == (temp_aaa = tr_cfg_parse_one_aaa_server(tmp_ctx, json_array_get(jaaas, i), rc))) {
+      talloc_free(tmp_ctx);
+      return NULL;
+    }
+    /* TBD -- IPv6 addresses */
+    //    tr_debug("tr_cfg_parse_aaa_servers: Configuring AAA Server: ip_addr = %s.", inet_ntoa(temp_aaa->aaa_server_addr));
+    temp_aaa->next = aaa;
+    aaa = temp_aaa;
+  }
+  tr_debug("tr_cfg_parse_aaa_servers: Finished (rc=%d)", *rc);
+
+  for (temp_aaa=aaa; temp_aaa!=NULL; temp_aaa=temp_aaa->next)
+    talloc_steal(mem_ctx, temp_aaa);
+  talloc_free(tmp_ctx);
+  return aaa;
+}
+
+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;
+
+  *rc = TR_CFG_SUCCESS;         /* presume success */
+
+  if ((!japc) || (!rc) || (!json_is_string(japc))) {
+    tr_debug("tr_cfg_parse_one_apc: Bad parameters.");
+    if (rc)
+      *rc = TR_CFG_BAD_PARAMS;
+    return NULL;
+  }
+
+  apc=tr_apc_new(mem_ctx);
+  if (apc==NULL) {
+    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_one_apc: No memory for APC name.");
+    tr_apc_free(apc);
+    *rc = TR_CFG_NOMEM;
+    return NULL;
+  }
+  tr_apc_set_id(apc, name); /* apc is now responsible for freeing the name */
+
+  return apc;
+}
+
+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;
+  *rc=TR_CFG_ERROR;
+
+  if ((jname==NULL) || (!json_is_string(jname))) {
+    tr_err("tr_cfg_parse_name: name missing or not a string");
+    *rc=TR_CFG_BAD_PARAMS;
+    name=NULL;
+  } else {
+    name=tr_new_name(json_string_value(jname));
+    if (name==NULL) {
+      tr_err("tr_cfg_parse_name: could not allocate name");
+      *rc=TR_CFG_NOMEM;
+    } else {
+      *rc=TR_CFG_SUCCESS;
+    }
+  }
+  return name;
+}
+
+static int tr_cfg_parse_shared_config(json_t *jsc, TR_CFG_RC *rc)
+{
+  const char *shared=NULL;
+  *rc=TR_CFG_SUCCESS;
+
+  if ((jsc==NULL) ||
+      (!json_is_string(jsc)) ||
+      (NULL==(shared=json_string_value(jsc)))) {
+    *rc=TR_CFG_BAD_PARAMS;
+    return -1;
+  }
+
+  if (0==strcmp(shared, "no"))
+    return 0;
+  else if (0==strcmp(shared, "yes"))
+    return 1;
+
+  /* any other value is an error */
+  tr_debug("tr_cfg_parse_shared_config: invalid shared_config value \"%s\" (should be \"yes\" or \"no\")",
+           shared);
+  *rc=TR_CFG_NOPARSE;
+  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)
+{
+  TALLOC_CTX *tmp_ctx=talloc_new(NULL);
+  TR_APC *apcs=NULL;
+  TR_AAA_SERVER *aaa=NULL;
+  TR_CFG_RC rc=TR_CFG_ERROR;
+
+  if (jidp==NULL)
+    goto cleanup;
+
+  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");
+    rc=TR_CFG_NOPARSE;
+    goto cleanup;
+  }
+
+  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;
+  }
+
+  aaa=tr_cfg_parse_aaa_servers(idp, json_object_get(jidp, "aaa_servers"), &rc);
+  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:
+  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;
+}
+
+/* parses idp realm */
+static TR_IDP_REALM *tr_cfg_parse_one_idp_realm(TALLOC_CTX *mem_ctx, json_t *jrealm, TR_CFG_RC *rc)
+{
+  TALLOC_CTX *tmp_ctx=talloc_new(NULL);
+  TR_IDP_REALM *realm=NULL;
+  TR_CFG_RC call_rc=TR_CFG_ERROR;
+
+  *rc=TR_CFG_ERROR; /* default to error if not set */
+
+  if ((!jrealm) || (!rc)) {
+    tr_err("tr_cfg_parse_one_idp_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_idp_realm: could not allocate idp realm.");
+    *rc=TR_CFG_NOMEM;
+    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"),
+                                    &call_rc);
+  if ((call_rc!=TR_CFG_SUCCESS) || (realm->realm_id==NULL)) {
+    tr_err("tr_cfg_parse_one_idp_realm: could not parse realm name");
+    *rc=TR_CFG_NOPARSE;
+    goto cleanup;
+  }
+  tr_debug("tr_cfg_parse_one_idp_realm: realm_id=\"%.*s\"",
+           realm->realm_id->len,
+           realm->realm_id->buf);
+
+  call_rc=tr_cfg_parse_idp(realm, json_object_get(jrealm, "identity_provider"));
+  if (call_rc!=TR_CFG_SUCCESS) {
+    tr_err("tr_cfg_parse_one_idp_realm: could not parse identity_provider.");
+    *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;
+}
+
+/* Determine whether the realm is an IDP realm */
+static int tr_cfg_is_idp_realm(json_t *jrealm)
+{
+  /* If a realm spec contains an identity_provider, it's an IDP realm. */
+  if (NULL != json_object_get(jrealm, "identity_provider"))
+    return 1;
+  else
+    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. */
+TR_IDP_REALM *tr_cfg_parse_idp_realms(TALLOC_CTX *mem_ctx, json_t *jrealms, TR_CFG_RC *rc)
+{
+  TALLOC_CTX *tmp_ctx=talloc_new(NULL);
+  TR_IDP_REALM *realms=NULL;
+  TR_IDP_REALM *new_realm=NULL;
+  json_t *this_jrealm=NULL;
+  int ii=0;
+
+  *rc=TR_CFG_ERROR;
+  if ((jrealms==NULL) || (!json_is_array(jrealms))) {
+    tr_err("tr_cfg_parse_idp_realms: realms not an array");
+    *rc=TR_CFG_BAD_PARAMS;
+    goto cleanup;
+  }
+
+  for (ii=0; ii<json_array_size(jrealms); ii++) {
+    this_jrealm=json_array_get(jrealms, ii);
+    if (tr_cfg_is_idp_realm(this_jrealm)) {
+      new_realm=tr_cfg_parse_one_idp_realm(tmp_ctx, this_jrealm, rc);
+      if ((*rc)!=TR_CFG_SUCCESS) {
+        tr_err("tr_cfg_parse_idp_realms: error decoding realm entry %d", ii+1);
+        *rc=TR_CFG_NOPARSE;
+        goto cleanup;
+      }
+      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);
+    }
+  }
+
+  *rc=TR_CFG_SUCCESS;
+  talloc_steal(mem_ctx, realms);
+
+cleanup:
+  talloc_free(tmp_ctx);
+  return realms;
+}
diff --git a/common/tr_config_rp_clients.c b/common/tr_config_rp_clients.c
new file mode 100644 (file)
index 0000000..960edce
--- /dev/null
@@ -0,0 +1,399 @@
+/*
+ * Copyright (c) 2012-2018, JANET(UK)
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * 3. Neither the name of JANET(UK) nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#include <stdlib.h>
+#include <string.h>
+#include <jansson.h>
+#include <dirent.h>
+#include <talloc.h>
+
+#include <tr_cfgwatch.h>
+#include <tr_comm.h>
+#include <tr_config.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>
+
+#if JANSSON_VERSION_HEX < 0x020500
+#include "jansson_iterators.h"
+#endif
+
+
+TR_CFG_RC tr_cfg_parse_gss_names(TALLOC_CTX *mem_ctx, json_t *jgss_names, TR_GSS_NAMES **gssn_out)
+{
+  TALLOC_CTX *tmp_ctx=talloc_new(NULL);
+  TR_GSS_NAMES *gn=NULL;
+  json_t *jname=NULL;
+  size_t ii=0;
+  TR_NAME *name=NULL;
+  TR_CFG_RC rc = TR_CFG_ERROR;
+
+  if (jgss_names==NULL) {
+    tr_err("tr_cfg_parse_gss_names: Bad parameters.");
+    rc=TR_CFG_BAD_PARAMS;
+    goto cleanup;
+  }
+
+  if (!json_is_array(jgss_names)) {
+    tr_err("tr_cfg_parse_gss_names: gss_names not an array.");
+    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.");
+      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.");
+      rc=TR_CFG_NOMEM;
+      goto cleanup;
+    }
+
+    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.");
+      rc=TR_CFG_ERROR;
+      goto cleanup;
+    }
+  }
+
+  *gssn_out = gn;
+  talloc_steal(mem_ctx, *gssn_out);
+  rc=TR_CFG_SUCCESS;
+
+cleanup:
+  talloc_free(tmp_ctx);
+  return rc;
+}
+
+/* default filter accepts realm and *.realm */
+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("*.");
+  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_filters: 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_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_filters: could not allocate filter.");
+    *rc=TR_CFG_NOMEM;
+    goto cleanup;
+  }
+  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_filters: 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_filters: could not allocate realm name.");
+    *rc=TR_CFG_NOMEM;
+    goto cleanup;
+  }
+  tr_fspec_add_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_filters: could not allocate wildcard realm name.");
+    *rc=TR_CFG_NOMEM;
+    goto cleanup;
+  }
+
+  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_filters: 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_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_filters: 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_filters: 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_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_filters: 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;
+
+  /* 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);
+
+  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_set;
+}
+
+/* parses rp client */
+static TR_RP_CLIENT *tr_cfg_parse_one_rp_client(TALLOC_CTX *mem_ctx, json_t *jrealm, TR_CFG_RC *rc)
+{
+  TALLOC_CTX *tmp_ctx=talloc_new(NULL);
+  TR_RP_CLIENT *client=NULL;
+  TR_CFG_RC call_rc=TR_CFG_ERROR;
+  TR_FILTER_SET *new_filts=NULL;
+  TR_NAME *realm=NULL;
+  json_t *jfilt=NULL;
+  json_t *jrealm_id=NULL;
+
+  *rc=TR_CFG_ERROR; /* default to error if not set */
+
+  if ((!jrealm) || (!rc)) {
+    tr_err("tr_cfg_parse_one_rp_client: Bad parameters.");
+    if (rc)
+      *rc=TR_CFG_BAD_PARAMS;
+    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));
+
+  if (call_rc!=TR_CFG_SUCCESS) {
+    tr_err("tr_cfg_parse_one_rp_client: could not parse gss_names.");
+    *rc=TR_CFG_NOPARSE;
+    goto cleanup;
+  }
+
+  /* parse filters */
+  jfilt=json_object_get(jrealm, "filters");
+  if (jfilt!=NULL) {
+    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;
+      goto cleanup;
+    }
+  } else {
+    tr_debug("tr_cfg_parse_one_rp_client: no filters specified, using default filters.");
+    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;
+      goto cleanup;
+    }
+  }
+
+  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;
+}
+
+/* Determine whether the realm is an RP realm */
+static int tr_cfg_is_rp_realm(json_t *jrealm)
+{
+  /* Check that we have a gss name. */
+  if (NULL != json_object_get(jrealm, "gss_names"))
+    return 1;
+  else
+    return 0;
+}
+
+/* Parse any rp clients in the j_realms object. Ignores other realms. */
+TR_RP_CLIENT *tr_cfg_parse_rp_clients(TALLOC_CTX *mem_ctx, json_t *jrealms, TR_CFG_RC *rc)
+{
+  TALLOC_CTX *tmp_ctx=talloc_new(NULL);
+  TR_RP_CLIENT *clients=NULL;
+  TR_RP_CLIENT *new_client=NULL;
+  json_t *this_jrealm=NULL;
+  int ii=0;
+
+  *rc=TR_CFG_ERROR;
+  if ((jrealms==NULL) || (!json_is_array(jrealms))) {
+    tr_err("tr_cfg_parse_rp_clients: realms not an array");
+    *rc=TR_CFG_BAD_PARAMS;
+    goto cleanup;
+  }
+
+  for (ii=0; ii<json_array_size(jrealms); ii++) {
+    this_jrealm=json_array_get(jrealms, ii);
+    if (tr_cfg_is_rp_realm(this_jrealm)) {
+      new_client=tr_cfg_parse_one_rp_client(tmp_ctx, this_jrealm, rc);
+      if ((*rc)!=TR_CFG_SUCCESS) {
+        tr_err("tr_cfg_parse_rp_clients: error decoding realm entry %d", ii+1);
+        *rc=TR_CFG_NOPARSE;
+        goto cleanup;
+      }
+      tr_rp_client_add(clients, new_client);
+    }
+  }
+
+  *rc=TR_CFG_SUCCESS;
+  talloc_steal(mem_ctx, clients);
+
+cleanup:
+  talloc_free(tmp_ctx);
+  return clients;
+}
index e2483b9..e7902ee 100644 (file)
@@ -73,6 +73,7 @@ static int tr_gss_auth_cb(gss_name_t clientName, gss_buffer_t displayName, void
   TR_NAME name ={(char *) displayName->value, (int) displayName->length};
   int result=0;
 
+  tr_debug("tr_gss_auth_cb: Calling auth handler for %.*s.", name.len, name.buf);
   if (cookie->auth_cb(clientName, &name, cookie->auth_cookie)) {
     tr_debug("tr_gss_auth_cb: client '%.*s' denied authorization.", name.len, name.buf);
     result=EACCES; /* denied */
@@ -86,16 +87,16 @@ static int tr_gss_auth_cb(gss_name_t clientName, gss_buffer_t displayName, void
  * Handle GSS authentication and authorization
  *
  * @param conn connection file descriptor
- * @param acceptor_name name of acceptor to present to initiator
- * @param acceptor_realm realm of acceptor to present to initiator
+ * @param acceptor_service name of acceptor to present to initiator
+ * @param acceptor_hostname hostname of acceptor to present to initiator
  * @param gssctx GSS context
  * @param auth_cb authorization callback
  * @param auth_cookie generic data to pass to the authorization callback
  * @return 0 on successful auth, 1 on disallowed auth, -1 on error
  */
 static int tr_gss_auth_connection(int conn,
-                                  const char *acceptor_name,
-                                  const char *acceptor_realm,
+                                  const char *acceptor_service,
+                                  const char *acceptor_hostname,
                                   gss_ctx_id_t *gssctx,
                                   TR_GSS_AUTH_FN auth_cb,
                                   void *auth_cookie)
@@ -105,7 +106,7 @@ static int tr_gss_auth_connection(int conn,
   gss_buffer_desc nameBuffer = {0, NULL};
   TR_GSS_COOKIE *cookie = NULL;
 
-  nameBuffer.value = talloc_asprintf(NULL, "%s@%s", acceptor_name, acceptor_realm);
+  nameBuffer.value = talloc_asprintf(NULL, "%s@%s", acceptor_service, acceptor_hostname);
   if (nameBuffer.value == NULL) {
     tr_err("tr_gss_auth_connection: Error allocating acceptor name.");
     return -1;
@@ -120,6 +121,8 @@ static int tr_gss_auth_connection(int conn,
   cookie->auth_cookie=auth_cookie;
 
   /* Now call gsscon with *our* auth callback and cookie */
+  tr_debug("tr_gss_auth_connection: Beginning passive authentication as %.*s",
+           nameBuffer.length, nameBuffer.value);
   rc = gsscon_passive_authenticate(conn, nameBuffer, gssctx, tr_gss_auth_cb, cookie);
   talloc_free(cookie);
   talloc_free(nameBuffer.value);
@@ -128,6 +131,7 @@ static int tr_gss_auth_connection(int conn,
     return -1;
   }
 
+  tr_debug("tr_gss_auth_connection: Authentication succeeded, now authorizing.");
   rc = gsscon_authorize(*gssctx, &auth, &autherr);
   if (rc) {
     tr_debug("tr_gss_auth_connection: Error from gsscon_authorize, rc = %d, autherr = %d.",
@@ -203,16 +207,16 @@ static int tr_gss_write_resp(int conn, gss_ctx_id_t gssctx, const char *resp)
  * callback to get a response, sends that, then returns.
  *
  * @param conn connection file descriptor
- * @param acceptor_name acceptor name to present
- * @param acceptor_realm acceptor realm to present
+ * @param acceptor_service acceptor name to present
+ * @param acceptor_hostname acceptor hostname to present
  * @param auth_cb callback for authorization
  * @param auth_cookie cookie for the auth_cb
  * @param req_cb callback to handle the request and produce the response
  * @param req_cookie cookie for the req_cb
  */
 void tr_gss_handle_connection(int conn,
-                              const char *acceptor_name,
-                              const char *acceptor_realm,
+                              const char *acceptor_service,
+                              const char *acceptor_hostname,
                               TR_GSS_AUTH_FN auth_cb,
                               void *auth_cookie,
                               TR_GSS_HANDLE_REQ_FN req_cb,
@@ -226,9 +230,12 @@ void tr_gss_handle_connection(int conn,
   TR_MSG *resp_msg = NULL;
   char *resp_str = NULL;
 
+  tr_debug("tr_gss_handle_connection: Attempting to accept %s connection on fd %d.",
+           acceptor_service, conn);
+
   if (tr_gss_auth_connection(conn,
-                             acceptor_name,
-                             acceptor_realm,
+                             acceptor_service,
+                             acceptor_hostname,
                              &gssctx,
                              auth_cb,
                              auth_cookie)) {
index 970efdb..df15202 100644 (file)
@@ -35,6 +35,7 @@
 #include <talloc.h>
 
 #include <tr_gss_names.h>
+#include <tr_debug.h>
 
 static int tr_gss_names_destructor(void *obj)
 {
@@ -81,6 +82,36 @@ int tr_gss_names_add(TR_GSS_NAMES *gn, TR_NAME *new)
     return -1;
 }
 
+/**
+ * Create a duplicate GSS names struct
+ *
+ * @param mem_ctx
+ * @param orig
+ * @return
+ */
+TR_GSS_NAMES *tr_gss_names_dup(TALLOC_CTX *mem_ctx, TR_GSS_NAMES *orig)
+{
+  TALLOC_CTX *tmp_ctx = talloc_new(NULL);
+  TR_GSS_NAMES *new = tr_gss_names_new(tmp_ctx);
+  TR_GSS_NAMES_ITER *iter = tr_gss_names_iter_new(tmp_ctx);
+  TR_NAME *this = NULL;
+
+  if ( !orig || !new || !iter ) {
+    talloc_free(tmp_ctx);
+    return NULL;
+  }
+  this = tr_gss_names_iter_first(iter, orig);
+  while (this) {
+    if (tr_gss_names_add(new, tr_dup_name(this)) != 0) {
+      talloc_free(tmp_ctx);
+      return NULL;
+    }
+    this = tr_gss_names_iter_next(iter);
+  }
+  /* success */
+  talloc_steal(mem_ctx, new);
+  return new;
+}
 int tr_gss_names_matches(TR_GSS_NAMES *gn, TR_NAME *name)
 {
   int ii=0;
index 313d931..7f1c917 100644 (file)
@@ -129,7 +129,7 @@ nfds_t tr_sock_listen_all(unsigned int port, int *fd_out, nfds_t max_fd)
     return 0;
   }
 
-  tr_debug("tr_sock_listen_all: monitoring interface listening on port %d on %d socket%s",
+  tr_debug("tr_sock_listen_all: listening on port %d on %d socket%s",
            port,
            n_opened,
            (n_opened==1)?"":"s");
index 47c962b..6b4639c 100644 (file)
@@ -1,5 +1,5 @@
 AC_PREREQ(2.63)
-AC_INIT([trust_router],[3.3.0],
+AC_INIT([trust_router],[3.4.0~1],
 [bugs@project-moonshot.org])
 AC_CONFIG_MACRO_DIR(m4)
 AC_CONFIG_AUX_DIR(build-aux)
index ff2be72..29ea514 100644 (file)
@@ -95,7 +95,7 @@ enum mon_opt_type {
 
   // System information
   OPT_TYPE_SHOW_VERSION,
-  OPT_TYPE_SHOW_SERIAL,
+  OPT_TYPE_SHOW_CONFIG_FILES,
 
   // System statistics
   OPT_TYPE_SHOW_UPTIME,
index 64339e7..30317de 100644 (file)
@@ -41,6 +41,7 @@
 #include <syslog.h>
 #include <sys/time.h>
 #include <talloc.h>
+#include <gmodule.h>
 
 #include <tr_comm.h>
 #include <tr_rp.h>
@@ -52,6 +53,7 @@
 #define TR_DEFAULT_MAX_TREE_DEPTH 12
 #define TR_DEFAULT_TRPS_PORT 12308
 #define TR_DEFAULT_TIDS_PORT 12309
+#define TR_DEFAULT_MONITORING_PORT 0 /* defaults to being turned off */
 #define TR_DEFAULT_LOG_THRESHOLD LOG_INFO
 #define TR_DEFAULT_CONSOLE_THRESHOLD LOG_NOTICE
 #define TR_DEFAULT_APC_EXPIRATION_INTERVAL 43200
@@ -62,6 +64,8 @@
 #define TR_DEFAULT_TID_RESP_NUMER 2
 #define TR_DEFAULT_TID_RESP_DENOM 3
 
+#define TR_CFG_INVALID_SERIAL -1
+
 typedef enum tr_cfg_rc {
   TR_CFG_SUCCESS = 0,  /* No error */
   TR_CFG_ERROR,                /* General processing error */
@@ -86,15 +90,23 @@ typedef struct tr_cfg_internal {
   unsigned int tid_req_timeout;
   unsigned int tid_resp_numer; /* numerator of fraction of AAA servers to wait for in unshared mode */
   unsigned int tid_resp_denom; /* denominator of fraction of AAA servers to wait for in unshared mode */
+  TR_GSS_NAMES *monitoring_credentials;
 } TR_CFG_INTERNAL;
 
+/* record of files loaded for this configuration */
+typedef struct tr_cfg_file {
+  const char *name;
+  json_int_t serial;
+} TR_CFG_FILE;
+
 typedef struct tr_cfg {
   TR_CFG_INTERNAL *internal;           /* internal trust router config */
   TR_RP_CLIENT *rp_clients;            /* locally associated RP Clients */
   TRP_PTABLE *peers; /* TRP peer table */
   TR_COMM_TABLE *ctable; /* communities/realms */
   TR_AAA_SERVER *default_servers;      /* default server list */
-  /* TBD -- Global Filters */
+
+  GArray *files; /* files loaded to make this configuration */
 } TR_CFG;
 
 typedef struct tr_cfg_mgr {
@@ -117,10 +129,32 @@ void tr_print_comms(TR_COMM_TABLE *ctab);
 void tr_print_comm_idps(TR_COMM_TABLE *ctab, TR_COMM *comm);
 void tr_print_comm_rps(TR_COMM_TABLE *ctab, TR_COMM *comm);
 
-TR_IDP_REALM *tr_cfg_find_idp (TR_CFG *cfg, TR_NAME *idp_id, TR_CFG_RC *rc);
-TR_RP_CLIENT *tr_cfg_find_rp (TR_CFG *cfg, TR_NAME *rp_gss, TR_CFG_RC *rc);
-
 /* tr_config_internal.c */
 TR_CFG_RC tr_cfg_parse_internal(TR_CFG *trc, json_t *jint);
 
+/* tr_config_comms.c */
+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_CFG_RC tr_cfg_parse_comms (TR_CFG *trc, json_t *jcfg);
+TR_CFG_RC tr_cfg_parse_default_servers (TR_CFG *trc, json_t *jcfg);
+
+/* tr_config_filters.c */
+TR_FILTER_SET *tr_cfg_parse_filters(TALLOC_CTX *mem_ctx, json_t *jfilts, TR_CFG_RC *rc);
+
+/* tr_config_orgs.c */
+TR_CFG_RC tr_cfg_parse_local_orgs(TR_CFG *trc, json_t *jcfg);
+TR_CFG_RC tr_cfg_parse_peer_orgs(TR_CFG *trc, json_t *jcfg);
+
+/* tr_config_realms.c */
+TR_IDP_REALM *tr_cfg_parse_idp_realms(TALLOC_CTX *mem_ctx, json_t *jrealms, TR_CFG_RC *rc);
+TR_AAA_SERVER *tr_cfg_parse_one_aaa_server(TALLOC_CTX *mem_ctx, json_t *jaddr, TR_CFG_RC *rc);
+TR_APC *tr_cfg_parse_apcs(TALLOC_CTX *mem_ctx, json_t *japcs, TR_CFG_RC *rc);
+
+/* tr_config_rp_clients.c */
+TR_RP_CLIENT *tr_cfg_parse_rp_clients(TALLOC_CTX *mem_ctx, json_t *jrealms, TR_CFG_RC *rc);
+TR_CFG_RC tr_cfg_parse_gss_names(TALLOC_CTX *mem_ctx, json_t *jgss_names, TR_GSS_NAMES **gssn_out);
+
+/* tr_config_encoders.c */
+json_t *tr_cfg_files_to_json_array(TR_CFG *cfg);
+
 #endif
index d035a4e..850f51b 100644 (file)
@@ -40,7 +40,7 @@
 typedef int (TR_GSS_AUTH_FN)(gss_name_t, TR_NAME *, void *);
 typedef TR_MSG *(TR_GSS_HANDLE_REQ_FN)(TALLOC_CTX *, TR_MSG *, void *);
 
-void tr_gss_handle_connection(int conn, const char *acceptor_name, const char *acceptor_realm, TR_GSS_AUTH_FN auth_cb,
+void tr_gss_handle_connection(int conn, const char *acceptor_service, const char *acceptor_hostname, TR_GSS_AUTH_FN auth_cb,
                               void *auth_cookie, TR_GSS_HANDLE_REQ_FN req_cb, void *req_cookie);
 
 #endif //TRUST_ROUTER_TR_GSS_H
index 7585798..f8f97a2 100644 (file)
@@ -52,6 +52,7 @@ typedef struct tr_gss_names_iter {
 TR_GSS_NAMES *tr_gss_names_new(TALLOC_CTX *mem_ctx);
 void tr_gss_names_free(TR_GSS_NAMES *gn);
 int tr_gss_names_add(TR_GSS_NAMES *gn, TR_NAME *new);
+TR_GSS_NAMES *tr_gss_names_dup(TALLOC_CTX *mem_ctx, TR_GSS_NAMES *orig);
 int tr_gss_names_matches(TR_GSS_NAMES *gn, TR_NAME *name);
 
 TR_GSS_NAMES_ITER *tr_gss_names_iter_new(TALLOC_CTX *mem_ctx);
index 69a6377..35ec577 100644 (file)
@@ -95,7 +95,7 @@ void tid_req_set_realm(TID_REQ *req, TR_NAME *realm);
 TR_EXPORT TR_NAME *tid_req_get_comm(TID_REQ *req);
 void tid_req_set_comm(TID_REQ *req, TR_NAME *comm);
 TR_EXPORT TR_NAME *tid_req_get_orig_coi(TID_REQ *req);
-void tid_req_set_rp_orig_coi(TID_REQ *req, TR_NAME *orig_coi);
+void tid_req_set_orig_coi(TID_REQ *req, TR_NAME *orig_coi);
 TR_EXPORT TIDC_RESP_FUNC *tid_req_get_resp_func(TID_REQ *req);
 void tid_req_set_resp_func(TID_REQ *req, TIDC_RESP_FUNC *resp_func);
 TR_EXPORT void *tid_req_get_cookie(TID_REQ *req);
index b03afda..a237b5d 100644 (file)
@@ -87,8 +87,8 @@ const char *mon_opt_type_to_string(MON_OPT_TYPE opt_type)
     case OPT_TYPE_SHOW_VERSION:
       return "version";
 
-    case OPT_TYPE_SHOW_SERIAL:
-      return "serial";
+    case OPT_TYPE_SHOW_CONFIG_FILES:
+      return "config_files";
 
     case OPT_TYPE_SHOW_UPTIME:
       return "uptime";
@@ -130,7 +130,7 @@ const char *mon_opt_type_to_string(MON_OPT_TYPE opt_type)
 MON_OPT_TYPE mon_opt_type_from_string(const char *s)
 {
   return_if_matches(s, OPT_TYPE_SHOW_VERSION);
-  return_if_matches(s, OPT_TYPE_SHOW_SERIAL);
+  return_if_matches(s, OPT_TYPE_SHOW_CONFIG_FILES);
   return_if_matches(s, OPT_TYPE_SHOW_UPTIME);
   return_if_matches(s, OPT_TYPE_SHOW_TID_REQ_COUNT);
   return_if_matches(s, OPT_TYPE_SHOW_TID_REQ_ERR_COUNT);
index 0c7e7f4..21bb64b 100644 (file)
@@ -126,7 +126,7 @@ MON_REQ *mon_req_parse(TALLOC_CTX *mem_ctx, const char *input)
  *
  * (options are optional)
  *
- * Caller must free the return value with MON_REQ_free().
+ * Caller must free the return value with mon_req_free().
  *
  * @param mem_ctx talloc context for the returned struct
  * @param req_json reference to JSON request object
@@ -171,8 +171,6 @@ MON_REQ *mon_req_decode(TALLOC_CTX *mem_ctx, json_t *req_json)
 
 cleanup:
   talloc_free(tmp_ctx);
-  if (req_json)
-    json_decref(req_json);
 
   return req;
 }
index 23c3dd8..7e41c65 100644 (file)
@@ -79,6 +79,7 @@ json_t *mon_resp_encode(MON_RESP *resp)
   /* If we have a payload, add it */
   if (resp->payload) {
     object_set_or_free_and_return(resp_json, jval, "payload", resp->payload);
+    json_incref(resp->payload); /* we just created a second reference to the payload */
   }
 
   return resp_json;
index 5391857..19ad64d 100644 (file)
@@ -52,7 +52,7 @@ static MON_REQ *show_options(const MON_OPT_TYPE *opts)
 static MON_REQ *show_all_options()
 {
   MON_OPT_TYPE opts[] = {
-      OPT_TYPE_SHOW_SERIAL,
+      OPT_TYPE_SHOW_CONFIG_FILES,
       OPT_TYPE_SHOW_VERSION,
       OPT_TYPE_SHOW_UPTIME,
       OPT_TYPE_SHOW_TID_REQ_COUNT,
index 80811fd..d3ae49c 100644 (file)
@@ -107,7 +107,7 @@ int main(void)
   free(expected);
 
   // Test show with many options
-  opts[0] = OPT_TYPE_SHOW_SERIAL;
+  opts[0] = OPT_TYPE_SHOW_CONFIG_FILES;
   opts[1] = OPT_TYPE_SHOW_VERSION;
   opts[2] = OPT_TYPE_SHOW_UPTIME;
   opts[3] = OPT_TYPE_SHOW_TID_REQ_COUNT;
index 081ed2d..6945b76 100644 (file)
@@ -64,10 +64,10 @@ static char *show_success()
                                mon_opt_type_to_string(OPT_TYPE_SHOW_VERSION),
                                json_string("1.2.3-4")));
   assert(! json_object_set_new(payload,
-                               mon_opt_type_to_string(OPT_TYPE_SHOW_SERIAL),
+                               mon_opt_type_to_string(OPT_TYPE_SHOW_CONFIG_FILES),
                                json_integer(1234567890)));
   assert(! json_object_set_new(payload,
-                               mon_opt_type_to_string(OPT_TYPE_SHOW_SERIAL),
+                               mon_opt_type_to_string(OPT_TYPE_SHOW_CONFIG_FILES),
                                json_integer(86400)));
   assert(! json_object_set_new(payload,
                                mon_opt_type_to_string(OPT_TYPE_SHOW_TID_REQ_PENDING),
index db87adf..88dd991 100644 (file)
@@ -164,7 +164,7 @@ TR_NAME *tid_req_get_orig_coi(TID_REQ *req)
   return(req->orig_coi);
 }
 
-void tid_req_set_rp_orig_coi(TID_REQ *req, TR_NAME *orig_coi)
+void tid_req_set_orig_coi(TID_REQ *req, TR_NAME *orig_coi)
 {
   req->orig_coi = orig_coi;
 }
index 61f210c..ba738c7 100644 (file)
@@ -173,6 +173,14 @@ static MON_RC tr_handle_show_rp_clients(void *cookie, json_t **response_ptr)
   return (*response_ptr == NULL) ? MON_NOMEM : MON_SUCCESS;
 }
 
+static MON_RC tr_handle_show_cfg_serial(void *cookie, json_t **response_ptr)
+{
+  TR_CFG_MGR *cfg_mgr = talloc_get_type_abort(cookie, TR_CFG_MGR);
+
+  *response_ptr = tr_cfg_files_to_json_array(cfg_mgr->active);
+  return (*response_ptr == NULL) ? MON_NOMEM : MON_SUCCESS;
+}
+
 
 
 int main(int argc, char *argv[])
@@ -249,6 +257,7 @@ int main(int argc, char *argv[])
 
   /* Register monitoring handlers */
   mons_register_handler(tr->mons, MON_CMD_SHOW, OPT_TYPE_SHOW_VERSION, tr_handle_version, NULL);
+  mons_register_handler(tr->mons, MON_CMD_SHOW, OPT_TYPE_SHOW_CONFIG_FILES, tr_handle_show_cfg_serial, tr->cfg_mgr);
   mons_register_handler(tr->mons, MON_CMD_SHOW, OPT_TYPE_SHOW_UPTIME, tr_handle_uptime, &start_time);
   mons_register_handler(tr->mons, MON_CMD_SHOW, OPT_TYPE_SHOW_RP_CLIENTS, tr_handle_show_rp_clients, tr->cfg_mgr);
   tr_tid_register_mons_handlers(tr->tids, tr->mons);
index 8138198..84ab353 100644 (file)
@@ -96,9 +96,8 @@ static int tr_mons_auth_handler(gss_name_t client_name, TR_NAME *gss_name, void
 {
   struct tr_mons_event_cookie *cookie=talloc_get_type_abort(cookie_in, struct tr_mons_event_cookie);
   MONS_INSTANCE *mons = cookie->mons;
-  TR_CFG_MGR *cfg_mgr = cookie->cfg_mgr;
 
-  if ((!client_name) || (!gss_name) || (!mons) || (!cfg_mgr)) {
+  if ((!client_name) || (!gss_name) || (!mons)) {
     tr_debug("tr_mons_gss_handler: Bad parameters.");
     return -1;
   }
@@ -145,6 +144,12 @@ int tr_mons_event_init(struct event_base *base,
     goto cleanup;
   }
 
+  if (cfg_mgr->active->internal->monitoring_port == 0) {
+    tr_notice("tr_mons_event_init: monitoring is disabled, not enabling events or opening sockets");
+    retval = 0;
+    goto cleanup;
+  }
+
   /* Create the cookie for callbacks. We'll put it in the mons context, so it will
    * be cleaned up when mons is freed by talloc_free. */
   cookie=talloc(tmp_ctx, struct tr_mons_event_cookie);
index e10b036..038cc3c 100644 (file)
@@ -50,6 +50,7 @@
 #include <tr_mq.h>
 #include <tr_util.h>
 #include <tr_tid.h>
+#include <tr_comm.h>
 
 /* Structure to hold data for the tid response callback */
 typedef struct tr_resp_cookie {
@@ -185,7 +186,7 @@ static void *tr_tids_req_fwd_thread(void *arg)
   }
   /* cookie->resp should now contain our copy of the response */
   success=1;
-  tr_debug("tr_tids_req_fwd_thread: thread %d received response.");
+  tr_debug("tr_tids_req_fwd_thread: thread %d received response.", cookie->thread_id);
 
 cleanup:
   /* Notify parent thread of the response, if it's still listening. */
@@ -223,13 +224,70 @@ static TID_RC tr_tids_merge_resps(TID_RESP *r1, TID_RESP *r2)
   if ((r1->result!=TID_SUCCESS) || (r2->result!=TID_SUCCESS))
     return TID_ERROR;
 
-  if ((0!=tr_name_cmp(r1->rp_realm, r2->rp_realm)) ||
-      (0!=tr_name_cmp(r1->realm, r2->realm)) ||
-      (0!=tr_name_cmp(r1->comm, r2->comm)))
-    return TID_ERROR;
+  if ((0 == tr_name_cmp(r1->rp_realm, r2->rp_realm))
+      && (0 == tr_name_cmp(r1->realm, r2->realm))
+      && ( (0 == tr_name_cmp(r1->comm, r2->comm))
+           || (0 == tr_name_cmp(r1->comm, r2->orig_coi))
+           || (0 == tr_name_cmp(r1->orig_coi, r2->comm)))) {
 
-  tid_srvr_blk_add(r1->servers, tid_srvr_blk_dup(r1, r2->servers));
-  return TID_SUCCESS;
+    tid_srvr_blk_add(r1->servers, tid_srvr_blk_dup(r1, r2->servers));
+    return TID_SUCCESS;
+  }
+
+  return TID_ERROR;
+}
+
+enum map_coi_result {
+  MAP_COI_SUCCESS = 0,
+  MAP_COI_MAP_NOT_REQUIRED,
+  MAP_COI_ALREADY_MAPPED,
+  MAP_COI_NO_APC,
+  MAP_COI_INVALID_APC,
+  MAP_COI_UNKNOWN_COMM,
+  MAP_COI_ERROR
+};
+
+static enum map_coi_result map_coi(TR_COMM_TABLE *ctable, TID_REQ *req)
+{
+  TR_COMM *orig_comm;
+  TR_NAME *apc_name;
+  TR_COMM *apc;
+  TR_APC *apcs;
+
+  if (tid_req_get_orig_coi(req) != NULL)
+    return MAP_COI_ALREADY_MAPPED;
+
+  /* look up the community */
+  orig_comm = tr_comm_table_find_comm(ctable, tid_req_get_comm(req));
+  if (orig_comm == NULL)
+    return MAP_COI_UNKNOWN_COMM;
+
+  if (tr_comm_get_type(orig_comm) == TR_COMM_APC)
+    return MAP_COI_MAP_NOT_REQUIRED; /* it was already an APC, no mapping to do */
+
+  /* use first (only) APC. These are just APC names  */
+  apcs = tr_comm_get_apcs(orig_comm);
+  if ((!apcs) || (!tr_apc_get_id(apcs)))
+    return MAP_COI_NO_APC;
+
+  /* get our own copy of the APC name */
+  apc_name = tr_dup_name(tr_apc_get_id(apcs));
+  if (apc_name == NULL) {
+    tr_err("map_coi: Error allocating apc_name");
+    return MAP_COI_ERROR;
+  }
+
+  /* Check that the APC is configured */
+  apc = tr_comm_table_find_comm(ctable, apc_name);
+  if (apc == NULL) {
+    tr_free_name(apc_name);
+    return MAP_COI_INVALID_APC;
+  }
+
+  tid_req_set_orig_coi(req, tid_req_get_comm(req)); /* was null, so no need to free anything */
+  tid_req_set_comm(req, apc_name); /* original contents will be freed via orig_coi */
+
+  return MAP_COI_SUCCESS; /* successfully mapped */
 }
 
 /**
@@ -259,7 +317,6 @@ static int tr_tids_req_handler(TIDS_INSTANCE *tids,
   TID_RESP *aaa_resp[TR_TID_MAX_AAA_SERVERS]={NULL};
   TR_RP_CLIENT *rp_client=NULL;
   TR_RP_CLIENT_ITER *rpc_iter=NULL;
-  TR_NAME *apc = NULL;
   TID_REQ *fwd_req = NULL;
   TR_COMM *cfg_comm = NULL;
   TR_COMM *cfg_apc = NULL;
@@ -298,6 +355,7 @@ static int tr_tids_req_handler(TIDS_INSTANCE *tids,
   }
   talloc_steal(tmp_ctx, fwd_req);
 
+  /* cfg_comm is now the community (APC or CoI) of the incoming request */
   if (NULL == (cfg_comm=tr_comm_table_find_comm(cfg_mgr->active->ctable, orig_req->comm))) {
     tr_notice("tr_tids_req_hander: Request for unknown comm: %s.", orig_req->comm->buf);
     tid_resp_set_err_msg(resp, tr_new_name("Unknown community"));
@@ -354,7 +412,8 @@ static int tr_tids_req_handler(TIDS_INSTANCE *tids,
   /* We get here whether or not a filter matched. If tr_filter_apply() doesn't match, it returns
    * a default action of reject, so we don't have to check why we exited the loop. */
   if (oaction != TR_FILTER_ACTION_ACCEPT) {
-    tr_notice("tr_tids_req_handler: Incoming TID request rejected by filter for GSS name", orig_req->rp_realm->buf);
+    tr_notice("tr_tids_req_handler: Incoming TID request rejected by RP client filter for GSS name %.*s",
+              tids->gss_name->len, tids->gss_name->buf);
     tid_resp_set_err_msg(resp, tr_new_name("Incoming TID request filter error"));
     retval = -1;
     goto cleanup;
@@ -362,60 +421,73 @@ static int tr_tids_req_handler(TIDS_INSTANCE *tids,
 
   /* Check that the rp_realm is a member of the community in the request */
   if (NULL == tr_comm_find_rp(cfg_mgr->active->ctable, cfg_comm, orig_req->rp_realm)) {
-    tr_notice("tr_tids_req_handler: RP Realm (%s) not member of community (%s).", orig_req->rp_realm->buf, orig_req->comm->buf);
-    tid_resp_set_err_msg(resp, tr_new_name("RP COI membership error"));
+    tr_notice("tr_tids_req_handler: RP Realm (%s) not member of community (%s).",
+              orig_req->rp_realm->buf, orig_req->comm->buf);
+    tid_resp_set_err_msg(resp, tr_new_name("RP community membership error"));
     retval=-1;
     goto cleanup;
   }
 
-  /* Map the comm in the request from a COI to an APC, if needed */
-  if (TR_COMM_COI == cfg_comm->type) {
-    if (orig_req->orig_coi!=NULL) {
-      tr_notice("tr_tids_req_handler: community %s is COI but COI to APC mapping already occurred. Dropping request.",
-               orig_req->comm->buf);
+  switch(map_coi(cfg_mgr->active->ctable, fwd_req)) {
+    case MAP_COI_MAP_NOT_REQUIRED:
+      cfg_apc = cfg_comm;
+      break;
+
+    case MAP_COI_SUCCESS:
+      cfg_apc = tr_comm_table_find_comm(cfg_mgr->active->ctable, tid_req_get_comm(fwd_req));
+      tr_debug("tr_tids_req_handler: Community %.*s is a COI, mapping to APC %.*s.",
+               tid_req_get_orig_coi(fwd_req)->len, tid_req_get_orig_coi(fwd_req)->buf,
+               tr_comm_get_id(cfg_apc)->len, tr_comm_get_id(cfg_apc)->buf);
+      break;
+
+    case MAP_COI_ALREADY_MAPPED:
+      tr_notice("tr_tids_req_handler: community %.*s is COI but COI to APC mapping already occurred. Dropping request.",
+                tid_req_get_comm(orig_req)->len, tid_req_get_comm(orig_req)->buf);
       tid_resp_set_err_msg(resp, tr_new_name("Second COI to APC mapping would result, permitted only once."));
-      retval=-1;
+      retval = -1;
       goto cleanup;
-    }
-    
-    tr_debug("tr_tids_req_handler: Community was a COI, switching.");
-    /* TBD -- In theory there can be more than one?  How would that work? */
-    if ((!cfg_comm->apcs) || (!cfg_comm->apcs->id)) {
-      tr_notice("No valid APC for COI %s.", orig_req->comm->buf);
+
+    case MAP_COI_NO_APC:
+      tr_notice("No valid APC for COI %.*s.",
+                tid_req_get_comm(orig_req)->len, tid_req_get_comm(orig_req)->buf);
       tid_resp_set_err_msg(resp, tr_new_name("No valid APC for community"));
-      retval=-1;
+      retval = -1;
       goto cleanup;
-    }
-    apc = tr_dup_name(cfg_comm->apcs->id);
 
-    /* Check that the APC is configured */
-    if (NULL == (cfg_apc = tr_comm_table_find_comm(cfg_mgr->active->ctable, apc))) {
-      tr_notice("tr_tids_req_hander: Request for unknown comm: %s.", apc->buf);
+    case MAP_COI_INVALID_APC:
+      tr_notice("tr_tids_req_hander: Request for unknown APC.");
       tid_resp_set_err_msg(resp, tr_new_name("Unknown APC"));
-      retval=-1;
+      retval = -1;
       goto cleanup;
-    }
 
-    fwd_req->comm = apc;
-    fwd_req->orig_coi = orig_req->comm;
-
-    /* Check that rp_realm is a  member of this APC */
-    if (NULL == (tr_comm_find_rp(cfg_mgr->active->ctable, cfg_apc, orig_req->rp_realm))) {
-      tr_notice("tr_tids_req_hander: RP Realm (%s) not member of community (%s).", orig_req->rp_realm->buf, orig_req->comm->buf);
-      tid_resp_set_err_msg(resp, tr_new_name("RP APC membership error"));
-      retval=-1;
+    default:
+      tr_notice("tr_tids_req_hander: Unexpected error mapping COI to APC.");
+      retval = -1;
       goto cleanup;
-    }
   }
 
-  /* Look up the route for this community/realm. */
+  /* cfg_comm is now the original community, and cfg_apc is the APC it belongs to. These
+   * may both be the same. If not, check that rp_realm is a  member of the mapped APC */
+  if ((cfg_apc != cfg_comm)
+      && (NULL == tr_comm_find_rp(cfg_mgr->active->ctable,
+                                  cfg_apc,
+                                  tid_req_get_rp_realm(fwd_req)))) {
+    tr_notice("tr_tids_req_hander: RP Realm (%.*s) not member of mapped APC (%.*s).",
+              tid_req_get_rp_realm(fwd_req)->len, tid_req_get_rp_realm(fwd_req)->buf,
+              tr_comm_get_id(cfg_apc)->len, tr_comm_get_id(cfg_apc)->buf);
+    tid_resp_set_err_msg(resp, tr_new_name("RP community membership error"));
+    retval=-1;
+    goto cleanup;
+  }
+
+  /* Look up the route for forwarding request's community/realm. */
   tr_debug("tr_tids_req_handler: looking up route.");
-  route=trps_get_selected_route(trps, orig_req->comm, orig_req->realm);
+  route=trps_get_selected_route(trps, fwd_req->comm, fwd_req->realm);
   if (route==NULL) {
     /* No route. Use default AAA servers if we have them. */
-    tr_debug("tr_tids_req_handler: No route for realm %s, defaulting.", orig_req->realm->buf);
+    tr_debug("tr_tids_req_handler: No route for realm %s, defaulting.", fwd_req->realm->buf);
     if (NULL == (aaa_servers = tr_default_server_lookup(cfg_mgr->active->default_servers,
-                                                        orig_req->comm))) {
+                                                        fwd_req->comm))) {
       tr_notice("tr_tids_req_handler: No default AAA servers, discarded.");
       tid_resp_set_err_msg(resp, tr_new_name("No path to AAA Server(s) for realm"));
       retval = -1;
@@ -423,13 +495,13 @@ static int tr_tids_req_handler(TIDS_INSTANCE *tids,
     }
     idp_shared = 0;
   } else {
-    /* Found a route. Determine the AAA servers or next hop address. */
+    /* Found a route. Determine the AAA servers or next hop address for the request we are forwarding. */
     tr_debug("tr_tids_req_handler: found route.");
     if (trp_route_is_local(route)) {
       tr_debug("tr_tids_req_handler: route is local.");
       aaa_servers = tr_idp_aaa_server_lookup(cfg_mgr->active->ctable->idp_realms,
-                                             orig_req->realm,
-                                             orig_req->comm,
+                                             fwd_req->realm,
+                                             fwd_req->comm,
                                              &idp_shared);
     } else {
       tr_debug("tr_tids_req_handler: route not local.");
@@ -437,22 +509,23 @@ static int tr_tids_req_handler(TIDS_INSTANCE *tids,
       idp_shared = 0;
     }
 
-    /* Since we aren't defaulting, check idp coi and apc membership */
-    if (NULL == (tr_comm_find_idp(cfg_mgr->active->ctable, cfg_comm, fwd_req->realm))) {
-      tr_notice("tr_tids_req_handler: IDP Realm (%s) not member of community (%s).", orig_req->realm->buf, orig_req->comm->buf);
+    /* Since we aren't defaulting, check idp coi and apc membership of the original request */
+    if (NULL == (tr_comm_find_idp(cfg_mgr->active->ctable, cfg_comm, orig_req->realm))) {
+      tr_notice("tr_tids_req_handler: IDP Realm (%s) not member of community (%s).", orig_req->realm->buf, cfg_comm->id->buf);
       tid_resp_set_err_msg(resp, tr_new_name("IDP community membership error"));
       retval=-1;
       goto cleanup;
     }
-    if ( cfg_apc && (NULL == (tr_comm_find_idp(cfg_mgr->active->ctable, cfg_apc, fwd_req->realm)))) {
-      tr_notice("tr_tids_req_handler: IDP Realm (%s) not member of APC (%s).", orig_req->realm->buf, orig_req->comm->buf);
+    if ( cfg_apc && (NULL == (tr_comm_find_idp(cfg_mgr->active->ctable, cfg_apc, orig_req->realm)))) {
+      tr_notice("tr_tids_req_handler: IDP Realm (%s) not member of APC (%s).", orig_req->realm->buf, cfg_apc->id->buf);
       tid_resp_set_err_msg(resp, tr_new_name("IDP APC membership error"));
       retval=-1;
       goto cleanup;
     }
   }
 
-  /* Make sure we came through with a AAA server. If not, we can't handle the request. */
+  /* Make sure we came through with a AAA server. If not, we can't handle the request.
+   * Report using the original request, not translated values. */
   if (NULL == aaa_servers) {
     tr_notice("tr_tids_req_handler: no route or AAA server for realm (%s) in community (%s).",
               orig_req->realm->buf, orig_req->comm->buf);
@@ -463,12 +536,12 @@ static int tr_tids_req_handler(TIDS_INSTANCE *tids,
 
   /* send a TID request to the AAA server(s), and get the answer(s) */
   tr_debug("tr_tids_req_handler: sending TID request(s).");
-  if (cfg_apc)
-    expiration_interval = cfg_apc->expiration_interval;
-  else expiration_interval = cfg_comm->expiration_interval;
+  /* Use the smaller of the APC's expiration interval and the expiration interval of the incoming request */
+  expiration_interval = cfg_apc->expiration_interval;
   if (fwd_req->expiration_interval)
     fwd_req->expiration_interval =  (expiration_interval < fwd_req->expiration_interval) ? expiration_interval : fwd_req->expiration_interval;
-  else fwd_req->expiration_interval = expiration_interval;
+  else
+    fwd_req->expiration_interval = expiration_interval;
 
   /* Set up message queue for replies from req forwarding threads */
   mq=tr_mq_new(tmp_ctx);
index 9dc1f8d..e075d58 100644 (file)
@@ -876,6 +876,16 @@ void tr_config_changed(TR_CFG *new_cfg, void *cookie)
   tr->tids->hostname = new_cfg->internal->hostname;
   tr->mons->hostname = new_cfg->internal->hostname;
 
+  /* Update the authorized monitoring gss names */
+  if (tr->mons->authorized_gss_names) {
+    tr_debug("tr_config_changed: freeing tr->mons->authorized_gss_names");
+    tr_gss_names_free(tr->mons->authorized_gss_names);
+  }
+  tr->mons->authorized_gss_names = tr_gss_names_dup(tr->mons, new_cfg->internal->monitoring_credentials);
+  if (tr->mons->authorized_gss_names == NULL) {
+    tr_err("tr_config_changed: Error configuring monitoring credentials");
+  }
+
   trps_set_connect_interval(trps, new_cfg->internal->trp_connect_interval);
   trps_set_update_interval(trps, new_cfg->internal->trp_update_interval);
   trps_set_sweep_interval(trps, new_cfg->internal->trp_sweep_interval);
index 1d30cbc..8e1809b 100644 (file)
@@ -88,15 +88,13 @@ static json_t *expiry_to_json_string(TRP_ROUTE *route)
   char *s = NULL;
   json_t *jstr = NULL;
 
-  if (tr_cmp_timespec(trp_route_get_expiry(route), &ts_zero) == 0) {
-    s = strdup("");
-  } else {
+  if (tr_cmp_timespec(trp_route_get_expiry(route), &ts_zero) > 0) {
     s = timespec_to_str(trp_route_get_expiry(route));
-  }
 
-  if (s) {
-    jstr = json_string(s);
-    free(s);
+    if (s) {
+      jstr = json_string(s);
+      free(s);
+    }
   }
 
   return jstr;
@@ -111,6 +109,12 @@ do {                                           \
     goto cleanup;                              \
 } while (0)
 
+#define OBJECT_SET_OR_SKIP(jobj, key, val)     \
+do {                                           \
+  if (val)                                     \
+    json_object_set_new((jobj),(key),(val));   \
+} while (0)
+
 json_t *trp_route_to_json(TRP_ROUTE *route)
 {
   json_t *route_json = NULL;
@@ -122,13 +126,15 @@ json_t *trp_route_to_json(TRP_ROUTE *route)
 
   OBJECT_SET_OR_FAIL(route_json, "community", tr_name_to_json_string(trp_route_get_comm(route)));
   OBJECT_SET_OR_FAIL(route_json, "realm", tr_name_to_json_string(trp_route_get_realm(route)));
-  OBJECT_SET_OR_FAIL(route_json, "peer", tr_name_to_json_string(trp_route_get_peer(route)));
+  if (trp_route_get_peer(route)->len > 0)
+    OBJECT_SET_OR_FAIL(route_json, "peer", tr_name_to_json_string(trp_route_get_peer(route)));
   OBJECT_SET_OR_FAIL(route_json, "metric", json_integer(trp_route_get_metric(route)));
   OBJECT_SET_OR_FAIL(route_json, "trust_router", tr_name_to_json_string(trp_route_get_trust_router(route)));
-  OBJECT_SET_OR_FAIL(route_json, "next_hop", tr_name_to_json_string(trp_route_get_next_hop(route)));
+  if (trp_route_get_next_hop(route)->len > 0)
+    OBJECT_SET_OR_FAIL(route_json, "next_hop", tr_name_to_json_string(trp_route_get_next_hop(route)));
   OBJECT_SET_OR_FAIL(route_json, "selected", json_boolean(trp_route_is_selected(route)));
   OBJECT_SET_OR_FAIL(route_json, "local", json_boolean(trp_route_is_local(route)));
-  OBJECT_SET_OR_FAIL(route_json, "expires", expiry_to_json_string(route));
+  OBJECT_SET_OR_SKIP(route_json, "expires", expiry_to_json_string(route));
 
   /* succeeded - set the return value and increment the reference count */
   retval = route_json;
index f0000ec..b962bcd 100644 (file)
@@ -1,6 +1,6 @@
 %global optflags %{optflags} -Wno-parentheses
 Name:           trust_router
-Version:        3.3.0
+Version:        3.4.0~1
 Release:        1%{?dist}
 Summary:        Moonshot Trust Router