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) 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>
26 fb1a36c0 2022-01-09 op #include <sys/wait.h>
28 fb1a36c0 2022-01-09 op #include <arpa/inet.h>
29 fb1a36c0 2022-01-09 op #include <netinet/in.h>
31 fb1a36c0 2022-01-09 op #include <errno.h>
32 fb1a36c0 2022-01-09 op #include <fcntl.h>
33 fb1a36c0 2022-01-09 op #include <pwd.h>
34 fb1a36c0 2022-01-09 op #include <signal.h>
35 fb1a36c0 2022-01-09 op #include <stdint.h>
36 fb1a36c0 2022-01-09 op #include <stdio.h>
37 fb1a36c0 2022-01-09 op #include <stdlib.h>
38 fb1a36c0 2022-01-09 op #include <string.h>
39 fb1a36c0 2022-01-09 op #include <syslog.h>
40 fb1a36c0 2022-01-09 op #include <unistd.h>
42 fb1a36c0 2022-01-09 op #include "client.h"
43 fb1a36c0 2022-01-09 op #include "control.h"
44 fb1a36c0 2022-01-09 op #include "kamid.h"
45 fb1a36c0 2022-01-09 op #include "listener.h"
46 fb1a36c0 2022-01-09 op #include "log.h"
47 fb1a36c0 2022-01-09 op #include "sandbox.h"
48 fb1a36c0 2022-01-09 op #include "table.h"
49 fb1a36c0 2022-01-09 op #include "utils.h"
51 fb1a36c0 2022-01-09 op enum kd_process {
53 fb1a36c0 2022-01-09 op PROC_LISTENER,
54 fb1a36c0 2022-01-09 op PROC_CLIENTCONN,
57 fb1a36c0 2022-01-09 op const char *saved_argv0;
58 fb1a36c0 2022-01-09 op static int debug, nflag;
61 fb1a36c0 2022-01-09 op __dead void usage(void);
63 fb1a36c0 2022-01-09 op void main_sig_handler(int, short, void *);
64 fb1a36c0 2022-01-09 op void main_dispatch_listener(int, short, void *);
65 fb1a36c0 2022-01-09 op int main_reload(void);
66 fb1a36c0 2022-01-09 op int main_imsg_send_config(struct kd_conf *);
67 fb1a36c0 2022-01-09 op void main_dispatch_listener(int, short, void *);
68 fb1a36c0 2022-01-09 op __dead void main_shutdown(void);
70 fb1a36c0 2022-01-09 op static pid_t start_child(enum kd_process, int, int, int);
72 fb1a36c0 2022-01-09 op struct kd_conf *main_conf;
73 fb1a36c0 2022-01-09 op static struct imsgev *iev_listener;
74 fb1a36c0 2022-01-09 op const char *conffile;
75 fb1a36c0 2022-01-09 op pid_t listener_pid;
76 fb1a36c0 2022-01-09 op uint32_t cmd_opts;
81 fb1a36c0 2022-01-09 op fprintf(stderr, "usage: %s [-dnv] [-f file] [-s socket]\n",
82 fb1a36c0 2022-01-09 op getprogname());
87 fb1a36c0 2022-01-09 op main(int argc, char **argv)
89 fb1a36c0 2022-01-09 op struct event ev_sigint, ev_sigterm, ev_sighup;
91 fb1a36c0 2022-01-09 op int listener_flag = 0, client_flag = 0;
92 fb1a36c0 2022-01-09 op int pipe_main2listener[2];
93 fb1a36c0 2022-01-09 op int control_fd;
94 fb1a36c0 2022-01-09 op const char *csock;
96 fb1a36c0 2022-01-09 op conffile = KD_CONF_FILE;
97 fb1a36c0 2022-01-09 op csock = KD_SOCKET;
99 fb1a36c0 2022-01-09 op log_init(1, LOG_DAEMON); /* Log to stderr until deamonized. */
100 fb1a36c0 2022-01-09 op log_setverbose(1);
102 fb1a36c0 2022-01-09 op saved_argv0 = argv[0];
103 fb1a36c0 2022-01-09 op if (saved_argv0 == NULL)
104 fb1a36c0 2022-01-09 op saved_argv0 = "kamid";
106 fb1a36c0 2022-01-09 op while ((ch = getopt(argc, argv, "D:df:nsT:v")) != -1) {
107 fb1a36c0 2022-01-09 op switch (ch) {
109 fb1a36c0 2022-01-09 op if (cmdline_symset(optarg) == -1)
110 fb1a36c0 2022-01-09 op log_warnx("could not parse macro definition %s",
117 fb1a36c0 2022-01-09 op conffile = optarg;
123 fb1a36c0 2022-01-09 op csock = optarg;
126 fb1a36c0 2022-01-09 op switch (*optarg) {
128 fb1a36c0 2022-01-09 op client_flag = 1;
131 fb1a36c0 2022-01-09 op listener_flag = 1;
134 fb1a36c0 2022-01-09 op fatalx("invalid process spec %c", *optarg);
145 fb1a36c0 2022-01-09 op argc -= optind;
146 fb1a36c0 2022-01-09 op argv += optind;
147 fb1a36c0 2022-01-09 op if (argc > 0 || (listener_flag && client_flag))
150 fb1a36c0 2022-01-09 op if (client_flag)
151 fb1a36c0 2022-01-09 op client(debug, verbose);
152 fb1a36c0 2022-01-09 op else if (listener_flag)
153 fb1a36c0 2022-01-09 op listener(debug, verbose);
155 fb1a36c0 2022-01-09 op if ((main_conf = parse_config(conffile)) == NULL)
159 fb1a36c0 2022-01-09 op fprintf(stderr, "configuration OK\n");
163 fb1a36c0 2022-01-09 op /* Check for root privileges. */
164 fb1a36c0 2022-01-09 op if (geteuid())
165 fb1a36c0 2022-01-09 op fatalx("need root privileges");
167 fb1a36c0 2022-01-09 op /* Check for assigned daemon user. */
168 fb1a36c0 2022-01-09 op if (getpwnam(KD_USER) == NULL)
169 fb1a36c0 2022-01-09 op fatalx("unknown user %s", KD_USER);
171 fb1a36c0 2022-01-09 op log_init(debug, LOG_DAEMON);
172 fb1a36c0 2022-01-09 op log_setverbose(verbose);
175 fb1a36c0 2022-01-09 op daemon(1, 0);
177 fb1a36c0 2022-01-09 op log_info("startup");
179 fb1a36c0 2022-01-09 op if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
180 fb1a36c0 2022-01-09 op PF_UNSPEC, pipe_main2listener) == -1)
181 fb1a36c0 2022-01-09 op fatal("main2listener socketpair");
183 fb1a36c0 2022-01-09 op /* Start children. */
184 fb1a36c0 2022-01-09 op listener_pid = start_child(PROC_LISTENER, pipe_main2listener[1],
185 fb1a36c0 2022-01-09 op debug, verbose);
187 fb1a36c0 2022-01-09 op log_procinit("main");
189 fb1a36c0 2022-01-09 op event_init();
191 fb1a36c0 2022-01-09 op /* Setup signal handler */
192 fb1a36c0 2022-01-09 op signal_set(&ev_sigint, SIGINT, main_sig_handler, NULL);
193 fb1a36c0 2022-01-09 op signal_set(&ev_sigterm, SIGTERM, main_sig_handler, NULL);
194 fb1a36c0 2022-01-09 op signal_set(&ev_sighup, SIGHUP, main_sig_handler, NULL);
196 fb1a36c0 2022-01-09 op signal_add(&ev_sigint, NULL);
197 fb1a36c0 2022-01-09 op signal_add(&ev_sigterm, NULL);
198 fb1a36c0 2022-01-09 op signal_add(&ev_sighup, NULL);
200 fb1a36c0 2022-01-09 op signal(SIGCHLD, SIG_IGN);
201 fb1a36c0 2022-01-09 op signal(SIGPIPE, SIG_IGN);
203 fb1a36c0 2022-01-09 op if ((iev_listener = malloc(sizeof(*iev_listener))) == NULL)
205 fb1a36c0 2022-01-09 op imsg_init(&iev_listener->ibuf, pipe_main2listener[0]);
206 fb1a36c0 2022-01-09 op iev_listener->handler = main_dispatch_listener;
208 fb1a36c0 2022-01-09 op /* Setup event handlers for pipes to listener. */
209 fb1a36c0 2022-01-09 op iev_listener->events = EV_READ;
210 fb1a36c0 2022-01-09 op event_set(&iev_listener->ev, iev_listener->ibuf.fd,
211 fb1a36c0 2022-01-09 op iev_listener->events, iev_listener->handler, iev_listener);
212 fb1a36c0 2022-01-09 op event_add(&iev_listener->ev, NULL);
214 fb1a36c0 2022-01-09 op if ((control_fd = control_init(csock)) == -1)
215 fb1a36c0 2022-01-09 op fatalx("control socket setup failed");
216 a80d2bca 2022-01-23 op control_listen(control_fd);
218 fb1a36c0 2022-01-09 op main_imsg_send_config(main_conf);
220 fb1a36c0 2022-01-09 op sandbox_main();
222 fb1a36c0 2022-01-09 op event_dispatch();
224 fb1a36c0 2022-01-09 op main_shutdown();
229 fb1a36c0 2022-01-09 op main_sig_handler(int sig, short event, void *arg)
232 fb1a36c0 2022-01-09 op * Normal signal handler rules don't apply because libevent
233 fb1a36c0 2022-01-09 op * decouples for us.
236 fb1a36c0 2022-01-09 op switch (sig) {
237 fb1a36c0 2022-01-09 op case SIGTERM:
239 fb1a36c0 2022-01-09 op main_shutdown();
242 fb1a36c0 2022-01-09 op if (main_reload() == -1)
243 fb1a36c0 2022-01-09 op log_warnx("configuration reload failed");
245 fb1a36c0 2022-01-09 op log_debug("configuration reloaded");
248 fb1a36c0 2022-01-09 op fatalx("unexpected signal %d", sig);
252 fb1a36c0 2022-01-09 op static inline struct table *
253 fb1a36c0 2022-01-09 op auth_table_by_id(uint32_t id)
255 fb1a36c0 2022-01-09 op struct kd_listen_conf *listen;
257 fb1a36c0 2022-01-09 op STAILQ_FOREACH(listen, &main_conf->listen_head, entry) {
258 fb1a36c0 2022-01-09 op if (listen->id == id)
259 fb1a36c0 2022-01-09 op return listen->auth_table;
265 fb1a36c0 2022-01-09 op static inline struct table *
266 fb1a36c0 2022-01-09 op virtual_table_by_id(uint32_t id)
268 fb1a36c0 2022-01-09 op struct kd_listen_conf *listen;
270 fb1a36c0 2022-01-09 op STAILQ_FOREACH(listen, &main_conf->listen_head, entry) {
271 fb1a36c0 2022-01-09 op if (listen->id == id)
272 fb1a36c0 2022-01-09 op return listen->virtual_table;
278 fb1a36c0 2022-01-09 op static inline struct table *
279 fb1a36c0 2022-01-09 op userdata_table_by_id(uint32_t id)
281 fb1a36c0 2022-01-09 op struct kd_listen_conf *listen;
283 fb1a36c0 2022-01-09 op STAILQ_FOREACH(listen, &main_conf->listen_head, entry) {
284 fb1a36c0 2022-01-09 op if (listen->id == id)
285 fb1a36c0 2022-01-09 op return listen->userdata_table;
291 fb1a36c0 2022-01-09 op static inline void
292 fb1a36c0 2022-01-09 op do_auth_tls(struct imsg *imsg)
294 fb1a36c0 2022-01-09 op char *username = NULL, *user = NULL, *home = NULL, *local_user;
295 fb1a36c0 2022-01-09 op struct passwd *pw;
296 fb1a36c0 2022-01-09 op struct table *auth, *virt, *userdata;
297 fb1a36c0 2022-01-09 op struct kd_auth_req kauth;
298 1e84c7b7 2022-01-10 op struct kd_auth_proc rauth;
299 fb1a36c0 2022-01-09 op int p[2], free_home = 1;
301 fb1a36c0 2022-01-09 op if (sizeof(kauth) != IMSG_DATA_SIZE(*imsg))
302 fb1a36c0 2022-01-09 op fatal("wrong size for IMSG_AUTH_TLS: "
303 fb1a36c0 2022-01-09 op "got %lu; want %lu", IMSG_DATA_SIZE(*imsg),
304 fb1a36c0 2022-01-09 op sizeof(kauth));
305 fb1a36c0 2022-01-09 op memcpy(&kauth, imsg->data, sizeof(kauth));
307 fb1a36c0 2022-01-09 op if (memmem(kauth.hash, sizeof(kauth.hash), "", 1) == NULL)
308 fb1a36c0 2022-01-09 op fatal("non NUL-terminated hash received");
310 fb1a36c0 2022-01-09 op log_debug("tls id=%u hash=%s", kauth.listen_id, kauth.hash);
312 fb1a36c0 2022-01-09 op if ((auth = auth_table_by_id(kauth.listen_id)) == NULL)
313 fb1a36c0 2022-01-09 op fatal("request for invalid listener id %d", imsg->hdr.pid);
315 fb1a36c0 2022-01-09 op virt = virtual_table_by_id(kauth.listen_id);
316 fb1a36c0 2022-01-09 op userdata = userdata_table_by_id(kauth.listen_id);
318 fb1a36c0 2022-01-09 op if (table_lookup(auth, kauth.hash, &username) == -1) {
319 fb1a36c0 2022-01-09 op log_warnx("login failed for hash %s", kauth.hash);
323 fb1a36c0 2022-01-09 op if (virt != NULL && table_lookup(virt, username, &user) == -1) {
324 fb1a36c0 2022-01-09 op log_warnx("virtual lookup failed for user %s", username);
328 fb1a36c0 2022-01-09 op /* the local user */
329 fb1a36c0 2022-01-09 op local_user = user != NULL ? user : username;
331 fb1a36c0 2022-01-09 op if (user != NULL)
332 fb1a36c0 2022-01-09 op log_debug("virtual user %s matched local user %s",
333 fb1a36c0 2022-01-09 op username, user);
335 fb1a36c0 2022-01-09 op log_debug("matched local user %s", username);
337 fb1a36c0 2022-01-09 op if (userdata != NULL && table_lookup(userdata, username, &home)
339 fb1a36c0 2022-01-09 op log_warnx("userdata lookup failed for user %s", username);
341 fb1a36c0 2022-01-09 op } else if (userdata == NULL) {
342 fb1a36c0 2022-01-09 op if ((pw = getpwnam(local_user)) == NULL) {
343 fb1a36c0 2022-01-09 op log_warnx("getpwnam(%s) failed", local_user);
347 fb1a36c0 2022-01-09 op free_home = 0;
348 fb1a36c0 2022-01-09 op home = pw->pw_dir;
351 fb1a36c0 2022-01-09 op if (user != NULL)
352 fb1a36c0 2022-01-09 op log_debug("matched home %s for virtual user %s",
353 fb1a36c0 2022-01-09 op home, username);
355 fb1a36c0 2022-01-09 op log_debug("matched home %s for local user %s",
356 fb1a36c0 2022-01-09 op home, username);
358 1e84c7b7 2022-01-10 op memset(&rauth, 0, sizeof(rauth));
359 1e84c7b7 2022-01-10 op strlcpy(rauth.uname, local_user, sizeof(rauth.uname));
360 1e84c7b7 2022-01-10 op if (strlcpy(rauth.dir, home, sizeof(rauth.dir)) >= sizeof(rauth.dir)) {
361 1e84c7b7 2022-01-10 op log_warnx("home for %s is bigger than PATH_MAX: %s",
362 1e84c7b7 2022-01-10 op username, home);
366 fb1a36c0 2022-01-09 op if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
367 fb1a36c0 2022-01-09 op PF_UNSPEC, p) == -1)
368 fb1a36c0 2022-01-09 op fatal("socketpair");
370 fb1a36c0 2022-01-09 op start_child(PROC_CLIENTCONN, p[1], debug, verbose);
372 fb1a36c0 2022-01-09 op main_imsg_compose_listener(IMSG_AUTH, p[0], imsg->hdr.peerid,
373 1e84c7b7 2022-01-10 op &rauth, sizeof(rauth));
375 fb1a36c0 2022-01-09 op free(username);
377 fb1a36c0 2022-01-09 op if (free_home)
382 fb1a36c0 2022-01-09 op free(username);
384 fb1a36c0 2022-01-09 op if (free_home)
386 912520f9 2022-01-16 op memset(&rauth, 0, sizeof(rauth));
387 fb1a36c0 2022-01-09 op main_imsg_compose_listener(IMSG_AUTH, -1, imsg->hdr.peerid,
388 912520f9 2022-01-16 op &rauth, sizeof(rauth));
392 fb1a36c0 2022-01-09 op main_dispatch_listener(int fd, short event, void *d)
394 fb1a36c0 2022-01-09 op struct imsgev *iev = d;
395 fb1a36c0 2022-01-09 op struct imsgbuf *ibuf;
396 fb1a36c0 2022-01-09 op struct imsg imsg;
398 fb1a36c0 2022-01-09 op int shut = 0;
400 fb1a36c0 2022-01-09 op ibuf = &iev->ibuf;
402 fb1a36c0 2022-01-09 op if (event & EV_READ) {
403 fb1a36c0 2022-01-09 op if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
404 fb1a36c0 2022-01-09 op fatal("imsg_read error");
405 fb1a36c0 2022-01-09 op if (n == 0) /* Connection closed. */
408 fb1a36c0 2022-01-09 op if (event & EV_WRITE) {
409 fb1a36c0 2022-01-09 op if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
410 fb1a36c0 2022-01-09 op fatal("msgbuf_write");
411 fb1a36c0 2022-01-09 op if (n == 0) /* Connection closed. */
416 fb1a36c0 2022-01-09 op if ((n = imsg_get(ibuf, &imsg)) == -1)
417 fb1a36c0 2022-01-09 op fatal("imsg_get");
418 fb1a36c0 2022-01-09 op if (n == 0) /* No more messages. */
421 fb1a36c0 2022-01-09 op switch (imsg.hdr.type) {
422 fb1a36c0 2022-01-09 op case IMSG_AUTH_TLS:
423 fb1a36c0 2022-01-09 op do_auth_tls(&imsg);
425 5c69b561 2022-01-29 op case IMSG_CTL_DEBUG_BACK:
426 5c69b561 2022-01-29 op case IMSG_CTL_DEBUG_END:
427 5c69b561 2022-01-29 op control_imsg_relay(&imsg);
430 fb1a36c0 2022-01-09 op log_debug("%s: error handling imsg %d", __func__,
431 fb1a36c0 2022-01-09 op imsg.hdr.type);
434 fb1a36c0 2022-01-09 op imsg_free(&imsg);
437 fb1a36c0 2022-01-09 op imsg_event_add(iev);
439 fb1a36c0 2022-01-09 op /* This pipe is dead. Remove its event handler. */
440 fb1a36c0 2022-01-09 op event_del(&iev->ev);
441 fb1a36c0 2022-01-09 op event_loopexit(NULL);
446 fb1a36c0 2022-01-09 op main_reload(void)
448 fb1a36c0 2022-01-09 op struct kd_conf *xconf;
450 fb1a36c0 2022-01-09 op if ((xconf = parse_config(conffile)) == NULL)
453 fb1a36c0 2022-01-09 op if (main_imsg_send_config(xconf) == -1)
456 da824663 2022-01-28 op /* replace old configuration with the new one */
457 da824663 2022-01-28 op clear_config(main_conf);
458 da824663 2022-01-28 op main_conf = xconf;
463 fb1a36c0 2022-01-09 op static inline int
464 fb1a36c0 2022-01-09 op make_socket_for(struct kd_listen_conf *l)
466 fb1a36c0 2022-01-09 op struct sockaddr_in addr4;
470 fb1a36c0 2022-01-09 op memset(&addr4, 0, sizeof(addr4));
471 fb1a36c0 2022-01-09 op addr4.sin_family = AF_INET;
472 fb1a36c0 2022-01-09 op addr4.sin_port = htons(l->port);
473 fb1a36c0 2022-01-09 op addr4.sin_addr.s_addr = INADDR_ANY;
475 fb1a36c0 2022-01-09 op if ((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
476 fb1a36c0 2022-01-09 op fatal("socket");
479 fb1a36c0 2022-01-09 op if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &v, sizeof(v)) == -1)
480 fb1a36c0 2022-01-09 op fatal("setsockopt(SO_REUSEADDR)");
483 fb1a36c0 2022-01-09 op if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &v, sizeof(v)) == -1)
484 fb1a36c0 2022-01-09 op fatal("setsockopt(SO_REUSEPORT)");
486 fb1a36c0 2022-01-09 op len = sizeof(addr4);
487 fb1a36c0 2022-01-09 op if (bind(fd, (struct sockaddr *)&addr4, len) == -1)
488 fb1a36c0 2022-01-09 op fatal("bind(%s, %d)", l->iface, l->port);
490 fb1a36c0 2022-01-09 op if (listen(fd, 16) == -1)
491 fb1a36c0 2022-01-09 op fatal("l(%s, %d)", l->iface, l->port);
497 fb1a36c0 2022-01-09 op main_imsg_send_config(struct kd_conf *xconf)
499 fb1a36c0 2022-01-09 op struct kd_pki_conf *pki;
500 fb1a36c0 2022-01-09 op struct kd_listen_conf *listen;
502 fb1a36c0 2022-01-09 op #define SEND(type, fd, data, len) do { \
503 fb1a36c0 2022-01-09 op if (main_imsg_compose_listener(type, fd, 0, data, len) \
508 fb1a36c0 2022-01-09 op /* Send fixed part of config to children. */
509 fb1a36c0 2022-01-09 op SEND(IMSG_RECONF_CONF, -1, xconf, sizeof(*xconf));
511 fb1a36c0 2022-01-09 op STAILQ_FOREACH(pki, &xconf->pki_head, entry) {
512 fb1a36c0 2022-01-09 op log_debug("sending pki %s", pki->name);
513 fb1a36c0 2022-01-09 op SEND(IMSG_RECONF_PKI, -1, pki->name, sizeof(pki->name));
514 fb1a36c0 2022-01-09 op SEND(IMSG_RECONF_PKI_CERT, -1, pki->cert, pki->certlen);
515 fb1a36c0 2022-01-09 op SEND(IMSG_RECONF_PKI_KEY, -1, pki->key, pki->keylen);
518 fb1a36c0 2022-01-09 op STAILQ_FOREACH(listen, &xconf->listen_head, entry) {
519 fb1a36c0 2022-01-09 op log_debug("sending listen on port %d", listen->port);
520 fb1a36c0 2022-01-09 op SEND(IMSG_RECONF_LISTEN, make_socket_for(listen), listen,
521 fb1a36c0 2022-01-09 op sizeof(*listen));
524 fb1a36c0 2022-01-09 op SEND(IMSG_RECONF_END, -1, NULL, 0);
530 fb1a36c0 2022-01-09 op struct kd_conf *
531 fb1a36c0 2022-01-09 op config_new_empty(void)
533 fb1a36c0 2022-01-09 op struct kd_conf *xconf;
535 fb1a36c0 2022-01-09 op if ((xconf = calloc(1, sizeof(*xconf))) == NULL)
538 fb1a36c0 2022-01-09 op /* set default values */
539 f25db28f 2022-01-28 op STAILQ_INIT(&xconf->pki_head);
540 f25db28f 2022-01-28 op STAILQ_INIT(&xconf->table_head);
541 f25db28f 2022-01-28 op STAILQ_INIT(&xconf->listen_head);
543 fb1a36c0 2022-01-09 op return xconf;
547 fb1a36c0 2022-01-09 op main_shutdown(void)
552 fb1a36c0 2022-01-09 op /* close pipes. */
553 0f22cda8 2022-01-23 op msgbuf_clear(&iev_listener->ibuf.w);
554 0f22cda8 2022-01-23 op close(iev_listener->ibuf.fd);
555 0f22cda8 2022-01-23 op free(iev_listener);
557 fb1a36c0 2022-01-09 op log_debug("waiting for children to terminate");
559 fb1a36c0 2022-01-09 op pid = wait(&status);
560 fb1a36c0 2022-01-09 op if (pid == -1) {
561 fb1a36c0 2022-01-09 op if (errno != EINTR && errno != ECHILD)
562 fb1a36c0 2022-01-09 op fatal("wait");
563 fb1a36c0 2022-01-09 op } else if (WIFSIGNALED(status))
564 fb1a36c0 2022-01-09 op log_warnx("%s terminated; signal %d",
565 fb1a36c0 2022-01-09 op (pid == listener_pid) ? "logger" : "clientconn",
566 fb1a36c0 2022-01-09 op WTERMSIG(status));
567 fb1a36c0 2022-01-09 op } while (pid != -1 || (pid == -1 && errno == EINTR));
569 fb1a36c0 2022-01-09 op log_info("terminating");
574 fb1a36c0 2022-01-09 op start_child(enum kd_process p, int fd, int debug, int verbose)
576 fb1a36c0 2022-01-09 op const char *argv[5];
577 fb1a36c0 2022-01-09 op int argc = 0;
580 fb1a36c0 2022-01-09 op switch (pid = fork()) {
582 fb1a36c0 2022-01-09 op fatal("cannot fork");
590 fb1a36c0 2022-01-09 op if (fd != 3) {
591 fb1a36c0 2022-01-09 op if (dup2(fd, 3) == -1)
592 fb1a36c0 2022-01-09 op fatal("cannot setup imsg fd");
593 fb1a36c0 2022-01-09 op } else if (fcntl(F_SETFD, 0) == -1)
594 fb1a36c0 2022-01-09 op fatal("cannot setup imsg fd");
596 fb1a36c0 2022-01-09 op argv[argc++] = saved_argv0;
598 fb1a36c0 2022-01-09 op case PROC_MAIN:
599 fb1a36c0 2022-01-09 op fatalx("Can not start main process");
600 fb1a36c0 2022-01-09 op case PROC_LISTENER:
601 fb1a36c0 2022-01-09 op argv[argc++] = "-Tl";
603 fb1a36c0 2022-01-09 op case PROC_CLIENTCONN:
604 fb1a36c0 2022-01-09 op argv[argc++] = "-Tc";
608 fb1a36c0 2022-01-09 op argv[argc++] = "-d";
610 fb1a36c0 2022-01-09 op argv[argc++] = "-v";
611 fb1a36c0 2022-01-09 op argv[argc++] = NULL;
613 fb1a36c0 2022-01-09 op /* really? */
614 fb1a36c0 2022-01-09 op execvp(saved_argv0, (char *const *)argv);
615 fb1a36c0 2022-01-09 op fatal("execvp");
619 fb1a36c0 2022-01-09 op main_imsg_compose_listener(int type, int fd, uint32_t peerid,
620 fb1a36c0 2022-01-09 op const void *data, uint16_t datalen)
622 fb1a36c0 2022-01-09 op if (iev_listener)
623 fb1a36c0 2022-01-09 op return imsg_compose_event(iev_listener, type, peerid, 0,
624 fb1a36c0 2022-01-09 op fd, data, datalen);