Skip to content

Commit ee02c1c

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 3cc5f05 commit ee02c1c

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

39063906
#define MAXBUFSIZE 256
39073907

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

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

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

39353950
if (!pqGetHomeDirectory(homedir, sizeof(homedir)))
3936-
{
3937-
printfPQExpBuffer(errorMessage, libpq_gettext("could not get home directory to locate service definition file"));
3938-
return 1;
3939-
}
3951+
goto next_file;
39403952
snprintf(serviceFile, MAXPGPATH, "%s/%s", homedir, ".pg_service.conf");
3941-
errno = 0;
3942-
if (stat(serviceFile, &stat_buf) != 0 && errno == ENOENT)
3953+
if (stat(serviceFile, &stat_buf) != 0)
39433954
goto next_file;
39443955
}
39453956

@@ -3955,8 +3966,7 @@ parseServiceInfo(PQconninfoOption *options, PQExpBuffer errorMessage)
39553966
*/
39563967
snprintf(serviceFile, MAXPGPATH, "%s/pg_service.conf",
39573968
getenv("PGSYSCONFDIR") ? getenv("PGSYSCONFDIR") : SYSCONFDIR);
3958-
errno = 0;
3959-
if (stat(serviceFile, &stat_buf) != 0 && errno == ENOENT)
3969+
if (stat(serviceFile, &stat_buf) != 0)
39603970
goto last_file;
39613971

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

0 commit comments

Comments
 (0)