Advertisement
tobast

[CC/cardauth] auth_server

Jul 30th, 2014
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.23 KB | None | 0 0
  1. --[[
  2.     Serveur d'authentification. Réagit aux requêtes réseau
  3.     d'identification, crée des badges.
  4.     Système de protection : hachage sha-256.
  5.  
  6.     Dépendances :
  7.     - slot_sig
  8.     - sha256
  9. --]]
  10.  
  11. os.loadAPI('/lib/slot_sig')
  12. os.loadAPI('/lib/sha256')
  13.  
  14. ------------- PARAMETERS --------------
  15. ID_READER_SIDE='left'
  16. ID_MAKER_SIDE='right'
  17. MODEM_SIDE='back'
  18. MAINTENANCE_STR='MAINTENANCE'
  19. ------------- END PARAMS --------------
  20.  
  21. local hashDb = {}
  22. local groupsDb = {}
  23. local id_maker_auth = {} -- set of authorized IDs to create IDs.
  24.  
  25. os.pullEvent = os.pullEventRaw -- No return to terminal.
  26.  
  27. function rednetSend(to, message)
  28.     rednet.send(to, message)
  29. end
  30.  
  31. function authRequest(senderId, data, distance)
  32.     if (hashDb[data.hash] ~= nil) then
  33.         rednetSend(senderId, { type='authreply',
  34.             user = hashDb[data.hash],
  35.             groups = groupsDb[hashDb[data.hash]] })
  36.     else
  37.         rednetSend(senderId, {type = 'authreply'})
  38.     end
  39. end
  40.  
  41. function onRednet(input)
  42.     ---EVENT: rednet_message
  43.     senderId = input[1]
  44.     if (type(input[2]) == 'string') then
  45.         data = textutils.unserialize(input[2])
  46.        
  47.         if (data.reqtype == 'auth') then
  48.             authRequest(senderId, data, distance)
  49.         end
  50.     end
  51. end
  52.  
  53. ----------- LOCAL ID FUNCTIONS -----------
  54.  
  55. function checkAuth()
  56.     if not disk.hasData(ID_READER_SIDE) then
  57.         return false
  58.     end
  59.  
  60.     loc = '/'..disk.getMountPath(ID_READER_SIDE)..'/key'
  61.     fileHandle = fs.open(loc,'r')
  62.     if fileHandle == nil then
  63.         return false
  64.     end
  65.     key = fileHandle.readLine()
  66.     fileHandle.close()
  67.  
  68.     if key==MAINTENANCE_STR then
  69.         slot_sig.quit()
  70.     end
  71.  
  72.     keyHash = sha256.sha256(key)
  73.     keyId = hashDb[keyHash]
  74.    
  75.     if keyId == nil or id_maker_auth[keyId] ~= true then
  76.         return false
  77.     end
  78.     return true
  79. end
  80.  
  81. function createKey()
  82.     key = ""
  83.     for i=1,2048 do
  84.         key = key..string.char(math.random(33,126))
  85.     end
  86.     return key
  87. end
  88.  
  89. function createID(idName, isMaster)
  90.     if not(disk.isPresent(ID_MAKER_SIDE)) then
  91.         printError("ID removed.")
  92.     end
  93.     disk.setLabel(ID_MAKER_SIDE, 'ID: '..idName)
  94.     mountpath = disk.getMountPath(ID_MAKER_SIDE)
  95.     keyHandle = fs.open(mountpath..'/key', 'w')
  96.  
  97.     key = createKey()
  98.     hash = sha256.sha256(key)
  99.  
  100.     hashDb[hash] = idName
  101.     if(isMaster) then
  102.         id_maker_auth[idName] = true
  103.     end
  104.  
  105.     groupSplit = {}
  106.     for grp in groups:gmatch("%S+") do
  107.         groupSplit[#groupSplit+1] = grp
  108.     end
  109.     groupsDb[idName] = groupSplit
  110.    
  111.     keyHandle.writeLine(key)
  112.     keyHandle.close()
  113.     disk.eject(ID_MAKER_SIDE)
  114. end
  115.  
  116. function onDiskInsert(data)
  117.     --- SIGNAL: disk
  118.     side = data[1]
  119.     if(side == ID_READER_SIDE) then
  120.         isAuth = checkAuth()
  121.         authorizedId(isAuth)
  122.     elseif(side == ID_MAKER_SIDE) then
  123.         if(checkAuth()) then
  124.             -- Logged in, and creating a new card
  125.             newId = getInput("Please type in the new ID:")
  126.             isMaster = (getInput("Register as master ID (yes/no)?") == 'yes')
  127.             groups = getInput("Groups the user belongs to (space separated:")
  128.             createID(newId, isMaster, groups)
  129.             disk.eject(ID_READER_SIDE)
  130.             writeDB('/auth/hashdb',hashDb)
  131.             writeDB('/auth/groupsdb',groupsDb)
  132.             if(isMaster) then
  133.                 writeDB('/auth/idmakerdb',id_maker_auth)
  134.             end
  135.         else
  136.             printError("Please insert your ID.")
  137.             disk.eject(ID_MAKER_SIDE)
  138.         end
  139.     end
  140. end
  141.  
  142. function onDiskRemoved(data)
  143.     -- SIGNAL: disk_eject
  144.     side = data[1]
  145.     if(side == ID_READER_SIDE) then
  146.         authorizedId(false)
  147.         disk.eject(ID_MAKER_SIDE)
  148.     end
  149. end
  150.  
  151. ----------- GUI FUNCTIONS ----------------
  152.  
  153. function serverRunning(isRunning)
  154.     term.setCursorPos(1,1)
  155.     if(isRunning) then
  156.         term.setBackgroundColor(colors.lime)
  157.         term.write("Server running")
  158.     else
  159.         term.setBackgroundColor(colors.red)
  160.         term.write("Server offline")
  161.     end
  162.     term.setBackgroundColor(colors.black)
  163. end
  164.  
  165. function authorizedId(isAuth)
  166.     term.setCursorPos(16,1)
  167.     if isAuth then
  168.         term.setBackgroundColor(colors.lime)
  169.         term.write("Logged in ")
  170.     else
  171.         term.setBackgroundColor(colors.red)
  172.         term.write("Not logged")
  173.     end
  174.     term.setBackgroundColor(colors.black)
  175. end
  176.  
  177. function clearLine(lineId)
  178.     term.setCursorPos(1,lineId)
  179.     term.clearLine()
  180. end
  181.  
  182. function printError(str)
  183.     term.setCursorPos(1,3)
  184.     term.clearLine()
  185.     term.setTextColor(colors.red)
  186.     term.write(str)
  187.     term.setTextColor(colors.black)
  188. end
  189.  
  190. function getInput(prompt)
  191.     term.setCursorPos(1,5)
  192.     term.write(prompt.." ")
  193.     out = read()
  194.     term.setCursorPos(1,5)
  195.     term.clearLine()
  196.     return out
  197. end
  198.  
  199. function mainscreen()
  200.     term.clear()
  201.     serverRunning(true)
  202.     authorizedId(checkAuth())
  203. end
  204.  
  205. -------- FS FUNCTIONS ----------------
  206.  
  207. function loadDB(path)
  208.     handle = fs.open(path, 'r')
  209.     if(handle == nil) then
  210.         return {}
  211.     end
  212.     var = textutils.unserialize(handle.readAll())
  213.     handle.close()
  214.  
  215.     if var == nil then
  216.         return {}
  217.     end
  218.     return var
  219. end
  220.  
  221. function writeDB(path,var)
  222.     handle = fs.open(path,'w')
  223.     if(handle == nil) then
  224.         return
  225.     end
  226.     handle.write(textutils.serialize(var))
  227.     handle.close()
  228. end
  229.  
  230. -------- MAIN FUNCTION ---------------
  231.  
  232. function main()
  233.     hashDb = loadDB('/auth/hashdb')
  234.     id_maker_auth = loadDB('/auth/idmakerdb')
  235.     groupsDb = loadDB('/auth/groupsdb')
  236.  
  237.     rednet.open(MODEM_SIDE)
  238.     rednet.host("auth", "server")
  239.  
  240.     mainscreen()
  241.    
  242.     slot_sig.connectSlot('rednet_message', onRednet)
  243.     slot_sig.connectSlot('disk', onDiskInsert)
  244.     slot_sig.connectSlot('disk_eject', onDiskRemoved)
  245.     slot_sig.run()
  246. end
  247. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement