Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local MAX_ATTEMPTS = 3 -- Maximum number of incorrect password attempts allowed
- local LOCKOUT_TIME = 30 -- Lockout time in seconds after reaching maximum attempts
- local USERS_FOLDER = "/disk/users/"
- local ERROR_FOLDER = "/disk/error/"
- local OS_GUI_PROGRAM = "/disk/os/gui"
- local NOT_ADMIN_MESSAGE = "This user is not an admin."
- local function getUserCredentials(username)
- local passwordFile = fs.combine(USERS_FOLDER .. username, "password.txt")
- if fs.exists(passwordFile) then
- local file = fs.open(passwordFile, "r")
- local storedPassword = file.readLine()
- file.close()
- return storedPassword
- else
- return nil -- User does not exist
- end
- end
- local function getAdminCredentials()
- local adminFile = "/disk/admin.txt"
- if fs.exists(adminFile) then
- local file = fs.open(adminFile, "r")
- local storedPassword = file.readLine()
- file.close()
- return storedPassword
- else
- return nil -- Admin file does not exist
- end
- end
- local function checkCredentials(username)
- if username == "admin" then
- local storedPassword = getAdminCredentials()
- if not storedPassword then
- return false -- Admin credentials not set
- end
- local attempts = 0
- repeat
- term.clear()
- term.setCursorPos(1, 1)
- print("Enter password for " .. username .. ":")
- local enteredPassword = read("*")
- attempts = attempts + 1
- if enteredPassword == storedPassword then
- return true
- else
- print("Incorrect password. Attempts left: " .. tostring(MAX_ATTEMPTS - attempts))
- end
- until attempts >= MAX_ATTEMPTS
- print("Too many incorrect attempts. Locked out for " .. LOCKOUT_TIME .. " seconds.")
- os.sleep(LOCKOUT_TIME)
- return false
- else
- -- Non-admin users are not allowed to log in
- print(NOT_ADMIN_MESSAGE)
- return false
- end
- end
- local function main()
- term.clear()
- term.setCursorPos(1, 1)
- print("Enter username:")
- local enteredUsername = read()
- if checkCredentials(enteredUsername) then
- print("Access granted. Welcome, " .. enteredUsername .. "!")
- shell.run(OS_GUI_PROGRAM) -- Run the OS GUI after successful login
- end
- end
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement