|
| 1 | +//go:build darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris |
| 2 | +// +build darwin dragonfly freebsd linux netbsd openbsd solaris |
| 3 | + |
| 4 | +package ssh |
| 5 | + |
| 6 | +import ( |
| 7 | + "fmt" |
| 8 | + "os" |
| 9 | + |
| 10 | + "github.com/creack/pty" |
| 11 | + "github.com/u-root/u-root/pkg/termios" |
| 12 | + "golang.org/x/crypto/ssh" |
| 13 | + "golang.org/x/sys/unix" |
| 14 | +) |
| 15 | + |
| 16 | +type impl struct { |
| 17 | + // Master is the master PTY file descriptor. |
| 18 | + Master *os.File |
| 19 | + |
| 20 | + // Slave is the slave PTY file descriptor. |
| 21 | + Slave *os.File |
| 22 | + |
| 23 | + // Name is the name of the slave PTY. |
| 24 | + Name string |
| 25 | +} |
| 26 | + |
| 27 | +// Read implements ptyInterface. |
| 28 | +func (i *impl) Read(p []byte) (n int, err error) { |
| 29 | + return i.Master.Read(p) |
| 30 | +} |
| 31 | + |
| 32 | +// Write implements ptyInterface. |
| 33 | +func (i *impl) Write(p []byte) (n int, err error) { |
| 34 | + return i.Master.Write(p) |
| 35 | +} |
| 36 | + |
| 37 | +func (i *impl) Close() error { |
| 38 | + if err := i.Master.Close(); err != nil { |
| 39 | + return err |
| 40 | + } |
| 41 | + return i.Slave.Close() |
| 42 | +} |
| 43 | + |
| 44 | +func (i *impl) Resize(w int, h int) (rErr error) { |
| 45 | + conn, err := i.Master.SyscallConn() |
| 46 | + if err != nil { |
| 47 | + return err |
| 48 | + } |
| 49 | + |
| 50 | + return conn.Control(func(fd uintptr) { |
| 51 | + rErr = termios.SetWinSize(fd, &termios.Winsize{ |
| 52 | + Winsize: unix.Winsize{ |
| 53 | + Row: uint16(h), |
| 54 | + Col: uint16(w), |
| 55 | + }, |
| 56 | + }) |
| 57 | + }) |
| 58 | +} |
| 59 | + |
| 60 | +func newPty(_ Context, _ string, win Window, modes ssh.TerminalModes) (_ impl, rErr error) { |
| 61 | + ptm, pts, err := pty.Open() |
| 62 | + if err != nil { |
| 63 | + return impl{}, err |
| 64 | + } |
| 65 | + |
| 66 | + conn, err := ptm.SyscallConn() |
| 67 | + if err != nil { |
| 68 | + return impl{}, err |
| 69 | + } |
| 70 | + |
| 71 | + if err := conn.Control(func(fd uintptr) { |
| 72 | + rErr = applyTerminalModesToFd(fd, win.Width, win.Height, modes) |
| 73 | + }); err != nil { |
| 74 | + return impl{}, err |
| 75 | + } |
| 76 | + |
| 77 | + return impl{Master: ptm, Slave: pts, Name: pts.Name()}, rErr |
| 78 | +} |
| 79 | + |
| 80 | +func applyTerminalModesToFd(fd uintptr, width int, height int, modes ssh.TerminalModes) error { |
| 81 | + // Get the current TTY configuration. |
| 82 | + tios, err := termios.GTTY(int(fd)) |
| 83 | + if err != nil { |
| 84 | + return fmt.Errorf("GTTY: %w", err) |
| 85 | + } |
| 86 | + |
| 87 | + // Apply the modes from the SSH request. |
| 88 | + tios.Row = height |
| 89 | + tios.Col = width |
| 90 | + |
| 91 | + for c, v := range modes { |
| 92 | + if c == ssh.TTY_OP_ISPEED { |
| 93 | + tios.Ispeed = int(v) |
| 94 | + continue |
| 95 | + } |
| 96 | + if c == ssh.TTY_OP_OSPEED { |
| 97 | + tios.Ospeed = int(v) |
| 98 | + continue |
| 99 | + } |
| 100 | + k, ok := terminalModeFlagNames[c] |
| 101 | + if !ok { |
| 102 | + continue |
| 103 | + } |
| 104 | + if _, ok := tios.CC[k]; ok { |
| 105 | + tios.CC[k] = uint8(v) |
| 106 | + continue |
| 107 | + } |
| 108 | + if _, ok := tios.Opts[k]; ok { |
| 109 | + tios.Opts[k] = v > 0 |
| 110 | + continue |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + // Save the new TTY configuration. |
| 115 | + if _, err := tios.STTY(int(fd)); err != nil { |
| 116 | + return fmt.Errorf("STTY: %w", err) |
| 117 | + } |
| 118 | + |
| 119 | + return nil |
| 120 | +} |
| 121 | + |
| 122 | +// terminalModeFlagNames maps the SSH terminal mode flags to mnemonic |
| 123 | +// names used by the termios package. |
| 124 | +var terminalModeFlagNames = map[uint8]string{ |
| 125 | + ssh.VINTR: "intr", |
| 126 | + ssh.VQUIT: "quit", |
| 127 | + ssh.VERASE: "erase", |
| 128 | + ssh.VKILL: "kill", |
| 129 | + ssh.VEOF: "eof", |
| 130 | + ssh.VEOL: "eol", |
| 131 | + ssh.VEOL2: "eol2", |
| 132 | + ssh.VSTART: "start", |
| 133 | + ssh.VSTOP: "stop", |
| 134 | + ssh.VSUSP: "susp", |
| 135 | + ssh.VDSUSP: "dsusp", |
| 136 | + ssh.VREPRINT: "rprnt", |
| 137 | + ssh.VWERASE: "werase", |
| 138 | + ssh.VLNEXT: "lnext", |
| 139 | + ssh.VFLUSH: "flush", |
| 140 | + ssh.VSWTCH: "swtch", |
| 141 | + ssh.VSTATUS: "status", |
| 142 | + ssh.VDISCARD: "discard", |
| 143 | + ssh.IGNPAR: "ignpar", |
| 144 | + ssh.PARMRK: "parmrk", |
| 145 | + ssh.INPCK: "inpck", |
| 146 | + ssh.ISTRIP: "istrip", |
| 147 | + ssh.INLCR: "inlcr", |
| 148 | + ssh.IGNCR: "igncr", |
| 149 | + ssh.ICRNL: "icrnl", |
| 150 | + ssh.IUCLC: "iuclc", |
| 151 | + ssh.IXON: "ixon", |
| 152 | + ssh.IXANY: "ixany", |
| 153 | + ssh.IXOFF: "ixoff", |
| 154 | + ssh.IMAXBEL: "imaxbel", |
| 155 | + ssh.IUTF8: "iutf8", |
| 156 | + ssh.ISIG: "isig", |
| 157 | + ssh.ICANON: "icanon", |
| 158 | + ssh.XCASE: "xcase", |
| 159 | + ssh.ECHO: "echo", |
| 160 | + ssh.ECHOE: "echoe", |
| 161 | + ssh.ECHOK: "echok", |
| 162 | + ssh.ECHONL: "echonl", |
| 163 | + ssh.NOFLSH: "noflsh", |
| 164 | + ssh.TOSTOP: "tostop", |
| 165 | + ssh.IEXTEN: "iexten", |
| 166 | + ssh.ECHOCTL: "echoctl", |
| 167 | + ssh.ECHOKE: "echoke", |
| 168 | + ssh.PENDIN: "pendin", |
| 169 | + ssh.OPOST: "opost", |
| 170 | + ssh.OLCUC: "olcuc", |
| 171 | + ssh.ONLCR: "onlcr", |
| 172 | + ssh.OCRNL: "ocrnl", |
| 173 | + ssh.ONOCR: "onocr", |
| 174 | + ssh.ONLRET: "onlret", |
| 175 | + ssh.CS7: "cs7", |
| 176 | + ssh.CS8: "cs8", |
| 177 | + ssh.PARENB: "parenb", |
| 178 | + ssh.PARODD: "parodd", |
| 179 | + ssh.TTY_OP_ISPEED: "tty_op_ispeed", |
| 180 | + ssh.TTY_OP_OSPEED: "tty_op_ospeed", |
| 181 | +} |
0 commit comments