Skip to content

Commit 5fc10c3

Browse files
committed
Remove PGPASSWORDFILE and default to always trying $HOME/.pgpass.
Cleanup up memory allocation for $HOME in related psql places. Update mention of $HOME/.pgpass in the docs; add mention in pg_dumpall.
1 parent f4aecac commit 5fc10c3

File tree

7 files changed

+91
-56
lines changed

7 files changed

+91
-56
lines changed

doc/src/sgml/libpq.sgml

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$Header: /cvsroot/pgsql/doc/src/sgml/libpq.sgml,v 1.94 2002/09/02 20:21:32 tgl Exp $
2+
$Header: /cvsroot/pgsql/doc/src/sgml/libpq.sgml,v 1.95 2002/09/05 22:05:48 momjian Exp $
33
-->
44

55
<chapter id="libpq">
@@ -2052,23 +2052,6 @@ reasons; consider migrating to <envar>PGPASSWORDFILE</envar>.
20522052
</listitem>
20532053
<listitem>
20542054
<para>
2055-
<indexterm>
2056-
<primary><envar>PGPASSWORDFILE</envar></primary>
2057-
</indexterm>
2058-
<envar>PGPASSWORDFILE</envar>
2059-
sets the password file used if the backend demands password authentication.
2060-
This file should have the format
2061-
<screen>
2062-
<replaceable>hostname</replaceable>:<replaceable>port</replaceable>:<replaceable>database</replaceable>:<replaceable>username</replaceable>:<replaceable>password</replaceable>
2063-
</screen>
2064-
Any of these may be a literal name, or a <literal>*</literal> that matches
2065-
anything. The first match will be the one used, so put more specific entries first.
2066-
Entries with <literal>:</literal> or <literal>\</literal> should be escaped
2067-
with <literal>\</literal>.
2068-
</para>
2069-
</listitem>
2070-
<listitem>
2071-
<para>
20722055
<envar>PGREALM</envar> sets the Kerberos realm to use with
20732056
<productname>PostgreSQL</productname>, if it is different from the local realm.
20742057
If <envar>PGREALM</envar> is set, <productname>PostgreSQL</productname>
@@ -2141,6 +2124,27 @@ for information on correct values for these environment variables.
21412124

21422125
</sect1>
21432126

2127+
2128+
<sect1 id="libpq-files">
2129+
<title>Files</title>
2130+
2131+
<indexterm zone="libpq-files">
2132+
<primary>files</primary>
2133+
</indexterm>
2134+
<para>
2135+
<filename>$HOME/.pgpass</filename> is a file that can contain passwords
2136+
to be used if the connection requires a password. This file should have the
2137+
format:
2138+
<screen>
2139+
<replaceable>hostname</replaceable>:<replaceable>port</replaceable>:<replaceable>database</replaceable>:<replaceable>username</replaceable>:<replaceable>password</replaceable>
2140+
</screen>
2141+
Any of these may be a literal name, or <literal>*</literal>, which matches
2142+
anything. The first match will be used so put more specific entries first.
2143+
Entries with <literal>:</literal> or <literal>\</literal> should be escaped
2144+
with <literal>\</literal>.
2145+
</para>
2146+
</sect1>
2147+
21442148
<sect1 id="libpq-threading">
21452149
<title>Threading Behavior</title>
21462150

doc/src/sgml/ref/clusterdb.sgml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$Header: /cvsroot/pgsql/doc/src/sgml/ref/clusterdb.sgml,v 1.2 2002/09/03 01:11:37 tgl Exp $
2+
$Header: /cvsroot/pgsql/doc/src/sgml/ref/clusterdb.sgml,v 1.3 2002/09/05 22:05:50 momjian Exp $
33
PostgreSQL documentation
44
-->
55

@@ -54,10 +54,10 @@ PostgreSQL documentation
5454
</para>
5555

5656
<para>
57-
<application>clusterdb</application> will need to connect several times to the
58-
<productname>PostgreSQL</productname> server. If you are using password
59-
authentication, it will ask for the password each time. It will probably be
60-
very convenient to have a PGPASSWORDFILE in that case.
57+
<application>clusterdb</application> might need to connect several
58+
times to the <productname>PostgreSQL</productname> server, asking for
59+
a password each time. It is convenient to have a
60+
<filename>$HOME/.pgpass</> file in such cases.
6161
</para>
6262

6363
</refsect1>

doc/src/sgml/ref/pg_dumpall.sgml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$Header: /cvsroot/pgsql/doc/src/sgml/ref/pg_dumpall.sgml,v 1.32 2002/08/27 18:57:26 petere Exp $
2+
$Header: /cvsroot/pgsql/doc/src/sgml/ref/pg_dumpall.sgml,v 1.33 2002/09/05 22:05:50 momjian Exp $
33
PostgreSQL documentation
44
-->
55

@@ -61,6 +61,14 @@ PostgreSQL documentation
6161
The SQL script will be written to the standard output. Shell
6262
operators should be used to redirect it into a file.
6363
</para>
64+
65+
<para>
66+
<application>pg_dumpall</application> might need to connect several
67+
times to the <productname>PostgreSQL</productname> server, asking for
68+
a password each time. It is convenient to have a
69+
<filename>$HOME/.pgpass</> file in such cases.
70+
</para>
71+
6472
</refsect1>
6573

6674
<refsect1>

doc/src/sgml/ref/vacuumdb.sgml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$Header: /cvsroot/pgsql/doc/src/sgml/ref/vacuumdb.sgml,v 1.24 2002/08/27 03:55:17 momjian Exp $
2+
$Header: /cvsroot/pgsql/doc/src/sgml/ref/vacuumdb.sgml,v 1.25 2002/09/05 22:05:50 momjian Exp $
33
PostgreSQL documentation
44
-->
55

@@ -62,9 +62,10 @@ PostgreSQL documentation
6262

6363

6464
<para>
65-
<application>vacuumdb</application> will need to connect several times to the
66-
<productname>PostgreSQL</productname> server, asking for the password each
67-
time. It will probably be very convenient to have a PGPASSWORDFILE in that case.
65+
<application>vacuumdb</application> might need to connect several
66+
times to the <productname>PostgreSQL</productname> server, asking for
67+
a password each time. It is convenient to have a
68+
<filename>$HOME/.pgpass</> file in such cases.
6869
</para>
6970

7071
</refsect1>

src/bin/psql/input.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright 2000 by PostgreSQL Global Development Group
55
*
6-
* $Header: /cvsroot/pgsql/src/bin/psql/input.c,v 1.19 2002/04/10 22:46:58 petere Exp $
6+
* $Header: /cvsroot/pgsql/src/bin/psql/input.c,v 1.20 2002/09/05 22:05:50 momjian Exp $
77
*/
88
#include "postgres_fe.h"
99
#include "input.h"
@@ -30,6 +30,8 @@ static void finishInput(void);
3030
static void finishInput(int, void *);
3131
#endif
3232

33+
#define PSQLHISTORY "/.psql_history"
34+
3335

3436
/*
3537
* gets_interactive()
@@ -142,11 +144,12 @@ initializeInput(int flags)
142144
home = getenv("HOME");
143145
if (home)
144146
{
145-
char *psql_history = (char *) malloc(strlen(home) + 20);
147+
char *psql_history = (char *) malloc(strlen(home) +
148+
strlen(PSQLHISTORY) + 1);
146149

147150
if (psql_history)
148151
{
149-
sprintf(psql_history, "%s/.psql_history", home);
152+
sprintf(psql_history, "%s" PSQLHISTORY, home);
150153
read_history(psql_history);
151154
free(psql_history);
152155
}
@@ -201,14 +204,15 @@ finishInput(int exitstatus, void *arg)
201204
home = getenv("HOME");
202205
if (home)
203206
{
204-
psql_history = (char *) malloc(strlen(home) + 20);
207+
psql_history = (char *) malloc(strlen(home) +
208+
strlen(PSQLHISTORY) + 1);
205209
if (psql_history)
206210
{
207211
const char *var = GetVariable(pset.vars, "HISTSIZE");
208212

209213
if (var)
210214
stifle_history(atoi(var));
211-
sprintf(psql_history, "%s/.psql_history", home);
215+
sprintf(psql_history, "%s" PSQLHISTORY, home);
212216
write_history(psql_history);
213217
free(psql_history);
214218
}

src/bin/psql/startup.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright 2000 by PostgreSQL Global Development Group
55
*
6-
* $Header: /cvsroot/pgsql/src/bin/psql/startup.c,v 1.64 2002/09/04 20:31:36 momjian Exp $
6+
* $Header: /cvsroot/pgsql/src/bin/psql/startup.c,v 1.65 2002/09/05 22:05:50 momjian Exp $
77
*/
88
#include "postgres_fe.h"
99

@@ -42,6 +42,7 @@
4242
*/
4343
PsqlSettings pset;
4444

45+
#define PSQLRC "/.psqlrc"
4546

4647
/*
4748
* Structures to pass information between the option parsing routine
@@ -604,19 +605,20 @@ process_psqlrc(void)
604605

605606
if (home)
606607
{
607-
psqlrc = malloc(strlen(home) + 20);
608+
psqlrc = malloc(strlen(home) + strlen(PSQLRC) + 1 +
609+
strlen(PG_VERSION) + 1);
608610
if (!psqlrc)
609611
{
610612
fprintf(stderr, gettext("%s: out of memory\n"), pset.progname);
611613
exit(EXIT_FAILURE);
612614
}
613615

614-
sprintf(psqlrc, "%s/.psqlrc-" PG_VERSION, home);
616+
sprintf(psqlrc, "%s" PSQLRC "-" PG_VERSION, home);
615617
if (access(psqlrc, R_OK) == 0)
616618
process_file(psqlrc);
617619
else
618620
{
619-
sprintf(psqlrc, "%s/.psqlrc", home);
621+
sprintf(psqlrc, "%s" PSQLRC, home);
620622
if (access(psqlrc, R_OK) == 0)
621623
process_file(psqlrc);
622624
}

src/interfaces/libpq/fe-connect.c

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v 1.201 2002/09/04 20:31:46 momjian Exp $
11+
* $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v 1.202 2002/09/05 22:05:50 momjian Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -66,6 +66,7 @@ inet_aton(const char *cp, struct in_addr * inp)
6666
#define NOTIFYLIST_INITIAL_SIZE 10
6767
#define NOTIFYLIST_GROWBY 10
6868

69+
#define PGPASSFILE "/.pgpass"
6970

7071
/* ----------
7172
* Definition of the conninfo parameters and their fallback resources.
@@ -186,7 +187,7 @@ static int parseServiceInfo(PQconninfoOption *options,
186187
PQExpBuffer errorMessage);
187188
char *pwdfMatchesString(char *buf, char *token);
188189
char *PasswordFromFile(char *hostname, char *port, char *dbname,
189-
char *username, char *pwdfile);
190+
char *username);
190191

191192
/*
192193
* Connecting to a Database
@@ -395,10 +396,6 @@ PQconndefaults(void)
395396
*
396397
* PGPASSWORD The user's password.
397398
*
398-
* PGPASSWORDFILE
399-
* A file that contains host:port:database:user:password
400-
* for authentication
401-
*
402399
* PGDATABASE name of database to which to connect if <pgdatabase>
403400
* argument is NULL or a null string
404401
*
@@ -506,8 +503,7 @@ PQsetdbLogin(const char *pghost, const char *pgport, const char *pgoptions,
506503
else if ((tmp = getenv("PGPASSWORD")) != NULL)
507504
conn->pgpass = strdup(tmp);
508505
else if ((tmp = PasswordFromFile(conn->pghost, conn->pgport,
509-
conn->dbName, conn->pguser,
510-
getenv("PGPASSWORDFILE"))) != NULL)
506+
conn->dbName, conn->pguser)))
511507
conn->pgpass = tmp;
512508
else
513509
conn->pgpass = strdup(DefaultPassword);
@@ -2905,22 +2901,20 @@ pwdfMatchesString(char *buf, char *token)
29052901

29062902
/* get a password from the password file. */
29072903
char *
2908-
PasswordFromFile(char *hostname, char *port, char *dbname,
2909-
char *username, char *pwdfile)
2904+
PasswordFromFile(char *hostname, char *port, char *dbname, char *username)
29102905
{
29112906
FILE *fp;
2907+
char *pgpassfile;
2908+
char *home;
2909+
struct stat stat_buf;
29122910

29132911
#define LINELEN NAMEDATALEN*5
29142912
char buf[LINELEN];
2915-
struct stat stat_buf;
2916-
2917-
if (pwdfile == NULL || strcmp(pwdfile, "") == 0)
2918-
return NULL;
29192913

2920-
if (dbname == NULL || strcmp(dbname, "") == 0)
2914+
if (dbname == NULL || strlen(dbname) == 0)
29212915
return NULL;
29222916

2923-
if (username == NULL || strcmp(username, "") == 0)
2917+
if (username == NULL || strlen(username) == 0)
29242918
return NULL;
29252919

29262920
if (hostname == NULL)
@@ -2929,20 +2923,41 @@ PasswordFromFile(char *hostname, char *port, char *dbname,
29292923
if (port == NULL)
29302924
port = DEF_PGPORT_STR;
29312925

2926+
/* Look for it in the home dir */
2927+
home = getenv("HOME");
2928+
if (home)
2929+
{
2930+
pgpassfile = malloc(strlen(home) + strlen(PGPASSFILE) + 1);
2931+
if (!pgpassfile)
2932+
{
2933+
fprintf(stderr, gettext("%s: out of memory\n"), pset.progname);
2934+
exit(EXIT_FAILURE);
2935+
}
2936+
}
2937+
else
2938+
return NULL;
2939+
2940+
sprintf(pgpassfile, "%s" PGPASSFILE, home);
2941+
29322942
/* If password file cannot be opened, ignore it. */
2933-
if (stat(pwdfile, &stat_buf) == -1)
2943+
if (stat(pgpassfile, &stat_buf) == -1)
2944+
{
2945+
free(pgpassfile);
29342946
return NULL;
2947+
}
29352948

29362949
/* If password file is insecure, alert the user and ignore it. */
29372950
if (stat_buf.st_mode & (S_IRWXG | S_IRWXO))
29382951
{
29392952
fprintf(stderr,
29402953
libpq_gettext("WARNING: Password file %s has world or group read access; permission should be u=rw (0600)"),
2941-
pwdfile);
2954+
pgpassfile);
2955+
free(pgpassfile);
29422956
return NULL;
29432957
}
29442958

2945-
fp = fopen(pwdfile, "r");
2959+
fp = fopen(pgpassfile, "r");
2960+
free(pgpassfile);
29462961
if (fp == NULL)
29472962
return NULL;
29482963

@@ -2965,6 +2980,7 @@ PasswordFromFile(char *hostname, char *port, char *dbname,
29652980
fclose(fp);
29662981
return ret;
29672982
}
2983+
29682984
fclose(fp);
29692985
return NULL;
29702986

0 commit comments

Comments
 (0)