Skip to content

Commit 453d74b

Browse files
committed
Add the "PGPASSFILE" environment variable to specify to the password
file. Andrew Dunstan
1 parent 3b167a4 commit 453d74b

File tree

2 files changed

+47
-12
lines changed

2 files changed

+47
-12
lines changed

doc/src/sgml/libpq.sgml

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/libpq.sgml,v 1.183 2005/06/09 19:08:28 tgl Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/libpq.sgml,v 1.184 2005/06/10 03:02:01 momjian Exp $
33
-->
44

55
<chapter id="libpq">
@@ -3712,6 +3712,17 @@ allow non-root users to see process environment variables via
37123712
</listitem>
37133713
<listitem>
37143714
<para>
3715+
<indexterm>
3716+
<primary><envar>PGPASSFILE</envar></primary>
3717+
</indexterm>
3718+
<envar>PGPASSFILE</envar>
3719+
specifies the name of the password file to use for lookups.
3720+
If not set, it defaults to <filename>~/.pgpass</>
3721+
(see <xref linkend="libpq-pgpass">).
3722+
</para>
3723+
</listitem>
3724+
<listitem>
3725+
<para>
37153726
<indexterm>
37163727
<primary><envar>PGSERVICE</envar></primary>
37173728
</indexterm>
@@ -3902,12 +3913,13 @@ internationalization.
39023913
</indexterm>
39033914

39043915
<para>
3905-
The file <filename>.pgpass</filename> in a user's home directory is a file
3906-
that can contain passwords to be used if the connection requires a
3907-
password (and no password has been specified otherwise).
3908-
On Microsoft Windows the file is named
3909-
<filename>%APPDATA%\postgresql\pgpass.conf</> (where <filename>%APPDATA%</>
3910-
refers to the Application Data subdirectory in the user's profile).
3916+
The file <filename>.pgpass</filename> in a user's home directory or the
3917+
file referenced by <envar>PGPASSFILE</envar> can contain passwords to
3918+
be used if the connection requires a password (and no password has been
3919+
specified otherwise). On Microsoft Windows the file is named
3920+
<filename>%APPDATA%\postgresql\pgpass.conf</> (where
3921+
<filename>%APPDATA%</> refers to the Application Data subdirectory in
3922+
the user's profile).
39113923
</para>
39123924

39133925
<para>

src/interfaces/libpq/fe-connect.c

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/interfaces/libpq/fe-connect.c,v 1.307 2005/06/04 20:42:43 momjian Exp $
11+
* $PostgreSQL: pgsql/src/interfaces/libpq/fe-connect.c,v 1.308 2005/06/10 03:02:30 momjian Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -3217,9 +3217,9 @@ static char *
32173217
PasswordFromFile(char *hostname, char *port, char *dbname, char *username)
32183218
{
32193219
FILE *fp;
3220-
char homedir[MAXPGPATH];
32213220
char pgpassfile[MAXPGPATH];
32223221
struct stat stat_buf;
3222+
char *passfile_env;
32233223

32243224
#define LINELEN NAMEDATALEN*5
32253225
char buf[LINELEN];
@@ -3236,15 +3236,38 @@ PasswordFromFile(char *hostname, char *port, char *dbname, char *username)
32363236
if (port == NULL)
32373237
port = DEF_PGPORT_STR;
32383238

3239-
if (!pqGetHomeDirectory(homedir, sizeof(homedir)))
3240-
return NULL;
3239+
if ((passfile_env = getenv("PGPASSFILE")) != NULL)
3240+
{
3241+
/* use the literal path from the environment, if set */
3242+
StrNCpy(pgpassfile, passfile_env, MAXPGPATH);
3243+
if (!pgpassfile)
3244+
{
3245+
fprintf(stderr, libpq_gettext("out of memory\n"));
3246+
return NULL;
3247+
}
3248+
}
3249+
else
3250+
{
3251+
char homedir[MAXPGPATH];
32413252

3242-
snprintf(pgpassfile, sizeof(pgpassfile), "%s/%s", homedir, PGPASSFILE);
3253+
if (!pqGetHomeDirectory(homedir, sizeof(homedir)))
3254+
return NULL;
3255+
snprintf(pgpassfile, sizeof(pgpassfile), "%s/%s", homedir, PGPASSFILE);
3256+
}
32433257

32443258
/* If password file cannot be opened, ignore it. */
32453259
if (stat(pgpassfile, &stat_buf) == -1)
32463260
return NULL;
32473261

3262+
if (!S_ISREG(stat_buf.st_mode))
3263+
{
3264+
fprintf(stderr,
3265+
libpq_gettext("WARNING: Password file %s is not a plain file.\n"),
3266+
pgpassfile);
3267+
free(pgpassfile);
3268+
return NULL;
3269+
}
3270+
32483271
#ifndef WIN32
32493272
/* If password file is insecure, alert the user and ignore it. */
32503273
if (stat_buf.st_mode & (S_IRWXG | S_IRWXO))

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy