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;
+163 -20
View File
@@ -289,26 +289,30 @@ static int bindtimeopt(const char *name){
!strcmp(name, "TCP_FASTOPEN");
}
/* Whether a socket with the options of l can be made one with those of srv:
/* Whether a socket with the options of l can be made one with those of b:
the same bind-time ones, with the same values; TCP_MAXSEG, whose default
is not 0, cannot be turned off again. */
static int sameopts(struct listener *l, struct srvparam *srv){
const struct sockoptset *a = &l->opts, *b = &srv->lissockopts;
static int sameoptset(struct listener *l, const struct sockoptset *b){
const struct sockoptset *a = &l->opts;
int i;
for(i = 0; sockopts[i].optname && i < MAXSOCKOPTS; i++){
unsigned bit = 1u << i;
for(i = 0; sockopts[i].optname && i < MAXSOCKOPTS; i++){
unsigned bit = 1u << i;
if(bindtimeopt(sockopts[i].optname)){
if((a->set & bit) != (b->set & bit)) return 0;
if((a->set & bit) && a->value[i] != b->value[i]) return 0;
}
if(bindtimeopt(sockopts[i].optname)){
if((a->set & bit) != (b->set & bit)) return 0;
if((a->set & bit) && a->value[i] != b->value[i]) return 0;
}
#ifdef TCP_MAXSEG
else if(sockopts[i].opt == TCP_MAXSEG && *sockopts[i].optname == 'T' &&
(a->set & bit) && !(b->set & bit)) return 0;
else if(sockopts[i].opt == TCP_MAXSEG && *sockopts[i].optname == 'T' &&
(a->set & bit) && !(b->set & bit)) return 0;
#endif
}
return 1;
}
return 1;
}
static int sameopts(struct listener *l, struct srvparam *srv){
return sameoptset(l, &srv->lissockopts);
}
/* Sets the options of srv on the socket of l taken over: those given, and
@@ -345,6 +349,32 @@ static int samelistener(struct listener *l, struct srvparam *srv, int isudp){
sameopts(l, srv);
}
/* samelistener without the options: the address, and what else makes a
socket of a place. */
static int samelistener_noopts(struct listener *l, struct srvparam *srv, int isudp){
if(l->isudp != isudp || l->cbdest) return 0;
#ifdef __linux__
if(!samestr(l->netns, srv->inetns)) return 0;
#endif
#if defined SO_BINDTODEVICE || defined IP_BOUND_IF
if(!samestr(l->device, srv->ibindtodevice)) return 0;
#endif
return SASIZE(&l->sa) == SASIZE(&srv->intsa) &&
!memcmp(&l->sa, &srv->intsa, SASIZE(&srv->intsa));
}
/* Whether a service takes over a reserved port: the socket is bound
already, so the options which act only before bind() cannot follow the
service's wishes — it must name them the same, or not name them at all.
The rest of its options are set on the socket when it is taken. */
/* Whether a service takes over a reserved port: the socket is bound
already, so it is what the reservation made of it — the options which
act only before bind() can follow neither's wishes but their author's,
and the service's other options are set on it when it is taken. */
static int samereservation(struct listener *l, struct srvparam *srv, int isudp){
return samelistener_noopts(l, srv, isudp);
}
/* Whether s goes on listener l: one connecting back (-r) on the
pseudo-listener of the place it connects to, any other on the socket for
its address. */
@@ -410,14 +440,24 @@ void mainclaim(void *service){
}
_3proxy_mutex_lock(&listener_mutex);
for(l = listeners; l; l = l->next){
if(!l->owners || !listenerfor(l, s)) continue;
if(listenerhas(l, srv->config)){
found = l;
break;
/* a reserved port: no service of it, its socket open; a service
of the address takes it over. A listener whose services are
gone, their last clients draining, has no socket to give */
if(!l->owners){
if(l->sock != INVALID_SOCKET && samereservation(l, srv, s->isudp)){
found = l;
break;
}
continue;
}
if(!listenerfor(l, s)) continue;
if(listenerhas(l, srv->config)){
found = l;
break;
}
if(!found || (l->owners->service == srv->service && found->owners->service != srv->service))
found = l;
}
if(!found || (l->owners->service == srv->service && found->owners->service != srv->service))
found = l;
}
if(!found && (found = newl)){
newl = NULL;
found->next = listeners;
@@ -502,6 +542,109 @@ static void slotrelease(struct srvparam *srv){
_3proxy_mutex_unlock(&srv->counter_mutex);
}
/* The port command: the listening socket opened here, at the time the
configuration is read, while the rights to open a privileged port may
still be had — before setuid and chroot. A service of the configuration
on the same address, with the same bind-time options, takes the socket
over when it starts; a reservation no service takes keeps the port until
the configuration is left. A reservation of the configuration before
already holds the port: its socket is shared, both reservations dropping
their hold with the configuration that made them. NULL if the socket
cannot be opened. */
void * mainreserve(int isudp, PROXYSOCKADDRTYPE *sa, const struct sockoptset *opts, int maxseg){
struct listener *l, *nl;
SOCKET sock;
if(!(nl = malloc(sizeof(*nl)))) return NULL;
memset(nl, 0, sizeof(*nl));
nl->isudp = isudp;
nl->sa = *sa;
nl->opts = *opts;
nl->maxseg = maxseg;
/* older than the configuration being read: a service taking the
socket over applies its own options to it, as one of a
configuration before does */
nl->optversion = conf->version - 1;
nl->sock = INVALID_SOCKET;
_3proxy_mutex_lock(&listener_mutex);
for(l = listeners; l; l = l->next){
if(l->isudp != isudp || l->cbdest || l->sock == INVALID_SOCKET) continue;
if(SASIZE(&l->sa) != SASIZE(sa) || memcmp(&l->sa, sa, SASIZE(sa))) continue;
if(!sameoptset(l, opts)) continue;
l->users++; /* this reservation's hold with it */
_3proxy_mutex_unlock(&listener_mutex);
free(nl);
return l;
}
sock = so._socket(so.state, SASOCK(sa), isudp? SOCK_DGRAM: SOCK_STREAM,
isudp? IPPROTO_UDP: IPPROTO_TCP);
if(sock == INVALID_SOCKET){
_3proxy_mutex_unlock(&listener_mutex);
free(nl);
return NULL;
}
setopts(sock, opts, maxseg);
#ifndef _WIN32
fcntl(sock, F_SETFL, O_NONBLOCK | fcntl(sock, F_GETFL));
#else
{
unsigned long ul = 1;
ioctlsocket(sock, FIONBIO, &ul);
}
#endif
if(so._bind(so.state, sock, (struct sockaddr *)sa, SASIZE(sa)) == -1 ||
(!isudp && so._listen(so.state, sock, 1+(conf->maxchild>>3)) == -1)){
so._closesocket(so.state, sock);
_3proxy_mutex_unlock(&listener_mutex);
free(nl);
return NULL;
}
nl->sock = sock;
nl->users = 1; /* the reservation's hold */
_3proxy_mutex_init(&nl->open_mutex);
_3proxy_mutex_init(&nl->count_mutex);
nl->next = listeners;
listeners = nl;
_3proxy_mutex_unlock(&listener_mutex);
return nl;
}
/* The configuration which made the reservation is left: its hold on the
listener is given back, the socket closed and the listener freed by
whoever lets go of it last, a reservation or the clients of a service. */
void maindropreserved(void *v){
struct listener *l = (struct listener *)v, **lp;
SOCKET sock = INVALID_SOCKET;
struct udpsession *hand = NULL;
int last;
_3proxy_mutex_lock(&listener_mutex);
last = !--l->users;
if(last) for(lp = &listeners; *lp; lp = &(*lp)->next) if(*lp == l){
*lp = l->next;
sock = l->sock;
l->sock = INVALID_SOCKET;
hand = l->udphand;
l->udphand = NULL;
break;
}
_3proxy_mutex_unlock(&listener_mutex);
#ifndef NOUDPMAIN
udpendall(hand, 26);
#endif
if(sock != INVALID_SOCKET) so._closesocket(so.state, sock);
if(!last) return;
#ifdef WITH_UN
ununlink(&l->sa, l->undev, l->unino);
#endif
_3proxy_mutex_destroy(&l->open_mutex);
_3proxy_mutex_destroy(&l->count_mutex);
if(l->netns) free(l->netns);
if(l->device) free(l->device);
if(l->cbdest) free(l->cbdest);
free(l);
}
/* After a reload, unless noforce: a client goes by the rules of a newest
service of its type on its listener, now accepting there, re-authenticated
as it if auth is set. It takes one of that service's maxconn slots,
+2
View File
@@ -630,6 +630,8 @@ int getopts(const char *s, struct sockoptset *opts);
void setopts(SOCKET s, const struct sockoptset *opts, int maxseg);
char * printopts(char *sep);
char * printfeatures(char *sep);
void * mainreserve(int isudp, PROXYSOCKADDRTYPE *sa, const struct sockoptset *opts, int maxseg);
void maindropreserved(void *listener);
#ifdef _WINCE
char * CEToUnicode (const char *str);
+11 -3
View File
@@ -965,9 +965,16 @@ struct threadpool {
extern struct threadpool tpool;
struct filemon {
char * path;
struct stat sb;
struct filemon *next;
char * path;
struct stat sb;
struct filemon *next;
};
/* A port the configuration reserved with the port command: its listener,
opaque here, held until the configuration is left */
struct portreservation {
struct portreservation *next;
void *listener;
};
@@ -1022,6 +1029,7 @@ struct extparam {
int refs;
struct hashtable pwl_table; /* users */
int resolvmode; /* RESOLVE_*: the last read, each service taking the one read before it */
struct portreservation *reservations; /* the port command's, dropped with the configuration */
};
struct property {