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

to $HOME.

--HG--
extra : convert_revision : 279bd16a3e639764df14dce868fdeea7d6a0f317
This commit is contained in:
Matt Johnston 2006-04-12 05:51:32 +00:00
parent 719b47e3aa
commit c1b602145f

View File

@ -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,14 +154,21 @@ 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;
if (!homedir)
homedir = getenv("HOME");
} }
len = strlen(pw->pw_dir); if (homedir) {
len = strlen(homedir);
filename = m_malloc(len + 18); /* "/.ssh/known_hosts" and null-terminator*/ filename = m_malloc(len + 18); /* "/.ssh/known_hosts" and null-terminator*/
snprintf(filename, len+18, "%s/.ssh", pw->pw_dir); snprintf(filename, len+18, "%s/.ssh", homedir);
/* Check that ~/.ssh exists - easiest way is just to mkdir */ /* Check that ~/.ssh exists - easiest way is just to mkdir */
if (mkdir(filename, S_IRWXU) != 0) { if (mkdir(filename, S_IRWXU) != 0) {
if (errno != EEXIST) { if (errno != EEXIST) {
@ -172,7 +180,7 @@ static void checkhostkey(unsigned char* keyblob, unsigned int keybloblen) {
} }
} }
snprintf(filename, len+18, "%s/.ssh/known_hosts", pw->pw_dir); snprintf(filename, len+18, "%s/.ssh/known_hosts", homedir);
hostsfile = fopen(filename, "a+"); hostsfile = fopen(filename, "a+");
if (hostsfile != NULL) { if (hostsfile != NULL) {
@ -185,9 +193,11 @@ static void checkhostkey(unsigned char* keyblob, unsigned int keybloblen) {
hostsfile = fopen(filename, "r"); 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 */
} }