Commit Graph

81 Commits

Author SHA1 Message Date
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
John Eikenberry 5a2fe52342 Merge branch 'soopsio-master' into test 2019-08-25 20:32:04 -07:00
John Eikenberry 5d724974df Merge branch 'kardianos-kardianos-server-ext' into test 2019-08-25 20:24:06 -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
Daniel Theophanes e5ded3a9d1 sftp: support rename extension for server
Previously if a client makes an unsupported operation,
like a POSIX rename, it would exit the server.

Both support POSIX rename, and do not abort the connection
if there is an unsupported operation is made by the client.
2019-08-25 20:17:08 -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 7f106a3327 change fstat to not reuse request object
When fstat is called it now uses the handle to get the opened request,
pulls the filepath from that and creates a new request for the fstat
call. This eliminates the need to change the Method on the request
object and furthers the goal of eliminating post-creation mutation of
the request object to open up the possibility to eliminate or at least
reduce the use of the global request lock.
2019-01-29 14:49:22 -08:00
John Eikenberry c71cbb17d3 'open' directory on opendir packet
Just like with files, the call to the hander to create the Lister is
done when the OPENDIR packet is received. This eliminates the need for
the lock in the filelist() method and opens the path for eliminating
more locks.
2019-01-29 14:49:22 -08:00
John Eikenberry 0a96e0de8a initial test of opening 'file' on open packet
Testing feasibility of creating the reader/writer when first receiving
the Open packet instead of waiting for the Get/Put.

This work is meant to make the Request locks easier to manage and
ultimately reducing/eliminating them as much as possible.
2019-01-29 14:49:22 -08: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 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 57673e38ea no longer need new request after opendir stat
PR #257 fixes the issue with running Stat and Readdir on the same
Request. Thus we no longer need to close and recreate the Request
when handling an Opendir call.
2018-06-10 18:16:20 -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 7d7ee5c869 Handle RequestServer the same 2018-03-19 11:53:13 -04: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 68e5d2f242 better/clearer function name 2018-03-10 12:51:41 -08:00
John Eikenberry 50d15dd2c8 remove some unused/unnecessary code 2018-02-15 11:46:17 -08:00
John Eikenberry 353daee2bc better request copy code
With state's lock as a pointer, we can use the `*r = *r2` statement for
copying which is much more concise and future proof.
2018-02-06 12:22:08 -08:00
John Eikenberry b00841bcd7 create master/parent context in the server session
Create a master Context in the per-session ReqeustServer.Serve method
that is then used as the parent for all Context's created in that
server. Its cancelFunc is then always called when Serve exits. This is
in line which out the http.serve code works.

Each Request gets a child WithCancel context created in the
requestFromPacket call. This Context is still closed on request.close().
2018-02-05 14:10:05 -08:00
John Eikenberry 36cbb62432 make sure request is always closed properly 2018-02-05 12:46:27 -08:00
John Eikenberry 30b632cf56 handle errors in file creation from open packet
open packet w/ the create flag would create the file but was ignoring
any errors from that. this fixes that and adds a test for it.
2018-01-07 18:30:26 -08:00
John Eikenberry c33862bb7d remove special handling of Fstat/Fsetstat
Recent refactoring has eliminated the need for Fstat and Fsetstat (stat
operations on file handle instead of path)to be special cased. They will
now just work with the normal case for any packet with a file handle.
2018-01-07 16:04:33 -08:00
John Eikenberry 49c117d57a Fix volume name mangling on windows.
Fixes #217, where the path cleaning code was mangling the volume name on
windows; ie. "c:/some/path" was turned into "/c:/some/path". Using the
filepath.IsAbs() (vs path.IsAbs()) uses OS specific tests and fixes
this.

This goes against strict POSIX paths, but this used to work and fixing
it breaks nothing on other platforms. And we have still standardized on
POSIX style delimiters.
2018-01-07 15:23:55 -08:00
John Eikenberry fa2d0c3125 respect create flag in open packet
When the SSH_FXP_OPEN packet has the SSH_FXF_CREAT pflag set the file
should get created even if no data is sent. We do this by having the
Open method call through to the FilePut Handler with empty packet data.

Fixes #214
2017-12-23 19:14:16 -08:00
John Eikenberry 8722020a70 simpify Request struct and Method updating
In #192 we normalized all Request use to have pointer receivers. This
simplified reasoning and allowed for a simpler Request struct. This
change completes that work by eliminating the need for the
state/stackLock pointers and also removes the need for a new Request
each packet, reducing the pressure on the GC.

The main bit is that the RequestServer.openRequests[] map now stores Request
pointers and it only creates new Requests (replacing the mapped entry) when the
Method changes. It never updates/modifies an existing Request and the packet to
Method mapping is now all in place place (requestMethod).
2017-12-23 16:34:22 -08:00
John Eikenberry 3030aca5da Fix issue with path 'cleaning' and adds tests
Fix #207

The primary issue of the resolved ticket was path cleaning code returned
the path of '/.' instead of '/'.

We also reviewed and clarified the use of filepath/path and how OS
specific paths are handled in the server code. All paths are converted
to POSIX paths as they come into the server. They are then POSIX paths
for all internal operations.
2017-12-21 12:05:53 -08:00
Mayank Agarwal 1816888769 fix(request-server): handle file close error 2017-12-12 23:35:18 +05:30
John Eikenberry 87518d071d remove vestigial packets channel from request
Request object had a packet channel where it stored packets for
read/write/readdir handling. It was required in an earlier version of
the code (that was refactored before merging) and never got cleaned up.

This removes that channel and just passes the packet through the methods
instead. This is much simpler and would have eliminated the chance of
issue #195. It also looks like it should allow for a few more
simplifications as well.
2017-08-22 19:24:14 -07:00
John Eikenberry 19d66c2d96 change request.handle() to request.callHandler()
Handle was already used for sftp's session 'handle' which is the name
used in the other code and sftp specs.
2017-08-20 17:51:45 -07:00
John Eikenberry aedf9a737a Fix #192 all Request methods use pointer recivers
Prevents subtle copy bugs and made the 1 required Request copy explicit.
2017-08-20 17:03:15 -07:00
John Eikenberry cffd2fabb0 Fix #195 Handler wrappers deal with errors
TLDR; have the Handler wrappers generate the response packets for the
errors instead of returning the errors.

Errors from the handers was returned up the stack to the request-server
handler call. In cases of errors the packet for the error was then
generated using the passed in packet (the incoming packet).

For file read/write/list operations the incoming packet data (includeing
ID) is put in a queue/channel so that it can be handled by the same file
handling code returned by the handler.

Due to gorouting scheduling, in some cases the packets in the channel
won't line up with the incoming packets. That is the worker that took an
incoming packet with one ID might respond with a packet with a different
ID. This doesn't matter as the reply order of packets doesn't matter.

BUT when this response packet that doesn't match IDs with the incoming
packet system returns an error, it uses the incoming packets ID to
generate the error packet. So the error response would use that ID and
the packet from the queue, once processed, would also use that ID
(because on success  it uses the ID from the packet in the queue).

This patch fixes the issue by having the code that works with the
packets, either incoming or from the queue, generate the error packet.
So there is no chance of them getting out of sync.
2017-08-20 16:15:01 -07: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
sandreas 653e2f1043 Merge branch 'master' into master 2017-08-13 17:00:45 +02: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
John Eikenberry 733115c415 Fixes #184; better fix for file batching
I didn't like how the initial fix for #184 required the handler author
to track the token/offset for the filelist. I came up with a way to do
it that mimic'd the FileReader handler interface using a method
something like ReaderAt except for file lists.

changed MaxFileinfoList to a var for testing and tweaking

Renaming FileInfoer interface to FileLister.
FileInfoer -> FileLister
Fileinfo -> Filelist
2017-08-10 15:36:41 -07:00
Andreas 939f0f3934 Fixed possible double slash in request 2017-08-10 12:50:08 +02:00
Andreas 505f2cb49e Fixed issues with backslashes on windows systems 2017-08-09 17:25:44 +02:00
John Eikenberry e97b9a47e1 avoid data race in worker creation
There is a data race with the waitgroup (wg) object used to synchronize
the workers with the server exit. The workers called wg.Add()
asynchronously and it was possible for the Wait() to get hit before any
of the Add() calls were made in certain conditions. I only ever saw this
sporatically in the travis tests.

This fixes it by making the wg.Add() calls synchronous.
2017-07-03 17:53:55 -07:00
John Eikenberry 07d4ed1b5c don't copy lock
govet pointed out that I was copying a lock when creating/assigning the
connection object. It didn't cause any issues, but was a bad practice.
2017-07-03 17:45:58 -07:00
John Eikenberry adb5b39283 convert fsetstat to setstat in request-server
Handle fsetstat in same was a fstat, by pulling path from stored file
info and processing in that form. Makes handlers simpler while
preserving functionality.
2017-04-24 21:53:56 -07:00
John Eikenberry 4cfaeaf9b7 fix hang on fstat calls in request server
Support fstat by converting to a standard Stat call and processing that.
I didn't want to add another special case to the request backend, I
wanted to keep it only having to support Stat.
2017-04-24 21:39:51 -07:00
John Eikenberry ced55b1bf0 request-server to use new packet ordering code
The 2 servers now use all the same packet managing code to ensure
ordered processing and reply packets.
2017-04-23 14:27:25 -07:00
Pavel Borzenkov 9aa225fc15 Don't copy Server struct in sendPacket/sendError
Server struct contains Mutex which might be copied in inconsistent
state. Avoid this by declaring methods on pointer receiver.

This calms down go race detector.

Signed-off-by: Pavel Borzenkov <pavel.borzenkov@gmail.com>
2017-04-05 14:15:57 -07:00
John Eikenberry 5152c21cf9 integrate packageManager into servers 2017-03-23 16:15:04 -07:00
John Eikenberry 51cb116815 better name: packet -> requestPacket 2017-03-23 16:15:04 -07:00
John Eikenberry 7e76fd5451 close reader/writer when deleting request 2017-02-18 18:20:37 -08:00