diff options
| author | Bruce Momjian | 2013-12-18 17:16:16 +0000 |
|---|---|---|
| committer | Bruce Momjian | 2013-12-18 17:16:21 +0000 |
| commit | 613c6d26bd42dd8c2dd0664315be9551475b8864 (patch) | |
| tree | e0eb178bf76220fc9b082d9e849bee67c03f9e13 /src/backend | |
| parent | 11ac4c73cb89551d7e0d0180b58d82186f072f8d (diff) | |
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
Diffstat (limited to 'src/backend')
| -rw-r--r-- | src/backend/libpq/auth.c | 15 | ||||
| -rw-r--r-- | src/backend/main/main.c | 37 |
2 files changed, 8 insertions, 44 deletions
diff --git a/src/backend/libpq/auth.c b/src/backend/libpq/auth.c index 2dbf7e53a12..6d11e576a4b 100644 --- a/src/backend/libpq/auth.c +++ b/src/backend/libpq/auth.c @@ -1771,7 +1771,8 @@ auth_peer(hbaPort *port) char ident_user[IDENT_USERNAME_MAX + 1]; uid_t uid; gid_t gid; - struct passwd *pass; + const char *user_name; + char *errstr; errno = 0; if (getpeereid(port->sock, &uid, &gid) != 0) @@ -1788,17 +1789,15 @@ auth_peer(hbaPort *port) return STATUS_ERROR; } - pass = getpwuid(uid); - - if (pass == NULL) + user_name = get_user_name(&errstr); + if (!user_name) { - ereport(LOG, - (errmsg("local user with ID %d does not exist", - (int) uid))); + ereport(LOG, (errmsg_internal("%s", errstr))); + pfree(errstr); return STATUS_ERROR; } - strlcpy(ident_user, pass->pw_name, IDENT_USERNAME_MAX + 1); + strlcpy(ident_user, user_name, IDENT_USERNAME_MAX + 1); return check_usermap(port->hba->usermap, port->user_name, ident_user, false); } diff --git a/src/backend/main/main.c b/src/backend/main/main.c index d71885dba9d..376aa39a985 100644 --- a/src/backend/main/main.c +++ b/src/backend/main/main.c @@ -20,7 +20,6 @@ */ #include "postgres.h" -#include <pwd.h> #include <unistd.h> #if defined(__alpha) && defined(__osf__) /* no __alpha__ ? */ @@ -49,7 +48,6 @@ const char *progname; static void startup_hacks(const char *progname); static void help(const char *progname); static void check_root(const char *progname); -static char *get_current_username(const char *progname); /* @@ -191,7 +189,7 @@ main(int argc, char *argv[]) else if (argc > 1 && strcmp(argv[1], "--single") == 0) PostgresMain(argc, argv, NULL, /* no dbname */ - get_current_username(progname)); /* does not return */ + strdup(get_user_name_or_exit(progname))); /* does not return */ else PostmasterMain(argc, argv); /* does not return */ abort(); /* should not get here */ @@ -372,36 +370,3 @@ check_root(const char *progname) } #endif /* WIN32 */ } - - - -static char * -get_current_username(const char *progname) -{ -#ifndef WIN32 - struct passwd *pw; - - pw = getpwuid(geteuid()); - if (pw == NULL) - { - write_stderr("%s: invalid effective UID: %d\n", - progname, (int) geteuid()); - exit(1); - } - /* Allocate new memory because later getpwuid() calls can overwrite it. */ - return strdup(pw->pw_name); -#else - unsigned long namesize = 256 /* UNLEN */ + 1; - char *name; - - name = malloc(namesize); - if (!GetUserName(name, &namesize)) - { - write_stderr("%s: could not determine user name (GetUserName failed)\n", - progname); - exit(1); - } - - return name; -#endif -} |
