Advertisement
DOGGYWOOF

Fully new protected Shell for DOggy OS

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