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 *);
120 __dead static void
121 usage(void)
123 fprintf(stderr, "usage: %s [-dnv] [-f config-file]\n", getprogname());
124 exit(1);
127 static int
128 unix_socket_listen(const char *unix_socket_path, uid_t uid, gid_t gid)
130 struct sockaddr_un sun;
131 int fd = -1;
132 mode_t old_umask, mode;
133 int sock_flags = SOCK_STREAM | SOCK_NONBLOCK;
135 #ifdef SOCK_CLOEXEC
136 sock_flags |= SOCK_CLOEXEC;
137 #endif
139 fd = socket(AF_UNIX, sock_flags, 0);
140 if (fd == -1) {
141 log_warn("socket");
142 return -1;
145 sun.sun_family = AF_UNIX;
146 if (strlcpy(sun.sun_path, unix_socket_path,
147 sizeof(sun.sun_path)) >= sizeof(sun.sun_path)) {
148 log_warnx("%s: name too long", unix_socket_path);
149 close(fd);
150 return -1;
153 if (unlink(unix_socket_path) == -1) {
154 if (errno != ENOENT) {
155 log_warn("unlink %s", unix_socket_path);
156 close(fd);
157 return -1;
161 old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
162 mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
164 if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
165 log_warn("bind: %s", unix_socket_path);
166 close(fd);
167 umask(old_umask);
168 return -1;
171 umask(old_umask);
173 if (chmod(unix_socket_path, mode) == -1) {
174 log_warn("chmod %o %s", mode, unix_socket_path);
175 close(fd);
176 unlink(unix_socket_path);
177 return -1;
180 if (chown(unix_socket_path, uid, gid) == -1) {
181 log_warn("chown %s uid=%d gid=%d", unix_socket_path, uid, gid);
182 close(fd);
183 unlink(unix_socket_path);
184 return -1;
187 if (listen(fd, GOTD_UNIX_SOCKET_BACKLOG) == -1) {
188 log_warn("listen");
189 close(fd);
190 unlink(unix_socket_path);
191 return -1;
194 return fd;
197 static uint64_t
198 client_hash(uint32_t client_id)
200 return SipHash24(&clients_hash_key, &client_id, sizeof(client_id));
203 static void
204 add_client(struct gotd_client *client)
206 uint64_t slot = client_hash(client->id) % nitems(gotd_clients);
207 STAILQ_INSERT_HEAD(&gotd_clients[slot], client, entry);
208 client_cnt++;
211 static struct gotd_client *
212 find_client(uint32_t client_id)
214 uint64_t slot;
215 struct gotd_client *c;
217 slot = client_hash(client_id) % nitems(gotd_clients);
218 STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
219 if (c->id == client_id)
220 return c;
223 return NULL;
226 static struct gotd_client *
227 find_client_by_proc_fd(int fd)
229 uint64_t slot;
231 for (slot = 0; slot < nitems(gotd_clients); slot++) {
232 struct gotd_client *c;
234 STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
235 if (c->repo && c->repo->iev.ibuf.fd == fd)
236 return c;
237 if (c->auth && c->auth->iev.ibuf.fd == fd)
238 return c;
239 if (c->session && c->session->iev.ibuf.fd == fd)
240 return c;
244 return NULL;
247 static int
248 client_is_reading(struct gotd_client *client)
250 return (client->required_auth &
251 (GOTD_AUTH_READ | GOTD_AUTH_WRITE)) == GOTD_AUTH_READ;
254 static int
255 client_is_writing(struct gotd_client *client)
257 return (client->required_auth &
258 (GOTD_AUTH_READ | GOTD_AUTH_WRITE)) ==
259 (GOTD_AUTH_READ | GOTD_AUTH_WRITE);
262 static const struct got_error *
263 ensure_client_is_not_writing(struct gotd_client *client)
265 if (client_is_writing(client)) {
266 return got_error_fmt(GOT_ERR_BAD_PACKET,
267 "uid %d made a read-request but is writing to "
268 "a repository", client->euid);
271 return NULL;
274 static const struct got_error *
275 ensure_client_is_not_reading(struct gotd_client *client)
277 if (client_is_reading(client)) {
278 return got_error_fmt(GOT_ERR_BAD_PACKET,
279 "uid %d made a write-request but is reading from "
280 "a repository", client->euid);
283 return NULL;
286 static void
287 proc_done(struct gotd_child_proc *proc)
289 struct gotd_client *client;
291 TAILQ_REMOVE(&procs, proc, entry);
293 client = find_client_by_proc_fd(proc->iev.ibuf.fd);
294 if (client != NULL) {
295 if (proc == client->repo)
296 client->repo = NULL;
297 if (proc == client->auth)
298 client->auth = NULL;
299 if (proc == client->session)
300 client->session = NULL;
301 disconnect(client);
304 evtimer_del(&proc->tmo);
306 if (proc->iev.ibuf.fd != -1) {
307 event_del(&proc->iev.ev);
308 msgbuf_clear(&proc->iev.ibuf.w);
309 close(proc->iev.ibuf.fd);
312 free(proc);
315 static void
316 kill_repo_proc(struct gotd_client *client)
318 if (client->repo == NULL)
319 return;
321 kill_proc(client->repo, 0);
322 client->repo = NULL;
325 static void
326 kill_auth_proc(struct gotd_client *client)
328 if (client->auth == NULL)
329 return;
331 kill_proc(client->auth, 0);
332 client->auth = NULL;
335 static void
336 kill_session_proc(struct gotd_client *client)
338 if (client->session == NULL)
339 return;
341 kill_proc(client->session, 0);
342 client->session = NULL;
345 static void
346 disconnect(struct gotd_client *client)
348 struct gotd_imsg_disconnect idisconnect;
349 struct gotd_child_proc *listen_proc = gotd.listen_proc;
350 uint64_t slot;
352 log_debug("uid %d: disconnecting", client->euid);
354 kill_auth_proc(client);
355 kill_session_proc(client);
356 kill_repo_proc(client);
358 idisconnect.client_id = client->id;
359 if (gotd_imsg_compose_event(&listen_proc->iev,
360 GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
361 &idisconnect, sizeof(idisconnect)) == -1)
362 log_warn("imsg compose DISCONNECT");
364 slot = client_hash(client->id) % nitems(gotd_clients);
365 STAILQ_REMOVE(&gotd_clients[slot], client, gotd_client, entry);
366 imsg_clear(&client->iev.ibuf);
367 event_del(&client->iev.ev);
368 evtimer_del(&client->tmo);
369 if (client->fd != -1)
370 close(client->fd);
371 else if (client->iev.ibuf.fd != -1)
372 close(client->iev.ibuf.fd);
373 free(client);
374 client_cnt--;
377 static void
378 disconnect_on_error(struct gotd_client *client, const struct got_error *err)
380 struct imsgbuf ibuf;
382 if (err->code != GOT_ERR_EOF) {
383 log_warnx("uid %d: %s", client->euid, err->msg);
384 if (client->fd != -1) {
385 imsg_init(&ibuf, client->fd);
386 gotd_imsg_send_error(&ibuf, 0, PROC_GOTD, err);
387 imsg_clear(&ibuf);
390 disconnect(client);
393 static const struct got_error *
394 send_repo_info(struct gotd_imsgev *iev, struct gotd_repo *repo)
396 const struct got_error *err = NULL;
397 struct gotd_imsg_info_repo irepo;
399 memset(&irepo, 0, sizeof(irepo));
401 if (strlcpy(irepo.repo_name, repo->name, sizeof(irepo.repo_name))
402 >= sizeof(irepo.repo_name))
403 return got_error_msg(GOT_ERR_NO_SPACE, "repo name too long");
404 if (strlcpy(irepo.repo_path, repo->path, sizeof(irepo.repo_path))
405 >= sizeof(irepo.repo_path))
406 return got_error_msg(GOT_ERR_NO_SPACE, "repo path too long");
408 if (gotd_imsg_compose_event(iev, GOTD_IMSG_INFO_REPO, PROC_GOTD, -1,
409 &irepo, sizeof(irepo)) == -1) {
410 err = got_error_from_errno("imsg compose INFO_REPO");
411 if (err)
412 return err;
415 return NULL;
418 static const struct got_error *
419 send_client_info(struct gotd_imsgev *iev, struct gotd_client *client)
421 const struct got_error *err = NULL;
422 struct gotd_imsg_info_client iclient;
423 struct gotd_child_proc *proc;
425 memset(&iclient, 0, sizeof(iclient));
426 iclient.euid = client->euid;
427 iclient.egid = client->egid;
429 proc = client->repo;
430 if (proc) {
431 if (strlcpy(iclient.repo_name, proc->repo_path,
432 sizeof(iclient.repo_name)) >= sizeof(iclient.repo_name)) {
433 return got_error_msg(GOT_ERR_NO_SPACE,
434 "repo name too long");
436 if (client_is_writing(client))
437 iclient.is_writing = 1;
439 iclient.repo_child_pid = proc->pid;
442 if (client->session)
443 iclient.session_child_pid = client->session->pid;
445 if (gotd_imsg_compose_event(iev, GOTD_IMSG_INFO_CLIENT, PROC_GOTD, -1,
446 &iclient, sizeof(iclient)) == -1) {
447 err = got_error_from_errno("imsg compose INFO_CLIENT");
448 if (err)
449 return err;
452 return NULL;
455 static const struct got_error *
456 send_info(struct gotd_client *client)
458 const struct got_error *err = NULL;
459 struct gotd_imsg_info info;
460 uint64_t slot;
461 struct gotd_repo *repo;
463 if (client->euid != 0)
464 return got_error_set_errno(EPERM, "info");
466 info.pid = gotd.pid;
467 info.verbosity = gotd.verbosity;
468 info.nrepos = gotd.nrepos;
469 info.nclients = client_cnt - 1;
471 if (gotd_imsg_compose_event(&client->iev, GOTD_IMSG_INFO, PROC_GOTD, -1,
472 &info, sizeof(info)) == -1) {
473 err = got_error_from_errno("imsg compose INFO");
474 if (err)
475 return err;
478 TAILQ_FOREACH(repo, &gotd.repos, entry) {
479 err = send_repo_info(&client->iev, repo);
480 if (err)
481 return err;
484 for (slot = 0; slot < nitems(gotd_clients); slot++) {
485 struct gotd_client *c;
486 STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
487 if (c->id == client->id)
488 continue;
489 err = send_client_info(&client->iev, c);
490 if (err)
491 return err;
495 return NULL;
498 static const struct got_error *
499 stop_gotd(struct gotd_client *client)
502 if (client->euid != 0)
503 return got_error_set_errno(EPERM, "stop");
505 gotd_shutdown();
506 /* NOTREACHED */
507 return NULL;
510 static const struct got_error *
511 start_client_authentication(struct gotd_client *client, struct imsg *imsg)
513 const struct got_error *err;
514 struct gotd_imsg_list_refs ireq;
515 struct gotd_repo *repo = NULL;
516 size_t datalen;
518 log_debug("list-refs request from uid %d", client->euid);
520 if (client->state != GOTD_CLIENT_STATE_NEW)
521 return got_error_msg(GOT_ERR_BAD_REQUEST,
522 "unexpected list-refs request received");
524 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
525 if (datalen != sizeof(ireq))
526 return got_error(GOT_ERR_PRIVSEP_LEN);
528 memcpy(&ireq, imsg->data, datalen);
530 if (ireq.client_is_reading) {
531 err = ensure_client_is_not_writing(client);
532 if (err)
533 return err;
534 repo = gotd_find_repo_by_name(ireq.repo_name, &gotd);
535 if (repo == NULL)
536 return got_error(GOT_ERR_NOT_GIT_REPO);
537 err = start_auth_child(client, GOTD_AUTH_READ, repo,
538 gotd.argv0, gotd.confpath, gotd.daemonize,
539 gotd.verbosity);
540 if (err)
541 return err;
542 } else {
543 err = ensure_client_is_not_reading(client);
544 if (err)
545 return err;
546 repo = gotd_find_repo_by_name(ireq.repo_name, &gotd);
547 if (repo == NULL)
548 return got_error(GOT_ERR_NOT_GIT_REPO);
549 err = start_auth_child(client,
550 GOTD_AUTH_READ | GOTD_AUTH_WRITE,
551 repo, gotd.argv0, gotd.confpath, gotd.daemonize,
552 gotd.verbosity);
553 if (err)
554 return err;
557 evtimer_add(&client->tmo, &auth_timeout);
559 /* Flow continues upon authentication success/failure or timeout. */
560 return NULL;
563 static void
564 gotd_request(int fd, short events, void *arg)
566 struct gotd_imsgev *iev = arg;
567 struct imsgbuf *ibuf = &iev->ibuf;
568 struct gotd_client *client = iev->handler_arg;
569 const struct got_error *err = NULL;
570 struct imsg imsg;
571 ssize_t n;
573 if (events & EV_WRITE) {
574 while (ibuf->w.queued) {
575 n = msgbuf_write(&ibuf->w);
576 if (n == -1 && errno == EPIPE) {
577 /*
578 * The client has closed its socket.
579 * This can happen when Git clients are
580 * done sending pack file data.
581 */
582 msgbuf_clear(&ibuf->w);
583 continue;
584 } else if (n == -1 && errno != EAGAIN) {
585 err = got_error_from_errno("imsg_flush");
586 disconnect_on_error(client, err);
587 return;
589 if (n == 0) {
590 /* Connection closed. */
591 err = got_error(GOT_ERR_EOF);
592 disconnect_on_error(client, err);
593 return;
597 /* Disconnect gotctl(8) now that messages have been sent. */
598 if (!client_is_reading(client) && !client_is_writing(client)) {
599 disconnect(client);
600 return;
604 if ((events & EV_READ) == 0)
605 return;
607 memset(&imsg, 0, sizeof(imsg));
609 while (err == NULL) {
610 err = gotd_imsg_recv(&imsg, ibuf, 0);
611 if (err) {
612 if (err->code == GOT_ERR_PRIVSEP_READ)
613 err = NULL;
614 break;
617 evtimer_del(&client->tmo);
619 switch (imsg.hdr.type) {
620 case GOTD_IMSG_INFO:
621 err = send_info(client);
622 break;
623 case GOTD_IMSG_STOP:
624 err = stop_gotd(client);
625 break;
626 case GOTD_IMSG_LIST_REFS:
627 err = start_client_authentication(client, &imsg);
628 break;
629 default:
630 log_debug("unexpected imsg %d", imsg.hdr.type);
631 err = got_error(GOT_ERR_PRIVSEP_MSG);
632 break;
635 imsg_free(&imsg);
638 if (err) {
639 disconnect_on_error(client, err);
640 } else {
641 gotd_imsg_event_add(&client->iev);
645 static void
646 gotd_auth_timeout(int fd, short events, void *arg)
648 struct gotd_client *client = arg;
650 log_debug("disconnecting uid %d due to authentication timeout",
651 client->euid);
652 disconnect(client);
655 static const struct got_error *
656 recv_connect(uint32_t *client_id, struct imsg *imsg)
658 const struct got_error *err = NULL;
659 struct gotd_imsg_connect iconnect;
660 size_t datalen;
661 int s = -1;
662 struct gotd_client *client = NULL;
664 *client_id = 0;
666 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
667 if (datalen != sizeof(iconnect))
668 return got_error(GOT_ERR_PRIVSEP_LEN);
669 memcpy(&iconnect, imsg->data, sizeof(iconnect));
671 s = imsg->fd;
672 if (s == -1) {
673 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
674 goto done;
677 if (find_client(iconnect.client_id)) {
678 err = got_error_msg(GOT_ERR_CLIENT_ID, "duplicate client ID");
679 goto done;
682 client = calloc(1, sizeof(*client));
683 if (client == NULL) {
684 err = got_error_from_errno("calloc");
685 goto done;
688 *client_id = iconnect.client_id;
690 client->state = GOTD_CLIENT_STATE_NEW;
691 client->id = iconnect.client_id;
692 client->fd = s;
693 s = -1;
694 /* The auth process will verify UID/GID for us. */
695 client->euid = iconnect.euid;
696 client->egid = iconnect.egid;
698 imsg_init(&client->iev.ibuf, client->fd);
699 client->iev.handler = gotd_request;
700 client->iev.events = EV_READ;
701 client->iev.handler_arg = client;
703 event_set(&client->iev.ev, client->fd, EV_READ, gotd_request,
704 &client->iev);
705 gotd_imsg_event_add(&client->iev);
707 evtimer_set(&client->tmo, gotd_auth_timeout, client);
709 add_client(client);
710 log_debug("%s: new client uid %d connected on fd %d", __func__,
711 client->euid, client->fd);
712 done:
713 if (err) {
714 struct gotd_child_proc *listen_proc = gotd.listen_proc;
715 struct gotd_imsg_disconnect idisconnect;
717 idisconnect.client_id = client->id;
718 if (gotd_imsg_compose_event(&listen_proc->iev,
719 GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
720 &idisconnect, sizeof(idisconnect)) == -1)
721 log_warn("imsg compose DISCONNECT");
723 if (s != -1)
724 close(s);
727 return err;
730 static const char *gotd_proc_names[PROC_MAX] = {
731 "parent",
732 "listen",
733 "auth",
734 "session_read",
735 "session_write",
736 "repo_read",
737 "repo_write",
738 "gitwrapper"
739 };
741 static void
742 kill_proc(struct gotd_child_proc *proc, int fatal)
744 struct timeval tv = { 5, 0 };
746 log_debug("kill -%d %d", fatal ? SIGKILL : SIGTERM, proc->pid);
748 if (proc->iev.ibuf.fd != -1) {
749 event_del(&proc->iev.ev);
750 msgbuf_clear(&proc->iev.ibuf.w);
751 close(proc->iev.ibuf.fd);
752 proc->iev.ibuf.fd = -1;
755 if (!evtimer_pending(&proc->tmo, NULL) && !fatal)
756 evtimer_add(&proc->tmo, &tv);
758 if (fatal) {
759 log_warnx("sending SIGKILL to PID %d", proc->pid);
760 kill(proc->pid, SIGKILL);
761 } else
762 kill(proc->pid, SIGTERM);
765 static void
766 kill_proc_timeout(int fd, short ev, void *d)
768 struct gotd_child_proc *proc = d;
770 log_warnx("timeout waiting for PID %d to terminate;"
771 " retrying with force", proc->pid);
772 kill_proc(proc, 1);
775 static void
776 gotd_shutdown(void)
778 uint64_t slot;
780 log_debug("shutting down");
781 for (slot = 0; slot < nitems(gotd_clients); slot++) {
782 struct gotd_client *c, *tmp;
784 STAILQ_FOREACH_SAFE(c, &gotd_clients[slot], entry, tmp)
785 disconnect(c);
788 kill_proc(gotd.listen_proc, 0);
790 log_info("terminating");
791 exit(0);
794 static struct gotd_child_proc *
795 find_proc_by_pid(pid_t pid)
797 struct gotd_child_proc *proc = NULL;
799 TAILQ_FOREACH(proc, &procs, entry)
800 if (proc->pid == pid)
801 break;
803 return proc;
806 void
807 gotd_sighdlr(int sig, short event, void *arg)
809 struct gotd_child_proc *proc;
810 pid_t pid;
811 int status;
813 /*
814 * Normal signal handler rules don't apply because libevent
815 * decouples for us.
816 */
818 switch (sig) {
819 case SIGHUP:
820 log_info("%s: ignoring SIGHUP", __func__);
821 break;
822 case SIGUSR1:
823 log_info("%s: ignoring SIGUSR1", __func__);
824 break;
825 case SIGTERM:
826 case SIGINT:
827 gotd_shutdown();
828 break;
829 case SIGCHLD:
830 for (;;) {
831 pid = waitpid(WAIT_ANY, &status, WNOHANG);
832 if (pid == -1) {
833 if (errno == EINTR)
834 continue;
835 if (errno == ECHILD)
836 break;
837 fatal("waitpid");
839 if (pid == 0)
840 break;
842 log_debug("reaped pid %d", pid);
843 proc = find_proc_by_pid(pid);
844 if (proc == NULL) {
845 log_info("caught exit of unknown child %d",
846 pid);
847 continue;
850 if (WIFSIGNALED(status)) {
851 log_warnx("child PID %d terminated with"
852 " signal %d", pid, WTERMSIG(status));
855 proc_done(proc);
857 break;
858 default:
859 fatalx("unexpected signal");
863 static const struct got_error *
864 ensure_proc_is_reading(struct gotd_client *client,
865 struct gotd_child_proc *proc)
867 if (!client_is_reading(client)) {
868 kill_proc(proc, 1);
869 return got_error_fmt(GOT_ERR_BAD_PACKET,
870 "PID %d handled a read-request for uid %d but this "
871 "user is not reading from a repository", proc->pid,
872 client->euid);
875 return NULL;
878 static const struct got_error *
879 ensure_proc_is_writing(struct gotd_client *client,
880 struct gotd_child_proc *proc)
882 if (!client_is_writing(client)) {
883 kill_proc(proc, 1);
884 return got_error_fmt(GOT_ERR_BAD_PACKET,
885 "PID %d handled a write-request for uid %d but this "
886 "user is not writing to a repository", proc->pid,
887 client->euid);
890 return NULL;
893 static int
894 verify_imsg_src(struct gotd_client *client, struct gotd_child_proc *proc,
895 struct imsg *imsg)
897 const struct got_error *err;
898 int ret = 0;
900 if (proc->type == PROC_REPO_READ || proc->type == PROC_REPO_WRITE) {
901 if (client->repo == NULL)
902 fatalx("no process found for uid %d", client->euid);
903 if (proc->pid != client->repo->pid) {
904 kill_proc(proc, 1);
905 log_warnx("received message from PID %d for uid %d, "
906 "while PID %d is the process serving this user",
907 proc->pid, client->euid, client->repo->pid);
908 return 0;
911 if (proc->type == PROC_SESSION_READ ||
912 proc->type == PROC_SESSION_WRITE) {
913 if (client->session == NULL) {
914 log_warnx("no session found for uid %d", client->euid);
915 return 0;
917 if (proc->pid != client->session->pid) {
918 kill_proc(proc, 1);
919 log_warnx("received message from PID %d for uid %d, "
920 "while PID %d is the process serving this user",
921 proc->pid, client->euid, client->session->pid);
922 return 0;
926 switch (imsg->hdr.type) {
927 case GOTD_IMSG_ERROR:
928 ret = 1;
929 break;
930 case GOTD_IMSG_CONNECT:
931 if (proc->type != PROC_LISTEN) {
932 err = got_error_fmt(GOT_ERR_BAD_PACKET,
933 "new connection for uid %d from PID %d "
934 "which is not the listen process",
935 proc->pid, client->euid);
936 } else
937 ret = 1;
938 break;
939 case GOTD_IMSG_ACCESS_GRANTED:
940 if (proc->type != PROC_AUTH) {
941 err = got_error_fmt(GOT_ERR_BAD_PACKET,
942 "authentication of uid %d from PID %d "
943 "which is not the auth process",
944 proc->pid, client->euid);
945 } else
946 ret = 1;
947 break;
948 case GOTD_IMSG_CLIENT_SESSION_READY:
949 if (proc->type != PROC_SESSION_READ &&
950 proc->type != PROC_SESSION_WRITE) {
951 err = got_error_fmt(GOT_ERR_BAD_PACKET,
952 "unexpected \"ready\" signal from PID %d",
953 proc->pid);
954 } else
955 ret = 1;
956 break;
957 case GOTD_IMSG_REPO_CHILD_READY:
958 if (proc->type != PROC_REPO_READ &&
959 proc->type != PROC_REPO_WRITE) {
960 err = got_error_fmt(GOT_ERR_BAD_PACKET,
961 "unexpected \"ready\" signal from PID %d",
962 proc->pid);
963 } else
964 ret = 1;
965 break;
966 case GOTD_IMSG_PACKFILE_DONE:
967 err = ensure_proc_is_reading(client, proc);
968 if (err)
969 log_warnx("uid %d: %s", client->euid, err->msg);
970 else
971 ret = 1;
972 break;
973 case GOTD_IMSG_PACKFILE_INSTALL:
974 case GOTD_IMSG_REF_UPDATES_START:
975 case GOTD_IMSG_REF_UPDATE:
976 err = ensure_proc_is_writing(client, proc);
977 if (err)
978 log_warnx("uid %d: %s", client->euid, err->msg);
979 else
980 ret = 1;
981 break;
982 default:
983 log_debug("%s: unexpected imsg %d", __func__, imsg->hdr.type);
984 break;
987 return ret;
990 static const struct got_error *
991 connect_repo_child(struct gotd_client *client,
992 struct gotd_child_proc *repo_proc)
994 static const struct got_error *err;
995 struct gotd_imsgev *session_iev = &client->session->iev;
996 struct gotd_imsg_connect_repo_child ireq;
997 int pipe[2];
998 int sock_flags = SOCK_STREAM | SOCK_NONBLOCK;
1000 #ifdef SOCK_CLOEXEC
1001 sock_flags |= SOCK_CLOEXEC;
1002 #endif
1004 if (client->state != GOTD_CLIENT_STATE_ACCESS_GRANTED)
1005 return got_error_msg(GOT_ERR_BAD_REQUEST,
1006 "unexpected repo child ready signal received");
1008 if (socketpair(AF_UNIX, sock_flags, PF_UNSPEC, pipe) == -1)
1009 fatal("socketpair");
1011 memset(&ireq, 0, sizeof(ireq));
1012 ireq.client_id = client->id;
1013 ireq.proc_id = repo_proc->type;
1015 /* Pass repo child pipe to session child process. */
1016 if (gotd_imsg_compose_event(session_iev, GOTD_IMSG_CONNECT_REPO_CHILD,
1017 PROC_GOTD, pipe[0], &ireq, sizeof(ireq)) == -1) {
1018 err = got_error_from_errno("imsg compose CONNECT_REPO_CHILD");
1019 close(pipe[0]);
1020 close(pipe[1]);
1021 return err;
1024 /* Pass session child pipe to repo child process. */
1025 if (gotd_imsg_compose_event(&repo_proc->iev,
1026 GOTD_IMSG_CONNECT_REPO_CHILD, PROC_GOTD, pipe[1], NULL, 0) == -1) {
1027 err = got_error_from_errno("imsg compose CONNECT_REPO_CHILD");
1028 close(pipe[1]);
1029 return err;
1032 return NULL;
1035 static void
1036 gotd_dispatch_listener(int fd, short event, void *arg)
1038 struct gotd_imsgev *iev = arg;
1039 struct imsgbuf *ibuf = &iev->ibuf;
1040 struct gotd_child_proc *proc = gotd.listen_proc;
1041 ssize_t n;
1042 int shut = 0;
1043 struct imsg imsg;
1045 if (proc->iev.ibuf.fd != fd)
1046 fatalx("%s: unexpected fd %d", __func__, fd);
1048 if (event & EV_READ) {
1049 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1050 fatal("imsg_read error");
1051 if (n == 0) {
1052 /* Connection closed. */
1053 shut = 1;
1054 goto done;
1058 if (event & EV_WRITE) {
1059 n = msgbuf_write(&ibuf->w);
1060 if (n == -1 && errno != EAGAIN)
1061 fatal("msgbuf_write");
1062 if (n == 0) {
1063 /* Connection closed. */
1064 shut = 1;
1065 goto done;
1069 for (;;) {
1070 const struct got_error *err = NULL;
1071 struct gotd_client *client = NULL;
1072 uint32_t client_id = 0;
1073 int do_disconnect = 0;
1075 if ((n = imsg_get(ibuf, &imsg)) == -1)
1076 fatal("%s: imsg_get error", __func__);
1077 if (n == 0) /* No more messages. */
1078 break;
1080 switch (imsg.hdr.type) {
1081 case GOTD_IMSG_ERROR:
1082 do_disconnect = 1;
1083 err = gotd_imsg_recv_error(&client_id, &imsg);
1084 break;
1085 case GOTD_IMSG_CONNECT:
1086 err = recv_connect(&client_id, &imsg);
1087 break;
1088 default:
1089 log_debug("unexpected imsg %d", imsg.hdr.type);
1090 break;
1093 client = find_client(client_id);
1094 if (client == NULL) {
1095 log_warnx("%s: client not found", __func__);
1096 imsg_free(&imsg);
1097 continue;
1100 if (err)
1101 log_warnx("uid %d: %s", client->euid, err->msg);
1103 if (do_disconnect) {
1104 if (err)
1105 disconnect_on_error(client, err);
1106 else
1107 disconnect(client);
1110 imsg_free(&imsg);
1112 done:
1113 if (!shut) {
1114 gotd_imsg_event_add(iev);
1115 } else {
1116 /* This pipe is dead. Remove its event handler */
1117 event_del(&iev->ev);
1118 event_loopexit(NULL);
1122 static void
1123 gotd_dispatch_auth_child(int fd, short event, void *arg)
1125 const struct got_error *err = NULL;
1126 struct gotd_imsgev *iev = arg;
1127 struct imsgbuf *ibuf = &iev->ibuf;
1128 struct gotd_client *client;
1129 struct gotd_repo *repo = NULL;
1130 ssize_t n;
1131 int shut = 0;
1132 struct imsg imsg;
1133 uint32_t client_id = 0;
1134 int do_disconnect = 0;
1136 client = find_client_by_proc_fd(fd);
1137 if (client == NULL) {
1138 /* Can happen during process teardown. */
1139 warnx("cannot find client for fd %d", fd);
1140 shut = 1;
1141 goto done;
1144 if (client->auth == NULL)
1145 fatalx("cannot find auth child process for fd %d", fd);
1147 if (event & EV_READ) {
1148 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1149 fatal("imsg_read error");
1150 if (n == 0) {
1151 /* Connection closed. */
1152 shut = 1;
1153 goto done;
1157 if (event & EV_WRITE) {
1158 n = msgbuf_write(&ibuf->w);
1159 if (n == -1 && errno != EAGAIN)
1160 fatal("msgbuf_write");
1161 if (n == 0) {
1162 /* Connection closed. */
1163 shut = 1;
1165 goto done;
1168 if (client->auth->iev.ibuf.fd != fd)
1169 fatalx("%s: unexpected fd %d", __func__, fd);
1171 if ((n = imsg_get(ibuf, &imsg)) == -1)
1172 fatal("%s: imsg_get error", __func__);
1173 if (n == 0) /* No more messages. */
1174 return;
1176 evtimer_del(&client->tmo);
1178 switch (imsg.hdr.type) {
1179 case GOTD_IMSG_ERROR:
1180 do_disconnect = 1;
1181 err = gotd_imsg_recv_error(&client_id, &imsg);
1182 break;
1183 case GOTD_IMSG_ACCESS_GRANTED:
1184 client->state = GOTD_CLIENT_STATE_ACCESS_GRANTED;
1185 break;
1186 default:
1187 do_disconnect = 1;
1188 log_debug("unexpected imsg %d", imsg.hdr.type);
1189 break;
1192 if (!verify_imsg_src(client, client->auth, &imsg)) {
1193 do_disconnect = 1;
1194 log_debug("dropping imsg type %d from PID %d",
1195 imsg.hdr.type, client->auth->pid);
1197 imsg_free(&imsg);
1199 if (do_disconnect) {
1200 if (err)
1201 disconnect_on_error(client, err);
1202 else
1203 disconnect(client);
1204 return;
1207 repo = gotd_find_repo_by_name(client->auth->repo_name, &gotd);
1208 if (repo == NULL) {
1209 err = got_error(GOT_ERR_NOT_GIT_REPO);
1210 goto done;
1212 kill_auth_proc(client);
1214 log_info("authenticated uid %d for repository %s",
1215 client->euid, repo->name);
1217 err = start_session_child(client, repo, gotd.argv0,
1218 gotd.confpath, gotd.daemonize, gotd.verbosity);
1219 if (err)
1220 goto done;
1221 done:
1222 if (err)
1223 log_warnx("uid %d: %s", client->euid, err->msg);
1225 /* We might have killed the auth process by now. */
1226 if (client->auth != NULL) {
1227 if (!shut) {
1228 gotd_imsg_event_add(iev);
1229 } else {
1230 /* This pipe is dead. Remove its event handler */
1231 event_del(&iev->ev);
1236 static const struct got_error *
1237 connect_session(struct gotd_client *client)
1239 const struct got_error *err = NULL;
1240 struct gotd_imsg_connect iconnect;
1241 int s;
1243 memset(&iconnect, 0, sizeof(iconnect));
1245 s = dup(client->fd);
1246 if (s == -1)
1247 return got_error_from_errno("dup");
1249 iconnect.client_id = client->id;
1250 iconnect.euid = client->euid;
1251 iconnect.egid = client->egid;
1253 if (gotd_imsg_compose_event(&client->session->iev, GOTD_IMSG_CONNECT,
1254 PROC_GOTD, s, &iconnect, sizeof(iconnect)) == -1) {
1255 err = got_error_from_errno("imsg compose CONNECT");
1256 close(s);
1257 return err;
1261 * We are no longer interested in messages from this client.
1262 * Further client requests will be handled by the session process.
1264 msgbuf_clear(&client->iev.ibuf.w);
1265 imsg_clear(&client->iev.ibuf);
1266 event_del(&client->iev.ev);
1267 client->fd = -1; /* will be closed via copy in client->iev.ibuf.fd */
1269 return NULL;
1272 static void
1273 gotd_dispatch_client_session(int fd, short event, void *arg)
1275 struct gotd_imsgev *iev = arg;
1276 struct imsgbuf *ibuf = &iev->ibuf;
1277 struct gotd_child_proc *proc = NULL;
1278 struct gotd_client *client = NULL;
1279 ssize_t n;
1280 int shut = 0;
1281 struct imsg imsg;
1283 client = find_client_by_proc_fd(fd);
1284 if (client == NULL) {
1285 /* Can happen during process teardown. */
1286 warnx("cannot find client for fd %d", fd);
1287 shut = 1;
1288 goto done;
1291 if (event & EV_READ) {
1292 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1293 fatal("imsg_read error");
1294 if (n == 0) {
1295 /* Connection closed. */
1296 shut = 1;
1297 goto done;
1301 if (event & EV_WRITE) {
1302 n = msgbuf_write(&ibuf->w);
1303 if (n == -1 && errno != EAGAIN)
1304 fatal("msgbuf_write");
1305 if (n == 0) {
1306 /* Connection closed. */
1307 shut = 1;
1308 goto done;
1312 proc = client->session;
1313 if (proc == NULL)
1314 fatalx("cannot find session child process for fd %d", fd);
1316 for (;;) {
1317 const struct got_error *err = NULL;
1318 uint32_t client_id = 0;
1319 int do_disconnect = 0, do_start_repo_child = 0;
1321 if ((n = imsg_get(ibuf, &imsg)) == -1)
1322 fatal("%s: imsg_get error", __func__);
1323 if (n == 0) /* No more messages. */
1324 break;
1326 switch (imsg.hdr.type) {
1327 case GOTD_IMSG_ERROR:
1328 do_disconnect = 1;
1329 err = gotd_imsg_recv_error(&client_id, &imsg);
1330 break;
1331 case GOTD_IMSG_CLIENT_SESSION_READY:
1332 if (client->state != GOTD_CLIENT_STATE_ACCESS_GRANTED) {
1333 err = got_error(GOT_ERR_PRIVSEP_MSG);
1334 break;
1336 do_start_repo_child = 1;
1337 break;
1338 case GOTD_IMSG_DISCONNECT:
1339 do_disconnect = 1;
1340 break;
1341 default:
1342 log_debug("unexpected imsg %d", imsg.hdr.type);
1343 break;
1346 if (!verify_imsg_src(client, proc, &imsg)) {
1347 log_debug("dropping imsg type %d from PID %d",
1348 imsg.hdr.type, proc->pid);
1349 imsg_free(&imsg);
1350 continue;
1352 if (err)
1353 log_warnx("uid %d: %s", client->euid, err->msg);
1355 if (do_start_repo_child) {
1356 struct gotd_repo *repo;
1357 const char *name = client->session->repo_name;
1359 repo = gotd_find_repo_by_name(name, &gotd);
1360 if (repo != NULL) {
1361 enum gotd_procid proc_type;
1363 if (client->required_auth & GOTD_AUTH_WRITE)
1364 proc_type = PROC_REPO_WRITE;
1365 else
1366 proc_type = PROC_REPO_READ;
1368 err = start_repo_child(client, proc_type, repo,
1369 gotd.argv0, gotd.confpath, gotd.daemonize,
1370 gotd.verbosity);
1371 } else
1372 err = got_error(GOT_ERR_NOT_GIT_REPO);
1374 if (err) {
1375 log_warnx("uid %d: %s", client->euid, err->msg);
1376 do_disconnect = 1;
1380 if (do_disconnect) {
1381 if (err)
1382 disconnect_on_error(client, err);
1383 else
1384 disconnect(client);
1387 imsg_free(&imsg);
1389 done:
1390 if (!shut) {
1391 gotd_imsg_event_add(iev);
1392 } else {
1393 /* This pipe is dead. Remove its event handler */
1394 event_del(&iev->ev);
1395 disconnect(client);
1399 static void
1400 gotd_dispatch_repo_child(int fd, short event, void *arg)
1402 struct gotd_imsgev *iev = arg;
1403 struct imsgbuf *ibuf = &iev->ibuf;
1404 struct gotd_child_proc *proc = NULL;
1405 struct gotd_client *client;
1406 ssize_t n;
1407 int shut = 0;
1408 struct imsg imsg;
1410 client = find_client_by_proc_fd(fd);
1411 if (client == NULL) {
1412 /* Can happen during process teardown. */
1413 warnx("cannot find client for fd %d", fd);
1414 shut = 1;
1415 goto done;
1418 if (event & EV_READ) {
1419 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1420 fatal("imsg_read error");
1421 if (n == 0) {
1422 /* Connection closed. */
1423 shut = 1;
1424 goto done;
1428 if (event & EV_WRITE) {
1429 n = msgbuf_write(&ibuf->w);
1430 if (n == -1 && errno != EAGAIN)
1431 fatal("msgbuf_write");
1432 if (n == 0) {
1433 /* Connection closed. */
1434 shut = 1;
1435 goto done;
1439 proc = client->repo;
1440 if (proc == NULL)
1441 fatalx("cannot find child process for fd %d", fd);
1443 for (;;) {
1444 const struct got_error *err = NULL;
1445 uint32_t client_id = 0;
1446 int do_disconnect = 0;
1448 if ((n = imsg_get(ibuf, &imsg)) == -1)
1449 fatal("%s: imsg_get error", __func__);
1450 if (n == 0) /* No more messages. */
1451 break;
1453 switch (imsg.hdr.type) {
1454 case GOTD_IMSG_ERROR:
1455 do_disconnect = 1;
1456 err = gotd_imsg_recv_error(&client_id, &imsg);
1457 break;
1458 case GOTD_IMSG_REPO_CHILD_READY:
1459 err = connect_session(client);
1460 if (err)
1461 break;
1462 err = connect_repo_child(client, proc);
1463 break;
1464 default:
1465 log_debug("unexpected imsg %d", imsg.hdr.type);
1466 break;
1469 if (!verify_imsg_src(client, proc, &imsg)) {
1470 log_debug("dropping imsg type %d from PID %d",
1471 imsg.hdr.type, proc->pid);
1472 imsg_free(&imsg);
1473 continue;
1475 if (err)
1476 log_warnx("uid %d: %s", client->euid, err->msg);
1478 if (do_disconnect) {
1479 if (err)
1480 disconnect_on_error(client, err);
1481 else
1482 disconnect(client);
1485 imsg_free(&imsg);
1487 done:
1488 if (!shut) {
1489 gotd_imsg_event_add(iev);
1490 } else {
1491 /* This pipe is dead. Remove its event handler */
1492 event_del(&iev->ev);
1493 disconnect(client);
1497 static pid_t
1498 start_child(enum gotd_procid proc_id, const char *repo_path,
1499 char *argv0, const char *confpath, int fd, int daemonize, int verbosity)
1501 char *argv[11];
1502 int argc = 0;
1503 pid_t pid;
1505 switch (pid = fork()) {
1506 case -1:
1507 fatal("cannot fork");
1508 case 0:
1509 break;
1510 default:
1511 close(fd);
1512 return pid;
1515 if (fd != GOTD_FILENO_MSG_PIPE) {
1516 if (dup2(fd, GOTD_FILENO_MSG_PIPE) == -1)
1517 fatal("cannot setup imsg fd");
1518 } else if (fcntl(fd, F_SETFD, 0) == -1)
1519 fatal("cannot setup imsg fd");
1521 argv[argc++] = argv0;
1522 switch (proc_id) {
1523 case PROC_LISTEN:
1524 argv[argc++] = (char *)"-L";
1525 break;
1526 case PROC_AUTH:
1527 argv[argc++] = (char *)"-A";
1528 break;
1529 case PROC_SESSION_READ:
1530 argv[argc++] = (char *)"-s";
1531 break;
1532 case PROC_SESSION_WRITE:
1533 argv[argc++] = (char *)"-S";
1534 break;
1535 case PROC_REPO_READ:
1536 argv[argc++] = (char *)"-R";
1537 break;
1538 case PROC_REPO_WRITE:
1539 argv[argc++] = (char *)"-W";
1540 break;
1541 default:
1542 fatalx("invalid process id %d", proc_id);
1545 argv[argc++] = (char *)"-f";
1546 argv[argc++] = (char *)confpath;
1548 if (repo_path) {
1549 argv[argc++] = (char *)"-P";
1550 argv[argc++] = (char *)repo_path;
1553 if (!daemonize)
1554 argv[argc++] = (char *)"-d";
1555 if (verbosity > 0)
1556 argv[argc++] = (char *)"-v";
1557 if (verbosity > 1)
1558 argv[argc++] = (char *)"-v";
1559 argv[argc++] = NULL;
1561 execvp(argv0, argv);
1562 fatal("execvp");
1565 static void
1566 start_listener(char *argv0, const char *confpath, int daemonize, int verbosity)
1568 struct gotd_child_proc *proc;
1569 int sock_flags = SOCK_STREAM|SOCK_NONBLOCK;
1571 #ifdef SOCK_CLOEXEC
1572 sock_flags |= SOCK_CLOEXEC;
1573 #endif
1575 proc = calloc(1, sizeof(*proc));
1576 if (proc == NULL)
1577 fatal("calloc");
1579 TAILQ_INSERT_HEAD(&procs, proc, entry);
1581 /* proc->tmo is initialized in main() after event_init() */
1583 proc->type = PROC_LISTEN;
1585 if (socketpair(AF_UNIX, sock_flags,
1586 PF_UNSPEC, proc->pipe) == -1)
1587 fatal("socketpair");
1589 proc->pid = start_child(proc->type, NULL, argv0, confpath,
1590 proc->pipe[1], daemonize, verbosity);
1591 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1592 proc->iev.handler = gotd_dispatch_listener;
1593 proc->iev.events = EV_READ;
1594 proc->iev.handler_arg = NULL;
1596 gotd.listen_proc = proc;
1599 static const struct got_error *
1600 start_session_child(struct gotd_client *client, struct gotd_repo *repo,
1601 char *argv0, const char *confpath, int daemonize, int verbosity)
1603 struct gotd_child_proc *proc;
1604 int sock_flags = SOCK_STREAM | SOCK_NONBLOCK;
1606 #ifdef SOCK_CLOEXEC
1607 sock_flags |= SOCK_CLOEXEC;
1608 #endif
1610 proc = calloc(1, sizeof(*proc));
1611 if (proc == NULL)
1612 return got_error_from_errno("calloc");
1614 TAILQ_INSERT_HEAD(&procs, proc, entry);
1615 evtimer_set(&proc->tmo, kill_proc_timeout, proc);
1617 if (client_is_reading(client))
1618 proc->type = PROC_SESSION_READ;
1619 else
1620 proc->type = PROC_SESSION_WRITE;
1621 if (strlcpy(proc->repo_name, repo->name,
1622 sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
1623 fatalx("repository name too long: %s", repo->name);
1624 log_debug("starting client uid %d session for repository %s",
1625 client->euid, repo->name);
1626 if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >=
1627 sizeof(proc->repo_path))
1628 fatalx("repository path too long: %s", repo->path);
1629 if (socketpair(AF_UNIX, sock_flags, PF_UNSPEC, proc->pipe) == -1)
1630 fatal("socketpair");
1631 proc->pid = start_child(proc->type, proc->repo_path, argv0,
1632 confpath, proc->pipe[1], daemonize, verbosity);
1633 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1634 log_debug("proc %s %s is on fd %d",
1635 gotd_proc_names[proc->type], proc->repo_path,
1636 proc->pipe[0]);
1637 proc->iev.handler = gotd_dispatch_client_session;
1638 proc->iev.events = EV_READ;
1639 proc->iev.handler_arg = NULL;
1640 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1641 gotd_dispatch_client_session, &proc->iev);
1642 gotd_imsg_event_add(&proc->iev);
1644 client->session = proc;
1645 return NULL;
1648 static const struct got_error *
1649 start_repo_child(struct gotd_client *client, enum gotd_procid proc_type,
1650 struct gotd_repo *repo, char *argv0, const char *confpath,
1651 int daemonize, int verbosity)
1653 struct gotd_child_proc *proc;
1654 int sock_flags = SOCK_STREAM|SOCK_NONBLOCK;
1656 #ifdef SOCK_CLOEXEC
1657 sock_flags |= SOCK_CLOEXEC;
1658 #endif
1660 if (proc_type != PROC_REPO_READ && proc_type != PROC_REPO_WRITE)
1661 return got_error_msg(GOT_ERR_NOT_IMPL, "bad process type");
1663 proc = calloc(1, sizeof(*proc));
1664 if (proc == NULL)
1665 return got_error_from_errno("calloc");
1667 TAILQ_INSERT_HEAD(&procs, proc, entry);
1668 evtimer_set(&proc->tmo, kill_proc_timeout, proc);
1670 proc->type = proc_type;
1671 if (strlcpy(proc->repo_name, repo->name,
1672 sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
1673 fatalx("repository name too long: %s", repo->name);
1674 log_debug("starting %s for repository %s",
1675 proc->type == PROC_REPO_READ ? "reader" : "writer", repo->name);
1677 if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >=
1678 sizeof(proc->repo_path))
1679 fatalx("repository path too long: %s", repo->path);
1680 if (realpath(repo->path, proc->repo_path) == NULL)
1681 fatal("%s", repo->path);
1682 if (socketpair(AF_UNIX, sock_flags,
1683 PF_UNSPEC, proc->pipe) == -1)
1684 fatal("socketpair");
1685 proc->pid = start_child(proc->type, proc->repo_path, argv0,
1686 confpath, proc->pipe[1], daemonize, verbosity);
1687 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1688 log_debug("proc %s %s is on fd %d",
1689 gotd_proc_names[proc->type], proc->repo_path,
1690 proc->pipe[0]);
1691 proc->iev.handler = gotd_dispatch_repo_child;
1692 proc->iev.events = EV_READ;
1693 proc->iev.handler_arg = NULL;
1694 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1695 gotd_dispatch_repo_child, &proc->iev);
1696 gotd_imsg_event_add(&proc->iev);
1698 client->repo = proc;
1699 return NULL;
1702 static const struct got_error *
1703 start_auth_child(struct gotd_client *client, int required_auth,
1704 struct gotd_repo *repo, char *argv0, const char *confpath,
1705 int daemonize, int verbosity)
1707 const struct got_error *err = NULL;
1708 struct gotd_child_proc *proc;
1709 struct gotd_imsg_auth iauth;
1710 int fd;
1711 int sock_flags = SOCK_STREAM|SOCK_NONBLOCK;
1713 #ifdef SOCK_CLOEXEC
1714 sock_flags |= SOCK_CLOEXEC;
1715 #endif
1717 memset(&iauth, 0, sizeof(iauth));
1719 fd = dup(client->fd);
1720 if (fd == -1)
1721 return got_error_from_errno("dup");
1723 proc = calloc(1, sizeof(*proc));
1724 if (proc == NULL) {
1725 err = got_error_from_errno("calloc");
1726 close(fd);
1727 return err;
1730 TAILQ_INSERT_HEAD(&procs, proc, entry);
1731 evtimer_set(&proc->tmo, kill_proc_timeout, proc);
1733 proc->type = PROC_AUTH;
1734 if (strlcpy(proc->repo_name, repo->name,
1735 sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
1736 fatalx("repository name too long: %s", repo->name);
1737 log_debug("starting auth for uid %d repository %s",
1738 client->euid, repo->name);
1739 if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >=
1740 sizeof(proc->repo_path))
1741 fatalx("repository path too long: %s", repo->path);
1742 if (realpath(repo->path, proc->repo_path) == NULL)
1743 fatal("%s", repo->path);
1744 if (socketpair(AF_UNIX, sock_flags,
1745 PF_UNSPEC, proc->pipe) == -1)
1746 fatal("socketpair");
1747 proc->pid = start_child(proc->type, proc->repo_path, argv0,
1748 confpath, proc->pipe[1], daemonize, verbosity);
1749 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1750 log_debug("proc %s %s is on fd %d",
1751 gotd_proc_names[proc->type], proc->repo_path,
1752 proc->pipe[0]);
1753 proc->iev.handler = gotd_dispatch_auth_child;
1754 proc->iev.events = EV_READ;
1755 proc->iev.handler_arg = NULL;
1756 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1757 gotd_dispatch_auth_child, &proc->iev);
1758 gotd_imsg_event_add(&proc->iev);
1760 iauth.euid = client->euid;
1761 iauth.egid = client->egid;
1762 iauth.required_auth = required_auth;
1763 iauth.client_id = client->id;
1764 if (gotd_imsg_compose_event(&proc->iev, GOTD_IMSG_AUTHENTICATE,
1765 PROC_GOTD, fd, &iauth, sizeof(iauth)) == -1) {
1766 log_warn("imsg compose AUTHENTICATE");
1767 close(fd);
1768 /* Let the auth_timeout handler tidy up. */
1771 client->auth = proc;
1772 client->required_auth = required_auth;
1773 return NULL;
1776 static void
1777 apply_unveil_repo_readonly(const char *repo_path, int need_tmpdir)
1779 if (need_tmpdir) {
1780 if (unveil(GOT_TMPDIR_STR, "rwc") == -1)
1781 fatal("unveil %s", GOT_TMPDIR_STR);
1784 if (unveil(repo_path, "r") == -1)
1785 fatal("unveil %s", repo_path);
1787 if (unveil(NULL, NULL) == -1)
1788 fatal("unveil");
1791 static void
1792 apply_unveil_repo_readwrite(const char *repo_path)
1794 if (unveil(repo_path, "rwc") == -1)
1795 fatal("unveil %s", repo_path);
1797 if (unveil(GOT_TMPDIR_STR, "rwc") == -1)
1798 fatal("unveil %s", GOT_TMPDIR_STR);
1800 if (unveil(NULL, NULL) == -1)
1801 fatal("unveil");
1804 static void
1805 apply_unveil_none(void)
1807 if (unveil("/", "") == -1)
1808 fatal("unveil");
1810 if (unveil(NULL, NULL) == -1)
1811 fatal("unveil");
1814 static void
1815 apply_unveil_selfexec(void)
1817 if (unveil(gotd.argv0, "x") == -1)
1818 fatal("unveil %s", gotd.argv0);
1820 if (unveil(NULL, NULL) == -1)
1821 fatal("unveil");
1824 int
1825 main(int argc, char **argv)
1827 const struct got_error *error = NULL;
1828 int ch, fd = -1, daemonize = 1, verbosity = 0, noaction = 0;
1829 const char *confpath = GOTD_CONF_PATH;
1830 char *argv0 = argv[0];
1831 char title[2048];
1832 struct passwd *pw = NULL;
1833 char *repo_path = NULL;
1834 enum gotd_procid proc_id = PROC_GOTD;
1835 struct event evsigint, evsigterm, evsighup, evsigusr1, evsigchld;
1836 int *pack_fds = NULL, *temp_fds = NULL;
1837 struct gotd_repo *repo = NULL;
1839 TAILQ_INIT(&procs);
1841 log_init(1, LOG_DAEMON); /* Log to stderr until daemonized. */
1843 while ((ch = getopt(argc, argv, "Adf:LnP:RsSvW")) != -1) {
1844 switch (ch) {
1845 case 'A':
1846 proc_id = PROC_AUTH;
1847 break;
1848 case 'd':
1849 daemonize = 0;
1850 break;
1851 case 'f':
1852 confpath = optarg;
1853 break;
1854 case 'L':
1855 proc_id = PROC_LISTEN;
1856 break;
1857 case 'n':
1858 noaction = 1;
1859 break;
1860 case 'P':
1861 repo_path = realpath(optarg, NULL);
1862 if (repo_path == NULL)
1863 fatal("realpath '%s'", optarg);
1864 break;
1865 case 'R':
1866 proc_id = PROC_REPO_READ;
1867 break;
1868 case 's':
1869 proc_id = PROC_SESSION_READ;
1870 break;
1871 case 'S':
1872 proc_id = PROC_SESSION_WRITE;
1873 break;
1874 case 'v':
1875 if (verbosity < 3)
1876 verbosity++;
1877 break;
1878 case 'W':
1879 proc_id = PROC_REPO_WRITE;
1880 break;
1881 default:
1882 usage();
1886 argc -= optind;
1887 argv += optind;
1889 if (argc != 0)
1890 usage();
1892 if (geteuid() && (proc_id == PROC_GOTD || proc_id == PROC_LISTEN))
1893 fatalx("need root privileges");
1895 if (parse_config(confpath, proc_id, &gotd) != 0)
1896 return 1;
1898 pw = getpwnam(gotd.user_name);
1899 if (pw == NULL)
1900 fatalx("user %s not found", gotd.user_name);
1902 if (pw->pw_uid == 0)
1903 fatalx("cannot run %s as the superuser", getprogname());
1905 if (noaction) {
1906 fprintf(stderr, "configuration OK\n");
1907 return 0;
1910 gotd.argv0 = argv0;
1911 gotd.daemonize = daemonize;
1912 gotd.verbosity = verbosity;
1913 gotd.confpath = confpath;
1915 /* Require an absolute path in argv[0] for reliable re-exec. */
1916 if (!got_path_is_absolute(argv0))
1917 fatalx("bad path \"%s\": must be an absolute path", argv0);
1919 log_init(daemonize ? 0 : 1, LOG_DAEMON);
1920 log_setverbose(verbosity);
1922 if (proc_id == PROC_GOTD) {
1923 snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
1924 arc4random_buf(&clients_hash_key, sizeof(clients_hash_key));
1925 if (daemonize && daemon(1, 0) == -1)
1926 fatal("daemon");
1927 gotd.pid = getpid();
1928 start_listener(argv0, confpath, daemonize, verbosity);
1929 } else if (proc_id == PROC_LISTEN) {
1930 snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
1931 if (verbosity) {
1932 log_info("socket: %s", gotd.unix_socket_path);
1933 log_info("user: %s", pw->pw_name);
1936 fd = unix_socket_listen(gotd.unix_socket_path, pw->pw_uid,
1937 pw->pw_gid);
1938 if (fd == -1) {
1939 fatal("cannot listen on unix socket %s",
1940 gotd.unix_socket_path);
1942 } else if (proc_id == PROC_AUTH) {
1943 snprintf(title, sizeof(title), "%s %s",
1944 gotd_proc_names[proc_id], repo_path);
1945 } else if (proc_id == PROC_REPO_READ || proc_id == PROC_REPO_WRITE ||
1946 proc_id == PROC_SESSION_READ || proc_id == PROC_SESSION_WRITE) {
1947 error = got_repo_pack_fds_open(&pack_fds);
1948 if (error != NULL)
1949 fatalx("cannot open pack tempfiles: %s", error->msg);
1950 error = got_repo_temp_fds_open(&temp_fds);
1951 if (error != NULL)
1952 fatalx("cannot open pack tempfiles: %s", error->msg);
1953 if (repo_path == NULL)
1954 fatalx("repository path not specified");
1955 snprintf(title, sizeof(title), "%s %s",
1956 gotd_proc_names[proc_id], repo_path);
1957 } else
1958 fatal("invalid process id %d", proc_id);
1960 setproctitle("%s", title);
1961 log_procinit(title);
1963 /* Drop root privileges. */
1964 if (setgid(pw->pw_gid) == -1)
1965 fatal("setgid %d failed", pw->pw_gid);
1966 if (setuid(pw->pw_uid) == -1)
1967 fatal("setuid %d failed", pw->pw_uid);
1969 event_init();
1971 switch (proc_id) {
1972 case PROC_GOTD:
1973 #ifndef PROFILE
1974 /* "exec" promise will be limited to argv[0] via unveil(2). */
1975 if (pledge("stdio proc exec sendfd recvfd unveil", NULL) == -1)
1976 err(1, "pledge");
1977 #endif
1978 break;
1979 case PROC_LISTEN:
1980 #ifndef PROFILE
1981 if (pledge("stdio sendfd unix unveil", NULL) == -1)
1982 err(1, "pledge");
1983 #endif
1985 * Ensure that AF_UNIX bind(2) cannot be used with any other
1986 * sockets by revoking all filesystem access via unveil(2).
1988 apply_unveil_none();
1990 listen_main(title, fd, gotd.connection_limits,
1991 gotd.nconnection_limits);
1992 /* NOTREACHED */
1993 break;
1994 case PROC_AUTH:
1995 #ifndef PROFILE
1996 if (pledge("stdio getpw recvfd unix unveil", NULL) == -1)
1997 err(1, "pledge");
1998 #endif
2000 * We need the "unix" pledge promise for getpeername(2) only.
2001 * Ensure that AF_UNIX bind(2) cannot be used by revoking all
2002 * filesystem access via unveil(2). Access to password database
2003 * files will still work since "getpw" bypasses unveil(2).
2005 apply_unveil_none();
2007 auth_main(title, &gotd.repos, repo_path);
2008 /* NOTREACHED */
2009 break;
2010 case PROC_SESSION_READ:
2011 case PROC_SESSION_WRITE:
2012 #ifndef PROFILE
2014 * The "recvfd" promise is only needed during setup and
2015 * will be removed in a later pledge(2) call.
2017 if (pledge("stdio rpath wpath cpath recvfd sendfd fattr flock "
2018 "unveil", NULL) == -1)
2019 err(1, "pledge");
2020 #endif
2021 if (proc_id == PROC_SESSION_READ)
2022 apply_unveil_repo_readonly(repo_path, 1);
2023 else
2024 apply_unveil_repo_readwrite(repo_path);
2025 session_main(title, repo_path, pack_fds, temp_fds,
2026 &gotd.request_timeout, proc_id);
2027 /* NOTREACHED */
2028 break;
2029 case PROC_REPO_READ:
2030 #ifndef PROFILE
2031 if (pledge("stdio rpath recvfd unveil", NULL) == -1)
2032 err(1, "pledge");
2033 #endif
2034 apply_unveil_repo_readonly(repo_path, 0);
2035 repo_read_main(title, repo_path, pack_fds, temp_fds);
2036 /* NOTREACHED */
2037 exit(0);
2038 case PROC_REPO_WRITE:
2039 #ifndef PROFILE
2040 if (pledge("stdio rpath recvfd unveil", NULL) == -1)
2041 err(1, "pledge");
2042 #endif
2043 apply_unveil_repo_readonly(repo_path, 0);
2044 repo = gotd_find_repo_by_path(repo_path, &gotd);
2045 if (repo == NULL)
2046 fatalx("no repository for path %s", repo_path);
2047 repo_write_main(title, repo_path, pack_fds, temp_fds,
2048 &repo->protected_tag_namespaces,
2049 &repo->protected_branch_namespaces,
2050 &repo->protected_branches);
2051 /* NOTREACHED */
2052 exit(0);
2053 default:
2054 fatal("invalid process id %d", proc_id);
2057 if (proc_id != PROC_GOTD)
2058 fatal("invalid process id %d", proc_id);
2060 evtimer_set(&gotd.listen_proc->tmo, kill_proc_timeout,
2061 gotd.listen_proc);
2063 apply_unveil_selfexec();
2065 signal_set(&evsigint, SIGINT, gotd_sighdlr, NULL);
2066 signal_set(&evsigterm, SIGTERM, gotd_sighdlr, NULL);
2067 signal_set(&evsighup, SIGHUP, gotd_sighdlr, NULL);
2068 signal_set(&evsigusr1, SIGUSR1, gotd_sighdlr, NULL);
2069 signal_set(&evsigchld, SIGCHLD, gotd_sighdlr, NULL);
2070 signal(SIGPIPE, SIG_IGN);
2072 signal_add(&evsigint, NULL);
2073 signal_add(&evsigterm, NULL);
2074 signal_add(&evsighup, NULL);
2075 signal_add(&evsigusr1, NULL);
2076 signal_add(&evsigchld, NULL);
2078 gotd_imsg_event_add(&gotd.listen_proc->iev);
2080 event_dispatch();
2082 free(repo_path);
2083 gotd_shutdown();
2085 return 0;