|
| 1 | +package agentssh |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/binary" |
| 6 | + "encoding/hex" |
| 7 | + "errors" |
| 8 | + "fmt" |
| 9 | + "net" |
| 10 | + "os" |
| 11 | + "path/filepath" |
| 12 | + "strconv" |
| 13 | + "time" |
| 14 | + |
| 15 | + "github.com/gliderlabs/ssh" |
| 16 | + "github.com/gofrs/flock" |
| 17 | + "github.com/spf13/afero" |
| 18 | + gossh "golang.org/x/crypto/ssh" |
| 19 | + "golang.org/x/xerrors" |
| 20 | + |
| 21 | + "cdr.dev/slog" |
| 22 | +) |
| 23 | + |
| 24 | +// x11Callback is called when the client requests X11 forwarding. |
| 25 | +// It adds an Xauthority entry to the Xauthority file. |
| 26 | +func (s *Server) x11Callback(ctx ssh.Context, x11 ssh.X11) bool { |
| 27 | + hostname, err := os.Hostname() |
| 28 | + if err != nil { |
| 29 | + s.logger.Warn(ctx, "failed to get hostname", slog.Error(err)) |
| 30 | + return false |
| 31 | + } |
| 32 | + |
| 33 | + err = s.fs.MkdirAll(s.x11SocketDir, 0o700) |
| 34 | + if err != nil { |
| 35 | + s.logger.Warn(ctx, "failed to make the x11 socket dir", slog.F("dir", s.x11SocketDir), slog.Error(err)) |
| 36 | + return false |
| 37 | + } |
| 38 | + |
| 39 | + err = addXauthEntry(ctx, s.fs, hostname, strconv.Itoa(int(x11.ScreenNumber)), x11.AuthProtocol, x11.AuthCookie) |
| 40 | + if err != nil { |
| 41 | + s.logger.Warn(ctx, "failed to add Xauthority entry", slog.Error(err)) |
| 42 | + return false |
| 43 | + } |
| 44 | + return true |
| 45 | +} |
| 46 | + |
| 47 | +// x11Handler is called when a session has requested X11 forwarding. |
| 48 | +// It listens for X11 connections and forwards them to the client. |
| 49 | +func (s *Server) x11Handler(ctx ssh.Context, x11 ssh.X11) bool { |
| 50 | + serverConn, valid := ctx.Value(ssh.ContextKeyConn).(*gossh.ServerConn) |
| 51 | + if !valid { |
| 52 | + s.logger.Warn(ctx, "failed to get server connection") |
| 53 | + return false |
| 54 | + } |
| 55 | + listener, err := net.Listen("unix", filepath.Join(s.x11SocketDir, fmt.Sprintf("X%d", x11.ScreenNumber))) |
| 56 | + if err != nil { |
| 57 | + s.logger.Warn(ctx, "failed to listen for X11", slog.Error(err)) |
| 58 | + return false |
| 59 | + } |
| 60 | + s.trackListener(listener, true) |
| 61 | + |
| 62 | + go func() { |
| 63 | + defer listener.Close() |
| 64 | + defer s.trackListener(listener, false) |
| 65 | + handledFirstConnection := false |
| 66 | + |
| 67 | + for { |
| 68 | + conn, err := listener.Accept() |
| 69 | + if err != nil { |
| 70 | + if errors.Is(err, net.ErrClosed) { |
| 71 | + return |
| 72 | + } |
| 73 | + s.logger.Warn(ctx, "failed to accept X11 connection", slog.Error(err)) |
| 74 | + return |
| 75 | + } |
| 76 | + if x11.SingleConnection && handledFirstConnection { |
| 77 | + s.logger.Warn(ctx, "X11 connection rejected because single connection is enabled") |
| 78 | + _ = conn.Close() |
| 79 | + continue |
| 80 | + } |
| 81 | + handledFirstConnection = true |
| 82 | + |
| 83 | + unixConn, ok := conn.(*net.UnixConn) |
| 84 | + if !ok { |
| 85 | + s.logger.Warn(ctx, fmt.Sprintf("failed to cast connection to UnixConn. got: %T", conn)) |
| 86 | + return |
| 87 | + } |
| 88 | + unixAddr, ok := unixConn.LocalAddr().(*net.UnixAddr) |
| 89 | + if !ok { |
| 90 | + s.logger.Warn(ctx, fmt.Sprintf("failed to cast local address to UnixAddr. got: %T", unixConn.LocalAddr())) |
| 91 | + return |
| 92 | + } |
| 93 | + |
| 94 | + channel, reqs, err := serverConn.OpenChannel("x11", gossh.Marshal(struct { |
| 95 | + OriginatorAddress string |
| 96 | + OriginatorPort uint32 |
| 97 | + }{ |
| 98 | + OriginatorAddress: unixAddr.Name, |
| 99 | + OriginatorPort: 0, |
| 100 | + })) |
| 101 | + if err != nil { |
| 102 | + s.logger.Warn(ctx, "failed to open X11 channel", slog.Error(err)) |
| 103 | + return |
| 104 | + } |
| 105 | + go gossh.DiscardRequests(reqs) |
| 106 | + go Bicopy(ctx, conn, channel) |
| 107 | + } |
| 108 | + }() |
| 109 | + return true |
| 110 | +} |
| 111 | + |
| 112 | +// addXauthEntry adds an Xauthority entry to the Xauthority file. |
| 113 | +// The Xauthority file is located at ~/.Xauthority. |
| 114 | +func addXauthEntry(ctx context.Context, fs afero.Fs, host string, display string, authProtocol string, authCookie string) error { |
| 115 | + // Get the Xauthority file path |
| 116 | + homeDir, err := os.UserHomeDir() |
| 117 | + if err != nil { |
| 118 | + return xerrors.Errorf("failed to get user home directory: %w", err) |
| 119 | + } |
| 120 | + |
| 121 | + xauthPath := filepath.Join(homeDir, ".Xauthority") |
| 122 | + |
| 123 | + lock := flock.New(xauthPath) |
| 124 | + defer lock.Close() |
| 125 | + ok, err := lock.TryLockContext(ctx, 100*time.Millisecond) |
| 126 | + if !ok { |
| 127 | + return xerrors.Errorf("failed to lock Xauthority file: %w", err) |
| 128 | + } |
| 129 | + if err != nil { |
| 130 | + return xerrors.Errorf("failed to lock Xauthority file: %w", err) |
| 131 | + } |
| 132 | + |
| 133 | + // Open or create the Xauthority file |
| 134 | + file, err := fs.OpenFile(xauthPath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0o600) |
| 135 | + if err != nil { |
| 136 | + return xerrors.Errorf("failed to open Xauthority file: %w", err) |
| 137 | + } |
| 138 | + defer file.Close() |
| 139 | + |
| 140 | + // Convert the authCookie from hex string to byte slice |
| 141 | + authCookieBytes, err := hex.DecodeString(authCookie) |
| 142 | + if err != nil { |
| 143 | + return xerrors.Errorf("failed to decode auth cookie: %w", err) |
| 144 | + } |
| 145 | + |
| 146 | + // Write Xauthority entry |
| 147 | + family := uint16(0x0100) // FamilyLocal |
| 148 | + err = binary.Write(file, binary.BigEndian, family) |
| 149 | + if err != nil { |
| 150 | + return xerrors.Errorf("failed to write family: %w", err) |
| 151 | + } |
| 152 | + |
| 153 | + err = binary.Write(file, binary.BigEndian, uint16(len(host))) |
| 154 | + if err != nil { |
| 155 | + return xerrors.Errorf("failed to write host length: %w", err) |
| 156 | + } |
| 157 | + _, err = file.WriteString(host) |
| 158 | + if err != nil { |
| 159 | + return xerrors.Errorf("failed to write host: %w", err) |
| 160 | + } |
| 161 | + |
| 162 | + err = binary.Write(file, binary.BigEndian, uint16(len(display))) |
| 163 | + if err != nil { |
| 164 | + return xerrors.Errorf("failed to write display length: %w", err) |
| 165 | + } |
| 166 | + _, err = file.WriteString(display) |
| 167 | + if err != nil { |
| 168 | + return xerrors.Errorf("failed to write display: %w", err) |
| 169 | + } |
| 170 | + |
| 171 | + err = binary.Write(file, binary.BigEndian, uint16(len(authProtocol))) |
| 172 | + if err != nil { |
| 173 | + return xerrors.Errorf("failed to write auth protocol length: %w", err) |
| 174 | + } |
| 175 | + _, err = file.WriteString(authProtocol) |
| 176 | + if err != nil { |
| 177 | + return xerrors.Errorf("failed to write auth protocol: %w", err) |
| 178 | + } |
| 179 | + |
| 180 | + err = binary.Write(file, binary.BigEndian, uint16(len(authCookieBytes))) |
| 181 | + if err != nil { |
| 182 | + return xerrors.Errorf("failed to write auth cookie length: %w", err) |
| 183 | + } |
| 184 | + _, err = file.Write(authCookieBytes) |
| 185 | + if err != nil { |
| 186 | + return xerrors.Errorf("failed to write auth cookie: %w", err) |
| 187 | + } |
| 188 | + |
| 189 | + return nil |
| 190 | +} |
0 commit comments