Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Function to handle errors more effectively for shell commands
- local function safeShellRun(command, ...)
- local success, result = pcall(shell.run, command, ...)
- if not success then
- print("Error: " .. result) -- Output the error message from pcall
- return false
- end
- return true
- end
- -- Function to trim any trailing slashes from a path
- local function trimTrailingSlash(path)
- return path:match("^(.-)/?$") -- Ensure leading slash is maintained
- end
- -- Function to check if the current directory is within any protected path
- local function checkDirectoryAgainstProtectedPaths(protectedPaths)
- local currentDirectory = trimTrailingSlash(shell.dir())
- if not currentDirectory:match("^/") then
- currentDirectory = "/" .. currentDirectory
- end
- print("You are currently in directory: " .. currentDirectory)
- for _, path in ipairs(protectedPaths) do
- local protectedPath = trimTrailingSlash(path)
- if not protectedPath:match("^/") then
- protectedPath = "/" .. protectedPath
- end
- if currentDirectory == protectedPath or currentDirectory:find("^" .. protectedPath, 1, true) then
- return true
- end
- end
- return false
- end
- termutils = {}
- termutils.clear = function()
- term.clear()
- term.setCursorPos(1, 1)
- end
- termutils.clearColor = function()
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.black)
- end
- function input()
- term.setTextColor(colors.blue)
- local dir = shell.dir() .. "/> "
- write(dir)
- termutils.clearColor()
- return io.read()
- end
- function isInProtectedPath(command, path)
- if not path:match("^/") then
- path = "/" .. path
- end
- if not command:match("^/") then
- command = "/" .. command
- end
- return command:find(path, 1, true) ~= nil
- end
- function checkProtection(command)
- local blockedCommands = { "sh", "shell" }
- local protectedPaths = {
- "/disk/boot/",
- "/disk/os/",
- "/disk/bootloader/",
- "/disk/users/",
- "/recovery/",
- "/disk/ACPI/",
- "/disk/error/",
- "/disk/startup",
- "/disk/setup",
- "/disk/install.lua",
- "/disk/install-assist",
- "startup",
- "no-os",
- "dev.cfg"
- }
- local protectedFiles = {
- "/disk/setup",
- "/disk/startup",
- "/disk/install.lua",
- "/disk/install-assist",
- "startup",
- "no-os",
- ".settings"
- }
- if command:lower() == "reboot" then
- safeShellRun("/disk/ACPI/reboot")
- return false
- elseif command:lower() == "shutdown" then
- safeShellRun("/disk/ACPI/shutdown")
- return false
- elseif command:lower() == "exit" then
- safeShellRun("/disk/os/gui")
- return false
- end
- for _, blocked in ipairs(blockedCommands) do
- if command:lower() == blocked then
- print("Error: The command '" .. blocked .. "' is not allowed.")
- return false
- end
- end
- for _, path in ipairs(protectedPaths) do
- if isInProtectedPath(command, path) then
- if not requestAdminCredentials("This command may modify critical files. Proceed with caution.") then
- return false
- end
- return true
- end
- end
- for _, file in ipairs(protectedFiles) do
- if command:find(file, 1, true) then
- if not requestAdminCredentials("This command involves a protected file. Proceed with caution.") then
- return false
- end
- return true
- end
- end
- if checkDirectoryAgainstProtectedPaths(protectedPaths) then
- if not requestAdminCredentials("You are in a protected directory. Proceed with caution.") then
- return false
- end
- return true
- end
- return true
- end
- function requestAdminCredentials(warningMessage)
- termutils.clear()
- local width, height = term.getSize()
- local boxWidth = 40
- local boxHeight = 12
- local startX = math.floor((width - boxWidth) / 2)
- local startY = math.floor((height - boxHeight) / 2)
- term.setBackgroundColor(colors.gray)
- term.setTextColor(colors.white)
- 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)
- 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)
- local password = io.read()
- term.setTextColor(colors.white)
- local isVerified = verifyPassword(username, password)
- if not isVerified then
- termutils.clear()
- print("Error: Verification failed.")
- print(warningMessage)
- end
- return isVerified
- end
- function verifyPassword(username, password)
- local passwordFilePath = "/disk/users/" .. username .. "/password.txt"
- local file, err = fs.open(passwordFilePath, "r")
- if not file then
- print("Error: Unable to open password file - " .. err)
- return false
- end
- local correctPassword = file.readAll()
- file.close()
- if password == correctPassword and userIsAdmin(username) then
- return true
- else
- return false
- end
- end
- function userIsAdmin(username)
- local adminFilePath = "/disk/users/" .. username .. "/admin.txt"
- local file, err = fs.open(adminFilePath, "r")
- if not file then
- print("Error: Unable to open admin file - " .. err)
- return false
- end
- file.close()
- return true
- end
- -- Main execution starts here
- termutils.clear()
- print("Doggy OS Terminal (13.0)")
- while true do
- local command = input()
- if checkProtection(command) then
- safeShellRun(command)
- else
- print("Command aborted.")
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement