mirror of
https://github.com/postgres/postgres.git
synced 2025-08-31 17:02:12 +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:
@@ -28,6 +28,7 @@
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <limits.h>
|
||||
#include <pwd.h>
|
||||
#include <sys/param.h> /* for MAXHOSTNAMELEN on most */
|
||||
#include <sys/socket.h>
|
||||
#ifdef HAVE_SYS_UCRED_H
|
||||
@@ -1203,7 +1204,10 @@ pg_fe_getusername(uid_t user_id, PQExpBuffer errorMessage)
|
||||
char username[256 + 1];
|
||||
DWORD namesize = sizeof(username);
|
||||
#else
|
||||
char pwdbuf[BUFSIZ];
|
||||
struct passwd pwbuf;
|
||||
struct passwd *pw;
|
||||
char buf[1024];
|
||||
int rc;
|
||||
#endif
|
||||
|
||||
#ifdef WIN32
|
||||
@@ -1214,10 +1218,19 @@ pg_fe_getusername(uid_t user_id, PQExpBuffer errorMessage)
|
||||
"user name lookup failure: error code %lu",
|
||||
GetLastError());
|
||||
#else
|
||||
if (pg_get_user_name(user_id, pwdbuf, sizeof(pwdbuf)))
|
||||
name = pwdbuf;
|
||||
else if (errorMessage)
|
||||
appendPQExpBuffer(errorMessage, "%s\n", pwdbuf);
|
||||
rc = getpwuid_r(user_id, &pwbuf, buf, sizeof buf, &pw);
|
||||
if (rc != 0)
|
||||
{
|
||||
errno = rc;
|
||||
if (errorMessage)
|
||||
libpq_append_error(errorMessage, "could not look up local user ID %ld: %m", (long) user_id);
|
||||
}
|
||||
else if (!pw)
|
||||
{
|
||||
if (errorMessage)
|
||||
libpq_append_error(errorMessage, "local user with ID %ld does not exist", (long) user_id);
|
||||
}
|
||||
name = pw->pw_name;
|
||||
#endif
|
||||
|
||||
if (name)
|
||||
|
@@ -50,6 +50,7 @@
|
||||
#include <netdb.h>
|
||||
#include <netinet/in.h>
|
||||
#include <netinet/tcp.h>
|
||||
#include <pwd.h>
|
||||
#endif
|
||||
|
||||
#ifdef WIN32
|
||||
@@ -7702,10 +7703,24 @@ pqGetHomeDirectory(char *buf, int bufsize)
|
||||
const char *home;
|
||||
|
||||
home = getenv("HOME");
|
||||
if (home == NULL || home[0] == '\0')
|
||||
return pg_get_user_home_dir(geteuid(), buf, bufsize);
|
||||
strlcpy(buf, home, bufsize);
|
||||
return true;
|
||||
if (home && home[0])
|
||||
{
|
||||
strlcpy(buf, home, bufsize);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
struct passwd pwbuf;
|
||||
struct passwd *pw;
|
||||
char tmpbuf[1024];
|
||||
int rc;
|
||||
|
||||
rc = getpwuid_r(geteuid(), &pwbuf, tmpbuf, sizeof tmpbuf, &pw);
|
||||
if (rc != 0 || !pw)
|
||||
return false;
|
||||
strlcpy(buf, pw->pw_dir, bufsize);
|
||||
return true;
|
||||
}
|
||||
#else
|
||||
char tmppath[MAX_PATH];
|
||||
|
||||
|
@@ -13,8 +13,7 @@ GETTEXT_FILES = fe-auth.c \
|
||||
fe-secure-common.c \
|
||||
fe-secure-gssapi.c \
|
||||
fe-secure-openssl.c \
|
||||
win32.c \
|
||||
../../port/user.c
|
||||
win32.c
|
||||
GETTEXT_TRIGGERS = libpq_append_conn_error:2 \
|
||||
libpq_append_error:2 \
|
||||
libpq_gettext \
|
||||
|
Reference in New Issue
Block a user