Advertisement
DOGGYWOOF

Untitled

Jan 3rd, 2025 (edited)
14
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.05 KB | None | 0 0
  1. -- Function to handle errors more effectively for shell commands
  2. local function safeShellRun(command, ...)
  3. local success, result = pcall(shell.run, command, ...)
  4. if not success then
  5. print("Error: " .. result) -- Output the error message from pcall
  6. return false
  7. end
  8. return true
  9. end
  10.  
  11. -- Function to trim any trailing slashes from a path
  12. local function trimTrailingSlash(path)
  13. return path:match("^(.-)/?$") -- Ensure leading slash is maintained
  14. end
  15.  
  16. -- Function to check if the current directory is within any protected path
  17. local function checkDirectoryAgainstProtectedPaths(protectedPaths)
  18. local currentDirectory = trimTrailingSlash(shell.dir())
  19.  
  20. if not currentDirectory:match("^/") then
  21. currentDirectory = "/" .. currentDirectory
  22. end
  23.  
  24. print("You are currently in directory: " .. currentDirectory)
  25.  
  26. for _, path in ipairs(protectedPaths) do
  27. local protectedPath = trimTrailingSlash(path)
  28. if not protectedPath:match("^/") then
  29. protectedPath = "/" .. protectedPath
  30. end
  31.  
  32. if currentDirectory == protectedPath or currentDirectory:find("^" .. protectedPath, 1, true) then
  33. return true
  34. end
  35. end
  36.  
  37. return false
  38. end
  39.  
  40. termutils = {}
  41.  
  42. termutils.clear = function()
  43. term.clear()
  44. term.setCursorPos(1, 1)
  45. end
  46.  
  47. termutils.clearColor = function()
  48. term.setTextColor(colors.white)
  49. term.setBackgroundColor(colors.black)
  50. end
  51.  
  52. function input()
  53. term.setTextColor(colors.blue)
  54. local dir = shell.dir() .. "/> "
  55. write(dir)
  56. termutils.clearColor()
  57. return io.read()
  58. end
  59.  
  60. function isInProtectedPath(command, path)
  61. if not path:match("^/") then
  62. path = "/" .. path
  63. end
  64. if not command:match("^/") then
  65. command = "/" .. command
  66. end
  67.  
  68. return command:find(path, 1, true) ~= nil
  69. end
  70.  
  71. function checkProtection(command)
  72. local blockedCommands = { "sh", "shell" }
  73. local protectedPaths = {
  74. "/disk/boot/",
  75. "/disk/os/",
  76. "/disk/bootloader/",
  77. "/disk/users/",
  78. "/recovery/",
  79. "/disk/ACPI/",
  80. "/disk/error/",
  81. "/disk/startup",
  82. "/disk/setup",
  83. "/disk/install.lua",
  84. "/disk/install-assist",
  85. "startup",
  86. "no-os",
  87. "dev.cfg"
  88. }
  89.  
  90. local protectedFiles = {
  91. "/disk/setup",
  92. "/disk/startup",
  93. "/disk/install.lua",
  94. "/disk/install-assist",
  95. "startup",
  96. "no-os",
  97. ".settings"
  98. }
  99.  
  100. if command:lower() == "reboot" then
  101. safeShellRun("/disk/ACPI/reboot")
  102. return false
  103. elseif command:lower() == "shutdown" then
  104. safeShellRun("/disk/ACPI/shutdown")
  105. return false
  106. elseif command:lower() == "exit" then
  107. safeShellRun("/disk/os/gui")
  108. return false
  109. end
  110.  
  111. for _, blocked in ipairs(blockedCommands) do
  112. if command:lower() == blocked then
  113. print("Error: The command '" .. blocked .. "' is not allowed.")
  114. return false
  115. end
  116. end
  117.  
  118. for _, path in ipairs(protectedPaths) do
  119. if isInProtectedPath(command, path) then
  120. if not requestAdminCredentials("This command may modify critical files. Proceed with caution.") then
  121. return false
  122. end
  123. return true
  124. end
  125. end
  126.  
  127. for _, file in ipairs(protectedFiles) do
  128. if command:find(file, 1, true) then
  129. if not requestAdminCredentials("This command involves a protected file. Proceed with caution.") then
  130. return false
  131. end
  132. return true
  133. end
  134. end
  135.  
  136. if checkDirectoryAgainstProtectedPaths(protectedPaths) then
  137. if not requestAdminCredentials("You are in a protected directory. Proceed with caution.") then
  138. return false
  139. end
  140. return true
  141. end
  142.  
  143. return true
  144. end
  145.  
  146. function requestAdminCredentials(warningMessage)
  147. termutils.clear()
  148.  
  149. local width, height = term.getSize()
  150. local boxWidth = 40
  151. local boxHeight = 12
  152. local startX = math.floor((width - boxWidth) / 2)
  153. local startY = math.floor((height - boxHeight) / 2)
  154.  
  155. term.setBackgroundColor(colors.gray)
  156. term.setTextColor(colors.white)
  157.  
  158. term.setCursorPos(startX, startY)
  159. write("+" .. string.rep("-", boxWidth - 2) .. "+")
  160.  
  161. for y = startY + 1, startY + boxHeight - 1 do
  162. term.setCursorPos(startX, y)
  163. write("|" .. string.rep(" ", boxWidth - 2) .. "|")
  164. end
  165.  
  166. term.setCursorPos(startX, startY + boxHeight)
  167. write("+" .. string.rep("-", boxWidth - 2) .. "+")
  168.  
  169. term.setBackgroundColor(colors.black)
  170. term.setTextColor(colors.white)
  171.  
  172. local contentX = startX + 2
  173. local contentY = startY + 2
  174.  
  175. term.setCursorPos(contentX, contentY)
  176. write("Doggy OS File System Protection")
  177.  
  178. term.setCursorPos(contentX, contentY + 2)
  179. write("Enter Administrator login.")
  180.  
  181. term.setCursorPos(contentX, contentY + 4)
  182. write("Username: ")
  183. local username = io.read()
  184.  
  185. term.setCursorPos(contentX, contentY + 6)
  186. write("Password: ")
  187. term.setTextColor(colors.black)
  188. local password = io.read()
  189. term.setTextColor(colors.white)
  190.  
  191. local isVerified = verifyPassword(username, password)
  192. if not isVerified then
  193. termutils.clear()
  194. print("Error: Verification failed.")
  195. print(warningMessage)
  196. end
  197. return isVerified
  198. end
  199.  
  200. function verifyPassword(username, password)
  201. local passwordFilePath = "/disk/users/" .. username .. "/password.txt"
  202. local file, err = fs.open(passwordFilePath, "r")
  203.  
  204. if not file then
  205. print("Error: Unable to open password file - " .. err)
  206. return false
  207. end
  208.  
  209. local correctPassword = file.readAll()
  210. file.close()
  211.  
  212. if password == correctPassword and userIsAdmin(username) then
  213. return true
  214. else
  215. return false
  216. end
  217. end
  218.  
  219. function userIsAdmin(username)
  220. local adminFilePath = "/disk/users/" .. username .. "/admin.txt"
  221. local file, err = fs.open(adminFilePath, "r")
  222.  
  223. if not file then
  224. print("Error: Unable to open admin file - " .. err)
  225. return false
  226. end
  227.  
  228. file.close()
  229. return true
  230. end
  231.  
  232. -- Main execution starts here
  233. termutils.clear()
  234. print("Doggy OS Terminal (13.0)")
  235.  
  236. while true do
  237. local command = input()
  238.  
  239. if checkProtection(command) then
  240. safeShellRun(command)
  241. else
  242. print("Command aborted.")
  243. end
  244. end
  245.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement