connection.go 48 KB

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