file.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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 webdavd
  15. import (
  16. "context"
  17. "encoding/xml"
  18. "errors"
  19. "io"
  20. "mime"
  21. "net/http"
  22. "os"
  23. "path"
  24. "sync/atomic"
  25. "time"
  26. "github.com/eikenb/pipeat"
  27. "golang.org/x/net/webdav"
  28. "github.com/drakkan/sftpgo/v2/internal/common"
  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. var (
  35. errTransferAborted = errors.New("transfer aborted")
  36. lastModifiedProps = []string{"Win32LastModifiedTime", "getlastmodified"}
  37. )
  38. type webDavFile struct {
  39. *common.BaseTransfer
  40. writer io.WriteCloser
  41. reader io.ReadCloser
  42. info os.FileInfo
  43. startOffset int64
  44. isFinished bool
  45. readTryed atomic.Bool
  46. }
  47. func newWebDavFile(baseTransfer *common.BaseTransfer, pipeWriter *vfs.PipeWriter, pipeReader *pipeat.PipeReaderAt) *webDavFile {
  48. var writer io.WriteCloser
  49. var reader io.ReadCloser
  50. if baseTransfer.File != nil {
  51. writer = baseTransfer.File
  52. reader = baseTransfer.File
  53. } else if pipeWriter != nil {
  54. writer = pipeWriter
  55. } else if pipeReader != nil {
  56. reader = pipeReader
  57. }
  58. f := &webDavFile{
  59. BaseTransfer: baseTransfer,
  60. writer: writer,
  61. reader: reader,
  62. isFinished: false,
  63. startOffset: 0,
  64. info: nil,
  65. }
  66. f.readTryed.Store(false)
  67. return f
  68. }
  69. type webDavFileInfo struct {
  70. os.FileInfo
  71. Fs vfs.Fs
  72. virtualPath string
  73. fsPath string
  74. }
  75. // ContentType implements webdav.ContentTyper interface
  76. func (fi *webDavFileInfo) ContentType(ctx context.Context) (string, error) {
  77. extension := path.Ext(fi.virtualPath)
  78. contentType := mime.TypeByExtension(extension)
  79. if contentType != "" {
  80. return contentType, nil
  81. }
  82. contentType = mimeTypeCache.getMimeFromCache(extension)
  83. if contentType != "" {
  84. return contentType, nil
  85. }
  86. contentType, err := fi.Fs.GetMimeType(fi.fsPath)
  87. if contentType != "" {
  88. mimeTypeCache.addMimeToCache(extension, contentType)
  89. return contentType, err
  90. }
  91. return "", webdav.ErrNotImplemented
  92. }
  93. // Readdir reads directory entries from the handle
  94. func (f *webDavFile) Readdir(count int) ([]os.FileInfo, error) {
  95. if !f.Connection.User.HasPerm(dataprovider.PermListItems, f.GetVirtualPath()) {
  96. return nil, f.Connection.GetPermissionDeniedError()
  97. }
  98. fileInfos, err := f.Connection.ListDir(f.GetVirtualPath())
  99. if err != nil {
  100. return nil, err
  101. }
  102. result := make([]os.FileInfo, 0, len(fileInfos))
  103. for _, fileInfo := range fileInfos {
  104. result = append(result, &webDavFileInfo{
  105. FileInfo: fileInfo,
  106. Fs: f.Fs,
  107. virtualPath: path.Join(f.GetVirtualPath(), fileInfo.Name()),
  108. fsPath: f.Fs.Join(f.GetFsPath(), fileInfo.Name()),
  109. })
  110. }
  111. return result, nil
  112. }
  113. // Stat the handle
  114. func (f *webDavFile) Stat() (os.FileInfo, error) {
  115. if f.GetType() == common.TransferDownload && !f.Connection.User.HasPerm(dataprovider.PermListItems, path.Dir(f.GetVirtualPath())) {
  116. return nil, f.Connection.GetPermissionDeniedError()
  117. }
  118. f.Lock()
  119. errUpload := f.ErrTransfer
  120. f.Unlock()
  121. if f.GetType() == common.TransferUpload && errUpload == nil {
  122. info := &webDavFileInfo{
  123. FileInfo: vfs.NewFileInfo(f.GetFsPath(), false, f.BytesReceived.Load(), time.Unix(0, 0), false),
  124. Fs: f.Fs,
  125. virtualPath: f.GetVirtualPath(),
  126. fsPath: f.GetFsPath(),
  127. }
  128. return info, nil
  129. }
  130. info, err := f.Fs.Stat(f.GetFsPath())
  131. if err != nil {
  132. return nil, err
  133. }
  134. if vfs.IsCryptOsFs(f.Fs) {
  135. info = f.Fs.(*vfs.CryptFs).ConvertFileInfo(info)
  136. }
  137. fi := &webDavFileInfo{
  138. FileInfo: info,
  139. Fs: f.Fs,
  140. virtualPath: f.GetVirtualPath(),
  141. fsPath: f.GetFsPath(),
  142. }
  143. return fi, nil
  144. }
  145. // Read reads the contents to downloads.
  146. func (f *webDavFile) Read(p []byte) (n int, err error) {
  147. if f.AbortTransfer.Load() {
  148. return 0, errTransferAborted
  149. }
  150. if !f.readTryed.Load() {
  151. if !f.Connection.User.HasPerm(dataprovider.PermDownload, path.Dir(f.GetVirtualPath())) {
  152. return 0, f.Connection.GetPermissionDeniedError()
  153. }
  154. transferQuota := f.BaseTransfer.GetTransferQuota()
  155. if !transferQuota.HasDownloadSpace() {
  156. f.Connection.Log(logger.LevelInfo, "denying file read due to quota limits")
  157. return 0, f.Connection.GetReadQuotaExceededError()
  158. }
  159. if ok, policy := f.Connection.User.IsFileAllowed(f.GetVirtualPath()); !ok {
  160. f.Connection.Log(logger.LevelWarn, "reading file %#v is not allowed", f.GetVirtualPath())
  161. return 0, f.Connection.GetErrorForDeniedFile(policy)
  162. }
  163. err := common.ExecutePreAction(f.Connection, common.OperationPreDownload, f.GetFsPath(), f.GetVirtualPath(), 0, 0)
  164. if err != nil {
  165. f.Connection.Log(logger.LevelDebug, "download for file %#v denied by pre action: %v", f.GetVirtualPath(), err)
  166. return 0, f.Connection.GetPermissionDeniedError()
  167. }
  168. f.readTryed.Store(true)
  169. }
  170. f.Connection.UpdateLastActivity()
  171. // the file is read sequentially we don't need to check for concurrent reads and so
  172. // lock the transfer while opening the remote file
  173. if f.reader == nil {
  174. if f.GetType() != common.TransferDownload {
  175. f.TransferError(common.ErrOpUnsupported)
  176. return 0, common.ErrOpUnsupported
  177. }
  178. _, r, cancelFn, e := f.Fs.Open(f.GetFsPath(), 0)
  179. f.Lock()
  180. if e == nil {
  181. f.reader = r
  182. f.BaseTransfer.SetCancelFn(cancelFn)
  183. }
  184. f.ErrTransfer = e
  185. f.startOffset = 0
  186. f.Unlock()
  187. if e != nil {
  188. return 0, e
  189. }
  190. }
  191. n, err = f.reader.Read(p)
  192. f.BytesSent.Add(int64(n))
  193. if err == nil {
  194. err = f.CheckRead()
  195. }
  196. if err != nil && err != io.EOF {
  197. f.TransferError(err)
  198. return
  199. }
  200. f.HandleThrottle()
  201. return
  202. }
  203. // Write writes the uploaded contents.
  204. func (f *webDavFile) Write(p []byte) (n int, err error) {
  205. if f.AbortTransfer.Load() {
  206. return 0, errTransferAborted
  207. }
  208. f.Connection.UpdateLastActivity()
  209. n, err = f.writer.Write(p)
  210. f.BytesReceived.Add(int64(n))
  211. if err == nil {
  212. err = f.CheckWrite()
  213. }
  214. if err != nil {
  215. f.TransferError(err)
  216. return
  217. }
  218. f.HandleThrottle()
  219. return
  220. }
  221. func (f *webDavFile) updateStatInfo() error {
  222. if f.info != nil {
  223. return nil
  224. }
  225. info, err := f.Fs.Stat(f.GetFsPath())
  226. if err != nil {
  227. return err
  228. }
  229. if vfs.IsCryptOsFs(f.Fs) {
  230. info = f.Fs.(*vfs.CryptFs).ConvertFileInfo(info)
  231. }
  232. f.info = info
  233. return nil
  234. }
  235. func (f *webDavFile) updateTransferQuotaOnSeek() {
  236. transferQuota := f.GetTransferQuota()
  237. if transferQuota.HasSizeLimits() {
  238. go func(ulSize, dlSize int64, user dataprovider.User) {
  239. dataprovider.UpdateUserTransferQuota(&user, ulSize, dlSize, false) //nolint:errcheck
  240. }(f.BytesReceived.Load(), f.BytesSent.Load(), f.Connection.User)
  241. }
  242. }
  243. // Seek sets the offset for the next Read or Write on the writer to offset,
  244. // interpreted according to whence: 0 means relative to the origin of the file,
  245. // 1 means relative to the current offset, and 2 means relative to the end.
  246. // It returns the new offset and an error, if any.
  247. func (f *webDavFile) Seek(offset int64, whence int) (int64, error) {
  248. f.Connection.UpdateLastActivity()
  249. if f.File != nil {
  250. ret, err := f.File.Seek(offset, whence)
  251. if err != nil {
  252. f.TransferError(err)
  253. }
  254. return ret, err
  255. }
  256. if f.GetType() == common.TransferDownload {
  257. readOffset := f.startOffset + f.BytesSent.Load()
  258. if offset == 0 && readOffset == 0 {
  259. if whence == io.SeekStart {
  260. return 0, nil
  261. } else if whence == io.SeekEnd {
  262. if err := f.updateStatInfo(); err != nil {
  263. return 0, err
  264. }
  265. return f.info.Size(), nil
  266. }
  267. }
  268. // close the reader and create a new one at startByte
  269. if f.reader != nil {
  270. f.reader.Close() //nolint:errcheck
  271. f.reader = nil
  272. }
  273. startByte := int64(0)
  274. f.BytesReceived.Store(0)
  275. f.BytesSent.Store(0)
  276. f.updateTransferQuotaOnSeek()
  277. switch whence {
  278. case io.SeekStart:
  279. startByte = offset
  280. case io.SeekCurrent:
  281. startByte = readOffset + offset
  282. case io.SeekEnd:
  283. if err := f.updateStatInfo(); err != nil {
  284. f.TransferError(err)
  285. return 0, err
  286. }
  287. startByte = f.info.Size() - offset
  288. }
  289. _, r, cancelFn, err := f.Fs.Open(f.GetFsPath(), startByte)
  290. f.Lock()
  291. if err == nil {
  292. f.startOffset = startByte
  293. f.reader = r
  294. }
  295. f.ErrTransfer = err
  296. f.BaseTransfer.SetCancelFn(cancelFn)
  297. f.Unlock()
  298. return startByte, err
  299. }
  300. return 0, common.ErrOpUnsupported
  301. }
  302. // Close closes the open directory or the current transfer
  303. func (f *webDavFile) Close() error {
  304. if err := f.setFinished(); err != nil {
  305. return err
  306. }
  307. err := f.closeIO()
  308. if f.isTransfer() {
  309. errBaseClose := f.BaseTransfer.Close()
  310. if errBaseClose != nil {
  311. err = errBaseClose
  312. }
  313. } else {
  314. f.Connection.RemoveTransfer(f.BaseTransfer)
  315. }
  316. return f.Connection.GetFsError(f.Fs, err)
  317. }
  318. func (f *webDavFile) closeIO() error {
  319. var err error
  320. if f.File != nil {
  321. err = f.File.Close()
  322. } else if f.writer != nil {
  323. err = f.writer.Close()
  324. f.Lock()
  325. // we set ErrTransfer here so quota is not updated, in this case the uploads are atomic
  326. if err != nil && f.ErrTransfer == nil {
  327. f.ErrTransfer = err
  328. }
  329. f.Unlock()
  330. } else if f.reader != nil {
  331. err = f.reader.Close()
  332. }
  333. return err
  334. }
  335. func (f *webDavFile) setFinished() error {
  336. f.Lock()
  337. defer f.Unlock()
  338. if f.isFinished {
  339. return common.ErrTransferClosed
  340. }
  341. f.isFinished = true
  342. return nil
  343. }
  344. func (f *webDavFile) isTransfer() bool {
  345. if f.GetType() == common.TransferDownload {
  346. return f.readTryed.Load()
  347. }
  348. return true
  349. }
  350. // DeadProps returns a copy of the dead properties held.
  351. // We always return nil for now, we only support the last modification time
  352. // and it is already included in "live" properties
  353. func (f *webDavFile) DeadProps() (map[xml.Name]webdav.Property, error) {
  354. return nil, nil
  355. }
  356. // Patch patches the dead properties held.
  357. // In our minimal implementation we just support Win32LastModifiedTime and
  358. // getlastmodified to set the the modification time.
  359. // We ignore any other property and just return an OK response if the patch sets
  360. // the modification time, otherwise a Forbidden response
  361. func (f *webDavFile) Patch(patches []webdav.Proppatch) ([]webdav.Propstat, error) {
  362. resp := make([]webdav.Propstat, 0, len(patches))
  363. hasError := false
  364. for _, patch := range patches {
  365. status := http.StatusForbidden
  366. pstat := webdav.Propstat{}
  367. for _, p := range patch.Props {
  368. if status == http.StatusForbidden && !hasError {
  369. if !patch.Remove && util.Contains(lastModifiedProps, p.XMLName.Local) {
  370. parsed, err := http.ParseTime(string(p.InnerXML))
  371. if err != nil {
  372. f.Connection.Log(logger.LevelWarn, "unsupported last modification time: %q, err: %v",
  373. string(p.InnerXML), err)
  374. hasError = true
  375. continue
  376. }
  377. attrs := &common.StatAttributes{
  378. Flags: common.StatAttrTimes,
  379. Atime: parsed,
  380. Mtime: parsed,
  381. }
  382. if err := f.Connection.SetStat(f.GetVirtualPath(), attrs); err != nil {
  383. f.Connection.Log(logger.LevelWarn, "unable to set modification time for %q, err :%v",
  384. f.GetVirtualPath(), err)
  385. hasError = true
  386. continue
  387. }
  388. status = http.StatusOK
  389. }
  390. }
  391. pstat.Props = append(pstat.Props, webdav.Property{XMLName: p.XMLName})
  392. }
  393. pstat.Status = status
  394. resp = append(resp, pstat)
  395. }
  396. return resp, nil
  397. }