Advertisement
DOGGYWOOF

Untitled

Jan 14th, 2025 (edited)
15
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.23 KB | None | 0 0
  1. -- Constants for security configuration
  2. local MAX_ATTEMPTS = 3 -- Maximum number of incorrect password attempts allowed
  3. local LOCKOUT_TIME = 30 -- Lockout time in seconds after reaching maximum attempts
  4.  
  5. local USERS_FOLDER = "/disk/users/"
  6. local ERROR_FOLDER = "/disk/error/"
  7. local BSOD_PROGRAM = "BSOD.lua"
  8. local CURRENT_USER_FILE = ".currentusr"
  9. local SHOW_ALL_USERS_FILE = "/disk/config/security/login/ShowAllUsers.cfg"
  10. local PACKAGES_DIR = "packages"
  11.  
  12. -- Utility function to draw a centered popup window with a text-based border
  13. local function drawPopupWindow(headerText, contentLines, windowWidth, windowHeight)
  14. term.clear()
  15. local w, h = term.getSize()
  16.  
  17. -- Determine dimensions of the popup
  18. local maxLength = #headerText
  19. for _, line in ipairs(contentLines) do
  20. maxLength = math.max(maxLength, #line)
  21. end
  22. windowWidth = windowWidth or (maxLength + 4)
  23. windowHeight = windowHeight or (#contentLines + 4)
  24.  
  25. local xStart = math.floor(w / 2 - windowWidth / 2)
  26. local yStart = math.floor(h / 2 - windowHeight / 2)
  27.  
  28. -- Draw border with animation
  29. term.setCursorPos(xStart, yStart)
  30. term.write("+" .. string.rep("-", windowWidth - 2) .. "+")
  31. for i = 1, windowHeight - 2 do
  32. term.setCursorPos(xStart, yStart + i)
  33. term.write("|" .. string.rep(" ", windowWidth - 2) .. "|")
  34. os.sleep(0.05)
  35. end
  36. term.setCursorPos(xStart, yStart + windowHeight - 1)
  37. term.write("+" .. string.rep("-", windowWidth - 2) .. "+")
  38.  
  39. -- Draw header
  40. term.setCursorPos(xStart + 2, yStart + 1)
  41. term.write(headerText)
  42.  
  43. -- Draw content
  44. for i, line in ipairs(contentLines) do
  45. term.setCursorPos(xStart + 2, yStart + 1 + i + 1)
  46. term.write(line)
  47. os.sleep(0.05)
  48. end
  49. end
  50.  
  51. -- Function to list all users
  52. local function listUsers()
  53. local users = fs.list(USERS_FOLDER)
  54. local usernames = {}
  55. for _, user in ipairs(users) do
  56. local userDir = fs.combine(USERS_FOLDER, user)
  57. if fs.isDir(userDir) then
  58. table.insert(usernames, user)
  59. end
  60. end
  61. return usernames
  62. end
  63.  
  64. -- Function to handle user selection from the list
  65. local function drawUsersScreen(usernames, selectedIndex)
  66. local contentLines = {}
  67. for i, username in ipairs(usernames) do
  68. if i == selectedIndex then
  69. table.insert(contentLines, "> " .. username)
  70. else
  71. table.insert(contentLines, " " .. username)
  72. end
  73. end
  74. drawPopupWindow("Select your user account:", contentLines, 30, #contentLines + 4)
  75. end
  76.  
  77. -- Function to select a user from the list interactively
  78. local function selectUserFromList()
  79. local usernames = listUsers()
  80. local selectedIndex = 1
  81.  
  82. while true do
  83. drawUsersScreen(usernames, selectedIndex)
  84.  
  85. local event, key = os.pullEvent("key")
  86. if key == keys.up then
  87. selectedIndex = math.max(1, selectedIndex - 1)
  88. elseif key == keys.down then
  89. selectedIndex = math.min(#usernames, selectedIndex + 1)
  90. elseif key == keys.enter then
  91. return usernames[selectedIndex]
  92. end
  93. end
  94. end
  95.  
  96. -- Function to get user credentials
  97. local function getUserCredentials(username)
  98. local passwordFile = fs.combine(USERS_FOLDER .. username, "password.txt")
  99. if fs.exists(passwordFile) then
  100. local file = fs.open(passwordFile, "r")
  101. local storedPassword = file.readLine()
  102. file.close()
  103. return storedPassword
  104. else
  105. return nil
  106. end
  107. end
  108.  
  109. -- Function to save the current user
  110. local function saveCurrentUser(username)
  111. if fs.exists(CURRENT_USER_FILE) then
  112. fs.delete(CURRENT_USER_FILE)
  113. end
  114. local file = fs.open(CURRENT_USER_FILE, "w")
  115. file.write(username)
  116. file.close()
  117. end
  118.  
  119. -- Function to lock out user
  120. local function lockoutUser(username)
  121. local disabledFile = fs.combine(USERS_FOLDER .. username, "disabled.txt")
  122. local file = fs.open(disabledFile, "w")
  123. file.close()
  124. end
  125.  
  126. -- Function to check if user is disabled
  127. local function checkDisabled(username)
  128. local disabledFile = fs.combine(USERS_FOLDER .. username, "disabled.txt")
  129. return fs.exists(disabledFile)
  130. end
  131.  
  132. -- Function to handle user login process
  133. local function checkCredentials(username)
  134. if checkDisabled(username) then
  135. drawPopupWindow("Doggy OS Account Lockout Service", {"This user account has been disabled."})
  136. os.sleep(2)
  137. return false
  138. end
  139.  
  140. local storedPassword = getUserCredentials(username)
  141. if not storedPassword then
  142. drawPopupWindow("Doggy OS Login Failure", {"User account doesn't exist or is corrupted."})
  143. os.sleep(3)
  144. return false
  145. end
  146.  
  147. local attempts = 0
  148. repeat
  149. drawPopupWindow("Login to Doggy OS", {"Username: " .. username, "Attempts left: " .. (MAX_ATTEMPTS - attempts), "", "Enter password:"})
  150. term.setCursorPos(1, select(2, term.getSize()))
  151. term.write(":")
  152. local enteredPassword = read("*")
  153. attempts = attempts + 1
  154.  
  155. if enteredPassword == storedPassword then
  156. saveCurrentUser(username)
  157. return true
  158. else
  159. drawPopupWindow("Access Denied", {"Incorrect Password"})
  160. os.sleep(2)
  161. end
  162. until attempts >= MAX_ATTEMPTS
  163.  
  164. lockoutUser(username)
  165. drawPopupWindow("Doggy OS Account Lockout Service", {"Too many incorrect attempts."})
  166. os.sleep(2)
  167. return false
  168. end
  169.  
  170. -- Function to check for connected disks and retrieve IDs
  171. local function checkDiskIDs()
  172. local peripherals = peripheral.getNames()
  173. local diskIDs = {}
  174.  
  175. for _, name in ipairs(peripherals) do
  176. if peripheral.getType(name) == "drive" then
  177. local diskID = disk.getID(name)
  178. if diskID then
  179. table.insert(diskIDs, {id = diskID, name = name})
  180. end
  181. end
  182. end
  183.  
  184. if #diskIDs > 0 then
  185. return diskIDs
  186. else
  187. return nil
  188. end
  189. end
  190.  
  191. -- Function to prompt for security card insertion
  192. local function drawSecurityCardPrompt()
  193. local contentLines = {
  194. "Insert a valid security card to login",
  195. "To login with a password press (ENTER)"
  196. }
  197. drawPopupWindow("Doggy OS Security", contentLines)
  198. end
  199.  
  200. -- Function to display an error message
  201. local function drawErrorMessage(message)
  202. drawPopupWindow("Doggy OS Login Failure", {message})
  203. end
  204.  
  205. -- Function to eject a disk
  206. local function ejectDisk(diskName)
  207. peripheral.call(diskName, "ejectDisk")
  208. end
  209.  
  210. -- Function to handle security card login
  211. local function insertSecurityCard(username)
  212. local idFolder = fs.combine(USERS_FOLDER .. username, "ID")
  213. if not fs.exists(idFolder) then
  214. return false
  215. end
  216.  
  217. while true do
  218. drawSecurityCardPrompt()
  219.  
  220. local event, key = os.pullEvent()
  221.  
  222. if event == "key" and key == keys.enter then
  223. return false -- Allow password login if enter is pressed
  224. elseif event == "disk" or event == "disk_insert" then
  225. local diskIDs = checkDiskIDs()
  226. if diskIDs then
  227. for _, disk in ipairs(diskIDs) do
  228. ejectDisk(disk.name) -- Eject the disk after checking
  229. local idFile = fs.combine(idFolder, tostring(disk.id) .. ".file")
  230. if fs.exists(idFile) then
  231. return true -- Allow access if a valid security ID is found
  232. end
  233. end
  234. drawErrorMessage("False Security Card Credintials")
  235. os.sleep(3)
  236. end
  237. end
  238. end
  239. end
  240.  
  241. -- Function to simulate user setup if `packages` directory is missing
  242. local function setupUser(username)
  243. local userDir = fs.combine(USERS_FOLDER, username)
  244. local packagesDir = fs.combine(userDir, PACKAGES_DIR)
  245.  
  246. if not fs.exists(packagesDir) then
  247. -- Simulate setup screen
  248. drawPopupWindow("Doggy OS User Setup Service", {"Setting up this user account for use..."})
  249. fs.makeDir(packagesDir)
  250. os.sleep(5) -- Simulate a brief setup process
  251. end
  252. end
  253.  
  254. -- Main function for login process
  255. local function main()
  256. term.setTextColor(colors.white)
  257. term.setBackgroundColor(colors.black)
  258. term.clear()
  259.  
  260. local username
  261.  
  262. if fs.exists(SHOW_ALL_USERS_FILE) then
  263. username = selectUserFromList()
  264. else
  265. drawPopupWindow("Protected by Doggy OS Security", {"Enter username:"})
  266. username = read()
  267. end
  268.  
  269. -- Simulate setup if `packages` directory is missing
  270. setupUser(username)
  271.  
  272. -- Attempt security card login first
  273. if insertSecurityCard(username) then
  274. drawPopupWindow("Access Granted", {"Security card verified. Welcome, " .. username .. "!"})
  275. os.sleep(2)
  276. saveCurrentUser(username)
  277. shell.run("/disk/os/gui")
  278. return
  279. end
  280.  
  281. -- Fallback to password login if card verification fails or is bypassed
  282. if checkCredentials(username) then
  283. drawPopupWindow("Access Granted", {"Welcome, " .. username .. "!"})
  284. os.sleep(2)
  285. shell.run("/disk/os/gui")
  286. else
  287. shell.run("/disk/os/lock.lua")
  288. end
  289. end
  290.  
  291. main()
  292.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement