Advertisement
DOGGYWOOF

Untitled

Jan 3rd, 2025 (edited)
12
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.29 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. -- Function to display a generic error UI
  50. function showErrorUI(message)
  51. termutils.clear()
  52.  
  53. local width, height = term.getSize()
  54. local boxWidth = 40
  55. local boxHeight = 12 -- Increased box height for error messages
  56. local startX = math.floor((width - boxWidth) / 2)
  57. local startY = math.floor((height - boxHeight) / 2)
  58.  
  59. term.setBackgroundColor(colors.red)
  60. term.setTextColor(colors.white)
  61.  
  62. -- Draw the box
  63. term.setCursorPos(startX, startY)
  64. write("+" .. string.rep("-", boxWidth - 2) .. "+")
  65.  
  66. for y = startY + 1, startY + boxHeight - 1 do
  67. term.setCursorPos(startX, y)
  68. write("|" .. string.rep(" ", boxWidth - 2) .. "|")
  69. end
  70.  
  71. term.setCursorPos(startX, startY + boxHeight)
  72. write("+" .. string.rep("-", boxWidth - 2) .. "+")
  73.  
  74. term.setBackgroundColor(colors.black)
  75. term.setTextColor(colors.white)
  76.  
  77. -- Display the error message inside the box
  78. local contentX = startX + 2
  79. local contentY = startY + 2
  80.  
  81. term.setCursorPos(contentX, contentY)
  82. write("Error: " .. message)
  83.  
  84. term.setCursorPos(contentX, contentY + 2)
  85. write("Press any key to continue.")
  86.  
  87. os.pullEvent("key")
  88. termutils.clear()
  89. end
  90.  
  91. -- Prompts the user for input and returns the entered value
  92. function input()
  93. term.setTextColor(colors.blue)
  94. local dir = shell.dir() .. "/> "
  95. write(dir)
  96. termutils.clearColor()
  97. return io.read()
  98. end
  99.  
  100. -- Checks if the command involves a protected path or its subdirectories
  101. function isInProtectedPath(command, path)
  102. -- Ensure both path and command have leading slashes
  103. if not path:match("^/") then
  104. path = "/" .. path
  105. end
  106. if not command:match("^/") then
  107. command = "/" .. command
  108. end
  109.  
  110. -- Check if the command starts with the protected path or if the protected path is found in the command
  111. return command:find(path, 1, true) ~= nil
  112. end
  113.  
  114. -- Checks if the command involves protected paths or files and handles protection
  115. function checkProtection(command)
  116. local blockedCommands = { "sh", "shell" }
  117. local protectedPaths = {
  118. "/disk/boot/",
  119. "/disk/os/",
  120. "/disk/bootloader/",
  121. "/disk/users/",
  122. "/recovery/", -- Directory path
  123. "/disk/ACPI/", -- Directory path
  124. "/disk/error/", -- Directory path
  125. "/disk/startup", -- Directory path
  126. "/disk/setup", -- Directory path
  127. "/disk/install.lua", -- File path
  128. "/disk/install-assist", -- File path
  129. "startup", -- File name
  130. "no-os", -- File name
  131. "dev.cfg" -- File name
  132. }
  133.  
  134. -- Separate list for protected files and paths
  135. local protectedFiles = {
  136. "/disk/setup",
  137. "/disk/startup",
  138. "/disk/install.lua",
  139. "/disk/install-assist",
  140. "startup",
  141. "no-os" ,
  142. ".settings"
  143. }
  144.  
  145. -- Handle specific commands for reboot and shutdown
  146. if command:lower() == "reboot" then
  147. executeCommand("/disk/ACPI/reboot")
  148. return false -- Prevent further command processing
  149. elseif command:lower() == "shutdown" then
  150. executeCommand("/disk/ACPI/shutdown")
  151. return false -- Prevent further command processing
  152. elseif command:lower() == "exit" then
  153. executeCommand("/disk/os/gui")
  154. return false -- Prevent further command processing
  155. end
  156.  
  157. -- Block specific commands
  158. for _, blocked in ipairs(blockedCommands) do
  159. if command:lower() == blocked then
  160. showErrorUI("The command '" .. blocked .. "' is not allowed.")
  161. return false
  162. end
  163. end
  164.  
  165. -- Check for protected paths/files
  166. for _, path in ipairs(protectedPaths) do
  167. if isInProtectedPath(command, path) then
  168. -- Show the FS protection screen and request admin credentials
  169. if not requestAdminCredentials("This command may modify critical files. Proceed with caution.") then
  170. return false
  171. end
  172. return true
  173. end
  174. end
  175.  
  176. -- Check for protected files directly
  177. for _, file in ipairs(protectedFiles) do
  178. if command:find(file, 1, true) then
  179. -- Check if the user is allowed to modify the file
  180. if not canModifyFile(file) then
  181. showErrorUI("Access denied to the file: " .. file)
  182. return false
  183. end
  184. -- Show the FS protection screen and request admin credentials
  185. if not requestAdminCredentials("This command involves a protected file. Proceed with caution.") then
  186. return false
  187. end
  188. return true
  189. end
  190. end
  191.  
  192. -- Check if the current directory is within any protected path
  193. if checkDirectoryAgainstProtectedPaths(protectedPaths) then
  194. -- Show the FS protection screen and request admin credentials
  195. if not requestAdminCredentials("You are in a protected directory. Proceed with caution.") then
  196. return false
  197. end
  198. return true
  199. end
  200.  
  201. return true
  202. end
  203.  
  204. -- Function to display the access denied GUI
  205. function showAccessDeniedGUI()
  206. termutils.clear()
  207.  
  208. local width, height = term.getSize()
  209. local boxWidth = 40
  210. local boxHeight = 12 -- Increased box height for error messages
  211. local startX = math.floor((width - boxWidth) / 2)
  212. local startY = math.floor((height - boxHeight) / 2)
  213.  
  214. term.setBackgroundColor(colors.red)
  215. term.setTextColor(colors.white)
  216.  
  217. -- Draw the box
  218. term.setCursorPos(startX, startY)
  219. write("+" .. string.rep("-", boxWidth - 2) .. "+")
  220.  
  221. for y = startY + 1, startY + boxHeight - 1 do
  222. term.setCursorPos(startX, y)
  223. write("|" .. string.rep(" ", boxWidth - 2) .. "|")
  224. end
  225.  
  226. term.setCursorPos(startX, startY + boxHeight)
  227. write("+" .. string.rep("-", boxWidth - 2) .. "+")
  228.  
  229. term.setBackgroundColor(colors.black)
  230. term.setTextColor(colors.white)
  231.  
  232. -- Display prompts inside the box
  233. local contentX = startX + 2
  234. local contentY = startY + 2
  235.  
  236. term.setCursorPos(contentX, contentY)
  237. write("Doggy OS File System Protection")
  238.  
  239. term.setCursorPos(contentX, contentY + 2)
  240. write("Access Denied")
  241.  
  242. term.setCursorPos(contentX, contentY + 4)
  243. write("Press any key to continue.")
  244.  
  245. os.pullEvent("key")
  246. termutils.clear()
  247. end
  248.  
  249. -- Requests admin credentials from the user
  250. function requestAdminCredentials(warningMessage)
  251. termutils.clear()
  252.  
  253. local width, height = term.getSize()
  254. local boxWidth = 40
  255. local boxHeight = 12 -- Increased box height for error messages
  256. local startX = math.floor((width - boxWidth) / 2)
  257. local startY = math.floor((height - boxHeight) / 2)
  258.  
  259. term.setBackgroundColor(colors.gray)
  260. term.setTextColor(colors.white)
  261.  
  262. -- Draw the box
  263. term.setCursorPos(startX, startY)
  264. write("+" .. string.rep("-", boxWidth - 2) .. "+")
  265.  
  266. for y = startY + 1, startY + boxHeight - 1 do
  267. term.setCursorPos(startX, y)
  268. write("|" .. string.rep(" ", boxWidth - 2) .. "|")
  269. end
  270.  
  271. term.setCursorPos(startX, startY + boxHeight)
  272. write("+" .. string.rep("-", boxWidth - 2) .. "+")
  273.  
  274. term.setBackgroundColor(colors.black)
  275. term.setTextColor(colors.white)
  276.  
  277. -- Display prompts inside the box
  278. local contentX = startX + 2
  279. local contentY = startY + 2
  280.  
  281. term.setCursorPos(contentX, contentY)
  282. write("Doggy OS File System Protection")
  283.  
  284. term.setCursorPos(contentX, contentY + 2)
  285. write("Enter Administrator login.")
  286.  
  287. term.setCursorPos(contentX, contentY + 4)
  288. write("Username: ")
  289. local username = io.read()
  290.  
  291. term.setCursorPos(contentX, contentY + 6)
  292. write("Password: ")
  293. term.setTextColor(colors.black) -- Change text color to black to hide the input
  294. local password = io.read()
  295. term.setTextColor(colors.white) -- Reset text color
  296.  
  297. -- Verify credentials and handle access
  298. local isVerified = verifyPassword(username, password)
  299. if not isVerified then
  300. termutils.clear() -- Clear the screen if verification fails
  301. showErrorUI("Verification failed. " .. warningMessage)
  302. end
  303. return isVerified
  304. end
  305.  
  306. -- Verifies the entered username and password
  307. function verifyPassword(username, password)
  308. local passwordFilePath = "/disk/users/" .. username .. "/password.txt"
  309. local file = fs.open(passwordFilePath, "r")
  310.  
  311. if file then
  312. local correctPassword = file.readAll()
  313. file.close()
  314.  
  315. if password == correctPassword and userIsAdmin(username) then
  316. return true
  317. else
  318. return false
  319. end
  320. else
  321. return false
  322. end
  323. end
  324.  
  325. -- Checks if the user has admin privileges
  326. function userIsAdmin(username)
  327. local adminFilePath = "/disk/users/" .. username .. "/admin.txt"
  328. local file = fs.open(adminFilePath, "r")
  329.  
  330. if file then
  331. file.close()
  332. return true
  333. else
  334. return false
  335. end
  336. end
  337.  
  338. -- Executes the command with error handling
  339. function executeCommand(command)
  340. local success, err = pcall(function() shell.run(command) end)
  341. if not success then
  342. showErrorUI("Error executing command: " .. err)
  343. end
  344. end
  345.  
  346. -- Checks if dev.cfg exists in the root directory
  347. function checkDevConfig()
  348. local file = fs.open("/dev.cfg", "r")
  349. if file then
  350. file.close()
  351. return true
  352. end
  353. return false
  354. end
  355.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement