connection.go 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342
  1. package common
  2. import (
  3. "errors"
  4. "fmt"
  5. "os"
  6. "path"
  7. "strings"
  8. "sync"
  9. "sync/atomic"
  10. "time"
  11. ftpserver "github.com/fclairamb/ftpserverlib"
  12. "github.com/pkg/sftp"
  13. "github.com/sftpgo/sdk"
  14. "github.com/drakkan/sftpgo/v2/dataprovider"
  15. "github.com/drakkan/sftpgo/v2/logger"
  16. "github.com/drakkan/sftpgo/v2/util"
  17. "github.com/drakkan/sftpgo/v2/vfs"
  18. )
  19. // BaseConnection defines common fields for a connection using any supported protocol
  20. type BaseConnection struct {
  21. // last activity for this connection.
  22. // Since this field is accessed atomically we put it as first element of the struct to achieve 64 bit alignment
  23. lastActivity int64
  24. // unique ID for a transfer.
  25. // This field is accessed atomically so we put it at the beginning of the struct to achieve 64 bit alignment
  26. transferID int64
  27. // Unique identifier for the connection
  28. ID string
  29. // user associated with this connection if any
  30. User dataprovider.User
  31. // start time for this connection
  32. startTime time.Time
  33. protocol string
  34. remoteAddr string
  35. localAddr string
  36. sync.RWMutex
  37. activeTransfers []ActiveTransfer
  38. }
  39. // NewBaseConnection returns a new BaseConnection
  40. func NewBaseConnection(id, protocol, localAddr, remoteAddr string, user dataprovider.User) *BaseConnection {
  41. connID := id
  42. if util.IsStringInSlice(protocol, supportedProtocols) {
  43. connID = fmt.Sprintf("%v_%v", protocol, id)
  44. }
  45. user.UploadBandwidth, user.DownloadBandwidth = user.GetBandwidthForIP(util.GetIPFromRemoteAddress(remoteAddr), connID)
  46. return &BaseConnection{
  47. ID: connID,
  48. User: user,
  49. startTime: time.Now(),
  50. protocol: protocol,
  51. localAddr: localAddr,
  52. remoteAddr: remoteAddr,
  53. lastActivity: time.Now().UnixNano(),
  54. transferID: 0,
  55. }
  56. }
  57. // Log outputs a log entry to the configured logger
  58. func (c *BaseConnection) Log(level logger.LogLevel, format string, v ...interface{}) {
  59. logger.Log(level, c.protocol, c.ID, format, v...)
  60. }
  61. // GetTransferID returns an unique transfer ID for this connection
  62. func (c *BaseConnection) GetTransferID() int64 {
  63. return atomic.AddInt64(&c.transferID, 1)
  64. }
  65. // GetID returns the connection ID
  66. func (c *BaseConnection) GetID() string {
  67. return c.ID
  68. }
  69. // GetUsername returns the authenticated username associated with this connection if any
  70. func (c *BaseConnection) GetUsername() string {
  71. return c.User.Username
  72. }
  73. // GetProtocol returns the protocol for the connection
  74. func (c *BaseConnection) GetProtocol() string {
  75. return c.protocol
  76. }
  77. // GetRemoteIP returns the remote ip address
  78. func (c *BaseConnection) GetRemoteIP() string {
  79. return util.GetIPFromRemoteAddress(c.remoteAddr)
  80. }
  81. // SetProtocol sets the protocol for this connection
  82. func (c *BaseConnection) SetProtocol(protocol string) {
  83. c.protocol = protocol
  84. if util.IsStringInSlice(c.protocol, supportedProtocols) {
  85. c.ID = fmt.Sprintf("%v_%v", c.protocol, c.ID)
  86. }
  87. }
  88. // GetConnectionTime returns the initial connection time
  89. func (c *BaseConnection) GetConnectionTime() time.Time {
  90. return c.startTime
  91. }
  92. // UpdateLastActivity updates last activity for this connection
  93. func (c *BaseConnection) UpdateLastActivity() {
  94. atomic.StoreInt64(&c.lastActivity, time.Now().UnixNano())
  95. }
  96. // GetLastActivity returns the last connection activity
  97. func (c *BaseConnection) GetLastActivity() time.Time {
  98. return time.Unix(0, atomic.LoadInt64(&c.lastActivity))
  99. }
  100. // CloseFS closes the underlying fs
  101. func (c *BaseConnection) CloseFS() error {
  102. return c.User.CloseFs()
  103. }
  104. // AddTransfer associates a new transfer to this connection
  105. func (c *BaseConnection) AddTransfer(t ActiveTransfer) {
  106. c.Lock()
  107. defer c.Unlock()
  108. c.activeTransfers = append(c.activeTransfers, t)
  109. c.Log(logger.LevelDebug, "transfer added, id: %v, active transfers: %v", t.GetID(), len(c.activeTransfers))
  110. if t.GetMaxAllowedSize() > 0 {
  111. folderName := ""
  112. if t.GetType() == TransferUpload {
  113. vfolder, err := c.User.GetVirtualFolderForPath(path.Dir(t.GetVirtualPath()))
  114. if err == nil {
  115. if !vfolder.IsIncludedInUserQuota() {
  116. folderName = vfolder.Name
  117. }
  118. }
  119. }
  120. go transfersChecker.AddTransfer(dataprovider.ActiveTransfer{
  121. ID: t.GetID(),
  122. Type: t.GetType(),
  123. ConnID: c.ID,
  124. Username: c.GetUsername(),
  125. FolderName: folderName,
  126. TruncatedSize: t.GetTruncatedSize(),
  127. CreatedAt: util.GetTimeAsMsSinceEpoch(time.Now()),
  128. UpdatedAt: util.GetTimeAsMsSinceEpoch(time.Now()),
  129. })
  130. }
  131. }
  132. // RemoveTransfer removes the specified transfer from the active ones
  133. func (c *BaseConnection) RemoveTransfer(t ActiveTransfer) {
  134. c.Lock()
  135. defer c.Unlock()
  136. if t.GetMaxAllowedSize() > 0 {
  137. go transfersChecker.RemoveTransfer(t.GetID(), c.ID)
  138. }
  139. for idx, transfer := range c.activeTransfers {
  140. if transfer.GetID() == t.GetID() {
  141. lastIdx := len(c.activeTransfers) - 1
  142. c.activeTransfers[idx] = c.activeTransfers[lastIdx]
  143. c.activeTransfers[lastIdx] = nil
  144. c.activeTransfers = c.activeTransfers[:lastIdx]
  145. c.Log(logger.LevelDebug, "transfer removed, id: %v active transfers: %v", t.GetID(), len(c.activeTransfers))
  146. return
  147. }
  148. }
  149. c.Log(logger.LevelWarn, "transfer to remove with id %v not found!", t.GetID())
  150. }
  151. // SignalTransferClose makes the transfer fail on the next read/write with the
  152. // specified error
  153. func (c *BaseConnection) SignalTransferClose(transferID int64, err error) {
  154. c.RLock()
  155. defer c.RUnlock()
  156. for _, t := range c.activeTransfers {
  157. if t.GetID() == transferID {
  158. c.Log(logger.LevelInfo, "signal transfer close for transfer id %v", transferID)
  159. t.SignalClose(err)
  160. }
  161. }
  162. }
  163. // GetTransfers returns the active transfers
  164. func (c *BaseConnection) GetTransfers() []ConnectionTransfer {
  165. c.RLock()
  166. defer c.RUnlock()
  167. transfers := make([]ConnectionTransfer, 0, len(c.activeTransfers))
  168. for _, t := range c.activeTransfers {
  169. var operationType string
  170. switch t.GetType() {
  171. case TransferDownload:
  172. operationType = operationDownload
  173. case TransferUpload:
  174. operationType = operationUpload
  175. }
  176. transfers = append(transfers, ConnectionTransfer{
  177. ID: t.GetID(),
  178. OperationType: operationType,
  179. StartTime: util.GetTimeAsMsSinceEpoch(t.GetStartTime()),
  180. Size: t.GetSize(),
  181. VirtualPath: t.GetVirtualPath(),
  182. MaxAllowedSize: t.GetMaxAllowedSize(),
  183. ULSize: t.GetUploadedSize(),
  184. DLSize: t.GetDownloadedSize(),
  185. })
  186. }
  187. return transfers
  188. }
  189. // SignalTransfersAbort signals to the active transfers to exit as soon as possible
  190. func (c *BaseConnection) SignalTransfersAbort() error {
  191. c.RLock()
  192. defer c.RUnlock()
  193. if len(c.activeTransfers) == 0 {
  194. return errors.New("no active transfer found")
  195. }
  196. for _, t := range c.activeTransfers {
  197. t.SignalClose(ErrTransferAborted)
  198. }
  199. return nil
  200. }
  201. func (c *BaseConnection) getRealFsPath(fsPath string) string {
  202. c.RLock()
  203. defer c.RUnlock()
  204. for _, t := range c.activeTransfers {
  205. if p := t.GetRealFsPath(fsPath); len(p) > 0 {
  206. return p
  207. }
  208. }
  209. return fsPath
  210. }
  211. func (c *BaseConnection) setTimes(fsPath string, atime time.Time, mtime time.Time) bool {
  212. c.RLock()
  213. defer c.RUnlock()
  214. for _, t := range c.activeTransfers {
  215. if t.SetTimes(fsPath, atime, mtime) {
  216. return true
  217. }
  218. }
  219. return false
  220. }
  221. func (c *BaseConnection) truncateOpenHandle(fsPath string, size int64) (int64, error) {
  222. c.RLock()
  223. defer c.RUnlock()
  224. for _, t := range c.activeTransfers {
  225. initialSize, err := t.Truncate(fsPath, size)
  226. if err != errTransferMismatch {
  227. return initialSize, err
  228. }
  229. }
  230. return 0, errNoTransfer
  231. }
  232. // ListDir reads the directory matching virtualPath and returns a list of directory entries
  233. func (c *BaseConnection) ListDir(virtualPath string) ([]os.FileInfo, error) {
  234. if !c.User.HasPerm(dataprovider.PermListItems, virtualPath) {
  235. return nil, c.GetPermissionDeniedError()
  236. }
  237. fs, fsPath, err := c.GetFsAndResolvedPath(virtualPath)
  238. if err != nil {
  239. return nil, err
  240. }
  241. files, err := fs.ReadDir(fsPath)
  242. if err != nil {
  243. c.Log(logger.LevelDebug, "error listing directory: %+v", err)
  244. return nil, c.GetFsError(fs, err)
  245. }
  246. return c.User.FilterListDir(files, virtualPath), nil
  247. }
  248. // CheckParentDirs tries to create the specified directory and any missing parent dirs
  249. func (c *BaseConnection) CheckParentDirs(virtualPath string) error {
  250. fs, err := c.User.GetFilesystemForPath(virtualPath, "")
  251. if err != nil {
  252. return err
  253. }
  254. if fs.HasVirtualFolders() {
  255. return nil
  256. }
  257. if _, err := c.DoStat(virtualPath, 0, false); !c.IsNotExistError(err) {
  258. return err
  259. }
  260. dirs := util.GetDirsForVirtualPath(virtualPath)
  261. for idx := len(dirs) - 1; idx >= 0; idx-- {
  262. fs, err = c.User.GetFilesystemForPath(dirs[idx], "")
  263. if err != nil {
  264. return err
  265. }
  266. if fs.HasVirtualFolders() {
  267. continue
  268. }
  269. if err = c.createDirIfMissing(dirs[idx]); err != nil {
  270. return fmt.Errorf("unable to check/create missing parent dir %#v for virtual path %#v: %w",
  271. dirs[idx], virtualPath, err)
  272. }
  273. }
  274. return nil
  275. }
  276. // CreateDir creates a new directory at the specified fsPath
  277. func (c *BaseConnection) CreateDir(virtualPath string, checkFilePatterns bool) error {
  278. if !c.User.HasPerm(dataprovider.PermCreateDirs, path.Dir(virtualPath)) {
  279. return c.GetPermissionDeniedError()
  280. }
  281. if checkFilePatterns {
  282. if ok, _ := c.User.IsFileAllowed(virtualPath); !ok {
  283. return c.GetPermissionDeniedError()
  284. }
  285. }
  286. if c.User.IsVirtualFolder(virtualPath) {
  287. c.Log(logger.LevelWarn, "mkdir not allowed %#v is a virtual folder", virtualPath)
  288. return c.GetPermissionDeniedError()
  289. }
  290. fs, fsPath, err := c.GetFsAndResolvedPath(virtualPath)
  291. if err != nil {
  292. return err
  293. }
  294. if err := fs.Mkdir(fsPath); err != nil {
  295. c.Log(logger.LevelError, "error creating dir: %#v error: %+v", fsPath, err)
  296. return c.GetFsError(fs, err)
  297. }
  298. vfs.SetPathPermissions(fs, fsPath, c.User.GetUID(), c.User.GetGID())
  299. logger.CommandLog(mkdirLogSender, fsPath, "", c.User.Username, "", c.ID, c.protocol, -1, -1, "", "", "", -1,
  300. c.localAddr, c.remoteAddr)
  301. ExecuteActionNotification(c, operationMkdir, fsPath, virtualPath, "", "", "", 0, nil)
  302. return nil
  303. }
  304. // IsRemoveFileAllowed returns an error if removing this file is not allowed
  305. func (c *BaseConnection) IsRemoveFileAllowed(virtualPath string) error {
  306. if !c.User.HasAnyPerm([]string{dataprovider.PermDeleteFiles, dataprovider.PermDelete}, path.Dir(virtualPath)) {
  307. return c.GetPermissionDeniedError()
  308. }
  309. if ok, policy := c.User.IsFileAllowed(virtualPath); !ok {
  310. c.Log(logger.LevelDebug, "removing file %#v is not allowed", virtualPath)
  311. return c.GetErrorForDeniedFile(policy)
  312. }
  313. return nil
  314. }
  315. // RemoveFile removes a file at the specified fsPath
  316. func (c *BaseConnection) RemoveFile(fs vfs.Fs, fsPath, virtualPath string, info os.FileInfo) error {
  317. if err := c.IsRemoveFileAllowed(virtualPath); err != nil {
  318. return err
  319. }
  320. size := info.Size()
  321. actionErr := ExecutePreAction(c, operationPreDelete, fsPath, virtualPath, size, 0)
  322. if actionErr == nil {
  323. c.Log(logger.LevelDebug, "remove for path %#v handled by pre-delete action", fsPath)
  324. } else {
  325. if err := fs.Remove(fsPath, false); err != nil {
  326. c.Log(logger.LevelError, "failed to remove file/symlink %#v: %+v", fsPath, err)
  327. return c.GetFsError(fs, err)
  328. }
  329. }
  330. logger.CommandLog(removeLogSender, fsPath, "", c.User.Username, "", c.ID, c.protocol, -1, -1, "", "", "", -1,
  331. c.localAddr, c.remoteAddr)
  332. if info.Mode()&os.ModeSymlink == 0 {
  333. vfolder, err := c.User.GetVirtualFolderForPath(path.Dir(virtualPath))
  334. if err == nil {
  335. dataprovider.UpdateVirtualFolderQuota(&vfolder.BaseVirtualFolder, -1, -size, false) //nolint:errcheck
  336. if vfolder.IsIncludedInUserQuota() {
  337. dataprovider.UpdateUserQuota(&c.User, -1, -size, false) //nolint:errcheck
  338. }
  339. } else {
  340. dataprovider.UpdateUserQuota(&c.User, -1, -size, false) //nolint:errcheck
  341. }
  342. }
  343. if actionErr != nil {
  344. ExecuteActionNotification(c, operationDelete, fsPath, virtualPath, "", "", "", size, nil)
  345. }
  346. return nil
  347. }
  348. // IsRemoveDirAllowed returns an error if removing this directory is not allowed
  349. func (c *BaseConnection) IsRemoveDirAllowed(fs vfs.Fs, fsPath, virtualPath string) error {
  350. if fs.GetRelativePath(fsPath) == "/" {
  351. c.Log(logger.LevelWarn, "removing root dir is not allowed")
  352. return c.GetPermissionDeniedError()
  353. }
  354. if c.User.IsVirtualFolder(virtualPath) {
  355. c.Log(logger.LevelWarn, "removing a virtual folder is not allowed: %#v", virtualPath)
  356. return c.GetPermissionDeniedError()
  357. }
  358. if c.User.HasVirtualFoldersInside(virtualPath) {
  359. c.Log(logger.LevelWarn, "removing a directory with a virtual folder inside is not allowed: %#v", virtualPath)
  360. return c.GetOpUnsupportedError()
  361. }
  362. if c.User.IsMappedPath(fsPath) {
  363. c.Log(logger.LevelWarn, "removing a directory mapped as virtual folder is not allowed: %#v", fsPath)
  364. return c.GetPermissionDeniedError()
  365. }
  366. if !c.User.HasAnyPerm([]string{dataprovider.PermDeleteDirs, dataprovider.PermDelete}, path.Dir(virtualPath)) {
  367. return c.GetPermissionDeniedError()
  368. }
  369. if ok, policy := c.User.IsFileAllowed(virtualPath); !ok {
  370. c.Log(logger.LevelDebug, "removing directory %#v is not allowed", virtualPath)
  371. return c.GetErrorForDeniedFile(policy)
  372. }
  373. return nil
  374. }
  375. // RemoveDir removes a directory at the specified fsPath
  376. func (c *BaseConnection) RemoveDir(virtualPath string) error {
  377. fs, fsPath, err := c.GetFsAndResolvedPath(virtualPath)
  378. if err != nil {
  379. return err
  380. }
  381. if err := c.IsRemoveDirAllowed(fs, fsPath, virtualPath); err != nil {
  382. return err
  383. }
  384. var fi os.FileInfo
  385. if fi, err = fs.Lstat(fsPath); err != nil {
  386. // see #149
  387. if fs.IsNotExist(err) && fs.HasVirtualFolders() {
  388. return nil
  389. }
  390. c.Log(logger.LevelError, "failed to remove a dir %#v: stat error: %+v", fsPath, err)
  391. return c.GetFsError(fs, err)
  392. }
  393. if !fi.IsDir() || fi.Mode()&os.ModeSymlink != 0 {
  394. c.Log(logger.LevelError, "cannot remove %#v is not a directory", fsPath)
  395. return c.GetGenericError(nil)
  396. }
  397. if err := fs.Remove(fsPath, true); err != nil {
  398. c.Log(logger.LevelError, "failed to remove directory %#v: %+v", fsPath, err)
  399. return c.GetFsError(fs, err)
  400. }
  401. logger.CommandLog(rmdirLogSender, fsPath, "", c.User.Username, "", c.ID, c.protocol, -1, -1, "", "", "", -1,
  402. c.localAddr, c.remoteAddr)
  403. ExecuteActionNotification(c, operationRmdir, fsPath, virtualPath, "", "", "", 0, nil)
  404. return nil
  405. }
  406. // Rename renames (moves) virtualSourcePath to virtualTargetPath
  407. func (c *BaseConnection) Rename(virtualSourcePath, virtualTargetPath string) error {
  408. if virtualSourcePath == virtualTargetPath {
  409. return fmt.Errorf("the rename source and target cannot be the same: %w", c.GetOpUnsupportedError())
  410. }
  411. fsSrc, fsSourcePath, err := c.GetFsAndResolvedPath(virtualSourcePath)
  412. if err != nil {
  413. return err
  414. }
  415. fsDst, fsTargetPath, err := c.GetFsAndResolvedPath(virtualTargetPath)
  416. if err != nil {
  417. return err
  418. }
  419. srcInfo, err := fsSrc.Lstat(fsSourcePath)
  420. if err != nil {
  421. return c.GetFsError(fsSrc, err)
  422. }
  423. if !c.isRenamePermitted(fsSrc, fsDst, fsSourcePath, fsTargetPath, virtualSourcePath, virtualTargetPath, srcInfo) {
  424. return c.GetPermissionDeniedError()
  425. }
  426. initialSize := int64(-1)
  427. if dstInfo, err := fsDst.Lstat(fsTargetPath); err == nil {
  428. if dstInfo.IsDir() {
  429. c.Log(logger.LevelWarn, "attempted to rename %#v overwriting an existing directory %#v",
  430. fsSourcePath, fsTargetPath)
  431. return c.GetOpUnsupportedError()
  432. }
  433. // we are overwriting an existing file/symlink
  434. if dstInfo.Mode().IsRegular() {
  435. initialSize = dstInfo.Size()
  436. }
  437. if !c.User.HasPerm(dataprovider.PermOverwrite, path.Dir(virtualTargetPath)) {
  438. c.Log(logger.LevelDebug, "renaming %#v -> %#v is not allowed. Target exists but the user %#v"+
  439. "has no overwrite permission", virtualSourcePath, virtualTargetPath, c.User.Username)
  440. return c.GetPermissionDeniedError()
  441. }
  442. }
  443. if srcInfo.IsDir() {
  444. if c.User.HasVirtualFoldersInside(virtualSourcePath) {
  445. c.Log(logger.LevelDebug, "renaming the folder %#v is not supported: it has virtual folders inside it",
  446. virtualSourcePath)
  447. return c.GetOpUnsupportedError()
  448. }
  449. if err = c.checkRecursiveRenameDirPermissions(fsSrc, fsDst, fsSourcePath, fsTargetPath); err != nil {
  450. c.Log(logger.LevelDebug, "error checking recursive permissions before renaming %#v: %+v", fsSourcePath, err)
  451. return err
  452. }
  453. }
  454. if !c.hasSpaceForRename(fsSrc, virtualSourcePath, virtualTargetPath, initialSize, fsSourcePath) {
  455. c.Log(logger.LevelInfo, "denying cross rename due to space limit")
  456. return c.GetGenericError(ErrQuotaExceeded)
  457. }
  458. if err := fsSrc.Rename(fsSourcePath, fsTargetPath); err != nil {
  459. c.Log(logger.LevelError, "failed to rename %#v -> %#v: %+v", fsSourcePath, fsTargetPath, err)
  460. return c.GetFsError(fsSrc, err)
  461. }
  462. vfs.SetPathPermissions(fsDst, fsTargetPath, c.User.GetUID(), c.User.GetGID())
  463. c.updateQuotaAfterRename(fsDst, virtualSourcePath, virtualTargetPath, fsTargetPath, initialSize) //nolint:errcheck
  464. logger.CommandLog(renameLogSender, fsSourcePath, fsTargetPath, c.User.Username, "", c.ID, c.protocol, -1, -1,
  465. "", "", "", -1, c.localAddr, c.remoteAddr)
  466. ExecuteActionNotification(c, operationRename, fsSourcePath, virtualSourcePath, fsTargetPath, virtualTargetPath,
  467. "", 0, nil)
  468. return nil
  469. }
  470. // CreateSymlink creates fsTargetPath as a symbolic link to fsSourcePath
  471. func (c *BaseConnection) CreateSymlink(virtualSourcePath, virtualTargetPath string) error {
  472. if c.isCrossFoldersRequest(virtualSourcePath, virtualTargetPath) {
  473. c.Log(logger.LevelWarn, "cross folder symlink is not supported, src: %v dst: %v", virtualSourcePath, virtualTargetPath)
  474. return c.GetOpUnsupportedError()
  475. }
  476. // we cannot have a cross folder request here so only one fs is enough
  477. fs, fsSourcePath, err := c.GetFsAndResolvedPath(virtualSourcePath)
  478. if err != nil {
  479. return err
  480. }
  481. fsTargetPath, err := fs.ResolvePath(virtualTargetPath)
  482. if err != nil {
  483. return c.GetFsError(fs, err)
  484. }
  485. if fs.GetRelativePath(fsSourcePath) == "/" {
  486. c.Log(logger.LevelError, "symlinking root dir is not allowed")
  487. return c.GetPermissionDeniedError()
  488. }
  489. if fs.GetRelativePath(fsTargetPath) == "/" {
  490. c.Log(logger.LevelError, "symlinking to root dir is not allowed")
  491. return c.GetPermissionDeniedError()
  492. }
  493. if !c.User.HasPerm(dataprovider.PermCreateSymlinks, path.Dir(virtualTargetPath)) {
  494. return c.GetPermissionDeniedError()
  495. }
  496. ok, policy := c.User.IsFileAllowed(virtualSourcePath)
  497. if !ok && policy == sdk.DenyPolicyHide {
  498. c.Log(logger.LevelError, "symlink source path %#v is not allowed", virtualSourcePath)
  499. return c.GetNotExistError()
  500. }
  501. if ok, _ = c.User.IsFileAllowed(virtualTargetPath); !ok {
  502. c.Log(logger.LevelError, "symlink target path %#v is not allowed", virtualTargetPath)
  503. return c.GetPermissionDeniedError()
  504. }
  505. if err := fs.Symlink(fsSourcePath, fsTargetPath); err != nil {
  506. c.Log(logger.LevelError, "failed to create symlink %#v -> %#v: %+v", fsSourcePath, fsTargetPath, err)
  507. return c.GetFsError(fs, err)
  508. }
  509. logger.CommandLog(symlinkLogSender, fsSourcePath, fsTargetPath, c.User.Username, "", c.ID, c.protocol, -1, -1, "",
  510. "", "", -1, c.localAddr, c.remoteAddr)
  511. return nil
  512. }
  513. func (c *BaseConnection) getPathForSetStatPerms(fs vfs.Fs, fsPath, virtualPath string) string {
  514. pathForPerms := virtualPath
  515. if fi, err := fs.Lstat(fsPath); err == nil {
  516. if fi.IsDir() {
  517. pathForPerms = path.Dir(virtualPath)
  518. }
  519. }
  520. return pathForPerms
  521. }
  522. // DoStat execute a Stat if mode = 0, Lstat if mode = 1
  523. func (c *BaseConnection) DoStat(virtualPath string, mode int, checkFilePatterns bool) (os.FileInfo, error) {
  524. // for some vfs we don't create intermediary folders so we cannot simply check
  525. // if virtualPath is a virtual folder
  526. vfolders := c.User.GetVirtualFoldersInPath(path.Dir(virtualPath))
  527. if _, ok := vfolders[virtualPath]; ok {
  528. return vfs.NewFileInfo(virtualPath, true, 0, time.Now(), false), nil
  529. }
  530. if checkFilePatterns {
  531. ok, policy := c.User.IsFileAllowed(virtualPath)
  532. if !ok && policy == sdk.DenyPolicyHide {
  533. return nil, c.GetNotExistError()
  534. }
  535. }
  536. var info os.FileInfo
  537. fs, fsPath, err := c.GetFsAndResolvedPath(virtualPath)
  538. if err != nil {
  539. return info, err
  540. }
  541. if mode == 1 {
  542. info, err = fs.Lstat(c.getRealFsPath(fsPath))
  543. } else {
  544. info, err = fs.Stat(c.getRealFsPath(fsPath))
  545. }
  546. if err != nil {
  547. c.Log(logger.LevelError, "stat error for path %#v: %+v", virtualPath, err)
  548. return info, c.GetFsError(fs, err)
  549. }
  550. if vfs.IsCryptOsFs(fs) {
  551. info = fs.(*vfs.CryptFs).ConvertFileInfo(info)
  552. }
  553. return info, nil
  554. }
  555. func (c *BaseConnection) createDirIfMissing(name string) error {
  556. _, err := c.DoStat(name, 0, false)
  557. if c.IsNotExistError(err) {
  558. return c.CreateDir(name, false)
  559. }
  560. return err
  561. }
  562. func (c *BaseConnection) ignoreSetStat(fs vfs.Fs) bool {
  563. if Config.SetstatMode == 1 {
  564. return true
  565. }
  566. if Config.SetstatMode == 2 && !vfs.IsLocalOrSFTPFs(fs) && !vfs.IsCryptOsFs(fs) {
  567. return true
  568. }
  569. return false
  570. }
  571. func (c *BaseConnection) handleChmod(fs vfs.Fs, fsPath, pathForPerms string, attributes *StatAttributes) error {
  572. if !c.User.HasPerm(dataprovider.PermChmod, pathForPerms) {
  573. return c.GetPermissionDeniedError()
  574. }
  575. if c.ignoreSetStat(fs) {
  576. return nil
  577. }
  578. if err := fs.Chmod(c.getRealFsPath(fsPath), attributes.Mode); err != nil {
  579. c.Log(logger.LevelError, "failed to chmod path %#v, mode: %v, err: %+v", fsPath, attributes.Mode.String(), err)
  580. return c.GetFsError(fs, err)
  581. }
  582. logger.CommandLog(chmodLogSender, fsPath, "", c.User.Username, attributes.Mode.String(), c.ID, c.protocol,
  583. -1, -1, "", "", "", -1, c.localAddr, c.remoteAddr)
  584. return nil
  585. }
  586. func (c *BaseConnection) handleChown(fs vfs.Fs, fsPath, pathForPerms string, attributes *StatAttributes) error {
  587. if !c.User.HasPerm(dataprovider.PermChown, pathForPerms) {
  588. return c.GetPermissionDeniedError()
  589. }
  590. if c.ignoreSetStat(fs) {
  591. return nil
  592. }
  593. if err := fs.Chown(c.getRealFsPath(fsPath), attributes.UID, attributes.GID); err != nil {
  594. c.Log(logger.LevelError, "failed to chown path %#v, uid: %v, gid: %v, err: %+v", fsPath, attributes.UID,
  595. attributes.GID, err)
  596. return c.GetFsError(fs, err)
  597. }
  598. logger.CommandLog(chownLogSender, fsPath, "", c.User.Username, "", c.ID, c.protocol, attributes.UID, attributes.GID,
  599. "", "", "", -1, c.localAddr, c.remoteAddr)
  600. return nil
  601. }
  602. func (c *BaseConnection) handleChtimes(fs vfs.Fs, fsPath, pathForPerms string, attributes *StatAttributes) error {
  603. if !c.User.HasPerm(dataprovider.PermChtimes, pathForPerms) {
  604. return c.GetPermissionDeniedError()
  605. }
  606. if Config.SetstatMode == 1 {
  607. return nil
  608. }
  609. isUploading := c.setTimes(fsPath, attributes.Atime, attributes.Mtime)
  610. if err := fs.Chtimes(c.getRealFsPath(fsPath), attributes.Atime, attributes.Mtime, isUploading); err != nil {
  611. c.setTimes(fsPath, time.Time{}, time.Time{})
  612. if errors.Is(err, vfs.ErrVfsUnsupported) && Config.SetstatMode == 2 {
  613. return nil
  614. }
  615. c.Log(logger.LevelError, "failed to chtimes for path %#v, access time: %v, modification time: %v, err: %+v",
  616. fsPath, attributes.Atime, attributes.Mtime, err)
  617. return c.GetFsError(fs, err)
  618. }
  619. accessTimeString := attributes.Atime.Format(chtimesFormat)
  620. modificationTimeString := attributes.Mtime.Format(chtimesFormat)
  621. logger.CommandLog(chtimesLogSender, fsPath, "", c.User.Username, "", c.ID, c.protocol, -1, -1,
  622. accessTimeString, modificationTimeString, "", -1, c.localAddr, c.remoteAddr)
  623. return nil
  624. }
  625. // SetStat set StatAttributes for the specified fsPath
  626. func (c *BaseConnection) SetStat(virtualPath string, attributes *StatAttributes) error {
  627. if ok, policy := c.User.IsFileAllowed(virtualPath); !ok {
  628. return c.GetErrorForDeniedFile(policy)
  629. }
  630. fs, fsPath, err := c.GetFsAndResolvedPath(virtualPath)
  631. if err != nil {
  632. return err
  633. }
  634. pathForPerms := c.getPathForSetStatPerms(fs, fsPath, virtualPath)
  635. if attributes.Flags&StatAttrTimes != 0 {
  636. if err = c.handleChtimes(fs, fsPath, pathForPerms, attributes); err != nil {
  637. return err
  638. }
  639. }
  640. if attributes.Flags&StatAttrPerms != 0 {
  641. if err = c.handleChmod(fs, fsPath, pathForPerms, attributes); err != nil {
  642. return err
  643. }
  644. }
  645. if attributes.Flags&StatAttrUIDGID != 0 {
  646. if err = c.handleChown(fs, fsPath, pathForPerms, attributes); err != nil {
  647. return err
  648. }
  649. }
  650. if attributes.Flags&StatAttrSize != 0 {
  651. if !c.User.HasPerm(dataprovider.PermOverwrite, pathForPerms) {
  652. return c.GetPermissionDeniedError()
  653. }
  654. if err = c.truncateFile(fs, fsPath, virtualPath, attributes.Size); err != nil {
  655. c.Log(logger.LevelError, "failed to truncate path %#v, size: %v, err: %+v", fsPath, attributes.Size, err)
  656. return c.GetFsError(fs, err)
  657. }
  658. logger.CommandLog(truncateLogSender, fsPath, "", c.User.Username, "", c.ID, c.protocol, -1, -1, "", "",
  659. "", attributes.Size, c.localAddr, c.remoteAddr)
  660. }
  661. return nil
  662. }
  663. func (c *BaseConnection) truncateFile(fs vfs.Fs, fsPath, virtualPath string, size int64) error {
  664. // check first if we have an open transfer for the given path and try to truncate the file already opened
  665. // if we found no transfer we truncate by path.
  666. var initialSize int64
  667. var err error
  668. initialSize, err = c.truncateOpenHandle(fsPath, size)
  669. if err == errNoTransfer {
  670. c.Log(logger.LevelDebug, "file path %#v not found in active transfers, execute trucate by path", fsPath)
  671. var info os.FileInfo
  672. info, err = fs.Stat(fsPath)
  673. if err != nil {
  674. return err
  675. }
  676. initialSize = info.Size()
  677. err = fs.Truncate(fsPath, size)
  678. }
  679. if err == nil && vfs.IsLocalOrSFTPFs(fs) {
  680. sizeDiff := initialSize - size
  681. vfolder, err := c.User.GetVirtualFolderForPath(path.Dir(virtualPath))
  682. if err == nil {
  683. dataprovider.UpdateVirtualFolderQuota(&vfolder.BaseVirtualFolder, 0, -sizeDiff, false) //nolint:errcheck
  684. if vfolder.IsIncludedInUserQuota() {
  685. dataprovider.UpdateUserQuota(&c.User, 0, -sizeDiff, false) //nolint:errcheck
  686. }
  687. } else {
  688. dataprovider.UpdateUserQuota(&c.User, 0, -sizeDiff, false) //nolint:errcheck
  689. }
  690. }
  691. return err
  692. }
  693. func (c *BaseConnection) checkRecursiveRenameDirPermissions(fsSrc, fsDst vfs.Fs, sourcePath, targetPath string) error {
  694. dstPerms := []string{
  695. dataprovider.PermCreateDirs,
  696. dataprovider.PermUpload,
  697. dataprovider.PermCreateSymlinks,
  698. }
  699. err := fsSrc.Walk(sourcePath, func(walkedPath string, info os.FileInfo, err error) error {
  700. if err != nil {
  701. return c.GetFsError(fsSrc, err)
  702. }
  703. dstPath := strings.Replace(walkedPath, sourcePath, targetPath, 1)
  704. virtualSrcPath := fsSrc.GetRelativePath(walkedPath)
  705. virtualDstPath := fsDst.GetRelativePath(dstPath)
  706. // walk scans the directory tree in order, checking the parent directory permissions we are sure that all contents
  707. // inside the parent path was checked. If the current dir has no subdirs with defined permissions inside it
  708. // and it has all the possible permissions we can stop scanning
  709. if !c.User.HasPermissionsInside(path.Dir(virtualSrcPath)) && !c.User.HasPermissionsInside(path.Dir(virtualDstPath)) {
  710. if c.User.HasPermsRenameAll(path.Dir(virtualSrcPath)) &&
  711. c.User.HasPermsRenameAll(path.Dir(virtualDstPath)) {
  712. return ErrSkipPermissionsCheck
  713. }
  714. if c.User.HasPermsDeleteAll(path.Dir(virtualSrcPath)) &&
  715. c.User.HasPerms(dstPerms, path.Dir(virtualDstPath)) {
  716. return ErrSkipPermissionsCheck
  717. }
  718. }
  719. if !c.isRenamePermitted(fsSrc, fsDst, walkedPath, dstPath, virtualSrcPath, virtualDstPath, info) {
  720. c.Log(logger.LevelInfo, "rename %#v -> %#v is not allowed, virtual destination path: %#v",
  721. walkedPath, dstPath, virtualDstPath)
  722. return c.GetPermissionDeniedError()
  723. }
  724. return nil
  725. })
  726. if err == ErrSkipPermissionsCheck {
  727. err = nil
  728. }
  729. return err
  730. }
  731. func (c *BaseConnection) hasRenamePerms(virtualSourcePath, virtualTargetPath string, fi os.FileInfo) bool {
  732. if c.User.HasPermsRenameAll(path.Dir(virtualSourcePath)) &&
  733. c.User.HasPermsRenameAll(path.Dir(virtualTargetPath)) {
  734. return true
  735. }
  736. if fi == nil {
  737. // we don't know if this is a file or a directory we have to check all the required permissions
  738. if !c.User.HasPermsDeleteAll(path.Dir(virtualSourcePath)) {
  739. return false
  740. }
  741. dstPerms := []string{
  742. dataprovider.PermCreateDirs,
  743. dataprovider.PermUpload,
  744. dataprovider.PermCreateSymlinks,
  745. }
  746. return c.User.HasPerms(dstPerms, path.Dir(virtualTargetPath))
  747. }
  748. if fi.IsDir() {
  749. if c.User.HasPerm(dataprovider.PermRenameDirs, path.Dir(virtualSourcePath)) &&
  750. c.User.HasPerm(dataprovider.PermRenameDirs, path.Dir(virtualTargetPath)) {
  751. return true
  752. }
  753. if !c.User.HasAnyPerm([]string{dataprovider.PermDeleteDirs, dataprovider.PermDelete}, path.Dir(virtualSourcePath)) {
  754. return false
  755. }
  756. return c.User.HasPerm(dataprovider.PermCreateDirs, path.Dir(virtualTargetPath))
  757. }
  758. // file or symlink
  759. if c.User.HasPerm(dataprovider.PermRenameFiles, path.Dir(virtualSourcePath)) &&
  760. c.User.HasPerm(dataprovider.PermRenameFiles, path.Dir(virtualTargetPath)) {
  761. return true
  762. }
  763. if !c.User.HasAnyPerm([]string{dataprovider.PermDeleteFiles, dataprovider.PermDelete}, path.Dir(virtualSourcePath)) {
  764. return false
  765. }
  766. if fi.Mode()&os.ModeSymlink != 0 {
  767. return c.User.HasPerm(dataprovider.PermCreateSymlinks, path.Dir(virtualTargetPath))
  768. }
  769. return c.User.HasPerm(dataprovider.PermUpload, path.Dir(virtualTargetPath))
  770. }
  771. func (c *BaseConnection) isRenamePermitted(fsSrc, fsDst vfs.Fs, fsSourcePath, fsTargetPath, virtualSourcePath, virtualTargetPath string, fi os.FileInfo) bool {
  772. if !c.isLocalOrSameFolderRename(virtualSourcePath, virtualTargetPath) {
  773. c.Log(logger.LevelInfo, "rename %#v->%#v is not allowed: the paths must be local or on the same virtual folder",
  774. virtualSourcePath, virtualTargetPath)
  775. return false
  776. }
  777. if c.User.IsMappedPath(fsSourcePath) && vfs.IsLocalOrCryptoFs(fsSrc) {
  778. c.Log(logger.LevelWarn, "renaming a directory mapped as virtual folder is not allowed: %#v", fsSourcePath)
  779. return false
  780. }
  781. if c.User.IsMappedPath(fsTargetPath) && vfs.IsLocalOrCryptoFs(fsDst) {
  782. c.Log(logger.LevelWarn, "renaming to a directory mapped as virtual folder is not allowed: %#v", fsTargetPath)
  783. return false
  784. }
  785. if fsSrc.GetRelativePath(fsSourcePath) == "/" {
  786. c.Log(logger.LevelWarn, "renaming root dir is not allowed")
  787. return false
  788. }
  789. if c.User.IsVirtualFolder(virtualSourcePath) || c.User.IsVirtualFolder(virtualTargetPath) {
  790. c.Log(logger.LevelWarn, "renaming a virtual folder is not allowed")
  791. return false
  792. }
  793. isSrcAllowed, _ := c.User.IsFileAllowed(virtualSourcePath)
  794. isDstAllowed, _ := c.User.IsFileAllowed(virtualTargetPath)
  795. if !isSrcAllowed || !isDstAllowed {
  796. c.Log(logger.LevelDebug, "renaming source: %#v to target: %#v not allowed", virtualSourcePath,
  797. virtualTargetPath)
  798. return false
  799. }
  800. return c.hasRenamePerms(virtualSourcePath, virtualTargetPath, fi)
  801. }
  802. func (c *BaseConnection) hasSpaceForRename(fs vfs.Fs, virtualSourcePath, virtualTargetPath string, initialSize int64,
  803. fsSourcePath string) bool {
  804. if dataprovider.GetQuotaTracking() == 0 {
  805. return true
  806. }
  807. sourceFolder, errSrc := c.User.GetVirtualFolderForPath(path.Dir(virtualSourcePath))
  808. dstFolder, errDst := c.User.GetVirtualFolderForPath(path.Dir(virtualTargetPath))
  809. if errSrc != nil && errDst != nil {
  810. // rename inside the user home dir
  811. return true
  812. }
  813. if errSrc == nil && errDst == nil {
  814. // rename between virtual folders
  815. if sourceFolder.Name == dstFolder.Name {
  816. // rename inside the same virtual folder
  817. return true
  818. }
  819. }
  820. if errSrc != nil && dstFolder.IsIncludedInUserQuota() {
  821. // rename between user root dir and a virtual folder included in user quota
  822. return true
  823. }
  824. quotaResult := c.HasSpace(true, false, virtualTargetPath)
  825. return c.hasSpaceForCrossRename(fs, quotaResult, initialSize, fsSourcePath)
  826. }
  827. // hasSpaceForCrossRename checks the quota after a rename between different folders
  828. func (c *BaseConnection) hasSpaceForCrossRename(fs vfs.Fs, quotaResult vfs.QuotaCheckResult, initialSize int64, sourcePath string) bool {
  829. if !quotaResult.HasSpace && initialSize == -1 {
  830. // we are over quota and this is not a file replace
  831. return false
  832. }
  833. fi, err := fs.Lstat(sourcePath)
  834. if err != nil {
  835. c.Log(logger.LevelError, "cross rename denied, stat error for path %#v: %v", sourcePath, err)
  836. return false
  837. }
  838. var sizeDiff int64
  839. var filesDiff int
  840. if fi.Mode().IsRegular() {
  841. sizeDiff = fi.Size()
  842. filesDiff = 1
  843. if initialSize != -1 {
  844. sizeDiff -= initialSize
  845. filesDiff = 0
  846. }
  847. } else if fi.IsDir() {
  848. filesDiff, sizeDiff, err = fs.GetDirSize(sourcePath)
  849. if err != nil {
  850. c.Log(logger.LevelError, "cross rename denied, error getting size for directory %#v: %v", sourcePath, err)
  851. return false
  852. }
  853. }
  854. if !quotaResult.HasSpace && initialSize != -1 {
  855. // we are over quota but we are overwriting an existing file so we check if the quota size after the rename is ok
  856. if quotaResult.QuotaSize == 0 {
  857. return true
  858. }
  859. c.Log(logger.LevelDebug, "cross rename overwrite, source %#v, used size %v, size to add %v",
  860. sourcePath, quotaResult.UsedSize, sizeDiff)
  861. quotaResult.UsedSize += sizeDiff
  862. return quotaResult.GetRemainingSize() >= 0
  863. }
  864. if quotaResult.QuotaFiles > 0 {
  865. remainingFiles := quotaResult.GetRemainingFiles()
  866. c.Log(logger.LevelDebug, "cross rename, source %#v remaining file %v to add %v", sourcePath,
  867. remainingFiles, filesDiff)
  868. if remainingFiles < filesDiff {
  869. return false
  870. }
  871. }
  872. if quotaResult.QuotaSize > 0 {
  873. remainingSize := quotaResult.GetRemainingSize()
  874. c.Log(logger.LevelDebug, "cross rename, source %#v remaining size %v to add %v", sourcePath,
  875. remainingSize, sizeDiff)
  876. if remainingSize < sizeDiff {
  877. return false
  878. }
  879. }
  880. return true
  881. }
  882. // GetMaxWriteSize returns the allowed size for an upload or an error
  883. // if no enough size is available for a resume/append
  884. func (c *BaseConnection) GetMaxWriteSize(quotaResult vfs.QuotaCheckResult, isResume bool, fileSize int64, isUploadResumeSupported bool) (int64, error) {
  885. maxWriteSize := quotaResult.GetRemainingSize()
  886. if isResume {
  887. if !isUploadResumeSupported {
  888. return 0, c.GetOpUnsupportedError()
  889. }
  890. if c.User.Filters.MaxUploadFileSize > 0 && c.User.Filters.MaxUploadFileSize <= fileSize {
  891. return 0, c.GetQuotaExceededError()
  892. }
  893. if c.User.Filters.MaxUploadFileSize > 0 {
  894. maxUploadSize := c.User.Filters.MaxUploadFileSize - fileSize
  895. if maxUploadSize < maxWriteSize || maxWriteSize == 0 {
  896. maxWriteSize = maxUploadSize
  897. }
  898. }
  899. } else {
  900. if maxWriteSize > 0 {
  901. maxWriteSize += fileSize
  902. }
  903. if c.User.Filters.MaxUploadFileSize > 0 && (c.User.Filters.MaxUploadFileSize < maxWriteSize || maxWriteSize == 0) {
  904. maxWriteSize = c.User.Filters.MaxUploadFileSize
  905. }
  906. }
  907. return maxWriteSize, nil
  908. }
  909. // HasSpace checks user's quota usage
  910. func (c *BaseConnection) HasSpace(checkFiles, getUsage bool, requestPath string) vfs.QuotaCheckResult {
  911. result := vfs.QuotaCheckResult{
  912. HasSpace: true,
  913. AllowedSize: 0,
  914. AllowedFiles: 0,
  915. UsedSize: 0,
  916. UsedFiles: 0,
  917. QuotaSize: 0,
  918. QuotaFiles: 0,
  919. }
  920. if dataprovider.GetQuotaTracking() == 0 {
  921. return result
  922. }
  923. var err error
  924. var vfolder vfs.VirtualFolder
  925. vfolder, err = c.User.GetVirtualFolderForPath(path.Dir(requestPath))
  926. if err == nil && !vfolder.IsIncludedInUserQuota() {
  927. if vfolder.HasNoQuotaRestrictions(checkFiles) && !getUsage {
  928. return result
  929. }
  930. result.QuotaSize = vfolder.QuotaSize
  931. result.QuotaFiles = vfolder.QuotaFiles
  932. result.UsedFiles, result.UsedSize, err = dataprovider.GetUsedVirtualFolderQuota(vfolder.Name)
  933. } else {
  934. if c.User.HasNoQuotaRestrictions(checkFiles) && !getUsage {
  935. return result
  936. }
  937. result.QuotaSize = c.User.QuotaSize
  938. result.QuotaFiles = c.User.QuotaFiles
  939. result.UsedFiles, result.UsedSize, err = dataprovider.GetUsedQuota(c.User.Username)
  940. }
  941. if err != nil {
  942. c.Log(logger.LevelError, "error getting used quota for %#v request path %#v: %v", c.User.Username, requestPath, err)
  943. result.HasSpace = false
  944. return result
  945. }
  946. result.AllowedFiles = result.QuotaFiles - result.UsedFiles
  947. result.AllowedSize = result.QuotaSize - result.UsedSize
  948. if (checkFiles && result.QuotaFiles > 0 && result.UsedFiles >= result.QuotaFiles) ||
  949. (result.QuotaSize > 0 && result.UsedSize >= result.QuotaSize) {
  950. c.Log(logger.LevelDebug, "quota exceed for user %#v, request path %#v, num files: %v/%v, size: %v/%v check files: %v",
  951. c.User.Username, requestPath, result.UsedFiles, result.QuotaFiles, result.UsedSize, result.QuotaSize, checkFiles)
  952. result.HasSpace = false
  953. return result
  954. }
  955. return result
  956. }
  957. // returns true if this is a rename on the same fs or local virtual folders
  958. func (c *BaseConnection) isLocalOrSameFolderRename(virtualSourcePath, virtualTargetPath string) bool {
  959. sourceFolder, errSrc := c.User.GetVirtualFolderForPath(virtualSourcePath)
  960. dstFolder, errDst := c.User.GetVirtualFolderForPath(virtualTargetPath)
  961. if errSrc != nil && errDst != nil {
  962. return true
  963. }
  964. if errSrc == nil && errDst == nil {
  965. if sourceFolder.Name == dstFolder.Name {
  966. return true
  967. }
  968. // we have different folders, only local fs is supported
  969. if sourceFolder.FsConfig.Provider == sdk.LocalFilesystemProvider &&
  970. dstFolder.FsConfig.Provider == sdk.LocalFilesystemProvider {
  971. return true
  972. }
  973. return false
  974. }
  975. if c.User.FsConfig.Provider != sdk.LocalFilesystemProvider {
  976. return false
  977. }
  978. if errSrc == nil {
  979. if sourceFolder.FsConfig.Provider == sdk.LocalFilesystemProvider {
  980. return true
  981. }
  982. }
  983. if errDst == nil {
  984. if dstFolder.FsConfig.Provider == sdk.LocalFilesystemProvider {
  985. return true
  986. }
  987. }
  988. return false
  989. }
  990. func (c *BaseConnection) isCrossFoldersRequest(virtualSourcePath, virtualTargetPath string) bool {
  991. sourceFolder, errSrc := c.User.GetVirtualFolderForPath(virtualSourcePath)
  992. dstFolder, errDst := c.User.GetVirtualFolderForPath(virtualTargetPath)
  993. if errSrc != nil && errDst != nil {
  994. return false
  995. }
  996. if errSrc == nil && errDst == nil {
  997. return sourceFolder.Name != dstFolder.Name
  998. }
  999. return true
  1000. }
  1001. func (c *BaseConnection) updateQuotaMoveBetweenVFolders(sourceFolder, dstFolder *vfs.VirtualFolder, initialSize,
  1002. filesSize int64, numFiles int) {
  1003. if sourceFolder.Name == dstFolder.Name {
  1004. // both files are inside the same virtual folder
  1005. if initialSize != -1 {
  1006. dataprovider.UpdateVirtualFolderQuota(&dstFolder.BaseVirtualFolder, -numFiles, -initialSize, false) //nolint:errcheck
  1007. if dstFolder.IsIncludedInUserQuota() {
  1008. dataprovider.UpdateUserQuota(&c.User, -numFiles, -initialSize, false) //nolint:errcheck
  1009. }
  1010. }
  1011. return
  1012. }
  1013. // files are inside different virtual folders
  1014. dataprovider.UpdateVirtualFolderQuota(&sourceFolder.BaseVirtualFolder, -numFiles, -filesSize, false) //nolint:errcheck
  1015. if sourceFolder.IsIncludedInUserQuota() {
  1016. dataprovider.UpdateUserQuota(&c.User, -numFiles, -filesSize, false) //nolint:errcheck
  1017. }
  1018. if initialSize == -1 {
  1019. dataprovider.UpdateVirtualFolderQuota(&dstFolder.BaseVirtualFolder, numFiles, filesSize, false) //nolint:errcheck
  1020. if dstFolder.IsIncludedInUserQuota() {
  1021. dataprovider.UpdateUserQuota(&c.User, numFiles, filesSize, false) //nolint:errcheck
  1022. }
  1023. } else {
  1024. // we cannot have a directory here, initialSize != -1 only for files
  1025. dataprovider.UpdateVirtualFolderQuota(&dstFolder.BaseVirtualFolder, 0, filesSize-initialSize, false) //nolint:errcheck
  1026. if dstFolder.IsIncludedInUserQuota() {
  1027. dataprovider.UpdateUserQuota(&c.User, 0, filesSize-initialSize, false) //nolint:errcheck
  1028. }
  1029. }
  1030. }
  1031. func (c *BaseConnection) updateQuotaMoveFromVFolder(sourceFolder *vfs.VirtualFolder, initialSize, filesSize int64, numFiles int) {
  1032. // move between a virtual folder and the user home dir
  1033. dataprovider.UpdateVirtualFolderQuota(&sourceFolder.BaseVirtualFolder, -numFiles, -filesSize, false) //nolint:errcheck
  1034. if sourceFolder.IsIncludedInUserQuota() {
  1035. dataprovider.UpdateUserQuota(&c.User, -numFiles, -filesSize, false) //nolint:errcheck
  1036. }
  1037. if initialSize == -1 {
  1038. dataprovider.UpdateUserQuota(&c.User, numFiles, filesSize, false) //nolint:errcheck
  1039. } else {
  1040. // we cannot have a directory here, initialSize != -1 only for files
  1041. dataprovider.UpdateUserQuota(&c.User, 0, filesSize-initialSize, false) //nolint:errcheck
  1042. }
  1043. }
  1044. func (c *BaseConnection) updateQuotaMoveToVFolder(dstFolder *vfs.VirtualFolder, initialSize, filesSize int64, numFiles int) {
  1045. // move between the user home dir and a virtual folder
  1046. dataprovider.UpdateUserQuota(&c.User, -numFiles, -filesSize, false) //nolint:errcheck
  1047. if initialSize == -1 {
  1048. dataprovider.UpdateVirtualFolderQuota(&dstFolder.BaseVirtualFolder, numFiles, filesSize, false) //nolint:errcheck
  1049. if dstFolder.IsIncludedInUserQuota() {
  1050. dataprovider.UpdateUserQuota(&c.User, numFiles, filesSize, false) //nolint:errcheck
  1051. }
  1052. } else {
  1053. // we cannot have a directory here, initialSize != -1 only for files
  1054. dataprovider.UpdateVirtualFolderQuota(&dstFolder.BaseVirtualFolder, 0, filesSize-initialSize, false) //nolint:errcheck
  1055. if dstFolder.IsIncludedInUserQuota() {
  1056. dataprovider.UpdateUserQuota(&c.User, 0, filesSize-initialSize, false) //nolint:errcheck
  1057. }
  1058. }
  1059. }
  1060. func (c *BaseConnection) updateQuotaAfterRename(fs vfs.Fs, virtualSourcePath, virtualTargetPath, targetPath string, initialSize int64) error {
  1061. if dataprovider.GetQuotaTracking() == 0 {
  1062. return nil
  1063. }
  1064. // we don't allow to overwrite an existing directory so targetPath can be:
  1065. // - a new file, a symlink is as a new file here
  1066. // - a file overwriting an existing one
  1067. // - a new directory
  1068. // initialSize != -1 only when overwriting files
  1069. sourceFolder, errSrc := c.User.GetVirtualFolderForPath(path.Dir(virtualSourcePath))
  1070. dstFolder, errDst := c.User.GetVirtualFolderForPath(path.Dir(virtualTargetPath))
  1071. if errSrc != nil && errDst != nil {
  1072. // both files are contained inside the user home dir
  1073. if initialSize != -1 {
  1074. // we cannot have a directory here, we are overwriting an existing file
  1075. // we need to subtract the size of the overwritten file from the user quota
  1076. dataprovider.UpdateUserQuota(&c.User, -1, -initialSize, false) //nolint:errcheck
  1077. }
  1078. return nil
  1079. }
  1080. filesSize := int64(0)
  1081. numFiles := 1
  1082. if fi, err := fs.Stat(targetPath); err == nil {
  1083. if fi.Mode().IsDir() {
  1084. numFiles, filesSize, err = fs.GetDirSize(targetPath)
  1085. if err != nil {
  1086. c.Log(logger.LevelError, "failed to update quota after rename, error scanning moved folder %#v: %v",
  1087. targetPath, err)
  1088. return err
  1089. }
  1090. } else {
  1091. filesSize = fi.Size()
  1092. }
  1093. } else {
  1094. c.Log(logger.LevelError, "failed to update quota after rename, file %#v stat error: %+v", targetPath, err)
  1095. return err
  1096. }
  1097. if errSrc == nil && errDst == nil {
  1098. c.updateQuotaMoveBetweenVFolders(&sourceFolder, &dstFolder, initialSize, filesSize, numFiles)
  1099. }
  1100. if errSrc == nil && errDst != nil {
  1101. c.updateQuotaMoveFromVFolder(&sourceFolder, initialSize, filesSize, numFiles)
  1102. }
  1103. if errSrc != nil && errDst == nil {
  1104. c.updateQuotaMoveToVFolder(&dstFolder, initialSize, filesSize, numFiles)
  1105. }
  1106. return nil
  1107. }
  1108. // IsNotExistError returns true if the specified fs error is not exist for the connection protocol
  1109. func (c *BaseConnection) IsNotExistError(err error) bool {
  1110. switch c.protocol {
  1111. case ProtocolSFTP:
  1112. return errors.Is(err, sftp.ErrSSHFxNoSuchFile)
  1113. case ProtocolWebDAV, ProtocolFTP, ProtocolHTTP, ProtocolHTTPShare, ProtocolDataRetention:
  1114. return errors.Is(err, os.ErrNotExist)
  1115. default:
  1116. return errors.Is(err, ErrNotExist)
  1117. }
  1118. }
  1119. // GetErrorForDeniedFile return permission denied or not exist error based on the specified policy
  1120. func (c *BaseConnection) GetErrorForDeniedFile(policy int) error {
  1121. switch policy {
  1122. case sdk.DenyPolicyHide:
  1123. return c.GetNotExistError()
  1124. default:
  1125. return c.GetPermissionDeniedError()
  1126. }
  1127. }
  1128. // GetPermissionDeniedError returns an appropriate permission denied error for the connection protocol
  1129. func (c *BaseConnection) GetPermissionDeniedError() error {
  1130. switch c.protocol {
  1131. case ProtocolSFTP:
  1132. return sftp.ErrSSHFxPermissionDenied
  1133. case ProtocolWebDAV, ProtocolFTP, ProtocolHTTP, ProtocolHTTPShare, ProtocolDataRetention:
  1134. return os.ErrPermission
  1135. default:
  1136. return ErrPermissionDenied
  1137. }
  1138. }
  1139. // GetNotExistError returns an appropriate not exist error for the connection protocol
  1140. func (c *BaseConnection) GetNotExistError() error {
  1141. switch c.protocol {
  1142. case ProtocolSFTP:
  1143. return sftp.ErrSSHFxNoSuchFile
  1144. case ProtocolWebDAV, ProtocolFTP, ProtocolHTTP, ProtocolHTTPShare, ProtocolDataRetention:
  1145. return os.ErrNotExist
  1146. default:
  1147. return ErrNotExist
  1148. }
  1149. }
  1150. // GetOpUnsupportedError returns an appropriate operation not supported error for the connection protocol
  1151. func (c *BaseConnection) GetOpUnsupportedError() error {
  1152. switch c.protocol {
  1153. case ProtocolSFTP:
  1154. return sftp.ErrSSHFxOpUnsupported
  1155. default:
  1156. return ErrOpUnsupported
  1157. }
  1158. }
  1159. func getQuotaExceededError(protocol string) error {
  1160. switch protocol {
  1161. case ProtocolSFTP:
  1162. return fmt.Errorf("%w: %v", sftp.ErrSSHFxFailure, ErrQuotaExceeded.Error())
  1163. case ProtocolFTP:
  1164. return ftpserver.ErrStorageExceeded
  1165. default:
  1166. return ErrQuotaExceeded
  1167. }
  1168. }
  1169. // GetQuotaExceededError returns an appropriate storage limit exceeded error for the connection protocol
  1170. func (c *BaseConnection) GetQuotaExceededError() error {
  1171. return getQuotaExceededError(c.protocol)
  1172. }
  1173. // IsQuotaExceededError returns true if the given error is a quota exceeded error
  1174. func (c *BaseConnection) IsQuotaExceededError(err error) bool {
  1175. switch c.protocol {
  1176. case ProtocolSFTP:
  1177. if err == nil {
  1178. return false
  1179. }
  1180. if errors.Is(err, ErrQuotaExceeded) {
  1181. return true
  1182. }
  1183. return errors.Is(err, sftp.ErrSSHFxFailure) && strings.Contains(err.Error(), ErrQuotaExceeded.Error())
  1184. case ProtocolFTP:
  1185. return errors.Is(err, ftpserver.ErrStorageExceeded) || errors.Is(err, ErrQuotaExceeded)
  1186. default:
  1187. return errors.Is(err, ErrQuotaExceeded)
  1188. }
  1189. }
  1190. // GetGenericError returns an appropriate generic error for the connection protocol
  1191. func (c *BaseConnection) GetGenericError(err error) error {
  1192. switch c.protocol {
  1193. case ProtocolSFTP:
  1194. if err == vfs.ErrStorageSizeUnavailable {
  1195. return fmt.Errorf("%w: %v", sftp.ErrSSHFxOpUnsupported, err.Error())
  1196. }
  1197. if err != nil {
  1198. if e, ok := err.(*os.PathError); ok {
  1199. return fmt.Errorf("%w: %v %v", sftp.ErrSSHFxFailure, e.Op, e.Err.Error())
  1200. }
  1201. return fmt.Errorf("%w: %v", sftp.ErrSSHFxFailure, err.Error())
  1202. }
  1203. return sftp.ErrSSHFxFailure
  1204. default:
  1205. if err == ErrPermissionDenied || err == ErrNotExist || err == ErrOpUnsupported ||
  1206. err == ErrQuotaExceeded || err == vfs.ErrStorageSizeUnavailable {
  1207. return err
  1208. }
  1209. return ErrGenericFailure
  1210. }
  1211. }
  1212. // GetFsError converts a filesystem error to a protocol error
  1213. func (c *BaseConnection) GetFsError(fs vfs.Fs, err error) error {
  1214. if fs.IsNotExist(err) {
  1215. return c.GetNotExistError()
  1216. } else if fs.IsPermission(err) {
  1217. return c.GetPermissionDeniedError()
  1218. } else if fs.IsNotSupported(err) {
  1219. return c.GetOpUnsupportedError()
  1220. } else if err != nil {
  1221. return c.GetGenericError(err)
  1222. }
  1223. return nil
  1224. }
  1225. // GetFsAndResolvedPath returns the fs and the fs path matching virtualPath
  1226. func (c *BaseConnection) GetFsAndResolvedPath(virtualPath string) (vfs.Fs, string, error) {
  1227. fs, err := c.User.GetFilesystemForPath(virtualPath, c.ID)
  1228. if err != nil {
  1229. if c.protocol == ProtocolWebDAV && strings.Contains(err.Error(), vfs.ErrSFTPLoop.Error()) {
  1230. // if there is an SFTP loop we return a permission error, for WebDAV, so the problematic folder
  1231. // will not be listed
  1232. return nil, "", c.GetPermissionDeniedError()
  1233. }
  1234. return nil, "", err
  1235. }
  1236. fsPath, err := fs.ResolvePath(virtualPath)
  1237. if err != nil {
  1238. return nil, "", c.GetFsError(fs, err)
  1239. }
  1240. return fs, fsPath, nil
  1241. }