Guido van Rossum | b5903ac | 1998-04-09 20:37:16 +0000 | [diff] [blame] | 1 | """Utilities to get a password and/or the current user name. |
| 2 | |
| 3 | getpass(prompt) - prompt for a password, with echo turned off |
| 4 | getuser() - get the user name from the environment or password database |
| 5 | |
Jeremy Hylton | 88d2330 | 1999-10-18 22:25:22 +0000 | [diff] [blame] | 6 | On Windows, the msvcrt module will be used. |
| 7 | On the Mac EasyDialogs.AskPassword is used, if available. |
| 8 | |
Guido van Rossum | b5903ac | 1998-04-09 20:37:16 +0000 | [diff] [blame] | 9 | """ |
| 10 | |
Guido van Rossum | 98d9fd3 | 2000-02-28 15:12:25 +0000 | [diff] [blame] | 11 | # Authors: Piers Lauder (original) |
| 12 | # Guido van Rossum (Windows support and cleanup) |
| 13 | |
Jeremy Hylton | 88d2330 | 1999-10-18 22:25:22 +0000 | [diff] [blame] | 14 | import sys |
Guido van Rossum | b5903ac | 1998-04-09 20:37:16 +0000 | [diff] [blame] | 15 | |
Skip Montanaro | eccd02a | 2001-01-20 23:34:12 +0000 | [diff] [blame] | 16 | __all__ = ["getpass","getuser"] |
| 17 | |
Jeremy Hylton | 88d2330 | 1999-10-18 22:25:22 +0000 | [diff] [blame] | 18 | def unix_getpass(prompt='Password: '): |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 19 | """Prompt for a password, with echo turned off. |
Guido van Rossum | b5903ac | 1998-04-09 20:37:16 +0000 | [diff] [blame] | 20 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 21 | Restore terminal settings at end. |
| 22 | """ |
Guido van Rossum | b5903ac | 1998-04-09 20:37:16 +0000 | [diff] [blame] | 23 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 24 | try: |
| 25 | fd = sys.stdin.fileno() |
| 26 | except: |
| 27 | return default_getpass(prompt) |
Guido van Rossum | b5903ac | 1998-04-09 20:37:16 +0000 | [diff] [blame] | 28 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 29 | old = termios.tcgetattr(fd) # a copy to save |
| 30 | new = old[:] |
Guido van Rossum | b5903ac | 1998-04-09 20:37:16 +0000 | [diff] [blame] | 31 | |
Fred Drake | 1191d01 | 2001-02-27 21:23:31 +0000 | [diff] [blame] | 32 | new[3] = new[3] & ~termios.ECHO # 3 == 'lflags' |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 33 | try: |
Fred Drake | 1191d01 | 2001-02-27 21:23:31 +0000 | [diff] [blame] | 34 | termios.tcsetattr(fd, termios.TCSADRAIN, new) |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 35 | passwd = _raw_input(prompt) |
| 36 | finally: |
Fred Drake | 1191d01 | 2001-02-27 21:23:31 +0000 | [diff] [blame] | 37 | termios.tcsetattr(fd, termios.TCSADRAIN, old) |
Guido van Rossum | b5903ac | 1998-04-09 20:37:16 +0000 | [diff] [blame] | 38 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 39 | sys.stdout.write('\n') |
| 40 | return passwd |
Guido van Rossum | b5903ac | 1998-04-09 20:37:16 +0000 | [diff] [blame] | 41 | |
| 42 | |
| 43 | def win_getpass(prompt='Password: '): |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 44 | """Prompt for password with echo off, using Windows getch().""" |
Guido van Rossum | 60250e2 | 2001-08-30 15:07:44 +0000 | [diff] [blame] | 45 | if sys.stdin is not sys.__stdin__: |
| 46 | return default_getpass(prompt) |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 47 | import msvcrt |
| 48 | for c in prompt: |
| 49 | msvcrt.putch(c) |
| 50 | pw = "" |
| 51 | while 1: |
| 52 | c = msvcrt.getch() |
| 53 | if c == '\r' or c == '\n': |
| 54 | break |
| 55 | if c == '\003': |
| 56 | raise KeyboardInterrupt |
| 57 | if c == '\b': |
| 58 | pw = pw[:-1] |
| 59 | else: |
| 60 | pw = pw + c |
| 61 | msvcrt.putch('\r') |
| 62 | msvcrt.putch('\n') |
| 63 | return pw |
Guido van Rossum | b5903ac | 1998-04-09 20:37:16 +0000 | [diff] [blame] | 64 | |
| 65 | |
Guido van Rossum | fb9b7fd | 1998-04-13 20:22:21 +0000 | [diff] [blame] | 66 | def default_getpass(prompt='Password: '): |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 67 | print "Warning: Problem with getpass. Passwords may be echoed." |
| 68 | return _raw_input(prompt) |
Guido van Rossum | 1a7bab0 | 1998-07-28 19:28:43 +0000 | [diff] [blame] | 69 | |
| 70 | |
| 71 | def _raw_input(prompt=""): |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 72 | # A raw_input() replacement that doesn't save the string in the |
| 73 | # GNU readline history. |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 74 | prompt = str(prompt) |
| 75 | if prompt: |
| 76 | sys.stdout.write(prompt) |
| 77 | line = sys.stdin.readline() |
| 78 | if not line: |
| 79 | raise EOFError |
| 80 | if line[-1] == '\n': |
| 81 | line = line[:-1] |
| 82 | return line |
Guido van Rossum | fb9b7fd | 1998-04-13 20:22:21 +0000 | [diff] [blame] | 83 | |
| 84 | |
Guido van Rossum | b5903ac | 1998-04-09 20:37:16 +0000 | [diff] [blame] | 85 | def getuser(): |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 86 | """Get the username from the environment or password database. |
Guido van Rossum | b5903ac | 1998-04-09 20:37:16 +0000 | [diff] [blame] | 87 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 88 | First try various environment variables, then the password |
| 89 | database. This works on Windows as long as USERNAME is set. |
Guido van Rossum | b5903ac | 1998-04-09 20:37:16 +0000 | [diff] [blame] | 90 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 91 | """ |
Guido van Rossum | b5903ac | 1998-04-09 20:37:16 +0000 | [diff] [blame] | 92 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 93 | import os |
Guido van Rossum | b5903ac | 1998-04-09 20:37:16 +0000 | [diff] [blame] | 94 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 95 | for name in ('LOGNAME', 'USER', 'LNAME', 'USERNAME'): |
| 96 | user = os.environ.get(name) |
| 97 | if user: |
| 98 | return user |
Guido van Rossum | b5903ac | 1998-04-09 20:37:16 +0000 | [diff] [blame] | 99 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 100 | # If this fails, the exception will "explain" why |
| 101 | import pwd |
| 102 | return pwd.getpwuid(os.getuid())[0] |
Jeremy Hylton | 88d2330 | 1999-10-18 22:25:22 +0000 | [diff] [blame] | 103 | |
| 104 | # Bind the name getpass to the appropriate function |
| 105 | try: |
Fred Drake | 1191d01 | 2001-02-27 21:23:31 +0000 | [diff] [blame] | 106 | import termios |
Jeremy Hylton | 88d2330 | 1999-10-18 22:25:22 +0000 | [diff] [blame] | 107 | except ImportError: |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 108 | try: |
| 109 | import msvcrt |
| 110 | except ImportError: |
| 111 | try: |
| 112 | from EasyDialogs import AskPassword |
| 113 | except ImportError: |
| 114 | getpass = default_getpass |
| 115 | else: |
| 116 | getpass = AskPassword |
| 117 | else: |
| 118 | getpass = win_getpass |
Jeremy Hylton | 88d2330 | 1999-10-18 22:25:22 +0000 | [diff] [blame] | 119 | else: |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 120 | getpass = unix_getpass |