Skip to content

Commit 523adcc

Browse files
committed
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
1 parent 2d60ce3 commit 523adcc

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

src/bin/psql/command.c

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1824,12 +1824,29 @@ exec_command_password(PsqlScanState scan_state, bool active_branch)
18241824

18251825
if (active_branch)
18261826
{
1827-
char *opt0 = psql_scan_slash_option(scan_state,
1827+
char *user = psql_scan_slash_option(scan_state,
18281828
OT_SQLID, NULL, true);
18291829
char pw1[100];
18301830
char pw2[100];
1831+
PQExpBufferData buf;
1832+
1833+
if (user == NULL)
1834+
{
1835+
/* By default, the command applies to CURRENT_USER */
1836+
PGresult *res;
1837+
1838+
res = PSQLexec("SELECT CURRENT_USER");
1839+
if (!res)
1840+
return PSQL_CMD_ERROR;
1841+
1842+
user = pg_strdup(PQgetvalue(res, 0, 0));
1843+
PQclear(res);
1844+
}
18311845

1832-
simple_prompt("Enter new password: ", pw1, sizeof(pw1), false);
1846+
initPQExpBuffer(&buf);
1847+
printfPQExpBuffer(&buf, _("Enter new password for user \"%s\": "), user);
1848+
1849+
simple_prompt(buf.data, pw1, sizeof(pw1), false);
18331850
simple_prompt("Enter it again: ", pw2, sizeof(pw2), false);
18341851

18351852
if (strcmp(pw1, pw2) != 0)
@@ -1839,14 +1856,8 @@ exec_command_password(PsqlScanState scan_state, bool active_branch)
18391856
}
18401857
else
18411858
{
1842-
char *user;
18431859
char *encrypted_password;
18441860

1845-
if (opt0)
1846-
user = opt0;
1847-
else
1848-
user = PQuser(pset.db);
1849-
18501861
encrypted_password = PQencryptPasswordConn(pset.db, pw1, user, NULL);
18511862

18521863
if (!encrypted_password)
@@ -1856,15 +1867,12 @@ exec_command_password(PsqlScanState scan_state, bool active_branch)
18561867
}
18571868
else
18581869
{
1859-
PQExpBufferData buf;
18601870
PGresult *res;
18611871

1862-
initPQExpBuffer(&buf);
18631872
printfPQExpBuffer(&buf, "ALTER USER %s PASSWORD ",
18641873
fmtId(user));
18651874
appendStringLiteralConn(&buf, encrypted_password, pset.db);
18661875
res = PSQLexec(buf.data);
1867-
termPQExpBuffer(&buf);
18681876
if (!res)
18691877
success = false;
18701878
else
@@ -1873,8 +1881,8 @@ exec_command_password(PsqlScanState scan_state, bool active_branch)
18731881
}
18741882
}
18751883

1876-
if (opt0)
1877-
free(opt0);
1884+
free(user);
1885+
termPQExpBuffer(&buf);
18781886
}
18791887
else
18801888
ignore_slash_options(scan_state);

0 commit comments

Comments
 (0)