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 1efdxO-0005dB-OT for pgsql-bugs@arkaria.postgresql.org; Sun, 28 Jan 2018 03:48:38 +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 1efdxN-0003I7-NW for pgsql-bugs@arkaria.postgresql.org; Sun, 28 Jan 2018 03:48:37 +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 1efdxN-0003Hy-Ho for pgsql-bugs@lists.postgresql.org; Sun, 28 Jan 2018 03:48:37 +0000 Received: from momjian.us ([72.94.173.45]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1efdxJ-0007OP-Rj for pgsql-bugs@lists.postgresql.org; Sun, 28 Jan 2018 03:48:37 +0000 Received: from bruce by momjian.us with local (Exim 4.84_2) (envelope-from ) id 1efdxI-0006ad-5L; Sat, 27 Jan 2018 22:48:32 -0500 Date: Sat, 27 Jan 2018 22:48:32 -0500 From: Bruce Momjian To: akos@elegran.com, pgsql-bugs@lists.postgresql.org Subject: Re: BUG #15025: PSQL CLI - inconsistency when both -d and -U supplies a username Message-ID: <20180128034832.GD9153@momjian.us> References: <20180123130013.7407.24749@wrigleys.postgresql.org> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="EVF5PPMfhYS0aIcm" Content-Disposition: inline In-Reply-To: <20180123130013.7407.24749@wrigleys.postgresql.org> User-Agent: Mutt/1.5.23 (2014-03-12) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --EVF5PPMfhYS0aIcm Content-Type: text/plain; charset=us-ascii Content-Disposition: inline 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 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 + --EVF5PPMfhYS0aIcm Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="uri.diff" 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: "), --EVF5PPMfhYS0aIcm--