aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2017-11-14 16:18:53 -0500
committerNick Mathewson <nickm@torproject.org>2017-12-05 19:49:28 -0500
commit7b34ab3e384b5c72e938d11e5e05c72b9d529bb8 (patch)
tree58bfece83dc694baf4e75a9127a1c7371e589824
parent2e872f6b32edaace096180c249934c620cd3ce35 (diff)
downloadtor-7b34ab3e384b5c72e938d11e5e05c72b9d529bb8.tar.gz
tor-7b34ab3e384b5c72e938d11e5e05c72b9d529bb8.zip
Extract common code for creating the keys directory.
This had somehow gotten duplicated between router.c and routerkeys.c
-rw-r--r--src/or/config.c26
-rw-r--r--src/or/config.h6
-rw-r--r--src/or/router.c17
-rw-r--r--src/or/routerkeys.c19
4 files changed, 38 insertions, 30 deletions
diff --git a/src/or/config.c b/src/or/config.c
index f024764f8a..d149a144d2 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 efdd8c59b0..de9858a25b 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 8ad5d038ef..fb8596a8d9 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 7295c19653..c6c689ecd5 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;