summaryrefslogtreecommitdiff
path: root/src/bin/psql/command.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/bin/psql/command.c')
-rw-r--r--src/bin/psql/command.c34
1 files changed, 21 insertions, 13 deletions
diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c
index 2ea5ba8fc49..6b7d78edf76 100644
--- a/src/bin/psql/command.c
+++ b/src/bin/psql/command.c
@@ -1839,12 +1839,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[100];
char pw2[100];
+ 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);
+ }
- simple_prompt("Enter new password: ", pw1, sizeof(pw1), false);
+ initPQExpBuffer(&buf);
+ printfPQExpBuffer(&buf, _("Enter new password for user \"%s\": "), user);
+
+ simple_prompt(buf.data, pw1, sizeof(pw1), false);
simple_prompt("Enter it again: ", pw2, sizeof(pw2), false);
if (strcmp(pw1, pw2) != 0)
@@ -1854,14 +1871,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)
@@ -1871,15 +1882,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
@@ -1888,8 +1896,8 @@ exec_command_password(PsqlScanState scan_state, bool active_branch)
}
}
- if (opt0)
- free(opt0);
+ free(user);
+ termPQExpBuffer(&buf);
}
else
ignore_slash_options(scan_state);