Advertisement
DOGGYWOOF

Doggy OS Defualt shell

Aug 20th, 2024 (edited)
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.38 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. -- Handle specific commands for reboot and shutdown
  93. if command:lower() == "reboot" then
  94. executeCommand("/disk/ACPI/reboot")
  95. return false -- Prevent further command processing
  96. elseif command:lower() == "shutdown" then
  97. executeCommand("/disk/ACPI/shutdown")
  98. return false -- Prevent further command processing
  99. elseif command:lower() == "exit" then
  100. executeCommand("/disk/os/gui")
  101. return false -- Prevent further command processing
  102. end
  103.  
  104. -- Block specific commands
  105. for _, blocked in ipairs(blockedCommands) do
  106. if command:lower() == blocked then
  107. print("Error: The command '" .. blocked .. "' is not allowed.")
  108. return false
  109. end
  110. end
  111.  
  112. -- Check for protected paths/files
  113. for _, path in ipairs(protectedPaths) do
  114. if isInProtectedPath(command, path) then
  115. -- Show the FS protection screen and request admin credentials
  116. if not requestAdminCredentials() then
  117. return false
  118. end
  119. print("Warning: This command may modify critical files. Proceed with caution.")
  120. return true
  121. end
  122. end
  123.  
  124. -- Check if the current directory is within any protected path
  125. if checkDirectoryAgainstProtectedPaths(protectedPaths) then
  126. -- Show the FS protection screen and request admin credentials
  127. if not requestAdminCredentials() then
  128. return false
  129. end
  130. print("Warning: You are in a protected directory. Proceed with caution.")
  131. return true
  132. end
  133.  
  134. return true
  135. end
  136.  
  137. -- Requests admin credentials from the user
  138. function requestAdminCredentials()
  139. termutils.clear()
  140.  
  141. local width, height = term.getSize()
  142. local boxWidth = 40
  143. local boxHeight = 10
  144. local startX = math.floor((width - boxWidth) / 2)
  145. local startY = math.floor((height - boxHeight) / 2)
  146.  
  147. term.setBackgroundColor(colors.gray)
  148. term.setTextColor(colors.white)
  149.  
  150. -- Draw the box
  151. term.setCursorPos(startX, startY)
  152. write("+" .. string.rep("-", boxWidth - 2) .. "+")
  153.  
  154. for y = startY + 1, startY + boxHeight - 1 do
  155. term.setCursorPos(startX, y)
  156. write("|" .. string.rep(" ", boxWidth - 2) .. "|")
  157. end
  158.  
  159. term.setCursorPos(startX, startY + boxHeight)
  160. write("+" .. string.rep("-", boxWidth - 2) .. "+")
  161.  
  162. term.setBackgroundColor(colors.black)
  163. term.setTextColor(colors.white)
  164.  
  165. -- Display prompts inside the box
  166. local contentX = startX + 2
  167. local contentY = startY + 2
  168.  
  169. term.setCursorPos(contentX, contentY)
  170. write("Doggy OS File System Protection")
  171.  
  172. term.setCursorPos(contentX, contentY + 2)
  173. write("Enter Administrator login.")
  174.  
  175. term.setCursorPos(contentX, contentY + 4)
  176. write("Username: ")
  177. local username = io.read()
  178.  
  179. term.setCursorPos(contentX, contentY + 6)
  180. write("Password: ")
  181. term.setTextColor(colors.black) -- Change text color to black to hide the input
  182. local password = io.read()
  183. term.setTextColor(colors.white) -- Reset text color
  184.  
  185. -- Verify credentials and handle access
  186. local isVerified = verifyPassword(username, password)
  187. if not isVerified then
  188. termutils.clear() -- Clear the screen if verification fails
  189. end
  190. return isVerified
  191. end
  192.  
  193. -- Verifies the entered username and password
  194. function verifyPassword(username, password)
  195. local passwordFilePath = "/disk/users/" .. username .. "/password.txt"
  196. local file = fs.open(passwordFilePath, "r")
  197.  
  198. if file then
  199. local correctPassword = file.readAll()
  200. file.close()
  201.  
  202. if password == correctPassword and userIsAdmin(username) then
  203. return true
  204. else
  205. print("Invalid credentials or insufficient privileges.")
  206. return false
  207. end
  208. else
  209. print("User not found.")
  210. return false
  211. end
  212. end
  213.  
  214. -- Checks if the user has admin privileges
  215. function userIsAdmin(username)
  216. local adminFilePath = "/disk/users/" .. username .. "/admin.txt"
  217. local file = fs.open(adminFilePath, "r")
  218.  
  219. if file then
  220. file.close()
  221. return true
  222. else
  223. return false
  224. end
  225. end
  226.  
  227. -- Executes the command with error handling
  228. function executeCommand(command)
  229. local success, err = pcall(function() shell.run(command) end)
  230. if not success then
  231. print("Error executing command:", err)
  232. end
  233. end
  234.  
  235. -- Checks if dev.cfg exists in the root directory
  236. function checkDevConfig()
  237. local file = fs.open("/dev.cfg", "r")
  238. if file then
  239. file.close()
  240. return true
  241. else
  242. return false
  243. end
  244. end
  245.  
  246. -- Main execution starts here
  247. termutils.clear()
  248. print("Doggy OS Terminal (13.0)")
  249.  
  250. while true do
  251. local command = input()
  252.  
  253. if checkProtection(command) then
  254. executeCommand(command)
  255. else
  256. print("Command aborted.")
  257. end
  258. end
  259.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement