aboutsummaryrefslogtreecommitdiffstats
path: root/modules/host/egress.lua
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2026-11-17 01:01:06 +0000
committertv <tv@krebsco.de>2026-11-17 01:01:06 +0000
commitff3dafcc0003ffc2d48244fcd6e98984b6a9174d (patch)
tree155a9ca9fed6d2ab3000665f724d98e8a0890937 /modules/host/egress.lua
parent9d7f66b9cbdc203226ef919a4108706248d9779d (diff)
per-user egress rules
The host module takes users = { NAME = TAPS; ... } instead of a single user: each user gets that many taps, and rules of their own, in /var/lib/fc-egress/USER/allowed, managed with fc-egress as before. The proxy tells whose a guest is by its address: each tap has a fixed one, handed out by dnsmasq by MAC address (derived from the tap's number, as the run script does), and the bridge drops anything a guest sends from another address. The decrypting server gets a socket per user, as it can't see the guest's address. The run script only takes the user's own taps.
Diffstat (limited to 'modules/host/egress.lua')
-rw-r--r--modules/host/egress.lua37
1 files changed, 23 insertions, 14 deletions
diff --git a/modules/host/egress.lua b/modules/host/egress.lua
index 7a2b3af..6875608 100644
--- a/modules/host/egress.lua
+++ b/modules/host/egress.lua
@@ -1,6 +1,8 @@
-- The guests' egress policy, for nginx (OpenResty), see default.nix.
--
--- The rules (one per line, read on every connection and request):
+-- Per user: whose a guest is, egress_users.lua says by its address (which
+-- the bridge makes sure of, see default.nix). The user's rules are in
+-- RULES/USER/allowed, one per line, read on every connection and request:
-- example.com example.com and its subdomains, passed through
-- without decrypting
-- example.com/foo/ only requests whose path starts with /foo/,
@@ -13,18 +15,19 @@
local _M = {}
-local RULES = os.getenv("FC_EGRESS_RULES")
+local RULES = os.getenv("FC_EGRESS_RULES") -- RULES/USER/allowed
local STATE = os.getenv("FC_EGRESS_STATE") -- ca.key, ca.pem
-local DECRYPT = os.getenv("FC_EGRESS_DECRYPT") -- the http server's socket
+local RUN = os.getenv("FC_EGRESS_RUN") -- RUN/decrypt-USER.sock: the http server
+local users = require("egress_users") -- guest address -> user
local function valid_name(s)
return s ~= nil and s:match("^[a-z0-9-]+[a-z0-9.-]*$") ~= nil
and not s:find("%.%.") and s:sub(-1) ~= "."
end
-local function rules()
+local function rules(user)
local domains, paths = {}, {}
- local f = io.open(RULES)
+ local f = io.open(RULES .. "/" .. user .. "/allowed")
if not f then
return domains, paths
end
@@ -44,12 +47,12 @@ local function rules()
end
-- nil (deny), "pass", or the list of allowed path prefixes
-local function policy(host)
+local function policy(host, user)
host = host and host:lower()
- if not valid_name(host) then
+ if not user or not valid_name(host) then
return nil
end
- local domains, paths = rules()
+ local domains, paths = rules(user)
local prefixes = {}
local suffix = host
while suffix do
@@ -69,14 +72,15 @@ end
-- stream, preread: pass through, decrypt, or close
function _M.preread()
local sni = ngx.var.ssl_preread_server_name
- local p = policy(sni)
+ local user = users[ngx.var.remote_addr]
+ local p = policy(sni, user)
if p == "pass" then
ngx.var.egress_upstream = sni:lower() .. ":443"
- ngx.var.egress_log = "ALLOW " .. sni:lower()
+ ngx.var.egress_log = "ALLOW " .. user .. " " .. sni:lower()
elseif p then
- ngx.var.egress_upstream = "unix:" .. DECRYPT
+ ngx.var.egress_upstream = "unix:" .. RUN .. "/decrypt-" .. user .. ".sock"
else
- ngx.var.egress_log = "DENY " .. (sni ~= "" and sni or "-")
+ ngx.var.egress_log = "DENY " .. (user or "-") .. " " .. (sni ~= "" and sni or "-")
return ngx.exit(403)
end
end
@@ -123,10 +127,13 @@ local function make_cert(name)
return assert(require("ngx.ssl").parse_pem_cert(cert:to_PEM()))
end
+-- Whose the connection is isn't known here; but only connections that
+-- some user's path rules allow get here (see preread), and each request
+-- gets checked in access.
function _M.certificate()
local ssl = require("ngx.ssl")
local name = ssl.server_name()
- if type(policy(name)) ~= "table" then
+ if not valid_name(name and name:lower()) then
return ngx.exit(ngx.ERROR)
end
name = name:lower()
@@ -142,10 +149,12 @@ end
-- http, access: check host and path of decrypted requests
function _M.access()
+ local user = (ngx.var.server_addr or ""):match("decrypt%-(.+)%.sock$")
+ ngx.var.egress_user = user or "-"
local sni = (ngx.var.ssl_server_name or ""):lower()
local host = (ngx.var.host or ""):lower()
local path = ngx.unescape_uri((ngx.var.request_uri or ""):match("^[^?]*"))
- local p = policy(sni)
+ local p = policy(sni, user)
local ok = type(p) == "table" and host == sni and path:sub(1, 1) == "/"
if ok then
for segment in path:gmatch("[^/]+") do