fatboychummy

Itsathing.lua

Nov 15th, 2018
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.25 KB | None | 0 0
  1.  
  2.  
  3. oldEvent = os.pullEvent
  4. os.pullEvent = os.pullEventRaw --no terminate rEEEEEE
  5.  
  6. if not fs.exists("hash") then
  7.   shell.run("pastebin get 6UV4qfNF hash")
  8. end
  9. os.loadAPI("hash")
  10. local data = {}
  11. local users = {}
  12. local white = {}
  13. local black = {}
  14. local listBlock = {}
  15. local craftOSVersion = 1.8
  16.  
  17.  
  18.  
  19. local function cleanExit(err)
  20.   os.pullEvent = oldEvent
  21.   if err then
  22.     printError(err)
  23.   end
  24.   while true do
  25.     os.queueEvent("exit")
  26.     os.sleep()
  27.   end
  28. end
  29.  
  30.  
  31. local function deepCopy(from,to)
  32.   if type(from) == "table" then
  33.     for k,v in pairs(from) do
  34.       if type(v) == "table" then
  35.         to[k] = {}
  36.         deepCopy(from[k],to[k])
  37.       else
  38.         to[k] = v
  39.       end
  40.     end
  41.   end
  42. end
  43.  
  44. local function saveData()
  45.   fs.delete(".data")
  46.   h = fs.open(".data","w")
  47.   h.write(textutils.serialize(data))
  48.   h.close()
  49.   print("Successfully saved data.  You will need to reboot to utilize the new data.")
  50. end
  51.  
  52. local function makeDefaultData()
  53.   local def = "print('This is a placeholder program for autocomplete in the lock')"
  54.   local a = fs.open("login","w")
  55.   a.writeLine(def)
  56.   a.close()
  57.   a = fs.open("users","w")
  58.   a.writeLine(def)
  59.   a.close()
  60.   a = fs.open("newUser","w")
  61.   a.writeLine(def)
  62.   a.close()
  63.   local h = fs.open(".data","w")
  64.   h.writeLine("{")
  65.   h.writeLine("  craftOSVersion = \"LOCKED\",")
  66.   h.writeLine("  MOTD = \"Edit .data to change this information...  Use command 'newUser' to create your first account.  User/pass is 'admin'\",")
  67.   h.writeLine("  users = {")
  68.   local hesh = hash.pbkdf2("admin","salty",3)
  69.   for i = 1,#hesh do
  70.     hesh[i] = string.char(hesh[i])
  71.   end
  72.   hesh = table.concat(hesh)
  73.   h.writeLine("    [\"admin\"] = {i = 3, s = \"salty\", o = \"" .. hesh .. "\"},")
  74.   h.writeLine("    --['username'] = {i = iterations, s = salt, o = pbkdf2_Hashed_Password}")
  75.   h.writeLine("  },")
  76.   h.writeLine("  whitelist = {")
  77.   h.writeLine("    \"login\",")
  78.   h.writeLine("    \"help\",")
  79.   h.writeLine("    \"users\",")
  80.   h.writeLine("    \"newUser\",")
  81.   h.writeLine("  },")
  82.   h.writeLine("}")
  83.   h.close()
  84.   printError("System has been setup.  Reboot (ctrl+r) then login with the user AND pass \"admin\".")
  85.   while true do
  86.     os.sleep(10)
  87.   end
  88. end
  89.  
  90. local function gatherData()
  91.   local h = fs.open(".data","r")
  92.   if h then
  93.     local a = h.readAll()
  94.     h.close()
  95.     data = textutils.unserialize(a)
  96.     if type(data) == "table" then
  97.       if type(data.users) == "table" then
  98.         deepCopy(data.users,users)
  99.       else
  100.         cleanExit("User table empty, add a user to lock.")
  101.       end
  102.       if type(data.whitelist) == "table" then
  103.         deepCopy(data.whitelist,white)
  104.       else
  105.         white = false
  106.       end
  107.       if type(data.blacklist) == "table" then
  108.         deepCopy(data.blacklist,black)
  109.       else
  110.         black = false
  111.       end
  112.       if type(data.noLS) == "table" then
  113.         deepCopy(data.noLS,listBlock)
  114.       else
  115.         noLS = false
  116.       end
  117.       if data.craftOSVersion then
  118.         craftOSVersion = data.craftOSVersion
  119.       end
  120.     else
  121.       cleanExit("Data table is borked")
  122.     end
  123.   else
  124.     makeDefaultData()
  125.   end
  126. end
  127.  
  128. local function getValidCompletions(buffer)
  129.   buffer = table.concat(buffer)
  130.   local a = shell.complete(buffer)
  131.   if a then
  132.     if white then
  133.       for i = 1,#a do
  134.         local flag = false
  135.         for o = 1,#white do
  136.           if a[i] then
  137.             if (buffer..a[i]):find(white[o]) then
  138.               flag = true
  139.             end
  140.           end
  141.         end
  142.         if not flag then
  143.           a[i] = nil
  144.         end
  145.       end
  146.     end
  147.     if black then
  148.       for i = 1,#a do
  149.         local flag = false
  150.         for o = 1,#black do
  151.           if a[i] then
  152.             if (buffer..a[i]):find(black[o]) == nil then
  153.               flag = true
  154.             end
  155.           end
  156.         end
  157.         if not flag then
  158.           a[i] = nil
  159.         end
  160.       end
  161.     end
  162.   else
  163.     a = {}
  164.   end
  165.  
  166.   local i = 1
  167.   local b = {}
  168.   for k,v in pairs(a) do
  169.     if type(v) == "string" then
  170.       b[i] = v
  171.       i = i + 1
  172.     end
  173.   end
  174.  
  175.   return b
  176. end
  177.  
  178. local function readAThing()
  179.   local sX,sY = term.getCursorPos()
  180.   local mX,mY = term.getSize()
  181.   local buf = {}
  182.   local cComp = 1
  183.   local pos = 0
  184.   local validComps = {}
  185.   while true do
  186.     term.setCursorBlink(true)
  187.  
  188.     local ev = {os.pullEvent()}
  189.     if ev[1] == "char" then
  190.       table.insert(buf,pos+1,ev[2])
  191.       cComp = 1
  192.       pos = pos + 1
  193.     elseif ev[1] == "key" then
  194.       if ev[2] == keys.backspace then
  195.         if pos > 0 then
  196.           table.remove(buf,pos)
  197.           cComp = 1
  198.           pos = pos - 1
  199.         end
  200.       elseif ev[2] == keys.enter then
  201.         local cX = term.getCursorPos()
  202.         print(string.rep(" ",mX-cX-1))
  203.         term.setCursorBlink(false)
  204.         return table.concat(buf)
  205.       elseif ev[2] == keys.up then
  206.         cComp = cComp + 1
  207.         if cComp > #validComps then cComp = 1 end
  208.       elseif ev[2] == keys.down then
  209.         cComp = cComp - 1
  210.         if cComp < 1 then
  211.           cComp = #validComps
  212.           if cComp == 0 then
  213.             cComp = 1
  214.           end
  215.         end
  216.       elseif ev[2] == keys.right then
  217.         if pos == #buf then
  218.           local completion = validComps[cComp]
  219.           if completion then
  220.             for i = 1,completion:len() do
  221.               buf[#buf+1] = string.sub(completion,i,i)
  222.               pos = pos + 1
  223.             end
  224.           end
  225.         else
  226.           pos = pos + 1
  227.           if pos > #buf then pos = #buf end
  228.         end
  229.       end
  230.     elseif ev[2] == keys.left then
  231.       pos = pos - 1
  232.       if pos < 0 then pos = 0 end
  233.     elseif ev[2] == keys.tab then
  234.       if pos == #buf then
  235.         local completion = validComps[cComp]
  236.         if completion then
  237.           for i = 1,completion:len() do
  238.             buf[#buf+1] = string.sub(completion,i,i)
  239.             pos = pos + 1
  240.           end
  241.         end
  242.       end
  243.     end
  244.     term.setCursorPos(sX,sY)
  245.     term.write(string.rep(" ",mX-sX))
  246.     term.setCursorPos(sX,sY)
  247.     local st = #buf + sX - (mX-1)
  248.     if st > 0 then
  249.       term.write(table.concat(buf,"",st))
  250.     else
  251.       term.write(table.concat(buf))
  252.     end
  253.     if #buf > 0 then
  254.       validComps = getValidCompletions(buf)
  255.     else
  256.       validComps = {}
  257.     end
  258.     local a = validComps[cComp]
  259.     if a then
  260.       term.setBackgroundColor(colors.gray)
  261.       term.write(a)
  262.       term.setBackgroundColor(colors.black)
  263.     end
  264.     term.setCursorPos(sX+pos,sY)
  265.   end
  266. end
  267.  
  268. local function doRead()
  269.   term.setBackgroundColor(colors.black)
  270.   term.setTextColor(colors.yellow)
  271.   term.write("> ")
  272.   term.setTextColor(colors.white)
  273.   return readAThing()
  274. end
  275.  
  276.  
  277. local function printStartup()
  278.   term.setBackgroundColor(colors.black)
  279.   term.clear()
  280.   term.setTextColor(colors.yellow)
  281.   term.setCursorPos(1,1)
  282.   print("CraftOS "..tostring(craftOSVersion))
  283.   if data.MOTD then
  284.     term.setTextColor(colors.white)
  285.     print(data.MOTD)
  286.   end
  287. end
  288.  
  289. local function login()
  290.   term.write("User: ")
  291.   local usr = read()
  292.   if users[usr] then
  293.     term.write("Pass: ")
  294.     local pass = read("*")
  295.     pass = hash.pbkdf2(pass,users[usr].s,users[usr].i)
  296.     for i = 1,#pass do
  297.       pass[i] = string.char(pass[i])
  298.     end
  299.     pass = table.concat(pass)
  300.     if users[usr].o == pass then
  301.       return true
  302.     else
  303.       printError("User and pass do not match.")
  304.       return false
  305.     end
  306.   else
  307.     printError("No user: "..tostring(usr))
  308.     return false
  309.   end
  310. end
  311.  
  312. local function runAThing(thing)
  313.   if thing then
  314.     if thing == "login" then
  315.       if login() then
  316.         cleanExit()
  317.       end
  318.       return
  319.     elseif thing == "newUser" then
  320.       print("Log in first.")
  321.       if login() then
  322.         print("Input username: ")
  323.         local username = read()
  324.         print("Input password: ")
  325.         local password = read("*")
  326.         print("Reinput password: ")
  327.         local p2 = read("*")
  328.         if password == p2 then
  329.           print("Input password salt: ")
  330.           local salt = read()
  331.           local iter = math.random(2,5)
  332.           if data.users["admin"] then
  333.             data.users["admin"] = nil
  334.             print("Initial 'admin' user deleted.")
  335.           end
  336.           password = hash.pbkdf2(password,salt,iter)
  337.           for i = 1,#password do
  338.             password[i] = string.char(password[i])
  339.           end
  340.           password = table.concat(password)
  341.           data.users[username] = {
  342.             s = salt,
  343.             i = iter,
  344.             o = password,
  345.           }
  346.           saveData()
  347.         else
  348.           printError("The passwords do not match.")
  349.         end
  350.       end
  351.       return
  352.     elseif thing == "help" then
  353.       if white then
  354.         print("Allowed commands/programs:")
  355.         for i = 1,#white do
  356.           print(" ",white[i])
  357.         end
  358.       elseif black then
  359.         print("Blacklisted commands/programs:")
  360.         for i = 1,#black do
  361.           print(" ",black[i])
  362.         end
  363.       end
  364.       return
  365.     elseif thing == "users" then
  366.       for k,v in pairs(users) do
  367.         print(k)
  368.       end
  369.       return
  370.     end
  371.     parallel.waitForAny(
  372.     function ()
  373.       local canRun = false
  374.       if white then
  375.         for i = 1,#white do
  376.           if thing:find(white[i]) then
  377.             canRun = true
  378.           end
  379.         end
  380.       end
  381.       if black then
  382.         for i = 1,#black do
  383.           if thing:find(black[i]) then
  384.             canRun = false
  385.           end
  386.         end
  387.       end
  388.       if canRun then
  389.         shell.run(thing)
  390.       else
  391.         printError("Access denied.  Try 'help'.")
  392.       end
  393.     end,
  394.     function ()
  395.       while true do
  396.         local ev = os.pullEvent()
  397.         if ev == "terminate" then
  398.           printError("Terminated")
  399.           break
  400.         end
  401.       end
  402.     end
  403.     )
  404.   else
  405.     printError("Shell.lua:666:Cannot handle")
  406.   end
  407. end
  408.  
  409. local function main()
  410.   gatherData()
  411.   printStartup()
  412.   while true do
  413.     runAThing(doRead())
  414.   end
  415. end
  416.  
  417. local function watch()
  418.   while true do
  419.     local ev = os.pullEvent()
  420.     if ev == "exit" then
  421.       return
  422.     end
  423.   end
  424. end
  425.  
  426.  
  427.  
  428.  
  429. parallel.waitForAny(main,watch)
  430. term.setBackgroundColor(colors.black)
  431. term.setTextColor(colors.yellow)
  432. term.clear()
  433. term.setCursorPos(1,1)
  434. print("CraftOS 1.8")
Add Comment
Please, Sign In to add comment