backend.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package smbserver
  2. import (
  3. "net"
  4. "time"
  5. )
  6. /*
  7. backend.go
  8. The small interface the protocol core calls. Only the aroz package
  9. implements it against ArozOS, so the core stays testable by itself, the
  10. same split as sftpserver.Access. Paths are relative to the share root, use
  11. forward slashes, have no leading slash, and "" is the root.
  12. */
  13. // FileInfo describes a file or folder. A zero ID makes the core hash the path
  14. // instead, and a zero BirthTime falls back to ModTime.
  15. type FileInfo struct {
  16. Name string
  17. Size int64
  18. IsDir bool
  19. ModTime time.Time
  20. BirthTime time.Time
  21. ID uint64
  22. }
  23. // File is an open file. Access is always positional, so requests of one
  24. // client may run at the same time.
  25. type File interface {
  26. ReadAt(b []byte, off int64) (int, error)
  27. WriteAt(b []byte, off int64) (int, error)
  28. Truncate(size int64) error
  29. Sync() error
  30. Close() error
  31. }
  32. // PendingFile is a File whose writes reach the drive later, such as a staged
  33. // copy uploaded when it closes. Until then its own handle reports the size
  34. // it has, instead of the one the drive still shows.
  35. type PendingFile interface {
  36. PendingSize() (size int64, ok bool)
  37. }
  38. // Share is one drive as one user sees it. CanRead and CanWrite are asked on
  39. // every operation rather than once at tree connect, so a change of a drive's
  40. // access list reaches sessions that are already open.
  41. type Share interface {
  42. Name() string
  43. Comment() string
  44. //Key identifies the namespace behind the share. Two shares with the same
  45. //key are the same files, so their locks and share modes are one.
  46. Key() string
  47. ReadOnly() bool
  48. CanRead(path string) bool
  49. CanWrite(path string) bool
  50. Stat(path string) (FileInfo, error)
  51. ReadDir(path string) ([]FileInfo, error)
  52. Open(path string, flag int) (File, error)
  53. Mkdir(path string) error
  54. Remove(path string) error
  55. Rename(oldPath, newPath string) error
  56. SetModTime(path string, t time.Time) error
  57. //Usage returns the total and free bytes the user should see
  58. Usage() (total, free uint64)
  59. //HaveSpace reports whether a file may grow by this many bytes
  60. HaveSpace(extra int64) bool
  61. //FileChanged runs when an open that changed a file closes, for quotas
  62. //and ownership. oldSize is -1 for a file this open created.
  63. FileChanged(path string, oldSize, newSize int64)
  64. }
  65. // UserView is what one logged in user can reach. Shares is evaluated each time
  66. // it is called.
  67. type UserView interface {
  68. Username() string
  69. Shares() []Share
  70. }
  71. // Backend connects the server to the accounts of the host.
  72. type Backend interface {
  73. //LookupHash returns the NT hash of an account and its real spelling. The
  74. //name is as the client typed it, so lookups should ignore case.
  75. LookupHash(username string) (name string, ntHash []byte, ok bool)
  76. //Login returns the view of a verified user
  77. Login(username string) (UserView, error)
  78. //Allow is asked for every new connection
  79. Allow(remote net.IP) bool
  80. //AuthLog records a login attempt from an ip:port address. For a failed one
  81. //it may return how long to hold the reply back, and zero leaves that to
  82. //Config.LoginDelay.
  83. AuthLog(username, remote string, ok bool) time.Duration
  84. }