Thread: psql patch for displaying the username when asking password

psql patch for displaying the username when asking password

From
Adrian Maier
Date:
Hello all,

The attached patch modifies the message displayed by psql
when asking the password "Password: "
to include the username as well:  "Password for user postgres : ".

Displaying the username is useful when running sql scripts
which are setting the session authorization (like the dump scripts
generated by older versions of pg_dump) and you don't have all the
usernames/passwords in ~/.pgpass.
In such cases it can happen that psql is asking several times :
Password:
Password:
Password:
Password:

and you don't know whether it's asking the pasword of the 'postgres' user
or the password of user that is importing the data .

I have the feeling that asking for "Password:" several times no longer
happens when running scripts generated by recent versions of pg_dump.
However, the patch might be useful for people who are upgrading .


Cheers,
Adrian Maier

Attachment

Re: psql patch for displaying the username when asking password

From
Peter Eisentraut
Date:
Am Donnerstag, 30. Juni 2005 09:34 schrieb Adrian Maier:
> The attached patch modifies the message displayed by psql
> when asking the password "Password: "
> to include the username as well:  "Password for user postgres : ".

I can't decode your attachment so I don't know if this is a typo or actually
in the patch, but there shouldn't be a space after the user name.

--
Peter Eisentraut
http://developer.postgresql.org/~petere/

Re: psql patch for displaying the username when asking password

From
Adrian Maier
Date:
On 6/30/05, Peter Eisentraut <peter_e@gmx.net> wrote:
> Am Donnerstag, 30. Juni 2005 09:34 schrieb Adrian Maier:
> > The attached patch modifies the message displayed by psql
> > when asking the password "Password: "
> > to include the username as well:  "Password for user postgres : ".
>
> I can't decode your attachment so I don't know if this is a typo or
> actually  in the patch, but there shouldn't be a space after the user name.

There was a space there.  I've removed the space between
username and colons.    The space after the colons is ok,  I hope ?

I'm attaching the modified patch.

Cheers,
Adrian Maier

Attachment

Re: psql patch for displaying the username when asking password

From
Bruce Momjian
Date:
Patch adjusted slightly, attached, and applied.  Thanks.

---------------------------------------------------------------------------

Adrian Maier wrote:
> On 6/30/05, Peter Eisentraut <peter_e@gmx.net> wrote:
> > Am Donnerstag, 30. Juni 2005 09:34 schrieb Adrian Maier:
> > > The attached patch modifies the message displayed by psql
> > > when asking the password "Password: "
> > > to include the username as well:  "Password for user postgres : ".
> >
> > I can't decode your attachment so I don't know if this is a typo or
> > actually  in the patch, but there shouldn't be a space after the user name.
>
> There was a space there.  I've removed the space between
> username and colons.    The space after the colons is ok,  I hope ?
>
> I'm attaching the modified patch.
>
> Cheers,
> Adrian Maier

[ Attachment, skipping... ]

>
> ---------------------------(end of broadcast)---------------------------
> TIP 7: don't forget to increase your free space map settings

--
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 359-1001
  +  If your life is a hard drive,     |  13 Roberts Road
  +  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073
Index: src/bin/psql/command.c
===================================================================
RCS file: /cvsroot/pgsql/src/bin/psql/command.c,v
retrieving revision 1.150
diff -c -c -r1.150 command.c
*** src/bin/psql/command.c    18 Jul 2005 20:57:53 -0000    1.150
--- src/bin/psql/command.c    25 Jul 2005 17:13:42 -0000
***************
*** 911,916 ****
--- 911,917 ----
      const char *dbparam = NULL;
      const char *userparam = NULL;
      const char *pwparam = NULL;
+     char       *password_prompt = NULL;
      char       *prompted_password = NULL;
      bool        need_pass;
      bool        success = false;
***************
*** 930,938 ****
      else
          userparam = new_user;

      /* need to prompt for password? */
      if (pset.getPassword)
!         pwparam = prompted_password = simple_prompt("Password: ", 100, false);

      /*
       * Use old password (if any) if no new one given and we are
--- 931,948 ----
      else
          userparam = new_user;

+     if (userparam == NULL)
+         password_prompt = strdup("Password: ");
+     else
+     {
+         password_prompt = malloc(strlen("Password for user %s: ") - 2 +
+                                  strlen(userparam) + 1);
+         sprintf(password_prompt,"Password for user %s: ", userparam);
+     }
+
      /* need to prompt for password? */
      if (pset.getPassword)
!         pwparam = prompted_password = simple_prompt(password_prompt, 100, false);

      /*
       * Use old password (if any) if no new one given and we are
***************
*** 956,966 ****
              need_pass = true;
              free(prompted_password);
              prompted_password = NULL;
!             pwparam = prompted_password = simple_prompt("Password: ", 100, false);
          }
      } while (need_pass);

      free(prompted_password);

      /*
       * If connection failed, try at least keep the old one. That's
--- 966,977 ----
              need_pass = true;
              free(prompted_password);
              prompted_password = NULL;
!             pwparam = prompted_password = simple_prompt(password_prompt, 100, false);
          }
      } while (need_pass);

      free(prompted_password);
+     free(password_prompt);

      /*
       * If connection failed, try at least keep the old one. That's
Index: src/bin/psql/startup.c
===================================================================
RCS file: /cvsroot/pgsql/src/bin/psql/startup.c,v
retrieving revision 1.119
diff -c -c -r1.119 startup.c
*** src/bin/psql/startup.c    14 Jul 2005 08:42:37 -0000    1.119
--- src/bin/psql/startup.c    25 Jul 2005 17:13:42 -0000
***************
*** 106,111 ****
--- 106,112 ----

      char       *username = NULL;
      char       *password = NULL;
+     char       *password_prompt = NULL;
      bool        need_pass;

      set_pglocale_pgservice(argv[0], "psql");
***************
*** 188,195 ****
              username = pg_strdup(options.username);
      }

      if (pset.getPassword)
!         password = simple_prompt("Password: ", 100, false);

      /* loop until we have a password if requested by backend */
      do
--- 189,205 ----
              username = pg_strdup(options.username);
      }

+     if (options.username == NULL)
+         password_prompt = strdup("Password: ");
+     else
+     {
+         password_prompt = malloc(strlen("Password for user %s: ") - 2 +
+                                  strlen(options.username) + 1);
+         sprintf(password_prompt,"Password for user %s: ", options.username);
+     }
+
      if (pset.getPassword)
!         password = simple_prompt(password_prompt, 100, false);

      /* loop until we have a password if requested by backend */
      do
***************
*** 207,218 ****
              need_pass = true;
              free(password);
              password = NULL;
!             password = simple_prompt("Password: ", 100, false);
          }
      } while (need_pass);

      free(username);
      free(password);

      if (PQstatus(pset.db) == CONNECTION_BAD)
      {
--- 217,229 ----
              need_pass = true;
              free(password);
              password = NULL;
!             password = simple_prompt(password_prompt, 100, false);
          }
      } while (need_pass);

      free(username);
      free(password);
+     free(password_prompt);

      if (PQstatus(pset.db) == CONNECTION_BAD)
      {

Re: psql patch for displaying the username when asking password

From
Bruce Momjian
Date:
Adrian Maier wrote:
> On 6/30/05, Peter Eisentraut <peter_e@gmx.net> wrote:
> > Am Donnerstag, 30. Juni 2005 09:34 schrieb Adrian Maier:
> > > The attached patch modifies the message displayed by psql
> > > when asking the password "Password: "
> > > to include the username as well:  "Password for user postgres : ".
> >
> > I can't decode your attachment so I don't know if this is a typo or
> > actually  in the patch, but there shouldn't be a space after the user name.
>
> There was a space there.  I've removed the space between
> username and colons.    The space after the colons is ok,  I hope ?
>
> I'm attaching the modified patch.

I made this adjustment in the applied version.  Thanks.

--
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 359-1001
  +  If your life is a hard drive,     |  13 Roberts Road
  +  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073