2019-11-18 22:30:37 +00:00
|
|
|
package sftpd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/md5"
|
|
|
|
"crypto/sha1"
|
2021-04-05 16:23:40 +00:00
|
|
|
"crypto/sha256"
|
2019-11-18 22:30:37 +00:00
|
|
|
"crypto/sha512"
|
2019-11-26 21:26:42 +00:00
|
|
|
"errors"
|
2019-11-18 22:30:37 +00:00
|
|
|
"fmt"
|
|
|
|
"hash"
|
|
|
|
"io"
|
|
|
|
"os"
|
2019-11-26 21:26:42 +00:00
|
|
|
"os/exec"
|
2020-06-13 20:48:51 +00:00
|
|
|
"path"
|
2020-10-31 10:02:04 +00:00
|
|
|
"runtime/debug"
|
2019-11-18 22:30:37 +00:00
|
|
|
"strings"
|
2019-11-26 21:26:42 +00:00
|
|
|
"sync"
|
2019-11-18 22:30:37 +00:00
|
|
|
|
2020-05-06 17:36:34 +00:00
|
|
|
"github.com/google/shlex"
|
2020-06-13 20:48:51 +00:00
|
|
|
fscopy "github.com/otiai10/copy"
|
2020-05-06 17:36:34 +00:00
|
|
|
"golang.org/x/crypto/ssh"
|
|
|
|
|
2021-06-26 05:31:41 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/common"
|
|
|
|
"github.com/drakkan/sftpgo/v2/dataprovider"
|
|
|
|
"github.com/drakkan/sftpgo/v2/logger"
|
|
|
|
"github.com/drakkan/sftpgo/v2/metrics"
|
|
|
|
"github.com/drakkan/sftpgo/v2/utils"
|
|
|
|
"github.com/drakkan/sftpgo/v2/vfs"
|
2019-11-18 22:30:37 +00:00
|
|
|
)
|
|
|
|
|
2020-07-24 21:39:38 +00:00
|
|
|
const (
|
|
|
|
scpCmdName = "scp"
|
|
|
|
sshCommandLogSender = "SSHCommand"
|
|
|
|
)
|
2020-04-30 12:23:55 +00:00
|
|
|
|
2019-11-26 21:26:42 +00:00
|
|
|
var (
|
2020-07-24 21:39:38 +00:00
|
|
|
errUnsupportedConfig = errors.New("command unsupported for this configuration")
|
2019-11-26 21:26:42 +00:00
|
|
|
)
|
|
|
|
|
2019-11-18 22:30:37 +00:00
|
|
|
type sshCommand struct {
|
|
|
|
command string
|
|
|
|
args []string
|
2020-07-24 21:39:38 +00:00
|
|
|
connection *Connection
|
2019-11-18 22:30:37 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 21:26:42 +00:00
|
|
|
type systemCommand struct {
|
2020-06-15 21:32:12 +00:00
|
|
|
cmd *exec.Cmd
|
|
|
|
fsPath string
|
|
|
|
quotaCheckPath string
|
2021-03-21 18:15:47 +00:00
|
|
|
fs vfs.Fs
|
2019-11-26 21:26:42 +00:00
|
|
|
}
|
|
|
|
|
2020-09-18 08:52:53 +00:00
|
|
|
func processSSHCommand(payload []byte, connection *Connection, enabledSSHCommands []string) bool {
|
2019-11-18 22:30:37 +00:00
|
|
|
var msg sshSubsystemExecMsg
|
|
|
|
if err := ssh.Unmarshal(payload, &msg); err == nil {
|
2020-02-14 15:17:32 +00:00
|
|
|
name, args, err := parseCommandPayload(msg.Command)
|
2020-07-24 21:39:38 +00:00
|
|
|
connection.Log(logger.LevelDebug, "new ssh command: %#v args: %v num args: %v user: %v, error: %v",
|
2020-02-14 15:17:32 +00:00
|
|
|
name, args, len(args), connection.User.Username, err)
|
2019-11-18 22:30:37 +00:00
|
|
|
if err == nil && utils.IsStringInSlice(name, enabledSSHCommands) {
|
2020-02-14 15:17:32 +00:00
|
|
|
connection.command = msg.Command
|
2020-04-30 12:23:55 +00:00
|
|
|
if name == scpCmdName && len(args) >= 2 {
|
2020-07-24 21:39:38 +00:00
|
|
|
connection.SetProtocol(common.ProtocolSCP)
|
2019-11-18 22:30:37 +00:00
|
|
|
scpCommand := scpCommand{
|
|
|
|
sshCommand: sshCommand{
|
|
|
|
command: name,
|
2020-07-24 21:39:38 +00:00
|
|
|
connection: connection,
|
2019-11-18 22:30:37 +00:00
|
|
|
args: args},
|
|
|
|
}
|
2020-04-30 12:23:55 +00:00
|
|
|
go scpCommand.handle() //nolint:errcheck
|
2019-11-18 22:30:37 +00:00
|
|
|
return true
|
|
|
|
}
|
2020-04-30 12:23:55 +00:00
|
|
|
if name != scpCmdName {
|
2020-07-24 21:39:38 +00:00
|
|
|
connection.SetProtocol(common.ProtocolSSH)
|
2019-11-18 22:30:37 +00:00
|
|
|
sshCommand := sshCommand{
|
|
|
|
command: name,
|
2020-07-24 21:39:38 +00:00
|
|
|
connection: connection,
|
2019-11-18 22:30:37 +00:00
|
|
|
args: args,
|
|
|
|
}
|
2020-04-30 12:23:55 +00:00
|
|
|
go sshCommand.handle() //nolint:errcheck
|
2019-11-18 22:30:37 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
} else {
|
2020-07-24 21:39:38 +00:00
|
|
|
connection.Log(logger.LevelInfo, "ssh command not enabled/supported: %#v", name)
|
2019-11-18 22:30:37 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
err := connection.CloseFS()
|
|
|
|
connection.Log(logger.LevelDebug, "unable to unmarsh ssh command, close fs, err: %v", err)
|
2019-11-18 22:30:37 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-10-31 10:02:04 +00:00
|
|
|
func (c *sshCommand) handle() (err error) {
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
logger.Error(logSender, "", "panic in handle ssh command: %#v stack strace: %v", r, string(debug.Stack()))
|
|
|
|
err = common.ErrGenericFailure
|
|
|
|
}
|
|
|
|
}()
|
2020-07-24 21:39:38 +00:00
|
|
|
common.Connections.Add(c.connection)
|
2020-07-29 19:56:56 +00:00
|
|
|
defer common.Connections.Remove(c.connection.GetID())
|
2020-07-24 21:39:38 +00:00
|
|
|
|
|
|
|
c.connection.UpdateLastActivity()
|
2019-11-18 22:30:37 +00:00
|
|
|
if utils.IsStringInSlice(c.command, sshHashCommands) {
|
2019-11-26 21:26:42 +00:00
|
|
|
return c.handleHashCommands()
|
2019-11-29 14:24:56 +00:00
|
|
|
} else if utils.IsStringInSlice(c.command, systemCommands) {
|
2019-11-26 21:26:42 +00:00
|
|
|
command, err := c.getSystemCommand()
|
|
|
|
if err != nil {
|
|
|
|
return c.sendErrorResponse(err)
|
2019-11-18 22:30:37 +00:00
|
|
|
}
|
2019-11-26 21:26:42 +00:00
|
|
|
return c.executeSystemCommand(command)
|
2019-11-18 22:30:37 +00:00
|
|
|
} else if c.command == "cd" {
|
|
|
|
c.sendExitStatus(nil)
|
|
|
|
} else if c.command == "pwd" {
|
|
|
|
// hard coded response to "/"
|
2020-04-30 12:23:55 +00:00
|
|
|
c.connection.channel.Write([]byte("/\n")) //nolint:errcheck
|
2019-11-18 22:30:37 +00:00
|
|
|
c.sendExitStatus(nil)
|
2020-06-13 20:48:51 +00:00
|
|
|
} else if c.command == "sftpgo-copy" {
|
2021-03-21 18:15:47 +00:00
|
|
|
return c.handleSFTPGoCopy()
|
2020-06-13 20:48:51 +00:00
|
|
|
} else if c.command == "sftpgo-remove" {
|
2021-03-21 18:15:47 +00:00
|
|
|
return c.handleSFTPGoRemove()
|
2019-11-18 22:30:37 +00:00
|
|
|
}
|
2020-10-31 10:02:04 +00:00
|
|
|
return
|
2019-11-18 22:30:37 +00:00
|
|
|
}
|
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
func (c *sshCommand) handleSFTPGoCopy() error {
|
|
|
|
fsSrc, fsDst, sshSourcePath, sshDestPath, fsSourcePath, fsDestPath, err := c.getFsAndCopyPaths()
|
2020-06-13 20:48:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return c.sendErrorResponse(err)
|
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
if !c.isLocalCopy(sshSourcePath, sshDestPath) {
|
|
|
|
return c.sendErrorResponse(errUnsupportedConfig)
|
2020-06-13 20:48:51 +00:00
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
|
|
|
|
if err := c.checkCopyDestination(fsDst, fsDestPath); err != nil {
|
|
|
|
return c.sendErrorResponse(c.connection.GetFsError(fsDst, err))
|
2020-06-13 20:48:51 +00:00
|
|
|
}
|
|
|
|
|
2020-07-24 21:39:38 +00:00
|
|
|
c.connection.Log(logger.LevelDebug, "requested copy %#v -> %#v sftp paths %#v -> %#v",
|
2020-06-13 20:48:51 +00:00
|
|
|
fsSourcePath, fsDestPath, sshSourcePath, sshDestPath)
|
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
fi, err := fsSrc.Lstat(fsSourcePath)
|
2020-06-13 20:48:51 +00:00
|
|
|
if err != nil {
|
2021-03-21 18:15:47 +00:00
|
|
|
return c.sendErrorResponse(c.connection.GetFsError(fsSrc, err))
|
2020-06-13 20:48:51 +00:00
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
if err := c.checkCopyPermissions(fsSrc, fsDst, fsSourcePath, fsDestPath, sshSourcePath, sshDestPath, fi); err != nil {
|
2020-06-26 21:38:29 +00:00
|
|
|
return c.sendErrorResponse(err)
|
|
|
|
}
|
2020-06-13 20:48:51 +00:00
|
|
|
filesNum := 0
|
|
|
|
filesSize := int64(0)
|
|
|
|
if fi.IsDir() {
|
2021-03-21 18:15:47 +00:00
|
|
|
filesNum, filesSize, err = fsSrc.GetDirSize(fsSourcePath)
|
2020-06-13 20:48:51 +00:00
|
|
|
if err != nil {
|
2021-03-21 18:15:47 +00:00
|
|
|
return c.sendErrorResponse(c.connection.GetFsError(fsSrc, err))
|
2020-06-13 20:48:51 +00:00
|
|
|
}
|
|
|
|
if c.connection.User.HasVirtualFoldersInside(sshSourcePath) {
|
|
|
|
err := errors.New("unsupported copy source: the source directory contains virtual folders")
|
|
|
|
return c.sendErrorResponse(err)
|
|
|
|
}
|
|
|
|
if c.connection.User.HasVirtualFoldersInside(sshDestPath) {
|
|
|
|
err := errors.New("unsupported copy source: the destination directory contains virtual folders")
|
|
|
|
return c.sendErrorResponse(err)
|
|
|
|
}
|
|
|
|
} else if fi.Mode().IsRegular() {
|
|
|
|
if !c.connection.User.IsFileAllowed(sshDestPath) {
|
|
|
|
err := errors.New("unsupported copy destination: this file is not allowed")
|
|
|
|
return c.sendErrorResponse(err)
|
|
|
|
}
|
|
|
|
filesNum = 1
|
|
|
|
filesSize = fi.Size()
|
|
|
|
} else {
|
|
|
|
err := errors.New("unsupported copy source: only files and directories are supported")
|
|
|
|
return c.sendErrorResponse(err)
|
|
|
|
}
|
2020-06-16 20:49:18 +00:00
|
|
|
if err := c.checkCopyQuota(filesNum, filesSize, sshDestPath); err != nil {
|
|
|
|
return c.sendErrorResponse(err)
|
|
|
|
}
|
2020-07-24 21:39:38 +00:00
|
|
|
c.connection.Log(logger.LevelDebug, "start copy %#v -> %#v", fsSourcePath, fsDestPath)
|
2021-05-30 21:07:46 +00:00
|
|
|
err = fscopy.Copy(fsSourcePath, fsDestPath, fscopy.Options{
|
|
|
|
OnSymlink: func(src string) fscopy.SymlinkAction {
|
|
|
|
return fscopy.Skip
|
|
|
|
},
|
|
|
|
})
|
2020-06-13 20:48:51 +00:00
|
|
|
if err != nil {
|
2021-03-21 18:15:47 +00:00
|
|
|
return c.sendErrorResponse(c.connection.GetFsError(fsSrc, err))
|
2020-06-13 20:48:51 +00:00
|
|
|
}
|
|
|
|
c.updateQuota(sshDestPath, filesNum, filesSize)
|
|
|
|
c.connection.channel.Write([]byte("OK\n")) //nolint:errcheck
|
|
|
|
c.sendExitStatus(nil)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
func (c *sshCommand) handleSFTPGoRemove() error {
|
2020-06-13 20:48:51 +00:00
|
|
|
sshDestPath, err := c.getRemovePath()
|
|
|
|
if err != nil {
|
|
|
|
return c.sendErrorResponse(err)
|
|
|
|
}
|
|
|
|
if !c.connection.User.HasPerm(dataprovider.PermDelete, path.Dir(sshDestPath)) {
|
2020-07-24 21:39:38 +00:00
|
|
|
return c.sendErrorResponse(common.ErrPermissionDenied)
|
2020-06-13 20:48:51 +00:00
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
fs, fsDestPath, err := c.connection.GetFsAndResolvedPath(sshDestPath)
|
2020-06-13 20:48:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return c.sendErrorResponse(err)
|
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
if !vfs.IsLocalOrCryptoFs(fs) {
|
|
|
|
return c.sendErrorResponse(errUnsupportedConfig)
|
|
|
|
}
|
|
|
|
fi, err := fs.Lstat(fsDestPath)
|
2020-06-13 20:48:51 +00:00
|
|
|
if err != nil {
|
2021-03-21 18:15:47 +00:00
|
|
|
return c.sendErrorResponse(c.connection.GetFsError(fs, err))
|
2020-06-13 20:48:51 +00:00
|
|
|
}
|
|
|
|
filesNum := 0
|
|
|
|
filesSize := int64(0)
|
|
|
|
if fi.IsDir() {
|
2021-03-21 18:15:47 +00:00
|
|
|
filesNum, filesSize, err = fs.GetDirSize(fsDestPath)
|
2020-06-13 20:48:51 +00:00
|
|
|
if err != nil {
|
2021-03-21 18:15:47 +00:00
|
|
|
return c.sendErrorResponse(c.connection.GetFsError(fs, err))
|
2020-06-13 20:48:51 +00:00
|
|
|
}
|
|
|
|
if sshDestPath == "/" {
|
|
|
|
err := errors.New("removing root dir is not allowed")
|
|
|
|
return c.sendErrorResponse(err)
|
|
|
|
}
|
|
|
|
if c.connection.User.HasVirtualFoldersInside(sshDestPath) {
|
|
|
|
err := errors.New("unsupported remove source: this directory contains virtual folders")
|
|
|
|
return c.sendErrorResponse(err)
|
|
|
|
}
|
|
|
|
if c.connection.User.IsVirtualFolder(sshDestPath) {
|
|
|
|
err := errors.New("unsupported remove source: this directory is a virtual folder")
|
|
|
|
return c.sendErrorResponse(err)
|
|
|
|
}
|
|
|
|
} else if fi.Mode().IsRegular() {
|
|
|
|
filesNum = 1
|
|
|
|
filesSize = fi.Size()
|
|
|
|
} else {
|
|
|
|
err := errors.New("unsupported remove source: only files and directories are supported")
|
|
|
|
return c.sendErrorResponse(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = os.RemoveAll(fsDestPath)
|
|
|
|
if err != nil {
|
|
|
|
return c.sendErrorResponse(err)
|
|
|
|
}
|
|
|
|
c.updateQuota(sshDestPath, -filesNum, -filesSize)
|
|
|
|
c.connection.channel.Write([]byte("OK\n")) //nolint:errcheck
|
|
|
|
c.sendExitStatus(nil)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *sshCommand) updateQuota(sshDestPath string, filesNum int, filesSize int64) {
|
|
|
|
vfolder, err := c.connection.User.GetVirtualFolderForPath(sshDestPath)
|
|
|
|
if err == nil {
|
2021-02-16 18:11:36 +00:00
|
|
|
dataprovider.UpdateVirtualFolderQuota(&vfolder.BaseVirtualFolder, filesNum, filesSize, false) //nolint:errcheck
|
2020-06-13 20:48:51 +00:00
|
|
|
if vfolder.IsIncludedInUserQuota() {
|
2021-02-16 18:11:36 +00:00
|
|
|
dataprovider.UpdateUserQuota(&c.connection.User, filesNum, filesSize, false) //nolint:errcheck
|
2020-06-13 20:48:51 +00:00
|
|
|
}
|
|
|
|
} else {
|
2021-02-16 18:11:36 +00:00
|
|
|
dataprovider.UpdateUserQuota(&c.connection.User, filesNum, filesSize, false) //nolint:errcheck
|
2020-06-13 20:48:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-26 21:26:42 +00:00
|
|
|
func (c *sshCommand) handleHashCommands() error {
|
|
|
|
var h hash.Hash
|
|
|
|
if c.command == "md5sum" {
|
|
|
|
h = md5.New()
|
|
|
|
} else if c.command == "sha1sum" {
|
|
|
|
h = sha1.New()
|
|
|
|
} else if c.command == "sha256sum" {
|
|
|
|
h = sha256.New()
|
|
|
|
} else if c.command == "sha384sum" {
|
|
|
|
h = sha512.New384()
|
|
|
|
} else {
|
|
|
|
h = sha512.New()
|
|
|
|
}
|
|
|
|
var response string
|
|
|
|
if len(c.args) == 0 {
|
|
|
|
// without args we need to read the string to hash from stdin
|
|
|
|
buf := make([]byte, 4096)
|
|
|
|
n, err := c.connection.channel.Read(buf)
|
|
|
|
if err != nil && err != io.EOF {
|
|
|
|
return c.sendErrorResponse(err)
|
|
|
|
}
|
2020-04-30 12:23:55 +00:00
|
|
|
h.Write(buf[:n]) //nolint:errcheck
|
2019-11-26 21:26:42 +00:00
|
|
|
response = fmt.Sprintf("%x -\n", h.Sum(nil))
|
|
|
|
} else {
|
|
|
|
sshPath := c.getDestPath()
|
2020-03-01 21:10:29 +00:00
|
|
|
if !c.connection.User.IsFileAllowed(sshPath) {
|
2020-07-24 21:39:38 +00:00
|
|
|
c.connection.Log(logger.LevelInfo, "hash not allowed for file %#v", sshPath)
|
2021-03-21 18:15:47 +00:00
|
|
|
return c.sendErrorResponse(c.connection.GetPermissionDeniedError())
|
2020-03-01 21:10:29 +00:00
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
fs, fsPath, err := c.connection.GetFsAndResolvedPath(sshPath)
|
2019-11-26 21:26:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return c.sendErrorResponse(err)
|
|
|
|
}
|
2020-01-05 10:41:25 +00:00
|
|
|
if !c.connection.User.HasPerm(dataprovider.PermListItems, sshPath) {
|
2021-03-21 18:15:47 +00:00
|
|
|
return c.sendErrorResponse(c.connection.GetPermissionDeniedError())
|
2019-12-25 17:20:19 +00:00
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
hash, err := c.computeHashForFile(fs, h, fsPath)
|
2019-11-26 21:26:42 +00:00
|
|
|
if err != nil {
|
2021-03-21 18:15:47 +00:00
|
|
|
return c.sendErrorResponse(c.connection.GetFsError(fs, err))
|
2019-11-26 21:26:42 +00:00
|
|
|
}
|
|
|
|
response = fmt.Sprintf("%v %v\n", hash, sshPath)
|
|
|
|
}
|
2020-04-30 12:23:55 +00:00
|
|
|
c.connection.channel.Write([]byte(response)) //nolint:errcheck
|
2019-11-26 21:26:42 +00:00
|
|
|
c.sendExitStatus(nil)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *sshCommand) executeSystemCommand(command systemCommand) error {
|
2021-03-21 18:15:47 +00:00
|
|
|
sshDestPath := c.getDestPath()
|
|
|
|
if !c.isLocalPath(sshDestPath) {
|
2020-01-19 06:41:05 +00:00
|
|
|
return c.sendErrorResponse(errUnsupportedConfig)
|
|
|
|
}
|
2021-02-11 18:45:52 +00:00
|
|
|
quotaResult := c.connection.HasSpace(true, false, command.quotaCheckPath)
|
2020-06-16 20:49:18 +00:00
|
|
|
if !quotaResult.HasSpace {
|
2020-07-24 21:39:38 +00:00
|
|
|
return c.sendErrorResponse(common.ErrQuotaExceeded)
|
2019-11-26 21:26:42 +00:00
|
|
|
}
|
|
|
|
perms := []string{dataprovider.PermDownload, dataprovider.PermUpload, dataprovider.PermCreateDirs, dataprovider.PermListItems,
|
2020-06-15 21:32:12 +00:00
|
|
|
dataprovider.PermOverwrite, dataprovider.PermDelete}
|
|
|
|
if !c.connection.User.HasPerms(perms, sshDestPath) {
|
2021-03-21 18:15:47 +00:00
|
|
|
return c.sendErrorResponse(c.connection.GetPermissionDeniedError())
|
2019-11-26 21:26:42 +00:00
|
|
|
}
|
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
initialFiles, initialSize, err := c.getSizeForPath(command.fs, command.fsPath)
|
2020-06-15 21:32:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return c.sendErrorResponse(err)
|
|
|
|
}
|
|
|
|
|
2019-11-26 21:26:42 +00:00
|
|
|
stdin, err := command.cmd.StdinPipe()
|
|
|
|
if err != nil {
|
|
|
|
return c.sendErrorResponse(err)
|
|
|
|
}
|
|
|
|
stdout, err := command.cmd.StdoutPipe()
|
|
|
|
if err != nil {
|
|
|
|
return c.sendErrorResponse(err)
|
|
|
|
}
|
|
|
|
stderr, err := command.cmd.StderrPipe()
|
|
|
|
if err != nil {
|
|
|
|
return c.sendErrorResponse(err)
|
|
|
|
}
|
|
|
|
err = command.cmd.Start()
|
|
|
|
if err != nil {
|
|
|
|
return c.sendErrorResponse(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
closeCmdOnError := func() {
|
2020-07-24 21:39:38 +00:00
|
|
|
c.connection.Log(logger.LevelDebug, "kill cmd: %#v and close ssh channel after read or write error",
|
2019-11-26 21:26:42 +00:00
|
|
|
c.connection.command)
|
2020-04-30 12:23:55 +00:00
|
|
|
killerr := command.cmd.Process.Kill()
|
|
|
|
closerr := c.connection.channel.Close()
|
2020-07-24 21:39:38 +00:00
|
|
|
c.connection.Log(logger.LevelDebug, "kill cmd error: %v close channel error: %v", killerr, closerr)
|
2019-11-26 21:26:42 +00:00
|
|
|
}
|
|
|
|
var once sync.Once
|
2019-11-27 21:33:17 +00:00
|
|
|
commandResponse := make(chan bool)
|
2019-11-26 21:26:42 +00:00
|
|
|
|
2020-06-16 20:49:18 +00:00
|
|
|
remainingQuotaSize := quotaResult.GetRemainingSize()
|
|
|
|
|
2019-11-26 21:26:42 +00:00
|
|
|
go func() {
|
|
|
|
defer stdin.Close()
|
2021-05-31 19:45:29 +00:00
|
|
|
baseTransfer := common.NewBaseTransfer(nil, c.connection.BaseConnection, nil, command.fsPath, command.fsPath, sshDestPath,
|
2021-03-21 18:15:47 +00:00
|
|
|
common.TransferUpload, 0, 0, remainingQuotaSize, false, command.fs)
|
2020-08-31 04:45:22 +00:00
|
|
|
transfer := newTransfer(baseTransfer, nil, nil, nil)
|
2020-07-24 21:39:38 +00:00
|
|
|
|
2020-06-18 20:38:03 +00:00
|
|
|
w, e := transfer.copyFromReaderToWriter(stdin, c.connection.channel)
|
2020-07-24 21:39:38 +00:00
|
|
|
c.connection.Log(logger.LevelDebug, "command: %#v, copy from remote command to sdtin ended, written: %v, "+
|
2019-11-29 14:24:56 +00:00
|
|
|
"initial remaining quota: %v, err: %v", c.connection.command, w, remainingQuotaSize, e)
|
2019-11-26 21:26:42 +00:00
|
|
|
if e != nil {
|
|
|
|
once.Do(closeCmdOnError)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
2021-05-31 19:45:29 +00:00
|
|
|
baseTransfer := common.NewBaseTransfer(nil, c.connection.BaseConnection, nil, command.fsPath, command.fsPath, sshDestPath,
|
2021-03-21 18:15:47 +00:00
|
|
|
common.TransferDownload, 0, 0, 0, false, command.fs)
|
2020-08-31 04:45:22 +00:00
|
|
|
transfer := newTransfer(baseTransfer, nil, nil, nil)
|
2020-07-24 21:39:38 +00:00
|
|
|
|
2020-06-18 20:38:03 +00:00
|
|
|
w, e := transfer.copyFromReaderToWriter(c.connection.channel, stdout)
|
2020-07-24 21:39:38 +00:00
|
|
|
c.connection.Log(logger.LevelDebug, "command: %#v, copy from sdtout to remote command ended, written: %v err: %v",
|
2019-11-26 21:26:42 +00:00
|
|
|
c.connection.command, w, e)
|
|
|
|
if e != nil {
|
|
|
|
once.Do(closeCmdOnError)
|
|
|
|
}
|
2019-11-27 21:33:17 +00:00
|
|
|
commandResponse <- true
|
2019-11-26 21:26:42 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
2021-05-31 19:45:29 +00:00
|
|
|
baseTransfer := common.NewBaseTransfer(nil, c.connection.BaseConnection, nil, command.fsPath, command.fsPath, sshDestPath,
|
2021-03-21 18:15:47 +00:00
|
|
|
common.TransferDownload, 0, 0, 0, false, command.fs)
|
2020-08-31 04:45:22 +00:00
|
|
|
transfer := newTransfer(baseTransfer, nil, nil, nil)
|
2020-07-24 21:39:38 +00:00
|
|
|
|
2020-10-29 18:23:33 +00:00
|
|
|
w, e := transfer.copyFromReaderToWriter(c.connection.channel.(ssh.Channel).Stderr(), stderr)
|
2020-07-24 21:39:38 +00:00
|
|
|
c.connection.Log(logger.LevelDebug, "command: %#v, copy from sdterr to remote command ended, written: %v err: %v",
|
2019-11-26 21:26:42 +00:00
|
|
|
c.connection.command, w, e)
|
2019-12-30 17:37:50 +00:00
|
|
|
// os.ErrClosed means that the command is finished so we don't need to do anything
|
2019-12-29 23:10:03 +00:00
|
|
|
if (e != nil && !errors.Is(e, os.ErrClosed)) || w > 0 {
|
2019-11-26 21:26:42 +00:00
|
|
|
once.Do(closeCmdOnError)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2019-11-27 21:33:17 +00:00
|
|
|
<-commandResponse
|
2019-11-26 21:26:42 +00:00
|
|
|
err = command.cmd.Wait()
|
|
|
|
c.sendExitStatus(err)
|
2020-06-15 21:32:12 +00:00
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
numFiles, dirSize, errSize := c.getSizeForPath(command.fs, command.fsPath)
|
2020-06-15 21:32:12 +00:00
|
|
|
if errSize == nil {
|
|
|
|
c.updateQuota(sshDestPath, numFiles-initialFiles, dirSize-initialSize)
|
|
|
|
}
|
2020-07-24 21:39:38 +00:00
|
|
|
c.connection.Log(logger.LevelDebug, "command %#v finished for path %#v, initial files %v initial size %v "+
|
2020-06-15 21:32:12 +00:00
|
|
|
"current files %v current size %v size err: %v", c.connection.command, command.fsPath, initialFiles, initialSize,
|
|
|
|
numFiles, dirSize, errSize)
|
2021-03-21 18:15:47 +00:00
|
|
|
return c.connection.GetFsError(command.fs, err)
|
2019-11-26 21:26:42 +00:00
|
|
|
}
|
|
|
|
|
2020-06-15 21:32:12 +00:00
|
|
|
func (c *sshCommand) isSystemCommandAllowed() error {
|
|
|
|
sshDestPath := c.getDestPath()
|
|
|
|
if c.connection.User.IsVirtualFolder(sshDestPath) {
|
|
|
|
// overlapped virtual path are not allowed
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if c.connection.User.HasVirtualFoldersInside(sshDestPath) {
|
2020-07-24 21:39:38 +00:00
|
|
|
c.connection.Log(logger.LevelDebug, "command %#v is not allowed, path %#v has virtual folders inside it, user %#v",
|
2020-06-15 21:32:12 +00:00
|
|
|
c.command, sshDestPath, c.connection.User.Username)
|
|
|
|
return errUnsupportedConfig
|
2020-03-01 21:10:29 +00:00
|
|
|
}
|
2021-05-22 10:28:05 +00:00
|
|
|
for _, f := range c.connection.User.Filters.FilePatterns {
|
2020-06-15 21:32:12 +00:00
|
|
|
if f.Path == sshDestPath {
|
2020-07-24 21:39:38 +00:00
|
|
|
c.connection.Log(logger.LevelDebug,
|
2021-05-23 20:02:01 +00:00
|
|
|
"command %#v is not allowed inside folders with file patterns filters %#v user %#v",
|
2020-06-15 21:32:12 +00:00
|
|
|
c.command, sshDestPath, c.connection.User.Username)
|
2020-03-01 21:10:29 +00:00
|
|
|
return errUnsupportedConfig
|
|
|
|
}
|
2020-06-15 21:32:12 +00:00
|
|
|
if len(sshDestPath) > len(f.Path) {
|
|
|
|
if strings.HasPrefix(sshDestPath, f.Path+"/") || f.Path == "/" {
|
2020-07-24 21:39:38 +00:00
|
|
|
c.connection.Log(logger.LevelDebug,
|
2021-05-23 20:02:01 +00:00
|
|
|
"command %#v is not allowed it includes folders with file patterns filters %#v user %#v",
|
2020-06-15 21:32:12 +00:00
|
|
|
c.command, sshDestPath, c.connection.User.Username)
|
|
|
|
return errUnsupportedConfig
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(sshDestPath) < len(f.Path) {
|
|
|
|
if strings.HasPrefix(sshDestPath+"/", f.Path) || sshDestPath == "/" {
|
2020-07-24 21:39:38 +00:00
|
|
|
c.connection.Log(logger.LevelDebug,
|
2021-05-23 20:02:01 +00:00
|
|
|
"command %#v is not allowed inside folder with file patterns filters %#v user %#v",
|
2020-06-15 21:32:12 +00:00
|
|
|
c.command, sshDestPath, c.connection.User.Username)
|
2020-03-01 21:10:29 +00:00
|
|
|
return errUnsupportedConfig
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-26 21:26:42 +00:00
|
|
|
func (c *sshCommand) getSystemCommand() (systemCommand, error) {
|
|
|
|
command := systemCommand{
|
2020-06-15 21:32:12 +00:00
|
|
|
cmd: nil,
|
2021-03-21 18:15:47 +00:00
|
|
|
fs: nil,
|
2020-06-15 21:32:12 +00:00
|
|
|
fsPath: "",
|
|
|
|
quotaCheckPath: "",
|
2019-11-26 21:26:42 +00:00
|
|
|
}
|
|
|
|
args := make([]string, len(c.args))
|
|
|
|
copy(args, c.args)
|
2020-06-15 21:32:12 +00:00
|
|
|
var fsPath, quotaPath string
|
2021-03-21 18:15:47 +00:00
|
|
|
sshPath := c.getDestPath()
|
|
|
|
fs, err := c.connection.User.GetFilesystemForPath(sshPath, c.connection.ID)
|
|
|
|
if err != nil {
|
|
|
|
return command, err
|
|
|
|
}
|
2019-12-25 17:20:19 +00:00
|
|
|
if len(c.args) > 0 {
|
|
|
|
var err error
|
2021-03-21 18:15:47 +00:00
|
|
|
fsPath, err = fs.ResolvePath(sshPath)
|
2019-12-25 17:20:19 +00:00
|
|
|
if err != nil {
|
2021-03-21 18:15:47 +00:00
|
|
|
return command, c.connection.GetFsError(fs, err)
|
2019-12-25 17:20:19 +00:00
|
|
|
}
|
2020-06-15 21:32:12 +00:00
|
|
|
quotaPath = sshPath
|
2021-03-21 18:15:47 +00:00
|
|
|
fi, err := fs.Stat(fsPath)
|
2020-06-15 21:32:12 +00:00
|
|
|
if err == nil && fi.IsDir() {
|
|
|
|
// if the target is an existing dir the command will write inside this dir
|
|
|
|
// so we need to check the quota for this directory and not its parent dir
|
|
|
|
quotaPath = path.Join(sshPath, "fakecontent")
|
|
|
|
}
|
2021-03-01 21:10:45 +00:00
|
|
|
if strings.HasSuffix(sshPath, "/") && !strings.HasSuffix(fsPath, string(os.PathSeparator)) {
|
|
|
|
fsPath += string(os.PathSeparator)
|
|
|
|
c.connection.Log(logger.LevelDebug, "path separator added to fsPath %#v", fsPath)
|
|
|
|
}
|
2019-12-25 17:20:19 +00:00
|
|
|
args = args[:len(args)-1]
|
2020-06-15 21:32:12 +00:00
|
|
|
args = append(args, fsPath)
|
2019-12-25 17:20:19 +00:00
|
|
|
}
|
2020-06-15 21:32:12 +00:00
|
|
|
if err := c.isSystemCommandAllowed(); err != nil {
|
|
|
|
return command, errUnsupportedConfig
|
2020-02-24 17:54:35 +00:00
|
|
|
}
|
2019-11-29 14:24:56 +00:00
|
|
|
if c.command == "rsync" {
|
2020-03-03 22:25:23 +00:00
|
|
|
// we cannot avoid that rsync creates symlinks so if the user has the permission
|
2019-11-29 14:24:56 +00:00
|
|
|
// to create symlinks we add the option --safe-links to the received rsync command if
|
|
|
|
// it is not already set. This should prevent to create symlinks that point outside
|
|
|
|
// the home dir.
|
|
|
|
// If the user cannot create symlinks we add the option --munge-links, if it is not
|
|
|
|
// already set. This should make symlinks unusable (but manually recoverable)
|
2020-01-05 10:41:25 +00:00
|
|
|
if c.connection.User.HasPerm(dataprovider.PermCreateSymlinks, c.getDestPath()) {
|
2019-11-29 14:24:56 +00:00
|
|
|
if !utils.IsStringInSlice("--safe-links", args) {
|
|
|
|
args = append([]string{"--safe-links"}, args...)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if !utils.IsStringInSlice("--munge-links", args) {
|
|
|
|
args = append([]string{"--munge-links"}, args...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-24 21:39:38 +00:00
|
|
|
c.connection.Log(logger.LevelDebug, "new system command %#v, with args: %+v fs path %#v quota check path %#v",
|
2020-06-15 21:32:12 +00:00
|
|
|
c.command, args, fsPath, quotaPath)
|
2019-11-26 21:26:42 +00:00
|
|
|
cmd := exec.Command(c.command, args...)
|
|
|
|
uid := c.connection.User.GetUID()
|
|
|
|
gid := c.connection.User.GetGID()
|
|
|
|
cmd = wrapCmd(cmd, uid, gid)
|
|
|
|
command.cmd = cmd
|
2020-06-15 21:32:12 +00:00
|
|
|
command.fsPath = fsPath
|
|
|
|
command.quotaCheckPath = quotaPath
|
2021-03-21 18:15:47 +00:00
|
|
|
command.fs = fs
|
2019-11-26 21:26:42 +00:00
|
|
|
return command, nil
|
|
|
|
}
|
|
|
|
|
2020-06-13 20:48:51 +00:00
|
|
|
// for the supported commands, the destination path, if any, is the last argument
|
2019-11-18 22:30:37 +00:00
|
|
|
func (c *sshCommand) getDestPath() string {
|
|
|
|
if len(c.args) == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
2020-06-13 20:48:51 +00:00
|
|
|
return cleanCommandPath(c.args[len(c.args)-1])
|
|
|
|
}
|
|
|
|
|
|
|
|
// for the supported commands, the destination path, if any, is the second-last argument
|
|
|
|
func (c *sshCommand) getSourcePath() string {
|
|
|
|
if len(c.args) < 2 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return cleanCommandPath(c.args[len(c.args)-2])
|
|
|
|
}
|
|
|
|
|
|
|
|
func cleanCommandPath(name string) string {
|
|
|
|
name = strings.Trim(name, "'")
|
|
|
|
name = strings.Trim(name, "\"")
|
2020-08-11 21:56:10 +00:00
|
|
|
result := utils.CleanPath(name)
|
2020-06-13 20:48:51 +00:00
|
|
|
if strings.HasSuffix(name, "/") && !strings.HasSuffix(result, "/") {
|
2019-11-18 22:30:37 +00:00
|
|
|
result += "/"
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
func (c *sshCommand) getFsAndCopyPaths() (vfs.Fs, vfs.Fs, string, string, string, string, error) {
|
2020-06-13 20:48:51 +00:00
|
|
|
sshSourcePath := strings.TrimSuffix(c.getSourcePath(), "/")
|
|
|
|
sshDestPath := c.getDestPath()
|
|
|
|
if strings.HasSuffix(sshDestPath, "/") {
|
|
|
|
sshDestPath = path.Join(sshDestPath, path.Base(sshSourcePath))
|
|
|
|
}
|
2021-01-05 08:50:22 +00:00
|
|
|
if sshSourcePath == "" || sshDestPath == "" || len(c.args) != 2 {
|
2020-06-13 20:48:51 +00:00
|
|
|
err := errors.New("usage sftpgo-copy <source dir path> <destination dir path>")
|
2021-03-21 18:15:47 +00:00
|
|
|
return nil, nil, "", "", "", "", err
|
|
|
|
}
|
|
|
|
fsSrc, fsSourcePath, err := c.connection.GetFsAndResolvedPath(sshSourcePath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, "", "", "", "", err
|
|
|
|
}
|
|
|
|
fsDst, fsDestPath, err := c.connection.GetFsAndResolvedPath(sshDestPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, "", "", "", "", err
|
2020-06-13 20:48:51 +00:00
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
return fsSrc, fsDst, sshSourcePath, sshDestPath, fsSourcePath, fsDestPath, nil
|
2020-06-13 20:48:51 +00:00
|
|
|
}
|
|
|
|
|
2020-06-26 21:38:29 +00:00
|
|
|
func (c *sshCommand) hasCopyPermissions(sshSourcePath, sshDestPath string, srcInfo os.FileInfo) bool {
|
|
|
|
if !c.connection.User.HasPerm(dataprovider.PermListItems, path.Dir(sshSourcePath)) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if srcInfo.IsDir() {
|
|
|
|
return c.connection.User.HasPerm(dataprovider.PermCreateDirs, path.Dir(sshDestPath))
|
2020-11-17 18:36:39 +00:00
|
|
|
} else if srcInfo.Mode()&os.ModeSymlink != 0 {
|
2020-06-26 21:38:29 +00:00
|
|
|
return c.connection.User.HasPerm(dataprovider.PermCreateSymlinks, path.Dir(sshDestPath))
|
|
|
|
}
|
|
|
|
return c.connection.User.HasPerm(dataprovider.PermUpload, path.Dir(sshDestPath))
|
|
|
|
}
|
|
|
|
|
|
|
|
// fsSourcePath must be a directory
|
2021-03-21 18:15:47 +00:00
|
|
|
func (c *sshCommand) checkRecursiveCopyPermissions(fsSrc vfs.Fs, fsDst vfs.Fs, fsSourcePath, fsDestPath, sshDestPath string) error {
|
2020-06-26 21:38:29 +00:00
|
|
|
if !c.connection.User.HasPerm(dataprovider.PermCreateDirs, path.Dir(sshDestPath)) {
|
2020-07-24 21:39:38 +00:00
|
|
|
return common.ErrPermissionDenied
|
2020-06-26 21:38:29 +00:00
|
|
|
}
|
|
|
|
dstPerms := []string{
|
|
|
|
dataprovider.PermCreateDirs,
|
|
|
|
dataprovider.PermCreateSymlinks,
|
|
|
|
dataprovider.PermUpload,
|
|
|
|
}
|
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
err := fsSrc.Walk(fsSourcePath, func(walkedPath string, info os.FileInfo, err error) error {
|
2020-06-26 21:38:29 +00:00
|
|
|
if err != nil {
|
2021-03-21 18:15:47 +00:00
|
|
|
return c.connection.GetFsError(fsSrc, err)
|
2020-06-26 21:38:29 +00:00
|
|
|
}
|
|
|
|
fsDstSubPath := strings.Replace(walkedPath, fsSourcePath, fsDestPath, 1)
|
2021-03-21 18:15:47 +00:00
|
|
|
sshSrcSubPath := fsSrc.GetRelativePath(walkedPath)
|
|
|
|
sshDstSubPath := fsDst.GetRelativePath(fsDstSubPath)
|
2020-06-26 21:38:29 +00:00
|
|
|
// If the current dir has no subdirs with defined permissions inside it
|
|
|
|
// and it has all the possible permissions we can stop scanning
|
|
|
|
if !c.connection.User.HasPermissionsInside(path.Dir(sshSrcSubPath)) &&
|
|
|
|
!c.connection.User.HasPermissionsInside(path.Dir(sshDstSubPath)) {
|
|
|
|
if c.connection.User.HasPerm(dataprovider.PermListItems, path.Dir(sshSrcSubPath)) &&
|
|
|
|
c.connection.User.HasPerms(dstPerms, path.Dir(sshDstSubPath)) {
|
2020-07-24 21:39:38 +00:00
|
|
|
return common.ErrSkipPermissionsCheck
|
2020-06-26 21:38:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if !c.hasCopyPermissions(sshSrcSubPath, sshDstSubPath, info) {
|
2020-07-24 21:39:38 +00:00
|
|
|
return common.ErrPermissionDenied
|
2020-06-26 21:38:29 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2020-07-24 21:39:38 +00:00
|
|
|
if err == common.ErrSkipPermissionsCheck {
|
2020-06-26 21:38:29 +00:00
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
func (c *sshCommand) checkCopyPermissions(fsSrc vfs.Fs, fsDst vfs.Fs, fsSourcePath, fsDestPath, sshSourcePath, sshDestPath string, info os.FileInfo) error {
|
2020-06-26 21:38:29 +00:00
|
|
|
if info.IsDir() {
|
2021-03-21 18:15:47 +00:00
|
|
|
return c.checkRecursiveCopyPermissions(fsSrc, fsDst, fsSourcePath, fsDestPath, sshDestPath)
|
2020-06-26 21:38:29 +00:00
|
|
|
}
|
|
|
|
if !c.hasCopyPermissions(sshSourcePath, sshDestPath, info) {
|
2021-03-21 18:15:47 +00:00
|
|
|
return c.connection.GetPermissionDeniedError()
|
2020-06-26 21:38:29 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-13 20:48:51 +00:00
|
|
|
func (c *sshCommand) getRemovePath() (string, error) {
|
|
|
|
sshDestPath := c.getDestPath()
|
2021-01-05 08:50:22 +00:00
|
|
|
if sshDestPath == "" || len(c.args) != 1 {
|
2020-06-13 20:48:51 +00:00
|
|
|
err := errors.New("usage sftpgo-remove <destination path>")
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if len(sshDestPath) > 1 {
|
|
|
|
sshDestPath = strings.TrimSuffix(sshDestPath, "/")
|
|
|
|
}
|
|
|
|
return sshDestPath, nil
|
|
|
|
}
|
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
func (c *sshCommand) isLocalPath(virtualPath string) bool {
|
|
|
|
folder, err := c.connection.User.GetVirtualFolderForPath(virtualPath)
|
2020-06-13 20:48:51 +00:00
|
|
|
if err != nil {
|
2021-03-21 18:15:47 +00:00
|
|
|
return c.connection.User.FsConfig.Provider == vfs.LocalFilesystemProvider
|
2020-06-13 20:48:51 +00:00
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
return folder.FsConfig.Provider == vfs.LocalFilesystemProvider
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *sshCommand) isLocalCopy(virtualSourcePath, virtualTargetPath string) bool {
|
|
|
|
if !c.isLocalPath(virtualSourcePath) {
|
|
|
|
return false
|
2020-06-13 20:48:51 +00:00
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
|
|
|
|
return c.isLocalPath(virtualTargetPath)
|
2020-06-13 20:48:51 +00:00
|
|
|
}
|
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
func (c *sshCommand) checkCopyDestination(fs vfs.Fs, fsDestPath string) error {
|
|
|
|
_, err := fs.Lstat(fsDestPath)
|
2020-06-13 20:48:51 +00:00
|
|
|
if err == nil {
|
|
|
|
err := errors.New("invalid copy destination: cannot overwrite an existing file or directory")
|
|
|
|
return err
|
2021-03-21 18:15:47 +00:00
|
|
|
} else if !fs.IsNotExist(err) {
|
2020-06-13 20:48:51 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-16 20:49:18 +00:00
|
|
|
func (c *sshCommand) checkCopyQuota(numFiles int, filesSize int64, requestPath string) error {
|
2021-02-11 18:45:52 +00:00
|
|
|
quotaResult := c.connection.HasSpace(true, false, requestPath)
|
2020-06-16 20:49:18 +00:00
|
|
|
if !quotaResult.HasSpace {
|
2020-07-24 21:39:38 +00:00
|
|
|
return common.ErrQuotaExceeded
|
2020-06-16 20:49:18 +00:00
|
|
|
}
|
|
|
|
if quotaResult.QuotaFiles > 0 {
|
|
|
|
remainingFiles := quotaResult.GetRemainingFiles()
|
|
|
|
if remainingFiles < numFiles {
|
2020-07-24 21:39:38 +00:00
|
|
|
c.connection.Log(logger.LevelDebug, "copy not allowed, file limit will be exceeded, "+
|
2020-06-16 20:49:18 +00:00
|
|
|
"remaining files: %v to copy: %v", remainingFiles, numFiles)
|
2020-07-24 21:39:38 +00:00
|
|
|
return common.ErrQuotaExceeded
|
2020-06-16 20:49:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if quotaResult.QuotaSize > 0 {
|
|
|
|
remainingSize := quotaResult.GetRemainingSize()
|
|
|
|
if remainingSize < filesSize {
|
2020-07-24 21:39:38 +00:00
|
|
|
c.connection.Log(logger.LevelDebug, "copy not allowed, size limit will be exceeded, "+
|
2020-06-16 20:49:18 +00:00
|
|
|
"remaining size: %v to copy: %v", remainingSize, filesSize)
|
2020-07-24 21:39:38 +00:00
|
|
|
return common.ErrQuotaExceeded
|
2020-06-16 20:49:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
func (c *sshCommand) getSizeForPath(fs vfs.Fs, name string) (int, int64, error) {
|
2020-06-15 21:32:12 +00:00
|
|
|
if dataprovider.GetQuotaTracking() > 0 {
|
2021-03-21 18:15:47 +00:00
|
|
|
fi, err := fs.Lstat(name)
|
2020-06-15 21:32:12 +00:00
|
|
|
if err != nil {
|
2021-03-21 18:15:47 +00:00
|
|
|
if fs.IsNotExist(err) {
|
2020-06-15 21:32:12 +00:00
|
|
|
return 0, 0, nil
|
|
|
|
}
|
2020-07-24 21:39:38 +00:00
|
|
|
c.connection.Log(logger.LevelDebug, "unable to stat %#v error: %v", name, err)
|
2020-06-15 21:32:12 +00:00
|
|
|
return 0, 0, err
|
|
|
|
}
|
|
|
|
if fi.IsDir() {
|
2021-03-21 18:15:47 +00:00
|
|
|
files, size, err := fs.GetDirSize(name)
|
2020-06-15 21:32:12 +00:00
|
|
|
if err != nil {
|
2020-07-24 21:39:38 +00:00
|
|
|
c.connection.Log(logger.LevelDebug, "unable to get size for dir %#v error: %v", name, err)
|
2020-06-15 21:32:12 +00:00
|
|
|
}
|
|
|
|
return files, size, err
|
|
|
|
} else if fi.Mode().IsRegular() {
|
|
|
|
return 1, fi.Size(), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0, 0, nil
|
|
|
|
}
|
|
|
|
|
2019-11-18 22:30:37 +00:00
|
|
|
func (c *sshCommand) sendErrorResponse(err error) error {
|
2021-03-21 18:15:47 +00:00
|
|
|
errorString := fmt.Sprintf("%v: %v %v\n", c.command, c.getDestPath(), err)
|
2020-04-30 12:23:55 +00:00
|
|
|
c.connection.channel.Write([]byte(errorString)) //nolint:errcheck
|
2019-11-18 22:30:37 +00:00
|
|
|
c.sendExitStatus(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *sshCommand) sendExitStatus(err error) {
|
|
|
|
status := uint32(0)
|
2020-06-13 20:48:51 +00:00
|
|
|
cmdPath := c.getDestPath()
|
|
|
|
targetPath := ""
|
|
|
|
if c.command == "sftpgo-copy" {
|
|
|
|
targetPath = cmdPath
|
|
|
|
cmdPath = c.getSourcePath()
|
|
|
|
}
|
2019-11-18 22:30:37 +00:00
|
|
|
if err != nil {
|
|
|
|
status = uint32(1)
|
2020-07-24 21:39:38 +00:00
|
|
|
c.connection.Log(logger.LevelWarn, "command failed: %#v args: %v user: %v err: %v",
|
2019-11-19 10:38:39 +00:00
|
|
|
c.command, c.args, c.connection.User.Username, err)
|
2019-11-18 22:30:37 +00:00
|
|
|
}
|
|
|
|
exitStatus := sshSubsystemExitStatus{
|
|
|
|
Status: status,
|
|
|
|
}
|
2021-05-26 05:48:37 +00:00
|
|
|
_, errClose := c.connection.channel.(ssh.Channel).SendRequest("exit-status", false, ssh.Marshal(&exitStatus))
|
|
|
|
c.connection.Log(logger.LevelDebug, "exit status sent, error: %v", errClose)
|
2019-11-18 22:30:37 +00:00
|
|
|
c.connection.channel.Close()
|
2019-11-26 21:26:42 +00:00
|
|
|
// for scp we notify single uploads/downloads
|
2020-04-30 12:23:55 +00:00
|
|
|
if c.command != scpCmdName {
|
2020-04-03 17:25:38 +00:00
|
|
|
metrics.SSHCommandCompleted(err)
|
2021-03-21 18:15:47 +00:00
|
|
|
if cmdPath != "" {
|
|
|
|
_, p, errFs := c.connection.GetFsAndResolvedPath(cmdPath)
|
|
|
|
if errFs == nil {
|
2020-06-13 20:48:51 +00:00
|
|
|
cmdPath = p
|
|
|
|
}
|
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
if targetPath != "" {
|
|
|
|
_, p, errFs := c.connection.GetFsAndResolvedPath(targetPath)
|
|
|
|
if errFs == nil {
|
2020-06-13 20:48:51 +00:00
|
|
|
targetPath = p
|
2020-01-09 11:00:37 +00:00
|
|
|
}
|
|
|
|
}
|
2021-05-26 05:48:37 +00:00
|
|
|
common.ExecuteActionNotification(&c.connection.User, common.OperationSSHCmd, cmdPath, c.getDestPath(), targetPath, c.command,
|
|
|
|
common.ProtocolSSH, 0, err)
|
2021-06-01 20:28:43 +00:00
|
|
|
if err == nil {
|
|
|
|
logger.CommandLog(sshCommandLogSender, cmdPath, targetPath, c.connection.User.Username, "", c.connection.ID,
|
|
|
|
common.ProtocolSSH, -1, -1, "", "", c.connection.command, -1, c.connection.GetRemoteAddress())
|
|
|
|
}
|
2019-11-26 21:26:42 +00:00
|
|
|
}
|
2019-11-18 22:30:37 +00:00
|
|
|
}
|
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
func (c *sshCommand) computeHashForFile(fs vfs.Fs, hasher hash.Hash, path string) (string, error) {
|
2019-11-18 22:30:37 +00:00
|
|
|
hash := ""
|
2021-03-21 18:15:47 +00:00
|
|
|
f, r, _, err := fs.Open(path, 0)
|
2019-11-18 22:30:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return hash, err
|
|
|
|
}
|
2020-12-13 14:11:55 +00:00
|
|
|
var reader io.ReadCloser
|
|
|
|
if f != nil {
|
|
|
|
reader = f
|
|
|
|
} else {
|
|
|
|
reader = r
|
|
|
|
}
|
|
|
|
defer reader.Close()
|
|
|
|
_, err = io.Copy(hasher, reader)
|
2019-11-18 22:30:37 +00:00
|
|
|
if err == nil {
|
|
|
|
hash = fmt.Sprintf("%x", hasher.Sum(nil))
|
|
|
|
}
|
|
|
|
return hash, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseCommandPayload(command string) (string, []string, error) {
|
2020-02-14 15:17:32 +00:00
|
|
|
parts, err := shlex.Split(command)
|
|
|
|
if err == nil && len(parts) == 0 {
|
|
|
|
err = fmt.Errorf("invalid command: %#v", command)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return "", []string{}, err
|
|
|
|
}
|
2019-11-18 22:30:37 +00:00
|
|
|
if len(parts) < 2 {
|
|
|
|
return parts[0], []string{}, nil
|
|
|
|
}
|
2020-02-14 15:17:32 +00:00
|
|
|
return parts[0], parts[1:], nil
|
2019-11-18 22:30:37 +00:00
|
|
|
}
|