diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2021-11-12 14:55:32 -0500 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2021-11-12 14:55:32 -0500 |
commit | d6eb5a0c258d3da5471814bcc6ed6498129fee16 (patch) | |
tree | e9857937e0901833461e6ad3d0b9cf91493a0a76 /src | |
parent | f8abb0f5e114d8c309239f0faa277b97f696d829 (diff) | |
download | postgresql-d6eb5a0c258d3da5471814bcc6ed6498129fee16.tar.gz postgresql-d6eb5a0c258d3da5471814bcc6ed6498129fee16.zip |
Make psql's \password default to CURRENT_USER, not PQuser(conn).
The documentation says plainly that \password acts on "the current user"
by default. What it actually acted on, or tried to, was the username
used to log into the current session. This is not the same thing if
one has since done SET ROLE or SET SESSION AUTHENTICATION. Aside from
the possible surprise factor, it's quite likely that the current role
doesn't have permissions to set the password of the original role.
To fix, use "SELECT CURRENT_USER" to get the role name to act on.
(This syntax works with servers at least back to 7.0.) Also, in
hopes of reducing confusion, include the role name that will be
acted on in the password prompt.
The discrepancy from the documentation makes this a bug, so
back-patch to all supported branches.
Patch by me; thanks to Nathan Bossart for review.
Discussion: https://postgr.es/m/747443.1635536754@sss.pgh.pa.us
Diffstat (limited to 'src')
-rw-r--r-- | src/bin/psql/command.c | 34 |
1 files changed, 21 insertions, 13 deletions
diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c index 49d4c0e3ce2..3de9d096fd3 100644 --- a/src/bin/psql/command.c +++ b/src/bin/psql/command.c @@ -2023,12 +2023,29 @@ exec_command_password(PsqlScanState scan_state, bool active_branch) if (active_branch) { - char *opt0 = psql_scan_slash_option(scan_state, + char *user = psql_scan_slash_option(scan_state, OT_SQLID, NULL, true); char *pw1; char *pw2; + PQExpBufferData buf; + + if (user == NULL) + { + /* By default, the command applies to CURRENT_USER */ + PGresult *res; + + res = PSQLexec("SELECT CURRENT_USER"); + if (!res) + return PSQL_CMD_ERROR; + + user = pg_strdup(PQgetvalue(res, 0, 0)); + PQclear(res); + } - pw1 = simple_prompt("Enter new password: ", false); + initPQExpBuffer(&buf); + printfPQExpBuffer(&buf, _("Enter new password for user \"%s\": "), user); + + pw1 = simple_prompt(buf.data, false); pw2 = simple_prompt("Enter it again: ", false); if (strcmp(pw1, pw2) != 0) @@ -2038,14 +2055,8 @@ exec_command_password(PsqlScanState scan_state, bool active_branch) } else { - char *user; char *encrypted_password; - if (opt0) - user = opt0; - else - user = PQuser(pset.db); - encrypted_password = PQencryptPasswordConn(pset.db, pw1, user, NULL); if (!encrypted_password) @@ -2055,15 +2066,12 @@ exec_command_password(PsqlScanState scan_state, bool active_branch) } else { - PQExpBufferData buf; PGresult *res; - initPQExpBuffer(&buf); printfPQExpBuffer(&buf, "ALTER USER %s PASSWORD ", fmtId(user)); appendStringLiteralConn(&buf, encrypted_password, pset.db); res = PSQLexec(buf.data); - termPQExpBuffer(&buf); if (!res) success = false; else @@ -2072,10 +2080,10 @@ exec_command_password(PsqlScanState scan_state, bool active_branch) } } - if (opt0) - free(opt0); + free(user); free(pw1); free(pw2); + termPQExpBuffer(&buf); } else ignore_slash_options(scan_state); |