Commit 192be006 authored by Nick Mathewson's avatar Nick Mathewson 👁
Browse files

Make the DataDirectory option immutable.

By convention, the torrc options that the user sets are
unchangeable.  If we need to change them, we should be using a copy
that's stored in another field

To avoid trouble, I'm keeping DataDirectory as the name for the
field that the rest of Tor uses, and using DataDirectory_option for
the confparse-controlled field.

This commit also modernizes some older string handling code in the
DataDirectory normalization function.
parent bf8a7201
......@@ -286,7 +286,7 @@ static config_var_t option_vars_[] = {
V(CookieAuthFileGroupReadable, BOOL, "0"),
V(CookieAuthFile, STRING, NULL),
V(CountPrivateBandwidth, BOOL, "0"),
V(DataDirectory, FILENAME, NULL),
VAR("DataDirectory", FILENAME, DataDirectory_option, NULL),
V(DataDirectoryGroupReadable, BOOL, "0"),
V(DisableOOSCheck, BOOL, "1"),
V(DisableNetwork, BOOL, "0"),
......@@ -941,6 +941,7 @@ or_options_free(or_options_t *options)
SMARTLIST_FOREACH(options->FilesOpenedByIncludes, char *, f, tor_free(f));
smartlist_free(options->FilesOpenedByIncludes);
}
tor_free(options->DataDirectory);
tor_free(options->BridgePassword_AuthDigest_);
tor_free(options->command_arg);
tor_free(options->master_key_fname);
......@@ -7682,31 +7683,29 @@ port_exists_by_type_addr32h_port(int listener_type, uint32_t addr_ipv4h,
check_wildcard);
}
/** Adjust the value of options->DataDirectory, or fill it in if it's
* absent. Return 0 on success, -1 on failure. */
static int
normalize_data_directory(or_options_t *options)
/** Allocate and return a good value for the DataDirectory based on
* <b>val</b>, which may be NULL. Return NULL on failure. */
static char *
get_data_directory(const char *val)
{
#ifdef _WIN32
char *p;
if (options->DataDirectory)
return 0; /* all set */
p = tor_malloc(MAX_PATH);
strlcpy(p,get_windows_conf_root(),MAX_PATH);
options->DataDirectory = p;
return 0;
if (val) {
return tor_strdup(val);
} else {
return tor_strdup(get_windows_conf_root());
}
#else /* !(defined(_WIN32)) */
const char *d = options->DataDirectory;
const char *d = val;
if (!d)
d = "~/.tor";
if (strncmp(d,"~/",2) == 0) {
if (!strcmpstart(d, "~/")) {
char *fn = expand_filename(d);
if (!fn) {
log_warn(LD_CONFIG,"Failed to expand filename \"%s\".", d);
return -1;
return NULL;
}
if (!options->DataDirectory && !strcmp(fn,"/.tor")) {
if (!val && !strcmp(fn,"/.tor")) {
/* If our homedir is /, we probably don't want to use it. */
/* Default to LOCALSTATEDIR/tor which is probably closer to what we
* want. */
......@@ -7717,10 +7716,9 @@ normalize_data_directory(or_options_t *options)
tor_free(fn);
fn = tor_strdup(LOCALSTATEDIR PATH_SEPARATOR "tor");
}
tor_free(options->DataDirectory);
options->DataDirectory = fn;
return fn;
}
return 0;
return tor_strdup(d);
#endif /* defined(_WIN32) */
}
......@@ -7729,9 +7727,10 @@ normalize_data_directory(or_options_t *options)
static int
validate_data_directory(or_options_t *options)
{
if (normalize_data_directory(options) < 0)
tor_free(options->DataDirectory);
options->DataDirectory = get_data_directory(options->DataDirectory_option);
if (!options->DataDirectory)
return -1;
tor_assert(options->DataDirectory);
if (strlen(options->DataDirectory) > (512-128)) {
log_warn(LD_CONFIG, "DataDirectory is too long.");
return -1;
......
......@@ -3641,7 +3641,9 @@ typedef struct {
char *SyslogIdentityTag; /**< Identity tag to add for syslog logging. */
char *DebugLogFile; /**< Where to send verbose log messages. */
char *DataDirectory; /**< OR only: where to store long-term data. */
char *DataDirectory_option; /**< Where to store long-term data, as
* configured by the user. */
char *DataDirectory; /**< Where to store long-term data, as modified. */
int DataDirectoryGroupReadable; /**< Boolean: Is the DataDirectory g+r? */
char *Nickname; /**< OR only: nickname of this onion router. */
char *Address; /**< OR only: configured address for this onion router. */
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment