Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- rednet.open("top")
- -- 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(door_id, message)
- local client_user_id = string.match(message, "<cui>(%w+)</cui>")
- local client_user_pass = string.match(message, "<cup>(%w+)</cup>")
- local client_user_pass = client_user_pass .. "0"
- local server_user_id_result = "unmatched"
- local server_user_pass_result = "incorrect"
- local newID = 00000
- local server_user_access_result = "denied"
- local user_serverside_pass = 00000
- local user_level = 0
- local file = fs.open("door_" .. door_id .. "_level.txt", "r")
- door_level = file.readLine()
- file.close()
- -- Search the server for the user_id file
- if fileExists(client_user_id .. ".txt") then
- local file = fs.open(client_user_id .. ".txt", "r")
- user_serverside_pass = file.readLine()
- file.close()
- user_serverside_pass = user_serverside_pass:gsub("[^%w ]", "")
- server_user_id_result = "recognized"
- print("about to wait for client to send pass")
- -- Wait for a message from the client
- print("user pass is: " .. client_user_pass)
- print("server pass is: " .. user_serverside_pass)
- print("about to test passwords")
- else
- -- Inform client computer that this is not a valid user id
- local server_user_id_result = "unmatched"
- end
- if (tostring(user_serverside_pass) == tostring(client_user_pass)) then
- print("passwords matched")
- server_user_pass_result = "verified"
- -- Send the client computer the new user password to store
- local file = fs.open(client_user_id .. ".txt", "w")
- -- Create a new, random user password and store it in the user_id file
- newID = math.random(10000, 99999)
- file.write(newID)
- file.close()
- print(client_user_id .. "_level.txt")
- local file = fs.open(client_user_id .. "_level.txt", "r")
- user_level = file.readLine()
- file.close()
- print("door_id is: " .. door_id)
- -- If the user has a sufficiently high access level, then return true
- print("user level is: " .. tonumber(user_level))
- print("door level is: " .. tonumber(door_level))
- else
- print("passwords did not match")
- -- Inform client computer that this is not a valid id card
- local server_user_pass_result = "incorrect"
- end
- if (tonumber(user_level) >= tonumber(door_level)) then
- --inform the client computer that access is granted
- server_user_access_result = "granted"
- print(server_user_access_result)
- else
- -- inform client computer that the user has an insufficient access level for this door_
- server_user_access_result = "denied"
- print(server_user_access_result)
- end
- rednet.send(door_id, "<suir>" .. server_user_id_result .. "</suir>" .. "<supr>" .. server_user_pass_result .. "</supr>" .. "<sunp>" .. newID .. "</sunp>" .. "<suar>" .. server_user_access_result .. "</suar>")
- if ((server_user_id_result == "recognized") and (server_user_pass_result == "verified") and (server_user_access_result == "granted")) then
- return true
- else
- return false
- end
- end
- while true do
- -- Wait for a message from a client
- local door_id, message = rednet.receive()
- local access_reason = string.match(message, "<ar>(%w+)</ar>")
- -- Check the message
- print("enter loop")
- print(message)
- if access_reason == "door" then
- print("received door access request")
- verifyUser(door_id, message)
- elseif access_reason == "add" then
- print("received new user request")
- -- Verify if the user generating the card is an admin level account
- if verifyUser(door_id, message) then
- -- Wait for the client to send the new user_id
- local door_id, user_id = rednet.receive()
- -- 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
- 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
- rednet.send(door_id, "user added")
- -- Send the admin console the new user password to store
- rednet.send(door_id, newID)
- -- Wait the admin console to send the access level for the user
- local door_id, user_level = rednet.receive()
- 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
- rednet.send(door_id, "not a unique user id")
- end
- else
- --Deny access
- print("access denied")
- end
- else
- --Do Nothing
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement