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);
262 static inline struct kd_listen_conf *
263 listen_by_id(uint32_t id)
265 struct kd_listen_conf *l;
267 STAILQ_FOREACH(l, &listener_conf->listen_head, entry) {
275 listener_dispatch_main(int fd, short event, void *d)
277 static struct kd_conf *nconf;
278 static struct kd_pki_conf *pki;
279 struct kd_listen_conf *listen;
280 struct client *client, find;
282 struct imsgev *iev = d;
283 struct imsgbuf *ibuf;
289 if (event & EV_READ) {
290 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
291 fatal("imsg_read error");
292 if (n == 0) /* Connection closed. */
295 if (event & EV_WRITE) {
296 if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
297 fatal("msgbuf_write");
298 if (n == 0) /* Connection closed. */
303 if ((n = imsg_get(ibuf, &imsg)) == -1)
304 fatal("%s: imsg_get error", __func__);
305 if (n == 0) /* No more messages. */
308 switch (imsg.hdr.type) {
309 case IMSG_CTL_LOG_VERBOSE:
310 if (IMSG_DATA_SIZE(imsg) != sizeof(verbose))
311 fatalx("wrong size for IMSG_CTL_LOG_VERBOSE");
312 memcpy(&verbose, imsg.data, sizeof(verbose));
313 log_setverbose(verbose);
314 SPLAY_FOREACH(client, clients_tree_id, &clients)
315 listener_imsg_compose_client(client,
317 &verbose, sizeof(verbose));
319 case IMSG_RECONF_CONF:
320 case IMSG_RECONF_PKI:
321 case IMSG_RECONF_PKI_CERT:
322 case IMSG_RECONF_PKI_KEY:
323 case IMSG_RECONF_LISTEN:
324 case IMSG_RECONF_END:
325 listener_receive_config(&imsg, &nconf, &pki);
328 if (IMSG_DATA_SIZE(imsg) != sizeof(struct kd_auth_proc))
329 fatalx("mismatching size for IMSG_AUTH");
331 find.id = imsg.hdr.peerid;
332 client = SPLAY_FIND(clients_tree_id, &clients, &find);
333 if (client == NULL) {
339 log_info("got fd = -1, auth failed?");
343 imsg_init(&client->iev.ibuf, imsg.fd);
344 client->iev.events = EV_READ;
345 client->iev.handler = listener_dispatch_client;
346 event_set(&client->iev.ev, client->iev.ibuf.fd,
347 client->iev.events, client->iev.handler, client);
348 listener_imsg_compose_client(client, IMSG_AUTH,
349 client->id, imsg.data, IMSG_DATA_SIZE(imsg));
351 client->bev = bufferevent_new(client->fd,
352 client_read, client_write, client_error,
354 if (client->bev == NULL) {
355 log_info("failed to allocate client buffer");
361 evbuffer_unfreeze(client->bev->input, 0);
362 evbuffer_unfreeze(client->bev->output, 1);
365 if (client->lflags & L_TLS) {
366 event_set(&client->bev->ev_read, client->fd,
367 EV_READ, client_tls_readcb, client->bev);
368 event_set(&client->bev->ev_write, client->fd,
369 EV_WRITE, client_tls_writecb, client->bev);
373 * Read or write at least a header before
374 * firing the callbacks. High watermark of 0
375 * to never stop reading/writing; probably to
378 /* bufferevent_setwatermark(client->bev, EV_READ|EV_WRITE, */
379 /* sizeof(struct np_msg_header), 0); */
380 bufferevent_enable(client->bev, EV_READ|EV_WRITE);
384 log_debug("%s: unexpected imsg %d", __func__,
392 listener_imsg_event_add(iev, d);
394 /* This pipe is dead. Remove its event handler. */
396 log_warnx("pipe closed, shutting down...");
397 event_loopexit(NULL);
402 listener_imsg_compose_main(int type, uint32_t peerid, const void *data,
405 return imsg_compose_event(iev_main, type, peerid, 0, -1, data,
410 listener_imsg_event_add(struct imsgev *iev, void *d)
412 iev->events = EV_READ;
413 if (iev->ibuf.w.queued)
414 iev->events |= EV_WRITE;
417 event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, d);
418 event_add(&iev->ev, NULL);
422 listener_dispatch_client(int fd, short event, void *d)
424 struct client find, *client = d;
427 struct imsgbuf *ibuf;
434 if (event & EV_READ) {
435 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
436 fatal("imsg_read error");
437 if (n == 0) /* Connection closed */
441 if (event & EV_WRITE) {
442 if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
443 fatal("msgbuf_write");
444 if (n == 0) /* Connection closed. */
449 if ((n = imsg_get(ibuf, &imsg)) == -1)
450 fatal("%s: imsg_get error", __func__);
451 if (n == 0) /* No more messages. */
454 switch (imsg.hdr.type) {
456 find.id = imsg.hdr.peerid;
457 client = SPLAY_FIND(clients_tree_id, &clients, &find);
458 if (client == NULL) {
459 log_info("got IMSG_BUF but client (%d) gone",
463 r = bufferevent_write(client->bev, imsg.data,
464 IMSG_DATA_SIZE(imsg));
466 log_warn("%s: bufferevent_write failed",
474 if (IMSG_DATA_SIZE(imsg) != sizeof(client->msize))
475 fatal("IMSG_MSIZE size mismatch: "
476 "got %zu want %zu", IMSG_DATA_SIZE(imsg),
477 sizeof(client->msize));
479 memcpy(&client->msize, imsg.data,
480 sizeof(client->msize));
482 if (client->msize == 0)
483 fatal("IMSG_MSIZE got msize = 0");
489 * Both EVBUFFER_READ or EVBUFFER_WRITE should
492 client_error(client->bev, EVBUFFER_READ, client);
496 log_debug("%s: unexpected imsg %d", __func__,
504 listener_imsg_event_add(iev, d);
506 /* This pipe is dead. Remove its handler */
507 log_debug("client proc vanished");
513 listener_imsg_compose_client(struct client *client, int type,
514 uint32_t peerid, const void *data, uint16_t len)
518 if ((ret = imsg_compose(&client->iev.ibuf, type, peerid, 0, -1,
520 listener_imsg_event_add(&client->iev, client);
525 static inline struct kd_pki_conf *
526 pki_by_name(const char *name)
528 struct kd_pki_conf *pki;
530 STAILQ_FOREACH(pki, &listener_conf->pki_head, entry) {
531 if (!strcmp(name, pki->name))
539 apply_config(struct kd_conf *conf)
541 struct kd_pki_conf *pki;
542 struct kd_listen_conf *listen;
545 /* drop any pre-auth inflight connections */
546 SPLAY_FOREACH(c, clients_tree_id, &clients) {
548 * c->event is set only during the handshake and the teardown
549 * of the connection; c->bev is set only after auth. Checking
550 * for both ensures we drop only incoming connection in the
553 if (event_pending(&c->event, EV_READ|EV_WRITE, NULL) &&
555 log_warn("closing in-flight connection due to reload");
560 /* swap the now config with the current one */
561 clear_config(listener_conf);
562 listener_conf = conf;
564 /* prepare the various tls_config */
565 STAILQ_FOREACH(pki, &listener_conf->pki_head, entry) {
566 if ((pki->tlsconf = tls_config_new()) == NULL)
567 fatal("tls_config_new");
568 tls_config_verify_client_optional(pki->tlsconf);
569 tls_config_insecure_noverifycert(pki->tlsconf);
570 if (tls_config_set_keypair_mem(pki->tlsconf,
571 pki->cert, pki->certlen,
572 pki->key, pki->keylen) == -1)
573 fatalx("tls_config_set_keypair_mem: %s",
574 tls_config_error(pki->tlsconf));
577 /* prepare and kickoff the listeners */
578 STAILQ_FOREACH(listen, &listener_conf->listen_head, entry) {
579 if ((listen->ctx = tls_server()) == NULL)
582 pki = pki_by_name(listen->pki);
583 if (tls_configure(listen->ctx, pki->tlsconf) == -1)
584 fatalx("tls_configure: %s",
585 tls_config_error(pki->tlsconf));
587 event_set(&listen->ev, listen->fd, EV_READ|EV_PERSIST,
588 handle_accept, listen);
589 event_add(&listen->ev, NULL);
594 yield_r(struct client *c, void (*fn)(int, short, void *))
596 if (event_pending(&c->event, EV_WRITE|EV_READ, NULL))
597 event_del(&c->event);
598 event_set(&c->event, c->fd, EV_READ, fn, c);
599 event_add(&c->event, NULL);
603 yield_w(struct client *c, void (*fn)(int, short, void *))
605 if (event_pending(&c->event, EV_WRITE|EV_READ, NULL))
606 event_del(&c->event);
607 event_set(&c->event, c->fd, EV_WRITE, fn, c);
608 event_add(&c->event, NULL);
612 handle_accept(int fd, short ev, void *data)
614 static uint32_t counter;
615 struct kd_listen_conf *listen = data;
619 if ((s = accept(fd, NULL, NULL)) == -1) {
624 c = xcalloc(1, sizeof(*c));
627 c->lflags = listen->flags;
630 if (tls_accept_socket(listen->ctx, &c->ctx, s) == -1) {
631 log_warnx("tls_accept_socket: %s",
632 tls_error(listen->ctx));
641 SPLAY_INSERT(clients_tree_id, &clients, c);
643 /* initialize the event */
644 event_set(&c->event, c->fd, EV_READ, NULL, NULL);
646 yield_r(c, handle_handshake);
650 handle_handshake(int fd, short ev, void *data)
652 struct client *c = data;
653 struct kd_auth_req auth;
657 switch (r = tls_handshake(c->ctx)) {
658 case TLS_WANT_POLLIN:
659 yield_r(c, handle_handshake);
661 case TLS_WANT_POLLOUT:
662 yield_w(c, handle_handshake);
665 log_debug("handhsake failed: %s", tls_error(c->ctx));
670 if ((hash = tls_peer_cert_hash(c->ctx)) == NULL) {
671 log_warnx("client didn't provide certificate");
676 memset(&auth, 0, sizeof(auth));
677 auth.listen_id = c->lid;
678 strlcpy(auth.hash, hash, sizeof(auth.hash));
679 log_debug("sending hash %s", auth.hash);
681 listener_imsg_compose_main(IMSG_AUTH_TLS, c->id,
682 &auth, sizeof(auth));
686 client_read(struct bufferevent *bev, void *d)
688 struct client *client = d;
689 struct evbuffer *src = EVBUFFER_INPUT(bev);
693 if (EVBUFFER_LENGTH(src) < 4)
696 memcpy(&len, EVBUFFER_DATA(src), sizeof(len));
698 log_debug("expecting a message %"PRIu32" bytes long "
699 "(of wich %zu already read)",
700 len, EVBUFFER_LENGTH(src));
702 if (len < HEADERSIZE) {
703 log_warnx("invalid message size %d (too low)", len);
704 client_error(bev, EVBUFFER_READ, client);
708 if (len > client->msize) {
709 log_warnx("incoming message bigger than msize "
710 "(%"PRIu32" vs %"PRIu32")", len, client->msize);
711 client_error(bev, EVBUFFER_READ, client);
715 if (len > EVBUFFER_LENGTH(src))
718 listener_imsg_compose_client(client, IMSG_BUF, client->id,
719 EVBUFFER_DATA(src), len);
720 evbuffer_drain(src, len);
725 client_write(struct bufferevent *bev, void *d)
728 * here we can do some fancy logic like deciding when to call
730 * (*bev->errorcb)(bev, EVBUFFER_WRITE, bev->cbarg)
732 * to signal the end of the transaction.
739 client_error(struct bufferevent *bev, short err, void *d)
741 struct client *client = d;
742 struct evbuffer *buf;
744 if (err & EVBUFFER_ERROR) {
745 if (errno == EFBIG) {
746 bufferevent_enable(bev, EV_READ);
749 log_debug("buffer event error");
754 if (err & EVBUFFER_EOF) {
759 if (err & (EVBUFFER_READ|EVBUFFER_WRITE)) {
760 bufferevent_disable(bev, EV_READ|EV_WRITE);
762 buf = EVBUFFER_OUTPUT(client->bev);
763 if (EVBUFFER_LENGTH(buf) != 0) {
764 /* finish writing all the data first */
765 bufferevent_enable(client->bev, EV_WRITE);
773 log_warnx("unknown event error, closing client connection");
778 client_tls_readcb(int fd, short event, void *d)
780 struct bufferevent *bufev = d;
781 struct client *client = bufev->cbarg;
782 char buf[IBUF_READ_SIZE];
783 int what = EVBUFFER_READ;
784 int howmuch = IBUF_READ_SIZE;
788 if (event == EV_TIMEOUT) {
789 what |= EVBUFFER_TIMEOUT;
793 if (bufev->wm_read.high != 0)
794 howmuch = MIN(sizeof(buf), bufev->wm_read.high);
796 switch (ret = tls_read(client->ctx, buf, howmuch)) {
797 case TLS_WANT_POLLIN:
798 case TLS_WANT_POLLOUT:
801 what |= EVBUFFER_ERROR;
807 what |= EVBUFFER_EOF;
811 if (evbuffer_add(bufev->input, buf, len) == -1) {
812 what |= EVBUFFER_ERROR;
816 event_add(&bufev->ev_read, NULL);
818 len = EVBUFFER_LENGTH(bufev->input);
819 if (bufev->wm_read.low != 0 && len < bufev->wm_read.low)
821 if (bufev->wm_read.high != 0 && len > bufev->wm_read.high) {
823 * here we could implement some read pressure
828 if (bufev->readcb != NULL)
829 (*bufev->readcb)(bufev, bufev->cbarg);
834 event_add(&bufev->ev_read, NULL);
838 (*bufev->errorcb)(bufev, what, bufev->cbarg);
842 client_tls_writecb(int fd, short event, void *d)
844 struct bufferevent *bufev = d;
845 struct client *client = bufev->cbarg;
848 short what = EVBUFFER_WRITE;
850 if (event == EV_TIMEOUT) {
851 what |= EVBUFFER_TIMEOUT;
855 if (EVBUFFER_LENGTH(bufev->output) != 0) {
856 ret = tls_write(client->ctx,
857 EVBUFFER_DATA(bufev->output),
858 EVBUFFER_LENGTH(bufev->output));
860 case TLS_WANT_POLLIN:
861 case TLS_WANT_POLLOUT:
864 what |= EVBUFFER_ERROR;
868 evbuffer_drain(bufev->output, len);
871 if (EVBUFFER_LENGTH(bufev->output) != 0)
872 event_add(&bufev->ev_write, NULL);
874 if (bufev->writecb != NULL &&
875 EVBUFFER_LENGTH(bufev->output) <= bufev->wm_write.low)
876 (*bufev->writecb)(bufev, bufev->cbarg);
880 event_add(&bufev->ev_write, NULL);
884 (*bufev->errorcb)(bufev, what, bufev->cbarg);
888 close_conn(struct client *c)
890 log_debug("closing connection");
892 SPLAY_REMOVE(clients_tree_id, &clients, c);
894 if (c->iev.ibuf.fd != -1) {
895 listener_imsg_compose_client(c, IMSG_CONN_GONE, 0, NULL, 0);
896 imsg_flush(&c->iev.ibuf);
897 msgbuf_clear(&c->iev.ibuf.w);
898 event_del(&c->iev.ev);
899 close(c->iev.ibuf.fd);
902 handle_close(c->fd, 0, c);
906 handle_close(int fd, short ev, void *d)
908 struct client *c = d;
910 switch (tls_close(c->ctx)) {
911 case TLS_WANT_POLLIN:
912 yield_r(c, handle_close);
914 case TLS_WANT_POLLOUT:
915 yield_w(c, handle_close);
919 event_del(&c->event);