Add the "PGPASSFILE" environment variable to specify to the password
authorBruce Momjian <bruce@momjian.us>
Fri, 10 Jun 2005 03:02:30 +0000 (03:02 +0000)
committerBruce Momjian <bruce@momjian.us>
Fri, 10 Jun 2005 03:02:30 +0000 (03:02 +0000)
file.

Andrew Dunstan

doc/src/sgml/libpq.sgml
src/interfaces/libpq/fe-connect.c

index adee93a8853ea05505612f93bc915208cf59778b..be92de3e701ef3624644e49aa4413e6b99afd0f4 100644 (file)
@@ -1,5 +1,5 @@
 <!--
-$PostgreSQL: pgsql/doc/src/sgml/libpq.sgml,v 1.183 2005/06/09 19:08:28 tgl Exp $
+$PostgreSQL: pgsql/doc/src/sgml/libpq.sgml,v 1.184 2005/06/10 03:02:01 momjian Exp $
 -->
 
  <chapter id="libpq">
@@ -3712,6 +3712,17 @@ allow non-root users to see process environment variables via
 </listitem>
 <listitem>
 <para>
+<indexterm>
+ <primary><envar>PGPASSFILE</envar></primary>
+</indexterm>
+<envar>PGPASSFILE</envar>
+specifies the name of the password file to use for lookups.
+If not set, it defaults to <filename>~/.pgpass</>
+(see <xref linkend="libpq-pgpass">).
+</para>
+</listitem>
+<listitem>
+<para>
 <indexterm>
  <primary><envar>PGSERVICE</envar></primary>
 </indexterm>
@@ -3902,12 +3913,13 @@ internationalization.
 </indexterm>
 
 <para>
-The file <filename>.pgpass</filename> in a user's home directory is a file
-that can contain passwords to be used if the connection requires a
-password (and no password has been specified otherwise).
-On Microsoft Windows the file is named
-<filename>%APPDATA%\postgresql\pgpass.conf</> (where <filename>%APPDATA%</>
-refers to the Application Data subdirectory in the user's profile).
+The file <filename>.pgpass</filename> in a user's home directory or the
+file referenced by <envar>PGPASSFILE</envar> can contain passwords to
+be used if the connection requires a password (and no password has been
+specified  otherwise). On Microsoft Windows the file is named
+<filename>%APPDATA%\postgresql\pgpass.conf</> (where
+<filename>%APPDATA%</> refers to the Application Data subdirectory in
+the user's profile).
 </para>
 
 <para>
index 05899450237f8d3345306702c26bf8b727976452..8c9a005602f900447f39a7f5f8871c6c80e92207 100644 (file)
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *   $PostgreSQL: pgsql/src/interfaces/libpq/fe-connect.c,v 1.307 2005/06/04 20:42:43 momjian Exp $
+ *   $PostgreSQL: pgsql/src/interfaces/libpq/fe-connect.c,v 1.308 2005/06/10 03:02:30 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -3217,9 +3217,9 @@ static char *
 PasswordFromFile(char *hostname, char *port, char *dbname, char *username)
 {
    FILE       *fp;
-   char        homedir[MAXPGPATH];
    char        pgpassfile[MAXPGPATH];
    struct stat stat_buf;
+   char       *passfile_env;
 
 #define LINELEN NAMEDATALEN*5
    char        buf[LINELEN];
@@ -3236,15 +3236,38 @@ PasswordFromFile(char *hostname, char *port, char *dbname, char *username)
    if (port == NULL)
        port = DEF_PGPORT_STR;
 
-   if (!pqGetHomeDirectory(homedir, sizeof(homedir)))
-       return NULL;
+   if ((passfile_env = getenv("PGPASSFILE")) != NULL)
+   {
+       /* use the literal path from the environment, if set */
+       StrNCpy(pgpassfile, passfile_env, MAXPGPATH);
+       if (!pgpassfile)
+       {
+           fprintf(stderr, libpq_gettext("out of memory\n"));
+           return NULL;
+       }
+   }
+   else
+   {
+       char        homedir[MAXPGPATH];
 
-   snprintf(pgpassfile, sizeof(pgpassfile), "%s/%s", homedir, PGPASSFILE);
+       if (!pqGetHomeDirectory(homedir, sizeof(homedir)))
+           return NULL;
+       snprintf(pgpassfile, sizeof(pgpassfile), "%s/%s", homedir, PGPASSFILE);
+   }
 
    /* If password file cannot be opened, ignore it. */
    if (stat(pgpassfile, &stat_buf) == -1)
        return NULL;
 
+   if (!S_ISREG(stat_buf.st_mode))
+   {
+       fprintf(stderr,
+               libpq_gettext("WARNING: Password file %s is not a plain file.\n"),
+               pgpassfile);
+       free(pgpassfile);
+       return NULL;
+   }
+
 #ifndef WIN32
    /* If password file is insecure, alert the user and ignore it. */
    if (stat_buf.st_mode & (S_IRWXG | S_IRWXO))