Class SftpSubsystem
- All Implemented Interfaces:
Closeable
,AutoCloseable
,Runnable
,FileSystemAware
,SessionContextHolder
,SessionHolder<ServerSession>
,ExecutorServiceCarrier
,ChannelDataReceiver
,ChannelSessionAware
,AsyncCommand
,Command
,CommandLifecycle
,ServerSessionHolder
,SessionAware
,SftpEventListenerManager
,SftpSubsystemEnvironment
,SftpSubsystemProxy
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final Buffer
protected ExitCallback
protected ChannelSession
protected static final Buffer
protected final AtomicBoolean
protected Path
protected Environment
protected IoOutputStream
protected CloseableExecutorService
protected int
protected FileSystem
protected int
protected IoOutputStream
protected Future<?>
protected Random
protected final BlockingQueue<Buffer>
protected final AtomicLong
protected ServerSession
protected int
protected byte[]
Fields inherited from class org.apache.sshd.sftp.server.AbstractSftpSubsystemHelper
DEFAULT_ACL_SUPPORTED_MASK, DEFAULT_OPEN_SSH_EXTENSIONS, DEFAULT_OPEN_SSH_EXTENSIONS_NAMES, DEFAULT_SUPPORTED_CLIENT_EXTENSIONS
Fields inherited from class org.apache.sshd.common.util.logging.AbstractLoggingBean
log
Fields inherited from interface org.apache.sshd.sftp.server.SftpSubsystemEnvironment
ALL_SFTP_IMPL, HIGHER_SFTP_IMPL, LOWER_SFTP_IMPL, SUPPORTED_SFTP_VERSIONS
-
Constructor Summary
ConstructorsConstructorDescriptionSftpSubsystem
(CloseableExecutorService executorService, UnsupportedAttributePolicy policy, SftpFileSystemAccessor accessor, SftpErrorStatusDataHandler errorStatusDataHandler) -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
protected void
protected void
createLink
(int id, String existingPath, String linkPath, boolean symLink) int
data
(ChannelSession channel, byte[] buf, int start, int len) Called when the server receives additional bytes from the client.void
destroy
(ChannelSession channel) This method is called by the SSH server to destroy the command because the client has disconnected somehow.protected void
protected void
doCheckFileHash
(int id, String targetType, String target, Collection<String> algos, long startOffset, long length, int blockSize, Buffer buffer) protected void
protected void
doCopyData
(int id, String readHandle, long readOffset, long readLength, String writeHandle, long writeOffset) protected void
doFSetStat
(int id, String handle, Map<String, ?> attrs) protected void
protected byte[]
doMD5Hash
(int id, String targetType, String target, long startOffset, long length, byte[] quickCheckHash) protected String
protected String
doOpenDir
(int id, String path, Path p, LinkOption... options) protected void
doOpenSSHFsync
(int id, String handle) protected void
protected int
protected void
protected void
doTextSeek
(int id, String handle, long line) protected void
protected void
doVersionSelect
(Buffer buffer, int id, String proposed) protected void
protected String
generateFileHandle
(Path file) int
protected Buffer
prepareReply
(Buffer buffer) void
run()
protected void
void
setChannelSession
(ChannelSession session) Receives the channel in which the command is being executed.void
Set the error stream that can be used by the shell to write its errors.void
setExitCallback
(ExitCallback callback) Set the callback that the shell has to call when it is closed.void
setFileSystem
(FileSystem fileSystem) Set the file system in which this shell will be executed.void
Set the input stream that can be used by the shell to read input.void
Set the error stream that can be used by the shell to write its errors.void
Set the input stream that can be used by the shell to read input.void
Set the output stream that can be used by the shell to write its output.void
Set the output stream that can be used by the shell to write its output.void
setSession
(ServerSession session) void
start
(ChannelSession channel, Environment env) Starts the command execution.Methods inherited from class org.apache.sshd.sftp.server.AbstractSftpSubsystemHelper
addMissingAttribute, addSftpEventListener, appendAclSupportedExtension, appendExtensions, appendNewlineExtension, appendOpenSSHExtensions, appendSupported2Extension, appendSupportedExtension, appendVendorIdExtension, appendVersionsExtension, checkVersionCompatibility, doBlock, doCheckFileHash, doCheckFileHash, doClose, doCopyData, doCopyFile, doCopyFile, doCopyFile, doExtended, doFSetStat, doFStat, doLink, doLink, doLStat, doLStat, doMakeDirectory, doMakeDirectory, doMD5Hash, doMD5Hash, doOpen, doOpenDir, doOpenSSHFsync, doOpenSSHHardLink, doOpenSSHHardLink, doRead, doReadDir, doReadLink, doReadLink, doRealPath, doRealPathV345, doRealPathV6, doRemove, doRemove, doRemove, doRemoveDirectory, doRemoveDirectory, doRename, doRename, doRename, doSetAttributes, doSetStat, doSetStat, doSpaceAvailable, doSpaceAvailable, doStat, doStat, doSymLink, doSymLink, doTextSeek, doUnblock, doUnsupported, doUnsupportedExtension, doVersionSelect, doWrite, executeExtendedCommand, getAttributes, getAttributes, getErrorStatusDataHandler, getFileSystemAccessor, getLongName, getLongName, getLongName, getPathResolutionLinkOption, getSftpEventListenerProxy, getShortName, getSupportedClientExtensions, getUnsupportedAttributePolicy, handleReadFileAttributesException, handleSetFileAttributeFailure, handleUnknownStatusFileAttributes, handleUnsupportedAttributes, handleUserPrincipalLookupServiceException, normalize, process, readAttrs, readFileAttributes, removeSftpEventListener, resolveAclSupportedCapabilities, resolveFile, resolveFileAttributes, resolveMissingFileAttributes, resolveMissingFileAttributeValue, resolveNewlineValue, resolveNormalizedLocation, resolveOpenSSHExtensions, resolvePathResolutionFollowLinks, sendAttrs, sendHandle, sendLink, sendPath, sendStatus, sendStatus, sendStatus, setFileAccessControl, setFileAttribute, setFileAttributes, setFileExtensions, setFileOwnership, setFilePermissions, setFileRawViewAttribute, setFileTime, signalOpenFailure, signalRemovalPreConditionFailure, toGroup, toUser, validateHandle, validateProposedVersion, validateRealPath, writeAttrs, writeDirEntry
Methods inherited from class org.apache.sshd.common.util.logging.AbstractLoggingBean
debug, debug, debug, debug, debug, error, error, error, error, error, getSimplifiedLogger, info, info, warn, warn, warn, warn, warn, warn, warn, warn
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.apache.sshd.common.file.FileSystemAware
setFileSystemFactory
Methods inherited from interface org.apache.sshd.common.session.SessionHolder
getSessionContext
Methods inherited from interface org.apache.sshd.sftp.server.SftpSubsystemEnvironment
getSession
-
Field Details
-
CLOSE
-
closed
-
requestsCount
-
extensions
-
handles
-
buffer
-
requests
-
callback
-
out
-
err
-
env
-
randomizer
-
fileHandleSize
protected int fileHandleSize -
maxFileHandleRounds
protected int maxFileHandleRounds -
pendingFuture
-
workBuf
protected byte[] workBuf -
fileSystem
-
defaultDir
-
version
protected int version -
serverSession
-
channelSession
-
executorService
-
-
Constructor Details
-
SftpSubsystem
public SftpSubsystem(CloseableExecutorService executorService, UnsupportedAttributePolicy policy, SftpFileSystemAccessor accessor, SftpErrorStatusDataHandler errorStatusDataHandler) - Parameters:
executorService
- TheCloseableExecutorService
to be used by theSftpSubsystem
command when starting execution. Ifnull
then a single-threaded ad-hoc service is used.policy
- TheUnsupportedAttributePolicy
to use if failed to access some local file attributesaccessor
- TheSftpFileSystemAccessor
to use for opening files and directorieserrorStatusDataHandler
- The (nevernull
)SftpErrorStatusDataHandler
to use when generating failed commands error messages- See Also:
-
-
Method Details
-
getVersion
public int getVersion()- Specified by:
getVersion
in interfaceSftpSubsystemEnvironment
- Returns:
- The negotiated version
-
getDefaultDirectory
- Specified by:
getDefaultDirectory
in interfaceSftpSubsystemEnvironment
- Returns:
- The default root directory used to resolve relative paths - a.k.a. the
chroot
location
-
getExecutorService
- Specified by:
getExecutorService
in interfaceExecutorServiceCarrier
- Returns:
- The
CloseableExecutorService
to use
-
setSession
- Specified by:
setSession
in interfaceSessionAware
- Parameters:
session
- TheServerSession
in which this shell will be executed.
-
getServerSession
- Specified by:
getServerSession
in interfaceServerSessionHolder
- Returns:
- The underlying
ServerSession
used
-
setChannelSession
Description copied from interface:ChannelSessionAware
Receives the channel in which the command is being executed.- Specified by:
setChannelSession
in interfaceChannelSessionAware
- Parameters:
session
- never null
-
setFileSystem
Description copied from interface:FileSystemAware
Set the file system in which this shell will be executed.- Specified by:
setFileSystem
in interfaceFileSystemAware
- Parameters:
fileSystem
- the file system
-
setExitCallback
Description copied from interface:Command
Set the callback that the shell has to call when it is closed.- Specified by:
setExitCallback
in interfaceCommand
- Parameters:
callback
- TheExitCallback
to call when shell is closed
-
setInputStream
Description copied from interface:Command
Set the input stream that can be used by the shell to read input.- Specified by:
setInputStream
in interfaceCommand
- Parameters:
in
- TheInputStream
used by the shell to read input.
-
setOutputStream
Description copied from interface:Command
Set the output stream that can be used by the shell to write its output.- Specified by:
setOutputStream
in interfaceCommand
- Parameters:
out
- TheOutputStream
used by the shell to write its output
-
setErrorStream
Description copied from interface:Command
Set the error stream that can be used by the shell to write its errors.- Specified by:
setErrorStream
in interfaceCommand
- Parameters:
err
- TheOutputStream
used by the shell to write its errors
-
setIoInputStream
Description copied from interface:AsyncCommand
Set the input stream that can be used by the shell to read input.- Specified by:
setIoInputStream
in interfaceAsyncCommand
- Parameters:
in
- TheIoInputStream
used by the shell to read input
-
setIoOutputStream
Description copied from interface:AsyncCommand
Set the output stream that can be used by the shell to write its output.- Specified by:
setIoOutputStream
in interfaceAsyncCommand
- Parameters:
out
- TheIoOutputStream
used by the shell to write its output
-
setIoErrorStream
Description copied from interface:AsyncCommand
Set the error stream that can be used by the shell to write its errors.- Specified by:
setIoErrorStream
in interfaceAsyncCommand
- Parameters:
err
- TheIoOutputStream
used by the shell to write its errors
-
start
Description copied from interface:CommandLifecycle
Starts the command execution. All streams must have been set before calling this method. The command should implementRunnable
, and this method should spawn a new thread like:Thread(this).start();
- Specified by:
start
in interfaceCommandLifecycle
- Parameters:
channel
- TheChannelSession
through which the command has been receivedenv
- TheEnvironment
- Throws:
IOException
- If failed to start
-
data
Description copied from interface:ChannelDataReceiver
Called when the server receives additional bytes from the client. When
Closeable.close()
-d then indicates EOF - The client will no longer send us any more data.SSH channels use the windowing mechanism to perform flow control, much like TCP does. The server gives the client the initial window size, which represents the number of bytes the client can send to the server. As the server receives data, it can send a message to the client to allow it to send more data.
The return value from this method is used to control this behaviour. Intuitively speaking, the callee returns the number of bytes consumed by this method, by the time this method returns. Picture a one-way long bridge (for example Golden Gate Bridge) with toll plazas on both sides. The window size is the maximum number of cars allowed on the bridge. Here we are on the receiving end, so our job here is to count the number of cars as it leaves the bridge, and if enough of them left, we'll signal the sending end that they can let in more cars. The return value of this method counts the number of cars that are leaving in this batch.
In simple cases, where the callee has consumed the bytes before it returns, the return value must be the same value as the 'len' parameter given.
On the other hand, if the callee is queueing up the received bytes somewhere to be consumed later (for example by another thread), then this method should return 0, for the bytes aren't really consumed yet. And when at some later point the bytes are actually used, then you'll invoke
channel.getLocalWindow().consumeAndCheck(len)
to let the channel know that bytes are consumed.This behaviour will result in a better flow control, as the server will not allow the SSH client to overflow its buffer. If instead you always return the value passed in the 'len' parameter, the place where you are queueing up bytes may overflow.
In either case, the callee must account for every bytes it receives in this method. Returning 0 and failing to call back
channel.getLocalWindow().consumeAndCheck(len)
later will dry up the window size, and eventually the client will stop sending you any data.In the SSH protocol, this method invocation is triggered by a SSH_MSG_CHANNEL_DATA message.
- Specified by:
data
in interfaceChannelDataReceiver
- Parameters:
channel
- The caller to which thisChannelDataReceiver
is assigned. Never null.buf
- Holds the bytes received. This buffer belongs to the caller, and it might get reused by the caller as soon as this method returns.start
- buf[start] is the first byte that received from the client.len
- the length of the bytes received. Can be zero.- Returns:
- The number of bytes consumed, for the purpose of the flow control. For a simple use case, you return the value given by the 'len' parameter. See the method javadoc for more details.
- Throws:
IOException
- if failed to consume the data
-
run
public void run() -
close
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Throws:
IOException
-
doProcess
- Overrides:
doProcess
in classAbstractSftpSubsystemHelper
- Throws:
IOException
-
createLink
protected void createLink(int id, String existingPath, String linkPath, boolean symLink) throws IOException - Specified by:
createLink
in classAbstractSftpSubsystemHelper
- Throws:
IOException
-
doTextSeek
- Specified by:
doTextSeek
in classAbstractSftpSubsystemHelper
- Throws:
IOException
-
doOpenSSHFsync
- Specified by:
doOpenSSHFsync
in classAbstractSftpSubsystemHelper
- Throws:
IOException
-
doCheckFileHash
protected void doCheckFileHash(int id, String targetType, String target, Collection<String> algos, long startOffset, long length, int blockSize, Buffer buffer) throws Exception - Specified by:
doCheckFileHash
in classAbstractSftpSubsystemHelper
- Throws:
Exception
-
doMD5Hash
protected byte[] doMD5Hash(int id, String targetType, String target, long startOffset, long length, byte[] quickCheckHash) throws Exception - Specified by:
doMD5Hash
in classAbstractSftpSubsystemHelper
- Throws:
Exception
-
doVersionSelect
- Specified by:
doVersionSelect
in classAbstractSftpSubsystemHelper
- Throws:
IOException
-
doBlock
protected void doBlock(int id, String handle, long offset, long length, int mask) throws IOException - Specified by:
doBlock
in classAbstractSftpSubsystemHelper
- Throws:
IOException
-
doUnblock
- Specified by:
doUnblock
in classAbstractSftpSubsystemHelper
- Throws:
IOException
-
doCopyData
protected void doCopyData(int id, String readHandle, long readOffset, long readLength, String writeHandle, long writeOffset) throws IOException - Specified by:
doCopyData
in classAbstractSftpSubsystemHelper
- Throws:
IOException
-
doReadDir
- Specified by:
doReadDir
in classAbstractSftpSubsystemHelper
- Throws:
IOException
-
doOpenDir
- Specified by:
doOpenDir
in classAbstractSftpSubsystemHelper
- Throws:
IOException
-
doFSetStat
- Specified by:
doFSetStat
in classAbstractSftpSubsystemHelper
- Throws:
IOException
-
doFStat
- Specified by:
doFStat
in classAbstractSftpSubsystemHelper
- Throws:
IOException
-
doWrite
protected void doWrite(int id, String handle, long offset, int length, byte[] data, int doff, int remaining) throws IOException - Specified by:
doWrite
in classAbstractSftpSubsystemHelper
- Throws:
IOException
-
doRead
protected int doRead(int id, String handle, long offset, int length, byte[] data, int doff) throws IOException - Specified by:
doRead
in classAbstractSftpSubsystemHelper
- Throws:
IOException
-
doClose
- Specified by:
doClose
in classAbstractSftpSubsystemHelper
- Throws:
IOException
-
doOpen
protected String doOpen(int id, String path, int pflags, int access, Map<String, Object> attrs) throws IOException- Specified by:
doOpen
in classAbstractSftpSubsystemHelper
- Parameters:
id
- Request idpath
- Pathpflags
- Open mode flags - seeSSH_FXF_XXX
flagsaccess
- Access mode flags - seeACE4_XXX
flagsattrs
- Requested attributes- Returns:
- The assigned (opaque) handle
- Throws:
IOException
- if failed to execute
-
generateFileHandle
- Throws:
IOException
-
doInit
- Specified by:
doInit
in classAbstractSftpSubsystemHelper
- Throws:
IOException
-
prepareReply
- Specified by:
prepareReply
in classAbstractSftpSubsystemHelper
-
send
- Specified by:
send
in classAbstractSftpSubsystemHelper
- Throws:
IOException
-
destroy
Description copied from interface:CommandLifecycle
This method is called by the SSH server to destroy the command because the client has disconnected somehow.- Specified by:
destroy
in interfaceCommandLifecycle
- Parameters:
channel
- TheChannelSession
through which the command has been received
-
closeAllHandles
protected void closeAllHandles()
-