mirror of
https://gitlab.torproject.org/tpo/core/tor.git
synced 2024-11-30 23:53:32 +01:00
Some platforms have weird translations when you open files in "test" mode; make read/write_str_to_file aware.
svn:r2336
This commit is contained in:
parent
c66e4c4870
commit
0ef85f6dba
@ -345,7 +345,7 @@ int crypto_pk_read_private_key_from_filename(crypto_pk_env_t *env, const char *k
|
|||||||
tor_assert(env && keyfile);
|
tor_assert(env && keyfile);
|
||||||
|
|
||||||
/* open the keyfile */
|
/* open the keyfile */
|
||||||
f_pr=fopen(keyfile,"rb");
|
f_pr=fopen(keyfile,"r");
|
||||||
if (!f_pr)
|
if (!f_pr)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
@ -449,7 +449,7 @@ crypto_pk_write_private_key_to_filename(crypto_pk_env_t *env,
|
|||||||
s = tor_malloc(len+1);
|
s = tor_malloc(len+1);
|
||||||
strncpy(s, cp, len);
|
strncpy(s, cp, len);
|
||||||
s[len] = '\0';
|
s[len] = '\0';
|
||||||
r = write_str_to_file(fname, s);
|
r = write_str_to_file(fname, s, 0);
|
||||||
BIO_free(bio);
|
BIO_free(bio);
|
||||||
free(s);
|
free(s);
|
||||||
return r;
|
return r;
|
||||||
|
@ -119,6 +119,10 @@
|
|||||||
#include "strlcat.c"
|
#include "strlcat.c"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifndef O_BINARY
|
||||||
|
#define O_BINARY 0
|
||||||
|
#endif
|
||||||
|
|
||||||
/** Allocate a chunk of <b>size</b> bytes of memory, and return a pointer to
|
/** Allocate a chunk of <b>size</b> bytes of memory, and return a pointer to
|
||||||
* result. On error, log and terminate the process. (Same as malloc(size),
|
* result. On error, log and terminate the process. (Same as malloc(size),
|
||||||
* but never returns NULL.)
|
* but never returns NULL.)
|
||||||
@ -1459,33 +1463,29 @@ int check_private_dir(const char *dirname, int create)
|
|||||||
* This function replaces the old file atomically, if possible.
|
* This function replaces the old file atomically, if possible.
|
||||||
*/
|
*/
|
||||||
int
|
int
|
||||||
write_str_to_file(const char *fname, const char *str)
|
write_str_to_file(const char *fname, const char *str, int bin)
|
||||||
{
|
{
|
||||||
char tempname[1024];
|
char tempname[1024];
|
||||||
int fd;
|
int fd;
|
||||||
FILE *file;
|
size_t len;
|
||||||
if ((strlcpy(tempname,fname,1024) >= 1024) ||
|
if ((strlcpy(tempname,fname,1024) >= 1024) ||
|
||||||
(strlcat(tempname,".tmp",1024) >= 1024)) {
|
(strlcat(tempname,".tmp",1024) >= 1024)) {
|
||||||
log(LOG_WARN, "Filename %s.tmp too long (>1024 chars)", fname);
|
log(LOG_WARN, "Filename %s.tmp too long (>1024 chars)", fname);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
if ((fd = open(tempname, O_WRONLY|O_CREAT|O_TRUNC, 0600)) < 0) {
|
if ((fd = open(tempname, O_WRONLY|O_CREAT|O_TRUNC|(bin?O_BINARY:0), 0600))
|
||||||
|
< 0) {
|
||||||
log(LOG_WARN, "Couldn't open %s for writing: %s", tempname,
|
log(LOG_WARN, "Couldn't open %s for writing: %s", tempname,
|
||||||
strerror(errno));
|
strerror(errno));
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
if (!(file = fdopen(fd, "w"))) {
|
len = strlen(str);
|
||||||
log(LOG_WARN, "Couldn't fdopen %s for writing: %s", tempname,
|
if (write_all(fd, str, len, 0) != len) {
|
||||||
strerror(errno));
|
log(LOG_WARN, "Error writing to %s: %s", tempname, strerror(errno));
|
||||||
close(fd);
|
close(fd);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
if (fputs(str,file) == EOF) {
|
if (close(fd)) {
|
||||||
log(LOG_WARN, "Error writing to %s: %s", tempname, strerror(errno));
|
|
||||||
fclose(file);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
if (fclose(file) == EOF) {
|
|
||||||
log(LOG_WARN,"Error flushing to %s: %s", tempname, strerror(errno));
|
log(LOG_WARN,"Error flushing to %s: %s", tempname, strerror(errno));
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@ -1521,7 +1521,7 @@ write_str_to_file(const char *fname, const char *str)
|
|||||||
/** Read the contents of <b>filename</b> into a newly allocated string; return the
|
/** Read the contents of <b>filename</b> into a newly allocated string; return the
|
||||||
* string on success or NULL on failure.
|
* string on success or NULL on failure.
|
||||||
*/
|
*/
|
||||||
char *read_file_to_str(const char *filename) {
|
char *read_file_to_str(const char *filename, int bin) {
|
||||||
int fd; /* router file */
|
int fd; /* router file */
|
||||||
struct stat statbuf;
|
struct stat statbuf;
|
||||||
char *string;
|
char *string;
|
||||||
@ -1533,7 +1533,7 @@ char *read_file_to_str(const char *filename) {
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
fd = open(filename,O_RDONLY,0);
|
fd = open(filename,O_RDONLY|(bin?O_BINARY:0),0);
|
||||||
if (fd<0) {
|
if (fd<0) {
|
||||||
log_fn(LOG_WARN,"Could not open %s.",filename);
|
log_fn(LOG_WARN,"Could not open %s.",filename);
|
||||||
return NULL;
|
return NULL;
|
||||||
|
@ -219,8 +219,8 @@ typedef enum { FN_ERROR, FN_NOENT, FN_FILE, FN_DIR} file_status_t;
|
|||||||
|
|
||||||
file_status_t file_status(const char *filename);
|
file_status_t file_status(const char *filename);
|
||||||
int check_private_dir(const char *dirname, int create);
|
int check_private_dir(const char *dirname, int create);
|
||||||
int write_str_to_file(const char *fname, const char *str);
|
int write_str_to_file(const char *fname, const char *str, int bin);
|
||||||
char *read_file_to_str(const char *filename);
|
char *read_file_to_str(const char *filename, int bin);
|
||||||
int parse_line_from_file(char *line, int maxlen, FILE *f, char **key_out, char **value_out);
|
int parse_line_from_file(char *line, int maxlen, FILE *f, char **key_out, char **value_out);
|
||||||
char *expand_filename(const char *filename);
|
char *expand_filename(const char *filename);
|
||||||
int replace_file(const char *from, const char *to);
|
int replace_file(const char *from, const char *to);
|
||||||
|
@ -652,7 +652,7 @@ void dirserv_set_cached_directory(const char *directory, time_t when)
|
|||||||
cached_directory_published = when;
|
cached_directory_published = when;
|
||||||
if(get_data_directory(&options)) {
|
if(get_data_directory(&options)) {
|
||||||
sprintf(filename,"%s/cached-directory", get_data_directory(&options));
|
sprintf(filename,"%s/cached-directory", get_data_directory(&options));
|
||||||
if(write_str_to_file(filename,cached_directory) < 0) {
|
if(write_str_to_file(filename,cached_directory,0) < 0) {
|
||||||
log_fn(LOG_WARN, "Couldn't write cached directory to disk. Ignoring.");
|
log_fn(LOG_WARN, "Couldn't write cached directory to disk. Ignoring.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -725,7 +725,7 @@ static int dirserv_regenerate_directory(void)
|
|||||||
free(new_directory);
|
free(new_directory);
|
||||||
if(get_data_directory(&options)) {
|
if(get_data_directory(&options)) {
|
||||||
sprintf(filename,"%s/cached-directory", get_data_directory(&options));
|
sprintf(filename,"%s/cached-directory", get_data_directory(&options));
|
||||||
if(write_str_to_file(filename,the_directory) < 0) {
|
if(write_str_to_file(filename,the_directory,0) < 0) {
|
||||||
log_fn(LOG_WARN, "Couldn't write cached directory to disk. Ignoring.");
|
log_fn(LOG_WARN, "Couldn't write cached directory to disk. Ignoring.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -764,7 +764,7 @@ static int do_hup(void) {
|
|||||||
router_rebuild_descriptor();
|
router_rebuild_descriptor();
|
||||||
sprintf(keydir,"%s/router.desc", get_data_directory(&options));
|
sprintf(keydir,"%s/router.desc", get_data_directory(&options));
|
||||||
log_fn(LOG_INFO,"Dumping descriptor to %s...",keydir);
|
log_fn(LOG_INFO,"Dumping descriptor to %s...",keydir);
|
||||||
if (write_str_to_file(keydir, router_get_my_descriptor())) {
|
if (write_str_to_file(keydir, router_get_my_descriptor(), 0)) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -317,7 +317,7 @@ int rend_service_load_keys(void)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
sprintf(buf, "%s.onion\n", s->service_id);
|
sprintf(buf, "%s.onion\n", s->service_id);
|
||||||
if (write_str_to_file(fname,buf)<0)
|
if (write_str_to_file(fname,buf,0)<0)
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -319,7 +319,7 @@ int init_keys(void) {
|
|||||||
|
|
||||||
sprintf(keydir,"%s/router.desc", datadir);
|
sprintf(keydir,"%s/router.desc", datadir);
|
||||||
log_fn(LOG_INFO,"Dumping descriptor to %s...",keydir);
|
log_fn(LOG_INFO,"Dumping descriptor to %s...",keydir);
|
||||||
if (write_str_to_file(keydir, mydesc)) {
|
if (write_str_to_file(keydir, mydesc,0)) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
/* 5. Dump fingerprint to 'fingerprint' */
|
/* 5. Dump fingerprint to 'fingerprint' */
|
||||||
@ -334,7 +334,7 @@ int init_keys(void) {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
strcat(fingerprint, "\n");
|
strcat(fingerprint, "\n");
|
||||||
if (write_str_to_file(keydir, fingerprint))
|
if (write_str_to_file(keydir, fingerprint, 0))
|
||||||
return -1;
|
return -1;
|
||||||
if(!authdir_mode())
|
if(!authdir_mode())
|
||||||
return 0;
|
return 0;
|
||||||
@ -348,7 +348,7 @@ int init_keys(void) {
|
|||||||
/* 7. [authdirserver only] load old directory, if it's there */
|
/* 7. [authdirserver only] load old directory, if it's there */
|
||||||
sprintf(keydir,"%s/cached-directory", datadir);
|
sprintf(keydir,"%s/cached-directory", datadir);
|
||||||
log_fn(LOG_INFO,"Loading cached directory from %s...",keydir);
|
log_fn(LOG_INFO,"Loading cached directory from %s...",keydir);
|
||||||
cp = read_file_to_str(keydir);
|
cp = read_file_to_str(keydir,0);
|
||||||
if(!cp) {
|
if(!cp) {
|
||||||
log_fn(LOG_INFO,"Cached directory %s not present. Ok.",keydir);
|
log_fn(LOG_INFO,"Cached directory %s not present. Ok.",keydir);
|
||||||
} else {
|
} else {
|
||||||
|
@ -60,7 +60,7 @@ int router_reload_router_list(void)
|
|||||||
if (get_data_directory(&options)) {
|
if (get_data_directory(&options)) {
|
||||||
char *s;
|
char *s;
|
||||||
sprintf(filename,"%s/cached-directory", get_data_directory(&options));
|
sprintf(filename,"%s/cached-directory", get_data_directory(&options));
|
||||||
s = read_file_to_str(filename);
|
s = read_file_to_str(filename,0);
|
||||||
if (s) {
|
if (s) {
|
||||||
log_fn(LOG_INFO, "Loading cached directory from %s", filename);
|
log_fn(LOG_INFO, "Loading cached directory from %s", filename);
|
||||||
if (router_load_routerlist_from_string(s, 0) < 0) {
|
if (router_load_routerlist_from_string(s, 0) < 0) {
|
||||||
@ -693,7 +693,7 @@ int router_load_routerlist_from_file(char *routerfile, int trusted)
|
|||||||
{
|
{
|
||||||
char *string;
|
char *string;
|
||||||
|
|
||||||
string = read_file_to_str(routerfile);
|
string = read_file_to_str(routerfile,0);
|
||||||
if(!string) {
|
if(!string) {
|
||||||
log_fn(LOG_WARN,"Failed to load routerfile %s.",routerfile);
|
log_fn(LOG_WARN,"Failed to load routerfile %s.",routerfile);
|
||||||
return -1;
|
return -1;
|
||||||
|
Loading…
Reference in New Issue
Block a user