Blob


1 /*
2 * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/queue.h>
18 #include <sys/tree.h>
19 #include <sys/time.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <sys/socket.h>
23 #include <sys/un.h>
24 #include <sys/wait.h>
25 #include <sys/resource.h>
27 #include <fcntl.h>
28 #include <err.h>
29 #include <errno.h>
30 #include <event.h>
31 #include <limits.h>
32 #include <pwd.h>
33 #include <imsg.h>
34 #include <sha1.h>
35 #include <sha2.h>
36 #include <signal.h>
37 #include <siphash.h>
38 #include <stdarg.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <syslog.h>
43 #include <unistd.h>
45 #include "got_error.h"
46 #include "got_opentemp.h"
47 #include "got_path.h"
48 #include "got_repository.h"
49 #include "got_object.h"
50 #include "got_reference.h"
51 #include "got_diff.h"
53 #include "got_lib_delta.h"
54 #include "got_lib_object.h"
55 #include "got_lib_object_cache.h"
56 #include "got_lib_hash.h"
57 #include "got_lib_gitproto.h"
58 #include "got_lib_pack.h"
59 #include "got_lib_repository.h"
61 #include "gotd.h"
62 #include "log.h"
63 #include "listen.h"
64 #include "auth.h"
65 #include "session.h"
66 #include "repo_read.h"
67 #include "repo_write.h"
68 #include "notify.h"
70 #ifndef nitems
71 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
72 #endif
74 enum gotd_client_state {
75 GOTD_CLIENT_STATE_NEW,
76 GOTD_CLIENT_STATE_ACCESS_GRANTED,
77 };
79 struct gotd_child_proc {
80 pid_t pid;
81 enum gotd_procid type;
82 char repo_name[NAME_MAX];
83 char repo_path[PATH_MAX];
84 int pipe[2];
85 struct gotd_imsgev iev;
86 struct event tmo;
88 TAILQ_ENTRY(gotd_child_proc) entry;
89 };
90 TAILQ_HEAD(gotd_procs, gotd_child_proc) procs;
92 struct gotd_client {
93 STAILQ_ENTRY(gotd_client) entry;
94 enum gotd_client_state state;
95 uint32_t id;
96 int fd;
97 struct gotd_imsgev iev;
98 struct event tmo;
99 uid_t euid;
100 gid_t egid;
101 char *username;
102 struct gotd_child_proc *repo;
103 struct gotd_child_proc *auth;
104 struct gotd_child_proc *session;
105 int required_auth;
106 };
107 STAILQ_HEAD(gotd_clients, gotd_client);
109 static struct gotd_clients gotd_clients[GOTD_CLIENT_TABLE_SIZE];
110 static SIPHASH_KEY clients_hash_key;
111 volatile int client_cnt;
112 static struct timeval auth_timeout = { 5, 0 };
113 static struct gotd gotd;
115 void gotd_sighdlr(int sig, short event, void *arg);
116 static void gotd_shutdown(void);
117 static const struct got_error *start_session_child(struct gotd_client *,
118 struct gotd_repo *, char *, const char *, int, int);
119 static const struct got_error *start_repo_child(struct gotd_client *,
120 enum gotd_procid, struct gotd_repo *, char *, const char *, int, int);
121 static const struct got_error *start_auth_child(struct gotd_client *, int,
122 struct gotd_repo *, char *, const char *, int, int);
123 static void kill_proc(struct gotd_child_proc *, int);
124 static void disconnect(struct gotd_client *);
126 __dead static void
127 usage(void)
129 fprintf(stderr, "usage: %s [-dnv] [-f config-file]\n", getprogname());
130 exit(1);
133 static int
134 unix_socket_listen(const char *unix_socket_path, uid_t uid, gid_t gid)
136 struct sockaddr_un sun;
137 int fd = -1;
138 mode_t old_umask, mode;
140 fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK| SOCK_CLOEXEC, 0);
141 if (fd == -1) {
142 log_warn("socket");
143 return -1;
146 sun.sun_family = AF_UNIX;
147 if (strlcpy(sun.sun_path, unix_socket_path,
148 sizeof(sun.sun_path)) >= sizeof(sun.sun_path)) {
149 log_warnx("%s: name too long", unix_socket_path);
150 close(fd);
151 return -1;
154 if (unlink(unix_socket_path) == -1) {
155 if (errno != ENOENT) {
156 log_warn("unlink %s", unix_socket_path);
157 close(fd);
158 return -1;
162 old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
163 mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
165 if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
166 log_warn("bind: %s", unix_socket_path);
167 close(fd);
168 umask(old_umask);
169 return -1;
172 umask(old_umask);
174 if (chmod(unix_socket_path, mode) == -1) {
175 log_warn("chmod %o %s", mode, unix_socket_path);
176 close(fd);
177 unlink(unix_socket_path);
178 return -1;
181 if (chown(unix_socket_path, uid, gid) == -1) {
182 log_warn("chown %s uid=%d gid=%d", unix_socket_path, uid, gid);
183 close(fd);
184 unlink(unix_socket_path);
185 return -1;
188 if (listen(fd, GOTD_UNIX_SOCKET_BACKLOG) == -1) {
189 log_warn("listen");
190 close(fd);
191 unlink(unix_socket_path);
192 return -1;
195 return fd;
198 static uint64_t
199 client_hash(uint32_t client_id)
201 return SipHash24(&clients_hash_key, &client_id, sizeof(client_id));
204 static void
205 add_client(struct gotd_client *client)
207 uint64_t slot = client_hash(client->id) % nitems(gotd_clients);
208 STAILQ_INSERT_HEAD(&gotd_clients[slot], client, entry);
209 client_cnt++;
212 static struct gotd_client *
213 find_client(uint32_t client_id)
215 uint64_t slot;
216 struct gotd_client *c;
218 slot = client_hash(client_id) % nitems(gotd_clients);
219 STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
220 if (c->id == client_id)
221 return c;
224 return NULL;
227 static struct gotd_client *
228 find_client_by_proc_fd(int fd)
230 uint64_t slot;
232 for (slot = 0; slot < nitems(gotd_clients); slot++) {
233 struct gotd_client *c;
235 STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
236 if (c->repo && c->repo->iev.ibuf.fd == fd)
237 return c;
238 if (c->auth && c->auth->iev.ibuf.fd == fd)
239 return c;
240 if (c->session && c->session->iev.ibuf.fd == fd)
241 return c;
245 return NULL;
248 static int
249 client_is_reading(struct gotd_client *client)
251 return (client->required_auth &
252 (GOTD_AUTH_READ | GOTD_AUTH_WRITE)) == GOTD_AUTH_READ;
255 static int
256 client_is_writing(struct gotd_client *client)
258 return (client->required_auth &
259 (GOTD_AUTH_READ | GOTD_AUTH_WRITE)) ==
260 (GOTD_AUTH_READ | GOTD_AUTH_WRITE);
263 static const struct got_error *
264 ensure_client_is_not_writing(struct gotd_client *client)
266 if (client_is_writing(client)) {
267 return got_error_fmt(GOT_ERR_BAD_PACKET,
268 "uid %d made a read-request but is writing to "
269 "a repository", client->euid);
272 return NULL;
275 static const struct got_error *
276 ensure_client_is_not_reading(struct gotd_client *client)
278 if (client_is_reading(client)) {
279 return got_error_fmt(GOT_ERR_BAD_PACKET,
280 "uid %d made a write-request but is reading from "
281 "a repository", client->euid);
284 return NULL;
287 static void
288 proc_done(struct gotd_child_proc *proc)
290 struct gotd_client *client;
292 TAILQ_REMOVE(&procs, proc, entry);
294 client = find_client_by_proc_fd(proc->iev.ibuf.fd);
295 if (client != NULL) {
296 if (proc == client->repo)
297 client->repo = NULL;
298 if (proc == client->auth)
299 client->auth = NULL;
300 if (proc == client->session)
301 client->session = NULL;
302 disconnect(client);
305 if (proc == gotd.notify_proc)
306 gotd.notify_proc = NULL;
308 evtimer_del(&proc->tmo);
310 if (proc->iev.ibuf.fd != -1) {
311 event_del(&proc->iev.ev);
312 msgbuf_clear(&proc->iev.ibuf.w);
313 close(proc->iev.ibuf.fd);
316 free(proc);
319 static void
320 kill_repo_proc(struct gotd_client *client)
322 if (client->repo == NULL)
323 return;
325 kill_proc(client->repo, 0);
326 client->repo = NULL;
329 static void
330 kill_auth_proc(struct gotd_client *client)
332 if (client->auth == NULL)
333 return;
335 kill_proc(client->auth, 0);
336 client->auth = NULL;
339 static void
340 kill_session_proc(struct gotd_client *client)
342 if (client->session == NULL)
343 return;
345 kill_proc(client->session, 0);
346 client->session = NULL;
349 static void
350 disconnect(struct gotd_client *client)
352 struct gotd_imsg_disconnect idisconnect;
353 struct gotd_child_proc *listen_proc = gotd.listen_proc;
354 uint64_t slot;
356 log_debug("uid %d: disconnecting", client->euid);
358 kill_auth_proc(client);
359 kill_session_proc(client);
360 kill_repo_proc(client);
362 idisconnect.client_id = client->id;
363 if (gotd_imsg_compose_event(&listen_proc->iev,
364 GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
365 &idisconnect, sizeof(idisconnect)) == -1)
366 log_warn("imsg compose DISCONNECT");
368 slot = client_hash(client->id) % nitems(gotd_clients);
369 STAILQ_REMOVE(&gotd_clients[slot], client, gotd_client, entry);
370 imsg_clear(&client->iev.ibuf);
371 event_del(&client->iev.ev);
372 evtimer_del(&client->tmo);
373 if (client->fd != -1)
374 close(client->fd);
375 else if (client->iev.ibuf.fd != -1)
376 close(client->iev.ibuf.fd);
377 free(client->username);
378 free(client);
379 client_cnt--;
382 static void
383 disconnect_on_error(struct gotd_client *client, const struct got_error *err)
385 struct imsgbuf ibuf;
387 if (err->code != GOT_ERR_EOF) {
388 log_warnx("uid %d: %s", client->euid, err->msg);
389 if (client->fd != -1) {
390 imsg_init(&ibuf, client->fd);
391 gotd_imsg_send_error(&ibuf, 0, PROC_GOTD, err);
392 imsg_clear(&ibuf);
395 disconnect(client);
398 static const struct got_error *
399 send_repo_info(struct gotd_imsgev *iev, struct gotd_repo *repo)
401 const struct got_error *err = NULL;
402 struct gotd_imsg_info_repo irepo;
404 memset(&irepo, 0, sizeof(irepo));
406 if (strlcpy(irepo.repo_name, repo->name, sizeof(irepo.repo_name))
407 >= sizeof(irepo.repo_name))
408 return got_error_msg(GOT_ERR_NO_SPACE, "repo name too long");
409 if (strlcpy(irepo.repo_path, repo->path, sizeof(irepo.repo_path))
410 >= sizeof(irepo.repo_path))
411 return got_error_msg(GOT_ERR_NO_SPACE, "repo path too long");
413 if (gotd_imsg_compose_event(iev, GOTD_IMSG_INFO_REPO, PROC_GOTD, -1,
414 &irepo, sizeof(irepo)) == -1) {
415 err = got_error_from_errno("imsg compose INFO_REPO");
416 if (err)
417 return err;
420 return NULL;
423 static const struct got_error *
424 send_client_info(struct gotd_imsgev *iev, struct gotd_client *client)
426 const struct got_error *err = NULL;
427 struct gotd_imsg_info_client iclient;
428 struct gotd_child_proc *proc;
430 memset(&iclient, 0, sizeof(iclient));
431 iclient.euid = client->euid;
432 iclient.egid = client->egid;
434 proc = client->repo;
435 if (proc) {
436 if (strlcpy(iclient.repo_name, proc->repo_path,
437 sizeof(iclient.repo_name)) >= sizeof(iclient.repo_name)) {
438 return got_error_msg(GOT_ERR_NO_SPACE,
439 "repo name too long");
441 if (client_is_writing(client))
442 iclient.is_writing = 1;
444 iclient.repo_child_pid = proc->pid;
447 if (client->session)
448 iclient.session_child_pid = client->session->pid;
450 if (gotd_imsg_compose_event(iev, GOTD_IMSG_INFO_CLIENT, PROC_GOTD, -1,
451 &iclient, sizeof(iclient)) == -1) {
452 err = got_error_from_errno("imsg compose INFO_CLIENT");
453 if (err)
454 return err;
457 return NULL;
460 static const struct got_error *
461 send_info(struct gotd_client *client)
463 const struct got_error *err = NULL;
464 struct gotd_imsg_info info;
465 uint64_t slot;
466 struct gotd_repo *repo;
468 if (client->euid != 0)
469 return got_error_set_errno(EPERM, "info");
471 info.pid = gotd.pid;
472 info.verbosity = gotd.verbosity;
473 info.nrepos = gotd.nrepos;
474 info.nclients = client_cnt - 1;
476 if (gotd_imsg_compose_event(&client->iev, GOTD_IMSG_INFO, PROC_GOTD, -1,
477 &info, sizeof(info)) == -1) {
478 err = got_error_from_errno("imsg compose INFO");
479 if (err)
480 return err;
483 TAILQ_FOREACH(repo, &gotd.repos, entry) {
484 err = send_repo_info(&client->iev, repo);
485 if (err)
486 return err;
489 for (slot = 0; slot < nitems(gotd_clients); slot++) {
490 struct gotd_client *c;
491 STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
492 if (c->id == client->id)
493 continue;
494 err = send_client_info(&client->iev, c);
495 if (err)
496 return err;
500 return NULL;
503 static const struct got_error *
504 stop_gotd(struct gotd_client *client)
507 if (client->euid != 0)
508 return got_error_set_errno(EPERM, "stop");
510 gotd_shutdown();
511 /* NOTREACHED */
512 return NULL;
515 static const struct got_error *
516 start_client_authentication(struct gotd_client *client, struct imsg *imsg)
518 const struct got_error *err;
519 struct gotd_imsg_list_refs ireq;
520 struct gotd_repo *repo = NULL;
521 size_t datalen;
523 log_debug("list-refs request from uid %d", client->euid);
525 if (client->state != GOTD_CLIENT_STATE_NEW)
526 return got_error_msg(GOT_ERR_BAD_REQUEST,
527 "unexpected list-refs request received");
529 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
530 if (datalen != sizeof(ireq))
531 return got_error(GOT_ERR_PRIVSEP_LEN);
533 memcpy(&ireq, imsg->data, datalen);
535 if (ireq.client_is_reading) {
536 err = ensure_client_is_not_writing(client);
537 if (err)
538 return err;
539 repo = gotd_find_repo_by_name(ireq.repo_name, &gotd.repos);
540 if (repo == NULL)
541 return got_error(GOT_ERR_NOT_GIT_REPO);
542 err = start_auth_child(client, GOTD_AUTH_READ, repo,
543 gotd.argv0, gotd.confpath, gotd.daemonize,
544 gotd.verbosity);
545 if (err)
546 return err;
547 } else {
548 err = ensure_client_is_not_reading(client);
549 if (err)
550 return err;
551 repo = gotd_find_repo_by_name(ireq.repo_name, &gotd.repos);
552 if (repo == NULL)
553 return got_error(GOT_ERR_NOT_GIT_REPO);
554 err = start_auth_child(client,
555 GOTD_AUTH_READ | GOTD_AUTH_WRITE,
556 repo, gotd.argv0, gotd.confpath, gotd.daemonize,
557 gotd.verbosity);
558 if (err)
559 return err;
562 evtimer_add(&client->tmo, &auth_timeout);
564 /* Flow continues upon authentication success/failure or timeout. */
565 return NULL;
568 static void
569 gotd_request(int fd, short events, void *arg)
571 struct gotd_imsgev *iev = arg;
572 struct imsgbuf *ibuf = &iev->ibuf;
573 struct gotd_client *client = iev->handler_arg;
574 const struct got_error *err = NULL;
575 struct imsg imsg;
576 ssize_t n;
578 if (events & EV_WRITE) {
579 while (ibuf->w.queued) {
580 n = msgbuf_write(&ibuf->w);
581 if (n == -1 && errno == EPIPE) {
582 /*
583 * The client has closed its socket.
584 * This can happen when Git clients are
585 * done sending pack file data.
586 */
587 msgbuf_clear(&ibuf->w);
588 continue;
589 } else if (n == -1 && errno != EAGAIN) {
590 err = got_error_from_errno("imsg_flush");
591 disconnect_on_error(client, err);
592 return;
594 if (n == 0) {
595 /* Connection closed. */
596 err = got_error(GOT_ERR_EOF);
597 disconnect_on_error(client, err);
598 return;
602 /* Disconnect gotctl(8) now that messages have been sent. */
603 if (!client_is_reading(client) && !client_is_writing(client)) {
604 disconnect(client);
605 return;
609 if ((events & EV_READ) == 0)
610 return;
612 memset(&imsg, 0, sizeof(imsg));
614 while (err == NULL) {
615 err = gotd_imsg_recv(&imsg, ibuf, 0);
616 if (err) {
617 if (err->code == GOT_ERR_PRIVSEP_READ)
618 err = NULL;
619 break;
622 evtimer_del(&client->tmo);
624 switch (imsg.hdr.type) {
625 case GOTD_IMSG_INFO:
626 err = send_info(client);
627 break;
628 case GOTD_IMSG_STOP:
629 err = stop_gotd(client);
630 break;
631 case GOTD_IMSG_LIST_REFS:
632 err = start_client_authentication(client, &imsg);
633 break;
634 default:
635 log_debug("unexpected imsg %d", imsg.hdr.type);
636 err = got_error(GOT_ERR_PRIVSEP_MSG);
637 break;
640 imsg_free(&imsg);
643 if (err) {
644 disconnect_on_error(client, err);
645 } else {
646 gotd_imsg_event_add(&client->iev);
650 static void
651 gotd_auth_timeout(int fd, short events, void *arg)
653 struct gotd_client *client = arg;
655 log_debug("disconnecting uid %d due to authentication timeout",
656 client->euid);
657 disconnect(client);
660 static const struct got_error *
661 recv_connect(uint32_t *client_id, struct imsg *imsg)
663 const struct got_error *err = NULL;
664 struct gotd_imsg_connect iconnect;
665 size_t datalen;
666 int s = -1;
667 struct gotd_client *client = NULL;
669 *client_id = 0;
671 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
672 if (datalen != sizeof(iconnect))
673 return got_error(GOT_ERR_PRIVSEP_LEN);
674 memcpy(&iconnect, imsg->data, sizeof(iconnect));
676 s = imsg_get_fd(imsg);
677 if (s == -1) {
678 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
679 goto done;
682 if (find_client(iconnect.client_id)) {
683 err = got_error_msg(GOT_ERR_CLIENT_ID, "duplicate client ID");
684 goto done;
687 client = calloc(1, sizeof(*client));
688 if (client == NULL) {
689 err = got_error_from_errno("calloc");
690 goto done;
693 *client_id = iconnect.client_id;
695 client->state = GOTD_CLIENT_STATE_NEW;
696 client->id = iconnect.client_id;
697 client->fd = s;
698 s = -1;
699 /* The auth process will verify UID/GID for us. */
700 client->euid = iconnect.euid;
701 client->egid = iconnect.egid;
703 imsg_init(&client->iev.ibuf, client->fd);
704 client->iev.handler = gotd_request;
705 client->iev.events = EV_READ;
706 client->iev.handler_arg = client;
708 event_set(&client->iev.ev, client->fd, EV_READ, gotd_request,
709 &client->iev);
710 gotd_imsg_event_add(&client->iev);
712 evtimer_set(&client->tmo, gotd_auth_timeout, client);
714 add_client(client);
715 log_debug("%s: new client uid %d connected on fd %d", __func__,
716 client->euid, client->fd);
717 done:
718 if (err) {
719 struct gotd_child_proc *listen_proc = gotd.listen_proc;
720 struct gotd_imsg_disconnect idisconnect;
722 idisconnect.client_id = client->id;
723 if (gotd_imsg_compose_event(&listen_proc->iev,
724 GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
725 &idisconnect, sizeof(idisconnect)) == -1)
726 log_warn("imsg compose DISCONNECT");
728 if (s != -1)
729 close(s);
732 return err;
735 static const char *gotd_proc_names[PROC_MAX] = {
736 "parent",
737 "listen",
738 "auth",
739 "session_read",
740 "session_write",
741 "repo_read",
742 "repo_write",
743 "gitwrapper",
744 "notify"
745 };
747 static void
748 kill_proc(struct gotd_child_proc *proc, int fatal)
750 struct timeval tv = { 5, 0 };
752 log_debug("kill -%d %d", fatal ? SIGKILL : SIGTERM, proc->pid);
754 if (proc->iev.ibuf.fd != -1) {
755 event_del(&proc->iev.ev);
756 msgbuf_clear(&proc->iev.ibuf.w);
757 close(proc->iev.ibuf.fd);
758 proc->iev.ibuf.fd = -1;
761 if (!evtimer_pending(&proc->tmo, NULL) && !fatal)
762 evtimer_add(&proc->tmo, &tv);
764 if (fatal) {
765 log_warnx("sending SIGKILL to PID %d", proc->pid);
766 kill(proc->pid, SIGKILL);
767 } else
768 kill(proc->pid, SIGTERM);
771 static void
772 kill_proc_timeout(int fd, short ev, void *d)
774 struct gotd_child_proc *proc = d;
776 log_warnx("timeout waiting for PID %d to terminate;"
777 " retrying with force", proc->pid);
778 kill_proc(proc, 1);
781 static void
782 gotd_shutdown(void)
784 uint64_t slot;
786 log_debug("shutting down");
787 for (slot = 0; slot < nitems(gotd_clients); slot++) {
788 struct gotd_client *c, *tmp;
790 STAILQ_FOREACH_SAFE(c, &gotd_clients[slot], entry, tmp)
791 disconnect(c);
794 kill_proc(gotd.listen_proc, 0);
796 log_info("terminating");
797 exit(0);
800 static struct gotd_child_proc *
801 find_proc_by_pid(pid_t pid)
803 struct gotd_child_proc *proc = NULL;
805 TAILQ_FOREACH(proc, &procs, entry)
806 if (proc->pid == pid)
807 break;
809 return proc;
812 void
813 gotd_sighdlr(int sig, short event, void *arg)
815 struct gotd_child_proc *proc;
816 pid_t pid;
817 int status;
819 /*
820 * Normal signal handler rules don't apply because libevent
821 * decouples for us.
822 */
824 switch (sig) {
825 case SIGHUP:
826 log_info("%s: ignoring SIGHUP", __func__);
827 break;
828 case SIGUSR1:
829 log_info("%s: ignoring SIGUSR1", __func__);
830 break;
831 case SIGTERM:
832 case SIGINT:
833 gotd_shutdown();
834 break;
835 case SIGCHLD:
836 for (;;) {
837 pid = waitpid(WAIT_ANY, &status, WNOHANG);
838 if (pid == -1) {
839 if (errno == EINTR)
840 continue;
841 if (errno == ECHILD)
842 break;
843 fatal("waitpid");
845 if (pid == 0)
846 break;
848 log_debug("reaped pid %d", pid);
849 proc = find_proc_by_pid(pid);
850 if (proc == NULL) {
851 log_info("caught exit of unknown child %d",
852 pid);
853 continue;
856 if (WIFSIGNALED(status)) {
857 log_warnx("child PID %d terminated with"
858 " signal %d", pid, WTERMSIG(status));
861 proc_done(proc);
863 break;
864 default:
865 fatalx("unexpected signal");
869 static const struct got_error *
870 ensure_proc_is_reading(struct gotd_client *client,
871 struct gotd_child_proc *proc)
873 if (!client_is_reading(client)) {
874 kill_proc(proc, 1);
875 return got_error_fmt(GOT_ERR_BAD_PACKET,
876 "PID %d handled a read-request for uid %d but this "
877 "user is not reading from a repository", proc->pid,
878 client->euid);
881 return NULL;
884 static const struct got_error *
885 ensure_proc_is_writing(struct gotd_client *client,
886 struct gotd_child_proc *proc)
888 if (!client_is_writing(client)) {
889 kill_proc(proc, 1);
890 return got_error_fmt(GOT_ERR_BAD_PACKET,
891 "PID %d handled a write-request for uid %d but this "
892 "user is not writing to a repository", proc->pid,
893 client->euid);
896 return NULL;
899 static int
900 verify_imsg_src(struct gotd_client *client, struct gotd_child_proc *proc,
901 struct imsg *imsg)
903 const struct got_error *err;
904 int ret = 0;
906 if (proc->type == PROC_REPO_READ || proc->type == PROC_REPO_WRITE) {
907 if (client->repo == NULL)
908 fatalx("no process found for uid %d", client->euid);
909 if (proc->pid != client->repo->pid) {
910 kill_proc(proc, 1);
911 log_warnx("received message from PID %d for uid %d, "
912 "while PID %d is the process serving this user",
913 proc->pid, client->euid, client->repo->pid);
914 return 0;
917 if (proc->type == PROC_SESSION_READ ||
918 proc->type == PROC_SESSION_WRITE) {
919 if (client->session == NULL) {
920 log_warnx("no session found for uid %d", client->euid);
921 return 0;
923 if (proc->pid != client->session->pid) {
924 kill_proc(proc, 1);
925 log_warnx("received message from PID %d for uid %d, "
926 "while PID %d is the process serving this user",
927 proc->pid, client->euid, client->session->pid);
928 return 0;
932 switch (imsg->hdr.type) {
933 case GOTD_IMSG_ERROR:
934 ret = 1;
935 break;
936 case GOTD_IMSG_CONNECT:
937 if (proc->type != PROC_LISTEN) {
938 err = got_error_fmt(GOT_ERR_BAD_PACKET,
939 "new connection for uid %d from PID %d "
940 "which is not the listen process",
941 client->euid, proc->pid);
942 } else
943 ret = 1;
944 break;
945 case GOTD_IMSG_ACCESS_GRANTED:
946 if (proc->type != PROC_AUTH) {
947 err = got_error_fmt(GOT_ERR_BAD_PACKET,
948 "authentication of uid %d from PID %d "
949 "which is not the auth process",
950 client->euid, proc->pid);
951 } else
952 ret = 1;
953 break;
954 case GOTD_IMSG_CLIENT_SESSION_READY:
955 if (proc->type != PROC_SESSION_READ &&
956 proc->type != PROC_SESSION_WRITE) {
957 err = got_error_fmt(GOT_ERR_BAD_PACKET,
958 "unexpected \"ready\" signal from PID %d",
959 proc->pid);
960 } else
961 ret = 1;
962 break;
963 case GOTD_IMSG_REPO_CHILD_READY:
964 if (proc->type != PROC_REPO_READ &&
965 proc->type != PROC_REPO_WRITE) {
966 err = got_error_fmt(GOT_ERR_BAD_PACKET,
967 "unexpected \"ready\" signal from PID %d",
968 proc->pid);
969 } else
970 ret = 1;
971 break;
972 case GOTD_IMSG_PACKFILE_DONE:
973 err = ensure_proc_is_reading(client, proc);
974 if (err)
975 log_warnx("uid %d: %s", client->euid, err->msg);
976 else
977 ret = 1;
978 break;
979 case GOTD_IMSG_PACKFILE_INSTALL:
980 case GOTD_IMSG_REF_UPDATES_START:
981 case GOTD_IMSG_REF_UPDATE:
982 err = ensure_proc_is_writing(client, proc);
983 if (err)
984 log_warnx("uid %d: %s", client->euid, err->msg);
985 else
986 ret = 1;
987 break;
988 default:
989 log_debug("%s: unexpected imsg %d", __func__, imsg->hdr.type);
990 break;
993 return ret;
996 static const struct got_error *
997 connect_repo_child(struct gotd_client *client,
998 struct gotd_child_proc *repo_proc)
1000 static const struct got_error *err;
1001 struct gotd_imsgev *session_iev = &client->session->iev;
1002 struct gotd_imsg_connect_repo_child ireq;
1003 int pipe[2];
1005 if (client->state != GOTD_CLIENT_STATE_ACCESS_GRANTED)
1006 return got_error_msg(GOT_ERR_BAD_REQUEST,
1007 "unexpected repo child ready signal received");
1009 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1010 PF_UNSPEC, pipe) == -1)
1011 fatal("socketpair");
1013 memset(&ireq, 0, sizeof(ireq));
1014 ireq.proc_id = repo_proc->type;
1016 /* Pass repo child pipe to session child process. */
1017 if (gotd_imsg_compose_event(session_iev, GOTD_IMSG_CONNECT_REPO_CHILD,
1018 PROC_GOTD, pipe[0], &ireq, sizeof(ireq)) == -1) {
1019 err = got_error_from_errno("imsg compose CONNECT_REPO_CHILD");
1020 close(pipe[0]);
1021 close(pipe[1]);
1022 return err;
1025 /* Pass session child pipe to repo child process. */
1026 if (gotd_imsg_compose_event(&repo_proc->iev,
1027 GOTD_IMSG_CONNECT_REPO_CHILD, PROC_GOTD, pipe[1], NULL, 0) == -1) {
1028 err = got_error_from_errno("imsg compose CONNECT_REPO_CHILD");
1029 close(pipe[1]);
1030 return err;
1033 return NULL;
1036 static void
1037 gotd_dispatch_listener(int fd, short event, void *arg)
1039 struct gotd_imsgev *iev = arg;
1040 struct imsgbuf *ibuf = &iev->ibuf;
1041 struct gotd_child_proc *proc = gotd.listen_proc;
1042 ssize_t n;
1043 int shut = 0;
1044 struct imsg imsg;
1046 if (proc->iev.ibuf.fd != fd)
1047 fatalx("%s: unexpected fd %d", __func__, fd);
1049 if (event & EV_READ) {
1050 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1051 fatal("imsg_read error");
1052 if (n == 0) {
1053 /* Connection closed. */
1054 shut = 1;
1055 goto done;
1059 if (event & EV_WRITE) {
1060 n = msgbuf_write(&ibuf->w);
1061 if (n == -1 && errno != EAGAIN)
1062 fatal("msgbuf_write");
1063 if (n == 0) {
1064 /* Connection closed. */
1065 shut = 1;
1066 goto done;
1070 for (;;) {
1071 const struct got_error *err = NULL;
1072 struct gotd_client *client = NULL;
1073 uint32_t client_id = 0;
1074 int do_disconnect = 0;
1076 if ((n = imsg_get(ibuf, &imsg)) == -1)
1077 fatal("%s: imsg_get error", __func__);
1078 if (n == 0) /* No more messages. */
1079 break;
1081 switch (imsg.hdr.type) {
1082 case GOTD_IMSG_ERROR:
1083 do_disconnect = 1;
1084 err = gotd_imsg_recv_error(&client_id, &imsg);
1085 break;
1086 case GOTD_IMSG_CONNECT:
1087 err = recv_connect(&client_id, &imsg);
1088 break;
1089 default:
1090 log_debug("unexpected imsg %d", imsg.hdr.type);
1091 break;
1094 client = find_client(client_id);
1095 if (client == NULL) {
1096 log_warnx("%s: client not found", __func__);
1097 imsg_free(&imsg);
1098 continue;
1101 if (err)
1102 log_warnx("uid %d: %s", client->euid, err->msg);
1104 if (do_disconnect) {
1105 if (err)
1106 disconnect_on_error(client, err);
1107 else
1108 disconnect(client);
1111 imsg_free(&imsg);
1113 done:
1114 if (!shut) {
1115 gotd_imsg_event_add(iev);
1116 } else {
1117 /* This pipe is dead. Remove its event handler */
1118 event_del(&iev->ev);
1119 event_loopexit(NULL);
1123 static void
1124 gotd_dispatch_notifier(int fd, short event, void *arg)
1126 struct gotd_imsgev *iev = arg;
1127 struct imsgbuf *ibuf = &iev->ibuf;
1128 struct gotd_child_proc *proc = gotd.notify_proc;
1129 ssize_t n;
1130 int shut = 0;
1131 struct imsg imsg;
1133 if (proc->iev.ibuf.fd != fd)
1134 fatalx("%s: unexpected fd %d", __func__, fd);
1136 if (event & EV_READ) {
1137 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1138 fatal("imsg_read error");
1139 if (n == 0) {
1140 /* Connection closed. */
1141 shut = 1;
1142 goto done;
1146 if (event & EV_WRITE) {
1147 n = msgbuf_write(&ibuf->w);
1148 if (n == -1 && errno != EAGAIN)
1149 fatal("msgbuf_write");
1150 if (n == 0) {
1151 /* Connection closed. */
1152 shut = 1;
1153 goto done;
1157 for (;;) {
1158 if ((n = imsg_get(ibuf, &imsg)) == -1)
1159 fatal("%s: imsg_get error", __func__);
1160 if (n == 0) /* No more messages. */
1161 break;
1163 switch (imsg.hdr.type) {
1164 default:
1165 log_debug("unexpected imsg %d", imsg.hdr.type);
1166 break;
1169 imsg_free(&imsg);
1171 done:
1172 if (!shut) {
1173 gotd_imsg_event_add(iev);
1174 } else {
1175 /* This pipe is dead. Remove its event handler */
1176 event_del(&iev->ev);
1179 * Do not exit all of gotd if the notification handler dies.
1180 * We can continue operating without notifications until an
1181 * operator intervenes.
1183 log_warnx("notify child process (pid %d) closed its imsg pipe "
1184 "unexpectedly", proc->pid);
1185 proc_done(proc);
1189 static void
1190 gotd_dispatch_auth_child(int fd, short event, void *arg)
1192 const struct got_error *err = NULL;
1193 struct gotd_imsgev *iev = arg;
1194 struct imsgbuf *ibuf = &iev->ibuf;
1195 struct gotd_client *client;
1196 struct gotd_repo *repo = NULL;
1197 ssize_t n;
1198 int shut = 0;
1199 struct imsg imsg;
1200 uint32_t client_id = 0;
1201 int do_disconnect = 0;
1202 size_t datalen;
1204 client = find_client_by_proc_fd(fd);
1205 if (client == NULL) {
1206 /* Can happen during process teardown. */
1207 warnx("cannot find client for fd %d", fd);
1208 shut = 1;
1209 goto done;
1212 if (client->auth == NULL)
1213 fatalx("cannot find auth child process for fd %d", fd);
1215 if (event & EV_READ) {
1216 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1217 fatal("imsg_read error");
1218 if (n == 0) {
1219 /* Connection closed. */
1220 shut = 1;
1221 goto done;
1225 if (event & EV_WRITE) {
1226 n = msgbuf_write(&ibuf->w);
1227 if (n == -1 && errno != EAGAIN)
1228 fatal("msgbuf_write");
1229 if (n == 0) {
1230 /* Connection closed. */
1231 shut = 1;
1233 goto done;
1236 if (client->auth->iev.ibuf.fd != fd)
1237 fatalx("%s: unexpected fd %d", __func__, fd);
1239 if ((n = imsg_get(ibuf, &imsg)) == -1)
1240 fatal("%s: imsg_get error", __func__);
1241 if (n == 0) /* No more messages. */
1242 return;
1244 evtimer_del(&client->tmo);
1246 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1248 switch (imsg.hdr.type) {
1249 case GOTD_IMSG_ERROR:
1250 do_disconnect = 1;
1251 err = gotd_imsg_recv_error(&client_id, &imsg);
1252 break;
1253 case GOTD_IMSG_ACCESS_GRANTED:
1254 if (client->state != GOTD_CLIENT_STATE_NEW) {
1255 do_disconnect = 1;
1256 err = got_error(GOT_ERR_PRIVSEP_MSG);
1258 break;
1259 default:
1260 do_disconnect = 1;
1261 log_debug("unexpected imsg %d", imsg.hdr.type);
1262 break;
1265 if (!verify_imsg_src(client, client->auth, &imsg)) {
1266 do_disconnect = 1;
1267 log_debug("dropping imsg type %d from PID %d",
1268 imsg.hdr.type, client->auth->pid);
1271 if (do_disconnect) {
1272 if (err)
1273 disconnect_on_error(client, err);
1274 else
1275 disconnect(client);
1276 imsg_free(&imsg);
1277 return;
1280 client->state = GOTD_CLIENT_STATE_ACCESS_GRANTED;
1281 if (datalen > 0)
1282 client->username = strndup(imsg.data, datalen);
1283 imsg_free(&imsg);
1284 if (client->username == NULL &&
1285 asprintf(&client->username, "uid %d", client->euid) == -1) {
1286 err = got_error_from_errno("asprintf");
1287 goto done;
1290 repo = gotd_find_repo_by_name(client->auth->repo_name, &gotd.repos);
1291 if (repo == NULL) {
1292 err = got_error(GOT_ERR_NOT_GIT_REPO);
1293 goto done;
1295 kill_auth_proc(client);
1297 log_info("authenticated %s for repository %s",
1298 client->username, repo->name);
1300 err = start_session_child(client, repo, gotd.argv0,
1301 gotd.confpath, gotd.daemonize, gotd.verbosity);
1302 if (err)
1303 goto done;
1304 done:
1305 if (err)
1306 log_warnx("uid %d: %s", client->euid, err->msg);
1308 /* We might have killed the auth process by now. */
1309 if (client->auth != NULL) {
1310 if (!shut) {
1311 gotd_imsg_event_add(iev);
1312 } else {
1313 /* This pipe is dead. Remove its event handler */
1314 event_del(&iev->ev);
1319 static const struct got_error *
1320 connect_session(struct gotd_client *client)
1322 const struct got_error *err = NULL;
1323 struct gotd_imsg_connect iconnect;
1324 int s;
1325 struct ibuf *wbuf;
1327 memset(&iconnect, 0, sizeof(iconnect));
1329 s = dup(client->fd);
1330 if (s == -1)
1331 return got_error_from_errno("dup");
1333 iconnect.client_id = client->id;
1334 iconnect.euid = client->euid;
1335 iconnect.egid = client->egid;
1336 iconnect.username_len = strlen(client->username);
1338 wbuf = imsg_create(&client->session->iev.ibuf, GOTD_IMSG_CONNECT,
1339 PROC_GOTD, gotd.pid, sizeof(iconnect) + iconnect.username_len);
1340 if (wbuf == NULL) {
1341 err = got_error_from_errno("imsg compose CONNECT");
1342 close(s);
1343 return err;
1345 if (imsg_add(wbuf, &iconnect, sizeof(iconnect)) == -1) {
1346 close(s);
1347 return got_error_from_errno("imsg_add CONNECT");
1349 if (imsg_add(wbuf, client->username, iconnect.username_len) == -1) {
1350 close(s);
1351 return got_error_from_errno("imsg_add CONNECT");
1354 ibuf_fd_set(wbuf, s);
1355 imsg_close(&client->session->iev.ibuf, wbuf);
1356 gotd_imsg_event_add(&client->session->iev);
1359 * We are no longer interested in messages from this client.
1360 * Further client requests will be handled by the session process.
1362 msgbuf_clear(&client->iev.ibuf.w);
1363 imsg_clear(&client->iev.ibuf);
1364 event_del(&client->iev.ev);
1365 client->fd = -1; /* will be closed via copy in client->iev.ibuf.fd */
1367 return NULL;
1370 static void
1371 gotd_dispatch_client_session(int fd, short event, void *arg)
1373 struct gotd_imsgev *iev = arg;
1374 struct imsgbuf *ibuf = &iev->ibuf;
1375 struct gotd_child_proc *proc = NULL;
1376 struct gotd_client *client = NULL;
1377 ssize_t n;
1378 int shut = 0;
1379 struct imsg imsg;
1381 client = find_client_by_proc_fd(fd);
1382 if (client == NULL) {
1383 /* Can happen during process teardown. */
1384 warnx("cannot find client for fd %d", fd);
1385 shut = 1;
1386 goto done;
1389 if (event & EV_READ) {
1390 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1391 fatal("imsg_read error");
1392 if (n == 0) {
1393 /* Connection closed. */
1394 shut = 1;
1395 goto done;
1399 if (event & EV_WRITE) {
1400 n = msgbuf_write(&ibuf->w);
1401 if (n == -1 && errno != EAGAIN)
1402 fatal("msgbuf_write");
1403 if (n == 0) {
1404 /* Connection closed. */
1405 shut = 1;
1406 goto done;
1410 proc = client->session;
1411 if (proc == NULL)
1412 fatalx("cannot find session child process for fd %d", fd);
1414 for (;;) {
1415 const struct got_error *err = NULL;
1416 uint32_t client_id = 0;
1417 int do_disconnect = 0, do_start_repo_child = 0;
1419 if ((n = imsg_get(ibuf, &imsg)) == -1)
1420 fatal("%s: imsg_get error", __func__);
1421 if (n == 0) /* No more messages. */
1422 break;
1424 switch (imsg.hdr.type) {
1425 case GOTD_IMSG_ERROR:
1426 do_disconnect = 1;
1427 err = gotd_imsg_recv_error(&client_id, &imsg);
1428 break;
1429 case GOTD_IMSG_CLIENT_SESSION_READY:
1430 if (client->state != GOTD_CLIENT_STATE_ACCESS_GRANTED) {
1431 err = got_error(GOT_ERR_PRIVSEP_MSG);
1432 break;
1434 do_start_repo_child = 1;
1435 break;
1436 case GOTD_IMSG_DISCONNECT:
1437 do_disconnect = 1;
1438 break;
1439 default:
1440 log_debug("unexpected imsg %d", imsg.hdr.type);
1441 break;
1444 if (!verify_imsg_src(client, proc, &imsg)) {
1445 log_debug("dropping imsg type %d from PID %d",
1446 imsg.hdr.type, proc->pid);
1447 imsg_free(&imsg);
1448 continue;
1450 if (err)
1451 log_warnx("uid %d: %s", client->euid, err->msg);
1453 if (do_start_repo_child) {
1454 struct gotd_repo *repo;
1455 const char *name = client->session->repo_name;
1457 repo = gotd_find_repo_by_name(name, &gotd.repos);
1458 if (repo != NULL) {
1459 enum gotd_procid proc_type;
1461 if (client->required_auth & GOTD_AUTH_WRITE)
1462 proc_type = PROC_REPO_WRITE;
1463 else
1464 proc_type = PROC_REPO_READ;
1466 err = start_repo_child(client, proc_type, repo,
1467 gotd.argv0, gotd.confpath, gotd.daemonize,
1468 gotd.verbosity);
1469 } else
1470 err = got_error(GOT_ERR_NOT_GIT_REPO);
1472 if (err) {
1473 log_warnx("uid %d: %s", client->euid, err->msg);
1474 do_disconnect = 1;
1478 if (do_disconnect) {
1479 if (err)
1480 disconnect_on_error(client, err);
1481 else
1482 disconnect(client);
1485 imsg_free(&imsg);
1487 done:
1488 if (!shut) {
1489 gotd_imsg_event_add(iev);
1490 } else {
1491 /* This pipe is dead. Remove its event handler */
1492 event_del(&iev->ev);
1493 disconnect(client);
1497 static const struct got_error *
1498 connect_notifier_and_session(struct gotd_client *client)
1500 const struct got_error *err = NULL;
1501 struct gotd_imsgev *session_iev = &client->session->iev;
1502 int pipe[2];
1504 if (gotd.notify_proc == NULL)
1505 return NULL;
1507 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1508 PF_UNSPEC, pipe) == -1)
1509 return got_error_from_errno("socketpair");
1511 /* Pass notifier pipe to session . */
1512 if (gotd_imsg_compose_event(session_iev, GOTD_IMSG_CONNECT_NOTIFIER,
1513 PROC_GOTD, pipe[0], NULL, 0) == -1) {
1514 err = got_error_from_errno("imsg compose CONNECT_NOTIFIER");
1515 close(pipe[0]);
1516 close(pipe[1]);
1517 return err;
1520 /* Pass session pipe to notifier. */
1521 if (gotd_imsg_compose_event(&gotd.notify_proc->iev,
1522 GOTD_IMSG_CONNECT_SESSION, PROC_GOTD, pipe[1], NULL, 0) == -1) {
1523 err = got_error_from_errno("imsg compose CONNECT_SESSION");
1524 close(pipe[1]);
1525 return err;
1528 return NULL;
1531 static void
1532 gotd_dispatch_repo_child(int fd, short event, void *arg)
1534 struct gotd_imsgev *iev = arg;
1535 struct imsgbuf *ibuf = &iev->ibuf;
1536 struct gotd_child_proc *proc = NULL;
1537 struct gotd_client *client;
1538 ssize_t n;
1539 int shut = 0;
1540 struct imsg imsg;
1542 client = find_client_by_proc_fd(fd);
1543 if (client == NULL) {
1544 /* Can happen during process teardown. */
1545 warnx("cannot find client for fd %d", fd);
1546 shut = 1;
1547 goto done;
1550 if (event & EV_READ) {
1551 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1552 fatal("imsg_read error");
1553 if (n == 0) {
1554 /* Connection closed. */
1555 shut = 1;
1556 goto done;
1560 if (event & EV_WRITE) {
1561 n = msgbuf_write(&ibuf->w);
1562 if (n == -1 && errno != EAGAIN)
1563 fatal("msgbuf_write");
1564 if (n == 0) {
1565 /* Connection closed. */
1566 shut = 1;
1567 goto done;
1571 proc = client->repo;
1572 if (proc == NULL)
1573 fatalx("cannot find child process for fd %d", fd);
1575 for (;;) {
1576 const struct got_error *err = NULL;
1577 uint32_t client_id = 0;
1578 int do_disconnect = 0;
1580 if ((n = imsg_get(ibuf, &imsg)) == -1)
1581 fatal("%s: imsg_get error", __func__);
1582 if (n == 0) /* No more messages. */
1583 break;
1585 switch (imsg.hdr.type) {
1586 case GOTD_IMSG_ERROR:
1587 do_disconnect = 1;
1588 err = gotd_imsg_recv_error(&client_id, &imsg);
1589 break;
1590 case GOTD_IMSG_REPO_CHILD_READY:
1591 err = connect_session(client);
1592 if (err)
1593 break;
1594 err = connect_notifier_and_session(client);
1595 if (err)
1596 break;
1597 err = connect_repo_child(client, proc);
1598 break;
1599 default:
1600 log_debug("unexpected imsg %d", imsg.hdr.type);
1601 break;
1604 if (!verify_imsg_src(client, proc, &imsg)) {
1605 log_debug("dropping imsg type %d from PID %d",
1606 imsg.hdr.type, proc->pid);
1607 imsg_free(&imsg);
1608 continue;
1610 if (err)
1611 log_warnx("uid %d: %s", client->euid, err->msg);
1613 if (do_disconnect) {
1614 if (err)
1615 disconnect_on_error(client, err);
1616 else
1617 disconnect(client);
1620 imsg_free(&imsg);
1622 done:
1623 if (!shut) {
1624 gotd_imsg_event_add(iev);
1625 } else {
1626 /* This pipe is dead. Remove its event handler */
1627 event_del(&iev->ev);
1628 disconnect(client);
1632 static pid_t
1633 start_child(enum gotd_procid proc_id, const char *repo_path,
1634 char *argv0, const char *confpath, int fd, int daemonize, int verbosity)
1636 char *argv[11];
1637 int argc = 0;
1638 pid_t pid;
1640 switch (pid = fork()) {
1641 case -1:
1642 fatal("cannot fork");
1643 case 0:
1644 break;
1645 default:
1646 close(fd);
1647 return pid;
1650 if (fd != GOTD_FILENO_MSG_PIPE) {
1651 if (dup2(fd, GOTD_FILENO_MSG_PIPE) == -1)
1652 fatal("cannot setup imsg fd");
1653 } else if (fcntl(fd, F_SETFD, 0) == -1)
1654 fatal("cannot setup imsg fd");
1656 argv[argc++] = argv0;
1657 switch (proc_id) {
1658 case PROC_LISTEN:
1659 argv[argc++] = (char *)"-L";
1660 break;
1661 case PROC_AUTH:
1662 argv[argc++] = (char *)"-A";
1663 break;
1664 case PROC_SESSION_READ:
1665 argv[argc++] = (char *)"-s";
1666 break;
1667 case PROC_SESSION_WRITE:
1668 argv[argc++] = (char *)"-S";
1669 break;
1670 case PROC_REPO_READ:
1671 argv[argc++] = (char *)"-R";
1672 break;
1673 case PROC_REPO_WRITE:
1674 argv[argc++] = (char *)"-W";
1675 break;
1676 case PROC_NOTIFY:
1677 argv[argc++] = (char *)"-N";
1678 break;
1679 default:
1680 fatalx("invalid process id %d", proc_id);
1683 argv[argc++] = (char *)"-f";
1684 argv[argc++] = (char *)confpath;
1686 if (repo_path) {
1687 argv[argc++] = (char *)"-P";
1688 argv[argc++] = (char *)repo_path;
1691 if (!daemonize)
1692 argv[argc++] = (char *)"-d";
1693 if (verbosity > 0)
1694 argv[argc++] = (char *)"-v";
1695 if (verbosity > 1)
1696 argv[argc++] = (char *)"-v";
1697 argv[argc++] = NULL;
1699 execvp(argv0, argv);
1700 fatal("execvp");
1703 static void
1704 start_listener(char *argv0, const char *confpath, int daemonize, int verbosity)
1706 struct gotd_child_proc *proc;
1708 proc = calloc(1, sizeof(*proc));
1709 if (proc == NULL)
1710 fatal("calloc");
1712 TAILQ_INSERT_HEAD(&procs, proc, entry);
1714 /* proc->tmo is initialized in main() after event_init() */
1716 proc->type = PROC_LISTEN;
1718 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1719 PF_UNSPEC, proc->pipe) == -1)
1720 fatal("socketpair");
1722 proc->pid = start_child(proc->type, NULL, argv0, confpath,
1723 proc->pipe[1], daemonize, verbosity);
1724 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1725 proc->iev.handler = gotd_dispatch_listener;
1726 proc->iev.events = EV_READ;
1727 proc->iev.handler_arg = NULL;
1729 gotd.listen_proc = proc;
1732 static void
1733 start_notifier(char *argv0, const char *confpath, int daemonize, int verbosity)
1735 struct gotd_child_proc *proc;
1737 proc = calloc(1, sizeof(*proc));
1738 if (proc == NULL)
1739 fatal("calloc");
1741 TAILQ_INSERT_HEAD(&procs, proc, entry);
1743 /* proc->tmo is initialized in main() after event_init() */
1745 proc->type = PROC_NOTIFY;
1747 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1748 PF_UNSPEC, proc->pipe) == -1)
1749 fatal("socketpair");
1751 proc->pid = start_child(proc->type, NULL, argv0, confpath,
1752 proc->pipe[1], daemonize, verbosity);
1753 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1754 proc->iev.handler = gotd_dispatch_notifier;
1755 proc->iev.events = EV_READ;
1756 proc->iev.handler_arg = NULL;
1757 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1758 gotd_dispatch_notifier, &proc->iev);
1760 gotd.notify_proc = proc;
1763 static const struct got_error *
1764 start_session_child(struct gotd_client *client, struct gotd_repo *repo,
1765 char *argv0, const char *confpath, int daemonize, int verbosity)
1767 struct gotd_child_proc *proc;
1769 proc = calloc(1, sizeof(*proc));
1770 if (proc == NULL)
1771 return got_error_from_errno("calloc");
1773 TAILQ_INSERT_HEAD(&procs, proc, entry);
1774 evtimer_set(&proc->tmo, kill_proc_timeout, proc);
1776 if (client_is_reading(client))
1777 proc->type = PROC_SESSION_READ;
1778 else
1779 proc->type = PROC_SESSION_WRITE;
1780 if (strlcpy(proc->repo_name, repo->name,
1781 sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
1782 fatalx("repository name too long: %s", repo->name);
1783 log_debug("starting client uid %d session for repository %s",
1784 client->euid, repo->name);
1785 if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >=
1786 sizeof(proc->repo_path))
1787 fatalx("repository path too long: %s", repo->path);
1788 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1789 PF_UNSPEC, proc->pipe) == -1)
1790 fatal("socketpair");
1791 proc->pid = start_child(proc->type, proc->repo_path, argv0,
1792 confpath, proc->pipe[1], daemonize, verbosity);
1793 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1794 log_debug("proc %s %s is on fd %d",
1795 gotd_proc_names[proc->type], proc->repo_path,
1796 proc->pipe[0]);
1797 proc->iev.handler = gotd_dispatch_client_session;
1798 proc->iev.events = EV_READ;
1799 proc->iev.handler_arg = NULL;
1800 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1801 gotd_dispatch_client_session, &proc->iev);
1802 gotd_imsg_event_add(&proc->iev);
1804 client->session = proc;
1805 return NULL;
1808 static const struct got_error *
1809 start_repo_child(struct gotd_client *client, enum gotd_procid proc_type,
1810 struct gotd_repo *repo, char *argv0, const char *confpath,
1811 int daemonize, int verbosity)
1813 struct gotd_child_proc *proc;
1815 if (proc_type != PROC_REPO_READ && proc_type != PROC_REPO_WRITE)
1816 return got_error_msg(GOT_ERR_NOT_IMPL, "bad process type");
1818 proc = calloc(1, sizeof(*proc));
1819 if (proc == NULL)
1820 return got_error_from_errno("calloc");
1822 TAILQ_INSERT_HEAD(&procs, proc, entry);
1823 evtimer_set(&proc->tmo, kill_proc_timeout, proc);
1825 proc->type = proc_type;
1826 if (strlcpy(proc->repo_name, repo->name,
1827 sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
1828 fatalx("repository name too long: %s", repo->name);
1829 log_debug("starting %s for repository %s",
1830 proc->type == PROC_REPO_READ ? "reader" : "writer", repo->name);
1831 if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >=
1832 sizeof(proc->repo_path))
1833 fatalx("repository path too long: %s", repo->path);
1834 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1835 PF_UNSPEC, proc->pipe) == -1)
1836 fatal("socketpair");
1837 proc->pid = start_child(proc->type, proc->repo_path, argv0,
1838 confpath, proc->pipe[1], daemonize, verbosity);
1839 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1840 log_debug("proc %s %s is on fd %d",
1841 gotd_proc_names[proc->type], proc->repo_path,
1842 proc->pipe[0]);
1843 proc->iev.handler = gotd_dispatch_repo_child;
1844 proc->iev.events = EV_READ;
1845 proc->iev.handler_arg = NULL;
1846 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1847 gotd_dispatch_repo_child, &proc->iev);
1848 gotd_imsg_event_add(&proc->iev);
1850 client->repo = proc;
1851 return NULL;
1854 static const struct got_error *
1855 start_auth_child(struct gotd_client *client, int required_auth,
1856 struct gotd_repo *repo, char *argv0, const char *confpath,
1857 int daemonize, int verbosity)
1859 const struct got_error *err = NULL;
1860 struct gotd_child_proc *proc;
1861 struct gotd_imsg_auth iauth;
1862 int fd;
1864 memset(&iauth, 0, sizeof(iauth));
1866 fd = dup(client->fd);
1867 if (fd == -1)
1868 return got_error_from_errno("dup");
1870 proc = calloc(1, sizeof(*proc));
1871 if (proc == NULL) {
1872 err = got_error_from_errno("calloc");
1873 close(fd);
1874 return err;
1877 TAILQ_INSERT_HEAD(&procs, proc, entry);
1878 evtimer_set(&proc->tmo, kill_proc_timeout, proc);
1880 proc->type = PROC_AUTH;
1881 if (strlcpy(proc->repo_name, repo->name,
1882 sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
1883 fatalx("repository name too long: %s", repo->name);
1884 log_debug("starting auth for uid %d repository %s",
1885 client->euid, repo->name);
1886 if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >=
1887 sizeof(proc->repo_path))
1888 fatalx("repository path too long: %s", repo->path);
1889 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1890 PF_UNSPEC, proc->pipe) == -1)
1891 fatal("socketpair");
1892 proc->pid = start_child(proc->type, proc->repo_path, argv0,
1893 confpath, proc->pipe[1], daemonize, verbosity);
1894 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1895 log_debug("proc %s %s is on fd %d",
1896 gotd_proc_names[proc->type], proc->repo_path,
1897 proc->pipe[0]);
1898 proc->iev.handler = gotd_dispatch_auth_child;
1899 proc->iev.events = EV_READ;
1900 proc->iev.handler_arg = NULL;
1901 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1902 gotd_dispatch_auth_child, &proc->iev);
1903 gotd_imsg_event_add(&proc->iev);
1905 iauth.euid = client->euid;
1906 iauth.egid = client->egid;
1907 iauth.required_auth = required_auth;
1908 iauth.client_id = client->id;
1909 if (gotd_imsg_compose_event(&proc->iev, GOTD_IMSG_AUTHENTICATE,
1910 PROC_GOTD, fd, &iauth, sizeof(iauth)) == -1) {
1911 log_warn("imsg compose AUTHENTICATE");
1912 close(fd);
1913 /* Let the auth_timeout handler tidy up. */
1916 client->auth = proc;
1917 client->required_auth = required_auth;
1918 return NULL;
1921 static void
1922 apply_unveil_repo_readonly(const char *repo_path, int need_tmpdir)
1924 if (need_tmpdir) {
1925 if (unveil(GOT_TMPDIR_STR, "rwc") == -1)
1926 fatal("unveil %s", GOT_TMPDIR_STR);
1929 if (unveil(repo_path, "r") == -1)
1930 fatal("unveil %s", repo_path);
1932 if (unveil(NULL, NULL) == -1)
1933 fatal("unveil");
1936 static void
1937 apply_unveil_repo_readwrite(const char *repo_path)
1939 if (unveil(repo_path, "rwc") == -1)
1940 fatal("unveil %s", repo_path);
1942 if (unveil(GOT_TMPDIR_STR, "rwc") == -1)
1943 fatal("unveil %s", GOT_TMPDIR_STR);
1945 if (unveil(NULL, NULL) == -1)
1946 fatal("unveil");
1949 static void
1950 apply_unveil_none(void)
1952 if (unveil("/", "") == -1)
1953 fatal("unveil");
1955 if (unveil(NULL, NULL) == -1)
1956 fatal("unveil");
1959 static void
1960 apply_unveil_selfexec(void)
1962 if (unveil(gotd.argv0, "x") == -1)
1963 fatal("unveil %s", gotd.argv0);
1965 if (unveil(NULL, NULL) == -1)
1966 fatal("unveil");
1969 static void
1970 set_max_datasize(void)
1972 struct rlimit rl;
1974 if (getrlimit(RLIMIT_DATA, &rl) != 0)
1975 return;
1977 rl.rlim_cur = rl.rlim_max;
1978 setrlimit(RLIMIT_DATA, &rl);
1981 static void
1982 unveil_notification_helpers(void)
1984 const char *helpers[] = {
1985 GOTD_PATH_PROG_NOTIFY_EMAIL,
1986 GOTD_PATH_PROG_NOTIFY_HTTP,
1988 size_t i;
1990 for (i = 0; i < nitems(helpers); i++) {
1991 if (unveil(helpers[i], "x") == 0)
1992 continue;
1993 fatal("unveil %s", helpers[i]);
1996 if (unveil(NULL, NULL) == -1)
1997 fatal("unveil");
2000 int
2001 main(int argc, char **argv)
2003 const struct got_error *error = NULL;
2004 int ch, fd = -1, daemonize = 1, verbosity = 0, noaction = 0;
2005 const char *confpath = GOTD_CONF_PATH;
2006 char *argv0 = argv[0];
2007 char title[2048];
2008 struct passwd *pw = NULL;
2009 char *repo_path = NULL;
2010 enum gotd_procid proc_id = PROC_GOTD;
2011 struct event evsigint, evsigterm, evsighup, evsigusr1, evsigchld;
2012 int *pack_fds = NULL, *temp_fds = NULL;
2013 struct gotd_repo *repo = NULL;
2014 char *default_sender = NULL;
2015 char hostname[HOST_NAME_MAX + 1];
2016 FILE *diff_f1 = NULL, *diff_f2 = NULL;
2017 int diff_fd1 = -1, diff_fd2 = -1;
2019 TAILQ_INIT(&procs);
2021 log_init(1, LOG_DAEMON); /* Log to stderr until daemonized. */
2023 while ((ch = getopt(argc, argv, "Adf:LnNP:RsSvW")) != -1) {
2024 switch (ch) {
2025 case 'A':
2026 proc_id = PROC_AUTH;
2027 break;
2028 case 'd':
2029 daemonize = 0;
2030 break;
2031 case 'f':
2032 confpath = optarg;
2033 break;
2034 case 'L':
2035 proc_id = PROC_LISTEN;
2036 break;
2037 case 'n':
2038 noaction = 1;
2039 break;
2040 case 'N':
2041 proc_id = PROC_NOTIFY;
2042 break;
2043 case 'P':
2044 repo_path = realpath(optarg, NULL);
2045 if (repo_path == NULL)
2046 fatal("realpath '%s'", optarg);
2047 break;
2048 case 'R':
2049 proc_id = PROC_REPO_READ;
2050 break;
2051 case 's':
2052 proc_id = PROC_SESSION_READ;
2053 break;
2054 case 'S':
2055 proc_id = PROC_SESSION_WRITE;
2056 break;
2057 case 'v':
2058 if (verbosity < 3)
2059 verbosity++;
2060 break;
2061 case 'W':
2062 proc_id = PROC_REPO_WRITE;
2063 break;
2064 default:
2065 usage();
2069 argc -= optind;
2070 argv += optind;
2072 if (argc != 0)
2073 usage();
2075 if (geteuid() && (proc_id == PROC_GOTD || proc_id == PROC_LISTEN))
2076 fatalx("need root privileges");
2078 if (parse_config(confpath, proc_id, &gotd) != 0)
2079 return 1;
2081 pw = getpwnam(gotd.user_name);
2082 if (pw == NULL)
2083 fatalx("user %s not found", gotd.user_name);
2085 if (pw->pw_uid == 0)
2086 fatalx("cannot run %s as the superuser", getprogname());
2088 if (noaction) {
2089 fprintf(stderr, "configuration OK\n");
2090 return 0;
2093 gotd.argv0 = argv0;
2094 gotd.daemonize = daemonize;
2095 gotd.verbosity = verbosity;
2096 gotd.confpath = confpath;
2098 /* Require an absolute path in argv[0] for reliable re-exec. */
2099 if (!got_path_is_absolute(argv0))
2100 fatalx("bad path \"%s\": must be an absolute path", argv0);
2102 log_init(daemonize ? 0 : 1, LOG_DAEMON);
2103 log_setverbose(verbosity);
2105 if (proc_id == PROC_GOTD) {
2106 snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
2107 arc4random_buf(&clients_hash_key, sizeof(clients_hash_key));
2108 if (daemonize && daemon(1, 0) == -1)
2109 fatal("daemon");
2110 gotd.pid = getpid();
2111 start_listener(argv0, confpath, daemonize, verbosity);
2112 start_notifier(argv0, confpath, daemonize, verbosity);
2113 } else if (proc_id == PROC_LISTEN) {
2114 snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
2115 if (verbosity) {
2116 log_info("socket: %s", gotd.unix_socket_path);
2117 log_info("user: %s", pw->pw_name);
2120 fd = unix_socket_listen(gotd.unix_socket_path, pw->pw_uid,
2121 pw->pw_gid);
2122 if (fd == -1) {
2123 fatal("cannot listen on unix socket %s",
2124 gotd.unix_socket_path);
2126 } else if (proc_id == PROC_AUTH) {
2127 snprintf(title, sizeof(title), "%s %s",
2128 gotd_proc_names[proc_id], repo_path);
2129 } else if (proc_id == PROC_REPO_READ || proc_id == PROC_REPO_WRITE ||
2130 proc_id == PROC_SESSION_READ || proc_id == PROC_SESSION_WRITE) {
2131 error = got_repo_pack_fds_open(&pack_fds);
2132 if (error != NULL)
2133 fatalx("cannot open pack tempfiles: %s", error->msg);
2134 error = got_repo_temp_fds_open(&temp_fds);
2135 if (error != NULL)
2136 fatalx("cannot open pack tempfiles: %s", error->msg);
2137 if (repo_path == NULL)
2138 fatalx("repository path not specified");
2139 snprintf(title, sizeof(title), "%s %s",
2140 gotd_proc_names[proc_id], repo_path);
2141 } else if (proc_id == PROC_NOTIFY) {
2142 snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
2143 if (gethostname(hostname, sizeof(hostname)) == -1)
2144 fatal("gethostname");
2145 if (asprintf(&default_sender, "%s@%s",
2146 pw->pw_name, hostname) == -1)
2147 fatal("asprintf");
2148 } else
2149 fatal("invalid process id %d", proc_id);
2151 setproctitle("%s", title);
2152 log_procinit(title);
2154 /* Drop root privileges. */
2155 if (setgid(pw->pw_gid) == -1)
2156 fatal("setgid %d failed", pw->pw_gid);
2157 if (setuid(pw->pw_uid) == -1)
2158 fatal("setuid %d failed", pw->pw_uid);
2160 event_init();
2162 switch (proc_id) {
2163 case PROC_GOTD:
2164 #ifndef PROFILE
2165 /* "exec" promise will be limited to argv[0] via unveil(2). */
2166 if (pledge("stdio proc exec sendfd recvfd unveil", NULL) == -1)
2167 err(1, "pledge");
2168 #endif
2169 break;
2170 case PROC_LISTEN:
2171 #ifndef PROFILE
2172 if (pledge("stdio sendfd unix unveil", NULL) == -1)
2173 err(1, "pledge");
2174 #endif
2176 * Ensure that AF_UNIX bind(2) cannot be used with any other
2177 * sockets by revoking all filesystem access via unveil(2).
2179 apply_unveil_none();
2181 listen_main(title, fd, gotd.connection_limits,
2182 gotd.nconnection_limits);
2183 /* NOTREACHED */
2184 break;
2185 case PROC_AUTH:
2186 #ifndef PROFILE
2187 if (pledge("stdio getpw recvfd unix unveil", NULL) == -1)
2188 err(1, "pledge");
2189 #endif
2191 * We need the "unix" pledge promise for getpeername(2) only.
2192 * Ensure that AF_UNIX bind(2) cannot be used by revoking all
2193 * filesystem access via unveil(2). Access to password database
2194 * files will still work since "getpw" bypasses unveil(2).
2196 apply_unveil_none();
2198 auth_main(title, &gotd.repos, repo_path);
2199 /* NOTREACHED */
2200 break;
2201 case PROC_SESSION_READ:
2202 case PROC_SESSION_WRITE:
2203 #ifndef PROFILE
2205 * The "recvfd" promise is only needed during setup and
2206 * will be removed in a later pledge(2) call.
2208 if (pledge("stdio rpath wpath cpath recvfd sendfd fattr flock "
2209 "unveil", NULL) == -1)
2210 err(1, "pledge");
2211 #endif
2212 if (proc_id == PROC_SESSION_READ)
2213 apply_unveil_repo_readonly(repo_path, 1);
2214 else {
2215 apply_unveil_repo_readwrite(repo_path);
2216 repo = gotd_find_repo_by_path(repo_path, &gotd);
2217 if (repo == NULL)
2218 fatalx("no repository for path %s", repo_path);
2220 session_main(title, repo_path, pack_fds, temp_fds,
2221 &gotd.request_timeout, repo, proc_id);
2222 /* NOTREACHED */
2223 break;
2224 case PROC_REPO_READ:
2225 set_max_datasize();
2226 #ifndef PROFILE
2227 if (pledge("stdio rpath recvfd unveil", NULL) == -1)
2228 err(1, "pledge");
2229 #endif
2230 apply_unveil_repo_readonly(repo_path, 0);
2231 repo_read_main(title, repo_path, pack_fds, temp_fds);
2232 /* NOTREACHED */
2233 exit(0);
2234 case PROC_REPO_WRITE:
2235 set_max_datasize();
2237 diff_f1 = got_opentemp();
2238 if (diff_f1 == NULL)
2239 fatal("got_opentemp");
2240 diff_f2 = got_opentemp();
2241 if (diff_f2 == NULL)
2242 fatal("got_opentemp");
2243 diff_fd1 = got_opentempfd();
2244 if (diff_fd1 == -1)
2245 fatal("got_opentempfd");
2246 diff_fd2 = got_opentempfd();
2247 if (diff_fd2 == -1)
2248 fatal("got_opentempfd");
2249 #ifndef PROFILE
2250 if (pledge("stdio rpath recvfd unveil", NULL) == -1)
2251 err(1, "pledge");
2252 #endif
2253 apply_unveil_repo_readonly(repo_path, 0);
2254 repo = gotd_find_repo_by_path(repo_path, &gotd);
2255 if (repo == NULL)
2256 fatalx("no repository for path %s", repo_path);
2257 repo_write_main(title, repo_path, pack_fds, temp_fds,
2258 diff_f1, diff_f2, diff_fd1, diff_fd2,
2259 &repo->protected_tag_namespaces,
2260 &repo->protected_branch_namespaces,
2261 &repo->protected_branches);
2262 /* NOTREACHED */
2263 exit(0);
2264 case PROC_NOTIFY:
2265 #ifndef PROFILE
2266 if (pledge("stdio proc exec recvfd unveil", NULL) == -1)
2267 err(1, "pledge");
2268 #endif
2270 * Limit "exec" promise to notification helpers via unveil(2).
2272 unveil_notification_helpers();
2274 notify_main(title, &gotd.repos, default_sender);
2275 /* NOTREACHED */
2276 exit(0);
2277 default:
2278 fatal("invalid process id %d", proc_id);
2281 if (proc_id != PROC_GOTD)
2282 fatal("invalid process id %d", proc_id);
2284 evtimer_set(&gotd.listen_proc->tmo, kill_proc_timeout,
2285 gotd.listen_proc);
2286 if (gotd.notify_proc) {
2287 evtimer_set(&gotd.notify_proc->tmo, kill_proc_timeout,
2288 gotd.notify_proc);
2291 apply_unveil_selfexec();
2293 signal_set(&evsigint, SIGINT, gotd_sighdlr, NULL);
2294 signal_set(&evsigterm, SIGTERM, gotd_sighdlr, NULL);
2295 signal_set(&evsighup, SIGHUP, gotd_sighdlr, NULL);
2296 signal_set(&evsigusr1, SIGUSR1, gotd_sighdlr, NULL);
2297 signal_set(&evsigchld, SIGCHLD, gotd_sighdlr, NULL);
2298 signal(SIGPIPE, SIG_IGN);
2300 signal_add(&evsigint, NULL);
2301 signal_add(&evsigterm, NULL);
2302 signal_add(&evsighup, NULL);
2303 signal_add(&evsigusr1, NULL);
2304 signal_add(&evsigchld, NULL);
2306 gotd_imsg_event_add(&gotd.listen_proc->iev);
2307 if (gotd.notify_proc)
2308 gotd_imsg_event_add(&gotd.notify_proc->iev);
2310 event_dispatch();
2312 free(repo_path);
2313 free(default_sender);
2314 gotd_shutdown();
2316 return 0;