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 void
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;
268 struct imsg imsg;
269 struct imsgev *iev = d;
270 struct imsgbuf *ibuf;
271 ssize_t n;
272 int shut = 0;
274 ibuf = &iev->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. */
280 shut = 1;
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. */
286 shut = 1;
289 for (;;) {
290 if ((n = imsg_get(ibuf, &imsg)) == -1)
291 fatal("%s: imsg_get error", __func__);
292 if (n == 0) /* No more messages. */
293 break;
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,
303 imsg.hdr.type, 0,
304 &verbose, sizeof(verbose));
305 break;
306 case IMSG_CTL_DEBUG:
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));
314 break;
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);
322 break;
323 case IMSG_AUTH:
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) {
330 if (imsg.fd != -1)
331 close(imsg.fd);
332 break;
334 if (imsg.fd == -1) {
335 log_info("got fd = -1, auth failed?");
336 close_conn(client);
337 break;
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,
349 client);
350 if (client->bev == NULL) {
351 log_info("failed to allocate client buffer");
352 close_conn(client);
353 return;
356 #if HAVE_EVENT2
357 evbuffer_unfreeze(client->bev->input, 0);
358 evbuffer_unfreeze(client->bev->output, 1);
359 #endif
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);
368 /*
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
372 * be revisited.
373 */
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);
377 break;
379 default:
380 log_debug("%s: unexpected imsg %d", __func__,
381 imsg.hdr.type);
382 break;
384 imsg_free(&imsg);
387 if (!shut)
388 listener_imsg_event_add(iev, d);
389 else {
390 /* This pipe is dead. Remove its event handler. */
391 event_del(&iev->ev);
392 log_warnx("pipe closed, shutting down...");
393 event_loopexit(NULL);
397 int
398 listener_imsg_compose_main(int type, uint32_t peerid, const void *data,
399 uint16_t datalen)
401 return imsg_compose_event(iev_main, type, peerid, 0, -1, data,
402 datalen);
405 static void
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;
412 event_del(&iev->ev);
413 event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, d);
414 event_add(&iev->ev, NULL);
417 static void
418 listener_dispatch_client(int fd, short event, void *d)
420 struct client find, *client = d;
421 struct imsg imsg;
422 struct imsgev *iev;
423 struct imsgbuf *ibuf;
424 ssize_t n;
425 int r, shut = 0;
427 iev = &client->iev;
428 ibuf = &iev->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 */
434 shut = 1;
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. */
441 shut = 1;
444 for (;;) {
445 if ((n = imsg_get(ibuf, &imsg)) == -1)
446 fatal("%s: imsg_get error", __func__);
447 if (n == 0) /* No more messages. */
448 break;
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));
455 break;
457 case IMSG_BUF:
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",
462 imsg.hdr.peerid);
463 break;
465 r = bufferevent_write(client->bev, imsg.data,
466 IMSG_DATA_SIZE(imsg));
467 if (r == -1) {
468 log_warn("%s: bufferevent_write failed",
469 __func__);
470 close_conn(client);
471 break;
473 break;
475 case IMSG_MSIZE:
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");
487 break;
489 case IMSG_CLOSE:
490 /*
491 * Both EVBUFFER_READ or EVBUFFER_WRITE should
492 * be fine.
493 */
494 client_error(client->bev, EVBUFFER_READ, client);
495 break;
497 default:
498 log_debug("%s: unexpected imsg %d", __func__,
499 imsg.hdr.type);
500 break;
502 imsg_free(&imsg);
505 if (!shut)
506 listener_imsg_event_add(iev, d);
507 else {
508 /* This pipe is dead. Remove its handler */
509 log_debug("client proc vanished");
510 close_conn(client);
514 static int
515 listener_imsg_compose_client(struct client *client, int type,
516 uint32_t peerid, const void *data, uint16_t len)
518 int ret;
520 if ((ret = imsg_compose(&client->iev.ibuf, type, peerid, 0, -1,
521 data, len)) != -1)
522 listener_imsg_event_add(&client->iev, client);
524 return ret;
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))
534 return pki;
537 return NULL;
540 static void
541 apply_config(struct kd_conf *conf)
543 struct kd_pki_conf *pki;
544 struct kd_listen_conf *listen;
545 struct client *c;
547 /* drop any pre-auth inflight connections */
548 SPLAY_FOREACH(c, clients_tree_id, &clients) {
549 /*
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
553 * pre-auth state.
554 */
555 if (event_pending(&c->event, EV_READ|EV_WRITE, NULL) &&
556 c->bev == NULL) {
557 log_warn("closing in-flight connection due to reload");
558 close_conn(c);
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)
582 fatal("tls_server");
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);
595 static inline void
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);
604 static inline void
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);
613 static void
614 handle_accept(int fd, short ev, void *data)
616 static uint32_t counter;
617 struct kd_listen_conf *listen = data;
618 struct client *c;
619 int s;
621 if ((s = accept(fd, NULL, NULL)) == -1) {
622 log_warn("accept");
623 return;
626 c = xcalloc(1, sizeof(*c));
627 c->msize = MSIZE9P;
628 c->lid = listen->id;
629 c->lflags = listen->flags;
630 c->iev.ibuf.fd = -1;
632 if (tls_accept_socket(listen->ctx, &c->ctx, s) == -1) {
633 log_warnx("tls_accept_socket: %s",
634 tls_error(listen->ctx));
635 free(c);
636 close(s);
637 return;
640 c->fd = s;
641 c->id = counter++;
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);
651 static void
652 handle_handshake(int fd, short ev, void *data)
654 struct client *c = data;
655 struct kd_auth_req auth;
656 ssize_t r;
657 const char *hash;
659 switch (r = tls_handshake(c->ctx)) {
660 case TLS_WANT_POLLIN:
661 yield_r(c, handle_handshake);
662 return;
663 case TLS_WANT_POLLOUT:
664 yield_w(c, handle_handshake);
665 return;
666 case -1:
667 log_debug("handhsake failed: %s", tls_error(c->ctx));
668 close_conn(c);
669 return;
672 if ((hash = tls_peer_cert_hash(c->ctx)) == NULL) {
673 log_warnx("client didn't provide certificate");
674 close_conn(c);
675 return;
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));
687 static void
688 client_read(struct bufferevent *bev, void *d)
690 struct client *client = d;
691 struct evbuffer *src = EVBUFFER_INPUT(bev);
692 uint32_t len;
694 for (;;) {
695 if (EVBUFFER_LENGTH(src) < 4)
696 return;
698 memcpy(&len, EVBUFFER_DATA(src), sizeof(len));
699 len = le32toh(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);
707 return;
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);
714 return;
717 if (len > EVBUFFER_LENGTH(src))
718 return;
720 listener_imsg_compose_client(client, IMSG_BUF, client->id,
721 EVBUFFER_DATA(src), len);
722 evbuffer_drain(src, len);
726 static void
727 client_write(struct bufferevent *bev, void *d)
729 /*
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.
735 */
737 return;
740 static void
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);
749 return;
751 log_debug("buffer event error");
752 close_conn(client);
753 return;
756 if (err & EVBUFFER_EOF) {
757 close_conn(client);
758 return;
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);
768 return;
771 close_conn(client);
772 return;
775 log_warnx("unknown event error, closing client connection");
776 close_conn(client);
779 static void
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;
787 ssize_t ret;
788 size_t len;
790 if (event == EV_TIMEOUT) {
791 what |= EVBUFFER_TIMEOUT;
792 goto err;
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:
801 goto retry;
802 case -1:
803 what |= EVBUFFER_ERROR;
804 goto err;
806 len = ret;
808 if (len == 0) {
809 what |= EVBUFFER_EOF;
810 goto err;
813 if (evbuffer_add(bufev->input, buf, len) == -1) {
814 what |= EVBUFFER_ERROR;
815 goto err;
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)
822 return;
823 if (bufev->wm_read.high != 0 && len > bufev->wm_read.high) {
824 /*
825 * here we could implement some read pressure
826 * mechanism.
827 */
830 if (bufev->readcb != NULL)
831 (*bufev->readcb)(bufev, bufev->cbarg);
833 return;
835 retry:
836 event_add(&bufev->ev_read, NULL);
837 return;
839 err:
840 (*bufev->errorcb)(bufev, what, bufev->cbarg);
843 static void
844 client_tls_writecb(int fd, short event, void *d)
846 struct bufferevent *bufev = d;
847 struct client *client = bufev->cbarg;
848 ssize_t ret;
849 size_t len;
850 short what = EVBUFFER_WRITE;
852 if (event == EV_TIMEOUT) {
853 what |= EVBUFFER_TIMEOUT;
854 goto err;
857 if (EVBUFFER_LENGTH(bufev->output) != 0) {
858 ret = tls_write(client->ctx,
859 EVBUFFER_DATA(bufev->output),
860 EVBUFFER_LENGTH(bufev->output));
861 switch (ret) {
862 case TLS_WANT_POLLIN:
863 case TLS_WANT_POLLOUT:
864 goto retry;
865 case -1:
866 what |= EVBUFFER_ERROR;
867 goto err;
869 len = ret;
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);
879 return;
881 retry:
882 event_add(&bufev->ev_write, NULL);
883 return;
885 err:
886 (*bufev->errorcb)(bufev, what, bufev->cbarg);
889 static void
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);
907 static void
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);
915 return;
916 case TLS_WANT_POLLOUT:
917 yield_w(c, handle_close);
918 return;
921 event_del(&c->event);
922 tls_free(c->ctx);
923 close(c->fd);
924 free(c);