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>
19 #include <sys/types.h>
21 #include <sys/socket.h>
41 #include "got_error.h"
42 #include "got_opentemp.h"
44 #include "got_repository.h"
45 #include "got_object.h"
46 #include "got_reference.h"
48 #include "got_lib_delta.h"
49 #include "got_lib_object.h"
50 #include "got_lib_object_cache.h"
51 #include "got_lib_hash.h"
52 #include "got_lib_gitproto.h"
53 #include "got_lib_pack.h"
54 #include "got_lib_repository.h"
61 #include "repo_read.h"
62 #include "repo_write.h"
65 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
68 enum gotd_client_state {
69 GOTD_CLIENT_STATE_NEW,
70 GOTD_CLIENT_STATE_ACCESS_GRANTED,
73 struct gotd_child_proc {
75 enum gotd_procid type;
76 char repo_name[NAME_MAX];
77 char repo_path[PATH_MAX];
79 struct gotd_imsgev iev;
82 TAILQ_ENTRY(gotd_child_proc) entry;
84 TAILQ_HEAD(gotd_procs, gotd_child_proc) procs;
87 STAILQ_ENTRY(gotd_client) entry;
88 enum gotd_client_state state;
91 struct gotd_imsgev iev;
95 struct gotd_child_proc *repo;
96 struct gotd_child_proc *auth;
97 struct gotd_child_proc *session;
100 STAILQ_HEAD(gotd_clients, gotd_client);
102 static struct gotd_clients gotd_clients[GOTD_CLIENT_TABLE_SIZE];
103 static SIPHASH_KEY clients_hash_key;
104 volatile int client_cnt;
105 static struct timeval auth_timeout = { 5, 0 };
106 static struct gotd gotd;
108 void gotd_sighdlr(int sig, short event, void *arg);
109 static void gotd_shutdown(void);
110 static const struct got_error *start_session_child(struct gotd_client *,
111 struct gotd_repo *, char *, const char *, int, int);
112 static const struct got_error *start_repo_child(struct gotd_client *,
113 enum gotd_procid, struct gotd_repo *, char *, const char *, int, int);
114 static const struct got_error *start_auth_child(struct gotd_client *, int,
115 struct gotd_repo *, char *, const char *, int, int);
116 static void kill_proc(struct gotd_child_proc *, int);
117 static void disconnect(struct gotd_client *);
122 fprintf(stderr, "usage: %s [-dnv] [-f config-file]\n", getprogname());
127 unix_socket_listen(const char *unix_socket_path, uid_t uid, gid_t gid)
129 struct sockaddr_un sun;
131 mode_t old_umask, mode;
133 fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK| SOCK_CLOEXEC, 0);
139 sun.sun_family = AF_UNIX;
140 if (strlcpy(sun.sun_path, unix_socket_path,
141 sizeof(sun.sun_path)) >= sizeof(sun.sun_path)) {
142 log_warnx("%s: name too long", unix_socket_path);
147 if (unlink(unix_socket_path) == -1) {
148 if (errno != ENOENT) {
149 log_warn("unlink %s", unix_socket_path);
155 old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
156 mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
158 if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
159 log_warn("bind: %s", unix_socket_path);
167 if (chmod(unix_socket_path, mode) == -1) {
168 log_warn("chmod %o %s", mode, unix_socket_path);
170 unlink(unix_socket_path);
174 if (chown(unix_socket_path, uid, gid) == -1) {
175 log_warn("chown %s uid=%d gid=%d", unix_socket_path, uid, gid);
177 unlink(unix_socket_path);
181 if (listen(fd, GOTD_UNIX_SOCKET_BACKLOG) == -1) {
184 unlink(unix_socket_path);
192 client_hash(uint32_t client_id)
194 return SipHash24(&clients_hash_key, &client_id, sizeof(client_id));
198 add_client(struct gotd_client *client)
200 uint64_t slot = client_hash(client->id) % nitems(gotd_clients);
201 STAILQ_INSERT_HEAD(&gotd_clients[slot], client, entry);
205 static struct gotd_client *
206 find_client(uint32_t client_id)
209 struct gotd_client *c;
211 slot = client_hash(client_id) % nitems(gotd_clients);
212 STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
213 if (c->id == client_id)
220 static struct gotd_client *
221 find_client_by_proc_fd(int fd)
225 for (slot = 0; slot < nitems(gotd_clients); slot++) {
226 struct gotd_client *c;
228 STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
229 if (c->repo && c->repo->iev.ibuf.fd == fd)
231 if (c->auth && c->auth->iev.ibuf.fd == fd)
233 if (c->session && c->session->iev.ibuf.fd == fd)
242 client_is_reading(struct gotd_client *client)
244 return (client->required_auth &
245 (GOTD_AUTH_READ | GOTD_AUTH_WRITE)) == GOTD_AUTH_READ;
249 client_is_writing(struct gotd_client *client)
251 return (client->required_auth &
252 (GOTD_AUTH_READ | GOTD_AUTH_WRITE)) ==
253 (GOTD_AUTH_READ | GOTD_AUTH_WRITE);
256 static const struct got_error *
257 ensure_client_is_not_writing(struct gotd_client *client)
259 if (client_is_writing(client)) {
260 return got_error_fmt(GOT_ERR_BAD_PACKET,
261 "uid %d made a read-request but is writing to "
262 "a repository", client->euid);
268 static const struct got_error *
269 ensure_client_is_not_reading(struct gotd_client *client)
271 if (client_is_reading(client)) {
272 return got_error_fmt(GOT_ERR_BAD_PACKET,
273 "uid %d made a write-request but is reading from "
274 "a repository", client->euid);
281 proc_done(struct gotd_child_proc *proc)
283 struct gotd_client *client;
285 TAILQ_REMOVE(&procs, proc, entry);
287 client = find_client_by_proc_fd(proc->iev.ibuf.fd);
288 if (client != NULL) {
289 if (proc == client->repo)
291 if (proc == client->auth)
293 if (proc == client->session)
294 client->session = NULL;
298 evtimer_del(&proc->tmo);
300 if (proc->iev.ibuf.fd != -1) {
301 event_del(&proc->iev.ev);
302 msgbuf_clear(&proc->iev.ibuf.w);
303 close(proc->iev.ibuf.fd);
310 kill_repo_proc(struct gotd_client *client)
312 if (client->repo == NULL)
315 kill_proc(client->repo, 0);
320 kill_auth_proc(struct gotd_client *client)
322 if (client->auth == NULL)
325 kill_proc(client->auth, 0);
330 kill_session_proc(struct gotd_client *client)
332 if (client->session == NULL)
335 kill_proc(client->session, 0);
336 client->session = NULL;
340 disconnect(struct gotd_client *client)
342 struct gotd_imsg_disconnect idisconnect;
343 struct gotd_child_proc *listen_proc = gotd.listen_proc;
346 log_debug("uid %d: disconnecting", client->euid);
348 kill_auth_proc(client);
349 kill_session_proc(client);
350 kill_repo_proc(client);
352 idisconnect.client_id = client->id;
353 if (gotd_imsg_compose_event(&listen_proc->iev,
354 GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
355 &idisconnect, sizeof(idisconnect)) == -1)
356 log_warn("imsg compose DISCONNECT");
358 slot = client_hash(client->id) % nitems(gotd_clients);
359 STAILQ_REMOVE(&gotd_clients[slot], client, gotd_client, entry);
360 imsg_clear(&client->iev.ibuf);
361 event_del(&client->iev.ev);
362 evtimer_del(&client->tmo);
363 if (client->fd != -1)
365 else if (client->iev.ibuf.fd != -1)
366 close(client->iev.ibuf.fd);
372 disconnect_on_error(struct gotd_client *client, const struct got_error *err)
376 log_warnx("uid %d: %s", client->euid, err->msg);
377 if (err->code != GOT_ERR_EOF && client->fd != -1) {
378 imsg_init(&ibuf, client->fd);
379 gotd_imsg_send_error(&ibuf, 0, PROC_GOTD, err);
385 static const struct got_error *
386 send_repo_info(struct gotd_imsgev *iev, struct gotd_repo *repo)
388 const struct got_error *err = NULL;
389 struct gotd_imsg_info_repo irepo;
391 memset(&irepo, 0, sizeof(irepo));
393 if (strlcpy(irepo.repo_name, repo->name, sizeof(irepo.repo_name))
394 >= sizeof(irepo.repo_name))
395 return got_error_msg(GOT_ERR_NO_SPACE, "repo name too long");
396 if (strlcpy(irepo.repo_path, repo->path, sizeof(irepo.repo_path))
397 >= sizeof(irepo.repo_path))
398 return got_error_msg(GOT_ERR_NO_SPACE, "repo path too long");
400 if (gotd_imsg_compose_event(iev, GOTD_IMSG_INFO_REPO, PROC_GOTD, -1,
401 &irepo, sizeof(irepo)) == -1) {
402 err = got_error_from_errno("imsg compose INFO_REPO");
410 static const struct got_error *
411 send_client_info(struct gotd_imsgev *iev, struct gotd_client *client)
413 const struct got_error *err = NULL;
414 struct gotd_imsg_info_client iclient;
415 struct gotd_child_proc *proc;
417 memset(&iclient, 0, sizeof(iclient));
418 iclient.euid = client->euid;
419 iclient.egid = client->egid;
423 if (strlcpy(iclient.repo_name, proc->repo_path,
424 sizeof(iclient.repo_name)) >= sizeof(iclient.repo_name)) {
425 return got_error_msg(GOT_ERR_NO_SPACE,
426 "repo name too long");
428 if (client_is_writing(client))
429 iclient.is_writing = 1;
431 iclient.repo_child_pid = proc->pid;
435 iclient.session_child_pid = client->session->pid;
437 if (gotd_imsg_compose_event(iev, GOTD_IMSG_INFO_CLIENT, PROC_GOTD, -1,
438 &iclient, sizeof(iclient)) == -1) {
439 err = got_error_from_errno("imsg compose INFO_CLIENT");
447 static const struct got_error *
448 send_info(struct gotd_client *client)
450 const struct got_error *err = NULL;
451 struct gotd_imsg_info info;
453 struct gotd_repo *repo;
455 if (client->euid != 0)
456 return got_error_set_errno(EPERM, "info");
459 info.verbosity = gotd.verbosity;
460 info.nrepos = gotd.nrepos;
461 info.nclients = client_cnt - 1;
463 if (gotd_imsg_compose_event(&client->iev, GOTD_IMSG_INFO, PROC_GOTD, -1,
464 &info, sizeof(info)) == -1) {
465 err = got_error_from_errno("imsg compose INFO");
470 TAILQ_FOREACH(repo, &gotd.repos, entry) {
471 err = send_repo_info(&client->iev, repo);
476 for (slot = 0; slot < nitems(gotd_clients); slot++) {
477 struct gotd_client *c;
478 STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
479 if (c->id == client->id)
481 err = send_client_info(&client->iev, c);
490 static const struct got_error *
491 stop_gotd(struct gotd_client *client)
494 if (client->euid != 0)
495 return got_error_set_errno(EPERM, "stop");
502 static const struct got_error *
503 start_client_authentication(struct gotd_client *client, struct imsg *imsg)
505 const struct got_error *err;
506 struct gotd_imsg_list_refs ireq;
507 struct gotd_repo *repo = NULL;
510 log_debug("list-refs request from uid %d", client->euid);
512 if (client->state != GOTD_CLIENT_STATE_NEW)
513 return got_error_msg(GOT_ERR_BAD_REQUEST,
514 "unexpected list-refs request received");
516 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
517 if (datalen != sizeof(ireq))
518 return got_error(GOT_ERR_PRIVSEP_LEN);
520 memcpy(&ireq, imsg->data, datalen);
522 if (ireq.client_is_reading) {
523 err = ensure_client_is_not_writing(client);
526 repo = gotd_find_repo_by_name(ireq.repo_name, &gotd);
528 return got_error(GOT_ERR_NOT_GIT_REPO);
529 err = start_auth_child(client, GOTD_AUTH_READ, repo,
530 gotd.argv0, gotd.confpath, gotd.daemonize,
535 err = ensure_client_is_not_reading(client);
538 repo = gotd_find_repo_by_name(ireq.repo_name, &gotd);
540 return got_error(GOT_ERR_NOT_GIT_REPO);
541 err = start_auth_child(client,
542 GOTD_AUTH_READ | GOTD_AUTH_WRITE,
543 repo, gotd.argv0, gotd.confpath, gotd.daemonize,
549 evtimer_add(&client->tmo, &auth_timeout);
551 /* Flow continues upon authentication successs/failure or timeout. */
556 gotd_request(int fd, short events, void *arg)
558 struct gotd_imsgev *iev = arg;
559 struct imsgbuf *ibuf = &iev->ibuf;
560 struct gotd_client *client = iev->handler_arg;
561 const struct got_error *err = NULL;
565 if (events & EV_WRITE) {
566 while (ibuf->w.queued) {
567 n = msgbuf_write(&ibuf->w);
568 if (n == -1 && errno == EPIPE) {
570 * The client has closed its socket.
571 * This can happen when Git clients are
572 * done sending pack file data.
574 msgbuf_clear(&ibuf->w);
576 } else if (n == -1 && errno != EAGAIN) {
577 err = got_error_from_errno("imsg_flush");
578 disconnect_on_error(client, err);
582 /* Connection closed. */
583 err = got_error(GOT_ERR_EOF);
584 disconnect_on_error(client, err);
589 /* Disconnect gotctl(8) now that messages have been sent. */
590 if (!client_is_reading(client) && !client_is_writing(client)) {
596 if ((events & EV_READ) == 0)
599 memset(&imsg, 0, sizeof(imsg));
601 while (err == NULL) {
602 err = gotd_imsg_recv(&imsg, ibuf, 0);
604 if (err->code == GOT_ERR_PRIVSEP_READ)
609 evtimer_del(&client->tmo);
611 switch (imsg.hdr.type) {
613 err = send_info(client);
616 err = stop_gotd(client);
618 case GOTD_IMSG_LIST_REFS:
619 err = start_client_authentication(client, &imsg);
622 log_debug("unexpected imsg %d", imsg.hdr.type);
623 err = got_error(GOT_ERR_PRIVSEP_MSG);
631 disconnect_on_error(client, err);
633 gotd_imsg_event_add(&client->iev);
638 gotd_auth_timeout(int fd, short events, void *arg)
640 struct gotd_client *client = arg;
642 log_debug("disconnecting uid %d due to authentication timeout",
647 static const struct got_error *
648 recv_connect(uint32_t *client_id, struct imsg *imsg)
650 const struct got_error *err = NULL;
651 struct gotd_imsg_connect iconnect;
654 struct gotd_client *client = NULL;
658 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
659 if (datalen != sizeof(iconnect))
660 return got_error(GOT_ERR_PRIVSEP_LEN);
661 memcpy(&iconnect, imsg->data, sizeof(iconnect));
665 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
669 if (find_client(iconnect.client_id)) {
670 err = got_error_msg(GOT_ERR_CLIENT_ID, "duplicate client ID");
674 client = calloc(1, sizeof(*client));
675 if (client == NULL) {
676 err = got_error_from_errno("calloc");
680 *client_id = iconnect.client_id;
682 client->state = GOTD_CLIENT_STATE_NEW;
683 client->id = iconnect.client_id;
686 /* The auth process will verify UID/GID for us. */
687 client->euid = iconnect.euid;
688 client->egid = iconnect.egid;
690 imsg_init(&client->iev.ibuf, client->fd);
691 client->iev.handler = gotd_request;
692 client->iev.events = EV_READ;
693 client->iev.handler_arg = client;
695 event_set(&client->iev.ev, client->fd, EV_READ, gotd_request,
697 gotd_imsg_event_add(&client->iev);
699 evtimer_set(&client->tmo, gotd_auth_timeout, client);
702 log_debug("%s: new client uid %d connected on fd %d", __func__,
703 client->euid, client->fd);
706 struct gotd_child_proc *listen_proc = gotd.listen_proc;
707 struct gotd_imsg_disconnect idisconnect;
709 idisconnect.client_id = client->id;
710 if (gotd_imsg_compose_event(&listen_proc->iev,
711 GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
712 &idisconnect, sizeof(idisconnect)) == -1)
713 log_warn("imsg compose DISCONNECT");
722 static const char *gotd_proc_names[PROC_MAX] = {
734 kill_proc(struct gotd_child_proc *proc, int fatal)
736 struct timeval tv = { 5, 0 };
738 log_debug("kill -%d %d", fatal ? SIGKILL : SIGTERM, proc->pid);
740 if (proc->iev.ibuf.fd != -1) {
741 event_del(&proc->iev.ev);
742 msgbuf_clear(&proc->iev.ibuf.w);
743 close(proc->iev.ibuf.fd);
744 proc->iev.ibuf.fd = -1;
747 if (!evtimer_pending(&proc->tmo, NULL) && !fatal)
748 evtimer_add(&proc->tmo, &tv);
751 log_warnx("sending SIGKILL to PID %d", proc->pid);
752 kill(proc->pid, SIGKILL);
754 kill(proc->pid, SIGTERM);
758 kill_proc_timeout(int fd, short ev, void *d)
760 struct gotd_child_proc *proc = d;
762 log_warnx("timeout waiting for PID %d to terminate;"
763 " retrying with force", proc->pid);
772 log_debug("shutting down");
773 for (slot = 0; slot < nitems(gotd_clients); slot++) {
774 struct gotd_client *c, *tmp;
776 STAILQ_FOREACH_SAFE(c, &gotd_clients[slot], entry, tmp)
780 kill_proc(gotd.listen_proc, 0);
782 log_info("terminating");
786 static struct gotd_child_proc *
787 find_proc_by_pid(pid_t pid)
789 struct gotd_child_proc *proc = NULL;
791 TAILQ_FOREACH(proc, &procs, entry)
792 if (proc->pid == pid)
799 gotd_sighdlr(int sig, short event, void *arg)
801 struct gotd_child_proc *proc;
806 * Normal signal handler rules don't apply because libevent
812 log_info("%s: ignoring SIGHUP", __func__);
815 log_info("%s: ignoring SIGUSR1", __func__);
823 pid = waitpid(WAIT_ANY, &status, WNOHANG);
834 log_debug("reaped pid %d", pid);
835 proc = find_proc_by_pid(pid);
837 log_info("caught exit of unknown child %d",
842 if (WIFSIGNALED(status)) {
843 log_warnx("child PID %d terminated with"
844 " signal %d", pid, WTERMSIG(status));
851 fatalx("unexpected signal");
855 static const struct got_error *
856 ensure_proc_is_reading(struct gotd_client *client,
857 struct gotd_child_proc *proc)
859 if (!client_is_reading(client)) {
861 return got_error_fmt(GOT_ERR_BAD_PACKET,
862 "PID %d handled a read-request for uid %d but this "
863 "user is not reading from a repository", proc->pid,
870 static const struct got_error *
871 ensure_proc_is_writing(struct gotd_client *client,
872 struct gotd_child_proc *proc)
874 if (!client_is_writing(client)) {
876 return got_error_fmt(GOT_ERR_BAD_PACKET,
877 "PID %d handled a write-request for uid %d but this "
878 "user is not writing to a repository", proc->pid,
886 verify_imsg_src(struct gotd_client *client, struct gotd_child_proc *proc,
889 const struct got_error *err;
892 if (proc->type == PROC_REPO_READ || proc->type == PROC_REPO_WRITE) {
893 if (client->repo == NULL)
894 fatalx("no process found for uid %d", client->euid);
895 if (proc->pid != client->repo->pid) {
897 log_warnx("received message from PID %d for uid %d, "
898 "while PID %d is the process serving this user",
899 proc->pid, client->euid, client->repo->pid);
903 if (proc->type == PROC_SESSION_READ ||
904 proc->type == PROC_SESSION_WRITE) {
905 if (client->session == NULL) {
906 log_warnx("no session found for uid %d", client->euid);
909 if (proc->pid != client->session->pid) {
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->session->pid);
918 switch (imsg->hdr.type) {
919 case GOTD_IMSG_ERROR:
922 case GOTD_IMSG_CONNECT:
923 if (proc->type != PROC_LISTEN) {
924 err = got_error_fmt(GOT_ERR_BAD_PACKET,
925 "new connection for uid %d from PID %d "
926 "which is not the listen process",
927 proc->pid, client->euid);
931 case GOTD_IMSG_ACCESS_GRANTED:
932 if (proc->type != PROC_AUTH) {
933 err = got_error_fmt(GOT_ERR_BAD_PACKET,
934 "authentication of uid %d from PID %d "
935 "which is not the auth process",
936 proc->pid, client->euid);
940 case GOTD_IMSG_CLIENT_SESSION_READY:
941 if (proc->type != PROC_SESSION_READ &&
942 proc->type != PROC_SESSION_WRITE) {
943 err = got_error_fmt(GOT_ERR_BAD_PACKET,
944 "unexpected \"ready\" signal from PID %d",
949 case GOTD_IMSG_REPO_CHILD_READY:
950 if (proc->type != PROC_REPO_READ &&
951 proc->type != PROC_REPO_WRITE) {
952 err = got_error_fmt(GOT_ERR_BAD_PACKET,
953 "unexpected \"ready\" signal from PID %d",
958 case GOTD_IMSG_PACKFILE_DONE:
959 err = ensure_proc_is_reading(client, proc);
961 log_warnx("uid %d: %s", client->euid, err->msg);
965 case GOTD_IMSG_PACKFILE_INSTALL:
966 case GOTD_IMSG_REF_UPDATES_START:
967 case GOTD_IMSG_REF_UPDATE:
968 err = ensure_proc_is_writing(client, proc);
970 log_warnx("uid %d: %s", client->euid, err->msg);
975 log_debug("%s: unexpected imsg %d", __func__, imsg->hdr.type);
982 static const struct got_error *
983 connect_repo_child(struct gotd_client *client,
984 struct gotd_child_proc *repo_proc)
986 static const struct got_error *err;
987 struct gotd_imsgev *session_iev = &client->session->iev;
988 struct gotd_imsg_connect_repo_child ireq;
991 if (client->state != GOTD_CLIENT_STATE_ACCESS_GRANTED)
992 return got_error_msg(GOT_ERR_BAD_REQUEST,
993 "unexpected repo child ready signal received");
995 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
996 PF_UNSPEC, pipe) == -1)
999 memset(&ireq, 0, sizeof(ireq));
1000 ireq.client_id = client->id;
1001 ireq.proc_id = repo_proc->type;
1003 /* Pass repo child pipe to session child process. */
1004 if (gotd_imsg_compose_event(session_iev, GOTD_IMSG_CONNECT_REPO_CHILD,
1005 PROC_GOTD, pipe[0], &ireq, sizeof(ireq)) == -1) {
1006 err = got_error_from_errno("imsg compose CONNECT_REPO_CHILD");
1012 /* Pass session child pipe to repo child process. */
1013 if (gotd_imsg_compose_event(&repo_proc->iev,
1014 GOTD_IMSG_CONNECT_REPO_CHILD, PROC_GOTD, pipe[1], NULL, 0) == -1) {
1015 err = got_error_from_errno("imsg compose CONNECT_REPO_CHILD");
1024 gotd_dispatch_listener(int fd, short event, void *arg)
1026 struct gotd_imsgev *iev = arg;
1027 struct imsgbuf *ibuf = &iev->ibuf;
1028 struct gotd_child_proc *proc = gotd.listen_proc;
1033 if (proc->iev.ibuf.fd != fd)
1034 fatalx("%s: unexpected fd %d", __func__, fd);
1036 if (event & EV_READ) {
1037 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1038 fatal("imsg_read error");
1040 /* Connection closed. */
1046 if (event & EV_WRITE) {
1047 n = msgbuf_write(&ibuf->w);
1048 if (n == -1 && errno != EAGAIN)
1049 fatal("msgbuf_write");
1051 /* Connection closed. */
1058 const struct got_error *err = NULL;
1059 struct gotd_client *client = NULL;
1060 uint32_t client_id = 0;
1061 int do_disconnect = 0;
1063 if ((n = imsg_get(ibuf, &imsg)) == -1)
1064 fatal("%s: imsg_get error", __func__);
1065 if (n == 0) /* No more messages. */
1068 switch (imsg.hdr.type) {
1069 case GOTD_IMSG_ERROR:
1071 err = gotd_imsg_recv_error(&client_id, &imsg);
1073 case GOTD_IMSG_CONNECT:
1074 err = recv_connect(&client_id, &imsg);
1077 log_debug("unexpected imsg %d", imsg.hdr.type);
1081 client = find_client(client_id);
1082 if (client == NULL) {
1083 log_warnx("%s: client not found", __func__);
1089 log_warnx("uid %d: %s", client->euid, err->msg);
1091 if (do_disconnect) {
1093 disconnect_on_error(client, err);
1102 gotd_imsg_event_add(iev);
1104 /* This pipe is dead. Remove its event handler */
1105 event_del(&iev->ev);
1106 event_loopexit(NULL);
1111 gotd_dispatch_auth_child(int fd, short event, void *arg)
1113 const struct got_error *err = NULL;
1114 struct gotd_imsgev *iev = arg;
1115 struct imsgbuf *ibuf = &iev->ibuf;
1116 struct gotd_client *client;
1117 struct gotd_repo *repo = NULL;
1121 uint32_t client_id = 0;
1122 int do_disconnect = 0;
1124 client = find_client_by_proc_fd(fd);
1125 if (client == NULL) {
1126 /* Can happen during process teardown. */
1127 warnx("cannot find client for fd %d", fd);
1132 if (client->auth == NULL)
1133 fatalx("cannot find auth child process for fd %d", fd);
1135 if (event & EV_READ) {
1136 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1137 fatal("imsg_read error");
1139 /* Connection closed. */
1145 if (event & EV_WRITE) {
1146 n = msgbuf_write(&ibuf->w);
1147 if (n == -1 && errno != EAGAIN)
1148 fatal("msgbuf_write");
1150 /* Connection closed. */
1156 if (client->auth->iev.ibuf.fd != fd)
1157 fatalx("%s: unexpected fd %d", __func__, fd);
1159 if ((n = imsg_get(ibuf, &imsg)) == -1)
1160 fatal("%s: imsg_get error", __func__);
1161 if (n == 0) /* No more messages. */
1164 evtimer_del(&client->tmo);
1166 switch (imsg.hdr.type) {
1167 case GOTD_IMSG_ERROR:
1169 err = gotd_imsg_recv_error(&client_id, &imsg);
1171 case GOTD_IMSG_ACCESS_GRANTED:
1172 client->state = GOTD_CLIENT_STATE_ACCESS_GRANTED;
1176 log_debug("unexpected imsg %d", imsg.hdr.type);
1180 if (!verify_imsg_src(client, client->auth, &imsg)) {
1182 log_debug("dropping imsg type %d from PID %d",
1183 imsg.hdr.type, client->auth->pid);
1187 if (do_disconnect) {
1189 disconnect_on_error(client, err);
1195 repo = gotd_find_repo_by_name(client->auth->repo_name, &gotd);
1197 err = got_error(GOT_ERR_NOT_GIT_REPO);
1200 kill_auth_proc(client);
1202 log_info("authenticated uid %d for repository %s",
1203 client->euid, repo->name);
1205 err = start_session_child(client, repo, gotd.argv0,
1206 gotd.confpath, gotd.daemonize, gotd.verbosity);
1211 log_warnx("uid %d: %s", client->euid, err->msg);
1213 /* We might have killed the auth process by now. */
1214 if (client->auth != NULL) {
1216 gotd_imsg_event_add(iev);
1218 /* This pipe is dead. Remove its event handler */
1219 event_del(&iev->ev);
1224 static const struct got_error *
1225 connect_session(struct gotd_client *client)
1227 const struct got_error *err = NULL;
1228 struct gotd_imsg_connect iconnect;
1231 memset(&iconnect, 0, sizeof(iconnect));
1233 s = dup(client->fd);
1235 return got_error_from_errno("dup");
1237 iconnect.client_id = client->id;
1238 iconnect.euid = client->euid;
1239 iconnect.egid = client->egid;
1241 if (gotd_imsg_compose_event(&client->session->iev, GOTD_IMSG_CONNECT,
1242 PROC_GOTD, s, &iconnect, sizeof(iconnect)) == -1) {
1243 err = got_error_from_errno("imsg compose CONNECT");
1249 * We are no longer interested in messages from this client.
1250 * Further client requests will be handled by the session process.
1252 msgbuf_clear(&client->iev.ibuf.w);
1253 imsg_clear(&client->iev.ibuf);
1254 event_del(&client->iev.ev);
1255 client->fd = -1; /* will be closed via copy in client->iev.ibuf.fd */
1261 gotd_dispatch_client_session(int fd, short event, void *arg)
1263 struct gotd_imsgev *iev = arg;
1264 struct imsgbuf *ibuf = &iev->ibuf;
1265 struct gotd_child_proc *proc = NULL;
1266 struct gotd_client *client = NULL;
1271 client = find_client_by_proc_fd(fd);
1272 if (client == NULL) {
1273 /* Can happen during process teardown. */
1274 warnx("cannot find client for fd %d", fd);
1279 if (event & EV_READ) {
1280 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1281 fatal("imsg_read error");
1283 /* Connection closed. */
1289 if (event & EV_WRITE) {
1290 n = msgbuf_write(&ibuf->w);
1291 if (n == -1 && errno != EAGAIN)
1292 fatal("msgbuf_write");
1294 /* Connection closed. */
1300 proc = client->session;
1302 fatalx("cannot find session child process for fd %d", fd);
1305 const struct got_error *err = NULL;
1306 uint32_t client_id = 0;
1307 int do_disconnect = 0, do_start_repo_child = 0;
1309 if ((n = imsg_get(ibuf, &imsg)) == -1)
1310 fatal("%s: imsg_get error", __func__);
1311 if (n == 0) /* No more messages. */
1314 switch (imsg.hdr.type) {
1315 case GOTD_IMSG_ERROR:
1317 err = gotd_imsg_recv_error(&client_id, &imsg);
1319 case GOTD_IMSG_CLIENT_SESSION_READY:
1320 if (client->state != GOTD_CLIENT_STATE_ACCESS_GRANTED) {
1321 err = got_error(GOT_ERR_PRIVSEP_MSG);
1324 do_start_repo_child = 1;
1326 case GOTD_IMSG_DISCONNECT:
1330 log_debug("unexpected imsg %d", imsg.hdr.type);
1334 if (!verify_imsg_src(client, proc, &imsg)) {
1335 log_debug("dropping imsg type %d from PID %d",
1336 imsg.hdr.type, proc->pid);
1341 log_warnx("uid %d: %s", client->euid, err->msg);
1343 if (do_start_repo_child) {
1344 struct gotd_repo *repo;
1345 const char *name = client->session->repo_name;
1347 repo = gotd_find_repo_by_name(name, &gotd);
1349 enum gotd_procid proc_type;
1351 if (client->required_auth & GOTD_AUTH_WRITE)
1352 proc_type = PROC_REPO_WRITE;
1354 proc_type = PROC_REPO_READ;
1356 err = start_repo_child(client, proc_type, repo,
1357 gotd.argv0, gotd.confpath, gotd.daemonize,
1360 err = got_error(GOT_ERR_NOT_GIT_REPO);
1363 log_warnx("uid %d: %s", client->euid, err->msg);
1368 if (do_disconnect) {
1370 disconnect_on_error(client, err);
1379 gotd_imsg_event_add(iev);
1381 /* This pipe is dead. Remove its event handler */
1382 event_del(&iev->ev);
1388 gotd_dispatch_repo_child(int fd, short event, void *arg)
1390 struct gotd_imsgev *iev = arg;
1391 struct imsgbuf *ibuf = &iev->ibuf;
1392 struct gotd_child_proc *proc = NULL;
1393 struct gotd_client *client;
1398 client = find_client_by_proc_fd(fd);
1399 if (client == NULL) {
1400 /* Can happen during process teardown. */
1401 warnx("cannot find client for fd %d", fd);
1406 if (event & EV_READ) {
1407 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1408 fatal("imsg_read error");
1410 /* Connection closed. */
1416 if (event & EV_WRITE) {
1417 n = msgbuf_write(&ibuf->w);
1418 if (n == -1 && errno != EAGAIN)
1419 fatal("msgbuf_write");
1421 /* Connection closed. */
1427 proc = client->repo;
1429 fatalx("cannot find child process for fd %d", fd);
1432 const struct got_error *err = NULL;
1433 uint32_t client_id = 0;
1434 int do_disconnect = 0;
1436 if ((n = imsg_get(ibuf, &imsg)) == -1)
1437 fatal("%s: imsg_get error", __func__);
1438 if (n == 0) /* No more messages. */
1441 switch (imsg.hdr.type) {
1442 case GOTD_IMSG_ERROR:
1444 err = gotd_imsg_recv_error(&client_id, &imsg);
1446 case GOTD_IMSG_REPO_CHILD_READY:
1447 err = connect_session(client);
1450 err = connect_repo_child(client, proc);
1453 log_debug("unexpected imsg %d", imsg.hdr.type);
1457 if (!verify_imsg_src(client, proc, &imsg)) {
1458 log_debug("dropping imsg type %d from PID %d",
1459 imsg.hdr.type, proc->pid);
1464 log_warnx("uid %d: %s", client->euid, err->msg);
1466 if (do_disconnect) {
1468 disconnect_on_error(client, err);
1477 gotd_imsg_event_add(iev);
1479 /* This pipe is dead. Remove its event handler */
1480 event_del(&iev->ev);
1486 start_child(enum gotd_procid proc_id, const char *repo_path,
1487 char *argv0, const char *confpath, int fd, int daemonize, int verbosity)
1493 switch (pid = fork()) {
1495 fatal("cannot fork");
1503 if (fd != GOTD_FILENO_MSG_PIPE) {
1504 if (dup2(fd, GOTD_FILENO_MSG_PIPE) == -1)
1505 fatal("cannot setup imsg fd");
1506 } else if (fcntl(fd, F_SETFD, 0) == -1)
1507 fatal("cannot setup imsg fd");
1509 argv[argc++] = argv0;
1512 argv[argc++] = (char *)"-L";
1515 argv[argc++] = (char *)"-A";
1517 case PROC_SESSION_READ:
1518 argv[argc++] = (char *)"-s";
1520 case PROC_SESSION_WRITE:
1521 argv[argc++] = (char *)"-S";
1523 case PROC_REPO_READ:
1524 argv[argc++] = (char *)"-R";
1526 case PROC_REPO_WRITE:
1527 argv[argc++] = (char *)"-W";
1530 fatalx("invalid process id %d", proc_id);
1533 argv[argc++] = (char *)"-f";
1534 argv[argc++] = (char *)confpath;
1537 argv[argc++] = (char *)"-P";
1538 argv[argc++] = (char *)repo_path;
1542 argv[argc++] = (char *)"-d";
1544 argv[argc++] = (char *)"-v";
1546 argv[argc++] = (char *)"-v";
1547 argv[argc++] = NULL;
1549 execvp(argv0, argv);
1554 start_listener(char *argv0, const char *confpath, int daemonize, int verbosity)
1556 struct gotd_child_proc *proc;
1558 proc = calloc(1, sizeof(*proc));
1562 TAILQ_INSERT_HEAD(&procs, proc, entry);
1564 /* proc->tmo is initialized in main() after event_init() */
1566 proc->type = PROC_LISTEN;
1568 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1569 PF_UNSPEC, proc->pipe) == -1)
1570 fatal("socketpair");
1572 proc->pid = start_child(proc->type, NULL, argv0, confpath,
1573 proc->pipe[1], daemonize, verbosity);
1574 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1575 proc->iev.handler = gotd_dispatch_listener;
1576 proc->iev.events = EV_READ;
1577 proc->iev.handler_arg = NULL;
1579 gotd.listen_proc = proc;
1582 static const struct got_error *
1583 start_session_child(struct gotd_client *client, struct gotd_repo *repo,
1584 char *argv0, const char *confpath, int daemonize, int verbosity)
1586 struct gotd_child_proc *proc;
1588 proc = calloc(1, sizeof(*proc));
1590 return got_error_from_errno("calloc");
1592 TAILQ_INSERT_HEAD(&procs, proc, entry);
1593 evtimer_set(&proc->tmo, kill_proc_timeout, proc);
1595 if (client_is_reading(client))
1596 proc->type = PROC_SESSION_READ;
1598 proc->type = PROC_SESSION_WRITE;
1599 if (strlcpy(proc->repo_name, repo->name,
1600 sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
1601 fatalx("repository name too long: %s", repo->name);
1602 log_debug("starting client uid %d session for repository %s",
1603 client->euid, repo->name);
1604 if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >=
1605 sizeof(proc->repo_path))
1606 fatalx("repository path too long: %s", repo->path);
1607 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1608 PF_UNSPEC, proc->pipe) == -1)
1609 fatal("socketpair");
1610 proc->pid = start_child(proc->type, proc->repo_path, argv0,
1611 confpath, proc->pipe[1], daemonize, verbosity);
1612 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1613 log_debug("proc %s %s is on fd %d",
1614 gotd_proc_names[proc->type], proc->repo_path,
1616 proc->iev.handler = gotd_dispatch_client_session;
1617 proc->iev.events = EV_READ;
1618 proc->iev.handler_arg = NULL;
1619 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1620 gotd_dispatch_client_session, &proc->iev);
1621 gotd_imsg_event_add(&proc->iev);
1623 client->session = proc;
1627 static const struct got_error *
1628 start_repo_child(struct gotd_client *client, enum gotd_procid proc_type,
1629 struct gotd_repo *repo, char *argv0, const char *confpath,
1630 int daemonize, int verbosity)
1632 struct gotd_child_proc *proc;
1634 if (proc_type != PROC_REPO_READ && proc_type != PROC_REPO_WRITE)
1635 return got_error_msg(GOT_ERR_NOT_IMPL, "bad process type");
1637 proc = calloc(1, sizeof(*proc));
1639 return got_error_from_errno("calloc");
1641 TAILQ_INSERT_HEAD(&procs, proc, entry);
1642 evtimer_set(&proc->tmo, kill_proc_timeout, proc);
1644 proc->type = proc_type;
1645 if (strlcpy(proc->repo_name, repo->name,
1646 sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
1647 fatalx("repository name too long: %s", repo->name);
1648 log_debug("starting %s for repository %s",
1649 proc->type == PROC_REPO_READ ? "reader" : "writer", repo->name);
1650 if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >=
1651 sizeof(proc->repo_path))
1652 fatalx("repository path too long: %s", repo->path);
1653 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1654 PF_UNSPEC, proc->pipe) == -1)
1655 fatal("socketpair");
1656 proc->pid = start_child(proc->type, proc->repo_path, argv0,
1657 confpath, proc->pipe[1], daemonize, verbosity);
1658 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1659 log_debug("proc %s %s is on fd %d",
1660 gotd_proc_names[proc->type], proc->repo_path,
1662 proc->iev.handler = gotd_dispatch_repo_child;
1663 proc->iev.events = EV_READ;
1664 proc->iev.handler_arg = NULL;
1665 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1666 gotd_dispatch_repo_child, &proc->iev);
1667 gotd_imsg_event_add(&proc->iev);
1669 client->repo = proc;
1673 static const struct got_error *
1674 start_auth_child(struct gotd_client *client, int required_auth,
1675 struct gotd_repo *repo, char *argv0, const char *confpath,
1676 int daemonize, int verbosity)
1678 const struct got_error *err = NULL;
1679 struct gotd_child_proc *proc;
1680 struct gotd_imsg_auth iauth;
1683 memset(&iauth, 0, sizeof(iauth));
1685 fd = dup(client->fd);
1687 return got_error_from_errno("dup");
1689 proc = calloc(1, sizeof(*proc));
1691 err = got_error_from_errno("calloc");
1696 TAILQ_INSERT_HEAD(&procs, proc, entry);
1697 evtimer_set(&proc->tmo, kill_proc_timeout, proc);
1699 proc->type = PROC_AUTH;
1700 if (strlcpy(proc->repo_name, repo->name,
1701 sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
1702 fatalx("repository name too long: %s", repo->name);
1703 log_debug("starting auth for uid %d repository %s",
1704 client->euid, repo->name);
1705 if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >=
1706 sizeof(proc->repo_path))
1707 fatalx("repository path too long: %s", repo->path);
1708 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1709 PF_UNSPEC, proc->pipe) == -1)
1710 fatal("socketpair");
1711 proc->pid = start_child(proc->type, proc->repo_path, argv0,
1712 confpath, proc->pipe[1], daemonize, verbosity);
1713 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1714 log_debug("proc %s %s is on fd %d",
1715 gotd_proc_names[proc->type], proc->repo_path,
1717 proc->iev.handler = gotd_dispatch_auth_child;
1718 proc->iev.events = EV_READ;
1719 proc->iev.handler_arg = NULL;
1720 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1721 gotd_dispatch_auth_child, &proc->iev);
1722 gotd_imsg_event_add(&proc->iev);
1724 iauth.euid = client->euid;
1725 iauth.egid = client->egid;
1726 iauth.required_auth = required_auth;
1727 iauth.client_id = client->id;
1728 if (gotd_imsg_compose_event(&proc->iev, GOTD_IMSG_AUTHENTICATE,
1729 PROC_GOTD, fd, &iauth, sizeof(iauth)) == -1) {
1730 log_warn("imsg compose AUTHENTICATE");
1732 /* Let the auth_timeout handler tidy up. */
1735 client->auth = proc;
1736 client->required_auth = required_auth;
1741 apply_unveil_repo_readonly(const char *repo_path, int need_tmpdir)
1744 if (unveil(GOT_TMPDIR_STR, "rwc") == -1)
1745 fatal("unveil %s", GOT_TMPDIR_STR);
1748 if (unveil(repo_path, "r") == -1)
1749 fatal("unveil %s", repo_path);
1751 if (unveil(NULL, NULL) == -1)
1756 apply_unveil_repo_readwrite(const char *repo_path)
1758 if (unveil(repo_path, "rwc") == -1)
1759 fatal("unveil %s", repo_path);
1761 if (unveil(GOT_TMPDIR_STR, "rwc") == -1)
1762 fatal("unveil %s", GOT_TMPDIR_STR);
1764 if (unveil(NULL, NULL) == -1)
1769 apply_unveil_none(void)
1771 if (unveil("/", "") == -1)
1774 if (unveil(NULL, NULL) == -1)
1779 apply_unveil_selfexec(void)
1781 if (unveil(gotd.argv0, "x") == -1)
1782 fatal("unveil %s", gotd.argv0);
1784 if (unveil(NULL, NULL) == -1)
1789 main(int argc, char **argv)
1791 const struct got_error *error = NULL;
1792 int ch, fd = -1, daemonize = 1, verbosity = 0, noaction = 0;
1793 const char *confpath = GOTD_CONF_PATH;
1794 char *argv0 = argv[0];
1796 struct passwd *pw = NULL;
1797 char *repo_path = NULL;
1798 enum gotd_procid proc_id = PROC_GOTD;
1799 struct event evsigint, evsigterm, evsighup, evsigusr1, evsigchld;
1800 int *pack_fds = NULL, *temp_fds = NULL;
1801 struct gotd_repo *repo = NULL;
1805 log_init(1, LOG_DAEMON); /* Log to stderr until daemonized. */
1807 while ((ch = getopt(argc, argv, "Adf:LnP:RsSvW")) != -1) {
1810 proc_id = PROC_AUTH;
1819 proc_id = PROC_LISTEN;
1825 repo_path = realpath(optarg, NULL);
1826 if (repo_path == NULL)
1827 fatal("realpath '%s'", optarg);
1830 proc_id = PROC_REPO_READ;
1833 proc_id = PROC_SESSION_READ;
1836 proc_id = PROC_SESSION_WRITE;
1843 proc_id = PROC_REPO_WRITE;
1856 if (geteuid() && (proc_id == PROC_GOTD || proc_id == PROC_LISTEN))
1857 fatalx("need root privileges");
1859 if (parse_config(confpath, proc_id, &gotd) != 0)
1862 pw = getpwnam(gotd.user_name);
1864 fatalx("user %s not found", gotd.user_name);
1866 if (pw->pw_uid == 0)
1867 fatalx("cannot run %s as the superuser", getprogname());
1870 fprintf(stderr, "configuration OK\n");
1875 gotd.daemonize = daemonize;
1876 gotd.verbosity = verbosity;
1877 gotd.confpath = confpath;
1879 /* Require an absolute path in argv[0] for reliable re-exec. */
1880 if (!got_path_is_absolute(argv0))
1881 fatalx("bad path \"%s\": must be an absolute path", argv0);
1883 log_init(daemonize ? 0 : 1, LOG_DAEMON);
1884 log_setverbose(verbosity);
1886 if (proc_id == PROC_GOTD) {
1887 snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
1888 arc4random_buf(&clients_hash_key, sizeof(clients_hash_key));
1889 if (daemonize && daemon(1, 0) == -1)
1891 gotd.pid = getpid();
1892 start_listener(argv0, confpath, daemonize, verbosity);
1893 } else if (proc_id == PROC_LISTEN) {
1894 snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
1896 log_info("socket: %s", gotd.unix_socket_path);
1897 log_info("user: %s", pw->pw_name);
1900 fd = unix_socket_listen(gotd.unix_socket_path, pw->pw_uid,
1903 fatal("cannot listen on unix socket %s",
1904 gotd.unix_socket_path);
1906 } else if (proc_id == PROC_AUTH) {
1907 snprintf(title, sizeof(title), "%s %s",
1908 gotd_proc_names[proc_id], repo_path);
1909 } else if (proc_id == PROC_REPO_READ || proc_id == PROC_REPO_WRITE ||
1910 proc_id == PROC_SESSION_READ || proc_id == PROC_SESSION_WRITE) {
1911 error = got_repo_pack_fds_open(&pack_fds);
1913 fatalx("cannot open pack tempfiles: %s", error->msg);
1914 error = got_repo_temp_fds_open(&temp_fds);
1916 fatalx("cannot open pack tempfiles: %s", error->msg);
1917 if (repo_path == NULL)
1918 fatalx("repository path not specified");
1919 snprintf(title, sizeof(title), "%s %s",
1920 gotd_proc_names[proc_id], repo_path);
1922 fatal("invalid process id %d", proc_id);
1924 setproctitle("%s", title);
1925 log_procinit(title);
1927 /* Drop root privileges. */
1928 if (setgid(pw->pw_gid) == -1)
1929 fatal("setgid %d failed", pw->pw_gid);
1930 if (setuid(pw->pw_uid) == -1)
1931 fatal("setuid %d failed", pw->pw_uid);
1938 /* "exec" promise will be limited to argv[0] via unveil(2). */
1939 if (pledge("stdio proc exec sendfd recvfd unveil", NULL) == -1)
1945 if (pledge("stdio sendfd unix unveil", NULL) == -1)
1949 * Ensure that AF_UNIX bind(2) cannot be used with any other
1950 * sockets by revoking all filesystem access via unveil(2).
1952 apply_unveil_none();
1954 listen_main(title, fd, gotd.connection_limits,
1955 gotd.nconnection_limits);
1960 if (pledge("stdio getpw recvfd unix unveil", NULL) == -1)
1964 * We need the "unix" pledge promise for getpeername(2) only.
1965 * Ensure that AF_UNIX bind(2) cannot be used by revoking all
1966 * filesystem access via unveil(2). Access to password database
1967 * files will still work since "getpw" bypasses unveil(2).
1969 apply_unveil_none();
1971 auth_main(title, &gotd.repos, repo_path);
1974 case PROC_SESSION_READ:
1975 case PROC_SESSION_WRITE:
1978 * The "recvfd" promise is only needed during setup and
1979 * will be removed in a later pledge(2) call.
1981 if (pledge("stdio rpath wpath cpath recvfd sendfd fattr flock "
1982 "unveil", NULL) == -1)
1985 if (proc_id == PROC_SESSION_READ)
1986 apply_unveil_repo_readonly(repo_path, 1);
1988 apply_unveil_repo_readwrite(repo_path);
1989 session_main(title, repo_path, pack_fds, temp_fds,
1990 &gotd.request_timeout, proc_id);
1993 case PROC_REPO_READ:
1995 if (pledge("stdio rpath recvfd unveil", NULL) == -1)
1998 apply_unveil_repo_readonly(repo_path, 0);
1999 repo_read_main(title, repo_path, pack_fds, temp_fds);
2002 case PROC_REPO_WRITE:
2004 if (pledge("stdio rpath recvfd unveil", NULL) == -1)
2007 apply_unveil_repo_readonly(repo_path, 0);
2008 repo = gotd_find_repo_by_path(repo_path, &gotd);
2010 fatalx("no repository for path %s", repo_path);
2011 repo_write_main(title, repo_path, pack_fds, temp_fds,
2012 &repo->protected_tag_namespaces,
2013 &repo->protected_branch_namespaces,
2014 &repo->protected_branches);
2018 fatal("invalid process id %d", proc_id);
2021 if (proc_id != PROC_GOTD)
2022 fatal("invalid process id %d", proc_id);
2024 evtimer_set(&gotd.listen_proc->tmo, kill_proc_timeout,
2027 apply_unveil_selfexec();
2029 signal_set(&evsigint, SIGINT, gotd_sighdlr, NULL);
2030 signal_set(&evsigterm, SIGTERM, gotd_sighdlr, NULL);
2031 signal_set(&evsighup, SIGHUP, gotd_sighdlr, NULL);
2032 signal_set(&evsigusr1, SIGUSR1, gotd_sighdlr, NULL);
2033 signal_set(&evsigchld, SIGCHLD, gotd_sighdlr, NULL);
2034 signal(SIGPIPE, SIG_IGN);
2036 signal_add(&evsigint, NULL);
2037 signal_add(&evsigterm, NULL);
2038 signal_add(&evsighup, NULL);
2039 signal_add(&evsigusr1, NULL);
2040 signal_add(&evsigchld, NULL);
2042 gotd_imsg_event_add(&gotd.listen_proc->iev);