1998-04-09 17:37:16 -03:00
|
|
|
"""Utilities to get a password and/or the current user name.
|
|
|
|
|
2008-04-22 05:08:41 -03:00
|
|
|
getpass(prompt[, stream]) - Prompt for a password, with echo turned off.
|
|
|
|
getuser() - Get the user name from the environment or password database.
|
|
|
|
|
|
|
|
GetPassWarning - This UserWarning is issued when getpass() cannot prevent
|
|
|
|
echoing of the password contents while reading.
|
1998-04-09 17:37:16 -03:00
|
|
|
|
1999-10-18 19:25:22 -03:00
|
|
|
On Windows, the msvcrt module will be used.
|
|
|
|
On the Mac EasyDialogs.AskPassword is used, if available.
|
|
|
|
|
1998-04-09 17:37:16 -03:00
|
|
|
"""
|
|
|
|
|
2000-02-28 11:12:25 -04:00
|
|
|
# Authors: Piers Lauder (original)
|
|
|
|
# Guido van Rossum (Windows support and cleanup)
|
2008-04-22 05:08:41 -03:00
|
|
|
# Gregory P. Smith (tty support & GetPassWarning)
|
2000-02-28 11:12:25 -04:00
|
|
|
|
2008-04-22 05:08:41 -03:00
|
|
|
import os, sys, warnings
|
2001-01-20 19:34:12 -04:00
|
|
|
|
2008-04-22 05:08:41 -03:00
|
|
|
__all__ = ["getpass","getuser","GetPassWarning"]
|
1998-04-09 17:37:16 -03:00
|
|
|
|
|
|
|
|
2008-04-22 05:08:41 -03:00
|
|
|
class GetPassWarning(UserWarning): pass
|
2008-04-21 18:31:08 -03:00
|
|
|
|
1998-04-09 17:37:16 -03:00
|
|
|
|
2008-04-22 05:08:41 -03:00
|
|
|
def unix_getpass(prompt='Password: ', stream=None):
|
|
|
|
"""Prompt for a password, with echo turned off.
|
1998-04-09 17:37:16 -03:00
|
|
|
|
2008-04-22 05:08:41 -03:00
|
|
|
Args:
|
|
|
|
prompt: Written on stream to ask for the input. Default: 'Password: '
|
|
|
|
stream: A writable file object to display the prompt. Defaults to
|
|
|
|
the tty. If no tty is available defaults to sys.stderr.
|
|
|
|
Returns:
|
|
|
|
The seKr3t input.
|
|
|
|
Raises:
|
|
|
|
EOFError: If our input tty or stdin was closed.
|
|
|
|
GetPassWarning: When we were unable to turn echo off on the input.
|
|
|
|
|
|
|
|
Always restores terminal settings before returning.
|
|
|
|
"""
|
|
|
|
fd = None
|
|
|
|
tty = None
|
2001-01-14 19:47:14 -04:00
|
|
|
try:
|
2008-04-22 05:08:41 -03:00
|
|
|
# Always try reading and writing directly on the tty first.
|
|
|
|
fd = os.open('/dev/tty', os.O_RDWR|os.O_NOCTTY)
|
|
|
|
tty = os.fdopen(fd, 'w+', 1)
|
|
|
|
input = tty
|
|
|
|
if not stream:
|
|
|
|
stream = tty
|
|
|
|
except EnvironmentError, e:
|
|
|
|
# If that fails, see if stdin can be controlled.
|
|
|
|
try:
|
|
|
|
fd = sys.stdin.fileno()
|
2009-09-16 23:46:54 -03:00
|
|
|
except (AttributeError, ValueError):
|
2008-04-22 05:08:41 -03:00
|
|
|
passwd = fallback_getpass(prompt, stream)
|
|
|
|
input = sys.stdin
|
|
|
|
if not stream:
|
|
|
|
stream = sys.stderr
|
|
|
|
|
|
|
|
if fd is not None:
|
|
|
|
passwd = None
|
|
|
|
try:
|
|
|
|
old = termios.tcgetattr(fd) # a copy to save
|
|
|
|
new = old[:]
|
2009-10-31 18:26:08 -03:00
|
|
|
new[3] &= ~(termios.ECHO|termios.ISIG) # 3 == 'lflags'
|
|
|
|
tcsetattr_flags = termios.TCSAFLUSH
|
|
|
|
if hasattr(termios, 'TCSASOFT'):
|
|
|
|
tcsetattr_flags |= termios.TCSASOFT
|
2008-04-22 05:08:41 -03:00
|
|
|
try:
|
2009-10-31 18:26:08 -03:00
|
|
|
termios.tcsetattr(fd, tcsetattr_flags, new)
|
2008-04-22 05:08:41 -03:00
|
|
|
passwd = _raw_input(prompt, stream, input=input)
|
|
|
|
finally:
|
2009-10-31 18:26:08 -03:00
|
|
|
termios.tcsetattr(fd, tcsetattr_flags, old)
|
|
|
|
stream.flush() # issue7208
|
2008-04-22 05:08:41 -03:00
|
|
|
except termios.error, e:
|
|
|
|
if passwd is not None:
|
|
|
|
# _raw_input succeeded. The final tcsetattr failed. Reraise
|
|
|
|
# instead of leaving the terminal in an unknown state.
|
|
|
|
raise
|
|
|
|
# We can't control the tty or stdin. Give up and use normal IO.
|
|
|
|
# fallback_getpass() raises an appropriate warning.
|
|
|
|
del input, tty # clean up unused file objects before blocking
|
|
|
|
passwd = fallback_getpass(prompt, stream)
|
1998-04-09 17:37:16 -03:00
|
|
|
|
2006-03-31 14:42:16 -04:00
|
|
|
stream.write('\n')
|
2001-01-14 19:47:14 -04:00
|
|
|
return passwd
|
1998-04-09 17:37:16 -03:00
|
|
|
|
|
|
|
|
2006-03-31 14:42:16 -04:00
|
|
|
def win_getpass(prompt='Password: ', stream=None):
|
2001-01-14 19:47:14 -04:00
|
|
|
"""Prompt for password with echo off, using Windows getch()."""
|
2001-08-30 12:07:44 -03:00
|
|
|
if sys.stdin is not sys.__stdin__:
|
2008-04-22 05:08:41 -03:00
|
|
|
return fallback_getpass(prompt, stream)
|
2001-01-14 19:47:14 -04:00
|
|
|
import msvcrt
|
|
|
|
for c in prompt:
|
|
|
|
msvcrt.putch(c)
|
|
|
|
pw = ""
|
|
|
|
while 1:
|
|
|
|
c = msvcrt.getch()
|
|
|
|
if c == '\r' or c == '\n':
|
|
|
|
break
|
|
|
|
if c == '\003':
|
|
|
|
raise KeyboardInterrupt
|
|
|
|
if c == '\b':
|
|
|
|
pw = pw[:-1]
|
|
|
|
else:
|
|
|
|
pw = pw + c
|
|
|
|
msvcrt.putch('\r')
|
|
|
|
msvcrt.putch('\n')
|
|
|
|
return pw
|
1998-04-09 17:37:16 -03:00
|
|
|
|
|
|
|
|
2008-04-22 05:08:41 -03:00
|
|
|
def fallback_getpass(prompt='Password: ', stream=None):
|
|
|
|
warnings.warn("Can not control echo on the terminal.", GetPassWarning,
|
|
|
|
stacklevel=2)
|
|
|
|
if not stream:
|
|
|
|
stream = sys.stderr
|
|
|
|
print >>stream, "Warning: Password input may be echoed."
|
2006-03-31 14:42:16 -04:00
|
|
|
return _raw_input(prompt, stream)
|
1998-07-28 16:28:43 -03:00
|
|
|
|
|
|
|
|
2008-04-22 05:08:41 -03:00
|
|
|
def _raw_input(prompt="", stream=None, input=None):
|
2001-01-14 19:47:14 -04:00
|
|
|
# A raw_input() replacement that doesn't save the string in the
|
|
|
|
# GNU readline history.
|
2008-04-22 05:08:41 -03:00
|
|
|
if not stream:
|
|
|
|
stream = sys.stderr
|
|
|
|
if not input:
|
|
|
|
input = sys.stdin
|
2001-01-14 19:47:14 -04:00
|
|
|
prompt = str(prompt)
|
|
|
|
if prompt:
|
2006-03-31 14:42:16 -04:00
|
|
|
stream.write(prompt)
|
2008-04-22 05:08:41 -03:00
|
|
|
stream.flush()
|
2009-10-31 18:26:08 -03:00
|
|
|
# NOTE: The Python C API calls flockfile() (and unlock) during readline.
|
2008-04-22 05:08:41 -03:00
|
|
|
line = input.readline()
|
2001-01-14 19:47:14 -04:00
|
|
|
if not line:
|
|
|
|
raise EOFError
|
|
|
|
if line[-1] == '\n':
|
|
|
|
line = line[:-1]
|
|
|
|
return line
|
1998-04-13 17:22:21 -03:00
|
|
|
|
|
|
|
|
1998-04-09 17:37:16 -03:00
|
|
|
def getuser():
|
2001-01-14 19:47:14 -04:00
|
|
|
"""Get the username from the environment or password database.
|
1998-04-09 17:37:16 -03:00
|
|
|
|
2001-01-14 19:47:14 -04:00
|
|
|
First try various environment variables, then the password
|
|
|
|
database. This works on Windows as long as USERNAME is set.
|
1998-04-09 17:37:16 -03:00
|
|
|
|
2001-01-14 19:47:14 -04:00
|
|
|
"""
|
1998-04-09 17:37:16 -03:00
|
|
|
|
2001-01-14 19:47:14 -04:00
|
|
|
import os
|
1998-04-09 17:37:16 -03:00
|
|
|
|
2001-01-14 19:47:14 -04:00
|
|
|
for name in ('LOGNAME', 'USER', 'LNAME', 'USERNAME'):
|
|
|
|
user = os.environ.get(name)
|
|
|
|
if user:
|
|
|
|
return user
|
1998-04-09 17:37:16 -03:00
|
|
|
|
2001-01-14 19:47:14 -04:00
|
|
|
# If this fails, the exception will "explain" why
|
|
|
|
import pwd
|
|
|
|
return pwd.getpwuid(os.getuid())[0]
|
1999-10-18 19:25:22 -03:00
|
|
|
|
|
|
|
# Bind the name getpass to the appropriate function
|
|
|
|
try:
|
2001-02-27 17:23:31 -04:00
|
|
|
import termios
|
2002-11-20 19:15:54 -04:00
|
|
|
# it's possible there is an incompatible termios from the
|
|
|
|
# McMillan Installer, make sure we have a UNIX-compatible termios
|
|
|
|
termios.tcgetattr, termios.tcsetattr
|
|
|
|
except (ImportError, AttributeError):
|
2001-01-14 19:47:14 -04:00
|
|
|
try:
|
|
|
|
import msvcrt
|
|
|
|
except ImportError:
|
|
|
|
try:
|
|
|
|
from EasyDialogs import AskPassword
|
|
|
|
except ImportError:
|
2008-04-22 05:08:41 -03:00
|
|
|
getpass = fallback_getpass
|
2001-01-14 19:47:14 -04:00
|
|
|
else:
|
|
|
|
getpass = AskPassword
|
|
|
|
else:
|
|
|
|
getpass = win_getpass
|
1999-10-18 19:25:22 -03:00
|
|
|
else:
|
2001-01-14 19:47:14 -04:00
|
|
|
getpass = unix_getpass
|