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>
52 const char *saved_argv0;
53 static int debug, nflag;
56 __dead void usage(void);
58 void main_sig_handler(int, short, void *);
59 void main_dispatch_listener(int, short, void *);
60 int main_reload(void);
61 int main_imsg_send_config(struct kd_conf *);
62 void main_dispatch_listener(int, short, void *);
63 __dead void main_shutdown(void);
65 static pid_t start_child(enum kd_process, int, int, int);
67 struct kd_conf *main_conf;
68 static struct imsgev *iev_listener;
76 fprintf(stderr, "usage: %s [-dnv] [-f file] [-s socket]\n",
82 main(int argc, char **argv)
84 struct event ev_sigint, ev_sigterm, ev_sighup;
86 int listener_flag = 0, client_flag = 0;
87 int pipe_main2listener[2];
91 conffile = KD_CONF_FILE;
94 log_init(1, LOG_DAEMON); /* Log to stderr until deamonized. */
97 saved_argv0 = argv[0];
98 if (saved_argv0 == NULL)
99 saved_argv0 = "kamid";
101 while ((ch = getopt(argc, argv, "D:df:nsT:v")) != -1) {
104 if (cmdline_symset(optarg) == -1)
105 log_warnx("could not parse macro definition %s",
129 fatalx("invalid process spec %c", *optarg);
142 if (argc > 0 || (listener_flag && client_flag))
146 client(debug, verbose);
147 else if (listener_flag)
148 listener(debug, verbose);
150 if ((main_conf = parse_config(conffile)) == NULL)
154 fprintf(stderr, "configuration OK\n");
158 /* Check for root privileges. */
160 fatalx("need root privileges");
162 /* Check for assigned daemon user. */
163 if (getpwnam(KD_USER) == NULL)
164 fatalx("unknown user %s", KD_USER);
166 log_init(debug, LOG_DAEMON);
167 log_setverbose(verbose);
174 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
175 PF_UNSPEC, pipe_main2listener) == -1)
176 fatal("main2listener socketpair");
178 /* Start children. */
179 listener_pid = start_child(PROC_LISTENER, pipe_main2listener[1],
182 log_procinit("main");
186 /* Setup signal handler */
187 signal_set(&ev_sigint, SIGINT, main_sig_handler, NULL);
188 signal_set(&ev_sigterm, SIGTERM, main_sig_handler, NULL);
189 signal_set(&ev_sighup, SIGHUP, main_sig_handler, NULL);
191 signal_add(&ev_sigint, NULL);
192 signal_add(&ev_sigterm, NULL);
193 signal_add(&ev_sighup, NULL);
195 signal(SIGCHLD, SIG_IGN);
196 signal(SIGPIPE, SIG_IGN);
198 if ((iev_listener = malloc(sizeof(*iev_listener))) == NULL)
200 imsg_init(&iev_listener->ibuf, pipe_main2listener[0]);
201 iev_listener->handler = main_dispatch_listener;
203 /* Setup event handlers for pipes to listener. */
204 iev_listener->events = EV_READ;
205 event_set(&iev_listener->ev, iev_listener->ibuf.fd,
206 iev_listener->events, iev_listener->handler, iev_listener);
207 event_add(&iev_listener->ev, NULL);
209 if ((control_fd = control_init(csock)) == -1)
210 fatalx("control socket setup failed");
212 main_imsg_compose_listener(IMSG_CONTROLFD, control_fd, 0,
214 main_imsg_send_config(main_conf);
225 main_sig_handler(int sig, short event, void *arg)
228 * Normal signal handler rules don't apply because libevent
238 if (main_reload() == -1)
239 log_warnx("configuration reload failed");
241 log_debug("configuration reloaded");
244 fatalx("unexpected signal %d", sig);
248 static inline struct table *
249 auth_table_by_id(uint32_t id)
251 struct kd_listen_conf *listen;
253 STAILQ_FOREACH(listen, &main_conf->listen_head, entry) {
254 if (listen->id == id)
255 return listen->auth_table;
261 static inline struct table *
262 virtual_table_by_id(uint32_t id)
264 struct kd_listen_conf *listen;
266 STAILQ_FOREACH(listen, &main_conf->listen_head, entry) {
267 if (listen->id == id)
268 return listen->virtual_table;
274 static inline struct table *
275 userdata_table_by_id(uint32_t id)
277 struct kd_listen_conf *listen;
279 STAILQ_FOREACH(listen, &main_conf->listen_head, entry) {
280 if (listen->id == id)
281 return listen->userdata_table;
288 do_auth_tls(struct imsg *imsg)
290 char *username = NULL, *user = NULL, *home = NULL, *local_user;
292 struct table *auth, *virt, *userdata;
293 struct kd_auth_req kauth;
294 int p[2], free_home = 1;
296 if (sizeof(kauth) != IMSG_DATA_SIZE(*imsg))
297 fatal("wrong size for IMSG_AUTH_TLS: "
298 "got %lu; want %lu", IMSG_DATA_SIZE(*imsg),
300 memcpy(&kauth, imsg->data, sizeof(kauth));
302 if (memmem(kauth.hash, sizeof(kauth.hash), "", 1) == NULL)
303 fatal("non NUL-terminated hash received");
305 log_debug("tls id=%u hash=%s", kauth.listen_id, kauth.hash);
307 if ((auth = auth_table_by_id(kauth.listen_id)) == NULL)
308 fatal("request for invalid listener id %d", imsg->hdr.pid);
310 virt = virtual_table_by_id(kauth.listen_id);
311 userdata = userdata_table_by_id(kauth.listen_id);
313 if (table_lookup(auth, kauth.hash, &username) == -1) {
314 log_warnx("login failed for hash %s", kauth.hash);
318 if (virt != NULL && table_lookup(virt, username, &user) == -1) {
319 log_warnx("virtual lookup failed for user %s", username);
324 local_user = user != NULL ? user : username;
327 log_debug("virtual user %s matched local user %s",
330 log_debug("matched local user %s", username);
332 if (userdata != NULL && table_lookup(userdata, username, &home)
334 log_warnx("userdata lookup failed for user %s", username);
336 } else if (userdata == NULL) {
337 if ((pw = getpwnam(local_user)) == NULL) {
338 log_warnx("getpwnam(%s) failed", local_user);
347 log_debug("matched home %s for virtual user %s",
350 log_debug("matched home %s for local user %s",
353 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
357 start_child(PROC_CLIENTCONN, p[1], debug, verbose);
359 main_imsg_compose_listener(IMSG_AUTH, p[0], imsg->hdr.peerid,
360 local_user, strlen(local_user)+1);
361 main_imsg_compose_listener(IMSG_AUTH_DIR, -1, imsg->hdr.peerid,
362 home, strlen(home)+1);
375 main_imsg_compose_listener(IMSG_AUTH, -1, imsg->hdr.peerid,
380 main_dispatch_listener(int fd, short event, void *d)
382 struct imsgev *iev = d;
383 struct imsgbuf *ibuf;
390 if (event & EV_READ) {
391 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
392 fatal("imsg_read error");
393 if (n == 0) /* Connection closed. */
396 if (event & EV_WRITE) {
397 if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
398 fatal("msgbuf_write");
399 if (n == 0) /* Connection closed. */
404 if ((n = imsg_get(ibuf, &imsg)) == -1)
406 if (n == 0) /* No more messages. */
409 switch (imsg.hdr.type) {
414 log_debug("%s: error handling imsg %d", __func__,
423 /* This pipe is dead. Remove its event handler. */
425 event_loopexit(NULL);
432 struct kd_conf *xconf;
434 if ((xconf = parse_config(conffile)) == NULL)
437 if (main_imsg_send_config(xconf) == -1)
440 merge_config(main_conf, xconf);
446 make_socket_for(struct kd_listen_conf *l)
448 struct sockaddr_in addr4;
452 memset(&addr4, 0, sizeof(addr4));
453 addr4.sin_family = AF_INET;
454 addr4.sin_port = htons(l->port);
455 addr4.sin_addr.s_addr = INADDR_ANY;
457 if ((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
461 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &v, sizeof(v)) == -1)
462 fatal("setsockopt(SO_REUSEADDR)");
465 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &v, sizeof(v)) == -1)
466 fatal("setsockopt(SO_REUSEPORT)");
469 if (bind(fd, (struct sockaddr *)&addr4, len) == -1)
470 fatal("bind(%s, %d)", l->iface, l->port);
472 if (listen(fd, 16) == -1)
473 fatal("l(%s, %d)", l->iface, l->port);
479 main_imsg_send_config(struct kd_conf *xconf)
481 struct kd_pki_conf *pki;
482 struct kd_listen_conf *listen;
484 #define SEND(type, fd, data, len) do { \
485 if (main_imsg_compose_listener(type, fd, 0, data, len) \
490 /* Send fixed part of config to children. */
491 SEND(IMSG_RECONF_CONF, -1, xconf, sizeof(*xconf));
493 STAILQ_FOREACH(pki, &xconf->pki_head, entry) {
494 log_debug("sending pki %s", pki->name);
495 SEND(IMSG_RECONF_PKI, -1, pki->name, sizeof(pki->name));
496 SEND(IMSG_RECONF_PKI_CERT, -1, pki->cert, pki->certlen);
497 SEND(IMSG_RECONF_PKI_KEY, -1, pki->key, pki->keylen);
500 STAILQ_FOREACH(listen, &xconf->listen_head, entry) {
501 log_debug("sending listen on port %d", listen->port);
502 SEND(IMSG_RECONF_LISTEN, make_socket_for(listen), listen,
506 SEND(IMSG_RECONF_END, -1, NULL, 0);
513 merge_config(struct kd_conf *conf, struct kd_conf *xconf)
521 config_new_empty(void)
523 struct kd_conf *xconf;
525 if ((xconf = calloc(1, sizeof(*xconf))) == NULL)
528 /* set default values */
534 config_clear(struct kd_conf *conf)
536 struct kd_conf *xconf;
538 /* Merge current config with an empty one. */
539 xconf = config_new_empty();
540 merge_config(conf, xconf);
552 config_clear(main_conf);
554 log_debug("waiting for children to terminate");
558 if (errno != EINTR && errno != ECHILD)
560 } else if (WIFSIGNALED(status))
561 log_warnx("%s terminated; signal %d",
562 (pid == listener_pid) ? "logger" : "clientconn",
564 } while (pid != -1 || (pid == -1 && errno == EINTR));
568 log_info("terminating");
573 start_child(enum kd_process p, int fd, int debug, int verbose)
579 switch (pid = fork()) {
581 fatal("cannot fork");
590 if (dup2(fd, 3) == -1)
591 fatal("cannot setup imsg fd");
592 } else if (fcntl(F_SETFD, 0) == -1)
593 fatal("cannot setup imsg fd");
595 argv[argc++] = saved_argv0;
598 fatalx("Can not start main process");
600 argv[argc++] = "-Tl";
602 case PROC_CLIENTCONN:
603 argv[argc++] = "-Tc";
613 execvp(saved_argv0, (char *const *)argv);
618 main_imsg_compose_listener(int type, int fd, uint32_t peerid,
619 const void *data, uint16_t datalen)
622 return imsg_compose_event(iev_listener, type, peerid, 0,