diff --git a/tg_server/tg_server.lua b/tg_server/tg_server.lua index 48a66b9..1cb5ec2 100644 --- a/tg_server/tg_server.lua +++ b/tg_server/tg_server.lua @@ -1,5 +1,6 @@ local fullPath = shell.getRunningProgram() local dir = fullPath:match("^(.*/)") or "" +local args = {...} local config = dofile(dir..'taskguard.cfg') @@ -50,62 +51,98 @@ local function validate_key(card_holder, card_clearance, random_chars) end end -rednet.open(config.modem_side) -rednet.host('tg', config.name) -print("Server started, waiting for requests...") +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, ',') + 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 - 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 - - 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 + 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 - -- Auth not granted - rednet.send(id, 'failed', 'tg') - print('Finished transaction, failed') - end - print("EOP") - elseif msg_parts[1] == 'authenticate' then - print("Beginning transaction for authentication with "..id) - local card_holder = msg_parts[2] - local card_clearance = msg_parts[3] + if has_clearance == false then + -- Auth not granted + rednet.send(id, 'failed', 'tg') + print('Finished transaction, failed') + end + print("EOP") + elseif msg_parts[1] == 'authenticate' then + print("Beginning transaction for authentication with "..id) + local card_holder = msg_parts[2] + 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 - table.insert(clearances, { computer_id = tonumber(id), clearance_level = tonumber(card_clearance), time_granted = os.clock() }) + 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) + 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") - print("Granted card "..card_hash.." clearance level "..card_clearance) - else - print("Unknown card "..card_hash) - rednet.send(id, "authentication failed", "tg") + else + rednet.send(id, "authentication failed", "tg") + end + print("Transaction finished") 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] + 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] - 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") + 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 - 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 \ No newline at end of file