Skip to content

Commit 9f7afb2

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 9cb28e9 commit 9f7afb2

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

39053905
#define MAXBUFSIZE 256
39063906

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

3935+
/* If no service name given, nothing to do */
39253936
if (service == NULL)
39263937
return 0;
39273938

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

39343949
if (!pqGetHomeDirectory(homedir, sizeof(homedir)))
3935-
{
3936-
printfPQExpBuffer(errorMessage, libpq_gettext("could not get home directory to locate service definition file"));
3937-
return 1;
3938-
}
3950+
goto next_file;
39393951
snprintf(serviceFile, MAXPGPATH, "%s/%s", homedir, ".pg_service.conf");
3940-
errno = 0;
3941-
if (stat(serviceFile, &stat_buf) != 0 && errno == ENOENT)
3952+
if (stat(serviceFile, &stat_buf) != 0)
39423953
goto next_file;
39433954
}
39443955

@@ -3954,8 +3965,7 @@ parseServiceInfo(PQconninfoOption *options, PQExpBuffer errorMessage)
39543965
*/
39553966
snprintf(serviceFile, MAXPGPATH, "%s/pg_service.conf",
39563967
getenv("PGSYSCONFDIR") ? getenv("PGSYSCONFDIR") : SYSCONFDIR);
3957-
errno = 0;
3958-
if (stat(serviceFile, &stat_buf) != 0 && errno == ENOENT)
3968+
if (stat(serviceFile, &stat_buf) != 0)
39593969
goto last_file;
39603970

39613971
status = parseServiceFile(serviceFile, service, options, errorMessage, &group_found);
@@ -5907,7 +5917,15 @@ dot_pg_pass_warning(PGconn *conn)
59075917
*
59085918
* This is essentially the same as get_home_path(), but we don't use that
59095919
* because we don't want to pull path.c into libpq (it pollutes application
5910-
* namespace)
5920+
* namespace).
5921+
*
5922+
* Returns true on success, false on failure to obtain the directory name.
5923+
*
5924+
* CAUTION: although in most situations failure is unexpected, there are users
5925+
* who like to run applications in a home-directory-less environment. On
5926+
* failure, you almost certainly DO NOT want to report an error. Just act as
5927+
* though whatever file you were hoping to find in the home directory isn't
5928+
* there (which it isn't).
59115929
*/
59125930
bool
59135931
pqGetHomeDirectory(char *buf, int bufsize)

0 commit comments

Comments
 (0)