2 1e84c7b7 2022-01-10 op * Copyright (c) 2021, 2022 Omar Polo <op@omarpolo.com>
3 fb1a36c0 2022-01-09 op * Copyright (c) 2018 Florian Obser <florian@openbsd.org>
4 fb1a36c0 2022-01-09 op * Copyright (c) 2004, 2005 Claudio Jeker <claudio@openbsd.org>
5 fb1a36c0 2022-01-09 op * Copyright (c) 2004 Esben Norby <norby@openbsd.org>
6 fb1a36c0 2022-01-09 op * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
8 fb1a36c0 2022-01-09 op * Permission to use, copy, modify, and distribute this software for any
9 fb1a36c0 2022-01-09 op * purpose with or without fee is hereby granted, provided that the above
10 fb1a36c0 2022-01-09 op * copyright notice and this permission notice appear in all copies.
12 fb1a36c0 2022-01-09 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 fb1a36c0 2022-01-09 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 fb1a36c0 2022-01-09 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 fb1a36c0 2022-01-09 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 fb1a36c0 2022-01-09 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 fb1a36c0 2022-01-09 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 fb1a36c0 2022-01-09 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 bbcba3ed 2022-01-10 op #include "compat.h"
23 fb1a36c0 2022-01-09 op #include <sys/socket.h>
24 fb1a36c0 2022-01-09 op #include <sys/types.h>
25 fb1a36c0 2022-01-09 op #include <sys/uio.h>
27 fb1a36c0 2022-01-09 op #include <errno.h>
28 eda58043 2022-12-21 op #include <grp.h>
29 fb1a36c0 2022-01-09 op #include <inttypes.h>
30 fb1a36c0 2022-01-09 op #include <pwd.h>
31 fb1a36c0 2022-01-09 op #include <signal.h>
32 fb1a36c0 2022-01-09 op #include <stdint.h>
33 fb1a36c0 2022-01-09 op #include <stdio.h>
34 fb1a36c0 2022-01-09 op #include <stdlib.h>
35 fb1a36c0 2022-01-09 op #include <string.h>
36 fb1a36c0 2022-01-09 op #include <syslog.h>
37 fb1a36c0 2022-01-09 op #include <unistd.h>
39 fb1a36c0 2022-01-09 op #include "control.h"
40 fb1a36c0 2022-01-09 op #include "kami.h"
41 fb1a36c0 2022-01-09 op #include "kamid.h"
42 fb1a36c0 2022-01-09 op #include "listener.h"
43 fb1a36c0 2022-01-09 op #include "log.h"
44 fb1a36c0 2022-01-09 op #include "sandbox.h"
45 fb1a36c0 2022-01-09 op #include "utils.h"
47 8b0a5062 2022-05-22 op #define IMSG_MAXSIZE (MAX_IMSGSIZE - IMSG_HEADER_SIZE)
49 fb1a36c0 2022-01-09 op static struct kd_conf *listener_conf;
50 fb1a36c0 2022-01-09 op static struct imsgev *iev_main;
52 fb1a36c0 2022-01-09 op static void listener_sig_handler(int, short, void *);
53 fb1a36c0 2022-01-09 op __dead void listener_shutdown(void);
55 fb1a36c0 2022-01-09 op SPLAY_HEAD(clients_tree_id, client) clients;
57 fb1a36c0 2022-01-09 op struct client {
60 38a6fd3d 2022-01-28 op uint32_t lflags;
61 fb1a36c0 2022-01-09 op uint32_t msize;
62 8b0a5062 2022-05-22 op uint32_t left;
64 fb1a36c0 2022-01-09 op struct tls *ctx;
65 fb1a36c0 2022-01-09 op struct event event;
66 fb1a36c0 2022-01-09 op struct imsgev iev;
67 fb1a36c0 2022-01-09 op struct bufferevent *bev;
68 fb1a36c0 2022-01-09 op SPLAY_ENTRY(client) sp_entry;
71 fb1a36c0 2022-01-09 op static void listener_imsg_event_add(struct imsgev *, void *);
72 fb1a36c0 2022-01-09 op static void listener_dispatch_client(int, short, void *);
73 fb1a36c0 2022-01-09 op static int listener_imsg_compose_client(struct client *, int,
74 fb1a36c0 2022-01-09 op uint32_t, const void *, uint16_t);
76 fb1a36c0 2022-01-09 op static void apply_config(struct kd_conf *);
77 fb1a36c0 2022-01-09 op static void handle_accept(int, short, void *);
79 fb1a36c0 2022-01-09 op static void handle_handshake(int, short, void *);
80 fb1a36c0 2022-01-09 op static void client_read(struct bufferevent *, void *);
81 fb1a36c0 2022-01-09 op static void client_write(struct bufferevent *, void *);
82 fb1a36c0 2022-01-09 op static void client_error(struct bufferevent *, short, void *);
83 fb1a36c0 2022-01-09 op static void client_tls_readcb(int, short, void *);
84 fb1a36c0 2022-01-09 op static void client_tls_writecb(int, short, void *);
85 fb1a36c0 2022-01-09 op static void close_conn(struct client *);
86 fb1a36c0 2022-01-09 op static void handle_close(int, short, void *);
88 fb1a36c0 2022-01-09 op static inline int
89 fb1a36c0 2022-01-09 op clients_tree_cmp(struct client *a, struct client *b)
91 fb1a36c0 2022-01-09 op if (a->id == b->id)
93 fb1a36c0 2022-01-09 op else if (a->id < b->id)
99 fb1a36c0 2022-01-09 op SPLAY_PROTOTYPE(clients_tree_id, client, sp_entry, clients_tree_cmp);
100 5745ff21 2022-12-21 op SPLAY_GENERATE(clients_tree_id, client, sp_entry, clients_tree_cmp);
103 fb1a36c0 2022-01-09 op listener_sig_handler(int sig, short event, void *d)
106 fb1a36c0 2022-01-09 op * Normal signal handler rules don't apply because libevent
107 fb1a36c0 2022-01-09 op * decouples for us.
110 fb1a36c0 2022-01-09 op switch (sig) {
112 fb1a36c0 2022-01-09 op case SIGTERM:
113 fb1a36c0 2022-01-09 op listener_shutdown();
115 fb1a36c0 2022-01-09 op fatalx("unexpected signal %d", sig);
120 fb1a36c0 2022-01-09 op listener(int debug, int verbose)
122 fb1a36c0 2022-01-09 op struct event ev_sigint, ev_sigterm;
123 fb1a36c0 2022-01-09 op struct passwd *pw;
125 fb1a36c0 2022-01-09 op /* listener_conf = config_new_empty(); */
127 fb1a36c0 2022-01-09 op log_init(debug, LOG_DAEMON);
128 fb1a36c0 2022-01-09 op log_setverbose(verbose);
130 fb1a36c0 2022-01-09 op if ((pw = getpwnam(KD_USER)) == NULL)
131 fb1a36c0 2022-01-09 op fatal("getpwnam");
133 fb1a36c0 2022-01-09 op if (chroot(pw->pw_dir) == -1)
134 fb1a36c0 2022-01-09 op fatal("chroot");
135 fb1a36c0 2022-01-09 op if (chdir("/") == -1)
136 fb1a36c0 2022-01-09 op fatal("chdir(\"/\")");
138 fb1a36c0 2022-01-09 op setproctitle("listener");
139 fb1a36c0 2022-01-09 op log_procinit("listener");
141 fb1a36c0 2022-01-09 op if (setgroups(1, &pw->pw_gid) ||
142 fb1a36c0 2022-01-09 op setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) ||
143 fb1a36c0 2022-01-09 op setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid))
144 fb1a36c0 2022-01-09 op fatal("can't drop privileges");
146 fb1a36c0 2022-01-09 op event_init();
148 fb1a36c0 2022-01-09 op /* Setup signal handlers(s). */
149 fb1a36c0 2022-01-09 op signal_set(&ev_sigint, SIGINT, listener_sig_handler, NULL);
150 fb1a36c0 2022-01-09 op signal_set(&ev_sigterm, SIGTERM, listener_sig_handler, NULL);
152 fb1a36c0 2022-01-09 op signal_add(&ev_sigint, NULL);
153 fb1a36c0 2022-01-09 op signal_add(&ev_sigterm, NULL);
155 fb1a36c0 2022-01-09 op signal(SIGPIPE, SIG_IGN);
156 fb1a36c0 2022-01-09 op signal(SIGHUP, SIG_IGN);
158 fb1a36c0 2022-01-09 op /* Setup pipe and event handler to the main process. */
159 fb1a36c0 2022-01-09 op if ((iev_main = malloc(sizeof(*iev_main))) == NULL)
162 fb1a36c0 2022-01-09 op imsg_init(&iev_main->ibuf, 3);
163 fb1a36c0 2022-01-09 op iev_main->handler = listener_dispatch_main;
165 fb1a36c0 2022-01-09 op /* Setup event handlers. */
166 fb1a36c0 2022-01-09 op iev_main->events = EV_READ;
167 fb1a36c0 2022-01-09 op event_set(&iev_main->ev, iev_main->ibuf.fd, iev_main->events,
168 fb1a36c0 2022-01-09 op iev_main->handler, iev_main);
169 fb1a36c0 2022-01-09 op event_add(&iev_main->ev, NULL);
171 fb1a36c0 2022-01-09 op sandbox_listener();
172 fb1a36c0 2022-01-09 op event_dispatch();
173 fb1a36c0 2022-01-09 op listener_shutdown();
177 fb1a36c0 2022-01-09 op listener_shutdown(void)
179 fb1a36c0 2022-01-09 op msgbuf_clear(&iev_main->ibuf.w);
180 fb1a36c0 2022-01-09 op close(iev_main->ibuf.fd);
182 ef9db8f6 2022-01-28 op clear_config(listener_conf);
184 fb1a36c0 2022-01-09 op free(iev_main);
186 fb1a36c0 2022-01-09 op log_info("listener exiting");
191 fb1a36c0 2022-01-09 op listener_receive_config(struct imsg *imsg, struct kd_conf **nconf,
192 fb1a36c0 2022-01-09 op struct kd_pki_conf **pki)
194 fb1a36c0 2022-01-09 op struct kd_listen_conf *listen;
197 fb1a36c0 2022-01-09 op switch (imsg->hdr.type) {
198 fb1a36c0 2022-01-09 op case IMSG_RECONF_CONF:
199 fb1a36c0 2022-01-09 op if (*nconf != NULL)
200 fb1a36c0 2022-01-09 op fatalx("%s: IMSG_RECONF_CONF already in "
201 fb1a36c0 2022-01-09 op "progress", __func__);
203 fb1a36c0 2022-01-09 op if (IMSG_DATA_SIZE(*imsg) != sizeof(struct kd_conf))
204 fb1a36c0 2022-01-09 op fatalx("%s: IMSG_RECONF_CONF wrong length: %lu",
205 fb1a36c0 2022-01-09 op __func__, IMSG_DATA_SIZE(*imsg));
206 fb1a36c0 2022-01-09 op if ((*nconf = malloc(sizeof(**nconf))) == NULL)
208 fb1a36c0 2022-01-09 op memcpy(*nconf, imsg->data, sizeof(**nconf));
209 d70ef554 2022-01-28 op STAILQ_INIT(&(*nconf)->pki_head);
210 d70ef554 2022-01-28 op STAILQ_INIT(&(*nconf)->table_head);
211 d70ef554 2022-01-28 op STAILQ_INIT(&(*nconf)->listen_head);
213 fb1a36c0 2022-01-09 op case IMSG_RECONF_PKI:
214 fb1a36c0 2022-01-09 op if (*nconf == NULL)
215 fb1a36c0 2022-01-09 op fatalx("%s: IMSG_RECONF_PKI without "
216 fb1a36c0 2022-01-09 op "IMSG_RECONF_CONF", __func__);
217 fb1a36c0 2022-01-09 op *pki = xcalloc(1, sizeof(**pki));
218 fb1a36c0 2022-01-09 op t = imsg->data;
219 fb1a36c0 2022-01-09 op t[IMSG_DATA_SIZE(*imsg)-1] = '\0';
220 fb1a36c0 2022-01-09 op strlcpy((*pki)->name, t, sizeof((*pki)->name));
222 fb1a36c0 2022-01-09 op case IMSG_RECONF_PKI_CERT:
223 fb1a36c0 2022-01-09 op if (*pki == NULL)
224 fb1a36c0 2022-01-09 op fatalx("%s: IMSG_RECONF_PKI_CERT without "
225 fb1a36c0 2022-01-09 op "IMSG_RECONF_PKI", __func__);
226 fb1a36c0 2022-01-09 op (*pki)->certlen = IMSG_DATA_SIZE(*imsg);
227 fb1a36c0 2022-01-09 op (*pki)->cert = xmemdup(imsg->data, (*pki)->certlen);
229 fb1a36c0 2022-01-09 op case IMSG_RECONF_PKI_KEY:
230 fb1a36c0 2022-01-09 op if (*pki == NULL)
231 fb1a36c0 2022-01-09 op fatalx("%s: IMSG_RECONF_PKI_KEY without "
232 fb1a36c0 2022-01-09 op "IMSG_RECONF_PKI", __func__);
233 fb1a36c0 2022-01-09 op (*pki)->keylen = IMSG_DATA_SIZE(*imsg);
234 fb1a36c0 2022-01-09 op (*pki)->key = xmemdup(imsg->data, (*pki)->keylen);
235 fb1a36c0 2022-01-09 op STAILQ_INSERT_HEAD(&(*nconf)->pki_head, *pki, entry);
238 fb1a36c0 2022-01-09 op case IMSG_RECONF_LISTEN:
239 fb1a36c0 2022-01-09 op if (*nconf == NULL)
240 fb1a36c0 2022-01-09 op fatalx("%s: IMSG_RECONF_LISTEN without "
241 fb1a36c0 2022-01-09 op "IMSG_RECONF_CONF", __func__);
242 fb1a36c0 2022-01-09 op if (IMSG_DATA_SIZE(*imsg) != sizeof(*listen))
243 fb1a36c0 2022-01-09 op fatalx("%s: IMSG_RECONF_LISTEN wrong length: %lu",
244 fb1a36c0 2022-01-09 op __func__, IMSG_DATA_SIZE(*imsg));
245 fb1a36c0 2022-01-09 op listen = xcalloc(1, sizeof(*listen));
246 fb1a36c0 2022-01-09 op memcpy(listen, imsg->data, sizeof(*listen));
247 fb1a36c0 2022-01-09 op memset(&listen->entry, 0, sizeof(listen->entry));
248 fb1a36c0 2022-01-09 op if ((listen->fd = imsg->fd) == -1)
249 fb1a36c0 2022-01-09 op fatalx("%s: IMSG_RECONF_LISTEN no fd",
251 fb1a36c0 2022-01-09 op listen->auth_table = NULL;
252 fb1a36c0 2022-01-09 op memset(&listen->ev, 0, sizeof(listen->ev));
253 fb1a36c0 2022-01-09 op STAILQ_INSERT_HEAD(&(*nconf)->listen_head, listen, entry);
255 fb1a36c0 2022-01-09 op case IMSG_RECONF_END:
256 fb1a36c0 2022-01-09 op if (*nconf == NULL)
257 fb1a36c0 2022-01-09 op fatalx("%s: IMSG_RECONF_END without "
258 fb1a36c0 2022-01-09 op "IMSG_RECONF_CONF", __func__);
259 fb1a36c0 2022-01-09 op apply_config(*nconf);
260 fb1a36c0 2022-01-09 op *nconf = NULL;
266 fb1a36c0 2022-01-09 op listener_dispatch_main(int fd, short event, void *d)
268 fb1a36c0 2022-01-09 op static struct kd_conf *nconf;
269 fb1a36c0 2022-01-09 op static struct kd_pki_conf *pki;
270 fb1a36c0 2022-01-09 op struct client *client, find;
271 fb1a36c0 2022-01-09 op struct imsg imsg;
272 fb1a36c0 2022-01-09 op struct imsgev *iev = d;
273 fb1a36c0 2022-01-09 op struct imsgbuf *ibuf;
275 fb1a36c0 2022-01-09 op int shut = 0;
277 fb1a36c0 2022-01-09 op ibuf = &iev->ibuf;
279 fb1a36c0 2022-01-09 op if (event & EV_READ) {
280 fb1a36c0 2022-01-09 op if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
281 fb1a36c0 2022-01-09 op fatal("imsg_read error");
282 fb1a36c0 2022-01-09 op if (n == 0) /* Connection closed. */
285 fb1a36c0 2022-01-09 op if (event & EV_WRITE) {
286 fb1a36c0 2022-01-09 op if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
287 fb1a36c0 2022-01-09 op fatal("msgbuf_write");
288 fb1a36c0 2022-01-09 op if (n == 0) /* Connection closed. */
293 fb1a36c0 2022-01-09 op if ((n = imsg_get(ibuf, &imsg)) == -1)
294 fb1a36c0 2022-01-09 op fatal("%s: imsg_get error", __func__);
295 fb1a36c0 2022-01-09 op if (n == 0) /* No more messages. */
298 fb1a36c0 2022-01-09 op switch (imsg.hdr.type) {
299 982f38dc 2022-01-23 op case IMSG_CTL_LOG_VERBOSE:
300 982f38dc 2022-01-23 op if (IMSG_DATA_SIZE(imsg) != sizeof(verbose))
301 982f38dc 2022-01-23 op fatalx("wrong size for IMSG_CTL_LOG_VERBOSE");
302 982f38dc 2022-01-23 op memcpy(&verbose, imsg.data, sizeof(verbose));
303 982f38dc 2022-01-23 op log_setverbose(verbose);
304 982f38dc 2022-01-23 op SPLAY_FOREACH(client, clients_tree_id, &clients)
305 982f38dc 2022-01-23 op listener_imsg_compose_client(client,
306 982f38dc 2022-01-23 op imsg.hdr.type, 0,
307 982f38dc 2022-01-23 op &verbose, sizeof(verbose));
309 5c69b561 2022-01-29 op case IMSG_CTL_DEBUG:
310 5c69b561 2022-01-29 op if (SPLAY_EMPTY(&clients))
311 5c69b561 2022-01-29 op listener_imsg_compose_main(IMSG_CTL_DEBUG_END,
312 5c69b561 2022-01-29 op imsg.hdr.peerid, NULL, 0);
313 5c69b561 2022-01-29 op SPLAY_FOREACH(client, clients_tree_id, &clients)
314 5c69b561 2022-01-29 op listener_imsg_compose_client(client,
315 5c69b561 2022-01-29 op imsg.hdr.type, imsg.hdr.peerid,
316 5c69b561 2022-01-29 op imsg.data, IMSG_DATA_SIZE(imsg));
318 fb1a36c0 2022-01-09 op case IMSG_RECONF_CONF:
319 fb1a36c0 2022-01-09 op case IMSG_RECONF_PKI:
320 fb1a36c0 2022-01-09 op case IMSG_RECONF_PKI_CERT:
321 fb1a36c0 2022-01-09 op case IMSG_RECONF_PKI_KEY:
322 fb1a36c0 2022-01-09 op case IMSG_RECONF_LISTEN:
323 fb1a36c0 2022-01-09 op case IMSG_RECONF_END:
324 fb1a36c0 2022-01-09 op listener_receive_config(&imsg, &nconf, &pki);
326 fb1a36c0 2022-01-09 op case IMSG_AUTH:
327 1e84c7b7 2022-01-10 op if (IMSG_DATA_SIZE(imsg) != sizeof(struct kd_auth_proc))
328 1e84c7b7 2022-01-10 op fatalx("mismatching size for IMSG_AUTH");
330 fb1a36c0 2022-01-09 op find.id = imsg.hdr.peerid;
331 fb1a36c0 2022-01-09 op client = SPLAY_FIND(clients_tree_id, &clients, &find);
332 fb1a36c0 2022-01-09 op if (client == NULL) {
333 fb1a36c0 2022-01-09 op if (imsg.fd != -1)
334 fb1a36c0 2022-01-09 op close(imsg.fd);
337 fb1a36c0 2022-01-09 op if (imsg.fd == -1) {
338 fb1a36c0 2022-01-09 op log_info("got fd = -1, auth failed?");
339 fb1a36c0 2022-01-09 op close_conn(client);
342 fb1a36c0 2022-01-09 op imsg_init(&client->iev.ibuf, imsg.fd);
343 fb1a36c0 2022-01-09 op client->iev.events = EV_READ;
344 fb1a36c0 2022-01-09 op client->iev.handler = listener_dispatch_client;
345 fb1a36c0 2022-01-09 op event_set(&client->iev.ev, client->iev.ibuf.fd,
346 fb1a36c0 2022-01-09 op client->iev.events, client->iev.handler, client);
347 fb1a36c0 2022-01-09 op listener_imsg_compose_client(client, IMSG_AUTH,
348 fb1a36c0 2022-01-09 op client->id, imsg.data, IMSG_DATA_SIZE(imsg));
350 fb1a36c0 2022-01-09 op client->bev = bufferevent_new(client->fd,
351 fb1a36c0 2022-01-09 op client_read, client_write, client_error,
353 fb1a36c0 2022-01-09 op if (client->bev == NULL) {
354 fb1a36c0 2022-01-09 op log_info("failed to allocate client buffer");
355 fb1a36c0 2022-01-09 op close_conn(client);
359 fb1a36c0 2022-01-09 op #if HAVE_EVENT2
360 fb1a36c0 2022-01-09 op evbuffer_unfreeze(client->bev->input, 0);
361 fb1a36c0 2022-01-09 op evbuffer_unfreeze(client->bev->output, 1);
364 38a6fd3d 2022-01-28 op if (client->lflags & L_TLS) {
365 fb1a36c0 2022-01-09 op event_set(&client->bev->ev_read, client->fd,
366 fb1a36c0 2022-01-09 op EV_READ, client_tls_readcb, client->bev);
367 fb1a36c0 2022-01-09 op event_set(&client->bev->ev_write, client->fd,
368 fb1a36c0 2022-01-09 op EV_WRITE, client_tls_writecb, client->bev);
372 fb1a36c0 2022-01-09 op * Read or write at least a header before
373 fb1a36c0 2022-01-09 op * firing the callbacks. High watermark of 0
374 fb1a36c0 2022-01-09 op * to never stop reading/writing; probably to
375 fb1a36c0 2022-01-09 op * be revisited.
377 fb1a36c0 2022-01-09 op /* bufferevent_setwatermark(client->bev, EV_READ|EV_WRITE, */
378 fb1a36c0 2022-01-09 op /* sizeof(struct np_msg_header), 0); */
379 fb1a36c0 2022-01-09 op bufferevent_enable(client->bev, EV_READ|EV_WRITE);
383 fb1a36c0 2022-01-09 op log_debug("%s: unexpected imsg %d", __func__,
384 fb1a36c0 2022-01-09 op imsg.hdr.type);
387 fb1a36c0 2022-01-09 op imsg_free(&imsg);
391 fb1a36c0 2022-01-09 op listener_imsg_event_add(iev, d);
393 fb1a36c0 2022-01-09 op /* This pipe is dead. Remove its event handler. */
394 fb1a36c0 2022-01-09 op event_del(&iev->ev);
395 fb1a36c0 2022-01-09 op log_warnx("pipe closed, shutting down...");
396 fb1a36c0 2022-01-09 op event_loopexit(NULL);
401 fb1a36c0 2022-01-09 op listener_imsg_compose_main(int type, uint32_t peerid, const void *data,
402 fb1a36c0 2022-01-09 op uint16_t datalen)
404 fb1a36c0 2022-01-09 op return imsg_compose_event(iev_main, type, peerid, 0, -1, data,
409 fb1a36c0 2022-01-09 op listener_imsg_event_add(struct imsgev *iev, void *d)
411 fb1a36c0 2022-01-09 op iev->events = EV_READ;
412 fb1a36c0 2022-01-09 op if (iev->ibuf.w.queued)
413 fb1a36c0 2022-01-09 op iev->events |= EV_WRITE;
415 fb1a36c0 2022-01-09 op event_del(&iev->ev);
416 fb1a36c0 2022-01-09 op event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, d);
417 fb1a36c0 2022-01-09 op event_add(&iev->ev, NULL);
421 fb1a36c0 2022-01-09 op listener_dispatch_client(int fd, short event, void *d)
423 fb1a36c0 2022-01-09 op struct client find, *client = d;
424 fb1a36c0 2022-01-09 op struct imsg imsg;
425 fb1a36c0 2022-01-09 op struct imsgev *iev;
426 fb1a36c0 2022-01-09 op struct imsgbuf *ibuf;
428 fb1a36c0 2022-01-09 op int r, shut = 0;
430 fb1a36c0 2022-01-09 op iev = &client->iev;
431 fb1a36c0 2022-01-09 op ibuf = &iev->ibuf;
433 fb1a36c0 2022-01-09 op if (event & EV_READ) {
434 fb1a36c0 2022-01-09 op if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
435 fb1a36c0 2022-01-09 op fatal("imsg_read error");
436 fb1a36c0 2022-01-09 op if (n == 0) /* Connection closed */
440 fb1a36c0 2022-01-09 op if (event & EV_WRITE) {
441 fb1a36c0 2022-01-09 op if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
442 fb1a36c0 2022-01-09 op fatal("msgbuf_write");
443 fb1a36c0 2022-01-09 op if (n == 0) /* Connection closed. */
448 fb1a36c0 2022-01-09 op if ((n = imsg_get(ibuf, &imsg)) == -1)
449 fb1a36c0 2022-01-09 op fatal("%s: imsg_get error", __func__);
450 fb1a36c0 2022-01-09 op if (n == 0) /* No more messages. */
453 fb1a36c0 2022-01-09 op switch (imsg.hdr.type) {
454 5c69b561 2022-01-29 op case IMSG_CTL_DEBUG_BACK:
455 5c69b561 2022-01-29 op case IMSG_CTL_DEBUG_END:
456 5c69b561 2022-01-29 op listener_imsg_compose_main(imsg.hdr.type,
457 5c69b561 2022-01-29 op imsg.hdr.peerid, imsg.data, IMSG_DATA_SIZE(imsg));
460 fb1a36c0 2022-01-09 op case IMSG_BUF:
461 fb1a36c0 2022-01-09 op find.id = imsg.hdr.peerid;
462 fb1a36c0 2022-01-09 op client = SPLAY_FIND(clients_tree_id, &clients, &find);
463 fb1a36c0 2022-01-09 op if (client == NULL) {
464 f4bc229f 2022-01-29 op log_info("got IMSG_BUF but client %d gone",
465 fb1a36c0 2022-01-09 op imsg.hdr.peerid);
468 fb1a36c0 2022-01-09 op r = bufferevent_write(client->bev, imsg.data,
469 fb1a36c0 2022-01-09 op IMSG_DATA_SIZE(imsg));
470 fb1a36c0 2022-01-09 op if (r == -1) {
471 fb1a36c0 2022-01-09 op log_warn("%s: bufferevent_write failed",
473 fb1a36c0 2022-01-09 op close_conn(client);
478 fb1a36c0 2022-01-09 op case IMSG_MSIZE:
479 fb1a36c0 2022-01-09 op if (IMSG_DATA_SIZE(imsg) != sizeof(client->msize))
480 fb1a36c0 2022-01-09 op fatal("IMSG_MSIZE size mismatch: "
481 fb1a36c0 2022-01-09 op "got %zu want %zu", IMSG_DATA_SIZE(imsg),
482 fb1a36c0 2022-01-09 op sizeof(client->msize));
484 fb1a36c0 2022-01-09 op memcpy(&client->msize, imsg.data,
485 fb1a36c0 2022-01-09 op sizeof(client->msize));
487 fb1a36c0 2022-01-09 op if (client->msize == 0)
488 fb1a36c0 2022-01-09 op fatal("IMSG_MSIZE got msize = 0");
489 8b0a5062 2022-05-22 op log_debug("set msize to %d", client->msize);
492 fb1a36c0 2022-01-09 op case IMSG_CLOSE:
494 fb1a36c0 2022-01-09 op * Both EVBUFFER_READ or EVBUFFER_WRITE should
497 fb1a36c0 2022-01-09 op client_error(client->bev, EVBUFFER_READ, client);
501 fb1a36c0 2022-01-09 op log_debug("%s: unexpected imsg %d", __func__,
502 fb1a36c0 2022-01-09 op imsg.hdr.type);
505 fb1a36c0 2022-01-09 op imsg_free(&imsg);
509 fb1a36c0 2022-01-09 op listener_imsg_event_add(iev, d);
511 fb1a36c0 2022-01-09 op /* This pipe is dead. Remove its handler */
512 fb1a36c0 2022-01-09 op log_debug("client proc vanished");
513 fb1a36c0 2022-01-09 op close_conn(client);
518 fb1a36c0 2022-01-09 op listener_imsg_compose_client(struct client *client, int type,
519 fb1a36c0 2022-01-09 op uint32_t peerid, const void *data, uint16_t len)
523 fb1a36c0 2022-01-09 op if ((ret = imsg_compose(&client->iev.ibuf, type, peerid, 0, -1,
524 fb1a36c0 2022-01-09 op data, len)) != -1)
525 fb1a36c0 2022-01-09 op listener_imsg_event_add(&client->iev, client);
530 fb1a36c0 2022-01-09 op static inline struct kd_pki_conf *
531 fb1a36c0 2022-01-09 op pki_by_name(const char *name)
533 fb1a36c0 2022-01-09 op struct kd_pki_conf *pki;
535 fb1a36c0 2022-01-09 op STAILQ_FOREACH(pki, &listener_conf->pki_head, entry) {
536 fb1a36c0 2022-01-09 op if (!strcmp(name, pki->name))
544 fb1a36c0 2022-01-09 op apply_config(struct kd_conf *conf)
546 fb1a36c0 2022-01-09 op struct kd_pki_conf *pki;
547 fb1a36c0 2022-01-09 op struct kd_listen_conf *listen;
548 ac796d32 2022-01-28 op struct client *c;
550 ac796d32 2022-01-28 op /* drop any pre-auth inflight connections */
551 ac796d32 2022-01-28 op SPLAY_FOREACH(c, clients_tree_id, &clients) {
553 ac796d32 2022-01-28 op * c->event is set only during the handshake and the teardown
554 ac796d32 2022-01-28 op * of the connection; c->bev is set only after auth. Checking
555 ac796d32 2022-01-28 op * for both ensures we drop only incoming connection in the
556 ac796d32 2022-01-28 op * pre-auth state.
558 ac796d32 2022-01-28 op if (event_pending(&c->event, EV_READ|EV_WRITE, NULL) &&
559 ac796d32 2022-01-28 op c->bev == NULL) {
560 ac796d32 2022-01-28 op log_warn("closing in-flight connection due to reload");
561 ac796d32 2022-01-28 op close_conn(c);
565 4c0ae0c3 2022-01-28 op /* swap the now config with the current one */
566 4c0ae0c3 2022-01-28 op clear_config(listener_conf);
567 fb1a36c0 2022-01-09 op listener_conf = conf;
569 fb1a36c0 2022-01-09 op /* prepare the various tls_config */
570 fb1a36c0 2022-01-09 op STAILQ_FOREACH(pki, &listener_conf->pki_head, entry) {
571 fb1a36c0 2022-01-09 op if ((pki->tlsconf = tls_config_new()) == NULL)
572 fb1a36c0 2022-01-09 op fatal("tls_config_new");
573 fb1a36c0 2022-01-09 op tls_config_verify_client_optional(pki->tlsconf);
574 fb1a36c0 2022-01-09 op tls_config_insecure_noverifycert(pki->tlsconf);
575 fb1a36c0 2022-01-09 op if (tls_config_set_keypair_mem(pki->tlsconf,
576 fb1a36c0 2022-01-09 op pki->cert, pki->certlen,
577 fb1a36c0 2022-01-09 op pki->key, pki->keylen) == -1)
578 fb1a36c0 2022-01-09 op fatalx("tls_config_set_keypair_mem: %s",
579 fb1a36c0 2022-01-09 op tls_config_error(pki->tlsconf));
582 fb1a36c0 2022-01-09 op /* prepare and kickoff the listeners */
583 fb1a36c0 2022-01-09 op STAILQ_FOREACH(listen, &listener_conf->listen_head, entry) {
584 fb1a36c0 2022-01-09 op if ((listen->ctx = tls_server()) == NULL)
585 fb1a36c0 2022-01-09 op fatal("tls_server");
587 fb1a36c0 2022-01-09 op pki = pki_by_name(listen->pki);
588 fb1a36c0 2022-01-09 op if (tls_configure(listen->ctx, pki->tlsconf) == -1)
589 fb1a36c0 2022-01-09 op fatalx("tls_configure: %s",
590 fb1a36c0 2022-01-09 op tls_config_error(pki->tlsconf));
592 fb1a36c0 2022-01-09 op event_set(&listen->ev, listen->fd, EV_READ|EV_PERSIST,
593 fb1a36c0 2022-01-09 op handle_accept, listen);
594 fb1a36c0 2022-01-09 op event_add(&listen->ev, NULL);
598 fb1a36c0 2022-01-09 op static inline void
599 fb1a36c0 2022-01-09 op yield_r(struct client *c, void (*fn)(int, short, void *))
601 fb1a36c0 2022-01-09 op if (event_pending(&c->event, EV_WRITE|EV_READ, NULL))
602 fb1a36c0 2022-01-09 op event_del(&c->event);
603 fb1a36c0 2022-01-09 op event_set(&c->event, c->fd, EV_READ, fn, c);
604 fb1a36c0 2022-01-09 op event_add(&c->event, NULL);
607 fb1a36c0 2022-01-09 op static inline void
608 fb1a36c0 2022-01-09 op yield_w(struct client *c, void (*fn)(int, short, void *))
610 fb1a36c0 2022-01-09 op if (event_pending(&c->event, EV_WRITE|EV_READ, NULL))
611 fb1a36c0 2022-01-09 op event_del(&c->event);
612 fb1a36c0 2022-01-09 op event_set(&c->event, c->fd, EV_WRITE, fn, c);
613 fb1a36c0 2022-01-09 op event_add(&c->event, NULL);
617 fb1a36c0 2022-01-09 op handle_accept(int fd, short ev, void *data)
619 057b5ab7 2022-01-28 op static uint32_t counter;
620 fb1a36c0 2022-01-09 op struct kd_listen_conf *listen = data;
621 fb1a36c0 2022-01-09 op struct client *c;
624 fb1a36c0 2022-01-09 op if ((s = accept(fd, NULL, NULL)) == -1) {
625 fb1a36c0 2022-01-09 op log_warn("accept");
629 fb1a36c0 2022-01-09 op c = xcalloc(1, sizeof(*c));
630 fb1a36c0 2022-01-09 op c->msize = MSIZE9P;
631 fb1a36c0 2022-01-09 op c->lid = listen->id;
632 38a6fd3d 2022-01-28 op c->lflags = listen->flags;
633 fb1a36c0 2022-01-09 op c->iev.ibuf.fd = -1;
635 fb1a36c0 2022-01-09 op if (tls_accept_socket(listen->ctx, &c->ctx, s) == -1) {
636 fb1a36c0 2022-01-09 op log_warnx("tls_accept_socket: %s",
637 fb1a36c0 2022-01-09 op tls_error(listen->ctx));
644 057b5ab7 2022-01-28 op c->id = counter++;
646 fb1a36c0 2022-01-09 op SPLAY_INSERT(clients_tree_id, &clients, c);
648 fb1a36c0 2022-01-09 op /* initialize the event */
649 fb1a36c0 2022-01-09 op event_set(&c->event, c->fd, EV_READ, NULL, NULL);
651 fb1a36c0 2022-01-09 op yield_r(c, handle_handshake);
655 fb1a36c0 2022-01-09 op handle_handshake(int fd, short ev, void *data)
657 fb1a36c0 2022-01-09 op struct client *c = data;
658 fb1a36c0 2022-01-09 op struct kd_auth_req auth;
660 fb1a36c0 2022-01-09 op const char *hash;
662 fb1a36c0 2022-01-09 op switch (r = tls_handshake(c->ctx)) {
663 fb1a36c0 2022-01-09 op case TLS_WANT_POLLIN:
664 fb1a36c0 2022-01-09 op yield_r(c, handle_handshake);
666 fb1a36c0 2022-01-09 op case TLS_WANT_POLLOUT:
667 fb1a36c0 2022-01-09 op yield_w(c, handle_handshake);
670 fb1a36c0 2022-01-09 op log_debug("handhsake failed: %s", tls_error(c->ctx));
671 fb1a36c0 2022-01-09 op close_conn(c);
675 fb1a36c0 2022-01-09 op if ((hash = tls_peer_cert_hash(c->ctx)) == NULL) {
676 fb1a36c0 2022-01-09 op log_warnx("client didn't provide certificate");
677 fb1a36c0 2022-01-09 op close_conn(c);
681 fb1a36c0 2022-01-09 op memset(&auth, 0, sizeof(auth));
682 fb1a36c0 2022-01-09 op auth.listen_id = c->lid;
683 fb1a36c0 2022-01-09 op strlcpy(auth.hash, hash, sizeof(auth.hash));
684 fb1a36c0 2022-01-09 op log_debug("sending hash %s", auth.hash);
686 fb1a36c0 2022-01-09 op listener_imsg_compose_main(IMSG_AUTH_TLS, c->id,
687 fb1a36c0 2022-01-09 op &auth, sizeof(auth));
691 fb1a36c0 2022-01-09 op client_read(struct bufferevent *bev, void *d)
693 fb1a36c0 2022-01-09 op struct client *client = d;
694 fb1a36c0 2022-01-09 op struct evbuffer *src = EVBUFFER_INPUT(bev);
695 8b0a5062 2022-05-22 op size_t evlen;
696 8b0a5062 2022-05-22 op uint32_t len;
699 8b0a5062 2022-05-22 op evlen = EVBUFFER_LENGTH(src);
701 8b0a5062 2022-05-22 op if (client->left != 0) {
702 8b0a5062 2022-05-22 op /* wait to fill a whole imsg if possible */
703 8b0a5062 2022-05-22 op if (client->left >= IMSG_MAXSIZE &&
704 8b0a5062 2022-05-22 op evlen < IMSG_MAXSIZE)
707 8b0a5062 2022-05-22 op len = MIN(client->left, evlen);
708 c234caae 2022-05-23 op len = MIN(len, IMSG_MAXSIZE);
710 8b0a5062 2022-05-22 op listener_imsg_compose_client(client, IMSG_BUF_CONT,
711 8b0a5062 2022-05-22 op client->id, EVBUFFER_DATA(src), len);
712 8b0a5062 2022-05-22 op evbuffer_drain(src, len);
713 8b0a5062 2022-05-22 op client->left -= len;
717 8b0a5062 2022-05-22 op if (evlen < 4)
720 fb1a36c0 2022-01-09 op memcpy(&len, EVBUFFER_DATA(src), sizeof(len));
721 fb1a36c0 2022-01-09 op len = le32toh(len);
722 fb1a36c0 2022-01-09 op log_debug("expecting a message %"PRIu32" bytes long "
723 8b0a5062 2022-05-22 op "(of wich %zu already read)", len, evlen);
725 fb1a36c0 2022-01-09 op if (len < HEADERSIZE) {
726 fb1a36c0 2022-01-09 op log_warnx("invalid message size %d (too low)", len);
727 fb1a36c0 2022-01-09 op client_error(bev, EVBUFFER_READ, client);
731 fb1a36c0 2022-01-09 op if (len > client->msize) {
732 fb1a36c0 2022-01-09 op log_warnx("incoming message bigger than msize "
733 fb1a36c0 2022-01-09 op "(%"PRIu32" vs %"PRIu32")", len, client->msize);
734 fb1a36c0 2022-01-09 op client_error(bev, EVBUFFER_READ, client);
738 8b0a5062 2022-05-22 op if (len > IMSG_MAXSIZE && evlen >= len) {
739 8b0a5062 2022-05-22 op listener_imsg_compose_client(client, IMSG_BUF,
740 8b0a5062 2022-05-22 op client->id, EVBUFFER_DATA(src), IMSG_MAXSIZE);
741 8b0a5062 2022-05-22 op evbuffer_drain(src, IMSG_MAXSIZE);
742 8b0a5062 2022-05-22 op client->left = len - IMSG_MAXSIZE;
746 8b0a5062 2022-05-22 op if (len > evlen)
749 fb1a36c0 2022-01-09 op listener_imsg_compose_client(client, IMSG_BUF, client->id,
750 fb1a36c0 2022-01-09 op EVBUFFER_DATA(src), len);
751 fb1a36c0 2022-01-09 op evbuffer_drain(src, len);
756 fb1a36c0 2022-01-09 op client_write(struct bufferevent *bev, void *d)
759 fb1a36c0 2022-01-09 op * here we can do some fancy logic like deciding when to call
761 fb1a36c0 2022-01-09 op * (*bev->errorcb)(bev, EVBUFFER_WRITE, bev->cbarg)
763 fb1a36c0 2022-01-09 op * to signal the end of the transaction.
770 fb1a36c0 2022-01-09 op client_error(struct bufferevent *bev, short err, void *d)
772 fb1a36c0 2022-01-09 op struct client *client = d;
773 fb1a36c0 2022-01-09 op struct evbuffer *buf;
775 fb1a36c0 2022-01-09 op if (err & EVBUFFER_ERROR) {
776 fb1a36c0 2022-01-09 op if (errno == EFBIG) {
777 fb1a36c0 2022-01-09 op bufferevent_enable(bev, EV_READ);
780 fb1a36c0 2022-01-09 op log_debug("buffer event error");
781 fb1a36c0 2022-01-09 op close_conn(client);
785 fb1a36c0 2022-01-09 op if (err & EVBUFFER_EOF) {
786 fb1a36c0 2022-01-09 op close_conn(client);
790 fb1a36c0 2022-01-09 op if (err & (EVBUFFER_READ|EVBUFFER_WRITE)) {
791 fb1a36c0 2022-01-09 op bufferevent_disable(bev, EV_READ|EV_WRITE);
793 fb1a36c0 2022-01-09 op buf = EVBUFFER_OUTPUT(client->bev);
794 fb1a36c0 2022-01-09 op if (EVBUFFER_LENGTH(buf) != 0) {
795 fb1a36c0 2022-01-09 op /* finish writing all the data first */
796 fb1a36c0 2022-01-09 op bufferevent_enable(client->bev, EV_WRITE);
800 fb1a36c0 2022-01-09 op close_conn(client);
804 fb1a36c0 2022-01-09 op log_warnx("unknown event error, closing client connection");
805 fb1a36c0 2022-01-09 op close_conn(client);
809 fb1a36c0 2022-01-09 op client_tls_readcb(int fd, short event, void *d)
811 fb1a36c0 2022-01-09 op struct bufferevent *bufev = d;
812 fb1a36c0 2022-01-09 op struct client *client = bufev->cbarg;
813 fb1a36c0 2022-01-09 op char buf[IBUF_READ_SIZE];
814 fb1a36c0 2022-01-09 op int what = EVBUFFER_READ;
815 fb1a36c0 2022-01-09 op int howmuch = IBUF_READ_SIZE;
819 fb1a36c0 2022-01-09 op if (event == EV_TIMEOUT) {
820 fb1a36c0 2022-01-09 op what |= EVBUFFER_TIMEOUT;
824 fb1a36c0 2022-01-09 op if (bufev->wm_read.high != 0)
825 fb1a36c0 2022-01-09 op howmuch = MIN(sizeof(buf), bufev->wm_read.high);
827 fb1a36c0 2022-01-09 op switch (ret = tls_read(client->ctx, buf, howmuch)) {
828 fb1a36c0 2022-01-09 op case TLS_WANT_POLLIN:
829 fb1a36c0 2022-01-09 op case TLS_WANT_POLLOUT:
832 fb1a36c0 2022-01-09 op what |= EVBUFFER_ERROR;
837 fb1a36c0 2022-01-09 op if (len == 0) {
838 fb1a36c0 2022-01-09 op what |= EVBUFFER_EOF;
842 fb1a36c0 2022-01-09 op if (evbuffer_add(bufev->input, buf, len) == -1) {
843 fb1a36c0 2022-01-09 op what |= EVBUFFER_ERROR;
847 fb1a36c0 2022-01-09 op event_add(&bufev->ev_read, NULL);
849 fb1a36c0 2022-01-09 op len = EVBUFFER_LENGTH(bufev->input);
850 fb1a36c0 2022-01-09 op if (bufev->wm_read.low != 0 && len < bufev->wm_read.low)
852 fb1a36c0 2022-01-09 op if (bufev->wm_read.high != 0 && len > bufev->wm_read.high) {
854 fb1a36c0 2022-01-09 op * here we could implement some read pressure
859 fb1a36c0 2022-01-09 op if (bufev->readcb != NULL)
860 fb1a36c0 2022-01-09 op (*bufev->readcb)(bufev, bufev->cbarg);
865 fb1a36c0 2022-01-09 op event_add(&bufev->ev_read, NULL);
869 fb1a36c0 2022-01-09 op (*bufev->errorcb)(bufev, what, bufev->cbarg);
873 fb1a36c0 2022-01-09 op client_tls_writecb(int fd, short event, void *d)
875 fb1a36c0 2022-01-09 op struct bufferevent *bufev = d;
876 fb1a36c0 2022-01-09 op struct client *client = bufev->cbarg;
879 fb1a36c0 2022-01-09 op short what = EVBUFFER_WRITE;
881 fb1a36c0 2022-01-09 op if (event == EV_TIMEOUT) {
882 fb1a36c0 2022-01-09 op what |= EVBUFFER_TIMEOUT;
886 fb1a36c0 2022-01-09 op if (EVBUFFER_LENGTH(bufev->output) != 0) {
887 fb1a36c0 2022-01-09 op ret = tls_write(client->ctx,
888 fb1a36c0 2022-01-09 op EVBUFFER_DATA(bufev->output),
889 fb1a36c0 2022-01-09 op EVBUFFER_LENGTH(bufev->output));
890 fb1a36c0 2022-01-09 op switch (ret) {
891 fb1a36c0 2022-01-09 op case TLS_WANT_POLLIN:
892 fb1a36c0 2022-01-09 op case TLS_WANT_POLLOUT:
895 fb1a36c0 2022-01-09 op what |= EVBUFFER_ERROR;
899 fb1a36c0 2022-01-09 op evbuffer_drain(bufev->output, len);
902 fb1a36c0 2022-01-09 op if (EVBUFFER_LENGTH(bufev->output) != 0)
903 fb1a36c0 2022-01-09 op event_add(&bufev->ev_write, NULL);
905 fb1a36c0 2022-01-09 op if (bufev->writecb != NULL &&
906 fb1a36c0 2022-01-09 op EVBUFFER_LENGTH(bufev->output) <= bufev->wm_write.low)
907 fb1a36c0 2022-01-09 op (*bufev->writecb)(bufev, bufev->cbarg);
911 fb1a36c0 2022-01-09 op event_add(&bufev->ev_write, NULL);
915 fb1a36c0 2022-01-09 op (*bufev->errorcb)(bufev, what, bufev->cbarg);
919 fb1a36c0 2022-01-09 op close_conn(struct client *c)
921 fb1a36c0 2022-01-09 op log_debug("closing connection");
923 2ab2c437 2022-01-28 op SPLAY_REMOVE(clients_tree_id, &clients, c);
925 fb1a36c0 2022-01-09 op if (c->iev.ibuf.fd != -1) {
926 fb1a36c0 2022-01-09 op listener_imsg_compose_client(c, IMSG_CONN_GONE, 0, NULL, 0);
927 fb1a36c0 2022-01-09 op imsg_flush(&c->iev.ibuf);
928 fb1a36c0 2022-01-09 op msgbuf_clear(&c->iev.ibuf.w);
929 fb1a36c0 2022-01-09 op event_del(&c->iev.ev);
930 fb1a36c0 2022-01-09 op close(c->iev.ibuf.fd);
933 fb1a36c0 2022-01-09 op handle_close(c->fd, 0, c);
937 fb1a36c0 2022-01-09 op handle_close(int fd, short ev, void *d)
939 fb1a36c0 2022-01-09 op struct client *c = d;
941 fb1a36c0 2022-01-09 op switch (tls_close(c->ctx)) {
942 fb1a36c0 2022-01-09 op case TLS_WANT_POLLIN:
943 fb1a36c0 2022-01-09 op yield_r(c, handle_close);
945 fb1a36c0 2022-01-09 op case TLS_WANT_POLLOUT:
946 fb1a36c0 2022-01-09 op yield_w(c, handle_close);
950 fb1a36c0 2022-01-09 op event_del(&c->event);
951 fb1a36c0 2022-01-09 op tls_free(c->ctx);
952 fb1a36c0 2022-01-09 op close(c->fd);