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/>.
|
|
|
|
|
2021-05-06 19:35:43 +00:00
|
|
|
package httpd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"strings"
|
2022-01-20 17:19:20 +00:00
|
|
|
"sync"
|
2021-11-30 17:40:50 +00:00
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
2021-05-06 19:35:43 +00:00
|
|
|
|
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"
|
2021-07-11 13:26:51 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/util"
|
2021-07-23 08:19:27 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/vfs"
|
2021-05-06 19:35:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Connection details for a HTTP connection used to inteact with an SFTPGo filesystem
|
|
|
|
type Connection struct {
|
|
|
|
*common.BaseConnection
|
|
|
|
request *http.Request
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetClientVersion returns the connected client's version.
|
|
|
|
func (c *Connection) GetClientVersion() string {
|
|
|
|
if c.request != nil {
|
|
|
|
return c.request.UserAgent()
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2021-07-24 18:11:17 +00:00
|
|
|
// GetLocalAddress returns local connection address
|
|
|
|
func (c *Connection) GetLocalAddress() string {
|
|
|
|
return util.GetHTTPLocalAddress(c.request)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetRemoteAddress returns the connected client's address
|
2021-05-06 19:35:43 +00:00
|
|
|
func (c *Connection) GetRemoteAddress() string {
|
|
|
|
if c.request != nil {
|
|
|
|
return c.request.RemoteAddr
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// Disconnect closes the active transfer
|
|
|
|
func (c *Connection) Disconnect() (err error) {
|
|
|
|
return c.SignalTransfersAbort()
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetCommand returns the request method
|
|
|
|
func (c *Connection) GetCommand() string {
|
|
|
|
if c.request != nil {
|
|
|
|
return strings.ToUpper(c.request.Method)
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stat returns a FileInfo describing the named file/directory, or an error,
|
|
|
|
// if any happens
|
|
|
|
func (c *Connection) Stat(name string, mode int) (os.FileInfo, error) {
|
|
|
|
c.UpdateLastActivity()
|
|
|
|
|
|
|
|
if !c.User.HasPerm(dataprovider.PermListItems, path.Dir(name)) {
|
|
|
|
return nil, c.GetPermissionDeniedError()
|
|
|
|
}
|
|
|
|
|
2022-01-15 16:16:49 +00:00
|
|
|
fi, err := c.DoStat(name, mode, true)
|
2021-05-06 19:35:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return fi, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadDir returns a list of directory entries
|
|
|
|
func (c *Connection) ReadDir(name string) ([]os.FileInfo, error) {
|
|
|
|
c.UpdateLastActivity()
|
|
|
|
|
|
|
|
return c.ListDir(name)
|
|
|
|
}
|
|
|
|
|
2021-05-26 05:48:37 +00:00
|
|
|
func (c *Connection) getFileReader(name string, offset int64, method string) (io.ReadCloser, error) {
|
2021-05-06 19:35:43 +00:00
|
|
|
c.UpdateLastActivity()
|
|
|
|
|
2022-01-30 10:42:36 +00:00
|
|
|
transferQuota := c.GetTransferQuota()
|
|
|
|
if !transferQuota.HasDownloadSpace() {
|
|
|
|
c.Log(logger.LevelInfo, "denying file read due to quota limits")
|
|
|
|
return nil, c.GetReadQuotaExceededError()
|
|
|
|
}
|
|
|
|
|
2021-05-06 19:35:43 +00:00
|
|
|
if !c.User.HasPerm(dataprovider.PermDownload, path.Dir(name)) {
|
2021-05-26 05:48:37 +00:00
|
|
|
return nil, c.GetPermissionDeniedError()
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
|
2022-01-15 16:16:49 +00:00
|
|
|
if ok, policy := c.User.IsFileAllowed(name); !ok {
|
2021-05-06 19:35:43 +00:00
|
|
|
c.Log(logger.LevelWarn, "reading file %#v is not allowed", name)
|
2022-01-15 16:16:49 +00:00
|
|
|
return nil, c.GetErrorForDeniedFile(policy)
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fs, p, err := c.GetFsAndResolvedPath(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-05-26 05:48:37 +00:00
|
|
|
if method != http.MethodHead {
|
2021-12-04 16:27:24 +00:00
|
|
|
if err := common.ExecutePreAction(c.BaseConnection, common.OperationPreDownload, p, name, 0, 0); err != nil {
|
2021-05-26 05:48:37 +00:00
|
|
|
c.Log(logger.LevelDebug, "download for file %#v denied by pre action: %v", name, err)
|
|
|
|
return nil, c.GetPermissionDeniedError()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-06 19:35:43 +00:00
|
|
|
file, r, cancelFn, err := fs.Open(p, offset)
|
|
|
|
if err != nil {
|
2021-12-16 18:53:00 +00:00
|
|
|
c.Log(logger.LevelError, "could not open file %#v for reading: %+v", p, err)
|
2021-05-06 19:35:43 +00:00
|
|
|
return nil, c.GetFsError(fs, err)
|
|
|
|
}
|
|
|
|
|
2021-05-31 19:45:29 +00:00
|
|
|
baseTransfer := common.NewBaseTransfer(file, c.BaseConnection, cancelFn, p, p, name, common.TransferDownload,
|
2022-01-30 10:42:36 +00:00
|
|
|
0, 0, 0, 0, false, fs, transferQuota)
|
2021-07-23 08:19:27 +00:00
|
|
|
return newHTTPDFile(baseTransfer, nil, r), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Connection) getFileWriter(name string) (io.WriteCloser, error) {
|
|
|
|
c.UpdateLastActivity()
|
|
|
|
|
2022-01-15 16:16:49 +00:00
|
|
|
if ok, _ := c.User.IsFileAllowed(name); !ok {
|
2021-07-23 08:19:27 +00:00
|
|
|
c.Log(logger.LevelWarn, "writing file %#v is not allowed", name)
|
|
|
|
return nil, c.GetPermissionDeniedError()
|
|
|
|
}
|
|
|
|
|
|
|
|
fs, p, err := c.GetFsAndResolvedPath(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
filePath := p
|
|
|
|
if common.Config.IsAtomicUploadEnabled() && fs.IsAtomicUploadSupported() {
|
|
|
|
filePath = fs.GetAtomicUploadPath(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
stat, statErr := fs.Lstat(p)
|
|
|
|
if (statErr == nil && stat.Mode()&os.ModeSymlink != 0) || fs.IsNotExist(statErr) {
|
|
|
|
if !c.User.HasPerm(dataprovider.PermUpload, path.Dir(name)) {
|
|
|
|
return nil, c.GetPermissionDeniedError()
|
|
|
|
}
|
|
|
|
return c.handleUploadFile(fs, p, filePath, name, true, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
if statErr != nil {
|
|
|
|
c.Log(logger.LevelError, "error performing file stat %#v: %+v", p, statErr)
|
|
|
|
return nil, c.GetFsError(fs, statErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
// This happen if we upload a file that has the same name of an existing directory
|
|
|
|
if stat.IsDir() {
|
2021-12-16 18:53:00 +00:00
|
|
|
c.Log(logger.LevelError, "attempted to open a directory for writing to: %#v", p)
|
2021-07-23 08:19:27 +00:00
|
|
|
return nil, c.GetOpUnsupportedError()
|
|
|
|
}
|
|
|
|
|
|
|
|
if !c.User.HasPerm(dataprovider.PermOverwrite, path.Dir(name)) {
|
|
|
|
return nil, c.GetPermissionDeniedError()
|
|
|
|
}
|
|
|
|
|
|
|
|
if common.Config.IsAtomicUploadEnabled() && fs.IsAtomicUploadSupported() {
|
|
|
|
err = fs.Rename(p, filePath)
|
|
|
|
if err != nil {
|
2021-12-16 18:53:00 +00:00
|
|
|
c.Log(logger.LevelError, "error renaming existing file for atomic upload, source: %#v, dest: %#v, err: %+v",
|
2021-07-23 08:19:27 +00:00
|
|
|
p, filePath, err)
|
|
|
|
return nil, c.GetFsError(fs, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.handleUploadFile(fs, p, filePath, name, false, stat.Size())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Connection) handleUploadFile(fs vfs.Fs, resolvedPath, filePath, requestPath string, isNewFile bool, fileSize int64) (io.WriteCloser, error) {
|
2022-01-30 10:42:36 +00:00
|
|
|
diskQuota, transferQuota := c.HasSpace(isNewFile, false, requestPath)
|
|
|
|
if !diskQuota.HasSpace || !transferQuota.HasUploadSpace() {
|
2021-07-23 08:19:27 +00:00
|
|
|
c.Log(logger.LevelInfo, "denying file write due to quota limits")
|
|
|
|
return nil, common.ErrQuotaExceeded
|
|
|
|
}
|
2021-12-04 16:27:24 +00:00
|
|
|
err := common.ExecutePreAction(c.BaseConnection, common.OperationPreUpload, resolvedPath, requestPath, fileSize, os.O_TRUNC)
|
2021-07-23 08:19:27 +00:00
|
|
|
if err != nil {
|
|
|
|
c.Log(logger.LevelDebug, "upload for file %#v denied by pre action: %v", requestPath, err)
|
|
|
|
return nil, c.GetPermissionDeniedError()
|
|
|
|
}
|
|
|
|
|
2022-01-30 10:42:36 +00:00
|
|
|
maxWriteSize, _ := c.GetMaxWriteSize(diskQuota, false, fileSize, fs.IsUploadResumeSupported())
|
2021-07-23 08:19:27 +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)
|
2021-07-23 08:19:27 +00:00
|
|
|
if err != nil {
|
2021-12-16 18:53:00 +00:00
|
|
|
c.Log(logger.LevelError, "error opening existing file, source: %#v, err: %+v", filePath, err)
|
2021-07-23 08:19:27 +00:00
|
|
|
return nil, c.GetFsError(fs, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
initialSize := int64(0)
|
2022-01-20 17:19:20 +00:00
|
|
|
truncatedSize := int64(0) // bytes truncated and not included in quota
|
2021-07-23 08:19:27 +00:00
|
|
|
if !isNewFile {
|
2022-06-11 08:41:34 +00:00
|
|
|
if vfs.HasTruncateSupport(fs) {
|
2021-07-23 08:19:27 +00:00
|
|
|
vfolder, err := c.User.GetVirtualFolderForPath(path.Dir(requestPath))
|
|
|
|
if err == nil {
|
|
|
|
dataprovider.UpdateVirtualFolderQuota(&vfolder.BaseVirtualFolder, 0, -fileSize, false) //nolint:errcheck
|
|
|
|
if vfolder.IsIncludedInUserQuota() {
|
|
|
|
dataprovider.UpdateUserQuota(&c.User, 0, -fileSize, false) //nolint:errcheck
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
dataprovider.UpdateUserQuota(&c.User, 0, -fileSize, false) //nolint:errcheck
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
initialSize = fileSize
|
2022-01-20 17:19:20 +00:00
|
|
|
truncatedSize = fileSize
|
2021-07-23 08:19:27 +00:00
|
|
|
}
|
|
|
|
if maxWriteSize > 0 {
|
|
|
|
maxWriteSize += fileSize
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
vfs.SetPathPermissions(fs, filePath, c.User.GetUID(), c.User.GetGID())
|
|
|
|
|
|
|
|
baseTransfer := common.NewBaseTransfer(file, c.BaseConnection, cancelFn, resolvedPath, filePath, requestPath,
|
2022-01-30 10:42:36 +00:00
|
|
|
common.TransferUpload, 0, initialSize, maxWriteSize, truncatedSize, isNewFile, fs, transferQuota)
|
2021-07-23 08:19:27 +00:00
|
|
|
return newHTTPDFile(baseTransfer, w, nil), nil
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
2021-11-30 17:40:50 +00:00
|
|
|
|
|
|
|
func newThrottledReader(r io.ReadCloser, limit int64, conn *Connection) *throttledReader {
|
|
|
|
t := &throttledReader{
|
|
|
|
bytesRead: 0,
|
|
|
|
id: conn.GetTransferID(),
|
|
|
|
limit: limit,
|
|
|
|
r: r,
|
|
|
|
abortTransfer: 0,
|
|
|
|
start: time.Now(),
|
|
|
|
conn: conn,
|
|
|
|
}
|
|
|
|
conn.AddTransfer(t)
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
|
|
|
type throttledReader struct {
|
|
|
|
bytesRead int64
|
2022-01-20 17:19:20 +00:00
|
|
|
id int64
|
2021-11-30 17:40:50 +00:00
|
|
|
limit int64
|
|
|
|
r io.ReadCloser
|
|
|
|
abortTransfer int32
|
|
|
|
start time.Time
|
|
|
|
conn *Connection
|
2022-01-20 17:19:20 +00:00
|
|
|
mu sync.Mutex
|
|
|
|
errAbort error
|
2021-11-30 17:40:50 +00:00
|
|
|
}
|
|
|
|
|
2022-01-20 17:19:20 +00:00
|
|
|
func (t *throttledReader) GetID() int64 {
|
2021-11-30 17:40:50 +00:00
|
|
|
return t.id
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *throttledReader) GetType() int {
|
|
|
|
return common.TransferUpload
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *throttledReader) GetSize() int64 {
|
|
|
|
return atomic.LoadInt64(&t.bytesRead)
|
|
|
|
}
|
|
|
|
|
2022-01-20 17:19:20 +00:00
|
|
|
func (t *throttledReader) GetDownloadedSize() int64 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *throttledReader) GetUploadedSize() int64 {
|
|
|
|
return atomic.LoadInt64(&t.bytesRead)
|
|
|
|
}
|
|
|
|
|
2021-11-30 17:40:50 +00:00
|
|
|
func (t *throttledReader) GetVirtualPath() string {
|
|
|
|
return "**reading request body**"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *throttledReader) GetStartTime() time.Time {
|
|
|
|
return t.start
|
|
|
|
}
|
|
|
|
|
2022-01-20 17:19:20 +00:00
|
|
|
func (t *throttledReader) GetAbortError() error {
|
|
|
|
t.mu.Lock()
|
|
|
|
defer t.mu.Unlock()
|
|
|
|
|
|
|
|
if t.errAbort != nil {
|
|
|
|
return t.errAbort
|
|
|
|
}
|
|
|
|
return common.ErrTransferAborted
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *throttledReader) SignalClose(err error) {
|
|
|
|
t.mu.Lock()
|
|
|
|
t.errAbort = err
|
|
|
|
t.mu.Unlock()
|
2021-11-30 17:40:50 +00:00
|
|
|
atomic.StoreInt32(&(t.abortTransfer), 1)
|
|
|
|
}
|
|
|
|
|
2022-01-20 17:19:20 +00:00
|
|
|
func (t *throttledReader) GetTruncatedSize() int64 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2022-01-30 10:42:36 +00:00
|
|
|
func (t *throttledReader) HasSizeLimit() bool {
|
|
|
|
return false
|
2022-01-20 17:19:20 +00:00
|
|
|
}
|
|
|
|
|
2021-11-30 17:40:50 +00:00
|
|
|
func (t *throttledReader) Truncate(fsPath string, size int64) (int64, error) {
|
|
|
|
return 0, vfs.ErrVfsUnsupported
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *throttledReader) GetRealFsPath(fsPath string) string {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *throttledReader) SetTimes(fsPath string, atime time.Time, mtime time.Time) bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *throttledReader) Read(p []byte) (n int, err error) {
|
|
|
|
if atomic.LoadInt32(&t.abortTransfer) == 1 {
|
2022-01-20 17:19:20 +00:00
|
|
|
return 0, t.GetAbortError()
|
2021-11-30 17:40:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
t.conn.UpdateLastActivity()
|
|
|
|
n, err = t.r.Read(p)
|
|
|
|
if t.limit > 0 {
|
|
|
|
atomic.AddInt64(&t.bytesRead, int64(n))
|
|
|
|
trasferredBytes := atomic.LoadInt64(&t.bytesRead)
|
|
|
|
elapsed := time.Since(t.start).Nanoseconds() / 1000000
|
|
|
|
wantedElapsed := 1000 * (trasferredBytes / 1024) / t.limit
|
|
|
|
if wantedElapsed > elapsed {
|
|
|
|
toSleep := time.Duration(wantedElapsed - elapsed)
|
|
|
|
time.Sleep(toSleep * time.Millisecond)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *throttledReader) Close() error {
|
|
|
|
return t.r.Close()
|
|
|
|
}
|