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 "got_compat.h"
19 #include <sys/queue.h>
21 #include <sys/types.h>
23 #include <sys/socket.h>
42 #include "got_error.h"
43 #include "got_opentemp.h"
45 #include "got_repository.h"
46 #include "got_object.h"
47 #include "got_reference.h"
49 #include "got_lib_delta.h"
50 #include "got_lib_object.h"
51 #include "got_lib_object_cache.h"
52 #include "got_lib_hash.h"
53 #include "got_lib_gitproto.h"
54 #include "got_lib_pack.h"
55 #include "got_lib_repository.h"
62 #include "repo_read.h"
63 #include "repo_write.h"
66 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
69 enum gotd_client_state {
70 GOTD_CLIENT_STATE_NEW,
71 GOTD_CLIENT_STATE_ACCESS_GRANTED,
74 struct gotd_child_proc {
76 enum gotd_procid type;
77 char repo_name[NAME_MAX];
78 char repo_path[PATH_MAX];
80 struct gotd_imsgev iev;
83 TAILQ_ENTRY(gotd_child_proc) entry;
85 TAILQ_HEAD(gotd_procs, gotd_child_proc) procs;
88 STAILQ_ENTRY(gotd_client) entry;
89 enum gotd_client_state state;
92 struct gotd_imsgev iev;
96 struct gotd_child_proc *repo;
97 struct gotd_child_proc *auth;
98 struct gotd_child_proc *session;
101 STAILQ_HEAD(gotd_clients, gotd_client);
103 static struct gotd_clients gotd_clients[GOTD_CLIENT_TABLE_SIZE];
104 static SIPHASH_KEY clients_hash_key;
105 volatile int client_cnt;
106 static struct timeval auth_timeout = { 5, 0 };
107 static struct gotd gotd;
109 void gotd_sighdlr(int sig, short event, void *arg);
110 static void gotd_shutdown(void);
111 static const struct got_error *start_session_child(struct gotd_client *,
112 struct gotd_repo *, char *, const char *, int, int);
113 static const struct got_error *start_repo_child(struct gotd_client *,
114 enum gotd_procid, struct gotd_repo *, char *, const char *, int, int);
115 static const struct got_error *start_auth_child(struct gotd_client *, int,
116 struct gotd_repo *, char *, const char *, int, int);
117 static void kill_proc(struct gotd_child_proc *, int);
118 static void disconnect(struct gotd_client *);
123 fprintf(stderr, "usage: %s [-dnv] [-f config-file]\n", getprogname());
128 unix_socket_listen(const char *unix_socket_path, uid_t uid, gid_t gid)
130 struct sockaddr_un sun;
132 mode_t old_umask, mode;
133 int sock_flags = SOCK_STREAM | SOCK_NONBLOCK;
136 sock_flags |= SOCK_CLOEXEC;
139 fd = socket(AF_UNIX, sock_flags, 0);
145 sun.sun_family = AF_UNIX;
146 if (strlcpy(sun.sun_path, unix_socket_path,
147 sizeof(sun.sun_path)) >= sizeof(sun.sun_path)) {
148 log_warnx("%s: name too long", unix_socket_path);
153 if (unlink(unix_socket_path) == -1) {
154 if (errno != ENOENT) {
155 log_warn("unlink %s", unix_socket_path);
161 old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
162 mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
164 if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
165 log_warn("bind: %s", unix_socket_path);
173 if (chmod(unix_socket_path, mode) == -1) {
174 log_warn("chmod %o %s", mode, unix_socket_path);
176 unlink(unix_socket_path);
180 if (chown(unix_socket_path, uid, gid) == -1) {
181 log_warn("chown %s uid=%d gid=%d", unix_socket_path, uid, gid);
183 unlink(unix_socket_path);
187 if (listen(fd, GOTD_UNIX_SOCKET_BACKLOG) == -1) {
190 unlink(unix_socket_path);
198 client_hash(uint32_t client_id)
200 return SipHash24(&clients_hash_key, &client_id, sizeof(client_id));
204 add_client(struct gotd_client *client)
206 uint64_t slot = client_hash(client->id) % nitems(gotd_clients);
207 STAILQ_INSERT_HEAD(&gotd_clients[slot], client, entry);
211 static struct gotd_client *
212 find_client(uint32_t client_id)
215 struct gotd_client *c;
217 slot = client_hash(client_id) % nitems(gotd_clients);
218 STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
219 if (c->id == client_id)
226 static struct gotd_client *
227 find_client_by_proc_fd(int fd)
231 for (slot = 0; slot < nitems(gotd_clients); slot++) {
232 struct gotd_client *c;
234 STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
235 if (c->repo && c->repo->iev.ibuf.fd == fd)
237 if (c->auth && c->auth->iev.ibuf.fd == fd)
239 if (c->session && c->session->iev.ibuf.fd == fd)
248 client_is_reading(struct gotd_client *client)
250 return (client->required_auth &
251 (GOTD_AUTH_READ | GOTD_AUTH_WRITE)) == GOTD_AUTH_READ;
255 client_is_writing(struct gotd_client *client)
257 return (client->required_auth &
258 (GOTD_AUTH_READ | GOTD_AUTH_WRITE)) ==
259 (GOTD_AUTH_READ | GOTD_AUTH_WRITE);
262 static const struct got_error *
263 ensure_client_is_not_writing(struct gotd_client *client)
265 if (client_is_writing(client)) {
266 return got_error_fmt(GOT_ERR_BAD_PACKET,
267 "uid %d made a read-request but is writing to "
268 "a repository", client->euid);
274 static const struct got_error *
275 ensure_client_is_not_reading(struct gotd_client *client)
277 if (client_is_reading(client)) {
278 return got_error_fmt(GOT_ERR_BAD_PACKET,
279 "uid %d made a write-request but is reading from "
280 "a repository", client->euid);
287 proc_done(struct gotd_child_proc *proc)
289 struct gotd_client *client;
291 TAILQ_REMOVE(&procs, proc, entry);
293 client = find_client_by_proc_fd(proc->iev.ibuf.fd);
294 if (client != NULL) {
295 if (proc == client->repo)
297 if (proc == client->auth)
299 if (proc == client->session)
300 client->session = NULL;
304 evtimer_del(&proc->tmo);
306 if (proc->iev.ibuf.fd != -1) {
307 event_del(&proc->iev.ev);
308 msgbuf_clear(&proc->iev.ibuf.w);
309 close(proc->iev.ibuf.fd);
316 kill_repo_proc(struct gotd_client *client)
318 if (client->repo == NULL)
321 kill_proc(client->repo, 0);
326 kill_auth_proc(struct gotd_client *client)
328 if (client->auth == NULL)
331 kill_proc(client->auth, 0);
336 kill_session_proc(struct gotd_client *client)
338 if (client->session == NULL)
341 kill_proc(client->session, 0);
342 client->session = NULL;
346 disconnect(struct gotd_client *client)
348 struct gotd_imsg_disconnect idisconnect;
349 struct gotd_child_proc *listen_proc = gotd.listen_proc;
352 log_debug("uid %d: disconnecting", client->euid);
354 kill_auth_proc(client);
355 kill_session_proc(client);
356 kill_repo_proc(client);
358 idisconnect.client_id = client->id;
359 if (gotd_imsg_compose_event(&listen_proc->iev,
360 GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
361 &idisconnect, sizeof(idisconnect)) == -1)
362 log_warn("imsg compose DISCONNECT");
364 slot = client_hash(client->id) % nitems(gotd_clients);
365 STAILQ_REMOVE(&gotd_clients[slot], client, gotd_client, entry);
366 imsg_clear(&client->iev.ibuf);
367 event_del(&client->iev.ev);
368 evtimer_del(&client->tmo);
369 if (client->fd != -1)
371 else if (client->iev.ibuf.fd != -1)
372 close(client->iev.ibuf.fd);
378 disconnect_on_error(struct gotd_client *client, const struct got_error *err)
382 if (err->code != GOT_ERR_EOF) {
383 log_warnx("uid %d: %s", client->euid, err->msg);
384 if (client->fd != -1) {
385 imsg_init(&ibuf, client->fd);
386 gotd_imsg_send_error(&ibuf, 0, PROC_GOTD, err);
393 static const struct got_error *
394 send_repo_info(struct gotd_imsgev *iev, struct gotd_repo *repo)
396 const struct got_error *err = NULL;
397 struct gotd_imsg_info_repo irepo;
399 memset(&irepo, 0, sizeof(irepo));
401 if (strlcpy(irepo.repo_name, repo->name, sizeof(irepo.repo_name))
402 >= sizeof(irepo.repo_name))
403 return got_error_msg(GOT_ERR_NO_SPACE, "repo name too long");
404 if (strlcpy(irepo.repo_path, repo->path, sizeof(irepo.repo_path))
405 >= sizeof(irepo.repo_path))
406 return got_error_msg(GOT_ERR_NO_SPACE, "repo path too long");
408 if (gotd_imsg_compose_event(iev, GOTD_IMSG_INFO_REPO, PROC_GOTD, -1,
409 &irepo, sizeof(irepo)) == -1) {
410 err = got_error_from_errno("imsg compose INFO_REPO");
418 static const struct got_error *
419 send_client_info(struct gotd_imsgev *iev, struct gotd_client *client)
421 const struct got_error *err = NULL;
422 struct gotd_imsg_info_client iclient;
423 struct gotd_child_proc *proc;
425 memset(&iclient, 0, sizeof(iclient));
426 iclient.euid = client->euid;
427 iclient.egid = client->egid;
431 if (strlcpy(iclient.repo_name, proc->repo_path,
432 sizeof(iclient.repo_name)) >= sizeof(iclient.repo_name)) {
433 return got_error_msg(GOT_ERR_NO_SPACE,
434 "repo name too long");
436 if (client_is_writing(client))
437 iclient.is_writing = 1;
439 iclient.repo_child_pid = proc->pid;
443 iclient.session_child_pid = client->session->pid;
445 if (gotd_imsg_compose_event(iev, GOTD_IMSG_INFO_CLIENT, PROC_GOTD, -1,
446 &iclient, sizeof(iclient)) == -1) {
447 err = got_error_from_errno("imsg compose INFO_CLIENT");
455 static const struct got_error *
456 send_info(struct gotd_client *client)
458 const struct got_error *err = NULL;
459 struct gotd_imsg_info info;
461 struct gotd_repo *repo;
463 if (client->euid != 0)
464 return got_error_set_errno(EPERM, "info");
467 info.verbosity = gotd.verbosity;
468 info.nrepos = gotd.nrepos;
469 info.nclients = client_cnt - 1;
471 if (gotd_imsg_compose_event(&client->iev, GOTD_IMSG_INFO, PROC_GOTD, -1,
472 &info, sizeof(info)) == -1) {
473 err = got_error_from_errno("imsg compose INFO");
478 TAILQ_FOREACH(repo, &gotd.repos, entry) {
479 err = send_repo_info(&client->iev, repo);
484 for (slot = 0; slot < nitems(gotd_clients); slot++) {
485 struct gotd_client *c;
486 STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
487 if (c->id == client->id)
489 err = send_client_info(&client->iev, c);
498 static const struct got_error *
499 stop_gotd(struct gotd_client *client)
502 if (client->euid != 0)
503 return got_error_set_errno(EPERM, "stop");
510 static const struct got_error *
511 start_client_authentication(struct gotd_client *client, struct imsg *imsg)
513 const struct got_error *err;
514 struct gotd_imsg_list_refs ireq;
515 struct gotd_repo *repo = NULL;
518 log_debug("list-refs request from uid %d", client->euid);
520 if (client->state != GOTD_CLIENT_STATE_NEW)
521 return got_error_msg(GOT_ERR_BAD_REQUEST,
522 "unexpected list-refs request received");
524 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
525 if (datalen != sizeof(ireq))
526 return got_error(GOT_ERR_PRIVSEP_LEN);
528 memcpy(&ireq, imsg->data, datalen);
530 if (ireq.client_is_reading) {
531 err = ensure_client_is_not_writing(client);
534 repo = gotd_find_repo_by_name(ireq.repo_name, &gotd);
536 return got_error(GOT_ERR_NOT_GIT_REPO);
537 err = start_auth_child(client, GOTD_AUTH_READ, repo,
538 gotd.argv0, gotd.confpath, gotd.daemonize,
543 err = ensure_client_is_not_reading(client);
546 repo = gotd_find_repo_by_name(ireq.repo_name, &gotd);
548 return got_error(GOT_ERR_NOT_GIT_REPO);
549 err = start_auth_child(client,
550 GOTD_AUTH_READ | GOTD_AUTH_WRITE,
551 repo, gotd.argv0, gotd.confpath, gotd.daemonize,
557 evtimer_add(&client->tmo, &auth_timeout);
559 /* Flow continues upon authentication success/failure or timeout. */
564 gotd_request(int fd, short events, void *arg)
566 struct gotd_imsgev *iev = arg;
567 struct imsgbuf *ibuf = &iev->ibuf;
568 struct gotd_client *client = iev->handler_arg;
569 const struct got_error *err = NULL;
573 if (events & EV_WRITE) {
574 while (ibuf->w.queued) {
575 n = msgbuf_write(&ibuf->w);
576 if (n == -1 && errno == EPIPE) {
578 * The client has closed its socket.
579 * This can happen when Git clients are
580 * done sending pack file data.
582 msgbuf_clear(&ibuf->w);
584 } else if (n == -1 && errno != EAGAIN) {
585 err = got_error_from_errno("imsg_flush");
586 disconnect_on_error(client, err);
590 /* Connection closed. */
591 err = got_error(GOT_ERR_EOF);
592 disconnect_on_error(client, err);
597 /* Disconnect gotctl(8) now that messages have been sent. */
598 if (!client_is_reading(client) && !client_is_writing(client)) {
604 if ((events & EV_READ) == 0)
607 memset(&imsg, 0, sizeof(imsg));
609 while (err == NULL) {
610 err = gotd_imsg_recv(&imsg, ibuf, 0);
612 if (err->code == GOT_ERR_PRIVSEP_READ)
617 evtimer_del(&client->tmo);
619 switch (imsg.hdr.type) {
621 err = send_info(client);
624 err = stop_gotd(client);
626 case GOTD_IMSG_LIST_REFS:
627 err = start_client_authentication(client, &imsg);
630 log_debug("unexpected imsg %d", imsg.hdr.type);
631 err = got_error(GOT_ERR_PRIVSEP_MSG);
639 disconnect_on_error(client, err);
641 gotd_imsg_event_add(&client->iev);
646 gotd_auth_timeout(int fd, short events, void *arg)
648 struct gotd_client *client = arg;
650 log_debug("disconnecting uid %d due to authentication timeout",
655 static const struct got_error *
656 recv_connect(uint32_t *client_id, struct imsg *imsg)
658 const struct got_error *err = NULL;
659 struct gotd_imsg_connect iconnect;
662 struct gotd_client *client = NULL;
666 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
667 if (datalen != sizeof(iconnect))
668 return got_error(GOT_ERR_PRIVSEP_LEN);
669 memcpy(&iconnect, imsg->data, sizeof(iconnect));
673 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
677 if (find_client(iconnect.client_id)) {
678 err = got_error_msg(GOT_ERR_CLIENT_ID, "duplicate client ID");
682 client = calloc(1, sizeof(*client));
683 if (client == NULL) {
684 err = got_error_from_errno("calloc");
688 *client_id = iconnect.client_id;
690 client->state = GOTD_CLIENT_STATE_NEW;
691 client->id = iconnect.client_id;
694 /* The auth process will verify UID/GID for us. */
695 client->euid = iconnect.euid;
696 client->egid = iconnect.egid;
698 imsg_init(&client->iev.ibuf, client->fd);
699 client->iev.handler = gotd_request;
700 client->iev.events = EV_READ;
701 client->iev.handler_arg = client;
703 event_set(&client->iev.ev, client->fd, EV_READ, gotd_request,
705 gotd_imsg_event_add(&client->iev);
707 evtimer_set(&client->tmo, gotd_auth_timeout, client);
710 log_debug("%s: new client uid %d connected on fd %d", __func__,
711 client->euid, client->fd);
714 struct gotd_child_proc *listen_proc = gotd.listen_proc;
715 struct gotd_imsg_disconnect idisconnect;
717 idisconnect.client_id = client->id;
718 if (gotd_imsg_compose_event(&listen_proc->iev,
719 GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
720 &idisconnect, sizeof(idisconnect)) == -1)
721 log_warn("imsg compose DISCONNECT");
730 static const char *gotd_proc_names[PROC_MAX] = {
742 kill_proc(struct gotd_child_proc *proc, int fatal)
744 struct timeval tv = { 5, 0 };
746 log_debug("kill -%d %d", fatal ? SIGKILL : SIGTERM, proc->pid);
748 if (proc->iev.ibuf.fd != -1) {
749 event_del(&proc->iev.ev);
750 msgbuf_clear(&proc->iev.ibuf.w);
751 close(proc->iev.ibuf.fd);
752 proc->iev.ibuf.fd = -1;
755 if (!evtimer_pending(&proc->tmo, NULL) && !fatal)
756 evtimer_add(&proc->tmo, &tv);
759 log_warnx("sending SIGKILL to PID %d", proc->pid);
760 kill(proc->pid, SIGKILL);
762 kill(proc->pid, SIGTERM);
766 kill_proc_timeout(int fd, short ev, void *d)
768 struct gotd_child_proc *proc = d;
770 log_warnx("timeout waiting for PID %d to terminate;"
771 " retrying with force", proc->pid);
780 log_debug("shutting down");
781 for (slot = 0; slot < nitems(gotd_clients); slot++) {
782 struct gotd_client *c, *tmp;
784 STAILQ_FOREACH_SAFE(c, &gotd_clients[slot], entry, tmp)
788 kill_proc(gotd.listen_proc, 0);
790 log_info("terminating");
794 static struct gotd_child_proc *
795 find_proc_by_pid(pid_t pid)
797 struct gotd_child_proc *proc = NULL;
799 TAILQ_FOREACH(proc, &procs, entry)
800 if (proc->pid == pid)
807 gotd_sighdlr(int sig, short event, void *arg)
809 struct gotd_child_proc *proc;
814 * Normal signal handler rules don't apply because libevent
820 log_info("%s: ignoring SIGHUP", __func__);
823 log_info("%s: ignoring SIGUSR1", __func__);
831 pid = waitpid(WAIT_ANY, &status, WNOHANG);
842 log_debug("reaped pid %d", pid);
843 proc = find_proc_by_pid(pid);
845 log_info("caught exit of unknown child %d",
850 if (WIFSIGNALED(status)) {
851 log_warnx("child PID %d terminated with"
852 " signal %d", pid, WTERMSIG(status));
859 fatalx("unexpected signal");
863 static const struct got_error *
864 ensure_proc_is_reading(struct gotd_client *client,
865 struct gotd_child_proc *proc)
867 if (!client_is_reading(client)) {
869 return got_error_fmt(GOT_ERR_BAD_PACKET,
870 "PID %d handled a read-request for uid %d but this "
871 "user is not reading from a repository", proc->pid,
878 static const struct got_error *
879 ensure_proc_is_writing(struct gotd_client *client,
880 struct gotd_child_proc *proc)
882 if (!client_is_writing(client)) {
884 return got_error_fmt(GOT_ERR_BAD_PACKET,
885 "PID %d handled a write-request for uid %d but this "
886 "user is not writing to a repository", proc->pid,
894 verify_imsg_src(struct gotd_client *client, struct gotd_child_proc *proc,
897 const struct got_error *err;
900 if (proc->type == PROC_REPO_READ || proc->type == PROC_REPO_WRITE) {
901 if (client->repo == NULL)
902 fatalx("no process found for uid %d", client->euid);
903 if (proc->pid != client->repo->pid) {
905 log_warnx("received message from PID %d for uid %d, "
906 "while PID %d is the process serving this user",
907 proc->pid, client->euid, client->repo->pid);
911 if (proc->type == PROC_SESSION_READ ||
912 proc->type == PROC_SESSION_WRITE) {
913 if (client->session == NULL) {
914 log_warnx("no session found for uid %d", client->euid);
917 if (proc->pid != client->session->pid) {
919 log_warnx("received message from PID %d for uid %d, "
920 "while PID %d is the process serving this user",
921 proc->pid, client->euid, client->session->pid);
926 switch (imsg->hdr.type) {
927 case GOTD_IMSG_ERROR:
930 case GOTD_IMSG_CONNECT:
931 if (proc->type != PROC_LISTEN) {
932 err = got_error_fmt(GOT_ERR_BAD_PACKET,
933 "new connection for uid %d from PID %d "
934 "which is not the listen process",
935 proc->pid, client->euid);
939 case GOTD_IMSG_ACCESS_GRANTED:
940 if (proc->type != PROC_AUTH) {
941 err = got_error_fmt(GOT_ERR_BAD_PACKET,
942 "authentication of uid %d from PID %d "
943 "which is not the auth process",
944 proc->pid, client->euid);
948 case GOTD_IMSG_CLIENT_SESSION_READY:
949 if (proc->type != PROC_SESSION_READ &&
950 proc->type != PROC_SESSION_WRITE) {
951 err = got_error_fmt(GOT_ERR_BAD_PACKET,
952 "unexpected \"ready\" signal from PID %d",
957 case GOTD_IMSG_REPO_CHILD_READY:
958 if (proc->type != PROC_REPO_READ &&
959 proc->type != PROC_REPO_WRITE) {
960 err = got_error_fmt(GOT_ERR_BAD_PACKET,
961 "unexpected \"ready\" signal from PID %d",
966 case GOTD_IMSG_PACKFILE_DONE:
967 err = ensure_proc_is_reading(client, proc);
969 log_warnx("uid %d: %s", client->euid, err->msg);
973 case GOTD_IMSG_PACKFILE_INSTALL:
974 case GOTD_IMSG_REF_UPDATES_START:
975 case GOTD_IMSG_REF_UPDATE:
976 err = ensure_proc_is_writing(client, proc);
978 log_warnx("uid %d: %s", client->euid, err->msg);
983 log_debug("%s: unexpected imsg %d", __func__, imsg->hdr.type);
990 static const struct got_error *
991 connect_repo_child(struct gotd_client *client,
992 struct gotd_child_proc *repo_proc)
994 static const struct got_error *err;
995 struct gotd_imsgev *session_iev = &client->session->iev;
996 struct gotd_imsg_connect_repo_child ireq;
998 int sock_flags = SOCK_STREAM | SOCK_NONBLOCK;
1001 sock_flags |= SOCK_CLOEXEC;
1004 if (client->state != GOTD_CLIENT_STATE_ACCESS_GRANTED)
1005 return got_error_msg(GOT_ERR_BAD_REQUEST,
1006 "unexpected repo child ready signal received");
1008 if (socketpair(AF_UNIX, sock_flags, PF_UNSPEC, pipe) == -1)
1009 fatal("socketpair");
1011 memset(&ireq, 0, sizeof(ireq));
1012 ireq.client_id = client->id;
1013 ireq.proc_id = repo_proc->type;
1015 /* Pass repo child pipe to session child process. */
1016 if (gotd_imsg_compose_event(session_iev, GOTD_IMSG_CONNECT_REPO_CHILD,
1017 PROC_GOTD, pipe[0], &ireq, sizeof(ireq)) == -1) {
1018 err = got_error_from_errno("imsg compose CONNECT_REPO_CHILD");
1024 /* Pass session child pipe to repo child process. */
1025 if (gotd_imsg_compose_event(&repo_proc->iev,
1026 GOTD_IMSG_CONNECT_REPO_CHILD, PROC_GOTD, pipe[1], NULL, 0) == -1) {
1027 err = got_error_from_errno("imsg compose CONNECT_REPO_CHILD");
1036 gotd_dispatch_listener(int fd, short event, void *arg)
1038 struct gotd_imsgev *iev = arg;
1039 struct imsgbuf *ibuf = &iev->ibuf;
1040 struct gotd_child_proc *proc = gotd.listen_proc;
1045 if (proc->iev.ibuf.fd != fd)
1046 fatalx("%s: unexpected fd %d", __func__, fd);
1048 if (event & EV_READ) {
1049 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1050 fatal("imsg_read error");
1052 /* Connection closed. */
1058 if (event & EV_WRITE) {
1059 n = msgbuf_write(&ibuf->w);
1060 if (n == -1 && errno != EAGAIN)
1061 fatal("msgbuf_write");
1063 /* Connection closed. */
1070 const struct got_error *err = NULL;
1071 struct gotd_client *client = NULL;
1072 uint32_t client_id = 0;
1073 int do_disconnect = 0;
1075 if ((n = imsg_get(ibuf, &imsg)) == -1)
1076 fatal("%s: imsg_get error", __func__);
1077 if (n == 0) /* No more messages. */
1080 switch (imsg.hdr.type) {
1081 case GOTD_IMSG_ERROR:
1083 err = gotd_imsg_recv_error(&client_id, &imsg);
1085 case GOTD_IMSG_CONNECT:
1086 err = recv_connect(&client_id, &imsg);
1089 log_debug("unexpected imsg %d", imsg.hdr.type);
1093 client = find_client(client_id);
1094 if (client == NULL) {
1095 log_warnx("%s: client not found", __func__);
1101 log_warnx("uid %d: %s", client->euid, err->msg);
1103 if (do_disconnect) {
1105 disconnect_on_error(client, err);
1114 gotd_imsg_event_add(iev);
1116 /* This pipe is dead. Remove its event handler */
1117 event_del(&iev->ev);
1118 event_loopexit(NULL);
1123 gotd_dispatch_auth_child(int fd, short event, void *arg)
1125 const struct got_error *err = NULL;
1126 struct gotd_imsgev *iev = arg;
1127 struct imsgbuf *ibuf = &iev->ibuf;
1128 struct gotd_client *client;
1129 struct gotd_repo *repo = NULL;
1133 uint32_t client_id = 0;
1134 int do_disconnect = 0;
1136 client = find_client_by_proc_fd(fd);
1137 if (client == NULL) {
1138 /* Can happen during process teardown. */
1139 warnx("cannot find client for fd %d", fd);
1144 if (client->auth == NULL)
1145 fatalx("cannot find auth child process for fd %d", fd);
1147 if (event & EV_READ) {
1148 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1149 fatal("imsg_read error");
1151 /* Connection closed. */
1157 if (event & EV_WRITE) {
1158 n = msgbuf_write(&ibuf->w);
1159 if (n == -1 && errno != EAGAIN)
1160 fatal("msgbuf_write");
1162 /* Connection closed. */
1168 if (client->auth->iev.ibuf.fd != fd)
1169 fatalx("%s: unexpected fd %d", __func__, fd);
1171 if ((n = imsg_get(ibuf, &imsg)) == -1)
1172 fatal("%s: imsg_get error", __func__);
1173 if (n == 0) /* No more messages. */
1176 evtimer_del(&client->tmo);
1178 switch (imsg.hdr.type) {
1179 case GOTD_IMSG_ERROR:
1181 err = gotd_imsg_recv_error(&client_id, &imsg);
1183 case GOTD_IMSG_ACCESS_GRANTED:
1184 client->state = GOTD_CLIENT_STATE_ACCESS_GRANTED;
1188 log_debug("unexpected imsg %d", imsg.hdr.type);
1192 if (!verify_imsg_src(client, client->auth, &imsg)) {
1194 log_debug("dropping imsg type %d from PID %d",
1195 imsg.hdr.type, client->auth->pid);
1199 if (do_disconnect) {
1201 disconnect_on_error(client, err);
1207 repo = gotd_find_repo_by_name(client->auth->repo_name, &gotd);
1209 err = got_error(GOT_ERR_NOT_GIT_REPO);
1212 kill_auth_proc(client);
1214 log_info("authenticated uid %d for repository %s",
1215 client->euid, repo->name);
1217 err = start_session_child(client, repo, gotd.argv0,
1218 gotd.confpath, gotd.daemonize, gotd.verbosity);
1223 log_warnx("uid %d: %s", client->euid, err->msg);
1225 /* We might have killed the auth process by now. */
1226 if (client->auth != NULL) {
1228 gotd_imsg_event_add(iev);
1230 /* This pipe is dead. Remove its event handler */
1231 event_del(&iev->ev);
1236 static const struct got_error *
1237 connect_session(struct gotd_client *client)
1239 const struct got_error *err = NULL;
1240 struct gotd_imsg_connect iconnect;
1243 memset(&iconnect, 0, sizeof(iconnect));
1245 s = dup(client->fd);
1247 return got_error_from_errno("dup");
1249 iconnect.client_id = client->id;
1250 iconnect.euid = client->euid;
1251 iconnect.egid = client->egid;
1253 if (gotd_imsg_compose_event(&client->session->iev, GOTD_IMSG_CONNECT,
1254 PROC_GOTD, s, &iconnect, sizeof(iconnect)) == -1) {
1255 err = got_error_from_errno("imsg compose CONNECT");
1261 * We are no longer interested in messages from this client.
1262 * Further client requests will be handled by the session process.
1264 msgbuf_clear(&client->iev.ibuf.w);
1265 imsg_clear(&client->iev.ibuf);
1266 event_del(&client->iev.ev);
1267 client->fd = -1; /* will be closed via copy in client->iev.ibuf.fd */
1273 gotd_dispatch_client_session(int fd, short event, void *arg)
1275 struct gotd_imsgev *iev = arg;
1276 struct imsgbuf *ibuf = &iev->ibuf;
1277 struct gotd_child_proc *proc = NULL;
1278 struct gotd_client *client = NULL;
1283 client = find_client_by_proc_fd(fd);
1284 if (client == NULL) {
1285 /* Can happen during process teardown. */
1286 warnx("cannot find client for fd %d", fd);
1291 if (event & EV_READ) {
1292 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1293 fatal("imsg_read error");
1295 /* Connection closed. */
1301 if (event & EV_WRITE) {
1302 n = msgbuf_write(&ibuf->w);
1303 if (n == -1 && errno != EAGAIN)
1304 fatal("msgbuf_write");
1306 /* Connection closed. */
1312 proc = client->session;
1314 fatalx("cannot find session child process for fd %d", fd);
1317 const struct got_error *err = NULL;
1318 uint32_t client_id = 0;
1319 int do_disconnect = 0, do_start_repo_child = 0;
1321 if ((n = imsg_get(ibuf, &imsg)) == -1)
1322 fatal("%s: imsg_get error", __func__);
1323 if (n == 0) /* No more messages. */
1326 switch (imsg.hdr.type) {
1327 case GOTD_IMSG_ERROR:
1329 err = gotd_imsg_recv_error(&client_id, &imsg);
1331 case GOTD_IMSG_CLIENT_SESSION_READY:
1332 if (client->state != GOTD_CLIENT_STATE_ACCESS_GRANTED) {
1333 err = got_error(GOT_ERR_PRIVSEP_MSG);
1336 do_start_repo_child = 1;
1338 case GOTD_IMSG_DISCONNECT:
1342 log_debug("unexpected imsg %d", imsg.hdr.type);
1346 if (!verify_imsg_src(client, proc, &imsg)) {
1347 log_debug("dropping imsg type %d from PID %d",
1348 imsg.hdr.type, proc->pid);
1353 log_warnx("uid %d: %s", client->euid, err->msg);
1355 if (do_start_repo_child) {
1356 struct gotd_repo *repo;
1357 const char *name = client->session->repo_name;
1359 repo = gotd_find_repo_by_name(name, &gotd);
1361 enum gotd_procid proc_type;
1363 if (client->required_auth & GOTD_AUTH_WRITE)
1364 proc_type = PROC_REPO_WRITE;
1366 proc_type = PROC_REPO_READ;
1368 err = start_repo_child(client, proc_type, repo,
1369 gotd.argv0, gotd.confpath, gotd.daemonize,
1372 err = got_error(GOT_ERR_NOT_GIT_REPO);
1375 log_warnx("uid %d: %s", client->euid, err->msg);
1380 if (do_disconnect) {
1382 disconnect_on_error(client, err);
1391 gotd_imsg_event_add(iev);
1393 /* This pipe is dead. Remove its event handler */
1394 event_del(&iev->ev);
1400 gotd_dispatch_repo_child(int fd, short event, void *arg)
1402 struct gotd_imsgev *iev = arg;
1403 struct imsgbuf *ibuf = &iev->ibuf;
1404 struct gotd_child_proc *proc = NULL;
1405 struct gotd_client *client;
1410 client = find_client_by_proc_fd(fd);
1411 if (client == NULL) {
1412 /* Can happen during process teardown. */
1413 warnx("cannot find client for fd %d", fd);
1418 if (event & EV_READ) {
1419 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1420 fatal("imsg_read error");
1422 /* Connection closed. */
1428 if (event & EV_WRITE) {
1429 n = msgbuf_write(&ibuf->w);
1430 if (n == -1 && errno != EAGAIN)
1431 fatal("msgbuf_write");
1433 /* Connection closed. */
1439 proc = client->repo;
1441 fatalx("cannot find child process for fd %d", fd);
1444 const struct got_error *err = NULL;
1445 uint32_t client_id = 0;
1446 int do_disconnect = 0;
1448 if ((n = imsg_get(ibuf, &imsg)) == -1)
1449 fatal("%s: imsg_get error", __func__);
1450 if (n == 0) /* No more messages. */
1453 switch (imsg.hdr.type) {
1454 case GOTD_IMSG_ERROR:
1456 err = gotd_imsg_recv_error(&client_id, &imsg);
1458 case GOTD_IMSG_REPO_CHILD_READY:
1459 err = connect_session(client);
1462 err = connect_repo_child(client, proc);
1465 log_debug("unexpected imsg %d", imsg.hdr.type);
1469 if (!verify_imsg_src(client, proc, &imsg)) {
1470 log_debug("dropping imsg type %d from PID %d",
1471 imsg.hdr.type, proc->pid);
1476 log_warnx("uid %d: %s", client->euid, err->msg);
1478 if (do_disconnect) {
1480 disconnect_on_error(client, err);
1489 gotd_imsg_event_add(iev);
1491 /* This pipe is dead. Remove its event handler */
1492 event_del(&iev->ev);
1498 start_child(enum gotd_procid proc_id, const char *repo_path,
1499 char *argv0, const char *confpath, int fd, int daemonize, int verbosity)
1505 switch (pid = fork()) {
1507 fatal("cannot fork");
1515 if (fd != GOTD_FILENO_MSG_PIPE) {
1516 if (dup2(fd, GOTD_FILENO_MSG_PIPE) == -1)
1517 fatal("cannot setup imsg fd");
1518 } else if (fcntl(fd, F_SETFD, 0) == -1)
1519 fatal("cannot setup imsg fd");
1521 argv[argc++] = argv0;
1524 argv[argc++] = (char *)"-L";
1527 argv[argc++] = (char *)"-A";
1529 case PROC_SESSION_READ:
1530 argv[argc++] = (char *)"-s";
1532 case PROC_SESSION_WRITE:
1533 argv[argc++] = (char *)"-S";
1535 case PROC_REPO_READ:
1536 argv[argc++] = (char *)"-R";
1538 case PROC_REPO_WRITE:
1539 argv[argc++] = (char *)"-W";
1542 fatalx("invalid process id %d", proc_id);
1545 argv[argc++] = (char *)"-f";
1546 argv[argc++] = (char *)confpath;
1549 argv[argc++] = (char *)"-P";
1550 argv[argc++] = (char *)repo_path;
1554 argv[argc++] = (char *)"-d";
1556 argv[argc++] = (char *)"-v";
1558 argv[argc++] = (char *)"-v";
1559 argv[argc++] = NULL;
1561 execvp(argv0, argv);
1566 start_listener(char *argv0, const char *confpath, int daemonize, int verbosity)
1568 struct gotd_child_proc *proc;
1569 int sock_flags = SOCK_STREAM|SOCK_NONBLOCK;
1572 sock_flags |= SOCK_CLOEXEC;
1575 proc = calloc(1, sizeof(*proc));
1579 TAILQ_INSERT_HEAD(&procs, proc, entry);
1581 /* proc->tmo is initialized in main() after event_init() */
1583 proc->type = PROC_LISTEN;
1585 if (socketpair(AF_UNIX, sock_flags,
1586 PF_UNSPEC, proc->pipe) == -1)
1587 fatal("socketpair");
1589 proc->pid = start_child(proc->type, NULL, argv0, confpath,
1590 proc->pipe[1], daemonize, verbosity);
1591 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1592 proc->iev.handler = gotd_dispatch_listener;
1593 proc->iev.events = EV_READ;
1594 proc->iev.handler_arg = NULL;
1596 gotd.listen_proc = proc;
1599 static const struct got_error *
1600 start_session_child(struct gotd_client *client, struct gotd_repo *repo,
1601 char *argv0, const char *confpath, int daemonize, int verbosity)
1603 struct gotd_child_proc *proc;
1604 int sock_flags = SOCK_STREAM | SOCK_NONBLOCK;
1607 sock_flags |= SOCK_CLOEXEC;
1610 proc = calloc(1, sizeof(*proc));
1612 return got_error_from_errno("calloc");
1614 TAILQ_INSERT_HEAD(&procs, proc, entry);
1615 evtimer_set(&proc->tmo, kill_proc_timeout, proc);
1617 if (client_is_reading(client))
1618 proc->type = PROC_SESSION_READ;
1620 proc->type = PROC_SESSION_WRITE;
1621 if (strlcpy(proc->repo_name, repo->name,
1622 sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
1623 fatalx("repository name too long: %s", repo->name);
1624 log_debug("starting client uid %d session for repository %s",
1625 client->euid, repo->name);
1626 if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >=
1627 sizeof(proc->repo_path))
1628 fatalx("repository path too long: %s", repo->path);
1629 if (socketpair(AF_UNIX, sock_flags, PF_UNSPEC, proc->pipe) == -1)
1630 fatal("socketpair");
1631 proc->pid = start_child(proc->type, proc->repo_path, argv0,
1632 confpath, proc->pipe[1], daemonize, verbosity);
1633 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1634 log_debug("proc %s %s is on fd %d",
1635 gotd_proc_names[proc->type], proc->repo_path,
1637 proc->iev.handler = gotd_dispatch_client_session;
1638 proc->iev.events = EV_READ;
1639 proc->iev.handler_arg = NULL;
1640 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1641 gotd_dispatch_client_session, &proc->iev);
1642 gotd_imsg_event_add(&proc->iev);
1644 client->session = proc;
1648 static const struct got_error *
1649 start_repo_child(struct gotd_client *client, enum gotd_procid proc_type,
1650 struct gotd_repo *repo, char *argv0, const char *confpath,
1651 int daemonize, int verbosity)
1653 struct gotd_child_proc *proc;
1654 int sock_flags = SOCK_STREAM|SOCK_NONBLOCK;
1657 sock_flags |= SOCK_CLOEXEC;
1660 if (proc_type != PROC_REPO_READ && proc_type != PROC_REPO_WRITE)
1661 return got_error_msg(GOT_ERR_NOT_IMPL, "bad process type");
1663 proc = calloc(1, sizeof(*proc));
1665 return got_error_from_errno("calloc");
1667 TAILQ_INSERT_HEAD(&procs, proc, entry);
1668 evtimer_set(&proc->tmo, kill_proc_timeout, proc);
1670 proc->type = proc_type;
1671 if (strlcpy(proc->repo_name, repo->name,
1672 sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
1673 fatalx("repository name too long: %s", repo->name);
1674 log_debug("starting %s for repository %s",
1675 proc->type == PROC_REPO_READ ? "reader" : "writer", repo->name);
1677 if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >=
1678 sizeof(proc->repo_path))
1679 fatalx("repository path too long: %s", repo->path);
1680 if (realpath(repo->path, proc->repo_path) == NULL)
1681 fatal("%s", repo->path);
1682 if (socketpair(AF_UNIX, sock_flags,
1683 PF_UNSPEC, proc->pipe) == -1)
1684 fatal("socketpair");
1685 proc->pid = start_child(proc->type, proc->repo_path, argv0,
1686 confpath, proc->pipe[1], daemonize, verbosity);
1687 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1688 log_debug("proc %s %s is on fd %d",
1689 gotd_proc_names[proc->type], proc->repo_path,
1691 proc->iev.handler = gotd_dispatch_repo_child;
1692 proc->iev.events = EV_READ;
1693 proc->iev.handler_arg = NULL;
1694 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1695 gotd_dispatch_repo_child, &proc->iev);
1696 gotd_imsg_event_add(&proc->iev);
1698 client->repo = proc;
1702 static const struct got_error *
1703 start_auth_child(struct gotd_client *client, int required_auth,
1704 struct gotd_repo *repo, char *argv0, const char *confpath,
1705 int daemonize, int verbosity)
1707 const struct got_error *err = NULL;
1708 struct gotd_child_proc *proc;
1709 struct gotd_imsg_auth iauth;
1711 int sock_flags = SOCK_STREAM|SOCK_NONBLOCK;
1714 sock_flags |= SOCK_CLOEXEC;
1717 memset(&iauth, 0, sizeof(iauth));
1719 fd = dup(client->fd);
1721 return got_error_from_errno("dup");
1723 proc = calloc(1, sizeof(*proc));
1725 err = got_error_from_errno("calloc");
1730 TAILQ_INSERT_HEAD(&procs, proc, entry);
1731 evtimer_set(&proc->tmo, kill_proc_timeout, proc);
1733 proc->type = PROC_AUTH;
1734 if (strlcpy(proc->repo_name, repo->name,
1735 sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
1736 fatalx("repository name too long: %s", repo->name);
1737 log_debug("starting auth for uid %d repository %s",
1738 client->euid, repo->name);
1739 if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >=
1740 sizeof(proc->repo_path))
1741 fatalx("repository path too long: %s", repo->path);
1742 if (realpath(repo->path, proc->repo_path) == NULL)
1743 fatal("%s", repo->path);
1744 if (socketpair(AF_UNIX, sock_flags,
1745 PF_UNSPEC, proc->pipe) == -1)
1746 fatal("socketpair");
1747 proc->pid = start_child(proc->type, proc->repo_path, argv0,
1748 confpath, proc->pipe[1], daemonize, verbosity);
1749 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1750 log_debug("proc %s %s is on fd %d",
1751 gotd_proc_names[proc->type], proc->repo_path,
1753 proc->iev.handler = gotd_dispatch_auth_child;
1754 proc->iev.events = EV_READ;
1755 proc->iev.handler_arg = NULL;
1756 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1757 gotd_dispatch_auth_child, &proc->iev);
1758 gotd_imsg_event_add(&proc->iev);
1760 iauth.euid = client->euid;
1761 iauth.egid = client->egid;
1762 iauth.required_auth = required_auth;
1763 iauth.client_id = client->id;
1764 if (gotd_imsg_compose_event(&proc->iev, GOTD_IMSG_AUTHENTICATE,
1765 PROC_GOTD, fd, &iauth, sizeof(iauth)) == -1) {
1766 log_warn("imsg compose AUTHENTICATE");
1768 /* Let the auth_timeout handler tidy up. */
1771 client->auth = proc;
1772 client->required_auth = required_auth;
1777 apply_unveil_repo_readonly(const char *repo_path, int need_tmpdir)
1780 if (unveil(GOT_TMPDIR_STR, "rwc") == -1)
1781 fatal("unveil %s", GOT_TMPDIR_STR);
1784 if (unveil(repo_path, "r") == -1)
1785 fatal("unveil %s", repo_path);
1787 if (unveil(NULL, NULL) == -1)
1792 apply_unveil_repo_readwrite(const char *repo_path)
1794 if (unveil(repo_path, "rwc") == -1)
1795 fatal("unveil %s", repo_path);
1797 if (unveil(GOT_TMPDIR_STR, "rwc") == -1)
1798 fatal("unveil %s", GOT_TMPDIR_STR);
1800 if (unveil(NULL, NULL) == -1)
1805 apply_unveil_none(void)
1807 if (unveil("/", "") == -1)
1810 if (unveil(NULL, NULL) == -1)
1815 apply_unveil_selfexec(void)
1817 if (unveil(gotd.argv0, "x") == -1)
1818 fatal("unveil %s", gotd.argv0);
1820 if (unveil(NULL, NULL) == -1)
1825 main(int argc, char **argv)
1827 const struct got_error *error = NULL;
1828 int ch, fd = -1, daemonize = 1, verbosity = 0, noaction = 0;
1829 const char *confpath = GOTD_CONF_PATH;
1830 char *argv0 = argv[0];
1832 struct passwd *pw = NULL;
1833 char *repo_path = NULL;
1834 enum gotd_procid proc_id = PROC_GOTD;
1835 struct event evsigint, evsigterm, evsighup, evsigusr1, evsigchld;
1836 int *pack_fds = NULL, *temp_fds = NULL;
1837 struct gotd_repo *repo = NULL;
1841 log_init(1, LOG_DAEMON); /* Log to stderr until daemonized. */
1843 while ((ch = getopt(argc, argv, "Adf:LnP:RsSvW")) != -1) {
1846 proc_id = PROC_AUTH;
1855 proc_id = PROC_LISTEN;
1861 repo_path = realpath(optarg, NULL);
1862 if (repo_path == NULL)
1863 fatal("realpath '%s'", optarg);
1866 proc_id = PROC_REPO_READ;
1869 proc_id = PROC_SESSION_READ;
1872 proc_id = PROC_SESSION_WRITE;
1879 proc_id = PROC_REPO_WRITE;
1892 if (geteuid() && (proc_id == PROC_GOTD || proc_id == PROC_LISTEN))
1893 fatalx("need root privileges");
1895 if (parse_config(confpath, proc_id, &gotd) != 0)
1898 pw = getpwnam(gotd.user_name);
1900 fatalx("user %s not found", gotd.user_name);
1902 if (pw->pw_uid == 0)
1903 fatalx("cannot run %s as the superuser", getprogname());
1906 fprintf(stderr, "configuration OK\n");
1911 gotd.daemonize = daemonize;
1912 gotd.verbosity = verbosity;
1913 gotd.confpath = confpath;
1915 /* Require an absolute path in argv[0] for reliable re-exec. */
1916 if (!got_path_is_absolute(argv0))
1917 fatalx("bad path \"%s\": must be an absolute path", argv0);
1919 log_init(daemonize ? 0 : 1, LOG_DAEMON);
1920 log_setverbose(verbosity);
1922 if (proc_id == PROC_GOTD) {
1923 snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
1924 arc4random_buf(&clients_hash_key, sizeof(clients_hash_key));
1925 if (daemonize && daemon(1, 0) == -1)
1927 gotd.pid = getpid();
1928 start_listener(argv0, confpath, daemonize, verbosity);
1929 } else if (proc_id == PROC_LISTEN) {
1930 snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
1932 log_info("socket: %s", gotd.unix_socket_path);
1933 log_info("user: %s", pw->pw_name);
1936 fd = unix_socket_listen(gotd.unix_socket_path, pw->pw_uid,
1939 fatal("cannot listen on unix socket %s",
1940 gotd.unix_socket_path);
1942 } else if (proc_id == PROC_AUTH) {
1943 snprintf(title, sizeof(title), "%s %s",
1944 gotd_proc_names[proc_id], repo_path);
1945 } else if (proc_id == PROC_REPO_READ || proc_id == PROC_REPO_WRITE ||
1946 proc_id == PROC_SESSION_READ || proc_id == PROC_SESSION_WRITE) {
1947 error = got_repo_pack_fds_open(&pack_fds);
1949 fatalx("cannot open pack tempfiles: %s", error->msg);
1950 error = got_repo_temp_fds_open(&temp_fds);
1952 fatalx("cannot open pack tempfiles: %s", error->msg);
1953 if (repo_path == NULL)
1954 fatalx("repository path not specified");
1955 snprintf(title, sizeof(title), "%s %s",
1956 gotd_proc_names[proc_id], repo_path);
1958 fatal("invalid process id %d", proc_id);
1960 setproctitle("%s", title);
1961 log_procinit(title);
1963 /* Drop root privileges. */
1964 if (setgid(pw->pw_gid) == -1)
1965 fatal("setgid %d failed", pw->pw_gid);
1966 if (setuid(pw->pw_uid) == -1)
1967 fatal("setuid %d failed", pw->pw_uid);
1974 /* "exec" promise will be limited to argv[0] via unveil(2). */
1975 if (pledge("stdio proc exec sendfd recvfd unveil", NULL) == -1)
1981 if (pledge("stdio sendfd unix unveil", NULL) == -1)
1985 * Ensure that AF_UNIX bind(2) cannot be used with any other
1986 * sockets by revoking all filesystem access via unveil(2).
1988 apply_unveil_none();
1990 listen_main(title, fd, gotd.connection_limits,
1991 gotd.nconnection_limits);
1996 if (pledge("stdio getpw recvfd unix unveil", NULL) == -1)
2000 * We need the "unix" pledge promise for getpeername(2) only.
2001 * Ensure that AF_UNIX bind(2) cannot be used by revoking all
2002 * filesystem access via unveil(2). Access to password database
2003 * files will still work since "getpw" bypasses unveil(2).
2005 apply_unveil_none();
2007 auth_main(title, &gotd.repos, repo_path);
2010 case PROC_SESSION_READ:
2011 case PROC_SESSION_WRITE:
2014 * The "recvfd" promise is only needed during setup and
2015 * will be removed in a later pledge(2) call.
2017 if (pledge("stdio rpath wpath cpath recvfd sendfd fattr flock "
2018 "unveil", NULL) == -1)
2021 if (proc_id == PROC_SESSION_READ)
2022 apply_unveil_repo_readonly(repo_path, 1);
2024 apply_unveil_repo_readwrite(repo_path);
2025 session_main(title, repo_path, pack_fds, temp_fds,
2026 &gotd.request_timeout, proc_id);
2029 case PROC_REPO_READ:
2031 if (pledge("stdio rpath recvfd unveil", NULL) == -1)
2034 apply_unveil_repo_readonly(repo_path, 0);
2035 repo_read_main(title, repo_path, pack_fds, temp_fds);
2038 case PROC_REPO_WRITE:
2040 if (pledge("stdio rpath recvfd unveil", NULL) == -1)
2043 apply_unveil_repo_readonly(repo_path, 0);
2044 repo = gotd_find_repo_by_path(repo_path, &gotd);
2046 fatalx("no repository for path %s", repo_path);
2047 repo_write_main(title, repo_path, pack_fds, temp_fds,
2048 &repo->protected_tag_namespaces,
2049 &repo->protected_branch_namespaces,
2050 &repo->protected_branches);
2054 fatal("invalid process id %d", proc_id);
2057 if (proc_id != PROC_GOTD)
2058 fatal("invalid process id %d", proc_id);
2060 evtimer_set(&gotd.listen_proc->tmo, kill_proc_timeout,
2063 apply_unveil_selfexec();
2065 signal_set(&evsigint, SIGINT, gotd_sighdlr, NULL);
2066 signal_set(&evsigterm, SIGTERM, gotd_sighdlr, NULL);
2067 signal_set(&evsighup, SIGHUP, gotd_sighdlr, NULL);
2068 signal_set(&evsigusr1, SIGUSR1, gotd_sighdlr, NULL);
2069 signal_set(&evsigchld, SIGCHLD, gotd_sighdlr, NULL);
2070 signal(SIGPIPE, SIG_IGN);
2072 signal_add(&evsigint, NULL);
2073 signal_add(&evsigterm, NULL);
2074 signal_add(&evsighup, NULL);
2075 signal_add(&evsigusr1, NULL);
2076 signal_add(&evsigchld, NULL);
2078 gotd_imsg_event_add(&gotd.listen_proc->iev);