sftpfs.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  1. package vfs
  2. import (
  3. "bufio"
  4. "errors"
  5. "fmt"
  6. "io"
  7. "io/fs"
  8. "net"
  9. "net/http"
  10. "os"
  11. "path"
  12. "path/filepath"
  13. "strings"
  14. "sync"
  15. "time"
  16. "github.com/eikenb/pipeat"
  17. "github.com/pkg/sftp"
  18. "github.com/rs/xid"
  19. "github.com/sftpgo/sdk"
  20. "golang.org/x/crypto/ssh"
  21. "github.com/drakkan/sftpgo/v2/kms"
  22. "github.com/drakkan/sftpgo/v2/logger"
  23. "github.com/drakkan/sftpgo/v2/util"
  24. "github.com/drakkan/sftpgo/v2/version"
  25. )
  26. const (
  27. // sftpFsName is the name for the SFTP Fs implementation
  28. sftpFsName = "sftpfs"
  29. )
  30. // ErrSFTPLoop defines the error to return if an SFTP loop is detected
  31. var ErrSFTPLoop = errors.New("SFTP loop or nested local SFTP folders detected")
  32. // SFTPFsConfig defines the configuration for SFTP based filesystem
  33. type SFTPFsConfig struct {
  34. sdk.BaseSFTPFsConfig
  35. Password *kms.Secret `json:"password,omitempty"`
  36. PrivateKey *kms.Secret `json:"private_key,omitempty"`
  37. KeyPassphrase *kms.Secret `json:"key_passphrase,omitempty"`
  38. forbiddenSelfUsernames []string `json:"-"`
  39. }
  40. // HideConfidentialData hides confidential data
  41. func (c *SFTPFsConfig) HideConfidentialData() {
  42. if c.Password != nil {
  43. c.Password.Hide()
  44. }
  45. if c.PrivateKey != nil {
  46. c.PrivateKey.Hide()
  47. }
  48. if c.KeyPassphrase != nil {
  49. c.KeyPassphrase.Hide()
  50. }
  51. }
  52. func (c *SFTPFsConfig) setNilSecretsIfEmpty() {
  53. if c.Password != nil && c.Password.IsEmpty() {
  54. c.Password = nil
  55. }
  56. if c.PrivateKey != nil && c.PrivateKey.IsEmpty() {
  57. c.PrivateKey = nil
  58. }
  59. if c.KeyPassphrase != nil && c.KeyPassphrase.IsEmpty() {
  60. c.KeyPassphrase = nil
  61. }
  62. }
  63. func (c *SFTPFsConfig) isEqual(other *SFTPFsConfig) bool {
  64. if c.Endpoint != other.Endpoint {
  65. return false
  66. }
  67. if c.Username != other.Username {
  68. return false
  69. }
  70. if c.Prefix != other.Prefix {
  71. return false
  72. }
  73. if c.DisableCouncurrentReads != other.DisableCouncurrentReads {
  74. return false
  75. }
  76. if c.BufferSize != other.BufferSize {
  77. return false
  78. }
  79. if len(c.Fingerprints) != len(other.Fingerprints) {
  80. return false
  81. }
  82. for _, fp := range c.Fingerprints {
  83. if !util.Contains(other.Fingerprints, fp) {
  84. return false
  85. }
  86. }
  87. c.setEmptyCredentialsIfNil()
  88. other.setEmptyCredentialsIfNil()
  89. if !c.Password.IsEqual(other.Password) {
  90. return false
  91. }
  92. if !c.KeyPassphrase.IsEqual(other.KeyPassphrase) {
  93. return false
  94. }
  95. return c.PrivateKey.IsEqual(other.PrivateKey)
  96. }
  97. func (c *SFTPFsConfig) setEmptyCredentialsIfNil() {
  98. if c.Password == nil {
  99. c.Password = kms.NewEmptySecret()
  100. }
  101. if c.PrivateKey == nil {
  102. c.PrivateKey = kms.NewEmptySecret()
  103. }
  104. if c.KeyPassphrase == nil {
  105. c.KeyPassphrase = kms.NewEmptySecret()
  106. }
  107. }
  108. // validate returns an error if the configuration is not valid
  109. func (c *SFTPFsConfig) validate() error {
  110. c.setEmptyCredentialsIfNil()
  111. if c.Endpoint == "" {
  112. return errors.New("endpoint cannot be empty")
  113. }
  114. _, _, err := net.SplitHostPort(c.Endpoint)
  115. if err != nil {
  116. return fmt.Errorf("invalid endpoint: %v", err)
  117. }
  118. if c.Username == "" {
  119. return errors.New("username cannot be empty")
  120. }
  121. if c.BufferSize < 0 || c.BufferSize > 16 {
  122. return errors.New("invalid buffer_size, valid range is 0-16")
  123. }
  124. if err := c.validateCredentials(); err != nil {
  125. return err
  126. }
  127. if c.Prefix != "" {
  128. c.Prefix = util.CleanPath(c.Prefix)
  129. } else {
  130. c.Prefix = "/"
  131. }
  132. return nil
  133. }
  134. func (c *SFTPFsConfig) validateCredentials() error {
  135. if c.Password.IsEmpty() && c.PrivateKey.IsEmpty() {
  136. return errors.New("credentials cannot be empty")
  137. }
  138. if c.Password.IsEncrypted() && !c.Password.IsValid() {
  139. return errors.New("invalid encrypted password")
  140. }
  141. if !c.Password.IsEmpty() && !c.Password.IsValidInput() {
  142. return errors.New("invalid password")
  143. }
  144. if c.PrivateKey.IsEncrypted() && !c.PrivateKey.IsValid() {
  145. return errors.New("invalid encrypted private key")
  146. }
  147. if !c.PrivateKey.IsEmpty() && !c.PrivateKey.IsValidInput() {
  148. return errors.New("invalid private key")
  149. }
  150. if c.KeyPassphrase.IsEncrypted() && !c.KeyPassphrase.IsValid() {
  151. return errors.New("invalid encrypted private key passphrase")
  152. }
  153. if !c.KeyPassphrase.IsEmpty() && !c.KeyPassphrase.IsValidInput() {
  154. return errors.New("invalid private key passphrase")
  155. }
  156. return nil
  157. }
  158. // ValidateAndEncryptCredentials encrypts password and/or private key if they are in plain text
  159. func (c *SFTPFsConfig) ValidateAndEncryptCredentials(additionalData string) error {
  160. if err := c.validate(); err != nil {
  161. return util.NewValidationError(fmt.Sprintf("could not validate SFTP fs config: %v", err))
  162. }
  163. if c.Password.IsPlain() {
  164. c.Password.SetAdditionalData(additionalData)
  165. if err := c.Password.Encrypt(); err != nil {
  166. return util.NewValidationError(fmt.Sprintf("could not encrypt SFTP fs password: %v", err))
  167. }
  168. }
  169. if c.PrivateKey.IsPlain() {
  170. c.PrivateKey.SetAdditionalData(additionalData)
  171. if err := c.PrivateKey.Encrypt(); err != nil {
  172. return util.NewValidationError(fmt.Sprintf("could not encrypt SFTP fs private key: %v", err))
  173. }
  174. }
  175. if c.KeyPassphrase.IsPlain() {
  176. c.KeyPassphrase.SetAdditionalData(additionalData)
  177. if err := c.KeyPassphrase.Encrypt(); err != nil {
  178. return util.NewValidationError(fmt.Sprintf("could not encrypt SFTP fs private key passphrase: %v", err))
  179. }
  180. }
  181. return nil
  182. }
  183. // SFTPFs is a Fs implementation for SFTP backends
  184. type SFTPFs struct {
  185. sync.Mutex
  186. connectionID string
  187. // if not empty this fs is mouted as virtual folder in the specified path
  188. mountPath string
  189. localTempDir string
  190. config *SFTPFsConfig
  191. sshClient *ssh.Client
  192. sftpClient *sftp.Client
  193. err chan error
  194. }
  195. // NewSFTPFs returns an SFTPFs object that allows to interact with an SFTP server
  196. func NewSFTPFs(connectionID, mountPath, localTempDir string, forbiddenSelfUsernames []string, config SFTPFsConfig) (Fs, error) {
  197. if localTempDir == "" {
  198. if tempPath != "" {
  199. localTempDir = tempPath
  200. } else {
  201. localTempDir = filepath.Clean(os.TempDir())
  202. }
  203. }
  204. if err := config.validate(); err != nil {
  205. return nil, err
  206. }
  207. if !config.Password.IsEmpty() {
  208. if err := config.Password.TryDecrypt(); err != nil {
  209. return nil, err
  210. }
  211. }
  212. if !config.PrivateKey.IsEmpty() {
  213. if err := config.PrivateKey.TryDecrypt(); err != nil {
  214. return nil, err
  215. }
  216. }
  217. if !config.KeyPassphrase.IsEmpty() {
  218. if err := config.KeyPassphrase.TryDecrypt(); err != nil {
  219. return nil, err
  220. }
  221. }
  222. config.forbiddenSelfUsernames = forbiddenSelfUsernames
  223. sftpFs := &SFTPFs{
  224. connectionID: connectionID,
  225. mountPath: getMountPath(mountPath),
  226. localTempDir: localTempDir,
  227. config: &config,
  228. err: make(chan error, 1),
  229. }
  230. err := sftpFs.createConnection()
  231. return sftpFs, err
  232. }
  233. // Name returns the name for the Fs implementation
  234. func (fs *SFTPFs) Name() string {
  235. return fmt.Sprintf("%v %#v", sftpFsName, fs.config.Endpoint)
  236. }
  237. // ConnectionID returns the connection ID associated to this Fs implementation
  238. func (fs *SFTPFs) ConnectionID() string {
  239. return fs.connectionID
  240. }
  241. // Stat returns a FileInfo describing the named file
  242. func (fs *SFTPFs) Stat(name string) (os.FileInfo, error) {
  243. if err := fs.checkConnection(); err != nil {
  244. return nil, err
  245. }
  246. return fs.sftpClient.Stat(name)
  247. }
  248. // Lstat returns a FileInfo describing the named file
  249. func (fs *SFTPFs) Lstat(name string) (os.FileInfo, error) {
  250. if err := fs.checkConnection(); err != nil {
  251. return nil, err
  252. }
  253. return fs.sftpClient.Lstat(name)
  254. }
  255. // Open opens the named file for reading
  256. func (fs *SFTPFs) Open(name string, offset int64) (File, *pipeat.PipeReaderAt, func(), error) {
  257. if err := fs.checkConnection(); err != nil {
  258. return nil, nil, nil, err
  259. }
  260. f, err := fs.sftpClient.Open(name)
  261. if err != nil {
  262. return nil, nil, nil, err
  263. }
  264. if fs.config.BufferSize == 0 {
  265. return f, nil, nil, err
  266. }
  267. if offset > 0 {
  268. _, err = f.Seek(offset, io.SeekStart)
  269. if err != nil {
  270. f.Close()
  271. return nil, nil, nil, err
  272. }
  273. }
  274. r, w, err := pipeat.PipeInDir(fs.localTempDir)
  275. if err != nil {
  276. f.Close()
  277. return nil, nil, nil, err
  278. }
  279. go func() {
  280. // if we enable buffering the client stalls
  281. //br := bufio.NewReaderSize(f, int(fs.config.BufferSize)*1024*1024)
  282. //n, err := fs.copy(w, br)
  283. n, err := io.Copy(w, f)
  284. w.CloseWithError(err) //nolint:errcheck
  285. f.Close()
  286. fsLog(fs, logger.LevelDebug, "download completed, path: %#v size: %v, err: %v", name, n, err)
  287. }()
  288. return nil, r, nil, nil
  289. }
  290. // Create creates or opens the named file for writing
  291. func (fs *SFTPFs) Create(name string, flag int) (File, *PipeWriter, func(), error) {
  292. err := fs.checkConnection()
  293. if err != nil {
  294. return nil, nil, nil, err
  295. }
  296. if fs.config.BufferSize == 0 {
  297. var f File
  298. if flag == 0 {
  299. f, err = fs.sftpClient.Create(name)
  300. } else {
  301. f, err = fs.sftpClient.OpenFile(name, flag)
  302. }
  303. return f, nil, nil, err
  304. }
  305. // buffering is enabled
  306. f, err := fs.sftpClient.OpenFile(name, os.O_WRONLY|os.O_CREATE|os.O_TRUNC)
  307. if err != nil {
  308. return nil, nil, nil, err
  309. }
  310. r, w, err := pipeat.PipeInDir(fs.localTempDir)
  311. if err != nil {
  312. f.Close()
  313. return nil, nil, nil, err
  314. }
  315. p := NewPipeWriter(w)
  316. go func() {
  317. bw := bufio.NewWriterSize(f, int(fs.config.BufferSize)*1024*1024)
  318. // we don't use io.Copy since bufio.Writer implements io.WriterTo and
  319. // so it calls the sftp.File WriteTo method without buffering
  320. n, err := fs.copy(bw, r)
  321. errFlush := bw.Flush()
  322. if err == nil && errFlush != nil {
  323. err = errFlush
  324. }
  325. var errTruncate error
  326. if err != nil {
  327. errTruncate = f.Truncate(n)
  328. }
  329. errClose := f.Close()
  330. if err == nil && errClose != nil {
  331. err = errClose
  332. }
  333. r.CloseWithError(err) //nolint:errcheck
  334. p.Done(err)
  335. fsLog(fs, logger.LevelDebug, "upload completed, path: %#v, readed bytes: %v, err: %v err truncate: %v",
  336. name, n, err, errTruncate)
  337. }()
  338. return nil, p, nil, nil
  339. }
  340. // Rename renames (moves) source to target.
  341. func (fs *SFTPFs) Rename(source, target string) error {
  342. if err := fs.checkConnection(); err != nil {
  343. return err
  344. }
  345. if _, ok := fs.sftpClient.HasExtension("[email protected]"); ok {
  346. return fs.sftpClient.PosixRename(source, target)
  347. }
  348. return fs.sftpClient.Rename(source, target)
  349. }
  350. // Remove removes the named file or (empty) directory.
  351. func (fs *SFTPFs) Remove(name string, isDir bool) error {
  352. if err := fs.checkConnection(); err != nil {
  353. return err
  354. }
  355. if isDir {
  356. return fs.sftpClient.RemoveDirectory(name)
  357. }
  358. return fs.sftpClient.Remove(name)
  359. }
  360. // Mkdir creates a new directory with the specified name and default permissions
  361. func (fs *SFTPFs) Mkdir(name string) error {
  362. if err := fs.checkConnection(); err != nil {
  363. return err
  364. }
  365. return fs.sftpClient.Mkdir(name)
  366. }
  367. // Symlink creates source as a symbolic link to target.
  368. func (fs *SFTPFs) Symlink(source, target string) error {
  369. if err := fs.checkConnection(); err != nil {
  370. return err
  371. }
  372. return fs.sftpClient.Symlink(source, target)
  373. }
  374. // Readlink returns the destination of the named symbolic link
  375. func (fs *SFTPFs) Readlink(name string) (string, error) {
  376. if err := fs.checkConnection(); err != nil {
  377. return "", err
  378. }
  379. resolved, err := fs.sftpClient.ReadLink(name)
  380. if err != nil {
  381. return resolved, err
  382. }
  383. resolved = path.Clean(resolved)
  384. if !path.IsAbs(resolved) {
  385. // we assume that multiple links are not followed
  386. resolved = path.Join(path.Dir(name), resolved)
  387. }
  388. return fs.GetRelativePath(resolved), nil
  389. }
  390. // Chown changes the numeric uid and gid of the named file.
  391. func (fs *SFTPFs) Chown(name string, uid int, gid int) error {
  392. if err := fs.checkConnection(); err != nil {
  393. return err
  394. }
  395. return fs.sftpClient.Chown(name, uid, gid)
  396. }
  397. // Chmod changes the mode of the named file to mode.
  398. func (fs *SFTPFs) Chmod(name string, mode os.FileMode) error {
  399. if err := fs.checkConnection(); err != nil {
  400. return err
  401. }
  402. return fs.sftpClient.Chmod(name, mode)
  403. }
  404. // Chtimes changes the access and modification times of the named file.
  405. func (fs *SFTPFs) Chtimes(name string, atime, mtime time.Time, isUploading bool) error {
  406. if err := fs.checkConnection(); err != nil {
  407. return err
  408. }
  409. return fs.sftpClient.Chtimes(name, atime, mtime)
  410. }
  411. // Truncate changes the size of the named file.
  412. func (fs *SFTPFs) Truncate(name string, size int64) error {
  413. if err := fs.checkConnection(); err != nil {
  414. return err
  415. }
  416. return fs.sftpClient.Truncate(name, size)
  417. }
  418. // ReadDir reads the directory named by dirname and returns
  419. // a list of directory entries.
  420. func (fs *SFTPFs) ReadDir(dirname string) ([]os.FileInfo, error) {
  421. if err := fs.checkConnection(); err != nil {
  422. return nil, err
  423. }
  424. return fs.sftpClient.ReadDir(dirname)
  425. }
  426. // IsUploadResumeSupported returns true if resuming uploads is supported.
  427. func (fs *SFTPFs) IsUploadResumeSupported() bool {
  428. return fs.config.BufferSize == 0
  429. }
  430. // IsAtomicUploadSupported returns true if atomic upload is supported.
  431. func (fs *SFTPFs) IsAtomicUploadSupported() bool {
  432. return fs.config.BufferSize == 0
  433. }
  434. // IsNotExist returns a boolean indicating whether the error is known to
  435. // report that a file or directory does not exist
  436. func (*SFTPFs) IsNotExist(err error) bool {
  437. return errors.Is(err, fs.ErrNotExist)
  438. }
  439. // IsPermission returns a boolean indicating whether the error is known to
  440. // report that permission is denied.
  441. func (*SFTPFs) IsPermission(err error) bool {
  442. if _, ok := err.(*pathResolutionError); ok {
  443. return true
  444. }
  445. return errors.Is(err, fs.ErrPermission)
  446. }
  447. // IsNotSupported returns true if the error indicate an unsupported operation
  448. func (*SFTPFs) IsNotSupported(err error) bool {
  449. if err == nil {
  450. return false
  451. }
  452. return err == ErrVfsUnsupported
  453. }
  454. // CheckRootPath creates the specified local root directory if it does not exists
  455. func (fs *SFTPFs) CheckRootPath(username string, uid int, gid int) bool {
  456. if fs.config.BufferSize > 0 {
  457. // we need a local directory for temporary files
  458. osFs := NewOsFs(fs.ConnectionID(), fs.localTempDir, "")
  459. osFs.CheckRootPath(username, uid, gid)
  460. }
  461. if fs.config.Prefix == "/" {
  462. return true
  463. }
  464. if err := fs.checkConnection(); err != nil {
  465. return false
  466. }
  467. if err := fs.sftpClient.MkdirAll(fs.config.Prefix); err != nil {
  468. fsLog(fs, logger.LevelDebug, "error creating root directory %#v for user %#v: %v", fs.config.Prefix, username, err)
  469. return false
  470. }
  471. return true
  472. }
  473. // ScanRootDirContents returns the number of files contained in a directory and
  474. // their size
  475. func (fs *SFTPFs) ScanRootDirContents() (int, int64, error) {
  476. return fs.GetDirSize(fs.config.Prefix)
  477. }
  478. // CheckMetadata checks the metadata consistency
  479. func (*SFTPFs) CheckMetadata() error {
  480. return nil
  481. }
  482. // GetAtomicUploadPath returns the path to use for an atomic upload
  483. func (*SFTPFs) GetAtomicUploadPath(name string) string {
  484. dir := path.Dir(name)
  485. guid := xid.New().String()
  486. return path.Join(dir, ".sftpgo-upload."+guid+"."+path.Base(name))
  487. }
  488. // GetRelativePath returns the path for a file relative to the sftp prefix if any.
  489. // This is the path as seen by SFTPGo users
  490. func (fs *SFTPFs) GetRelativePath(name string) string {
  491. rel := path.Clean(name)
  492. if rel == "." {
  493. rel = ""
  494. }
  495. if !path.IsAbs(rel) {
  496. return "/" + rel
  497. }
  498. if fs.config.Prefix != "/" {
  499. if !strings.HasPrefix(rel, fs.config.Prefix) {
  500. rel = "/"
  501. }
  502. rel = path.Clean("/" + strings.TrimPrefix(rel, fs.config.Prefix))
  503. }
  504. if fs.mountPath != "" {
  505. rel = path.Join(fs.mountPath, rel)
  506. }
  507. return rel
  508. }
  509. // Walk walks the file tree rooted at root, calling walkFn for each file or
  510. // directory in the tree, including root
  511. func (fs *SFTPFs) Walk(root string, walkFn filepath.WalkFunc) error {
  512. if err := fs.checkConnection(); err != nil {
  513. return err
  514. }
  515. walker := fs.sftpClient.Walk(root)
  516. for walker.Step() {
  517. err := walker.Err()
  518. if err != nil {
  519. return err
  520. }
  521. err = walkFn(walker.Path(), walker.Stat(), err)
  522. if err != nil {
  523. return err
  524. }
  525. }
  526. return nil
  527. }
  528. // Join joins any number of path elements into a single path
  529. func (*SFTPFs) Join(elem ...string) string {
  530. return path.Join(elem...)
  531. }
  532. // HasVirtualFolders returns true if folders are emulated
  533. func (*SFTPFs) HasVirtualFolders() bool {
  534. return false
  535. }
  536. // ResolvePath returns the matching filesystem path for the specified virtual path
  537. func (fs *SFTPFs) ResolvePath(virtualPath string) (string, error) {
  538. if fs.mountPath != "" {
  539. virtualPath = strings.TrimPrefix(virtualPath, fs.mountPath)
  540. }
  541. if !path.IsAbs(virtualPath) {
  542. virtualPath = path.Clean("/" + virtualPath)
  543. }
  544. fsPath := fs.Join(fs.config.Prefix, virtualPath)
  545. if fs.config.Prefix != "/" && fsPath != "/" {
  546. // we need to check if this path is a symlink outside the given prefix
  547. // or a file/dir inside a dir symlinked outside the prefix
  548. if err := fs.checkConnection(); err != nil {
  549. return "", err
  550. }
  551. var validatedPath string
  552. var err error
  553. validatedPath, err = fs.getRealPath(fsPath)
  554. isNotExist := fs.IsNotExist(err)
  555. if err != nil && !isNotExist {
  556. fsLog(fs, logger.LevelError, "Invalid path resolution, original path %v resolved %#v err: %v",
  557. virtualPath, fsPath, err)
  558. return "", err
  559. } else if isNotExist {
  560. for fs.IsNotExist(err) {
  561. validatedPath = path.Dir(validatedPath)
  562. if validatedPath == "/" {
  563. err = nil
  564. break
  565. }
  566. validatedPath, err = fs.getRealPath(validatedPath)
  567. }
  568. if err != nil {
  569. fsLog(fs, logger.LevelError, "Invalid path resolution, dir %#v original path %#v resolved %#v err: %v",
  570. validatedPath, virtualPath, fsPath, err)
  571. return "", err
  572. }
  573. }
  574. if err := fs.isSubDir(validatedPath); err != nil {
  575. fsLog(fs, logger.LevelError, "Invalid path resolution, dir %#v original path %#v resolved %#v err: %v",
  576. validatedPath, virtualPath, fsPath, err)
  577. return "", err
  578. }
  579. }
  580. return fsPath, nil
  581. }
  582. // getRealPath returns the real remote path trying to resolve symbolic links if any
  583. func (fs *SFTPFs) getRealPath(name string) (string, error) {
  584. linksWalked := 0
  585. for {
  586. info, err := fs.sftpClient.Lstat(name)
  587. if err != nil {
  588. return name, err
  589. }
  590. if info.Mode()&os.ModeSymlink == 0 {
  591. return name, nil
  592. }
  593. resolvedLink, err := fs.sftpClient.ReadLink(name)
  594. if err != nil {
  595. return name, err
  596. }
  597. resolvedLink = path.Clean(resolvedLink)
  598. if path.IsAbs(resolvedLink) {
  599. name = resolvedLink
  600. } else {
  601. name = path.Join(path.Dir(name), resolvedLink)
  602. }
  603. linksWalked++
  604. if linksWalked > 10 {
  605. return "", &pathResolutionError{err: "too many links"}
  606. }
  607. }
  608. }
  609. func (fs *SFTPFs) isSubDir(name string) error {
  610. if name == fs.config.Prefix {
  611. return nil
  612. }
  613. if len(name) < len(fs.config.Prefix) {
  614. err := fmt.Errorf("path %#v is not inside: %#v", name, fs.config.Prefix)
  615. return &pathResolutionError{err: err.Error()}
  616. }
  617. if !strings.HasPrefix(name, fs.config.Prefix+"/") {
  618. err := fmt.Errorf("path %#v is not inside: %#v", name, fs.config.Prefix)
  619. return &pathResolutionError{err: err.Error()}
  620. }
  621. return nil
  622. }
  623. // GetDirSize returns the number of files and the size for a folder
  624. // including any subfolders
  625. func (fs *SFTPFs) GetDirSize(dirname string) (int, int64, error) {
  626. numFiles := 0
  627. size := int64(0)
  628. if err := fs.checkConnection(); err != nil {
  629. return numFiles, size, err
  630. }
  631. isDir, err := IsDirectory(fs, dirname)
  632. if err == nil && isDir {
  633. walker := fs.sftpClient.Walk(dirname)
  634. for walker.Step() {
  635. err := walker.Err()
  636. if err != nil {
  637. return numFiles, size, err
  638. }
  639. if walker.Stat().Mode().IsRegular() {
  640. size += walker.Stat().Size()
  641. numFiles++
  642. }
  643. }
  644. }
  645. return numFiles, size, err
  646. }
  647. // GetMimeType returns the content type
  648. func (fs *SFTPFs) GetMimeType(name string) (string, error) {
  649. if err := fs.checkConnection(); err != nil {
  650. return "", err
  651. }
  652. f, err := fs.sftpClient.OpenFile(name, os.O_RDONLY)
  653. if err != nil {
  654. return "", err
  655. }
  656. defer f.Close()
  657. var buf [512]byte
  658. n, err := io.ReadFull(f, buf[:])
  659. if err != nil && err != io.EOF && err != io.ErrUnexpectedEOF {
  660. return "", err
  661. }
  662. ctype := http.DetectContentType(buf[:n])
  663. // Rewind file.
  664. _, err = f.Seek(0, io.SeekStart)
  665. return ctype, err
  666. }
  667. // GetAvailableDiskSize returns the available size for the specified path
  668. func (fs *SFTPFs) GetAvailableDiskSize(dirName string) (*sftp.StatVFS, error) {
  669. if err := fs.checkConnection(); err != nil {
  670. return nil, err
  671. }
  672. if _, ok := fs.sftpClient.HasExtension("[email protected]"); !ok {
  673. return nil, ErrStorageSizeUnavailable
  674. }
  675. return fs.sftpClient.StatVFS(dirName)
  676. }
  677. // Close the connection
  678. func (fs *SFTPFs) Close() error {
  679. fs.Lock()
  680. defer fs.Unlock()
  681. var sftpErr, sshErr error
  682. if fs.sftpClient != nil {
  683. sftpErr = fs.sftpClient.Close()
  684. }
  685. if fs.sshClient != nil {
  686. sshErr = fs.sshClient.Close()
  687. }
  688. if sftpErr != nil {
  689. return sftpErr
  690. }
  691. return sshErr
  692. }
  693. func (fs *SFTPFs) copy(dst io.Writer, src io.Reader) (written int64, err error) {
  694. buf := make([]byte, 32768)
  695. for {
  696. nr, er := src.Read(buf)
  697. if nr > 0 {
  698. nw, ew := dst.Write(buf[0:nr])
  699. if nw < 0 || nr < nw {
  700. nw = 0
  701. if ew == nil {
  702. ew = errors.New("invalid write")
  703. }
  704. }
  705. written += int64(nw)
  706. if ew != nil {
  707. err = ew
  708. break
  709. }
  710. if nr != nw {
  711. err = io.ErrShortWrite
  712. break
  713. }
  714. }
  715. if er != nil {
  716. if er != io.EOF {
  717. err = er
  718. }
  719. break
  720. }
  721. }
  722. return written, err
  723. }
  724. func (fs *SFTPFs) checkConnection() error {
  725. err := fs.closed()
  726. if err == nil {
  727. return nil
  728. }
  729. return fs.createConnection()
  730. }
  731. func (fs *SFTPFs) createConnection() error {
  732. fs.Lock()
  733. defer fs.Unlock()
  734. var err error
  735. clientConfig := &ssh.ClientConfig{
  736. User: fs.config.Username,
  737. HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
  738. fp := ssh.FingerprintSHA256(key)
  739. if util.Contains(sftpFingerprints, fp) {
  740. if util.Contains(fs.config.forbiddenSelfUsernames, fs.config.Username) {
  741. fsLog(fs, logger.LevelError, "SFTP loop or nested local SFTP folders detected, mount path %#v, username %#v, forbidden usernames: %+v",
  742. fs.mountPath, fs.config.Username, fs.config.forbiddenSelfUsernames)
  743. return ErrSFTPLoop
  744. }
  745. }
  746. if len(fs.config.Fingerprints) > 0 {
  747. for _, provided := range fs.config.Fingerprints {
  748. if provided == fp {
  749. return nil
  750. }
  751. }
  752. return fmt.Errorf("invalid fingerprint %#v", fp)
  753. }
  754. fsLog(fs, logger.LevelWarn, "login without host key validation, please provide at least a fingerprint!")
  755. return nil
  756. },
  757. Timeout: 10 * time.Second,
  758. ClientVersion: fmt.Sprintf("SSH-2.0-SFTPGo_%v", version.Get().Version),
  759. }
  760. if fs.config.PrivateKey.GetPayload() != "" {
  761. var signer ssh.Signer
  762. if fs.config.KeyPassphrase.GetPayload() != "" {
  763. signer, err = ssh.ParsePrivateKeyWithPassphrase([]byte(fs.config.PrivateKey.GetPayload()),
  764. []byte(fs.config.KeyPassphrase.GetPayload()))
  765. } else {
  766. signer, err = ssh.ParsePrivateKey([]byte(fs.config.PrivateKey.GetPayload()))
  767. }
  768. if err != nil {
  769. fs.err <- err
  770. return fmt.Errorf("sftpfs: unable to parse the private key: %w", err)
  771. }
  772. clientConfig.Auth = append(clientConfig.Auth, ssh.PublicKeys(signer))
  773. }
  774. if fs.config.Password.GetPayload() != "" {
  775. clientConfig.Auth = append(clientConfig.Auth, ssh.Password(fs.config.Password.GetPayload()))
  776. }
  777. // add more ciphers, KEXs and MACs, they are negotiated according to the order
  778. clientConfig.Ciphers = []string{"[email protected]", "[email protected]", "[email protected]",
  779. "aes128-ctr", "aes192-ctr", "aes256-ctr", "aes128-cbc", "aes192-cbc", "aes256-cbc"}
  780. clientConfig.KeyExchanges = []string{"curve25519-sha256", "[email protected]",
  781. "ecdh-sha2-nistp256", "ecdh-sha2-nistp384", "ecdh-sha2-nistp521",
  782. "diffie-hellman-group14-sha256", "diffie-hellman-group16-sha512", "diffie-hellman-group18-sha512",
  783. "diffie-hellman-group-exchange-sha256", "diffie-hellman-group-exchange-sha1",
  784. "diffie-hellman-group14-sha1", "diffie-hellman-group1-sha1"}
  785. clientConfig.MACs = []string{"[email protected]", "hmac-sha2-256",
  786. "[email protected]", "hmac-sha2-512",
  787. "hmac-sha1", "hmac-sha1-96"}
  788. fs.sshClient, err = ssh.Dial("tcp", fs.config.Endpoint, clientConfig)
  789. if err != nil {
  790. fsLog(fs, logger.LevelError, "unable to connect: %v", err)
  791. fs.err <- err
  792. return err
  793. }
  794. fs.sftpClient, err = sftp.NewClient(fs.sshClient)
  795. if err != nil {
  796. fsLog(fs, logger.LevelError, "unable to create SFTP client: %v", err)
  797. fs.sshClient.Close()
  798. fs.err <- err
  799. return err
  800. }
  801. if fs.config.DisableCouncurrentReads {
  802. fsLog(fs, logger.LevelDebug, "disabling concurrent reads")
  803. opt := sftp.UseConcurrentReads(false)
  804. opt(fs.sftpClient) //nolint:errcheck
  805. }
  806. if fs.config.BufferSize > 0 {
  807. fsLog(fs, logger.LevelDebug, "enabling concurrent writes")
  808. opt := sftp.UseConcurrentWrites(true)
  809. opt(fs.sftpClient) //nolint:errcheck
  810. }
  811. go fs.wait()
  812. return nil
  813. }
  814. func (fs *SFTPFs) wait() {
  815. // we wait on the sftp client otherwise if the channel is closed but not the connection
  816. // we don't detect the event.
  817. fs.err <- fs.sftpClient.Wait()
  818. fsLog(fs, logger.LevelDebug, "sftp channel closed")
  819. fs.Lock()
  820. defer fs.Unlock()
  821. if fs.sshClient != nil {
  822. fs.sshClient.Close()
  823. }
  824. }
  825. func (fs *SFTPFs) closed() error {
  826. select {
  827. case err := <-fs.err:
  828. return err
  829. default:
  830. return nil
  831. }
  832. }