| 
									
										
										
										
											2017-04-11 02:25:53 +03:00
										 |  |  | // Copyright 2011 The Go Authors. All rights reserved. | 
					
						
							|  |  |  | // Use of this source code is governed by a BSD-style | 
					
						
							|  |  |  | // license that can be found in the LICENSE file. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // +build darwin dragonfly freebsd linux,!appengine netbsd openbsd | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Package terminal provides support functions for dealing with terminals, as | 
					
						
							|  |  |  | // commonly found on UNIX systems. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // Putting a terminal into raw mode is the most common requirement: | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // 	oldState, err := terminal.MakeRaw(0) | 
					
						
							|  |  |  | // 	if err != nil { | 
					
						
							|  |  |  | // 	        panic(err) | 
					
						
							|  |  |  | // 	} | 
					
						
							|  |  |  | // 	defer terminal.Restore(0, oldState) | 
					
						
							|  |  |  | package terminal // import "golang.org/x/crypto/ssh/terminal" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2017-11-12 12:24:42 -08:00
										 |  |  | 	"golang.org/x/sys/unix" | 
					
						
							| 
									
										
										
										
											2017-04-11 02:25:53 +03:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // State contains the state of a terminal. | 
					
						
							|  |  |  | type State struct { | 
					
						
							| 
									
										
										
										
											2017-11-12 12:24:42 -08:00
										 |  |  | 	termios unix.Termios | 
					
						
							| 
									
										
										
										
											2017-04-11 02:25:53 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // IsTerminal returns true if the given file descriptor is a terminal. | 
					
						
							|  |  |  | func IsTerminal(fd int) bool { | 
					
						
							| 
									
										
										
										
											2017-11-12 12:24:42 -08:00
										 |  |  | 	_, err := unix.IoctlGetTermios(fd, ioctlReadTermios) | 
					
						
							|  |  |  | 	return err == nil | 
					
						
							| 
									
										
										
										
											2017-04-11 02:25:53 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // MakeRaw put the terminal connected to the given file descriptor into raw | 
					
						
							|  |  |  | // mode and returns the previous state of the terminal so that it can be | 
					
						
							|  |  |  | // restored. | 
					
						
							|  |  |  | func MakeRaw(fd int) (*State, error) { | 
					
						
							| 
									
										
										
										
											2017-11-12 12:24:42 -08:00
										 |  |  | 	termios, err := unix.IoctlGetTermios(fd, ioctlReadTermios) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2017-04-11 02:25:53 +03:00
										 |  |  | 		return nil, err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-12 12:24:42 -08:00
										 |  |  | 	oldState := State{termios: *termios} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-11 02:25:53 +03:00
										 |  |  | 	// This attempts to replicate the behaviour documented for cfmakeraw in | 
					
						
							|  |  |  | 	// the termios(3) manpage. | 
					
						
							| 
									
										
										
										
											2017-11-12 12:24:42 -08:00
										 |  |  | 	termios.Iflag &^= unix.IGNBRK | unix.BRKINT | unix.PARMRK | unix.ISTRIP | unix.INLCR | unix.IGNCR | unix.ICRNL | unix.IXON | 
					
						
							|  |  |  | 	termios.Oflag &^= unix.OPOST | 
					
						
							|  |  |  | 	termios.Lflag &^= unix.ECHO | unix.ECHONL | unix.ICANON | unix.ISIG | unix.IEXTEN | 
					
						
							|  |  |  | 	termios.Cflag &^= unix.CSIZE | unix.PARENB | 
					
						
							|  |  |  | 	termios.Cflag |= unix.CS8 | 
					
						
							|  |  |  | 	termios.Cc[unix.VMIN] = 1 | 
					
						
							|  |  |  | 	termios.Cc[unix.VTIME] = 0 | 
					
						
							|  |  |  | 	if err := unix.IoctlSetTermios(fd, ioctlWriteTermios, termios); err != nil { | 
					
						
							| 
									
										
										
										
											2017-04-11 02:25:53 +03:00
										 |  |  | 		return nil, err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return &oldState, nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // GetState returns the current state of a terminal which may be useful to | 
					
						
							|  |  |  | // restore the terminal after a signal. | 
					
						
							|  |  |  | func GetState(fd int) (*State, error) { | 
					
						
							| 
									
										
										
										
											2017-11-12 12:24:42 -08:00
										 |  |  | 	termios, err := unix.IoctlGetTermios(fd, ioctlReadTermios) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2017-04-11 02:25:53 +03:00
										 |  |  | 		return nil, err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-12 12:24:42 -08:00
										 |  |  | 	return &State{termios: *termios}, nil | 
					
						
							| 
									
										
										
										
											2017-04-11 02:25:53 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Restore restores the terminal connected to the given file descriptor to a | 
					
						
							|  |  |  | // previous state. | 
					
						
							|  |  |  | func Restore(fd int, state *State) error { | 
					
						
							| 
									
										
										
										
											2017-11-12 12:24:42 -08:00
										 |  |  | 	return unix.IoctlSetTermios(fd, ioctlWriteTermios, &state.termios) | 
					
						
							| 
									
										
										
										
											2017-04-11 02:25:53 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // GetSize returns the dimensions of the given terminal. | 
					
						
							|  |  |  | func GetSize(fd int) (width, height int, err error) { | 
					
						
							| 
									
										
										
										
											2017-11-12 12:24:42 -08:00
										 |  |  | 	ws, err := unix.IoctlGetWinsize(fd, unix.TIOCGWINSZ) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2017-04-11 02:25:53 +03:00
										 |  |  | 		return -1, -1, err | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-11-12 12:24:42 -08:00
										 |  |  | 	return int(ws.Col), int(ws.Row), nil | 
					
						
							| 
									
										
										
										
											2017-04-11 02:25:53 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // passwordReader is an io.Reader that reads from a specific file descriptor. | 
					
						
							|  |  |  | type passwordReader int | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (r passwordReader) Read(buf []byte) (int, error) { | 
					
						
							| 
									
										
										
										
											2017-11-12 12:24:42 -08:00
										 |  |  | 	return unix.Read(int(r), buf) | 
					
						
							| 
									
										
										
										
											2017-04-11 02:25:53 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // ReadPassword reads a line of input from a terminal without local echo.  This | 
					
						
							|  |  |  | // is commonly used for inputting passwords and other sensitive data. The slice | 
					
						
							|  |  |  | // returned does not include the \n. | 
					
						
							|  |  |  | func ReadPassword(fd int) ([]byte, error) { | 
					
						
							| 
									
										
										
										
											2017-11-12 12:24:42 -08:00
										 |  |  | 	termios, err := unix.IoctlGetTermios(fd, ioctlReadTermios) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2017-04-11 02:25:53 +03:00
										 |  |  | 		return nil, err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-12 12:24:42 -08:00
										 |  |  | 	newState := *termios | 
					
						
							|  |  |  | 	newState.Lflag &^= unix.ECHO | 
					
						
							|  |  |  | 	newState.Lflag |= unix.ICANON | unix.ISIG | 
					
						
							|  |  |  | 	newState.Iflag |= unix.ICRNL | 
					
						
							|  |  |  | 	if err := unix.IoctlSetTermios(fd, ioctlWriteTermios, &newState); err != nil { | 
					
						
							| 
									
										
										
										
											2017-04-11 02:25:53 +03:00
										 |  |  | 		return nil, err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	defer func() { | 
					
						
							| 
									
										
										
										
											2017-11-12 12:24:42 -08:00
										 |  |  | 		unix.IoctlSetTermios(fd, ioctlWriteTermios, termios) | 
					
						
							| 
									
										
										
										
											2017-04-11 02:25:53 +03:00
										 |  |  | 	}() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return readPasswordLine(passwordReader(fd)) | 
					
						
							|  |  |  | } |