2 * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
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.
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.
17 #include <sys/queue.h>
20 #include <sys/types.h>
22 #include <sys/socket.h>
44 #include "got_error.h"
45 #include "got_opentemp.h"
47 #include "got_repository.h"
48 #include "got_object.h"
49 #include "got_reference.h"
51 #include "got_lib_delta.h"
52 #include "got_lib_object.h"
53 #include "got_lib_object_cache.h"
54 #include "got_lib_hash.h"
55 #include "got_lib_gitproto.h"
56 #include "got_lib_pack.h"
57 #include "got_lib_repository.h"
64 #include "repo_read.h"
65 #include "repo_write.h"
68 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
71 enum gotd_client_state {
72 GOTD_CLIENT_STATE_NEW,
73 GOTD_CLIENT_STATE_ACCESS_GRANTED,
77 STAILQ_ENTRY(gotd_client) entry;
78 enum gotd_client_state state;
81 struct gotd_imsgev iev;
85 struct gotd_child_proc *repo;
86 struct gotd_child_proc *auth;
87 struct gotd_child_proc *session;
90 STAILQ_HEAD(gotd_clients, gotd_client);
92 static struct gotd_clients gotd_clients[GOTD_CLIENT_TABLE_SIZE];
93 static SIPHASH_KEY clients_hash_key;
94 volatile int client_cnt;
95 static struct timeval auth_timeout = { 5, 0 };
96 static struct gotd gotd;
98 void gotd_sighdlr(int sig, short event, void *arg);
99 static void gotd_shutdown(void);
100 static const struct got_error *start_session_child(struct gotd_client *,
101 struct gotd_repo *, char *, const char *, int, int);
102 static const struct got_error *start_repo_child(struct gotd_client *,
103 enum gotd_procid, struct gotd_repo *, char *, const char *, int, int);
104 static const struct got_error *start_auth_child(struct gotd_client *, int,
105 struct gotd_repo *, char *, const char *, int, int);
106 static void kill_proc(struct gotd_child_proc *, int);
111 fprintf(stderr, "usage: %s [-dnv] [-f config-file]\n", getprogname());
116 unix_socket_listen(const char *unix_socket_path, uid_t uid, gid_t gid)
118 struct sockaddr_un sun;
120 mode_t old_umask, mode;
122 fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK| SOCK_CLOEXEC, 0);
128 sun.sun_family = AF_UNIX;
129 if (strlcpy(sun.sun_path, unix_socket_path,
130 sizeof(sun.sun_path)) >= sizeof(sun.sun_path)) {
131 log_warnx("%s: name too long", unix_socket_path);
136 if (unlink(unix_socket_path) == -1) {
137 if (errno != ENOENT) {
138 log_warn("unlink %s", unix_socket_path);
144 old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
145 mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
147 if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
148 log_warn("bind: %s", unix_socket_path);
156 if (chmod(unix_socket_path, mode) == -1) {
157 log_warn("chmod %o %s", mode, unix_socket_path);
159 unlink(unix_socket_path);
163 if (chown(unix_socket_path, uid, gid) == -1) {
164 log_warn("chown %s uid=%d gid=%d", unix_socket_path, uid, gid);
166 unlink(unix_socket_path);
170 if (listen(fd, GOTD_UNIX_SOCKET_BACKLOG) == -1) {
173 unlink(unix_socket_path);
181 client_hash(uint32_t client_id)
183 return SipHash24(&clients_hash_key, &client_id, sizeof(client_id));
187 add_client(struct gotd_client *client)
189 uint64_t slot = client_hash(client->id) % nitems(gotd_clients);
190 STAILQ_INSERT_HEAD(&gotd_clients[slot], client, entry);
194 static struct gotd_client *
195 find_client(uint32_t client_id)
198 struct gotd_client *c;
200 slot = client_hash(client_id) % nitems(gotd_clients);
201 STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
202 if (c->id == client_id)
209 static struct gotd_client *
210 find_client_by_proc_fd(int fd)
214 for (slot = 0; slot < nitems(gotd_clients); slot++) {
215 struct gotd_client *c;
217 STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
218 if (c->repo && c->repo->iev.ibuf.fd == fd)
220 if (c->auth && c->auth->iev.ibuf.fd == fd)
222 if (c->session && c->session->iev.ibuf.fd == fd)
231 client_is_reading(struct gotd_client *client)
233 return (client->required_auth &
234 (GOTD_AUTH_READ | GOTD_AUTH_WRITE)) == GOTD_AUTH_READ;
238 client_is_writing(struct gotd_client *client)
240 return (client->required_auth &
241 (GOTD_AUTH_READ | GOTD_AUTH_WRITE)) ==
242 (GOTD_AUTH_READ | GOTD_AUTH_WRITE);
245 static const struct got_error *
246 ensure_client_is_not_writing(struct gotd_client *client)
248 if (client_is_writing(client)) {
249 return got_error_fmt(GOT_ERR_BAD_PACKET,
250 "uid %d made a read-request but is writing to "
251 "a repository", client->euid);
257 static const struct got_error *
258 ensure_client_is_not_reading(struct gotd_client *client)
260 if (client_is_reading(client)) {
261 return got_error_fmt(GOT_ERR_BAD_PACKET,
262 "uid %d made a write-request but is reading from "
263 "a repository", client->euid);
270 wait_for_child(pid_t child_pid)
275 log_debug("waiting for child PID %ld to terminate",
279 pid = waitpid(child_pid, &status, WNOHANG);
281 if (errno != EINTR && errno != ECHILD)
283 } else if (WIFSIGNALED(status)) {
284 log_warnx("child PID %ld terminated; signal %d",
285 (long)pid, WTERMSIG(status));
287 } while (pid != -1 || (pid == -1 && errno == EINTR));
291 proc_done(struct gotd_child_proc *proc)
293 event_del(&proc->iev.ev);
294 msgbuf_clear(&proc->iev.ibuf.w);
295 close(proc->iev.ibuf.fd);
297 wait_for_child(proc->pid);
302 kill_auth_proc(struct gotd_client *client)
304 struct gotd_child_proc *proc;
306 if (client->auth == NULL)
316 kill_session_proc(struct gotd_client *client)
318 struct gotd_child_proc *proc;
320 if (client->session == NULL)
323 proc = client->session;
324 client->session = NULL;
330 disconnect(struct gotd_client *client)
332 struct gotd_imsg_disconnect idisconnect;
333 struct gotd_child_proc *proc = client->repo;
334 struct gotd_child_proc *listen_proc = &gotd.listen_proc;
337 log_debug("uid %d: disconnecting", client->euid);
339 kill_auth_proc(client);
340 kill_session_proc(client);
342 idisconnect.client_id = client->id;
344 if (gotd_imsg_compose_event(&proc->iev,
345 GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
346 &idisconnect, sizeof(idisconnect)) == -1)
347 log_warn("imsg compose DISCONNECT");
349 msgbuf_clear(&proc->iev.ibuf.w);
350 close(proc->iev.ibuf.fd);
352 wait_for_child(proc->pid);
357 if (gotd_imsg_compose_event(&listen_proc->iev,
358 GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
359 &idisconnect, sizeof(idisconnect)) == -1)
360 log_warn("imsg compose DISCONNECT");
362 slot = client_hash(client->id) % nitems(gotd_clients);
363 STAILQ_REMOVE(&gotd_clients[slot], client, gotd_client, entry);
364 imsg_clear(&client->iev.ibuf);
365 event_del(&client->iev.ev);
366 evtimer_del(&client->tmo);
367 if (client->fd != -1)
369 else if (client->iev.ibuf.fd != -1)
370 close(client->iev.ibuf.fd);
376 disconnect_on_error(struct gotd_client *client, const struct got_error *err)
380 log_warnx("uid %d: %s", client->euid, err->msg);
381 if (err->code != GOT_ERR_EOF && client->fd != -1) {
382 imsg_init(&ibuf, client->fd);
383 gotd_imsg_send_error(&ibuf, 0, PROC_GOTD, err);
389 static const struct got_error *
390 send_repo_info(struct gotd_imsgev *iev, struct gotd_repo *repo)
392 const struct got_error *err = NULL;
393 struct gotd_imsg_info_repo irepo;
395 memset(&irepo, 0, sizeof(irepo));
397 if (strlcpy(irepo.repo_name, repo->name, sizeof(irepo.repo_name))
398 >= sizeof(irepo.repo_name))
399 return got_error_msg(GOT_ERR_NO_SPACE, "repo name too long");
400 if (strlcpy(irepo.repo_path, repo->path, sizeof(irepo.repo_path))
401 >= sizeof(irepo.repo_path))
402 return got_error_msg(GOT_ERR_NO_SPACE, "repo path too long");
404 if (gotd_imsg_compose_event(iev, GOTD_IMSG_INFO_REPO, PROC_GOTD, -1,
405 &irepo, sizeof(irepo)) == -1) {
406 err = got_error_from_errno("imsg compose INFO_REPO");
414 static const struct got_error *
415 send_client_info(struct gotd_imsgev *iev, struct gotd_client *client)
417 const struct got_error *err = NULL;
418 struct gotd_imsg_info_client iclient;
419 struct gotd_child_proc *proc;
421 memset(&iclient, 0, sizeof(iclient));
422 iclient.euid = client->euid;
423 iclient.egid = client->egid;
427 if (strlcpy(iclient.repo_name, proc->repo_path,
428 sizeof(iclient.repo_name)) >= sizeof(iclient.repo_name)) {
429 return got_error_msg(GOT_ERR_NO_SPACE,
430 "repo name too long");
432 if (client_is_writing(client))
433 iclient.is_writing = 1;
435 iclient.repo_child_pid = proc->pid;
439 iclient.session_child_pid = client->session->pid;
441 if (gotd_imsg_compose_event(iev, GOTD_IMSG_INFO_CLIENT, PROC_GOTD, -1,
442 &iclient, sizeof(iclient)) == -1) {
443 err = got_error_from_errno("imsg compose INFO_CLIENT");
451 static const struct got_error *
452 send_info(struct gotd_client *client)
454 const struct got_error *err = NULL;
455 struct gotd_imsg_info info;
457 struct gotd_repo *repo;
459 if (client->euid != 0)
460 return got_error_set_errno(EPERM, "info");
463 info.verbosity = gotd.verbosity;
464 info.nrepos = gotd.nrepos;
465 info.nclients = client_cnt - 1;
467 if (gotd_imsg_compose_event(&client->iev, GOTD_IMSG_INFO, PROC_GOTD, -1,
468 &info, sizeof(info)) == -1) {
469 err = got_error_from_errno("imsg compose INFO");
474 TAILQ_FOREACH(repo, &gotd.repos, entry) {
475 err = send_repo_info(&client->iev, repo);
480 for (slot = 0; slot < nitems(gotd_clients); slot++) {
481 struct gotd_client *c;
482 STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
483 if (c->id == client->id)
485 err = send_client_info(&client->iev, c);
494 static const struct got_error *
495 stop_gotd(struct gotd_client *client)
498 if (client->euid != 0)
499 return got_error_set_errno(EPERM, "stop");
506 static struct gotd_repo *
507 find_repo_by_name(const char *repo_name)
509 struct gotd_repo *repo;
512 TAILQ_FOREACH(repo, &gotd.repos, entry) {
513 namelen = strlen(repo->name);
514 if (strncmp(repo->name, repo_name, namelen) != 0)
516 if (repo_name[namelen] == '\0' ||
517 strcmp(&repo_name[namelen], ".git") == 0)
524 static const struct got_error *
525 start_client_authentication(struct gotd_client *client, struct imsg *imsg)
527 const struct got_error *err;
528 struct gotd_imsg_list_refs ireq;
529 struct gotd_repo *repo = NULL;
532 log_debug("list-refs request from uid %d", client->euid);
534 if (client->state != GOTD_CLIENT_STATE_NEW)
535 return got_error_msg(GOT_ERR_BAD_REQUEST,
536 "unexpected list-refs request received");
538 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
539 if (datalen != sizeof(ireq))
540 return got_error(GOT_ERR_PRIVSEP_LEN);
542 memcpy(&ireq, imsg->data, datalen);
544 if (ireq.client_is_reading) {
545 err = ensure_client_is_not_writing(client);
548 repo = find_repo_by_name(ireq.repo_name);
550 return got_error(GOT_ERR_NOT_GIT_REPO);
551 err = start_auth_child(client, GOTD_AUTH_READ, repo,
552 gotd.argv0, gotd.confpath, gotd.daemonize,
557 err = ensure_client_is_not_reading(client);
560 repo = find_repo_by_name(ireq.repo_name);
562 return got_error(GOT_ERR_NOT_GIT_REPO);
563 err = start_auth_child(client,
564 GOTD_AUTH_READ | GOTD_AUTH_WRITE,
565 repo, gotd.argv0, gotd.confpath, gotd.daemonize,
571 evtimer_add(&client->tmo, &auth_timeout);
573 /* Flow continues upon authentication successs/failure or timeout. */
578 gotd_request(int fd, short events, void *arg)
580 struct gotd_imsgev *iev = arg;
581 struct imsgbuf *ibuf = &iev->ibuf;
582 struct gotd_client *client = iev->handler_arg;
583 const struct got_error *err = NULL;
587 if (events & EV_WRITE) {
588 while (ibuf->w.queued) {
589 n = msgbuf_write(&ibuf->w);
590 if (n == -1 && errno == EPIPE) {
592 * The client has closed its socket.
593 * This can happen when Git clients are
594 * done sending pack file data.
596 msgbuf_clear(&ibuf->w);
598 } else if (n == -1 && errno != EAGAIN) {
599 err = got_error_from_errno("imsg_flush");
600 disconnect_on_error(client, err);
604 /* Connection closed. */
605 err = got_error(GOT_ERR_EOF);
606 disconnect_on_error(client, err);
611 /* Disconnect gotctl(8) now that messages have been sent. */
612 if (!client_is_reading(client) && !client_is_writing(client)) {
618 if ((events & EV_READ) == 0)
621 memset(&imsg, 0, sizeof(imsg));
623 while (err == NULL) {
624 err = gotd_imsg_recv(&imsg, ibuf, 0);
626 if (err->code == GOT_ERR_PRIVSEP_READ)
631 evtimer_del(&client->tmo);
633 switch (imsg.hdr.type) {
635 err = send_info(client);
638 err = stop_gotd(client);
640 case GOTD_IMSG_LIST_REFS:
641 err = start_client_authentication(client, &imsg);
644 log_debug("unexpected imsg %d", imsg.hdr.type);
645 err = got_error(GOT_ERR_PRIVSEP_MSG);
653 disconnect_on_error(client, err);
655 gotd_imsg_event_add(&client->iev);
660 gotd_auth_timeout(int fd, short events, void *arg)
662 struct gotd_client *client = arg;
664 log_debug("disconnecting uid %d due to authentication timeout",
669 static const struct got_error *
670 recv_connect(uint32_t *client_id, struct imsg *imsg)
672 const struct got_error *err = NULL;
673 struct gotd_imsg_connect iconnect;
676 struct gotd_client *client = NULL;
680 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
681 if (datalen != sizeof(iconnect))
682 return got_error(GOT_ERR_PRIVSEP_LEN);
683 memcpy(&iconnect, imsg->data, sizeof(iconnect));
687 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
691 if (find_client(iconnect.client_id)) {
692 err = got_error_msg(GOT_ERR_CLIENT_ID, "duplicate client ID");
696 client = calloc(1, sizeof(*client));
697 if (client == NULL) {
698 err = got_error_from_errno("calloc");
702 *client_id = iconnect.client_id;
704 client->state = GOTD_CLIENT_STATE_NEW;
705 client->id = iconnect.client_id;
708 /* The auth process will verify UID/GID for us. */
709 client->euid = iconnect.euid;
710 client->egid = iconnect.egid;
712 imsg_init(&client->iev.ibuf, client->fd);
713 client->iev.handler = gotd_request;
714 client->iev.events = EV_READ;
715 client->iev.handler_arg = client;
717 event_set(&client->iev.ev, client->fd, EV_READ, gotd_request,
719 gotd_imsg_event_add(&client->iev);
721 evtimer_set(&client->tmo, gotd_auth_timeout, client);
724 log_debug("%s: new client uid %d connected on fd %d", __func__,
725 client->euid, client->fd);
728 struct gotd_child_proc *listen_proc = &gotd.listen_proc;
729 struct gotd_imsg_disconnect idisconnect;
731 idisconnect.client_id = client->id;
732 if (gotd_imsg_compose_event(&listen_proc->iev,
733 GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
734 &idisconnect, sizeof(idisconnect)) == -1)
735 log_warn("imsg compose DISCONNECT");
744 static const char *gotd_proc_names[PROC_MAX] = {
754 kill_proc(struct gotd_child_proc *proc, int fatal)
757 log_warnx("sending SIGKILL to PID %d", proc->pid);
758 kill(proc->pid, SIGKILL);
760 kill(proc->pid, SIGTERM);
766 struct gotd_child_proc *proc;
769 log_debug("shutting down");
770 for (slot = 0; slot < nitems(gotd_clients); slot++) {
771 struct gotd_client *c, *tmp;
773 STAILQ_FOREACH_SAFE(c, &gotd_clients[slot], entry, tmp)
777 proc = &gotd.listen_proc;
778 msgbuf_clear(&proc->iev.ibuf.w);
779 close(proc->iev.ibuf.fd);
781 wait_for_child(proc->pid);
783 log_info("terminating");
788 gotd_sighdlr(int sig, short event, void *arg)
791 * Normal signal handler rules don't apply because libevent
797 log_info("%s: ignoring SIGHUP", __func__);
800 log_info("%s: ignoring SIGUSR1", __func__);
807 fatalx("unexpected signal");
811 static const struct got_error *
812 ensure_proc_is_reading(struct gotd_client *client,
813 struct gotd_child_proc *proc)
815 if (!client_is_reading(client)) {
817 return got_error_fmt(GOT_ERR_BAD_PACKET,
818 "PID %d handled a read-request for uid %d but this "
819 "user is not reading from a repository", proc->pid,
826 static const struct got_error *
827 ensure_proc_is_writing(struct gotd_client *client,
828 struct gotd_child_proc *proc)
830 if (!client_is_writing(client)) {
832 return got_error_fmt(GOT_ERR_BAD_PACKET,
833 "PID %d handled a write-request for uid %d but this "
834 "user is not writing to a repository", proc->pid,
842 verify_imsg_src(struct gotd_client *client, struct gotd_child_proc *proc,
845 const struct got_error *err;
848 if (proc->type == PROC_REPO_READ || proc->type == PROC_REPO_WRITE) {
849 if (client->repo == NULL)
850 fatalx("no process found for uid %d", client->euid);
851 if (proc->pid != client->repo->pid) {
853 log_warnx("received message from PID %d for uid %d, "
854 "while PID %d is the process serving this user",
855 proc->pid, client->euid, client->repo->pid);
859 if (proc->type == PROC_SESSION) {
860 if (client->session == NULL) {
861 log_warnx("no session found for uid %d", client->euid);
864 if (proc->pid != client->session->pid) {
866 log_warnx("received message from PID %d for uid %d, "
867 "while PID %d is the process serving this user",
868 proc->pid, client->euid, client->session->pid);
873 switch (imsg->hdr.type) {
874 case GOTD_IMSG_ERROR:
877 case GOTD_IMSG_CONNECT:
878 if (proc->type != PROC_LISTEN) {
879 err = got_error_fmt(GOT_ERR_BAD_PACKET,
880 "new connection for uid %d from PID %d "
881 "which is not the listen process",
882 proc->pid, client->euid);
886 case GOTD_IMSG_ACCESS_GRANTED:
887 if (proc->type != PROC_AUTH) {
888 err = got_error_fmt(GOT_ERR_BAD_PACKET,
889 "authentication of uid %d from PID %d "
890 "which is not the auth process",
891 proc->pid, client->euid);
895 case GOTD_IMSG_CLIENT_SESSION_READY:
896 if (proc->type != PROC_SESSION) {
897 err = got_error_fmt(GOT_ERR_BAD_PACKET,
898 "unexpected \"ready\" signal from PID %d",
903 case GOTD_IMSG_REPO_CHILD_READY:
904 if (proc->type != PROC_REPO_READ &&
905 proc->type != PROC_REPO_WRITE) {
906 err = got_error_fmt(GOT_ERR_BAD_PACKET,
907 "unexpected \"ready\" signal from PID %d",
912 case GOTD_IMSG_PACKFILE_DONE:
913 err = ensure_proc_is_reading(client, proc);
915 log_warnx("uid %d: %s", client->euid, err->msg);
919 case GOTD_IMSG_PACKFILE_INSTALL:
920 case GOTD_IMSG_REF_UPDATES_START:
921 case GOTD_IMSG_REF_UPDATE:
922 err = ensure_proc_is_writing(client, proc);
924 log_warnx("uid %d: %s", client->euid, err->msg);
929 log_debug("%s: unexpected imsg %d", __func__, imsg->hdr.type);
936 static const struct got_error *
937 connect_repo_child(struct gotd_client *client,
938 struct gotd_child_proc *repo_proc)
940 static const struct got_error *err;
941 struct gotd_imsgev *session_iev = &client->session->iev;
942 struct gotd_imsg_connect_repo_child ireq;
945 if (client->state != GOTD_CLIENT_STATE_ACCESS_GRANTED)
946 return got_error_msg(GOT_ERR_BAD_REQUEST,
947 "unexpected repo child ready signal received");
949 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
950 PF_UNSPEC, pipe) == -1)
953 memset(&ireq, 0, sizeof(ireq));
954 ireq.client_id = client->id;
955 ireq.proc_id = repo_proc->type;
957 /* Pass repo child pipe to session child process. */
958 if (gotd_imsg_compose_event(session_iev, GOTD_IMSG_CONNECT_REPO_CHILD,
959 PROC_GOTD, pipe[0], &ireq, sizeof(ireq)) == -1) {
960 err = got_error_from_errno("imsg compose CONNECT_REPO_CHILD");
966 /* Pass session child pipe to repo child process. */
967 if (gotd_imsg_compose_event(&repo_proc->iev,
968 GOTD_IMSG_CONNECT_REPO_CHILD, PROC_GOTD, pipe[1], NULL, 0) == -1) {
969 err = got_error_from_errno("imsg compose CONNECT_REPO_CHILD");
978 gotd_dispatch_listener(int fd, short event, void *arg)
980 struct gotd_imsgev *iev = arg;
981 struct imsgbuf *ibuf = &iev->ibuf;
982 struct gotd_child_proc *proc = &gotd.listen_proc;
987 if (proc->iev.ibuf.fd != fd)
988 fatalx("%s: unexpected fd %d", __func__, fd);
990 if (event & EV_READ) {
991 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
992 fatal("imsg_read error");
994 /* Connection closed. */
1000 if (event & EV_WRITE) {
1001 n = msgbuf_write(&ibuf->w);
1002 if (n == -1 && errno != EAGAIN)
1003 fatal("msgbuf_write");
1005 /* Connection closed. */
1012 const struct got_error *err = NULL;
1013 struct gotd_client *client = NULL;
1014 uint32_t client_id = 0;
1015 int do_disconnect = 0;
1017 if ((n = imsg_get(ibuf, &imsg)) == -1)
1018 fatal("%s: imsg_get error", __func__);
1019 if (n == 0) /* No more messages. */
1022 switch (imsg.hdr.type) {
1023 case GOTD_IMSG_ERROR:
1025 err = gotd_imsg_recv_error(&client_id, &imsg);
1027 case GOTD_IMSG_CONNECT:
1028 err = recv_connect(&client_id, &imsg);
1031 log_debug("unexpected imsg %d", imsg.hdr.type);
1035 client = find_client(client_id);
1036 if (client == NULL) {
1037 log_warnx("%s: client not found", __func__);
1043 log_warnx("uid %d: %s", client->euid, err->msg);
1045 if (do_disconnect) {
1047 disconnect_on_error(client, err);
1056 gotd_imsg_event_add(iev);
1058 /* This pipe is dead. Remove its event handler */
1059 event_del(&iev->ev);
1060 event_loopexit(NULL);
1065 gotd_dispatch_auth_child(int fd, short event, void *arg)
1067 const struct got_error *err = NULL;
1068 struct gotd_imsgev *iev = arg;
1069 struct imsgbuf *ibuf = &iev->ibuf;
1070 struct gotd_client *client;
1071 struct gotd_repo *repo = NULL;
1075 uint32_t client_id = 0;
1076 int do_disconnect = 0;
1078 client = find_client_by_proc_fd(fd);
1080 fatalx("cannot find client for fd %d", fd);
1082 if (client->auth == NULL)
1083 fatalx("cannot find auth child process for fd %d", fd);
1085 if (event & EV_READ) {
1086 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1087 fatal("imsg_read error");
1089 /* Connection closed. */
1095 if (event & EV_WRITE) {
1096 n = msgbuf_write(&ibuf->w);
1097 if (n == -1 && errno != EAGAIN)
1098 fatal("msgbuf_write");
1100 /* Connection closed. */
1106 if (client->auth->iev.ibuf.fd != fd)
1107 fatalx("%s: unexpected fd %d", __func__, fd);
1109 if ((n = imsg_get(ibuf, &imsg)) == -1)
1110 fatal("%s: imsg_get error", __func__);
1111 if (n == 0) /* No more messages. */
1114 evtimer_del(&client->tmo);
1116 switch (imsg.hdr.type) {
1117 case GOTD_IMSG_ERROR:
1119 err = gotd_imsg_recv_error(&client_id, &imsg);
1121 case GOTD_IMSG_ACCESS_GRANTED:
1122 client->state = GOTD_CLIENT_STATE_ACCESS_GRANTED;
1126 log_debug("unexpected imsg %d", imsg.hdr.type);
1130 if (!verify_imsg_src(client, client->auth, &imsg)) {
1132 log_debug("dropping imsg type %d from PID %d",
1133 imsg.hdr.type, client->auth->pid);
1137 if (do_disconnect) {
1139 disconnect_on_error(client, err);
1145 repo = find_repo_by_name(client->auth->repo_name);
1147 err = got_error(GOT_ERR_NOT_GIT_REPO);
1150 kill_auth_proc(client);
1152 log_info("authenticated uid %d for repository %s",
1153 client->euid, repo->name);
1155 err = start_session_child(client, repo, gotd.argv0,
1156 gotd.confpath, gotd.daemonize, gotd.verbosity);
1161 log_warnx("uid %d: %s", client->euid, err->msg);
1163 /* We might have killed the auth process by now. */
1164 if (client->auth != NULL) {
1166 gotd_imsg_event_add(iev);
1168 /* This pipe is dead. Remove its event handler */
1169 event_del(&iev->ev);
1174 static const struct got_error *
1175 connect_session(struct gotd_client *client)
1177 const struct got_error *err = NULL;
1178 struct gotd_imsg_connect iconnect;
1181 memset(&iconnect, 0, sizeof(iconnect));
1183 s = dup(client->fd);
1185 return got_error_from_errno("dup");
1187 iconnect.client_id = client->id;
1188 iconnect.euid = client->euid;
1189 iconnect.egid = client->egid;
1191 if (gotd_imsg_compose_event(&client->session->iev, GOTD_IMSG_CONNECT,
1192 PROC_GOTD, s, &iconnect, sizeof(iconnect)) == -1) {
1193 err = got_error_from_errno("imsg compose CONNECT");
1199 * We are no longer interested in messages from this client.
1200 * Further client requests will be handled by the session process.
1202 msgbuf_clear(&client->iev.ibuf.w);
1203 imsg_clear(&client->iev.ibuf);
1204 event_del(&client->iev.ev);
1205 client->fd = -1; /* will be closed via copy in client->iev.ibuf.fd */
1211 gotd_dispatch_client_session(int fd, short event, void *arg)
1213 struct gotd_imsgev *iev = arg;
1214 struct imsgbuf *ibuf = &iev->ibuf;
1215 struct gotd_child_proc *proc = NULL;
1216 struct gotd_client *client = NULL;
1221 client = find_client_by_proc_fd(fd);
1223 fatalx("cannot find client for fd %d", fd);
1225 if (event & EV_READ) {
1226 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1227 fatal("imsg_read error");
1229 /* Connection closed. */
1235 if (event & EV_WRITE) {
1236 n = msgbuf_write(&ibuf->w);
1237 if (n == -1 && errno != EAGAIN)
1238 fatal("msgbuf_write");
1240 /* Connection closed. */
1246 proc = client->session;
1248 fatalx("cannot find session child process for fd %d", fd);
1251 const struct got_error *err = NULL;
1252 uint32_t client_id = 0;
1253 int do_disconnect = 0, do_start_repo_child = 0;
1255 if ((n = imsg_get(ibuf, &imsg)) == -1)
1256 fatal("%s: imsg_get error", __func__);
1257 if (n == 0) /* No more messages. */
1260 switch (imsg.hdr.type) {
1261 case GOTD_IMSG_ERROR:
1263 err = gotd_imsg_recv_error(&client_id, &imsg);
1265 case GOTD_IMSG_CLIENT_SESSION_READY:
1266 if (client->state != GOTD_CLIENT_STATE_ACCESS_GRANTED) {
1267 err = got_error(GOT_ERR_PRIVSEP_MSG);
1270 do_start_repo_child = 1;
1272 case GOTD_IMSG_DISCONNECT:
1276 log_debug("unexpected imsg %d", imsg.hdr.type);
1280 if (!verify_imsg_src(client, proc, &imsg)) {
1281 log_debug("dropping imsg type %d from PID %d",
1282 imsg.hdr.type, proc->pid);
1287 log_warnx("uid %d: %s", client->euid, err->msg);
1289 if (do_start_repo_child) {
1290 struct gotd_repo *repo;
1292 repo = find_repo_by_name(client->session->repo_name);
1294 enum gotd_procid proc_type;
1296 if (client->required_auth & GOTD_AUTH_WRITE)
1297 proc_type = PROC_REPO_WRITE;
1299 proc_type = PROC_REPO_READ;
1301 err = start_repo_child(client, proc_type, repo,
1302 gotd.argv0, gotd.confpath, gotd.daemonize,
1305 err = got_error(GOT_ERR_NOT_GIT_REPO);
1308 log_warnx("uid %d: %s", client->euid, err->msg);
1313 if (do_disconnect) {
1315 disconnect_on_error(client, err);
1324 gotd_imsg_event_add(iev);
1326 /* This pipe is dead. Remove its event handler */
1327 event_del(&iev->ev);
1333 gotd_dispatch_repo_child(int fd, short event, void *arg)
1335 struct gotd_imsgev *iev = arg;
1336 struct imsgbuf *ibuf = &iev->ibuf;
1337 struct gotd_child_proc *proc = NULL;
1338 struct gotd_client *client;
1343 client = find_client_by_proc_fd(fd);
1345 fatalx("cannot find client for fd %d", fd);
1347 if (event & EV_READ) {
1348 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1349 fatal("imsg_read error");
1351 /* Connection closed. */
1357 if (event & EV_WRITE) {
1358 n = msgbuf_write(&ibuf->w);
1359 if (n == -1 && errno != EAGAIN)
1360 fatal("msgbuf_write");
1362 /* Connection closed. */
1368 proc = client->repo;
1370 fatalx("cannot find child process for fd %d", fd);
1373 const struct got_error *err = NULL;
1374 uint32_t client_id = 0;
1375 int do_disconnect = 0;
1377 if ((n = imsg_get(ibuf, &imsg)) == -1)
1378 fatal("%s: imsg_get error", __func__);
1379 if (n == 0) /* No more messages. */
1382 switch (imsg.hdr.type) {
1383 case GOTD_IMSG_ERROR:
1385 err = gotd_imsg_recv_error(&client_id, &imsg);
1387 case GOTD_IMSG_REPO_CHILD_READY:
1388 err = connect_session(client);
1391 err = connect_repo_child(client, proc);
1394 log_debug("unexpected imsg %d", imsg.hdr.type);
1398 if (!verify_imsg_src(client, proc, &imsg)) {
1399 log_debug("dropping imsg type %d from PID %d",
1400 imsg.hdr.type, proc->pid);
1405 log_warnx("uid %d: %s", client->euid, err->msg);
1407 if (do_disconnect) {
1409 disconnect_on_error(client, err);
1418 gotd_imsg_event_add(iev);
1420 /* This pipe is dead. Remove its event handler */
1421 event_del(&iev->ev);
1427 start_child(enum gotd_procid proc_id, const char *repo_path,
1428 char *argv0, const char *confpath, int fd, int daemonize, int verbosity)
1434 switch (pid = fork()) {
1436 fatal("cannot fork");
1444 if (fd != GOTD_FILENO_MSG_PIPE) {
1445 if (dup2(fd, GOTD_FILENO_MSG_PIPE) == -1)
1446 fatal("cannot setup imsg fd");
1447 } else if (fcntl(fd, F_SETFD, 0) == -1)
1448 fatal("cannot setup imsg fd");
1450 argv[argc++] = argv0;
1453 argv[argc++] = (char *)"-L";
1456 argv[argc++] = (char *)"-A";
1459 argv[argc++] = (char *)"-S";
1461 case PROC_REPO_READ:
1462 argv[argc++] = (char *)"-R";
1464 case PROC_REPO_WRITE:
1465 argv[argc++] = (char *)"-W";
1468 fatalx("invalid process id %d", proc_id);
1471 argv[argc++] = (char *)"-f";
1472 argv[argc++] = (char *)confpath;
1475 argv[argc++] = (char *)"-P";
1476 argv[argc++] = (char *)repo_path;
1480 argv[argc++] = (char *)"-d";
1482 argv[argc++] = (char *)"-v";
1484 argv[argc++] = (char *)"-v";
1485 argv[argc++] = NULL;
1487 execvp(argv0, argv);
1492 start_listener(char *argv0, const char *confpath, int daemonize, int verbosity)
1494 struct gotd_child_proc *proc = &gotd.listen_proc;
1496 proc->type = PROC_LISTEN;
1498 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1499 PF_UNSPEC, proc->pipe) == -1)
1500 fatal("socketpair");
1502 proc->pid = start_child(proc->type, NULL, argv0, confpath,
1503 proc->pipe[1], daemonize, verbosity);
1504 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1505 proc->iev.handler = gotd_dispatch_listener;
1506 proc->iev.events = EV_READ;
1507 proc->iev.handler_arg = NULL;
1510 static const struct got_error *
1511 start_session_child(struct gotd_client *client, struct gotd_repo *repo,
1512 char *argv0, const char *confpath, int daemonize, int verbosity)
1514 struct gotd_child_proc *proc;
1516 proc = calloc(1, sizeof(*proc));
1518 return got_error_from_errno("calloc");
1520 proc->type = PROC_SESSION;
1521 if (strlcpy(proc->repo_name, repo->name,
1522 sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
1523 fatalx("repository name too long: %s", repo->name);
1524 log_debug("starting client uid %d session for repository %s",
1525 client->euid, repo->name);
1526 if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >=
1527 sizeof(proc->repo_path))
1528 fatalx("repository path too long: %s", repo->path);
1529 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1530 PF_UNSPEC, proc->pipe) == -1)
1531 fatal("socketpair");
1532 proc->pid = start_child(proc->type, proc->repo_path, argv0,
1533 confpath, proc->pipe[1], daemonize, verbosity);
1534 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1535 log_debug("proc %s %s is on fd %d",
1536 gotd_proc_names[proc->type], proc->repo_path,
1538 proc->iev.handler = gotd_dispatch_client_session;
1539 proc->iev.events = EV_READ;
1540 proc->iev.handler_arg = NULL;
1541 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1542 gotd_dispatch_client_session, &proc->iev);
1543 gotd_imsg_event_add(&proc->iev);
1545 client->session = proc;
1549 static const struct got_error *
1550 start_repo_child(struct gotd_client *client, enum gotd_procid proc_type,
1551 struct gotd_repo *repo, char *argv0, const char *confpath,
1552 int daemonize, int verbosity)
1554 struct gotd_child_proc *proc;
1556 if (proc_type != PROC_REPO_READ && proc_type != PROC_REPO_WRITE)
1557 return got_error_msg(GOT_ERR_NOT_IMPL, "bad process type");
1559 proc = calloc(1, sizeof(*proc));
1561 return got_error_from_errno("calloc");
1563 proc->type = proc_type;
1564 if (strlcpy(proc->repo_name, repo->name,
1565 sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
1566 fatalx("repository name too long: %s", repo->name);
1567 log_debug("starting %s for repository %s",
1568 proc->type == PROC_REPO_READ ? "reader" : "writer", repo->name);
1569 if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >=
1570 sizeof(proc->repo_path))
1571 fatalx("repository path too long: %s", repo->path);
1572 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1573 PF_UNSPEC, proc->pipe) == -1)
1574 fatal("socketpair");
1575 proc->pid = start_child(proc->type, proc->repo_path, argv0,
1576 confpath, proc->pipe[1], daemonize, verbosity);
1577 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1578 log_debug("proc %s %s is on fd %d",
1579 gotd_proc_names[proc->type], proc->repo_path,
1581 proc->iev.handler = gotd_dispatch_repo_child;
1582 proc->iev.events = EV_READ;
1583 proc->iev.handler_arg = NULL;
1584 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1585 gotd_dispatch_repo_child, &proc->iev);
1586 gotd_imsg_event_add(&proc->iev);
1588 client->repo = proc;
1592 static const struct got_error *
1593 start_auth_child(struct gotd_client *client, int required_auth,
1594 struct gotd_repo *repo, char *argv0, const char *confpath,
1595 int daemonize, int verbosity)
1597 const struct got_error *err = NULL;
1598 struct gotd_child_proc *proc;
1599 struct gotd_imsg_auth iauth;
1602 memset(&iauth, 0, sizeof(iauth));
1604 fd = dup(client->fd);
1606 return got_error_from_errno("dup");
1608 proc = calloc(1, sizeof(*proc));
1610 err = got_error_from_errno("calloc");
1615 proc->type = PROC_AUTH;
1616 if (strlcpy(proc->repo_name, repo->name,
1617 sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
1618 fatalx("repository name too long: %s", repo->name);
1619 log_debug("starting auth for uid %d repository %s",
1620 client->euid, repo->name);
1621 if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >=
1622 sizeof(proc->repo_path))
1623 fatalx("repository path too long: %s", repo->path);
1624 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1625 PF_UNSPEC, proc->pipe) == -1)
1626 fatal("socketpair");
1627 proc->pid = start_child(proc->type, proc->repo_path, argv0,
1628 confpath, proc->pipe[1], daemonize, verbosity);
1629 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1630 log_debug("proc %s %s is on fd %d",
1631 gotd_proc_names[proc->type], proc->repo_path,
1633 proc->iev.handler = gotd_dispatch_auth_child;
1634 proc->iev.events = EV_READ;
1635 proc->iev.handler_arg = NULL;
1636 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1637 gotd_dispatch_auth_child, &proc->iev);
1638 gotd_imsg_event_add(&proc->iev);
1640 iauth.euid = client->euid;
1641 iauth.egid = client->egid;
1642 iauth.required_auth = required_auth;
1643 iauth.client_id = client->id;
1644 if (gotd_imsg_compose_event(&proc->iev, GOTD_IMSG_AUTHENTICATE,
1645 PROC_GOTD, fd, &iauth, sizeof(iauth)) == -1) {
1646 log_warn("imsg compose AUTHENTICATE");
1648 /* Let the auth_timeout handler tidy up. */
1651 client->auth = proc;
1652 client->required_auth = required_auth;
1657 apply_unveil_repo_readonly(const char *repo_path)
1659 if (unveil(repo_path, "r") == -1)
1660 fatal("unveil %s", repo_path);
1662 if (unveil(NULL, NULL) == -1)
1667 apply_unveil_repo_readwrite(const char *repo_path)
1669 if (unveil(repo_path, "rwc") == -1)
1670 fatal("unveil %s", repo_path);
1672 if (unveil(GOT_TMPDIR_STR, "rwc") == -1)
1673 fatal("unveil %s", GOT_TMPDIR_STR);
1675 if (unveil(NULL, NULL) == -1)
1680 apply_unveil_none(void)
1682 if (unveil("/", "") == -1)
1685 if (unveil(NULL, NULL) == -1)
1690 apply_unveil_selfexec(void)
1692 if (unveil(gotd.argv0, "x") == -1)
1693 fatal("unveil %s", gotd.argv0);
1695 if (unveil(NULL, NULL) == -1)
1700 main(int argc, char **argv)
1702 const struct got_error *error = NULL;
1703 int ch, fd = -1, daemonize = 1, verbosity = 0, noaction = 0;
1704 const char *confpath = GOTD_CONF_PATH;
1705 char *argv0 = argv[0];
1707 struct passwd *pw = NULL;
1708 char *repo_path = NULL;
1709 enum gotd_procid proc_id = PROC_GOTD;
1710 struct event evsigint, evsigterm, evsighup, evsigusr1;
1711 int *pack_fds = NULL, *temp_fds = NULL;
1713 log_init(1, LOG_DAEMON); /* Log to stderr until daemonized. */
1715 while ((ch = getopt(argc, argv, "Adf:LnP:RSvW")) != -1) {
1718 proc_id = PROC_AUTH;
1727 proc_id = PROC_LISTEN;
1733 repo_path = realpath(optarg, NULL);
1734 if (repo_path == NULL)
1735 fatal("realpath '%s'", optarg);
1738 proc_id = PROC_REPO_READ;
1741 proc_id = PROC_SESSION;
1748 proc_id = PROC_REPO_WRITE;
1761 if (geteuid() && (proc_id == PROC_GOTD || proc_id == PROC_LISTEN))
1762 fatalx("need root privileges");
1764 if (parse_config(confpath, proc_id, &gotd) != 0)
1767 pw = getpwnam(gotd.user_name);
1769 fatalx("user %s not found", gotd.user_name);
1771 if (pw->pw_uid == 0)
1772 fatalx("cannot run %s as the superuser", getprogname());
1775 fprintf(stderr, "configuration OK\n");
1780 gotd.daemonize = daemonize;
1781 gotd.verbosity = verbosity;
1782 gotd.confpath = confpath;
1784 /* Require an absolute path in argv[0] for reliable re-exec. */
1785 if (!got_path_is_absolute(argv0))
1786 fatalx("bad path \"%s\": must be an absolute path", argv0);
1788 log_init(daemonize ? 0 : 1, LOG_DAEMON);
1789 log_setverbose(verbosity);
1791 if (proc_id == PROC_GOTD) {
1792 snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
1793 arc4random_buf(&clients_hash_key, sizeof(clients_hash_key));
1794 if (daemonize && daemon(1, 0) == -1)
1796 gotd.pid = getpid();
1797 start_listener(argv0, confpath, daemonize, verbosity);
1798 } else if (proc_id == PROC_LISTEN) {
1799 snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
1801 log_info("socket: %s", gotd.unix_socket_path);
1802 log_info("user: %s", pw->pw_name);
1805 fd = unix_socket_listen(gotd.unix_socket_path, pw->pw_uid,
1808 fatal("cannot listen on unix socket %s",
1809 gotd.unix_socket_path);
1811 } else if (proc_id == PROC_AUTH) {
1812 snprintf(title, sizeof(title), "%s %s",
1813 gotd_proc_names[proc_id], repo_path);
1814 } else if (proc_id == PROC_REPO_READ || proc_id == PROC_REPO_WRITE ||
1815 proc_id == PROC_SESSION) {
1816 error = got_repo_pack_fds_open(&pack_fds);
1818 fatalx("cannot open pack tempfiles: %s", error->msg);
1819 error = got_repo_temp_fds_open(&temp_fds);
1821 fatalx("cannot open pack tempfiles: %s", error->msg);
1822 if (repo_path == NULL)
1823 fatalx("repository path not specified");
1824 snprintf(title, sizeof(title), "%s %s",
1825 gotd_proc_names[proc_id], repo_path);
1827 fatal("invalid process id %d", proc_id);
1829 setproctitle("%s", title);
1830 log_procinit(title);
1832 /* Drop root privileges. */
1833 if (setgid(pw->pw_gid) == -1)
1834 fatal("setgid %d failed", pw->pw_gid);
1835 if (setuid(pw->pw_uid) == -1)
1836 fatal("setuid %d failed", pw->pw_uid);
1843 /* "exec" promise will be limited to argv[0] via unveil(2). */
1844 if (pledge("stdio proc exec sendfd recvfd unveil", NULL) == -1)
1850 if (pledge("stdio sendfd unix unveil", NULL) == -1)
1854 * Ensure that AF_UNIX bind(2) cannot be used with any other
1855 * sockets by revoking all filesystem access via unveil(2).
1857 apply_unveil_none();
1859 listen_main(title, fd, gotd.connection_limits,
1860 gotd.nconnection_limits);
1865 if (pledge("stdio getpw recvfd unix unveil", NULL) == -1)
1869 * We need the "unix" pledge promise for getpeername(2) only.
1870 * Ensure that AF_UNIX bind(2) cannot be used by revoking all
1871 * filesystem access via unveil(2). Access to password database
1872 * files will still work since "getpw" bypasses unveil(2).
1874 apply_unveil_none();
1876 auth_main(title, &gotd.repos, repo_path);
1882 * The "recvfd" promise is only needed during setup and
1883 * will be removed in a later pledge(2) call.
1885 if (pledge("stdio rpath wpath cpath recvfd sendfd fattr flock "
1886 "unveil", NULL) == -1)
1889 apply_unveil_repo_readwrite(repo_path);
1890 session_main(title, repo_path, pack_fds, temp_fds,
1891 &gotd.request_timeout);
1894 case PROC_REPO_READ:
1896 if (pledge("stdio rpath recvfd unveil", NULL) == -1)
1899 apply_unveil_repo_readonly(repo_path);
1900 repo_read_main(title, repo_path, pack_fds, temp_fds);
1903 case PROC_REPO_WRITE:
1905 if (pledge("stdio rpath recvfd unveil", NULL) == -1)
1908 apply_unveil_repo_readonly(repo_path);
1909 repo_write_main(title, repo_path, pack_fds, temp_fds);
1913 fatal("invalid process id %d", proc_id);
1916 if (proc_id != PROC_GOTD)
1917 fatal("invalid process id %d", proc_id);
1919 apply_unveil_selfexec();
1921 signal_set(&evsigint, SIGINT, gotd_sighdlr, NULL);
1922 signal_set(&evsigterm, SIGTERM, gotd_sighdlr, NULL);
1923 signal_set(&evsighup, SIGHUP, gotd_sighdlr, NULL);
1924 signal_set(&evsigusr1, SIGUSR1, gotd_sighdlr, NULL);
1925 signal(SIGPIPE, SIG_IGN);
1927 signal_add(&evsigint, NULL);
1928 signal_add(&evsigterm, NULL);
1929 signal_add(&evsighup, NULL);
1930 signal_add(&evsigusr1, NULL);
1932 gotd_imsg_event_add(&gotd.listen_proc.iev);