Merge branch 'jennifer/march2016-patches'
[trust_router.git] / common / tr_config.c
index ff92fd0..587a47c 100644 (file)
 #include <talloc.h>
 
 #include <tr_config.h>
+#include <tr_debug.h>
 #include <tr.h>
 #include <tr_filter.h>
 #include <trust_router/tr_constraint.h>
 
-void tr_print_config (FILE *stream, TR_CFG *cfg) {
-  fprintf(stream, "tr_print_config: Not yet implemented.\n");
-  return;
+void tr_print_config (TR_CFG *cfg) {
+  tr_notice("tr_print_config: Logging running trust router configuration.");
+  tr_print_comms(cfg->comms);
+}
+
+void tr_print_comms (TR_COMM *comm_list) {
+  TR_COMM *comm = NULL;
+
+  for (comm = comm_list; NULL != comm; comm = comm->next) {
+    tr_notice("tr_print_config: Community %s:", comm->id->buf);
+
+    tr_notice("tr_print_config:  - Member IdPs:");
+    tr_print_comm_idps(comm->idp_realms);
+
+    tr_notice("tr_print_config:  - Member RPs:");
+    tr_print_comm_rps(comm->rp_realms);
+  }
+}
+
+void tr_print_comm_idps (TR_IDP_REALM *idp_list) {
+  TR_IDP_REALM *idp = NULL;
+
+  for (idp = idp_list; NULL != idp; idp = idp->comm_next) {
+    tr_notice("tr_print_config:    - @%s", idp->realm_id->buf);
+  }
+}
+
+void tr_print_comm_rps(TR_RP_REALM *rp_list) {
+  TR_RP_REALM *rp = NULL;
+
+  for (rp = rp_list; NULL != rp; rp = rp->next) {
+    tr_notice("tr_print_config:    - %s", rp->realm_name->buf);
+  }
 }
 
 void tr_cfg_free (TR_CFG *cfg) {
@@ -61,6 +92,10 @@ TR_CFG_RC tr_apply_new_config (TR_INSTANCE *tr) {
     tr_cfg_free(tr->active_cfg);
 
   tr->active_cfg = tr->new_cfg;
+
+  tr_log_threshold(tr->active_cfg->internal->log_threshold);
+  tr_console_threshold(tr->active_cfg->internal->console_threshold);
+
   return TR_CFG_SUCCESS;
 }
 
@@ -69,6 +104,9 @@ static TR_CFG_RC tr_cfg_parse_internal (TR_CFG *trc, json_t *jcfg) {
   json_t *jmtd = NULL;
   json_t *jtp = NULL;
   json_t *jhname = NULL;
+  json_t *jlog = NULL;
+  json_t *jconthres = NULL;
+  json_t *jlogthres = NULL;
 
   if ((!trc) || (!jcfg))
     return TR_CFG_BAD_PARAMS;
@@ -85,7 +123,7 @@ static TR_CFG_RC tr_cfg_parse_internal (TR_CFG *trc, json_t *jcfg) {
       if (json_is_number(jmtd)) {
        trc->internal->max_tree_depth = json_integer_value(jmtd);
       } else {
-       fprintf(stderr,"tr_cfg_parse_internal: Parsing error, max_tree_depth is not a number.\n");
+       tr_debug("tr_cfg_parse_internal: Parsing error, max_tree_depth is not a number.");
        return TR_CFG_NOPARSE;
       }
     } else {
@@ -96,7 +134,7 @@ static TR_CFG_RC tr_cfg_parse_internal (TR_CFG *trc, json_t *jcfg) {
       if (json_is_number(jtp)) {
        trc->internal->tids_port = json_integer_value(jtp);
       } else {
-       fprintf(stderr,"tr_cfg_parse_internal: Parsing error, port is not a number.\n");
+       tr_debug("tr_cfg_parse_internal: Parsing error, port is not a number.");
        return TR_CFG_NOPARSE;
       }
     } else {
@@ -107,11 +145,42 @@ static TR_CFG_RC tr_cfg_parse_internal (TR_CFG *trc, json_t *jcfg) {
       if (json_is_string(jhname)) {
        trc->internal->hostname = json_string_value(jhname);
       } else {
-       fprintf(stderr,"tr_cfg_parse_internal: Parsing error, hostname is not a string.\n");
+       tr_debug("tr_cfg_parse_internal: Parsing error, hostname is not a string.");
        return TR_CFG_NOPARSE;
       }
     }
-    fprintf(stderr, "tr_cfg_parse_internal: Internal config parsed.\n");
+
+    if (NULL != (jlog = json_object_get(jint, "logging"))) {
+      if (NULL != (jlogthres = json_object_get(jlog, "log_threshold"))) {
+        if (json_is_string(jlogthres)) {
+                 trc->internal->log_threshold = str2sev(json_string_value(jlogthres));
+        } else {
+          tr_debug("tr_cfg_parse_internal: Parsing error, log_threshold is not a string.");
+          return TR_CFG_NOPARSE;
+        }
+      } else {
+        /* If not configured, use the default */
+        trc->internal->log_threshold = TR_DEFAULT_LOG_THRESHOLD;
+      }
+
+      if (NULL != (jconthres = json_object_get(jlog, "console_threshold"))) {
+        if (json_is_string(jconthres)) {
+            trc->internal->console_threshold = str2sev(json_string_value(jconthres));
+        } else {
+          tr_debug("tr_cfg_parse_internal: Parsing error, console_threshold is not a string.");
+          return TR_CFG_NOPARSE;
+        }
+      } else {
+        /* If not configured, use the default */
+        trc->internal->console_threshold = TR_DEFAULT_CONSOLE_THRESHOLD;
+      }
+    } else {
+        /* If not configured, use the default */
+        trc->internal->console_threshold = TR_DEFAULT_CONSOLE_THRESHOLD;
+        trc->internal->log_threshold = TR_DEFAULT_LOG_THRESHOLD;
+    }
+
+    tr_debug("tr_cfg_parse_internal: Internal config parsed.");
     return TR_CFG_SUCCESS;
   }
   return TR_CFG_SUCCESS;
@@ -127,13 +196,13 @@ static TR_CONSTRAINT *tr_cfg_parse_one_constraint (TR_CFG *trc, char *ctype, jso
       (0 >= json_array_size(jc)) ||
       (TR_MAX_CONST_MATCHES < json_array_size(jc)) ||
       (!json_is_string(json_array_get(jc, 0)))) {
-    fprintf(stderr, "tr_cfg_parse_one_constraint: config error.\n");
+    tr_debug("tr_cfg_parse_one_constraint: config error.");
     *rc = TR_CFG_NOPARSE;
     return NULL;
   }
 
   if (NULL == (cons = talloc(trc, TR_CONSTRAINT))) {
-    fprintf(stderr, "tr_cfg_parse_one_constraint: Out of memory (cons).\n");
+    tr_debug("tr_cfg_parse_one_constraint: Out of memory (cons).");
     *rc = TR_CFG_NOMEM;
     return NULL;
   }
@@ -141,7 +210,7 @@ static TR_CONSTRAINT *tr_cfg_parse_one_constraint (TR_CFG *trc, char *ctype, jso
   memset(cons, 0, sizeof(TR_CONSTRAINT));
 
   if (NULL == (cons->type = tr_new_name(ctype))) {
-    fprintf(stderr, "tr_cfg_parse_one_constraint: Out of memory (type).\n");
+    tr_debug("tr_cfg_parse_one_constraint: Out of memory (type).");
     *rc = TR_CFG_NOMEM;
     return NULL;
   }
@@ -168,26 +237,26 @@ static TR_FILTER *tr_cfg_parse_one_filter (TR_CFG *trc, json_t *jfilt, TR_CFG_RC
 
   if ((NULL == (jftype = json_object_get(jfilt, "type"))) ||
       (!json_is_string(jftype))) {
-    fprintf(stderr, "tr_cfg_parse_one_filter: Error parsing filter type.\n");
+    tr_debug("tr_cfg_parse_one_filter: Error parsing filter type.");
     *rc = TR_CFG_NOPARSE;
     return NULL;
   }
 
   if ((NULL == (jfls = json_object_get(jfilt, "filter_lines"))) ||
       (!json_is_array(jfls))) {
-    fprintf(stderr, "tr_cfg_parse_one_filter: Error parsing filter type.\n");
+    tr_debug("tr_cfg_parse_one_filter: Error parsing filter type.");
     *rc = TR_CFG_NOPARSE;
     return NULL;
   }
 
   if (TR_MAX_FILTER_LINES < json_array_size(jfls)) {
-    fprintf(stderr, "tr_cfg_parse_one_filter: Filter has too many filter_lines, maximimum of %d.\n", TR_MAX_FILTER_LINES);
+    tr_debug("tr_cfg_parse_one_filter: Filter has too many filter_lines, maximimum of %d.", TR_MAX_FILTER_LINES);
     *rc = TR_CFG_NOPARSE;
     return NULL;
   }
 
   if (NULL == (filt = talloc(trc, TR_FILTER))) {
-    fprintf(stderr, "tr_cfg_parse_one_filter: Out of memory.\n");
+    tr_debug("tr_cfg_parse_one_filter: Out of memory.");
     *rc = TR_CFG_NOMEM;
     return NULL;
   }
@@ -198,7 +267,7 @@ static TR_FILTER *tr_cfg_parse_one_filter (TR_CFG *trc, json_t *jfilt, TR_CFG_RC
     filt->type = TR_FILTER_TYPE_RP_PERMITTED;
   }
   else {
-    fprintf(stderr, "tr_cfg_parse_one_filter: Error parsing filter type, unknown type '%s'.\n", json_string_value(jftype));
+    tr_debug("tr_cfg_parse_one_filter: Error parsing filter type, unknown type '%s'.", json_string_value(jftype));
     *rc = TR_CFG_NOPARSE;
     tr_filter_free(filt);
     return NULL;
@@ -209,7 +278,7 @@ static TR_FILTER *tr_cfg_parse_one_filter (TR_CFG *trc, json_t *jfilt, TR_CFG_RC
 
     if ((NULL == (jfaction = json_object_get(json_array_get(jfls, i), "action"))) ||
        (!json_is_string(jfaction))) {
-      fprintf(stderr, "tr_cfg_parse_one_filter: Error parsing filter action.\n");
+      tr_debug("tr_cfg_parse_one_filter: Error parsing filter action.");
       *rc = TR_CFG_NOPARSE;
       tr_filter_free(filt);
       return NULL;
@@ -218,21 +287,21 @@ static TR_FILTER *tr_cfg_parse_one_filter (TR_CFG *trc, json_t *jfilt, TR_CFG_RC
     if ((NULL == (jfspecs = json_object_get(json_array_get(jfls, i), "filter_specs"))) ||
        (!json_is_array(jfspecs)) ||
        (0 == json_array_size(jfspecs))) {
-      fprintf(stderr, "tr_cfg_parse_one_filter: Error parsing filter specs.\n");
+      tr_debug("tr_cfg_parse_one_filter: Error parsing filter specs.");
       *rc = TR_CFG_NOPARSE;
       tr_filter_free(filt);
       return NULL;
     }
   
     if (TR_MAX_FILTER_SPECS < json_array_size(jfspecs)) {
-      fprintf(stderr, "tr_cfg_parse_one_filter: Filter has too many filter_specs, maximimum of %d.\n", TR_MAX_FILTER_SPECS);
+      tr_debug("tr_cfg_parse_one_filter: Filter has too many filter_specs, maximimum of %d.", TR_MAX_FILTER_SPECS);
       *rc = TR_CFG_NOPARSE;
       tr_filter_free(filt);
       return NULL;
     }
 
     if (NULL == (filt->lines[i] = talloc(trc, TR_FLINE))) {
-      fprintf(stderr, "tr_cfg_parse_one_filter: Out of memory (fline).\n");
+      tr_debug("tr_cfg_parse_one_filter: Out of memory (fline).");
       *rc = TR_CFG_NOMEM;
       tr_filter_free(filt);
       return NULL;
@@ -247,7 +316,7 @@ static TR_FILTER *tr_cfg_parse_one_filter (TR_CFG *trc, json_t *jfilt, TR_CFG_RC
       filt->lines[i]->action = TR_FILTER_ACTION_REJECT;
     }
     else {
-      fprintf(stderr, "tr_cfg_parse_one_filter: Error parsing filter action, unknown action' %s'.\n", json_string_value(jfaction));
+      tr_debug("tr_cfg_parse_one_filter: Error parsing filter action, unknown action' %s'.", json_string_value(jfaction));
       *rc = TR_CFG_NOPARSE;
       tr_filter_free(filt);
       return NULL;
@@ -259,7 +328,7 @@ static TR_FILTER *tr_cfg_parse_one_filter (TR_CFG *trc, json_t *jfilt, TR_CFG_RC
        (TR_MAX_CONST_MATCHES >= json_array_size(jrc))) {
 
       if (NULL == (filt->lines[i]->realm_cons = tr_cfg_parse_one_constraint(trc, "realm", jrc, rc))) {
-       fprintf(stderr, "tr_cfg_parse_one_filter: Error parsing realm constraint");
+       tr_debug("tr_cfg_parse_one_filter: Error parsing realm constraint");
       tr_filter_free(filt);
       return NULL;
       }
@@ -271,7 +340,7 @@ static TR_FILTER *tr_cfg_parse_one_filter (TR_CFG *trc, json_t *jfilt, TR_CFG_RC
        (TR_MAX_CONST_MATCHES >= json_array_size(jdc))) {
 
       if (NULL == (filt->lines[i]->domain_cons = tr_cfg_parse_one_constraint(trc, "domain", jdc, rc))) {
-       fprintf(stderr, "tr_cfg_parse_one_filter: Error parsing domain constraint");
+       tr_debug("tr_cfg_parse_one_filter: Error parsing domain constraint");
       tr_filter_free(filt);
       return NULL;
       }
@@ -284,14 +353,14 @@ static TR_FILTER *tr_cfg_parse_one_filter (TR_CFG *trc, json_t *jfilt, TR_CFG_RC
          (!json_is_string(jffield)) ||
          (NULL == (jfmatch = json_object_get(json_array_get(jfspecs, j), "match"))) ||
          (!json_is_string(jfmatch))) {
-       fprintf (stderr, "tr_cfg_parse_one_filter: Error parsing filter field and match for filter spec %d, filter line %d.\n", i, j);
+       tr_debug("tr_cfg_parse_one_filter: Error parsing filter field and match for filter spec %d, filter line %d.", i, j);
        *rc = TR_CFG_NOPARSE;
        tr_filter_free(filt);
        return NULL;
       }
 
       if (NULL == (filt->lines[i]->specs[j] = talloc(trc, TR_FSPEC))) {
-       fprintf(stderr, "tr_cfg_parse_one_filter: Out of memory.\n");
+       tr_debug("tr_cfg_parse_one_filter: Out of memory.");
        *rc = TR_CFG_NOMEM;
        tr_filter_free(filt);
        return NULL;
@@ -301,7 +370,7 @@ static TR_FILTER *tr_cfg_parse_one_filter (TR_CFG *trc, json_t *jfilt, TR_CFG_RC
     
       if ((NULL == (filt->lines[i]->specs[j]->field = tr_new_name((char *)json_string_value(jffield)))) ||
          (NULL == (filt->lines[i]->specs[j]->match = tr_new_name((char *)json_string_value(jfmatch))))) {
-       fprintf(stderr, "tr_cfg_parse_one_filter: Out of memory.\n");
+       tr_debug("tr_cfg_parse_one_filter: Out of memory.");
        *rc = TR_CFG_NOMEM;
        tr_filter_free(filt);
        return NULL;
@@ -321,7 +390,7 @@ static TR_RP_CLIENT *tr_cfg_parse_one_rp_client (TR_CFG *trc, json_t *jrp, TR_CF
   int i = 0;
 
   if ((!trc) || (!jrp) || (!rc)) {
-    fprintf(stderr, "tr_cfg_parse_one_rp_realm: Bad parameters.\n");
+    tr_debug("tr_cfg_parse_one_rp_realm: Bad parameters.");
     if (rc)
       *rc = TR_CFG_BAD_PARAMS;
     return NULL;
@@ -329,14 +398,14 @@ static TR_RP_CLIENT *tr_cfg_parse_one_rp_client (TR_CFG *trc, json_t *jrp, TR_CF
 
   if ((NULL == (jgns = json_object_get(jrp, "gss_names"))) ||
       (!json_is_array(jgns))) {
-    fprintf(stderr, "tr_cfg_parse_one_rp_client: Error parsing RP client configuration, no GSS names.\n");
+    tr_debug("tr_cfg_parse_one_rp_client: Error parsing RP client configuration, no GSS names.");
     *rc = TR_CFG_NOPARSE;
     return NULL;
   }
 
   /* TBD -- Support more than one filter per RP client? */
   if (NULL == (jfilt = json_object_get(jrp, "filter"))) {
-    fprintf(stderr, "tr_cfg_parse_one_rp_client: Error parsing RP client configuration, no filter.\n");
+    tr_debug("tr_cfg_parse_one_rp_client: Error parsing RP client configuration, no filter.");
     *rc = TR_CFG_NOPARSE;
     return NULL;
   }
@@ -345,19 +414,19 @@ static TR_RP_CLIENT *tr_cfg_parse_one_rp_client (TR_CFG *trc, json_t *jrp, TR_CF
   if ((NULL == (jftype = json_object_get(jfilt, "type"))) ||
       (!json_is_string(jftype)) ||
       (strcmp(json_string_value(jftype), "rp_permitted"))) {
-    fprintf(stderr, "tr_cfg_parse_one_rp_client: Error parsing RP client filter type.\n");
+    tr_debug("tr_cfg_parse_one_rp_client: Error parsing RP client filter type.");
     *rc = TR_CFG_NOPARSE;
     return NULL;
   }
 
   if (TR_MAX_GSS_NAMES < json_array_size(jgns)) {
-    fprintf(stderr, "tr_cfg_parse_one_rp_client: RP Client has too many GSS Names.\n");
+    tr_debug("tr_cfg_parse_one_rp_client: RP Client has too many GSS Names.");
     *rc = TR_CFG_NOPARSE;
     return NULL;
   }
 
   if (NULL == (rp = talloc(trc, TR_RP_CLIENT))) {
-    fprintf(stderr, "tr_cfg_parse_one_rp_realm: Out of memory.\n");
+    tr_debug("tr_cfg_parse_one_rp_realm: Out of memory.");
     *rc = TR_CFG_NOMEM;
     return NULL;
   }
@@ -366,16 +435,14 @@ static TR_RP_CLIENT *tr_cfg_parse_one_rp_client (TR_CFG *trc, json_t *jrp, TR_CF
 
   /* TBD -- support more than one filter entry per RP Client? */
   if (NULL == (rp->filter = tr_cfg_parse_one_filter(trc, jfilt, rc))) {
-    fprintf(stderr, "tr_cfg_parse_one_rp_client: Error parsing filter.\n");
-    free(rp);
+    tr_debug("tr_cfg_parse_one_rp_client: Error parsing filter.");
     *rc = TR_CFG_NOPARSE;
     return NULL;
   }
     
   for (i = 0; i < json_array_size(jgns); i++) {
     if (NULL == (rp->gss_names[i] = tr_new_name ((char *)json_string_value(json_array_get(jgns, i))))) {
-      fprintf(stderr, "tr_cfg_parse_one_rp_client: No memory for GSS Name.\n");
-      free(rp);
+      tr_debug("tr_cfg_parse_one_rp_client: No memory for GSS Name.");
       *rc = TR_CFG_NOMEM;
       return NULL;
     }
@@ -405,7 +472,7 @@ static TR_CFG_RC tr_cfg_parse_rp_clients (TR_CFG *trc, json_t *jcfg) {
                                                   &rc))) {
        return rc;
       }
-      fprintf(stderr, "tr_cfg_parse_rp_clients: RP client configured -- first gss: %s", rp->gss_names[0]->buf);
+      tr_debug("tr_cfg_parse_rp_clients: RP client configured -- first gss: %s", rp->gss_names[0]->buf);
       rp->next = trc->rp_clients;
       trc->rp_clients = rp;
     }
@@ -417,13 +484,13 @@ static TR_AAA_SERVER *tr_cfg_parse_one_aaa_server (TR_CFG *trc, json_t *jaddr, T
   TR_AAA_SERVER *aaa = NULL;
 
   if ((!trc) || (!jaddr) || (!json_is_string(jaddr))) {
-    fprintf(stderr, "tr_cfg_parse_one_aaa_server: Bad parameters.\n");
+    tr_debug("tr_cfg_parse_one_aaa_server: Bad parameters.");
     *rc = TR_CFG_BAD_PARAMS;
     return NULL;
   }
 
   if (NULL == (aaa = talloc(trc, TR_AAA_SERVER))) {
-    fprintf(stderr, "tr_cfg_parse_one_aaa_server: Out of memory.\n");
+    tr_debug("tr_cfg_parse_one_aaa_server: Out of memory.");
     *rc = TR_CFG_NOMEM;
     return NULL;
   }
@@ -446,7 +513,7 @@ static TR_AAA_SERVER *tr_cfg_parse_aaa_servers (TR_CFG *trc, json_t *jaaas, TR_C
       return NULL;
     }
     /* TBD -- IPv6 addresses */
-    //    fprintf(stderr, "tr_cfg_parse_aaa_servers: Configuring AAA Server: ip_addr = %s.\n", inet_ntoa(temp_aaa->aaa_server_addr));
+    //    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;
   }
@@ -460,14 +527,14 @@ static TR_APC *tr_cfg_parse_apcs (TR_CFG *trc, json_t *japcs, TR_CFG_RC *rc)
   *rc = TR_CFG_SUCCESS;                /* presume success */
 
   if ((!trc) || (!japcs) || (!rc)) {
-    fprintf(stderr, "tr_cfg_parse_apcs: Bad parameters.\n");
+    tr_debug("tr_cfg_parse_apcs: Bad parameters.");
     if (rc) 
       *rc = TR_CFG_BAD_PARAMS;
     return NULL;
   }
 
   if (NULL == (apc = talloc(trc, TR_APC))) {
-    fprintf (stderr, "tr_cfg_parse_apcs: Out of memory.\n");
+    tr_debug("tr_cfg_parse_apcs: Out of memory.");
     *rc = TR_CFG_NOMEM;
     return NULL;
   }
@@ -480,7 +547,7 @@ static TR_APC *tr_cfg_parse_apcs (TR_CFG *trc, json_t *japcs, TR_CFG_RC *rc)
     return NULL;
 
   if (NULL == (apc->id = tr_new_name((char *)json_string_value(json_array_get(japcs, 0))))) {
-    fprintf(stderr, "tr_cfg_parse_apcs: No memory for APC name.\n");
+    tr_debug("tr_cfg_parse_apcs: No memory for APC name.");
     *rc = TR_CFG_NOMEM;
     return NULL;
   }
@@ -496,14 +563,14 @@ static TR_IDP_REALM *tr_cfg_parse_one_idp_realm (TR_CFG *trc, json_t *jidp, TR_C
   json_t *japcs = NULL;
 
   if ((!trc) || (!jidp) || (!rc)) {
-    fprintf(stderr, "tr_cfg_parse_one_idp_realm: Bad parameters.\n");
+    tr_debug("tr_cfg_parse_one_idp_realm: Bad parameters.");
     if (rc)
       *rc = TR_CFG_BAD_PARAMS;
     return NULL;
   }
 
   if (NULL == (idp = talloc(trc, TR_IDP_REALM))) {
-    fprintf(stderr, "tr_cfg_parse_one_idp_realm: Out of memory.\n");
+    tr_debug("tr_cfg_parse_one_idp_realm: Out of memory.");
     *rc = TR_CFG_NOMEM;
     return NULL;
   }
@@ -516,8 +583,7 @@ static TR_IDP_REALM *tr_cfg_parse_one_idp_realm (TR_CFG *trc, json_t *jidp, TR_C
       (!json_is_string(jscfg)) ||
       (NULL == (jsrvrs = json_object_get(jidp, "aaa_servers"))) ||
       (!json_is_array(jsrvrs))) {
-    fprintf(stderr, "tr_cfg_parse_one_idp_realm: Error parsing IDP realm configuration.\n");
-    free(idp);
+    tr_debug("tr_cfg_parse_one_idp_realm: Error parsing IDP realm configuration.");
     *rc = TR_CFG_NOPARSE;
     return NULL;
   }
@@ -529,26 +595,23 @@ static TR_IDP_REALM *tr_cfg_parse_one_idp_realm (TR_CFG *trc, json_t *jidp, TR_C
   }
 
   if (NULL == (idp->realm_id = tr_new_name((char *)json_string_value(jrid)))) {
-    free(idp);
-    fprintf(stderr, "tr_cfg_parse_one_idp_realm: No memory for realm id.\n");
+    tr_debug("tr_cfg_parse_one_idp_realm: No memory for realm id.");
     *rc = TR_CFG_NOMEM;
     return NULL;
   }
 
   if (NULL == (idp->aaa_servers = tr_cfg_parse_aaa_servers(trc, jsrvrs, rc))) {
-    fprintf(stderr, "tr_cfg_parse_one_idp_realm: Can't parse AAA servers for realm %s.\n", idp->realm_id->buf);
+    tr_debug("tr_cfg_parse_one_idp_realm: Can't parse AAA servers for realm %s.", idp->realm_id->buf);
     tr_free_name(idp->realm_id);
-    free(idp);
     return NULL;
   }
 
   if ((NULL != (japcs = json_object_get(jidp, "apcs"))) &&
       (json_is_array(japcs))) {
     if (NULL == (idp->apcs = tr_cfg_parse_apcs(trc, japcs, rc))) {
-      fprintf(stderr, "tr_cfg_parse_one_idp_realm: Can't parse APCs for realm %s .\n", idp->realm_id->buf);
+      tr_debug("tr_cfg_parse_one_idp_realm: Can't parse APCs for realm %s .", idp->realm_id->buf);
       tr_free_name(idp->realm_id);
       /* TBD -- free aaa_servers */;
-      free(idp);
       return NULL;
     }
   } 
@@ -576,7 +639,7 @@ static TR_CFG_RC tr_cfg_parse_default_servers (TR_CFG *trc, json_t *jcfg)
                                                  &rc))) {
        return rc;
       }
-      fprintf(stderr, "tr_cfg_parse_default_servers: Default server configured.\n");
+      tr_debug("tr_cfg_parse_default_servers: Default server configured: %s", ds->hostname->buf);
       ds->next = trc->default_servers;
       trc->default_servers = ds;
     }
@@ -604,7 +667,7 @@ static TR_CFG_RC tr_cfg_parse_idp_realms (TR_CFG *trc, json_t *jcfg)
                                                    &rc))) {
        return rc;
       }
-      fprintf(stderr, "tr_cfg_parse_idp_realms: IDP realm configured: %s.\n", idp->realm_id->buf);
+      tr_debug("tr_cfg_parse_idp_realms: IDP realm configured: %s.", idp->realm_id->buf);
       idp->next = trc->idp_realms;
       trc->idp_realms = idp;
     }
@@ -616,6 +679,7 @@ static TR_CFG_RC tr_cfg_parse_idp_realms (TR_CFG *trc, json_t *jcfg)
 static TR_IDP_REALM *tr_cfg_parse_comm_idps (TR_CFG *trc, json_t *jidps, TR_CFG_RC *rc)
 {
   TR_IDP_REALM *idp = NULL;
+  TR_IDP_REALM *found_idp = NULL;
   TR_IDP_REALM *temp_idp = NULL;
   int i = 0;
 
@@ -628,14 +692,25 @@ static TR_IDP_REALM *tr_cfg_parse_comm_idps (TR_CFG *trc, json_t *jidps, TR_CFG_
   }
 
   for (i = 0; i < json_array_size(jidps); i++) {
-    if (NULL == (temp_idp = (tr_cfg_find_idp(trc, 
+    if (NULL == (temp_idp = talloc(trc, TR_IDP_REALM))) {
+      tr_debug("tr_cfg_parse_comm_idps: Can't allocate memory for IdP Realm.");
+      if (rc)
+       *rc = TR_CFG_NOMEM;
+      return NULL;
+    }
+    memset (temp_idp, 0, sizeof(TR_IDP_REALM));
+
+    if (NULL == (found_idp = (tr_cfg_find_idp(trc, 
                                             tr_new_name((char *)json_string_value(json_array_get(jidps, i))), 
                                             rc)))) {
-      fprintf(stderr, "tr_cfg_parse_comm_idps: Unknown IDP %s.\n", 
+      tr_debug("tr_cfg_parse_comm_idps: Unknown IDP %s.", 
              (char *)json_string_value(json_array_get(jidps, i)));
       return NULL;
     }
 
+    // We *MUST* do a dereferenced copy here or the second community will corrupt the linked list we create here.
+    *temp_idp = *found_idp;
+
     temp_idp->comm_next = idp;
     idp = temp_idp;
   }
@@ -659,7 +734,7 @@ static TR_RP_REALM *tr_cfg_parse_comm_rps (TR_CFG *trc, json_t *jrps, TR_CFG_RC
 
   for (i = (json_array_size(jrps)-1); i >= 0; i--) {
     if (NULL == (temp_rp = talloc(trc, TR_RP_REALM))) {
-      fprintf(stderr, "tr_cfg_parse_comm_rps: Can't allocate memory for RP Realm.\n");
+      tr_debug("tr_cfg_parse_comm_rps: Can't allocate memory for RP Realm.");
       if (rc)
        *rc = TR_CFG_NOMEM;
       return NULL;
@@ -667,7 +742,7 @@ static TR_RP_REALM *tr_cfg_parse_comm_rps (TR_CFG *trc, json_t *jrps, TR_CFG_RC
     memset (temp_rp, 0, sizeof(TR_RP_REALM));
 
     if (NULL == (temp_rp->realm_name = tr_new_name((char *)json_string_value(json_array_get(jrps, i))))) {
-      fprintf(stderr, "tr_cfg_parse_comm_rps: No memory for RP Realm Name.\n");
+      tr_debug("tr_cfg_parse_comm_rps: No memory for RP Realm Name.");
       if (rc)
        *rc = TR_CFG_NOMEM;
       return NULL;
@@ -689,19 +764,18 @@ static TR_COMM *tr_cfg_parse_one_comm (TR_CFG *trc, json_t *jcomm, TR_CFG_RC *rc
   json_t *jrps = NULL;
 
   if ((!trc) || (!jcomm) || (!rc)) {
-    fprintf(stderr, "tr_cfg_parse_one_comm: Bad parameters.\n");
+    tr_debug("tr_cfg_parse_one_comm: Bad parameters.");
     if (rc)
       *rc = TR_CFG_BAD_PARAMS;
     return NULL;
   }
 
-  if (NULL == (comm = talloc(trc, TR_COMM))) {
-    fprintf(stderr, "tr_cfg_parse_one_comm: Out of memory.\n");
+  if (NULL == (comm = talloc_zero(trc, TR_COMM))) {
+    tr_crit("tr_cfg_parse_one_comm: Out of memory.");
     *rc = TR_CFG_NOMEM;
     return NULL;
   }
 
-  memset(comm, 0, sizeof(TR_COMM));
 
   if ((NULL == (jid = json_object_get(jcomm, "community_id"))) ||
       (!json_is_string(jid)) ||
@@ -713,15 +787,13 @@ static TR_COMM *tr_cfg_parse_one_comm (TR_CFG *trc, json_t *jcomm, TR_CFG_RC *rc
       (!json_is_array(jidps)) ||
       (NULL == (jrps = json_object_get(jcomm, "rp_realms"))) ||
       (!json_is_array(jrps))) {
-    fprintf(stderr, "tr_cfg_parse_one_comm: Error parsing Communities configuration.\n");
-    free(comm);
+    tr_debug("tr_cfg_parse_one_comm: Error parsing Communities configuration.");
     *rc = TR_CFG_NOPARSE;
     return NULL;
   }
 
   if (NULL == (comm->id = tr_new_name((char *)json_string_value(jid)))) {
-    free(comm);
-    fprintf(stderr, "tr_cfg_parse_one_comm: No memory for community id.\n");
+    tr_debug("tr_cfg_parse_one_comm: No memory for community id.");
     *rc = TR_CFG_NOMEM;
     return NULL;
   }
@@ -731,36 +803,47 @@ static TR_COMM *tr_cfg_parse_one_comm (TR_CFG *trc, json_t *jcomm, TR_CFG_RC *rc
   } else if (0 == strcmp(json_string_value(jtype), "coi")) {
     comm->type = TR_COMM_COI;
     if (NULL == (comm->apcs = tr_cfg_parse_apcs(trc, japcs, rc))) {
-      fprintf(stderr, "tr_cfg_parse_one_comm: Can't parse APCs for COI %s.\n", comm->id->buf);
+      tr_debug("tr_cfg_parse_one_comm: Can't parse APCs for COI %s.", comm->id->buf);
       tr_free_name(comm->id);
-      free(comm);
       return NULL;
     }
   } else {
-    fprintf(stderr, "tr_cfg_parse_one_comm: Invalid community type, comm = %s, type = %s\n", comm->id->buf, json_string_value(jtype));
+    tr_debug("tr_cfg_parse_one_comm: Invalid community type, comm = %s, type = %s", comm->id->buf, json_string_value(jtype));
     tr_free_name(comm->id);
-    free(comm);
     *rc = TR_CFG_NOPARSE;
     return NULL;
   }
 
   comm->idp_realms = tr_cfg_parse_comm_idps(trc, jidps, rc);
   if (TR_CFG_SUCCESS != *rc) {
-    fprintf(stderr, "tr_cfg_parse_one_comm: Can't parse IDP realms for comm %s.\n", comm->id->buf);
+    tr_debug("tr_cfg_parse_one_comm: Can't parse IDP realms for comm %s.", comm->id->buf);
     tr_free_name(comm->id);
-    free(comm);
     return NULL;
   }
 
   comm->rp_realms = tr_cfg_parse_comm_rps(trc, jrps, rc);
   if (TR_CFG_SUCCESS != *rc) {
-    fprintf(stderr, "tr_cfg_parse_comm: Can't parse RP realms for comm %s .\n", comm->id->buf);
+    tr_debug("tr_cfg_parse_comm: Can't parse RP realms for comm %s .", comm->id->buf);
     tr_free_name(comm->id);
-    /* TBD -- free idps? */;
-    free(comm);
     return NULL;
   }
 
+  if (TR_COMM_APC == comm->type) {
+    json_t *jexpire  = json_object_get(jcomm, "expiration_interval");
+    comm->expiration_interval = 43200; /*30 days*/
+    if (jexpire) {
+       if (!json_is_integer(jexpire)) {
+         fprintf(stderr, "tr_parse_comm: expirae_interval is not an integer\n");
+         return NULL;
+       }
+       comm->expiration_interval = json_integer_value(jexpire);
+       if (comm->expiration_interval <= 10)
+         comm->expiration_interval = 11; /* Freeradius waits 10 minutes between successful TR queries*/
+       if (comm->expiration_interval > 129600) /* 90 days*/
+       comm->expiration_interval = 129600;
+    }
+  }
+  
   return comm;
 }
 
@@ -772,7 +855,7 @@ static TR_CFG_RC tr_cfg_parse_comms (TR_CFG *trc, json_t *jcfg)
   int i = 0;
 
   if ((!trc) || (!jcfg)) {
-    fprintf(stderr, "tr_cfg_parse_comms: Bad Parameters.\n");
+    tr_debug("tr_cfg_parse_comms: Bad Parameters.");
     return TR_CFG_BAD_PARAMS;
   }
 
@@ -787,7 +870,7 @@ static TR_CFG_RC tr_cfg_parse_comms (TR_CFG *trc, json_t *jcfg)
                                                &rc))) {
        return rc;
       }
-      fprintf(stderr, "tr_cfg_parse_comms: Community configured: %s.\n", comm->id->buf);
+      tr_debug("tr_cfg_parse_comms: Community configured: %s.", comm->id->buf);
       comm->next = trc->comms;
       trc->comms = comm;
     }
@@ -803,34 +886,42 @@ TR_CFG_RC tr_cfg_validate (TR_CFG *trc) {
 
   if ((NULL == trc->internal)||
       (NULL == trc->internal->hostname)) {
-    fprintf(stderr, "tr_cfg_validate: Error: No internal configuration, or no hostname.\n");
+    tr_debug("tr_cfg_validate: Error: No internal configuration, or no hostname.");
     rc = TR_CFG_ERROR;
   }
 
   if (NULL == trc->rp_clients) {
-    fprintf(stderr, "tr_cfg_validate: Error: No RP Clients configured\n");
+    tr_debug("tr_cfg_validate: Error: No RP Clients configured");
     rc = TR_CFG_ERROR;
   }
 
   if (NULL == trc->comms) {
-    fprintf(stderr, "tr_cfg_validate: Error: No Communities configured\n");
+    tr_debug("tr_cfg_validate: Error: No Communities configured");
     rc = TR_CFG_ERROR;
   }
 
   if ((NULL == trc->default_servers) && (NULL == trc->idp_realms)) {
-    fprintf(stderr, "tr_cfg_validate: Error: No default servers or IDPs configured.\n");
+    tr_debug("tr_cfg_validate: Error: No default servers or IDPs configured.");
     rc = TR_CFG_ERROR;
   }
   
   return rc;
 }
 
-TR_CFG_RC tr_parse_config (TR_INSTANCE *tr, struct dirent **cfg_files) {
+/* Join two paths and return a pointer to the result. This should be freed
+ * via talloc_free. Returns NULL on failure. */
+static char *join_paths(const char *p1, const char *p2) {
+  return talloc_asprintf(NULL, "%s/%s", p1, p2); /* returns NULL on a failure */
+}
+
+/* Reads configuration files in config_dir ("" or "./" will use the current directory) */
+TR_CFG_RC tr_parse_config (TR_INSTANCE *tr, const char *config_dir, int n, struct dirent **cfg_files) {
   json_t *jcfg;
+  json_t *jser;
   json_error_t rc;
-  int n;
+  char *file_with_path;
 
-  if ((!tr) || (!cfg_files))
+  if ((!tr) || (!cfg_files) || (n<=0))
     return TR_CFG_BAD_PARAMS;
 
   /* If there is a partial/abandoned config lying around, free it */
@@ -844,19 +935,35 @@ TR_CFG_RC tr_parse_config (TR_INSTANCE *tr, struct dirent **cfg_files) {
 
   /* Parse configuration information from each config file */
   while (n--) {
-    fprintf(stderr, "tr_read_config: Parsing %s.\n", cfg_files[n]->d_name);
-    if (NULL == (jcfg = json_load_file(cfg_files[n]->d_name, 
-                                      JSON_DISABLE_EOF_CHECK, &rc))) {
-      fprintf (stderr, "tr_read_config: Error parsing config file %s.\n", 
-              cfg_files[n]->d_name);
+    file_with_path=join_paths(config_dir, cfg_files[n]->d_name); /* must free result with talloc_free */
+    if(file_with_path == NULL) {
+      tr_crit("tr_parse_config: error joining path.");
+      return TR_CFG_NOMEM;
+    }
+    tr_debug("tr_parse_config: Parsing %s.", cfg_files[n]->d_name); /* print the filename without the path */
+    if (NULL == (jcfg = json_load_file(file_with_path, 
+                                       JSON_DISABLE_EOF_CHECK, &rc))) {
+      tr_debug("tr_parse_config: Error parsing config file %s.", 
+               cfg_files[n]->d_name);
+      talloc_free(file_with_path);
       return TR_CFG_NOPARSE;
     }
-       
+    talloc_free(file_with_path); /* done with filename */
+
+    // Look for serial number and log it if it exists
+    if (NULL != (jser = json_object_get(jcfg, "serial_number"))) {
+      if (json_is_number(jser)) {
+        tr_notice("tr_read_config: Attempting to load revision %i of %s.",
+                  (int *) json_integer_value(jser),
+                  cfg_files[n]->d_name);
+      }
+    }
+
     if ((TR_CFG_SUCCESS != tr_cfg_parse_internal(tr->new_cfg, jcfg)) ||
-       (TR_CFG_SUCCESS != tr_cfg_parse_rp_clients(tr->new_cfg, jcfg)) ||
-       (TR_CFG_SUCCESS != tr_cfg_parse_idp_realms(tr->new_cfg, jcfg)) ||
-       (TR_CFG_SUCCESS != tr_cfg_parse_default_servers(tr->new_cfg, jcfg)) ||
-       (TR_CFG_SUCCESS != tr_cfg_parse_comms(tr->new_cfg, jcfg))) {
+        (TR_CFG_SUCCESS != tr_cfg_parse_rp_clients(tr->new_cfg, jcfg)) ||
+        (TR_CFG_SUCCESS != tr_cfg_parse_idp_realms(tr->new_cfg, jcfg)) ||
+        (TR_CFG_SUCCESS != tr_cfg_parse_default_servers(tr->new_cfg, jcfg)) ||
+        (TR_CFG_SUCCESS != tr_cfg_parse_comms(tr->new_cfg, jcfg))) {
       tr_cfg_free(tr->new_cfg);
       return TR_CFG_ERROR;
     }
@@ -864,7 +971,7 @@ TR_CFG_RC tr_parse_config (TR_INSTANCE *tr, struct dirent **cfg_files) {
 
   /* make sure we got a complete, consistent configuration */
   if (TR_CFG_SUCCESS != tr_cfg_validate(tr->new_cfg)) {
-    fprintf(stderr, "tr_parse_config: Error: INVALID CONFIGURATION, EXITING\n");
+    tr_debug("tr_parse_config: Error: INVALID CONFIGURATION, EXITING");
     return TR_CFG_ERROR;
   }
 
@@ -884,7 +991,7 @@ TR_IDP_REALM *tr_cfg_find_idp (TR_CFG *tr_cfg, TR_NAME *idp_id, TR_CFG_RC *rc)
 
   for (cfg_idp = tr_cfg->idp_realms; NULL != cfg_idp; cfg_idp = cfg_idp->next) {
     if (!tr_name_cmp (idp_id, cfg_idp->realm_id)) {
-      fprintf(stderr, "tr_cfg_find_idp: Found %s.\n", idp_id->buf);
+      tr_debug("tr_cfg_find_idp: Found %s.", idp_id->buf);
       return cfg_idp;
     }
   }
@@ -906,7 +1013,7 @@ TR_RP_CLIENT *tr_cfg_find_rp (TR_CFG *tr_cfg, TR_NAME *rp_gss, TR_CFG_RC *rc)
   for (cfg_rp = tr_cfg->rp_clients; NULL != cfg_rp; cfg_rp = cfg_rp->next) {
     for (i = 0; i < TR_MAX_GSS_NAMES; i++) {
       if (!tr_name_cmp (rp_gss, cfg_rp->gss_names[i])) {
-       fprintf(stderr, "tr_cfg_find_rp: Found %s.\n", rp_gss->buf);
+       tr_debug("tr_cfg_find_rp: Found %s.", rp_gss->buf);
        return cfg_rp;
       }
     }
@@ -915,44 +1022,26 @@ TR_RP_CLIENT *tr_cfg_find_rp (TR_CFG *tr_cfg, TR_NAME *rp_gss, TR_CFG_RC *rc)
   return NULL;
 }
 
-#if 0
-json_t *tr_read_config (int n, struct dirent **cfg_files) {
-  json_t *jcfg = NULL;
-  json_t *temp = NULL;
-  json_error_t err;
-
-  if (!cfg_files)
-    return NULL;
+static int is_cfg_file(const struct dirent *dent) {
+  int n;
 
-  while (n--) {
-    fprintf(stderr, "tr_read_config: Parsing %s.\n", cfg_files[n]->d_name);
-    if (NULL == (temp = json_load_file(cfg_files[n]->d_name, JSON_DISABLE_EOF_CHECK, &err))) {
-      fprintf (stderr, "tr_read_config: Error parsing config file %s.\n", cfg_files[n]->d_name);
-      return NULL;
-    }
+  /* Only accept filenames ending in ".cfg" and starting with a character
+   * other than an ASCII '.' */
 
-    if (!jcfg) {
-      jcfg = temp;
-    }else {
-      if (-1 == json_object_update(jcfg, temp)) {
-       fprintf(stderr, "tr_read_config: Error merging config information.\n");
-       return NULL;
-      }
-    }
+  /* filename must be at least 4 characters long to be acceptable */
+  n=strlen(dent->d_name);
+  if (n < 4) {
+    return 0;
   }
 
-  fprintf(stderr, "tr_read_config: Merged configuration complete:\n%s\n", json_dumps(jcfg, 0));
-
-  return jcfg;
-}
-#endif 
-
-static int is_cfg_file(const struct dirent *dent) {
-  int n;
+  /* filename must not start with '.' */
+  if ('.' == dent->d_name[0]) {
+    return 0;
+  }
 
-  /* if the last four letters of the filename are .cfg, return true. */
-  if ((4 <= (n = strlen(dent->d_name))) &&
-      (0 == strcmp(&(dent->d_name[n-4]), ".cfg"))) {
+  /* If the above passed and the last four characters of the filename are .cfg, accept.
+   * (n.b., assumes an earlier test checked that the name is >= 4 chars long.) */
+  if (0 == strcmp(&(dent->d_name[n-4]), ".cfg")) {
     return 1;
   }
 
@@ -960,26 +1049,43 @@ static int is_cfg_file(const struct dirent *dent) {
   return 0;
 }
 
-int tr_find_config_files (struct dirent ***cfg_files) {
+/* Find configuration files in a particular directory. Returns the
+ * number of entries found, 0 if none are found, or <0 for some
+ * errors. If n>=0, the cfg_files parameter will contain a newly
+ * allocated array of pointers to struct dirent entries, as returned
+ * by scandir(). These can be freed with tr_free_config_file_list().
+ */
+int tr_find_config_files (const char *config_dir, struct dirent ***cfg_files) {
   int n = 0, i = 0;
   
-  n = scandir(".", cfg_files, &is_cfg_file, 0);
+  n = scandir(config_dir, cfg_files, &is_cfg_file, 0);
 
   if (n < 0) {
     perror("scandir");
-    fprintf(stderr, "tr_find_config: scandir error.\n");
-    return 0;
+    tr_debug("tr_find_config: scandir error trying to scan %s.", config_dir);
+  } else if (n == 0) {
+    tr_debug("tr_find_config: No config files found.");
+  } else {
+    i = n;
+    while(i--) {
+      tr_debug("tr_find_config: Config file found (%s).", (*cfg_files)[i]->d_name);
+    }
   }
 
-  if (n == 0) {
-    fprintf (stderr, "tr_find_config: No config files found.\n");
-    return 0;
-  }
+  return n;
+}
 
-  i = n;
-  while(i--) {
-    fprintf(stderr, "tr_find_config: Config file found (%s).\n", (*cfg_files)[i]->d_name);
+/* Free memory allocated for configuration file list returned from tr_find_config_files().
+ * This can be called regardless of the return value of tr_find_config_values(). */
+void tr_free_config_file_list(int n, struct dirent ***cfg_files) {
+  int ii;
+
+  /* if n < 0, then scandir did not allocate anything because it failed */
+  if((n>=0) && (*cfg_files != NULL)) {
+    for(ii=0; ii<n; ii++) {
+      free((*cfg_files)[ii]);
+    }
+    free(*cfg_files); /* safe even if n==0 */
+    *cfg_files=NULL; /* this will help prevent accidentally freeing twice */
   }
-    
-  return n;
 }