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()
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 idisconnect.client_id = client->id;
343 if (proc) {
344 if (gotd_imsg_compose_event(&proc->iev,
345 GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
346 &idisconnect, sizeof(idisconnect)) == -1)
347 log_warn("imsg compose DISCONNECT");
349 msgbuf_clear(&proc->iev.ibuf.w);
350 close(proc->iev.ibuf.fd);
351 kill_proc(proc, 0);
352 wait_for_child(proc->pid);
353 free(proc);
354 proc = NULL;
357 if (gotd_imsg_compose_event(&listen_proc->iev,
358 GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
359 &idisconnect, sizeof(idisconnect)) == -1)
360 log_warn("imsg compose DISCONNECT");
362 slot = client_hash(client->id) % nitems(gotd_clients);
363 STAILQ_REMOVE(&gotd_clients[slot], client, gotd_client, entry);
364 imsg_clear(&client->iev.ibuf);
365 event_del(&client->iev.ev);
366 evtimer_del(&client->tmo);
367 if (client->fd != -1)
368 close(client->fd);
369 else if (client->iev.ibuf.fd != -1)
370 close(client->iev.ibuf.fd);
371 free(client);
372 client_cnt--;
375 static void
376 disconnect_on_error(struct gotd_client *client, const struct got_error *err)
378 struct imsgbuf ibuf;
380 log_warnx("uid %d: %s", client->euid, err->msg);
381 if (err->code != GOT_ERR_EOF && client->fd != -1) {
382 imsg_init(&ibuf, client->fd);
383 gotd_imsg_send_error(&ibuf, 0, PROC_GOTD, err);
384 imsg_clear(&ibuf);
386 disconnect(client);
389 static const struct got_error *
390 send_repo_info(struct gotd_imsgev *iev, struct gotd_repo *repo)
392 const struct got_error *err = NULL;
393 struct gotd_imsg_info_repo irepo;
395 memset(&irepo, 0, sizeof(irepo));
397 if (strlcpy(irepo.repo_name, repo->name, sizeof(irepo.repo_name))
398 >= sizeof(irepo.repo_name))
399 return got_error_msg(GOT_ERR_NO_SPACE, "repo name too long");
400 if (strlcpy(irepo.repo_path, repo->path, sizeof(irepo.repo_path))
401 >= sizeof(irepo.repo_path))
402 return got_error_msg(GOT_ERR_NO_SPACE, "repo path too long");
404 if (gotd_imsg_compose_event(iev, GOTD_IMSG_INFO_REPO, PROC_GOTD, -1,
405 &irepo, sizeof(irepo)) == -1) {
406 err = got_error_from_errno("imsg compose INFO_REPO");
407 if (err)
408 return err;
411 return NULL;
414 static const struct got_error *
415 send_client_info(struct gotd_imsgev *iev, struct gotd_client *client)
417 const struct got_error *err = NULL;
418 struct gotd_imsg_info_client iclient;
419 struct gotd_child_proc *proc;
421 memset(&iclient, 0, sizeof(iclient));
422 iclient.euid = client->euid;
423 iclient.egid = client->egid;
425 proc = client->repo;
426 if (proc) {
427 if (strlcpy(iclient.repo_name, proc->repo_path,
428 sizeof(iclient.repo_name)) >= sizeof(iclient.repo_name)) {
429 return got_error_msg(GOT_ERR_NO_SPACE,
430 "repo name too long");
432 if (client_is_writing(client))
433 iclient.is_writing = 1;
435 iclient.repo_child_pid = proc->pid;
438 if (client->session)
439 iclient.session_child_pid = client->session->pid;
441 if (gotd_imsg_compose_event(iev, GOTD_IMSG_INFO_CLIENT, PROC_GOTD, -1,
442 &iclient, sizeof(iclient)) == -1) {
443 err = got_error_from_errno("imsg compose INFO_CLIENT");
444 if (err)
445 return err;
448 return NULL;
451 static const struct got_error *
452 send_info(struct gotd_client *client)
454 const struct got_error *err = NULL;
455 struct gotd_imsg_info info;
456 uint64_t slot;
457 struct gotd_repo *repo;
459 if (client->euid != 0)
460 return got_error_set_errno(EPERM, "info");
462 info.pid = gotd.pid;
463 info.verbosity = gotd.verbosity;
464 info.nrepos = gotd.nrepos;
465 info.nclients = client_cnt - 1;
467 if (gotd_imsg_compose_event(&client->iev, GOTD_IMSG_INFO, PROC_GOTD, -1,
468 &info, sizeof(info)) == -1) {
469 err = got_error_from_errno("imsg compose INFO");
470 if (err)
471 return err;
474 TAILQ_FOREACH(repo, &gotd.repos, entry) {
475 err = send_repo_info(&client->iev, repo);
476 if (err)
477 return err;
480 for (slot = 0; slot < nitems(gotd_clients); slot++) {
481 struct gotd_client *c;
482 STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
483 if (c->id == client->id)
484 continue;
485 err = send_client_info(&client->iev, c);
486 if (err)
487 return err;
491 return NULL;
494 static const struct got_error *
495 stop_gotd(struct gotd_client *client)
498 if (client->euid != 0)
499 return got_error_set_errno(EPERM, "stop");
501 gotd_shutdown();
502 /* NOTREACHED */
503 return NULL;
506 static struct gotd_repo *
507 find_repo_by_name(const char *repo_name)
509 struct gotd_repo *repo;
510 size_t namelen;
512 TAILQ_FOREACH(repo, &gotd.repos, entry) {
513 namelen = strlen(repo->name);
514 if (strncmp(repo->name, repo_name, namelen) != 0)
515 continue;
516 if (repo_name[namelen] == '\0' ||
517 strcmp(&repo_name[namelen], ".git") == 0)
518 return repo;
521 return NULL;
524 static const struct got_error *
525 start_client_authentication(struct gotd_client *client, struct imsg *imsg)
527 const struct got_error *err;
528 struct gotd_imsg_list_refs ireq;
529 struct gotd_repo *repo = NULL;
530 size_t datalen;
532 log_debug("list-refs request from uid %d", client->euid);
534 if (client->state != GOTD_CLIENT_STATE_NEW)
535 return got_error_msg(GOT_ERR_BAD_REQUEST,
536 "unexpected list-refs request received");
538 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
539 if (datalen != sizeof(ireq))
540 return got_error(GOT_ERR_PRIVSEP_LEN);
542 memcpy(&ireq, imsg->data, datalen);
544 if (ireq.client_is_reading) {
545 err = ensure_client_is_not_writing(client);
546 if (err)
547 return err;
548 repo = find_repo_by_name(ireq.repo_name);
549 if (repo == NULL)
550 return got_error(GOT_ERR_NOT_GIT_REPO);
551 err = start_auth_child(client, GOTD_AUTH_READ, repo,
552 gotd.argv0, gotd.confpath, gotd.daemonize,
553 gotd.verbosity);
554 if (err)
555 return err;
556 } else {
557 err = ensure_client_is_not_reading(client);
558 if (err)
559 return err;
560 repo = find_repo_by_name(ireq.repo_name);
561 if (repo == NULL)
562 return got_error(GOT_ERR_NOT_GIT_REPO);
563 err = start_auth_child(client,
564 GOTD_AUTH_READ | GOTD_AUTH_WRITE,
565 repo, gotd.argv0, gotd.confpath, gotd.daemonize,
566 gotd.verbosity);
567 if (err)
568 return err;
571 evtimer_add(&client->tmo, &auth_timeout);
573 /* Flow continues upon authentication successs/failure or timeout. */
574 return NULL;
577 static void
578 gotd_request(int fd, short events, void *arg)
580 struct gotd_imsgev *iev = arg;
581 struct imsgbuf *ibuf = &iev->ibuf;
582 struct gotd_client *client = iev->handler_arg;
583 const struct got_error *err = NULL;
584 struct imsg imsg;
585 ssize_t n;
587 if (events & EV_WRITE) {
588 while (ibuf->w.queued) {
589 n = msgbuf_write(&ibuf->w);
590 if (n == -1 && errno == EPIPE) {
591 /*
592 * The client has closed its socket.
593 * This can happen when Git clients are
594 * done sending pack file data.
595 */
596 msgbuf_clear(&ibuf->w);
597 continue;
598 } else if (n == -1 && errno != EAGAIN) {
599 err = got_error_from_errno("imsg_flush");
600 disconnect_on_error(client, err);
601 return;
603 if (n == 0) {
604 /* Connection closed. */
605 err = got_error(GOT_ERR_EOF);
606 disconnect_on_error(client, err);
607 return;
611 /* Disconnect gotctl(8) now that messages have been sent. */
612 if (!client_is_reading(client) && !client_is_writing(client)) {
613 disconnect(client);
614 return;
618 if ((events & EV_READ) == 0)
619 return;
621 memset(&imsg, 0, sizeof(imsg));
623 while (err == NULL) {
624 err = gotd_imsg_recv(&imsg, ibuf, 0);
625 if (err) {
626 if (err->code == GOT_ERR_PRIVSEP_READ)
627 err = NULL;
628 break;
631 evtimer_del(&client->tmo);
633 switch (imsg.hdr.type) {
634 case GOTD_IMSG_INFO:
635 err = send_info(client);
636 break;
637 case GOTD_IMSG_STOP:
638 err = stop_gotd(client);
639 break;
640 case GOTD_IMSG_LIST_REFS:
641 err = start_client_authentication(client, &imsg);
642 break;
643 default:
644 log_debug("unexpected imsg %d", imsg.hdr.type);
645 err = got_error(GOT_ERR_PRIVSEP_MSG);
646 break;
649 imsg_free(&imsg);
652 if (err) {
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",
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 if (geteuid() && (proc_id == PROC_GOTD || proc_id == PROC_LISTEN))
1762 fatalx("need root privileges");
1764 if (parse_config(confpath, proc_id, &gotd) != 0)
1765 return 1;
1767 pw = getpwnam(gotd.user_name);
1768 if (pw == NULL)
1769 fatalx("user %s not found", gotd.user_name);
1771 if (pw->pw_uid == 0)
1772 fatalx("cannot run %s as the superuser", getprogname());
1774 if (noaction) {
1775 fprintf(stderr, "configuration OK\n");
1776 return 0;
1779 gotd.argv0 = argv0;
1780 gotd.daemonize = daemonize;
1781 gotd.verbosity = verbosity;
1782 gotd.confpath = confpath;
1784 /* Require an absolute path in argv[0] for reliable re-exec. */
1785 if (!got_path_is_absolute(argv0))
1786 fatalx("bad path \"%s\": must be an absolute path", argv0);
1788 log_init(daemonize ? 0 : 1, LOG_DAEMON);
1789 log_setverbose(verbosity);
1791 if (proc_id == PROC_GOTD) {
1792 snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
1793 arc4random_buf(&clients_hash_key, sizeof(clients_hash_key));
1794 if (daemonize && daemon(1, 0) == -1)
1795 fatal("daemon");
1796 gotd.pid = getpid();
1797 start_listener(argv0, confpath, daemonize, verbosity);
1798 } else if (proc_id == PROC_LISTEN) {
1799 snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
1800 if (verbosity) {
1801 log_info("socket: %s", gotd.unix_socket_path);
1802 log_info("user: %s", pw->pw_name);
1805 fd = unix_socket_listen(gotd.unix_socket_path, pw->pw_uid,
1806 pw->pw_gid);
1807 if (fd == -1) {
1808 fatal("cannot listen on unix socket %s",
1809 gotd.unix_socket_path);
1811 } else if (proc_id == PROC_AUTH) {
1812 snprintf(title, sizeof(title), "%s %s",
1813 gotd_proc_names[proc_id], repo_path);
1814 } else if (proc_id == PROC_REPO_READ || proc_id == PROC_REPO_WRITE ||
1815 proc_id == PROC_SESSION) {
1816 error = got_repo_pack_fds_open(&pack_fds);
1817 if (error != NULL)
1818 fatalx("cannot open pack tempfiles: %s", error->msg);
1819 error = got_repo_temp_fds_open(&temp_fds);
1820 if (error != NULL)
1821 fatalx("cannot open pack tempfiles: %s", error->msg);
1822 if (repo_path == NULL)
1823 fatalx("repository path not specified");
1824 snprintf(title, sizeof(title), "%s %s",
1825 gotd_proc_names[proc_id], repo_path);
1826 } else
1827 fatal("invalid process id %d", proc_id);
1829 setproctitle("%s", title);
1830 log_procinit(title);
1832 /* Drop root privileges. */
1833 if (setgid(pw->pw_gid) == -1)
1834 fatal("setgid %d failed", pw->pw_gid);
1835 if (setuid(pw->pw_uid) == -1)
1836 fatal("setuid %d failed", pw->pw_uid);
1838 event_init();
1840 switch (proc_id) {
1841 case PROC_GOTD:
1842 #ifndef PROFILE
1843 /* "exec" promise will be limited to argv[0] via unveil(2). */
1844 if (pledge("stdio proc exec sendfd recvfd unveil", NULL) == -1)
1845 err(1, "pledge");
1846 #endif
1847 break;
1848 case PROC_LISTEN:
1849 #ifndef PROFILE
1850 if (pledge("stdio sendfd unix unveil", NULL) == -1)
1851 err(1, "pledge");
1852 #endif
1854 * Ensure that AF_UNIX bind(2) cannot be used with any other
1855 * sockets by revoking all filesystem access via unveil(2).
1857 apply_unveil_none();
1859 listen_main(title, fd, gotd.connection_limits,
1860 gotd.nconnection_limits);
1861 /* NOTREACHED */
1862 break;
1863 case PROC_AUTH:
1864 #ifndef PROFILE
1865 if (pledge("stdio getpw recvfd unix unveil", NULL) == -1)
1866 err(1, "pledge");
1867 #endif
1869 * We need the "unix" pledge promise for getpeername(2) only.
1870 * Ensure that AF_UNIX bind(2) cannot be used by revoking all
1871 * filesystem access via unveil(2). Access to password database
1872 * files will still work since "getpw" bypasses unveil(2).
1874 apply_unveil_none();
1876 auth_main(title, &gotd.repos, repo_path);
1877 /* NOTREACHED */
1878 break;
1879 case PROC_SESSION:
1880 #ifndef PROFILE
1882 * The "recvfd" promise is only needed during setup and
1883 * will be removed in a later pledge(2) call.
1885 if (pledge("stdio rpath wpath cpath recvfd sendfd fattr flock "
1886 "unveil", NULL) == -1)
1887 err(1, "pledge");
1888 #endif
1889 apply_unveil_repo_readwrite(repo_path);
1890 session_main(title, repo_path, pack_fds, temp_fds,
1891 &gotd.request_timeout);
1892 /* NOTREACHED */
1893 break;
1894 case PROC_REPO_READ:
1895 #ifndef PROFILE
1896 if (pledge("stdio rpath recvfd unveil", NULL) == -1)
1897 err(1, "pledge");
1898 #endif
1899 apply_unveil_repo_readonly(repo_path);
1900 repo_read_main(title, repo_path, pack_fds, temp_fds);
1901 /* NOTREACHED */
1902 exit(0);
1903 case PROC_REPO_WRITE:
1904 #ifndef PROFILE
1905 if (pledge("stdio rpath recvfd unveil", NULL) == -1)
1906 err(1, "pledge");
1907 #endif
1908 apply_unveil_repo_readonly(repo_path);
1909 repo_write_main(title, repo_path, pack_fds, temp_fds);
1910 /* NOTREACHED */
1911 exit(0);
1912 default:
1913 fatal("invalid process id %d", proc_id);
1916 if (proc_id != PROC_GOTD)
1917 fatal("invalid process id %d", proc_id);
1919 apply_unveil_selfexec();
1921 signal_set(&evsigint, SIGINT, gotd_sighdlr, NULL);
1922 signal_set(&evsigterm, SIGTERM, gotd_sighdlr, NULL);
1923 signal_set(&evsighup, SIGHUP, gotd_sighdlr, NULL);
1924 signal_set(&evsigusr1, SIGUSR1, gotd_sighdlr, NULL);
1925 signal(SIGPIPE, SIG_IGN);
1927 signal_add(&evsigint, NULL);
1928 signal_add(&evsigterm, NULL);
1929 signal_add(&evsighup, NULL);
1930 signal_add(&evsigusr1, NULL);
1932 gotd_imsg_event_add(&gotd.listen_proc.iev);
1934 event_dispatch();
1936 free(repo_path);
1937 gotd_shutdown();
1939 return 0;