Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit fcef161

Browse files
committed
psql: fix \connect with URIs and conninfo strings
psql was already accepting conninfo strings as the first parameter in \connect, but the way it worked wasn't sane; some of the other parameters would get the previous connection's values, causing it to connect to a completely unexpected server or, more likely, not finding any server at all because of completely wrong combinations of parameters. Fix by explicitely checking for a conninfo-looking parameter in the dbname position; if one is found, use its complete specification rather than mix with the other arguments. Also, change tab-completion to not try to complete conninfo/URI-looking "dbnames" and document that conninfos are accepted as first argument. There was a weak consensus to backpatch this, because while the behavior of using the dbname as a conninfo is nowhere documented for \connect, it is reasonable to expect that it works because it does work in many other contexts. Therefore this is backpatched all the way back to 9.0. To implement this, routines previously private to libpq have been duplicated so that psql can decide what looks like a conninfo/URI string. In back branches, just duplicate the same code all the way back to 9.2, where URIs where introduced; 9.0 and 9.1 have a simpler version. In master, the routines are moved to src/common and renamed. Author: David Fetter, Andrew Dunstan. Some editorialization by me (probably earning a Gierth's "Sloppy" badge in the process.) Reviewers: Andrew Gierth, Erik Rijkers, Pavel Stěhule, Stephen Frost, Robert Haas, Andrew Dunstan.
1 parent a0efc71 commit fcef161

File tree

8 files changed

+175
-79
lines changed

8 files changed

+175
-79
lines changed

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

+29-11
Original file line numberDiff line numberDiff line change
@@ -796,23 +796,31 @@ testdb=>
796796
</varlistentry>
797797

798798
<varlistentry>
799-
<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> ] ]</literal></term>
799+
<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>
800800
<listitem>
801801
<para>
802802
Establishes a new connection to a <productname>PostgreSQL</>
803-
server. If the new connection is successfully made, the
804-
previous connection is closed. If any of <replaceable
805-
class="parameter">dbname</replaceable>, <replaceable
806-
class="parameter">username</replaceable>, <replaceable
807-
class="parameter">host</replaceable> or <replaceable
808-
class="parameter">port</replaceable> are omitted or specified
809-
as <literal>-</literal>, the value of that parameter from the
810-
previous connection is used. If there is no previous
811-
connection, the <application>libpq</application> default for
812-
the parameter's value is used.
803+
server. The connection parameters to use can be specified either
804+
using a positional syntax, or using <literal>conninfo</> connection
805+
strings as detailed in <xref linkend="libpq-connstring">.
813806
</para>
814807

815808
<para>
809+
When using positional parameters, if any of
810+
<replaceable class="parameter">dbname</replaceable>,
811+
<replaceable class="parameter">username</replaceable>,
812+
<replaceable class="parameter">host</replaceable> or
813+
<replaceable class="parameter">port</replaceable> are omitted or
814+
specified as <literal>-</literal>, the value of that parameter from
815+
the previous connection is used; if there is no previous connection,
816+
the <application>libpq</application> default for the parameter's value
817+
is used. When using <literal>conninfo</> strings, no values from the
818+
previous connection are used for the new connection.
819+
</para>
820+
821+
<para>
822+
If the new connection is successfully made, the previous
823+
connection is closed.
816824
If the connection attempt failed (wrong user name, access
817825
denied, etc.), the previous connection will only be kept if
818826
<application>psql</application> is in interactive mode. When
@@ -822,6 +830,16 @@ testdb=&gt;
822830
mechanism that scripts are not accidentally acting on the
823831
wrong database on the other hand.
824832
</para>
833+
834+
<para>
835+
Examples:
836+
</para>
837+
<programlisting>
838+
=&gt; \c mydb myuser host.dom 6432
839+
=&gt; \c service=foo
840+
=&gt; \c "host=localhost port=5432 dbname=mydb connect_timeout=10 sslmode=disable"
841+
=&gt; \c postgresql://tom@localhost/mydb?application_name=myapp
842+
</programlisting>
825843
</listitem>
826844
</varlistentry>
827845

src/bin/psql/command.c

+57-23
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <sys/stat.h> /* for stat() */
3232
#endif
3333

34+
#include "common/connstrings.h"
3435
#include "portability/instr_time.h"
3536

3637
#include "libpq-fe.h"
@@ -1608,6 +1609,8 @@ do_connect(char *dbname, char *user, char *host, char *port)
16081609
PGconn *o_conn = pset.db,
16091610
*n_conn;
16101611
char *password = NULL;
1612+
bool keep_password;
1613+
bool has_connection_string;
16111614

16121615
if (!o_conn && (!dbname || !user || !host || !port))
16131616
{
@@ -1621,15 +1624,35 @@ do_connect(char *dbname, char *user, char *host, char *port)
16211624
return false;
16221625
}
16231626

1624-
if (!dbname)
1625-
dbname = PQdb(o_conn);
1627+
/* grab values from the old connection, unless supplied by caller */
16261628
if (!user)
16271629
user = PQuser(o_conn);
16281630
if (!host)
16291631
host = PQhost(o_conn);
16301632
if (!port)
16311633
port = PQport(o_conn);
16321634

1635+
has_connection_string =
1636+
dbname ? libpq_connstring_is_recognized(dbname) : false;
1637+
1638+
/*
1639+
* Any change in the parameters read above makes us discard the password.
1640+
* We also discard it if we're to use a conninfo rather than the positional
1641+
* syntax.
1642+
*/
1643+
keep_password =
1644+
((strcmp(user, PQuser(o_conn)) == 0) &&
1645+
(!host || strcmp(host, PQhost(o_conn)) == 0) &&
1646+
(strcmp(port, PQport(o_conn)) == 0) &&
1647+
!has_connection_string);
1648+
1649+
/*
1650+
* Grab dbname from old connection unless supplied by caller. No password
1651+
* discard if this changes: passwords aren't (usually) database-specific.
1652+
*/
1653+
if (!dbname)
1654+
dbname = PQdb(o_conn);
1655+
16331656
/*
16341657
* If the user asked to be prompted for a password, ask for one now. If
16351658
* not, use the password from the old connection, provided the username
@@ -1644,42 +1667,53 @@ do_connect(char *dbname, char *user, char *host, char *port)
16441667
{
16451668
password = prompt_for_password(user);
16461669
}
1647-
else if (o_conn && user && strcmp(PQuser(o_conn), user) == 0)
1670+
else if (o_conn && keep_password)
16481671
{
1649-
password = pg_strdup(PQpass(o_conn));
1672+
password = PQpass(o_conn);
1673+
if (password && *password)
1674+
password = pg_strdup(password);
1675+
else
1676+
password = NULL;
16501677
}
16511678

16521679
while (true)
16531680
{
16541681
#define PARAMS_ARRAY_SIZE 8
16551682
const char **keywords = pg_malloc(PARAMS_ARRAY_SIZE * sizeof(*keywords));
16561683
const char **values = pg_malloc(PARAMS_ARRAY_SIZE * sizeof(*values));
1684+
int paramnum = 0;
1685+
1686+
keywords[0] = "dbname";
1687+
values[0] = dbname;
1688+
1689+
if (!has_connection_string)
1690+
{
1691+
keywords[++paramnum] = "host";
1692+
values[paramnum] = host;
1693+
keywords[++paramnum] = "port";
1694+
values[paramnum] = port;
1695+
keywords[++paramnum] = "user";
1696+
values[paramnum] = user;
1697+
}
1698+
keywords[++paramnum] = "password";
1699+
values[paramnum] = password;
1700+
keywords[++paramnum] = "fallback_application_name";
1701+
values[paramnum] = pset.progname;
1702+
keywords[++paramnum] = "client_encoding";
1703+
values[paramnum] = (pset.notty || getenv("PGCLIENTENCODING")) ? NULL : "auto";
16571704

1658-
keywords[0] = "host";
1659-
values[0] = host;
1660-
keywords[1] = "port";
1661-
values[1] = port;
1662-
keywords[2] = "user";
1663-
values[2] = user;
1664-
keywords[3] = "password";
1665-
values[3] = password;
1666-
keywords[4] = "dbname";
1667-
values[4] = dbname;
1668-
keywords[5] = "fallback_application_name";
1669-
values[5] = pset.progname;
1670-
keywords[6] = "client_encoding";
1671-
values[6] = (pset.notty || getenv("PGCLIENTENCODING")) ? NULL : "auto";
1672-
keywords[7] = NULL;
1673-
values[7] = NULL;
1705+
/* add array terminator */
1706+
keywords[++paramnum] = NULL;
1707+
values[paramnum] = NULL;
16741708

16751709
n_conn = PQconnectdbParams(keywords, values, true);
16761710

1677-
free(keywords);
1678-
free(values);
1711+
pg_free(keywords);
1712+
pg_free(values);
16791713

16801714
/* We can immediately discard the password -- no longer needed */
16811715
if (password)
1682-
free(password);
1716+
pg_free(password);
16831717

16841718
if (PQstatus(n_conn) == CONNECTION_OK)
16851719
break;

src/bin/psql/help.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -260,11 +260,11 @@ slashUsage(unsigned short int pager)
260260

261261
fprintf(output, _("Connection\n"));
262262
if (currdb)
263-
fprintf(output, _(" \\c[onnect] [DBNAME|- USER|- HOST|- PORT|-]\n"
263+
fprintf(output, _(" \\c[onnect] {[DBNAME|- USER|- HOST|- PORT|-] | conninfo}\n"
264264
" connect to new database (currently \"%s\")\n"),
265265
currdb);
266266
else
267-
fprintf(output, _(" \\c[onnect] [DBNAME|- USER|- HOST|- PORT|-]\n"
267+
fprintf(output, _(" \\c[onnect] {[DBNAME|- USER|- HOST|- PORT|-] | conninfo}\n"
268268
" connect to new database (currently no connection)\n"));
269269
fprintf(output, _(" \\encoding [ENCODING] show or set client encoding\n"));
270270
fprintf(output, _(" \\password [USERNAME] securely change the password for a user\n"));

src/bin/psql/tab-complete.c

+11-2
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
#include "libpq-fe.h"
5353
#include "pqexpbuffer.h"
5454
#include "common.h"
55+
#include "common/connstrings.h"
5556
#include "settings.h"
5657
#include "stringutils.h"
5758

@@ -3706,10 +3707,18 @@ psql_completion(const char *text, int start, int end)
37063707

37073708
COMPLETE_WITH_LIST_CS(my_list);
37083709
}
3710+
37093711
else if (strcmp(prev_wd, "\\connect") == 0 || strcmp(prev_wd, "\\c") == 0)
3710-
COMPLETE_WITH_QUERY(Query_for_list_of_databases);
3712+
{
3713+
if (!libpq_connstring_is_recognized(text))
3714+
COMPLETE_WITH_QUERY(Query_for_list_of_databases);
3715+
/* TODO: URI/service completion. Nothing for now */
3716+
}
37113717
else if (strcmp(prev2_wd, "\\connect") == 0 || strcmp(prev2_wd, "\\c") == 0)
3712-
COMPLETE_WITH_QUERY(Query_for_list_of_roles);
3718+
{
3719+
if (!libpq_connstring_is_recognized(prev_wd))
3720+
COMPLETE_WITH_QUERY(Query_for_list_of_roles);
3721+
}
37133722

37143723
else if (strncmp(prev_wd, "\\da", strlen("\\da")) == 0)
37153724
COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_aggregates, NULL);

src/common/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ LIBS += $(PTHREAD_LIBS)
2626
OBJS_COMMON = exec.o pg_crc.o pg_lzcompress.o pgfnames.o psprintf.o relpath.o \
2727
rmtree.o string.o username.o wait_error.o
2828

29-
OBJS_FRONTEND = $(OBJS_COMMON) fe_memutils.o restricted_token.o
29+
OBJS_FRONTEND = $(OBJS_COMMON) fe_memutils.o restricted_token.o connstrings.o
3030

3131
OBJS_SRV = $(OBJS_COMMON:%.o=%_srv.o)
3232

src/common/connstrings.c

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* connstrings.c
3+
* connecting string processing functions
4+
*
5+
* Copyright (c) 2012-2015, PostgreSQL Global Development Group
6+
*
7+
* src/include/common/connstrings.c
8+
*/
9+
#include "postgres_fe.h"
10+
11+
#include <string.h>
12+
13+
#include "common/connstrings.h"
14+
15+
16+
/* The connection URI must start with either of the following designators: */
17+
static const char uri_designator[] = "postgresql://";
18+
static const char short_uri_designator[] = "postgres://";
19+
20+
21+
/*
22+
* Checks if connection string starts with either of the valid URI prefix
23+
* designators.
24+
*
25+
* Returns the URI prefix length, 0 if the string doesn't contain a URI prefix.
26+
*/
27+
int
28+
libpq_connstring_uri_prefix_length(const char *connstr)
29+
{
30+
if (strncmp(connstr, uri_designator,
31+
sizeof(uri_designator) - 1) == 0)
32+
return sizeof(uri_designator) - 1;
33+
34+
if (strncmp(connstr, short_uri_designator,
35+
sizeof(short_uri_designator) - 1) == 0)
36+
return sizeof(short_uri_designator) - 1;
37+
38+
return 0;
39+
}
40+
41+
/*
42+
* Recognized connection string either starts with a valid URI prefix or
43+
* contains a "=" in it.
44+
*
45+
* Must be consistent with parse_connection_string: anything for which this
46+
* returns true should at least look like it's parseable by that routine.
47+
*/
48+
bool
49+
libpq_connstring_is_recognized(const char *connstr)
50+
{
51+
return libpq_connstring_uri_prefix_length(connstr) != 0 ||
52+
strchr(connstr, '=') != NULL;
53+
}

src/include/common/connstrings.h

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* connstrings.h
3+
* connecting string processing prototypes
4+
*
5+
* Copyright (c) 2012-2015, PostgreSQL Global Development Group
6+
*
7+
* src/include/common/connstrings.h
8+
*/
9+
#ifndef CONNSTRINGS_H
10+
#define CONNSTRINGS_H
11+
12+
13+
extern int libpq_connstring_uri_prefix_length(const char *connstr);
14+
extern bool libpq_connstring_is_recognized(const char *connstr);
15+
16+
#endif /* CONNSTRINGS_H */

0 commit comments

Comments
 (0)