connection.go 65 KB

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