2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 * Copyright (c) 2018 Florian Obser <florian@openbsd.org>
4 * Copyright (c) 2005 Claudio Jeker <claudio@openbsd.org>
5 * Copyright (c) 2004 Esben Norby <norby@openbsd.org>
6 * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 #include <sys/socket.h>
24 #include <arpa/inet.h>
25 #include <netinet/in.h>
51 const char *saved_argv0;
52 static int debug, nflag;
55 __dead void usage(void);
57 void main_sig_handler(int, short, void *);
58 void main_dispatch_listener(int, short, void *);
59 int main_reload(void);
60 int main_imsg_send_config(struct kd_conf *);
61 void main_dispatch_listener(int, short, void *);
62 __dead void main_shutdown(void);
64 static pid_t start_child(enum kd_process, int, int, int);
66 struct kd_conf *main_conf;
67 static struct imsgev *iev_listener;
75 fprintf(stderr, "usage: %s [-dnv] [-f file] [-s socket]\n",
81 main(int argc, char **argv)
83 struct event ev_sigint, ev_sigterm, ev_sighup;
85 int listener_flag = 0, client_flag = 0;
86 int pipe_main2listener[2];
90 conffile = KD_CONF_FILE;
93 log_init(1, LOG_DAEMON); /* Log to stderr until deamonized. */
96 saved_argv0 = argv[0];
97 if (saved_argv0 == NULL)
98 saved_argv0 = "kamid";
100 while ((ch = getopt(argc, argv, "D:df:nsT:v")) != -1) {
103 if (cmdline_symset(optarg) == -1)
104 log_warnx("could not parse macro definition %s",
128 fatalx("invalid process spec %c", *optarg);
141 if (argc > 0 || (listener_flag && client_flag))
145 client(debug, verbose);
146 else if (listener_flag)
147 listener(debug, verbose);
149 if ((main_conf = parse_config(conffile)) == NULL)
153 fprintf(stderr, "configuratino OK\n");
157 /* Check for root privileges. */
159 fatalx("need root privileges");
161 /* Check for assigned daemon user. */
162 if (getpwnam(KD_USER) == NULL)
163 fatalx("unknown user %s", KD_USER);
165 log_init(debug, LOG_DAEMON);
166 log_setverbose(verbose);
173 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
174 PF_UNSPEC, pipe_main2listener) == -1)
175 fatal("main2listener socketpair");
177 /* Start children. */
178 listener_pid = start_child(PROC_LISTENER, pipe_main2listener[1],
181 log_procinit("main");
185 /* Setup signal handler */
186 signal_set(&ev_sigint, SIGINT, main_sig_handler, NULL);
187 signal_set(&ev_sigterm, SIGTERM, main_sig_handler, NULL);
188 signal_set(&ev_sighup, SIGHUP, main_sig_handler, NULL);
190 signal_add(&ev_sigint, NULL);
191 signal_add(&ev_sigterm, NULL);
192 signal_add(&ev_sighup, NULL);
194 signal(SIGCHLD, SIG_IGN);
195 signal(SIGPIPE, SIG_IGN);
197 if ((iev_listener = malloc(sizeof(*iev_listener))) == NULL)
199 imsg_init(&iev_listener->ibuf, pipe_main2listener[0]);
200 iev_listener->handler = main_dispatch_listener;
202 /* Setup event handlers for pipes to listener. */
203 iev_listener->events = EV_READ;
204 event_set(&iev_listener->ev, iev_listener->ibuf.fd,
205 iev_listener->events, iev_listener->handler, iev_listener);
206 event_add(&iev_listener->ev, NULL);
208 if ((control_fd = control_init(csock)) == -1)
209 fatalx("control socket setup failed");
211 main_imsg_compose_listener(IMSG_CONTROLFD, control_fd, 0,
213 main_imsg_send_config(main_conf);
224 main_sig_handler(int sig, short event, void *arg)
227 * Normal signal handler rules don't apply because libevent
237 if (main_reload() == -1)
238 log_warnx("configuration reload failed");
240 log_debug("configuration reloaded");
243 fatalx("unexpected signal %d", sig);
248 do_auth_tls(struct imsg *imsg)
250 const char *hash, *username = "op";
255 if (hash[IMSG_DATA_SIZE(*imsg)-1] != '\0')
258 log_debug("tls hash=%s", hash);
259 log_debug("assuming it refers to user `%s'",
262 if ((pw = getpwnam(username)) == NULL) {
263 log_warn("getpwnam(%s)", username);
267 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
271 start_child(PROC_CLIENTCONN, p[1], debug, verbose);
273 main_imsg_compose_listener(IMSG_AUTH, p[0], imsg->hdr.peerid,
274 username, strlen(username)+1);
275 main_imsg_compose_listener(IMSG_AUTH_DIR, -1, imsg->hdr.peerid,
276 pw->pw_dir, strlen(pw->pw_dir)+1);
281 main_imsg_compose_listener(IMSG_AUTH, -1, imsg->hdr.peerid,
286 main_dispatch_listener(int fd, short event, void *d)
288 struct imsgev *iev = d;
289 struct imsgbuf *ibuf;
296 if (event & EV_READ) {
297 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
298 fatal("imsg_read error");
299 if (n == 0) /* Connection closed. */
302 if (event & EV_WRITE) {
303 if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
304 fatal("msgbuf_write");
305 if (n == 0) /* Connection closed. */
310 if ((n = imsg_get(ibuf, &imsg)) == -1)
312 if (n == 0) /* No more messages. */
315 switch (imsg.hdr.type) {
320 log_debug("%s: error handling imsg %d", __func__,
329 /* This pipe is dead. Remove its event handler. */
331 event_loopexit(NULL);
338 struct kd_conf *xconf;
340 if ((xconf = parse_config(conffile)) == NULL)
343 if (main_imsg_send_config(xconf) == -1)
346 merge_config(main_conf, xconf);
352 make_socket_for(struct kd_listen_conf *l)
354 struct sockaddr_in addr4;
358 memset(&addr4, 0, sizeof(addr4));
359 addr4.sin_family = AF_INET;
360 addr4.sin_port = htons(l->port);
361 addr4.sin_addr.s_addr = INADDR_ANY;
363 if ((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
367 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &v, sizeof(v)) == -1)
368 fatal("setsockopt(SO_REUSEADDR)");
371 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &v, sizeof(v)) == -1)
372 fatal("setsockopt(SO_REUSEPORT)");
375 if (bind(fd, (struct sockaddr *)&addr4, len) == -1)
376 fatal("bind(%s, %d)", l->iface, l->port);
378 if (listen(fd, 16) == -1)
379 fatal("l(%s, %d)", l->iface, l->port);
385 main_imsg_send_config(struct kd_conf *xconf)
387 struct kd_pki_conf *pki;
388 struct kd_listen_conf *listen;
390 #define SEND(type, fd, data, len) do { \
391 if (main_imsg_compose_listener(type, fd, 0, data, len) \
396 /* Send fixed part of config to children. */
397 SEND(IMSG_RECONF_CONF, -1, xconf, sizeof(*xconf));
399 SIMPLEQ_FOREACH(pki, &xconf->pki_head, entry) {
400 log_debug("sending pki %s", pki->name);
401 SEND(IMSG_RECONF_PKI, -1, pki->name, sizeof(pki->name));
402 SEND(IMSG_RECONF_PKI_CERT, -1, pki->cert, pki->certlen);
403 SEND(IMSG_RECONF_PKI_KEY, -1, pki->key, pki->keylen);
406 SIMPLEQ_FOREACH(listen, &xconf->listen_head, entry) {
407 log_debug("sending listen on port %d", listen->port);
408 SEND(IMSG_RECONF_LISTEN, make_socket_for(listen), listen,
412 SEND(IMSG_RECONF_END, -1, NULL, 0);
419 merge_config(struct kd_conf *conf, struct kd_conf *xconf)
427 config_new_empty(void)
429 struct kd_conf *xconf;
431 if ((xconf = calloc(1, sizeof(*xconf))) == NULL)
434 /* set default values */
440 config_clear(struct kd_conf *conf)
442 struct kd_conf *xconf;
444 /* Merge current config with an empty one. */
445 xconf = config_new_empty();
446 merge_config(conf, xconf);
458 config_clear(main_conf);
460 log_debug("waiting for children to terminate");
464 if (errno != EINTR && errno != ECHILD)
466 } else if (WIFSIGNALED(status))
467 log_warnx("%s terminated; signal %d",
468 (pid == listener_pid) ? "logger" : "clientconn",
470 } while (pid != -1 || (pid == -1 && errno == EINTR));
474 log_info("terminating");
479 start_child(enum kd_process p, int fd, int debug, int verbose)
485 switch (pid = fork()) {
487 fatal("cannot fork");
496 if (dup2(fd, 3) == -1)
497 fatal("cannot setup imsg fd");
498 } else if (fcntl(F_SETFD, 0) == -1)
499 fatal("cannot setup imsg fd");
501 argv[argc++] = saved_argv0;
504 fatalx("Can not start main process");
506 argv[argc++] = "-Tl";
508 case PROC_CLIENTCONN:
509 argv[argc++] = "-Tc";
519 execvp(saved_argv0, (char *const *)argv);
524 imsg_event_add(struct imsgev *iev)
526 iev->events = EV_READ;
527 if (iev->ibuf.w.queued)
528 iev->events |= EV_WRITE;
531 event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev);
532 event_add(&iev->ev, NULL);
536 imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
537 pid_t pid, int fd, const void *data, uint16_t datalen)
541 if ((ret = imsg_compose(&iev->ibuf, type, peerid, pid, fd, data,
549 main_imsg_compose_listener(int type, int fd, uint32_t peerid,
550 const void *data, uint16_t datalen)
553 return imsg_compose_event(iev_listener, type, peerid, 0,