blob: 857188fd2f3accc915d1992b5b12a2315c77bddd [file] [log] [blame]
Guido van Rossumb5903ac1998-04-09 20:37:16 +00001"""Utilities to get a password and/or the current user name.
2
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003getpass(prompt[, stream]) - Prompt for a password, with echo turned off.
4getuser() - Get the user name from the environment or password database.
5
6GetPassWarning - This UserWarning is issued when getpass() cannot prevent
7 echoing of the password contents while reading.
Guido van Rossumb5903ac1998-04-09 20:37:16 +00008
Jeremy Hylton88d23301999-10-18 22:25:22 +00009On Windows, the msvcrt module will be used.
10On the Mac EasyDialogs.AskPassword is used, if available.
11
Guido van Rossumb5903ac1998-04-09 20:37:16 +000012"""
13
Guido van Rossum98d9fd32000-02-28 15:12:25 +000014# Authors: Piers Lauder (original)
15# Guido van Rossum (Windows support and cleanup)
Christian Heimes81ee3ef2008-05-04 22:42:01 +000016# Gregory P. Smith (tty support & GetPassWarning)
Guido van Rossum98d9fd32000-02-28 15:12:25 +000017
Christian Heimes81ee3ef2008-05-04 22:42:01 +000018import os, sys, warnings
Guido van Rossumb5903ac1998-04-09 20:37:16 +000019
Christian Heimes81ee3ef2008-05-04 22:42:01 +000020__all__ = ["getpass","getuser","GetPassWarning"]
21
22
23class GetPassWarning(UserWarning): pass
24
Skip Montanaroeccd02a2001-01-20 23:34:12 +000025
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000026def unix_getpass(prompt='Password: ', stream=None):
Tim Peters07e99cb2001-01-14 23:47:14 +000027 """Prompt for a password, with echo turned off.
Guido van Rossumb5903ac1998-04-09 20:37:16 +000028
Christian Heimes81ee3ef2008-05-04 22:42:01 +000029 Args:
30 prompt: Written on stream to ask for the input. Default: 'Password: '
31 stream: A writable file object to display the prompt. Defaults to
32 the tty. If no tty is available defaults to sys.stderr.
33 Returns:
34 The seKr3t input.
35 Raises:
36 EOFError: If our input tty or stdin was closed.
37 GetPassWarning: When we were unable to turn echo off on the input.
38
39 Always restores terminal settings before returning.
Tim Peters07e99cb2001-01-14 23:47:14 +000040 """
Christian Heimes81ee3ef2008-05-04 22:42:01 +000041 fd = None
42 tty = None
Tim Peters07e99cb2001-01-14 23:47:14 +000043 try:
Christian Heimes81ee3ef2008-05-04 22:42:01 +000044 # Always try reading and writing directly on the tty first.
45 fd = os.open('/dev/tty', os.O_RDWR|os.O_NOCTTY)
46 tty = os.fdopen(fd, 'w+', 1)
47 input = tty
48 if not stream:
49 stream = tty
50 except EnvironmentError as e:
51 # If that fails, see if stdin can be controlled.
52 try:
53 fd = sys.stdin.fileno()
54 except:
55 passwd = fallback_getpass(prompt, stream)
56 input = sys.stdin
57 if not stream:
58 stream = sys.stderr
Guido van Rossumb5903ac1998-04-09 20:37:16 +000059
Christian Heimes81ee3ef2008-05-04 22:42:01 +000060 if fd is not None:
61 passwd = None
62 try:
63 old = termios.tcgetattr(fd) # a copy to save
64 new = old[:]
65 new[3] &= ~termios.ECHO # 3 == 'lflags'
66 try:
67 termios.tcsetattr(fd, termios.TCSADRAIN, new)
68 passwd = _raw_input(prompt, stream, input=input)
69 finally:
70 termios.tcsetattr(fd, termios.TCSADRAIN, old)
71 except termios.error as e:
72 if passwd is not None:
73 # _raw_input succeeded. The final tcsetattr failed. Reraise
74 # instead of leaving the terminal in an unknown state.
75 raise
76 # We can't control the tty or stdin. Give up and use normal IO.
77 # fallback_getpass() raises an appropriate warning.
78 del input, tty # clean up unused file objects before blocking
79 passwd = fallback_getpass(prompt, stream)
Guido van Rossumb5903ac1998-04-09 20:37:16 +000080
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000081 stream.write('\n')
Tim Peters07e99cb2001-01-14 23:47:14 +000082 return passwd
Guido van Rossumb5903ac1998-04-09 20:37:16 +000083
84
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000085def win_getpass(prompt='Password: ', stream=None):
Tim Peters07e99cb2001-01-14 23:47:14 +000086 """Prompt for password with echo off, using Windows getch()."""
Guido van Rossum60250e22001-08-30 15:07:44 +000087 if sys.stdin is not sys.__stdin__:
Christian Heimes81ee3ef2008-05-04 22:42:01 +000088 return fallback_getpass(prompt, stream)
Tim Peters07e99cb2001-01-14 23:47:14 +000089 import msvcrt
90 for c in prompt:
Christian Heimes0ec88b32007-12-10 17:02:00 +000091 msvcrt.putwch(c)
Tim Peters07e99cb2001-01-14 23:47:14 +000092 pw = ""
93 while 1:
Christian Heimes0ec88b32007-12-10 17:02:00 +000094 c = msvcrt.getwch()
Tim Peters07e99cb2001-01-14 23:47:14 +000095 if c == '\r' or c == '\n':
96 break
97 if c == '\003':
98 raise KeyboardInterrupt
99 if c == '\b':
100 pw = pw[:-1]
101 else:
102 pw = pw + c
Christian Heimes0ec88b32007-12-10 17:02:00 +0000103 msvcrt.putwch('\r')
104 msvcrt.putwch('\n')
Tim Peters07e99cb2001-01-14 23:47:14 +0000105 return pw
Guido van Rossumb5903ac1998-04-09 20:37:16 +0000106
107
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000108def fallback_getpass(prompt='Password: ', stream=None):
109 warnings.warn("Can not control echo on the terminal.", GetPassWarning,
110 stacklevel=2)
111 if not stream:
112 stream = sys.stderr
113 print("Warning: Password input may be echoed.", file=stream)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000114 return _raw_input(prompt, stream)
Guido van Rossum1a7bab01998-07-28 19:28:43 +0000115
116
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000117def _raw_input(prompt="", stream=None, input=None):
Neal Norwitzce96f692006-03-17 06:49:51 +0000118 # This doesn't save the string in the GNU readline history.
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000119 if not stream:
120 stream = sys.stderr
121 if not input:
122 input = sys.stdin
Tim Peters07e99cb2001-01-14 23:47:14 +0000123 prompt = str(prompt)
124 if prompt:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000125 stream.write(prompt)
Guido van Rossumcfb83332008-01-01 16:30:47 +0000126 stream.flush()
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000127 line = input.readline()
Tim Peters07e99cb2001-01-14 23:47:14 +0000128 if not line:
129 raise EOFError
130 if line[-1] == '\n':
131 line = line[:-1]
132 return line
Guido van Rossumfb9b7fd1998-04-13 20:22:21 +0000133
134
Guido van Rossumb5903ac1998-04-09 20:37:16 +0000135def getuser():
Tim Peters07e99cb2001-01-14 23:47:14 +0000136 """Get the username from the environment or password database.
Guido van Rossumb5903ac1998-04-09 20:37:16 +0000137
Tim Peters07e99cb2001-01-14 23:47:14 +0000138 First try various environment variables, then the password
139 database. This works on Windows as long as USERNAME is set.
Guido van Rossumb5903ac1998-04-09 20:37:16 +0000140
Tim Peters07e99cb2001-01-14 23:47:14 +0000141 """
Guido van Rossumb5903ac1998-04-09 20:37:16 +0000142
Tim Peters07e99cb2001-01-14 23:47:14 +0000143 import os
Guido van Rossumb5903ac1998-04-09 20:37:16 +0000144
Tim Peters07e99cb2001-01-14 23:47:14 +0000145 for name in ('LOGNAME', 'USER', 'LNAME', 'USERNAME'):
146 user = os.environ.get(name)
147 if user:
148 return user
Guido van Rossumb5903ac1998-04-09 20:37:16 +0000149
Tim Peters07e99cb2001-01-14 23:47:14 +0000150 # If this fails, the exception will "explain" why
151 import pwd
152 return pwd.getpwuid(os.getuid())[0]
Jeremy Hylton88d23301999-10-18 22:25:22 +0000153
154# Bind the name getpass to the appropriate function
155try:
Fred Drake1191d012001-02-27 21:23:31 +0000156 import termios
Neal Norwitz201626e2002-11-20 23:15:54 +0000157 # it's possible there is an incompatible termios from the
158 # McMillan Installer, make sure we have a UNIX-compatible termios
159 termios.tcgetattr, termios.tcsetattr
160except (ImportError, AttributeError):
Tim Peters07e99cb2001-01-14 23:47:14 +0000161 try:
162 import msvcrt
163 except ImportError:
164 try:
165 from EasyDialogs import AskPassword
166 except ImportError:
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000167 getpass = fallback_getpass
Tim Peters07e99cb2001-01-14 23:47:14 +0000168 else:
169 getpass = AskPassword
170 else:
171 getpass = win_getpass
Jeremy Hylton88d23301999-10-18 22:25:22 +0000172else:
Tim Peters07e99cb2001-01-14 23:47:14 +0000173 getpass = unix_getpass