termmode_linux.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // +build linux
  2. package xs
  3. import (
  4. "errors"
  5. "io"
  6. unix "golang.org/x/sys/unix"
  7. )
  8. /* -------------
  9. * minimal terminal APIs brought in from ssh/terminal
  10. * (they have no real business being there as they aren't specific to
  11. * ssh, but as of Go v1.10, late 2019, core go stdlib hasn't yet done
  12. * the planned terminal lib reorgs.)
  13. * ------------- */
  14. // From github.com/golang/crypto/blob/master/ssh/terminal/util_linux.go
  15. const ioctlReadTermios = unix.TCGETS
  16. const ioctlWriteTermios = unix.TCSETS
  17. // From github.com/golang/crypto/blob/master/ssh/terminal/util.go
  18. // State contains the state of a terminal.
  19. type State struct {
  20. termios unix.Termios
  21. }
  22. // MakeRaw put the terminal connected to the given file descriptor into raw
  23. // mode and returns the previous state of the terminal so that it can be
  24. // restored.
  25. func MakeRaw(fd uintptr) (*State, error) {
  26. termios, err := unix.IoctlGetTermios(int(fd), ioctlReadTermios)
  27. if err != nil {
  28. return nil, err
  29. }
  30. oldState := State{termios: *termios}
  31. // This attempts to replicate the behaviour documented for cfmakeraw in
  32. // the termios(3) manpage.
  33. termios.Iflag &^= unix.IGNBRK | unix.BRKINT | unix.PARMRK | unix.ISTRIP | unix.INLCR | unix.IGNCR | unix.ICRNL | unix.IXON
  34. termios.Oflag &^= unix.OPOST
  35. termios.Lflag &^= unix.ECHO | unix.ECHONL | unix.ICANON | unix.ISIG | unix.IEXTEN
  36. termios.Cflag &^= unix.CSIZE | unix.PARENB
  37. termios.Cflag |= unix.CS8
  38. termios.Cc[unix.VMIN] = 1
  39. termios.Cc[unix.VTIME] = 0
  40. if err := unix.IoctlSetTermios(int(fd), ioctlWriteTermios, termios); err != nil {
  41. return nil, err
  42. }
  43. return &oldState, nil
  44. }
  45. // GetState returns the current state of a terminal which may be useful to
  46. // restore the terminal after a signal.
  47. func GetState(fd uintptr) (*State, error) {
  48. termios, err := unix.IoctlGetTermios(int(fd), ioctlReadTermios)
  49. if err != nil {
  50. return nil, err
  51. }
  52. return &State{termios: *termios}, nil
  53. }
  54. // Restore restores the terminal connected to the given file descriptor to a
  55. // previous state.
  56. func Restore(fd uintptr, state *State) error {
  57. if state != nil {
  58. return unix.IoctlSetTermios(int(fd), ioctlWriteTermios, &state.termios)
  59. } else {
  60. return errors.New("nil State")
  61. }
  62. }
  63. // ReadPassword reads a line of input from a terminal without local echo. This
  64. // is commonly used for inputting passwords and other sensitive data. The slice
  65. // returned does not include the \n.
  66. func ReadPassword(fd uintptr) ([]byte, error) {
  67. termios, err := unix.IoctlGetTermios(int(fd), ioctlReadTermios)
  68. if err != nil {
  69. return nil, err
  70. }
  71. newState := *termios
  72. newState.Lflag &^= unix.ECHO
  73. newState.Lflag |= unix.ICANON | unix.ISIG
  74. newState.Iflag |= unix.ICRNL
  75. if err := unix.IoctlSetTermios(int(fd), ioctlWriteTermios, &newState); err != nil {
  76. return nil, err
  77. }
  78. defer func() {
  79. _ = unix.IoctlSetTermios(int(fd), ioctlWriteTermios, termios) // nolint: gosec
  80. }()
  81. return readPasswordLine(passwordReader(fd))
  82. }
  83. // passwordReader is an io.Reader that reads from a specific file descriptor.
  84. type passwordReader int
  85. func (r passwordReader) Read(buf []byte) (int, error) {
  86. return unix.Read(int(r), buf)
  87. }
  88. // readPasswordLine reads from reader until it finds \n or io.EOF.
  89. // The slice returned does not include the \n.
  90. // readPasswordLine also ignores any \r it finds.
  91. func readPasswordLine(reader io.Reader) ([]byte, error) {
  92. var buf [1]byte
  93. var ret []byte
  94. for {
  95. n, err := reader.Read(buf[:])
  96. if n > 0 {
  97. switch buf[0] {
  98. case '\n':
  99. return ret, nil
  100. case '\r':
  101. // remove \r from passwords on Windows
  102. default:
  103. ret = append(ret, buf[0])
  104. }
  105. continue
  106. }
  107. if err != nil {
  108. if err == io.EOF && len(ret) > 0 {
  109. return ret, nil
  110. }
  111. return ret, err
  112. }
  113. }
  114. }