Advertisement
Guest User

perm.l / Moonux S11

a guest
Sep 20th, 2016
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.39 KB | None | 0 0
  1. ```lua
  2. --[[
  3.   mnx11: Moonux Satellite 11
  4.   NAME:        /lib/perm.l
  5.   CATEGORY:    corelib
  6.   VERSION:     11:90816A
  7.   DESCRIPTION: Permission and user library
  8.  
  9.   Made by thecrimulo
  10.   ~ Moonux Project
  11. ]]--
  12.  
  13. api.load('/lib/sha.l')
  14.  
  15. ftmode = false
  16. if not g.exists('_u') then
  17.     g.add('_u', {})
  18.     ftmode = true
  19. end
  20.  
  21. local filter = {'/vit', '/boot', '/core'}
  22.  
  23. local password = {}
  24. function password.hash(raw)
  25.     return sha.sha256(raw)
  26. end
  27.  
  28. if ftmode then g.add('_cu', "root")
  29.  
  30. local user = {}
  31. function user.add(name, hash)
  32.     _u[name] == hash
  33.     vold.fs.makeDir("/home/"..name)
  34.     if SYSDEBUG then print("[perm.l] Added user "..user.." with hash "..hash) end
  35. end
  36.  
  37. function user.exists(name)
  38.     if _u[name] == nil then return false else return true end
  39. end
  40.  
  41. function user.remove(name)
  42.     if user.exists(name) then _u[name] = nil; vold.fs.delete("/home/"..name); return true
  43.     if SYSDEBUG then print("[perm.l] Removed user "..user) end else return false end
  44. end
  45.  
  46. function user.login(name, rawpass)
  47.     if user.exists(name) then
  48.         if password.hash(rawpass) == _u[name] then
  49.             _cu = name return true
  50.             if SYSDEBUG then print("[perm.l] Logged in: "..name) end
  51.         else
  52.             return false
  53.             if SYSDEBUG then print("[perm.l] Attempt to log in failed. Wrong password") end
  54.         end
  55.     else
  56.         return false
  57.         if SYSDEBUG then print("[perm.l] Attempt to log in failed. Wrong user") end
  58.     end
  59. end
  60.  
  61. if ftmode then user.add("root", password.hash("root")) end
  62. if ftmode then user.login("root", "root") end
  63.  
  64. local permission = {}
  65. function permission.addUserToFile(file, user, canw, canx)
  66.     addu = function(inner)
  67.         for item in inner do
  68.             if item['$isdir'] then addu(item)
  69.             elseif item.name == file then item['users'] = {[user] = {['w'] = canw, ['x'] = canx}};
  70.             if SYSDEBUG then print("[perm.l] Added "..user.." to "..file.." with custom wx permissions") end; break end
  71.         end
  72.     end
  73. end
  74.  
  75. function permission.check(path, user, wx)
  76.     if not user.exists(user) return false end
  77.     for _,filt in filter do if string.startsWith(path, filt) then return false end
  78.     local patht = string.seperate(path, "/")
  79.     g.add('_tmp-permission-check', table.copy(_fs))
  80.     c = 1
  81.     while patht[c] != nil do
  82.         _tmp-permission-check = _tmp-permission-check[patht[1]]
  83.     end
  84.     if not wx then
  85.         return _tmp-permission-check.users[user]['w']
  86.     else
  87.         return _tmp-permission-check.users[user]['x']
  88.     end
  89. end
  90.  
  91. _G.perm = {user, permission}
  92. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement