2024-01-01 10:31:45 +00:00
|
|
|
// Copyright (C) 2019 Nicola Murino
|
2022-07-17 18:16:00 +00:00
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published
|
|
|
|
// by the Free Software Foundation, version 3.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
2023-01-03 09:18:30 +00:00
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2022-07-17 18:16:00 +00:00
|
|
|
|
2020-07-29 19:56:56 +00:00
|
|
|
package ftpd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2021-06-28 17:40:04 +00:00
|
|
|
"fmt"
|
2020-12-12 09:31:09 +00:00
|
|
|
"io"
|
2020-07-29 19:56:56 +00:00
|
|
|
"os"
|
|
|
|
"path"
|
2022-05-04 17:32:02 +00:00
|
|
|
"strings"
|
2020-07-29 19:56:56 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
ftpserver "github.com/fclairamb/ftpserverlib"
|
|
|
|
"github.com/spf13/afero"
|
|
|
|
|
2022-07-24 14:18:54 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/internal/common"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/dataprovider"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/logger"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/vfs"
|
2020-07-29 19:56:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
2022-07-12 10:04:01 +00:00
|
|
|
clientContext ftpserver.ClientContext
|
|
|
|
doWildcardListDir bool
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|
|
|
|
|
2021-08-10 11:07:38 +00:00
|
|
|
func (c *Connection) getFTPMode() string {
|
|
|
|
if c.clientContext == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
switch c.clientContext.GetLastDataChannel() {
|
|
|
|
case ftpserver.DataChannelActive:
|
|
|
|
return "active"
|
|
|
|
case ftpserver.DataChannelPassive:
|
|
|
|
return "passive"
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2020-07-29 19:56:56 +00:00
|
|
|
// 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"
|
|
|
|
}
|
|
|
|
|
2021-07-24 18:11:17 +00:00
|
|
|
// GetLocalAddress returns local connection address
|
|
|
|
func (c *Connection) GetLocalAddress() string {
|
|
|
|
return c.clientContext.LocalAddr().String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetRemoteAddress returns the connected client's address
|
2020-07-29 19:56:56 +00:00
|
|
|
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
|
2023-03-22 18:02:54 +00:00
|
|
|
func (c *Connection) Create(_ string) (afero.File, error) {
|
2020-07-29 19:56:56 +00:00
|
|
|
return nil, errNotImplemented
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mkdir creates a directory using the connection filesystem
|
2023-03-22 18:02:54 +00:00
|
|
|
func (c *Connection) Mkdir(name string, _ os.FileMode) error {
|
2020-07-29 19:56:56 +00:00
|
|
|
c.UpdateLastActivity()
|
|
|
|
|
2022-01-15 16:16:49 +00:00
|
|
|
return c.CreateDir(name, true)
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MkdirAll is not implemented, we don't need it
|
2023-03-22 18:02:54 +00:00
|
|
|
func (c *Connection) MkdirAll(_ string, _ os.FileMode) error {
|
2020-07-29 19:56:56 +00:00
|
|
|
return errNotImplemented
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open is not implemented we use ClientDriverExtentionFileTransfer and ClientDriverExtensionFileList
|
2023-03-22 18:02:54 +00:00
|
|
|
func (c *Connection) Open(_ string) (afero.File, error) {
|
2020-07-29 19:56:56 +00:00
|
|
|
return nil, errNotImplemented
|
|
|
|
}
|
|
|
|
|
|
|
|
// OpenFile is not implemented we use ClientDriverExtentionFileTransfer
|
2023-03-22 18:02:54 +00:00
|
|
|
func (c *Connection) OpenFile(_ string, _ int, _ os.FileMode) (afero.File, error) {
|
2020-07-29 19:56:56 +00:00
|
|
|
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 {
|
2023-02-27 18:02:43 +00:00
|
|
|
c.Log(logger.LevelError, "failed to remove file %q: 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 {
|
2023-02-27 18:02:43 +00:00
|
|
|
c.Log(logger.LevelError, "cannot remove %q 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
|
2023-03-22 18:02:54 +00:00
|
|
|
func (c *Connection) RemoveAll(_ string) error {
|
2020-07-29 19:56:56 +00:00
|
|
|
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()
|
2022-07-12 10:04:01 +00:00
|
|
|
c.doWildcardListDir = false
|
2020-07-29 19:56:56 +00:00
|
|
|
|
|
|
|
if !c.User.HasPerm(dataprovider.PermListItems, path.Dir(name)) {
|
|
|
|
return nil, c.GetPermissionDeniedError()
|
|
|
|
}
|
|
|
|
|
2022-01-15 16:16:49 +00:00
|
|
|
fi, err := c.DoStat(name, 0, true)
|
2020-07-29 19:56:56 +00:00
|
|
|
if err != nil {
|
2022-07-12 10:04:01 +00:00
|
|
|
if c.isListDirWithWildcards(path.Base(name)) {
|
|
|
|
c.doWildcardListDir = true
|
2022-08-16 15:59:13 +00:00
|
|
|
return vfs.NewFileInfo(name, true, 0, time.Unix(0, 0), false), nil
|
2022-05-04 17:32:02 +00:00
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
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
|
2023-03-22 18:02:54 +00:00
|
|
|
func (c *Connection) Chown(_ string, _, _ int) error {
|
2020-12-17 12:40:36 +00:00
|
|
|
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()
|
|
|
|
|
2022-01-30 10:42:36 +00:00
|
|
|
diskQuota, transferQuota := c.HasSpace(false, false, path.Join(dirName, "fakefile.txt"))
|
|
|
|
if !diskQuota.HasSpace || !transferQuota.HasUploadSpace() {
|
2020-12-25 10:14:08 +00:00
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
2022-01-30 10:42:36 +00:00
|
|
|
if diskQuota.AllowedSize == 0 && transferQuota.AllowedULSize == 0 && transferQuota.AllowedTotalSize == 0 {
|
2020-12-25 10:14:08 +00:00
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2022-01-30 10:42:36 +00:00
|
|
|
allowedDiskSize := diskQuota.AllowedSize
|
|
|
|
allowedUploadSize := transferQuota.AllowedULSize
|
|
|
|
if transferQuota.AllowedTotalSize > 0 {
|
|
|
|
allowedUploadSize = transferQuota.AllowedTotalSize
|
|
|
|
}
|
|
|
|
allowedSize := allowedDiskSize
|
|
|
|
if allowedSize == 0 {
|
|
|
|
allowedSize = allowedUploadSize
|
|
|
|
} else {
|
|
|
|
if allowedUploadSize > 0 && allowedUploadSize < allowedSize {
|
|
|
|
allowedSize = allowedUploadSize
|
|
|
|
}
|
|
|
|
}
|
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 {
|
2022-01-30 10:42:36 +00:00
|
|
|
if c.User.Filters.MaxUploadFileSize < allowedSize {
|
2020-12-25 10:14:08 +00:00
|
|
|
return c.User.Filters.MaxUploadFileSize, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-30 10:42:36 +00:00
|
|
|
return allowedSize, nil
|
2020-12-25 10:14:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AllocateSpace implements ClientDriverExtensionAllocate interface
|
2023-03-22 18:02:54 +00:00
|
|
|
func (c *Connection) AllocateSpace(_ int) error {
|
2020-07-29 19:56:56 +00:00
|
|
|
c.UpdateLastActivity()
|
2022-01-30 10:42:36 +00:00
|
|
|
// we treat ALLO as NOOP see RFC 959
|
|
|
|
return nil
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|
|
|
|
|
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
|
2024-02-15 19:53:56 +00:00
|
|
|
func (c *Connection) ReadDir(name string) (ftpserver.DirLister, error) {
|
2020-07-29 19:56:56 +00:00
|
|
|
c.UpdateLastActivity()
|
|
|
|
|
2022-07-12 10:04:01 +00:00
|
|
|
if c.doWildcardListDir {
|
|
|
|
c.doWildcardListDir = false
|
2022-05-04 17:32:02 +00:00
|
|
|
baseName := path.Base(name)
|
2022-07-12 10:04:01 +00:00
|
|
|
// we only support wildcards for the last path level, for example:
|
|
|
|
// - *.xml is supported
|
|
|
|
// - dir*/*.xml is not supported
|
2022-09-18 11:08:48 +00:00
|
|
|
name = path.Dir(name)
|
|
|
|
c.clientContext.SetListPath(name)
|
2024-02-15 19:53:56 +00:00
|
|
|
lister, err := c.ListDir(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &patternDirLister{
|
|
|
|
DirLister: lister,
|
|
|
|
pattern: baseName,
|
|
|
|
lastCommand: c.clientContext.GetLastCommand(),
|
|
|
|
dirName: name,
|
|
|
|
connectionPath: c.clientContext.Path(),
|
|
|
|
}, nil
|
2021-07-31 07:42:23 +00:00
|
|
|
}
|
2022-07-12 10:04:01 +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()
|
|
|
|
}
|
2022-01-30 10:42:36 +00:00
|
|
|
transferQuota := c.GetTransferQuota()
|
|
|
|
if !transferQuota.HasDownloadSpace() {
|
|
|
|
c.Log(logger.LevelInfo, "denying file read due to quota limits")
|
|
|
|
return nil, c.GetReadQuotaExceededError()
|
|
|
|
}
|
2020-07-29 19:56:56 +00:00
|
|
|
|
2022-01-15 16:16:49 +00:00
|
|
|
if ok, policy := c.User.IsFileAllowed(ftpPath); !ok {
|
2023-02-27 18:02:43 +00:00
|
|
|
c.Log(logger.LevelWarn, "reading file %q is not allowed", ftpPath)
|
2022-01-15 16:16:49 +00:00
|
|
|
return nil, c.GetErrorForDeniedFile(policy)
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|
|
|
|
|
2023-01-02 14:59:00 +00:00
|
|
|
if _, err := common.ExecutePreAction(c.BaseConnection, common.OperationPreDownload, fsPath, ftpPath, 0, 0); err != nil {
|
2023-02-27 18:02:43 +00:00
|
|
|
c.Log(logger.LevelDebug, "download for file %q denied by pre action: %v", ftpPath, err)
|
2021-05-26 05:48:37 +00:00
|
|
|
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 {
|
2023-02-27 18:02:43 +00:00
|
|
|
c.Log(logger.LevelError, "could not open file %q 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
|
|
|
}
|
|
|
|
|
2022-01-20 17:19:20 +00:00
|
|
|
baseTransfer := common.NewBaseTransfer(file, c.BaseConnection, cancelFn, fsPath, fsPath, ftpPath,
|
2022-01-30 10:42:36 +00:00
|
|
|
common.TransferDownload, 0, 0, 0, 0, false, fs, transferQuota)
|
2021-08-10 11:07:38 +00:00
|
|
|
baseTransfer.SetFtpMode(c.getFTPMode())
|
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) {
|
2022-01-15 16:16:49 +00:00
|
|
|
if ok, _ := c.User.IsFileAllowed(ftpPath); !ok {
|
2023-02-27 18:02:43 +00:00
|
|
|
c.Log(logger.LevelWarn, "writing file %q is not allowed", ftpPath)
|
2021-06-28 17:40:04 +00:00
|
|
|
return nil, ftpserver.ErrFileNameNotAllowed
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)) {
|
2021-06-28 17:40:04 +00:00
|
|
|
return nil, fmt.Errorf("%w, no upload permission", ftpserver.ErrFileNameNotAllowed)
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|
2022-02-17 17:22:27 +00:00
|
|
|
return c.handleFTPUploadToNewFile(fs, flags, fsPath, filePath, ftpPath)
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if statErr != nil {
|
2023-02-27 18:02:43 +00:00
|
|
|
c.Log(logger.LevelError, "error performing file stat %q: %+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() {
|
2023-02-27 18:02:43 +00:00
|
|
|
c.Log(logger.LevelError, "attempted to open a directory for writing to: %q", fsPath)
|
2020-07-29 19:56:56 +00:00
|
|
|
return nil, c.GetOpUnsupportedError()
|
|
|
|
}
|
|
|
|
|
|
|
|
if !c.User.HasPerm(dataprovider.PermOverwrite, path.Dir(ftpPath)) {
|
2021-06-28 17:40:04 +00:00
|
|
|
return nil, fmt.Errorf("%w, no overwrite permission", ftpserver.ErrFileNameNotAllowed)
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-02-17 17:22:27 +00:00
|
|
|
func (c *Connection) handleFTPUploadToNewFile(fs vfs.Fs, flags int, resolvedPath, filePath, requestPath string) (ftpserver.FileTransfer, error) {
|
2022-01-30 10:42:36 +00:00
|
|
|
diskQuota, transferQuota := c.HasSpace(true, false, requestPath)
|
|
|
|
if !diskQuota.HasSpace || !transferQuota.HasUploadSpace() {
|
2020-07-29 19:56:56 +00:00
|
|
|
c.Log(logger.LevelInfo, "denying file write due to quota limits")
|
2021-06-28 17:40:04 +00:00
|
|
|
return nil, ftpserver.ErrStorageExceeded
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|
2023-01-02 14:59:00 +00:00
|
|
|
if _, err := common.ExecutePreAction(c.BaseConnection, common.OperationPreUpload, resolvedPath, requestPath, 0, 0); err != nil {
|
2023-02-27 18:02:43 +00:00
|
|
|
c.Log(logger.LevelDebug, "upload for file %q denied by pre action: %v", requestPath, err)
|
2023-02-28 17:01:09 +00:00
|
|
|
return nil, ftpserver.ErrFileNameNotAllowed
|
2021-05-26 05:48:37 +00:00
|
|
|
}
|
2023-10-22 14:09:30 +00:00
|
|
|
file, w, cancelFn, err := fs.Create(filePath, flags, c.GetCreateChecks(requestPath, true, false))
|
2020-07-29 19:56:56 +00:00
|
|
|
if err != nil {
|
2023-02-27 18:02:43 +00:00
|
|
|
c.Log(logger.LevelError, "error creating file %q, flags %v: %+v", resolvedPath, flags, 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
|
2022-01-30 10:42:36 +00:00
|
|
|
maxWriteSize, _ := c.GetMaxWriteSize(diskQuota, 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,
|
2022-01-30 10:42:36 +00:00
|
|
|
common.TransferUpload, 0, 0, maxWriteSize, 0, true, fs, transferQuota)
|
2021-08-10 11:07:38 +00:00
|
|
|
baseTransfer.SetFtpMode(c.getFTPMode())
|
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
|
2022-01-30 10:42:36 +00:00
|
|
|
diskQuota, transferQuota := c.HasSpace(false, false, requestPath)
|
|
|
|
if !diskQuota.HasSpace || !transferQuota.HasUploadSpace() {
|
2020-07-29 19:56:56 +00:00
|
|
|
c.Log(logger.LevelInfo, "denying file write due to quota limits")
|
2021-06-28 17:40:04 +00:00
|
|
|
return nil, ftpserver.ErrStorageExceeded
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|
|
|
|
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
|
2023-10-22 14:09:30 +00:00
|
|
|
maxWriteSize, err := c.GetMaxWriteSize(diskQuota, isResume, fileSize, vfs.IsUploadResumeSupported(fs, fileSize))
|
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
|
|
|
}
|
2023-01-02 14:59:00 +00:00
|
|
|
if _, err := common.ExecutePreAction(c.BaseConnection, common.OperationPreUpload, resolvedPath, requestPath, fileSize, flags); err != nil {
|
2023-02-27 18:02:43 +00:00
|
|
|
c.Log(logger.LevelDebug, "upload for file %q denied by pre action: %v", requestPath, err)
|
2023-02-28 17:01:09 +00:00
|
|
|
return nil, ftpserver.ErrFileNameNotAllowed
|
2021-05-31 20:40:47 +00:00
|
|
|
}
|
2020-07-29 19:56:56 +00:00
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
if common.Config.IsAtomicUploadEnabled() && fs.IsAtomicUploadSupported() {
|
2023-01-06 11:33:50 +00:00
|
|
|
_, _, err = fs.Rename(resolvedPath, filePath)
|
2020-07-29 19:56:56 +00:00
|
|
|
if err != nil {
|
2023-02-27 18:02:43 +00:00
|
|
|
c.Log(logger.LevelError, "error renaming existing file for atomic upload, source: %q, dest: %q, err: %+v",
|
2020-07-29 19:56:56 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-22 14:09:30 +00:00
|
|
|
file, w, cancelFn, err := fs.Create(filePath, flags, c.GetCreateChecks(requestPath, false, isResume))
|
2020-07-29 19:56:56 +00:00
|
|
|
if err != nil {
|
2023-02-27 18:02:43 +00:00
|
|
|
c.Log(logger.LevelError, "error opening existing file, flags: %v, source: %q, 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)
|
2022-01-20 17:19:20 +00:00
|
|
|
truncatedSize := int64(0) // bytes truncated and not included in quota
|
2020-08-16 18:17:02 +00:00
|
|
|
if isResume {
|
2023-02-27 18:02:43 +00:00
|
|
|
c.Log(logger.LevelDebug, "resuming upload requested, file path: %q 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 {
|
2022-06-11 08:41:34 +00:00
|
|
|
if vfs.HasTruncateSupport(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
|
2022-01-20 17:19:20 +00:00
|
|
|
truncatedSize = fileSize
|
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
|
|
|
|
2021-05-31 19:45:29 +00:00
|
|
|
baseTransfer := common.NewBaseTransfer(file, c.BaseConnection, cancelFn, resolvedPath, filePath, requestPath,
|
2022-01-30 10:42:36 +00:00
|
|
|
common.TransferUpload, minWriteOffset, initialSize, maxWriteSize, truncatedSize, false, fs, transferQuota)
|
2021-08-10 11:07:38 +00:00
|
|
|
baseTransfer.SetFtpMode(c.getFTPMode())
|
2023-10-24 17:14:33 +00:00
|
|
|
t := newTransfer(baseTransfer, w, nil, minWriteOffset)
|
2020-07-29 19:56:56 +00:00
|
|
|
|
|
|
|
return t, nil
|
|
|
|
}
|
2022-05-04 17:32:02 +00:00
|
|
|
|
2022-07-12 10:04:01 +00:00
|
|
|
func (c *Connection) isListDirWithWildcards(name string) bool {
|
2022-11-18 17:12:04 +00:00
|
|
|
if strings.ContainsAny(name, "*?[]^") {
|
2022-05-04 17:32:02 +00:00
|
|
|
lastCommand := c.clientContext.GetLastCommand()
|
2022-07-12 10:04:01 +00:00
|
|
|
return lastCommand == "LIST" || lastCommand == "NLST"
|
2022-05-04 17:32:02 +00:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func getPathRelativeTo(base, target string) string {
|
|
|
|
var sb strings.Builder
|
|
|
|
for {
|
|
|
|
if base == target {
|
|
|
|
return sb.String()
|
|
|
|
}
|
|
|
|
if !strings.HasSuffix(base, "/") {
|
|
|
|
base += "/"
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(target, base) {
|
|
|
|
sb.WriteString(strings.TrimPrefix(target, base))
|
|
|
|
return sb.String()
|
|
|
|
}
|
|
|
|
if base == "/" || base == "./" {
|
|
|
|
return target
|
|
|
|
}
|
|
|
|
sb.WriteString("../")
|
|
|
|
base = path.Dir(path.Clean(base))
|
|
|
|
}
|
|
|
|
}
|
2024-02-15 19:53:56 +00:00
|
|
|
|
|
|
|
type patternDirLister struct {
|
|
|
|
vfs.DirLister
|
|
|
|
pattern string
|
|
|
|
lastCommand string
|
|
|
|
dirName string
|
|
|
|
connectionPath string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *patternDirLister) Next(limit int) ([]os.FileInfo, error) {
|
|
|
|
for {
|
|
|
|
files, err := l.DirLister.Next(limit)
|
|
|
|
if len(files) == 0 {
|
|
|
|
return files, err
|
|
|
|
}
|
|
|
|
validIdx := 0
|
|
|
|
var relativeBase string
|
|
|
|
if l.lastCommand != "NLST" {
|
|
|
|
relativeBase = getPathRelativeTo(l.connectionPath, l.dirName)
|
|
|
|
}
|
|
|
|
for _, fi := range files {
|
|
|
|
match, errMatch := path.Match(l.pattern, fi.Name())
|
|
|
|
if errMatch != nil {
|
|
|
|
return nil, errMatch
|
|
|
|
}
|
|
|
|
if match {
|
|
|
|
files[validIdx] = vfs.NewFileInfo(path.Join(relativeBase, fi.Name()), fi.IsDir(), fi.Size(),
|
|
|
|
fi.ModTime(), true)
|
|
|
|
validIdx++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
files = files[:validIdx]
|
|
|
|
if err != nil || len(files) > 0 {
|
|
|
|
return files, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|