connection.go 47 KB

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