Advertisement
DOGGYWOOF

Untitled

Nov 23rd, 2024 (edited)
10
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.90 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. if not fs.exists(idFolder) then
  168. return false
  169. end
  170.  
  171. while true do
  172. drawSecurityCardPrompt()
  173.  
  174. local event, key = os.pullEvent()
  175.  
  176. if event == "key" and key == keys.enter then
  177. return false
  178. elseif event == "disk" or event == "disk_insert" then
  179. local diskIDs = checkDiskIDs()
  180. if diskIDs then
  181. for _, disk in ipairs(diskIDs) do
  182. ejectDisk(disk.name) -- Eject the disk
  183. local idFile = fs.combine(idFolder, tostring(disk.id) .. ".file")
  184. if fs.exists(idFile) then
  185. return true -- Allow access if a valid ID is found
  186. end
  187. end
  188. drawErrorMessage("Error: Unregistered security key.")
  189. os.sleep(2)
  190. end
  191. end
  192. end
  193. end
  194.  
  195. local function main()
  196. term.setBackgroundColor(colors.black) -- Set overall background color to black
  197. term.clear()
  198.  
  199. -- Retrieve the current user from /disk/users/currentusr.status
  200. local currentUsrFile = "/disk/users/currentusr.status"
  201. if not fs.exists(currentUsrFile) then
  202. drawErrorMessage("Current user file not found!")
  203. os.sleep(3)
  204. return
  205. end
  206.  
  207. local file = fs.open(currentUsrFile, "r")
  208. local enteredUsername = file.readLine()
  209. file.close()
  210.  
  211. if checkDisabled(enteredUsername) then
  212. drawErrorMessage("Account disabled")
  213. os.sleep(3) -- Display the disabled message for 3 seconds
  214. shell.run("/disk/os/lock.lua") -- Run the lock.lua program
  215. return
  216. end
  217.  
  218. local users = fs.list(USERS_FOLDER)
  219.  
  220. if not users or #users == 0 then
  221. -- No users detected, run the BSOD program
  222. shell.run(ERROR_FOLDER .. BSOD_PROGRAM)
  223. elseif checkSecurityCard(enteredUsername) then
  224. drawMessage("Welcome", "Access granted. Welcome, " .. enteredUsername .. "!")
  225. os.sleep(2) -- Display the success message for 2 seconds
  226. shell.run("/disk/os/gui")
  227. -- Your main OS code goes here
  228. elseif checkCredentials(enteredUsername) then
  229. drawMessage("Welcome", "Access granted. Welcome, " .. enteredUsername .. "!")
  230. os.sleep(2) -- Display the success message for 2 seconds
  231. shell.run("/disk/os/gui")
  232. -- Your main OS code goes here
  233. else
  234. drawErrorMessage("Access denied.")
  235. os.sleep(2) -- Display the access denied message for 2 seconds
  236. shell.run("/disk/os/lock.lua")
  237. end
  238. end
  239.  
  240. main()
  241.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement