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 <signal.h>
35 #include <siphash.h>
36 #include <stdarg.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <syslog.h>
41 #include <unistd.h>
43 #include "got_error.h"
44 #include "got_opentemp.h"
45 #include "got_path.h"
46 #include "got_repository.h"
47 #include "got_object.h"
48 #include "got_reference.h"
50 #include "got_lib_delta.h"
51 #include "got_lib_object.h"
52 #include "got_lib_object_cache.h"
53 #include "got_lib_sha1.h"
54 #include "got_lib_gitproto.h"
55 #include "got_lib_pack.h"
56 #include "got_lib_repository.h"
58 #include "gotd.h"
59 #include "log.h"
60 #include "listen.h"
61 #include "auth.h"
62 #include "session.h"
63 #include "repo_read.h"
64 #include "repo_write.h"
66 #ifndef nitems
67 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
68 #endif
70 enum gotd_client_state {
71 GOTD_CLIENT_STATE_NEW,
72 GOTD_CLIENT_STATE_ACCESS_GRANTED,
73 };
75 struct gotd_client {
76 STAILQ_ENTRY(gotd_client) entry;
77 enum gotd_client_state state;
78 uint32_t id;
79 int fd;
80 struct gotd_imsgev iev;
81 struct event tmo;
82 uid_t euid;
83 gid_t egid;
84 struct gotd_child_proc *repo;
85 struct gotd_child_proc *auth;
86 struct gotd_child_proc *session;
87 int required_auth;
88 };
89 STAILQ_HEAD(gotd_clients, gotd_client);
91 static struct gotd_clients gotd_clients[GOTD_CLIENT_TABLE_SIZE];
92 static SIPHASH_KEY clients_hash_key;
93 volatile int client_cnt;
94 static struct timeval auth_timeout = { 5, 0 };
95 static struct gotd gotd;
97 void gotd_sighdlr(int sig, short event, void *arg);
98 static void gotd_shutdown(void);
99 static const struct got_error *start_session_child(struct gotd_client *,
100 struct gotd_repo *, char *, const char *, int, int);
101 static const struct got_error *start_repo_child(struct gotd_client *,
102 enum gotd_procid, struct gotd_repo *, char *, const char *, int, int);
103 static const struct got_error *start_auth_child(struct gotd_client *, int,
104 struct gotd_repo *, char *, const char *, int, int);
105 static void kill_proc(struct gotd_child_proc *, int);
107 __dead static void
108 usage()
110 fprintf(stderr, "usage: %s [-dv] [-f config-file]\n", getprogname());
111 exit(1);
114 static int
115 unix_socket_listen(const char *unix_socket_path, uid_t uid, gid_t gid)
117 struct sockaddr_un sun;
118 int fd = -1;
119 mode_t old_umask, mode;
121 fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK| SOCK_CLOEXEC, 0);
122 if (fd == -1) {
123 log_warn("socket");
124 return -1;
127 sun.sun_family = AF_UNIX;
128 if (strlcpy(sun.sun_path, unix_socket_path,
129 sizeof(sun.sun_path)) >= sizeof(sun.sun_path)) {
130 log_warnx("%s: name too long", unix_socket_path);
131 close(fd);
132 return -1;
135 if (unlink(unix_socket_path) == -1) {
136 if (errno != ENOENT) {
137 log_warn("unlink %s", unix_socket_path);
138 close(fd);
139 return -1;
143 old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
144 mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
146 if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
147 log_warn("bind: %s", unix_socket_path);
148 close(fd);
149 umask(old_umask);
150 return -1;
153 umask(old_umask);
155 if (chmod(unix_socket_path, mode) == -1) {
156 log_warn("chmod %o %s", mode, unix_socket_path);
157 close(fd);
158 unlink(unix_socket_path);
159 return -1;
162 if (chown(unix_socket_path, uid, gid) == -1) {
163 log_warn("chown %s uid=%d gid=%d", unix_socket_path, uid, gid);
164 close(fd);
165 unlink(unix_socket_path);
166 return -1;
169 if (listen(fd, GOTD_UNIX_SOCKET_BACKLOG) == -1) {
170 log_warn("listen");
171 close(fd);
172 unlink(unix_socket_path);
173 return -1;
176 return fd;
179 static uint64_t
180 client_hash(uint32_t client_id)
182 return SipHash24(&clients_hash_key, &client_id, sizeof(client_id));
185 static void
186 add_client(struct gotd_client *client)
188 uint64_t slot = client_hash(client->id) % nitems(gotd_clients);
189 STAILQ_INSERT_HEAD(&gotd_clients[slot], client, entry);
190 client_cnt++;
193 static struct gotd_client *
194 find_client(uint32_t client_id)
196 uint64_t slot;
197 struct gotd_client *c;
199 slot = client_hash(client_id) % nitems(gotd_clients);
200 STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
201 if (c->id == client_id)
202 return c;
205 return NULL;
208 static struct gotd_client *
209 find_client_by_proc_fd(int fd)
211 uint64_t slot;
213 for (slot = 0; slot < nitems(gotd_clients); slot++) {
214 struct gotd_client *c;
216 STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
217 if (c->repo && c->repo->iev.ibuf.fd == fd)
218 return c;
219 if (c->auth && c->auth->iev.ibuf.fd == fd)
220 return c;
221 if (c->session && c->session->iev.ibuf.fd == fd)
222 return c;
226 return NULL;
229 static int
230 client_is_reading(struct gotd_client *client)
232 return (client->required_auth &
233 (GOTD_AUTH_READ | GOTD_AUTH_WRITE)) == GOTD_AUTH_READ;
236 static int
237 client_is_writing(struct gotd_client *client)
239 return (client->required_auth &
240 (GOTD_AUTH_READ | GOTD_AUTH_WRITE)) ==
241 (GOTD_AUTH_READ | GOTD_AUTH_WRITE);
244 static const struct got_error *
245 ensure_client_is_not_writing(struct gotd_client *client)
247 if (client_is_writing(client)) {
248 return got_error_fmt(GOT_ERR_BAD_PACKET,
249 "uid %d made a read-request but is writing to "
250 "a repository", client->euid);
253 return NULL;
256 static const struct got_error *
257 ensure_client_is_not_reading(struct gotd_client *client)
259 if (client_is_reading(client)) {
260 return got_error_fmt(GOT_ERR_BAD_PACKET,
261 "uid %d made a write-request but is reading from "
262 "a repository", client->euid);
265 return NULL;
268 static void
269 wait_for_child(pid_t child_pid)
271 pid_t pid;
272 int status;
274 log_debug("waiting for child PID %ld to terminate",
275 (long)child_pid);
277 do {
278 pid = waitpid(child_pid, &status, WNOHANG);
279 if (pid == -1) {
280 if (errno != EINTR && errno != ECHILD)
281 fatal("wait");
282 } else if (WIFSIGNALED(status)) {
283 log_warnx("child PID %ld terminated; signal %d",
284 (long)pid, WTERMSIG(status));
286 } while (pid != -1 || (pid == -1 && errno == EINTR));
289 static void
290 proc_done(struct gotd_child_proc *proc)
292 event_del(&proc->iev.ev);
293 msgbuf_clear(&proc->iev.ibuf.w);
294 close(proc->iev.ibuf.fd);
295 kill_proc(proc, 0);
296 wait_for_child(proc->pid);
297 free(proc);
300 static void
301 kill_auth_proc(struct gotd_client *client)
303 struct gotd_child_proc *proc;
305 if (client->auth == NULL)
306 return;
308 proc = client->auth;
309 client->auth = NULL;
311 proc_done(proc);
314 static void
315 kill_session_proc(struct gotd_client *client)
317 struct gotd_child_proc *proc;
319 if (client->session == NULL)
320 return;
322 proc = client->session;
323 client->session = NULL;
325 proc_done(proc);
328 static void
329 disconnect(struct gotd_client *client)
331 struct gotd_imsg_disconnect idisconnect;
332 struct gotd_child_proc *proc = client->repo;
333 struct gotd_child_proc *listen_proc = &gotd.listen_proc;
334 uint64_t slot;
336 log_debug("uid %d: disconnecting", client->euid);
338 kill_auth_proc(client);
339 kill_session_proc(client);
341 idisconnect.client_id = client->id;
342 if (proc) {
343 if (gotd_imsg_compose_event(&proc->iev,
344 GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
345 &idisconnect, sizeof(idisconnect)) == -1)
346 log_warn("imsg compose DISCONNECT");
348 msgbuf_clear(&proc->iev.ibuf.w);
349 close(proc->iev.ibuf.fd);
350 kill_proc(proc, 0);
351 wait_for_child(proc->pid);
352 free(proc);
353 proc = NULL;
356 if (gotd_imsg_compose_event(&listen_proc->iev,
357 GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
358 &idisconnect, sizeof(idisconnect)) == -1)
359 log_warn("imsg compose DISCONNECT");
361 slot = client_hash(client->id) % nitems(gotd_clients);
362 STAILQ_REMOVE(&gotd_clients[slot], client, gotd_client, entry);
363 imsg_clear(&client->iev.ibuf);
364 event_del(&client->iev.ev);
365 evtimer_del(&client->tmo);
366 if (client->fd != -1)
367 close(client->fd);
368 else if (client->iev.ibuf.fd != -1)
369 close(client->iev.ibuf.fd);
370 free(client);
371 client_cnt--;
374 static void
375 disconnect_on_error(struct gotd_client *client, const struct got_error *err)
377 struct imsgbuf ibuf;
379 log_warnx("uid %d: %s", client->euid, err->msg);
380 if (err->code != GOT_ERR_EOF && client->fd != -1) {
381 imsg_init(&ibuf, client->fd);
382 gotd_imsg_send_error(&ibuf, 0, PROC_GOTD, err);
383 imsg_clear(&ibuf);
385 disconnect(client);
388 static const struct got_error *
389 send_repo_info(struct gotd_imsgev *iev, struct gotd_repo *repo)
391 const struct got_error *err = NULL;
392 struct gotd_imsg_info_repo irepo;
394 memset(&irepo, 0, sizeof(irepo));
396 if (strlcpy(irepo.repo_name, repo->name, sizeof(irepo.repo_name))
397 >= sizeof(irepo.repo_name))
398 return got_error_msg(GOT_ERR_NO_SPACE, "repo name too long");
399 if (strlcpy(irepo.repo_path, repo->path, sizeof(irepo.repo_path))
400 >= sizeof(irepo.repo_path))
401 return got_error_msg(GOT_ERR_NO_SPACE, "repo path too long");
403 if (gotd_imsg_compose_event(iev, GOTD_IMSG_INFO_REPO, PROC_GOTD, -1,
404 &irepo, sizeof(irepo)) == -1) {
405 err = got_error_from_errno("imsg compose INFO_REPO");
406 if (err)
407 return err;
410 return NULL;
413 static const struct got_error *
414 send_client_info(struct gotd_imsgev *iev, struct gotd_client *client)
416 const struct got_error *err = NULL;
417 struct gotd_imsg_info_client iclient;
418 struct gotd_child_proc *proc;
420 memset(&iclient, 0, sizeof(iclient));
421 iclient.euid = client->euid;
422 iclient.egid = client->egid;
424 proc = client->repo;
425 if (proc) {
426 if (strlcpy(iclient.repo_name, proc->repo_path,
427 sizeof(iclient.repo_name)) >= sizeof(iclient.repo_name)) {
428 return got_error_msg(GOT_ERR_NO_SPACE,
429 "repo name too long");
431 if (client_is_writing(client))
432 iclient.is_writing = 1;
434 iclient.repo_child_pid = proc->pid;
437 if (client->session)
438 iclient.session_child_pid = client->session->pid;
440 if (gotd_imsg_compose_event(iev, GOTD_IMSG_INFO_CLIENT, PROC_GOTD, -1,
441 &iclient, sizeof(iclient)) == -1) {
442 err = got_error_from_errno("imsg compose INFO_CLIENT");
443 if (err)
444 return err;
447 return NULL;
450 static const struct got_error *
451 send_info(struct gotd_client *client)
453 const struct got_error *err = NULL;
454 struct gotd_imsg_info info;
455 uint64_t slot;
456 struct gotd_repo *repo;
458 if (client->euid != 0)
459 return got_error_set_errno(EPERM, "info");
461 info.pid = gotd.pid;
462 info.verbosity = gotd.verbosity;
463 info.nrepos = gotd.nrepos;
464 info.nclients = client_cnt - 1;
466 if (gotd_imsg_compose_event(&client->iev, GOTD_IMSG_INFO, PROC_GOTD, -1,
467 &info, sizeof(info)) == -1) {
468 err = got_error_from_errno("imsg compose INFO");
469 if (err)
470 return err;
473 TAILQ_FOREACH(repo, &gotd.repos, entry) {
474 err = send_repo_info(&client->iev, repo);
475 if (err)
476 return err;
479 for (slot = 0; slot < nitems(gotd_clients); slot++) {
480 struct gotd_client *c;
481 STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
482 if (c->id == client->id)
483 continue;
484 err = send_client_info(&client->iev, c);
485 if (err)
486 return err;
490 return NULL;
493 static const struct got_error *
494 stop_gotd(struct gotd_client *client)
497 if (client->euid != 0)
498 return got_error_set_errno(EPERM, "stop");
500 gotd_shutdown();
501 /* NOTREACHED */
502 return NULL;
505 static struct gotd_repo *
506 find_repo_by_name(const char *repo_name)
508 struct gotd_repo *repo;
509 size_t namelen;
511 TAILQ_FOREACH(repo, &gotd.repos, entry) {
512 namelen = strlen(repo->name);
513 if (strncmp(repo->name, repo_name, namelen) != 0)
514 continue;
515 if (repo_name[namelen] == '\0' ||
516 strcmp(&repo_name[namelen], ".git") == 0)
517 return repo;
520 return NULL;
523 static const struct got_error *
524 start_client_authentication(struct gotd_client *client, struct imsg *imsg)
526 const struct got_error *err;
527 struct gotd_imsg_list_refs ireq;
528 struct gotd_repo *repo = NULL;
529 size_t datalen;
531 log_debug("list-refs request from uid %d", client->euid);
533 if (client->state != GOTD_CLIENT_STATE_NEW)
534 return got_error_msg(GOT_ERR_BAD_REQUEST,
535 "unexpected list-refs request received");
537 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
538 if (datalen != sizeof(ireq))
539 return got_error(GOT_ERR_PRIVSEP_LEN);
541 memcpy(&ireq, imsg->data, datalen);
543 if (ireq.client_is_reading) {
544 err = ensure_client_is_not_writing(client);
545 if (err)
546 return err;
547 repo = find_repo_by_name(ireq.repo_name);
548 if (repo == NULL)
549 return got_error(GOT_ERR_NOT_GIT_REPO);
550 err = start_auth_child(client, GOTD_AUTH_READ, repo,
551 gotd.argv0, gotd.confpath, gotd.daemonize,
552 gotd.verbosity);
553 if (err)
554 return err;
555 } else {
556 err = ensure_client_is_not_reading(client);
557 if (err)
558 return err;
559 repo = find_repo_by_name(ireq.repo_name);
560 if (repo == NULL)
561 return got_error(GOT_ERR_NOT_GIT_REPO);
562 err = start_auth_child(client,
563 GOTD_AUTH_READ | GOTD_AUTH_WRITE,
564 repo, gotd.argv0, gotd.confpath, gotd.daemonize,
565 gotd.verbosity);
566 if (err)
567 return err;
570 evtimer_add(&client->tmo, &auth_timeout);
572 /* Flow continues upon authentication successs/failure or timeout. */
573 return NULL;
576 static void
577 gotd_request(int fd, short events, void *arg)
579 struct gotd_imsgev *iev = arg;
580 struct imsgbuf *ibuf = &iev->ibuf;
581 struct gotd_client *client = iev->handler_arg;
582 const struct got_error *err = NULL;
583 struct imsg imsg;
584 ssize_t n;
586 if (events & EV_WRITE) {
587 while (ibuf->w.queued) {
588 n = msgbuf_write(&ibuf->w);
589 if (n == -1 && errno == EPIPE) {
590 /*
591 * The client has closed its socket.
592 * This can happen when Git clients are
593 * done sending pack file data.
594 */
595 msgbuf_clear(&ibuf->w);
596 continue;
597 } else if (n == -1 && errno != EAGAIN) {
598 err = got_error_from_errno("imsg_flush");
599 disconnect_on_error(client, err);
600 return;
602 if (n == 0) {
603 /* Connection closed. */
604 err = got_error(GOT_ERR_EOF);
605 disconnect_on_error(client, err);
606 return;
610 /* Disconnect gotctl(8) now that messages have been sent. */
611 if (!client_is_reading(client) && !client_is_writing(client)) {
612 disconnect(client);
613 return;
617 if ((events & EV_READ) == 0)
618 return;
620 memset(&imsg, 0, sizeof(imsg));
622 while (err == NULL) {
623 err = gotd_imsg_recv(&imsg, ibuf, 0);
624 if (err) {
625 if (err->code == GOT_ERR_PRIVSEP_READ)
626 err = NULL;
627 break;
630 evtimer_del(&client->tmo);
632 switch (imsg.hdr.type) {
633 case GOTD_IMSG_INFO:
634 err = send_info(client);
635 break;
636 case GOTD_IMSG_STOP:
637 err = stop_gotd(client);
638 break;
639 case GOTD_IMSG_LIST_REFS:
640 err = start_client_authentication(client, &imsg);
641 break;
642 default:
643 log_debug("unexpected imsg %d", imsg.hdr.type);
644 err = got_error(GOT_ERR_PRIVSEP_MSG);
645 break;
648 imsg_free(&imsg);
651 if (err) {
652 if (err->code != GOT_ERR_EOF)
653 disconnect_on_error(client, err);
654 } else {
655 gotd_imsg_event_add(&client->iev);
659 static void
660 gotd_auth_timeout(int fd, short events, void *arg)
662 struct gotd_client *client = arg;
664 log_debug("disconnecting uid %d due to authentication timeout",
665 client->euid);
666 disconnect(client);
669 static const struct got_error *
670 recv_connect(uint32_t *client_id, struct imsg *imsg)
672 const struct got_error *err = NULL;
673 struct gotd_imsg_connect iconnect;
674 size_t datalen;
675 int s = -1;
676 struct gotd_client *client = NULL;
678 *client_id = 0;
680 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
681 if (datalen != sizeof(iconnect))
682 return got_error(GOT_ERR_PRIVSEP_LEN);
683 memcpy(&iconnect, imsg->data, sizeof(iconnect));
685 s = imsg->fd;
686 if (s == -1) {
687 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
688 goto done;
691 if (find_client(iconnect.client_id)) {
692 err = got_error_msg(GOT_ERR_CLIENT_ID, "duplicate client ID");
693 goto done;
696 client = calloc(1, sizeof(*client));
697 if (client == NULL) {
698 err = got_error_from_errno("calloc");
699 goto done;
702 *client_id = iconnect.client_id;
704 client->state = GOTD_CLIENT_STATE_NEW;
705 client->id = iconnect.client_id;
706 client->fd = s;
707 s = -1;
708 /* The auth process will verify UID/GID for us. */
709 client->euid = iconnect.euid;
710 client->egid = iconnect.egid;
712 imsg_init(&client->iev.ibuf, client->fd);
713 client->iev.handler = gotd_request;
714 client->iev.events = EV_READ;
715 client->iev.handler_arg = client;
717 event_set(&client->iev.ev, client->fd, EV_READ, gotd_request,
718 &client->iev);
719 gotd_imsg_event_add(&client->iev);
721 evtimer_set(&client->tmo, gotd_auth_timeout, client);
723 add_client(client);
724 log_debug("%s: new client uid %d connected on fd %d", __func__,
725 client->euid, client->fd);
726 done:
727 if (err) {
728 struct gotd_child_proc *listen_proc = &gotd.listen_proc;
729 struct gotd_imsg_disconnect idisconnect;
731 idisconnect.client_id = client->id;
732 if (gotd_imsg_compose_event(&listen_proc->iev,
733 GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
734 &idisconnect, sizeof(idisconnect)) == -1)
735 log_warn("imsg compose DISCONNECT");
737 if (s != -1)
738 close(s);
741 return err;
744 static const char *gotd_proc_names[PROC_MAX] = {
745 "parent",
746 "listen",
747 "auth",
748 "session",
749 "repo_read",
750 "repo_write"
751 };
753 static void
754 kill_proc(struct gotd_child_proc *proc, int fatal)
756 if (fatal) {
757 log_warnx("sending SIGKILL to PID %d", proc->pid);
758 kill(proc->pid, SIGKILL);
759 } else
760 kill(proc->pid, SIGTERM);
763 static void
764 gotd_shutdown(void)
766 struct gotd_child_proc *proc;
767 uint64_t slot;
769 log_debug("shutting down");
770 for (slot = 0; slot < nitems(gotd_clients); slot++) {
771 struct gotd_client *c, *tmp;
773 STAILQ_FOREACH_SAFE(c, &gotd_clients[slot], entry, tmp)
774 disconnect(c);
777 proc = &gotd.listen_proc;
778 msgbuf_clear(&proc->iev.ibuf.w);
779 close(proc->iev.ibuf.fd);
780 kill_proc(proc, 0);
781 wait_for_child(proc->pid);
783 log_info("terminating");
784 exit(0);
787 void
788 gotd_sighdlr(int sig, short event, void *arg)
790 /*
791 * Normal signal handler rules don't apply because libevent
792 * decouples for us.
793 */
795 switch (sig) {
796 case SIGHUP:
797 log_info("%s: ignoring SIGHUP", __func__);
798 break;
799 case SIGUSR1:
800 log_info("%s: ignoring SIGUSR1", __func__);
801 break;
802 case SIGTERM:
803 case SIGINT:
804 gotd_shutdown();
805 break;
806 default:
807 fatalx("unexpected signal");
811 static const struct got_error *
812 ensure_proc_is_reading(struct gotd_client *client,
813 struct gotd_child_proc *proc)
815 if (!client_is_reading(client)) {
816 kill_proc(proc, 1);
817 return got_error_fmt(GOT_ERR_BAD_PACKET,
818 "PID %d handled a read-request for uid %d but this "
819 "user is not reading from a repository", proc->pid,
820 client->euid);
823 return NULL;
826 static const struct got_error *
827 ensure_proc_is_writing(struct gotd_client *client,
828 struct gotd_child_proc *proc)
830 if (!client_is_writing(client)) {
831 kill_proc(proc, 1);
832 return got_error_fmt(GOT_ERR_BAD_PACKET,
833 "PID %d handled a write-request for uid %d but this "
834 "user is not writing to a repository", proc->pid,
835 client->euid);
838 return NULL;
841 static int
842 verify_imsg_src(struct gotd_client *client, struct gotd_child_proc *proc,
843 struct imsg *imsg)
845 const struct got_error *err;
846 int ret = 0;
848 if (proc->type == PROC_REPO_READ || proc->type == PROC_REPO_WRITE) {
849 if (client->repo == NULL)
850 fatalx("no process found for uid %d", client->euid);
851 if (proc->pid != client->repo->pid) {
852 kill_proc(proc, 1);
853 log_warnx("received message from PID %d for uid %d, "
854 "while PID %d is the process serving this user",
855 proc->pid, client->euid, client->repo->pid);
856 return 0;
859 if (proc->type == PROC_SESSION) {
860 if (client->session == NULL) {
861 log_warnx("no session found for uid %d", client->euid);
862 return 0;
864 if (proc->pid != client->session->pid) {
865 kill_proc(proc, 1);
866 log_warnx("received message from PID %d for uid %d, "
867 "while PID %d is the process serving this user",
868 proc->pid, client->euid, client->session->pid);
869 return 0;
873 switch (imsg->hdr.type) {
874 case GOTD_IMSG_ERROR:
875 ret = 1;
876 break;
877 case GOTD_IMSG_CONNECT:
878 if (proc->type != PROC_LISTEN) {
879 err = got_error_fmt(GOT_ERR_BAD_PACKET,
880 "new connection for uid %d from PID %d "
881 "which is not the listen process",
882 proc->pid, client->euid);
883 } else
884 ret = 1;
885 break;
886 case GOTD_IMSG_ACCESS_GRANTED:
887 if (proc->type != PROC_AUTH) {
888 err = got_error_fmt(GOT_ERR_BAD_PACKET,
889 "authentication of uid %d from PID %d "
890 "which is not the auth process",
891 proc->pid, client->euid);
892 } else
893 ret = 1;
894 break;
895 case GOTD_IMSG_CLIENT_SESSION_READY:
896 if (proc->type != PROC_SESSION) {
897 err = got_error_fmt(GOT_ERR_BAD_PACKET,
898 "unexpected \"ready\" signal from PID %d",
899 proc->pid);
900 } else
901 ret = 1;
902 break;
903 case GOTD_IMSG_REPO_CHILD_READY:
904 if (proc->type != PROC_REPO_READ &&
905 proc->type != PROC_REPO_WRITE) {
906 err = got_error_fmt(GOT_ERR_BAD_PACKET,
907 "unexpected \"ready\" signal from PID %d",
908 proc->pid);
909 } else
910 ret = 1;
911 break;
912 case GOTD_IMSG_PACKFILE_DONE:
913 err = ensure_proc_is_reading(client, proc);
914 if (err)
915 log_warnx("uid %d: %s", client->euid, err->msg);
916 else
917 ret = 1;
918 break;
919 case GOTD_IMSG_PACKFILE_INSTALL:
920 case GOTD_IMSG_REF_UPDATES_START:
921 case GOTD_IMSG_REF_UPDATE:
922 err = ensure_proc_is_writing(client, proc);
923 if (err)
924 log_warnx("uid %d: %s", client->euid, err->msg);
925 else
926 ret = 1;
927 break;
928 default:
929 log_debug("%s: unexpected imsg %d", __func__, imsg->hdr.type);
930 break;
933 return ret;
936 static const struct got_error *
937 connect_repo_child(struct gotd_client *client,
938 struct gotd_child_proc *repo_proc)
940 static const struct got_error *err;
941 struct gotd_imsgev *session_iev = &client->session->iev;
942 struct gotd_imsg_connect_repo_child ireq;
943 int pipe[2];
945 if (client->state != GOTD_CLIENT_STATE_ACCESS_GRANTED)
946 return got_error_msg(GOT_ERR_BAD_REQUEST,
947 "unexpected repo child ready signal received");
949 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
950 PF_UNSPEC, pipe) == -1)
951 fatal("socketpair");
953 memset(&ireq, 0, sizeof(ireq));
954 ireq.client_id = client->id;
955 ireq.proc_id = repo_proc->type;
957 /* Pass repo child pipe to session child process. */
958 if (gotd_imsg_compose_event(session_iev, GOTD_IMSG_CONNECT_REPO_CHILD,
959 PROC_GOTD, pipe[0], &ireq, sizeof(ireq)) == -1) {
960 err = got_error_from_errno("imsg compose CONNECT_REPO_CHILD");
961 close(pipe[0]);
962 close(pipe[1]);
963 return err;
966 /* Pass session child pipe to repo child process. */
967 if (gotd_imsg_compose_event(&repo_proc->iev,
968 GOTD_IMSG_CONNECT_REPO_CHILD, PROC_GOTD, pipe[1], NULL, 0) == -1) {
969 err = got_error_from_errno("imsg compose CONNECT_REPO_CHILD");
970 close(pipe[1]);
971 return err;
974 return NULL;
977 static void
978 gotd_dispatch_listener(int fd, short event, void *arg)
980 struct gotd_imsgev *iev = arg;
981 struct imsgbuf *ibuf = &iev->ibuf;
982 struct gotd_child_proc *proc = &gotd.listen_proc;
983 ssize_t n;
984 int shut = 0;
985 struct imsg imsg;
987 if (proc->iev.ibuf.fd != fd)
988 fatalx("%s: unexpected fd %d", __func__, fd);
990 if (event & EV_READ) {
991 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
992 fatal("imsg_read error");
993 if (n == 0) {
994 /* Connection closed. */
995 shut = 1;
996 goto done;
1000 if (event & EV_WRITE) {
1001 n = msgbuf_write(&ibuf->w);
1002 if (n == -1 && errno != EAGAIN)
1003 fatal("msgbuf_write");
1004 if (n == 0) {
1005 /* Connection closed. */
1006 shut = 1;
1007 goto done;
1011 for (;;) {
1012 const struct got_error *err = NULL;
1013 struct gotd_client *client = NULL;
1014 uint32_t client_id = 0;
1015 int do_disconnect = 0;
1017 if ((n = imsg_get(ibuf, &imsg)) == -1)
1018 fatal("%s: imsg_get error", __func__);
1019 if (n == 0) /* No more messages. */
1020 break;
1022 switch (imsg.hdr.type) {
1023 case GOTD_IMSG_ERROR:
1024 do_disconnect = 1;
1025 err = gotd_imsg_recv_error(&client_id, &imsg);
1026 break;
1027 case GOTD_IMSG_CONNECT:
1028 err = recv_connect(&client_id, &imsg);
1029 break;
1030 default:
1031 log_debug("unexpected imsg %d", imsg.hdr.type);
1032 break;
1035 client = find_client(client_id);
1036 if (client == NULL) {
1037 log_warnx("%s: client not found", __func__);
1038 imsg_free(&imsg);
1039 continue;
1042 if (err)
1043 log_warnx("uid %d: %s", client->euid, err->msg);
1045 if (do_disconnect) {
1046 if (err)
1047 disconnect_on_error(client, err);
1048 else
1049 disconnect(client);
1052 imsg_free(&imsg);
1054 done:
1055 if (!shut) {
1056 gotd_imsg_event_add(iev);
1057 } else {
1058 /* This pipe is dead. Remove its event handler */
1059 event_del(&iev->ev);
1060 event_loopexit(NULL);
1064 static void
1065 gotd_dispatch_auth_child(int fd, short event, void *arg)
1067 const struct got_error *err = NULL;
1068 struct gotd_imsgev *iev = arg;
1069 struct imsgbuf *ibuf = &iev->ibuf;
1070 struct gotd_client *client;
1071 struct gotd_repo *repo = NULL;
1072 ssize_t n;
1073 int shut = 0;
1074 struct imsg imsg;
1075 uint32_t client_id = 0;
1076 int do_disconnect = 0;
1078 client = find_client_by_proc_fd(fd);
1079 if (client == NULL)
1080 fatalx("cannot find client for fd %d", fd);
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\n",
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 fatalx("cannot find client for fd %d", fd);
1225 if (event & EV_READ) {
1226 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1227 fatal("imsg_read error");
1228 if (n == 0) {
1229 /* Connection closed. */
1230 shut = 1;
1231 goto done;
1235 if (event & EV_WRITE) {
1236 n = msgbuf_write(&ibuf->w);
1237 if (n == -1 && errno != EAGAIN)
1238 fatal("msgbuf_write");
1239 if (n == 0) {
1240 /* Connection closed. */
1241 shut = 1;
1242 goto done;
1246 proc = client->session;
1247 if (proc == NULL)
1248 fatalx("cannot find session child process for fd %d", fd);
1250 for (;;) {
1251 const struct got_error *err = NULL;
1252 uint32_t client_id = 0;
1253 int do_disconnect = 0, do_start_repo_child = 0;
1255 if ((n = imsg_get(ibuf, &imsg)) == -1)
1256 fatal("%s: imsg_get error", __func__);
1257 if (n == 0) /* No more messages. */
1258 break;
1260 switch (imsg.hdr.type) {
1261 case GOTD_IMSG_ERROR:
1262 do_disconnect = 1;
1263 err = gotd_imsg_recv_error(&client_id, &imsg);
1264 break;
1265 case GOTD_IMSG_CLIENT_SESSION_READY:
1266 if (client->state != GOTD_CLIENT_STATE_ACCESS_GRANTED) {
1267 err = got_error(GOT_ERR_PRIVSEP_MSG);
1268 break;
1270 do_start_repo_child = 1;
1271 break;
1272 case GOTD_IMSG_DISCONNECT:
1273 do_disconnect = 1;
1274 break;
1275 default:
1276 log_debug("unexpected imsg %d", imsg.hdr.type);
1277 break;
1280 if (!verify_imsg_src(client, proc, &imsg)) {
1281 log_debug("dropping imsg type %d from PID %d",
1282 imsg.hdr.type, proc->pid);
1283 imsg_free(&imsg);
1284 continue;
1286 if (err)
1287 log_warnx("uid %d: %s", client->euid, err->msg);
1289 if (do_start_repo_child) {
1290 struct gotd_repo *repo;
1292 repo = find_repo_by_name(client->session->repo_name);
1293 if (repo != NULL) {
1294 enum gotd_procid proc_type;
1296 if (client->required_auth & GOTD_AUTH_WRITE)
1297 proc_type = PROC_REPO_WRITE;
1298 else
1299 proc_type = PROC_REPO_READ;
1301 err = start_repo_child(client, proc_type, repo,
1302 gotd.argv0, gotd.confpath, gotd.daemonize,
1303 gotd.verbosity);
1304 } else
1305 err = got_error(GOT_ERR_NOT_GIT_REPO);
1307 if (err) {
1308 log_warnx("uid %d: %s", client->euid, err->msg);
1309 do_disconnect = 1;
1313 if (do_disconnect) {
1314 if (err)
1315 disconnect_on_error(client, err);
1316 else
1317 disconnect(client);
1320 imsg_free(&imsg);
1322 done:
1323 if (!shut) {
1324 gotd_imsg_event_add(iev);
1325 } else {
1326 /* This pipe is dead. Remove its event handler */
1327 event_del(&iev->ev);
1328 disconnect(client);
1332 static void
1333 gotd_dispatch_repo_child(int fd, short event, void *arg)
1335 struct gotd_imsgev *iev = arg;
1336 struct imsgbuf *ibuf = &iev->ibuf;
1337 struct gotd_child_proc *proc = NULL;
1338 struct gotd_client *client;
1339 ssize_t n;
1340 int shut = 0;
1341 struct imsg imsg;
1343 client = find_client_by_proc_fd(fd);
1344 if (client == NULL)
1345 fatalx("cannot find client for fd %d", fd);
1347 if (event & EV_READ) {
1348 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1349 fatal("imsg_read error");
1350 if (n == 0) {
1351 /* Connection closed. */
1352 shut = 1;
1353 goto done;
1357 if (event & EV_WRITE) {
1358 n = msgbuf_write(&ibuf->w);
1359 if (n == -1 && errno != EAGAIN)
1360 fatal("msgbuf_write");
1361 if (n == 0) {
1362 /* Connection closed. */
1363 shut = 1;
1364 goto done;
1368 proc = client->repo;
1369 if (proc == NULL)
1370 fatalx("cannot find child process for fd %d", fd);
1372 for (;;) {
1373 const struct got_error *err = NULL;
1374 uint32_t client_id = 0;
1375 int do_disconnect = 0;
1377 if ((n = imsg_get(ibuf, &imsg)) == -1)
1378 fatal("%s: imsg_get error", __func__);
1379 if (n == 0) /* No more messages. */
1380 break;
1382 switch (imsg.hdr.type) {
1383 case GOTD_IMSG_ERROR:
1384 do_disconnect = 1;
1385 err = gotd_imsg_recv_error(&client_id, &imsg);
1386 break;
1387 case GOTD_IMSG_REPO_CHILD_READY:
1388 err = connect_session(client);
1389 if (err)
1390 break;
1391 err = connect_repo_child(client, proc);
1392 break;
1393 default:
1394 log_debug("unexpected imsg %d", imsg.hdr.type);
1395 break;
1398 if (!verify_imsg_src(client, proc, &imsg)) {
1399 log_debug("dropping imsg type %d from PID %d",
1400 imsg.hdr.type, proc->pid);
1401 imsg_free(&imsg);
1402 continue;
1404 if (err)
1405 log_warnx("uid %d: %s", client->euid, err->msg);
1407 if (do_disconnect) {
1408 if (err)
1409 disconnect_on_error(client, err);
1410 else
1411 disconnect(client);
1414 imsg_free(&imsg);
1416 done:
1417 if (!shut) {
1418 gotd_imsg_event_add(iev);
1419 } else {
1420 /* This pipe is dead. Remove its event handler */
1421 event_del(&iev->ev);
1422 disconnect(client);
1426 static pid_t
1427 start_child(enum gotd_procid proc_id, const char *repo_path,
1428 char *argv0, const char *confpath, int fd, int daemonize, int verbosity)
1430 char *argv[11];
1431 int argc = 0;
1432 pid_t pid;
1434 switch (pid = fork()) {
1435 case -1:
1436 fatal("cannot fork");
1437 case 0:
1438 break;
1439 default:
1440 close(fd);
1441 return pid;
1444 if (fd != GOTD_FILENO_MSG_PIPE) {
1445 if (dup2(fd, GOTD_FILENO_MSG_PIPE) == -1)
1446 fatal("cannot setup imsg fd");
1447 } else if (fcntl(fd, F_SETFD, 0) == -1)
1448 fatal("cannot setup imsg fd");
1450 argv[argc++] = argv0;
1451 switch (proc_id) {
1452 case PROC_LISTEN:
1453 argv[argc++] = (char *)"-L";
1454 break;
1455 case PROC_AUTH:
1456 argv[argc++] = (char *)"-A";
1457 break;
1458 case PROC_SESSION:
1459 argv[argc++] = (char *)"-S";
1460 break;
1461 case PROC_REPO_READ:
1462 argv[argc++] = (char *)"-R";
1463 break;
1464 case PROC_REPO_WRITE:
1465 argv[argc++] = (char *)"-W";
1466 break;
1467 default:
1468 fatalx("invalid process id %d", proc_id);
1471 argv[argc++] = (char *)"-f";
1472 argv[argc++] = (char *)confpath;
1474 if (repo_path) {
1475 argv[argc++] = (char *)"-P";
1476 argv[argc++] = (char *)repo_path;
1479 if (!daemonize)
1480 argv[argc++] = (char *)"-d";
1481 if (verbosity > 0)
1482 argv[argc++] = (char *)"-v";
1483 if (verbosity > 1)
1484 argv[argc++] = (char *)"-v";
1485 argv[argc++] = NULL;
1487 execvp(argv0, argv);
1488 fatal("execvp");
1491 static void
1492 start_listener(char *argv0, const char *confpath, int daemonize, int verbosity)
1494 struct gotd_child_proc *proc = &gotd.listen_proc;
1496 proc->type = PROC_LISTEN;
1498 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1499 PF_UNSPEC, proc->pipe) == -1)
1500 fatal("socketpair");
1502 proc->pid = start_child(proc->type, NULL, argv0, confpath,
1503 proc->pipe[1], daemonize, verbosity);
1504 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1505 proc->iev.handler = gotd_dispatch_listener;
1506 proc->iev.events = EV_READ;
1507 proc->iev.handler_arg = NULL;
1510 static const struct got_error *
1511 start_session_child(struct gotd_client *client, struct gotd_repo *repo,
1512 char *argv0, const char *confpath, int daemonize, int verbosity)
1514 struct gotd_child_proc *proc;
1516 proc = calloc(1, sizeof(*proc));
1517 if (proc == NULL)
1518 return got_error_from_errno("calloc");
1520 proc->type = PROC_SESSION;
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)
1659 if (unveil(repo_path, "r") == -1)
1660 fatal("unveil %s", repo_path);
1662 if (unveil(NULL, NULL) == -1)
1663 fatal("unveil");
1666 static void
1667 apply_unveil_repo_readwrite(const char *repo_path)
1669 if (unveil(repo_path, "rwc") == -1)
1670 fatal("unveil %s", repo_path);
1672 if (unveil(GOT_TMPDIR_STR, "rwc") == -1)
1673 fatal("unveil %s", GOT_TMPDIR_STR);
1675 if (unveil(NULL, NULL) == -1)
1676 fatal("unveil");
1679 static void
1680 apply_unveil_none(void)
1682 if (unveil("/", "") == -1)
1683 fatal("unveil");
1685 if (unveil(NULL, NULL) == -1)
1686 fatal("unveil");
1689 static void
1690 apply_unveil_selfexec(void)
1692 if (unveil(gotd.argv0, "x") == -1)
1693 fatal("unveil %s", gotd.argv0);
1695 if (unveil(NULL, NULL) == -1)
1696 fatal("unveil");
1699 int
1700 main(int argc, char **argv)
1702 const struct got_error *error = NULL;
1703 int ch, fd = -1, daemonize = 1, verbosity = 0, noaction = 0;
1704 const char *confpath = GOTD_CONF_PATH;
1705 char *argv0 = argv[0];
1706 char title[2048];
1707 struct passwd *pw = NULL;
1708 char *repo_path = NULL;
1709 enum gotd_procid proc_id = PROC_GOTD;
1710 struct event evsigint, evsigterm, evsighup, evsigusr1;
1711 int *pack_fds = NULL, *temp_fds = NULL;
1713 log_init(1, LOG_DAEMON); /* Log to stderr until daemonized. */
1715 while ((ch = getopt(argc, argv, "Adf:LnP:RSvW")) != -1) {
1716 switch (ch) {
1717 case 'A':
1718 proc_id = PROC_AUTH;
1719 break;
1720 case 'd':
1721 daemonize = 0;
1722 break;
1723 case 'f':
1724 confpath = optarg;
1725 break;
1726 case 'L':
1727 proc_id = PROC_LISTEN;
1728 break;
1729 case 'n':
1730 noaction = 1;
1731 break;
1732 case 'P':
1733 repo_path = realpath(optarg, NULL);
1734 if (repo_path == NULL)
1735 fatal("realpath '%s'", optarg);
1736 break;
1737 case 'R':
1738 proc_id = PROC_REPO_READ;
1739 break;
1740 case 'S':
1741 proc_id = PROC_SESSION;
1742 break;
1743 case 'v':
1744 if (verbosity < 3)
1745 verbosity++;
1746 break;
1747 case 'W':
1748 proc_id = PROC_REPO_WRITE;
1749 break;
1750 default:
1751 usage();
1755 argc -= optind;
1756 argv += optind;
1758 if (argc != 0)
1759 usage();
1761 /* Require an absolute path in argv[0] for reliable re-exec. */
1762 if (!got_path_is_absolute(argv0))
1763 fatalx("bad path \"%s\": must be an absolute path", argv0);
1765 if (geteuid() && (proc_id == PROC_GOTD || proc_id == PROC_LISTEN))
1766 fatalx("need root privileges");
1768 log_init(daemonize ? 0 : 1, LOG_DAEMON);
1769 log_setverbose(verbosity);
1771 if (parse_config(confpath, proc_id, &gotd) != 0)
1772 return 1;
1774 gotd.argv0 = argv0;
1775 gotd.daemonize = daemonize;
1776 gotd.verbosity = verbosity;
1777 gotd.confpath = confpath;
1779 if (proc_id == PROC_GOTD &&
1780 (gotd.nrepos == 0 || TAILQ_EMPTY(&gotd.repos)))
1781 fatalx("no repository defined in configuration file");
1783 pw = getpwnam(gotd.user_name);
1784 if (pw == NULL)
1785 fatalx("user %s not found", gotd.user_name);
1787 if (pw->pw_uid == 0) {
1788 fatalx("cannot run %s as %s: the user running %s "
1789 "must not be the superuser",
1790 getprogname(), pw->pw_name, getprogname());
1793 if (proc_id == PROC_LISTEN &&
1794 !got_path_is_absolute(gotd.unix_socket_path))
1795 fatalx("bad unix socket path \"%s\": must be an absolute path",
1796 gotd.unix_socket_path);
1798 if (noaction)
1799 return 0;
1801 if (proc_id == PROC_GOTD) {
1802 gotd.pid = getpid();
1803 snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
1804 start_listener(argv0, confpath, daemonize, verbosity);
1805 arc4random_buf(&clients_hash_key, sizeof(clients_hash_key));
1806 if (daemonize && daemon(1, 0) == -1)
1807 fatal("daemon");
1808 } else if (proc_id == PROC_LISTEN) {
1809 snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
1810 if (verbosity) {
1811 log_info("socket: %s", gotd.unix_socket_path);
1812 log_info("user: %s", pw->pw_name);
1815 fd = unix_socket_listen(gotd.unix_socket_path, pw->pw_uid,
1816 pw->pw_gid);
1817 if (fd == -1) {
1818 fatal("cannot listen on unix socket %s",
1819 gotd.unix_socket_path);
1821 if (daemonize && daemon(0, 0) == -1)
1822 fatal("daemon");
1823 } else if (proc_id == PROC_AUTH) {
1824 snprintf(title, sizeof(title), "%s %s",
1825 gotd_proc_names[proc_id], repo_path);
1826 if (daemonize && daemon(0, 0) == -1)
1827 fatal("daemon");
1828 } else if (proc_id == PROC_REPO_READ || proc_id == PROC_REPO_WRITE ||
1829 proc_id == PROC_SESSION) {
1830 error = got_repo_pack_fds_open(&pack_fds);
1831 if (error != NULL)
1832 fatalx("cannot open pack tempfiles: %s", error->msg);
1833 error = got_repo_temp_fds_open(&temp_fds);
1834 if (error != NULL)
1835 fatalx("cannot open pack tempfiles: %s", error->msg);
1836 if (repo_path == NULL)
1837 fatalx("repository path not specified");
1838 snprintf(title, sizeof(title), "%s %s",
1839 gotd_proc_names[proc_id], repo_path);
1840 if (daemonize && daemon(0, 0) == -1)
1841 fatal("daemon");
1842 } else
1843 fatal("invalid process id %d", proc_id);
1845 setproctitle("%s", title);
1846 log_procinit(title);
1848 /* Drop root privileges. */
1849 if (setgid(pw->pw_gid) == -1)
1850 fatal("setgid %d failed", pw->pw_gid);
1851 if (setuid(pw->pw_uid) == -1)
1852 fatal("setuid %d failed", pw->pw_uid);
1854 event_init();
1856 switch (proc_id) {
1857 case PROC_GOTD:
1858 #ifndef PROFILE
1859 /* "exec" promise will be limited to argv[0] via unveil(2). */
1860 if (pledge("stdio proc exec sendfd recvfd unveil", NULL) == -1)
1861 err(1, "pledge");
1862 #endif
1863 break;
1864 case PROC_LISTEN:
1865 #ifndef PROFILE
1866 if (pledge("stdio sendfd unix unveil", NULL) == -1)
1867 err(1, "pledge");
1868 #endif
1870 * Ensure that AF_UNIX bind(2) cannot be used with any other
1871 * sockets by revoking all filesystem access via unveil(2).
1873 apply_unveil_none();
1875 listen_main(title, fd, gotd.connection_limits,
1876 gotd.nconnection_limits);
1877 /* NOTREACHED */
1878 break;
1879 case PROC_AUTH:
1880 #ifndef PROFILE
1881 if (pledge("stdio getpw recvfd unix unveil", NULL) == -1)
1882 err(1, "pledge");
1883 #endif
1885 * We need the "unix" pledge promise for getpeername(2) only.
1886 * Ensure that AF_UNIX bind(2) cannot be used by revoking all
1887 * filesystem access via unveil(2). Access to password database
1888 * files will still work since "getpw" bypasses unveil(2).
1890 apply_unveil_none();
1892 auth_main(title, &gotd.repos, repo_path);
1893 /* NOTREACHED */
1894 break;
1895 case PROC_SESSION:
1896 #ifndef PROFILE
1898 * The "recvfd" promise is only needed during setup and
1899 * will be removed in a later pledge(2) call.
1901 if (pledge("stdio rpath wpath cpath recvfd sendfd fattr flock "
1902 "unveil", NULL) == -1)
1903 err(1, "pledge");
1904 #endif
1905 apply_unveil_repo_readwrite(repo_path);
1906 session_main(title, repo_path, pack_fds, temp_fds,
1907 &gotd.request_timeout);
1908 /* NOTREACHED */
1909 break;
1910 case PROC_REPO_READ:
1911 #ifndef PROFILE
1912 if (pledge("stdio rpath recvfd unveil", NULL) == -1)
1913 err(1, "pledge");
1914 #endif
1915 apply_unveil_repo_readonly(repo_path);
1916 repo_read_main(title, repo_path, pack_fds, temp_fds);
1917 /* NOTREACHED */
1918 exit(0);
1919 case PROC_REPO_WRITE:
1920 #ifndef PROFILE
1921 if (pledge("stdio rpath recvfd unveil", NULL) == -1)
1922 err(1, "pledge");
1923 #endif
1924 apply_unveil_repo_readonly(repo_path);
1925 repo_write_main(title, repo_path, pack_fds, temp_fds);
1926 /* NOTREACHED */
1927 exit(0);
1928 default:
1929 fatal("invalid process id %d", proc_id);
1932 if (proc_id != PROC_GOTD)
1933 fatal("invalid process id %d", proc_id);
1935 apply_unveil_selfexec();
1937 signal_set(&evsigint, SIGINT, gotd_sighdlr, NULL);
1938 signal_set(&evsigterm, SIGTERM, gotd_sighdlr, NULL);
1939 signal_set(&evsighup, SIGHUP, gotd_sighdlr, NULL);
1940 signal_set(&evsigusr1, SIGUSR1, gotd_sighdlr, NULL);
1941 signal(SIGPIPE, SIG_IGN);
1943 signal_add(&evsigint, NULL);
1944 signal_add(&evsigterm, NULL);
1945 signal_add(&evsighup, NULL);
1946 signal_add(&evsigusr1, NULL);
1948 gotd_imsg_event_add(&gotd.listen_proc.iev);
1950 event_dispatch();
1952 free(repo_path);
1953 gotd_shutdown();
1955 return 0;