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 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 drawCenteredText(y, text, color)
- local screenWidth, _ = term.getSize()
- local x = math.floor((screenWidth - #text) / 2)
- term.setCursorPos(x, y)
- term.setTextColor(color or colors.white)
- term.write(text)
- end
- local function drawBackground()
- term.setBackgroundColor(colors.gray)
- term.clear()
- end
- local function drawLoginScreen(username, attemptsLeft)
- drawBackground()
- local screenWidth, screenHeight = term.getSize()
- -- Draw icons and colors
- term.setBackgroundColor(colors.blue)
- term.setTextColor(colors.white)
- drawCenteredText(2, "🔒 Doggy OS", colors.lightBlue)
- drawCenteredText(4, "Login Screen", colors.white)
- drawCenteredText(6, "Username: " .. username, colors.yellow)
- drawCenteredText(7, "Attempts left: " .. attemptsLeft, colors.red)
- -- Draw profile picture box
- term.setBackgroundColor(colors.lightGray)
- term.setCursorPos(screenWidth // 2 - 1, screenHeight // 2 - 2)
- term.write(" ")
- term.setCursorPos(screenWidth // 2 - 1, screenHeight // 2 - 1)
- term.write(" ")
- term.setCursorPos(screenWidth // 2 - 1, screenHeight // 2)
- term.write(" ")
- -- Password prompt
- drawCenteredText(screenHeight // 2 + 2, "Enter password:", colors.white)
- term.setCursorPos(screenWidth // 2 - 10, screenHeight // 2 + 3)
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.black)
- term.write(string.rep(" ", 20))
- term.setCursorPos(screenWidth // 2 - 10, screenHeight // 2 + 3)
- 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
- term.clear()
- term.setCursorPos(1, 1)
- term.setTextColor(colors.red)
- print("This user has been disabled due to security reasons.")
- os.sleep(3) -- Display the disabled message for 3 seconds
- print("Contact your administrator for help.")
- os.sleep(2) -- Display the contact administrator message for 2 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
- term.setCursorPos(1, 10)
- term.setTextColor(colors.red)
- print("Incorrect password. Please try again.")
- os.sleep(2) -- Display the error message for 2 seconds
- end
- until attempts > MAX_ATTEMPTS
- print("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()
- term.clear()
- term.setCursorPos(1, 1)
- term.setTextColor(colors.blue)
- term.write("+--------------------------+")
- term.setCursorPos(1, 2)
- term.write("| Insert Security Card |")
- term.setCursorPos(1, 3)
- term.write("+--------------------------+")
- term.setCursorPos(1, 5)
- term.setTextColor(colors.yellow)
- term.write("Please insert your security card.")
- term.setCursorPos(1, 6)
- term.write("Press ENTER to use password instead.")
- end
- local function drawErrorMessage(message)
- term.clear()
- term.setCursorPos(1, 1)
- term.setTextColor(colors.red)
- print(message)
- os.sleep(2)
- 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.")
- end
- end
- end
- end
- local function main()
- term.clear()
- term.setCursorPos(1, 1)
- term.setTextColor(colors.green)
- term.write("+-------------------------------------+")
- term.setCursorPos(1, 2)
- term.write("| Protected by Doggy OS Security |")
- term.setCursorPos(1, 3)
- term.write("+-------------------------------------+")
- term.setCursorPos(1, 5)
- term.setTextColor(colors.white)
- print("Enter username:")
- local enteredUsername = read()
- if checkDisabled(enteredUsername) then
- term.clear()
- term.setCursorPos(1, 1)
- term.setTextColor(colors.red)
- print("This user has been disabled due to security reasons.")
- os.sleep(3) -- Display the disabled message for 3 seconds
- print("Contact your administrator for help.")
- os.sleep(2) -- Display the contact administrator message for 2 seconds
- shell.run("/disk/os/lock.lua") -- Run the lock.lua program
- return
- end
- local users = fs.list(USERS_FOLDER)
- if not users or #users == 0 then
- -- No users detected, run the BSOD program
- shell.run(ERROR_FOLDER .. BSOD_PROGRAM)
- elseif checkSecurityCard(enteredUsername) then
- term.clear()
- term.setCursorPos(1, 1)
- term.setTextColor(colors.lime)
- print("Access granted. Welcome, " .. enteredUsername .. "!")
- os.sleep(2) -- Display the success message for 2 seconds
- term.setTextColor(colors.white)
- shell.run("/disk/os/gui")
- -- Your main OS code goes here
- elseif checkCredentials(enteredUsername) then
- term.clear()
- term.setCursorPos(1, 1)
- term.setTextColor(colors.lime)
- print("Access granted. Welcome, " .. enteredUsername .. "!")
- os.sleep(2) -- Display the success message for 2 seconds
- term.setTextColor(colors.white)
- shell.run("/disk/os/gui")
- -- Your main OS code goes here
- else
- term.clear()
- term.setCursorPos(1, 1)
- term.setTextColor(colors.red)
- print("Access denied.")
- os.sleep(2) -- Display the access denied message for 2 seconds
- shell.run("/disk/os/lock.lua")
- end
- end
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement