Advertisement
DOGGYWOOF

Untitled

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