Blob


1 /*
2 * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/queue.h>
18 #include <sys/tree.h>
19 #include <sys/time.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <sys/socket.h>
23 #include <sys/un.h>
24 #include <sys/wait.h>
26 #include <fcntl.h>
27 #include <err.h>
28 #include <errno.h>
29 #include <event.h>
30 #include <limits.h>
31 #include <pwd.h>
32 #include <imsg.h>
33 #include <sha1.h>
34 #include <signal.h>
35 #include <siphash.h>
36 #include <stdarg.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <syslog.h>
41 #include <unistd.h>
43 #include "got_error.h"
44 #include "got_opentemp.h"
45 #include "got_path.h"
46 #include "got_repository.h"
47 #include "got_object.h"
48 #include "got_reference.h"
50 #include "got_lib_delta.h"
51 #include "got_lib_object.h"
52 #include "got_lib_object_cache.h"
53 #include "got_lib_sha1.h"
54 #include "got_lib_gitproto.h"
55 #include "got_lib_pack.h"
56 #include "got_lib_repository.h"
58 #include "gotd.h"
59 #include "log.h"
60 #include "listen.h"
61 #include "auth.h"
62 #include "session.h"
63 #include "repo_read.h"
64 #include "repo_write.h"
66 #ifndef nitems
67 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
68 #endif
70 struct gotd_client {
71 STAILQ_ENTRY(gotd_client) entry;
72 enum gotd_client_state state;
73 struct gotd_client_capability *capabilities;
74 size_t ncapa_alloc;
75 size_t ncapabilities;
76 uint32_t id;
77 int fd;
78 struct gotd_imsgev iev;
79 struct event tmo;
80 uid_t euid;
81 gid_t egid;
82 struct gotd_child_proc *repo_read;
83 struct gotd_child_proc *repo_write;
84 struct gotd_child_proc *auth;
85 struct gotd_child_proc *session;
86 int required_auth;
87 };
88 STAILQ_HEAD(gotd_clients, gotd_client);
90 static struct gotd_clients gotd_clients[GOTD_CLIENT_TABLE_SIZE];
91 static SIPHASH_KEY clients_hash_key;
92 volatile int client_cnt;
93 static struct timeval auth_timeout = { 5, 0 };
94 static struct gotd gotd;
96 void gotd_sighdlr(int sig, short event, void *arg);
97 static void gotd_shutdown(void);
98 static const struct got_error *start_session_child(struct gotd_client *,
99 struct gotd_repo *, char *, const char *, int, int);
100 static const struct got_error *start_repo_child(struct gotd_client *,
101 enum gotd_procid, struct gotd_repo *, char *, const char *, int, int);
102 static const struct got_error *start_auth_child(struct gotd_client *, int,
103 struct gotd_repo *, char *, const char *, int, int);
104 static void kill_proc(struct gotd_child_proc *, int);
106 __dead static void
107 usage()
109 fprintf(stderr, "usage: %s [-dv] [-f config-file]\n", getprogname());
110 exit(1);
113 static int
114 unix_socket_listen(const char *unix_socket_path, uid_t uid, gid_t gid)
116 struct sockaddr_un sun;
117 int fd = -1;
118 mode_t old_umask, mode;
120 fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK| SOCK_CLOEXEC, 0);
121 if (fd == -1) {
122 log_warn("socket");
123 return -1;
126 sun.sun_family = AF_UNIX;
127 if (strlcpy(sun.sun_path, unix_socket_path,
128 sizeof(sun.sun_path)) >= sizeof(sun.sun_path)) {
129 log_warnx("%s: name too long", unix_socket_path);
130 close(fd);
131 return -1;
134 if (unlink(unix_socket_path) == -1) {
135 if (errno != ENOENT) {
136 log_warn("unlink %s", unix_socket_path);
137 close(fd);
138 return -1;
142 old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
143 mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
145 if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
146 log_warn("bind: %s", unix_socket_path);
147 close(fd);
148 umask(old_umask);
149 return -1;
152 umask(old_umask);
154 if (chmod(unix_socket_path, mode) == -1) {
155 log_warn("chmod %o %s", mode, unix_socket_path);
156 close(fd);
157 unlink(unix_socket_path);
158 return -1;
161 if (chown(unix_socket_path, uid, gid) == -1) {
162 log_warn("chown %s uid=%d gid=%d", unix_socket_path, uid, gid);
163 close(fd);
164 unlink(unix_socket_path);
165 return -1;
168 if (listen(fd, GOTD_UNIX_SOCKET_BACKLOG) == -1) {
169 log_warn("listen");
170 close(fd);
171 unlink(unix_socket_path);
172 return -1;
175 return fd;
178 static uint64_t
179 client_hash(uint32_t client_id)
181 return SipHash24(&clients_hash_key, &client_id, sizeof(client_id));
184 static void
185 add_client(struct gotd_client *client)
187 uint64_t slot = client_hash(client->id) % nitems(gotd_clients);
188 STAILQ_INSERT_HEAD(&gotd_clients[slot], client, entry);
189 client_cnt++;
192 static struct gotd_client *
193 find_client(uint32_t client_id)
195 uint64_t slot;
196 struct gotd_client *c;
198 slot = client_hash(client_id) % nitems(gotd_clients);
199 STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
200 if (c->id == client_id)
201 return c;
204 return NULL;
207 static struct gotd_child_proc *
208 get_client_repo_proc(struct gotd_client *client)
210 if (client->repo_read && client->repo_write) {
211 fatalx("uid %d is reading and writing in the same session",
212 client->euid);
213 /* NOTREACHED */
216 if (client->repo_read)
217 return client->repo_read;
218 else if (client->repo_write)
219 return client->repo_write;
221 return NULL;
224 static struct gotd_client *
225 find_client_by_proc_fd(int fd)
227 uint64_t slot;
229 for (slot = 0; slot < nitems(gotd_clients); slot++) {
230 struct gotd_client *c;
232 STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
233 struct gotd_child_proc *proc = get_client_repo_proc(c);
234 if (proc && proc->iev.ibuf.fd == fd)
235 return c;
236 if (c->auth && c->auth->iev.ibuf.fd == fd)
237 return c;
238 if (c->session && c->session->iev.ibuf.fd == fd)
239 return c;
243 return NULL;
246 static int
247 client_is_reading(struct gotd_client *client)
249 return client->repo_read != NULL;
252 static int
253 client_is_writing(struct gotd_client *client)
255 return client->repo_write != NULL;
258 static const struct got_error *
259 ensure_client_is_not_writing(struct gotd_client *client)
261 if (client_is_writing(client)) {
262 return got_error_fmt(GOT_ERR_BAD_PACKET,
263 "uid %d made a read-request but is writing to "
264 "a repository", client->euid);
267 return NULL;
270 static const struct got_error *
271 ensure_client_is_not_reading(struct gotd_client *client)
273 if (client_is_reading(client)) {
274 return got_error_fmt(GOT_ERR_BAD_PACKET,
275 "uid %d made a write-request but is reading from "
276 "a repository", client->euid);
279 return NULL;
282 static void
283 wait_for_child(pid_t child_pid)
285 pid_t pid;
286 int status;
288 log_debug("waiting for child PID %ld to terminate",
289 (long)child_pid);
291 do {
292 pid = waitpid(child_pid, &status, WNOHANG);
293 if (pid == -1) {
294 if (errno != EINTR && errno != ECHILD)
295 fatal("wait");
296 } else if (WIFSIGNALED(status)) {
297 log_warnx("child PID %ld terminated; signal %d",
298 (long)pid, WTERMSIG(status));
300 } while (pid != -1 || (pid == -1 && errno == EINTR));
303 static void
304 proc_done(struct gotd_child_proc *proc)
306 event_del(&proc->iev.ev);
307 msgbuf_clear(&proc->iev.ibuf.w);
308 close(proc->iev.ibuf.fd);
309 kill_proc(proc, 0);
310 wait_for_child(proc->pid);
311 free(proc);
314 static void
315 kill_auth_proc(struct gotd_client *client)
317 struct gotd_child_proc *proc;
319 if (client->auth == NULL)
320 return;
322 proc = client->auth;
323 client->auth = NULL;
325 proc_done(proc);
328 static void
329 kill_session_proc(struct gotd_client *client)
331 struct gotd_child_proc *proc;
333 if (client->session == NULL)
334 return;
336 proc = client->session;
337 client->session = NULL;
339 proc_done(proc);
342 static void
343 disconnect(struct gotd_client *client)
345 struct gotd_imsg_disconnect idisconnect;
346 struct gotd_child_proc *proc = get_client_repo_proc(client);
347 struct gotd_child_proc *listen_proc = &gotd.listen_proc;
348 uint64_t slot;
350 log_debug("uid %d: disconnecting", client->euid);
352 kill_auth_proc(client);
353 kill_session_proc(client);
355 idisconnect.client_id = client->id;
356 if (proc) {
357 if (gotd_imsg_compose_event(&proc->iev,
358 GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
359 &idisconnect, sizeof(idisconnect)) == -1)
360 log_warn("imsg compose DISCONNECT");
362 msgbuf_clear(&proc->iev.ibuf.w);
363 close(proc->iev.ibuf.fd);
364 kill_proc(proc, 0);
365 wait_for_child(proc->pid);
366 free(proc);
367 proc = NULL;
370 if (gotd_imsg_compose_event(&listen_proc->iev,
371 GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
372 &idisconnect, sizeof(idisconnect)) == -1)
373 log_warn("imsg compose DISCONNECT");
375 slot = client_hash(client->id) % nitems(gotd_clients);
376 STAILQ_REMOVE(&gotd_clients[slot], client, gotd_client, entry);
377 imsg_clear(&client->iev.ibuf);
378 event_del(&client->iev.ev);
379 evtimer_del(&client->tmo);
380 if (client->fd != -1)
381 close(client->fd);
382 else if (client->iev.ibuf.fd != -1)
383 close(client->iev.ibuf.fd);
384 free(client->capabilities);
385 free(client);
386 client_cnt--;
389 static void
390 disconnect_on_error(struct gotd_client *client, const struct got_error *err)
392 struct imsgbuf ibuf;
394 log_warnx("uid %d: %s", client->euid, err->msg);
395 if (err->code != GOT_ERR_EOF && client->fd != -1) {
396 imsg_init(&ibuf, client->fd);
397 gotd_imsg_send_error(&ibuf, 0, PROC_GOTD, err);
398 imsg_clear(&ibuf);
400 disconnect(client);
403 static const struct got_error *
404 send_repo_info(struct gotd_imsgev *iev, struct gotd_repo *repo)
406 const struct got_error *err = NULL;
407 struct gotd_imsg_info_repo irepo;
409 memset(&irepo, 0, sizeof(irepo));
411 if (strlcpy(irepo.repo_name, repo->name, sizeof(irepo.repo_name))
412 >= sizeof(irepo.repo_name))
413 return got_error_msg(GOT_ERR_NO_SPACE, "repo name too long");
414 if (strlcpy(irepo.repo_path, repo->path, sizeof(irepo.repo_path))
415 >= sizeof(irepo.repo_path))
416 return got_error_msg(GOT_ERR_NO_SPACE, "repo path too long");
418 if (gotd_imsg_compose_event(iev, GOTD_IMSG_INFO_REPO, PROC_GOTD, -1,
419 &irepo, sizeof(irepo)) == -1) {
420 err = got_error_from_errno("imsg compose INFO_REPO");
421 if (err)
422 return err;
425 return NULL;
428 static const struct got_error *
429 send_capability(struct gotd_client_capability *capa, struct gotd_imsgev* iev)
431 const struct got_error *err = NULL;
432 struct gotd_imsg_capability icapa;
433 size_t len;
434 struct ibuf *wbuf;
436 memset(&icapa, 0, sizeof(icapa));
438 icapa.key_len = strlen(capa->key);
439 len = sizeof(icapa) + icapa.key_len;
440 if (capa->value) {
441 icapa.value_len = strlen(capa->value);
442 len += icapa.value_len;
445 wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_CAPABILITY, 0, 0, len);
446 if (wbuf == NULL) {
447 err = got_error_from_errno("imsg_create CAPABILITY");
448 return err;
451 if (imsg_add(wbuf, &icapa, sizeof(icapa)) == -1)
452 return got_error_from_errno("imsg_add CAPABILITY");
453 if (imsg_add(wbuf, capa->key, icapa.key_len) == -1)
454 return got_error_from_errno("imsg_add CAPABILITY");
455 if (capa->value) {
456 if (imsg_add(wbuf, capa->value, icapa.value_len) == -1)
457 return got_error_from_errno("imsg_add CAPABILITY");
460 wbuf->fd = -1;
461 imsg_close(&iev->ibuf, wbuf);
463 gotd_imsg_event_add(iev);
465 return NULL;
468 static const struct got_error *
469 send_client_info(struct gotd_imsgev *iev, struct gotd_client *client)
471 const struct got_error *err = NULL;
472 struct gotd_imsg_info_client iclient;
473 struct gotd_child_proc *proc;
474 size_t i;
476 memset(&iclient, 0, sizeof(iclient));
477 iclient.euid = client->euid;
478 iclient.egid = client->egid;
480 proc = get_client_repo_proc(client);
481 if (proc) {
482 if (strlcpy(iclient.repo_name, proc->repo_path,
483 sizeof(iclient.repo_name)) >= sizeof(iclient.repo_name)) {
484 return got_error_msg(GOT_ERR_NO_SPACE,
485 "repo name too long");
487 if (client_is_writing(client))
488 iclient.is_writing = 1;
490 iclient.repo_child_pid = proc->pid;
493 iclient.state = client->state;
494 if (client->session)
495 iclient.session_child_pid = client->session->pid;
496 iclient.ncapabilities = client->ncapabilities;
498 if (gotd_imsg_compose_event(iev, GOTD_IMSG_INFO_CLIENT, PROC_GOTD, -1,
499 &iclient, sizeof(iclient)) == -1) {
500 err = got_error_from_errno("imsg compose INFO_CLIENT");
501 if (err)
502 return err;
505 for (i = 0; i < client->ncapabilities; i++) {
506 struct gotd_client_capability *capa;
507 capa = &client->capabilities[i];
508 err = send_capability(capa, iev);
509 if (err)
510 return err;
513 return NULL;
516 static const struct got_error *
517 send_info(struct gotd_client *client)
519 const struct got_error *err = NULL;
520 struct gotd_imsg_info info;
521 uint64_t slot;
522 struct gotd_repo *repo;
524 if (client->euid != 0)
525 return got_error_set_errno(EPERM, "info");
527 info.pid = gotd.pid;
528 info.verbosity = gotd.verbosity;
529 info.nrepos = gotd.nrepos;
530 info.nclients = client_cnt - 1;
532 if (gotd_imsg_compose_event(&client->iev, GOTD_IMSG_INFO, PROC_GOTD, -1,
533 &info, sizeof(info)) == -1) {
534 err = got_error_from_errno("imsg compose INFO");
535 if (err)
536 return err;
539 TAILQ_FOREACH(repo, &gotd.repos, entry) {
540 err = send_repo_info(&client->iev, repo);
541 if (err)
542 return err;
545 for (slot = 0; slot < nitems(gotd_clients); slot++) {
546 struct gotd_client *c;
547 STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
548 if (c->id == client->id)
549 continue;
550 err = send_client_info(&client->iev, c);
551 if (err)
552 return err;
556 return NULL;
559 static const struct got_error *
560 stop_gotd(struct gotd_client *client)
563 if (client->euid != 0)
564 return got_error_set_errno(EPERM, "stop");
566 gotd_shutdown();
567 /* NOTREACHED */
568 return NULL;
571 static struct gotd_repo *
572 find_repo_by_name(const char *repo_name)
574 struct gotd_repo *repo;
575 size_t namelen;
577 TAILQ_FOREACH(repo, &gotd.repos, entry) {
578 namelen = strlen(repo->name);
579 if (strncmp(repo->name, repo_name, namelen) != 0)
580 continue;
581 if (repo_name[namelen] == '\0' ||
582 strcmp(&repo_name[namelen], ".git") == 0)
583 return repo;
586 return NULL;
589 static const struct got_error *
590 start_client_authentication(struct gotd_client *client, struct imsg *imsg)
592 const struct got_error *err;
593 struct gotd_imsg_list_refs ireq;
594 struct gotd_repo *repo = NULL;
595 size_t datalen;
597 log_debug("list-refs request from uid %d", client->euid);
599 if (client->state != GOTD_STATE_EXPECT_LIST_REFS)
600 return got_error_msg(GOT_ERR_BAD_REQUEST,
601 "unexpected list-refs request received");
603 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
604 if (datalen != sizeof(ireq))
605 return got_error(GOT_ERR_PRIVSEP_LEN);
607 memcpy(&ireq, imsg->data, datalen);
609 if (ireq.client_is_reading) {
610 err = ensure_client_is_not_writing(client);
611 if (err)
612 return err;
613 repo = find_repo_by_name(ireq.repo_name);
614 if (repo == NULL)
615 return got_error(GOT_ERR_NOT_GIT_REPO);
616 err = start_auth_child(client, GOTD_AUTH_READ, repo,
617 gotd.argv0, gotd.confpath, gotd.daemonize,
618 gotd.verbosity);
619 if (err)
620 return err;
621 } else {
622 err = ensure_client_is_not_reading(client);
623 if (err)
624 return err;
625 repo = find_repo_by_name(ireq.repo_name);
626 if (repo == NULL)
627 return got_error(GOT_ERR_NOT_GIT_REPO);
628 err = start_auth_child(client,
629 GOTD_AUTH_READ | GOTD_AUTH_WRITE,
630 repo, gotd.argv0, gotd.confpath, gotd.daemonize,
631 gotd.verbosity);
632 if (err)
633 return err;
636 evtimer_add(&client->tmo, &auth_timeout);
638 /* Flow continues upon authentication successs/failure or timeout. */
639 return NULL;
642 static void
643 gotd_request(int fd, short events, void *arg)
645 struct gotd_imsgev *iev = arg;
646 struct imsgbuf *ibuf = &iev->ibuf;
647 struct gotd_client *client = iev->handler_arg;
648 const struct got_error *err = NULL;
649 struct imsg imsg;
650 ssize_t n;
652 if (events & EV_WRITE) {
653 while (ibuf->w.queued) {
654 n = msgbuf_write(&ibuf->w);
655 if (n == -1 && errno == EPIPE) {
656 /*
657 * The client has closed its socket.
658 * This can happen when Git clients are
659 * done sending pack file data.
660 */
661 msgbuf_clear(&ibuf->w);
662 continue;
663 } else if (n == -1 && errno != EAGAIN) {
664 err = got_error_from_errno("imsg_flush");
665 disconnect_on_error(client, err);
666 return;
668 if (n == 0) {
669 /* Connection closed. */
670 err = got_error(GOT_ERR_EOF);
671 disconnect_on_error(client, err);
672 return;
676 /* Disconnect gotctl(8) now that messages have been sent. */
677 if (!client_is_reading(client) && !client_is_writing(client)) {
678 disconnect(client);
679 return;
683 if ((events & EV_READ) == 0)
684 return;
686 memset(&imsg, 0, sizeof(imsg));
688 while (err == NULL) {
689 err = gotd_imsg_recv(&imsg, ibuf, 0);
690 if (err) {
691 if (err->code == GOT_ERR_PRIVSEP_READ)
692 err = NULL;
693 break;
696 evtimer_del(&client->tmo);
698 switch (imsg.hdr.type) {
699 case GOTD_IMSG_INFO:
700 err = send_info(client);
701 break;
702 case GOTD_IMSG_STOP:
703 err = stop_gotd(client);
704 break;
705 case GOTD_IMSG_LIST_REFS:
706 err = start_client_authentication(client, &imsg);
707 break;
708 default:
709 log_debug("unexpected imsg %d", imsg.hdr.type);
710 err = got_error(GOT_ERR_PRIVSEP_MSG);
711 break;
714 imsg_free(&imsg);
717 if (err) {
718 if (err->code != GOT_ERR_EOF ||
719 client->state != GOTD_STATE_EXPECT_PACKFILE)
720 disconnect_on_error(client, err);
721 } else {
722 gotd_imsg_event_add(&client->iev);
726 static void
727 gotd_auth_timeout(int fd, short events, void *arg)
729 struct gotd_client *client = arg;
731 log_debug("disconnecting uid %d due to authentication timeout",
732 client->euid);
733 disconnect(client);
736 static const struct got_error *
737 recv_connect(uint32_t *client_id, struct imsg *imsg)
739 const struct got_error *err = NULL;
740 struct gotd_imsg_connect iconnect;
741 size_t datalen;
742 int s = -1;
743 struct gotd_client *client = NULL;
745 *client_id = 0;
747 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
748 if (datalen != sizeof(iconnect))
749 return got_error(GOT_ERR_PRIVSEP_LEN);
750 memcpy(&iconnect, imsg->data, sizeof(iconnect));
752 s = imsg->fd;
753 if (s == -1) {
754 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
755 goto done;
758 if (find_client(iconnect.client_id)) {
759 err = got_error_msg(GOT_ERR_CLIENT_ID, "duplicate client ID");
760 goto done;
763 client = calloc(1, sizeof(*client));
764 if (client == NULL) {
765 err = got_error_from_errno("calloc");
766 goto done;
769 *client_id = iconnect.client_id;
771 client->state = GOTD_STATE_EXPECT_LIST_REFS;
772 client->id = iconnect.client_id;
773 client->fd = s;
774 s = -1;
775 /* The auth process will verify UID/GID for us. */
776 client->euid = iconnect.euid;
777 client->egid = iconnect.egid;
779 imsg_init(&client->iev.ibuf, client->fd);
780 client->iev.handler = gotd_request;
781 client->iev.events = EV_READ;
782 client->iev.handler_arg = client;
784 event_set(&client->iev.ev, client->fd, EV_READ, gotd_request,
785 &client->iev);
786 gotd_imsg_event_add(&client->iev);
788 evtimer_set(&client->tmo, gotd_auth_timeout, client);
790 add_client(client);
791 log_debug("%s: new client uid %d connected on fd %d", __func__,
792 client->euid, client->fd);
793 done:
794 if (err) {
795 struct gotd_child_proc *listen_proc = &gotd.listen_proc;
796 struct gotd_imsg_disconnect idisconnect;
798 idisconnect.client_id = client->id;
799 if (gotd_imsg_compose_event(&listen_proc->iev,
800 GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
801 &idisconnect, sizeof(idisconnect)) == -1)
802 log_warn("imsg compose DISCONNECT");
804 if (s != -1)
805 close(s);
808 return err;
811 static const char *gotd_proc_names[PROC_MAX] = {
812 "parent",
813 "listen",
814 "auth",
815 "session",
816 "repo_read",
817 "repo_write"
818 };
820 static void
821 kill_proc(struct gotd_child_proc *proc, int fatal)
823 if (fatal) {
824 log_warnx("sending SIGKILL to PID %d", proc->pid);
825 kill(proc->pid, SIGKILL);
826 } else
827 kill(proc->pid, SIGTERM);
830 static void
831 gotd_shutdown(void)
833 struct gotd_child_proc *proc;
834 uint64_t slot;
836 log_debug("shutting down");
837 for (slot = 0; slot < nitems(gotd_clients); slot++) {
838 struct gotd_client *c, *tmp;
840 STAILQ_FOREACH_SAFE(c, &gotd_clients[slot], entry, tmp)
841 disconnect(c);
844 proc = &gotd.listen_proc;
845 msgbuf_clear(&proc->iev.ibuf.w);
846 close(proc->iev.ibuf.fd);
847 kill_proc(proc, 0);
848 wait_for_child(proc->pid);
850 log_info("terminating");
851 exit(0);
854 void
855 gotd_sighdlr(int sig, short event, void *arg)
857 /*
858 * Normal signal handler rules don't apply because libevent
859 * decouples for us.
860 */
862 switch (sig) {
863 case SIGHUP:
864 log_info("%s: ignoring SIGHUP", __func__);
865 break;
866 case SIGUSR1:
867 log_info("%s: ignoring SIGUSR1", __func__);
868 break;
869 case SIGTERM:
870 case SIGINT:
871 gotd_shutdown();
872 break;
873 default:
874 fatalx("unexpected signal");
878 static const struct got_error *
879 ensure_proc_is_reading(struct gotd_client *client,
880 struct gotd_child_proc *proc)
882 if (!client_is_reading(client)) {
883 kill_proc(proc, 1);
884 return got_error_fmt(GOT_ERR_BAD_PACKET,
885 "PID %d handled a read-request for uid %d but this "
886 "user is not reading from a repository", proc->pid,
887 client->euid);
890 return NULL;
893 static const struct got_error *
894 ensure_proc_is_writing(struct gotd_client *client,
895 struct gotd_child_proc *proc)
897 if (!client_is_writing(client)) {
898 kill_proc(proc, 1);
899 return got_error_fmt(GOT_ERR_BAD_PACKET,
900 "PID %d handled a write-request for uid %d but this "
901 "user is not writing to a repository", proc->pid,
902 client->euid);
905 return NULL;
908 static int
909 verify_imsg_src(struct gotd_client *client, struct gotd_child_proc *proc,
910 struct imsg *imsg)
912 const struct got_error *err;
913 struct gotd_child_proc *client_proc;
914 int ret = 0;
916 if (proc->type == PROC_REPO_READ || proc->type == PROC_REPO_WRITE) {
917 client_proc = get_client_repo_proc(client);
918 if (client_proc == NULL)
919 fatalx("no process found for uid %d", client->euid);
920 if (proc->pid != client_proc->pid) {
921 kill_proc(proc, 1);
922 log_warnx("received message from PID %d for uid %d, "
923 "while PID %d is the process serving this user",
924 proc->pid, client->euid, client_proc->pid);
925 return 0;
928 if (proc->type == PROC_SESSION) {
929 if (client->session == NULL) {
930 log_warnx("no session found for uid %d", client->euid);
931 return 0;
933 if (proc->pid != client->session->pid) {
934 kill_proc(proc, 1);
935 log_warnx("received message from PID %d for uid %d, "
936 "while PID %d is the process serving this user",
937 proc->pid, client->euid, client->session->pid);
938 return 0;
942 switch (imsg->hdr.type) {
943 case GOTD_IMSG_ERROR:
944 ret = 1;
945 break;
946 case GOTD_IMSG_CONNECT:
947 if (proc->type != PROC_LISTEN) {
948 err = got_error_fmt(GOT_ERR_BAD_PACKET,
949 "new connection for uid %d from PID %d "
950 "which is not the listen process",
951 proc->pid, client->euid);
952 } else
953 ret = 1;
954 break;
955 case GOTD_IMSG_ACCESS_GRANTED:
956 if (proc->type != PROC_AUTH) {
957 err = got_error_fmt(GOT_ERR_BAD_PACKET,
958 "authentication of uid %d from PID %d "
959 "which is not the auth process",
960 proc->pid, client->euid);
961 } else
962 ret = 1;
963 break;
964 case GOTD_IMSG_CLIENT_SESSION_READY:
965 if (proc->type != PROC_SESSION) {
966 err = got_error_fmt(GOT_ERR_BAD_PACKET,
967 "unexpected \"ready\" signal from PID %d",
968 proc->pid);
969 } else
970 ret = 1;
971 break;
972 case GOTD_IMSG_REPO_CHILD_READY:
973 if (proc->type != PROC_REPO_READ &&
974 proc->type != PROC_REPO_WRITE) {
975 err = got_error_fmt(GOT_ERR_BAD_PACKET,
976 "unexpected \"ready\" signal from PID %d",
977 proc->pid);
978 } else
979 ret = 1;
980 break;
981 case GOTD_IMSG_PACKFILE_DONE:
982 err = ensure_proc_is_reading(client, proc);
983 if (err)
984 log_warnx("uid %d: %s", client->euid, err->msg);
985 else
986 ret = 1;
987 break;
988 case GOTD_IMSG_PACKFILE_INSTALL:
989 case GOTD_IMSG_REF_UPDATES_START:
990 case GOTD_IMSG_REF_UPDATE:
991 err = ensure_proc_is_writing(client, proc);
992 if (err)
993 log_warnx("uid %d: %s", client->euid, err->msg);
994 else
995 ret = 1;
996 break;
997 default:
998 log_debug("%s: unexpected imsg %d", __func__, imsg->hdr.type);
999 break;
1002 return ret;
1005 static const struct got_error *
1006 connect_repo_child(struct gotd_client *client,
1007 struct gotd_child_proc *repo_proc)
1009 static const struct got_error *err;
1010 struct gotd_imsgev *session_iev = &client->session->iev;
1011 struct gotd_imsg_connect_repo_child ireq;
1012 int pipe[2];
1014 if (client->state != GOTD_STATE_EXPECT_LIST_REFS)
1015 return got_error_msg(GOT_ERR_BAD_REQUEST,
1016 "unexpected repo child ready signal received");
1018 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1019 PF_UNSPEC, pipe) == -1)
1020 fatal("socketpair");
1022 memset(&ireq, 0, sizeof(ireq));
1023 ireq.client_id = client->id;
1024 ireq.proc_id = repo_proc->type;
1026 /* Pass repo child pipe to session child process. */
1027 if (gotd_imsg_compose_event(session_iev, GOTD_IMSG_CONNECT_REPO_CHILD,
1028 PROC_GOTD, pipe[0], &ireq, sizeof(ireq)) == -1) {
1029 err = got_error_from_errno("imsg compose CONNECT_REPO_CHILD");
1030 close(pipe[0]);
1031 close(pipe[1]);
1032 return err;
1035 /* Pass session child pipe to repo child process. */
1036 if (gotd_imsg_compose_event(&repo_proc->iev,
1037 GOTD_IMSG_CONNECT_REPO_CHILD, PROC_GOTD, pipe[1], NULL, 0) == -1) {
1038 err = got_error_from_errno("imsg compose CONNECT_REPO_CHILD");
1039 close(pipe[1]);
1040 return err;
1043 return NULL;
1046 static void
1047 gotd_dispatch_listener(int fd, short event, void *arg)
1049 struct gotd_imsgev *iev = arg;
1050 struct imsgbuf *ibuf = &iev->ibuf;
1051 struct gotd_child_proc *proc = &gotd.listen_proc;
1052 ssize_t n;
1053 int shut = 0;
1054 struct imsg imsg;
1056 if (proc->iev.ibuf.fd != fd)
1057 fatalx("%s: unexpected fd %d", __func__, fd);
1059 if (event & EV_READ) {
1060 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1061 fatal("imsg_read error");
1062 if (n == 0) {
1063 /* Connection closed. */
1064 shut = 1;
1065 goto done;
1069 if (event & EV_WRITE) {
1070 n = msgbuf_write(&ibuf->w);
1071 if (n == -1 && errno != EAGAIN)
1072 fatal("msgbuf_write");
1073 if (n == 0) {
1074 /* Connection closed. */
1075 shut = 1;
1076 goto done;
1080 for (;;) {
1081 const struct got_error *err = NULL;
1082 struct gotd_client *client = NULL;
1083 uint32_t client_id = 0;
1084 int do_disconnect = 0;
1086 if ((n = imsg_get(ibuf, &imsg)) == -1)
1087 fatal("%s: imsg_get error", __func__);
1088 if (n == 0) /* No more messages. */
1089 break;
1091 switch (imsg.hdr.type) {
1092 case GOTD_IMSG_ERROR:
1093 do_disconnect = 1;
1094 err = gotd_imsg_recv_error(&client_id, &imsg);
1095 break;
1096 case GOTD_IMSG_CONNECT:
1097 err = recv_connect(&client_id, &imsg);
1098 break;
1099 default:
1100 log_debug("unexpected imsg %d", imsg.hdr.type);
1101 break;
1104 client = find_client(client_id);
1105 if (client == NULL) {
1106 log_warnx("%s: client not found", __func__);
1107 imsg_free(&imsg);
1108 continue;
1111 if (err)
1112 log_warnx("uid %d: %s", client->euid, err->msg);
1114 if (do_disconnect) {
1115 if (err)
1116 disconnect_on_error(client, err);
1117 else
1118 disconnect(client);
1121 imsg_free(&imsg);
1123 done:
1124 if (!shut) {
1125 gotd_imsg_event_add(iev);
1126 } else {
1127 /* This pipe is dead. Remove its event handler */
1128 event_del(&iev->ev);
1129 event_loopexit(NULL);
1133 static void
1134 gotd_dispatch_auth_child(int fd, short event, void *arg)
1136 const struct got_error *err = NULL;
1137 struct gotd_imsgev *iev = arg;
1138 struct imsgbuf *ibuf = &iev->ibuf;
1139 struct gotd_client *client;
1140 struct gotd_repo *repo = NULL;
1141 ssize_t n;
1142 int shut = 0;
1143 struct imsg imsg;
1144 uint32_t client_id = 0;
1145 int do_disconnect = 0;
1147 client = find_client_by_proc_fd(fd);
1148 if (client == NULL)
1149 fatalx("cannot find client for fd %d", fd);
1151 if (client->auth == NULL)
1152 fatalx("cannot find auth child process for fd %d", fd);
1154 if (event & EV_READ) {
1155 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1156 fatal("imsg_read error");
1157 if (n == 0) {
1158 /* Connection closed. */
1159 shut = 1;
1160 goto done;
1164 if (event & EV_WRITE) {
1165 n = msgbuf_write(&ibuf->w);
1166 if (n == -1 && errno != EAGAIN)
1167 fatal("msgbuf_write");
1168 if (n == 0) {
1169 /* Connection closed. */
1170 shut = 1;
1172 goto done;
1175 if (client->auth->iev.ibuf.fd != fd)
1176 fatalx("%s: unexpected fd %d", __func__, fd);
1178 if ((n = imsg_get(ibuf, &imsg)) == -1)
1179 fatal("%s: imsg_get error", __func__);
1180 if (n == 0) /* No more messages. */
1181 return;
1183 evtimer_del(&client->tmo);
1185 switch (imsg.hdr.type) {
1186 case GOTD_IMSG_ERROR:
1187 do_disconnect = 1;
1188 err = gotd_imsg_recv_error(&client_id, &imsg);
1189 break;
1190 case GOTD_IMSG_ACCESS_GRANTED:
1191 break;
1192 default:
1193 do_disconnect = 1;
1194 log_debug("unexpected imsg %d", imsg.hdr.type);
1195 break;
1198 if (!verify_imsg_src(client, client->auth, &imsg)) {
1199 do_disconnect = 1;
1200 log_debug("dropping imsg type %d from PID %d",
1201 imsg.hdr.type, client->auth->pid);
1203 imsg_free(&imsg);
1205 if (do_disconnect) {
1206 if (err)
1207 disconnect_on_error(client, err);
1208 else
1209 disconnect(client);
1210 goto done;
1213 repo = find_repo_by_name(client->auth->repo_name);
1214 if (repo == NULL) {
1215 err = got_error(GOT_ERR_NOT_GIT_REPO);
1216 goto done;
1218 kill_auth_proc(client);
1220 log_info("authenticated uid %d for repository %s\n",
1221 client->euid, repo->name);
1223 err = start_session_child(client, repo, gotd.argv0,
1224 gotd.confpath, gotd.daemonize, gotd.verbosity);
1225 if (err)
1226 goto done;
1227 done:
1228 if (err)
1229 log_warnx("uid %d: %s", client->euid, err->msg);
1231 /* We might have killed the auth process by now. */
1232 if (client->auth != NULL) {
1233 if (!shut) {
1234 gotd_imsg_event_add(iev);
1235 } else {
1236 /* This pipe is dead. Remove its event handler */
1237 event_del(&iev->ev);
1242 static const struct got_error *
1243 connect_session(struct gotd_client *client)
1245 const struct got_error *err = NULL;
1246 struct gotd_imsg_connect iconnect;
1247 int s;
1249 memset(&iconnect, 0, sizeof(iconnect));
1251 s = dup(client->fd);
1252 if (s == -1)
1253 return got_error_from_errno("dup");
1255 iconnect.client_id = client->id;
1256 iconnect.euid = client->euid;
1257 iconnect.egid = client->egid;
1259 if (gotd_imsg_compose_event(&client->session->iev, GOTD_IMSG_CONNECT,
1260 PROC_GOTD, s, &iconnect, sizeof(iconnect)) == -1) {
1261 err = got_error_from_errno("imsg compose CONNECT");
1262 close(s);
1263 return err;
1267 * We are no longer interested in messages from this client.
1268 * Further client requests will be handled by the session process.
1270 msgbuf_clear(&client->iev.ibuf.w);
1271 imsg_clear(&client->iev.ibuf);
1272 event_del(&client->iev.ev);
1273 client->fd = -1; /* will be closed via copy in client->iev.ibuf.fd */
1275 return NULL;
1278 static void
1279 gotd_dispatch_client_session(int fd, short event, void *arg)
1281 struct gotd_imsgev *iev = arg;
1282 struct imsgbuf *ibuf = &iev->ibuf;
1283 struct gotd_child_proc *proc = NULL;
1284 struct gotd_client *client = NULL;
1285 ssize_t n;
1286 int shut = 0;
1287 struct imsg imsg;
1289 client = find_client_by_proc_fd(fd);
1290 if (client == NULL)
1291 fatalx("cannot find client for fd %d", fd);
1293 if (event & EV_READ) {
1294 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1295 fatal("imsg_read error");
1296 if (n == 0) {
1297 /* Connection closed. */
1298 shut = 1;
1299 goto done;
1303 if (event & EV_WRITE) {
1304 n = msgbuf_write(&ibuf->w);
1305 if (n == -1 && errno != EAGAIN)
1306 fatal("msgbuf_write");
1307 if (n == 0) {
1308 /* Connection closed. */
1309 shut = 1;
1310 goto done;
1314 proc = client->session;
1315 if (proc == NULL)
1316 fatalx("cannot find session child process for fd %d", fd);
1318 for (;;) {
1319 const struct got_error *err = NULL;
1320 uint32_t client_id = 0;
1321 int do_disconnect = 0, do_start_repo_child = 0;
1323 if ((n = imsg_get(ibuf, &imsg)) == -1)
1324 fatal("%s: imsg_get error", __func__);
1325 if (n == 0) /* No more messages. */
1326 break;
1328 switch (imsg.hdr.type) {
1329 case GOTD_IMSG_ERROR:
1330 do_disconnect = 1;
1331 err = gotd_imsg_recv_error(&client_id, &imsg);
1332 break;
1333 case GOTD_IMSG_CLIENT_SESSION_READY:
1334 if (client->state != GOTD_STATE_EXPECT_LIST_REFS) {
1335 err = got_error(GOT_ERR_PRIVSEP_MSG);
1336 break;
1338 do_start_repo_child = 1;
1339 break;
1340 case GOTD_IMSG_DISCONNECT:
1341 do_disconnect = 1;
1342 break;
1343 default:
1344 log_debug("unexpected imsg %d", imsg.hdr.type);
1345 break;
1348 if (!verify_imsg_src(client, proc, &imsg)) {
1349 log_debug("dropping imsg type %d from PID %d",
1350 imsg.hdr.type, proc->pid);
1351 imsg_free(&imsg);
1352 continue;
1354 if (err)
1355 log_warnx("uid %d: %s", client->euid, err->msg);
1357 if (do_start_repo_child) {
1358 struct gotd_repo *repo;
1360 repo = find_repo_by_name(client->session->repo_name);
1361 if (repo != NULL) {
1362 enum gotd_procid proc_type;
1364 if (client->required_auth & GOTD_AUTH_WRITE)
1365 proc_type = PROC_REPO_WRITE;
1366 else
1367 proc_type = PROC_REPO_READ;
1369 err = start_repo_child(client, proc_type, repo,
1370 gotd.argv0, gotd.confpath, gotd.daemonize,
1371 gotd.verbosity);
1372 } else
1373 err = got_error(GOT_ERR_NOT_GIT_REPO);
1375 if (err) {
1376 log_warnx("uid %d: %s", client->euid, err->msg);
1377 do_disconnect = 1;
1381 if (do_disconnect) {
1382 if (err)
1383 disconnect_on_error(client, err);
1384 else
1385 disconnect(client);
1388 imsg_free(&imsg);
1390 done:
1391 if (!shut) {
1392 gotd_imsg_event_add(iev);
1393 } else {
1394 /* This pipe is dead. Remove its event handler */
1395 event_del(&iev->ev);
1396 disconnect(client);
1400 static void
1401 gotd_dispatch_repo_child(int fd, short event, void *arg)
1403 struct gotd_imsgev *iev = arg;
1404 struct imsgbuf *ibuf = &iev->ibuf;
1405 struct gotd_child_proc *proc = NULL;
1406 struct gotd_client *client;
1407 ssize_t n;
1408 int shut = 0;
1409 struct imsg imsg;
1411 client = find_client_by_proc_fd(fd);
1412 if (client == NULL)
1413 fatalx("cannot find client for fd %d", fd);
1415 if (event & EV_READ) {
1416 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1417 fatal("imsg_read error");
1418 if (n == 0) {
1419 /* Connection closed. */
1420 shut = 1;
1421 goto done;
1425 if (event & EV_WRITE) {
1426 n = msgbuf_write(&ibuf->w);
1427 if (n == -1 && errno != EAGAIN)
1428 fatal("msgbuf_write");
1429 if (n == 0) {
1430 /* Connection closed. */
1431 shut = 1;
1432 goto done;
1436 proc = get_client_repo_proc(client);
1437 if (proc == NULL)
1438 fatalx("cannot find child process for fd %d", fd);
1440 for (;;) {
1441 const struct got_error *err = NULL;
1442 uint32_t client_id = 0;
1443 int do_disconnect = 0;
1445 if ((n = imsg_get(ibuf, &imsg)) == -1)
1446 fatal("%s: imsg_get error", __func__);
1447 if (n == 0) /* No more messages. */
1448 break;
1450 switch (imsg.hdr.type) {
1451 case GOTD_IMSG_ERROR:
1452 do_disconnect = 1;
1453 err = gotd_imsg_recv_error(&client_id, &imsg);
1454 break;
1455 case GOTD_IMSG_REPO_CHILD_READY:
1456 err = connect_session(client);
1457 if (err)
1458 break;
1459 err = connect_repo_child(client, proc);
1460 break;
1461 default:
1462 log_debug("unexpected imsg %d", imsg.hdr.type);
1463 break;
1466 if (!verify_imsg_src(client, proc, &imsg)) {
1467 log_debug("dropping imsg type %d from PID %d",
1468 imsg.hdr.type, proc->pid);
1469 imsg_free(&imsg);
1470 continue;
1472 if (err)
1473 log_warnx("uid %d: %s", client->euid, err->msg);
1475 if (do_disconnect) {
1476 if (err)
1477 disconnect_on_error(client, err);
1478 else
1479 disconnect(client);
1482 imsg_free(&imsg);
1484 done:
1485 if (!shut) {
1486 gotd_imsg_event_add(iev);
1487 } else {
1488 /* This pipe is dead. Remove its event handler */
1489 event_del(&iev->ev);
1490 disconnect(client);
1494 static pid_t
1495 start_child(enum gotd_procid proc_id, const char *repo_path,
1496 char *argv0, const char *confpath, int fd, int daemonize, int verbosity)
1498 char *argv[11];
1499 int argc = 0;
1500 pid_t pid;
1502 switch (pid = fork()) {
1503 case -1:
1504 fatal("cannot fork");
1505 case 0:
1506 break;
1507 default:
1508 close(fd);
1509 return pid;
1512 if (fd != GOTD_FILENO_MSG_PIPE) {
1513 if (dup2(fd, GOTD_FILENO_MSG_PIPE) == -1)
1514 fatal("cannot setup imsg fd");
1515 } else if (fcntl(fd, F_SETFD, 0) == -1)
1516 fatal("cannot setup imsg fd");
1518 argv[argc++] = argv0;
1519 switch (proc_id) {
1520 case PROC_LISTEN:
1521 argv[argc++] = (char *)"-L";
1522 break;
1523 case PROC_AUTH:
1524 argv[argc++] = (char *)"-A";
1525 break;
1526 case PROC_SESSION:
1527 argv[argc++] = (char *)"-S";
1528 break;
1529 case PROC_REPO_READ:
1530 argv[argc++] = (char *)"-R";
1531 break;
1532 case PROC_REPO_WRITE:
1533 argv[argc++] = (char *)"-W";
1534 break;
1535 default:
1536 fatalx("invalid process id %d", proc_id);
1539 argv[argc++] = (char *)"-f";
1540 argv[argc++] = (char *)confpath;
1542 if (repo_path) {
1543 argv[argc++] = (char *)"-P";
1544 argv[argc++] = (char *)repo_path;
1547 if (!daemonize)
1548 argv[argc++] = (char *)"-d";
1549 if (verbosity > 0)
1550 argv[argc++] = (char *)"-v";
1551 if (verbosity > 1)
1552 argv[argc++] = (char *)"-v";
1553 argv[argc++] = NULL;
1555 execvp(argv0, argv);
1556 fatal("execvp");
1559 static void
1560 start_listener(char *argv0, const char *confpath, int daemonize, int verbosity)
1562 struct gotd_child_proc *proc = &gotd.listen_proc;
1564 proc->type = PROC_LISTEN;
1566 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1567 PF_UNSPEC, proc->pipe) == -1)
1568 fatal("socketpair");
1570 proc->pid = start_child(proc->type, NULL, argv0, confpath,
1571 proc->pipe[1], daemonize, verbosity);
1572 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1573 proc->iev.handler = gotd_dispatch_listener;
1574 proc->iev.events = EV_READ;
1575 proc->iev.handler_arg = NULL;
1578 static const struct got_error *
1579 start_session_child(struct gotd_client *client, struct gotd_repo *repo,
1580 char *argv0, const char *confpath, int daemonize, int verbosity)
1582 struct gotd_child_proc *proc;
1584 proc = calloc(1, sizeof(*proc));
1585 if (proc == NULL)
1586 return got_error_from_errno("calloc");
1588 proc->type = PROC_SESSION;
1589 if (strlcpy(proc->repo_name, repo->name,
1590 sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
1591 fatalx("repository name too long: %s", repo->name);
1592 log_debug("starting client uid %d session for repository %s",
1593 client->euid, repo->name);
1594 if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >=
1595 sizeof(proc->repo_path))
1596 fatalx("repository path too long: %s", repo->path);
1597 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1598 PF_UNSPEC, proc->pipe) == -1)
1599 fatal("socketpair");
1600 proc->pid = start_child(proc->type, proc->repo_path, argv0,
1601 confpath, proc->pipe[1], daemonize, verbosity);
1602 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1603 log_debug("proc %s %s is on fd %d",
1604 gotd_proc_names[proc->type], proc->repo_path,
1605 proc->pipe[0]);
1606 proc->iev.handler = gotd_dispatch_client_session;
1607 proc->iev.events = EV_READ;
1608 proc->iev.handler_arg = NULL;
1609 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1610 gotd_dispatch_client_session, &proc->iev);
1611 gotd_imsg_event_add(&proc->iev);
1613 client->session = proc;
1614 return NULL;
1617 static const struct got_error *
1618 start_repo_child(struct gotd_client *client, enum gotd_procid proc_type,
1619 struct gotd_repo *repo, char *argv0, const char *confpath,
1620 int daemonize, int verbosity)
1622 struct gotd_child_proc *proc;
1624 if (proc_type != PROC_REPO_READ && proc_type != PROC_REPO_WRITE)
1625 return got_error_msg(GOT_ERR_NOT_IMPL, "bad process type");
1627 proc = calloc(1, sizeof(*proc));
1628 if (proc == NULL)
1629 return got_error_from_errno("calloc");
1631 proc->type = proc_type;
1632 if (strlcpy(proc->repo_name, repo->name,
1633 sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
1634 fatalx("repository name too long: %s", repo->name);
1635 log_debug("starting %s for repository %s",
1636 proc->type == PROC_REPO_READ ? "reader" : "writer", repo->name);
1637 if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >=
1638 sizeof(proc->repo_path))
1639 fatalx("repository path too long: %s", repo->path);
1640 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1641 PF_UNSPEC, proc->pipe) == -1)
1642 fatal("socketpair");
1643 proc->pid = start_child(proc->type, proc->repo_path, argv0,
1644 confpath, proc->pipe[1], daemonize, verbosity);
1645 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1646 log_debug("proc %s %s is on fd %d",
1647 gotd_proc_names[proc->type], proc->repo_path,
1648 proc->pipe[0]);
1649 proc->iev.handler = gotd_dispatch_repo_child;
1650 proc->iev.events = EV_READ;
1651 proc->iev.handler_arg = NULL;
1652 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1653 gotd_dispatch_repo_child, &proc->iev);
1654 gotd_imsg_event_add(&proc->iev);
1656 if (proc->type == PROC_REPO_READ)
1657 client->repo_read = proc;
1658 else
1659 client->repo_write = proc;
1661 return NULL;
1664 static const struct got_error *
1665 start_auth_child(struct gotd_client *client, int required_auth,
1666 struct gotd_repo *repo, char *argv0, const char *confpath,
1667 int daemonize, int verbosity)
1669 const struct got_error *err = NULL;
1670 struct gotd_child_proc *proc;
1671 struct gotd_imsg_auth iauth;
1672 int fd;
1674 memset(&iauth, 0, sizeof(iauth));
1676 fd = dup(client->fd);
1677 if (fd == -1)
1678 return got_error_from_errno("dup");
1680 proc = calloc(1, sizeof(*proc));
1681 if (proc == NULL) {
1682 err = got_error_from_errno("calloc");
1683 close(fd);
1684 return err;
1687 proc->type = PROC_AUTH;
1688 if (strlcpy(proc->repo_name, repo->name,
1689 sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
1690 fatalx("repository name too long: %s", repo->name);
1691 log_debug("starting auth for uid %d repository %s",
1692 client->euid, repo->name);
1693 if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >=
1694 sizeof(proc->repo_path))
1695 fatalx("repository path too long: %s", repo->path);
1696 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1697 PF_UNSPEC, proc->pipe) == -1)
1698 fatal("socketpair");
1699 proc->pid = start_child(proc->type, proc->repo_path, argv0,
1700 confpath, proc->pipe[1], daemonize, verbosity);
1701 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1702 log_debug("proc %s %s is on fd %d",
1703 gotd_proc_names[proc->type], proc->repo_path,
1704 proc->pipe[0]);
1705 proc->iev.handler = gotd_dispatch_auth_child;
1706 proc->iev.events = EV_READ;
1707 proc->iev.handler_arg = NULL;
1708 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1709 gotd_dispatch_auth_child, &proc->iev);
1710 gotd_imsg_event_add(&proc->iev);
1712 iauth.euid = client->euid;
1713 iauth.egid = client->egid;
1714 iauth.required_auth = required_auth;
1715 iauth.client_id = client->id;
1716 if (gotd_imsg_compose_event(&proc->iev, GOTD_IMSG_AUTHENTICATE,
1717 PROC_GOTD, fd, &iauth, sizeof(iauth)) == -1) {
1718 log_warn("imsg compose AUTHENTICATE");
1719 close(fd);
1720 /* Let the auth_timeout handler tidy up. */
1723 client->auth = proc;
1724 client->required_auth = required_auth;
1725 return NULL;
1728 static void
1729 apply_unveil_repo_readonly(const char *repo_path)
1731 if (unveil(repo_path, "r") == -1)
1732 fatal("unveil %s", repo_path);
1734 if (unveil(NULL, NULL) == -1)
1735 fatal("unveil");
1738 static void
1739 apply_unveil_repo_readwrite(const char *repo_path)
1741 if (unveil(repo_path, "rwc") == -1)
1742 fatal("unveil %s", repo_path);
1744 if (unveil(GOT_TMPDIR_STR, "rwc") == -1)
1745 fatal("unveil %s", GOT_TMPDIR_STR);
1747 if (unveil(NULL, NULL) == -1)
1748 fatal("unveil");
1751 static void
1752 apply_unveil_none(void)
1754 if (unveil("/", "") == -1)
1755 fatal("unveil");
1757 if (unveil(NULL, NULL) == -1)
1758 fatal("unveil");
1761 static void
1762 apply_unveil_selfexec(void)
1764 if (unveil(gotd.argv0, "x") == -1)
1765 fatal("unveil %s", gotd.argv0);
1767 if (unveil(NULL, NULL) == -1)
1768 fatal("unveil");
1771 int
1772 main(int argc, char **argv)
1774 const struct got_error *error = NULL;
1775 int ch, fd = -1, daemonize = 1, verbosity = 0, noaction = 0;
1776 const char *confpath = GOTD_CONF_PATH;
1777 char *argv0 = argv[0];
1778 char title[2048];
1779 struct passwd *pw = NULL;
1780 char *repo_path = NULL;
1781 enum gotd_procid proc_id = PROC_GOTD;
1782 struct event evsigint, evsigterm, evsighup, evsigusr1;
1783 int *pack_fds = NULL, *temp_fds = NULL;
1785 log_init(1, LOG_DAEMON); /* Log to stderr until daemonized. */
1787 while ((ch = getopt(argc, argv, "Adf:LnP:RSvW")) != -1) {
1788 switch (ch) {
1789 case 'A':
1790 proc_id = PROC_AUTH;
1791 break;
1792 case 'd':
1793 daemonize = 0;
1794 break;
1795 case 'f':
1796 confpath = optarg;
1797 break;
1798 case 'L':
1799 proc_id = PROC_LISTEN;
1800 break;
1801 case 'n':
1802 noaction = 1;
1803 break;
1804 case 'P':
1805 repo_path = realpath(optarg, NULL);
1806 if (repo_path == NULL)
1807 fatal("realpath '%s'", optarg);
1808 break;
1809 case 'R':
1810 proc_id = PROC_REPO_READ;
1811 break;
1812 case 'S':
1813 proc_id = PROC_SESSION;
1814 break;
1815 case 'v':
1816 if (verbosity < 3)
1817 verbosity++;
1818 break;
1819 case 'W':
1820 proc_id = PROC_REPO_WRITE;
1821 break;
1822 default:
1823 usage();
1827 argc -= optind;
1828 argv += optind;
1830 if (argc != 0)
1831 usage();
1833 /* Require an absolute path in argv[0] for reliable re-exec. */
1834 if (!got_path_is_absolute(argv0))
1835 fatalx("bad path \"%s\": must be an absolute path", argv0);
1837 if (geteuid() && (proc_id == PROC_GOTD || proc_id == PROC_LISTEN))
1838 fatalx("need root privileges");
1840 log_init(daemonize ? 0 : 1, LOG_DAEMON);
1841 log_setverbose(verbosity);
1843 if (parse_config(confpath, proc_id, &gotd) != 0)
1844 return 1;
1846 gotd.argv0 = argv0;
1847 gotd.daemonize = daemonize;
1848 gotd.verbosity = verbosity;
1849 gotd.confpath = confpath;
1851 if (proc_id == PROC_GOTD &&
1852 (gotd.nrepos == 0 || TAILQ_EMPTY(&gotd.repos)))
1853 fatalx("no repository defined in configuration file");
1855 pw = getpwnam(gotd.user_name);
1856 if (pw == NULL)
1857 fatalx("user %s not found", gotd.user_name);
1859 if (pw->pw_uid == 0) {
1860 fatalx("cannot run %s as %s: the user running %s "
1861 "must not be the superuser",
1862 getprogname(), pw->pw_name, getprogname());
1865 if (proc_id == PROC_LISTEN &&
1866 !got_path_is_absolute(gotd.unix_socket_path))
1867 fatalx("bad unix socket path \"%s\": must be an absolute path",
1868 gotd.unix_socket_path);
1870 if (noaction)
1871 return 0;
1873 if (proc_id == PROC_GOTD) {
1874 gotd.pid = getpid();
1875 snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
1876 start_listener(argv0, confpath, daemonize, verbosity);
1877 arc4random_buf(&clients_hash_key, sizeof(clients_hash_key));
1878 if (daemonize && daemon(1, 0) == -1)
1879 fatal("daemon");
1880 } else if (proc_id == PROC_LISTEN) {
1881 snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
1882 if (verbosity) {
1883 log_info("socket: %s", gotd.unix_socket_path);
1884 log_info("user: %s", pw->pw_name);
1887 fd = unix_socket_listen(gotd.unix_socket_path, pw->pw_uid,
1888 pw->pw_gid);
1889 if (fd == -1) {
1890 fatal("cannot listen on unix socket %s",
1891 gotd.unix_socket_path);
1893 if (daemonize && daemon(0, 0) == -1)
1894 fatal("daemon");
1895 } else if (proc_id == PROC_AUTH) {
1896 snprintf(title, sizeof(title), "%s %s",
1897 gotd_proc_names[proc_id], repo_path);
1898 if (daemonize && daemon(0, 0) == -1)
1899 fatal("daemon");
1900 } else if (proc_id == PROC_REPO_READ || proc_id == PROC_REPO_WRITE ||
1901 proc_id == PROC_SESSION) {
1902 error = got_repo_pack_fds_open(&pack_fds);
1903 if (error != NULL)
1904 fatalx("cannot open pack tempfiles: %s", error->msg);
1905 error = got_repo_temp_fds_open(&temp_fds);
1906 if (error != NULL)
1907 fatalx("cannot open pack tempfiles: %s", error->msg);
1908 if (repo_path == NULL)
1909 fatalx("repository path not specified");
1910 snprintf(title, sizeof(title), "%s %s",
1911 gotd_proc_names[proc_id], repo_path);
1912 if (daemonize && daemon(0, 0) == -1)
1913 fatal("daemon");
1914 } else
1915 fatal("invalid process id %d", proc_id);
1917 setproctitle("%s", title);
1918 log_procinit(title);
1920 /* Drop root privileges. */
1921 if (setgid(pw->pw_gid) == -1)
1922 fatal("setgid %d failed", pw->pw_gid);
1923 if (setuid(pw->pw_uid) == -1)
1924 fatal("setuid %d failed", pw->pw_uid);
1926 event_init();
1928 switch (proc_id) {
1929 case PROC_GOTD:
1930 #ifndef PROFILE
1931 /* "exec" promise will be limited to argv[0] via unveil(2). */
1932 if (pledge("stdio proc exec sendfd recvfd unveil", NULL) == -1)
1933 err(1, "pledge");
1934 #endif
1935 break;
1936 case PROC_LISTEN:
1937 #ifndef PROFILE
1938 if (pledge("stdio sendfd unix unveil", NULL) == -1)
1939 err(1, "pledge");
1940 #endif
1942 * Ensure that AF_UNIX bind(2) cannot be used with any other
1943 * sockets by revoking all filesystem access via unveil(2).
1945 apply_unveil_none();
1947 listen_main(title, fd, gotd.connection_limits,
1948 gotd.nconnection_limits);
1949 /* NOTREACHED */
1950 break;
1951 case PROC_AUTH:
1952 #ifndef PROFILE
1953 if (pledge("stdio getpw recvfd unix unveil", NULL) == -1)
1954 err(1, "pledge");
1955 #endif
1957 * We need the "unix" pledge promise for getpeername(2) only.
1958 * Ensure that AF_UNIX bind(2) cannot be used by revoking all
1959 * filesystem access via unveil(2). Access to password database
1960 * files will still work since "getpw" bypasses unveil(2).
1962 apply_unveil_none();
1964 auth_main(title, &gotd.repos, repo_path);
1965 /* NOTREACHED */
1966 break;
1967 case PROC_SESSION:
1968 #ifndef PROFILE
1970 * The "recvfd" promise is only needed during setup and
1971 * will be removed in a later pledge(2) call.
1973 if (pledge("stdio rpath wpath cpath recvfd sendfd fattr flock "
1974 "unveil", NULL) == -1)
1975 err(1, "pledge");
1976 #endif
1977 apply_unveil_repo_readwrite(repo_path);
1978 session_main(title, repo_path, pack_fds, temp_fds,
1979 &gotd.request_timeout);
1980 /* NOTREACHED */
1981 break;
1982 case PROC_REPO_READ:
1983 #ifndef PROFILE
1984 if (pledge("stdio rpath recvfd unveil", NULL) == -1)
1985 err(1, "pledge");
1986 #endif
1987 apply_unveil_repo_readonly(repo_path);
1988 repo_read_main(title, repo_path, pack_fds, temp_fds);
1989 /* NOTREACHED */
1990 exit(0);
1991 case PROC_REPO_WRITE:
1992 #ifndef PROFILE
1993 if (pledge("stdio rpath recvfd unveil", NULL) == -1)
1994 err(1, "pledge");
1995 #endif
1996 apply_unveil_repo_readonly(repo_path);
1997 repo_write_main(title, repo_path, pack_fds, temp_fds);
1998 /* NOTREACHED */
1999 exit(0);
2000 default:
2001 fatal("invalid process id %d", proc_id);
2004 if (proc_id != PROC_GOTD)
2005 fatal("invalid process id %d", proc_id);
2007 apply_unveil_selfexec();
2009 signal_set(&evsigint, SIGINT, gotd_sighdlr, NULL);
2010 signal_set(&evsigterm, SIGTERM, gotd_sighdlr, NULL);
2011 signal_set(&evsighup, SIGHUP, gotd_sighdlr, NULL);
2012 signal_set(&evsigusr1, SIGUSR1, gotd_sighdlr, NULL);
2013 signal(SIGPIPE, SIG_IGN);
2015 signal_add(&evsigint, NULL);
2016 signal_add(&evsigterm, NULL);
2017 signal_add(&evsighup, NULL);
2018 signal_add(&evsigusr1, NULL);
2020 gotd_imsg_event_add(&gotd.listen_proc.iev);
2022 event_dispatch();
2024 free(repo_path);
2025 gotd_shutdown();
2027 return 0;