Skip to content

Commit 7dc66a2

Browse files
committed
Fix libpq to not require user's home directory to exist.
Some people like to run libpq-using applications in environments where there's no home directory. We've broken that scenario before (cf commits 5b40677 and bd58d9d), and commit ba005f1 broke it again, by making it a hard error if we fail to get the home directory name while looking for ~/.pgpass. The previous precedent is that if we can't get the home directory name, we should just silently act as though the file we hoped to find there doesn't exist. Rearrange the new code to honor that. Looking around, the service-file code added by commit 41a4e45 had the same disease. Apparently, that escaped notice because it only runs when a service name has been specified, which I guess the people who use this scenario don't do. Nonetheless, it's wrong too, so fix that case as well. Add a comment about this policy to pqGetHomeDirectory, in the probably vain hope of forestalling the same error in future. And upgrade the rather miserable commenting in parseServiceInfo, too. In passing, also back off parseServiceInfo's assumption that only ENOENT is an ignorable error from stat() when checking a service file. We would need to ignore at least ENOTDIR as well (cf 5b40677), and seeing that the far-better-tested code for ~/.pgpass treats all stat() failures alike, I think this code ought to as well. Per bug #14872 from Dan Watson. Back-patch the .pgpass change to v10 where ba005f1 came in. The service-file bugs are far older, so back-patch the other changes to all supported branches. Discussion: https://postgr.es/m/20171025200457.1471.34504@wrigleys.postgresql.org
1 parent 98efa5e commit 7dc66a2

File tree

1 file changed

+27
-9
lines changed

1 file changed

+27
-9
lines changed

src/interfaces/libpq/fe-connect.c

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3906,6 +3906,16 @@ ldapServiceLookup(const char *purl, PQconninfoOption *options,
39063906

39073907
#define MAXBUFSIZE 256
39083908

3909+
/*
3910+
* parseServiceInfo: if a service name has been given, look it up and absorb
3911+
* connection options from it into *options.
3912+
*
3913+
* Returns 0 on success, nonzero on failure. On failure, if errorMessage
3914+
* isn't null, also store an error message there. (Note: the only reason
3915+
* this function and related ones don't dump core on errorMessage == NULL
3916+
* is the undocumented fact that printfPQExpBuffer does nothing when passed
3917+
* a null PQExpBuffer pointer.)
3918+
*/
39093919
static int
39103920
parseServiceInfo(PQconninfoOption *options, PQExpBuffer errorMessage)
39113921
{
@@ -3924,23 +3934,24 @@ parseServiceInfo(PQconninfoOption *options, PQExpBuffer errorMessage)
39243934
if (service == NULL)
39253935
service = getenv("PGSERVICE");
39263936

3937+
/* If no service name given, nothing to do */
39273938
if (service == NULL)
39283939
return 0;
39293940

3941+
/*
3942+
* Try PGSERVICEFILE if specified, else try ~/.pg_service.conf (if that
3943+
* exists).
3944+
*/
39303945
if ((env = getenv("PGSERVICEFILE")) != NULL)
39313946
strlcpy(serviceFile, env, sizeof(serviceFile));
39323947
else
39333948
{
39343949
char homedir[MAXPGPATH];
39353950

39363951
if (!pqGetHomeDirectory(homedir, sizeof(homedir)))
3937-
{
3938-
printfPQExpBuffer(errorMessage, libpq_gettext("could not get home directory to locate service definition file"));
3939-
return 1;
3940-
}
3952+
goto next_file;
39413953
snprintf(serviceFile, MAXPGPATH, "%s/%s", homedir, ".pg_service.conf");
3942-
errno = 0;
3943-
if (stat(serviceFile, &stat_buf) != 0 && errno == ENOENT)
3954+
if (stat(serviceFile, &stat_buf) != 0)
39443955
goto next_file;
39453956
}
39463957

@@ -3956,8 +3967,7 @@ parseServiceInfo(PQconninfoOption *options, PQExpBuffer errorMessage)
39563967
*/
39573968
snprintf(serviceFile, MAXPGPATH, "%s/pg_service.conf",
39583969
getenv("PGSYSCONFDIR") ? getenv("PGSYSCONFDIR") : SYSCONFDIR);
3959-
errno = 0;
3960-
if (stat(serviceFile, &stat_buf) != 0 && errno == ENOENT)
3970+
if (stat(serviceFile, &stat_buf) != 0)
39613971
goto last_file;
39623972

39633973
status = parseServiceFile(serviceFile, service, options, errorMessage, &group_found);
@@ -5922,7 +5932,15 @@ dot_pg_pass_warning(PGconn *conn)
59225932
*
59235933
* This is essentially the same as get_home_path(), but we don't use that
59245934
* because we don't want to pull path.c into libpq (it pollutes application
5925-
* namespace)
5935+
* namespace).
5936+
*
5937+
* Returns true on success, false on failure to obtain the directory name.
5938+
*
5939+
* CAUTION: although in most situations failure is unexpected, there are users
5940+
* who like to run applications in a home-directory-less environment. On
5941+
* failure, you almost certainly DO NOT want to report an error. Just act as
5942+
* though whatever file you were hoping to find in the home directory isn't
5943+
* there (which it isn't).
59265944
*/
59275945
bool
59285946
pqGetHomeDirectory(char *buf, int bufsize)

0 commit comments

Comments
 (0)