[tor-commits] [tor/master] Extract common code for creating the keys directory.

nickm at torproject.org nickm at torproject.org
Wed Dec 6 00:53:09 UTC 2017


commit 7b34ab3e384b5c72e938d11e5e05c72b9d529bb8
Author: Nick Mathewson <nickm at torproject.org>
Date:   Tue Nov 14 16:18:53 2017 -0500

    Extract common code for creating the keys directory.
    
    This had somehow gotten duplicated between router.c and routerkeys.c
---
 src/or/config.c     | 26 ++++++++++++++++++++++++++
 src/or/config.h     |  6 ++++++
 src/or/router.c     | 17 ++---------------
 src/or/routerkeys.c | 19 ++++---------------
 4 files changed, 38 insertions(+), 30 deletions(-)

diff --git a/src/or/config.c b/src/or/config.c
index f024764f8..d149a144d 100644
--- a/src/or/config.c
+++ b/src/or/config.c
@@ -1289,6 +1289,32 @@ check_and_create_data_directory(int create,
   return 0;
 }
 
+/**
+ * Ensure that our keys directory exists, with appropriate permissions.
+ * Return 0 on success, -1 on failure.
+ */
+int
+create_keys_directory(const or_options_t *options)
+{
+  /* Make sure DataDirectory exists, and is private. */
+  cpd_check_t cpd_opts = CPD_CREATE;
+  if (options->DataDirectoryGroupReadable)
+    cpd_opts |= CPD_GROUP_READ;
+  if (check_private_dir(options->DataDirectory, cpd_opts, options->User)) {
+    log_err(LD_OR, "Can't create/check datadirectory %s",
+            options->DataDirectory);
+    return -1;
+  }
+  /* Check the key directory. */
+  char *keydir = options_get_datadir_fname(options, "keys");
+  if (check_private_dir(keydir, CPD_CREATE, options->User)) {
+    tor_free(keydir);
+    return -1;
+  }
+  tor_free(keydir);
+  return 0;
+}
+
 /* Helps determine flags to pass to switch_id. */
 static int have_low_ports = -1;
 
diff --git a/src/or/config.h b/src/or/config.h
index efdd8c59b..de9858a25 100644
--- a/src/or/config.h
+++ b/src/or/config.h
@@ -72,6 +72,10 @@ MOCK_DECL(char *,
  * get_datadir_fname2_suffix.  */
 #define get_datadir_fname2(sub1,sub2) \
   get_datadir_fname2_suffix((sub1), (sub2), NULL)
+/** Return a newly allocated string containing datadir/sub1 relative to
+ * opts.  See get_datadir_fname2_suffix.  */
+#define options_get_datadir_fname(opts,sub1)                    \
+  options_get_datadir_fname2_suffix((opts),(sub1), NULL, NULL)
 /** Return a newly allocated string containing datadir/sub1/sub2 relative to
  * opts.  See get_datadir_fname2_suffix.  */
 #define options_get_datadir_fname2(opts,sub1,sub2)                      \
@@ -83,6 +87,8 @@ MOCK_DECL(char *,
 
 int using_default_dir_authorities(const or_options_t *options);
 
+int create_keys_directory(const or_options_t *options);
+
 int check_or_create_data_subdir(const char *subdir);
 int write_to_data_subdir(const char* subdir, const char* fname,
                          const char* str, const char* descr);
diff --git a/src/or/router.c b/src/or/router.c
index 8ad5d038e..fb8596a8d 100644
--- a/src/or/router.c
+++ b/src/or/router.c
@@ -932,22 +932,9 @@ init_keys(void)
   }
   if (init_keys_common() < 0)
     return -1;
-  /* Make sure DataDirectory exists, and is private. */
-  cpd_check_t cpd_opts = CPD_CREATE;
-  if (options->DataDirectoryGroupReadable)
-    cpd_opts |= CPD_GROUP_READ;
-  if (check_private_dir(options->DataDirectory, cpd_opts, options->User)) {
-    log_err(LD_OR, "Can't create/check datadirectory %s",
-            options->DataDirectory);
-    return -1;
-  }
-  /* Check the key directory. */
-  keydir = get_datadir_fname("keys");
-  if (check_private_dir(keydir, CPD_CREATE, options->User)) {
-    tor_free(keydir);
+
+  if (create_keys_directory(options) < 0)
     return -1;
-  }
-  tor_free(keydir);
 
   /* 1a. Read v3 directory authority key/cert information. */
   memset(v3_digest, 0, sizeof(v3_digest));
diff --git a/src/or/routerkeys.c b/src/or/routerkeys.c
index 7295c1965..c6c689ecd 100644
--- a/src/or/routerkeys.c
+++ b/src/or/routerkeys.c
@@ -813,21 +813,10 @@ load_ed_keys(const or_options_t *options, time_t now)
       flags |= INIT_ED_KEY_TRY_ENCRYPTED;
 
     /* Check/Create the key directory */
-    cpd_check_t cpd_opts = CPD_CREATE;
-    if (options->DataDirectoryGroupReadable)
-      cpd_opts |= CPD_GROUP_READ;
-    if (check_private_dir(options->DataDirectory, cpd_opts, options->User)) {
-      log_err(LD_OR, "Can't create/check datadirectory %s",
-              options->DataDirectory);
-      goto err;
-    }
-    char *fname = get_datadir_fname("keys");
-    if (check_private_dir(fname, CPD_CREATE, options->User) < 0) {
-      log_err(LD_OR, "Problem creating/checking key directory %s", fname);
-      tor_free(fname);
-      goto err;
-    }
-    tor_free(fname);
+    if (create_keys_directory(options) < 0)
+      return -1;
+
+    char *fname;
     if (options->master_key_fname) {
       fname = tor_strdup(options->master_key_fname);
       flags |= INIT_ED_KEY_EXPLICIT_FNAME;





More information about the tor-commits mailing list