1
0
mirror of https://github.com/postgres/postgres.git synced 2025-11-21 00:42:43 +03:00

More use of getpwuid_r() directly

Remove src/port/user.c, call getpwuid_r() directly.  This reduces some
complexity and allows better control of the error behavior.  For
example, the old code would in some circumstances silently truncate
the result string, or produce error message strings that the caller
wouldn't use.

src/port/user.c used to be called src/port/thread.c and contained
various portability complications to support thread-safety.  These are
all obsolete, and all but the user-lookup functions have already been
removed.  This patch completes this by also removing the user-lookup
functions.

Also convert src/backend/libpq/auth.c to use getpwuid_r() for
thread-safety.

Originally, I tried to be overly correct by using
sysconf(_SC_GETPW_R_SIZE_MAX) to get the buffer size for getpwuid_r(),
but that doesn't work on FreeBSD.  All the OS where I could find the
source code internally use 1024 as the suggested buffer size, so I
just ended up hardcoding that.  The previous code used BUFSIZ, which
is an unrelated constant from stdio.h, so its use seemed
inappropriate.

Reviewed-by: Heikki Linnakangas <hlinnaka@iki.fi>
Discussion: https://www.postgresql.org/message-id/flat/5f293da9-ceb4-4937-8e52-82c25db8e4d3%40eisentraut.org
This commit is contained in:
Peter Eisentraut
2024-09-02 08:16:25 +02:00
parent 23138284cd
commit 4d5111b3f1
10 changed files with 72 additions and 123 deletions

View File

@@ -1857,7 +1857,10 @@ auth_peer(hbaPort *port)
uid_t uid;
gid_t gid;
#ifndef WIN32
struct passwd pwbuf;
struct passwd *pw;
char buf[1024];
int rc;
int ret;
#endif
@@ -1876,16 +1879,18 @@ auth_peer(hbaPort *port)
}
#ifndef WIN32
errno = 0; /* clear errno before call */
pw = getpwuid(uid);
if (!pw)
rc = getpwuid_r(uid, &pwbuf, buf, sizeof buf, &pw);
if (rc != 0)
{
int save_errno = errno;
errno = rc;
ereport(LOG,
(errmsg("could not look up local user ID %ld: %s",
(long) uid,
save_errno ? strerror(save_errno) : _("user does not exist"))));
errmsg("could not look up local user ID %ld: %m", (long) uid));
return STATUS_ERROR;
}
else if (!pw)
{
ereport(LOG,
errmsg("local user with ID %ld does not exist", (long) uid));
return STATUS_ERROR;
}