Advertisement
DOGGYWOOF

New sh

Dec 15th, 2024 (edited)
11
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.68 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 rootProtectedPaths = {
  76. "/disk/boot/",
  77. "/disk/os/",
  78. "/disk/bootloader/",
  79. "/disk/users/root/"
  80. }
  81. local protectedPaths = {
  82. "/disk/boot/",
  83. "/disk/os/",
  84. "/disk/bootloader/",
  85. "/disk/users/",
  86. "/recovery/", -- Directory path
  87. "/disk/ACPI/", -- Directory path
  88. "/disk/error/", -- Directory path
  89. "/disk/startup", -- Directory path
  90. "/disk/setup", -- Directory path
  91. "/disk/install.lua", -- File path
  92. "/disk/install-assist", -- File path
  93. "startup", -- File name
  94. "no-os", -- File name
  95. "dev.cfg" -- File name
  96. }
  97.  
  98. -- Handle specific commands for reboot and shutdown
  99. if command:lower() == "reboot" then
  100. executeCommand("/disk/ACPI/reboot")
  101. return false -- Prevent further command processing
  102. elseif command:lower() == "shutdown" then
  103. executeCommand("/disk/ACPI/shutdown")
  104. return false -- Prevent further command processing
  105. elseif command:lower():match("^exit$") then
  106. executeCommand("/disk/os/gui")
  107. return false -- Prevent further command processing
  108. elseif command:lower():match("^reboot%s+/s$") then
  109. executeCommand("/disk/ACPI/soft-reboot")
  110. return false -- Prevent further command processing
  111. end
  112.  
  113. -- Block specific commands
  114. for _, blocked in ipairs(blockedCommands) do
  115. if command:lower() == blocked then
  116. print("Error: The command '" .. blocked .. "' is not allowed.")
  117. return false
  118. end
  119. end
  120.  
  121. -- Check for root-protected paths
  122. for _, path in ipairs(rootProtectedPaths) do
  123. if isInProtectedPath(command, path) then
  124. -- Request root credentials for root-protected paths
  125. if not requestRootCredentials("This command may modify critical system files. Proceed with caution.") then
  126. return false
  127. end
  128. return true
  129. end
  130. end
  131.  
  132. -- Check for non-root protected paths and request admin credentials
  133. for _, path in ipairs(protectedPaths) do
  134. if isInProtectedPath(command, path) then
  135. -- Request admin credentials for non-root-protected paths
  136. if not requestAdminCredentials("This command may modify critical files. Proceed with caution.") then
  137. return false
  138. end
  139. return true
  140. end
  141. end
  142.  
  143. return true
  144. end
  145.  
  146. -- Requests admin credentials from the user
  147. function requestAdminCredentials(warningMessage)
  148. termutils.clear()
  149.  
  150. local width, height = term.getSize()
  151. local boxWidth = 40
  152. local boxHeight = 12 -- Increased box height for error messages
  153. local startX = math.floor((width - boxWidth) / 2)
  154. local startY = math.floor((height - boxHeight) / 2)
  155.  
  156. term.setBackgroundColor(colors.gray)
  157. term.setTextColor(colors.white)
  158.  
  159. -- Draw the box
  160. term.setCursorPos(startX, startY)
  161. write("+" .. string.rep("-", boxWidth - 2) .. "+")
  162.  
  163. for y = startY + 1, startY + boxHeight - 1 do
  164. term.setCursorPos(startX, y)
  165. write("|" .. string.rep(" ", boxWidth - 2) .. "|")
  166. end
  167.  
  168. term.setCursorPos(startX, startY + boxHeight)
  169. write("+" .. string.rep("-", boxWidth - 2) .. "+")
  170.  
  171. term.setBackgroundColor(colors.black)
  172. term.setTextColor(colors.white)
  173.  
  174. -- Display prompts inside the box
  175. local contentX = startX + 2
  176. local contentY = startY + 2
  177.  
  178. term.setCursorPos(contentX, contentY)
  179. write("Doggy OS File System Protection")
  180.  
  181. term.setCursorPos(contentX, contentY + 2)
  182. write("Enter Administrator login.")
  183.  
  184. term.setCursorPos(contentX, contentY + 4)
  185. write("Username: ")
  186. local username = io.read()
  187.  
  188. term.setCursorPos(contentX, contentY + 6)
  189. write("Password: ")
  190. term.setTextColor(colors.black) -- Change text color to black to hide the input
  191. local password = io.read()
  192. term.setTextColor(colors.white) -- Reset text color
  193.  
  194. -- Verify credentials and handle access
  195. local isVerified = verifyPassword(username, password)
  196. if not isVerified then
  197. termutils.clear() -- Clear the screen if verification fails
  198. print("Error: Verification failed.")
  199. print(warningMessage)
  200. end
  201. return isVerified
  202. end
  203.  
  204. -- Requests root credentials from the user
  205. function requestRootCredentials(warningMessage)
  206. return requestAdminCredentials(warningMessage) -- Same process as admin, for root
  207. end
  208.  
  209. -- Verifies the entered username and password
  210. function verifyPassword(username, password)
  211. local passwordFilePath = "/disk/users/" .. username .. "/password.txt"
  212. local file = fs.open(passwordFilePath, "r")
  213.  
  214. if file then
  215. local correctPassword = file.readAll()
  216. file.close()
  217.  
  218. if password == correctPassword and userIsAdmin(username) then
  219. return true
  220. else
  221. return false
  222. end
  223. else
  224. return false
  225. end
  226. end
  227.  
  228. -- Checks if the user has admin privileges
  229. function userIsAdmin(username)
  230. local adminFilePath = "/disk/users/" .. username .. "/admin.txt"
  231. local file = fs.open(adminFilePath, "r")
  232.  
  233. if file then
  234. file.close()
  235. return true
  236. else
  237. return false
  238. end
  239. end
  240.  
  241. -- Executes the command with error handling
  242. function executeCommand(command)
  243. local success, err = pcall(function() shell.run(command) end)
  244. if not success then
  245. print("Error executing command:", err)
  246. end
  247. end
  248.  
  249. -- Main execution starts here
  250. termutils.clear()
  251. print("Doggy OS Terminal (13.0)")
  252.  
  253. while true do
  254. local command = input()
  255.  
  256. if checkProtection(command) then
  257. executeCommand(command)
  258. else
  259. print("Command aborted.")
  260. end
  261. end
  262.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement