Blob


1 /*
2 * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/queue.h>
18 #include <sys/tree.h>
19 #include <sys/time.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <sys/socket.h>
23 #include <sys/un.h>
24 #include <sys/wait.h>
25 #include <sys/resource.h>
27 #include <fcntl.h>
28 #include <err.h>
29 #include <errno.h>
30 #include <event.h>
31 #include <limits.h>
32 #include <pwd.h>
33 #include <imsg.h>
34 #include <sha1.h>
35 #include <sha2.h>
36 #include <signal.h>
37 #include <siphash.h>
38 #include <stdarg.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <syslog.h>
43 #include <unistd.h>
45 #include "got_error.h"
46 #include "got_opentemp.h"
47 #include "got_path.h"
48 #include "got_repository.h"
49 #include "got_object.h"
50 #include "got_reference.h"
52 #include "got_lib_delta.h"
53 #include "got_lib_object.h"
54 #include "got_lib_object_cache.h"
55 #include "got_lib_hash.h"
56 #include "got_lib_gitproto.h"
57 #include "got_lib_pack.h"
58 #include "got_lib_repository.h"
60 #include "gotd.h"
61 #include "log.h"
62 #include "listen.h"
63 #include "auth.h"
64 #include "session.h"
65 #include "repo_read.h"
66 #include "repo_write.h"
68 #ifndef nitems
69 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
70 #endif
72 enum gotd_client_state {
73 GOTD_CLIENT_STATE_NEW,
74 GOTD_CLIENT_STATE_ACCESS_GRANTED,
75 };
77 struct gotd_child_proc {
78 pid_t pid;
79 enum gotd_procid type;
80 char repo_name[NAME_MAX];
81 char repo_path[PATH_MAX];
82 int pipe[2];
83 struct gotd_imsgev iev;
84 struct event tmo;
86 TAILQ_ENTRY(gotd_child_proc) entry;
87 };
88 TAILQ_HEAD(gotd_procs, gotd_child_proc) procs;
90 struct gotd_client {
91 STAILQ_ENTRY(gotd_client) entry;
92 enum gotd_client_state state;
93 uint32_t id;
94 int fd;
95 struct gotd_imsgev iev;
96 struct event tmo;
97 uid_t euid;
98 gid_t egid;
99 char *username;
100 struct gotd_child_proc *repo;
101 struct gotd_child_proc *auth;
102 struct gotd_child_proc *session;
103 int required_auth;
104 };
105 STAILQ_HEAD(gotd_clients, gotd_client);
107 static struct gotd_clients gotd_clients[GOTD_CLIENT_TABLE_SIZE];
108 static SIPHASH_KEY clients_hash_key;
109 volatile int client_cnt;
110 static struct timeval auth_timeout = { 5, 0 };
111 static struct gotd gotd;
113 void gotd_sighdlr(int sig, short event, void *arg);
114 static void gotd_shutdown(void);
115 static const struct got_error *start_session_child(struct gotd_client *,
116 struct gotd_repo *, char *, const char *, int, int);
117 static const struct got_error *start_repo_child(struct gotd_client *,
118 enum gotd_procid, struct gotd_repo *, char *, const char *, int, int);
119 static const struct got_error *start_auth_child(struct gotd_client *, int,
120 struct gotd_repo *, char *, const char *, int, int);
121 static void kill_proc(struct gotd_child_proc *, int);
122 static void disconnect(struct gotd_client *);
124 __dead static void
125 usage(void)
127 fprintf(stderr, "usage: %s [-dnv] [-f config-file]\n", getprogname());
128 exit(1);
131 static int
132 unix_socket_listen(const char *unix_socket_path, uid_t uid, gid_t gid)
134 struct sockaddr_un sun;
135 int fd = -1;
136 mode_t old_umask, mode;
138 fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK| SOCK_CLOEXEC, 0);
139 if (fd == -1) {
140 log_warn("socket");
141 return -1;
144 sun.sun_family = AF_UNIX;
145 if (strlcpy(sun.sun_path, unix_socket_path,
146 sizeof(sun.sun_path)) >= sizeof(sun.sun_path)) {
147 log_warnx("%s: name too long", unix_socket_path);
148 close(fd);
149 return -1;
152 if (unlink(unix_socket_path) == -1) {
153 if (errno != ENOENT) {
154 log_warn("unlink %s", unix_socket_path);
155 close(fd);
156 return -1;
160 old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
161 mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
163 if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
164 log_warn("bind: %s", unix_socket_path);
165 close(fd);
166 umask(old_umask);
167 return -1;
170 umask(old_umask);
172 if (chmod(unix_socket_path, mode) == -1) {
173 log_warn("chmod %o %s", mode, unix_socket_path);
174 close(fd);
175 unlink(unix_socket_path);
176 return -1;
179 if (chown(unix_socket_path, uid, gid) == -1) {
180 log_warn("chown %s uid=%d gid=%d", unix_socket_path, uid, gid);
181 close(fd);
182 unlink(unix_socket_path);
183 return -1;
186 if (listen(fd, GOTD_UNIX_SOCKET_BACKLOG) == -1) {
187 log_warn("listen");
188 close(fd);
189 unlink(unix_socket_path);
190 return -1;
193 return fd;
196 static uint64_t
197 client_hash(uint32_t client_id)
199 return SipHash24(&clients_hash_key, &client_id, sizeof(client_id));
202 static void
203 add_client(struct gotd_client *client)
205 uint64_t slot = client_hash(client->id) % nitems(gotd_clients);
206 STAILQ_INSERT_HEAD(&gotd_clients[slot], client, entry);
207 client_cnt++;
210 static struct gotd_client *
211 find_client(uint32_t client_id)
213 uint64_t slot;
214 struct gotd_client *c;
216 slot = client_hash(client_id) % nitems(gotd_clients);
217 STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
218 if (c->id == client_id)
219 return c;
222 return NULL;
225 static struct gotd_client *
226 find_client_by_proc_fd(int fd)
228 uint64_t slot;
230 for (slot = 0; slot < nitems(gotd_clients); slot++) {
231 struct gotd_client *c;
233 STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
234 if (c->repo && c->repo->iev.ibuf.fd == fd)
235 return c;
236 if (c->auth && c->auth->iev.ibuf.fd == fd)
237 return c;
238 if (c->session && c->session->iev.ibuf.fd == fd)
239 return c;
243 return NULL;
246 static int
247 client_is_reading(struct gotd_client *client)
249 return (client->required_auth &
250 (GOTD_AUTH_READ | GOTD_AUTH_WRITE)) == GOTD_AUTH_READ;
253 static int
254 client_is_writing(struct gotd_client *client)
256 return (client->required_auth &
257 (GOTD_AUTH_READ | GOTD_AUTH_WRITE)) ==
258 (GOTD_AUTH_READ | GOTD_AUTH_WRITE);
261 static const struct got_error *
262 ensure_client_is_not_writing(struct gotd_client *client)
264 if (client_is_writing(client)) {
265 return got_error_fmt(GOT_ERR_BAD_PACKET,
266 "uid %d made a read-request but is writing to "
267 "a repository", client->euid);
270 return NULL;
273 static const struct got_error *
274 ensure_client_is_not_reading(struct gotd_client *client)
276 if (client_is_reading(client)) {
277 return got_error_fmt(GOT_ERR_BAD_PACKET,
278 "uid %d made a write-request but is reading from "
279 "a repository", client->euid);
282 return NULL;
285 static void
286 proc_done(struct gotd_child_proc *proc)
288 struct gotd_client *client;
290 TAILQ_REMOVE(&procs, proc, entry);
292 client = find_client_by_proc_fd(proc->iev.ibuf.fd);
293 if (client != NULL) {
294 if (proc == client->repo)
295 client->repo = NULL;
296 if (proc == client->auth)
297 client->auth = NULL;
298 if (proc == client->session)
299 client->session = NULL;
300 disconnect(client);
303 evtimer_del(&proc->tmo);
305 if (proc->iev.ibuf.fd != -1) {
306 event_del(&proc->iev.ev);
307 msgbuf_clear(&proc->iev.ibuf.w);
308 close(proc->iev.ibuf.fd);
311 free(proc);
314 static void
315 kill_repo_proc(struct gotd_client *client)
317 if (client->repo == NULL)
318 return;
320 kill_proc(client->repo, 0);
321 client->repo = NULL;
324 static void
325 kill_auth_proc(struct gotd_client *client)
327 if (client->auth == NULL)
328 return;
330 kill_proc(client->auth, 0);
331 client->auth = NULL;
334 static void
335 kill_session_proc(struct gotd_client *client)
337 if (client->session == NULL)
338 return;
340 kill_proc(client->session, 0);
341 client->session = NULL;
344 static void
345 disconnect(struct gotd_client *client)
347 struct gotd_imsg_disconnect idisconnect;
348 struct gotd_child_proc *listen_proc = gotd.listen_proc;
349 uint64_t slot;
351 log_debug("uid %d: disconnecting", client->euid);
353 kill_auth_proc(client);
354 kill_session_proc(client);
355 kill_repo_proc(client);
357 idisconnect.client_id = client->id;
358 if (gotd_imsg_compose_event(&listen_proc->iev,
359 GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
360 &idisconnect, sizeof(idisconnect)) == -1)
361 log_warn("imsg compose DISCONNECT");
363 slot = client_hash(client->id) % nitems(gotd_clients);
364 STAILQ_REMOVE(&gotd_clients[slot], client, gotd_client, entry);
365 imsg_clear(&client->iev.ibuf);
366 event_del(&client->iev.ev);
367 evtimer_del(&client->tmo);
368 if (client->fd != -1)
369 close(client->fd);
370 else if (client->iev.ibuf.fd != -1)
371 close(client->iev.ibuf.fd);
372 free(client->username);
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 client->euid, proc->pid);
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 client->euid, proc->pid);
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];
999 if (client->state != GOTD_CLIENT_STATE_ACCESS_GRANTED)
1000 return got_error_msg(GOT_ERR_BAD_REQUEST,
1001 "unexpected repo child ready signal received");
1003 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1004 PF_UNSPEC, pipe) == -1)
1005 fatal("socketpair");
1007 memset(&ireq, 0, sizeof(ireq));
1008 ireq.client_id = client->id;
1009 ireq.proc_id = repo_proc->type;
1011 /* Pass repo child pipe to session child process. */
1012 if (gotd_imsg_compose_event(session_iev, GOTD_IMSG_CONNECT_REPO_CHILD,
1013 PROC_GOTD, pipe[0], &ireq, sizeof(ireq)) == -1) {
1014 err = got_error_from_errno("imsg compose CONNECT_REPO_CHILD");
1015 close(pipe[0]);
1016 close(pipe[1]);
1017 return err;
1020 /* Pass session child pipe to repo child process. */
1021 if (gotd_imsg_compose_event(&repo_proc->iev,
1022 GOTD_IMSG_CONNECT_REPO_CHILD, PROC_GOTD, pipe[1], NULL, 0) == -1) {
1023 err = got_error_from_errno("imsg compose CONNECT_REPO_CHILD");
1024 close(pipe[1]);
1025 return err;
1028 return NULL;
1031 static void
1032 gotd_dispatch_listener(int fd, short event, void *arg)
1034 struct gotd_imsgev *iev = arg;
1035 struct imsgbuf *ibuf = &iev->ibuf;
1036 struct gotd_child_proc *proc = gotd.listen_proc;
1037 ssize_t n;
1038 int shut = 0;
1039 struct imsg imsg;
1041 if (proc->iev.ibuf.fd != fd)
1042 fatalx("%s: unexpected fd %d", __func__, fd);
1044 if (event & EV_READ) {
1045 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1046 fatal("imsg_read error");
1047 if (n == 0) {
1048 /* Connection closed. */
1049 shut = 1;
1050 goto done;
1054 if (event & EV_WRITE) {
1055 n = msgbuf_write(&ibuf->w);
1056 if (n == -1 && errno != EAGAIN)
1057 fatal("msgbuf_write");
1058 if (n == 0) {
1059 /* Connection closed. */
1060 shut = 1;
1061 goto done;
1065 for (;;) {
1066 const struct got_error *err = NULL;
1067 struct gotd_client *client = NULL;
1068 uint32_t client_id = 0;
1069 int do_disconnect = 0;
1071 if ((n = imsg_get(ibuf, &imsg)) == -1)
1072 fatal("%s: imsg_get error", __func__);
1073 if (n == 0) /* No more messages. */
1074 break;
1076 switch (imsg.hdr.type) {
1077 case GOTD_IMSG_ERROR:
1078 do_disconnect = 1;
1079 err = gotd_imsg_recv_error(&client_id, &imsg);
1080 break;
1081 case GOTD_IMSG_CONNECT:
1082 err = recv_connect(&client_id, &imsg);
1083 break;
1084 default:
1085 log_debug("unexpected imsg %d", imsg.hdr.type);
1086 break;
1089 client = find_client(client_id);
1090 if (client == NULL) {
1091 log_warnx("%s: client not found", __func__);
1092 imsg_free(&imsg);
1093 continue;
1096 if (err)
1097 log_warnx("uid %d: %s", client->euid, err->msg);
1099 if (do_disconnect) {
1100 if (err)
1101 disconnect_on_error(client, err);
1102 else
1103 disconnect(client);
1106 imsg_free(&imsg);
1108 done:
1109 if (!shut) {
1110 gotd_imsg_event_add(iev);
1111 } else {
1112 /* This pipe is dead. Remove its event handler */
1113 event_del(&iev->ev);
1114 event_loopexit(NULL);
1118 static void
1119 gotd_dispatch_auth_child(int fd, short event, void *arg)
1121 const struct got_error *err = NULL;
1122 struct gotd_imsgev *iev = arg;
1123 struct imsgbuf *ibuf = &iev->ibuf;
1124 struct gotd_client *client;
1125 struct gotd_repo *repo = NULL;
1126 ssize_t n;
1127 int shut = 0;
1128 struct imsg imsg;
1129 uint32_t client_id = 0;
1130 int do_disconnect = 0;
1131 size_t datalen;
1133 client = find_client_by_proc_fd(fd);
1134 if (client == NULL) {
1135 /* Can happen during process teardown. */
1136 warnx("cannot find client for fd %d", fd);
1137 shut = 1;
1138 goto done;
1141 if (client->auth == NULL)
1142 fatalx("cannot find auth child process for fd %d", fd);
1144 if (event & EV_READ) {
1145 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1146 fatal("imsg_read error");
1147 if (n == 0) {
1148 /* Connection closed. */
1149 shut = 1;
1150 goto done;
1154 if (event & EV_WRITE) {
1155 n = msgbuf_write(&ibuf->w);
1156 if (n == -1 && errno != EAGAIN)
1157 fatal("msgbuf_write");
1158 if (n == 0) {
1159 /* Connection closed. */
1160 shut = 1;
1162 goto done;
1165 if (client->auth->iev.ibuf.fd != fd)
1166 fatalx("%s: unexpected fd %d", __func__, fd);
1168 if ((n = imsg_get(ibuf, &imsg)) == -1)
1169 fatal("%s: imsg_get error", __func__);
1170 if (n == 0) /* No more messages. */
1171 return;
1173 evtimer_del(&client->tmo);
1175 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1177 switch (imsg.hdr.type) {
1178 case GOTD_IMSG_ERROR:
1179 do_disconnect = 1;
1180 err = gotd_imsg_recv_error(&client_id, &imsg);
1181 break;
1182 case GOTD_IMSG_ACCESS_GRANTED:
1183 if (client->state != GOTD_CLIENT_STATE_NEW) {
1184 do_disconnect = 1;
1185 err = got_error(GOT_ERR_PRIVSEP_MSG);
1187 break;
1188 default:
1189 do_disconnect = 1;
1190 log_debug("unexpected imsg %d", imsg.hdr.type);
1191 break;
1194 if (!verify_imsg_src(client, client->auth, &imsg)) {
1195 do_disconnect = 1;
1196 log_debug("dropping imsg type %d from PID %d",
1197 imsg.hdr.type, client->auth->pid);
1200 if (do_disconnect) {
1201 if (err)
1202 disconnect_on_error(client, err);
1203 else
1204 disconnect(client);
1205 imsg_free(&imsg);
1206 return;
1209 client->state = GOTD_CLIENT_STATE_ACCESS_GRANTED;
1210 if (datalen > 0)
1211 client->username = strndup(imsg.data, datalen);
1212 imsg_free(&imsg);
1213 if (client->username == NULL &&
1214 asprintf(&client->username, "uid %d", client->euid) == -1) {
1215 err = got_error_from_errno("asprintf");
1216 goto done;
1219 repo = gotd_find_repo_by_name(client->auth->repo_name, &gotd);
1220 if (repo == NULL) {
1221 err = got_error(GOT_ERR_NOT_GIT_REPO);
1222 goto done;
1224 kill_auth_proc(client);
1226 log_info("authenticated %s for repository %s",
1227 client->username, repo->name);
1229 err = start_session_child(client, repo, gotd.argv0,
1230 gotd.confpath, gotd.daemonize, gotd.verbosity);
1231 if (err)
1232 goto done;
1233 done:
1234 if (err)
1235 log_warnx("uid %d: %s", client->euid, err->msg);
1237 /* We might have killed the auth process by now. */
1238 if (client->auth != NULL) {
1239 if (!shut) {
1240 gotd_imsg_event_add(iev);
1241 } else {
1242 /* This pipe is dead. Remove its event handler */
1243 event_del(&iev->ev);
1248 static const struct got_error *
1249 connect_session(struct gotd_client *client)
1251 const struct got_error *err = NULL;
1252 struct gotd_imsg_connect iconnect;
1253 int s;
1255 memset(&iconnect, 0, sizeof(iconnect));
1257 s = dup(client->fd);
1258 if (s == -1)
1259 return got_error_from_errno("dup");
1261 iconnect.client_id = client->id;
1262 iconnect.euid = client->euid;
1263 iconnect.egid = client->egid;
1265 if (gotd_imsg_compose_event(&client->session->iev, GOTD_IMSG_CONNECT,
1266 PROC_GOTD, s, &iconnect, sizeof(iconnect)) == -1) {
1267 err = got_error_from_errno("imsg compose CONNECT");
1268 close(s);
1269 return err;
1273 * We are no longer interested in messages from this client.
1274 * Further client requests will be handled by the session process.
1276 msgbuf_clear(&client->iev.ibuf.w);
1277 imsg_clear(&client->iev.ibuf);
1278 event_del(&client->iev.ev);
1279 client->fd = -1; /* will be closed via copy in client->iev.ibuf.fd */
1281 return NULL;
1284 static void
1285 gotd_dispatch_client_session(int fd, short event, void *arg)
1287 struct gotd_imsgev *iev = arg;
1288 struct imsgbuf *ibuf = &iev->ibuf;
1289 struct gotd_child_proc *proc = NULL;
1290 struct gotd_client *client = NULL;
1291 ssize_t n;
1292 int shut = 0;
1293 struct imsg imsg;
1295 client = find_client_by_proc_fd(fd);
1296 if (client == NULL) {
1297 /* Can happen during process teardown. */
1298 warnx("cannot find client for fd %d", fd);
1299 shut = 1;
1300 goto done;
1303 if (event & EV_READ) {
1304 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1305 fatal("imsg_read error");
1306 if (n == 0) {
1307 /* Connection closed. */
1308 shut = 1;
1309 goto done;
1313 if (event & EV_WRITE) {
1314 n = msgbuf_write(&ibuf->w);
1315 if (n == -1 && errno != EAGAIN)
1316 fatal("msgbuf_write");
1317 if (n == 0) {
1318 /* Connection closed. */
1319 shut = 1;
1320 goto done;
1324 proc = client->session;
1325 if (proc == NULL)
1326 fatalx("cannot find session child process for fd %d", fd);
1328 for (;;) {
1329 const struct got_error *err = NULL;
1330 uint32_t client_id = 0;
1331 int do_disconnect = 0, do_start_repo_child = 0;
1333 if ((n = imsg_get(ibuf, &imsg)) == -1)
1334 fatal("%s: imsg_get error", __func__);
1335 if (n == 0) /* No more messages. */
1336 break;
1338 switch (imsg.hdr.type) {
1339 case GOTD_IMSG_ERROR:
1340 do_disconnect = 1;
1341 err = gotd_imsg_recv_error(&client_id, &imsg);
1342 break;
1343 case GOTD_IMSG_CLIENT_SESSION_READY:
1344 if (client->state != GOTD_CLIENT_STATE_ACCESS_GRANTED) {
1345 err = got_error(GOT_ERR_PRIVSEP_MSG);
1346 break;
1348 do_start_repo_child = 1;
1349 break;
1350 case GOTD_IMSG_DISCONNECT:
1351 do_disconnect = 1;
1352 break;
1353 default:
1354 log_debug("unexpected imsg %d", imsg.hdr.type);
1355 break;
1358 if (!verify_imsg_src(client, proc, &imsg)) {
1359 log_debug("dropping imsg type %d from PID %d",
1360 imsg.hdr.type, proc->pid);
1361 imsg_free(&imsg);
1362 continue;
1364 if (err)
1365 log_warnx("uid %d: %s", client->euid, err->msg);
1367 if (do_start_repo_child) {
1368 struct gotd_repo *repo;
1369 const char *name = client->session->repo_name;
1371 repo = gotd_find_repo_by_name(name, &gotd);
1372 if (repo != NULL) {
1373 enum gotd_procid proc_type;
1375 if (client->required_auth & GOTD_AUTH_WRITE)
1376 proc_type = PROC_REPO_WRITE;
1377 else
1378 proc_type = PROC_REPO_READ;
1380 err = start_repo_child(client, proc_type, repo,
1381 gotd.argv0, gotd.confpath, gotd.daemonize,
1382 gotd.verbosity);
1383 } else
1384 err = got_error(GOT_ERR_NOT_GIT_REPO);
1386 if (err) {
1387 log_warnx("uid %d: %s", client->euid, err->msg);
1388 do_disconnect = 1;
1392 if (do_disconnect) {
1393 if (err)
1394 disconnect_on_error(client, err);
1395 else
1396 disconnect(client);
1399 imsg_free(&imsg);
1401 done:
1402 if (!shut) {
1403 gotd_imsg_event_add(iev);
1404 } else {
1405 /* This pipe is dead. Remove its event handler */
1406 event_del(&iev->ev);
1407 disconnect(client);
1411 static void
1412 gotd_dispatch_repo_child(int fd, short event, void *arg)
1414 struct gotd_imsgev *iev = arg;
1415 struct imsgbuf *ibuf = &iev->ibuf;
1416 struct gotd_child_proc *proc = NULL;
1417 struct gotd_client *client;
1418 ssize_t n;
1419 int shut = 0;
1420 struct imsg imsg;
1422 client = find_client_by_proc_fd(fd);
1423 if (client == NULL) {
1424 /* Can happen during process teardown. */
1425 warnx("cannot find client for fd %d", fd);
1426 shut = 1;
1427 goto done;
1430 if (event & EV_READ) {
1431 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1432 fatal("imsg_read error");
1433 if (n == 0) {
1434 /* Connection closed. */
1435 shut = 1;
1436 goto done;
1440 if (event & EV_WRITE) {
1441 n = msgbuf_write(&ibuf->w);
1442 if (n == -1 && errno != EAGAIN)
1443 fatal("msgbuf_write");
1444 if (n == 0) {
1445 /* Connection closed. */
1446 shut = 1;
1447 goto done;
1451 proc = client->repo;
1452 if (proc == NULL)
1453 fatalx("cannot find child process for fd %d", fd);
1455 for (;;) {
1456 const struct got_error *err = NULL;
1457 uint32_t client_id = 0;
1458 int do_disconnect = 0;
1460 if ((n = imsg_get(ibuf, &imsg)) == -1)
1461 fatal("%s: imsg_get error", __func__);
1462 if (n == 0) /* No more messages. */
1463 break;
1465 switch (imsg.hdr.type) {
1466 case GOTD_IMSG_ERROR:
1467 do_disconnect = 1;
1468 err = gotd_imsg_recv_error(&client_id, &imsg);
1469 break;
1470 case GOTD_IMSG_REPO_CHILD_READY:
1471 err = connect_session(client);
1472 if (err)
1473 break;
1474 err = connect_repo_child(client, proc);
1475 break;
1476 default:
1477 log_debug("unexpected imsg %d", imsg.hdr.type);
1478 break;
1481 if (!verify_imsg_src(client, proc, &imsg)) {
1482 log_debug("dropping imsg type %d from PID %d",
1483 imsg.hdr.type, proc->pid);
1484 imsg_free(&imsg);
1485 continue;
1487 if (err)
1488 log_warnx("uid %d: %s", client->euid, err->msg);
1490 if (do_disconnect) {
1491 if (err)
1492 disconnect_on_error(client, err);
1493 else
1494 disconnect(client);
1497 imsg_free(&imsg);
1499 done:
1500 if (!shut) {
1501 gotd_imsg_event_add(iev);
1502 } else {
1503 /* This pipe is dead. Remove its event handler */
1504 event_del(&iev->ev);
1505 disconnect(client);
1509 static pid_t
1510 start_child(enum gotd_procid proc_id, const char *repo_path,
1511 char *argv0, const char *confpath, int fd, int daemonize, int verbosity)
1513 char *argv[11];
1514 int argc = 0;
1515 pid_t pid;
1517 switch (pid = fork()) {
1518 case -1:
1519 fatal("cannot fork");
1520 case 0:
1521 break;
1522 default:
1523 close(fd);
1524 return pid;
1527 if (fd != GOTD_FILENO_MSG_PIPE) {
1528 if (dup2(fd, GOTD_FILENO_MSG_PIPE) == -1)
1529 fatal("cannot setup imsg fd");
1530 } else if (fcntl(fd, F_SETFD, 0) == -1)
1531 fatal("cannot setup imsg fd");
1533 argv[argc++] = argv0;
1534 switch (proc_id) {
1535 case PROC_LISTEN:
1536 argv[argc++] = (char *)"-L";
1537 break;
1538 case PROC_AUTH:
1539 argv[argc++] = (char *)"-A";
1540 break;
1541 case PROC_SESSION_READ:
1542 argv[argc++] = (char *)"-s";
1543 break;
1544 case PROC_SESSION_WRITE:
1545 argv[argc++] = (char *)"-S";
1546 break;
1547 case PROC_REPO_READ:
1548 argv[argc++] = (char *)"-R";
1549 break;
1550 case PROC_REPO_WRITE:
1551 argv[argc++] = (char *)"-W";
1552 break;
1553 default:
1554 fatalx("invalid process id %d", proc_id);
1557 argv[argc++] = (char *)"-f";
1558 argv[argc++] = (char *)confpath;
1560 if (repo_path) {
1561 argv[argc++] = (char *)"-P";
1562 argv[argc++] = (char *)repo_path;
1565 if (!daemonize)
1566 argv[argc++] = (char *)"-d";
1567 if (verbosity > 0)
1568 argv[argc++] = (char *)"-v";
1569 if (verbosity > 1)
1570 argv[argc++] = (char *)"-v";
1571 argv[argc++] = NULL;
1573 execvp(argv0, argv);
1574 fatal("execvp");
1577 static void
1578 start_listener(char *argv0, const char *confpath, int daemonize, int verbosity)
1580 struct gotd_child_proc *proc;
1582 proc = calloc(1, sizeof(*proc));
1583 if (proc == NULL)
1584 fatal("calloc");
1586 TAILQ_INSERT_HEAD(&procs, proc, entry);
1588 /* proc->tmo is initialized in main() after event_init() */
1590 proc->type = PROC_LISTEN;
1592 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1593 PF_UNSPEC, proc->pipe) == -1)
1594 fatal("socketpair");
1596 proc->pid = start_child(proc->type, NULL, argv0, confpath,
1597 proc->pipe[1], daemonize, verbosity);
1598 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1599 proc->iev.handler = gotd_dispatch_listener;
1600 proc->iev.events = EV_READ;
1601 proc->iev.handler_arg = NULL;
1603 gotd.listen_proc = proc;
1606 static const struct got_error *
1607 start_session_child(struct gotd_client *client, struct gotd_repo *repo,
1608 char *argv0, const char *confpath, int daemonize, int verbosity)
1610 struct gotd_child_proc *proc;
1612 proc = calloc(1, sizeof(*proc));
1613 if (proc == NULL)
1614 return got_error_from_errno("calloc");
1616 TAILQ_INSERT_HEAD(&procs, proc, entry);
1617 evtimer_set(&proc->tmo, kill_proc_timeout, proc);
1619 if (client_is_reading(client))
1620 proc->type = PROC_SESSION_READ;
1621 else
1622 proc->type = PROC_SESSION_WRITE;
1623 if (strlcpy(proc->repo_name, repo->name,
1624 sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
1625 fatalx("repository name too long: %s", repo->name);
1626 log_debug("starting client uid %d session for repository %s",
1627 client->euid, repo->name);
1628 if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >=
1629 sizeof(proc->repo_path))
1630 fatalx("repository path too long: %s", repo->path);
1631 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1632 PF_UNSPEC, proc->pipe) == -1)
1633 fatal("socketpair");
1634 proc->pid = start_child(proc->type, proc->repo_path, argv0,
1635 confpath, proc->pipe[1], daemonize, verbosity);
1636 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1637 log_debug("proc %s %s is on fd %d",
1638 gotd_proc_names[proc->type], proc->repo_path,
1639 proc->pipe[0]);
1640 proc->iev.handler = gotd_dispatch_client_session;
1641 proc->iev.events = EV_READ;
1642 proc->iev.handler_arg = NULL;
1643 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1644 gotd_dispatch_client_session, &proc->iev);
1645 gotd_imsg_event_add(&proc->iev);
1647 client->session = proc;
1648 return NULL;
1651 static const struct got_error *
1652 start_repo_child(struct gotd_client *client, enum gotd_procid proc_type,
1653 struct gotd_repo *repo, char *argv0, const char *confpath,
1654 int daemonize, int verbosity)
1656 struct gotd_child_proc *proc;
1658 if (proc_type != PROC_REPO_READ && proc_type != PROC_REPO_WRITE)
1659 return got_error_msg(GOT_ERR_NOT_IMPL, "bad process type");
1661 proc = calloc(1, sizeof(*proc));
1662 if (proc == NULL)
1663 return got_error_from_errno("calloc");
1665 TAILQ_INSERT_HEAD(&procs, proc, entry);
1666 evtimer_set(&proc->tmo, kill_proc_timeout, proc);
1668 proc->type = proc_type;
1669 if (strlcpy(proc->repo_name, repo->name,
1670 sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
1671 fatalx("repository name too long: %s", repo->name);
1672 log_debug("starting %s for repository %s",
1673 proc->type == PROC_REPO_READ ? "reader" : "writer", repo->name);
1674 if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >=
1675 sizeof(proc->repo_path))
1676 fatalx("repository path too long: %s", repo->path);
1677 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1678 PF_UNSPEC, proc->pipe) == -1)
1679 fatal("socketpair");
1680 proc->pid = start_child(proc->type, proc->repo_path, argv0,
1681 confpath, proc->pipe[1], daemonize, verbosity);
1682 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1683 log_debug("proc %s %s is on fd %d",
1684 gotd_proc_names[proc->type], proc->repo_path,
1685 proc->pipe[0]);
1686 proc->iev.handler = gotd_dispatch_repo_child;
1687 proc->iev.events = EV_READ;
1688 proc->iev.handler_arg = NULL;
1689 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1690 gotd_dispatch_repo_child, &proc->iev);
1691 gotd_imsg_event_add(&proc->iev);
1693 client->repo = proc;
1694 return NULL;
1697 static const struct got_error *
1698 start_auth_child(struct gotd_client *client, int required_auth,
1699 struct gotd_repo *repo, char *argv0, const char *confpath,
1700 int daemonize, int verbosity)
1702 const struct got_error *err = NULL;
1703 struct gotd_child_proc *proc;
1704 struct gotd_imsg_auth iauth;
1705 int fd;
1707 memset(&iauth, 0, sizeof(iauth));
1709 fd = dup(client->fd);
1710 if (fd == -1)
1711 return got_error_from_errno("dup");
1713 proc = calloc(1, sizeof(*proc));
1714 if (proc == NULL) {
1715 err = got_error_from_errno("calloc");
1716 close(fd);
1717 return err;
1720 TAILQ_INSERT_HEAD(&procs, proc, entry);
1721 evtimer_set(&proc->tmo, kill_proc_timeout, proc);
1723 proc->type = PROC_AUTH;
1724 if (strlcpy(proc->repo_name, repo->name,
1725 sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
1726 fatalx("repository name too long: %s", repo->name);
1727 log_debug("starting auth for uid %d repository %s",
1728 client->euid, repo->name);
1729 if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >=
1730 sizeof(proc->repo_path))
1731 fatalx("repository path too long: %s", repo->path);
1732 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1733 PF_UNSPEC, proc->pipe) == -1)
1734 fatal("socketpair");
1735 proc->pid = start_child(proc->type, proc->repo_path, argv0,
1736 confpath, proc->pipe[1], daemonize, verbosity);
1737 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1738 log_debug("proc %s %s is on fd %d",
1739 gotd_proc_names[proc->type], proc->repo_path,
1740 proc->pipe[0]);
1741 proc->iev.handler = gotd_dispatch_auth_child;
1742 proc->iev.events = EV_READ;
1743 proc->iev.handler_arg = NULL;
1744 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1745 gotd_dispatch_auth_child, &proc->iev);
1746 gotd_imsg_event_add(&proc->iev);
1748 iauth.euid = client->euid;
1749 iauth.egid = client->egid;
1750 iauth.required_auth = required_auth;
1751 iauth.client_id = client->id;
1752 if (gotd_imsg_compose_event(&proc->iev, GOTD_IMSG_AUTHENTICATE,
1753 PROC_GOTD, fd, &iauth, sizeof(iauth)) == -1) {
1754 log_warn("imsg compose AUTHENTICATE");
1755 close(fd);
1756 /* Let the auth_timeout handler tidy up. */
1759 client->auth = proc;
1760 client->required_auth = required_auth;
1761 return NULL;
1764 static void
1765 apply_unveil_repo_readonly(const char *repo_path, int need_tmpdir)
1767 if (need_tmpdir) {
1768 if (unveil(GOT_TMPDIR_STR, "rwc") == -1)
1769 fatal("unveil %s", GOT_TMPDIR_STR);
1772 if (unveil(repo_path, "r") == -1)
1773 fatal("unveil %s", repo_path);
1775 if (unveil(NULL, NULL) == -1)
1776 fatal("unveil");
1779 static void
1780 apply_unveil_repo_readwrite(const char *repo_path)
1782 if (unveil(repo_path, "rwc") == -1)
1783 fatal("unveil %s", repo_path);
1785 if (unveil(GOT_TMPDIR_STR, "rwc") == -1)
1786 fatal("unveil %s", GOT_TMPDIR_STR);
1788 if (unveil(NULL, NULL) == -1)
1789 fatal("unveil");
1792 static void
1793 apply_unveil_none(void)
1795 if (unveil("/", "") == -1)
1796 fatal("unveil");
1798 if (unveil(NULL, NULL) == -1)
1799 fatal("unveil");
1802 static void
1803 apply_unveil_selfexec(void)
1805 if (unveil(gotd.argv0, "x") == -1)
1806 fatal("unveil %s", gotd.argv0);
1808 if (unveil(NULL, NULL) == -1)
1809 fatal("unveil");
1812 static void
1813 set_max_datasize(void)
1815 struct rlimit rl;
1817 if (getrlimit(RLIMIT_DATA, &rl) != 0)
1818 return;
1820 rl.rlim_cur = rl.rlim_max;
1821 setrlimit(RLIMIT_DATA, &rl);
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 set_max_datasize();
2031 #ifndef PROFILE
2032 if (pledge("stdio rpath recvfd unveil", NULL) == -1)
2033 err(1, "pledge");
2034 #endif
2035 apply_unveil_repo_readonly(repo_path, 0);
2036 repo_read_main(title, repo_path, pack_fds, temp_fds);
2037 /* NOTREACHED */
2038 exit(0);
2039 case PROC_REPO_WRITE:
2040 set_max_datasize();
2041 #ifndef PROFILE
2042 if (pledge("stdio rpath recvfd unveil", NULL) == -1)
2043 err(1, "pledge");
2044 #endif
2045 apply_unveil_repo_readonly(repo_path, 0);
2046 repo = gotd_find_repo_by_path(repo_path, &gotd);
2047 if (repo == NULL)
2048 fatalx("no repository for path %s", repo_path);
2049 repo_write_main(title, repo_path, pack_fds, temp_fds,
2050 &repo->protected_tag_namespaces,
2051 &repo->protected_branch_namespaces,
2052 &repo->protected_branches);
2053 /* NOTREACHED */
2054 exit(0);
2055 default:
2056 fatal("invalid process id %d", proc_id);
2059 if (proc_id != PROC_GOTD)
2060 fatal("invalid process id %d", proc_id);
2062 evtimer_set(&gotd.listen_proc->tmo, kill_proc_timeout,
2063 gotd.listen_proc);
2065 apply_unveil_selfexec();
2067 signal_set(&evsigint, SIGINT, gotd_sighdlr, NULL);
2068 signal_set(&evsigterm, SIGTERM, gotd_sighdlr, NULL);
2069 signal_set(&evsighup, SIGHUP, gotd_sighdlr, NULL);
2070 signal_set(&evsigusr1, SIGUSR1, gotd_sighdlr, NULL);
2071 signal_set(&evsigchld, SIGCHLD, gotd_sighdlr, NULL);
2072 signal(SIGPIPE, SIG_IGN);
2074 signal_add(&evsigint, NULL);
2075 signal_add(&evsigterm, NULL);
2076 signal_add(&evsighup, NULL);
2077 signal_add(&evsigusr1, NULL);
2078 signal_add(&evsigchld, NULL);
2080 gotd_imsg_event_add(&gotd.listen_proc->iev);
2082 event_dispatch();
2084 free(repo_path);
2085 gotd_shutdown();
2087 return 0;