directoryHandler.go 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. package user
  2. import (
  3. fs "imuslab.com/arozos/mod/filesystem"
  4. "imuslab.com/arozos/mod/filesystem/arozfs"
  5. "imuslab.com/arozos/mod/storage"
  6. "imuslab.com/arozos/mod/storage/access"
  7. )
  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. */
  22. // Get the user home file system handler, aka user:/
  23. func (u *User) GetHomeFileSystemHandler() (*fs.FileSystemHandler, error) {
  24. return getHandlerFromID(u.HomeDirectories.Storages, "user")
  25. }
  26. // Get all user Acessible file system handlers (ignore special fsh like backups)
  27. func (u *User) GetAllAccessibleFileSystemHandler() []*fs.FileSystemHandler {
  28. results := []*fs.FileSystemHandler{}
  29. fshs := u.GetAllFileSystemHandler()
  30. for _, fsh := range fshs {
  31. if fsh.Hierarchy != "backup" {
  32. results = append(results, fsh)
  33. }
  34. }
  35. return results
  36. }
  37. // Try to get the root file system handler from vpath where the root file system handler must be in user scope of permission
  38. func (u *User) GetRootFSHFromVpathInUserScope(vpath string) *fs.FileSystemHandler {
  39. allFsh := u.GetAllAccessibleFileSystemHandler()
  40. var vpathSourceFsh *fs.FileSystemHandler
  41. for _, thisFsh := range allFsh {
  42. if thisFsh.IsRootOf(vpath) {
  43. vpathSourceFsh = thisFsh
  44. return vpathSourceFsh
  45. }
  46. }
  47. return nil
  48. }
  49. // drivePool is a storage pool a user may reach a drive through. home is the
  50. // permission group the pool belongs to, empty for the system pool.
  51. type drivePool struct {
  52. home string
  53. pool *storage.StoragePool
  54. }
  55. // drivePools lists every pool: the system pool, the user's own groups, then
  56. // all other groups, whose drives a user reaches through a row of the drive's
  57. // access list. The order matters only for a drive id used by two pools,
  58. // where the first one wins the same way it always did.
  59. func (u *User) drivePools() []drivePool {
  60. pools := []drivePool{}
  61. if u.HomeDirectories != nil {
  62. pools = append(pools, drivePool{pool: u.HomeDirectories})
  63. }
  64. listed := map[string]bool{}
  65. for _, pg := range u.PermissionGroup {
  66. listed[pg.Name] = true
  67. if pg.StoragePool != nil {
  68. pools = append(pools, drivePool{home: pg.Name, pool: pg.StoragePool})
  69. }
  70. }
  71. if u.parent != nil && u.parent.phandler != nil {
  72. for _, pg := range u.parent.phandler.PermissionGroups {
  73. if !listed[pg.Name] && pg.StoragePool != nil {
  74. pools = append(pools, drivePool{home: pg.Name, pool: pg.StoragePool})
  75. }
  76. }
  77. }
  78. return pools
  79. }
  80. // accessSubject describes this user to the access rules
  81. func (u *User) accessSubject() access.Subject {
  82. return access.Subject{Groups: u.GetUserPermissionGroupNames(), IsAdmin: u.IsAdmin()}
  83. }
  84. // driveRule returns the access list of a drive
  85. func (u *User) driveRule(uuid string, home string) access.Rule {
  86. if u.parent != nil && u.parent.driveRules != nil {
  87. if rule, ok := u.parent.driveRules.Rule(uuid); ok {
  88. return rule
  89. }
  90. }
  91. return access.DefaultRule(uuid, home)
  92. }
  93. // explainDrive works out what this user may do on one drive of one pool
  94. func (u *User) explainDrive(fsh *fs.FileSystemHandler, home string, subject access.Subject) access.Decision {
  95. drive := access.Drive{UUID: fsh.UUID, HomeGroup: home, ReadOnly: fsh.ReadOnly}
  96. return access.Explain(drive, subject, u.driveRule(fsh.UUID, home))
  97. }
  98. // GetAllFileSystemHandler lists the opened drives this user may use, whichever
  99. // pool they are configured in
  100. func (u *User) GetAllFileSystemHandler() []*fs.FileSystemHandler {
  101. results := []*fs.FileSystemHandler{}
  102. listed := map[string]bool{}
  103. subject := u.accessSubject()
  104. for _, dp := range u.drivePools() {
  105. for _, store := range dp.pool.Storages {
  106. if store.Closed || listed[store.UUID] {
  107. continue
  108. }
  109. if u.explainDrive(store, dp.home, subject).Level == arozfs.FsDenied {
  110. continue
  111. }
  112. results = append(results, store)
  113. listed[store.UUID] = true
  114. }
  115. }
  116. return results
  117. }
  118. /*
  119. func (u *User) VirtualPathToRealPath(vpath string) (string, error) {
  120. //Get all usable filesystem handler from the user's home directory and permission groups
  121. userFsHandlers := u.GetAllFileSystemHandler()
  122. //Clear the path
  123. virtualPath := filepath.ToSlash(filepath.Clean(vpath))
  124. //Check for path escape
  125. if len(virtualPath) > 2 && virtualPath[:2] == ".." {
  126. return "", errors.New("Request path out of storage root")
  127. }
  128. //Check for valid virtual device id
  129. vid, subpath, err := getIDFromVirtualPath(vpath)
  130. if err != nil {
  131. return "", err
  132. }
  133. if strings.Contains(filepath.ToSlash(filepath.Clean(subpath)), "../") || filepath.Clean(subpath) == ".." {
  134. return "", errors.New("Request path out of storage root")
  135. }
  136. //Look for the handler with the same virtualPath ID
  137. for _, storage := range userFsHandlers {
  138. if storage.UUID == vid {
  139. //This storage is the one we are looking at
  140. //Check if this has been closed
  141. if storage.Closed == true {
  142. return "", errors.New("Request Filesystem Handler has been closed by another process")
  143. }
  144. //Check if this is a backup drive
  145. if storage.Hierarchy == "backup" {
  146. return "", errors.New("Request Filesystem Handler do not allow direct access")
  147. }
  148. //A bit hacky to make sure subpath contains no traversal
  149. //Will migrate this to File System Vpath Resolver in the next large update
  150. subpath = strings.ReplaceAll(subpath, "../", "")
  151. //Handle general cases
  152. if storage.Hierarchy == "user" {
  153. return filepath.ToSlash(filepath.Join(filepath.Clean(storage.Path), "/users/", u.Username, subpath)), nil
  154. } else if storage.Hierarchy == "public" {
  155. return filepath.ToSlash(filepath.Join(filepath.Clean(storage.Path), subpath)), nil
  156. } else {
  157. return "", errors.New("Unknown Filesystem Handler Hierarchy")
  158. }
  159. }
  160. }
  161. return "", errors.New("Translation failed: Vitrual storage ID not found")
  162. }
  163. func (u *User) RealPathToVirtualPath(rpath string) (string, error) {
  164. //Get all usable filesystem handler
  165. userFsHandlers := u.GetAllFileSystemHandler()
  166. //Clear the path
  167. realPath := filepath.ToSlash(filepath.Clean(rpath))
  168. //Check for path escape
  169. if len(realPath) > 2 && realPath[:2] == ".." {
  170. //Fix: 20 May 2021: Allow using ../folder as virtual root directory
  171. //Check if there are vroots that actually use relative path as root directory.
  172. allowSpecialCasePassThrough := false
  173. for _, fsh := range userFsHandlers {
  174. thisVrootPath := fsh.Path
  175. if len(realPath) > len(thisVrootPath) && filepath.ToSlash(realPath[:len(thisVrootPath)]) == filepath.ToSlash(thisVrootPath) {
  176. allowSpecialCasePassThrough = true
  177. }
  178. }
  179. if !allowSpecialCasePassThrough {
  180. return "", errors.New("Request path out of storage root")
  181. }
  182. }
  183. //Look for a real path of a virtual device that the realpath is containing
  184. for _, storage := range userFsHandlers {
  185. thisStorageRoot := filepath.Clean(filepath.ToSlash(storage.Path))
  186. thisStorageRootAbs, err := filepath.Abs(thisStorageRoot)
  187. if err != nil {
  188. //Fail to abs this path. Maybe this is a emulated file system?
  189. thisStorageRootAbs = thisStorageRoot
  190. }
  191. thisStorageRootAbs = filepath.ToSlash(filepath.Clean(thisStorageRootAbs))
  192. pathContained := false
  193. subPath := ""
  194. if len(realPath) > len(thisStorageRoot) && filepath.ToSlash(realPath[:len(thisStorageRoot)]) == filepath.ToSlash(thisStorageRoot) {
  195. //This realpath is in contained inside this storage root
  196. pathContained = true
  197. subtractionPath := thisStorageRoot
  198. if storage.Hierarchy == "user" {
  199. //Check if this file is belongs to this user
  200. startOffset := len(filepath.Clean(thisStorageRoot) + "/users/")
  201. if len(realPath) < startOffset+len(u.Username) {
  202. //This file is not owned by this user
  203. return "", errors.New("File not owned by this user")
  204. } else {
  205. userNameMatch := realPath[startOffset : startOffset+len(u.Username)]
  206. if userNameMatch != u.Username {
  207. //This file is not owned by this user
  208. return "", errors.New("File not owned by this user")
  209. }
  210. }
  211. //Generate subtraction path
  212. subtractionPath = thisStorageRoot + "/users/" + u.Username + "/"
  213. }
  214. if len(subtractionPath) < len(realPath) {
  215. subPath = realPath[len(subtractionPath):]
  216. }
  217. } else if len(realPath) > len(thisStorageRootAbs) && filepath.ToSlash(realPath[:len(thisStorageRootAbs)]) == filepath.ToSlash(thisStorageRootAbs) {
  218. //The realpath contains the absolute path of this storage root
  219. pathContained = true
  220. subtractionPath := thisStorageRootAbs
  221. if storage.Hierarchy == "user" {
  222. subtractionPath = thisStorageRootAbs + "/users/" + u.Username + "/"
  223. }
  224. if len(subtractionPath) < len(realPath) {
  225. subPath = realPath[len(subtractionPath):]
  226. }
  227. } else if filepath.ToSlash(realPath) == filepath.ToSlash(thisStorageRoot) {
  228. //Storage Root's root
  229. pathContained = true
  230. subPath = ""
  231. }
  232. if len(subPath) > 1 && subPath[:1] == "/" {
  233. subPath = subPath[1:]
  234. }
  235. if pathContained == true {
  236. //This storage is one of the root of the given realpath. Translate it into this
  237. if storage == true {
  238. return "", errors.New("Request Filesystem Handler has been closed by another process")
  239. }
  240. return storage.UUID + ":/" + subPath, nil
  241. }
  242. }
  243. return "", errors.New("Unable to resolve realpath in virtual devices root path")
  244. }
  245. */
  246. // Get a file system handler from a virtual path, this file system handler might not be the highest prioity one
  247. func (u *User) GetFileSystemHandlerFromVirtualPath(vpath string) (*fs.FileSystemHandler, error) {
  248. fsHandlers := u.GetAllFileSystemHandler()
  249. handler, err := getHandlerFromVirtualPath(fsHandlers, vpath)
  250. return handler, err
  251. }
  252. /*
  253. func (u *User) GetFileSystemHandlerFromRealPath(rpath string) (*fs.FileSystemHandler, error) {
  254. vpath, err := u.RealPathToVirtualPath(rpath)
  255. if err != nil {
  256. return &fs.FileSystemHandler{}, err
  257. }
  258. return u.GetFileSystemHandlerFromVirtualPath(vpath)
  259. }
  260. */