Advertisement
DOGGYWOOF

Untitled

Sep 7th, 2024
11
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.01 KB | None | 0 0
  1. local MAX_ATTEMPTS = 3 -- Maximum number of incorrect password attempts allowed
  2. local LOCKOUT_TIME = 30 -- Lockout time in seconds after reaching maximum attempts
  3.  
  4. local USERS_FOLDER = "/disk/users/"
  5. local ERROR_FOLDER = "/disk/error/"
  6. local BSOD_PROGRAM = "BSOD.lua"
  7.  
  8. -- Utility function to draw a centered popup window with a text-based border
  9. local function drawPopupWindow(headerText, contentLines)
  10. term.clear() -- Clear the screen before drawing the popup window
  11.  
  12. local w, h = term.getSize()
  13. local maxLength = #headerText
  14. for _, line in ipairs(contentLines) do
  15. maxLength = math.max(maxLength, #line)
  16. end
  17.  
  18. local windowWidth = maxLength + 4 -- Width of the popup window
  19. local windowHeight = #contentLines + 4 -- Height of the popup window
  20. local xStart = math.floor(w / 2 - windowWidth / 2)
  21. local yStart = math.floor(h / 2 - windowHeight / 2)
  22.  
  23. -- Draw border
  24. term.setCursorPos(xStart, yStart)
  25. term.write("+" .. string.rep("-", windowWidth - 2) .. "+")
  26. for i = 1, windowHeight - 2 do
  27. term.setCursorPos(xStart, yStart + i)
  28. term.write("|" .. string.rep(" ", windowWidth - 2) .. "|")
  29. end
  30. term.setCursorPos(xStart, yStart + windowHeight - 1)
  31. term.write("+" .. string.rep("-", windowWidth - 2) .. "+")
  32.  
  33. -- Draw header
  34. term.setCursorPos(xStart + 2, yStart + 1)
  35. term.write(headerText)
  36.  
  37. -- Draw content
  38. for i, line in ipairs(contentLines) do
  39. term.setCursorPos(xStart + 2, yStart + 1 + i + 1)
  40. term.write(line)
  41. end
  42. end
  43.  
  44. local function getUserCredentials(username)
  45. local passwordFile = fs.combine(USERS_FOLDER .. username, "password.txt")
  46.  
  47. if fs.exists(passwordFile) then
  48. local file = fs.open(passwordFile, "r")
  49. local storedPassword = file.readLine()
  50. file.close()
  51. return storedPassword
  52. else
  53. return nil -- User does not exist
  54. end
  55. end
  56.  
  57. local function drawLoginScreen(username, attemptsLeft)
  58. local contentLines = {
  59. "Username: " .. username,
  60. "Attempts left: " .. attemptsLeft,
  61. "",
  62. "Enter password:"
  63. }
  64. drawPopupWindow("Login to Doggy OS", contentLines)
  65. term.setCursorPos(math.floor(term.getSize() / 2) - 2, math.floor(term.getSize() / 2) + 1) -- Position cursor for password input
  66. end
  67.  
  68. local function lockoutUser(username)
  69. local disabledFile = fs.combine(USERS_FOLDER .. username, "disabled.txt")
  70. local file = fs.open(disabledFile, "w")
  71. file.close()
  72. end
  73.  
  74. local function checkDisabled(username)
  75. local disabledFile = fs.combine(USERS_FOLDER .. username, "disabled.txt")
  76. return fs.exists(disabledFile)
  77. end
  78.  
  79. local function checkCredentials(username)
  80. if checkDisabled(username) then
  81. drawPopupWindow("Access Denied", {"This user has been disabled due to security reasons.", "", "Contact your administrator for help."})
  82. os.sleep(5) -- Display the disabled message for 5 seconds
  83. shell.run("/disk/os/lock.lua") -- Run the lock.lua program
  84. return false
  85. end
  86.  
  87. local storedPassword = getUserCredentials(username)
  88.  
  89. if not storedPassword then
  90. return false -- User does not exist
  91. end
  92.  
  93. local attempts = 0
  94.  
  95. repeat
  96. drawLoginScreen(username, MAX_ATTEMPTS - attempts)
  97.  
  98. local enteredPassword = read("*")
  99. attempts = attempts + 1
  100.  
  101. if enteredPassword == storedPassword then
  102. return true
  103. else
  104. drawPopupWindow("Access Denied", {"Incorrect password. Please try again."})
  105. os.sleep(2) -- Display the error message for 2 seconds
  106. end
  107. until attempts > MAX_ATTEMPTS
  108.  
  109. drawPopupWindow("Account Disabled", {"Too many incorrect attempts. User has been disabled."})
  110. lockoutUser(username)
  111. os.sleep(2) -- Display the lockout message for 2 seconds
  112.  
  113. return false
  114. end
  115.  
  116. local function checkDiskIDs()
  117. -- Get a list of all connected peripherals
  118. local peripherals = peripheral.getNames()
  119.  
  120. -- Array to store disk IDs
  121. local diskIDs = {}
  122.  
  123. -- Loop through all peripherals
  124. for _, name in ipairs(peripherals) do
  125. -- Check if the peripheral is a disk drive
  126. if peripheral.getType(name) == "drive" then
  127. -- Get the disk ID from the disk drive
  128. local diskID = disk.getID(name)
  129.  
  130. -- If a disk is inserted, add its ID to the array
  131. if diskID then
  132. table.insert(diskIDs, {id = diskID, name = name})
  133. end
  134. end
  135. end
  136.  
  137. -- Check if any disks were found
  138. if #diskIDs > 0 then
  139. return diskIDs
  140. else
  141. return nil
  142. end
  143. end
  144.  
  145. local function drawSecurityCardPrompt()
  146. local contentLines = {
  147. "Please insert your security card.",
  148. "Press ENTER to use password instead."
  149. }
  150. drawPopupWindow("Insert Security Card", contentLines)
  151. end
  152.  
  153. local function drawErrorMessage(message)
  154. drawPopupWindow("Error", {message})
  155. end
  156.  
  157. local function drawMessage(header, message)
  158. drawPopupWindow(header, {message})
  159. end
  160.  
  161. local function ejectDisk(diskName)
  162. peripheral.call(diskName, "ejectDisk")
  163. end
  164.  
  165. local function checkSecurityCard(username)
  166. local idFolder = fs.combine(USERS_FOLDER .. username, "ID")
  167. local forcelogoffFile = fs.combine(USERS_FOLDER .. username, "forcelogoff.config")
  168.  
  169. if not fs.exists(idFolder) and not fs.exists(forcelogoffFile) then
  170. return false
  171. end
  172.  
  173. while true do
  174. drawSecurityCardPrompt()
  175.  
  176. local event, key = os.pullEvent()
  177.  
  178. if event == "key" and key == keys.enter then
  179. return false
  180. elseif event == "disk" or event == "disk_insert" then
  181. local diskIDs = checkDiskIDs()
  182. if diskIDs then
  183. for _, disk in ipairs(diskIDs) do
  184. local idFile = fs.combine(idFolder, tostring(disk.id) .. ".file")
  185. if fs.exists(idFile) then
  186. return true -- Allow access if a valid ID is found
  187. end
  188. end
  189. drawErrorMessage("Error: Unregistered security key.")
  190. os.sleep(2)
  191. end
  192. elseif event == "disk_eject" then
  193. if fs.exists(forcelogoffFile) then
  194. drawErrorMessage("Disk ejected. Logging off.")
  195. os.sleep(2)
  196. shell.run("/disk/os/lock.lua")
  197. return false
  198. end
  199. end
  200. end
  201. end
  202.  
  203. local function main()
  204. -- Set the text color to white and the background color to black
  205. term.setTextColor(colors.white)
  206. term.setBackgroundColor(colors.black)
  207. term.clear()
  208.  
  209. drawPopupWindow("Protected by Doggy OS Security", {"Enter username:"})
  210.  
  211. local enteredUsername = read()
  212.  
  213. if checkDisabled(enteredUsername) then
  214. drawErrorMessage("Account disabled")
  215. os.sleep(3) -- Display the disabled message for 3 seconds
  216. shell.run("/disk/os/lock.lua") -- Run the lock.lua program
  217. return
  218. end
  219.  
  220. local users = fs.list(USERS_FOLDER)
  221.  
  222. if not users or #users == 0 then
  223. -- No users detected, run the BSOD program
  224. shell.run(ERROR_FOLDER .. BSOD_PROGRAM)
  225. elseif checkSecurityCard(enteredUsername) then
  226. drawMessage("Welcome", "Access granted. Welcome, " .. enteredUsername .. "!")
  227. os.sleep(2) -- Display the success message for 2 seconds
  228. shell.run("/disk/os/gui")
  229. -- Your main OS code goes here
  230. elseif checkCredentials(enteredUsername) then
  231. drawMessage("Welcome", "Access granted. Welcome, " .. enteredUsername .. "!")
  232. os.sleep(2) -- Display the success message for 2 seconds
  233. shell.run("/disk/os/gui")
  234. -- Your main OS code goes here
  235. else
  236. drawErrorMessage("Access denied.")
  237. os.sleep(2) -- Display the access denied message for 2 seconds
  238. shell.run("/disk/os/lock.lua")
  239. end
  240. end
  241.  
  242. main()
  243.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement