Advertisement
DOGGYWOOF

Untitled

Dec 29th, 2024 (edited)
12
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.00 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. }
  90.  
  91. -- Separate list for protected files and paths
  92. local protectedFiles = {
  93. "/disk/setup",
  94. "/disk/startup",
  95. "/disk/install.lua",
  96. "/disk/install-assist",
  97. "startup",
  98. "no-os" ,
  99. ".settings"
  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() == "exit" then
  110. executeCommand("/disk/os/gui")
  111. return false -- Prevent further command processing
  112. end
  113.  
  114. -- Block specific commands
  115. for _, blocked in ipairs(blockedCommands) do
  116. if command:lower() == blocked then
  117. print("Error: The command '" .. blocked .. "' is not allowed.")
  118. return false
  119. end
  120. end
  121.  
  122. -- Check for protected paths/files
  123. for _, path in ipairs(protectedPaths) do
  124. if isInProtectedPath(command, path) then
  125. -- Prompt for password before proceeding
  126. if not requestPassword("This command may modify critical files. Enter the SUDO password for this PC.") then
  127. return false
  128. end
  129. return true
  130. end
  131. end
  132.  
  133. -- Check for protected files directly
  134. for _, file in ipairs(protectedFiles) do
  135. if command:find(file, 1, true) then
  136. -- Check if the user is allowed to modify the file
  137. if not canModifyFile(file) then
  138. return false
  139. end
  140. -- Prompt for password before proceeding
  141. if not requestPassword("This command involves a protected file. Enter the SUDO password for this PC.") then
  142. return false
  143. end
  144. return true
  145. end
  146. end
  147.  
  148. -- Check if the current directory is within any protected path
  149. if checkDirectoryAgainstProtectedPaths(protectedPaths) then
  150. -- Prompt for password before proceeding
  151. if not requestPassword("You are in a protected directory. Enter the SUDO password for this PC.") then
  152. return false
  153. end
  154. return true
  155. end
  156.  
  157. return true
  158. end
  159.  
  160. -- Function to request password for access control
  161. function requestPassword(warningMessage)
  162. termutils.clear()
  163.  
  164. local width, height = term.getSize()
  165. local boxWidth = 40
  166. local boxHeight = 12 -- Increased box height for error messages
  167. local startX = math.floor((width - boxWidth) / 2)
  168. local startY = math.floor((height - boxHeight) / 2)
  169.  
  170. term.setBackgroundColor(colors.gray)
  171. term.setTextColor(colors.white)
  172.  
  173. -- Draw the box
  174. term.setCursorPos(startX, startY)
  175. write("+" .. string.rep("-", boxWidth - 2) .. "+")
  176.  
  177. for y = startY + 1, startY + boxHeight - 1 do
  178. term.setCursorPos(startX, y)
  179. write("|" .. string.rep(" ", boxWidth - 2) .. "|")
  180. end
  181.  
  182. term.setCursorPos(startX, startY + boxHeight)
  183. write("+" .. string.rep("-", boxWidth - 2) .. "+")
  184.  
  185. term.setBackgroundColor(colors.black)
  186. term.setTextColor(colors.white)
  187.  
  188. -- Display prompts inside the box
  189. local contentX = startX + 2
  190. local contentY = startY + 2
  191.  
  192. term.setCursorPos(contentX, contentY)
  193. write("Doggy OS File System Protection")
  194.  
  195. term.setCursorPos(contentX, contentY + 2)
  196. write(warningMessage)
  197.  
  198. term.setCursorPos(contentX, contentY + 4)
  199. write("Password: ")
  200. term.setTextColor(colors.black) -- Change text color to black to hide the input
  201. local enteredPassword = io.read()
  202. term.setTextColor(colors.white) -- Reset text color
  203.  
  204. -- Verify the password against the file /disk/users/root/password.txt
  205. local isVerified = verifyPassword(enteredPassword)
  206. if not isVerified then
  207. termutils.clear() -- Clear the screen if verification fails
  208. showAdminScreen()
  209. return false
  210. end
  211. return true
  212. end
  213.  
  214. -- Verifies the entered password against the root password file
  215. function verifyPassword(password)
  216. local file = fs.open("/disk/users/root/password.txt", "r")
  217.  
  218. if file then
  219. local correctPassword = file.readAll()
  220. file.close()
  221.  
  222. if password == correctPassword then
  223. return true
  224. else
  225. return false
  226. end
  227. else
  228. return false
  229. end
  230. end
  231.  
  232. -- Admin screen that shows when password is incorrect
  233. function showAdminScreen()
  234. termutils.clear()
  235.  
  236. local width, height = term.getSize()
  237. local boxWidth = 40
  238. local boxHeight = 12
  239. local startX = math.floor((width - boxWidth) / 2)
  240. local startY = math.floor((height - boxHeight) / 2)
  241.  
  242. term.setBackgroundColor(colors.red)
  243. term.setTextColor(colors.white)
  244.  
  245. -- Draw the box
  246. term.setCursorPos(startX, startY)
  247. write("+" .. string.rep("-", boxWidth - 2) .. "+")
  248.  
  249. for y = startY + 1, startY + boxHeight - 1 do
  250. term.setCursorPos(startX, y)
  251. write("|" .. string.rep(" ", boxWidth - 2) .. "|")
  252. end
  253.  
  254. term.setCursorPos(startX, startY + boxHeight)
  255. write("+" .. string.rep("-", boxWidth - 2) .. "+")
  256.  
  257. term.setBackgroundColor(colors.black)
  258. term.setTextColor(colors.white)
  259.  
  260. -- Display prompts inside the box
  261. local contentX = startX + 2
  262. local contentY = startY + 2
  263.  
  264. term.setCursorPos(contentX, contentY)
  265. write("ADMIN ACCESS REQUIRED")
  266.  
  267. term.setCursorPos(contentX, contentY + 2)
  268. write("Incorrect password.")
  269.  
  270. term.setCursorPos(contentX, contentY + 4)
  271. write("Please try again.")
  272. sleep(2) -- Wait for a moment before clearing the screen
  273. termutils.clear()
  274. end
  275.  
  276. -- Checks if the user has permission to modify the file
  277. function canModifyFile(file)
  278. local currentUserFile = "/disk/.currentusr"
  279. local fileContent = fs.open(currentUserFile, "r")
  280. if fileContent then
  281. local content = fileContent.readAll()
  282. fileContent.close()
  283. -- Check if the current user is root
  284. if content:find("root") then
  285. return true
  286. else
  287. return false
  288. end
  289. else
  290. return false
  291. end
  292. end
  293.  
  294. -- Executes the command with error handling
  295. function executeCommand(command)
  296. local success, err = pcall(function() shell.run(command) end)
  297. if not success then
  298. print("Error executing command:", err)
  299. end
  300. end
  301.  
  302. -- Main execution starts here
  303. termutils.clear()
  304. print("Doggy OS Terminal (13.0)")
  305.  
  306. while true do
  307. local command = input()
  308.  
  309. if checkProtection(command) then
  310. executeCommand(command)
  311. else
  312. print("Command aborted.")
  313. end
  314. end
  315.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement