blob: 2ac6fd7f38ad46634c0eb19c6ae0601ccf3a04c3 [file] [log] [blame]
Guido van Rossumb5903ac1998-04-09 20:37:16 +00001"""Utilities to get a password and/or the current user name.
2
Gregory P. Smith19b44112008-04-22 08:08:41 +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)
Gregory P. Smith19b44112008-04-22 08:08:41 +000016# Gregory P. Smith (tty support & GetPassWarning)
Guido van Rossum98d9fd32000-02-28 15:12:25 +000017
Gregory P. Smith19b44112008-04-22 08:08:41 +000018import os, sys, warnings
Guido van Rossumb5903ac1998-04-09 20:37:16 +000019
Gregory P. Smith19b44112008-04-22 08:08:41 +000020__all__ = ["getpass","getuser","GetPassWarning"]
21
22
23class GetPassWarning(UserWarning): pass
24
Skip Montanaroeccd02a2001-01-20 23:34:12 +000025
Georg Brandl338ef7d2006-03-31 18:42:16 +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
Gregory P. Smith19b44112008-04-22 08:08:41 +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 """
Gregory P. Smith19b44112008-04-22 08:08:41 +000041 fd = None
42 tty = None
Tim Peters07e99cb2001-01-14 23:47:14 +000043 try:
Gregory P. Smith19b44112008-04-22 08:08:41 +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, e:
51 # If that fails, see if stdin can be controlled.
52 try:
53 fd = sys.stdin.fileno()
Benjamin Peterson4d714cb2009-09-17 02:46:54 +000054 except (AttributeError, ValueError):
Gregory P. Smith19b44112008-04-22 08:08:41 +000055 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
Gregory P. Smith19b44112008-04-22 08:08:41 +000060 if fd is not None:
61 passwd = None
62 try:
63 old = termios.tcgetattr(fd) # a copy to save
64 new = old[:]
Senthil Kumaran83627ed2011-04-26 21:02:26 +080065 new[3] &= ~termios.ECHO # 3 == 'lflags'
Gregory P. Smith29b53652009-10-31 21:26:08 +000066 tcsetattr_flags = termios.TCSAFLUSH
67 if hasattr(termios, 'TCSASOFT'):
68 tcsetattr_flags |= termios.TCSASOFT
Gregory P. Smith19b44112008-04-22 08:08:41 +000069 try:
Gregory P. Smith29b53652009-10-31 21:26:08 +000070 termios.tcsetattr(fd, tcsetattr_flags, new)
Gregory P. Smith19b44112008-04-22 08:08:41 +000071 passwd = _raw_input(prompt, stream, input=input)
72 finally:
Gregory P. Smith29b53652009-10-31 21:26:08 +000073 termios.tcsetattr(fd, tcsetattr_flags, old)
74 stream.flush() # issue7208
Gregory P. Smith19b44112008-04-22 08:08:41 +000075 except termios.error, e:
76 if passwd is not None:
77 # _raw_input succeeded. The final tcsetattr failed. Reraise
78 # instead of leaving the terminal in an unknown state.
79 raise
80 # We can't control the tty or stdin. Give up and use normal IO.
81 # fallback_getpass() raises an appropriate warning.
82 del input, tty # clean up unused file objects before blocking
83 passwd = fallback_getpass(prompt, stream)
Guido van Rossumb5903ac1998-04-09 20:37:16 +000084
Georg Brandl338ef7d2006-03-31 18:42:16 +000085 stream.write('\n')
Tim Peters07e99cb2001-01-14 23:47:14 +000086 return passwd
Guido van Rossumb5903ac1998-04-09 20:37:16 +000087
88
Georg Brandl338ef7d2006-03-31 18:42:16 +000089def win_getpass(prompt='Password: ', stream=None):
Tim Peters07e99cb2001-01-14 23:47:14 +000090 """Prompt for password with echo off, using Windows getch()."""
Guido van Rossum60250e22001-08-30 15:07:44 +000091 if sys.stdin is not sys.__stdin__:
Gregory P. Smith19b44112008-04-22 08:08:41 +000092 return fallback_getpass(prompt, stream)
Tim Peters07e99cb2001-01-14 23:47:14 +000093 import msvcrt
94 for c in prompt:
95 msvcrt.putch(c)
96 pw = ""
97 while 1:
98 c = msvcrt.getch()
99 if c == '\r' or c == '\n':
100 break
101 if c == '\003':
102 raise KeyboardInterrupt
103 if c == '\b':
104 pw = pw[:-1]
105 else:
106 pw = pw + c
107 msvcrt.putch('\r')
108 msvcrt.putch('\n')
109 return pw
Guido van Rossumb5903ac1998-04-09 20:37:16 +0000110
111
Gregory P. Smith19b44112008-04-22 08:08:41 +0000112def fallback_getpass(prompt='Password: ', stream=None):
113 warnings.warn("Can not control echo on the terminal.", GetPassWarning,
114 stacklevel=2)
115 if not stream:
116 stream = sys.stderr
117 print >>stream, "Warning: Password input may be echoed."
Georg Brandl338ef7d2006-03-31 18:42:16 +0000118 return _raw_input(prompt, stream)
Guido van Rossum1a7bab01998-07-28 19:28:43 +0000119
120
Gregory P. Smith19b44112008-04-22 08:08:41 +0000121def _raw_input(prompt="", stream=None, input=None):
Tim Peters07e99cb2001-01-14 23:47:14 +0000122 # A raw_input() replacement that doesn't save the string in the
123 # GNU readline history.
Gregory P. Smith19b44112008-04-22 08:08:41 +0000124 if not stream:
125 stream = sys.stderr
126 if not input:
127 input = sys.stdin
Tim Peters07e99cb2001-01-14 23:47:14 +0000128 prompt = str(prompt)
129 if prompt:
Georg Brandl338ef7d2006-03-31 18:42:16 +0000130 stream.write(prompt)
Gregory P. Smith19b44112008-04-22 08:08:41 +0000131 stream.flush()
Gregory P. Smith29b53652009-10-31 21:26:08 +0000132 # NOTE: The Python C API calls flockfile() (and unlock) during readline.
Gregory P. Smith19b44112008-04-22 08:08:41 +0000133 line = input.readline()
Tim Peters07e99cb2001-01-14 23:47:14 +0000134 if not line:
135 raise EOFError
136 if line[-1] == '\n':
137 line = line[:-1]
138 return line
Guido van Rossumfb9b7fd1998-04-13 20:22:21 +0000139
140
Guido van Rossumb5903ac1998-04-09 20:37:16 +0000141def getuser():
Tim Peters07e99cb2001-01-14 23:47:14 +0000142 """Get the username from the environment or password database.
Guido van Rossumb5903ac1998-04-09 20:37:16 +0000143
Tim Peters07e99cb2001-01-14 23:47:14 +0000144 First try various environment variables, then the password
145 database. This works on Windows as long as USERNAME is set.
Guido van Rossumb5903ac1998-04-09 20:37:16 +0000146
Tim Peters07e99cb2001-01-14 23:47:14 +0000147 """
Guido van Rossumb5903ac1998-04-09 20:37:16 +0000148
Tim Peters07e99cb2001-01-14 23:47:14 +0000149 import os
Guido van Rossumb5903ac1998-04-09 20:37:16 +0000150
Tim Peters07e99cb2001-01-14 23:47:14 +0000151 for name in ('LOGNAME', 'USER', 'LNAME', 'USERNAME'):
152 user = os.environ.get(name)
153 if user:
154 return user
Guido van Rossumb5903ac1998-04-09 20:37:16 +0000155
Tim Peters07e99cb2001-01-14 23:47:14 +0000156 # If this fails, the exception will "explain" why
157 import pwd
158 return pwd.getpwuid(os.getuid())[0]
Jeremy Hylton88d23301999-10-18 22:25:22 +0000159
160# Bind the name getpass to the appropriate function
161try:
Fred Drake1191d012001-02-27 21:23:31 +0000162 import termios
Neal Norwitz201626e2002-11-20 23:15:54 +0000163 # it's possible there is an incompatible termios from the
164 # McMillan Installer, make sure we have a UNIX-compatible termios
165 termios.tcgetattr, termios.tcsetattr
166except (ImportError, AttributeError):
Tim Peters07e99cb2001-01-14 23:47:14 +0000167 try:
168 import msvcrt
169 except ImportError:
170 try:
171 from EasyDialogs import AskPassword
172 except ImportError:
Gregory P. Smith19b44112008-04-22 08:08:41 +0000173 getpass = fallback_getpass
Tim Peters07e99cb2001-01-14 23:47:14 +0000174 else:
175 getpass = AskPassword
176 else:
177 getpass = win_getpass
Jeremy Hylton88d23301999-10-18 22:25:22 +0000178else:
Tim Peters07e99cb2001-01-14 23:47:14 +0000179 getpass = unix_getpass