Resolve #1 #3
No files matched your search
+24
-3
@@ -1,3 +1,5 @@
|
|||||||
|
tg_server_name = "tg_server"
|
||||||
|
|
||||||
local args = {...}
|
local args = {...}
|
||||||
|
|
||||||
local function split(str, divider)
|
local function split(str, divider)
|
||||||
@@ -21,7 +23,7 @@ local function establish_connection()
|
|||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
function validate_clearance(computer_id, clearance_level, tg_server_name)
|
function validate_clearance(computer_id, clearance_level)
|
||||||
if establish_connection() then
|
if establish_connection() then
|
||||||
tg_server_id = rednet.lookup("tg", tg_server_name)
|
tg_server_id = rednet.lookup("tg", tg_server_name)
|
||||||
rednet.send(tg_server_id, "validate_clearance,"..computer_id..","..clearance_level)
|
rednet.send(tg_server_id, "validate_clearance,"..computer_id..","..clearance_level)
|
||||||
@@ -36,12 +38,31 @@ function validate_clearance(computer_id, clearance_level, tg_server_name)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function validate_key(minimum_clearance)
|
||||||
|
if establish_connection() then
|
||||||
|
if fs.exists("/disk/key") then
|
||||||
|
key_file = fs.open("/disk/key","r")
|
||||||
|
key = key_file.readLine()
|
||||||
|
tg_server_id = rednet.lookup('tg', tg_server_name)
|
||||||
|
rednet.send(tg_server_id, "validate_key,"..key..","..minimum_clearance, "tg")
|
||||||
|
id, msg, _ = rednet.receive("tg")
|
||||||
|
if msg == "authentication successful" then
|
||||||
|
return true
|
||||||
|
else
|
||||||
|
return false, "authentication error: "..msg
|
||||||
|
end
|
||||||
|
else
|
||||||
|
return false, "key file not found"
|
||||||
|
end
|
||||||
|
else
|
||||||
|
return false, "modem not found"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
if fs.exists('/disk/key') then
|
if fs.exists('/disk/key') then
|
||||||
key_file = fs.open('/disk/key', 'r')
|
key_file = fs.open('/disk/key', 'r')
|
||||||
key = key_file.readLine()
|
key = key_file.readLine()
|
||||||
if establish_connection() then
|
if establish_connection() then
|
||||||
local tg_server_name = "tg_server"
|
|
||||||
|
|
||||||
tg_server_id = rednet.lookup("tg", tg_server_name)
|
tg_server_id = rednet.lookup("tg", tg_server_name)
|
||||||
rednet.send(tg_server_id, "authenticate,"..key,"tg")
|
rednet.send(tg_server_id, "authenticate,"..key,"tg")
|
||||||
local id, msg, proto = rednet.receive("tg")
|
local id, msg, proto = rednet.receive("tg")
|
||||||
|
|||||||
+37
-15
@@ -34,7 +34,28 @@ local function check_for_outdated_sessions()
|
|||||||
#clearances .. " active session(s) remain.")
|
#clearances .. " active session(s) remain.")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function validate_key(card_holder, card_clearance, random_chars)
|
||||||
|
local card_hash = hash(card_holder..','..card_clearance..','..random_chars)
|
||||||
|
local key_file = fs.open(dir..config.key_file, 'r')
|
||||||
|
local card_authentic = false
|
||||||
|
|
||||||
|
while true do
|
||||||
|
local trusted_hash = key_file.readLine()
|
||||||
|
if trusted_hash == card_hash then
|
||||||
|
print("Valid card "..card_hash)
|
||||||
|
card_authentic = true
|
||||||
|
return true, card_hash
|
||||||
|
end
|
||||||
|
if not line then break end -- Stop when EOF reached
|
||||||
|
end
|
||||||
|
|
||||||
|
key_file.close()
|
||||||
|
|
||||||
|
if card_authentic == false then
|
||||||
|
print("Unknown card "..card_hash)
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
rednet.open(config.modem_side)
|
rednet.open(config.modem_side)
|
||||||
rednet.host('tg', config.name)
|
rednet.host('tg', config.name)
|
||||||
@@ -69,28 +90,29 @@ while true do
|
|||||||
print("Beginning transaction for authentication with "..id)
|
print("Beginning transaction for authentication with "..id)
|
||||||
local card_holder = msg_parts[2]
|
local card_holder = msg_parts[2]
|
||||||
local card_clearance = msg_parts[3]
|
local card_clearance = msg_parts[3]
|
||||||
local card_hash = hash(card_holder..','..card_clearance..','..msg_parts[4])
|
|
||||||
local key_file = fs.open(dir..config.key_file, 'r')
|
|
||||||
local card_authentic = false
|
|
||||||
|
|
||||||
while true do
|
local is_authentic, card_hash = validate_key(card_holder, card_clearance, msg_parts[4])
|
||||||
local trusted_hash = key_file.readLine()
|
|
||||||
if trusted_hash == card_hash then
|
if is_authentic then
|
||||||
table.insert(clearances, { computer_id = tonumber(id), clearance_level = tonumber(card_clearance), time_granted = os.clock() })
|
table.insert(clearances, { computer_id = tonumber(id), clearance_level = tonumber(card_clearance), time_granted = os.clock() })
|
||||||
rednet.send(id, "authentication successful", "tg")
|
rednet.send(id, "authentication successful", "tg")
|
||||||
print("Granted card "..card_hash.." clearance level "..card_clearance)
|
print("Granted card "..card_hash.." clearance level "..card_clearance)
|
||||||
|
else
|
||||||
card_authentic = true
|
|
||||||
end
|
|
||||||
if not line then break end -- Stop when EOF reached
|
|
||||||
end
|
|
||||||
|
|
||||||
key_file.close()
|
|
||||||
|
|
||||||
if card_authentic == false then
|
|
||||||
print("Unknown card "..card_hash)
|
print("Unknown card "..card_hash)
|
||||||
rednet.send(id, "authentication failed", "tg")
|
rednet.send(id, "authentication failed", "tg")
|
||||||
end
|
end
|
||||||
print("Transaction finished")
|
print("Transaction finished")
|
||||||
|
elseif msg_parts[1] == 'validate_key' then
|
||||||
|
print("Beginning transaction for single-validation with "..id)
|
||||||
|
local card_holder = msg_parts[2]
|
||||||
|
local card_clearance = msg_parts[3]
|
||||||
|
local minimum_clearance = msg_parts[5]
|
||||||
|
|
||||||
|
if validate_key(card_holder, card_clearance, msg_parts[4]) and tonumber(card_clearance) >= tonumber(minimum_clearance) then
|
||||||
|
rednet.send(id, "authentication successful", "tg")
|
||||||
|
else
|
||||||
|
rednet.send(id, "authentication failed", "tg")
|
||||||
|
end
|
||||||
|
print("Transaction finished")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
Reference in new issue
Block a user