Compare commits

...
6 Commits
Author SHA1 Message Date
juhnsa c16da21d63 Add cli to add and remove cards 2025-08-27 13:09:04 +02:00
juhnsa b01b0d4eb5 outsource hash function 2025-08-26 16:14:10 +02:00
juhnsa a64c72d4fc Extend client api by authentication 2025-08-26 14:55:19 +02:00
Juhn-Vitus Saß 41cfa4c9c6 tg_server/tg_server.lua aktualisiert 2025-08-26 13:40:20 +02:00
Juhn-Vitus Saß 6d063ddf97 Merge pull request 'Resolve #1' (#3) from issue-1 into main
Reviewed-on: #3
2025-08-26 13:35:40 +02:00
juhnsa 6fa2e49578 Resolve #1 2025-08-26 13:33:03 +02:00
3 changed files with 132 additions and 41 deletions

No files matched your search

+45 -13
View File
@@ -1,3 +1,5 @@
tg_server_name = "tg_server"
local args = {...}
local function split(str, divider)
@@ -13,18 +15,16 @@ local function establish_connection()
for _, side in ipairs(peripherals) do
if peripheral.getType(side) == "modem" then
rednet.open(side)
print("Opened modem: "..side)
return true
end
end
print("No modem found!")
return false
end
function validate_clearance(computer_id, clearance_level, tg_server_name)
function validate_clearance(computer_id, clearance_level)
if establish_connection() then
tg_server_id = rednet.lookup("tg", tg_server_name)
rednet.send(tg_server_id, "validate_clearance,"..computer_id..","..clearance_level)
tg_server_id = rednet.lookup('tg', tg_server_name)
rednet.send(tg_server_id, "validate_clearance,"..computer_id..","..clearance_level,"tg")
local id, msg, proto = rednet.receive("tg")
if msg == "granted" then
return true
@@ -36,29 +36,61 @@ function validate_clearance(computer_id, clearance_level, tg_server_name)
end
end
if fs.exists('/disk/key') then
key_file = fs.open('/disk/key', 'r')
key = key_file.readLine()
function validate_key(minimum_clearance)
if establish_connection() then
local tg_server_name = "tg_server"
if fs.exists("/disk/key") then
local key_file = fs.open("/disk/key","r")
local key = key_file.readLine()
tg_server_id = rednet.lookup('tg', tg_server_name)
rednet.send(tg_server_id, "validate_key,"..key..","..minimum_clearance, "tg")
local 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
function authenticate()
if fs.exists('/disk/key') then
local key_file = fs.open('/disk/key', 'r')
local key = key_file.readLine()
if establish_connection() then
tg_server_id = rednet.lookup("tg", tg_server_name)
rednet.send(tg_server_id, "authenticate,"..key,"tg")
local id, msg, proto = rednet.receive("tg")
if msg == "authentication successful" then
key_parts = split(key,',')
local key_parts = split(key,',')
local card_holder = key_parts[1]
local card_clearance = key_parts[2]
print("Authentication was successful.")
print("Hello "..card_holder..", you were granted clearance level "..card_clearance.." for this computer.")
return true, _, card_holder, card_clearance
else
print("Authentication failed.")
return false, "authentication failed"
end
else
return false, "modem not found"
end
key_file.close()
else
else
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
+7
View File
@@ -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
+80 -28
View File
@@ -1,9 +1,10 @@
local fullPath = shell.getRunningProgram()
local dir = fullPath:match("^(.*/)") or ""
local args = {...}
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 t = {}
@@ -13,14 +14,7 @@ local function split(str, divider)
return t
end
local 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
dofile(dir..'hash.lua')
local function check_for_outdated_sessions()
local expired = 0
@@ -34,13 +28,34 @@ local function check_for_outdated_sessions()
#clearances .. " active session(s) remain.")
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
rednet.open(config.modem_side)
rednet.host('tg', config.name)
print("Server started, waiting for requests...")
key_file.close()
while true do
if card_authentic == false then
print("Unknown card "..card_hash)
return false
end
end
if args[1] == "serve" then
rednet.open(config.modem_side)
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, ',')
@@ -69,28 +84,65 @@ while true do
print("Beginning transaction for authentication with "..id)
local card_holder = msg_parts[2]
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 trusted_hash = key_file.readLine()
if trusted_hash == card_hash then
local is_authentic, card_hash = validate_key(card_holder, card_clearance, msg_parts[4])
if is_authentic then
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)
card_authentic = true
end
if not line then break end -- Stop when EOF reached
end
key_file.close()
if card_authentic == false then
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")
else
rednet.send(id, "authentication failed", "tg")
end
print("Transaction finished")
end
end
elseif args[1] == "add_card" then
if args[2] then
local new_card = args[2]
local new_card_parts = split(new_card, ',')
local card_holder = new_card_parts[1]
local key_file = fs.open(card_holder..'.key', 'w')
key_file.write(new_card)
key_file.close()
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
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