Also validate/normalize the DataDirectory better.
svn:r2732
/*XXX in options_validate, we should check if this is going to fail */
/* Ensure data directory is private; create if possible. */
- if (get_data_directory() &&
- check_private_dir(get_data_directory(), 1) != 0) {
+ if (check_private_dir(options->DataDirectory, 1) != 0) {
log_fn(LOG_ERR, "Couldn't access/create private data directory %s",
- get_data_directory());
+ options->DataDirectory);
return -1;
}
/* Start backgrounding the process, if requested. */
if (options->RunAsDaemon) {
- start_daemon(get_data_directory());
+ start_daemon(options->DataDirectory);
}
/* Finish backgrounding the process */
return r;
}
-/** Return the place where we are currently configured to store and read all
- * of our persistant data. */
-const char *
-get_data_directory(void)
-{
- return get_options()->DataDirectory;
-}
-
static int
-validate_data_directory(or_options_t *options) {
- const char *d = options->DataDirectory;
-
- if (!options->DataDirectory) {
+normalize_data_directory(or_options_t *options) {
#ifdef MS_WINDOWS
- char *p;
- p = tor_malloc(MAX_PATH);
- strlcpy(p,get_windows_conf_root(),MAX_PATH);
- options->DataDirectory = p;
- return p;
+ 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;
#else
+ const char *d = options->DataDirectory;
+ if(!d)
d = "~/.tor";
- }
-#endif
- if (d && strncmp(d,"~/",2) == 0) {
+ if (strncmp(d,"~/",2) == 0) {
char *fn = expand_filename(d);
if (!fn) {
- log_fn(LOG_ERR,"Failed to expand filename '%s'. Exiting.", d);
- exit(1);
+ log_fn(LOG_ERR,"Failed to expand filename '%s'.", d);
+ return -1;
}
tor_free(options->DataDirectory);
options->DataDirectory = fn;
}
-
return 0;
+#endif
+}
+
+static int
+validate_data_directory(or_options_t *options) {
+ if(normalize_data_directory(options) < 0)
+ return -1;
+ tor_assert(options->DataDirectory);
+ if (strlen(options->DataDirectory) > (512-128)) {
+ log_fn(LOG_ERR, "DataDirectory is too long.");
+ return -1;
+ }
+ return 0;
}
+
/*
Local Variables:
mode:c
/* XXXX009 NM add config option to disable this. */
tor_snprintf(fname, sizeof(fname), "%s/control_auth_cookie",
- get_data_directory());
+ get_options()->DataDirectory);
crypto_rand(authentication_cookie, AUTHENTICATION_COOKIE_LEN);
authentication_cookie_is_set = 1;
if (write_bytes_to_file(fname, authentication_cookie,
log_fn(LOG_WARN,"Error compressing cached directory");
}
cached_directory_published = when;
- if(get_data_directory()) {
- tor_snprintf(filename,sizeof(filename),"%s/cached-directory", get_data_directory());
+ if(get_options()->DataDirectory) {
+ tor_snprintf(filename,sizeof(filename),"%s/cached-directory", get_options()->DataDirectory);
if(write_str_to_file(filename,cached_directory,0) < 0) {
log_fn(LOG_WARN, "Couldn't write cached directory to disk. Ignoring.");
}
(unsigned long)n_seconds_active_in_interval,
(unsigned long)expected_bandwidth_usage);
tor_snprintf(fname, sizeof(fname), "%s/bw_accounting",
- get_data_directory());
+ get_options()->DataDirectory);
return write_str_to_file(fname, buf, 0);
}
int ok;
tor_snprintf(fname, sizeof(fname), "%s/bw_accounting",
- get_data_directory());
+ get_options()->DataDirectory);
if (!(s = read_file_to_str(fname, 0))) {
return 0;
}
* retry all connections, re-upload all descriptors, and so on. */
static int do_hup(void) {
char keydir[512];
+ or_options_t *options = get_options();
log_fn(LOG_NOTICE,"Received sighup. Reloading config.");
has_completed_circuit=0;
log_fn(LOG_ERR,"Reading config failed--see warnings above. For usage, try -h.");
return -1;
}
+ options = get_options();
/*XXX this should move to options_act, but only once it's been
* removed from init_keys() */
- if(authdir_mode(get_options())) {
+ if(authdir_mode(options)) {
/* reload the approved-routers file */
- tor_snprintf(keydir,sizeof(keydir),"%s/approved-routers", get_data_directory());
+ tor_snprintf(keydir,sizeof(keydir),"%s/approved-routers", options->DataDirectory);
log_fn(LOG_INFO,"Reloading approved fingerprints from %s...",keydir);
if(dirserv_parse_fingerprint_file(keydir) < 0) {
log_fn(LOG_WARN, "Error reloading fingerprints. Continuing with old list.");
}
/* Fetch a new directory. Even authdirservers do this. */
directory_get_from_dirserver(DIR_PURPOSE_FETCH_DIR, NULL, 0);
- if(server_mode(get_options())) {
+ if(server_mode(options)) {
/* Restart cpuworker and dnsworker processes, so they get up-to-date
* configuration options. */
cpuworkers_rotate();
dnsworkers_rotate();
/* Rebuild fresh descriptor as needed. */
router_rebuild_descriptor();
- tor_snprintf(keydir,sizeof(keydir),"%s/router.desc", get_data_directory());
+ tor_snprintf(keydir,sizeof(keydir),"%s/router.desc", options->DataDirectory);
log_fn(LOG_INFO,"Dumping descriptor to %s...",keydir);
if (write_str_to_file(keydir, router_get_my_descriptor(), 0)) {
return -1;
void config_parse_exit_policy(struct config_line_t *cfg,
struct exit_policy_t **dest);
void exit_policy_free(struct exit_policy_t *p);
-const char *get_data_directory(void);
int config_option_is_recognized(const char *key);
struct config_line_t *config_get_assigned_option(or_options_t *options,
const char *key);
char fname_prev[512];
crypto_pk_env_t *prkey;
tor_snprintf(fname,sizeof(fname),
- "%s/keys/secret_onion_key",get_data_directory());
+ "%s/keys/secret_onion_key",get_options()->DataDirectory);
tor_snprintf(fname_prev,sizeof(fname_prev),
- "%s/keys/secret_onion_key.old",get_data_directory());
+ "%s/keys/secret_onion_key.old",get_options()->DataDirectory);
if (!(prkey = crypto_new_pk_env())) {
log(LOG_ERR, "Error creating crypto environment.");
goto error;
return 0;
}
/* Make sure DataDirectory exists, and is private. */
- datadir = get_data_directory();
- tor_assert(datadir);
- if (strlen(datadir) > (512-128)) {
- log_fn(LOG_ERR, "DataDirectory is too long.");
- return -1;
- }
+ datadir = options->DataDirectory;
if (check_private_dir(datadir, 1)) {
return -1;
}
char filename[512];
int is_recent;
struct stat st;
- if (get_data_directory()) {
- char *s;
- tor_snprintf(filename,sizeof(filename),"%s/cached-directory", get_data_directory());
- if (stat(filename, &st)) {
- log_fn(LOG_WARN, "Unable to check status for '%s': %s", filename,
- strerror(errno));
- return 0;
+ char *s;
+ tor_assert(get_options()->DataDirectory);
+
+ tor_snprintf(filename,sizeof(filename),"%s/cached-directory",
+ get_options()->DataDirectory);
+ if (stat(filename, &st)) {
+ log_fn(LOG_WARN, "Unable to check status for '%s': %s", filename,
+ strerror(errno));
+ return 0;
+ }
+ s = read_file_to_str(filename,0);
+ if (s) {
+ tor_strstrip(s,"\r"); /* XXXX This is a bug workaround for win32. */
+ log_fn(LOG_INFO, "Loading cached directory from %s", filename);
+ is_recent = st.st_mtime > time(NULL) - 60*15;
+ if (router_load_routerlist_from_directory(s, NULL, is_recent) < 0) {
+ log_fn(LOG_WARN, "Cached directory '%s' was unparseable; ignoring.", filename);
}
- s = read_file_to_str(filename,0);
- if (s) {
- tor_strstrip(s,"\r"); /* XXXX This is a bug workaround for win32. */
- log_fn(LOG_INFO, "Loading cached directory from %s", filename);
- is_recent = st.st_mtime > time(NULL) - 60*15;
- if (router_load_routerlist_from_directory(s, NULL, is_recent) < 0) {
- log_fn(LOG_WARN, "Cached directory '%s' was unparseable; ignoring.", filename);
- }
- if(routerlist &&
- ((routerlist->published_on > time(NULL) - OLD_MIN_ONION_KEY_LIFETIME/2)
- || is_recent)) {
- /* XXX use new onion key lifetime when 0.0.8 servers are obsolete */
- directory_has_arrived(st.st_mtime); /* do things we've been waiting to do */
- }
- tor_free(s);
+ if(routerlist &&
+ ((routerlist->published_on > time(NULL) - OLD_MIN_ONION_KEY_LIFETIME/2)
+ || is_recent)) {
+ /* XXX use new onion key lifetime when 0.0.8 servers are obsolete */
+ directory_has_arrived(st.st_mtime); /* do things we've been waiting to do */
}
+ tor_free(s);
}
return 0;
}