Advertisement
DOGGYWOOF

secure lock with local users

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