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,
76 struct gotd_child_proc {
78 enum gotd_procid type;
79 char repo_name[NAME_MAX];
80 char repo_path[PATH_MAX];
82 struct gotd_imsgev iev;
85 TAILQ_ENTRY(gotd_child_proc) entry;
87 TAILQ_HEAD(gotd_procs, gotd_child_proc) procs;
90 STAILQ_ENTRY(gotd_client) entry;
91 enum gotd_client_state state;
94 struct gotd_imsgev iev;
98 struct gotd_child_proc *repo;
99 struct gotd_child_proc *auth;
100 struct gotd_child_proc *session;
103 STAILQ_HEAD(gotd_clients, gotd_client);
105 static struct gotd_clients gotd_clients[GOTD_CLIENT_TABLE_SIZE];
106 static SIPHASH_KEY clients_hash_key;
107 volatile int client_cnt;
108 static struct timeval auth_timeout = { 5, 0 };
109 static struct gotd gotd;
111 void gotd_sighdlr(int sig, short event, void *arg);
112 static void gotd_shutdown(void);
113 static const struct got_error *start_session_child(struct gotd_client *,
114 struct gotd_repo *, char *, const char *, int, int);
115 static const struct got_error *start_repo_child(struct gotd_client *,
116 enum gotd_procid, struct gotd_repo *, char *, const char *, int, int);
117 static const struct got_error *start_auth_child(struct gotd_client *, int,
118 struct gotd_repo *, char *, const char *, int, int);
119 static void kill_proc(struct gotd_child_proc *, int);
120 static void disconnect(struct gotd_client *);
125 fprintf(stderr, "usage: %s [-dnv] [-f config-file]\n", getprogname());
130 unix_socket_listen(const char *unix_socket_path, uid_t uid, gid_t gid)
132 struct sockaddr_un sun;
134 mode_t old_umask, mode;
136 fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK| SOCK_CLOEXEC, 0);
142 sun.sun_family = AF_UNIX;
143 if (strlcpy(sun.sun_path, unix_socket_path,
144 sizeof(sun.sun_path)) >= sizeof(sun.sun_path)) {
145 log_warnx("%s: name too long", unix_socket_path);
150 if (unlink(unix_socket_path) == -1) {
151 if (errno != ENOENT) {
152 log_warn("unlink %s", unix_socket_path);
158 old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
159 mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
161 if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
162 log_warn("bind: %s", unix_socket_path);
170 if (chmod(unix_socket_path, mode) == -1) {
171 log_warn("chmod %o %s", mode, unix_socket_path);
173 unlink(unix_socket_path);
177 if (chown(unix_socket_path, uid, gid) == -1) {
178 log_warn("chown %s uid=%d gid=%d", unix_socket_path, uid, gid);
180 unlink(unix_socket_path);
184 if (listen(fd, GOTD_UNIX_SOCKET_BACKLOG) == -1) {
187 unlink(unix_socket_path);
195 client_hash(uint32_t client_id)
197 return SipHash24(&clients_hash_key, &client_id, sizeof(client_id));
201 add_client(struct gotd_client *client)
203 uint64_t slot = client_hash(client->id) % nitems(gotd_clients);
204 STAILQ_INSERT_HEAD(&gotd_clients[slot], client, entry);
208 static struct gotd_client *
209 find_client(uint32_t client_id)
212 struct gotd_client *c;
214 slot = client_hash(client_id) % nitems(gotd_clients);
215 STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
216 if (c->id == client_id)
223 static struct gotd_client *
224 find_client_by_proc_fd(int fd)
228 for (slot = 0; slot < nitems(gotd_clients); slot++) {
229 struct gotd_client *c;
231 STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
232 if (c->repo && c->repo->iev.ibuf.fd == fd)
234 if (c->auth && c->auth->iev.ibuf.fd == fd)
236 if (c->session && c->session->iev.ibuf.fd == fd)
245 client_is_reading(struct gotd_client *client)
247 return (client->required_auth &
248 (GOTD_AUTH_READ | GOTD_AUTH_WRITE)) == GOTD_AUTH_READ;
252 client_is_writing(struct gotd_client *client)
254 return (client->required_auth &
255 (GOTD_AUTH_READ | GOTD_AUTH_WRITE)) ==
256 (GOTD_AUTH_READ | GOTD_AUTH_WRITE);
259 static const struct got_error *
260 ensure_client_is_not_writing(struct gotd_client *client)
262 if (client_is_writing(client)) {
263 return got_error_fmt(GOT_ERR_BAD_PACKET,
264 "uid %d made a read-request but is writing to "
265 "a repository", client->euid);
271 static const struct got_error *
272 ensure_client_is_not_reading(struct gotd_client *client)
274 if (client_is_reading(client)) {
275 return got_error_fmt(GOT_ERR_BAD_PACKET,
276 "uid %d made a write-request but is reading from "
277 "a repository", client->euid);
284 proc_done(struct gotd_child_proc *proc)
286 struct gotd_client *client;
288 TAILQ_REMOVE(&procs, proc, entry);
290 client = find_client_by_proc_fd(proc->iev.ibuf.fd);
291 if (client != NULL) {
292 if (proc == client->repo)
294 if (proc == client->auth)
296 if (proc == client->session)
297 client->session = NULL;
301 evtimer_del(&proc->tmo);
303 if (proc->iev.ibuf.fd != -1) {
304 event_del(&proc->iev.ev);
305 msgbuf_clear(&proc->iev.ibuf.w);
306 close(proc->iev.ibuf.fd);
313 kill_repo_proc(struct gotd_client *client)
315 if (client->repo == NULL)
318 kill_proc(client->repo, 0);
323 kill_auth_proc(struct gotd_client *client)
325 if (client->auth == NULL)
328 kill_proc(client->auth, 0);
333 kill_session_proc(struct gotd_client *client)
335 if (client->session == NULL)
338 kill_proc(client->session, 0);
339 client->session = NULL;
343 disconnect(struct gotd_client *client)
345 struct gotd_imsg_disconnect idisconnect;
346 struct gotd_child_proc *listen_proc = gotd.listen_proc;
349 log_debug("uid %d: disconnecting", client->euid);
351 kill_auth_proc(client);
352 kill_session_proc(client);
353 kill_repo_proc(client);
355 idisconnect.client_id = client->id;
356 if (gotd_imsg_compose_event(&listen_proc->iev,
357 GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
358 &idisconnect, sizeof(idisconnect)) == -1)
359 log_warn("imsg compose DISCONNECT");
361 slot = client_hash(client->id) % nitems(gotd_clients);
362 STAILQ_REMOVE(&gotd_clients[slot], client, gotd_client, entry);
363 imsg_clear(&client->iev.ibuf);
364 event_del(&client->iev.ev);
365 evtimer_del(&client->tmo);
366 if (client->fd != -1)
368 else if (client->iev.ibuf.fd != -1)
369 close(client->iev.ibuf.fd);
375 disconnect_on_error(struct gotd_client *client, const struct got_error *err)
379 log_warnx("uid %d: %s", client->euid, err->msg);
380 if (err->code != GOT_ERR_EOF && client->fd != -1) {
381 imsg_init(&ibuf, client->fd);
382 gotd_imsg_send_error(&ibuf, 0, PROC_GOTD, err);
388 static const struct got_error *
389 send_repo_info(struct gotd_imsgev *iev, struct gotd_repo *repo)
391 const struct got_error *err = NULL;
392 struct gotd_imsg_info_repo irepo;
394 memset(&irepo, 0, sizeof(irepo));
396 if (strlcpy(irepo.repo_name, repo->name, sizeof(irepo.repo_name))
397 >= sizeof(irepo.repo_name))
398 return got_error_msg(GOT_ERR_NO_SPACE, "repo name too long");
399 if (strlcpy(irepo.repo_path, repo->path, sizeof(irepo.repo_path))
400 >= sizeof(irepo.repo_path))
401 return got_error_msg(GOT_ERR_NO_SPACE, "repo path too long");
403 if (gotd_imsg_compose_event(iev, GOTD_IMSG_INFO_REPO, PROC_GOTD, -1,
404 &irepo, sizeof(irepo)) == -1) {
405 err = got_error_from_errno("imsg compose INFO_REPO");
413 static const struct got_error *
414 send_client_info(struct gotd_imsgev *iev, struct gotd_client *client)
416 const struct got_error *err = NULL;
417 struct gotd_imsg_info_client iclient;
418 struct gotd_child_proc *proc;
420 memset(&iclient, 0, sizeof(iclient));
421 iclient.euid = client->euid;
422 iclient.egid = client->egid;
426 if (strlcpy(iclient.repo_name, proc->repo_path,
427 sizeof(iclient.repo_name)) >= sizeof(iclient.repo_name)) {
428 return got_error_msg(GOT_ERR_NO_SPACE,
429 "repo name too long");
431 if (client_is_writing(client))
432 iclient.is_writing = 1;
434 iclient.repo_child_pid = proc->pid;
438 iclient.session_child_pid = client->session->pid;
440 if (gotd_imsg_compose_event(iev, GOTD_IMSG_INFO_CLIENT, PROC_GOTD, -1,
441 &iclient, sizeof(iclient)) == -1) {
442 err = got_error_from_errno("imsg compose INFO_CLIENT");
450 static const struct got_error *
451 send_info(struct gotd_client *client)
453 const struct got_error *err = NULL;
454 struct gotd_imsg_info info;
456 struct gotd_repo *repo;
458 if (client->euid != 0)
459 return got_error_set_errno(EPERM, "info");
462 info.verbosity = gotd.verbosity;
463 info.nrepos = gotd.nrepos;
464 info.nclients = client_cnt - 1;
466 if (gotd_imsg_compose_event(&client->iev, GOTD_IMSG_INFO, PROC_GOTD, -1,
467 &info, sizeof(info)) == -1) {
468 err = got_error_from_errno("imsg compose INFO");
473 TAILQ_FOREACH(repo, &gotd.repos, entry) {
474 err = send_repo_info(&client->iev, repo);
479 for (slot = 0; slot < nitems(gotd_clients); slot++) {
480 struct gotd_client *c;
481 STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
482 if (c->id == client->id)
484 err = send_client_info(&client->iev, c);
493 static const struct got_error *
494 stop_gotd(struct gotd_client *client)
497 if (client->euid != 0)
498 return got_error_set_errno(EPERM, "stop");
505 static const struct got_error *
506 start_client_authentication(struct gotd_client *client, struct imsg *imsg)
508 const struct got_error *err;
509 struct gotd_imsg_list_refs ireq;
510 struct gotd_repo *repo = NULL;
513 log_debug("list-refs request from uid %d", client->euid);
515 if (client->state != GOTD_CLIENT_STATE_NEW)
516 return got_error_msg(GOT_ERR_BAD_REQUEST,
517 "unexpected list-refs request received");
519 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
520 if (datalen != sizeof(ireq))
521 return got_error(GOT_ERR_PRIVSEP_LEN);
523 memcpy(&ireq, imsg->data, datalen);
525 if (ireq.client_is_reading) {
526 err = ensure_client_is_not_writing(client);
529 repo = gotd_find_repo_by_name(ireq.repo_name, &gotd);
531 return got_error(GOT_ERR_NOT_GIT_REPO);
532 err = start_auth_child(client, GOTD_AUTH_READ, repo,
533 gotd.argv0, gotd.confpath, gotd.daemonize,
538 err = ensure_client_is_not_reading(client);
541 repo = gotd_find_repo_by_name(ireq.repo_name, &gotd);
543 return got_error(GOT_ERR_NOT_GIT_REPO);
544 err = start_auth_child(client,
545 GOTD_AUTH_READ | GOTD_AUTH_WRITE,
546 repo, gotd.argv0, gotd.confpath, gotd.daemonize,
552 evtimer_add(&client->tmo, &auth_timeout);
554 /* Flow continues upon authentication successs/failure or timeout. */
559 gotd_request(int fd, short events, void *arg)
561 struct gotd_imsgev *iev = arg;
562 struct imsgbuf *ibuf = &iev->ibuf;
563 struct gotd_client *client = iev->handler_arg;
564 const struct got_error *err = NULL;
568 if (events & EV_WRITE) {
569 while (ibuf->w.queued) {
570 n = msgbuf_write(&ibuf->w);
571 if (n == -1 && errno == EPIPE) {
573 * The client has closed its socket.
574 * This can happen when Git clients are
575 * done sending pack file data.
577 msgbuf_clear(&ibuf->w);
579 } else if (n == -1 && errno != EAGAIN) {
580 err = got_error_from_errno("imsg_flush");
581 disconnect_on_error(client, err);
585 /* Connection closed. */
586 err = got_error(GOT_ERR_EOF);
587 disconnect_on_error(client, err);
592 /* Disconnect gotctl(8) now that messages have been sent. */
593 if (!client_is_reading(client) && !client_is_writing(client)) {
599 if ((events & EV_READ) == 0)
602 memset(&imsg, 0, sizeof(imsg));
604 while (err == NULL) {
605 err = gotd_imsg_recv(&imsg, ibuf, 0);
607 if (err->code == GOT_ERR_PRIVSEP_READ)
612 evtimer_del(&client->tmo);
614 switch (imsg.hdr.type) {
616 err = send_info(client);
619 err = stop_gotd(client);
621 case GOTD_IMSG_LIST_REFS:
622 err = start_client_authentication(client, &imsg);
625 log_debug("unexpected imsg %d", imsg.hdr.type);
626 err = got_error(GOT_ERR_PRIVSEP_MSG);
634 disconnect_on_error(client, err);
636 gotd_imsg_event_add(&client->iev);
641 gotd_auth_timeout(int fd, short events, void *arg)
643 struct gotd_client *client = arg;
645 log_debug("disconnecting uid %d due to authentication timeout",
650 static const struct got_error *
651 recv_connect(uint32_t *client_id, struct imsg *imsg)
653 const struct got_error *err = NULL;
654 struct gotd_imsg_connect iconnect;
657 struct gotd_client *client = NULL;
661 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
662 if (datalen != sizeof(iconnect))
663 return got_error(GOT_ERR_PRIVSEP_LEN);
664 memcpy(&iconnect, imsg->data, sizeof(iconnect));
668 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
672 if (find_client(iconnect.client_id)) {
673 err = got_error_msg(GOT_ERR_CLIENT_ID, "duplicate client ID");
677 client = calloc(1, sizeof(*client));
678 if (client == NULL) {
679 err = got_error_from_errno("calloc");
683 *client_id = iconnect.client_id;
685 client->state = GOTD_CLIENT_STATE_NEW;
686 client->id = iconnect.client_id;
689 /* The auth process will verify UID/GID for us. */
690 client->euid = iconnect.euid;
691 client->egid = iconnect.egid;
693 imsg_init(&client->iev.ibuf, client->fd);
694 client->iev.handler = gotd_request;
695 client->iev.events = EV_READ;
696 client->iev.handler_arg = client;
698 event_set(&client->iev.ev, client->fd, EV_READ, gotd_request,
700 gotd_imsg_event_add(&client->iev);
702 evtimer_set(&client->tmo, gotd_auth_timeout, client);
705 log_debug("%s: new client uid %d connected on fd %d", __func__,
706 client->euid, client->fd);
709 struct gotd_child_proc *listen_proc = gotd.listen_proc;
710 struct gotd_imsg_disconnect idisconnect;
712 idisconnect.client_id = client->id;
713 if (gotd_imsg_compose_event(&listen_proc->iev,
714 GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
715 &idisconnect, sizeof(idisconnect)) == -1)
716 log_warn("imsg compose DISCONNECT");
725 static const char *gotd_proc_names[PROC_MAX] = {
737 kill_proc(struct gotd_child_proc *proc, int fatal)
739 struct timeval tv = { 5, 0 };
741 log_debug("kill -%d %d", fatal ? SIGKILL : SIGTERM, proc->pid);
743 if (proc->iev.ibuf.fd != -1) {
744 event_del(&proc->iev.ev);
745 msgbuf_clear(&proc->iev.ibuf.w);
746 close(proc->iev.ibuf.fd);
747 proc->iev.ibuf.fd = -1;
750 if (!evtimer_pending(&proc->tmo, NULL) && !fatal)
751 evtimer_add(&proc->tmo, &tv);
754 log_warnx("sending SIGKILL to PID %d", proc->pid);
755 kill(proc->pid, SIGKILL);
757 kill(proc->pid, SIGTERM);
761 kill_proc_timeout(int fd, short ev, void *d)
763 struct gotd_child_proc *proc = d;
765 log_warnx("timeout waiting for PID %d to terminate;"
766 " retrying with force", proc->pid);
775 log_debug("shutting down");
776 for (slot = 0; slot < nitems(gotd_clients); slot++) {
777 struct gotd_client *c, *tmp;
779 STAILQ_FOREACH_SAFE(c, &gotd_clients[slot], entry, tmp)
783 kill_proc(gotd.listen_proc, 0);
785 log_info("terminating");
789 static struct gotd_child_proc *
790 find_proc_by_pid(pid_t pid)
792 struct gotd_child_proc *proc = NULL;
794 TAILQ_FOREACH(proc, &procs, entry)
795 if (proc->pid == pid)
802 gotd_sighdlr(int sig, short event, void *arg)
804 struct gotd_child_proc *proc;
809 * Normal signal handler rules don't apply because libevent
815 log_info("%s: ignoring SIGHUP", __func__);
818 log_info("%s: ignoring SIGUSR1", __func__);
826 pid = waitpid(WAIT_ANY, &status, WNOHANG);
837 log_debug("reaped pid %d", pid);
838 proc = find_proc_by_pid(pid);
840 log_info("caught exit of unknown child %d",
845 if (WIFSIGNALED(status)) {
846 log_warnx("child PID %d terminated with"
847 " signal %d", pid, WTERMSIG(status));
854 fatalx("unexpected signal");
858 static const struct got_error *
859 ensure_proc_is_reading(struct gotd_client *client,
860 struct gotd_child_proc *proc)
862 if (!client_is_reading(client)) {
864 return got_error_fmt(GOT_ERR_BAD_PACKET,
865 "PID %d handled a read-request for uid %d but this "
866 "user is not reading from a repository", proc->pid,
873 static const struct got_error *
874 ensure_proc_is_writing(struct gotd_client *client,
875 struct gotd_child_proc *proc)
877 if (!client_is_writing(client)) {
879 return got_error_fmt(GOT_ERR_BAD_PACKET,
880 "PID %d handled a write-request for uid %d but this "
881 "user is not writing to a repository", proc->pid,
889 verify_imsg_src(struct gotd_client *client, struct gotd_child_proc *proc,
892 const struct got_error *err;
895 if (proc->type == PROC_REPO_READ || proc->type == PROC_REPO_WRITE) {
896 if (client->repo == NULL)
897 fatalx("no process found for uid %d", client->euid);
898 if (proc->pid != client->repo->pid) {
900 log_warnx("received message from PID %d for uid %d, "
901 "while PID %d is the process serving this user",
902 proc->pid, client->euid, client->repo->pid);
906 if (proc->type == PROC_SESSION_READ ||
907 proc->type == PROC_SESSION_WRITE) {
908 if (client->session == NULL) {
909 log_warnx("no session found for uid %d", client->euid);
912 if (proc->pid != client->session->pid) {
914 log_warnx("received message from PID %d for uid %d, "
915 "while PID %d is the process serving this user",
916 proc->pid, client->euid, client->session->pid);
921 switch (imsg->hdr.type) {
922 case GOTD_IMSG_ERROR:
925 case GOTD_IMSG_CONNECT:
926 if (proc->type != PROC_LISTEN) {
927 err = got_error_fmt(GOT_ERR_BAD_PACKET,
928 "new connection for uid %d from PID %d "
929 "which is not the listen process",
930 proc->pid, client->euid);
934 case GOTD_IMSG_ACCESS_GRANTED:
935 if (proc->type != PROC_AUTH) {
936 err = got_error_fmt(GOT_ERR_BAD_PACKET,
937 "authentication of uid %d from PID %d "
938 "which is not the auth process",
939 proc->pid, client->euid);
943 case GOTD_IMSG_CLIENT_SESSION_READY:
944 if (proc->type != PROC_SESSION_READ &&
945 proc->type != PROC_SESSION_WRITE) {
946 err = got_error_fmt(GOT_ERR_BAD_PACKET,
947 "unexpected \"ready\" signal from PID %d",
952 case GOTD_IMSG_REPO_CHILD_READY:
953 if (proc->type != PROC_REPO_READ &&
954 proc->type != PROC_REPO_WRITE) {
955 err = got_error_fmt(GOT_ERR_BAD_PACKET,
956 "unexpected \"ready\" signal from PID %d",
961 case GOTD_IMSG_PACKFILE_DONE:
962 err = ensure_proc_is_reading(client, proc);
964 log_warnx("uid %d: %s", client->euid, err->msg);
968 case GOTD_IMSG_PACKFILE_INSTALL:
969 case GOTD_IMSG_REF_UPDATES_START:
970 case GOTD_IMSG_REF_UPDATE:
971 err = ensure_proc_is_writing(client, proc);
973 log_warnx("uid %d: %s", client->euid, err->msg);
978 log_debug("%s: unexpected imsg %d", __func__, imsg->hdr.type);
985 static const struct got_error *
986 connect_repo_child(struct gotd_client *client,
987 struct gotd_child_proc *repo_proc)
989 static const struct got_error *err;
990 struct gotd_imsgev *session_iev = &client->session->iev;
991 struct gotd_imsg_connect_repo_child ireq;
994 if (client->state != GOTD_CLIENT_STATE_ACCESS_GRANTED)
995 return got_error_msg(GOT_ERR_BAD_REQUEST,
996 "unexpected repo child ready signal received");
998 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
999 PF_UNSPEC, pipe) == -1)
1000 fatal("socketpair");
1002 memset(&ireq, 0, sizeof(ireq));
1003 ireq.client_id = client->id;
1004 ireq.proc_id = repo_proc->type;
1006 /* Pass repo child pipe to session child process. */
1007 if (gotd_imsg_compose_event(session_iev, GOTD_IMSG_CONNECT_REPO_CHILD,
1008 PROC_GOTD, pipe[0], &ireq, sizeof(ireq)) == -1) {
1009 err = got_error_from_errno("imsg compose CONNECT_REPO_CHILD");
1015 /* Pass session child pipe to repo child process. */
1016 if (gotd_imsg_compose_event(&repo_proc->iev,
1017 GOTD_IMSG_CONNECT_REPO_CHILD, PROC_GOTD, pipe[1], NULL, 0) == -1) {
1018 err = got_error_from_errno("imsg compose CONNECT_REPO_CHILD");
1027 gotd_dispatch_listener(int fd, short event, void *arg)
1029 struct gotd_imsgev *iev = arg;
1030 struct imsgbuf *ibuf = &iev->ibuf;
1031 struct gotd_child_proc *proc = gotd.listen_proc;
1036 if (proc->iev.ibuf.fd != fd)
1037 fatalx("%s: unexpected fd %d", __func__, fd);
1039 if (event & EV_READ) {
1040 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1041 fatal("imsg_read error");
1043 /* Connection closed. */
1049 if (event & EV_WRITE) {
1050 n = msgbuf_write(&ibuf->w);
1051 if (n == -1 && errno != EAGAIN)
1052 fatal("msgbuf_write");
1054 /* Connection closed. */
1061 const struct got_error *err = NULL;
1062 struct gotd_client *client = NULL;
1063 uint32_t client_id = 0;
1064 int do_disconnect = 0;
1066 if ((n = imsg_get(ibuf, &imsg)) == -1)
1067 fatal("%s: imsg_get error", __func__);
1068 if (n == 0) /* No more messages. */
1071 switch (imsg.hdr.type) {
1072 case GOTD_IMSG_ERROR:
1074 err = gotd_imsg_recv_error(&client_id, &imsg);
1076 case GOTD_IMSG_CONNECT:
1077 err = recv_connect(&client_id, &imsg);
1080 log_debug("unexpected imsg %d", imsg.hdr.type);
1084 client = find_client(client_id);
1085 if (client == NULL) {
1086 log_warnx("%s: client not found", __func__);
1092 log_warnx("uid %d: %s", client->euid, err->msg);
1094 if (do_disconnect) {
1096 disconnect_on_error(client, err);
1105 gotd_imsg_event_add(iev);
1107 /* This pipe is dead. Remove its event handler */
1108 event_del(&iev->ev);
1109 event_loopexit(NULL);
1114 gotd_dispatch_auth_child(int fd, short event, void *arg)
1116 const struct got_error *err = NULL;
1117 struct gotd_imsgev *iev = arg;
1118 struct imsgbuf *ibuf = &iev->ibuf;
1119 struct gotd_client *client;
1120 struct gotd_repo *repo = NULL;
1124 uint32_t client_id = 0;
1125 int do_disconnect = 0;
1127 client = find_client_by_proc_fd(fd);
1128 if (client == NULL) {
1129 /* Can happen during process teardown. */
1130 warnx("cannot find client for fd %d", fd);
1135 if (client->auth == NULL)
1136 fatalx("cannot find auth child process for fd %d", fd);
1138 if (event & EV_READ) {
1139 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1140 fatal("imsg_read error");
1142 /* Connection closed. */
1148 if (event & EV_WRITE) {
1149 n = msgbuf_write(&ibuf->w);
1150 if (n == -1 && errno != EAGAIN)
1151 fatal("msgbuf_write");
1153 /* Connection closed. */
1159 if (client->auth->iev.ibuf.fd != fd)
1160 fatalx("%s: unexpected fd %d", __func__, fd);
1162 if ((n = imsg_get(ibuf, &imsg)) == -1)
1163 fatal("%s: imsg_get error", __func__);
1164 if (n == 0) /* No more messages. */
1167 evtimer_del(&client->tmo);
1169 switch (imsg.hdr.type) {
1170 case GOTD_IMSG_ERROR:
1172 err = gotd_imsg_recv_error(&client_id, &imsg);
1174 case GOTD_IMSG_ACCESS_GRANTED:
1175 client->state = GOTD_CLIENT_STATE_ACCESS_GRANTED;
1179 log_debug("unexpected imsg %d", imsg.hdr.type);
1183 if (!verify_imsg_src(client, client->auth, &imsg)) {
1185 log_debug("dropping imsg type %d from PID %d",
1186 imsg.hdr.type, client->auth->pid);
1190 if (do_disconnect) {
1192 disconnect_on_error(client, err);
1198 repo = gotd_find_repo_by_name(client->auth->repo_name, &gotd);
1200 err = got_error(GOT_ERR_NOT_GIT_REPO);
1203 kill_auth_proc(client);
1205 log_info("authenticated uid %d for repository %s",
1206 client->euid, repo->name);
1208 err = start_session_child(client, repo, gotd.argv0,
1209 gotd.confpath, gotd.daemonize, gotd.verbosity);
1214 log_warnx("uid %d: %s", client->euid, err->msg);
1216 /* We might have killed the auth process by now. */
1217 if (client->auth != NULL) {
1219 gotd_imsg_event_add(iev);
1221 /* This pipe is dead. Remove its event handler */
1222 event_del(&iev->ev);
1227 static const struct got_error *
1228 connect_session(struct gotd_client *client)
1230 const struct got_error *err = NULL;
1231 struct gotd_imsg_connect iconnect;
1234 memset(&iconnect, 0, sizeof(iconnect));
1236 s = dup(client->fd);
1238 return got_error_from_errno("dup");
1240 iconnect.client_id = client->id;
1241 iconnect.euid = client->euid;
1242 iconnect.egid = client->egid;
1244 if (gotd_imsg_compose_event(&client->session->iev, GOTD_IMSG_CONNECT,
1245 PROC_GOTD, s, &iconnect, sizeof(iconnect)) == -1) {
1246 err = got_error_from_errno("imsg compose CONNECT");
1252 * We are no longer interested in messages from this client.
1253 * Further client requests will be handled by the session process.
1255 msgbuf_clear(&client->iev.ibuf.w);
1256 imsg_clear(&client->iev.ibuf);
1257 event_del(&client->iev.ev);
1258 client->fd = -1; /* will be closed via copy in client->iev.ibuf.fd */
1264 gotd_dispatch_client_session(int fd, short event, void *arg)
1266 struct gotd_imsgev *iev = arg;
1267 struct imsgbuf *ibuf = &iev->ibuf;
1268 struct gotd_child_proc *proc = NULL;
1269 struct gotd_client *client = NULL;
1274 client = find_client_by_proc_fd(fd);
1275 if (client == NULL) {
1276 /* Can happen during process teardown. */
1277 warnx("cannot find client for fd %d", fd);
1282 if (event & EV_READ) {
1283 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1284 fatal("imsg_read error");
1286 /* Connection closed. */
1292 if (event & EV_WRITE) {
1293 n = msgbuf_write(&ibuf->w);
1294 if (n == -1 && errno != EAGAIN)
1295 fatal("msgbuf_write");
1297 /* Connection closed. */
1303 proc = client->session;
1305 fatalx("cannot find session child process for fd %d", fd);
1308 const struct got_error *err = NULL;
1309 uint32_t client_id = 0;
1310 int do_disconnect = 0, do_start_repo_child = 0;
1312 if ((n = imsg_get(ibuf, &imsg)) == -1)
1313 fatal("%s: imsg_get error", __func__);
1314 if (n == 0) /* No more messages. */
1317 switch (imsg.hdr.type) {
1318 case GOTD_IMSG_ERROR:
1320 err = gotd_imsg_recv_error(&client_id, &imsg);
1322 case GOTD_IMSG_CLIENT_SESSION_READY:
1323 if (client->state != GOTD_CLIENT_STATE_ACCESS_GRANTED) {
1324 err = got_error(GOT_ERR_PRIVSEP_MSG);
1327 do_start_repo_child = 1;
1329 case GOTD_IMSG_DISCONNECT:
1333 log_debug("unexpected imsg %d", imsg.hdr.type);
1337 if (!verify_imsg_src(client, proc, &imsg)) {
1338 log_debug("dropping imsg type %d from PID %d",
1339 imsg.hdr.type, proc->pid);
1344 log_warnx("uid %d: %s", client->euid, err->msg);
1346 if (do_start_repo_child) {
1347 struct gotd_repo *repo;
1348 const char *name = client->session->repo_name;
1350 repo = gotd_find_repo_by_name(name, &gotd);
1352 enum gotd_procid proc_type;
1354 if (client->required_auth & GOTD_AUTH_WRITE)
1355 proc_type = PROC_REPO_WRITE;
1357 proc_type = PROC_REPO_READ;
1359 err = start_repo_child(client, proc_type, repo,
1360 gotd.argv0, gotd.confpath, gotd.daemonize,
1363 err = got_error(GOT_ERR_NOT_GIT_REPO);
1366 log_warnx("uid %d: %s", client->euid, err->msg);
1371 if (do_disconnect) {
1373 disconnect_on_error(client, err);
1382 gotd_imsg_event_add(iev);
1384 /* This pipe is dead. Remove its event handler */
1385 event_del(&iev->ev);
1391 gotd_dispatch_repo_child(int fd, short event, void *arg)
1393 struct gotd_imsgev *iev = arg;
1394 struct imsgbuf *ibuf = &iev->ibuf;
1395 struct gotd_child_proc *proc = NULL;
1396 struct gotd_client *client;
1401 client = find_client_by_proc_fd(fd);
1402 if (client == NULL) {
1403 /* Can happen during process teardown. */
1404 warnx("cannot find client for fd %d", fd);
1409 if (event & EV_READ) {
1410 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1411 fatal("imsg_read error");
1413 /* Connection closed. */
1419 if (event & EV_WRITE) {
1420 n = msgbuf_write(&ibuf->w);
1421 if (n == -1 && errno != EAGAIN)
1422 fatal("msgbuf_write");
1424 /* Connection closed. */
1430 proc = client->repo;
1432 fatalx("cannot find child process for fd %d", fd);
1435 const struct got_error *err = NULL;
1436 uint32_t client_id = 0;
1437 int do_disconnect = 0;
1439 if ((n = imsg_get(ibuf, &imsg)) == -1)
1440 fatal("%s: imsg_get error", __func__);
1441 if (n == 0) /* No more messages. */
1444 switch (imsg.hdr.type) {
1445 case GOTD_IMSG_ERROR:
1447 err = gotd_imsg_recv_error(&client_id, &imsg);
1449 case GOTD_IMSG_REPO_CHILD_READY:
1450 err = connect_session(client);
1453 err = connect_repo_child(client, proc);
1456 log_debug("unexpected imsg %d", imsg.hdr.type);
1460 if (!verify_imsg_src(client, proc, &imsg)) {
1461 log_debug("dropping imsg type %d from PID %d",
1462 imsg.hdr.type, proc->pid);
1467 log_warnx("uid %d: %s", client->euid, err->msg);
1469 if (do_disconnect) {
1471 disconnect_on_error(client, err);
1480 gotd_imsg_event_add(iev);
1482 /* This pipe is dead. Remove its event handler */
1483 event_del(&iev->ev);
1489 start_child(enum gotd_procid proc_id, const char *repo_path,
1490 char *argv0, const char *confpath, int fd, int daemonize, int verbosity)
1496 switch (pid = fork()) {
1498 fatal("cannot fork");
1506 if (fd != GOTD_FILENO_MSG_PIPE) {
1507 if (dup2(fd, GOTD_FILENO_MSG_PIPE) == -1)
1508 fatal("cannot setup imsg fd");
1509 } else if (fcntl(fd, F_SETFD, 0) == -1)
1510 fatal("cannot setup imsg fd");
1512 argv[argc++] = argv0;
1515 argv[argc++] = (char *)"-L";
1518 argv[argc++] = (char *)"-A";
1520 case PROC_SESSION_READ:
1521 argv[argc++] = (char *)"-s";
1523 case PROC_SESSION_WRITE:
1524 argv[argc++] = (char *)"-S";
1526 case PROC_REPO_READ:
1527 argv[argc++] = (char *)"-R";
1529 case PROC_REPO_WRITE:
1530 argv[argc++] = (char *)"-W";
1533 fatalx("invalid process id %d", proc_id);
1536 argv[argc++] = (char *)"-f";
1537 argv[argc++] = (char *)confpath;
1540 argv[argc++] = (char *)"-P";
1541 argv[argc++] = (char *)repo_path;
1545 argv[argc++] = (char *)"-d";
1547 argv[argc++] = (char *)"-v";
1549 argv[argc++] = (char *)"-v";
1550 argv[argc++] = NULL;
1552 execvp(argv0, argv);
1557 start_listener(char *argv0, const char *confpath, int daemonize, int verbosity)
1559 struct gotd_child_proc *proc;
1561 proc = calloc(1, sizeof(*proc));
1565 TAILQ_INSERT_HEAD(&procs, proc, entry);
1567 /* proc->tmo is initialized in main() after event_init() */
1569 proc->type = PROC_LISTEN;
1571 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1572 PF_UNSPEC, proc->pipe) == -1)
1573 fatal("socketpair");
1575 proc->pid = start_child(proc->type, NULL, argv0, confpath,
1576 proc->pipe[1], daemonize, verbosity);
1577 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1578 proc->iev.handler = gotd_dispatch_listener;
1579 proc->iev.events = EV_READ;
1580 proc->iev.handler_arg = NULL;
1582 gotd.listen_proc = proc;
1585 static const struct got_error *
1586 start_session_child(struct gotd_client *client, struct gotd_repo *repo,
1587 char *argv0, const char *confpath, int daemonize, int verbosity)
1589 struct gotd_child_proc *proc;
1591 proc = calloc(1, sizeof(*proc));
1593 return got_error_from_errno("calloc");
1595 TAILQ_INSERT_HEAD(&procs, proc, entry);
1596 evtimer_set(&proc->tmo, kill_proc_timeout, proc);
1598 if (client_is_reading(client))
1599 proc->type = PROC_SESSION_READ;
1601 proc->type = PROC_SESSION_WRITE;
1602 if (strlcpy(proc->repo_name, repo->name,
1603 sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
1604 fatalx("repository name too long: %s", repo->name);
1605 log_debug("starting client uid %d session for repository %s",
1606 client->euid, repo->name);
1607 if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >=
1608 sizeof(proc->repo_path))
1609 fatalx("repository path too long: %s", repo->path);
1610 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1611 PF_UNSPEC, proc->pipe) == -1)
1612 fatal("socketpair");
1613 proc->pid = start_child(proc->type, proc->repo_path, argv0,
1614 confpath, proc->pipe[1], daemonize, verbosity);
1615 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1616 log_debug("proc %s %s is on fd %d",
1617 gotd_proc_names[proc->type], proc->repo_path,
1619 proc->iev.handler = gotd_dispatch_client_session;
1620 proc->iev.events = EV_READ;
1621 proc->iev.handler_arg = NULL;
1622 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1623 gotd_dispatch_client_session, &proc->iev);
1624 gotd_imsg_event_add(&proc->iev);
1626 client->session = proc;
1630 static const struct got_error *
1631 start_repo_child(struct gotd_client *client, enum gotd_procid proc_type,
1632 struct gotd_repo *repo, char *argv0, const char *confpath,
1633 int daemonize, int verbosity)
1635 struct gotd_child_proc *proc;
1637 if (proc_type != PROC_REPO_READ && proc_type != PROC_REPO_WRITE)
1638 return got_error_msg(GOT_ERR_NOT_IMPL, "bad process type");
1640 proc = calloc(1, sizeof(*proc));
1642 return got_error_from_errno("calloc");
1644 TAILQ_INSERT_HEAD(&procs, proc, entry);
1645 evtimer_set(&proc->tmo, kill_proc_timeout, proc);
1647 proc->type = proc_type;
1648 if (strlcpy(proc->repo_name, repo->name,
1649 sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
1650 fatalx("repository name too long: %s", repo->name);
1651 log_debug("starting %s for repository %s",
1652 proc->type == PROC_REPO_READ ? "reader" : "writer", repo->name);
1653 if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >=
1654 sizeof(proc->repo_path))
1655 fatalx("repository path too long: %s", repo->path);
1656 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1657 PF_UNSPEC, proc->pipe) == -1)
1658 fatal("socketpair");
1659 proc->pid = start_child(proc->type, proc->repo_path, argv0,
1660 confpath, proc->pipe[1], daemonize, verbosity);
1661 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1662 log_debug("proc %s %s is on fd %d",
1663 gotd_proc_names[proc->type], proc->repo_path,
1665 proc->iev.handler = gotd_dispatch_repo_child;
1666 proc->iev.events = EV_READ;
1667 proc->iev.handler_arg = NULL;
1668 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1669 gotd_dispatch_repo_child, &proc->iev);
1670 gotd_imsg_event_add(&proc->iev);
1672 client->repo = proc;
1676 static const struct got_error *
1677 start_auth_child(struct gotd_client *client, int required_auth,
1678 struct gotd_repo *repo, char *argv0, const char *confpath,
1679 int daemonize, int verbosity)
1681 const struct got_error *err = NULL;
1682 struct gotd_child_proc *proc;
1683 struct gotd_imsg_auth iauth;
1686 memset(&iauth, 0, sizeof(iauth));
1688 fd = dup(client->fd);
1690 return got_error_from_errno("dup");
1692 proc = calloc(1, sizeof(*proc));
1694 err = got_error_from_errno("calloc");
1699 TAILQ_INSERT_HEAD(&procs, proc, entry);
1700 evtimer_set(&proc->tmo, kill_proc_timeout, proc);
1702 proc->type = PROC_AUTH;
1703 if (strlcpy(proc->repo_name, repo->name,
1704 sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
1705 fatalx("repository name too long: %s", repo->name);
1706 log_debug("starting auth for uid %d repository %s",
1707 client->euid, repo->name);
1708 if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >=
1709 sizeof(proc->repo_path))
1710 fatalx("repository path too long: %s", repo->path);
1711 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1712 PF_UNSPEC, proc->pipe) == -1)
1713 fatal("socketpair");
1714 proc->pid = start_child(proc->type, proc->repo_path, argv0,
1715 confpath, proc->pipe[1], daemonize, verbosity);
1716 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1717 log_debug("proc %s %s is on fd %d",
1718 gotd_proc_names[proc->type], proc->repo_path,
1720 proc->iev.handler = gotd_dispatch_auth_child;
1721 proc->iev.events = EV_READ;
1722 proc->iev.handler_arg = NULL;
1723 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1724 gotd_dispatch_auth_child, &proc->iev);
1725 gotd_imsg_event_add(&proc->iev);
1727 iauth.euid = client->euid;
1728 iauth.egid = client->egid;
1729 iauth.required_auth = required_auth;
1730 iauth.client_id = client->id;
1731 if (gotd_imsg_compose_event(&proc->iev, GOTD_IMSG_AUTHENTICATE,
1732 PROC_GOTD, fd, &iauth, sizeof(iauth)) == -1) {
1733 log_warn("imsg compose AUTHENTICATE");
1735 /* Let the auth_timeout handler tidy up. */
1738 client->auth = proc;
1739 client->required_auth = required_auth;
1744 apply_unveil_repo_readonly(const char *repo_path, int need_tmpdir)
1747 if (unveil(GOT_TMPDIR_STR, "rwc") == -1)
1748 fatal("unveil %s", GOT_TMPDIR_STR);
1751 if (unveil(repo_path, "r") == -1)
1752 fatal("unveil %s", repo_path);
1754 if (unveil(NULL, NULL) == -1)
1759 apply_unveil_repo_readwrite(const char *repo_path)
1761 if (unveil(repo_path, "rwc") == -1)
1762 fatal("unveil %s", repo_path);
1764 if (unveil(GOT_TMPDIR_STR, "rwc") == -1)
1765 fatal("unveil %s", GOT_TMPDIR_STR);
1767 if (unveil(NULL, NULL) == -1)
1772 apply_unveil_none(void)
1774 if (unveil("/", "") == -1)
1777 if (unveil(NULL, NULL) == -1)
1782 apply_unveil_selfexec(void)
1784 if (unveil(gotd.argv0, "x") == -1)
1785 fatal("unveil %s", gotd.argv0);
1787 if (unveil(NULL, NULL) == -1)
1792 main(int argc, char **argv)
1794 const struct got_error *error = NULL;
1795 int ch, fd = -1, daemonize = 1, verbosity = 0, noaction = 0;
1796 const char *confpath = GOTD_CONF_PATH;
1797 char *argv0 = argv[0];
1799 struct passwd *pw = NULL;
1800 char *repo_path = NULL;
1801 enum gotd_procid proc_id = PROC_GOTD;
1802 struct event evsigint, evsigterm, evsighup, evsigusr1, evsigchld;
1803 int *pack_fds = NULL, *temp_fds = NULL;
1804 struct gotd_repo *repo = NULL;
1808 log_init(1, LOG_DAEMON); /* Log to stderr until daemonized. */
1810 while ((ch = getopt(argc, argv, "Adf:LnP:RsSvW")) != -1) {
1813 proc_id = PROC_AUTH;
1822 proc_id = PROC_LISTEN;
1828 repo_path = realpath(optarg, NULL);
1829 if (repo_path == NULL)
1830 fatal("realpath '%s'", optarg);
1833 proc_id = PROC_REPO_READ;
1836 proc_id = PROC_SESSION_READ;
1839 proc_id = PROC_SESSION_WRITE;
1846 proc_id = PROC_REPO_WRITE;
1859 if (geteuid() && (proc_id == PROC_GOTD || proc_id == PROC_LISTEN))
1860 fatalx("need root privileges");
1862 if (parse_config(confpath, proc_id, &gotd) != 0)
1865 pw = getpwnam(gotd.user_name);
1867 fatalx("user %s not found", gotd.user_name);
1869 if (pw->pw_uid == 0)
1870 fatalx("cannot run %s as the superuser", getprogname());
1873 fprintf(stderr, "configuration OK\n");
1878 gotd.daemonize = daemonize;
1879 gotd.verbosity = verbosity;
1880 gotd.confpath = confpath;
1882 /* Require an absolute path in argv[0] for reliable re-exec. */
1883 if (!got_path_is_absolute(argv0))
1884 fatalx("bad path \"%s\": must be an absolute path", argv0);
1886 log_init(daemonize ? 0 : 1, LOG_DAEMON);
1887 log_setverbose(verbosity);
1889 if (proc_id == PROC_GOTD) {
1890 snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
1891 arc4random_buf(&clients_hash_key, sizeof(clients_hash_key));
1892 if (daemonize && daemon(1, 0) == -1)
1894 gotd.pid = getpid();
1895 start_listener(argv0, confpath, daemonize, verbosity);
1896 } else if (proc_id == PROC_LISTEN) {
1897 snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
1899 log_info("socket: %s", gotd.unix_socket_path);
1900 log_info("user: %s", pw->pw_name);
1903 fd = unix_socket_listen(gotd.unix_socket_path, pw->pw_uid,
1906 fatal("cannot listen on unix socket %s",
1907 gotd.unix_socket_path);
1909 } else if (proc_id == PROC_AUTH) {
1910 snprintf(title, sizeof(title), "%s %s",
1911 gotd_proc_names[proc_id], repo_path);
1912 } else if (proc_id == PROC_REPO_READ || proc_id == PROC_REPO_WRITE ||
1913 proc_id == PROC_SESSION_READ || proc_id == PROC_SESSION_WRITE) {
1914 error = got_repo_pack_fds_open(&pack_fds);
1916 fatalx("cannot open pack tempfiles: %s", error->msg);
1917 error = got_repo_temp_fds_open(&temp_fds);
1919 fatalx("cannot open pack tempfiles: %s", error->msg);
1920 if (repo_path == NULL)
1921 fatalx("repository path not specified");
1922 snprintf(title, sizeof(title), "%s %s",
1923 gotd_proc_names[proc_id], repo_path);
1925 fatal("invalid process id %d", proc_id);
1927 setproctitle("%s", title);
1928 log_procinit(title);
1930 /* Drop root privileges. */
1931 if (setgid(pw->pw_gid) == -1)
1932 fatal("setgid %d failed", pw->pw_gid);
1933 if (setuid(pw->pw_uid) == -1)
1934 fatal("setuid %d failed", pw->pw_uid);
1941 /* "exec" promise will be limited to argv[0] via unveil(2). */
1942 if (pledge("stdio proc exec sendfd recvfd unveil", NULL) == -1)
1948 if (pledge("stdio sendfd unix unveil", NULL) == -1)
1952 * Ensure that AF_UNIX bind(2) cannot be used with any other
1953 * sockets by revoking all filesystem access via unveil(2).
1955 apply_unveil_none();
1957 listen_main(title, fd, gotd.connection_limits,
1958 gotd.nconnection_limits);
1963 if (pledge("stdio getpw recvfd unix unveil", NULL) == -1)
1967 * We need the "unix" pledge promise for getpeername(2) only.
1968 * Ensure that AF_UNIX bind(2) cannot be used by revoking all
1969 * filesystem access via unveil(2). Access to password database
1970 * files will still work since "getpw" bypasses unveil(2).
1972 apply_unveil_none();
1974 auth_main(title, &gotd.repos, repo_path);
1977 case PROC_SESSION_READ:
1978 case PROC_SESSION_WRITE:
1981 * The "recvfd" promise is only needed during setup and
1982 * will be removed in a later pledge(2) call.
1984 if (pledge("stdio rpath wpath cpath recvfd sendfd fattr flock "
1985 "unveil", NULL) == -1)
1988 if (proc_id == PROC_SESSION_READ)
1989 apply_unveil_repo_readonly(repo_path, 1);
1991 apply_unveil_repo_readwrite(repo_path);
1992 session_main(title, repo_path, pack_fds, temp_fds,
1993 &gotd.request_timeout, proc_id);
1996 case PROC_REPO_READ:
1998 if (pledge("stdio rpath recvfd unveil", NULL) == -1)
2001 apply_unveil_repo_readonly(repo_path, 0);
2002 repo_read_main(title, repo_path, pack_fds, temp_fds);
2005 case PROC_REPO_WRITE:
2007 if (pledge("stdio rpath recvfd unveil", NULL) == -1)
2010 apply_unveil_repo_readonly(repo_path, 0);
2011 repo = gotd_find_repo_by_path(repo_path, &gotd);
2013 fatalx("no repository for path %s", repo_path);
2014 repo_write_main(title, repo_path, pack_fds, temp_fds,
2015 &repo->protected_tag_namespaces,
2016 &repo->protected_branch_namespaces,
2017 &repo->protected_branches);
2021 fatal("invalid process id %d", proc_id);
2024 if (proc_id != PROC_GOTD)
2025 fatal("invalid process id %d", proc_id);
2027 evtimer_set(&gotd.listen_proc->tmo, kill_proc_timeout,
2030 apply_unveil_selfexec();
2032 signal_set(&evsigint, SIGINT, gotd_sighdlr, NULL);
2033 signal_set(&evsigterm, SIGTERM, gotd_sighdlr, NULL);
2034 signal_set(&evsighup, SIGHUP, gotd_sighdlr, NULL);
2035 signal_set(&evsigusr1, SIGUSR1, gotd_sighdlr, NULL);
2036 signal_set(&evsigchld, SIGCHLD, gotd_sighdlr, NULL);
2037 signal(SIGPIPE, SIG_IGN);
2039 signal_add(&evsigint, NULL);
2040 signal_add(&evsigterm, NULL);
2041 signal_add(&evsighup, NULL);
2042 signal_add(&evsigusr1, NULL);
2043 signal_add(&evsigchld, NULL);
2045 gotd_imsg_event_add(&gotd.listen_proc->iev);