Advertisement
Guest User

Untitled

a guest
Oct 1st, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.28 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.  
  49. local function USERS_ID(users, tofind)
  50. local ID = nil
  51. for i = 1,#users["Users"] do
  52. if tofind == nil then
  53. ID = i
  54. else
  55. if users["Users"][i] == tofind then
  56. ID = i
  57. end
  58. end
  59. end
  60. return ID
  61. end
  62.  
  63. local function USERS_BIOLOCK(users, tofind)
  64. local ID = nil
  65. for i = 1,#users["Users"] do
  66. if tofind == nil then return false
  67. else
  68. if users["Users"][i] == tofind then
  69. ID = i
  70. end
  71. end
  72. end
  73. return ID
  74. end
  75.  
  76. local function USERS_TOKEN(users, tofind)
  77. local ID = nil
  78. for i = 1,#users["Token"] do
  79. if tofind == nil then return false
  80. else
  81. if users["Token"][i] == tofind then
  82. ID = i
  83. end
  84. end
  85. end
  86. return ID
  87. end
  88.  
  89. local function USERS_CREATE(name, pass, biolock, accesslevel)
  90. local oUSERS = USERS_GET()
  91. local fUSERS = oUSERS
  92. local lastID = USERS_ID(oUSERS)
  93.  
  94. --Check if user already exist because or AL will not fuck my script ;(
  95. if USERS_ID(oUSERS, name) ~= nil then return false end
  96. if accesslevel > 5 or accesslevel < 1 then return false end
  97. if lastID == nil then lastID = 1 else lastID = lastID + 1 end
  98. if name == nil or pass == nil or accesslevel == nil then
  99. return false
  100. end
  101. if biolock == nil then biolock = "unknown" end
  102. --end of check
  103.  
  104. fUSERS["Users"][lastID] = name
  105. fUSERS["Password"][lastID] = StringUtils.SHA1(pass)
  106. fUSERS["Biolock"][lastID] = biolock
  107. fUSERS["Token"][lastID] = "unknown"
  108. fUSERS["AccessLevel"][lastID] = accesslevel
  109. local TO_SAVE = fs.open(USER_FILE,"w")
  110. TO_SAVE.write(textutils.serialise(fUSERS))
  111. TO_SAVE.close()
  112. return true
  113. end
  114.  
  115. local function USERS_UPDATE(toUpdate, userID, newUpdate)
  116. if type(toUpdate) ~= "string" or type(userID) ~= "number" or newUpdate == nil then return false end
  117. local oUSERS = USERS_GET()
  118. --Check if id exist
  119. local lastID = USERS_ID(oUSERS)
  120. if userID > lastID then return false end
  121.  
  122. if toUpdate == "token" then
  123. oUSERS["Token"][userID] = newUpdate
  124. local TO_SAVE = fs.open(USER_FILE,"w")
  125. TO_SAVE.write(textutils.serialise(oUSERS))
  126. TO_SAVE.close()
  127. return true
  128. elseif toUpdate == "accesslevel" then
  129. if type(newUpdate) ~= "number" then return false end
  130. oUSERS["AccessLevel"][userID] = newUpdate
  131. local TO_SAVE = fs.open(USER_FILE,"w")
  132. TO_SAVE.write(textutils.serialise(oUSERS))
  133. TO_SAVE.close()
  134. return true
  135. end
  136.  
  137. end
  138.  
  139. --Token functions
  140.  
  141. local function TOKEN_GEN()
  142. local TOKEN_LENGHT = 25
  143. local TOKEN = ""
  144. local TOKEN_TABLE = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","0","1","2","3","4","5","6","7","8","9","#", "$", "*"}
  145. while TOKEN_LENGHT == 0 do
  146. local mRandom = math.random(1,#TOKEN_TABLE)
  147. TOKEN = TOKEN..TOKEN_TABLE[mRandom]
  148. TOKEN_LENGHT = TOKEN_LENGHT - 1
  149. end
  150. return TOKEN
  151. end
  152.  
  153. local function TOKEN_CREATE(id)
  154. local USERS = USERS_GET()
  155. if type(id) ~= "number" then return false end
  156. local newToken = TOKEN_GEN()
  157. --Check if token already exist
  158. local YES = true
  159. while YES do
  160. if USERS_TOKEN(USERS, newToken) == nil then YES = false else
  161. newToken = TOKEN_GEN() end
  162. end
  163. --Write the new token
  164. if USERS_UPDATE("token", id, newToken) then
  165. return newToken
  166. else
  167. return false
  168. end
  169. end
  170.  
  171. --We need the famous Gitano Parser !
  172. local function gitanoParser(sTable,sID)
  173. print(sTable.type)
  174. if sTable.type == "mdr" then
  175. log(sID, sTable, "bad type")
  176. return false
  177. elseif sTable.type == "token" then
  178. if sTable.todo == "createToken" then
  179. if type(sTable.enc) ~= "string" or type(sTable.Username) ~= "string" or type(sTable.math) ~= "string" then rednet.send(sID, "nil entry") else
  180. local oTable = USERS_GET()
  181. local uID = USERS_ID(oTable,sTable.Username)
  182. if uID == nil then rednet.send(sID, "Unknown user") return false end
  183. local SHA1Pass = oTable["Password"][uID]
  184. local crypted = StringUtils.decrypt(sTable.enc, SHA1Pass)
  185. if crypted == sTable.math then
  186. --He got it we need to give a token to him
  187. rednet.send(sID, TOKEN_CREATE(uID))
  188. else
  189. rednet.send(sID, "Bad password")
  190. end
  191.  
  192. if sTable.todo == "getToken" then
  193. if type(sTable.enc) ~= "string" or type(sTable.Username) ~= "string" or type(sTable.math) ~= "string" then rednet.send(sID, "nil entry") else
  194. local oTable = USERS_GET()
  195. local uID = USERS_ID(oTable,sTable.Username)
  196. if uID == nil then rednet.send(sID, "Unknown user") return false end
  197. local SHA1Pass = oTable["Password"][uID]
  198. local crypted = StringUtils.decrypt(sTable.enc, SHA1Pass)
  199. if crypted == sTable.math then
  200. --He got it we need to give a token to him
  201. rednet.send(sID, oTable["Token"][uID])
  202. else
  203. rednet.send(sID, "Bad password")
  204. end
  205. end
  206. elseif sTable.type == "log" then
  207. elseif sTable.type == "ping" then
  208. rednet.send(sID, "true")
  209. log(sID, sTable, "ping")
  210. elseif sTable.type == "WAS" then --Wow address system
  211. elseif sTable.type == "database" then
  212. if sTable.todo == "createUser" then
  213. if sTable.Username == nil or sTable.Password == nil or sTable.Biolock == nil or type(sTable.accesslevel) == number then
  214. print("User creation failed")
  215. rednet.send(sID, "nil in table")
  216. else
  217. if USERS_CREATE(sTable.Username,sTable.Password,sTable.Biolock,sTable.accesslevel) then
  218. print("User Created")
  219. rednet.send(sID, "true")
  220. else
  221. print("Failed to create")
  222. rednet.send(sID, "error")
  223. end
  224. end
  225. elseif sTable.todo == "reset" then
  226. USERS_CREATEDB()
  227. print("Database reseted") --Pls add a Terminal confirm
  228. rednet.send(sID, "true")
  229. elseif sTable.todo == "Userinfo" then
  230. if sTable.Username == nil then
  231. rednet.send(sID, false)
  232. --finish error
  233. else
  234. --[[
  235. Table to resend:
  236. id
  237. biolockid
  238. accesslevel
  239. ]]
  240. local TUserTable = USERS_GET()
  241. local Tid = USERS_ID(TUserTable, sTable.Username)
  242. if Tid == nil then print("Nil TID") rednet.send(sID, "nil id") else
  243. local Tbiolock = TUserTable["Biolock"][Tid]
  244. local TAccessL = TUserTable["AccessLevel"][Tid]
  245. local TEMP_TABLE = {id = Tid, biolockid = Tbiolock, accesslevel = TAccessL}
  246. --print(textutils.serialise(TEMP_TABLE))
  247. rednet.send(sID, TEMP_TABLE)
  248. print("Succefuly sended")
  249. end
  250. end
  251. end
  252. elseif sTable.type == "network" then
  253. else
  254. rednet.send(sID, "bad TableType")
  255. end
  256. end
  257. --Database
  258. term.write("Rednet state: ")
  259. if rednet.isOpen() then print("true") else print("false") end
  260. if rednet.isOpen() ~= true then rednet.open("top") end
  261. rednet.host("HomePI", "main")
  262. print("Rednet opened\nID: ".. os.computerID())
  263.  
  264. --USERS_CREATEDB()
  265. --print(USERS_CREATE("admin", "test", "unknown", 5)
  266.  
  267. local function receiveparse()
  268. while RP_BOOL do
  269. local sID, msg = rednet.receive()
  270. if type(msg) ~= "table" then rednet.send(sID, "Please use API") log(sID, msg, "Dont use a table") else
  271. gitanoParser(msg,sID, "received") -- redirect to gitanoParser
  272. end
  273. end
  274. end
  275.  
  276. parallel.waitForAll(receiveparse)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement