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 success/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 "gitwrapper"
734 };
736 static void
737 kill_proc(struct gotd_child_proc *proc, int fatal)
739 struct timeval tv = { 5, 0 };
741 log_debug("kill -%d %d", fatal ? SIGKILL : SIGTERM, proc->pid);
743 if (proc->iev.ibuf.fd != -1) {
744 event_del(&proc->iev.ev);
745 msgbuf_clear(&proc->iev.ibuf.w);
746 close(proc->iev.ibuf.fd);
747 proc->iev.ibuf.fd = -1;
750 if (!evtimer_pending(&proc->tmo, NULL) && !fatal)
751 evtimer_add(&proc->tmo, &tv);
753 if (fatal) {
754 log_warnx("sending SIGKILL to PID %d", proc->pid);
755 kill(proc->pid, SIGKILL);
756 } else
757 kill(proc->pid, SIGTERM);
760 static void
761 kill_proc_timeout(int fd, short ev, void *d)
763 struct gotd_child_proc *proc = d;
765 log_warnx("timeout waiting for PID %d to terminate;"
766 " retrying with force", proc->pid);
767 kill_proc(proc, 1);
770 static void
771 gotd_shutdown(void)
773 uint64_t slot;
775 log_debug("shutting down");
776 for (slot = 0; slot < nitems(gotd_clients); slot++) {
777 struct gotd_client *c, *tmp;
779 STAILQ_FOREACH_SAFE(c, &gotd_clients[slot], entry, tmp)
780 disconnect(c);
783 kill_proc(gotd.listen_proc, 0);
785 log_info("terminating");
786 exit(0);
789 static struct gotd_child_proc *
790 find_proc_by_pid(pid_t pid)
792 struct gotd_child_proc *proc = NULL;
794 TAILQ_FOREACH(proc, &procs, entry)
795 if (proc->pid == pid)
796 break;
798 return proc;
801 void
802 gotd_sighdlr(int sig, short event, void *arg)
804 struct gotd_child_proc *proc;
805 pid_t pid;
806 int status;
808 /*
809 * Normal signal handler rules don't apply because libevent
810 * decouples for us.
811 */
813 switch (sig) {
814 case SIGHUP:
815 log_info("%s: ignoring SIGHUP", __func__);
816 break;
817 case SIGUSR1:
818 log_info("%s: ignoring SIGUSR1", __func__);
819 break;
820 case SIGTERM:
821 case SIGINT:
822 gotd_shutdown();
823 break;
824 case SIGCHLD:
825 for (;;) {
826 pid = waitpid(WAIT_ANY, &status, WNOHANG);
827 if (pid == -1) {
828 if (errno == EINTR)
829 continue;
830 if (errno == ECHILD)
831 break;
832 fatal("waitpid");
834 if (pid == 0)
835 break;
837 log_debug("reaped pid %d", pid);
838 proc = find_proc_by_pid(pid);
839 if (proc == NULL) {
840 log_info("caught exit of unknown child %d",
841 pid);
842 continue;
845 if (WIFSIGNALED(status)) {
846 log_warnx("child PID %d terminated with"
847 " signal %d", pid, WTERMSIG(status));
850 proc_done(proc);
852 break;
853 default:
854 fatalx("unexpected signal");
858 static const struct got_error *
859 ensure_proc_is_reading(struct gotd_client *client,
860 struct gotd_child_proc *proc)
862 if (!client_is_reading(client)) {
863 kill_proc(proc, 1);
864 return got_error_fmt(GOT_ERR_BAD_PACKET,
865 "PID %d handled a read-request for uid %d but this "
866 "user is not reading from a repository", proc->pid,
867 client->euid);
870 return NULL;
873 static const struct got_error *
874 ensure_proc_is_writing(struct gotd_client *client,
875 struct gotd_child_proc *proc)
877 if (!client_is_writing(client)) {
878 kill_proc(proc, 1);
879 return got_error_fmt(GOT_ERR_BAD_PACKET,
880 "PID %d handled a write-request for uid %d but this "
881 "user is not writing to a repository", proc->pid,
882 client->euid);
885 return NULL;
888 static int
889 verify_imsg_src(struct gotd_client *client, struct gotd_child_proc *proc,
890 struct imsg *imsg)
892 const struct got_error *err;
893 int ret = 0;
895 if (proc->type == PROC_REPO_READ || proc->type == PROC_REPO_WRITE) {
896 if (client->repo == NULL)
897 fatalx("no process found for uid %d", client->euid);
898 if (proc->pid != client->repo->pid) {
899 kill_proc(proc, 1);
900 log_warnx("received message from PID %d for uid %d, "
901 "while PID %d is the process serving this user",
902 proc->pid, client->euid, client->repo->pid);
903 return 0;
906 if (proc->type == PROC_SESSION_READ ||
907 proc->type == PROC_SESSION_WRITE) {
908 if (client->session == NULL) {
909 log_warnx("no session found for uid %d", client->euid);
910 return 0;
912 if (proc->pid != client->session->pid) {
913 kill_proc(proc, 1);
914 log_warnx("received message from PID %d for uid %d, "
915 "while PID %d is the process serving this user",
916 proc->pid, client->euid, client->session->pid);
917 return 0;
921 switch (imsg->hdr.type) {
922 case GOTD_IMSG_ERROR:
923 ret = 1;
924 break;
925 case GOTD_IMSG_CONNECT:
926 if (proc->type != PROC_LISTEN) {
927 err = got_error_fmt(GOT_ERR_BAD_PACKET,
928 "new connection for uid %d from PID %d "
929 "which is not the listen process",
930 proc->pid, client->euid);
931 } else
932 ret = 1;
933 break;
934 case GOTD_IMSG_ACCESS_GRANTED:
935 if (proc->type != PROC_AUTH) {
936 err = got_error_fmt(GOT_ERR_BAD_PACKET,
937 "authentication of uid %d from PID %d "
938 "which is not the auth process",
939 proc->pid, client->euid);
940 } else
941 ret = 1;
942 break;
943 case GOTD_IMSG_CLIENT_SESSION_READY:
944 if (proc->type != PROC_SESSION_READ &&
945 proc->type != PROC_SESSION_WRITE) {
946 err = got_error_fmt(GOT_ERR_BAD_PACKET,
947 "unexpected \"ready\" signal from PID %d",
948 proc->pid);
949 } else
950 ret = 1;
951 break;
952 case GOTD_IMSG_REPO_CHILD_READY:
953 if (proc->type != PROC_REPO_READ &&
954 proc->type != PROC_REPO_WRITE) {
955 err = got_error_fmt(GOT_ERR_BAD_PACKET,
956 "unexpected \"ready\" signal from PID %d",
957 proc->pid);
958 } else
959 ret = 1;
960 break;
961 case GOTD_IMSG_PACKFILE_DONE:
962 err = ensure_proc_is_reading(client, proc);
963 if (err)
964 log_warnx("uid %d: %s", client->euid, err->msg);
965 else
966 ret = 1;
967 break;
968 case GOTD_IMSG_PACKFILE_INSTALL:
969 case GOTD_IMSG_REF_UPDATES_START:
970 case GOTD_IMSG_REF_UPDATE:
971 err = ensure_proc_is_writing(client, proc);
972 if (err)
973 log_warnx("uid %d: %s", client->euid, err->msg);
974 else
975 ret = 1;
976 break;
977 default:
978 log_debug("%s: unexpected imsg %d", __func__, imsg->hdr.type);
979 break;
982 return ret;
985 static const struct got_error *
986 connect_repo_child(struct gotd_client *client,
987 struct gotd_child_proc *repo_proc)
989 static const struct got_error *err;
990 struct gotd_imsgev *session_iev = &client->session->iev;
991 struct gotd_imsg_connect_repo_child ireq;
992 int pipe[2];
994 if (client->state != GOTD_CLIENT_STATE_ACCESS_GRANTED)
995 return got_error_msg(GOT_ERR_BAD_REQUEST,
996 "unexpected repo child ready signal received");
998 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
999 PF_UNSPEC, pipe) == -1)
1000 fatal("socketpair");
1002 memset(&ireq, 0, sizeof(ireq));
1003 ireq.client_id = client->id;
1004 ireq.proc_id = repo_proc->type;
1006 /* Pass repo child pipe to session child process. */
1007 if (gotd_imsg_compose_event(session_iev, GOTD_IMSG_CONNECT_REPO_CHILD,
1008 PROC_GOTD, pipe[0], &ireq, sizeof(ireq)) == -1) {
1009 err = got_error_from_errno("imsg compose CONNECT_REPO_CHILD");
1010 close(pipe[0]);
1011 close(pipe[1]);
1012 return err;
1015 /* Pass session child pipe to repo child process. */
1016 if (gotd_imsg_compose_event(&repo_proc->iev,
1017 GOTD_IMSG_CONNECT_REPO_CHILD, PROC_GOTD, pipe[1], NULL, 0) == -1) {
1018 err = got_error_from_errno("imsg compose CONNECT_REPO_CHILD");
1019 close(pipe[1]);
1020 return err;
1023 return NULL;
1026 static void
1027 gotd_dispatch_listener(int fd, short event, void *arg)
1029 struct gotd_imsgev *iev = arg;
1030 struct imsgbuf *ibuf = &iev->ibuf;
1031 struct gotd_child_proc *proc = gotd.listen_proc;
1032 ssize_t n;
1033 int shut = 0;
1034 struct imsg imsg;
1036 if (proc->iev.ibuf.fd != fd)
1037 fatalx("%s: unexpected fd %d", __func__, fd);
1039 if (event & EV_READ) {
1040 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1041 fatal("imsg_read error");
1042 if (n == 0) {
1043 /* Connection closed. */
1044 shut = 1;
1045 goto done;
1049 if (event & EV_WRITE) {
1050 n = msgbuf_write(&ibuf->w);
1051 if (n == -1 && errno != EAGAIN)
1052 fatal("msgbuf_write");
1053 if (n == 0) {
1054 /* Connection closed. */
1055 shut = 1;
1056 goto done;
1060 for (;;) {
1061 const struct got_error *err = NULL;
1062 struct gotd_client *client = NULL;
1063 uint32_t client_id = 0;
1064 int do_disconnect = 0;
1066 if ((n = imsg_get(ibuf, &imsg)) == -1)
1067 fatal("%s: imsg_get error", __func__);
1068 if (n == 0) /* No more messages. */
1069 break;
1071 switch (imsg.hdr.type) {
1072 case GOTD_IMSG_ERROR:
1073 do_disconnect = 1;
1074 err = gotd_imsg_recv_error(&client_id, &imsg);
1075 break;
1076 case GOTD_IMSG_CONNECT:
1077 err = recv_connect(&client_id, &imsg);
1078 break;
1079 default:
1080 log_debug("unexpected imsg %d", imsg.hdr.type);
1081 break;
1084 client = find_client(client_id);
1085 if (client == NULL) {
1086 log_warnx("%s: client not found", __func__);
1087 imsg_free(&imsg);
1088 continue;
1091 if (err)
1092 log_warnx("uid %d: %s", client->euid, err->msg);
1094 if (do_disconnect) {
1095 if (err)
1096 disconnect_on_error(client, err);
1097 else
1098 disconnect(client);
1101 imsg_free(&imsg);
1103 done:
1104 if (!shut) {
1105 gotd_imsg_event_add(iev);
1106 } else {
1107 /* This pipe is dead. Remove its event handler */
1108 event_del(&iev->ev);
1109 event_loopexit(NULL);
1113 static void
1114 gotd_dispatch_auth_child(int fd, short event, void *arg)
1116 const struct got_error *err = NULL;
1117 struct gotd_imsgev *iev = arg;
1118 struct imsgbuf *ibuf = &iev->ibuf;
1119 struct gotd_client *client;
1120 struct gotd_repo *repo = NULL;
1121 ssize_t n;
1122 int shut = 0;
1123 struct imsg imsg;
1124 uint32_t client_id = 0;
1125 int do_disconnect = 0;
1127 client = find_client_by_proc_fd(fd);
1128 if (client == NULL) {
1129 /* Can happen during process teardown. */
1130 warnx("cannot find client for fd %d", fd);
1131 shut = 1;
1132 goto done;
1135 if (client->auth == NULL)
1136 fatalx("cannot find auth child process for fd %d", fd);
1138 if (event & EV_READ) {
1139 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1140 fatal("imsg_read error");
1141 if (n == 0) {
1142 /* Connection closed. */
1143 shut = 1;
1144 goto done;
1148 if (event & EV_WRITE) {
1149 n = msgbuf_write(&ibuf->w);
1150 if (n == -1 && errno != EAGAIN)
1151 fatal("msgbuf_write");
1152 if (n == 0) {
1153 /* Connection closed. */
1154 shut = 1;
1156 goto done;
1159 if (client->auth->iev.ibuf.fd != fd)
1160 fatalx("%s: unexpected fd %d", __func__, fd);
1162 if ((n = imsg_get(ibuf, &imsg)) == -1)
1163 fatal("%s: imsg_get error", __func__);
1164 if (n == 0) /* No more messages. */
1165 return;
1167 evtimer_del(&client->tmo);
1169 switch (imsg.hdr.type) {
1170 case GOTD_IMSG_ERROR:
1171 do_disconnect = 1;
1172 err = gotd_imsg_recv_error(&client_id, &imsg);
1173 break;
1174 case GOTD_IMSG_ACCESS_GRANTED:
1175 client->state = GOTD_CLIENT_STATE_ACCESS_GRANTED;
1176 break;
1177 default:
1178 do_disconnect = 1;
1179 log_debug("unexpected imsg %d", imsg.hdr.type);
1180 break;
1183 if (!verify_imsg_src(client, client->auth, &imsg)) {
1184 do_disconnect = 1;
1185 log_debug("dropping imsg type %d from PID %d",
1186 imsg.hdr.type, client->auth->pid);
1188 imsg_free(&imsg);
1190 if (do_disconnect) {
1191 if (err)
1192 disconnect_on_error(client, err);
1193 else
1194 disconnect(client);
1195 return;
1198 repo = gotd_find_repo_by_name(client->auth->repo_name, &gotd);
1199 if (repo == NULL) {
1200 err = got_error(GOT_ERR_NOT_GIT_REPO);
1201 goto done;
1203 kill_auth_proc(client);
1205 log_info("authenticated uid %d for repository %s",
1206 client->euid, repo->name);
1208 err = start_session_child(client, repo, gotd.argv0,
1209 gotd.confpath, gotd.daemonize, gotd.verbosity);
1210 if (err)
1211 goto done;
1212 done:
1213 if (err)
1214 log_warnx("uid %d: %s", client->euid, err->msg);
1216 /* We might have killed the auth process by now. */
1217 if (client->auth != NULL) {
1218 if (!shut) {
1219 gotd_imsg_event_add(iev);
1220 } else {
1221 /* This pipe is dead. Remove its event handler */
1222 event_del(&iev->ev);
1227 static const struct got_error *
1228 connect_session(struct gotd_client *client)
1230 const struct got_error *err = NULL;
1231 struct gotd_imsg_connect iconnect;
1232 int s;
1234 memset(&iconnect, 0, sizeof(iconnect));
1236 s = dup(client->fd);
1237 if (s == -1)
1238 return got_error_from_errno("dup");
1240 iconnect.client_id = client->id;
1241 iconnect.euid = client->euid;
1242 iconnect.egid = client->egid;
1244 if (gotd_imsg_compose_event(&client->session->iev, GOTD_IMSG_CONNECT,
1245 PROC_GOTD, s, &iconnect, sizeof(iconnect)) == -1) {
1246 err = got_error_from_errno("imsg compose CONNECT");
1247 close(s);
1248 return err;
1252 * We are no longer interested in messages from this client.
1253 * Further client requests will be handled by the session process.
1255 msgbuf_clear(&client->iev.ibuf.w);
1256 imsg_clear(&client->iev.ibuf);
1257 event_del(&client->iev.ev);
1258 client->fd = -1; /* will be closed via copy in client->iev.ibuf.fd */
1260 return NULL;
1263 static void
1264 gotd_dispatch_client_session(int fd, short event, void *arg)
1266 struct gotd_imsgev *iev = arg;
1267 struct imsgbuf *ibuf = &iev->ibuf;
1268 struct gotd_child_proc *proc = NULL;
1269 struct gotd_client *client = NULL;
1270 ssize_t n;
1271 int shut = 0;
1272 struct imsg imsg;
1274 client = find_client_by_proc_fd(fd);
1275 if (client == NULL) {
1276 /* Can happen during process teardown. */
1277 warnx("cannot find client for fd %d", fd);
1278 shut = 1;
1279 goto done;
1282 if (event & EV_READ) {
1283 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1284 fatal("imsg_read error");
1285 if (n == 0) {
1286 /* Connection closed. */
1287 shut = 1;
1288 goto done;
1292 if (event & EV_WRITE) {
1293 n = msgbuf_write(&ibuf->w);
1294 if (n == -1 && errno != EAGAIN)
1295 fatal("msgbuf_write");
1296 if (n == 0) {
1297 /* Connection closed. */
1298 shut = 1;
1299 goto done;
1303 proc = client->session;
1304 if (proc == NULL)
1305 fatalx("cannot find session child process for fd %d", fd);
1307 for (;;) {
1308 const struct got_error *err = NULL;
1309 uint32_t client_id = 0;
1310 int do_disconnect = 0, do_start_repo_child = 0;
1312 if ((n = imsg_get(ibuf, &imsg)) == -1)
1313 fatal("%s: imsg_get error", __func__);
1314 if (n == 0) /* No more messages. */
1315 break;
1317 switch (imsg.hdr.type) {
1318 case GOTD_IMSG_ERROR:
1319 do_disconnect = 1;
1320 err = gotd_imsg_recv_error(&client_id, &imsg);
1321 break;
1322 case GOTD_IMSG_CLIENT_SESSION_READY:
1323 if (client->state != GOTD_CLIENT_STATE_ACCESS_GRANTED) {
1324 err = got_error(GOT_ERR_PRIVSEP_MSG);
1325 break;
1327 do_start_repo_child = 1;
1328 break;
1329 case GOTD_IMSG_DISCONNECT:
1330 do_disconnect = 1;
1331 break;
1332 default:
1333 log_debug("unexpected imsg %d", imsg.hdr.type);
1334 break;
1337 if (!verify_imsg_src(client, proc, &imsg)) {
1338 log_debug("dropping imsg type %d from PID %d",
1339 imsg.hdr.type, proc->pid);
1340 imsg_free(&imsg);
1341 continue;
1343 if (err)
1344 log_warnx("uid %d: %s", client->euid, err->msg);
1346 if (do_start_repo_child) {
1347 struct gotd_repo *repo;
1348 const char *name = client->session->repo_name;
1350 repo = gotd_find_repo_by_name(name, &gotd);
1351 if (repo != NULL) {
1352 enum gotd_procid proc_type;
1354 if (client->required_auth & GOTD_AUTH_WRITE)
1355 proc_type = PROC_REPO_WRITE;
1356 else
1357 proc_type = PROC_REPO_READ;
1359 err = start_repo_child(client, proc_type, repo,
1360 gotd.argv0, gotd.confpath, gotd.daemonize,
1361 gotd.verbosity);
1362 } else
1363 err = got_error(GOT_ERR_NOT_GIT_REPO);
1365 if (err) {
1366 log_warnx("uid %d: %s", client->euid, err->msg);
1367 do_disconnect = 1;
1371 if (do_disconnect) {
1372 if (err)
1373 disconnect_on_error(client, err);
1374 else
1375 disconnect(client);
1378 imsg_free(&imsg);
1380 done:
1381 if (!shut) {
1382 gotd_imsg_event_add(iev);
1383 } else {
1384 /* This pipe is dead. Remove its event handler */
1385 event_del(&iev->ev);
1386 disconnect(client);
1390 static void
1391 gotd_dispatch_repo_child(int fd, short event, void *arg)
1393 struct gotd_imsgev *iev = arg;
1394 struct imsgbuf *ibuf = &iev->ibuf;
1395 struct gotd_child_proc *proc = NULL;
1396 struct gotd_client *client;
1397 ssize_t n;
1398 int shut = 0;
1399 struct imsg imsg;
1401 client = find_client_by_proc_fd(fd);
1402 if (client == NULL) {
1403 /* Can happen during process teardown. */
1404 warnx("cannot find client for fd %d", fd);
1405 shut = 1;
1406 goto done;
1409 if (event & EV_READ) {
1410 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1411 fatal("imsg_read error");
1412 if (n == 0) {
1413 /* Connection closed. */
1414 shut = 1;
1415 goto done;
1419 if (event & EV_WRITE) {
1420 n = msgbuf_write(&ibuf->w);
1421 if (n == -1 && errno != EAGAIN)
1422 fatal("msgbuf_write");
1423 if (n == 0) {
1424 /* Connection closed. */
1425 shut = 1;
1426 goto done;
1430 proc = client->repo;
1431 if (proc == NULL)
1432 fatalx("cannot find child process for fd %d", fd);
1434 for (;;) {
1435 const struct got_error *err = NULL;
1436 uint32_t client_id = 0;
1437 int do_disconnect = 0;
1439 if ((n = imsg_get(ibuf, &imsg)) == -1)
1440 fatal("%s: imsg_get error", __func__);
1441 if (n == 0) /* No more messages. */
1442 break;
1444 switch (imsg.hdr.type) {
1445 case GOTD_IMSG_ERROR:
1446 do_disconnect = 1;
1447 err = gotd_imsg_recv_error(&client_id, &imsg);
1448 break;
1449 case GOTD_IMSG_REPO_CHILD_READY:
1450 err = connect_session(client);
1451 if (err)
1452 break;
1453 err = connect_repo_child(client, proc);
1454 break;
1455 default:
1456 log_debug("unexpected imsg %d", imsg.hdr.type);
1457 break;
1460 if (!verify_imsg_src(client, proc, &imsg)) {
1461 log_debug("dropping imsg type %d from PID %d",
1462 imsg.hdr.type, proc->pid);
1463 imsg_free(&imsg);
1464 continue;
1466 if (err)
1467 log_warnx("uid %d: %s", client->euid, err->msg);
1469 if (do_disconnect) {
1470 if (err)
1471 disconnect_on_error(client, err);
1472 else
1473 disconnect(client);
1476 imsg_free(&imsg);
1478 done:
1479 if (!shut) {
1480 gotd_imsg_event_add(iev);
1481 } else {
1482 /* This pipe is dead. Remove its event handler */
1483 event_del(&iev->ev);
1484 disconnect(client);
1488 static pid_t
1489 start_child(enum gotd_procid proc_id, const char *repo_path,
1490 char *argv0, const char *confpath, int fd, int daemonize, int verbosity)
1492 char *argv[11];
1493 int argc = 0;
1494 pid_t pid;
1496 switch (pid = fork()) {
1497 case -1:
1498 fatal("cannot fork");
1499 case 0:
1500 break;
1501 default:
1502 close(fd);
1503 return pid;
1506 if (fd != GOTD_FILENO_MSG_PIPE) {
1507 if (dup2(fd, GOTD_FILENO_MSG_PIPE) == -1)
1508 fatal("cannot setup imsg fd");
1509 } else if (fcntl(fd, F_SETFD, 0) == -1)
1510 fatal("cannot setup imsg fd");
1512 argv[argc++] = argv0;
1513 switch (proc_id) {
1514 case PROC_LISTEN:
1515 argv[argc++] = (char *)"-L";
1516 break;
1517 case PROC_AUTH:
1518 argv[argc++] = (char *)"-A";
1519 break;
1520 case PROC_SESSION_READ:
1521 argv[argc++] = (char *)"-s";
1522 break;
1523 case PROC_SESSION_WRITE:
1524 argv[argc++] = (char *)"-S";
1525 break;
1526 case PROC_REPO_READ:
1527 argv[argc++] = (char *)"-R";
1528 break;
1529 case PROC_REPO_WRITE:
1530 argv[argc++] = (char *)"-W";
1531 break;
1532 default:
1533 fatalx("invalid process id %d", proc_id);
1536 argv[argc++] = (char *)"-f";
1537 argv[argc++] = (char *)confpath;
1539 if (repo_path) {
1540 argv[argc++] = (char *)"-P";
1541 argv[argc++] = (char *)repo_path;
1544 if (!daemonize)
1545 argv[argc++] = (char *)"-d";
1546 if (verbosity > 0)
1547 argv[argc++] = (char *)"-v";
1548 if (verbosity > 1)
1549 argv[argc++] = (char *)"-v";
1550 argv[argc++] = NULL;
1552 execvp(argv0, argv);
1553 fatal("execvp");
1556 static void
1557 start_listener(char *argv0, const char *confpath, int daemonize, int verbosity)
1559 struct gotd_child_proc *proc;
1561 proc = calloc(1, sizeof(*proc));
1562 if (proc == NULL)
1563 fatal("calloc");
1565 TAILQ_INSERT_HEAD(&procs, proc, entry);
1567 /* proc->tmo is initialized in main() after event_init() */
1569 proc->type = PROC_LISTEN;
1571 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1572 PF_UNSPEC, proc->pipe) == -1)
1573 fatal("socketpair");
1575 proc->pid = start_child(proc->type, NULL, argv0, confpath,
1576 proc->pipe[1], daemonize, verbosity);
1577 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1578 proc->iev.handler = gotd_dispatch_listener;
1579 proc->iev.events = EV_READ;
1580 proc->iev.handler_arg = NULL;
1582 gotd.listen_proc = proc;
1585 static const struct got_error *
1586 start_session_child(struct gotd_client *client, struct gotd_repo *repo,
1587 char *argv0, const char *confpath, int daemonize, int verbosity)
1589 struct gotd_child_proc *proc;
1591 proc = calloc(1, sizeof(*proc));
1592 if (proc == NULL)
1593 return got_error_from_errno("calloc");
1595 TAILQ_INSERT_HEAD(&procs, proc, entry);
1596 evtimer_set(&proc->tmo, kill_proc_timeout, proc);
1598 if (client_is_reading(client))
1599 proc->type = PROC_SESSION_READ;
1600 else
1601 proc->type = PROC_SESSION_WRITE;
1602 if (strlcpy(proc->repo_name, repo->name,
1603 sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
1604 fatalx("repository name too long: %s", repo->name);
1605 log_debug("starting client uid %d session for repository %s",
1606 client->euid, repo->name);
1607 if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >=
1608 sizeof(proc->repo_path))
1609 fatalx("repository path too long: %s", repo->path);
1610 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1611 PF_UNSPEC, proc->pipe) == -1)
1612 fatal("socketpair");
1613 proc->pid = start_child(proc->type, proc->repo_path, argv0,
1614 confpath, proc->pipe[1], daemonize, verbosity);
1615 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1616 log_debug("proc %s %s is on fd %d",
1617 gotd_proc_names[proc->type], proc->repo_path,
1618 proc->pipe[0]);
1619 proc->iev.handler = gotd_dispatch_client_session;
1620 proc->iev.events = EV_READ;
1621 proc->iev.handler_arg = NULL;
1622 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1623 gotd_dispatch_client_session, &proc->iev);
1624 gotd_imsg_event_add(&proc->iev);
1626 client->session = proc;
1627 return NULL;
1630 static const struct got_error *
1631 start_repo_child(struct gotd_client *client, enum gotd_procid proc_type,
1632 struct gotd_repo *repo, char *argv0, const char *confpath,
1633 int daemonize, int verbosity)
1635 struct gotd_child_proc *proc;
1637 if (proc_type != PROC_REPO_READ && proc_type != PROC_REPO_WRITE)
1638 return got_error_msg(GOT_ERR_NOT_IMPL, "bad process type");
1640 proc = calloc(1, sizeof(*proc));
1641 if (proc == NULL)
1642 return got_error_from_errno("calloc");
1644 TAILQ_INSERT_HEAD(&procs, proc, entry);
1645 evtimer_set(&proc->tmo, kill_proc_timeout, proc);
1647 proc->type = proc_type;
1648 if (strlcpy(proc->repo_name, repo->name,
1649 sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
1650 fatalx("repository name too long: %s", repo->name);
1651 log_debug("starting %s for repository %s",
1652 proc->type == PROC_REPO_READ ? "reader" : "writer", repo->name);
1653 if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >=
1654 sizeof(proc->repo_path))
1655 fatalx("repository path too long: %s", repo->path);
1656 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1657 PF_UNSPEC, proc->pipe) == -1)
1658 fatal("socketpair");
1659 proc->pid = start_child(proc->type, proc->repo_path, argv0,
1660 confpath, proc->pipe[1], daemonize, verbosity);
1661 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1662 log_debug("proc %s %s is on fd %d",
1663 gotd_proc_names[proc->type], proc->repo_path,
1664 proc->pipe[0]);
1665 proc->iev.handler = gotd_dispatch_repo_child;
1666 proc->iev.events = EV_READ;
1667 proc->iev.handler_arg = NULL;
1668 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1669 gotd_dispatch_repo_child, &proc->iev);
1670 gotd_imsg_event_add(&proc->iev);
1672 client->repo = proc;
1673 return NULL;
1676 static const struct got_error *
1677 start_auth_child(struct gotd_client *client, int required_auth,
1678 struct gotd_repo *repo, char *argv0, const char *confpath,
1679 int daemonize, int verbosity)
1681 const struct got_error *err = NULL;
1682 struct gotd_child_proc *proc;
1683 struct gotd_imsg_auth iauth;
1684 int fd;
1686 memset(&iauth, 0, sizeof(iauth));
1688 fd = dup(client->fd);
1689 if (fd == -1)
1690 return got_error_from_errno("dup");
1692 proc = calloc(1, sizeof(*proc));
1693 if (proc == NULL) {
1694 err = got_error_from_errno("calloc");
1695 close(fd);
1696 return err;
1699 TAILQ_INSERT_HEAD(&procs, proc, entry);
1700 evtimer_set(&proc->tmo, kill_proc_timeout, proc);
1702 proc->type = PROC_AUTH;
1703 if (strlcpy(proc->repo_name, repo->name,
1704 sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
1705 fatalx("repository name too long: %s", repo->name);
1706 log_debug("starting auth for uid %d repository %s",
1707 client->euid, repo->name);
1708 if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >=
1709 sizeof(proc->repo_path))
1710 fatalx("repository path too long: %s", repo->path);
1711 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1712 PF_UNSPEC, proc->pipe) == -1)
1713 fatal("socketpair");
1714 proc->pid = start_child(proc->type, proc->repo_path, argv0,
1715 confpath, proc->pipe[1], daemonize, verbosity);
1716 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1717 log_debug("proc %s %s is on fd %d",
1718 gotd_proc_names[proc->type], proc->repo_path,
1719 proc->pipe[0]);
1720 proc->iev.handler = gotd_dispatch_auth_child;
1721 proc->iev.events = EV_READ;
1722 proc->iev.handler_arg = NULL;
1723 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1724 gotd_dispatch_auth_child, &proc->iev);
1725 gotd_imsg_event_add(&proc->iev);
1727 iauth.euid = client->euid;
1728 iauth.egid = client->egid;
1729 iauth.required_auth = required_auth;
1730 iauth.client_id = client->id;
1731 if (gotd_imsg_compose_event(&proc->iev, GOTD_IMSG_AUTHENTICATE,
1732 PROC_GOTD, fd, &iauth, sizeof(iauth)) == -1) {
1733 log_warn("imsg compose AUTHENTICATE");
1734 close(fd);
1735 /* Let the auth_timeout handler tidy up. */
1738 client->auth = proc;
1739 client->required_auth = required_auth;
1740 return NULL;
1743 static void
1744 apply_unveil_repo_readonly(const char *repo_path, int need_tmpdir)
1746 if (need_tmpdir) {
1747 if (unveil(GOT_TMPDIR_STR, "rwc") == -1)
1748 fatal("unveil %s", GOT_TMPDIR_STR);
1751 if (unveil(repo_path, "r") == -1)
1752 fatal("unveil %s", repo_path);
1754 if (unveil(NULL, NULL) == -1)
1755 fatal("unveil");
1758 static void
1759 apply_unveil_repo_readwrite(const char *repo_path)
1761 if (unveil(repo_path, "rwc") == -1)
1762 fatal("unveil %s", repo_path);
1764 if (unveil(GOT_TMPDIR_STR, "rwc") == -1)
1765 fatal("unveil %s", GOT_TMPDIR_STR);
1767 if (unveil(NULL, NULL) == -1)
1768 fatal("unveil");
1771 static void
1772 apply_unveil_none(void)
1774 if (unveil("/", "") == -1)
1775 fatal("unveil");
1777 if (unveil(NULL, NULL) == -1)
1778 fatal("unveil");
1781 static void
1782 apply_unveil_selfexec(void)
1784 if (unveil(gotd.argv0, "x") == -1)
1785 fatal("unveil %s", gotd.argv0);
1787 if (unveil(NULL, NULL) == -1)
1788 fatal("unveil");
1791 int
1792 main(int argc, char **argv)
1794 const struct got_error *error = NULL;
1795 int ch, fd = -1, daemonize = 1, verbosity = 0, noaction = 0;
1796 const char *confpath = GOTD_CONF_PATH;
1797 char *argv0 = argv[0];
1798 char title[2048];
1799 struct passwd *pw = NULL;
1800 char *repo_path = NULL;
1801 enum gotd_procid proc_id = PROC_GOTD;
1802 struct event evsigint, evsigterm, evsighup, evsigusr1, evsigchld;
1803 int *pack_fds = NULL, *temp_fds = NULL;
1804 struct gotd_repo *repo = NULL;
1806 TAILQ_INIT(&procs);
1808 log_init(1, LOG_DAEMON); /* Log to stderr until daemonized. */
1810 while ((ch = getopt(argc, argv, "Adf:LnP:RsSvW")) != -1) {
1811 switch (ch) {
1812 case 'A':
1813 proc_id = PROC_AUTH;
1814 break;
1815 case 'd':
1816 daemonize = 0;
1817 break;
1818 case 'f':
1819 confpath = optarg;
1820 break;
1821 case 'L':
1822 proc_id = PROC_LISTEN;
1823 break;
1824 case 'n':
1825 noaction = 1;
1826 break;
1827 case 'P':
1828 repo_path = realpath(optarg, NULL);
1829 if (repo_path == NULL)
1830 fatal("realpath '%s'", optarg);
1831 break;
1832 case 'R':
1833 proc_id = PROC_REPO_READ;
1834 break;
1835 case 's':
1836 proc_id = PROC_SESSION_READ;
1837 break;
1838 case 'S':
1839 proc_id = PROC_SESSION_WRITE;
1840 break;
1841 case 'v':
1842 if (verbosity < 3)
1843 verbosity++;
1844 break;
1845 case 'W':
1846 proc_id = PROC_REPO_WRITE;
1847 break;
1848 default:
1849 usage();
1853 argc -= optind;
1854 argv += optind;
1856 if (argc != 0)
1857 usage();
1859 if (geteuid() && (proc_id == PROC_GOTD || proc_id == PROC_LISTEN))
1860 fatalx("need root privileges");
1862 if (parse_config(confpath, proc_id, &gotd) != 0)
1863 return 1;
1865 pw = getpwnam(gotd.user_name);
1866 if (pw == NULL)
1867 fatalx("user %s not found", gotd.user_name);
1869 if (pw->pw_uid == 0)
1870 fatalx("cannot run %s as the superuser", getprogname());
1872 if (noaction) {
1873 fprintf(stderr, "configuration OK\n");
1874 return 0;
1877 gotd.argv0 = argv0;
1878 gotd.daemonize = daemonize;
1879 gotd.verbosity = verbosity;
1880 gotd.confpath = confpath;
1882 /* Require an absolute path in argv[0] for reliable re-exec. */
1883 if (!got_path_is_absolute(argv0))
1884 fatalx("bad path \"%s\": must be an absolute path", argv0);
1886 log_init(daemonize ? 0 : 1, LOG_DAEMON);
1887 log_setverbose(verbosity);
1889 if (proc_id == PROC_GOTD) {
1890 snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
1891 arc4random_buf(&clients_hash_key, sizeof(clients_hash_key));
1892 if (daemonize && daemon(1, 0) == -1)
1893 fatal("daemon");
1894 gotd.pid = getpid();
1895 start_listener(argv0, confpath, daemonize, verbosity);
1896 } else if (proc_id == PROC_LISTEN) {
1897 snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
1898 if (verbosity) {
1899 log_info("socket: %s", gotd.unix_socket_path);
1900 log_info("user: %s", pw->pw_name);
1903 fd = unix_socket_listen(gotd.unix_socket_path, pw->pw_uid,
1904 pw->pw_gid);
1905 if (fd == -1) {
1906 fatal("cannot listen on unix socket %s",
1907 gotd.unix_socket_path);
1909 } else if (proc_id == PROC_AUTH) {
1910 snprintf(title, sizeof(title), "%s %s",
1911 gotd_proc_names[proc_id], repo_path);
1912 } else if (proc_id == PROC_REPO_READ || proc_id == PROC_REPO_WRITE ||
1913 proc_id == PROC_SESSION_READ || proc_id == PROC_SESSION_WRITE) {
1914 error = got_repo_pack_fds_open(&pack_fds);
1915 if (error != NULL)
1916 fatalx("cannot open pack tempfiles: %s", error->msg);
1917 error = got_repo_temp_fds_open(&temp_fds);
1918 if (error != NULL)
1919 fatalx("cannot open pack tempfiles: %s", error->msg);
1920 if (repo_path == NULL)
1921 fatalx("repository path not specified");
1922 snprintf(title, sizeof(title), "%s %s",
1923 gotd_proc_names[proc_id], repo_path);
1924 } else
1925 fatal("invalid process id %d", proc_id);
1927 setproctitle("%s", title);
1928 log_procinit(title);
1930 /* Drop root privileges. */
1931 if (setgid(pw->pw_gid) == -1)
1932 fatal("setgid %d failed", pw->pw_gid);
1933 if (setuid(pw->pw_uid) == -1)
1934 fatal("setuid %d failed", pw->pw_uid);
1936 event_init();
1938 switch (proc_id) {
1939 case PROC_GOTD:
1940 #ifndef PROFILE
1941 /* "exec" promise will be limited to argv[0] via unveil(2). */
1942 if (pledge("stdio proc exec sendfd recvfd unveil", NULL) == -1)
1943 err(1, "pledge");
1944 #endif
1945 break;
1946 case PROC_LISTEN:
1947 #ifndef PROFILE
1948 if (pledge("stdio sendfd unix unveil", NULL) == -1)
1949 err(1, "pledge");
1950 #endif
1952 * Ensure that AF_UNIX bind(2) cannot be used with any other
1953 * sockets by revoking all filesystem access via unveil(2).
1955 apply_unveil_none();
1957 listen_main(title, fd, gotd.connection_limits,
1958 gotd.nconnection_limits);
1959 /* NOTREACHED */
1960 break;
1961 case PROC_AUTH:
1962 #ifndef PROFILE
1963 if (pledge("stdio getpw recvfd unix unveil", NULL) == -1)
1964 err(1, "pledge");
1965 #endif
1967 * We need the "unix" pledge promise for getpeername(2) only.
1968 * Ensure that AF_UNIX bind(2) cannot be used by revoking all
1969 * filesystem access via unveil(2). Access to password database
1970 * files will still work since "getpw" bypasses unveil(2).
1972 apply_unveil_none();
1974 auth_main(title, &gotd.repos, repo_path);
1975 /* NOTREACHED */
1976 break;
1977 case PROC_SESSION_READ:
1978 case PROC_SESSION_WRITE:
1979 #ifndef PROFILE
1981 * The "recvfd" promise is only needed during setup and
1982 * will be removed in a later pledge(2) call.
1984 if (pledge("stdio rpath wpath cpath recvfd sendfd fattr flock "
1985 "unveil", NULL) == -1)
1986 err(1, "pledge");
1987 #endif
1988 if (proc_id == PROC_SESSION_READ)
1989 apply_unveil_repo_readonly(repo_path, 1);
1990 else
1991 apply_unveil_repo_readwrite(repo_path);
1992 session_main(title, repo_path, pack_fds, temp_fds,
1993 &gotd.request_timeout, proc_id);
1994 /* NOTREACHED */
1995 break;
1996 case PROC_REPO_READ:
1997 #ifndef PROFILE
1998 if (pledge("stdio rpath recvfd unveil", NULL) == -1)
1999 err(1, "pledge");
2000 #endif
2001 apply_unveil_repo_readonly(repo_path, 0);
2002 repo_read_main(title, repo_path, pack_fds, temp_fds);
2003 /* NOTREACHED */
2004 exit(0);
2005 case PROC_REPO_WRITE:
2006 #ifndef PROFILE
2007 if (pledge("stdio rpath recvfd unveil", NULL) == -1)
2008 err(1, "pledge");
2009 #endif
2010 apply_unveil_repo_readonly(repo_path, 0);
2011 repo = gotd_find_repo_by_path(repo_path, &gotd);
2012 if (repo == NULL)
2013 fatalx("no repository for path %s", repo_path);
2014 repo_write_main(title, repo_path, pack_fds, temp_fds,
2015 &repo->protected_tag_namespaces,
2016 &repo->protected_branch_namespaces,
2017 &repo->protected_branches);
2018 /* NOTREACHED */
2019 exit(0);
2020 default:
2021 fatal("invalid process id %d", proc_id);
2024 if (proc_id != PROC_GOTD)
2025 fatal("invalid process id %d", proc_id);
2027 evtimer_set(&gotd.listen_proc->tmo, kill_proc_timeout,
2028 gotd.listen_proc);
2030 apply_unveil_selfexec();
2032 signal_set(&evsigint, SIGINT, gotd_sighdlr, NULL);
2033 signal_set(&evsigterm, SIGTERM, gotd_sighdlr, NULL);
2034 signal_set(&evsighup, SIGHUP, gotd_sighdlr, NULL);
2035 signal_set(&evsigusr1, SIGUSR1, gotd_sighdlr, NULL);
2036 signal_set(&evsigchld, SIGCHLD, gotd_sighdlr, NULL);
2037 signal(SIGPIPE, SIG_IGN);
2039 signal_add(&evsigint, NULL);
2040 signal_add(&evsigterm, NULL);
2041 signal_add(&evsighup, NULL);
2042 signal_add(&evsigusr1, NULL);
2043 signal_add(&evsigchld, NULL);
2045 gotd_imsg_event_add(&gotd.listen_proc->iev);
2047 event_dispatch();
2049 free(repo_path);
2050 gotd_shutdown();
2052 return 0;