Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- The key and IV used for encryption and decryption
- local key = "a secret key"
- local iv = "an initialization vector"
- -- Function to check if a file exists
- function fileExists(path)
- local file = fs.open(path, "r")
- if file then
- file.close()
- return true
- end
- return false
- end
- -- Function to verify a user and their security level
- function verifyUser()
- -- Wait for a message from the client
- local door_id, message = rednet.receive()
- -- Decrypt the message
- local decryptedMessage = decrypt("aes-256-cbc", key, iv, message)
- -- Search the server for the user_id file
- if fileExists(decryptedMessage .. ".txt") then
- local file = fs.open(decryptedMessage .. ".txt", "r")
- user_serverside_pass = file.readLine()
- file.close()
- -- Inform the client computer that the user has been recognized
- local message = encrypt("aes-256-cbc", key, iv, "user recognized")
- rednet.send(door_id, message)
- -- Wait for a message from the client
- local door_id, message = rednet.receive()
- -- Decrypt the message
- local user_clientside_pass = decrypt("aes-256-cbc", key, iv, message)
- if (tostring(user_serverside_pass) == tostring(user_clientside_pass)) then
- local message = encrypt("aes-256-cbc", key, iv, "user verified")
- rednet.send(door_id, message)
- -- Send the client computer the new user password to store
- local file = fs.open(decryptedMessage .. ".txt", "w")
- -- Create a new, random user password and store it in the user_id file
- local newID = math.random(10000, 99999)
- file.write(newID)
- file.close()
- local encryptedID = encrypt("aes-256-cbc", key, iv, tostring(newID))
- rednet.send(door_id, encryptedID)
- local user_level = fs.open(decryptedMessage .. "_level.txt", "r")
- local door_level = fs.open("door_" .. door_id .. "_level", "r")
- -- If the user has a sufficiently high access level, then return true
- if (tonumber(user_level) >= tonumber(door_level)) then
- --inform the client computer that access is granted
- local message = encrypt("aes-256-cbc", key, iv, "access granted")
- rednet.send(door_id, message)
- return true
- else
- -- inform client computer that the user has an insufficient access level for this door_
- local message = encrypt("aes-256-cbc", key, iv, "access denied")
- rednet.send(door_id, message)
- return false
- end
- else
- -- Inform client computer that this is not a valid id card
- local message = encrypt("aes-256-cbc", key, iv, "wrong password")
- rednet.send(door_id, message)
- return false
- end
- else
- -- Inform client computer that this is not a valid user id
- local message = encrypt("aes-256-cbc", key, iv, "invalid user id")
- rednet.send(door_id, message)
- return false
- end
- end
- while true do
- -- Wait for a message from a client
- local door_id, message = rednet.receive()
- -- Decrypt the message
- local decryptedMessage = decrypt("aes-256-cbc", key, iv, message)
- -- Check the message
- if decryptedMessage == "add user" then
- -- Verify if the user generating the card is an admin level account
- if verifyUser() then
- -- Wait for the client to send the new user_id
- local door_id, message = rednet.receive()
- -- Decrypt the message
- local user_id = decrypt("aes-256-cbc", key, iv, message)
- -- check to make sure user_id does not already exist
- if not fileExists(user_id .. ".txt") then
- -- Inform the admin console that this was an acceptable user id
- -- Create a file with the new user_id
- local file = fs.open(user_id .. ".txt", "w")
- -- Create a new, random user password and store it in the user_id file
- local newID = math.random(10000, 99999)
- file.write(newID)
- file.close()
- -- Inform the admin console that the user has been added
- local message = encrypt("aes-256-cbc", key, iv, "user added")
- rednet.send(door_id, message)
- -- Send the admin console the new user password to store
- local encryptedID = encrypt("aes-256-cbc", key, iv, tostring(newID))
- rednet.send(door_id, encryptedID)
- -- Wait the admin console to send the access level for the user
- local door_id, message = rednet.receive()
- -- Decrypt the message
- local user_level = decrypt("aes-256-cbc", key, iv, message)
- local file = fs.open(user_id .. "_level.txt", "w")
- file.write(user_level)
- file.close()
- else
- -- Tell admin console to pick a different user_id
- local message = encrypt("aes-256-cbc", key, iv, "not a unique user id")
- rednet.send(door_id, message)
- end
- end
- else
- if verifyUser() then
- -- Client computer has been informed that access is granted
- else
- -- Client computer has been informed of reason for failure
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement