Advertisement
DOGGYWOOF

New Doggy OS Terminal with root protection

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