2019-07-20 10:26:52 +00:00
|
|
|
package sftpd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"os"
|
2020-01-05 10:41:25 +00:00
|
|
|
"path"
|
2019-07-20 10:26:52 +00:00
|
|
|
"time"
|
|
|
|
|
2020-05-06 17:36:34 +00:00
|
|
|
"github.com/pkg/sftp"
|
2019-09-11 14:29:56 +00:00
|
|
|
"golang.org/x/crypto/ssh"
|
2019-07-20 10:26:52 +00:00
|
|
|
|
2020-07-24 21:39:38 +00:00
|
|
|
"github.com/drakkan/sftpgo/common"
|
2019-07-20 10:26:52 +00:00
|
|
|
"github.com/drakkan/sftpgo/dataprovider"
|
|
|
|
"github.com/drakkan/sftpgo/logger"
|
2020-05-06 17:36:34 +00:00
|
|
|
"github.com/drakkan/sftpgo/vfs"
|
2019-07-20 10:26:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Connection details for an authenticated user
|
|
|
|
type Connection struct {
|
2020-07-24 21:39:38 +00:00
|
|
|
*common.BaseConnection
|
2019-07-30 18:51:29 +00:00
|
|
|
// client's version string
|
2019-07-20 10:26:52 +00:00
|
|
|
ClientVersion string
|
2019-07-30 18:51:29 +00:00
|
|
|
// Remote address for this connection
|
|
|
|
RemoteAddr net.Addr
|
2020-07-24 21:39:38 +00:00
|
|
|
netConn net.Conn
|
|
|
|
channel ssh.Channel
|
|
|
|
command string
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
2020-07-24 21:39:38 +00:00
|
|
|
// GetClientVersion returns the connected client's version
|
|
|
|
func (c *Connection) GetClientVersion() string {
|
|
|
|
return c.ClientVersion
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetRemoteAddress return the connected client's address
|
|
|
|
func (c *Connection) GetRemoteAddress() string {
|
|
|
|
return c.RemoteAddr.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetConnDeadline sets a deadline on the network connection so it will be eventually closed
|
|
|
|
func (c *Connection) SetConnDeadline() {
|
|
|
|
c.netConn.SetDeadline(time.Now().Add(2 * time.Minute)) //nolint:errcheck
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetCommand returns the SSH command, if any
|
|
|
|
func (c *Connection) GetCommand() string {
|
|
|
|
return c.command
|
2019-09-06 09:23:06 +00:00
|
|
|
}
|
|
|
|
|
2019-07-20 10:26:52 +00:00
|
|
|
// Fileread creates a reader for a file on the system and returns the reader back.
|
2020-07-24 21:39:38 +00:00
|
|
|
func (c *Connection) Fileread(request *sftp.Request) (io.ReaderAt, error) {
|
|
|
|
c.UpdateLastActivity()
|
2019-07-20 10:26:52 +00:00
|
|
|
|
2020-01-05 10:41:25 +00:00
|
|
|
if !c.User.HasPerm(dataprovider.PermDownload, path.Dir(request.Filepath)) {
|
|
|
|
return nil, sftp.ErrSSHFxPermissionDenied
|
|
|
|
}
|
|
|
|
|
2020-03-01 21:10:29 +00:00
|
|
|
if !c.User.IsFileAllowed(request.Filepath) {
|
2020-07-24 21:39:38 +00:00
|
|
|
c.Log(logger.LevelWarn, "reading file %#v is not allowed", request.Filepath)
|
2020-03-01 21:10:29 +00:00
|
|
|
return nil, sftp.ErrSSHFxPermissionDenied
|
|
|
|
}
|
|
|
|
|
2020-07-24 21:39:38 +00:00
|
|
|
p, err := c.Fs.ResolvePath(request.Filepath)
|
2019-07-20 10:26:52 +00:00
|
|
|
if err != nil {
|
2020-07-24 21:39:38 +00:00
|
|
|
return nil, c.GetFsError(err)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
2020-08-03 16:03:09 +00:00
|
|
|
file, r, cancelFn, err := c.Fs.Open(p, 0)
|
2019-07-20 10:26:52 +00:00
|
|
|
if err != nil {
|
2020-07-24 21:39:38 +00:00
|
|
|
c.Log(logger.LevelWarn, "could not open file %#v for reading: %+v", p, err)
|
|
|
|
return nil, c.GetFsError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
baseTransfer := common.NewBaseTransfer(file, c.BaseConnection, cancelFn, p, request.Filepath, common.TransferDownload,
|
2020-08-22 08:12:00 +00:00
|
|
|
0, 0, 0, false, c.Fs)
|
2020-08-31 04:45:22 +00:00
|
|
|
t := newTransfer(baseTransfer, nil, r, nil)
|
2020-07-24 21:39:38 +00:00
|
|
|
|
|
|
|
return t, nil
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
2020-08-31 04:45:22 +00:00
|
|
|
// OpenFile implements OpenFileWriter interface
|
|
|
|
func (c *Connection) OpenFile(request *sftp.Request) (sftp.WriterAtReaderAt, error) {
|
|
|
|
return c.handleFilewrite(request)
|
|
|
|
}
|
|
|
|
|
2019-07-20 10:26:52 +00:00
|
|
|
// Filewrite handles the write actions for a file on the system.
|
2020-07-24 21:39:38 +00:00
|
|
|
func (c *Connection) Filewrite(request *sftp.Request) (io.WriterAt, error) {
|
2020-08-31 04:45:22 +00:00
|
|
|
return c.handleFilewrite(request)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Connection) handleFilewrite(request *sftp.Request) (sftp.WriterAtReaderAt, error) {
|
2020-07-24 21:39:38 +00:00
|
|
|
c.UpdateLastActivity()
|
2020-03-01 21:10:29 +00:00
|
|
|
|
|
|
|
if !c.User.IsFileAllowed(request.Filepath) {
|
2020-07-24 21:39:38 +00:00
|
|
|
c.Log(logger.LevelWarn, "writing file %#v is not allowed", request.Filepath)
|
2020-03-01 21:10:29 +00:00
|
|
|
return nil, sftp.ErrSSHFxPermissionDenied
|
|
|
|
}
|
|
|
|
|
2020-07-24 21:39:38 +00:00
|
|
|
p, err := c.Fs.ResolvePath(request.Filepath)
|
2019-07-20 10:26:52 +00:00
|
|
|
if err != nil {
|
2020-07-24 21:39:38 +00:00
|
|
|
return nil, c.GetFsError(err)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
2019-08-04 07:37:58 +00:00
|
|
|
filePath := p
|
2020-07-24 21:39:38 +00:00
|
|
|
if common.Config.IsAtomicUploadEnabled() && c.Fs.IsAtomicUploadSupported() {
|
|
|
|
filePath = c.Fs.GetAtomicUploadPath(p)
|
2019-08-04 07:37:58 +00:00
|
|
|
}
|
|
|
|
|
2020-08-31 04:45:22 +00:00
|
|
|
var errForRead error
|
|
|
|
if !vfs.IsLocalOsFs(c.Fs) && request.Pflags().Read {
|
|
|
|
// read and write mode is only supported for local filesystem
|
|
|
|
errForRead = sftp.ErrSSHFxOpUnsupported
|
|
|
|
}
|
|
|
|
if !c.User.HasPerm(dataprovider.PermDownload, path.Dir(request.Filepath)) {
|
|
|
|
// we can try to read only for local fs here, see above.
|
|
|
|
// os.ErrPermission will become sftp.ErrSSHFxPermissionDenied when sent to
|
|
|
|
// the client
|
|
|
|
errForRead = os.ErrPermission
|
|
|
|
}
|
|
|
|
|
2020-07-24 21:39:38 +00:00
|
|
|
stat, statErr := c.Fs.Lstat(p)
|
|
|
|
if (statErr == nil && stat.Mode()&os.ModeSymlink == os.ModeSymlink) || c.Fs.IsNotExist(statErr) {
|
2020-01-05 10:41:25 +00:00
|
|
|
if !c.User.HasPerm(dataprovider.PermUpload, path.Dir(request.Filepath)) {
|
2019-12-25 17:20:19 +00:00
|
|
|
return nil, sftp.ErrSSHFxPermissionDenied
|
|
|
|
}
|
2020-08-31 04:45:22 +00:00
|
|
|
return c.handleSFTPUploadToNewFile(p, filePath, request.Filepath, errForRead)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if statErr != nil {
|
2020-07-24 21:39:38 +00:00
|
|
|
c.Log(logger.LevelError, "error performing file stat %#v: %+v", p, statErr)
|
|
|
|
return nil, c.GetFsError(statErr)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
2019-08-04 07:37:58 +00:00
|
|
|
// This happen if we upload a file that has the same name of an existing directory
|
2019-07-20 10:26:52 +00:00
|
|
|
if stat.IsDir() {
|
2020-07-24 21:39:38 +00:00
|
|
|
c.Log(logger.LevelWarn, "attempted to open a directory for writing to: %#v", p)
|
2019-10-14 20:44:57 +00:00
|
|
|
return nil, sftp.ErrSSHFxOpUnsupported
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
2020-01-05 10:41:25 +00:00
|
|
|
if !c.User.HasPerm(dataprovider.PermOverwrite, path.Dir(request.Filepath)) {
|
2019-10-14 20:44:57 +00:00
|
|
|
return nil, sftp.ErrSSHFxPermissionDenied
|
2019-09-17 06:53:45 +00:00
|
|
|
}
|
|
|
|
|
2020-08-31 04:45:22 +00:00
|
|
|
return c.handleSFTPUploadToExistingFile(request.Pflags(), p, filePath, stat.Size(), request.Filepath, errForRead)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Filecmd hander for basic SFTP system calls related to files, but not anything to do with reading
|
|
|
|
// or writing to those files.
|
2020-07-24 21:39:38 +00:00
|
|
|
func (c *Connection) Filecmd(request *sftp.Request) error {
|
|
|
|
c.UpdateLastActivity()
|
2019-07-20 10:26:52 +00:00
|
|
|
|
2020-07-24 21:39:38 +00:00
|
|
|
p, err := c.Fs.ResolvePath(request.Filepath)
|
2019-07-20 10:26:52 +00:00
|
|
|
if err != nil {
|
2020-07-24 21:39:38 +00:00
|
|
|
return c.GetFsError(err)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
2019-07-20 22:19:17 +00:00
|
|
|
target, err := c.getSFTPCmdTargetPath(request.Target)
|
|
|
|
if err != nil {
|
2020-07-24 21:39:38 +00:00
|
|
|
return c.GetFsError(err)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
2020-07-24 21:39:38 +00:00
|
|
|
c.Log(logger.LevelDebug, "new cmd, method: %v, sourcePath: %#v, targetPath: %#v", request.Method, p, target)
|
2019-07-20 10:26:52 +00:00
|
|
|
|
|
|
|
switch request.Method {
|
|
|
|
case "Setstat":
|
2019-11-15 11:15:07 +00:00
|
|
|
return c.handleSFTPSetstat(p, request)
|
2019-07-20 10:26:52 +00:00
|
|
|
case "Rename":
|
2020-07-24 21:39:38 +00:00
|
|
|
if err = c.Rename(p, target, request.Filepath, request.Target); err != nil {
|
2019-07-20 22:19:17 +00:00
|
|
|
return err
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
case "Rmdir":
|
2020-07-24 21:39:38 +00:00
|
|
|
return c.RemoveDir(p, request.Filepath)
|
2019-07-20 10:26:52 +00:00
|
|
|
case "Mkdir":
|
2020-07-24 21:39:38 +00:00
|
|
|
err = c.CreateDir(p, request.Filepath)
|
2019-07-20 10:26:52 +00:00
|
|
|
if err != nil {
|
2019-07-20 22:19:17 +00:00
|
|
|
return err
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
case "Symlink":
|
2020-07-24 21:39:38 +00:00
|
|
|
if err = c.CreateSymlink(p, target, request.Filepath, request.Target); err != nil {
|
2019-07-20 22:19:17 +00:00
|
|
|
return err
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
case "Remove":
|
2020-01-05 10:41:25 +00:00
|
|
|
return c.handleSFTPRemove(p, request)
|
2019-07-20 10:26:52 +00:00
|
|
|
default:
|
2019-10-14 20:44:57 +00:00
|
|
|
return sftp.ErrSSHFxOpUnsupported
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
2019-10-14 20:44:57 +00:00
|
|
|
return sftp.ErrSSHFxOk
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Filelist is the handler for SFTP filesystem list calls. This will handle calls to list the contents of
|
|
|
|
// a directory as well as perform file/folder stat calls.
|
2020-07-24 21:39:38 +00:00
|
|
|
func (c *Connection) Filelist(request *sftp.Request) (sftp.ListerAt, error) {
|
|
|
|
c.UpdateLastActivity()
|
|
|
|
p, err := c.Fs.ResolvePath(request.Filepath)
|
2019-07-20 10:26:52 +00:00
|
|
|
if err != nil {
|
2020-07-24 21:39:38 +00:00
|
|
|
return nil, c.GetFsError(err)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch request.Method {
|
|
|
|
case "List":
|
2020-07-24 21:39:38 +00:00
|
|
|
files, err := c.ListDir(p, request.Filepath)
|
2019-11-15 11:15:07 +00:00
|
|
|
if err != nil {
|
2020-07-24 21:39:38 +00:00
|
|
|
return nil, err
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
2020-07-24 21:39:38 +00:00
|
|
|
return listerAt(files), nil
|
2019-07-20 10:26:52 +00:00
|
|
|
case "Stat":
|
2020-01-05 10:41:25 +00:00
|
|
|
if !c.User.HasPerm(dataprovider.PermListItems, path.Dir(request.Filepath)) {
|
2019-10-14 20:44:57 +00:00
|
|
|
return nil, sftp.ErrSSHFxPermissionDenied
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
2020-08-25 16:23:00 +00:00
|
|
|
s, err := c.DoStat(p, 0)
|
2019-11-15 11:15:07 +00:00
|
|
|
if err != nil {
|
2020-08-25 16:23:00 +00:00
|
|
|
c.Log(logger.LevelDebug, "error running stat on path %#v: %+v", p, err)
|
2020-07-24 21:39:38 +00:00
|
|
|
return nil, c.GetFsError(err)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
2019-07-30 18:51:29 +00:00
|
|
|
return listerAt([]os.FileInfo{s}), nil
|
2020-08-22 12:52:17 +00:00
|
|
|
case "Readlink":
|
|
|
|
if !c.User.HasPerm(dataprovider.PermListItems, path.Dir(request.Filepath)) {
|
|
|
|
return nil, sftp.ErrSSHFxPermissionDenied
|
|
|
|
}
|
|
|
|
|
|
|
|
s, err := c.Fs.Readlink(p)
|
|
|
|
if err != nil {
|
2020-08-25 16:23:00 +00:00
|
|
|
c.Log(logger.LevelDebug, "error running readlink on path %#v: %+v", p, err)
|
2020-08-22 12:52:17 +00:00
|
|
|
return nil, c.GetFsError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !c.User.HasPerm(dataprovider.PermListItems, path.Dir(s)) {
|
|
|
|
return nil, sftp.ErrSSHFxPermissionDenied
|
|
|
|
}
|
|
|
|
|
|
|
|
return listerAt([]os.FileInfo{vfs.NewFileInfo(s, false, 0, time.Now(), true)}), nil
|
|
|
|
|
2019-07-20 10:26:52 +00:00
|
|
|
default:
|
2019-10-14 20:44:57 +00:00
|
|
|
return nil, sftp.ErrSSHFxOpUnsupported
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-11 07:30:25 +00:00
|
|
|
// Lstat implements LstatFileLister interface
|
|
|
|
func (c *Connection) Lstat(request *sftp.Request) (sftp.ListerAt, error) {
|
|
|
|
c.UpdateLastActivity()
|
|
|
|
|
|
|
|
p, err := c.Fs.ResolvePath(request.Filepath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, c.GetFsError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !c.User.HasPerm(dataprovider.PermListItems, path.Dir(request.Filepath)) {
|
|
|
|
return nil, sftp.ErrSSHFxPermissionDenied
|
|
|
|
}
|
|
|
|
|
|
|
|
s, err := c.DoStat(p, 1)
|
|
|
|
if err != nil {
|
|
|
|
c.Log(logger.LevelDebug, "error running lstat on path %#v: %+v", p, err)
|
|
|
|
return nil, c.GetFsError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return listerAt([]os.FileInfo{s}), nil
|
|
|
|
}
|
|
|
|
|
2020-07-24 21:39:38 +00:00
|
|
|
func (c *Connection) getSFTPCmdTargetPath(requestTarget string) (string, error) {
|
2019-07-20 22:19:17 +00:00
|
|
|
var target string
|
|
|
|
// If a target is provided in this request validate that it is going to the correct
|
2019-11-15 11:15:07 +00:00
|
|
|
// location for the server. If it is not, return an error
|
|
|
|
if len(requestTarget) > 0 {
|
2019-07-20 22:19:17 +00:00
|
|
|
var err error
|
2020-07-24 21:39:38 +00:00
|
|
|
target, err = c.Fs.ResolvePath(requestTarget)
|
2019-07-20 22:19:17 +00:00
|
|
|
if err != nil {
|
2020-07-24 21:39:38 +00:00
|
|
|
return target, err
|
2019-07-20 22:19:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return target, nil
|
|
|
|
}
|
|
|
|
|
2020-07-24 21:39:38 +00:00
|
|
|
func (c *Connection) handleSFTPSetstat(filePath string, request *sftp.Request) error {
|
|
|
|
attrs := common.StatAttributes{
|
|
|
|
Flags: 0,
|
2019-12-25 17:20:19 +00:00
|
|
|
}
|
2020-07-24 21:39:38 +00:00
|
|
|
if request.AttrFlags().Permissions {
|
|
|
|
attrs.Flags |= common.StatAttrPerms
|
|
|
|
attrs.Mode = request.Attributes().FileMode()
|
2020-06-07 21:30:18 +00:00
|
|
|
}
|
2020-07-24 21:39:38 +00:00
|
|
|
if request.AttrFlags().UidGid {
|
|
|
|
attrs.Flags |= common.StatAttrUIDGID
|
|
|
|
attrs.UID = int(request.Attributes().UID)
|
|
|
|
attrs.GID = int(request.Attributes().GID)
|
2020-06-10 07:11:32 +00:00
|
|
|
}
|
2020-07-24 21:39:38 +00:00
|
|
|
if request.AttrFlags().Acmodtime {
|
|
|
|
attrs.Flags |= common.StatAttrTimes
|
|
|
|
attrs.Atime = time.Unix(int64(request.Attributes().Atime), 0)
|
|
|
|
attrs.Mtime = time.Unix(int64(request.Attributes().Mtime), 0)
|
2019-07-20 22:19:17 +00:00
|
|
|
}
|
2020-08-20 11:54:36 +00:00
|
|
|
if request.AttrFlags().Size {
|
|
|
|
attrs.Flags |= common.StatAttrSize
|
|
|
|
attrs.Size = int64(request.Attributes().Size)
|
|
|
|
}
|
2019-07-20 22:19:17 +00:00
|
|
|
|
2020-07-24 21:39:38 +00:00
|
|
|
return c.SetStat(filePath, request.Filepath, &attrs)
|
2019-07-20 22:19:17 +00:00
|
|
|
}
|
|
|
|
|
2020-07-24 21:39:38 +00:00
|
|
|
func (c *Connection) handleSFTPRemove(filePath string, request *sftp.Request) error {
|
2019-07-20 22:19:17 +00:00
|
|
|
var fi os.FileInfo
|
|
|
|
var err error
|
2020-07-24 21:39:38 +00:00
|
|
|
if fi, err = c.Fs.Lstat(filePath); err != nil {
|
|
|
|
c.Log(logger.LevelWarn, "failed to remove a file %#v: stat error: %+v", filePath, err)
|
|
|
|
return c.GetFsError(err)
|
2019-07-20 22:19:17 +00:00
|
|
|
}
|
2019-10-16 05:48:22 +00:00
|
|
|
if fi.IsDir() && fi.Mode()&os.ModeSymlink != os.ModeSymlink {
|
2020-07-24 21:39:38 +00:00
|
|
|
c.Log(logger.LevelDebug, "cannot remove %#v is not a file/symlink", filePath)
|
2019-10-16 05:48:22 +00:00
|
|
|
return sftp.ErrSSHFxFailure
|
|
|
|
}
|
2020-03-01 21:10:29 +00:00
|
|
|
|
2020-07-24 21:39:38 +00:00
|
|
|
return c.RemoveFile(filePath, request.Filepath, fi)
|
2019-07-20 22:19:17 +00:00
|
|
|
}
|
|
|
|
|
2020-08-31 04:45:22 +00:00
|
|
|
func (c *Connection) handleSFTPUploadToNewFile(resolvedPath, filePath, requestPath string, errForRead error) (sftp.WriterAtReaderAt, error) {
|
2020-07-24 21:39:38 +00:00
|
|
|
quotaResult := c.HasSpace(true, requestPath)
|
2020-06-16 20:49:18 +00:00
|
|
|
if !quotaResult.HasSpace {
|
2020-07-24 21:39:38 +00:00
|
|
|
c.Log(logger.LevelInfo, "denying file write due to quota limits")
|
2019-10-14 20:44:57 +00:00
|
|
|
return nil, sftp.ErrSSHFxFailure
|
2019-08-04 07:37:58 +00:00
|
|
|
}
|
|
|
|
|
2020-07-24 21:39:38 +00:00
|
|
|
file, w, cancelFn, err := c.Fs.Create(filePath, 0)
|
2019-08-04 07:37:58 +00:00
|
|
|
if err != nil {
|
2020-07-24 21:39:38 +00:00
|
|
|
c.Log(logger.LevelWarn, "error creating file %#v: %+v", resolvedPath, err)
|
|
|
|
return nil, c.GetFsError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
vfs.SetPathPermissions(c.Fs, filePath, c.User.GetUID(), c.User.GetGID())
|
|
|
|
|
2020-08-16 18:17:02 +00:00
|
|
|
// we can get an error only for resume
|
|
|
|
maxWriteSize, _ := c.GetMaxWriteSize(quotaResult, false, 0)
|
|
|
|
|
2020-07-24 21:39:38 +00:00
|
|
|
baseTransfer := common.NewBaseTransfer(file, c.BaseConnection, cancelFn, resolvedPath, requestPath,
|
2020-08-22 08:12:00 +00:00
|
|
|
common.TransferUpload, 0, 0, maxWriteSize, true, c.Fs)
|
2020-08-31 04:45:22 +00:00
|
|
|
t := newTransfer(baseTransfer, w, nil, errForRead)
|
2020-07-24 21:39:38 +00:00
|
|
|
|
|
|
|
return t, nil
|
2019-08-04 07:37:58 +00:00
|
|
|
}
|
|
|
|
|
2020-07-24 21:39:38 +00:00
|
|
|
func (c *Connection) handleSFTPUploadToExistingFile(pflags sftp.FileOpenFlags, resolvedPath, filePath string,
|
2020-08-31 04:45:22 +00:00
|
|
|
fileSize int64, requestPath string, errForRead error) (sftp.WriterAtReaderAt, error) {
|
2019-08-04 07:37:58 +00:00
|
|
|
var err error
|
2020-07-24 21:39:38 +00:00
|
|
|
quotaResult := c.HasSpace(false, requestPath)
|
2020-06-16 20:49:18 +00:00
|
|
|
if !quotaResult.HasSpace {
|
2020-07-24 21:39:38 +00:00
|
|
|
c.Log(logger.LevelInfo, "denying file write due to quota limits")
|
2019-10-14 20:44:57 +00:00
|
|
|
return nil, sftp.ErrSSHFxFailure
|
2019-08-04 07:37:58 +00:00
|
|
|
}
|
|
|
|
|
2019-10-09 15:33:30 +00:00
|
|
|
minWriteOffset := int64(0)
|
2019-08-04 09:02:38 +00:00
|
|
|
osFlags := getOSOpenFlags(pflags)
|
2020-08-22 08:12:00 +00:00
|
|
|
isTruncate := osFlags&os.O_TRUNC != 0
|
|
|
|
isResume := pflags.Append && !isTruncate
|
2019-08-04 07:37:58 +00:00
|
|
|
|
2020-08-22 08:12:00 +00:00
|
|
|
// if there is a size limit the remaining size cannot be 0 here, since quotaResult.HasSpace
|
|
|
|
// will return false in this case and we deny the upload before.
|
|
|
|
// For Cloud FS GetMaxWriteSize will return unsupported operation
|
2020-08-16 18:17:02 +00:00
|
|
|
maxWriteSize, err := c.GetMaxWriteSize(quotaResult, isResume, fileSize)
|
|
|
|
if err != nil {
|
|
|
|
c.Log(logger.LevelDebug, "unable to get max write size: %v", err)
|
|
|
|
return nil, err
|
2020-01-19 06:41:05 +00:00
|
|
|
}
|
|
|
|
|
2020-07-24 21:39:38 +00:00
|
|
|
if common.Config.IsAtomicUploadEnabled() && c.Fs.IsAtomicUploadSupported() {
|
|
|
|
err = c.Fs.Rename(resolvedPath, filePath)
|
2019-08-04 07:37:58 +00:00
|
|
|
if err != nil {
|
2020-07-24 21:39:38 +00:00
|
|
|
c.Log(logger.LevelWarn, "error renaming existing file for atomic upload, source: %#v, dest: %#v, err: %+v",
|
2020-06-07 21:30:18 +00:00
|
|
|
resolvedPath, filePath, err)
|
2020-07-24 21:39:38 +00:00
|
|
|
return nil, c.GetFsError(err)
|
2019-08-04 07:37:58 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-19 06:41:05 +00:00
|
|
|
|
2020-07-24 21:39:38 +00:00
|
|
|
file, w, cancelFn, err := c.Fs.Create(filePath, osFlags)
|
2019-08-04 07:37:58 +00:00
|
|
|
if err != nil {
|
2020-07-24 21:39:38 +00:00
|
|
|
c.Log(logger.LevelWarn, "error opening existing file, flags: %v, source: %#v, err: %+v", pflags, filePath, err)
|
|
|
|
return nil, c.GetFsError(err)
|
2019-08-04 07:37:58 +00:00
|
|
|
}
|
|
|
|
|
2020-01-23 09:19:56 +00:00
|
|
|
initialSize := int64(0)
|
2020-08-16 18:17:02 +00:00
|
|
|
if isResume {
|
|
|
|
c.Log(logger.LevelDebug, "upload resume requested, file path %#v initial size: %v", filePath, fileSize)
|
2019-10-09 15:33:30 +00:00
|
|
|
minWriteOffset = fileSize
|
2020-08-22 08:12:00 +00:00
|
|
|
initialSize = fileSize
|
2019-10-09 15:33:30 +00:00
|
|
|
} else {
|
2020-08-22 08:12:00 +00:00
|
|
|
if vfs.IsLocalOsFs(c.Fs) && isTruncate {
|
2020-06-15 21:32:12 +00:00
|
|
|
vfolder, err := c.User.GetVirtualFolderForPath(path.Dir(requestPath))
|
2020-06-07 21:30:18 +00:00
|
|
|
if err == nil {
|
2020-07-08 17:59:31 +00:00
|
|
|
dataprovider.UpdateVirtualFolderQuota(vfolder.BaseVirtualFolder, 0, -fileSize, false) //nolint:errcheck
|
2020-06-07 21:30:18 +00:00
|
|
|
if vfolder.IsIncludedInUserQuota() {
|
2020-07-08 17:59:31 +00:00
|
|
|
dataprovider.UpdateUserQuota(c.User, 0, -fileSize, false) //nolint:errcheck
|
2020-06-07 21:30:18 +00:00
|
|
|
}
|
|
|
|
} else {
|
2020-07-08 17:59:31 +00:00
|
|
|
dataprovider.UpdateUserQuota(c.User, 0, -fileSize, false) //nolint:errcheck
|
2020-05-01 13:27:53 +00:00
|
|
|
}
|
2020-01-23 09:19:56 +00:00
|
|
|
} else {
|
|
|
|
initialSize = fileSize
|
|
|
|
}
|
2020-08-22 08:12:00 +00:00
|
|
|
if maxWriteSize > 0 && isTruncate {
|
2020-06-18 20:38:03 +00:00
|
|
|
maxWriteSize += fileSize
|
|
|
|
}
|
2019-10-09 15:33:30 +00:00
|
|
|
}
|
2019-08-04 07:37:58 +00:00
|
|
|
|
2020-07-24 21:39:38 +00:00
|
|
|
vfs.SetPathPermissions(c.Fs, filePath, c.User.GetUID(), c.User.GetGID())
|
2020-06-07 21:30:18 +00:00
|
|
|
|
2020-07-24 21:39:38 +00:00
|
|
|
baseTransfer := common.NewBaseTransfer(file, c.BaseConnection, cancelFn, resolvedPath, requestPath,
|
2020-08-22 08:12:00 +00:00
|
|
|
common.TransferUpload, minWriteOffset, initialSize, maxWriteSize, false, c.Fs)
|
2020-08-31 04:45:22 +00:00
|
|
|
t := newTransfer(baseTransfer, w, nil, errForRead)
|
2020-06-16 20:49:18 +00:00
|
|
|
|
2020-07-24 21:39:38 +00:00
|
|
|
return t, nil
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
2020-07-24 21:39:38 +00:00
|
|
|
// Disconnect disconnects the client closing the network connection
|
|
|
|
func (c *Connection) Disconnect() error {
|
2019-09-11 14:29:56 +00:00
|
|
|
if c.channel != nil {
|
|
|
|
err := c.channel.Close()
|
2020-07-24 21:39:38 +00:00
|
|
|
c.Log(logger.LevelInfo, "channel close, err: %v", err)
|
2019-09-11 14:29:56 +00:00
|
|
|
}
|
2019-09-11 10:46:21 +00:00
|
|
|
return c.netConn.Close()
|
|
|
|
}
|
|
|
|
|
2019-08-04 09:02:38 +00:00
|
|
|
func getOSOpenFlags(requestFlags sftp.FileOpenFlags) (flags int) {
|
2019-07-20 22:19:17 +00:00
|
|
|
var osFlags int
|
|
|
|
if requestFlags.Read && requestFlags.Write {
|
|
|
|
osFlags |= os.O_RDWR
|
|
|
|
} else if requestFlags.Write {
|
|
|
|
osFlags |= os.O_WRONLY
|
|
|
|
}
|
2019-10-09 15:33:30 +00:00
|
|
|
// we ignore Append flag since pkg/sftp use WriteAt that cannot work with os.O_APPEND
|
|
|
|
/*if requestFlags.Append {
|
2019-07-20 22:19:17 +00:00
|
|
|
osFlags |= os.O_APPEND
|
2019-10-09 15:33:30 +00:00
|
|
|
}*/
|
2019-07-20 22:19:17 +00:00
|
|
|
if requestFlags.Creat {
|
|
|
|
osFlags |= os.O_CREATE
|
|
|
|
}
|
|
|
|
if requestFlags.Trunc {
|
|
|
|
osFlags |= os.O_TRUNC
|
|
|
|
}
|
|
|
|
if requestFlags.Excl {
|
|
|
|
osFlags |= os.O_EXCL
|
|
|
|
}
|
2019-08-04 09:02:38 +00:00
|
|
|
return osFlags
|
2019-07-20 22:19:17 +00:00
|
|
|
}
|