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>
25 #include <sys/resource.h>
27 #include <fcntl.h>
28 #include <err.h>
29 #include <errno.h>
30 #include <event.h>
31 #include <limits.h>
32 #include <pwd.h>
33 #include <imsg.h>
34 #include <sha1.h>
35 #include <sha2.h>
36 #include <signal.h>
37 #include <siphash.h>
38 #include <stdarg.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <syslog.h>
43 #include <unistd.h>
45 #include "got_error.h"
46 #include "got_opentemp.h"
47 #include "got_path.h"
48 #include "got_repository.h"
49 #include "got_object.h"
50 #include "got_reference.h"
52 #include "got_lib_delta.h"
53 #include "got_lib_object.h"
54 #include "got_lib_object_cache.h"
55 #include "got_lib_hash.h"
56 #include "got_lib_gitproto.h"
57 #include "got_lib_pack.h"
58 #include "got_lib_repository.h"
60 #include "gotd.h"
61 #include "log.h"
62 #include "listen.h"
63 #include "auth.h"
64 #include "session.h"
65 #include "repo_read.h"
66 #include "repo_write.h"
68 #ifndef nitems
69 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
70 #endif
72 enum gotd_client_state {
73 GOTD_CLIENT_STATE_NEW,
74 GOTD_CLIENT_STATE_ACCESS_GRANTED,
75 };
77 struct gotd_child_proc {
78 pid_t pid;
79 enum gotd_procid type;
80 char repo_name[NAME_MAX];
81 char repo_path[PATH_MAX];
82 int pipe[2];
83 struct gotd_imsgev iev;
84 struct event tmo;
86 TAILQ_ENTRY(gotd_child_proc) entry;
87 };
88 TAILQ_HEAD(gotd_procs, gotd_child_proc) procs;
90 struct gotd_client {
91 STAILQ_ENTRY(gotd_client) entry;
92 enum gotd_client_state state;
93 uint32_t id;
94 int fd;
95 struct gotd_imsgev iev;
96 struct event tmo;
97 uid_t euid;
98 gid_t egid;
99 struct gotd_child_proc *repo;
100 struct gotd_child_proc *auth;
101 struct gotd_child_proc *session;
102 int required_auth;
103 };
104 STAILQ_HEAD(gotd_clients, gotd_client);
106 static struct gotd_clients gotd_clients[GOTD_CLIENT_TABLE_SIZE];
107 static SIPHASH_KEY clients_hash_key;
108 volatile int client_cnt;
109 static struct timeval auth_timeout = { 5, 0 };
110 static struct gotd gotd;
112 void gotd_sighdlr(int sig, short event, void *arg);
113 static void gotd_shutdown(void);
114 static const struct got_error *start_session_child(struct gotd_client *,
115 struct gotd_repo *, char *, const char *, int, int);
116 static const struct got_error *start_repo_child(struct gotd_client *,
117 enum gotd_procid, struct gotd_repo *, char *, const char *, int, int);
118 static const struct got_error *start_auth_child(struct gotd_client *, int,
119 struct gotd_repo *, char *, const char *, int, int);
120 static void kill_proc(struct gotd_child_proc *, int);
121 static void disconnect(struct gotd_client *);
123 __dead static void
124 usage(void)
126 fprintf(stderr, "usage: %s [-dnv] [-f config-file]\n", getprogname());
127 exit(1);
130 static int
131 unix_socket_listen(const char *unix_socket_path, uid_t uid, gid_t gid)
133 struct sockaddr_un sun;
134 int fd = -1;
135 mode_t old_umask, mode;
137 fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK| SOCK_CLOEXEC, 0);
138 if (fd == -1) {
139 log_warn("socket");
140 return -1;
143 sun.sun_family = AF_UNIX;
144 if (strlcpy(sun.sun_path, unix_socket_path,
145 sizeof(sun.sun_path)) >= sizeof(sun.sun_path)) {
146 log_warnx("%s: name too long", unix_socket_path);
147 close(fd);
148 return -1;
151 if (unlink(unix_socket_path) == -1) {
152 if (errno != ENOENT) {
153 log_warn("unlink %s", unix_socket_path);
154 close(fd);
155 return -1;
159 old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
160 mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
162 if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
163 log_warn("bind: %s", unix_socket_path);
164 close(fd);
165 umask(old_umask);
166 return -1;
169 umask(old_umask);
171 if (chmod(unix_socket_path, mode) == -1) {
172 log_warn("chmod %o %s", mode, unix_socket_path);
173 close(fd);
174 unlink(unix_socket_path);
175 return -1;
178 if (chown(unix_socket_path, uid, gid) == -1) {
179 log_warn("chown %s uid=%d gid=%d", unix_socket_path, uid, gid);
180 close(fd);
181 unlink(unix_socket_path);
182 return -1;
185 if (listen(fd, GOTD_UNIX_SOCKET_BACKLOG) == -1) {
186 log_warn("listen");
187 close(fd);
188 unlink(unix_socket_path);
189 return -1;
192 return fd;
195 static uint64_t
196 client_hash(uint32_t client_id)
198 return SipHash24(&clients_hash_key, &client_id, sizeof(client_id));
201 static void
202 add_client(struct gotd_client *client)
204 uint64_t slot = client_hash(client->id) % nitems(gotd_clients);
205 STAILQ_INSERT_HEAD(&gotd_clients[slot], client, entry);
206 client_cnt++;
209 static struct gotd_client *
210 find_client(uint32_t client_id)
212 uint64_t slot;
213 struct gotd_client *c;
215 slot = client_hash(client_id) % nitems(gotd_clients);
216 STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
217 if (c->id == client_id)
218 return c;
221 return NULL;
224 static struct gotd_client *
225 find_client_by_proc_fd(int fd)
227 uint64_t slot;
229 for (slot = 0; slot < nitems(gotd_clients); slot++) {
230 struct gotd_client *c;
232 STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
233 if (c->repo && c->repo->iev.ibuf.fd == fd)
234 return c;
235 if (c->auth && c->auth->iev.ibuf.fd == fd)
236 return c;
237 if (c->session && c->session->iev.ibuf.fd == fd)
238 return c;
242 return NULL;
245 static int
246 client_is_reading(struct gotd_client *client)
248 return (client->required_auth &
249 (GOTD_AUTH_READ | GOTD_AUTH_WRITE)) == GOTD_AUTH_READ;
252 static int
253 client_is_writing(struct gotd_client *client)
255 return (client->required_auth &
256 (GOTD_AUTH_READ | GOTD_AUTH_WRITE)) ==
257 (GOTD_AUTH_READ | GOTD_AUTH_WRITE);
260 static const struct got_error *
261 ensure_client_is_not_writing(struct gotd_client *client)
263 if (client_is_writing(client)) {
264 return got_error_fmt(GOT_ERR_BAD_PACKET,
265 "uid %d made a read-request but is writing to "
266 "a repository", client->euid);
269 return NULL;
272 static const struct got_error *
273 ensure_client_is_not_reading(struct gotd_client *client)
275 if (client_is_reading(client)) {
276 return got_error_fmt(GOT_ERR_BAD_PACKET,
277 "uid %d made a write-request but is reading from "
278 "a repository", client->euid);
281 return NULL;
284 static void
285 proc_done(struct gotd_child_proc *proc)
287 struct gotd_client *client;
289 TAILQ_REMOVE(&procs, proc, entry);
291 client = find_client_by_proc_fd(proc->iev.ibuf.fd);
292 if (client != NULL) {
293 if (proc == client->repo)
294 client->repo = NULL;
295 if (proc == client->auth)
296 client->auth = NULL;
297 if (proc == client->session)
298 client->session = NULL;
299 disconnect(client);
302 evtimer_del(&proc->tmo);
304 if (proc->iev.ibuf.fd != -1) {
305 event_del(&proc->iev.ev);
306 msgbuf_clear(&proc->iev.ibuf.w);
307 close(proc->iev.ibuf.fd);
310 free(proc);
313 static void
314 kill_repo_proc(struct gotd_client *client)
316 if (client->repo == NULL)
317 return;
319 kill_proc(client->repo, 0);
320 client->repo = NULL;
323 static void
324 kill_auth_proc(struct gotd_client *client)
326 if (client->auth == NULL)
327 return;
329 kill_proc(client->auth, 0);
330 client->auth = NULL;
333 static void
334 kill_session_proc(struct gotd_client *client)
336 if (client->session == NULL)
337 return;
339 kill_proc(client->session, 0);
340 client->session = NULL;
343 static void
344 disconnect(struct gotd_client *client)
346 struct gotd_imsg_disconnect idisconnect;
347 struct gotd_child_proc *listen_proc = gotd.listen_proc;
348 uint64_t slot;
350 log_debug("uid %d: disconnecting", client->euid);
352 kill_auth_proc(client);
353 kill_session_proc(client);
354 kill_repo_proc(client);
356 idisconnect.client_id = client->id;
357 if (gotd_imsg_compose_event(&listen_proc->iev,
358 GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
359 &idisconnect, sizeof(idisconnect)) == -1)
360 log_warn("imsg compose DISCONNECT");
362 slot = client_hash(client->id) % nitems(gotd_clients);
363 STAILQ_REMOVE(&gotd_clients[slot], client, gotd_client, entry);
364 imsg_clear(&client->iev.ibuf);
365 event_del(&client->iev.ev);
366 evtimer_del(&client->tmo);
367 if (client->fd != -1)
368 close(client->fd);
369 else if (client->iev.ibuf.fd != -1)
370 close(client->iev.ibuf.fd);
371 free(client);
372 client_cnt--;
375 static void
376 disconnect_on_error(struct gotd_client *client, const struct got_error *err)
378 struct imsgbuf ibuf;
380 if (err->code != GOT_ERR_EOF) {
381 log_warnx("uid %d: %s", client->euid, err->msg);
382 if (client->fd != -1) {
383 imsg_init(&ibuf, client->fd);
384 gotd_imsg_send_error(&ibuf, 0, PROC_GOTD, err);
385 imsg_clear(&ibuf);
388 disconnect(client);
391 static const struct got_error *
392 send_repo_info(struct gotd_imsgev *iev, struct gotd_repo *repo)
394 const struct got_error *err = NULL;
395 struct gotd_imsg_info_repo irepo;
397 memset(&irepo, 0, sizeof(irepo));
399 if (strlcpy(irepo.repo_name, repo->name, sizeof(irepo.repo_name))
400 >= sizeof(irepo.repo_name))
401 return got_error_msg(GOT_ERR_NO_SPACE, "repo name too long");
402 if (strlcpy(irepo.repo_path, repo->path, sizeof(irepo.repo_path))
403 >= sizeof(irepo.repo_path))
404 return got_error_msg(GOT_ERR_NO_SPACE, "repo path too long");
406 if (gotd_imsg_compose_event(iev, GOTD_IMSG_INFO_REPO, PROC_GOTD, -1,
407 &irepo, sizeof(irepo)) == -1) {
408 err = got_error_from_errno("imsg compose INFO_REPO");
409 if (err)
410 return err;
413 return NULL;
416 static const struct got_error *
417 send_client_info(struct gotd_imsgev *iev, struct gotd_client *client)
419 const struct got_error *err = NULL;
420 struct gotd_imsg_info_client iclient;
421 struct gotd_child_proc *proc;
423 memset(&iclient, 0, sizeof(iclient));
424 iclient.euid = client->euid;
425 iclient.egid = client->egid;
427 proc = client->repo;
428 if (proc) {
429 if (strlcpy(iclient.repo_name, proc->repo_path,
430 sizeof(iclient.repo_name)) >= sizeof(iclient.repo_name)) {
431 return got_error_msg(GOT_ERR_NO_SPACE,
432 "repo name too long");
434 if (client_is_writing(client))
435 iclient.is_writing = 1;
437 iclient.repo_child_pid = proc->pid;
440 if (client->session)
441 iclient.session_child_pid = client->session->pid;
443 if (gotd_imsg_compose_event(iev, GOTD_IMSG_INFO_CLIENT, PROC_GOTD, -1,
444 &iclient, sizeof(iclient)) == -1) {
445 err = got_error_from_errno("imsg compose INFO_CLIENT");
446 if (err)
447 return err;
450 return NULL;
453 static const struct got_error *
454 send_info(struct gotd_client *client)
456 const struct got_error *err = NULL;
457 struct gotd_imsg_info info;
458 uint64_t slot;
459 struct gotd_repo *repo;
461 if (client->euid != 0)
462 return got_error_set_errno(EPERM, "info");
464 info.pid = gotd.pid;
465 info.verbosity = gotd.verbosity;
466 info.nrepos = gotd.nrepos;
467 info.nclients = client_cnt - 1;
469 if (gotd_imsg_compose_event(&client->iev, GOTD_IMSG_INFO, PROC_GOTD, -1,
470 &info, sizeof(info)) == -1) {
471 err = got_error_from_errno("imsg compose INFO");
472 if (err)
473 return err;
476 TAILQ_FOREACH(repo, &gotd.repos, entry) {
477 err = send_repo_info(&client->iev, repo);
478 if (err)
479 return err;
482 for (slot = 0; slot < nitems(gotd_clients); slot++) {
483 struct gotd_client *c;
484 STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
485 if (c->id == client->id)
486 continue;
487 err = send_client_info(&client->iev, c);
488 if (err)
489 return err;
493 return NULL;
496 static const struct got_error *
497 stop_gotd(struct gotd_client *client)
500 if (client->euid != 0)
501 return got_error_set_errno(EPERM, "stop");
503 gotd_shutdown();
504 /* NOTREACHED */
505 return NULL;
508 static const struct got_error *
509 start_client_authentication(struct gotd_client *client, struct imsg *imsg)
511 const struct got_error *err;
512 struct gotd_imsg_list_refs ireq;
513 struct gotd_repo *repo = NULL;
514 size_t datalen;
516 log_debug("list-refs request from uid %d", client->euid);
518 if (client->state != GOTD_CLIENT_STATE_NEW)
519 return got_error_msg(GOT_ERR_BAD_REQUEST,
520 "unexpected list-refs request received");
522 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
523 if (datalen != sizeof(ireq))
524 return got_error(GOT_ERR_PRIVSEP_LEN);
526 memcpy(&ireq, imsg->data, datalen);
528 if (ireq.client_is_reading) {
529 err = ensure_client_is_not_writing(client);
530 if (err)
531 return err;
532 repo = gotd_find_repo_by_name(ireq.repo_name, &gotd);
533 if (repo == NULL)
534 return got_error(GOT_ERR_NOT_GIT_REPO);
535 err = start_auth_child(client, GOTD_AUTH_READ, repo,
536 gotd.argv0, gotd.confpath, gotd.daemonize,
537 gotd.verbosity);
538 if (err)
539 return err;
540 } else {
541 err = ensure_client_is_not_reading(client);
542 if (err)
543 return err;
544 repo = gotd_find_repo_by_name(ireq.repo_name, &gotd);
545 if (repo == NULL)
546 return got_error(GOT_ERR_NOT_GIT_REPO);
547 err = start_auth_child(client,
548 GOTD_AUTH_READ | GOTD_AUTH_WRITE,
549 repo, gotd.argv0, gotd.confpath, gotd.daemonize,
550 gotd.verbosity);
551 if (err)
552 return err;
555 evtimer_add(&client->tmo, &auth_timeout);
557 /* Flow continues upon authentication success/failure or timeout. */
558 return NULL;
561 static void
562 gotd_request(int fd, short events, void *arg)
564 struct gotd_imsgev *iev = arg;
565 struct imsgbuf *ibuf = &iev->ibuf;
566 struct gotd_client *client = iev->handler_arg;
567 const struct got_error *err = NULL;
568 struct imsg imsg;
569 ssize_t n;
571 if (events & EV_WRITE) {
572 while (ibuf->w.queued) {
573 n = msgbuf_write(&ibuf->w);
574 if (n == -1 && errno == EPIPE) {
575 /*
576 * The client has closed its socket.
577 * This can happen when Git clients are
578 * done sending pack file data.
579 */
580 msgbuf_clear(&ibuf->w);
581 continue;
582 } else if (n == -1 && errno != EAGAIN) {
583 err = got_error_from_errno("imsg_flush");
584 disconnect_on_error(client, err);
585 return;
587 if (n == 0) {
588 /* Connection closed. */
589 err = got_error(GOT_ERR_EOF);
590 disconnect_on_error(client, err);
591 return;
595 /* Disconnect gotctl(8) now that messages have been sent. */
596 if (!client_is_reading(client) && !client_is_writing(client)) {
597 disconnect(client);
598 return;
602 if ((events & EV_READ) == 0)
603 return;
605 memset(&imsg, 0, sizeof(imsg));
607 while (err == NULL) {
608 err = gotd_imsg_recv(&imsg, ibuf, 0);
609 if (err) {
610 if (err->code == GOT_ERR_PRIVSEP_READ)
611 err = NULL;
612 break;
615 evtimer_del(&client->tmo);
617 switch (imsg.hdr.type) {
618 case GOTD_IMSG_INFO:
619 err = send_info(client);
620 break;
621 case GOTD_IMSG_STOP:
622 err = stop_gotd(client);
623 break;
624 case GOTD_IMSG_LIST_REFS:
625 err = start_client_authentication(client, &imsg);
626 break;
627 default:
628 log_debug("unexpected imsg %d", imsg.hdr.type);
629 err = got_error(GOT_ERR_PRIVSEP_MSG);
630 break;
633 imsg_free(&imsg);
636 if (err) {
637 disconnect_on_error(client, err);
638 } else {
639 gotd_imsg_event_add(&client->iev);
643 static void
644 gotd_auth_timeout(int fd, short events, void *arg)
646 struct gotd_client *client = arg;
648 log_debug("disconnecting uid %d due to authentication timeout",
649 client->euid);
650 disconnect(client);
653 static const struct got_error *
654 recv_connect(uint32_t *client_id, struct imsg *imsg)
656 const struct got_error *err = NULL;
657 struct gotd_imsg_connect iconnect;
658 size_t datalen;
659 int s = -1;
660 struct gotd_client *client = NULL;
662 *client_id = 0;
664 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
665 if (datalen != sizeof(iconnect))
666 return got_error(GOT_ERR_PRIVSEP_LEN);
667 memcpy(&iconnect, imsg->data, sizeof(iconnect));
669 s = imsg->fd;
670 if (s == -1) {
671 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
672 goto done;
675 if (find_client(iconnect.client_id)) {
676 err = got_error_msg(GOT_ERR_CLIENT_ID, "duplicate client ID");
677 goto done;
680 client = calloc(1, sizeof(*client));
681 if (client == NULL) {
682 err = got_error_from_errno("calloc");
683 goto done;
686 *client_id = iconnect.client_id;
688 client->state = GOTD_CLIENT_STATE_NEW;
689 client->id = iconnect.client_id;
690 client->fd = s;
691 s = -1;
692 /* The auth process will verify UID/GID for us. */
693 client->euid = iconnect.euid;
694 client->egid = iconnect.egid;
696 imsg_init(&client->iev.ibuf, client->fd);
697 client->iev.handler = gotd_request;
698 client->iev.events = EV_READ;
699 client->iev.handler_arg = client;
701 event_set(&client->iev.ev, client->fd, EV_READ, gotd_request,
702 &client->iev);
703 gotd_imsg_event_add(&client->iev);
705 evtimer_set(&client->tmo, gotd_auth_timeout, client);
707 add_client(client);
708 log_debug("%s: new client uid %d connected on fd %d", __func__,
709 client->euid, client->fd);
710 done:
711 if (err) {
712 struct gotd_child_proc *listen_proc = gotd.listen_proc;
713 struct gotd_imsg_disconnect idisconnect;
715 idisconnect.client_id = client->id;
716 if (gotd_imsg_compose_event(&listen_proc->iev,
717 GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
718 &idisconnect, sizeof(idisconnect)) == -1)
719 log_warn("imsg compose DISCONNECT");
721 if (s != -1)
722 close(s);
725 return err;
728 static const char *gotd_proc_names[PROC_MAX] = {
729 "parent",
730 "listen",
731 "auth",
732 "session_read",
733 "session_write",
734 "repo_read",
735 "repo_write",
736 "gitwrapper"
737 };
739 static void
740 kill_proc(struct gotd_child_proc *proc, int fatal)
742 struct timeval tv = { 5, 0 };
744 log_debug("kill -%d %d", fatal ? SIGKILL : SIGTERM, proc->pid);
746 if (proc->iev.ibuf.fd != -1) {
747 event_del(&proc->iev.ev);
748 msgbuf_clear(&proc->iev.ibuf.w);
749 close(proc->iev.ibuf.fd);
750 proc->iev.ibuf.fd = -1;
753 if (!evtimer_pending(&proc->tmo, NULL) && !fatal)
754 evtimer_add(&proc->tmo, &tv);
756 if (fatal) {
757 log_warnx("sending SIGKILL to PID %d", proc->pid);
758 kill(proc->pid, SIGKILL);
759 } else
760 kill(proc->pid, SIGTERM);
763 static void
764 kill_proc_timeout(int fd, short ev, void *d)
766 struct gotd_child_proc *proc = d;
768 log_warnx("timeout waiting for PID %d to terminate;"
769 " retrying with force", proc->pid);
770 kill_proc(proc, 1);
773 static void
774 gotd_shutdown(void)
776 uint64_t slot;
778 log_debug("shutting down");
779 for (slot = 0; slot < nitems(gotd_clients); slot++) {
780 struct gotd_client *c, *tmp;
782 STAILQ_FOREACH_SAFE(c, &gotd_clients[slot], entry, tmp)
783 disconnect(c);
786 kill_proc(gotd.listen_proc, 0);
788 log_info("terminating");
789 exit(0);
792 static struct gotd_child_proc *
793 find_proc_by_pid(pid_t pid)
795 struct gotd_child_proc *proc = NULL;
797 TAILQ_FOREACH(proc, &procs, entry)
798 if (proc->pid == pid)
799 break;
801 return proc;
804 void
805 gotd_sighdlr(int sig, short event, void *arg)
807 struct gotd_child_proc *proc;
808 pid_t pid;
809 int status;
811 /*
812 * Normal signal handler rules don't apply because libevent
813 * decouples for us.
814 */
816 switch (sig) {
817 case SIGHUP:
818 log_info("%s: ignoring SIGHUP", __func__);
819 break;
820 case SIGUSR1:
821 log_info("%s: ignoring SIGUSR1", __func__);
822 break;
823 case SIGTERM:
824 case SIGINT:
825 gotd_shutdown();
826 break;
827 case SIGCHLD:
828 for (;;) {
829 pid = waitpid(WAIT_ANY, &status, WNOHANG);
830 if (pid == -1) {
831 if (errno == EINTR)
832 continue;
833 if (errno == ECHILD)
834 break;
835 fatal("waitpid");
837 if (pid == 0)
838 break;
840 log_debug("reaped pid %d", pid);
841 proc = find_proc_by_pid(pid);
842 if (proc == NULL) {
843 log_info("caught exit of unknown child %d",
844 pid);
845 continue;
848 if (WIFSIGNALED(status)) {
849 log_warnx("child PID %d terminated with"
850 " signal %d", pid, WTERMSIG(status));
853 proc_done(proc);
855 break;
856 default:
857 fatalx("unexpected signal");
861 static const struct got_error *
862 ensure_proc_is_reading(struct gotd_client *client,
863 struct gotd_child_proc *proc)
865 if (!client_is_reading(client)) {
866 kill_proc(proc, 1);
867 return got_error_fmt(GOT_ERR_BAD_PACKET,
868 "PID %d handled a read-request for uid %d but this "
869 "user is not reading from a repository", proc->pid,
870 client->euid);
873 return NULL;
876 static const struct got_error *
877 ensure_proc_is_writing(struct gotd_client *client,
878 struct gotd_child_proc *proc)
880 if (!client_is_writing(client)) {
881 kill_proc(proc, 1);
882 return got_error_fmt(GOT_ERR_BAD_PACKET,
883 "PID %d handled a write-request for uid %d but this "
884 "user is not writing to a repository", proc->pid,
885 client->euid);
888 return NULL;
891 static int
892 verify_imsg_src(struct gotd_client *client, struct gotd_child_proc *proc,
893 struct imsg *imsg)
895 const struct got_error *err;
896 int ret = 0;
898 if (proc->type == PROC_REPO_READ || proc->type == PROC_REPO_WRITE) {
899 if (client->repo == NULL)
900 fatalx("no process found for uid %d", client->euid);
901 if (proc->pid != client->repo->pid) {
902 kill_proc(proc, 1);
903 log_warnx("received message from PID %d for uid %d, "
904 "while PID %d is the process serving this user",
905 proc->pid, client->euid, client->repo->pid);
906 return 0;
909 if (proc->type == PROC_SESSION_READ ||
910 proc->type == PROC_SESSION_WRITE) {
911 if (client->session == NULL) {
912 log_warnx("no session found for uid %d", client->euid);
913 return 0;
915 if (proc->pid != client->session->pid) {
916 kill_proc(proc, 1);
917 log_warnx("received message from PID %d for uid %d, "
918 "while PID %d is the process serving this user",
919 proc->pid, client->euid, client->session->pid);
920 return 0;
924 switch (imsg->hdr.type) {
925 case GOTD_IMSG_ERROR:
926 ret = 1;
927 break;
928 case GOTD_IMSG_CONNECT:
929 if (proc->type != PROC_LISTEN) {
930 err = got_error_fmt(GOT_ERR_BAD_PACKET,
931 "new connection for uid %d from PID %d "
932 "which is not the listen process",
933 proc->pid, client->euid);
934 } else
935 ret = 1;
936 break;
937 case GOTD_IMSG_ACCESS_GRANTED:
938 if (proc->type != PROC_AUTH) {
939 err = got_error_fmt(GOT_ERR_BAD_PACKET,
940 "authentication of uid %d from PID %d "
941 "which is not the auth process",
942 proc->pid, client->euid);
943 } else
944 ret = 1;
945 break;
946 case GOTD_IMSG_CLIENT_SESSION_READY:
947 if (proc->type != PROC_SESSION_READ &&
948 proc->type != PROC_SESSION_WRITE) {
949 err = got_error_fmt(GOT_ERR_BAD_PACKET,
950 "unexpected \"ready\" signal from PID %d",
951 proc->pid);
952 } else
953 ret = 1;
954 break;
955 case GOTD_IMSG_REPO_CHILD_READY:
956 if (proc->type != PROC_REPO_READ &&
957 proc->type != PROC_REPO_WRITE) {
958 err = got_error_fmt(GOT_ERR_BAD_PACKET,
959 "unexpected \"ready\" signal from PID %d",
960 proc->pid);
961 } else
962 ret = 1;
963 break;
964 case GOTD_IMSG_PACKFILE_DONE:
965 err = ensure_proc_is_reading(client, proc);
966 if (err)
967 log_warnx("uid %d: %s", client->euid, err->msg);
968 else
969 ret = 1;
970 break;
971 case GOTD_IMSG_PACKFILE_INSTALL:
972 case GOTD_IMSG_REF_UPDATES_START:
973 case GOTD_IMSG_REF_UPDATE:
974 err = ensure_proc_is_writing(client, proc);
975 if (err)
976 log_warnx("uid %d: %s", client->euid, err->msg);
977 else
978 ret = 1;
979 break;
980 default:
981 log_debug("%s: unexpected imsg %d", __func__, imsg->hdr.type);
982 break;
985 return ret;
988 static const struct got_error *
989 connect_repo_child(struct gotd_client *client,
990 struct gotd_child_proc *repo_proc)
992 static const struct got_error *err;
993 struct gotd_imsgev *session_iev = &client->session->iev;
994 struct gotd_imsg_connect_repo_child ireq;
995 int pipe[2];
997 if (client->state != GOTD_CLIENT_STATE_ACCESS_GRANTED)
998 return got_error_msg(GOT_ERR_BAD_REQUEST,
999 "unexpected repo child ready signal received");
1001 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1002 PF_UNSPEC, pipe) == -1)
1003 fatal("socketpair");
1005 memset(&ireq, 0, sizeof(ireq));
1006 ireq.client_id = client->id;
1007 ireq.proc_id = repo_proc->type;
1009 /* Pass repo child pipe to session child process. */
1010 if (gotd_imsg_compose_event(session_iev, GOTD_IMSG_CONNECT_REPO_CHILD,
1011 PROC_GOTD, pipe[0], &ireq, sizeof(ireq)) == -1) {
1012 err = got_error_from_errno("imsg compose CONNECT_REPO_CHILD");
1013 close(pipe[0]);
1014 close(pipe[1]);
1015 return err;
1018 /* Pass session child pipe to repo child process. */
1019 if (gotd_imsg_compose_event(&repo_proc->iev,
1020 GOTD_IMSG_CONNECT_REPO_CHILD, PROC_GOTD, pipe[1], NULL, 0) == -1) {
1021 err = got_error_from_errno("imsg compose CONNECT_REPO_CHILD");
1022 close(pipe[1]);
1023 return err;
1026 return NULL;
1029 static void
1030 gotd_dispatch_listener(int fd, short event, void *arg)
1032 struct gotd_imsgev *iev = arg;
1033 struct imsgbuf *ibuf = &iev->ibuf;
1034 struct gotd_child_proc *proc = gotd.listen_proc;
1035 ssize_t n;
1036 int shut = 0;
1037 struct imsg imsg;
1039 if (proc->iev.ibuf.fd != fd)
1040 fatalx("%s: unexpected fd %d", __func__, fd);
1042 if (event & EV_READ) {
1043 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1044 fatal("imsg_read error");
1045 if (n == 0) {
1046 /* Connection closed. */
1047 shut = 1;
1048 goto done;
1052 if (event & EV_WRITE) {
1053 n = msgbuf_write(&ibuf->w);
1054 if (n == -1 && errno != EAGAIN)
1055 fatal("msgbuf_write");
1056 if (n == 0) {
1057 /* Connection closed. */
1058 shut = 1;
1059 goto done;
1063 for (;;) {
1064 const struct got_error *err = NULL;
1065 struct gotd_client *client = NULL;
1066 uint32_t client_id = 0;
1067 int do_disconnect = 0;
1069 if ((n = imsg_get(ibuf, &imsg)) == -1)
1070 fatal("%s: imsg_get error", __func__);
1071 if (n == 0) /* No more messages. */
1072 break;
1074 switch (imsg.hdr.type) {
1075 case GOTD_IMSG_ERROR:
1076 do_disconnect = 1;
1077 err = gotd_imsg_recv_error(&client_id, &imsg);
1078 break;
1079 case GOTD_IMSG_CONNECT:
1080 err = recv_connect(&client_id, &imsg);
1081 break;
1082 default:
1083 log_debug("unexpected imsg %d", imsg.hdr.type);
1084 break;
1087 client = find_client(client_id);
1088 if (client == NULL) {
1089 log_warnx("%s: client not found", __func__);
1090 imsg_free(&imsg);
1091 continue;
1094 if (err)
1095 log_warnx("uid %d: %s", client->euid, err->msg);
1097 if (do_disconnect) {
1098 if (err)
1099 disconnect_on_error(client, err);
1100 else
1101 disconnect(client);
1104 imsg_free(&imsg);
1106 done:
1107 if (!shut) {
1108 gotd_imsg_event_add(iev);
1109 } else {
1110 /* This pipe is dead. Remove its event handler */
1111 event_del(&iev->ev);
1112 event_loopexit(NULL);
1116 static void
1117 gotd_dispatch_auth_child(int fd, short event, void *arg)
1119 const struct got_error *err = NULL;
1120 struct gotd_imsgev *iev = arg;
1121 struct imsgbuf *ibuf = &iev->ibuf;
1122 struct gotd_client *client;
1123 struct gotd_repo *repo = NULL;
1124 ssize_t n;
1125 int shut = 0;
1126 struct imsg imsg;
1127 uint32_t client_id = 0;
1128 int do_disconnect = 0;
1130 client = find_client_by_proc_fd(fd);
1131 if (client == NULL) {
1132 /* Can happen during process teardown. */
1133 warnx("cannot find client for fd %d", fd);
1134 shut = 1;
1135 goto done;
1138 if (client->auth == NULL)
1139 fatalx("cannot find auth child process for fd %d", fd);
1141 if (event & EV_READ) {
1142 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1143 fatal("imsg_read error");
1144 if (n == 0) {
1145 /* Connection closed. */
1146 shut = 1;
1147 goto done;
1151 if (event & EV_WRITE) {
1152 n = msgbuf_write(&ibuf->w);
1153 if (n == -1 && errno != EAGAIN)
1154 fatal("msgbuf_write");
1155 if (n == 0) {
1156 /* Connection closed. */
1157 shut = 1;
1159 goto done;
1162 if (client->auth->iev.ibuf.fd != fd)
1163 fatalx("%s: unexpected fd %d", __func__, fd);
1165 if ((n = imsg_get(ibuf, &imsg)) == -1)
1166 fatal("%s: imsg_get error", __func__);
1167 if (n == 0) /* No more messages. */
1168 return;
1170 evtimer_del(&client->tmo);
1172 switch (imsg.hdr.type) {
1173 case GOTD_IMSG_ERROR:
1174 do_disconnect = 1;
1175 err = gotd_imsg_recv_error(&client_id, &imsg);
1176 break;
1177 case GOTD_IMSG_ACCESS_GRANTED:
1178 client->state = GOTD_CLIENT_STATE_ACCESS_GRANTED;
1179 break;
1180 default:
1181 do_disconnect = 1;
1182 log_debug("unexpected imsg %d", imsg.hdr.type);
1183 break;
1186 if (!verify_imsg_src(client, client->auth, &imsg)) {
1187 do_disconnect = 1;
1188 log_debug("dropping imsg type %d from PID %d",
1189 imsg.hdr.type, client->auth->pid);
1191 imsg_free(&imsg);
1193 if (do_disconnect) {
1194 if (err)
1195 disconnect_on_error(client, err);
1196 else
1197 disconnect(client);
1198 return;
1201 repo = gotd_find_repo_by_name(client->auth->repo_name, &gotd);
1202 if (repo == NULL) {
1203 err = got_error(GOT_ERR_NOT_GIT_REPO);
1204 goto done;
1206 kill_auth_proc(client);
1208 log_info("authenticated uid %d for repository %s",
1209 client->euid, repo->name);
1211 err = start_session_child(client, repo, gotd.argv0,
1212 gotd.confpath, gotd.daemonize, gotd.verbosity);
1213 if (err)
1214 goto done;
1215 done:
1216 if (err)
1217 log_warnx("uid %d: %s", client->euid, err->msg);
1219 /* We might have killed the auth process by now. */
1220 if (client->auth != NULL) {
1221 if (!shut) {
1222 gotd_imsg_event_add(iev);
1223 } else {
1224 /* This pipe is dead. Remove its event handler */
1225 event_del(&iev->ev);
1230 static const struct got_error *
1231 connect_session(struct gotd_client *client)
1233 const struct got_error *err = NULL;
1234 struct gotd_imsg_connect iconnect;
1235 int s;
1237 memset(&iconnect, 0, sizeof(iconnect));
1239 s = dup(client->fd);
1240 if (s == -1)
1241 return got_error_from_errno("dup");
1243 iconnect.client_id = client->id;
1244 iconnect.euid = client->euid;
1245 iconnect.egid = client->egid;
1247 if (gotd_imsg_compose_event(&client->session->iev, GOTD_IMSG_CONNECT,
1248 PROC_GOTD, s, &iconnect, sizeof(iconnect)) == -1) {
1249 err = got_error_from_errno("imsg compose CONNECT");
1250 close(s);
1251 return err;
1255 * We are no longer interested in messages from this client.
1256 * Further client requests will be handled by the session process.
1258 msgbuf_clear(&client->iev.ibuf.w);
1259 imsg_clear(&client->iev.ibuf);
1260 event_del(&client->iev.ev);
1261 client->fd = -1; /* will be closed via copy in client->iev.ibuf.fd */
1263 return NULL;
1266 static void
1267 gotd_dispatch_client_session(int fd, short event, void *arg)
1269 struct gotd_imsgev *iev = arg;
1270 struct imsgbuf *ibuf = &iev->ibuf;
1271 struct gotd_child_proc *proc = NULL;
1272 struct gotd_client *client = NULL;
1273 ssize_t n;
1274 int shut = 0;
1275 struct imsg imsg;
1277 client = find_client_by_proc_fd(fd);
1278 if (client == NULL) {
1279 /* Can happen during process teardown. */
1280 warnx("cannot find client for fd %d", fd);
1281 shut = 1;
1282 goto done;
1285 if (event & EV_READ) {
1286 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1287 fatal("imsg_read error");
1288 if (n == 0) {
1289 /* Connection closed. */
1290 shut = 1;
1291 goto done;
1295 if (event & EV_WRITE) {
1296 n = msgbuf_write(&ibuf->w);
1297 if (n == -1 && errno != EAGAIN)
1298 fatal("msgbuf_write");
1299 if (n == 0) {
1300 /* Connection closed. */
1301 shut = 1;
1302 goto done;
1306 proc = client->session;
1307 if (proc == NULL)
1308 fatalx("cannot find session child process for fd %d", fd);
1310 for (;;) {
1311 const struct got_error *err = NULL;
1312 uint32_t client_id = 0;
1313 int do_disconnect = 0, do_start_repo_child = 0;
1315 if ((n = imsg_get(ibuf, &imsg)) == -1)
1316 fatal("%s: imsg_get error", __func__);
1317 if (n == 0) /* No more messages. */
1318 break;
1320 switch (imsg.hdr.type) {
1321 case GOTD_IMSG_ERROR:
1322 do_disconnect = 1;
1323 err = gotd_imsg_recv_error(&client_id, &imsg);
1324 break;
1325 case GOTD_IMSG_CLIENT_SESSION_READY:
1326 if (client->state != GOTD_CLIENT_STATE_ACCESS_GRANTED) {
1327 err = got_error(GOT_ERR_PRIVSEP_MSG);
1328 break;
1330 do_start_repo_child = 1;
1331 break;
1332 case GOTD_IMSG_DISCONNECT:
1333 do_disconnect = 1;
1334 break;
1335 default:
1336 log_debug("unexpected imsg %d", imsg.hdr.type);
1337 break;
1340 if (!verify_imsg_src(client, proc, &imsg)) {
1341 log_debug("dropping imsg type %d from PID %d",
1342 imsg.hdr.type, proc->pid);
1343 imsg_free(&imsg);
1344 continue;
1346 if (err)
1347 log_warnx("uid %d: %s", client->euid, err->msg);
1349 if (do_start_repo_child) {
1350 struct gotd_repo *repo;
1351 const char *name = client->session->repo_name;
1353 repo = gotd_find_repo_by_name(name, &gotd);
1354 if (repo != NULL) {
1355 enum gotd_procid proc_type;
1357 if (client->required_auth & GOTD_AUTH_WRITE)
1358 proc_type = PROC_REPO_WRITE;
1359 else
1360 proc_type = PROC_REPO_READ;
1362 err = start_repo_child(client, proc_type, repo,
1363 gotd.argv0, gotd.confpath, gotd.daemonize,
1364 gotd.verbosity);
1365 } else
1366 err = got_error(GOT_ERR_NOT_GIT_REPO);
1368 if (err) {
1369 log_warnx("uid %d: %s", client->euid, err->msg);
1370 do_disconnect = 1;
1374 if (do_disconnect) {
1375 if (err)
1376 disconnect_on_error(client, err);
1377 else
1378 disconnect(client);
1381 imsg_free(&imsg);
1383 done:
1384 if (!shut) {
1385 gotd_imsg_event_add(iev);
1386 } else {
1387 /* This pipe is dead. Remove its event handler */
1388 event_del(&iev->ev);
1389 disconnect(client);
1393 static void
1394 gotd_dispatch_repo_child(int fd, short event, void *arg)
1396 struct gotd_imsgev *iev = arg;
1397 struct imsgbuf *ibuf = &iev->ibuf;
1398 struct gotd_child_proc *proc = NULL;
1399 struct gotd_client *client;
1400 ssize_t n;
1401 int shut = 0;
1402 struct imsg imsg;
1404 client = find_client_by_proc_fd(fd);
1405 if (client == NULL) {
1406 /* Can happen during process teardown. */
1407 warnx("cannot find client for fd %d", fd);
1408 shut = 1;
1409 goto done;
1412 if (event & EV_READ) {
1413 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1414 fatal("imsg_read error");
1415 if (n == 0) {
1416 /* Connection closed. */
1417 shut = 1;
1418 goto done;
1422 if (event & EV_WRITE) {
1423 n = msgbuf_write(&ibuf->w);
1424 if (n == -1 && errno != EAGAIN)
1425 fatal("msgbuf_write");
1426 if (n == 0) {
1427 /* Connection closed. */
1428 shut = 1;
1429 goto done;
1433 proc = client->repo;
1434 if (proc == NULL)
1435 fatalx("cannot find child process for fd %d", fd);
1437 for (;;) {
1438 const struct got_error *err = NULL;
1439 uint32_t client_id = 0;
1440 int do_disconnect = 0;
1442 if ((n = imsg_get(ibuf, &imsg)) == -1)
1443 fatal("%s: imsg_get error", __func__);
1444 if (n == 0) /* No more messages. */
1445 break;
1447 switch (imsg.hdr.type) {
1448 case GOTD_IMSG_ERROR:
1449 do_disconnect = 1;
1450 err = gotd_imsg_recv_error(&client_id, &imsg);
1451 break;
1452 case GOTD_IMSG_REPO_CHILD_READY:
1453 err = connect_session(client);
1454 if (err)
1455 break;
1456 err = connect_repo_child(client, proc);
1457 break;
1458 default:
1459 log_debug("unexpected imsg %d", imsg.hdr.type);
1460 break;
1463 if (!verify_imsg_src(client, proc, &imsg)) {
1464 log_debug("dropping imsg type %d from PID %d",
1465 imsg.hdr.type, proc->pid);
1466 imsg_free(&imsg);
1467 continue;
1469 if (err)
1470 log_warnx("uid %d: %s", client->euid, err->msg);
1472 if (do_disconnect) {
1473 if (err)
1474 disconnect_on_error(client, err);
1475 else
1476 disconnect(client);
1479 imsg_free(&imsg);
1481 done:
1482 if (!shut) {
1483 gotd_imsg_event_add(iev);
1484 } else {
1485 /* This pipe is dead. Remove its event handler */
1486 event_del(&iev->ev);
1487 disconnect(client);
1491 static pid_t
1492 start_child(enum gotd_procid proc_id, const char *repo_path,
1493 char *argv0, const char *confpath, int fd, int daemonize, int verbosity)
1495 char *argv[11];
1496 int argc = 0;
1497 pid_t pid;
1499 switch (pid = fork()) {
1500 case -1:
1501 fatal("cannot fork");
1502 case 0:
1503 break;
1504 default:
1505 close(fd);
1506 return pid;
1509 if (fd != GOTD_FILENO_MSG_PIPE) {
1510 if (dup2(fd, GOTD_FILENO_MSG_PIPE) == -1)
1511 fatal("cannot setup imsg fd");
1512 } else if (fcntl(fd, F_SETFD, 0) == -1)
1513 fatal("cannot setup imsg fd");
1515 argv[argc++] = argv0;
1516 switch (proc_id) {
1517 case PROC_LISTEN:
1518 argv[argc++] = (char *)"-L";
1519 break;
1520 case PROC_AUTH:
1521 argv[argc++] = (char *)"-A";
1522 break;
1523 case PROC_SESSION_READ:
1524 argv[argc++] = (char *)"-s";
1525 break;
1526 case PROC_SESSION_WRITE:
1527 argv[argc++] = (char *)"-S";
1528 break;
1529 case PROC_REPO_READ:
1530 argv[argc++] = (char *)"-R";
1531 break;
1532 case PROC_REPO_WRITE:
1533 argv[argc++] = (char *)"-W";
1534 break;
1535 default:
1536 fatalx("invalid process id %d", proc_id);
1539 argv[argc++] = (char *)"-f";
1540 argv[argc++] = (char *)confpath;
1542 if (repo_path) {
1543 argv[argc++] = (char *)"-P";
1544 argv[argc++] = (char *)repo_path;
1547 if (!daemonize)
1548 argv[argc++] = (char *)"-d";
1549 if (verbosity > 0)
1550 argv[argc++] = (char *)"-v";
1551 if (verbosity > 1)
1552 argv[argc++] = (char *)"-v";
1553 argv[argc++] = NULL;
1555 execvp(argv0, argv);
1556 fatal("execvp");
1559 static void
1560 start_listener(char *argv0, const char *confpath, int daemonize, int verbosity)
1562 struct gotd_child_proc *proc;
1564 proc = calloc(1, sizeof(*proc));
1565 if (proc == NULL)
1566 fatal("calloc");
1568 TAILQ_INSERT_HEAD(&procs, proc, entry);
1570 /* proc->tmo is initialized in main() after event_init() */
1572 proc->type = PROC_LISTEN;
1574 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1575 PF_UNSPEC, proc->pipe) == -1)
1576 fatal("socketpair");
1578 proc->pid = start_child(proc->type, NULL, argv0, confpath,
1579 proc->pipe[1], daemonize, verbosity);
1580 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1581 proc->iev.handler = gotd_dispatch_listener;
1582 proc->iev.events = EV_READ;
1583 proc->iev.handler_arg = NULL;
1585 gotd.listen_proc = proc;
1588 static const struct got_error *
1589 start_session_child(struct gotd_client *client, struct gotd_repo *repo,
1590 char *argv0, const char *confpath, int daemonize, int verbosity)
1592 struct gotd_child_proc *proc;
1594 proc = calloc(1, sizeof(*proc));
1595 if (proc == NULL)
1596 return got_error_from_errno("calloc");
1598 TAILQ_INSERT_HEAD(&procs, proc, entry);
1599 evtimer_set(&proc->tmo, kill_proc_timeout, proc);
1601 if (client_is_reading(client))
1602 proc->type = PROC_SESSION_READ;
1603 else
1604 proc->type = PROC_SESSION_WRITE;
1605 if (strlcpy(proc->repo_name, repo->name,
1606 sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
1607 fatalx("repository name too long: %s", repo->name);
1608 log_debug("starting client uid %d session for repository %s",
1609 client->euid, repo->name);
1610 if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >=
1611 sizeof(proc->repo_path))
1612 fatalx("repository path too long: %s", repo->path);
1613 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1614 PF_UNSPEC, proc->pipe) == -1)
1615 fatal("socketpair");
1616 proc->pid = start_child(proc->type, proc->repo_path, argv0,
1617 confpath, proc->pipe[1], daemonize, verbosity);
1618 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1619 log_debug("proc %s %s is on fd %d",
1620 gotd_proc_names[proc->type], proc->repo_path,
1621 proc->pipe[0]);
1622 proc->iev.handler = gotd_dispatch_client_session;
1623 proc->iev.events = EV_READ;
1624 proc->iev.handler_arg = NULL;
1625 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1626 gotd_dispatch_client_session, &proc->iev);
1627 gotd_imsg_event_add(&proc->iev);
1629 client->session = proc;
1630 return NULL;
1633 static const struct got_error *
1634 start_repo_child(struct gotd_client *client, enum gotd_procid proc_type,
1635 struct gotd_repo *repo, char *argv0, const char *confpath,
1636 int daemonize, int verbosity)
1638 struct gotd_child_proc *proc;
1640 if (proc_type != PROC_REPO_READ && proc_type != PROC_REPO_WRITE)
1641 return got_error_msg(GOT_ERR_NOT_IMPL, "bad process type");
1643 proc = calloc(1, sizeof(*proc));
1644 if (proc == NULL)
1645 return got_error_from_errno("calloc");
1647 TAILQ_INSERT_HEAD(&procs, proc, entry);
1648 evtimer_set(&proc->tmo, kill_proc_timeout, proc);
1650 proc->type = proc_type;
1651 if (strlcpy(proc->repo_name, repo->name,
1652 sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
1653 fatalx("repository name too long: %s", repo->name);
1654 log_debug("starting %s for repository %s",
1655 proc->type == PROC_REPO_READ ? "reader" : "writer", repo->name);
1656 if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >=
1657 sizeof(proc->repo_path))
1658 fatalx("repository path too long: %s", repo->path);
1659 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1660 PF_UNSPEC, proc->pipe) == -1)
1661 fatal("socketpair");
1662 proc->pid = start_child(proc->type, proc->repo_path, argv0,
1663 confpath, proc->pipe[1], daemonize, verbosity);
1664 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1665 log_debug("proc %s %s is on fd %d",
1666 gotd_proc_names[proc->type], proc->repo_path,
1667 proc->pipe[0]);
1668 proc->iev.handler = gotd_dispatch_repo_child;
1669 proc->iev.events = EV_READ;
1670 proc->iev.handler_arg = NULL;
1671 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1672 gotd_dispatch_repo_child, &proc->iev);
1673 gotd_imsg_event_add(&proc->iev);
1675 client->repo = proc;
1676 return NULL;
1679 static const struct got_error *
1680 start_auth_child(struct gotd_client *client, int required_auth,
1681 struct gotd_repo *repo, char *argv0, const char *confpath,
1682 int daemonize, int verbosity)
1684 const struct got_error *err = NULL;
1685 struct gotd_child_proc *proc;
1686 struct gotd_imsg_auth iauth;
1687 int fd;
1689 memset(&iauth, 0, sizeof(iauth));
1691 fd = dup(client->fd);
1692 if (fd == -1)
1693 return got_error_from_errno("dup");
1695 proc = calloc(1, sizeof(*proc));
1696 if (proc == NULL) {
1697 err = got_error_from_errno("calloc");
1698 close(fd);
1699 return err;
1702 TAILQ_INSERT_HEAD(&procs, proc, entry);
1703 evtimer_set(&proc->tmo, kill_proc_timeout, proc);
1705 proc->type = PROC_AUTH;
1706 if (strlcpy(proc->repo_name, repo->name,
1707 sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
1708 fatalx("repository name too long: %s", repo->name);
1709 log_debug("starting auth for uid %d repository %s",
1710 client->euid, repo->name);
1711 if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >=
1712 sizeof(proc->repo_path))
1713 fatalx("repository path too long: %s", repo->path);
1714 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1715 PF_UNSPEC, proc->pipe) == -1)
1716 fatal("socketpair");
1717 proc->pid = start_child(proc->type, proc->repo_path, argv0,
1718 confpath, proc->pipe[1], daemonize, verbosity);
1719 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1720 log_debug("proc %s %s is on fd %d",
1721 gotd_proc_names[proc->type], proc->repo_path,
1722 proc->pipe[0]);
1723 proc->iev.handler = gotd_dispatch_auth_child;
1724 proc->iev.events = EV_READ;
1725 proc->iev.handler_arg = NULL;
1726 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1727 gotd_dispatch_auth_child, &proc->iev);
1728 gotd_imsg_event_add(&proc->iev);
1730 iauth.euid = client->euid;
1731 iauth.egid = client->egid;
1732 iauth.required_auth = required_auth;
1733 iauth.client_id = client->id;
1734 if (gotd_imsg_compose_event(&proc->iev, GOTD_IMSG_AUTHENTICATE,
1735 PROC_GOTD, fd, &iauth, sizeof(iauth)) == -1) {
1736 log_warn("imsg compose AUTHENTICATE");
1737 close(fd);
1738 /* Let the auth_timeout handler tidy up. */
1741 client->auth = proc;
1742 client->required_auth = required_auth;
1743 return NULL;
1746 static void
1747 apply_unveil_repo_readonly(const char *repo_path, int need_tmpdir)
1749 if (need_tmpdir) {
1750 if (unveil(GOT_TMPDIR_STR, "rwc") == -1)
1751 fatal("unveil %s", GOT_TMPDIR_STR);
1754 if (unveil(repo_path, "r") == -1)
1755 fatal("unveil %s", repo_path);
1757 if (unveil(NULL, NULL) == -1)
1758 fatal("unveil");
1761 static void
1762 apply_unveil_repo_readwrite(const char *repo_path)
1764 if (unveil(repo_path, "rwc") == -1)
1765 fatal("unveil %s", repo_path);
1767 if (unveil(GOT_TMPDIR_STR, "rwc") == -1)
1768 fatal("unveil %s", GOT_TMPDIR_STR);
1770 if (unveil(NULL, NULL) == -1)
1771 fatal("unveil");
1774 static void
1775 apply_unveil_none(void)
1777 if (unveil("/", "") == -1)
1778 fatal("unveil");
1780 if (unveil(NULL, NULL) == -1)
1781 fatal("unveil");
1784 static void
1785 apply_unveil_selfexec(void)
1787 if (unveil(gotd.argv0, "x") == -1)
1788 fatal("unveil %s", gotd.argv0);
1790 if (unveil(NULL, NULL) == -1)
1791 fatal("unveil");
1794 static void
1795 set_max_datasize(void)
1797 struct rlimit rl;
1799 if (getrlimit(RLIMIT_DATA, &rl) != 0)
1800 return;
1802 rl.rlim_cur = rl.rlim_max;
1803 setrlimit(RLIMIT_DATA, &rl);
1806 int
1807 main(int argc, char **argv)
1809 const struct got_error *error = NULL;
1810 int ch, fd = -1, daemonize = 1, verbosity = 0, noaction = 0;
1811 const char *confpath = GOTD_CONF_PATH;
1812 char *argv0 = argv[0];
1813 char title[2048];
1814 struct passwd *pw = NULL;
1815 char *repo_path = NULL;
1816 enum gotd_procid proc_id = PROC_GOTD;
1817 struct event evsigint, evsigterm, evsighup, evsigusr1, evsigchld;
1818 int *pack_fds = NULL, *temp_fds = NULL;
1819 struct gotd_repo *repo = NULL;
1821 TAILQ_INIT(&procs);
1823 log_init(1, LOG_DAEMON); /* Log to stderr until daemonized. */
1825 while ((ch = getopt(argc, argv, "Adf:LnP:RsSvW")) != -1) {
1826 switch (ch) {
1827 case 'A':
1828 proc_id = PROC_AUTH;
1829 break;
1830 case 'd':
1831 daemonize = 0;
1832 break;
1833 case 'f':
1834 confpath = optarg;
1835 break;
1836 case 'L':
1837 proc_id = PROC_LISTEN;
1838 break;
1839 case 'n':
1840 noaction = 1;
1841 break;
1842 case 'P':
1843 repo_path = realpath(optarg, NULL);
1844 if (repo_path == NULL)
1845 fatal("realpath '%s'", optarg);
1846 break;
1847 case 'R':
1848 proc_id = PROC_REPO_READ;
1849 break;
1850 case 's':
1851 proc_id = PROC_SESSION_READ;
1852 break;
1853 case 'S':
1854 proc_id = PROC_SESSION_WRITE;
1855 break;
1856 case 'v':
1857 if (verbosity < 3)
1858 verbosity++;
1859 break;
1860 case 'W':
1861 proc_id = PROC_REPO_WRITE;
1862 break;
1863 default:
1864 usage();
1868 argc -= optind;
1869 argv += optind;
1871 if (argc != 0)
1872 usage();
1874 if (geteuid() && (proc_id == PROC_GOTD || proc_id == PROC_LISTEN))
1875 fatalx("need root privileges");
1877 if (parse_config(confpath, proc_id, &gotd) != 0)
1878 return 1;
1880 pw = getpwnam(gotd.user_name);
1881 if (pw == NULL)
1882 fatalx("user %s not found", gotd.user_name);
1884 if (pw->pw_uid == 0)
1885 fatalx("cannot run %s as the superuser", getprogname());
1887 if (noaction) {
1888 fprintf(stderr, "configuration OK\n");
1889 return 0;
1892 gotd.argv0 = argv0;
1893 gotd.daemonize = daemonize;
1894 gotd.verbosity = verbosity;
1895 gotd.confpath = confpath;
1897 /* Require an absolute path in argv[0] for reliable re-exec. */
1898 if (!got_path_is_absolute(argv0))
1899 fatalx("bad path \"%s\": must be an absolute path", argv0);
1901 log_init(daemonize ? 0 : 1, LOG_DAEMON);
1902 log_setverbose(verbosity);
1904 if (proc_id == PROC_GOTD) {
1905 snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
1906 arc4random_buf(&clients_hash_key, sizeof(clients_hash_key));
1907 if (daemonize && daemon(1, 0) == -1)
1908 fatal("daemon");
1909 gotd.pid = getpid();
1910 start_listener(argv0, confpath, daemonize, verbosity);
1911 } else if (proc_id == PROC_LISTEN) {
1912 snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
1913 if (verbosity) {
1914 log_info("socket: %s", gotd.unix_socket_path);
1915 log_info("user: %s", pw->pw_name);
1918 fd = unix_socket_listen(gotd.unix_socket_path, pw->pw_uid,
1919 pw->pw_gid);
1920 if (fd == -1) {
1921 fatal("cannot listen on unix socket %s",
1922 gotd.unix_socket_path);
1924 } else if (proc_id == PROC_AUTH) {
1925 snprintf(title, sizeof(title), "%s %s",
1926 gotd_proc_names[proc_id], repo_path);
1927 } else if (proc_id == PROC_REPO_READ || proc_id == PROC_REPO_WRITE ||
1928 proc_id == PROC_SESSION_READ || proc_id == PROC_SESSION_WRITE) {
1929 error = got_repo_pack_fds_open(&pack_fds);
1930 if (error != NULL)
1931 fatalx("cannot open pack tempfiles: %s", error->msg);
1932 error = got_repo_temp_fds_open(&temp_fds);
1933 if (error != NULL)
1934 fatalx("cannot open pack tempfiles: %s", error->msg);
1935 if (repo_path == NULL)
1936 fatalx("repository path not specified");
1937 snprintf(title, sizeof(title), "%s %s",
1938 gotd_proc_names[proc_id], repo_path);
1939 } else
1940 fatal("invalid process id %d", proc_id);
1942 setproctitle("%s", title);
1943 log_procinit(title);
1945 /* Drop root privileges. */
1946 if (setgid(pw->pw_gid) == -1)
1947 fatal("setgid %d failed", pw->pw_gid);
1948 if (setuid(pw->pw_uid) == -1)
1949 fatal("setuid %d failed", pw->pw_uid);
1951 event_init();
1953 switch (proc_id) {
1954 case PROC_GOTD:
1955 #ifndef PROFILE
1956 /* "exec" promise will be limited to argv[0] via unveil(2). */
1957 if (pledge("stdio proc exec sendfd recvfd unveil", NULL) == -1)
1958 err(1, "pledge");
1959 #endif
1960 break;
1961 case PROC_LISTEN:
1962 #ifndef PROFILE
1963 if (pledge("stdio sendfd unix unveil", NULL) == -1)
1964 err(1, "pledge");
1965 #endif
1967 * Ensure that AF_UNIX bind(2) cannot be used with any other
1968 * sockets by revoking all filesystem access via unveil(2).
1970 apply_unveil_none();
1972 listen_main(title, fd, gotd.connection_limits,
1973 gotd.nconnection_limits);
1974 /* NOTREACHED */
1975 break;
1976 case PROC_AUTH:
1977 #ifndef PROFILE
1978 if (pledge("stdio getpw recvfd unix unveil", NULL) == -1)
1979 err(1, "pledge");
1980 #endif
1982 * We need the "unix" pledge promise for getpeername(2) only.
1983 * Ensure that AF_UNIX bind(2) cannot be used by revoking all
1984 * filesystem access via unveil(2). Access to password database
1985 * files will still work since "getpw" bypasses unveil(2).
1987 apply_unveil_none();
1989 auth_main(title, &gotd.repos, repo_path);
1990 /* NOTREACHED */
1991 break;
1992 case PROC_SESSION_READ:
1993 case PROC_SESSION_WRITE:
1994 #ifndef PROFILE
1996 * The "recvfd" promise is only needed during setup and
1997 * will be removed in a later pledge(2) call.
1999 if (pledge("stdio rpath wpath cpath recvfd sendfd fattr flock "
2000 "unveil", NULL) == -1)
2001 err(1, "pledge");
2002 #endif
2003 if (proc_id == PROC_SESSION_READ)
2004 apply_unveil_repo_readonly(repo_path, 1);
2005 else
2006 apply_unveil_repo_readwrite(repo_path);
2007 session_main(title, repo_path, pack_fds, temp_fds,
2008 &gotd.request_timeout, proc_id);
2009 /* NOTREACHED */
2010 break;
2011 case PROC_REPO_READ:
2012 set_max_datasize();
2013 #ifndef PROFILE
2014 if (pledge("stdio rpath recvfd unveil", NULL) == -1)
2015 err(1, "pledge");
2016 #endif
2017 apply_unveil_repo_readonly(repo_path, 0);
2018 repo_read_main(title, repo_path, pack_fds, temp_fds);
2019 /* NOTREACHED */
2020 exit(0);
2021 case PROC_REPO_WRITE:
2022 set_max_datasize();
2023 #ifndef PROFILE
2024 if (pledge("stdio rpath recvfd unveil", NULL) == -1)
2025 err(1, "pledge");
2026 #endif
2027 apply_unveil_repo_readonly(repo_path, 0);
2028 repo = gotd_find_repo_by_path(repo_path, &gotd);
2029 if (repo == NULL)
2030 fatalx("no repository for path %s", repo_path);
2031 repo_write_main(title, repo_path, pack_fds, temp_fds,
2032 &repo->protected_tag_namespaces,
2033 &repo->protected_branch_namespaces,
2034 &repo->protected_branches);
2035 /* NOTREACHED */
2036 exit(0);
2037 default:
2038 fatal("invalid process id %d", proc_id);
2041 if (proc_id != PROC_GOTD)
2042 fatal("invalid process id %d", proc_id);
2044 evtimer_set(&gotd.listen_proc->tmo, kill_proc_timeout,
2045 gotd.listen_proc);
2047 apply_unveil_selfexec();
2049 signal_set(&evsigint, SIGINT, gotd_sighdlr, NULL);
2050 signal_set(&evsigterm, SIGTERM, gotd_sighdlr, NULL);
2051 signal_set(&evsighup, SIGHUP, gotd_sighdlr, NULL);
2052 signal_set(&evsigusr1, SIGUSR1, gotd_sighdlr, NULL);
2053 signal_set(&evsigchld, SIGCHLD, gotd_sighdlr, NULL);
2054 signal(SIGPIPE, SIG_IGN);
2056 signal_add(&evsigint, NULL);
2057 signal_add(&evsigterm, NULL);
2058 signal_add(&evsighup, NULL);
2059 signal_add(&evsigusr1, NULL);
2060 signal_add(&evsigchld, NULL);
2062 gotd_imsg_event_add(&gotd.listen_proc->iev);
2064 event_dispatch();
2066 free(repo_path);
2067 gotd_shutdown();
2069 return 0;