pg.ddx.io pgsql-bugs@postgresql.org mailing list archive
help / color / mirror / Atom feedBUG #8139: initdb: Misleading error message when current user not in /etc/passwd
24+ messages / 6 participants
[nested] [flat]
* BUG #8139: initdb: Misleading error message when current user not in /etc/passwd
@ 2013-05-07 15:42 nicolas@marchildon.net
0 siblings, 1 reply; 24+ messages in thread
From: nicolas@marchildon.net @ 2013-05-07 15:42 UTC (permalink / raw)
To: pgsql-bugs
The following bug has been logged on the website:
Bug reference: 8139
Logged by: Nicolas Marchildon
Email address: nicolas@marchildon.net
PostgreSQL version: 9.2.4
Operating system: RHEL 6
Description:
Running initdb while logged in as a user that has no entry in /etc/passwd,
which happens when authenticating with Kerberos, and missing sssd-client
prints a misleading error message:
"initdb: could not obtain information about current user: Success"
The misleading part is the "Success". This comes from errno:
pw = getpwuid(geteuid());
if (!pw)
{
fprintf(stderr,
_("%s: could not obtain information about current
user: %s\n"),
progname, strerror(errno));
exit(1);
}
The man page says:
RETURN VALUE
The getpwnam() and getpwuid() functions return a pointer to a
passwd
structure, or NULL if the matching entry is not found or an
error
occurs. If an error occurs, errno is set appropriately. If one
wants
to check errno after the call, it should be set to zero before
the
call.
First, initdb's get_id function does not set errno to zero, which is a bug.
Second, when the return value is NULL, it should only print strerror(errno)
when errno != 0.
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
^ permalink raw reply [nested|flat] 24+ messages in thread
* Re: BUG #8139: initdb: Misleading error message when current user not in /etc/passwd
@ 2013-05-07 18:10 Tom Lane <tgl@sss.pgh.pa.us>
parent: nicolas@marchildon.net
0 siblings, 1 reply; 24+ messages in thread
From: Tom Lane @ 2013-05-07 18:10 UTC (permalink / raw)
To: nicolas@marchildon.net; +Cc: pgsql-bugs
nicolas@marchildon.net writes:
> "initdb: could not obtain information about current user: Success"
> The misleading part is the "Success". This comes from errno:
> pw = getpwuid(geteuid());
> if (!pw)
> {
> fprintf(stderr,
> _("%s: could not obtain information about current
> user: %s\n"),
> progname, strerror(errno));
> exit(1);
> }
> The man page says:
> RETURN VALUE
> The getpwnam() and getpwuid() functions return a pointer to a
> passwd
> structure, or NULL if the matching entry is not found or an
> error
> occurs. If an error occurs, errno is set appropriately. If one
> wants
> to check errno after the call, it should be set to zero before
> the
> call.
AFAICS, getpwuid is not honoring its specification here: it failed to
set errno. I don't see that suppressing the strerror result would add
anything much.
regards, tom lane
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
^ permalink raw reply [nested|flat] 24+ messages in thread
* Re: BUG #8139: initdb: Misleading error message when current user not in /etc/passwd
@ 2013-05-07 18:19 Alvaro Herrera <alvherre@2ndquadrant.com>
parent: Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 1 reply; 24+ messages in thread
From: Alvaro Herrera @ 2013-05-07 18:19 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: nicolas@marchildon.net; pgsql-bugs
Tom Lane wrote:
> nicolas@marchildon.net writes:
> > The man page says:
>
> > RETURN VALUE
> > The getpwnam() and getpwuid() functions return a pointer to a
> > passwd
> > structure, or NULL if the matching entry is not found or an
> > error
> > occurs. If an error occurs, errno is set appropriately. If one
> > wants
> > to check errno after the call, it should be set to zero before
> > the
> > call.
>
> AFAICS, getpwuid is not honoring its specification here: it failed to
> set errno. I don't see that suppressing the strerror result would add
> anything much.
Well, in this case no error occured, but no matching entry was found.
The wording in the manpage is explicit that there not being an entry is
not an error.
--
Álvaro Herrera http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
^ permalink raw reply [nested|flat] 24+ messages in thread
* Re: BUG #8139: initdb: Misleading error message when current user not in /etc/passwd
@ 2013-12-04 15:52 Bruce Momjian <bruce@momjian.us>
parent: Alvaro Herrera <alvherre@2ndquadrant.com>
0 siblings, 2 replies; 24+ messages in thread
From: Bruce Momjian @ 2013-12-04 15:52 UTC (permalink / raw)
To: Alvaro Herrera <alvherre@2ndquadrant.com>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; nicolas@marchildon.net; pgsql-bugs
On Tue, May 7, 2013 at 02:19:05PM -0400, Alvaro Herrera wrote:
> Tom Lane wrote:
> > nicolas@marchildon.net writes:
>
> > > The man page says:
> >
> > > RETURN VALUE
> > > The getpwnam() and getpwuid() functions return a pointer to a
> > > passwd
> > > structure, or NULL if the matching entry is not found or an
> > > error
> > > occurs. If an error occurs, errno is set appropriately. If one
> > > wants
> > > to check errno after the call, it should be set to zero before
> > > the
> > > call.
> >
> > AFAICS, getpwuid is not honoring its specification here: it failed to
> > set errno. I don't see that suppressing the strerror result would add
> > anything much.
>
> Well, in this case no error occured, but no matching entry was found.
> The wording in the manpage is explicit that there not being an entry is
> not an error.
I have developed the attached patch to fix this and another instance I
saw.
--
Bruce Momjian <bruce@momjian.us> http://momjian.us
EnterpriseDB http://enterprisedb.com
+ Everyone has their own god. +
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
Attachments:
passwd.diff (text/x-diff, 1.1K) download
diff --git a/contrib/pg_upgrade/util.c b/contrib/pg_upgrade/util.c
new file mode 100644
index a67bd64..9ad4220
*** a/contrib/pg_upgrade/util.c
--- b/contrib/pg_upgrade/util.c
*************** get_user_info(char **user_name)
*** 228,234 ****
user_id = 1;
#endif
! *user_name = pg_strdup(pw->pw_name);
return user_id;
}
--- 228,237 ----
user_id = 1;
#endif
! if (pw)
! *user_name = pg_strdup(pw->pw_name);
! else
! *user_name = NULL;
return user_id;
}
diff --git a/src/bin/initdb/initdb.c b/src/bin/initdb/initdb.c
new file mode 100644
index 30e3701..8665595
*** a/src/bin/initdb/initdb.c
--- b/src/bin/initdb/initdb.c
*************** get_id(void)
*** 795,801 ****
{
fprintf(stderr,
_("%s: could not obtain information about current user: %s\n"),
! progname, strerror(errno));
exit(1);
}
#else /* the windows code */
--- 795,801 ----
{
fprintf(stderr,
_("%s: could not obtain information about current user: %s\n"),
! progname, errno ? strerror(errno) : "not an operating system user");
exit(1);
}
#else /* the windows code */
^ permalink raw reply [nested|flat] 24+ messages in thread
* Re: BUG #8139: initdb: Misleading error message when current user not in /etc/passwd
@ 2013-12-04 19:03 Alvaro Herrera <alvherre@2ndquadrant.com>
parent: Bruce Momjian <bruce@momjian.us>
1 sibling, 1 reply; 24+ messages in thread
From: Alvaro Herrera @ 2013-12-04 19:03 UTC (permalink / raw)
To: Bruce Momjian <bruce@momjian.us>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; nicolas@marchildon.net; pgsql-bugs
Bruce Momjian wrote:
> On Tue, May 7, 2013 at 02:19:05PM -0400, Alvaro Herrera wrote:
> I have developed the attached patch to fix this and another instance I
> saw.
Remember to set errno = 0 before calling the getpw* function; at least
in initdb it would be meaningful. And in the pg_upgrade case, it seems
better to abort the upgrade if this call doesn't work. (Also, there are
other uses of getpwuid/getpwnam elsewhere. Not sure we want to worry
too much about them.)
--
Álvaro Herrera http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
^ permalink raw reply [nested|flat] 24+ messages in thread
* Re: BUG #8139: initdb: Misleading error message when current user not in /etc/passwd
@ 2013-12-04 19:34 Tom Lane <tgl@sss.pgh.pa.us>
parent: Bruce Momjian <bruce@momjian.us>
1 sibling, 0 replies; 24+ messages in thread
From: Tom Lane @ 2013-12-04 19:34 UTC (permalink / raw)
To: Bruce Momjian <bruce@momjian.us>; +Cc: Alvaro Herrera <alvherre@2ndquadrant.com>; nicolas@marchildon.net; pgsql-bugs
Bruce Momjian <bruce@momjian.us> writes:
> I have developed the attached patch to fix this and another instance I
> saw.
I think you need to re-read the part of the man page you quoted,
and set errno to zero beforehand, if you're going to rely on it
being zero after a non-error return.
regards, tom lane
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
^ permalink raw reply [nested|flat] 24+ messages in thread
* Re: BUG #8139: initdb: Misleading error message when current user not in /etc/passwd
@ 2013-12-09 21:05 Bruce Momjian <bruce@momjian.us>
parent: Alvaro Herrera <alvherre@2ndquadrant.com>
0 siblings, 2 replies; 24+ messages in thread
From: Bruce Momjian @ 2013-12-09 21:05 UTC (permalink / raw)
To: Alvaro Herrera <alvherre@2ndquadrant.com>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; nicolas@marchildon.net; pgsql-bugs
On Wed, Dec 4, 2013 at 04:03:39PM -0300, Alvaro Herrera wrote:
> Bruce Momjian wrote:
> > On Tue, May 7, 2013 at 02:19:05PM -0400, Alvaro Herrera wrote:
>
> > I have developed the attached patch to fix this and another instance I
> > saw.
>
> Remember to set errno = 0 before calling the getpw* function; at least
> in initdb it would be meaningful. And in the pg_upgrade case, it seems
> better to abort the upgrade if this call doesn't work. (Also, there are
> other uses of getpwuid/getpwnam elsewhere. Not sure we want to worry
> too much about them.)
Wow, that is an odd API. Once I started to look at all the errno
clearing necessary, I realized that the function in bin/scripts/common.c
could be moved to src/port and generalized to reduce code duplication.
Updated patch attached, with centralized checking for errno and more
consistent behavior for username lookups.
--
Bruce Momjian <bruce@momjian.us> http://momjian.us
EnterpriseDB http://enterprisedb.com
+ Everyone has their own god. +
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
Attachments:
passwd.diff (text/x-diff, 18.8K) download
diff --git a/contrib/pg_upgrade/util.c b/contrib/pg_upgrade/util.c
new file mode 100644
index a67bd64..c3d4523
*** a/contrib/pg_upgrade/util.c
--- b/contrib/pg_upgrade/util.c
*************** quote_identifier(const char *s)
*** 203,234 ****
/*
* get_user_info()
- * (copied from initdb.c) find the current user
*/
int
get_user_info(char **user_name)
{
int user_id;
#ifndef WIN32
- struct passwd *pw = getpwuid(geteuid());
-
user_id = geteuid();
! #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;
!
! GetUserName(pw->pw_name, &pwname_size);
!
user_id = 1;
#endif
! *user_name = pg_strdup(pw->pw_name);
return user_id;
}
--- 203,227 ----
/*
* get_user_info()
*/
int
get_user_info(char **user_name)
{
int user_id;
+ char *errstr;
#ifndef WIN32
user_id = geteuid();
! #else
user_id = 1;
#endif
! *user_name = get_user_name(&errstr);
! if (!*user_name)
! pg_fatal("%s\n", errstr);
!
! /* make a copy */
! *user_name = pg_strdup(*user_name);
return user_id;
}
diff --git a/src/backend/libpq/auth.c b/src/backend/libpq/auth.c
new file mode 100644
index 2dbf7e5..3c499fe
*** a/src/backend/libpq/auth.c
--- b/src/backend/libpq/auth.c
*************** auth_peer(hbaPort *port)
*** 1771,1777 ****
char ident_user[IDENT_USERNAME_MAX + 1];
uid_t uid;
gid_t gid;
! struct passwd *pass;
errno = 0;
if (getpeereid(port->sock, &uid, &gid) != 0)
--- 1771,1778 ----
char ident_user[IDENT_USERNAME_MAX + 1];
uid_t uid;
gid_t gid;
! const char *user_name;
! char *errstr;
errno = 0;
if (getpeereid(port->sock, &uid, &gid) != 0)
*************** auth_peer(hbaPort *port)
*** 1788,1804 ****
return STATUS_ERROR;
}
! pass = getpwuid(uid);
!
! if (pass == NULL)
{
! ereport(LOG,
! (errmsg("local user with ID %d does not exist",
! (int) uid)));
return STATUS_ERROR;
}
! strlcpy(ident_user, pass->pw_name, IDENT_USERNAME_MAX + 1);
return check_usermap(port->hba->usermap, port->user_name, ident_user, false);
}
--- 1789,1803 ----
return STATUS_ERROR;
}
! user_name = get_user_name(&errstr);
! if (!user_name)
{
! ereport(LOG, (errmsg("%s\n", errstr)));
! pfree(errstr);
return STATUS_ERROR;
}
! 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
new file mode 100644
index d71885d..376aa39
*** a/src/backend/main/main.c
--- b/src/backend/main/main.c
***************
*** 20,26 ****
*/
#include "postgres.h"
- #include <pwd.h>
#include <unistd.h>
#if defined(__alpha) && defined(__osf__) /* no __alpha__ ? */
--- 20,25 ----
*************** const char *progname;
*** 49,55 ****
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);
/*
--- 48,53 ----
*************** main(int argc, char *argv[])
*** 191,197 ****
else if (argc > 1 && strcmp(argv[1], "--single") == 0)
PostgresMain(argc, argv,
NULL, /* no dbname */
! get_current_username(progname)); /* does not return */
else
PostmasterMain(argc, argv); /* does not return */
abort(); /* should not get here */
--- 189,195 ----
else if (argc > 1 && strcmp(argv[1], "--single") == 0)
PostgresMain(argc, argv,
NULL, /* no dbname */
! strdup(get_user_name_or_exit(progname))); /* does not return */
else
PostmasterMain(argc, argv); /* does not return */
abort(); /* should not get here */
*************** check_root(const char *progname)
*** 372,407 ****
}
#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
- }
--- 370,372 ----
diff --git a/src/bin/initdb/initdb.c b/src/bin/initdb/initdb.c
new file mode 100644
index 30e3701..65d6b3f
*** a/src/bin/initdb/initdb.c
--- b/src/bin/initdb/initdb.c
*************** exit_nicely(void)
*** 770,784 ****
/*
* find the current user
*
! * on unix make sure it isn't really root
*/
static char *
get_id(void)
{
! #ifndef WIN32
!
! struct passwd *pw;
if (geteuid() == 0) /* 0 is root's uid */
{
fprintf(stderr,
--- 770,783 ----
/*
* find the current user
*
! * on unix make sure it isn't root
*/
static char *
get_id(void)
{
! const char *username;
+ #ifndef WIN32
if (geteuid() == 0) /* 0 is root's uid */
{
fprintf(stderr,
*************** get_id(void)
*** 789,823 ****
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);
}
static char *
--- 788,798 ----
progname);
exit(1);
}
#endif
! 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
new file mode 100644
index 638d8cb..d60a661
*** a/src/bin/psql/command.c
--- b/src/bin/psql/command.c
*************** exec_command(const char *cmd,
*** 265,274 ****
#ifndef WIN32
struct passwd *pw;
pw = getpwuid(geteuid());
if (!pw)
{
! psql_error("could not get home directory: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
dir = pw->pw_dir;
--- 265,277 ----
#ifndef WIN32
struct passwd *pw;
+ errno = 0; /* clear errno before call */
pw = getpwuid(geteuid());
if (!pw)
{
! 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
new file mode 100644
index 30530f2..80b1ec0
*** a/src/bin/psql/help.c
--- b/src/bin/psql/help.c
***************
*** 8,16 ****
#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
--- 8,13 ----
*************** usage(void)
*** 52,82 ****
{
const char *env;
const char *user;
!
! #ifndef WIN32
! struct passwd *pw = NULL;
! #endif
/* 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
{
! psql_error("could not get current user name: %s\n", strerror(errno));
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"));
--- 49,66 ----
{
const char *env;
const char *user;
! char *errstr;
/* Find default user, in case we need it. */
user = getenv("PGUSER");
if (!user)
{
! user = get_user_name(&errstr);
! if (!user)
{
! psql_error("%s\n", errstr);
exit(EXIT_FAILURE);
}
}
printf(_("psql is the PostgreSQL interactive terminal.\n\n"));
diff --git a/src/bin/scripts/clusterdb.c b/src/bin/scripts/clusterdb.c
new file mode 100644
index cd54e8f..e7065ce
*** a/src/bin/scripts/clusterdb.c
--- b/src/bin/scripts/clusterdb.c
*************** main(int argc, char *argv[])
*** 160,166 ****
else if (getenv("PGUSER"))
dbname = getenv("PGUSER");
else
! dbname = get_user_name(progname);
}
if (tables.head != NULL)
--- 160,166 ----
else if (getenv("PGUSER"))
dbname = getenv("PGUSER");
else
! 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
new file mode 100644
index 4645bc1..e57e520
*** a/src/bin/scripts/common.c
--- b/src/bin/scripts/common.c
***************
*** 14,20 ****
#include "postgres_fe.h"
- #include <pwd.h>
#include <signal.h>
#include <unistd.h>
--- 14,19 ----
*************** static CRITICAL_SECTION cancelConnLock;
*** 30,67 ****
#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.
*/
--- 29,34 ----
diff --git a/src/bin/scripts/common.h b/src/bin/scripts/common.h
new file mode 100644
index 6cf490f..a8a81f6
*** a/src/bin/scripts/common.h
--- b/src/bin/scripts/common.h
*************** enum trivalue
*** 22,29 ****
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);
--- 22,27 ----
diff --git a/src/bin/scripts/createdb.c b/src/bin/scripts/createdb.c
new file mode 100644
index 14cd128..856a04d
*** a/src/bin/scripts/createdb.c
--- b/src/bin/scripts/createdb.c
*************** main(int argc, char *argv[])
*** 174,180 ****
else if (getenv("PGUSER"))
dbname = getenv("PGUSER");
else
! dbname = get_user_name(progname);
}
initPQExpBuffer(&sql);
--- 174,180 ----
else if (getenv("PGUSER"))
dbname = getenv("PGUSER");
else
! dbname = get_user_name_or_exit(progname);
}
initPQExpBuffer(&sql);
diff --git a/src/bin/scripts/createlang.c b/src/bin/scripts/createlang.c
new file mode 100644
index ff544a8..5cfba8e
*** a/src/bin/scripts/createlang.c
--- b/src/bin/scripts/createlang.c
*************** main(int argc, char *argv[])
*** 127,133 ****
else if (getenv("PGUSER"))
dbname = getenv("PGUSER");
else
! dbname = get_user_name(progname);
}
initPQExpBuffer(&sql);
--- 127,133 ----
else if (getenv("PGUSER"))
dbname = getenv("PGUSER");
else
! dbname = get_user_name_or_exit(progname);
}
initPQExpBuffer(&sql);
diff --git a/src/bin/scripts/createuser.c b/src/bin/scripts/createuser.c
new file mode 100644
index 83623ea..e4e6e64
*** a/src/bin/scripts/createuser.c
--- b/src/bin/scripts/createuser.c
*************** main(int argc, char *argv[])
*** 188,194 ****
if (getenv("PGUSER"))
newuser = getenv("PGUSER");
else
! newuser = get_user_name(progname);
}
}
--- 188,194 ----
if (getenv("PGUSER"))
newuser = getenv("PGUSER");
else
! newuser = get_user_name_or_exit(progname);
}
}
diff --git a/src/bin/scripts/droplang.c b/src/bin/scripts/droplang.c
new file mode 100644
index de20317..b9664a9
*** a/src/bin/scripts/droplang.c
--- b/src/bin/scripts/droplang.c
*************** main(int argc, char *argv[])
*** 126,132 ****
else if (getenv("PGUSER"))
dbname = getenv("PGUSER");
else
! dbname = get_user_name(progname);
}
initPQExpBuffer(&sql);
--- 126,132 ----
else if (getenv("PGUSER"))
dbname = getenv("PGUSER");
else
! dbname = get_user_name_or_exit(progname);
}
initPQExpBuffer(&sql);
diff --git a/src/bin/scripts/reindexdb.c b/src/bin/scripts/reindexdb.c
new file mode 100644
index f7c09be..4e762ea
*** a/src/bin/scripts/reindexdb.c
--- b/src/bin/scripts/reindexdb.c
*************** main(int argc, char *argv[])
*** 188,194 ****
else if (getenv("PGUSER"))
dbname = getenv("PGUSER");
else
! dbname = get_user_name(progname);
}
reindex_system_catalogs(dbname, host, port, username, prompt_password,
--- 188,194 ----
else if (getenv("PGUSER"))
dbname = getenv("PGUSER");
else
! dbname = get_user_name_or_exit(progname);
}
reindex_system_catalogs(dbname, host, port, username, prompt_password,
*************** main(int argc, char *argv[])
*** 203,209 ****
else if (getenv("PGUSER"))
dbname = getenv("PGUSER");
else
! dbname = get_user_name(progname);
}
if (indexes.head != NULL)
--- 203,209 ----
else if (getenv("PGUSER"))
dbname = getenv("PGUSER");
else
! 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
new file mode 100644
index 616d933..8970ec6
*** a/src/bin/scripts/vacuumdb.c
--- b/src/bin/scripts/vacuumdb.c
*************** main(int argc, char *argv[])
*** 202,208 ****
else if (getenv("PGUSER"))
dbname = getenv("PGUSER");
else
! dbname = get_user_name(progname);
}
if (tables.head != NULL)
--- 202,208 ----
else if (getenv("PGUSER"))
dbname = getenv("PGUSER");
else
! dbname = get_user_name_or_exit(progname);
}
if (tables.head != NULL)
diff --git a/src/include/port.h b/src/include/port.h
new file mode 100644
index 5ef4b0a..473c4f6
*** a/src/include/port.h
--- b/src/include/port.h
*************** extern pqsigfunc pqsignal(int signo, pqs
*** 473,478 ****
--- 473,482 ----
/* port/quotes.c */
extern char *escape_single_quotes_ascii(const char *src);
+ /* port/username.c */
+ extern const char *get_user_name(char **errstr);
+ extern const char *get_user_name_or_exit(const char *progname);
+
/* port/wait_error.c */
extern char *wait_result_to_str(int exit_status);
diff --git a/src/port/Makefile b/src/port/Makefile
new file mode 100644
index 1be4ff5..a50e0af
*** a/src/port/Makefile
--- b/src/port/Makefile
*************** LIBS += $(PTHREAD_LIBS)
*** 33,39 ****
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
# foo_srv.o and foo.o are both built from foo.c, but only foo.o has -DFRONTEND
OBJS_SRV = $(OBJS:%.o=%_srv.o)
--- 33,39 ----
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 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)
diff --git a/src/port/username.c b/src/port/username.c
new file mode 100644
index ...272bb0e
*** a/src/port/username.c
--- b/src/port/username.c
***************
*** 0 ****
--- 1,83 ----
+ /*-------------------------------------------------------------------------
+ *
+ * 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;
+
+ *errstr = NULL;
+
+ errno = 0; /* clear errno before call */
+ pw = getpwuid(geteuid());
+ if (!pw)
+ {
+ *errstr = psprintf(_("effective user id %d lookup failure: %s"),
+ (int) geteuid(), 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;
+ }
diff --git a/src/tools/msvc/Mkvcbuild.pm b/src/tools/msvc/Mkvcbuild.pm
new file mode 100644
index bc7f449..d6b6eaf
*** a/src/tools/msvc/Mkvcbuild.pm
--- b/src/tools/msvc/Mkvcbuild.pm
*************** sub mkvcbuild
*** 70,77 ****
erand48.c snprintf.c strlcat.c strlcpy.c dirmod.c noblock.c path.c
pgcheckdir.c pg_crc.c pgmkdirp.c pgsleep.c pgstrcasecmp.c pqsignal.c
qsort.c qsort_arg.c quotes.c
! sprompt.c tar.c thread.c getopt.c getopt_long.c dirent.c rint.c win32env.c
! win32error.c win32setlocale.c);
our @pgcommonallfiles = qw(
exec.c pgfnames.c psprintf.c relpath.c rmtree.c wait_error.c);
--- 70,77 ----
erand48.c snprintf.c strlcat.c strlcpy.c dirmod.c noblock.c path.c
pgcheckdir.c pg_crc.c pgmkdirp.c pgsleep.c pgstrcasecmp.c pqsignal.c
qsort.c qsort_arg.c quotes.c
! sprompt.c tar.c thread.c getopt.c getopt_long.c dirent.c rint.c username.c
! win32env.c win32error.c win32setlocale.c);
our @pgcommonallfiles = qw(
exec.c pgfnames.c psprintf.c relpath.c rmtree.c wait_error.c);
^ permalink raw reply [nested|flat] 24+ messages in thread
* Re: BUG #8139: initdb: Misleading error message when current user not in /etc/passwd
@ 2013-12-09 21:35 Tom Lane <tgl@sss.pgh.pa.us>
parent: Bruce Momjian <bruce@momjian.us>
1 sibling, 1 reply; 24+ messages in thread
From: Tom Lane @ 2013-12-09 21:35 UTC (permalink / raw)
To: Bruce Momjian <bruce@momjian.us>; +Cc: Alvaro Herrera <alvherre@2ndquadrant.com>; nicolas@marchildon.net; pgsql-bugs
Bruce Momjian <bruce@momjian.us> writes:
> Updated patch attached, with centralized checking for errno and more
> consistent behavior for username lookups.
This bit is not good:
+ errno = 0; /* clear errno before call */
+ pw = getpwuid(geteuid());
+ if (!pw)
+ {
+ *errstr = psprintf(_("effective user id %d lookup failure: %s"),
+ (int) geteuid(), errno ? strerror(errno) :
+ _("user does not exist"));
The second call of geteuid() could clobber errno before strerror can see it.
You should compute geteuid() just once and keep it in a variable.
Also, I don't think that error message meets our style guidelines.
Maybe "failed to look up user id %d: ..." ?
regards, tom lane
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
^ permalink raw reply [nested|flat] 24+ messages in thread
* Re: BUG #8139: initdb: Misleading error message when current user not in /etc/passwd
@ 2013-12-09 21:45 Alvaro Herrera <alvherre@2ndquadrant.com>
parent: Bruce Momjian <bruce@momjian.us>
1 sibling, 1 reply; 24+ messages in thread
From: Alvaro Herrera @ 2013-12-09 21:45 UTC (permalink / raw)
To: Bruce Momjian <bruce@momjian.us>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; nicolas@marchildon.net; pgsql-bugs
Bruce Momjian wrote:
> return STATUS_ERROR;
> }
>
> ! user_name = get_user_name(&errstr);
> ! if (!user_name)
> {
> ! ereport(LOG, (errmsg("%s\n", errstr)));
> ! pfree(errstr);
> return STATUS_ERROR;
> }
The message is already translated by get_user_name, so I think this
should use errmsg_internal() instead of errmsg(). Also, why do you add
a newline?
Not clear whether the new file should be in src/port or src/common.
--
Álvaro Herrera http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
^ permalink raw reply [nested|flat] 24+ messages in thread
* Re: BUG #8139: initdb: Misleading error message when current user not in /etc/passwd
@ 2013-12-10 00:46 Bruce Momjian <bruce@momjian.us>
parent: Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 0 replies; 24+ messages in thread
From: Bruce Momjian @ 2013-12-10 00:46 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Alvaro Herrera <alvherre@2ndquadrant.com>; nicolas@marchildon.net; pgsql-bugs
On Mon, Dec 9, 2013 at 04:35:56PM -0500, Tom Lane wrote:
> Bruce Momjian <bruce@momjian.us> writes:
> > Updated patch attached, with centralized checking for errno and more
> > consistent behavior for username lookups.
>
> This bit is not good:
>
> + errno = 0; /* clear errno before call */
> + pw = getpwuid(geteuid());
> + if (!pw)
> + {
> + *errstr = psprintf(_("effective user id %d lookup failure: %s"),
> + (int) geteuid(), errno ? strerror(errno) :
> + _("user does not exist"));
>
> The second call of geteuid() could clobber errno before strerror can see it.
> You should compute geteuid() just once and keep it in a variable.
Good point --- fixed.
> Also, I don't think that error message meets our style guidelines.
> Maybe "failed to look up user id %d: ..." ?
OK, updated.
Updated patch attached to Alvaro's email reply.
--
Bruce Momjian <bruce@momjian.us> http://momjian.us
EnterpriseDB http://enterprisedb.com
+ Everyone has their own god. +
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
^ permalink raw reply [nested|flat] 24+ messages in thread
* Re: BUG #8139: initdb: Misleading error message when current user not in /etc/passwd
@ 2013-12-10 00:47 Bruce Momjian <bruce@momjian.us>
parent: Alvaro Herrera <alvherre@2ndquadrant.com>
0 siblings, 2 replies; 24+ messages in thread
From: Bruce Momjian @ 2013-12-10 00:47 UTC (permalink / raw)
To: Alvaro Herrera <alvherre@2ndquadrant.com>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; nicolas@marchildon.net; pgsql-bugs
On Mon, Dec 9, 2013 at 06:45:39PM -0300, Alvaro Herrera wrote:
> Bruce Momjian wrote:
>
> > return STATUS_ERROR;
> > }
> >
> > ! user_name = get_user_name(&errstr);
> > ! if (!user_name)
> > {
> > ! ereport(LOG, (errmsg("%s\n", errstr)));
> > ! pfree(errstr);
> > return STATUS_ERROR;
> > }
>
> The message is already translated by get_user_name, so I think this
> should use errmsg_internal() instead of errmsg(). Also, why do you add
> a newline?
OK, done.
> Not clear whether the new file should be in src/port or src/common.
Agreed. It isn't designed to add missing OS functionality, but it is
mostly OS-specific code.
Updated patch attached, with Tom's requested changes.
--
Bruce Momjian <bruce@momjian.us> http://momjian.us
EnterpriseDB http://enterprisedb.com
+ Everyone has their own god. +
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
Attachments:
passwd.diff (text/x-diff, 18.8K) download
diff --git a/contrib/pg_upgrade/util.c b/contrib/pg_upgrade/util.c
new file mode 100644
index a67bd64..c3d4523
*** a/contrib/pg_upgrade/util.c
--- b/contrib/pg_upgrade/util.c
*************** quote_identifier(const char *s)
*** 203,234 ****
/*
* get_user_info()
- * (copied from initdb.c) find the current user
*/
int
get_user_info(char **user_name)
{
int user_id;
#ifndef WIN32
- struct passwd *pw = getpwuid(geteuid());
-
user_id = geteuid();
! #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;
!
! GetUserName(pw->pw_name, &pwname_size);
!
user_id = 1;
#endif
! *user_name = pg_strdup(pw->pw_name);
return user_id;
}
--- 203,227 ----
/*
* get_user_info()
*/
int
get_user_info(char **user_name)
{
int user_id;
+ char *errstr;
#ifndef WIN32
user_id = geteuid();
! #else
user_id = 1;
#endif
! *user_name = get_user_name(&errstr);
! if (!*user_name)
! pg_fatal("%s\n", errstr);
!
! /* make a copy */
! *user_name = pg_strdup(*user_name);
return user_id;
}
diff --git a/src/backend/libpq/auth.c b/src/backend/libpq/auth.c
new file mode 100644
index 2dbf7e5..6d11e57
*** a/src/backend/libpq/auth.c
--- b/src/backend/libpq/auth.c
*************** auth_peer(hbaPort *port)
*** 1771,1777 ****
char ident_user[IDENT_USERNAME_MAX + 1];
uid_t uid;
gid_t gid;
! struct passwd *pass;
errno = 0;
if (getpeereid(port->sock, &uid, &gid) != 0)
--- 1771,1778 ----
char ident_user[IDENT_USERNAME_MAX + 1];
uid_t uid;
gid_t gid;
! const char *user_name;
! char *errstr;
errno = 0;
if (getpeereid(port->sock, &uid, &gid) != 0)
*************** auth_peer(hbaPort *port)
*** 1788,1804 ****
return STATUS_ERROR;
}
! pass = getpwuid(uid);
!
! if (pass == NULL)
{
! ereport(LOG,
! (errmsg("local user with ID %d does not exist",
! (int) uid)));
return STATUS_ERROR;
}
! strlcpy(ident_user, pass->pw_name, IDENT_USERNAME_MAX + 1);
return check_usermap(port->hba->usermap, port->user_name, ident_user, false);
}
--- 1789,1803 ----
return STATUS_ERROR;
}
! user_name = get_user_name(&errstr);
! if (!user_name)
{
! ereport(LOG, (errmsg_internal("%s", errstr)));
! pfree(errstr);
return STATUS_ERROR;
}
! 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
new file mode 100644
index d71885d..376aa39
*** a/src/backend/main/main.c
--- b/src/backend/main/main.c
***************
*** 20,26 ****
*/
#include "postgres.h"
- #include <pwd.h>
#include <unistd.h>
#if defined(__alpha) && defined(__osf__) /* no __alpha__ ? */
--- 20,25 ----
*************** const char *progname;
*** 49,55 ****
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);
/*
--- 48,53 ----
*************** main(int argc, char *argv[])
*** 191,197 ****
else if (argc > 1 && strcmp(argv[1], "--single") == 0)
PostgresMain(argc, argv,
NULL, /* no dbname */
! get_current_username(progname)); /* does not return */
else
PostmasterMain(argc, argv); /* does not return */
abort(); /* should not get here */
--- 189,195 ----
else if (argc > 1 && strcmp(argv[1], "--single") == 0)
PostgresMain(argc, argv,
NULL, /* no dbname */
! strdup(get_user_name_or_exit(progname))); /* does not return */
else
PostmasterMain(argc, argv); /* does not return */
abort(); /* should not get here */
*************** check_root(const char *progname)
*** 372,407 ****
}
#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
- }
--- 370,372 ----
diff --git a/src/bin/initdb/initdb.c b/src/bin/initdb/initdb.c
new file mode 100644
index 30e3701..65d6b3f
*** a/src/bin/initdb/initdb.c
--- b/src/bin/initdb/initdb.c
*************** exit_nicely(void)
*** 770,784 ****
/*
* find the current user
*
! * on unix make sure it isn't really root
*/
static char *
get_id(void)
{
! #ifndef WIN32
!
! struct passwd *pw;
if (geteuid() == 0) /* 0 is root's uid */
{
fprintf(stderr,
--- 770,783 ----
/*
* find the current user
*
! * on unix make sure it isn't root
*/
static char *
get_id(void)
{
! const char *username;
+ #ifndef WIN32
if (geteuid() == 0) /* 0 is root's uid */
{
fprintf(stderr,
*************** get_id(void)
*** 789,823 ****
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);
}
static char *
--- 788,798 ----
progname);
exit(1);
}
#endif
! 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
new file mode 100644
index 638d8cb..d60a661
*** a/src/bin/psql/command.c
--- b/src/bin/psql/command.c
*************** exec_command(const char *cmd,
*** 265,274 ****
#ifndef WIN32
struct passwd *pw;
pw = getpwuid(geteuid());
if (!pw)
{
! psql_error("could not get home directory: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
dir = pw->pw_dir;
--- 265,277 ----
#ifndef WIN32
struct passwd *pw;
+ errno = 0; /* clear errno before call */
pw = getpwuid(geteuid());
if (!pw)
{
! 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
new file mode 100644
index 30530f2..80b1ec0
*** a/src/bin/psql/help.c
--- b/src/bin/psql/help.c
***************
*** 8,16 ****
#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
--- 8,13 ----
*************** usage(void)
*** 52,82 ****
{
const char *env;
const char *user;
!
! #ifndef WIN32
! struct passwd *pw = NULL;
! #endif
/* 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
{
! psql_error("could not get current user name: %s\n", strerror(errno));
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"));
--- 49,66 ----
{
const char *env;
const char *user;
! char *errstr;
/* Find default user, in case we need it. */
user = getenv("PGUSER");
if (!user)
{
! user = get_user_name(&errstr);
! if (!user)
{
! psql_error("%s\n", errstr);
exit(EXIT_FAILURE);
}
}
printf(_("psql is the PostgreSQL interactive terminal.\n\n"));
diff --git a/src/bin/scripts/clusterdb.c b/src/bin/scripts/clusterdb.c
new file mode 100644
index cd54e8f..e7065ce
*** a/src/bin/scripts/clusterdb.c
--- b/src/bin/scripts/clusterdb.c
*************** main(int argc, char *argv[])
*** 160,166 ****
else if (getenv("PGUSER"))
dbname = getenv("PGUSER");
else
! dbname = get_user_name(progname);
}
if (tables.head != NULL)
--- 160,166 ----
else if (getenv("PGUSER"))
dbname = getenv("PGUSER");
else
! 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
new file mode 100644
index 4645bc1..e57e520
*** a/src/bin/scripts/common.c
--- b/src/bin/scripts/common.c
***************
*** 14,20 ****
#include "postgres_fe.h"
- #include <pwd.h>
#include <signal.h>
#include <unistd.h>
--- 14,19 ----
*************** static CRITICAL_SECTION cancelConnLock;
*** 30,67 ****
#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.
*/
--- 29,34 ----
diff --git a/src/bin/scripts/common.h b/src/bin/scripts/common.h
new file mode 100644
index 6cf490f..a8a81f6
*** a/src/bin/scripts/common.h
--- b/src/bin/scripts/common.h
*************** enum trivalue
*** 22,29 ****
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);
--- 22,27 ----
diff --git a/src/bin/scripts/createdb.c b/src/bin/scripts/createdb.c
new file mode 100644
index 14cd128..856a04d
*** a/src/bin/scripts/createdb.c
--- b/src/bin/scripts/createdb.c
*************** main(int argc, char *argv[])
*** 174,180 ****
else if (getenv("PGUSER"))
dbname = getenv("PGUSER");
else
! dbname = get_user_name(progname);
}
initPQExpBuffer(&sql);
--- 174,180 ----
else if (getenv("PGUSER"))
dbname = getenv("PGUSER");
else
! dbname = get_user_name_or_exit(progname);
}
initPQExpBuffer(&sql);
diff --git a/src/bin/scripts/createlang.c b/src/bin/scripts/createlang.c
new file mode 100644
index ff544a8..5cfba8e
*** a/src/bin/scripts/createlang.c
--- b/src/bin/scripts/createlang.c
*************** main(int argc, char *argv[])
*** 127,133 ****
else if (getenv("PGUSER"))
dbname = getenv("PGUSER");
else
! dbname = get_user_name(progname);
}
initPQExpBuffer(&sql);
--- 127,133 ----
else if (getenv("PGUSER"))
dbname = getenv("PGUSER");
else
! dbname = get_user_name_or_exit(progname);
}
initPQExpBuffer(&sql);
diff --git a/src/bin/scripts/createuser.c b/src/bin/scripts/createuser.c
new file mode 100644
index 83623ea..e4e6e64
*** a/src/bin/scripts/createuser.c
--- b/src/bin/scripts/createuser.c
*************** main(int argc, char *argv[])
*** 188,194 ****
if (getenv("PGUSER"))
newuser = getenv("PGUSER");
else
! newuser = get_user_name(progname);
}
}
--- 188,194 ----
if (getenv("PGUSER"))
newuser = getenv("PGUSER");
else
! newuser = get_user_name_or_exit(progname);
}
}
diff --git a/src/bin/scripts/droplang.c b/src/bin/scripts/droplang.c
new file mode 100644
index de20317..b9664a9
*** a/src/bin/scripts/droplang.c
--- b/src/bin/scripts/droplang.c
*************** main(int argc, char *argv[])
*** 126,132 ****
else if (getenv("PGUSER"))
dbname = getenv("PGUSER");
else
! dbname = get_user_name(progname);
}
initPQExpBuffer(&sql);
--- 126,132 ----
else if (getenv("PGUSER"))
dbname = getenv("PGUSER");
else
! dbname = get_user_name_or_exit(progname);
}
initPQExpBuffer(&sql);
diff --git a/src/bin/scripts/reindexdb.c b/src/bin/scripts/reindexdb.c
new file mode 100644
index f7c09be..4e762ea
*** a/src/bin/scripts/reindexdb.c
--- b/src/bin/scripts/reindexdb.c
*************** main(int argc, char *argv[])
*** 188,194 ****
else if (getenv("PGUSER"))
dbname = getenv("PGUSER");
else
! dbname = get_user_name(progname);
}
reindex_system_catalogs(dbname, host, port, username, prompt_password,
--- 188,194 ----
else if (getenv("PGUSER"))
dbname = getenv("PGUSER");
else
! dbname = get_user_name_or_exit(progname);
}
reindex_system_catalogs(dbname, host, port, username, prompt_password,
*************** main(int argc, char *argv[])
*** 203,209 ****
else if (getenv("PGUSER"))
dbname = getenv("PGUSER");
else
! dbname = get_user_name(progname);
}
if (indexes.head != NULL)
--- 203,209 ----
else if (getenv("PGUSER"))
dbname = getenv("PGUSER");
else
! 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
new file mode 100644
index 616d933..8970ec6
*** a/src/bin/scripts/vacuumdb.c
--- b/src/bin/scripts/vacuumdb.c
*************** main(int argc, char *argv[])
*** 202,208 ****
else if (getenv("PGUSER"))
dbname = getenv("PGUSER");
else
! dbname = get_user_name(progname);
}
if (tables.head != NULL)
--- 202,208 ----
else if (getenv("PGUSER"))
dbname = getenv("PGUSER");
else
! dbname = get_user_name_or_exit(progname);
}
if (tables.head != NULL)
diff --git a/src/include/port.h b/src/include/port.h
new file mode 100644
index 5ef4b0a..473c4f6
*** a/src/include/port.h
--- b/src/include/port.h
*************** extern pqsigfunc pqsignal(int signo, pqs
*** 473,478 ****
--- 473,482 ----
/* port/quotes.c */
extern char *escape_single_quotes_ascii(const char *src);
+ /* port/username.c */
+ extern const char *get_user_name(char **errstr);
+ extern const char *get_user_name_or_exit(const char *progname);
+
/* port/wait_error.c */
extern char *wait_result_to_str(int exit_status);
diff --git a/src/port/Makefile b/src/port/Makefile
new file mode 100644
index 1be4ff5..a50e0af
*** a/src/port/Makefile
--- b/src/port/Makefile
*************** LIBS += $(PTHREAD_LIBS)
*** 33,39 ****
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
# foo_srv.o and foo.o are both built from foo.c, but only foo.o has -DFRONTEND
OBJS_SRV = $(OBJS:%.o=%_srv.o)
--- 33,39 ----
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 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)
diff --git a/src/port/username.c b/src/port/username.c
new file mode 100644
index ...25dc939
*** a/src/port/username.c
--- b/src/port/username.c
***************
*** 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;
+ }
diff --git a/src/tools/msvc/Mkvcbuild.pm b/src/tools/msvc/Mkvcbuild.pm
new file mode 100644
index bc7f449..d6b6eaf
*** a/src/tools/msvc/Mkvcbuild.pm
--- b/src/tools/msvc/Mkvcbuild.pm
*************** sub mkvcbuild
*** 70,77 ****
erand48.c snprintf.c strlcat.c strlcpy.c dirmod.c noblock.c path.c
pgcheckdir.c pg_crc.c pgmkdirp.c pgsleep.c pgstrcasecmp.c pqsignal.c
qsort.c qsort_arg.c quotes.c
! sprompt.c tar.c thread.c getopt.c getopt_long.c dirent.c rint.c win32env.c
! win32error.c win32setlocale.c);
our @pgcommonallfiles = qw(
exec.c pgfnames.c psprintf.c relpath.c rmtree.c wait_error.c);
--- 70,77 ----
erand48.c snprintf.c strlcat.c strlcpy.c dirmod.c noblock.c path.c
pgcheckdir.c pg_crc.c pgmkdirp.c pgsleep.c pgstrcasecmp.c pqsignal.c
qsort.c qsort_arg.c quotes.c
! sprompt.c tar.c thread.c getopt.c getopt_long.c dirent.c rint.c username.c
! win32env.c win32error.c win32setlocale.c);
our @pgcommonallfiles = qw(
exec.c pgfnames.c psprintf.c relpath.c rmtree.c wait_error.c);
^ permalink raw reply [nested|flat] 24+ messages in thread
* Re: BUG #8139: initdb: Misleading error message when current user not in /etc/passwd
@ 2013-12-18 17:16 Bruce Momjian <bruce@momjian.us>
parent: Bruce Momjian <bruce@momjian.us>
1 sibling, 1 reply; 24+ messages in thread
From: Bruce Momjian @ 2013-12-18 17:16 UTC (permalink / raw)
To: Alvaro Herrera <alvherre@2ndquadrant.com>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; nicolas@marchildon.net; pgsql-bugs
On Mon, Dec 9, 2013 at 07:47:34PM -0500, Bruce Momjian wrote:
> On Mon, Dec 9, 2013 at 06:45:39PM -0300, Alvaro Herrera wrote:
> > Bruce Momjian wrote:
> >
> > > return STATUS_ERROR;
> > > }
> > >
> > > ! user_name = get_user_name(&errstr);
> > > ! if (!user_name)
> > > {
> > > ! ereport(LOG, (errmsg("%s\n", errstr)));
> > > ! pfree(errstr);
> > > return STATUS_ERROR;
> > > }
> >
> > The message is already translated by get_user_name, so I think this
> > should use errmsg_internal() instead of errmsg(). Also, why do you add
> > a newline?
>
> OK, done.
>
> > Not clear whether the new file should be in src/port or src/common.
>
> Agreed. It isn't designed to add missing OS functionality, but it is
> mostly OS-specific code.
>
> Updated patch attached, with Tom's requested changes.
Patch applied.
--
Bruce Momjian <bruce@momjian.us> http://momjian.us
EnterpriseDB http://enterprisedb.com
+ Everyone has their own god. +
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
^ permalink raw reply [nested|flat] 24+ messages in thread
* Re: BUG #8139: initdb: Misleading error message when current user not in /etc/passwd
@ 2013-12-20 15:46 Peter Eisentraut <peter_e@gmx.net>
parent: Bruce Momjian <bruce@momjian.us>
1 sibling, 1 reply; 24+ messages in thread
From: Peter Eisentraut @ 2013-12-20 15:46 UTC (permalink / raw)
To: Bruce Momjian <bruce@momjian.us>; Alvaro Herrera <alvherre@2ndquadrant.com>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; nicolas@marchildon.net; pgsql-bugs
On 12/9/13, 7:47 PM, Bruce Momjian wrote:
>> Not clear whether the new file should be in src/port or src/common.
> Agreed. It isn't designed to add missing OS functionality, but it is
> mostly OS-specific code.
It's not for portability, though, is it?
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
^ permalink raw reply [nested|flat] 24+ messages in thread
* Re: BUG #8139: initdb: Misleading error message when current user not in /etc/passwd
@ 2014-01-07 04:41 Bruce Momjian <bruce@momjian.us>
parent: Peter Eisentraut <peter_e@gmx.net>
0 siblings, 1 reply; 24+ messages in thread
From: Bruce Momjian @ 2014-01-07 04:41 UTC (permalink / raw)
To: Peter Eisentraut <peter_e@gmx.net>; +Cc: Alvaro Herrera <alvherre@2ndquadrant.com>; Tom Lane <tgl@sss.pgh.pa.us>; nicolas@marchildon.net; pgsql-bugs
On Fri, Dec 20, 2013 at 10:46:43AM -0500, Peter Eisentraut wrote:
> On 12/9/13, 7:47 PM, Bruce Momjian wrote:
> >> Not clear whether the new file should be in src/port or src/common.
> > Agreed. It isn't designed to add missing OS functionality, but it is
> > mostly OS-specific code.
>
> It's not for portability, though, is it?
Well, neither is sprompt.c, but that has a lot of port-specific code in
it, so I used that as a guide.
--
Bruce Momjian <bruce@momjian.us> http://momjian.us
EnterpriseDB http://enterprisedb.com
+ Everyone has their own god. +
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
^ permalink raw reply [nested|flat] 24+ messages in thread
* Re: BUG #8139: initdb: Misleading error message when current user not in /etc/passwd
@ 2014-01-07 21:40 Alvaro Herrera <alvherre@2ndquadrant.com>
parent: Bruce Momjian <bruce@momjian.us>
0 siblings, 1 reply; 24+ messages in thread
From: Alvaro Herrera @ 2014-01-07 21:40 UTC (permalink / raw)
To: Bruce Momjian <bruce@momjian.us>; +Cc: Peter Eisentraut <peter_e@gmx.net>; Tom Lane <tgl@sss.pgh.pa.us>; nicolas@marchildon.net; pgsql-bugs
Bruce Momjian wrote:
> On Fri, Dec 20, 2013 at 10:46:43AM -0500, Peter Eisentraut wrote:
> > On 12/9/13, 7:47 PM, Bruce Momjian wrote:
> > >> Not clear whether the new file should be in src/port or src/common.
> > > Agreed. It isn't designed to add missing OS functionality, but it is
> > > mostly OS-specific code.
> >
> > It's not for portability, though, is it?
>
> Well, neither is sprompt.c, but that has a lot of port-specific code in
> it, so I used that as a guide.
src/common was created much later than sprompt.c was written. I would
have thought that the consideration would have been that sprompt.c
should eventually be moved to src/common; not that it would serve as a
precedent for anything.
--
Álvaro Herrera http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
^ permalink raw reply [nested|flat] 24+ messages in thread
* Re: BUG #8139: initdb: Misleading error message when current user not in /etc/passwd
@ 2014-01-10 19:50 Bruce Momjian <bruce@momjian.us>
parent: Alvaro Herrera <alvherre@2ndquadrant.com>
0 siblings, 1 reply; 24+ messages in thread
From: Bruce Momjian @ 2014-01-10 19:50 UTC (permalink / raw)
To: Alvaro Herrera <alvherre@2ndquadrant.com>; +Cc: Peter Eisentraut <peter_e@gmx.net>; Tom Lane <tgl@sss.pgh.pa.us>; nicolas@marchildon.net; pgsql-bugs
On Tue, Jan 7, 2014 at 06:40:14PM -0300, Alvaro Herrera wrote:
> Bruce Momjian wrote:
> > On Fri, Dec 20, 2013 at 10:46:43AM -0500, Peter Eisentraut wrote:
> > > On 12/9/13, 7:47 PM, Bruce Momjian wrote:
> > > >> Not clear whether the new file should be in src/port or src/common.
> > > > Agreed. It isn't designed to add missing OS functionality, but it is
> > > > mostly OS-specific code.
> > >
> > > It's not for portability, though, is it?
> >
> > Well, neither is sprompt.c, but that has a lot of port-specific code in
> > it, so I used that as a guide.
>
> src/common was created much later than sprompt.c was written. I would
> have thought that the consideration would have been that sprompt.c
> should eventually be moved to src/common; not that it would serve as a
> precedent for anything.
Are we not moving items over to common where appropriate? Are we
worried about bring external applications?
--
Bruce Momjian <bruce@momjian.us> http://momjian.us
EnterpriseDB http://enterprisedb.com
+ Everyone has their own god. +
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
^ permalink raw reply [nested|flat] 24+ messages in thread
* Re: BUG #8139: initdb: Misleading error message when current user not in /etc/passwd
@ 2014-01-10 20:23 Alvaro Herrera <alvherre@2ndquadrant.com>
parent: Bruce Momjian <bruce@momjian.us>
0 siblings, 1 reply; 24+ messages in thread
From: Alvaro Herrera @ 2014-01-10 20:23 UTC (permalink / raw)
To: Bruce Momjian <bruce@momjian.us>; +Cc: Peter Eisentraut <peter_e@gmx.net>; Tom Lane <tgl@sss.pgh.pa.us>; nicolas@marchildon.net; pgsql-bugs
Bruce Momjian wrote:
> On Tue, Jan 7, 2014 at 06:40:14PM -0300, Alvaro Herrera wrote:
> > Bruce Momjian wrote:
> > > On Fri, Dec 20, 2013 at 10:46:43AM -0500, Peter Eisentraut wrote:
> > > > On 12/9/13, 7:47 PM, Bruce Momjian wrote:
> > > > >> Not clear whether the new file should be in src/port or src/common.
> > > > > Agreed. It isn't designed to add missing OS functionality, but it is
> > > > > mostly OS-specific code.
> > > >
> > > > It's not for portability, though, is it?
> > >
> > > Well, neither is sprompt.c, but that has a lot of port-specific code in
> > > it, so I used that as a guide.
> >
> > src/common was created much later than sprompt.c was written. I would
> > have thought that the consideration would have been that sprompt.c
> > should eventually be moved to src/common; not that it would serve as a
> > precedent for anything.
>
> Are we not moving items over to common where appropriate?
I don't think we're moving code from src/port to src/common just for the
heck of it; but ISTM if we're adding new code which belongs to
src/common, and there's a natural file for it in src/port which should
arguably also be in src/common, then it makes sense to put both the new
code and the old file together in src/common.
Note that nothing in src/port should depend on stuff in src/common. As
I see it, src/port is very bare-bones stuff which src/common builds on
top of. It might also make sense, in some cases, to consider low-level
routines in src/port that are used by higher level routines in
src/common.
> Are we worried about bring external applications?
Please rephrase. What are we worried about?
--
Álvaro Herrera http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
^ permalink raw reply [nested|flat] 24+ messages in thread
* Re: BUG #8139: initdb: Misleading error message when current user not in /etc/passwd
@ 2014-01-10 23:04 Bruce Momjian <bruce@momjian.us>
parent: Alvaro Herrera <alvherre@2ndquadrant.com>
0 siblings, 1 reply; 24+ messages in thread
From: Bruce Momjian @ 2014-01-10 23:04 UTC (permalink / raw)
To: Alvaro Herrera <alvherre@2ndquadrant.com>; +Cc: Peter Eisentraut <peter_e@gmx.net>; Tom Lane <tgl@sss.pgh.pa.us>; nicolas@marchildon.net; pgsql-bugs
On Fri, Jan 10, 2014 at 05:23:43PM -0300, Alvaro Herrera wrote:
> > Are we not moving items over to common where appropriate?
>
> I don't think we're moving code from src/port to src/common just for the
> heck of it; but ISTM if we're adding new code which belongs to
> src/common, and there's a natural file for it in src/port which should
> arguably also be in src/common, then it makes sense to put both the new
> code and the old file together in src/common.
>
> Note that nothing in src/port should depend on stuff in src/common. As
> I see it, src/port is very bare-bones stuff which src/common builds on
> top of. It might also make sense, in some cases, to consider low-level
> routines in src/port that are used by higher level routines in
> src/common.
OK, C file moved from /port to /common.
> > Are we worried about bring external applications?
>
> Please rephrase. What are we worried about?
I was asking if we did not move old functions from port to common
because of concern about breaking 3rd party applications.
--
Bruce Momjian <bruce@momjian.us> http://momjian.us
EnterpriseDB http://enterprisedb.com
+ Everyone has their own god. +
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
^ permalink raw reply [nested|flat] 24+ messages in thread
* Re: BUG #8139: initdb: Misleading error message when current user not in /etc/passwd
@ 2014-01-11 01:49 Peter Eisentraut <peter_e@gmx.net>
parent: Bruce Momjian <bruce@momjian.us>
0 siblings, 1 reply; 24+ messages in thread
From: Peter Eisentraut @ 2014-01-11 01:49 UTC (permalink / raw)
To: Bruce Momjian <bruce@momjian.us>; +Cc: Alvaro Herrera <alvherre@2ndquadrant.com>; Tom Lane <tgl@sss.pgh.pa.us>; nicolas@marchildon.net; pgsql-bugs
On Fri, 2014-01-10 at 18:04 -0500, Bruce Momjian wrote:
> OK, C file moved from /port to /common.
Please fix the new compiler warnings in pg_upgrade.
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
^ permalink raw reply [nested|flat] 24+ messages in thread
* Re: BUG #8139: initdb: Misleading error message when current user not in /etc/passwd
@ 2014-01-11 01:57 Bruce Momjian <bruce@momjian.us>
parent: Peter Eisentraut <peter_e@gmx.net>
0 siblings, 0 replies; 24+ messages in thread
From: Bruce Momjian @ 2014-01-11 01:57 UTC (permalink / raw)
To: Peter Eisentraut <peter_e@gmx.net>; +Cc: Alvaro Herrera <alvherre@2ndquadrant.com>; Tom Lane <tgl@sss.pgh.pa.us>; nicolas@marchildon.net; pgsql-bugs
On Fri, Jan 10, 2014 at 08:49:18PM -0500, Peter Eisentraut wrote:
> On Fri, 2014-01-10 at 18:04 -0500, Bruce Momjian wrote:
> > OK, C file moved from /port to /common.
>
> Please fix the new compiler warnings in pg_upgrade.
Thanks, fixed.
--
Bruce Momjian <bruce@momjian.us> http://momjian.us
EnterpriseDB http://enterprisedb.com
+ Everyone has their own god. +
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
^ permalink raw reply [nested|flat] 24+ messages in thread
* Re: BUG #8139: initdb: Misleading error message when current user not in /etc/passwd
@ 2014-03-28 10:41 Christoph Berg <cb@df7cb.de>
parent: Bruce Momjian <bruce@momjian.us>
0 siblings, 1 reply; 24+ messages in thread
From: Christoph Berg @ 2014-03-28 10:41 UTC (permalink / raw)
To: Bruce Momjian <bruce@momjian.us>; +Cc: Alvaro Herrera <alvherre@2ndquadrant.com>; Tom Lane <tgl@sss.pgh.pa.us>; nicolas@marchildon.net; pgsql-bugs; David Fetter <david@fetter.org>
Re: Bruce Momjian 2013-12-18 <20131218171628.GA1690@momjian.us>
> On Mon, Dec 9, 2013 at 07:47:34PM -0500, Bruce Momjian wrote:
> > On Mon, Dec 9, 2013 at 06:45:39PM -0300, Alvaro Herrera wrote:
> > > Bruce Momjian wrote:
> > >
> > > > return STATUS_ERROR;
> > > > }
> > > >
> > > > ! user_name = get_user_name(&errstr);
> > > > ! if (!user_name)
> > > > {
> > > > ! ereport(LOG, (errmsg("%s\n", errstr)));
> > > > ! pfree(errstr);
> > > > return STATUS_ERROR;
> > > > }
> > >
> > > The message is already translated by get_user_name, so I think this
> > > should use errmsg_internal() instead of errmsg(). Also, why do you add
> > > a newline?
> >
> > OK, done.
> >
> > > Not clear whether the new file should be in src/port or src/common.
> >
> > Agreed. It isn't designed to add missing OS functionality, but it is
> > mostly OS-specific code.
> >
> > Updated patch attached, with Tom's requested changes.
>
> Patch applied.
Hi,
the quoted code bit above in src/backend/libpq/auth.c is utterly
broken: for peer authentication, it uses get_user_name(), which yields
the *server* user name, not the client's. For that reason, peer
authentication in 9.4devel is broken - you can't log in with your user
name, but you can just say -U postgres (or what the initdb user was),
and it will let you in.
The attached patch reverts the src/backend/libpq/auth.c portion of
613c6d26bd42dd8c2dd0664315be9551475b8864 and fixes peer auth.
Christoph
--
cb@df7cb.de | http://www.df7cb.de/
Attachments:
peer-auth-broken (text/plain, 1.4K) download
diff --git a/src/backend/libpq/auth.c b/src/backend/libpq/auth.c
new file mode 100644
index 2a46f7b..d5f15b0
*** a/src/backend/libpq/auth.c
--- b/src/backend/libpq/auth.c
*************** auth_peer(hbaPort *port)
*** 1560,1567 ****
char ident_user[IDENT_USERNAME_MAX + 1];
uid_t uid;
gid_t gid;
! const char *user_name;
! char *errstr;
errno = 0;
if (getpeereid(port->sock, &uid, &gid) != 0)
--- 1560,1566 ----
char ident_user[IDENT_USERNAME_MAX + 1];
uid_t uid;
gid_t gid;
! struct passwd *pass;
errno = 0;
if (getpeereid(port->sock, &uid, &gid) != 0)
*************** auth_peer(hbaPort *port)
*** 1578,1592 ****
return STATUS_ERROR;
}
! user_name = get_user_name(&errstr);
! if (!user_name)
{
! ereport(LOG, (errmsg_internal("%s", errstr)));
! pfree(errstr);
return STATUS_ERROR;
}
! strlcpy(ident_user, user_name, IDENT_USERNAME_MAX + 1);
return check_usermap(port->hba->usermap, port->user_name, ident_user, false);
}
--- 1577,1593 ----
return STATUS_ERROR;
}
! pass = getpwuid(uid);
!
! if (pass == NULL)
{
! ereport(LOG,
! (errmsg("local user with ID %d does not exist",
! (int) uid)));
return STATUS_ERROR;
}
! strlcpy(ident_user, pass->pw_name, IDENT_USERNAME_MAX + 1);
return check_usermap(port->hba->usermap, port->user_name, ident_user, false);
}
signature.asc (application/pgp-signature, 818B) download
^ permalink raw reply [nested|flat] 24+ messages in thread
* Re: BUG #8139: initdb: Misleading error message when current user not in /etc/passwd
@ 2014-03-28 14:31 Tom Lane <tgl@sss.pgh.pa.us>
parent: Christoph Berg <cb@df7cb.de>
0 siblings, 1 reply; 24+ messages in thread
From: Tom Lane @ 2014-03-28 14:31 UTC (permalink / raw)
To: Christoph Berg <cb@df7cb.de>; +Cc: Bruce Momjian <bruce@momjian.us>; Alvaro Herrera <alvherre@2ndquadrant.com>; nicolas@marchildon.net; pgsql-bugs; David Fetter <david@fetter.org>
Christoph Berg <cb@df7cb.de> writes:
> the quoted code bit above in src/backend/libpq/auth.c is utterly
> broken: for peer authentication, it uses get_user_name(), which yields
> the *server* user name, not the client's. For that reason, peer
> authentication in 9.4devel is broken - you can't log in with your user
> name, but you can just say -U postgres (or what the initdb user was),
> and it will let you in.
> The attached patch reverts the src/backend/libpq/auth.c portion of
> 613c6d26bd42dd8c2dd0664315be9551475b8864 and fixes peer auth.
Applied, thanks!
regards, tom lane
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
^ permalink raw reply [nested|flat] 24+ messages in thread
* Re: BUG #8139: initdb: Misleading error message when current user not in /etc/passwd
@ 2014-03-28 14:33 Bruce Momjian <bruce@momjian.us>
parent: Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 1 reply; 24+ messages in thread
From: Bruce Momjian @ 2014-03-28 14:33 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Christoph Berg <cb@df7cb.de>; Alvaro Herrera <alvherre@2ndquadrant.com>; nicolas@marchildon.net; pgsql-bugs; David Fetter <david@fetter.org>
On Fri, Mar 28, 2014 at 10:31:06AM -0400, Tom Lane wrote:
> Christoph Berg <cb@df7cb.de> writes:
> > the quoted code bit above in src/backend/libpq/auth.c is utterly
> > broken: for peer authentication, it uses get_user_name(), which yields
> > the *server* user name, not the client's. For that reason, peer
> > authentication in 9.4devel is broken - you can't log in with your user
> > name, but you can just say -U postgres (or what the initdb user was),
> > and it will let you in.
>
> > The attached patch reverts the src/backend/libpq/auth.c portion of
> > 613c6d26bd42dd8c2dd0664315be9551475b8864 and fixes peer auth.
>
> Applied, thanks!
I guess I should have said I was working on an updated patch. I will
merge my changes in.
--
Bruce Momjian <bruce@momjian.us> http://momjian.us
EnterpriseDB http://enterprisedb.com
+ Everyone has their own god. +
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
^ permalink raw reply [nested|flat] 24+ messages in thread
* Re: BUG #8139: initdb: Misleading error message when current user not in /etc/passwd
@ 2014-03-28 16:55 Bruce Momjian <bruce@momjian.us>
parent: Bruce Momjian <bruce@momjian.us>
0 siblings, 0 replies; 24+ messages in thread
From: Bruce Momjian @ 2014-03-28 16:55 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Christoph Berg <cb@df7cb.de>; Alvaro Herrera <alvherre@2ndquadrant.com>; nicolas@marchildon.net; pgsql-bugs; David Fetter <david@fetter.org>
On Fri, Mar 28, 2014 at 10:33:48AM -0400, Bruce Momjian wrote:
> On Fri, Mar 28, 2014 at 10:31:06AM -0400, Tom Lane wrote:
> > Christoph Berg <cb@df7cb.de> writes:
> > > the quoted code bit above in src/backend/libpq/auth.c is utterly
> > > broken: for peer authentication, it uses get_user_name(), which yields
> > > the *server* user name, not the client's. For that reason, peer
> > > authentication in 9.4devel is broken - you can't log in with your user
> > > name, but you can just say -U postgres (or what the initdb user was),
> > > and it will let you in.
> >
> > > The attached patch reverts the src/backend/libpq/auth.c portion of
> > > 613c6d26bd42dd8c2dd0664315be9551475b8864 and fixes peer auth.
> >
> > Applied, thanks!
>
> I guess I should have said I was working on an updated patch. I will
> merge my changes in.
I have applied the attached patch to make the code more closely match
what is done in src/common/username.c, particularly to display the errno
string on failure.
Christoph, thanks so much for finding this error now, rather than later.
I looked over the original patch that introduced this bug and
auth_peer() is the only place where we were passing in a user id, rather
than passing geteuid() directly to getpwuid, e.g.:
getpwuid(geteuid())
I think we are good now. My apologies for introducing this bug.
--
Bruce Momjian <bruce@momjian.us> http://momjian.us
EnterpriseDB http://enterprisedb.com
+ Everyone has their own god. +
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
Attachments:
peer.diff (text/x-diff, 1.6K) download
diff --git a/src/backend/libpq/auth.c b/src/backend/libpq/auth.c
new file mode 100644
index a2f1c96..31ade0b
*** a/src/backend/libpq/auth.c
--- b/src/backend/libpq/auth.c
*************** auth_peer(hbaPort *port)
*** 1559,1567 ****
char ident_user[IDENT_USERNAME_MAX + 1];
uid_t uid;
gid_t gid;
! struct passwd *pass;
- errno = 0;
if (getpeereid(port->sock, &uid, &gid) != 0)
{
/* Provide special error message if getpeereid is a stub */
--- 1559,1566 ----
char ident_user[IDENT_USERNAME_MAX + 1];
uid_t uid;
gid_t gid;
! struct passwd *pw;
if (getpeereid(port->sock, &uid, &gid) != 0)
{
/* Provide special error message if getpeereid is a stub */
*************** auth_peer(hbaPort *port)
*** 1576,1592 ****
return STATUS_ERROR;
}
! pass = getpwuid(uid);
!
! if (pass == NULL)
{
ereport(LOG,
! (errmsg("local user with ID %d does not exist",
! (int) uid)));
return STATUS_ERROR;
}
! strlcpy(ident_user, pass->pw_name, IDENT_USERNAME_MAX + 1);
return check_usermap(port->hba->usermap, port->user_name, ident_user, false);
}
--- 1575,1591 ----
return STATUS_ERROR;
}
! errno = 0; /* clear errno before call */
! pw = getpwuid(uid);
! if (!pw)
{
ereport(LOG,
! (errmsg("failed to look up local user id %ld: %s",
! (long) uid, errno ? strerror(errno) : _("user does not exist"))));
return STATUS_ERROR;
}
! strlcpy(ident_user, pw->pw_name, IDENT_USERNAME_MAX + 1);
return check_usermap(port->hba->usermap, port->user_name, ident_user, false);
}
^ permalink raw reply [nested|flat] 24+ messages in thread
end of thread, other threads:[~2014-03-28 16:55 UTC | newest]
Thread overview: 24+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2013-05-07 15:42 BUG #8139: initdb: Misleading error message when current user not in /etc/passwd nicolas@marchildon.net
2013-05-07 18:10 ` Tom Lane <tgl@sss.pgh.pa.us>
2013-05-07 18:19 ` Alvaro Herrera <alvherre@2ndquadrant.com>
2013-12-04 15:52 ` Bruce Momjian <bruce@momjian.us>
2013-12-04 19:03 ` Alvaro Herrera <alvherre@2ndquadrant.com>
2013-12-09 21:05 ` Bruce Momjian <bruce@momjian.us>
2013-12-09 21:35 ` Tom Lane <tgl@sss.pgh.pa.us>
2013-12-10 00:46 ` Bruce Momjian <bruce@momjian.us>
2013-12-09 21:45 ` Alvaro Herrera <alvherre@2ndquadrant.com>
2013-12-10 00:47 ` Bruce Momjian <bruce@momjian.us>
2013-12-18 17:16 ` Bruce Momjian <bruce@momjian.us>
2014-03-28 10:41 ` Christoph Berg <cb@df7cb.de>
2014-03-28 14:31 ` Tom Lane <tgl@sss.pgh.pa.us>
2014-03-28 14:33 ` Bruce Momjian <bruce@momjian.us>
2014-03-28 16:55 ` Bruce Momjian <bruce@momjian.us>
2013-12-20 15:46 ` Peter Eisentraut <peter_e@gmx.net>
2014-01-07 04:41 ` Bruce Momjian <bruce@momjian.us>
2014-01-07 21:40 ` Alvaro Herrera <alvherre@2ndquadrant.com>
2014-01-10 19:50 ` Bruce Momjian <bruce@momjian.us>
2014-01-10 20:23 ` Alvaro Herrera <alvherre@2ndquadrant.com>
2014-01-10 23:04 ` Bruce Momjian <bruce@momjian.us>
2014-01-11 01:49 ` Peter Eisentraut <peter_e@gmx.net>
2014-01-11 01:57 ` Bruce Momjian <bruce@momjian.us>
2013-12-04 19:34 ` Tom Lane <tgl@sss.pgh.pa.us>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox