Advertisement
Guest User

startup

a guest
Oct 1st, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.54 KB | None | 0 0
  1. --Var init
  2. local RP_BOOL = true
  3. os.loadAPI("StringUtils")
  4.  
  5. --Log
  6. local function log(ID, oMSG, reason)
  7.   local FILE = fs.open("/log", "a")
  8.  
  9.   if type(oMSG) == "string" then
  10.     toMSG = "bad type(string)"
  11.   elseif type(oMSG) == "table" then
  12.     if oMSG["type"] ~= nil then
  13.       toMSG = "bad table"
  14.     else
  15.       toMSG = oMSG["type"]
  16.     end
  17.   else
  18.     toMSG = "unknown"
  19.   end
  20.  
  21.   local TEMP = "[LOG] ID:"..ID.." toMSG: "..toMSG.." Reason: "..reason
  22.   print(TEMP)
  23.   FILE.writeLine(TEMP)
  24.   FILE.close()
  25.  
  26.   return true
  27. end
  28.  
  29. --Users management
  30. local USER_FILE = "/users"
  31. local DB_SCHEM = {Users = {}, Password = {}, Biolock = {}, AccessLevel = {}, Token= {}}
  32. local function USERS_CREATEDB()
  33.   local USER_TEMP = fs.open(USER_FILE, "w")
  34.   USER_TEMP.write(textutils.serialise(DB_SCHEM))
  35.   USER_TEMP.close()
  36. end
  37.  
  38. local function USERS_GET()
  39.   local OBJECT_UF = fs.open(USER_FILE, "r")
  40.   local USERS = textutils.unserialise(OBJECT_UF.readAll())
  41.   if USERS == nil then
  42.     USERS_CREATEDB()
  43.     USERS = textutils.unserialise(OBJECT_UF.readAll())
  44.   end
  45.   OBJECT_UF.close()
  46.   return USERS
  47. end
  48. local function USERS_ID(users, tofind)
  49.   local ID = nil
  50.   for i = 1,#users["Users"] do
  51.     if tofind == nil then
  52.       ID = i
  53.     else
  54.       if users["Users"][i] == tofind then
  55.         ID = i
  56.       end
  57.     end
  58.   end
  59.   return ID
  60. end
  61. local function USERS_CREATE(name, pass, biolock, accesslevel)
  62.   local oUSERS = USERS_GET()
  63.   local fUSERS = oUSERS
  64.   local lastID = USERS_ID(oUSERS)
  65.  
  66.   --Check if user already exist because or AL will not fuck my script ;(
  67.   if USERS_ID(oUSERS, name) ~= nil then return false end
  68.   if accesslevel > 5 or accesslevel < 1 then return false end
  69.   if lastID == nil then lastID = 1 else lastID = lastID + 1 end
  70.   if name == nil or pass == nil or accesslevel == nil then
  71.     return false
  72.   end
  73.   if biolock == nil then biolock = "unknown" end
  74.   --end of check
  75.  
  76.   fUSERS["Users"][lastID] = name
  77.   fUSERS["Password"][lastID] = StringUtils.SHA1(pass)
  78.   fUSERS["Biolock"][lastID] = biolock
  79.   fUSERS["Token"][lastID] = "unknown" --add token generator
  80.   fUSERS["AccessLevel"][lastID] = accesslevel
  81.   local TO_SAVE = fs.open(USER_FILE,"w")
  82.   TO_SAVE.write(textutils.serialise(fUSERS))
  83.   TO_SAVE.close()
  84.   return true
  85. end
  86. --We need the famous Gitano Parser !
  87. local function gitanoParser(sTable,sID)
  88.   print(sTable.type)
  89.   if sTable.type == "mdr" then
  90.     log(sID, sTable, "bad type")
  91.     return false
  92.   elseif sTable.type == "token" then
  93.   elseif sTable.type == "log" then
  94.   elseif sTable.type == "ping" then
  95.     rednet.send(sID, "true")
  96.     log(sID, sTable, "ping")
  97.   elseif sTable.type == "WAS" then --Wow address system
  98.   elseif sTable.type == "database" then
  99.     if sTable.todo == "createUser" then
  100.       if sTable.Username == nil or sTable.Password == nil or sTable.Biolock == nil or type(sTable.accesslevel) == number then
  101.         print("User creation failed")
  102.         rednet.send(sID, "nil in table")
  103.       else
  104.         if USERS_CREATE(sTable.Username,sTable.Password,sTable.Biolock,sTable.accesslevel) then
  105.           print("User Created")
  106.           rednet.send(sID, "true")
  107.         else
  108.           print("Failed to create")
  109.           rednet.send(sID, "error")
  110.         end
  111.       end
  112.     elseif sTable.todo == "Userinfo" then
  113.       if sTable.Username == nil then
  114.       rednet.send(sID, false)
  115.       --finish error
  116.       else
  117.         --[[
  118.         Table to resend:
  119.         id
  120.         biolockid
  121.         accesslevel
  122.         ]]
  123.         local TUserTable = USERS_GET()
  124.         local Tid = USERS_ID(TUserTable, sTable.Username)
  125.         if Tid == nil then print("Nil TID") rednet.send(sID, "nil id") else
  126.         local Tbiolock = TUserTable["Biolock"][Tid]
  127.         local TAccessL = TUserTable["AccessLevel"][Tid]
  128.         local TEMP_TABLE = {id = Tid, biolockid = Tbiolock, accesslevel = TAccessL}
  129.         --print(textutils.serialise(TEMP_TABLE))
  130.         rednet.send(sID, TEMP_TABLE)
  131.         print("Succefuly sended")
  132.         end
  133.       end
  134.     elseif sTable.todo == "checkpassword" then
  135.       if type(sTable.enc) ~= "string" or type(sTable.Username) ~= "string" or type(sTable.math) ~= "string" then rednet.send(sID, "nil entry") else
  136.       local oTable = USERS_GET()
  137.       local uID = USERS_ID(oTable,sTable.Username)  
  138.       if uID == nil then rednet.send(sID, "Unknown user") return false end
  139.       local SHA1Pass = oTable["Password"][uID]
  140.       local crypted = StringUtils.decrypt(sTable.enc, SHA1Pass)
  141.       if crypted == sTable.math then
  142.         --He got it we need to give a token to him
  143.         rednet.send(sID, "true")
  144.       else
  145.         rednet.send(sID, "Bad password")  
  146.       end
  147.       end  
  148.     elseif sTable.todo == "reset" then
  149.       USERS_CREATEDB()
  150.       print("Database reseted") --Pls add a Terminal confirm
  151.       rednet.send(sID, "true")
  152.     end
  153.   elseif sTable.type == "network" then
  154.   else
  155.     rednet.send(sID, "bad TableType")
  156.   end
  157. end
  158. --Database
  159. term.write("Rednet state: ")
  160. if rednet.isOpen() then print("true") else print("false") end
  161. if rednet.isOpen() ~= true then rednet.open("top") end
  162. rednet.host("HomePI", "main")
  163. print("Rednet opened\nID: ".. os.computerID())
  164.  
  165. --USERS_CREATEDB()
  166. --print(USERS_CREATE("admin", "test", "unknown", 5)
  167.  
  168. local function receiveparse()
  169.   while RP_BOOL do
  170.     local sID, msg = rednet.receive()
  171.     if type(msg) ~= "table" then rednet.send(sID, "Please use API") log(sID, msg, "Dont use a table") else
  172.       gitanoParser(msg,sID, "received")       -- redirect to gitanoParser
  173.     end
  174.   end
  175. end
  176.  
  177. parallel.waitForAll(receiveparse)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement