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 "got_compat.h"
19 #include <sys/queue.h>
20 #include <sys/time.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <sys/socket.h>
24 #include <sys/un.h>
25 #include <sys/wait.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 <signal.h>
35 #include <stdarg.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <syslog.h>
40 #include <unistd.h>
42 #include "got_error.h"
43 #include "got_opentemp.h"
44 #include "got_path.h"
45 #include "got_repository.h"
46 #include "got_object.h"
47 #include "got_reference.h"
49 #include "got_lib_delta.h"
50 #include "got_lib_object.h"
51 #include "got_lib_object_cache.h"
52 #include "got_lib_hash.h"
53 #include "got_lib_gitproto.h"
54 #include "got_lib_pack.h"
55 #include "got_lib_repository.h"
57 #include "gotd.h"
58 #include "log.h"
59 #include "listen.h"
60 #include "auth.h"
61 #include "session.h"
62 #include "repo_read.h"
63 #include "repo_write.h"
65 #ifndef nitems
66 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
67 #endif
69 enum gotd_client_state {
70 GOTD_CLIENT_STATE_NEW,
71 GOTD_CLIENT_STATE_ACCESS_GRANTED,
72 };
74 struct gotd_child_proc {
75 pid_t pid;
76 enum gotd_procid type;
77 char repo_name[NAME_MAX];
78 char repo_path[PATH_MAX];
79 int pipe[2];
80 struct gotd_imsgev iev;
81 struct event tmo;
83 TAILQ_ENTRY(gotd_child_proc) entry;
84 };
85 TAILQ_HEAD(gotd_procs, gotd_child_proc) procs;
87 struct gotd_client {
88 STAILQ_ENTRY(gotd_client) entry;
89 enum gotd_client_state state;
90 uint32_t id;
91 int fd;
92 struct gotd_imsgev iev;
93 struct event tmo;
94 uid_t euid;
95 gid_t egid;
96 struct gotd_child_proc *repo;
97 struct gotd_child_proc *auth;
98 struct gotd_child_proc *session;
99 int required_auth;
100 };
101 STAILQ_HEAD(gotd_clients, gotd_client);
103 static struct gotd_clients gotd_clients[GOTD_CLIENT_TABLE_SIZE];
104 static SIPHASH_KEY clients_hash_key;
105 volatile int client_cnt;
106 static struct timeval auth_timeout = { 5, 0 };
107 static struct gotd gotd;
109 void gotd_sighdlr(int sig, short event, void *arg);
110 static void gotd_shutdown(void);
111 static const struct got_error *start_session_child(struct gotd_client *,
112 struct gotd_repo *, char *, const char *, int, int);
113 static const struct got_error *start_repo_child(struct gotd_client *,
114 enum gotd_procid, struct gotd_repo *, char *, const char *, int, int);
115 static const struct got_error *start_auth_child(struct gotd_client *, int,
116 struct gotd_repo *, char *, const char *, int, int);
117 static void kill_proc(struct gotd_child_proc *, int);
118 static void disconnect(struct gotd_client *);
119 static void drop_privs(struct passwd *);
121 __dead static void
122 usage(void)
124 fprintf(stderr, "usage: %s [-dnv] [-f config-file]\n", getprogname());
125 exit(1);
128 static int
129 unix_socket_listen(const char *unix_socket_path, uid_t uid, gid_t gid)
131 struct sockaddr_un sun;
132 int fd = -1;
133 mode_t old_umask, mode;
134 int sock_flags = SOCK_STREAM | SOCK_NONBLOCK;
136 #ifdef SOCK_CLOEXEC
137 sock_flags |= SOCK_CLOEXEC;
138 #endif
140 fd = socket(AF_UNIX, sock_flags, 0);
141 if (fd == -1) {
142 log_warn("socket");
143 return -1;
146 sun.sun_family = AF_UNIX;
147 if (strlcpy(sun.sun_path, unix_socket_path,
148 sizeof(sun.sun_path)) >= sizeof(sun.sun_path)) {
149 log_warnx("%s: name too long", unix_socket_path);
150 close(fd);
151 return -1;
154 if (unlink(unix_socket_path) == -1) {
155 if (errno != ENOENT) {
156 log_warn("unlink %s", unix_socket_path);
157 close(fd);
158 return -1;
162 old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
163 mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
165 if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
166 log_warn("bind: %s", unix_socket_path);
167 close(fd);
168 umask(old_umask);
169 return -1;
172 umask(old_umask);
174 if (chmod(unix_socket_path, mode) == -1) {
175 log_warn("chmod %o %s", mode, unix_socket_path);
176 close(fd);
177 unlink(unix_socket_path);
178 return -1;
181 if (chown(unix_socket_path, uid, gid) == -1) {
182 log_warn("chown %s uid=%d gid=%d", unix_socket_path, uid, gid);
183 close(fd);
184 unlink(unix_socket_path);
185 return -1;
188 if (listen(fd, GOTD_UNIX_SOCKET_BACKLOG) == -1) {
189 log_warn("listen");
190 close(fd);
191 unlink(unix_socket_path);
192 return -1;
195 return fd;
198 static uint64_t
199 client_hash(uint32_t client_id)
201 return SipHash24(&clients_hash_key, &client_id, sizeof(client_id));
204 static void
205 add_client(struct gotd_client *client)
207 uint64_t slot = client_hash(client->id) % nitems(gotd_clients);
208 STAILQ_INSERT_HEAD(&gotd_clients[slot], client, entry);
209 client_cnt++;
212 static struct gotd_client *
213 find_client(uint32_t client_id)
215 uint64_t slot;
216 struct gotd_client *c;
218 slot = client_hash(client_id) % nitems(gotd_clients);
219 STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
220 if (c->id == client_id)
221 return c;
224 return NULL;
227 static struct gotd_client *
228 find_client_by_proc_fd(int fd)
230 uint64_t slot;
232 for (slot = 0; slot < nitems(gotd_clients); slot++) {
233 struct gotd_client *c;
235 STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
236 if (c->repo && c->repo->iev.ibuf.fd == fd)
237 return c;
238 if (c->auth && c->auth->iev.ibuf.fd == fd)
239 return c;
240 if (c->session && c->session->iev.ibuf.fd == fd)
241 return c;
245 return NULL;
248 static int
249 client_is_reading(struct gotd_client *client)
251 return (client->required_auth &
252 (GOTD_AUTH_READ | GOTD_AUTH_WRITE)) == GOTD_AUTH_READ;
255 static int
256 client_is_writing(struct gotd_client *client)
258 return (client->required_auth &
259 (GOTD_AUTH_READ | GOTD_AUTH_WRITE)) ==
260 (GOTD_AUTH_READ | GOTD_AUTH_WRITE);
263 static const struct got_error *
264 ensure_client_is_not_writing(struct gotd_client *client)
266 if (client_is_writing(client)) {
267 return got_error_fmt(GOT_ERR_BAD_PACKET,
268 "uid %d made a read-request but is writing to "
269 "a repository", client->euid);
272 return NULL;
275 static const struct got_error *
276 ensure_client_is_not_reading(struct gotd_client *client)
278 if (client_is_reading(client)) {
279 return got_error_fmt(GOT_ERR_BAD_PACKET,
280 "uid %d made a write-request but is reading from "
281 "a repository", client->euid);
284 return NULL;
287 static void
288 proc_done(struct gotd_child_proc *proc)
290 struct gotd_client *client;
292 TAILQ_REMOVE(&procs, proc, entry);
294 client = find_client_by_proc_fd(proc->iev.ibuf.fd);
295 if (client != NULL) {
296 if (proc == client->repo)
297 client->repo = NULL;
298 if (proc == client->auth)
299 client->auth = NULL;
300 if (proc == client->session)
301 client->session = NULL;
302 disconnect(client);
305 evtimer_del(&proc->tmo);
307 if (proc->iev.ibuf.fd != -1) {
308 event_del(&proc->iev.ev);
309 msgbuf_clear(&proc->iev.ibuf.w);
310 close(proc->iev.ibuf.fd);
313 free(proc);
316 static void
317 kill_repo_proc(struct gotd_client *client)
319 if (client->repo == NULL)
320 return;
322 kill_proc(client->repo, 0);
323 client->repo = NULL;
326 static void
327 kill_auth_proc(struct gotd_client *client)
329 if (client->auth == NULL)
330 return;
332 kill_proc(client->auth, 0);
333 client->auth = NULL;
336 static void
337 kill_session_proc(struct gotd_client *client)
339 if (client->session == NULL)
340 return;
342 kill_proc(client->session, 0);
343 client->session = NULL;
346 static void
347 disconnect(struct gotd_client *client)
349 struct gotd_imsg_disconnect idisconnect;
350 struct gotd_child_proc *listen_proc = gotd.listen_proc;
351 uint64_t slot;
353 log_debug("uid %d: disconnecting", client->euid);
355 kill_auth_proc(client);
356 kill_session_proc(client);
357 kill_repo_proc(client);
359 idisconnect.client_id = client->id;
360 if (gotd_imsg_compose_event(&listen_proc->iev,
361 GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
362 &idisconnect, sizeof(idisconnect)) == -1)
363 log_warn("imsg compose DISCONNECT");
365 slot = client_hash(client->id) % nitems(gotd_clients);
366 STAILQ_REMOVE(&gotd_clients[slot], client, gotd_client, entry);
367 imsg_clear(&client->iev.ibuf);
368 event_del(&client->iev.ev);
369 evtimer_del(&client->tmo);
370 if (client->fd != -1)
371 close(client->fd);
372 else if (client->iev.ibuf.fd != -1)
373 close(client->iev.ibuf.fd);
374 free(client);
375 client_cnt--;
378 static void
379 disconnect_on_error(struct gotd_client *client, const struct got_error *err)
381 struct imsgbuf ibuf;
383 if (err->code != GOT_ERR_EOF) {
384 log_warnx("uid %d: %s", client->euid, err->msg);
385 if (client->fd != -1) {
386 imsg_init(&ibuf, client->fd);
387 gotd_imsg_send_error(&ibuf, 0, PROC_GOTD, err);
388 imsg_clear(&ibuf);
391 disconnect(client);
394 static const struct got_error *
395 send_repo_info(struct gotd_imsgev *iev, struct gotd_repo *repo)
397 const struct got_error *err = NULL;
398 struct gotd_imsg_info_repo irepo;
400 memset(&irepo, 0, sizeof(irepo));
402 if (strlcpy(irepo.repo_name, repo->name, sizeof(irepo.repo_name))
403 >= sizeof(irepo.repo_name))
404 return got_error_msg(GOT_ERR_NO_SPACE, "repo name too long");
405 if (strlcpy(irepo.repo_path, repo->path, sizeof(irepo.repo_path))
406 >= sizeof(irepo.repo_path))
407 return got_error_msg(GOT_ERR_NO_SPACE, "repo path too long");
409 if (gotd_imsg_compose_event(iev, GOTD_IMSG_INFO_REPO, PROC_GOTD, -1,
410 &irepo, sizeof(irepo)) == -1) {
411 err = got_error_from_errno("imsg compose INFO_REPO");
412 if (err)
413 return err;
416 return NULL;
419 static const struct got_error *
420 send_client_info(struct gotd_imsgev *iev, struct gotd_client *client)
422 const struct got_error *err = NULL;
423 struct gotd_imsg_info_client iclient;
424 struct gotd_child_proc *proc;
426 memset(&iclient, 0, sizeof(iclient));
427 iclient.euid = client->euid;
428 iclient.egid = client->egid;
430 proc = client->repo;
431 if (proc) {
432 if (strlcpy(iclient.repo_name, proc->repo_path,
433 sizeof(iclient.repo_name)) >= sizeof(iclient.repo_name)) {
434 return got_error_msg(GOT_ERR_NO_SPACE,
435 "repo name too long");
437 if (client_is_writing(client))
438 iclient.is_writing = 1;
440 iclient.repo_child_pid = proc->pid;
443 if (client->session)
444 iclient.session_child_pid = client->session->pid;
446 if (gotd_imsg_compose_event(iev, GOTD_IMSG_INFO_CLIENT, PROC_GOTD, -1,
447 &iclient, sizeof(iclient)) == -1) {
448 err = got_error_from_errno("imsg compose INFO_CLIENT");
449 if (err)
450 return err;
453 return NULL;
456 static const struct got_error *
457 send_info(struct gotd_client *client)
459 const struct got_error *err = NULL;
460 struct gotd_imsg_info info;
461 uint64_t slot;
462 struct gotd_repo *repo;
464 if (client->euid != 0)
465 return got_error_set_errno(EPERM, "info");
467 info.pid = gotd.pid;
468 info.verbosity = gotd.verbosity;
469 info.nrepos = gotd.nrepos;
470 info.nclients = client_cnt - 1;
472 if (gotd_imsg_compose_event(&client->iev, GOTD_IMSG_INFO, PROC_GOTD, -1,
473 &info, sizeof(info)) == -1) {
474 err = got_error_from_errno("imsg compose INFO");
475 if (err)
476 return err;
479 TAILQ_FOREACH(repo, &gotd.repos, entry) {
480 err = send_repo_info(&client->iev, repo);
481 if (err)
482 return err;
485 for (slot = 0; slot < nitems(gotd_clients); slot++) {
486 struct gotd_client *c;
487 STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
488 if (c->id == client->id)
489 continue;
490 err = send_client_info(&client->iev, c);
491 if (err)
492 return err;
496 return NULL;
499 static const struct got_error *
500 stop_gotd(struct gotd_client *client)
503 if (client->euid != 0)
504 return got_error_set_errno(EPERM, "stop");
506 gotd_shutdown();
507 /* NOTREACHED */
508 return NULL;
511 static const struct got_error *
512 start_client_authentication(struct gotd_client *client, struct imsg *imsg)
514 const struct got_error *err;
515 struct gotd_imsg_list_refs ireq;
516 struct gotd_repo *repo = NULL;
517 size_t datalen;
519 log_debug("list-refs request from uid %d", client->euid);
521 if (client->state != GOTD_CLIENT_STATE_NEW)
522 return got_error_msg(GOT_ERR_BAD_REQUEST,
523 "unexpected list-refs request received");
525 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
526 if (datalen != sizeof(ireq))
527 return got_error(GOT_ERR_PRIVSEP_LEN);
529 memcpy(&ireq, imsg->data, datalen);
531 if (ireq.client_is_reading) {
532 err = ensure_client_is_not_writing(client);
533 if (err)
534 return err;
535 repo = gotd_find_repo_by_name(ireq.repo_name, &gotd);
536 if (repo == NULL)
537 return got_error(GOT_ERR_NOT_GIT_REPO);
538 err = start_auth_child(client, GOTD_AUTH_READ, repo,
539 gotd.argv0, gotd.confpath, gotd.daemonize,
540 gotd.verbosity);
541 if (err)
542 return err;
543 } else {
544 err = ensure_client_is_not_reading(client);
545 if (err)
546 return err;
547 repo = gotd_find_repo_by_name(ireq.repo_name, &gotd);
548 if (repo == NULL)
549 return got_error(GOT_ERR_NOT_GIT_REPO);
550 err = start_auth_child(client,
551 GOTD_AUTH_READ | GOTD_AUTH_WRITE,
552 repo, gotd.argv0, gotd.confpath, gotd.daemonize,
553 gotd.verbosity);
554 if (err)
555 return err;
558 evtimer_add(&client->tmo, &auth_timeout);
560 /* Flow continues upon authentication success/failure or timeout. */
561 return NULL;
564 static void
565 gotd_request(int fd, short events, void *arg)
567 struct gotd_imsgev *iev = arg;
568 struct imsgbuf *ibuf = &iev->ibuf;
569 struct gotd_client *client = iev->handler_arg;
570 const struct got_error *err = NULL;
571 struct imsg imsg;
572 ssize_t n;
574 if (events & EV_WRITE) {
575 while (ibuf->w.queued) {
576 n = msgbuf_write(&ibuf->w);
577 if (n == -1 && errno == EPIPE) {
578 /*
579 * The client has closed its socket.
580 * This can happen when Git clients are
581 * done sending pack file data.
582 */
583 msgbuf_clear(&ibuf->w);
584 continue;
585 } else if (n == -1 && errno != EAGAIN) {
586 err = got_error_from_errno("imsg_flush");
587 disconnect_on_error(client, err);
588 return;
590 if (n == 0) {
591 /* Connection closed. */
592 err = got_error(GOT_ERR_EOF);
593 disconnect_on_error(client, err);
594 return;
598 /* Disconnect gotctl(8) now that messages have been sent. */
599 if (!client_is_reading(client) && !client_is_writing(client)) {
600 disconnect(client);
601 return;
605 if ((events & EV_READ) == 0)
606 return;
608 memset(&imsg, 0, sizeof(imsg));
610 while (err == NULL) {
611 err = gotd_imsg_recv(&imsg, ibuf, 0);
612 if (err) {
613 if (err->code == GOT_ERR_PRIVSEP_READ)
614 err = NULL;
615 break;
618 evtimer_del(&client->tmo);
620 switch (imsg.hdr.type) {
621 case GOTD_IMSG_INFO:
622 err = send_info(client);
623 break;
624 case GOTD_IMSG_STOP:
625 err = stop_gotd(client);
626 break;
627 case GOTD_IMSG_LIST_REFS:
628 err = start_client_authentication(client, &imsg);
629 break;
630 default:
631 log_debug("unexpected imsg %d", imsg.hdr.type);
632 err = got_error(GOT_ERR_PRIVSEP_MSG);
633 break;
636 imsg_free(&imsg);
639 if (err) {
640 disconnect_on_error(client, err);
641 } else {
642 gotd_imsg_event_add(&client->iev);
646 static void
647 gotd_auth_timeout(int fd, short events, void *arg)
649 struct gotd_client *client = arg;
651 log_debug("disconnecting uid %d due to authentication timeout",
652 client->euid);
653 disconnect(client);
656 static const struct got_error *
657 recv_connect(uint32_t *client_id, struct imsg *imsg)
659 const struct got_error *err = NULL;
660 struct gotd_imsg_connect iconnect;
661 size_t datalen;
662 int s = -1;
663 struct gotd_client *client = NULL;
665 *client_id = 0;
667 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
668 if (datalen != sizeof(iconnect))
669 return got_error(GOT_ERR_PRIVSEP_LEN);
670 memcpy(&iconnect, imsg->data, sizeof(iconnect));
672 s = imsg->fd;
673 if (s == -1) {
674 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
675 goto done;
678 if (find_client(iconnect.client_id)) {
679 err = got_error_msg(GOT_ERR_CLIENT_ID, "duplicate client ID");
680 goto done;
683 client = calloc(1, sizeof(*client));
684 if (client == NULL) {
685 err = got_error_from_errno("calloc");
686 goto done;
689 *client_id = iconnect.client_id;
691 client->state = GOTD_CLIENT_STATE_NEW;
692 client->id = iconnect.client_id;
693 client->fd = s;
694 s = -1;
695 /* The auth process will verify UID/GID for us. */
696 client->euid = iconnect.euid;
697 client->egid = iconnect.egid;
699 imsg_init(&client->iev.ibuf, client->fd);
700 client->iev.handler = gotd_request;
701 client->iev.events = EV_READ;
702 client->iev.handler_arg = client;
704 event_set(&client->iev.ev, client->fd, EV_READ, gotd_request,
705 &client->iev);
706 gotd_imsg_event_add(&client->iev);
708 evtimer_set(&client->tmo, gotd_auth_timeout, client);
710 add_client(client);
711 log_debug("%s: new client uid %d connected on fd %d", __func__,
712 client->euid, client->fd);
713 done:
714 if (err) {
715 struct gotd_child_proc *listen_proc = gotd.listen_proc;
716 struct gotd_imsg_disconnect idisconnect;
718 idisconnect.client_id = client->id;
719 if (gotd_imsg_compose_event(&listen_proc->iev,
720 GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
721 &idisconnect, sizeof(idisconnect)) == -1)
722 log_warn("imsg compose DISCONNECT");
724 if (s != -1)
725 close(s);
728 return err;
731 static const char *gotd_proc_names[PROC_MAX] = {
732 "parent",
733 "listen",
734 "auth",
735 "session_read",
736 "session_write",
737 "repo_read",
738 "repo_write",
739 "gitwrapper"
740 };
742 static void
743 kill_proc(struct gotd_child_proc *proc, int fatal)
745 struct timeval tv = { 5, 0 };
747 log_debug("kill -%d %d", fatal ? SIGKILL : SIGTERM, proc->pid);
749 if (proc->iev.ibuf.fd != -1) {
750 event_del(&proc->iev.ev);
751 msgbuf_clear(&proc->iev.ibuf.w);
752 close(proc->iev.ibuf.fd);
753 proc->iev.ibuf.fd = -1;
756 if (!evtimer_pending(&proc->tmo, NULL) && !fatal)
757 evtimer_add(&proc->tmo, &tv);
759 if (fatal) {
760 log_warnx("sending SIGKILL to PID %d", proc->pid);
761 kill(proc->pid, SIGKILL);
762 } else
763 kill(proc->pid, SIGTERM);
766 static void
767 kill_proc_timeout(int fd, short ev, void *d)
769 struct gotd_child_proc *proc = d;
771 log_warnx("timeout waiting for PID %d to terminate;"
772 " retrying with force", proc->pid);
773 kill_proc(proc, 1);
776 static void
777 gotd_shutdown(void)
779 uint64_t slot;
781 log_debug("shutting down");
782 for (slot = 0; slot < nitems(gotd_clients); slot++) {
783 struct gotd_client *c, *tmp;
785 STAILQ_FOREACH_SAFE(c, &gotd_clients[slot], entry, tmp)
786 disconnect(c);
789 kill_proc(gotd.listen_proc, 0);
791 log_info("terminating");
792 exit(0);
795 static struct gotd_child_proc *
796 find_proc_by_pid(pid_t pid)
798 struct gotd_child_proc *proc = NULL;
800 TAILQ_FOREACH(proc, &procs, entry)
801 if (proc->pid == pid)
802 break;
804 return proc;
807 void
808 gotd_sighdlr(int sig, short event, void *arg)
810 struct gotd_child_proc *proc;
811 pid_t pid;
812 int status;
814 /*
815 * Normal signal handler rules don't apply because libevent
816 * decouples for us.
817 */
819 switch (sig) {
820 case SIGHUP:
821 log_info("%s: ignoring SIGHUP", __func__);
822 break;
823 case SIGUSR1:
824 log_info("%s: ignoring SIGUSR1", __func__);
825 break;
826 case SIGTERM:
827 case SIGINT:
828 gotd_shutdown();
829 break;
830 case SIGCHLD:
831 for (;;) {
832 pid = waitpid(WAIT_ANY, &status, WNOHANG);
833 if (pid == -1) {
834 if (errno == EINTR)
835 continue;
836 if (errno == ECHILD)
837 break;
838 fatal("waitpid");
840 if (pid == 0)
841 break;
843 log_debug("reaped pid %d", pid);
844 proc = find_proc_by_pid(pid);
845 if (proc == NULL) {
846 log_info("caught exit of unknown child %d",
847 pid);
848 continue;
851 if (WIFSIGNALED(status)) {
852 log_warnx("child PID %d terminated with"
853 " signal %d", pid, WTERMSIG(status));
856 proc_done(proc);
858 break;
859 default:
860 fatalx("unexpected signal");
864 static const struct got_error *
865 ensure_proc_is_reading(struct gotd_client *client,
866 struct gotd_child_proc *proc)
868 if (!client_is_reading(client)) {
869 kill_proc(proc, 1);
870 return got_error_fmt(GOT_ERR_BAD_PACKET,
871 "PID %d handled a read-request for uid %d but this "
872 "user is not reading from a repository", proc->pid,
873 client->euid);
876 return NULL;
879 static const struct got_error *
880 ensure_proc_is_writing(struct gotd_client *client,
881 struct gotd_child_proc *proc)
883 if (!client_is_writing(client)) {
884 kill_proc(proc, 1);
885 return got_error_fmt(GOT_ERR_BAD_PACKET,
886 "PID %d handled a write-request for uid %d but this "
887 "user is not writing to a repository", proc->pid,
888 client->euid);
891 return NULL;
894 static int
895 verify_imsg_src(struct gotd_client *client, struct gotd_child_proc *proc,
896 struct imsg *imsg)
898 const struct got_error *err;
899 int ret = 0;
901 if (proc->type == PROC_REPO_READ || proc->type == PROC_REPO_WRITE) {
902 if (client->repo == NULL)
903 fatalx("no process found for uid %d", client->euid);
904 if (proc->pid != client->repo->pid) {
905 kill_proc(proc, 1);
906 log_warnx("received message from PID %d for uid %d, "
907 "while PID %d is the process serving this user",
908 proc->pid, client->euid, client->repo->pid);
909 return 0;
912 if (proc->type == PROC_SESSION_READ ||
913 proc->type == PROC_SESSION_WRITE) {
914 if (client->session == NULL) {
915 log_warnx("no session found for uid %d", client->euid);
916 return 0;
918 if (proc->pid != client->session->pid) {
919 kill_proc(proc, 1);
920 log_warnx("received message from PID %d for uid %d, "
921 "while PID %d is the process serving this user",
922 proc->pid, client->euid, client->session->pid);
923 return 0;
927 switch (imsg->hdr.type) {
928 case GOTD_IMSG_ERROR:
929 ret = 1;
930 break;
931 case GOTD_IMSG_CONNECT:
932 if (proc->type != PROC_LISTEN) {
933 err = got_error_fmt(GOT_ERR_BAD_PACKET,
934 "new connection for uid %d from PID %d "
935 "which is not the listen process",
936 proc->pid, client->euid);
937 } else
938 ret = 1;
939 break;
940 case GOTD_IMSG_ACCESS_GRANTED:
941 if (proc->type != PROC_AUTH) {
942 err = got_error_fmt(GOT_ERR_BAD_PACKET,
943 "authentication of uid %d from PID %d "
944 "which is not the auth process",
945 proc->pid, client->euid);
946 } else
947 ret = 1;
948 break;
949 case GOTD_IMSG_CLIENT_SESSION_READY:
950 if (proc->type != PROC_SESSION_READ &&
951 proc->type != PROC_SESSION_WRITE) {
952 err = got_error_fmt(GOT_ERR_BAD_PACKET,
953 "unexpected \"ready\" signal from PID %d",
954 proc->pid);
955 } else
956 ret = 1;
957 break;
958 case GOTD_IMSG_REPO_CHILD_READY:
959 if (proc->type != PROC_REPO_READ &&
960 proc->type != PROC_REPO_WRITE) {
961 err = got_error_fmt(GOT_ERR_BAD_PACKET,
962 "unexpected \"ready\" signal from PID %d",
963 proc->pid);
964 } else
965 ret = 1;
966 break;
967 case GOTD_IMSG_PACKFILE_DONE:
968 err = ensure_proc_is_reading(client, proc);
969 if (err)
970 log_warnx("uid %d: %s", client->euid, err->msg);
971 else
972 ret = 1;
973 break;
974 case GOTD_IMSG_PACKFILE_INSTALL:
975 case GOTD_IMSG_REF_UPDATES_START:
976 case GOTD_IMSG_REF_UPDATE:
977 err = ensure_proc_is_writing(client, proc);
978 if (err)
979 log_warnx("uid %d: %s", client->euid, err->msg);
980 else
981 ret = 1;
982 break;
983 default:
984 log_debug("%s: unexpected imsg %d", __func__, imsg->hdr.type);
985 break;
988 return ret;
991 static const struct got_error *
992 connect_repo_child(struct gotd_client *client,
993 struct gotd_child_proc *repo_proc)
995 static const struct got_error *err;
996 struct gotd_imsgev *session_iev = &client->session->iev;
997 struct gotd_imsg_connect_repo_child ireq;
998 int pipe[2];
999 int sock_flags = SOCK_STREAM | SOCK_NONBLOCK;
1001 #ifdef SOCK_CLOEXEC
1002 sock_flags |= SOCK_CLOEXEC;
1003 #endif
1005 if (client->state != GOTD_CLIENT_STATE_ACCESS_GRANTED)
1006 return got_error_msg(GOT_ERR_BAD_REQUEST,
1007 "unexpected repo child ready signal received");
1009 if (socketpair(AF_UNIX, sock_flags, PF_UNSPEC, pipe) == -1)
1010 fatal("socketpair");
1012 memset(&ireq, 0, sizeof(ireq));
1013 ireq.client_id = client->id;
1014 ireq.proc_id = repo_proc->type;
1016 /* Pass repo child pipe to session child process. */
1017 if (gotd_imsg_compose_event(session_iev, GOTD_IMSG_CONNECT_REPO_CHILD,
1018 PROC_GOTD, pipe[0], &ireq, sizeof(ireq)) == -1) {
1019 err = got_error_from_errno("imsg compose CONNECT_REPO_CHILD");
1020 close(pipe[0]);
1021 close(pipe[1]);
1022 return err;
1025 /* Pass session child pipe to repo child process. */
1026 if (gotd_imsg_compose_event(&repo_proc->iev,
1027 GOTD_IMSG_CONNECT_REPO_CHILD, PROC_GOTD, pipe[1], NULL, 0) == -1) {
1028 err = got_error_from_errno("imsg compose CONNECT_REPO_CHILD");
1029 close(pipe[1]);
1030 return err;
1033 return NULL;
1036 static void
1037 gotd_dispatch_listener(int fd, short event, void *arg)
1039 struct gotd_imsgev *iev = arg;
1040 struct imsgbuf *ibuf = &iev->ibuf;
1041 struct gotd_child_proc *proc = gotd.listen_proc;
1042 ssize_t n;
1043 int shut = 0;
1044 struct imsg imsg;
1046 if (proc->iev.ibuf.fd != fd)
1047 fatalx("%s: unexpected fd %d", __func__, fd);
1049 if (event & EV_READ) {
1050 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1051 fatal("imsg_read error");
1052 if (n == 0) {
1053 /* Connection closed. */
1054 shut = 1;
1055 goto done;
1059 if (event & EV_WRITE) {
1060 n = msgbuf_write(&ibuf->w);
1061 if (n == -1 && errno != EAGAIN)
1062 fatal("msgbuf_write");
1063 if (n == 0) {
1064 /* Connection closed. */
1065 shut = 1;
1066 goto done;
1070 for (;;) {
1071 const struct got_error *err = NULL;
1072 struct gotd_client *client = NULL;
1073 uint32_t client_id = 0;
1074 int do_disconnect = 0;
1076 if ((n = imsg_get(ibuf, &imsg)) == -1)
1077 fatal("%s: imsg_get error", __func__);
1078 if (n == 0) /* No more messages. */
1079 break;
1081 switch (imsg.hdr.type) {
1082 case GOTD_IMSG_ERROR:
1083 do_disconnect = 1;
1084 err = gotd_imsg_recv_error(&client_id, &imsg);
1085 break;
1086 case GOTD_IMSG_CONNECT:
1087 err = recv_connect(&client_id, &imsg);
1088 break;
1089 default:
1090 log_debug("unexpected imsg %d", imsg.hdr.type);
1091 break;
1094 client = find_client(client_id);
1095 if (client == NULL) {
1096 log_warnx("%s: client not found", __func__);
1097 imsg_free(&imsg);
1098 continue;
1101 if (err)
1102 log_warnx("uid %d: %s", client->euid, err->msg);
1104 if (do_disconnect) {
1105 if (err)
1106 disconnect_on_error(client, err);
1107 else
1108 disconnect(client);
1111 imsg_free(&imsg);
1113 done:
1114 if (!shut) {
1115 gotd_imsg_event_add(iev);
1116 } else {
1117 /* This pipe is dead. Remove its event handler */
1118 event_del(&iev->ev);
1119 event_loopexit(NULL);
1123 static void
1124 gotd_dispatch_auth_child(int fd, short event, void *arg)
1126 const struct got_error *err = NULL;
1127 struct gotd_imsgev *iev = arg;
1128 struct imsgbuf *ibuf = &iev->ibuf;
1129 struct gotd_client *client;
1130 struct gotd_repo *repo = NULL;
1131 ssize_t n;
1132 int shut = 0;
1133 struct imsg imsg;
1134 uint32_t client_id = 0;
1135 int do_disconnect = 0;
1137 client = find_client_by_proc_fd(fd);
1138 if (client == NULL) {
1139 /* Can happen during process teardown. */
1140 warnx("cannot find client for fd %d", fd);
1141 shut = 1;
1142 goto done;
1145 if (client->auth == NULL)
1146 fatalx("cannot find auth child process for fd %d", fd);
1148 if (event & EV_READ) {
1149 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1150 fatal("imsg_read error");
1151 if (n == 0) {
1152 /* Connection closed. */
1153 shut = 1;
1154 goto done;
1158 if (event & EV_WRITE) {
1159 n = msgbuf_write(&ibuf->w);
1160 if (n == -1 && errno != EAGAIN)
1161 fatal("msgbuf_write");
1162 if (n == 0) {
1163 /* Connection closed. */
1164 shut = 1;
1166 goto done;
1169 if (client->auth->iev.ibuf.fd != fd)
1170 fatalx("%s: unexpected fd %d", __func__, fd);
1172 if ((n = imsg_get(ibuf, &imsg)) == -1)
1173 fatal("%s: imsg_get error", __func__);
1174 if (n == 0) /* No more messages. */
1175 return;
1177 evtimer_del(&client->tmo);
1179 switch (imsg.hdr.type) {
1180 case GOTD_IMSG_ERROR:
1181 do_disconnect = 1;
1182 err = gotd_imsg_recv_error(&client_id, &imsg);
1183 break;
1184 case GOTD_IMSG_ACCESS_GRANTED:
1185 client->state = GOTD_CLIENT_STATE_ACCESS_GRANTED;
1186 break;
1187 default:
1188 do_disconnect = 1;
1189 log_debug("unexpected imsg %d", imsg.hdr.type);
1190 break;
1193 if (!verify_imsg_src(client, client->auth, &imsg)) {
1194 do_disconnect = 1;
1195 log_debug("dropping imsg type %d from PID %d",
1196 imsg.hdr.type, client->auth->pid);
1198 imsg_free(&imsg);
1200 if (do_disconnect) {
1201 if (err)
1202 disconnect_on_error(client, err);
1203 else
1204 disconnect(client);
1205 return;
1208 repo = gotd_find_repo_by_name(client->auth->repo_name, &gotd);
1209 if (repo == NULL) {
1210 err = got_error(GOT_ERR_NOT_GIT_REPO);
1211 goto done;
1213 kill_auth_proc(client);
1215 log_info("authenticated uid %d for repository %s",
1216 client->euid, repo->name);
1218 err = start_session_child(client, repo, gotd.argv0,
1219 gotd.confpath, gotd.daemonize, gotd.verbosity);
1220 if (err)
1221 goto done;
1222 done:
1223 if (err)
1224 log_warnx("uid %d: %s", client->euid, err->msg);
1226 /* We might have killed the auth process by now. */
1227 if (client->auth != NULL) {
1228 if (!shut) {
1229 gotd_imsg_event_add(iev);
1230 } else {
1231 /* This pipe is dead. Remove its event handler */
1232 event_del(&iev->ev);
1237 static const struct got_error *
1238 connect_session(struct gotd_client *client)
1240 const struct got_error *err = NULL;
1241 struct gotd_imsg_connect iconnect;
1242 int s;
1244 memset(&iconnect, 0, sizeof(iconnect));
1246 s = dup(client->fd);
1247 if (s == -1)
1248 return got_error_from_errno("dup");
1250 iconnect.client_id = client->id;
1251 iconnect.euid = client->euid;
1252 iconnect.egid = client->egid;
1254 if (gotd_imsg_compose_event(&client->session->iev, GOTD_IMSG_CONNECT,
1255 PROC_GOTD, s, &iconnect, sizeof(iconnect)) == -1) {
1256 err = got_error_from_errno("imsg compose CONNECT");
1257 close(s);
1258 return err;
1262 * We are no longer interested in messages from this client.
1263 * Further client requests will be handled by the session process.
1265 msgbuf_clear(&client->iev.ibuf.w);
1266 imsg_clear(&client->iev.ibuf);
1267 event_del(&client->iev.ev);
1268 client->fd = -1; /* will be closed via copy in client->iev.ibuf.fd */
1270 return NULL;
1273 static void
1274 gotd_dispatch_client_session(int fd, short event, void *arg)
1276 struct gotd_imsgev *iev = arg;
1277 struct imsgbuf *ibuf = &iev->ibuf;
1278 struct gotd_child_proc *proc = NULL;
1279 struct gotd_client *client = NULL;
1280 ssize_t n;
1281 int shut = 0;
1282 struct imsg imsg;
1284 client = find_client_by_proc_fd(fd);
1285 if (client == NULL) {
1286 /* Can happen during process teardown. */
1287 warnx("cannot find client for fd %d", fd);
1288 shut = 1;
1289 goto done;
1292 if (event & EV_READ) {
1293 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1294 fatal("imsg_read error");
1295 if (n == 0) {
1296 /* Connection closed. */
1297 shut = 1;
1298 goto done;
1302 if (event & EV_WRITE) {
1303 n = msgbuf_write(&ibuf->w);
1304 if (n == -1 && errno != EAGAIN)
1305 fatal("msgbuf_write");
1306 if (n == 0) {
1307 /* Connection closed. */
1308 shut = 1;
1309 goto done;
1313 proc = client->session;
1314 if (proc == NULL)
1315 fatalx("cannot find session child process for fd %d", fd);
1317 for (;;) {
1318 const struct got_error *err = NULL;
1319 uint32_t client_id = 0;
1320 int do_disconnect = 0, do_start_repo_child = 0;
1322 if ((n = imsg_get(ibuf, &imsg)) == -1)
1323 fatal("%s: imsg_get error", __func__);
1324 if (n == 0) /* No more messages. */
1325 break;
1327 switch (imsg.hdr.type) {
1328 case GOTD_IMSG_ERROR:
1329 do_disconnect = 1;
1330 err = gotd_imsg_recv_error(&client_id, &imsg);
1331 break;
1332 case GOTD_IMSG_CLIENT_SESSION_READY:
1333 if (client->state != GOTD_CLIENT_STATE_ACCESS_GRANTED) {
1334 err = got_error(GOT_ERR_PRIVSEP_MSG);
1335 break;
1337 do_start_repo_child = 1;
1338 break;
1339 case GOTD_IMSG_DISCONNECT:
1340 do_disconnect = 1;
1341 break;
1342 default:
1343 log_debug("unexpected imsg %d", imsg.hdr.type);
1344 break;
1347 if (!verify_imsg_src(client, proc, &imsg)) {
1348 log_debug("dropping imsg type %d from PID %d",
1349 imsg.hdr.type, proc->pid);
1350 imsg_free(&imsg);
1351 continue;
1353 if (err)
1354 log_warnx("uid %d: %s", client->euid, err->msg);
1356 if (do_start_repo_child) {
1357 struct gotd_repo *repo;
1358 const char *name = client->session->repo_name;
1360 repo = gotd_find_repo_by_name(name, &gotd);
1361 if (repo != NULL) {
1362 enum gotd_procid proc_type;
1364 if (client->required_auth & GOTD_AUTH_WRITE)
1365 proc_type = PROC_REPO_WRITE;
1366 else
1367 proc_type = PROC_REPO_READ;
1369 err = start_repo_child(client, proc_type, repo,
1370 gotd.argv0, gotd.confpath, gotd.daemonize,
1371 gotd.verbosity);
1372 } else
1373 err = got_error(GOT_ERR_NOT_GIT_REPO);
1375 if (err) {
1376 log_warnx("uid %d: %s", client->euid, err->msg);
1377 do_disconnect = 1;
1381 if (do_disconnect) {
1382 if (err)
1383 disconnect_on_error(client, err);
1384 else
1385 disconnect(client);
1388 imsg_free(&imsg);
1390 done:
1391 if (!shut) {
1392 gotd_imsg_event_add(iev);
1393 } else {
1394 /* This pipe is dead. Remove its event handler */
1395 event_del(&iev->ev);
1396 disconnect(client);
1400 static void
1401 gotd_dispatch_repo_child(int fd, short event, void *arg)
1403 struct gotd_imsgev *iev = arg;
1404 struct imsgbuf *ibuf = &iev->ibuf;
1405 struct gotd_child_proc *proc = NULL;
1406 struct gotd_client *client;
1407 ssize_t n;
1408 int shut = 0;
1409 struct imsg imsg;
1411 client = find_client_by_proc_fd(fd);
1412 if (client == NULL) {
1413 /* Can happen during process teardown. */
1414 warnx("cannot find client for fd %d", fd);
1415 shut = 1;
1416 goto done;
1419 if (event & EV_READ) {
1420 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1421 fatal("imsg_read error");
1422 if (n == 0) {
1423 /* Connection closed. */
1424 shut = 1;
1425 goto done;
1429 if (event & EV_WRITE) {
1430 n = msgbuf_write(&ibuf->w);
1431 if (n == -1 && errno != EAGAIN)
1432 fatal("msgbuf_write");
1433 if (n == 0) {
1434 /* Connection closed. */
1435 shut = 1;
1436 goto done;
1440 proc = client->repo;
1441 if (proc == NULL)
1442 fatalx("cannot find child process for fd %d", fd);
1444 for (;;) {
1445 const struct got_error *err = NULL;
1446 uint32_t client_id = 0;
1447 int do_disconnect = 0;
1449 if ((n = imsg_get(ibuf, &imsg)) == -1)
1450 fatal("%s: imsg_get error", __func__);
1451 if (n == 0) /* No more messages. */
1452 break;
1454 switch (imsg.hdr.type) {
1455 case GOTD_IMSG_ERROR:
1456 do_disconnect = 1;
1457 err = gotd_imsg_recv_error(&client_id, &imsg);
1458 break;
1459 case GOTD_IMSG_REPO_CHILD_READY:
1460 err = connect_session(client);
1461 if (err)
1462 break;
1463 err = connect_repo_child(client, proc);
1464 break;
1465 default:
1466 log_debug("unexpected imsg %d", imsg.hdr.type);
1467 break;
1470 if (!verify_imsg_src(client, proc, &imsg)) {
1471 log_debug("dropping imsg type %d from PID %d",
1472 imsg.hdr.type, proc->pid);
1473 imsg_free(&imsg);
1474 continue;
1476 if (err)
1477 log_warnx("uid %d: %s", client->euid, err->msg);
1479 if (do_disconnect) {
1480 if (err)
1481 disconnect_on_error(client, err);
1482 else
1483 disconnect(client);
1486 imsg_free(&imsg);
1488 done:
1489 if (!shut) {
1490 gotd_imsg_event_add(iev);
1491 } else {
1492 /* This pipe is dead. Remove its event handler */
1493 event_del(&iev->ev);
1494 disconnect(client);
1498 static pid_t
1499 start_child(enum gotd_procid proc_id, const char *repo_path,
1500 char *argv0, const char *confpath, int fd, int daemonize, int verbosity)
1502 char *argv[11];
1503 int argc = 0;
1504 pid_t pid;
1506 switch (pid = fork()) {
1507 case -1:
1508 fatal("cannot fork");
1509 case 0:
1510 break;
1511 default:
1512 close(fd);
1513 return pid;
1516 if (fd != GOTD_FILENO_MSG_PIPE) {
1517 if (dup2(fd, GOTD_FILENO_MSG_PIPE) == -1)
1518 fatal("cannot setup imsg fd");
1519 } else if (fcntl(fd, F_SETFD, 0) == -1)
1520 fatal("cannot setup imsg fd");
1522 argv[argc++] = argv0;
1523 switch (proc_id) {
1524 case PROC_LISTEN:
1525 argv[argc++] = (char *)"-L";
1526 break;
1527 case PROC_AUTH:
1528 argv[argc++] = (char *)"-A";
1529 break;
1530 case PROC_SESSION_READ:
1531 argv[argc++] = (char *)"-s";
1532 break;
1533 case PROC_SESSION_WRITE:
1534 argv[argc++] = (char *)"-S";
1535 break;
1536 case PROC_REPO_READ:
1537 argv[argc++] = (char *)"-R";
1538 break;
1539 case PROC_REPO_WRITE:
1540 argv[argc++] = (char *)"-W";
1541 break;
1542 default:
1543 fatalx("invalid process id %d", proc_id);
1546 argv[argc++] = (char *)"-f";
1547 argv[argc++] = (char *)confpath;
1549 if (repo_path) {
1550 argv[argc++] = (char *)"-P";
1551 argv[argc++] = (char *)repo_path;
1554 if (!daemonize)
1555 argv[argc++] = (char *)"-d";
1556 if (verbosity > 0)
1557 argv[argc++] = (char *)"-v";
1558 if (verbosity > 1)
1559 argv[argc++] = (char *)"-v";
1560 argv[argc++] = NULL;
1562 execvp(argv0, argv);
1563 fatal("execvp");
1566 static void
1567 start_listener(char *argv0, const char *confpath, int daemonize, int verbosity)
1569 struct gotd_child_proc *proc;
1570 int sock_flags = SOCK_STREAM|SOCK_NONBLOCK;
1572 #ifdef SOCK_CLOEXEC
1573 sock_flags |= SOCK_CLOEXEC;
1574 #endif
1576 proc = calloc(1, sizeof(*proc));
1577 if (proc == NULL)
1578 fatal("calloc");
1580 TAILQ_INSERT_HEAD(&procs, proc, entry);
1582 /* proc->tmo is initialized in main() after event_init() */
1584 proc->type = PROC_LISTEN;
1586 if (socketpair(AF_UNIX, sock_flags,
1587 PF_UNSPEC, proc->pipe) == -1)
1588 fatal("socketpair");
1590 proc->pid = start_child(proc->type, NULL, argv0, confpath,
1591 proc->pipe[1], daemonize, verbosity);
1592 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1593 proc->iev.handler = gotd_dispatch_listener;
1594 proc->iev.events = EV_READ;
1595 proc->iev.handler_arg = NULL;
1597 gotd.listen_proc = proc;
1600 static const struct got_error *
1601 start_session_child(struct gotd_client *client, struct gotd_repo *repo,
1602 char *argv0, const char *confpath, int daemonize, int verbosity)
1604 struct gotd_child_proc *proc;
1605 int sock_flags = SOCK_STREAM | SOCK_NONBLOCK;
1607 #ifdef SOCK_CLOEXEC
1608 sock_flags |= SOCK_CLOEXEC;
1609 #endif
1611 proc = calloc(1, sizeof(*proc));
1612 if (proc == NULL)
1613 return got_error_from_errno("calloc");
1615 TAILQ_INSERT_HEAD(&procs, proc, entry);
1616 evtimer_set(&proc->tmo, kill_proc_timeout, proc);
1618 if (client_is_reading(client))
1619 proc->type = PROC_SESSION_READ;
1620 else
1621 proc->type = PROC_SESSION_WRITE;
1622 if (strlcpy(proc->repo_name, repo->name,
1623 sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
1624 fatalx("repository name too long: %s", repo->name);
1625 log_debug("starting client uid %d session for repository %s",
1626 client->euid, repo->name);
1627 if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >=
1628 sizeof(proc->repo_path))
1629 fatalx("repository path too long: %s", repo->path);
1630 if (socketpair(AF_UNIX, sock_flags, PF_UNSPEC, proc->pipe) == -1)
1631 fatal("socketpair");
1632 proc->pid = start_child(proc->type, proc->repo_path, argv0,
1633 confpath, proc->pipe[1], daemonize, verbosity);
1634 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1635 log_debug("proc %s %s is on fd %d",
1636 gotd_proc_names[proc->type], proc->repo_path,
1637 proc->pipe[0]);
1638 proc->iev.handler = gotd_dispatch_client_session;
1639 proc->iev.events = EV_READ;
1640 proc->iev.handler_arg = NULL;
1641 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1642 gotd_dispatch_client_session, &proc->iev);
1643 gotd_imsg_event_add(&proc->iev);
1645 client->session = proc;
1646 return NULL;
1649 static const struct got_error *
1650 start_repo_child(struct gotd_client *client, enum gotd_procid proc_type,
1651 struct gotd_repo *repo, char *argv0, const char *confpath,
1652 int daemonize, int verbosity)
1654 struct gotd_child_proc *proc;
1655 int sock_flags = SOCK_STREAM|SOCK_NONBLOCK;
1657 #ifdef SOCK_CLOEXEC
1658 sock_flags |= SOCK_CLOEXEC;
1659 #endif
1661 if (proc_type != PROC_REPO_READ && proc_type != PROC_REPO_WRITE)
1662 return got_error_msg(GOT_ERR_NOT_IMPL, "bad process type");
1664 proc = calloc(1, sizeof(*proc));
1665 if (proc == NULL)
1666 return got_error_from_errno("calloc");
1668 TAILQ_INSERT_HEAD(&procs, proc, entry);
1669 evtimer_set(&proc->tmo, kill_proc_timeout, proc);
1671 proc->type = proc_type;
1672 if (strlcpy(proc->repo_name, repo->name,
1673 sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
1674 fatalx("repository name too long: %s", repo->name);
1675 log_debug("starting %s for repository %s",
1676 proc->type == PROC_REPO_READ ? "reader" : "writer", repo->name);
1678 if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >=
1679 sizeof(proc->repo_path))
1680 fatalx("repository path too long: %s", repo->path);
1681 if (realpath(repo->path, proc->repo_path) == NULL)
1682 fatal("%s", repo->path);
1683 if (socketpair(AF_UNIX, sock_flags,
1684 PF_UNSPEC, proc->pipe) == -1)
1685 fatal("socketpair");
1686 proc->pid = start_child(proc->type, proc->repo_path, argv0,
1687 confpath, proc->pipe[1], daemonize, verbosity);
1688 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1689 log_debug("proc %s %s is on fd %d",
1690 gotd_proc_names[proc->type], proc->repo_path,
1691 proc->pipe[0]);
1692 proc->iev.handler = gotd_dispatch_repo_child;
1693 proc->iev.events = EV_READ;
1694 proc->iev.handler_arg = NULL;
1695 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1696 gotd_dispatch_repo_child, &proc->iev);
1697 gotd_imsg_event_add(&proc->iev);
1699 client->repo = proc;
1700 return NULL;
1703 static const struct got_error *
1704 start_auth_child(struct gotd_client *client, int required_auth,
1705 struct gotd_repo *repo, char *argv0, const char *confpath,
1706 int daemonize, int verbosity)
1708 const struct got_error *err = NULL;
1709 struct gotd_child_proc *proc;
1710 struct gotd_imsg_auth iauth;
1711 int fd;
1712 int sock_flags = SOCK_STREAM|SOCK_NONBLOCK;
1714 #ifdef SOCK_CLOEXEC
1715 sock_flags |= SOCK_CLOEXEC;
1716 #endif
1718 memset(&iauth, 0, sizeof(iauth));
1720 fd = dup(client->fd);
1721 if (fd == -1)
1722 return got_error_from_errno("dup");
1724 proc = calloc(1, sizeof(*proc));
1725 if (proc == NULL) {
1726 err = got_error_from_errno("calloc");
1727 close(fd);
1728 return err;
1731 TAILQ_INSERT_HEAD(&procs, proc, entry);
1732 evtimer_set(&proc->tmo, kill_proc_timeout, proc);
1734 proc->type = PROC_AUTH;
1735 if (strlcpy(proc->repo_name, repo->name,
1736 sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
1737 fatalx("repository name too long: %s", repo->name);
1738 log_debug("starting auth for uid %d repository %s",
1739 client->euid, repo->name);
1740 if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >=
1741 sizeof(proc->repo_path))
1742 fatalx("repository path too long: %s", repo->path);
1743 if (realpath(repo->path, proc->repo_path) == NULL)
1744 fatal("%s", repo->path);
1745 if (socketpair(AF_UNIX, sock_flags,
1746 PF_UNSPEC, proc->pipe) == -1)
1747 fatal("socketpair");
1748 proc->pid = start_child(proc->type, proc->repo_path, argv0,
1749 confpath, proc->pipe[1], daemonize, verbosity);
1750 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1751 log_debug("proc %s %s is on fd %d",
1752 gotd_proc_names[proc->type], proc->repo_path,
1753 proc->pipe[0]);
1754 proc->iev.handler = gotd_dispatch_auth_child;
1755 proc->iev.events = EV_READ;
1756 proc->iev.handler_arg = NULL;
1757 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1758 gotd_dispatch_auth_child, &proc->iev);
1759 gotd_imsg_event_add(&proc->iev);
1761 iauth.euid = client->euid;
1762 iauth.egid = client->egid;
1763 iauth.required_auth = required_auth;
1764 iauth.client_id = client->id;
1765 if (gotd_imsg_compose_event(&proc->iev, GOTD_IMSG_AUTHENTICATE,
1766 PROC_GOTD, fd, &iauth, sizeof(iauth)) == -1) {
1767 log_warn("imsg compose AUTHENTICATE");
1768 close(fd);
1769 /* Let the auth_timeout handler tidy up. */
1772 client->auth = proc;
1773 client->required_auth = required_auth;
1774 return NULL;
1777 static void
1778 apply_unveil_repo_readonly(const char *repo_path, int need_tmpdir)
1780 if (need_tmpdir) {
1781 if (unveil(GOT_TMPDIR_STR, "rwc") == -1)
1782 fatal("unveil %s", GOT_TMPDIR_STR);
1785 if (unveil(repo_path, "r") == -1)
1786 fatal("unveil %s", repo_path);
1788 if (unveil(NULL, NULL) == -1)
1789 fatal("unveil");
1792 static void
1793 apply_unveil_repo_readwrite(const char *repo_path)
1795 if (unveil(repo_path, "rwc") == -1)
1796 fatal("unveil %s", repo_path);
1798 if (unveil(GOT_TMPDIR_STR, "rwc") == -1)
1799 fatal("unveil %s", GOT_TMPDIR_STR);
1801 if (unveil(NULL, NULL) == -1)
1802 fatal("unveil");
1805 static void
1806 apply_unveil_none(void)
1808 if (unveil("/", "") == -1)
1809 fatal("unveil");
1811 if (unveil(NULL, NULL) == -1)
1812 fatal("unveil");
1815 static void
1816 apply_unveil_selfexec(void)
1818 if (unveil(gotd.argv0, "x") == -1)
1819 fatal("unveil %s", gotd.argv0);
1821 if (unveil(NULL, NULL) == -1)
1822 fatal("unveil");
1825 static void
1826 drop_privs(struct passwd *pw)
1828 /* Drop root privileges. */
1829 if (setgid(pw->pw_gid) == -1)
1830 fatal("setgid %d failed", pw->pw_gid);
1831 if (setuid(pw->pw_uid) == -1)
1832 fatal("setuid %d failed", pw->pw_uid);
1835 int
1836 main(int argc, char **argv)
1838 const struct got_error *error = NULL;
1839 int ch, fd = -1, daemonize = 1, verbosity = 0, noaction = 0;
1840 const char *confpath = GOTD_CONF_PATH;
1841 char *argv0 = argv[0];
1842 char title[2048];
1843 struct passwd *pw = NULL;
1844 char *repo_path = NULL;
1845 enum gotd_procid proc_id = PROC_GOTD;
1846 struct event evsigint, evsigterm, evsighup, evsigusr1, evsigchld;
1847 int *pack_fds = NULL, *temp_fds = NULL;
1848 struct gotd_repo *repo = NULL;
1850 TAILQ_INIT(&procs);
1852 log_init(1, LOG_DAEMON); /* Log to stderr until daemonized. */
1854 while ((ch = getopt(argc, argv, "Adf:LnP:RsSvW")) != -1) {
1855 switch (ch) {
1856 case 'A':
1857 proc_id = PROC_AUTH;
1858 break;
1859 case 'd':
1860 daemonize = 0;
1861 break;
1862 case 'f':
1863 confpath = optarg;
1864 break;
1865 case 'L':
1866 proc_id = PROC_LISTEN;
1867 break;
1868 case 'n':
1869 noaction = 1;
1870 break;
1871 case 'P':
1872 repo_path = realpath(optarg, NULL);
1873 if (repo_path == NULL)
1874 fatal("realpath '%s'", optarg);
1875 break;
1876 case 'R':
1877 proc_id = PROC_REPO_READ;
1878 break;
1879 case 's':
1880 proc_id = PROC_SESSION_READ;
1881 break;
1882 case 'S':
1883 proc_id = PROC_SESSION_WRITE;
1884 break;
1885 case 'v':
1886 if (verbosity < 3)
1887 verbosity++;
1888 break;
1889 case 'W':
1890 proc_id = PROC_REPO_WRITE;
1891 break;
1892 default:
1893 usage();
1897 argc -= optind;
1898 argv += optind;
1900 if (argc != 0)
1901 usage();
1903 if (geteuid() && (proc_id == PROC_GOTD || proc_id == PROC_LISTEN))
1904 fatalx("need root privileges");
1906 if (parse_config(confpath, proc_id, &gotd) != 0)
1907 return 1;
1909 pw = getpwnam(gotd.user_name);
1910 if (pw == NULL)
1911 fatalx("user %s not found", gotd.user_name);
1913 if (pw->pw_uid == 0)
1914 fatalx("cannot run %s as the superuser", getprogname());
1916 if (noaction) {
1917 fprintf(stderr, "configuration OK\n");
1918 return 0;
1921 gotd.argv0 = argv0;
1922 gotd.daemonize = daemonize;
1923 gotd.verbosity = verbosity;
1924 gotd.confpath = confpath;
1926 /* Require an absolute path in argv[0] for reliable re-exec. */
1927 if (!got_path_is_absolute(argv0))
1928 fatalx("bad path \"%s\": must be an absolute path", argv0);
1930 log_init(daemonize ? 0 : 1, LOG_DAEMON);
1931 log_setverbose(verbosity);
1933 if (proc_id == PROC_GOTD) {
1934 snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
1935 arc4random_buf(&clients_hash_key, sizeof(clients_hash_key));
1936 if (daemonize && daemon(1, 0) == -1)
1937 fatal("daemon");
1938 gotd.pid = getpid();
1939 start_listener(argv0, confpath, daemonize, verbosity);
1940 } else if (proc_id == PROC_LISTEN) {
1941 snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
1942 if (verbosity) {
1943 log_info("socket: %s", gotd.unix_socket_path);
1944 log_info("user: %s", pw->pw_name);
1947 fd = unix_socket_listen(gotd.unix_socket_path, pw->pw_uid,
1948 pw->pw_gid);
1949 if (fd == -1) {
1950 fatal("cannot listen on unix socket %s",
1951 gotd.unix_socket_path);
1953 } else if (proc_id == PROC_AUTH) {
1954 snprintf(title, sizeof(title), "%s %s",
1955 gotd_proc_names[proc_id], repo_path);
1956 } else if (proc_id == PROC_REPO_READ || proc_id == PROC_REPO_WRITE ||
1957 proc_id == PROC_SESSION_READ || proc_id == PROC_SESSION_WRITE) {
1958 error = got_repo_pack_fds_open(&pack_fds);
1959 if (error != NULL)
1960 fatalx("cannot open pack tempfiles: %s", error->msg);
1961 error = got_repo_temp_fds_open(&temp_fds);
1962 if (error != NULL)
1963 fatalx("cannot open pack tempfiles: %s", error->msg);
1964 if (repo_path == NULL)
1965 fatalx("repository path not specified");
1966 snprintf(title, sizeof(title), "%s %s",
1967 gotd_proc_names[proc_id], repo_path);
1968 } else
1969 fatal("invalid process id %d", proc_id);
1971 setproctitle("%s", title);
1972 log_procinit(title);
1974 if (proc_id != PROC_GOTD && proc_id != PROC_LISTEN &&
1975 proc_id != PROC_REPO_READ && proc_id != PROC_REPO_WRITE) {
1976 /* Drop root privileges. */
1977 if (setgid(pw->pw_gid) == -1)
1978 fatal("setgid %d failed", pw->pw_gid);
1979 if (setuid(pw->pw_uid) == -1)
1980 fatal("setuid %d failed", pw->pw_uid);
1983 event_init();
1985 switch (proc_id) {
1986 case PROC_GOTD:
1987 #ifndef PROFILE
1988 /* "exec" promise will be limited to argv[0] via unveil(2). */
1989 if (pledge("stdio proc exec sendfd recvfd unveil", NULL) == -1)
1990 err(1, "pledge");
1991 #endif
1992 break;
1993 case PROC_LISTEN:
1994 #ifndef PROFILE
1995 if (pledge("stdio sendfd unix unveil", NULL) == -1)
1996 err(1, "pledge");
1997 #endif
1999 * Ensure that AF_UNIX bind(2) cannot be used with any other
2000 * sockets by revoking all filesystem access via unveil(2).
2002 apply_unveil_none();
2004 enter_chroot(GOTD_EMPTY_PATH);
2005 drop_privs(pw);
2007 listen_main(title, fd, gotd.connection_limits,
2008 gotd.nconnection_limits);
2009 /* NOTREACHED */
2010 break;
2011 case PROC_AUTH:
2012 #ifndef PROFILE
2013 if (pledge("stdio getpw recvfd unix unveil", NULL) == -1)
2014 err(1, "pledge");
2015 #endif
2017 * We need the "unix" pledge promise for getpeername(2) only.
2018 * Ensure that AF_UNIX bind(2) cannot be used by revoking all
2019 * filesystem access via unveil(2). Access to password database
2020 * files will still work since "getpw" bypasses unveil(2).
2022 apply_unveil_none();
2024 auth_main(title, &gotd.repos, repo_path);
2025 /* NOTREACHED */
2026 break;
2027 case PROC_SESSION_READ:
2028 case PROC_SESSION_WRITE:
2029 #ifndef PROFILE
2031 * The "recvfd" promise is only needed during setup and
2032 * will be removed in a later pledge(2) call.
2034 if (pledge("stdio rpath wpath cpath recvfd sendfd fattr flock "
2035 "unveil", NULL) == -1)
2036 err(1, "pledge");
2037 #endif
2038 if (proc_id == PROC_SESSION_READ)
2039 apply_unveil_repo_readonly(repo_path, 1);
2040 else
2041 apply_unveil_repo_readwrite(repo_path);
2043 session_main(title, repo_path, pack_fds, temp_fds,
2044 &gotd.request_timeout, proc_id);
2045 /* NOTREACHED */
2046 break;
2047 case PROC_REPO_READ:
2048 #ifndef PROFILE
2049 if (pledge("stdio rpath recvfd unveil", NULL) == -1)
2050 err(1, "pledge");
2051 #endif
2052 apply_unveil_repo_readonly(repo_path, 0);
2054 if (enter_chroot(repo_path)) {
2055 log_info("change repo path %s", repo_path);
2056 free(repo_path);
2057 repo_path = strdup("/");
2058 if (repo_path == NULL)
2059 fatal("strdup");
2060 log_info("repo path is now %s", repo_path);
2062 drop_privs(pw);
2064 repo_read_main(title, repo_path, pack_fds, temp_fds);
2065 /* NOTREACHED */
2066 exit(0);
2067 case PROC_REPO_WRITE:
2068 #ifndef PROFILE
2069 if (pledge("stdio rpath recvfd unveil", NULL) == -1)
2070 err(1, "pledge");
2071 #endif
2072 apply_unveil_repo_readonly(repo_path, 0);
2073 repo = gotd_find_repo_by_path(repo_path, &gotd);
2074 if (repo == NULL)
2075 fatalx("no repository for path %s", repo_path);
2077 if (enter_chroot(repo_path)) {
2078 free(repo_path);
2079 repo_path = strdup("/");
2080 if (repo_path == NULL)
2081 fatal("strdup");
2083 drop_privs(pw);
2085 repo_write_main(title, repo_path, pack_fds, temp_fds,
2086 &repo->protected_tag_namespaces,
2087 &repo->protected_branch_namespaces,
2088 &repo->protected_branches);
2089 /* NOTREACHED */
2090 exit(0);
2091 default:
2092 fatal("invalid process id %d", proc_id);
2095 if (proc_id != PROC_GOTD)
2096 fatal("invalid process id %d", proc_id);
2098 evtimer_set(&gotd.listen_proc->tmo, kill_proc_timeout,
2099 gotd.listen_proc);
2101 apply_unveil_selfexec();
2103 signal_set(&evsigint, SIGINT, gotd_sighdlr, NULL);
2104 signal_set(&evsigterm, SIGTERM, gotd_sighdlr, NULL);
2105 signal_set(&evsighup, SIGHUP, gotd_sighdlr, NULL);
2106 signal_set(&evsigusr1, SIGUSR1, gotd_sighdlr, NULL);
2107 signal_set(&evsigchld, SIGCHLD, gotd_sighdlr, NULL);
2108 signal(SIGPIPE, SIG_IGN);
2110 signal_add(&evsigint, NULL);
2111 signal_add(&evsigterm, NULL);
2112 signal_add(&evsighup, NULL);
2113 signal_add(&evsigusr1, NULL);
2114 signal_add(&evsigchld, NULL);
2116 gotd_imsg_event_add(&gotd.listen_proc->iev);
2118 event_dispatch();
2120 free(repo_path);
2121 gotd_shutdown();
2123 return 0;