1998-04-09 22:37:16 +02:00
|
|
|
"""Utilities to get a password and/or the current user name.
|
|
|
|
|
|
|
|
getpass(prompt) - prompt for a password, with echo turned off
|
|
|
|
getuser() - get the user name from the environment or password database
|
|
|
|
|
1999-10-19 00:25:22 +02:00
|
|
|
On Windows, the msvcrt module will be used.
|
|
|
|
On the Mac EasyDialogs.AskPassword is used, if available.
|
|
|
|
|
1998-04-09 22:37:16 +02:00
|
|
|
"""
|
|
|
|
|
2000-02-28 16:12:25 +01:00
|
|
|
# Authors: Piers Lauder (original)
|
|
|
|
# Guido van Rossum (Windows support and cleanup)
|
|
|
|
|
1999-10-19 00:25:22 +02:00
|
|
|
import sys
|
1998-04-09 22:37:16 +02:00
|
|
|
|
2001-01-21 00:34:12 +01:00
|
|
|
__all__ = ["getpass","getuser"]
|
|
|
|
|
2006-04-21 12:40:58 +02:00
|
|
|
def unix_getpass(prompt='Password: ', stream=None):
|
2001-01-15 00:47:14 +01:00
|
|
|
"""Prompt for a password, with echo turned off.
|
2006-04-21 12:40:58 +02:00
|
|
|
The prompt is written on stream, by default stdout.
|
1998-04-09 22:37:16 +02:00
|
|
|
|
2001-01-15 00:47:14 +01:00
|
|
|
Restore terminal settings at end.
|
|
|
|
"""
|
2006-04-21 12:40:58 +02:00
|
|
|
if stream is None:
|
|
|
|
stream = sys.stdout
|
1998-04-09 22:37:16 +02:00
|
|
|
|
2001-01-15 00:47:14 +01:00
|
|
|
try:
|
|
|
|
fd = sys.stdin.fileno()
|
|
|
|
except:
|
|
|
|
return default_getpass(prompt)
|
1998-04-09 22:37:16 +02:00
|
|
|
|
2001-01-15 00:47:14 +01:00
|
|
|
old = termios.tcgetattr(fd) # a copy to save
|
|
|
|
new = old[:]
|
1998-04-09 22:37:16 +02:00
|
|
|
|
2001-02-27 22:23:31 +01:00
|
|
|
new[3] = new[3] & ~termios.ECHO # 3 == 'lflags'
|
2001-01-15 00:47:14 +01:00
|
|
|
try:
|
2001-02-27 22:23:31 +01:00
|
|
|
termios.tcsetattr(fd, termios.TCSADRAIN, new)
|
2006-04-21 12:40:58 +02:00
|
|
|
passwd = _raw_input(prompt, stream)
|
2001-01-15 00:47:14 +01:00
|
|
|
finally:
|
2001-02-27 22:23:31 +01:00
|
|
|
termios.tcsetattr(fd, termios.TCSADRAIN, old)
|
1998-04-09 22:37:16 +02:00
|
|
|
|
2006-04-21 12:40:58 +02:00
|
|
|
stream.write('\n')
|
2001-01-15 00:47:14 +01:00
|
|
|
return passwd
|
1998-04-09 22:37:16 +02:00
|
|
|
|
|
|
|
|
2006-04-21 12:40:58 +02:00
|
|
|
def win_getpass(prompt='Password: ', stream=None):
|
2001-01-15 00:47:14 +01:00
|
|
|
"""Prompt for password with echo off, using Windows getch()."""
|
2001-08-30 17:07:44 +02:00
|
|
|
if sys.stdin is not sys.__stdin__:
|
2006-04-21 12:40:58 +02:00
|
|
|
return default_getpass(prompt, stream)
|
2001-01-15 00:47:14 +01:00
|
|
|
import msvcrt
|
|
|
|
for c in prompt:
|
2007-12-10 18:02:00 +01:00
|
|
|
msvcrt.putwch(c)
|
2001-01-15 00:47:14 +01:00
|
|
|
pw = ""
|
|
|
|
while 1:
|
2007-12-10 18:02:00 +01:00
|
|
|
c = msvcrt.getwch()
|
2001-01-15 00:47:14 +01:00
|
|
|
if c == '\r' or c == '\n':
|
|
|
|
break
|
|
|
|
if c == '\003':
|
|
|
|
raise KeyboardInterrupt
|
|
|
|
if c == '\b':
|
|
|
|
pw = pw[:-1]
|
|
|
|
else:
|
|
|
|
pw = pw + c
|
2007-12-10 18:02:00 +01:00
|
|
|
msvcrt.putwch('\r')
|
|
|
|
msvcrt.putwch('\n')
|
2001-01-15 00:47:14 +01:00
|
|
|
return pw
|
1998-04-09 22:37:16 +02:00
|
|
|
|
|
|
|
|
2006-04-21 12:40:58 +02:00
|
|
|
def default_getpass(prompt='Password: ', stream=None):
|
2007-02-09 06:37:30 +01:00
|
|
|
print("Warning: Problem with getpass. Passwords may be echoed.", file=sys.stderr)
|
2006-04-21 12:40:58 +02:00
|
|
|
return _raw_input(prompt, stream)
|
1998-07-28 21:28:43 +02:00
|
|
|
|
|
|
|
|
2006-04-21 12:40:58 +02:00
|
|
|
def _raw_input(prompt="", stream=None):
|
2006-03-17 07:49:51 +01:00
|
|
|
# This doesn't save the string in the GNU readline history.
|
2006-04-21 12:40:58 +02:00
|
|
|
if stream is None:
|
|
|
|
stream = sys.stdout
|
2001-01-15 00:47:14 +01:00
|
|
|
prompt = str(prompt)
|
|
|
|
if prompt:
|
2006-04-21 12:40:58 +02:00
|
|
|
stream.write(prompt)
|
2001-01-15 00:47:14 +01:00
|
|
|
line = sys.stdin.readline()
|
|
|
|
if not line:
|
|
|
|
raise EOFError
|
|
|
|
if line[-1] == '\n':
|
|
|
|
line = line[:-1]
|
|
|
|
return line
|
1998-04-13 22:22:21 +02:00
|
|
|
|
|
|
|
|
1998-04-09 22:37:16 +02:00
|
|
|
def getuser():
|
2001-01-15 00:47:14 +01:00
|
|
|
"""Get the username from the environment or password database.
|
1998-04-09 22:37:16 +02:00
|
|
|
|
2001-01-15 00:47:14 +01:00
|
|
|
First try various environment variables, then the password
|
|
|
|
database. This works on Windows as long as USERNAME is set.
|
1998-04-09 22:37:16 +02:00
|
|
|
|
2001-01-15 00:47:14 +01:00
|
|
|
"""
|
1998-04-09 22:37:16 +02:00
|
|
|
|
2001-01-15 00:47:14 +01:00
|
|
|
import os
|
1998-04-09 22:37:16 +02:00
|
|
|
|
2001-01-15 00:47:14 +01:00
|
|
|
for name in ('LOGNAME', 'USER', 'LNAME', 'USERNAME'):
|
|
|
|
user = os.environ.get(name)
|
|
|
|
if user:
|
|
|
|
return user
|
1998-04-09 22:37:16 +02:00
|
|
|
|
2001-01-15 00:47:14 +01:00
|
|
|
# If this fails, the exception will "explain" why
|
|
|
|
import pwd
|
|
|
|
return pwd.getpwuid(os.getuid())[0]
|
1999-10-19 00:25:22 +02:00
|
|
|
|
|
|
|
# Bind the name getpass to the appropriate function
|
|
|
|
try:
|
2001-02-27 22:23:31 +01:00
|
|
|
import termios
|
2002-11-21 00:15:54 +01: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-15 00:47:14 +01:00
|
|
|
try:
|
|
|
|
import msvcrt
|
|
|
|
except ImportError:
|
|
|
|
try:
|
|
|
|
from EasyDialogs import AskPassword
|
|
|
|
except ImportError:
|
|
|
|
getpass = default_getpass
|
|
|
|
else:
|
|
|
|
getpass = AskPassword
|
|
|
|
else:
|
|
|
|
getpass = win_getpass
|
1999-10-19 00:25:22 +02:00
|
|
|
else:
|
2001-01-15 00:47:14 +01:00
|
|
|
getpass = unix_getpass
|