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 config_clear(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 (listener_conf != NULL)
201 fatalx("%s: don't know how reload the "
202 "configuration yet", __func__);
204 if (IMSG_DATA_SIZE(*imsg) != sizeof(struct kd_conf))
205 fatalx("%s: IMSG_RECONF_CONF wrong length: %lu",
206 __func__, IMSG_DATA_SIZE(*imsg));
207 if ((*nconf = malloc(sizeof(**nconf))) == NULL)
209 memcpy(*nconf, imsg->data, sizeof(**nconf));
210 memset(&(*nconf)->pki_head, 0, sizeof((*nconf)->pki_head));
211 memset(&(*nconf)->table_head, 0, sizeof((*nconf)->table_head));
212 memset(&(*nconf)->listen_head, 0, sizeof((*nconf)->listen_head));
214 case IMSG_RECONF_PKI:
216 fatalx("%s: IMSG_RECONF_PKI without "
217 "IMSG_RECONF_CONF", __func__);
218 *pki = xcalloc(1, sizeof(**pki));
220 t[IMSG_DATA_SIZE(*imsg)-1] = '\0';
221 strlcpy((*pki)->name, t, sizeof((*pki)->name));
223 case IMSG_RECONF_PKI_CERT:
225 fatalx("%s: IMSG_RECONF_PKI_CERT without "
226 "IMSG_RECONF_PKI", __func__);
227 (*pki)->certlen = IMSG_DATA_SIZE(*imsg);
228 (*pki)->cert = xmemdup(imsg->data, (*pki)->certlen);
230 case IMSG_RECONF_PKI_KEY:
232 fatalx("%s: IMSG_RECONF_PKI_KEY without "
233 "IMSG_RECONF_PKI", __func__);
234 (*pki)->keylen = IMSG_DATA_SIZE(*imsg);
235 (*pki)->key = xmemdup(imsg->data, (*pki)->keylen);
236 STAILQ_INSERT_HEAD(&(*nconf)->pki_head, *pki, entry);
239 case IMSG_RECONF_LISTEN:
241 fatalx("%s: IMSG_RECONF_LISTEN without "
242 "IMSG_RECONF_CONF", __func__);
243 if (IMSG_DATA_SIZE(*imsg) != sizeof(*listen))
244 fatalx("%s: IMSG_RECONF_LISTEN wrong length: %lu",
245 __func__, IMSG_DATA_SIZE(*imsg));
246 listen = xcalloc(1, sizeof(*listen));
247 memcpy(listen, imsg->data, sizeof(*listen));
248 memset(&listen->entry, 0, sizeof(listen->entry));
249 if ((listen->fd = imsg->fd) == -1)
250 fatalx("%s: IMSG_RECONF_LISTEN no fd",
252 listen->auth_table = NULL;
253 memset(&listen->ev, 0, sizeof(listen->ev));
254 STAILQ_INSERT_HEAD(&(*nconf)->listen_head, listen, entry);
256 case IMSG_RECONF_END:
258 fatalx("%s: IMSG_RECONF_END without "
259 "IMSG_RECONF_CONF", __func__);
260 /* merge_config(listener_conf, nconf); */
261 apply_config(*nconf);
267 static inline struct kd_listen_conf *
268 listen_by_id(uint32_t id)
270 struct kd_listen_conf *l;
272 STAILQ_FOREACH(l, &listener_conf->listen_head, entry) {
280 listener_dispatch_main(int fd, short event, void *d)
282 static struct kd_conf *nconf;
283 static struct kd_pki_conf *pki;
284 struct kd_listen_conf *listen;
285 struct client *client, find;
287 struct imsgev *iev = d;
288 struct imsgbuf *ibuf;
294 if (event & EV_READ) {
295 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
296 fatal("imsg_read error");
297 if (n == 0) /* Connection closed. */
300 if (event & EV_WRITE) {
301 if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
302 fatal("msgbuf_write");
303 if (n == 0) /* Connection closed. */
308 if ((n = imsg_get(ibuf, &imsg)) == -1)
309 fatal("%s: imsg_get error", __func__);
310 if (n == 0) /* No more messages. */
313 switch (imsg.hdr.type) {
314 case IMSG_CTL_LOG_VERBOSE:
315 if (IMSG_DATA_SIZE(imsg) != sizeof(verbose))
316 fatalx("wrong size for IMSG_CTL_LOG_VERBOSE");
317 memcpy(&verbose, imsg.data, sizeof(verbose));
318 log_setverbose(verbose);
319 SPLAY_FOREACH(client, clients_tree_id, &clients)
320 listener_imsg_compose_client(client,
322 &verbose, sizeof(verbose));
324 case IMSG_RECONF_CONF:
325 case IMSG_RECONF_PKI:
326 case IMSG_RECONF_PKI_CERT:
327 case IMSG_RECONF_PKI_KEY:
328 case IMSG_RECONF_LISTEN:
329 case IMSG_RECONF_END:
330 listener_receive_config(&imsg, &nconf, &pki);
333 if (IMSG_DATA_SIZE(imsg) != sizeof(struct kd_auth_proc))
334 fatalx("mismatching size for IMSG_AUTH");
336 find.id = imsg.hdr.peerid;
337 client = SPLAY_FIND(clients_tree_id, &clients, &find);
338 if (client == NULL) {
344 log_info("got fd = -1, auth failed?");
348 imsg_init(&client->iev.ibuf, imsg.fd);
349 client->iev.events = EV_READ;
350 client->iev.handler = listener_dispatch_client;
351 event_set(&client->iev.ev, client->iev.ibuf.fd,
352 client->iev.events, client->iev.handler, client);
353 listener_imsg_compose_client(client, IMSG_AUTH,
354 client->id, imsg.data, IMSG_DATA_SIZE(imsg));
356 client->bev = bufferevent_new(client->fd,
357 client_read, client_write, client_error,
359 if (client->bev == NULL) {
360 log_info("failed to allocate client buffer");
366 evbuffer_unfreeze(client->bev->input, 0);
367 evbuffer_unfreeze(client->bev->output, 1);
370 listen = listen_by_id(client->lid);
371 if (listen->flags & L_TLS) {
372 event_set(&client->bev->ev_read, client->fd,
373 EV_READ, client_tls_readcb, client->bev);
374 event_set(&client->bev->ev_write, client->fd,
375 EV_WRITE, client_tls_writecb, client->bev);
379 * Read or write at least a header before
380 * firing the callbacks. High watermark of 0
381 * to never stop reading/writing; probably to
384 /* bufferevent_setwatermark(client->bev, EV_READ|EV_WRITE, */
385 /* sizeof(struct np_msg_header), 0); */
386 bufferevent_enable(client->bev, EV_READ|EV_WRITE);
390 log_debug("%s: unexpected imsg %d", __func__,
398 listener_imsg_event_add(iev, d);
400 /* This pipe is dead. Remove its event handler. */
402 log_warnx("pipe closed, shutting down...");
403 event_loopexit(NULL);
408 listener_imsg_compose_main(int type, uint32_t peerid, const void *data,
411 return imsg_compose_event(iev_main, type, peerid, 0, -1, data,
416 listener_imsg_event_add(struct imsgev *iev, void *d)
418 iev->events = EV_READ;
419 if (iev->ibuf.w.queued)
420 iev->events |= EV_WRITE;
423 event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, d);
424 event_add(&iev->ev, NULL);
428 listener_dispatch_client(int fd, short event, void *d)
430 struct client find, *client = d;
433 struct imsgbuf *ibuf;
440 if (event & EV_READ) {
441 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
442 fatal("imsg_read error");
443 if (n == 0) /* Connection closed */
447 if (event & EV_WRITE) {
448 if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
449 fatal("msgbuf_write");
450 if (n == 0) /* Connection closed. */
455 if ((n = imsg_get(ibuf, &imsg)) == -1)
456 fatal("%s: imsg_get error", __func__);
457 if (n == 0) /* No more messages. */
460 switch (imsg.hdr.type) {
462 find.id = imsg.hdr.peerid;
463 client = SPLAY_FIND(clients_tree_id, &clients, &find);
464 if (client == NULL) {
465 log_info("got IMSG_BUF but client (%d) gone",
469 r = bufferevent_write(client->bev, imsg.data,
470 IMSG_DATA_SIZE(imsg));
472 log_warn("%s: bufferevent_write failed",
480 if (IMSG_DATA_SIZE(imsg) != sizeof(client->msize))
481 fatal("IMSG_MSIZE size mismatch: "
482 "got %zu want %zu", IMSG_DATA_SIZE(imsg),
483 sizeof(client->msize));
485 memcpy(&client->msize, imsg.data,
486 sizeof(client->msize));
488 if (client->msize == 0)
489 fatal("IMSG_MSIZE got msize = 0");
495 * Both EVBUFFER_READ or EVBUFFER_WRITE should
498 client_error(client->bev, EVBUFFER_READ, client);
502 log_debug("%s: unexpected imsg %d", __func__,
510 listener_imsg_event_add(iev, d);
512 /* This pipe is dead. Remove its handler */
513 log_debug("client proc vanished");
519 listener_imsg_compose_client(struct client *client, int type,
520 uint32_t peerid, const void *data, uint16_t len)
524 if ((ret = imsg_compose(&client->iev.ibuf, type, peerid, 0, -1,
526 listener_imsg_event_add(&client->iev, client);
531 static inline struct kd_pki_conf *
532 pki_by_name(const char *name)
534 struct kd_pki_conf *pki;
536 STAILQ_FOREACH(pki, &listener_conf->pki_head, entry) {
537 if (!strcmp(name, pki->name))
545 apply_config(struct kd_conf *conf)
547 struct kd_pki_conf *pki;
548 struct kd_listen_conf *listen;
550 listener_conf = conf;
552 /* prepare the various tls_config */
553 STAILQ_FOREACH(pki, &listener_conf->pki_head, entry) {
554 if ((pki->tlsconf = tls_config_new()) == NULL)
555 fatal("tls_config_new");
556 tls_config_verify_client_optional(pki->tlsconf);
557 tls_config_insecure_noverifycert(pki->tlsconf);
558 if (tls_config_set_keypair_mem(pki->tlsconf,
559 pki->cert, pki->certlen,
560 pki->key, pki->keylen) == -1)
561 fatalx("tls_config_set_keypair_mem: %s",
562 tls_config_error(pki->tlsconf));
565 /* prepare and kickoff the listeners */
566 STAILQ_FOREACH(listen, &listener_conf->listen_head, entry) {
567 if ((listen->ctx = tls_server()) == NULL)
570 pki = pki_by_name(listen->pki);
571 if (tls_configure(listen->ctx, pki->tlsconf) == -1)
572 fatalx("tls_configure: %s",
573 tls_config_error(pki->tlsconf));
575 event_set(&listen->ev, listen->fd, EV_READ|EV_PERSIST,
576 handle_accept, listen);
577 event_add(&listen->ev, NULL);
582 yield_r(struct client *c, void (*fn)(int, short, void *))
584 if (event_pending(&c->event, EV_WRITE|EV_READ, NULL))
585 event_del(&c->event);
586 event_set(&c->event, c->fd, EV_READ, fn, c);
587 event_add(&c->event, NULL);
591 yield_w(struct client *c, void (*fn)(int, short, void *))
593 if (event_pending(&c->event, EV_WRITE|EV_READ, NULL))
594 event_del(&c->event);
595 event_set(&c->event, c->fd, EV_WRITE, fn, c);
596 event_add(&c->event, NULL);
599 static inline uint32_t
603 # define RANDID() arc4random()
605 /* not as pretty as a random id */
606 static uint32_t counter = 0;
607 # define RANDID() counter++
610 struct client find, *res;
614 res = SPLAY_FIND(clients_tree_id, &clients, &find);
623 handle_accept(int fd, short ev, void *data)
625 struct kd_listen_conf *listen = data;
629 if ((s = accept(fd, NULL, NULL)) == -1) {
634 c = xcalloc(1, sizeof(*c));
639 if (tls_accept_socket(listen->ctx, &c->ctx, s) == -1) {
640 log_warnx("tls_accept_socket: %s",
641 tls_error(listen->ctx));
650 SPLAY_INSERT(clients_tree_id, &clients, c);
652 /* initialize the event */
653 event_set(&c->event, c->fd, EV_READ, NULL, NULL);
655 yield_r(c, handle_handshake);
659 handle_handshake(int fd, short ev, void *data)
661 struct client *c = data;
662 struct kd_auth_req auth;
666 switch (r = tls_handshake(c->ctx)) {
667 case TLS_WANT_POLLIN:
668 yield_r(c, handle_handshake);
670 case TLS_WANT_POLLOUT:
671 yield_w(c, handle_handshake);
674 log_debug("handhsake failed: %s", tls_error(c->ctx));
679 if ((hash = tls_peer_cert_hash(c->ctx)) == NULL) {
680 log_warnx("client didn't provide certificate");
685 memset(&auth, 0, sizeof(auth));
686 auth.listen_id = c->lid;
687 strlcpy(auth.hash, hash, sizeof(auth.hash));
688 log_debug("sending hash %s", auth.hash);
690 listener_imsg_compose_main(IMSG_AUTH_TLS, c->id,
691 &auth, sizeof(auth));
695 client_read(struct bufferevent *bev, void *d)
697 struct client *client = d;
698 struct evbuffer *src = EVBUFFER_INPUT(bev);
702 if (EVBUFFER_LENGTH(src) < 4)
705 memcpy(&len, EVBUFFER_DATA(src), sizeof(len));
707 log_debug("expecting a message %"PRIu32" bytes long "
708 "(of wich %zu already read)",
709 len, EVBUFFER_LENGTH(src));
711 if (len < HEADERSIZE) {
712 log_warnx("invalid message size %d (too low)", len);
713 client_error(bev, EVBUFFER_READ, client);
717 if (len > client->msize) {
718 log_warnx("incoming message bigger than msize "
719 "(%"PRIu32" vs %"PRIu32")", len, client->msize);
720 client_error(bev, EVBUFFER_READ, client);
724 if (len > EVBUFFER_LENGTH(src))
727 listener_imsg_compose_client(client, IMSG_BUF, client->id,
728 EVBUFFER_DATA(src), len);
729 evbuffer_drain(src, len);
734 client_write(struct bufferevent *bev, void *d)
737 * here we can do some fancy logic like deciding when to call
739 * (*bev->errorcb)(bev, EVBUFFER_WRITE, bev->cbarg)
741 * to signal the end of the transaction.
748 client_error(struct bufferevent *bev, short err, void *d)
750 struct client *client = d;
751 struct evbuffer *buf;
753 if (err & EVBUFFER_ERROR) {
754 if (errno == EFBIG) {
755 bufferevent_enable(bev, EV_READ);
758 log_debug("buffer event error");
763 if (err & EVBUFFER_EOF) {
768 if (err & (EVBUFFER_READ|EVBUFFER_WRITE)) {
769 bufferevent_disable(bev, EV_READ|EV_WRITE);
772 buf = EVBUFFER_OUTPUT(client->bev);
773 if (EVBUFFER_LENGTH(buf) != 0) {
774 /* finish writing all the data first */
775 bufferevent_enable(client->bev, EV_WRITE);
783 log_warnx("unknown event error, closing client connection");
788 client_tls_readcb(int fd, short event, void *d)
790 struct bufferevent *bufev = d;
791 struct client *client = bufev->cbarg;
792 char buf[IBUF_READ_SIZE];
793 int what = EVBUFFER_READ;
794 int howmuch = IBUF_READ_SIZE;
798 if (event == EV_TIMEOUT) {
799 what |= EVBUFFER_TIMEOUT;
803 if (bufev->wm_read.high != 0)
804 howmuch = MIN(sizeof(buf), bufev->wm_read.high);
806 switch (ret = tls_read(client->ctx, buf, howmuch)) {
807 case TLS_WANT_POLLIN:
808 case TLS_WANT_POLLOUT:
811 what |= EVBUFFER_ERROR;
817 what |= EVBUFFER_EOF;
821 if (evbuffer_add(bufev->input, buf, len) == -1) {
822 what |= EVBUFFER_ERROR;
826 event_add(&bufev->ev_read, NULL);
828 len = EVBUFFER_LENGTH(bufev->input);
829 if (bufev->wm_read.low != 0 && len < bufev->wm_read.low)
831 if (bufev->wm_read.high != 0 && len > bufev->wm_read.high) {
833 * here we could implement some read pressure
838 if (bufev->readcb != NULL)
839 (*bufev->readcb)(bufev, bufev->cbarg);
844 event_add(&bufev->ev_read, NULL);
848 (*bufev->errorcb)(bufev, what, bufev->cbarg);
852 client_tls_writecb(int fd, short event, void *d)
854 struct bufferevent *bufev = d;
855 struct client *client = bufev->cbarg;
858 short what = EVBUFFER_WRITE;
860 if (event == EV_TIMEOUT) {
861 what |= EVBUFFER_TIMEOUT;
865 if (EVBUFFER_LENGTH(bufev->output) != 0) {
866 ret = tls_write(client->ctx,
867 EVBUFFER_DATA(bufev->output),
868 EVBUFFER_LENGTH(bufev->output));
870 case TLS_WANT_POLLIN:
871 case TLS_WANT_POLLOUT:
874 what |= EVBUFFER_ERROR;
878 evbuffer_drain(bufev->output, len);
881 if (EVBUFFER_LENGTH(bufev->output) != 0)
882 event_add(&bufev->ev_write, NULL);
884 if (bufev->writecb != NULL &&
885 EVBUFFER_LENGTH(bufev->output) <= bufev->wm_write.low)
886 (*bufev->writecb)(bufev, bufev->cbarg);
890 event_add(&bufev->ev_write, NULL);
894 (*bufev->errorcb)(bufev, what, bufev->cbarg);
898 close_conn(struct client *c)
900 log_debug("closing connection");
902 if (c->iev.ibuf.fd != -1) {
903 listener_imsg_compose_client(c, IMSG_CONN_GONE, 0, NULL, 0);
904 imsg_flush(&c->iev.ibuf);
905 msgbuf_clear(&c->iev.ibuf.w);
906 event_del(&c->iev.ev);
907 close(c->iev.ibuf.fd);
910 handle_close(c->fd, 0, c);
914 handle_close(int fd, short ev, void *d)
916 struct client *c = d;
918 switch (tls_close(c->ctx)) {
919 case TLS_WANT_POLLIN:
920 yield_r(c, handle_close);
922 case TLS_WANT_POLLOUT:
923 yield_w(c, handle_close);
927 event_del(&c->event);