Guest User

Untitled

a guest
Dec 12th, 2018
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.44 KB | None | 0 0
  1. local sides = {"top", "bottom", "left", "right", "back", "front"}
  2. local serverID = os.computerID()
  3. local users = {}
  4. local passwords = {}
  5.  
  6. function clear()
  7.    term.clear()
  8.    term.setCursorPos(1,1)
  9. end
  10.  
  11. function updateAdminText()
  12.       print("Welcome, admin.")
  13.       print("What would you like to do?")
  14.       print("[1] Make a new account.")
  15.       print("[2] Quit the server.")
  16.       print("[3] Return to normal server mode.")
  17.       print("[4] Change the admin password.")
  18.       print("[5] Print out the current users database.")
  19.       write("> ")
  20. end
  21.  
  22. function saveUsersVar()
  23.    local usersFile = fs.open("users", "w")
  24.    usersFile.write(textutils.serialize(users))
  25.    usersFile.close()
  26. end
  27.  
  28. function getUsersVar()
  29.    local usersFile = fs.open("users", "r")
  30.    users = textutils.unserialize(usersFile.readAll())
  31.    usersFile.close()
  32. end
  33.  
  34. function savePasswordsVar()
  35.    local passwordsFile = fs.open("passwords", "w")
  36.    passwordsFile.write(textutils.serialize(passwords))
  37.    passwordsFile.close()
  38. end
  39.  
  40. function getPasswordsVar()
  41.    local passwordsFile = fs.open("passwords", "r")
  42.    passwords = textutils.unserialize(passwordsFile.readAll())
  43.    passwordsFile.close()
  44. end
  45.  
  46. function bootUp()
  47.    local rnetSuccess = false
  48.    for i = 1, #sides do
  49.       rednet.open(sides[i])
  50.       if peripheral.getType(sides[i]) == "modem" then
  51.          rnetSuccess = true
  52.       end
  53.    end
  54.    if not rnetSuccess then
  55.       print("Error: No wireless modem attached.")
  56.       error()
  57.    end
  58.    if not fs.exists("adminPassword") then
  59.       local adminPass = ""
  60.       repeat
  61.          clear()
  62.          textutils.slowPrint("No admin password detected.")
  63.          textutils.slowWrite("Enter a new password: ")
  64.          adminPass = read()
  65.       until adminPass ~= ""
  66.       adminPassFile = fs.open("adminPassword", "w")
  67.       adminPassFile.write(adminPass)
  68.       adminPassFile.close()
  69.    end
  70.    if not fs.exists("users") then
  71.       saveUsersVar()
  72.    else
  73.       getUsersVar()
  74.    end
  75.    if not fs.exists("passwords") then
  76.       savePasswordsVar()
  77.    else
  78.       getPasswordsVar()
  79.    end
  80. end
  81.  
  82. function newAccount(newUsername, newPassword)
  83.    local usernameKey = usernameToKey(newUsername)
  84.    if usernameKey == nil then
  85.       table.insert(users, 1, newUsername)
  86.       table.insert(passwords, 1, newPassword)
  87.       print("New account created:")
  88.       print("New username: "..username)
  89.       print("New password: "..password)
  90.       sleep(2)
  91.    else
  92.       print("Username "..newUsername.." already in use:")
  93.       print("Setting "..newUsername.."\'s password as "..newPassword..".")
  94.       passwords[usernameKey] = newPassword
  95.       sleep(3)
  96.    end
  97.    saveUsersVar()
  98.    savePasswordsVar()
  99. end
  100.  
  101. function usernameToKey(username)
  102.    for i = 1, #users do
  103.       if users[i] == username then
  104.          return i
  105.       end
  106.    end
  107.    return nil
  108. end
  109.  
  110. function adminMode()
  111.    while true do
  112.       clear()
  113.       updateAdminText()
  114.       local option = read()
  115.       if option == "1" then
  116.          repeat
  117.             local continue = true
  118.             write("Enter the new account's username: ")
  119.             username = read()
  120.             write("Enter the new account's password: ")
  121.             password = read()
  122.             if username == "" then
  123.                print("Enter a username.")
  124.                continue = false
  125.             end
  126.             if password == "" then
  127.                print("Enter a password.")
  128.                continue = false
  129.             end
  130.             if username == "rom" or username == "startup" or username == "adminPassword" or username == "passwords" or username == "users" then
  131.                print("Username cannot be: rom, startup, adminPassword, users, passwords.")
  132.                continue = false
  133.             end
  134.          until continue
  135.          newAccount(username, password)
  136.       elseif option == "2" then
  137.          clear()
  138.          write("Entering normal command-line mode")
  139.          os.sleep(0.2)
  140.          textutils.slowPrint("...", 3)
  141.          os.sleep(0.2)
  142.          error()
  143.       elseif option == "3" then
  144.          clear()
  145.          write("Returning to normal server mode")
  146.          os.sleep(0.2)
  147.          textutils.slowPrint("...", 3)
  148.          os.sleep(0.2)
  149.          return
  150.       elseif option == "4" then
  151.          local newPassword = ""
  152.          repeat
  153.             write("Enter the new password: ")
  154.             newPassword = read()
  155.          until newPassword ~= ""
  156.          adminPassFile = fs.open("adminPassword", "w")
  157.          adminPassFile.write(newPassword)
  158.          adminPassFile.close()
  159.          print("Successfully changed the password to "..newPassword..".")
  160.          sleep(1.5)
  161.       elseif option == "5" then
  162.          clear()
  163.          print("Username(Password)")
  164.          for key, user in ipairs(users) do
  165.             print(users[key].."("..passwords[key]..")")
  166.             sleep(0.5)
  167.          end
  168.          print("Press any key to continue...")
  169.          os.pullEvent("key")
  170.          sleep(0.2)
  171.       else
  172.          print("Not a valid option.")
  173.          os.sleep(1.5)
  174.       end
  175.    end
  176. end
  177.  
  178. function updateText()
  179.    print("This is the login server with ID "..serverID..".")
  180.    print("Only the administrator can login here.")
  181.    print("Enter the password to be granted access.")
  182.    write("> ")
  183. end
  184.  
  185. function normalMode()
  186.    while true do
  187.       clear()
  188.       updateText()
  189.       local password = read("*")
  190.       local adminPassFile = fs.open("adminPassword", "r")
  191.       adminPass = adminPassFile.readLine()
  192.       adminPassFile.close()
  193.       if password == adminPass then
  194.          adminMode()
  195.       else
  196.          print("Password invalid!")
  197.          sleep(2)
  198.       end
  199.    end
  200. end
  201.  
  202. --======Rednet Message Interpreter™======--
  203. ----To Server----
  204. --LOGIN = Signalling server to receive username and password in next 2 messages
  205. ----To Terminal----
  206. --ERROR100 = Error recieving message
  207. --ERROR101 = Username doesn't exist - ask admin to make one
  208. --ERROR102 = Invalid password!
  209. --VALIDATED = No errors! Yay!
  210. --NOMOREFILES = No more files to send
  211.  
  212. function serverSub()
  213.    while true do
  214.       local senderID, message = rednet.receive()
  215.       rednet.send(senderID, message)
  216.       if message == "LOGIN" then
  217.          local senderID2, username = rednet.receive(10)
  218.          local senderID3, password = rednet.receive(10)
  219.          if senderID ~= senderID2 or senderID ~= senderID3 then
  220.             rednet.send(senderID, "ERROR100")
  221.          else
  222.             parallel.waitForAll(userLogin(senderID, username, password), serverSub())
  223.          end
  224.       end
  225.    end
  226. end
  227.  
  228. userLogin(senderID, username, password)
  229.    local accountKey = usernameToKey(username)
  230.    if accountKey == nil then
  231.       rednet.send(senderID, "ERROR101")
  232.    else
  233.       if password ~= passwords[accountKey] then
  234.          rednet.send(senderID, "ERROR102")
  235.       else
  236.          rednet.send("VALIDATED")
  237.          if fs.exists("/"..username.."/") then
  238.             local stop = false
  239.             repeat
  240.                print(":D file sending :P")
  241.                local mkDirs = {}
  242.                local currentDir = ""
  243.                local list = fs.list(currentDir)
  244.                for _, file in ipairs(list) do
  245.                   if fs.isDir(fs.combine(currentDir, file)) then
  246.                      print(":"..currentDir..":"..file..":")
  247.                   end
  248.                end
  249.             until stop
  250.          end
  251.          rednet.send("NOMOREFILES")
  252.       end
  253.    end
  254. end
  255.  
  256. bootUp()
  257. parallel.waitForAny(normalMode(), serverSub())
Add Comment
Please, Sign In to add comment