2 * Copyright (c) 2021, 2022 Omar Polo <op@omarpolo.com>
3 * Copyright (c) 2018 Florian Obser <florian@openbsd.org>
4 * Copyright (c) 2004, 2005 Claudio Jeker <claudio@openbsd.org>
5 * Copyright (c) 2004 Esben Norby <norby@openbsd.org>
6 * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 #include <sys/socket.h>
24 #include <sys/types.h>
47 static struct kd_conf *listener_conf;
48 static struct imsgev *iev_main;
50 static void listener_sig_handler(int, short, void *);
51 __dead void listener_shutdown(void);
53 SPLAY_HEAD(clients_tree_id, client) clients;
64 struct bufferevent *bev;
65 SPLAY_ENTRY(client) sp_entry;
68 static void listener_imsg_event_add(struct imsgev *, void *);
69 static void listener_dispatch_client(int, short, void *);
70 static int listener_imsg_compose_client(struct client *, int,
71 uint32_t, const void *, uint16_t);
73 static void apply_config(struct kd_conf *);
74 static void handle_accept(int, short, void *);
76 static void handle_handshake(int, short, void *);
77 static void client_read(struct bufferevent *, void *);
78 static void client_write(struct bufferevent *, void *);
79 static void client_error(struct bufferevent *, short, void *);
80 static void client_tls_readcb(int, short, void *);
81 static void client_tls_writecb(int, short, void *);
82 static void close_conn(struct client *);
83 static void handle_close(int, short, void *);
86 clients_tree_cmp(struct client *a, struct client *b)
90 else if (a->id < b->id)
96 SPLAY_PROTOTYPE(clients_tree_id, client, sp_entry, clients_tree_cmp);
97 SPLAY_GENERATE(clients_tree_id, client, sp_entry, clients_tree_cmp)
100 listener_sig_handler(int sig, short event, void *d)
103 * Normal signal handler rules don't apply because libevent
112 fatalx("unexpected signal %d", sig);
117 listener(int debug, int verbose)
119 struct event ev_sigint, ev_sigterm;
122 /* listener_conf = config_new_empty(); */
124 log_init(debug, LOG_DAEMON);
125 log_setverbose(verbose);
127 if ((pw = getpwnam(KD_USER)) == NULL)
130 if (chroot(pw->pw_dir) == -1)
132 if (chdir("/") == -1)
133 fatal("chdir(\"/\")");
135 setproctitle("listener");
136 log_procinit("listener");
138 if (setgroups(1, &pw->pw_gid) ||
139 setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) ||
140 setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid))
141 fatal("can't drop privileges");
145 /* Setup signal handlers(s). */
146 signal_set(&ev_sigint, SIGINT, listener_sig_handler, NULL);
147 signal_set(&ev_sigterm, SIGTERM, listener_sig_handler, NULL);
149 signal_add(&ev_sigint, NULL);
150 signal_add(&ev_sigterm, NULL);
152 signal(SIGPIPE, SIG_IGN);
153 signal(SIGHUP, SIG_IGN);
155 /* Setup pipe and event handler to the main process. */
156 if ((iev_main = malloc(sizeof(*iev_main))) == NULL)
159 imsg_init(&iev_main->ibuf, 3);
160 iev_main->handler = listener_dispatch_main;
162 /* Setup event handlers. */
163 iev_main->events = EV_READ;
164 event_set(&iev_main->ev, iev_main->ibuf.fd, iev_main->events,
165 iev_main->handler, iev_main);
166 event_add(&iev_main->ev, NULL);
174 listener_shutdown(void)
176 msgbuf_clear(&iev_main->ibuf.w);
177 close(iev_main->ibuf.fd);
179 clear_config(listener_conf);
183 log_info("listener exiting");
188 listener_receive_config(struct imsg *imsg, struct kd_conf **nconf,
189 struct kd_pki_conf **pki)
191 struct kd_listen_conf *listen;
194 switch (imsg->hdr.type) {
195 case IMSG_RECONF_CONF:
197 fatalx("%s: IMSG_RECONF_CONF already in "
198 "progress", __func__);
200 if (IMSG_DATA_SIZE(*imsg) != sizeof(struct kd_conf))
201 fatalx("%s: IMSG_RECONF_CONF wrong length: %lu",
202 __func__, IMSG_DATA_SIZE(*imsg));
203 if ((*nconf = malloc(sizeof(**nconf))) == NULL)
205 memcpy(*nconf, imsg->data, sizeof(**nconf));
206 STAILQ_INIT(&(*nconf)->pki_head);
207 STAILQ_INIT(&(*nconf)->table_head);
208 STAILQ_INIT(&(*nconf)->listen_head);
210 case IMSG_RECONF_PKI:
212 fatalx("%s: IMSG_RECONF_PKI without "
213 "IMSG_RECONF_CONF", __func__);
214 *pki = xcalloc(1, sizeof(**pki));
216 t[IMSG_DATA_SIZE(*imsg)-1] = '\0';
217 strlcpy((*pki)->name, t, sizeof((*pki)->name));
219 case IMSG_RECONF_PKI_CERT:
221 fatalx("%s: IMSG_RECONF_PKI_CERT without "
222 "IMSG_RECONF_PKI", __func__);
223 (*pki)->certlen = IMSG_DATA_SIZE(*imsg);
224 (*pki)->cert = xmemdup(imsg->data, (*pki)->certlen);
226 case IMSG_RECONF_PKI_KEY:
228 fatalx("%s: IMSG_RECONF_PKI_KEY without "
229 "IMSG_RECONF_PKI", __func__);
230 (*pki)->keylen = IMSG_DATA_SIZE(*imsg);
231 (*pki)->key = xmemdup(imsg->data, (*pki)->keylen);
232 STAILQ_INSERT_HEAD(&(*nconf)->pki_head, *pki, entry);
235 case IMSG_RECONF_LISTEN:
237 fatalx("%s: IMSG_RECONF_LISTEN without "
238 "IMSG_RECONF_CONF", __func__);
239 if (IMSG_DATA_SIZE(*imsg) != sizeof(*listen))
240 fatalx("%s: IMSG_RECONF_LISTEN wrong length: %lu",
241 __func__, IMSG_DATA_SIZE(*imsg));
242 listen = xcalloc(1, sizeof(*listen));
243 memcpy(listen, imsg->data, sizeof(*listen));
244 memset(&listen->entry, 0, sizeof(listen->entry));
245 if ((listen->fd = imsg->fd) == -1)
246 fatalx("%s: IMSG_RECONF_LISTEN no fd",
248 listen->auth_table = NULL;
249 memset(&listen->ev, 0, sizeof(listen->ev));
250 STAILQ_INSERT_HEAD(&(*nconf)->listen_head, listen, entry);
252 case IMSG_RECONF_END:
254 fatalx("%s: IMSG_RECONF_END without "
255 "IMSG_RECONF_CONF", __func__);
256 apply_config(*nconf);
263 listener_dispatch_main(int fd, short event, void *d)
265 static struct kd_conf *nconf;
266 static struct kd_pki_conf *pki;
267 struct client *client, find;
269 struct imsgev *iev = d;
270 struct imsgbuf *ibuf;
276 if (event & EV_READ) {
277 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
278 fatal("imsg_read error");
279 if (n == 0) /* Connection closed. */
282 if (event & EV_WRITE) {
283 if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
284 fatal("msgbuf_write");
285 if (n == 0) /* Connection closed. */
290 if ((n = imsg_get(ibuf, &imsg)) == -1)
291 fatal("%s: imsg_get error", __func__);
292 if (n == 0) /* No more messages. */
295 switch (imsg.hdr.type) {
296 case IMSG_CTL_LOG_VERBOSE:
297 if (IMSG_DATA_SIZE(imsg) != sizeof(verbose))
298 fatalx("wrong size for IMSG_CTL_LOG_VERBOSE");
299 memcpy(&verbose, imsg.data, sizeof(verbose));
300 log_setverbose(verbose);
301 SPLAY_FOREACH(client, clients_tree_id, &clients)
302 listener_imsg_compose_client(client,
304 &verbose, sizeof(verbose));
307 if (SPLAY_EMPTY(&clients))
308 listener_imsg_compose_main(IMSG_CTL_DEBUG_END,
309 imsg.hdr.peerid, NULL, 0);
310 SPLAY_FOREACH(client, clients_tree_id, &clients)
311 listener_imsg_compose_client(client,
312 imsg.hdr.type, imsg.hdr.peerid,
313 imsg.data, IMSG_DATA_SIZE(imsg));
315 case IMSG_RECONF_CONF:
316 case IMSG_RECONF_PKI:
317 case IMSG_RECONF_PKI_CERT:
318 case IMSG_RECONF_PKI_KEY:
319 case IMSG_RECONF_LISTEN:
320 case IMSG_RECONF_END:
321 listener_receive_config(&imsg, &nconf, &pki);
324 if (IMSG_DATA_SIZE(imsg) != sizeof(struct kd_auth_proc))
325 fatalx("mismatching size for IMSG_AUTH");
327 find.id = imsg.hdr.peerid;
328 client = SPLAY_FIND(clients_tree_id, &clients, &find);
329 if (client == NULL) {
335 log_info("got fd = -1, auth failed?");
339 imsg_init(&client->iev.ibuf, imsg.fd);
340 client->iev.events = EV_READ;
341 client->iev.handler = listener_dispatch_client;
342 event_set(&client->iev.ev, client->iev.ibuf.fd,
343 client->iev.events, client->iev.handler, client);
344 listener_imsg_compose_client(client, IMSG_AUTH,
345 client->id, imsg.data, IMSG_DATA_SIZE(imsg));
347 client->bev = bufferevent_new(client->fd,
348 client_read, client_write, client_error,
350 if (client->bev == NULL) {
351 log_info("failed to allocate client buffer");
357 evbuffer_unfreeze(client->bev->input, 0);
358 evbuffer_unfreeze(client->bev->output, 1);
361 if (client->lflags & L_TLS) {
362 event_set(&client->bev->ev_read, client->fd,
363 EV_READ, client_tls_readcb, client->bev);
364 event_set(&client->bev->ev_write, client->fd,
365 EV_WRITE, client_tls_writecb, client->bev);
369 * Read or write at least a header before
370 * firing the callbacks. High watermark of 0
371 * to never stop reading/writing; probably to
374 /* bufferevent_setwatermark(client->bev, EV_READ|EV_WRITE, */
375 /* sizeof(struct np_msg_header), 0); */
376 bufferevent_enable(client->bev, EV_READ|EV_WRITE);
380 log_debug("%s: unexpected imsg %d", __func__,
388 listener_imsg_event_add(iev, d);
390 /* This pipe is dead. Remove its event handler. */
392 log_warnx("pipe closed, shutting down...");
393 event_loopexit(NULL);
398 listener_imsg_compose_main(int type, uint32_t peerid, const void *data,
401 return imsg_compose_event(iev_main, type, peerid, 0, -1, data,
406 listener_imsg_event_add(struct imsgev *iev, void *d)
408 iev->events = EV_READ;
409 if (iev->ibuf.w.queued)
410 iev->events |= EV_WRITE;
413 event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, d);
414 event_add(&iev->ev, NULL);
418 listener_dispatch_client(int fd, short event, void *d)
420 struct client find, *client = d;
423 struct imsgbuf *ibuf;
430 if (event & EV_READ) {
431 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
432 fatal("imsg_read error");
433 if (n == 0) /* Connection closed */
437 if (event & EV_WRITE) {
438 if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
439 fatal("msgbuf_write");
440 if (n == 0) /* Connection closed. */
445 if ((n = imsg_get(ibuf, &imsg)) == -1)
446 fatal("%s: imsg_get error", __func__);
447 if (n == 0) /* No more messages. */
450 switch (imsg.hdr.type) {
451 case IMSG_CTL_DEBUG_BACK:
452 case IMSG_CTL_DEBUG_END:
453 listener_imsg_compose_main(imsg.hdr.type,
454 imsg.hdr.peerid, imsg.data, IMSG_DATA_SIZE(imsg));
458 find.id = imsg.hdr.peerid;
459 client = SPLAY_FIND(clients_tree_id, &clients, &find);
460 if (client == NULL) {
461 log_info("got IMSG_BUF but client %d gone",
465 r = bufferevent_write(client->bev, imsg.data,
466 IMSG_DATA_SIZE(imsg));
468 log_warn("%s: bufferevent_write failed",
476 if (IMSG_DATA_SIZE(imsg) != sizeof(client->msize))
477 fatal("IMSG_MSIZE size mismatch: "
478 "got %zu want %zu", IMSG_DATA_SIZE(imsg),
479 sizeof(client->msize));
481 memcpy(&client->msize, imsg.data,
482 sizeof(client->msize));
484 if (client->msize == 0)
485 fatal("IMSG_MSIZE got msize = 0");
491 * Both EVBUFFER_READ or EVBUFFER_WRITE should
494 client_error(client->bev, EVBUFFER_READ, client);
498 log_debug("%s: unexpected imsg %d", __func__,
506 listener_imsg_event_add(iev, d);
508 /* This pipe is dead. Remove its handler */
509 log_debug("client proc vanished");
515 listener_imsg_compose_client(struct client *client, int type,
516 uint32_t peerid, const void *data, uint16_t len)
520 if ((ret = imsg_compose(&client->iev.ibuf, type, peerid, 0, -1,
522 listener_imsg_event_add(&client->iev, client);
527 static inline struct kd_pki_conf *
528 pki_by_name(const char *name)
530 struct kd_pki_conf *pki;
532 STAILQ_FOREACH(pki, &listener_conf->pki_head, entry) {
533 if (!strcmp(name, pki->name))
541 apply_config(struct kd_conf *conf)
543 struct kd_pki_conf *pki;
544 struct kd_listen_conf *listen;
547 /* drop any pre-auth inflight connections */
548 SPLAY_FOREACH(c, clients_tree_id, &clients) {
550 * c->event is set only during the handshake and the teardown
551 * of the connection; c->bev is set only after auth. Checking
552 * for both ensures we drop only incoming connection in the
555 if (event_pending(&c->event, EV_READ|EV_WRITE, NULL) &&
557 log_warn("closing in-flight connection due to reload");
562 /* swap the now config with the current one */
563 clear_config(listener_conf);
564 listener_conf = conf;
566 /* prepare the various tls_config */
567 STAILQ_FOREACH(pki, &listener_conf->pki_head, entry) {
568 if ((pki->tlsconf = tls_config_new()) == NULL)
569 fatal("tls_config_new");
570 tls_config_verify_client_optional(pki->tlsconf);
571 tls_config_insecure_noverifycert(pki->tlsconf);
572 if (tls_config_set_keypair_mem(pki->tlsconf,
573 pki->cert, pki->certlen,
574 pki->key, pki->keylen) == -1)
575 fatalx("tls_config_set_keypair_mem: %s",
576 tls_config_error(pki->tlsconf));
579 /* prepare and kickoff the listeners */
580 STAILQ_FOREACH(listen, &listener_conf->listen_head, entry) {
581 if ((listen->ctx = tls_server()) == NULL)
584 pki = pki_by_name(listen->pki);
585 if (tls_configure(listen->ctx, pki->tlsconf) == -1)
586 fatalx("tls_configure: %s",
587 tls_config_error(pki->tlsconf));
589 event_set(&listen->ev, listen->fd, EV_READ|EV_PERSIST,
590 handle_accept, listen);
591 event_add(&listen->ev, NULL);
596 yield_r(struct client *c, void (*fn)(int, short, void *))
598 if (event_pending(&c->event, EV_WRITE|EV_READ, NULL))
599 event_del(&c->event);
600 event_set(&c->event, c->fd, EV_READ, fn, c);
601 event_add(&c->event, NULL);
605 yield_w(struct client *c, void (*fn)(int, short, void *))
607 if (event_pending(&c->event, EV_WRITE|EV_READ, NULL))
608 event_del(&c->event);
609 event_set(&c->event, c->fd, EV_WRITE, fn, c);
610 event_add(&c->event, NULL);
614 handle_accept(int fd, short ev, void *data)
616 static uint32_t counter;
617 struct kd_listen_conf *listen = data;
621 if ((s = accept(fd, NULL, NULL)) == -1) {
626 c = xcalloc(1, sizeof(*c));
629 c->lflags = listen->flags;
632 if (tls_accept_socket(listen->ctx, &c->ctx, s) == -1) {
633 log_warnx("tls_accept_socket: %s",
634 tls_error(listen->ctx));
643 SPLAY_INSERT(clients_tree_id, &clients, c);
645 /* initialize the event */
646 event_set(&c->event, c->fd, EV_READ, NULL, NULL);
648 yield_r(c, handle_handshake);
652 handle_handshake(int fd, short ev, void *data)
654 struct client *c = data;
655 struct kd_auth_req auth;
659 switch (r = tls_handshake(c->ctx)) {
660 case TLS_WANT_POLLIN:
661 yield_r(c, handle_handshake);
663 case TLS_WANT_POLLOUT:
664 yield_w(c, handle_handshake);
667 log_debug("handhsake failed: %s", tls_error(c->ctx));
672 if ((hash = tls_peer_cert_hash(c->ctx)) == NULL) {
673 log_warnx("client didn't provide certificate");
678 memset(&auth, 0, sizeof(auth));
679 auth.listen_id = c->lid;
680 strlcpy(auth.hash, hash, sizeof(auth.hash));
681 log_debug("sending hash %s", auth.hash);
683 listener_imsg_compose_main(IMSG_AUTH_TLS, c->id,
684 &auth, sizeof(auth));
688 client_read(struct bufferevent *bev, void *d)
690 struct client *client = d;
691 struct evbuffer *src = EVBUFFER_INPUT(bev);
695 if (EVBUFFER_LENGTH(src) < 4)
698 memcpy(&len, EVBUFFER_DATA(src), sizeof(len));
700 log_debug("expecting a message %"PRIu32" bytes long "
701 "(of wich %zu already read)",
702 len, EVBUFFER_LENGTH(src));
704 if (len < HEADERSIZE) {
705 log_warnx("invalid message size %d (too low)", len);
706 client_error(bev, EVBUFFER_READ, client);
710 if (len > client->msize) {
711 log_warnx("incoming message bigger than msize "
712 "(%"PRIu32" vs %"PRIu32")", len, client->msize);
713 client_error(bev, EVBUFFER_READ, client);
717 if (len > EVBUFFER_LENGTH(src))
720 listener_imsg_compose_client(client, IMSG_BUF, client->id,
721 EVBUFFER_DATA(src), len);
722 evbuffer_drain(src, len);
727 client_write(struct bufferevent *bev, void *d)
730 * here we can do some fancy logic like deciding when to call
732 * (*bev->errorcb)(bev, EVBUFFER_WRITE, bev->cbarg)
734 * to signal the end of the transaction.
741 client_error(struct bufferevent *bev, short err, void *d)
743 struct client *client = d;
744 struct evbuffer *buf;
746 if (err & EVBUFFER_ERROR) {
747 if (errno == EFBIG) {
748 bufferevent_enable(bev, EV_READ);
751 log_debug("buffer event error");
756 if (err & EVBUFFER_EOF) {
761 if (err & (EVBUFFER_READ|EVBUFFER_WRITE)) {
762 bufferevent_disable(bev, EV_READ|EV_WRITE);
764 buf = EVBUFFER_OUTPUT(client->bev);
765 if (EVBUFFER_LENGTH(buf) != 0) {
766 /* finish writing all the data first */
767 bufferevent_enable(client->bev, EV_WRITE);
775 log_warnx("unknown event error, closing client connection");
780 client_tls_readcb(int fd, short event, void *d)
782 struct bufferevent *bufev = d;
783 struct client *client = bufev->cbarg;
784 char buf[IBUF_READ_SIZE];
785 int what = EVBUFFER_READ;
786 int howmuch = IBUF_READ_SIZE;
790 if (event == EV_TIMEOUT) {
791 what |= EVBUFFER_TIMEOUT;
795 if (bufev->wm_read.high != 0)
796 howmuch = MIN(sizeof(buf), bufev->wm_read.high);
798 switch (ret = tls_read(client->ctx, buf, howmuch)) {
799 case TLS_WANT_POLLIN:
800 case TLS_WANT_POLLOUT:
803 what |= EVBUFFER_ERROR;
809 what |= EVBUFFER_EOF;
813 if (evbuffer_add(bufev->input, buf, len) == -1) {
814 what |= EVBUFFER_ERROR;
818 event_add(&bufev->ev_read, NULL);
820 len = EVBUFFER_LENGTH(bufev->input);
821 if (bufev->wm_read.low != 0 && len < bufev->wm_read.low)
823 if (bufev->wm_read.high != 0 && len > bufev->wm_read.high) {
825 * here we could implement some read pressure
830 if (bufev->readcb != NULL)
831 (*bufev->readcb)(bufev, bufev->cbarg);
836 event_add(&bufev->ev_read, NULL);
840 (*bufev->errorcb)(bufev, what, bufev->cbarg);
844 client_tls_writecb(int fd, short event, void *d)
846 struct bufferevent *bufev = d;
847 struct client *client = bufev->cbarg;
850 short what = EVBUFFER_WRITE;
852 if (event == EV_TIMEOUT) {
853 what |= EVBUFFER_TIMEOUT;
857 if (EVBUFFER_LENGTH(bufev->output) != 0) {
858 ret = tls_write(client->ctx,
859 EVBUFFER_DATA(bufev->output),
860 EVBUFFER_LENGTH(bufev->output));
862 case TLS_WANT_POLLIN:
863 case TLS_WANT_POLLOUT:
866 what |= EVBUFFER_ERROR;
870 evbuffer_drain(bufev->output, len);
873 if (EVBUFFER_LENGTH(bufev->output) != 0)
874 event_add(&bufev->ev_write, NULL);
876 if (bufev->writecb != NULL &&
877 EVBUFFER_LENGTH(bufev->output) <= bufev->wm_write.low)
878 (*bufev->writecb)(bufev, bufev->cbarg);
882 event_add(&bufev->ev_write, NULL);
886 (*bufev->errorcb)(bufev, what, bufev->cbarg);
890 close_conn(struct client *c)
892 log_debug("closing connection");
894 SPLAY_REMOVE(clients_tree_id, &clients, c);
896 if (c->iev.ibuf.fd != -1) {
897 listener_imsg_compose_client(c, IMSG_CONN_GONE, 0, NULL, 0);
898 imsg_flush(&c->iev.ibuf);
899 msgbuf_clear(&c->iev.ibuf.w);
900 event_del(&c->iev.ev);
901 close(c->iev.ibuf.fd);
904 handle_close(c->fd, 0, c);
908 handle_close(int fd, short ev, void *d)
910 struct client *c = d;
912 switch (tls_close(c->ctx)) {
913 case TLS_WANT_POLLIN:
914 yield_r(c, handle_close);
916 case TLS_WANT_POLLOUT:
917 yield_w(c, handle_close);
921 event_del(&c->event);