Compare commits

...

5 Commits

Author SHA1 Message Date
Cassondra Foesch fbfc8f8bba
Merge c7176b3c6e into 36ce9cfd5c 2025-05-30 14:00:21 +00:00
Cassondra Foesch c7176b3c6e rework recv debug messages to be context-added errors 2025-05-30 14:00:15 +00:00
Cassondra Foesch d9ce3caa72 convert uses of uint8 instead of fxp to fxp 2025-05-30 13:30:46 +00:00
Cassondra Foesch 9ae47f4170 better debug info 2025-05-30 13:23:15 +00:00
Cassondra Foesch 8a0fc6568b DO NOT close the CopyStderrTo writer 2025-05-30 12:50:06 +00:00
7 changed files with 54 additions and 33 deletions

View File

@ -159,7 +159,10 @@ func UseFstat(value bool) ClientOption {
} }
// CopyStderrTo specifies a writer to which the standard error of the remote sftp-server command should be written. // CopyStderrTo specifies a writer to which the standard error of the remote sftp-server command should be written.
func CopyStderrTo(wr io.WriteCloser) ClientOption { //
// The writer passed in will not be automatically closed.
// It is the responsibility of the caller to coordinate closure of any writers.
func CopyStderrTo(wr io.Writer) ClientOption {
return func(c *Client) error { return func(c *Client) error {
c.stderrTo = wr c.stderrTo = wr
return nil return nil
@ -174,7 +177,7 @@ func CopyStderrTo(wr io.WriteCloser) ClientOption {
type Client struct { type Client struct {
clientConn clientConn
stderrTo io.WriteCloser stderrTo io.Writer
ext map[string]string // Extensions (name -> data). ext map[string]string // Extensions (name -> data).
@ -214,17 +217,17 @@ func NewClient(conn *ssh.Client, opts ...ClientOption) (*Client, error) {
return nil, err return nil, err
} }
return newClientPipe(pr, pw, perr, s.Wait, opts...) return newClientPipe(pr, perr, pw, s.Wait, opts...)
} }
// NewClientPipe creates a new SFTP client given a Reader and a WriteCloser. // NewClientPipe creates a new SFTP client given a Reader and a WriteCloser.
// This can be used for connecting to an SFTP server over TCP/TLS or by using // This can be used for connecting to an SFTP server over TCP/TLS or by using
// the system's ssh client program (e.g. via exec.Command). // the system's ssh client program (e.g. via exec.Command).
func NewClientPipe(rd io.Reader, wr io.WriteCloser, opts ...ClientOption) (*Client, error) { func NewClientPipe(rd io.Reader, wr io.WriteCloser, opts ...ClientOption) (*Client, error) {
return newClientPipe(rd, wr, nil, nil, opts...) return newClientPipe(rd, nil, wr, nil, opts...)
} }
func newClientPipe(rd io.Reader, wr io.WriteCloser, stderr io.Reader, wait func() error, opts ...ClientOption) (*Client, error) { func newClientPipe(rd, stderr io.Reader, wr io.WriteCloser, wait func() error, opts ...ClientOption) (*Client, error) {
c := &Client{ c := &Client{
clientConn: clientConn{ clientConn: clientConn{
conn: conn{ conn: conn{
@ -256,13 +259,10 @@ func newClientPipe(rd io.Reader, wr io.WriteCloser, stderr io.Reader, wait func(
} }
go func() { go func() {
defer func() { // DO NOT close the writer!
if closer, ok := wr.(io.Closer); ok { // Programs may pass in `os.Stderr` to write the remote stderr to,
if err := closer.Close(); err != nil { // and the program may continue after disconnect by reconnecting.
debug("error closing stderrTo: %v", err) // But if we've closed their stderr, then we just messed everything up.
}
}
}()
if _, err := io.Copy(wr, stderr); err != nil { if _, err := io.Copy(wr, stderr); err != nil {
debug("error copying stderr: %v", err) debug("error copying stderr: %v", err)

View File

@ -22,7 +22,7 @@ type conn struct {
// For the client mode just pass 0. // For the client mode just pass 0.
// It returns io.EOF if the connection is closed and // It returns io.EOF if the connection is closed and
// there are no more packets to read. // there are no more packets to read.
func (c *conn) recvPacket(orderID uint32) (uint8, []byte, error) { func (c *conn) recvPacket(orderID uint32) (fxp, []byte, error) {
return recvPacket(c, c.alloc, orderID) return recvPacket(c, c.alloc, orderID)
} }
@ -142,7 +142,7 @@ func (c *clientConn) getChannel(sid uint32) (chan<- result, bool) {
// result captures the result of receiving the a packet from the server // result captures the result of receiving the a packet from the server
type result struct { type result struct {
typ byte typ fxp
data []byte data []byte
err error err error
} }
@ -152,7 +152,7 @@ type idmarshaler interface {
encoding.BinaryMarshaler encoding.BinaryMarshaler
} }
func (c *clientConn) sendPacket(ctx context.Context, ch chan result, p idmarshaler) (byte, []byte, error) { func (c *clientConn) sendPacket(ctx context.Context, ch chan result, p idmarshaler) (fxp, []byte, error) {
if cap(ch) < 1 { if cap(ch) < 1 {
ch = make(chan result, 1) ch = make(chan result, 1)
} }

View File

@ -304,16 +304,22 @@ func sendPacket(w io.Writer, m encoding.BinaryMarshaler) error {
return nil return nil
} }
func recvPacket(r io.Reader, alloc *allocator, orderID uint32) (uint8, []byte, error) { func recvPacket(r io.Reader, alloc *allocator, orderID uint32) (fxp, []byte, error) {
var b []byte var b []byte
if alloc != nil { if alloc != nil {
b = alloc.GetPage(orderID) b = alloc.GetPage(orderID)
} else { } else {
b = make([]byte, 4) b = make([]byte, 4)
} }
if _, err := io.ReadFull(r, b[:4]); err != nil {
return 0, nil, err if n, err := io.ReadFull(r, b[:4]); err != nil {
if err == io.EOF {
return 0, nil, err
}
return 0, nil, fmt.Errorf("error reading packet length: %d of 4: %w", n, err)
} }
length, _ := unmarshalUint32(b) length, _ := unmarshalUint32(b)
if length > maxMsgLength { if length > maxMsgLength {
debug("recv packet %d bytes too long", length) debug("recv packet %d bytes too long", length)
@ -323,24 +329,39 @@ func recvPacket(r io.Reader, alloc *allocator, orderID uint32) (uint8, []byte, e
debug("recv packet of 0 bytes too short") debug("recv packet of 0 bytes too short")
return 0, nil, errShortPacket return 0, nil, errShortPacket
} }
if alloc == nil { if alloc == nil {
b = make([]byte, length) b = make([]byte, length)
} }
if _, err := io.ReadFull(r, b[:length]); err != nil {
n, err := io.ReadFull(r, b[:length])
b = b[:n]
if err != nil {
debug("recv packet error: %d of %d bytes: %x", n, length, b)
// ReadFull only returns EOF if it has read no bytes. // ReadFull only returns EOF if it has read no bytes.
// In this case, that means a partial packet, and thus unexpected. // In this case, that means a partial packet, and thus unexpected.
if err == io.EOF { if err == io.EOF {
err = io.ErrUnexpectedEOF err = io.ErrUnexpectedEOF
} }
debug("recv packet %d bytes: err %v", length, err)
return 0, nil, err if n == 0 {
return 0, nil, fmt.Errorf("error reading packet body: %d of %d: %w", n, length, err)
}
return 0, nil, fmt.Errorf("error reading packet body: %d of %d: (%s) %w", n, length, fxp(b[0]), err)
} }
typ, payload := fxp(b[0]), b[1:n]
if debugDumpRxPacketBytes { if debugDumpRxPacketBytes {
debug("recv packet: %s %d bytes %x", fxp(b[0]), length, b[1:length]) debug("recv packet: %s %d bytes %x", typ, length, payload)
} else if debugDumpRxPacket { } else if debugDumpRxPacket {
debug("recv packet: %s %d bytes", fxp(b[0]), length) debug("recv packet: %s %d bytes", typ, length)
} }
return b[0], b[1:length], nil
return typ, payload, nil
} }
type extensionPair struct { type extensionPair struct {

View File

@ -468,7 +468,7 @@ func TestRecvPacket(t *testing.T) {
var recvPacketTests = []struct { var recvPacketTests = []struct {
b []byte b []byte
want uint8 want fxp
body []byte body []byte
wantErr error wantErr error
}{ }{

View File

@ -148,7 +148,7 @@ func (rs *RequestServer) serveLoop(pktChan chan<- orderedRequest) error {
var err error var err error
var pkt requestPacket var pkt requestPacket
var pktType uint8 var pktType fxp
var pktBytes []byte var pktBytes []byte
for { for {
@ -158,7 +158,7 @@ func (rs *RequestServer) serveLoop(pktChan chan<- orderedRequest) error {
return err return err
} }
pkt, err = makePacket(rxPacket{fxp(pktType), pktBytes}) pkt, err = makePacket(rxPacket{pktType, pktBytes})
if err != nil { if err != nil {
switch { switch {
case errors.Is(err, errUnknownExtendedPacket): case errors.Is(err, errUnknownExtendedPacket):

View File

@ -390,7 +390,7 @@ func (svr *Server) Serve() error {
var err error var err error
var pkt requestPacket var pkt requestPacket
var pktType uint8 var pktType fxp
var pktBytes []byte var pktBytes []byte
for { for {
pktType, pktBytes, err = svr.serverConn.recvPacket(svr.pktMgr.getNextOrderID()) pktType, pktBytes, err = svr.serverConn.recvPacket(svr.pktMgr.getNextOrderID())
@ -403,7 +403,7 @@ func (svr *Server) Serve() error {
break break
} }
pkt, err = makePacket(rxPacket{fxp(pktType), pktBytes}) pkt, err = makePacket(rxPacket{pktType, pktBytes})
if err != nil { if err != nil {
switch { switch {
case errors.Is(err, errUnknownExtendedPacket): case errors.Is(err, errUnknownExtendedPacket):

View File

@ -184,15 +184,15 @@ func (f fx) String() string {
} }
type unexpectedPacketErr struct { type unexpectedPacketErr struct {
want, got uint8 want, got fxp
} }
func (u *unexpectedPacketErr) Error() string { func (u *unexpectedPacketErr) Error() string {
return fmt.Sprintf("sftp: unexpected packet: want %v, got %v", fxp(u.want), fxp(u.got)) return fmt.Sprintf("sftp: unexpected packet: want %v, got %v", u.want, u.got)
} }
func unimplementedPacketErr(u uint8) error { func unimplementedPacketErr(u fxp) error {
return fmt.Errorf("sftp: unimplemented packet type: got %v", fxp(u)) return fmt.Errorf("sftp: unimplemented packet type: got %v", u)
} }
type unexpectedIDErr struct{ want, got uint32 } type unexpectedIDErr struct{ want, got uint32 }