diff --git a/man/3proxy.cfg.5 b/man/3proxy.cfg.5 index 52a2ccf..f620cdb 100644 --- a/man/3proxy.cfg.5 +++ b/man/3proxy.cfg.5 @@ -937,13 +937,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 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 -carry a service port after a slash, as X-Y/S or, for any destination, */S: -the rule matches only requests that came through the service listening on -the port S — the port of the proxy or socks it connected to — whatever -their destination. The limiters are shared by all the services, so this is -how a limit is given to one service's connections alone, e.g. +carry the service the request came through after an @, as X-Y@S or, for any +destination, @S: the rule matches only requests that arrived through the +service S — the address it listens on, its port, or both, the address in +brackets when IPv6 (@1080, @10.0.0.1, @10.0.0.1:1080, @[::1]:1080). The +limiters are shared by all the services, so this is how a limit is given to +one service's connections alone, e.g. .br - connlim 5 60 * * * */1080 + connlim 5 60 * * * @1080 .br denies the sixth concurrent connection of the service on port 1080, whatever it connects to. For any field * diff --git a/src/acl.c b/src/acl.c index 1617a91..415f718 100644 --- a/src/acl.c +++ b/src/acl.c @@ -310,8 +310,16 @@ int ACLmatches(struct ace* acentry, struct clientparam * param){ } if(!ipentry && !hstentry) return 0; } - /* 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 */ + /* the service the request came through, by the port it listens on + 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 *)¶m->srv->intsa, ipentry)) { + break; + } + if(!ipentry) return 0; + } if(acentry->viaports) { int viaport = ntohs(*SAPORT(¶m->srv->intsa)); diff --git a/src/conf.c b/src/conf.c index b4fd847..37b1ce5 100644 --- a/src/conf.c +++ b/src/conf.c @@ -1402,6 +1402,59 @@ static int portadd(struct portlist **head, const char *s){ 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 * acl; unsigned char *arg; @@ -1501,16 +1554,10 @@ struct ace * make_ace (int argc, unsigned char ** argv){ if(argc > 3 && strcmp("*", (char *)argv[3])) { arg = (unsigned char *)strtok((char *)argv[3], ","); if(arg) do { - unsigned char *via = (unsigned char *)strchr((char *)arg, '/'); + unsigned char *via = (unsigned char *)strchr((char *)arg, '@'); - if(via) { - *via++ = 0; - if(!*via || !*arg) { - fprintf(stderr, "Invalid port or port range, line %d\n", linenum); - return(NULL); - } - } - if(strcmp((char *)arg, "*")) { + if(via) *via++ = 0; + if(*arg && strcmp((char *)arg, "*")) { if(portadd(&acl->ports, (char *)arg)) { fprintf(stderr, "Invalid port or port range, line %d\n", linenum); return(NULL); @@ -1520,9 +1567,21 @@ struct ace * make_ace (int argc, unsigned char ** argv){ fprintf(stderr, "Invalid port or port range, line %d\n", linenum); return(NULL); } - if(via && portadd(&acl->viaports, (char *)via)) { - fprintf(stderr, "Invalid port or port range, line %d\n", linenum); - return(NULL); + if(via) { + unsigned char *ip, *port; + + if(viasplit(via, &ip, &port)) { + fprintf(stderr, "Invalid port or port range, line %d\n", linenum); + 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, ","))); } diff --git a/src/datatypes.c b/src/datatypes.c index dbfd49f..e0e85e5 100644 --- a/src/datatypes.c +++ b/src/datatypes.c @@ -422,6 +422,10 @@ static void * ef_ace_viaports(struct node * node){ 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){ return ((struct ace *)node->value) -> chains; } @@ -855,7 +859,8 @@ static struct property prop_ace[] = { {"dst", ef_ace_dst, TYPE_IPLIST, "list of destination ips"}, {"dstnames", ef_ace_dstnames, TYPE_HOSTNAME, "list of destination hostnames"}, {"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)"}, {"wdays", ef_ace_weekdays, TYPE_WEEKDAYS, "days of week"}, {"periods", ef_ace_period, TYPE_PERIOD, "time of the day"}, diff --git a/src/mainconf.c b/src/mainconf.c index 2f27e1e..811ecc3 100644 --- a/src/mainconf.c +++ b/src/mainconf.c @@ -1222,6 +1222,7 @@ void freeacl(struct ace *ac){ 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->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(ul = ac->users; ul; ul = (struct userlist *)itfree(ul, ul->next)){ if(ul->user)free(ul->user); diff --git a/src/structures.h b/src/structures.h index f18b73c..6018004 100644 --- a/src/structures.h +++ b/src/structures.h @@ -414,6 +414,7 @@ struct ace { struct hostname *dstnames; struct portlist *ports; struct portlist *viaports; + struct iplist *viasrc; struct chain *chains; }; diff --git a/tests/cases/viaport.py b/tests/cases/viaport.py index 54c2e21..4c6305b 100644 --- a/tests/cases/viaport.py +++ b/tests/cases/viaport.py @@ -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 -been no way to limit the connections of one service alone. X/S — or -*/S for any destination — matches only requests that came through -the service listening on the port S: a deny so written closes one +been no way to limit the connections of one service alone. After an @ a +port in a rule names that service — by its port, by the address it +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 another service of the same configuration. """ @@ -24,12 +25,12 @@ def run(t): """, ports=[origin]) 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() t.start("viaport_deny", f""" log auth iponly - deny * * * */{second} + deny * * * @{second} allow * proxy -p{first} proxy -p{second} @@ -40,12 +41,12 @@ def run(t): t.ne(200, t.http(url, proxy=f"127.0.0.1:{second}").status, "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() t.start("viaport_connlim", f""" log auth iponly - connlim 1 0 * * * */{third} + connlim 1 0 * * * @{third} allow * proxy -p{third} proxy -p{fourth} @@ -73,8 +74,26 @@ def run(t): t.eq(200, result.get("status", 0), "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", - "log\nallow * * * 443/\nnot_a_command\n") + "log\nallow * * * 443@\nnot_a_command\n") 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")