aofs.go 8.5 KB

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