comparison dbutil.c @ 1851:7f549ee3df48

Use HOME before /etc/passwd to find id_dropbear (#137) Currently dbclient uses the value of HOME by default when looking for ~/.ssh/known_hosts, falling back to /etc/passwd if HOME is not set (so that people can work around broken values in /etc/passwd). However, when locating the default authentication key (defaults to ~/.ssh/id_dropbear), paths not starting with / are always prefixed with the value from /etc/passwd. Make the behaviour consistent by adjusting expand_homedir_path to use the value of HOME, falling back to /etc/passwd if HOME is not set.
author Matt Robinson <git@nerdoftheherd.com>
date Tue, 19 Oct 2021 06:02:47 +0100
parents 90ac15aeac43
children 180e580778df
comparison
equal deleted inserted replaced
1850:d99b167994b5 1851:7f549ee3df48
607 /* Returns malloced path. inpath beginning with '/' is returned as-is, 607 /* Returns malloced path. inpath beginning with '/' is returned as-is,
608 otherwise home directory is prepended */ 608 otherwise home directory is prepended */
609 char * expand_homedir_path(const char *inpath) { 609 char * expand_homedir_path(const char *inpath) {
610 struct passwd *pw = NULL; 610 struct passwd *pw = NULL;
611 if (inpath[0] != '/') { 611 if (inpath[0] != '/') {
612 pw = getpwuid(getuid()); 612 char *homedir = getenv("HOME");
613 if (pw && pw->pw_dir) { 613
614 int len = strlen(inpath) + strlen(pw->pw_dir) + 2; 614 if (!homedir) {
615 pw = getpwuid(getuid());
616 if (pw) {
617 homedir = pw->pw_dir;
618 }
619 }
620
621 if (homedir) {
622 int len = strlen(inpath) + strlen(homedir) + 2;
615 char *buf = m_malloc(len); 623 char *buf = m_malloc(len);
616 snprintf(buf, len, "%s/%s", pw->pw_dir, inpath); 624 snprintf(buf, len, "%s/%s", homedir, inpath);
617 return buf; 625 return buf;
618 } 626 }
619 } 627 }
620 628
621 /* Fallback */ 629 /* Fallback */