@@ -37,6 +37,7 @@ type forwardedUnixHandler struct {
3737}
3838
3939func (h * forwardedUnixHandler ) HandleSSHRequest (ctx ssh.Context , _ * ssh.Server , req * gossh.Request ) (bool , []byte ) {
40+ h .log .Debug (ctx , "handling SSH unix forward" )
4041 h .Lock ()
4142 if h .forwards == nil {
4243 h .forwards = make (map [string ]net.Listener )
@@ -47,22 +48,25 @@ func (h *forwardedUnixHandler) HandleSSHRequest(ctx ssh.Context, _ *ssh.Server,
4748 h .log .Warn (ctx , "SSH unix forward request from client with no gossh connection" )
4849 return false , nil
4950 }
51+ log := h .log .With (slog .F ("remote_addr" , conn .RemoteAddr ()))
5052
5153 switch req .Type {
5254 case "streamlocal-forward@openssh.com" :
5355 var reqPayload streamLocalForwardPayload
5456 err := gossh .Unmarshal (req .Payload , & reqPayload )
5557 if err != nil {
56- h .log .Warn (ctx , "parse streamlocal-forward@openssh.com request payload from client" , slog .Error (err ))
58+ h .log .Warn (ctx , "parse streamlocal-forward@openssh.com request (SSH unix forward) payload from client" , slog .Error (err ))
5759 return false , nil
5860 }
5961
6062 addr := reqPayload .SocketPath
63+ log = log .With (slog .F ("socket_path" , addr ))
64+ log .Debug (ctx , "request begin SSH unix forward" )
6165 h .Lock ()
6266 _ , ok := h .forwards [addr ]
6367 h .Unlock ()
6468 if ok {
65- h . log .Warn (ctx , "SSH unix forward request for socket path that is already being forwarded (maybe to another client?)" ,
69+ log .Warn (ctx , "SSH unix forward request for socket path that is already being forwarded (maybe to another client?)" ,
6670 slog .F ("socket_path" , addr ),
6771 )
6872 return false , nil
@@ -72,22 +76,22 @@ func (h *forwardedUnixHandler) HandleSSHRequest(ctx ssh.Context, _ *ssh.Server,
7276 parentDir := filepath .Dir (addr )
7377 err = os .MkdirAll (parentDir , 0o700 )
7478 if err != nil {
75- h . log .Warn (ctx , "create parent dir for SSH unix forward request" ,
79+ log .Warn (ctx , "create parent dir for SSH unix forward request" ,
7680 slog .F ("parent_dir" , parentDir ),
77- slog .F ("socket_path" , addr ),
7881 slog .Error (err ),
7982 )
8083 return false , nil
8184 }
8285
8386 ln , err := net .Listen ("unix" , addr )
8487 if err != nil {
85- h . log .Warn (ctx , "listen on Unix socket for SSH unix forward request" ,
88+ log .Warn (ctx , "listen on Unix socket for SSH unix forward request" ,
8689 slog .F ("socket_path" , addr ),
8790 slog .Error (err ),
8891 )
8992 return false , nil
9093 }
94+ log .Debug (ctx , "SSH unix forward listening on socket" )
9195
9296 // The listener needs to successfully start before it can be added to
9397 // the map, so we don't have to worry about checking for an existing
@@ -97,6 +101,7 @@ func (h *forwardedUnixHandler) HandleSSHRequest(ctx ssh.Context, _ *ssh.Server,
97101 h .Lock ()
98102 h .forwards [addr ] = ln
99103 h .Unlock ()
104+ log .Debug (ctx , "SSH unix forward added to cache" )
100105
101106 ctx , cancel := context .WithCancel (ctx )
102107 go func () {
@@ -110,22 +115,23 @@ func (h *forwardedUnixHandler) HandleSSHRequest(ctx ssh.Context, _ *ssh.Server,
110115 c , err := ln .Accept ()
111116 if err != nil {
112117 if ! xerrors .Is (err , net .ErrClosed ) {
113- h .log .Warn (ctx , "accept on local Unix socket for SSH unix forward request" ,
114- slog .F ("socket_path" , addr ),
118+ log .Warn (ctx , "accept on local Unix socket for SSH unix forward request" ,
115119 slog .Error (err ),
116120 )
117121 }
118122 // closed below
123+ log .Debug (ctx , "SSH unix forward listener closed" )
119124 break
120125 }
126+ log .Debug (ctx , "accepted SSH unix forward connection" )
121127 payload := gossh .Marshal (& forwardedStreamLocalPayload {
122128 SocketPath : addr ,
123129 })
124130
125131 go func () {
126132 ch , reqs , err := conn .OpenChannel ("forwarded-streamlocal@openssh.com" , payload )
127133 if err != nil {
128- h .log .Warn (ctx , "open SSH channel to forward Unix connection to client" ,
134+ h .log .Warn (ctx , "open SSH unix forward channel to client" ,
129135 slog .F ("socket_path" , addr ),
130136 slog .Error (err ),
131137 )
@@ -143,6 +149,7 @@ func (h *forwardedUnixHandler) HandleSSHRequest(ctx ssh.Context, _ *ssh.Server,
143149 delete (h .forwards , addr )
144150 }
145151 h .Unlock ()
152+ log .Debug (ctx , "SSH unix forward listener removed from cache" , slog .F ("path" , addr ))
146153 _ = ln .Close ()
147154 }()
148155
@@ -152,9 +159,10 @@ func (h *forwardedUnixHandler) HandleSSHRequest(ctx ssh.Context, _ *ssh.Server,
152159 var reqPayload streamLocalForwardPayload
153160 err := gossh .Unmarshal (req .Payload , & reqPayload )
154161 if err != nil {
155- h .log .Warn (ctx , "parse cancel-streamlocal-forward@openssh.com request payload from client" , slog .Error (err ))
162+ h .log .Warn (ctx , "parse cancel-streamlocal-forward@openssh.com (SSH unix forward) request payload from client" , slog .Error (err ))
156163 return false , nil
157164 }
165+ log .Debug (ctx , "request to cancel SSH unix forward" , slog .F ("path" , reqPayload .SocketPath ))
158166 h .Lock ()
159167 ln , ok := h .forwards [reqPayload .SocketPath ]
160168 h .Unlock ()
0 commit comments