aofs.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. package ftp
  2. //arozos virtual path translation handler
  3. //author: tobychui
  4. import (
  5. "errors"
  6. "io/ioutil"
  7. "log"
  8. "os"
  9. "path/filepath"
  10. "strconv"
  11. "strings"
  12. "time"
  13. "github.com/spf13/afero"
  14. fs "imuslab.com/arozos/mod/filesystem"
  15. "imuslab.com/arozos/mod/filesystem/hidden"
  16. "imuslab.com/arozos/mod/user"
  17. )
  18. type aofs struct {
  19. userinfo *user.User
  20. tmpFolder string
  21. }
  22. func (a aofs) Create(name string) (afero.File, error) {
  23. rewritePath, _, err := a.pathRewrite(name)
  24. if err != nil {
  25. return nil, err
  26. }
  27. if !a.checkAllowAccess(rewritePath, "write") {
  28. return nil, errors.New("Permission Denied")
  29. }
  30. //log.Println("Create", rewritePath)
  31. fd, err := os.Create(rewritePath)
  32. if err != nil {
  33. return nil, err
  34. }
  35. return fd, nil
  36. }
  37. func (a aofs) Chown(name string, uid, gid int) error {
  38. rewritePath, _, err := a.pathRewrite(name)
  39. if err != nil {
  40. return err
  41. }
  42. if !a.checkAllowAccess(rewritePath, "write") {
  43. return errors.New("Permission Denied")
  44. }
  45. return os.Chown(name, uid, gid)
  46. }
  47. func (a aofs) Mkdir(name string, perm os.FileMode) error {
  48. rewritePath, _, err := a.pathRewrite(name)
  49. if err != nil {
  50. return err
  51. }
  52. if !a.checkAllowAccess(rewritePath, "write") {
  53. return errors.New("Permission Denied")
  54. }
  55. os.Mkdir(rewritePath, perm)
  56. return nil
  57. }
  58. func (a aofs) MkdirAll(path string, perm os.FileMode) error {
  59. rewritePath, _, err := a.pathRewrite(path)
  60. if err != nil {
  61. return err
  62. }
  63. if !a.checkAllowAccess(rewritePath, "write") {
  64. return errors.New("Permission Denied")
  65. }
  66. os.MkdirAll(rewritePath, perm)
  67. return nil
  68. }
  69. func (a aofs) Open(name string) (afero.File, error) {
  70. rewritePath, _, err := a.pathRewrite(name)
  71. if err != nil {
  72. return nil, err
  73. }
  74. if !a.checkAllowAccess(rewritePath, "read") {
  75. return nil, errors.New("Permission Denied")
  76. }
  77. //log.Println("Open", name, rewritePath)
  78. fd, err := os.Open(rewritePath)
  79. if err != nil {
  80. return nil, err
  81. }
  82. return fd, nil
  83. }
  84. func (a aofs) Stat(name string) (os.FileInfo, error) {
  85. rewritePath, _, err := a.pathRewrite(name)
  86. if err != nil {
  87. return nil, err
  88. }
  89. if !a.checkAllowAccess(rewritePath, "read") {
  90. return nil, errors.New("Permission Denied")
  91. }
  92. //log.Println("Stat", rewritePath)
  93. fileStat, err := os.Stat(rewritePath)
  94. return fileStat, err
  95. }
  96. func (a aofs) OpenFile(name string, flag int, perm os.FileMode) (afero.File, error) {
  97. rewritePath, _, err := a.pathRewrite(name)
  98. if err != nil {
  99. return nil, err
  100. }
  101. //log.Println("OpenFile", rewritePath)
  102. if !fileExists(rewritePath) {
  103. if !a.checkAllowAccess(rewritePath, "write") {
  104. return nil, errors.New("Directory is Read Only")
  105. }
  106. //Set ownership of this file to user.
  107. //Cannot use SetOwnership due to the filesize of the given file didn't exists yet
  108. fsh, _ := a.userinfo.GetFileSystemHandlerFromRealPath(rewritePath)
  109. fsh.CreateFileRecord(rewritePath, a.userinfo.Username)
  110. //Create the upload pending file
  111. fd, err := os.Create(rewritePath)
  112. if err != nil {
  113. return nil, err
  114. }
  115. return fd, nil
  116. } else {
  117. if !a.checkAllowAccess(rewritePath, "read") {
  118. return nil, errors.New("Permission Denied")
  119. }
  120. fd, err := os.Open(rewritePath)
  121. if err != nil {
  122. return nil, err
  123. }
  124. return fd, nil
  125. }
  126. }
  127. func (a aofs) AllocateSpace(size int) error {
  128. //log.Println("AllocateSpace", size)
  129. if a.userinfo.StorageQuota.HaveSpace(int64(size)) {
  130. return nil
  131. }
  132. return errors.New("Storage Quota Fulled")
  133. }
  134. func (a aofs) Remove(name string) error {
  135. rewritePath, _, err := a.pathRewrite(name)
  136. if err != nil {
  137. return err
  138. }
  139. if !a.checkAllowAccess(rewritePath, "write") {
  140. return errors.New("Target is Read Only")
  141. }
  142. isHiddenFile, _ := hidden.IsHidden(rewritePath, true)
  143. if isHiddenFile {
  144. //Hidden files, include cache or trash
  145. return errors.New("Access denied for hidden files")
  146. }
  147. log.Println(a.userinfo.Username + " removed " + rewritePath + " via FTP endpoint")
  148. os.MkdirAll(filepath.Dir(rewritePath)+"/.metadata/.trash/", 0755)
  149. os.Rename(rewritePath, filepath.Dir(rewritePath)+"/.metadata/.trash/"+filepath.Base(rewritePath)+"."+strconv.Itoa(int(time.Now().Unix())))
  150. return nil
  151. }
  152. func (a aofs) RemoveAll(path string) error {
  153. rewritePath, _, err := a.pathRewrite(path)
  154. if err != nil {
  155. return err
  156. }
  157. //log.Println("RemoveAll", rewritePath)
  158. isHiddenFile, _ := hidden.IsHidden(rewritePath, true)
  159. if isHiddenFile {
  160. //Hidden files, include cache or trash
  161. return errors.New("Target is Read Only")
  162. }
  163. if !a.checkAllowAccess(rewritePath, "write") {
  164. return errors.New("Permission Denied")
  165. }
  166. os.MkdirAll(filepath.Dir(rewritePath)+"/.metadata/.trash/", 0755)
  167. os.Rename(rewritePath, filepath.Dir(rewritePath)+"/.metadata/.trash/"+filepath.Base(rewritePath)+"."+strconv.Itoa(int(time.Now().Unix())))
  168. return nil
  169. }
  170. func (a aofs) Rename(oldname, newname string) error {
  171. oldpath, _, err := a.pathRewrite(oldname)
  172. if err != nil {
  173. return err
  174. }
  175. newpath, _, err := a.pathRewrite(newname)
  176. if err != nil {
  177. return err
  178. }
  179. if !a.checkAllowAccess(oldpath, "write") {
  180. return errors.New("Target is Read Only")
  181. }
  182. if !a.checkAllowAccess(newpath, "write") {
  183. return errors.New("Target is Read Only")
  184. }
  185. if fileExists(newpath) {
  186. return errors.New("File already exists")
  187. }
  188. os.Rename(oldpath, newpath)
  189. //log.Println("Rename", oldpath, newpath)
  190. return nil
  191. }
  192. func (a aofs) Name() string {
  193. return "arozos virtualFS"
  194. }
  195. func (a aofs) Chmod(name string, mode os.FileMode) error {
  196. //log.Println("Chmod", name, mode)
  197. return nil
  198. }
  199. func (a aofs) Chtimes(name string, atime time.Time, mtime time.Time) error {
  200. //log.Println("Chtimes", name, atime, mtime)
  201. return nil
  202. }
  203. //arozos adaptive functions
  204. //This function rewrite the path from ftp representation to real filepath on disk
  205. func (a aofs) pathRewrite(path string) (string, *fs.FileSystemHandler, error) {
  206. path = filepath.ToSlash(filepath.Clean(path))
  207. //log.Println("Original path: ", path)
  208. if path == "/" {
  209. //Roots. Show ftpbuf root
  210. fsHandlers := a.userinfo.GetAllFileSystemHandler()
  211. for _, fsh := range fsHandlers {
  212. //Create a folder representation for this virtual directory
  213. if !(fsh.Hierarchy == "backup" || fsh.IsVirtual()) {
  214. os.Mkdir(a.tmpFolder+fsh.UUID, 0755)
  215. }
  216. }
  217. readmeContent, err := ioutil.ReadFile("./system/ftp/README.txt")
  218. if err != nil {
  219. readmeContent = []byte("DO NOT UPLOAD FILES INTO THE ROOT DIRECTORY")
  220. }
  221. ioutil.WriteFile(a.tmpFolder+"README.txt", readmeContent, 0755)
  222. //Return the tmpFolder root
  223. tmpfs, _ := a.userinfo.GetFileSystemHandlerFromVirtualPath("tmp:/")
  224. return a.tmpFolder, tmpfs, nil
  225. } else if path == "/README.txt" {
  226. tmpfs, _ := a.userinfo.GetFileSystemHandlerFromVirtualPath("tmp:/")
  227. return a.tmpFolder + "README.txt", tmpfs, nil
  228. } else if len(path) > 0 {
  229. //Rewrite the path for any alternative filepath
  230. //Get the uuid of the filepath
  231. path := path[1:]
  232. subpaths := strings.Split(path, "/")
  233. fsHandlerUUID := subpaths[0]
  234. remainingPaths := subpaths[1:]
  235. //Look for the fsHandler with this UUID
  236. fsHandlers := a.userinfo.GetAllFileSystemHandler()
  237. for _, fsh := range fsHandlers {
  238. //Create a folder representation for this virtual directory
  239. if fsh.UUID == fsHandlerUUID {
  240. //This is the correct handler
  241. if fsh.Hierarchy == "user" {
  242. return filepath.ToSlash(filepath.Clean(fsh.Path)) + "/users/" + a.userinfo.Username + "/" + strings.Join(remainingPaths, "/"), fsh, nil
  243. } else if fsh.Hierarchy == "public" {
  244. return filepath.ToSlash(filepath.Clean(fsh.Path)) + "/" + strings.Join(remainingPaths, "/"), fsh, nil
  245. }
  246. }
  247. }
  248. //fsh not found.
  249. return "", nil, errors.New("Path is READ ONLY")
  250. } else {
  251. //fsh not found.
  252. return "", nil, errors.New("Invalid path")
  253. }
  254. }
  255. //Check if user has access to the given path, mode can be string {read / write}
  256. func (a aofs) checkAllowAccess(path string, mode string) bool {
  257. //Convert the realpath to virtualpath
  258. vpath, err := a.userinfo.RealPathToVirtualPath(path)
  259. if err != nil {
  260. log.Println("ERROR: " + a.userinfo.Username + " tried to access " + path + " but failed: " + err.Error())
  261. return false
  262. }
  263. isHiddenFolder, _ := hidden.IsHidden(vpath, true)
  264. if isHiddenFolder {
  265. return false
  266. }
  267. if mode == "read" {
  268. return a.userinfo.CanRead(vpath)
  269. } else if mode == "write" {
  270. return a.userinfo.CanWrite(vpath)
  271. }
  272. log.Println(path, mode)
  273. //Unknown path. Return false for security purposes
  274. return false
  275. }
  276. //Helper functs
  277. func isDir(path string) bool {
  278. fileInfo, err := os.Stat(path)
  279. if err != nil {
  280. return false
  281. }
  282. return fileInfo.IsDir()
  283. }
  284. func fileExists(path string) bool {
  285. if _, err := os.Stat(path); os.IsNotExist(err) {
  286. return false
  287. }
  288. return true
  289. }