Advertisement
DOGGYWOOF

users

Jan 20th, 2024 (edited)
15
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1. local MAX_ATTEMPTS = 3 -- Maximum number of incorrect password attempts allowed
  2. local LOCKOUT_TIME = 30 -- Lockout time in seconds after reaching maximum attempts
  3.  
  4. local USERS_FOLDER = "/disk/users/"
  5. local ERROR_FOLDER = "/disk/error/"
  6. local BSOD_PROGRAM = "BSOD.lua"
  7.  
  8. local function getUserCredentials(username)
  9. local passwordFile = fs.combine(USERS_FOLDER .. username, "password.txt")
  10.  
  11. if fs.exists(passwordFile) then
  12. local file = fs.open(passwordFile, "r")
  13. local password = file.readLine()
  14. file.close()
  15. return password
  16. else
  17. return nil -- User does not exist
  18. end
  19. end
  20.  
  21. local function setUserCredentials(username, password)
  22. local userFolder = fs.combine(USERS_FOLDER, username)
  23. fs.makeDir(userFolder)
  24.  
  25. local passwordFile = fs.combine(userFolder, "password.txt")
  26.  
  27. local file = fs.open(passwordFile, "w")
  28. file.writeLine(password)
  29. file.close()
  30. end
  31.  
  32. local function checkCredentials(username)
  33. local storedPassword = getUserCredentials(username)
  34.  
  35. if not storedPassword then
  36. return false -- User does not exist
  37. end
  38.  
  39. local attempts = 0
  40.  
  41. repeat
  42. term.clear()
  43. term.setCursorPos(1, 1)
  44. print("Enter password for " .. username .. ":")
  45. local enteredPassword = read("*")
  46. attempts = attempts + 1
  47.  
  48. if enteredPassword == storedPassword then
  49. return true
  50. else
  51. print("Incorrect password. Attempts left: " .. tostring(MAX_ATTEMPTS - attempts))
  52. end
  53. until attempts >= MAX_ATTEMPTS
  54.  
  55. print("Too many incorrect attempts. Locked out for " .. LOCKOUT_TIME .. " seconds.")
  56. os.sleep(LOCKOUT_TIME)
  57.  
  58. return false
  59. end
  60.  
  61. local function main()
  62. local users = fs.list(USERS_FOLDER)
  63.  
  64. if not users or #users == 0 then
  65. -- No users detected, run the BSOD program
  66. shell.run(ERROR_FOLDER .. BSOD_PROGRAM)
  67. else
  68. term.clear()
  69. term.setCursorPos(1, 1)
  70.  
  71. print("Enter username:")
  72. local enteredUsername = read()
  73.  
  74. if checkCredentials(enteredUsername) then
  75. print("Access granted. Welcome, " .. enteredUsername .. "!")
  76. -- Your main OS code goes here
  77. else
  78. print("Access denied.")
  79. end
  80. end
  81. end
  82.  
  83. main()
  84.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement