D_Puppy

security server for oc and opensecurity

Jul 22nd, 2016
4,831
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.  
  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.  
  12. local function convert( chars, dist, inv )
  13.   return string.char( ( string.byte( chars ) - 32 + ( inv and -dist or dist ) ) % 95 + 32 )
  14. end
  15.  
  16.  
  17. local function crypt(str,k,inv)
  18.   local enc= "";
  19.   for i=1,#str do
  20.     if(#str-k[5] >= i or not inv)then
  21.       for inc=0,3 do
  22.     if(i%4 == inc)then
  23.       enc = enc .. convert(string.sub(str,i,i),k[inc+1],inv);
  24.       break;
  25.     end
  26.       end
  27.     end
  28.   end
  29.   if(not inv)then
  30.     for i=1,k[5] do
  31.       enc = enc .. string.char(math.random(32,126));
  32.     end
  33.   end
  34.   return enc;
  35. end
  36.  
  37. --// exportstring( string )
  38. --// returns a "Lua" portable version of the string
  39. local function exportstring( s )
  40.     s = string.format( "%q",s )
  41.     -- to replace
  42.     s = string.gsub( s,"\\\n","\\n" )
  43.     s = string.gsub( s,"\r","\\r" )
  44.     s = string.gsub( s,string.char(26),"\"..string.char(26)..\"" )
  45.     return s
  46. end
  47. --// The Save Function
  48. function saveTable(  tbl,filename )
  49.     local charS,charE = "   ","\n"
  50.     local file,err
  51.     -- create a pseudo file that writes to a string and return the string
  52.     if not filename then
  53.         file =  { write = function( self,newstr ) self.str = self.str..newstr end, str = "" }
  54.         charS,charE = "",""
  55.     -- write table to tmpfile
  56.     elseif filename == true or filename == 1 then
  57.         charS,charE,file = "","",io.tmpfile()
  58.     -- write table to file
  59.     -- use io.open here rather than io.output, since in windows when clicking on a file opened with io.output will create an error
  60.     else
  61.         file,err = io.open( filename, "w" )
  62.         if err then return _,err end
  63.     end
  64.     -- initiate variables for save procedure
  65.     local tables,lookup = { tbl },{ [tbl] = 1 }
  66.     file:write( "return {"..charE )
  67.     for idx,t in ipairs( tables ) do
  68.         if filename and filename ~= true and filename ~= 1 then
  69.             file:write( "-- Table: {"..idx.."}"..charE )
  70.         end
  71.         file:write( "{"..charE )
  72.         local thandled = {}
  73.         for i,v in ipairs( t ) do
  74.             thandled[i] = true
  75.             -- escape functions and userdata
  76.             if type( v ) ~= "userdata" then
  77.                 -- only handle value
  78.                 if type( v ) == "table" then
  79.                     if not lookup[v] then
  80.                         table.insert( tables, v )
  81.                         lookup[v] = #tables
  82.                     end
  83.                     file:write( charS.."{"..lookup[v].."},"..charE )
  84.                 elseif type( v ) == "function" then
  85.                     file:write( charS.."loadstring("..exportstring(string.dump( v )).."),"..charE )
  86.                 else
  87.                     local value =  ( type( v ) == "string" and exportstring( v ) ) or tostring( v )
  88.                     file:write(  charS..value..","..charE )
  89.                 end
  90.             end
  91.         end
  92.         for i,v in pairs( t ) do
  93.             -- escape functions and userdata
  94.             if (not thandled[i]) and type( v ) ~= "userdata" then
  95.                 -- handle index
  96.                 if type( i ) == "table" then
  97.                     if not lookup[i] then
  98.                         table.insert( tables,i )
  99.                         lookup[i] = #tables
  100.                     end
  101.                     file:write( charS.."[{"..lookup[i].."}]=" )
  102.                 else
  103.                     local index = ( type( i ) == "string" and "["..exportstring( i ).."]" ) or string.format( "[%d]",i )
  104.                     file:write( charS..index.."=" )
  105.                 end
  106.                 -- handle value
  107.                 if type( v ) == "table" then
  108.                     if not lookup[v] then
  109.                         table.insert( tables,v )
  110.                         lookup[v] = #tables
  111.                     end
  112.                     file:write( "{"..lookup[v].."},"..charE )
  113.                 elseif type( v ) == "function" then
  114.                     file:write( "loadstring("..exportstring(string.dump( v )).."),"..charE )
  115.                 else
  116.                     local value =  ( type( v ) == "string" and exportstring( v ) ) or tostring( v )
  117.                     file:write( value..","..charE )
  118.                 end
  119.             end
  120.         end
  121.         file:write( "},"..charE )
  122.     end
  123.     file:write( "}" )
  124.     -- Return Values
  125.     -- return stringtable from string
  126.     if not filename then
  127.         -- set marker for stringtable
  128.         return file.str.."--|"
  129.     -- return stringttable from file
  130.     elseif filename == true or filename == 1 then
  131.         file:seek ( "set" )
  132.         -- no need to close file, it gets closed and removed automatically
  133.         -- set marker for stringtable
  134.         return file:read( "*a" ).."--|"
  135.     -- close file and return 1
  136.     else
  137.         file:close()
  138.         return 1
  139.     end
  140. end
  141.  
  142. --// The Load Function
  143. function loadTable( sfile )
  144.     local tables, err, _
  145.     -- catch marker for stringtable
  146.     if string.sub( sfile,-3,-1 ) == "--|" then
  147.         tables,err = loadstring( sfile )
  148.     else
  149.         tables,err = loadfile( sfile )
  150.     end
  151.     if err then return _,err
  152.     end
  153.     tables = tables()
  154.     for idx = 1,#tables do
  155.         local tolinkv,tolinki = {},{}
  156.         for i,v in pairs( tables[idx] ) do
  157.             if type( v ) == "table" and tables[v[1]] then
  158.                 table.insert( tolinkv,{ i,tables[v[1]] } )
  159.             end
  160.             if type( i ) == "table" and tables[i[1]] then
  161.                 table.insert( tolinki,{ i,tables[i[1]] } )
  162.             end
  163.         end
  164.         -- link values, first due to possible changes of indices
  165.         for _,v in ipairs( tolinkv ) do
  166.             tables[idx][v[1]] = v[2]
  167.         end
  168.         -- link indices
  169.         for _,v in ipairs( tolinki ) do
  170.             tables[idx][v[2]],tables[idx][v[1]] =  tables[idx][v[1]],nil
  171.         end
  172.     end
  173.     return tables[1]
  174. end
  175.  
  176.  
  177. term.clear()
  178. print("Security server")
  179. print("---------------------------------------------------------------------------")
  180.  
  181. local userTable = loadTable("userlist.txt")
  182. local doorTable = loadTable("doorlist.txt")
  183. if userTable == nil then
  184.   userTable = {}
  185. end
  186. if doorTable == nil then
  187.   doorTable = {}
  188. end
  189.  
  190. function checkUser(user)
  191.   for key, value in pairs(userTable) do
  192.     if value.name == user then
  193.       return true, not value.blocked, tonumber(value.level)
  194.     end
  195.   end
  196.   return false
  197. end
  198.  
  199. function checkLevel(id)
  200.   for key, value in pairs(doorTable) do
  201.     if key == id then
  202.       return tonumber(value)
  203.     end
  204.   end
  205.   return 0
  206. end
  207.  
  208. while true do
  209.   if modem.isOpen(modemPort) == false then
  210.     modem.open(modemPort)
  211.   end
  212.  
  213.   local _, _, from, port, _, command, msg = event.pull("modem_message")
  214.   local data = crypt(msg, cryptKey, true)
  215.   term.write(from .. ":" .. port .. ":" .. command)
  216.   if command == "updateuser" then
  217.     userTable = ser.unserialize(data)
  218.     term.write("\n")
  219.     saveTable(userTable, "userlist.txt")
  220.   elseif command == "setlevel" then
  221.     term.write(" " .. data .. "\n")
  222.     doorTable[from] = data
  223.     saveTable(doorTable, "doorlist.txt")
  224.   elseif command == "checkuser" then
  225.     term.write(":" .. data .. ":")
  226.     local cu, isBlocked, level = checkUser(data)
  227.     if cu == true then          -- user found
  228.       if isBlocked == false then
  229.     data = crypt("false", cryptKey)
  230.     term.write("blocked\n")
  231.     modem.send(from, port, data)
  232.       else
  233.     local cl = checkLevel(from)
  234.     if cl > level then
  235.       data = crypt("false", cryptKey)
  236.       term.write("level too low\n")
  237.       modem.send(from, port, data)
  238.     else
  239.       data = crypt("true", cryptKey)
  240.       term.write("ok\n")
  241.       modem.send(from, port, data)
  242.     end
  243.       end
  244.     else
  245.       data = crypt("false", cryptKey)
  246.       term.write("not found\n")
  247.       modem.send(from, port, data)
  248.     end
  249.   end
  250. end
RAW Paste Data