Blob


1 /*
2 * Copyright (c) 2021 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/tree.h>
26 #include <errno.h>
27 #include <inttypes.h>
28 #include <pwd.h>
29 #include <signal.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <syslog.h>
34 #include <unistd.h>
36 #include "control.h"
37 #include "kamid.h"
38 #include "listener.h"
39 #include "log.h"
40 #include "sandbox.h"
41 #include "utils.h"
43 #define MIN(a, b) ((a) < (b) ? (a) : (b))
45 static struct kd_conf *listener_conf;
46 static struct imsgev *iev_main;
48 static void listener_sig_handler(int, short, void *);
49 __dead void listener_shutdown(void);
51 SPLAY_HEAD(clients_tree_id, client) clients;
53 struct client {
54 uint32_t id;
55 int fd;
56 int done;
57 struct tls *ctx;
58 struct event event;
59 struct imsgev iev;
60 struct bufferevent *bev;
61 SPLAY_ENTRY(client) sp_entry;
62 };
64 static void listener_imsg_event_add(struct imsgev *, void *);
65 static void listener_dispatch_client(int, short, void *);
66 static int listener_imsg_compose_client(struct client *, int,
67 uint32_t, const void *, uint16_t);
69 static void apply_config(struct kd_conf *);
70 static void handle_accept(int, short, void *);
72 static void handle_handshake(int, short, void *);
73 static void client_read(struct bufferevent *, void *);
74 static void client_write(struct bufferevent *, void *);
75 static void client_error(struct bufferevent *, short, void *);
76 static void client_tls_readcb(int, short, void *);
77 static void client_tls_writecb(int, short, void *);
78 static void close_conn(struct client *);
79 static void handle_close(int, short, void *);
81 static inline int
82 clients_tree_cmp(struct client *a, struct client *b)
83 {
84 if (a->id == b->id)
85 return 0;
86 else if (a->id < b->id)
87 return -1;
88 else
89 return +1;
90 }
92 SPLAY_PROTOTYPE(clients_tree_id, client, sp_entry, clients_tree_cmp);
93 SPLAY_GENERATE(clients_tree_id, client, sp_entry, clients_tree_cmp)
95 static void
96 listener_sig_handler(int sig, short event, void *d)
97 {
98 /*
99 * Normal signal handler rules don't apply because libevent
100 * decouples for us.
101 */
103 switch (sig) {
104 case SIGINT:
105 case SIGTERM:
106 listener_shutdown();
107 default:
108 fatalx("unexpected signal %d", sig);
112 void
113 listener(int debug, int verbose)
115 struct event ev_sigint, ev_sigterm;
116 struct passwd *pw;
118 /* listener_conf = config_new_empty(); */
120 log_init(debug, LOG_DAEMON);
121 log_setverbose(verbose);
123 if ((pw = getpwnam(KD_USER)) == NULL)
124 fatal("getpwnam");
126 if (chroot(pw->pw_dir) == -1)
127 fatal("chroot");
128 if (chdir("/") == -1)
129 fatal("chdir(\"/\")");
131 setproctitle("listener");
132 log_procinit("listener");
134 if (setgroups(1, &pw->pw_gid) ||
135 setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) ||
136 setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid))
137 fatal("can't drop privileges");
139 event_init();
141 /* Setup signal handlers(s). */
142 signal_set(&ev_sigint, SIGINT, listener_sig_handler, NULL);
143 signal_set(&ev_sigterm, SIGTERM, listener_sig_handler, NULL);
145 signal_add(&ev_sigint, NULL);
146 signal_add(&ev_sigterm, NULL);
148 signal(SIGPIPE, SIG_IGN);
149 signal(SIGHUP, SIG_IGN);
151 /* Setup pipe and event handler to the main process. */
152 if ((iev_main = malloc(sizeof(*iev_main))) == NULL)
153 fatal(NULL);
155 imsg_init(&iev_main->ibuf, 3);
156 iev_main->handler = listener_dispatch_main;
158 /* Setup event handlers. */
159 iev_main->events = EV_READ;
160 event_set(&iev_main->ev, iev_main->ibuf.fd, iev_main->events,
161 iev_main->handler, iev_main);
162 event_add(&iev_main->ev, NULL);
164 sandbox_listener();
165 event_dispatch();
166 listener_shutdown();
169 __dead void
170 listener_shutdown(void)
172 msgbuf_clear(&iev_main->ibuf.w);
173 close(iev_main->ibuf.fd);
175 config_clear(listener_conf);
177 free(iev_main);
179 log_info("listener exiting");
180 exit(0);
183 static void
184 listener_receive_config(struct imsg *imsg, struct kd_conf **nconf,
185 struct kd_pki_conf **pki)
187 struct kd_listen_conf *listen;
188 char *t;
190 switch (imsg->hdr.type) {
191 case IMSG_RECONF_CONF:
192 if (*nconf != NULL)
193 fatalx("%s: IMSG_RECONF_CONF already in "
194 "progress", __func__);
196 if (listener_conf != NULL)
197 fatalx("%s: don't know how reload the "
198 "configuration yet", __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 memset(&(*nconf)->pki_head, 0, sizeof((*nconf)->pki_head));
207 memset(&(*nconf)->table_head, 0, sizeof((*nconf)->table_head));
208 memset(&(*nconf)->listen_head, 0, sizeof((*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 SIMPLEQ_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 SIMPLEQ_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 /* merge_config(listener_conf, nconf); */
257 apply_config(*nconf);
258 *nconf = NULL;
259 break;
263 void
264 listener_dispatch_main(int fd, short event, void *d)
266 static struct kd_conf *nconf;
267 static struct kd_pki_conf *pki;
268 struct client *client, find;
269 struct imsg imsg;
270 struct imsgev *iev = d;
271 struct imsgbuf *ibuf;
272 ssize_t n;
273 int shut = 0;
275 ibuf = &iev->ibuf;
277 if (event & EV_READ) {
278 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
279 fatal("imsg_read error");
280 if (n == 0) /* Connection closed. */
281 shut = 1;
283 if (event & EV_WRITE) {
284 if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
285 fatal("msgbuf_write");
286 if (n == 0) /* Connection closed. */
287 shut = 1;
290 for (;;) {
291 if ((n = imsg_get(ibuf, &imsg)) == -1)
292 fatal("%s: imsg_get error", __func__);
293 if (n == 0) /* No more messages. */
294 break;
296 switch (imsg.hdr.type) {
297 case IMSG_CONTROLFD:
298 if ((fd = imsg.fd) == -1)
299 fatalx("%s: expected to receive imsg "
300 "control fd but didn't receive any",
301 __func__);
302 /* Listen on control socket. */
303 control_listen(fd);
304 break;
305 case IMSG_RECONF_CONF:
306 case IMSG_RECONF_PKI:
307 case IMSG_RECONF_PKI_CERT:
308 case IMSG_RECONF_PKI_KEY:
309 case IMSG_RECONF_LISTEN:
310 case IMSG_RECONF_END:
311 listener_receive_config(&imsg, &nconf, &pki);
312 break;
313 case IMSG_AUTH:
314 find.id = imsg.hdr.peerid;
315 client = SPLAY_FIND(clients_tree_id, &clients, &find);
316 if (client == NULL) {
317 if (imsg.fd == -1)
318 close(imsg.fd);
319 break;
321 if (imsg.fd == -1) {
322 close_conn(client);
323 break;
325 imsg_init(&client->iev.ibuf, imsg.fd);
326 client->iev.events = EV_READ;
327 client->iev.handler = listener_dispatch_client;
328 event_set(&client->iev.ev, client->iev.ibuf.fd,
329 client->iev.events, client->iev.handler, client);
330 listener_imsg_compose_client(client, IMSG_AUTH,
331 0, imsg.data, IMSG_DATA_SIZE(imsg));
332 break;
333 case IMSG_AUTH_DIR:
334 find.id = imsg.hdr.peerid;
335 client = SPLAY_FIND(clients_tree_id, &clients, &find);
336 if (client == NULL) {
337 log_info("got AUTH_DIR but client gone");
338 break;
340 listener_imsg_compose_client(client, IMSG_AUTH_DIR,
341 0, imsg.data, IMSG_DATA_SIZE(imsg));
342 client->bev = bufferevent_new(client->fd,
343 client_read, client_write, client_error,
344 client);
345 if (client->bev == NULL) {
346 log_info("failed to allocate client buffer");
347 close_conn(client);
348 return;
350 event_set(&client->bev->ev_read, client->fd, EV_READ,
351 client_tls_readcb, client->bev);
352 event_set(&client->bev->ev_write, client->fd, EV_WRITE,
353 client_tls_writecb, client->bev);
355 /* TODO: adjust watermarks */
356 bufferevent_setwatermark(client->bev, EV_WRITE, 1, 0);
357 bufferevent_setwatermark(client->bev, EV_READ, 1, 0);
359 bufferevent_enable(client->bev, EV_READ|EV_WRITE);
360 break;
361 default:
362 log_debug("%s: unexpected imsg %d", __func__,
363 imsg.hdr.type);
364 break;
366 imsg_free(&imsg);
369 if (!shut)
370 listener_imsg_event_add(iev, d);
371 else {
372 /* This pipe is dead. Remove its event handler. */
373 event_del(&iev->ev);
374 log_warnx("pipe closed, shutting down...");
375 event_loopexit(NULL);
379 int
380 listener_imsg_compose_main(int type, uint32_t peerid, const void *data,
381 uint16_t datalen)
383 return imsg_compose_event(iev_main, type, peerid, 0, -1, data,
384 datalen);
387 static void
388 listener_imsg_event_add(struct imsgev *iev, void *d)
390 iev->events = EV_READ;
391 if (iev->ibuf.w.queued)
392 iev->events |= EV_WRITE;
394 event_del(&iev->ev);
395 event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, d);
396 event_add(&iev->ev, NULL);
399 static void
400 listener_dispatch_client(int fd, short event, void *d)
402 struct client find, *client = d;
403 struct imsg imsg;
404 struct imsgev *iev;
405 struct imsgbuf *ibuf;
406 ssize_t n;
407 int r, shut = 0;
409 iev = &client->iev;
410 ibuf = &iev->ibuf;
412 if (event & EV_READ) {
413 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
414 fatal("imsg_read error");
415 if (n == 0) /* Connection closed */
416 shut = 1;
419 if (event & EV_WRITE) {
420 if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
421 fatal("msgbuf_write");
422 if (n == 0) /* Connection closed. */
423 shut = 1;
426 for (;;) {
427 if ((n = imsg_get(ibuf, &imsg)) == -1)
428 fatal("%s: imsg_get error", __func__);
429 if (n == 0) /* No more messages. */
430 break;
432 switch (imsg.hdr.type) {
433 case IMSG_BUF:
434 find.id = imsg.hdr.peerid;
435 client = SPLAY_FIND(clients_tree_id, &clients, &find);
436 if (client == NULL) {
437 log_info("got IMSG_BUF but client (%d) gone",
438 imsg.hdr.peerid);
439 break;
441 r = bufferevent_write(client->bev, imsg.data,
442 IMSG_DATA_SIZE(imsg));
443 if (r == -1) {
444 log_warn("%s: bufferevent_write failed",
445 __func__);
446 close_conn(client);
447 break;
449 break;
450 default:
451 log_debug("%s: unexpected imsg %d", __func__,
452 imsg.hdr.type);
453 break;
455 imsg_free(&imsg);
458 if (!shut)
459 listener_imsg_event_add(iev, d);
460 else {
461 /* This pipe is dead. Remove its handler */
462 log_debug("client proc vanished");
463 close_conn(client);
467 static int
468 listener_imsg_compose_client(struct client *client, int type,
469 uint32_t peerid, const void *data, uint16_t len)
471 int ret;
473 if ((ret = imsg_compose(&client->iev.ibuf, type, peerid, 0, -1,
474 data, len)) != -1)
475 listener_imsg_event_add(&client->iev, client);
477 return ret;
480 static inline struct kd_pki_conf *
481 pki_by_name(const char *name)
483 struct kd_pki_conf *pki;
485 SIMPLEQ_FOREACH(pki, &listener_conf->pki_head, entry) {
486 if (!strcmp(name, pki->name))
487 return pki;
490 return NULL;
493 static void
494 apply_config(struct kd_conf *conf)
496 struct kd_pki_conf *pki;
497 struct kd_listen_conf *listen;
499 listener_conf = conf;
501 /* prepare the various tls_config */
502 SIMPLEQ_FOREACH(pki, &listener_conf->pki_head, entry) {
503 if ((pki->tlsconf = tls_config_new()) == NULL)
504 fatal("tls_config_new");
505 tls_config_verify_client_optional(pki->tlsconf);
506 tls_config_insecure_noverifycert(pki->tlsconf);
507 if (tls_config_set_keypair_mem(pki->tlsconf,
508 pki->cert, pki->certlen,
509 pki->key, pki->keylen) == -1)
510 fatalx("tls_config_set_keypair_mem: %s",
511 tls_config_error(pki->tlsconf));
514 /* prepare and kickoff the listeners */
515 SIMPLEQ_FOREACH(listen, &listener_conf->listen_head, entry) {
516 if ((listen->ctx = tls_server()) == NULL)
517 fatal("tls_server");
519 pki = pki_by_name(listen->pki);
520 if (tls_configure(listen->ctx, pki->tlsconf) == -1)
521 fatalx("tls_configure: %s",
522 tls_config_error(pki->tlsconf));
524 event_set(&listen->ev, listen->fd, EV_READ|EV_PERSIST,
525 handle_accept, listen);
526 event_add(&listen->ev, NULL);
530 static inline void
531 yield_r(struct client *c, void (*fn)(int, short, void *))
533 if (event_pending(&c->event, EV_WRITE|EV_READ, NULL))
534 event_del(&c->event);
535 event_set(&c->event, c->fd, EV_READ, fn, c);
536 event_add(&c->event, NULL);
539 static inline void
540 yield_w(struct client *c, void (*fn)(int, short, void *))
542 if (event_pending(&c->event, EV_WRITE|EV_READ, NULL))
543 event_del(&c->event);
544 event_set(&c->event, c->fd, EV_WRITE, fn, c);
545 event_add(&c->event, NULL);
548 static inline uint32_t
549 random_id(void)
551 struct client find, *res;
553 for (;;) {
554 find.id = arc4random();
555 res = SPLAY_FIND(clients_tree_id, &clients, &find);
556 if (res == NULL)
557 return find.id;
561 static void
562 handle_accept(int fd, short ev, void *data)
564 struct kd_listen_conf *listen = data;
565 struct client *c;
566 int s;
568 if ((s = accept(fd, NULL, NULL)) == -1) {
569 log_warn("accept");
570 return;
573 c = xcalloc(1, sizeof(*c));
574 c->iev.ibuf.fd = -1;
576 if (tls_accept_socket(listen->ctx, &c->ctx, s) == -1) {
577 log_warnx("tls_accept_socket: %s",
578 tls_error(listen->ctx));
579 free(c);
580 close(s);
581 return;
584 c->fd = s;
585 c->id = random_id();
587 SPLAY_INSERT(clients_tree_id, &clients, c);
589 /* initialize the event */
590 event_set(&c->event, c->fd, EV_READ, NULL, NULL);
592 yield_r(c, handle_handshake);
595 static void
596 handle_handshake(int fd, short ev, void *data)
598 struct client *c = data;
599 ssize_t r;
600 const char *hash;
602 switch (r = tls_handshake(c->ctx)) {
603 case TLS_WANT_POLLIN:
604 yield_r(c, handle_handshake);
605 return;
606 case TLS_WANT_POLLOUT:
607 yield_w(c, handle_handshake);
608 return;
609 case -1:
610 log_debug("handhsake failed: %s", tls_error(c->ctx));
611 close_conn(c);
612 return;
615 if ((hash = tls_peer_cert_hash(c->ctx)) == NULL) {
616 log_warnx("client didn't provide certificate");
617 close_conn(c);
618 return;
621 listener_imsg_compose_main(IMSG_AUTH_TLS, c->id,
622 hash, strlen(hash)+1);
625 static void
626 client_read(struct bufferevent *bev, void *d)
628 struct client *client = d;
629 struct evbuffer *src = EVBUFFER_INPUT(bev);
630 char buf[BUFSIZ];
631 size_t len;
633 if (!EVBUFFER_LENGTH(src))
634 return;
636 len = bufferevent_read(bev, buf, sizeof(buf));
637 if (len == 0) {
638 (*bev->errorcb)(bev, EVBUFFER_READ, bev->cbarg);
639 return;
642 listener_imsg_compose_client(client, IMSG_BUF, client->id, buf, len);
645 static void
646 client_write(struct bufferevent *bev, void *d)
648 /*
649 * here we can do some fancy logic like deciding when to call
651 * (*bev->errorcb)(bev, EVBUFFER_WRITE, bev->cbarg)
653 * to signal the end of the transaction.
654 */
656 return;
659 static void
660 client_error(struct bufferevent *bev, short err, void *d)
662 struct client *client = d;
663 struct evbuffer *buf;
665 if (err & EVBUFFER_ERROR) {
666 if (errno == EFBIG) {
667 bufferevent_enable(bev, EV_READ);
668 return;
670 log_debug("buffer event error");
671 close_conn(client);
672 return;
675 if (err & EVBUFFER_EOF) {
676 close_conn(client);
677 return;
680 if (err & (EVBUFFER_READ|EVBUFFER_WRITE)) {
681 bufferevent_disable(bev, EV_READ|EV_WRITE);
682 client->done = 1;
684 buf = EVBUFFER_OUTPUT(client->bev);
685 if (EVBUFFER_LENGTH(buf) != 0) {
686 /* finish writing all the data first */
687 bufferevent_enable(client->bev, EV_WRITE);
688 return;
691 close_conn(client);
692 return;
695 log_warnx("unknown event error, closing client connection");
696 close_conn(client);
699 static void
700 client_tls_readcb(int fd, short event, void *d)
702 struct bufferevent *bufev = d;
703 struct client *client = bufev->cbarg;
704 char buf[BUFSIZ];
705 int what = EVBUFFER_READ;
706 int howmuch = IBUF_READ_SIZE;
707 ssize_t ret;
708 size_t len;
710 if (event == EV_TIMEOUT) {
711 what |= EVBUFFER_TIMEOUT;
712 goto err;
715 if (bufev->wm_read.high != 0)
716 howmuch = MIN(sizeof(buf), bufev->wm_read.high);
718 switch (ret = tls_read(client->ctx, buf, howmuch)) {
719 case TLS_WANT_POLLIN:
720 case TLS_WANT_POLLOUT:
721 goto retry;
722 case -1:
723 what |= EVBUFFER_ERROR;
724 goto err;
726 len = ret;
728 if (len == 0) {
729 what |= EVBUFFER_EOF;
730 goto err;
733 if (evbuffer_add(bufev->input, buf, len) == -1) {
734 what |= EVBUFFER_ERROR;
735 goto err;
738 event_add(&bufev->ev_read, NULL);
740 len = EVBUFFER_LENGTH(bufev->input);
741 if (bufev->wm_read.low != 0 && len < bufev->wm_read.low)
742 return;
743 if (bufev->wm_read.high != 0 && len > bufev->wm_read.high) {
744 /*
745 * here we could implement some read pressure
746 * mechanism.
747 */
750 if (bufev->readcb != NULL)
751 (*bufev->readcb)(bufev, bufev->cbarg);
753 return;
755 retry:
756 event_add(&bufev->ev_read, NULL);
757 return;
759 err:
760 (*bufev->errorcb)(bufev, what, bufev->cbarg);
763 static void
764 client_tls_writecb(int fd, short event, void *d)
766 struct bufferevent *bufev = d;
767 struct client *client = bufev->cbarg;
768 ssize_t ret;
769 size_t len;
770 short what = EVBUFFER_WRITE;
772 if (event == EV_TIMEOUT) {
773 what |= EVBUFFER_TIMEOUT;
774 goto err;
777 if (EVBUFFER_LENGTH(bufev->output) != 0) {
778 ret = tls_write(client->ctx,
779 EVBUFFER_DATA(bufev->output),
780 EVBUFFER_LENGTH(bufev->output));
781 switch (ret) {
782 case TLS_WANT_POLLIN:
783 case TLS_WANT_POLLOUT:
784 goto retry;
785 case -1:
786 what |= EVBUFFER_ERROR;
787 goto err;
789 len = ret;
790 evbuffer_drain(bufev->output, len);
793 if (EVBUFFER_LENGTH(bufev->output) != 0)
794 event_add(&bufev->ev_write, NULL);
796 if (bufev->writecb != NULL &&
797 EVBUFFER_LENGTH(bufev->output) <= bufev->wm_write.low)
798 (*bufev->writecb)(bufev, bufev->cbarg);
799 return;
801 retry:
802 event_add(&bufev->ev_write, NULL);
803 return;
805 err:
806 (*bufev->errorcb)(bufev, what, bufev->cbarg);
809 static void
810 close_conn(struct client *c)
812 log_debug("closing connection");
814 if (c->iev.ibuf.fd != -1) {
815 listener_imsg_compose_client(c, IMSG_CONN_GONE, 0, NULL, 0);
816 imsg_flush(&c->iev.ibuf);
817 msgbuf_clear(&c->iev.ibuf.w);
818 event_del(&c->iev.ev);
819 close(c->iev.ibuf.fd);
822 handle_close(c->fd, 0, c);
825 static void
826 handle_close(int fd, short ev, void *d)
828 struct client *c = d;
830 switch (tls_close(c->ctx)) {
831 case TLS_WANT_POLLIN:
832 yield_r(c, handle_close);
833 return;
834 case TLS_WANT_POLLOUT:
835 yield_w(c, handle_close);
836 return;
839 event_del(&c->event);
840 tls_free(c->ctx);
841 close(c->fd);
842 free(c);