summaryrefslogtreecommitdiff
path: root/src/port/thread.c
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2015-01-11 12:35:44 -0500
committerTom Lane <tgl@sss.pgh.pa.us>2015-01-11 12:35:44 -0500
commit080eabe2e8a184ff40b7380aaaa9418714acace9 (patch)
treef203be9f71fbc124f6b1bb95d98326c8c5bdb138 /src/port/thread.c
parentde6429a8fdd3538e977b482d90389785d733e373 (diff)
Fix libpq's behavior when /etc/passwd isn't readable.
Some users run their applications in chroot environments that lack an /etc/passwd file. This means that the current UID's user name and home directory are not obtainable. libpq used to be all right with that, so long as the database role name to use was specified explicitly. But commit a4c8f14364c27508233f8a31ac4b10a4c90235a9 broke such cases by causing any failure of pg_fe_getauthname() to be treated as a hard error. In any case it did little to advance its nominal goal of causing errors in pg_fe_getauthname() to be reported better. So revert that and instead put some real error-reporting code in place. This requires changes to the APIs of pg_fe_getauthname() and pqGetpwuid(), since the latter had departed from the POSIX-specified API of getpwuid_r() in a way that made it impossible to distinguish actual lookup errors from "no such user". To allow such failures to be reported, while not failing if the caller supplies a role name, add a second call of pg_fe_getauthname() in connectOptions2(). This is a tad ugly, and could perhaps be avoided with some refactoring of PQsetdbLogin(), but I'll leave that idea for later. (Note that the complained-of misbehavior only occurs in PQsetdbLogin, not when using the PQconnect functions, because in the latter we will never bother to call pg_fe_getauthname() if the user gives a role name.) In passing also clean up the Windows-side usage of GetUserName(): the recommended buffer size is 257 bytes, the passed buffer length should be the buffer size not buffer size less 1, and any error is reported by GetLastError() not errno. Per report from Christoph Berg. Back-patch to 9.4 where the chroot failure case was introduced. The generally poor reporting of errors here is of very long standing, of course, but given the lack of field complaints about it we won't risk changing these APIs further back (even though they're theoretically internal to libpq).
Diffstat (limited to 'src/port/thread.c')
-rw-r--r--src/port/thread.c17
1 files changed, 13 insertions, 4 deletions
diff --git a/src/port/thread.c b/src/port/thread.c
index 1568803d626..aab74516ac9 100644
--- a/src/port/thread.c
+++ b/src/port/thread.c
@@ -83,6 +83,12 @@ pqStrerror(int errnum, char *strerrbuf, size_t buflen)
/*
* Wrapper around getpwuid() or getpwuid_r() to mimic POSIX getpwuid_r()
* behaviour, if it is not available or required.
+ *
+ * Per POSIX, the possible cases are:
+ * success: returns zero, *result is non-NULL
+ * uid not found: returns zero, *result is NULL
+ * error during lookup: returns an errno code, *result is NULL
+ * (caller should *not* assume that the errno variable is set)
*/
#ifndef WIN32
int
@@ -93,22 +99,25 @@ pqGetpwuid(uid_t uid, struct passwd * resultbuf, char *buffer,
#ifdef GETPWUID_R_5ARG
/* POSIX version */
- getpwuid_r(uid, resultbuf, buffer, buflen, result);
+ return getpwuid_r(uid, resultbuf, buffer, buflen, result);
#else
/*
* Early POSIX draft of getpwuid_r() returns 'struct passwd *'.
* getpwuid_r(uid, resultbuf, buffer, buflen)
*/
+ errno = 0;
*result = getpwuid_r(uid, resultbuf, buffer, buflen);
+ /* paranoia: ensure we return zero on success */
+ return (*result == NULL) ? errno : 0;
#endif
#else
-
/* no getpwuid_r() available, just use getpwuid() */
+ errno = 0;
*result = getpwuid(uid);
+ /* paranoia: ensure we return zero on success */
+ return (*result == NULL) ? errno : 0;
#endif
-
- return (*result == NULL) ? -1 : 0;
}
#endif