Blob


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