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>
35 #include "got_error.h"
42 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
45 struct gotd_listen_client {
46 STAILQ_ENTRY(gotd_listen_client) entry;
51 STAILQ_HEAD(gotd_listen_clients, gotd_listen_client);
53 static struct gotd_listen_clients gotd_listen_clients[GOTD_CLIENT_TABLE_SIZE];
54 static SIPHASH_KEY clients_hash_key;
55 static volatile int listen_client_cnt;
58 struct gotd_uid_connection_counter {
59 STAILQ_ENTRY(gotd_uid_connection_counter) entry;
63 STAILQ_HEAD(gotd_client_uids, gotd_uid_connection_counter);
64 static struct gotd_client_uids gotd_client_uids[GOTD_CLIENT_TABLE_SIZE];
65 static SIPHASH_KEY uid_hash_key;
71 struct gotd_imsgev iev;
72 struct gotd_imsgev pause;
77 static void listen_shutdown(void);
80 listen_sighdlr(int sig, short event, void *arg)
83 * Normal signal handler rules don't apply because libevent
98 fatalx("unexpected signal");
103 client_hash(uint32_t client_id)
105 return SipHash24(&clients_hash_key, &client_id, sizeof(client_id));
109 add_client(struct gotd_listen_client *client)
111 uint64_t slot = client_hash(client->id) % nitems(gotd_listen_clients);
112 STAILQ_INSERT_HEAD(&gotd_listen_clients[slot], client, entry);
116 static struct gotd_listen_client *
117 find_client(uint32_t client_id)
120 struct gotd_listen_client *c;
122 slot = client_hash(client_id) % nitems(gotd_listen_clients);
123 STAILQ_FOREACH(c, &gotd_listen_clients[slot], entry) {
124 if (c->id == client_id)
139 duplicate = (find_client(id) != NULL);
140 } while (duplicate || id == 0);
148 return SipHash24(&uid_hash_key, &euid, sizeof(euid));
152 add_uid_connection_counter(struct gotd_uid_connection_counter *counter)
154 uint64_t slot = uid_hash(counter->euid) % nitems(gotd_client_uids);
155 STAILQ_INSERT_HEAD(&gotd_client_uids[slot], counter, entry);
159 remove_uid_connection_counter(struct gotd_uid_connection_counter *counter)
161 uint64_t slot = uid_hash(counter->euid) % nitems(gotd_client_uids);
162 STAILQ_REMOVE(&gotd_client_uids[slot], counter,
163 gotd_uid_connection_counter, entry);
166 static struct gotd_uid_connection_counter *
167 find_uid_connection_counter(uid_t euid)
170 struct gotd_uid_connection_counter *c;
172 slot = uid_hash(euid) % nitems(gotd_client_uids);
173 STAILQ_FOREACH(c, &gotd_client_uids[slot], entry) {
181 static const struct got_error *
182 disconnect(struct gotd_listen_client *client)
184 struct gotd_uid_connection_counter *counter;
188 log_debug("client on fd %d disconnecting", client->fd);
190 slot = client_hash(client->id) % nitems(gotd_listen_clients);
191 STAILQ_REMOVE(&gotd_listen_clients[slot], client,
192 gotd_listen_client, entry);
194 counter = find_uid_connection_counter(client->euid);
196 if (counter->nconnections > 0)
197 counter->nconnections--;
198 if (counter->nconnections == 0) {
199 remove_uid_connection_counter(counter);
204 client_fd = client->fd;
208 if (close(client_fd) == -1)
209 return got_error_from_errno("close");
215 accept_reserve(int fd, struct sockaddr *addr, socklen_t *addrlen,
216 int reserve, volatile int *counter)
220 if (getdtablecount() + reserve +
221 ((*counter + 1) * GOTD_FD_NEEDED) >= getdtablesize()) {
222 log_debug("inflight fds exceeded");
227 if ((ret = accept4(fd, addr, addrlen,
228 SOCK_NONBLOCK | SOCK_CLOEXEC)) > -1) {
236 gotd_accept_paused(int fd, short event, void *arg)
238 event_add(&gotd_listen.iev.ev, NULL);
242 gotd_accept(int fd, short event, void *arg)
244 struct gotd_imsgev *iev = arg;
245 struct sockaddr_storage ss;
246 struct timeval backoff;
249 struct gotd_listen_client *client = NULL;
250 struct gotd_uid_connection_counter *counter = NULL;
251 struct gotd_imsg_connect iconn;
258 if (event_add(&gotd_listen.iev.ev, NULL) == -1) {
259 log_warn("event_add");
262 if (event & EV_TIMEOUT)
267 /* Other backoff conditions apart from EMFILE/ENFILE? */
268 s = accept_reserve(fd, (struct sockaddr *)&ss, &len, GOTD_FD_RESERVE,
278 event_del(&gotd_listen.iev.ev);
279 evtimer_add(&gotd_listen.pause.ev, &backoff);
287 if (listen_client_cnt >= GOTD_MAXCLIENTS)
290 if (getpeereid(s, &euid, &egid) == -1) {
291 log_warn("getpeerid");
295 counter = find_uid_connection_counter(euid);
296 if (counter == NULL) {
297 counter = calloc(1, sizeof(*counter));
298 if (counter == NULL) {
299 log_warn("%s: calloc", __func__);
302 counter->euid = euid;
303 counter->nconnections = 1;
304 add_uid_connection_counter(counter);
306 if (counter->nconnections >= GOTD_MAX_CONN_PER_UID) {
307 log_warnx("maximum connections exceeded for uid %d",
311 counter->nconnections++;
314 client = calloc(1, sizeof(*client));
315 if (client == NULL) {
316 log_warn("%s: calloc", __func__);
319 client->id = get_client_id();
324 log_debug("%s: new client connected on fd %d uid %d gid %d", __func__,
325 client->fd, euid, egid);
327 memset(&iconn, 0, sizeof(iconn));
328 iconn.client_id = client->id;
333 log_warn("%s: dup", __func__);
336 if (gotd_imsg_compose_event(iev, GOTD_IMSG_CONNECT, PROC_LISTEN, s,
337 &iconn, sizeof(iconn)) == -1) {
338 log_warn("imsg compose CONNECT");
351 static const struct got_error *
352 recv_disconnect(struct imsg *imsg)
354 struct gotd_imsg_disconnect idisconnect;
356 struct gotd_listen_client *client = NULL;
358 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
359 if (datalen != sizeof(idisconnect))
360 return got_error(GOT_ERR_PRIVSEP_LEN);
361 memcpy(&idisconnect, imsg->data, sizeof(idisconnect));
363 log_debug("client disconnecting");
365 client = find_client(idisconnect.client_id);
367 return got_error(GOT_ERR_CLIENT_ID);
369 return disconnect(client);
373 listen_dispatch(int fd, short event, void *arg)
375 const struct got_error *err = NULL;
376 struct gotd_imsgev *iev = arg;
377 struct imsgbuf *ibuf = &iev->ibuf;
382 if (event & EV_READ) {
383 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
384 fatal("imsg_read error");
385 if (n == 0) /* Connection closed. */
389 if (event & EV_WRITE) {
390 n = msgbuf_write(&ibuf->w);
391 if (n == -1 && errno != EAGAIN)
392 fatal("msgbuf_write");
393 if (n == 0) /* Connection closed. */
398 if ((n = imsg_get(ibuf, &imsg)) == -1)
399 fatal("%s: imsg_get", __func__);
400 if (n == 0) /* No more messages. */
403 switch (imsg.hdr.type) {
404 case GOTD_IMSG_DISCONNECT:
405 err = recv_disconnect(&imsg);
407 log_warnx("%s: disconnect: %s",
408 gotd_listen.title, err->msg);
411 log_debug("%s: unexpected imsg %d", gotd_listen.title,
420 gotd_imsg_event_add(iev);
422 /* This pipe is dead. Remove its event handler */
424 event_loopexit(NULL);
429 listen_main(const char *title, int gotd_socket)
431 struct gotd_imsgev iev;
432 struct event evsigint, evsigterm, evsighup, evsigusr1;
434 arc4random_buf(&clients_hash_key, sizeof(clients_hash_key));
435 arc4random_buf(&uid_hash_key, sizeof(uid_hash_key));
437 gotd_listen.title = title;
438 gotd_listen.pid = getpid();
439 gotd_listen.fd = gotd_socket;
441 signal_set(&evsigint, SIGINT, listen_sighdlr, NULL);
442 signal_set(&evsigterm, SIGTERM, listen_sighdlr, NULL);
443 signal_set(&evsighup, SIGHUP, listen_sighdlr, NULL);
444 signal_set(&evsigusr1, SIGUSR1, listen_sighdlr, NULL);
445 signal(SIGPIPE, SIG_IGN);
447 signal_add(&evsigint, NULL);
448 signal_add(&evsigterm, NULL);
449 signal_add(&evsighup, NULL);
450 signal_add(&evsigusr1, NULL);
452 imsg_init(&iev.ibuf, GOTD_FILENO_MSG_PIPE);
453 iev.handler = listen_dispatch;
454 iev.events = EV_READ;
455 iev.handler_arg = NULL;
456 event_set(&iev.ev, iev.ibuf.fd, EV_READ, listen_dispatch, &iev);
457 if (event_add(&iev.ev, NULL) == -1)
460 event_set(&gotd_listen.iev.ev, gotd_listen.fd, EV_READ | EV_PERSIST,
462 if (event_add(&gotd_listen.iev.ev, NULL))
464 evtimer_set(&gotd_listen.pause.ev, gotd_accept_paused, NULL);
472 listen_shutdown(void)
474 log_debug("%s: shutting down", gotd_listen.title);
476 if (gotd_listen.fd != -1)
477 close(gotd_listen.fd);