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/time.h>
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <sys/socket.h>
22 #include <sys/un.h>
23 #include <sys/wait.h>
25 #include <fcntl.h>
26 #include <err.h>
27 #include <errno.h>
28 #include <event.h>
29 #include <limits.h>
30 #include <pwd.h>
31 #include <imsg.h>
32 #include <signal.h>
33 #include <siphash.h>
34 #include <stdarg.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <syslog.h>
39 #include <unistd.h>
41 #include "got_error.h"
42 #include "got_opentemp.h"
43 #include "got_path.h"
44 #include "got_repository.h"
45 #include "got_object.h"
46 #include "got_reference.h"
48 #include "got_lib_delta.h"
49 #include "got_lib_object.h"
50 #include "got_lib_object_cache.h"
51 #include "got_lib_hash.h"
52 #include "got_lib_gitproto.h"
53 #include "got_lib_pack.h"
54 #include "got_lib_repository.h"
56 #include "gotd.h"
57 #include "log.h"
58 #include "listen.h"
59 #include "auth.h"
60 #include "session.h"
61 #include "repo_read.h"
62 #include "repo_write.h"
64 #ifndef nitems
65 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
66 #endif
68 enum gotd_client_state {
69 GOTD_CLIENT_STATE_NEW,
70 GOTD_CLIENT_STATE_ACCESS_GRANTED,
71 };
73 struct gotd_child_proc {
74 pid_t pid;
75 enum gotd_procid type;
76 char repo_name[NAME_MAX];
77 char repo_path[PATH_MAX];
78 int pipe[2];
79 struct gotd_imsgev iev;
80 struct event tmo;
82 TAILQ_ENTRY(gotd_child_proc) entry;
83 };
84 TAILQ_HEAD(gotd_procs, gotd_child_proc) procs;
86 struct gotd_client {
87 STAILQ_ENTRY(gotd_client) entry;
88 enum gotd_client_state state;
89 uint32_t id;
90 int fd;
91 struct gotd_imsgev iev;
92 struct event tmo;
93 uid_t euid;
94 gid_t egid;
95 struct gotd_child_proc *repo;
96 struct gotd_child_proc *auth;
97 struct gotd_child_proc *session;
98 int required_auth;
99 };
100 STAILQ_HEAD(gotd_clients, gotd_client);
102 static struct gotd_clients gotd_clients[GOTD_CLIENT_TABLE_SIZE];
103 static SIPHASH_KEY clients_hash_key;
104 volatile int client_cnt;
105 static struct timeval auth_timeout = { 5, 0 };
106 static struct gotd gotd;
108 void gotd_sighdlr(int sig, short event, void *arg);
109 static void gotd_shutdown(void);
110 static const struct got_error *start_session_child(struct gotd_client *,
111 struct gotd_repo *, char *, const char *, int, int);
112 static const struct got_error *start_repo_child(struct gotd_client *,
113 enum gotd_procid, struct gotd_repo *, char *, const char *, int, int);
114 static const struct got_error *start_auth_child(struct gotd_client *, int,
115 struct gotd_repo *, char *, const char *, int, int);
116 static void kill_proc(struct gotd_child_proc *, int);
117 static void disconnect(struct gotd_client *);
119 __dead static void
120 usage(void)
122 fprintf(stderr, "usage: %s [-dnv] [-f config-file]\n", getprogname());
123 exit(1);
126 static int
127 unix_socket_listen(const char *unix_socket_path, uid_t uid, gid_t gid)
129 struct sockaddr_un sun;
130 int fd = -1;
131 mode_t old_umask, mode;
133 fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK| SOCK_CLOEXEC, 0);
134 if (fd == -1) {
135 log_warn("socket");
136 return -1;
139 sun.sun_family = AF_UNIX;
140 if (strlcpy(sun.sun_path, unix_socket_path,
141 sizeof(sun.sun_path)) >= sizeof(sun.sun_path)) {
142 log_warnx("%s: name too long", unix_socket_path);
143 close(fd);
144 return -1;
147 if (unlink(unix_socket_path) == -1) {
148 if (errno != ENOENT) {
149 log_warn("unlink %s", unix_socket_path);
150 close(fd);
151 return -1;
155 old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
156 mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
158 if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
159 log_warn("bind: %s", unix_socket_path);
160 close(fd);
161 umask(old_umask);
162 return -1;
165 umask(old_umask);
167 if (chmod(unix_socket_path, mode) == -1) {
168 log_warn("chmod %o %s", mode, unix_socket_path);
169 close(fd);
170 unlink(unix_socket_path);
171 return -1;
174 if (chown(unix_socket_path, uid, gid) == -1) {
175 log_warn("chown %s uid=%d gid=%d", unix_socket_path, uid, gid);
176 close(fd);
177 unlink(unix_socket_path);
178 return -1;
181 if (listen(fd, GOTD_UNIX_SOCKET_BACKLOG) == -1) {
182 log_warn("listen");
183 close(fd);
184 unlink(unix_socket_path);
185 return -1;
188 return fd;
191 static uint64_t
192 client_hash(uint32_t client_id)
194 return SipHash24(&clients_hash_key, &client_id, sizeof(client_id));
197 static void
198 add_client(struct gotd_client *client)
200 uint64_t slot = client_hash(client->id) % nitems(gotd_clients);
201 STAILQ_INSERT_HEAD(&gotd_clients[slot], client, entry);
202 client_cnt++;
205 static struct gotd_client *
206 find_client(uint32_t client_id)
208 uint64_t slot;
209 struct gotd_client *c;
211 slot = client_hash(client_id) % nitems(gotd_clients);
212 STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
213 if (c->id == client_id)
214 return c;
217 return NULL;
220 static struct gotd_client *
221 find_client_by_proc_fd(int fd)
223 uint64_t slot;
225 for (slot = 0; slot < nitems(gotd_clients); slot++) {
226 struct gotd_client *c;
228 STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
229 if (c->repo && c->repo->iev.ibuf.fd == fd)
230 return c;
231 if (c->auth && c->auth->iev.ibuf.fd == fd)
232 return c;
233 if (c->session && c->session->iev.ibuf.fd == fd)
234 return c;
238 return NULL;
241 static int
242 client_is_reading(struct gotd_client *client)
244 return (client->required_auth &
245 (GOTD_AUTH_READ | GOTD_AUTH_WRITE)) == GOTD_AUTH_READ;
248 static int
249 client_is_writing(struct gotd_client *client)
251 return (client->required_auth &
252 (GOTD_AUTH_READ | GOTD_AUTH_WRITE)) ==
253 (GOTD_AUTH_READ | GOTD_AUTH_WRITE);
256 static const struct got_error *
257 ensure_client_is_not_writing(struct gotd_client *client)
259 if (client_is_writing(client)) {
260 return got_error_fmt(GOT_ERR_BAD_PACKET,
261 "uid %d made a read-request but is writing to "
262 "a repository", client->euid);
265 return NULL;
268 static const struct got_error *
269 ensure_client_is_not_reading(struct gotd_client *client)
271 if (client_is_reading(client)) {
272 return got_error_fmt(GOT_ERR_BAD_PACKET,
273 "uid %d made a write-request but is reading from "
274 "a repository", client->euid);
277 return NULL;
280 static void
281 proc_done(struct gotd_child_proc *proc)
283 struct gotd_client *client;
285 TAILQ_REMOVE(&procs, proc, entry);
287 client = find_client_by_proc_fd(proc->iev.ibuf.fd);
288 if (client != NULL) {
289 if (proc == client->repo)
290 client->repo = NULL;
291 if (proc == client->auth)
292 client->auth = NULL;
293 if (proc == client->session)
294 client->session = NULL;
295 disconnect(client);
298 evtimer_del(&proc->tmo);
300 if (proc->iev.ibuf.fd != -1) {
301 event_del(&proc->iev.ev);
302 msgbuf_clear(&proc->iev.ibuf.w);
303 close(proc->iev.ibuf.fd);
306 free(proc);
309 static void
310 kill_repo_proc(struct gotd_client *client)
312 if (client->repo == NULL)
313 return;
315 kill_proc(client->repo, 0);
316 client->repo = NULL;
319 static void
320 kill_auth_proc(struct gotd_client *client)
322 if (client->auth == NULL)
323 return;
325 kill_proc(client->auth, 0);
326 client->auth = NULL;
329 static void
330 kill_session_proc(struct gotd_client *client)
332 if (client->session == NULL)
333 return;
335 kill_proc(client->session, 0);
336 client->session = NULL;
339 static void
340 disconnect(struct gotd_client *client)
342 struct gotd_imsg_disconnect idisconnect;
343 struct gotd_child_proc *listen_proc = gotd.listen_proc;
344 uint64_t slot;
346 log_debug("uid %d: disconnecting", client->euid);
348 kill_auth_proc(client);
349 kill_session_proc(client);
350 kill_repo_proc(client);
352 idisconnect.client_id = client->id;
353 if (gotd_imsg_compose_event(&listen_proc->iev,
354 GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
355 &idisconnect, sizeof(idisconnect)) == -1)
356 log_warn("imsg compose DISCONNECT");
358 slot = client_hash(client->id) % nitems(gotd_clients);
359 STAILQ_REMOVE(&gotd_clients[slot], client, gotd_client, entry);
360 imsg_clear(&client->iev.ibuf);
361 event_del(&client->iev.ev);
362 evtimer_del(&client->tmo);
363 if (client->fd != -1)
364 close(client->fd);
365 else if (client->iev.ibuf.fd != -1)
366 close(client->iev.ibuf.fd);
367 free(client);
368 client_cnt--;
371 static void
372 disconnect_on_error(struct gotd_client *client, const struct got_error *err)
374 struct imsgbuf ibuf;
376 if (err->code != GOT_ERR_EOF) {
377 log_warnx("uid %d: %s", client->euid, err->msg);
378 if (client->fd != -1) {
379 imsg_init(&ibuf, client->fd);
380 gotd_imsg_send_error(&ibuf, 0, PROC_GOTD, err);
381 imsg_clear(&ibuf);
384 disconnect(client);
387 static const struct got_error *
388 send_repo_info(struct gotd_imsgev *iev, struct gotd_repo *repo)
390 const struct got_error *err = NULL;
391 struct gotd_imsg_info_repo irepo;
393 memset(&irepo, 0, sizeof(irepo));
395 if (strlcpy(irepo.repo_name, repo->name, sizeof(irepo.repo_name))
396 >= sizeof(irepo.repo_name))
397 return got_error_msg(GOT_ERR_NO_SPACE, "repo name too long");
398 if (strlcpy(irepo.repo_path, repo->path, sizeof(irepo.repo_path))
399 >= sizeof(irepo.repo_path))
400 return got_error_msg(GOT_ERR_NO_SPACE, "repo path too long");
402 if (gotd_imsg_compose_event(iev, GOTD_IMSG_INFO_REPO, PROC_GOTD, -1,
403 &irepo, sizeof(irepo)) == -1) {
404 err = got_error_from_errno("imsg compose INFO_REPO");
405 if (err)
406 return err;
409 return NULL;
412 static const struct got_error *
413 send_client_info(struct gotd_imsgev *iev, struct gotd_client *client)
415 const struct got_error *err = NULL;
416 struct gotd_imsg_info_client iclient;
417 struct gotd_child_proc *proc;
419 memset(&iclient, 0, sizeof(iclient));
420 iclient.euid = client->euid;
421 iclient.egid = client->egid;
423 proc = client->repo;
424 if (proc) {
425 if (strlcpy(iclient.repo_name, proc->repo_path,
426 sizeof(iclient.repo_name)) >= sizeof(iclient.repo_name)) {
427 return got_error_msg(GOT_ERR_NO_SPACE,
428 "repo name too long");
430 if (client_is_writing(client))
431 iclient.is_writing = 1;
433 iclient.repo_child_pid = proc->pid;
436 if (client->session)
437 iclient.session_child_pid = client->session->pid;
439 if (gotd_imsg_compose_event(iev, GOTD_IMSG_INFO_CLIENT, PROC_GOTD, -1,
440 &iclient, sizeof(iclient)) == -1) {
441 err = got_error_from_errno("imsg compose INFO_CLIENT");
442 if (err)
443 return err;
446 return NULL;
449 static const struct got_error *
450 send_info(struct gotd_client *client)
452 const struct got_error *err = NULL;
453 struct gotd_imsg_info info;
454 uint64_t slot;
455 struct gotd_repo *repo;
457 if (client->euid != 0)
458 return got_error_set_errno(EPERM, "info");
460 info.pid = gotd.pid;
461 info.verbosity = gotd.verbosity;
462 info.nrepos = gotd.nrepos;
463 info.nclients = client_cnt - 1;
465 if (gotd_imsg_compose_event(&client->iev, GOTD_IMSG_INFO, PROC_GOTD, -1,
466 &info, sizeof(info)) == -1) {
467 err = got_error_from_errno("imsg compose INFO");
468 if (err)
469 return err;
472 TAILQ_FOREACH(repo, &gotd.repos, entry) {
473 err = send_repo_info(&client->iev, repo);
474 if (err)
475 return err;
478 for (slot = 0; slot < nitems(gotd_clients); slot++) {
479 struct gotd_client *c;
480 STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
481 if (c->id == client->id)
482 continue;
483 err = send_client_info(&client->iev, c);
484 if (err)
485 return err;
489 return NULL;
492 static const struct got_error *
493 stop_gotd(struct gotd_client *client)
496 if (client->euid != 0)
497 return got_error_set_errno(EPERM, "stop");
499 gotd_shutdown();
500 /* NOTREACHED */
501 return NULL;
504 static const struct got_error *
505 start_client_authentication(struct gotd_client *client, struct imsg *imsg)
507 const struct got_error *err;
508 struct gotd_imsg_list_refs ireq;
509 struct gotd_repo *repo = NULL;
510 size_t datalen;
512 log_debug("list-refs request from uid %d", client->euid);
514 if (client->state != GOTD_CLIENT_STATE_NEW)
515 return got_error_msg(GOT_ERR_BAD_REQUEST,
516 "unexpected list-refs request received");
518 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
519 if (datalen != sizeof(ireq))
520 return got_error(GOT_ERR_PRIVSEP_LEN);
522 memcpy(&ireq, imsg->data, datalen);
524 if (ireq.client_is_reading) {
525 err = ensure_client_is_not_writing(client);
526 if (err)
527 return err;
528 repo = gotd_find_repo_by_name(ireq.repo_name, &gotd);
529 if (repo == NULL)
530 return got_error(GOT_ERR_NOT_GIT_REPO);
531 err = start_auth_child(client, GOTD_AUTH_READ, repo,
532 gotd.argv0, gotd.confpath, gotd.daemonize,
533 gotd.verbosity);
534 if (err)
535 return err;
536 } else {
537 err = ensure_client_is_not_reading(client);
538 if (err)
539 return err;
540 repo = gotd_find_repo_by_name(ireq.repo_name, &gotd);
541 if (repo == NULL)
542 return got_error(GOT_ERR_NOT_GIT_REPO);
543 err = start_auth_child(client,
544 GOTD_AUTH_READ | GOTD_AUTH_WRITE,
545 repo, gotd.argv0, gotd.confpath, gotd.daemonize,
546 gotd.verbosity);
547 if (err)
548 return err;
551 evtimer_add(&client->tmo, &auth_timeout);
553 /* Flow continues upon authentication success/failure or timeout. */
554 return NULL;
557 static void
558 gotd_request(int fd, short events, void *arg)
560 struct gotd_imsgev *iev = arg;
561 struct imsgbuf *ibuf = &iev->ibuf;
562 struct gotd_client *client = iev->handler_arg;
563 const struct got_error *err = NULL;
564 struct imsg imsg;
565 ssize_t n;
567 if (events & EV_WRITE) {
568 while (ibuf->w.queued) {
569 n = msgbuf_write(&ibuf->w);
570 if (n == -1 && errno == EPIPE) {
571 /*
572 * The client has closed its socket.
573 * This can happen when Git clients are
574 * done sending pack file data.
575 */
576 msgbuf_clear(&ibuf->w);
577 continue;
578 } else if (n == -1 && errno != EAGAIN) {
579 err = got_error_from_errno("imsg_flush");
580 disconnect_on_error(client, err);
581 return;
583 if (n == 0) {
584 /* Connection closed. */
585 err = got_error(GOT_ERR_EOF);
586 disconnect_on_error(client, err);
587 return;
591 /* Disconnect gotctl(8) now that messages have been sent. */
592 if (!client_is_reading(client) && !client_is_writing(client)) {
593 disconnect(client);
594 return;
598 if ((events & EV_READ) == 0)
599 return;
601 memset(&imsg, 0, sizeof(imsg));
603 while (err == NULL) {
604 err = gotd_imsg_recv(&imsg, ibuf, 0);
605 if (err) {
606 if (err->code == GOT_ERR_PRIVSEP_READ)
607 err = NULL;
608 break;
611 evtimer_del(&client->tmo);
613 switch (imsg.hdr.type) {
614 case GOTD_IMSG_INFO:
615 err = send_info(client);
616 break;
617 case GOTD_IMSG_STOP:
618 err = stop_gotd(client);
619 break;
620 case GOTD_IMSG_LIST_REFS:
621 err = start_client_authentication(client, &imsg);
622 break;
623 default:
624 log_debug("unexpected imsg %d", imsg.hdr.type);
625 err = got_error(GOT_ERR_PRIVSEP_MSG);
626 break;
629 imsg_free(&imsg);
632 if (err) {
633 disconnect_on_error(client, err);
634 } else {
635 gotd_imsg_event_add(&client->iev);
639 static void
640 gotd_auth_timeout(int fd, short events, void *arg)
642 struct gotd_client *client = arg;
644 log_debug("disconnecting uid %d due to authentication timeout",
645 client->euid);
646 disconnect(client);
649 static const struct got_error *
650 recv_connect(uint32_t *client_id, struct imsg *imsg)
652 const struct got_error *err = NULL;
653 struct gotd_imsg_connect iconnect;
654 size_t datalen;
655 int s = -1;
656 struct gotd_client *client = NULL;
658 *client_id = 0;
660 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
661 if (datalen != sizeof(iconnect))
662 return got_error(GOT_ERR_PRIVSEP_LEN);
663 memcpy(&iconnect, imsg->data, sizeof(iconnect));
665 s = imsg->fd;
666 if (s == -1) {
667 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
668 goto done;
671 if (find_client(iconnect.client_id)) {
672 err = got_error_msg(GOT_ERR_CLIENT_ID, "duplicate client ID");
673 goto done;
676 client = calloc(1, sizeof(*client));
677 if (client == NULL) {
678 err = got_error_from_errno("calloc");
679 goto done;
682 *client_id = iconnect.client_id;
684 client->state = GOTD_CLIENT_STATE_NEW;
685 client->id = iconnect.client_id;
686 client->fd = s;
687 s = -1;
688 /* The auth process will verify UID/GID for us. */
689 client->euid = iconnect.euid;
690 client->egid = iconnect.egid;
692 imsg_init(&client->iev.ibuf, client->fd);
693 client->iev.handler = gotd_request;
694 client->iev.events = EV_READ;
695 client->iev.handler_arg = client;
697 event_set(&client->iev.ev, client->fd, EV_READ, gotd_request,
698 &client->iev);
699 gotd_imsg_event_add(&client->iev);
701 evtimer_set(&client->tmo, gotd_auth_timeout, client);
703 add_client(client);
704 log_debug("%s: new client uid %d connected on fd %d", __func__,
705 client->euid, client->fd);
706 done:
707 if (err) {
708 struct gotd_child_proc *listen_proc = gotd.listen_proc;
709 struct gotd_imsg_disconnect idisconnect;
711 idisconnect.client_id = client->id;
712 if (gotd_imsg_compose_event(&listen_proc->iev,
713 GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
714 &idisconnect, sizeof(idisconnect)) == -1)
715 log_warn("imsg compose DISCONNECT");
717 if (s != -1)
718 close(s);
721 return err;
724 static const char *gotd_proc_names[PROC_MAX] = {
725 "parent",
726 "listen",
727 "auth",
728 "session_read",
729 "session_write",
730 "repo_read",
731 "repo_write",
732 "gitwrapper"
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) != 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;