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