Skip to content
Snippets Groups Projects
Commit c1b60214 authored by Matt Johnston's avatar Matt Johnston
Browse files

don't fail fatally if the client can't get homedir from getpwuid(), fallback

to $HOME.

--HG--
extra : convert_revision : 279bd16a3e639764df14dce868fdeea7d6a0f317
parent 719b47e3
No related merge requests found
...@@ -145,6 +145,7 @@ static void checkhostkey(unsigned char* keyblob, unsigned int keybloblen) { ...@@ -145,6 +145,7 @@ static void checkhostkey(unsigned char* keyblob, unsigned int keybloblen) {
FILE *hostsfile = NULL; FILE *hostsfile = NULL;
int readonly = 0; int readonly = 0;
struct passwd *pw = NULL; struct passwd *pw = NULL;
char * homedir = NULL;
unsigned int hostlen, algolen; unsigned int hostlen, algolen;
unsigned long len; unsigned long len;
const char *algoname = NULL; const char *algoname = NULL;
...@@ -153,41 +154,50 @@ static void checkhostkey(unsigned char* keyblob, unsigned int keybloblen) { ...@@ -153,41 +154,50 @@ static void checkhostkey(unsigned char* keyblob, unsigned int keybloblen) {
pw = getpwuid(getuid()); pw = getpwuid(getuid());
if (pw == NULL) { if (pw)
dropbear_exit("Failed to get homedir"); homedir = pw->pw_dir;
} }
pw = NULL;
len = strlen(pw->pw_dir); if (!homedir)
filename = m_malloc(len + 18); /* "/.ssh/known_hosts" and null-terminator*/ homedir = getenv("HOME");
snprintf(filename, len+18, "%s/.ssh", pw->pw_dir);
/* Check that ~/.ssh exists - easiest way is just to mkdir */
if (mkdir(filename, S_IRWXU) != 0) {
if (errno != EEXIST) {
dropbear_log(LOG_INFO, "Warning: failed creating ~/.ssh: %s",
strerror(errno));
TRACE(("mkdir didn't work: %s", strerror(errno)))
ask_to_confirm(keyblob, keybloblen);
goto out; /* only get here on success */
}
} }
snprintf(filename, len+18, "%s/.ssh/known_hosts", pw->pw_dir); if (homedir) {
hostsfile = fopen(filename, "a+");
len = strlen(homedir);
if (hostsfile != NULL) { filename = m_malloc(len + 18); /* "/.ssh/known_hosts" and null-terminator*/
fseek(hostsfile, 0, SEEK_SET);
} else { snprintf(filename, len+18, "%s/.ssh", homedir);
/* We mightn't have been able to open it if it was read-only */ /* Check that ~/.ssh exists - easiest way is just to mkdir */
if (errno == EACCES || errno == EROFS) { if (mkdir(filename, S_IRWXU) != 0) {
TRACE(("trying readonly: %s", strerror(errno))) if (errno != EEXIST) {
readonly = 1; dropbear_log(LOG_INFO, "Warning: failed creating ~/.ssh: %s",
hostsfile = fopen(filename, "r"); strerror(errno));
TRACE(("mkdir didn't work: %s", strerror(errno)))
ask_to_confirm(keyblob, keybloblen);
goto out; /* only get here on success */
}
}
snprintf(filename, len+18, "%s/.ssh/known_hosts", homedir);
hostsfile = fopen(filename, "a+");
if (hostsfile != NULL) {
fseek(hostsfile, 0, SEEK_SET);
} else {
/* We mightn't have been able to open it if it was read-only */
if (errno == EACCES || errno == EROFS) {
TRACE(("trying readonly: %s", strerror(errno)))
readonly = 1;
hostsfile = fopen(filename, "r");
}
} }
} }
if (hostsfile == NULL) { if (hostsfile == NULL) {
TRACE(("hostsfile didn't open: %s", strerror(errno))) TRACE(("hostsfile didn't open: %s", strerror(errno)))
dropbear_log(LOG_WARNING, "Failed to open ~/.ssh/known_hosts");
ask_to_confirm(keyblob, keybloblen); ask_to_confirm(keyblob, keybloblen);
goto out; /* We only get here on success */ goto out; /* We only get here on success */
} }
......
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