Blob


1 /*
2 * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/queue.h>
18 #include <sys/time.h>
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <sys/socket.h>
22 #include <sys/un.h>
23 #include <sys/wait.h>
25 #include <fcntl.h>
26 #include <err.h>
27 #include <errno.h>
28 #include <event.h>
29 #include <limits.h>
30 #include <pwd.h>
31 #include <imsg.h>
32 #include <signal.h>
33 #include <siphash.h>
34 #include <stdarg.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <syslog.h>
39 #include <unistd.h>
41 #include "got_error.h"
42 #include "got_opentemp.h"
43 #include "got_path.h"
44 #include "got_repository.h"
45 #include "got_object.h"
46 #include "got_reference.h"
48 #include "got_lib_delta.h"
49 #include "got_lib_object.h"
50 #include "got_lib_object_cache.h"
51 #include "got_lib_hash.h"
52 #include "got_lib_gitproto.h"
53 #include "got_lib_pack.h"
54 #include "got_lib_repository.h"
56 #include "gotd.h"
57 #include "log.h"
58 #include "listen.h"
59 #include "auth.h"
60 #include "session.h"
61 #include "repo_read.h"
62 #include "repo_write.h"
64 #ifndef nitems
65 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
66 #endif
68 enum gotd_client_state {
69 GOTD_CLIENT_STATE_NEW,
70 GOTD_CLIENT_STATE_ACCESS_GRANTED,
71 };
73 struct gotd_client {
74 STAILQ_ENTRY(gotd_client) entry;
75 enum gotd_client_state state;
76 uint32_t id;
77 int fd;
78 struct gotd_imsgev iev;
79 struct event tmo;
80 uid_t euid;
81 gid_t egid;
82 struct gotd_child_proc *repo;
83 struct gotd_child_proc *auth;
84 struct gotd_child_proc *session;
85 int required_auth;
86 };
87 STAILQ_HEAD(gotd_clients, gotd_client);
89 static struct gotd_clients gotd_clients[GOTD_CLIENT_TABLE_SIZE];
90 static SIPHASH_KEY clients_hash_key;
91 volatile int client_cnt;
92 static struct timeval auth_timeout = { 5, 0 };
93 static struct gotd gotd;
95 void gotd_sighdlr(int sig, short event, void *arg);
96 static void gotd_shutdown(void);
97 static const struct got_error *start_session_child(struct gotd_client *,
98 struct gotd_repo *, char *, const char *, int, int);
99 static const struct got_error *start_repo_child(struct gotd_client *,
100 enum gotd_procid, struct gotd_repo *, char *, const char *, int, int);
101 static const struct got_error *start_auth_child(struct gotd_client *, int,
102 struct gotd_repo *, char *, const char *, int, int);
103 static void kill_proc(struct gotd_child_proc *, int);
105 __dead static void
106 usage(void)
108 fprintf(stderr, "usage: %s [-dnv] [-f config-file]\n", getprogname());
109 exit(1);
112 static int
113 unix_socket_listen(const char *unix_socket_path, uid_t uid, gid_t gid)
115 struct sockaddr_un sun;
116 int fd = -1;
117 mode_t old_umask, mode;
119 fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK| SOCK_CLOEXEC, 0);
120 if (fd == -1) {
121 log_warn("socket");
122 return -1;
125 sun.sun_family = AF_UNIX;
126 if (strlcpy(sun.sun_path, unix_socket_path,
127 sizeof(sun.sun_path)) >= sizeof(sun.sun_path)) {
128 log_warnx("%s: name too long", unix_socket_path);
129 close(fd);
130 return -1;
133 if (unlink(unix_socket_path) == -1) {
134 if (errno != ENOENT) {
135 log_warn("unlink %s", unix_socket_path);
136 close(fd);
137 return -1;
141 old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
142 mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
144 if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
145 log_warn("bind: %s", unix_socket_path);
146 close(fd);
147 umask(old_umask);
148 return -1;
151 umask(old_umask);
153 if (chmod(unix_socket_path, mode) == -1) {
154 log_warn("chmod %o %s", mode, unix_socket_path);
155 close(fd);
156 unlink(unix_socket_path);
157 return -1;
160 if (chown(unix_socket_path, uid, gid) == -1) {
161 log_warn("chown %s uid=%d gid=%d", unix_socket_path, uid, gid);
162 close(fd);
163 unlink(unix_socket_path);
164 return -1;
167 if (listen(fd, GOTD_UNIX_SOCKET_BACKLOG) == -1) {
168 log_warn("listen");
169 close(fd);
170 unlink(unix_socket_path);
171 return -1;
174 return fd;
177 static uint64_t
178 client_hash(uint32_t client_id)
180 return SipHash24(&clients_hash_key, &client_id, sizeof(client_id));
183 static void
184 add_client(struct gotd_client *client)
186 uint64_t slot = client_hash(client->id) % nitems(gotd_clients);
187 STAILQ_INSERT_HEAD(&gotd_clients[slot], client, entry);
188 client_cnt++;
191 static struct gotd_client *
192 find_client(uint32_t client_id)
194 uint64_t slot;
195 struct gotd_client *c;
197 slot = client_hash(client_id) % nitems(gotd_clients);
198 STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
199 if (c->id == client_id)
200 return c;
203 return NULL;
206 static struct gotd_client *
207 find_client_by_proc_fd(int fd)
209 uint64_t slot;
211 for (slot = 0; slot < nitems(gotd_clients); slot++) {
212 struct gotd_client *c;
214 STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
215 if (c->repo && c->repo->iev.ibuf.fd == fd)
216 return c;
217 if (c->auth && c->auth->iev.ibuf.fd == fd)
218 return c;
219 if (c->session && c->session->iev.ibuf.fd == fd)
220 return c;
224 return NULL;
227 static int
228 client_is_reading(struct gotd_client *client)
230 return (client->required_auth &
231 (GOTD_AUTH_READ | GOTD_AUTH_WRITE)) == GOTD_AUTH_READ;
234 static int
235 client_is_writing(struct gotd_client *client)
237 return (client->required_auth &
238 (GOTD_AUTH_READ | GOTD_AUTH_WRITE)) ==
239 (GOTD_AUTH_READ | GOTD_AUTH_WRITE);
242 static const struct got_error *
243 ensure_client_is_not_writing(struct gotd_client *client)
245 if (client_is_writing(client)) {
246 return got_error_fmt(GOT_ERR_BAD_PACKET,
247 "uid %d made a read-request but is writing to "
248 "a repository", client->euid);
251 return NULL;
254 static const struct got_error *
255 ensure_client_is_not_reading(struct gotd_client *client)
257 if (client_is_reading(client)) {
258 return got_error_fmt(GOT_ERR_BAD_PACKET,
259 "uid %d made a write-request but is reading from "
260 "a repository", client->euid);
263 return NULL;
266 static void
267 wait_for_child(pid_t child_pid)
269 pid_t pid;
270 int status;
272 log_debug("waiting for child PID %ld to terminate",
273 (long)child_pid);
275 do {
276 pid = waitpid(child_pid, &status, WNOHANG);
277 if (pid == -1) {
278 if (errno != EINTR && errno != ECHILD)
279 fatal("wait");
280 } else if (WIFSIGNALED(status)) {
281 log_warnx("child PID %ld terminated; signal %d",
282 (long)pid, WTERMSIG(status));
284 } while (pid != -1 || (pid == -1 && errno == EINTR));
287 static void
288 proc_done(struct gotd_child_proc *proc)
290 event_del(&proc->iev.ev);
291 msgbuf_clear(&proc->iev.ibuf.w);
292 close(proc->iev.ibuf.fd);
293 kill_proc(proc, 0);
294 wait_for_child(proc->pid);
295 free(proc);
298 static void
299 kill_auth_proc(struct gotd_client *client)
301 struct gotd_child_proc *proc;
303 if (client->auth == NULL)
304 return;
306 proc = client->auth;
307 client->auth = NULL;
309 proc_done(proc);
312 static void
313 kill_session_proc(struct gotd_client *client)
315 struct gotd_child_proc *proc;
317 if (client->session == NULL)
318 return;
320 proc = client->session;
321 client->session = NULL;
323 proc_done(proc);
326 static void
327 disconnect(struct gotd_client *client)
329 struct gotd_imsg_disconnect idisconnect;
330 struct gotd_child_proc *proc = client->repo;
331 struct gotd_child_proc *listen_proc = &gotd.listen_proc;
332 uint64_t slot;
334 log_debug("uid %d: disconnecting", client->euid);
336 kill_auth_proc(client);
337 kill_session_proc(client);
339 if (proc) {
340 event_del(&proc->iev.ev);
341 msgbuf_clear(&proc->iev.ibuf.w);
342 close(proc->iev.ibuf.fd);
343 kill_proc(proc, 0);
344 wait_for_child(proc->pid);
345 free(proc);
346 proc = NULL;
349 idisconnect.client_id = client->id;
350 if (gotd_imsg_compose_event(&listen_proc->iev,
351 GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
352 &idisconnect, sizeof(idisconnect)) == -1)
353 log_warn("imsg compose DISCONNECT");
355 slot = client_hash(client->id) % nitems(gotd_clients);
356 STAILQ_REMOVE(&gotd_clients[slot], client, gotd_client, entry);
357 imsg_clear(&client->iev.ibuf);
358 event_del(&client->iev.ev);
359 evtimer_del(&client->tmo);
360 if (client->fd != -1)
361 close(client->fd);
362 else if (client->iev.ibuf.fd != -1)
363 close(client->iev.ibuf.fd);
364 free(client);
365 client_cnt--;
368 static void
369 disconnect_on_error(struct gotd_client *client, const struct got_error *err)
371 struct imsgbuf ibuf;
373 log_warnx("uid %d: %s", client->euid, err->msg);
374 if (err->code != GOT_ERR_EOF && client->fd != -1) {
375 imsg_init(&ibuf, client->fd);
376 gotd_imsg_send_error(&ibuf, 0, PROC_GOTD, err);
377 imsg_clear(&ibuf);
379 disconnect(client);
382 static const struct got_error *
383 send_repo_info(struct gotd_imsgev *iev, struct gotd_repo *repo)
385 const struct got_error *err = NULL;
386 struct gotd_imsg_info_repo irepo;
388 memset(&irepo, 0, sizeof(irepo));
390 if (strlcpy(irepo.repo_name, repo->name, sizeof(irepo.repo_name))
391 >= sizeof(irepo.repo_name))
392 return got_error_msg(GOT_ERR_NO_SPACE, "repo name too long");
393 if (strlcpy(irepo.repo_path, repo->path, sizeof(irepo.repo_path))
394 >= sizeof(irepo.repo_path))
395 return got_error_msg(GOT_ERR_NO_SPACE, "repo path too long");
397 if (gotd_imsg_compose_event(iev, GOTD_IMSG_INFO_REPO, PROC_GOTD, -1,
398 &irepo, sizeof(irepo)) == -1) {
399 err = got_error_from_errno("imsg compose INFO_REPO");
400 if (err)
401 return err;
404 return NULL;
407 static const struct got_error *
408 send_client_info(struct gotd_imsgev *iev, struct gotd_client *client)
410 const struct got_error *err = NULL;
411 struct gotd_imsg_info_client iclient;
412 struct gotd_child_proc *proc;
414 memset(&iclient, 0, sizeof(iclient));
415 iclient.euid = client->euid;
416 iclient.egid = client->egid;
418 proc = client->repo;
419 if (proc) {
420 if (strlcpy(iclient.repo_name, proc->repo_path,
421 sizeof(iclient.repo_name)) >= sizeof(iclient.repo_name)) {
422 return got_error_msg(GOT_ERR_NO_SPACE,
423 "repo name too long");
425 if (client_is_writing(client))
426 iclient.is_writing = 1;
428 iclient.repo_child_pid = proc->pid;
431 if (client->session)
432 iclient.session_child_pid = client->session->pid;
434 if (gotd_imsg_compose_event(iev, GOTD_IMSG_INFO_CLIENT, PROC_GOTD, -1,
435 &iclient, sizeof(iclient)) == -1) {
436 err = got_error_from_errno("imsg compose INFO_CLIENT");
437 if (err)
438 return err;
441 return NULL;
444 static const struct got_error *
445 send_info(struct gotd_client *client)
447 const struct got_error *err = NULL;
448 struct gotd_imsg_info info;
449 uint64_t slot;
450 struct gotd_repo *repo;
452 if (client->euid != 0)
453 return got_error_set_errno(EPERM, "info");
455 info.pid = gotd.pid;
456 info.verbosity = gotd.verbosity;
457 info.nrepos = gotd.nrepos;
458 info.nclients = client_cnt - 1;
460 if (gotd_imsg_compose_event(&client->iev, GOTD_IMSG_INFO, PROC_GOTD, -1,
461 &info, sizeof(info)) == -1) {
462 err = got_error_from_errno("imsg compose INFO");
463 if (err)
464 return err;
467 TAILQ_FOREACH(repo, &gotd.repos, entry) {
468 err = send_repo_info(&client->iev, repo);
469 if (err)
470 return err;
473 for (slot = 0; slot < nitems(gotd_clients); slot++) {
474 struct gotd_client *c;
475 STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
476 if (c->id == client->id)
477 continue;
478 err = send_client_info(&client->iev, c);
479 if (err)
480 return err;
484 return NULL;
487 static const struct got_error *
488 stop_gotd(struct gotd_client *client)
491 if (client->euid != 0)
492 return got_error_set_errno(EPERM, "stop");
494 gotd_shutdown();
495 /* NOTREACHED */
496 return NULL;
499 static const struct got_error *
500 start_client_authentication(struct gotd_client *client, struct imsg *imsg)
502 const struct got_error *err;
503 struct gotd_imsg_list_refs ireq;
504 struct gotd_repo *repo = NULL;
505 size_t datalen;
507 log_debug("list-refs request from uid %d", client->euid);
509 if (client->state != GOTD_CLIENT_STATE_NEW)
510 return got_error_msg(GOT_ERR_BAD_REQUEST,
511 "unexpected list-refs request received");
513 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
514 if (datalen != sizeof(ireq))
515 return got_error(GOT_ERR_PRIVSEP_LEN);
517 memcpy(&ireq, imsg->data, datalen);
519 if (ireq.client_is_reading) {
520 err = ensure_client_is_not_writing(client);
521 if (err)
522 return err;
523 repo = gotd_find_repo_by_name(ireq.repo_name, &gotd);
524 if (repo == NULL)
525 return got_error(GOT_ERR_NOT_GIT_REPO);
526 err = start_auth_child(client, GOTD_AUTH_READ, repo,
527 gotd.argv0, gotd.confpath, gotd.daemonize,
528 gotd.verbosity);
529 if (err)
530 return err;
531 } else {
532 err = ensure_client_is_not_reading(client);
533 if (err)
534 return err;
535 repo = gotd_find_repo_by_name(ireq.repo_name, &gotd);
536 if (repo == NULL)
537 return got_error(GOT_ERR_NOT_GIT_REPO);
538 err = start_auth_child(client,
539 GOTD_AUTH_READ | GOTD_AUTH_WRITE,
540 repo, gotd.argv0, gotd.confpath, gotd.daemonize,
541 gotd.verbosity);
542 if (err)
543 return err;
546 evtimer_add(&client->tmo, &auth_timeout);
548 /* Flow continues upon authentication successs/failure or timeout. */
549 return NULL;
552 static void
553 gotd_request(int fd, short events, void *arg)
555 struct gotd_imsgev *iev = arg;
556 struct imsgbuf *ibuf = &iev->ibuf;
557 struct gotd_client *client = iev->handler_arg;
558 const struct got_error *err = NULL;
559 struct imsg imsg;
560 ssize_t n;
562 if (events & EV_WRITE) {
563 while (ibuf->w.queued) {
564 n = msgbuf_write(&ibuf->w);
565 if (n == -1 && errno == EPIPE) {
566 /*
567 * The client has closed its socket.
568 * This can happen when Git clients are
569 * done sending pack file data.
570 */
571 msgbuf_clear(&ibuf->w);
572 continue;
573 } else if (n == -1 && errno != EAGAIN) {
574 err = got_error_from_errno("imsg_flush");
575 disconnect_on_error(client, err);
576 return;
578 if (n == 0) {
579 /* Connection closed. */
580 err = got_error(GOT_ERR_EOF);
581 disconnect_on_error(client, err);
582 return;
586 /* Disconnect gotctl(8) now that messages have been sent. */
587 if (!client_is_reading(client) && !client_is_writing(client)) {
588 disconnect(client);
589 return;
593 if ((events & EV_READ) == 0)
594 return;
596 memset(&imsg, 0, sizeof(imsg));
598 while (err == NULL) {
599 err = gotd_imsg_recv(&imsg, ibuf, 0);
600 if (err) {
601 if (err->code == GOT_ERR_PRIVSEP_READ)
602 err = NULL;
603 break;
606 evtimer_del(&client->tmo);
608 switch (imsg.hdr.type) {
609 case GOTD_IMSG_INFO:
610 err = send_info(client);
611 break;
612 case GOTD_IMSG_STOP:
613 err = stop_gotd(client);
614 break;
615 case GOTD_IMSG_LIST_REFS:
616 err = start_client_authentication(client, &imsg);
617 break;
618 default:
619 log_debug("unexpected imsg %d", imsg.hdr.type);
620 err = got_error(GOT_ERR_PRIVSEP_MSG);
621 break;
624 imsg_free(&imsg);
627 if (err) {
628 disconnect_on_error(client, err);
629 } else {
630 gotd_imsg_event_add(&client->iev);
634 static void
635 gotd_auth_timeout(int fd, short events, void *arg)
637 struct gotd_client *client = arg;
639 log_debug("disconnecting uid %d due to authentication timeout",
640 client->euid);
641 disconnect(client);
644 static const struct got_error *
645 recv_connect(uint32_t *client_id, struct imsg *imsg)
647 const struct got_error *err = NULL;
648 struct gotd_imsg_connect iconnect;
649 size_t datalen;
650 int s = -1;
651 struct gotd_client *client = NULL;
653 *client_id = 0;
655 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
656 if (datalen != sizeof(iconnect))
657 return got_error(GOT_ERR_PRIVSEP_LEN);
658 memcpy(&iconnect, imsg->data, sizeof(iconnect));
660 s = imsg->fd;
661 if (s == -1) {
662 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
663 goto done;
666 if (find_client(iconnect.client_id)) {
667 err = got_error_msg(GOT_ERR_CLIENT_ID, "duplicate client ID");
668 goto done;
671 client = calloc(1, sizeof(*client));
672 if (client == NULL) {
673 err = got_error_from_errno("calloc");
674 goto done;
677 *client_id = iconnect.client_id;
679 client->state = GOTD_CLIENT_STATE_NEW;
680 client->id = iconnect.client_id;
681 client->fd = s;
682 s = -1;
683 /* The auth process will verify UID/GID for us. */
684 client->euid = iconnect.euid;
685 client->egid = iconnect.egid;
687 imsg_init(&client->iev.ibuf, client->fd);
688 client->iev.handler = gotd_request;
689 client->iev.events = EV_READ;
690 client->iev.handler_arg = client;
692 event_set(&client->iev.ev, client->fd, EV_READ, gotd_request,
693 &client->iev);
694 gotd_imsg_event_add(&client->iev);
696 evtimer_set(&client->tmo, gotd_auth_timeout, client);
698 add_client(client);
699 log_debug("%s: new client uid %d connected on fd %d", __func__,
700 client->euid, client->fd);
701 done:
702 if (err) {
703 struct gotd_child_proc *listen_proc = &gotd.listen_proc;
704 struct gotd_imsg_disconnect idisconnect;
706 idisconnect.client_id = client->id;
707 if (gotd_imsg_compose_event(&listen_proc->iev,
708 GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
709 &idisconnect, sizeof(idisconnect)) == -1)
710 log_warn("imsg compose DISCONNECT");
712 if (s != -1)
713 close(s);
716 return err;
719 static const char *gotd_proc_names[PROC_MAX] = {
720 "parent",
721 "listen",
722 "auth",
723 "session_read",
724 "session_write",
725 "repo_read",
726 "repo_write"
727 };
729 static void
730 kill_proc(struct gotd_child_proc *proc, int fatal)
732 if (fatal) {
733 log_warnx("sending SIGKILL to PID %d", proc->pid);
734 kill(proc->pid, SIGKILL);
735 } else
736 kill(proc->pid, SIGTERM);
739 static void
740 gotd_shutdown(void)
742 struct gotd_child_proc *proc;
743 uint64_t slot;
745 log_debug("shutting down");
746 for (slot = 0; slot < nitems(gotd_clients); slot++) {
747 struct gotd_client *c, *tmp;
749 STAILQ_FOREACH_SAFE(c, &gotd_clients[slot], entry, tmp)
750 disconnect(c);
753 proc = &gotd.listen_proc;
754 msgbuf_clear(&proc->iev.ibuf.w);
755 close(proc->iev.ibuf.fd);
756 kill_proc(proc, 0);
757 wait_for_child(proc->pid);
759 log_info("terminating");
760 exit(0);
763 void
764 gotd_sighdlr(int sig, short event, void *arg)
766 /*
767 * Normal signal handler rules don't apply because libevent
768 * decouples for us.
769 */
771 switch (sig) {
772 case SIGHUP:
773 log_info("%s: ignoring SIGHUP", __func__);
774 break;
775 case SIGUSR1:
776 log_info("%s: ignoring SIGUSR1", __func__);
777 break;
778 case SIGTERM:
779 case SIGINT:
780 gotd_shutdown();
781 break;
782 default:
783 fatalx("unexpected signal");
787 static const struct got_error *
788 ensure_proc_is_reading(struct gotd_client *client,
789 struct gotd_child_proc *proc)
791 if (!client_is_reading(client)) {
792 kill_proc(proc, 1);
793 return got_error_fmt(GOT_ERR_BAD_PACKET,
794 "PID %d handled a read-request for uid %d but this "
795 "user is not reading from a repository", proc->pid,
796 client->euid);
799 return NULL;
802 static const struct got_error *
803 ensure_proc_is_writing(struct gotd_client *client,
804 struct gotd_child_proc *proc)
806 if (!client_is_writing(client)) {
807 kill_proc(proc, 1);
808 return got_error_fmt(GOT_ERR_BAD_PACKET,
809 "PID %d handled a write-request for uid %d but this "
810 "user is not writing to a repository", proc->pid,
811 client->euid);
814 return NULL;
817 static int
818 verify_imsg_src(struct gotd_client *client, struct gotd_child_proc *proc,
819 struct imsg *imsg)
821 const struct got_error *err;
822 int ret = 0;
824 if (proc->type == PROC_REPO_READ || proc->type == PROC_REPO_WRITE) {
825 if (client->repo == NULL)
826 fatalx("no process found for uid %d", client->euid);
827 if (proc->pid != client->repo->pid) {
828 kill_proc(proc, 1);
829 log_warnx("received message from PID %d for uid %d, "
830 "while PID %d is the process serving this user",
831 proc->pid, client->euid, client->repo->pid);
832 return 0;
835 if (proc->type == PROC_SESSION_READ ||
836 proc->type == PROC_SESSION_WRITE) {
837 if (client->session == NULL) {
838 log_warnx("no session found for uid %d", client->euid);
839 return 0;
841 if (proc->pid != client->session->pid) {
842 kill_proc(proc, 1);
843 log_warnx("received message from PID %d for uid %d, "
844 "while PID %d is the process serving this user",
845 proc->pid, client->euid, client->session->pid);
846 return 0;
850 switch (imsg->hdr.type) {
851 case GOTD_IMSG_ERROR:
852 ret = 1;
853 break;
854 case GOTD_IMSG_CONNECT:
855 if (proc->type != PROC_LISTEN) {
856 err = got_error_fmt(GOT_ERR_BAD_PACKET,
857 "new connection for uid %d from PID %d "
858 "which is not the listen process",
859 proc->pid, client->euid);
860 } else
861 ret = 1;
862 break;
863 case GOTD_IMSG_ACCESS_GRANTED:
864 if (proc->type != PROC_AUTH) {
865 err = got_error_fmt(GOT_ERR_BAD_PACKET,
866 "authentication of uid %d from PID %d "
867 "which is not the auth process",
868 proc->pid, client->euid);
869 } else
870 ret = 1;
871 break;
872 case GOTD_IMSG_CLIENT_SESSION_READY:
873 if (proc->type != PROC_SESSION_READ &&
874 proc->type != PROC_SESSION_WRITE) {
875 err = got_error_fmt(GOT_ERR_BAD_PACKET,
876 "unexpected \"ready\" signal from PID %d",
877 proc->pid);
878 } else
879 ret = 1;
880 break;
881 case GOTD_IMSG_REPO_CHILD_READY:
882 if (proc->type != PROC_REPO_READ &&
883 proc->type != PROC_REPO_WRITE) {
884 err = got_error_fmt(GOT_ERR_BAD_PACKET,
885 "unexpected \"ready\" signal from PID %d",
886 proc->pid);
887 } else
888 ret = 1;
889 break;
890 case GOTD_IMSG_PACKFILE_DONE:
891 err = ensure_proc_is_reading(client, proc);
892 if (err)
893 log_warnx("uid %d: %s", client->euid, err->msg);
894 else
895 ret = 1;
896 break;
897 case GOTD_IMSG_PACKFILE_INSTALL:
898 case GOTD_IMSG_REF_UPDATES_START:
899 case GOTD_IMSG_REF_UPDATE:
900 err = ensure_proc_is_writing(client, proc);
901 if (err)
902 log_warnx("uid %d: %s", client->euid, err->msg);
903 else
904 ret = 1;
905 break;
906 default:
907 log_debug("%s: unexpected imsg %d", __func__, imsg->hdr.type);
908 break;
911 return ret;
914 static const struct got_error *
915 connect_repo_child(struct gotd_client *client,
916 struct gotd_child_proc *repo_proc)
918 static const struct got_error *err;
919 struct gotd_imsgev *session_iev = &client->session->iev;
920 struct gotd_imsg_connect_repo_child ireq;
921 int pipe[2];
923 if (client->state != GOTD_CLIENT_STATE_ACCESS_GRANTED)
924 return got_error_msg(GOT_ERR_BAD_REQUEST,
925 "unexpected repo child ready signal received");
927 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
928 PF_UNSPEC, pipe) == -1)
929 fatal("socketpair");
931 memset(&ireq, 0, sizeof(ireq));
932 ireq.client_id = client->id;
933 ireq.proc_id = repo_proc->type;
935 /* Pass repo child pipe to session child process. */
936 if (gotd_imsg_compose_event(session_iev, GOTD_IMSG_CONNECT_REPO_CHILD,
937 PROC_GOTD, pipe[0], &ireq, sizeof(ireq)) == -1) {
938 err = got_error_from_errno("imsg compose CONNECT_REPO_CHILD");
939 close(pipe[0]);
940 close(pipe[1]);
941 return err;
944 /* Pass session child pipe to repo child process. */
945 if (gotd_imsg_compose_event(&repo_proc->iev,
946 GOTD_IMSG_CONNECT_REPO_CHILD, PROC_GOTD, pipe[1], NULL, 0) == -1) {
947 err = got_error_from_errno("imsg compose CONNECT_REPO_CHILD");
948 close(pipe[1]);
949 return err;
952 return NULL;
955 static void
956 gotd_dispatch_listener(int fd, short event, void *arg)
958 struct gotd_imsgev *iev = arg;
959 struct imsgbuf *ibuf = &iev->ibuf;
960 struct gotd_child_proc *proc = &gotd.listen_proc;
961 ssize_t n;
962 int shut = 0;
963 struct imsg imsg;
965 if (proc->iev.ibuf.fd != fd)
966 fatalx("%s: unexpected fd %d", __func__, fd);
968 if (event & EV_READ) {
969 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
970 fatal("imsg_read error");
971 if (n == 0) {
972 /* Connection closed. */
973 shut = 1;
974 goto done;
978 if (event & EV_WRITE) {
979 n = msgbuf_write(&ibuf->w);
980 if (n == -1 && errno != EAGAIN)
981 fatal("msgbuf_write");
982 if (n == 0) {
983 /* Connection closed. */
984 shut = 1;
985 goto done;
989 for (;;) {
990 const struct got_error *err = NULL;
991 struct gotd_client *client = NULL;
992 uint32_t client_id = 0;
993 int do_disconnect = 0;
995 if ((n = imsg_get(ibuf, &imsg)) == -1)
996 fatal("%s: imsg_get error", __func__);
997 if (n == 0) /* No more messages. */
998 break;
1000 switch (imsg.hdr.type) {
1001 case GOTD_IMSG_ERROR:
1002 do_disconnect = 1;
1003 err = gotd_imsg_recv_error(&client_id, &imsg);
1004 break;
1005 case GOTD_IMSG_CONNECT:
1006 err = recv_connect(&client_id, &imsg);
1007 break;
1008 default:
1009 log_debug("unexpected imsg %d", imsg.hdr.type);
1010 break;
1013 client = find_client(client_id);
1014 if (client == NULL) {
1015 log_warnx("%s: client not found", __func__);
1016 imsg_free(&imsg);
1017 continue;
1020 if (err)
1021 log_warnx("uid %d: %s", client->euid, err->msg);
1023 if (do_disconnect) {
1024 if (err)
1025 disconnect_on_error(client, err);
1026 else
1027 disconnect(client);
1030 imsg_free(&imsg);
1032 done:
1033 if (!shut) {
1034 gotd_imsg_event_add(iev);
1035 } else {
1036 /* This pipe is dead. Remove its event handler */
1037 event_del(&iev->ev);
1038 event_loopexit(NULL);
1042 static void
1043 gotd_dispatch_auth_child(int fd, short event, void *arg)
1045 const struct got_error *err = NULL;
1046 struct gotd_imsgev *iev = arg;
1047 struct imsgbuf *ibuf = &iev->ibuf;
1048 struct gotd_client *client;
1049 struct gotd_repo *repo = NULL;
1050 ssize_t n;
1051 int shut = 0;
1052 struct imsg imsg;
1053 uint32_t client_id = 0;
1054 int do_disconnect = 0;
1056 client = find_client_by_proc_fd(fd);
1057 if (client == NULL) {
1058 /* Can happen during process teardown. */
1059 warnx("cannot find client for fd %d", fd);
1060 shut = 1;
1061 goto done;
1064 if (client->auth == NULL)
1065 fatalx("cannot find auth child process for fd %d", fd);
1067 if (event & EV_READ) {
1068 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1069 fatal("imsg_read error");
1070 if (n == 0) {
1071 /* Connection closed. */
1072 shut = 1;
1073 goto done;
1077 if (event & EV_WRITE) {
1078 n = msgbuf_write(&ibuf->w);
1079 if (n == -1 && errno != EAGAIN)
1080 fatal("msgbuf_write");
1081 if (n == 0) {
1082 /* Connection closed. */
1083 shut = 1;
1085 goto done;
1088 if (client->auth->iev.ibuf.fd != fd)
1089 fatalx("%s: unexpected fd %d", __func__, fd);
1091 if ((n = imsg_get(ibuf, &imsg)) == -1)
1092 fatal("%s: imsg_get error", __func__);
1093 if (n == 0) /* No more messages. */
1094 return;
1096 evtimer_del(&client->tmo);
1098 switch (imsg.hdr.type) {
1099 case GOTD_IMSG_ERROR:
1100 do_disconnect = 1;
1101 err = gotd_imsg_recv_error(&client_id, &imsg);
1102 break;
1103 case GOTD_IMSG_ACCESS_GRANTED:
1104 client->state = GOTD_CLIENT_STATE_ACCESS_GRANTED;
1105 break;
1106 default:
1107 do_disconnect = 1;
1108 log_debug("unexpected imsg %d", imsg.hdr.type);
1109 break;
1112 if (!verify_imsg_src(client, client->auth, &imsg)) {
1113 do_disconnect = 1;
1114 log_debug("dropping imsg type %d from PID %d",
1115 imsg.hdr.type, client->auth->pid);
1117 imsg_free(&imsg);
1119 if (do_disconnect) {
1120 if (err)
1121 disconnect_on_error(client, err);
1122 else
1123 disconnect(client);
1124 return;
1127 repo = gotd_find_repo_by_name(client->auth->repo_name, &gotd);
1128 if (repo == NULL) {
1129 err = got_error(GOT_ERR_NOT_GIT_REPO);
1130 goto done;
1132 kill_auth_proc(client);
1134 log_info("authenticated uid %d for repository %s",
1135 client->euid, repo->name);
1137 err = start_session_child(client, repo, gotd.argv0,
1138 gotd.confpath, gotd.daemonize, gotd.verbosity);
1139 if (err)
1140 goto done;
1141 done:
1142 if (err)
1143 log_warnx("uid %d: %s", client->euid, err->msg);
1145 /* We might have killed the auth process by now. */
1146 if (client->auth != NULL) {
1147 if (!shut) {
1148 gotd_imsg_event_add(iev);
1149 } else {
1150 /* This pipe is dead. Remove its event handler */
1151 event_del(&iev->ev);
1156 static const struct got_error *
1157 connect_session(struct gotd_client *client)
1159 const struct got_error *err = NULL;
1160 struct gotd_imsg_connect iconnect;
1161 int s;
1163 memset(&iconnect, 0, sizeof(iconnect));
1165 s = dup(client->fd);
1166 if (s == -1)
1167 return got_error_from_errno("dup");
1169 iconnect.client_id = client->id;
1170 iconnect.euid = client->euid;
1171 iconnect.egid = client->egid;
1173 if (gotd_imsg_compose_event(&client->session->iev, GOTD_IMSG_CONNECT,
1174 PROC_GOTD, s, &iconnect, sizeof(iconnect)) == -1) {
1175 err = got_error_from_errno("imsg compose CONNECT");
1176 close(s);
1177 return err;
1181 * We are no longer interested in messages from this client.
1182 * Further client requests will be handled by the session process.
1184 msgbuf_clear(&client->iev.ibuf.w);
1185 imsg_clear(&client->iev.ibuf);
1186 event_del(&client->iev.ev);
1187 client->fd = -1; /* will be closed via copy in client->iev.ibuf.fd */
1189 return NULL;
1192 static void
1193 gotd_dispatch_client_session(int fd, short event, void *arg)
1195 struct gotd_imsgev *iev = arg;
1196 struct imsgbuf *ibuf = &iev->ibuf;
1197 struct gotd_child_proc *proc = NULL;
1198 struct gotd_client *client = NULL;
1199 ssize_t n;
1200 int shut = 0;
1201 struct imsg imsg;
1203 client = find_client_by_proc_fd(fd);
1204 if (client == NULL) {
1205 /* Can happen during process teardown. */
1206 warnx("cannot find client for fd %d", fd);
1207 shut = 1;
1208 goto done;
1211 if (event & EV_READ) {
1212 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1213 fatal("imsg_read error");
1214 if (n == 0) {
1215 /* Connection closed. */
1216 shut = 1;
1217 goto done;
1221 if (event & EV_WRITE) {
1222 n = msgbuf_write(&ibuf->w);
1223 if (n == -1 && errno != EAGAIN)
1224 fatal("msgbuf_write");
1225 if (n == 0) {
1226 /* Connection closed. */
1227 shut = 1;
1228 goto done;
1232 proc = client->session;
1233 if (proc == NULL)
1234 fatalx("cannot find session child process for fd %d", fd);
1236 for (;;) {
1237 const struct got_error *err = NULL;
1238 uint32_t client_id = 0;
1239 int do_disconnect = 0, do_start_repo_child = 0;
1241 if ((n = imsg_get(ibuf, &imsg)) == -1)
1242 fatal("%s: imsg_get error", __func__);
1243 if (n == 0) /* No more messages. */
1244 break;
1246 switch (imsg.hdr.type) {
1247 case GOTD_IMSG_ERROR:
1248 do_disconnect = 1;
1249 err = gotd_imsg_recv_error(&client_id, &imsg);
1250 break;
1251 case GOTD_IMSG_CLIENT_SESSION_READY:
1252 if (client->state != GOTD_CLIENT_STATE_ACCESS_GRANTED) {
1253 err = got_error(GOT_ERR_PRIVSEP_MSG);
1254 break;
1256 do_start_repo_child = 1;
1257 break;
1258 case GOTD_IMSG_DISCONNECT:
1259 do_disconnect = 1;
1260 break;
1261 default:
1262 log_debug("unexpected imsg %d", imsg.hdr.type);
1263 break;
1266 if (!verify_imsg_src(client, proc, &imsg)) {
1267 log_debug("dropping imsg type %d from PID %d",
1268 imsg.hdr.type, proc->pid);
1269 imsg_free(&imsg);
1270 continue;
1272 if (err)
1273 log_warnx("uid %d: %s", client->euid, err->msg);
1275 if (do_start_repo_child) {
1276 struct gotd_repo *repo;
1277 const char *name = client->session->repo_name;
1279 repo = gotd_find_repo_by_name(name, &gotd);
1280 if (repo != NULL) {
1281 enum gotd_procid proc_type;
1283 if (client->required_auth & GOTD_AUTH_WRITE)
1284 proc_type = PROC_REPO_WRITE;
1285 else
1286 proc_type = PROC_REPO_READ;
1288 err = start_repo_child(client, proc_type, repo,
1289 gotd.argv0, gotd.confpath, gotd.daemonize,
1290 gotd.verbosity);
1291 } else
1292 err = got_error(GOT_ERR_NOT_GIT_REPO);
1294 if (err) {
1295 log_warnx("uid %d: %s", client->euid, err->msg);
1296 do_disconnect = 1;
1300 if (do_disconnect) {
1301 if (err)
1302 disconnect_on_error(client, err);
1303 else
1304 disconnect(client);
1307 imsg_free(&imsg);
1309 done:
1310 if (!shut) {
1311 gotd_imsg_event_add(iev);
1312 } else {
1313 /* This pipe is dead. Remove its event handler */
1314 event_del(&iev->ev);
1315 disconnect(client);
1319 static void
1320 gotd_dispatch_repo_child(int fd, short event, void *arg)
1322 struct gotd_imsgev *iev = arg;
1323 struct imsgbuf *ibuf = &iev->ibuf;
1324 struct gotd_child_proc *proc = NULL;
1325 struct gotd_client *client;
1326 ssize_t n;
1327 int shut = 0;
1328 struct imsg imsg;
1330 client = find_client_by_proc_fd(fd);
1331 if (client == NULL) {
1332 /* Can happen during process teardown. */
1333 warnx("cannot find client for fd %d", fd);
1334 shut = 1;
1335 goto done;
1338 if (event & EV_READ) {
1339 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1340 fatal("imsg_read error");
1341 if (n == 0) {
1342 /* Connection closed. */
1343 shut = 1;
1344 goto done;
1348 if (event & EV_WRITE) {
1349 n = msgbuf_write(&ibuf->w);
1350 if (n == -1 && errno != EAGAIN)
1351 fatal("msgbuf_write");
1352 if (n == 0) {
1353 /* Connection closed. */
1354 shut = 1;
1355 goto done;
1359 proc = client->repo;
1360 if (proc == NULL)
1361 fatalx("cannot find child process for fd %d", fd);
1363 for (;;) {
1364 const struct got_error *err = NULL;
1365 uint32_t client_id = 0;
1366 int do_disconnect = 0;
1368 if ((n = imsg_get(ibuf, &imsg)) == -1)
1369 fatal("%s: imsg_get error", __func__);
1370 if (n == 0) /* No more messages. */
1371 break;
1373 switch (imsg.hdr.type) {
1374 case GOTD_IMSG_ERROR:
1375 do_disconnect = 1;
1376 err = gotd_imsg_recv_error(&client_id, &imsg);
1377 break;
1378 case GOTD_IMSG_REPO_CHILD_READY:
1379 err = connect_session(client);
1380 if (err)
1381 break;
1382 err = connect_repo_child(client, proc);
1383 break;
1384 default:
1385 log_debug("unexpected imsg %d", imsg.hdr.type);
1386 break;
1389 if (!verify_imsg_src(client, proc, &imsg)) {
1390 log_debug("dropping imsg type %d from PID %d",
1391 imsg.hdr.type, proc->pid);
1392 imsg_free(&imsg);
1393 continue;
1395 if (err)
1396 log_warnx("uid %d: %s", client->euid, err->msg);
1398 if (do_disconnect) {
1399 if (err)
1400 disconnect_on_error(client, err);
1401 else
1402 disconnect(client);
1405 imsg_free(&imsg);
1407 done:
1408 if (!shut) {
1409 gotd_imsg_event_add(iev);
1410 } else {
1411 /* This pipe is dead. Remove its event handler */
1412 event_del(&iev->ev);
1413 disconnect(client);
1417 static pid_t
1418 start_child(enum gotd_procid proc_id, const char *repo_path,
1419 char *argv0, const char *confpath, int fd, int daemonize, int verbosity)
1421 char *argv[11];
1422 int argc = 0;
1423 pid_t pid;
1425 switch (pid = fork()) {
1426 case -1:
1427 fatal("cannot fork");
1428 case 0:
1429 break;
1430 default:
1431 close(fd);
1432 return pid;
1435 if (fd != GOTD_FILENO_MSG_PIPE) {
1436 if (dup2(fd, GOTD_FILENO_MSG_PIPE) == -1)
1437 fatal("cannot setup imsg fd");
1438 } else if (fcntl(fd, F_SETFD, 0) == -1)
1439 fatal("cannot setup imsg fd");
1441 argv[argc++] = argv0;
1442 switch (proc_id) {
1443 case PROC_LISTEN:
1444 argv[argc++] = (char *)"-L";
1445 break;
1446 case PROC_AUTH:
1447 argv[argc++] = (char *)"-A";
1448 break;
1449 case PROC_SESSION_READ:
1450 argv[argc++] = (char *)"-s";
1451 break;
1452 case PROC_SESSION_WRITE:
1453 argv[argc++] = (char *)"-S";
1454 break;
1455 case PROC_REPO_READ:
1456 argv[argc++] = (char *)"-R";
1457 break;
1458 case PROC_REPO_WRITE:
1459 argv[argc++] = (char *)"-W";
1460 break;
1461 default:
1462 fatalx("invalid process id %d", proc_id);
1465 argv[argc++] = (char *)"-f";
1466 argv[argc++] = (char *)confpath;
1468 if (repo_path) {
1469 argv[argc++] = (char *)"-P";
1470 argv[argc++] = (char *)repo_path;
1473 if (!daemonize)
1474 argv[argc++] = (char *)"-d";
1475 if (verbosity > 0)
1476 argv[argc++] = (char *)"-v";
1477 if (verbosity > 1)
1478 argv[argc++] = (char *)"-v";
1479 argv[argc++] = NULL;
1481 execvp(argv0, argv);
1482 fatal("execvp");
1485 static void
1486 start_listener(char *argv0, const char *confpath, int daemonize, int verbosity)
1488 struct gotd_child_proc *proc = &gotd.listen_proc;
1490 proc->type = PROC_LISTEN;
1492 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1493 PF_UNSPEC, proc->pipe) == -1)
1494 fatal("socketpair");
1496 proc->pid = start_child(proc->type, NULL, argv0, confpath,
1497 proc->pipe[1], daemonize, verbosity);
1498 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1499 proc->iev.handler = gotd_dispatch_listener;
1500 proc->iev.events = EV_READ;
1501 proc->iev.handler_arg = NULL;
1504 static const struct got_error *
1505 start_session_child(struct gotd_client *client, struct gotd_repo *repo,
1506 char *argv0, const char *confpath, int daemonize, int verbosity)
1508 struct gotd_child_proc *proc;
1510 proc = calloc(1, sizeof(*proc));
1511 if (proc == NULL)
1512 return got_error_from_errno("calloc");
1514 if (client_is_reading(client))
1515 proc->type = PROC_SESSION_READ;
1516 else
1517 proc->type = PROC_SESSION_WRITE;
1518 if (strlcpy(proc->repo_name, repo->name,
1519 sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
1520 fatalx("repository name too long: %s", repo->name);
1521 log_debug("starting client uid %d session for repository %s",
1522 client->euid, repo->name);
1523 if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >=
1524 sizeof(proc->repo_path))
1525 fatalx("repository path too long: %s", repo->path);
1526 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1527 PF_UNSPEC, proc->pipe) == -1)
1528 fatal("socketpair");
1529 proc->pid = start_child(proc->type, proc->repo_path, argv0,
1530 confpath, proc->pipe[1], daemonize, verbosity);
1531 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1532 log_debug("proc %s %s is on fd %d",
1533 gotd_proc_names[proc->type], proc->repo_path,
1534 proc->pipe[0]);
1535 proc->iev.handler = gotd_dispatch_client_session;
1536 proc->iev.events = EV_READ;
1537 proc->iev.handler_arg = NULL;
1538 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1539 gotd_dispatch_client_session, &proc->iev);
1540 gotd_imsg_event_add(&proc->iev);
1542 client->session = proc;
1543 return NULL;
1546 static const struct got_error *
1547 start_repo_child(struct gotd_client *client, enum gotd_procid proc_type,
1548 struct gotd_repo *repo, char *argv0, const char *confpath,
1549 int daemonize, int verbosity)
1551 struct gotd_child_proc *proc;
1553 if (proc_type != PROC_REPO_READ && proc_type != PROC_REPO_WRITE)
1554 return got_error_msg(GOT_ERR_NOT_IMPL, "bad process type");
1556 proc = calloc(1, sizeof(*proc));
1557 if (proc == NULL)
1558 return got_error_from_errno("calloc");
1560 proc->type = proc_type;
1561 if (strlcpy(proc->repo_name, repo->name,
1562 sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
1563 fatalx("repository name too long: %s", repo->name);
1564 log_debug("starting %s for repository %s",
1565 proc->type == PROC_REPO_READ ? "reader" : "writer", repo->name);
1566 if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >=
1567 sizeof(proc->repo_path))
1568 fatalx("repository path too long: %s", repo->path);
1569 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1570 PF_UNSPEC, proc->pipe) == -1)
1571 fatal("socketpair");
1572 proc->pid = start_child(proc->type, proc->repo_path, argv0,
1573 confpath, proc->pipe[1], daemonize, verbosity);
1574 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1575 log_debug("proc %s %s is on fd %d",
1576 gotd_proc_names[proc->type], proc->repo_path,
1577 proc->pipe[0]);
1578 proc->iev.handler = gotd_dispatch_repo_child;
1579 proc->iev.events = EV_READ;
1580 proc->iev.handler_arg = NULL;
1581 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1582 gotd_dispatch_repo_child, &proc->iev);
1583 gotd_imsg_event_add(&proc->iev);
1585 client->repo = proc;
1586 return NULL;
1589 static const struct got_error *
1590 start_auth_child(struct gotd_client *client, int required_auth,
1591 struct gotd_repo *repo, char *argv0, const char *confpath,
1592 int daemonize, int verbosity)
1594 const struct got_error *err = NULL;
1595 struct gotd_child_proc *proc;
1596 struct gotd_imsg_auth iauth;
1597 int fd;
1599 memset(&iauth, 0, sizeof(iauth));
1601 fd = dup(client->fd);
1602 if (fd == -1)
1603 return got_error_from_errno("dup");
1605 proc = calloc(1, sizeof(*proc));
1606 if (proc == NULL) {
1607 err = got_error_from_errno("calloc");
1608 close(fd);
1609 return err;
1612 proc->type = PROC_AUTH;
1613 if (strlcpy(proc->repo_name, repo->name,
1614 sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
1615 fatalx("repository name too long: %s", repo->name);
1616 log_debug("starting auth for uid %d repository %s",
1617 client->euid, repo->name);
1618 if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >=
1619 sizeof(proc->repo_path))
1620 fatalx("repository path too long: %s", repo->path);
1621 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1622 PF_UNSPEC, proc->pipe) == -1)
1623 fatal("socketpair");
1624 proc->pid = start_child(proc->type, proc->repo_path, argv0,
1625 confpath, proc->pipe[1], daemonize, verbosity);
1626 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1627 log_debug("proc %s %s is on fd %d",
1628 gotd_proc_names[proc->type], proc->repo_path,
1629 proc->pipe[0]);
1630 proc->iev.handler = gotd_dispatch_auth_child;
1631 proc->iev.events = EV_READ;
1632 proc->iev.handler_arg = NULL;
1633 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1634 gotd_dispatch_auth_child, &proc->iev);
1635 gotd_imsg_event_add(&proc->iev);
1637 iauth.euid = client->euid;
1638 iauth.egid = client->egid;
1639 iauth.required_auth = required_auth;
1640 iauth.client_id = client->id;
1641 if (gotd_imsg_compose_event(&proc->iev, GOTD_IMSG_AUTHENTICATE,
1642 PROC_GOTD, fd, &iauth, sizeof(iauth)) == -1) {
1643 log_warn("imsg compose AUTHENTICATE");
1644 close(fd);
1645 /* Let the auth_timeout handler tidy up. */
1648 client->auth = proc;
1649 client->required_auth = required_auth;
1650 return NULL;
1653 static void
1654 apply_unveil_repo_readonly(const char *repo_path, int need_tmpdir)
1656 if (need_tmpdir) {
1657 if (unveil(GOT_TMPDIR_STR, "rwc") == -1)
1658 fatal("unveil %s", GOT_TMPDIR_STR);
1661 if (unveil(repo_path, "r") == -1)
1662 fatal("unveil %s", repo_path);
1664 if (unveil(NULL, NULL) == -1)
1665 fatal("unveil");
1668 static void
1669 apply_unveil_repo_readwrite(const char *repo_path)
1671 if (unveil(repo_path, "rwc") == -1)
1672 fatal("unveil %s", repo_path);
1674 if (unveil(GOT_TMPDIR_STR, "rwc") == -1)
1675 fatal("unveil %s", GOT_TMPDIR_STR);
1677 if (unveil(NULL, NULL) == -1)
1678 fatal("unveil");
1681 static void
1682 apply_unveil_none(void)
1684 if (unveil("/", "") == -1)
1685 fatal("unveil");
1687 if (unveil(NULL, NULL) == -1)
1688 fatal("unveil");
1691 static void
1692 apply_unveil_selfexec(void)
1694 if (unveil(gotd.argv0, "x") == -1)
1695 fatal("unveil %s", gotd.argv0);
1697 if (unveil(NULL, NULL) == -1)
1698 fatal("unveil");
1701 int
1702 main(int argc, char **argv)
1704 const struct got_error *error = NULL;
1705 int ch, fd = -1, daemonize = 1, verbosity = 0, noaction = 0;
1706 const char *confpath = GOTD_CONF_PATH;
1707 char *argv0 = argv[0];
1708 char title[2048];
1709 struct passwd *pw = NULL;
1710 char *repo_path = NULL;
1711 enum gotd_procid proc_id = PROC_GOTD;
1712 struct event evsigint, evsigterm, evsighup, evsigusr1;
1713 int *pack_fds = NULL, *temp_fds = NULL;
1714 struct gotd_repo *repo = NULL;
1716 log_init(1, LOG_DAEMON); /* Log to stderr until daemonized. */
1718 while ((ch = getopt(argc, argv, "Adf:LnP:RsSvW")) != -1) {
1719 switch (ch) {
1720 case 'A':
1721 proc_id = PROC_AUTH;
1722 break;
1723 case 'd':
1724 daemonize = 0;
1725 break;
1726 case 'f':
1727 confpath = optarg;
1728 break;
1729 case 'L':
1730 proc_id = PROC_LISTEN;
1731 break;
1732 case 'n':
1733 noaction = 1;
1734 break;
1735 case 'P':
1736 repo_path = realpath(optarg, NULL);
1737 if (repo_path == NULL)
1738 fatal("realpath '%s'", optarg);
1739 break;
1740 case 'R':
1741 proc_id = PROC_REPO_READ;
1742 break;
1743 case 's':
1744 proc_id = PROC_SESSION_READ;
1745 break;
1746 case 'S':
1747 proc_id = PROC_SESSION_WRITE;
1748 break;
1749 case 'v':
1750 if (verbosity < 3)
1751 verbosity++;
1752 break;
1753 case 'W':
1754 proc_id = PROC_REPO_WRITE;
1755 break;
1756 default:
1757 usage();
1761 argc -= optind;
1762 argv += optind;
1764 if (argc != 0)
1765 usage();
1767 if (geteuid() && (proc_id == PROC_GOTD || proc_id == PROC_LISTEN))
1768 fatalx("need root privileges");
1770 if (parse_config(confpath, proc_id, &gotd, 1) != 0)
1771 return 1;
1773 pw = getpwnam(gotd.user_name);
1774 if (pw == NULL)
1775 fatalx("user %s not found", gotd.user_name);
1777 if (pw->pw_uid == 0)
1778 fatalx("cannot run %s as the superuser", getprogname());
1780 if (noaction) {
1781 fprintf(stderr, "configuration OK\n");
1782 return 0;
1785 gotd.argv0 = argv0;
1786 gotd.daemonize = daemonize;
1787 gotd.verbosity = verbosity;
1788 gotd.confpath = confpath;
1790 /* Require an absolute path in argv[0] for reliable re-exec. */
1791 if (!got_path_is_absolute(argv0))
1792 fatalx("bad path \"%s\": must be an absolute path", argv0);
1794 log_init(daemonize ? 0 : 1, LOG_DAEMON);
1795 log_setverbose(verbosity);
1797 if (proc_id == PROC_GOTD) {
1798 snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
1799 arc4random_buf(&clients_hash_key, sizeof(clients_hash_key));
1800 if (daemonize && daemon(1, 0) == -1)
1801 fatal("daemon");
1802 gotd.pid = getpid();
1803 start_listener(argv0, confpath, daemonize, verbosity);
1804 } else if (proc_id == PROC_LISTEN) {
1805 snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
1806 if (verbosity) {
1807 log_info("socket: %s", gotd.unix_socket_path);
1808 log_info("user: %s", pw->pw_name);
1811 fd = unix_socket_listen(gotd.unix_socket_path, pw->pw_uid,
1812 pw->pw_gid);
1813 if (fd == -1) {
1814 fatal("cannot listen on unix socket %s",
1815 gotd.unix_socket_path);
1817 } else if (proc_id == PROC_AUTH) {
1818 snprintf(title, sizeof(title), "%s %s",
1819 gotd_proc_names[proc_id], repo_path);
1820 } else if (proc_id == PROC_REPO_READ || proc_id == PROC_REPO_WRITE ||
1821 proc_id == PROC_SESSION_READ || proc_id == PROC_SESSION_WRITE) {
1822 error = got_repo_pack_fds_open(&pack_fds);
1823 if (error != NULL)
1824 fatalx("cannot open pack tempfiles: %s", error->msg);
1825 error = got_repo_temp_fds_open(&temp_fds);
1826 if (error != NULL)
1827 fatalx("cannot open pack tempfiles: %s", error->msg);
1828 if (repo_path == NULL)
1829 fatalx("repository path not specified");
1830 snprintf(title, sizeof(title), "%s %s",
1831 gotd_proc_names[proc_id], repo_path);
1832 } else
1833 fatal("invalid process id %d", proc_id);
1835 setproctitle("%s", title);
1836 log_procinit(title);
1838 /* Drop root privileges. */
1839 if (setgid(pw->pw_gid) == -1)
1840 fatal("setgid %d failed", pw->pw_gid);
1841 if (setuid(pw->pw_uid) == -1)
1842 fatal("setuid %d failed", pw->pw_uid);
1844 event_init();
1846 switch (proc_id) {
1847 case PROC_GOTD:
1848 #ifndef PROFILE
1849 /* "exec" promise will be limited to argv[0] via unveil(2). */
1850 if (pledge("stdio proc exec sendfd recvfd unveil", NULL) == -1)
1851 err(1, "pledge");
1852 #endif
1853 break;
1854 case PROC_LISTEN:
1855 #ifndef PROFILE
1856 if (pledge("stdio sendfd unix unveil", NULL) == -1)
1857 err(1, "pledge");
1858 #endif
1860 * Ensure that AF_UNIX bind(2) cannot be used with any other
1861 * sockets by revoking all filesystem access via unveil(2).
1863 apply_unveil_none();
1865 listen_main(title, fd, gotd.connection_limits,
1866 gotd.nconnection_limits);
1867 /* NOTREACHED */
1868 break;
1869 case PROC_AUTH:
1870 #ifndef PROFILE
1871 if (pledge("stdio getpw recvfd unix unveil", NULL) == -1)
1872 err(1, "pledge");
1873 #endif
1875 * We need the "unix" pledge promise for getpeername(2) only.
1876 * Ensure that AF_UNIX bind(2) cannot be used by revoking all
1877 * filesystem access via unveil(2). Access to password database
1878 * files will still work since "getpw" bypasses unveil(2).
1880 apply_unveil_none();
1882 auth_main(title, &gotd.repos, repo_path);
1883 /* NOTREACHED */
1884 break;
1885 case PROC_SESSION_READ:
1886 case PROC_SESSION_WRITE:
1887 #ifndef PROFILE
1889 * The "recvfd" promise is only needed during setup and
1890 * will be removed in a later pledge(2) call.
1892 if (pledge("stdio rpath wpath cpath recvfd sendfd fattr flock "
1893 "unveil", NULL) == -1)
1894 err(1, "pledge");
1895 #endif
1896 if (proc_id == PROC_SESSION_READ)
1897 apply_unveil_repo_readonly(repo_path, 1);
1898 else
1899 apply_unveil_repo_readwrite(repo_path);
1900 session_main(title, repo_path, pack_fds, temp_fds,
1901 &gotd.request_timeout, proc_id);
1902 /* NOTREACHED */
1903 break;
1904 case PROC_REPO_READ:
1905 #ifndef PROFILE
1906 if (pledge("stdio rpath recvfd unveil", NULL) == -1)
1907 err(1, "pledge");
1908 #endif
1909 apply_unveil_repo_readonly(repo_path, 0);
1910 repo_read_main(title, repo_path, pack_fds, temp_fds);
1911 /* NOTREACHED */
1912 exit(0);
1913 case PROC_REPO_WRITE:
1914 #ifndef PROFILE
1915 if (pledge("stdio rpath recvfd unveil", NULL) == -1)
1916 err(1, "pledge");
1917 #endif
1918 apply_unveil_repo_readonly(repo_path, 0);
1919 repo = gotd_find_repo_by_path(repo_path, &gotd);
1920 if (repo == NULL)
1921 fatalx("no repository for path %s", repo_path);
1922 repo_write_main(title, repo_path, pack_fds, temp_fds,
1923 &repo->protected_tag_namespaces,
1924 &repo->protected_branch_namespaces,
1925 &repo->protected_branches);
1926 /* NOTREACHED */
1927 exit(0);
1928 default:
1929 fatal("invalid process id %d", proc_id);
1932 if (proc_id != PROC_GOTD)
1933 fatal("invalid process id %d", proc_id);
1935 apply_unveil_selfexec();
1937 signal_set(&evsigint, SIGINT, gotd_sighdlr, NULL);
1938 signal_set(&evsigterm, SIGTERM, gotd_sighdlr, NULL);
1939 signal_set(&evsighup, SIGHUP, gotd_sighdlr, NULL);
1940 signal_set(&evsigusr1, SIGUSR1, gotd_sighdlr, NULL);
1941 signal(SIGPIPE, SIG_IGN);
1943 signal_add(&evsigint, NULL);
1944 signal_add(&evsigterm, NULL);
1945 signal_add(&evsighup, NULL);
1946 signal_add(&evsigusr1, NULL);
1948 gotd_imsg_event_add(&gotd.listen_proc.iev);
1950 event_dispatch();
1952 free(repo_path);
1953 gotd_shutdown();
1955 return 0;