2 * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/types.h>
18 #include <sys/queue.h>
19 #include <sys/socket.h>
36 #include "got_error.h"
44 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
47 struct gotd_listen_client {
48 STAILQ_ENTRY(gotd_listen_client) entry;
53 STAILQ_HEAD(gotd_listen_clients, gotd_listen_client);
55 static struct gotd_listen_clients gotd_listen_clients[GOTD_CLIENT_TABLE_SIZE];
56 static SIPHASH_KEY clients_hash_key;
57 static volatile int listen_client_cnt;
60 struct gotd_uid_connection_counter {
61 STAILQ_ENTRY(gotd_uid_connection_counter) entry;
65 STAILQ_HEAD(gotd_client_uids, gotd_uid_connection_counter);
66 static struct gotd_client_uids gotd_client_uids[GOTD_CLIENT_TABLE_SIZE];
67 static SIPHASH_KEY uid_hash_key;
73 struct gotd_imsgev iev;
74 struct gotd_imsgev pause;
75 struct gotd_uid_connection_limit *connection_limits;
76 size_t nconnection_limits;
81 static void listen_shutdown(void);
84 listen_sighdlr(int sig, short event, void *arg)
87 * Normal signal handler rules don't apply because libevent
102 fatalx("unexpected signal");
107 client_hash(uint32_t client_id)
109 return SipHash24(&clients_hash_key, &client_id, sizeof(client_id));
113 add_client(struct gotd_listen_client *client)
115 uint64_t slot = client_hash(client->id) % nitems(gotd_listen_clients);
116 STAILQ_INSERT_HEAD(&gotd_listen_clients[slot], client, entry);
120 static struct gotd_listen_client *
121 find_client(uint32_t client_id)
124 struct gotd_listen_client *c;
126 slot = client_hash(client_id) % nitems(gotd_listen_clients);
127 STAILQ_FOREACH(c, &gotd_listen_clients[slot], entry) {
128 if (c->id == client_id)
143 duplicate = (find_client(id) != NULL);
144 } while (duplicate || id == 0);
152 return SipHash24(&uid_hash_key, &euid, sizeof(euid));
156 add_uid_connection_counter(struct gotd_uid_connection_counter *counter)
158 uint64_t slot = uid_hash(counter->euid) % nitems(gotd_client_uids);
159 STAILQ_INSERT_HEAD(&gotd_client_uids[slot], counter, entry);
163 remove_uid_connection_counter(struct gotd_uid_connection_counter *counter)
165 uint64_t slot = uid_hash(counter->euid) % nitems(gotd_client_uids);
166 STAILQ_REMOVE(&gotd_client_uids[slot], counter,
167 gotd_uid_connection_counter, entry);
170 static struct gotd_uid_connection_counter *
171 find_uid_connection_counter(uid_t euid)
174 struct gotd_uid_connection_counter *c;
176 slot = uid_hash(euid) % nitems(gotd_client_uids);
177 STAILQ_FOREACH(c, &gotd_client_uids[slot], entry) {
185 static const struct got_error *
186 disconnect(struct gotd_listen_client *client)
188 struct gotd_uid_connection_counter *counter;
192 log_debug("client on fd %d disconnecting", client->fd);
194 slot = client_hash(client->id) % nitems(gotd_listen_clients);
195 STAILQ_REMOVE(&gotd_listen_clients[slot], client,
196 gotd_listen_client, entry);
198 counter = find_uid_connection_counter(client->euid);
200 if (counter->nconnections > 0)
201 counter->nconnections--;
202 if (counter->nconnections == 0) {
203 remove_uid_connection_counter(counter);
208 client_fd = client->fd;
212 if (close(client_fd) == -1)
213 return got_error_from_errno("close");
219 accept_reserve(int fd, struct sockaddr *addr, socklen_t *addrlen,
220 int reserve, volatile int *counter)
224 if (getdtablecount() + reserve +
225 ((*counter + 1) * GOTD_FD_NEEDED) >= getdtablesize()) {
226 log_debug("inflight fds exceeded");
231 if ((ret = accept4(fd, addr, addrlen,
232 SOCK_NONBLOCK | SOCK_CLOEXEC)) > -1) {
240 gotd_accept_paused(int fd, short event, void *arg)
242 event_add(&gotd_listen.iev.ev, NULL);
246 gotd_accept(int fd, short event, void *arg)
248 struct gotd_imsgev *iev = arg;
249 struct sockaddr_storage ss;
250 struct timeval backoff;
253 struct gotd_listen_client *client = NULL;
254 struct gotd_uid_connection_counter *counter = NULL;
255 struct gotd_imsg_connect iconn;
262 if (event_add(&gotd_listen.iev.ev, NULL) == -1) {
263 log_warn("event_add");
266 if (event & EV_TIMEOUT)
271 /* Other backoff conditions apart from EMFILE/ENFILE? */
272 s = accept_reserve(fd, (struct sockaddr *)&ss, &len, GOTD_FD_RESERVE,
282 event_del(&gotd_listen.iev.ev);
283 evtimer_add(&gotd_listen.pause.ev, &backoff);
291 if (listen_client_cnt >= GOTD_MAXCLIENTS)
294 if (getpeereid(s, &euid, &egid) == -1) {
295 log_warn("getpeerid");
299 counter = find_uid_connection_counter(euid);
300 if (counter == NULL) {
301 counter = calloc(1, sizeof(*counter));
302 if (counter == NULL) {
303 log_warn("%s: calloc", __func__);
306 counter->euid = euid;
307 counter->nconnections = 1;
308 add_uid_connection_counter(counter);
310 int max_connections = GOTD_MAX_CONN_PER_UID;
311 struct gotd_uid_connection_limit *limit;
313 limit = gotd_find_uid_connection_limit(
314 gotd_listen.connection_limits,
315 gotd_listen.nconnection_limits, euid);
317 max_connections = limit->max_connections;
319 if (counter->nconnections >= max_connections) {
320 log_warnx("maximum connections exceeded for uid %d",
324 counter->nconnections++;
327 client = calloc(1, sizeof(*client));
328 if (client == NULL) {
329 log_warn("%s: calloc", __func__);
332 client->id = get_client_id();
337 log_debug("%s: new client connected on fd %d uid %d gid %d", __func__,
338 client->fd, euid, egid);
340 memset(&iconn, 0, sizeof(iconn));
341 iconn.client_id = client->id;
346 log_warn("%s: dup", __func__);
349 if (gotd_imsg_compose_event(iev, GOTD_IMSG_CONNECT, PROC_LISTEN, s,
350 &iconn, sizeof(iconn)) == -1) {
351 log_warn("imsg compose CONNECT");
364 static const struct got_error *
365 recv_disconnect(struct imsg *imsg)
367 struct gotd_imsg_disconnect idisconnect;
369 struct gotd_listen_client *client = NULL;
371 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
372 if (datalen != sizeof(idisconnect))
373 return got_error(GOT_ERR_PRIVSEP_LEN);
374 memcpy(&idisconnect, imsg->data, sizeof(idisconnect));
376 log_debug("client disconnecting");
378 client = find_client(idisconnect.client_id);
380 return got_error(GOT_ERR_CLIENT_ID);
382 return disconnect(client);
386 listen_dispatch(int fd, short event, void *arg)
388 const struct got_error *err = NULL;
389 struct gotd_imsgev *iev = arg;
390 struct imsgbuf *ibuf = &iev->ibuf;
395 if (event & EV_READ) {
396 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
397 fatal("imsg_read error");
398 if (n == 0) /* Connection closed. */
402 if (event & EV_WRITE) {
403 n = msgbuf_write(&ibuf->w);
404 if (n == -1 && errno != EAGAIN)
405 fatal("msgbuf_write");
406 if (n == 0) /* Connection closed. */
411 if ((n = imsg_get(ibuf, &imsg)) == -1)
412 fatal("%s: imsg_get", __func__);
413 if (n == 0) /* No more messages. */
416 switch (imsg.hdr.type) {
417 case GOTD_IMSG_DISCONNECT:
418 err = recv_disconnect(&imsg);
420 log_warnx("disconnect: %s", err->msg);
423 log_debug("unexpected imsg %d", imsg.hdr.type);
431 gotd_imsg_event_add(iev);
433 /* This pipe is dead. Remove its event handler */
435 event_loopexit(NULL);
440 listen_main(const char *title, int gotd_socket,
441 struct gotd_uid_connection_limit *connection_limits,
442 size_t nconnection_limits)
444 struct gotd_imsgev iev;
445 struct event evsigint, evsigterm, evsighup, evsigusr1;
447 arc4random_buf(&clients_hash_key, sizeof(clients_hash_key));
448 arc4random_buf(&uid_hash_key, sizeof(uid_hash_key));
450 gotd_listen.title = title;
451 gotd_listen.pid = getpid();
452 gotd_listen.fd = gotd_socket;
453 gotd_listen.connection_limits = connection_limits;
454 gotd_listen.nconnection_limits = nconnection_limits;
456 signal_set(&evsigint, SIGINT, listen_sighdlr, NULL);
457 signal_set(&evsigterm, SIGTERM, listen_sighdlr, NULL);
458 signal_set(&evsighup, SIGHUP, listen_sighdlr, NULL);
459 signal_set(&evsigusr1, SIGUSR1, listen_sighdlr, NULL);
460 signal(SIGPIPE, SIG_IGN);
462 signal_add(&evsigint, NULL);
463 signal_add(&evsigterm, NULL);
464 signal_add(&evsighup, NULL);
465 signal_add(&evsigusr1, NULL);
467 imsg_init(&iev.ibuf, GOTD_FILENO_MSG_PIPE);
468 iev.handler = listen_dispatch;
469 iev.events = EV_READ;
470 iev.handler_arg = NULL;
471 event_set(&iev.ev, iev.ibuf.fd, EV_READ, listen_dispatch, &iev);
472 if (event_add(&iev.ev, NULL) == -1)
475 event_set(&gotd_listen.iev.ev, gotd_listen.fd, EV_READ | EV_PERSIST,
477 if (event_add(&gotd_listen.iev.ev, NULL))
479 evtimer_set(&gotd_listen.pause.ev, gotd_accept_paused, NULL);
487 listen_shutdown(void)
489 log_debug("shutting down");
491 free(gotd_listen.connection_limits);
492 if (gotd_listen.fd != -1)
493 close(gotd_listen.fd);