Advertisement
DOGGYWOOF

Untitled

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