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