| 
									
										
										
										
											1998-04-09 20:37:16 +00:00
										 |  |  | """Utilities to get a password and/or the current user name.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | getpass(prompt) - prompt for a password, with echo turned off | 
					
						
							|  |  |  | getuser() - get the user name from the environment or password database | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-10-18 22:25:22 +00:00
										 |  |  | On Windows, the msvcrt module will be used. | 
					
						
							|  |  |  | On the Mac EasyDialogs.AskPassword is used, if available. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-04-09 20:37:16 +00:00
										 |  |  | """
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-02-28 15:12:25 +00:00
										 |  |  | # Authors: Piers Lauder (original) | 
					
						
							|  |  |  | #          Guido van Rossum (Windows support and cleanup) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-10-18 22:25:22 +00:00
										 |  |  | import sys | 
					
						
							| 
									
										
										
										
											1998-04-09 20:37:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-01-20 23:34:12 +00:00
										 |  |  | __all__ = ["getpass","getuser"] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-10-18 22:25:22 +00:00
										 |  |  | def unix_getpass(prompt='Password: '): | 
					
						
							| 
									
										
										
										
											2001-01-14 23:47:14 +00:00
										 |  |  |     """Prompt for a password, with echo turned off.
 | 
					
						
							| 
									
										
										
										
											1998-04-09 20:37:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-01-14 23:47:14 +00:00
										 |  |  |     Restore terminal settings at end. | 
					
						
							|  |  |  |     """
 | 
					
						
							| 
									
										
										
										
											1998-04-09 20:37:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-01-14 23:47:14 +00:00
										 |  |  |     try: | 
					
						
							|  |  |  |         fd = sys.stdin.fileno() | 
					
						
							|  |  |  |     except: | 
					
						
							|  |  |  |         return default_getpass(prompt) | 
					
						
							| 
									
										
										
										
											1998-04-09 20:37:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-01-14 23:47:14 +00:00
										 |  |  |     old = termios.tcgetattr(fd)     # a copy to save | 
					
						
							|  |  |  |     new = old[:] | 
					
						
							| 
									
										
										
										
											1998-04-09 20:37:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-02-27 21:23:31 +00:00
										 |  |  |     new[3] = new[3] & ~termios.ECHO # 3 == 'lflags' | 
					
						
							| 
									
										
										
										
											2001-01-14 23:47:14 +00:00
										 |  |  |     try: | 
					
						
							| 
									
										
										
										
											2001-02-27 21:23:31 +00:00
										 |  |  |         termios.tcsetattr(fd, termios.TCSADRAIN, new) | 
					
						
							| 
									
										
										
										
											2001-01-14 23:47:14 +00:00
										 |  |  |         passwd = _raw_input(prompt) | 
					
						
							|  |  |  |     finally: | 
					
						
							| 
									
										
										
										
											2001-02-27 21:23:31 +00:00
										 |  |  |         termios.tcsetattr(fd, termios.TCSADRAIN, old) | 
					
						
							| 
									
										
										
										
											1998-04-09 20:37:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-01-14 23:47:14 +00:00
										 |  |  |     sys.stdout.write('\n') | 
					
						
							|  |  |  |     return passwd | 
					
						
							| 
									
										
										
										
											1998-04-09 20:37:16 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def win_getpass(prompt='Password: '): | 
					
						
							| 
									
										
										
										
											2001-01-14 23:47:14 +00:00
										 |  |  |     """Prompt for password with echo off, using Windows getch().""" | 
					
						
							| 
									
										
										
										
											2001-08-30 15:07:44 +00:00
										 |  |  |     if sys.stdin is not sys.__stdin__: | 
					
						
							|  |  |  |         return default_getpass(prompt) | 
					
						
							| 
									
										
										
										
											2001-01-14 23:47:14 +00:00
										 |  |  |     import msvcrt | 
					
						
							|  |  |  |     for c in prompt: | 
					
						
							|  |  |  |         msvcrt.putch(c) | 
					
						
							|  |  |  |     pw = "" | 
					
						
							|  |  |  |     while 1: | 
					
						
							|  |  |  |         c = msvcrt.getch() | 
					
						
							|  |  |  |         if c == '\r' or c == '\n': | 
					
						
							|  |  |  |             break | 
					
						
							|  |  |  |         if c == '\003': | 
					
						
							|  |  |  |             raise KeyboardInterrupt | 
					
						
							|  |  |  |         if c == '\b': | 
					
						
							|  |  |  |             pw = pw[:-1] | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             pw = pw + c | 
					
						
							|  |  |  |     msvcrt.putch('\r') | 
					
						
							|  |  |  |     msvcrt.putch('\n') | 
					
						
							|  |  |  |     return pw | 
					
						
							| 
									
										
										
										
											1998-04-09 20:37:16 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-04-13 20:22:21 +00:00
										 |  |  | def default_getpass(prompt='Password: '): | 
					
						
							| 
									
										
										
										
											2001-01-14 23:47:14 +00:00
										 |  |  |     print "Warning: Problem with getpass. Passwords may be echoed." | 
					
						
							|  |  |  |     return _raw_input(prompt) | 
					
						
							| 
									
										
										
										
											1998-07-28 19:28:43 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def _raw_input(prompt=""): | 
					
						
							| 
									
										
										
										
											2001-01-14 23:47:14 +00:00
										 |  |  |     # A raw_input() replacement that doesn't save the string in the | 
					
						
							|  |  |  |     # GNU readline history. | 
					
						
							|  |  |  |     prompt = str(prompt) | 
					
						
							|  |  |  |     if prompt: | 
					
						
							|  |  |  |         sys.stdout.write(prompt) | 
					
						
							|  |  |  |     line = sys.stdin.readline() | 
					
						
							|  |  |  |     if not line: | 
					
						
							|  |  |  |         raise EOFError | 
					
						
							|  |  |  |     if line[-1] == '\n': | 
					
						
							|  |  |  |         line = line[:-1] | 
					
						
							|  |  |  |     return line | 
					
						
							| 
									
										
										
										
											1998-04-13 20:22:21 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-04-09 20:37:16 +00:00
										 |  |  | def getuser(): | 
					
						
							| 
									
										
										
										
											2001-01-14 23:47:14 +00:00
										 |  |  |     """Get the username from the environment or password database.
 | 
					
						
							| 
									
										
										
										
											1998-04-09 20:37:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-01-14 23:47:14 +00:00
										 |  |  |     First try various environment variables, then the password | 
					
						
							|  |  |  |     database.  This works on Windows as long as USERNAME is set. | 
					
						
							| 
									
										
										
										
											1998-04-09 20:37:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-01-14 23:47:14 +00:00
										 |  |  |     """
 | 
					
						
							| 
									
										
										
										
											1998-04-09 20:37:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-01-14 23:47:14 +00:00
										 |  |  |     import os | 
					
						
							| 
									
										
										
										
											1998-04-09 20:37:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-01-14 23:47:14 +00:00
										 |  |  |     for name in ('LOGNAME', 'USER', 'LNAME', 'USERNAME'): | 
					
						
							|  |  |  |         user = os.environ.get(name) | 
					
						
							|  |  |  |         if user: | 
					
						
							|  |  |  |             return user | 
					
						
							| 
									
										
										
										
											1998-04-09 20:37:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-01-14 23:47:14 +00:00
										 |  |  |     # If this fails, the exception will "explain" why | 
					
						
							|  |  |  |     import pwd | 
					
						
							|  |  |  |     return pwd.getpwuid(os.getuid())[0] | 
					
						
							| 
									
										
										
										
											1999-10-18 22:25:22 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | # Bind the name getpass to the appropriate function | 
					
						
							|  |  |  | try: | 
					
						
							| 
									
										
										
										
											2001-02-27 21:23:31 +00:00
										 |  |  |     import termios | 
					
						
							| 
									
										
										
										
											1999-10-18 22:25:22 +00:00
										 |  |  | except ImportError: | 
					
						
							| 
									
										
										
										
											2001-01-14 23:47:14 +00:00
										 |  |  |     try: | 
					
						
							|  |  |  |         import msvcrt | 
					
						
							|  |  |  |     except ImportError: | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             from EasyDialogs import AskPassword | 
					
						
							|  |  |  |         except ImportError: | 
					
						
							|  |  |  |             getpass = default_getpass | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             getpass = AskPassword | 
					
						
							|  |  |  |     else: | 
					
						
							|  |  |  |         getpass = win_getpass | 
					
						
							| 
									
										
										
										
											1999-10-18 22:25:22 +00:00
										 |  |  | else: | 
					
						
							| 
									
										
										
										
											2001-01-14 23:47:14 +00:00
										 |  |  |     getpass = unix_getpass |