· 10 months ago · Feb 25, 2025, 09:55 PM
1-- Function to draw a border around the screen
2local function drawBorder()
3 term.clear()
4 local w, h = term.getSize()
5 for y = 1, h do
6 term.setCursorPos(1, y)
7 term.write("|")
8 term.setCursorPos(w, y)
9 term.write("|")
10 end
11 for x = 1, w do
12 term.setCursorPos(x, 1)
13 term.write("-")
14 term.setCursorPos(x, h)
15 term.write("-")
16 end
17 term.setCursorPos(1, 1)
18 term.write("+")
19 term.setCursorPos(w, 1)
20 term.write("+")
21 term.setCursorPos(1, h)
22 term.write("+")
23 term.setCursorPos(w, h)
24 term.write("+")
25end
26
27-- Function to center text on the screen
28local function centerText(text, y)
29 local w, _ = term.getSize()
30 local x = math.floor((w - #text) / 2) + 1
31 term.setCursorPos(x, y)
32 term.write(text)
33end
34
35-- Function to verify user credentials
36local function verifyCredentials(username, password)
37 local userPath = "/disk/users/" .. username
38 if fs.exists(userPath .. "/admin.txt") and fs.exists(userPath .. "/password.txt") then
39 local file = fs.open(userPath .. "/password.txt", "r")
40 local storedPassword = file.readAll()
41 file.close()
42 return storedPassword == password
43 end
44 return false
45end
46
47-- Function to authenticate the user
48local function authenticate()
49 term.clear()
50 drawBorder()
51 local _, h = term.getSize()
52 centerText("Doggy OS UEFI Authentication", 3)
53
54 local w, _ = term.getSize()
55 local usernameX = math.floor(w / 2) - 10
56
57 term.setCursorPos(usernameX, 5)
58 term.write("Username: ")
59 term.setCursorPos(usernameX + 10, 5)
60 local username = read()
61
62 term.setCursorPos(usernameX, 7)
63 term.write("Password: ")
64 term.setCursorPos(usernameX + 10, 7)
65 local password = read("*") -- Masked input for password
66
67 if verifyCredentials(username, password) then
68 showMenu()
69 else
70 centerText("Invalid credentials. Access denied.", 10)
71 os.sleep(2)
72
73 -- Prompt if they have forgotten their password
74 term.clear()
75 drawBorder()
76 centerText("Forgot your password? (Y/N)", math.floor(h / 2) + 2)
77 term.setCursorPos(1, math.floor(h / 2) + 3)
78 local response = string.lower(read())
79
80 if response == "y" then
81 recoveryKey() -- Call the function for recovery
82 else
83 os.reboot()
84 end
85 end
86end
87
88-- Function to show the recovery prompt
89local function recoveryKey()
90 term.clear()
91 drawBorder()
92 local _, h = term.getSize()
93 centerText("Password Recovery", math.floor(h / 2) - 2)
94
95 local w, _ = term.getSize()
96 local recoveryKeyX = math.floor(w / 2) - 10
97
98 term.setCursorPos(recoveryKeyX, math.floor(h / 2))
99 term.write("Enter recovery key: ")
100 term.setCursorPos(recoveryKeyX + 20, math.floor(h / 2))
101 local recoveryKey = read()
102
103 if verifyRecoveryKey(recoveryKey) then
104 centerText("Recovery successful.", math.floor(h / 2) + 1)
105 os.sleep(2)
106 -- Reset password or other actions
107 showMenu() -- Call the menu if recovery was successful
108 else
109 centerText("Invalid recovery key.", math.floor(h / 2) + 1)
110 os.sleep(2)
111 os.reboot()
112 end
113end
114
115-- Function to verify the recovery key
116local function verifyRecoveryKey(recoveryKey)
117 local filePath = "/recovery/rckey.txt"
118 if not fs.exists(filePath) then
119 centerText("Recovery key file not found.", math.floor(h / 2) + 1)
120 os.sleep(2)
121 os.reboot()
122 return false
123 end
124
125 local file = fs.open(filePath, "r")
126 if not file then
127 centerText("Error opening recovery key file.", math.floor(h / 2) + 1)
128 os.sleep(2)
129 os.reboot()
130 return false
131 end
132
133 local storedKey = file.readAll()
134 file.close()
135
136 return storedKey == recoveryKey
137end
138
139-- Function to show UEFI options menu after successful authentication
140local function showMenu()
141 term.clear()
142 drawBorder()
143 local _, h = term.getSize()
144 centerText("Doggy OS UEFI Options", math.floor(h / 2) - 2)
145 centerText("1. Recovery", math.floor(h / 2))
146 centerText("2. Bootable devices", math.floor(h / 2) + 1)
147 centerText("3. Shutdown", math.floor(h / 2) + 2)
148 term.setCursorPos(1, math.floor(h / 2) + 3)
149 term.write("Select an option: ")
150
151 local choice = tonumber(read())
152 if choice == 1 then
153 -- Recovery
154 shell.run("/disk/boot/Recovery.lua")
155 elseif choice == 2 then
156 -- Bootable devices (assuming this function exists)
157 listBootableDevices()
158 elseif choice == 3 then
159 os.shutdown()
160 else
161 centerText("Invalid selection, rebooting.", math.floor(h / 2) + 4)
162 os.sleep(2)
163 os.reboot()
164 end
165end
166
167-- Start the authentication process
168authenticate()
169