Advertisement
DOGGYWOOF

Untitled

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