Merge pull request #49 from painless-security/jennifer/mon_msg_encoders
[trust_router.git] / common / tr_config.c
index b0d8b4c..c1fc6d9 100644 (file)
@@ -237,7 +237,7 @@ static TR_CFG_RC tr_cfg_parse_internal(TR_CFG *trc, json_t *jcfg)
     }
     if (NULL != (jhname = json_object_get(jint, "hostname"))) {
       if (json_is_string(jhname)) {
-        trc->internal->hostname = json_string_value(jhname);
+        trc->internal->hostname = talloc_strdup(trc->internal, json_string_value(jhname));
       } else {
         tr_debug("tr_cfg_parse_internal: Parsing error, hostname is not a string.");
         return TR_CFG_NOPARSE;
@@ -1460,60 +1460,6 @@ static TR_NAME *tr_cfg_parse_org_name(TALLOC_CTX *mem_ctx, json_t *j_org, TR_CFG
   return name;
 }
 
-#if 0
-/* TODO: are we using this? JLR */
-/* Update the community information with data from a new batch of IDP realms.
- * May partially add realms if there is a failure, no guarantees.
- * Call like comms=tr_comm_idp_update(comms, new_realms, &rc) */
-static TR_COMM *tr_cfg_comm_idp_update(TALLOC_CTX *mem_ctx,
-                                       TR_COMM_TABLE *ctab,
-                                       TR_IDP_REALM *new_realms,
-                                       TR_CFG_RC *rc)
-{
-  TALLOC_CTX *tmp_ctx=talloc_new(NULL);
-  TR_COMM *comm=NULL; /* community looked up in comms table */
-  TR_COMM *new_comms=NULL; /* new communities as we create them */
-  TR_IDP_REALM *realm=NULL;
-  TR_APC *apc=NULL; /* apc of one realm */
-
-  if (rc==NULL) {
-    *rc=TR_CFG_BAD_PARAMS;
-    goto cleanup;
-  }
-
-  /* start with an empty list communities, then fill that in */
-  for (realm=new_realms; realm!=NULL; realm=realm->next) {
-    for (apc=realm->apcs; apc!=NULL; apc=apc->next) {
-      comm=tr_comm_lookup(comms, apc->id);
-      if (comm==NULL) {
-        comm=tr_comm_new(tmp_ctx);
-        if (comm==NULL) {
-          tr_debug("tr_cfg_comm_idp_update: unable to allocate new community.");
-          *rc=TR_CFG_NOMEM;
-          goto cleanup;
-        }
-        /* fill in the community with info */
-        comm->type=TR_COMM_APC; /* realms added this way are in APCs */
-        comm->expiration_interval=TR_DEFAULT_APC_EXPIRATION_INTERVAL;
-        tr_comm_set_id(comm, tr_dup_name(apc->id));
-        tr_comm_add_idp_realm(comm, realm);
-        tr_comm_add(new_comms, comm);
-      } else {
-        /* add this realm to the comm */
-        tr_comm_add_idp_realm(comm, realm);
-      }
-    }
-  }
-
-  /* we successfully built a list, add it to the other list */
-  tr_comm_add(comms, new_comms);
-  talloc_steal(mem_ctx, comms);
- cleanup:
-  talloc_free(tmp_ctx);
-  return comms;
-}
-#endif
-
 static TR_CFG_RC tr_cfg_parse_one_local_org(TR_CFG *trc, json_t *jlorg)
 {
   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
@@ -1650,7 +1596,7 @@ static TR_CFG_RC tr_cfg_parse_one_peer_org(TR_CFG *trc, json_t *jporg)
     goto cleanup;
   }
 
-  trp_peer_set_server(new_peer, json_string_value(jhost));
+  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
@@ -2121,13 +2067,14 @@ typedef TR_CFG_RC (TR_CFG_PARSE_FN)(TR_CFG *, json_t *);
  * @param parse_fn Function to apply
  * @return TR_CFG_SUCCESS on success, _FAIL or an error code on failure
  */
-static TR_CFG_RC tr_cfg_parse_helper(TR_CFG *cfg, json_t **jcfgs, size_t n_jcfg, TR_CFG_PARSE_FN parse_fn)
+static TR_CFG_RC tr_cfg_parse_helper(TR_CFG *cfg, unsigned int n_jcfg, json_t **jcfgs, TR_CFG_PARSE_FN parse_fn)
 {
   size_t ii=0;
   json_t *this_jcfg=NULL;
   TR_CFG_RC ret=TR_CFG_ERROR;
 
-  /* TODO validate arguments */
+  if ((cfg==NULL) || (jcfgs==NULL) || (parse_fn==NULL))
+    return TR_CFG_ERROR;
 
   for (ii=0; ii<n_jcfg; ii++) {
     this_jcfg=jcfgs[ii];
@@ -2177,11 +2124,11 @@ TR_CFG_RC tr_parse_config(TR_CFG_MGR *cfg_mgr, unsigned int n_files, char **file
   cfg_mgr->new->peers=trp_ptable_new(cfg_mgr); /* not sure why this isn't in cfg_mgr->new's context */
 
   /* now run through the parsers on the JSON */
-  if ((TR_CFG_SUCCESS != (cfg_rc=tr_cfg_parse_helper(cfg_mgr->new, jcfgs, n_files, tr_cfg_parse_internal))) ||
-      (TR_CFG_SUCCESS != (cfg_rc=tr_cfg_parse_helper(cfg_mgr->new, jcfgs, n_files, tr_cfg_parse_local_orgs))) ||
-      (TR_CFG_SUCCESS != (cfg_rc=tr_cfg_parse_helper(cfg_mgr->new, jcfgs, n_files, tr_cfg_parse_peer_orgs))) ||
-      (TR_CFG_SUCCESS != (cfg_rc=tr_cfg_parse_helper(cfg_mgr->new, jcfgs, n_files, tr_cfg_parse_default_servers))) ||
-      (TR_CFG_SUCCESS != (cfg_rc=tr_cfg_parse_helper(cfg_mgr->new, jcfgs, n_files, tr_cfg_parse_comms))))
+  if ((TR_CFG_SUCCESS != (cfg_rc=tr_cfg_parse_helper(cfg_mgr->new, n_files, jcfgs, tr_cfg_parse_internal))) ||
+      (TR_CFG_SUCCESS != (cfg_rc=tr_cfg_parse_helper(cfg_mgr->new, n_files, jcfgs, tr_cfg_parse_local_orgs))) ||
+      (TR_CFG_SUCCESS != (cfg_rc=tr_cfg_parse_helper(cfg_mgr->new, n_files, jcfgs, tr_cfg_parse_peer_orgs))) ||
+      (TR_CFG_SUCCESS != (cfg_rc=tr_cfg_parse_helper(cfg_mgr->new, n_files, jcfgs, tr_cfg_parse_default_servers))) ||
+      (TR_CFG_SUCCESS != (cfg_rc=tr_cfg_parse_helper(cfg_mgr->new, n_files, jcfgs, tr_cfg_parse_comms))))
     goto cleanup; /* cfg_rc was set above */
 
   /* make sure we got a complete, consistent configuration */