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