Skip to content

Commit 777ac03

Browse files
committed
Fix recently-introduced breakage in psql's \connect command.
Through my misreading of what the existing code actually did, commits 85c5428 et al. broke psql's behavior for the case where "\c connstring" provides a password in the connstring. We should use that password in such a case, but as of 85c5428 we ignored it (and instead, prompted for a password). Commit 94929f1 fixed that in HEAD, but since I thought it was cleaning up a longstanding misbehavior and not one I'd just created, I didn't back-patch it. Hence, back-patch the portions of 94929f1 having to do with password management. In addition to fixing the introduced bug, this means that "\c -reuse-previous=on connstring" will allow re-use of an existing connection's password if the connstring doesn't change user/host/port. That didn't happen before, but it seems like a bug fix, and anyway I'm loath to have significant differences in this code across versions. Also fix an error with the same root cause about whether or not to override a connstring's setting of client_encoding. As of 85c5428 we always did so; restore the previous behavior of overriding only when stdin/stdout are a terminal and there's no environment setting of PGCLIENTENCODING. (I find that definition a bit surprising, but right now doesn't seem like the time to revisit it.) Per bug #16746 from Krzysztof Gradek. As with the previous patch, back-patch to all supported branches. Discussion: https://postgr.es/m/16746-44b30e2edf4335d4@postgresql.org
1 parent 5fe3e93 commit 777ac03

File tree

2 files changed

+51
-17
lines changed

2 files changed

+51
-17
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -911,6 +911,8 @@ testdb=>
911911
is changed from its previous value using the positional syntax,
912912
any <replaceable>hostaddr</replaceable> setting present in the
913913
existing connection's parameters is dropped.
914+
Also, any password used for the existing connection will be re-used
915+
only if the user, host, and port settings are not changed.
914916
When the command neither specifies nor reuses a particular parameter,
915917
the <application>libpq</application> default is used.
916918
</para>

src/bin/psql/command.c

Lines changed: 49 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2857,6 +2857,7 @@ do_connect(enum trivalue reuse_previous_specification,
28572857
int nconnopts = 0;
28582858
bool same_host = false;
28592859
char *password = NULL;
2860+
char *client_encoding;
28602861
bool success = true;
28612862
bool keep_password = true;
28622863
bool has_connection_string;
@@ -2927,6 +2928,7 @@ do_connect(enum trivalue reuse_previous_specification,
29272928
{
29282929
PQconninfoOption *ci;
29292930
PQconninfoOption *replci;
2931+
bool have_password = false;
29302932

29312933
for (ci = cinfo, replci = replcinfo;
29322934
ci->keyword && replci->keyword;
@@ -2945,6 +2947,26 @@ do_connect(enum trivalue reuse_previous_specification,
29452947

29462948
replci->val = ci->val;
29472949
ci->val = swap;
2950+
2951+
/*
2952+
* Check whether connstring provides options affecting
2953+
* password re-use. While any change in user, host,
2954+
* hostaddr, or port causes us to ignore the old
2955+
* connection's password, we don't force that for
2956+
* dbname, since passwords aren't database-specific.
2957+
*/
2958+
if (replci->val == NULL ||
2959+
strcmp(ci->val, replci->val) != 0)
2960+
{
2961+
if (strcmp(replci->keyword, "user") == 0 ||
2962+
strcmp(replci->keyword, "host") == 0 ||
2963+
strcmp(replci->keyword, "hostaddr") == 0 ||
2964+
strcmp(replci->keyword, "port") == 0)
2965+
keep_password = false;
2966+
}
2967+
/* Also note whether connstring contains a password. */
2968+
if (strcmp(replci->keyword, "password") == 0)
2969+
have_password = true;
29482970
}
29492971
}
29502972
Assert(ci->keyword == NULL && replci->keyword == NULL);
@@ -2954,8 +2976,13 @@ do_connect(enum trivalue reuse_previous_specification,
29542976

29552977
PQconninfoFree(replcinfo);
29562978

2957-
/* We never re-use a password with a conninfo string. */
2958-
keep_password = false;
2979+
/*
2980+
* If the connstring contains a password, tell the loop below
2981+
* that we may use it, regardless of other settings (i.e.,
2982+
* cinfo's password is no longer an "old" password).
2983+
*/
2984+
if (have_password)
2985+
keep_password = true;
29592986

29602987
/* Don't let code below try to inject dbname into params. */
29612988
dbname = NULL;
@@ -3043,14 +3070,16 @@ do_connect(enum trivalue reuse_previous_specification,
30433070
*/
30443071
password = prompt_for_password(has_connection_string ? NULL : user);
30453072
}
3046-
else if (o_conn && keep_password)
3047-
{
3048-
password = PQpass(o_conn);
3049-
if (password && *password)
3050-
password = pg_strdup(password);
3051-
else
3052-
password = NULL;
3053-
}
3073+
3074+
/*
3075+
* Consider whether to force client_encoding to "auto" (overriding
3076+
* anything in the connection string). We do so if we have a terminal
3077+
* connection and there is no PGCLIENTENCODING environment setting.
3078+
*/
3079+
if (pset.notty || getenv("PGCLIENTENCODING"))
3080+
client_encoding = NULL;
3081+
else
3082+
client_encoding = "auto";
30543083

30553084
/* Loop till we have a connection or fail, which we might've already */
30563085
while (success)
@@ -3062,12 +3091,12 @@ do_connect(enum trivalue reuse_previous_specification,
30623091

30633092
/*
30643093
* Copy non-default settings into the PQconnectdbParams parameter
3065-
* arrays; but override any values specified old-style, as well as the
3066-
* password and a couple of fields we want to set forcibly.
3094+
* arrays; but inject any values specified old-style, as well as any
3095+
* interactively-obtained password, and a couple of fields we want to
3096+
* set forcibly.
30673097
*
30683098
* If you change this code, see also the initial-connection code in
3069-
* main(). For no good reason, a connection string password= takes
3070-
* precedence in main() but not here.
3099+
* main().
30713100
*/
30723101
for (ci = cinfo; ci->keyword; ci++)
30733102
{
@@ -3086,12 +3115,15 @@ do_connect(enum trivalue reuse_previous_specification,
30863115
}
30873116
else if (port && strcmp(ci->keyword, "port") == 0)
30883117
values[paramnum++] = port;
3089-
else if (strcmp(ci->keyword, "password") == 0)
3118+
/* If !keep_password, we unconditionally drop old password */
3119+
else if ((password || !keep_password) &&
3120+
strcmp(ci->keyword, "password") == 0)
30903121
values[paramnum++] = password;
30913122
else if (strcmp(ci->keyword, "fallback_application_name") == 0)
30923123
values[paramnum++] = pset.progname;
3093-
else if (strcmp(ci->keyword, "client_encoding") == 0)
3094-
values[paramnum++] = (pset.notty || getenv("PGCLIENTENCODING")) ? NULL : "auto";
3124+
else if (client_encoding &&
3125+
strcmp(ci->keyword, "client_encoding") == 0)
3126+
values[paramnum++] = client_encoding;
30953127
else if (ci->val)
30963128
values[paramnum++] = ci->val;
30973129
/* else, don't bother making libpq parse this keyword */

0 commit comments

Comments
 (0)