Commit Graph

87 Commits

Author SHA1 Message Date
Peter Verraedt c1f47ba1b9
Add WithMaxTxPacket server option
Add the WithMaxTxPacket and WithRSMaxTxPacket server options to increase
the maximum tx packet size to a value above 32K. This allows to send
bigger chunks of data to the client as response to a read request. As
the client specifies the wanted length, it should be safe to increase
the server maximum value.

This in particular allows the implemented Client with the
MaxPacketUnchecked option to retrieve data in larger chunks.

Signed-off-by: Peter Verraedt <peter@verraedt.be>
2024-04-26 09:04:33 +02:00
Cassondra Foesch 72aa4039a1 more short-circuits 2024-02-06 08:41:41 +00:00
Cassondra Foesch f3501dc6ba address code review 2024-01-19 01:23:22 +00:00
Cassondra Foesch d1903fbd46 rework client to prevent after-close usage, and support perm at open 2024-01-19 00:20:23 +00:00
Joe Tsai e9377c8373 Properly handle io.EOF error conditions when reading
Previously, the Server.Serve method would never return nil,
because the infinite for-loop handling request packets would
only break if reading a packet reported an error.
A common termination condition is when the underlying connection
is closed and recvPacket returns io.EOF.
In which case Serve should ignore io.EOF and
treat it as a normal shutdown.

However, this means that recvPacket must correctly handle io.EOF
such that it never reports io.EOF if a packet is partially read.
There are two calls to io.ReadFull in recvPacket.
The first call correctly forwards an io.EOF error
if no additional bytes of the next packet are read.
However, the second call incorrectly forwards io.EOF
when no bytes of the payload could be read.
This is incorrect since we already read the length and
should convert the io.EOF into an io.ErrUnexpectedEOF.
2023-08-09 16:35:43 -07:00
Peter Verraedt c632ce6285
Marshal extended data if file info supports it
If the file information returned by ListAt supports the
FileInfoExtendedData interface, use it to retrieve extended data and
pass it back to the client. In similar fashion, we add the
FileInfoUidGid interface to allow for implementations to return uid and
gid data, even if the os lacks support in syscall.Stat_t.

Signed-off-by: Peter Verraedt <peter@verraedt.be>
2023-07-14 15:52:44 +02:00
Mathias Fredriksson c93b0a0af2 Refactor tests and make `toLocalPath` a method on `Server` 2022-10-18 18:17:45 +03:00
Mathias Fredriksson 6a7168cf46 Add support for working directory in Server
This commit allows the working directory for the (old) Server
implementation to be changed without doing a `os.Chdir` first.

The feature can be enabled with `sftp.WithServerWorkingDirectory(dir)`
passed as an option to `sftp.NewServer`.

It is useful when the `sftp` is used as part of a larger service that
does more than just serve `sftp` and using `os.Chdir` is not an option.

The fallback behavior (when the option is not specified) is that the
path remains unmodified (as before).
2022-10-14 15:54:59 +03:00
Cassondra Foesch 975b486c5e document the weirdness of the reversal of arguments to SSH_FXP_SYMLINK 2022-09-29 08:51:09 +00:00
Cassondra Foesch 7cbc4bdd2f PosixRename, Hardlink: need to convert remote paths to local paths 2022-01-31 21:12:05 +00:00
Cassondra Foesch de654c18c1 godoc for exported function of exported type 2021-08-17 11:41:05 +00:00
Cassondra Foesch d1599c6376 remove clamp function 2021-08-11 11:12:34 +00:00
Cassondra Foesch ba854bee45 collect all marshal/unmarshal functions into packet.go 2021-08-11 11:12:34 +00:00
Sebastien Rosset (serosset) 3b8042dfc0 Use go errors instead of github.com/pkg/errors 2021-06-04 14:18:41 -07:00
Cassondra Foesch f1e28f8a88 Improve benchmarks and errors 2021-03-17 11:03:51 +00:00
Cassondra Foesch 861a8eaf5c pointer receivers and statusFromError(uint32, error) 2021-02-22 12:11:42 +00:00
Cassondra Foesch 5e8f9f4960 MarshalBinary now gives a 4-byte header for length, marshalPacket gives a two-stage write 2021-02-22 12:04:43 +00:00
Cassondra Foesch d4ff5aeb4f remove unnecessary append()s 2021-02-22 12:04:43 +00:00
greatroar cb1556337d Don't crash when the packet length is zero 2020-11-02 17:07:10 +01:00
greatroar d352a1d176 Add Client.Sync method
This uses the fsync@openssh.com extension:
https://github.com/openssh/openssh-portable/blob/master/PROTOCOL, §3.6.
2020-10-23 13:09:54 +02:00
Nicola Murino 3177348ca5 pack structures so they have less size
This is a micro optimization, it fixes warnings like this one:

packet.go:707:24: struct of size 32 bytes could be of size 24 bytes
2020-09-08 10:18:15 +02:00
Nicola Murino 1f178f9671 the allocator can now be enabled per request
Other minor changes as per review comments
2020-03-18 09:36:07 +01:00
Nicola Murino 3f969fcd59 add optional AllocationModeOptimized
after processing a packet we keep in memory the allocated slices and we reuse
them for new packets.
Slices are allocated in:

- recvPacket
- when we receive a sshFxpReadPacket (downloads)

The allocated slices have a fixed size = maxMsgLength.

Allocated slices are referenced to the request order id and are marked for reuse
after a request is served in maybeSendPackets.

The allocator is added to the packetManager struct and it is cleaned at the end
of the Serve() function.

This allocation mode is optional and disabled by default
2020-03-14 19:42:19 +01:00
John Eikenberry 18dc4db7a4
Merge pull request #343 from drakkan/allocations
fileget: allocate a slice with enough capacity
2020-03-10 15:48:33 -07:00
Nicola Murino 0a45bc4b6d sshFxpDataPacket: document the required capacity to avoid a new allocation 2020-03-10 22:56:41 +01:00
Nicola Murino 0f0e40a3a6 minor changes as requested in the review 2020-03-10 15:35:56 +01:00
Nicola Murino eeafeeff60 FxpReadPacket: add an helper method for slice allocation
This way we can use the same method in both server and request-server
2020-03-10 11:46:46 +01:00
Nicola Murino 44e44d716f fileget: allocate a slice with enough capacity
so a new allocation is not needed in MarshalBinary and sendPacket.

Here are some profiling results while downloading a file (file size is about 1GB),

before this patch:

1254.24MB 55.18% 55.18%  1254.24MB 55.18%  github.com/pkg/sftp.sshFxpDataPacket.MarshalBinary
  991.81MB 43.63% 98.81%   991.81MB 43.63%  github.com/pkg/sftp.fileget
       1MB 0.044% 98.86%  1255.24MB 55.22%  github.com/pkg/sftp.(*packetManager).maybeSendPackets
    0.50MB 0.022% 98.88%  1260.24MB 55.44%  github.com/pkg/sftp.(*packetManager).controller
         0     0% 98.88%   991.81MB 43.63%  github.com/pkg/sftp.(*Request).call
         0     0% 98.88%   993.31MB 43.70%  github.com/pkg/sftp.(*RequestServer).Serve.func1.1
         0     0% 98.88%   993.31MB 43.70%  github.com/pkg/sftp.(*RequestServer).packetWorker
         0     0% 98.88%  1254.24MB 55.18%  github.com/pkg/sftp.(*conn).sendPacket
         0     0% 98.88%  1254.24MB 55.18%  github.com/pkg/sftp.sendPacket

with this patch:

1209.48MB 98.46% 98.46%  1209.48MB 98.46%  github.com/pkg/sftp.fileget
       2MB  0.16% 98.63%     7.50MB  0.61%  github.com/pkg/sftp.recvPacket
         0     0% 98.63%        8MB  0.65%  github.com/drakkan/sftpgo/sftpd.Configuration.handleSftpConnection
         0     0% 98.63%  1209.48MB 98.46%  github.com/pkg/sftp.(*Request).call
         0     0% 98.63%        8MB  0.65%  github.com/pkg/sftp.(*RequestServer).Serve
         0     0% 98.63%  1209.98MB 98.50%  github.com/pkg/sftp.(*RequestServer).Serve.func1.1
         0     0% 98.63%  1209.98MB 98.50%  github.com/pkg/sftp.(*RequestServer).packetWorker
         0     0% 98.63%     7.50MB  0.61%  github.com/pkg/sftp.(*conn).recvPacket (inline)
2020-03-09 19:22:48 +01:00
Nicola Murino 01ec2e2e14 write packet UnmarshalBinary: reuse already allocated data 2020-02-26 12:04:13 +01:00
Nicola Murino 4eda1f42bb data packet unmarshal binary: remove uneeded copy 2020-02-16 21:52:13 +01:00
John Eikenberry 84e6527392
Merge pull request #309 from wutz/master
fix: received packet too long
2019-09-29 14:37:50 -07:00
Taizeng Wu cfce8a5728 fix: received packet too long
Close #308
2019-09-29 14:39:33 +08:00
Nicola Murino b4ea0fd6f6 fix lint issues
These lint issues remain:

- request-errors.go, aliases for new error types
- request-attrs.go, UidGid. Changing this will break compatibility
2019-08-30 17:04:37 +02:00
Tommie Gannert 2c24eaad1c Implement the hardlink@openssh.com extension.
Both client and server. This is documented in

  https://github.com/openssh/openssh-portable/blob/master/PROTOCOL

Draft 7 of SFTP added support for SSH_FXP_LINK which supports both
symlinks and hardlinks, but unfortunately OpenSSH doesn't support
that:

  https://tools.ietf.org/html/draft-ietf-secsh-filexfer-07#section-7.7

Adding support for this as an option would be a nice extension to
this.
2019-08-25 20:16:37 -07:00
John Eikenberry d4a0cecfdc remove unstandard error
All unmarshall calls return the same errShortPacket except this one for
no reason. Fix that.
2019-08-25 18:21:33 -07:00
John Eikenberry b9345f483d
Merge pull request #248 from perkeep/packetwrite
Write packet length header & payload together on the wire.
2018-08-24 15:50:03 -07:00
John Eikenberry 1afc1d9a78 refactor server response to allow for extending
Instead of sendPacket/sendError being sprayed all over the place, this
change has all those places instead return a responsePacket (eventually)
back to the main handling function which then calls sendPacket in one
place.

Behaviour of the code should remain exactly the same.

This makes it much easier to work with the response packets (eg. for the
packet ordering issue I'm working on).
2018-07-25 15:01:43 -07:00
John Eikenberry b50b1f9eaf fix sendError usage to match packet type signature
sendError takes a requestPacket but was simplifying it to an ider
interface. Future work needed it to be requestPacket but I wanted to fix
didn't up type usage in its own commit.
2018-07-23 16:53:51 -07:00
Brad Fitzpatrick fbf066c9de Write packet length header & payload together on the wire. 2018-05-11 21:44:57 -07:00
Allan Feid 820ccceeef Send unsupported error on extended packets.
Following the rules outlined here:

https://tools.ietf.org/html/draft-ietf-secsh-filexfer-extensions-00

Return an SSH_FXP_STATUS with appropriate status error for extended
packets that we do not support.
2018-03-19 10:32:22 -04:00
John Eikenberry 9649a986f0 remove unused variables 2018-02-15 11:00:22 -08:00
Iain Wade 9b6cdb8fab Add PosixRename method which uses the posix-rename@openssh.com extension
to support actual rename() operations rather than the link() call
effectively mandated by the sftp v3/draft-2 requirement that targets
not be overwritten.
2017-09-05 04:46:03 -07:00
unclejack 94df0a367c client,packet: remove redundant select & return
Signed-off-by: Cristian Staretu <cristian.staretu@gmail.com>
2017-02-25 12:56:13 -08:00
Dave Cheney 9d52a809ce Always close client conn when clientConn.recv exits (#117)
If recv has exited then nobody will retrive the response, so
make sure we cannot send anything by shutting down the Write side of the
connection.
2016-06-15 20:04:26 +10:00
Dave Cheney 04d5f67e72 Small cleanups 2016-06-14 18:05:33 +10:00
Mark Sheahan 9ff4de5c31 add serverside StatVFS function, implemented for darwin and linux (#89) 2016-06-13 14:45:13 +10:00
Dave Cheney 8bf4044e7f remove svr.in svr.out, replace with svr.rwc (#107) 2016-05-29 18:22:40 +10:00
Dave Cheney 5e9ad277fc Use pkg/errors for error handling (#101) 2016-05-19 15:16:48 +10:00
Mark Sheahan 7e2e721fdf use ssh_FXP_STAT for Stat(); previously Stat() and Lstat() were both Lstat() 2016-04-26 12:07:21 -07:00
Matt Layher 051287be6d *: remove all named and naked returns 2016-01-07 15:56:04 -05:00