Advertisement
DOGGYWOOF

Shell

Oct 6th, 2024 (edited)
12
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.10 KB | None | 0 0
  1. -- Function to trim any trailing slashes from a path
  2. local function trimTrailingSlash(path)
  3. return path:match("^(.-)/?$") -- Ensure leading slash is maintained
  4. end
  5.  
  6. -- Function to check if the current directory is within any protected path
  7. local function checkDirectoryAgainstProtectedPaths(protectedPaths)
  8. -- Get the current working directory from the shell API
  9. local currentDirectory = trimTrailingSlash(shell.dir())
  10.  
  11. -- Ensure the current directory has a leading slash
  12. if not currentDirectory:match("^/") then
  13. currentDirectory = "/" .. currentDirectory
  14. end
  15.  
  16. -- Display the current directory
  17. print("You are currently in directory: " .. currentDirectory)
  18.  
  19. -- Check if the current directory is within any protected paths
  20. for _, path in ipairs(protectedPaths) do
  21. -- Ensure path has a leading slash
  22. local protectedPath = trimTrailingSlash(path)
  23. if not protectedPath:match("^/") then
  24. protectedPath = "/" .. protectedPath
  25. end
  26.  
  27. if currentDirectory == protectedPath or currentDirectory:find("^" .. protectedPath, 1, true) then
  28. return true
  29. end
  30. end
  31.  
  32. return false
  33. end
  34.  
  35. termutils = {}
  36.  
  37. -- Clears the terminal and resets the cursor position
  38. termutils.clear = function()
  39. term.clear()
  40. term.setCursorPos(1, 1)
  41. end
  42.  
  43. -- Resets text and background colors to default
  44. termutils.clearColor = function()
  45. term.setTextColor(colors.white)
  46. term.setBackgroundColor(colors.black)
  47. end
  48.  
  49. -- Prompts the user for input and returns the entered value
  50. function input()
  51. term.setTextColor(colors.blue)
  52. local dir = shell.dir() .. "/> "
  53. write(dir)
  54. termutils.clearColor()
  55. return io.read()
  56. end
  57.  
  58. -- Checks if the command involves a protected path or its subdirectories
  59. function isInProtectedPath(command, path)
  60. -- Ensure both path and command have leading slashes
  61. if not path:match("^/") then
  62. path = "/" .. path
  63. end
  64. if not command:match("^/") then
  65. command = "/" .. command
  66. end
  67.  
  68. -- Check if the command starts with the protected path or if the protected path is found in the command
  69. return command:find(path, 1, true) ~= nil
  70. end
  71.  
  72. -- Checks if the command involves protected paths or files and handles protection
  73. function checkProtection(command)
  74. local blockedCommands = { "sh", "shell" }
  75. local protectedPaths = {
  76. "/disk/boot/",
  77. "/disk/os/",
  78. "/disk/bootloader/",
  79. "/disk/users/",
  80. "/recovery/", -- Directory path
  81. "/disk/ACPI/", -- Directory path
  82. "/disk/error/", -- Directory path
  83. "/disk/startup", -- Directory path
  84. "/disk/setup", -- Directory path
  85. "/disk/install.lua", -- File path
  86. "/disk/install-assist", -- File path
  87. "startup", -- File name
  88. "no-os", -- File name
  89. "dev.cfg" -- File name
  90. }
  91.  
  92. -- Separate list for protected files and paths
  93. local protectedFiles = {
  94. "/disk/setup",
  95. "/disk/startup",
  96. "/disk/install.lua",
  97. "/disk/install-assist",
  98. "startup",
  99. "no-os"
  100. }
  101.  
  102. -- Handle specific commands for reboot and shutdown
  103. if command:lower() == "reboot" then
  104. executeCommand("/disk/ACPI/reboot")
  105. return false -- Prevent further command processing
  106. elseif command:lower() == "shutdown" then
  107. executeCommand("/disk/ACPI/shutdown")
  108. return false -- Prevent further command processing
  109. elseif command:lower():match("^exit$") then
  110. executeCommand("/disk/os/gui")
  111. return false -- Prevent further command processing
  112. elseif command:lower():match("^reboot%s+/s$") then
  113. executeCommand("/disk/ACPI/soft-reboot")
  114. return false -- Prevent further command processing
  115. end
  116.  
  117. -- Block specific commands
  118. for _, blocked in ipairs(blockedCommands) do
  119. if command:lower() == blocked then
  120. print("Error: The command '" .. blocked .. "' is not allowed.")
  121. return false
  122. end
  123. end
  124.  
  125. -- Check for protected paths/files
  126. for _, path in ipairs(protectedPaths) do
  127. if isInProtectedPath(command, path) then
  128. -- Show the FS protection screen and request admin credentials
  129. if not requestAdminCredentials("This command may modify critical files. Proceed with caution.") then
  130. return false
  131. end
  132. return true
  133. end
  134. end
  135.  
  136. -- Check for protected files directly
  137. for _, file in ipairs(protectedFiles) do
  138. if command:find(file, 1, true) then
  139. -- Show the FS protection screen and request admin credentials
  140. if not requestAdminCredentials("This command involves a protected file. Proceed with caution.") then
  141. return false
  142. end
  143. return true
  144. end
  145. end
  146.  
  147. -- Check if the current directory is within any protected path
  148. if checkDirectoryAgainstProtectedPaths(protectedPaths) then
  149. -- Show the FS protection screen and request admin credentials
  150. if not requestAdminCredentials("You are in a protected directory. Proceed with caution.") then
  151. return false
  152. end
  153. return true
  154. end
  155.  
  156. return true
  157. end
  158.  
  159. -- Requests admin credentials from the user
  160. function requestAdminCredentials(warningMessage)
  161. termutils.clear()
  162.  
  163. local width, height = term.getSize()
  164. local boxWidth = 40
  165. local boxHeight = 12 -- Increased box height for error messages
  166. local startX = math.floor((width - boxWidth) / 2)
  167. local startY = math.floor((height - boxHeight) / 2)
  168.  
  169. term.setBackgroundColor(colors.gray)
  170. term.setTextColor(colors.white)
  171.  
  172. -- Draw the box
  173. term.setCursorPos(startX, startY)
  174. write("+" .. string.rep("-", boxWidth - 2) .. "+")
  175.  
  176. for y = startY + 1, startY + boxHeight - 1 do
  177. term.setCursorPos(startX, y)
  178. write("|" .. string.rep(" ", boxWidth - 2) .. "|")
  179. end
  180.  
  181. term.setCursorPos(startX, startY + boxHeight)
  182. write("+" .. string.rep("-", boxWidth - 2) .. "+")
  183.  
  184. term.setBackgroundColor(colors.black)
  185. term.setTextColor(colors.white)
  186.  
  187. -- Display prompts inside the box
  188. local contentX = startX + 2
  189. local contentY = startY + 2
  190.  
  191. term.setCursorPos(contentX, contentY)
  192. write("Doggy OS File System Protection")
  193.  
  194. term.setCursorPos(contentX, contentY + 2)
  195. write("Enter Administrator login.")
  196.  
  197. term.setCursorPos(contentX, contentY + 4)
  198. write("Username: ")
  199. local username = io.read()
  200.  
  201. term.setCursorPos(contentX, contentY + 6)
  202. write("Password: ")
  203. term.setTextColor(colors.black) -- Change text color to black to hide the input
  204. local password = io.read()
  205. term.setTextColor(colors.white) -- Reset text color
  206.  
  207. -- Verify credentials and handle access
  208. local isVerified = verifyPassword(username, password)
  209. if not isVerified then
  210. termutils.clear() -- Clear the screen if verification fails
  211. print("Error: Verification failed.")
  212. print(warningMessage)
  213. end
  214. return isVerified
  215. end
  216.  
  217. -- Verifies the entered username and password
  218. function verifyPassword(username, password)
  219. local passwordFilePath = "/disk/users/" .. username .. "/password.txt"
  220. local file = fs.open(passwordFilePath, "r")
  221.  
  222. if file then
  223. local correctPassword = file.readAll()
  224. file.close()
  225.  
  226. if password == correctPassword and userIsAdmin(username) then
  227. return true
  228. else
  229. return false
  230. end
  231. else
  232. return false
  233. end
  234. end
  235.  
  236. -- Checks if the user has admin privileges
  237. function userIsAdmin(username)
  238. local adminFilePath = "/disk/users/" .. username .. "/admin.txt"
  239. local file = fs.open(adminFilePath, "r")
  240.  
  241. if file then
  242. file.close()
  243. return true
  244. else
  245. return false
  246. end
  247. end
  248.  
  249. -- Executes the command with error handling
  250. function executeCommand(command)
  251. local success, err = pcall(function() shell.run(command) end)
  252. if not success then
  253. print("Error executing command:", err)
  254. end
  255. end
  256.  
  257. -- Checks if dev.cfg exists in the root directory
  258. function checkDevConfig()
  259. local file = fs.open("/dev.cfg", "r")
  260. if file then
  261. file.close()
  262. return true
  263. else
  264. return false
  265. end
  266. end
  267.  
  268. -- Main execution starts here
  269. termutils.clear()
  270. print("Doggy OS Terminal (13.0)")
  271.  
  272. while true do
  273. local command = input()
  274.  
  275. if checkProtection(command) then
  276. executeCommand(command)
  277. else
  278. print("Command aborted.")
  279. end
  280. end
  281.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement