aofs.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. package ftp
  2. //arozos virtual path translation handler
  3. //author: tobychui
  4. import (
  5. "errors"
  6. "os"
  7. "path/filepath"
  8. "strings"
  9. "time"
  10. "github.com/spf13/afero"
  11. "imuslab.com/arozos/mod/filesystem"
  12. "imuslab.com/arozos/mod/user"
  13. )
  14. var (
  15. aofsCanRead = 1
  16. aofsCanWrite = 2
  17. )
  18. type aofs struct {
  19. userinfo *user.User
  20. tmpFolder string
  21. }
  22. func (a aofs) Create(name string) (afero.File, error) {
  23. fsh, rewritePath, err := a.pathRewrite(name)
  24. if err != nil {
  25. return nil, err
  26. }
  27. if !a.checkAllowAccess(fsh, rewritePath, aofsCanWrite) {
  28. return nil, errors.New("Permission denied")
  29. }
  30. return fsh.FileSystemAbstraction.Create(rewritePath)
  31. }
  32. func (a aofs) Chown(name string, uid, gid int) error {
  33. fsh, rewritePath, err := a.pathRewrite(name)
  34. if err != nil {
  35. return err
  36. }
  37. if !a.checkAllowAccess(fsh, rewritePath, aofsCanWrite) {
  38. return errors.New("Permission denied")
  39. }
  40. return fsh.FileSystemAbstraction.Chown(rewritePath, uid, gid)
  41. }
  42. func (a aofs) Mkdir(name string, perm os.FileMode) error {
  43. fsh, rewritePath, err := a.pathRewrite(name)
  44. if err != nil {
  45. return err
  46. }
  47. if !a.checkAllowAccess(fsh, rewritePath, aofsCanWrite) {
  48. return errors.New("Permission denied")
  49. }
  50. return fsh.FileSystemAbstraction.Mkdir(rewritePath, perm)
  51. }
  52. func (a aofs) MkdirAll(path string, perm os.FileMode) error {
  53. fsh, rewritePath, err := a.pathRewrite(path)
  54. if err != nil {
  55. return err
  56. }
  57. if !a.checkAllowAccess(fsh, rewritePath, aofsCanWrite) {
  58. return errors.New("Permission denied")
  59. }
  60. return fsh.FileSystemAbstraction.MkdirAll(rewritePath, perm)
  61. }
  62. func (a aofs) Open(name string) (afero.File, error) {
  63. //fmt.Println("FTP OPEN")
  64. fsh, rewritePath, err := a.pathRewrite(name)
  65. if err != nil {
  66. return nil, err
  67. }
  68. if !a.checkAllowAccess(fsh, rewritePath, aofsCanWrite) {
  69. return nil, errors.New("Permission denied")
  70. }
  71. return fsh.FileSystemAbstraction.Open(rewritePath)
  72. }
  73. func (a aofs) Stat(name string) (os.FileInfo, error) {
  74. //fmt.Println("FTP STAT")
  75. fsh, rewritePath, err := a.pathRewrite(name)
  76. if err != nil {
  77. return nil, err
  78. }
  79. if !a.checkAllowAccess(fsh, rewritePath, aofsCanRead) {
  80. return nil, errors.New("Permission denied")
  81. }
  82. return fsh.FileSystemAbstraction.Stat(rewritePath)
  83. }
  84. func (a aofs) OpenFile(name string, flag int, perm os.FileMode) (afero.File, error) {
  85. //fmt.Println("FTP OPEN FILE")
  86. fsh, rewritePath, err := a.pathRewrite(name)
  87. if err != nil {
  88. return nil, err
  89. }
  90. if !a.checkAllowAccess(fsh, rewritePath, aofsCanWrite) {
  91. return nil, errors.New("Permission denied")
  92. }
  93. return fsh.FileSystemAbstraction.OpenFile(rewritePath, flag, perm)
  94. }
  95. func (a aofs) AllocateSpace(size int) error {
  96. if a.userinfo.StorageQuota.HaveSpace(int64(size)) {
  97. return nil
  98. }
  99. return errors.New("Storage Quota Fulled")
  100. }
  101. func (a aofs) Remove(name string) error {
  102. fsh, rewritePath, err := a.pathRewrite(name)
  103. if err != nil {
  104. return err
  105. }
  106. if !a.checkAllowAccess(fsh, rewritePath, aofsCanWrite) {
  107. return errors.New("Permission denied")
  108. }
  109. return fsh.FileSystemAbstraction.Remove(rewritePath)
  110. }
  111. func (a aofs) RemoveAll(path string) error {
  112. fsh, rewritePath, err := a.pathRewrite(path)
  113. if err != nil {
  114. return err
  115. }
  116. if !a.checkAllowAccess(fsh, rewritePath, aofsCanWrite) {
  117. return errors.New("Permission denied")
  118. }
  119. return fsh.FileSystemAbstraction.RemoveAll(rewritePath)
  120. }
  121. func (a aofs) Rename(oldname, newname string) error {
  122. fshsrc, rewritePathsrc, err := a.pathRewrite(oldname)
  123. if err != nil {
  124. return err
  125. }
  126. fshdest, rewritePathdest, err := a.pathRewrite(newname)
  127. if err != nil {
  128. return err
  129. }
  130. if !a.checkAllowAccess(fshsrc, rewritePathsrc, aofsCanWrite) {
  131. return errors.New("Permission denied")
  132. }
  133. if !a.checkAllowAccess(fshdest, rewritePathdest, aofsCanWrite) {
  134. return errors.New("Permission denied")
  135. }
  136. if !fshdest.FileSystemAbstraction.FileExists(filepath.Dir(rewritePathdest)) {
  137. fshdest.FileSystemAbstraction.MkdirAll(filepath.Dir(rewritePathdest), 0775)
  138. }
  139. if fshsrc.UUID == fshdest.UUID {
  140. //Renaming in same fsh
  141. return fshsrc.FileSystemAbstraction.Rename(rewritePathsrc, rewritePathdest)
  142. } else {
  143. //Cross fsh read write.
  144. f, err := fshsrc.FileSystemAbstraction.ReadStream(rewritePathsrc)
  145. if err != nil {
  146. return err
  147. }
  148. defer f.Close()
  149. err = fshdest.FileSystemAbstraction.WriteStream(rewritePathdest, f, 0775)
  150. if err != nil {
  151. return err
  152. }
  153. err = fshsrc.FileSystemAbstraction.RemoveAll(rewritePathsrc)
  154. if err != nil {
  155. return err
  156. }
  157. }
  158. return nil
  159. }
  160. func (a aofs) Name() string {
  161. return "arozos virtualFS"
  162. }
  163. func (a aofs) Chmod(name string, mode os.FileMode) error {
  164. fsh, rewritePath, err := a.pathRewrite(name)
  165. if err != nil {
  166. return err
  167. }
  168. if !a.checkAllowAccess(fsh, rewritePath, aofsCanWrite) {
  169. return errors.New("Permission denied")
  170. }
  171. return fsh.FileSystemAbstraction.Chmod(rewritePath, mode)
  172. }
  173. func (a aofs) Chtimes(name string, atime time.Time, mtime time.Time) error {
  174. fsh, rewritePath, err := a.pathRewrite(name)
  175. if err != nil {
  176. return err
  177. }
  178. if !a.checkAllowAccess(fsh, rewritePath, aofsCanWrite) {
  179. return errors.New("Permission denied")
  180. }
  181. return fsh.FileSystemAbstraction.Chtimes(rewritePath, atime, mtime)
  182. }
  183. // arozos adaptive functions
  184. // This function rewrite the path from ftp representation to real filepath on disk
  185. func (a aofs) pathRewrite(path string) (*filesystem.FileSystemHandler, string, error) {
  186. path = filepath.ToSlash(filepath.Clean(path))
  187. //log.Println("Original path: ", path)
  188. if path == "/" {
  189. //Roots. Show ftpbuf root
  190. fshs := a.userinfo.GetAllFileSystemHandler()
  191. for _, fsh := range fshs {
  192. //Create a folder representation for this virtual directory
  193. if !fsh.RequireBuffer {
  194. os.MkdirAll(filepath.Join(a.tmpFolder, fsh.UUID), 0755)
  195. }
  196. }
  197. readmeContent, err := os.ReadFile("./system/ftp/README.txt")
  198. if err != nil {
  199. readmeContent = []byte("DO NOT UPLOAD FILES INTO THE ROOT DIRECTORY")
  200. }
  201. os.WriteFile(filepath.Join(a.tmpFolder, "README.txt"), readmeContent, 0755)
  202. //Return the tmpFolder root
  203. tmpfs, _ := a.userinfo.GetFileSystemHandlerFromVirtualPath("tmp:/")
  204. return tmpfs, a.tmpFolder, nil
  205. } else if path == "/README.txt" {
  206. tmpfs, _ := a.userinfo.GetFileSystemHandlerFromVirtualPath("tmp:/")
  207. return tmpfs, a.tmpFolder + "README.txt", nil
  208. } else if len(path) > 0 {
  209. //Rewrite the path for any alternative filepath
  210. //Get the uuid of the filepath
  211. path := path[1:]
  212. subpaths := strings.Split(path, "/")
  213. fsHandlerUUID := subpaths[0]
  214. remainingPaths := subpaths[1:]
  215. fsh, err := a.userinfo.GetFileSystemHandlerFromVirtualPath(fsHandlerUUID + ":")
  216. if err != nil {
  217. return nil, "", errors.New("File System Abstraction not found")
  218. }
  219. rpath, err := fsh.FileSystemAbstraction.VirtualPathToRealPath(fsh.UUID+":/"+strings.Join(remainingPaths, "/"), a.userinfo.Username)
  220. if err != nil {
  221. return nil, "", errors.New("File System Handler Hierarchy not supported by FTP driver")
  222. }
  223. return fsh, rpath, nil
  224. } else {
  225. //fsh not found.
  226. return nil, "", errors.New("Invalid path")
  227. }
  228. }
  229. // Check if user has access to the given path, mode can be string {read / write}
  230. func (a aofs) checkAllowAccess(fsh *filesystem.FileSystemHandler, path string, mode int) bool {
  231. vpath, err := fsh.FileSystemAbstraction.RealPathToVirtualPath(path, a.userinfo.Username)
  232. if err != nil {
  233. return false
  234. }
  235. if mode == aofsCanRead {
  236. return a.userinfo.CanRead(vpath)
  237. } else if mode == aofsCanWrite {
  238. return a.userinfo.CanWrite(vpath)
  239. } else {
  240. return false
  241. }
  242. }