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 struct gotd_repo *
503 find_repo_by_name(const char *repo_name)
505 struct gotd_repo *repo;
506 size_t namelen;
508 TAILQ_FOREACH(repo, &gotd.repos, entry) {
509 namelen = strlen(repo->name);
510 if (strncmp(repo->name, repo_name, namelen) != 0)
511 continue;
512 if (repo_name[namelen] == '\0' ||
513 strcmp(&repo_name[namelen], ".git") == 0)
514 return repo;
517 return NULL;
520 static const struct got_error *
521 start_client_authentication(struct gotd_client *client, struct imsg *imsg)
523 const struct got_error *err;
524 struct gotd_imsg_list_refs ireq;
525 struct gotd_repo *repo = NULL;
526 size_t datalen;
528 log_debug("list-refs request from uid %d", client->euid);
530 if (client->state != GOTD_CLIENT_STATE_NEW)
531 return got_error_msg(GOT_ERR_BAD_REQUEST,
532 "unexpected list-refs request received");
534 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
535 if (datalen != sizeof(ireq))
536 return got_error(GOT_ERR_PRIVSEP_LEN);
538 memcpy(&ireq, imsg->data, datalen);
540 if (ireq.client_is_reading) {
541 err = ensure_client_is_not_writing(client);
542 if (err)
543 return err;
544 repo = find_repo_by_name(ireq.repo_name);
545 if (repo == NULL)
546 return got_error(GOT_ERR_NOT_GIT_REPO);
547 err = start_auth_child(client, GOTD_AUTH_READ, repo,
548 gotd.argv0, gotd.confpath, gotd.daemonize,
549 gotd.verbosity);
550 if (err)
551 return err;
552 } else {
553 err = ensure_client_is_not_reading(client);
554 if (err)
555 return err;
556 repo = find_repo_by_name(ireq.repo_name);
557 if (repo == NULL)
558 return got_error(GOT_ERR_NOT_GIT_REPO);
559 err = start_auth_child(client,
560 GOTD_AUTH_READ | GOTD_AUTH_WRITE,
561 repo, gotd.argv0, gotd.confpath, gotd.daemonize,
562 gotd.verbosity);
563 if (err)
564 return err;
567 evtimer_add(&client->tmo, &auth_timeout);
569 /* Flow continues upon authentication successs/failure or timeout. */
570 return NULL;
573 static void
574 gotd_request(int fd, short events, void *arg)
576 struct gotd_imsgev *iev = arg;
577 struct imsgbuf *ibuf = &iev->ibuf;
578 struct gotd_client *client = iev->handler_arg;
579 const struct got_error *err = NULL;
580 struct imsg imsg;
581 ssize_t n;
583 if (events & EV_WRITE) {
584 while (ibuf->w.queued) {
585 n = msgbuf_write(&ibuf->w);
586 if (n == -1 && errno == EPIPE) {
587 /*
588 * The client has closed its socket.
589 * This can happen when Git clients are
590 * done sending pack file data.
591 */
592 msgbuf_clear(&ibuf->w);
593 continue;
594 } else if (n == -1 && errno != EAGAIN) {
595 err = got_error_from_errno("imsg_flush");
596 disconnect_on_error(client, err);
597 return;
599 if (n == 0) {
600 /* Connection closed. */
601 err = got_error(GOT_ERR_EOF);
602 disconnect_on_error(client, err);
603 return;
607 /* Disconnect gotctl(8) now that messages have been sent. */
608 if (!client_is_reading(client) && !client_is_writing(client)) {
609 disconnect(client);
610 return;
614 if ((events & EV_READ) == 0)
615 return;
617 memset(&imsg, 0, sizeof(imsg));
619 while (err == NULL) {
620 err = gotd_imsg_recv(&imsg, ibuf, 0);
621 if (err) {
622 if (err->code == GOT_ERR_PRIVSEP_READ)
623 err = NULL;
624 break;
627 evtimer_del(&client->tmo);
629 switch (imsg.hdr.type) {
630 case GOTD_IMSG_INFO:
631 err = send_info(client);
632 break;
633 case GOTD_IMSG_STOP:
634 err = stop_gotd(client);
635 break;
636 case GOTD_IMSG_LIST_REFS:
637 err = start_client_authentication(client, &imsg);
638 break;
639 default:
640 log_debug("unexpected imsg %d", imsg.hdr.type);
641 err = got_error(GOT_ERR_PRIVSEP_MSG);
642 break;
645 imsg_free(&imsg);
648 if (err) {
649 disconnect_on_error(client, err);
650 } else {
651 gotd_imsg_event_add(&client->iev);
655 static void
656 gotd_auth_timeout(int fd, short events, void *arg)
658 struct gotd_client *client = arg;
660 log_debug("disconnecting uid %d due to authentication timeout",
661 client->euid);
662 disconnect(client);
665 static const struct got_error *
666 recv_connect(uint32_t *client_id, struct imsg *imsg)
668 const struct got_error *err = NULL;
669 struct gotd_imsg_connect iconnect;
670 size_t datalen;
671 int s = -1;
672 struct gotd_client *client = NULL;
674 *client_id = 0;
676 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
677 if (datalen != sizeof(iconnect))
678 return got_error(GOT_ERR_PRIVSEP_LEN);
679 memcpy(&iconnect, imsg->data, sizeof(iconnect));
681 s = imsg->fd;
682 if (s == -1) {
683 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
684 goto done;
687 if (find_client(iconnect.client_id)) {
688 err = got_error_msg(GOT_ERR_CLIENT_ID, "duplicate client ID");
689 goto done;
692 client = calloc(1, sizeof(*client));
693 if (client == NULL) {
694 err = got_error_from_errno("calloc");
695 goto done;
698 *client_id = iconnect.client_id;
700 client->state = GOTD_CLIENT_STATE_NEW;
701 client->id = iconnect.client_id;
702 client->fd = s;
703 s = -1;
704 /* The auth process will verify UID/GID for us. */
705 client->euid = iconnect.euid;
706 client->egid = iconnect.egid;
708 imsg_init(&client->iev.ibuf, client->fd);
709 client->iev.handler = gotd_request;
710 client->iev.events = EV_READ;
711 client->iev.handler_arg = client;
713 event_set(&client->iev.ev, client->fd, EV_READ, gotd_request,
714 &client->iev);
715 gotd_imsg_event_add(&client->iev);
717 evtimer_set(&client->tmo, gotd_auth_timeout, client);
719 add_client(client);
720 log_debug("%s: new client uid %d connected on fd %d", __func__,
721 client->euid, client->fd);
722 done:
723 if (err) {
724 struct gotd_child_proc *listen_proc = &gotd.listen_proc;
725 struct gotd_imsg_disconnect idisconnect;
727 idisconnect.client_id = client->id;
728 if (gotd_imsg_compose_event(&listen_proc->iev,
729 GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
730 &idisconnect, sizeof(idisconnect)) == -1)
731 log_warn("imsg compose DISCONNECT");
733 if (s != -1)
734 close(s);
737 return err;
740 static const char *gotd_proc_names[PROC_MAX] = {
741 "parent",
742 "listen",
743 "auth",
744 "session",
745 "repo_read",
746 "repo_write"
747 };
749 static void
750 kill_proc(struct gotd_child_proc *proc, int fatal)
752 if (fatal) {
753 log_warnx("sending SIGKILL to PID %d", proc->pid);
754 kill(proc->pid, SIGKILL);
755 } else
756 kill(proc->pid, SIGTERM);
759 static void
760 gotd_shutdown(void)
762 struct gotd_child_proc *proc;
763 uint64_t slot;
765 log_debug("shutting down");
766 for (slot = 0; slot < nitems(gotd_clients); slot++) {
767 struct gotd_client *c, *tmp;
769 STAILQ_FOREACH_SAFE(c, &gotd_clients[slot], entry, tmp)
770 disconnect(c);
773 proc = &gotd.listen_proc;
774 msgbuf_clear(&proc->iev.ibuf.w);
775 close(proc->iev.ibuf.fd);
776 kill_proc(proc, 0);
777 wait_for_child(proc->pid);
779 log_info("terminating");
780 exit(0);
783 void
784 gotd_sighdlr(int sig, short event, void *arg)
786 /*
787 * Normal signal handler rules don't apply because libevent
788 * decouples for us.
789 */
791 switch (sig) {
792 case SIGHUP:
793 log_info("%s: ignoring SIGHUP", __func__);
794 break;
795 case SIGUSR1:
796 log_info("%s: ignoring SIGUSR1", __func__);
797 break;
798 case SIGTERM:
799 case SIGINT:
800 gotd_shutdown();
801 break;
802 default:
803 fatalx("unexpected signal");
807 static const struct got_error *
808 ensure_proc_is_reading(struct gotd_client *client,
809 struct gotd_child_proc *proc)
811 if (!client_is_reading(client)) {
812 kill_proc(proc, 1);
813 return got_error_fmt(GOT_ERR_BAD_PACKET,
814 "PID %d handled a read-request for uid %d but this "
815 "user is not reading from a repository", proc->pid,
816 client->euid);
819 return NULL;
822 static const struct got_error *
823 ensure_proc_is_writing(struct gotd_client *client,
824 struct gotd_child_proc *proc)
826 if (!client_is_writing(client)) {
827 kill_proc(proc, 1);
828 return got_error_fmt(GOT_ERR_BAD_PACKET,
829 "PID %d handled a write-request for uid %d but this "
830 "user is not writing to a repository", proc->pid,
831 client->euid);
834 return NULL;
837 static int
838 verify_imsg_src(struct gotd_client *client, struct gotd_child_proc *proc,
839 struct imsg *imsg)
841 const struct got_error *err;
842 int ret = 0;
844 if (proc->type == PROC_REPO_READ || proc->type == PROC_REPO_WRITE) {
845 if (client->repo == NULL)
846 fatalx("no process found for uid %d", client->euid);
847 if (proc->pid != client->repo->pid) {
848 kill_proc(proc, 1);
849 log_warnx("received message from PID %d for uid %d, "
850 "while PID %d is the process serving this user",
851 proc->pid, client->euid, client->repo->pid);
852 return 0;
855 if (proc->type == PROC_SESSION) {
856 if (client->session == NULL) {
857 log_warnx("no session found for uid %d", client->euid);
858 return 0;
860 if (proc->pid != client->session->pid) {
861 kill_proc(proc, 1);
862 log_warnx("received message from PID %d for uid %d, "
863 "while PID %d is the process serving this user",
864 proc->pid, client->euid, client->session->pid);
865 return 0;
869 switch (imsg->hdr.type) {
870 case GOTD_IMSG_ERROR:
871 ret = 1;
872 break;
873 case GOTD_IMSG_CONNECT:
874 if (proc->type != PROC_LISTEN) {
875 err = got_error_fmt(GOT_ERR_BAD_PACKET,
876 "new connection for uid %d from PID %d "
877 "which is not the listen process",
878 proc->pid, client->euid);
879 } else
880 ret = 1;
881 break;
882 case GOTD_IMSG_ACCESS_GRANTED:
883 if (proc->type != PROC_AUTH) {
884 err = got_error_fmt(GOT_ERR_BAD_PACKET,
885 "authentication of uid %d from PID %d "
886 "which is not the auth process",
887 proc->pid, client->euid);
888 } else
889 ret = 1;
890 break;
891 case GOTD_IMSG_CLIENT_SESSION_READY:
892 if (proc->type != PROC_SESSION) {
893 err = got_error_fmt(GOT_ERR_BAD_PACKET,
894 "unexpected \"ready\" signal from PID %d",
895 proc->pid);
896 } else
897 ret = 1;
898 break;
899 case GOTD_IMSG_REPO_CHILD_READY:
900 if (proc->type != PROC_REPO_READ &&
901 proc->type != PROC_REPO_WRITE) {
902 err = got_error_fmt(GOT_ERR_BAD_PACKET,
903 "unexpected \"ready\" signal from PID %d",
904 proc->pid);
905 } else
906 ret = 1;
907 break;
908 case GOTD_IMSG_PACKFILE_DONE:
909 err = ensure_proc_is_reading(client, proc);
910 if (err)
911 log_warnx("uid %d: %s", client->euid, err->msg);
912 else
913 ret = 1;
914 break;
915 case GOTD_IMSG_PACKFILE_INSTALL:
916 case GOTD_IMSG_REF_UPDATES_START:
917 case GOTD_IMSG_REF_UPDATE:
918 err = ensure_proc_is_writing(client, proc);
919 if (err)
920 log_warnx("uid %d: %s", client->euid, err->msg);
921 else
922 ret = 1;
923 break;
924 default:
925 log_debug("%s: unexpected imsg %d", __func__, imsg->hdr.type);
926 break;
929 return ret;
932 static const struct got_error *
933 connect_repo_child(struct gotd_client *client,
934 struct gotd_child_proc *repo_proc)
936 static const struct got_error *err;
937 struct gotd_imsgev *session_iev = &client->session->iev;
938 struct gotd_imsg_connect_repo_child ireq;
939 int pipe[2];
941 if (client->state != GOTD_CLIENT_STATE_ACCESS_GRANTED)
942 return got_error_msg(GOT_ERR_BAD_REQUEST,
943 "unexpected repo child ready signal received");
945 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
946 PF_UNSPEC, pipe) == -1)
947 fatal("socketpair");
949 memset(&ireq, 0, sizeof(ireq));
950 ireq.client_id = client->id;
951 ireq.proc_id = repo_proc->type;
953 /* Pass repo child pipe to session child process. */
954 if (gotd_imsg_compose_event(session_iev, GOTD_IMSG_CONNECT_REPO_CHILD,
955 PROC_GOTD, pipe[0], &ireq, sizeof(ireq)) == -1) {
956 err = got_error_from_errno("imsg compose CONNECT_REPO_CHILD");
957 close(pipe[0]);
958 close(pipe[1]);
959 return err;
962 /* Pass session child pipe to repo child process. */
963 if (gotd_imsg_compose_event(&repo_proc->iev,
964 GOTD_IMSG_CONNECT_REPO_CHILD, PROC_GOTD, pipe[1], NULL, 0) == -1) {
965 err = got_error_from_errno("imsg compose CONNECT_REPO_CHILD");
966 close(pipe[1]);
967 return err;
970 return NULL;
973 static void
974 gotd_dispatch_listener(int fd, short event, void *arg)
976 struct gotd_imsgev *iev = arg;
977 struct imsgbuf *ibuf = &iev->ibuf;
978 struct gotd_child_proc *proc = &gotd.listen_proc;
979 ssize_t n;
980 int shut = 0;
981 struct imsg imsg;
983 if (proc->iev.ibuf.fd != fd)
984 fatalx("%s: unexpected fd %d", __func__, fd);
986 if (event & EV_READ) {
987 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
988 fatal("imsg_read error");
989 if (n == 0) {
990 /* Connection closed. */
991 shut = 1;
992 goto done;
996 if (event & EV_WRITE) {
997 n = msgbuf_write(&ibuf->w);
998 if (n == -1 && errno != EAGAIN)
999 fatal("msgbuf_write");
1000 if (n == 0) {
1001 /* Connection closed. */
1002 shut = 1;
1003 goto done;
1007 for (;;) {
1008 const struct got_error *err = NULL;
1009 struct gotd_client *client = NULL;
1010 uint32_t client_id = 0;
1011 int do_disconnect = 0;
1013 if ((n = imsg_get(ibuf, &imsg)) == -1)
1014 fatal("%s: imsg_get error", __func__);
1015 if (n == 0) /* No more messages. */
1016 break;
1018 switch (imsg.hdr.type) {
1019 case GOTD_IMSG_ERROR:
1020 do_disconnect = 1;
1021 err = gotd_imsg_recv_error(&client_id, &imsg);
1022 break;
1023 case GOTD_IMSG_CONNECT:
1024 err = recv_connect(&client_id, &imsg);
1025 break;
1026 default:
1027 log_debug("unexpected imsg %d", imsg.hdr.type);
1028 break;
1031 client = find_client(client_id);
1032 if (client == NULL) {
1033 log_warnx("%s: client not found", __func__);
1034 imsg_free(&imsg);
1035 continue;
1038 if (err)
1039 log_warnx("uid %d: %s", client->euid, err->msg);
1041 if (do_disconnect) {
1042 if (err)
1043 disconnect_on_error(client, err);
1044 else
1045 disconnect(client);
1048 imsg_free(&imsg);
1050 done:
1051 if (!shut) {
1052 gotd_imsg_event_add(iev);
1053 } else {
1054 /* This pipe is dead. Remove its event handler */
1055 event_del(&iev->ev);
1056 event_loopexit(NULL);
1060 static void
1061 gotd_dispatch_auth_child(int fd, short event, void *arg)
1063 const struct got_error *err = NULL;
1064 struct gotd_imsgev *iev = arg;
1065 struct imsgbuf *ibuf = &iev->ibuf;
1066 struct gotd_client *client;
1067 struct gotd_repo *repo = NULL;
1068 ssize_t n;
1069 int shut = 0;
1070 struct imsg imsg;
1071 uint32_t client_id = 0;
1072 int do_disconnect = 0;
1074 client = find_client_by_proc_fd(fd);
1075 if (client == NULL) {
1076 /* Can happen during process teardown. */
1077 warnx("cannot find client for fd %d", fd);
1078 shut = 1;
1079 goto done;
1082 if (client->auth == NULL)
1083 fatalx("cannot find auth child process for fd %d", fd);
1085 if (event & EV_READ) {
1086 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1087 fatal("imsg_read error");
1088 if (n == 0) {
1089 /* Connection closed. */
1090 shut = 1;
1091 goto done;
1095 if (event & EV_WRITE) {
1096 n = msgbuf_write(&ibuf->w);
1097 if (n == -1 && errno != EAGAIN)
1098 fatal("msgbuf_write");
1099 if (n == 0) {
1100 /* Connection closed. */
1101 shut = 1;
1103 goto done;
1106 if (client->auth->iev.ibuf.fd != fd)
1107 fatalx("%s: unexpected fd %d", __func__, fd);
1109 if ((n = imsg_get(ibuf, &imsg)) == -1)
1110 fatal("%s: imsg_get error", __func__);
1111 if (n == 0) /* No more messages. */
1112 return;
1114 evtimer_del(&client->tmo);
1116 switch (imsg.hdr.type) {
1117 case GOTD_IMSG_ERROR:
1118 do_disconnect = 1;
1119 err = gotd_imsg_recv_error(&client_id, &imsg);
1120 break;
1121 case GOTD_IMSG_ACCESS_GRANTED:
1122 client->state = GOTD_CLIENT_STATE_ACCESS_GRANTED;
1123 break;
1124 default:
1125 do_disconnect = 1;
1126 log_debug("unexpected imsg %d", imsg.hdr.type);
1127 break;
1130 if (!verify_imsg_src(client, client->auth, &imsg)) {
1131 do_disconnect = 1;
1132 log_debug("dropping imsg type %d from PID %d",
1133 imsg.hdr.type, client->auth->pid);
1135 imsg_free(&imsg);
1137 if (do_disconnect) {
1138 if (err)
1139 disconnect_on_error(client, err);
1140 else
1141 disconnect(client);
1142 goto done;
1145 repo = find_repo_by_name(client->auth->repo_name);
1146 if (repo == NULL) {
1147 err = got_error(GOT_ERR_NOT_GIT_REPO);
1148 goto done;
1150 kill_auth_proc(client);
1152 log_info("authenticated uid %d for repository %s",
1153 client->euid, repo->name);
1155 err = start_session_child(client, repo, gotd.argv0,
1156 gotd.confpath, gotd.daemonize, gotd.verbosity);
1157 if (err)
1158 goto done;
1159 done:
1160 if (err)
1161 log_warnx("uid %d: %s", client->euid, err->msg);
1163 /* We might have killed the auth process by now. */
1164 if (client->auth != NULL) {
1165 if (!shut) {
1166 gotd_imsg_event_add(iev);
1167 } else {
1168 /* This pipe is dead. Remove its event handler */
1169 event_del(&iev->ev);
1174 static const struct got_error *
1175 connect_session(struct gotd_client *client)
1177 const struct got_error *err = NULL;
1178 struct gotd_imsg_connect iconnect;
1179 int s;
1181 memset(&iconnect, 0, sizeof(iconnect));
1183 s = dup(client->fd);
1184 if (s == -1)
1185 return got_error_from_errno("dup");
1187 iconnect.client_id = client->id;
1188 iconnect.euid = client->euid;
1189 iconnect.egid = client->egid;
1191 if (gotd_imsg_compose_event(&client->session->iev, GOTD_IMSG_CONNECT,
1192 PROC_GOTD, s, &iconnect, sizeof(iconnect)) == -1) {
1193 err = got_error_from_errno("imsg compose CONNECT");
1194 close(s);
1195 return err;
1199 * We are no longer interested in messages from this client.
1200 * Further client requests will be handled by the session process.
1202 msgbuf_clear(&client->iev.ibuf.w);
1203 imsg_clear(&client->iev.ibuf);
1204 event_del(&client->iev.ev);
1205 client->fd = -1; /* will be closed via copy in client->iev.ibuf.fd */
1207 return NULL;
1210 static void
1211 gotd_dispatch_client_session(int fd, short event, void *arg)
1213 struct gotd_imsgev *iev = arg;
1214 struct imsgbuf *ibuf = &iev->ibuf;
1215 struct gotd_child_proc *proc = NULL;
1216 struct gotd_client *client = NULL;
1217 ssize_t n;
1218 int shut = 0;
1219 struct imsg imsg;
1221 client = find_client_by_proc_fd(fd);
1222 if (client == NULL) {
1223 /* Can happen during process teardown. */
1224 warnx("cannot find client for fd %d", fd);
1225 shut = 1;
1226 goto done;
1229 if (event & EV_READ) {
1230 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1231 fatal("imsg_read error");
1232 if (n == 0) {
1233 /* Connection closed. */
1234 shut = 1;
1235 goto done;
1239 if (event & EV_WRITE) {
1240 n = msgbuf_write(&ibuf->w);
1241 if (n == -1 && errno != EAGAIN)
1242 fatal("msgbuf_write");
1243 if (n == 0) {
1244 /* Connection closed. */
1245 shut = 1;
1246 goto done;
1250 proc = client->session;
1251 if (proc == NULL)
1252 fatalx("cannot find session child process for fd %d", fd);
1254 for (;;) {
1255 const struct got_error *err = NULL;
1256 uint32_t client_id = 0;
1257 int do_disconnect = 0, do_start_repo_child = 0;
1259 if ((n = imsg_get(ibuf, &imsg)) == -1)
1260 fatal("%s: imsg_get error", __func__);
1261 if (n == 0) /* No more messages. */
1262 break;
1264 switch (imsg.hdr.type) {
1265 case GOTD_IMSG_ERROR:
1266 do_disconnect = 1;
1267 err = gotd_imsg_recv_error(&client_id, &imsg);
1268 break;
1269 case GOTD_IMSG_CLIENT_SESSION_READY:
1270 if (client->state != GOTD_CLIENT_STATE_ACCESS_GRANTED) {
1271 err = got_error(GOT_ERR_PRIVSEP_MSG);
1272 break;
1274 do_start_repo_child = 1;
1275 break;
1276 case GOTD_IMSG_DISCONNECT:
1277 do_disconnect = 1;
1278 break;
1279 default:
1280 log_debug("unexpected imsg %d", imsg.hdr.type);
1281 break;
1284 if (!verify_imsg_src(client, proc, &imsg)) {
1285 log_debug("dropping imsg type %d from PID %d",
1286 imsg.hdr.type, proc->pid);
1287 imsg_free(&imsg);
1288 continue;
1290 if (err)
1291 log_warnx("uid %d: %s", client->euid, err->msg);
1293 if (do_start_repo_child) {
1294 struct gotd_repo *repo;
1296 repo = find_repo_by_name(client->session->repo_name);
1297 if (repo != NULL) {
1298 enum gotd_procid proc_type;
1300 if (client->required_auth & GOTD_AUTH_WRITE)
1301 proc_type = PROC_REPO_WRITE;
1302 else
1303 proc_type = PROC_REPO_READ;
1305 err = start_repo_child(client, proc_type, repo,
1306 gotd.argv0, gotd.confpath, gotd.daemonize,
1307 gotd.verbosity);
1308 } else
1309 err = got_error(GOT_ERR_NOT_GIT_REPO);
1311 if (err) {
1312 log_warnx("uid %d: %s", client->euid, err->msg);
1313 do_disconnect = 1;
1317 if (do_disconnect) {
1318 if (err)
1319 disconnect_on_error(client, err);
1320 else
1321 disconnect(client);
1324 imsg_free(&imsg);
1326 done:
1327 if (!shut) {
1328 gotd_imsg_event_add(iev);
1329 } else {
1330 /* This pipe is dead. Remove its event handler */
1331 event_del(&iev->ev);
1332 disconnect(client);
1336 static void
1337 gotd_dispatch_repo_child(int fd, short event, void *arg)
1339 struct gotd_imsgev *iev = arg;
1340 struct imsgbuf *ibuf = &iev->ibuf;
1341 struct gotd_child_proc *proc = NULL;
1342 struct gotd_client *client;
1343 ssize_t n;
1344 int shut = 0;
1345 struct imsg imsg;
1347 client = find_client_by_proc_fd(fd);
1348 if (client == NULL) {
1349 /* Can happen during process teardown. */
1350 warnx("cannot find client for fd %d", fd);
1351 shut = 1;
1352 goto done;
1355 if (event & EV_READ) {
1356 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1357 fatal("imsg_read error");
1358 if (n == 0) {
1359 /* Connection closed. */
1360 shut = 1;
1361 goto done;
1365 if (event & EV_WRITE) {
1366 n = msgbuf_write(&ibuf->w);
1367 if (n == -1 && errno != EAGAIN)
1368 fatal("msgbuf_write");
1369 if (n == 0) {
1370 /* Connection closed. */
1371 shut = 1;
1372 goto done;
1376 proc = client->repo;
1377 if (proc == NULL)
1378 fatalx("cannot find child process for fd %d", fd);
1380 for (;;) {
1381 const struct got_error *err = NULL;
1382 uint32_t client_id = 0;
1383 int do_disconnect = 0;
1385 if ((n = imsg_get(ibuf, &imsg)) == -1)
1386 fatal("%s: imsg_get error", __func__);
1387 if (n == 0) /* No more messages. */
1388 break;
1390 switch (imsg.hdr.type) {
1391 case GOTD_IMSG_ERROR:
1392 do_disconnect = 1;
1393 err = gotd_imsg_recv_error(&client_id, &imsg);
1394 break;
1395 case GOTD_IMSG_REPO_CHILD_READY:
1396 err = connect_session(client);
1397 if (err)
1398 break;
1399 err = connect_repo_child(client, proc);
1400 break;
1401 default:
1402 log_debug("unexpected imsg %d", imsg.hdr.type);
1403 break;
1406 if (!verify_imsg_src(client, proc, &imsg)) {
1407 log_debug("dropping imsg type %d from PID %d",
1408 imsg.hdr.type, proc->pid);
1409 imsg_free(&imsg);
1410 continue;
1412 if (err)
1413 log_warnx("uid %d: %s", client->euid, err->msg);
1415 if (do_disconnect) {
1416 if (err)
1417 disconnect_on_error(client, err);
1418 else
1419 disconnect(client);
1422 imsg_free(&imsg);
1424 done:
1425 if (!shut) {
1426 gotd_imsg_event_add(iev);
1427 } else {
1428 /* This pipe is dead. Remove its event handler */
1429 event_del(&iev->ev);
1430 disconnect(client);
1434 static pid_t
1435 start_child(enum gotd_procid proc_id, const char *repo_path,
1436 char *argv0, const char *confpath, int fd, int daemonize, int verbosity)
1438 char *argv[11];
1439 int argc = 0;
1440 pid_t pid;
1442 switch (pid = fork()) {
1443 case -1:
1444 fatal("cannot fork");
1445 case 0:
1446 break;
1447 default:
1448 close(fd);
1449 return pid;
1452 if (fd != GOTD_FILENO_MSG_PIPE) {
1453 if (dup2(fd, GOTD_FILENO_MSG_PIPE) == -1)
1454 fatal("cannot setup imsg fd");
1455 } else if (fcntl(fd, F_SETFD, 0) == -1)
1456 fatal("cannot setup imsg fd");
1458 argv[argc++] = argv0;
1459 switch (proc_id) {
1460 case PROC_LISTEN:
1461 argv[argc++] = (char *)"-L";
1462 break;
1463 case PROC_AUTH:
1464 argv[argc++] = (char *)"-A";
1465 break;
1466 case PROC_SESSION:
1467 argv[argc++] = (char *)"-S";
1468 break;
1469 case PROC_REPO_READ:
1470 argv[argc++] = (char *)"-R";
1471 break;
1472 case PROC_REPO_WRITE:
1473 argv[argc++] = (char *)"-W";
1474 break;
1475 default:
1476 fatalx("invalid process id %d", proc_id);
1479 argv[argc++] = (char *)"-f";
1480 argv[argc++] = (char *)confpath;
1482 if (repo_path) {
1483 argv[argc++] = (char *)"-P";
1484 argv[argc++] = (char *)repo_path;
1487 if (!daemonize)
1488 argv[argc++] = (char *)"-d";
1489 if (verbosity > 0)
1490 argv[argc++] = (char *)"-v";
1491 if (verbosity > 1)
1492 argv[argc++] = (char *)"-v";
1493 argv[argc++] = NULL;
1495 execvp(argv0, argv);
1496 fatal("execvp");
1499 static void
1500 start_listener(char *argv0, const char *confpath, int daemonize, int verbosity)
1502 struct gotd_child_proc *proc = &gotd.listen_proc;
1504 proc->type = PROC_LISTEN;
1506 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1507 PF_UNSPEC, proc->pipe) == -1)
1508 fatal("socketpair");
1510 proc->pid = start_child(proc->type, NULL, argv0, confpath,
1511 proc->pipe[1], daemonize, verbosity);
1512 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1513 proc->iev.handler = gotd_dispatch_listener;
1514 proc->iev.events = EV_READ;
1515 proc->iev.handler_arg = NULL;
1518 static const struct got_error *
1519 start_session_child(struct gotd_client *client, struct gotd_repo *repo,
1520 char *argv0, const char *confpath, int daemonize, int verbosity)
1522 struct gotd_child_proc *proc;
1524 proc = calloc(1, sizeof(*proc));
1525 if (proc == NULL)
1526 return got_error_from_errno("calloc");
1528 proc->type = PROC_SESSION;
1529 if (strlcpy(proc->repo_name, repo->name,
1530 sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
1531 fatalx("repository name too long: %s", repo->name);
1532 log_debug("starting client uid %d session for repository %s",
1533 client->euid, repo->name);
1534 if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >=
1535 sizeof(proc->repo_path))
1536 fatalx("repository path too long: %s", repo->path);
1537 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1538 PF_UNSPEC, proc->pipe) == -1)
1539 fatal("socketpair");
1540 proc->pid = start_child(proc->type, proc->repo_path, argv0,
1541 confpath, proc->pipe[1], daemonize, verbosity);
1542 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1543 log_debug("proc %s %s is on fd %d",
1544 gotd_proc_names[proc->type], proc->repo_path,
1545 proc->pipe[0]);
1546 proc->iev.handler = gotd_dispatch_client_session;
1547 proc->iev.events = EV_READ;
1548 proc->iev.handler_arg = NULL;
1549 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1550 gotd_dispatch_client_session, &proc->iev);
1551 gotd_imsg_event_add(&proc->iev);
1553 client->session = proc;
1554 return NULL;
1557 static const struct got_error *
1558 start_repo_child(struct gotd_client *client, enum gotd_procid proc_type,
1559 struct gotd_repo *repo, char *argv0, const char *confpath,
1560 int daemonize, int verbosity)
1562 struct gotd_child_proc *proc;
1564 if (proc_type != PROC_REPO_READ && proc_type != PROC_REPO_WRITE)
1565 return got_error_msg(GOT_ERR_NOT_IMPL, "bad process type");
1567 proc = calloc(1, sizeof(*proc));
1568 if (proc == NULL)
1569 return got_error_from_errno("calloc");
1571 proc->type = proc_type;
1572 if (strlcpy(proc->repo_name, repo->name,
1573 sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
1574 fatalx("repository name too long: %s", repo->name);
1575 log_debug("starting %s for repository %s",
1576 proc->type == PROC_REPO_READ ? "reader" : "writer", repo->name);
1577 if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >=
1578 sizeof(proc->repo_path))
1579 fatalx("repository path too long: %s", repo->path);
1580 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1581 PF_UNSPEC, proc->pipe) == -1)
1582 fatal("socketpair");
1583 proc->pid = start_child(proc->type, proc->repo_path, argv0,
1584 confpath, proc->pipe[1], daemonize, verbosity);
1585 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1586 log_debug("proc %s %s is on fd %d",
1587 gotd_proc_names[proc->type], proc->repo_path,
1588 proc->pipe[0]);
1589 proc->iev.handler = gotd_dispatch_repo_child;
1590 proc->iev.events = EV_READ;
1591 proc->iev.handler_arg = NULL;
1592 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1593 gotd_dispatch_repo_child, &proc->iev);
1594 gotd_imsg_event_add(&proc->iev);
1596 client->repo = proc;
1597 return NULL;
1600 static const struct got_error *
1601 start_auth_child(struct gotd_client *client, int required_auth,
1602 struct gotd_repo *repo, char *argv0, const char *confpath,
1603 int daemonize, int verbosity)
1605 const struct got_error *err = NULL;
1606 struct gotd_child_proc *proc;
1607 struct gotd_imsg_auth iauth;
1608 int fd;
1610 memset(&iauth, 0, sizeof(iauth));
1612 fd = dup(client->fd);
1613 if (fd == -1)
1614 return got_error_from_errno("dup");
1616 proc = calloc(1, sizeof(*proc));
1617 if (proc == NULL) {
1618 err = got_error_from_errno("calloc");
1619 close(fd);
1620 return err;
1623 proc->type = PROC_AUTH;
1624 if (strlcpy(proc->repo_name, repo->name,
1625 sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
1626 fatalx("repository name too long: %s", repo->name);
1627 log_debug("starting auth for uid %d repository %s",
1628 client->euid, repo->name);
1629 if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >=
1630 sizeof(proc->repo_path))
1631 fatalx("repository path too long: %s", repo->path);
1632 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1633 PF_UNSPEC, proc->pipe) == -1)
1634 fatal("socketpair");
1635 proc->pid = start_child(proc->type, proc->repo_path, argv0,
1636 confpath, proc->pipe[1], daemonize, verbosity);
1637 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1638 log_debug("proc %s %s is on fd %d",
1639 gotd_proc_names[proc->type], proc->repo_path,
1640 proc->pipe[0]);
1641 proc->iev.handler = gotd_dispatch_auth_child;
1642 proc->iev.events = EV_READ;
1643 proc->iev.handler_arg = NULL;
1644 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1645 gotd_dispatch_auth_child, &proc->iev);
1646 gotd_imsg_event_add(&proc->iev);
1648 iauth.euid = client->euid;
1649 iauth.egid = client->egid;
1650 iauth.required_auth = required_auth;
1651 iauth.client_id = client->id;
1652 if (gotd_imsg_compose_event(&proc->iev, GOTD_IMSG_AUTHENTICATE,
1653 PROC_GOTD, fd, &iauth, sizeof(iauth)) == -1) {
1654 log_warn("imsg compose AUTHENTICATE");
1655 close(fd);
1656 /* Let the auth_timeout handler tidy up. */
1659 client->auth = proc;
1660 client->required_auth = required_auth;
1661 return NULL;
1664 static void
1665 apply_unveil_repo_readonly(const char *repo_path)
1667 if (unveil(repo_path, "r") == -1)
1668 fatal("unveil %s", repo_path);
1670 if (unveil(NULL, NULL) == -1)
1671 fatal("unveil");
1674 static void
1675 apply_unveil_repo_readwrite(const char *repo_path)
1677 if (unveil(repo_path, "rwc") == -1)
1678 fatal("unveil %s", repo_path);
1680 if (unveil(GOT_TMPDIR_STR, "rwc") == -1)
1681 fatal("unveil %s", GOT_TMPDIR_STR);
1683 if (unveil(NULL, NULL) == -1)
1684 fatal("unveil");
1687 static void
1688 apply_unveil_none(void)
1690 if (unveil("/", "") == -1)
1691 fatal("unveil");
1693 if (unveil(NULL, NULL) == -1)
1694 fatal("unveil");
1697 static void
1698 apply_unveil_selfexec(void)
1700 if (unveil(gotd.argv0, "x") == -1)
1701 fatal("unveil %s", gotd.argv0);
1703 if (unveil(NULL, NULL) == -1)
1704 fatal("unveil");
1707 int
1708 main(int argc, char **argv)
1710 const struct got_error *error = NULL;
1711 int ch, fd = -1, daemonize = 1, verbosity = 0, noaction = 0;
1712 const char *confpath = GOTD_CONF_PATH;
1713 char *argv0 = argv[0];
1714 char title[2048];
1715 struct passwd *pw = NULL;
1716 char *repo_path = NULL;
1717 enum gotd_procid proc_id = PROC_GOTD;
1718 struct event evsigint, evsigterm, evsighup, evsigusr1;
1719 int *pack_fds = NULL, *temp_fds = NULL;
1721 log_init(1, LOG_DAEMON); /* Log to stderr until daemonized. */
1723 while ((ch = getopt(argc, argv, "Adf:LnP:RSvW")) != -1) {
1724 switch (ch) {
1725 case 'A':
1726 proc_id = PROC_AUTH;
1727 break;
1728 case 'd':
1729 daemonize = 0;
1730 break;
1731 case 'f':
1732 confpath = optarg;
1733 break;
1734 case 'L':
1735 proc_id = PROC_LISTEN;
1736 break;
1737 case 'n':
1738 noaction = 1;
1739 break;
1740 case 'P':
1741 repo_path = realpath(optarg, NULL);
1742 if (repo_path == NULL)
1743 fatal("realpath '%s'", optarg);
1744 break;
1745 case 'R':
1746 proc_id = PROC_REPO_READ;
1747 break;
1748 case 'S':
1749 proc_id = PROC_SESSION;
1750 break;
1751 case 'v':
1752 if (verbosity < 3)
1753 verbosity++;
1754 break;
1755 case 'W':
1756 proc_id = PROC_REPO_WRITE;
1757 break;
1758 default:
1759 usage();
1763 argc -= optind;
1764 argv += optind;
1766 if (argc != 0)
1767 usage();
1769 if (geteuid() && (proc_id == PROC_GOTD || proc_id == PROC_LISTEN))
1770 fatalx("need root privileges");
1772 if (parse_config(confpath, proc_id, &gotd) != 0)
1773 return 1;
1775 pw = getpwnam(gotd.user_name);
1776 if (pw == NULL)
1777 fatalx("user %s not found", gotd.user_name);
1779 if (pw->pw_uid == 0)
1780 fatalx("cannot run %s as the superuser", getprogname());
1782 if (noaction) {
1783 fprintf(stderr, "configuration OK\n");
1784 return 0;
1787 gotd.argv0 = argv0;
1788 gotd.daemonize = daemonize;
1789 gotd.verbosity = verbosity;
1790 gotd.confpath = confpath;
1792 /* Require an absolute path in argv[0] for reliable re-exec. */
1793 if (!got_path_is_absolute(argv0))
1794 fatalx("bad path \"%s\": must be an absolute path", argv0);
1796 log_init(daemonize ? 0 : 1, LOG_DAEMON);
1797 log_setverbose(verbosity);
1799 if (proc_id == PROC_GOTD) {
1800 snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
1801 arc4random_buf(&clients_hash_key, sizeof(clients_hash_key));
1802 if (daemonize && daemon(1, 0) == -1)
1803 fatal("daemon");
1804 gotd.pid = getpid();
1805 start_listener(argv0, confpath, daemonize, verbosity);
1806 } else if (proc_id == PROC_LISTEN) {
1807 snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
1808 if (verbosity) {
1809 log_info("socket: %s", gotd.unix_socket_path);
1810 log_info("user: %s", pw->pw_name);
1813 fd = unix_socket_listen(gotd.unix_socket_path, pw->pw_uid,
1814 pw->pw_gid);
1815 if (fd == -1) {
1816 fatal("cannot listen on unix socket %s",
1817 gotd.unix_socket_path);
1819 } else if (proc_id == PROC_AUTH) {
1820 snprintf(title, sizeof(title), "%s %s",
1821 gotd_proc_names[proc_id], repo_path);
1822 } else if (proc_id == PROC_REPO_READ || proc_id == PROC_REPO_WRITE ||
1823 proc_id == PROC_SESSION) {
1824 error = got_repo_pack_fds_open(&pack_fds);
1825 if (error != NULL)
1826 fatalx("cannot open pack tempfiles: %s", error->msg);
1827 error = got_repo_temp_fds_open(&temp_fds);
1828 if (error != NULL)
1829 fatalx("cannot open pack tempfiles: %s", error->msg);
1830 if (repo_path == NULL)
1831 fatalx("repository path not specified");
1832 snprintf(title, sizeof(title), "%s %s",
1833 gotd_proc_names[proc_id], repo_path);
1834 } else
1835 fatal("invalid process id %d", proc_id);
1837 setproctitle("%s", title);
1838 log_procinit(title);
1840 /* Drop root privileges. */
1841 if (setgid(pw->pw_gid) == -1)
1842 fatal("setgid %d failed", pw->pw_gid);
1843 if (setuid(pw->pw_uid) == -1)
1844 fatal("setuid %d failed", pw->pw_uid);
1846 event_init();
1848 switch (proc_id) {
1849 case PROC_GOTD:
1850 #ifndef PROFILE
1851 /* "exec" promise will be limited to argv[0] via unveil(2). */
1852 if (pledge("stdio proc exec sendfd recvfd unveil", NULL) == -1)
1853 err(1, "pledge");
1854 #endif
1855 break;
1856 case PROC_LISTEN:
1857 #ifndef PROFILE
1858 if (pledge("stdio sendfd unix unveil", NULL) == -1)
1859 err(1, "pledge");
1860 #endif
1862 * Ensure that AF_UNIX bind(2) cannot be used with any other
1863 * sockets by revoking all filesystem access via unveil(2).
1865 apply_unveil_none();
1867 listen_main(title, fd, gotd.connection_limits,
1868 gotd.nconnection_limits);
1869 /* NOTREACHED */
1870 break;
1871 case PROC_AUTH:
1872 #ifndef PROFILE
1873 if (pledge("stdio getpw recvfd unix unveil", NULL) == -1)
1874 err(1, "pledge");
1875 #endif
1877 * We need the "unix" pledge promise for getpeername(2) only.
1878 * Ensure that AF_UNIX bind(2) cannot be used by revoking all
1879 * filesystem access via unveil(2). Access to password database
1880 * files will still work since "getpw" bypasses unveil(2).
1882 apply_unveil_none();
1884 auth_main(title, &gotd.repos, repo_path);
1885 /* NOTREACHED */
1886 break;
1887 case PROC_SESSION:
1888 #ifndef PROFILE
1890 * The "recvfd" promise is only needed during setup and
1891 * will be removed in a later pledge(2) call.
1893 if (pledge("stdio rpath wpath cpath recvfd sendfd fattr flock "
1894 "unveil", NULL) == -1)
1895 err(1, "pledge");
1896 #endif
1897 apply_unveil_repo_readwrite(repo_path);
1898 session_main(title, repo_path, pack_fds, temp_fds,
1899 &gotd.request_timeout);
1900 /* NOTREACHED */
1901 break;
1902 case PROC_REPO_READ:
1903 #ifndef PROFILE
1904 if (pledge("stdio rpath recvfd unveil", NULL) == -1)
1905 err(1, "pledge");
1906 #endif
1907 apply_unveil_repo_readonly(repo_path);
1908 repo_read_main(title, repo_path, pack_fds, temp_fds);
1909 /* NOTREACHED */
1910 exit(0);
1911 case PROC_REPO_WRITE:
1912 #ifndef PROFILE
1913 if (pledge("stdio rpath recvfd unveil", NULL) == -1)
1914 err(1, "pledge");
1915 #endif
1916 apply_unveil_repo_readonly(repo_path);
1917 repo_write_main(title, repo_path, pack_fds, temp_fds);
1918 /* NOTREACHED */
1919 exit(0);
1920 default:
1921 fatal("invalid process id %d", proc_id);
1924 if (proc_id != PROC_GOTD)
1925 fatal("invalid process id %d", proc_id);
1927 apply_unveil_selfexec();
1929 signal_set(&evsigint, SIGINT, gotd_sighdlr, NULL);
1930 signal_set(&evsigterm, SIGTERM, gotd_sighdlr, NULL);
1931 signal_set(&evsighup, SIGHUP, gotd_sighdlr, NULL);
1932 signal_set(&evsigusr1, SIGUSR1, gotd_sighdlr, NULL);
1933 signal(SIGPIPE, SIG_IGN);
1935 signal_add(&evsigint, NULL);
1936 signal_add(&evsigterm, NULL);
1937 signal_add(&evsighup, NULL);
1938 signal_add(&evsigusr1, NULL);
1940 gotd_imsg_event_add(&gotd.listen_proc.iev);
1942 event_dispatch();
1944 free(repo_path);
1945 gotd_shutdown();
1947 return 0;