2019-07-20 10:26:52 +00:00
|
|
|
package sftpd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/drakkan/sftpgo/utils"
|
2019-08-04 07:37:58 +00:00
|
|
|
"github.com/rs/xid"
|
2019-09-11 14:29:56 +00:00
|
|
|
"golang.org/x/crypto/ssh"
|
2019-07-20 10:26:52 +00:00
|
|
|
|
|
|
|
"github.com/drakkan/sftpgo/dataprovider"
|
|
|
|
"github.com/drakkan/sftpgo/logger"
|
|
|
|
|
|
|
|
"github.com/pkg/sftp"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Connection details for an authenticated user
|
|
|
|
type Connection struct {
|
2019-07-30 18:51:29 +00:00
|
|
|
// Unique identifier for the connection
|
|
|
|
ID string
|
|
|
|
// logged in user's details
|
|
|
|
User dataprovider.User
|
|
|
|
// client's version string
|
2019-07-20 10:26:52 +00:00
|
|
|
ClientVersion string
|
2019-07-30 18:51:29 +00:00
|
|
|
// Remote address for this connection
|
|
|
|
RemoteAddr net.Addr
|
|
|
|
// start time for this connection
|
|
|
|
StartTime time.Time
|
|
|
|
// last activity for this connection
|
|
|
|
lastActivity time.Time
|
2019-08-24 12:41:15 +00:00
|
|
|
protocol string
|
2019-07-30 18:51:29 +00:00
|
|
|
lock *sync.Mutex
|
2019-09-11 07:41:46 +00:00
|
|
|
netConn net.Conn
|
2019-09-11 14:29:56 +00:00
|
|
|
channel ssh.Channel
|
2019-11-18 22:30:37 +00:00
|
|
|
command string
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
2019-09-06 09:23:06 +00:00
|
|
|
// Log outputs a log entry to the configured logger
|
2019-09-06 13:19:01 +00:00
|
|
|
func (c Connection) Log(level logger.LogLevel, sender string, format string, v ...interface{}) {
|
|
|
|
logger.Log(level, sender, c.ID, format, v...)
|
2019-09-06 09:23:06 +00:00
|
|
|
}
|
|
|
|
|
2019-07-20 10:26:52 +00:00
|
|
|
// Fileread creates a reader for a file on the system and returns the reader back.
|
|
|
|
func (c Connection) Fileread(request *sftp.Request) (io.ReaderAt, error) {
|
|
|
|
updateConnectionActivity(c.ID)
|
|
|
|
|
|
|
|
p, err := c.buildPath(request.Filepath)
|
|
|
|
if err != nil {
|
2019-11-15 11:15:07 +00:00
|
|
|
return nil, getSFTPErrorFromOSError(err)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
2019-12-25 17:20:19 +00:00
|
|
|
if !c.User.HasPerm(dataprovider.PermDownload, filepath.Dir(p)) {
|
|
|
|
return nil, sftp.ErrSSHFxPermissionDenied
|
|
|
|
}
|
2019-07-20 10:26:52 +00:00
|
|
|
|
|
|
|
c.lock.Lock()
|
|
|
|
defer c.lock.Unlock()
|
|
|
|
|
2019-11-15 11:15:07 +00:00
|
|
|
if _, err := os.Stat(p); err != nil {
|
|
|
|
return nil, getSFTPErrorFromOSError(err)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
file, err := os.Open(p)
|
|
|
|
if err != nil {
|
2019-11-15 11:15:07 +00:00
|
|
|
c.Log(logger.LevelWarn, logSender, "could not open file %#v for reading: %v", p, err)
|
|
|
|
return nil, getSFTPErrorFromOSError(err)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
2019-09-06 13:19:01 +00:00
|
|
|
c.Log(logger.LevelDebug, logSender, "fileread requested for path: %#v", p)
|
2019-07-20 10:26:52 +00:00
|
|
|
|
|
|
|
transfer := Transfer{
|
2019-10-09 15:33:30 +00:00
|
|
|
file: file,
|
|
|
|
path: p,
|
|
|
|
start: time.Now(),
|
|
|
|
bytesSent: 0,
|
|
|
|
bytesReceived: 0,
|
|
|
|
user: c.User,
|
|
|
|
connectionID: c.ID,
|
|
|
|
transferType: transferDownload,
|
|
|
|
lastActivity: time.Now(),
|
|
|
|
isNewFile: false,
|
|
|
|
protocol: c.protocol,
|
|
|
|
transferError: nil,
|
|
|
|
isFinished: false,
|
|
|
|
minWriteOffset: 0,
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
addTransfer(&transfer)
|
|
|
|
return &transfer, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filewrite handles the write actions for a file on the system.
|
|
|
|
func (c Connection) Filewrite(request *sftp.Request) (io.WriterAt, error) {
|
|
|
|
updateConnectionActivity(c.ID)
|
|
|
|
p, err := c.buildPath(request.Filepath)
|
|
|
|
if err != nil {
|
2019-11-15 11:15:07 +00:00
|
|
|
return nil, getSFTPErrorFromOSError(err)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
2019-08-04 07:37:58 +00:00
|
|
|
filePath := p
|
2019-10-09 15:33:30 +00:00
|
|
|
if isAtomicUploadEnabled() {
|
2019-08-04 07:37:58 +00:00
|
|
|
filePath = getUploadTempFilePath(p)
|
|
|
|
}
|
|
|
|
|
2019-07-20 10:26:52 +00:00
|
|
|
c.lock.Lock()
|
|
|
|
defer c.lock.Unlock()
|
|
|
|
|
|
|
|
stat, statErr := os.Stat(p)
|
|
|
|
if os.IsNotExist(statErr) {
|
2019-12-25 17:20:19 +00:00
|
|
|
if !c.User.HasPerm(dataprovider.PermUpload, filepath.Dir(p)) {
|
|
|
|
return nil, sftp.ErrSSHFxPermissionDenied
|
|
|
|
}
|
2019-08-04 07:37:58 +00:00
|
|
|
return c.handleSFTPUploadToNewFile(p, filePath)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if statErr != nil {
|
2019-09-06 13:19:01 +00:00
|
|
|
c.Log(logger.LevelError, logSender, "error performing file stat %#v: %v", p, statErr)
|
2019-11-15 11:15:07 +00:00
|
|
|
return nil, getSFTPErrorFromOSError(err)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
2019-08-04 07:37:58 +00:00
|
|
|
// This happen if we upload a file that has the same name of an existing directory
|
2019-07-20 10:26:52 +00:00
|
|
|
if stat.IsDir() {
|
2019-09-06 13:19:01 +00:00
|
|
|
c.Log(logger.LevelWarn, logSender, "attempted to open a directory for writing to: %#v", p)
|
2019-10-14 20:44:57 +00:00
|
|
|
return nil, sftp.ErrSSHFxOpUnsupported
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
2019-12-25 17:20:19 +00:00
|
|
|
if !c.User.HasPerm(dataprovider.PermOverwrite, filepath.Dir(filePath)) {
|
2019-10-14 20:44:57 +00:00
|
|
|
return nil, sftp.ErrSSHFxPermissionDenied
|
2019-09-17 06:53:45 +00:00
|
|
|
}
|
|
|
|
|
2019-08-04 07:37:58 +00:00
|
|
|
return c.handleSFTPUploadToExistingFile(request.Pflags(), p, filePath, stat.Size())
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Filecmd hander for basic SFTP system calls related to files, but not anything to do with reading
|
|
|
|
// or writing to those files.
|
|
|
|
func (c Connection) Filecmd(request *sftp.Request) error {
|
|
|
|
updateConnectionActivity(c.ID)
|
|
|
|
|
|
|
|
p, err := c.buildPath(request.Filepath)
|
|
|
|
if err != nil {
|
2019-11-15 11:15:07 +00:00
|
|
|
return getSFTPErrorFromOSError(err)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
2019-07-20 22:19:17 +00:00
|
|
|
target, err := c.getSFTPCmdTargetPath(request.Target)
|
|
|
|
if err != nil {
|
2019-11-15 11:15:07 +00:00
|
|
|
return err
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
2019-09-06 13:19:01 +00:00
|
|
|
c.Log(logger.LevelDebug, logSender, "new cmd, method: %v, sourcePath: %#v, targetPath: %#v", request.Method,
|
2019-07-20 22:19:17 +00:00
|
|
|
p, target)
|
2019-07-20 10:26:52 +00:00
|
|
|
|
|
|
|
switch request.Method {
|
|
|
|
case "Setstat":
|
2019-11-15 11:15:07 +00:00
|
|
|
return c.handleSFTPSetstat(p, request)
|
2019-07-20 10:26:52 +00:00
|
|
|
case "Rename":
|
2019-11-15 11:15:07 +00:00
|
|
|
if err = c.handleSFTPRename(p, target); err != nil {
|
2019-07-20 22:19:17 +00:00
|
|
|
return err
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
break
|
|
|
|
case "Rmdir":
|
2019-07-20 22:19:17 +00:00
|
|
|
return c.handleSFTPRmdir(p)
|
2019-07-20 10:26:52 +00:00
|
|
|
|
|
|
|
case "Mkdir":
|
2019-07-20 22:19:17 +00:00
|
|
|
err = c.handleSFTPMkdir(p)
|
2019-07-20 10:26:52 +00:00
|
|
|
if err != nil {
|
2019-07-20 22:19:17 +00:00
|
|
|
return err
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
break
|
|
|
|
case "Symlink":
|
2019-11-15 11:15:07 +00:00
|
|
|
if err = c.handleSFTPSymlink(p, target); err != nil {
|
2019-07-20 22:19:17 +00:00
|
|
|
return err
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
break
|
|
|
|
case "Remove":
|
2019-07-20 22:19:17 +00:00
|
|
|
return c.handleSFTPRemove(p)
|
2019-07-20 10:26:52 +00:00
|
|
|
|
|
|
|
default:
|
2019-10-14 20:44:57 +00:00
|
|
|
return sftp.ErrSSHFxOpUnsupported
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var fileLocation = p
|
|
|
|
if target != "" {
|
|
|
|
fileLocation = target
|
|
|
|
}
|
|
|
|
|
2019-07-20 22:19:17 +00:00
|
|
|
// we return if we remove a file or a dir so source path or target path always exists here
|
2019-07-20 10:26:52 +00:00
|
|
|
utils.SetPathPermissions(fileLocation, c.User.GetUID(), c.User.GetGID())
|
|
|
|
|
2019-10-14 20:44:57 +00:00
|
|
|
return sftp.ErrSSHFxOk
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Filelist is the handler for SFTP filesystem list calls. This will handle calls to list the contents of
|
|
|
|
// a directory as well as perform file/folder stat calls.
|
|
|
|
func (c Connection) Filelist(request *sftp.Request) (sftp.ListerAt, error) {
|
|
|
|
updateConnectionActivity(c.ID)
|
|
|
|
p, err := c.buildPath(request.Filepath)
|
|
|
|
if err != nil {
|
2019-11-15 11:15:07 +00:00
|
|
|
return nil, getSFTPErrorFromOSError(err)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch request.Method {
|
|
|
|
case "List":
|
2019-12-25 17:20:19 +00:00
|
|
|
if !c.User.HasPerm(dataprovider.PermListItems, p) {
|
2019-10-14 20:44:57 +00:00
|
|
|
return nil, sftp.ErrSSHFxPermissionDenied
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
2019-09-06 13:19:01 +00:00
|
|
|
c.Log(logger.LevelDebug, logSender, "requested list file for dir: %#v", p)
|
2019-07-20 10:26:52 +00:00
|
|
|
|
|
|
|
files, err := ioutil.ReadDir(p)
|
2019-11-15 11:15:07 +00:00
|
|
|
if err != nil {
|
|
|
|
c.Log(logger.LevelWarn, logSender, "error listing directory: %#v", err)
|
|
|
|
return nil, getSFTPErrorFromOSError(err)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
2019-07-30 18:51:29 +00:00
|
|
|
return listerAt(files), nil
|
2019-07-20 10:26:52 +00:00
|
|
|
case "Stat":
|
2019-12-25 17:20:19 +00:00
|
|
|
if !c.User.HasPerm(dataprovider.PermListItems, filepath.Dir(p)) {
|
2019-10-14 20:44:57 +00:00
|
|
|
return nil, sftp.ErrSSHFxPermissionDenied
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
2019-11-16 09:23:41 +00:00
|
|
|
c.Log(logger.LevelDebug, logSender, "requested stat for path: %#v", p)
|
2019-07-20 10:26:52 +00:00
|
|
|
s, err := os.Stat(p)
|
2019-11-15 11:15:07 +00:00
|
|
|
if err != nil {
|
2019-11-16 09:23:41 +00:00
|
|
|
c.Log(logger.LevelWarn, logSender, "error running stat on path: %#v", err)
|
2019-11-15 11:15:07 +00:00
|
|
|
return nil, getSFTPErrorFromOSError(err)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
2019-07-30 18:51:29 +00:00
|
|
|
return listerAt([]os.FileInfo{s}), nil
|
2019-07-20 10:26:52 +00:00
|
|
|
default:
|
2019-10-14 20:44:57 +00:00
|
|
|
return nil, sftp.ErrSSHFxOpUnsupported
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-20 22:19:17 +00:00
|
|
|
func (c Connection) getSFTPCmdTargetPath(requestTarget string) (string, error) {
|
|
|
|
var target string
|
|
|
|
// If a target is provided in this request validate that it is going to the correct
|
2019-11-15 11:15:07 +00:00
|
|
|
// location for the server. If it is not, return an error
|
|
|
|
if len(requestTarget) > 0 {
|
2019-07-20 22:19:17 +00:00
|
|
|
var err error
|
|
|
|
target, err = c.buildPath(requestTarget)
|
|
|
|
if err != nil {
|
2019-11-15 11:15:07 +00:00
|
|
|
return target, getSFTPErrorFromOSError(err)
|
2019-07-20 22:19:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return target, nil
|
|
|
|
}
|
|
|
|
|
2019-11-15 11:15:07 +00:00
|
|
|
func (c Connection) handleSFTPSetstat(path string, request *sftp.Request) error {
|
|
|
|
if setstatMode == 1 {
|
|
|
|
return nil
|
|
|
|
}
|
2019-12-04 07:31:47 +00:00
|
|
|
if len(request.Attrs) < 1 {
|
|
|
|
c.Log(logger.LevelInfo, logSender, "cannot handle Setstat request with no attrs, this is probably a buggy client: %v",
|
|
|
|
c.ClientVersion)
|
|
|
|
return sftp.ErrSSHFxBadMessage
|
|
|
|
}
|
2019-12-25 17:20:19 +00:00
|
|
|
pathForPerms := path
|
|
|
|
if fi, err := os.Lstat(path); err == nil {
|
|
|
|
if fi.IsDir() {
|
|
|
|
pathForPerms = filepath.Dir(path)
|
|
|
|
}
|
|
|
|
}
|
2019-11-15 11:15:07 +00:00
|
|
|
attrFlags := request.AttrFlags()
|
|
|
|
if attrFlags.Permissions {
|
2019-12-25 17:20:19 +00:00
|
|
|
if !c.User.HasPerm(dataprovider.PermChmod, pathForPerms) {
|
2019-11-15 11:15:07 +00:00
|
|
|
return sftp.ErrSSHFxPermissionDenied
|
|
|
|
}
|
|
|
|
fileMode := request.Attributes().FileMode()
|
|
|
|
if err := os.Chmod(path, fileMode); err != nil {
|
|
|
|
c.Log(logger.LevelWarn, logSender, "failed to chmod path %#v, mode: %v, err: %v", path, fileMode.String(), err)
|
|
|
|
return getSFTPErrorFromOSError(err)
|
|
|
|
}
|
2019-11-19 10:38:39 +00:00
|
|
|
logger.CommandLog(chmodLogSender, path, "", c.User.Username, fileMode.String(), c.ID, c.protocol, -1, -1, "", "", "")
|
2019-11-15 11:15:07 +00:00
|
|
|
return nil
|
|
|
|
} else if attrFlags.UidGid {
|
2019-12-25 17:20:19 +00:00
|
|
|
if !c.User.HasPerm(dataprovider.PermChown, pathForPerms) {
|
2019-11-15 11:15:07 +00:00
|
|
|
return sftp.ErrSSHFxPermissionDenied
|
|
|
|
}
|
|
|
|
uid := int(request.Attributes().UID)
|
|
|
|
gid := int(request.Attributes().GID)
|
|
|
|
if err := os.Chown(path, uid, gid); err != nil {
|
|
|
|
c.Log(logger.LevelWarn, logSender, "failed to chown path %#v, uid: %v, gid: %v, err: %v", path, uid, gid, err)
|
|
|
|
return getSFTPErrorFromOSError(err)
|
|
|
|
}
|
2019-11-19 10:38:39 +00:00
|
|
|
logger.CommandLog(chownLogSender, path, "", c.User.Username, "", c.ID, c.protocol, uid, gid, "", "", "")
|
2019-11-16 09:23:41 +00:00
|
|
|
return nil
|
|
|
|
} else if attrFlags.Acmodtime {
|
2019-12-25 17:20:19 +00:00
|
|
|
if !c.User.HasPerm(dataprovider.PermChtimes, pathForPerms) {
|
2019-11-16 09:23:41 +00:00
|
|
|
return sftp.ErrSSHFxPermissionDenied
|
|
|
|
}
|
|
|
|
dateFormat := "2006-01-02T15:04:05" // YYYY-MM-DDTHH:MM:SS
|
|
|
|
accessTime := time.Unix(int64(request.Attributes().Atime), 0)
|
|
|
|
modificationTime := time.Unix(int64(request.Attributes().Mtime), 0)
|
|
|
|
accessTimeString := accessTime.Format(dateFormat)
|
|
|
|
modificationTimeString := modificationTime.Format(dateFormat)
|
|
|
|
if err := os.Chtimes(path, accessTime, modificationTime); err != nil {
|
|
|
|
c.Log(logger.LevelWarn, logSender, "failed to chtimes for path %#v, access time: %v, modification time: %v, err: %v",
|
|
|
|
path, accessTime, modificationTime, err)
|
|
|
|
return getSFTPErrorFromOSError(err)
|
|
|
|
}
|
|
|
|
logger.CommandLog(chtimesLogSender, path, "", c.User.Username, "", c.ID, c.protocol, -1, -1, accessTimeString,
|
2019-11-19 10:38:39 +00:00
|
|
|
modificationTimeString, "")
|
2019-11-15 11:15:07 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-20 22:19:17 +00:00
|
|
|
func (c Connection) handleSFTPRename(sourcePath string, targetPath string) error {
|
2019-12-29 06:43:59 +00:00
|
|
|
if c.User.GetRelativePath(sourcePath) == "/" {
|
|
|
|
c.Log(logger.LevelWarn, logSender, "renaming root dir is not allowed")
|
|
|
|
return sftp.ErrSSHFxPermissionDenied
|
|
|
|
}
|
2019-12-25 17:20:19 +00:00
|
|
|
if !c.User.HasPerm(dataprovider.PermRename, filepath.Dir(targetPath)) {
|
2019-10-14 20:44:57 +00:00
|
|
|
return sftp.ErrSSHFxPermissionDenied
|
2019-07-20 22:19:17 +00:00
|
|
|
}
|
|
|
|
if err := os.Rename(sourcePath, targetPath); err != nil {
|
2019-11-15 11:15:07 +00:00
|
|
|
c.Log(logger.LevelWarn, logSender, "failed to rename file, source: %#v target: %#v: %v", sourcePath, targetPath, err)
|
|
|
|
return getSFTPErrorFromOSError(err)
|
2019-07-20 22:19:17 +00:00
|
|
|
}
|
2019-11-19 10:38:39 +00:00
|
|
|
logger.CommandLog(renameLogSender, sourcePath, targetPath, c.User.Username, "", c.ID, c.protocol, -1, -1, "", "", "")
|
2019-11-26 21:26:42 +00:00
|
|
|
go executeAction(operationRename, c.User.Username, sourcePath, targetPath, "")
|
2019-07-20 22:19:17 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c Connection) handleSFTPRmdir(path string) error {
|
2019-12-29 06:43:59 +00:00
|
|
|
if c.User.GetRelativePath(path) == "/" {
|
|
|
|
c.Log(logger.LevelWarn, logSender, "removing root dir is not allowed")
|
|
|
|
return sftp.ErrSSHFxPermissionDenied
|
|
|
|
}
|
2019-12-25 17:20:19 +00:00
|
|
|
if !c.User.HasPerm(dataprovider.PermDelete, filepath.Dir(path)) {
|
2019-10-14 20:44:57 +00:00
|
|
|
return sftp.ErrSSHFxPermissionDenied
|
2019-07-20 22:19:17 +00:00
|
|
|
}
|
|
|
|
|
2019-10-16 05:48:22 +00:00
|
|
|
var fi os.FileInfo
|
|
|
|
var err error
|
|
|
|
if fi, err = os.Lstat(path); err != nil {
|
2019-11-15 11:15:07 +00:00
|
|
|
c.Log(logger.LevelWarn, logSender, "failed to remove a dir %#v: stat error: %v", path, err)
|
|
|
|
return getSFTPErrorFromOSError(err)
|
2019-07-20 22:19:17 +00:00
|
|
|
}
|
2019-10-16 05:48:22 +00:00
|
|
|
if !fi.IsDir() || fi.Mode()&os.ModeSymlink == os.ModeSymlink {
|
|
|
|
c.Log(logger.LevelDebug, logSender, "cannot remove %#v is not a directory", path)
|
|
|
|
return sftp.ErrSSHFxFailure
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = os.Remove(path); err != nil {
|
2019-11-15 11:15:07 +00:00
|
|
|
c.Log(logger.LevelWarn, logSender, "failed to remove directory %#v: %v", path, err)
|
|
|
|
return getSFTPErrorFromOSError(err)
|
2019-07-20 22:19:17 +00:00
|
|
|
}
|
|
|
|
|
2019-11-19 10:38:39 +00:00
|
|
|
logger.CommandLog(rmdirLogSender, path, "", c.User.Username, "", c.ID, c.protocol, -1, -1, "", "", "")
|
2019-10-14 20:44:57 +00:00
|
|
|
return sftp.ErrSSHFxOk
|
2019-07-20 22:19:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c Connection) handleSFTPSymlink(sourcePath string, targetPath string) error {
|
2019-12-29 06:43:59 +00:00
|
|
|
if c.User.GetRelativePath(sourcePath) == "/" {
|
|
|
|
c.Log(logger.LevelWarn, logSender, "symlinking root dir is not allowed")
|
|
|
|
return sftp.ErrSSHFxPermissionDenied
|
|
|
|
}
|
2019-12-25 17:20:19 +00:00
|
|
|
if !c.User.HasPerm(dataprovider.PermCreateSymlinks, filepath.Dir(targetPath)) {
|
2019-10-14 20:44:57 +00:00
|
|
|
return sftp.ErrSSHFxPermissionDenied
|
2019-07-20 22:19:17 +00:00
|
|
|
}
|
|
|
|
if err := os.Symlink(sourcePath, targetPath); err != nil {
|
2019-09-06 13:19:01 +00:00
|
|
|
c.Log(logger.LevelWarn, logSender, "failed to create symlink %#v -> %#v: %v", sourcePath, targetPath, err)
|
2019-11-15 11:15:07 +00:00
|
|
|
return getSFTPErrorFromOSError(err)
|
2019-07-20 22:19:17 +00:00
|
|
|
}
|
|
|
|
|
2019-11-19 10:38:39 +00:00
|
|
|
logger.CommandLog(symlinkLogSender, sourcePath, targetPath, c.User.Username, "", c.ID, c.protocol, -1, -1, "", "", "")
|
2019-07-20 22:19:17 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c Connection) handleSFTPMkdir(path string) error {
|
2019-12-25 17:20:19 +00:00
|
|
|
if !c.User.HasPerm(dataprovider.PermCreateDirs, filepath.Dir(path)) {
|
2019-10-14 20:44:57 +00:00
|
|
|
return sftp.ErrSSHFxPermissionDenied
|
2019-07-20 22:19:17 +00:00
|
|
|
}
|
2019-10-16 05:48:22 +00:00
|
|
|
if err := os.Mkdir(path, 0777); err != nil {
|
2019-11-15 11:15:07 +00:00
|
|
|
c.Log(logger.LevelWarn, logSender, "error creating missing dir: %#v error: %v", path, err)
|
|
|
|
return getSFTPErrorFromOSError(err)
|
2019-07-20 22:19:17 +00:00
|
|
|
}
|
2019-10-16 05:48:22 +00:00
|
|
|
utils.SetPathPermissions(path, c.User.GetUID(), c.User.GetGID())
|
|
|
|
|
2019-11-19 10:38:39 +00:00
|
|
|
logger.CommandLog(mkdirLogSender, path, "", c.User.Username, "", c.ID, c.protocol, -1, -1, "", "", "")
|
2019-07-20 22:19:17 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c Connection) handleSFTPRemove(path string) error {
|
2019-12-25 17:20:19 +00:00
|
|
|
if !c.User.HasPerm(dataprovider.PermDelete, filepath.Dir(path)) {
|
2019-10-14 20:44:57 +00:00
|
|
|
return sftp.ErrSSHFxPermissionDenied
|
2019-07-20 22:19:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var size int64
|
|
|
|
var fi os.FileInfo
|
|
|
|
var err error
|
|
|
|
if fi, err = os.Lstat(path); err != nil {
|
2019-11-15 11:15:07 +00:00
|
|
|
c.Log(logger.LevelWarn, logSender, "failed to remove a file %#v: stat error: %v", path, err)
|
|
|
|
return getSFTPErrorFromOSError(err)
|
2019-07-20 22:19:17 +00:00
|
|
|
}
|
2019-10-16 05:48:22 +00:00
|
|
|
if fi.IsDir() && fi.Mode()&os.ModeSymlink != os.ModeSymlink {
|
|
|
|
c.Log(logger.LevelDebug, logSender, "cannot remove %#v is not a file/symlink", path)
|
|
|
|
return sftp.ErrSSHFxFailure
|
|
|
|
}
|
2019-07-20 22:19:17 +00:00
|
|
|
size = fi.Size()
|
|
|
|
if err := os.Remove(path); err != nil {
|
2019-11-15 11:15:07 +00:00
|
|
|
c.Log(logger.LevelWarn, logSender, "failed to remove a file/symlink %#v: %v", path, err)
|
|
|
|
return getSFTPErrorFromOSError(err)
|
2019-07-20 22:19:17 +00:00
|
|
|
}
|
|
|
|
|
2019-11-19 10:38:39 +00:00
|
|
|
logger.CommandLog(removeLogSender, path, "", c.User.Username, "", c.ID, c.protocol, -1, -1, "", "", "")
|
2019-07-20 22:19:17 +00:00
|
|
|
if fi.Mode()&os.ModeSymlink != os.ModeSymlink {
|
2019-07-28 20:04:50 +00:00
|
|
|
dataprovider.UpdateUserQuota(dataProvider, c.User, -1, -size, false)
|
2019-07-20 22:19:17 +00:00
|
|
|
}
|
2019-11-26 21:26:42 +00:00
|
|
|
go executeAction(operationDelete, c.User.Username, path, "", "")
|
2019-07-20 22:19:17 +00:00
|
|
|
|
2019-10-14 20:44:57 +00:00
|
|
|
return sftp.ErrSSHFxOk
|
2019-07-20 22:19:17 +00:00
|
|
|
}
|
|
|
|
|
2019-08-04 07:37:58 +00:00
|
|
|
func (c Connection) handleSFTPUploadToNewFile(requestPath, filePath string) (io.WriterAt, error) {
|
|
|
|
if !c.hasSpace(true) {
|
2019-09-06 13:19:01 +00:00
|
|
|
c.Log(logger.LevelInfo, logSender, "denying file write due to space limit")
|
2019-10-14 20:44:57 +00:00
|
|
|
return nil, sftp.ErrSSHFxFailure
|
2019-08-04 07:37:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
file, err := os.Create(filePath)
|
|
|
|
if err != nil {
|
2019-11-15 11:15:07 +00:00
|
|
|
c.Log(logger.LevelWarn, logSender, "error creating file %#v: %v", requestPath, err)
|
|
|
|
return nil, getSFTPErrorFromOSError(err)
|
2019-08-04 07:37:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
utils.SetPathPermissions(filePath, c.User.GetUID(), c.User.GetGID())
|
|
|
|
|
|
|
|
transfer := Transfer{
|
2019-10-09 15:33:30 +00:00
|
|
|
file: file,
|
|
|
|
path: requestPath,
|
|
|
|
start: time.Now(),
|
|
|
|
bytesSent: 0,
|
|
|
|
bytesReceived: 0,
|
|
|
|
user: c.User,
|
|
|
|
connectionID: c.ID,
|
|
|
|
transferType: transferUpload,
|
|
|
|
lastActivity: time.Now(),
|
|
|
|
isNewFile: true,
|
|
|
|
protocol: c.protocol,
|
|
|
|
transferError: nil,
|
|
|
|
isFinished: false,
|
|
|
|
minWriteOffset: 0,
|
2019-08-04 07:37:58 +00:00
|
|
|
}
|
|
|
|
addTransfer(&transfer)
|
|
|
|
return &transfer, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c Connection) handleSFTPUploadToExistingFile(pflags sftp.FileOpenFlags, requestPath, filePath string,
|
|
|
|
fileSize int64) (io.WriterAt, error) {
|
|
|
|
var err error
|
|
|
|
if !c.hasSpace(false) {
|
2019-09-06 13:19:01 +00:00
|
|
|
c.Log(logger.LevelInfo, logSender, "denying file write due to space limit")
|
2019-10-14 20:44:57 +00:00
|
|
|
return nil, sftp.ErrSSHFxFailure
|
2019-08-04 07:37:58 +00:00
|
|
|
}
|
|
|
|
|
2019-10-09 15:33:30 +00:00
|
|
|
minWriteOffset := int64(0)
|
2019-08-04 09:02:38 +00:00
|
|
|
osFlags := getOSOpenFlags(pflags)
|
2019-08-04 07:37:58 +00:00
|
|
|
|
2019-10-09 15:33:30 +00:00
|
|
|
if isAtomicUploadEnabled() {
|
2019-08-04 07:37:58 +00:00
|
|
|
err = os.Rename(requestPath, filePath)
|
|
|
|
if err != nil {
|
2019-11-15 11:15:07 +00:00
|
|
|
c.Log(logger.LevelWarn, logSender, "error renaming existing file for atomic upload, source: %#v, dest: %#v, err: %v",
|
2019-08-04 07:37:58 +00:00
|
|
|
requestPath, filePath, err)
|
2019-11-15 11:15:07 +00:00
|
|
|
return nil, getSFTPErrorFromOSError(err)
|
2019-08-04 07:37:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// we use 0666 so the umask is applied
|
|
|
|
file, err := os.OpenFile(filePath, osFlags, 0666)
|
|
|
|
if err != nil {
|
2019-11-15 11:15:07 +00:00
|
|
|
c.Log(logger.LevelWarn, logSender, "error opening existing file, flags: %v, source: %#v, err: %v", pflags, filePath, err)
|
|
|
|
return nil, getSFTPErrorFromOSError(err)
|
2019-08-04 07:37:58 +00:00
|
|
|
}
|
|
|
|
|
2019-10-09 15:33:30 +00:00
|
|
|
if pflags.Append && osFlags&os.O_TRUNC == 0 {
|
|
|
|
c.Log(logger.LevelDebug, logSender, "upload resume requested, file path: %#v initial size: %v", filePath, fileSize)
|
|
|
|
minWriteOffset = fileSize
|
|
|
|
} else {
|
|
|
|
dataprovider.UpdateUserQuota(dataProvider, c.User, 0, -fileSize, false)
|
|
|
|
}
|
2019-08-04 07:37:58 +00:00
|
|
|
|
|
|
|
utils.SetPathPermissions(filePath, c.User.GetUID(), c.User.GetGID())
|
|
|
|
|
|
|
|
transfer := Transfer{
|
2019-10-09 15:33:30 +00:00
|
|
|
file: file,
|
|
|
|
path: requestPath,
|
|
|
|
start: time.Now(),
|
|
|
|
bytesSent: 0,
|
|
|
|
bytesReceived: 0,
|
|
|
|
user: c.User,
|
|
|
|
connectionID: c.ID,
|
|
|
|
transferType: transferUpload,
|
|
|
|
lastActivity: time.Now(),
|
|
|
|
isNewFile: false,
|
|
|
|
protocol: c.protocol,
|
|
|
|
transferError: nil,
|
|
|
|
isFinished: false,
|
|
|
|
minWriteOffset: minWriteOffset,
|
2019-08-04 07:37:58 +00:00
|
|
|
}
|
|
|
|
addTransfer(&transfer)
|
|
|
|
return &transfer, nil
|
|
|
|
}
|
|
|
|
|
2019-07-20 10:26:52 +00:00
|
|
|
func (c Connection) hasSpace(checkFiles bool) bool {
|
|
|
|
if (checkFiles && c.User.QuotaFiles > 0) || c.User.QuotaSize > 0 {
|
2019-07-26 11:07:08 +00:00
|
|
|
numFile, size, err := dataprovider.GetUsedQuota(dataProvider, c.User.Username)
|
2019-07-20 10:26:52 +00:00
|
|
|
if err != nil {
|
|
|
|
if _, ok := err.(*dataprovider.MethodDisabledError); ok {
|
2019-09-06 13:19:01 +00:00
|
|
|
c.Log(logger.LevelWarn, logSender, "quota enforcement not possible for user %v: %v", c.User.Username, err)
|
2019-07-20 10:26:52 +00:00
|
|
|
return true
|
|
|
|
}
|
2019-09-06 13:19:01 +00:00
|
|
|
c.Log(logger.LevelWarn, logSender, "error getting used quota for %v: %v", c.User.Username, err)
|
2019-07-20 10:26:52 +00:00
|
|
|
return false
|
|
|
|
}
|
2019-07-28 20:04:50 +00:00
|
|
|
if (checkFiles && c.User.QuotaFiles > 0 && numFile >= c.User.QuotaFiles) ||
|
|
|
|
(c.User.QuotaSize > 0 && size >= c.User.QuotaSize) {
|
2019-09-06 13:19:01 +00:00
|
|
|
c.Log(logger.LevelDebug, logSender, "quota exceed for user %v, num files: %v/%v, size: %v/%v check files: %v",
|
2019-07-20 10:26:52 +00:00
|
|
|
c.User.Username, numFile, c.User.QuotaFiles, size, c.User.QuotaSize, checkFiles)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-11-18 22:30:37 +00:00
|
|
|
// Normalizes a file/directory we get from the SFTP request to ensure the user is not able to escape
|
|
|
|
// from their data directory. After normalization if the file/directory is still within their home
|
2019-07-20 10:26:52 +00:00
|
|
|
// path it is returned. If they managed to "escape" an error will be returned.
|
|
|
|
func (c Connection) buildPath(rawPath string) (string, error) {
|
|
|
|
r := filepath.Clean(filepath.Join(c.User.HomeDir, rawPath))
|
|
|
|
p, err := filepath.EvalSymlinks(r)
|
|
|
|
if err != nil && !os.IsNotExist(err) {
|
|
|
|
return "", err
|
|
|
|
} else if os.IsNotExist(err) {
|
2019-11-18 22:30:37 +00:00
|
|
|
// The requested path doesn't exist, so at this point we need to iterate up the
|
2019-07-20 10:26:52 +00:00
|
|
|
// path chain until we hit a directory that _does_ exist and can be validated.
|
|
|
|
_, err = c.findFirstExistingDir(r)
|
|
|
|
if err != nil {
|
2019-09-06 13:19:01 +00:00
|
|
|
c.Log(logger.LevelWarn, logSender, "error resolving not existent path: %#v", err)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
return r, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = c.isSubDir(p)
|
|
|
|
if err != nil {
|
2019-09-06 13:19:01 +00:00
|
|
|
c.Log(logger.LevelWarn, logSender, "Invalid path resolution, dir: %#v outside user home: %#v err: %v", p, c.User.HomeDir, err)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
return r, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// iterate up the path chain until we hit a directory that does exist and can be validated.
|
|
|
|
// all nonexistent directories will be returned
|
|
|
|
func (c Connection) findNonexistentDirs(path string) ([]string, error) {
|
|
|
|
results := []string{}
|
|
|
|
cleanPath := filepath.Clean(path)
|
|
|
|
parent := filepath.Dir(cleanPath)
|
|
|
|
_, err := os.Stat(parent)
|
|
|
|
|
|
|
|
for os.IsNotExist(err) {
|
|
|
|
results = append(results, parent)
|
|
|
|
parent = filepath.Dir(parent)
|
|
|
|
_, err = os.Stat(parent)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return results, err
|
|
|
|
}
|
|
|
|
p, err := filepath.EvalSymlinks(parent)
|
|
|
|
if err != nil {
|
|
|
|
return results, err
|
|
|
|
}
|
|
|
|
err = c.isSubDir(p)
|
|
|
|
if err != nil {
|
2019-09-06 13:19:01 +00:00
|
|
|
c.Log(logger.LevelWarn, logSender, "Error finding non existing dir: %v", err)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
return results, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// iterate up the path chain until we hit a directory that does exist and can be validated.
|
|
|
|
func (c Connection) findFirstExistingDir(path string) (string, error) {
|
|
|
|
results, err := c.findNonexistentDirs(path)
|
|
|
|
if err != nil {
|
2019-09-06 13:19:01 +00:00
|
|
|
c.Log(logger.LevelWarn, logSender, "unable to find non existent dirs: %v", err)
|
2019-07-20 10:26:52 +00:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
var parent string
|
|
|
|
if len(results) > 0 {
|
|
|
|
lastMissingDir := results[len(results)-1]
|
|
|
|
parent = filepath.Dir(lastMissingDir)
|
|
|
|
} else {
|
|
|
|
parent = c.User.GetHomeDir()
|
|
|
|
}
|
|
|
|
p, err := filepath.EvalSymlinks(parent)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
fileInfo, err := os.Stat(p)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if !fileInfo.IsDir() {
|
2019-09-05 13:39:19 +00:00
|
|
|
return "", fmt.Errorf("resolved path is not a dir: %#v", p)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
err = c.isSubDir(p)
|
|
|
|
return p, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// checks if sub is a subpath of the user home dir.
|
|
|
|
// EvalSymlink must be used on sub before calling this method
|
|
|
|
func (c Connection) isSubDir(sub string) error {
|
|
|
|
// home dir must exist and it is already a validated absolute path
|
|
|
|
parent, err := filepath.EvalSymlinks(c.User.HomeDir)
|
|
|
|
if err != nil {
|
2019-09-06 13:19:01 +00:00
|
|
|
c.Log(logger.LevelWarn, logSender, "invalid home dir %#v: %v", c.User.HomeDir, err)
|
2019-07-20 10:26:52 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !strings.HasPrefix(sub, parent) {
|
2019-11-18 22:30:37 +00:00
|
|
|
c.Log(logger.LevelWarn, logSender, "path %#v is not inside: %#v ", sub, parent)
|
|
|
|
return fmt.Errorf("path %#v is not inside: %#v", sub, parent)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2019-07-20 22:19:17 +00:00
|
|
|
|
2019-09-11 10:46:21 +00:00
|
|
|
func (c Connection) close() error {
|
2019-09-11 14:29:56 +00:00
|
|
|
if c.channel != nil {
|
|
|
|
err := c.channel.Close()
|
|
|
|
c.Log(logger.LevelInfo, logSender, "channel close, err: %v", err)
|
|
|
|
}
|
2019-09-11 10:46:21 +00:00
|
|
|
return c.netConn.Close()
|
|
|
|
}
|
|
|
|
|
2019-08-04 09:02:38 +00:00
|
|
|
func getOSOpenFlags(requestFlags sftp.FileOpenFlags) (flags int) {
|
2019-07-20 22:19:17 +00:00
|
|
|
var osFlags int
|
|
|
|
if requestFlags.Read && requestFlags.Write {
|
|
|
|
osFlags |= os.O_RDWR
|
|
|
|
} else if requestFlags.Write {
|
|
|
|
osFlags |= os.O_WRONLY
|
|
|
|
}
|
2019-10-09 15:33:30 +00:00
|
|
|
// we ignore Append flag since pkg/sftp use WriteAt that cannot work with os.O_APPEND
|
|
|
|
/*if requestFlags.Append {
|
2019-07-20 22:19:17 +00:00
|
|
|
osFlags |= os.O_APPEND
|
2019-10-09 15:33:30 +00:00
|
|
|
}*/
|
2019-07-20 22:19:17 +00:00
|
|
|
if requestFlags.Creat {
|
|
|
|
osFlags |= os.O_CREATE
|
|
|
|
}
|
|
|
|
if requestFlags.Trunc {
|
|
|
|
osFlags |= os.O_TRUNC
|
|
|
|
}
|
|
|
|
if requestFlags.Excl {
|
|
|
|
osFlags |= os.O_EXCL
|
|
|
|
}
|
2019-08-04 09:02:38 +00:00
|
|
|
return osFlags
|
2019-07-20 22:19:17 +00:00
|
|
|
}
|
2019-08-04 07:37:58 +00:00
|
|
|
|
|
|
|
func getUploadTempFilePath(path string) string {
|
|
|
|
dir := filepath.Dir(path)
|
|
|
|
guid := xid.New().String()
|
|
|
|
return filepath.Join(dir, ".sftpgo-upload."+guid+"."+filepath.Base(path))
|
|
|
|
}
|
2019-11-15 11:15:07 +00:00
|
|
|
|
|
|
|
func getSFTPErrorFromOSError(err error) error {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return sftp.ErrSSHFxNoSuchFile
|
|
|
|
} else if os.IsPermission(err) {
|
|
|
|
return sftp.ErrSSHFxPermissionDenied
|
|
|
|
} else if err != nil {
|
|
|
|
return sftp.ErrSSHFxFailure
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|