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 BSOD_PROGRAM = "BSOD.lua"
- local HEADER_COLOR = colors.blue -- Color for the highlighted selection
- -- Utility function to draw a centered popup window with a text-based border
- local function drawPopupWindow(headerText, contentLines)
- term.clear() -- Clear the screen before drawing the popup window
- local w, h = term.getSize()
- local maxLength = #headerText
- for _, line in ipairs(contentLines) do
- maxLength = math.max(maxLength, #line)
- end
- local windowWidth = maxLength + 4 -- Width of the popup window
- local windowHeight = #contentLines + 4 -- Height of the popup window
- local xStart = math.floor(w / 2 - windowWidth / 2)
- local yStart = math.floor(h / 2 - windowHeight / 2)
- -- Draw border
- 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) .. "|")
- 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)
- end
- end
- -- Function to get directories (users) from the USERS_FOLDER
- local function getDirectories(path)
- local directories = {}
- for _, file in ipairs(fs.list(path)) do
- local fullPath = fs.combine(path, file)
- if fs.isDir(fullPath) then
- table.insert(directories, file) -- Add directory to list
- end
- end
- return directories
- end
- 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 drawLoginScreen(username, attemptsLeft)
- local contentLines = {
- "Username: " .. username,
- "Attempts left: " .. attemptsLeft,
- "",
- "Enter password:"
- }
- drawPopupWindow("Login to Doggy OS", contentLines)
- term.setCursorPos(math.floor(term.getSize() / 2) - 2, math.floor(term.getSize() / 2) + 1) -- Position cursor for password input
- end
- local function lockoutUser(username)
- local disabledFile = fs.combine(USERS_FOLDER .. username, "disabled.txt")
- local file = fs.open(disabledFile, "w")
- file.close()
- end
- local function checkDisabled(username)
- local disabledFile = fs.combine(USERS_FOLDER .. username, "disabled.txt")
- return fs.exists(disabledFile)
- end
- local function checkCredentials(username)
- if checkDisabled(username) then
- drawPopupWindow("Access Denied", {"This user has been disabled due to security reasons.", "", "Contact your administrator for help."})
- os.sleep(5) -- Display the disabled message for 5 seconds
- shell.run("/disk/os/lock.lua") -- Run the lock.lua program
- return false
- end
- local storedPassword = getUserCredentials(username)
- if not storedPassword then
- return false -- User does not exist
- end
- local attempts = 0
- repeat
- drawLoginScreen(username, MAX_ATTEMPTS - attempts)
- local enteredPassword = read("*")
- attempts = attempts + 1
- if enteredPassword == storedPassword then
- return true
- else
- drawPopupWindow("Access Denied", {"Incorrect password. Please try again."})
- os.sleep(2) -- Display the error message for 2 seconds
- end
- until attempts > MAX_ATTEMPTS
- drawPopupWindow("Account Disabled", {"Too many incorrect attempts. User has been disabled."})
- lockoutUser(username)
- os.sleep(2) -- Display the lockout message for 2 seconds
- return false
- end
- local function checkDiskIDs()
- -- Get a list of all connected peripherals
- local peripherals = peripheral.getNames()
- -- Array to store disk IDs
- local diskIDs = {}
- -- Loop through all peripherals
- for _, name in ipairs(peripherals) do
- -- Check if the peripheral is a disk drive
- if peripheral.getType(name) == "drive" then
- -- Get the disk ID from the disk drive
- local diskID = disk.getID(name)
- -- If a disk is inserted, add its ID to the array
- if diskID then
- table.insert(diskIDs, {id = diskID, name = name})
- end
- end
- end
- -- Check if any disks were found
- if #diskIDs > 0 then
- return diskIDs
- else
- return nil
- end
- end
- local function drawSecurityCardPrompt()
- local contentLines = {
- "Please insert your security card.",
- "Press ENTER to use password instead."
- }
- drawPopupWindow("Insert Security Card", contentLines)
- end
- local function drawErrorMessage(message)
- drawPopupWindow("Error", {message})
- end
- local function drawMessage(header, message)
- drawPopupWindow(header, {message})
- end
- local function ejectDisk(diskName)
- peripheral.call(diskName, "ejectDisk")
- end
- local function checkSecurityCard(username)
- local idFolder = fs.combine(USERS_FOLDER .. username, "ID")
- if not fs.exists(idFolder) then
- return false
- end
- while true do
- drawSecurityCardPrompt()
- local event, key = os.pullEvent()
- if event == "key" and key == keys.enter then
- return false
- elseif event == "disk" or event == "disk_insert" then
- local diskIDs = checkDiskIDs()
- if diskIDs then
- for _, disk in ipairs(diskIDs) do
- ejectDisk(disk.name) -- Eject the disk
- local idFile = fs.combine(idFolder, tostring(disk.id) .. ".file")
- if fs.exists(idFile) then
- return true -- Allow access if a valid ID is found
- end
- end
- drawErrorMessage("Error: Unregistered security key.")
- os.sleep(2)
- end
- end
- end
- end
- local function selectUserFromList()
- local users = getDirectories(USERS_FOLDER) -- Only get directories as users
- if #users == 0 then
- drawPopupWindow("No Users Found", {"No user directories found.", "Please create a user directory."})
- os.sleep(3)
- return
- end
- local selectedIndex = 1
- local totalUsers = #users
- while true do
- local contentLines = {
- "Select your user account:"
- }
- for i, user in ipairs(users) do
- if i == selectedIndex then
- -- Highlight the selected user
- term.setBackgroundColor(HEADER_COLOR) -- Blue background for selection
- term.setTextColor(colors.white) -- White text for contrast
- table.insert(contentLines, "> " .. user)
- else
- -- Normal color for non-selected users
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- table.insert(contentLines, " " .. user)
- end
- end
- -- Redraw the popup window with updated list
- term.setTextColor(colors.white) -- Reset text color to white
- term.setBackgroundColor(colors.black) -- Reset background color to black
- drawPopupWindow("Select User", contentLines)
- -- Handle keyboard input
- local event, key = os.pullEvent()
- if event == "key" then
- if key == keys.down then
- selectedIndex = math.min(selectedIndex + 1, totalUsers)
- elseif key == keys.up then
- selectedIndex = math.max(selectedIndex - 1, 1)
- elseif key == keys.enter then
- return users[selectedIndex] -- Return selected user
- end
- end
- end
- end
- local function main()
- -- Set the text color to white and the background color to black
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.black)
- term.clear()
- drawPopupWindow("Protected by Doggy OS Security", {"Select a user:"})
- local enteredUsername = selectUserFromList()
- if enteredUsername then
- if checkCredentials(enteredUsername) then
- -- Allow user to proceed after successful login or security card insert
- drawMessage("Login Success", "Welcome, " .. enteredUsername)
- end
- else
- drawErrorMessage("No user selected. Exiting.")
- os.sleep(2)
- end
- end
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement