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");
217 fb1a36c0 2022-01-09 op main_imsg_compose_listener(IMSG_CONTROLFD, control_fd, 0,
219 fb1a36c0 2022-01-09 op main_imsg_send_config(main_conf);
221 fb1a36c0 2022-01-09 op sandbox_main();
223 fb1a36c0 2022-01-09 op event_dispatch();
225 fb1a36c0 2022-01-09 op main_shutdown();
230 fb1a36c0 2022-01-09 op main_sig_handler(int sig, short event, void *arg)
233 fb1a36c0 2022-01-09 op * Normal signal handler rules don't apply because libevent
234 fb1a36c0 2022-01-09 op * decouples for us.
237 fb1a36c0 2022-01-09 op switch (sig) {
238 fb1a36c0 2022-01-09 op case SIGTERM:
240 fb1a36c0 2022-01-09 op main_shutdown();
243 fb1a36c0 2022-01-09 op if (main_reload() == -1)
244 fb1a36c0 2022-01-09 op log_warnx("configuration reload failed");
246 fb1a36c0 2022-01-09 op log_debug("configuration reloaded");
249 fb1a36c0 2022-01-09 op fatalx("unexpected signal %d", sig);
253 fb1a36c0 2022-01-09 op static inline struct table *
254 fb1a36c0 2022-01-09 op auth_table_by_id(uint32_t id)
256 fb1a36c0 2022-01-09 op struct kd_listen_conf *listen;
258 fb1a36c0 2022-01-09 op STAILQ_FOREACH(listen, &main_conf->listen_head, entry) {
259 fb1a36c0 2022-01-09 op if (listen->id == id)
260 fb1a36c0 2022-01-09 op return listen->auth_table;
266 fb1a36c0 2022-01-09 op static inline struct table *
267 fb1a36c0 2022-01-09 op virtual_table_by_id(uint32_t id)
269 fb1a36c0 2022-01-09 op struct kd_listen_conf *listen;
271 fb1a36c0 2022-01-09 op STAILQ_FOREACH(listen, &main_conf->listen_head, entry) {
272 fb1a36c0 2022-01-09 op if (listen->id == id)
273 fb1a36c0 2022-01-09 op return listen->virtual_table;
279 fb1a36c0 2022-01-09 op static inline struct table *
280 fb1a36c0 2022-01-09 op userdata_table_by_id(uint32_t id)
282 fb1a36c0 2022-01-09 op struct kd_listen_conf *listen;
284 fb1a36c0 2022-01-09 op STAILQ_FOREACH(listen, &main_conf->listen_head, entry) {
285 fb1a36c0 2022-01-09 op if (listen->id == id)
286 fb1a36c0 2022-01-09 op return listen->userdata_table;
292 fb1a36c0 2022-01-09 op static inline void
293 fb1a36c0 2022-01-09 op do_auth_tls(struct imsg *imsg)
295 fb1a36c0 2022-01-09 op char *username = NULL, *user = NULL, *home = NULL, *local_user;
296 fb1a36c0 2022-01-09 op struct passwd *pw;
297 fb1a36c0 2022-01-09 op struct table *auth, *virt, *userdata;
298 fb1a36c0 2022-01-09 op struct kd_auth_req kauth;
299 1e84c7b7 2022-01-10 op struct kd_auth_proc rauth;
300 fb1a36c0 2022-01-09 op int p[2], free_home = 1;
302 fb1a36c0 2022-01-09 op if (sizeof(kauth) != IMSG_DATA_SIZE(*imsg))
303 fb1a36c0 2022-01-09 op fatal("wrong size for IMSG_AUTH_TLS: "
304 fb1a36c0 2022-01-09 op "got %lu; want %lu", IMSG_DATA_SIZE(*imsg),
305 fb1a36c0 2022-01-09 op sizeof(kauth));
306 fb1a36c0 2022-01-09 op memcpy(&kauth, imsg->data, sizeof(kauth));
308 fb1a36c0 2022-01-09 op if (memmem(kauth.hash, sizeof(kauth.hash), "", 1) == NULL)
309 fb1a36c0 2022-01-09 op fatal("non NUL-terminated hash received");
311 fb1a36c0 2022-01-09 op log_debug("tls id=%u hash=%s", kauth.listen_id, kauth.hash);
313 fb1a36c0 2022-01-09 op if ((auth = auth_table_by_id(kauth.listen_id)) == NULL)
314 fb1a36c0 2022-01-09 op fatal("request for invalid listener id %d", imsg->hdr.pid);
316 fb1a36c0 2022-01-09 op virt = virtual_table_by_id(kauth.listen_id);
317 fb1a36c0 2022-01-09 op userdata = userdata_table_by_id(kauth.listen_id);
319 fb1a36c0 2022-01-09 op if (table_lookup(auth, kauth.hash, &username) == -1) {
320 fb1a36c0 2022-01-09 op log_warnx("login failed for hash %s", kauth.hash);
324 fb1a36c0 2022-01-09 op if (virt != NULL && table_lookup(virt, username, &user) == -1) {
325 fb1a36c0 2022-01-09 op log_warnx("virtual lookup failed for user %s", username);
329 fb1a36c0 2022-01-09 op /* the local user */
330 fb1a36c0 2022-01-09 op local_user = user != NULL ? user : username;
332 fb1a36c0 2022-01-09 op if (user != NULL)
333 fb1a36c0 2022-01-09 op log_debug("virtual user %s matched local user %s",
334 fb1a36c0 2022-01-09 op username, user);
336 fb1a36c0 2022-01-09 op log_debug("matched local user %s", username);
338 fb1a36c0 2022-01-09 op if (userdata != NULL && table_lookup(userdata, username, &home)
340 fb1a36c0 2022-01-09 op log_warnx("userdata lookup failed for user %s", username);
342 fb1a36c0 2022-01-09 op } else if (userdata == NULL) {
343 fb1a36c0 2022-01-09 op if ((pw = getpwnam(local_user)) == NULL) {
344 fb1a36c0 2022-01-09 op log_warnx("getpwnam(%s) failed", local_user);
348 fb1a36c0 2022-01-09 op free_home = 0;
349 fb1a36c0 2022-01-09 op home = pw->pw_dir;
352 fb1a36c0 2022-01-09 op if (user != NULL)
353 fb1a36c0 2022-01-09 op log_debug("matched home %s for virtual user %s",
354 fb1a36c0 2022-01-09 op home, username);
356 fb1a36c0 2022-01-09 op log_debug("matched home %s for local user %s",
357 fb1a36c0 2022-01-09 op home, username);
359 1e84c7b7 2022-01-10 op memset(&rauth, 0, sizeof(rauth));
360 1e84c7b7 2022-01-10 op strlcpy(rauth.uname, local_user, sizeof(rauth.uname));
361 1e84c7b7 2022-01-10 op if (strlcpy(rauth.dir, home, sizeof(rauth.dir)) >= sizeof(rauth.dir)) {
362 1e84c7b7 2022-01-10 op log_warnx("home for %s is bigger than PATH_MAX: %s",
363 1e84c7b7 2022-01-10 op username, home);
367 fb1a36c0 2022-01-09 op if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
368 fb1a36c0 2022-01-09 op PF_UNSPEC, p) == -1)
369 fb1a36c0 2022-01-09 op fatal("socketpair");
371 fb1a36c0 2022-01-09 op start_child(PROC_CLIENTCONN, p[1], debug, verbose);
373 fb1a36c0 2022-01-09 op main_imsg_compose_listener(IMSG_AUTH, p[0], imsg->hdr.peerid,
374 1e84c7b7 2022-01-10 op &rauth, sizeof(rauth));
376 fb1a36c0 2022-01-09 op free(username);
378 fb1a36c0 2022-01-09 op if (free_home)
383 fb1a36c0 2022-01-09 op free(username);
385 fb1a36c0 2022-01-09 op if (free_home)
387 fb1a36c0 2022-01-09 op main_imsg_compose_listener(IMSG_AUTH, -1, imsg->hdr.peerid,
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);
426 fb1a36c0 2022-01-09 op log_debug("%s: error handling imsg %d", __func__,
427 fb1a36c0 2022-01-09 op imsg.hdr.type);
430 fb1a36c0 2022-01-09 op imsg_free(&imsg);
433 fb1a36c0 2022-01-09 op imsg_event_add(iev);
435 fb1a36c0 2022-01-09 op /* This pipe is dead. Remove its event handler. */
436 fb1a36c0 2022-01-09 op event_del(&iev->ev);
437 fb1a36c0 2022-01-09 op event_loopexit(NULL);
442 fb1a36c0 2022-01-09 op main_reload(void)
444 fb1a36c0 2022-01-09 op struct kd_conf *xconf;
446 fb1a36c0 2022-01-09 op if ((xconf = parse_config(conffile)) == NULL)
449 fb1a36c0 2022-01-09 op if (main_imsg_send_config(xconf) == -1)
452 fb1a36c0 2022-01-09 op merge_config(main_conf, xconf);
457 fb1a36c0 2022-01-09 op static inline int
458 fb1a36c0 2022-01-09 op make_socket_for(struct kd_listen_conf *l)
460 fb1a36c0 2022-01-09 op struct sockaddr_in addr4;
464 fb1a36c0 2022-01-09 op memset(&addr4, 0, sizeof(addr4));
465 fb1a36c0 2022-01-09 op addr4.sin_family = AF_INET;
466 fb1a36c0 2022-01-09 op addr4.sin_port = htons(l->port);
467 fb1a36c0 2022-01-09 op addr4.sin_addr.s_addr = INADDR_ANY;
469 fb1a36c0 2022-01-09 op if ((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
470 fb1a36c0 2022-01-09 op fatal("socket");
473 fb1a36c0 2022-01-09 op if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &v, sizeof(v)) == -1)
474 fb1a36c0 2022-01-09 op fatal("setsockopt(SO_REUSEADDR)");
477 fb1a36c0 2022-01-09 op if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &v, sizeof(v)) == -1)
478 fb1a36c0 2022-01-09 op fatal("setsockopt(SO_REUSEPORT)");
480 fb1a36c0 2022-01-09 op len = sizeof(addr4);
481 fb1a36c0 2022-01-09 op if (bind(fd, (struct sockaddr *)&addr4, len) == -1)
482 fb1a36c0 2022-01-09 op fatal("bind(%s, %d)", l->iface, l->port);
484 fb1a36c0 2022-01-09 op if (listen(fd, 16) == -1)
485 fb1a36c0 2022-01-09 op fatal("l(%s, %d)", l->iface, l->port);
491 fb1a36c0 2022-01-09 op main_imsg_send_config(struct kd_conf *xconf)
493 fb1a36c0 2022-01-09 op struct kd_pki_conf *pki;
494 fb1a36c0 2022-01-09 op struct kd_listen_conf *listen;
496 fb1a36c0 2022-01-09 op #define SEND(type, fd, data, len) do { \
497 fb1a36c0 2022-01-09 op if (main_imsg_compose_listener(type, fd, 0, data, len) \
502 fb1a36c0 2022-01-09 op /* Send fixed part of config to children. */
503 fb1a36c0 2022-01-09 op SEND(IMSG_RECONF_CONF, -1, xconf, sizeof(*xconf));
505 fb1a36c0 2022-01-09 op STAILQ_FOREACH(pki, &xconf->pki_head, entry) {
506 fb1a36c0 2022-01-09 op log_debug("sending pki %s", pki->name);
507 fb1a36c0 2022-01-09 op SEND(IMSG_RECONF_PKI, -1, pki->name, sizeof(pki->name));
508 fb1a36c0 2022-01-09 op SEND(IMSG_RECONF_PKI_CERT, -1, pki->cert, pki->certlen);
509 fb1a36c0 2022-01-09 op SEND(IMSG_RECONF_PKI_KEY, -1, pki->key, pki->keylen);
512 fb1a36c0 2022-01-09 op STAILQ_FOREACH(listen, &xconf->listen_head, entry) {
513 fb1a36c0 2022-01-09 op log_debug("sending listen on port %d", listen->port);
514 fb1a36c0 2022-01-09 op SEND(IMSG_RECONF_LISTEN, make_socket_for(listen), listen,
515 fb1a36c0 2022-01-09 op sizeof(*listen));
518 fb1a36c0 2022-01-09 op SEND(IMSG_RECONF_END, -1, NULL, 0);
525 fb1a36c0 2022-01-09 op merge_config(struct kd_conf *conf, struct kd_conf *xconf)
527 fb1a36c0 2022-01-09 op /* do stuff... */
532 fb1a36c0 2022-01-09 op struct kd_conf *
533 fb1a36c0 2022-01-09 op config_new_empty(void)
535 fb1a36c0 2022-01-09 op struct kd_conf *xconf;
537 fb1a36c0 2022-01-09 op if ((xconf = calloc(1, sizeof(*xconf))) == NULL)
540 fb1a36c0 2022-01-09 op /* set default values */
542 fb1a36c0 2022-01-09 op return xconf;
546 fb1a36c0 2022-01-09 op config_clear(struct kd_conf *conf)
548 fb1a36c0 2022-01-09 op struct kd_conf *xconf;
550 fb1a36c0 2022-01-09 op /* Merge current config with an empty one. */
551 fb1a36c0 2022-01-09 op xconf = config_new_empty();
552 fb1a36c0 2022-01-09 op merge_config(conf, xconf);
558 fb1a36c0 2022-01-09 op main_shutdown(void)
563 fb1a36c0 2022-01-09 op /* close pipes. */
564 fb1a36c0 2022-01-09 op config_clear(main_conf);
566 fb1a36c0 2022-01-09 op log_debug("waiting for children to terminate");
568 fb1a36c0 2022-01-09 op pid = wait(&status);
569 fb1a36c0 2022-01-09 op if (pid == -1) {
570 fb1a36c0 2022-01-09 op if (errno != EINTR && errno != ECHILD)
571 fb1a36c0 2022-01-09 op fatal("wait");
572 fb1a36c0 2022-01-09 op } else if (WIFSIGNALED(status))
573 fb1a36c0 2022-01-09 op log_warnx("%s terminated; signal %d",
574 fb1a36c0 2022-01-09 op (pid == listener_pid) ? "logger" : "clientconn",
575 fb1a36c0 2022-01-09 op WTERMSIG(status));
576 fb1a36c0 2022-01-09 op } while (pid != -1 || (pid == -1 && errno == EINTR));
578 fb1a36c0 2022-01-09 op free(iev_listener);
580 fb1a36c0 2022-01-09 op log_info("terminating");
585 fb1a36c0 2022-01-09 op start_child(enum kd_process p, int fd, int debug, int verbose)
587 fb1a36c0 2022-01-09 op const char *argv[5];
588 fb1a36c0 2022-01-09 op int argc = 0;
591 fb1a36c0 2022-01-09 op switch (pid = fork()) {
593 fb1a36c0 2022-01-09 op fatal("cannot fork");
601 fb1a36c0 2022-01-09 op if (fd != 3) {
602 fb1a36c0 2022-01-09 op if (dup2(fd, 3) == -1)
603 fb1a36c0 2022-01-09 op fatal("cannot setup imsg fd");
604 fb1a36c0 2022-01-09 op } else if (fcntl(F_SETFD, 0) == -1)
605 fb1a36c0 2022-01-09 op fatal("cannot setup imsg fd");
607 fb1a36c0 2022-01-09 op argv[argc++] = saved_argv0;
609 fb1a36c0 2022-01-09 op case PROC_MAIN:
610 fb1a36c0 2022-01-09 op fatalx("Can not start main process");
611 fb1a36c0 2022-01-09 op case PROC_LISTENER:
612 fb1a36c0 2022-01-09 op argv[argc++] = "-Tl";
614 fb1a36c0 2022-01-09 op case PROC_CLIENTCONN:
615 fb1a36c0 2022-01-09 op argv[argc++] = "-Tc";
619 fb1a36c0 2022-01-09 op argv[argc++] = "-d";
621 fb1a36c0 2022-01-09 op argv[argc++] = "-v";
622 fb1a36c0 2022-01-09 op argv[argc++] = NULL;
624 fb1a36c0 2022-01-09 op /* really? */
625 fb1a36c0 2022-01-09 op execvp(saved_argv0, (char *const *)argv);
626 fb1a36c0 2022-01-09 op fatal("execvp");
630 fb1a36c0 2022-01-09 op main_imsg_compose_listener(int type, int fd, uint32_t peerid,
631 fb1a36c0 2022-01-09 op const void *data, uint16_t datalen)
633 fb1a36c0 2022-01-09 op if (iev_listener)
634 fb1a36c0 2022-01-09 op return imsg_compose_event(iev_listener, type, peerid, 0,
635 fb1a36c0 2022-01-09 op fd, data, datalen);