Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Constants for security configuration
- 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 BSOD_PROGRAM = "BSOD.lua"
- local CURRENT_USER_FILE = ".currentusr"
- local SHOW_ALL_USERS_FILE = "/disk/config/security/login/ShowAllUsers.cfg"
- -- Utility function to draw a centered popup window with a text-based border
- local function drawPopupWindow(headerText, contentLines, windowWidth, windowHeight)
- term.clear()
- local w, h = term.getSize()
- -- Determine dimensions of the popup
- local maxLength = #headerText
- for _, line in ipairs(contentLines) do
- maxLength = math.max(maxLength, #line)
- end
- windowWidth = windowWidth or (maxLength + 4)
- windowHeight = windowHeight or (#contentLines + 4)
- local xStart = math.floor(w / 2 - windowWidth / 2)
- local yStart = math.floor(h / 2 - windowHeight / 2)
- -- Draw border with animation
- term.setCursorPos(xStart, yStart)
- term.write("+" .. string.rep("-", windowWidth - 2) .. "+")
- for i = 1, windowHeight - 2 do
- term.setCursorPos(xStart, yStart + i)
- term.write("|" .. string.rep(" ", windowWidth - 2) .. "|")
- os.sleep(0.05)
- end
- term.setCursorPos(xStart, yStart + windowHeight - 1)
- term.write("+" .. string.rep("-", windowWidth - 2) .. "+")
- -- Draw header
- term.setCursorPos(xStart + 2, yStart + 1)
- term.write(headerText)
- -- Draw content
- for i, line in ipairs(contentLines) do
- term.setCursorPos(xStart + 2, yStart + 1 + i + 1)
- term.write(line)
- os.sleep(0.05)
- end
- end
- -- Function to reset the password
- local function resetPassword(username)
- -- Check for recovery methods: security card or recovery key
- local idFolder = fs.combine(USERS_FOLDER .. username, "ID")
- local recoveryKeyFile = fs.combine(USERS_FOLDER .. username, "rckey.txt")
- local resetMethod = nil
- -- Prompt for reset method
- drawPopupWindow("Password Reset Options", {
- "Choose one of the following methods:",
- "1. Insert a valid security card.",
- "2. Enter your recovery key."
- })
- while true do
- local event, key = os.pullEvent("key")
- if key == keys.one then
- resetMethod = "card"
- break
- elseif key == keys.two then
- resetMethod = "key"
- break
- end
- end
- if resetMethod == "card" then
- -- Attempt security card verification
- while true do
- drawPopupWindow("Insert Security Card", {"Insert a valid security card to reset your password."})
- local event = os.pullEvent()
- if event == "disk" or event == "disk_insert" then
- local diskIDs = checkDiskIDs()
- if diskIDs then
- for _, disk in ipairs(diskIDs) do
- ejectDisk(disk.name)
- local idFile = fs.combine(idFolder, tostring(disk.id) .. ".file")
- if fs.exists(idFile) then
- drawPopupWindow("Card Verified", {"Security card accepted!"})
- os.sleep(2)
- goto setNewPassword
- end
- end
- end
- drawPopupWindow("Invalid Card", {"The security card is not valid."})
- os.sleep(2)
- end
- end
- elseif resetMethod == "key" then
- -- Attempt recovery key verification
- if not fs.exists(recoveryKeyFile) then
- drawPopupWindow("Error", {"No recovery key file found for this user."})
- os.sleep(2)
- return
- end
- local file = fs.open(recoveryKeyFile, "r")
- local storedRecoveryKey = file.readLine()
- file.close()
- drawPopupWindow("Recovery Key Verification", {"Enter your recovery key:"})
- term.setCursorPos(1, select(2, term.getSize()))
- term.write(":")
- local enteredKey = read("*")
- if enteredKey ~= storedRecoveryKey then
- drawPopupWindow("Invalid Recovery Key", {"The recovery key you entered is incorrect."})
- os.sleep(2)
- return
- end
- drawPopupWindow("Recovery Key Verified", {"Recovery key accepted!"})
- os.sleep(2)
- end
- ::setNewPassword::
- -- Set a new password for the user
- drawPopupWindow("Set New Password", {"Enter a new password for user: " .. username})
- term.setCursorPos(1, select(2, term.getSize()))
- term.write(":")
- local newPassword = read("*")
- -- Save the new password
- local passwordFile = fs.combine(USERS_FOLDER .. username, "password.txt")
- local file = fs.open(passwordFile, "w")
- file.write(newPassword)
- file.close()
- drawPopupWindow("Password Reset", {"Password has been successfully reset!"})
- os.sleep(2)
- -- Automatically log in the user after resetting the password
- saveCurrentUser(username)
- shell.run("/disk/os/gui")
- end
- -- Other functions remain unchanged, except for integrating resetPassword into the flow
- -- Main function for login process
- local function main()
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.black)
- term.clear()
- local username
- if fs.exists(SHOW_ALL_USERS_FILE) then
- username = selectUserFromList()
- else
- drawPopupWindow("Protected by Doggy OS Security", {"Enter username:"})
- username = read()
- end
- -- Attempt security card login first
- if insertSecurityCard(username) then
- drawPopupWindow("Access Granted", {"Security card verified. Welcome, " .. username .. "!"})
- os.sleep(2)
- saveCurrentUser(username)
- shell.run("/disk/os/gui")
- return
- end
- -- Fallback to password login if card verification fails or is bypassed
- if checkCredentials(username) then
- drawPopupWindow("Access Granted", {"Welcome, " .. username .. "!"})
- os.sleep(2)
- shell.run("/disk/os/gui")
- else
- shell.run("/disk/os/lock.lua")
- end
- end
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement