diff --git a/man/3proxy.cfg.5 b/man/3proxy.cfg.5 index 5f6b931..75544fe 100644 --- a/man/3proxy.cfg.5 +++ b/man/3proxy.cfg.5 @@ -936,20 +936,26 @@ quoted or written \fB$$\fR, since a lone dollar outside quotes begins the name 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 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. +Targetlist and targetportlist may name, after one @, the service the +request came through: the targetlist by the addresses it listens on, +the targetportlist by its ports, the lists before and after the @ each +written as any other — + +.br + allow * * 192.168.1.0/24@10.0.0.1 443@1080,1090 +.br + +matches only requests of the service on 10.0.0.1:1080 or :1090. A side +of the @ may be left out: * (any) before it, or nothing after it — +@1080 names a port alone, *@10.0.0.1 an address alone, and an @ with +nothing after it is refused. An IPv6 address needs no brackets there, +for no port follows it. The limiters are shared by all the services, +so this is how a limit is given to one service's connections alone: .br connlim 5 60 * * * @1080 .br denies the sixth concurrent connection of the service on port 1080, -whatever it connects to. A comma ends an entry, so several services are -listed by repeating it: *@1080,*@1090 names the two, and a bare -@1080,1090 would leave 1090 a destination port. For any field * +whatever it connects to. For any field * sign means ANY. If access list is empty it\'s assumed to be .br allow * diff --git a/src/conf.c b/src/conf.c index 37b1ce5..9d9285b 100644 --- a/src/conf.c +++ b/src/conf.c @@ -1416,53 +1416,27 @@ static int ipadd(struct iplist **head, const char *s){ 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; +/* The part of a field after its one @: the service the request came + through, named by the addresses it listens on in the targetlist and + by its ports in the targetportlist, the lists written as any other. */ +static unsigned char * viapart(unsigned char *field, int argi, int argc){ - *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(argi < argc){ + unsigned char *via = (unsigned char *)strchr((char *)field, '@'); + + if(via) *via++ = 0; + return via; } - 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; + return NULL; } struct ace * make_ace (int argc, unsigned char ** argv){ struct ace * acl; - unsigned char *arg; - struct iplist *ipl=NULL; - struct portlist *portl=NULL; + unsigned char *arg, *via; struct userlist *userl=NULL; struct hostname *hostnamel=NULL; - int res; + struct iplist tmpip={NULL}; + int arglen; acl = malloc(sizeof(struct ace)); if(!acl) return acl; @@ -1490,30 +1464,16 @@ struct ace * make_ace (int argc, unsigned char ** argv){ if(argc > 1 && strcmp("*", (char *)argv[1])) { arg = (unsigned char *)strtok((char *)argv[1], ","); if(arg) do { - if(!acl->src) { - acl->src = ipl = malloc(sizeof(struct iplist)); - } - else { - ipl->next = malloc(sizeof(struct iplist)); - ipl = ipl -> next; - } - if(!ipl) { - fprintf(stderr, "No memory for ACL entry, line %d\n", linenum); - return(NULL); - } - memset(ipl, 0, sizeof(struct iplist)); - if (scanipl(arg, ipl)) { + if(ipadd(&acl->src, (char *)arg)) { fprintf(stderr, "Invalid IP, IP range or CIDR, line %d\n", linenum); return(NULL); } } while((arg = (unsigned char *)strtok((char *)NULL, ","))); } + via = viapart(argv[2], 2, argc); if(argc > 2 && strcmp("*", (char *)argv[2])) { arg = (unsigned char *)strtok((char *)argv[2], ","); if(arg) do { - int arglen; - struct iplist tmpip={NULL}; - arglen = (int)strlen((char *)arg); if(scanipl(arg, &tmpip)){ if(!arglen) continue; @@ -1535,53 +1495,44 @@ struct ace * make_ace (int argc, unsigned char ** argv){ } } else { - - if(!acl->dst) { - acl->dst = ipl = malloc(sizeof(struct iplist)); - } - else { - ipl->next = malloc(sizeof(struct iplist)); - ipl = ipl -> next; - } - if(!ipl) { + if(ipadd(&acl->dst, (char *)arg)) { fprintf(stderr, "No memory for ACL entry, line %d\n", linenum); return(NULL); } - *ipl = tmpip; } }while((arg = (unsigned char *)strtok((char *)NULL, ","))); } + if(via){ + if(!*via || !(arg = (unsigned char *)strtok((char *)via, ","))) { + fprintf(stderr, "Invalid IP, IP range or CIDR, line %d\n", linenum); + return(NULL); + } + do { + if(ipadd(&acl->viasrc, (char *)arg)) { + fprintf(stderr, "Invalid IP, IP range or CIDR, line %d\n", linenum); + return(NULL); + } + } while((arg = (unsigned char *)strtok((char *)NULL, ","))); + } + via = viapart(argv[3], 3, argc); 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, '@'); - - 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); - } - } - else if(!via) { + if(portadd(&acl->ports, (char *)arg)) { 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, ","))); + } + if(via){ + if(!*via || !(arg = (unsigned char *)strtok((char *)via, ","))) { + fprintf(stderr, "Invalid port or port range, line %d\n", linenum); + return(NULL); + } + do { + if(portadd(&acl->viaports, (char *)arg)) { + fprintf(stderr, "Invalid port or port range, line %d\n", linenum); + return(NULL); } } while((arg = (unsigned char *)strtok((char *)NULL, ","))); } diff --git a/tests/cases/viaport.py b/tests/cases/viaport.py index 0a1e037..fd4af9a 100644 --- a/tests/cases/viaport.py +++ b/tests/cases/viaport.py @@ -1,10 +1,10 @@ -"""A port in a rule may name the service the request came through. +"""A rule names, after one @, 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. 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 +been no way to limit the connections of one service alone. The +targetlist names the service after its @ by the addresses it listens +on, the targetportlist by its ports — each side written as any other +list, a side left out standing for any. A deny so written closes one service, a connlim so written caps one service, and neither touches another service of the same configuration. """ @@ -74,13 +74,13 @@ def run(t): t.eq(200, result.get("status", 0), "and the request the limit waited for finishes whole") - # a service named by its address, and by its address and port + # a service named by its address, in the targetlist if t.has_ipv6(): fifth, sixth = t.free_port(), t.free_port() t.start("viaport_ip", f""" log auth iponly - deny * * * @[::1] + deny * * *@::1 * allow * proxy -p{fifth} -i127.0.0.1 proxy -p{sixth} -i::1 @@ -92,24 +92,24 @@ def run(t): 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") - t.contains(out, "Invalid port or port range", - "a service with nothing after the @ is refused") - - # several services in one rule: the entry repeated with each + # several services named by one rule: the list after the @ sev1, sev2 = t.free_port(), t.free_port() t.start("viaport_both", f""" log auth iponly - deny * * * @{sev1},@{sev2} + deny * * * @{sev1},{sev2} allow * proxy -p{sev1} proxy -p{sev2} """, ports=[sev1, sev2]) t.ne(200, t.http(url, proxy=f"127.0.0.1:{sev1}").status, - "the first service a repeated entry names is refused") + "the first service the rule names is refused") t.ne(200, t.http(url, proxy=f"127.0.0.1:{sev2}").status, "and so is the second") + + # an @ with nothing after it is refused + out = t.run_config("viaport_bad", + "log\nallow * * * 443@\nnot_a_command\n") + t.contains(out, "Invalid port or port range", + "a service with nothing after the @ is refused")