Extract common code for creating the keys directory.

This had somehow gotten duplicated between router.c and routerkeys.c
This commit is contained in:
Nick Mathewson 2017-11-14 16:18:53 -05:00
parent 2e872f6b32
commit 7b34ab3e38
4 changed files with 38 additions and 30 deletions

View file

@ -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;

View file

@ -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);

View file

@ -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);
if (create_keys_directory(options) < 0)
return -1;
}
/* Check the key directory. */
keydir = get_datadir_fname("keys");
if (check_private_dir(keydir, CPD_CREATE, options->User)) {
tor_free(keydir);
return -1;
}
tor_free(keydir);
/* 1a. Read v3 directory authority key/cert information. */
memset(v3_digest, 0, sizeof(v3_digest));

View file

@ -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;