From: Bossart, Nathan <bossartn@amazon.com>
To: Tom Lane <tgl@sss.pgh.pa.us>
Cc: pgsql-hackers@lists.postgresql.org <pgsql-hackers@lists.postgresql.org>
Subject: Re: Improving psql's \password command
Date: Fri, 12 Nov 2021 17:23:58 +0000
Message-ID: <E0C46CDF-46B2-4388-B53B-96EB2CA0F71E@amazon.com> (raw)
In-Reply-To: <1553329.1636647111@sss.pgh.pa.us>
References: <747443.1635536754@sss.pgh.pa.us>
<C2787D93-6871-4D93-9CF3-CD7BB489F47F@amazon.com>
<765509.1635552418@sss.pgh.pa.us>
<7BAC8A28-148B-4B03-A345-D68071388300@amazon.com>
<1553329.1636647111@sss.pgh.pa.us>
On 11/11/21, 8:12 AM, "Tom Lane" <tgl@sss.pgh.pa.us> wrote:
> It seems to me it'd overcomplicate simple_prompt's API for one use-case
> ... but if you want to try it, step right up. (I suppose some of that
> objection could be overcome by making simple_prompt into a wrapper
> around another function not_so_simple_prompt.)
I haven't started on the SIGINT stuff yet, but I did extract the
PQuser() fix so that we can hopefully get that one out of the way. I
made some small adjustments in an attempt to keep the branching in
this function from getting too complicated.
Nathan
Attachments:download all (2 files, .tar.gz)v2-0001-Adjust-password-to-use-current-user-instead-of-au.patch (application/octet-stream, 1.7K) download
From 02459bbd107d7bf04e97184a00d823c860688a51 Mon Sep 17 00:00:00 2001
From: Nathan Bossart <bossartn@amazon.com>
Date: Fri, 12 Nov 2021 16:57:36 +0000
Subject: [PATCH v2 1/2] Adjust \password to use current user instead of
authenticated user by default.
---
src/bin/psql/command.c | 25 ++++++++++++++++---------
1 file changed, 16 insertions(+), 9 deletions(-)
diff --git a/src/bin/psql/command.c b/src/bin/psql/command.cindex 49d4c0e3ce..5619ed685a 100644--- a/src/bin/psql/command.c+++ b/src/bin/psql/command.c@@ -2023,11 +2023,24 @@ 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;
+ if (user == NULL)+ {+ /* Fetch current user so we can report whose PW will be changed */+ 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);
pw2 = simple_prompt("Enter it again: ", false);
@@ -2038,14 +2051,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)
@@ -2072,8 +2079,8 @@ exec_command_password(PsqlScanState scan_state, bool active_branch)
}
}
- if (opt0)- free(opt0);+ if (user)+ free(user);
free(pw1);
free(pw2);
}
--
2.16.6
v2-0002-Add-role-name-to-password-prompt.patch (application/octet-stream, 1.7K) download
From 0f92b7b1d992c6174b49358f337d84ba719b9576 Mon Sep 17 00:00:00 2001
From: Nathan Bossart <bossartn@amazon.com>
Date: Fri, 12 Nov 2021 17:10:51 +0000
Subject: [PATCH v2 2/2] Add role name to \password prompt.
---
src/bin/psql/command.c | 10 ++++++----
1 file changed, 6 insertions(+), 4 deletions(-)
diff --git a/src/bin/psql/command.c b/src/bin/psql/command.cindex 5619ed685a..eedc6243d3 100644--- a/src/bin/psql/command.c+++ b/src/bin/psql/command.c@@ -2027,6 +2027,7 @@ exec_command_password(PsqlScanState scan_state, bool active_branch)
OT_SQLID, NULL, true);
char *pw1;
char *pw2;
+ PQExpBufferData buf;
if (user == NULL)
{
@@ -2041,7 +2042,10 @@ exec_command_password(PsqlScanState scan_state, bool active_branch)
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)
@@ -2062,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
@@ -2083,6 +2084,7 @@ exec_command_password(PsqlScanState scan_state, bool active_branch)
free(user);
free(pw1);
free(pw2);
+ termPQExpBuffer(&buf);
}
else
ignore_slash_options(scan_state);
--
2.16.6
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-hackers@postgresql.org
Cc: bossartn@amazon.com, tgl@sss.pgh.pa.us, pgsql-hackers@lists.postgresql.org
Subject: Re: Improving psql's \password command
In-Reply-To: <E0C46CDF-46B2-4388-B53B-96EB2CA0F71E@amazon.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox