2 fb1a36c0 2022-01-09 op * Copyright (c) 2021 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 fb1a36c0 2022-01-09 op #include <sys/socket.h>
22 fb1a36c0 2022-01-09 op #include <sys/types.h>
23 fb1a36c0 2022-01-09 op #include <sys/tree.h>
24 fb1a36c0 2022-01-09 op #include <sys/queue.h>
25 fb1a36c0 2022-01-09 op #include <sys/uio.h>
27 fb1a36c0 2022-01-09 op #include <endian.h>
28 fb1a36c0 2022-01-09 op #include <errno.h>
29 fb1a36c0 2022-01-09 op #include <event.h>
30 fb1a36c0 2022-01-09 op #include <inttypes.h>
31 fb1a36c0 2022-01-09 op #include <pwd.h>
32 fb1a36c0 2022-01-09 op #include <signal.h>
33 fb1a36c0 2022-01-09 op #include <stdint.h>
34 fb1a36c0 2022-01-09 op #include <stdio.h>
35 fb1a36c0 2022-01-09 op #include <stdlib.h>
36 fb1a36c0 2022-01-09 op #include <string.h>
37 fb1a36c0 2022-01-09 op #include <syslog.h>
38 fb1a36c0 2022-01-09 op #include <unistd.h>
39 fb1a36c0 2022-01-09 op #include <imsg.h>
41 fb1a36c0 2022-01-09 op #include "control.h"
42 fb1a36c0 2022-01-09 op #include "kami.h"
43 fb1a36c0 2022-01-09 op #include "kamid.h"
44 fb1a36c0 2022-01-09 op #include "listener.h"
45 fb1a36c0 2022-01-09 op #include "log.h"
46 fb1a36c0 2022-01-09 op #include "sandbox.h"
47 fb1a36c0 2022-01-09 op #include "utils.h"
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 fb1a36c0 2022-01-09 op uint32_t msize;
63 fb1a36c0 2022-01-09 op struct tls *ctx;
64 fb1a36c0 2022-01-09 op struct event event;
65 fb1a36c0 2022-01-09 op struct imsgev iev;
66 fb1a36c0 2022-01-09 op struct bufferevent *bev;
67 fb1a36c0 2022-01-09 op SPLAY_ENTRY(client) sp_entry;
70 fb1a36c0 2022-01-09 op static void listener_imsg_event_add(struct imsgev *, void *);
71 fb1a36c0 2022-01-09 op static void listener_dispatch_client(int, short, void *);
72 fb1a36c0 2022-01-09 op static int listener_imsg_compose_client(struct client *, int,
73 fb1a36c0 2022-01-09 op uint32_t, const void *, uint16_t);
75 fb1a36c0 2022-01-09 op static void apply_config(struct kd_conf *);
76 fb1a36c0 2022-01-09 op static void handle_accept(int, short, void *);
78 fb1a36c0 2022-01-09 op static void handle_handshake(int, short, void *);
79 fb1a36c0 2022-01-09 op static void client_read(struct bufferevent *, void *);
80 fb1a36c0 2022-01-09 op static void client_write(struct bufferevent *, void *);
81 fb1a36c0 2022-01-09 op static void client_error(struct bufferevent *, short, void *);
82 fb1a36c0 2022-01-09 op static void client_tls_readcb(int, short, void *);
83 fb1a36c0 2022-01-09 op static void client_tls_writecb(int, short, void *);
84 fb1a36c0 2022-01-09 op static void close_conn(struct client *);
85 fb1a36c0 2022-01-09 op static void handle_close(int, short, void *);
87 fb1a36c0 2022-01-09 op static inline int
88 fb1a36c0 2022-01-09 op clients_tree_cmp(struct client *a, struct client *b)
90 fb1a36c0 2022-01-09 op if (a->id == b->id)
92 fb1a36c0 2022-01-09 op else if (a->id < b->id)
98 fb1a36c0 2022-01-09 op SPLAY_PROTOTYPE(clients_tree_id, client, sp_entry, clients_tree_cmp);
99 fb1a36c0 2022-01-09 op SPLAY_GENERATE(clients_tree_id, client, sp_entry, clients_tree_cmp)
102 fb1a36c0 2022-01-09 op listener_sig_handler(int sig, short event, void *d)
105 fb1a36c0 2022-01-09 op * Normal signal handler rules don't apply because libevent
106 fb1a36c0 2022-01-09 op * decouples for us.
109 fb1a36c0 2022-01-09 op switch (sig) {
111 fb1a36c0 2022-01-09 op case SIGTERM:
112 fb1a36c0 2022-01-09 op listener_shutdown();
114 fb1a36c0 2022-01-09 op fatalx("unexpected signal %d", sig);
119 fb1a36c0 2022-01-09 op listener(int debug, int verbose)
121 fb1a36c0 2022-01-09 op struct event ev_sigint, ev_sigterm;
122 fb1a36c0 2022-01-09 op struct passwd *pw;
124 fb1a36c0 2022-01-09 op /* listener_conf = config_new_empty(); */
126 fb1a36c0 2022-01-09 op log_init(debug, LOG_DAEMON);
127 fb1a36c0 2022-01-09 op log_setverbose(verbose);
129 fb1a36c0 2022-01-09 op if ((pw = getpwnam(KD_USER)) == NULL)
130 fb1a36c0 2022-01-09 op fatal("getpwnam");
132 fb1a36c0 2022-01-09 op if (chroot(pw->pw_dir) == -1)
133 fb1a36c0 2022-01-09 op fatal("chroot");
134 fb1a36c0 2022-01-09 op if (chdir("/") == -1)
135 fb1a36c0 2022-01-09 op fatal("chdir(\"/\")");
137 fb1a36c0 2022-01-09 op setproctitle("listener");
138 fb1a36c0 2022-01-09 op log_procinit("listener");
140 fb1a36c0 2022-01-09 op if (setgroups(1, &pw->pw_gid) ||
141 fb1a36c0 2022-01-09 op setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) ||
142 fb1a36c0 2022-01-09 op setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid))
143 fb1a36c0 2022-01-09 op fatal("can't drop privileges");
145 fb1a36c0 2022-01-09 op event_init();
147 fb1a36c0 2022-01-09 op /* Setup signal handlers(s). */
148 fb1a36c0 2022-01-09 op signal_set(&ev_sigint, SIGINT, listener_sig_handler, NULL);
149 fb1a36c0 2022-01-09 op signal_set(&ev_sigterm, SIGTERM, listener_sig_handler, NULL);
151 fb1a36c0 2022-01-09 op signal_add(&ev_sigint, NULL);
152 fb1a36c0 2022-01-09 op signal_add(&ev_sigterm, NULL);
154 fb1a36c0 2022-01-09 op signal(SIGPIPE, SIG_IGN);
155 fb1a36c0 2022-01-09 op signal(SIGHUP, SIG_IGN);
157 fb1a36c0 2022-01-09 op /* Setup pipe and event handler to the main process. */
158 fb1a36c0 2022-01-09 op if ((iev_main = malloc(sizeof(*iev_main))) == NULL)
161 fb1a36c0 2022-01-09 op imsg_init(&iev_main->ibuf, 3);
162 fb1a36c0 2022-01-09 op iev_main->handler = listener_dispatch_main;
164 fb1a36c0 2022-01-09 op /* Setup event handlers. */
165 fb1a36c0 2022-01-09 op iev_main->events = EV_READ;
166 fb1a36c0 2022-01-09 op event_set(&iev_main->ev, iev_main->ibuf.fd, iev_main->events,
167 fb1a36c0 2022-01-09 op iev_main->handler, iev_main);
168 fb1a36c0 2022-01-09 op event_add(&iev_main->ev, NULL);
170 fb1a36c0 2022-01-09 op sandbox_listener();
171 fb1a36c0 2022-01-09 op event_dispatch();
172 fb1a36c0 2022-01-09 op listener_shutdown();
176 fb1a36c0 2022-01-09 op listener_shutdown(void)
178 fb1a36c0 2022-01-09 op msgbuf_clear(&iev_main->ibuf.w);
179 fb1a36c0 2022-01-09 op close(iev_main->ibuf.fd);
181 fb1a36c0 2022-01-09 op config_clear(listener_conf);
183 fb1a36c0 2022-01-09 op free(iev_main);
185 fb1a36c0 2022-01-09 op log_info("listener exiting");
190 fb1a36c0 2022-01-09 op listener_receive_config(struct imsg *imsg, struct kd_conf **nconf,
191 fb1a36c0 2022-01-09 op struct kd_pki_conf **pki)
193 fb1a36c0 2022-01-09 op struct kd_listen_conf *listen;
196 fb1a36c0 2022-01-09 op switch (imsg->hdr.type) {
197 fb1a36c0 2022-01-09 op case IMSG_RECONF_CONF:
198 fb1a36c0 2022-01-09 op if (*nconf != NULL)
199 fb1a36c0 2022-01-09 op fatalx("%s: IMSG_RECONF_CONF already in "
200 fb1a36c0 2022-01-09 op "progress", __func__);
202 fb1a36c0 2022-01-09 op if (listener_conf != NULL)
203 fb1a36c0 2022-01-09 op fatalx("%s: don't know how reload the "
204 fb1a36c0 2022-01-09 op "configuration yet", __func__);
206 fb1a36c0 2022-01-09 op if (IMSG_DATA_SIZE(*imsg) != sizeof(struct kd_conf))
207 fb1a36c0 2022-01-09 op fatalx("%s: IMSG_RECONF_CONF wrong length: %lu",
208 fb1a36c0 2022-01-09 op __func__, IMSG_DATA_SIZE(*imsg));
209 fb1a36c0 2022-01-09 op if ((*nconf = malloc(sizeof(**nconf))) == NULL)
211 fb1a36c0 2022-01-09 op memcpy(*nconf, imsg->data, sizeof(**nconf));
212 fb1a36c0 2022-01-09 op memset(&(*nconf)->pki_head, 0, sizeof((*nconf)->pki_head));
213 fb1a36c0 2022-01-09 op memset(&(*nconf)->table_head, 0, sizeof((*nconf)->table_head));
214 fb1a36c0 2022-01-09 op memset(&(*nconf)->listen_head, 0, sizeof((*nconf)->listen_head));
216 fb1a36c0 2022-01-09 op case IMSG_RECONF_PKI:
217 fb1a36c0 2022-01-09 op if (*nconf == NULL)
218 fb1a36c0 2022-01-09 op fatalx("%s: IMSG_RECONF_PKI without "
219 fb1a36c0 2022-01-09 op "IMSG_RECONF_CONF", __func__);
220 fb1a36c0 2022-01-09 op *pki = xcalloc(1, sizeof(**pki));
221 fb1a36c0 2022-01-09 op t = imsg->data;
222 fb1a36c0 2022-01-09 op t[IMSG_DATA_SIZE(*imsg)-1] = '\0';
223 fb1a36c0 2022-01-09 op strlcpy((*pki)->name, t, sizeof((*pki)->name));
225 fb1a36c0 2022-01-09 op case IMSG_RECONF_PKI_CERT:
226 fb1a36c0 2022-01-09 op if (*pki == NULL)
227 fb1a36c0 2022-01-09 op fatalx("%s: IMSG_RECONF_PKI_CERT without "
228 fb1a36c0 2022-01-09 op "IMSG_RECONF_PKI", __func__);
229 fb1a36c0 2022-01-09 op (*pki)->certlen = IMSG_DATA_SIZE(*imsg);
230 fb1a36c0 2022-01-09 op (*pki)->cert = xmemdup(imsg->data, (*pki)->certlen);
232 fb1a36c0 2022-01-09 op case IMSG_RECONF_PKI_KEY:
233 fb1a36c0 2022-01-09 op if (*pki == NULL)
234 fb1a36c0 2022-01-09 op fatalx("%s: IMSG_RECONF_PKI_KEY without "
235 fb1a36c0 2022-01-09 op "IMSG_RECONF_PKI", __func__);
236 fb1a36c0 2022-01-09 op (*pki)->keylen = IMSG_DATA_SIZE(*imsg);
237 fb1a36c0 2022-01-09 op (*pki)->key = xmemdup(imsg->data, (*pki)->keylen);
238 fb1a36c0 2022-01-09 op STAILQ_INSERT_HEAD(&(*nconf)->pki_head, *pki, entry);
241 fb1a36c0 2022-01-09 op case IMSG_RECONF_LISTEN:
242 fb1a36c0 2022-01-09 op if (*nconf == NULL)
243 fb1a36c0 2022-01-09 op fatalx("%s: IMSG_RECONF_LISTEN without "
244 fb1a36c0 2022-01-09 op "IMSG_RECONF_CONF", __func__);
245 fb1a36c0 2022-01-09 op if (IMSG_DATA_SIZE(*imsg) != sizeof(*listen))
246 fb1a36c0 2022-01-09 op fatalx("%s: IMSG_RECONF_LISTEN wrong length: %lu",
247 fb1a36c0 2022-01-09 op __func__, IMSG_DATA_SIZE(*imsg));
248 fb1a36c0 2022-01-09 op listen = xcalloc(1, sizeof(*listen));
249 fb1a36c0 2022-01-09 op memcpy(listen, imsg->data, sizeof(*listen));
250 fb1a36c0 2022-01-09 op memset(&listen->entry, 0, sizeof(listen->entry));
251 fb1a36c0 2022-01-09 op if ((listen->fd = imsg->fd) == -1)
252 fb1a36c0 2022-01-09 op fatalx("%s: IMSG_RECONF_LISTEN no fd",
254 fb1a36c0 2022-01-09 op listen->auth_table = NULL;
255 fb1a36c0 2022-01-09 op memset(&listen->ev, 0, sizeof(listen->ev));
256 fb1a36c0 2022-01-09 op STAILQ_INSERT_HEAD(&(*nconf)->listen_head, listen, entry);
258 fb1a36c0 2022-01-09 op case IMSG_RECONF_END:
259 fb1a36c0 2022-01-09 op if (*nconf == NULL)
260 fb1a36c0 2022-01-09 op fatalx("%s: IMSG_RECONF_END without "
261 fb1a36c0 2022-01-09 op "IMSG_RECONF_CONF", __func__);
262 fb1a36c0 2022-01-09 op /* merge_config(listener_conf, nconf); */
263 fb1a36c0 2022-01-09 op apply_config(*nconf);
264 fb1a36c0 2022-01-09 op *nconf = NULL;
269 fb1a36c0 2022-01-09 op static inline struct kd_listen_conf *
270 fb1a36c0 2022-01-09 op listen_by_id(uint32_t id)
272 fb1a36c0 2022-01-09 op struct kd_listen_conf *l;
274 fb1a36c0 2022-01-09 op STAILQ_FOREACH(l, &listener_conf->listen_head, entry) {
275 fb1a36c0 2022-01-09 op if (l->id == id)
282 fb1a36c0 2022-01-09 op listener_dispatch_main(int fd, short event, void *d)
284 fb1a36c0 2022-01-09 op static struct kd_conf *nconf;
285 fb1a36c0 2022-01-09 op static struct kd_pki_conf *pki;
286 fb1a36c0 2022-01-09 op struct kd_listen_conf *listen;
287 fb1a36c0 2022-01-09 op struct client *client, find;
288 fb1a36c0 2022-01-09 op struct imsg imsg;
289 fb1a36c0 2022-01-09 op struct imsgev *iev = d;
290 fb1a36c0 2022-01-09 op struct imsgbuf *ibuf;
292 fb1a36c0 2022-01-09 op int shut = 0;
294 fb1a36c0 2022-01-09 op ibuf = &iev->ibuf;
296 fb1a36c0 2022-01-09 op if (event & EV_READ) {
297 fb1a36c0 2022-01-09 op if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
298 fb1a36c0 2022-01-09 op fatal("imsg_read error");
299 fb1a36c0 2022-01-09 op if (n == 0) /* Connection closed. */
302 fb1a36c0 2022-01-09 op if (event & EV_WRITE) {
303 fb1a36c0 2022-01-09 op if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
304 fb1a36c0 2022-01-09 op fatal("msgbuf_write");
305 fb1a36c0 2022-01-09 op if (n == 0) /* Connection closed. */
310 fb1a36c0 2022-01-09 op if ((n = imsg_get(ibuf, &imsg)) == -1)
311 fb1a36c0 2022-01-09 op fatal("%s: imsg_get error", __func__);
312 fb1a36c0 2022-01-09 op if (n == 0) /* No more messages. */
315 fb1a36c0 2022-01-09 op switch (imsg.hdr.type) {
316 fb1a36c0 2022-01-09 op case IMSG_CONTROLFD:
317 fb1a36c0 2022-01-09 op if ((fd = imsg.fd) == -1)
318 fb1a36c0 2022-01-09 op fatalx("%s: expected to receive imsg "
319 fb1a36c0 2022-01-09 op "control fd but didn't receive any",
321 fb1a36c0 2022-01-09 op /* Listen on control socket. */
322 fb1a36c0 2022-01-09 op control_listen(fd);
324 fb1a36c0 2022-01-09 op case IMSG_RECONF_CONF:
325 fb1a36c0 2022-01-09 op case IMSG_RECONF_PKI:
326 fb1a36c0 2022-01-09 op case IMSG_RECONF_PKI_CERT:
327 fb1a36c0 2022-01-09 op case IMSG_RECONF_PKI_KEY:
328 fb1a36c0 2022-01-09 op case IMSG_RECONF_LISTEN:
329 fb1a36c0 2022-01-09 op case IMSG_RECONF_END:
330 fb1a36c0 2022-01-09 op listener_receive_config(&imsg, &nconf, &pki);
332 fb1a36c0 2022-01-09 op case IMSG_AUTH:
333 fb1a36c0 2022-01-09 op find.id = imsg.hdr.peerid;
334 fb1a36c0 2022-01-09 op client = SPLAY_FIND(clients_tree_id, &clients, &find);
335 fb1a36c0 2022-01-09 op if (client == NULL) {
336 fb1a36c0 2022-01-09 op if (imsg.fd != -1)
337 fb1a36c0 2022-01-09 op close(imsg.fd);
340 fb1a36c0 2022-01-09 op if (imsg.fd == -1) {
341 fb1a36c0 2022-01-09 op log_info("got fd = -1, auth failed?");
342 fb1a36c0 2022-01-09 op close_conn(client);
345 fb1a36c0 2022-01-09 op imsg_init(&client->iev.ibuf, imsg.fd);
346 fb1a36c0 2022-01-09 op client->iev.events = EV_READ;
347 fb1a36c0 2022-01-09 op client->iev.handler = listener_dispatch_client;
348 fb1a36c0 2022-01-09 op event_set(&client->iev.ev, client->iev.ibuf.fd,
349 fb1a36c0 2022-01-09 op client->iev.events, client->iev.handler, client);
350 fb1a36c0 2022-01-09 op listener_imsg_compose_client(client, IMSG_AUTH,
351 fb1a36c0 2022-01-09 op client->id, imsg.data, IMSG_DATA_SIZE(imsg));
353 fb1a36c0 2022-01-09 op case IMSG_AUTH_DIR:
354 fb1a36c0 2022-01-09 op find.id = imsg.hdr.peerid;
355 fb1a36c0 2022-01-09 op client = SPLAY_FIND(clients_tree_id, &clients, &find);
356 fb1a36c0 2022-01-09 op if (client == NULL) {
357 fb1a36c0 2022-01-09 op log_info("got AUTH_DIR but client gone");
361 fb1a36c0 2022-01-09 op listener_imsg_compose_client(client, IMSG_AUTH_DIR,
362 fb1a36c0 2022-01-09 op 0, imsg.data, IMSG_DATA_SIZE(imsg));
364 fb1a36c0 2022-01-09 op client->bev = bufferevent_new(client->fd,
365 fb1a36c0 2022-01-09 op client_read, client_write, client_error,
367 fb1a36c0 2022-01-09 op if (client->bev == NULL) {
368 fb1a36c0 2022-01-09 op log_info("failed to allocate client buffer");
369 fb1a36c0 2022-01-09 op close_conn(client);
373 fb1a36c0 2022-01-09 op #if HAVE_EVENT2
374 fb1a36c0 2022-01-09 op evbuffer_unfreeze(client->bev->input, 0);
375 fb1a36c0 2022-01-09 op evbuffer_unfreeze(client->bev->output, 1);
378 fb1a36c0 2022-01-09 op listen = listen_by_id(client->lid);
379 fb1a36c0 2022-01-09 op if (listen->flags & L_TLS) {
380 fb1a36c0 2022-01-09 op event_set(&client->bev->ev_read, client->fd,
381 fb1a36c0 2022-01-09 op EV_READ, client_tls_readcb, client->bev);
382 fb1a36c0 2022-01-09 op event_set(&client->bev->ev_write, client->fd,
383 fb1a36c0 2022-01-09 op EV_WRITE, client_tls_writecb, client->bev);
387 fb1a36c0 2022-01-09 op * Read or write at least a header before
388 fb1a36c0 2022-01-09 op * firing the callbacks. High watermark of 0
389 fb1a36c0 2022-01-09 op * to never stop reading/writing; probably to
390 fb1a36c0 2022-01-09 op * be revisited.
392 fb1a36c0 2022-01-09 op /* bufferevent_setwatermark(client->bev, EV_READ|EV_WRITE, */
393 fb1a36c0 2022-01-09 op /* sizeof(struct np_msg_header), 0); */
394 fb1a36c0 2022-01-09 op bufferevent_enable(client->bev, EV_READ|EV_WRITE);
398 fb1a36c0 2022-01-09 op log_debug("%s: unexpected imsg %d", __func__,
399 fb1a36c0 2022-01-09 op imsg.hdr.type);
402 fb1a36c0 2022-01-09 op imsg_free(&imsg);
406 fb1a36c0 2022-01-09 op listener_imsg_event_add(iev, d);
408 fb1a36c0 2022-01-09 op /* This pipe is dead. Remove its event handler. */
409 fb1a36c0 2022-01-09 op event_del(&iev->ev);
410 fb1a36c0 2022-01-09 op log_warnx("pipe closed, shutting down...");
411 fb1a36c0 2022-01-09 op event_loopexit(NULL);
416 fb1a36c0 2022-01-09 op listener_imsg_compose_main(int type, uint32_t peerid, const void *data,
417 fb1a36c0 2022-01-09 op uint16_t datalen)
419 fb1a36c0 2022-01-09 op return imsg_compose_event(iev_main, type, peerid, 0, -1, data,
424 fb1a36c0 2022-01-09 op listener_imsg_event_add(struct imsgev *iev, void *d)
426 fb1a36c0 2022-01-09 op iev->events = EV_READ;
427 fb1a36c0 2022-01-09 op if (iev->ibuf.w.queued)
428 fb1a36c0 2022-01-09 op iev->events |= EV_WRITE;
430 fb1a36c0 2022-01-09 op event_del(&iev->ev);
431 fb1a36c0 2022-01-09 op event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, d);
432 fb1a36c0 2022-01-09 op event_add(&iev->ev, NULL);
436 fb1a36c0 2022-01-09 op listener_dispatch_client(int fd, short event, void *d)
438 fb1a36c0 2022-01-09 op struct client find, *client = d;
439 fb1a36c0 2022-01-09 op struct imsg imsg;
440 fb1a36c0 2022-01-09 op struct imsgev *iev;
441 fb1a36c0 2022-01-09 op struct imsgbuf *ibuf;
443 fb1a36c0 2022-01-09 op int r, shut = 0;
445 fb1a36c0 2022-01-09 op iev = &client->iev;
446 fb1a36c0 2022-01-09 op ibuf = &iev->ibuf;
448 fb1a36c0 2022-01-09 op if (event & EV_READ) {
449 fb1a36c0 2022-01-09 op if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
450 fb1a36c0 2022-01-09 op fatal("imsg_read error");
451 fb1a36c0 2022-01-09 op if (n == 0) /* Connection closed */
455 fb1a36c0 2022-01-09 op if (event & EV_WRITE) {
456 fb1a36c0 2022-01-09 op if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
457 fb1a36c0 2022-01-09 op fatal("msgbuf_write");
458 fb1a36c0 2022-01-09 op if (n == 0) /* Connection closed. */
463 fb1a36c0 2022-01-09 op if ((n = imsg_get(ibuf, &imsg)) == -1)
464 fb1a36c0 2022-01-09 op fatal("%s: imsg_get error", __func__);
465 fb1a36c0 2022-01-09 op if (n == 0) /* No more messages. */
468 fb1a36c0 2022-01-09 op switch (imsg.hdr.type) {
469 fb1a36c0 2022-01-09 op case IMSG_BUF:
470 fb1a36c0 2022-01-09 op find.id = imsg.hdr.peerid;
471 fb1a36c0 2022-01-09 op client = SPLAY_FIND(clients_tree_id, &clients, &find);
472 fb1a36c0 2022-01-09 op if (client == NULL) {
473 fb1a36c0 2022-01-09 op log_info("got IMSG_BUF but client (%d) gone",
474 fb1a36c0 2022-01-09 op imsg.hdr.peerid);
477 fb1a36c0 2022-01-09 op r = bufferevent_write(client->bev, imsg.data,
478 fb1a36c0 2022-01-09 op IMSG_DATA_SIZE(imsg));
479 fb1a36c0 2022-01-09 op if (r == -1) {
480 fb1a36c0 2022-01-09 op log_warn("%s: bufferevent_write failed",
482 fb1a36c0 2022-01-09 op close_conn(client);
487 fb1a36c0 2022-01-09 op case IMSG_MSIZE:
488 fb1a36c0 2022-01-09 op if (IMSG_DATA_SIZE(imsg) != sizeof(client->msize))
489 fb1a36c0 2022-01-09 op fatal("IMSG_MSIZE size mismatch: "
490 fb1a36c0 2022-01-09 op "got %zu want %zu", IMSG_DATA_SIZE(imsg),
491 fb1a36c0 2022-01-09 op sizeof(client->msize));
493 fb1a36c0 2022-01-09 op memcpy(&client->msize, imsg.data,
494 fb1a36c0 2022-01-09 op sizeof(client->msize));
496 fb1a36c0 2022-01-09 op if (client->msize == 0)
497 fb1a36c0 2022-01-09 op fatal("IMSG_MSIZE got msize = 0");
501 fb1a36c0 2022-01-09 op case IMSG_CLOSE:
503 fb1a36c0 2022-01-09 op * Both EVBUFFER_READ or EVBUFFER_WRITE should
506 fb1a36c0 2022-01-09 op client_error(client->bev, EVBUFFER_READ, client);
510 fb1a36c0 2022-01-09 op log_debug("%s: unexpected imsg %d", __func__,
511 fb1a36c0 2022-01-09 op imsg.hdr.type);
514 fb1a36c0 2022-01-09 op imsg_free(&imsg);
518 fb1a36c0 2022-01-09 op listener_imsg_event_add(iev, d);
520 fb1a36c0 2022-01-09 op /* This pipe is dead. Remove its handler */
521 fb1a36c0 2022-01-09 op log_debug("client proc vanished");
522 fb1a36c0 2022-01-09 op close_conn(client);
527 fb1a36c0 2022-01-09 op listener_imsg_compose_client(struct client *client, int type,
528 fb1a36c0 2022-01-09 op uint32_t peerid, const void *data, uint16_t len)
532 fb1a36c0 2022-01-09 op if ((ret = imsg_compose(&client->iev.ibuf, type, peerid, 0, -1,
533 fb1a36c0 2022-01-09 op data, len)) != -1)
534 fb1a36c0 2022-01-09 op listener_imsg_event_add(&client->iev, client);
539 fb1a36c0 2022-01-09 op static inline struct kd_pki_conf *
540 fb1a36c0 2022-01-09 op pki_by_name(const char *name)
542 fb1a36c0 2022-01-09 op struct kd_pki_conf *pki;
544 fb1a36c0 2022-01-09 op STAILQ_FOREACH(pki, &listener_conf->pki_head, entry) {
545 fb1a36c0 2022-01-09 op if (!strcmp(name, pki->name))
553 fb1a36c0 2022-01-09 op apply_config(struct kd_conf *conf)
555 fb1a36c0 2022-01-09 op struct kd_pki_conf *pki;
556 fb1a36c0 2022-01-09 op struct kd_listen_conf *listen;
558 fb1a36c0 2022-01-09 op listener_conf = conf;
560 fb1a36c0 2022-01-09 op /* prepare the various tls_config */
561 fb1a36c0 2022-01-09 op STAILQ_FOREACH(pki, &listener_conf->pki_head, entry) {
562 fb1a36c0 2022-01-09 op if ((pki->tlsconf = tls_config_new()) == NULL)
563 fb1a36c0 2022-01-09 op fatal("tls_config_new");
564 fb1a36c0 2022-01-09 op tls_config_verify_client_optional(pki->tlsconf);
565 fb1a36c0 2022-01-09 op tls_config_insecure_noverifycert(pki->tlsconf);
566 fb1a36c0 2022-01-09 op if (tls_config_set_keypair_mem(pki->tlsconf,
567 fb1a36c0 2022-01-09 op pki->cert, pki->certlen,
568 fb1a36c0 2022-01-09 op pki->key, pki->keylen) == -1)
569 fb1a36c0 2022-01-09 op fatalx("tls_config_set_keypair_mem: %s",
570 fb1a36c0 2022-01-09 op tls_config_error(pki->tlsconf));
573 fb1a36c0 2022-01-09 op /* prepare and kickoff the listeners */
574 fb1a36c0 2022-01-09 op STAILQ_FOREACH(listen, &listener_conf->listen_head, entry) {
575 fb1a36c0 2022-01-09 op if ((listen->ctx = tls_server()) == NULL)
576 fb1a36c0 2022-01-09 op fatal("tls_server");
578 fb1a36c0 2022-01-09 op pki = pki_by_name(listen->pki);
579 fb1a36c0 2022-01-09 op if (tls_configure(listen->ctx, pki->tlsconf) == -1)
580 fb1a36c0 2022-01-09 op fatalx("tls_configure: %s",
581 fb1a36c0 2022-01-09 op tls_config_error(pki->tlsconf));
583 fb1a36c0 2022-01-09 op event_set(&listen->ev, listen->fd, EV_READ|EV_PERSIST,
584 fb1a36c0 2022-01-09 op handle_accept, listen);
585 fb1a36c0 2022-01-09 op event_add(&listen->ev, NULL);
589 fb1a36c0 2022-01-09 op static inline void
590 fb1a36c0 2022-01-09 op yield_r(struct client *c, void (*fn)(int, short, void *))
592 fb1a36c0 2022-01-09 op if (event_pending(&c->event, EV_WRITE|EV_READ, NULL))
593 fb1a36c0 2022-01-09 op event_del(&c->event);
594 fb1a36c0 2022-01-09 op event_set(&c->event, c->fd, EV_READ, fn, c);
595 fb1a36c0 2022-01-09 op event_add(&c->event, NULL);
598 fb1a36c0 2022-01-09 op static inline void
599 fb1a36c0 2022-01-09 op yield_w(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_WRITE, fn, c);
604 fb1a36c0 2022-01-09 op event_add(&c->event, NULL);
607 fb1a36c0 2022-01-09 op static inline uint32_t
608 fb1a36c0 2022-01-09 op random_id(void)
610 fb1a36c0 2022-01-09 op #if HAVE_ARC4RANDOM
611 fb1a36c0 2022-01-09 op # define RANDID() arc4random()
613 fb1a36c0 2022-01-09 op /* not as pretty as a random id */
614 fb1a36c0 2022-01-09 op static uint32_t counter = 0;
615 fb1a36c0 2022-01-09 op # define RANDID() counter++
618 fb1a36c0 2022-01-09 op struct client find, *res;
621 fb1a36c0 2022-01-09 op find.id = RANDID();
622 fb1a36c0 2022-01-09 op res = SPLAY_FIND(clients_tree_id, &clients, &find);
623 fb1a36c0 2022-01-09 op if (res == NULL)
624 fb1a36c0 2022-01-09 op return find.id;
627 fb1a36c0 2022-01-09 op #undef RANDID
631 fb1a36c0 2022-01-09 op handle_accept(int fd, short ev, void *data)
633 fb1a36c0 2022-01-09 op struct kd_listen_conf *listen = data;
634 fb1a36c0 2022-01-09 op struct client *c;
637 fb1a36c0 2022-01-09 op if ((s = accept(fd, NULL, NULL)) == -1) {
638 fb1a36c0 2022-01-09 op log_warn("accept");
642 fb1a36c0 2022-01-09 op c = xcalloc(1, sizeof(*c));
643 fb1a36c0 2022-01-09 op c->msize = MSIZE9P;
644 fb1a36c0 2022-01-09 op c->lid = listen->id;
645 fb1a36c0 2022-01-09 op c->iev.ibuf.fd = -1;
647 fb1a36c0 2022-01-09 op if (tls_accept_socket(listen->ctx, &c->ctx, s) == -1) {
648 fb1a36c0 2022-01-09 op log_warnx("tls_accept_socket: %s",
649 fb1a36c0 2022-01-09 op tls_error(listen->ctx));
656 fb1a36c0 2022-01-09 op c->id = random_id();
658 fb1a36c0 2022-01-09 op SPLAY_INSERT(clients_tree_id, &clients, c);
660 fb1a36c0 2022-01-09 op /* initialize the event */
661 fb1a36c0 2022-01-09 op event_set(&c->event, c->fd, EV_READ, NULL, NULL);
663 fb1a36c0 2022-01-09 op yield_r(c, handle_handshake);
667 fb1a36c0 2022-01-09 op handle_handshake(int fd, short ev, void *data)
669 fb1a36c0 2022-01-09 op struct client *c = data;
670 fb1a36c0 2022-01-09 op struct kd_auth_req auth;
672 fb1a36c0 2022-01-09 op const char *hash;
674 fb1a36c0 2022-01-09 op switch (r = tls_handshake(c->ctx)) {
675 fb1a36c0 2022-01-09 op case TLS_WANT_POLLIN:
676 fb1a36c0 2022-01-09 op yield_r(c, handle_handshake);
678 fb1a36c0 2022-01-09 op case TLS_WANT_POLLOUT:
679 fb1a36c0 2022-01-09 op yield_w(c, handle_handshake);
682 fb1a36c0 2022-01-09 op log_debug("handhsake failed: %s", tls_error(c->ctx));
683 fb1a36c0 2022-01-09 op close_conn(c);
687 fb1a36c0 2022-01-09 op if ((hash = tls_peer_cert_hash(c->ctx)) == NULL) {
688 fb1a36c0 2022-01-09 op log_warnx("client didn't provide certificate");
689 fb1a36c0 2022-01-09 op close_conn(c);
693 fb1a36c0 2022-01-09 op memset(&auth, 0, sizeof(auth));
694 fb1a36c0 2022-01-09 op auth.listen_id = c->lid;
695 fb1a36c0 2022-01-09 op strlcpy(auth.hash, hash, sizeof(auth.hash));
696 fb1a36c0 2022-01-09 op log_debug("sending hash %s", auth.hash);
698 fb1a36c0 2022-01-09 op listener_imsg_compose_main(IMSG_AUTH_TLS, c->id,
699 fb1a36c0 2022-01-09 op &auth, sizeof(auth));
703 fb1a36c0 2022-01-09 op client_read(struct bufferevent *bev, void *d)
705 fb1a36c0 2022-01-09 op struct client *client = d;
706 fb1a36c0 2022-01-09 op struct evbuffer *src = EVBUFFER_INPUT(bev);
707 fb1a36c0 2022-01-09 op uint32_t len;
710 fb1a36c0 2022-01-09 op if (EVBUFFER_LENGTH(src) < 4)
713 fb1a36c0 2022-01-09 op memcpy(&len, EVBUFFER_DATA(src), sizeof(len));
714 fb1a36c0 2022-01-09 op len = le32toh(len);
715 fb1a36c0 2022-01-09 op log_debug("expecting a message %"PRIu32" bytes long "
716 fb1a36c0 2022-01-09 op "(of wich %zu already read)",
717 fb1a36c0 2022-01-09 op len, EVBUFFER_LENGTH(src));
719 fb1a36c0 2022-01-09 op if (len < HEADERSIZE) {
720 fb1a36c0 2022-01-09 op log_warnx("invalid message size %d (too low)", len);
721 fb1a36c0 2022-01-09 op client_error(bev, EVBUFFER_READ, client);
725 fb1a36c0 2022-01-09 op if (len > client->msize) {
726 fb1a36c0 2022-01-09 op log_warnx("incoming message bigger than msize "
727 fb1a36c0 2022-01-09 op "(%"PRIu32" vs %"PRIu32")", len, client->msize);
728 fb1a36c0 2022-01-09 op client_error(bev, EVBUFFER_READ, client);
732 fb1a36c0 2022-01-09 op if (len > EVBUFFER_LENGTH(src))
735 fb1a36c0 2022-01-09 op listener_imsg_compose_client(client, IMSG_BUF, client->id,
736 fb1a36c0 2022-01-09 op EVBUFFER_DATA(src), len);
737 fb1a36c0 2022-01-09 op evbuffer_drain(src, len);
742 fb1a36c0 2022-01-09 op client_write(struct bufferevent *bev, void *d)
745 fb1a36c0 2022-01-09 op * here we can do some fancy logic like deciding when to call
747 fb1a36c0 2022-01-09 op * (*bev->errorcb)(bev, EVBUFFER_WRITE, bev->cbarg)
749 fb1a36c0 2022-01-09 op * to signal the end of the transaction.
756 fb1a36c0 2022-01-09 op client_error(struct bufferevent *bev, short err, void *d)
758 fb1a36c0 2022-01-09 op struct client *client = d;
759 fb1a36c0 2022-01-09 op struct evbuffer *buf;
761 fb1a36c0 2022-01-09 op if (err & EVBUFFER_ERROR) {
762 fb1a36c0 2022-01-09 op if (errno == EFBIG) {
763 fb1a36c0 2022-01-09 op bufferevent_enable(bev, EV_READ);
766 fb1a36c0 2022-01-09 op log_debug("buffer event error");
767 fb1a36c0 2022-01-09 op close_conn(client);
771 fb1a36c0 2022-01-09 op if (err & EVBUFFER_EOF) {
772 fb1a36c0 2022-01-09 op close_conn(client);
776 fb1a36c0 2022-01-09 op if (err & (EVBUFFER_READ|EVBUFFER_WRITE)) {
777 fb1a36c0 2022-01-09 op bufferevent_disable(bev, EV_READ|EV_WRITE);
778 fb1a36c0 2022-01-09 op client->done = 1;
780 fb1a36c0 2022-01-09 op buf = EVBUFFER_OUTPUT(client->bev);
781 fb1a36c0 2022-01-09 op if (EVBUFFER_LENGTH(buf) != 0) {
782 fb1a36c0 2022-01-09 op /* finish writing all the data first */
783 fb1a36c0 2022-01-09 op bufferevent_enable(client->bev, EV_WRITE);
787 fb1a36c0 2022-01-09 op close_conn(client);
791 fb1a36c0 2022-01-09 op log_warnx("unknown event error, closing client connection");
792 fb1a36c0 2022-01-09 op close_conn(client);
796 fb1a36c0 2022-01-09 op client_tls_readcb(int fd, short event, void *d)
798 fb1a36c0 2022-01-09 op struct bufferevent *bufev = d;
799 fb1a36c0 2022-01-09 op struct client *client = bufev->cbarg;
800 fb1a36c0 2022-01-09 op char buf[IBUF_READ_SIZE];
801 fb1a36c0 2022-01-09 op int what = EVBUFFER_READ;
802 fb1a36c0 2022-01-09 op int howmuch = IBUF_READ_SIZE;
806 fb1a36c0 2022-01-09 op if (event == EV_TIMEOUT) {
807 fb1a36c0 2022-01-09 op what |= EVBUFFER_TIMEOUT;
811 fb1a36c0 2022-01-09 op if (bufev->wm_read.high != 0)
812 fb1a36c0 2022-01-09 op howmuch = MIN(sizeof(buf), bufev->wm_read.high);
814 fb1a36c0 2022-01-09 op switch (ret = tls_read(client->ctx, buf, howmuch)) {
815 fb1a36c0 2022-01-09 op case TLS_WANT_POLLIN:
816 fb1a36c0 2022-01-09 op case TLS_WANT_POLLOUT:
819 fb1a36c0 2022-01-09 op what |= EVBUFFER_ERROR;
824 fb1a36c0 2022-01-09 op if (len == 0) {
825 fb1a36c0 2022-01-09 op what |= EVBUFFER_EOF;
829 fb1a36c0 2022-01-09 op if (evbuffer_add(bufev->input, buf, len) == -1) {
830 fb1a36c0 2022-01-09 op what |= EVBUFFER_ERROR;
834 fb1a36c0 2022-01-09 op event_add(&bufev->ev_read, NULL);
836 fb1a36c0 2022-01-09 op len = EVBUFFER_LENGTH(bufev->input);
837 fb1a36c0 2022-01-09 op if (bufev->wm_read.low != 0 && len < bufev->wm_read.low)
839 fb1a36c0 2022-01-09 op if (bufev->wm_read.high != 0 && len > bufev->wm_read.high) {
841 fb1a36c0 2022-01-09 op * here we could implement some read pressure
846 fb1a36c0 2022-01-09 op if (bufev->readcb != NULL)
847 fb1a36c0 2022-01-09 op (*bufev->readcb)(bufev, bufev->cbarg);
852 fb1a36c0 2022-01-09 op event_add(&bufev->ev_read, NULL);
856 fb1a36c0 2022-01-09 op (*bufev->errorcb)(bufev, what, bufev->cbarg);
860 fb1a36c0 2022-01-09 op client_tls_writecb(int fd, short event, void *d)
862 fb1a36c0 2022-01-09 op struct bufferevent *bufev = d;
863 fb1a36c0 2022-01-09 op struct client *client = bufev->cbarg;
866 fb1a36c0 2022-01-09 op short what = EVBUFFER_WRITE;
868 fb1a36c0 2022-01-09 op if (event == EV_TIMEOUT) {
869 fb1a36c0 2022-01-09 op what |= EVBUFFER_TIMEOUT;
873 fb1a36c0 2022-01-09 op if (EVBUFFER_LENGTH(bufev->output) != 0) {
874 fb1a36c0 2022-01-09 op ret = tls_write(client->ctx,
875 fb1a36c0 2022-01-09 op EVBUFFER_DATA(bufev->output),
876 fb1a36c0 2022-01-09 op EVBUFFER_LENGTH(bufev->output));
877 fb1a36c0 2022-01-09 op switch (ret) {
878 fb1a36c0 2022-01-09 op case TLS_WANT_POLLIN:
879 fb1a36c0 2022-01-09 op case TLS_WANT_POLLOUT:
882 fb1a36c0 2022-01-09 op what |= EVBUFFER_ERROR;
886 fb1a36c0 2022-01-09 op evbuffer_drain(bufev->output, len);
889 fb1a36c0 2022-01-09 op if (EVBUFFER_LENGTH(bufev->output) != 0)
890 fb1a36c0 2022-01-09 op event_add(&bufev->ev_write, NULL);
892 fb1a36c0 2022-01-09 op if (bufev->writecb != NULL &&
893 fb1a36c0 2022-01-09 op EVBUFFER_LENGTH(bufev->output) <= bufev->wm_write.low)
894 fb1a36c0 2022-01-09 op (*bufev->writecb)(bufev, bufev->cbarg);
898 fb1a36c0 2022-01-09 op event_add(&bufev->ev_write, NULL);
902 fb1a36c0 2022-01-09 op (*bufev->errorcb)(bufev, what, bufev->cbarg);
906 fb1a36c0 2022-01-09 op close_conn(struct client *c)
908 fb1a36c0 2022-01-09 op log_debug("closing connection");
910 fb1a36c0 2022-01-09 op if (c->iev.ibuf.fd != -1) {
911 fb1a36c0 2022-01-09 op listener_imsg_compose_client(c, IMSG_CONN_GONE, 0, NULL, 0);
912 fb1a36c0 2022-01-09 op imsg_flush(&c->iev.ibuf);
913 fb1a36c0 2022-01-09 op msgbuf_clear(&c->iev.ibuf.w);
914 fb1a36c0 2022-01-09 op event_del(&c->iev.ev);
915 fb1a36c0 2022-01-09 op close(c->iev.ibuf.fd);
918 fb1a36c0 2022-01-09 op handle_close(c->fd, 0, c);
922 fb1a36c0 2022-01-09 op handle_close(int fd, short ev, void *d)
924 fb1a36c0 2022-01-09 op struct client *c = d;
926 fb1a36c0 2022-01-09 op switch (tls_close(c->ctx)) {
927 fb1a36c0 2022-01-09 op case TLS_WANT_POLLIN:
928 fb1a36c0 2022-01-09 op yield_r(c, handle_close);
930 fb1a36c0 2022-01-09 op case TLS_WANT_POLLOUT:
931 fb1a36c0 2022-01-09 op yield_w(c, handle_close);
935 fb1a36c0 2022-01-09 op event_del(&c->event);
936 fb1a36c0 2022-01-09 op tls_free(c->ctx);
937 fb1a36c0 2022-01-09 op close(c->fd);