blob: 6b786122ecb71ce9146112eaaa52786fdcbf90f5 [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
Jeremy Hylton88d23301999-10-18 22:25:22 +00006On Windows, the msvcrt module will be used.
7On the Mac EasyDialogs.AskPassword is used, if available.
8
Guido van Rossumb5903ac1998-04-09 20:37:16 +00009"""
10
Guido van Rossum98d9fd32000-02-28 15:12:25 +000011# Authors: Piers Lauder (original)
12# Guido van Rossum (Windows support and cleanup)
13
Jeremy Hylton88d23301999-10-18 22:25:22 +000014import sys
Guido van Rossumb5903ac1998-04-09 20:37:16 +000015
Skip Montanaroeccd02a2001-01-20 23:34:12 +000016__all__ = ["getpass","getuser"]
17
Georg Brandl338ef7d2006-03-31 18:42:16 +000018def unix_getpass(prompt='Password: ', stream=None):
Tim Peters07e99cb2001-01-14 23:47:14 +000019 """Prompt for a password, with echo turned off.
Georg Brandl338ef7d2006-03-31 18:42:16 +000020 The prompt is written on stream, by default stdout.
Guido van Rossumb5903ac1998-04-09 20:37:16 +000021
Tim Peters07e99cb2001-01-14 23:47:14 +000022 Restore terminal settings at end.
23 """
Georg Brandl338ef7d2006-03-31 18:42:16 +000024 if stream is None:
25 stream = sys.stdout
Guido van Rossumb5903ac1998-04-09 20:37:16 +000026
Tim Peters07e99cb2001-01-14 23:47:14 +000027 try:
28 fd = sys.stdin.fileno()
29 except:
30 return default_getpass(prompt)
Guido van Rossumb5903ac1998-04-09 20:37:16 +000031
Tim Peters07e99cb2001-01-14 23:47:14 +000032 old = termios.tcgetattr(fd) # a copy to save
33 new = old[:]
Guido van Rossumb5903ac1998-04-09 20:37:16 +000034
Fred Drake1191d012001-02-27 21:23:31 +000035 new[3] = new[3] & ~termios.ECHO # 3 == 'lflags'
Tim Peters07e99cb2001-01-14 23:47:14 +000036 try:
Fred Drake1191d012001-02-27 21:23:31 +000037 termios.tcsetattr(fd, termios.TCSADRAIN, new)
Georg Brandl338ef7d2006-03-31 18:42:16 +000038 passwd = _raw_input(prompt, stream)
Tim Peters07e99cb2001-01-14 23:47:14 +000039 finally:
Fred Drake1191d012001-02-27 21:23:31 +000040 termios.tcsetattr(fd, termios.TCSADRAIN, old)
Guido van Rossumb5903ac1998-04-09 20:37:16 +000041
Georg Brandl338ef7d2006-03-31 18:42:16 +000042 stream.write('\n')
Tim Peters07e99cb2001-01-14 23:47:14 +000043 return passwd
Guido van Rossumb5903ac1998-04-09 20:37:16 +000044
45
Georg Brandl338ef7d2006-03-31 18:42:16 +000046def win_getpass(prompt='Password: ', stream=None):
Tim Peters07e99cb2001-01-14 23:47:14 +000047 """Prompt for password with echo off, using Windows getch()."""
Guido van Rossum60250e22001-08-30 15:07:44 +000048 if sys.stdin is not sys.__stdin__:
Georg Brandl338ef7d2006-03-31 18:42:16 +000049 return default_getpass(prompt, stream)
Tim Peters07e99cb2001-01-14 23:47:14 +000050 import msvcrt
51 for c in prompt:
52 msvcrt.putch(c)
53 pw = ""
54 while 1:
55 c = msvcrt.getch()
56 if c == '\r' or c == '\n':
57 break
58 if c == '\003':
59 raise KeyboardInterrupt
60 if c == '\b':
61 pw = pw[:-1]
62 else:
63 pw = pw + c
64 msvcrt.putch('\r')
65 msvcrt.putch('\n')
66 return pw
Guido van Rossumb5903ac1998-04-09 20:37:16 +000067
68
Georg Brandl338ef7d2006-03-31 18:42:16 +000069def default_getpass(prompt='Password: ', stream=None):
70 print >>sys.stderr, "Warning: Problem with getpass. Passwords may be echoed."
71 return _raw_input(prompt, stream)
Guido van Rossum1a7bab01998-07-28 19:28:43 +000072
73
Georg Brandl338ef7d2006-03-31 18:42:16 +000074def _raw_input(prompt="", stream=None):
Tim Peters07e99cb2001-01-14 23:47:14 +000075 # A raw_input() replacement that doesn't save the string in the
76 # GNU readline history.
Georg Brandl338ef7d2006-03-31 18:42:16 +000077 if stream is None:
78 stream = sys.stdout
Tim Peters07e99cb2001-01-14 23:47:14 +000079 prompt = str(prompt)
80 if prompt:
Georg Brandl338ef7d2006-03-31 18:42:16 +000081 stream.write(prompt)
Tim Peters07e99cb2001-01-14 23:47:14 +000082 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():
Tim Peters07e99cb2001-01-14 23:47:14 +000091 """Get the username from the environment or password database.
Guido van Rossumb5903ac1998-04-09 20:37:16 +000092
Tim Peters07e99cb2001-01-14 23:47:14 +000093 First try various environment variables, then the password
94 database. This works on Windows as long as USERNAME is set.
Guido van Rossumb5903ac1998-04-09 20:37:16 +000095
Tim Peters07e99cb2001-01-14 23:47:14 +000096 """
Guido van Rossumb5903ac1998-04-09 20:37:16 +000097
Tim Peters07e99cb2001-01-14 23:47:14 +000098 import os
Guido van Rossumb5903ac1998-04-09 20:37:16 +000099
Tim Peters07e99cb2001-01-14 23:47:14 +0000100 for name in ('LOGNAME', 'USER', 'LNAME', 'USERNAME'):
101 user = os.environ.get(name)
102 if user:
103 return user
Guido van Rossumb5903ac1998-04-09 20:37:16 +0000104
Tim Peters07e99cb2001-01-14 23:47:14 +0000105 # If this fails, the exception will "explain" why
106 import pwd
107 return pwd.getpwuid(os.getuid())[0]
Jeremy Hylton88d23301999-10-18 22:25:22 +0000108
109# Bind the name getpass to the appropriate function
110try:
Fred Drake1191d012001-02-27 21:23:31 +0000111 import termios
Neal Norwitz201626e2002-11-20 23:15:54 +0000112 # it's possible there is an incompatible termios from the
113 # McMillan Installer, make sure we have a UNIX-compatible termios
114 termios.tcgetattr, termios.tcsetattr
115except (ImportError, AttributeError):
Tim Peters07e99cb2001-01-14 23:47:14 +0000116 try:
117 import msvcrt
118 except ImportError:
119 try:
120 from EasyDialogs import AskPassword
121 except ImportError:
122 getpass = default_getpass
123 else:
124 getpass = AskPassword
125 else:
126 getpass = win_getpass
Jeremy Hylton88d23301999-10-18 22:25:22 +0000127else:
Tim Peters07e99cb2001-01-14 23:47:14 +0000128 getpass = unix_getpass