pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Bruce Momjian <bruce@momjian.us>
To: akos@elegran.com
To: pgsql-bugs@lists.postgresql.org
Subject: Re: BUG #15025: PSQL CLI - inconsistency when both -d and -U supplies a username
Date: Sat, 27 Jan 2018 22:48:32 -0500
Message-ID: <20180128034832.GD9153@momjian.us> (raw)
In-Reply-To: <20180123130013.7407.24749@wrigleys.postgresql.org>
References: <20180123130013.7407.24749@wrigleys.postgresql.org>

On Tue, Jan 23, 2018 at 01:00:13PM +0000, PG Bug reporting form wrote:
> The following bug has been logged on the website:
> 
> Bug reference:      15025
> Logged by:          Akos Vandra
> Email address:      akos@elegran.com
> PostgreSQL version: 9.5.4
> Operating system:   Debian
> Description:        
> 
> Repro:
> 
> case 1: psql -U other_user -d "postgresql://some_user@host/db" 
> 
> case 2: psql -d "postgresql://some_user@host/db"  -U other_user
> 
> Expectation:
> 
> Use whatever is given later:
> 
>  case 1: log in as user
>  case 2: log in as other_user
> 
> Actual:
> 
>  case 1:  logs in as user
>  case 2: 
>     - the password prompt asks for the pw of user
>     - psql uses the password given to log in with other_user
>     - if the pw is correct for user, or incorrect it displays that the
> password is incorrect for other_user
>     - if the password is correct for other_user, it connects to the db as
> other_user
> 
> 
> $ psql "postgresql://user@host/db" -U other_user
> Password for user other_user:
> psql: FATAL:  password authentication failed for user "user"
> FATAL:  password authentication failed for user "user"

I was able to make a clearer example. First create two users:

	CREATE USER user1 PASSWORD 'abc1';
	CREATE USER user2 PASSWORD 'abc2';

then:

	psql -d "postgresql://user2@momjian.us/test" -U user1
-->	Password for user user1:

but it wants the user2 password.  Same with:

	psql -U user1 -d "postgresql://user2@momjian.us/test"
-->	Password for user user1:

It doesn't matter whether -U is first or last, it always prompts for the
-U user, but connects as the -d user.

Because the URI is parsed by libpq, I don't think we can do any better
than just suppress the user name in the password prompt when a URI is
used.  This is done in the attached patch, e.g.:
e.g.:

	$psql -U user1 -d "postgresql://user2@momjian.us/test"
	Password:

Can someone tell me if I need to update the prompt in
psql/command.c::prompt_for_password()?  I can't figure out how to
trigger that prompt.

-- 
  Bruce Momjian  <bruce@momjian.us>        http://momjian.us
  EnterpriseDB                             http://enterprisedb.com

+ As you are, so once was I.  As I am, so you will be. +
+                      Ancient Roman grave inscription +

Attachments:

uri.diff (text/x-diff, 855B) download diff --git a/src/bin/psql/startup.c b/src/bin/psql/startup.c new file mode 100644 index ec6ae45..711186f *** a/src/bin/psql/startup.c --- b/src/bin/psql/startup.c *************** main(int argc, char *argv[]) *** 205,211 **** pset.popt.topt.recordSep.separator_zero = false; } ! if (options.username == NULL) password_prompt = pg_strdup(_("Password: ")); else password_prompt = psprintf(_("Password for user %s: "), --- 205,216 ---- pset.popt.topt.recordSep.separator_zero = false; } ! if (options.username == NULL || ! /* ! * If the dbname is a URI, it might contain a different user, ! * so don't display the -U user. ! */ ! (options.dbname && recognized_connection_string(options.dbname))) password_prompt = pg_strdup(_("Password: ")); else password_prompt = psprintf(_("Password for user %s: "),

view thread (8+ messages)  latest in thread

Message-ID: <20180128034832.GD9153@momjian.us>
Permalink:  ../20180128034832.GD9153@momjian.us/
Also on:    postgresql.org/message-id/20180128034832.GD9153@momjian.us

 ·  · 

reply

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 from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: bruce@momjian.us, akos@elegran.com
  Subject: Re: BUG #15025: PSQL CLI - inconsistency when both -d and -U supplies a username
  In-Reply-To: <20180128034832.GD9153@momjian.us>
  References: <20180123130013.7407.24749@wrigleys.postgresql.org> <20180128034832.GD9153@momjian.us>

* 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