2020-07-29 19:56:56 +00:00
|
|
|
package ftpd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2020-12-12 09:31:09 +00:00
|
|
|
"io"
|
2020-07-29 19:56:56 +00:00
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
ftpserver "github.com/fclairamb/ftpserverlib"
|
|
|
|
"github.com/spf13/afero"
|
|
|
|
|
|
|
|
"github.com/drakkan/sftpgo/common"
|
|
|
|
"github.com/drakkan/sftpgo/dataprovider"
|
|
|
|
"github.com/drakkan/sftpgo/logger"
|
|
|
|
"github.com/drakkan/sftpgo/vfs"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2021-03-21 18:15:47 +00:00
|
|
|
errNotImplemented = errors.New("not implemented")
|
2020-12-24 17:48:06 +00:00
|
|
|
errCOMBNotSupported = errors.New("COMB is not supported for this filesystem")
|
2020-07-29 19:56:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Connection details for an FTP connection.
|
|
|
|
// It implements common.ActiveConnection and ftpserver.ClientDriver interfaces
|
|
|
|
type Connection struct {
|
|
|
|
*common.BaseConnection
|
|
|
|
clientContext ftpserver.ClientContext
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetClientVersion returns the connected client's version.
|
|
|
|
// It returns "Unknown" if the client does not advertise its
|
|
|
|
// version
|
|
|
|
func (c *Connection) GetClientVersion() string {
|
|
|
|
version := c.clientContext.GetClientVersion()
|
|
|
|
if len(version) > 0 {
|
|
|
|
return version
|
|
|
|
}
|
|
|
|
return "Unknown"
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetRemoteAddress return the connected client's address
|
|
|
|
func (c *Connection) GetRemoteAddress() string {
|
|
|
|
return c.clientContext.RemoteAddr().String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Disconnect disconnects the client
|
|
|
|
func (c *Connection) Disconnect() error {
|
2020-12-24 17:48:06 +00:00
|
|
|
return c.clientContext.Close()
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|
|
|
|
|
2020-12-25 10:14:08 +00:00
|
|
|
// GetCommand returns the last received FTP command
|
2020-07-29 19:56:56 +00:00
|
|
|
func (c *Connection) GetCommand() string {
|
2020-12-24 17:48:06 +00:00
|
|
|
return c.clientContext.GetLastCommand()
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create is not implemented we use ClientDriverExtentionFileTransfer
|
|
|
|
func (c *Connection) Create(name string) (afero.File, error) {
|
|
|
|
return nil, errNotImplemented
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mkdir creates a directory using the connection filesystem
|
|
|
|
func (c *Connection) Mkdir(name string, perm os.FileMode) error {
|
|
|
|
c.UpdateLastActivity()
|
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
return c.CreateDir(name)
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MkdirAll is not implemented, we don't need it
|
|
|
|
func (c *Connection) MkdirAll(path string, perm os.FileMode) error {
|
|
|
|
return errNotImplemented
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open is not implemented we use ClientDriverExtentionFileTransfer and ClientDriverExtensionFileList
|
|
|
|
func (c *Connection) Open(name string) (afero.File, error) {
|
|
|
|
return nil, errNotImplemented
|
|
|
|
}
|
|
|
|
|
|
|
|
// OpenFile is not implemented we use ClientDriverExtentionFileTransfer
|
|
|
|
func (c *Connection) OpenFile(name string, flag int, perm os.FileMode) (afero.File, error) {
|
|
|
|
return nil, errNotImplemented
|
|
|
|
}
|
|
|
|
|
2020-08-03 15:36:43 +00:00
|
|
|
// Remove removes a file.
|
|
|
|
// We implements ClientDriverExtensionRemoveDir for directories
|
2020-07-29 19:56:56 +00:00
|
|
|
func (c *Connection) Remove(name string) error {
|
|
|
|
c.UpdateLastActivity()
|
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
fs, p, err := c.GetFsAndResolvedPath(name)
|
2020-07-29 19:56:56 +00:00
|
|
|
if err != nil {
|
2021-03-21 18:15:47 +00:00
|
|
|
return err
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var fi os.FileInfo
|
2021-03-21 18:15:47 +00:00
|
|
|
if fi, err = fs.Lstat(p); err != nil {
|
2020-07-29 19:56:56 +00:00
|
|
|
c.Log(logger.LevelWarn, "failed to remove a file %#v: stat error: %+v", p, err)
|
2021-03-21 18:15:47 +00:00
|
|
|
return c.GetFsError(fs, err)
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 18:36:39 +00:00
|
|
|
if fi.IsDir() && fi.Mode()&os.ModeSymlink == 0 {
|
2020-08-03 15:36:43 +00:00
|
|
|
c.Log(logger.LevelDebug, "cannot remove %#v is not a file/symlink", p)
|
2020-09-19 08:14:30 +00:00
|
|
|
return c.GetGenericError(nil)
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
return c.RemoveFile(fs, p, name, fi)
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveAll is not implemented, we don't need it
|
|
|
|
func (c *Connection) RemoveAll(path string) error {
|
|
|
|
return errNotImplemented
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rename renames a file or a directory
|
|
|
|
func (c *Connection) Rename(oldname, newname string) error {
|
|
|
|
c.UpdateLastActivity()
|
|
|
|
|
2021-05-26 05:48:37 +00:00
|
|
|
return c.BaseConnection.Rename(oldname, newname)
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stat returns a FileInfo describing the named file/directory, or an error,
|
|
|
|
// if any happens
|
|
|
|
func (c *Connection) Stat(name string) (os.FileInfo, error) {
|
|
|
|
c.UpdateLastActivity()
|
|
|
|
|
|
|
|
if !c.User.HasPerm(dataprovider.PermListItems, path.Dir(name)) {
|
|
|
|
return nil, c.GetPermissionDeniedError()
|
|
|
|
}
|
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
fi, err := c.DoStat(name, 0)
|
2020-07-29 19:56:56 +00:00
|
|
|
if err != nil {
|
2021-03-21 18:15:47 +00:00
|
|
|
c.Log(logger.LevelDebug, "error running stat on path %#v: %+v", name, err)
|
|
|
|
return nil, err
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|
|
|
|
return fi, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name returns the name of this connection
|
|
|
|
func (c *Connection) Name() string {
|
|
|
|
return c.GetID()
|
|
|
|
}
|
|
|
|
|
2020-12-17 12:40:36 +00:00
|
|
|
// Chown changes the uid and gid of the named file
|
|
|
|
func (c *Connection) Chown(name string, uid, gid int) error {
|
|
|
|
c.UpdateLastActivity()
|
|
|
|
|
|
|
|
return common.ErrOpUnsupported
|
|
|
|
/*p, err := c.Fs.ResolvePath(name)
|
|
|
|
if err != nil {
|
|
|
|
return c.GetFsError(err)
|
|
|
|
}
|
|
|
|
attrs := common.StatAttributes{
|
|
|
|
Flags: common.StatAttrUIDGID,
|
|
|
|
UID: uid,
|
|
|
|
GID: gid,
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.SetStat(p, name, &attrs)*/
|
|
|
|
}
|
|
|
|
|
2020-07-29 19:56:56 +00:00
|
|
|
// Chmod changes the mode of the named file/directory
|
|
|
|
func (c *Connection) Chmod(name string, mode os.FileMode) error {
|
|
|
|
c.UpdateLastActivity()
|
|
|
|
|
|
|
|
attrs := common.StatAttributes{
|
|
|
|
Flags: common.StatAttrPerms,
|
|
|
|
Mode: mode,
|
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
return c.SetStat(name, &attrs)
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Chtimes changes the access and modification times of the named file
|
|
|
|
func (c *Connection) Chtimes(name string, atime time.Time, mtime time.Time) error {
|
|
|
|
c.UpdateLastActivity()
|
|
|
|
|
|
|
|
attrs := common.StatAttributes{
|
|
|
|
Flags: common.StatAttrTimes,
|
|
|
|
Atime: atime,
|
|
|
|
Mtime: mtime,
|
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
return c.SetStat(name, &attrs)
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|
|
|
|
|
2020-12-25 10:14:08 +00:00
|
|
|
// GetAvailableSpace implements ClientDriverExtensionAvailableSpace interface
|
|
|
|
func (c *Connection) GetAvailableSpace(dirName string) (int64, error) {
|
|
|
|
c.UpdateLastActivity()
|
|
|
|
|
2021-02-11 18:45:52 +00:00
|
|
|
quotaResult := c.HasSpace(false, false, path.Join(dirName, "fakefile.txt"))
|
2020-12-25 10:14:08 +00:00
|
|
|
if !quotaResult.HasSpace {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if quotaResult.AllowedSize == 0 {
|
|
|
|
// no quota restrictions
|
|
|
|
if c.User.Filters.MaxUploadFileSize > 0 {
|
|
|
|
return c.User.Filters.MaxUploadFileSize, nil
|
|
|
|
}
|
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
fs, p, err := c.GetFsAndResolvedPath(dirName)
|
2020-12-25 10:14:08 +00:00
|
|
|
if err != nil {
|
2021-03-21 18:15:47 +00:00
|
|
|
return 0, err
|
2020-12-25 10:14:08 +00:00
|
|
|
}
|
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
statVFS, err := fs.GetAvailableDiskSize(p)
|
2021-02-11 18:45:52 +00:00
|
|
|
if err != nil {
|
2021-03-21 18:15:47 +00:00
|
|
|
return 0, c.GetFsError(fs, err)
|
2021-02-11 18:45:52 +00:00
|
|
|
}
|
|
|
|
return int64(statVFS.FreeSpace()), nil
|
2020-12-25 10:14:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// the available space is the minimum between MaxUploadFileSize, if setted,
|
|
|
|
// and quota allowed size
|
|
|
|
if c.User.Filters.MaxUploadFileSize > 0 {
|
|
|
|
if c.User.Filters.MaxUploadFileSize < quotaResult.AllowedSize {
|
|
|
|
return c.User.Filters.MaxUploadFileSize, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return quotaResult.AllowedSize, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AllocateSpace implements ClientDriverExtensionAllocate interface
|
2020-07-29 19:56:56 +00:00
|
|
|
func (c *Connection) AllocateSpace(size int) error {
|
|
|
|
c.UpdateLastActivity()
|
2020-08-16 18:17:02 +00:00
|
|
|
// check the max allowed file size first
|
|
|
|
if c.User.Filters.MaxUploadFileSize > 0 && int64(size) > c.User.Filters.MaxUploadFileSize {
|
|
|
|
return common.ErrQuotaExceeded
|
|
|
|
}
|
|
|
|
|
2020-07-29 19:56:56 +00:00
|
|
|
// we don't have a path here so we check home dir and any virtual folders
|
|
|
|
// we return no error if there is space in any folder
|
|
|
|
folders := []string{"/"}
|
|
|
|
for _, v := range c.User.VirtualFolders {
|
|
|
|
// the space is checked for the parent folder
|
|
|
|
folders = append(folders, path.Join(v.VirtualPath, "fakefile.txt"))
|
|
|
|
}
|
|
|
|
for _, f := range folders {
|
2021-02-11 18:45:52 +00:00
|
|
|
quotaResult := c.HasSpace(false, false, f)
|
2020-07-29 19:56:56 +00:00
|
|
|
if quotaResult.HasSpace {
|
|
|
|
if quotaResult.QuotaSize == 0 {
|
|
|
|
// unlimited size is allowed
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if quotaResult.GetRemainingSize() > int64(size) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return common.ErrQuotaExceeded
|
|
|
|
}
|
|
|
|
|
2020-08-03 15:36:43 +00:00
|
|
|
// RemoveDir implements ClientDriverExtensionRemoveDir
|
|
|
|
func (c *Connection) RemoveDir(name string) error {
|
|
|
|
c.UpdateLastActivity()
|
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
return c.BaseConnection.RemoveDir(name)
|
2020-08-03 15:36:43 +00:00
|
|
|
}
|
|
|
|
|
2020-08-15 11:02:25 +00:00
|
|
|
// Symlink implements ClientDriverExtensionSymlink
|
|
|
|
func (c *Connection) Symlink(oldname, newname string) error {
|
|
|
|
c.UpdateLastActivity()
|
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
return c.BaseConnection.CreateSymlink(oldname, newname)
|
2020-08-15 11:02:25 +00:00
|
|
|
}
|
|
|
|
|
2020-07-29 19:56:56 +00:00
|
|
|
// ReadDir implements ClientDriverExtensionFilelist
|
|
|
|
func (c *Connection) ReadDir(name string) ([]os.FileInfo, error) {
|
|
|
|
c.UpdateLastActivity()
|
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
return c.ListDir(name)
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetHandle implements ClientDriverExtentionFileTransfer
|
2020-08-03 16:03:09 +00:00
|
|
|
func (c *Connection) GetHandle(name string, flags int, offset int64) (ftpserver.FileTransfer, error) {
|
2020-07-29 19:56:56 +00:00
|
|
|
c.UpdateLastActivity()
|
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
fs, p, err := c.GetFsAndResolvedPath(name)
|
2020-07-29 19:56:56 +00:00
|
|
|
if err != nil {
|
2021-03-21 18:15:47 +00:00
|
|
|
return nil, err
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|
2020-12-24 17:48:06 +00:00
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
if c.GetCommand() == "COMB" && !vfs.IsLocalOsFs(fs) {
|
2020-12-24 17:48:06 +00:00
|
|
|
return nil, errCOMBNotSupported
|
|
|
|
}
|
|
|
|
|
2020-07-29 19:56:56 +00:00
|
|
|
if flags&os.O_WRONLY != 0 {
|
2021-03-21 18:15:47 +00:00
|
|
|
return c.uploadFile(fs, p, name, flags)
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
return c.downloadFile(fs, p, name, offset)
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
func (c *Connection) downloadFile(fs vfs.Fs, fsPath, ftpPath string, offset int64) (ftpserver.FileTransfer, error) {
|
2020-07-29 19:56:56 +00:00
|
|
|
if !c.User.HasPerm(dataprovider.PermDownload, path.Dir(ftpPath)) {
|
|
|
|
return nil, c.GetPermissionDeniedError()
|
|
|
|
}
|
|
|
|
|
|
|
|
if !c.User.IsFileAllowed(ftpPath) {
|
|
|
|
c.Log(logger.LevelWarn, "reading file %#v is not allowed", ftpPath)
|
|
|
|
return nil, c.GetPermissionDeniedError()
|
|
|
|
}
|
|
|
|
|
2021-05-31 20:33:23 +00:00
|
|
|
if err := common.ExecutePreAction(&c.User, common.OperationPreDownload, fsPath, ftpPath, c.GetProtocol(), 0, 0); err != nil {
|
2021-05-26 05:48:37 +00:00
|
|
|
c.Log(logger.LevelDebug, "download for file %#v denied by pre action: %v", ftpPath, err)
|
|
|
|
return nil, c.GetPermissionDeniedError()
|
|
|
|
}
|
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
file, r, cancelFn, err := fs.Open(fsPath, offset)
|
2020-07-29 19:56:56 +00:00
|
|
|
if err != nil {
|
|
|
|
c.Log(logger.LevelWarn, "could not open file %#v for reading: %+v", fsPath, err)
|
2021-03-21 18:15:47 +00:00
|
|
|
return nil, c.GetFsError(fs, err)
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|
|
|
|
|
2021-05-31 19:45:29 +00:00
|
|
|
baseTransfer := common.NewBaseTransfer(file, c.BaseConnection, cancelFn, fsPath, fsPath, ftpPath, common.TransferDownload,
|
2021-03-21 18:15:47 +00:00
|
|
|
0, 0, 0, false, fs)
|
2020-08-22 08:12:00 +00:00
|
|
|
t := newTransfer(baseTransfer, nil, r, offset)
|
2020-07-29 19:56:56 +00:00
|
|
|
|
|
|
|
return t, nil
|
|
|
|
}
|
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
func (c *Connection) uploadFile(fs vfs.Fs, fsPath, ftpPath string, flags int) (ftpserver.FileTransfer, error) {
|
2020-07-29 19:56:56 +00:00
|
|
|
if !c.User.IsFileAllowed(ftpPath) {
|
|
|
|
c.Log(logger.LevelWarn, "writing file %#v is not allowed", ftpPath)
|
|
|
|
return nil, c.GetPermissionDeniedError()
|
|
|
|
}
|
|
|
|
|
|
|
|
filePath := fsPath
|
2021-03-21 18:15:47 +00:00
|
|
|
if common.Config.IsAtomicUploadEnabled() && fs.IsAtomicUploadSupported() {
|
|
|
|
filePath = fs.GetAtomicUploadPath(fsPath)
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
stat, statErr := fs.Lstat(fsPath)
|
|
|
|
if (statErr == nil && stat.Mode()&os.ModeSymlink != 0) || fs.IsNotExist(statErr) {
|
2020-07-29 19:56:56 +00:00
|
|
|
if !c.User.HasPerm(dataprovider.PermUpload, path.Dir(ftpPath)) {
|
|
|
|
return nil, c.GetPermissionDeniedError()
|
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
return c.handleFTPUploadToNewFile(fs, fsPath, filePath, ftpPath)
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if statErr != nil {
|
|
|
|
c.Log(logger.LevelError, "error performing file stat %#v: %+v", fsPath, statErr)
|
2021-03-21 18:15:47 +00:00
|
|
|
return nil, c.GetFsError(fs, statErr)
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// This happen if we upload a file that has the same name of an existing directory
|
|
|
|
if stat.IsDir() {
|
|
|
|
c.Log(logger.LevelWarn, "attempted to open a directory for writing to: %#v", fsPath)
|
|
|
|
return nil, c.GetOpUnsupportedError()
|
|
|
|
}
|
|
|
|
|
|
|
|
if !c.User.HasPerm(dataprovider.PermOverwrite, path.Dir(ftpPath)) {
|
|
|
|
return nil, c.GetPermissionDeniedError()
|
|
|
|
}
|
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
return c.handleFTPUploadToExistingFile(fs, flags, fsPath, filePath, stat.Size(), ftpPath)
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
func (c *Connection) handleFTPUploadToNewFile(fs vfs.Fs, resolvedPath, filePath, requestPath string) (ftpserver.FileTransfer, error) {
|
2021-02-11 18:45:52 +00:00
|
|
|
quotaResult := c.HasSpace(true, false, requestPath)
|
2020-07-29 19:56:56 +00:00
|
|
|
if !quotaResult.HasSpace {
|
|
|
|
c.Log(logger.LevelInfo, "denying file write due to quota limits")
|
|
|
|
return nil, common.ErrQuotaExceeded
|
|
|
|
}
|
2021-05-31 20:33:23 +00:00
|
|
|
if err := common.ExecutePreAction(&c.User, common.OperationPreUpload, resolvedPath, requestPath, c.GetProtocol(), 0, 0); err != nil {
|
2021-05-26 05:48:37 +00:00
|
|
|
c.Log(logger.LevelDebug, "upload for file %#v denied by pre action: %v", requestPath, err)
|
|
|
|
return nil, c.GetPermissionDeniedError()
|
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
file, w, cancelFn, err := fs.Create(filePath, 0)
|
2020-07-29 19:56:56 +00:00
|
|
|
if err != nil {
|
|
|
|
c.Log(logger.LevelWarn, "error creating file %#v: %+v", resolvedPath, err)
|
2021-03-21 18:15:47 +00:00
|
|
|
return nil, c.GetFsError(fs, err)
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
vfs.SetPathPermissions(fs, filePath, c.User.GetUID(), c.User.GetGID())
|
2020-07-29 19:56:56 +00:00
|
|
|
|
2020-08-16 18:17:02 +00:00
|
|
|
// we can get an error only for resume
|
2021-03-21 18:15:47 +00:00
|
|
|
maxWriteSize, _ := c.GetMaxWriteSize(quotaResult, false, 0, fs.IsUploadResumeSupported())
|
2020-08-16 18:17:02 +00:00
|
|
|
|
2021-05-31 19:45:29 +00:00
|
|
|
baseTransfer := common.NewBaseTransfer(file, c.BaseConnection, cancelFn, resolvedPath, filePath, requestPath,
|
2021-03-21 18:15:47 +00:00
|
|
|
common.TransferUpload, 0, 0, maxWriteSize, true, fs)
|
2020-08-22 08:12:00 +00:00
|
|
|
t := newTransfer(baseTransfer, w, nil, 0)
|
2020-07-29 19:56:56 +00:00
|
|
|
|
|
|
|
return t, nil
|
|
|
|
}
|
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
func (c *Connection) handleFTPUploadToExistingFile(fs vfs.Fs, flags int, resolvedPath, filePath string, fileSize int64,
|
2020-07-29 19:56:56 +00:00
|
|
|
requestPath string) (ftpserver.FileTransfer, error) {
|
|
|
|
var err error
|
2021-02-11 18:45:52 +00:00
|
|
|
quotaResult := c.HasSpace(false, false, requestPath)
|
2020-07-29 19:56:56 +00:00
|
|
|
if !quotaResult.HasSpace {
|
|
|
|
c.Log(logger.LevelInfo, "denying file write due to quota limits")
|
|
|
|
return nil, common.ErrQuotaExceeded
|
|
|
|
}
|
|
|
|
minWriteOffset := int64(0)
|
2020-12-24 17:48:06 +00:00
|
|
|
// ftpserverlib sets:
|
|
|
|
// - os.O_WRONLY | os.O_APPEND for APPE and COMB
|
|
|
|
// - os.O_WRONLY | os.O_CREATE for REST.
|
|
|
|
// - os.O_WRONLY | os.O_CREATE | os.O_TRUNC if the command is not APPE and REST = 0
|
|
|
|
// so if we don't have O_TRUNC is a resume.
|
2020-12-05 12:48:13 +00:00
|
|
|
isResume := flags&os.O_TRUNC == 0
|
2020-08-16 18:17:02 +00:00
|
|
|
// if there is a size limit remaining size cannot be 0 here, since quotaResult.HasSpace
|
|
|
|
// will return false in this case and we deny the upload before
|
2021-03-21 18:15:47 +00:00
|
|
|
maxWriteSize, err := c.GetMaxWriteSize(quotaResult, isResume, fileSize, fs.IsUploadResumeSupported())
|
2020-08-16 18:17:02 +00:00
|
|
|
if err != nil {
|
|
|
|
c.Log(logger.LevelDebug, "unable to get max write size: %v", err)
|
|
|
|
return nil, err
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|
2021-05-31 20:40:47 +00:00
|
|
|
if err := common.ExecutePreAction(&c.User, common.OperationPreUpload, resolvedPath, requestPath, c.GetProtocol(), fileSize, flags); err != nil {
|
|
|
|
c.Log(logger.LevelDebug, "upload for file %#v denied by pre action: %v", requestPath, err)
|
|
|
|
return nil, c.GetPermissionDeniedError()
|
|
|
|
}
|
2020-07-29 19:56:56 +00:00
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
if common.Config.IsAtomicUploadEnabled() && fs.IsAtomicUploadSupported() {
|
|
|
|
err = fs.Rename(resolvedPath, filePath)
|
2020-07-29 19:56:56 +00:00
|
|
|
if err != nil {
|
|
|
|
c.Log(logger.LevelWarn, "error renaming existing file for atomic upload, source: %#v, dest: %#v, err: %+v",
|
|
|
|
resolvedPath, filePath, err)
|
2021-03-21 18:15:47 +00:00
|
|
|
return nil, c.GetFsError(fs, err)
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
file, w, cancelFn, err := fs.Create(filePath, flags)
|
2020-07-29 19:56:56 +00:00
|
|
|
if err != nil {
|
|
|
|
c.Log(logger.LevelWarn, "error opening existing file, flags: %v, source: %#v, err: %+v", flags, filePath, err)
|
2021-03-21 18:15:47 +00:00
|
|
|
return nil, c.GetFsError(fs, err)
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
initialSize := int64(0)
|
2020-08-16 18:17:02 +00:00
|
|
|
if isResume {
|
2021-04-03 14:00:55 +00:00
|
|
|
c.Log(logger.LevelDebug, "resuming upload requested, file path: %#v initial size: %v", filePath, fileSize)
|
2020-07-29 19:56:56 +00:00
|
|
|
minWriteOffset = fileSize
|
2020-08-22 08:12:00 +00:00
|
|
|
initialSize = fileSize
|
2021-04-03 14:00:55 +00:00
|
|
|
if vfs.IsSFTPFs(fs) && fs.IsUploadResumeSupported() {
|
2020-12-12 09:31:09 +00:00
|
|
|
// we need this since we don't allow resume with wrong offset, we should fix this in pkg/sftp
|
2021-04-03 14:00:55 +00:00
|
|
|
file.Seek(initialSize, io.SeekStart) //nolint:errcheck // for sftp seek simply set the offset
|
2020-12-12 09:31:09 +00:00
|
|
|
}
|
2020-07-29 19:56:56 +00:00
|
|
|
} else {
|
2021-03-21 18:15:47 +00:00
|
|
|
if vfs.IsLocalOrSFTPFs(fs) {
|
2020-07-29 19:56:56 +00:00
|
|
|
vfolder, err := c.User.GetVirtualFolderForPath(path.Dir(requestPath))
|
|
|
|
if err == nil {
|
2021-02-16 18:11:36 +00:00
|
|
|
dataprovider.UpdateVirtualFolderQuota(&vfolder.BaseVirtualFolder, 0, -fileSize, false) //nolint:errcheck
|
2020-07-29 19:56:56 +00:00
|
|
|
if vfolder.IsIncludedInUserQuota() {
|
2021-02-16 18:11:36 +00:00
|
|
|
dataprovider.UpdateUserQuota(&c.User, 0, -fileSize, false) //nolint:errcheck
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|
|
|
|
} else {
|
2021-02-16 18:11:36 +00:00
|
|
|
dataprovider.UpdateUserQuota(&c.User, 0, -fileSize, false) //nolint:errcheck
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
initialSize = fileSize
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
vfs.SetPathPermissions(fs, filePath, c.User.GetUID(), c.User.GetGID())
|
2020-07-29 19:56:56 +00:00
|
|
|
|
2021-05-31 19:45:29 +00:00
|
|
|
baseTransfer := common.NewBaseTransfer(file, c.BaseConnection, cancelFn, resolvedPath, filePath, requestPath,
|
2021-03-21 18:15:47 +00:00
|
|
|
common.TransferUpload, minWriteOffset, initialSize, maxWriteSize, false, fs)
|
2020-08-22 08:12:00 +00:00
|
|
|
t := newTransfer(baseTransfer, w, nil, 0)
|
2020-07-29 19:56:56 +00:00
|
|
|
|
|
|
|
return t, nil
|
|
|
|
}
|