directoryHandler.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. package user
  2. import (
  3. "errors"
  4. "os"
  5. "path/filepath"
  6. fs "imuslab.com/arozos/mod/filesystem"
  7. )
  8. func (u *User) GetHomeDirectory() (string, error) {
  9. //Return the realpath of the user home directory
  10. for _, dir := range u.HomeDirectories.Storages {
  11. if dir.UUID == "user" {
  12. //This is the target user root
  13. root := filepath.ToSlash(filepath.Clean(dir.Path) + "/users/" + u.Username + "/")
  14. os.MkdirAll(root, 0755)
  15. return root, nil
  16. }
  17. }
  18. return "", errors.New("User root not found. Is this a permission group instead of a real user?")
  19. }
  20. //Get all user Acessible file system handlers (ignore special fsh like backups)
  21. func (u *User) GetAllAccessibleFileSystemHandler() []*fs.FileSystemHandler {
  22. results := []*fs.FileSystemHandler{}
  23. fshs := u.GetAllFileSystemHandler()
  24. for _, fsh := range fshs {
  25. if fsh.Hierarchy != "backup" {
  26. results = append(results, fsh)
  27. }
  28. }
  29. return results
  30. }
  31. func (u *User) GetAllFileSystemHandler() []*fs.FileSystemHandler {
  32. results := []*fs.FileSystemHandler{}
  33. uuids := []string{}
  34. //Get all FileSystem Handler from this user's Home Directory (aka base directory)
  35. for _, store := range u.HomeDirectories.Storages {
  36. if store.Closed == false {
  37. //Only return opened file system handlers
  38. results = append(results, store)
  39. uuids = append(uuids, store.UUID)
  40. }
  41. }
  42. //Get all the FileSystem handler that is accessable by this user
  43. for _, pg := range u.PermissionGroup {
  44. //For each permission group that this user is in
  45. for _, store := range pg.StoragePool.Storages {
  46. //Get each of the storage of this permission group is assigned to
  47. if !inSlice(uuids, store.UUID) {
  48. if store.Closed == false {
  49. //Only return opened file system handlers
  50. results = append(results, store)
  51. uuids = append(uuids, store.UUID)
  52. }
  53. }
  54. }
  55. }
  56. return results
  57. }
  58. func (u *User) VirtualPathToRealPath(vpath string) (string, error) {
  59. //Get all usable filesystem handler from the user's home directory and permission groups
  60. userFsHandlers := u.GetAllFileSystemHandler()
  61. //Clear the path
  62. virtualPath := filepath.ToSlash(filepath.Clean(vpath))
  63. //Check for path escape
  64. if len(virtualPath) > 2 && virtualPath[:2] == ".." {
  65. return "", errors.New("Request path out of storage root")
  66. }
  67. //Check for valid virtual device id
  68. vid, subpath, err := getIDFromVirtualPath(vpath)
  69. if err != nil {
  70. return "", err
  71. }
  72. //Look for the handler with the same virtualPath ID
  73. for _, storage := range userFsHandlers {
  74. if storage.UUID == vid {
  75. //This storage is the one we are looking at
  76. //Check if this has been closed
  77. if storage.Closed == true {
  78. return "", errors.New("Request Filesystem Handler has been closed by another process")
  79. }
  80. //Check if this is a backup drive
  81. if storage.Hierarchy == "backup" {
  82. return "", errors.New("Request Filesystem Handler do not allow direct access")
  83. }
  84. //Handle general cases
  85. if storage.Hierarchy == "user" {
  86. return filepath.ToSlash(filepath.Join(filepath.Clean(storage.Path), "/users/", u.Username, subpath)), nil
  87. } else if storage.Hierarchy == "public" {
  88. return filepath.ToSlash(filepath.Join(filepath.Clean(storage.Path), subpath)), nil
  89. } else if storage.Hierarchy == "share" {
  90. return (*u.parent.shareEntryTable).ResolveShareVrootPath(subpath, u.Username, u.GetUserPermissionGroupNames())
  91. } else {
  92. return "", errors.New("Unknown Filesystem Handler Hierarchy")
  93. }
  94. }
  95. }
  96. return "", errors.New("Translation failed: Vitrual storage ID not found")
  97. }
  98. func (u *User) RealPathToVirtualPath(rpath string) (string, error) {
  99. //Get all usable filesystem handler
  100. userFsHandlers := u.GetAllFileSystemHandler()
  101. //Clear the path
  102. realPath := filepath.ToSlash(filepath.Clean(rpath))
  103. //Check for path escape
  104. if len(realPath) > 2 && realPath[:2] == ".." {
  105. //Fix: 20 May 2021: Allow using ../folder as virtual root directory
  106. //Check if there are vroots that actually use relative path as root directory.
  107. allowSpecialCasePassThrough := false
  108. for _, fsh := range userFsHandlers {
  109. thisVrootPath := fsh.Path
  110. if len(realPath) > len(thisVrootPath) && filepath.ToSlash(realPath[:len(thisVrootPath)]) == filepath.ToSlash(thisVrootPath) {
  111. allowSpecialCasePassThrough = true
  112. }
  113. }
  114. if !allowSpecialCasePassThrough {
  115. return "", errors.New("Request path out of storage root")
  116. }
  117. }
  118. //Look for a real path of a virtual device that the realpath is containing
  119. for _, storage := range userFsHandlers {
  120. thisStorageRoot := filepath.Clean(filepath.ToSlash(storage.Path))
  121. thisStorageRootAbs, err := filepath.Abs(thisStorageRoot)
  122. if err != nil {
  123. //Fail to abs this path. Maybe this is a emulated file system?
  124. thisStorageRootAbs = thisStorageRoot
  125. }
  126. thisStorageRootAbs = filepath.ToSlash(filepath.Clean(thisStorageRootAbs))
  127. pathContained := false
  128. subPath := ""
  129. if len(realPath) > len(thisStorageRoot) && filepath.ToSlash(realPath[:len(thisStorageRoot)]) == filepath.ToSlash(thisStorageRoot) {
  130. //This realpath is in contained inside this storage root
  131. pathContained = true
  132. subtractionPath := thisStorageRoot
  133. if storage.Hierarchy == "user" {
  134. //Check if this file is belongs to this user
  135. startOffset := len(filepath.Clean(thisStorageRoot) + "/users/")
  136. if len(realPath) < startOffset+len(u.Username) {
  137. //This file is not owned by this user
  138. return "", errors.New("File not owned by this user")
  139. } else {
  140. userNameMatch := realPath[startOffset : startOffset+len(u.Username)]
  141. if userNameMatch != u.Username {
  142. //This file is not owned by this user
  143. return "", errors.New("File not owned by this user")
  144. }
  145. }
  146. //Generate subtraction path
  147. subtractionPath = thisStorageRoot + "/users/" + u.Username + "/"
  148. }
  149. if len(subtractionPath) < len(realPath) {
  150. subPath = realPath[len(subtractionPath):]
  151. }
  152. } else if len(realPath) > len(thisStorageRootAbs) && filepath.ToSlash(realPath[:len(thisStorageRootAbs)]) == filepath.ToSlash(thisStorageRootAbs) {
  153. //The realpath contains the absolute path of this storage root
  154. pathContained = true
  155. subtractionPath := thisStorageRootAbs
  156. if storage.Hierarchy == "user" {
  157. subtractionPath = thisStorageRootAbs + "/users/" + u.Username + "/"
  158. }
  159. if len(subtractionPath) < len(realPath) {
  160. subPath = realPath[len(subtractionPath):]
  161. }
  162. } else if filepath.ToSlash(realPath) == filepath.ToSlash(thisStorageRoot) {
  163. //Storage Root's root
  164. pathContained = true
  165. subPath = ""
  166. }
  167. if len(subPath) > 1 && subPath[:1] == "/" {
  168. subPath = subPath[1:]
  169. }
  170. if pathContained == true {
  171. //This storage is one of the root of the given realpath. Translate it into this
  172. if storage.Closed == true {
  173. return "", errors.New("Request Filesystem Handler has been closed by another process")
  174. }
  175. return storage.UUID + ":/" + subPath, nil
  176. }
  177. }
  178. return "", errors.New("Unable to resolve realpath in virtual devices root path")
  179. }
  180. //Get a file system handler from a virtual path, this file system handler might not be the highest prioity one
  181. func (u *User) GetFileSystemHandlerFromVirtualPath(vpath string) (*fs.FileSystemHandler, error) {
  182. fsHandlers := u.GetAllFileSystemHandler()
  183. handler, err := getHandlerFromVirtualPath(fsHandlers, vpath)
  184. return handler, err
  185. }
  186. func (u *User) GetFileSystemHandlerFromRealPath(rpath string) (*fs.FileSystemHandler, error) {
  187. vpath, err := u.RealPathToVirtualPath(rpath)
  188. if err != nil {
  189. return &fs.FileSystemHandler{}, err
  190. }
  191. return u.GetFileSystemHandlerFromVirtualPath(vpath)
  192. }