Blob


1 /*
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>
7 *
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.
11 *
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.
19 */
21 #include "compat.h"
23 #include <sys/socket.h>
24 #include <sys/types.h>
25 #include <sys/uio.h>
27 #include <endian.h>
28 #include <errno.h>
29 #include <inttypes.h>
30 #include <pwd.h>
31 #include <signal.h>
32 #include <stdint.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <syslog.h>
37 #include <unistd.h>
39 #include "control.h"
40 #include "kami.h"
41 #include "kamid.h"
42 #include "listener.h"
43 #include "log.h"
44 #include "sandbox.h"
45 #include "utils.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;
55 struct client {
56 uint32_t id;
57 uint32_t lid;
58 uint32_t lflags;
59 uint32_t msize;
60 int fd;
61 struct tls *ctx;
62 struct event event;
63 struct imsgev iev;
64 struct bufferevent *bev;
65 SPLAY_ENTRY(client) sp_entry;
66 };
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 *);
85 static inline int
86 clients_tree_cmp(struct client *a, struct client *b)
87 {
88 if (a->id == b->id)
89 return 0;
90 else if (a->id < b->id)
91 return -1;
92 else
93 return +1;
94 }
96 SPLAY_PROTOTYPE(clients_tree_id, client, sp_entry, clients_tree_cmp);
97 SPLAY_GENERATE(clients_tree_id, client, sp_entry, clients_tree_cmp)
99 static void
100 listener_sig_handler(int sig, short event, void *d)
102 /*
103 * Normal signal handler rules don't apply because libevent
104 * decouples for us.
105 */
107 switch (sig) {
108 case SIGINT:
109 case SIGTERM:
110 listener_shutdown();
111 default:
112 fatalx("unexpected signal %d", sig);
116 void
117 listener(int debug, int verbose)
119 struct event ev_sigint, ev_sigterm;
120 struct passwd *pw;
122 /* listener_conf = config_new_empty(); */
124 log_init(debug, LOG_DAEMON);
125 log_setverbose(verbose);
127 if ((pw = getpwnam(KD_USER)) == NULL)
128 fatal("getpwnam");
130 if (chroot(pw->pw_dir) == -1)
131 fatal("chroot");
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");
143 event_init();
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)
157 fatal(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);
168 sandbox_listener();
169 event_dispatch();
170 listener_shutdown();
173 __dead void
174 listener_shutdown(void)
176 msgbuf_clear(&iev_main->ibuf.w);
177 close(iev_main->ibuf.fd);
179 clear_config(listener_conf);
181 free(iev_main);
183 log_info("listener exiting");
184 exit(0);
187 static void
188 listener_receive_config(struct imsg *imsg, struct kd_conf **nconf,
189 struct kd_pki_conf **pki)
191 struct kd_listen_conf *listen;
192 char *t;
194 switch (imsg->hdr.type) {
195 case IMSG_RECONF_CONF:
196 if (*nconf != NULL)
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)
204 fatal(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);
209 break;
210 case IMSG_RECONF_PKI:
211 if (*nconf == NULL)
212 fatalx("%s: IMSG_RECONF_PKI without "
213 "IMSG_RECONF_CONF", __func__);
214 *pki = xcalloc(1, sizeof(**pki));
215 t = imsg->data;
216 t[IMSG_DATA_SIZE(*imsg)-1] = '\0';
217 strlcpy((*pki)->name, t, sizeof((*pki)->name));
218 break;
219 case IMSG_RECONF_PKI_CERT:
220 if (*pki == NULL)
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);
225 break;
226 case IMSG_RECONF_PKI_KEY:
227 if (*pki == NULL)
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);
233 pki = NULL;
234 break;
235 case IMSG_RECONF_LISTEN:
236 if (*nconf == NULL)
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",
247 __func__);
248 listen->auth_table = NULL;
249 memset(&listen->ev, 0, sizeof(listen->ev));
250 STAILQ_INSERT_HEAD(&(*nconf)->listen_head, listen, entry);
251 break;
252 case IMSG_RECONF_END:
253 if (*nconf == NULL)
254 fatalx("%s: IMSG_RECONF_END without "
255 "IMSG_RECONF_CONF", __func__);
256 apply_config(*nconf);
257 *nconf = NULL;
258 break;
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) {
268 if (l->id == id)
269 return l;
271 return NULL;
274 void
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;
281 struct imsg imsg;
282 struct imsgev *iev = d;
283 struct imsgbuf *ibuf;
284 ssize_t n;
285 int shut = 0;
287 ibuf = &iev->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. */
293 shut = 1;
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. */
299 shut = 1;
302 for (;;) {
303 if ((n = imsg_get(ibuf, &imsg)) == -1)
304 fatal("%s: imsg_get error", __func__);
305 if (n == 0) /* No more messages. */
306 break;
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,
316 imsg.hdr.type, 0,
317 &verbose, sizeof(verbose));
318 break;
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);
326 break;
327 case IMSG_AUTH:
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) {
334 if (imsg.fd != -1)
335 close(imsg.fd);
336 break;
338 if (imsg.fd == -1) {
339 log_info("got fd = -1, auth failed?");
340 close_conn(client);
341 break;
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,
353 client);
354 if (client->bev == NULL) {
355 log_info("failed to allocate client buffer");
356 close_conn(client);
357 return;
360 #if HAVE_EVENT2
361 evbuffer_unfreeze(client->bev->input, 0);
362 evbuffer_unfreeze(client->bev->output, 1);
363 #endif
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);
372 /*
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
376 * be revisited.
377 */
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);
381 break;
383 default:
384 log_debug("%s: unexpected imsg %d", __func__,
385 imsg.hdr.type);
386 break;
388 imsg_free(&imsg);
391 if (!shut)
392 listener_imsg_event_add(iev, d);
393 else {
394 /* This pipe is dead. Remove its event handler. */
395 event_del(&iev->ev);
396 log_warnx("pipe closed, shutting down...");
397 event_loopexit(NULL);
401 int
402 listener_imsg_compose_main(int type, uint32_t peerid, const void *data,
403 uint16_t datalen)
405 return imsg_compose_event(iev_main, type, peerid, 0, -1, data,
406 datalen);
409 static void
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;
416 event_del(&iev->ev);
417 event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, d);
418 event_add(&iev->ev, NULL);
421 static void
422 listener_dispatch_client(int fd, short event, void *d)
424 struct client find, *client = d;
425 struct imsg imsg;
426 struct imsgev *iev;
427 struct imsgbuf *ibuf;
428 ssize_t n;
429 int r, shut = 0;
431 iev = &client->iev;
432 ibuf = &iev->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 */
438 shut = 1;
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. */
445 shut = 1;
448 for (;;) {
449 if ((n = imsg_get(ibuf, &imsg)) == -1)
450 fatal("%s: imsg_get error", __func__);
451 if (n == 0) /* No more messages. */
452 break;
454 switch (imsg.hdr.type) {
455 case IMSG_BUF:
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",
460 imsg.hdr.peerid);
461 break;
463 r = bufferevent_write(client->bev, imsg.data,
464 IMSG_DATA_SIZE(imsg));
465 if (r == -1) {
466 log_warn("%s: bufferevent_write failed",
467 __func__);
468 close_conn(client);
469 break;
471 break;
473 case IMSG_MSIZE:
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");
485 break;
487 case IMSG_CLOSE:
488 /*
489 * Both EVBUFFER_READ or EVBUFFER_WRITE should
490 * be fine.
491 */
492 client_error(client->bev, EVBUFFER_READ, client);
493 break;
495 default:
496 log_debug("%s: unexpected imsg %d", __func__,
497 imsg.hdr.type);
498 break;
500 imsg_free(&imsg);
503 if (!shut)
504 listener_imsg_event_add(iev, d);
505 else {
506 /* This pipe is dead. Remove its handler */
507 log_debug("client proc vanished");
508 close_conn(client);
512 static int
513 listener_imsg_compose_client(struct client *client, int type,
514 uint32_t peerid, const void *data, uint16_t len)
516 int ret;
518 if ((ret = imsg_compose(&client->iev.ibuf, type, peerid, 0, -1,
519 data, len)) != -1)
520 listener_imsg_event_add(&client->iev, client);
522 return ret;
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))
532 return pki;
535 return NULL;
538 static void
539 apply_config(struct kd_conf *conf)
541 struct kd_pki_conf *pki;
542 struct kd_listen_conf *listen;
543 struct client *c;
545 /* drop any pre-auth inflight connections */
546 SPLAY_FOREACH(c, clients_tree_id, &clients) {
547 /*
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
551 * pre-auth state.
552 */
553 if (event_pending(&c->event, EV_READ|EV_WRITE, NULL) &&
554 c->bev == NULL) {
555 log_warn("closing in-flight connection due to reload");
556 close_conn(c);
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)
580 fatal("tls_server");
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);
593 static inline void
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);
602 static inline void
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);
611 static void
612 handle_accept(int fd, short ev, void *data)
614 static uint32_t counter;
615 struct kd_listen_conf *listen = data;
616 struct client *c;
617 int s;
619 if ((s = accept(fd, NULL, NULL)) == -1) {
620 log_warn("accept");
621 return;
624 c = xcalloc(1, sizeof(*c));
625 c->msize = MSIZE9P;
626 c->lid = listen->id;
627 c->lflags = listen->flags;
628 c->iev.ibuf.fd = -1;
630 if (tls_accept_socket(listen->ctx, &c->ctx, s) == -1) {
631 log_warnx("tls_accept_socket: %s",
632 tls_error(listen->ctx));
633 free(c);
634 close(s);
635 return;
638 c->fd = s;
639 c->id = counter++;
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);
649 static void
650 handle_handshake(int fd, short ev, void *data)
652 struct client *c = data;
653 struct kd_auth_req auth;
654 ssize_t r;
655 const char *hash;
657 switch (r = tls_handshake(c->ctx)) {
658 case TLS_WANT_POLLIN:
659 yield_r(c, handle_handshake);
660 return;
661 case TLS_WANT_POLLOUT:
662 yield_w(c, handle_handshake);
663 return;
664 case -1:
665 log_debug("handhsake failed: %s", tls_error(c->ctx));
666 close_conn(c);
667 return;
670 if ((hash = tls_peer_cert_hash(c->ctx)) == NULL) {
671 log_warnx("client didn't provide certificate");
672 close_conn(c);
673 return;
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));
685 static void
686 client_read(struct bufferevent *bev, void *d)
688 struct client *client = d;
689 struct evbuffer *src = EVBUFFER_INPUT(bev);
690 uint32_t len;
692 for (;;) {
693 if (EVBUFFER_LENGTH(src) < 4)
694 return;
696 memcpy(&len, EVBUFFER_DATA(src), sizeof(len));
697 len = le32toh(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);
705 return;
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);
712 return;
715 if (len > EVBUFFER_LENGTH(src))
716 return;
718 listener_imsg_compose_client(client, IMSG_BUF, client->id,
719 EVBUFFER_DATA(src), len);
720 evbuffer_drain(src, len);
724 static void
725 client_write(struct bufferevent *bev, void *d)
727 /*
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.
733 */
735 return;
738 static void
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);
747 return;
749 log_debug("buffer event error");
750 close_conn(client);
751 return;
754 if (err & EVBUFFER_EOF) {
755 close_conn(client);
756 return;
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);
766 return;
769 close_conn(client);
770 return;
773 log_warnx("unknown event error, closing client connection");
774 close_conn(client);
777 static void
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;
785 ssize_t ret;
786 size_t len;
788 if (event == EV_TIMEOUT) {
789 what |= EVBUFFER_TIMEOUT;
790 goto err;
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:
799 goto retry;
800 case -1:
801 what |= EVBUFFER_ERROR;
802 goto err;
804 len = ret;
806 if (len == 0) {
807 what |= EVBUFFER_EOF;
808 goto err;
811 if (evbuffer_add(bufev->input, buf, len) == -1) {
812 what |= EVBUFFER_ERROR;
813 goto err;
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)
820 return;
821 if (bufev->wm_read.high != 0 && len > bufev->wm_read.high) {
822 /*
823 * here we could implement some read pressure
824 * mechanism.
825 */
828 if (bufev->readcb != NULL)
829 (*bufev->readcb)(bufev, bufev->cbarg);
831 return;
833 retry:
834 event_add(&bufev->ev_read, NULL);
835 return;
837 err:
838 (*bufev->errorcb)(bufev, what, bufev->cbarg);
841 static void
842 client_tls_writecb(int fd, short event, void *d)
844 struct bufferevent *bufev = d;
845 struct client *client = bufev->cbarg;
846 ssize_t ret;
847 size_t len;
848 short what = EVBUFFER_WRITE;
850 if (event == EV_TIMEOUT) {
851 what |= EVBUFFER_TIMEOUT;
852 goto err;
855 if (EVBUFFER_LENGTH(bufev->output) != 0) {
856 ret = tls_write(client->ctx,
857 EVBUFFER_DATA(bufev->output),
858 EVBUFFER_LENGTH(bufev->output));
859 switch (ret) {
860 case TLS_WANT_POLLIN:
861 case TLS_WANT_POLLOUT:
862 goto retry;
863 case -1:
864 what |= EVBUFFER_ERROR;
865 goto err;
867 len = ret;
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);
877 return;
879 retry:
880 event_add(&bufev->ev_write, NULL);
881 return;
883 err:
884 (*bufev->errorcb)(bufev, what, bufev->cbarg);
887 static void
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);
905 static void
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);
913 return;
914 case TLS_WANT_POLLOUT:
915 yield_w(c, handle_close);
916 return;
919 event_del(&c->event);
920 tls_free(c->ctx);
921 close(c->fd);
922 free(c);