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>
25 #include <sys/resource.h>
45 #include "got_error.h"
46 #include "got_opentemp.h"
48 #include "got_repository.h"
49 #include "got_object.h"
50 #include "got_reference.h"
52 #include "got_lib_delta.h"
53 #include "got_lib_object.h"
54 #include "got_lib_object_cache.h"
55 #include "got_lib_hash.h"
56 #include "got_lib_gitproto.h"
57 #include "got_lib_pack.h"
58 #include "got_lib_repository.h"
65 #include "repo_read.h"
66 #include "repo_write.h"
69 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
72 enum gotd_client_state {
73 GOTD_CLIENT_STATE_NEW,
74 GOTD_CLIENT_STATE_ACCESS_GRANTED,
77 struct gotd_child_proc {
79 enum gotd_procid type;
80 char repo_name[NAME_MAX];
81 char repo_path[PATH_MAX];
83 struct gotd_imsgev iev;
86 TAILQ_ENTRY(gotd_child_proc) entry;
88 TAILQ_HEAD(gotd_procs, gotd_child_proc) procs;
91 STAILQ_ENTRY(gotd_client) entry;
92 enum gotd_client_state state;
95 struct gotd_imsgev iev;
99 struct gotd_child_proc *repo;
100 struct gotd_child_proc *auth;
101 struct gotd_child_proc *session;
104 STAILQ_HEAD(gotd_clients, gotd_client);
106 static struct gotd_clients gotd_clients[GOTD_CLIENT_TABLE_SIZE];
107 static SIPHASH_KEY clients_hash_key;
108 volatile int client_cnt;
109 static struct timeval auth_timeout = { 5, 0 };
110 static struct gotd gotd;
112 void gotd_sighdlr(int sig, short event, void *arg);
113 static void gotd_shutdown(void);
114 static const struct got_error *start_session_child(struct gotd_client *,
115 struct gotd_repo *, char *, const char *, int, int);
116 static const struct got_error *start_repo_child(struct gotd_client *,
117 enum gotd_procid, struct gotd_repo *, char *, const char *, int, int);
118 static const struct got_error *start_auth_child(struct gotd_client *, int,
119 struct gotd_repo *, char *, const char *, int, int);
120 static void kill_proc(struct gotd_child_proc *, int);
121 static void disconnect(struct gotd_client *);
126 fprintf(stderr, "usage: %s [-dnv] [-f config-file]\n", getprogname());
131 unix_socket_listen(const char *unix_socket_path, uid_t uid, gid_t gid)
133 struct sockaddr_un sun;
135 mode_t old_umask, mode;
137 fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK| SOCK_CLOEXEC, 0);
143 sun.sun_family = AF_UNIX;
144 if (strlcpy(sun.sun_path, unix_socket_path,
145 sizeof(sun.sun_path)) >= sizeof(sun.sun_path)) {
146 log_warnx("%s: name too long", unix_socket_path);
151 if (unlink(unix_socket_path) == -1) {
152 if (errno != ENOENT) {
153 log_warn("unlink %s", unix_socket_path);
159 old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
160 mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
162 if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
163 log_warn("bind: %s", unix_socket_path);
171 if (chmod(unix_socket_path, mode) == -1) {
172 log_warn("chmod %o %s", mode, unix_socket_path);
174 unlink(unix_socket_path);
178 if (chown(unix_socket_path, uid, gid) == -1) {
179 log_warn("chown %s uid=%d gid=%d", unix_socket_path, uid, gid);
181 unlink(unix_socket_path);
185 if (listen(fd, GOTD_UNIX_SOCKET_BACKLOG) == -1) {
188 unlink(unix_socket_path);
196 client_hash(uint32_t client_id)
198 return SipHash24(&clients_hash_key, &client_id, sizeof(client_id));
202 add_client(struct gotd_client *client)
204 uint64_t slot = client_hash(client->id) % nitems(gotd_clients);
205 STAILQ_INSERT_HEAD(&gotd_clients[slot], client, entry);
209 static struct gotd_client *
210 find_client(uint32_t client_id)
213 struct gotd_client *c;
215 slot = client_hash(client_id) % nitems(gotd_clients);
216 STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
217 if (c->id == client_id)
224 static struct gotd_client *
225 find_client_by_proc_fd(int fd)
229 for (slot = 0; slot < nitems(gotd_clients); slot++) {
230 struct gotd_client *c;
232 STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
233 if (c->repo && c->repo->iev.ibuf.fd == fd)
235 if (c->auth && c->auth->iev.ibuf.fd == fd)
237 if (c->session && c->session->iev.ibuf.fd == fd)
246 client_is_reading(struct gotd_client *client)
248 return (client->required_auth &
249 (GOTD_AUTH_READ | GOTD_AUTH_WRITE)) == GOTD_AUTH_READ;
253 client_is_writing(struct gotd_client *client)
255 return (client->required_auth &
256 (GOTD_AUTH_READ | GOTD_AUTH_WRITE)) ==
257 (GOTD_AUTH_READ | GOTD_AUTH_WRITE);
260 static const struct got_error *
261 ensure_client_is_not_writing(struct gotd_client *client)
263 if (client_is_writing(client)) {
264 return got_error_fmt(GOT_ERR_BAD_PACKET,
265 "uid %d made a read-request but is writing to "
266 "a repository", client->euid);
272 static const struct got_error *
273 ensure_client_is_not_reading(struct gotd_client *client)
275 if (client_is_reading(client)) {
276 return got_error_fmt(GOT_ERR_BAD_PACKET,
277 "uid %d made a write-request but is reading from "
278 "a repository", client->euid);
285 proc_done(struct gotd_child_proc *proc)
287 struct gotd_client *client;
289 TAILQ_REMOVE(&procs, proc, entry);
291 client = find_client_by_proc_fd(proc->iev.ibuf.fd);
292 if (client != NULL) {
293 if (proc == client->repo)
295 if (proc == client->auth)
297 if (proc == client->session)
298 client->session = NULL;
302 evtimer_del(&proc->tmo);
304 if (proc->iev.ibuf.fd != -1) {
305 event_del(&proc->iev.ev);
306 msgbuf_clear(&proc->iev.ibuf.w);
307 close(proc->iev.ibuf.fd);
314 kill_repo_proc(struct gotd_client *client)
316 if (client->repo == NULL)
319 kill_proc(client->repo, 0);
324 kill_auth_proc(struct gotd_client *client)
326 if (client->auth == NULL)
329 kill_proc(client->auth, 0);
334 kill_session_proc(struct gotd_client *client)
336 if (client->session == NULL)
339 kill_proc(client->session, 0);
340 client->session = NULL;
344 disconnect(struct gotd_client *client)
346 struct gotd_imsg_disconnect idisconnect;
347 struct gotd_child_proc *listen_proc = gotd.listen_proc;
350 log_debug("uid %d: disconnecting", client->euid);
352 kill_auth_proc(client);
353 kill_session_proc(client);
354 kill_repo_proc(client);
356 idisconnect.client_id = client->id;
357 if (gotd_imsg_compose_event(&listen_proc->iev,
358 GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
359 &idisconnect, sizeof(idisconnect)) == -1)
360 log_warn("imsg compose DISCONNECT");
362 slot = client_hash(client->id) % nitems(gotd_clients);
363 STAILQ_REMOVE(&gotd_clients[slot], client, gotd_client, entry);
364 imsg_clear(&client->iev.ibuf);
365 event_del(&client->iev.ev);
366 evtimer_del(&client->tmo);
367 if (client->fd != -1)
369 else if (client->iev.ibuf.fd != -1)
370 close(client->iev.ibuf.fd);
376 disconnect_on_error(struct gotd_client *client, const struct got_error *err)
380 if (err->code != GOT_ERR_EOF) {
381 log_warnx("uid %d: %s", client->euid, err->msg);
382 if (client->fd != -1) {
383 imsg_init(&ibuf, client->fd);
384 gotd_imsg_send_error(&ibuf, 0, PROC_GOTD, err);
391 static const struct got_error *
392 send_repo_info(struct gotd_imsgev *iev, struct gotd_repo *repo)
394 const struct got_error *err = NULL;
395 struct gotd_imsg_info_repo irepo;
397 memset(&irepo, 0, sizeof(irepo));
399 if (strlcpy(irepo.repo_name, repo->name, sizeof(irepo.repo_name))
400 >= sizeof(irepo.repo_name))
401 return got_error_msg(GOT_ERR_NO_SPACE, "repo name too long");
402 if (strlcpy(irepo.repo_path, repo->path, sizeof(irepo.repo_path))
403 >= sizeof(irepo.repo_path))
404 return got_error_msg(GOT_ERR_NO_SPACE, "repo path too long");
406 if (gotd_imsg_compose_event(iev, GOTD_IMSG_INFO_REPO, PROC_GOTD, -1,
407 &irepo, sizeof(irepo)) == -1) {
408 err = got_error_from_errno("imsg compose INFO_REPO");
416 static const struct got_error *
417 send_client_info(struct gotd_imsgev *iev, struct gotd_client *client)
419 const struct got_error *err = NULL;
420 struct gotd_imsg_info_client iclient;
421 struct gotd_child_proc *proc;
423 memset(&iclient, 0, sizeof(iclient));
424 iclient.euid = client->euid;
425 iclient.egid = client->egid;
429 if (strlcpy(iclient.repo_name, proc->repo_path,
430 sizeof(iclient.repo_name)) >= sizeof(iclient.repo_name)) {
431 return got_error_msg(GOT_ERR_NO_SPACE,
432 "repo name too long");
434 if (client_is_writing(client))
435 iclient.is_writing = 1;
437 iclient.repo_child_pid = proc->pid;
441 iclient.session_child_pid = client->session->pid;
443 if (gotd_imsg_compose_event(iev, GOTD_IMSG_INFO_CLIENT, PROC_GOTD, -1,
444 &iclient, sizeof(iclient)) == -1) {
445 err = got_error_from_errno("imsg compose INFO_CLIENT");
453 static const struct got_error *
454 send_info(struct gotd_client *client)
456 const struct got_error *err = NULL;
457 struct gotd_imsg_info info;
459 struct gotd_repo *repo;
461 if (client->euid != 0)
462 return got_error_set_errno(EPERM, "info");
465 info.verbosity = gotd.verbosity;
466 info.nrepos = gotd.nrepos;
467 info.nclients = client_cnt - 1;
469 if (gotd_imsg_compose_event(&client->iev, GOTD_IMSG_INFO, PROC_GOTD, -1,
470 &info, sizeof(info)) == -1) {
471 err = got_error_from_errno("imsg compose INFO");
476 TAILQ_FOREACH(repo, &gotd.repos, entry) {
477 err = send_repo_info(&client->iev, repo);
482 for (slot = 0; slot < nitems(gotd_clients); slot++) {
483 struct gotd_client *c;
484 STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
485 if (c->id == client->id)
487 err = send_client_info(&client->iev, c);
496 static const struct got_error *
497 stop_gotd(struct gotd_client *client)
500 if (client->euid != 0)
501 return got_error_set_errno(EPERM, "stop");
508 static const struct got_error *
509 start_client_authentication(struct gotd_client *client, struct imsg *imsg)
511 const struct got_error *err;
512 struct gotd_imsg_list_refs ireq;
513 struct gotd_repo *repo = NULL;
516 log_debug("list-refs request from uid %d", client->euid);
518 if (client->state != GOTD_CLIENT_STATE_NEW)
519 return got_error_msg(GOT_ERR_BAD_REQUEST,
520 "unexpected list-refs request received");
522 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
523 if (datalen != sizeof(ireq))
524 return got_error(GOT_ERR_PRIVSEP_LEN);
526 memcpy(&ireq, imsg->data, datalen);
528 if (ireq.client_is_reading) {
529 err = ensure_client_is_not_writing(client);
532 repo = gotd_find_repo_by_name(ireq.repo_name, &gotd);
534 return got_error(GOT_ERR_NOT_GIT_REPO);
535 err = start_auth_child(client, GOTD_AUTH_READ, repo,
536 gotd.argv0, gotd.confpath, gotd.daemonize,
541 err = ensure_client_is_not_reading(client);
544 repo = gotd_find_repo_by_name(ireq.repo_name, &gotd);
546 return got_error(GOT_ERR_NOT_GIT_REPO);
547 err = start_auth_child(client,
548 GOTD_AUTH_READ | GOTD_AUTH_WRITE,
549 repo, gotd.argv0, gotd.confpath, gotd.daemonize,
555 evtimer_add(&client->tmo, &auth_timeout);
557 /* Flow continues upon authentication success/failure or timeout. */
562 gotd_request(int fd, short events, void *arg)
564 struct gotd_imsgev *iev = arg;
565 struct imsgbuf *ibuf = &iev->ibuf;
566 struct gotd_client *client = iev->handler_arg;
567 const struct got_error *err = NULL;
571 if (events & EV_WRITE) {
572 while (ibuf->w.queued) {
573 n = msgbuf_write(&ibuf->w);
574 if (n == -1 && errno == EPIPE) {
576 * The client has closed its socket.
577 * This can happen when Git clients are
578 * done sending pack file data.
580 msgbuf_clear(&ibuf->w);
582 } else if (n == -1 && errno != EAGAIN) {
583 err = got_error_from_errno("imsg_flush");
584 disconnect_on_error(client, err);
588 /* Connection closed. */
589 err = got_error(GOT_ERR_EOF);
590 disconnect_on_error(client, err);
595 /* Disconnect gotctl(8) now that messages have been sent. */
596 if (!client_is_reading(client) && !client_is_writing(client)) {
602 if ((events & EV_READ) == 0)
605 memset(&imsg, 0, sizeof(imsg));
607 while (err == NULL) {
608 err = gotd_imsg_recv(&imsg, ibuf, 0);
610 if (err->code == GOT_ERR_PRIVSEP_READ)
615 evtimer_del(&client->tmo);
617 switch (imsg.hdr.type) {
619 err = send_info(client);
622 err = stop_gotd(client);
624 case GOTD_IMSG_LIST_REFS:
625 err = start_client_authentication(client, &imsg);
628 log_debug("unexpected imsg %d", imsg.hdr.type);
629 err = got_error(GOT_ERR_PRIVSEP_MSG);
637 disconnect_on_error(client, err);
639 gotd_imsg_event_add(&client->iev);
644 gotd_auth_timeout(int fd, short events, void *arg)
646 struct gotd_client *client = arg;
648 log_debug("disconnecting uid %d due to authentication timeout",
653 static const struct got_error *
654 recv_connect(uint32_t *client_id, struct imsg *imsg)
656 const struct got_error *err = NULL;
657 struct gotd_imsg_connect iconnect;
660 struct gotd_client *client = NULL;
664 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
665 if (datalen != sizeof(iconnect))
666 return got_error(GOT_ERR_PRIVSEP_LEN);
667 memcpy(&iconnect, imsg->data, sizeof(iconnect));
671 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
675 if (find_client(iconnect.client_id)) {
676 err = got_error_msg(GOT_ERR_CLIENT_ID, "duplicate client ID");
680 client = calloc(1, sizeof(*client));
681 if (client == NULL) {
682 err = got_error_from_errno("calloc");
686 *client_id = iconnect.client_id;
688 client->state = GOTD_CLIENT_STATE_NEW;
689 client->id = iconnect.client_id;
692 /* The auth process will verify UID/GID for us. */
693 client->euid = iconnect.euid;
694 client->egid = iconnect.egid;
696 imsg_init(&client->iev.ibuf, client->fd);
697 client->iev.handler = gotd_request;
698 client->iev.events = EV_READ;
699 client->iev.handler_arg = client;
701 event_set(&client->iev.ev, client->fd, EV_READ, gotd_request,
703 gotd_imsg_event_add(&client->iev);
705 evtimer_set(&client->tmo, gotd_auth_timeout, client);
708 log_debug("%s: new client uid %d connected on fd %d", __func__,
709 client->euid, client->fd);
712 struct gotd_child_proc *listen_proc = gotd.listen_proc;
713 struct gotd_imsg_disconnect idisconnect;
715 idisconnect.client_id = client->id;
716 if (gotd_imsg_compose_event(&listen_proc->iev,
717 GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
718 &idisconnect, sizeof(idisconnect)) == -1)
719 log_warn("imsg compose DISCONNECT");
728 static const char *gotd_proc_names[PROC_MAX] = {
740 kill_proc(struct gotd_child_proc *proc, int fatal)
742 struct timeval tv = { 5, 0 };
744 log_debug("kill -%d %d", fatal ? SIGKILL : SIGTERM, proc->pid);
746 if (proc->iev.ibuf.fd != -1) {
747 event_del(&proc->iev.ev);
748 msgbuf_clear(&proc->iev.ibuf.w);
749 close(proc->iev.ibuf.fd);
750 proc->iev.ibuf.fd = -1;
753 if (!evtimer_pending(&proc->tmo, NULL) && !fatal)
754 evtimer_add(&proc->tmo, &tv);
757 log_warnx("sending SIGKILL to PID %d", proc->pid);
758 kill(proc->pid, SIGKILL);
760 kill(proc->pid, SIGTERM);
764 kill_proc_timeout(int fd, short ev, void *d)
766 struct gotd_child_proc *proc = d;
768 log_warnx("timeout waiting for PID %d to terminate;"
769 " retrying with force", proc->pid);
778 log_debug("shutting down");
779 for (slot = 0; slot < nitems(gotd_clients); slot++) {
780 struct gotd_client *c, *tmp;
782 STAILQ_FOREACH_SAFE(c, &gotd_clients[slot], entry, tmp)
786 kill_proc(gotd.listen_proc, 0);
788 log_info("terminating");
792 static struct gotd_child_proc *
793 find_proc_by_pid(pid_t pid)
795 struct gotd_child_proc *proc = NULL;
797 TAILQ_FOREACH(proc, &procs, entry)
798 if (proc->pid == pid)
805 gotd_sighdlr(int sig, short event, void *arg)
807 struct gotd_child_proc *proc;
812 * Normal signal handler rules don't apply because libevent
818 log_info("%s: ignoring SIGHUP", __func__);
821 log_info("%s: ignoring SIGUSR1", __func__);
829 pid = waitpid(WAIT_ANY, &status, WNOHANG);
840 log_debug("reaped pid %d", pid);
841 proc = find_proc_by_pid(pid);
843 log_info("caught exit of unknown child %d",
848 if (WIFSIGNALED(status)) {
849 log_warnx("child PID %d terminated with"
850 " signal %d", pid, WTERMSIG(status));
857 fatalx("unexpected signal");
861 static const struct got_error *
862 ensure_proc_is_reading(struct gotd_client *client,
863 struct gotd_child_proc *proc)
865 if (!client_is_reading(client)) {
867 return got_error_fmt(GOT_ERR_BAD_PACKET,
868 "PID %d handled a read-request for uid %d but this "
869 "user is not reading from a repository", proc->pid,
876 static const struct got_error *
877 ensure_proc_is_writing(struct gotd_client *client,
878 struct gotd_child_proc *proc)
880 if (!client_is_writing(client)) {
882 return got_error_fmt(GOT_ERR_BAD_PACKET,
883 "PID %d handled a write-request for uid %d but this "
884 "user is not writing to a repository", proc->pid,
892 verify_imsg_src(struct gotd_client *client, struct gotd_child_proc *proc,
895 const struct got_error *err;
898 if (proc->type == PROC_REPO_READ || proc->type == PROC_REPO_WRITE) {
899 if (client->repo == NULL)
900 fatalx("no process found for uid %d", client->euid);
901 if (proc->pid != client->repo->pid) {
903 log_warnx("received message from PID %d for uid %d, "
904 "while PID %d is the process serving this user",
905 proc->pid, client->euid, client->repo->pid);
909 if (proc->type == PROC_SESSION_READ ||
910 proc->type == PROC_SESSION_WRITE) {
911 if (client->session == NULL) {
912 log_warnx("no session found for uid %d", client->euid);
915 if (proc->pid != client->session->pid) {
917 log_warnx("received message from PID %d for uid %d, "
918 "while PID %d is the process serving this user",
919 proc->pid, client->euid, client->session->pid);
924 switch (imsg->hdr.type) {
925 case GOTD_IMSG_ERROR:
928 case GOTD_IMSG_CONNECT:
929 if (proc->type != PROC_LISTEN) {
930 err = got_error_fmt(GOT_ERR_BAD_PACKET,
931 "new connection for uid %d from PID %d "
932 "which is not the listen process",
933 proc->pid, client->euid);
937 case GOTD_IMSG_ACCESS_GRANTED:
938 if (proc->type != PROC_AUTH) {
939 err = got_error_fmt(GOT_ERR_BAD_PACKET,
940 "authentication of uid %d from PID %d "
941 "which is not the auth process",
942 proc->pid, client->euid);
946 case GOTD_IMSG_CLIENT_SESSION_READY:
947 if (proc->type != PROC_SESSION_READ &&
948 proc->type != PROC_SESSION_WRITE) {
949 err = got_error_fmt(GOT_ERR_BAD_PACKET,
950 "unexpected \"ready\" signal from PID %d",
955 case GOTD_IMSG_REPO_CHILD_READY:
956 if (proc->type != PROC_REPO_READ &&
957 proc->type != PROC_REPO_WRITE) {
958 err = got_error_fmt(GOT_ERR_BAD_PACKET,
959 "unexpected \"ready\" signal from PID %d",
964 case GOTD_IMSG_PACKFILE_DONE:
965 err = ensure_proc_is_reading(client, proc);
967 log_warnx("uid %d: %s", client->euid, err->msg);
971 case GOTD_IMSG_PACKFILE_INSTALL:
972 case GOTD_IMSG_REF_UPDATES_START:
973 case GOTD_IMSG_REF_UPDATE:
974 err = ensure_proc_is_writing(client, proc);
976 log_warnx("uid %d: %s", client->euid, err->msg);
981 log_debug("%s: unexpected imsg %d", __func__, imsg->hdr.type);
988 static const struct got_error *
989 connect_repo_child(struct gotd_client *client,
990 struct gotd_child_proc *repo_proc)
992 static const struct got_error *err;
993 struct gotd_imsgev *session_iev = &client->session->iev;
994 struct gotd_imsg_connect_repo_child ireq;
997 if (client->state != GOTD_CLIENT_STATE_ACCESS_GRANTED)
998 return got_error_msg(GOT_ERR_BAD_REQUEST,
999 "unexpected repo child ready signal received");
1001 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1002 PF_UNSPEC, pipe) == -1)
1003 fatal("socketpair");
1005 memset(&ireq, 0, sizeof(ireq));
1006 ireq.client_id = client->id;
1007 ireq.proc_id = repo_proc->type;
1009 /* Pass repo child pipe to session child process. */
1010 if (gotd_imsg_compose_event(session_iev, GOTD_IMSG_CONNECT_REPO_CHILD,
1011 PROC_GOTD, pipe[0], &ireq, sizeof(ireq)) == -1) {
1012 err = got_error_from_errno("imsg compose CONNECT_REPO_CHILD");
1018 /* Pass session child pipe to repo child process. */
1019 if (gotd_imsg_compose_event(&repo_proc->iev,
1020 GOTD_IMSG_CONNECT_REPO_CHILD, PROC_GOTD, pipe[1], NULL, 0) == -1) {
1021 err = got_error_from_errno("imsg compose CONNECT_REPO_CHILD");
1030 gotd_dispatch_listener(int fd, short event, void *arg)
1032 struct gotd_imsgev *iev = arg;
1033 struct imsgbuf *ibuf = &iev->ibuf;
1034 struct gotd_child_proc *proc = gotd.listen_proc;
1039 if (proc->iev.ibuf.fd != fd)
1040 fatalx("%s: unexpected fd %d", __func__, fd);
1042 if (event & EV_READ) {
1043 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1044 fatal("imsg_read error");
1046 /* Connection closed. */
1052 if (event & EV_WRITE) {
1053 n = msgbuf_write(&ibuf->w);
1054 if (n == -1 && errno != EAGAIN)
1055 fatal("msgbuf_write");
1057 /* Connection closed. */
1064 const struct got_error *err = NULL;
1065 struct gotd_client *client = NULL;
1066 uint32_t client_id = 0;
1067 int do_disconnect = 0;
1069 if ((n = imsg_get(ibuf, &imsg)) == -1)
1070 fatal("%s: imsg_get error", __func__);
1071 if (n == 0) /* No more messages. */
1074 switch (imsg.hdr.type) {
1075 case GOTD_IMSG_ERROR:
1077 err = gotd_imsg_recv_error(&client_id, &imsg);
1079 case GOTD_IMSG_CONNECT:
1080 err = recv_connect(&client_id, &imsg);
1083 log_debug("unexpected imsg %d", imsg.hdr.type);
1087 client = find_client(client_id);
1088 if (client == NULL) {
1089 log_warnx("%s: client not found", __func__);
1095 log_warnx("uid %d: %s", client->euid, err->msg);
1097 if (do_disconnect) {
1099 disconnect_on_error(client, err);
1108 gotd_imsg_event_add(iev);
1110 /* This pipe is dead. Remove its event handler */
1111 event_del(&iev->ev);
1112 event_loopexit(NULL);
1117 gotd_dispatch_auth_child(int fd, short event, void *arg)
1119 const struct got_error *err = NULL;
1120 struct gotd_imsgev *iev = arg;
1121 struct imsgbuf *ibuf = &iev->ibuf;
1122 struct gotd_client *client;
1123 struct gotd_repo *repo = NULL;
1127 uint32_t client_id = 0;
1128 int do_disconnect = 0;
1130 client = find_client_by_proc_fd(fd);
1131 if (client == NULL) {
1132 /* Can happen during process teardown. */
1133 warnx("cannot find client for fd %d", fd);
1138 if (client->auth == NULL)
1139 fatalx("cannot find auth child process for fd %d", fd);
1141 if (event & EV_READ) {
1142 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1143 fatal("imsg_read error");
1145 /* Connection closed. */
1151 if (event & EV_WRITE) {
1152 n = msgbuf_write(&ibuf->w);
1153 if (n == -1 && errno != EAGAIN)
1154 fatal("msgbuf_write");
1156 /* Connection closed. */
1162 if (client->auth->iev.ibuf.fd != fd)
1163 fatalx("%s: unexpected fd %d", __func__, fd);
1165 if ((n = imsg_get(ibuf, &imsg)) == -1)
1166 fatal("%s: imsg_get error", __func__);
1167 if (n == 0) /* No more messages. */
1170 evtimer_del(&client->tmo);
1172 switch (imsg.hdr.type) {
1173 case GOTD_IMSG_ERROR:
1175 err = gotd_imsg_recv_error(&client_id, &imsg);
1177 case GOTD_IMSG_ACCESS_GRANTED:
1178 client->state = GOTD_CLIENT_STATE_ACCESS_GRANTED;
1182 log_debug("unexpected imsg %d", imsg.hdr.type);
1186 if (!verify_imsg_src(client, client->auth, &imsg)) {
1188 log_debug("dropping imsg type %d from PID %d",
1189 imsg.hdr.type, client->auth->pid);
1193 if (do_disconnect) {
1195 disconnect_on_error(client, err);
1201 repo = gotd_find_repo_by_name(client->auth->repo_name, &gotd);
1203 err = got_error(GOT_ERR_NOT_GIT_REPO);
1206 kill_auth_proc(client);
1208 log_info("authenticated uid %d for repository %s",
1209 client->euid, repo->name);
1211 err = start_session_child(client, repo, gotd.argv0,
1212 gotd.confpath, gotd.daemonize, gotd.verbosity);
1217 log_warnx("uid %d: %s", client->euid, err->msg);
1219 /* We might have killed the auth process by now. */
1220 if (client->auth != NULL) {
1222 gotd_imsg_event_add(iev);
1224 /* This pipe is dead. Remove its event handler */
1225 event_del(&iev->ev);
1230 static const struct got_error *
1231 connect_session(struct gotd_client *client)
1233 const struct got_error *err = NULL;
1234 struct gotd_imsg_connect iconnect;
1237 memset(&iconnect, 0, sizeof(iconnect));
1239 s = dup(client->fd);
1241 return got_error_from_errno("dup");
1243 iconnect.client_id = client->id;
1244 iconnect.euid = client->euid;
1245 iconnect.egid = client->egid;
1247 if (gotd_imsg_compose_event(&client->session->iev, GOTD_IMSG_CONNECT,
1248 PROC_GOTD, s, &iconnect, sizeof(iconnect)) == -1) {
1249 err = got_error_from_errno("imsg compose CONNECT");
1255 * We are no longer interested in messages from this client.
1256 * Further client requests will be handled by the session process.
1258 msgbuf_clear(&client->iev.ibuf.w);
1259 imsg_clear(&client->iev.ibuf);
1260 event_del(&client->iev.ev);
1261 client->fd = -1; /* will be closed via copy in client->iev.ibuf.fd */
1267 gotd_dispatch_client_session(int fd, short event, void *arg)
1269 struct gotd_imsgev *iev = arg;
1270 struct imsgbuf *ibuf = &iev->ibuf;
1271 struct gotd_child_proc *proc = NULL;
1272 struct gotd_client *client = NULL;
1277 client = find_client_by_proc_fd(fd);
1278 if (client == NULL) {
1279 /* Can happen during process teardown. */
1280 warnx("cannot find client for fd %d", fd);
1285 if (event & EV_READ) {
1286 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1287 fatal("imsg_read error");
1289 /* Connection closed. */
1295 if (event & EV_WRITE) {
1296 n = msgbuf_write(&ibuf->w);
1297 if (n == -1 && errno != EAGAIN)
1298 fatal("msgbuf_write");
1300 /* Connection closed. */
1306 proc = client->session;
1308 fatalx("cannot find session child process for fd %d", fd);
1311 const struct got_error *err = NULL;
1312 uint32_t client_id = 0;
1313 int do_disconnect = 0, do_start_repo_child = 0;
1315 if ((n = imsg_get(ibuf, &imsg)) == -1)
1316 fatal("%s: imsg_get error", __func__);
1317 if (n == 0) /* No more messages. */
1320 switch (imsg.hdr.type) {
1321 case GOTD_IMSG_ERROR:
1323 err = gotd_imsg_recv_error(&client_id, &imsg);
1325 case GOTD_IMSG_CLIENT_SESSION_READY:
1326 if (client->state != GOTD_CLIENT_STATE_ACCESS_GRANTED) {
1327 err = got_error(GOT_ERR_PRIVSEP_MSG);
1330 do_start_repo_child = 1;
1332 case GOTD_IMSG_DISCONNECT:
1336 log_debug("unexpected imsg %d", imsg.hdr.type);
1340 if (!verify_imsg_src(client, proc, &imsg)) {
1341 log_debug("dropping imsg type %d from PID %d",
1342 imsg.hdr.type, proc->pid);
1347 log_warnx("uid %d: %s", client->euid, err->msg);
1349 if (do_start_repo_child) {
1350 struct gotd_repo *repo;
1351 const char *name = client->session->repo_name;
1353 repo = gotd_find_repo_by_name(name, &gotd);
1355 enum gotd_procid proc_type;
1357 if (client->required_auth & GOTD_AUTH_WRITE)
1358 proc_type = PROC_REPO_WRITE;
1360 proc_type = PROC_REPO_READ;
1362 err = start_repo_child(client, proc_type, repo,
1363 gotd.argv0, gotd.confpath, gotd.daemonize,
1366 err = got_error(GOT_ERR_NOT_GIT_REPO);
1369 log_warnx("uid %d: %s", client->euid, err->msg);
1374 if (do_disconnect) {
1376 disconnect_on_error(client, err);
1385 gotd_imsg_event_add(iev);
1387 /* This pipe is dead. Remove its event handler */
1388 event_del(&iev->ev);
1394 gotd_dispatch_repo_child(int fd, short event, void *arg)
1396 struct gotd_imsgev *iev = arg;
1397 struct imsgbuf *ibuf = &iev->ibuf;
1398 struct gotd_child_proc *proc = NULL;
1399 struct gotd_client *client;
1404 client = find_client_by_proc_fd(fd);
1405 if (client == NULL) {
1406 /* Can happen during process teardown. */
1407 warnx("cannot find client for fd %d", fd);
1412 if (event & EV_READ) {
1413 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1414 fatal("imsg_read error");
1416 /* Connection closed. */
1422 if (event & EV_WRITE) {
1423 n = msgbuf_write(&ibuf->w);
1424 if (n == -1 && errno != EAGAIN)
1425 fatal("msgbuf_write");
1427 /* Connection closed. */
1433 proc = client->repo;
1435 fatalx("cannot find child process for fd %d", fd);
1438 const struct got_error *err = NULL;
1439 uint32_t client_id = 0;
1440 int do_disconnect = 0;
1442 if ((n = imsg_get(ibuf, &imsg)) == -1)
1443 fatal("%s: imsg_get error", __func__);
1444 if (n == 0) /* No more messages. */
1447 switch (imsg.hdr.type) {
1448 case GOTD_IMSG_ERROR:
1450 err = gotd_imsg_recv_error(&client_id, &imsg);
1452 case GOTD_IMSG_REPO_CHILD_READY:
1453 err = connect_session(client);
1456 err = connect_repo_child(client, proc);
1459 log_debug("unexpected imsg %d", imsg.hdr.type);
1463 if (!verify_imsg_src(client, proc, &imsg)) {
1464 log_debug("dropping imsg type %d from PID %d",
1465 imsg.hdr.type, proc->pid);
1470 log_warnx("uid %d: %s", client->euid, err->msg);
1472 if (do_disconnect) {
1474 disconnect_on_error(client, err);
1483 gotd_imsg_event_add(iev);
1485 /* This pipe is dead. Remove its event handler */
1486 event_del(&iev->ev);
1492 start_child(enum gotd_procid proc_id, const char *repo_path,
1493 char *argv0, const char *confpath, int fd, int daemonize, int verbosity)
1499 switch (pid = fork()) {
1501 fatal("cannot fork");
1509 if (fd != GOTD_FILENO_MSG_PIPE) {
1510 if (dup2(fd, GOTD_FILENO_MSG_PIPE) == -1)
1511 fatal("cannot setup imsg fd");
1512 } else if (fcntl(fd, F_SETFD, 0) == -1)
1513 fatal("cannot setup imsg fd");
1515 argv[argc++] = argv0;
1518 argv[argc++] = (char *)"-L";
1521 argv[argc++] = (char *)"-A";
1523 case PROC_SESSION_READ:
1524 argv[argc++] = (char *)"-s";
1526 case PROC_SESSION_WRITE:
1527 argv[argc++] = (char *)"-S";
1529 case PROC_REPO_READ:
1530 argv[argc++] = (char *)"-R";
1532 case PROC_REPO_WRITE:
1533 argv[argc++] = (char *)"-W";
1536 fatalx("invalid process id %d", proc_id);
1539 argv[argc++] = (char *)"-f";
1540 argv[argc++] = (char *)confpath;
1543 argv[argc++] = (char *)"-P";
1544 argv[argc++] = (char *)repo_path;
1548 argv[argc++] = (char *)"-d";
1550 argv[argc++] = (char *)"-v";
1552 argv[argc++] = (char *)"-v";
1553 argv[argc++] = NULL;
1555 execvp(argv0, argv);
1560 start_listener(char *argv0, const char *confpath, int daemonize, int verbosity)
1562 struct gotd_child_proc *proc;
1564 proc = calloc(1, sizeof(*proc));
1568 TAILQ_INSERT_HEAD(&procs, proc, entry);
1570 /* proc->tmo is initialized in main() after event_init() */
1572 proc->type = PROC_LISTEN;
1574 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1575 PF_UNSPEC, proc->pipe) == -1)
1576 fatal("socketpair");
1578 proc->pid = start_child(proc->type, NULL, argv0, confpath,
1579 proc->pipe[1], daemonize, verbosity);
1580 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1581 proc->iev.handler = gotd_dispatch_listener;
1582 proc->iev.events = EV_READ;
1583 proc->iev.handler_arg = NULL;
1585 gotd.listen_proc = proc;
1588 static const struct got_error *
1589 start_session_child(struct gotd_client *client, struct gotd_repo *repo,
1590 char *argv0, const char *confpath, int daemonize, int verbosity)
1592 struct gotd_child_proc *proc;
1594 proc = calloc(1, sizeof(*proc));
1596 return got_error_from_errno("calloc");
1598 TAILQ_INSERT_HEAD(&procs, proc, entry);
1599 evtimer_set(&proc->tmo, kill_proc_timeout, proc);
1601 if (client_is_reading(client))
1602 proc->type = PROC_SESSION_READ;
1604 proc->type = PROC_SESSION_WRITE;
1605 if (strlcpy(proc->repo_name, repo->name,
1606 sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
1607 fatalx("repository name too long: %s", repo->name);
1608 log_debug("starting client uid %d session for repository %s",
1609 client->euid, repo->name);
1610 if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >=
1611 sizeof(proc->repo_path))
1612 fatalx("repository path too long: %s", repo->path);
1613 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1614 PF_UNSPEC, proc->pipe) == -1)
1615 fatal("socketpair");
1616 proc->pid = start_child(proc->type, proc->repo_path, argv0,
1617 confpath, proc->pipe[1], daemonize, verbosity);
1618 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1619 log_debug("proc %s %s is on fd %d",
1620 gotd_proc_names[proc->type], proc->repo_path,
1622 proc->iev.handler = gotd_dispatch_client_session;
1623 proc->iev.events = EV_READ;
1624 proc->iev.handler_arg = NULL;
1625 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1626 gotd_dispatch_client_session, &proc->iev);
1627 gotd_imsg_event_add(&proc->iev);
1629 client->session = proc;
1633 static const struct got_error *
1634 start_repo_child(struct gotd_client *client, enum gotd_procid proc_type,
1635 struct gotd_repo *repo, char *argv0, const char *confpath,
1636 int daemonize, int verbosity)
1638 struct gotd_child_proc *proc;
1640 if (proc_type != PROC_REPO_READ && proc_type != PROC_REPO_WRITE)
1641 return got_error_msg(GOT_ERR_NOT_IMPL, "bad process type");
1643 proc = calloc(1, sizeof(*proc));
1645 return got_error_from_errno("calloc");
1647 TAILQ_INSERT_HEAD(&procs, proc, entry);
1648 evtimer_set(&proc->tmo, kill_proc_timeout, proc);
1650 proc->type = proc_type;
1651 if (strlcpy(proc->repo_name, repo->name,
1652 sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
1653 fatalx("repository name too long: %s", repo->name);
1654 log_debug("starting %s for repository %s",
1655 proc->type == PROC_REPO_READ ? "reader" : "writer", repo->name);
1656 if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >=
1657 sizeof(proc->repo_path))
1658 fatalx("repository path too long: %s", repo->path);
1659 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1660 PF_UNSPEC, proc->pipe) == -1)
1661 fatal("socketpair");
1662 proc->pid = start_child(proc->type, proc->repo_path, argv0,
1663 confpath, proc->pipe[1], daemonize, verbosity);
1664 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1665 log_debug("proc %s %s is on fd %d",
1666 gotd_proc_names[proc->type], proc->repo_path,
1668 proc->iev.handler = gotd_dispatch_repo_child;
1669 proc->iev.events = EV_READ;
1670 proc->iev.handler_arg = NULL;
1671 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1672 gotd_dispatch_repo_child, &proc->iev);
1673 gotd_imsg_event_add(&proc->iev);
1675 client->repo = proc;
1679 static const struct got_error *
1680 start_auth_child(struct gotd_client *client, int required_auth,
1681 struct gotd_repo *repo, char *argv0, const char *confpath,
1682 int daemonize, int verbosity)
1684 const struct got_error *err = NULL;
1685 struct gotd_child_proc *proc;
1686 struct gotd_imsg_auth iauth;
1689 memset(&iauth, 0, sizeof(iauth));
1691 fd = dup(client->fd);
1693 return got_error_from_errno("dup");
1695 proc = calloc(1, sizeof(*proc));
1697 err = got_error_from_errno("calloc");
1702 TAILQ_INSERT_HEAD(&procs, proc, entry);
1703 evtimer_set(&proc->tmo, kill_proc_timeout, proc);
1705 proc->type = PROC_AUTH;
1706 if (strlcpy(proc->repo_name, repo->name,
1707 sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
1708 fatalx("repository name too long: %s", repo->name);
1709 log_debug("starting auth for uid %d repository %s",
1710 client->euid, repo->name);
1711 if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >=
1712 sizeof(proc->repo_path))
1713 fatalx("repository path too long: %s", repo->path);
1714 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1715 PF_UNSPEC, proc->pipe) == -1)
1716 fatal("socketpair");
1717 proc->pid = start_child(proc->type, proc->repo_path, argv0,
1718 confpath, proc->pipe[1], daemonize, verbosity);
1719 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1720 log_debug("proc %s %s is on fd %d",
1721 gotd_proc_names[proc->type], proc->repo_path,
1723 proc->iev.handler = gotd_dispatch_auth_child;
1724 proc->iev.events = EV_READ;
1725 proc->iev.handler_arg = NULL;
1726 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1727 gotd_dispatch_auth_child, &proc->iev);
1728 gotd_imsg_event_add(&proc->iev);
1730 iauth.euid = client->euid;
1731 iauth.egid = client->egid;
1732 iauth.required_auth = required_auth;
1733 iauth.client_id = client->id;
1734 if (gotd_imsg_compose_event(&proc->iev, GOTD_IMSG_AUTHENTICATE,
1735 PROC_GOTD, fd, &iauth, sizeof(iauth)) == -1) {
1736 log_warn("imsg compose AUTHENTICATE");
1738 /* Let the auth_timeout handler tidy up. */
1741 client->auth = proc;
1742 client->required_auth = required_auth;
1747 apply_unveil_repo_readonly(const char *repo_path, int need_tmpdir)
1750 if (unveil(GOT_TMPDIR_STR, "rwc") == -1)
1751 fatal("unveil %s", GOT_TMPDIR_STR);
1754 if (unveil(repo_path, "r") == -1)
1755 fatal("unveil %s", repo_path);
1757 if (unveil(NULL, NULL) == -1)
1762 apply_unveil_repo_readwrite(const char *repo_path)
1764 if (unveil(repo_path, "rwc") == -1)
1765 fatal("unveil %s", repo_path);
1767 if (unveil(GOT_TMPDIR_STR, "rwc") == -1)
1768 fatal("unveil %s", GOT_TMPDIR_STR);
1770 if (unveil(NULL, NULL) == -1)
1775 apply_unveil_none(void)
1777 if (unveil("/", "") == -1)
1780 if (unveil(NULL, NULL) == -1)
1785 apply_unveil_selfexec(void)
1787 if (unveil(gotd.argv0, "x") == -1)
1788 fatal("unveil %s", gotd.argv0);
1790 if (unveil(NULL, NULL) == -1)
1795 set_max_datasize(void)
1799 if (getrlimit(RLIMIT_DATA, &rl) != 0)
1802 rl.rlim_cur = rl.rlim_max;
1803 setrlimit(RLIMIT_DATA, &rl);
1807 main(int argc, char **argv)
1809 const struct got_error *error = NULL;
1810 int ch, fd = -1, daemonize = 1, verbosity = 0, noaction = 0;
1811 const char *confpath = GOTD_CONF_PATH;
1812 char *argv0 = argv[0];
1814 struct passwd *pw = NULL;
1815 char *repo_path = NULL;
1816 enum gotd_procid proc_id = PROC_GOTD;
1817 struct event evsigint, evsigterm, evsighup, evsigusr1, evsigchld;
1818 int *pack_fds = NULL, *temp_fds = NULL;
1819 struct gotd_repo *repo = NULL;
1823 log_init(1, LOG_DAEMON); /* Log to stderr until daemonized. */
1825 while ((ch = getopt(argc, argv, "Adf:LnP:RsSvW")) != -1) {
1828 proc_id = PROC_AUTH;
1837 proc_id = PROC_LISTEN;
1843 repo_path = realpath(optarg, NULL);
1844 if (repo_path == NULL)
1845 fatal("realpath '%s'", optarg);
1848 proc_id = PROC_REPO_READ;
1851 proc_id = PROC_SESSION_READ;
1854 proc_id = PROC_SESSION_WRITE;
1861 proc_id = PROC_REPO_WRITE;
1874 if (geteuid() && (proc_id == PROC_GOTD || proc_id == PROC_LISTEN))
1875 fatalx("need root privileges");
1877 if (parse_config(confpath, proc_id, &gotd) != 0)
1880 pw = getpwnam(gotd.user_name);
1882 fatalx("user %s not found", gotd.user_name);
1884 if (pw->pw_uid == 0)
1885 fatalx("cannot run %s as the superuser", getprogname());
1888 fprintf(stderr, "configuration OK\n");
1893 gotd.daemonize = daemonize;
1894 gotd.verbosity = verbosity;
1895 gotd.confpath = confpath;
1897 /* Require an absolute path in argv[0] for reliable re-exec. */
1898 if (!got_path_is_absolute(argv0))
1899 fatalx("bad path \"%s\": must be an absolute path", argv0);
1901 log_init(daemonize ? 0 : 1, LOG_DAEMON);
1902 log_setverbose(verbosity);
1904 if (proc_id == PROC_GOTD) {
1905 snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
1906 arc4random_buf(&clients_hash_key, sizeof(clients_hash_key));
1907 if (daemonize && daemon(1, 0) == -1)
1909 gotd.pid = getpid();
1910 start_listener(argv0, confpath, daemonize, verbosity);
1911 } else if (proc_id == PROC_LISTEN) {
1912 snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
1914 log_info("socket: %s", gotd.unix_socket_path);
1915 log_info("user: %s", pw->pw_name);
1918 fd = unix_socket_listen(gotd.unix_socket_path, pw->pw_uid,
1921 fatal("cannot listen on unix socket %s",
1922 gotd.unix_socket_path);
1924 } else if (proc_id == PROC_AUTH) {
1925 snprintf(title, sizeof(title), "%s %s",
1926 gotd_proc_names[proc_id], repo_path);
1927 } else if (proc_id == PROC_REPO_READ || proc_id == PROC_REPO_WRITE ||
1928 proc_id == PROC_SESSION_READ || proc_id == PROC_SESSION_WRITE) {
1929 error = got_repo_pack_fds_open(&pack_fds);
1931 fatalx("cannot open pack tempfiles: %s", error->msg);
1932 error = got_repo_temp_fds_open(&temp_fds);
1934 fatalx("cannot open pack tempfiles: %s", error->msg);
1935 if (repo_path == NULL)
1936 fatalx("repository path not specified");
1937 snprintf(title, sizeof(title), "%s %s",
1938 gotd_proc_names[proc_id], repo_path);
1940 fatal("invalid process id %d", proc_id);
1942 setproctitle("%s", title);
1943 log_procinit(title);
1945 /* Drop root privileges. */
1946 if (setgid(pw->pw_gid) == -1)
1947 fatal("setgid %d failed", pw->pw_gid);
1948 if (setuid(pw->pw_uid) == -1)
1949 fatal("setuid %d failed", pw->pw_uid);
1956 /* "exec" promise will be limited to argv[0] via unveil(2). */
1957 if (pledge("stdio proc exec sendfd recvfd unveil", NULL) == -1)
1963 if (pledge("stdio sendfd unix unveil", NULL) == -1)
1967 * Ensure that AF_UNIX bind(2) cannot be used with any other
1968 * sockets by revoking all filesystem access via unveil(2).
1970 apply_unveil_none();
1972 listen_main(title, fd, gotd.connection_limits,
1973 gotd.nconnection_limits);
1978 if (pledge("stdio getpw recvfd unix unveil", NULL) == -1)
1982 * We need the "unix" pledge promise for getpeername(2) only.
1983 * Ensure that AF_UNIX bind(2) cannot be used by revoking all
1984 * filesystem access via unveil(2). Access to password database
1985 * files will still work since "getpw" bypasses unveil(2).
1987 apply_unveil_none();
1989 auth_main(title, &gotd.repos, repo_path);
1992 case PROC_SESSION_READ:
1993 case PROC_SESSION_WRITE:
1996 * The "recvfd" promise is only needed during setup and
1997 * will be removed in a later pledge(2) call.
1999 if (pledge("stdio rpath wpath cpath recvfd sendfd fattr flock "
2000 "unveil", NULL) == -1)
2003 if (proc_id == PROC_SESSION_READ)
2004 apply_unveil_repo_readonly(repo_path, 1);
2006 apply_unveil_repo_readwrite(repo_path);
2007 session_main(title, repo_path, pack_fds, temp_fds,
2008 &gotd.request_timeout, proc_id);
2011 case PROC_REPO_READ:
2014 if (pledge("stdio rpath recvfd unveil", NULL) == -1)
2017 apply_unveil_repo_readonly(repo_path, 0);
2018 repo_read_main(title, repo_path, pack_fds, temp_fds);
2021 case PROC_REPO_WRITE:
2024 if (pledge("stdio rpath recvfd unveil", NULL) == -1)
2027 apply_unveil_repo_readonly(repo_path, 0);
2028 repo = gotd_find_repo_by_path(repo_path, &gotd);
2030 fatalx("no repository for path %s", repo_path);
2031 repo_write_main(title, repo_path, pack_fds, temp_fds,
2032 &repo->protected_tag_namespaces,
2033 &repo->protected_branch_namespaces,
2034 &repo->protected_branches);
2038 fatal("invalid process id %d", proc_id);
2041 if (proc_id != PROC_GOTD)
2042 fatal("invalid process id %d", proc_id);
2044 evtimer_set(&gotd.listen_proc->tmo, kill_proc_timeout,
2047 apply_unveil_selfexec();
2049 signal_set(&evsigint, SIGINT, gotd_sighdlr, NULL);
2050 signal_set(&evsigterm, SIGTERM, gotd_sighdlr, NULL);
2051 signal_set(&evsighup, SIGHUP, gotd_sighdlr, NULL);
2052 signal_set(&evsigusr1, SIGUSR1, gotd_sighdlr, NULL);
2053 signal_set(&evsigchld, SIGCHLD, gotd_sighdlr, NULL);
2054 signal(SIGPIPE, SIG_IGN);
2056 signal_add(&evsigint, NULL);
2057 signal_add(&evsigterm, NULL);
2058 signal_add(&evsighup, NULL);
2059 signal_add(&evsigusr1, NULL);
2060 signal_add(&evsigchld, NULL);
2062 gotd_imsg_event_add(&gotd.listen_proc->iev);