R3C0NF16UR3D

usermanagement.lua

Jul 31st, 2023
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.07 KB | None | 0 0
  1. local component = require("component")
  2. local gpu = component.gpu
  3. local computer = require("computer")
  4. local modem = component.modem
  5. local wm = require("windowmanager/libs/wm")
  6. local gui = require("windowmanager/libs/guiElements")
  7. local crypt = require("windowmanager/libs/crypt")
  8.  
  9. local user = {}
  10. local version = "1.0"
  11.  
  12.  
  13.  
  14.  
  15. -- see if the file exists
  16. function file_exists(file)
  17. local f = io.open(file, "rb")
  18. if f then f:close() end
  19. return f ~= nil
  20. end
  21.  
  22. -- get all lines from a file, returns an empty
  23. -- list/table if the file does not exist
  24. function lines_from(file)
  25. if not file_exists(file) then return {} end
  26. lines = {}
  27. for line in io.lines(file) do
  28. lines[#lines + 1] = line
  29. end
  30. return lines
  31. end
  32.  
  33.  
  34. local activeUser = ""
  35. local username = ""
  36. local password = ""
  37. local level = 0
  38.  
  39. local userPasswordTable = {}
  40.  
  41. function writePasswd()
  42. local file = io.open("/etc/windowmanager/passwd","w")
  43. for i = 1, #userPasswordTable do
  44. file:write(userPasswordTable[i].user .. ":" .. tonumber(userPasswordTable[i].level) .. ":" .. userPasswordTable[i].password)
  45. end
  46. file:close()
  47. end
  48.  
  49. function cryptPassword(password)
  50. local k = crypt.string2key(password)
  51. return crypt.crypt(password,k)
  52. end
  53.  
  54. function checkPassword(pass, key)
  55. local k = crypt.string2key(key)
  56. local p = crypt.crypt(pass, k, true)
  57. if p == key then
  58. return true
  59. end
  60. return false
  61. end
  62.  
  63. local function readPasswd()
  64. userPasswordTable = {}
  65. local file = "/etc/windowmanager/passwd"
  66. local lines = lines_from(file)
  67.  
  68. for key,value in pairs(lines) do
  69.  
  70. local pos = string.find(value,":")
  71. local tmpTable ={}
  72.  
  73. local us = string.sub(value, 1, pos - 1)
  74.  
  75. local tmpstr = string.sub(value, pos + 1)
  76. pos = string.find(tmpstr, ":")
  77.  
  78. local level = tonumber(string.sub(tmpstr, 1, pos - 1))
  79. local pw = string.sub(tmpstr, pos + 1)
  80.  
  81. tmpTable.user = us
  82. tmpTable.password = pw
  83. tmpTable.level = level
  84.  
  85. table.insert(userPasswordTable, tmpTable)
  86. end
  87. end
  88.  
  89. function user.addUser(username, password, level)
  90. for i = 1, #userPasswordTable do
  91. if userPasswordTable[i].user == username then
  92. return false, "user exists"
  93. end
  94. end
  95. computer.removeUser(username) -- just in case
  96. r, t = computer.addUser(username)
  97. if r == true then
  98. local tmpTable = {}
  99. tmpTable.user = username
  100. tmpTable.level = level
  101. tmpTable.password = cryptPassword(password)
  102. table.insert(userPasswordTable, tmpTable)
  103. writePasswd()
  104. return true
  105. else
  106. return r, t
  107. end
  108. end
  109.  
  110. function user.remove(username)
  111. readPasswd()
  112. for i = 1, #userPasswordTable do
  113. if userPasswordTable[i].user == username then
  114. num = i
  115. break
  116. end
  117. end
  118. if computer.removeUser(username) == true then
  119. table.remove(userPasswordTable, num)
  120. writePasswd()
  121. end
  122. end
  123.  
  124. function user.changePassword(username, oldPassword, password)
  125. readPasswd()
  126. local num = 0
  127. for i = 1, #userPasswordTable do
  128. if userPasswordTable[i].user == username then
  129. if checkPassword(userPasswordTable[i].password, oldPassword) == true then
  130. num = i
  131. break
  132. end
  133. end
  134. end
  135. if num > 0 then
  136. local tmpTable = {}
  137. tmpTable.level = userPasswordTable[num].level
  138. table.remove(userPasswordTable, num)
  139. tmpTable.user = username
  140. tmpTable.password = cryptPassword(password)
  141. table.insert(userPasswordTable, tmpTable)
  142. writePasswd()
  143. return true
  144. end
  145. return false
  146. end
  147.  
  148. function user.setLevel(username, level)
  149. readPasswd()
  150. local num = 0
  151. for i = 1, #userPasswordTable do
  152. if username == userPasswordTable[i].user then
  153. num = i
  154. break
  155. end
  156. end
  157. if num > 0 then
  158. local tmpTable = {}
  159. tmpTable.password = userPasswordTable[num].password
  160. table.remove(userPasswordTable, num)
  161. tmpTable.user = username
  162. tmpTable.level = level
  163. table.insert(userPasswordTable, tmpTable)
  164. writePasswd()
  165. return true
  166. end
  167. return false
  168. end
  169.  
  170.  
  171. function user.getLevel(username)
  172. readPasswd()
  173. for i = 1, #userPasswordTable do
  174. return userPasswordTable[i].level
  175. end
  176. return -1
  177. end
  178.  
  179. local function checkUser(username)
  180. readPasswd()
  181. for i = 1, #userPasswordTable do
  182. if username == userPasswordTable[i].user then
  183. return true
  184. end
  185. end
  186. return false
  187. end
  188.  
  189. local ScreenWidth, ScreenHeight = gpu.getResolution()
  190.  
  191.  
  192. local function loginCallback(self, win)
  193. if checkUser(username) == true then
  194. local p, num
  195. for i = 1, #userPasswordTable do
  196. if userPasswordTable[i].user == username then
  197. p = userPasswordTable[i].password
  198. num = i
  199. break
  200. end
  201. end
  202. if checkPassword(p, password) == true then
  203. wm.setUserLevel(tonumber(userPasswordTable[num].level))
  204. activeUser = username
  205. wm.setUsername(username)
  206. gui.setElementText(nameInput, "")
  207. gui.setElementText(passwordInput, "")
  208. wm.closeWindow(window)
  209. wm.blockMouse(false)
  210. end
  211. end
  212. end
  213.  
  214. function user.getActiveUser()
  215. return activeUser
  216. end
  217.  
  218. function user.getUserList()
  219. readPasswd()
  220. local tmpTable = {}
  221. for i = 1, #userPasswordTable do
  222. local tmp = {}
  223. tmp.user = userPasswordTable[i].user
  224. tmp.level =userPasswordTable[i].level
  225. table.insert(tmpTable, tmp)
  226. end
  227. return tmpTable
  228. end
  229.  
  230. local function inputNameCallback(self, win)
  231. username = gui.getElementText(self)
  232. end
  233.  
  234. local function inputPasswordCallback(self, win)
  235. password = gui.getElementText(self)
  236. end
  237.  
  238. function user.login()
  239. wm.blockMouse(true)
  240.  
  241. readPasswd()
  242.  
  243. local windowWidth = 40
  244. local windowHeight = 7
  245.  
  246. local window = wm.newWindow(math.floor(ScreenWidth/2) - math.floor(windowWidth/2), math.floor(ScreenHeight/2) - math.floor(windowHeight/2), windowWidth, windowHeight, "Login")
  247. wm.disableWindowButtons(window, true)
  248. wm.setWindowSticky(window, true)
  249.  
  250. wm.addElement(window, gui.newLabel(2, 2, 11, 1, "Username :"))
  251. wm.addElement(window, gui.newLabel(2, 4, 11, 1, "Password :"))
  252.  
  253. nameInput = gui.newInput(13, 2, 25, "", inputNameCallback)
  254. passwordInput = gui.newInput(13, 4, 25, "", inputPasswordCallback)
  255. wm.addElement(window, nameInput)
  256. gui.setElementProtected(passwordInput, true)
  257. wm.addElement(window, passwordInput)
  258.  
  259.  
  260. wm.addElement(window, gui.newLine( 1, 5, windowWidth -2))
  261.  
  262. local CloseButton = gui.newButton(math.floor(windowWidth/2) - 3, windowHeight - 1, 5, 1, "login", loginCallback)
  263. wm.addElement(window, CloseButton)
  264.  
  265. wm.raiseWindow(window)
  266. gui.setElementIgnoreMouseBlock(nameInput, true)
  267. gui.setElementIgnoreMouseBlock(passwordInput, true)
  268. gui.setElementIgnoreMouseBlock(CloseButton, true)
  269. -- wm.blockMouse(false) nicht vergessen :-)
  270. end
  271.  
  272. function user.logout()
  273. wm.setUserLevel(0)
  274. activeUser = ""
  275. wm.setUsername("")
  276. username = ""
  277. password = ""
  278. level = 0
  279. wm.raiseWindow(window)
  280. wm.blockMouse(true)
  281. end
  282.  
  283.  
  284.  
  285. function user.start()
  286. wm.registerDriver("user manager", "Handles all user managements, rights and passwords", "S.Kempa", version)
  287. end
  288.  
  289. function user.stop()
  290. end
  291.  
  292. return user
Advertisement
Add Comment
Please, Sign In to add comment