Commit Graph

108 Commits

Author SHA1 Message Date
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
Cassondra Foesch bd61319b07 refactor sshfx encoding, fix link rot, go fmt 2023-03-27 17:05:24 +00:00
Nicola Murino 08880975fb statusFromError: improve support for error wrapping 2022-12-25 17:21:42 +01:00
Mathias Fredriksson c93b0a0af2 Refactor tests and make `toLocalPath` a method on `Server` 2022-10-18 18:17:45 +03:00
Mathias Fredriksson 32ac60b179 Fix use of double-toLocalPath in `sshFxpOpendirPacket` 2022-10-17 17:11:18 +03:00
Mathias Fredriksson 78e59618f6 Revert unintended change 2022-10-17 17:09:17 +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 b94f1ccedd NameLookupFileLister so that RequestServer does not automatically lookup system usernames 2021-08-17 11:40:30 +00:00
Cassondra Foesch 00dc491201 refactor ls formatting work 2021-08-11 15:09:43 +00:00
Cassondra Foesch d1599c6376 remove clamp function 2021-08-11 11:12:34 +00:00
Cassondra Foesch e683b3b6ed cleanup request mutex usage 2021-08-02 11:23:37 +00:00
Nicola Murino eaa697cc21
Merge pull request #441 from CiscoM31/migrate_to_go_errors
Use go errors instead of github.com/pkg/errors
2021-07-05 21:28:12 +02:00
Cassondra Foesch 71618243c4 deconvert encoded paths on windows servers 2021-06-29 15:16:15 +00:00
Sebastien Rosset (serosset) 3b8042dfc0 Use go errors instead of github.com/pkg/errors 2021-06-04 14:18:41 -07:00
Nicola Murino a88961882e remove some redundant types 2021-02-22 22:29:35 +01: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
Fazlul Shahriar bbd5cf2737
Merge branch 'master' into plan9 2020-09-15 12:06:10 -04:00
Will Norris 8df2eb9876 server: better handle IsNotExist errors
Return early from statusFromError if os.IsNotExist is true.
2020-09-14 07:58:12 -07:00
Will Norris 28f6fd9187 server: use os.IsNotExist to map sshFxNoSuchFile
Always use os.IsNotExist to identify any OS specific error types that
represent missing files or directories.  This resolves an issue on
Windows where some system errors (ENOTDIR) were not being identified as
'not found' errors and mapped to sshFxNoSuchFile.

fixes #381
2020-09-13 21:38:21 -07:00
Fazlul Shahriar 6120cae121
Fix build on Plan 9
All test are passing on Plan 9, and I've also verified
`examples/go-sftp-server` is working.
2020-09-10 18:18:22 -04: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
Nicola Murino 0f0e40a3a6 minor changes as requested in the review 2020-03-10 15:35:56 +01:00
Nicola Murino 3ea9f24c15 server: remove now unused maxTxPacket
maxTxPacket was only used to get the size for the read packet it is not
needed anymore
2020-03-10 11:48:52 +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
John Eikenberry 1bc3ea14d1 fix issue with file put resume/append
Fixes issue with append uploads. Was opening the file with O_APPEND, but
it uses WriteAt() to write the data which doesn't work with a file
opened in append mode. Removing the append flag fixes the issue as the
client is sending the offsets anyways.

Also added a note to the Request server's FileWriter interface on
handling append flags.
2020-01-05 14:23:49 -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
Nicola Murino b519cb8db9 define supported extensions in one place and use for both server and request-server 2019-08-27 09:18:15 +02:00
Tommie Gannert 687cea6a44 Announce the posix-rename extension in the server. 2019-08-26 09:02:10 +02:00
John Eikenberry 5a2fe52342 Merge branch 'soopsio-master' into test 2019-08-25 20:32:04 -07:00
soopsio 55133952b5 gracefully handle unknown extended packets
Fixed a problem that caused secureFX to crash when
errUnknownExtendedPacket in SFTP server mode
2019-08-25 20:19:45 -07: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 7f7e75b40d ensure packet responses in same order as requests
Previous code used the request ids to do ordering. This worked until a
client came along that used un-ordered request ids. This reworks the
ordering to use an internal counter (per session) to order all packets
ensuring that responses are sent in the same order as the requests were
received.

Fixes #260
2018-08-01 13:18:57 -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
John Eikenberry 523bded012 replace interface{} with more specific type 2018-07-23 16:38:21 -07:00
John Eikenberry 048358fb96 use correct param type instead of asserting
Instead of accepting a more general type and then asserting it to the
proper type, just take the proper type as the argument.
Also clean up some of the use of it where it checked old direct sending
code's return error (error is now always nil).
2018-07-23 15:53:41 -07:00
John Eikenberry 4cad28be43 eliminate bare values in composite literals
Using key-value pairs is much more future proof.
2018-07-23 14:49:12 -07:00
John Eikenberry d0a1c8098b remove unnecessary type
Added to shorten code text, but not used enough to be worth the extra type.
2018-07-23 12:18:00 -07:00
John Eikenberry 218c0d4148 Opendir return an error status when not found
The initial Opendir packet is supposed to repond with an error status if
the directory wasn't found. It was just returning a handle without
checking, now it does a Stat on the path and only returns the handle if
the Stat is successful and it indicates it is a directory, otherwise it
returns an error.
2018-05-26 13:59:55 -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
John Eikenberry 738e088bbd support handlers returning explicit error codes
Add errors for all the SSH_FXP_STATUS codes to give the developer
implementing request server handlers greater control over the returned
codes. Most helpful in cases where nothing currently would work (eg.
unsupported).

Fixes #223
2018-01-26 17:26:44 -08:00
John Eikenberry 4ab81b0271 improve statusFromError readability 2018-01-25 17:29:36 -08:00
John Eikenberry bc6b56aae0 packageManager to use pointer receivers everywhere
Had a problem with getting a copy because a value receiver called a
pointer receiver.
2017-08-20 15:23:55 -07:00
John Eikenberry 26ceac758e add os.ErrNotExist handling into statusFromError
And remove errorAdapter() as this makes it unnecessary.
2017-08-20 14:54:45 -07:00
andreas 4d7bb970c4 Resolved conflict with SftpServerWorkerCount
Splitted cleanPath into cleanPacketPath and cleanPath for better handling of slashes in file paths
Added test for cleanPath func
Removed code duplication => filepath.ToSlash(filepath.Clean(...)) => cleanPath(...)
Fixed tests for runLs to match year or time
Renamed constants to fit hound rules
2017-08-13 14:00:08 +02:00
andreas ec08c0d53b Moved permission detection for runLs to server.go
Make runLs return the recommended format in stubs, even if it does not contain all information
Removed unused parameter
2017-08-10 07:34:48 +02:00