1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-04-22 21:45:03 -05:00

Idiot me! store_host_key() was blindly _appending_ new host keys to

the end of the host key file. This is perfectly all right if a host
key never changes, but it's completely useless if you need to
replace an existing entry. This version should do better.

[originally from svn r3719]
This commit is contained in:
Simon Tatham 2004-01-17 13:00:18 +00:00
parent 2817a7e150
commit 276c8335af

View File

@ -19,7 +19,7 @@
#include "tree234.h" #include "tree234.h"
enum { enum {
INDEX_DIR, INDEX_HOSTKEYS, INDEX_RANDSEED, INDEX_DIR, INDEX_HOSTKEYS, INDEX_HOSTKEYS_TMP, INDEX_RANDSEED,
INDEX_SESSIONDIR, INDEX_SESSION, INDEX_SESSIONDIR, INDEX_SESSION,
}; };
@ -98,6 +98,7 @@ static void make_filename(char *filename, int index, const char *subname)
index == INDEX_DIR ? "/.putty" : index == INDEX_DIR ? "/.putty" :
index == INDEX_SESSIONDIR ? "/.putty/sessions" : index == INDEX_SESSIONDIR ? "/.putty/sessions" :
index == INDEX_HOSTKEYS ? "/.putty/sshhostkeys" : index == INDEX_HOSTKEYS ? "/.putty/sshhostkeys" :
index == INDEX_HOSTKEYS_TMP ? "/.putty/sshhostkeys.tmp" :
index == INDEX_RANDSEED ? "/.putty/randomseed" : index == INDEX_RANDSEED ? "/.putty/randomseed" :
"/.putty/ERROR", FILENAME_MAX - len); "/.putty/ERROR", FILENAME_MAX - len);
} }
@ -495,26 +496,48 @@ int verify_host_key(const char *hostname, int port,
void store_host_key(const char *hostname, int port, void store_host_key(const char *hostname, int port,
const char *keytype, const char *key) const char *keytype, const char *key)
{ {
FILE *fp; FILE *rfp, *wfp;
int fd; char *newtext, *line;
char filename[FILENAME_MAX]; int headerlen;
char filename[FILENAME_MAX], tmpfilename[FILENAME_MAX];
newtext = dupprintf("%s@%d:%s %s\n", keytype, port, hostname, key);
headerlen = 1 + strcspn(newtext, " "); /* count the space too */
/*
* Open both the old file and a new file.
*/
make_filename(filename, INDEX_HOSTKEYS, NULL); make_filename(filename, INDEX_HOSTKEYS, NULL);
fd = open(filename, O_CREAT | O_APPEND | O_RDWR, 0600); rfp = fopen(filename, "r");
if (fd < 0) { if (!rfp)
char dir[FILENAME_MAX]; return;
make_filename(tmpfilename, INDEX_HOSTKEYS_TMP, NULL);
wfp = fopen(tmpfilename, "w");
if (!wfp) {
fclose(rfp);
return;
}
make_filename(dir, INDEX_DIR, NULL); /*
mkdir(dir, 0700); * Copy all lines from the old file to the new one that _don't_
fd = open(filename, O_CREAT | O_APPEND | O_RDWR, 0600); * involve the same host key identifier as the one we're adding.
*/
while ( (line = fgetline(rfp)) ) {
if (strncmp(line, newtext, headerlen))
fputs(line, wfp);
} }
if (fd < 0) {
perror(filename); /*
exit(1); * Now add the new line at the end.
} */
fp = fdopen(fd, "a"); fputs(newtext, wfp);
fprintf(fp, "%s@%d:%s %s\n", keytype, port, hostname, key);
fclose(fp); fclose(rfp);
fclose(wfp);
rename(tmpfilename, filename);
sfree(newtext);
} }
void read_random_seed(noise_consumer_t consumer) void read_random_seed(noise_consumer_t consumer)