Add TRP handling events, plus change to cfg layout.
[trust_router.git] / common / tr_config.c
index 1da6dc4..0625790 100644 (file)
@@ -49,22 +49,38 @@ void tr_print_config (FILE *stream, TR_CFG *cfg) {
   return;
 }
 
+TR_CFG *tr_cfg_new(TALLOC_CTX *mem_ctx)
+{
+  return talloc_zero(mem_ctx, TR_CFG);
+}
+
 void tr_cfg_free (TR_CFG *cfg) {
   talloc_free(cfg);
-  return;
 }
 
-TR_CFG_RC tr_apply_new_config (TR_INSTANCE *tr) {
-  if (!tr)
+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 (tr->active_cfg)
-    tr_cfg_free(tr->active_cfg);
+  if (cfg_mgr->active != NULL)
+    tr_cfg_free(cfg_mgr->active);
 
-  tr->active_cfg = tr->new_cfg;
+  cfg_mgr->active = cfg_mgr->new;
+  cfg_mgr->new=NULL; /* only keep a single handle on the new configuration */
 
-  tr_log_threshold(tr->active_cfg->internal->log_threshold);
-  tr_console_threshold(tr->active_cfg->internal->console_threshold);
+  tr_log_threshold(cfg_mgr->active->internal->log_threshold);
+  tr_console_threshold(cfg_mgr->active->internal->console_threshold);
 
   return TR_CFG_SUCCESS;
 }
@@ -72,20 +88,21 @@ TR_CFG_RC tr_apply_new_config (TR_INSTANCE *tr) {
 static TR_CFG_RC tr_cfg_parse_internal (TR_CFG *trc, json_t *jcfg) {
   json_t *jint = NULL;
   json_t *jmtd = NULL;
-  json_t *jtp = NULL;
+  json_t *jtidsp = NULL;
+  json_t *jtrpsp = NULL;
   json_t *jhname = NULL;
   json_t *jlog = NULL;
   json_t *jconthres = NULL;
   json_t *jlogthres = NULL;
+  json_t *jcfgpoll = NULL;
+  json_t *jcfgsettle = NULL;
 
   if ((!trc) || (!jcfg))
     return TR_CFG_BAD_PARAMS;
 
   if (NULL == trc->internal) {
-    if (NULL == (trc->internal = talloc(trc, TR_CFG_INTERNAL)))
+    if (NULL == (trc->internal = talloc_zero(trc, TR_CFG_INTERNAL)))
       return TR_CFG_NOMEM;
-
-    memset(trc->internal, 0, sizeof(TR_CFG_INTERNAL));
   }
 
   if (NULL != (jint = json_object_get(jcfg, "tr_internal"))) {
@@ -100,17 +117,28 @@ static TR_CFG_RC tr_cfg_parse_internal (TR_CFG *trc, json_t *jcfg) {
       /* If not configured, use the default */
       trc->internal->max_tree_depth = TR_DEFAULT_MAX_TREE_DEPTH;
     }
-    if (NULL != (jtp = json_object_get(jint, "tids_port"))) {
-      if (json_is_number(jtp)) {
-       trc->internal->tids_port = json_integer_value(jtp);
+    if (NULL != (jtidsp = json_object_get(jint, "tids_port"))) {
+      if (json_is_number(jtidsp)) {
+       trc->internal->tids_port = json_integer_value(jtidsp);
       } else {
-       tr_debug("tr_cfg_parse_internal: Parsing error, port is not a number.");
+       tr_debug("tr_cfg_parse_internal: Parsing error, tids_port is not a number.");
        return TR_CFG_NOPARSE;
       }
     } else {
       /* If not configured, use the default */
       trc->internal->tids_port = TR_DEFAULT_TIDS_PORT;
     }
+    if (NULL != (jtrpsp = json_object_get(jint, "trps_port"))) {
+      if (json_is_number(jtrpsp)) {
+       trc->internal->trps_port = json_integer_value(jtrpsp);
+      } else {
+       tr_debug("tr_cfg_parse_internal: Parsing error, trps_port is not a number.");
+       return TR_CFG_NOPARSE;
+      }
+    } else {
+      /* If not configured, use the default */
+      trc->internal->trps_port = TR_DEFAULT_TRPS_PORT;
+    }
     if (NULL != (jhname = json_object_get(jint, "hostname"))) {
       if (json_is_string(jhname)) {
        trc->internal->hostname = json_string_value(jhname);
@@ -119,6 +147,22 @@ static TR_CFG_RC tr_cfg_parse_internal (TR_CFG *trc, json_t *jcfg) {
        return TR_CFG_NOPARSE;
       }
     }
+    if (NULL != (jcfgpoll = json_object_get(jint, "cfg_poll_interval"))) {
+      if (json_is_number(jcfgpoll)) {
+       trc->internal->cfg_poll_interval = json_integer_value(jcfgpoll);
+      } else {
+       tr_debug("tr_cfg_parse_internal: Parsing error, cfg_poll_interval is not a number.");
+       return TR_CFG_NOPARSE;
+      }
+    }
+    if (NULL != (jcfgsettle = json_object_get(jint, "cfg_settle_count"))) {
+      if (json_is_number(jcfgsettle)) {
+       trc->internal->cfg_settle_count = json_integer_value(jcfgsettle);
+      } else {
+       tr_debug("tr_cfg_parse_internal: Parsing error, cfg_settle_count is not a number.");
+       return TR_CFG_NOPARSE;
+      }
+    }
 
     if (NULL != (jlog = json_object_get(jint, "logging"))) {
       if (NULL != (jlogthres = json_object_get(jlog, "log_threshold"))) {
@@ -171,14 +215,12 @@ static TR_CONSTRAINT *tr_cfg_parse_one_constraint (TR_CFG *trc, char *ctype, jso
     return NULL;
   }
 
-  if (NULL == (cons = talloc(trc, TR_CONSTRAINT))) {
+  if (NULL == (cons = talloc_zero(trc, TR_CONSTRAINT))) {
     tr_debug("tr_cfg_parse_one_constraint: Out of memory (cons).");
     *rc = TR_CFG_NOMEM;
     return NULL;
   }
 
-  memset(cons, 0, sizeof(TR_CONSTRAINT));
-
   if (NULL == (cons->type = tr_new_name(ctype))) {
     tr_debug("tr_cfg_parse_one_constraint: Out of memory (type).");
     *rc = TR_CFG_NOMEM;
@@ -225,14 +267,12 @@ static TR_FILTER *tr_cfg_parse_one_filter (TR_CFG *trc, json_t *jfilt, TR_CFG_RC
     return NULL;
   }
 
-  if (NULL == (filt = talloc(trc, TR_FILTER))) {
+  if (NULL == (filt = talloc_zero(trc, TR_FILTER))) {
     tr_debug("tr_cfg_parse_one_filter: Out of memory.");
     *rc = TR_CFG_NOMEM;
     return NULL;
   }
 
-  memset(filt, 0, sizeof(TR_FILTER));
-
   if (!strcmp(json_string_value(jftype), "rp_permitted")) {
     filt->type = TR_FILTER_TYPE_RP_PERMITTED;
   }
@@ -270,15 +310,13 @@ static TR_FILTER *tr_cfg_parse_one_filter (TR_CFG *trc, json_t *jfilt, TR_CFG_RC
       return NULL;
     }
 
-    if (NULL == (filt->lines[i] = talloc(trc, TR_FLINE))) {
+    if (NULL == (filt->lines[i] = talloc_zero(trc, TR_FLINE))) {
       tr_debug("tr_cfg_parse_one_filter: Out of memory (fline).");
       *rc = TR_CFG_NOMEM;
       tr_filter_free(filt);
       return NULL;
     }
 
-    memset(filt->lines[i], 0, sizeof(TR_FLINE));
-
     if (!strcmp(json_string_value(jfaction), "accept")) {
        filt->lines[i]->action = TR_FILTER_ACTION_ACCEPT;
     }
@@ -329,15 +367,13 @@ static TR_FILTER *tr_cfg_parse_one_filter (TR_CFG *trc, json_t *jfilt, TR_CFG_RC
        return NULL;
       }
 
-      if (NULL == (filt->lines[i]->specs[j] = talloc(trc, TR_FSPEC))) {
+      if (NULL == (filt->lines[i]->specs[j] = talloc_zero(trc, TR_FSPEC))) {
        tr_debug("tr_cfg_parse_one_filter: Out of memory.");
        *rc = TR_CFG_NOMEM;
        tr_filter_free(filt);
        return NULL;
       }
 
-      memset(filt->lines[i]->specs[j], 0, sizeof(TR_FSPEC));
-    
       if ((NULL == (filt->lines[i]->specs[j]->field = tr_new_name((char *)json_string_value(jffield)))) ||
          (NULL == (filt->lines[i]->specs[j]->match = tr_new_name((char *)json_string_value(jfmatch))))) {
        tr_debug("tr_cfg_parse_one_filter: Out of memory.");
@@ -395,13 +431,11 @@ static TR_RP_CLIENT *tr_cfg_parse_one_rp_client (TR_CFG *trc, json_t *jrp, TR_CF
     return NULL;
   }
 
-  if (NULL == (rp = talloc(trc, TR_RP_CLIENT))) {
+  if (NULL == (rp = talloc_zero(trc, TR_RP_CLIENT))) {
     tr_debug("tr_cfg_parse_one_rp_realm: Out of memory.");
     *rc = TR_CFG_NOMEM;
     return NULL;
   }
-  
-  memset(rp, 0, sizeof(TR_RP_CLIENT));
 
   /* TBD -- support more than one filter entry per RP Client? */
   if (NULL == (rp->filter = tr_cfg_parse_one_filter(trc, jfilt, rc))) {
@@ -447,6 +481,7 @@ static TR_CFG_RC tr_cfg_parse_rp_clients (TR_CFG *trc, json_t *jcfg) {
       trc->rp_clients = rp;
     }
   }
+  tr_debug("tr_cfg_parse_rp_clients: Finished (rc=%d)", rc);
   return rc;
 }
 
@@ -459,14 +494,12 @@ static TR_AAA_SERVER *tr_cfg_parse_one_aaa_server (TR_CFG *trc, json_t *jaddr, T
     return NULL;
   }
 
-  if (NULL == (aaa = talloc(trc, TR_AAA_SERVER))) {
+  if (NULL == (aaa = talloc_zero(trc, TR_AAA_SERVER))) {
     tr_debug("tr_cfg_parse_one_aaa_server: Out of memory.");
     *rc = TR_CFG_NOMEM;
     return NULL;
   }
 
-  memset(aaa, 0, sizeof(TR_AAA_SERVER));
-
   aaa->hostname = tr_new_name((char *)(json_string_value(jaddr)));
 
   return aaa;
@@ -487,6 +520,7 @@ static TR_AAA_SERVER *tr_cfg_parse_aaa_servers (TR_CFG *trc, json_t *jaaas, TR_C
     temp_aaa->next = aaa;
     aaa = temp_aaa;
   }
+  tr_debug("tr_cfg_parse_aaa_servers: Finished (rc=%d)", *rc);
   return aaa;
 }
 
@@ -503,14 +537,12 @@ static TR_APC *tr_cfg_parse_apcs (TR_CFG *trc, json_t *japcs, TR_CFG_RC *rc)
     return NULL;
   }
 
-  if (NULL == (apc = talloc(trc, TR_APC))) {
+  if (NULL == (apc = talloc_zero(trc, TR_APC))) {
     tr_debug("tr_cfg_parse_apcs: Out of memory.");
     *rc = TR_CFG_NOMEM;
     return NULL;
   }
 
-  memset(apc, 0, sizeof(TR_APC));
-
   /* TBD, deal with more than one APC.  In the meantime, though...                */
   /* Only parse the first APC, because we only know how to deal with one, anyway. */
   if (0 == json_array_size(japcs))
@@ -522,6 +554,7 @@ static TR_APC *tr_cfg_parse_apcs (TR_CFG *trc, json_t *japcs, TR_CFG_RC *rc)
     return NULL;
   }
 
+  tr_debug("tr_cfg_parse_apcs: Finished (rc=%d)", *rc);
   return apc;
 }
 
@@ -539,14 +572,12 @@ static TR_IDP_REALM *tr_cfg_parse_one_idp_realm (TR_CFG *trc, json_t *jidp, TR_C
     return NULL;
   }
 
-  if (NULL == (idp = talloc(trc, TR_IDP_REALM))) {
+  if (NULL == (idp = talloc_zero(trc, TR_IDP_REALM))) {
     tr_debug("tr_cfg_parse_one_idp_realm: Out of memory.");
     *rc = TR_CFG_NOMEM;
     return NULL;
   }
 
-  memset(idp, 0, sizeof(TR_IDP_REALM));
-
   if ((NULL == (jrid = json_object_get(jidp, "realm_id"))) ||
       (!json_is_string(jrid)) ||
       (NULL == (jscfg = json_object_get(jidp, "shared_config"))) ||
@@ -615,6 +646,7 @@ static TR_CFG_RC tr_cfg_parse_default_servers (TR_CFG *trc, json_t *jcfg)
     }
   } 
 
+  tr_debug("tr_cfg_parse_default_servers: Finished (rc=%d)", rc);
   return rc;
 }
 
@@ -643,6 +675,7 @@ static TR_CFG_RC tr_cfg_parse_idp_realms (TR_CFG *trc, json_t *jcfg)
     }
   }
 
+  tr_debug("tr_cfg_parse_idp_realms: Finished (rc=%d)", rc);
   return rc;
 }
 
@@ -662,8 +695,8 @@ 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, 
-                                            tr_new_name((char *)json_string_value(json_array_get(jidps, i))), 
-                                            rc)))) {
+                                             tr_new_name((char *)json_string_value(json_array_get(jidps, i))), 
+                                             rc)))) {
       tr_debug("tr_cfg_parse_comm_idps: Unknown IDP %s.", 
              (char *)json_string_value(json_array_get(jidps, i)));
       return NULL;
@@ -691,13 +724,12 @@ 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))) {
+    if (NULL == (temp_rp = talloc_zero(trc, TR_RP_REALM))) {
       tr_debug("tr_cfg_parse_comm_rps: Can't allocate memory for RP Realm.");
       if (rc)
        *rc = TR_CFG_NOMEM;
       return NULL;
     }
-    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))))) {
       tr_debug("tr_cfg_parse_comm_rps: No memory for RP Realm Name.");
@@ -833,6 +865,7 @@ static TR_CFG_RC tr_cfg_parse_comms (TR_CFG *trc, json_t *jcfg)
       trc->comms = comm;
     }
   }
+  tr_debug("tr_cfg_parse_comms: Finished (rc=%d)", rc);
   return rc;
 }
 
@@ -868,62 +901,74 @@ TR_CFG_RC tr_cfg_validate (TR_CFG *trc) {
 
 /* 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 */
+static char *join_paths(TALLOC_CTX *mem_ctx, const char *p1, const char *p2) {
+  return talloc_asprintf(mem_ctx, "%s/%s", p1, p2); /* returns NULL on a failure */
 }
 
-/* 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) {
+/* Reads configuration files in config_dir ("" or "./" will use the current directory). */
+TR_CFG_RC tr_parse_config (TR_CFG_MGR *cfg_mgr, const char *config_dir, int n, struct dirent **cfg_files)
+{
+  TALLOC_CTX *tmp_ctx=talloc_new(NULL);
   json_t *jcfg;
   json_error_t rc;
   char *file_with_path;
+  int ii;
+  TR_CFG_RC cfg_rc=TR_CFG_ERROR;
 
-  if ((!tr) || (!cfg_files) || (n<=0))
-    return TR_CFG_BAD_PARAMS;
-
-  /* If there is a partial/abandoned config lying around, free it */
-  if (tr->new_cfg) 
-    tr_cfg_free(tr->new_cfg);
-  
-  if (NULL == (tr->new_cfg = talloc(NULL, TR_CFG)))
-    return TR_CFG_NOMEM;
+  if ((!cfg_mgr) || (!cfg_files) || (n<=0)) {
+    cfg_rc=TR_CFG_BAD_PARAMS;
+    goto cleanup;
+  }
 
-  memset(tr->new_cfg, 0, sizeof(TR_CFG));
+  if (cfg_mgr->new != NULL)
+    tr_cfg_free(cfg_mgr->new);
+  cfg_mgr->new=tr_cfg_new(tmp_ctx); /* belongs to the temporary context for now */
+  if (cfg_mgr->new == NULL) {
+    cfg_rc=TR_CFG_NOMEM;
+    goto cleanup;
+  }
 
   /* Parse configuration information from each config file */
-  while (n--) {
-    file_with_path=join_paths(config_dir, cfg_files[n]->d_name); /* must free result with talloc_free */
+  for (ii=0; ii<n; ii++) {
+    file_with_path=join_paths(tmp_ctx, config_dir, cfg_files[ii]->d_name); /* must free result with talloc_free */
     if(file_with_path == NULL) {
       tr_crit("tr_parse_config: error joining path.");
-      return TR_CFG_NOMEM;
+      cfg_rc=TR_CFG_NOMEM;
+      goto cleanup;
     }
-    tr_debug("tr_parse_config: Parsing %s.", cfg_files[n]->d_name); /* print the filename without the path */
+    tr_debug("tr_parse_config: Parsing %s.", cfg_files[ii]->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;
+               cfg_files[ii]->d_name);
+      cfg_rc=TR_CFG_NOPARSE;
+      goto cleanup;
     }
-    talloc_free(file_with_path); /* done with filename */
-
-    if ((TR_CFG_SUCCESS != tr_cfg_parse_internal(tr->new_cfg, jcfg)) ||
-        (TR_CFG_SUCCESS != tr_cfg_parse_rp_clients(tr->new_cfg, jcfg)) ||
-        (TR_CFG_SUCCESS != tr_cfg_parse_idp_realms(tr->new_cfg, jcfg)) ||
-        (TR_CFG_SUCCESS != tr_cfg_parse_default_servers(tr->new_cfg, jcfg)) ||
-        (TR_CFG_SUCCESS != tr_cfg_parse_comms(tr->new_cfg, jcfg))) {
-      tr_cfg_free(tr->new_cfg);
-      return TR_CFG_ERROR;
+
+    if ((TR_CFG_SUCCESS != tr_cfg_parse_internal(cfg_mgr->new, jcfg)) ||
+        (TR_CFG_SUCCESS != tr_cfg_parse_rp_clients(cfg_mgr->new, jcfg)) ||
+        (TR_CFG_SUCCESS != tr_cfg_parse_idp_realms(cfg_mgr->new, jcfg)) ||
+        (TR_CFG_SUCCESS != tr_cfg_parse_default_servers(cfg_mgr->new, jcfg)) ||
+        (TR_CFG_SUCCESS != tr_cfg_parse_comms(cfg_mgr->new, jcfg))) {
+      cfg_rc=TR_CFG_ERROR;
+      goto cleanup;
     }
   }
 
   /* make sure we got a complete, consistent configuration */
-  if (TR_CFG_SUCCESS != tr_cfg_validate(tr->new_cfg)) {
-    tr_debug("tr_parse_config: Error: INVALID CONFIGURATION, EXITING");
-    return TR_CFG_ERROR;
+  if (TR_CFG_SUCCESS != tr_cfg_validate(cfg_mgr->new)) {
+    tr_err("tr_parse_config: Error: INVALID CONFIGURATION");
+    cfg_rc=TR_CFG_ERROR;
+    goto cleanup;
   }
 
-  return TR_CFG_SUCCESS;
+  /* success! */
+  talloc_steal(cfg_mgr, cfg_mgr->new); /* hand this over to the cfg_mgr context */
+  cfg_rc=TR_CFG_SUCCESS;
+
+cleanup:
+  talloc_free(tmp_ctx);
+  return cfg_rc;
 }
 
 TR_IDP_REALM *tr_cfg_find_idp (TR_CFG *tr_cfg, TR_NAME *idp_id, TR_CFG_RC *rc)
@@ -1004,21 +1049,14 @@ static int is_cfg_file(const struct dirent *dent) {
  * 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;
+  int n = 0;
   
-  n = scandir(config_dir, cfg_files, &is_cfg_file, 0);
+  n = scandir(config_dir, cfg_files, is_cfg_file, alphasort);
 
   if (n < 0) {
     perror("scandir");
     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);
-    }
-  }
+  } 
 
   return n;
 }
@@ -1029,10 +1067,11 @@ 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) {
+  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 */
   }
 }