Advertisement
DOGGYWOOF

Untitled

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