diff options
author | Bruce Momjian <bruce@momjian.us> | 2013-12-18 12:16:16 -0500 |
---|---|---|
committer | Bruce Momjian <bruce@momjian.us> | 2013-12-18 12:16:21 -0500 |
commit | 613c6d26bd42dd8c2dd0664315be9551475b8864 (patch) | |
tree | e0eb178bf76220fc9b082d9e849bee67c03f9e13 /src/bin | |
parent | 11ac4c73cb89551d7e0d0180b58d82186f072f8d (diff) | |
download | postgresql-613c6d26bd42dd8c2dd0664315be9551475b8864.tar.gz postgresql-613c6d26bd42dd8c2dd0664315be9551475b8864.zip |
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/bin')
-rw-r--r-- | src/bin/initdb/initdb.c | 37 | ||||
-rw-r--r-- | src/bin/psql/command.c | 5 | ||||
-rw-r--r-- | src/bin/psql/help.c | 24 | ||||
-rw-r--r-- | src/bin/scripts/clusterdb.c | 2 | ||||
-rw-r--r-- | src/bin/scripts/common.c | 33 | ||||
-rw-r--r-- | src/bin/scripts/common.h | 2 | ||||
-rw-r--r-- | src/bin/scripts/createdb.c | 2 | ||||
-rw-r--r-- | src/bin/scripts/createlang.c | 2 | ||||
-rw-r--r-- | src/bin/scripts/createuser.c | 2 | ||||
-rw-r--r-- | src/bin/scripts/droplang.c | 2 | ||||
-rw-r--r-- | src/bin/scripts/reindexdb.c | 4 | ||||
-rw-r--r-- | src/bin/scripts/vacuumdb.c | 2 |
12 files changed, 22 insertions, 95 deletions
diff --git a/src/bin/initdb/initdb.c b/src/bin/initdb/initdb.c index e6bb132beaf..964d284bb66 100644 --- a/src/bin/initdb/initdb.c +++ b/src/bin/initdb/initdb.c @@ -770,15 +770,14 @@ exit_nicely(void) /* * find the current user * - * on unix make sure it isn't really root + * on unix make sure it isn't root */ static char * get_id(void) { -#ifndef WIN32 - - struct passwd *pw; + const char *username; +#ifndef WIN32 if (geteuid() == 0) /* 0 is root's uid */ { fprintf(stderr, @@ -789,35 +788,11 @@ get_id(void) progname); exit(1); } - - pw = getpwuid(geteuid()); - if (!pw) - { - fprintf(stderr, - _("%s: could not obtain information about current user: %s\n"), - progname, strerror(errno)); - exit(1); - } -#else /* the windows code */ - - struct passwd_win32 - { - int pw_uid; - char pw_name[128]; - } pass_win32; - struct passwd_win32 *pw = &pass_win32; - DWORD pwname_size = sizeof(pass_win32.pw_name) - 1; - - pw->pw_uid = 1; - if (!GetUserName(pw->pw_name, &pwname_size)) - { - fprintf(stderr, _("%s: could not get current user name: %s\n"), - progname, strerror(errno)); - exit(1); - } #endif - return pg_strdup(pw->pw_name); + username = get_user_name_or_exit(progname); + + return pg_strdup(username); } static char * diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c index 638d8cb5c31..d60a661543d 100644 --- a/src/bin/psql/command.c +++ b/src/bin/psql/command.c @@ -265,10 +265,13 @@ exec_command(const char *cmd, #ifndef WIN32 struct passwd *pw; + errno = 0; /* clear errno before call */ pw = getpwuid(geteuid()); if (!pw) { - psql_error("could not get home directory: %s\n", strerror(errno)); + psql_error("could not get home directory for user id %d: %s\n", + (int) geteuid(), errno ? + strerror(errno) : "user does not exist"); exit(EXIT_FAILURE); } dir = pw->pw_dir; diff --git a/src/bin/psql/help.c b/src/bin/psql/help.c index 30530f2e37c..80b1ec0dfc7 100644 --- a/src/bin/psql/help.c +++ b/src/bin/psql/help.c @@ -8,9 +8,6 @@ #include "postgres_fe.h" #ifndef WIN32 -#ifdef HAVE_PWD_H -#include <pwd.h> /* for getpwuid() */ -#endif #include <sys/types.h> /* (ditto) */ #include <unistd.h> /* for geteuid() */ #else @@ -52,31 +49,18 @@ usage(void) { const char *env; const char *user; - -#ifndef WIN32 - struct passwd *pw = NULL; -#endif + char *errstr; /* Find default user, in case we need it. */ user = getenv("PGUSER"); if (!user) { -#if !defined(WIN32) && !defined(__OS2__) - pw = getpwuid(geteuid()); - if (pw) - user = pw->pw_name; - else + user = get_user_name(&errstr); + if (!user) { - psql_error("could not get current user name: %s\n", strerror(errno)); + psql_error("%s\n", errstr); exit(EXIT_FAILURE); } -#else /* WIN32 */ - char buf[128]; - DWORD bufsize = sizeof(buf) - 1; - - if (GetUserName(buf, &bufsize)) - user = buf; -#endif /* WIN32 */ } printf(_("psql is the PostgreSQL interactive terminal.\n\n")); diff --git a/src/bin/scripts/clusterdb.c b/src/bin/scripts/clusterdb.c index cd54e8f47f7..e7065ce5df9 100644 --- a/src/bin/scripts/clusterdb.c +++ b/src/bin/scripts/clusterdb.c @@ -160,7 +160,7 @@ main(int argc, char *argv[]) else if (getenv("PGUSER")) dbname = getenv("PGUSER"); else - dbname = get_user_name(progname); + dbname = get_user_name_or_exit(progname); } if (tables.head != NULL) diff --git a/src/bin/scripts/common.c b/src/bin/scripts/common.c index 4645bc137f9..e57e5207c6b 100644 --- a/src/bin/scripts/common.c +++ b/src/bin/scripts/common.c @@ -14,7 +14,6 @@ #include "postgres_fe.h" -#include <pwd.h> #include <signal.h> #include <unistd.h> @@ -30,38 +29,6 @@ static CRITICAL_SECTION cancelConnLock; #endif /* - * Returns the current user name. - */ -const char * -get_user_name(const char *progname) -{ -#ifndef WIN32 - struct passwd *pw; - - pw = getpwuid(geteuid()); - if (!pw) - { - fprintf(stderr, _("%s: could not obtain information about current user: %s\n"), - progname, strerror(errno)); - exit(1); - } - return pw->pw_name; -#else - static char username[128]; /* remains after function exit */ - DWORD len = sizeof(username) - 1; - - if (!GetUserName(username, &len)) - { - fprintf(stderr, _("%s: could not get current user name: %s\n"), - progname, strerror(errno)); - exit(1); - } - return username; -#endif -} - - -/* * Provide strictly harmonized handling of --help and --version * options. */ diff --git a/src/bin/scripts/common.h b/src/bin/scripts/common.h index 6cf490f7484..a8a81f66b4b 100644 --- a/src/bin/scripts/common.h +++ b/src/bin/scripts/common.h @@ -22,8 +22,6 @@ enum trivalue typedef void (*help_handler) (const char *progname); -extern const char *get_user_name(const char *progname); - extern void handle_help_version_opts(int argc, char *argv[], const char *fixed_progname, help_handler hlp); diff --git a/src/bin/scripts/createdb.c b/src/bin/scripts/createdb.c index 14cd1284906..856a04d6485 100644 --- a/src/bin/scripts/createdb.c +++ b/src/bin/scripts/createdb.c @@ -174,7 +174,7 @@ main(int argc, char *argv[]) else if (getenv("PGUSER")) dbname = getenv("PGUSER"); else - dbname = get_user_name(progname); + dbname = get_user_name_or_exit(progname); } initPQExpBuffer(&sql); diff --git a/src/bin/scripts/createlang.c b/src/bin/scripts/createlang.c index ff544a803d1..5cfba8e3d5a 100644 --- a/src/bin/scripts/createlang.c +++ b/src/bin/scripts/createlang.c @@ -127,7 +127,7 @@ main(int argc, char *argv[]) else if (getenv("PGUSER")) dbname = getenv("PGUSER"); else - dbname = get_user_name(progname); + dbname = get_user_name_or_exit(progname); } initPQExpBuffer(&sql); diff --git a/src/bin/scripts/createuser.c b/src/bin/scripts/createuser.c index 61805917704..e3ce0dc455f 100644 --- a/src/bin/scripts/createuser.c +++ b/src/bin/scripts/createuser.c @@ -193,7 +193,7 @@ main(int argc, char *argv[]) if (getenv("PGUSER")) newuser = getenv("PGUSER"); else - newuser = get_user_name(progname); + newuser = get_user_name_or_exit(progname); } } diff --git a/src/bin/scripts/droplang.c b/src/bin/scripts/droplang.c index de203173438..b9664a91850 100644 --- a/src/bin/scripts/droplang.c +++ b/src/bin/scripts/droplang.c @@ -126,7 +126,7 @@ main(int argc, char *argv[]) else if (getenv("PGUSER")) dbname = getenv("PGUSER"); else - dbname = get_user_name(progname); + dbname = get_user_name_or_exit(progname); } initPQExpBuffer(&sql); diff --git a/src/bin/scripts/reindexdb.c b/src/bin/scripts/reindexdb.c index f7c09bebf8a..4e762ea56f4 100644 --- a/src/bin/scripts/reindexdb.c +++ b/src/bin/scripts/reindexdb.c @@ -188,7 +188,7 @@ main(int argc, char *argv[]) else if (getenv("PGUSER")) dbname = getenv("PGUSER"); else - dbname = get_user_name(progname); + dbname = get_user_name_or_exit(progname); } reindex_system_catalogs(dbname, host, port, username, prompt_password, @@ -203,7 +203,7 @@ main(int argc, char *argv[]) else if (getenv("PGUSER")) dbname = getenv("PGUSER"); else - dbname = get_user_name(progname); + dbname = get_user_name_or_exit(progname); } if (indexes.head != NULL) diff --git a/src/bin/scripts/vacuumdb.c b/src/bin/scripts/vacuumdb.c index 616d9339e1e..8970ec6ad2e 100644 --- a/src/bin/scripts/vacuumdb.c +++ b/src/bin/scripts/vacuumdb.c @@ -202,7 +202,7 @@ main(int argc, char *argv[]) else if (getenv("PGUSER")) dbname = getenv("PGUSER"); else - dbname = get_user_name(progname); + dbname = get_user_name_or_exit(progname); } if (tables.head != NULL) |