Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1eftbT-0004TC-31 for pgsql-bugs@arkaria.postgresql.org; Sun, 28 Jan 2018 20:31:03 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eftbR-0005mS-F5 for pgsql-bugs@arkaria.postgresql.org; Sun, 28 Jan 2018 20:31:01 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1eftbR-0005mJ-9M for pgsql-bugs@lists.postgresql.org; Sun, 28 Jan 2018 20:31:01 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1eftbO-0003un-1R for pgsql-bugs@lists.postgresql.org; Sun, 28 Jan 2018 20:31:00 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id w0SKUsvt014401; Sun, 28 Jan 2018 15:30:54 -0500 From: Tom Lane To: Bruce Momjian cc: akos@elegran.com, pgsql-bugs@lists.postgresql.org Subject: Re: BUG #15025: PSQL CLI - inconsistency when both -d and -U supplies a username In-reply-to: <20180128200418.GA4380@momjian.us> References: <20180123130013.7407.24749@wrigleys.postgresql.org> <20180128034832.GD9153@momjian.us> <29202.1517168326@sss.pgh.pa.us> <20180128200418.GA4380@momjian.us> Comments: In-reply-to Bruce Momjian message dated "Sun, 28 Jan 2018 15:04:18 -0500" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <14304.1517171349.0@sss.pgh.pa.us> Date: Sun, 28 Jan 2018 15:30:54 -0500 Message-ID: <14400.1517171454@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <14304.1517171349.1@sss.pgh.pa.us> Bruce Momjian writes: > On Sun, Jan 28, 2018 at 02:38:46PM -0500, Tom Lane wrote: >> Isn't it possible to get the URI parse >> results back out of libpq? > Well, there is PQuser(), but you need to pass a connection struct to > that, and before you connect you don't have one. Yeah, but we normally don't prompt for password till after a failed connection attempt, at which point we can get the info. So I propose something like the attached. There's room for debate about what we ought to do when -W (--password) is specified, but I think that that's not really that exciting because the only real use-cases for it are noninteractive applications that aren't going to care what the prompt is. So in the startup.c case I have it just offering the neutral "Password: " prompt always. In the \c case, I left it using the same initial username as it was before, because the odds that that's right seem considerably higher with \c. You can still fool it by giving a URI dbname to \c, so maybe there's an argument for lobotomizing the initial prompt in \c too, but I didn't do that here. regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-diff; name="fix-username-in-password-prompts-1.patch"; charset="us-ascii" Content-ID: <14304.1517171349.2@sss.pgh.pa.us> Content-Description: fix-username-in-password-prompts-1.patch Content-Transfer-Encoding: quoted-printable diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c index 015c391..63a6f99 100644 --- a/src/bin/psql/command.c +++ b/src/bin/psql/command.c @@ -2829,7 +2829,7 @@ prompt_for_password(const char *username) { char buf[100]; = - if (username =3D=3D NULL) + if (username =3D=3D NULL || username[0] =3D=3D '\0') simple_prompt("Password: ", buf, sizeof(buf), false); else { @@ -2957,6 +2957,11 @@ do_connect(enum trivalue reuse_previous_specificati= on, * XXX: this behavior leads to spurious connection attempts recorded in * the postmaster's log. But libpq offers no API that would let us obta= in * a password and then continue with the first connection attempt. + * + * XXX: prompting with "user" might be the wrong thing, if the dbname is= a + * connstring or URI that overrides that. But getPassword =3D=3D TRI_YE= S is a + * seldom-used option, so it doesn't seem worth sweating over. The norm= al + * prompting path below gets it right. */ if (pset.getPassword =3D=3D TRI_YES) { @@ -3026,8 +3031,12 @@ do_connect(enum trivalue reuse_previous_specificati= on, */ if (!password && PQconnectionNeedsPassword(n_conn) && pset.getPassword = !=3D TRI_NO) { + /* + * Prompt for password using the username we actually connected + * with --- it might've come out of "dbname" rather than "user". + */ + password =3D prompt_for_password(PQuser(n_conn)); PQfinish(n_conn); - password =3D prompt_for_password(user); continue; } = diff --git a/src/bin/psql/startup.c b/src/bin/psql/startup.c index ec6ae45..be57574 100644 --- a/src/bin/psql/startup.c +++ b/src/bin/psql/startup.c @@ -101,7 +101,6 @@ main(int argc, char *argv[]) int successResult; bool have_password =3D false; char password[100]; - char *password_prompt =3D NULL; bool new_pass; = set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("psql")); @@ -205,15 +204,14 @@ main(int argc, char *argv[]) pset.popt.topt.recordSep.separator_zero =3D false; } = - if (options.username =3D=3D NULL) - password_prompt =3D pg_strdup(_("Password: ")); - else - password_prompt =3D psprintf(_("Password for user %s: "), - options.username); - if (pset.getPassword =3D=3D TRI_YES) { - simple_prompt(password_prompt, password, sizeof(password), false); + /* + * We can't be sure yet of the username that will be used, so don't + * offer a potentially wrong one. Typical uses of this option are + * noninteractive anyway. + */ + simple_prompt("Password: ", password, sizeof(password), false); have_password =3D true; } = @@ -252,15 +250,28 @@ main(int argc, char *argv[]) !have_password && pset.getPassword !=3D TRI_NO) { + /* + * Before closing the old PGconn, extract the user name that was + * actually connected with --- it might've come out of a URI or + * connstring "database name" rather than options.username. + */ + const char *realusername =3D PQuser(pset.db); + char *password_prompt; + + if (realusername && realusername[0]) + password_prompt =3D psprintf(_("Password for user %s: "), + realusername); + else + password_prompt =3D pg_strdup(_("Password: ")); PQfinish(pset.db); + simple_prompt(password_prompt, password, sizeof(password), false); + free(password_prompt); have_password =3D true; new_pass =3D true; } } while (new_pass); = - free(password_prompt); - if (PQstatus(pset.db) =3D=3D CONNECTION_BAD) { fprintf(stderr, "%s: %s", pset.progname, PQerrorMessage(pset.db)); ------- =_aaaaaaaaaa0--