connection.go 64 KB

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