Download raw body.
relayd: add "pass log" and "block log" to log filter decisions per request
An unexpectedly simple diff to log all pass/block filter rules. The
syntax is the same as in pf.conf. Here’s an example of what it looks
like:
block log:
│Nov 08 08:43:34 xxx relayd-dev[89074]: relay https, session 2402, rule 14: block request from 195.178.110.242: GET sizeofvoid.org/wp-config.php.bak
│Nov 08 08:43:34 xxx relayd-dev[89074]: relay https, session 2404, rule 14: block request from 195.178.110.242: GET sizeofvoid.org/wp-config.php.bak
│Nov 08 08:43:34 xxx relayd-dev[89074]: relay https, session 2405, rule 14: block request from 195.178.110.242: GET sizeofvoid.org/wp-content/debug.log
│Nov 08 08:48:39 xxx relayd-dev[64971]: relay https, session 3360, rule 17: block request from 45.138.12.6: GET rsadowski.de/.git/config
hello php2shell ;)
pass log:
│Nov 08 09:14:01 xxx relayd-dev[34622]: relay https, session 5, rule 20: pass request from 78.242.81.208: GET rsadowski.de/index.xml
│Nov 08 09:13:01 xxx relayd-dev[59710]: relay https, session 2, rule 20: pass request from 10.100.0.6: PROPFIND cloud.org/remote.php/dav/calendars/xxx/
I'm sure it could be taken even further, but I think it does the job.
Feedback, OK?
commit 778f29dc26cd5693d89aeb2886f9ad4e11658ddc
Author: Rafael Sadowski <rafael@sizeofvoid.org>
Date: Sat Nov 7 22:46:01 2026 +0100
relayd: add "pass log" and "block log" to log filter decisions per request
diff --git a/parse.y b/parse.y
index fcc019e..16d13ed 100644
--- a/parse.y
+++ b/parse.y
@@ -217,7 +217,7 @@ typedef struct {
%type <v.number> dstmode flag forwardmode retry
%type <v.number> opttls opttlsclient optproxyproto
%type <v.number> redirect_proto relay_proto match pflog
-%type <v.number> action ruleaf key_option optpattern
+%type <v.number> action actionlog ruleaf key_option optpattern
%type <v.port> port
%type <v.host> host
%type <v.addr> address rulesrc ruledst addrprefix
@@ -1506,19 +1506,24 @@ flag : STRING {
}
;
-filterrule : action dir quick ruleaf rulesrc ruledst {
+filterrule : action actionlog dir quick ruleaf rulesrc ruledst {
+ if ($2 && $1 == RULE_ACTION_MATCH) {
+ yyerror("log is only supported for pass "
+ "and block");
+ YYERROR;
+ }
if ((rule = calloc(1, sizeof(*rule))) == NULL)
fatal("out of memory");
rule->rule_action = $1;
rule->rule_proto = proto->type;
- rule->rule_dir = $2;
- rule->rule_flags |= $3;
- rule->rule_af = $4;
- rule->rule_src.addr = $5.ss;
- rule->rule_src.addr_mask = $5.prefixlen;
- rule->rule_dst.addr = $6.ss;
- rule->rule_dst.addr_mask = $6.prefixlen;
+ rule->rule_dir = $3;
+ rule->rule_flags |= $2 | $4;
+ rule->rule_af = $5;
+ rule->rule_src.addr = $6.ss;
+ rule->rule_src.addr_mask = $6.prefixlen;
+ rule->rule_dst.addr = $7.ss;
+ rule->rule_dst.addr_mask = $7.prefixlen;
if (RELAY_AF_NEQ(rule->rule_af,
rule->rule_src.addr.ss_family) ||
@@ -1581,6 +1586,11 @@ quick : /* empty */ { $$ = 0; }
| QUICK { $$ = RULE_FLAG_QUICK; }
;
+actionlog : /* empty */ { $$ = 0; }
+ | LOG { $$ = RULE_FLAG_LOG; }
+ ;
+
+
ruleaf : /* empty */ { $$ = AF_UNSPEC; }
| INET6 { $$ = AF_INET6; }
| INET { $$ = AF_INET; }
diff --git a/relay.c b/relay.c
index 1f28ccd..ffb2a9c 100644
--- a/relay.c
+++ b/relay.c
@@ -155,6 +155,9 @@ relay_ruledebug(struct relay_rule *rule)
if (rule->rule_flags & RULE_FLAG_QUICK)
fprintf(stderr, "quick ");
+ if (rule->rule_flags & RULE_FLAG_LOG)
+ fprintf(stderr, "log ");
+
switch (rule->rule_af) {
case AF_INET:
fprintf(stderr, "inet ");
diff --git a/relay_http.c b/relay_http.c
index ad95fef..ec781e0 100644
--- a/relay_http.c
+++ b/relay_http.c
@@ -83,6 +83,8 @@ char *server_root_strip(char *, int);
int relay_http_parse_startline(struct ctl_relay_event *, char *,
enum httpmethod *);
static int relay_url_log_match(const struct kv *, const char *);
+static void relay_log_rule(struct relay_rule *, struct http_descriptor *,
+ struct rsession *, struct ctl_relay_event *, u_int);
static struct relayd *env = NULL;
@@ -2046,6 +2048,8 @@ relay_test(struct protocol *proto, struct ctl_relay_event *cre)
action = RES_DROP;
}
+ relay_log_rule(rule, desc, con, cre, action);
+
log_debug("%s: session %d: action %d", __func__,
con->se_id, action);
@@ -2249,3 +2253,30 @@ relay_url_log_match(const struct kv *kv, const char *str)
/* without a keyword, url log keys are case-insensitive glob(7) */
return (fnmatch(kv->kv_key, str, FNM_CASEFOLD) != FNM_NOMATCH);
}
+
+static void
+relay_log_rule(struct relay_rule *rule, struct http_descriptor *desc,
+ struct rsession *con, struct ctl_relay_event *cre, u_int action)
+{
+ struct kv *host, key;
+ const char *meth;
+ char hbuf[128];
+
+ if (rule == NULL || !(rule->rule_flags & RULE_FLAG_LOG))
+ return;
+
+ memset(&key, 0, sizeof(key));
+ key.kv_key = "Host";
+ host = kv_find(&desc->http_headers, &key);
+ meth = relay_httpmethod_byid(desc->http_method);
+
+ (void)print_host(&con->se_in.ss, hbuf, sizeof(hbuf));
+
+ log_info("relay %s, session %d, rule %d: %s %s from %s: %s %s%s",
+ con->se_relay->rl_conf.name, con->se_id, rule->rule_id,
+ action == RES_PASS ? "pass" : "block",
+ cre->dir == RELAY_DIR_REQUEST ? "request" : "response",
+ hbuf, meth == NULL ? "-" : meth,
+ host == NULL || host->kv_value == NULL ? "" : host->kv_value,
+ desc->http_path == NULL ? "" : desc->http_path);
+}
diff --git a/relayd.conf.5 b/relayd.conf.5
index 377ff8f..9c20499 100644
--- a/relayd.conf.5
+++ b/relayd.conf.5
@@ -1154,6 +1154,15 @@ The connection is passed;
will continue to process the relay session normally.
.El
.Pp
+If the action is directly followed by the
+.Ic log
+keyword, the decision of a
+.Ic pass
+or
+.Ic block
+rule is logged for every request or response
+for which it is the deciding rule.
+.Pp
These filter parameters can be used in the rules:
.Bl -tag -width Ds
.It Ic request No or Ic response
diff --git a/relayd.h b/relayd.h
index 80466a9..bf2cec2 100644
--- a/relayd.h
+++ b/relayd.h
@@ -653,6 +653,7 @@ struct relay_rule {
objid_t rule_protoid;
u_int rule_action;
+ u_int action_log;
#define RULE_SKIP_PROTO 0
#define RULE_SKIP_DIR 1
#define RULE_SKIP_AF 2
@@ -663,6 +664,7 @@ struct relay_rule {
struct relay_rule *rule_skip[RULE_SKIP_COUNT];
#define RULE_FLAG_QUICK 0x01
+#define RULE_FLAG_LOG 0x02
u_int8_t rule_flags;
int rule_label;
relayd: add "pass log" and "block log" to log filter decisions per request