Blob


1 /*
2 * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
3 *
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.
7 *
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.
15 */
17 #include <sys/queue.h>
18 #include <sys/tree.h>
19 #include <sys/time.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <sys/socket.h>
23 #include <sys/un.h>
24 #include <sys/wait.h>
26 #include <fcntl.h>
27 #include <err.h>
28 #include <errno.h>
29 #include <event.h>
30 #include <limits.h>
31 #include <pwd.h>
32 #include <imsg.h>
33 #include <sha1.h>
34 #include <sha2.h>
35 #include <signal.h>
36 #include <siphash.h>
37 #include <stdarg.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <syslog.h>
42 #include <unistd.h>
44 #include "got_error.h"
45 #include "got_opentemp.h"
46 #include "got_path.h"
47 #include "got_repository.h"
48 #include "got_object.h"
49 #include "got_reference.h"
51 #include "got_lib_delta.h"
52 #include "got_lib_object.h"
53 #include "got_lib_object_cache.h"
54 #include "got_lib_hash.h"
55 #include "got_lib_gitproto.h"
56 #include "got_lib_pack.h"
57 #include "got_lib_repository.h"
59 #include "gotd.h"
60 #include "log.h"
61 #include "listen.h"
62 #include "auth.h"
63 #include "session.h"
64 #include "repo_read.h"
65 #include "repo_write.h"
67 #ifndef nitems
68 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
69 #endif
71 enum gotd_client_state {
72 GOTD_CLIENT_STATE_NEW,
73 GOTD_CLIENT_STATE_ACCESS_GRANTED,
74 };
76 struct gotd_child_proc {
77 pid_t pid;
78 enum gotd_procid type;
79 char repo_name[NAME_MAX];
80 char repo_path[PATH_MAX];
81 int pipe[2];
82 struct gotd_imsgev iev;
83 struct event tmo;
85 TAILQ_ENTRY(gotd_child_proc) entry;
86 };
87 TAILQ_HEAD(gotd_procs, gotd_child_proc) procs;
89 struct gotd_client {
90 STAILQ_ENTRY(gotd_client) entry;
91 enum gotd_client_state state;
92 uint32_t id;
93 int fd;
94 struct gotd_imsgev iev;
95 struct event tmo;
96 uid_t euid;
97 gid_t egid;
98 struct gotd_child_proc *repo;
99 struct gotd_child_proc *auth;
100 struct gotd_child_proc *session;
101 int required_auth;
102 };
103 STAILQ_HEAD(gotd_clients, gotd_client);
105 static struct gotd_clients gotd_clients[GOTD_CLIENT_TABLE_SIZE];
106 static SIPHASH_KEY clients_hash_key;
107 volatile int client_cnt;
108 static struct timeval auth_timeout = { 5, 0 };
109 static struct gotd gotd;
111 void gotd_sighdlr(int sig, short event, void *arg);
112 static void gotd_shutdown(void);
113 static const struct got_error *start_session_child(struct gotd_client *,
114 struct gotd_repo *, char *, const char *, int, int);
115 static const struct got_error *start_repo_child(struct gotd_client *,
116 enum gotd_procid, struct gotd_repo *, char *, const char *, int, int);
117 static const struct got_error *start_auth_child(struct gotd_client *, int,
118 struct gotd_repo *, char *, const char *, int, int);
119 static void kill_proc(struct gotd_child_proc *, int);
120 static void disconnect(struct gotd_client *);
122 __dead static void
123 usage(void)
125 fprintf(stderr, "usage: %s [-dnv] [-f config-file]\n", getprogname());
126 exit(1);
129 static int
130 unix_socket_listen(const char *unix_socket_path, uid_t uid, gid_t gid)
132 struct sockaddr_un sun;
133 int fd = -1;
134 mode_t old_umask, mode;
136 fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK| SOCK_CLOEXEC, 0);
137 if (fd == -1) {
138 log_warn("socket");
139 return -1;
142 sun.sun_family = AF_UNIX;
143 if (strlcpy(sun.sun_path, unix_socket_path,
144 sizeof(sun.sun_path)) >= sizeof(sun.sun_path)) {
145 log_warnx("%s: name too long", unix_socket_path);
146 close(fd);
147 return -1;
150 if (unlink(unix_socket_path) == -1) {
151 if (errno != ENOENT) {
152 log_warn("unlink %s", unix_socket_path);
153 close(fd);
154 return -1;
158 old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
159 mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
161 if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
162 log_warn("bind: %s", unix_socket_path);
163 close(fd);
164 umask(old_umask);
165 return -1;
168 umask(old_umask);
170 if (chmod(unix_socket_path, mode) == -1) {
171 log_warn("chmod %o %s", mode, unix_socket_path);
172 close(fd);
173 unlink(unix_socket_path);
174 return -1;
177 if (chown(unix_socket_path, uid, gid) == -1) {
178 log_warn("chown %s uid=%d gid=%d", unix_socket_path, uid, gid);
179 close(fd);
180 unlink(unix_socket_path);
181 return -1;
184 if (listen(fd, GOTD_UNIX_SOCKET_BACKLOG) == -1) {
185 log_warn("listen");
186 close(fd);
187 unlink(unix_socket_path);
188 return -1;
191 return fd;
194 static uint64_t
195 client_hash(uint32_t client_id)
197 return SipHash24(&clients_hash_key, &client_id, sizeof(client_id));
200 static void
201 add_client(struct gotd_client *client)
203 uint64_t slot = client_hash(client->id) % nitems(gotd_clients);
204 STAILQ_INSERT_HEAD(&gotd_clients[slot], client, entry);
205 client_cnt++;
208 static struct gotd_client *
209 find_client(uint32_t client_id)
211 uint64_t slot;
212 struct gotd_client *c;
214 slot = client_hash(client_id) % nitems(gotd_clients);
215 STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
216 if (c->id == client_id)
217 return c;
220 return NULL;
223 static struct gotd_client *
224 find_client_by_proc_fd(int fd)
226 uint64_t slot;
228 for (slot = 0; slot < nitems(gotd_clients); slot++) {
229 struct gotd_client *c;
231 STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
232 if (c->repo && c->repo->iev.ibuf.fd == fd)
233 return c;
234 if (c->auth && c->auth->iev.ibuf.fd == fd)
235 return c;
236 if (c->session && c->session->iev.ibuf.fd == fd)
237 return c;
241 return NULL;
244 static int
245 client_is_reading(struct gotd_client *client)
247 return (client->required_auth &
248 (GOTD_AUTH_READ | GOTD_AUTH_WRITE)) == GOTD_AUTH_READ;
251 static int
252 client_is_writing(struct gotd_client *client)
254 return (client->required_auth &
255 (GOTD_AUTH_READ | GOTD_AUTH_WRITE)) ==
256 (GOTD_AUTH_READ | GOTD_AUTH_WRITE);
259 static const struct got_error *
260 ensure_client_is_not_writing(struct gotd_client *client)
262 if (client_is_writing(client)) {
263 return got_error_fmt(GOT_ERR_BAD_PACKET,
264 "uid %d made a read-request but is writing to "
265 "a repository", client->euid);
268 return NULL;
271 static const struct got_error *
272 ensure_client_is_not_reading(struct gotd_client *client)
274 if (client_is_reading(client)) {
275 return got_error_fmt(GOT_ERR_BAD_PACKET,
276 "uid %d made a write-request but is reading from "
277 "a repository", client->euid);
280 return NULL;
283 static void
284 proc_done(struct gotd_child_proc *proc)
286 struct gotd_client *client;
288 TAILQ_REMOVE(&procs, proc, entry);
290 client = find_client_by_proc_fd(proc->iev.ibuf.fd);
291 if (client != NULL) {
292 if (proc == client->repo)
293 client->repo = NULL;
294 if (proc == client->auth)
295 client->auth = NULL;
296 if (proc == client->session)
297 client->session = NULL;
298 disconnect(client);
301 evtimer_del(&proc->tmo);
303 if (proc->iev.ibuf.fd != -1) {
304 event_del(&proc->iev.ev);
305 msgbuf_clear(&proc->iev.ibuf.w);
306 close(proc->iev.ibuf.fd);
309 free(proc);
312 static void
313 kill_repo_proc(struct gotd_client *client)
315 if (client->repo == NULL)
316 return;
318 kill_proc(client->repo, 0);
319 client->repo = NULL;
322 static void
323 kill_auth_proc(struct gotd_client *client)
325 if (client->auth == NULL)
326 return;
328 kill_proc(client->auth, 0);
329 client->auth = NULL;
332 static void
333 kill_session_proc(struct gotd_client *client)
335 if (client->session == NULL)
336 return;
338 kill_proc(client->session, 0);
339 client->session = NULL;
342 static void
343 disconnect(struct gotd_client *client)
345 struct gotd_imsg_disconnect idisconnect;
346 struct gotd_child_proc *listen_proc = gotd.listen_proc;
347 uint64_t slot;
349 log_debug("uid %d: disconnecting", client->euid);
351 kill_auth_proc(client);
352 kill_session_proc(client);
353 kill_repo_proc(client);
355 idisconnect.client_id = client->id;
356 if (gotd_imsg_compose_event(&listen_proc->iev,
357 GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
358 &idisconnect, sizeof(idisconnect)) == -1)
359 log_warn("imsg compose DISCONNECT");
361 slot = client_hash(client->id) % nitems(gotd_clients);
362 STAILQ_REMOVE(&gotd_clients[slot], client, gotd_client, entry);
363 imsg_clear(&client->iev.ibuf);
364 event_del(&client->iev.ev);
365 evtimer_del(&client->tmo);
366 if (client->fd != -1)
367 close(client->fd);
368 else if (client->iev.ibuf.fd != -1)
369 close(client->iev.ibuf.fd);
370 free(client);
371 client_cnt--;
374 static void
375 disconnect_on_error(struct gotd_client *client, const struct got_error *err)
377 struct imsgbuf ibuf;
379 log_warnx("uid %d: %s", client->euid, err->msg);
380 if (err->code != GOT_ERR_EOF && client->fd != -1) {
381 imsg_init(&ibuf, client->fd);
382 gotd_imsg_send_error(&ibuf, 0, PROC_GOTD, err);
383 imsg_clear(&ibuf);
385 disconnect(client);
388 static const struct got_error *
389 send_repo_info(struct gotd_imsgev *iev, struct gotd_repo *repo)
391 const struct got_error *err = NULL;
392 struct gotd_imsg_info_repo irepo;
394 memset(&irepo, 0, sizeof(irepo));
396 if (strlcpy(irepo.repo_name, repo->name, sizeof(irepo.repo_name))
397 >= sizeof(irepo.repo_name))
398 return got_error_msg(GOT_ERR_NO_SPACE, "repo name too long");
399 if (strlcpy(irepo.repo_path, repo->path, sizeof(irepo.repo_path))
400 >= sizeof(irepo.repo_path))
401 return got_error_msg(GOT_ERR_NO_SPACE, "repo path too long");
403 if (gotd_imsg_compose_event(iev, GOTD_IMSG_INFO_REPO, PROC_GOTD, -1,
404 &irepo, sizeof(irepo)) == -1) {
405 err = got_error_from_errno("imsg compose INFO_REPO");
406 if (err)
407 return err;
410 return NULL;
413 static const struct got_error *
414 send_client_info(struct gotd_imsgev *iev, struct gotd_client *client)
416 const struct got_error *err = NULL;
417 struct gotd_imsg_info_client iclient;
418 struct gotd_child_proc *proc;
420 memset(&iclient, 0, sizeof(iclient));
421 iclient.euid = client->euid;
422 iclient.egid = client->egid;
424 proc = client->repo;
425 if (proc) {
426 if (strlcpy(iclient.repo_name, proc->repo_path,
427 sizeof(iclient.repo_name)) >= sizeof(iclient.repo_name)) {
428 return got_error_msg(GOT_ERR_NO_SPACE,
429 "repo name too long");
431 if (client_is_writing(client))
432 iclient.is_writing = 1;
434 iclient.repo_child_pid = proc->pid;
437 if (client->session)
438 iclient.session_child_pid = client->session->pid;
440 if (gotd_imsg_compose_event(iev, GOTD_IMSG_INFO_CLIENT, PROC_GOTD, -1,
441 &iclient, sizeof(iclient)) == -1) {
442 err = got_error_from_errno("imsg compose INFO_CLIENT");
443 if (err)
444 return err;
447 return NULL;
450 static const struct got_error *
451 send_info(struct gotd_client *client)
453 const struct got_error *err = NULL;
454 struct gotd_imsg_info info;
455 uint64_t slot;
456 struct gotd_repo *repo;
458 if (client->euid != 0)
459 return got_error_set_errno(EPERM, "info");
461 info.pid = gotd.pid;
462 info.verbosity = gotd.verbosity;
463 info.nrepos = gotd.nrepos;
464 info.nclients = client_cnt - 1;
466 if (gotd_imsg_compose_event(&client->iev, GOTD_IMSG_INFO, PROC_GOTD, -1,
467 &info, sizeof(info)) == -1) {
468 err = got_error_from_errno("imsg compose INFO");
469 if (err)
470 return err;
473 TAILQ_FOREACH(repo, &gotd.repos, entry) {
474 err = send_repo_info(&client->iev, repo);
475 if (err)
476 return err;
479 for (slot = 0; slot < nitems(gotd_clients); slot++) {
480 struct gotd_client *c;
481 STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
482 if (c->id == client->id)
483 continue;
484 err = send_client_info(&client->iev, c);
485 if (err)
486 return err;
490 return NULL;
493 static const struct got_error *
494 stop_gotd(struct gotd_client *client)
497 if (client->euid != 0)
498 return got_error_set_errno(EPERM, "stop");
500 gotd_shutdown();
501 /* NOTREACHED */
502 return NULL;
505 static const struct got_error *
506 start_client_authentication(struct gotd_client *client, struct imsg *imsg)
508 const struct got_error *err;
509 struct gotd_imsg_list_refs ireq;
510 struct gotd_repo *repo = NULL;
511 size_t datalen;
513 log_debug("list-refs request from uid %d", client->euid);
515 if (client->state != GOTD_CLIENT_STATE_NEW)
516 return got_error_msg(GOT_ERR_BAD_REQUEST,
517 "unexpected list-refs request received");
519 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
520 if (datalen != sizeof(ireq))
521 return got_error(GOT_ERR_PRIVSEP_LEN);
523 memcpy(&ireq, imsg->data, datalen);
525 if (ireq.client_is_reading) {
526 err = ensure_client_is_not_writing(client);
527 if (err)
528 return err;
529 repo = gotd_find_repo_by_name(ireq.repo_name, &gotd);
530 if (repo == NULL)
531 return got_error(GOT_ERR_NOT_GIT_REPO);
532 err = start_auth_child(client, GOTD_AUTH_READ, repo,
533 gotd.argv0, gotd.confpath, gotd.daemonize,
534 gotd.verbosity);
535 if (err)
536 return err;
537 } else {
538 err = ensure_client_is_not_reading(client);
539 if (err)
540 return err;
541 repo = gotd_find_repo_by_name(ireq.repo_name, &gotd);
542 if (repo == NULL)
543 return got_error(GOT_ERR_NOT_GIT_REPO);
544 err = start_auth_child(client,
545 GOTD_AUTH_READ | GOTD_AUTH_WRITE,
546 repo, gotd.argv0, gotd.confpath, gotd.daemonize,
547 gotd.verbosity);
548 if (err)
549 return err;
552 evtimer_add(&client->tmo, &auth_timeout);
554 /* Flow continues upon authentication successs/failure or timeout. */
555 return NULL;
558 static void
559 gotd_request(int fd, short events, void *arg)
561 struct gotd_imsgev *iev = arg;
562 struct imsgbuf *ibuf = &iev->ibuf;
563 struct gotd_client *client = iev->handler_arg;
564 const struct got_error *err = NULL;
565 struct imsg imsg;
566 ssize_t n;
568 if (events & EV_WRITE) {
569 while (ibuf->w.queued) {
570 n = msgbuf_write(&ibuf->w);
571 if (n == -1 && errno == EPIPE) {
572 /*
573 * The client has closed its socket.
574 * This can happen when Git clients are
575 * done sending pack file data.
576 */
577 msgbuf_clear(&ibuf->w);
578 continue;
579 } else if (n == -1 && errno != EAGAIN) {
580 err = got_error_from_errno("imsg_flush");
581 disconnect_on_error(client, err);
582 return;
584 if (n == 0) {
585 /* Connection closed. */
586 err = got_error(GOT_ERR_EOF);
587 disconnect_on_error(client, err);
588 return;
592 /* Disconnect gotctl(8) now that messages have been sent. */
593 if (!client_is_reading(client) && !client_is_writing(client)) {
594 disconnect(client);
595 return;
599 if ((events & EV_READ) == 0)
600 return;
602 memset(&imsg, 0, sizeof(imsg));
604 while (err == NULL) {
605 err = gotd_imsg_recv(&imsg, ibuf, 0);
606 if (err) {
607 if (err->code == GOT_ERR_PRIVSEP_READ)
608 err = NULL;
609 break;
612 evtimer_del(&client->tmo);
614 switch (imsg.hdr.type) {
615 case GOTD_IMSG_INFO:
616 err = send_info(client);
617 break;
618 case GOTD_IMSG_STOP:
619 err = stop_gotd(client);
620 break;
621 case GOTD_IMSG_LIST_REFS:
622 err = start_client_authentication(client, &imsg);
623 break;
624 default:
625 log_debug("unexpected imsg %d", imsg.hdr.type);
626 err = got_error(GOT_ERR_PRIVSEP_MSG);
627 break;
630 imsg_free(&imsg);
633 if (err) {
634 disconnect_on_error(client, err);
635 } else {
636 gotd_imsg_event_add(&client->iev);
640 static void
641 gotd_auth_timeout(int fd, short events, void *arg)
643 struct gotd_client *client = arg;
645 log_debug("disconnecting uid %d due to authentication timeout",
646 client->euid);
647 disconnect(client);
650 static const struct got_error *
651 recv_connect(uint32_t *client_id, struct imsg *imsg)
653 const struct got_error *err = NULL;
654 struct gotd_imsg_connect iconnect;
655 size_t datalen;
656 int s = -1;
657 struct gotd_client *client = NULL;
659 *client_id = 0;
661 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
662 if (datalen != sizeof(iconnect))
663 return got_error(GOT_ERR_PRIVSEP_LEN);
664 memcpy(&iconnect, imsg->data, sizeof(iconnect));
666 s = imsg->fd;
667 if (s == -1) {
668 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
669 goto done;
672 if (find_client(iconnect.client_id)) {
673 err = got_error_msg(GOT_ERR_CLIENT_ID, "duplicate client ID");
674 goto done;
677 client = calloc(1, sizeof(*client));
678 if (client == NULL) {
679 err = got_error_from_errno("calloc");
680 goto done;
683 *client_id = iconnect.client_id;
685 client->state = GOTD_CLIENT_STATE_NEW;
686 client->id = iconnect.client_id;
687 client->fd = s;
688 s = -1;
689 /* The auth process will verify UID/GID for us. */
690 client->euid = iconnect.euid;
691 client->egid = iconnect.egid;
693 imsg_init(&client->iev.ibuf, client->fd);
694 client->iev.handler = gotd_request;
695 client->iev.events = EV_READ;
696 client->iev.handler_arg = client;
698 event_set(&client->iev.ev, client->fd, EV_READ, gotd_request,
699 &client->iev);
700 gotd_imsg_event_add(&client->iev);
702 evtimer_set(&client->tmo, gotd_auth_timeout, client);
704 add_client(client);
705 log_debug("%s: new client uid %d connected on fd %d", __func__,
706 client->euid, client->fd);
707 done:
708 if (err) {
709 struct gotd_child_proc *listen_proc = gotd.listen_proc;
710 struct gotd_imsg_disconnect idisconnect;
712 idisconnect.client_id = client->id;
713 if (gotd_imsg_compose_event(&listen_proc->iev,
714 GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
715 &idisconnect, sizeof(idisconnect)) == -1)
716 log_warn("imsg compose DISCONNECT");
718 if (s != -1)
719 close(s);
722 return err;
725 static const char *gotd_proc_names[PROC_MAX] = {
726 "parent",
727 "listen",
728 "auth",
729 "session_read",
730 "session_write",
731 "repo_read",
732 "repo_write"
733 };
735 static void
736 kill_proc(struct gotd_child_proc *proc, int fatal)
738 struct timeval tv = { 5, 0 };
740 log_debug("kill -%d %d", fatal ? SIGKILL : SIGTERM, proc->pid);
742 if (proc->iev.ibuf.fd != -1) {
743 event_del(&proc->iev.ev);
744 msgbuf_clear(&proc->iev.ibuf.w);
745 close(proc->iev.ibuf.fd);
746 proc->iev.ibuf.fd = -1;
749 if (!evtimer_pending(&proc->tmo, NULL) && !fatal)
750 evtimer_add(&proc->tmo, &tv);
752 if (fatal) {
753 log_warnx("sending SIGKILL to PID %d", proc->pid);
754 kill(proc->pid, SIGKILL);
755 } else
756 kill(proc->pid, SIGTERM);
759 static void
760 kill_proc_timeout(int fd, short ev, void *d)
762 struct gotd_child_proc *proc = d;
764 log_warnx("timeout waiting for PID %d to terminate;"
765 " retrying with force", proc->pid);
766 kill_proc(proc, 1);
769 static void
770 gotd_shutdown(void)
772 uint64_t slot;
774 log_debug("shutting down");
775 for (slot = 0; slot < nitems(gotd_clients); slot++) {
776 struct gotd_client *c, *tmp;
778 STAILQ_FOREACH_SAFE(c, &gotd_clients[slot], entry, tmp)
779 disconnect(c);
782 kill_proc(gotd.listen_proc, 0);
784 log_info("terminating");
785 exit(0);
788 static struct gotd_child_proc *
789 find_proc_by_pid(pid_t pid)
791 struct gotd_child_proc *proc = NULL;
793 TAILQ_FOREACH(proc, &procs, entry)
794 if (proc->pid == pid)
795 break;
797 return proc;
800 void
801 gotd_sighdlr(int sig, short event, void *arg)
803 struct gotd_child_proc *proc;
804 pid_t pid;
805 int status;
807 /*
808 * Normal signal handler rules don't apply because libevent
809 * decouples for us.
810 */
812 switch (sig) {
813 case SIGHUP:
814 log_info("%s: ignoring SIGHUP", __func__);
815 break;
816 case SIGUSR1:
817 log_info("%s: ignoring SIGUSR1", __func__);
818 break;
819 case SIGTERM:
820 case SIGINT:
821 gotd_shutdown();
822 break;
823 case SIGCHLD:
824 for (;;) {
825 pid = waitpid(WAIT_ANY, &status, WNOHANG);
826 if (pid == -1) {
827 if (errno == EINTR)
828 continue;
829 if (errno == ECHILD)
830 break;
831 fatal("waitpid");
833 if (pid == 0)
834 break;
836 log_debug("reaped pid %d", pid);
837 proc = find_proc_by_pid(pid);
838 if (proc == NULL) {
839 log_info("caught exit of unknown child %d",
840 pid);
841 continue;
844 if (WIFSIGNALED(status)) {
845 log_warnx("child PID %d terminated with"
846 " signal %d", pid, WTERMSIG(status));
849 proc_done(proc);
851 break;
852 default:
853 fatalx("unexpected signal");
857 static const struct got_error *
858 ensure_proc_is_reading(struct gotd_client *client,
859 struct gotd_child_proc *proc)
861 if (!client_is_reading(client)) {
862 kill_proc(proc, 1);
863 return got_error_fmt(GOT_ERR_BAD_PACKET,
864 "PID %d handled a read-request for uid %d but this "
865 "user is not reading from a repository", proc->pid,
866 client->euid);
869 return NULL;
872 static const struct got_error *
873 ensure_proc_is_writing(struct gotd_client *client,
874 struct gotd_child_proc *proc)
876 if (!client_is_writing(client)) {
877 kill_proc(proc, 1);
878 return got_error_fmt(GOT_ERR_BAD_PACKET,
879 "PID %d handled a write-request for uid %d but this "
880 "user is not writing to a repository", proc->pid,
881 client->euid);
884 return NULL;
887 static int
888 verify_imsg_src(struct gotd_client *client, struct gotd_child_proc *proc,
889 struct imsg *imsg)
891 const struct got_error *err;
892 int ret = 0;
894 if (proc->type == PROC_REPO_READ || proc->type == PROC_REPO_WRITE) {
895 if (client->repo == NULL)
896 fatalx("no process found for uid %d", client->euid);
897 if (proc->pid != client->repo->pid) {
898 kill_proc(proc, 1);
899 log_warnx("received message from PID %d for uid %d, "
900 "while PID %d is the process serving this user",
901 proc->pid, client->euid, client->repo->pid);
902 return 0;
905 if (proc->type == PROC_SESSION_READ ||
906 proc->type == PROC_SESSION_WRITE) {
907 if (client->session == NULL) {
908 log_warnx("no session found for uid %d", client->euid);
909 return 0;
911 if (proc->pid != client->session->pid) {
912 kill_proc(proc, 1);
913 log_warnx("received message from PID %d for uid %d, "
914 "while PID %d is the process serving this user",
915 proc->pid, client->euid, client->session->pid);
916 return 0;
920 switch (imsg->hdr.type) {
921 case GOTD_IMSG_ERROR:
922 ret = 1;
923 break;
924 case GOTD_IMSG_CONNECT:
925 if (proc->type != PROC_LISTEN) {
926 err = got_error_fmt(GOT_ERR_BAD_PACKET,
927 "new connection for uid %d from PID %d "
928 "which is not the listen process",
929 proc->pid, client->euid);
930 } else
931 ret = 1;
932 break;
933 case GOTD_IMSG_ACCESS_GRANTED:
934 if (proc->type != PROC_AUTH) {
935 err = got_error_fmt(GOT_ERR_BAD_PACKET,
936 "authentication of uid %d from PID %d "
937 "which is not the auth process",
938 proc->pid, client->euid);
939 } else
940 ret = 1;
941 break;
942 case GOTD_IMSG_CLIENT_SESSION_READY:
943 if (proc->type != PROC_SESSION_READ &&
944 proc->type != PROC_SESSION_WRITE) {
945 err = got_error_fmt(GOT_ERR_BAD_PACKET,
946 "unexpected \"ready\" signal from PID %d",
947 proc->pid);
948 } else
949 ret = 1;
950 break;
951 case GOTD_IMSG_REPO_CHILD_READY:
952 if (proc->type != PROC_REPO_READ &&
953 proc->type != PROC_REPO_WRITE) {
954 err = got_error_fmt(GOT_ERR_BAD_PACKET,
955 "unexpected \"ready\" signal from PID %d",
956 proc->pid);
957 } else
958 ret = 1;
959 break;
960 case GOTD_IMSG_PACKFILE_DONE:
961 err = ensure_proc_is_reading(client, proc);
962 if (err)
963 log_warnx("uid %d: %s", client->euid, err->msg);
964 else
965 ret = 1;
966 break;
967 case GOTD_IMSG_PACKFILE_INSTALL:
968 case GOTD_IMSG_REF_UPDATES_START:
969 case GOTD_IMSG_REF_UPDATE:
970 err = ensure_proc_is_writing(client, proc);
971 if (err)
972 log_warnx("uid %d: %s", client->euid, err->msg);
973 else
974 ret = 1;
975 break;
976 default:
977 log_debug("%s: unexpected imsg %d", __func__, imsg->hdr.type);
978 break;
981 return ret;
984 static const struct got_error *
985 connect_repo_child(struct gotd_client *client,
986 struct gotd_child_proc *repo_proc)
988 static const struct got_error *err;
989 struct gotd_imsgev *session_iev = &client->session->iev;
990 struct gotd_imsg_connect_repo_child ireq;
991 int pipe[2];
993 if (client->state != GOTD_CLIENT_STATE_ACCESS_GRANTED)
994 return got_error_msg(GOT_ERR_BAD_REQUEST,
995 "unexpected repo child ready signal received");
997 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
998 PF_UNSPEC, pipe) == -1)
999 fatal("socketpair");
1001 memset(&ireq, 0, sizeof(ireq));
1002 ireq.client_id = client->id;
1003 ireq.proc_id = repo_proc->type;
1005 /* Pass repo child pipe to session child process. */
1006 if (gotd_imsg_compose_event(session_iev, GOTD_IMSG_CONNECT_REPO_CHILD,
1007 PROC_GOTD, pipe[0], &ireq, sizeof(ireq)) == -1) {
1008 err = got_error_from_errno("imsg compose CONNECT_REPO_CHILD");
1009 close(pipe[0]);
1010 close(pipe[1]);
1011 return err;
1014 /* Pass session child pipe to repo child process. */
1015 if (gotd_imsg_compose_event(&repo_proc->iev,
1016 GOTD_IMSG_CONNECT_REPO_CHILD, PROC_GOTD, pipe[1], NULL, 0) == -1) {
1017 err = got_error_from_errno("imsg compose CONNECT_REPO_CHILD");
1018 close(pipe[1]);
1019 return err;
1022 return NULL;
1025 static void
1026 gotd_dispatch_listener(int fd, short event, void *arg)
1028 struct gotd_imsgev *iev = arg;
1029 struct imsgbuf *ibuf = &iev->ibuf;
1030 struct gotd_child_proc *proc = gotd.listen_proc;
1031 ssize_t n;
1032 int shut = 0;
1033 struct imsg imsg;
1035 if (proc->iev.ibuf.fd != fd)
1036 fatalx("%s: unexpected fd %d", __func__, fd);
1038 if (event & EV_READ) {
1039 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1040 fatal("imsg_read error");
1041 if (n == 0) {
1042 /* Connection closed. */
1043 shut = 1;
1044 goto done;
1048 if (event & EV_WRITE) {
1049 n = msgbuf_write(&ibuf->w);
1050 if (n == -1 && errno != EAGAIN)
1051 fatal("msgbuf_write");
1052 if (n == 0) {
1053 /* Connection closed. */
1054 shut = 1;
1055 goto done;
1059 for (;;) {
1060 const struct got_error *err = NULL;
1061 struct gotd_client *client = NULL;
1062 uint32_t client_id = 0;
1063 int do_disconnect = 0;
1065 if ((n = imsg_get(ibuf, &imsg)) == -1)
1066 fatal("%s: imsg_get error", __func__);
1067 if (n == 0) /* No more messages. */
1068 break;
1070 switch (imsg.hdr.type) {
1071 case GOTD_IMSG_ERROR:
1072 do_disconnect = 1;
1073 err = gotd_imsg_recv_error(&client_id, &imsg);
1074 break;
1075 case GOTD_IMSG_CONNECT:
1076 err = recv_connect(&client_id, &imsg);
1077 break;
1078 default:
1079 log_debug("unexpected imsg %d", imsg.hdr.type);
1080 break;
1083 client = find_client(client_id);
1084 if (client == NULL) {
1085 log_warnx("%s: client not found", __func__);
1086 imsg_free(&imsg);
1087 continue;
1090 if (err)
1091 log_warnx("uid %d: %s", client->euid, err->msg);
1093 if (do_disconnect) {
1094 if (err)
1095 disconnect_on_error(client, err);
1096 else
1097 disconnect(client);
1100 imsg_free(&imsg);
1102 done:
1103 if (!shut) {
1104 gotd_imsg_event_add(iev);
1105 } else {
1106 /* This pipe is dead. Remove its event handler */
1107 event_del(&iev->ev);
1108 event_loopexit(NULL);
1112 static void
1113 gotd_dispatch_auth_child(int fd, short event, void *arg)
1115 const struct got_error *err = NULL;
1116 struct gotd_imsgev *iev = arg;
1117 struct imsgbuf *ibuf = &iev->ibuf;
1118 struct gotd_client *client;
1119 struct gotd_repo *repo = NULL;
1120 ssize_t n;
1121 int shut = 0;
1122 struct imsg imsg;
1123 uint32_t client_id = 0;
1124 int do_disconnect = 0;
1126 client = find_client_by_proc_fd(fd);
1127 if (client == NULL) {
1128 /* Can happen during process teardown. */
1129 warnx("cannot find client for fd %d", fd);
1130 shut = 1;
1131 goto done;
1134 if (client->auth == NULL)
1135 fatalx("cannot find auth child process for fd %d", fd);
1137 if (event & EV_READ) {
1138 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1139 fatal("imsg_read error");
1140 if (n == 0) {
1141 /* Connection closed. */
1142 shut = 1;
1143 goto done;
1147 if (event & EV_WRITE) {
1148 n = msgbuf_write(&ibuf->w);
1149 if (n == -1 && errno != EAGAIN)
1150 fatal("msgbuf_write");
1151 if (n == 0) {
1152 /* Connection closed. */
1153 shut = 1;
1155 goto done;
1158 if (client->auth->iev.ibuf.fd != fd)
1159 fatalx("%s: unexpected fd %d", __func__, fd);
1161 if ((n = imsg_get(ibuf, &imsg)) == -1)
1162 fatal("%s: imsg_get error", __func__);
1163 if (n == 0) /* No more messages. */
1164 return;
1166 evtimer_del(&client->tmo);
1168 switch (imsg.hdr.type) {
1169 case GOTD_IMSG_ERROR:
1170 do_disconnect = 1;
1171 err = gotd_imsg_recv_error(&client_id, &imsg);
1172 break;
1173 case GOTD_IMSG_ACCESS_GRANTED:
1174 client->state = GOTD_CLIENT_STATE_ACCESS_GRANTED;
1175 break;
1176 default:
1177 do_disconnect = 1;
1178 log_debug("unexpected imsg %d", imsg.hdr.type);
1179 break;
1182 if (!verify_imsg_src(client, client->auth, &imsg)) {
1183 do_disconnect = 1;
1184 log_debug("dropping imsg type %d from PID %d",
1185 imsg.hdr.type, client->auth->pid);
1187 imsg_free(&imsg);
1189 if (do_disconnect) {
1190 if (err)
1191 disconnect_on_error(client, err);
1192 else
1193 disconnect(client);
1194 return;
1197 repo = gotd_find_repo_by_name(client->auth->repo_name, &gotd);
1198 if (repo == NULL) {
1199 err = got_error(GOT_ERR_NOT_GIT_REPO);
1200 goto done;
1202 kill_auth_proc(client);
1204 log_info("authenticated uid %d for repository %s",
1205 client->euid, repo->name);
1207 err = start_session_child(client, repo, gotd.argv0,
1208 gotd.confpath, gotd.daemonize, gotd.verbosity);
1209 if (err)
1210 goto done;
1211 done:
1212 if (err)
1213 log_warnx("uid %d: %s", client->euid, err->msg);
1215 /* We might have killed the auth process by now. */
1216 if (client->auth != NULL) {
1217 if (!shut) {
1218 gotd_imsg_event_add(iev);
1219 } else {
1220 /* This pipe is dead. Remove its event handler */
1221 event_del(&iev->ev);
1226 static const struct got_error *
1227 connect_session(struct gotd_client *client)
1229 const struct got_error *err = NULL;
1230 struct gotd_imsg_connect iconnect;
1231 int s;
1233 memset(&iconnect, 0, sizeof(iconnect));
1235 s = dup(client->fd);
1236 if (s == -1)
1237 return got_error_from_errno("dup");
1239 iconnect.client_id = client->id;
1240 iconnect.euid = client->euid;
1241 iconnect.egid = client->egid;
1243 if (gotd_imsg_compose_event(&client->session->iev, GOTD_IMSG_CONNECT,
1244 PROC_GOTD, s, &iconnect, sizeof(iconnect)) == -1) {
1245 err = got_error_from_errno("imsg compose CONNECT");
1246 close(s);
1247 return err;
1251 * We are no longer interested in messages from this client.
1252 * Further client requests will be handled by the session process.
1254 msgbuf_clear(&client->iev.ibuf.w);
1255 imsg_clear(&client->iev.ibuf);
1256 event_del(&client->iev.ev);
1257 client->fd = -1; /* will be closed via copy in client->iev.ibuf.fd */
1259 return NULL;
1262 static void
1263 gotd_dispatch_client_session(int fd, short event, void *arg)
1265 struct gotd_imsgev *iev = arg;
1266 struct imsgbuf *ibuf = &iev->ibuf;
1267 struct gotd_child_proc *proc = NULL;
1268 struct gotd_client *client = NULL;
1269 ssize_t n;
1270 int shut = 0;
1271 struct imsg imsg;
1273 client = find_client_by_proc_fd(fd);
1274 if (client == NULL) {
1275 /* Can happen during process teardown. */
1276 warnx("cannot find client for fd %d", fd);
1277 shut = 1;
1278 goto done;
1281 if (event & EV_READ) {
1282 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1283 fatal("imsg_read error");
1284 if (n == 0) {
1285 /* Connection closed. */
1286 shut = 1;
1287 goto done;
1291 if (event & EV_WRITE) {
1292 n = msgbuf_write(&ibuf->w);
1293 if (n == -1 && errno != EAGAIN)
1294 fatal("msgbuf_write");
1295 if (n == 0) {
1296 /* Connection closed. */
1297 shut = 1;
1298 goto done;
1302 proc = client->session;
1303 if (proc == NULL)
1304 fatalx("cannot find session child process for fd %d", fd);
1306 for (;;) {
1307 const struct got_error *err = NULL;
1308 uint32_t client_id = 0;
1309 int do_disconnect = 0, do_start_repo_child = 0;
1311 if ((n = imsg_get(ibuf, &imsg)) == -1)
1312 fatal("%s: imsg_get error", __func__);
1313 if (n == 0) /* No more messages. */
1314 break;
1316 switch (imsg.hdr.type) {
1317 case GOTD_IMSG_ERROR:
1318 do_disconnect = 1;
1319 err = gotd_imsg_recv_error(&client_id, &imsg);
1320 break;
1321 case GOTD_IMSG_CLIENT_SESSION_READY:
1322 if (client->state != GOTD_CLIENT_STATE_ACCESS_GRANTED) {
1323 err = got_error(GOT_ERR_PRIVSEP_MSG);
1324 break;
1326 do_start_repo_child = 1;
1327 break;
1328 case GOTD_IMSG_DISCONNECT:
1329 do_disconnect = 1;
1330 break;
1331 default:
1332 log_debug("unexpected imsg %d", imsg.hdr.type);
1333 break;
1336 if (!verify_imsg_src(client, proc, &imsg)) {
1337 log_debug("dropping imsg type %d from PID %d",
1338 imsg.hdr.type, proc->pid);
1339 imsg_free(&imsg);
1340 continue;
1342 if (err)
1343 log_warnx("uid %d: %s", client->euid, err->msg);
1345 if (do_start_repo_child) {
1346 struct gotd_repo *repo;
1347 const char *name = client->session->repo_name;
1349 repo = gotd_find_repo_by_name(name, &gotd);
1350 if (repo != NULL) {
1351 enum gotd_procid proc_type;
1353 if (client->required_auth & GOTD_AUTH_WRITE)
1354 proc_type = PROC_REPO_WRITE;
1355 else
1356 proc_type = PROC_REPO_READ;
1358 err = start_repo_child(client, proc_type, repo,
1359 gotd.argv0, gotd.confpath, gotd.daemonize,
1360 gotd.verbosity);
1361 } else
1362 err = got_error(GOT_ERR_NOT_GIT_REPO);
1364 if (err) {
1365 log_warnx("uid %d: %s", client->euid, err->msg);
1366 do_disconnect = 1;
1370 if (do_disconnect) {
1371 if (err)
1372 disconnect_on_error(client, err);
1373 else
1374 disconnect(client);
1377 imsg_free(&imsg);
1379 done:
1380 if (!shut) {
1381 gotd_imsg_event_add(iev);
1382 } else {
1383 /* This pipe is dead. Remove its event handler */
1384 event_del(&iev->ev);
1385 disconnect(client);
1389 static void
1390 gotd_dispatch_repo_child(int fd, short event, void *arg)
1392 struct gotd_imsgev *iev = arg;
1393 struct imsgbuf *ibuf = &iev->ibuf;
1394 struct gotd_child_proc *proc = NULL;
1395 struct gotd_client *client;
1396 ssize_t n;
1397 int shut = 0;
1398 struct imsg imsg;
1400 client = find_client_by_proc_fd(fd);
1401 if (client == NULL) {
1402 /* Can happen during process teardown. */
1403 warnx("cannot find client for fd %d", fd);
1404 shut = 1;
1405 goto done;
1408 if (event & EV_READ) {
1409 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1410 fatal("imsg_read error");
1411 if (n == 0) {
1412 /* Connection closed. */
1413 shut = 1;
1414 goto done;
1418 if (event & EV_WRITE) {
1419 n = msgbuf_write(&ibuf->w);
1420 if (n == -1 && errno != EAGAIN)
1421 fatal("msgbuf_write");
1422 if (n == 0) {
1423 /* Connection closed. */
1424 shut = 1;
1425 goto done;
1429 proc = client->repo;
1430 if (proc == NULL)
1431 fatalx("cannot find child process for fd %d", fd);
1433 for (;;) {
1434 const struct got_error *err = NULL;
1435 uint32_t client_id = 0;
1436 int do_disconnect = 0;
1438 if ((n = imsg_get(ibuf, &imsg)) == -1)
1439 fatal("%s: imsg_get error", __func__);
1440 if (n == 0) /* No more messages. */
1441 break;
1443 switch (imsg.hdr.type) {
1444 case GOTD_IMSG_ERROR:
1445 do_disconnect = 1;
1446 err = gotd_imsg_recv_error(&client_id, &imsg);
1447 break;
1448 case GOTD_IMSG_REPO_CHILD_READY:
1449 err = connect_session(client);
1450 if (err)
1451 break;
1452 err = connect_repo_child(client, proc);
1453 break;
1454 default:
1455 log_debug("unexpected imsg %d", imsg.hdr.type);
1456 break;
1459 if (!verify_imsg_src(client, proc, &imsg)) {
1460 log_debug("dropping imsg type %d from PID %d",
1461 imsg.hdr.type, proc->pid);
1462 imsg_free(&imsg);
1463 continue;
1465 if (err)
1466 log_warnx("uid %d: %s", client->euid, err->msg);
1468 if (do_disconnect) {
1469 if (err)
1470 disconnect_on_error(client, err);
1471 else
1472 disconnect(client);
1475 imsg_free(&imsg);
1477 done:
1478 if (!shut) {
1479 gotd_imsg_event_add(iev);
1480 } else {
1481 /* This pipe is dead. Remove its event handler */
1482 event_del(&iev->ev);
1483 disconnect(client);
1487 static pid_t
1488 start_child(enum gotd_procid proc_id, const char *repo_path,
1489 char *argv0, const char *confpath, int fd, int daemonize, int verbosity)
1491 char *argv[11];
1492 int argc = 0;
1493 pid_t pid;
1495 switch (pid = fork()) {
1496 case -1:
1497 fatal("cannot fork");
1498 case 0:
1499 break;
1500 default:
1501 close(fd);
1502 return pid;
1505 if (fd != GOTD_FILENO_MSG_PIPE) {
1506 if (dup2(fd, GOTD_FILENO_MSG_PIPE) == -1)
1507 fatal("cannot setup imsg fd");
1508 } else if (fcntl(fd, F_SETFD, 0) == -1)
1509 fatal("cannot setup imsg fd");
1511 argv[argc++] = argv0;
1512 switch (proc_id) {
1513 case PROC_LISTEN:
1514 argv[argc++] = (char *)"-L";
1515 break;
1516 case PROC_AUTH:
1517 argv[argc++] = (char *)"-A";
1518 break;
1519 case PROC_SESSION_READ:
1520 argv[argc++] = (char *)"-s";
1521 break;
1522 case PROC_SESSION_WRITE:
1523 argv[argc++] = (char *)"-S";
1524 break;
1525 case PROC_REPO_READ:
1526 argv[argc++] = (char *)"-R";
1527 break;
1528 case PROC_REPO_WRITE:
1529 argv[argc++] = (char *)"-W";
1530 break;
1531 default:
1532 fatalx("invalid process id %d", proc_id);
1535 argv[argc++] = (char *)"-f";
1536 argv[argc++] = (char *)confpath;
1538 if (repo_path) {
1539 argv[argc++] = (char *)"-P";
1540 argv[argc++] = (char *)repo_path;
1543 if (!daemonize)
1544 argv[argc++] = (char *)"-d";
1545 if (verbosity > 0)
1546 argv[argc++] = (char *)"-v";
1547 if (verbosity > 1)
1548 argv[argc++] = (char *)"-v";
1549 argv[argc++] = NULL;
1551 execvp(argv0, argv);
1552 fatal("execvp");
1555 static void
1556 start_listener(char *argv0, const char *confpath, int daemonize, int verbosity)
1558 struct gotd_child_proc *proc;
1560 proc = calloc(1, sizeof(*proc));
1561 if (proc == NULL)
1562 fatal("calloc");
1564 TAILQ_INSERT_HEAD(&procs, proc, entry);
1566 /* proc->tmo is initialized in main() after event_init() */
1568 proc->type = PROC_LISTEN;
1570 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1571 PF_UNSPEC, proc->pipe) == -1)
1572 fatal("socketpair");
1574 proc->pid = start_child(proc->type, NULL, argv0, confpath,
1575 proc->pipe[1], daemonize, verbosity);
1576 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1577 proc->iev.handler = gotd_dispatch_listener;
1578 proc->iev.events = EV_READ;
1579 proc->iev.handler_arg = NULL;
1581 gotd.listen_proc = proc;
1584 static const struct got_error *
1585 start_session_child(struct gotd_client *client, struct gotd_repo *repo,
1586 char *argv0, const char *confpath, int daemonize, int verbosity)
1588 struct gotd_child_proc *proc;
1590 proc = calloc(1, sizeof(*proc));
1591 if (proc == NULL)
1592 return got_error_from_errno("calloc");
1594 TAILQ_INSERT_HEAD(&procs, proc, entry);
1595 evtimer_set(&proc->tmo, kill_proc_timeout, proc);
1597 if (client_is_reading(client))
1598 proc->type = PROC_SESSION_READ;
1599 else
1600 proc->type = PROC_SESSION_WRITE;
1601 if (strlcpy(proc->repo_name, repo->name,
1602 sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
1603 fatalx("repository name too long: %s", repo->name);
1604 log_debug("starting client uid %d session for repository %s",
1605 client->euid, repo->name);
1606 if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >=
1607 sizeof(proc->repo_path))
1608 fatalx("repository path too long: %s", repo->path);
1609 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1610 PF_UNSPEC, proc->pipe) == -1)
1611 fatal("socketpair");
1612 proc->pid = start_child(proc->type, proc->repo_path, argv0,
1613 confpath, proc->pipe[1], daemonize, verbosity);
1614 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1615 log_debug("proc %s %s is on fd %d",
1616 gotd_proc_names[proc->type], proc->repo_path,
1617 proc->pipe[0]);
1618 proc->iev.handler = gotd_dispatch_client_session;
1619 proc->iev.events = EV_READ;
1620 proc->iev.handler_arg = NULL;
1621 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1622 gotd_dispatch_client_session, &proc->iev);
1623 gotd_imsg_event_add(&proc->iev);
1625 client->session = proc;
1626 return NULL;
1629 static const struct got_error *
1630 start_repo_child(struct gotd_client *client, enum gotd_procid proc_type,
1631 struct gotd_repo *repo, char *argv0, const char *confpath,
1632 int daemonize, int verbosity)
1634 struct gotd_child_proc *proc;
1636 if (proc_type != PROC_REPO_READ && proc_type != PROC_REPO_WRITE)
1637 return got_error_msg(GOT_ERR_NOT_IMPL, "bad process type");
1639 proc = calloc(1, sizeof(*proc));
1640 if (proc == NULL)
1641 return got_error_from_errno("calloc");
1643 TAILQ_INSERT_HEAD(&procs, proc, entry);
1644 evtimer_set(&proc->tmo, kill_proc_timeout, proc);
1646 proc->type = proc_type;
1647 if (strlcpy(proc->repo_name, repo->name,
1648 sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
1649 fatalx("repository name too long: %s", repo->name);
1650 log_debug("starting %s for repository %s",
1651 proc->type == PROC_REPO_READ ? "reader" : "writer", repo->name);
1652 if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >=
1653 sizeof(proc->repo_path))
1654 fatalx("repository path too long: %s", repo->path);
1655 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1656 PF_UNSPEC, proc->pipe) == -1)
1657 fatal("socketpair");
1658 proc->pid = start_child(proc->type, proc->repo_path, argv0,
1659 confpath, proc->pipe[1], daemonize, verbosity);
1660 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1661 log_debug("proc %s %s is on fd %d",
1662 gotd_proc_names[proc->type], proc->repo_path,
1663 proc->pipe[0]);
1664 proc->iev.handler = gotd_dispatch_repo_child;
1665 proc->iev.events = EV_READ;
1666 proc->iev.handler_arg = NULL;
1667 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1668 gotd_dispatch_repo_child, &proc->iev);
1669 gotd_imsg_event_add(&proc->iev);
1671 client->repo = proc;
1672 return NULL;
1675 static const struct got_error *
1676 start_auth_child(struct gotd_client *client, int required_auth,
1677 struct gotd_repo *repo, char *argv0, const char *confpath,
1678 int daemonize, int verbosity)
1680 const struct got_error *err = NULL;
1681 struct gotd_child_proc *proc;
1682 struct gotd_imsg_auth iauth;
1683 int fd;
1685 memset(&iauth, 0, sizeof(iauth));
1687 fd = dup(client->fd);
1688 if (fd == -1)
1689 return got_error_from_errno("dup");
1691 proc = calloc(1, sizeof(*proc));
1692 if (proc == NULL) {
1693 err = got_error_from_errno("calloc");
1694 close(fd);
1695 return err;
1698 TAILQ_INSERT_HEAD(&procs, proc, entry);
1699 evtimer_set(&proc->tmo, kill_proc_timeout, proc);
1701 proc->type = PROC_AUTH;
1702 if (strlcpy(proc->repo_name, repo->name,
1703 sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
1704 fatalx("repository name too long: %s", repo->name);
1705 log_debug("starting auth for uid %d repository %s",
1706 client->euid, repo->name);
1707 if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >=
1708 sizeof(proc->repo_path))
1709 fatalx("repository path too long: %s", repo->path);
1710 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1711 PF_UNSPEC, proc->pipe) == -1)
1712 fatal("socketpair");
1713 proc->pid = start_child(proc->type, proc->repo_path, argv0,
1714 confpath, proc->pipe[1], daemonize, verbosity);
1715 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1716 log_debug("proc %s %s is on fd %d",
1717 gotd_proc_names[proc->type], proc->repo_path,
1718 proc->pipe[0]);
1719 proc->iev.handler = gotd_dispatch_auth_child;
1720 proc->iev.events = EV_READ;
1721 proc->iev.handler_arg = NULL;
1722 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1723 gotd_dispatch_auth_child, &proc->iev);
1724 gotd_imsg_event_add(&proc->iev);
1726 iauth.euid = client->euid;
1727 iauth.egid = client->egid;
1728 iauth.required_auth = required_auth;
1729 iauth.client_id = client->id;
1730 if (gotd_imsg_compose_event(&proc->iev, GOTD_IMSG_AUTHENTICATE,
1731 PROC_GOTD, fd, &iauth, sizeof(iauth)) == -1) {
1732 log_warn("imsg compose AUTHENTICATE");
1733 close(fd);
1734 /* Let the auth_timeout handler tidy up. */
1737 client->auth = proc;
1738 client->required_auth = required_auth;
1739 return NULL;
1742 static void
1743 apply_unveil_repo_readonly(const char *repo_path, int need_tmpdir)
1745 if (need_tmpdir) {
1746 if (unveil(GOT_TMPDIR_STR, "rwc") == -1)
1747 fatal("unveil %s", GOT_TMPDIR_STR);
1750 if (unveil(repo_path, "r") == -1)
1751 fatal("unveil %s", repo_path);
1753 if (unveil(NULL, NULL) == -1)
1754 fatal("unveil");
1757 static void
1758 apply_unveil_repo_readwrite(const char *repo_path)
1760 if (unveil(repo_path, "rwc") == -1)
1761 fatal("unveil %s", repo_path);
1763 if (unveil(GOT_TMPDIR_STR, "rwc") == -1)
1764 fatal("unveil %s", GOT_TMPDIR_STR);
1766 if (unveil(NULL, NULL) == -1)
1767 fatal("unveil");
1770 static void
1771 apply_unveil_none(void)
1773 if (unveil("/", "") == -1)
1774 fatal("unveil");
1776 if (unveil(NULL, NULL) == -1)
1777 fatal("unveil");
1780 static void
1781 apply_unveil_selfexec(void)
1783 if (unveil(gotd.argv0, "x") == -1)
1784 fatal("unveil %s", gotd.argv0);
1786 if (unveil(NULL, NULL) == -1)
1787 fatal("unveil");
1790 int
1791 main(int argc, char **argv)
1793 const struct got_error *error = NULL;
1794 int ch, fd = -1, daemonize = 1, verbosity = 0, noaction = 0;
1795 const char *confpath = GOTD_CONF_PATH;
1796 char *argv0 = argv[0];
1797 char title[2048];
1798 struct passwd *pw = NULL;
1799 char *repo_path = NULL;
1800 enum gotd_procid proc_id = PROC_GOTD;
1801 struct event evsigint, evsigterm, evsighup, evsigusr1, evsigchld;
1802 int *pack_fds = NULL, *temp_fds = NULL;
1803 struct gotd_repo *repo = NULL;
1805 TAILQ_INIT(&procs);
1807 log_init(1, LOG_DAEMON); /* Log to stderr until daemonized. */
1809 while ((ch = getopt(argc, argv, "Adf:LnP:RsSvW")) != -1) {
1810 switch (ch) {
1811 case 'A':
1812 proc_id = PROC_AUTH;
1813 break;
1814 case 'd':
1815 daemonize = 0;
1816 break;
1817 case 'f':
1818 confpath = optarg;
1819 break;
1820 case 'L':
1821 proc_id = PROC_LISTEN;
1822 break;
1823 case 'n':
1824 noaction = 1;
1825 break;
1826 case 'P':
1827 repo_path = realpath(optarg, NULL);
1828 if (repo_path == NULL)
1829 fatal("realpath '%s'", optarg);
1830 break;
1831 case 'R':
1832 proc_id = PROC_REPO_READ;
1833 break;
1834 case 's':
1835 proc_id = PROC_SESSION_READ;
1836 break;
1837 case 'S':
1838 proc_id = PROC_SESSION_WRITE;
1839 break;
1840 case 'v':
1841 if (verbosity < 3)
1842 verbosity++;
1843 break;
1844 case 'W':
1845 proc_id = PROC_REPO_WRITE;
1846 break;
1847 default:
1848 usage();
1852 argc -= optind;
1853 argv += optind;
1855 if (argc != 0)
1856 usage();
1858 if (geteuid() && (proc_id == PROC_GOTD || proc_id == PROC_LISTEN))
1859 fatalx("need root privileges");
1861 if (parse_config(confpath, proc_id, &gotd, 1) != 0)
1862 return 1;
1864 pw = getpwnam(gotd.user_name);
1865 if (pw == NULL)
1866 fatalx("user %s not found", gotd.user_name);
1868 if (pw->pw_uid == 0)
1869 fatalx("cannot run %s as the superuser", getprogname());
1871 if (noaction) {
1872 fprintf(stderr, "configuration OK\n");
1873 return 0;
1876 gotd.argv0 = argv0;
1877 gotd.daemonize = daemonize;
1878 gotd.verbosity = verbosity;
1879 gotd.confpath = confpath;
1881 /* Require an absolute path in argv[0] for reliable re-exec. */
1882 if (!got_path_is_absolute(argv0))
1883 fatalx("bad path \"%s\": must be an absolute path", argv0);
1885 log_init(daemonize ? 0 : 1, LOG_DAEMON);
1886 log_setverbose(verbosity);
1888 if (proc_id == PROC_GOTD) {
1889 snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
1890 arc4random_buf(&clients_hash_key, sizeof(clients_hash_key));
1891 if (daemonize && daemon(1, 0) == -1)
1892 fatal("daemon");
1893 gotd.pid = getpid();
1894 start_listener(argv0, confpath, daemonize, verbosity);
1895 } else if (proc_id == PROC_LISTEN) {
1896 snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
1897 if (verbosity) {
1898 log_info("socket: %s", gotd.unix_socket_path);
1899 log_info("user: %s", pw->pw_name);
1902 fd = unix_socket_listen(gotd.unix_socket_path, pw->pw_uid,
1903 pw->pw_gid);
1904 if (fd == -1) {
1905 fatal("cannot listen on unix socket %s",
1906 gotd.unix_socket_path);
1908 } else if (proc_id == PROC_AUTH) {
1909 snprintf(title, sizeof(title), "%s %s",
1910 gotd_proc_names[proc_id], repo_path);
1911 } else if (proc_id == PROC_REPO_READ || proc_id == PROC_REPO_WRITE ||
1912 proc_id == PROC_SESSION_READ || proc_id == PROC_SESSION_WRITE) {
1913 error = got_repo_pack_fds_open(&pack_fds);
1914 if (error != NULL)
1915 fatalx("cannot open pack tempfiles: %s", error->msg);
1916 error = got_repo_temp_fds_open(&temp_fds);
1917 if (error != NULL)
1918 fatalx("cannot open pack tempfiles: %s", error->msg);
1919 if (repo_path == NULL)
1920 fatalx("repository path not specified");
1921 snprintf(title, sizeof(title), "%s %s",
1922 gotd_proc_names[proc_id], repo_path);
1923 } else
1924 fatal("invalid process id %d", proc_id);
1926 setproctitle("%s", title);
1927 log_procinit(title);
1929 /* Drop root privileges. */
1930 if (setgid(pw->pw_gid) == -1)
1931 fatal("setgid %d failed", pw->pw_gid);
1932 if (setuid(pw->pw_uid) == -1)
1933 fatal("setuid %d failed", pw->pw_uid);
1935 event_init();
1937 switch (proc_id) {
1938 case PROC_GOTD:
1939 #ifndef PROFILE
1940 /* "exec" promise will be limited to argv[0] via unveil(2). */
1941 if (pledge("stdio proc exec sendfd recvfd unveil", NULL) == -1)
1942 err(1, "pledge");
1943 #endif
1944 break;
1945 case PROC_LISTEN:
1946 #ifndef PROFILE
1947 if (pledge("stdio sendfd unix unveil", NULL) == -1)
1948 err(1, "pledge");
1949 #endif
1951 * Ensure that AF_UNIX bind(2) cannot be used with any other
1952 * sockets by revoking all filesystem access via unveil(2).
1954 apply_unveil_none();
1956 listen_main(title, fd, gotd.connection_limits,
1957 gotd.nconnection_limits);
1958 /* NOTREACHED */
1959 break;
1960 case PROC_AUTH:
1961 #ifndef PROFILE
1962 if (pledge("stdio getpw recvfd unix unveil", NULL) == -1)
1963 err(1, "pledge");
1964 #endif
1966 * We need the "unix" pledge promise for getpeername(2) only.
1967 * Ensure that AF_UNIX bind(2) cannot be used by revoking all
1968 * filesystem access via unveil(2). Access to password database
1969 * files will still work since "getpw" bypasses unveil(2).
1971 apply_unveil_none();
1973 auth_main(title, &gotd.repos, repo_path);
1974 /* NOTREACHED */
1975 break;
1976 case PROC_SESSION_READ:
1977 case PROC_SESSION_WRITE:
1978 #ifndef PROFILE
1980 * The "recvfd" promise is only needed during setup and
1981 * will be removed in a later pledge(2) call.
1983 if (pledge("stdio rpath wpath cpath recvfd sendfd fattr flock "
1984 "unveil", NULL) == -1)
1985 err(1, "pledge");
1986 #endif
1987 if (proc_id == PROC_SESSION_READ)
1988 apply_unveil_repo_readonly(repo_path, 1);
1989 else
1990 apply_unveil_repo_readwrite(repo_path);
1991 session_main(title, repo_path, pack_fds, temp_fds,
1992 &gotd.request_timeout, proc_id);
1993 /* NOTREACHED */
1994 break;
1995 case PROC_REPO_READ:
1996 #ifndef PROFILE
1997 if (pledge("stdio rpath recvfd unveil", NULL) == -1)
1998 err(1, "pledge");
1999 #endif
2000 apply_unveil_repo_readonly(repo_path, 0);
2001 repo_read_main(title, repo_path, pack_fds, temp_fds);
2002 /* NOTREACHED */
2003 exit(0);
2004 case PROC_REPO_WRITE:
2005 #ifndef PROFILE
2006 if (pledge("stdio rpath recvfd unveil", NULL) == -1)
2007 err(1, "pledge");
2008 #endif
2009 apply_unveil_repo_readonly(repo_path, 0);
2010 repo = gotd_find_repo_by_path(repo_path, &gotd);
2011 if (repo == NULL)
2012 fatalx("no repository for path %s", repo_path);
2013 repo_write_main(title, repo_path, pack_fds, temp_fds,
2014 &repo->protected_tag_namespaces,
2015 &repo->protected_branch_namespaces,
2016 &repo->protected_branches);
2017 /* NOTREACHED */
2018 exit(0);
2019 default:
2020 fatal("invalid process id %d", proc_id);
2023 if (proc_id != PROC_GOTD)
2024 fatal("invalid process id %d", proc_id);
2026 evtimer_set(&gotd.listen_proc->tmo, kill_proc_timeout,
2027 gotd.listen_proc);
2029 apply_unveil_selfexec();
2031 signal_set(&evsigint, SIGINT, gotd_sighdlr, NULL);
2032 signal_set(&evsigterm, SIGTERM, gotd_sighdlr, NULL);
2033 signal_set(&evsighup, SIGHUP, gotd_sighdlr, NULL);
2034 signal_set(&evsigusr1, SIGUSR1, gotd_sighdlr, NULL);
2035 signal_set(&evsigchld, SIGCHLD, gotd_sighdlr, NULL);
2036 signal(SIGPIPE, SIG_IGN);
2038 signal_add(&evsigint, NULL);
2039 signal_add(&evsigterm, NULL);
2040 signal_add(&evsighup, NULL);
2041 signal_add(&evsigusr1, NULL);
2042 signal_add(&evsigchld, NULL);
2044 gotd_imsg_event_add(&gotd.listen_proc->iev);
2046 event_dispatch();
2048 free(repo_path);
2049 gotd_shutdown();
2051 return 0;