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 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3 | getpass(prompt[, stream]) - Prompt for a password, with echo turned off. |
| 4 | getuser() - Get the user name from the environment or password database. |
| 5 | |
| 6 | GetPassWarning - This UserWarning is issued when getpass() cannot prevent |
| 7 | echoing of the password contents while reading. |
Guido van Rossum | b5903ac | 1998-04-09 20:37:16 +0000 | [diff] [blame] | 8 | |
Jeremy Hylton | 88d2330 | 1999-10-18 22:25:22 +0000 | [diff] [blame] | 9 | On Windows, the msvcrt module will be used. |
| 10 | On the Mac EasyDialogs.AskPassword is used, if available. |
| 11 | |
Guido van Rossum | b5903ac | 1998-04-09 20:37:16 +0000 | [diff] [blame] | 12 | """ |
| 13 | |
Guido van Rossum | 98d9fd3 | 2000-02-28 15:12:25 +0000 | [diff] [blame] | 14 | # Authors: Piers Lauder (original) |
| 15 | # Guido van Rossum (Windows support and cleanup) |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 16 | # Gregory P. Smith (tty support & GetPassWarning) |
Guido van Rossum | 98d9fd3 | 2000-02-28 15:12:25 +0000 | [diff] [blame] | 17 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 18 | import os, sys, warnings |
Guido van Rossum | b5903ac | 1998-04-09 20:37:16 +0000 | [diff] [blame] | 19 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 20 | __all__ = ["getpass","getuser","GetPassWarning"] |
| 21 | |
| 22 | |
| 23 | class GetPassWarning(UserWarning): pass |
| 24 | |
Skip Montanaro | eccd02a | 2001-01-20 23:34:12 +0000 | [diff] [blame] | 25 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 26 | def unix_getpass(prompt='Password: ', stream=None): |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 27 | """Prompt for a password, with echo turned off. |
Guido van Rossum | b5903ac | 1998-04-09 20:37:16 +0000 | [diff] [blame] | 28 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 29 | Args: |
| 30 | prompt: Written on stream to ask for the input. Default: 'Password: ' |
| 31 | stream: A writable file object to display the prompt. Defaults to |
| 32 | the tty. If no tty is available defaults to sys.stderr. |
| 33 | Returns: |
| 34 | The seKr3t input. |
| 35 | Raises: |
| 36 | EOFError: If our input tty or stdin was closed. |
| 37 | GetPassWarning: When we were unable to turn echo off on the input. |
| 38 | |
| 39 | Always restores terminal settings before returning. |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 40 | """ |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 41 | fd = None |
| 42 | tty = None |
R David Murray | 319d58d | 2013-04-08 01:48:22 -0400 | [diff] [blame] | 43 | passwd = None |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 44 | try: |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 45 | # Always try reading and writing directly on the tty first. |
| 46 | fd = os.open('/dev/tty', os.O_RDWR|os.O_NOCTTY) |
| 47 | tty = os.fdopen(fd, 'w+', 1) |
| 48 | input = tty |
| 49 | if not stream: |
| 50 | stream = tty |
Andrew Svetlov | 3438fa4 | 2012-12-17 23:35:18 +0200 | [diff] [blame] | 51 | except OSError as e: |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 52 | # If that fails, see if stdin can be controlled. |
| 53 | try: |
| 54 | fd = sys.stdin.fileno() |
Benjamin Peterson | 4ac9ce4 | 2009-10-04 14:49:41 +0000 | [diff] [blame] | 55 | except (AttributeError, ValueError): |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 56 | passwd = fallback_getpass(prompt, stream) |
| 57 | input = sys.stdin |
| 58 | if not stream: |
| 59 | stream = sys.stderr |
Guido van Rossum | b5903ac | 1998-04-09 20:37:16 +0000 | [diff] [blame] | 60 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 61 | if fd is not None: |
| 62 | passwd = None |
| 63 | try: |
| 64 | old = termios.tcgetattr(fd) # a copy to save |
| 65 | new = old[:] |
Senthil Kumaran | ea8b024 | 2011-03-24 22:27:01 +0800 | [diff] [blame] | 66 | new[3] &= ~termios.ECHO # 3 == 'lflags' |
Gregory P. Smith | 5046788 | 2009-11-01 18:42:17 +0000 | [diff] [blame] | 67 | tcsetattr_flags = termios.TCSAFLUSH |
| 68 | if hasattr(termios, 'TCSASOFT'): |
| 69 | tcsetattr_flags |= termios.TCSASOFT |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 70 | try: |
Gregory P. Smith | 5046788 | 2009-11-01 18:42:17 +0000 | [diff] [blame] | 71 | termios.tcsetattr(fd, tcsetattr_flags, new) |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 72 | passwd = _raw_input(prompt, stream, input=input) |
| 73 | finally: |
Gregory P. Smith | 5046788 | 2009-11-01 18:42:17 +0000 | [diff] [blame] | 74 | termios.tcsetattr(fd, tcsetattr_flags, old) |
| 75 | stream.flush() # issue7208 |
Florent Xicluna | 54540ec | 2011-11-04 08:29:17 +0100 | [diff] [blame] | 76 | except termios.error: |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 77 | if passwd is not None: |
| 78 | # _raw_input succeeded. The final tcsetattr failed. Reraise |
| 79 | # instead of leaving the terminal in an unknown state. |
| 80 | raise |
| 81 | # We can't control the tty or stdin. Give up and use normal IO. |
| 82 | # fallback_getpass() raises an appropriate warning. |
| 83 | del input, tty # clean up unused file objects before blocking |
| 84 | passwd = fallback_getpass(prompt, stream) |
Guido van Rossum | b5903ac | 1998-04-09 20:37:16 +0000 | [diff] [blame] | 85 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 86 | stream.write('\n') |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 87 | return passwd |
Guido van Rossum | b5903ac | 1998-04-09 20:37:16 +0000 | [diff] [blame] | 88 | |
| 89 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 90 | def win_getpass(prompt='Password: ', stream=None): |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 91 | """Prompt for password with echo off, using Windows getch().""" |
Guido van Rossum | 60250e2 | 2001-08-30 15:07:44 +0000 | [diff] [blame] | 92 | if sys.stdin is not sys.__stdin__: |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 93 | return fallback_getpass(prompt, stream) |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 94 | import msvcrt |
| 95 | for c in prompt: |
Christian Heimes | 0ec88b3 | 2007-12-10 17:02:00 +0000 | [diff] [blame] | 96 | msvcrt.putwch(c) |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 97 | pw = "" |
| 98 | while 1: |
Christian Heimes | 0ec88b3 | 2007-12-10 17:02:00 +0000 | [diff] [blame] | 99 | c = msvcrt.getwch() |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 100 | if c == '\r' or c == '\n': |
| 101 | break |
| 102 | if c == '\003': |
| 103 | raise KeyboardInterrupt |
| 104 | if c == '\b': |
| 105 | pw = pw[:-1] |
| 106 | else: |
| 107 | pw = pw + c |
Christian Heimes | 0ec88b3 | 2007-12-10 17:02:00 +0000 | [diff] [blame] | 108 | msvcrt.putwch('\r') |
| 109 | msvcrt.putwch('\n') |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 110 | return pw |
Guido van Rossum | b5903ac | 1998-04-09 20:37:16 +0000 | [diff] [blame] | 111 | |
| 112 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 113 | def fallback_getpass(prompt='Password: ', stream=None): |
| 114 | warnings.warn("Can not control echo on the terminal.", GetPassWarning, |
| 115 | stacklevel=2) |
| 116 | if not stream: |
| 117 | stream = sys.stderr |
| 118 | print("Warning: Password input may be echoed.", file=stream) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 119 | return _raw_input(prompt, stream) |
Guido van Rossum | 1a7bab0 | 1998-07-28 19:28:43 +0000 | [diff] [blame] | 120 | |
| 121 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 122 | def _raw_input(prompt="", stream=None, input=None): |
Neal Norwitz | ce96f69 | 2006-03-17 06:49:51 +0000 | [diff] [blame] | 123 | # This doesn't save the string in the GNU readline history. |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 124 | if not stream: |
| 125 | stream = sys.stderr |
| 126 | if not input: |
| 127 | input = sys.stdin |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 128 | prompt = str(prompt) |
| 129 | if prompt: |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 130 | stream.write(prompt) |
Guido van Rossum | cfb8333 | 2008-01-01 16:30:47 +0000 | [diff] [blame] | 131 | stream.flush() |
Gregory P. Smith | 5046788 | 2009-11-01 18:42:17 +0000 | [diff] [blame] | 132 | # NOTE: The Python C API calls flockfile() (and unlock) during readline. |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 133 | line = input.readline() |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 134 | if not line: |
| 135 | raise EOFError |
| 136 | if line[-1] == '\n': |
| 137 | line = line[:-1] |
| 138 | return line |
Guido van Rossum | fb9b7fd | 1998-04-13 20:22:21 +0000 | [diff] [blame] | 139 | |
| 140 | |
Guido van Rossum | b5903ac | 1998-04-09 20:37:16 +0000 | [diff] [blame] | 141 | def getuser(): |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 142 | """Get the username from the environment or password database. |
Guido van Rossum | b5903ac | 1998-04-09 20:37:16 +0000 | [diff] [blame] | 143 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 144 | First try various environment variables, then the password |
| 145 | database. This works on Windows as long as USERNAME is set. |
Guido van Rossum | b5903ac | 1998-04-09 20:37:16 +0000 | [diff] [blame] | 146 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 147 | """ |
Guido van Rossum | b5903ac | 1998-04-09 20:37:16 +0000 | [diff] [blame] | 148 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 149 | for name in ('LOGNAME', 'USER', 'LNAME', 'USERNAME'): |
| 150 | user = os.environ.get(name) |
| 151 | if user: |
| 152 | return user |
Guido van Rossum | b5903ac | 1998-04-09 20:37:16 +0000 | [diff] [blame] | 153 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 154 | # If this fails, the exception will "explain" why |
| 155 | import pwd |
| 156 | return pwd.getpwuid(os.getuid())[0] |
Jeremy Hylton | 88d2330 | 1999-10-18 22:25:22 +0000 | [diff] [blame] | 157 | |
| 158 | # Bind the name getpass to the appropriate function |
| 159 | try: |
Fred Drake | 1191d01 | 2001-02-27 21:23:31 +0000 | [diff] [blame] | 160 | import termios |
Neal Norwitz | 201626e | 2002-11-20 23:15:54 +0000 | [diff] [blame] | 161 | # it's possible there is an incompatible termios from the |
| 162 | # McMillan Installer, make sure we have a UNIX-compatible termios |
| 163 | termios.tcgetattr, termios.tcsetattr |
| 164 | except (ImportError, AttributeError): |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 165 | try: |
| 166 | import msvcrt |
Brett Cannon | cd171c8 | 2013-07-04 17:43:24 -0400 | [diff] [blame^] | 167 | except ImportError: |
Brian Curtin | ff47a13 | 2010-12-16 03:24:49 +0000 | [diff] [blame] | 168 | getpass = fallback_getpass |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 169 | else: |
| 170 | getpass = win_getpass |
Jeremy Hylton | 88d2330 | 1999-10-18 22:25:22 +0000 | [diff] [blame] | 171 | else: |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 172 | getpass = unix_getpass |