Advertisement
DOGGYWOOF

Untitled

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