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 "repo_read.h"
63 #include "repo_write.h"
65 #ifndef nitems
66 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
67 #endif
69 struct gotd_client {
70 STAILQ_ENTRY(gotd_client) entry;
71 enum gotd_client_state state;
72 struct gotd_client_capability *capabilities;
73 size_t ncapa_alloc;
74 size_t ncapabilities;
75 uint32_t id;
76 int fd;
77 int delta_cache_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 int required_auth;
86 char *packfile_path;
87 char *packidx_path;
88 int nref_updates;
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_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_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_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;
241 return NULL;
244 static int
245 client_is_reading(struct gotd_client *client)
247 return client->repo_read != NULL;
250 static int
251 client_is_writing(struct gotd_client *client)
253 return client->repo_write != NULL;
256 static const struct got_error *
257 ensure_client_is_reading(struct gotd_client *client)
259 if (!client_is_reading(client)) {
260 return got_error_fmt(GOT_ERR_BAD_PACKET,
261 "uid %d made a read-request but is not reading from "
262 "a repository", client->euid);
265 return NULL;
268 static const struct got_error *
269 ensure_client_is_writing(struct gotd_client *client)
271 if (!client_is_writing(client)) {
272 return got_error_fmt(GOT_ERR_BAD_PACKET,
273 "uid %d made a write-request but is not writing to "
274 "a repository", client->euid);
277 return NULL;
280 static const struct got_error *
281 ensure_client_is_not_writing(struct gotd_client *client)
283 if (client_is_writing(client)) {
284 return got_error_fmt(GOT_ERR_BAD_PACKET,
285 "uid %d made a read-request but is writing to "
286 "a repository", client->euid);
289 return NULL;
292 static const struct got_error *
293 ensure_client_is_not_reading(struct gotd_client *client)
295 if (client_is_reading(client)) {
296 return got_error_fmt(GOT_ERR_BAD_PACKET,
297 "uid %d made a write-request but is reading from "
298 "a repository", client->euid);
301 return NULL;
304 static void
305 wait_for_child(pid_t child_pid)
307 pid_t pid;
308 int status;
310 log_debug("waiting for child PID %ld to terminate",
311 (long)child_pid);
313 do {
314 pid = waitpid(child_pid, &status, WNOHANG);
315 if (pid == -1) {
316 if (errno != EINTR && errno != ECHILD)
317 fatal("wait");
318 } else if (WIFSIGNALED(status)) {
319 log_warnx("child PID %ld terminated; signal %d",
320 (long)pid, WTERMSIG(status));
322 } while (pid != -1 || (pid == -1 && errno == EINTR));
325 static void
326 kill_auth_proc(struct gotd_client *client)
328 struct gotd_child_proc *proc;
330 if (client->auth == NULL)
331 return;
333 proc = client->auth;
334 client->auth = NULL;
336 event_del(&proc->iev.ev);
337 msgbuf_clear(&proc->iev.ibuf.w);
338 close(proc->iev.ibuf.fd);
339 kill_proc(proc, 0);
340 wait_for_child(proc->pid);
341 free(proc);
344 static void
345 disconnect(struct gotd_client *client)
347 struct gotd_imsg_disconnect idisconnect;
348 struct gotd_child_proc *proc = get_client_proc(client);
349 struct gotd_child_proc *listen_proc = &gotd.listen_proc;
350 uint64_t slot;
352 log_debug("uid %d: disconnecting", client->euid);
354 kill_auth_proc(client);
356 idisconnect.client_id = client->id;
357 if (proc) {
358 if (gotd_imsg_compose_event(&proc->iev,
359 GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
360 &idisconnect, sizeof(idisconnect)) == -1)
361 log_warn("imsg compose DISCONNECT");
363 msgbuf_clear(&proc->iev.ibuf.w);
364 close(proc->iev.ibuf.fd);
365 kill_proc(proc, 0);
366 wait_for_child(proc->pid);
367 free(proc);
368 proc = NULL;
371 if (gotd_imsg_compose_event(&listen_proc->iev,
372 GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
373 &idisconnect, sizeof(idisconnect)) == -1)
374 log_warn("imsg compose DISCONNECT");
376 slot = client_hash(client->id) % nitems(gotd_clients);
377 STAILQ_REMOVE(&gotd_clients[slot], client, gotd_client, entry);
378 imsg_clear(&client->iev.ibuf);
379 event_del(&client->iev.ev);
380 evtimer_del(&client->tmo);
381 close(client->fd);
382 if (client->delta_cache_fd != -1)
383 close(client->delta_cache_fd);
384 if (client->packfile_path) {
385 if (unlink(client->packfile_path) == -1 && errno != ENOENT)
386 log_warn("unlink %s: ", client->packfile_path);
387 free(client->packfile_path);
389 if (client->packidx_path) {
390 if (unlink(client->packidx_path) == -1 && errno != ENOENT)
391 log_warn("unlink %s: ", client->packidx_path);
392 free(client->packidx_path);
394 free(client->capabilities);
395 free(client);
396 client_cnt--;
399 static void
400 disconnect_on_error(struct gotd_client *client, const struct got_error *err)
402 struct imsgbuf ibuf;
404 log_warnx("uid %d: %s", client->euid, err->msg);
405 if (err->code != GOT_ERR_EOF) {
406 imsg_init(&ibuf, client->fd);
407 gotd_imsg_send_error(&ibuf, 0, PROC_GOTD, err);
408 imsg_clear(&ibuf);
410 disconnect(client);
413 static const struct got_error *
414 send_repo_info(struct gotd_imsgev *iev, struct gotd_repo *repo)
416 const struct got_error *err = NULL;
417 struct gotd_imsg_info_repo irepo;
419 memset(&irepo, 0, sizeof(irepo));
421 if (strlcpy(irepo.repo_name, repo->name, sizeof(irepo.repo_name))
422 >= sizeof(irepo.repo_name))
423 return got_error_msg(GOT_ERR_NO_SPACE, "repo name too long");
424 if (strlcpy(irepo.repo_path, repo->path, sizeof(irepo.repo_path))
425 >= sizeof(irepo.repo_path))
426 return got_error_msg(GOT_ERR_NO_SPACE, "repo path too long");
428 if (gotd_imsg_compose_event(iev, GOTD_IMSG_INFO_REPO, PROC_GOTD, -1,
429 &irepo, sizeof(irepo)) == -1) {
430 err = got_error_from_errno("imsg compose INFO_REPO");
431 if (err)
432 return err;
435 return NULL;
438 static const struct got_error *
439 send_capability(struct gotd_client_capability *capa, struct gotd_imsgev* iev)
441 const struct got_error *err = NULL;
442 struct gotd_imsg_capability icapa;
443 size_t len;
444 struct ibuf *wbuf;
446 memset(&icapa, 0, sizeof(icapa));
448 icapa.key_len = strlen(capa->key);
449 len = sizeof(icapa) + icapa.key_len;
450 if (capa->value) {
451 icapa.value_len = strlen(capa->value);
452 len += icapa.value_len;
455 wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_CAPABILITY, 0, 0, len);
456 if (wbuf == NULL) {
457 err = got_error_from_errno("imsg_create CAPABILITY");
458 return err;
461 if (imsg_add(wbuf, &icapa, sizeof(icapa)) == -1)
462 return got_error_from_errno("imsg_add CAPABILITY");
463 if (imsg_add(wbuf, capa->key, icapa.key_len) == -1)
464 return got_error_from_errno("imsg_add CAPABILITY");
465 if (capa->value) {
466 if (imsg_add(wbuf, capa->value, icapa.value_len) == -1)
467 return got_error_from_errno("imsg_add CAPABILITY");
470 wbuf->fd = -1;
471 imsg_close(&iev->ibuf, wbuf);
473 gotd_imsg_event_add(iev);
475 return NULL;
478 static const struct got_error *
479 send_client_info(struct gotd_imsgev *iev, struct gotd_client *client)
481 const struct got_error *err = NULL;
482 struct gotd_imsg_info_client iclient;
483 struct gotd_child_proc *proc;
484 size_t i;
486 memset(&iclient, 0, sizeof(iclient));
487 iclient.euid = client->euid;
488 iclient.egid = client->egid;
490 proc = get_client_proc(client);
491 if (proc) {
492 if (strlcpy(iclient.repo_name, proc->repo_path,
493 sizeof(iclient.repo_name)) >= sizeof(iclient.repo_name)) {
494 return got_error_msg(GOT_ERR_NO_SPACE,
495 "repo name too long");
497 if (client_is_writing(client))
498 iclient.is_writing = 1;
501 iclient.state = client->state;
502 iclient.ncapabilities = client->ncapabilities;
504 if (gotd_imsg_compose_event(iev, GOTD_IMSG_INFO_CLIENT, PROC_GOTD, -1,
505 &iclient, sizeof(iclient)) == -1) {
506 err = got_error_from_errno("imsg compose INFO_CLIENT");
507 if (err)
508 return err;
511 for (i = 0; i < client->ncapabilities; i++) {
512 struct gotd_client_capability *capa;
513 capa = &client->capabilities[i];
514 err = send_capability(capa, iev);
515 if (err)
516 return err;
519 return NULL;
522 static const struct got_error *
523 send_info(struct gotd_client *client)
525 const struct got_error *err = NULL;
526 struct gotd_imsg_info info;
527 uint64_t slot;
528 struct gotd_repo *repo;
530 if (client->euid != 0)
531 return got_error_set_errno(EPERM, "info");
533 info.pid = gotd.pid;
534 info.verbosity = gotd.verbosity;
535 info.nrepos = gotd.nrepos;
536 info.nclients = client_cnt - 1;
538 if (gotd_imsg_compose_event(&client->iev, GOTD_IMSG_INFO, PROC_GOTD, -1,
539 &info, sizeof(info)) == -1) {
540 err = got_error_from_errno("imsg compose INFO");
541 if (err)
542 return err;
545 TAILQ_FOREACH(repo, &gotd.repos, entry) {
546 err = send_repo_info(&client->iev, repo);
547 if (err)
548 return err;
551 for (slot = 0; slot < nitems(gotd_clients); slot++) {
552 struct gotd_client *c;
553 STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
554 if (c->id == client->id)
555 continue;
556 err = send_client_info(&client->iev, c);
557 if (err)
558 return err;
562 return NULL;
565 static const struct got_error *
566 stop_gotd(struct gotd_client *client)
569 if (client->euid != 0)
570 return got_error_set_errno(EPERM, "stop");
572 gotd_shutdown();
573 /* NOTREACHED */
574 return NULL;
577 static struct gotd_repo *
578 find_repo_by_name(const char *repo_name)
580 struct gotd_repo *repo;
581 size_t namelen;
583 TAILQ_FOREACH(repo, &gotd.repos, entry) {
584 namelen = strlen(repo->name);
585 if (strncmp(repo->name, repo_name, namelen) != 0)
586 continue;
587 if (repo_name[namelen] == '\0' ||
588 strcmp(&repo_name[namelen], ".git") == 0)
589 return repo;
592 return NULL;
595 static const struct got_error *
596 start_client_session(struct gotd_client *client, struct imsg *imsg)
598 const struct got_error *err;
599 struct gotd_imsg_list_refs ireq;
600 struct gotd_repo *repo = NULL;
601 size_t datalen;
603 log_debug("list-refs request from uid %d", client->euid);
605 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
606 if (datalen != sizeof(ireq))
607 return got_error(GOT_ERR_PRIVSEP_LEN);
609 memcpy(&ireq, imsg->data, datalen);
611 if (ireq.client_is_reading) {
612 err = ensure_client_is_not_writing(client);
613 if (err)
614 return err;
615 repo = find_repo_by_name(ireq.repo_name);
616 if (repo == NULL)
617 return got_error(GOT_ERR_NOT_GIT_REPO);
618 err = start_auth_child(client, GOTD_AUTH_READ, repo,
619 gotd.argv0, gotd.confpath, gotd.daemonize,
620 gotd.verbosity);
621 if (err)
622 return err;
623 } else {
624 err = ensure_client_is_not_reading(client);
625 if (err)
626 return err;
627 repo = find_repo_by_name(ireq.repo_name);
628 if (repo == NULL)
629 return got_error(GOT_ERR_NOT_GIT_REPO);
630 err = start_auth_child(client,
631 GOTD_AUTH_READ | GOTD_AUTH_WRITE,
632 repo, gotd.argv0, gotd.confpath, gotd.daemonize,
633 gotd.verbosity);
634 if (err)
635 return err;
638 /* Flow continues upon authentication successs/failure or timeout. */
639 return NULL;
642 static const struct got_error *
643 forward_want(struct gotd_client *client, struct imsg *imsg)
645 struct gotd_imsg_want ireq;
646 struct gotd_imsg_want iwant;
647 size_t datalen;
649 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
650 if (datalen != sizeof(ireq))
651 return got_error(GOT_ERR_PRIVSEP_LEN);
653 memcpy(&ireq, imsg->data, datalen);
655 memset(&iwant, 0, sizeof(iwant));
656 memcpy(iwant.object_id, ireq.object_id, SHA1_DIGEST_LENGTH);
657 iwant.client_id = client->id;
659 if (gotd_imsg_compose_event(&client->repo_read->iev, GOTD_IMSG_WANT,
660 PROC_GOTD, -1, &iwant, sizeof(iwant)) == -1)
661 return got_error_from_errno("imsg compose WANT");
663 return NULL;
666 static const struct got_error *
667 forward_ref_update(struct gotd_client *client, struct imsg *imsg)
669 const struct got_error *err = NULL;
670 struct gotd_imsg_ref_update ireq;
671 struct gotd_imsg_ref_update *iref = NULL;
672 size_t datalen;
674 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
675 if (datalen < sizeof(ireq))
676 return got_error(GOT_ERR_PRIVSEP_LEN);
677 memcpy(&ireq, imsg->data, sizeof(ireq));
678 if (datalen != sizeof(ireq) + ireq.name_len)
679 return got_error(GOT_ERR_PRIVSEP_LEN);
681 iref = malloc(datalen);
682 if (iref == NULL)
683 return got_error_from_errno("malloc");
684 memcpy(iref, imsg->data, datalen);
686 iref->client_id = client->id;
687 if (gotd_imsg_compose_event(&client->repo_write->iev,
688 GOTD_IMSG_REF_UPDATE, PROC_GOTD, -1, iref, datalen) == -1)
689 err = got_error_from_errno("imsg compose REF_UPDATE");
690 free(iref);
691 return err;
694 static const struct got_error *
695 forward_have(struct gotd_client *client, struct imsg *imsg)
697 struct gotd_imsg_have ireq;
698 struct gotd_imsg_have ihave;
699 size_t datalen;
701 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
702 if (datalen != sizeof(ireq))
703 return got_error(GOT_ERR_PRIVSEP_LEN);
705 memcpy(&ireq, imsg->data, datalen);
707 memset(&ihave, 0, sizeof(ihave));
708 memcpy(ihave.object_id, ireq.object_id, SHA1_DIGEST_LENGTH);
709 ihave.client_id = client->id;
711 if (gotd_imsg_compose_event(&client->repo_read->iev, GOTD_IMSG_HAVE,
712 PROC_GOTD, -1, &ihave, sizeof(ihave)) == -1)
713 return got_error_from_errno("imsg compose HAVE");
715 return NULL;
718 static int
719 client_has_capability(struct gotd_client *client, const char *capastr)
721 struct gotd_client_capability *capa;
722 size_t i;
724 if (client->ncapabilities == 0)
725 return 0;
727 for (i = 0; i < client->ncapabilities; i++) {
728 capa = &client->capabilities[i];
729 if (strcmp(capa->key, capastr) == 0)
730 return 1;
733 return 0;
736 static const struct got_error *
737 recv_capabilities(struct gotd_client *client, struct imsg *imsg)
739 struct gotd_imsg_capabilities icapas;
740 size_t datalen;
742 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
743 if (datalen != sizeof(icapas))
744 return got_error(GOT_ERR_PRIVSEP_LEN);
745 memcpy(&icapas, imsg->data, sizeof(icapas));
747 client->ncapa_alloc = icapas.ncapabilities;
748 client->capabilities = calloc(client->ncapa_alloc,
749 sizeof(*client->capabilities));
750 if (client->capabilities == NULL) {
751 client->ncapa_alloc = 0;
752 return got_error_from_errno("calloc");
755 log_debug("expecting %zu capabilities from uid %d",
756 client->ncapa_alloc, client->euid);
757 return NULL;
760 static const struct got_error *
761 recv_capability(struct gotd_client *client, struct imsg *imsg)
763 struct gotd_imsg_capability icapa;
764 struct gotd_client_capability *capa;
765 size_t datalen;
766 char *key, *value = NULL;
768 if (client->capabilities == NULL ||
769 client->ncapabilities >= client->ncapa_alloc) {
770 return got_error_msg(GOT_ERR_BAD_REQUEST,
771 "unexpected capability received");
774 memset(&icapa, 0, sizeof(icapa));
776 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
777 if (datalen < sizeof(icapa))
778 return got_error(GOT_ERR_PRIVSEP_LEN);
779 memcpy(&icapa, imsg->data, sizeof(icapa));
781 if (datalen != sizeof(icapa) + icapa.key_len + icapa.value_len)
782 return got_error(GOT_ERR_PRIVSEP_LEN);
784 key = malloc(icapa.key_len + 1);
785 if (key == NULL)
786 return got_error_from_errno("malloc");
787 if (icapa.value_len > 0) {
788 value = malloc(icapa.value_len + 1);
789 if (value == NULL) {
790 free(key);
791 return got_error_from_errno("malloc");
795 memcpy(key, imsg->data + sizeof(icapa), icapa.key_len);
796 key[icapa.key_len] = '\0';
797 if (value) {
798 memcpy(value, imsg->data + sizeof(icapa) + icapa.key_len,
799 icapa.value_len);
800 value[icapa.value_len] = '\0';
803 capa = &client->capabilities[client->ncapabilities++];
804 capa->key = key;
805 capa->value = value;
807 if (value)
808 log_debug("uid %d: capability %s=%s", client->euid, key, value);
809 else
810 log_debug("uid %d: capability %s", client->euid, key);
812 return NULL;
815 static const struct got_error *
816 send_packfile(struct gotd_client *client)
818 const struct got_error *err = NULL;
819 struct gotd_imsg_send_packfile ipack;
820 struct gotd_imsg_packfile_pipe ipipe;
821 int pipe[2];
823 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe) == -1)
824 return got_error_from_errno("socketpair");
826 memset(&ipack, 0, sizeof(ipack));
827 memset(&ipipe, 0, sizeof(ipipe));
829 ipack.client_id = client->id;
830 if (client_has_capability(client, GOT_CAPA_SIDE_BAND_64K))
831 ipack.report_progress = 1;
833 client->delta_cache_fd = got_opentempfd();
834 if (client->delta_cache_fd == -1)
835 return got_error_from_errno("got_opentempfd");
837 if (gotd_imsg_compose_event(&client->repo_read->iev,
838 GOTD_IMSG_SEND_PACKFILE, PROC_GOTD, client->delta_cache_fd,
839 &ipack, sizeof(ipack)) == -1) {
840 err = got_error_from_errno("imsg compose SEND_PACKFILE");
841 close(pipe[0]);
842 close(pipe[1]);
843 return err;
846 ipipe.client_id = client->id;
848 /* Send pack pipe end 0 to repo_read. */
849 if (gotd_imsg_compose_event(&client->repo_read->iev,
850 GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[0],
851 &ipipe, sizeof(ipipe)) == -1) {
852 err = got_error_from_errno("imsg compose PACKFILE_PIPE");
853 close(pipe[1]);
854 return err;
857 /* Send pack pipe end 1 to gotsh(1) (expects just an fd, no data). */
858 if (gotd_imsg_compose_event(&client->iev,
859 GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[1], NULL, 0) == -1)
860 err = got_error_from_errno("imsg compose PACKFILE_PIPE");
862 return err;
865 static const struct got_error *
866 recv_packfile(struct gotd_client *client)
868 const struct got_error *err = NULL;
869 struct gotd_imsg_recv_packfile ipack;
870 struct gotd_imsg_packfile_pipe ipipe;
871 struct gotd_imsg_packidx_file ifile;
872 char *basepath = NULL, *pack_path = NULL, *idx_path = NULL;
873 int packfd = -1, idxfd = -1;
874 int pipe[2] = { -1, -1 };
876 if (client->packfile_path) {
877 return got_error_fmt(GOT_ERR_PRIVSEP_MSG,
878 "uid %d already has a pack file", client->euid);
881 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe) == -1)
882 return got_error_from_errno("socketpair");
884 memset(&ipipe, 0, sizeof(ipipe));
885 ipipe.client_id = client->id;
887 if (gotd_imsg_compose_event(&client->repo_write->iev,
888 GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[0],
889 &ipipe, sizeof(ipipe)) == -1) {
890 err = got_error_from_errno("imsg compose PACKFILE_PIPE");
891 pipe[0] = -1;
892 goto done;
894 pipe[0] = -1;
896 if (gotd_imsg_compose_event(&client->repo_write->iev,
897 GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[1],
898 &ipipe, sizeof(ipipe)) == -1)
899 err = got_error_from_errno("imsg compose PACKFILE_PIPE");
900 pipe[1] = -1;
902 if (asprintf(&basepath, "%s/%s/receiving-from-uid-%d.pack",
903 client->repo_write->repo_path, GOT_OBJECTS_PACK_DIR,
904 client->euid) == -1) {
905 err = got_error_from_errno("asprintf");
906 goto done;
909 err = got_opentemp_named_fd(&pack_path, &packfd, basepath, "");
910 if (err)
911 goto done;
913 free(basepath);
914 if (asprintf(&basepath, "%s/%s/receiving-from-uid-%d.idx",
915 client->repo_write->repo_path, GOT_OBJECTS_PACK_DIR,
916 client->euid) == -1) {
917 err = got_error_from_errno("asprintf");
918 basepath = NULL;
919 goto done;
921 err = got_opentemp_named_fd(&idx_path, &idxfd, basepath, "");
922 if (err)
923 goto done;
925 memset(&ifile, 0, sizeof(ifile));
926 ifile.client_id = client->id;
927 if (gotd_imsg_compose_event(&client->repo_write->iev,
928 GOTD_IMSG_PACKIDX_FILE, PROC_GOTD, idxfd,
929 &ifile, sizeof(ifile)) == -1) {
930 err = got_error_from_errno("imsg compose PACKIDX_FILE");
931 idxfd = -1;
932 goto done;
934 idxfd = -1;
936 memset(&ipack, 0, sizeof(ipack));
937 ipack.client_id = client->id;
938 if (client_has_capability(client, GOT_CAPA_REPORT_STATUS))
939 ipack.report_status = 1;
941 if (gotd_imsg_compose_event(&client->repo_write->iev,
942 GOTD_IMSG_RECV_PACKFILE, PROC_GOTD, packfd,
943 &ipack, sizeof(ipack)) == -1) {
944 err = got_error_from_errno("imsg compose RECV_PACKFILE");
945 packfd = -1;
946 goto done;
948 packfd = -1;
950 done:
951 free(basepath);
952 if (pipe[0] != -1 && close(pipe[0]) == -1 && err == NULL)
953 err = got_error_from_errno("close");
954 if (pipe[1] != -1 && close(pipe[1]) == -1 && err == NULL)
955 err = got_error_from_errno("close");
956 if (packfd != -1 && close(packfd) == -1 && err == NULL)
957 err = got_error_from_errno("close");
958 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
959 err = got_error_from_errno("close");
960 if (err) {
961 free(pack_path);
962 free(idx_path);
963 } else {
964 client->packfile_path = pack_path;
965 client->packidx_path = idx_path;
967 return err;
970 static void
971 gotd_request(int fd, short events, void *arg)
973 struct gotd_imsgev *iev = arg;
974 struct imsgbuf *ibuf = &iev->ibuf;
975 struct gotd_client *client = iev->handler_arg;
976 const struct got_error *err = NULL;
977 struct imsg imsg;
978 ssize_t n;
980 if (events & EV_WRITE) {
981 while (ibuf->w.queued) {
982 n = msgbuf_write(&ibuf->w);
983 if (n == -1 && errno == EPIPE) {
984 /*
985 * The client has closed its socket.
986 * This can happen when Git clients are
987 * done sending pack file data.
988 */
989 msgbuf_clear(&ibuf->w);
990 continue;
991 } else if (n == -1 && errno != EAGAIN) {
992 err = got_error_from_errno("imsg_flush");
993 disconnect_on_error(client, err);
994 return;
996 if (n == 0) {
997 /* Connection closed. */
998 err = got_error(GOT_ERR_EOF);
999 disconnect_on_error(client, err);
1000 return;
1004 /* Disconnect gotctl(8) now that messages have been sent. */
1005 if (!client_is_reading(client) && !client_is_writing(client)) {
1006 disconnect(client);
1007 return;
1011 if ((events & EV_READ) == 0)
1012 return;
1014 memset(&imsg, 0, sizeof(imsg));
1016 while (err == NULL) {
1017 err = gotd_imsg_recv(&imsg, ibuf, 0);
1018 if (err) {
1019 if (err->code == GOT_ERR_PRIVSEP_READ)
1020 err = NULL;
1021 break;
1024 evtimer_del(&client->tmo);
1026 switch (imsg.hdr.type) {
1027 case GOTD_IMSG_INFO:
1028 err = send_info(client);
1029 break;
1030 case GOTD_IMSG_STOP:
1031 err = stop_gotd(client);
1032 break;
1033 case GOTD_IMSG_LIST_REFS:
1034 if (client->state != GOTD_STATE_EXPECT_LIST_REFS) {
1035 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1036 "unexpected list-refs request received");
1037 break;
1039 err = start_client_session(client, &imsg);
1040 if (err)
1041 break;
1042 break;
1043 case GOTD_IMSG_CAPABILITIES:
1044 if (client->state != GOTD_STATE_EXPECT_CAPABILITIES) {
1045 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1046 "unexpected capabilities received");
1047 break;
1049 log_debug("receiving capabilities from uid %d",
1050 client->euid);
1051 err = recv_capabilities(client, &imsg);
1052 break;
1053 case GOTD_IMSG_CAPABILITY:
1054 if (client->state != GOTD_STATE_EXPECT_CAPABILITIES) {
1055 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1056 "unexpected capability received");
1057 break;
1059 err = recv_capability(client, &imsg);
1060 if (err || client->ncapabilities < client->ncapa_alloc)
1061 break;
1062 if (client_is_reading(client)) {
1063 client->state = GOTD_STATE_EXPECT_WANT;
1064 log_debug("uid %d: expecting want-lines",
1065 client->euid);
1066 } else if (client_is_writing(client)) {
1067 client->state = GOTD_STATE_EXPECT_REF_UPDATE;
1068 log_debug("uid %d: expecting ref-update-lines",
1069 client->euid);
1070 } else
1071 fatalx("client %d is both reading and writing",
1072 client->euid);
1073 break;
1074 case GOTD_IMSG_WANT:
1075 if (client->state != GOTD_STATE_EXPECT_WANT) {
1076 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1077 "unexpected want-line received");
1078 break;
1080 log_debug("received want-line from uid %d",
1081 client->euid);
1082 err = ensure_client_is_reading(client);
1083 if (err)
1084 break;
1085 err = forward_want(client, &imsg);
1086 break;
1087 case GOTD_IMSG_REF_UPDATE:
1088 if (client->state != GOTD_STATE_EXPECT_REF_UPDATE) {
1089 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1090 "unexpected ref-update-line received");
1091 break;
1093 log_debug("received ref-update-line from uid %d",
1094 client->euid);
1095 err = ensure_client_is_writing(client);
1096 if (err)
1097 break;
1098 err = forward_ref_update(client, &imsg);
1099 if (err)
1100 break;
1101 client->state = GOTD_STATE_EXPECT_MORE_REF_UPDATES;
1102 break;
1103 case GOTD_IMSG_HAVE:
1104 if (client->state != GOTD_STATE_EXPECT_HAVE) {
1105 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1106 "unexpected have-line received");
1107 break;
1109 log_debug("received have-line from uid %d",
1110 client->euid);
1111 err = ensure_client_is_reading(client);
1112 if (err)
1113 break;
1114 err = forward_have(client, &imsg);
1115 if (err)
1116 break;
1117 break;
1118 case GOTD_IMSG_FLUSH:
1119 if (client->state == GOTD_STATE_EXPECT_WANT ||
1120 client->state == GOTD_STATE_EXPECT_HAVE) {
1121 err = ensure_client_is_reading(client);
1122 if (err)
1123 break;
1124 } else if (client->state ==
1125 GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1126 err = ensure_client_is_writing(client);
1127 if (err)
1128 break;
1129 } else {
1130 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1131 "unexpected flush-pkt received");
1132 break;
1134 log_debug("received flush-pkt from uid %d",
1135 client->euid);
1136 if (client->state == GOTD_STATE_EXPECT_WANT) {
1137 client->state = GOTD_STATE_EXPECT_HAVE;
1138 log_debug("uid %d: expecting have-lines",
1139 client->euid);
1140 } else if (client->state == GOTD_STATE_EXPECT_HAVE) {
1141 client->state = GOTD_STATE_EXPECT_DONE;
1142 log_debug("uid %d: expecting 'done'",
1143 client->euid);
1144 } else if (client->state ==
1145 GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1146 client->state = GOTD_STATE_EXPECT_PACKFILE;
1147 log_debug("uid %d: expecting packfile",
1148 client->euid);
1149 err = recv_packfile(client);
1150 } else {
1151 /* should not happen, see above */
1152 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1153 "unexpected client state");
1154 break;
1156 break;
1157 case GOTD_IMSG_DONE:
1158 if (client->state != GOTD_STATE_EXPECT_HAVE &&
1159 client->state != GOTD_STATE_EXPECT_DONE) {
1160 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1161 "unexpected flush-pkt received");
1162 break;
1164 log_debug("received 'done' from uid %d", client->euid);
1165 err = ensure_client_is_reading(client);
1166 if (err)
1167 break;
1168 client->state = GOTD_STATE_DONE;
1169 err = send_packfile(client);
1170 break;
1171 default:
1172 err = got_error(GOT_ERR_PRIVSEP_MSG);
1173 break;
1176 imsg_free(&imsg);
1179 if (err) {
1180 if (err->code != GOT_ERR_EOF ||
1181 client->state != GOTD_STATE_EXPECT_PACKFILE)
1182 disconnect_on_error(client, err);
1183 } else {
1184 gotd_imsg_event_add(&client->iev);
1185 if (client->state == GOTD_STATE_EXPECT_LIST_REFS)
1186 evtimer_add(&client->tmo, &auth_timeout);
1187 else
1188 evtimer_add(&client->tmo, &gotd.request_timeout);
1192 static void
1193 gotd_request_timeout(int fd, short events, void *arg)
1195 struct gotd_client *client = arg;
1197 log_debug("disconnecting uid %d due to timeout", client->euid);
1198 disconnect(client);
1201 static const struct got_error *
1202 recv_connect(uint32_t *client_id, struct imsg *imsg)
1204 const struct got_error *err = NULL;
1205 struct gotd_imsg_connect iconnect;
1206 size_t datalen;
1207 int s = -1;
1208 struct gotd_client *client = NULL;
1210 *client_id = 0;
1212 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1213 if (datalen != sizeof(iconnect))
1214 return got_error(GOT_ERR_PRIVSEP_LEN);
1215 memcpy(&iconnect, imsg->data, sizeof(iconnect));
1217 s = imsg->fd;
1218 if (s == -1) {
1219 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1220 goto done;
1223 if (find_client(iconnect.client_id)) {
1224 err = got_error_msg(GOT_ERR_CLIENT_ID, "duplicate client ID");
1225 goto done;
1228 client = calloc(1, sizeof(*client));
1229 if (client == NULL) {
1230 err = got_error_from_errno("calloc");
1231 goto done;
1234 *client_id = iconnect.client_id;
1236 client->state = GOTD_STATE_EXPECT_LIST_REFS;
1237 client->id = iconnect.client_id;
1238 client->fd = s;
1239 s = -1;
1240 client->delta_cache_fd = -1;
1241 /* The auth process will verify UID/GID for us. */
1242 client->euid = iconnect.euid;
1243 client->egid = iconnect.egid;
1244 client->nref_updates = -1;
1246 imsg_init(&client->iev.ibuf, client->fd);
1247 client->iev.handler = gotd_request;
1248 client->iev.events = EV_READ;
1249 client->iev.handler_arg = client;
1251 event_set(&client->iev.ev, client->fd, EV_READ, gotd_request,
1252 &client->iev);
1253 gotd_imsg_event_add(&client->iev);
1255 evtimer_set(&client->tmo, gotd_request_timeout, client);
1257 add_client(client);
1258 log_debug("%s: new client uid %d connected on fd %d", __func__,
1259 client->euid, client->fd);
1260 done:
1261 if (err) {
1262 struct gotd_child_proc *listen_proc = &gotd.listen_proc;
1263 struct gotd_imsg_disconnect idisconnect;
1265 idisconnect.client_id = client->id;
1266 if (gotd_imsg_compose_event(&listen_proc->iev,
1267 GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
1268 &idisconnect, sizeof(idisconnect)) == -1)
1269 log_warn("imsg compose DISCONNECT");
1271 if (s != -1)
1272 close(s);
1275 return err;
1278 static const char *gotd_proc_names[PROC_MAX] = {
1279 "parent",
1280 "listen",
1281 "auth",
1282 "repo_read",
1283 "repo_write"
1286 static void
1287 kill_proc(struct gotd_child_proc *proc, int fatal)
1289 if (fatal) {
1290 log_warnx("sending SIGKILL to PID %d", proc->pid);
1291 kill(proc->pid, SIGKILL);
1292 } else
1293 kill(proc->pid, SIGTERM);
1296 static void
1297 gotd_shutdown(void)
1299 struct gotd_child_proc *proc;
1300 uint64_t slot;
1302 for (slot = 0; slot < nitems(gotd_clients); slot++) {
1303 struct gotd_client *c, *tmp;
1305 STAILQ_FOREACH_SAFE(c, &gotd_clients[slot], entry, tmp)
1306 disconnect(c);
1309 proc = &gotd.listen_proc;
1310 msgbuf_clear(&proc->iev.ibuf.w);
1311 close(proc->iev.ibuf.fd);
1312 kill_proc(proc, 0);
1313 wait_for_child(proc->pid);
1315 log_info("terminating");
1316 exit(0);
1319 void
1320 gotd_sighdlr(int sig, short event, void *arg)
1323 * Normal signal handler rules don't apply because libevent
1324 * decouples for us.
1327 switch (sig) {
1328 case SIGHUP:
1329 log_info("%s: ignoring SIGHUP", __func__);
1330 break;
1331 case SIGUSR1:
1332 log_info("%s: ignoring SIGUSR1", __func__);
1333 break;
1334 case SIGTERM:
1335 case SIGINT:
1336 gotd_shutdown();
1337 log_warnx("gotd terminating");
1338 exit(0);
1339 break;
1340 default:
1341 fatalx("unexpected signal");
1345 static const struct got_error *
1346 ensure_proc_is_reading(struct gotd_client *client,
1347 struct gotd_child_proc *proc)
1349 if (!client_is_reading(client)) {
1350 kill_proc(proc, 1);
1351 return got_error_fmt(GOT_ERR_BAD_PACKET,
1352 "PID %d handled a read-request for uid %d but this "
1353 "user is not reading from a repository", proc->pid,
1354 client->euid);
1357 return NULL;
1360 static const struct got_error *
1361 ensure_proc_is_writing(struct gotd_client *client,
1362 struct gotd_child_proc *proc)
1364 if (!client_is_writing(client)) {
1365 kill_proc(proc, 1);
1366 return got_error_fmt(GOT_ERR_BAD_PACKET,
1367 "PID %d handled a write-request for uid %d but this "
1368 "user is not writing to a repository", proc->pid,
1369 client->euid);
1372 return NULL;
1375 static int
1376 verify_imsg_src(struct gotd_client *client, struct gotd_child_proc *proc,
1377 struct imsg *imsg)
1379 const struct got_error *err;
1380 struct gotd_child_proc *client_proc;
1381 int ret = 0;
1383 if (proc->type == PROC_REPO_READ || proc->type == PROC_REPO_WRITE) {
1384 client_proc = get_client_proc(client);
1385 if (client_proc == NULL)
1386 fatalx("no process found for uid %d", client->euid);
1387 if (proc->pid != client_proc->pid) {
1388 kill_proc(proc, 1);
1389 log_warnx("received message from PID %d for uid %d, "
1390 "while PID %d is the process serving this user",
1391 proc->pid, client->euid, client_proc->pid);
1392 return 0;
1396 switch (imsg->hdr.type) {
1397 case GOTD_IMSG_ERROR:
1398 ret = 1;
1399 break;
1400 case GOTD_IMSG_CONNECT:
1401 if (proc->type != PROC_LISTEN) {
1402 err = got_error_fmt(GOT_ERR_BAD_PACKET,
1403 "new connection for uid %d from PID %d "
1404 "which is not the listen process",
1405 proc->pid, client->euid);
1406 } else
1407 ret = 1;
1408 break;
1409 case GOTD_IMSG_ACCESS_GRANTED:
1410 if (proc->type != PROC_AUTH) {
1411 err = got_error_fmt(GOT_ERR_BAD_PACKET,
1412 "authentication of uid %d from PID %d "
1413 "which is not the auth process",
1414 proc->pid, client->euid);
1415 } else
1416 ret = 1;
1417 break;
1418 case GOTD_IMSG_REPO_CHILD_READY:
1419 if (proc->type != PROC_REPO_READ &&
1420 proc->type != PROC_REPO_WRITE) {
1421 err = got_error_fmt(GOT_ERR_BAD_PACKET,
1422 "unexpected \"ready\" signal from PID %d",
1423 proc->pid);
1424 } else
1425 ret = 1;
1426 break;
1427 case GOTD_IMSG_PACKFILE_DONE:
1428 err = ensure_proc_is_reading(client, proc);
1429 if (err)
1430 log_warnx("uid %d: %s", client->euid, err->msg);
1431 else
1432 ret = 1;
1433 break;
1434 case GOTD_IMSG_PACKFILE_INSTALL:
1435 case GOTD_IMSG_REF_UPDATES_START:
1436 case GOTD_IMSG_REF_UPDATE:
1437 err = ensure_proc_is_writing(client, proc);
1438 if (err)
1439 log_warnx("uid %d: %s", client->euid, err->msg);
1440 else
1441 ret = 1;
1442 break;
1443 default:
1444 log_debug("%s: unexpected imsg %d", __func__, imsg->hdr.type);
1445 break;
1448 return ret;
1451 static const struct got_error *
1452 list_refs_request(struct gotd_client *client, struct gotd_imsgev *iev)
1454 static const struct got_error *err;
1455 struct gotd_imsg_list_refs_internal ilref;
1456 int fd;
1458 memset(&ilref, 0, sizeof(ilref));
1459 ilref.client_id = client->id;
1461 fd = dup(client->fd);
1462 if (fd == -1)
1463 return got_error_from_errno("dup");
1465 if (gotd_imsg_compose_event(iev, GOTD_IMSG_LIST_REFS_INTERNAL,
1466 PROC_GOTD, fd, &ilref, sizeof(ilref)) == -1) {
1467 err = got_error_from_errno("imsg compose WANT");
1468 close(fd);
1469 return err;
1472 client->state = GOTD_STATE_EXPECT_CAPABILITIES;
1473 log_debug("uid %d: expecting capabilities", client->euid);
1474 return NULL;
1477 static const struct got_error *
1478 recv_packfile_done(uint32_t *client_id, struct imsg *imsg)
1480 struct gotd_imsg_packfile_done idone;
1481 size_t datalen;
1483 log_debug("packfile-done received");
1485 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1486 if (datalen != sizeof(idone))
1487 return got_error(GOT_ERR_PRIVSEP_LEN);
1488 memcpy(&idone, imsg->data, sizeof(idone));
1490 *client_id = idone.client_id;
1491 return NULL;
1494 static const struct got_error *
1495 recv_packfile_install(uint32_t *client_id, struct imsg *imsg)
1497 struct gotd_imsg_packfile_install inst;
1498 size_t datalen;
1500 log_debug("packfile-install received");
1502 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1503 if (datalen != sizeof(inst))
1504 return got_error(GOT_ERR_PRIVSEP_LEN);
1505 memcpy(&inst, imsg->data, sizeof(inst));
1507 *client_id = inst.client_id;
1508 return NULL;
1511 static const struct got_error *
1512 recv_ref_updates_start(uint32_t *client_id, struct imsg *imsg)
1514 struct gotd_imsg_ref_updates_start istart;
1515 size_t datalen;
1517 log_debug("ref-updates-start received");
1519 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1520 if (datalen != sizeof(istart))
1521 return got_error(GOT_ERR_PRIVSEP_LEN);
1522 memcpy(&istart, imsg->data, sizeof(istart));
1524 *client_id = istart.client_id;
1525 return NULL;
1528 static const struct got_error *
1529 recv_ref_update(uint32_t *client_id, struct imsg *imsg)
1531 struct gotd_imsg_ref_update iref;
1532 size_t datalen;
1534 log_debug("ref-update received");
1536 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1537 if (datalen < sizeof(iref))
1538 return got_error(GOT_ERR_PRIVSEP_LEN);
1539 memcpy(&iref, imsg->data, sizeof(iref));
1541 *client_id = iref.client_id;
1542 return NULL;
1545 static const struct got_error *
1546 send_ref_update_ok(struct gotd_client *client,
1547 struct gotd_imsg_ref_update *iref, const char *refname)
1549 struct gotd_imsg_ref_update_ok iok;
1550 struct ibuf *wbuf;
1551 size_t len;
1553 memset(&iok, 0, sizeof(iok));
1554 iok.client_id = client->id;
1555 memcpy(iok.old_id, iref->old_id, SHA1_DIGEST_LENGTH);
1556 memcpy(iok.new_id, iref->new_id, SHA1_DIGEST_LENGTH);
1557 iok.name_len = strlen(refname);
1559 len = sizeof(iok) + iok.name_len;
1560 wbuf = imsg_create(&client->iev.ibuf, GOTD_IMSG_REF_UPDATE_OK,
1561 PROC_GOTD, gotd.pid, len);
1562 if (wbuf == NULL)
1563 return got_error_from_errno("imsg_create REF_UPDATE_OK");
1565 if (imsg_add(wbuf, &iok, sizeof(iok)) == -1)
1566 return got_error_from_errno("imsg_add REF_UPDATE_OK");
1567 if (imsg_add(wbuf, refname, iok.name_len) == -1)
1568 return got_error_from_errno("imsg_add REF_UPDATE_OK");
1570 wbuf->fd = -1;
1571 imsg_close(&client->iev.ibuf, wbuf);
1572 gotd_imsg_event_add(&client->iev);
1573 return NULL;
1576 static void
1577 send_refs_updated(struct gotd_client *client)
1579 if (gotd_imsg_compose_event(&client->iev,
1580 GOTD_IMSG_REFS_UPDATED, PROC_GOTD, -1, NULL, 0) == -1)
1581 log_warn("imsg compose REFS_UPDATED");
1584 static const struct got_error *
1585 send_ref_update_ng(struct gotd_client *client,
1586 struct gotd_imsg_ref_update *iref, const char *refname,
1587 const char *reason)
1589 const struct got_error *ng_err;
1590 struct gotd_imsg_ref_update_ng ing;
1591 struct ibuf *wbuf;
1592 size_t len;
1594 memset(&ing, 0, sizeof(ing));
1595 ing.client_id = client->id;
1596 memcpy(ing.old_id, iref->old_id, SHA1_DIGEST_LENGTH);
1597 memcpy(ing.new_id, iref->new_id, SHA1_DIGEST_LENGTH);
1598 ing.name_len = strlen(refname);
1600 ng_err = got_error_fmt(GOT_ERR_REF_BUSY, "%s", reason);
1601 ing.reason_len = strlen(ng_err->msg);
1603 len = sizeof(ing) + ing.name_len + ing.reason_len;
1604 wbuf = imsg_create(&client->iev.ibuf, GOTD_IMSG_REF_UPDATE_NG,
1605 PROC_GOTD, gotd.pid, len);
1606 if (wbuf == NULL)
1607 return got_error_from_errno("imsg_create REF_UPDATE_NG");
1609 if (imsg_add(wbuf, &ing, sizeof(ing)) == -1)
1610 return got_error_from_errno("imsg_add REF_UPDATE_NG");
1611 if (imsg_add(wbuf, refname, ing.name_len) == -1)
1612 return got_error_from_errno("imsg_add REF_UPDATE_NG");
1613 if (imsg_add(wbuf, ng_err->msg, ing.reason_len) == -1)
1614 return got_error_from_errno("imsg_add REF_UPDATE_NG");
1616 wbuf->fd = -1;
1617 imsg_close(&client->iev.ibuf, wbuf);
1618 gotd_imsg_event_add(&client->iev);
1619 return NULL;
1622 static const struct got_error *
1623 install_pack(struct gotd_client *client, const char *repo_path,
1624 struct imsg *imsg)
1626 const struct got_error *err = NULL;
1627 struct gotd_imsg_packfile_install inst;
1628 char hex[SHA1_DIGEST_STRING_LENGTH];
1629 size_t datalen;
1630 char *packfile_path = NULL, *packidx_path = NULL;
1632 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1633 if (datalen != sizeof(inst))
1634 return got_error(GOT_ERR_PRIVSEP_LEN);
1635 memcpy(&inst, imsg->data, sizeof(inst));
1637 if (client->packfile_path == NULL)
1638 return got_error_msg(GOT_ERR_BAD_REQUEST,
1639 "client has no pack file");
1640 if (client->packidx_path == NULL)
1641 return got_error_msg(GOT_ERR_BAD_REQUEST,
1642 "client has no pack file index");
1644 if (got_sha1_digest_to_str(inst.pack_sha1, hex, sizeof(hex)) == NULL)
1645 return got_error_msg(GOT_ERR_NO_SPACE,
1646 "could not convert pack file SHA1 to hex");
1648 if (asprintf(&packfile_path, "/%s/%s/pack-%s.pack",
1649 repo_path, GOT_OBJECTS_PACK_DIR, hex) == -1) {
1650 err = got_error_from_errno("asprintf");
1651 goto done;
1654 if (asprintf(&packidx_path, "/%s/%s/pack-%s.idx",
1655 repo_path, GOT_OBJECTS_PACK_DIR, hex) == -1) {
1656 err = got_error_from_errno("asprintf");
1657 goto done;
1660 if (rename(client->packfile_path, packfile_path) == -1) {
1661 err = got_error_from_errno3("rename", client->packfile_path,
1662 packfile_path);
1663 goto done;
1666 free(client->packfile_path);
1667 client->packfile_path = NULL;
1669 if (rename(client->packidx_path, packidx_path) == -1) {
1670 err = got_error_from_errno3("rename", client->packidx_path,
1671 packidx_path);
1672 goto done;
1675 free(client->packidx_path);
1676 client->packidx_path = NULL;
1677 done:
1678 free(packfile_path);
1679 free(packidx_path);
1680 return err;
1683 static const struct got_error *
1684 begin_ref_updates(struct gotd_client *client, struct imsg *imsg)
1686 struct gotd_imsg_ref_updates_start istart;
1687 size_t datalen;
1689 if (client->nref_updates != -1)
1690 return got_error(GOT_ERR_PRIVSEP_MSG);
1692 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1693 if (datalen != sizeof(istart))
1694 return got_error(GOT_ERR_PRIVSEP_LEN);
1695 memcpy(&istart, imsg->data, sizeof(istart));
1697 if (istart.nref_updates <= 0)
1698 return got_error(GOT_ERR_PRIVSEP_MSG);
1700 client->nref_updates = istart.nref_updates;
1701 return NULL;
1704 static const struct got_error *
1705 update_ref(struct gotd_client *client, const char *repo_path,
1706 struct imsg *imsg)
1708 const struct got_error *err = NULL;
1709 struct got_repository *repo = NULL;
1710 struct got_reference *ref = NULL;
1711 struct gotd_imsg_ref_update iref;
1712 struct got_object_id old_id, new_id;
1713 struct got_object_id *id = NULL;
1714 struct got_object *obj = NULL;
1715 char *refname = NULL;
1716 size_t datalen;
1717 int locked = 0;
1719 log_debug("update-ref from uid %d", client->euid);
1721 if (client->nref_updates <= 0)
1722 return got_error(GOT_ERR_PRIVSEP_MSG);
1724 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1725 if (datalen < sizeof(iref))
1726 return got_error(GOT_ERR_PRIVSEP_LEN);
1727 memcpy(&iref, imsg->data, sizeof(iref));
1728 if (datalen != sizeof(iref) + iref.name_len)
1729 return got_error(GOT_ERR_PRIVSEP_LEN);
1730 refname = malloc(iref.name_len + 1);
1731 if (refname == NULL)
1732 return got_error_from_errno("malloc");
1733 memcpy(refname, imsg->data + sizeof(iref), iref.name_len);
1734 refname[iref.name_len] = '\0';
1736 log_debug("updating ref %s for uid %d", refname, client->euid);
1738 err = got_repo_open(&repo, repo_path, NULL, NULL);
1739 if (err)
1740 goto done;
1742 memcpy(old_id.sha1, iref.old_id, SHA1_DIGEST_LENGTH);
1743 memcpy(new_id.sha1, iref.new_id, SHA1_DIGEST_LENGTH);
1744 err = got_object_open(&obj, repo, &new_id);
1745 if (err)
1746 goto done;
1748 if (iref.ref_is_new) {
1749 err = got_ref_open(&ref, repo, refname, 0);
1750 if (err) {
1751 if (err->code != GOT_ERR_NOT_REF)
1752 goto done;
1753 err = got_ref_alloc(&ref, refname, &new_id);
1754 if (err)
1755 goto done;
1756 err = got_ref_write(ref, repo); /* will lock/unlock */
1757 if (err)
1758 goto done;
1759 } else {
1760 err = got_error_fmt(GOT_ERR_REF_BUSY,
1761 "%s has been created by someone else "
1762 "while transaction was in progress",
1763 got_ref_get_name(ref));
1764 goto done;
1766 } else {
1767 err = got_ref_open(&ref, repo, refname, 1 /* lock */);
1768 if (err)
1769 goto done;
1770 locked = 1;
1772 err = got_ref_resolve(&id, repo, ref);
1773 if (err)
1774 goto done;
1776 if (got_object_id_cmp(id, &old_id) != 0) {
1777 err = got_error_fmt(GOT_ERR_REF_BUSY,
1778 "%s has been modified by someone else "
1779 "while transaction was in progress",
1780 got_ref_get_name(ref));
1781 goto done;
1784 err = got_ref_change_ref(ref, &new_id);
1785 if (err)
1786 goto done;
1788 err = got_ref_write(ref, repo);
1789 if (err)
1790 goto done;
1792 free(id);
1793 id = NULL;
1795 done:
1796 if (err) {
1797 if (err->code == GOT_ERR_LOCKFILE_TIMEOUT) {
1798 err = got_error_fmt(GOT_ERR_LOCKFILE_TIMEOUT,
1799 "could not acquire exclusive file lock for %s",
1800 refname);
1802 send_ref_update_ng(client, &iref, refname, err->msg);
1803 } else
1804 send_ref_update_ok(client, &iref, refname);
1806 if (client->nref_updates > 0) {
1807 client->nref_updates--;
1808 if (client->nref_updates == 0)
1809 send_refs_updated(client);
1812 if (locked) {
1813 const struct got_error *unlock_err;
1814 unlock_err = got_ref_unlock(ref);
1815 if (unlock_err && err == NULL)
1816 err = unlock_err;
1818 if (ref)
1819 got_ref_close(ref);
1820 if (obj)
1821 got_object_close(obj);
1822 if (repo)
1823 got_repo_close(repo);
1824 free(refname);
1825 free(id);
1826 return err;
1829 static void
1830 gotd_dispatch_listener(int fd, short event, void *arg)
1832 struct gotd_imsgev *iev = arg;
1833 struct imsgbuf *ibuf = &iev->ibuf;
1834 struct gotd_child_proc *proc = &gotd.listen_proc;
1835 ssize_t n;
1836 int shut = 0;
1837 struct imsg imsg;
1839 if (proc->iev.ibuf.fd != fd)
1840 fatalx("%s: unexpected fd %d", __func__, fd);
1842 if (event & EV_READ) {
1843 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1844 fatal("imsg_read error");
1845 if (n == 0) {
1846 /* Connection closed. */
1847 shut = 1;
1848 goto done;
1852 if (event & EV_WRITE) {
1853 n = msgbuf_write(&ibuf->w);
1854 if (n == -1 && errno != EAGAIN)
1855 fatal("msgbuf_write");
1856 if (n == 0) {
1857 /* Connection closed. */
1858 shut = 1;
1859 goto done;
1863 for (;;) {
1864 const struct got_error *err = NULL;
1865 struct gotd_client *client = NULL;
1866 uint32_t client_id = 0;
1867 int do_disconnect = 0;
1869 if ((n = imsg_get(ibuf, &imsg)) == -1)
1870 fatal("%s: imsg_get error", __func__);
1871 if (n == 0) /* No more messages. */
1872 break;
1874 switch (imsg.hdr.type) {
1875 case GOTD_IMSG_ERROR:
1876 do_disconnect = 1;
1877 err = gotd_imsg_recv_error(&client_id, &imsg);
1878 break;
1879 case GOTD_IMSG_CONNECT:
1880 err = recv_connect(&client_id, &imsg);
1881 break;
1882 default:
1883 log_debug("unexpected imsg %d", imsg.hdr.type);
1884 break;
1887 client = find_client(client_id);
1888 if (client == NULL) {
1889 log_warnx("%s: client not found", __func__);
1890 imsg_free(&imsg);
1891 continue;
1894 if (err)
1895 log_warnx("uid %d: %s", client->euid, err->msg);
1897 if (do_disconnect) {
1898 if (err)
1899 disconnect_on_error(client, err);
1900 else
1901 disconnect(client);
1904 imsg_free(&imsg);
1906 done:
1907 if (!shut) {
1908 gotd_imsg_event_add(iev);
1909 } else {
1910 /* This pipe is dead. Remove its event handler */
1911 event_del(&iev->ev);
1912 event_loopexit(NULL);
1916 static void
1917 gotd_dispatch_auth_child(int fd, short event, void *arg)
1919 const struct got_error *err = NULL;
1920 struct gotd_imsgev *iev = arg;
1921 struct imsgbuf *ibuf = &iev->ibuf;
1922 struct gotd_client *client;
1923 struct gotd_repo *repo = NULL;
1924 ssize_t n;
1925 int shut = 0;
1926 struct imsg imsg;
1927 uint32_t client_id = 0;
1928 int do_disconnect = 0;
1929 enum gotd_procid proc_type;
1931 client = find_client_by_proc_fd(fd);
1932 if (client == NULL)
1933 fatalx("cannot find client for fd %d", fd);
1935 if (client->auth == NULL)
1936 fatalx("cannot find auth child process for fd %d", fd);
1938 if (event & EV_READ) {
1939 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1940 fatal("imsg_read error");
1941 if (n == 0) {
1942 /* Connection closed. */
1943 shut = 1;
1944 goto done;
1948 if (event & EV_WRITE) {
1949 n = msgbuf_write(&ibuf->w);
1950 if (n == -1 && errno != EAGAIN)
1951 fatal("msgbuf_write");
1952 if (n == 0) {
1953 /* Connection closed. */
1954 shut = 1;
1956 goto done;
1959 if (client->auth->iev.ibuf.fd != fd)
1960 fatalx("%s: unexpected fd %d", __func__, fd);
1962 if ((n = imsg_get(ibuf, &imsg)) == -1)
1963 fatal("%s: imsg_get error", __func__);
1964 if (n == 0) /* No more messages. */
1965 return;
1967 evtimer_del(&client->tmo);
1969 switch (imsg.hdr.type) {
1970 case GOTD_IMSG_ERROR:
1971 do_disconnect = 1;
1972 err = gotd_imsg_recv_error(&client_id, &imsg);
1973 break;
1974 case GOTD_IMSG_ACCESS_GRANTED:
1975 break;
1976 default:
1977 do_disconnect = 1;
1978 log_debug("unexpected imsg %d", imsg.hdr.type);
1979 break;
1982 if (!verify_imsg_src(client, client->auth, &imsg)) {
1983 do_disconnect = 1;
1984 log_debug("dropping imsg type %d from PID %d",
1985 imsg.hdr.type, client->auth->pid);
1987 imsg_free(&imsg);
1989 if (do_disconnect) {
1990 if (err)
1991 disconnect_on_error(client, err);
1992 else
1993 disconnect(client);
1994 goto done;
1997 repo = find_repo_by_name(client->auth->repo_name);
1998 if (repo == NULL) {
1999 err = got_error(GOT_ERR_NOT_GIT_REPO);
2000 goto done;
2002 kill_auth_proc(client);
2004 log_info("authenticated uid %d for repository %s\n",
2005 client->euid, repo->name);
2007 if (client->required_auth & GOTD_AUTH_WRITE)
2008 proc_type = PROC_REPO_WRITE;
2009 else
2010 proc_type = PROC_REPO_READ;
2012 err = start_repo_child(client, proc_type, repo, gotd.argv0,
2013 gotd.confpath, gotd.daemonize, gotd.verbosity);
2014 done:
2015 if (err)
2016 log_warnx("uid %d: %s", client->euid, err->msg);
2018 /* We might have killed the auth process by now. */
2019 if (client->auth != NULL) {
2020 if (!shut) {
2021 gotd_imsg_event_add(iev);
2022 } else {
2023 /* This pipe is dead. Remove its event handler */
2024 event_del(&iev->ev);
2029 static void
2030 gotd_dispatch_repo_child(int fd, short event, void *arg)
2032 struct gotd_imsgev *iev = arg;
2033 struct imsgbuf *ibuf = &iev->ibuf;
2034 struct gotd_child_proc *proc = NULL;
2035 struct gotd_client *client = NULL;
2036 ssize_t n;
2037 int shut = 0;
2038 struct imsg imsg;
2040 if (event & EV_READ) {
2041 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
2042 fatal("imsg_read error");
2043 if (n == 0) {
2044 /* Connection closed. */
2045 shut = 1;
2046 goto done;
2050 if (event & EV_WRITE) {
2051 n = msgbuf_write(&ibuf->w);
2052 if (n == -1 && errno != EAGAIN)
2053 fatal("msgbuf_write");
2054 if (n == 0) {
2055 /* Connection closed. */
2056 shut = 1;
2057 goto done;
2061 client = find_client_by_proc_fd(fd);
2062 if (client == NULL)
2063 fatalx("cannot find client for fd %d", fd);
2065 proc = get_client_proc(client);
2066 if (proc == NULL)
2067 fatalx("cannot find child process for fd %d", fd);
2069 for (;;) {
2070 const struct got_error *err = NULL;
2071 uint32_t client_id = 0;
2072 int do_disconnect = 0;
2073 int do_list_refs = 0, do_ref_updates = 0, do_ref_update = 0;
2074 int do_packfile_install = 0;
2076 if ((n = imsg_get(ibuf, &imsg)) == -1)
2077 fatal("%s: imsg_get error", __func__);
2078 if (n == 0) /* No more messages. */
2079 break;
2081 switch (imsg.hdr.type) {
2082 case GOTD_IMSG_ERROR:
2083 do_disconnect = 1;
2084 err = gotd_imsg_recv_error(&client_id, &imsg);
2085 break;
2086 case GOTD_IMSG_REPO_CHILD_READY:
2087 do_list_refs = 1;
2088 break;
2089 case GOTD_IMSG_PACKFILE_DONE:
2090 do_disconnect = 1;
2091 err = recv_packfile_done(&client_id, &imsg);
2092 break;
2093 case GOTD_IMSG_PACKFILE_INSTALL:
2094 err = recv_packfile_install(&client_id, &imsg);
2095 if (err == NULL)
2096 do_packfile_install = 1;
2097 break;
2098 case GOTD_IMSG_REF_UPDATES_START:
2099 err = recv_ref_updates_start(&client_id, &imsg);
2100 if (err == NULL)
2101 do_ref_updates = 1;
2102 break;
2103 case GOTD_IMSG_REF_UPDATE:
2104 err = recv_ref_update(&client_id, &imsg);
2105 if (err == NULL)
2106 do_ref_update = 1;
2107 break;
2108 default:
2109 log_debug("unexpected imsg %d", imsg.hdr.type);
2110 break;
2113 if (!verify_imsg_src(client, proc, &imsg)) {
2114 log_debug("dropping imsg type %d from PID %d",
2115 imsg.hdr.type, proc->pid);
2116 imsg_free(&imsg);
2117 continue;
2119 if (err)
2120 log_warnx("uid %d: %s", client->euid, err->msg);
2122 if (do_disconnect) {
2123 if (err)
2124 disconnect_on_error(client, err);
2125 else
2126 disconnect(client);
2127 } else {
2128 if (do_list_refs)
2129 err = list_refs_request(client, iev);
2130 else if (do_packfile_install)
2131 err = install_pack(client, proc->repo_path,
2132 &imsg);
2133 else if (do_ref_updates)
2134 err = begin_ref_updates(client, &imsg);
2135 else if (do_ref_update)
2136 err = update_ref(client, proc->repo_path,
2137 &imsg);
2138 if (err)
2139 log_warnx("uid %d: %s", client->euid, err->msg);
2141 imsg_free(&imsg);
2143 done:
2144 if (!shut) {
2145 gotd_imsg_event_add(iev);
2146 } else {
2147 /* This pipe is dead. Remove its event handler */
2148 event_del(&iev->ev);
2149 event_loopexit(NULL);
2153 static pid_t
2154 start_child(enum gotd_procid proc_id, const char *repo_path,
2155 char *argv0, const char *confpath, int fd, int daemonize, int verbosity)
2157 char *argv[11];
2158 int argc = 0;
2159 pid_t pid;
2161 switch (pid = fork()) {
2162 case -1:
2163 fatal("cannot fork");
2164 case 0:
2165 break;
2166 default:
2167 close(fd);
2168 return pid;
2171 if (fd != GOTD_FILENO_MSG_PIPE) {
2172 if (dup2(fd, GOTD_FILENO_MSG_PIPE) == -1)
2173 fatal("cannot setup imsg fd");
2174 } else if (fcntl(fd, F_SETFD, 0) == -1)
2175 fatal("cannot setup imsg fd");
2177 argv[argc++] = argv0;
2178 switch (proc_id) {
2179 case PROC_LISTEN:
2180 argv[argc++] = (char *)"-L";
2181 break;
2182 case PROC_AUTH:
2183 argv[argc++] = (char *)"-A";
2184 break;
2185 case PROC_REPO_READ:
2186 argv[argc++] = (char *)"-R";
2187 break;
2188 case PROC_REPO_WRITE:
2189 argv[argc++] = (char *)"-W";
2190 break;
2191 default:
2192 fatalx("invalid process id %d", proc_id);
2195 argv[argc++] = (char *)"-f";
2196 argv[argc++] = (char *)confpath;
2198 if (repo_path) {
2199 argv[argc++] = (char *)"-P";
2200 argv[argc++] = (char *)repo_path;
2203 if (!daemonize)
2204 argv[argc++] = (char *)"-d";
2205 if (verbosity > 0)
2206 argv[argc++] = (char *)"-v";
2207 if (verbosity > 1)
2208 argv[argc++] = (char *)"-v";
2209 argv[argc++] = NULL;
2211 execvp(argv0, argv);
2212 fatal("execvp");
2215 static void
2216 start_listener(char *argv0, const char *confpath, int daemonize, int verbosity)
2218 struct gotd_child_proc *proc = &gotd.listen_proc;
2220 proc->type = PROC_LISTEN;
2222 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
2223 PF_UNSPEC, proc->pipe) == -1)
2224 fatal("socketpair");
2226 proc->pid = start_child(proc->type, NULL, argv0, confpath,
2227 proc->pipe[1], daemonize, verbosity);
2228 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
2229 proc->iev.handler = gotd_dispatch_listener;
2230 proc->iev.events = EV_READ;
2231 proc->iev.handler_arg = NULL;
2234 static const struct got_error *
2235 start_repo_child(struct gotd_client *client, enum gotd_procid proc_type,
2236 struct gotd_repo *repo, char *argv0, const char *confpath,
2237 int daemonize, int verbosity)
2239 struct gotd_child_proc *proc;
2241 if (proc_type != PROC_REPO_READ && proc_type != PROC_REPO_WRITE)
2242 return got_error_msg(GOT_ERR_NOT_IMPL, "bad process type");
2244 proc = calloc(1, sizeof(*proc));
2245 if (proc == NULL)
2246 return got_error_from_errno("calloc");
2248 proc->type = proc_type;
2249 if (strlcpy(proc->repo_name, repo->name,
2250 sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
2251 fatalx("repository name too long: %s", repo->name);
2252 log_debug("starting %s for repository %s",
2253 proc->type == PROC_REPO_READ ? "reader" : "writer", repo->name);
2254 if (realpath(repo->path, proc->repo_path) == NULL)
2255 fatal("%s", repo->path);
2256 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
2257 PF_UNSPEC, proc->pipe) == -1)
2258 fatal("socketpair");
2259 proc->pid = start_child(proc->type, proc->repo_path, argv0,
2260 confpath, proc->pipe[1], daemonize, verbosity);
2261 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
2262 log_debug("proc %s %s is on fd %d",
2263 gotd_proc_names[proc->type], proc->repo_path,
2264 proc->pipe[0]);
2265 proc->iev.handler = gotd_dispatch_repo_child;
2266 proc->iev.events = EV_READ;
2267 proc->iev.handler_arg = NULL;
2268 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
2269 gotd_dispatch_repo_child, &proc->iev);
2270 gotd_imsg_event_add(&proc->iev);
2272 if (proc->type == PROC_REPO_READ)
2273 client->repo_read = proc;
2274 else
2275 client->repo_write = proc;
2277 return NULL;
2280 static const struct got_error *
2281 start_auth_child(struct gotd_client *client, int required_auth,
2282 struct gotd_repo *repo, char *argv0, const char *confpath,
2283 int daemonize, int verbosity)
2285 const struct got_error *err = NULL;
2286 struct gotd_child_proc *proc;
2287 struct gotd_imsg_auth iauth;
2288 int fd;
2290 memset(&iauth, 0, sizeof(iauth));
2292 fd = dup(client->fd);
2293 if (fd == -1)
2294 return got_error_from_errno("dup");
2296 proc = calloc(1, sizeof(*proc));
2297 if (proc == NULL) {
2298 err = got_error_from_errno("calloc");
2299 close(fd);
2300 return err;
2303 proc->type = PROC_AUTH;
2304 if (strlcpy(proc->repo_name, repo->name,
2305 sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
2306 fatalx("repository name too long: %s", repo->name);
2307 log_debug("starting auth for uid %d repository %s",
2308 client->euid, repo->name);
2309 if (realpath(repo->path, proc->repo_path) == NULL)
2310 fatal("%s", repo->path);
2311 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
2312 PF_UNSPEC, proc->pipe) == -1)
2313 fatal("socketpair");
2314 proc->pid = start_child(proc->type, proc->repo_path, argv0,
2315 confpath, proc->pipe[1], daemonize, verbosity);
2316 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
2317 log_debug("proc %s %s is on fd %d",
2318 gotd_proc_names[proc->type], proc->repo_path,
2319 proc->pipe[0]);
2320 proc->iev.handler = gotd_dispatch_auth_child;
2321 proc->iev.events = EV_READ;
2322 proc->iev.handler_arg = NULL;
2323 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
2324 gotd_dispatch_auth_child, &proc->iev);
2325 gotd_imsg_event_add(&proc->iev);
2327 iauth.euid = client->euid;
2328 iauth.egid = client->egid;
2329 iauth.required_auth = required_auth;
2330 iauth.client_id = client->id;
2331 if (gotd_imsg_compose_event(&proc->iev, GOTD_IMSG_AUTHENTICATE,
2332 PROC_GOTD, fd, &iauth, sizeof(iauth)) == -1) {
2333 log_warn("imsg compose AUTHENTICATE");
2334 close(fd);
2335 /* Let the auth_timeout handler tidy up. */
2338 client->auth = proc;
2339 client->required_auth = required_auth;
2340 return NULL;
2343 static void
2344 apply_unveil_repo_readonly(const char *repo_path)
2346 if (unveil(repo_path, "r") == -1)
2347 fatal("unveil %s", repo_path);
2349 if (unveil(NULL, NULL) == -1)
2350 fatal("unveil");
2353 static void
2354 apply_unveil_none(void)
2356 if (unveil("/", "") == -1)
2357 fatal("unveil");
2359 if (unveil(NULL, NULL) == -1)
2360 fatal("unveil");
2363 static void
2364 apply_unveil(void)
2366 struct gotd_repo *repo;
2368 if (unveil(gotd.argv0, "x") == -1)
2369 fatal("unveil %s", gotd.argv0);
2371 TAILQ_FOREACH(repo, &gotd.repos, entry) {
2372 if (unveil(repo->path, "rwc") == -1)
2373 fatal("unveil %s", repo->path);
2376 if (unveil(GOT_TMPDIR_STR, "rwc") == -1)
2377 fatal("unveil %s", GOT_TMPDIR_STR);
2379 if (unveil(NULL, NULL) == -1)
2380 fatal("unveil");
2383 int
2384 main(int argc, char **argv)
2386 const struct got_error *error = NULL;
2387 int ch, fd = -1, daemonize = 1, verbosity = 0, noaction = 0;
2388 const char *confpath = GOTD_CONF_PATH;
2389 char *argv0 = argv[0];
2390 char title[2048];
2391 struct passwd *pw = NULL;
2392 char *repo_path = NULL;
2393 enum gotd_procid proc_id = PROC_GOTD;
2394 struct event evsigint, evsigterm, evsighup, evsigusr1;
2395 int *pack_fds = NULL, *temp_fds = NULL;
2397 log_init(1, LOG_DAEMON); /* Log to stderr until daemonized. */
2399 while ((ch = getopt(argc, argv, "Adf:LnP:RvW")) != -1) {
2400 switch (ch) {
2401 case 'A':
2402 proc_id = PROC_AUTH;
2403 break;
2404 case 'd':
2405 daemonize = 0;
2406 break;
2407 case 'f':
2408 confpath = optarg;
2409 break;
2410 case 'L':
2411 proc_id = PROC_LISTEN;
2412 break;
2413 case 'n':
2414 noaction = 1;
2415 break;
2416 case 'P':
2417 repo_path = realpath(optarg, NULL);
2418 if (repo_path == NULL)
2419 fatal("realpath '%s'", optarg);
2420 break;
2421 case 'R':
2422 proc_id = PROC_REPO_READ;
2423 break;
2424 case 'v':
2425 if (verbosity < 3)
2426 verbosity++;
2427 break;
2428 case 'W':
2429 proc_id = PROC_REPO_WRITE;
2430 break;
2431 default:
2432 usage();
2436 argc -= optind;
2437 argv += optind;
2439 if (argc != 0)
2440 usage();
2442 /* Require an absolute path in argv[0] for reliable re-exec. */
2443 if (!got_path_is_absolute(argv0))
2444 fatalx("bad path \"%s\": must be an absolute path", argv0);
2446 if (geteuid() && (proc_id == PROC_GOTD || proc_id == PROC_LISTEN))
2447 fatalx("need root privileges");
2449 log_init(daemonize ? 0 : 1, LOG_DAEMON);
2450 log_setverbose(verbosity);
2452 if (parse_config(confpath, proc_id, &gotd) != 0)
2453 return 1;
2455 gotd.argv0 = argv0;
2456 gotd.daemonize = daemonize;
2457 gotd.verbosity = verbosity;
2458 gotd.confpath = confpath;
2460 if (proc_id == PROC_GOTD &&
2461 (gotd.nrepos == 0 || TAILQ_EMPTY(&gotd.repos)))
2462 fatalx("no repository defined in configuration file");
2464 pw = getpwnam(gotd.user_name);
2465 if (pw == NULL)
2466 fatalx("user %s not found", gotd.user_name);
2468 if (pw->pw_uid == 0) {
2469 fatalx("cannot run %s as %s: the user running %s "
2470 "must not be the superuser",
2471 getprogname(), pw->pw_name, getprogname());
2474 if (proc_id == PROC_LISTEN &&
2475 !got_path_is_absolute(gotd.unix_socket_path))
2476 fatalx("bad unix socket path \"%s\": must be an absolute path",
2477 gotd.unix_socket_path);
2479 if (noaction)
2480 return 0;
2482 if (proc_id == PROC_GOTD) {
2483 gotd.pid = getpid();
2484 snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
2485 start_listener(argv0, confpath, daemonize, verbosity);
2486 arc4random_buf(&clients_hash_key, sizeof(clients_hash_key));
2487 if (daemonize && daemon(1, 0) == -1)
2488 fatal("daemon");
2489 } else if (proc_id == PROC_LISTEN) {
2490 snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
2491 if (verbosity) {
2492 log_info("socket: %s", gotd.unix_socket_path);
2493 log_info("user: %s", pw->pw_name);
2496 fd = unix_socket_listen(gotd.unix_socket_path, pw->pw_uid,
2497 pw->pw_gid);
2498 if (fd == -1) {
2499 fatal("cannot listen on unix socket %s",
2500 gotd.unix_socket_path);
2502 if (daemonize && daemon(0, 0) == -1)
2503 fatal("daemon");
2504 } else if (proc_id == PROC_AUTH) {
2505 snprintf(title, sizeof(title), "%s %s",
2506 gotd_proc_names[proc_id], repo_path);
2507 if (daemonize && daemon(0, 0) == -1)
2508 fatal("daemon");
2509 } else if (proc_id == PROC_REPO_READ || proc_id == PROC_REPO_WRITE) {
2510 error = got_repo_pack_fds_open(&pack_fds);
2511 if (error != NULL)
2512 fatalx("cannot open pack tempfiles: %s", error->msg);
2513 error = got_repo_temp_fds_open(&temp_fds);
2514 if (error != NULL)
2515 fatalx("cannot open pack tempfiles: %s", error->msg);
2516 if (repo_path == NULL)
2517 fatalx("repository path not specified");
2518 snprintf(title, sizeof(title), "%s %s",
2519 gotd_proc_names[proc_id], repo_path);
2520 if (daemonize && daemon(0, 0) == -1)
2521 fatal("daemon");
2522 } else
2523 fatal("invalid process id %d", proc_id);
2525 setproctitle("%s", title);
2526 log_procinit(title);
2528 /* Drop root privileges. */
2529 if (setgid(pw->pw_gid) == -1)
2530 fatal("setgid %d failed", pw->pw_gid);
2531 if (setuid(pw->pw_uid) == -1)
2532 fatal("setuid %d failed", pw->pw_uid);
2534 event_init();
2536 switch (proc_id) {
2537 case PROC_GOTD:
2538 #ifndef PROFILE
2539 if (pledge("stdio rpath wpath cpath proc exec "
2540 "sendfd recvfd fattr flock unveil", NULL) == -1)
2541 err(1, "pledge");
2542 #endif
2543 break;
2544 case PROC_LISTEN:
2545 #ifndef PROFILE
2546 if (pledge("stdio sendfd unix unveil", NULL) == -1)
2547 err(1, "pledge");
2548 #endif
2550 * Ensure that AF_UNIX bind(2) cannot be used with any other
2551 * sockets by revoking all filesystem access via unveil(2).
2553 apply_unveil_none();
2555 listen_main(title, fd, gotd.connection_limits,
2556 gotd.nconnection_limits);
2557 /* NOTREACHED */
2558 break;
2559 case PROC_AUTH:
2560 #ifndef PROFILE
2561 if (pledge("stdio getpw recvfd unix unveil", NULL) == -1)
2562 err(1, "pledge");
2563 #endif
2565 * We need the "unix" pledge promise for getpeername(2) only.
2566 * Ensure that AF_UNIX bind(2) cannot be used by revoking all
2567 * filesystem access via unveil(2). Access to password database
2568 * files will still work since "getpw" bypasses unveil(2).
2570 apply_unveil_none();
2572 auth_main(title, &gotd.repos, repo_path);
2573 /* NOTREACHED */
2574 break;
2575 case PROC_REPO_READ:
2576 #ifndef PROFILE
2577 if (pledge("stdio rpath recvfd unveil", NULL) == -1)
2578 err(1, "pledge");
2579 #endif
2580 apply_unveil_repo_readonly(repo_path);
2581 repo_read_main(title, repo_path, pack_fds, temp_fds);
2582 /* NOTREACHED */
2583 exit(0);
2584 case PROC_REPO_WRITE:
2585 #ifndef PROFILE
2586 if (pledge("stdio rpath recvfd unveil", NULL) == -1)
2587 err(1, "pledge");
2588 #endif
2589 apply_unveil_repo_readonly(repo_path);
2590 repo_write_main(title, repo_path, pack_fds, temp_fds);
2591 /* NOTREACHED */
2592 exit(0);
2593 default:
2594 fatal("invalid process id %d", proc_id);
2597 if (proc_id != PROC_GOTD)
2598 fatal("invalid process id %d", proc_id);
2600 apply_unveil();
2602 signal_set(&evsigint, SIGINT, gotd_sighdlr, NULL);
2603 signal_set(&evsigterm, SIGTERM, gotd_sighdlr, NULL);
2604 signal_set(&evsighup, SIGHUP, gotd_sighdlr, NULL);
2605 signal_set(&evsigusr1, SIGUSR1, gotd_sighdlr, NULL);
2606 signal(SIGPIPE, SIG_IGN);
2608 signal_add(&evsigint, NULL);
2609 signal_add(&evsigterm, NULL);
2610 signal_add(&evsighup, NULL);
2611 signal_add(&evsigusr1, NULL);
2613 gotd_imsg_event_add(&gotd.listen_proc.iev);
2615 event_dispatch();
2617 if (pack_fds)
2618 got_repo_pack_fds_close(pack_fds);
2619 free(repo_path);
2620 return 0;