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>
26 #include <fcntl.h>
27 #include <err.h>
28 #include <errno.h>
29 #include <event.h>
30 #include <limits.h>
31 #include <pwd.h>
32 #include <imsg.h>
33 #include <sha1.h>
34 #include <sha2.h>
35 #include <signal.h>
36 #include <siphash.h>
37 #include <stdarg.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <syslog.h>
42 #include <unistd.h>
44 #include "got_error.h"
45 #include "got_opentemp.h"
46 #include "got_path.h"
47 #include "got_repository.h"
48 #include "got_object.h"
49 #include "got_reference.h"
51 #include "got_lib_delta.h"
52 #include "got_lib_object.h"
53 #include "got_lib_object_cache.h"
54 #include "got_lib_hash.h"
55 #include "got_lib_gitproto.h"
56 #include "got_lib_pack.h"
57 #include "got_lib_repository.h"
59 #include "gotd.h"
60 #include "log.h"
61 #include "listen.h"
62 #include "auth.h"
63 #include "session.h"
64 #include "repo_read.h"
65 #include "repo_write.h"
67 #ifndef nitems
68 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
69 #endif
71 enum gotd_client_state {
72 GOTD_CLIENT_STATE_NEW,
73 GOTD_CLIENT_STATE_ACCESS_GRANTED,
74 };
76 struct gotd_client {
77 STAILQ_ENTRY(gotd_client) entry;
78 enum gotd_client_state state;
79 uint32_t id;
80 int fd;
81 struct gotd_imsgev iev;
82 struct event tmo;
83 uid_t euid;
84 gid_t egid;
85 struct gotd_child_proc *repo;
86 struct gotd_child_proc *auth;
87 struct gotd_child_proc *session;
88 int required_auth;
89 };
90 STAILQ_HEAD(gotd_clients, gotd_client);
92 static struct gotd_clients gotd_clients[GOTD_CLIENT_TABLE_SIZE];
93 static SIPHASH_KEY clients_hash_key;
94 volatile int client_cnt;
95 static struct timeval auth_timeout = { 5, 0 };
96 static struct gotd gotd;
98 void gotd_sighdlr(int sig, short event, void *arg);
99 static void gotd_shutdown(void);
100 static const struct got_error *start_session_child(struct gotd_client *,
101 struct gotd_repo *, char *, const char *, int, int);
102 static const struct got_error *start_repo_child(struct gotd_client *,
103 enum gotd_procid, struct gotd_repo *, char *, const char *, int, int);
104 static const struct got_error *start_auth_child(struct gotd_client *, int,
105 struct gotd_repo *, char *, const char *, int, int);
106 static void kill_proc(struct gotd_child_proc *, int);
108 __dead static void
109 usage(void)
111 fprintf(stderr, "usage: %s [-dnv] [-f config-file]\n", getprogname());
112 exit(1);
115 static int
116 unix_socket_listen(const char *unix_socket_path, uid_t uid, gid_t gid)
118 struct sockaddr_un sun;
119 int fd = -1;
120 mode_t old_umask, mode;
122 fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK| SOCK_CLOEXEC, 0);
123 if (fd == -1) {
124 log_warn("socket");
125 return -1;
128 sun.sun_family = AF_UNIX;
129 if (strlcpy(sun.sun_path, unix_socket_path,
130 sizeof(sun.sun_path)) >= sizeof(sun.sun_path)) {
131 log_warnx("%s: name too long", unix_socket_path);
132 close(fd);
133 return -1;
136 if (unlink(unix_socket_path) == -1) {
137 if (errno != ENOENT) {
138 log_warn("unlink %s", unix_socket_path);
139 close(fd);
140 return -1;
144 old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
145 mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
147 if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
148 log_warn("bind: %s", unix_socket_path);
149 close(fd);
150 umask(old_umask);
151 return -1;
154 umask(old_umask);
156 if (chmod(unix_socket_path, mode) == -1) {
157 log_warn("chmod %o %s", mode, unix_socket_path);
158 close(fd);
159 unlink(unix_socket_path);
160 return -1;
163 if (chown(unix_socket_path, uid, gid) == -1) {
164 log_warn("chown %s uid=%d gid=%d", unix_socket_path, uid, gid);
165 close(fd);
166 unlink(unix_socket_path);
167 return -1;
170 if (listen(fd, GOTD_UNIX_SOCKET_BACKLOG) == -1) {
171 log_warn("listen");
172 close(fd);
173 unlink(unix_socket_path);
174 return -1;
177 return fd;
180 static uint64_t
181 client_hash(uint32_t client_id)
183 return SipHash24(&clients_hash_key, &client_id, sizeof(client_id));
186 static void
187 add_client(struct gotd_client *client)
189 uint64_t slot = client_hash(client->id) % nitems(gotd_clients);
190 STAILQ_INSERT_HEAD(&gotd_clients[slot], client, entry);
191 client_cnt++;
194 static struct gotd_client *
195 find_client(uint32_t client_id)
197 uint64_t slot;
198 struct gotd_client *c;
200 slot = client_hash(client_id) % nitems(gotd_clients);
201 STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
202 if (c->id == client_id)
203 return c;
206 return NULL;
209 static struct gotd_client *
210 find_client_by_proc_fd(int fd)
212 uint64_t slot;
214 for (slot = 0; slot < nitems(gotd_clients); slot++) {
215 struct gotd_client *c;
217 STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
218 if (c->repo && c->repo->iev.ibuf.fd == fd)
219 return c;
220 if (c->auth && c->auth->iev.ibuf.fd == fd)
221 return c;
222 if (c->session && c->session->iev.ibuf.fd == fd)
223 return c;
227 return NULL;
230 static int
231 client_is_reading(struct gotd_client *client)
233 return (client->required_auth &
234 (GOTD_AUTH_READ | GOTD_AUTH_WRITE)) == GOTD_AUTH_READ;
237 static int
238 client_is_writing(struct gotd_client *client)
240 return (client->required_auth &
241 (GOTD_AUTH_READ | GOTD_AUTH_WRITE)) ==
242 (GOTD_AUTH_READ | GOTD_AUTH_WRITE);
245 static const struct got_error *
246 ensure_client_is_not_writing(struct gotd_client *client)
248 if (client_is_writing(client)) {
249 return got_error_fmt(GOT_ERR_BAD_PACKET,
250 "uid %d made a read-request but is writing to "
251 "a repository", client->euid);
254 return NULL;
257 static const struct got_error *
258 ensure_client_is_not_reading(struct gotd_client *client)
260 if (client_is_reading(client)) {
261 return got_error_fmt(GOT_ERR_BAD_PACKET,
262 "uid %d made a write-request but is reading from "
263 "a repository", client->euid);
266 return NULL;
269 static void
270 wait_for_child(pid_t child_pid)
272 pid_t pid;
273 int status;
275 log_debug("waiting for child PID %ld to terminate",
276 (long)child_pid);
278 do {
279 pid = waitpid(child_pid, &status, WNOHANG);
280 if (pid == -1) {
281 if (errno != EINTR && errno != ECHILD)
282 fatal("wait");
283 } else if (WIFSIGNALED(status)) {
284 log_warnx("child PID %ld terminated; signal %d",
285 (long)pid, WTERMSIG(status));
287 } while (pid != -1 || (pid == -1 && errno == EINTR));
290 static void
291 proc_done(struct gotd_child_proc *proc)
293 event_del(&proc->iev.ev);
294 msgbuf_clear(&proc->iev.ibuf.w);
295 close(proc->iev.ibuf.fd);
296 kill_proc(proc, 0);
297 wait_for_child(proc->pid);
298 free(proc);
301 static void
302 kill_auth_proc(struct gotd_client *client)
304 struct gotd_child_proc *proc;
306 if (client->auth == NULL)
307 return;
309 proc = client->auth;
310 client->auth = NULL;
312 proc_done(proc);
315 static void
316 kill_session_proc(struct gotd_client *client)
318 struct gotd_child_proc *proc;
320 if (client->session == NULL)
321 return;
323 proc = client->session;
324 client->session = NULL;
326 proc_done(proc);
329 static void
330 disconnect(struct gotd_client *client)
332 struct gotd_imsg_disconnect idisconnect;
333 struct gotd_child_proc *proc = client->repo;
334 struct gotd_child_proc *listen_proc = &gotd.listen_proc;
335 uint64_t slot;
337 log_debug("uid %d: disconnecting", client->euid);
339 kill_auth_proc(client);
340 kill_session_proc(client);
342 if (proc) {
343 event_del(&proc->iev.ev);
344 msgbuf_clear(&proc->iev.ibuf.w);
345 close(proc->iev.ibuf.fd);
346 kill_proc(proc, 0);
347 wait_for_child(proc->pid);
348 free(proc);
349 proc = NULL;
352 idisconnect.client_id = client->id;
353 if (gotd_imsg_compose_event(&listen_proc->iev,
354 GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
355 &idisconnect, sizeof(idisconnect)) == -1)
356 log_warn("imsg compose DISCONNECT");
358 slot = client_hash(client->id) % nitems(gotd_clients);
359 STAILQ_REMOVE(&gotd_clients[slot], client, gotd_client, entry);
360 imsg_clear(&client->iev.ibuf);
361 event_del(&client->iev.ev);
362 evtimer_del(&client->tmo);
363 if (client->fd != -1)
364 close(client->fd);
365 else if (client->iev.ibuf.fd != -1)
366 close(client->iev.ibuf.fd);
367 free(client);
368 client_cnt--;
371 static void
372 disconnect_on_error(struct gotd_client *client, const struct got_error *err)
374 struct imsgbuf ibuf;
376 log_warnx("uid %d: %s", client->euid, err->msg);
377 if (err->code != GOT_ERR_EOF && client->fd != -1) {
378 imsg_init(&ibuf, client->fd);
379 gotd_imsg_send_error(&ibuf, 0, PROC_GOTD, err);
380 imsg_clear(&ibuf);
382 disconnect(client);
385 static const struct got_error *
386 send_repo_info(struct gotd_imsgev *iev, struct gotd_repo *repo)
388 const struct got_error *err = NULL;
389 struct gotd_imsg_info_repo irepo;
391 memset(&irepo, 0, sizeof(irepo));
393 if (strlcpy(irepo.repo_name, repo->name, sizeof(irepo.repo_name))
394 >= sizeof(irepo.repo_name))
395 return got_error_msg(GOT_ERR_NO_SPACE, "repo name too long");
396 if (strlcpy(irepo.repo_path, repo->path, sizeof(irepo.repo_path))
397 >= sizeof(irepo.repo_path))
398 return got_error_msg(GOT_ERR_NO_SPACE, "repo path too long");
400 if (gotd_imsg_compose_event(iev, GOTD_IMSG_INFO_REPO, PROC_GOTD, -1,
401 &irepo, sizeof(irepo)) == -1) {
402 err = got_error_from_errno("imsg compose INFO_REPO");
403 if (err)
404 return err;
407 return NULL;
410 static const struct got_error *
411 send_client_info(struct gotd_imsgev *iev, struct gotd_client *client)
413 const struct got_error *err = NULL;
414 struct gotd_imsg_info_client iclient;
415 struct gotd_child_proc *proc;
417 memset(&iclient, 0, sizeof(iclient));
418 iclient.euid = client->euid;
419 iclient.egid = client->egid;
421 proc = client->repo;
422 if (proc) {
423 if (strlcpy(iclient.repo_name, proc->repo_path,
424 sizeof(iclient.repo_name)) >= sizeof(iclient.repo_name)) {
425 return got_error_msg(GOT_ERR_NO_SPACE,
426 "repo name too long");
428 if (client_is_writing(client))
429 iclient.is_writing = 1;
431 iclient.repo_child_pid = proc->pid;
434 if (client->session)
435 iclient.session_child_pid = client->session->pid;
437 if (gotd_imsg_compose_event(iev, GOTD_IMSG_INFO_CLIENT, PROC_GOTD, -1,
438 &iclient, sizeof(iclient)) == -1) {
439 err = got_error_from_errno("imsg compose INFO_CLIENT");
440 if (err)
441 return err;
444 return NULL;
447 static const struct got_error *
448 send_info(struct gotd_client *client)
450 const struct got_error *err = NULL;
451 struct gotd_imsg_info info;
452 uint64_t slot;
453 struct gotd_repo *repo;
455 if (client->euid != 0)
456 return got_error_set_errno(EPERM, "info");
458 info.pid = gotd.pid;
459 info.verbosity = gotd.verbosity;
460 info.nrepos = gotd.nrepos;
461 info.nclients = client_cnt - 1;
463 if (gotd_imsg_compose_event(&client->iev, GOTD_IMSG_INFO, PROC_GOTD, -1,
464 &info, sizeof(info)) == -1) {
465 err = got_error_from_errno("imsg compose INFO");
466 if (err)
467 return err;
470 TAILQ_FOREACH(repo, &gotd.repos, entry) {
471 err = send_repo_info(&client->iev, repo);
472 if (err)
473 return err;
476 for (slot = 0; slot < nitems(gotd_clients); slot++) {
477 struct gotd_client *c;
478 STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
479 if (c->id == client->id)
480 continue;
481 err = send_client_info(&client->iev, c);
482 if (err)
483 return err;
487 return NULL;
490 static const struct got_error *
491 stop_gotd(struct gotd_client *client)
494 if (client->euid != 0)
495 return got_error_set_errno(EPERM, "stop");
497 gotd_shutdown();
498 /* NOTREACHED */
499 return NULL;
502 static const struct got_error *
503 start_client_authentication(struct gotd_client *client, struct imsg *imsg)
505 const struct got_error *err;
506 struct gotd_imsg_list_refs ireq;
507 struct gotd_repo *repo = NULL;
508 size_t datalen;
510 log_debug("list-refs request from uid %d", client->euid);
512 if (client->state != GOTD_CLIENT_STATE_NEW)
513 return got_error_msg(GOT_ERR_BAD_REQUEST,
514 "unexpected list-refs request received");
516 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
517 if (datalen != sizeof(ireq))
518 return got_error(GOT_ERR_PRIVSEP_LEN);
520 memcpy(&ireq, imsg->data, datalen);
522 if (ireq.client_is_reading) {
523 err = ensure_client_is_not_writing(client);
524 if (err)
525 return err;
526 repo = gotd_find_repo_by_name(ireq.repo_name, &gotd);
527 if (repo == NULL)
528 return got_error(GOT_ERR_NOT_GIT_REPO);
529 err = start_auth_child(client, GOTD_AUTH_READ, repo,
530 gotd.argv0, gotd.confpath, gotd.daemonize,
531 gotd.verbosity);
532 if (err)
533 return err;
534 } else {
535 err = ensure_client_is_not_reading(client);
536 if (err)
537 return err;
538 repo = gotd_find_repo_by_name(ireq.repo_name, &gotd);
539 if (repo == NULL)
540 return got_error(GOT_ERR_NOT_GIT_REPO);
541 err = start_auth_child(client,
542 GOTD_AUTH_READ | GOTD_AUTH_WRITE,
543 repo, gotd.argv0, gotd.confpath, gotd.daemonize,
544 gotd.verbosity);
545 if (err)
546 return err;
549 evtimer_add(&client->tmo, &auth_timeout);
551 /* Flow continues upon authentication successs/failure or timeout. */
552 return NULL;
555 static void
556 gotd_request(int fd, short events, void *arg)
558 struct gotd_imsgev *iev = arg;
559 struct imsgbuf *ibuf = &iev->ibuf;
560 struct gotd_client *client = iev->handler_arg;
561 const struct got_error *err = NULL;
562 struct imsg imsg;
563 ssize_t n;
565 if (events & EV_WRITE) {
566 while (ibuf->w.queued) {
567 n = msgbuf_write(&ibuf->w);
568 if (n == -1 && errno == EPIPE) {
569 /*
570 * The client has closed its socket.
571 * This can happen when Git clients are
572 * done sending pack file data.
573 */
574 msgbuf_clear(&ibuf->w);
575 continue;
576 } else if (n == -1 && errno != EAGAIN) {
577 err = got_error_from_errno("imsg_flush");
578 disconnect_on_error(client, err);
579 return;
581 if (n == 0) {
582 /* Connection closed. */
583 err = got_error(GOT_ERR_EOF);
584 disconnect_on_error(client, err);
585 return;
589 /* Disconnect gotctl(8) now that messages have been sent. */
590 if (!client_is_reading(client) && !client_is_writing(client)) {
591 disconnect(client);
592 return;
596 if ((events & EV_READ) == 0)
597 return;
599 memset(&imsg, 0, sizeof(imsg));
601 while (err == NULL) {
602 err = gotd_imsg_recv(&imsg, ibuf, 0);
603 if (err) {
604 if (err->code == GOT_ERR_PRIVSEP_READ)
605 err = NULL;
606 break;
609 evtimer_del(&client->tmo);
611 switch (imsg.hdr.type) {
612 case GOTD_IMSG_INFO:
613 err = send_info(client);
614 break;
615 case GOTD_IMSG_STOP:
616 err = stop_gotd(client);
617 break;
618 case GOTD_IMSG_LIST_REFS:
619 err = start_client_authentication(client, &imsg);
620 break;
621 default:
622 log_debug("unexpected imsg %d", imsg.hdr.type);
623 err = got_error(GOT_ERR_PRIVSEP_MSG);
624 break;
627 imsg_free(&imsg);
630 if (err) {
631 disconnect_on_error(client, err);
632 } else {
633 gotd_imsg_event_add(&client->iev);
637 static void
638 gotd_auth_timeout(int fd, short events, void *arg)
640 struct gotd_client *client = arg;
642 log_debug("disconnecting uid %d due to authentication timeout",
643 client->euid);
644 disconnect(client);
647 static const struct got_error *
648 recv_connect(uint32_t *client_id, struct imsg *imsg)
650 const struct got_error *err = NULL;
651 struct gotd_imsg_connect iconnect;
652 size_t datalen;
653 int s = -1;
654 struct gotd_client *client = NULL;
656 *client_id = 0;
658 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
659 if (datalen != sizeof(iconnect))
660 return got_error(GOT_ERR_PRIVSEP_LEN);
661 memcpy(&iconnect, imsg->data, sizeof(iconnect));
663 s = imsg->fd;
664 if (s == -1) {
665 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
666 goto done;
669 if (find_client(iconnect.client_id)) {
670 err = got_error_msg(GOT_ERR_CLIENT_ID, "duplicate client ID");
671 goto done;
674 client = calloc(1, sizeof(*client));
675 if (client == NULL) {
676 err = got_error_from_errno("calloc");
677 goto done;
680 *client_id = iconnect.client_id;
682 client->state = GOTD_CLIENT_STATE_NEW;
683 client->id = iconnect.client_id;
684 client->fd = s;
685 s = -1;
686 /* The auth process will verify UID/GID for us. */
687 client->euid = iconnect.euid;
688 client->egid = iconnect.egid;
690 imsg_init(&client->iev.ibuf, client->fd);
691 client->iev.handler = gotd_request;
692 client->iev.events = EV_READ;
693 client->iev.handler_arg = client;
695 event_set(&client->iev.ev, client->fd, EV_READ, gotd_request,
696 &client->iev);
697 gotd_imsg_event_add(&client->iev);
699 evtimer_set(&client->tmo, gotd_auth_timeout, client);
701 add_client(client);
702 log_debug("%s: new client uid %d connected on fd %d", __func__,
703 client->euid, client->fd);
704 done:
705 if (err) {
706 struct gotd_child_proc *listen_proc = &gotd.listen_proc;
707 struct gotd_imsg_disconnect idisconnect;
709 idisconnect.client_id = client->id;
710 if (gotd_imsg_compose_event(&listen_proc->iev,
711 GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
712 &idisconnect, sizeof(idisconnect)) == -1)
713 log_warn("imsg compose DISCONNECT");
715 if (s != -1)
716 close(s);
719 return err;
722 static const char *gotd_proc_names[PROC_MAX] = {
723 "parent",
724 "listen",
725 "auth",
726 "session_read",
727 "session_write",
728 "repo_read",
729 "repo_write"
730 };
732 static void
733 kill_proc(struct gotd_child_proc *proc, int fatal)
735 if (fatal) {
736 log_warnx("sending SIGKILL to PID %d", proc->pid);
737 kill(proc->pid, SIGKILL);
738 } else
739 kill(proc->pid, SIGTERM);
742 static void
743 gotd_shutdown(void)
745 struct gotd_child_proc *proc;
746 uint64_t slot;
748 log_debug("shutting down");
749 for (slot = 0; slot < nitems(gotd_clients); slot++) {
750 struct gotd_client *c, *tmp;
752 STAILQ_FOREACH_SAFE(c, &gotd_clients[slot], entry, tmp)
753 disconnect(c);
756 proc = &gotd.listen_proc;
757 msgbuf_clear(&proc->iev.ibuf.w);
758 close(proc->iev.ibuf.fd);
759 kill_proc(proc, 0);
760 wait_for_child(proc->pid);
762 log_info("terminating");
763 exit(0);
766 void
767 gotd_sighdlr(int sig, short event, void *arg)
769 /*
770 * Normal signal handler rules don't apply because libevent
771 * decouples for us.
772 */
774 switch (sig) {
775 case SIGHUP:
776 log_info("%s: ignoring SIGHUP", __func__);
777 break;
778 case SIGUSR1:
779 log_info("%s: ignoring SIGUSR1", __func__);
780 break;
781 case SIGTERM:
782 case SIGINT:
783 gotd_shutdown();
784 break;
785 default:
786 fatalx("unexpected signal");
790 static const struct got_error *
791 ensure_proc_is_reading(struct gotd_client *client,
792 struct gotd_child_proc *proc)
794 if (!client_is_reading(client)) {
795 kill_proc(proc, 1);
796 return got_error_fmt(GOT_ERR_BAD_PACKET,
797 "PID %d handled a read-request for uid %d but this "
798 "user is not reading from a repository", proc->pid,
799 client->euid);
802 return NULL;
805 static const struct got_error *
806 ensure_proc_is_writing(struct gotd_client *client,
807 struct gotd_child_proc *proc)
809 if (!client_is_writing(client)) {
810 kill_proc(proc, 1);
811 return got_error_fmt(GOT_ERR_BAD_PACKET,
812 "PID %d handled a write-request for uid %d but this "
813 "user is not writing to a repository", proc->pid,
814 client->euid);
817 return NULL;
820 static int
821 verify_imsg_src(struct gotd_client *client, struct gotd_child_proc *proc,
822 struct imsg *imsg)
824 const struct got_error *err;
825 int ret = 0;
827 if (proc->type == PROC_REPO_READ || proc->type == PROC_REPO_WRITE) {
828 if (client->repo == NULL)
829 fatalx("no process found for uid %d", client->euid);
830 if (proc->pid != client->repo->pid) {
831 kill_proc(proc, 1);
832 log_warnx("received message from PID %d for uid %d, "
833 "while PID %d is the process serving this user",
834 proc->pid, client->euid, client->repo->pid);
835 return 0;
838 if (proc->type == PROC_SESSION_READ ||
839 proc->type == PROC_SESSION_WRITE) {
840 if (client->session == NULL) {
841 log_warnx("no session found for uid %d", client->euid);
842 return 0;
844 if (proc->pid != client->session->pid) {
845 kill_proc(proc, 1);
846 log_warnx("received message from PID %d for uid %d, "
847 "while PID %d is the process serving this user",
848 proc->pid, client->euid, client->session->pid);
849 return 0;
853 switch (imsg->hdr.type) {
854 case GOTD_IMSG_ERROR:
855 ret = 1;
856 break;
857 case GOTD_IMSG_CONNECT:
858 if (proc->type != PROC_LISTEN) {
859 err = got_error_fmt(GOT_ERR_BAD_PACKET,
860 "new connection for uid %d from PID %d "
861 "which is not the listen process",
862 proc->pid, client->euid);
863 } else
864 ret = 1;
865 break;
866 case GOTD_IMSG_ACCESS_GRANTED:
867 if (proc->type != PROC_AUTH) {
868 err = got_error_fmt(GOT_ERR_BAD_PACKET,
869 "authentication of uid %d from PID %d "
870 "which is not the auth process",
871 proc->pid, client->euid);
872 } else
873 ret = 1;
874 break;
875 case GOTD_IMSG_CLIENT_SESSION_READY:
876 if (proc->type != PROC_SESSION_READ &&
877 proc->type != PROC_SESSION_WRITE) {
878 err = got_error_fmt(GOT_ERR_BAD_PACKET,
879 "unexpected \"ready\" signal from PID %d",
880 proc->pid);
881 } else
882 ret = 1;
883 break;
884 case GOTD_IMSG_REPO_CHILD_READY:
885 if (proc->type != PROC_REPO_READ &&
886 proc->type != PROC_REPO_WRITE) {
887 err = got_error_fmt(GOT_ERR_BAD_PACKET,
888 "unexpected \"ready\" signal from PID %d",
889 proc->pid);
890 } else
891 ret = 1;
892 break;
893 case GOTD_IMSG_PACKFILE_DONE:
894 err = ensure_proc_is_reading(client, proc);
895 if (err)
896 log_warnx("uid %d: %s", client->euid, err->msg);
897 else
898 ret = 1;
899 break;
900 case GOTD_IMSG_PACKFILE_INSTALL:
901 case GOTD_IMSG_REF_UPDATES_START:
902 case GOTD_IMSG_REF_UPDATE:
903 err = ensure_proc_is_writing(client, proc);
904 if (err)
905 log_warnx("uid %d: %s", client->euid, err->msg);
906 else
907 ret = 1;
908 break;
909 default:
910 log_debug("%s: unexpected imsg %d", __func__, imsg->hdr.type);
911 break;
914 return ret;
917 static const struct got_error *
918 connect_repo_child(struct gotd_client *client,
919 struct gotd_child_proc *repo_proc)
921 static const struct got_error *err;
922 struct gotd_imsgev *session_iev = &client->session->iev;
923 struct gotd_imsg_connect_repo_child ireq;
924 int pipe[2];
926 if (client->state != GOTD_CLIENT_STATE_ACCESS_GRANTED)
927 return got_error_msg(GOT_ERR_BAD_REQUEST,
928 "unexpected repo child ready signal received");
930 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
931 PF_UNSPEC, pipe) == -1)
932 fatal("socketpair");
934 memset(&ireq, 0, sizeof(ireq));
935 ireq.client_id = client->id;
936 ireq.proc_id = repo_proc->type;
938 /* Pass repo child pipe to session child process. */
939 if (gotd_imsg_compose_event(session_iev, GOTD_IMSG_CONNECT_REPO_CHILD,
940 PROC_GOTD, pipe[0], &ireq, sizeof(ireq)) == -1) {
941 err = got_error_from_errno("imsg compose CONNECT_REPO_CHILD");
942 close(pipe[0]);
943 close(pipe[1]);
944 return err;
947 /* Pass session child pipe to repo child process. */
948 if (gotd_imsg_compose_event(&repo_proc->iev,
949 GOTD_IMSG_CONNECT_REPO_CHILD, PROC_GOTD, pipe[1], NULL, 0) == -1) {
950 err = got_error_from_errno("imsg compose CONNECT_REPO_CHILD");
951 close(pipe[1]);
952 return err;
955 return NULL;
958 static void
959 gotd_dispatch_listener(int fd, short event, void *arg)
961 struct gotd_imsgev *iev = arg;
962 struct imsgbuf *ibuf = &iev->ibuf;
963 struct gotd_child_proc *proc = &gotd.listen_proc;
964 ssize_t n;
965 int shut = 0;
966 struct imsg imsg;
968 if (proc->iev.ibuf.fd != fd)
969 fatalx("%s: unexpected fd %d", __func__, fd);
971 if (event & EV_READ) {
972 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
973 fatal("imsg_read error");
974 if (n == 0) {
975 /* Connection closed. */
976 shut = 1;
977 goto done;
981 if (event & EV_WRITE) {
982 n = msgbuf_write(&ibuf->w);
983 if (n == -1 && errno != EAGAIN)
984 fatal("msgbuf_write");
985 if (n == 0) {
986 /* Connection closed. */
987 shut = 1;
988 goto done;
992 for (;;) {
993 const struct got_error *err = NULL;
994 struct gotd_client *client = NULL;
995 uint32_t client_id = 0;
996 int do_disconnect = 0;
998 if ((n = imsg_get(ibuf, &imsg)) == -1)
999 fatal("%s: imsg_get error", __func__);
1000 if (n == 0) /* No more messages. */
1001 break;
1003 switch (imsg.hdr.type) {
1004 case GOTD_IMSG_ERROR:
1005 do_disconnect = 1;
1006 err = gotd_imsg_recv_error(&client_id, &imsg);
1007 break;
1008 case GOTD_IMSG_CONNECT:
1009 err = recv_connect(&client_id, &imsg);
1010 break;
1011 default:
1012 log_debug("unexpected imsg %d", imsg.hdr.type);
1013 break;
1016 client = find_client(client_id);
1017 if (client == NULL) {
1018 log_warnx("%s: client not found", __func__);
1019 imsg_free(&imsg);
1020 continue;
1023 if (err)
1024 log_warnx("uid %d: %s", client->euid, err->msg);
1026 if (do_disconnect) {
1027 if (err)
1028 disconnect_on_error(client, err);
1029 else
1030 disconnect(client);
1033 imsg_free(&imsg);
1035 done:
1036 if (!shut) {
1037 gotd_imsg_event_add(iev);
1038 } else {
1039 /* This pipe is dead. Remove its event handler */
1040 event_del(&iev->ev);
1041 event_loopexit(NULL);
1045 static void
1046 gotd_dispatch_auth_child(int fd, short event, void *arg)
1048 const struct got_error *err = NULL;
1049 struct gotd_imsgev *iev = arg;
1050 struct imsgbuf *ibuf = &iev->ibuf;
1051 struct gotd_client *client;
1052 struct gotd_repo *repo = NULL;
1053 ssize_t n;
1054 int shut = 0;
1055 struct imsg imsg;
1056 uint32_t client_id = 0;
1057 int do_disconnect = 0;
1059 client = find_client_by_proc_fd(fd);
1060 if (client == NULL) {
1061 /* Can happen during process teardown. */
1062 warnx("cannot find client for fd %d", fd);
1063 shut = 1;
1064 goto done;
1067 if (client->auth == NULL)
1068 fatalx("cannot find auth child process for fd %d", fd);
1070 if (event & EV_READ) {
1071 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1072 fatal("imsg_read error");
1073 if (n == 0) {
1074 /* Connection closed. */
1075 shut = 1;
1076 goto done;
1080 if (event & EV_WRITE) {
1081 n = msgbuf_write(&ibuf->w);
1082 if (n == -1 && errno != EAGAIN)
1083 fatal("msgbuf_write");
1084 if (n == 0) {
1085 /* Connection closed. */
1086 shut = 1;
1088 goto done;
1091 if (client->auth->iev.ibuf.fd != fd)
1092 fatalx("%s: unexpected fd %d", __func__, fd);
1094 if ((n = imsg_get(ibuf, &imsg)) == -1)
1095 fatal("%s: imsg_get error", __func__);
1096 if (n == 0) /* No more messages. */
1097 return;
1099 evtimer_del(&client->tmo);
1101 switch (imsg.hdr.type) {
1102 case GOTD_IMSG_ERROR:
1103 do_disconnect = 1;
1104 err = gotd_imsg_recv_error(&client_id, &imsg);
1105 break;
1106 case GOTD_IMSG_ACCESS_GRANTED:
1107 client->state = GOTD_CLIENT_STATE_ACCESS_GRANTED;
1108 break;
1109 default:
1110 do_disconnect = 1;
1111 log_debug("unexpected imsg %d", imsg.hdr.type);
1112 break;
1115 if (!verify_imsg_src(client, client->auth, &imsg)) {
1116 do_disconnect = 1;
1117 log_debug("dropping imsg type %d from PID %d",
1118 imsg.hdr.type, client->auth->pid);
1120 imsg_free(&imsg);
1122 if (do_disconnect) {
1123 if (err)
1124 disconnect_on_error(client, err);
1125 else
1126 disconnect(client);
1127 return;
1130 repo = gotd_find_repo_by_name(client->auth->repo_name, &gotd);
1131 if (repo == NULL) {
1132 err = got_error(GOT_ERR_NOT_GIT_REPO);
1133 goto done;
1135 kill_auth_proc(client);
1137 log_info("authenticated uid %d for repository %s",
1138 client->euid, repo->name);
1140 err = start_session_child(client, repo, gotd.argv0,
1141 gotd.confpath, gotd.daemonize, gotd.verbosity);
1142 if (err)
1143 goto done;
1144 done:
1145 if (err)
1146 log_warnx("uid %d: %s", client->euid, err->msg);
1148 /* We might have killed the auth process by now. */
1149 if (client->auth != NULL) {
1150 if (!shut) {
1151 gotd_imsg_event_add(iev);
1152 } else {
1153 /* This pipe is dead. Remove its event handler */
1154 event_del(&iev->ev);
1159 static const struct got_error *
1160 connect_session(struct gotd_client *client)
1162 const struct got_error *err = NULL;
1163 struct gotd_imsg_connect iconnect;
1164 int s;
1166 memset(&iconnect, 0, sizeof(iconnect));
1168 s = dup(client->fd);
1169 if (s == -1)
1170 return got_error_from_errno("dup");
1172 iconnect.client_id = client->id;
1173 iconnect.euid = client->euid;
1174 iconnect.egid = client->egid;
1176 if (gotd_imsg_compose_event(&client->session->iev, GOTD_IMSG_CONNECT,
1177 PROC_GOTD, s, &iconnect, sizeof(iconnect)) == -1) {
1178 err = got_error_from_errno("imsg compose CONNECT");
1179 close(s);
1180 return err;
1184 * We are no longer interested in messages from this client.
1185 * Further client requests will be handled by the session process.
1187 msgbuf_clear(&client->iev.ibuf.w);
1188 imsg_clear(&client->iev.ibuf);
1189 event_del(&client->iev.ev);
1190 client->fd = -1; /* will be closed via copy in client->iev.ibuf.fd */
1192 return NULL;
1195 static void
1196 gotd_dispatch_client_session(int fd, short event, void *arg)
1198 struct gotd_imsgev *iev = arg;
1199 struct imsgbuf *ibuf = &iev->ibuf;
1200 struct gotd_child_proc *proc = NULL;
1201 struct gotd_client *client = NULL;
1202 ssize_t n;
1203 int shut = 0;
1204 struct imsg imsg;
1206 client = find_client_by_proc_fd(fd);
1207 if (client == NULL) {
1208 /* Can happen during process teardown. */
1209 warnx("cannot find client for fd %d", fd);
1210 shut = 1;
1211 goto done;
1214 if (event & EV_READ) {
1215 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1216 fatal("imsg_read error");
1217 if (n == 0) {
1218 /* Connection closed. */
1219 shut = 1;
1220 goto done;
1224 if (event & EV_WRITE) {
1225 n = msgbuf_write(&ibuf->w);
1226 if (n == -1 && errno != EAGAIN)
1227 fatal("msgbuf_write");
1228 if (n == 0) {
1229 /* Connection closed. */
1230 shut = 1;
1231 goto done;
1235 proc = client->session;
1236 if (proc == NULL)
1237 fatalx("cannot find session child process for fd %d", fd);
1239 for (;;) {
1240 const struct got_error *err = NULL;
1241 uint32_t client_id = 0;
1242 int do_disconnect = 0, do_start_repo_child = 0;
1244 if ((n = imsg_get(ibuf, &imsg)) == -1)
1245 fatal("%s: imsg_get error", __func__);
1246 if (n == 0) /* No more messages. */
1247 break;
1249 switch (imsg.hdr.type) {
1250 case GOTD_IMSG_ERROR:
1251 do_disconnect = 1;
1252 err = gotd_imsg_recv_error(&client_id, &imsg);
1253 break;
1254 case GOTD_IMSG_CLIENT_SESSION_READY:
1255 if (client->state != GOTD_CLIENT_STATE_ACCESS_GRANTED) {
1256 err = got_error(GOT_ERR_PRIVSEP_MSG);
1257 break;
1259 do_start_repo_child = 1;
1260 break;
1261 case GOTD_IMSG_DISCONNECT:
1262 do_disconnect = 1;
1263 break;
1264 default:
1265 log_debug("unexpected imsg %d", imsg.hdr.type);
1266 break;
1269 if (!verify_imsg_src(client, proc, &imsg)) {
1270 log_debug("dropping imsg type %d from PID %d",
1271 imsg.hdr.type, proc->pid);
1272 imsg_free(&imsg);
1273 continue;
1275 if (err)
1276 log_warnx("uid %d: %s", client->euid, err->msg);
1278 if (do_start_repo_child) {
1279 struct gotd_repo *repo;
1280 const char *name = client->session->repo_name;
1282 repo = gotd_find_repo_by_name(name, &gotd);
1283 if (repo != NULL) {
1284 enum gotd_procid proc_type;
1286 if (client->required_auth & GOTD_AUTH_WRITE)
1287 proc_type = PROC_REPO_WRITE;
1288 else
1289 proc_type = PROC_REPO_READ;
1291 err = start_repo_child(client, proc_type, repo,
1292 gotd.argv0, gotd.confpath, gotd.daemonize,
1293 gotd.verbosity);
1294 } else
1295 err = got_error(GOT_ERR_NOT_GIT_REPO);
1297 if (err) {
1298 log_warnx("uid %d: %s", client->euid, err->msg);
1299 do_disconnect = 1;
1303 if (do_disconnect) {
1304 if (err)
1305 disconnect_on_error(client, err);
1306 else
1307 disconnect(client);
1310 imsg_free(&imsg);
1312 done:
1313 if (!shut) {
1314 gotd_imsg_event_add(iev);
1315 } else {
1316 /* This pipe is dead. Remove its event handler */
1317 event_del(&iev->ev);
1318 disconnect(client);
1322 static void
1323 gotd_dispatch_repo_child(int fd, short event, void *arg)
1325 struct gotd_imsgev *iev = arg;
1326 struct imsgbuf *ibuf = &iev->ibuf;
1327 struct gotd_child_proc *proc = NULL;
1328 struct gotd_client *client;
1329 ssize_t n;
1330 int shut = 0;
1331 struct imsg imsg;
1333 client = find_client_by_proc_fd(fd);
1334 if (client == NULL) {
1335 /* Can happen during process teardown. */
1336 warnx("cannot find client for fd %d", fd);
1337 shut = 1;
1338 goto done;
1341 if (event & EV_READ) {
1342 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1343 fatal("imsg_read error");
1344 if (n == 0) {
1345 /* Connection closed. */
1346 shut = 1;
1347 goto done;
1351 if (event & EV_WRITE) {
1352 n = msgbuf_write(&ibuf->w);
1353 if (n == -1 && errno != EAGAIN)
1354 fatal("msgbuf_write");
1355 if (n == 0) {
1356 /* Connection closed. */
1357 shut = 1;
1358 goto done;
1362 proc = client->repo;
1363 if (proc == NULL)
1364 fatalx("cannot find child process for fd %d", fd);
1366 for (;;) {
1367 const struct got_error *err = NULL;
1368 uint32_t client_id = 0;
1369 int do_disconnect = 0;
1371 if ((n = imsg_get(ibuf, &imsg)) == -1)
1372 fatal("%s: imsg_get error", __func__);
1373 if (n == 0) /* No more messages. */
1374 break;
1376 switch (imsg.hdr.type) {
1377 case GOTD_IMSG_ERROR:
1378 do_disconnect = 1;
1379 err = gotd_imsg_recv_error(&client_id, &imsg);
1380 break;
1381 case GOTD_IMSG_REPO_CHILD_READY:
1382 err = connect_session(client);
1383 if (err)
1384 break;
1385 err = connect_repo_child(client, proc);
1386 break;
1387 default:
1388 log_debug("unexpected imsg %d", imsg.hdr.type);
1389 break;
1392 if (!verify_imsg_src(client, proc, &imsg)) {
1393 log_debug("dropping imsg type %d from PID %d",
1394 imsg.hdr.type, proc->pid);
1395 imsg_free(&imsg);
1396 continue;
1398 if (err)
1399 log_warnx("uid %d: %s", client->euid, err->msg);
1401 if (do_disconnect) {
1402 if (err)
1403 disconnect_on_error(client, err);
1404 else
1405 disconnect(client);
1408 imsg_free(&imsg);
1410 done:
1411 if (!shut) {
1412 gotd_imsg_event_add(iev);
1413 } else {
1414 /* This pipe is dead. Remove its event handler */
1415 event_del(&iev->ev);
1416 disconnect(client);
1420 static pid_t
1421 start_child(enum gotd_procid proc_id, const char *repo_path,
1422 char *argv0, const char *confpath, int fd, int daemonize, int verbosity)
1424 char *argv[11];
1425 int argc = 0;
1426 pid_t pid;
1428 switch (pid = fork()) {
1429 case -1:
1430 fatal("cannot fork");
1431 case 0:
1432 break;
1433 default:
1434 close(fd);
1435 return pid;
1438 if (fd != GOTD_FILENO_MSG_PIPE) {
1439 if (dup2(fd, GOTD_FILENO_MSG_PIPE) == -1)
1440 fatal("cannot setup imsg fd");
1441 } else if (fcntl(fd, F_SETFD, 0) == -1)
1442 fatal("cannot setup imsg fd");
1444 argv[argc++] = argv0;
1445 switch (proc_id) {
1446 case PROC_LISTEN:
1447 argv[argc++] = (char *)"-L";
1448 break;
1449 case PROC_AUTH:
1450 argv[argc++] = (char *)"-A";
1451 break;
1452 case PROC_SESSION_READ:
1453 argv[argc++] = (char *)"-s";
1454 break;
1455 case PROC_SESSION_WRITE:
1456 argv[argc++] = (char *)"-S";
1457 break;
1458 case PROC_REPO_READ:
1459 argv[argc++] = (char *)"-R";
1460 break;
1461 case PROC_REPO_WRITE:
1462 argv[argc++] = (char *)"-W";
1463 break;
1464 default:
1465 fatalx("invalid process id %d", proc_id);
1468 argv[argc++] = (char *)"-f";
1469 argv[argc++] = (char *)confpath;
1471 if (repo_path) {
1472 argv[argc++] = (char *)"-P";
1473 argv[argc++] = (char *)repo_path;
1476 if (!daemonize)
1477 argv[argc++] = (char *)"-d";
1478 if (verbosity > 0)
1479 argv[argc++] = (char *)"-v";
1480 if (verbosity > 1)
1481 argv[argc++] = (char *)"-v";
1482 argv[argc++] = NULL;
1484 execvp(argv0, argv);
1485 fatal("execvp");
1488 static void
1489 start_listener(char *argv0, const char *confpath, int daemonize, int verbosity)
1491 struct gotd_child_proc *proc = &gotd.listen_proc;
1493 proc->type = PROC_LISTEN;
1495 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1496 PF_UNSPEC, proc->pipe) == -1)
1497 fatal("socketpair");
1499 proc->pid = start_child(proc->type, NULL, argv0, confpath,
1500 proc->pipe[1], daemonize, verbosity);
1501 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1502 proc->iev.handler = gotd_dispatch_listener;
1503 proc->iev.events = EV_READ;
1504 proc->iev.handler_arg = NULL;
1507 static const struct got_error *
1508 start_session_child(struct gotd_client *client, struct gotd_repo *repo,
1509 char *argv0, const char *confpath, int daemonize, int verbosity)
1511 struct gotd_child_proc *proc;
1513 proc = calloc(1, sizeof(*proc));
1514 if (proc == NULL)
1515 return got_error_from_errno("calloc");
1517 if (client_is_reading(client))
1518 proc->type = PROC_SESSION_READ;
1519 else
1520 proc->type = PROC_SESSION_WRITE;
1521 if (strlcpy(proc->repo_name, repo->name,
1522 sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
1523 fatalx("repository name too long: %s", repo->name);
1524 log_debug("starting client uid %d session for repository %s",
1525 client->euid, repo->name);
1526 if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >=
1527 sizeof(proc->repo_path))
1528 fatalx("repository path too long: %s", repo->path);
1529 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1530 PF_UNSPEC, proc->pipe) == -1)
1531 fatal("socketpair");
1532 proc->pid = start_child(proc->type, proc->repo_path, argv0,
1533 confpath, proc->pipe[1], daemonize, verbosity);
1534 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1535 log_debug("proc %s %s is on fd %d",
1536 gotd_proc_names[proc->type], proc->repo_path,
1537 proc->pipe[0]);
1538 proc->iev.handler = gotd_dispatch_client_session;
1539 proc->iev.events = EV_READ;
1540 proc->iev.handler_arg = NULL;
1541 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1542 gotd_dispatch_client_session, &proc->iev);
1543 gotd_imsg_event_add(&proc->iev);
1545 client->session = proc;
1546 return NULL;
1549 static const struct got_error *
1550 start_repo_child(struct gotd_client *client, enum gotd_procid proc_type,
1551 struct gotd_repo *repo, char *argv0, const char *confpath,
1552 int daemonize, int verbosity)
1554 struct gotd_child_proc *proc;
1556 if (proc_type != PROC_REPO_READ && proc_type != PROC_REPO_WRITE)
1557 return got_error_msg(GOT_ERR_NOT_IMPL, "bad process type");
1559 proc = calloc(1, sizeof(*proc));
1560 if (proc == NULL)
1561 return got_error_from_errno("calloc");
1563 proc->type = proc_type;
1564 if (strlcpy(proc->repo_name, repo->name,
1565 sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
1566 fatalx("repository name too long: %s", repo->name);
1567 log_debug("starting %s for repository %s",
1568 proc->type == PROC_REPO_READ ? "reader" : "writer", repo->name);
1569 if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >=
1570 sizeof(proc->repo_path))
1571 fatalx("repository path too long: %s", repo->path);
1572 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1573 PF_UNSPEC, proc->pipe) == -1)
1574 fatal("socketpair");
1575 proc->pid = start_child(proc->type, proc->repo_path, argv0,
1576 confpath, proc->pipe[1], daemonize, verbosity);
1577 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1578 log_debug("proc %s %s is on fd %d",
1579 gotd_proc_names[proc->type], proc->repo_path,
1580 proc->pipe[0]);
1581 proc->iev.handler = gotd_dispatch_repo_child;
1582 proc->iev.events = EV_READ;
1583 proc->iev.handler_arg = NULL;
1584 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1585 gotd_dispatch_repo_child, &proc->iev);
1586 gotd_imsg_event_add(&proc->iev);
1588 client->repo = proc;
1589 return NULL;
1592 static const struct got_error *
1593 start_auth_child(struct gotd_client *client, int required_auth,
1594 struct gotd_repo *repo, char *argv0, const char *confpath,
1595 int daemonize, int verbosity)
1597 const struct got_error *err = NULL;
1598 struct gotd_child_proc *proc;
1599 struct gotd_imsg_auth iauth;
1600 int fd;
1602 memset(&iauth, 0, sizeof(iauth));
1604 fd = dup(client->fd);
1605 if (fd == -1)
1606 return got_error_from_errno("dup");
1608 proc = calloc(1, sizeof(*proc));
1609 if (proc == NULL) {
1610 err = got_error_from_errno("calloc");
1611 close(fd);
1612 return err;
1615 proc->type = PROC_AUTH;
1616 if (strlcpy(proc->repo_name, repo->name,
1617 sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
1618 fatalx("repository name too long: %s", repo->name);
1619 log_debug("starting auth for uid %d repository %s",
1620 client->euid, repo->name);
1621 if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >=
1622 sizeof(proc->repo_path))
1623 fatalx("repository path too long: %s", repo->path);
1624 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1625 PF_UNSPEC, proc->pipe) == -1)
1626 fatal("socketpair");
1627 proc->pid = start_child(proc->type, proc->repo_path, argv0,
1628 confpath, proc->pipe[1], daemonize, verbosity);
1629 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1630 log_debug("proc %s %s is on fd %d",
1631 gotd_proc_names[proc->type], proc->repo_path,
1632 proc->pipe[0]);
1633 proc->iev.handler = gotd_dispatch_auth_child;
1634 proc->iev.events = EV_READ;
1635 proc->iev.handler_arg = NULL;
1636 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1637 gotd_dispatch_auth_child, &proc->iev);
1638 gotd_imsg_event_add(&proc->iev);
1640 iauth.euid = client->euid;
1641 iauth.egid = client->egid;
1642 iauth.required_auth = required_auth;
1643 iauth.client_id = client->id;
1644 if (gotd_imsg_compose_event(&proc->iev, GOTD_IMSG_AUTHENTICATE,
1645 PROC_GOTD, fd, &iauth, sizeof(iauth)) == -1) {
1646 log_warn("imsg compose AUTHENTICATE");
1647 close(fd);
1648 /* Let the auth_timeout handler tidy up. */
1651 client->auth = proc;
1652 client->required_auth = required_auth;
1653 return NULL;
1656 static void
1657 apply_unveil_repo_readonly(const char *repo_path, int need_tmpdir)
1659 if (need_tmpdir) {
1660 if (unveil(GOT_TMPDIR_STR, "rwc") == -1)
1661 fatal("unveil %s", GOT_TMPDIR_STR);
1664 if (unveil(repo_path, "r") == -1)
1665 fatal("unveil %s", repo_path);
1667 if (unveil(NULL, NULL) == -1)
1668 fatal("unveil");
1671 static void
1672 apply_unveil_repo_readwrite(const char *repo_path)
1674 if (unveil(repo_path, "rwc") == -1)
1675 fatal("unveil %s", repo_path);
1677 if (unveil(GOT_TMPDIR_STR, "rwc") == -1)
1678 fatal("unveil %s", GOT_TMPDIR_STR);
1680 if (unveil(NULL, NULL) == -1)
1681 fatal("unveil");
1684 static void
1685 apply_unveil_none(void)
1687 if (unveil("/", "") == -1)
1688 fatal("unveil");
1690 if (unveil(NULL, NULL) == -1)
1691 fatal("unveil");
1694 static void
1695 apply_unveil_selfexec(void)
1697 if (unveil(gotd.argv0, "x") == -1)
1698 fatal("unveil %s", gotd.argv0);
1700 if (unveil(NULL, NULL) == -1)
1701 fatal("unveil");
1704 int
1705 main(int argc, char **argv)
1707 const struct got_error *error = NULL;
1708 int ch, fd = -1, daemonize = 1, verbosity = 0, noaction = 0;
1709 const char *confpath = GOTD_CONF_PATH;
1710 char *argv0 = argv[0];
1711 char title[2048];
1712 struct passwd *pw = NULL;
1713 char *repo_path = NULL;
1714 enum gotd_procid proc_id = PROC_GOTD;
1715 struct event evsigint, evsigterm, evsighup, evsigusr1;
1716 int *pack_fds = NULL, *temp_fds = NULL;
1717 struct gotd_repo *repo = NULL;
1719 log_init(1, LOG_DAEMON); /* Log to stderr until daemonized. */
1721 while ((ch = getopt(argc, argv, "Adf:LnP:RsSvW")) != -1) {
1722 switch (ch) {
1723 case 'A':
1724 proc_id = PROC_AUTH;
1725 break;
1726 case 'd':
1727 daemonize = 0;
1728 break;
1729 case 'f':
1730 confpath = optarg;
1731 break;
1732 case 'L':
1733 proc_id = PROC_LISTEN;
1734 break;
1735 case 'n':
1736 noaction = 1;
1737 break;
1738 case 'P':
1739 repo_path = realpath(optarg, NULL);
1740 if (repo_path == NULL)
1741 fatal("realpath '%s'", optarg);
1742 break;
1743 case 'R':
1744 proc_id = PROC_REPO_READ;
1745 break;
1746 case 's':
1747 proc_id = PROC_SESSION_READ;
1748 break;
1749 case 'S':
1750 proc_id = PROC_SESSION_WRITE;
1751 break;
1752 case 'v':
1753 if (verbosity < 3)
1754 verbosity++;
1755 break;
1756 case 'W':
1757 proc_id = PROC_REPO_WRITE;
1758 break;
1759 default:
1760 usage();
1764 argc -= optind;
1765 argv += optind;
1767 if (argc != 0)
1768 usage();
1770 if (geteuid() && (proc_id == PROC_GOTD || proc_id == PROC_LISTEN))
1771 fatalx("need root privileges");
1773 if (parse_config(confpath, proc_id, &gotd, 1) != 0)
1774 return 1;
1776 pw = getpwnam(gotd.user_name);
1777 if (pw == NULL)
1778 fatalx("user %s not found", gotd.user_name);
1780 if (pw->pw_uid == 0)
1781 fatalx("cannot run %s as the superuser", getprogname());
1783 if (noaction) {
1784 fprintf(stderr, "configuration OK\n");
1785 return 0;
1788 gotd.argv0 = argv0;
1789 gotd.daemonize = daemonize;
1790 gotd.verbosity = verbosity;
1791 gotd.confpath = confpath;
1793 /* Require an absolute path in argv[0] for reliable re-exec. */
1794 if (!got_path_is_absolute(argv0))
1795 fatalx("bad path \"%s\": must be an absolute path", argv0);
1797 log_init(daemonize ? 0 : 1, LOG_DAEMON);
1798 log_setverbose(verbosity);
1800 if (proc_id == PROC_GOTD) {
1801 snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
1802 arc4random_buf(&clients_hash_key, sizeof(clients_hash_key));
1803 if (daemonize && daemon(1, 0) == -1)
1804 fatal("daemon");
1805 gotd.pid = getpid();
1806 start_listener(argv0, confpath, daemonize, verbosity);
1807 } else if (proc_id == PROC_LISTEN) {
1808 snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
1809 if (verbosity) {
1810 log_info("socket: %s", gotd.unix_socket_path);
1811 log_info("user: %s", pw->pw_name);
1814 fd = unix_socket_listen(gotd.unix_socket_path, pw->pw_uid,
1815 pw->pw_gid);
1816 if (fd == -1) {
1817 fatal("cannot listen on unix socket %s",
1818 gotd.unix_socket_path);
1820 } else if (proc_id == PROC_AUTH) {
1821 snprintf(title, sizeof(title), "%s %s",
1822 gotd_proc_names[proc_id], repo_path);
1823 } else if (proc_id == PROC_REPO_READ || proc_id == PROC_REPO_WRITE ||
1824 proc_id == PROC_SESSION_READ || proc_id == PROC_SESSION_WRITE) {
1825 error = got_repo_pack_fds_open(&pack_fds);
1826 if (error != NULL)
1827 fatalx("cannot open pack tempfiles: %s", error->msg);
1828 error = got_repo_temp_fds_open(&temp_fds);
1829 if (error != NULL)
1830 fatalx("cannot open pack tempfiles: %s", error->msg);
1831 if (repo_path == NULL)
1832 fatalx("repository path not specified");
1833 snprintf(title, sizeof(title), "%s %s",
1834 gotd_proc_names[proc_id], repo_path);
1835 } else
1836 fatal("invalid process id %d", proc_id);
1838 setproctitle("%s", title);
1839 log_procinit(title);
1841 /* Drop root privileges. */
1842 if (setgid(pw->pw_gid) == -1)
1843 fatal("setgid %d failed", pw->pw_gid);
1844 if (setuid(pw->pw_uid) == -1)
1845 fatal("setuid %d failed", pw->pw_uid);
1847 event_init();
1849 switch (proc_id) {
1850 case PROC_GOTD:
1851 #ifndef PROFILE
1852 /* "exec" promise will be limited to argv[0] via unveil(2). */
1853 if (pledge("stdio proc exec sendfd recvfd unveil", NULL) == -1)
1854 err(1, "pledge");
1855 #endif
1856 break;
1857 case PROC_LISTEN:
1858 #ifndef PROFILE
1859 if (pledge("stdio sendfd unix unveil", NULL) == -1)
1860 err(1, "pledge");
1861 #endif
1863 * Ensure that AF_UNIX bind(2) cannot be used with any other
1864 * sockets by revoking all filesystem access via unveil(2).
1866 apply_unveil_none();
1868 listen_main(title, fd, gotd.connection_limits,
1869 gotd.nconnection_limits);
1870 /* NOTREACHED */
1871 break;
1872 case PROC_AUTH:
1873 #ifndef PROFILE
1874 if (pledge("stdio getpw recvfd unix unveil", NULL) == -1)
1875 err(1, "pledge");
1876 #endif
1878 * We need the "unix" pledge promise for getpeername(2) only.
1879 * Ensure that AF_UNIX bind(2) cannot be used by revoking all
1880 * filesystem access via unveil(2). Access to password database
1881 * files will still work since "getpw" bypasses unveil(2).
1883 apply_unveil_none();
1885 auth_main(title, &gotd.repos, repo_path);
1886 /* NOTREACHED */
1887 break;
1888 case PROC_SESSION_READ:
1889 case PROC_SESSION_WRITE:
1890 #ifndef PROFILE
1892 * The "recvfd" promise is only needed during setup and
1893 * will be removed in a later pledge(2) call.
1895 if (pledge("stdio rpath wpath cpath recvfd sendfd fattr flock "
1896 "unveil", NULL) == -1)
1897 err(1, "pledge");
1898 #endif
1899 if (proc_id == PROC_SESSION_READ)
1900 apply_unveil_repo_readonly(repo_path, 1);
1901 else
1902 apply_unveil_repo_readwrite(repo_path);
1903 session_main(title, repo_path, pack_fds, temp_fds,
1904 &gotd.request_timeout, proc_id);
1905 /* NOTREACHED */
1906 break;
1907 case PROC_REPO_READ:
1908 #ifndef PROFILE
1909 if (pledge("stdio rpath recvfd unveil", NULL) == -1)
1910 err(1, "pledge");
1911 #endif
1912 apply_unveil_repo_readonly(repo_path, 0);
1913 repo_read_main(title, repo_path, pack_fds, temp_fds);
1914 /* NOTREACHED */
1915 exit(0);
1916 case PROC_REPO_WRITE:
1917 #ifndef PROFILE
1918 if (pledge("stdio rpath recvfd unveil", NULL) == -1)
1919 err(1, "pledge");
1920 #endif
1921 apply_unveil_repo_readonly(repo_path, 0);
1922 repo = gotd_find_repo_by_path(repo_path, &gotd);
1923 if (repo == NULL)
1924 fatalx("no repository for path %s", repo_path);
1925 repo_write_main(title, repo_path, pack_fds, temp_fds,
1926 &repo->protected_tag_namespaces,
1927 &repo->protected_branch_namespaces,
1928 &repo->protected_branches);
1929 /* NOTREACHED */
1930 exit(0);
1931 default:
1932 fatal("invalid process id %d", proc_id);
1935 if (proc_id != PROC_GOTD)
1936 fatal("invalid process id %d", proc_id);
1938 apply_unveil_selfexec();
1940 signal_set(&evsigint, SIGINT, gotd_sighdlr, NULL);
1941 signal_set(&evsigterm, SIGTERM, gotd_sighdlr, NULL);
1942 signal_set(&evsighup, SIGHUP, gotd_sighdlr, NULL);
1943 signal_set(&evsigusr1, SIGUSR1, gotd_sighdlr, NULL);
1944 signal(SIGPIPE, SIG_IGN);
1946 signal_add(&evsigint, NULL);
1947 signal_add(&evsigterm, NULL);
1948 signal_add(&evsighup, NULL);
1949 signal_add(&evsigusr1, NULL);
1951 gotd_imsg_event_add(&gotd.listen_proc.iev);
1953 event_dispatch();
1955 free(repo_path);
1956 gotd_shutdown();
1958 return 0;