Skip to content

Commit f744e89

Browse files
committed
Introduce a psql "\connect -reuse-previous=on|off" option.
The decision to reuse values of parameters from a previous connection has been based on whether the new target is a conninfo string. Add this means of overriding that default. This feature arose as one component of a fix for security vulnerabilities in pg_dump, pg_dumpall, and pg_upgrade, so back-patch to 9.1 (all supported versions). In 9.3 and later, comment paragraphs that required update had already-incorrect claims about behavior when no connection is open; fix those problems. Security: CVE-2016-5424
1 parent 0cc3b12 commit f744e89

File tree

3 files changed

+89
-44
lines changed

3 files changed

+89
-44
lines changed

doc/src/sgml/ref/psql-ref.sgml

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -774,7 +774,7 @@ testdb=>
774774
</varlistentry>
775775

776776
<varlistentry>
777-
<term><literal>\c</literal> or <literal>\connect</literal> <literal>[ <replaceable class="parameter">dbname</replaceable> [ <replaceable class="parameter">username</replaceable> ] [ <replaceable class="parameter">host</replaceable> ] [ <replaceable class="parameter">port</replaceable> ] ] | <replaceable class="parameter">conninfo</replaceable> </literal></term>
777+
<term><literal>\c</literal> or <literal>\connect [ -reuse-previous=<replaceable class="parameter">on|off</replaceable> ] [ <replaceable class="parameter">dbname</replaceable> [ <replaceable class="parameter">username</replaceable> ] [ <replaceable class="parameter">host</replaceable> ] [ <replaceable class="parameter">port</replaceable> ] | <replaceable class="parameter">conninfo</replaceable> ]</literal></term>
778778
<listitem>
779779
<para>
780780
Establishes a new connection to a <productname>PostgreSQL</>
@@ -784,16 +784,19 @@ testdb=&gt;
784784
</para>
785785

786786
<para>
787-
When using positional parameters, if any of
788-
<replaceable class="parameter">dbname</replaceable>,
787+
Where the command omits database name, user, host, or port, the new
788+
connection can reuse values from the previous connection. By default,
789+
values from the previous connection are reused except when processing
790+
a <literal>conninfo</> string. Passing a first argument
791+
of <literal>-reuse-previous=on</>
792+
or <literal>-reuse-previous=off</literal> overrides that default.
793+
When the command neither specifies nor reuses a particular parameter,
794+
the <application>libpq</application> default is used. Specifying any
795+
of <replaceable class="parameter">dbname</replaceable>,
789796
<replaceable class="parameter">username</replaceable>,
790797
<replaceable class="parameter">host</replaceable> or
791-
<replaceable class="parameter">port</replaceable> are omitted or
792-
specified as <literal>-</literal>, the value of that parameter from
793-
the previous connection is used; if there is no previous connection,
794-
the <application>libpq</application> default for the parameter's value
795-
is used. When using <literal>conninfo</> strings, no values from the
796-
previous connection are used for the new connection.
798+
<replaceable class="parameter">port</replaceable>
799+
as <literal>-</literal> is equivalent to omitting that parameter.
797800
</para>
798801

799802
<para>

src/bin/psql/command.c

Lines changed: 76 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ static backslashResult exec_command(const char *cmd,
5858
PQExpBuffer query_buf);
5959
static bool do_edit(const char *filename_arg, PQExpBuffer query_buf,
6060
int lineno, bool *edited);
61-
static bool do_connect(char *dbname, char *user, char *host, char *port);
61+
static bool do_connect(enum trivalue reuse_previous_specification,
62+
char *dbname, char *user, char *host, char *port);
6263
static bool do_shell(const char *command);
6364
static bool lookup_function_oid(PGconn *conn, const char *desc, Oid *foid);
6465
static bool get_create_function_cmd(PGconn *conn, Oid oid, PQExpBuffer buf);
@@ -216,12 +217,9 @@ exec_command(const char *cmd,
216217
/*
217218
* \c or \connect -- connect to database using the specified parameters.
218219
*
219-
* \c dbname user host port
220+
* \c [-reuse-previous=BOOL] dbname user host port
220221
*
221-
* If any of these parameters are omitted or specified as '-', the current
222-
* value of the parameter will be used instead. If the parameter has no
223-
* current value, the default value for that parameter will be used. Some
224-
* examples:
222+
* Specifying a parameter as '-' is equivalent to omitting it. Examples:
225223
*
226224
* \c - - hst Connect to current database on current port of host
227225
* "hst" as current user. \c - usr - prt Connect to current database on
@@ -230,17 +228,31 @@ exec_command(const char *cmd,
230228
*/
231229
else if (strcmp(cmd, "c") == 0 || strcmp(cmd, "connect") == 0)
232230
{
231+
static const char prefix[] = "-reuse-previous=";
233232
char *opt1,
234233
*opt2,
235234
*opt3,
236235
*opt4;
236+
enum trivalue reuse_previous;
237237

238238
opt1 = read_connect_arg(scan_state);
239+
if (opt1 != NULL && strncmp(opt1, prefix, sizeof(prefix) - 1) == 0)
240+
{
241+
reuse_previous =
242+
ParseVariableBool(opt1 + sizeof(prefix) - 1, prefix) ?
243+
TRI_YES : TRI_NO;
244+
245+
free(opt1);
246+
opt1 = read_connect_arg(scan_state);
247+
}
248+
else
249+
reuse_previous = TRI_DEFAULT;
250+
239251
opt2 = read_connect_arg(scan_state);
240252
opt3 = read_connect_arg(scan_state);
241253
opt4 = read_connect_arg(scan_state);
242254

243-
success = do_connect(opt1, opt2, opt3, opt4);
255+
success = do_connect(reuse_previous, opt1, opt2, opt3, opt4);
244256

245257
free(opt1);
246258
free(opt2);
@@ -1492,34 +1504,57 @@ param_is_newly_set(const char *old_val, const char *new_val)
14921504
/*
14931505
* do_connect -- handler for \connect
14941506
*
1495-
* Connects to a database with given parameters. If there exists an
1496-
* established connection, NULL values will be replaced with the ones
1497-
* in the current connection. Otherwise NULL will be passed for that
1498-
* parameter to PQconnectdbParams(), so the libpq defaults will be used.
1507+
* Connects to a database with given parameters. Absent an established
1508+
* connection, all parameters are required. Given any of -reuse-previous=off,
1509+
* a connection string without -reuse-previous=on, or lack of an established
1510+
* connection, NULL values will pass through to PQconnectdbParams(), so the
1511+
* libpq defaults will be used. Otherwise, NULL values will be replaced with
1512+
* the ones in the current connection.
14991513
*
15001514
* In interactive mode, if connection fails with the given parameters,
15011515
* the old connection will be kept.
15021516
*/
15031517
static bool
1504-
do_connect(char *dbname, char *user, char *host, char *port)
1518+
do_connect(enum trivalue reuse_previous_specification,
1519+
char *dbname, char *user, char *host, char *port)
15051520
{
15061521
PGconn *o_conn = pset.db,
15071522
*n_conn;
15081523
char *password = NULL;
15091524
bool keep_password;
15101525
bool has_connection_string;
1526+
bool reuse_previous;
15111527

1512-
/* grab values from the old connection, unless supplied by caller */
1513-
if (!user)
1528+
has_connection_string = dbname ?
1529+
recognized_connection_string(dbname) : false;
1530+
switch (reuse_previous_specification)
1531+
{
1532+
case TRI_YES:
1533+
reuse_previous = true;
1534+
break;
1535+
case TRI_NO:
1536+
reuse_previous = false;
1537+
break;
1538+
default:
1539+
reuse_previous = !has_connection_string;
1540+
break;
1541+
}
1542+
/* Silently ignore arguments subsequent to a connection string. */
1543+
if (has_connection_string)
1544+
{
1545+
user = NULL;
1546+
host = NULL;
1547+
port = NULL;
1548+
}
1549+
1550+
/* grab missing values from the old connection */
1551+
if (!user && reuse_previous)
15141552
user = PQuser(o_conn);
1515-
if (!host)
1553+
if (!host && reuse_previous)
15161554
host = PQhost(o_conn);
1517-
if (!port)
1555+
if (!port && reuse_previous)
15181556
port = PQport(o_conn);
15191557

1520-
has_connection_string =
1521-
dbname ? recognized_connection_string(dbname) : false;
1522-
15231558
/*
15241559
* Any change in the parameters read above makes us discard the password.
15251560
* We also discard it if we're to use a conninfo rather than the
@@ -1536,10 +1571,10 @@ do_connect(char *dbname, char *user, char *host, char *port)
15361571
(port && PQport(o_conn) && strcmp(port, PQport(o_conn)) == 0);
15371572

15381573
/*
1539-
* Grab dbname from old connection unless supplied by caller. No password
1540-
* discard if this changes: passwords aren't (usually) database-specific.
1574+
* Grab missing dbname from old connection. No password discard if this
1575+
* changes: passwords aren't (usually) database-specific.
15411576
*/
1542-
if (!dbname)
1577+
if (!dbname && reuse_previous)
15431578
dbname = PQdb(o_conn);
15441579

15451580
/*
@@ -1566,20 +1601,27 @@ do_connect(char *dbname, char *user, char *host, char *port)
15661601
#define PARAMS_ARRAY_SIZE 8
15671602
const char **keywords = pg_malloc(PARAMS_ARRAY_SIZE * sizeof(*keywords));
15681603
const char **values = pg_malloc(PARAMS_ARRAY_SIZE * sizeof(*values));
1569-
int paramnum = 0;
1604+
int paramnum = -1;
15701605

1571-
keywords[0] = "dbname";
1572-
values[0] = dbname;
1606+
keywords[++paramnum] = "host";
1607+
values[paramnum] = host;
1608+
keywords[++paramnum] = "port";
1609+
values[paramnum] = port;
1610+
keywords[++paramnum] = "user";
1611+
values[paramnum] = user;
15731612

1574-
if (!has_connection_string)
1575-
{
1576-
keywords[++paramnum] = "host";
1577-
values[paramnum] = host;
1578-
keywords[++paramnum] = "port";
1579-
values[paramnum] = port;
1580-
keywords[++paramnum] = "user";
1581-
values[paramnum] = user;
1582-
}
1613+
/*
1614+
* Position in the array matters when the dbname is a connection
1615+
* string, because settings in a connection string override earlier
1616+
* array entries only. Thus, user= in the connection string always
1617+
* takes effect, but client_encoding= often will not.
1618+
*
1619+
* If you change this code, also change the initial-connection code in
1620+
* main(). For no good reason, a connection string password= takes
1621+
* precedence in main() but not here.
1622+
*/
1623+
keywords[++paramnum] = "dbname";
1624+
values[paramnum] = dbname;
15831625
keywords[++paramnum] = "password";
15841626
values[paramnum] = password;
15851627
keywords[++paramnum] = "fallback_application_name";

src/bin/psql/startup.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ main(int argc, char *argv[])
192192
values[2] = options.username;
193193
keywords[3] = "password";
194194
values[3] = password;
195-
keywords[4] = "dbname";
195+
keywords[4] = "dbname"; /* see do_connect() */
196196
values[4] = (options.action == ACT_LIST_DB &&
197197
options.dbname == NULL) ?
198198
"postgres" : options.dbname;

0 commit comments

Comments
 (0)