aofs.go 8.2 KB

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