Advertisement
Guest User

Untitled

a guest
Oct 1st, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.62 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. elseif sTable.todo == "checkpassword" 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, "true")
  188. else
  189. rednet.send(sID, "Bad password")
  190. end
  191. end
  192. elseif sTable.todo == "reset" then
  193. USERS_CREATEDB()
  194. print("Database reseted") --Pls add a Terminal confirm
  195. rednet.send(sID, "true")
  196. end
  197. elseif sTable.type == "log" then
  198. elseif sTable.type == "ping" then
  199. rednet.send(sID, "true")
  200. log(sID, sTable, "ping")
  201. elseif sTable.type == "WAS" then --Wow address system
  202. elseif sTable.type == "database" then
  203. if sTable.todo == "createUser" then
  204. if sTable.Username == nil or sTable.Password == nil or sTable.Biolock == nil or type(sTable.accesslevel) == number then
  205. print("User creation failed")
  206. rednet.send(sID, "nil in table")
  207. else
  208. if USERS_CREATE(sTable.Username,sTable.Password,sTable.Biolock,sTable.accesslevel) then
  209. print("User Created")
  210. rednet.send(sID, "true")
  211. else
  212. print("Failed to create")
  213. rednet.send(sID, "error")
  214. end
  215. end
  216. elseif sTable.todo == "Userinfo" then
  217. if sTable.Username == nil then
  218. rednet.send(sID, false)
  219. --finish error
  220. else
  221. --[[
  222. Table to resend:
  223. id
  224. biolockid
  225. accesslevel
  226. ]]
  227. local TUserTable = USERS_GET()
  228. local Tid = USERS_ID(TUserTable, sTable.Username)
  229. if Tid == nil then print("Nil TID") rednet.send(sID, "nil id") else
  230. local Tbiolock = TUserTable["Biolock"][Tid]
  231. local TAccessL = TUserTable["AccessLevel"][Tid]
  232. local TEMP_TABLE = {id = Tid, biolockid = Tbiolock, accesslevel = TAccessL}
  233. --print(textutils.serialise(TEMP_TABLE))
  234. rednet.send(sID, TEMP_TABLE)
  235. print("Succefuly sended")
  236. end
  237. end
  238. elseif sTable.type == "network" then
  239. else
  240. rednet.send(sID, "bad TableType")
  241. end
  242. end
  243. --Database
  244. term.write("Rednet state: ")
  245. if rednet.isOpen() then print("true") else print("false") end
  246. if rednet.isOpen() ~= true then rednet.open("top") end
  247. rednet.host("HomePI", "main")
  248. print("Rednet opened\nID: ".. os.computerID())
  249.  
  250. --USERS_CREATEDB()
  251. --print(USERS_CREATE("admin", "test", "unknown", 5)
  252.  
  253. local function receiveparse()
  254. while RP_BOOL do
  255. local sID, msg = rednet.receive()
  256. if type(msg) ~= "table" then rednet.send(sID, "Please use API") log(sID, msg, "Dont use a table") else
  257. gitanoParser(msg,sID, "received") -- redirect to gitanoParser
  258. end
  259. end
  260. end
  261.  
  262. parallel.waitForAll(receiveparse)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement