Advertisement
DOGGYWOOF

sh

Sep 13th, 2024 (edited)
12
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.39 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. -- Detect external disks
  75. detectExternalDisks()
  76.  
  77. local blockedCommands = { "sh", "shell" }
  78. local protectedPaths = {
  79. "/disk/boot/",
  80. "/disk/os/",
  81. "/disk/bootloader/",
  82. "/disk/users/",
  83. "/recovery/",
  84. "/disk/ACPI/",
  85. "/disk/error/",
  86. "/disk/startup",
  87. "/disk/setup",
  88. "/disk/install.lua",
  89. "/disk/install-assist",
  90. "startup",
  91. "no-os",
  92. "dev.cfg"
  93. }
  94.  
  95. -- Separate list for protected files and paths
  96. local protectedFiles = {
  97. "/disk/setup",
  98. "/disk/startup",
  99. "/disk/install.lua",
  100. "/disk/install-assist",
  101. "startup",
  102. "no-os"
  103. }
  104.  
  105. -- Handle specific commands for reboot and shutdown
  106. if command:lower() == "reboot" then
  107. executeCommand("/disk/ACPI/reboot")
  108. return false -- Prevent further command processing
  109. elseif command:lower() == "shutdown" then
  110. executeCommand("/disk/ACPI/shutdown")
  111. return false -- Prevent further command processing
  112. elseif command:lower() == "exit" then
  113. executeCommand("/disk/os/gui")
  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. if not requestAdminCredentials("This command may modify critical files. Proceed with caution.") then
  129. return false
  130. end
  131. return true
  132. end
  133. end
  134.  
  135. -- Check for protected files directly
  136. for _, file in ipairs(protectedFiles) do
  137. if command:find(file, 1, true) then
  138. if not requestAdminCredentials("This command involves a protected file. Proceed with caution.") then
  139. return false
  140. end
  141. return true
  142. end
  143. end
  144.  
  145. -- Check if the current directory is within any protected path
  146. if checkDirectoryAgainstProtectedPaths(protectedPaths) then
  147. if not requestAdminCredentials("You are in a protected directory. Proceed with caution.") then
  148. return false
  149. end
  150. return true
  151. end
  152.  
  153. return true
  154. end
  155.  
  156. -- Requests admin credentials from the user
  157. function requestAdminCredentials(warningMessage)
  158. termutils.clear()
  159.  
  160. local width, height = term.getSize()
  161. local boxWidth = 40
  162. local boxHeight = 12 -- Increased box height for error messages
  163. local startX = math.floor((width - boxWidth) / 2)
  164. local startY = math.floor((height - boxHeight) / 2)
  165.  
  166. term.setBackgroundColor(colors.gray)
  167. term.setTextColor(colors.white)
  168.  
  169. -- Draw the box
  170. term.setCursorPos(startX, startY)
  171. write("+" .. string.rep("-", boxWidth - 2) .. "+")
  172.  
  173. for y = startY + 1, startY + boxHeight - 1 do
  174. term.setCursorPos(startX, y)
  175. write("|" .. string.rep(" ", boxWidth - 2) .. "|")
  176. end
  177.  
  178. term.setCursorPos(startX, startY + boxHeight)
  179. write("+" .. string.rep("-", boxWidth - 2) .. "+")
  180.  
  181. term.setBackgroundColor(colors.black)
  182. term.setTextColor(colors.white)
  183.  
  184. -- Display prompts inside the box
  185. local contentX = startX + 2
  186. local contentY = startY + 2
  187.  
  188. term.setCursorPos(contentX, contentY)
  189. write("Doggy OS File System Protection")
  190.  
  191. term.setCursorPos(contentX, contentY + 2)
  192. write("Enter Administrator login.")
  193.  
  194. term.setCursorPos(contentX, contentY + 4)
  195. write("Username: ")
  196. local username = io.read()
  197.  
  198. term.setCursorPos(contentX, contentY + 6)
  199. write("Password: ")
  200. term.setTextColor(colors.black) -- Change text color to black to hide the input
  201. local password = io.read()
  202. term.setTextColor(colors.white) -- Reset text color
  203.  
  204. -- Verify credentials and handle access
  205. local isVerified = verifyPassword(username, password)
  206. if not isVerified then
  207. termutils.clear() -- Clear the screen if verification fails
  208. print("Error: Verification failed.")
  209. print(warningMessage)
  210. end
  211. return isVerified
  212. end
  213.  
  214. -- Verifies the entered username and password
  215. function verifyPassword(username, password)
  216. local passwordFilePath = "/disk/users/" .. username .. "/password.txt"
  217. local file = fs.open(passwordFilePath, "r")
  218.  
  219. if file then
  220. local correctPassword = file.readAll()
  221. file.close()
  222.  
  223. if password == correctPassword and userIsAdmin(username) then
  224. return true
  225. else
  226. return false
  227. end
  228. else
  229. return false
  230. end
  231. end
  232.  
  233. -- Checks if the user has admin privileges
  234. function userIsAdmin(username)
  235. local adminFilePath = "/disk/users/" .. username .. "/admin.txt"
  236. local file = fs.open(adminFilePath, "r")
  237.  
  238. if file then
  239. file.close()
  240. return true
  241. else
  242. return false
  243. end
  244. end
  245.  
  246. -- Executes the command with error handling
  247. function executeCommand(command)
  248. local success, err = pcall(function() shell.run(command) end)
  249. if not success then
  250. print("Error executing command:", err)
  251. end
  252. end
  253.  
  254. -- Checks if dev.cfg exists in the root directory
  255. function checkDevConfig()
  256. local file = fs.open("/dev.cfg", "r")
  257. if file then
  258. file.close()
  259. return true
  260. else
  261. return false
  262. end
  263. end
  264.  
  265. -- Function to detect new external disks and prompt the user
  266. local function detectExternalDisks()
  267. -- List of possible disk paths
  268. local diskPaths = {"/disk2/", "/disk3/", "/disk4/"}
  269.  
  270. for _, path in ipairs(diskPaths) do
  271. if fs.exists(path) then
  272. termutils.clear()
  273. print("External Storage Connected: " .. path)
  274. print("Launch disk manager? (y/n)")
  275. local response = io.read():lower()
  276. if response == "y" then
  277. executeCommand("/disk/os/dskmgr.lua")
  278. elseif response == "n" then
  279. print("Disk manager not launched.")
  280. else
  281. print("Invalid response. Disk manager not launched.")
  282. end
  283. return -- Exit after handling one detected disk
  284. end
  285. end
  286. end
  287.  
  288. -- Main execution starts here
  289. termutils.clear()
  290. print("Doggy OS Terminal (13.0)")
  291.  
  292. while true do
  293. detectExternalDisks() -- Check for external disks in every loop iteration
  294. local command = input()
  295.  
  296. if checkProtection(command) then
  297. executeCommand(command)
  298. else
  299. print("Command aborted.")
  300. end
  301. end
  302.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement