Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit523adcc

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 usernameused to log into the current session. This is not the same thing ifone has since done SET ROLE or SET SESSION AUTHENTICATION. Aside fromthe possible surprise factor, it's quite likely that the current roledoesn'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, inhopes of reducing confusion, include the role name that will beacted on in the password prompt.The discrepancy from the documentation makes this a bug, soback-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 parent2d60ce3 commit523adcc

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
charpw1[100];
18301830
charpw2[100];
1831+
PQExpBufferDatabuf;
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+
returnPSQL_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-
PQExpBufferDatabuf;
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)

[8]ページ先頭

©2009-2025 Movatter.jp