2022-07-17 18:16:00 +00:00
|
|
|
// Copyright (C) 2019-2022 Nicola Murino
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
2019-08-24 12:41:15 +00:00
|
|
|
package sftpd
|
|
|
|
|
|
|
|
import (
|
2021-01-20 21:37:59 +00:00
|
|
|
"errors"
|
2019-08-24 12:41:15 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"math"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
2020-10-31 10:02:04 +00:00
|
|
|
"runtime/debug"
|
2019-08-24 12:41:15 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
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/util"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/vfs"
|
2019-08-24 12:41:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2020-07-24 21:39:38 +00:00
|
|
|
okMsg = []byte{0x00}
|
|
|
|
warnMsg = []byte{0x01} // must be followed by an optional message and a newline
|
|
|
|
errMsg = []byte{0x02} // must be followed by an optional message and a newline
|
|
|
|
newLine = []byte{0x0A}
|
2019-08-24 12:41:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type scpCommand struct {
|
2019-11-18 22:30:37 +00:00
|
|
|
sshCommand
|
2019-08-24 12:41:15 +00:00
|
|
|
}
|
|
|
|
|
2020-10-31 10:02:04 +00:00
|
|
|
func (c *scpCommand) handle() (err error) {
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
2022-04-28 10:55:01 +00:00
|
|
|
logger.Error(logSender, "", "panic in handle scp command: %#v stack trace: %v", r, string(debug.Stack()))
|
2020-10-31 10:02:04 +00:00
|
|
|
err = common.ErrGenericFailure
|
|
|
|
}
|
|
|
|
}()
|
2022-04-14 17:07:41 +00:00
|
|
|
if err := common.Connections.Add(c.connection); err != nil {
|
|
|
|
logger.Info(logSender, "", "unable to add SCP connection: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
2020-07-29 19:56:56 +00:00
|
|
|
defer common.Connections.Remove(c.connection.GetID())
|
2020-07-24 21:39:38 +00:00
|
|
|
|
2019-08-24 12:41:15 +00:00
|
|
|
destPath := c.getDestPath()
|
|
|
|
commandType := c.getCommandType()
|
2020-07-24 21:39:38 +00:00
|
|
|
c.connection.Log(logger.LevelDebug, "handle scp command, args: %v user: %v command type: %v, dest path: %#v",
|
2019-08-24 12:41:15 +00:00
|
|
|
c.args, c.connection.User.Username, commandType, destPath)
|
|
|
|
if commandType == "-t" {
|
|
|
|
// -t means "to", so upload
|
2021-01-20 21:37:59 +00:00
|
|
|
err = c.sendConfirmationMessage()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-08-24 12:41:15 +00:00
|
|
|
err = c.handleRecursiveUpload()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else if commandType == "-f" {
|
|
|
|
// -f means "from" so download
|
|
|
|
err = c.readConfirmationMessage()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = c.handleDownload(destPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err = fmt.Errorf("scp command not supported, args: %v", c.args)
|
2020-07-24 21:39:38 +00:00
|
|
|
c.connection.Log(logger.LevelDebug, "unsupported scp command, args: %v", c.args)
|
2019-08-24 12:41:15 +00:00
|
|
|
}
|
|
|
|
c.sendExitStatus(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *scpCommand) handleRecursiveUpload() error {
|
|
|
|
numDirs := 0
|
|
|
|
destPath := c.getDestPath()
|
|
|
|
for {
|
2021-03-21 18:15:47 +00:00
|
|
|
fs, err := c.connection.User.GetFilesystemForPath(destPath, c.connection.ID)
|
|
|
|
if err != nil {
|
2021-12-16 18:53:00 +00:00
|
|
|
c.connection.Log(logger.LevelError, "error uploading file %#v: %+v", destPath, err)
|
2021-03-21 18:15:47 +00:00
|
|
|
c.sendErrorMessage(nil, fmt.Errorf("unable to get fs for path %#v", destPath))
|
|
|
|
return err
|
|
|
|
}
|
2019-08-24 12:41:15 +00:00
|
|
|
command, err := c.getNextUploadProtocolMessage()
|
|
|
|
if err != nil {
|
2021-01-20 21:37:59 +00:00
|
|
|
if errors.Is(err, io.EOF) {
|
|
|
|
return nil
|
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
c.sendErrorMessage(fs, err)
|
2019-08-24 12:41:15 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(command, "E") {
|
|
|
|
numDirs--
|
2020-07-24 21:39:38 +00:00
|
|
|
c.connection.Log(logger.LevelDebug, "received end dir command, num dirs: %v", numDirs)
|
2021-01-20 21:37:59 +00:00
|
|
|
if numDirs < 0 {
|
2021-03-21 18:15:47 +00:00
|
|
|
err = errors.New("unacceptable end dir command")
|
|
|
|
c.sendErrorMessage(nil, err)
|
|
|
|
return err
|
2019-08-24 12:41:15 +00:00
|
|
|
}
|
2021-01-20 21:37:59 +00:00
|
|
|
// the destination dir is now the parent directory
|
|
|
|
destPath = path.Join(destPath, "..")
|
2019-08-24 12:41:15 +00:00
|
|
|
} else {
|
2021-03-21 18:15:47 +00:00
|
|
|
sizeToRead, name, err := c.parseUploadMessage(fs, command)
|
2019-08-24 12:41:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(command, "D") {
|
|
|
|
numDirs++
|
2019-08-24 20:44:01 +00:00
|
|
|
destPath = path.Join(destPath, name)
|
2021-03-21 18:15:47 +00:00
|
|
|
fs, err = c.connection.User.GetFilesystemForPath(destPath, c.connection.ID)
|
|
|
|
if err != nil {
|
2021-12-16 18:53:00 +00:00
|
|
|
c.connection.Log(logger.LevelError, "error uploading file %#v: %+v", destPath, err)
|
2021-03-21 18:15:47 +00:00
|
|
|
c.sendErrorMessage(nil, fmt.Errorf("unable to get fs for path %#v", destPath))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = c.handleCreateDir(fs, destPath)
|
2019-08-24 12:41:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-07-24 21:39:38 +00:00
|
|
|
c.connection.Log(logger.LevelDebug, "received start dir command, num dirs: %v destPath: %#v", numDirs, destPath)
|
2019-08-24 12:41:15 +00:00
|
|
|
} else if strings.HasPrefix(command, "C") {
|
2021-03-21 18:15:47 +00:00
|
|
|
err = c.handleUpload(c.getFileUploadDestPath(fs, destPath, name), sizeToRead)
|
2019-08-24 12:41:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-20 21:37:59 +00:00
|
|
|
err = c.sendConfirmationMessage()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-08-24 12:41:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
func (c *scpCommand) handleCreateDir(fs vfs.Fs, dirPath string) error {
|
2020-07-24 21:39:38 +00:00
|
|
|
c.connection.UpdateLastActivity()
|
2021-03-21 18:15:47 +00:00
|
|
|
|
|
|
|
p, err := fs.ResolvePath(dirPath)
|
2019-08-24 12:41:15 +00:00
|
|
|
if err != nil {
|
2021-12-16 18:53:00 +00:00
|
|
|
c.connection.Log(logger.LevelError, "error creating dir: %#v, invalid file path, err: %v", dirPath, err)
|
2021-03-21 18:15:47 +00:00
|
|
|
c.sendErrorMessage(fs, err)
|
2019-08-24 12:41:15 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-01-05 10:41:25 +00:00
|
|
|
if !c.connection.User.HasPerm(dataprovider.PermCreateDirs, path.Dir(dirPath)) {
|
2021-12-16 18:53:00 +00:00
|
|
|
c.connection.Log(logger.LevelError, "error creating dir: %#v, permission denied", dirPath)
|
2021-03-21 18:15:47 +00:00
|
|
|
c.sendErrorMessage(fs, common.ErrPermissionDenied)
|
2020-09-19 08:14:30 +00:00
|
|
|
return common.ErrPermissionDenied
|
2019-12-25 17:20:19 +00:00
|
|
|
}
|
2019-08-24 12:41:15 +00:00
|
|
|
|
2022-01-15 16:16:49 +00:00
|
|
|
info, err := c.connection.DoStat(dirPath, 1, true)
|
2021-03-21 18:15:47 +00:00
|
|
|
if err == nil && info.IsDir() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
err = c.createDir(fs, p)
|
2019-08-24 12:41:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-07-24 21:39:38 +00:00
|
|
|
c.connection.Log(logger.LevelDebug, "created dir %#v", dirPath)
|
2019-08-24 12:41:15 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// we need to close the transfer if we have an error
|
2020-07-24 21:39:38 +00:00
|
|
|
func (c *scpCommand) getUploadFileData(sizeToRead int64, transfer *transfer) error {
|
2019-08-24 12:41:15 +00:00
|
|
|
err := c.sendConfirmationMessage()
|
|
|
|
if err != nil {
|
2019-09-07 21:10:20 +00:00
|
|
|
transfer.TransferError(err)
|
2019-08-24 12:41:15 +00:00
|
|
|
transfer.Close()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if sizeToRead > 0 {
|
2021-01-17 21:29:08 +00:00
|
|
|
// we could replace this method with io.CopyN implementing "Write" method in transfer struct
|
2019-08-24 12:41:15 +00:00
|
|
|
remaining := sizeToRead
|
|
|
|
buf := make([]byte, int64(math.Min(32768, float64(sizeToRead))))
|
|
|
|
for {
|
2019-09-11 14:29:56 +00:00
|
|
|
n, err := c.connection.channel.Read(buf)
|
2019-08-24 12:41:15 +00:00
|
|
|
if err != nil {
|
2021-03-21 18:15:47 +00:00
|
|
|
c.sendErrorMessage(transfer.Fs, err)
|
2019-09-07 21:10:20 +00:00
|
|
|
transfer.TransferError(err)
|
2019-08-24 12:41:15 +00:00
|
|
|
transfer.Close()
|
|
|
|
return err
|
|
|
|
}
|
2020-02-19 10:26:40 +00:00
|
|
|
_, err = transfer.WriteAt(buf[:n], sizeToRead-remaining)
|
|
|
|
if err != nil {
|
2021-03-21 18:15:47 +00:00
|
|
|
c.sendErrorMessage(transfer.Fs, err)
|
2020-02-19 10:26:40 +00:00
|
|
|
transfer.Close()
|
|
|
|
return err
|
|
|
|
}
|
2019-08-24 12:41:15 +00:00
|
|
|
remaining -= int64(n)
|
|
|
|
if remaining <= 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if remaining < int64(len(buf)) {
|
|
|
|
buf = make([]byte, remaining)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err = c.readConfirmationMessage()
|
|
|
|
if err != nil {
|
2019-09-07 21:10:20 +00:00
|
|
|
transfer.TransferError(err)
|
2019-08-24 12:41:15 +00:00
|
|
|
transfer.Close()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = transfer.Close()
|
|
|
|
if err != nil {
|
2021-03-21 18:15:47 +00:00
|
|
|
c.sendErrorMessage(transfer.Fs, err)
|
2019-08-24 12:41:15 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-01-20 21:37:59 +00:00
|
|
|
return nil
|
2019-08-24 12:41:15 +00:00
|
|
|
}
|
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
func (c *scpCommand) handleUploadFile(fs vfs.Fs, resolvedPath, filePath string, sizeToRead int64, isNewFile bool, fileSize int64, requestPath string) error {
|
2022-01-30 10:42:36 +00:00
|
|
|
diskQuota, transferQuota := c.connection.HasSpace(isNewFile, false, requestPath)
|
|
|
|
if !diskQuota.HasSpace || !transferQuota.HasUploadSpace() {
|
2020-06-07 21:30:18 +00:00
|
|
|
err := fmt.Errorf("denying file write due to quota limits")
|
2021-12-16 18:53:00 +00:00
|
|
|
c.connection.Log(logger.LevelError, "error uploading file: %#v, err: %v", filePath, err)
|
2022-01-30 10:42:36 +00:00
|
|
|
c.sendErrorMessage(nil, err)
|
2019-08-24 12:41:15 +00:00
|
|
|
return err
|
|
|
|
}
|
2023-01-02 14:59:00 +00:00
|
|
|
_, err := common.ExecutePreAction(c.connection.BaseConnection, common.OperationPreUpload, resolvedPath, requestPath,
|
2021-12-04 16:27:24 +00:00
|
|
|
fileSize, os.O_TRUNC)
|
2021-05-26 05:48:37 +00:00
|
|
|
if err != nil {
|
|
|
|
c.connection.Log(logger.LevelDebug, "upload for file %#v denied by pre action: %v", requestPath, err)
|
|
|
|
err = c.connection.GetPermissionDeniedError()
|
|
|
|
c.sendErrorMessage(fs, err)
|
|
|
|
return err
|
|
|
|
}
|
2019-08-24 12:41:15 +00:00
|
|
|
|
2022-01-30 10:42:36 +00:00
|
|
|
maxWriteSize, _ := c.connection.GetMaxWriteSize(diskQuota, false, fileSize, fs.IsUploadResumeSupported())
|
2020-08-16 18:17:02 +00:00
|
|
|
|
2022-02-17 17:22:27 +00:00
|
|
|
file, w, cancelFn, err := fs.Create(filePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC)
|
2020-06-15 21:32:12 +00:00
|
|
|
if err != nil {
|
2020-07-24 21:39:38 +00:00
|
|
|
c.connection.Log(logger.LevelError, "error creating file %#v: %v", resolvedPath, err)
|
2021-03-21 18:15:47 +00:00
|
|
|
c.sendErrorMessage(fs, err)
|
2020-06-15 21:32:12 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-01-23 09:19: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-01-23 09:19:56 +00:00
|
|
|
if !isNewFile {
|
2022-06-11 08:41:34 +00:00
|
|
|
if vfs.HasTruncateSupport(fs) {
|
2020-06-15 21:32:12 +00:00
|
|
|
vfolder, err := c.connection.User.GetVirtualFolderForPath(path.Dir(requestPath))
|
2020-06-07 21:30:18 +00:00
|
|
|
if err == nil {
|
2021-02-16 18:11:36 +00:00
|
|
|
dataprovider.UpdateVirtualFolderQuota(&vfolder.BaseVirtualFolder, 0, -fileSize, false) //nolint:errcheck
|
2020-06-07 21:30:18 +00:00
|
|
|
if vfolder.IsIncludedInUserQuota() {
|
2021-02-16 18:11:36 +00:00
|
|
|
dataprovider.UpdateUserQuota(&c.connection.User, 0, -fileSize, false) //nolint:errcheck
|
2020-06-07 21:30:18 +00:00
|
|
|
}
|
|
|
|
} else {
|
2021-02-16 18:11:36 +00:00
|
|
|
dataprovider.UpdateUserQuota(&c.connection.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
|
2022-01-20 17:19:20 +00:00
|
|
|
truncatedSize = initialSize
|
2020-01-23 09:19:56 +00:00
|
|
|
}
|
2020-06-18 20:38:03 +00:00
|
|
|
if maxWriteSize > 0 {
|
|
|
|
maxWriteSize += fileSize
|
|
|
|
}
|
2020-01-23 09:19:56 +00:00
|
|
|
}
|
2019-08-24 12:41:15 +00:00
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
vfs.SetPathPermissions(fs, filePath, c.connection.User.GetUID(), c.connection.User.GetGID())
|
2020-07-24 21:39:38 +00:00
|
|
|
|
2021-05-31 19:45:29 +00:00
|
|
|
baseTransfer := common.NewBaseTransfer(file, c.connection.BaseConnection, cancelFn, resolvedPath, filePath, requestPath,
|
2022-01-30 10:42:36 +00:00
|
|
|
common.TransferUpload, 0, initialSize, maxWriteSize, truncatedSize, isNewFile, fs, transferQuota)
|
2020-08-31 04:45:22 +00:00
|
|
|
t := newTransfer(baseTransfer, w, nil, nil)
|
2020-07-24 21:39:38 +00:00
|
|
|
|
|
|
|
return c.getUploadFileData(sizeToRead, t)
|
2019-08-24 12:41:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *scpCommand) handleUpload(uploadFilePath string, sizeToRead int64) error {
|
2020-07-24 21:39:38 +00:00
|
|
|
c.connection.UpdateLastActivity()
|
2019-08-24 12:41:15 +00:00
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
fs, p, err := c.connection.GetFsAndResolvedPath(uploadFilePath)
|
|
|
|
if err != nil {
|
2021-12-16 18:53:00 +00:00
|
|
|
c.connection.Log(logger.LevelError, "error uploading file: %#v, err: %v", uploadFilePath, err)
|
2021-03-21 18:15:47 +00:00
|
|
|
c.sendErrorMessage(nil, err)
|
|
|
|
return err
|
|
|
|
}
|
2019-08-24 12:41:15 +00:00
|
|
|
|
2022-01-15 16:16:49 +00:00
|
|
|
if ok, _ := c.connection.User.IsFileAllowed(uploadFilePath); !ok {
|
2020-07-24 21:39:38 +00:00
|
|
|
c.connection.Log(logger.LevelWarn, "writing file %#v is not allowed", uploadFilePath)
|
2022-01-15 16:16:49 +00:00
|
|
|
c.sendErrorMessage(fs, c.connection.GetPermissionDeniedError())
|
2020-09-19 08:14:30 +00:00
|
|
|
return common.ErrPermissionDenied
|
2020-03-01 21:10:29 +00:00
|
|
|
}
|
|
|
|
|
2019-08-24 12:41:15 +00:00
|
|
|
filePath := p
|
2021-03-21 18:15:47 +00:00
|
|
|
if common.Config.IsAtomicUploadEnabled() && fs.IsAtomicUploadSupported() {
|
|
|
|
filePath = fs.GetAtomicUploadPath(p)
|
2019-08-24 12:41:15 +00:00
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
stat, statErr := fs.Lstat(p)
|
|
|
|
if (statErr == nil && stat.Mode()&os.ModeSymlink != 0) || fs.IsNotExist(statErr) {
|
2020-01-05 10:41:25 +00:00
|
|
|
if !c.connection.User.HasPerm(dataprovider.PermUpload, path.Dir(uploadFilePath)) {
|
2020-07-24 21:39:38 +00:00
|
|
|
c.connection.Log(logger.LevelWarn, "cannot upload file: %#v, permission denied", uploadFilePath)
|
2021-03-21 18:15:47 +00:00
|
|
|
c.sendErrorMessage(fs, common.ErrPermissionDenied)
|
2020-09-19 08:14:30 +00:00
|
|
|
return common.ErrPermissionDenied
|
2019-12-25 17:20:19 +00:00
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
return c.handleUploadFile(fs, p, filePath, sizeToRead, true, 0, uploadFilePath)
|
2019-08-24 12:41:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if statErr != nil {
|
2020-07-24 21:39:38 +00:00
|
|
|
c.connection.Log(logger.LevelError, "error performing file stat %#v: %v", p, statErr)
|
2021-03-21 18:15:47 +00:00
|
|
|
c.sendErrorMessage(fs, statErr)
|
2020-01-19 06:41:05 +00:00
|
|
|
return statErr
|
2019-08-24 12:41:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if stat.IsDir() {
|
2021-12-16 18:53:00 +00:00
|
|
|
c.connection.Log(logger.LevelError, "attempted to open a directory for writing to: %#v", p)
|
2021-03-21 18:15:47 +00:00
|
|
|
err = fmt.Errorf("attempted to open a directory for writing: %#v", p)
|
|
|
|
c.sendErrorMessage(fs, err)
|
2019-08-24 12:41:15 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-01-05 10:41:25 +00:00
|
|
|
if !c.connection.User.HasPerm(dataprovider.PermOverwrite, uploadFilePath) {
|
2020-07-24 21:39:38 +00:00
|
|
|
c.connection.Log(logger.LevelWarn, "cannot overwrite file: %#v, permission denied", uploadFilePath)
|
2021-03-21 18:15:47 +00:00
|
|
|
c.sendErrorMessage(fs, common.ErrPermissionDenied)
|
2020-09-19 08:14:30 +00:00
|
|
|
return common.ErrPermissionDenied
|
2019-09-17 06:53:45 +00:00
|
|
|
}
|
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
if common.Config.IsAtomicUploadEnabled() && fs.IsAtomicUploadSupported() {
|
|
|
|
err = fs.Rename(p, filePath)
|
2019-08-24 12:41:15 +00:00
|
|
|
if err != nil {
|
2020-07-24 21:39:38 +00:00
|
|
|
c.connection.Log(logger.LevelError, "error renaming existing file for atomic upload, source: %#v, dest: %#v, err: %v",
|
2019-08-24 12:41:15 +00:00
|
|
|
p, filePath, err)
|
2021-03-21 18:15:47 +00:00
|
|
|
c.sendErrorMessage(fs, err)
|
2019-08-24 12:41:15 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
return c.handleUploadFile(fs, p, filePath, sizeToRead, false, stat.Size(), uploadFilePath)
|
2019-08-24 12:41:15 +00:00
|
|
|
}
|
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
func (c *scpCommand) sendDownloadProtocolMessages(virtualDirPath string, stat os.FileInfo) error {
|
2019-08-24 12:41:15 +00:00
|
|
|
var err error
|
|
|
|
if c.sendFileTime() {
|
|
|
|
modTime := stat.ModTime().UnixNano() / 1000000000
|
|
|
|
tCommand := fmt.Sprintf("T%v 0 %v 0\n", modTime, modTime)
|
|
|
|
err = c.sendProtocolMessage(tCommand)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = c.readConfirmationMessage()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
dirName := path.Base(virtualDirPath)
|
|
|
|
if dirName == "/" || dirName == "." {
|
|
|
|
dirName = c.connection.User.Username
|
2020-02-24 17:54:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fileMode := fmt.Sprintf("D%v 0 %v\n", getFileModeAsString(stat.Mode(), stat.IsDir()), dirName)
|
2019-08-24 12:41:15 +00:00
|
|
|
err = c.sendProtocolMessage(fileMode)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = c.readConfirmationMessage()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-06-07 21:30:18 +00:00
|
|
|
// We send first all the files in the root directory and then the directories.
|
|
|
|
// For each directory we recursively call this method again
|
2021-03-21 18:15:47 +00:00
|
|
|
func (c *scpCommand) handleRecursiveDownload(fs vfs.Fs, dirPath, virtualPath string, stat os.FileInfo) error {
|
2019-08-24 12:41:15 +00:00
|
|
|
var err error
|
|
|
|
if c.isRecursive() {
|
2021-03-21 18:15:47 +00:00
|
|
|
c.connection.Log(logger.LevelDebug, "recursive download, dir path %#v virtual path %#v", dirPath, virtualPath)
|
|
|
|
err = c.sendDownloadProtocolMessages(virtualPath, stat)
|
2019-08-24 12:41:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
files, err := fs.ReadDir(dirPath)
|
2019-08-24 12:41:15 +00:00
|
|
|
if err != nil {
|
2021-03-21 18:15:47 +00:00
|
|
|
c.sendErrorMessage(fs, err)
|
2019-08-24 12:41:15 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-01-15 16:16:49 +00:00
|
|
|
files = c.connection.User.FilterListDir(files, fs.GetRelativePath(dirPath))
|
2019-08-24 12:41:15 +00:00
|
|
|
var dirs []string
|
|
|
|
for _, file := range files {
|
2021-03-21 18:15:47 +00:00
|
|
|
filePath := fs.GetRelativePath(fs.Join(dirPath, file.Name()))
|
2020-11-17 18:36:39 +00:00
|
|
|
if file.Mode().IsRegular() || file.Mode()&os.ModeSymlink != 0 {
|
2019-08-24 12:41:15 +00:00
|
|
|
err = c.handleDownload(filePath)
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
} else if file.IsDir() {
|
|
|
|
dirs = append(dirs, filePath)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != nil {
|
2021-03-21 18:15:47 +00:00
|
|
|
c.sendErrorMessage(fs, err)
|
2019-08-24 12:41:15 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, dir := range dirs {
|
|
|
|
err = c.handleDownload(dir)
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != nil {
|
2021-03-21 18:15:47 +00:00
|
|
|
c.sendErrorMessage(fs, err)
|
2019-08-24 12:41:15 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = c.sendProtocolMessage("E\n")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
return c.readConfirmationMessage()
|
2019-08-24 12:41:15 +00:00
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
err = fmt.Errorf("unable to send directory for non recursive copy")
|
|
|
|
c.sendErrorMessage(nil, err)
|
2019-08-24 12:41:15 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
func (c *scpCommand) sendDownloadFileData(fs vfs.Fs, filePath string, stat os.FileInfo, transfer *transfer) error {
|
2019-08-24 12:41:15 +00:00
|
|
|
var err error
|
|
|
|
if c.sendFileTime() {
|
|
|
|
modTime := stat.ModTime().UnixNano() / 1000000000
|
|
|
|
tCommand := fmt.Sprintf("T%v 0 %v 0\n", modTime, modTime)
|
|
|
|
err = c.sendProtocolMessage(tCommand)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = c.readConfirmationMessage()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
if vfs.IsCryptOsFs(fs) {
|
|
|
|
stat = fs.(*vfs.CryptFs).ConvertFileInfo(stat)
|
2020-12-05 12:48:13 +00:00
|
|
|
}
|
2019-08-24 12:41:15 +00:00
|
|
|
|
|
|
|
fileSize := stat.Size()
|
|
|
|
readed := int64(0)
|
|
|
|
fileMode := fmt.Sprintf("C%v %v %v\n", getFileModeAsString(stat.Mode(), stat.IsDir()), fileSize, filepath.Base(filePath))
|
|
|
|
err = c.sendProtocolMessage(fileMode)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = c.readConfirmationMessage()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
// we could replace this method with io.CopyN implementing "Read" method in transfer struct
|
2019-08-24 12:41:15 +00:00
|
|
|
buf := make([]byte, 32768)
|
2020-01-10 18:20:22 +00:00
|
|
|
var n int
|
2019-08-24 12:41:15 +00:00
|
|
|
for {
|
2020-01-10 18:20:22 +00:00
|
|
|
n, err = transfer.ReadAt(buf, readed)
|
2019-08-24 12:41:15 +00:00
|
|
|
if err == nil || err == io.EOF {
|
|
|
|
if n > 0 {
|
2019-09-11 14:29:56 +00:00
|
|
|
_, err = c.connection.channel.Write(buf[:n])
|
2019-08-24 12:41:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
readed += int64(n)
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2020-02-03 23:08:00 +00:00
|
|
|
if err != io.EOF {
|
2021-03-21 18:15:47 +00:00
|
|
|
c.sendErrorMessage(fs, err)
|
2019-08-24 12:41:15 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = c.sendConfirmationMessage()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = c.readConfirmationMessage()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *scpCommand) handleDownload(filePath string) error {
|
2020-07-24 21:39:38 +00:00
|
|
|
c.connection.UpdateLastActivity()
|
2022-01-30 10:42:36 +00:00
|
|
|
transferQuota := c.connection.GetTransferQuota()
|
|
|
|
if !transferQuota.HasDownloadSpace() {
|
|
|
|
c.connection.Log(logger.LevelInfo, "denying file read due to quota limits")
|
|
|
|
c.sendErrorMessage(nil, c.connection.GetReadQuotaExceededError())
|
|
|
|
return c.connection.GetReadQuotaExceededError()
|
|
|
|
}
|
2019-08-24 12:41:15 +00:00
|
|
|
var err error
|
|
|
|
|
2022-10-22 09:56:41 +00:00
|
|
|
fs, p, err := c.connection.GetFsAndResolvedPath(filePath)
|
2021-03-21 18:15:47 +00:00
|
|
|
if err != nil {
|
2022-10-22 09:56:41 +00:00
|
|
|
c.connection.Log(logger.LevelError, "error downloading file %q: %+v", filePath, err)
|
|
|
|
c.sendErrorMessage(nil, fmt.Errorf("unable to download file %q: %w", filePath, err))
|
2019-08-24 12:41:15 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var stat os.FileInfo
|
2021-03-21 18:15:47 +00:00
|
|
|
if stat, err = fs.Stat(p); err != nil {
|
2021-12-16 18:53:00 +00:00
|
|
|
c.connection.Log(logger.LevelError, "error downloading file: %#v->%#v, err: %v", filePath, p, err)
|
2021-03-21 18:15:47 +00:00
|
|
|
c.sendErrorMessage(fs, err)
|
2019-08-24 12:41:15 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if stat.IsDir() {
|
2020-01-05 10:41:25 +00:00
|
|
|
if !c.connection.User.HasPerm(dataprovider.PermDownload, filePath) {
|
2020-07-24 21:39:38 +00:00
|
|
|
c.connection.Log(logger.LevelWarn, "error downloading dir: %#v, permission denied", filePath)
|
2021-03-21 18:15:47 +00:00
|
|
|
c.sendErrorMessage(fs, common.ErrPermissionDenied)
|
2020-09-19 08:14:30 +00:00
|
|
|
return common.ErrPermissionDenied
|
2019-12-25 17:20:19 +00:00
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
err = c.handleRecursiveDownload(fs, p, filePath, stat)
|
2019-08-24 12:41:15 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-01-05 10:41:25 +00:00
|
|
|
if !c.connection.User.HasPerm(dataprovider.PermDownload, path.Dir(filePath)) {
|
2020-07-24 21:39:38 +00:00
|
|
|
c.connection.Log(logger.LevelWarn, "error downloading dir: %#v, permission denied", filePath)
|
2021-03-21 18:15:47 +00:00
|
|
|
c.sendErrorMessage(fs, common.ErrPermissionDenied)
|
2020-09-19 08:14:30 +00:00
|
|
|
return common.ErrPermissionDenied
|
2020-03-01 21:10:29 +00:00
|
|
|
}
|
|
|
|
|
2022-01-15 16:16:49 +00:00
|
|
|
if ok, policy := c.connection.User.IsFileAllowed(filePath); !ok {
|
2020-07-24 21:39:38 +00:00
|
|
|
c.connection.Log(logger.LevelWarn, "reading file %#v is not allowed", filePath)
|
2022-01-15 16:16:49 +00:00
|
|
|
c.sendErrorMessage(fs, c.connection.GetErrorForDeniedFile(policy))
|
2020-09-19 08:14:30 +00:00
|
|
|
return common.ErrPermissionDenied
|
2019-12-25 17:20:19 +00:00
|
|
|
}
|
|
|
|
|
2023-01-02 14:59:00 +00:00
|
|
|
if _, err := common.ExecutePreAction(c.connection.BaseConnection, common.OperationPreDownload, p, filePath, 0, 0); err != nil {
|
2021-05-26 05:48:37 +00:00
|
|
|
c.connection.Log(logger.LevelDebug, "download for file %#v denied by pre action: %v", filePath, err)
|
|
|
|
c.sendErrorMessage(fs, common.ErrPermissionDenied)
|
|
|
|
return common.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
file, r, cancelFn, err := fs.Open(p, 0)
|
2019-08-24 12:41:15 +00:00
|
|
|
if err != nil {
|
2020-07-24 21:39:38 +00:00
|
|
|
c.connection.Log(logger.LevelError, "could not open file %#v for reading: %v", p, err)
|
2021-03-21 18:15:47 +00:00
|
|
|
c.sendErrorMessage(fs, err)
|
2019-08-24 12:41:15 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-05-31 19:45:29 +00:00
|
|
|
baseTransfer := common.NewBaseTransfer(file, c.connection.BaseConnection, cancelFn, p, p, filePath,
|
2022-01-30 10:42:36 +00:00
|
|
|
common.TransferDownload, 0, 0, 0, 0, false, fs, transferQuota)
|
2020-08-31 04:45:22 +00:00
|
|
|
t := newTransfer(baseTransfer, nil, r, nil)
|
2020-07-24 21:39:38 +00:00
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
err = c.sendDownloadFileData(fs, p, stat, t)
|
2019-08-24 12:41:15 +00:00
|
|
|
// we need to call Close anyway and return close error if any and
|
|
|
|
// if we have no previous error
|
|
|
|
if err == nil {
|
2020-07-24 21:39:38 +00:00
|
|
|
err = t.Close()
|
2019-08-24 12:41:15 +00:00
|
|
|
} else {
|
2020-07-24 21:39:38 +00:00
|
|
|
t.TransferError(err)
|
|
|
|
t.Close()
|
2019-08-24 12:41:15 +00:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *scpCommand) getCommandType() string {
|
|
|
|
return c.args[len(c.args)-2]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *scpCommand) sendFileTime() bool {
|
2022-05-19 17:49:51 +00:00
|
|
|
return util.Contains(c.args, "-p")
|
2019-08-24 12:41:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *scpCommand) isRecursive() bool {
|
2022-05-19 17:49:51 +00:00
|
|
|
return util.Contains(c.args, "-r")
|
2019-08-24 12:41:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// read the SCP confirmation message and the optional text message
|
|
|
|
// the channel will be closed on errors
|
|
|
|
func (c *scpCommand) readConfirmationMessage() error {
|
|
|
|
var msg strings.Builder
|
|
|
|
buf := make([]byte, 1)
|
2019-09-11 14:29:56 +00:00
|
|
|
n, err := c.connection.channel.Read(buf)
|
2019-08-24 12:41:15 +00:00
|
|
|
if err != nil {
|
2019-09-11 14:29:56 +00:00
|
|
|
c.connection.channel.Close()
|
2019-08-24 12:41:15 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if n == 1 && (buf[0] == warnMsg[0] || buf[0] == errMsg[0]) {
|
|
|
|
isError := buf[0] == errMsg[0]
|
|
|
|
for {
|
2019-09-11 14:29:56 +00:00
|
|
|
n, err = c.connection.channel.Read(buf)
|
2019-08-24 12:41:15 +00:00
|
|
|
readed := buf[:n]
|
|
|
|
if err != nil || (n == 1 && readed[0] == newLine[0]) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if n > 0 {
|
2021-01-05 21:32:30 +00:00
|
|
|
msg.Write(readed)
|
2019-08-24 12:41:15 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-24 21:39:38 +00:00
|
|
|
c.connection.Log(logger.LevelInfo, "scp error message received: %v is error: %v", msg.String(), isError)
|
2019-08-24 12:41:15 +00:00
|
|
|
err = fmt.Errorf("%v", msg.String())
|
2019-09-11 14:29:56 +00:00
|
|
|
c.connection.channel.Close()
|
2019-08-24 12:41:15 +00:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// protool messages are newline terminated
|
|
|
|
func (c *scpCommand) readProtocolMessage() (string, error) {
|
|
|
|
var command strings.Builder
|
|
|
|
var err error
|
|
|
|
buf := make([]byte, 1)
|
|
|
|
for {
|
|
|
|
var n int
|
2019-09-11 14:29:56 +00:00
|
|
|
n, err = c.connection.channel.Read(buf)
|
2019-08-24 12:41:15 +00:00
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if n > 0 {
|
|
|
|
readed := buf[:n]
|
|
|
|
if n == 1 && readed[0] == newLine[0] {
|
|
|
|
break
|
|
|
|
}
|
2021-01-05 21:32:30 +00:00
|
|
|
command.Write(readed)
|
2019-08-24 12:41:15 +00:00
|
|
|
}
|
|
|
|
}
|
2021-01-20 21:37:59 +00:00
|
|
|
if err != nil && !errors.Is(err, io.EOF) {
|
2019-09-11 14:29:56 +00:00
|
|
|
c.connection.channel.Close()
|
2019-08-24 12:41:15 +00:00
|
|
|
}
|
|
|
|
return command.String(), err
|
|
|
|
}
|
|
|
|
|
2022-08-04 19:50:38 +00:00
|
|
|
// sendErrorMessage sends an error message and close the channel
|
|
|
|
// we don't check write errors here, we have to close the channel anyway
|
|
|
|
//
|
|
|
|
//nolint:errcheck
|
2021-03-21 18:15:47 +00:00
|
|
|
func (c *scpCommand) sendErrorMessage(fs vfs.Fs, err error) {
|
2019-09-11 14:29:56 +00:00
|
|
|
c.connection.channel.Write(errMsg)
|
2021-03-21 18:15:47 +00:00
|
|
|
if fs != nil {
|
|
|
|
c.connection.channel.Write([]byte(c.connection.GetFsError(fs, err).Error()))
|
|
|
|
} else {
|
|
|
|
c.connection.channel.Write([]byte(err.Error()))
|
|
|
|
}
|
2019-09-11 14:29:56 +00:00
|
|
|
c.connection.channel.Write(newLine)
|
|
|
|
c.connection.channel.Close()
|
2019-08-24 12:41:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// send scp confirmation message and close the channel if an error happen
|
|
|
|
func (c *scpCommand) sendConfirmationMessage() error {
|
2019-09-11 14:29:56 +00:00
|
|
|
_, err := c.connection.channel.Write(okMsg)
|
2019-08-24 12:41:15 +00:00
|
|
|
if err != nil {
|
2019-09-11 14:29:56 +00:00
|
|
|
c.connection.channel.Close()
|
2019-08-24 12:41:15 +00:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// sends a protocol message and close the channel on error
|
|
|
|
func (c *scpCommand) sendProtocolMessage(message string) error {
|
2019-09-11 14:29:56 +00:00
|
|
|
_, err := c.connection.channel.Write([]byte(message))
|
2019-08-24 12:41:15 +00:00
|
|
|
if err != nil {
|
2021-12-16 18:53:00 +00:00
|
|
|
c.connection.Log(logger.LevelError, "error sending protocol message: %v, err: %v", message, err)
|
2019-09-11 14:29:56 +00:00
|
|
|
c.connection.channel.Close()
|
2019-08-24 12:41:15 +00:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the next upload protocol message ignoring T command if any
|
|
|
|
func (c *scpCommand) getNextUploadProtocolMessage() (string, error) {
|
|
|
|
var command string
|
|
|
|
var err error
|
|
|
|
for {
|
|
|
|
command, err = c.readProtocolMessage()
|
|
|
|
if err != nil {
|
|
|
|
return command, err
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(command, "T") {
|
|
|
|
err = c.sendConfirmationMessage()
|
|
|
|
if err != nil {
|
|
|
|
return command, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return command, err
|
|
|
|
}
|
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
func (c *scpCommand) createDir(fs vfs.Fs, dirPath string) error {
|
|
|
|
err := fs.Mkdir(dirPath)
|
|
|
|
if err != nil {
|
2020-07-24 21:39:38 +00:00
|
|
|
c.connection.Log(logger.LevelError, "error creating dir %#v: %v", dirPath, err)
|
2021-03-21 18:15:47 +00:00
|
|
|
c.sendErrorMessage(fs, err)
|
2019-08-24 12:41:15 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
vfs.SetPathPermissions(fs, dirPath, c.connection.User.GetUID(), c.connection.User.GetGID())
|
2019-08-24 12:41:15 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse protocol messages such as:
|
|
|
|
// D0755 0 testdir
|
|
|
|
// or:
|
|
|
|
// C0644 6 testfile
|
|
|
|
// and returns file size and file/directory name
|
2021-03-21 18:15:47 +00:00
|
|
|
func (c *scpCommand) parseUploadMessage(fs vfs.Fs, command string) (int64, string, error) {
|
2019-08-24 12:41:15 +00:00
|
|
|
var size int64
|
|
|
|
var name string
|
|
|
|
var err error
|
|
|
|
if !strings.HasPrefix(command, "C") && !strings.HasPrefix(command, "D") {
|
|
|
|
err = fmt.Errorf("unknown or invalid upload message: %v args: %v user: %v",
|
|
|
|
command, c.args, c.connection.User.Username)
|
2021-12-16 18:53:00 +00:00
|
|
|
c.connection.Log(logger.LevelError, "error: %v", err)
|
2021-03-21 18:15:47 +00:00
|
|
|
c.sendErrorMessage(fs, err)
|
2019-08-24 12:41:15 +00:00
|
|
|
return size, name, err
|
|
|
|
}
|
2020-02-23 10:30:26 +00:00
|
|
|
parts := strings.SplitN(command, " ", 3)
|
2019-08-24 12:41:15 +00:00
|
|
|
if len(parts) == 3 {
|
|
|
|
size, err = strconv.ParseInt(parts[1], 10, 64)
|
|
|
|
if err != nil {
|
2021-12-16 18:53:00 +00:00
|
|
|
c.connection.Log(logger.LevelError, "error getting size from upload message: %v", err)
|
2021-03-21 18:15:47 +00:00
|
|
|
c.sendErrorMessage(fs, err)
|
2019-08-24 12:41:15 +00:00
|
|
|
return size, name, err
|
|
|
|
}
|
|
|
|
name = parts[2]
|
2021-01-05 08:50:22 +00:00
|
|
|
if name == "" {
|
2019-08-24 12:41:15 +00:00
|
|
|
err = fmt.Errorf("error getting name from upload message, cannot be empty")
|
2021-12-16 18:53:00 +00:00
|
|
|
c.connection.Log(logger.LevelError, "error: %v", err)
|
2021-03-21 18:15:47 +00:00
|
|
|
c.sendErrorMessage(fs, err)
|
2019-08-24 12:41:15 +00:00
|
|
|
return size, name, err
|
|
|
|
}
|
|
|
|
} else {
|
2021-01-05 21:32:30 +00:00
|
|
|
err = fmt.Errorf("unable to split upload message: %#v", command)
|
2021-12-16 18:53:00 +00:00
|
|
|
c.connection.Log(logger.LevelError, "error: %v", err)
|
2021-03-21 18:15:47 +00:00
|
|
|
c.sendErrorMessage(fs, err)
|
2019-08-24 12:41:15 +00:00
|
|
|
return size, name, err
|
|
|
|
}
|
|
|
|
return size, name, err
|
|
|
|
}
|
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
func (c *scpCommand) getFileUploadDestPath(fs vfs.Fs, scpDestPath, fileName string) string {
|
2019-08-24 20:44:01 +00:00
|
|
|
if !c.isRecursive() {
|
|
|
|
// if the upload is not recursive and the destination path does not end with "/"
|
|
|
|
// then scpDestPath is the wanted filename, for example:
|
|
|
|
// scp fileName.txt user@127.0.0.1:/newFileName.txt
|
|
|
|
// or
|
|
|
|
// scp fileName.txt user@127.0.0.1:/fileName.txt
|
|
|
|
if !strings.HasSuffix(scpDestPath, "/") {
|
|
|
|
// but if scpDestPath is an existing directory then we put the uploaded file
|
|
|
|
// inside that directory this is as scp command works, for example:
|
|
|
|
// scp fileName.txt user@127.0.0.1:/existing_dir
|
2021-03-21 18:15:47 +00:00
|
|
|
if p, err := fs.ResolvePath(scpDestPath); err == nil {
|
|
|
|
if stat, err := fs.Stat(p); err == nil {
|
2019-08-24 20:44:01 +00:00
|
|
|
if stat.IsDir() {
|
|
|
|
return path.Join(scpDestPath, fileName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return scpDestPath
|
|
|
|
}
|
|
|
|
}
|
2019-08-25 11:51:54 +00:00
|
|
|
// if the upload is recursive or scpDestPath has the "/" suffix then the destination
|
|
|
|
// file is relative to scpDestPath
|
2019-08-24 20:44:01 +00:00
|
|
|
return path.Join(scpDestPath, fileName)
|
|
|
|
}
|
|
|
|
|
2019-08-24 12:41:15 +00:00
|
|
|
func getFileModeAsString(fileMode os.FileMode, isDir bool) string {
|
|
|
|
var defaultMode string
|
|
|
|
if isDir {
|
|
|
|
defaultMode = "0755"
|
|
|
|
} else {
|
|
|
|
defaultMode = "0644"
|
|
|
|
}
|
|
|
|
if fileMode == 0 {
|
|
|
|
return defaultMode
|
|
|
|
}
|
|
|
|
modeString := []byte(fileMode.String())
|
|
|
|
nullPerm := []byte("-")
|
|
|
|
u := 0
|
|
|
|
g := 0
|
|
|
|
o := 0
|
|
|
|
s := 0
|
|
|
|
lastChar := len(modeString) - 1
|
|
|
|
if fileMode&os.ModeSticky != 0 {
|
|
|
|
s++
|
|
|
|
}
|
|
|
|
if fileMode&os.ModeSetuid != 0 {
|
|
|
|
s += 2
|
|
|
|
}
|
|
|
|
if fileMode&os.ModeSetgid != 0 {
|
|
|
|
s += 4
|
|
|
|
}
|
|
|
|
if modeString[lastChar-8] != nullPerm[0] {
|
|
|
|
u += 4
|
|
|
|
}
|
|
|
|
if modeString[lastChar-7] != nullPerm[0] {
|
|
|
|
u += 2
|
|
|
|
}
|
|
|
|
if modeString[lastChar-6] != nullPerm[0] {
|
|
|
|
u++
|
|
|
|
}
|
|
|
|
if modeString[lastChar-5] != nullPerm[0] {
|
|
|
|
g += 4
|
|
|
|
}
|
|
|
|
if modeString[lastChar-4] != nullPerm[0] {
|
|
|
|
g += 2
|
|
|
|
}
|
|
|
|
if modeString[lastChar-3] != nullPerm[0] {
|
|
|
|
g++
|
|
|
|
}
|
|
|
|
if modeString[lastChar-2] != nullPerm[0] {
|
|
|
|
o += 4
|
|
|
|
}
|
|
|
|
if modeString[lastChar-1] != nullPerm[0] {
|
|
|
|
o += 2
|
|
|
|
}
|
|
|
|
if modeString[lastChar] != nullPerm[0] {
|
|
|
|
o++
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%v%v%v%v", s, u, g, o)
|
|
|
|
}
|