summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Lane2022-01-10 00:19:02 +0000
committerTom Lane2022-01-10 00:19:02 +0000
commit376ce3e404b75d267089c4bc6dc7c18d0b38728b (patch)
tree197a5ab6a9a66e7ada4fd1d027e1e5e5147c717a
parent6867f963e319934cbdafeb4bd5beaea5797c7be2 (diff)
Prefer $HOME when looking up the current user's home directory.
When we need to identify the home directory on non-Windows, first consult getenv("HOME"). If that's empty or unset, fall back on our previous method of checking the <pwd.h> database. Preferring $HOME allows the user to intentionally point at some other directory, and it seems to be in line with the behavior of most other utilities. However, we shouldn't rely on it completely, as $HOME is likely to be unset when running as a daemon. Anders Kaseorg Discussion: https://postgr.es/m/[email protected]
-rw-r--r--src/bin/psql/command.c31
-rw-r--r--src/interfaces/libpq/fe-connect.c21
-rw-r--r--src/port/path.c25
3 files changed, 51 insertions, 26 deletions
diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c
index 053332e7af..f590474855 100644
--- a/src/bin/psql/command.c
+++ b/src/bin/psql/command.c
@@ -558,19 +558,25 @@ exec_command_cd(PsqlScanState scan_state, bool active_branch, const char *cmd)
else
{
#ifndef WIN32
- struct passwd *pw;
- uid_t user_id = geteuid();
-
- errno = 0; /* clear errno before call */
- pw = getpwuid(user_id);
- if (!pw)
+ /* This should match get_home_path() */
+ dir = getenv("HOME");
+ if (dir == NULL || dir[0] == '\0')
{
- pg_log_error("could not get home directory for user ID %ld: %s",
- (long) user_id,
- errno ? strerror(errno) : _("user does not exist"));
- exit(EXIT_FAILURE);
+ uid_t user_id = geteuid();
+ struct passwd *pw;
+
+ errno = 0; /* clear errno before call */
+ pw = getpwuid(user_id);
+ if (pw)
+ dir = pw->pw_dir;
+ else
+ {
+ pg_log_error("could not get home directory for user ID %ld: %s",
+ (long) user_id,
+ errno ? strerror(errno) : _("user does not exist"));
+ success = false;
+ }
}
- dir = pw->pw_dir;
#else /* WIN32 */
/*
@@ -581,7 +587,8 @@ exec_command_cd(PsqlScanState scan_state, bool active_branch, const char *cmd)
#endif /* WIN32 */
}
- if (chdir(dir) == -1)
+ if (success &&
+ chdir(dir) < 0)
{
pg_log_error("\\%s: could not change directory to \"%s\": %m",
cmd, dir);
diff --git a/src/interfaces/libpq/fe-connect.c b/src/interfaces/libpq/fe-connect.c
index 72914116ee..a12e0180fd 100644
--- a/src/interfaces/libpq/fe-connect.c
+++ b/src/interfaces/libpq/fe-connect.c
@@ -7267,14 +7267,21 @@ bool
pqGetHomeDirectory(char *buf, int bufsize)
{
#ifndef WIN32
- char pwdbuf[BUFSIZ];
- struct passwd pwdstr;
- struct passwd *pwd = NULL;
+ const char *home;
- (void) pqGetpwuid(geteuid(), &pwdstr, pwdbuf, sizeof(pwdbuf), &pwd);
- if (pwd == NULL)
- return false;
- strlcpy(buf, pwd->pw_dir, bufsize);
+ home = getenv("HOME");
+ if (home == NULL || home[0] == '\0')
+ {
+ char pwdbuf[BUFSIZ];
+ struct passwd pwdstr;
+ struct passwd *pwd = NULL;
+
+ (void) pqGetpwuid(geteuid(), &pwdstr, pwdbuf, sizeof(pwdbuf), &pwd);
+ if (pwd == NULL)
+ return false;
+ home = pwd->pw_dir;
+ }
+ strlcpy(buf, home, bufsize);
return true;
#else
char tmppath[MAX_PATH];
diff --git a/src/port/path.c b/src/port/path.c
index ee4227ec98..5ac26f4bcf 100644
--- a/src/port/path.c
+++ b/src/port/path.c
@@ -807,14 +807,25 @@ bool
get_home_path(char *ret_path)
{
#ifndef WIN32
- char pwdbuf[BUFSIZ];
- struct passwd pwdstr;
- struct passwd *pwd = NULL;
+ /*
+ * We first consult $HOME. If that's unset, try to get the info from
+ * <pwd.h>.
+ */
+ const char *home;
- (void) pqGetpwuid(geteuid(), &pwdstr, pwdbuf, sizeof(pwdbuf), &pwd);
- if (pwd == NULL)
- return false;
- strlcpy(ret_path, pwd->pw_dir, MAXPGPATH);
+ home = getenv("HOME");
+ if (home == NULL || home[0] == '\0')
+ {
+ char pwdbuf[BUFSIZ];
+ struct passwd pwdstr;
+ struct passwd *pwd = NULL;
+
+ (void) pqGetpwuid(geteuid(), &pwdstr, pwdbuf, sizeof(pwdbuf), &pwd);
+ if (pwd == NULL)
+ return false;
+ home = pwd->pw_dir;
+ }
+ strlcpy(ret_path, home, MAXPGPATH);
return true;
#else
char *tmppath;