Advertisement
DOGGYWOOF

Doggy OS Lockscreen

Nov 23rd, 2024 (edited)
11
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.21 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 getLoggedInUsername()
  58. local userFile = "/disk/users/currentusr.status"
  59. if fs.exists(userFile) then
  60. local file = fs.open(userFile, "r")
  61. local username = file.readLine()
  62. file.close()
  63. return username
  64. else
  65. return nil
  66. end
  67. end
  68.  
  69. local function drawLockscreen(username, attemptsLeft)
  70. local contentLines = {
  71. "Session Locked",
  72. "Username: " .. username,
  73. "Attempts left: " .. attemptsLeft,
  74. "",
  75. "Enter password to unlock:"
  76. }
  77. drawPopupWindow("Unlock Session", contentLines)
  78. term.setCursorPos(math.floor(term.getSize() / 2) - 2, math.floor(term.getSize() / 2) + 1) -- Position cursor for password input
  79. end
  80.  
  81. local function lockoutUser(username)
  82. local disabledFile = fs.combine(USERS_FOLDER .. username, "disabled.txt")
  83. local file = fs.open(disabledFile, "w")
  84. file.close()
  85. end
  86.  
  87. local function checkDisabled(username)
  88. local disabledFile = fs.combine(USERS_FOLDER .. username, "disabled.txt")
  89. return fs.exists(disabledFile)
  90. end
  91.  
  92. local function checkCredentials(username)
  93. if checkDisabled(username) then
  94. drawPopupWindow("Access Denied", {"This user has been disabled due to security reasons.", "", "Contact your administrator for help."})
  95. os.sleep(5) -- Display the disabled message for 5 seconds
  96. shell.run("/disk/os/lock.lua") -- Run the lock.lua program
  97. return false
  98. end
  99.  
  100. local storedPassword = getUserCredentials(username)
  101.  
  102. if not storedPassword then
  103. return false -- User does not exist
  104. end
  105.  
  106. local attempts = 0
  107.  
  108. repeat
  109. drawLockscreen(username, MAX_ATTEMPTS - attempts)
  110.  
  111. local enteredPassword = read("*")
  112. attempts = attempts + 1
  113.  
  114. if enteredPassword == storedPassword then
  115. return true
  116. else
  117. drawPopupWindow("Access Denied", {"Incorrect password. Please try again."})
  118. os.sleep(2) -- Display the error message for 2 seconds
  119. end
  120. until attempts > MAX_ATTEMPTS
  121.  
  122. drawPopupWindow("Account Disabled", {"Too many incorrect attempts. User has been disabled."})
  123. lockoutUser(username)
  124. os.sleep(2) -- Display the lockout message for 2 seconds
  125.  
  126. return false
  127. end
  128.  
  129. local function checkDiskIDs()
  130. -- Get a list of all connected peripherals
  131. local peripherals = peripheral.getNames()
  132.  
  133. -- Array to store disk IDs
  134. local diskIDs = {}
  135.  
  136. -- Loop through all peripherals
  137. for _, name in ipairs(peripherals) do
  138. -- Check if the peripheral is a disk drive
  139. if peripheral.getType(name) == "drive" then
  140. -- Get the disk ID from the disk drive
  141. local diskID = disk.getID(name)
  142.  
  143. -- If a disk is inserted, add its ID to the array
  144. if diskID then
  145. table.insert(diskIDs, {id = diskID, name = name})
  146. end
  147. end
  148. end
  149.  
  150. -- Check if any disks were found
  151. if #diskIDs > 0 then
  152. return diskIDs
  153. else
  154. return nil
  155. end
  156. end
  157.  
  158. local function drawSecurityCardPrompt()
  159. local contentLines = {
  160. "Session Locked",
  161. "Please insert your security card to unlock.",
  162. "",
  163. "Press ENTER to use password instead."
  164. }
  165. drawPopupWindow("Unlock with Security Card", contentLines)
  166. end
  167.  
  168. local function drawErrorMessage(message)
  169. drawPopupWindow("Error", {message})
  170. end
  171.  
  172. local function drawMessage(header, message)
  173. drawPopupWindow(header, {message})
  174. end
  175.  
  176. local function ejectDisk(diskName)
  177. peripheral.call(diskName, "ejectDisk")
  178. end
  179.  
  180. local function checkSecurityCard(username)
  181. local idFolder = fs.combine(USERS_FOLDER .. username, "ID")
  182. if not fs.exists(idFolder) then
  183. return false
  184. end
  185.  
  186. while true do
  187. drawSecurityCardPrompt()
  188.  
  189. local event, key = os.pullEvent()
  190.  
  191. if event == "key" and key == keys.enter then
  192. return false
  193. elseif event == "disk" or event == "disk_insert" then
  194. local diskIDs = checkDiskIDs()
  195. if diskIDs then
  196. for _, disk in ipairs(diskIDs) do
  197. ejectDisk(disk.name) -- Eject the disk
  198. local idFile = fs.combine(idFolder, tostring(disk.id) .. ".file")
  199. if fs.exists(idFile) then
  200. return true -- Allow access if a valid ID is found
  201. end
  202. end
  203. drawErrorMessage("Error: Unregistered security key.")
  204. os.sleep(2)
  205. end
  206. end
  207. end
  208. end
  209.  
  210. local function main()
  211. term.setBackgroundColor(colors.black) -- Set overall background color to black
  212. term.clear()
  213.  
  214. local username = getLoggedInUsername() -- Automatically get the logged-in username
  215.  
  216. if not username then
  217. drawErrorMessage("No user is currently logged in.")
  218. os.sleep(3) -- Display the error message for 3 seconds
  219. return
  220. end
  221.  
  222. drawPopupWindow("Protected by Doggy OS Security", {"Welcome, " .. username})
  223.  
  224. if checkDisabled(username) then
  225. drawErrorMessage("Account disabled")
  226. os.sleep(3) -- Display the disabled message for 3 seconds
  227. shell.run("/disk/os/lock.lua") -- Run the lock.lua program
  228. return
  229. end
  230.  
  231. local users = fs.list(USERS_FOLDER)
  232.  
  233. if not users or #users == 0 then
  234. -- No users detected, run the BSOD program
  235. shell.run(ERROR_FOLDER .. BSOD_PROGRAM)
  236. elseif checkSecurityCard(username) then
  237. drawMessage("Welcome", "Access granted. Welcome, " .. username .. "!")
  238. os.sleep(2) -- Display the success message for 2 seconds
  239. shell.run("/disk/os/gui")
  240. -- Your main OS code goes here
  241. elseif checkCredentials(username) then
  242. drawMessage("Welcome", "Access granted. Welcome, " .. username .. "!")
  243. os.sleep(2) -- Display the success message for 2 seconds
  244. shell.run("/disk/os/gui")
  245. -- Your main OS code goes here
  246. else
  247. drawErrorMessage("Access denied.")
  248. os.sleep(2) -- Display the access denied message for 2 seconds
  249. shell.run("/disk/os/lock.lua")
  250. end
  251. end
  252.  
  253. main()
  254.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement