connection.go 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410
  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/internal/dataprovider"
  28. "github.com/drakkan/sftpgo/v2/internal/logger"
  29. "github.com/drakkan/sftpgo/v2/internal/util"
  30. "github.com/drakkan/sftpgo/v2/internal/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) //nolint:errcheck
  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) //nolint:errcheck
  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) //nolint:errcheck
  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 %q overwriting an existing directory %q",
  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,
  468. virtualSourcePath, virtualTargetPath, srcInfo); err != nil {
  469. c.Log(logger.LevelDebug, "error checking recursive permissions before renaming %#v: %+v", fsSourcePath, err)
  470. return err
  471. }
  472. }
  473. if !c.hasSpaceForRename(fsSrc, virtualSourcePath, virtualTargetPath, initialSize, fsSourcePath) {
  474. c.Log(logger.LevelInfo, "denying cross rename due to space limit")
  475. return c.GetGenericError(ErrQuotaExceeded)
  476. }
  477. if err := fsSrc.Rename(fsSourcePath, fsTargetPath); err != nil {
  478. c.Log(logger.LevelError, "failed to rename %#v -> %#v: %+v", fsSourcePath, fsTargetPath, err)
  479. return c.GetFsError(fsSrc, err)
  480. }
  481. vfs.SetPathPermissions(fsDst, fsTargetPath, c.User.GetUID(), c.User.GetGID())
  482. c.updateQuotaAfterRename(fsDst, virtualSourcePath, virtualTargetPath, fsTargetPath, initialSize) //nolint:errcheck
  483. logger.CommandLog(renameLogSender, fsSourcePath, fsTargetPath, c.User.Username, "", c.ID, c.protocol, -1, -1,
  484. "", "", "", -1, c.localAddr, c.remoteAddr)
  485. ExecuteActionNotification(c, operationRename, fsSourcePath, virtualSourcePath, fsTargetPath, //nolint:errcheck
  486. virtualTargetPath, "", 0, nil)
  487. return nil
  488. }
  489. // CreateSymlink creates fsTargetPath as a symbolic link to fsSourcePath
  490. func (c *BaseConnection) CreateSymlink(virtualSourcePath, virtualTargetPath string) error {
  491. if c.isCrossFoldersRequest(virtualSourcePath, virtualTargetPath) {
  492. c.Log(logger.LevelWarn, "cross folder symlink is not supported, src: %v dst: %v", virtualSourcePath, virtualTargetPath)
  493. return c.GetOpUnsupportedError()
  494. }
  495. // we cannot have a cross folder request here so only one fs is enough
  496. fs, fsSourcePath, err := c.GetFsAndResolvedPath(virtualSourcePath)
  497. if err != nil {
  498. return err
  499. }
  500. fsTargetPath, err := fs.ResolvePath(virtualTargetPath)
  501. if err != nil {
  502. return c.GetFsError(fs, err)
  503. }
  504. if fs.GetRelativePath(fsSourcePath) == "/" {
  505. c.Log(logger.LevelError, "symlinking root dir is not allowed")
  506. return c.GetPermissionDeniedError()
  507. }
  508. if fs.GetRelativePath(fsTargetPath) == "/" {
  509. c.Log(logger.LevelError, "symlinking to root dir is not allowed")
  510. return c.GetPermissionDeniedError()
  511. }
  512. if !c.User.HasPerm(dataprovider.PermCreateSymlinks, path.Dir(virtualTargetPath)) {
  513. return c.GetPermissionDeniedError()
  514. }
  515. ok, policy := c.User.IsFileAllowed(virtualSourcePath)
  516. if !ok && policy == sdk.DenyPolicyHide {
  517. c.Log(logger.LevelError, "symlink source path %#v is not allowed", virtualSourcePath)
  518. return c.GetNotExistError()
  519. }
  520. if ok, _ = c.User.IsFileAllowed(virtualTargetPath); !ok {
  521. c.Log(logger.LevelError, "symlink target path %#v is not allowed", virtualTargetPath)
  522. return c.GetPermissionDeniedError()
  523. }
  524. if err := fs.Symlink(fsSourcePath, fsTargetPath); err != nil {
  525. c.Log(logger.LevelError, "failed to create symlink %#v -> %#v: %+v", fsSourcePath, fsTargetPath, err)
  526. return c.GetFsError(fs, err)
  527. }
  528. logger.CommandLog(symlinkLogSender, fsSourcePath, fsTargetPath, c.User.Username, "", c.ID, c.protocol, -1, -1, "",
  529. "", "", -1, c.localAddr, c.remoteAddr)
  530. return nil
  531. }
  532. func (c *BaseConnection) getPathForSetStatPerms(fs vfs.Fs, fsPath, virtualPath string) string {
  533. pathForPerms := virtualPath
  534. if fi, err := fs.Lstat(fsPath); err == nil {
  535. if fi.IsDir() {
  536. pathForPerms = path.Dir(virtualPath)
  537. }
  538. }
  539. return pathForPerms
  540. }
  541. // DoStat execute a Stat if mode = 0, Lstat if mode = 1
  542. func (c *BaseConnection) DoStat(virtualPath string, mode int, checkFilePatterns bool) (os.FileInfo, error) {
  543. // for some vfs we don't create intermediary folders so we cannot simply check
  544. // if virtualPath is a virtual folder
  545. vfolders := c.User.GetVirtualFoldersInPath(path.Dir(virtualPath))
  546. if _, ok := vfolders[virtualPath]; ok {
  547. return vfs.NewFileInfo(virtualPath, true, 0, time.Now(), false), nil
  548. }
  549. if checkFilePatterns {
  550. ok, policy := c.User.IsFileAllowed(virtualPath)
  551. if !ok && policy == sdk.DenyPolicyHide {
  552. return nil, c.GetNotExistError()
  553. }
  554. }
  555. var info os.FileInfo
  556. fs, fsPath, err := c.GetFsAndResolvedPath(virtualPath)
  557. if err != nil {
  558. return info, err
  559. }
  560. if mode == 1 {
  561. info, err = fs.Lstat(c.getRealFsPath(fsPath))
  562. } else {
  563. info, err = fs.Stat(c.getRealFsPath(fsPath))
  564. }
  565. if err != nil {
  566. c.Log(logger.LevelWarn, "stat error for path %#v: %+v", virtualPath, err)
  567. return info, c.GetFsError(fs, err)
  568. }
  569. if vfs.IsCryptOsFs(fs) {
  570. info = fs.(*vfs.CryptFs).ConvertFileInfo(info)
  571. }
  572. return info, nil
  573. }
  574. func (c *BaseConnection) createDirIfMissing(name string) error {
  575. _, err := c.DoStat(name, 0, false)
  576. if c.IsNotExistError(err) {
  577. return c.CreateDir(name, false)
  578. }
  579. return err
  580. }
  581. func (c *BaseConnection) ignoreSetStat(fs vfs.Fs) bool {
  582. if Config.SetstatMode == 1 {
  583. return true
  584. }
  585. if Config.SetstatMode == 2 && !vfs.IsLocalOrSFTPFs(fs) && !vfs.IsCryptOsFs(fs) {
  586. return true
  587. }
  588. return false
  589. }
  590. func (c *BaseConnection) handleChmod(fs vfs.Fs, fsPath, pathForPerms string, attributes *StatAttributes) error {
  591. if !c.User.HasPerm(dataprovider.PermChmod, pathForPerms) {
  592. return c.GetPermissionDeniedError()
  593. }
  594. if c.ignoreSetStat(fs) {
  595. return nil
  596. }
  597. if err := fs.Chmod(c.getRealFsPath(fsPath), attributes.Mode); err != nil {
  598. c.Log(logger.LevelError, "failed to chmod path %#v, mode: %v, err: %+v", fsPath, attributes.Mode.String(), err)
  599. return c.GetFsError(fs, err)
  600. }
  601. logger.CommandLog(chmodLogSender, fsPath, "", c.User.Username, attributes.Mode.String(), c.ID, c.protocol,
  602. -1, -1, "", "", "", -1, c.localAddr, c.remoteAddr)
  603. return nil
  604. }
  605. func (c *BaseConnection) handleChown(fs vfs.Fs, fsPath, pathForPerms string, attributes *StatAttributes) error {
  606. if !c.User.HasPerm(dataprovider.PermChown, pathForPerms) {
  607. return c.GetPermissionDeniedError()
  608. }
  609. if c.ignoreSetStat(fs) {
  610. return nil
  611. }
  612. if err := fs.Chown(c.getRealFsPath(fsPath), attributes.UID, attributes.GID); err != nil {
  613. c.Log(logger.LevelError, "failed to chown path %#v, uid: %v, gid: %v, err: %+v", fsPath, attributes.UID,
  614. attributes.GID, err)
  615. return c.GetFsError(fs, err)
  616. }
  617. logger.CommandLog(chownLogSender, fsPath, "", c.User.Username, "", c.ID, c.protocol, attributes.UID, attributes.GID,
  618. "", "", "", -1, c.localAddr, c.remoteAddr)
  619. return nil
  620. }
  621. func (c *BaseConnection) handleChtimes(fs vfs.Fs, fsPath, pathForPerms string, attributes *StatAttributes) error {
  622. if !c.User.HasPerm(dataprovider.PermChtimes, pathForPerms) {
  623. return c.GetPermissionDeniedError()
  624. }
  625. if Config.SetstatMode == 1 {
  626. return nil
  627. }
  628. isUploading := c.setTimes(fsPath, attributes.Atime, attributes.Mtime)
  629. if err := fs.Chtimes(c.getRealFsPath(fsPath), attributes.Atime, attributes.Mtime, isUploading); err != nil {
  630. c.setTimes(fsPath, time.Time{}, time.Time{})
  631. if errors.Is(err, vfs.ErrVfsUnsupported) && Config.SetstatMode == 2 {
  632. return nil
  633. }
  634. c.Log(logger.LevelError, "failed to chtimes for path %#v, access time: %v, modification time: %v, err: %+v",
  635. fsPath, attributes.Atime, attributes.Mtime, err)
  636. return c.GetFsError(fs, err)
  637. }
  638. accessTimeString := attributes.Atime.Format(chtimesFormat)
  639. modificationTimeString := attributes.Mtime.Format(chtimesFormat)
  640. logger.CommandLog(chtimesLogSender, fsPath, "", c.User.Username, "", c.ID, c.protocol, -1, -1,
  641. accessTimeString, modificationTimeString, "", -1, c.localAddr, c.remoteAddr)
  642. return nil
  643. }
  644. // SetStat set StatAttributes for the specified fsPath
  645. func (c *BaseConnection) SetStat(virtualPath string, attributes *StatAttributes) error {
  646. if ok, policy := c.User.IsFileAllowed(virtualPath); !ok {
  647. return c.GetErrorForDeniedFile(policy)
  648. }
  649. fs, fsPath, err := c.GetFsAndResolvedPath(virtualPath)
  650. if err != nil {
  651. return err
  652. }
  653. pathForPerms := c.getPathForSetStatPerms(fs, fsPath, virtualPath)
  654. if attributes.Flags&StatAttrTimes != 0 {
  655. if err = c.handleChtimes(fs, fsPath, pathForPerms, attributes); err != nil {
  656. return err
  657. }
  658. }
  659. if attributes.Flags&StatAttrPerms != 0 {
  660. if err = c.handleChmod(fs, fsPath, pathForPerms, attributes); err != nil {
  661. return err
  662. }
  663. }
  664. if attributes.Flags&StatAttrUIDGID != 0 {
  665. if err = c.handleChown(fs, fsPath, pathForPerms, attributes); err != nil {
  666. return err
  667. }
  668. }
  669. if attributes.Flags&StatAttrSize != 0 {
  670. if !c.User.HasPerm(dataprovider.PermOverwrite, pathForPerms) {
  671. return c.GetPermissionDeniedError()
  672. }
  673. if err = c.truncateFile(fs, fsPath, virtualPath, attributes.Size); err != nil {
  674. c.Log(logger.LevelError, "failed to truncate path %#v, size: %v, err: %+v", fsPath, attributes.Size, err)
  675. return c.GetFsError(fs, err)
  676. }
  677. logger.CommandLog(truncateLogSender, fsPath, "", c.User.Username, "", c.ID, c.protocol, -1, -1, "", "",
  678. "", attributes.Size, c.localAddr, c.remoteAddr)
  679. }
  680. return nil
  681. }
  682. func (c *BaseConnection) truncateFile(fs vfs.Fs, fsPath, virtualPath string, size int64) error {
  683. // check first if we have an open transfer for the given path and try to truncate the file already opened
  684. // if we found no transfer we truncate by path.
  685. var initialSize int64
  686. var err error
  687. initialSize, err = c.truncateOpenHandle(fsPath, size)
  688. if err == errNoTransfer {
  689. c.Log(logger.LevelDebug, "file path %#v not found in active transfers, execute trucate by path", fsPath)
  690. var info os.FileInfo
  691. info, err = fs.Stat(fsPath)
  692. if err != nil {
  693. return err
  694. }
  695. initialSize = info.Size()
  696. err = fs.Truncate(fsPath, size)
  697. }
  698. if err == nil && vfs.HasTruncateSupport(fs) {
  699. sizeDiff := initialSize - size
  700. vfolder, err := c.User.GetVirtualFolderForPath(path.Dir(virtualPath))
  701. if err == nil {
  702. dataprovider.UpdateVirtualFolderQuota(&vfolder.BaseVirtualFolder, 0, -sizeDiff, false) //nolint:errcheck
  703. if vfolder.IsIncludedInUserQuota() {
  704. dataprovider.UpdateUserQuota(&c.User, 0, -sizeDiff, false) //nolint:errcheck
  705. }
  706. } else {
  707. dataprovider.UpdateUserQuota(&c.User, 0, -sizeDiff, false) //nolint:errcheck
  708. }
  709. }
  710. return err
  711. }
  712. func (c *BaseConnection) checkRecursiveRenameDirPermissions(fsSrc, fsDst vfs.Fs, sourcePath, targetPath,
  713. virtualSourcePath, virtualTargetPath string, fi os.FileInfo,
  714. ) error {
  715. if !c.User.HasPermissionsInside(virtualSourcePath) &&
  716. !c.User.HasPermissionsInside(virtualTargetPath) {
  717. if !c.isRenamePermitted(fsSrc, fsDst, sourcePath, targetPath, virtualSourcePath, virtualTargetPath, fi) {
  718. c.Log(logger.LevelInfo, "rename %#v -> %#v is not allowed, virtual destination path: %#v",
  719. sourcePath, targetPath, virtualTargetPath)
  720. return c.GetPermissionDeniedError()
  721. }
  722. // if all rename permissions are granted we have finished, otherwise we have to walk
  723. // because we could have the rename dir permission but not the rename file and the dir to
  724. // rename could contain files
  725. if c.User.HasPermsRenameAll(path.Dir(virtualSourcePath)) && c.User.HasPermsRenameAll(path.Dir(virtualTargetPath)) {
  726. return nil
  727. }
  728. }
  729. return fsSrc.Walk(sourcePath, func(walkedPath string, info os.FileInfo, err error) error {
  730. if err != nil {
  731. return c.GetFsError(fsSrc, err)
  732. }
  733. dstPath := strings.Replace(walkedPath, sourcePath, targetPath, 1)
  734. virtualSrcPath := fsSrc.GetRelativePath(walkedPath)
  735. virtualDstPath := fsDst.GetRelativePath(dstPath)
  736. if !c.isRenamePermitted(fsSrc, fsDst, walkedPath, dstPath, virtualSrcPath, virtualDstPath, info) {
  737. c.Log(logger.LevelInfo, "rename %#v -> %#v is not allowed, virtual destination path: %#v",
  738. walkedPath, dstPath, virtualDstPath)
  739. return c.GetPermissionDeniedError()
  740. }
  741. return nil
  742. })
  743. }
  744. func (c *BaseConnection) hasRenamePerms(virtualSourcePath, virtualTargetPath string, fi os.FileInfo) bool {
  745. if c.User.HasPermsRenameAll(path.Dir(virtualSourcePath)) &&
  746. c.User.HasPermsRenameAll(path.Dir(virtualTargetPath)) {
  747. return true
  748. }
  749. if fi == nil {
  750. // we don't know if this is a file or a directory and we don't have all the rename perms, return false
  751. return false
  752. }
  753. if fi.IsDir() {
  754. perms := []string{
  755. dataprovider.PermRenameDirs,
  756. dataprovider.PermRename,
  757. }
  758. return c.User.HasAnyPerm(perms, path.Dir(virtualSourcePath)) &&
  759. c.User.HasAnyPerm(perms, path.Dir(virtualTargetPath))
  760. }
  761. // file or symlink
  762. perms := []string{
  763. dataprovider.PermRenameFiles,
  764. dataprovider.PermRename,
  765. }
  766. return c.User.HasAnyPerm(perms, path.Dir(virtualSourcePath)) &&
  767. c.User.HasAnyPerm(perms, path.Dir(virtualTargetPath))
  768. }
  769. func (c *BaseConnection) isRenamePermitted(fsSrc, fsDst vfs.Fs, fsSourcePath, fsTargetPath, virtualSourcePath,
  770. virtualTargetPath string, fi os.FileInfo,
  771. ) bool {
  772. if !c.isLocalOrSameFolderRename(virtualSourcePath, virtualTargetPath) {
  773. c.Log(logger.LevelInfo, "rename %#v->%#v is not allowed: the paths must be local or on the same virtual folder",
  774. virtualSourcePath, virtualTargetPath)
  775. return false
  776. }
  777. if c.User.IsMappedPath(fsSourcePath) && vfs.IsLocalOrCryptoFs(fsSrc) {
  778. c.Log(logger.LevelWarn, "renaming a directory mapped as virtual folder is not allowed: %#v", fsSourcePath)
  779. return false
  780. }
  781. if c.User.IsMappedPath(fsTargetPath) && vfs.IsLocalOrCryptoFs(fsDst) {
  782. c.Log(logger.LevelWarn, "renaming to a directory mapped as virtual folder is not allowed: %#v", fsTargetPath)
  783. return false
  784. }
  785. if fsSrc.GetRelativePath(fsSourcePath) == "/" {
  786. c.Log(logger.LevelWarn, "renaming root dir is not allowed")
  787. return false
  788. }
  789. if c.User.IsVirtualFolder(virtualSourcePath) || c.User.IsVirtualFolder(virtualTargetPath) {
  790. c.Log(logger.LevelWarn, "renaming a virtual folder is not allowed")
  791. return false
  792. }
  793. isSrcAllowed, _ := c.User.IsFileAllowed(virtualSourcePath)
  794. isDstAllowed, _ := c.User.IsFileAllowed(virtualTargetPath)
  795. if !isSrcAllowed || !isDstAllowed {
  796. c.Log(logger.LevelDebug, "renaming source: %#v to target: %#v not allowed", virtualSourcePath,
  797. virtualTargetPath)
  798. return false
  799. }
  800. return c.hasRenamePerms(virtualSourcePath, virtualTargetPath, fi)
  801. }
  802. func (c *BaseConnection) hasSpaceForRename(fs vfs.Fs, virtualSourcePath, virtualTargetPath string, initialSize int64,
  803. fsSourcePath string) bool {
  804. if dataprovider.GetQuotaTracking() == 0 {
  805. return true
  806. }
  807. sourceFolder, errSrc := c.User.GetVirtualFolderForPath(path.Dir(virtualSourcePath))
  808. dstFolder, errDst := c.User.GetVirtualFolderForPath(path.Dir(virtualTargetPath))
  809. if errSrc != nil && errDst != nil {
  810. // rename inside the user home dir
  811. return true
  812. }
  813. if errSrc == nil && errDst == nil {
  814. // rename between virtual folders
  815. if sourceFolder.Name == dstFolder.Name {
  816. // rename inside the same virtual folder
  817. return true
  818. }
  819. }
  820. if errSrc != nil && dstFolder.IsIncludedInUserQuota() {
  821. // rename between user root dir and a virtual folder included in user quota
  822. return true
  823. }
  824. quotaResult, _ := c.HasSpace(true, false, virtualTargetPath)
  825. return c.hasSpaceForCrossRename(fs, quotaResult, initialSize, fsSourcePath)
  826. }
  827. // hasSpaceForCrossRename checks the quota after a rename between different folders
  828. func (c *BaseConnection) hasSpaceForCrossRename(fs vfs.Fs, quotaResult vfs.QuotaCheckResult, initialSize int64, sourcePath string) bool {
  829. if !quotaResult.HasSpace && initialSize == -1 {
  830. // we are over quota and this is not a file replace
  831. return false
  832. }
  833. fi, err := fs.Lstat(sourcePath)
  834. if err != nil {
  835. c.Log(logger.LevelError, "cross rename denied, stat error for path %#v: %v", sourcePath, err)
  836. return false
  837. }
  838. var sizeDiff int64
  839. var filesDiff int
  840. if fi.Mode().IsRegular() {
  841. sizeDiff = fi.Size()
  842. filesDiff = 1
  843. if initialSize != -1 {
  844. sizeDiff -= initialSize
  845. filesDiff = 0
  846. }
  847. } else if fi.IsDir() {
  848. filesDiff, sizeDiff, err = fs.GetDirSize(sourcePath)
  849. if err != nil {
  850. c.Log(logger.LevelError, "cross rename denied, error getting size for directory %#v: %v", sourcePath, err)
  851. return false
  852. }
  853. }
  854. if !quotaResult.HasSpace && initialSize != -1 {
  855. // we are over quota but we are overwriting an existing file so we check if the quota size after the rename is ok
  856. if quotaResult.QuotaSize == 0 {
  857. return true
  858. }
  859. c.Log(logger.LevelDebug, "cross rename overwrite, source %#v, used size %v, size to add %v",
  860. sourcePath, quotaResult.UsedSize, sizeDiff)
  861. quotaResult.UsedSize += sizeDiff
  862. return quotaResult.GetRemainingSize() >= 0
  863. }
  864. if quotaResult.QuotaFiles > 0 {
  865. remainingFiles := quotaResult.GetRemainingFiles()
  866. c.Log(logger.LevelDebug, "cross rename, source %#v remaining file %v to add %v", sourcePath,
  867. remainingFiles, filesDiff)
  868. if remainingFiles < filesDiff {
  869. return false
  870. }
  871. }
  872. if quotaResult.QuotaSize > 0 {
  873. remainingSize := quotaResult.GetRemainingSize()
  874. c.Log(logger.LevelDebug, "cross rename, source %#v remaining size %v to add %v", sourcePath,
  875. remainingSize, sizeDiff)
  876. if remainingSize < sizeDiff {
  877. return false
  878. }
  879. }
  880. return true
  881. }
  882. // GetMaxWriteSize returns the allowed size for an upload or an error
  883. // if no enough size is available for a resume/append
  884. func (c *BaseConnection) GetMaxWriteSize(quotaResult vfs.QuotaCheckResult, isResume bool, fileSize int64,
  885. isUploadResumeSupported bool,
  886. ) (int64, error) {
  887. maxWriteSize := quotaResult.GetRemainingSize()
  888. if isResume {
  889. if !isUploadResumeSupported {
  890. return 0, c.GetOpUnsupportedError()
  891. }
  892. if c.User.Filters.MaxUploadFileSize > 0 && c.User.Filters.MaxUploadFileSize <= fileSize {
  893. return 0, c.GetQuotaExceededError()
  894. }
  895. if c.User.Filters.MaxUploadFileSize > 0 {
  896. maxUploadSize := c.User.Filters.MaxUploadFileSize - fileSize
  897. if maxUploadSize < maxWriteSize || maxWriteSize == 0 {
  898. maxWriteSize = maxUploadSize
  899. }
  900. }
  901. } else {
  902. if maxWriteSize > 0 {
  903. maxWriteSize += fileSize
  904. }
  905. if c.User.Filters.MaxUploadFileSize > 0 && (c.User.Filters.MaxUploadFileSize < maxWriteSize || maxWriteSize == 0) {
  906. maxWriteSize = c.User.Filters.MaxUploadFileSize
  907. }
  908. }
  909. return maxWriteSize, nil
  910. }
  911. // GetTransferQuota returns the data transfers quota
  912. func (c *BaseConnection) GetTransferQuota() dataprovider.TransferQuota {
  913. result, _, _ := c.checkUserQuota()
  914. return result
  915. }
  916. func (c *BaseConnection) checkUserQuota() (dataprovider.TransferQuota, int, int64) {
  917. clientIP := c.GetRemoteIP()
  918. ul, dl, total := c.User.GetDataTransferLimits(clientIP)
  919. result := dataprovider.TransferQuota{
  920. ULSize: ul,
  921. DLSize: dl,
  922. TotalSize: total,
  923. AllowedULSize: 0,
  924. AllowedDLSize: 0,
  925. AllowedTotalSize: 0,
  926. }
  927. if !c.User.HasTransferQuotaRestrictions() {
  928. return result, -1, -1
  929. }
  930. usedFiles, usedSize, usedULSize, usedDLSize, err := dataprovider.GetUsedQuota(c.User.Username)
  931. if err != nil {
  932. c.Log(logger.LevelError, "error getting used quota for %#v: %v", c.User.Username, err)
  933. result.AllowedTotalSize = -1
  934. return result, -1, -1
  935. }
  936. if result.TotalSize > 0 {
  937. result.AllowedTotalSize = result.TotalSize - (usedULSize + usedDLSize)
  938. }
  939. if result.ULSize > 0 {
  940. result.AllowedULSize = result.ULSize - usedULSize
  941. }
  942. if result.DLSize > 0 {
  943. result.AllowedDLSize = result.DLSize - usedDLSize
  944. }
  945. return result, usedFiles, usedSize
  946. }
  947. // HasSpace checks user's quota usage
  948. func (c *BaseConnection) HasSpace(checkFiles, getUsage bool, requestPath string) (vfs.QuotaCheckResult,
  949. dataprovider.TransferQuota,
  950. ) {
  951. result := vfs.QuotaCheckResult{
  952. HasSpace: true,
  953. AllowedSize: 0,
  954. AllowedFiles: 0,
  955. UsedSize: 0,
  956. UsedFiles: 0,
  957. QuotaSize: 0,
  958. QuotaFiles: 0,
  959. }
  960. if dataprovider.GetQuotaTracking() == 0 {
  961. return result, dataprovider.TransferQuota{}
  962. }
  963. transferQuota, usedFiles, usedSize := c.checkUserQuota()
  964. var err error
  965. var vfolder vfs.VirtualFolder
  966. vfolder, err = c.User.GetVirtualFolderForPath(path.Dir(requestPath))
  967. if err == nil && !vfolder.IsIncludedInUserQuota() {
  968. if vfolder.HasNoQuotaRestrictions(checkFiles) && !getUsage {
  969. return result, transferQuota
  970. }
  971. result.QuotaSize = vfolder.QuotaSize
  972. result.QuotaFiles = vfolder.QuotaFiles
  973. result.UsedFiles, result.UsedSize, err = dataprovider.GetUsedVirtualFolderQuota(vfolder.Name)
  974. } else {
  975. if c.User.HasNoQuotaRestrictions(checkFiles) && !getUsage {
  976. return result, transferQuota
  977. }
  978. result.QuotaSize = c.User.QuotaSize
  979. result.QuotaFiles = c.User.QuotaFiles
  980. if usedSize == -1 {
  981. result.UsedFiles, result.UsedSize, _, _, err = dataprovider.GetUsedQuota(c.User.Username)
  982. } else {
  983. err = nil
  984. result.UsedFiles = usedFiles
  985. result.UsedSize = usedSize
  986. }
  987. }
  988. if err != nil {
  989. c.Log(logger.LevelError, "error getting used quota for %#v request path %#v: %v", c.User.Username, requestPath, err)
  990. result.HasSpace = false
  991. return result, transferQuota
  992. }
  993. result.AllowedFiles = result.QuotaFiles - result.UsedFiles
  994. result.AllowedSize = result.QuotaSize - result.UsedSize
  995. if (checkFiles && result.QuotaFiles > 0 && result.UsedFiles >= result.QuotaFiles) ||
  996. (result.QuotaSize > 0 && result.UsedSize >= result.QuotaSize) {
  997. c.Log(logger.LevelDebug, "quota exceed for user %#v, request path %#v, num files: %v/%v, size: %v/%v check files: %v",
  998. c.User.Username, requestPath, result.UsedFiles, result.QuotaFiles, result.UsedSize, result.QuotaSize, checkFiles)
  999. result.HasSpace = false
  1000. return result, transferQuota
  1001. }
  1002. return result, transferQuota
  1003. }
  1004. // returns true if this is a rename on the same fs or local virtual folders
  1005. func (c *BaseConnection) isLocalOrSameFolderRename(virtualSourcePath, virtualTargetPath string) bool {
  1006. sourceFolder, errSrc := c.User.GetVirtualFolderForPath(virtualSourcePath)
  1007. dstFolder, errDst := c.User.GetVirtualFolderForPath(virtualTargetPath)
  1008. if errSrc != nil && errDst != nil {
  1009. return true
  1010. }
  1011. if errSrc == nil && errDst == nil {
  1012. if sourceFolder.Name == dstFolder.Name {
  1013. return true
  1014. }
  1015. // we have different folders, only local fs is supported
  1016. if sourceFolder.FsConfig.Provider == sdk.LocalFilesystemProvider &&
  1017. dstFolder.FsConfig.Provider == sdk.LocalFilesystemProvider {
  1018. return true
  1019. }
  1020. return false
  1021. }
  1022. if c.User.FsConfig.Provider != sdk.LocalFilesystemProvider {
  1023. return false
  1024. }
  1025. if errSrc == nil {
  1026. if sourceFolder.FsConfig.Provider == sdk.LocalFilesystemProvider {
  1027. return true
  1028. }
  1029. }
  1030. if errDst == nil {
  1031. if dstFolder.FsConfig.Provider == sdk.LocalFilesystemProvider {
  1032. return true
  1033. }
  1034. }
  1035. return false
  1036. }
  1037. func (c *BaseConnection) isCrossFoldersRequest(virtualSourcePath, virtualTargetPath string) bool {
  1038. sourceFolder, errSrc := c.User.GetVirtualFolderForPath(virtualSourcePath)
  1039. dstFolder, errDst := c.User.GetVirtualFolderForPath(virtualTargetPath)
  1040. if errSrc != nil && errDst != nil {
  1041. return false
  1042. }
  1043. if errSrc == nil && errDst == nil {
  1044. return sourceFolder.Name != dstFolder.Name
  1045. }
  1046. return true
  1047. }
  1048. func (c *BaseConnection) updateQuotaMoveBetweenVFolders(sourceFolder, dstFolder *vfs.VirtualFolder, initialSize,
  1049. filesSize int64, numFiles int) {
  1050. if sourceFolder.Name == dstFolder.Name {
  1051. // both files are inside the same virtual folder
  1052. if initialSize != -1 {
  1053. dataprovider.UpdateVirtualFolderQuota(&dstFolder.BaseVirtualFolder, -numFiles, -initialSize, false) //nolint:errcheck
  1054. if dstFolder.IsIncludedInUserQuota() {
  1055. dataprovider.UpdateUserQuota(&c.User, -numFiles, -initialSize, false) //nolint:errcheck
  1056. }
  1057. }
  1058. return
  1059. }
  1060. // files are inside different virtual folders
  1061. dataprovider.UpdateVirtualFolderQuota(&sourceFolder.BaseVirtualFolder, -numFiles, -filesSize, false) //nolint:errcheck
  1062. if sourceFolder.IsIncludedInUserQuota() {
  1063. dataprovider.UpdateUserQuota(&c.User, -numFiles, -filesSize, false) //nolint:errcheck
  1064. }
  1065. if initialSize == -1 {
  1066. dataprovider.UpdateVirtualFolderQuota(&dstFolder.BaseVirtualFolder, numFiles, filesSize, false) //nolint:errcheck
  1067. if dstFolder.IsIncludedInUserQuota() {
  1068. dataprovider.UpdateUserQuota(&c.User, numFiles, filesSize, false) //nolint:errcheck
  1069. }
  1070. } else {
  1071. // we cannot have a directory here, initialSize != -1 only for files
  1072. dataprovider.UpdateVirtualFolderQuota(&dstFolder.BaseVirtualFolder, 0, filesSize-initialSize, false) //nolint:errcheck
  1073. if dstFolder.IsIncludedInUserQuota() {
  1074. dataprovider.UpdateUserQuota(&c.User, 0, filesSize-initialSize, false) //nolint:errcheck
  1075. }
  1076. }
  1077. }
  1078. func (c *BaseConnection) updateQuotaMoveFromVFolder(sourceFolder *vfs.VirtualFolder, initialSize, filesSize int64, numFiles int) {
  1079. // move between a virtual folder and the user home dir
  1080. dataprovider.UpdateVirtualFolderQuota(&sourceFolder.BaseVirtualFolder, -numFiles, -filesSize, false) //nolint:errcheck
  1081. if sourceFolder.IsIncludedInUserQuota() {
  1082. dataprovider.UpdateUserQuota(&c.User, -numFiles, -filesSize, false) //nolint:errcheck
  1083. }
  1084. if initialSize == -1 {
  1085. dataprovider.UpdateUserQuota(&c.User, numFiles, filesSize, false) //nolint:errcheck
  1086. } else {
  1087. // we cannot have a directory here, initialSize != -1 only for files
  1088. dataprovider.UpdateUserQuota(&c.User, 0, filesSize-initialSize, false) //nolint:errcheck
  1089. }
  1090. }
  1091. func (c *BaseConnection) updateQuotaMoveToVFolder(dstFolder *vfs.VirtualFolder, initialSize, filesSize int64, numFiles int) {
  1092. // move between the user home dir and a virtual folder
  1093. dataprovider.UpdateUserQuota(&c.User, -numFiles, -filesSize, false) //nolint:errcheck
  1094. if initialSize == -1 {
  1095. dataprovider.UpdateVirtualFolderQuota(&dstFolder.BaseVirtualFolder, numFiles, filesSize, false) //nolint:errcheck
  1096. if dstFolder.IsIncludedInUserQuota() {
  1097. dataprovider.UpdateUserQuota(&c.User, numFiles, filesSize, false) //nolint:errcheck
  1098. }
  1099. } else {
  1100. // we cannot have a directory here, initialSize != -1 only for files
  1101. dataprovider.UpdateVirtualFolderQuota(&dstFolder.BaseVirtualFolder, 0, filesSize-initialSize, false) //nolint:errcheck
  1102. if dstFolder.IsIncludedInUserQuota() {
  1103. dataprovider.UpdateUserQuota(&c.User, 0, filesSize-initialSize, false) //nolint:errcheck
  1104. }
  1105. }
  1106. }
  1107. func (c *BaseConnection) updateQuotaAfterRename(fs vfs.Fs, virtualSourcePath, virtualTargetPath, targetPath string, initialSize int64) error {
  1108. if dataprovider.GetQuotaTracking() == 0 {
  1109. return nil
  1110. }
  1111. // we don't allow to overwrite an existing directory so targetPath can be:
  1112. // - a new file, a symlink is as a new file here
  1113. // - a file overwriting an existing one
  1114. // - a new directory
  1115. // initialSize != -1 only when overwriting files
  1116. sourceFolder, errSrc := c.User.GetVirtualFolderForPath(path.Dir(virtualSourcePath))
  1117. dstFolder, errDst := c.User.GetVirtualFolderForPath(path.Dir(virtualTargetPath))
  1118. if errSrc != nil && errDst != nil {
  1119. // both files are contained inside the user home dir
  1120. if initialSize != -1 {
  1121. // we cannot have a directory here, we are overwriting an existing file
  1122. // we need to subtract the size of the overwritten file from the user quota
  1123. dataprovider.UpdateUserQuota(&c.User, -1, -initialSize, false) //nolint:errcheck
  1124. }
  1125. return nil
  1126. }
  1127. filesSize := int64(0)
  1128. numFiles := 1
  1129. if fi, err := fs.Stat(targetPath); err == nil {
  1130. if fi.Mode().IsDir() {
  1131. numFiles, filesSize, err = fs.GetDirSize(targetPath)
  1132. if err != nil {
  1133. c.Log(logger.LevelError, "failed to update quota after rename, error scanning moved folder %#v: %v",
  1134. targetPath, err)
  1135. return err
  1136. }
  1137. } else {
  1138. filesSize = fi.Size()
  1139. }
  1140. } else {
  1141. c.Log(logger.LevelError, "failed to update quota after rename, file %#v stat error: %+v", targetPath, err)
  1142. return err
  1143. }
  1144. if errSrc == nil && errDst == nil {
  1145. c.updateQuotaMoveBetweenVFolders(&sourceFolder, &dstFolder, initialSize, filesSize, numFiles)
  1146. }
  1147. if errSrc == nil && errDst != nil {
  1148. c.updateQuotaMoveFromVFolder(&sourceFolder, initialSize, filesSize, numFiles)
  1149. }
  1150. if errSrc != nil && errDst == nil {
  1151. c.updateQuotaMoveToVFolder(&dstFolder, initialSize, filesSize, numFiles)
  1152. }
  1153. return nil
  1154. }
  1155. // IsNotExistError returns true if the specified fs error is not exist for the connection protocol
  1156. func (c *BaseConnection) IsNotExistError(err error) bool {
  1157. switch c.protocol {
  1158. case ProtocolSFTP:
  1159. return errors.Is(err, sftp.ErrSSHFxNoSuchFile)
  1160. case ProtocolWebDAV, ProtocolFTP, ProtocolHTTP, ProtocolOIDC, ProtocolHTTPShare, ProtocolDataRetention:
  1161. return errors.Is(err, os.ErrNotExist)
  1162. default:
  1163. return errors.Is(err, ErrNotExist)
  1164. }
  1165. }
  1166. // GetErrorForDeniedFile return permission denied or not exist error based on the specified policy
  1167. func (c *BaseConnection) GetErrorForDeniedFile(policy int) error {
  1168. switch policy {
  1169. case sdk.DenyPolicyHide:
  1170. return c.GetNotExistError()
  1171. default:
  1172. return c.GetPermissionDeniedError()
  1173. }
  1174. }
  1175. // GetPermissionDeniedError returns an appropriate permission denied error for the connection protocol
  1176. func (c *BaseConnection) GetPermissionDeniedError() error {
  1177. switch c.protocol {
  1178. case ProtocolSFTP:
  1179. return sftp.ErrSSHFxPermissionDenied
  1180. case ProtocolWebDAV, ProtocolFTP, ProtocolHTTP, ProtocolOIDC, ProtocolHTTPShare, ProtocolDataRetention:
  1181. return os.ErrPermission
  1182. default:
  1183. return ErrPermissionDenied
  1184. }
  1185. }
  1186. // GetNotExistError returns an appropriate not exist error for the connection protocol
  1187. func (c *BaseConnection) GetNotExistError() error {
  1188. switch c.protocol {
  1189. case ProtocolSFTP:
  1190. return sftp.ErrSSHFxNoSuchFile
  1191. case ProtocolWebDAV, ProtocolFTP, ProtocolHTTP, ProtocolOIDC, ProtocolHTTPShare, ProtocolDataRetention:
  1192. return os.ErrNotExist
  1193. default:
  1194. return ErrNotExist
  1195. }
  1196. }
  1197. // GetOpUnsupportedError returns an appropriate operation not supported error for the connection protocol
  1198. func (c *BaseConnection) GetOpUnsupportedError() error {
  1199. switch c.protocol {
  1200. case ProtocolSFTP:
  1201. return sftp.ErrSSHFxOpUnsupported
  1202. default:
  1203. return ErrOpUnsupported
  1204. }
  1205. }
  1206. func getQuotaExceededError(protocol string) error {
  1207. switch protocol {
  1208. case ProtocolSFTP:
  1209. return fmt.Errorf("%w: %v", sftp.ErrSSHFxFailure, ErrQuotaExceeded.Error())
  1210. case ProtocolFTP:
  1211. return ftpserver.ErrStorageExceeded
  1212. default:
  1213. return ErrQuotaExceeded
  1214. }
  1215. }
  1216. func getReadQuotaExceededError(protocol string) error {
  1217. switch protocol {
  1218. case ProtocolSFTP:
  1219. return fmt.Errorf("%w: %v", sftp.ErrSSHFxFailure, ErrReadQuotaExceeded.Error())
  1220. default:
  1221. return ErrReadQuotaExceeded
  1222. }
  1223. }
  1224. // GetQuotaExceededError returns an appropriate storage limit exceeded error for the connection protocol
  1225. func (c *BaseConnection) GetQuotaExceededError() error {
  1226. return getQuotaExceededError(c.protocol)
  1227. }
  1228. // GetReadQuotaExceededError returns an appropriate read quota limit exceeded error for the connection protocol
  1229. func (c *BaseConnection) GetReadQuotaExceededError() error {
  1230. return getReadQuotaExceededError(c.protocol)
  1231. }
  1232. // IsQuotaExceededError returns true if the given error is a quota exceeded error
  1233. func (c *BaseConnection) IsQuotaExceededError(err error) bool {
  1234. switch c.protocol {
  1235. case ProtocolSFTP:
  1236. if err == nil {
  1237. return false
  1238. }
  1239. if errors.Is(err, ErrQuotaExceeded) {
  1240. return true
  1241. }
  1242. return errors.Is(err, sftp.ErrSSHFxFailure) && strings.Contains(err.Error(), ErrQuotaExceeded.Error())
  1243. case ProtocolFTP:
  1244. return errors.Is(err, ftpserver.ErrStorageExceeded) || errors.Is(err, ErrQuotaExceeded)
  1245. default:
  1246. return errors.Is(err, ErrQuotaExceeded)
  1247. }
  1248. }
  1249. // GetGenericError returns an appropriate generic error for the connection protocol
  1250. func (c *BaseConnection) GetGenericError(err error) error {
  1251. switch c.protocol {
  1252. case ProtocolSFTP:
  1253. if err == vfs.ErrStorageSizeUnavailable {
  1254. return fmt.Errorf("%w: %v", sftp.ErrSSHFxOpUnsupported, err.Error())
  1255. }
  1256. if err != nil {
  1257. if e, ok := err.(*os.PathError); ok {
  1258. c.Log(logger.LevelError, "generic path error: %+v", e)
  1259. return fmt.Errorf("%w: %v %v", sftp.ErrSSHFxFailure, e.Op, e.Err.Error())
  1260. }
  1261. c.Log(logger.LevelError, "generic error: %+v", err)
  1262. return fmt.Errorf("%w: %v", sftp.ErrSSHFxFailure, ErrGenericFailure.Error())
  1263. }
  1264. return sftp.ErrSSHFxFailure
  1265. default:
  1266. if err == ErrPermissionDenied || err == ErrNotExist || err == ErrOpUnsupported ||
  1267. err == ErrQuotaExceeded || err == vfs.ErrStorageSizeUnavailable {
  1268. return err
  1269. }
  1270. return ErrGenericFailure
  1271. }
  1272. }
  1273. // GetFsError converts a filesystem error to a protocol error
  1274. func (c *BaseConnection) GetFsError(fs vfs.Fs, err error) error {
  1275. if fs.IsNotExist(err) {
  1276. return c.GetNotExistError()
  1277. } else if fs.IsPermission(err) {
  1278. return c.GetPermissionDeniedError()
  1279. } else if fs.IsNotSupported(err) {
  1280. return c.GetOpUnsupportedError()
  1281. } else if err != nil {
  1282. return c.GetGenericError(err)
  1283. }
  1284. return nil
  1285. }
  1286. // GetFsAndResolvedPath returns the fs and the fs path matching virtualPath
  1287. func (c *BaseConnection) GetFsAndResolvedPath(virtualPath string) (vfs.Fs, string, error) {
  1288. fs, err := c.User.GetFilesystemForPath(virtualPath, c.ID)
  1289. if err != nil {
  1290. if c.protocol == ProtocolWebDAV && strings.Contains(err.Error(), vfs.ErrSFTPLoop.Error()) {
  1291. // if there is an SFTP loop we return a permission error, for WebDAV, so the problematic folder
  1292. // will not be listed
  1293. return nil, "", c.GetPermissionDeniedError()
  1294. }
  1295. return nil, "", err
  1296. }
  1297. fsPath, err := fs.ResolvePath(virtualPath)
  1298. if err != nil {
  1299. return nil, "", c.GetFsError(fs, err)
  1300. }
  1301. return fs, fsPath, nil
  1302. }