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 *);
119 static void drop_privs(struct passwd *);
124 fprintf(stderr, "usage: %s [-dnv] [-f config-file]\n", getprogname());
129 unix_socket_listen(const char *unix_socket_path, uid_t uid, gid_t gid)
131 struct sockaddr_un sun;
133 mode_t old_umask, mode;
134 int sock_flags = SOCK_STREAM | SOCK_NONBLOCK;
137 sock_flags |= SOCK_CLOEXEC;
140 fd = socket(AF_UNIX, sock_flags, 0);
146 sun.sun_family = AF_UNIX;
147 if (strlcpy(sun.sun_path, unix_socket_path,
148 sizeof(sun.sun_path)) >= sizeof(sun.sun_path)) {
149 log_warnx("%s: name too long", unix_socket_path);
154 if (unlink(unix_socket_path) == -1) {
155 if (errno != ENOENT) {
156 log_warn("unlink %s", unix_socket_path);
162 old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
163 mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
165 if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
166 log_warn("bind: %s", unix_socket_path);
174 if (chmod(unix_socket_path, mode) == -1) {
175 log_warn("chmod %o %s", mode, unix_socket_path);
177 unlink(unix_socket_path);
181 if (chown(unix_socket_path, uid, gid) == -1) {
182 log_warn("chown %s uid=%d gid=%d", unix_socket_path, uid, gid);
184 unlink(unix_socket_path);
188 if (listen(fd, GOTD_UNIX_SOCKET_BACKLOG) == -1) {
191 unlink(unix_socket_path);
199 client_hash(uint32_t client_id)
201 return SipHash24(&clients_hash_key, &client_id, sizeof(client_id));
205 add_client(struct gotd_client *client)
207 uint64_t slot = client_hash(client->id) % nitems(gotd_clients);
208 STAILQ_INSERT_HEAD(&gotd_clients[slot], client, entry);
212 static struct gotd_client *
213 find_client(uint32_t client_id)
216 struct gotd_client *c;
218 slot = client_hash(client_id) % nitems(gotd_clients);
219 STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
220 if (c->id == client_id)
227 static struct gotd_client *
228 find_client_by_proc_fd(int fd)
232 for (slot = 0; slot < nitems(gotd_clients); slot++) {
233 struct gotd_client *c;
235 STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
236 if (c->repo && c->repo->iev.ibuf.fd == fd)
238 if (c->auth && c->auth->iev.ibuf.fd == fd)
240 if (c->session && c->session->iev.ibuf.fd == fd)
249 client_is_reading(struct gotd_client *client)
251 return (client->required_auth &
252 (GOTD_AUTH_READ | GOTD_AUTH_WRITE)) == GOTD_AUTH_READ;
256 client_is_writing(struct gotd_client *client)
258 return (client->required_auth &
259 (GOTD_AUTH_READ | GOTD_AUTH_WRITE)) ==
260 (GOTD_AUTH_READ | GOTD_AUTH_WRITE);
263 static const struct got_error *
264 ensure_client_is_not_writing(struct gotd_client *client)
266 if (client_is_writing(client)) {
267 return got_error_fmt(GOT_ERR_BAD_PACKET,
268 "uid %d made a read-request but is writing to "
269 "a repository", client->euid);
275 static const struct got_error *
276 ensure_client_is_not_reading(struct gotd_client *client)
278 if (client_is_reading(client)) {
279 return got_error_fmt(GOT_ERR_BAD_PACKET,
280 "uid %d made a write-request but is reading from "
281 "a repository", client->euid);
288 proc_done(struct gotd_child_proc *proc)
290 struct gotd_client *client;
292 TAILQ_REMOVE(&procs, proc, entry);
294 client = find_client_by_proc_fd(proc->iev.ibuf.fd);
295 if (client != NULL) {
296 if (proc == client->repo)
298 if (proc == client->auth)
300 if (proc == client->session)
301 client->session = NULL;
305 evtimer_del(&proc->tmo);
307 if (proc->iev.ibuf.fd != -1) {
308 event_del(&proc->iev.ev);
309 msgbuf_clear(&proc->iev.ibuf.w);
310 close(proc->iev.ibuf.fd);
317 kill_repo_proc(struct gotd_client *client)
319 if (client->repo == NULL)
322 kill_proc(client->repo, 0);
327 kill_auth_proc(struct gotd_client *client)
329 if (client->auth == NULL)
332 kill_proc(client->auth, 0);
337 kill_session_proc(struct gotd_client *client)
339 if (client->session == NULL)
342 kill_proc(client->session, 0);
343 client->session = NULL;
347 disconnect(struct gotd_client *client)
349 struct gotd_imsg_disconnect idisconnect;
350 struct gotd_child_proc *listen_proc = gotd.listen_proc;
353 log_debug("uid %d: disconnecting", client->euid);
355 kill_auth_proc(client);
356 kill_session_proc(client);
357 kill_repo_proc(client);
359 idisconnect.client_id = client->id;
360 if (gotd_imsg_compose_event(&listen_proc->iev,
361 GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
362 &idisconnect, sizeof(idisconnect)) == -1)
363 log_warn("imsg compose DISCONNECT");
365 slot = client_hash(client->id) % nitems(gotd_clients);
366 STAILQ_REMOVE(&gotd_clients[slot], client, gotd_client, entry);
367 imsg_clear(&client->iev.ibuf);
368 event_del(&client->iev.ev);
369 evtimer_del(&client->tmo);
370 if (client->fd != -1)
372 else if (client->iev.ibuf.fd != -1)
373 close(client->iev.ibuf.fd);
379 disconnect_on_error(struct gotd_client *client, const struct got_error *err)
383 if (err->code != GOT_ERR_EOF) {
384 log_warnx("uid %d: %s", client->euid, err->msg);
385 if (client->fd != -1) {
386 imsg_init(&ibuf, client->fd);
387 gotd_imsg_send_error(&ibuf, 0, PROC_GOTD, err);
394 static const struct got_error *
395 send_repo_info(struct gotd_imsgev *iev, struct gotd_repo *repo)
397 const struct got_error *err = NULL;
398 struct gotd_imsg_info_repo irepo;
400 memset(&irepo, 0, sizeof(irepo));
402 if (strlcpy(irepo.repo_name, repo->name, sizeof(irepo.repo_name))
403 >= sizeof(irepo.repo_name))
404 return got_error_msg(GOT_ERR_NO_SPACE, "repo name too long");
405 if (strlcpy(irepo.repo_path, repo->path, sizeof(irepo.repo_path))
406 >= sizeof(irepo.repo_path))
407 return got_error_msg(GOT_ERR_NO_SPACE, "repo path too long");
409 if (gotd_imsg_compose_event(iev, GOTD_IMSG_INFO_REPO, PROC_GOTD, -1,
410 &irepo, sizeof(irepo)) == -1) {
411 err = got_error_from_errno("imsg compose INFO_REPO");
419 static const struct got_error *
420 send_client_info(struct gotd_imsgev *iev, struct gotd_client *client)
422 const struct got_error *err = NULL;
423 struct gotd_imsg_info_client iclient;
424 struct gotd_child_proc *proc;
426 memset(&iclient, 0, sizeof(iclient));
427 iclient.euid = client->euid;
428 iclient.egid = client->egid;
432 if (strlcpy(iclient.repo_name, proc->repo_path,
433 sizeof(iclient.repo_name)) >= sizeof(iclient.repo_name)) {
434 return got_error_msg(GOT_ERR_NO_SPACE,
435 "repo name too long");
437 if (client_is_writing(client))
438 iclient.is_writing = 1;
440 iclient.repo_child_pid = proc->pid;
444 iclient.session_child_pid = client->session->pid;
446 if (gotd_imsg_compose_event(iev, GOTD_IMSG_INFO_CLIENT, PROC_GOTD, -1,
447 &iclient, sizeof(iclient)) == -1) {
448 err = got_error_from_errno("imsg compose INFO_CLIENT");
456 static const struct got_error *
457 send_info(struct gotd_client *client)
459 const struct got_error *err = NULL;
460 struct gotd_imsg_info info;
462 struct gotd_repo *repo;
464 if (client->euid != 0)
465 return got_error_set_errno(EPERM, "info");
468 info.verbosity = gotd.verbosity;
469 info.nrepos = gotd.nrepos;
470 info.nclients = client_cnt - 1;
472 if (gotd_imsg_compose_event(&client->iev, GOTD_IMSG_INFO, PROC_GOTD, -1,
473 &info, sizeof(info)) == -1) {
474 err = got_error_from_errno("imsg compose INFO");
479 TAILQ_FOREACH(repo, &gotd.repos, entry) {
480 err = send_repo_info(&client->iev, repo);
485 for (slot = 0; slot < nitems(gotd_clients); slot++) {
486 struct gotd_client *c;
487 STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
488 if (c->id == client->id)
490 err = send_client_info(&client->iev, c);
499 static const struct got_error *
500 stop_gotd(struct gotd_client *client)
503 if (client->euid != 0)
504 return got_error_set_errno(EPERM, "stop");
511 static const struct got_error *
512 start_client_authentication(struct gotd_client *client, struct imsg *imsg)
514 const struct got_error *err;
515 struct gotd_imsg_list_refs ireq;
516 struct gotd_repo *repo = NULL;
519 log_debug("list-refs request from uid %d", client->euid);
521 if (client->state != GOTD_CLIENT_STATE_NEW)
522 return got_error_msg(GOT_ERR_BAD_REQUEST,
523 "unexpected list-refs request received");
525 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
526 if (datalen != sizeof(ireq))
527 return got_error(GOT_ERR_PRIVSEP_LEN);
529 memcpy(&ireq, imsg->data, datalen);
531 if (ireq.client_is_reading) {
532 err = ensure_client_is_not_writing(client);
535 repo = gotd_find_repo_by_name(ireq.repo_name, &gotd);
537 return got_error(GOT_ERR_NOT_GIT_REPO);
538 err = start_auth_child(client, GOTD_AUTH_READ, repo,
539 gotd.argv0, gotd.confpath, gotd.daemonize,
544 err = ensure_client_is_not_reading(client);
547 repo = gotd_find_repo_by_name(ireq.repo_name, &gotd);
549 return got_error(GOT_ERR_NOT_GIT_REPO);
550 err = start_auth_child(client,
551 GOTD_AUTH_READ | GOTD_AUTH_WRITE,
552 repo, gotd.argv0, gotd.confpath, gotd.daemonize,
558 evtimer_add(&client->tmo, &auth_timeout);
560 /* Flow continues upon authentication success/failure or timeout. */
565 gotd_request(int fd, short events, void *arg)
567 struct gotd_imsgev *iev = arg;
568 struct imsgbuf *ibuf = &iev->ibuf;
569 struct gotd_client *client = iev->handler_arg;
570 const struct got_error *err = NULL;
574 if (events & EV_WRITE) {
575 while (ibuf->w.queued) {
576 n = msgbuf_write(&ibuf->w);
577 if (n == -1 && errno == EPIPE) {
579 * The client has closed its socket.
580 * This can happen when Git clients are
581 * done sending pack file data.
583 msgbuf_clear(&ibuf->w);
585 } else if (n == -1 && errno != EAGAIN) {
586 err = got_error_from_errno("imsg_flush");
587 disconnect_on_error(client, err);
591 /* Connection closed. */
592 err = got_error(GOT_ERR_EOF);
593 disconnect_on_error(client, err);
598 /* Disconnect gotctl(8) now that messages have been sent. */
599 if (!client_is_reading(client) && !client_is_writing(client)) {
605 if ((events & EV_READ) == 0)
608 memset(&imsg, 0, sizeof(imsg));
610 while (err == NULL) {
611 err = gotd_imsg_recv(&imsg, ibuf, 0);
613 if (err->code == GOT_ERR_PRIVSEP_READ)
618 evtimer_del(&client->tmo);
620 switch (imsg.hdr.type) {
622 err = send_info(client);
625 err = stop_gotd(client);
627 case GOTD_IMSG_LIST_REFS:
628 err = start_client_authentication(client, &imsg);
631 log_debug("unexpected imsg %d", imsg.hdr.type);
632 err = got_error(GOT_ERR_PRIVSEP_MSG);
640 disconnect_on_error(client, err);
642 gotd_imsg_event_add(&client->iev);
647 gotd_auth_timeout(int fd, short events, void *arg)
649 struct gotd_client *client = arg;
651 log_debug("disconnecting uid %d due to authentication timeout",
656 static const struct got_error *
657 recv_connect(uint32_t *client_id, struct imsg *imsg)
659 const struct got_error *err = NULL;
660 struct gotd_imsg_connect iconnect;
663 struct gotd_client *client = NULL;
667 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
668 if (datalen != sizeof(iconnect))
669 return got_error(GOT_ERR_PRIVSEP_LEN);
670 memcpy(&iconnect, imsg->data, sizeof(iconnect));
674 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
678 if (find_client(iconnect.client_id)) {
679 err = got_error_msg(GOT_ERR_CLIENT_ID, "duplicate client ID");
683 client = calloc(1, sizeof(*client));
684 if (client == NULL) {
685 err = got_error_from_errno("calloc");
689 *client_id = iconnect.client_id;
691 client->state = GOTD_CLIENT_STATE_NEW;
692 client->id = iconnect.client_id;
695 /* The auth process will verify UID/GID for us. */
696 client->euid = iconnect.euid;
697 client->egid = iconnect.egid;
699 imsg_init(&client->iev.ibuf, client->fd);
700 client->iev.handler = gotd_request;
701 client->iev.events = EV_READ;
702 client->iev.handler_arg = client;
704 event_set(&client->iev.ev, client->fd, EV_READ, gotd_request,
706 gotd_imsg_event_add(&client->iev);
708 evtimer_set(&client->tmo, gotd_auth_timeout, client);
711 log_debug("%s: new client uid %d connected on fd %d", __func__,
712 client->euid, client->fd);
715 struct gotd_child_proc *listen_proc = gotd.listen_proc;
716 struct gotd_imsg_disconnect idisconnect;
718 idisconnect.client_id = client->id;
719 if (gotd_imsg_compose_event(&listen_proc->iev,
720 GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
721 &idisconnect, sizeof(idisconnect)) == -1)
722 log_warn("imsg compose DISCONNECT");
731 static const char *gotd_proc_names[PROC_MAX] = {
743 kill_proc(struct gotd_child_proc *proc, int fatal)
745 struct timeval tv = { 5, 0 };
747 log_debug("kill -%d %d", fatal ? SIGKILL : SIGTERM, proc->pid);
749 if (proc->iev.ibuf.fd != -1) {
750 event_del(&proc->iev.ev);
751 msgbuf_clear(&proc->iev.ibuf.w);
752 close(proc->iev.ibuf.fd);
753 proc->iev.ibuf.fd = -1;
756 if (!evtimer_pending(&proc->tmo, NULL) && !fatal)
757 evtimer_add(&proc->tmo, &tv);
760 log_warnx("sending SIGKILL to PID %d", proc->pid);
761 kill(proc->pid, SIGKILL);
763 kill(proc->pid, SIGTERM);
767 kill_proc_timeout(int fd, short ev, void *d)
769 struct gotd_child_proc *proc = d;
771 log_warnx("timeout waiting for PID %d to terminate;"
772 " retrying with force", proc->pid);
781 log_debug("shutting down");
782 for (slot = 0; slot < nitems(gotd_clients); slot++) {
783 struct gotd_client *c, *tmp;
785 STAILQ_FOREACH_SAFE(c, &gotd_clients[slot], entry, tmp)
789 kill_proc(gotd.listen_proc, 0);
791 log_info("terminating");
795 static struct gotd_child_proc *
796 find_proc_by_pid(pid_t pid)
798 struct gotd_child_proc *proc = NULL;
800 TAILQ_FOREACH(proc, &procs, entry)
801 if (proc->pid == pid)
808 gotd_sighdlr(int sig, short event, void *arg)
810 struct gotd_child_proc *proc;
815 * Normal signal handler rules don't apply because libevent
821 log_info("%s: ignoring SIGHUP", __func__);
824 log_info("%s: ignoring SIGUSR1", __func__);
832 pid = waitpid(WAIT_ANY, &status, WNOHANG);
843 log_debug("reaped pid %d", pid);
844 proc = find_proc_by_pid(pid);
846 log_info("caught exit of unknown child %d",
851 if (WIFSIGNALED(status)) {
852 log_warnx("child PID %d terminated with"
853 " signal %d", pid, WTERMSIG(status));
860 fatalx("unexpected signal");
864 static const struct got_error *
865 ensure_proc_is_reading(struct gotd_client *client,
866 struct gotd_child_proc *proc)
868 if (!client_is_reading(client)) {
870 return got_error_fmt(GOT_ERR_BAD_PACKET,
871 "PID %d handled a read-request for uid %d but this "
872 "user is not reading from a repository", proc->pid,
879 static const struct got_error *
880 ensure_proc_is_writing(struct gotd_client *client,
881 struct gotd_child_proc *proc)
883 if (!client_is_writing(client)) {
885 return got_error_fmt(GOT_ERR_BAD_PACKET,
886 "PID %d handled a write-request for uid %d but this "
887 "user is not writing to a repository", proc->pid,
895 verify_imsg_src(struct gotd_client *client, struct gotd_child_proc *proc,
898 const struct got_error *err;
901 if (proc->type == PROC_REPO_READ || proc->type == PROC_REPO_WRITE) {
902 if (client->repo == NULL)
903 fatalx("no process found for uid %d", client->euid);
904 if (proc->pid != client->repo->pid) {
906 log_warnx("received message from PID %d for uid %d, "
907 "while PID %d is the process serving this user",
908 proc->pid, client->euid, client->repo->pid);
912 if (proc->type == PROC_SESSION_READ ||
913 proc->type == PROC_SESSION_WRITE) {
914 if (client->session == NULL) {
915 log_warnx("no session found for uid %d", client->euid);
918 if (proc->pid != client->session->pid) {
920 log_warnx("received message from PID %d for uid %d, "
921 "while PID %d is the process serving this user",
922 proc->pid, client->euid, client->session->pid);
927 switch (imsg->hdr.type) {
928 case GOTD_IMSG_ERROR:
931 case GOTD_IMSG_CONNECT:
932 if (proc->type != PROC_LISTEN) {
933 err = got_error_fmt(GOT_ERR_BAD_PACKET,
934 "new connection for uid %d from PID %d "
935 "which is not the listen process",
936 proc->pid, client->euid);
940 case GOTD_IMSG_ACCESS_GRANTED:
941 if (proc->type != PROC_AUTH) {
942 err = got_error_fmt(GOT_ERR_BAD_PACKET,
943 "authentication of uid %d from PID %d "
944 "which is not the auth process",
945 proc->pid, client->euid);
949 case GOTD_IMSG_CLIENT_SESSION_READY:
950 if (proc->type != PROC_SESSION_READ &&
951 proc->type != PROC_SESSION_WRITE) {
952 err = got_error_fmt(GOT_ERR_BAD_PACKET,
953 "unexpected \"ready\" signal from PID %d",
958 case GOTD_IMSG_REPO_CHILD_READY:
959 if (proc->type != PROC_REPO_READ &&
960 proc->type != PROC_REPO_WRITE) {
961 err = got_error_fmt(GOT_ERR_BAD_PACKET,
962 "unexpected \"ready\" signal from PID %d",
967 case GOTD_IMSG_PACKFILE_DONE:
968 err = ensure_proc_is_reading(client, proc);
970 log_warnx("uid %d: %s", client->euid, err->msg);
974 case GOTD_IMSG_PACKFILE_INSTALL:
975 case GOTD_IMSG_REF_UPDATES_START:
976 case GOTD_IMSG_REF_UPDATE:
977 err = ensure_proc_is_writing(client, proc);
979 log_warnx("uid %d: %s", client->euid, err->msg);
984 log_debug("%s: unexpected imsg %d", __func__, imsg->hdr.type);
991 static const struct got_error *
992 connect_repo_child(struct gotd_client *client,
993 struct gotd_child_proc *repo_proc)
995 static const struct got_error *err;
996 struct gotd_imsgev *session_iev = &client->session->iev;
997 struct gotd_imsg_connect_repo_child ireq;
999 int sock_flags = SOCK_STREAM | SOCK_NONBLOCK;
1002 sock_flags |= SOCK_CLOEXEC;
1005 if (client->state != GOTD_CLIENT_STATE_ACCESS_GRANTED)
1006 return got_error_msg(GOT_ERR_BAD_REQUEST,
1007 "unexpected repo child ready signal received");
1009 if (socketpair(AF_UNIX, sock_flags, PF_UNSPEC, pipe) == -1)
1010 fatal("socketpair");
1012 memset(&ireq, 0, sizeof(ireq));
1013 ireq.client_id = client->id;
1014 ireq.proc_id = repo_proc->type;
1016 /* Pass repo child pipe to session child process. */
1017 if (gotd_imsg_compose_event(session_iev, GOTD_IMSG_CONNECT_REPO_CHILD,
1018 PROC_GOTD, pipe[0], &ireq, sizeof(ireq)) == -1) {
1019 err = got_error_from_errno("imsg compose CONNECT_REPO_CHILD");
1025 /* Pass session child pipe to repo child process. */
1026 if (gotd_imsg_compose_event(&repo_proc->iev,
1027 GOTD_IMSG_CONNECT_REPO_CHILD, PROC_GOTD, pipe[1], NULL, 0) == -1) {
1028 err = got_error_from_errno("imsg compose CONNECT_REPO_CHILD");
1037 gotd_dispatch_listener(int fd, short event, void *arg)
1039 struct gotd_imsgev *iev = arg;
1040 struct imsgbuf *ibuf = &iev->ibuf;
1041 struct gotd_child_proc *proc = gotd.listen_proc;
1046 if (proc->iev.ibuf.fd != fd)
1047 fatalx("%s: unexpected fd %d", __func__, fd);
1049 if (event & EV_READ) {
1050 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1051 fatal("imsg_read error");
1053 /* Connection closed. */
1059 if (event & EV_WRITE) {
1060 n = msgbuf_write(&ibuf->w);
1061 if (n == -1 && errno != EAGAIN)
1062 fatal("msgbuf_write");
1064 /* Connection closed. */
1071 const struct got_error *err = NULL;
1072 struct gotd_client *client = NULL;
1073 uint32_t client_id = 0;
1074 int do_disconnect = 0;
1076 if ((n = imsg_get(ibuf, &imsg)) == -1)
1077 fatal("%s: imsg_get error", __func__);
1078 if (n == 0) /* No more messages. */
1081 switch (imsg.hdr.type) {
1082 case GOTD_IMSG_ERROR:
1084 err = gotd_imsg_recv_error(&client_id, &imsg);
1086 case GOTD_IMSG_CONNECT:
1087 err = recv_connect(&client_id, &imsg);
1090 log_debug("unexpected imsg %d", imsg.hdr.type);
1094 client = find_client(client_id);
1095 if (client == NULL) {
1096 log_warnx("%s: client not found", __func__);
1102 log_warnx("uid %d: %s", client->euid, err->msg);
1104 if (do_disconnect) {
1106 disconnect_on_error(client, err);
1115 gotd_imsg_event_add(iev);
1117 /* This pipe is dead. Remove its event handler */
1118 event_del(&iev->ev);
1119 event_loopexit(NULL);
1124 gotd_dispatch_auth_child(int fd, short event, void *arg)
1126 const struct got_error *err = NULL;
1127 struct gotd_imsgev *iev = arg;
1128 struct imsgbuf *ibuf = &iev->ibuf;
1129 struct gotd_client *client;
1130 struct gotd_repo *repo = NULL;
1134 uint32_t client_id = 0;
1135 int do_disconnect = 0;
1137 client = find_client_by_proc_fd(fd);
1138 if (client == NULL) {
1139 /* Can happen during process teardown. */
1140 warnx("cannot find client for fd %d", fd);
1145 if (client->auth == NULL)
1146 fatalx("cannot find auth child process for fd %d", fd);
1148 if (event & EV_READ) {
1149 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1150 fatal("imsg_read error");
1152 /* Connection closed. */
1158 if (event & EV_WRITE) {
1159 n = msgbuf_write(&ibuf->w);
1160 if (n == -1 && errno != EAGAIN)
1161 fatal("msgbuf_write");
1163 /* Connection closed. */
1169 if (client->auth->iev.ibuf.fd != fd)
1170 fatalx("%s: unexpected fd %d", __func__, fd);
1172 if ((n = imsg_get(ibuf, &imsg)) == -1)
1173 fatal("%s: imsg_get error", __func__);
1174 if (n == 0) /* No more messages. */
1177 evtimer_del(&client->tmo);
1179 switch (imsg.hdr.type) {
1180 case GOTD_IMSG_ERROR:
1182 err = gotd_imsg_recv_error(&client_id, &imsg);
1184 case GOTD_IMSG_ACCESS_GRANTED:
1185 client->state = GOTD_CLIENT_STATE_ACCESS_GRANTED;
1189 log_debug("unexpected imsg %d", imsg.hdr.type);
1193 if (!verify_imsg_src(client, client->auth, &imsg)) {
1195 log_debug("dropping imsg type %d from PID %d",
1196 imsg.hdr.type, client->auth->pid);
1200 if (do_disconnect) {
1202 disconnect_on_error(client, err);
1208 repo = gotd_find_repo_by_name(client->auth->repo_name, &gotd);
1210 err = got_error(GOT_ERR_NOT_GIT_REPO);
1213 kill_auth_proc(client);
1215 log_info("authenticated uid %d for repository %s",
1216 client->euid, repo->name);
1218 err = start_session_child(client, repo, gotd.argv0,
1219 gotd.confpath, gotd.daemonize, gotd.verbosity);
1224 log_warnx("uid %d: %s", client->euid, err->msg);
1226 /* We might have killed the auth process by now. */
1227 if (client->auth != NULL) {
1229 gotd_imsg_event_add(iev);
1231 /* This pipe is dead. Remove its event handler */
1232 event_del(&iev->ev);
1237 static const struct got_error *
1238 connect_session(struct gotd_client *client)
1240 const struct got_error *err = NULL;
1241 struct gotd_imsg_connect iconnect;
1244 memset(&iconnect, 0, sizeof(iconnect));
1246 s = dup(client->fd);
1248 return got_error_from_errno("dup");
1250 iconnect.client_id = client->id;
1251 iconnect.euid = client->euid;
1252 iconnect.egid = client->egid;
1254 if (gotd_imsg_compose_event(&client->session->iev, GOTD_IMSG_CONNECT,
1255 PROC_GOTD, s, &iconnect, sizeof(iconnect)) == -1) {
1256 err = got_error_from_errno("imsg compose CONNECT");
1262 * We are no longer interested in messages from this client.
1263 * Further client requests will be handled by the session process.
1265 msgbuf_clear(&client->iev.ibuf.w);
1266 imsg_clear(&client->iev.ibuf);
1267 event_del(&client->iev.ev);
1268 client->fd = -1; /* will be closed via copy in client->iev.ibuf.fd */
1274 gotd_dispatch_client_session(int fd, short event, void *arg)
1276 struct gotd_imsgev *iev = arg;
1277 struct imsgbuf *ibuf = &iev->ibuf;
1278 struct gotd_child_proc *proc = NULL;
1279 struct gotd_client *client = NULL;
1284 client = find_client_by_proc_fd(fd);
1285 if (client == NULL) {
1286 /* Can happen during process teardown. */
1287 warnx("cannot find client for fd %d", fd);
1292 if (event & EV_READ) {
1293 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1294 fatal("imsg_read error");
1296 /* Connection closed. */
1302 if (event & EV_WRITE) {
1303 n = msgbuf_write(&ibuf->w);
1304 if (n == -1 && errno != EAGAIN)
1305 fatal("msgbuf_write");
1307 /* Connection closed. */
1313 proc = client->session;
1315 fatalx("cannot find session child process for fd %d", fd);
1318 const struct got_error *err = NULL;
1319 uint32_t client_id = 0;
1320 int do_disconnect = 0, do_start_repo_child = 0;
1322 if ((n = imsg_get(ibuf, &imsg)) == -1)
1323 fatal("%s: imsg_get error", __func__);
1324 if (n == 0) /* No more messages. */
1327 switch (imsg.hdr.type) {
1328 case GOTD_IMSG_ERROR:
1330 err = gotd_imsg_recv_error(&client_id, &imsg);
1332 case GOTD_IMSG_CLIENT_SESSION_READY:
1333 if (client->state != GOTD_CLIENT_STATE_ACCESS_GRANTED) {
1334 err = got_error(GOT_ERR_PRIVSEP_MSG);
1337 do_start_repo_child = 1;
1339 case GOTD_IMSG_DISCONNECT:
1343 log_debug("unexpected imsg %d", imsg.hdr.type);
1347 if (!verify_imsg_src(client, proc, &imsg)) {
1348 log_debug("dropping imsg type %d from PID %d",
1349 imsg.hdr.type, proc->pid);
1354 log_warnx("uid %d: %s", client->euid, err->msg);
1356 if (do_start_repo_child) {
1357 struct gotd_repo *repo;
1358 const char *name = client->session->repo_name;
1360 repo = gotd_find_repo_by_name(name, &gotd);
1362 enum gotd_procid proc_type;
1364 if (client->required_auth & GOTD_AUTH_WRITE)
1365 proc_type = PROC_REPO_WRITE;
1367 proc_type = PROC_REPO_READ;
1369 err = start_repo_child(client, proc_type, repo,
1370 gotd.argv0, gotd.confpath, gotd.daemonize,
1373 err = got_error(GOT_ERR_NOT_GIT_REPO);
1376 log_warnx("uid %d: %s", client->euid, err->msg);
1381 if (do_disconnect) {
1383 disconnect_on_error(client, err);
1392 gotd_imsg_event_add(iev);
1394 /* This pipe is dead. Remove its event handler */
1395 event_del(&iev->ev);
1401 gotd_dispatch_repo_child(int fd, short event, void *arg)
1403 struct gotd_imsgev *iev = arg;
1404 struct imsgbuf *ibuf = &iev->ibuf;
1405 struct gotd_child_proc *proc = NULL;
1406 struct gotd_client *client;
1411 client = find_client_by_proc_fd(fd);
1412 if (client == NULL) {
1413 /* Can happen during process teardown. */
1414 warnx("cannot find client for fd %d", fd);
1419 if (event & EV_READ) {
1420 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1421 fatal("imsg_read error");
1423 /* Connection closed. */
1429 if (event & EV_WRITE) {
1430 n = msgbuf_write(&ibuf->w);
1431 if (n == -1 && errno != EAGAIN)
1432 fatal("msgbuf_write");
1434 /* Connection closed. */
1440 proc = client->repo;
1442 fatalx("cannot find child process for fd %d", fd);
1445 const struct got_error *err = NULL;
1446 uint32_t client_id = 0;
1447 int do_disconnect = 0;
1449 if ((n = imsg_get(ibuf, &imsg)) == -1)
1450 fatal("%s: imsg_get error", __func__);
1451 if (n == 0) /* No more messages. */
1454 switch (imsg.hdr.type) {
1455 case GOTD_IMSG_ERROR:
1457 err = gotd_imsg_recv_error(&client_id, &imsg);
1459 case GOTD_IMSG_REPO_CHILD_READY:
1460 err = connect_session(client);
1463 err = connect_repo_child(client, proc);
1466 log_debug("unexpected imsg %d", imsg.hdr.type);
1470 if (!verify_imsg_src(client, proc, &imsg)) {
1471 log_debug("dropping imsg type %d from PID %d",
1472 imsg.hdr.type, proc->pid);
1477 log_warnx("uid %d: %s", client->euid, err->msg);
1479 if (do_disconnect) {
1481 disconnect_on_error(client, err);
1490 gotd_imsg_event_add(iev);
1492 /* This pipe is dead. Remove its event handler */
1493 event_del(&iev->ev);
1499 start_child(enum gotd_procid proc_id, const char *repo_path,
1500 char *argv0, const char *confpath, int fd, int daemonize, int verbosity)
1506 switch (pid = fork()) {
1508 fatal("cannot fork");
1516 if (fd != GOTD_FILENO_MSG_PIPE) {
1517 if (dup2(fd, GOTD_FILENO_MSG_PIPE) == -1)
1518 fatal("cannot setup imsg fd");
1519 } else if (fcntl(fd, F_SETFD, 0) == -1)
1520 fatal("cannot setup imsg fd");
1522 argv[argc++] = argv0;
1525 argv[argc++] = (char *)"-L";
1528 argv[argc++] = (char *)"-A";
1530 case PROC_SESSION_READ:
1531 argv[argc++] = (char *)"-s";
1533 case PROC_SESSION_WRITE:
1534 argv[argc++] = (char *)"-S";
1536 case PROC_REPO_READ:
1537 argv[argc++] = (char *)"-R";
1539 case PROC_REPO_WRITE:
1540 argv[argc++] = (char *)"-W";
1543 fatalx("invalid process id %d", proc_id);
1546 argv[argc++] = (char *)"-f";
1547 argv[argc++] = (char *)confpath;
1550 argv[argc++] = (char *)"-P";
1551 argv[argc++] = (char *)repo_path;
1555 argv[argc++] = (char *)"-d";
1557 argv[argc++] = (char *)"-v";
1559 argv[argc++] = (char *)"-v";
1560 argv[argc++] = NULL;
1562 execvp(argv0, argv);
1567 start_listener(char *argv0, const char *confpath, int daemonize, int verbosity)
1569 struct gotd_child_proc *proc;
1570 int sock_flags = SOCK_STREAM|SOCK_NONBLOCK;
1573 sock_flags |= SOCK_CLOEXEC;
1576 proc = calloc(1, sizeof(*proc));
1580 TAILQ_INSERT_HEAD(&procs, proc, entry);
1582 /* proc->tmo is initialized in main() after event_init() */
1584 proc->type = PROC_LISTEN;
1586 if (socketpair(AF_UNIX, sock_flags,
1587 PF_UNSPEC, proc->pipe) == -1)
1588 fatal("socketpair");
1590 proc->pid = start_child(proc->type, NULL, argv0, confpath,
1591 proc->pipe[1], daemonize, verbosity);
1592 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1593 proc->iev.handler = gotd_dispatch_listener;
1594 proc->iev.events = EV_READ;
1595 proc->iev.handler_arg = NULL;
1597 gotd.listen_proc = proc;
1600 static const struct got_error *
1601 start_session_child(struct gotd_client *client, struct gotd_repo *repo,
1602 char *argv0, const char *confpath, int daemonize, int verbosity)
1604 struct gotd_child_proc *proc;
1605 int sock_flags = SOCK_STREAM | SOCK_NONBLOCK;
1608 sock_flags |= SOCK_CLOEXEC;
1611 proc = calloc(1, sizeof(*proc));
1613 return got_error_from_errno("calloc");
1615 TAILQ_INSERT_HEAD(&procs, proc, entry);
1616 evtimer_set(&proc->tmo, kill_proc_timeout, proc);
1618 if (client_is_reading(client))
1619 proc->type = PROC_SESSION_READ;
1621 proc->type = PROC_SESSION_WRITE;
1622 if (strlcpy(proc->repo_name, repo->name,
1623 sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
1624 fatalx("repository name too long: %s", repo->name);
1625 log_debug("starting client uid %d session for repository %s",
1626 client->euid, repo->name);
1627 if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >=
1628 sizeof(proc->repo_path))
1629 fatalx("repository path too long: %s", repo->path);
1630 if (socketpair(AF_UNIX, sock_flags, PF_UNSPEC, proc->pipe) == -1)
1631 fatal("socketpair");
1632 proc->pid = start_child(proc->type, proc->repo_path, argv0,
1633 confpath, proc->pipe[1], daemonize, verbosity);
1634 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1635 log_debug("proc %s %s is on fd %d",
1636 gotd_proc_names[proc->type], proc->repo_path,
1638 proc->iev.handler = gotd_dispatch_client_session;
1639 proc->iev.events = EV_READ;
1640 proc->iev.handler_arg = NULL;
1641 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1642 gotd_dispatch_client_session, &proc->iev);
1643 gotd_imsg_event_add(&proc->iev);
1645 client->session = proc;
1649 static const struct got_error *
1650 start_repo_child(struct gotd_client *client, enum gotd_procid proc_type,
1651 struct gotd_repo *repo, char *argv0, const char *confpath,
1652 int daemonize, int verbosity)
1654 struct gotd_child_proc *proc;
1655 int sock_flags = SOCK_STREAM|SOCK_NONBLOCK;
1658 sock_flags |= SOCK_CLOEXEC;
1661 if (proc_type != PROC_REPO_READ && proc_type != PROC_REPO_WRITE)
1662 return got_error_msg(GOT_ERR_NOT_IMPL, "bad process type");
1664 proc = calloc(1, sizeof(*proc));
1666 return got_error_from_errno("calloc");
1668 TAILQ_INSERT_HEAD(&procs, proc, entry);
1669 evtimer_set(&proc->tmo, kill_proc_timeout, proc);
1671 proc->type = proc_type;
1672 if (strlcpy(proc->repo_name, repo->name,
1673 sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
1674 fatalx("repository name too long: %s", repo->name);
1675 log_debug("starting %s for repository %s",
1676 proc->type == PROC_REPO_READ ? "reader" : "writer", repo->name);
1678 if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >=
1679 sizeof(proc->repo_path))
1680 fatalx("repository path too long: %s", repo->path);
1681 if (realpath(repo->path, proc->repo_path) == NULL)
1682 fatal("%s", repo->path);
1683 if (socketpair(AF_UNIX, sock_flags,
1684 PF_UNSPEC, proc->pipe) == -1)
1685 fatal("socketpair");
1686 proc->pid = start_child(proc->type, proc->repo_path, argv0,
1687 confpath, proc->pipe[1], daemonize, verbosity);
1688 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1689 log_debug("proc %s %s is on fd %d",
1690 gotd_proc_names[proc->type], proc->repo_path,
1692 proc->iev.handler = gotd_dispatch_repo_child;
1693 proc->iev.events = EV_READ;
1694 proc->iev.handler_arg = NULL;
1695 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1696 gotd_dispatch_repo_child, &proc->iev);
1697 gotd_imsg_event_add(&proc->iev);
1699 client->repo = proc;
1703 static const struct got_error *
1704 start_auth_child(struct gotd_client *client, int required_auth,
1705 struct gotd_repo *repo, char *argv0, const char *confpath,
1706 int daemonize, int verbosity)
1708 const struct got_error *err = NULL;
1709 struct gotd_child_proc *proc;
1710 struct gotd_imsg_auth iauth;
1712 int sock_flags = SOCK_STREAM|SOCK_NONBLOCK;
1715 sock_flags |= SOCK_CLOEXEC;
1718 memset(&iauth, 0, sizeof(iauth));
1720 fd = dup(client->fd);
1722 return got_error_from_errno("dup");
1724 proc = calloc(1, sizeof(*proc));
1726 err = got_error_from_errno("calloc");
1731 TAILQ_INSERT_HEAD(&procs, proc, entry);
1732 evtimer_set(&proc->tmo, kill_proc_timeout, proc);
1734 proc->type = PROC_AUTH;
1735 if (strlcpy(proc->repo_name, repo->name,
1736 sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
1737 fatalx("repository name too long: %s", repo->name);
1738 log_debug("starting auth for uid %d repository %s",
1739 client->euid, repo->name);
1740 if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >=
1741 sizeof(proc->repo_path))
1742 fatalx("repository path too long: %s", repo->path);
1743 if (realpath(repo->path, proc->repo_path) == NULL)
1744 fatal("%s", repo->path);
1745 if (socketpair(AF_UNIX, sock_flags,
1746 PF_UNSPEC, proc->pipe) == -1)
1747 fatal("socketpair");
1748 proc->pid = start_child(proc->type, proc->repo_path, argv0,
1749 confpath, proc->pipe[1], daemonize, verbosity);
1750 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1751 log_debug("proc %s %s is on fd %d",
1752 gotd_proc_names[proc->type], proc->repo_path,
1754 proc->iev.handler = gotd_dispatch_auth_child;
1755 proc->iev.events = EV_READ;
1756 proc->iev.handler_arg = NULL;
1757 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1758 gotd_dispatch_auth_child, &proc->iev);
1759 gotd_imsg_event_add(&proc->iev);
1761 iauth.euid = client->euid;
1762 iauth.egid = client->egid;
1763 iauth.required_auth = required_auth;
1764 iauth.client_id = client->id;
1765 if (gotd_imsg_compose_event(&proc->iev, GOTD_IMSG_AUTHENTICATE,
1766 PROC_GOTD, fd, &iauth, sizeof(iauth)) == -1) {
1767 log_warn("imsg compose AUTHENTICATE");
1769 /* Let the auth_timeout handler tidy up. */
1772 client->auth = proc;
1773 client->required_auth = required_auth;
1778 apply_unveil_repo_readonly(const char *repo_path, int need_tmpdir)
1781 if (unveil(GOT_TMPDIR_STR, "rwc") == -1)
1782 fatal("unveil %s", GOT_TMPDIR_STR);
1785 if (unveil(repo_path, "r") == -1)
1786 fatal("unveil %s", repo_path);
1788 if (unveil(NULL, NULL) == -1)
1793 apply_unveil_repo_readwrite(const char *repo_path)
1795 if (unveil(repo_path, "rwc") == -1)
1796 fatal("unveil %s", repo_path);
1798 if (unveil(GOT_TMPDIR_STR, "rwc") == -1)
1799 fatal("unveil %s", GOT_TMPDIR_STR);
1801 if (unveil(NULL, NULL) == -1)
1806 apply_unveil_none(void)
1808 if (unveil("/", "") == -1)
1811 if (unveil(NULL, NULL) == -1)
1816 apply_unveil_selfexec(void)
1818 if (unveil(gotd.argv0, "x") == -1)
1819 fatal("unveil %s", gotd.argv0);
1821 if (unveil(NULL, NULL) == -1)
1826 drop_privs(struct passwd *pw)
1828 /* Drop root privileges. */
1829 if (setgid(pw->pw_gid) == -1)
1830 fatal("setgid %d failed", pw->pw_gid);
1831 if (setuid(pw->pw_uid) == -1)
1832 fatal("setuid %d failed", pw->pw_uid);
1836 main(int argc, char **argv)
1838 const struct got_error *error = NULL;
1839 int ch, fd = -1, daemonize = 1, verbosity = 0, noaction = 0;
1840 const char *confpath = GOTD_CONF_PATH;
1841 char *argv0 = argv[0];
1843 struct passwd *pw = NULL;
1844 char *repo_path = NULL;
1845 enum gotd_procid proc_id = PROC_GOTD;
1846 struct event evsigint, evsigterm, evsighup, evsigusr1, evsigchld;
1847 int *pack_fds = NULL, *temp_fds = NULL;
1848 struct gotd_repo *repo = NULL;
1852 log_init(1, LOG_DAEMON); /* Log to stderr until daemonized. */
1854 while ((ch = getopt(argc, argv, "Adf:LnP:RsSvW")) != -1) {
1857 proc_id = PROC_AUTH;
1866 proc_id = PROC_LISTEN;
1872 repo_path = realpath(optarg, NULL);
1873 if (repo_path == NULL)
1874 fatal("realpath '%s'", optarg);
1877 proc_id = PROC_REPO_READ;
1880 proc_id = PROC_SESSION_READ;
1883 proc_id = PROC_SESSION_WRITE;
1890 proc_id = PROC_REPO_WRITE;
1903 if (geteuid() && (proc_id == PROC_GOTD || proc_id == PROC_LISTEN))
1904 fatalx("need root privileges");
1906 if (parse_config(confpath, proc_id, &gotd) != 0)
1909 pw = getpwnam(gotd.user_name);
1911 fatalx("user %s not found", gotd.user_name);
1913 if (pw->pw_uid == 0)
1914 fatalx("cannot run %s as the superuser", getprogname());
1917 fprintf(stderr, "configuration OK\n");
1922 gotd.daemonize = daemonize;
1923 gotd.verbosity = verbosity;
1924 gotd.confpath = confpath;
1926 /* Require an absolute path in argv[0] for reliable re-exec. */
1927 if (!got_path_is_absolute(argv0))
1928 fatalx("bad path \"%s\": must be an absolute path", argv0);
1930 log_init(daemonize ? 0 : 1, LOG_DAEMON);
1931 log_setverbose(verbosity);
1933 if (proc_id == PROC_GOTD) {
1934 snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
1935 arc4random_buf(&clients_hash_key, sizeof(clients_hash_key));
1936 if (daemonize && daemon(1, 0) == -1)
1938 gotd.pid = getpid();
1939 start_listener(argv0, confpath, daemonize, verbosity);
1940 } else if (proc_id == PROC_LISTEN) {
1941 snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
1943 log_info("socket: %s", gotd.unix_socket_path);
1944 log_info("user: %s", pw->pw_name);
1947 fd = unix_socket_listen(gotd.unix_socket_path, pw->pw_uid,
1950 fatal("cannot listen on unix socket %s",
1951 gotd.unix_socket_path);
1953 } else if (proc_id == PROC_AUTH) {
1954 snprintf(title, sizeof(title), "%s %s",
1955 gotd_proc_names[proc_id], repo_path);
1956 } else if (proc_id == PROC_REPO_READ || proc_id == PROC_REPO_WRITE ||
1957 proc_id == PROC_SESSION_READ || proc_id == PROC_SESSION_WRITE) {
1958 error = got_repo_pack_fds_open(&pack_fds);
1960 fatalx("cannot open pack tempfiles: %s", error->msg);
1961 error = got_repo_temp_fds_open(&temp_fds);
1963 fatalx("cannot open pack tempfiles: %s", error->msg);
1964 if (repo_path == NULL)
1965 fatalx("repository path not specified");
1966 snprintf(title, sizeof(title), "%s %s",
1967 gotd_proc_names[proc_id], repo_path);
1969 fatal("invalid process id %d", proc_id);
1971 setproctitle("%s", title);
1972 log_procinit(title);
1974 if (proc_id != PROC_GOTD && proc_id != PROC_LISTEN &&
1975 proc_id != PROC_REPO_READ && proc_id != PROC_REPO_WRITE) {
1976 /* Drop root privileges. */
1977 if (setgid(pw->pw_gid) == -1)
1978 fatal("setgid %d failed", pw->pw_gid);
1979 if (setuid(pw->pw_uid) == -1)
1980 fatal("setuid %d failed", pw->pw_uid);
1988 /* "exec" promise will be limited to argv[0] via unveil(2). */
1989 if (pledge("stdio proc exec sendfd recvfd unveil", NULL) == -1)
1995 if (pledge("stdio sendfd unix unveil", NULL) == -1)
1999 * Ensure that AF_UNIX bind(2) cannot be used with any other
2000 * sockets by revoking all filesystem access via unveil(2).
2002 apply_unveil_none();
2004 enter_chroot(GOTD_EMPTY_PATH);
2007 listen_main(title, fd, gotd.connection_limits,
2008 gotd.nconnection_limits);
2013 if (pledge("stdio getpw recvfd unix unveil", NULL) == -1)
2017 * We need the "unix" pledge promise for getpeername(2) only.
2018 * Ensure that AF_UNIX bind(2) cannot be used by revoking all
2019 * filesystem access via unveil(2). Access to password database
2020 * files will still work since "getpw" bypasses unveil(2).
2022 apply_unveil_none();
2024 auth_main(title, &gotd.repos, repo_path);
2027 case PROC_SESSION_READ:
2028 case PROC_SESSION_WRITE:
2031 * The "recvfd" promise is only needed during setup and
2032 * will be removed in a later pledge(2) call.
2034 if (pledge("stdio rpath wpath cpath recvfd sendfd fattr flock "
2035 "unveil", NULL) == -1)
2038 if (proc_id == PROC_SESSION_READ)
2039 apply_unveil_repo_readonly(repo_path, 1);
2041 apply_unveil_repo_readwrite(repo_path);
2043 session_main(title, repo_path, pack_fds, temp_fds,
2044 &gotd.request_timeout, proc_id);
2047 case PROC_REPO_READ:
2049 if (pledge("stdio rpath recvfd unveil", NULL) == -1)
2052 apply_unveil_repo_readonly(repo_path, 0);
2054 if (enter_chroot(repo_path)) {
2055 log_info("change repo path %s", repo_path);
2057 repo_path = strdup("/");
2058 if (repo_path == NULL)
2060 log_info("repo path is now %s", repo_path);
2064 repo_read_main(title, repo_path, pack_fds, temp_fds);
2067 case PROC_REPO_WRITE:
2069 if (pledge("stdio rpath recvfd unveil", NULL) == -1)
2072 apply_unveil_repo_readonly(repo_path, 0);
2073 repo = gotd_find_repo_by_path(repo_path, &gotd);
2075 fatalx("no repository for path %s", repo_path);
2077 if (enter_chroot(repo_path)) {
2079 repo_path = strdup("/");
2080 if (repo_path == NULL)
2085 repo_write_main(title, repo_path, pack_fds, temp_fds,
2086 &repo->protected_tag_namespaces,
2087 &repo->protected_branch_namespaces,
2088 &repo->protected_branches);
2092 fatal("invalid process id %d", proc_id);
2095 if (proc_id != PROC_GOTD)
2096 fatal("invalid process id %d", proc_id);
2098 evtimer_set(&gotd.listen_proc->tmo, kill_proc_timeout,
2101 apply_unveil_selfexec();
2103 signal_set(&evsigint, SIGINT, gotd_sighdlr, NULL);
2104 signal_set(&evsigterm, SIGTERM, gotd_sighdlr, NULL);
2105 signal_set(&evsighup, SIGHUP, gotd_sighdlr, NULL);
2106 signal_set(&evsigusr1, SIGUSR1, gotd_sighdlr, NULL);
2107 signal_set(&evsigchld, SIGCHLD, gotd_sighdlr, NULL);
2108 signal(SIGPIPE, SIG_IGN);
2110 signal_add(&evsigint, NULL);
2111 signal_add(&evsigterm, NULL);
2112 signal_add(&evsighup, NULL);
2113 signal_add(&evsigusr1, NULL);
2114 signal_add(&evsigchld, NULL);
2116 gotd_imsg_event_add(&gotd.listen_proc->iev);