blob: 952e023d1efe1b3b6f30e84e56515c2bb7737439 [file] [log] [blame]
Guido van Rossumb5903ac1998-04-09 20:37:16 +00001"""Utilities to get a password and/or the current user name.
2
3getpass(prompt) - prompt for a password, with echo turned off
4getuser() - get the user name from the environment or password database
5
6Authors: Piers Lauder (original)
7 Guido van Rossum (Windows support and cleanup)
8"""
9
10
11def getpass(prompt='Password: '):
12 """Prompt for a password, with echo turned off.
13
14 Restore terminal settings at end.
15
16 On Windows, this calls win_getpass(prompt) which uses the
17 msvcrt module to get the same effect.
Guido van Rossumc7317231999-02-11 14:41:46 +000018
19 On the Mac EasyDialogs.AskPassword is used, if available.
Guido van Rossumb5903ac1998-04-09 20:37:16 +000020
21 """
22
23 import sys
24 try:
Guido van Rossum0238a251998-09-22 02:38:42 +000025 fd = sys.stdin.fileno()
26 except:
27 return default_getpass(prompt)
28 try:
Guido van Rossumb5903ac1998-04-09 20:37:16 +000029 import termios, TERMIOS
30 except ImportError:
Guido van Rossumfb9b7fd1998-04-13 20:22:21 +000031 try:
32 import msvcrt
33 except ImportError:
Guido van Rossumc7317231999-02-11 14:41:46 +000034 try:
35 from EasyDialogs import AskPassword
36 except ImportError:
37 return default_getpass(prompt)
38 else:
39 return AskPassword(prompt)
Guido van Rossumfb9b7fd1998-04-13 20:22:21 +000040 else:
41 return win_getpass(prompt)
Guido van Rossumb5903ac1998-04-09 20:37:16 +000042
Guido van Rossumb5903ac1998-04-09 20:37:16 +000043 old = termios.tcgetattr(fd) # a copy to save
44 new = old[:]
45
46 new[3] = new[3] & ~TERMIOS.ECHO # 3 == 'lflags'
47 try:
48 termios.tcsetattr(fd, TERMIOS.TCSADRAIN, new)
Guido van Rossum1a7bab01998-07-28 19:28:43 +000049 passwd = _raw_input(prompt)
Guido van Rossumb5903ac1998-04-09 20:37:16 +000050 finally:
51 termios.tcsetattr(fd, TERMIOS.TCSADRAIN, old)
52
53 sys.stdout.write('\n')
54 return passwd
55
56
57def win_getpass(prompt='Password: '):
58 """Prompt for password with echo off, using Windows getch()."""
59 import msvcrt
60 for c in prompt:
61 msvcrt.putch(c)
62 pw = ""
63 while 1:
64 c = msvcrt.getch()
65 if c == '\r' or c == '\n':
66 break
Guido van Rossumc3da02e1998-06-12 14:28:38 +000067 if c == '\003':
68 raise KeyboardInterrupt
Guido van Rossumb5903ac1998-04-09 20:37:16 +000069 if c == '\b':
70 pw = pw[:-1]
71 else:
72 pw = pw + c
73 msvcrt.putch('\r')
74 msvcrt.putch('\n')
75 return pw
76
77
Guido van Rossumfb9b7fd1998-04-13 20:22:21 +000078def default_getpass(prompt='Password: '):
Guido van Rossum1a7bab01998-07-28 19:28:43 +000079 return _raw_input(prompt)
80
81
82def _raw_input(prompt=""):
83 # A raw_input() replacement that doesn't save the string in the
84 # GNU readline history.
85 import sys
86 prompt = str(prompt)
87 if prompt:
88 sys.stdout.write(prompt)
89 line = sys.stdin.readline()
90 if not line:
91 raise EOFError
92 if line[-1] == '\n':
93 line = line[:-1]
94 return line
Guido van Rossumfb9b7fd1998-04-13 20:22:21 +000095
96
Guido van Rossumb5903ac1998-04-09 20:37:16 +000097def getuser():
98 """Get the username from the environment or password database.
99
100 First try various environment variables, then the password
101 database. This works on Windows as long as USERNAME is set.
102
103 """
104
105 import os
106
107 for name in ('LOGNAME', 'USER', 'LNAME', 'USERNAME'):
108 user = os.environ.get(name)
109 if user:
110 return user
111
112 # If this fails, the exception will "explain" why
113 import pwd
114 return pwd.getpwuid(os.getuid())[0]