port: a listening socket reserved while the configuration is read

port tcp|udp [ip:]port [socket_options] opens, binds and listens at
the time the line is read, so a privileged port can be taken before
setuid and chroot drop the rights to open it: a service of the
configuration on the same address, of the same type, takes the socket
over when it starts, instead of binding one of its own, and a port no
service takes stays reserved until the configuration is left. A
reservation of the configuration before holds the port over a reload:
the new one shares its socket, each dropping its hold with its
configuration.

The socket is what the reservation made of it: the options which act
only before bind() cannot follow a service's wishes on a socket already
bound, so the service takes it as it is, its other options set on it.
The reservation is built from the same default address a service is, so
it matches in every byte a listener is compared by.
This commit is contained in:
Vladimir Dubrovin committed 2026-11-21 16:12:40 +03:00
1 parent 37dfb863fb
commit 17306f63f9
6 files changed
+316 -23

No files matched your search

+46
View File
@@ -439,6 +439,43 @@ static int h_threadpool(int argc, unsigned char **argv){
return 0;
}
/* Reserves a listening socket while the configuration is read: a
privileged port is opened before setuid and chroot take the rights
away, and a service of the configuration on the same address takes
the socket over when it starts. */
static int h_port(int argc, unsigned char **argv){
struct portreservation *r;
PROXYSOCKADDRTYPE sa;
struct sockoptset opts;
int isudp;
if(!strcmp((char *)argv[1], "udp")) isudp = 1;
else if(!strcmp((char *)argv[1], "tcp")) isudp = 0;
else return 1;
memset(&opts, 0, sizeof(opts));
if(argc > 3 && getopts((char *)argv[3], &opts)) return 2;
/* as a service's address: from the defaults, so the same in every
byte a listener is matched by */
sa = conf->intsa;
if(strchr((char *)argv[2], ':') || *argv[2] == '['){
if(parsehost(46, argv[2], (struct sockaddr *)&sa)) return 3;
}
else {
*SAFAMILY(&sa) = AF_INET;
*SAPORT(&sa) = htons((uint16_t)atoi((char *)argv[2]));
}
if(!*SAPORT(&sa)) return 3;
if(!(r = malloc(sizeof(*r)))) return 4;
if(!(r->listener = mainreserve(isudp, &sa, &opts, 0))){
free(r);
fprintf(stderr, "Unable to reserve port, line %d\n", linenum);
return 5;
}
r->next = conf->reservations;
conf->reservations = r;
return 0;
}
static int h_force(int argc, unsigned char **argv){
conf->noforce = 0;
@@ -2146,6 +2183,7 @@ struct commands commandhandlers[]={
{NULL, "minlogqueue", h_minlogqueue, 2, 2},
{NULL, "maxlogqueue", h_maxlogqueue, 2, 2},
{NULL, "threadpool", h_threadpool, 2, 3},
{NULL, "port", h_port, 3, 4},
{NULL, "force", h_force, 1, 1},
{NULL, "noforce", h_noforce, 1, 1},
{NULL, "parentretries", h_parentretries, 2, 2},
@@ -2414,11 +2452,19 @@ void freeconf(struct extparam *confp){
struct trafcount * tc;
struct ace *acl;
struct filemon *fm;
struct portreservation *pr;
int counterd, archiverc;
unsigned char **archiver;
unsigned char * logformat;
int i;
while(confp->reservations){
pr = confp->reservations;
confp->reservations = pr->next;
maindropreserved(pr->listener);
free(pr);
}
_3proxy_mutex_lock(&tc_mutex);
confp->trafcountfunc = NULL;
tc = confp->trafcounter;