Guest User

Untitled

a guest
Aug 29th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.49 KB | None | 0 0
  1. local passFile = 'etc/pwd'
  2. local logFile = 'etc/log'
  3. local roomFile = '/etc/rooms'
  4. local sha1API = 'sha1'
  5.  
  6. local doorSide = 'front'
  7.  
  8. local passTable = {}
  9. local roomTable = {}
  10.  
  11. local codes = {succes=1, wrong_user=2, wrong_pass=3, unprivileged=4}
  12. local codeNames = {"logged in succesfully", "wrong username", "wrong password", "unprivileged user"}
  13. local privs = { admin=1, mod=2, habitant=3, tempHabitant=4, guest=5}
  14. local privNames = { "Administrator", "Moderator", "Habitant", "Temporary Habitant", "Guest" }
  15.  
  16. local function readLoginsToTable()
  17. local h = fs.open(passFile, 'r')
  18. while true do
  19. local l = h.readLine()
  20. if not l then
  21. break
  22. end
  23. local iter = string.gmatch(l, "(%w+):(%w+):(%w+):(%w+)")
  24. local id, user, hash, priv = iter()
  25. if user and hash then
  26. passTable[#passTable+1] = {}
  27. passTable[#passTable].id = id
  28. passTable[#passTable].user = user
  29. passTable[#passTable].hash = hash
  30. passTable[#passTable].privilege = priv
  31. end
  32. end
  33. h.close()
  34. end
  35.  
  36. local function saveLoginsToFile()
  37. h = fs.open(passFile, 'w')
  38. for i,v in ipairs(passTable) do
  39. h.writeLine(v.id .. ':' .. v.user .. ':' .. v.hash .. ':' .. v.privilege)
  40. end
  41. h.close()
  42. end
  43.  
  44. local function readRoomsToTable()
  45. local h = fs.open(roomFile, 'r')
  46. while true do
  47. local l = h.readLine()
  48. if not l then
  49. break
  50. end
  51. local iter = string.gmatch(l, "(%w+):(%w+):(%w+)")
  52. local id, rnID, userID = iter()
  53. if id and rnID and userID then
  54. roomTable[#roomTable+1] = {}
  55. roomTable[#roomTable].id = id
  56. roomTable[#roomTable].rnID = rnID
  57. roomTable[#roomTable].userID = userID
  58. end
  59. end
  60. h.close()
  61. end
  62.  
  63. local function saveRoomsToFile()
  64. local h = fs.open(roomFile, 'w')
  65. for i,v in ipairs(roomTable) do
  66. h.writeLine(v.id .. ':' .. v.rnID .. ':' .. v.userID)
  67. end
  68. h.close()
  69. end
  70.  
  71. local function writeLog(user, code)
  72. if not (user and code) then
  73. return
  74. end
  75. h = fs.open(logFile, 'a')
  76. h.writeLine("User '" .. user .. "' tried logging in with status: " .. codeNames[code] .. ".")
  77. h.close()
  78. end
  79.  
  80. local function addUser(user, pass, privilege)
  81. if user and pass then
  82. local hash = sha1.sha1(pass)
  83. passTable[#passTable+1] = {}
  84. local id
  85. if (#passTable == 1) then
  86. id = 1
  87. else
  88. id = passTable[#passTable-1].id + 1
  89. end
  90. passTable[#passTable].id = id
  91. passTable[#passTable].user = user
  92. passTable[#passTable].hash = hash
  93. -- change this:
  94. passTable[#passTable].privilege = privilege
  95. saveLoginsToFile()
  96. return true
  97. else
  98. return false
  99. end
  100. end
  101.  
  102. local function delUser(username)
  103. if not username then
  104. return false
  105. end
  106. for i,v in ipairs(passTable) do
  107. if (v.user == username) then
  108. table.remove(passTable, i)
  109. end
  110. end
  111. saveLoginsToFile()
  112. return true
  113. end
  114.  
  115. local function userExists(username)
  116. if not username then
  117. return nil
  118. end
  119. for i,v in ipairs(passTable) do
  120. if (v.user == username) then
  121. return true
  122. end
  123. end
  124. return false
  125. end
  126.  
  127. local function getUserByID(userID)
  128. if not userID then
  129. return nil
  130. end
  131. for i,v in ipairs(passTable) do
  132. if (v.id == tostring(userID)) then
  133. return v
  134. end
  135. end
  136. return nil
  137. end
  138.  
  139. local function closeDoor()
  140. rs.setOutput(doorSide, true)
  141. end
  142.  
  143. local function openDoor()
  144. rs.setOutput(doorSide, false)
  145. sleep(5)
  146. rs.setOutput(doorSide, true)
  147. end
  148.  
  149. local function openTrap()
  150. rs.setOutput('left', true)
  151. end
  152.  
  153. local function closeTrap()
  154. rs.setOutput('left', false)
  155. end
  156.  
  157. local function isRoomDoor(senderID)
  158. for i,v in ipairs(roomTable) do
  159. if (tonumber(v.rnID) == tonumber(senderID)) then
  160. return true
  161. end
  162. end
  163. return false
  164. end
  165.  
  166. local function getRoomUser(senderID)
  167. for i,v in ipairs(roomTable) do
  168. if (tonumber(v.rnID) == tonumber(senderID)) then
  169. return tonumber(v.userID)
  170. end
  171. end
  172. return 0
  173. end
  174.  
  175. local function evaluatePass(user,pass,senderID)
  176. local hash = sha1.sha1(pass)
  177. for i,v in ipairs(passTable) do
  178. if v.user == user then
  179. if v.hash == hash then
  180. if( isRoomDoor(senderID) == true) then
  181. if( tonumber(v.privilege) <= 2 ) or (getRoomUser(senderID) == tonumber(v.id) ) then
  182. return codes.succes
  183. else
  184. return codes.unprivileged
  185. end
  186. else
  187. return codes.succes
  188. end
  189. else
  190. return codes.wrong_pass
  191. end
  192. end
  193. end
  194. return codes.wrong_user
  195. end
  196.  
  197. local function startModem()
  198. local sides = {"left", "right", "front", "back", "top", "bottom"}
  199. for i=1,table.getn(sides) do
  200. rednet.open(sides[i])
  201. end
  202. end
  203.  
  204. local function main()
  205. local failedAttempts = 0
  206. local time = os.time()
  207. os.loadAPI(sha1API)
  208. startModem()
  209. readLoginsToTable()
  210. readRoomsToTable()
  211. closeDoor()
  212. while true do
  213. repeat
  214. local e,id,msg = os.pullEvent('rednet_message')
  215. if not (msg) or (#msg < 3) then
  216. break
  217. end
  218. local iter = string.gmatch(msg, "(%w+):(%w+)")
  219. local user, pass = iter()
  220. if not (user and pass) then
  221. break
  222. end
  223. local code = evaluatePass(user, pass, id)
  224. rednet.send(id, tostring(code))
  225. writeLog(user, code)
  226. if (code == codes.succes) and (isRoomDoor(id) == false) then
  227. openDoor()
  228. else
  229. if ( isRoomDoor(id) == false ) then
  230. failedAttempts = failedAttempts + 1
  231. if (failedAttempts >= 3 and (os.time() - time) < 60) then
  232. openTrap()
  233. sleep(3)
  234. closeTrap()
  235. failedAttempts = 0
  236. time = os.time()
  237. end
  238. end
  239. end
  240. until true
  241. end
  242. os.unloadAPI(sha1API)
  243. end
  244.  
  245. local function buttonHandler()
  246. while true do
  247. os.pullEvent('redstone')
  248. if rs.getInput('right') == true then
  249. openDoor()
  250. end
  251. end
  252. end
  253.  
  254. function displayMenu(first, second, lst) -- Put the menu text in. User chooses one. The function returns the selection he choose.
  255. --You now need to type in the number the menu starts, and the number it ends. Made by libraryaddict
  256. local MenuStuff = lst
  257. local Scrolled = 1
  258. local DownDown = 0
  259. local Mouse = first
  260. local function rewrite()
  261. local i = 0
  262. for n=first, second do
  263. i = i+1
  264. if MenuStuff[i+DownDown] then
  265. term.setCursorPos(4, n)
  266. term.write(string.rep(" ", string.len(MenuStuff[i+DownDown])))
  267. end
  268. end
  269. end
  270. local function Draw()
  271. local i = 0
  272. for n=first,second do
  273. i = i+1
  274. term.setCursorPos(4, n)
  275. term.write(MenuStuff[i+DownDown])
  276. end
  277. term.setCursorPos(1, Mouse)
  278. term.write(">>")
  279. end
  280. Draw()
  281. while true do
  282. event,param1 = os.pullEvent()
  283. if event == "key" then
  284. term.setCursorPos(1, Mouse)
  285. term.write(" ")
  286. rewrite()
  287. if param1 == 200 then -- Up
  288. if Scrolled > 1 then
  289. if Mouse == first and MenuStuff[Scrolled-1] then
  290. DownDown = DownDown-1
  291. else
  292. Mouse = Mouse-1
  293. end
  294. Scrolled = Scrolled-1
  295. end
  296. elseif param1 == 208 then -- Down
  297. if Scrolled < #MenuStuff then
  298. if Mouse == second then
  299. DownDown = DownDown+1
  300. else
  301. Mouse = Mouse+1
  302. end
  303. Scrolled = Scrolled+1
  304. end
  305. elseif param1 == 28 then -- Enter
  306. rewrite()
  307. -- return MenuStuff[Scrolled]
  308. return Scrolled
  309. -- break
  310. end
  311. Draw()
  312. end
  313. end
  314. end
  315.  
  316. local function menu()
  317. while true do
  318. term.setCursorPos(1,1)
  319. term.clear()
  320. local choice = displayMenu(1,5, {'open trap', 'close trap', 'add user', 'delete user', 'room management'})
  321. if (choice == 3) then
  322. term.clear()
  323. term.setCursorPos(1,1)
  324. print('Enter the username for the new user:')
  325. local username = read()
  326. print('Enter password:')
  327. local password = read()
  328. local privilege = 5
  329. print ("Choose user's privilege:")
  330. local choosenPriv = displayMenu(6,18, privNames)
  331. if choosenPriv then
  332. privilege = choosenPriv
  333. end
  334. local exists = userExists(username)
  335. if (exists) and (exists == true) then
  336. print('User exists allready.')
  337. else
  338. if addUser(username, password, privilege) then
  339. print("User '" .. username .. "' was added!")
  340. else
  341. print('Username or password wasn\'t legit...')
  342. end
  343. end
  344. elseif (choice == 4) then
  345. term.clear()
  346. term.setCursorPos(1,1)
  347. print('Pick the user to delete:')
  348. local userNames = {}
  349. local userTexts = {}
  350. table.insert(userNames, 'CANCEL')
  351. table.insert(userTexts, 'CANCEL')
  352. for i,v in ipairs(passTable) do
  353. table.insert(userNames, v.user)
  354. table.insert(userTexts, v.id .. ': ' .. v.user .. ' (' .. privNames[tonumber(v.privilege)] .. ')')
  355. end
  356. local selected_user = displayMenu(2,18, userTexts)
  357. if (selected_user) and not (selected_user == 1) then
  358. term.clear()
  359. term.setCursorPos(1,1)
  360. print("Are you sure you want to delete user '" .. userNames[selected_user] .. "'?")
  361. local yesNo = displayMenu(2,4, {'yes', 'no'})
  362. if( yesNo == 1) then
  363. if delUser(userNames[selected_user]) then
  364. print("User '" .. userNames[selected_user] .. "' has succesfully been deleted.")
  365. else
  366. print("Couldn't delete user '" .. userNames[selected_user] .. "'.")
  367. end
  368. else
  369. print("User '" .. userNames[selected_user] .. "' was not deleted.")
  370. end
  371. end
  372. elseif (choice == 5) then
  373. local rooms = {}
  374. local roomTexts = {}
  375. table.insert(roomTexts, 'BACK')
  376. for i,v in ipairs(roomTable) do
  377. table.insert(rooms, v)
  378. if(v.userID == '0') then
  379. table.insert(roomTexts, 'room ' .. v.id .. ': free')
  380. else
  381. local user = getUserByID(v.userID)
  382. if user then
  383. table.insert(roomTexts, 'room ' .. v.id .. ": occupied by '" .. user.user .. "'")
  384. else
  385. table.insert(roomTexts, 'room ' .. v.id .. ": occupied by a nonexistent user")
  386. end
  387. end
  388. end
  389. local selected_room = displayMenu(1,18, roomTexts)
  390. if not (selected_room == 1) then
  391. local room = rooms[selected_room-1]
  392. term.clear()
  393. term.setCursorPos(1,1)
  394. local freeCheck = displayMenu(2,4, {'free room', 'check user in for this room'})
  395. if freeCheck == 1 then
  396. for i,v in ipairs(roomTable) do
  397. if (v.id == room.id) then
  398. v.userID = '0'
  399. term.clear()
  400. term.setCursorPos(1,1)
  401. print('Room ' .. room.id .. ' has been freed.')
  402. saveRoomsToFile()
  403. break
  404. end
  405. end
  406. else
  407. term.clear()
  408. term.setCursorPos(1,1)
  409. print('Choose the user to assign to this room:')
  410. local userIDs = {}
  411. local userTexts = {}
  412. table.insert(userIDs, 'CANCEL')
  413. table.insert(userTexts, 'CANCEL')
  414. for i,v in ipairs(passTable) do
  415. table.insert(userIDs, v.id)
  416. table.insert(userTexts, v.id .. ': ' .. v.user .. ' (' .. privNames[tonumber(v.privilege)] .. ')')
  417. end
  418. local selected_user = displayMenu(2,18, userTexts)
  419. if (selected_user > 1) then
  420. for i,v in ipairs(roomTable) do
  421. if (v.id == room.id) then
  422. v.userID = userIDs[selected_user]
  423. local user = getUserByID(room.userID)
  424. term.clear()
  425. term.setCursorPos(1,1)
  426. print('Room ' .. room.id .. " has been assigned to user '" .. user.user .. "'.")
  427. saveRoomsToFile()
  428. break
  429. end
  430. end
  431. end
  432. end
  433. end
  434. elseif (choice == 1) then
  435. openTrap()
  436. term.clear()
  437. term.setCursorPos(1,1)
  438. print('Opened trap.')
  439. elseif (choice == 2) then
  440. closeTrap()
  441. term.clear()
  442. term.setCursorPos(1,1)
  443. print('Closed trap.')
  444. end
  445. sleep(1)
  446. end
  447. end
  448.  
  449. local function serverSideStuff()
  450. parallel.waitForAny(menu, buttonHandler)
  451. end
  452.  
  453. parallel.waitForAny(main, serverSideStuff)
Add Comment
Please, Sign In to add comment