acl: a rule's port names the service it came through by @, not by a slash

The part after the @ of a port in a rule names the service: by its
port, by the address it listens on, or by both, the address in
brackets when IPv6 —

 connlim 5 60 * * * @1080
 connlim 5 60 * * * @10.0.0.1:1080
 deny * * * @[::1]

A slash, as the delimiter of the first take, would have read as a
CIDR habit the day a service is named inside an address list, and
@ cannot appear in a port or an address. Digits alone are a port, a
dashed pair a port range, anything else an address with a port behind
a colon; an @ with nothing after it is refused. The destination port
before the @ is unchanged, and so is every rule without one.
This commit is contained in:
Vladimir Dubrovin committed 2026-11-21 17:07:09 +03:00
1 parent f951160329
commit a3e93aa23a
7 files changed
+124 -30

No files matched your search

+7 -6
View File
@@ -918,13 +918,14 @@ of a file to include. The same patterns, and the same prefix, are used by the
\fBhttp\fR command, see BUILT IN HTTP SERVER. Regular expressions are matched \fBhttp\fR command, see BUILT IN HTTP SERVER. Regular expressions are matched
per request and cost more than a wildmask, which is enough for most rules. per request and cost more than a wildmask, which is enough for most rules.
Targetportlist may contain ports (X) or port ranges lists (X-Y). A port may Targetportlist may contain ports (X) or port ranges lists (X-Y). A port may
carry a service port after a slash, as X-Y/S or, for any destination, */S: carry the service the request came through after an @, as X-Y@S or, for any
the rule matches only requests that came through the service listening on destination, @S: the rule matches only requests that arrived through the
the port S — the port of the proxy or socks it connected to — whatever service S — the address it listens on, its port, or both, the address in
their destination. The limiters are shared by all the services, so this is brackets when IPv6 (@1080, @10.0.0.1, @10.0.0.1:1080, @[::1]:1080). The
how a limit is given to one service's connections alone, e.g. limiters are shared by all the services, so this is how a limit is given to
one service's connections alone, e.g.
.br .br
connlim 5 60 * * * */1080 connlim 5 60 * * * @1080
.br .br
denies the sixth concurrent connection of the service on port 1080, denies the sixth concurrent connection of the service on port 1080,
whatever it connects to. For any field * whatever it connects to. For any field *
+10 -2
View File
@@ -310,8 +310,16 @@ int ACLmatches(struct ace* acentry, struct clientparam * param){
} }
if(!ipentry && !hstentry) return 0; if(!ipentry && !hstentry) return 0;
} }
/* the service the request came through, by the port it listens on: /* the service the request came through, by the port it listens on
what the part after the slash of a port in a rule names */ and by its address: what the part after the @ of a port in a
rule names */
if(acentry->viasrc) {
for (ipentry = acentry->viasrc; ipentry; ipentry = ipentry->next)
if(IPInentry((struct sockaddr *)&param->srv->intsa, ipentry)) {
break;
}
if(!ipentry) return 0;
}
if(acentry->viaports) { if(acentry->viaports) {
int viaport = ntohs(*SAPORT(&param->srv->intsa)); int viaport = ntohs(*SAPORT(&param->srv->intsa));
+69 -10
View File
@@ -1399,6 +1399,59 @@ static int portadd(struct portlist **head, const char *s){
return 0; return 0;
} }
/* Appends an IP, an IP range or a CIDR to a list: 0, or 1 for one that
is none of them, or for want of memory. */
static int ipadd(struct iplist **head, const char *s){
struct iplist **tail, tmp;
for(tail = head; *tail; tail = &(*tail)->next);
memset(&tmp, 0, sizeof(tmp));
if(scanipl((unsigned char *)s, &tmp)) return 1;
if(!(*tail = malloc(sizeof(struct iplist)))) return 1;
**tail = tmp;
(*tail)->next = NULL;
return 0;
}
/* The part after the @ of a port in a rule: the service the request
came through, by its port, its address, or both, the address in
brackets when IPv6 — 1080, 10.0.0.1, 10.0.0.1:1080, [::1]:1080.
Digits alone, or with a dash, are a port or a port range; anything
else is an address, with a port behind a colon. 0, or 1 for what is
none of these. *ip and *port point into s, or stay NULL. */
static int viasplit(unsigned char *s, unsigned char **ip, unsigned char **port){
unsigned char *close;
*ip = *port = NULL;
if(!*s) return 1;
if(*s == '['){
close = (unsigned char *)strchr((char *)s, ']');
if(!close || close == s + 1) return 1;
*ip = s + 1;
*close = 0;
s = close + 1;
if(!*s) return 0;
if(*s != ':') return 1;
s++;
if(!*s) return 1;
*port = s;
return 0;
}
if(*s != '-' && strspn((char *)s, "0123456789-") == strlen((char *)s)){
*port = s;
return 0;
}
close = (unsigned char *)strchr((char *)s, ':');
if(close && isnumber(*(close+1))){
*close = 0;
*ip = s;
*port = close + 1;
return 0;
}
*ip = s;
return 0;
}
struct ace * make_ace (int argc, unsigned char ** argv){ struct ace * make_ace (int argc, unsigned char ** argv){
struct ace * acl; struct ace * acl;
unsigned char *arg; unsigned char *arg;
@@ -1498,16 +1551,10 @@ struct ace * make_ace (int argc, unsigned char ** argv){
if(argc > 3 && strcmp("*", (char *)argv[3])) { if(argc > 3 && strcmp("*", (char *)argv[3])) {
arg = (unsigned char *)strtok((char *)argv[3], ","); arg = (unsigned char *)strtok((char *)argv[3], ",");
if(arg) do { if(arg) do {
unsigned char *via = (unsigned char *)strchr((char *)arg, '/'); unsigned char *via = (unsigned char *)strchr((char *)arg, '@');
if(via) { if(via) *via++ = 0;
*via++ = 0; if(*arg && strcmp((char *)arg, "*")) {
if(!*via || !*arg) {
fprintf(stderr, "Invalid port or port range, line %d\n", linenum);
return(NULL);
}
}
if(strcmp((char *)arg, "*")) {
if(portadd(&acl->ports, (char *)arg)) { if(portadd(&acl->ports, (char *)arg)) {
fprintf(stderr, "Invalid port or port range, line %d\n", linenum); fprintf(stderr, "Invalid port or port range, line %d\n", linenum);
return(NULL); return(NULL);
@@ -1517,10 +1564,22 @@ struct ace * make_ace (int argc, unsigned char ** argv){
fprintf(stderr, "Invalid port or port range, line %d\n", linenum); fprintf(stderr, "Invalid port or port range, line %d\n", linenum);
return(NULL); return(NULL);
} }
if(via && portadd(&acl->viaports, (char *)via)) { if(via) {
unsigned char *ip, *port;
if(viasplit(via, &ip, &port)) {
fprintf(stderr, "Invalid port or port range, line %d\n", linenum); fprintf(stderr, "Invalid port or port range, line %d\n", linenum);
return(NULL); return(NULL);
} }
if(ip && ipadd(&acl->viasrc, (char *)ip)) {
fprintf(stderr, "Invalid IP, IP range or CIDR, line %d\n", linenum);
return(NULL);
}
if(port && portadd(&acl->viaports, (char *)port)) {
fprintf(stderr, "Invalid port or port range, line %d\n", linenum);
return(NULL);
}
}
} while((arg = (unsigned char *)strtok((char *)NULL, ","))); } while((arg = (unsigned char *)strtok((char *)NULL, ",")));
} }
if(argc > 4 && strcmp("*", (char *)argv[4])) { if(argc > 4 && strcmp("*", (char *)argv[4])) {
+6 -1
View File
@@ -426,6 +426,10 @@ static void * ef_ace_viaports(struct node * node){
return ((struct ace *)node->value) -> viaports; return ((struct ace *)node->value) -> viaports;
} }
static void * ef_ace_viasrc(struct node * node){
return ((struct ace *)node->value) -> viasrc;
}
static void * ef_ace_chain(struct node * node){ static void * ef_ace_chain(struct node * node){
return ((struct ace *)node->value) -> chains; return ((struct ace *)node->value) -> chains;
} }
@@ -861,7 +865,8 @@ static struct property prop_ace[] = {
{"dst", ef_ace_dst, TYPE_IPLIST, "list of destination ips"}, {"dst", ef_ace_dst, TYPE_IPLIST, "list of destination ips"},
{"dstnames", ef_ace_dstnames, TYPE_HOSTNAME, "list of destination hostnames"}, {"dstnames", ef_ace_dstnames, TYPE_HOSTNAME, "list of destination hostnames"},
{"ports", ef_ace_ports, TYPE_PORTLIST, "list of destination ports"}, {"ports", ef_ace_ports, TYPE_PORTLIST, "list of destination ports"},
{"viaports", ef_ace_viaports, TYPE_PORTLIST, "list of the services' ports it matches"}, {"viaports", ef_ace_viaports, TYPE_PORTLIST, "the services' ports it matches"},
{"viasrc", ef_ace_viasrc, TYPE_IPLIST, "the services' addresses it matches"},
{"chain", ef_ace_chain, TYPE_CHAIN, "redirect to parent(s)"}, {"chain", ef_ace_chain, TYPE_CHAIN, "redirect to parent(s)"},
{"wdays", ef_ace_weekdays, TYPE_WEEKDAYS, "days of week"}, {"wdays", ef_ace_weekdays, TYPE_WEEKDAYS, "days of week"},
{"periods", ef_ace_period, TYPE_PERIOD, "time of the day"}, {"periods", ef_ace_period, TYPE_PERIOD, "time of the day"},
+1
View File
@@ -1210,6 +1210,7 @@ void freeacl(struct ace *ac){
for(ipl = ac->dst; ipl; ipl = (struct iplist *)itfree(ipl,ipl->next)); for(ipl = ac->dst; ipl; ipl = (struct iplist *)itfree(ipl,ipl->next));
for(pl = ac->ports; pl; pl = (struct portlist *)itfree(pl, pl->next)); for(pl = ac->ports; pl; pl = (struct portlist *)itfree(pl, pl->next));
for(pl = ac->viaports; pl; pl = (struct portlist *)itfree(pl, pl->next)); for(pl = ac->viaports; pl; pl = (struct portlist *)itfree(pl, pl->next));
for(ipl = ac->viasrc; ipl; ipl = (struct iplist *)itfree(ipl, ipl->next));
for(pel = ac->periods; pel; pel = (struct period *)itfree(pel, pel->next)); for(pel = ac->periods; pel; pel = (struct period *)itfree(pel, pel->next));
for(ul = ac->users; ul; ul = (struct userlist *)itfree(ul, ul->next)){ for(ul = ac->users; ul; ul = (struct userlist *)itfree(ul, ul->next)){
if(ul->user)free(ul->user); if(ul->user)free(ul->user);
+1
View File
@@ -456,6 +456,7 @@ struct ace {
struct hostname *dstnames; struct hostname *dstnames;
struct portlist *ports; struct portlist *ports;
struct portlist *viaports; struct portlist *viaports;
struct iplist *viasrc;
struct chain *chains; struct chain *chains;
}; };
+30 -11
View File
@@ -1,9 +1,10 @@
"""A port in a rule may carry a service port after a slash. """A port in a rule may name the service the request came through.
The rules of the limiters are shared by all the services, so there has The rules of the limiters are shared by all the services, so there has
been no way to limit the connections of one service alone. X/S — or been no way to limit the connections of one service alone. After an @ a
*/S for any destination — matches only requests that came through port in a rule names that service — by its port, by the address it
the service listening on the port S: a deny so written closes one listens on, or by both: @1080, @10.0.0.1, @[::1]:1080 — and, with no
destination written, any destination. A deny so written closes one
service, a connlim so written caps one service, and neither touches service, a connlim so written caps one service, and neither touches
another service of the same configuration. another service of the same configuration.
""" """
@@ -24,12 +25,12 @@ def run(t):
""", ports=[origin]) """, ports=[origin])
url = f"http://127.0.0.1:{origin}/data" url = f"http://127.0.0.1:{origin}/data"
# a deny written with a service port closes that service alone # a deny naming a service by its port closes that service alone
first, second = t.free_port(), t.free_port() first, second = t.free_port(), t.free_port()
t.start("viaport_deny", f""" t.start("viaport_deny", f"""
log log
auth iponly auth iponly
deny * * * */{second} deny * * * @{second}
allow * allow *
proxy -p{first} proxy -p{first}
proxy -p{second} proxy -p{second}
@@ -40,12 +41,12 @@ def run(t):
t.ne(200, t.http(url, proxy=f"127.0.0.1:{second}").status, t.ne(200, t.http(url, proxy=f"127.0.0.1:{second}").status,
"the service the rule names by its port is refused") "the service the rule names by its port is refused")
# a connlim written with a service port caps that service alone # a connlim naming a service by its port caps that service alone
third, fourth = t.free_port(), t.free_port() third, fourth = t.free_port(), t.free_port()
t.start("viaport_connlim", f""" t.start("viaport_connlim", f"""
log log
auth iponly auth iponly
connlim 1 0 * * * */{third} connlim 1 0 * * * @{third}
allow * allow *
proxy -p{third} proxy -p{third}
proxy -p{fourth} proxy -p{fourth}
@@ -73,8 +74,26 @@ def run(t):
t.eq(200, result.get("status", 0), t.eq(200, result.get("status", 0),
"and the request the limit waited for finishes whole") "and the request the limit waited for finishes whole")
# a slash with nothing after it, or a bare * in a list, is refused # a service named by its address, and by its address and port
if t.has_ipv6():
fifth, sixth = t.free_port(), t.free_port()
t.start("viaport_ip", f"""
log
auth iponly
deny * * * @[::1]
allow *
proxy -p{fifth} -i127.0.0.1
proxy -p{sixth} -i::1
""", ports=[fifth, ("::1", sixth)])
t.eq(200, t.http(url, proxy=f"127.0.0.1:{fifth}").status,
"the service on another address is served")
r = t.http(url, proxy=f"[::1]:{sixth}")
t.ne(200, r.status if r else 0,
"the service the rule names by its address is refused")
# an @ with nothing after it is refused
out = t.run_config("viaport_bad", out = t.run_config("viaport_bad",
"log\nallow * * * 443/\nnot_a_command\n") "log\nallow * * * 443@\nnot_a_command\n")
t.contains(out, "Invalid port or port range", t.contains(out, "Invalid port or port range",
"a service port with no destination port is refused") "a service with nothing after the @ is refused")