connection.go 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914
  1. // Copyright (C) 2019 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. "io"
  19. "os"
  20. "path"
  21. "strings"
  22. "sync"
  23. "sync/atomic"
  24. "time"
  25. ftpserver "github.com/fclairamb/ftpserverlib"
  26. "github.com/pkg/sftp"
  27. "github.com/sftpgo/sdk"
  28. "github.com/drakkan/sftpgo/v2/internal/dataprovider"
  29. "github.com/drakkan/sftpgo/v2/internal/logger"
  30. "github.com/drakkan/sftpgo/v2/internal/util"
  31. "github.com/drakkan/sftpgo/v2/internal/vfs"
  32. )
  33. // BaseConnection defines common fields for a connection using any supported protocol
  34. type BaseConnection struct {
  35. // last activity for this connection.
  36. // Since this field is accessed atomically we put it as first element of the struct to achieve 64 bit alignment
  37. lastActivity atomic.Int64
  38. uploadDone atomic.Bool
  39. downloadDone atomic.Bool
  40. // unique ID for a transfer.
  41. // This field is accessed atomically so we put it at the beginning of the struct to achieve 64 bit alignment
  42. transferID atomic.Int64
  43. // Unique identifier for the connection
  44. ID string
  45. // user associated with this connection if any
  46. User dataprovider.User
  47. // start time for this connection
  48. startTime time.Time
  49. protocol string
  50. remoteAddr string
  51. localAddr string
  52. sync.RWMutex
  53. activeTransfers []ActiveTransfer
  54. }
  55. // NewBaseConnection returns a new BaseConnection
  56. func NewBaseConnection(id, protocol, localAddr, remoteAddr string, user dataprovider.User) *BaseConnection {
  57. connID := id
  58. if util.Contains(supportedProtocols, protocol) {
  59. connID = fmt.Sprintf("%s_%s", protocol, id)
  60. }
  61. user.UploadBandwidth, user.DownloadBandwidth = user.GetBandwidthForIP(util.GetIPFromRemoteAddress(remoteAddr), connID)
  62. c := &BaseConnection{
  63. ID: connID,
  64. User: user,
  65. startTime: time.Now(),
  66. protocol: protocol,
  67. localAddr: localAddr,
  68. remoteAddr: remoteAddr,
  69. }
  70. c.transferID.Store(0)
  71. c.lastActivity.Store(time.Now().UnixNano())
  72. return c
  73. }
  74. // Log outputs a log entry to the configured logger
  75. func (c *BaseConnection) Log(level logger.LogLevel, format string, v ...any) {
  76. logger.Log(level, c.protocol, c.ID, format, v...)
  77. }
  78. // GetTransferID returns an unique transfer ID for this connection
  79. func (c *BaseConnection) GetTransferID() int64 {
  80. return c.transferID.Add(1)
  81. }
  82. // GetID returns the connection ID
  83. func (c *BaseConnection) GetID() string {
  84. return c.ID
  85. }
  86. // GetUsername returns the authenticated username associated with this connection if any
  87. func (c *BaseConnection) GetUsername() string {
  88. return c.User.Username
  89. }
  90. // GetRole returns the role for the user associated with this connection
  91. func (c *BaseConnection) GetRole() string {
  92. return c.User.Role
  93. }
  94. // GetMaxSessions returns the maximum number of concurrent sessions allowed
  95. func (c *BaseConnection) GetMaxSessions() int {
  96. return c.User.MaxSessions
  97. }
  98. // isAccessAllowed returns true if the user's access conditions are met
  99. func (c *BaseConnection) isAccessAllowed() bool {
  100. if err := c.User.CheckLoginConditions(); err != nil {
  101. return false
  102. }
  103. return true
  104. }
  105. // GetProtocol returns the protocol for the connection
  106. func (c *BaseConnection) GetProtocol() string {
  107. return c.protocol
  108. }
  109. // GetRemoteIP returns the remote ip address
  110. func (c *BaseConnection) GetRemoteIP() string {
  111. return util.GetIPFromRemoteAddress(c.remoteAddr)
  112. }
  113. // SetProtocol sets the protocol for this connection
  114. func (c *BaseConnection) SetProtocol(protocol string) {
  115. c.protocol = protocol
  116. if util.Contains(supportedProtocols, c.protocol) {
  117. c.ID = fmt.Sprintf("%v_%v", c.protocol, c.ID)
  118. }
  119. }
  120. // GetConnectionTime returns the initial connection time
  121. func (c *BaseConnection) GetConnectionTime() time.Time {
  122. return c.startTime
  123. }
  124. // UpdateLastActivity updates last activity for this connection
  125. func (c *BaseConnection) UpdateLastActivity() {
  126. c.lastActivity.Store(time.Now().UnixNano())
  127. }
  128. // GetLastActivity returns the last connection activity
  129. func (c *BaseConnection) GetLastActivity() time.Time {
  130. return time.Unix(0, c.lastActivity.Load())
  131. }
  132. // CloseFS closes the underlying fs
  133. func (c *BaseConnection) CloseFS() error {
  134. return c.User.CloseFs()
  135. }
  136. // AddTransfer associates a new transfer to this connection
  137. func (c *BaseConnection) AddTransfer(t ActiveTransfer) {
  138. c.Lock()
  139. defer c.Unlock()
  140. c.activeTransfers = append(c.activeTransfers, t)
  141. c.Log(logger.LevelDebug, "transfer added, id: %v, active transfers: %v", t.GetID(), len(c.activeTransfers))
  142. if t.HasSizeLimit() {
  143. folderName := ""
  144. if t.GetType() == TransferUpload {
  145. vfolder, err := c.User.GetVirtualFolderForPath(path.Dir(t.GetVirtualPath()))
  146. if err == nil {
  147. if !vfolder.IsIncludedInUserQuota() {
  148. folderName = vfolder.Name
  149. }
  150. }
  151. }
  152. go transfersChecker.AddTransfer(dataprovider.ActiveTransfer{
  153. ID: t.GetID(),
  154. Type: t.GetType(),
  155. ConnID: c.ID,
  156. Username: c.GetUsername(),
  157. FolderName: folderName,
  158. IP: c.GetRemoteIP(),
  159. TruncatedSize: t.GetTruncatedSize(),
  160. CreatedAt: util.GetTimeAsMsSinceEpoch(time.Now()),
  161. UpdatedAt: util.GetTimeAsMsSinceEpoch(time.Now()),
  162. })
  163. }
  164. }
  165. // RemoveTransfer removes the specified transfer from the active ones
  166. func (c *BaseConnection) RemoveTransfer(t ActiveTransfer) {
  167. c.Lock()
  168. defer c.Unlock()
  169. if t.HasSizeLimit() {
  170. go transfersChecker.RemoveTransfer(t.GetID(), c.ID)
  171. }
  172. for idx, transfer := range c.activeTransfers {
  173. if transfer.GetID() == t.GetID() {
  174. lastIdx := len(c.activeTransfers) - 1
  175. c.activeTransfers[idx] = c.activeTransfers[lastIdx]
  176. c.activeTransfers[lastIdx] = nil
  177. c.activeTransfers = c.activeTransfers[:lastIdx]
  178. c.Log(logger.LevelDebug, "transfer removed, id: %v active transfers: %v", t.GetID(), len(c.activeTransfers))
  179. return
  180. }
  181. }
  182. c.Log(logger.LevelWarn, "transfer to remove with id %v not found!", t.GetID())
  183. }
  184. // SignalTransferClose makes the transfer fail on the next read/write with the
  185. // specified error
  186. func (c *BaseConnection) SignalTransferClose(transferID int64, err error) {
  187. c.RLock()
  188. defer c.RUnlock()
  189. for _, t := range c.activeTransfers {
  190. if t.GetID() == transferID {
  191. c.Log(logger.LevelInfo, "signal transfer close for transfer id %v", transferID)
  192. t.SignalClose(err)
  193. }
  194. }
  195. }
  196. // GetTransfers returns the active transfers
  197. func (c *BaseConnection) GetTransfers() []ConnectionTransfer {
  198. c.RLock()
  199. defer c.RUnlock()
  200. transfers := make([]ConnectionTransfer, 0, len(c.activeTransfers))
  201. for _, t := range c.activeTransfers {
  202. var operationType string
  203. switch t.GetType() {
  204. case TransferDownload:
  205. operationType = operationDownload
  206. case TransferUpload:
  207. operationType = operationUpload
  208. }
  209. transfers = append(transfers, ConnectionTransfer{
  210. ID: t.GetID(),
  211. OperationType: operationType,
  212. StartTime: util.GetTimeAsMsSinceEpoch(t.GetStartTime()),
  213. Size: t.GetSize(),
  214. VirtualPath: t.GetVirtualPath(),
  215. HasSizeLimit: t.HasSizeLimit(),
  216. ULSize: t.GetUploadedSize(),
  217. DLSize: t.GetDownloadedSize(),
  218. })
  219. }
  220. return transfers
  221. }
  222. // SignalTransfersAbort signals to the active transfers to exit as soon as possible
  223. func (c *BaseConnection) SignalTransfersAbort() error {
  224. c.RLock()
  225. defer c.RUnlock()
  226. if len(c.activeTransfers) == 0 {
  227. return errors.New("no active transfer found")
  228. }
  229. for _, t := range c.activeTransfers {
  230. t.SignalClose(ErrTransferAborted)
  231. }
  232. return nil
  233. }
  234. func (c *BaseConnection) getRealFsPath(fsPath string) string {
  235. c.RLock()
  236. defer c.RUnlock()
  237. for _, t := range c.activeTransfers {
  238. if p := t.GetRealFsPath(fsPath); p != "" {
  239. return p
  240. }
  241. }
  242. return fsPath
  243. }
  244. func (c *BaseConnection) setTimes(fsPath string, atime time.Time, mtime time.Time) bool {
  245. c.RLock()
  246. defer c.RUnlock()
  247. for _, t := range c.activeTransfers {
  248. if t.SetTimes(fsPath, atime, mtime) {
  249. return true
  250. }
  251. }
  252. return false
  253. }
  254. func (c *BaseConnection) truncateOpenHandle(fsPath string, size int64) (int64, error) {
  255. c.RLock()
  256. defer c.RUnlock()
  257. for _, t := range c.activeTransfers {
  258. initialSize, err := t.Truncate(fsPath, size)
  259. if err != errTransferMismatch {
  260. return initialSize, err
  261. }
  262. }
  263. return 0, errNoTransfer
  264. }
  265. // ListDir reads the directory matching virtualPath and returns a list of directory entries
  266. func (c *BaseConnection) ListDir(virtualPath string) (*DirListerAt, error) {
  267. if !c.User.HasPerm(dataprovider.PermListItems, virtualPath) {
  268. return nil, c.GetPermissionDeniedError()
  269. }
  270. fs, fsPath, err := c.GetFsAndResolvedPath(virtualPath)
  271. if err != nil {
  272. return nil, err
  273. }
  274. lister, err := fs.ReadDir(fsPath)
  275. if err != nil {
  276. c.Log(logger.LevelDebug, "error listing directory: %+v", err)
  277. return nil, c.GetFsError(fs, err)
  278. }
  279. return &DirListerAt{
  280. virtualPath: virtualPath,
  281. user: &c.User,
  282. info: c.User.GetVirtualFoldersInfo(virtualPath),
  283. id: c.ID,
  284. protocol: c.protocol,
  285. lister: lister,
  286. }, nil
  287. }
  288. // CheckParentDirs tries to create the specified directory and any missing parent dirs
  289. func (c *BaseConnection) CheckParentDirs(virtualPath string) error {
  290. fs, err := c.User.GetFilesystemForPath(virtualPath, c.GetID())
  291. if err != nil {
  292. return err
  293. }
  294. if fs.HasVirtualFolders() {
  295. return nil
  296. }
  297. if _, err := c.DoStat(virtualPath, 0, false); !c.IsNotExistError(err) {
  298. return err
  299. }
  300. dirs := util.GetDirsForVirtualPath(virtualPath)
  301. for idx := len(dirs) - 1; idx >= 0; idx-- {
  302. fs, err = c.User.GetFilesystemForPath(dirs[idx], c.GetID())
  303. if err != nil {
  304. return err
  305. }
  306. if fs.HasVirtualFolders() {
  307. continue
  308. }
  309. if err = c.createDirIfMissing(dirs[idx]); err != nil {
  310. return fmt.Errorf("unable to check/create missing parent dir %q for virtual path %q: %w",
  311. dirs[idx], virtualPath, err)
  312. }
  313. }
  314. return nil
  315. }
  316. // GetCreateChecks returns the checks for creating new files
  317. func (c *BaseConnection) GetCreateChecks(virtualPath string, isNewFile bool, isResume bool) int {
  318. result := 0
  319. if !isNewFile {
  320. if isResume {
  321. result += vfs.CheckResume
  322. }
  323. return result
  324. }
  325. if !c.User.HasPerm(dataprovider.PermCreateDirs, path.Dir(virtualPath)) {
  326. result += vfs.CheckParentDir
  327. return result
  328. }
  329. return result
  330. }
  331. // CreateDir creates a new directory at the specified fsPath
  332. func (c *BaseConnection) CreateDir(virtualPath string, checkFilePatterns bool) error {
  333. if !c.User.HasPerm(dataprovider.PermCreateDirs, path.Dir(virtualPath)) {
  334. return c.GetPermissionDeniedError()
  335. }
  336. if checkFilePatterns {
  337. if ok, _ := c.User.IsFileAllowed(virtualPath); !ok {
  338. return c.GetPermissionDeniedError()
  339. }
  340. }
  341. if c.User.IsVirtualFolder(virtualPath) {
  342. c.Log(logger.LevelWarn, "mkdir not allowed %q is a virtual folder", virtualPath)
  343. return c.GetPermissionDeniedError()
  344. }
  345. fs, fsPath, err := c.GetFsAndResolvedPath(virtualPath)
  346. if err != nil {
  347. return err
  348. }
  349. startTime := time.Now()
  350. if err := fs.Mkdir(fsPath); err != nil {
  351. c.Log(logger.LevelError, "error creating dir: %q error: %+v", fsPath, err)
  352. return c.GetFsError(fs, err)
  353. }
  354. vfs.SetPathPermissions(fs, fsPath, c.User.GetUID(), c.User.GetGID())
  355. elapsed := time.Since(startTime).Nanoseconds() / 1000000
  356. logger.CommandLog(mkdirLogSender, fsPath, "", c.User.Username, "", c.ID, c.protocol, -1, -1, "", "", "", -1,
  357. c.localAddr, c.remoteAddr, elapsed)
  358. ExecuteActionNotification(c, operationMkdir, fsPath, virtualPath, "", "", "", 0, nil, elapsed, nil) //nolint:errcheck
  359. return nil
  360. }
  361. // IsRemoveFileAllowed returns an error if removing this file is not allowed
  362. func (c *BaseConnection) IsRemoveFileAllowed(virtualPath string) error {
  363. if !c.User.HasAnyPerm([]string{dataprovider.PermDeleteFiles, dataprovider.PermDelete}, path.Dir(virtualPath)) {
  364. return c.GetPermissionDeniedError()
  365. }
  366. if ok, policy := c.User.IsFileAllowed(virtualPath); !ok {
  367. c.Log(logger.LevelDebug, "removing file %q is not allowed", virtualPath)
  368. return c.GetErrorForDeniedFile(policy)
  369. }
  370. return nil
  371. }
  372. // RemoveFile removes a file at the specified fsPath
  373. func (c *BaseConnection) RemoveFile(fs vfs.Fs, fsPath, virtualPath string, info os.FileInfo) error {
  374. if err := c.IsRemoveFileAllowed(virtualPath); err != nil {
  375. return err
  376. }
  377. size := info.Size()
  378. status, err := ExecutePreAction(c, operationPreDelete, fsPath, virtualPath, size, 0)
  379. if err != nil {
  380. c.Log(logger.LevelDebug, "delete for file %q denied by pre action: %v", virtualPath, err)
  381. return c.GetPermissionDeniedError()
  382. }
  383. updateQuota := true
  384. startTime := time.Now()
  385. if err := fs.Remove(fsPath, false); err != nil {
  386. if status > 0 && fs.IsNotExist(err) {
  387. // file removed in the pre-action, if the file was deleted from the EventManager the quota is already updated
  388. c.Log(logger.LevelDebug, "file deleted from the hook, status: %d", status)
  389. updateQuota = (status == 1)
  390. } else {
  391. c.Log(logger.LevelError, "failed to remove file/symlink %q: %+v", fsPath, err)
  392. return c.GetFsError(fs, err)
  393. }
  394. }
  395. elapsed := time.Since(startTime).Nanoseconds() / 1000000
  396. logger.CommandLog(removeLogSender, fsPath, "", c.User.Username, "", c.ID, c.protocol, -1, -1, "", "", "", -1,
  397. c.localAddr, c.remoteAddr, elapsed)
  398. if updateQuota && info.Mode()&os.ModeSymlink == 0 {
  399. vfolder, err := c.User.GetVirtualFolderForPath(path.Dir(virtualPath))
  400. if err == nil {
  401. dataprovider.UpdateVirtualFolderQuota(&vfolder.BaseVirtualFolder, -1, -size, false) //nolint:errcheck
  402. if vfolder.IsIncludedInUserQuota() {
  403. dataprovider.UpdateUserQuota(&c.User, -1, -size, false) //nolint:errcheck
  404. }
  405. } else {
  406. dataprovider.UpdateUserQuota(&c.User, -1, -size, false) //nolint:errcheck
  407. }
  408. }
  409. ExecuteActionNotification(c, operationDelete, fsPath, virtualPath, "", "", "", size, nil, elapsed, nil) //nolint:errcheck
  410. return nil
  411. }
  412. // IsRemoveDirAllowed returns an error if removing this directory is not allowed
  413. func (c *BaseConnection) IsRemoveDirAllowed(fs vfs.Fs, fsPath, virtualPath string) error {
  414. if virtualPath == "/" || fs.GetRelativePath(fsPath) == "/" {
  415. c.Log(logger.LevelWarn, "removing root dir is not allowed")
  416. return c.GetPermissionDeniedError()
  417. }
  418. if c.User.IsVirtualFolder(virtualPath) {
  419. c.Log(logger.LevelWarn, "removing a virtual folder is not allowed: %q", virtualPath)
  420. return fmt.Errorf("removing virtual folders is not allowed: %w", c.GetPermissionDeniedError())
  421. }
  422. if c.User.HasVirtualFoldersInside(virtualPath) {
  423. c.Log(logger.LevelWarn, "removing a directory with a virtual folder inside is not allowed: %q", virtualPath)
  424. return fmt.Errorf("cannot remove directory %q with virtual folders inside: %w", virtualPath, c.GetOpUnsupportedError())
  425. }
  426. if c.User.IsMappedPath(fsPath) {
  427. c.Log(logger.LevelWarn, "removing a directory mapped as virtual folder is not allowed: %q", fsPath)
  428. return fmt.Errorf("removing the directory %q mapped as virtual folder is not allowed: %w",
  429. virtualPath, c.GetPermissionDeniedError())
  430. }
  431. if !c.User.HasAnyPerm([]string{dataprovider.PermDeleteDirs, dataprovider.PermDelete}, path.Dir(virtualPath)) {
  432. return c.GetPermissionDeniedError()
  433. }
  434. if ok, policy := c.User.IsFileAllowed(virtualPath); !ok {
  435. c.Log(logger.LevelDebug, "removing directory %q is not allowed", virtualPath)
  436. return c.GetErrorForDeniedFile(policy)
  437. }
  438. return nil
  439. }
  440. // RemoveDir removes a directory at the specified fsPath
  441. func (c *BaseConnection) RemoveDir(virtualPath string) error {
  442. fs, fsPath, err := c.GetFsAndResolvedPath(virtualPath)
  443. if err != nil {
  444. return err
  445. }
  446. if err := c.IsRemoveDirAllowed(fs, fsPath, virtualPath); err != nil {
  447. return err
  448. }
  449. var fi os.FileInfo
  450. if fi, err = fs.Lstat(fsPath); err != nil {
  451. // see #149
  452. if fs.IsNotExist(err) && fs.HasVirtualFolders() {
  453. return nil
  454. }
  455. c.Log(logger.LevelError, "failed to remove a dir %q: stat error: %+v", fsPath, err)
  456. return c.GetFsError(fs, err)
  457. }
  458. if !fi.IsDir() || fi.Mode()&os.ModeSymlink != 0 {
  459. c.Log(logger.LevelError, "cannot remove %q is not a directory", fsPath)
  460. return c.GetGenericError(nil)
  461. }
  462. startTime := time.Now()
  463. if err := fs.Remove(fsPath, true); err != nil {
  464. c.Log(logger.LevelError, "failed to remove directory %q: %+v", fsPath, err)
  465. return c.GetFsError(fs, err)
  466. }
  467. elapsed := time.Since(startTime).Nanoseconds() / 1000000
  468. logger.CommandLog(rmdirLogSender, fsPath, "", c.User.Username, "", c.ID, c.protocol, -1, -1, "", "", "", -1,
  469. c.localAddr, c.remoteAddr, elapsed)
  470. ExecuteActionNotification(c, operationRmdir, fsPath, virtualPath, "", "", "", 0, nil, elapsed, nil) //nolint:errcheck
  471. return nil
  472. }
  473. func (c *BaseConnection) doRecursiveRemoveDirEntry(virtualPath string, info os.FileInfo, recursion int) error {
  474. fs, fsPath, err := c.GetFsAndResolvedPath(virtualPath)
  475. if err != nil {
  476. return err
  477. }
  478. return c.doRecursiveRemove(fs, fsPath, virtualPath, info, recursion)
  479. }
  480. func (c *BaseConnection) doRecursiveRemove(fs vfs.Fs, fsPath, virtualPath string, info os.FileInfo, recursion int) error {
  481. if info.IsDir() {
  482. if recursion >= util.MaxRecursion {
  483. c.Log(logger.LevelError, "recursive rename failed, recursion too depth: %d", recursion)
  484. return util.ErrRecursionTooDeep
  485. }
  486. recursion++
  487. lister, err := c.ListDir(virtualPath)
  488. if err != nil {
  489. return fmt.Errorf("unable to get lister for dir %q: %w", virtualPath, err)
  490. }
  491. defer lister.Close()
  492. for {
  493. entries, err := lister.Next(vfs.ListerBatchSize)
  494. finished := errors.Is(err, io.EOF)
  495. if err != nil && !finished {
  496. return fmt.Errorf("unable to get content for dir %q: %w", virtualPath, err)
  497. }
  498. for _, fi := range entries {
  499. targetPath := path.Join(virtualPath, fi.Name())
  500. if err := c.doRecursiveRemoveDirEntry(targetPath, fi, recursion); err != nil {
  501. return err
  502. }
  503. }
  504. if finished {
  505. lister.Close()
  506. break
  507. }
  508. }
  509. return c.RemoveDir(virtualPath)
  510. }
  511. return c.RemoveFile(fs, fsPath, virtualPath, info)
  512. }
  513. // RemoveAll removes the specified path and any children it contains
  514. func (c *BaseConnection) RemoveAll(virtualPath string) error {
  515. fs, fsPath, err := c.GetFsAndResolvedPath(virtualPath)
  516. if err != nil {
  517. return err
  518. }
  519. fi, err := fs.Lstat(fsPath)
  520. if err != nil {
  521. c.Log(logger.LevelDebug, "failed to remove path %q: stat error: %+v", fsPath, err)
  522. return c.GetFsError(fs, err)
  523. }
  524. if fi.IsDir() && fi.Mode()&os.ModeSymlink == 0 {
  525. if err := c.IsRemoveDirAllowed(fs, fsPath, virtualPath); err != nil {
  526. return err
  527. }
  528. return c.doRecursiveRemove(fs, fsPath, virtualPath, fi, 0)
  529. }
  530. return c.RemoveFile(fs, fsPath, virtualPath, fi)
  531. }
  532. func (c *BaseConnection) checkCopy(srcInfo, dstInfo os.FileInfo, virtualSource, virtualTarget string) error {
  533. _, fsSourcePath, err := c.GetFsAndResolvedPath(virtualSource)
  534. if err != nil {
  535. return err
  536. }
  537. _, fsTargetPath, err := c.GetFsAndResolvedPath(virtualTarget)
  538. if err != nil {
  539. return err
  540. }
  541. if srcInfo.IsDir() {
  542. if dstInfo != nil && !dstInfo.IsDir() {
  543. return fmt.Errorf("cannot overwrite file %q with dir %q: %w", virtualTarget, virtualSource, c.GetOpUnsupportedError())
  544. }
  545. if util.IsDirOverlapped(virtualSource, virtualTarget, true, "/") {
  546. return fmt.Errorf("nested copy %q => %q is not supported: %w", virtualSource, virtualTarget, c.GetOpUnsupportedError())
  547. }
  548. if util.IsDirOverlapped(fsSourcePath, fsTargetPath, true, c.User.FsConfig.GetPathSeparator()) {
  549. c.Log(logger.LevelWarn, "nested fs copy %q => %q not allowed", fsSourcePath, fsTargetPath)
  550. return fmt.Errorf("nested fs copy is not supported: %w", c.GetOpUnsupportedError())
  551. }
  552. return nil
  553. }
  554. if dstInfo != nil && dstInfo.IsDir() {
  555. return fmt.Errorf("cannot overwrite file %q with dir %q: %w", virtualSource, virtualTarget, c.GetOpUnsupportedError())
  556. }
  557. if fsSourcePath == fsTargetPath {
  558. return fmt.Errorf("the copy source and target cannot be the same: %w", c.GetOpUnsupportedError())
  559. }
  560. return nil
  561. }
  562. func (c *BaseConnection) copyFile(virtualSourcePath, virtualTargetPath string, srcSize int64) error {
  563. if !c.User.HasPerm(dataprovider.PermCopy, virtualSourcePath) || !c.User.HasPerm(dataprovider.PermCopy, virtualTargetPath) {
  564. return c.GetPermissionDeniedError()
  565. }
  566. if ok, _ := c.User.IsFileAllowed(virtualTargetPath); !ok {
  567. return fmt.Errorf("file %q is not allowed: %w", virtualTargetPath, c.GetPermissionDeniedError())
  568. }
  569. if c.IsSameResource(virtualSourcePath, virtualTargetPath) {
  570. fs, fsTargetPath, err := c.GetFsAndResolvedPath(virtualTargetPath)
  571. if err != nil {
  572. return err
  573. }
  574. if copier, ok := fs.(vfs.FsFileCopier); ok {
  575. _, fsSourcePath, err := c.GetFsAndResolvedPath(virtualSourcePath)
  576. if err != nil {
  577. return err
  578. }
  579. startTime := time.Now()
  580. numFiles, sizeDiff, err := copier.CopyFile(fsSourcePath, fsTargetPath, srcSize)
  581. elapsed := time.Since(startTime).Nanoseconds() / 1000000
  582. updateUserQuotaAfterFileWrite(c, virtualTargetPath, numFiles, sizeDiff)
  583. logger.CommandLog(copyLogSender, fsSourcePath, fsTargetPath, c.User.Username, "", c.ID, c.protocol, -1, -1,
  584. "", "", "", srcSize, c.localAddr, c.remoteAddr, elapsed)
  585. ExecuteActionNotification(c, operationCopy, fsSourcePath, virtualSourcePath, fsTargetPath, virtualTargetPath, "", srcSize, err, elapsed, nil) //nolint:errcheck
  586. return err
  587. }
  588. }
  589. reader, rCancelFn, err := getFileReader(c, virtualSourcePath)
  590. if err != nil {
  591. return fmt.Errorf("unable to get reader for path %q: %w", virtualSourcePath, err)
  592. }
  593. defer rCancelFn()
  594. defer reader.Close()
  595. writer, numFiles, truncatedSize, wCancelFn, err := getFileWriter(c, virtualTargetPath, srcSize)
  596. if err != nil {
  597. return fmt.Errorf("unable to get writer for path %q: %w", virtualTargetPath, err)
  598. }
  599. defer wCancelFn()
  600. startTime := time.Now()
  601. _, err = io.Copy(writer, reader)
  602. return closeWriterAndUpdateQuota(writer, c, virtualSourcePath, virtualTargetPath, numFiles, truncatedSize,
  603. err, operationCopy, startTime)
  604. }
  605. func (c *BaseConnection) doRecursiveCopy(virtualSourcePath, virtualTargetPath string, srcInfo os.FileInfo,
  606. createTargetDir bool, recursion int,
  607. ) error {
  608. if srcInfo.IsDir() {
  609. if recursion >= util.MaxRecursion {
  610. c.Log(logger.LevelError, "recursive copy failed, recursion too depth: %d", recursion)
  611. return util.ErrRecursionTooDeep
  612. }
  613. recursion++
  614. if createTargetDir {
  615. if err := c.CreateDir(virtualTargetPath, false); err != nil {
  616. return fmt.Errorf("unable to create directory %q: %w", virtualTargetPath, err)
  617. }
  618. }
  619. lister, err := c.ListDir(virtualSourcePath)
  620. if err != nil {
  621. return fmt.Errorf("unable to get lister for dir %q: %w", virtualSourcePath, err)
  622. }
  623. defer lister.Close()
  624. for {
  625. entries, err := lister.Next(vfs.ListerBatchSize)
  626. finished := errors.Is(err, io.EOF)
  627. if err != nil && !finished {
  628. return fmt.Errorf("unable to get contents for dir %q: %w", virtualSourcePath, err)
  629. }
  630. if err := c.recursiveCopyEntries(virtualSourcePath, virtualTargetPath, entries, recursion); err != nil {
  631. return err
  632. }
  633. if finished {
  634. return nil
  635. }
  636. }
  637. }
  638. if !srcInfo.Mode().IsRegular() {
  639. c.Log(logger.LevelInfo, "skipping copy for non regular file %q", virtualSourcePath)
  640. return nil
  641. }
  642. return c.copyFile(virtualSourcePath, virtualTargetPath, srcInfo.Size())
  643. }
  644. func (c *BaseConnection) recursiveCopyEntries(virtualSourcePath, virtualTargetPath string, entries []os.FileInfo, recursion int) error {
  645. for _, info := range entries {
  646. sourcePath := path.Join(virtualSourcePath, info.Name())
  647. targetPath := path.Join(virtualTargetPath, info.Name())
  648. targetInfo, err := c.DoStat(targetPath, 1, false)
  649. if err == nil {
  650. if info.IsDir() && targetInfo.IsDir() {
  651. c.Log(logger.LevelDebug, "target copy dir %q already exists", targetPath)
  652. continue
  653. }
  654. }
  655. if err != nil && !c.IsNotExistError(err) {
  656. return err
  657. }
  658. if err := c.checkCopy(info, targetInfo, sourcePath, targetPath); err != nil {
  659. return err
  660. }
  661. if err := c.doRecursiveCopy(sourcePath, targetPath, info, true, recursion); err != nil {
  662. if c.IsNotExistError(err) {
  663. c.Log(logger.LevelInfo, "skipping copy for source path %q: %v", sourcePath, err)
  664. continue
  665. }
  666. return err
  667. }
  668. }
  669. return nil
  670. }
  671. // Copy virtualSourcePath to virtualTargetPath
  672. func (c *BaseConnection) Copy(virtualSourcePath, virtualTargetPath string) error {
  673. copyFromSource := strings.HasSuffix(virtualSourcePath, "/")
  674. copyInTarget := strings.HasSuffix(virtualTargetPath, "/")
  675. virtualSourcePath = path.Clean(virtualSourcePath)
  676. virtualTargetPath = path.Clean(virtualTargetPath)
  677. if virtualSourcePath == virtualTargetPath {
  678. return fmt.Errorf("the copy source and target cannot be the same: %w", c.GetOpUnsupportedError())
  679. }
  680. srcInfo, err := c.DoStat(virtualSourcePath, 1, false)
  681. if err != nil {
  682. return err
  683. }
  684. if srcInfo.Mode()&os.ModeSymlink != 0 {
  685. return fmt.Errorf("copying symlinks is not supported: %w", c.GetOpUnsupportedError())
  686. }
  687. dstInfo, err := c.DoStat(virtualTargetPath, 1, false)
  688. if err == nil && !copyFromSource {
  689. copyInTarget = dstInfo.IsDir()
  690. }
  691. if err != nil && !c.IsNotExistError(err) {
  692. return err
  693. }
  694. destPath := virtualTargetPath
  695. if copyInTarget {
  696. destPath = path.Join(virtualTargetPath, path.Base(virtualSourcePath))
  697. dstInfo, err = c.DoStat(destPath, 1, false)
  698. if err != nil && !c.IsNotExistError(err) {
  699. return err
  700. }
  701. }
  702. createTargetDir := true
  703. if dstInfo != nil && dstInfo.IsDir() {
  704. createTargetDir = false
  705. }
  706. if err := c.checkCopy(srcInfo, dstInfo, virtualSourcePath, destPath); err != nil {
  707. return err
  708. }
  709. if err := c.CheckParentDirs(path.Dir(destPath)); err != nil {
  710. return err
  711. }
  712. done := make(chan bool)
  713. defer close(done)
  714. go keepConnectionAlive(c, done, 2*time.Minute)
  715. return c.doRecursiveCopy(virtualSourcePath, destPath, srcInfo, createTargetDir, 0)
  716. }
  717. // Rename renames (moves) virtualSourcePath to virtualTargetPath
  718. func (c *BaseConnection) Rename(virtualSourcePath, virtualTargetPath string) error {
  719. return c.renameInternal(virtualSourcePath, virtualTargetPath, false)
  720. }
  721. func (c *BaseConnection) renameInternal(virtualSourcePath, virtualTargetPath string, checkParentDestination bool) error {
  722. if virtualSourcePath == virtualTargetPath {
  723. return fmt.Errorf("the rename source and target cannot be the same: %w", c.GetOpUnsupportedError())
  724. }
  725. fsSrc, fsSourcePath, err := c.GetFsAndResolvedPath(virtualSourcePath)
  726. if err != nil {
  727. return err
  728. }
  729. fsDst, fsTargetPath, err := c.GetFsAndResolvedPath(virtualTargetPath)
  730. if err != nil {
  731. return err
  732. }
  733. startTime := time.Now()
  734. srcInfo, err := fsSrc.Lstat(fsSourcePath)
  735. if err != nil {
  736. return c.GetFsError(fsSrc, err)
  737. }
  738. if !c.isRenamePermitted(fsSrc, fsDst, fsSourcePath, fsTargetPath, virtualSourcePath, virtualTargetPath, srcInfo) {
  739. return c.GetPermissionDeniedError()
  740. }
  741. initialSize := int64(-1)
  742. if dstInfo, err := fsDst.Lstat(fsTargetPath); err == nil {
  743. checkParentDestination = false
  744. if dstInfo.IsDir() {
  745. c.Log(logger.LevelWarn, "attempted to rename %q overwriting an existing directory %q",
  746. fsSourcePath, fsTargetPath)
  747. return c.GetOpUnsupportedError()
  748. }
  749. // we are overwriting an existing file/symlink
  750. if dstInfo.Mode().IsRegular() {
  751. initialSize = dstInfo.Size()
  752. }
  753. if !c.User.HasPerm(dataprovider.PermOverwrite, path.Dir(virtualTargetPath)) {
  754. c.Log(logger.LevelDebug, "renaming %q -> %q is not allowed. Target exists but the user %q"+
  755. "has no overwrite permission", virtualSourcePath, virtualTargetPath, c.User.Username)
  756. return c.GetPermissionDeniedError()
  757. }
  758. }
  759. if srcInfo.IsDir() {
  760. if err := c.checkFolderRename(fsSrc, fsDst, fsSourcePath, fsTargetPath, virtualSourcePath, virtualTargetPath, srcInfo); err != nil {
  761. return err
  762. }
  763. }
  764. if !c.hasSpaceForRename(fsSrc, virtualSourcePath, virtualTargetPath, initialSize, fsSourcePath) {
  765. c.Log(logger.LevelInfo, "denying cross rename due to space limit")
  766. return c.GetGenericError(ErrQuotaExceeded)
  767. }
  768. if checkParentDestination {
  769. c.CheckParentDirs(path.Dir(virtualTargetPath)) //nolint:errcheck
  770. }
  771. done := make(chan bool)
  772. defer close(done)
  773. go keepConnectionAlive(c, done, 2*time.Minute)
  774. files, size, err := fsDst.Rename(fsSourcePath, fsTargetPath)
  775. if err != nil {
  776. c.Log(logger.LevelError, "failed to rename %q -> %q: %+v", fsSourcePath, fsTargetPath, err)
  777. return c.GetFsError(fsSrc, err)
  778. }
  779. vfs.SetPathPermissions(fsDst, fsTargetPath, c.User.GetUID(), c.User.GetGID())
  780. elapsed := time.Since(startTime).Nanoseconds() / 1000000
  781. c.updateQuotaAfterRename(fsDst, virtualSourcePath, virtualTargetPath, fsTargetPath, initialSize, files, size) //nolint:errcheck
  782. logger.CommandLog(renameLogSender, fsSourcePath, fsTargetPath, c.User.Username, "", c.ID, c.protocol, -1, -1,
  783. "", "", "", -1, c.localAddr, c.remoteAddr, elapsed)
  784. ExecuteActionNotification(c, operationRename, fsSourcePath, virtualSourcePath, fsTargetPath, //nolint:errcheck
  785. virtualTargetPath, "", 0, nil, elapsed, nil)
  786. return nil
  787. }
  788. // CreateSymlink creates fsTargetPath as a symbolic link to fsSourcePath
  789. func (c *BaseConnection) CreateSymlink(virtualSourcePath, virtualTargetPath string) error {
  790. var relativePath string
  791. if !path.IsAbs(virtualSourcePath) {
  792. relativePath = virtualSourcePath
  793. virtualSourcePath = path.Join(path.Dir(virtualTargetPath), relativePath)
  794. c.Log(logger.LevelDebug, "link relative path %q resolved as %q, target path %q",
  795. relativePath, virtualSourcePath, virtualTargetPath)
  796. }
  797. if c.isCrossFoldersRequest(virtualSourcePath, virtualTargetPath) {
  798. c.Log(logger.LevelWarn, "cross folder symlink is not supported, src: %v dst: %v", virtualSourcePath, virtualTargetPath)
  799. return c.GetOpUnsupportedError()
  800. }
  801. // we cannot have a cross folder request here so only one fs is enough
  802. fs, fsSourcePath, err := c.GetFsAndResolvedPath(virtualSourcePath)
  803. if err != nil {
  804. return err
  805. }
  806. fsTargetPath, err := fs.ResolvePath(virtualTargetPath)
  807. if err != nil {
  808. return c.GetFsError(fs, err)
  809. }
  810. if fs.GetRelativePath(fsSourcePath) == "/" {
  811. c.Log(logger.LevelError, "symlinking root dir is not allowed")
  812. return c.GetPermissionDeniedError()
  813. }
  814. if fs.GetRelativePath(fsTargetPath) == "/" {
  815. c.Log(logger.LevelError, "symlinking to root dir is not allowed")
  816. return c.GetPermissionDeniedError()
  817. }
  818. if !c.User.HasPerm(dataprovider.PermCreateSymlinks, path.Dir(virtualTargetPath)) {
  819. return c.GetPermissionDeniedError()
  820. }
  821. ok, policy := c.User.IsFileAllowed(virtualSourcePath)
  822. if !ok && policy == sdk.DenyPolicyHide {
  823. c.Log(logger.LevelError, "symlink source path %q is not allowed", virtualSourcePath)
  824. return c.GetNotExistError()
  825. }
  826. if ok, _ = c.User.IsFileAllowed(virtualTargetPath); !ok {
  827. c.Log(logger.LevelError, "symlink target path %q is not allowed", virtualTargetPath)
  828. return c.GetPermissionDeniedError()
  829. }
  830. if relativePath != "" {
  831. fsSourcePath = relativePath
  832. }
  833. startTime := time.Now()
  834. if err := fs.Symlink(fsSourcePath, fsTargetPath); err != nil {
  835. c.Log(logger.LevelError, "failed to create symlink %q -> %q: %+v", fsSourcePath, fsTargetPath, err)
  836. return c.GetFsError(fs, err)
  837. }
  838. elapsed := time.Since(startTime).Nanoseconds() / 1000000
  839. logger.CommandLog(symlinkLogSender, fsSourcePath, fsTargetPath, c.User.Username, "", c.ID, c.protocol, -1, -1, "",
  840. "", "", -1, c.localAddr, c.remoteAddr, elapsed)
  841. return nil
  842. }
  843. func (c *BaseConnection) getPathForSetStatPerms(fs vfs.Fs, fsPath, virtualPath string) string {
  844. pathForPerms := virtualPath
  845. if fi, err := fs.Lstat(fsPath); err == nil {
  846. if fi.IsDir() {
  847. pathForPerms = path.Dir(virtualPath)
  848. }
  849. }
  850. return pathForPerms
  851. }
  852. func (c *BaseConnection) doStatInternal(virtualPath string, mode int, checkFilePatterns,
  853. convertResult bool,
  854. ) (os.FileInfo, error) {
  855. // for some vfs we don't create intermediary folders so we cannot simply check
  856. // if virtualPath is a virtual folder. Allowing stat for hidden virtual folders
  857. // is by purpose.
  858. vfolders := c.User.GetVirtualFoldersInPath(path.Dir(virtualPath))
  859. if _, ok := vfolders[virtualPath]; ok {
  860. return vfs.NewFileInfo(virtualPath, true, 0, time.Unix(0, 0), false), nil
  861. }
  862. if checkFilePatterns && virtualPath != "/" {
  863. ok, policy := c.User.IsFileAllowed(virtualPath)
  864. if !ok && policy == sdk.DenyPolicyHide {
  865. return nil, c.GetNotExistError()
  866. }
  867. }
  868. var info os.FileInfo
  869. fs, fsPath, err := c.GetFsAndResolvedPath(virtualPath)
  870. if err != nil {
  871. return nil, err
  872. }
  873. if mode == 1 {
  874. info, err = fs.Lstat(c.getRealFsPath(fsPath))
  875. } else {
  876. info, err = fs.Stat(c.getRealFsPath(fsPath))
  877. }
  878. if err != nil {
  879. if !fs.IsNotExist(err) {
  880. c.Log(logger.LevelWarn, "stat error for path %q: %+v", virtualPath, err)
  881. }
  882. return nil, c.GetFsError(fs, err)
  883. }
  884. if convertResult && vfs.IsCryptOsFs(fs) {
  885. info = fs.(*vfs.CryptFs).ConvertFileInfo(info)
  886. }
  887. return info, nil
  888. }
  889. // DoStat execute a Stat if mode = 0, Lstat if mode = 1
  890. func (c *BaseConnection) DoStat(virtualPath string, mode int, checkFilePatterns bool) (os.FileInfo, error) {
  891. return c.doStatInternal(virtualPath, mode, checkFilePatterns, true)
  892. }
  893. func (c *BaseConnection) createDirIfMissing(name string) error {
  894. _, err := c.DoStat(name, 0, false)
  895. if c.IsNotExistError(err) {
  896. return c.CreateDir(name, false)
  897. }
  898. return err
  899. }
  900. func (c *BaseConnection) ignoreSetStat(fs vfs.Fs) bool {
  901. if Config.SetstatMode == 1 {
  902. return true
  903. }
  904. if Config.SetstatMode == 2 && !vfs.IsLocalOrSFTPFs(fs) && !vfs.IsCryptOsFs(fs) {
  905. return true
  906. }
  907. return false
  908. }
  909. func (c *BaseConnection) handleChmod(fs vfs.Fs, fsPath, pathForPerms string, attributes *StatAttributes) error {
  910. if !c.User.HasPerm(dataprovider.PermChmod, pathForPerms) {
  911. return c.GetPermissionDeniedError()
  912. }
  913. if c.ignoreSetStat(fs) {
  914. return nil
  915. }
  916. startTime := time.Now()
  917. if err := fs.Chmod(c.getRealFsPath(fsPath), attributes.Mode); err != nil {
  918. c.Log(logger.LevelError, "failed to chmod path %q, mode: %v, err: %+v", fsPath, attributes.Mode.String(), err)
  919. return c.GetFsError(fs, err)
  920. }
  921. elapsed := time.Since(startTime).Nanoseconds() / 1000000
  922. logger.CommandLog(chmodLogSender, fsPath, "", c.User.Username, attributes.Mode.String(), c.ID, c.protocol,
  923. -1, -1, "", "", "", -1, c.localAddr, c.remoteAddr, elapsed)
  924. return nil
  925. }
  926. func (c *BaseConnection) handleChown(fs vfs.Fs, fsPath, pathForPerms string, attributes *StatAttributes) error {
  927. if !c.User.HasPerm(dataprovider.PermChown, pathForPerms) {
  928. return c.GetPermissionDeniedError()
  929. }
  930. if c.ignoreSetStat(fs) {
  931. return nil
  932. }
  933. startTime := time.Now()
  934. if err := fs.Chown(c.getRealFsPath(fsPath), attributes.UID, attributes.GID); err != nil {
  935. c.Log(logger.LevelError, "failed to chown path %q, uid: %v, gid: %v, err: %+v", fsPath, attributes.UID,
  936. attributes.GID, err)
  937. return c.GetFsError(fs, err)
  938. }
  939. elapsed := time.Since(startTime).Nanoseconds() / 1000000
  940. logger.CommandLog(chownLogSender, fsPath, "", c.User.Username, "", c.ID, c.protocol, attributes.UID, attributes.GID,
  941. "", "", "", -1, c.localAddr, c.remoteAddr, elapsed)
  942. return nil
  943. }
  944. func (c *BaseConnection) handleChtimes(fs vfs.Fs, fsPath, pathForPerms string, attributes *StatAttributes) error {
  945. if !c.User.HasPerm(dataprovider.PermChtimes, pathForPerms) {
  946. return c.GetPermissionDeniedError()
  947. }
  948. if Config.SetstatMode == 1 {
  949. return nil
  950. }
  951. startTime := time.Now()
  952. isUploading := c.setTimes(fsPath, attributes.Atime, attributes.Mtime)
  953. if err := fs.Chtimes(c.getRealFsPath(fsPath), attributes.Atime, attributes.Mtime, isUploading); err != nil {
  954. c.setTimes(fsPath, time.Time{}, time.Time{})
  955. if errors.Is(err, vfs.ErrVfsUnsupported) && Config.SetstatMode == 2 {
  956. return nil
  957. }
  958. c.Log(logger.LevelError, "failed to chtimes for path %q, access time: %v, modification time: %v, err: %+v",
  959. fsPath, attributes.Atime, attributes.Mtime, err)
  960. return c.GetFsError(fs, err)
  961. }
  962. elapsed := time.Since(startTime).Nanoseconds() / 1000000
  963. accessTimeString := attributes.Atime.Format(chtimesFormat)
  964. modificationTimeString := attributes.Mtime.Format(chtimesFormat)
  965. logger.CommandLog(chtimesLogSender, fsPath, "", c.User.Username, "", c.ID, c.protocol, -1, -1,
  966. accessTimeString, modificationTimeString, "", -1, c.localAddr, c.remoteAddr, elapsed)
  967. return nil
  968. }
  969. // SetStat set StatAttributes for the specified fsPath
  970. func (c *BaseConnection) SetStat(virtualPath string, attributes *StatAttributes) error {
  971. if ok, policy := c.User.IsFileAllowed(virtualPath); !ok {
  972. return c.GetErrorForDeniedFile(policy)
  973. }
  974. fs, fsPath, err := c.GetFsAndResolvedPath(virtualPath)
  975. if err != nil {
  976. return err
  977. }
  978. pathForPerms := c.getPathForSetStatPerms(fs, fsPath, virtualPath)
  979. if attributes.Flags&StatAttrTimes != 0 {
  980. if err = c.handleChtimes(fs, fsPath, pathForPerms, attributes); err != nil {
  981. return err
  982. }
  983. }
  984. if attributes.Flags&StatAttrPerms != 0 {
  985. if err = c.handleChmod(fs, fsPath, pathForPerms, attributes); err != nil {
  986. return err
  987. }
  988. }
  989. if attributes.Flags&StatAttrUIDGID != 0 {
  990. if err = c.handleChown(fs, fsPath, pathForPerms, attributes); err != nil {
  991. return err
  992. }
  993. }
  994. if attributes.Flags&StatAttrSize != 0 {
  995. if !c.User.HasPerm(dataprovider.PermOverwrite, pathForPerms) {
  996. return c.GetPermissionDeniedError()
  997. }
  998. startTime := time.Now()
  999. if err = c.truncateFile(fs, fsPath, virtualPath, attributes.Size); err != nil {
  1000. c.Log(logger.LevelError, "failed to truncate path %q, size: %v, err: %+v", fsPath, attributes.Size, err)
  1001. return c.GetFsError(fs, err)
  1002. }
  1003. elapsed := time.Since(startTime).Nanoseconds() / 1000000
  1004. logger.CommandLog(truncateLogSender, fsPath, "", c.User.Username, "", c.ID, c.protocol, -1, -1, "", "",
  1005. "", attributes.Size, c.localAddr, c.remoteAddr, elapsed)
  1006. }
  1007. return nil
  1008. }
  1009. func (c *BaseConnection) truncateFile(fs vfs.Fs, fsPath, virtualPath string, size int64) error {
  1010. // check first if we have an open transfer for the given path and try to truncate the file already opened
  1011. // if we found no transfer we truncate by path.
  1012. var initialSize int64
  1013. var err error
  1014. initialSize, err = c.truncateOpenHandle(fsPath, size)
  1015. if err == errNoTransfer {
  1016. c.Log(logger.LevelDebug, "file path %q not found in active transfers, execute trucate by path", fsPath)
  1017. var info os.FileInfo
  1018. info, err = fs.Stat(fsPath)
  1019. if err != nil {
  1020. return err
  1021. }
  1022. initialSize = info.Size()
  1023. err = fs.Truncate(fsPath, size)
  1024. }
  1025. if err == nil && vfs.HasTruncateSupport(fs) {
  1026. sizeDiff := initialSize - size
  1027. vfolder, err := c.User.GetVirtualFolderForPath(path.Dir(virtualPath))
  1028. if err == nil {
  1029. dataprovider.UpdateVirtualFolderQuota(&vfolder.BaseVirtualFolder, 0, -sizeDiff, false) //nolint:errcheck
  1030. if vfolder.IsIncludedInUserQuota() {
  1031. dataprovider.UpdateUserQuota(&c.User, 0, -sizeDiff, false) //nolint:errcheck
  1032. }
  1033. } else {
  1034. dataprovider.UpdateUserQuota(&c.User, 0, -sizeDiff, false) //nolint:errcheck
  1035. }
  1036. }
  1037. return err
  1038. }
  1039. func (c *BaseConnection) checkRecursiveRenameDirPermissions(fsSrc, fsDst vfs.Fs, sourcePath, targetPath,
  1040. virtualSourcePath, virtualTargetPath string, fi os.FileInfo,
  1041. ) error {
  1042. if !c.User.HasPermissionsInside(virtualSourcePath) &&
  1043. !c.User.HasPermissionsInside(virtualTargetPath) {
  1044. if !c.isRenamePermitted(fsSrc, fsDst, sourcePath, targetPath, virtualSourcePath, virtualTargetPath, fi) {
  1045. c.Log(logger.LevelInfo, "rename %q -> %q is not allowed, virtual destination path: %q",
  1046. sourcePath, targetPath, virtualTargetPath)
  1047. return c.GetPermissionDeniedError()
  1048. }
  1049. // if all rename permissions are granted we have finished, otherwise we have to walk
  1050. // because we could have the rename dir permission but not the rename file and the dir to
  1051. // rename could contain files
  1052. if c.User.HasPermsRenameAll(path.Dir(virtualSourcePath)) && c.User.HasPermsRenameAll(path.Dir(virtualTargetPath)) {
  1053. return nil
  1054. }
  1055. }
  1056. return fsSrc.Walk(sourcePath, func(walkedPath string, info os.FileInfo, err error) error {
  1057. if err != nil {
  1058. return c.GetFsError(fsSrc, err)
  1059. }
  1060. if walkedPath != sourcePath && !vfs.IsRenameAtomic(fsSrc) && Config.RenameMode == 0 {
  1061. c.Log(logger.LevelInfo, "cannot rename non empty directory %q on this filesystem", virtualSourcePath)
  1062. return c.GetOpUnsupportedError()
  1063. }
  1064. dstPath := strings.Replace(walkedPath, sourcePath, targetPath, 1)
  1065. virtualSrcPath := fsSrc.GetRelativePath(walkedPath)
  1066. virtualDstPath := fsDst.GetRelativePath(dstPath)
  1067. if !c.isRenamePermitted(fsSrc, fsDst, walkedPath, dstPath, virtualSrcPath, virtualDstPath, info) {
  1068. c.Log(logger.LevelInfo, "rename %q -> %q is not allowed, virtual destination path: %q",
  1069. walkedPath, dstPath, virtualDstPath)
  1070. return c.GetPermissionDeniedError()
  1071. }
  1072. return nil
  1073. })
  1074. }
  1075. func (c *BaseConnection) hasRenamePerms(virtualSourcePath, virtualTargetPath string, fi os.FileInfo) bool {
  1076. if c.User.HasPermsRenameAll(path.Dir(virtualSourcePath)) &&
  1077. c.User.HasPermsRenameAll(path.Dir(virtualTargetPath)) {
  1078. return true
  1079. }
  1080. if fi == nil {
  1081. // we don't know if this is a file or a directory and we don't have all the rename perms, return false
  1082. return false
  1083. }
  1084. if fi.IsDir() {
  1085. perms := []string{
  1086. dataprovider.PermRenameDirs,
  1087. dataprovider.PermRename,
  1088. }
  1089. return c.User.HasAnyPerm(perms, path.Dir(virtualSourcePath)) &&
  1090. c.User.HasAnyPerm(perms, path.Dir(virtualTargetPath))
  1091. }
  1092. // file or symlink
  1093. perms := []string{
  1094. dataprovider.PermRenameFiles,
  1095. dataprovider.PermRename,
  1096. }
  1097. return c.User.HasAnyPerm(perms, path.Dir(virtualSourcePath)) &&
  1098. c.User.HasAnyPerm(perms, path.Dir(virtualTargetPath))
  1099. }
  1100. func (c *BaseConnection) checkFolderRename(fsSrc, fsDst vfs.Fs, fsSourcePath, fsTargetPath, virtualSourcePath,
  1101. virtualTargetPath string, fi os.FileInfo) error {
  1102. if util.IsDirOverlapped(virtualSourcePath, virtualTargetPath, true, "/") {
  1103. c.Log(logger.LevelDebug, "renaming the folder %q->%q is not supported: nested folders",
  1104. virtualSourcePath, virtualTargetPath)
  1105. return fmt.Errorf("nested rename %q => %q is not supported: %w",
  1106. virtualSourcePath, virtualTargetPath, c.GetOpUnsupportedError())
  1107. }
  1108. if util.IsDirOverlapped(fsSourcePath, fsTargetPath, true, c.User.FsConfig.GetPathSeparator()) {
  1109. c.Log(logger.LevelDebug, "renaming the folder %q->%q is not supported: nested fs folders",
  1110. fsSourcePath, fsTargetPath)
  1111. return fmt.Errorf("nested fs rename %q => %q is not supported: %w",
  1112. fsSourcePath, fsTargetPath, c.GetOpUnsupportedError())
  1113. }
  1114. if c.User.HasVirtualFoldersInside(virtualSourcePath) {
  1115. c.Log(logger.LevelDebug, "renaming the folder %q is not supported: it has virtual folders inside it",
  1116. virtualSourcePath)
  1117. return fmt.Errorf("folder %q has virtual folders inside it: %w", virtualSourcePath, c.GetOpUnsupportedError())
  1118. }
  1119. if c.User.HasVirtualFoldersInside(virtualTargetPath) {
  1120. c.Log(logger.LevelDebug, "renaming the folder %q is not supported, the target %q has virtual folders inside it",
  1121. virtualSourcePath, virtualTargetPath)
  1122. return fmt.Errorf("folder %q has virtual folders inside it: %w", virtualTargetPath, c.GetOpUnsupportedError())
  1123. }
  1124. if err := c.checkRecursiveRenameDirPermissions(fsSrc, fsDst, fsSourcePath, fsTargetPath,
  1125. virtualSourcePath, virtualTargetPath, fi); err != nil {
  1126. c.Log(logger.LevelDebug, "error checking recursive permissions before renaming %q: %+v", fsSourcePath, err)
  1127. return err
  1128. }
  1129. return nil
  1130. }
  1131. func (c *BaseConnection) isRenamePermitted(fsSrc, fsDst vfs.Fs, fsSourcePath, fsTargetPath, virtualSourcePath,
  1132. virtualTargetPath string, fi os.FileInfo,
  1133. ) bool {
  1134. if !c.IsSameResource(virtualSourcePath, virtualTargetPath) {
  1135. c.Log(logger.LevelInfo, "rename %q->%q is not allowed: the paths must be on the same resource",
  1136. virtualSourcePath, virtualTargetPath)
  1137. return false
  1138. }
  1139. if c.User.IsMappedPath(fsSourcePath) && vfs.IsLocalOrCryptoFs(fsSrc) {
  1140. c.Log(logger.LevelWarn, "renaming a directory mapped as virtual folder is not allowed: %q", fsSourcePath)
  1141. return false
  1142. }
  1143. if c.User.IsMappedPath(fsTargetPath) && vfs.IsLocalOrCryptoFs(fsDst) {
  1144. c.Log(logger.LevelWarn, "renaming to a directory mapped as virtual folder is not allowed: %q", fsTargetPath)
  1145. return false
  1146. }
  1147. if virtualSourcePath == "/" || virtualTargetPath == "/" || fsSrc.GetRelativePath(fsSourcePath) == "/" {
  1148. c.Log(logger.LevelWarn, "renaming root dir is not allowed")
  1149. return false
  1150. }
  1151. if c.User.IsVirtualFolder(virtualSourcePath) || c.User.IsVirtualFolder(virtualTargetPath) {
  1152. c.Log(logger.LevelWarn, "renaming a virtual folder is not allowed")
  1153. return false
  1154. }
  1155. isSrcAllowed, _ := c.User.IsFileAllowed(virtualSourcePath)
  1156. isDstAllowed, _ := c.User.IsFileAllowed(virtualTargetPath)
  1157. if !isSrcAllowed || !isDstAllowed {
  1158. c.Log(logger.LevelDebug, "renaming source: %q to target: %q not allowed", virtualSourcePath,
  1159. virtualTargetPath)
  1160. return false
  1161. }
  1162. return c.hasRenamePerms(virtualSourcePath, virtualTargetPath, fi)
  1163. }
  1164. func (c *BaseConnection) hasSpaceForRename(fs vfs.Fs, virtualSourcePath, virtualTargetPath string, initialSize int64,
  1165. fsSourcePath string) bool {
  1166. if dataprovider.GetQuotaTracking() == 0 {
  1167. return true
  1168. }
  1169. sourceFolder, errSrc := c.User.GetVirtualFolderForPath(path.Dir(virtualSourcePath))
  1170. dstFolder, errDst := c.User.GetVirtualFolderForPath(path.Dir(virtualTargetPath))
  1171. if errSrc != nil && errDst != nil {
  1172. // rename inside the user home dir
  1173. return true
  1174. }
  1175. if errSrc == nil && errDst == nil {
  1176. // rename between virtual folders
  1177. if sourceFolder.Name == dstFolder.Name {
  1178. // rename inside the same virtual folder
  1179. return true
  1180. }
  1181. }
  1182. if errSrc != nil && dstFolder.IsIncludedInUserQuota() {
  1183. // rename between user root dir and a virtual folder included in user quota
  1184. return true
  1185. }
  1186. if errDst != nil && sourceFolder.IsIncludedInUserQuota() {
  1187. // rename between a virtual folder included in user quota and the user root dir
  1188. return true
  1189. }
  1190. quotaResult, _ := c.HasSpace(true, false, virtualTargetPath)
  1191. if quotaResult.HasSpace && quotaResult.QuotaSize == 0 && quotaResult.QuotaFiles == 0 {
  1192. // no quota restrictions
  1193. return true
  1194. }
  1195. return c.hasSpaceForCrossRename(fs, quotaResult, initialSize, fsSourcePath)
  1196. }
  1197. // hasSpaceForCrossRename checks the quota after a rename between different folders
  1198. func (c *BaseConnection) hasSpaceForCrossRename(fs vfs.Fs, quotaResult vfs.QuotaCheckResult, initialSize int64, sourcePath string) bool {
  1199. if !quotaResult.HasSpace && initialSize == -1 {
  1200. // we are over quota and this is not a file replace
  1201. return false
  1202. }
  1203. fi, err := fs.Lstat(sourcePath)
  1204. if err != nil {
  1205. c.Log(logger.LevelError, "cross rename denied, stat error for path %q: %v", sourcePath, err)
  1206. return false
  1207. }
  1208. var sizeDiff int64
  1209. var filesDiff int
  1210. if fi.Mode().IsRegular() {
  1211. sizeDiff = fi.Size()
  1212. filesDiff = 1
  1213. if initialSize != -1 {
  1214. sizeDiff -= initialSize
  1215. filesDiff = 0
  1216. }
  1217. } else if fi.IsDir() {
  1218. filesDiff, sizeDiff, err = fs.GetDirSize(sourcePath)
  1219. if err != nil {
  1220. c.Log(logger.LevelError, "cross rename denied, error getting size for directory %q: %v", sourcePath, err)
  1221. return false
  1222. }
  1223. }
  1224. if !quotaResult.HasSpace && initialSize != -1 {
  1225. // we are over quota but we are overwriting an existing file so we check if the quota size after the rename is ok
  1226. if quotaResult.QuotaSize == 0 {
  1227. return true
  1228. }
  1229. c.Log(logger.LevelDebug, "cross rename overwrite, source %q, used size %d, size to add %d",
  1230. sourcePath, quotaResult.UsedSize, sizeDiff)
  1231. quotaResult.UsedSize += sizeDiff
  1232. return quotaResult.GetRemainingSize() >= 0
  1233. }
  1234. if quotaResult.QuotaFiles > 0 {
  1235. remainingFiles := quotaResult.GetRemainingFiles()
  1236. c.Log(logger.LevelDebug, "cross rename, source %q remaining file %d to add %d", sourcePath,
  1237. remainingFiles, filesDiff)
  1238. if remainingFiles < filesDiff {
  1239. return false
  1240. }
  1241. }
  1242. if quotaResult.QuotaSize > 0 {
  1243. remainingSize := quotaResult.GetRemainingSize()
  1244. c.Log(logger.LevelDebug, "cross rename, source %q remaining size %d to add %d", sourcePath,
  1245. remainingSize, sizeDiff)
  1246. if remainingSize < sizeDiff {
  1247. return false
  1248. }
  1249. }
  1250. return true
  1251. }
  1252. // GetMaxWriteSize returns the allowed size for an upload or an error
  1253. // if no enough size is available for a resume/append
  1254. func (c *BaseConnection) GetMaxWriteSize(quotaResult vfs.QuotaCheckResult, isResume bool, fileSize int64,
  1255. isUploadResumeSupported bool,
  1256. ) (int64, error) {
  1257. maxWriteSize := quotaResult.GetRemainingSize()
  1258. if isResume {
  1259. if !isUploadResumeSupported {
  1260. return 0, c.GetOpUnsupportedError()
  1261. }
  1262. if c.User.Filters.MaxUploadFileSize > 0 && c.User.Filters.MaxUploadFileSize <= fileSize {
  1263. return 0, c.GetQuotaExceededError()
  1264. }
  1265. if c.User.Filters.MaxUploadFileSize > 0 {
  1266. maxUploadSize := c.User.Filters.MaxUploadFileSize - fileSize
  1267. if maxUploadSize < maxWriteSize || maxWriteSize == 0 {
  1268. maxWriteSize = maxUploadSize
  1269. }
  1270. }
  1271. } else {
  1272. if maxWriteSize > 0 {
  1273. maxWriteSize += fileSize
  1274. }
  1275. if c.User.Filters.MaxUploadFileSize > 0 && (c.User.Filters.MaxUploadFileSize < maxWriteSize || maxWriteSize == 0) {
  1276. maxWriteSize = c.User.Filters.MaxUploadFileSize
  1277. }
  1278. }
  1279. return maxWriteSize, nil
  1280. }
  1281. // GetTransferQuota returns the data transfers quota
  1282. func (c *BaseConnection) GetTransferQuota() dataprovider.TransferQuota {
  1283. result, _, _ := c.checkUserQuota()
  1284. return result
  1285. }
  1286. func (c *BaseConnection) checkUserQuota() (dataprovider.TransferQuota, int, int64) {
  1287. ul, dl, total := c.User.GetDataTransferLimits()
  1288. result := dataprovider.TransferQuota{
  1289. ULSize: ul,
  1290. DLSize: dl,
  1291. TotalSize: total,
  1292. AllowedULSize: 0,
  1293. AllowedDLSize: 0,
  1294. AllowedTotalSize: 0,
  1295. }
  1296. if !c.User.HasTransferQuotaRestrictions() {
  1297. return result, -1, -1
  1298. }
  1299. usedFiles, usedSize, usedULSize, usedDLSize, err := dataprovider.GetUsedQuota(c.User.Username)
  1300. if err != nil {
  1301. c.Log(logger.LevelError, "error getting used quota for %q: %v", c.User.Username, err)
  1302. result.AllowedTotalSize = -1
  1303. return result, -1, -1
  1304. }
  1305. if result.TotalSize > 0 {
  1306. result.AllowedTotalSize = result.TotalSize - (usedULSize + usedDLSize)
  1307. }
  1308. if result.ULSize > 0 {
  1309. result.AllowedULSize = result.ULSize - usedULSize
  1310. }
  1311. if result.DLSize > 0 {
  1312. result.AllowedDLSize = result.DLSize - usedDLSize
  1313. }
  1314. return result, usedFiles, usedSize
  1315. }
  1316. // HasSpace checks user's quota usage
  1317. func (c *BaseConnection) HasSpace(checkFiles, getUsage bool, requestPath string) (vfs.QuotaCheckResult,
  1318. dataprovider.TransferQuota,
  1319. ) {
  1320. result := vfs.QuotaCheckResult{
  1321. HasSpace: true,
  1322. AllowedSize: 0,
  1323. AllowedFiles: 0,
  1324. UsedSize: 0,
  1325. UsedFiles: 0,
  1326. QuotaSize: 0,
  1327. QuotaFiles: 0,
  1328. }
  1329. if dataprovider.GetQuotaTracking() == 0 {
  1330. return result, dataprovider.TransferQuota{}
  1331. }
  1332. transferQuota, usedFiles, usedSize := c.checkUserQuota()
  1333. var err error
  1334. var vfolder vfs.VirtualFolder
  1335. vfolder, err = c.User.GetVirtualFolderForPath(path.Dir(requestPath))
  1336. if err == nil && !vfolder.IsIncludedInUserQuota() {
  1337. if vfolder.HasNoQuotaRestrictions(checkFiles) && !getUsage {
  1338. return result, transferQuota
  1339. }
  1340. result.QuotaSize = vfolder.QuotaSize
  1341. result.QuotaFiles = vfolder.QuotaFiles
  1342. result.UsedFiles, result.UsedSize, err = dataprovider.GetUsedVirtualFolderQuota(vfolder.Name)
  1343. } else {
  1344. if c.User.HasNoQuotaRestrictions(checkFiles) && !getUsage {
  1345. return result, transferQuota
  1346. }
  1347. result.QuotaSize = c.User.QuotaSize
  1348. result.QuotaFiles = c.User.QuotaFiles
  1349. if usedSize == -1 {
  1350. result.UsedFiles, result.UsedSize, _, _, err = dataprovider.GetUsedQuota(c.User.Username)
  1351. } else {
  1352. err = nil
  1353. result.UsedFiles = usedFiles
  1354. result.UsedSize = usedSize
  1355. }
  1356. }
  1357. if err != nil {
  1358. c.Log(logger.LevelError, "error getting used quota for %q request path %q: %v", c.User.Username, requestPath, err)
  1359. result.HasSpace = false
  1360. return result, transferQuota
  1361. }
  1362. result.AllowedFiles = result.QuotaFiles - result.UsedFiles
  1363. result.AllowedSize = result.QuotaSize - result.UsedSize
  1364. if (checkFiles && result.QuotaFiles > 0 && result.UsedFiles >= result.QuotaFiles) ||
  1365. (result.QuotaSize > 0 && result.UsedSize >= result.QuotaSize) {
  1366. c.Log(logger.LevelDebug, "quota exceed for user %q, request path %q, num files: %d/%d, size: %d/%d check files: %t",
  1367. c.User.Username, requestPath, result.UsedFiles, result.QuotaFiles, result.UsedSize, result.QuotaSize, checkFiles)
  1368. result.HasSpace = false
  1369. return result, transferQuota
  1370. }
  1371. return result, transferQuota
  1372. }
  1373. // IsSameResource returns true if source and target paths are on the same resource
  1374. func (c *BaseConnection) IsSameResource(virtualSourcePath, virtualTargetPath string) bool {
  1375. sourceFolder, errSrc := c.User.GetVirtualFolderForPath(virtualSourcePath)
  1376. dstFolder, errDst := c.User.GetVirtualFolderForPath(virtualTargetPath)
  1377. if errSrc != nil && errDst != nil {
  1378. return true
  1379. }
  1380. if errSrc == nil && errDst == nil {
  1381. if sourceFolder.Name == dstFolder.Name {
  1382. return true
  1383. }
  1384. // we have different folders, check if they point to the same resource
  1385. return sourceFolder.FsConfig.IsSameResource(dstFolder.FsConfig)
  1386. }
  1387. if errSrc == nil {
  1388. return sourceFolder.FsConfig.IsSameResource(c.User.FsConfig)
  1389. }
  1390. return dstFolder.FsConfig.IsSameResource(c.User.FsConfig)
  1391. }
  1392. func (c *BaseConnection) isCrossFoldersRequest(virtualSourcePath, virtualTargetPath string) bool {
  1393. sourceFolder, errSrc := c.User.GetVirtualFolderForPath(virtualSourcePath)
  1394. dstFolder, errDst := c.User.GetVirtualFolderForPath(virtualTargetPath)
  1395. if errSrc != nil && errDst != nil {
  1396. return false
  1397. }
  1398. if errSrc == nil && errDst == nil {
  1399. return sourceFolder.Name != dstFolder.Name
  1400. }
  1401. return true
  1402. }
  1403. func (c *BaseConnection) updateQuotaMoveBetweenVFolders(sourceFolder, dstFolder *vfs.VirtualFolder, initialSize,
  1404. filesSize int64, numFiles int) {
  1405. if sourceFolder.Name == dstFolder.Name {
  1406. // both files are inside the same virtual folder
  1407. if initialSize != -1 {
  1408. dataprovider.UpdateVirtualFolderQuota(&dstFolder.BaseVirtualFolder, -numFiles, -initialSize, false) //nolint:errcheck
  1409. if dstFolder.IsIncludedInUserQuota() {
  1410. dataprovider.UpdateUserQuota(&c.User, -numFiles, -initialSize, false) //nolint:errcheck
  1411. }
  1412. }
  1413. return
  1414. }
  1415. // files are inside different virtual folders
  1416. dataprovider.UpdateVirtualFolderQuota(&sourceFolder.BaseVirtualFolder, -numFiles, -filesSize, false) //nolint:errcheck
  1417. if sourceFolder.IsIncludedInUserQuota() {
  1418. dataprovider.UpdateUserQuota(&c.User, -numFiles, -filesSize, false) //nolint:errcheck
  1419. }
  1420. if initialSize == -1 {
  1421. dataprovider.UpdateVirtualFolderQuota(&dstFolder.BaseVirtualFolder, numFiles, filesSize, false) //nolint:errcheck
  1422. if dstFolder.IsIncludedInUserQuota() {
  1423. dataprovider.UpdateUserQuota(&c.User, numFiles, filesSize, false) //nolint:errcheck
  1424. }
  1425. } else {
  1426. // we cannot have a directory here, initialSize != -1 only for files
  1427. dataprovider.UpdateVirtualFolderQuota(&dstFolder.BaseVirtualFolder, 0, filesSize-initialSize, false) //nolint:errcheck
  1428. if dstFolder.IsIncludedInUserQuota() {
  1429. dataprovider.UpdateUserQuota(&c.User, 0, filesSize-initialSize, false) //nolint:errcheck
  1430. }
  1431. }
  1432. }
  1433. func (c *BaseConnection) updateQuotaMoveFromVFolder(sourceFolder *vfs.VirtualFolder, initialSize, filesSize int64, numFiles int) {
  1434. // move between a virtual folder and the user home dir
  1435. dataprovider.UpdateVirtualFolderQuota(&sourceFolder.BaseVirtualFolder, -numFiles, -filesSize, false) //nolint:errcheck
  1436. if sourceFolder.IsIncludedInUserQuota() {
  1437. dataprovider.UpdateUserQuota(&c.User, -numFiles, -filesSize, false) //nolint:errcheck
  1438. }
  1439. if initialSize == -1 {
  1440. dataprovider.UpdateUserQuota(&c.User, numFiles, filesSize, false) //nolint:errcheck
  1441. } else {
  1442. // we cannot have a directory here, initialSize != -1 only for files
  1443. dataprovider.UpdateUserQuota(&c.User, 0, filesSize-initialSize, false) //nolint:errcheck
  1444. }
  1445. }
  1446. func (c *BaseConnection) updateQuotaMoveToVFolder(dstFolder *vfs.VirtualFolder, initialSize, filesSize int64, numFiles int) {
  1447. // move between the user home dir and a virtual folder
  1448. dataprovider.UpdateUserQuota(&c.User, -numFiles, -filesSize, false) //nolint:errcheck
  1449. if initialSize == -1 {
  1450. dataprovider.UpdateVirtualFolderQuota(&dstFolder.BaseVirtualFolder, numFiles, filesSize, false) //nolint:errcheck
  1451. if dstFolder.IsIncludedInUserQuota() {
  1452. dataprovider.UpdateUserQuota(&c.User, numFiles, filesSize, false) //nolint:errcheck
  1453. }
  1454. } else {
  1455. // we cannot have a directory here, initialSize != -1 only for files
  1456. dataprovider.UpdateVirtualFolderQuota(&dstFolder.BaseVirtualFolder, 0, filesSize-initialSize, false) //nolint:errcheck
  1457. if dstFolder.IsIncludedInUserQuota() {
  1458. dataprovider.UpdateUserQuota(&c.User, 0, filesSize-initialSize, false) //nolint:errcheck
  1459. }
  1460. }
  1461. }
  1462. func (c *BaseConnection) updateQuotaAfterRename(fs vfs.Fs, virtualSourcePath, virtualTargetPath, targetPath string,
  1463. initialSize int64, numFiles int, filesSize int64,
  1464. ) error {
  1465. if dataprovider.GetQuotaTracking() == 0 {
  1466. return nil
  1467. }
  1468. // we don't allow to overwrite an existing directory so targetPath can be:
  1469. // - a new file, a symlink is as a new file here
  1470. // - a file overwriting an existing one
  1471. // - a new directory
  1472. // initialSize != -1 only when overwriting files
  1473. sourceFolder, errSrc := c.User.GetVirtualFolderForPath(path.Dir(virtualSourcePath))
  1474. dstFolder, errDst := c.User.GetVirtualFolderForPath(path.Dir(virtualTargetPath))
  1475. if errSrc != nil && errDst != nil {
  1476. // both files are contained inside the user home dir
  1477. if initialSize != -1 {
  1478. // we cannot have a directory here, we are overwriting an existing file
  1479. // we need to subtract the size of the overwritten file from the user quota
  1480. dataprovider.UpdateUserQuota(&c.User, -1, -initialSize, false) //nolint:errcheck
  1481. }
  1482. return nil
  1483. }
  1484. if filesSize == -1 {
  1485. // fs.Rename didn't return the affected files/sizes, we need to calculate them
  1486. numFiles = 1
  1487. if fi, err := fs.Stat(targetPath); err == nil {
  1488. if fi.Mode().IsDir() {
  1489. numFiles, filesSize, err = fs.GetDirSize(targetPath)
  1490. if err != nil {
  1491. c.Log(logger.LevelError, "failed to update quota after rename, error scanning moved folder %q: %+v",
  1492. targetPath, err)
  1493. return err
  1494. }
  1495. } else {
  1496. filesSize = fi.Size()
  1497. }
  1498. } else {
  1499. c.Log(logger.LevelError, "failed to update quota after renaming, file %q stat error: %+v", targetPath, err)
  1500. return err
  1501. }
  1502. c.Log(logger.LevelDebug, "calculated renamed files: %d, size: %d bytes", numFiles, filesSize)
  1503. } else {
  1504. c.Log(logger.LevelDebug, "returned renamed files: %d, size: %d bytes", numFiles, filesSize)
  1505. }
  1506. if errSrc == nil && errDst == nil {
  1507. c.updateQuotaMoveBetweenVFolders(&sourceFolder, &dstFolder, initialSize, filesSize, numFiles)
  1508. }
  1509. if errSrc == nil && errDst != nil {
  1510. c.updateQuotaMoveFromVFolder(&sourceFolder, initialSize, filesSize, numFiles)
  1511. }
  1512. if errSrc != nil && errDst == nil {
  1513. c.updateQuotaMoveToVFolder(&dstFolder, initialSize, filesSize, numFiles)
  1514. }
  1515. return nil
  1516. }
  1517. // IsNotExistError returns true if the specified fs error is not exist for the connection protocol
  1518. func (c *BaseConnection) IsNotExistError(err error) bool {
  1519. switch c.protocol {
  1520. case ProtocolSFTP:
  1521. return errors.Is(err, sftp.ErrSSHFxNoSuchFile)
  1522. case ProtocolWebDAV, ProtocolFTP, ProtocolHTTP, ProtocolOIDC, ProtocolHTTPShare, ProtocolDataRetention:
  1523. return errors.Is(err, os.ErrNotExist)
  1524. default:
  1525. return errors.Is(err, ErrNotExist)
  1526. }
  1527. }
  1528. // GetErrorForDeniedFile return permission denied or not exist error based on the specified policy
  1529. func (c *BaseConnection) GetErrorForDeniedFile(policy int) error {
  1530. switch policy {
  1531. case sdk.DenyPolicyHide:
  1532. return c.GetNotExistError()
  1533. default:
  1534. return c.GetPermissionDeniedError()
  1535. }
  1536. }
  1537. // GetPermissionDeniedError returns an appropriate permission denied error for the connection protocol
  1538. func (c *BaseConnection) GetPermissionDeniedError() error {
  1539. return getPermissionDeniedError(c.protocol)
  1540. }
  1541. // GetNotExistError returns an appropriate not exist error for the connection protocol
  1542. func (c *BaseConnection) GetNotExistError() error {
  1543. switch c.protocol {
  1544. case ProtocolSFTP:
  1545. return sftp.ErrSSHFxNoSuchFile
  1546. case ProtocolWebDAV, ProtocolFTP, ProtocolHTTP, ProtocolOIDC, ProtocolHTTPShare, ProtocolDataRetention:
  1547. return os.ErrNotExist
  1548. default:
  1549. return ErrNotExist
  1550. }
  1551. }
  1552. // GetOpUnsupportedError returns an appropriate operation not supported error for the connection protocol
  1553. func (c *BaseConnection) GetOpUnsupportedError() error {
  1554. switch c.protocol {
  1555. case ProtocolSFTP:
  1556. return sftp.ErrSSHFxOpUnsupported
  1557. default:
  1558. return ErrOpUnsupported
  1559. }
  1560. }
  1561. func getQuotaExceededError(protocol string) error {
  1562. switch protocol {
  1563. case ProtocolSFTP:
  1564. return fmt.Errorf("%w: %v", sftp.ErrSSHFxFailure, ErrQuotaExceeded.Error())
  1565. case ProtocolFTP:
  1566. return ftpserver.ErrStorageExceeded
  1567. default:
  1568. return ErrQuotaExceeded
  1569. }
  1570. }
  1571. func getReadQuotaExceededError(protocol string) error {
  1572. switch protocol {
  1573. case ProtocolSFTP:
  1574. return fmt.Errorf("%w: %v", sftp.ErrSSHFxFailure, ErrReadQuotaExceeded.Error())
  1575. default:
  1576. return ErrReadQuotaExceeded
  1577. }
  1578. }
  1579. // GetQuotaExceededError returns an appropriate storage limit exceeded error for the connection protocol
  1580. func (c *BaseConnection) GetQuotaExceededError() error {
  1581. return getQuotaExceededError(c.protocol)
  1582. }
  1583. // GetReadQuotaExceededError returns an appropriate read quota limit exceeded error for the connection protocol
  1584. func (c *BaseConnection) GetReadQuotaExceededError() error {
  1585. return getReadQuotaExceededError(c.protocol)
  1586. }
  1587. // IsQuotaExceededError returns true if the given error is a quota exceeded error
  1588. func (c *BaseConnection) IsQuotaExceededError(err error) bool {
  1589. switch c.protocol {
  1590. case ProtocolSFTP:
  1591. if err == nil {
  1592. return false
  1593. }
  1594. if errors.Is(err, ErrQuotaExceeded) {
  1595. return true
  1596. }
  1597. return errors.Is(err, sftp.ErrSSHFxFailure) && strings.Contains(err.Error(), ErrQuotaExceeded.Error())
  1598. case ProtocolFTP:
  1599. return errors.Is(err, ftpserver.ErrStorageExceeded) || errors.Is(err, ErrQuotaExceeded)
  1600. default:
  1601. return errors.Is(err, ErrQuotaExceeded)
  1602. }
  1603. }
  1604. // GetGenericError returns an appropriate generic error for the connection protocol
  1605. func (c *BaseConnection) GetGenericError(err error) error {
  1606. switch c.protocol {
  1607. case ProtocolSFTP:
  1608. if err == vfs.ErrStorageSizeUnavailable {
  1609. return fmt.Errorf("%w: %v", sftp.ErrSSHFxOpUnsupported, err.Error())
  1610. }
  1611. if err == ErrShuttingDown {
  1612. return fmt.Errorf("%w: %v", sftp.ErrSSHFxFailure, err.Error())
  1613. }
  1614. if err != nil {
  1615. if e, ok := err.(*os.PathError); ok {
  1616. c.Log(logger.LevelError, "generic path error: %+v", e)
  1617. return fmt.Errorf("%w: %v %v", sftp.ErrSSHFxFailure, e.Op, e.Err.Error())
  1618. }
  1619. c.Log(logger.LevelError, "generic error: %+v", err)
  1620. return fmt.Errorf("%w: %v", sftp.ErrSSHFxFailure, ErrGenericFailure.Error())
  1621. }
  1622. return sftp.ErrSSHFxFailure
  1623. default:
  1624. if errors.Is(err, ErrPermissionDenied) || errors.Is(err, ErrNotExist) || errors.Is(err, ErrOpUnsupported) ||
  1625. errors.Is(err, ErrQuotaExceeded) || errors.Is(err, ErrReadQuotaExceeded) ||
  1626. errors.Is(err, vfs.ErrStorageSizeUnavailable) || errors.Is(err, ErrShuttingDown) {
  1627. return err
  1628. }
  1629. c.Log(logger.LevelError, "generic error: %+v", err)
  1630. return ErrGenericFailure
  1631. }
  1632. }
  1633. // GetFsError converts a filesystem error to a protocol error
  1634. func (c *BaseConnection) GetFsError(fs vfs.Fs, err error) error {
  1635. if fs.IsNotExist(err) {
  1636. return c.GetNotExistError()
  1637. } else if fs.IsPermission(err) {
  1638. return c.GetPermissionDeniedError()
  1639. } else if fs.IsNotSupported(err) {
  1640. return c.GetOpUnsupportedError()
  1641. } else if err != nil {
  1642. return c.GetGenericError(err)
  1643. }
  1644. return nil
  1645. }
  1646. func (c *BaseConnection) getNotificationStatus(err error) int {
  1647. if err == nil {
  1648. return 1
  1649. }
  1650. if c.IsQuotaExceededError(err) {
  1651. return 3
  1652. }
  1653. return 2
  1654. }
  1655. // GetFsAndResolvedPath returns the fs and the fs path matching virtualPath
  1656. func (c *BaseConnection) GetFsAndResolvedPath(virtualPath string) (vfs.Fs, string, error) {
  1657. fs, err := c.User.GetFilesystemForPath(virtualPath, c.ID)
  1658. if err != nil {
  1659. if c.protocol == ProtocolWebDAV && strings.Contains(err.Error(), vfs.ErrSFTPLoop.Error()) {
  1660. // if there is an SFTP loop we return a permission error, for WebDAV, so the problematic folder
  1661. // will not be listed
  1662. return nil, "", util.NewI18nError(c.GetPermissionDeniedError(), util.I18nError403Message)
  1663. }
  1664. return nil, "", c.GetGenericError(err)
  1665. }
  1666. if isShuttingDown.Load() {
  1667. return nil, "", c.GetFsError(fs, ErrShuttingDown)
  1668. }
  1669. fsPath, err := fs.ResolvePath(virtualPath)
  1670. if err != nil {
  1671. return nil, "", c.GetFsError(fs, err)
  1672. }
  1673. return fs, fsPath, nil
  1674. }
  1675. // DirListerAt defines a directory lister implementing the ListAt method.
  1676. type DirListerAt struct {
  1677. virtualPath string
  1678. user *dataprovider.User
  1679. info []os.FileInfo
  1680. id string
  1681. protocol string
  1682. mu sync.Mutex
  1683. lister vfs.DirLister
  1684. }
  1685. // Add adds the given os.FileInfo to the internal cache
  1686. func (l *DirListerAt) Add(fi os.FileInfo) {
  1687. l.mu.Lock()
  1688. defer l.mu.Unlock()
  1689. l.info = append(l.info, fi)
  1690. }
  1691. // ListAt implements sftp.ListerAt
  1692. func (l *DirListerAt) ListAt(f []os.FileInfo, _ int64) (int, error) {
  1693. l.mu.Lock()
  1694. defer l.mu.Unlock()
  1695. if len(f) == 0 {
  1696. return 0, errors.New("invalid ListAt destination, zero size")
  1697. }
  1698. if len(f) <= len(l.info) {
  1699. files := make([]os.FileInfo, 0, len(f))
  1700. for idx := len(l.info) - 1; idx >= 0; idx-- {
  1701. files = append(files, l.info[idx])
  1702. if len(files) == len(f) {
  1703. l.info = l.info[:idx]
  1704. n := copy(f, files)
  1705. return n, nil
  1706. }
  1707. }
  1708. }
  1709. limit := len(f) - len(l.info)
  1710. files, err := l.Next(limit)
  1711. n := copy(f, files)
  1712. return n, err
  1713. }
  1714. // Next reads the directory and returns a slice of up to n FileInfo values.
  1715. func (l *DirListerAt) Next(limit int) ([]os.FileInfo, error) {
  1716. for {
  1717. files, err := l.lister.Next(limit)
  1718. if err != nil && !errors.Is(err, io.EOF) {
  1719. logger.Debug(l.protocol, l.id, "error retrieving directory entries: %+v", err)
  1720. return files, err
  1721. }
  1722. files = l.user.FilterListDir(files, l.virtualPath)
  1723. if len(l.info) > 0 {
  1724. for _, fi := range l.info {
  1725. files = util.PrependFileInfo(files, fi)
  1726. }
  1727. l.info = nil
  1728. }
  1729. if err != nil || len(files) > 0 {
  1730. return files, err
  1731. }
  1732. }
  1733. }
  1734. // Close closes the DirListerAt
  1735. func (l *DirListerAt) Close() error {
  1736. l.mu.Lock()
  1737. defer l.mu.Unlock()
  1738. return l.lister.Close()
  1739. }
  1740. func (l *DirListerAt) convertError(err error) error {
  1741. if errors.Is(err, io.EOF) {
  1742. return nil
  1743. }
  1744. return err
  1745. }
  1746. func getPermissionDeniedError(protocol string) error {
  1747. switch protocol {
  1748. case ProtocolSFTP:
  1749. return sftp.ErrSSHFxPermissionDenied
  1750. case ProtocolWebDAV, ProtocolFTP, ProtocolHTTP, ProtocolOIDC, ProtocolHTTPShare, ProtocolDataRetention:
  1751. return os.ErrPermission
  1752. default:
  1753. return ErrPermissionDenied
  1754. }
  1755. }
  1756. func keepConnectionAlive(c *BaseConnection, done chan bool, interval time.Duration) {
  1757. ticker := time.NewTicker(interval)
  1758. defer func() {
  1759. ticker.Stop()
  1760. }()
  1761. for {
  1762. select {
  1763. case <-done:
  1764. return
  1765. case <-ticker.C:
  1766. c.UpdateLastActivity()
  1767. }
  1768. }
  1769. }