Remove PGPASSWORDFILE and default to always trying $HOME/.pgpass.
authorBruce Momjian
Thu, 5 Sep 2002 22:05:50 +0000 (22:05 +0000)
committerBruce Momjian
Thu, 5 Sep 2002 22:05:50 +0000 (22:05 +0000)
Cleanup up memory allocation for $HOME in related psql places.

Update mention of $HOME/.pgpass in the docs;  add mention in pg_dumpall.

doc/src/sgml/libpq.sgml
doc/src/sgml/ref/clusterdb.sgml
doc/src/sgml/ref/pg_dumpall.sgml
doc/src/sgml/ref/vacuumdb.sgml
src/bin/psql/input.c
src/bin/psql/startup.c
src/interfaces/libpq/fe-connect.c

index d1179fa92fc3fd1c09edc4c1b72b89468a7d45b4..90e1c48125df25c3a383314a77da897cb75117d5 100644 (file)
@@ -1,5 +1,5 @@
 
 
  
@@ -2052,23 +2052,6 @@ reasons; consider migrating to PGPASSWORDFILE.
 
 
 
-
PGPASSWORDFILE
-
-PGPASSWORDFILE
-sets the password file used if the backend demands password authentication.
-This file should have the format
-
-hostname:port:database:username:password
-
-Any of these may be a literal name, or a * that matches
-anything.  The first match will be the one used, so put more specific entries first.
-Entries with : or \ should be escaped
-with \.
-
-
-
-
 PGREALM sets the Kerberos realm to  use  with  
 PostgreSQL, if  it is different from the local realm.
 If PGREALM is set, PostgreSQL 
@@ -2141,6 +2124,27 @@ for information on correct values for these environment variables.
 
 
 
+
+
+Files
+
+
files
+
+
+$HOME/.pgpass is a file that can contain passwords
+to be used if the connection requires a password. This file should have the
+format:
+
+hostname:port:database:username:password
+
+Any of these may be a literal name, or *, which matches
+anything.  The first match will be used so put more specific entries first.
+Entries with : or \ should be escaped
+with \.
+
+
+
 
 Threading Behavior
 
index 114fbaafa3858ff2891086a096686ac6c3bebcc5..1eae14e9672f21441526fc10793669e97822120b 100644 (file)
@@ -1,5 +1,5 @@
 
 
@@ -54,10 +54,10 @@ PostgreSQL documentation
   
 
   
-  clusterdb will need to connect several times to the
-  PostgreSQL server.  If you are using password
-  authentication, it will ask for the password each time. It will probably be
-  very convenient to have a PGPASSWORDFILE in that case.
+  clusterdb might need to connect several
+  times to the PostgreSQL server, asking for
+  a password each time. It is convenient to have a
+  $HOME/.pgpass file in such cases.
   
 
  
index 869f480a3416a3161d8c8327bcc29170c392ec2c..e6f156dcc6e9f8218b2290d3a82fc931f86970a1 100644 (file)
@@ -1,5 +1,5 @@
 
 
@@ -61,6 +61,14 @@ PostgreSQL documentation
    The SQL script will be written to the standard output.  Shell
    operators should be used to redirect it into a file.
   
+
+  
+  pg_dumpall might need to connect several
+  times to the PostgreSQL server, asking for
+  a password each time. It is convenient to have a
+  $HOME/.pgpass file in such cases.
+  
+
  
 
  
index 286e3de4c75f543ebe136b5e8f729af704b847f0..40e6e0743197228be986c477e4c04b78b0eca0d8 100644 (file)
@@ -1,5 +1,5 @@
 
 
@@ -62,9 +62,10 @@ PostgreSQL documentation
 
 
   
-  vacuumdb will need to connect several times to the
-  PostgreSQL server, asking for the password each
-  time. It will probably be very convenient to have a PGPASSWORDFILE in that case.
+  vacuumdb might need to connect several
+  times to the PostgreSQL server, asking for
+  a password each time. It is convenient to have a
+  $HOME/.pgpass file in such cases.
   
 
  
index 6e083506f3e84c16fe3501fc71c0a7c88d2b1844..7ac11102669349921b79586e65140d6921f68df9 100644 (file)
@@ -3,7 +3,7 @@
  *
  * Copyright 2000 by PostgreSQL Global Development Group
  *
- * $Header: /cvsroot/pgsql/src/bin/psql/input.c,v 1.19 2002/04/10 22:46:58 petere Exp $
+ * $Header: /cvsroot/pgsql/src/bin/psql/input.c,v 1.20 2002/09/05 22:05:50 momjian Exp $
  */
 #include "postgres_fe.h"
 #include "input.h"
@@ -30,6 +30,8 @@ static void finishInput(void);
 static void finishInput(int, void *);
 #endif
 
+#define PSQLHISTORY    "/.psql_history"
+
 
 /*
  * gets_interactive()
@@ -142,11 +144,12 @@ initializeInput(int flags)
        home = getenv("HOME");
        if (home)
        {
-           char       *psql_history = (char *) malloc(strlen(home) + 20);
+           char       *psql_history = (char *) malloc(strlen(home) +
+                                               strlen(PSQLHISTORY) + 1);
 
            if (psql_history)
            {
-               sprintf(psql_history, "%s/.psql_history", home);
+               sprintf(psql_history, "%s" PSQLHISTORY, home);
                read_history(psql_history);
                free(psql_history);
            }
@@ -201,14 +204,15 @@ finishInput(int exitstatus, void *arg)
        home = getenv("HOME");
        if (home)
        {
-           psql_history = (char *) malloc(strlen(home) + 20);
+           psql_history = (char *) malloc(strlen(home) +
+                                   strlen(PSQLHISTORY) + 1);
            if (psql_history)
            {
                const char *var = GetVariable(pset.vars, "HISTSIZE");
 
                if (var)
                    stifle_history(atoi(var));
-               sprintf(psql_history, "%s/.psql_history", home);
+               sprintf(psql_history, "%s" PSQLHISTORY, home);
                write_history(psql_history);
                free(psql_history);
            }
index ef84d9410b6e0ec706fc03c511cf4a1c20fc567f..1ab0ac85243aacc22f351d0a1e008528473b0fab 100644 (file)
@@ -3,7 +3,7 @@
  *
  * Copyright 2000 by PostgreSQL Global Development Group
  *
- * $Header: /cvsroot/pgsql/src/bin/psql/startup.c,v 1.64 2002/09/04 20:31:36 momjian Exp $
+ * $Header: /cvsroot/pgsql/src/bin/psql/startup.c,v 1.65 2002/09/05 22:05:50 momjian Exp $
  */
 #include "postgres_fe.h"
 
@@ -42,6 +42,7 @@
  */
 PsqlSettings pset;
 
+#define PSQLRC "/.psqlrc"
 
 /*
  * Structures to pass information between the option parsing routine
@@ -604,19 +605,20 @@ process_psqlrc(void)
 
    if (home)
    {
-       psqlrc = malloc(strlen(home) + 20);
+       psqlrc = malloc(strlen(home) + strlen(PSQLRC) + 1 +
+                strlen(PG_VERSION) + 1);
        if (!psqlrc)
        {
            fprintf(stderr, gettext("%s: out of memory\n"), pset.progname);
            exit(EXIT_FAILURE);
        }
 
-       sprintf(psqlrc, "%s/.psqlrc-" PG_VERSION, home);
+       sprintf(psqlrc, "%s" PSQLRC "-" PG_VERSION, home);
        if (access(psqlrc, R_OK) == 0)
            process_file(psqlrc);
        else
        {
-           sprintf(psqlrc, "%s/.psqlrc", home);
+           sprintf(psqlrc, "%s" PSQLRC, home);
            if (access(psqlrc, R_OK) == 0)
                process_file(psqlrc);
        }
index c875ce7a60c1b22699329e8a0df45db2809e0c7e..564cbeb04441fe19990d08dec6aa551af6e13cbb 100644 (file)
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *   $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v 1.201 2002/09/04 20:31:46 momjian Exp $
+ *   $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v 1.202 2002/09/05 22:05:50 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -66,6 +66,7 @@ inet_aton(const char *cp, struct in_addr * inp)
 #define NOTIFYLIST_INITIAL_SIZE 10
 #define NOTIFYLIST_GROWBY 10
 
+#define PGPASSFILE "/.pgpass"
 
 /* ----------
  * Definition of the conninfo parameters and their fallback resources.
@@ -186,7 +187,7 @@ static int parseServiceInfo(PQconninfoOption *options,
                 PQExpBuffer errorMessage);
 char      *pwdfMatchesString(char *buf, char *token);
 char *PasswordFromFile(char *hostname, char *port, char *dbname,
-                char *username, char *pwdfile);
+                      char *username);
 
 /*
  *     Connecting to a Database
@@ -395,10 +396,6 @@ PQconndefaults(void)
  *
  *   PGPASSWORD   The user's password.
  *
- *   PGPASSWORDFILE
- *                A file that contains host:port:database:user:password
- *                for authentication
- *
  *   PGDATABASE   name of database to which to connect if 
  *                argument is NULL or a null string
  *
@@ -506,8 +503,7 @@ PQsetdbLogin(const char *pghost, const char *pgport, const char *pgoptions,
    else if ((tmp = getenv("PGPASSWORD")) != NULL)
        conn->pgpass = strdup(tmp);
    else if ((tmp = PasswordFromFile(conn->pghost, conn->pgport,
-                                    conn->dbName, conn->pguser,
-                                    getenv("PGPASSWORDFILE"))) != NULL)
+                                    conn->dbName, conn->pguser)))
        conn->pgpass = tmp;
    else
        conn->pgpass = strdup(DefaultPassword);
@@ -2905,22 +2901,20 @@ pwdfMatchesString(char *buf, char *token)
 
 /* get a password from the password file. */
 char *
-PasswordFromFile(char *hostname, char *port, char *dbname,
-                char *username, char *pwdfile)
+PasswordFromFile(char *hostname, char *port, char *dbname, char *username)
 {
    FILE       *fp;
+   char       *pgpassfile;
+   char       *home;
+   struct stat stat_buf;
 
 #define LINELEN NAMEDATALEN*5
    char        buf[LINELEN];
-   struct stat stat_buf;
-
-   if (pwdfile == NULL || strcmp(pwdfile, "") == 0)
-       return NULL;
 
-   if (dbname == NULL || strcmp(dbname, "") == 0)
+   if (dbname == NULL || strlen(dbname) == 0)
        return NULL;
 
-   if (username == NULL || strcmp(username, "") == 0)
+   if (username == NULL || strlen(username) == 0)
        return NULL;
 
    if (hostname == NULL)
@@ -2929,20 +2923,41 @@ PasswordFromFile(char *hostname, char *port, char *dbname,
    if (port == NULL)
        port = DEF_PGPORT_STR;
 
+   /* Look for it in the home dir */
+   home = getenv("HOME");
+   if (home)
+   {
+       pgpassfile = malloc(strlen(home) + strlen(PGPASSFILE) + 1);
+       if (!pgpassfile)
+       {
+           fprintf(stderr, gettext("%s: out of memory\n"), pset.progname);
+           exit(EXIT_FAILURE);
+       }
+   }
+   else
+       return NULL;
+
+   sprintf(pgpassfile, "%s" PGPASSFILE, home);
+
    /* If password file cannot be opened, ignore it. */
-   if (stat(pwdfile, &stat_buf) == -1)
+   if (stat(pgpassfile, &stat_buf) == -1)
+   {
+       free(pgpassfile);
        return NULL;
+   }
 
    /* If password file is insecure, alert the user and ignore it. */
    if (stat_buf.st_mode & (S_IRWXG | S_IRWXO))
    {
        fprintf(stderr,
                libpq_gettext("WARNING: Password file %s has world or group read access; permission should be u=rw (0600)"),
-               pwdfile);
+               pgpassfile);
+       free(pgpassfile);
        return NULL;
    }
 
-   fp = fopen(pwdfile, "r");
+   fp = fopen(pgpassfile, "r");
+   free(pgpassfile);
    if (fp == NULL)
        return NULL;
 
@@ -2965,6 +2980,7 @@ PasswordFromFile(char *hostname, char *port, char *dbname,
        fclose(fp);
        return ret;
    }
+
    fclose(fp);
    return NULL;