Compare commits
7
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
dca01a4116 | ||
|
|
c16da21d63 | ||
|
|
b01b0d4eb5 | ||
|
|
a64c72d4fc | ||
|
|
41cfa4c9c6 | ||
|
|
6d063ddf97 | ||
|
|
502065149b |
No files matched your search
+36
-25
@@ -15,18 +15,16 @@ local function establish_connection()
|
|||||||
for _, side in ipairs(peripherals) do
|
for _, side in ipairs(peripherals) do
|
||||||
if peripheral.getType(side) == "modem" then
|
if peripheral.getType(side) == "modem" then
|
||||||
rednet.open(side)
|
rednet.open(side)
|
||||||
print("Opened modem: "..side)
|
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
print("No modem found!")
|
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
function validate_clearance(computer_id, clearance_level)
|
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,"tg")
|
||||||
local id, msg, proto = rednet.receive("tg")
|
local id, msg, proto = rednet.receive("tg")
|
||||||
if msg == "granted" then
|
if msg == "granted" then
|
||||||
return true
|
return true
|
||||||
@@ -41,11 +39,11 @@ end
|
|||||||
function validate_key(minimum_clearance)
|
function validate_key(minimum_clearance)
|
||||||
if establish_connection() then
|
if establish_connection() then
|
||||||
if fs.exists("/disk/key") then
|
if fs.exists("/disk/key") then
|
||||||
key_file = fs.open("/disk/key","r")
|
local key_file = fs.open("/disk/key","r")
|
||||||
key = key_file.readLine()
|
local key = key_file.readLine()
|
||||||
tg_server_id = rednet.lookup('tg', tg_server_name)
|
tg_server_id = rednet.lookup('tg', tg_server_name)
|
||||||
rednet.send(tg_server_id, "validate_key,"..key..","..minimum_clearance, "tg")
|
rednet.send(tg_server_id, "validate_key,"..key..","..minimum_clearance, "tg")
|
||||||
id, msg, _ = rednet.receive("tg")
|
local id, msg, _ = rednet.receive("tg")
|
||||||
if msg == "authentication successful" then
|
if msg == "authentication successful" then
|
||||||
return true
|
return true
|
||||||
else
|
else
|
||||||
@@ -59,27 +57,40 @@ function validate_key(minimum_clearance)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if fs.exists('/disk/key') then
|
function authenticate()
|
||||||
key_file = fs.open('/disk/key', 'r')
|
if fs.exists('/disk/key') then
|
||||||
key = key_file.readLine()
|
local key_file = fs.open('/disk/key', 'r')
|
||||||
if establish_connection() then
|
local key = key_file.readLine()
|
||||||
tg_server_id = rednet.lookup("tg", tg_server_name)
|
if establish_connection() then
|
||||||
rednet.send(tg_server_id, "authenticate,"..key,"tg")
|
tg_server_id = rednet.lookup("tg", tg_server_name)
|
||||||
local id, msg, proto = rednet.receive("tg")
|
rednet.send(tg_server_id, "authenticate,"..key,"tg")
|
||||||
|
local id, msg, proto = rednet.receive("tg")
|
||||||
|
|
||||||
if msg == "authentication successful" then
|
if msg == "authentication successful" then
|
||||||
key_parts = split(key,',')
|
local key_parts = split(key,',')
|
||||||
local card_holder = key_parts[1]
|
local card_holder = key_parts[1]
|
||||||
local card_clearance = key_parts[2]
|
local card_clearance = key_parts[2]
|
||||||
print("Authentication was successful.")
|
return true, _, card_holder, card_clearance
|
||||||
print("Hello "..card_holder..", you were granted clearance level "..card_clearance.." for this computer.")
|
else
|
||||||
|
print("Authentication failed.")
|
||||||
|
return false, "authentication failed"
|
||||||
|
end
|
||||||
else
|
else
|
||||||
print("Authentication failed.")
|
return false, "modem not found"
|
||||||
end
|
end
|
||||||
|
key_file.close()
|
||||||
else
|
else
|
||||||
return false, "modem not found"
|
print("Key file not found. Aborting.")
|
||||||
|
return false, "key file not found"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if args[1] == "auth" or args[1] == "authenticate" then
|
||||||
|
local success, error, card_holder, card_clearance = authenticate()
|
||||||
|
if success then
|
||||||
|
print("Authentication was successful.")
|
||||||
|
print("Hello "..card_holder..", you were granted clearance level "..card_clearance.." for this computer.")
|
||||||
|
else
|
||||||
|
print("Authentication failed: "..error)
|
||||||
end
|
end
|
||||||
key_file.close()
|
|
||||||
else
|
|
||||||
print("Key file not found. Aborting.")
|
|
||||||
end
|
end
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
function hash(str)
|
||||||
|
local hash = 0
|
||||||
|
for i = 1, #str do
|
||||||
|
hash = (hash * 31 + string.byte(str, i)) % 65536
|
||||||
|
end
|
||||||
|
return string.format("%04X", hash)
|
||||||
|
end
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
local config = {
|
local config = {
|
||||||
name = "tg_server",
|
name = "tg_server",
|
||||||
modem_side = "top",
|
modem_side = "top",
|
||||||
clearance_expiry = 600, -- Minutes until clearance will expire
|
clearance_expiry = 600, -- Seconds until clearance will expire
|
||||||
key_file = "verified_keys.txt", -- Name of the key file, relative to tg_server.lua
|
key_file = "verified_keys.txt", -- Name of the key file, relative to tg_server.lua
|
||||||
}
|
}
|
||||||
return config
|
return config
|
||||||
+87
-57
@@ -1,9 +1,10 @@
|
|||||||
local fullPath = shell.getRunningProgram()
|
local fullPath = shell.getRunningProgram()
|
||||||
local dir = fullPath:match("^(.*/)") or ""
|
local dir = fullPath:match("^(.*/)") or ""
|
||||||
|
local args = {...}
|
||||||
|
|
||||||
local config = dofile(dir..'taskguard.cfg')
|
local config = dofile(dir..'taskguard.cfg')
|
||||||
|
|
||||||
local clearances = { { computer_id = 1, clearance_level = 1, time_granted = os.clock() } }
|
local clearances = { } -- { computer_id = 1, clearance_level = 1, time_granted = os.clock() }
|
||||||
|
|
||||||
local function split(str, divider)
|
local function split(str, divider)
|
||||||
local t = {}
|
local t = {}
|
||||||
@@ -13,14 +14,7 @@ local function split(str, divider)
|
|||||||
return t
|
return t
|
||||||
end
|
end
|
||||||
|
|
||||||
local function hash(str)
|
dofile(dir..'hash.lua')
|
||||||
local hash = 0
|
|
||||||
for i = 1, #str do
|
|
||||||
hash = (hash * 31 + string.byte(str, i)) % 65536
|
|
||||||
end
|
|
||||||
return string.format("%04X", hash)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
local function check_for_outdated_sessions()
|
local function check_for_outdated_sessions()
|
||||||
local expired = 0
|
local expired = 0
|
||||||
@@ -57,62 +51,98 @@ local function validate_key(card_holder, card_clearance, random_chars)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
rednet.open(config.modem_side)
|
if args[1] == "serve" then
|
||||||
rednet.host('tg', config.name)
|
rednet.open(config.modem_side)
|
||||||
print("Server started, waiting for requests...")
|
rednet.host('tg', config.name)
|
||||||
|
print("Server started, waiting for requests...")
|
||||||
|
while true do
|
||||||
|
local id, msg, proto = rednet.receive('tg')
|
||||||
|
check_for_outdated_sessions()
|
||||||
|
msg_parts = split(msg, ',')
|
||||||
|
if msg_parts[1] == 'validate_clearance' then
|
||||||
|
print("Beginning transaction for clearance query with "..id)
|
||||||
|
local target_id = tonumber(msg_parts[2])
|
||||||
|
local requested_clearance = tonumber(msg_parts[3])
|
||||||
|
local has_clearance = false
|
||||||
|
|
||||||
while true do
|
for i, entry in ipairs(clearances) do
|
||||||
local id, msg, proto = rednet.receive('tg')
|
if entry.computer_id == target_id and entry.clearance_level >= requested_clearance then
|
||||||
check_for_outdated_sessions()
|
-- Auth granted
|
||||||
msg_parts = split(msg, ',')
|
rednet.send(id, 'granted', 'tg')
|
||||||
if msg_parts[1] == 'validate_clearance' then
|
print('Finished transaction, granted')
|
||||||
print("Beginning transaction for clearance query with "..id)
|
has_clearance = true
|
||||||
local target_id = tonumber(msg_parts[2])
|
end
|
||||||
local requested_clearance = tonumber(msg_parts[3])
|
|
||||||
local has_clearance = false
|
|
||||||
|
|
||||||
for i, entry in ipairs(clearances) do
|
|
||||||
if entry.computer_id == target_id and entry.clearance_level >= requested_clearance then
|
|
||||||
-- Auth granted
|
|
||||||
rednet.send(id, 'granted', 'tg')
|
|
||||||
print('Finished transaction, granted')
|
|
||||||
has_clearance = true
|
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
if has_clearance == false then
|
if has_clearance == false then
|
||||||
-- Auth not granted
|
-- Auth not granted
|
||||||
rednet.send(id, 'failed', 'tg')
|
rednet.send(id, 'failed', 'tg')
|
||||||
print('Finished transaction, failed')
|
print('Finished transaction, failed')
|
||||||
end
|
end
|
||||||
print("EOP")
|
print("EOP")
|
||||||
elseif msg_parts[1] == 'authenticate' then
|
elseif msg_parts[1] == 'authenticate' then
|
||||||
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 is_authentic, card_hash = validate_key(card_holder, card_clearance, msg_parts[4])
|
local is_authentic, card_hash = validate_key(card_holder, card_clearance, msg_parts[4])
|
||||||
|
|
||||||
if is_authentic 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")
|
||||||
|
print("Granted card "..card_hash.." clearance level "..card_clearance)
|
||||||
|
else
|
||||||
|
print("Unknown card "..card_hash)
|
||||||
|
rednet.send(id, "authentication failed", "tg")
|
||||||
|
end
|
||||||
|
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")
|
rednet.send(id, "authentication successful", "tg")
|
||||||
print("Granted card "..card_hash.." clearance level "..card_clearance)
|
else
|
||||||
else
|
rednet.send(id, "authentication failed", "tg")
|
||||||
print("Unknown card "..card_hash)
|
end
|
||||||
rednet.send(id, "authentication failed", "tg")
|
print("Transaction finished")
|
||||||
end
|
end
|
||||||
print("Transaction finished")
|
end
|
||||||
elseif msg_parts[1] == 'validate_key' then
|
elseif args[1] == "add_card" then
|
||||||
print("Beginning transaction for single-validation with "..id)
|
if args[2] then
|
||||||
local card_holder = msg_parts[2]
|
local new_card = args[2]
|
||||||
local card_clearance = msg_parts[3]
|
local new_card_parts = split(new_card, ',')
|
||||||
local minimum_clearance = msg_parts[5]
|
local card_holder = new_card_parts[1]
|
||||||
|
|
||||||
if validate_key(card_holder, card_clearance, msg_parts[4]) and tonumber(card_clearance) >= tonumber(minimum_clearance) then
|
local key_file = fs.open(card_holder..'.key', 'w')
|
||||||
rednet.send(id, "authentication successful", "tg")
|
key_file.write(new_card)
|
||||||
else
|
key_file.close()
|
||||||
rednet.send(id, "authentication failed", "tg")
|
|
||||||
|
local trusted_keys = fs.open(dir..config.key_file, 'a')
|
||||||
|
local card_hash = hash(new_card)
|
||||||
|
trusted_keys.write(card_hash)
|
||||||
|
trusted_keys.close()
|
||||||
|
end
|
||||||
|
elseif args[1] == "remove_card" then
|
||||||
|
if args[2] then
|
||||||
|
local key_hash = args[2]
|
||||||
|
local trusted_keys = fs.open(dir..config.key_file, 'r')
|
||||||
|
local new_key_table = {}
|
||||||
|
while true do
|
||||||
|
local trusted_hash = trusted_keys.readLine()
|
||||||
|
if trusted_hash ~= key_hash then
|
||||||
|
table.insert(new_key_table, trusted_hash)
|
||||||
|
end
|
||||||
|
if not trusted_hash then break end -- Stop when EOF reached
|
||||||
end
|
end
|
||||||
print("Transaction finished")
|
trusted_keys.close()
|
||||||
|
|
||||||
|
local trusted_keys = fs.open(dir..config.key_file, 'w+')
|
||||||
|
for _, entry in ipairs(new_key_table) do
|
||||||
|
trusted_keys.write(entry .. '\n')
|
||||||
|
end
|
||||||
|
trusted_keys.close()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
Reference in new issue
Block a user