open-vault/vendor/golang.org/x/crypto/ssh/terminal/util.go

117 lines
3.4 KiB
Go
Raw Normal View History

2015-04-27 23:19:51 +00: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)
2016-06-01 14:24:48 +00:00
package terminal // import "golang.org/x/crypto/ssh/terminal"
2015-04-27 23:19:51 +00:00
import (
2017-09-05 22:06:47 +00:00
"golang.org/x/sys/unix"
2015-04-27 23:19:51 +00:00
)
// State contains the state of a terminal.
type State struct {
2017-11-07 16:57:05 +00:00
termios unix.Termios
2015-04-27 23:19:51 +00:00
}
// IsTerminal returns true if the given file descriptor is a terminal.
func IsTerminal(fd int) bool {
2017-11-07 16:57:05 +00:00
_, err := unix.IoctlGetTermios(fd, ioctlReadTermios)
return err == nil
2015-04-27 23:19:51 +00: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-07 16:57:05 +00:00
termios, err := unix.IoctlGetTermios(fd, ioctlReadTermios)
if err != nil {
2015-04-27 23:19:51 +00:00
return nil, err
}
2017-11-07 16:57:05 +00:00
oldState := State{termios: *termios}
2016-07-23 00:11:47 +00:00
// This attempts to replicate the behaviour documented for cfmakeraw in
// the termios(3) manpage.
2017-11-07 16:57:05 +00: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 {
2015-04-27 23:19:51 +00: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-07 16:57:05 +00:00
termios, err := unix.IoctlGetTermios(fd, ioctlReadTermios)
if err != nil {
2015-04-27 23:19:51 +00:00
return nil, err
}
2017-11-07 16:57:05 +00:00
return &State{termios: *termios}, nil
2015-04-27 23:19:51 +00:00
}
// Restore restores the terminal connected to the given file descriptor to a
// previous state.
func Restore(fd int, state *State) error {
2017-11-07 16:57:05 +00:00
return unix.IoctlSetTermios(fd, ioctlWriteTermios, &state.termios)
2015-04-27 23:19:51 +00:00
}
// GetSize returns the dimensions of the given terminal.
func GetSize(fd int) (width, height int, err error) {
2017-11-07 16:57:05 +00:00
ws, err := unix.IoctlGetWinsize(fd, unix.TIOCGWINSZ)
if err != nil {
2015-04-27 23:19:51 +00:00
return -1, -1, err
}
2017-11-07 16:57:05 +00:00
return int(ws.Col), int(ws.Row), nil
2015-04-27 23:19:51 +00:00
}
2017-01-27 01:16:19 +00: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-07 16:57:05 +00:00
return unix.Read(int(r), buf)
2017-01-27 01:16:19 +00:00
}
2015-04-27 23:19:51 +00: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-07 16:57:05 +00:00
termios, err := unix.IoctlGetTermios(fd, ioctlReadTermios)
if err != nil {
2015-04-27 23:19:51 +00:00
return nil, err
}
2017-11-07 16:57:05 +00: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 {
2015-04-27 23:19:51 +00:00
return nil, err
}
defer func() {
2017-11-07 16:57:05 +00:00
unix.IoctlSetTermios(fd, ioctlWriteTermios, termios)
2015-04-27 23:19:51 +00:00
}()
2017-01-27 01:16:19 +00:00
return readPasswordLine(passwordReader(fd))
2015-04-27 23:19:51 +00:00
}