mirror of
https://github.com/postgres/postgres.git
synced 2025-09-02 04:21:28 +03:00
Fix incorrect error message reported for non-existent users
Previously, lookups of non-existent user names could return "Success"; it will now return "User does not exist" by resetting errno. This also centralizes the user name lookup code in libpgport. Report and analysis by Nicolas Marchildon; patch by me
This commit is contained in:
@@ -33,7 +33,7 @@ LIBS += $(PTHREAD_LIBS)
|
||||
OBJS = $(LIBOBJS) chklocale.o dirmod.o erand48.o fls.o inet_net_ntop.o \
|
||||
noblock.o path.o pgcheckdir.o pg_crc.o pgmkdirp.o pgsleep.o \
|
||||
pgstrcasecmp.o pqsignal.o \
|
||||
qsort.o qsort_arg.o quotes.o sprompt.o tar.o thread.o
|
||||
qsort.o qsort_arg.o quotes.o sprompt.o tar.o thread.o username.o
|
||||
|
||||
# foo_srv.o and foo.o are both built from foo.c, but only foo.o has -DFRONTEND
|
||||
OBJS_SRV = $(OBJS:%.o=%_srv.o)
|
||||
|
84
src/port/username.c
Normal file
84
src/port/username.c
Normal file
@@ -0,0 +1,84 @@
|
||||
/*-------------------------------------------------------------------------
|
||||
*
|
||||
* username.c
|
||||
* get user name
|
||||
*
|
||||
* Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
|
||||
* Portions Copyright (c) 1994, Regents of the University of California
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* src/port/username.c
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
#ifndef FRONTEND
|
||||
#include "postgres.h"
|
||||
#else
|
||||
#include "postgres_fe.h"
|
||||
#endif
|
||||
|
||||
#include <errno.h>
|
||||
#include <pwd.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
|
||||
/*
|
||||
* Returns the current user name in a static buffer, or NULL on error and
|
||||
* sets errstr
|
||||
*/
|
||||
const char *
|
||||
get_user_name(char **errstr)
|
||||
{
|
||||
#ifndef WIN32
|
||||
struct passwd *pw;
|
||||
uid_t user_id = geteuid();
|
||||
|
||||
*errstr = NULL;
|
||||
|
||||
errno = 0; /* clear errno before call */
|
||||
pw = getpwuid(user_id);
|
||||
if (!pw)
|
||||
{
|
||||
*errstr = psprintf(_("failed to look up effective user id %d: %s"),
|
||||
(int) user_id, errno ? strerror(errno) :
|
||||
_("user does not exist"));
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return pw->pw_name;
|
||||
#else
|
||||
/* UNLEN = 256, 'static' variable remains after function exit */
|
||||
static char username[256 + 1];
|
||||
DWORD len = sizeof(username) - 1;
|
||||
|
||||
if (!GetUserName(username, &len))
|
||||
{
|
||||
*errstr = psprintf(_("user name lookup failure: %s"), strerror(errno));
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return username;
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Returns the current user name in a static buffer or exits
|
||||
*/
|
||||
const char *
|
||||
get_user_name_or_exit(const char *progname)
|
||||
{
|
||||
const char *user_name;
|
||||
char *errstr;
|
||||
|
||||
user_name = get_user_name(&errstr);
|
||||
|
||||
if (!user_name)
|
||||
{
|
||||
fprintf(stderr, "%s: %s\n", progname, errstr);
|
||||
exit(1);
|
||||
}
|
||||
return user_name;
|
||||
}
|
Reference in New Issue
Block a user