Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local sides = {"top", "bottom", "left", "right", "back", "front"}
- local serverID = os.computerID()
- local users = {}
- local passwords = {}
- function clear()
- term.clear()
- term.setCursorPos(1,1)
- end
- function updateAdminText()
- print("Welcome, admin.")
- print("What would you like to do?")
- print("[1] Make a new account.")
- print("[2] Quit the server.")
- print("[3] Return to normal server mode.")
- print("[4] Change the admin password.")
- print("[5] Print out the current users database.")
- write("> ")
- end
- function saveUsersVar()
- local usersFile = fs.open("users", "w")
- usersFile.write(textutils.serialize(users))
- usersFile.close()
- end
- function getUsersVar()
- local usersFile = fs.open("users", "r")
- users = textutils.unserialize(usersFile.readAll())
- usersFile.close()
- end
- function savePasswordsVar()
- local passwordsFile = fs.open("passwords", "w")
- passwordsFile.write(textutils.serialize(passwords))
- passwordsFile.close()
- end
- function getPasswordsVar()
- local passwordsFile = fs.open("passwords", "r")
- passwords = textutils.unserialize(passwordsFile.readAll())
- passwordsFile.close()
- end
- function bootUp()
- local rnetSuccess = false
- for i = 1, #sides do
- rednet.open(sides[i])
- if peripheral.getType(sides[i]) == "modem" then
- rnetSuccess = true
- end
- end
- if not rnetSuccess then
- print("Error: No wireless modem attached.")
- error()
- end
- if not fs.exists("adminPassword") then
- local adminPass = ""
- repeat
- clear()
- textutils.slowPrint("No admin password detected.")
- textutils.slowWrite("Enter a new password: ")
- adminPass = read()
- until adminPass ~= ""
- adminPassFile = fs.open("adminPassword", "w")
- adminPassFile.write(adminPass)
- adminPassFile.close()
- end
- if not fs.exists("users") then
- saveUsersVar()
- else
- getUsersVar()
- end
- if not fs.exists("passwords") then
- savePasswordsVar()
- else
- getPasswordsVar()
- end
- end
- function newAccount(newUsername, newPassword)
- local usernameKey = usernameToKey(newUsername)
- if usernameKey == nil then
- table.insert(users, 1, newUsername)
- table.insert(passwords, 1, newPassword)
- print("New account created:")
- print("New username: "..username)
- print("New password: "..password)
- sleep(2)
- else
- print("Username "..newUsername.." already in use:")
- print("Setting "..newUsername.."\'s password as "..newPassword..".")
- passwords[usernameKey] = newPassword
- sleep(3)
- end
- saveUsersVar()
- savePasswordsVar()
- end
- function usernameToKey(username)
- for i = 1, #users do
- if users[i] == username then
- return i
- end
- end
- return nil
- end
- function adminMode()
- while true do
- clear()
- updateAdminText()
- local option = read()
- if option == "1" then
- repeat
- local continue = true
- write("Enter the new account's username: ")
- username = read()
- write("Enter the new account's password: ")
- password = read()
- if username == "" then
- print("Enter a username.")
- continue = false
- end
- if password == "" then
- print("Enter a password.")
- continue = false
- end
- if username == "rom" or username == "startup" or username == "adminPassword" or username == "passwords" or username == "users" then
- print("Username cannot be: rom, startup, adminPassword, users, passwords.")
- continue = false
- end
- until continue
- newAccount(username, password)
- elseif option == "2" then
- clear()
- write("Entering normal command-line mode")
- os.sleep(0.2)
- textutils.slowPrint("...", 3)
- os.sleep(0.2)
- error()
- elseif option == "3" then
- clear()
- write("Returning to normal server mode")
- os.sleep(0.2)
- textutils.slowPrint("...", 3)
- os.sleep(0.2)
- return
- elseif option == "4" then
- local newPassword = ""
- repeat
- write("Enter the new password: ")
- newPassword = read()
- until newPassword ~= ""
- adminPassFile = fs.open("adminPassword", "w")
- adminPassFile.write(newPassword)
- adminPassFile.close()
- print("Successfully changed the password to "..newPassword..".")
- sleep(1.5)
- elseif option == "5" then
- clear()
- print("Username(Password)")
- for key, user in ipairs(users) do
- print(users[key].."("..passwords[key]..")")
- sleep(0.5)
- end
- print("Press any key to continue...")
- os.pullEvent("key")
- sleep(0.2)
- else
- print("Not a valid option.")
- os.sleep(1.5)
- end
- end
- end
- function updateText()
- print("This is the login server with ID "..serverID..".")
- print("Only the administrator can login here.")
- print("Enter the password to be granted access.")
- write("> ")
- end
- function normalMode()
- while true do
- clear()
- updateText()
- local password = read("*")
- local adminPassFile = fs.open("adminPassword", "r")
- adminPass = adminPassFile.readLine()
- adminPassFile.close()
- if password == adminPass then
- adminMode()
- else
- print("Password invalid!")
- sleep(2)
- end
- end
- end
- --======Rednet Message Interpreter™======--
- ----To Server----
- --LOGIN = Signalling server to receive username and password in next 2 messages
- ----To Terminal----
- --ERROR100 = Error recieving message
- --ERROR101 = Username doesn't exist - ask admin to make one
- --ERROR102 = Invalid password!
- --VALIDATED = No errors! Yay!
- --NOMOREFILES = No more files to send
- function serverSub()
- while true do
- local senderID, message = rednet.receive()
- rednet.send(senderID, message)
- if message == "LOGIN" then
- local senderID2, username = rednet.receive(10)
- local senderID3, password = rednet.receive(10)
- if senderID ~= senderID2 or senderID ~= senderID3 then
- rednet.send(senderID, "ERROR100")
- else
- parallel.waitForAll(userLogin(senderID, username, password), serverSub())
- end
- end
- end
- end
- userLogin(senderID, username, password)
- local accountKey = usernameToKey(username)
- if accountKey == nil then
- rednet.send(senderID, "ERROR101")
- else
- if password ~= passwords[accountKey] then
- rednet.send(senderID, "ERROR102")
- else
- rednet.send("VALIDATED")
- if fs.exists("/"..username.."/") then
- local stop = false
- repeat
- print(":D file sending :P")
- local mkDirs = {}
- local currentDir = ""
- local list = fs.list(currentDir)
- for _, file in ipairs(list) do
- if fs.isDir(fs.combine(currentDir, file)) then
- print(":"..currentDir..":"..file..":")
- end
- end
- until stop
- end
- rednet.send("NOMOREFILES")
- end
- end
- end
- bootUp()
- parallel.waitForAny(normalMode(), serverSub())
Add Comment
Please, Sign In to add comment