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.client_id = client->id;
1015 ireq.proc_id = repo_proc->type;
1017 /* Pass repo child pipe to session child process. */
1018 if (gotd_imsg_compose_event(session_iev, GOTD_IMSG_CONNECT_REPO_CHILD,
1019 PROC_GOTD, pipe[0], &ireq, sizeof(ireq)) == -1) {
1020 err = got_error_from_errno("imsg compose CONNECT_REPO_CHILD");
1021 close(pipe[0]);
1022 close(pipe[1]);
1023 return err;
1026 /* Pass session child pipe to repo child process. */
1027 if (gotd_imsg_compose_event(&repo_proc->iev,
1028 GOTD_IMSG_CONNECT_REPO_CHILD, PROC_GOTD, pipe[1], NULL, 0) == -1) {
1029 err = got_error_from_errno("imsg compose CONNECT_REPO_CHILD");
1030 close(pipe[1]);
1031 return err;
1034 return NULL;
1037 static void
1038 gotd_dispatch_listener(int fd, short event, void *arg)
1040 struct gotd_imsgev *iev = arg;
1041 struct imsgbuf *ibuf = &iev->ibuf;
1042 struct gotd_child_proc *proc = gotd.listen_proc;
1043 ssize_t n;
1044 int shut = 0;
1045 struct imsg imsg;
1047 if (proc->iev.ibuf.fd != fd)
1048 fatalx("%s: unexpected fd %d", __func__, fd);
1050 if (event & EV_READ) {
1051 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1052 fatal("imsg_read error");
1053 if (n == 0) {
1054 /* Connection closed. */
1055 shut = 1;
1056 goto done;
1060 if (event & EV_WRITE) {
1061 n = msgbuf_write(&ibuf->w);
1062 if (n == -1 && errno != EAGAIN)
1063 fatal("msgbuf_write");
1064 if (n == 0) {
1065 /* Connection closed. */
1066 shut = 1;
1067 goto done;
1071 for (;;) {
1072 const struct got_error *err = NULL;
1073 struct gotd_client *client = NULL;
1074 uint32_t client_id = 0;
1075 int do_disconnect = 0;
1077 if ((n = imsg_get(ibuf, &imsg)) == -1)
1078 fatal("%s: imsg_get error", __func__);
1079 if (n == 0) /* No more messages. */
1080 break;
1082 switch (imsg.hdr.type) {
1083 case GOTD_IMSG_ERROR:
1084 do_disconnect = 1;
1085 err = gotd_imsg_recv_error(&client_id, &imsg);
1086 break;
1087 case GOTD_IMSG_CONNECT:
1088 err = recv_connect(&client_id, &imsg);
1089 break;
1090 default:
1091 log_debug("unexpected imsg %d", imsg.hdr.type);
1092 break;
1095 client = find_client(client_id);
1096 if (client == NULL) {
1097 log_warnx("%s: client not found", __func__);
1098 imsg_free(&imsg);
1099 continue;
1102 if (err)
1103 log_warnx("uid %d: %s", client->euid, err->msg);
1105 if (do_disconnect) {
1106 if (err)
1107 disconnect_on_error(client, err);
1108 else
1109 disconnect(client);
1112 imsg_free(&imsg);
1114 done:
1115 if (!shut) {
1116 gotd_imsg_event_add(iev);
1117 } else {
1118 /* This pipe is dead. Remove its event handler */
1119 event_del(&iev->ev);
1120 event_loopexit(NULL);
1124 static void
1125 gotd_dispatch_notifier(int fd, short event, void *arg)
1127 struct gotd_imsgev *iev = arg;
1128 struct imsgbuf *ibuf = &iev->ibuf;
1129 struct gotd_child_proc *proc = gotd.notify_proc;
1130 ssize_t n;
1131 int shut = 0;
1132 struct imsg imsg;
1134 if (proc->iev.ibuf.fd != fd)
1135 fatalx("%s: unexpected fd %d", __func__, fd);
1137 if (event & EV_READ) {
1138 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1139 fatal("imsg_read error");
1140 if (n == 0) {
1141 /* Connection closed. */
1142 shut = 1;
1143 goto done;
1147 if (event & EV_WRITE) {
1148 n = msgbuf_write(&ibuf->w);
1149 if (n == -1 && errno != EAGAIN)
1150 fatal("msgbuf_write");
1151 if (n == 0) {
1152 /* Connection closed. */
1153 shut = 1;
1154 goto done;
1158 for (;;) {
1159 if ((n = imsg_get(ibuf, &imsg)) == -1)
1160 fatal("%s: imsg_get error", __func__);
1161 if (n == 0) /* No more messages. */
1162 break;
1164 switch (imsg.hdr.type) {
1165 default:
1166 log_debug("unexpected imsg %d", imsg.hdr.type);
1167 break;
1170 imsg_free(&imsg);
1172 done:
1173 if (!shut) {
1174 gotd_imsg_event_add(iev);
1175 } else {
1176 /* This pipe is dead. Remove its event handler */
1177 event_del(&iev->ev);
1180 * Do not exit all of gotd if the notification handler dies.
1181 * We can continue operating without notifications until an
1182 * operator intervenes.
1184 log_warnx("notify child process (pid %d) closed its imsg pipe "
1185 "unexpectedly", proc->pid);
1186 proc_done(proc);
1190 static void
1191 gotd_dispatch_auth_child(int fd, short event, void *arg)
1193 const struct got_error *err = NULL;
1194 struct gotd_imsgev *iev = arg;
1195 struct imsgbuf *ibuf = &iev->ibuf;
1196 struct gotd_client *client;
1197 struct gotd_repo *repo = NULL;
1198 ssize_t n;
1199 int shut = 0;
1200 struct imsg imsg;
1201 uint32_t client_id = 0;
1202 int do_disconnect = 0;
1203 size_t datalen;
1205 client = find_client_by_proc_fd(fd);
1206 if (client == NULL) {
1207 /* Can happen during process teardown. */
1208 warnx("cannot find client for fd %d", fd);
1209 shut = 1;
1210 goto done;
1213 if (client->auth == NULL)
1214 fatalx("cannot find auth child process for fd %d", fd);
1216 if (event & EV_READ) {
1217 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1218 fatal("imsg_read error");
1219 if (n == 0) {
1220 /* Connection closed. */
1221 shut = 1;
1222 goto done;
1226 if (event & EV_WRITE) {
1227 n = msgbuf_write(&ibuf->w);
1228 if (n == -1 && errno != EAGAIN)
1229 fatal("msgbuf_write");
1230 if (n == 0) {
1231 /* Connection closed. */
1232 shut = 1;
1234 goto done;
1237 if (client->auth->iev.ibuf.fd != fd)
1238 fatalx("%s: unexpected fd %d", __func__, fd);
1240 if ((n = imsg_get(ibuf, &imsg)) == -1)
1241 fatal("%s: imsg_get error", __func__);
1242 if (n == 0) /* No more messages. */
1243 return;
1245 evtimer_del(&client->tmo);
1247 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1249 switch (imsg.hdr.type) {
1250 case GOTD_IMSG_ERROR:
1251 do_disconnect = 1;
1252 err = gotd_imsg_recv_error(&client_id, &imsg);
1253 break;
1254 case GOTD_IMSG_ACCESS_GRANTED:
1255 if (client->state != GOTD_CLIENT_STATE_NEW) {
1256 do_disconnect = 1;
1257 err = got_error(GOT_ERR_PRIVSEP_MSG);
1259 break;
1260 default:
1261 do_disconnect = 1;
1262 log_debug("unexpected imsg %d", imsg.hdr.type);
1263 break;
1266 if (!verify_imsg_src(client, client->auth, &imsg)) {
1267 do_disconnect = 1;
1268 log_debug("dropping imsg type %d from PID %d",
1269 imsg.hdr.type, client->auth->pid);
1272 if (do_disconnect) {
1273 if (err)
1274 disconnect_on_error(client, err);
1275 else
1276 disconnect(client);
1277 imsg_free(&imsg);
1278 return;
1281 client->state = GOTD_CLIENT_STATE_ACCESS_GRANTED;
1282 if (datalen > 0)
1283 client->username = strndup(imsg.data, datalen);
1284 imsg_free(&imsg);
1285 if (client->username == NULL &&
1286 asprintf(&client->username, "uid %d", client->euid) == -1) {
1287 err = got_error_from_errno("asprintf");
1288 goto done;
1291 repo = gotd_find_repo_by_name(client->auth->repo_name, &gotd.repos);
1292 if (repo == NULL) {
1293 err = got_error(GOT_ERR_NOT_GIT_REPO);
1294 goto done;
1296 kill_auth_proc(client);
1298 log_info("authenticated %s for repository %s",
1299 client->username, repo->name);
1301 err = start_session_child(client, repo, gotd.argv0,
1302 gotd.confpath, gotd.daemonize, gotd.verbosity);
1303 if (err)
1304 goto done;
1305 done:
1306 if (err)
1307 log_warnx("uid %d: %s", client->euid, err->msg);
1309 /* We might have killed the auth process by now. */
1310 if (client->auth != NULL) {
1311 if (!shut) {
1312 gotd_imsg_event_add(iev);
1313 } else {
1314 /* This pipe is dead. Remove its event handler */
1315 event_del(&iev->ev);
1320 static const struct got_error *
1321 connect_session(struct gotd_client *client)
1323 const struct got_error *err = NULL;
1324 struct gotd_imsg_connect iconnect;
1325 int s;
1326 struct ibuf *wbuf;
1328 memset(&iconnect, 0, sizeof(iconnect));
1330 s = dup(client->fd);
1331 if (s == -1)
1332 return got_error_from_errno("dup");
1334 iconnect.client_id = client->id;
1335 iconnect.euid = client->euid;
1336 iconnect.egid = client->egid;
1337 iconnect.username_len = strlen(client->username);
1339 wbuf = imsg_create(&client->session->iev.ibuf, GOTD_IMSG_CONNECT,
1340 PROC_GOTD, gotd.pid, sizeof(iconnect) + iconnect.username_len);
1341 if (wbuf == NULL) {
1342 err = got_error_from_errno("imsg compose CONNECT");
1343 close(s);
1344 return err;
1346 if (imsg_add(wbuf, &iconnect, sizeof(iconnect)) == -1) {
1347 close(s);
1348 return got_error_from_errno("imsg_add CONNECT");
1350 if (imsg_add(wbuf, client->username, iconnect.username_len) == -1) {
1351 close(s);
1352 return got_error_from_errno("imsg_add CONNECT");
1355 ibuf_fd_set(wbuf, s);
1356 imsg_close(&client->session->iev.ibuf, wbuf);
1357 gotd_imsg_event_add(&client->session->iev);
1360 * We are no longer interested in messages from this client.
1361 * Further client requests will be handled by the session process.
1363 msgbuf_clear(&client->iev.ibuf.w);
1364 imsg_clear(&client->iev.ibuf);
1365 event_del(&client->iev.ev);
1366 client->fd = -1; /* will be closed via copy in client->iev.ibuf.fd */
1368 return NULL;
1371 static void
1372 gotd_dispatch_client_session(int fd, short event, void *arg)
1374 struct gotd_imsgev *iev = arg;
1375 struct imsgbuf *ibuf = &iev->ibuf;
1376 struct gotd_child_proc *proc = NULL;
1377 struct gotd_client *client = NULL;
1378 ssize_t n;
1379 int shut = 0;
1380 struct imsg imsg;
1382 client = find_client_by_proc_fd(fd);
1383 if (client == NULL) {
1384 /* Can happen during process teardown. */
1385 warnx("cannot find client for fd %d", fd);
1386 shut = 1;
1387 goto done;
1390 if (event & EV_READ) {
1391 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1392 fatal("imsg_read error");
1393 if (n == 0) {
1394 /* Connection closed. */
1395 shut = 1;
1396 goto done;
1400 if (event & EV_WRITE) {
1401 n = msgbuf_write(&ibuf->w);
1402 if (n == -1 && errno != EAGAIN)
1403 fatal("msgbuf_write");
1404 if (n == 0) {
1405 /* Connection closed. */
1406 shut = 1;
1407 goto done;
1411 proc = client->session;
1412 if (proc == NULL)
1413 fatalx("cannot find session child process for fd %d", fd);
1415 for (;;) {
1416 const struct got_error *err = NULL;
1417 uint32_t client_id = 0;
1418 int do_disconnect = 0, do_start_repo_child = 0;
1420 if ((n = imsg_get(ibuf, &imsg)) == -1)
1421 fatal("%s: imsg_get error", __func__);
1422 if (n == 0) /* No more messages. */
1423 break;
1425 switch (imsg.hdr.type) {
1426 case GOTD_IMSG_ERROR:
1427 do_disconnect = 1;
1428 err = gotd_imsg_recv_error(&client_id, &imsg);
1429 break;
1430 case GOTD_IMSG_CLIENT_SESSION_READY:
1431 if (client->state != GOTD_CLIENT_STATE_ACCESS_GRANTED) {
1432 err = got_error(GOT_ERR_PRIVSEP_MSG);
1433 break;
1435 do_start_repo_child = 1;
1436 break;
1437 case GOTD_IMSG_DISCONNECT:
1438 do_disconnect = 1;
1439 break;
1440 default:
1441 log_debug("unexpected imsg %d", imsg.hdr.type);
1442 break;
1445 if (!verify_imsg_src(client, proc, &imsg)) {
1446 log_debug("dropping imsg type %d from PID %d",
1447 imsg.hdr.type, proc->pid);
1448 imsg_free(&imsg);
1449 continue;
1451 if (err)
1452 log_warnx("uid %d: %s", client->euid, err->msg);
1454 if (do_start_repo_child) {
1455 struct gotd_repo *repo;
1456 const char *name = client->session->repo_name;
1458 repo = gotd_find_repo_by_name(name, &gotd.repos);
1459 if (repo != NULL) {
1460 enum gotd_procid proc_type;
1462 if (client->required_auth & GOTD_AUTH_WRITE)
1463 proc_type = PROC_REPO_WRITE;
1464 else
1465 proc_type = PROC_REPO_READ;
1467 err = start_repo_child(client, proc_type, repo,
1468 gotd.argv0, gotd.confpath, gotd.daemonize,
1469 gotd.verbosity);
1470 } else
1471 err = got_error(GOT_ERR_NOT_GIT_REPO);
1473 if (err) {
1474 log_warnx("uid %d: %s", client->euid, err->msg);
1475 do_disconnect = 1;
1479 if (do_disconnect) {
1480 if (err)
1481 disconnect_on_error(client, err);
1482 else
1483 disconnect(client);
1486 imsg_free(&imsg);
1488 done:
1489 if (!shut) {
1490 gotd_imsg_event_add(iev);
1491 } else {
1492 /* This pipe is dead. Remove its event handler */
1493 event_del(&iev->ev);
1494 disconnect(client);
1498 static const struct got_error *
1499 connect_notifier_and_session(struct gotd_client *client)
1501 const struct got_error *err = NULL;
1502 struct gotd_imsgev *session_iev = &client->session->iev;
1503 int pipe[2];
1505 if (gotd.notify_proc == NULL)
1506 return NULL;
1508 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1509 PF_UNSPEC, pipe) == -1)
1510 return got_error_from_errno("socketpair");
1512 /* Pass notifier pipe to session . */
1513 if (gotd_imsg_compose_event(session_iev, GOTD_IMSG_CONNECT_NOTIFIER,
1514 PROC_GOTD, pipe[0], NULL, 0) == -1) {
1515 err = got_error_from_errno("imsg compose CONNECT_NOTIFIER");
1516 close(pipe[0]);
1517 close(pipe[1]);
1518 return err;
1521 /* Pass session pipe to notifier. */
1522 if (gotd_imsg_compose_event(&gotd.notify_proc->iev,
1523 GOTD_IMSG_CONNECT_SESSION, PROC_GOTD, pipe[1], NULL, 0) == -1) {
1524 err = got_error_from_errno("imsg compose CONNECT_SESSION");
1525 close(pipe[1]);
1526 return err;
1529 return NULL;
1532 static void
1533 gotd_dispatch_repo_child(int fd, short event, void *arg)
1535 struct gotd_imsgev *iev = arg;
1536 struct imsgbuf *ibuf = &iev->ibuf;
1537 struct gotd_child_proc *proc = NULL;
1538 struct gotd_client *client;
1539 ssize_t n;
1540 int shut = 0;
1541 struct imsg imsg;
1543 client = find_client_by_proc_fd(fd);
1544 if (client == NULL) {
1545 /* Can happen during process teardown. */
1546 warnx("cannot find client for fd %d", fd);
1547 shut = 1;
1548 goto done;
1551 if (event & EV_READ) {
1552 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1553 fatal("imsg_read error");
1554 if (n == 0) {
1555 /* Connection closed. */
1556 shut = 1;
1557 goto done;
1561 if (event & EV_WRITE) {
1562 n = msgbuf_write(&ibuf->w);
1563 if (n == -1 && errno != EAGAIN)
1564 fatal("msgbuf_write");
1565 if (n == 0) {
1566 /* Connection closed. */
1567 shut = 1;
1568 goto done;
1572 proc = client->repo;
1573 if (proc == NULL)
1574 fatalx("cannot find child process for fd %d", fd);
1576 for (;;) {
1577 const struct got_error *err = NULL;
1578 uint32_t client_id = 0;
1579 int do_disconnect = 0;
1581 if ((n = imsg_get(ibuf, &imsg)) == -1)
1582 fatal("%s: imsg_get error", __func__);
1583 if (n == 0) /* No more messages. */
1584 break;
1586 switch (imsg.hdr.type) {
1587 case GOTD_IMSG_ERROR:
1588 do_disconnect = 1;
1589 err = gotd_imsg_recv_error(&client_id, &imsg);
1590 break;
1591 case GOTD_IMSG_REPO_CHILD_READY:
1592 err = connect_session(client);
1593 if (err)
1594 break;
1595 err = connect_notifier_and_session(client);
1596 if (err)
1597 break;
1598 err = connect_repo_child(client, proc);
1599 break;
1600 default:
1601 log_debug("unexpected imsg %d", imsg.hdr.type);
1602 break;
1605 if (!verify_imsg_src(client, proc, &imsg)) {
1606 log_debug("dropping imsg type %d from PID %d",
1607 imsg.hdr.type, proc->pid);
1608 imsg_free(&imsg);
1609 continue;
1611 if (err)
1612 log_warnx("uid %d: %s", client->euid, err->msg);
1614 if (do_disconnect) {
1615 if (err)
1616 disconnect_on_error(client, err);
1617 else
1618 disconnect(client);
1621 imsg_free(&imsg);
1623 done:
1624 if (!shut) {
1625 gotd_imsg_event_add(iev);
1626 } else {
1627 /* This pipe is dead. Remove its event handler */
1628 event_del(&iev->ev);
1629 disconnect(client);
1633 static pid_t
1634 start_child(enum gotd_procid proc_id, const char *repo_path,
1635 char *argv0, const char *confpath, int fd, int daemonize, int verbosity)
1637 char *argv[11];
1638 int argc = 0;
1639 pid_t pid;
1641 switch (pid = fork()) {
1642 case -1:
1643 fatal("cannot fork");
1644 case 0:
1645 break;
1646 default:
1647 close(fd);
1648 return pid;
1651 if (fd != GOTD_FILENO_MSG_PIPE) {
1652 if (dup2(fd, GOTD_FILENO_MSG_PIPE) == -1)
1653 fatal("cannot setup imsg fd");
1654 } else if (fcntl(fd, F_SETFD, 0) == -1)
1655 fatal("cannot setup imsg fd");
1657 argv[argc++] = argv0;
1658 switch (proc_id) {
1659 case PROC_LISTEN:
1660 argv[argc++] = (char *)"-L";
1661 break;
1662 case PROC_AUTH:
1663 argv[argc++] = (char *)"-A";
1664 break;
1665 case PROC_SESSION_READ:
1666 argv[argc++] = (char *)"-s";
1667 break;
1668 case PROC_SESSION_WRITE:
1669 argv[argc++] = (char *)"-S";
1670 break;
1671 case PROC_REPO_READ:
1672 argv[argc++] = (char *)"-R";
1673 break;
1674 case PROC_REPO_WRITE:
1675 argv[argc++] = (char *)"-W";
1676 break;
1677 case PROC_NOTIFY:
1678 argv[argc++] = (char *)"-N";
1679 break;
1680 default:
1681 fatalx("invalid process id %d", proc_id);
1684 argv[argc++] = (char *)"-f";
1685 argv[argc++] = (char *)confpath;
1687 if (repo_path) {
1688 argv[argc++] = (char *)"-P";
1689 argv[argc++] = (char *)repo_path;
1692 if (!daemonize)
1693 argv[argc++] = (char *)"-d";
1694 if (verbosity > 0)
1695 argv[argc++] = (char *)"-v";
1696 if (verbosity > 1)
1697 argv[argc++] = (char *)"-v";
1698 argv[argc++] = NULL;
1700 execvp(argv0, argv);
1701 fatal("execvp");
1704 static void
1705 start_listener(char *argv0, const char *confpath, int daemonize, int verbosity)
1707 struct gotd_child_proc *proc;
1709 proc = calloc(1, sizeof(*proc));
1710 if (proc == NULL)
1711 fatal("calloc");
1713 TAILQ_INSERT_HEAD(&procs, proc, entry);
1715 /* proc->tmo is initialized in main() after event_init() */
1717 proc->type = PROC_LISTEN;
1719 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1720 PF_UNSPEC, proc->pipe) == -1)
1721 fatal("socketpair");
1723 proc->pid = start_child(proc->type, NULL, argv0, confpath,
1724 proc->pipe[1], daemonize, verbosity);
1725 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1726 proc->iev.handler = gotd_dispatch_listener;
1727 proc->iev.events = EV_READ;
1728 proc->iev.handler_arg = NULL;
1730 gotd.listen_proc = proc;
1733 static void
1734 start_notifier(char *argv0, const char *confpath, int daemonize, int verbosity)
1736 struct gotd_child_proc *proc;
1738 proc = calloc(1, sizeof(*proc));
1739 if (proc == NULL)
1740 fatal("calloc");
1742 TAILQ_INSERT_HEAD(&procs, proc, entry);
1744 /* proc->tmo is initialized in main() after event_init() */
1746 proc->type = PROC_NOTIFY;
1748 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1749 PF_UNSPEC, proc->pipe) == -1)
1750 fatal("socketpair");
1752 proc->pid = start_child(proc->type, NULL, argv0, confpath,
1753 proc->pipe[1], daemonize, verbosity);
1754 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1755 proc->iev.handler = gotd_dispatch_notifier;
1756 proc->iev.events = EV_READ;
1757 proc->iev.handler_arg = NULL;
1758 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1759 gotd_dispatch_notifier, &proc->iev);
1761 gotd.notify_proc = proc;
1764 static const struct got_error *
1765 start_session_child(struct gotd_client *client, struct gotd_repo *repo,
1766 char *argv0, const char *confpath, int daemonize, int verbosity)
1768 struct gotd_child_proc *proc;
1770 proc = calloc(1, sizeof(*proc));
1771 if (proc == NULL)
1772 return got_error_from_errno("calloc");
1774 TAILQ_INSERT_HEAD(&procs, proc, entry);
1775 evtimer_set(&proc->tmo, kill_proc_timeout, proc);
1777 if (client_is_reading(client))
1778 proc->type = PROC_SESSION_READ;
1779 else
1780 proc->type = PROC_SESSION_WRITE;
1781 if (strlcpy(proc->repo_name, repo->name,
1782 sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
1783 fatalx("repository name too long: %s", repo->name);
1784 log_debug("starting client uid %d session for repository %s",
1785 client->euid, repo->name);
1786 if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >=
1787 sizeof(proc->repo_path))
1788 fatalx("repository path too long: %s", repo->path);
1789 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1790 PF_UNSPEC, proc->pipe) == -1)
1791 fatal("socketpair");
1792 proc->pid = start_child(proc->type, proc->repo_path, argv0,
1793 confpath, proc->pipe[1], daemonize, verbosity);
1794 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1795 log_debug("proc %s %s is on fd %d",
1796 gotd_proc_names[proc->type], proc->repo_path,
1797 proc->pipe[0]);
1798 proc->iev.handler = gotd_dispatch_client_session;
1799 proc->iev.events = EV_READ;
1800 proc->iev.handler_arg = NULL;
1801 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1802 gotd_dispatch_client_session, &proc->iev);
1803 gotd_imsg_event_add(&proc->iev);
1805 client->session = proc;
1806 return NULL;
1809 static const struct got_error *
1810 start_repo_child(struct gotd_client *client, enum gotd_procid proc_type,
1811 struct gotd_repo *repo, char *argv0, const char *confpath,
1812 int daemonize, int verbosity)
1814 struct gotd_child_proc *proc;
1816 if (proc_type != PROC_REPO_READ && proc_type != PROC_REPO_WRITE)
1817 return got_error_msg(GOT_ERR_NOT_IMPL, "bad process type");
1819 proc = calloc(1, sizeof(*proc));
1820 if (proc == NULL)
1821 return got_error_from_errno("calloc");
1823 TAILQ_INSERT_HEAD(&procs, proc, entry);
1824 evtimer_set(&proc->tmo, kill_proc_timeout, proc);
1826 proc->type = proc_type;
1827 if (strlcpy(proc->repo_name, repo->name,
1828 sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
1829 fatalx("repository name too long: %s", repo->name);
1830 log_debug("starting %s for repository %s",
1831 proc->type == PROC_REPO_READ ? "reader" : "writer", repo->name);
1832 if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >=
1833 sizeof(proc->repo_path))
1834 fatalx("repository path too long: %s", repo->path);
1835 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1836 PF_UNSPEC, proc->pipe) == -1)
1837 fatal("socketpair");
1838 proc->pid = start_child(proc->type, proc->repo_path, argv0,
1839 confpath, proc->pipe[1], daemonize, verbosity);
1840 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1841 log_debug("proc %s %s is on fd %d",
1842 gotd_proc_names[proc->type], proc->repo_path,
1843 proc->pipe[0]);
1844 proc->iev.handler = gotd_dispatch_repo_child;
1845 proc->iev.events = EV_READ;
1846 proc->iev.handler_arg = NULL;
1847 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1848 gotd_dispatch_repo_child, &proc->iev);
1849 gotd_imsg_event_add(&proc->iev);
1851 client->repo = proc;
1852 return NULL;
1855 static const struct got_error *
1856 start_auth_child(struct gotd_client *client, int required_auth,
1857 struct gotd_repo *repo, char *argv0, const char *confpath,
1858 int daemonize, int verbosity)
1860 const struct got_error *err = NULL;
1861 struct gotd_child_proc *proc;
1862 struct gotd_imsg_auth iauth;
1863 int fd;
1865 memset(&iauth, 0, sizeof(iauth));
1867 fd = dup(client->fd);
1868 if (fd == -1)
1869 return got_error_from_errno("dup");
1871 proc = calloc(1, sizeof(*proc));
1872 if (proc == NULL) {
1873 err = got_error_from_errno("calloc");
1874 close(fd);
1875 return err;
1878 TAILQ_INSERT_HEAD(&procs, proc, entry);
1879 evtimer_set(&proc->tmo, kill_proc_timeout, proc);
1881 proc->type = PROC_AUTH;
1882 if (strlcpy(proc->repo_name, repo->name,
1883 sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
1884 fatalx("repository name too long: %s", repo->name);
1885 log_debug("starting auth for uid %d repository %s",
1886 client->euid, repo->name);
1887 if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >=
1888 sizeof(proc->repo_path))
1889 fatalx("repository path too long: %s", repo->path);
1890 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1891 PF_UNSPEC, proc->pipe) == -1)
1892 fatal("socketpair");
1893 proc->pid = start_child(proc->type, proc->repo_path, argv0,
1894 confpath, proc->pipe[1], daemonize, verbosity);
1895 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1896 log_debug("proc %s %s is on fd %d",
1897 gotd_proc_names[proc->type], proc->repo_path,
1898 proc->pipe[0]);
1899 proc->iev.handler = gotd_dispatch_auth_child;
1900 proc->iev.events = EV_READ;
1901 proc->iev.handler_arg = NULL;
1902 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1903 gotd_dispatch_auth_child, &proc->iev);
1904 gotd_imsg_event_add(&proc->iev);
1906 iauth.euid = client->euid;
1907 iauth.egid = client->egid;
1908 iauth.required_auth = required_auth;
1909 iauth.client_id = client->id;
1910 if (gotd_imsg_compose_event(&proc->iev, GOTD_IMSG_AUTHENTICATE,
1911 PROC_GOTD, fd, &iauth, sizeof(iauth)) == -1) {
1912 log_warn("imsg compose AUTHENTICATE");
1913 close(fd);
1914 /* Let the auth_timeout handler tidy up. */
1917 client->auth = proc;
1918 client->required_auth = required_auth;
1919 return NULL;
1922 static void
1923 apply_unveil_repo_readonly(const char *repo_path, int need_tmpdir)
1925 if (need_tmpdir) {
1926 if (unveil(GOT_TMPDIR_STR, "rwc") == -1)
1927 fatal("unveil %s", GOT_TMPDIR_STR);
1930 if (unveil(repo_path, "r") == -1)
1931 fatal("unveil %s", repo_path);
1933 if (unveil(NULL, NULL) == -1)
1934 fatal("unveil");
1937 static void
1938 apply_unveil_repo_readwrite(const char *repo_path)
1940 if (unveil(repo_path, "rwc") == -1)
1941 fatal("unveil %s", repo_path);
1943 if (unveil(GOT_TMPDIR_STR, "rwc") == -1)
1944 fatal("unveil %s", GOT_TMPDIR_STR);
1946 if (unveil(NULL, NULL) == -1)
1947 fatal("unveil");
1950 static void
1951 apply_unveil_none(void)
1953 if (unveil("/", "") == -1)
1954 fatal("unveil");
1956 if (unveil(NULL, NULL) == -1)
1957 fatal("unveil");
1960 static void
1961 apply_unveil_selfexec(void)
1963 if (unveil(gotd.argv0, "x") == -1)
1964 fatal("unveil %s", gotd.argv0);
1966 if (unveil(NULL, NULL) == -1)
1967 fatal("unveil");
1970 static void
1971 set_max_datasize(void)
1973 struct rlimit rl;
1975 if (getrlimit(RLIMIT_DATA, &rl) != 0)
1976 return;
1978 rl.rlim_cur = rl.rlim_max;
1979 setrlimit(RLIMIT_DATA, &rl);
1982 static void
1983 unveil_notification_helpers(void)
1985 const char *helpers[] = {
1986 GOTD_PATH_PROG_NOTIFY_EMAIL,
1987 GOTD_PATH_PROG_NOTIFY_HTTP,
1989 size_t i;
1991 for (i = 0; i < nitems(helpers); i++) {
1992 if (unveil(helpers[i], "x") == 0)
1993 continue;
1994 fatal("unveil %s", helpers[i]);
1997 if (unveil(NULL, NULL) == -1)
1998 fatal("unveil");
2001 int
2002 main(int argc, char **argv)
2004 const struct got_error *error = NULL;
2005 int ch, fd = -1, daemonize = 1, verbosity = 0, noaction = 0;
2006 const char *confpath = GOTD_CONF_PATH;
2007 char *argv0 = argv[0];
2008 char title[2048];
2009 struct passwd *pw = NULL;
2010 char *repo_path = NULL;
2011 enum gotd_procid proc_id = PROC_GOTD;
2012 struct event evsigint, evsigterm, evsighup, evsigusr1, evsigchld;
2013 int *pack_fds = NULL, *temp_fds = NULL;
2014 struct gotd_repo *repo = NULL;
2015 char *default_sender = NULL;
2016 char hostname[HOST_NAME_MAX + 1];
2017 FILE *diff_f1 = NULL, *diff_f2 = NULL;
2018 int diff_fd1 = -1, diff_fd2 = -1;
2020 TAILQ_INIT(&procs);
2022 log_init(1, LOG_DAEMON); /* Log to stderr until daemonized. */
2024 while ((ch = getopt(argc, argv, "Adf:LnNP:RsSvW")) != -1) {
2025 switch (ch) {
2026 case 'A':
2027 proc_id = PROC_AUTH;
2028 break;
2029 case 'd':
2030 daemonize = 0;
2031 break;
2032 case 'f':
2033 confpath = optarg;
2034 break;
2035 case 'L':
2036 proc_id = PROC_LISTEN;
2037 break;
2038 case 'n':
2039 noaction = 1;
2040 break;
2041 case 'N':
2042 proc_id = PROC_NOTIFY;
2043 break;
2044 case 'P':
2045 repo_path = realpath(optarg, NULL);
2046 if (repo_path == NULL)
2047 fatal("realpath '%s'", optarg);
2048 break;
2049 case 'R':
2050 proc_id = PROC_REPO_READ;
2051 break;
2052 case 's':
2053 proc_id = PROC_SESSION_READ;
2054 break;
2055 case 'S':
2056 proc_id = PROC_SESSION_WRITE;
2057 break;
2058 case 'v':
2059 if (verbosity < 3)
2060 verbosity++;
2061 break;
2062 case 'W':
2063 proc_id = PROC_REPO_WRITE;
2064 break;
2065 default:
2066 usage();
2070 argc -= optind;
2071 argv += optind;
2073 if (argc != 0)
2074 usage();
2076 if (geteuid() && (proc_id == PROC_GOTD || proc_id == PROC_LISTEN))
2077 fatalx("need root privileges");
2079 if (parse_config(confpath, proc_id, &gotd) != 0)
2080 return 1;
2082 pw = getpwnam(gotd.user_name);
2083 if (pw == NULL)
2084 fatalx("user %s not found", gotd.user_name);
2086 if (pw->pw_uid == 0)
2087 fatalx("cannot run %s as the superuser", getprogname());
2089 if (noaction) {
2090 fprintf(stderr, "configuration OK\n");
2091 return 0;
2094 gotd.argv0 = argv0;
2095 gotd.daemonize = daemonize;
2096 gotd.verbosity = verbosity;
2097 gotd.confpath = confpath;
2099 /* Require an absolute path in argv[0] for reliable re-exec. */
2100 if (!got_path_is_absolute(argv0))
2101 fatalx("bad path \"%s\": must be an absolute path", argv0);
2103 log_init(daemonize ? 0 : 1, LOG_DAEMON);
2104 log_setverbose(verbosity);
2106 if (proc_id == PROC_GOTD) {
2107 snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
2108 arc4random_buf(&clients_hash_key, sizeof(clients_hash_key));
2109 if (daemonize && daemon(1, 0) == -1)
2110 fatal("daemon");
2111 gotd.pid = getpid();
2112 start_listener(argv0, confpath, daemonize, verbosity);
2113 start_notifier(argv0, confpath, daemonize, verbosity);
2114 } else if (proc_id == PROC_LISTEN) {
2115 snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
2116 if (verbosity) {
2117 log_info("socket: %s", gotd.unix_socket_path);
2118 log_info("user: %s", pw->pw_name);
2121 fd = unix_socket_listen(gotd.unix_socket_path, pw->pw_uid,
2122 pw->pw_gid);
2123 if (fd == -1) {
2124 fatal("cannot listen on unix socket %s",
2125 gotd.unix_socket_path);
2127 } else if (proc_id == PROC_AUTH) {
2128 snprintf(title, sizeof(title), "%s %s",
2129 gotd_proc_names[proc_id], repo_path);
2130 } else if (proc_id == PROC_REPO_READ || proc_id == PROC_REPO_WRITE ||
2131 proc_id == PROC_SESSION_READ || proc_id == PROC_SESSION_WRITE) {
2132 error = got_repo_pack_fds_open(&pack_fds);
2133 if (error != NULL)
2134 fatalx("cannot open pack tempfiles: %s", error->msg);
2135 error = got_repo_temp_fds_open(&temp_fds);
2136 if (error != NULL)
2137 fatalx("cannot open pack tempfiles: %s", error->msg);
2138 if (repo_path == NULL)
2139 fatalx("repository path not specified");
2140 snprintf(title, sizeof(title), "%s %s",
2141 gotd_proc_names[proc_id], repo_path);
2142 } else if (proc_id == PROC_NOTIFY) {
2143 snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
2144 if (gethostname(hostname, sizeof(hostname)) == -1)
2145 fatal("gethostname");
2146 if (asprintf(&default_sender, "%s@%s",
2147 pw->pw_name, hostname) == -1)
2148 fatal("asprintf");
2149 } else
2150 fatal("invalid process id %d", proc_id);
2152 setproctitle("%s", title);
2153 log_procinit(title);
2155 /* Drop root privileges. */
2156 if (setgid(pw->pw_gid) == -1)
2157 fatal("setgid %d failed", pw->pw_gid);
2158 if (setuid(pw->pw_uid) == -1)
2159 fatal("setuid %d failed", pw->pw_uid);
2161 event_init();
2163 switch (proc_id) {
2164 case PROC_GOTD:
2165 #ifndef PROFILE
2166 /* "exec" promise will be limited to argv[0] via unveil(2). */
2167 if (pledge("stdio proc exec sendfd recvfd unveil", NULL) == -1)
2168 err(1, "pledge");
2169 #endif
2170 break;
2171 case PROC_LISTEN:
2172 #ifndef PROFILE
2173 if (pledge("stdio sendfd unix unveil", NULL) == -1)
2174 err(1, "pledge");
2175 #endif
2177 * Ensure that AF_UNIX bind(2) cannot be used with any other
2178 * sockets by revoking all filesystem access via unveil(2).
2180 apply_unveil_none();
2182 listen_main(title, fd, gotd.connection_limits,
2183 gotd.nconnection_limits);
2184 /* NOTREACHED */
2185 break;
2186 case PROC_AUTH:
2187 #ifndef PROFILE
2188 if (pledge("stdio getpw recvfd unix unveil", NULL) == -1)
2189 err(1, "pledge");
2190 #endif
2192 * We need the "unix" pledge promise for getpeername(2) only.
2193 * Ensure that AF_UNIX bind(2) cannot be used by revoking all
2194 * filesystem access via unveil(2). Access to password database
2195 * files will still work since "getpw" bypasses unveil(2).
2197 apply_unveil_none();
2199 auth_main(title, &gotd.repos, repo_path);
2200 /* NOTREACHED */
2201 break;
2202 case PROC_SESSION_READ:
2203 case PROC_SESSION_WRITE:
2204 #ifndef PROFILE
2206 * The "recvfd" promise is only needed during setup and
2207 * will be removed in a later pledge(2) call.
2209 if (pledge("stdio rpath wpath cpath recvfd sendfd fattr flock "
2210 "unveil", NULL) == -1)
2211 err(1, "pledge");
2212 #endif
2213 if (proc_id == PROC_SESSION_READ)
2214 apply_unveil_repo_readonly(repo_path, 1);
2215 else {
2216 apply_unveil_repo_readwrite(repo_path);
2217 repo = gotd_find_repo_by_path(repo_path, &gotd);
2218 if (repo == NULL)
2219 fatalx("no repository for path %s", repo_path);
2221 session_main(title, repo_path, pack_fds, temp_fds,
2222 &gotd.request_timeout, repo, proc_id);
2223 /* NOTREACHED */
2224 break;
2225 case PROC_REPO_READ:
2226 set_max_datasize();
2227 #ifndef PROFILE
2228 if (pledge("stdio rpath recvfd unveil", NULL) == -1)
2229 err(1, "pledge");
2230 #endif
2231 apply_unveil_repo_readonly(repo_path, 0);
2232 repo_read_main(title, repo_path, pack_fds, temp_fds);
2233 /* NOTREACHED */
2234 exit(0);
2235 case PROC_REPO_WRITE:
2236 set_max_datasize();
2238 diff_f1 = got_opentemp();
2239 if (diff_f1 == NULL)
2240 fatal("got_opentemp");
2241 diff_f2 = got_opentemp();
2242 if (diff_f2 == NULL)
2243 fatal("got_opentemp");
2244 diff_fd1 = got_opentempfd();
2245 if (diff_fd1 == -1)
2246 fatal("got_opentempfd");
2247 diff_fd2 = got_opentempfd();
2248 if (diff_fd2 == -1)
2249 fatal("got_opentempfd");
2250 #ifndef PROFILE
2251 if (pledge("stdio rpath recvfd unveil", NULL) == -1)
2252 err(1, "pledge");
2253 #endif
2254 apply_unveil_repo_readonly(repo_path, 0);
2255 repo = gotd_find_repo_by_path(repo_path, &gotd);
2256 if (repo == NULL)
2257 fatalx("no repository for path %s", repo_path);
2258 repo_write_main(title, repo_path, pack_fds, temp_fds,
2259 diff_f1, diff_f2, diff_fd1, diff_fd2,
2260 &repo->protected_tag_namespaces,
2261 &repo->protected_branch_namespaces,
2262 &repo->protected_branches);
2263 /* NOTREACHED */
2264 exit(0);
2265 case PROC_NOTIFY:
2266 #ifndef PROFILE
2267 if (pledge("stdio proc exec recvfd unveil", NULL) == -1)
2268 err(1, "pledge");
2269 #endif
2271 * Limit "exec" promise to notification helpers via unveil(2).
2273 unveil_notification_helpers();
2275 notify_main(title, &gotd.repos, default_sender);
2276 /* NOTREACHED */
2277 exit(0);
2278 default:
2279 fatal("invalid process id %d", proc_id);
2282 if (proc_id != PROC_GOTD)
2283 fatal("invalid process id %d", proc_id);
2285 evtimer_set(&gotd.listen_proc->tmo, kill_proc_timeout,
2286 gotd.listen_proc);
2287 if (gotd.notify_proc) {
2288 evtimer_set(&gotd.notify_proc->tmo, kill_proc_timeout,
2289 gotd.notify_proc);
2292 apply_unveil_selfexec();
2294 signal_set(&evsigint, SIGINT, gotd_sighdlr, NULL);
2295 signal_set(&evsigterm, SIGTERM, gotd_sighdlr, NULL);
2296 signal_set(&evsighup, SIGHUP, gotd_sighdlr, NULL);
2297 signal_set(&evsigusr1, SIGUSR1, gotd_sighdlr, NULL);
2298 signal_set(&evsigchld, SIGCHLD, gotd_sighdlr, NULL);
2299 signal(SIGPIPE, SIG_IGN);
2301 signal_add(&evsigint, NULL);
2302 signal_add(&evsigterm, NULL);
2303 signal_add(&evsighup, NULL);
2304 signal_add(&evsigusr1, NULL);
2305 signal_add(&evsigchld, NULL);
2307 gotd_imsg_event_add(&gotd.listen_proc->iev);
2308 if (gotd.notify_proc)
2309 gotd_imsg_event_add(&gotd.notify_proc->iev);
2311 event_dispatch();
2313 free(repo_path);
2314 free(default_sender);
2315 gotd_shutdown();
2317 return 0;