- Notifications
You must be signed in to change notification settings - Fork 31.7k
/
Copy pathgetpass.py
185 lines (156 loc) · 5.85 KB
/
getpass.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
"""Utilities to get a password and/or the current user name.
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.
On Windows, the msvcrt module will be used.
"""
# Authors: Piers Lauder (original)
# Guido van Rossum (Windows support and cleanup)
# Gregory P. Smith (tty support & GetPassWarning)
importcontextlib
importio
importos
importsys
importwarnings
__all__= ["getpass","getuser","GetPassWarning"]
classGetPassWarning(UserWarning): pass
defunix_getpass(prompt='Password: ', stream=None):
"""Prompt for a password, with echo turned off.
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.
"""
passwd=None
withcontextlib.ExitStack() asstack:
try:
# Always try reading and writing directly on the tty first.
fd=os.open('/dev/tty', os.O_RDWR|os.O_NOCTTY)
tty=io.FileIO(fd, 'w+')
stack.enter_context(tty)
input=io.TextIOWrapper(tty)
stack.enter_context(input)
ifnotstream:
stream=input
exceptOSErrorase:
# If that fails, see if stdin can be controlled.
stack.close()
try:
fd=sys.stdin.fileno()
except (AttributeError, ValueError):
fd=None
passwd=fallback_getpass(prompt, stream)
input=sys.stdin
ifnotstream:
stream=sys.stderr
iffdisnotNone:
try:
old=termios.tcgetattr(fd) # a copy to save
new=old[:]
new[3] &=~termios.ECHO# 3 == 'lflags'
tcsetattr_flags=termios.TCSAFLUSH
ifhasattr(termios, 'TCSASOFT'):
tcsetattr_flags|=termios.TCSASOFT
try:
termios.tcsetattr(fd, tcsetattr_flags, new)
passwd=_raw_input(prompt, stream, input=input)
finally:
termios.tcsetattr(fd, tcsetattr_flags, old)
stream.flush() # issue7208
excepttermios.error:
ifpasswdisnotNone:
# _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.
ifstreamisnotinput:
# clean up unused file objects before blocking
stack.close()
passwd=fallback_getpass(prompt, stream)
stream.write('\n')
returnpasswd
defwin_getpass(prompt='Password: ', stream=None):
"""Prompt for password with echo off, using Windows getch()."""
ifsys.stdinisnotsys.__stdin__:
returnfallback_getpass(prompt, stream)
forcinprompt:
msvcrt.putwch(c)
pw=""
while1:
c=msvcrt.getwch()
ifc=='\r'orc=='\n':
break
ifc=='\003':
raiseKeyboardInterrupt
ifc=='\b':
pw=pw[:-1]
else:
pw=pw+c
msvcrt.putwch('\r')
msvcrt.putwch('\n')
returnpw
deffallback_getpass(prompt='Password: ', stream=None):
warnings.warn("Can not control echo on the terminal.", GetPassWarning,
stacklevel=2)
ifnotstream:
stream=sys.stderr
print("Warning: Password input may be echoed.", file=stream)
return_raw_input(prompt, stream)
def_raw_input(prompt="", stream=None, input=None):
# This doesn't save the string in the GNU readline history.
ifnotstream:
stream=sys.stderr
ifnotinput:
input=sys.stdin
prompt=str(prompt)
ifprompt:
try:
stream.write(prompt)
exceptUnicodeEncodeError:
# Use replace error handler to get as much as possible printed.
prompt=prompt.encode(stream.encoding, 'replace')
prompt=prompt.decode(stream.encoding)
stream.write(prompt)
stream.flush()
# NOTE: The Python C API calls flockfile() (and unlock) during readline.
line=input.readline()
ifnotline:
raiseEOFError
ifline[-1] =='\n':
line=line[:-1]
returnline
defgetuser():
"""Get the username from the environment or password database.
First try various environment variables, then the password
database. This works on Windows as long as USERNAME is set.
"""
fornamein ('LOGNAME', 'USER', 'LNAME', 'USERNAME'):
user=os.environ.get(name)
ifuser:
returnuser
# If this fails, the exception will "explain" why
importpwd
returnpwd.getpwuid(os.getuid())[0]
# Bind the name getpass to the appropriate function
try:
importtermios
# 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):
try:
importmsvcrt
exceptImportError:
getpass=fallback_getpass
else:
getpass=win_getpass
else:
getpass=unix_getpass