cadergator10

Security Server

Jun 18th, 2021 (edited)
275
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local cryptKey = {1, 2, 3, 4, 5}
  2. local modemPort = 199
  3.  
  4. local lockDoors = false
  5.  
  6. local component = require("component")
  7. local event = require("event")
  8. local modem = component.modem
  9. local ser = require ("serialization")
  10. local term = require("term")
  11. local ios = require("io")
  12.  
  13. local function convert( chars, dist, inv )
  14.   return string.char( ( string.byte( chars ) - 32 + ( inv and -dist or dist ) ) % 95 + 32 )
  15. end
  16.  
  17.  
  18. local function crypt(str,k,inv)
  19.   local enc= "";
  20.   for i=1,#str do
  21.     if(#str-k[5] >= i or not inv)then
  22.       for inc=0,3 do
  23.     if(i%4 == inc)then
  24.       enc = enc .. convert(string.sub(str,i,i),k[inc+1],inv);
  25.       break;
  26.     end
  27.       end
  28.     end
  29.   end
  30.   if(not inv)then
  31.     for i=1,k[5] do
  32.       enc = enc .. string.char(math.random(32,126));
  33.     end
  34.   end
  35.   return enc;
  36. end
  37.  
  38. --// exportstring( string )
  39. --// returns a "Lua" portable version of the string
  40. local function exportstring( s )
  41.     s = string.format( "%q",s )
  42.     -- to replace
  43.     s = string.gsub( s,"\\\n","\\n" )
  44.     s = string.gsub( s,"\r","\\r" )
  45.     s = string.gsub( s,string.char(26),"\"..string.char(26)..\"" )
  46.     return s
  47. end
  48. --// The Save Function
  49. function saveTable(  tbl,filename )
  50.     local tableFile = assert(io.open(filename, "w"))
  51.   tableFile:write(ser.serialize(tbl))
  52.   tableFile:close()
  53. end
  54.  
  55. --// The Load Function
  56. function loadTable( sfile )
  57.     local tableFile = assert(io.open(sfile))
  58.     return ser.unserialize(tableFile:read("*all"))
  59. end
  60.  
  61.  
  62. term.clear()
  63. print("Security server")
  64. print("---------------------------------------------------------------------------")
  65.  
  66. local userTable = loadTable("userlist.txt")
  67. local doorTable = loadTable("doorlist.txt")
  68. if userTable == nil then
  69.   userTable = {}
  70. end
  71. if doorTable == nil then
  72.   doorTable = {}
  73. end
  74.  
  75. function getUserName(user)
  76.    for key, value in pairs(userTable) do
  77.     if value.uuid == user then
  78.       return value.name
  79.     end
  80.   end
  81.    return "NilUser"
  82. end
  83.  
  84. function checkUser(user)
  85.   for key, value in pairs(userTable) do
  86.     if value.uuid == user then
  87.       return true, not value.blocked, tonumber(value.level), value.staff
  88.     end
  89.   end
  90.   return false
  91. end
  92.  
  93. function checkMtf(user)
  94.   for key, value in pairs(userTable) do
  95.     if value.uuid == user then
  96.       return true, not value.blocked, value.mtf, value.staff
  97.     end
  98.   end
  99.   return false
  100. end
  101.  
  102. function checkGoi(user)
  103.   for key, value in pairs(userTable) do
  104.     if value.uuid == user then
  105.       return true, not value.blocked, value.goi, value.staff
  106.     end
  107.   end
  108.   return false
  109. end
  110.  
  111. function checkSec(user)
  112.   for key, value in pairs(userTable) do
  113.     if value.uuid == user then
  114.       return true, not value.blocked, value.sec, value.staff
  115.     end
  116.   end
  117.   return false
  118. end
  119.  
  120. function checkUserA(user)
  121.   for key, value in pairs(userTable) do
  122.     if value.uuid == user then
  123.       return true, not value.blocked, tonumber(value.armory), value.staff
  124.     end
  125.   end
  126.   return false
  127. end
  128.  
  129. function checkUserD(user)
  130.   for key, value in pairs(userTable) do
  131.     if value.uuid == user then
  132.       return true, not value.blocked, tonumber(value.department), value.staff
  133.     end
  134.   end
  135.   return false
  136. end
  137.  
  138. function checkLevel(id)
  139.   for key, value in pairs(doorTable) do
  140.     if key == id then
  141.       return tonumber(value)
  142.     end
  143.   end
  144.   return 0
  145. end
  146.  
  147. function checkInt(user)
  148.   for key, value in pairs(userTable) do
  149.     if value.uuid == user then
  150.       return true, not value.blocked, value.int, value.staff
  151.     end
  152.   end
  153.   return false
  154. end
  155.  
  156.   if modem.isOpen(198) == false then
  157.     modem.open(198)
  158.   end
  159.   if modem.isOpen(197) == false then
  160.     modem.open(197)
  161.   end
  162. while true do
  163.   if modem.isOpen(modemPort) == false then
  164.     modem.open(modemPort)
  165.   end
  166.  
  167.   local _, _, from, port, _, command, msg = event.pull("modem_message")
  168.   local data = msg
  169.   if command == "updatedoors" then
  170.    
  171.   else
  172.     data = crypt(msg, cryptKey, true)
  173.   end
  174.   local thisUserName = getUserName(data)
  175.   if command == "updatedoors" then
  176.      os.execute("pastebin get -f QxnRYA3B ctrl.lua")
  177.      local filetemp = ios.open("/mnt/b93/ctrl.lua","r")
  178.      local file = filetemp:read("*a")
  179.      modem.broadcast(198, tostring(file))
  180.      os.execute("pastebin get -f sHfsrt2M ctrlE.lua")
  181.      filetemp = ios.open("/mnt/b93/ctrlE.lua","r")
  182.      file = filetemp:read("*a")
  183.      modem.broadcast(197, tostring(file))
  184.      term.write("Updating door command received\n")
  185.   elseif command == "updateuser" then
  186.     userTable = ser.unserialize(data)
  187.     term.write("Updated userlist received\n")
  188.     saveTable(userTable, "userlist.txt")
  189.   elseif command == "setlevel" then
  190.     term.write("Received level from door: " .. data .. "\n")
  191.     doorTable[from] = data
  192.     saveTable(doorTable, "doorlist.txt")
  193.   elseif command == "checkuser" then
  194.     if lockDoors == true then
  195.         term.write("Doors have been locked. Unable to open door\n")
  196.         data = crypt("locked", cryptKey)
  197.         modem.send(from, port, data)
  198.     else
  199.     term.write("Checking " .. thisUserName .. "'s access level:")
  200.     local cu, isBlocked, level, isStaff = checkUser(data)
  201.     if cu == true then          -- user found
  202.         if isBlocked == false then
  203.             data = crypt("false", cryptKey)
  204.             term.write(" user is blocked\n")
  205.             modem.send(from, port, data)
  206.         else
  207.             local cl = checkLevel(from)
  208.             if cl > level then
  209.                 if isStaff == true then
  210.                     data = crypt("true", cryptKey)
  211.                     term.write(" access granted due to staff\n")
  212.                     modem.send(from, port, data)        
  213.                 else
  214.                     data = crypt("false", cryptKey)
  215.                     term.write(" user level is too low\n")
  216.                     modem.send(from, port, data)            
  217.                 end
  218.             else
  219.                 data = crypt("true", cryptKey)
  220.                 term.write(" access granted\n")
  221.                 modem.send(from, port, data)
  222.             end
  223.         end
  224.     else
  225.       data = crypt("false", cryptKey)
  226.       term.write(" user not found\n")
  227.       modem.send(from, port, data)
  228.     end
  229.     end
  230.     elseif command == "checkMtf" then
  231.         if lockDoors == true then
  232.         term.write("Doors have been locked. Unable to open door\n")
  233.         data = crypt("locked", cryptKey)
  234.         modem.send(from, port, data)
  235.     else
  236.         term.write("Checking if user " .. thisUserName .. " is MTF:")
  237.         local cu, isBlocked, isMtf, isStaff = checkMtf(data)
  238.         if cu == true then
  239.             if isBlocked == false then
  240.             data = crypt("false", cryptKey)
  241.             term.write(" user is blocked\n")
  242.             modem.send(from, port, data)
  243.             else
  244.             if isMtf == true then
  245.             data = crypt("true", cryptKey)
  246.             term.write(" access granted\n")
  247.             modem.send(from, port, data)
  248.             else
  249.                 if isStaff == true then
  250.                     data = crypt("true", cryptKey)
  251.                     term.write(" access granted due to staff\n")
  252.                     modem.send(from, port, data)        
  253.                 else
  254.                     data = crypt("false", cryptKey)
  255.                     term.write(" access denied\n")
  256.                     modem.send(from, port, data)          
  257.                 end
  258.          end
  259.          end
  260.                 else
  261.                 data = crypt("false", cryptKey)
  262.                 term.write(" user not found\n")
  263.                 modem.send(from, port, data)
  264.          end
  265.       end  
  266.         elseif command == "checkgoi" then
  267.         if lockDoors == true then
  268.         term.write("Doors have been locked. Unable to open door\n")
  269.         data = crypt("locked", cryptKey)
  270.         modem.send(from, port, data)
  271.         else
  272.         term.write("Checking if user " .. thisUserName .. " is GOI:")
  273.         local cu, isBlocked, isGoi, isStaff = checkGoi(data)
  274.         if cu == true then
  275.             if isBlocked == false then
  276.             data = crypt("false", cryptKey)
  277.             term.write(" user is blocked\n")
  278.             modem.send(from, port, data)
  279.             else
  280.             if isGoi == true then
  281.             data = crypt("true", cryptKey)
  282.             term.write(" access granted\n")
  283.             modem.send(from, port, data)
  284.             else
  285.             if isStaff == true then
  286.                     data = crypt("true", cryptKey)
  287.                     term.write(" access granted due to staff\n")
  288.                     modem.send(from, port, data)        
  289.                 else
  290.                     data = crypt("false", cryptKey)
  291.                     term.write(" access denied\n")
  292.                     modem.send(from, port, data)          
  293.                 end
  294.          end
  295.          end
  296.                 else
  297.                 data = crypt("false", cryptKey)
  298.                 term.write(" user not found\n")
  299.                 modem.send(from, port, data)
  300.          end
  301.       end  
  302.     elseif command == "checksec" then
  303.         if lockDoors == true then
  304.             term.write("Doors have been locked. Unable to open door\n")
  305.             data = crypt("locked", cryptKey)
  306.             modem.send(from, port, data)
  307.         else
  308.             term.write("Checking if user " .. thisUserName .. " has Security pass:")
  309.             local cu, isBlocked, isSec, isStaff = checkSec(data)
  310.             if cu == true then
  311.                 if isBlocked == false then
  312.                     data = crypt("false", cryptKey)
  313.                     term.write(" user is blocked\n")
  314.                     modem.send(from, port, data)
  315.                 else
  316.                     if isSec == true then
  317.                         data = crypt("true", cryptKey)
  318.                         term.write(" access granted\n")
  319.                         modem.send(from, port, data)
  320.                     else
  321.                         if isStaff == true then
  322.                     data = crypt("true", cryptKey)
  323.                     term.write(" access granted due to staff\n")
  324.                     modem.send(from, port, data)        
  325.                 else
  326.                     data = crypt("false", cryptKey)
  327.                     term.write(" access denied\n")
  328.                     modem.send(from, port, data)          
  329.                 end
  330.                     end
  331.                 end
  332.             else
  333.                 data = crypt("false", cryptKey)
  334.                 term.write(" user not found\n")
  335.                 modem.send(from, port, data)
  336.             end
  337.         end  
  338.     elseif command == "checkarmor" then
  339.         if lockDoors == true then
  340.         term.write("Doors have been locked. Unable to open door\n")
  341.         data = crypt("locked", cryptKey)
  342.         modem.send(from, port, data)
  343.     else
  344.     term.write("Checking " .. thisUserName .. "'s armory level:")
  345.     local cu, isBlocked, level, isStaff = checkUserA(data)
  346.     if cu == true then          -- user found
  347.       if isBlocked == false then
  348.     data = crypt("false", cryptKey)
  349.     term.write(" user is blocked\n")
  350.     modem.send(from, port, data)
  351.       else
  352.     local cl = checkLevel(from)
  353.     if cl > level then
  354.                 if isStaff == true then
  355.                     data = crypt("true", cryptKey)
  356.                     term.write(" access granted due to staff\n")
  357.                     modem.send(from, port, data)        
  358.                 else
  359.                     data = crypt("false", cryptKey)
  360.                     term.write(" users level too low\n")
  361.                     modem.send(from, port, data)        
  362.                 end
  363.     else
  364.       data = crypt("true", cryptKey)
  365.       term.write(" access granted\n")
  366.       modem.send(from, port, data)
  367.     end
  368.       end
  369.     else
  370.       data = crypt("false", cryptKey)
  371.       term.write(" user not found\n")
  372.       modem.send(from, port, data)
  373.     end
  374.     end
  375.     elseif command == "checkdepartment" then
  376.         if lockDoors == true then
  377.         term.write("Doors have been locked. Unable to open door\n")
  378.         data = crypt("locked", cryptKey)
  379.         modem.send(from, port, data)
  380.     else
  381.     term.write("Checking " .. thisUserName .. "'s department:")
  382.     local cu, isBlocked, level, isStaff = checkUserD(data)
  383.     if cu == true then          -- user found
  384.       if isBlocked == false then
  385.     data = crypt("false", cryptKey)
  386.     term.write(" user is blocked\n")
  387.     modem.send(from, port, data)
  388.       else
  389.     local cl = checkLevel(from)
  390.     if cl ~= level and level ~= 5 then
  391.       if isStaff == true then
  392.                     data = crypt("true", cryptKey)
  393.                     term.write(" access granted due to staff\n")
  394.                     modem.send(from, port, data)        
  395.                 else
  396.                     data = crypt("false", cryptKey)
  397.                     term.write(" incorrect department\n")
  398.                     modem.send(from, port, data)    
  399.                 end
  400.     else
  401.       data = crypt("true", cryptKey)
  402.       if cl == 5 then
  403.         term.write(" O5 clearance granted\n")
  404.         modem.send(from, port, data)
  405.       else
  406.         term.write(" access granted\n")
  407.         modem.send(from, port, data)                
  408.       end
  409.     end
  410.       end
  411.     else
  412.       data = crypt("false", cryptKey)
  413.       term.write(" user not found\n")
  414.       modem.send(from, port, data)
  415.     end
  416.    end
  417.        elseif command == "checkint" then
  418.         if lockDoors == true then
  419.         term.write("Doors have been locked. Unable to open door\n")
  420.         data = crypt("locked", cryptKey)
  421.         modem.send(from, port, data)
  422.         else
  423.         term.write("Checking if user " .. thisUserName .. " has Intercom pass:")
  424.         local cu, isBlocked, isInt, isStaff = checkInt(data)
  425.         if cu == true then
  426.             if isBlocked == false then
  427.             data = crypt("false", cryptKey)
  428.             term.write(" user is blocked\n")
  429.             modem.send(from, port, data)
  430.             else
  431.             if isInt == true then
  432.             data = crypt("true", cryptKey)
  433.             term.write(" access granted\n")
  434.             modem.send(from, port, data)
  435.             else
  436.                 if isStaff == true then
  437.                     data = crypt("true", cryptKey)
  438.                     term.write(" access granted due to staff\n")
  439.                     modem.send(from, port, data)        
  440.                 else
  441.                     data = crypt("false", cryptKey)
  442.                     term.write(" access denied\n")
  443.                     modem.send(from, port, data)
  444.                 end
  445.          end
  446.          end
  447.                 else
  448.                 data = crypt("false", cryptKey)
  449.                 term.write(" user not found\n")
  450.                 modem.send(from, port, data)
  451.          end
  452.       end  
  453.    end
  454. end
RAW Paste Data