Skip to content

Commit 4ec457a

Browse files
committed
Fix regression in .pgpass support. From Neil Conway.
1 parent 887edf4 commit 4ec457a

File tree

2 files changed

+63
-31
lines changed

2 files changed

+63
-31
lines changed

doc/src/sgml/libpq.sgml

Lines changed: 35 additions & 12 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.106 2003/01/19 00:13:28 momjian Exp $
2+
$Header: /cvsroot/pgsql/doc/src/sgml/libpq.sgml,v 1.107 2003/01/30 19:49:54 tgl Exp $
33
-->
44

55
<chapter id="libpq">
@@ -203,9 +203,12 @@ PGconn *PQconnectdb(const char *conninfo)
203203
<term><literal>requiressl</literal></term>
204204
<listitem>
205205
<para>
206-
Set to 1 to require SSL connection to the backend. <application>Libpq</>
207-
will then refuse to connect if the server does not support
208-
SSL. Set to 0 (default) to negotiate with server.
206+
Set to 1 to require <acronym>SSL</acronym> connection to the server.
207+
<application>Libpq</> will then refuse to connect if the server does not
208+
accept an <acronym>SSL</acronym> connection.
209+
Set to 0 (default) to negotiate with server.
210+
This option is only available if
211+
<productname>PostgreSQL</> is compiled with SSL support.
209212
</para>
210213
</listitem>
211214
</varlistentry>
@@ -2010,10 +2013,11 @@ routines like <function>PQgetvalue</function>.
20102013

20112014
<para>
20122015
The following environment variables can be used to select default
2013-
connection parameter values, which will be used by <function>PQconnectdb</function> or
2014-
<function>PQsetdbLogin</function> if no value is directly specified by the calling code.
2015-
These are useful to avoid hard-coding database names into simple
2016-
application programs.
2016+
connection parameter values, which will be used by
2017+
<function>PQconnectdb</>, <function>PQsetdbLogin</> and
2018+
<function>PQsetdb</> if no value is directly specified by the calling
2019+
code. These are useful to avoid hard-coding database connection
2020+
information into simple client applications.
20172021

20182022
<itemizedlist>
20192023
<listitem>
@@ -2091,6 +2095,25 @@ the <productname>PostgreSQL</productname> backend.
20912095
messages from the backend server are displayed.
20922096
</para>
20932097
</listitem>
2098+
<listitem>
2099+
<para>
2100+
<envar>PGREQUIRESSL</envar> sets whether or not the connection must be
2101+
made over <acronym>SSL</acronym>. If set to
2102+
<quote>1</quote>, <application>libpq</>
2103+
will refuse to connect if the server does not accept
2104+
an <acronym>SSL</acronym> connection.
2105+
This option is only available if
2106+
<productname>PostgreSQL</> is compiled with SSL support.
2107+
</para>
2108+
</listitem>
2109+
<listitem>
2110+
<para>
2111+
<envar>PGCONNECT_TIMEOUT</envar> sets the maximum number of seconds
2112+
that <application>libpq</application> will wait when attempting to
2113+
connect to the <productname>PostgreSQL</productname> server. This
2114+
option should be set to at least 2 seconds.
2115+
</para>
2116+
</listitem>
20942117
</itemizedlist>
20952118
</para>
20962119

@@ -2161,10 +2184,10 @@ a password. This file should have the format:
21612184
<synopsis>
21622185
<replaceable>hostname</replaceable>:<replaceable>port</replaceable>:<replaceable>database</replaceable>:<replaceable>username</replaceable>:<replaceable>password</replaceable>
21632186
</synopsis>
2164-
Any of these may be a literal name, or <literal>*</literal>, which matches
2165-
anything. The first match will be used so put more specific entries first.
2166-
Entries with <literal>:</literal> or <literal>\</literal> should be escaped
2167-
with <literal>\</literal>.
2187+
Any of these may be a literal name, or <literal>*</literal>, which
2188+
matches anything. The first matching entry will be used, so put more-specific
2189+
entries first. When an entry contains <literal>:</literal> or
2190+
<literal>\</literal>, it must be escaped with <literal>\</literal>.
21682191
</para>
21692192
<para>
21702193
The permissions on <filename>.pgpass</filename> must disallow any

src/interfaces/libpq/fe-connect.c

Lines changed: 28 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.221 2003/01/08 21:33:27 momjian Exp $
11+
* $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v 1.222 2003/01/30 19:49:54 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -123,7 +123,7 @@ static const PQconninfoOption PQconninfoOptions[] = {
123123
"Database-Password", "*", 20},
124124

125125
{"connect_timeout", "PGCONNECT_TIMEOUT", NULL, NULL,
126-
"Connect-timeout", "", 10}, /* strlen( INT32_MAX) == 10 */
126+
"Connect-timeout", "", 10}, /* strlen(INT32_MAX) == 10 */
127127

128128
{"dbname", "PGDATABASE", NULL, NULL,
129129
"Database-Name", "", 20},
@@ -315,8 +315,14 @@ PQconnectStart(const char *conninfo)
315315
tmp = conninfo_getval(connOptions, "password");
316316
conn->pgpass = tmp ? strdup(tmp) : NULL;
317317
if (conn->pgpass == NULL || conn->pgpass[0] == '\0')
318+
{
319+
if (conn->pgpass)
320+
free(conn->pgpass);
318321
conn->pgpass = PasswordFromFile(conn->pghost, conn->pgport,
319-
conn->dbName, conn->pguser);
322+
conn->dbName, conn->pguser);
323+
if (conn->pgpass == NULL)
324+
conn->pgpass = strdup(DefaultPassword);
325+
}
320326
tmp = conninfo_getval(connOptions, "connect_timeout");
321327
conn->connect_timeout = tmp ? strdup(tmp) : NULL;
322328
#ifdef USE_SSL
@@ -506,14 +512,13 @@ PQsetdbLogin(const char *pghost, const char *pgport, const char *pgoptions,
506512
else
507513
conn->dbName = strdup(dbName);
508514

509-
/*
510-
* getPasswordFromFile mallocs its result, so we don't need strdup
511-
* here
512-
*/
513515
if (pwd)
514516
conn->pgpass = strdup(pwd);
515517
else if ((tmp = getenv("PGPASSWORD")) != NULL)
516518
conn->pgpass = strdup(tmp);
519+
else if ((tmp = PasswordFromFile(conn->pghost, conn->pgport,
520+
conn->dbName, conn->pguser)) != NULL)
521+
conn->pgpass = tmp;
517522
else
518523
conn->pgpass = strdup(DefaultPassword);
519524

@@ -2946,7 +2951,7 @@ pwdfMatchesString(char *buf, char *token)
29462951
return NULL;
29472952
}
29482953

2949-
/* get a password from the password file. */
2954+
/* Get a password from the password file. Return value is malloc'd. */
29502955
char *
29512956
PasswordFromFile(char *hostname, char *port, char *dbname, char *username)
29522957
{
@@ -2972,17 +2977,15 @@ PasswordFromFile(char *hostname, char *port, char *dbname, char *username)
29722977

29732978
/* Look for it in the home dir */
29742979
home = getenv("HOME");
2975-
if (home)
2980+
if (!home)
2981+
return NULL;
2982+
2983+
pgpassfile = malloc(strlen(home) + 1 + strlen(PGPASSFILE) + 1);
2984+
if (!pgpassfile)
29762985
{
2977-
pgpassfile = malloc(strlen(home) + 1 + strlen(PGPASSFILE) + 1);
2978-
if (!pgpassfile)
2979-
{
2980-
fprintf(stderr, libpq_gettext("out of memory\n"));
2981-
return NULL;
2982-
}
2983-
}
2984-
else
2986+
fprintf(stderr, libpq_gettext("out of memory\n"));
29852987
return NULL;
2988+
}
29862989

29872990
sprintf(pgpassfile, "%s/%s", home, PGPASSFILE);
29882991

@@ -3014,12 +3017,18 @@ PasswordFromFile(char *hostname, char *port, char *dbname, char *username)
30143017
{
30153018
char *t = buf,
30163019
*ret;
3020+
int len;
30173021

30183022
fgets(buf, LINELEN - 1, fp);
3019-
if (strlen(buf) == 0)
3023+
3024+
len = strlen(buf);
3025+
if (len == 0)
30203026
continue;
30213027

3022-
buf[strlen(buf) - 1] = 0;
3028+
/* Remove trailing newline */
3029+
if (buf[len - 1] == '\n')
3030+
buf[len - 1] = 0;
3031+
30233032
if ((t = pwdfMatchesString(t, hostname)) == NULL ||
30243033
(t = pwdfMatchesString(t, port)) == NULL ||
30253034
(t = pwdfMatchesString(t, dbname)) == NULL ||

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