Minor patches from Adam Bishop to fix build issues from commit 1bc4bf5.
[trust_router.git] / common / tr_config.c
index 51999c4..433f726 100644 (file)
 #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.");
-  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) {
@@ -120,7 +150,7 @@ static TR_CFG_RC tr_cfg_parse_internal (TR_CFG *trc, json_t *jcfg) {
       }
     }
 
-    if (NULL != (jlog = json_object_get(jcfg, "logging"))) {
+    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));
@@ -649,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;
 
@@ -661,7 +692,15 @@ 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)))) {
       tr_debug("tr_cfg_parse_comm_idps: Unknown IDP %s.", 
@@ -669,6 +708,9 @@ static TR_IDP_REALM *tr_cfg_parse_comm_idps (TR_CFG *trc, json_t *jidps, TR_CFG_
       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;
   }
@@ -866,11 +908,20 @@ TR_CFG_RC tr_cfg_validate (TR_CFG *trc) {
   return rc;
 }
 
-TR_CFG_RC tr_parse_config (TR_INSTANCE *tr, int n, 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;
+  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 */
@@ -884,19 +935,35 @@ TR_CFG_RC tr_parse_config (TR_INSTANCE *tr, int n, struct dirent **cfg_files) {
 
   /* Parse configuration information from each config file */
   while (n--) {
-    tr_debug("tr_read_config: Parsing %s.", cfg_files[n]->d_name);
-    if (NULL == (jcfg = json_load_file(cfg_files[n]->d_name, 
-                                      JSON_DISABLE_EOF_CHECK, &rc))) {
-      tr_debug("tr_read_config: Error parsing config file %s.", 
-              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 %" JSON_INTEGER_FORMAT " of '%s'.",
+                  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;
     }
@@ -958,9 +1025,23 @@ TR_RP_CLIENT *tr_cfg_find_rp (TR_CFG *tr_cfg, TR_NAME *rp_gss, TR_CFG_RC *rc)
 static int is_cfg_file(const struct dirent *dent) {
   int n;
 
-  /* 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"))) {
+  /* Only accept filenames ending in ".cfg" and starting with a character
+   * other than an ASCII '.' */
+
+  /* filename must be at least 4 characters long to be acceptable */
+  n=strlen(dent->d_name);
+  if (n < 4) {
+    return 0;
+  }
+
+  /* filename must not start with '.' */
+  if ('.' == dent->d_name[0]) {
+    return 0;
+  }
+
+  /* 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;
   }
 
@@ -968,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");
-    tr_debug("tr_find_config: scandir error.");
-    return 0;
-  }
-
-  if (n == 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.");
-    return 0;
+  } else {
+    i = n;
+    while(i--) {
+      tr_debug("tr_find_config: Config file found (%s).", (*cfg_files)[i]->d_name);
+    }
   }
 
-  i = n;
-  while(i--) {
-    tr_debug("tr_find_config: Config file found (%s).", (*cfg_files)[i]->d_name);
-  }
-    
   return n;
 }
+
+/* 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 */
+  }
+}