connection.go 48 KB

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