blob: 66b1aeebcf546f82721c836226c20264f87f5c08 [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.
18
19 """
20
21 import sys
22 try:
Guido van Rossum0238a251998-09-22 02:38:42 +000023 fd = sys.stdin.fileno()
24 except:
25 return default_getpass(prompt)
26 try:
Guido van Rossumb5903ac1998-04-09 20:37:16 +000027 import termios, TERMIOS
28 except ImportError:
Guido van Rossumfb9b7fd1998-04-13 20:22:21 +000029 try:
30 import msvcrt
31 except ImportError:
32 return default_getpass(prompt)
33 else:
34 return win_getpass(prompt)
Guido van Rossumb5903ac1998-04-09 20:37:16 +000035
Guido van Rossumb5903ac1998-04-09 20:37:16 +000036 old = termios.tcgetattr(fd) # a copy to save
37 new = old[:]
38
39 new[3] = new[3] & ~TERMIOS.ECHO # 3 == 'lflags'
40 try:
41 termios.tcsetattr(fd, TERMIOS.TCSADRAIN, new)
Guido van Rossum1a7bab01998-07-28 19:28:43 +000042 passwd = _raw_input(prompt)
Guido van Rossumb5903ac1998-04-09 20:37:16 +000043 finally:
44 termios.tcsetattr(fd, TERMIOS.TCSADRAIN, old)
45
46 sys.stdout.write('\n')
47 return passwd
48
49
50def win_getpass(prompt='Password: '):
51 """Prompt for password with echo off, using Windows getch()."""
52 import msvcrt
53 for c in prompt:
54 msvcrt.putch(c)
55 pw = ""
56 while 1:
57 c = msvcrt.getch()
58 if c == '\r' or c == '\n':
59 break
Guido van Rossumc3da02e1998-06-12 14:28:38 +000060 if c == '\003':
61 raise KeyboardInterrupt
Guido van Rossumb5903ac1998-04-09 20:37:16 +000062 if c == '\b':
63 pw = pw[:-1]
64 else:
65 pw = pw + c
66 msvcrt.putch('\r')
67 msvcrt.putch('\n')
68 return pw
69
70
Guido van Rossumfb9b7fd1998-04-13 20:22:21 +000071def default_getpass(prompt='Password: '):
Guido van Rossum1a7bab01998-07-28 19:28:43 +000072 return _raw_input(prompt)
73
74
75def _raw_input(prompt=""):
76 # A raw_input() replacement that doesn't save the string in the
77 # GNU readline history.
78 import sys
79 prompt = str(prompt)
80 if prompt:
81 sys.stdout.write(prompt)
82 line = sys.stdin.readline()
83 if not line:
84 raise EOFError
85 if line[-1] == '\n':
86 line = line[:-1]
87 return line
Guido van Rossumfb9b7fd1998-04-13 20:22:21 +000088
89
Guido van Rossumb5903ac1998-04-09 20:37:16 +000090def getuser():
91 """Get the username from the environment or password database.
92
93 First try various environment variables, then the password
94 database. This works on Windows as long as USERNAME is set.
95
96 """
97
98 import os
99
100 for name in ('LOGNAME', 'USER', 'LNAME', 'USERNAME'):
101 user = os.environ.get(name)
102 if user:
103 return user
104
105 # If this fails, the exception will "explain" why
106 import pwd
107 return pwd.getpwuid(os.getuid())[0]