directoryHandler.go 8.0 KB

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