Cleanup up memory allocation for $HOME in related psql places.
Update mention of $HOME/.pgpass in the docs; add mention in pg_dumpall.
<!--
-$Header: /cvsroot/pgsql/doc/src/sgml/libpq.sgml,v 1.94 2002/09/02 20:21:32 tgl Exp $
+$Header: /cvsroot/pgsql/doc/src/sgml/libpq.sgml,v 1.95 2002/09/05 22:05:48 momjian Exp $
-->
<chapter id="libpq">
</listitem>
<listitem>
<para>
-<indexterm>
- <primary><envar>PGPASSWORDFILE</envar></primary>
-</indexterm>
-<envar>PGPASSWORDFILE</envar>
-sets the password file used if the backend demands password authentication.
-This file should have the format
-<screen>
-<replaceable>hostname</replaceable>:<replaceable>port</replaceable>:<replaceable>database</replaceable>:<replaceable>username</replaceable>:<replaceable>password</replaceable>
-</screen>
-Any of these may be a literal name, or a <literal>*</literal> that matches
-anything. The first match will be the one used, so put more specific entries first.
-Entries with <literal>:</literal> or <literal>\</literal> should be escaped
-with <literal>\</literal>.
-</para>
-</listitem>
-<listitem>
-<para>
<envar>PGREALM</envar> sets the Kerberos realm to use with
<productname>PostgreSQL</productname>, if it is different from the local realm.
If <envar>PGREALM</envar> is set, <productname>PostgreSQL</productname>
</sect1>
+
+<sect1 id="libpq-files">
+<title>Files</title>
+
+<indexterm zone="libpq-files">
+ <primary>files</primary>
+</indexterm>
+<para>
+<filename>$HOME/.pgpass</filename> is a file that can contain passwords
+to be used if the connection requires a password. This file should have the
+format:
+<screen>
+<replaceable>hostname</replaceable>:<replaceable>port</replaceable>:<replaceable>database</replaceable>:<replaceable>username</replaceable>:<replaceable>password</replaceable>
+</screen>
+Any of these may be a literal name, or <literal>*</literal>, which matches
+anything. The first match will be used so put more specific entries first.
+Entries with <literal>:</literal> or <literal>\</literal> should be escaped
+with <literal>\</literal>.
+</para>
+</sect1>
+
<sect1 id="libpq-threading">
<title>Threading Behavior</title>
<!--
-$Header: /cvsroot/pgsql/doc/src/sgml/ref/clusterdb.sgml,v 1.2 2002/09/03 01:11:37 tgl Exp $
+$Header: /cvsroot/pgsql/doc/src/sgml/ref/clusterdb.sgml,v 1.3 2002/09/05 22:05:50 momjian Exp $
PostgreSQL documentation
-->
</para>
<para>
- <application>clusterdb</application> will need to connect several times to the
- <productname>PostgreSQL</productname> 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.
+ <application>clusterdb</application> might need to connect several
+ times to the <productname>PostgreSQL</productname> server, asking for
+ a password each time. It is convenient to have a
+ <filename>$HOME/.pgpass</> file in such cases.
</para>
</refsect1>
<!--
-$Header: /cvsroot/pgsql/doc/src/sgml/ref/pg_dumpall.sgml,v 1.32 2002/08/27 18:57:26 petere Exp $
+$Header: /cvsroot/pgsql/doc/src/sgml/ref/pg_dumpall.sgml,v 1.33 2002/09/05 22:05:50 momjian Exp $
PostgreSQL documentation
-->
The SQL script will be written to the standard output. Shell
operators should be used to redirect it into a file.
</para>
+
+ <para>
+ <application>pg_dumpall</application> might need to connect several
+ times to the <productname>PostgreSQL</productname> server, asking for
+ a password each time. It is convenient to have a
+ <filename>$HOME/.pgpass</> file in such cases.
+ </para>
+
</refsect1>
<refsect1>
<!--
-$Header: /cvsroot/pgsql/doc/src/sgml/ref/vacuumdb.sgml,v 1.24 2002/08/27 03:55:17 momjian Exp $
+$Header: /cvsroot/pgsql/doc/src/sgml/ref/vacuumdb.sgml,v 1.25 2002/09/05 22:05:50 momjian Exp $
PostgreSQL documentation
-->
<para>
- <application>vacuumdb</application> will need to connect several times to the
- <productname>PostgreSQL</productname> server, asking for the password each
- time. It will probably be very convenient to have a PGPASSWORDFILE in that case.
+ <application>vacuumdb</application> might need to connect several
+ times to the <productname>PostgreSQL</productname> server, asking for
+ a password each time. It is convenient to have a
+ <filename>$HOME/.pgpass</> file in such cases.
</para>
</refsect1>
*
* 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"
static void finishInput(int, void *);
#endif
+#define PSQLHISTORY "/.psql_history"
+
/*
* gets_interactive()
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);
}
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);
}
*
* 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"
*/
PsqlSettings pset;
+#define PSQLRC "/.psqlrc"
/*
* Structures to pass information between the option parsing routine
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);
}
*
*
* 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 $
*
*-------------------------------------------------------------------------
*/
#define NOTIFYLIST_INITIAL_SIZE 10
#define NOTIFYLIST_GROWBY 10
+#define PGPASSFILE "/.pgpass"
/* ----------
* Definition of the conninfo parameters and their fallback resources.
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
*
* 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 <pgdatabase>
* argument is NULL or a null string
*
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);
/* 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)
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;
fclose(fp);
return ret;
}
+
fclose(fp);
return NULL;