Advertisement
DOGGYWOOF

Untitled

Aug 7th, 2024 (edited)
10
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.41 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. local DEV_CFG = "/dev.cfg" -- Path to the developer configuration file
  8. local DEV_MODE = false -- Flag to indicate if developer mode is enabled
  9.  
  10. -- Check if developer mode is enabled
  11. local function checkDeveloperMode()
  12. if fs.exists(DEV_CFG) then
  13. DEV_MODE = true
  14. term.clear()
  15. term.setCursorPos(1, 1)
  16.  
  17. term.setBackgroundColor(colors.black)
  18. term.setTextColor(colors.white)
  19.  
  20. -- Draw the UI for developer mode enabled message
  21. term.setCursorPos(1, 1)
  22. term.write("+---------------------------------------+")
  23. term.setCursorPos(1, 2)
  24. term.write("| DEVELOPER MODE ENABLED |")
  25. term.setCursorPos(1, 3)
  26. term.write("+---------------------------------------+")
  27.  
  28. term.setCursorPos(1, 5)
  29. term.setTextColor(colors.red)
  30. term.write("Device Security has been disabled.")
  31.  
  32. term.setCursorPos(1, 7)
  33. term.setTextColor(colors.white)
  34. term.write("This device is currently in dev mode")
  35.  
  36. os.sleep(5) -- Display the messages for 5 seconds
  37.  
  38. term.clear()
  39. end
  40. end
  41.  
  42. local function getUserCredentials(username)
  43. local passwordFile = fs.combine(USERS_FOLDER .. username, "password.txt")
  44.  
  45. if fs.exists(passwordFile) then
  46. local file = fs.open(passwordFile, "r")
  47. local storedPassword = file.readLine()
  48. file.close()
  49. return storedPassword
  50. else
  51. return nil -- User does not exist
  52. end
  53. end
  54.  
  55. local function drawLoginScreen(username, attemptsLeft)
  56. term.clear()
  57. term.setCursorPos(1, 1)
  58.  
  59. term.setTextColor(colors.cyan)
  60. term.write("+-------------------------+")
  61. term.setCursorPos(1, 2)
  62. term.write("| Login to Doggy OS |")
  63. term.setCursorPos(1, 3)
  64. term.write("+-------------------------+")
  65.  
  66. term.setCursorPos(1, 5)
  67. term.setTextColor(colors.yellow)
  68. term.write("Username: " .. username)
  69.  
  70. term.setCursorPos(1, 6)
  71. term.write("Attempts left: " .. attemptsLeft)
  72.  
  73. term.setCursorPos(1, 8)
  74. term.setTextColor(colors.white)
  75. term.write("Enter password:")
  76. term.setCursorPos(1, 9)
  77. end
  78.  
  79. local function lockoutUser(username)
  80. local disabledFile = fs.combine(USERS_FOLDER .. username, "disabled.txt")
  81. local file = fs.open(disabledFile, "w")
  82. file.close()
  83. end
  84.  
  85. local function checkDisabled(username)
  86. local disabledFile = fs.combine(USERS_FOLDER .. username, "disabled.txt")
  87. return fs.exists(disabledFile)
  88. end
  89.  
  90. local function checkCredentials(username)
  91. if DEV_MODE or checkDisabled(username) then
  92. if not DEV_MODE then
  93. term.clear()
  94. term.setCursorPos(1, 1)
  95. term.setTextColor(colors.red)
  96. print("This user has been disabled due to security reasons.")
  97. os.sleep(3) -- Display the disabled message for 3 seconds
  98. print("Contact your administrator for help.")
  99. os.sleep(2) -- Display the contact administrator message for 2 seconds
  100. shell.run("/disk/os/lock.lua") -- Run the lock.lua program
  101. end
  102. return DEV_MODE -- In developer mode, return true regardless of user state
  103. end
  104.  
  105. local storedPassword = getUserCredentials(username)
  106.  
  107. if not storedPassword then
  108. return false -- User does not exist
  109. end
  110.  
  111. local attempts = 0
  112.  
  113. repeat
  114. drawLoginScreen(username, MAX_ATTEMPTS - attempts)
  115.  
  116. local enteredPassword = read("*")
  117. attempts = attempts + 1
  118.  
  119. if enteredPassword == storedPassword then
  120. return true
  121. else
  122. term.setCursorPos(1, 10)
  123. term.setTextColor(colors.red)
  124. print("Incorrect password. Please try again.")
  125. os.sleep(2) -- Display the error message for 2 seconds
  126. end
  127. until attempts > MAX_ATTEMPTS
  128.  
  129. print("Too many incorrect attempts. User has been disabled.")
  130. lockoutUser(username)
  131. os.sleep(2) -- Display the lockout message for 2 seconds
  132.  
  133. return false
  134. end
  135.  
  136. local function checkDiskIDs()
  137. -- Get a list of all connected peripherals
  138. local peripherals = peripheral.getNames()
  139.  
  140. -- Array to store disk IDs
  141. local diskIDs = {}
  142.  
  143. -- Loop through all peripherals
  144. for _, name in ipairs(peripherals) do
  145. -- Check if the peripheral is a disk drive
  146. if peripheral.getType(name) == "drive" then
  147. -- Get the disk ID from the disk drive
  148. local diskID = disk.getID(name)
  149.  
  150. -- If a disk is inserted, add its ID to the array
  151. if diskID then
  152. table.insert(diskIDs, {id = diskID, name = name})
  153. end
  154. end
  155. end
  156.  
  157. -- Check if any disks were found
  158. if #diskIDs > 0 then
  159. return diskIDs
  160. else
  161. return nil
  162. end
  163. end
  164.  
  165. local function drawSecurityCardPrompt()
  166. term.clear()
  167. term.setCursorPos(1, 1)
  168.  
  169. term.setTextColor(colors.blue)
  170. term.write("+--------------------------+")
  171. term.setCursorPos(1, 2)
  172. term.write("| Insert Security Card |")
  173. term.setCursorPos(1, 3)
  174. term.write("+--------------------------+")
  175.  
  176. term.setCursorPos(1, 5)
  177. term.setTextColor(colors.yellow)
  178. term.write("Please insert your security card.")
  179. term.setCursorPos(1, 6)
  180. term.write("Press ENTER to use password instead.")
  181. end
  182.  
  183. local function drawErrorMessage(message)
  184. term.clear()
  185. term.setCursorPos(1, 1)
  186.  
  187. term.setTextColor(colors.red)
  188. print(message)
  189.  
  190. os.sleep(2)
  191. end
  192.  
  193. local function ejectDisk(diskName)
  194. peripheral.call(diskName, "ejectDisk")
  195. end
  196.  
  197. local function checkSecurityCard(username)
  198. if DEV_MODE then
  199. return true -- In developer mode, bypass security card check
  200. end
  201.  
  202. local idFolder = fs.combine(USERS_FOLDER .. username, "ID")
  203. if not fs.exists(idFolder) then
  204. return false
  205. end
  206.  
  207. while true do
  208. drawSecurityCardPrompt()
  209.  
  210. local event, key = os.pullEvent()
  211.  
  212. if event == "key" and key == keys.enter then
  213. return false
  214. elseif event == "disk" or event == "disk_insert" then
  215. local diskIDs = checkDiskIDs()
  216. if diskIDs then
  217. for _, disk in ipairs(diskIDs) do
  218. ejectDisk(disk.name) -- Eject the disk
  219. local idFile = fs.combine(idFolder, tostring(disk.id) .. ".file")
  220. if fs.exists(idFile) then
  221. return true -- Allow access if a valid ID is found
  222. end
  223. end
  224. drawErrorMessage("Error: Unregistered security key.")
  225. end
  226. end
  227. end
  228. end
  229.  
  230. local function main()
  231. checkDeveloperMode() -- Check if developer mode should be enabled
  232.  
  233. term.clear()
  234. term.setCursorPos(1, 1)
  235.  
  236. term.setTextColor(colors.green)
  237. term.write("+-------------------------------------+")
  238. term.setCursorPos(1, 2)
  239. term.write("| Protected by Doggy OS Security |")
  240. term.setCursorPos(1, 3)
  241. term.write("+-------------------------------------+")
  242.  
  243. term.setCursorPos(1, 5)
  244. term.setTextColor(colors.white)
  245. print("Enter username:")
  246.  
  247. local enteredUsername = read()
  248.  
  249. if DEV_MODE or checkDisabled(enteredUsername) then
  250. term.clear()
  251. term.setCursorPos(1, 1)
  252. term.setTextColor(colors.red)
  253. print("This user has been disabled due to security reasons.")
  254. os.sleep(3) -- Display the disabled message for 3 seconds
  255. print("Contact your administrator for help.")
  256. os.sleep(2) -- Display the contact administrator message for 2 seconds
  257. shell.run("/disk/os/lock.lua") -- Run the lock.lua program
  258. return
  259. end
  260.  
  261. local users = fs.list(USERS_FOLDER)
  262.  
  263. if not users or #users == 0 then
  264. -- No users detected, run the BSOD program
  265. shell.run(ERROR_FOLDER .. BSOD_PROGRAM)
  266. elseif checkSecurityCard(enteredUsername) then
  267. term.clear()
  268. term.setCursorPos(1, 1)
  269.  
  270. term.setTextColor(colors.lime)
  271. print("Access granted. Welcome, " .. enteredUsername .. "!")
  272. os.sleep(2) -- Display the success message for 2 seconds
  273. term.setTextColor(colors.white)
  274. shell.run("/disk/os/gui")
  275. -- Your main OS code goes here
  276. elseif checkCredentials(enteredUsername) then
  277. term.clear()
  278. term.setCursorPos(1, 1)
  279.  
  280. term.setTextColor(colors.lime)
  281. print("Access granted. Welcome, " .. enteredUsername .. "!")
  282. os.sleep(2) -- Display the success message for 2 seconds
  283. term.setTextColor(colors.white)
  284. shell.run("/disk/os/gui")
  285. -- Your main OS code goes here
  286. else
  287. term.clear()
  288. term.setCursorPos(1, 1)
  289.  
  290. term.setTextColor(colors.red)
  291. print("Access denied.")
  292. os.sleep(2) -- Display the access denied message for 2 seconds
  293. shell.run("/disk/os/lock.lua")
  294. end
  295. end
  296.  
  297. main()
  298.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement