Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- termutils = {}
- -- Clears the terminal and resets the cursor position
- termutils.clear = function()
- term.clear()
- term.setCursorPos(1, 1)
- end
- -- Resets text and background colors to default
- termutils.clearColor = function()
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.black)
- end
- -- Function to display the current directory
- local function showCurrentDirectory()
- -- Get the current working directory from the shell API
- local currentDirectory = shell.dir()
- -- Display the current directory
- print("You are currently in directory: " .. currentDirectory)
- -- Check if the current directory is a protected path
- if isInProtectedPath(currentDirectory) then
- print("Warning: You are in a protected directory.")
- if not requestAdminCredentials() then
- return false -- Stop if credentials are incorrect
- end
- end
- return true
- end
- -- Prompts the user for input and returns the entered value
- function input()
- term.setTextColor(colors.blue)
- local dir = shell.dir() .. "/> "
- write(dir)
- termutils.clearColor()
- return io.read()
- end
- -- Checks if the command involves protected paths or files and handles protection
- function checkProtection(command)
- local blockedCommands = { "sh", "shell" }
- local protectedPaths = {
- "/disk/boot/",
- "/disk/os/",
- "/disk/bootloader/",
- "/disk/users/",
- "/recovery/", -- Directory path
- "/disk/ACPI/", -- Directory path
- "/disk/error/", -- Directory path
- "/disk/startup", -- Directory path
- "/disk/setup", -- Directory path
- "/disk/install.lua", -- File path
- "/disk/install-assist", -- File path
- "startup", -- File name
- "no-os", -- File name
- "dev.cfg" -- File name
- }
- -- Handle specific commands for reboot and shutdown
- if command:lower() == "reboot" then
- executeCommand("/disk/ACPI/reboot")
- return false -- Prevent further command processing
- elseif command:lower() == "shutdown" then
- executeCommand("/disk/ACPI/shutdown")
- return false -- Prevent further command processing
- end
- -- Block specific commands
- for _, blocked in ipairs(blockedCommands) do
- if command:lower() == blocked then
- print("Error: The command '" .. blocked .. "' is not allowed.")
- return false
- end
- end
- -- Show current directory and check protection
- if not showCurrentDirectory() then
- return false
- end
- -- Check for protected paths/files
- for _, path in ipairs(protectedPaths) do
- if isInProtectedPath(command, path) then
- if checkDevConfig() then
- print("Failed to verify system... continuing with shell command")
- end
- if not requestAdminCredentials() then
- return false
- end
- print("Warning: This command may modify critical files. Proceed with caution.")
- return true
- end
- end
- return true
- end
- -- Checks if the command or directory involves a protected path or its subdirectories
- function isInProtectedPath(commandOrPath, path)
- local protectedPaths = {
- "/disk/boot/",
- "/disk/os/",
- "/disk/bootloader/",
- "/disk/users/",
- "/recovery/", -- Directory path
- "/disk/ACPI/", -- Directory path
- "/disk/error/", -- Directory path
- "/disk/startup", -- Directory path
- "/disk/setup", -- Directory path
- "/disk/install.lua", -- File path
- "/disk/install-assist", -- File path
- "startup", -- File name
- "no-os", -- File name
- "dev.cfg" -- File name
- }
- -- Check if the commandOrPath starts with or matches any protected path
- for _, path in ipairs(protectedPaths) do
- if commandOrPath:find(path, 1, true) == 1 then
- return true
- end
- end
- return false
- end
- -- Requests admin credentials from the user
- function requestAdminCredentials()
- termutils.clear()
- local width, height = term.getSize()
- local boxWidth = 40
- local boxHeight = 10
- local startX = math.floor((width - boxWidth) / 2)
- local startY = math.floor((height - boxHeight) / 2)
- term.setBackgroundColor(colors.gray)
- term.setTextColor(colors.white)
- -- Draw the box
- term.setCursorPos(startX, startY)
- write("+" .. string.rep("-", boxWidth - 2) .. "+")
- for y = startY + 1, startY + boxHeight - 1 do
- term.setCursorPos(startX, y)
- write("|" .. string.rep(" ", boxWidth - 2) .. "|")
- end
- term.setCursorPos(startX, startY + boxHeight)
- write("+" .. string.rep("-", boxWidth - 2) .. "+")
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- -- Display prompts inside the box
- local contentX = startX + 2
- local contentY = startY + 2
- term.setCursorPos(contentX, contentY)
- write("Doggy OS File System Protection")
- term.setCursorPos(contentX, contentY + 2)
- write("Enter Administrator login.")
- term.setCursorPos(contentX, contentY + 4)
- write("Username: ")
- local username = io.read()
- term.setCursorPos(contentX, contentY + 6)
- write("Password: ")
- term.setTextColor(colors.black) -- Change text color to black to hide the input
- local password = io.read()
- term.setTextColor(colors.white) -- Reset text color
- -- Verify credentials and handle access
- local isVerified = verifyPassword(username, password)
- if not isVerified then
- termutils.clear() -- Clear the screen if verification fails
- end
- return isVerified
- end
- -- Verifies the entered username and password
- function verifyPassword(username, password)
- local passwordFilePath = "/disk/users/" .. username .. "/password.txt"
- local file = fs.open(passwordFilePath, "r")
- if file then
- local correctPassword = file.readAll()
- file.close()
- if password == correctPassword and userIsAdmin(username) then
- return true
- else
- print("Invalid credentials or insufficient privileges.")
- return false
- end
- else
- print("User not found.")
- return false
- end
- end
- -- Checks if the user has admin privileges
- function userIsAdmin(username)
- local adminFilePath = "/disk/users/" .. username .. "/admin.txt"
- local file = fs.open(adminFilePath, "r")
- if file then
- file.close()
- return true
- else
- return false
- end
- end
- -- Executes the command with error handling
- function executeCommand(command)
- local success, err = pcall(function() shell.run(command) end)
- if not success then
- print("Error executing command:", err)
- end
- end
- -- Checks if dev.cfg exists in the root directory
- function checkDevConfig()
- local file = fs.open("/dev.cfg", "r")
- if file then
- file.close()
- return true
- else
- return false
- end
- end
- -- Main execution starts here
- termutils.clear()
- print("Doggy OS Terminal (13.0)")
- while true do
- local command = input()
- if checkProtection(command) then
- executeCommand(command)
- else
- print("Command aborted.")
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement