Skip to content

Commit 6dd7a12

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 da82bb1 commit 6dd7a12

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
@@ -3897,6 +3897,16 @@ ldapServiceLookup(const char *purl, PQconninfoOption *options,
38973897

38983898
#define MAXBUFSIZE 256
38993899

3900+
/*
3901+
* parseServiceInfo: if a service name has been given, look it up and absorb
3902+
* connection options from it into *options.
3903+
*
3904+
* Returns 0 on success, nonzero on failure. On failure, if errorMessage
3905+
* isn't null, also store an error message there. (Note: the only reason
3906+
* this function and related ones don't dump core on errorMessage == NULL
3907+
* is the undocumented fact that printfPQExpBuffer does nothing when passed
3908+
* a null PQExpBuffer pointer.)
3909+
*/
39003910
static int
39013911
parseServiceInfo(PQconninfoOption *options, PQExpBuffer errorMessage)
39023912
{
@@ -3915,23 +3925,24 @@ parseServiceInfo(PQconninfoOption *options, PQExpBuffer errorMessage)
39153925
if (service == NULL)
39163926
service = getenv("PGSERVICE");
39173927

3928+
/* If no service name given, nothing to do */
39183929
if (service == NULL)
39193930
return 0;
39203931

3932+
/*
3933+
* Try PGSERVICEFILE if specified, else try ~/.pg_service.conf (if that
3934+
* exists).
3935+
*/
39213936
if ((env = getenv("PGSERVICEFILE")) != NULL)
39223937
strlcpy(serviceFile, env, sizeof(serviceFile));
39233938
else
39243939
{
39253940
char homedir[MAXPGPATH];
39263941

39273942
if (!pqGetHomeDirectory(homedir, sizeof(homedir)))
3928-
{
3929-
printfPQExpBuffer(errorMessage, libpq_gettext("could not get home directory to locate service definition file"));
3930-
return 1;
3931-
}
3943+
goto next_file;
39323944
snprintf(serviceFile, MAXPGPATH, "%s/%s", homedir, ".pg_service.conf");
3933-
errno = 0;
3934-
if (stat(serviceFile, &stat_buf) != 0 && errno == ENOENT)
3945+
if (stat(serviceFile, &stat_buf) != 0)
39353946
goto next_file;
39363947
}
39373948

@@ -3947,8 +3958,7 @@ parseServiceInfo(PQconninfoOption *options, PQExpBuffer errorMessage)
39473958
*/
39483959
snprintf(serviceFile, MAXPGPATH, "%s/pg_service.conf",
39493960
getenv("PGSYSCONFDIR") ? getenv("PGSYSCONFDIR") : SYSCONFDIR);
3950-
errno = 0;
3951-
if (stat(serviceFile, &stat_buf) != 0 && errno == ENOENT)
3961+
if (stat(serviceFile, &stat_buf) != 0)
39523962
goto last_file;
39533963

39543964
status = parseServiceFile(serviceFile, service, options, errorMessage, &group_found);
@@ -5892,7 +5902,15 @@ dot_pg_pass_warning(PGconn *conn)
58925902
*
58935903
* This is essentially the same as get_home_path(), but we don't use that
58945904
* because we don't want to pull path.c into libpq (it pollutes application
5895-
* namespace)
5905+
* namespace).
5906+
*
5907+
* Returns true on success, false on failure to obtain the directory name.
5908+
*
5909+
* CAUTION: although in most situations failure is unexpected, there are users
5910+
* who like to run applications in a home-directory-less environment. On
5911+
* failure, you almost certainly DO NOT want to report an error. Just act as
5912+
* though whatever file you were hoping to find in the home directory isn't
5913+
* there (which it isn't).
58965914
*/
58975915
bool
58985916
pqGetHomeDirectory(char *buf, int bufsize)

0 commit comments

Comments
 (0)