connection.go 48 KB

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