|
| 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 | +iferr!=nil { |
| 29 | +s.logger.Warn(ctx,"failed to get hostname",slog.Error(err)) |
| 30 | +returnfalse |
| 31 | +} |
| 32 | + |
| 33 | +err=s.fs.MkdirAll(s.x11SocketDir,0o700) |
| 34 | +iferr!=nil { |
| 35 | +s.logger.Warn(ctx,"failed to make the x11 socket dir",slog.F("dir",s.x11SocketDir),slog.Error(err)) |
| 36 | +returnfalse |
| 37 | +} |
| 38 | + |
| 39 | +err=addXauthEntry(ctx,s.fs,hostname,strconv.Itoa(int(x11.ScreenNumber)),x11.AuthProtocol,x11.AuthCookie) |
| 40 | +iferr!=nil { |
| 41 | +s.logger.Warn(ctx,"failed to add Xauthority entry",slog.Error(err)) |
| 42 | +returnfalse |
| 43 | +} |
| 44 | +returntrue |
| 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 | +returnfalse |
| 54 | +} |
| 55 | +listener,err:=net.Listen("unix",filepath.Join(s.x11SocketDir,fmt.Sprintf("X%d",x11.ScreenNumber))) |
| 56 | +iferr!=nil { |
| 57 | +s.logger.Warn(ctx,"failed to listen for X11",slog.Error(err)) |
| 58 | +returnfalse |
| 59 | +} |
| 60 | +s.trackListener(listener,true) |
| 61 | + |
| 62 | +gofunc() { |
| 63 | +deferlistener.Close() |
| 64 | +defers.trackListener(listener,false) |
| 65 | +handledFirstConnection:=false |
| 66 | + |
| 67 | +for { |
| 68 | +conn,err:=listener.Accept() |
| 69 | +iferr!=nil { |
| 70 | +iferrors.Is(err,net.ErrClosed) { |
| 71 | +return |
| 72 | +} |
| 73 | +s.logger.Warn(ctx,"failed to accept X11 connection",slog.Error(err)) |
| 74 | +return |
| 75 | +} |
| 76 | +ifx11.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 | +OriginatorAddressstring |
| 96 | +OriginatorPortuint32 |
| 97 | +}{ |
| 98 | +OriginatorAddress:unixAddr.Name, |
| 99 | +OriginatorPort:0, |
| 100 | +})) |
| 101 | +iferr!=nil { |
| 102 | +s.logger.Warn(ctx,"failed to open X11 channel",slog.Error(err)) |
| 103 | +return |
| 104 | +} |
| 105 | +gogossh.DiscardRequests(reqs) |
| 106 | +goBicopy(ctx,conn,channel) |
| 107 | +} |
| 108 | +}() |
| 109 | +returntrue |
| 110 | +} |
| 111 | + |
| 112 | +// addXauthEntry adds an Xauthority entry to the Xauthority file. |
| 113 | +// The Xauthority file is located at ~/.Xauthority. |
| 114 | +funcaddXauthEntry(ctx context.Context,fs afero.Fs,hoststring,displaystring,authProtocolstring,authCookiestring)error { |
| 115 | +// Get the Xauthority file path |
| 116 | +homeDir,err:=os.UserHomeDir() |
| 117 | +iferr!=nil { |
| 118 | +returnxerrors.Errorf("failed to get user home directory: %w",err) |
| 119 | +} |
| 120 | + |
| 121 | +xauthPath:=filepath.Join(homeDir,".Xauthority") |
| 122 | + |
| 123 | +lock:=flock.New(xauthPath) |
| 124 | +deferlock.Close() |
| 125 | +ok,err:=lock.TryLockContext(ctx,100*time.Millisecond) |
| 126 | +if!ok { |
| 127 | +returnxerrors.Errorf("failed to lock Xauthority file: %w",err) |
| 128 | +} |
| 129 | +iferr!=nil { |
| 130 | +returnxerrors.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 | +iferr!=nil { |
| 136 | +returnxerrors.Errorf("failed to open Xauthority file: %w",err) |
| 137 | +} |
| 138 | +deferfile.Close() |
| 139 | + |
| 140 | +// Convert the authCookie from hex string to byte slice |
| 141 | +authCookieBytes,err:=hex.DecodeString(authCookie) |
| 142 | +iferr!=nil { |
| 143 | +returnxerrors.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 | +iferr!=nil { |
| 150 | +returnxerrors.Errorf("failed to write family: %w",err) |
| 151 | +} |
| 152 | + |
| 153 | +err=binary.Write(file,binary.BigEndian,uint16(len(host))) |
| 154 | +iferr!=nil { |
| 155 | +returnxerrors.Errorf("failed to write host length: %w",err) |
| 156 | +} |
| 157 | +_,err=file.WriteString(host) |
| 158 | +iferr!=nil { |
| 159 | +returnxerrors.Errorf("failed to write host: %w",err) |
| 160 | +} |
| 161 | + |
| 162 | +err=binary.Write(file,binary.BigEndian,uint16(len(display))) |
| 163 | +iferr!=nil { |
| 164 | +returnxerrors.Errorf("failed to write display length: %w",err) |
| 165 | +} |
| 166 | +_,err=file.WriteString(display) |
| 167 | +iferr!=nil { |
| 168 | +returnxerrors.Errorf("failed to write display: %w",err) |
| 169 | +} |
| 170 | + |
| 171 | +err=binary.Write(file,binary.BigEndian,uint16(len(authProtocol))) |
| 172 | +iferr!=nil { |
| 173 | +returnxerrors.Errorf("failed to write auth protocol length: %w",err) |
| 174 | +} |
| 175 | +_,err=file.WriteString(authProtocol) |
| 176 | +iferr!=nil { |
| 177 | +returnxerrors.Errorf("failed to write auth protocol: %w",err) |
| 178 | +} |
| 179 | + |
| 180 | +err=binary.Write(file,binary.BigEndian,uint16(len(authCookieBytes))) |
| 181 | +iferr!=nil { |
| 182 | +returnxerrors.Errorf("failed to write auth cookie length: %w",err) |
| 183 | +} |
| 184 | +_,err=file.Write(authCookieBytes) |
| 185 | +iferr!=nil { |
| 186 | +returnxerrors.Errorf("failed to write auth cookie: %w",err) |
| 187 | +} |
| 188 | + |
| 189 | +returnnil |
| 190 | +} |