Advertisement
DOGGYWOOF

CENTERED LOGON WITH DOGGY OS ENCRYPTION

Jul 13th, 2024
12
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.83 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 generateKey()
  45. local chars = {}
  46. for i = 32, 126 do -- ASCII range for printable characters
  47. table.insert(chars, string.char(i))
  48. end
  49.  
  50. local key = {}
  51. while #chars > 0 do
  52. local index = math.random(#chars)
  53. table.insert(key, table.remove(chars, index))
  54. end
  55.  
  56. return table.concat(key)
  57. end
  58.  
  59. local function createMap(key)
  60. local map = {}
  61. for i = 32, 126 do
  62. map[string.char(i)] = key:sub(i - 31, i - 31)
  63. end
  64. return map
  65. end
  66.  
  67. local function invertMap(map)
  68. local invMap = {}
  69. for k, v in pairs(map) do
  70. invMap[v] = k
  71. end
  72. return invMap
  73. end
  74.  
  75. local function encrypt(text, key)
  76. local map = createMap(key)
  77. local encrypted = {}
  78. for i = 1, #text do
  79. local char = text:sub(i, i)
  80. table.insert(encrypted, map[char] or char)
  81. end
  82. return table.concat(encrypted)
  83. end
  84.  
  85. local function decrypt(text, key)
  86. local map = invertMap(createMap(key))
  87. local decrypted = {}
  88. for i = 1, #text do
  89. local char = text:sub(i, i)
  90. table.insert(decrypted, map[char] or char)
  91. end
  92. return table.concat(decrypted)
  93. end
  94.  
  95. local function saveKeyToFile(key, dir)
  96. local randomNumber = math.random(1, 1000)
  97. local fileName = dir .. "/key" .. randomNumber .. ".txt"
  98. local file = fs.open(fileName, "w")
  99. file.write(key)
  100. file.close()
  101. return fileName
  102. end
  103.  
  104. local function loadKeyFromFile(fileName)
  105. local file = fs.open(fileName, "r")
  106. local key = file.readAll()
  107. file.close()
  108. return key
  109. end
  110.  
  111. local function findKeyFile(dir)
  112. local files = fs.list(dir)
  113. for _, file in ipairs(files) do
  114. if file:match("^key%d+%.txt$") then
  115. return dir .. "/" .. file
  116. end
  117. end
  118. return nil
  119. end
  120.  
  121. local function loadEncryptedText(userDir)
  122. local fileName = userDir .. "/password.txt"
  123. if not fs.exists(fileName) then
  124. return nil
  125. end
  126. local file = fs.open(fileName, "r")
  127. local encryptedText = file.readAll()
  128. file.close()
  129. return encryptedText
  130. end
  131.  
  132. local function getUserCredentials(username)
  133. local userDir = fs.combine(USERS_FOLDER, username)
  134. local encryptedText = loadEncryptedText(userDir)
  135. if not encryptedText then
  136. return nil -- User does not exist
  137. end
  138.  
  139. local keyFile = findKeyFile(userDir)
  140. if not keyFile then
  141. return nil -- Key file not found
  142. end
  143.  
  144. local key = loadKeyFromFile(keyFile)
  145. local decryptedPassword = decrypt(encryptedText, key)
  146. return decryptedPassword
  147. end
  148.  
  149. local function drawLoginScreen(username, attemptsLeft)
  150. local contentLines = {
  151. "Username: " .. username,
  152. "Attempts left: " .. attemptsLeft,
  153. "",
  154. "Enter password:"
  155. }
  156. drawPopupWindow("Login to Doggy OS", contentLines)
  157. term.setCursorPos(math.floor(term.getSize() / 2) - 2, math.floor(term.getSize() / 2) + 1) -- Position cursor for password input
  158. end
  159.  
  160. local function lockoutUser(username)
  161. local disabledFile = fs.combine(USERS_FOLDER .. username, "disabled.txt")
  162. local file = fs.open(disabledFile, "w")
  163. file.close()
  164. end
  165.  
  166. local function checkDisabled(username)
  167. local disabledFile = fs.combine(USERS_FOLDER .. username, "disabled.txt")
  168. return fs.exists(disabledFile)
  169. end
  170.  
  171. local function checkCredentials(username)
  172. if checkDisabled(username) then
  173. drawPopupWindow("Access Denied", {"This user has been disabled due to security reasons.", "", "Contact your administrator for help."})
  174. os.sleep(5) -- Display the disabled message for 5 seconds
  175. shell.run("/disk/os/lock.lua") -- Run the lock.lua program
  176. return false
  177. end
  178.  
  179. local storedPassword = getUserCredentials(username)
  180.  
  181. if not storedPassword then
  182. return false -- User does not exist
  183. end
  184.  
  185. local attempts = 0
  186.  
  187. repeat
  188. drawLoginScreen(username, MAX_ATTEMPTS - attempts)
  189.  
  190. local enteredPassword = read("*")
  191. attempts = attempts + 1
  192.  
  193. if enteredPassword == storedPassword then
  194. return true
  195. else
  196. drawPopupWindow("Access Denied", {"Incorrect password. Please try again."})
  197. os.sleep(2) -- Display the error message for 2 seconds
  198. end
  199. until attempts > MAX_ATTEMPTS
  200.  
  201. drawPopupWindow("Account Disabled", {"Too many incorrect attempts. User has been disabled."})
  202. lockoutUser(username)
  203. os.sleep(2) -- Display the lockout message for 2 seconds
  204.  
  205. return false
  206. end
  207.  
  208. local function checkDiskIDs()
  209. -- Get a list of all connected peripherals
  210. local peripherals = peripheral.getNames()
  211.  
  212. -- Array to store disk IDs
  213. local diskIDs = {}
  214.  
  215. -- Loop through all peripherals
  216. for _, name in ipairs(peripherals) do
  217. -- Check if the peripheral is a disk drive
  218. if peripheral.getType(name) == "drive" then
  219. -- Get the disk ID from the disk drive
  220. local diskID = disk.getID(name)
  221.  
  222. -- If a disk is inserted, add its ID to the array
  223. if diskID then
  224. table.insert(diskIDs, {id = diskID, name = name})
  225. end
  226. end
  227. end
  228.  
  229. -- Check if any disks were found
  230. if #diskIDs > 0 then
  231. return diskIDs
  232. else
  233. return nil
  234. end
  235. end
  236.  
  237. local function drawSecurityCardPrompt()
  238. local contentLines = {
  239. "Please insert your security card.",
  240. "Press ENTER to use password instead."
  241. }
  242. drawPopupWindow("Insert Security Card", contentLines)
  243. end
  244.  
  245. local function drawErrorMessage(message)
  246. drawPopupWindow("Error", {message})
  247. end
  248.  
  249. local function drawMessage(header, message)
  250. drawPopupWindow(header, {message})
  251. end
  252.  
  253. local function ejectDisk(diskName)
  254. peripheral.call(diskName, "ejectDisk")
  255. end
  256.  
  257. local function checkSecurityCard(username)
  258. local idFolder = fs.combine(USERS_FOLDER .. username, "ID")
  259. if not fs.exists(idFolder) then
  260. return false
  261. end
  262.  
  263. while true do
  264. drawSecurityCardPrompt()
  265.  
  266. local event, key = os.pullEvent()
  267.  
  268. if event == "key" and key == keys.enter then
  269. return false
  270. elseif event == "disk" or event == "disk_insert" then
  271. local diskIDs = checkDiskIDs()
  272. if diskIDs then
  273. for _, disk in ipairs(diskIDs) do
  274. ejectDisk(disk.name) -- Eject the disk
  275. local idFile = fs.combine(idFolder, tostring(disk.id) .. ".file")
  276. if fs.exists(idFile) then
  277. return true -- Allow access if a valid ID is found
  278. end
  279. end
  280. drawErrorMessage("Error: Unregistered security key.")
  281. os.sleep(2)
  282. end
  283. end
  284. end
  285. end
  286.  
  287. local function main()
  288. term.setBackgroundColor(colors.black) -- Set overall background color to black
  289. term.clear()
  290.  
  291. drawPopupWindow("Protected by Doggy OS Security", {"Enter username:"})
  292.  
  293. local enteredUsername = read()
  294.  
  295. if checkDisabled(enteredUsername) then
  296. drawErrorMessage("Account disabled")
  297. os.sleep(3) -- Display the disabled message for 3 seconds
  298. shell.run("/disk/os/lock.lua") -- Run the lock.lua program
  299. return
  300. end
  301.  
  302. local users = fs.list(USERS_FOLDER)
  303.  
  304. if not users or #users == 0 then
  305. -- No users detected, run the BSOD program
  306. shell.run(ERROR_FOLDER .. BSOD_PROGRAM)
  307. elseif checkSecurityCard(enteredUsername) then
  308. drawMessage("Welcome", "Access granted. Welcome, " .. enteredUsername .. "!")
  309. os.sleep(2) -- Display the success message for 2 seconds
  310. shell.run("/disk/os/gui")
  311. -- Your main OS code goes here
  312. elseif checkCredentials(enteredUsername) then
  313. drawMessage("Welcome", "Access granted. Welcome, " .. enteredUsername .. "!")
  314. os.sleep(2) -- Display the success message for 2 seconds
  315. shell.run("/disk/os/gui")
  316. -- Your main OS code goes here
  317. else
  318. drawErrorMessage("Access denied.")
  319. os.sleep(2) -- Display the access denied message for 2 seconds
  320. shell.run("/disk/os/lock.lua")
  321. end
  322. end
  323.  
  324. main()
  325.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement