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>
43 #include "got_error.h"
44 #include "got_opentemp.h"
46 #include "got_repository.h"
47 #include "got_object.h"
48 #include "got_reference.h"
50 #include "got_lib_delta.h"
51 #include "got_lib_object.h"
52 #include "got_lib_object_cache.h"
53 #include "got_lib_sha1.h"
54 #include "got_lib_gitproto.h"
55 #include "got_lib_pack.h"
56 #include "got_lib_repository.h"
63 #include "repo_read.h"
64 #include "repo_write.h"
67 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
70 enum gotd_client_state {
71 GOTD_CLIENT_STATE_NEW,
72 GOTD_CLIENT_STATE_ACCESS_GRANTED,
76 STAILQ_ENTRY(gotd_client) entry;
77 enum gotd_client_state state;
80 struct gotd_imsgev iev;
84 struct gotd_child_proc *repo;
85 struct gotd_child_proc *auth;
86 struct gotd_child_proc *session;
89 STAILQ_HEAD(gotd_clients, gotd_client);
91 static struct gotd_clients gotd_clients[GOTD_CLIENT_TABLE_SIZE];
92 static SIPHASH_KEY clients_hash_key;
93 volatile int client_cnt;
94 static struct timeval auth_timeout = { 5, 0 };
95 static struct gotd gotd;
97 void gotd_sighdlr(int sig, short event, void *arg);
98 static void gotd_shutdown(void);
99 static const struct got_error *start_session_child(struct gotd_client *,
100 struct gotd_repo *, char *, const char *, int, int);
101 static const struct got_error *start_repo_child(struct gotd_client *,
102 enum gotd_procid, struct gotd_repo *, char *, const char *, int, int);
103 static const struct got_error *start_auth_child(struct gotd_client *, int,
104 struct gotd_repo *, char *, const char *, int, int);
105 static void kill_proc(struct gotd_child_proc *, int);
110 fprintf(stderr, "usage: %s [-dnv] [-f config-file]\n", getprogname());
115 unix_socket_listen(const char *unix_socket_path, uid_t uid, gid_t gid)
117 struct sockaddr_un sun;
119 mode_t old_umask, mode;
121 fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK| SOCK_CLOEXEC, 0);
127 sun.sun_family = AF_UNIX;
128 if (strlcpy(sun.sun_path, unix_socket_path,
129 sizeof(sun.sun_path)) >= sizeof(sun.sun_path)) {
130 log_warnx("%s: name too long", unix_socket_path);
135 if (unlink(unix_socket_path) == -1) {
136 if (errno != ENOENT) {
137 log_warn("unlink %s", unix_socket_path);
143 old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
144 mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
146 if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
147 log_warn("bind: %s", unix_socket_path);
155 if (chmod(unix_socket_path, mode) == -1) {
156 log_warn("chmod %o %s", mode, unix_socket_path);
158 unlink(unix_socket_path);
162 if (chown(unix_socket_path, uid, gid) == -1) {
163 log_warn("chown %s uid=%d gid=%d", unix_socket_path, uid, gid);
165 unlink(unix_socket_path);
169 if (listen(fd, GOTD_UNIX_SOCKET_BACKLOG) == -1) {
172 unlink(unix_socket_path);
180 client_hash(uint32_t client_id)
182 return SipHash24(&clients_hash_key, &client_id, sizeof(client_id));
186 add_client(struct gotd_client *client)
188 uint64_t slot = client_hash(client->id) % nitems(gotd_clients);
189 STAILQ_INSERT_HEAD(&gotd_clients[slot], client, entry);
193 static struct gotd_client *
194 find_client(uint32_t client_id)
197 struct gotd_client *c;
199 slot = client_hash(client_id) % nitems(gotd_clients);
200 STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
201 if (c->id == client_id)
208 static struct gotd_client *
209 find_client_by_proc_fd(int fd)
213 for (slot = 0; slot < nitems(gotd_clients); slot++) {
214 struct gotd_client *c;
216 STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
217 if (c->repo && c->repo->iev.ibuf.fd == fd)
219 if (c->auth && c->auth->iev.ibuf.fd == fd)
221 if (c->session && c->session->iev.ibuf.fd == fd)
230 client_is_reading(struct gotd_client *client)
232 return (client->required_auth &
233 (GOTD_AUTH_READ | GOTD_AUTH_WRITE)) == GOTD_AUTH_READ;
237 client_is_writing(struct gotd_client *client)
239 return (client->required_auth &
240 (GOTD_AUTH_READ | GOTD_AUTH_WRITE)) ==
241 (GOTD_AUTH_READ | GOTD_AUTH_WRITE);
244 static const struct got_error *
245 ensure_client_is_not_writing(struct gotd_client *client)
247 if (client_is_writing(client)) {
248 return got_error_fmt(GOT_ERR_BAD_PACKET,
249 "uid %d made a read-request but is writing to "
250 "a repository", client->euid);
256 static const struct got_error *
257 ensure_client_is_not_reading(struct gotd_client *client)
259 if (client_is_reading(client)) {
260 return got_error_fmt(GOT_ERR_BAD_PACKET,
261 "uid %d made a write-request but is reading from "
262 "a repository", client->euid);
269 wait_for_child(pid_t child_pid)
274 log_debug("waiting for child PID %ld to terminate",
278 pid = waitpid(child_pid, &status, WNOHANG);
280 if (errno != EINTR && errno != ECHILD)
282 } else if (WIFSIGNALED(status)) {
283 log_warnx("child PID %ld terminated; signal %d",
284 (long)pid, WTERMSIG(status));
286 } while (pid != -1 || (pid == -1 && errno == EINTR));
290 proc_done(struct gotd_child_proc *proc)
292 event_del(&proc->iev.ev);
293 msgbuf_clear(&proc->iev.ibuf.w);
294 close(proc->iev.ibuf.fd);
296 wait_for_child(proc->pid);
301 kill_auth_proc(struct gotd_client *client)
303 struct gotd_child_proc *proc;
305 if (client->auth == NULL)
315 kill_session_proc(struct gotd_client *client)
317 struct gotd_child_proc *proc;
319 if (client->session == NULL)
322 proc = client->session;
323 client->session = NULL;
329 disconnect(struct gotd_client *client)
331 struct gotd_imsg_disconnect idisconnect;
332 struct gotd_child_proc *proc = client->repo;
333 struct gotd_child_proc *listen_proc = &gotd.listen_proc;
336 log_debug("uid %d: disconnecting", client->euid);
338 kill_auth_proc(client);
339 kill_session_proc(client);
342 event_del(&proc->iev.ev);
343 msgbuf_clear(&proc->iev.ibuf.w);
344 close(proc->iev.ibuf.fd);
346 wait_for_child(proc->pid);
351 idisconnect.client_id = client->id;
352 if (gotd_imsg_compose_event(&listen_proc->iev,
353 GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
354 &idisconnect, sizeof(idisconnect)) == -1)
355 log_warn("imsg compose DISCONNECT");
357 slot = client_hash(client->id) % nitems(gotd_clients);
358 STAILQ_REMOVE(&gotd_clients[slot], client, gotd_client, entry);
359 imsg_clear(&client->iev.ibuf);
360 event_del(&client->iev.ev);
361 evtimer_del(&client->tmo);
362 if (client->fd != -1)
364 else if (client->iev.ibuf.fd != -1)
365 close(client->iev.ibuf.fd);
371 disconnect_on_error(struct gotd_client *client, const struct got_error *err)
375 log_warnx("uid %d: %s", client->euid, err->msg);
376 if (err->code != GOT_ERR_EOF && client->fd != -1) {
377 imsg_init(&ibuf, client->fd);
378 gotd_imsg_send_error(&ibuf, 0, PROC_GOTD, err);
384 static const struct got_error *
385 send_repo_info(struct gotd_imsgev *iev, struct gotd_repo *repo)
387 const struct got_error *err = NULL;
388 struct gotd_imsg_info_repo irepo;
390 memset(&irepo, 0, sizeof(irepo));
392 if (strlcpy(irepo.repo_name, repo->name, sizeof(irepo.repo_name))
393 >= sizeof(irepo.repo_name))
394 return got_error_msg(GOT_ERR_NO_SPACE, "repo name too long");
395 if (strlcpy(irepo.repo_path, repo->path, sizeof(irepo.repo_path))
396 >= sizeof(irepo.repo_path))
397 return got_error_msg(GOT_ERR_NO_SPACE, "repo path too long");
399 if (gotd_imsg_compose_event(iev, GOTD_IMSG_INFO_REPO, PROC_GOTD, -1,
400 &irepo, sizeof(irepo)) == -1) {
401 err = got_error_from_errno("imsg compose INFO_REPO");
409 static const struct got_error *
410 send_client_info(struct gotd_imsgev *iev, struct gotd_client *client)
412 const struct got_error *err = NULL;
413 struct gotd_imsg_info_client iclient;
414 struct gotd_child_proc *proc;
416 memset(&iclient, 0, sizeof(iclient));
417 iclient.euid = client->euid;
418 iclient.egid = client->egid;
422 if (strlcpy(iclient.repo_name, proc->repo_path,
423 sizeof(iclient.repo_name)) >= sizeof(iclient.repo_name)) {
424 return got_error_msg(GOT_ERR_NO_SPACE,
425 "repo name too long");
427 if (client_is_writing(client))
428 iclient.is_writing = 1;
430 iclient.repo_child_pid = proc->pid;
434 iclient.session_child_pid = client->session->pid;
436 if (gotd_imsg_compose_event(iev, GOTD_IMSG_INFO_CLIENT, PROC_GOTD, -1,
437 &iclient, sizeof(iclient)) == -1) {
438 err = got_error_from_errno("imsg compose INFO_CLIENT");
446 static const struct got_error *
447 send_info(struct gotd_client *client)
449 const struct got_error *err = NULL;
450 struct gotd_imsg_info info;
452 struct gotd_repo *repo;
454 if (client->euid != 0)
455 return got_error_set_errno(EPERM, "info");
458 info.verbosity = gotd.verbosity;
459 info.nrepos = gotd.nrepos;
460 info.nclients = client_cnt - 1;
462 if (gotd_imsg_compose_event(&client->iev, GOTD_IMSG_INFO, PROC_GOTD, -1,
463 &info, sizeof(info)) == -1) {
464 err = got_error_from_errno("imsg compose INFO");
469 TAILQ_FOREACH(repo, &gotd.repos, entry) {
470 err = send_repo_info(&client->iev, repo);
475 for (slot = 0; slot < nitems(gotd_clients); slot++) {
476 struct gotd_client *c;
477 STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
478 if (c->id == client->id)
480 err = send_client_info(&client->iev, c);
489 static const struct got_error *
490 stop_gotd(struct gotd_client *client)
493 if (client->euid != 0)
494 return got_error_set_errno(EPERM, "stop");
501 static struct gotd_repo *
502 find_repo_by_name(const char *repo_name)
504 struct gotd_repo *repo;
507 TAILQ_FOREACH(repo, &gotd.repos, entry) {
508 namelen = strlen(repo->name);
509 if (strncmp(repo->name, repo_name, namelen) != 0)
511 if (repo_name[namelen] == '\0' ||
512 strcmp(&repo_name[namelen], ".git") == 0)
519 static const struct got_error *
520 start_client_authentication(struct gotd_client *client, struct imsg *imsg)
522 const struct got_error *err;
523 struct gotd_imsg_list_refs ireq;
524 struct gotd_repo *repo = NULL;
527 log_debug("list-refs request from uid %d", client->euid);
529 if (client->state != GOTD_CLIENT_STATE_NEW)
530 return got_error_msg(GOT_ERR_BAD_REQUEST,
531 "unexpected list-refs request received");
533 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
534 if (datalen != sizeof(ireq))
535 return got_error(GOT_ERR_PRIVSEP_LEN);
537 memcpy(&ireq, imsg->data, datalen);
539 if (ireq.client_is_reading) {
540 err = ensure_client_is_not_writing(client);
543 repo = find_repo_by_name(ireq.repo_name);
545 return got_error(GOT_ERR_NOT_GIT_REPO);
546 err = start_auth_child(client, GOTD_AUTH_READ, repo,
547 gotd.argv0, gotd.confpath, gotd.daemonize,
552 err = ensure_client_is_not_reading(client);
555 repo = find_repo_by_name(ireq.repo_name);
557 return got_error(GOT_ERR_NOT_GIT_REPO);
558 err = start_auth_child(client,
559 GOTD_AUTH_READ | GOTD_AUTH_WRITE,
560 repo, gotd.argv0, gotd.confpath, gotd.daemonize,
566 evtimer_add(&client->tmo, &auth_timeout);
568 /* Flow continues upon authentication successs/failure or timeout. */
573 gotd_request(int fd, short events, void *arg)
575 struct gotd_imsgev *iev = arg;
576 struct imsgbuf *ibuf = &iev->ibuf;
577 struct gotd_client *client = iev->handler_arg;
578 const struct got_error *err = NULL;
582 if (events & EV_WRITE) {
583 while (ibuf->w.queued) {
584 n = msgbuf_write(&ibuf->w);
585 if (n == -1 && errno == EPIPE) {
587 * The client has closed its socket.
588 * This can happen when Git clients are
589 * done sending pack file data.
591 msgbuf_clear(&ibuf->w);
593 } else if (n == -1 && errno != EAGAIN) {
594 err = got_error_from_errno("imsg_flush");
595 disconnect_on_error(client, err);
599 /* Connection closed. */
600 err = got_error(GOT_ERR_EOF);
601 disconnect_on_error(client, err);
606 /* Disconnect gotctl(8) now that messages have been sent. */
607 if (!client_is_reading(client) && !client_is_writing(client)) {
613 if ((events & EV_READ) == 0)
616 memset(&imsg, 0, sizeof(imsg));
618 while (err == NULL) {
619 err = gotd_imsg_recv(&imsg, ibuf, 0);
621 if (err->code == GOT_ERR_PRIVSEP_READ)
626 evtimer_del(&client->tmo);
628 switch (imsg.hdr.type) {
630 err = send_info(client);
633 err = stop_gotd(client);
635 case GOTD_IMSG_LIST_REFS:
636 err = start_client_authentication(client, &imsg);
639 log_debug("unexpected imsg %d", imsg.hdr.type);
640 err = got_error(GOT_ERR_PRIVSEP_MSG);
648 disconnect_on_error(client, err);
650 gotd_imsg_event_add(&client->iev);
655 gotd_auth_timeout(int fd, short events, void *arg)
657 struct gotd_client *client = arg;
659 log_debug("disconnecting uid %d due to authentication timeout",
664 static const struct got_error *
665 recv_connect(uint32_t *client_id, struct imsg *imsg)
667 const struct got_error *err = NULL;
668 struct gotd_imsg_connect iconnect;
671 struct gotd_client *client = NULL;
675 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
676 if (datalen != sizeof(iconnect))
677 return got_error(GOT_ERR_PRIVSEP_LEN);
678 memcpy(&iconnect, imsg->data, sizeof(iconnect));
682 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
686 if (find_client(iconnect.client_id)) {
687 err = got_error_msg(GOT_ERR_CLIENT_ID, "duplicate client ID");
691 client = calloc(1, sizeof(*client));
692 if (client == NULL) {
693 err = got_error_from_errno("calloc");
697 *client_id = iconnect.client_id;
699 client->state = GOTD_CLIENT_STATE_NEW;
700 client->id = iconnect.client_id;
703 /* The auth process will verify UID/GID for us. */
704 client->euid = iconnect.euid;
705 client->egid = iconnect.egid;
707 imsg_init(&client->iev.ibuf, client->fd);
708 client->iev.handler = gotd_request;
709 client->iev.events = EV_READ;
710 client->iev.handler_arg = client;
712 event_set(&client->iev.ev, client->fd, EV_READ, gotd_request,
714 gotd_imsg_event_add(&client->iev);
716 evtimer_set(&client->tmo, gotd_auth_timeout, client);
719 log_debug("%s: new client uid %d connected on fd %d", __func__,
720 client->euid, client->fd);
723 struct gotd_child_proc *listen_proc = &gotd.listen_proc;
724 struct gotd_imsg_disconnect idisconnect;
726 idisconnect.client_id = client->id;
727 if (gotd_imsg_compose_event(&listen_proc->iev,
728 GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
729 &idisconnect, sizeof(idisconnect)) == -1)
730 log_warn("imsg compose DISCONNECT");
739 static const char *gotd_proc_names[PROC_MAX] = {
749 kill_proc(struct gotd_child_proc *proc, int fatal)
752 log_warnx("sending SIGKILL to PID %d", proc->pid);
753 kill(proc->pid, SIGKILL);
755 kill(proc->pid, SIGTERM);
761 struct gotd_child_proc *proc;
764 log_debug("shutting down");
765 for (slot = 0; slot < nitems(gotd_clients); slot++) {
766 struct gotd_client *c, *tmp;
768 STAILQ_FOREACH_SAFE(c, &gotd_clients[slot], entry, tmp)
772 proc = &gotd.listen_proc;
773 msgbuf_clear(&proc->iev.ibuf.w);
774 close(proc->iev.ibuf.fd);
776 wait_for_child(proc->pid);
778 log_info("terminating");
783 gotd_sighdlr(int sig, short event, void *arg)
786 * Normal signal handler rules don't apply because libevent
792 log_info("%s: ignoring SIGHUP", __func__);
795 log_info("%s: ignoring SIGUSR1", __func__);
802 fatalx("unexpected signal");
806 static const struct got_error *
807 ensure_proc_is_reading(struct gotd_client *client,
808 struct gotd_child_proc *proc)
810 if (!client_is_reading(client)) {
812 return got_error_fmt(GOT_ERR_BAD_PACKET,
813 "PID %d handled a read-request for uid %d but this "
814 "user is not reading from a repository", proc->pid,
821 static const struct got_error *
822 ensure_proc_is_writing(struct gotd_client *client,
823 struct gotd_child_proc *proc)
825 if (!client_is_writing(client)) {
827 return got_error_fmt(GOT_ERR_BAD_PACKET,
828 "PID %d handled a write-request for uid %d but this "
829 "user is not writing to a repository", proc->pid,
837 verify_imsg_src(struct gotd_client *client, struct gotd_child_proc *proc,
840 const struct got_error *err;
843 if (proc->type == PROC_REPO_READ || proc->type == PROC_REPO_WRITE) {
844 if (client->repo == NULL)
845 fatalx("no process found for uid %d", client->euid);
846 if (proc->pid != client->repo->pid) {
848 log_warnx("received message from PID %d for uid %d, "
849 "while PID %d is the process serving this user",
850 proc->pid, client->euid, client->repo->pid);
854 if (proc->type == PROC_SESSION) {
855 if (client->session == NULL) {
856 log_warnx("no session found for uid %d", client->euid);
859 if (proc->pid != client->session->pid) {
861 log_warnx("received message from PID %d for uid %d, "
862 "while PID %d is the process serving this user",
863 proc->pid, client->euid, client->session->pid);
868 switch (imsg->hdr.type) {
869 case GOTD_IMSG_ERROR:
872 case GOTD_IMSG_CONNECT:
873 if (proc->type != PROC_LISTEN) {
874 err = got_error_fmt(GOT_ERR_BAD_PACKET,
875 "new connection for uid %d from PID %d "
876 "which is not the listen process",
877 proc->pid, client->euid);
881 case GOTD_IMSG_ACCESS_GRANTED:
882 if (proc->type != PROC_AUTH) {
883 err = got_error_fmt(GOT_ERR_BAD_PACKET,
884 "authentication of uid %d from PID %d "
885 "which is not the auth process",
886 proc->pid, client->euid);
890 case GOTD_IMSG_CLIENT_SESSION_READY:
891 if (proc->type != PROC_SESSION) {
892 err = got_error_fmt(GOT_ERR_BAD_PACKET,
893 "unexpected \"ready\" signal from PID %d",
898 case GOTD_IMSG_REPO_CHILD_READY:
899 if (proc->type != PROC_REPO_READ &&
900 proc->type != PROC_REPO_WRITE) {
901 err = got_error_fmt(GOT_ERR_BAD_PACKET,
902 "unexpected \"ready\" signal from PID %d",
907 case GOTD_IMSG_PACKFILE_DONE:
908 err = ensure_proc_is_reading(client, proc);
910 log_warnx("uid %d: %s", client->euid, err->msg);
914 case GOTD_IMSG_PACKFILE_INSTALL:
915 case GOTD_IMSG_REF_UPDATES_START:
916 case GOTD_IMSG_REF_UPDATE:
917 err = ensure_proc_is_writing(client, proc);
919 log_warnx("uid %d: %s", client->euid, err->msg);
924 log_debug("%s: unexpected imsg %d", __func__, imsg->hdr.type);
931 static const struct got_error *
932 connect_repo_child(struct gotd_client *client,
933 struct gotd_child_proc *repo_proc)
935 static const struct got_error *err;
936 struct gotd_imsgev *session_iev = &client->session->iev;
937 struct gotd_imsg_connect_repo_child ireq;
940 if (client->state != GOTD_CLIENT_STATE_ACCESS_GRANTED)
941 return got_error_msg(GOT_ERR_BAD_REQUEST,
942 "unexpected repo child ready signal received");
944 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
945 PF_UNSPEC, pipe) == -1)
948 memset(&ireq, 0, sizeof(ireq));
949 ireq.client_id = client->id;
950 ireq.proc_id = repo_proc->type;
952 /* Pass repo child pipe to session child process. */
953 if (gotd_imsg_compose_event(session_iev, GOTD_IMSG_CONNECT_REPO_CHILD,
954 PROC_GOTD, pipe[0], &ireq, sizeof(ireq)) == -1) {
955 err = got_error_from_errno("imsg compose CONNECT_REPO_CHILD");
961 /* Pass session child pipe to repo child process. */
962 if (gotd_imsg_compose_event(&repo_proc->iev,
963 GOTD_IMSG_CONNECT_REPO_CHILD, PROC_GOTD, pipe[1], NULL, 0) == -1) {
964 err = got_error_from_errno("imsg compose CONNECT_REPO_CHILD");
973 gotd_dispatch_listener(int fd, short event, void *arg)
975 struct gotd_imsgev *iev = arg;
976 struct imsgbuf *ibuf = &iev->ibuf;
977 struct gotd_child_proc *proc = &gotd.listen_proc;
982 if (proc->iev.ibuf.fd != fd)
983 fatalx("%s: unexpected fd %d", __func__, fd);
985 if (event & EV_READ) {
986 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
987 fatal("imsg_read error");
989 /* Connection closed. */
995 if (event & EV_WRITE) {
996 n = msgbuf_write(&ibuf->w);
997 if (n == -1 && errno != EAGAIN)
998 fatal("msgbuf_write");
1000 /* Connection closed. */
1007 const struct got_error *err = NULL;
1008 struct gotd_client *client = NULL;
1009 uint32_t client_id = 0;
1010 int do_disconnect = 0;
1012 if ((n = imsg_get(ibuf, &imsg)) == -1)
1013 fatal("%s: imsg_get error", __func__);
1014 if (n == 0) /* No more messages. */
1017 switch (imsg.hdr.type) {
1018 case GOTD_IMSG_ERROR:
1020 err = gotd_imsg_recv_error(&client_id, &imsg);
1022 case GOTD_IMSG_CONNECT:
1023 err = recv_connect(&client_id, &imsg);
1026 log_debug("unexpected imsg %d", imsg.hdr.type);
1030 client = find_client(client_id);
1031 if (client == NULL) {
1032 log_warnx("%s: client not found", __func__);
1038 log_warnx("uid %d: %s", client->euid, err->msg);
1040 if (do_disconnect) {
1042 disconnect_on_error(client, err);
1051 gotd_imsg_event_add(iev);
1053 /* This pipe is dead. Remove its event handler */
1054 event_del(&iev->ev);
1055 event_loopexit(NULL);
1060 gotd_dispatch_auth_child(int fd, short event, void *arg)
1062 const struct got_error *err = NULL;
1063 struct gotd_imsgev *iev = arg;
1064 struct imsgbuf *ibuf = &iev->ibuf;
1065 struct gotd_client *client;
1066 struct gotd_repo *repo = NULL;
1070 uint32_t client_id = 0;
1071 int do_disconnect = 0;
1073 client = find_client_by_proc_fd(fd);
1074 if (client == NULL) {
1075 /* Can happen during process teardown. */
1076 warnx("cannot find client for fd %d", fd);
1081 if (client->auth == NULL)
1082 fatalx("cannot find auth child process for fd %d", fd);
1084 if (event & EV_READ) {
1085 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1086 fatal("imsg_read error");
1088 /* Connection closed. */
1094 if (event & EV_WRITE) {
1095 n = msgbuf_write(&ibuf->w);
1096 if (n == -1 && errno != EAGAIN)
1097 fatal("msgbuf_write");
1099 /* Connection closed. */
1105 if (client->auth->iev.ibuf.fd != fd)
1106 fatalx("%s: unexpected fd %d", __func__, fd);
1108 if ((n = imsg_get(ibuf, &imsg)) == -1)
1109 fatal("%s: imsg_get error", __func__);
1110 if (n == 0) /* No more messages. */
1113 evtimer_del(&client->tmo);
1115 switch (imsg.hdr.type) {
1116 case GOTD_IMSG_ERROR:
1118 err = gotd_imsg_recv_error(&client_id, &imsg);
1120 case GOTD_IMSG_ACCESS_GRANTED:
1121 client->state = GOTD_CLIENT_STATE_ACCESS_GRANTED;
1125 log_debug("unexpected imsg %d", imsg.hdr.type);
1129 if (!verify_imsg_src(client, client->auth, &imsg)) {
1131 log_debug("dropping imsg type %d from PID %d",
1132 imsg.hdr.type, client->auth->pid);
1136 if (do_disconnect) {
1138 disconnect_on_error(client, err);
1144 repo = find_repo_by_name(client->auth->repo_name);
1146 err = got_error(GOT_ERR_NOT_GIT_REPO);
1149 kill_auth_proc(client);
1151 log_info("authenticated uid %d for repository %s",
1152 client->euid, repo->name);
1154 err = start_session_child(client, repo, gotd.argv0,
1155 gotd.confpath, gotd.daemonize, gotd.verbosity);
1160 log_warnx("uid %d: %s", client->euid, err->msg);
1162 /* We might have killed the auth process by now. */
1163 if (client->auth != NULL) {
1165 gotd_imsg_event_add(iev);
1167 /* This pipe is dead. Remove its event handler */
1168 event_del(&iev->ev);
1173 static const struct got_error *
1174 connect_session(struct gotd_client *client)
1176 const struct got_error *err = NULL;
1177 struct gotd_imsg_connect iconnect;
1180 memset(&iconnect, 0, sizeof(iconnect));
1182 s = dup(client->fd);
1184 return got_error_from_errno("dup");
1186 iconnect.client_id = client->id;
1187 iconnect.euid = client->euid;
1188 iconnect.egid = client->egid;
1190 if (gotd_imsg_compose_event(&client->session->iev, GOTD_IMSG_CONNECT,
1191 PROC_GOTD, s, &iconnect, sizeof(iconnect)) == -1) {
1192 err = got_error_from_errno("imsg compose CONNECT");
1198 * We are no longer interested in messages from this client.
1199 * Further client requests will be handled by the session process.
1201 msgbuf_clear(&client->iev.ibuf.w);
1202 imsg_clear(&client->iev.ibuf);
1203 event_del(&client->iev.ev);
1204 client->fd = -1; /* will be closed via copy in client->iev.ibuf.fd */
1210 gotd_dispatch_client_session(int fd, short event, void *arg)
1212 struct gotd_imsgev *iev = arg;
1213 struct imsgbuf *ibuf = &iev->ibuf;
1214 struct gotd_child_proc *proc = NULL;
1215 struct gotd_client *client = NULL;
1220 client = find_client_by_proc_fd(fd);
1221 if (client == NULL) {
1222 /* Can happen during process teardown. */
1223 warnx("cannot find client for fd %d", fd);
1228 if (event & EV_READ) {
1229 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1230 fatal("imsg_read error");
1232 /* Connection closed. */
1238 if (event & EV_WRITE) {
1239 n = msgbuf_write(&ibuf->w);
1240 if (n == -1 && errno != EAGAIN)
1241 fatal("msgbuf_write");
1243 /* Connection closed. */
1249 proc = client->session;
1251 fatalx("cannot find session child process for fd %d", fd);
1254 const struct got_error *err = NULL;
1255 uint32_t client_id = 0;
1256 int do_disconnect = 0, do_start_repo_child = 0;
1258 if ((n = imsg_get(ibuf, &imsg)) == -1)
1259 fatal("%s: imsg_get error", __func__);
1260 if (n == 0) /* No more messages. */
1263 switch (imsg.hdr.type) {
1264 case GOTD_IMSG_ERROR:
1266 err = gotd_imsg_recv_error(&client_id, &imsg);
1268 case GOTD_IMSG_CLIENT_SESSION_READY:
1269 if (client->state != GOTD_CLIENT_STATE_ACCESS_GRANTED) {
1270 err = got_error(GOT_ERR_PRIVSEP_MSG);
1273 do_start_repo_child = 1;
1275 case GOTD_IMSG_DISCONNECT:
1279 log_debug("unexpected imsg %d", imsg.hdr.type);
1283 if (!verify_imsg_src(client, proc, &imsg)) {
1284 log_debug("dropping imsg type %d from PID %d",
1285 imsg.hdr.type, proc->pid);
1290 log_warnx("uid %d: %s", client->euid, err->msg);
1292 if (do_start_repo_child) {
1293 struct gotd_repo *repo;
1295 repo = find_repo_by_name(client->session->repo_name);
1297 enum gotd_procid proc_type;
1299 if (client->required_auth & GOTD_AUTH_WRITE)
1300 proc_type = PROC_REPO_WRITE;
1302 proc_type = PROC_REPO_READ;
1304 err = start_repo_child(client, proc_type, repo,
1305 gotd.argv0, gotd.confpath, gotd.daemonize,
1308 err = got_error(GOT_ERR_NOT_GIT_REPO);
1311 log_warnx("uid %d: %s", client->euid, err->msg);
1316 if (do_disconnect) {
1318 disconnect_on_error(client, err);
1327 gotd_imsg_event_add(iev);
1329 /* This pipe is dead. Remove its event handler */
1330 event_del(&iev->ev);
1336 gotd_dispatch_repo_child(int fd, short event, void *arg)
1338 struct gotd_imsgev *iev = arg;
1339 struct imsgbuf *ibuf = &iev->ibuf;
1340 struct gotd_child_proc *proc = NULL;
1341 struct gotd_client *client;
1346 client = find_client_by_proc_fd(fd);
1347 if (client == NULL) {
1348 /* Can happen during process teardown. */
1349 warnx("cannot find client for fd %d", fd);
1354 if (event & EV_READ) {
1355 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1356 fatal("imsg_read error");
1358 /* Connection closed. */
1364 if (event & EV_WRITE) {
1365 n = msgbuf_write(&ibuf->w);
1366 if (n == -1 && errno != EAGAIN)
1367 fatal("msgbuf_write");
1369 /* Connection closed. */
1375 proc = client->repo;
1377 fatalx("cannot find child process for fd %d", fd);
1380 const struct got_error *err = NULL;
1381 uint32_t client_id = 0;
1382 int do_disconnect = 0;
1384 if ((n = imsg_get(ibuf, &imsg)) == -1)
1385 fatal("%s: imsg_get error", __func__);
1386 if (n == 0) /* No more messages. */
1389 switch (imsg.hdr.type) {
1390 case GOTD_IMSG_ERROR:
1392 err = gotd_imsg_recv_error(&client_id, &imsg);
1394 case GOTD_IMSG_REPO_CHILD_READY:
1395 err = connect_session(client);
1398 err = connect_repo_child(client, proc);
1401 log_debug("unexpected imsg %d", imsg.hdr.type);
1405 if (!verify_imsg_src(client, proc, &imsg)) {
1406 log_debug("dropping imsg type %d from PID %d",
1407 imsg.hdr.type, proc->pid);
1412 log_warnx("uid %d: %s", client->euid, err->msg);
1414 if (do_disconnect) {
1416 disconnect_on_error(client, err);
1425 gotd_imsg_event_add(iev);
1427 /* This pipe is dead. Remove its event handler */
1428 event_del(&iev->ev);
1434 start_child(enum gotd_procid proc_id, const char *repo_path,
1435 char *argv0, const char *confpath, int fd, int daemonize, int verbosity)
1441 switch (pid = fork()) {
1443 fatal("cannot fork");
1451 if (fd != GOTD_FILENO_MSG_PIPE) {
1452 if (dup2(fd, GOTD_FILENO_MSG_PIPE) == -1)
1453 fatal("cannot setup imsg fd");
1454 } else if (fcntl(fd, F_SETFD, 0) == -1)
1455 fatal("cannot setup imsg fd");
1457 argv[argc++] = argv0;
1460 argv[argc++] = (char *)"-L";
1463 argv[argc++] = (char *)"-A";
1466 argv[argc++] = (char *)"-S";
1468 case PROC_REPO_READ:
1469 argv[argc++] = (char *)"-R";
1471 case PROC_REPO_WRITE:
1472 argv[argc++] = (char *)"-W";
1475 fatalx("invalid process id %d", proc_id);
1478 argv[argc++] = (char *)"-f";
1479 argv[argc++] = (char *)confpath;
1482 argv[argc++] = (char *)"-P";
1483 argv[argc++] = (char *)repo_path;
1487 argv[argc++] = (char *)"-d";
1489 argv[argc++] = (char *)"-v";
1491 argv[argc++] = (char *)"-v";
1492 argv[argc++] = NULL;
1494 execvp(argv0, argv);
1499 start_listener(char *argv0, const char *confpath, int daemonize, int verbosity)
1501 struct gotd_child_proc *proc = &gotd.listen_proc;
1503 proc->type = PROC_LISTEN;
1505 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1506 PF_UNSPEC, proc->pipe) == -1)
1507 fatal("socketpair");
1509 proc->pid = start_child(proc->type, NULL, argv0, confpath,
1510 proc->pipe[1], daemonize, verbosity);
1511 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1512 proc->iev.handler = gotd_dispatch_listener;
1513 proc->iev.events = EV_READ;
1514 proc->iev.handler_arg = NULL;
1517 static const struct got_error *
1518 start_session_child(struct gotd_client *client, struct gotd_repo *repo,
1519 char *argv0, const char *confpath, int daemonize, int verbosity)
1521 struct gotd_child_proc *proc;
1523 proc = calloc(1, sizeof(*proc));
1525 return got_error_from_errno("calloc");
1527 proc->type = PROC_SESSION;
1528 if (strlcpy(proc->repo_name, repo->name,
1529 sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
1530 fatalx("repository name too long: %s", repo->name);
1531 log_debug("starting client uid %d session for repository %s",
1532 client->euid, repo->name);
1533 if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >=
1534 sizeof(proc->repo_path))
1535 fatalx("repository path too long: %s", repo->path);
1536 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1537 PF_UNSPEC, proc->pipe) == -1)
1538 fatal("socketpair");
1539 proc->pid = start_child(proc->type, proc->repo_path, argv0,
1540 confpath, proc->pipe[1], daemonize, verbosity);
1541 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1542 log_debug("proc %s %s is on fd %d",
1543 gotd_proc_names[proc->type], proc->repo_path,
1545 proc->iev.handler = gotd_dispatch_client_session;
1546 proc->iev.events = EV_READ;
1547 proc->iev.handler_arg = NULL;
1548 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1549 gotd_dispatch_client_session, &proc->iev);
1550 gotd_imsg_event_add(&proc->iev);
1552 client->session = proc;
1556 static const struct got_error *
1557 start_repo_child(struct gotd_client *client, enum gotd_procid proc_type,
1558 struct gotd_repo *repo, char *argv0, const char *confpath,
1559 int daemonize, int verbosity)
1561 struct gotd_child_proc *proc;
1563 if (proc_type != PROC_REPO_READ && proc_type != PROC_REPO_WRITE)
1564 return got_error_msg(GOT_ERR_NOT_IMPL, "bad process type");
1566 proc = calloc(1, sizeof(*proc));
1568 return got_error_from_errno("calloc");
1570 proc->type = proc_type;
1571 if (strlcpy(proc->repo_name, repo->name,
1572 sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
1573 fatalx("repository name too long: %s", repo->name);
1574 log_debug("starting %s for repository %s",
1575 proc->type == PROC_REPO_READ ? "reader" : "writer", repo->name);
1576 if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >=
1577 sizeof(proc->repo_path))
1578 fatalx("repository path too long: %s", repo->path);
1579 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1580 PF_UNSPEC, proc->pipe) == -1)
1581 fatal("socketpair");
1582 proc->pid = start_child(proc->type, proc->repo_path, argv0,
1583 confpath, proc->pipe[1], daemonize, verbosity);
1584 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1585 log_debug("proc %s %s is on fd %d",
1586 gotd_proc_names[proc->type], proc->repo_path,
1588 proc->iev.handler = gotd_dispatch_repo_child;
1589 proc->iev.events = EV_READ;
1590 proc->iev.handler_arg = NULL;
1591 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1592 gotd_dispatch_repo_child, &proc->iev);
1593 gotd_imsg_event_add(&proc->iev);
1595 client->repo = proc;
1599 static const struct got_error *
1600 start_auth_child(struct gotd_client *client, int required_auth,
1601 struct gotd_repo *repo, char *argv0, const char *confpath,
1602 int daemonize, int verbosity)
1604 const struct got_error *err = NULL;
1605 struct gotd_child_proc *proc;
1606 struct gotd_imsg_auth iauth;
1609 memset(&iauth, 0, sizeof(iauth));
1611 fd = dup(client->fd);
1613 return got_error_from_errno("dup");
1615 proc = calloc(1, sizeof(*proc));
1617 err = got_error_from_errno("calloc");
1622 proc->type = PROC_AUTH;
1623 if (strlcpy(proc->repo_name, repo->name,
1624 sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
1625 fatalx("repository name too long: %s", repo->name);
1626 log_debug("starting auth for uid %d repository %s",
1627 client->euid, repo->name);
1628 if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >=
1629 sizeof(proc->repo_path))
1630 fatalx("repository path too long: %s", repo->path);
1631 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1632 PF_UNSPEC, proc->pipe) == -1)
1633 fatal("socketpair");
1634 proc->pid = start_child(proc->type, proc->repo_path, argv0,
1635 confpath, proc->pipe[1], daemonize, verbosity);
1636 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1637 log_debug("proc %s %s is on fd %d",
1638 gotd_proc_names[proc->type], proc->repo_path,
1640 proc->iev.handler = gotd_dispatch_auth_child;
1641 proc->iev.events = EV_READ;
1642 proc->iev.handler_arg = NULL;
1643 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1644 gotd_dispatch_auth_child, &proc->iev);
1645 gotd_imsg_event_add(&proc->iev);
1647 iauth.euid = client->euid;
1648 iauth.egid = client->egid;
1649 iauth.required_auth = required_auth;
1650 iauth.client_id = client->id;
1651 if (gotd_imsg_compose_event(&proc->iev, GOTD_IMSG_AUTHENTICATE,
1652 PROC_GOTD, fd, &iauth, sizeof(iauth)) == -1) {
1653 log_warn("imsg compose AUTHENTICATE");
1655 /* Let the auth_timeout handler tidy up. */
1658 client->auth = proc;
1659 client->required_auth = required_auth;
1664 apply_unveil_repo_readonly(const char *repo_path)
1666 if (unveil(repo_path, "r") == -1)
1667 fatal("unveil %s", repo_path);
1669 if (unveil(NULL, NULL) == -1)
1674 apply_unveil_repo_readwrite(const char *repo_path)
1676 if (unveil(repo_path, "rwc") == -1)
1677 fatal("unveil %s", repo_path);
1679 if (unveil(GOT_TMPDIR_STR, "rwc") == -1)
1680 fatal("unveil %s", GOT_TMPDIR_STR);
1682 if (unveil(NULL, NULL) == -1)
1687 apply_unveil_none(void)
1689 if (unveil("/", "") == -1)
1692 if (unveil(NULL, NULL) == -1)
1697 apply_unveil_selfexec(void)
1699 if (unveil(gotd.argv0, "x") == -1)
1700 fatal("unveil %s", gotd.argv0);
1702 if (unveil(NULL, NULL) == -1)
1707 main(int argc, char **argv)
1709 const struct got_error *error = NULL;
1710 int ch, fd = -1, daemonize = 1, verbosity = 0, noaction = 0;
1711 const char *confpath = GOTD_CONF_PATH;
1712 char *argv0 = argv[0];
1714 struct passwd *pw = NULL;
1715 char *repo_path = NULL;
1716 enum gotd_procid proc_id = PROC_GOTD;
1717 struct event evsigint, evsigterm, evsighup, evsigusr1;
1718 int *pack_fds = NULL, *temp_fds = NULL;
1720 log_init(1, LOG_DAEMON); /* Log to stderr until daemonized. */
1722 while ((ch = getopt(argc, argv, "Adf:LnP:RSvW")) != -1) {
1725 proc_id = PROC_AUTH;
1734 proc_id = PROC_LISTEN;
1740 repo_path = realpath(optarg, NULL);
1741 if (repo_path == NULL)
1742 fatal("realpath '%s'", optarg);
1745 proc_id = PROC_REPO_READ;
1748 proc_id = PROC_SESSION;
1755 proc_id = PROC_REPO_WRITE;
1768 if (geteuid() && (proc_id == PROC_GOTD || proc_id == PROC_LISTEN))
1769 fatalx("need root privileges");
1771 if (parse_config(confpath, proc_id, &gotd) != 0)
1774 pw = getpwnam(gotd.user_name);
1776 fatalx("user %s not found", gotd.user_name);
1778 if (pw->pw_uid == 0)
1779 fatalx("cannot run %s as the superuser", getprogname());
1782 fprintf(stderr, "configuration OK\n");
1787 gotd.daemonize = daemonize;
1788 gotd.verbosity = verbosity;
1789 gotd.confpath = confpath;
1791 /* Require an absolute path in argv[0] for reliable re-exec. */
1792 if (!got_path_is_absolute(argv0))
1793 fatalx("bad path \"%s\": must be an absolute path", argv0);
1795 log_init(daemonize ? 0 : 1, LOG_DAEMON);
1796 log_setverbose(verbosity);
1798 if (proc_id == PROC_GOTD) {
1799 snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
1800 arc4random_buf(&clients_hash_key, sizeof(clients_hash_key));
1801 if (daemonize && daemon(1, 0) == -1)
1803 gotd.pid = getpid();
1804 start_listener(argv0, confpath, daemonize, verbosity);
1805 } else if (proc_id == PROC_LISTEN) {
1806 snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
1808 log_info("socket: %s", gotd.unix_socket_path);
1809 log_info("user: %s", pw->pw_name);
1812 fd = unix_socket_listen(gotd.unix_socket_path, pw->pw_uid,
1815 fatal("cannot listen on unix socket %s",
1816 gotd.unix_socket_path);
1818 } else if (proc_id == PROC_AUTH) {
1819 snprintf(title, sizeof(title), "%s %s",
1820 gotd_proc_names[proc_id], repo_path);
1821 } else if (proc_id == PROC_REPO_READ || proc_id == PROC_REPO_WRITE ||
1822 proc_id == PROC_SESSION) {
1823 error = got_repo_pack_fds_open(&pack_fds);
1825 fatalx("cannot open pack tempfiles: %s", error->msg);
1826 error = got_repo_temp_fds_open(&temp_fds);
1828 fatalx("cannot open pack tempfiles: %s", error->msg);
1829 if (repo_path == NULL)
1830 fatalx("repository path not specified");
1831 snprintf(title, sizeof(title), "%s %s",
1832 gotd_proc_names[proc_id], repo_path);
1834 fatal("invalid process id %d", proc_id);
1836 setproctitle("%s", title);
1837 log_procinit(title);
1839 /* Drop root privileges. */
1840 if (setgid(pw->pw_gid) == -1)
1841 fatal("setgid %d failed", pw->pw_gid);
1842 if (setuid(pw->pw_uid) == -1)
1843 fatal("setuid %d failed", pw->pw_uid);
1850 /* "exec" promise will be limited to argv[0] via unveil(2). */
1851 if (pledge("stdio proc exec sendfd recvfd unveil", NULL) == -1)
1857 if (pledge("stdio sendfd unix unveil", NULL) == -1)
1861 * Ensure that AF_UNIX bind(2) cannot be used with any other
1862 * sockets by revoking all filesystem access via unveil(2).
1864 apply_unveil_none();
1866 listen_main(title, fd, gotd.connection_limits,
1867 gotd.nconnection_limits);
1872 if (pledge("stdio getpw recvfd unix unveil", NULL) == -1)
1876 * We need the "unix" pledge promise for getpeername(2) only.
1877 * Ensure that AF_UNIX bind(2) cannot be used by revoking all
1878 * filesystem access via unveil(2). Access to password database
1879 * files will still work since "getpw" bypasses unveil(2).
1881 apply_unveil_none();
1883 auth_main(title, &gotd.repos, repo_path);
1889 * The "recvfd" promise is only needed during setup and
1890 * will be removed in a later pledge(2) call.
1892 if (pledge("stdio rpath wpath cpath recvfd sendfd fattr flock "
1893 "unveil", NULL) == -1)
1896 apply_unveil_repo_readwrite(repo_path);
1897 session_main(title, repo_path, pack_fds, temp_fds,
1898 &gotd.request_timeout);
1901 case PROC_REPO_READ:
1903 if (pledge("stdio rpath recvfd unveil", NULL) == -1)
1906 apply_unveil_repo_readonly(repo_path);
1907 repo_read_main(title, repo_path, pack_fds, temp_fds);
1910 case PROC_REPO_WRITE:
1912 if (pledge("stdio rpath recvfd unveil", NULL) == -1)
1915 apply_unveil_repo_readonly(repo_path);
1916 repo_write_main(title, repo_path, pack_fds, temp_fds);
1920 fatal("invalid process id %d", proc_id);
1923 if (proc_id != PROC_GOTD)
1924 fatal("invalid process id %d", proc_id);
1926 apply_unveil_selfexec();
1928 signal_set(&evsigint, SIGINT, gotd_sighdlr, NULL);
1929 signal_set(&evsigterm, SIGTERM, gotd_sighdlr, NULL);
1930 signal_set(&evsighup, SIGHUP, gotd_sighdlr, NULL);
1931 signal_set(&evsigusr1, SIGUSR1, gotd_sighdlr, NULL);
1932 signal(SIGPIPE, SIG_IGN);
1934 signal_add(&evsigint, NULL);
1935 signal_add(&evsigterm, NULL);
1936 signal_add(&evsighup, NULL);
1937 signal_add(&evsigusr1, NULL);
1939 gotd_imsg_event_add(&gotd.listen_proc.iev);