2023-01-03 09:18:30 +00:00
|
|
|
// Copyright (C) 2019-2023 Nicola Murino
|
2022-07-17 18:16:00 +00:00
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published
|
|
|
|
// by the Free Software Foundation, version 3.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
2023-01-03 09:18:30 +00:00
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2022-07-17 18:16:00 +00:00
|
|
|
|
2020-08-11 21:56:10 +00:00
|
|
|
package webdavd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path"
|
2022-11-19 18:39:28 +00:00
|
|
|
"strconv"
|
2020-08-11 21:56:10 +00:00
|
|
|
"strings"
|
2022-11-19 18:39:28 +00:00
|
|
|
"time"
|
2020-08-11 21:56:10 +00:00
|
|
|
|
2022-11-03 07:31:40 +00:00
|
|
|
"github.com/drakkan/webdav"
|
2020-08-11 21:56:10 +00:00
|
|
|
|
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"
|
2020-08-11 21:56:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Connection details for a WebDav connection.
|
|
|
|
type Connection struct {
|
|
|
|
*common.BaseConnection
|
|
|
|
request *http.Request
|
|
|
|
}
|
|
|
|
|
2022-11-19 18:39:28 +00:00
|
|
|
func (c *Connection) getModificationTime() time.Time {
|
|
|
|
if c.request == nil {
|
|
|
|
return time.Time{}
|
|
|
|
}
|
|
|
|
if val := c.request.Header.Get("X-OC-Mtime"); val != "" {
|
|
|
|
if unixTime, err := strconv.ParseInt(val, 10, 64); err == nil {
|
|
|
|
return time.Unix(unixTime, 0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return time.Time{}
|
|
|
|
}
|
|
|
|
|
2020-08-11 21:56:10 +00:00
|
|
|
// 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
|
2020-08-11 21:56:10 +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() 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 ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mkdir creates a directory using the connection filesystem
|
2023-03-22 18:02:54 +00:00
|
|
|
func (c *Connection) Mkdir(_ context.Context, name string, _ os.FileMode) error {
|
2020-08-11 21:56:10 +00:00
|
|
|
c.UpdateLastActivity()
|
|
|
|
|
2021-07-11 13:26:51 +00:00
|
|
|
name = util.CleanPath(name)
|
2022-01-15 16:16:49 +00:00
|
|
|
return c.CreateDir(name, true)
|
2020-08-11 21:56:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Rename renames a file or a directory
|
2023-03-22 18:02:54 +00:00
|
|
|
func (c *Connection) Rename(_ context.Context, oldName, newName string) error {
|
2020-08-11 21:56:10 +00:00
|
|
|
c.UpdateLastActivity()
|
|
|
|
|
2021-07-11 13:26:51 +00:00
|
|
|
oldName = util.CleanPath(oldName)
|
|
|
|
newName = util.CleanPath(newName)
|
2020-08-11 21:56:10 +00:00
|
|
|
|
2022-11-19 18:39:28 +00:00
|
|
|
err := c.BaseConnection.Rename(oldName, newName)
|
|
|
|
if err == nil {
|
|
|
|
if mtime := c.getModificationTime(); !mtime.IsZero() {
|
|
|
|
attrs := &common.StatAttributes{
|
|
|
|
Flags: common.StatAttrTimes,
|
|
|
|
Atime: mtime,
|
|
|
|
Mtime: mtime,
|
|
|
|
}
|
|
|
|
setStatErr := c.SetStat(newName, attrs)
|
|
|
|
c.Log(logger.LevelDebug, "mtime header found for %q, value: %s, err: %v", newName, mtime, setStatErr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
2020-08-11 21:56:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stat returns a FileInfo describing the named file/directory, or an error,
|
|
|
|
// if any happens
|
2023-03-22 18:02:54 +00:00
|
|
|
func (c *Connection) Stat(_ context.Context, name string) (os.FileInfo, error) {
|
2020-08-11 21:56:10 +00:00
|
|
|
c.UpdateLastActivity()
|
|
|
|
|
2021-07-11 13:26:51 +00:00
|
|
|
name = util.CleanPath(name)
|
2020-08-11 21:56:10 +00:00
|
|
|
if !c.User.HasPerm(dataprovider.PermListItems, path.Dir(name)) {
|
|
|
|
return nil, c.GetPermissionDeniedError()
|
|
|
|
}
|
|
|
|
|
2022-01-15 16:16:49 +00:00
|
|
|
fi, err := c.DoStat(name, 0, true)
|
2020-08-11 21:56:10 +00:00
|
|
|
if err != nil {
|
2021-03-21 18:15:47 +00:00
|
|
|
return nil, err
|
2020-08-11 21:56:10 +00:00
|
|
|
}
|
|
|
|
return fi, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveAll removes path and any children it contains.
|
|
|
|
// If the path does not exist, RemoveAll returns nil (no error).
|
2023-03-22 18:02:54 +00:00
|
|
|
func (c *Connection) RemoveAll(_ context.Context, name string) error {
|
2020-08-11 21:56:10 +00:00
|
|
|
c.UpdateLastActivity()
|
|
|
|
|
2021-07-11 13:26:51 +00:00
|
|
|
name = util.CleanPath(name)
|
2022-10-27 06:27:44 +00:00
|
|
|
return c.BaseConnection.RemoveAll(name)
|
2020-08-11 21:56:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// OpenFile opens the named file with specified flag.
|
|
|
|
// This method is used for uploads and downloads but also for Stat and Readdir
|
2023-03-22 18:02:54 +00:00
|
|
|
func (c *Connection) OpenFile(_ context.Context, name string, flag int, _ os.FileMode) (webdav.File, error) {
|
2020-08-11 21:56:10 +00:00
|
|
|
c.UpdateLastActivity()
|
|
|
|
|
2021-07-11 13:26:51 +00:00
|
|
|
name = util.CleanPath(name)
|
2021-03-21 18:15:47 +00:00
|
|
|
fs, p, err := c.GetFsAndResolvedPath(name)
|
2020-08-11 21:56:10 +00:00
|
|
|
if err != nil {
|
2021-03-21 18:15:47 +00:00
|
|
|
return nil, err
|
2020-08-11 21:56:10 +00:00
|
|
|
}
|
2020-12-06 07:19:41 +00:00
|
|
|
|
|
|
|
if flag == os.O_RDONLY || c.request.Method == "PROPPATCH" {
|
2020-11-04 18:11:40 +00:00
|
|
|
// Download, Stat, Readdir or simply open/close
|
2021-03-21 18:15:47 +00:00
|
|
|
return c.getFile(fs, p, name)
|
2020-08-11 21:56:10 +00:00
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
return c.putFile(fs, p, name)
|
2020-08-11 21:56:10 +00:00
|
|
|
}
|
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
func (c *Connection) getFile(fs vfs.Fs, fsPath, virtualPath string) (webdav.File, error) {
|
2020-08-11 21:56:10 +00:00
|
|
|
var cancelFn func()
|
|
|
|
|
2022-11-03 07:31:40 +00:00
|
|
|
// we open the file when we receive the first read so we only open the file if necessary
|
|
|
|
baseTransfer := common.NewBaseTransfer(nil, c.BaseConnection, cancelFn, fsPath, fsPath, virtualPath,
|
2022-01-30 10:42:36 +00:00
|
|
|
common.TransferDownload, 0, 0, 0, 0, false, fs, c.GetTransferQuota())
|
2020-08-11 21:56:10 +00:00
|
|
|
|
2022-11-03 07:31:40 +00:00
|
|
|
return newWebDavFile(baseTransfer, nil, nil), nil
|
2020-08-11 21:56:10 +00:00
|
|
|
}
|
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
func (c *Connection) putFile(fs vfs.Fs, fsPath, virtualPath string) (webdav.File, error) {
|
2022-01-15 16:16:49 +00:00
|
|
|
if ok, _ := c.User.IsFileAllowed(virtualPath); !ok {
|
2023-02-27 18:02:43 +00:00
|
|
|
c.Log(logger.LevelWarn, "writing file %q is not allowed", virtualPath)
|
2020-08-11 21:56:10 +00:00
|
|
|
return nil, c.GetPermissionDeniedError()
|
|
|
|
}
|
|
|
|
|
|
|
|
filePath := fsPath
|
2021-03-21 18:15:47 +00:00
|
|
|
if common.Config.IsAtomicUploadEnabled() && fs.IsAtomicUploadSupported() {
|
|
|
|
filePath = fs.GetAtomicUploadPath(fsPath)
|
2020-08-11 21:56:10 +00:00
|
|
|
}
|
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
stat, statErr := fs.Lstat(fsPath)
|
|
|
|
if (statErr == nil && stat.Mode()&os.ModeSymlink != 0) || fs.IsNotExist(statErr) {
|
2020-08-11 21:56:10 +00:00
|
|
|
if !c.User.HasPerm(dataprovider.PermUpload, path.Dir(virtualPath)) {
|
|
|
|
return nil, c.GetPermissionDeniedError()
|
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
return c.handleUploadToNewFile(fs, fsPath, filePath, virtualPath)
|
2020-08-11 21:56:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if statErr != nil {
|
2023-02-27 18:02:43 +00:00
|
|
|
c.Log(logger.LevelError, "error performing file stat %q: %+v", fsPath, statErr)
|
2021-03-21 18:15:47 +00:00
|
|
|
return nil, c.GetFsError(fs, statErr)
|
2020-08-11 21:56:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// This happen if we upload a file that has the same name of an existing directory
|
|
|
|
if stat.IsDir() {
|
2023-02-27 18:02:43 +00:00
|
|
|
c.Log(logger.LevelError, "attempted to open a directory for writing to: %q", fsPath)
|
2020-08-11 21:56:10 +00:00
|
|
|
return nil, c.GetOpUnsupportedError()
|
|
|
|
}
|
|
|
|
|
|
|
|
if !c.User.HasPerm(dataprovider.PermOverwrite, path.Dir(virtualPath)) {
|
|
|
|
return nil, c.GetPermissionDeniedError()
|
|
|
|
}
|
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
return c.handleUploadToExistingFile(fs, fsPath, filePath, stat.Size(), virtualPath)
|
2020-08-11 21:56:10 +00:00
|
|
|
}
|
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
func (c *Connection) handleUploadToNewFile(fs vfs.Fs, resolvedPath, filePath, requestPath string) (webdav.File, error) {
|
2022-01-30 10:42:36 +00:00
|
|
|
diskQuota, transferQuota := c.HasSpace(true, false, requestPath)
|
|
|
|
if !diskQuota.HasSpace || !transferQuota.HasUploadSpace() {
|
2020-08-11 21:56:10 +00:00
|
|
|
c.Log(logger.LevelInfo, "denying file write due to quota limits")
|
|
|
|
return nil, common.ErrQuotaExceeded
|
|
|
|
}
|
2023-01-02 14:59:00 +00:00
|
|
|
if _, err := common.ExecutePreAction(c.BaseConnection, common.OperationPreUpload, resolvedPath, requestPath, 0, 0); err != nil {
|
2023-02-27 18:02:43 +00:00
|
|
|
c.Log(logger.LevelDebug, "upload for file %q denied by pre action: %v", requestPath, err)
|
2021-05-26 05:48:37 +00:00
|
|
|
return nil, c.GetPermissionDeniedError()
|
|
|
|
}
|
2023-10-22 14:09:30 +00:00
|
|
|
file, w, cancelFn, err := fs.Create(filePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, c.GetCreateChecks(requestPath, true, false))
|
2020-08-11 21:56:10 +00:00
|
|
|
if err != nil {
|
2023-02-27 18:02:43 +00:00
|
|
|
c.Log(logger.LevelError, "error creating file %q: %+v", resolvedPath, err)
|
2021-03-21 18:15:47 +00:00
|
|
|
return nil, c.GetFsError(fs, err)
|
2020-08-11 21:56:10 +00:00
|
|
|
}
|
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
vfs.SetPathPermissions(fs, filePath, c.User.GetUID(), c.User.GetGID())
|
2020-08-11 21:56:10 +00:00
|
|
|
|
2020-08-16 18:17:02 +00:00
|
|
|
// we can get an error only for resume
|
2022-01-30 10:42:36 +00:00
|
|
|
maxWriteSize, _ := c.GetMaxWriteSize(diskQuota, false, 0, fs.IsUploadResumeSupported())
|
2020-08-16 18:17:02 +00:00
|
|
|
|
2021-05-31 19:45:29 +00:00
|
|
|
baseTransfer := common.NewBaseTransfer(file, c.BaseConnection, cancelFn, resolvedPath, filePath, requestPath,
|
2022-01-30 10:42:36 +00:00
|
|
|
common.TransferUpload, 0, 0, maxWriteSize, 0, true, fs, transferQuota)
|
2022-11-19 18:39:28 +00:00
|
|
|
mtime := c.getModificationTime()
|
|
|
|
baseTransfer.SetTimes(resolvedPath, mtime, mtime)
|
2020-08-11 21:56:10 +00:00
|
|
|
|
2020-11-04 18:11:40 +00:00
|
|
|
return newWebDavFile(baseTransfer, w, nil), nil
|
2020-08-11 21:56:10 +00:00
|
|
|
}
|
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
func (c *Connection) handleUploadToExistingFile(fs vfs.Fs, resolvedPath, filePath string, fileSize int64,
|
2022-01-30 10:42:36 +00:00
|
|
|
requestPath string,
|
|
|
|
) (webdav.File, error) {
|
2020-08-11 21:56:10 +00:00
|
|
|
var err error
|
2022-01-30 10:42:36 +00:00
|
|
|
diskQuota, transferQuota := c.HasSpace(false, false, requestPath)
|
|
|
|
if !diskQuota.HasSpace || !transferQuota.HasUploadSpace() {
|
2020-08-11 21:56:10 +00:00
|
|
|
c.Log(logger.LevelInfo, "denying file write due to quota limits")
|
|
|
|
return nil, common.ErrQuotaExceeded
|
|
|
|
}
|
2023-01-02 14:59:00 +00:00
|
|
|
if _, err := common.ExecutePreAction(c.BaseConnection, common.OperationPreUpload, resolvedPath, requestPath,
|
2021-10-10 11:08:05 +00:00
|
|
|
fileSize, os.O_TRUNC); err != nil {
|
2023-02-27 18:02:43 +00:00
|
|
|
c.Log(logger.LevelDebug, "upload for file %q denied by pre action: %v", requestPath, err)
|
2021-05-26 05:48:37 +00:00
|
|
|
return nil, c.GetPermissionDeniedError()
|
|
|
|
}
|
2020-08-11 21:56:10 +00:00
|
|
|
|
2020-08-16 18:17:02 +00:00
|
|
|
// if there is a size limit remaining size cannot be 0 here, since quotaResult.HasSpace
|
|
|
|
// will return false in this case and we deny the upload before
|
2022-01-30 10:42:36 +00:00
|
|
|
maxWriteSize, _ := c.GetMaxWriteSize(diskQuota, false, fileSize, fs.IsUploadResumeSupported())
|
2020-08-16 18:17:02 +00:00
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
if common.Config.IsAtomicUploadEnabled() && fs.IsAtomicUploadSupported() {
|
2023-01-06 11:33:50 +00:00
|
|
|
_, _, err = fs.Rename(resolvedPath, filePath)
|
2020-08-11 21:56:10 +00:00
|
|
|
if err != nil {
|
2023-02-27 18:02:43 +00:00
|
|
|
c.Log(logger.LevelError, "error renaming existing file for atomic upload, source: %q, dest: %q, err: %+v",
|
2020-08-11 21:56:10 +00:00
|
|
|
resolvedPath, filePath, err)
|
2021-03-21 18:15:47 +00:00
|
|
|
return nil, c.GetFsError(fs, err)
|
2020-08-11 21:56:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-22 14:09:30 +00:00
|
|
|
file, w, cancelFn, err := fs.Create(filePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, c.GetCreateChecks(requestPath, false, false))
|
2020-08-11 21:56:10 +00:00
|
|
|
if err != nil {
|
2023-02-27 18:02:43 +00:00
|
|
|
c.Log(logger.LevelError, "error creating file %q: %+v", resolvedPath, err)
|
2021-03-21 18:15:47 +00:00
|
|
|
return nil, c.GetFsError(fs, err)
|
2020-08-11 21:56:10 +00:00
|
|
|
}
|
|
|
|
initialSize := int64(0)
|
2022-01-20 17:19:20 +00:00
|
|
|
truncatedSize := int64(0) // bytes truncated and not included in quota
|
2022-06-11 08:41:34 +00:00
|
|
|
if vfs.HasTruncateSupport(fs) {
|
2020-08-11 21:56:10 +00:00
|
|
|
vfolder, err := c.User.GetVirtualFolderForPath(path.Dir(requestPath))
|
|
|
|
if err == nil {
|
2021-02-16 18:11:36 +00:00
|
|
|
dataprovider.UpdateVirtualFolderQuota(&vfolder.BaseVirtualFolder, 0, -fileSize, false) //nolint:errcheck
|
2020-08-11 21:56:10 +00:00
|
|
|
if vfolder.IsIncludedInUserQuota() {
|
2021-02-16 18:11:36 +00:00
|
|
|
dataprovider.UpdateUserQuota(&c.User, 0, -fileSize, false) //nolint:errcheck
|
2020-08-11 21:56:10 +00:00
|
|
|
}
|
|
|
|
} else {
|
2021-02-16 18:11:36 +00:00
|
|
|
dataprovider.UpdateUserQuota(&c.User, 0, -fileSize, false) //nolint:errcheck
|
2020-08-11 21:56:10 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
initialSize = fileSize
|
2022-01-20 17:19:20 +00:00
|
|
|
truncatedSize = fileSize
|
2020-08-11 21:56:10 +00:00
|
|
|
}
|
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
vfs.SetPathPermissions(fs, filePath, c.User.GetUID(), c.User.GetGID())
|
2020-08-11 21:56:10 +00:00
|
|
|
|
2021-05-31 19:45:29 +00:00
|
|
|
baseTransfer := common.NewBaseTransfer(file, c.BaseConnection, cancelFn, resolvedPath, filePath, requestPath,
|
2022-01-30 10:42:36 +00:00
|
|
|
common.TransferUpload, 0, initialSize, maxWriteSize, truncatedSize, false, fs, transferQuota)
|
2022-11-19 18:39:28 +00:00
|
|
|
mtime := c.getModificationTime()
|
|
|
|
baseTransfer.SetTimes(resolvedPath, mtime, mtime)
|
2020-08-11 21:56:10 +00:00
|
|
|
|
2020-11-04 18:11:40 +00:00
|
|
|
return newWebDavFile(baseTransfer, w, nil), nil
|
2020-08-11 21:56:10 +00:00
|
|
|
}
|