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 <grp.h>
33 #include <imsg.h>
34 #include <sha1.h>
35 #include <signal.h>
36 #include <siphash.h>
37 #include <stdarg.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <syslog.h>
43 #include <unistd.h>
45 #include "got_error.h"
46 #include "got_opentemp.h"
47 #include "got_path.h"
48 #include "got_repository.h"
49 #include "got_object.h"
50 #include "got_reference.h"
52 #include "got_lib_delta.h"
53 #include "got_lib_object.h"
54 #include "got_lib_object_cache.h"
55 #include "got_lib_sha1.h"
56 #include "got_lib_gitproto.h"
57 #include "got_lib_pack.h"
58 #include "got_lib_repository.h"
60 #include "gotd.h"
61 #include "log.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 char *packfile_path;
85 char *packidx_path;
86 int nref_updates;
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 timeout = { 3600, 0 };
94 static int inflight;
95 static struct gotd gotd;
97 void gotd_sighdlr(int sig, short event, void *arg);
99 __dead static void
100 usage()
102 fprintf(stderr, "usage: %s [-dv] [-f config-file]\n", getprogname());
103 exit(1);
106 static int
107 unix_socket_listen(const char *unix_socket_path, uid_t uid, gid_t gid)
109 struct sockaddr_un sun;
110 int fd = -1;
111 mode_t old_umask, mode;
113 fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK| SOCK_CLOEXEC, 0);
114 if (fd == -1) {
115 log_warn("socket");
116 return -1;
119 sun.sun_family = AF_UNIX;
120 if (strlcpy(sun.sun_path, unix_socket_path,
121 sizeof(sun.sun_path)) >= sizeof(sun.sun_path)) {
122 log_warnx("%s: name too long", unix_socket_path);
123 close(fd);
124 return -1;
127 if (unlink(unix_socket_path) == -1) {
128 if (errno != ENOENT) {
129 log_warn("unlink %s", unix_socket_path);
130 close(fd);
131 return -1;
135 old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
136 mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP;
138 if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
139 log_warn("bind: %s", unix_socket_path);
140 close(fd);
141 umask(old_umask);
142 return -1;
145 umask(old_umask);
147 if (chmod(unix_socket_path, mode) == -1) {
148 log_warn("chmod %o %s", mode, unix_socket_path);
149 close(fd);
150 unlink(unix_socket_path);
151 return -1;
154 if (chown(unix_socket_path, uid, gid) == -1) {
155 log_warn("chown %s uid=%d gid=%d", unix_socket_path, uid, gid);
156 close(fd);
157 unlink(unix_socket_path);
158 return -1;
161 if (listen(fd, GOTD_UNIX_SOCKET_BACKLOG) == -1) {
162 log_warn("listen");
163 close(fd);
164 unlink(unix_socket_path);
165 return -1;
168 return fd;
171 static struct group *
172 match_group(gid_t *groups, int ngroups, const char *unix_group_name)
174 struct group *gr;
175 int i;
177 for (i = 0; i < ngroups; i++) {
178 gr = getgrgid(groups[i]);
179 if (gr == NULL) {
180 log_warn("getgrgid %d", groups[i]);
181 continue;
183 if (strcmp(gr->gr_name, unix_group_name) == 0)
184 return gr;
187 return NULL;
190 static int
191 accept_reserve(int fd, struct sockaddr *addr, socklen_t *addrlen,
192 int reserve, volatile int *counter)
194 int ret;
196 if (getdtablecount() + reserve +
197 ((*counter + 1) * GOTD_FD_NEEDED) >= getdtablesize()) {
198 log_debug("inflight fds exceeded");
199 errno = EMFILE;
200 return -1;
203 if ((ret = accept4(fd, addr, addrlen,
204 SOCK_NONBLOCK | SOCK_CLOEXEC)) > -1) {
205 (*counter)++;
208 return ret;
211 static uint64_t
212 client_hash(uint32_t client_id)
214 return SipHash24(&clients_hash_key, &client_id, sizeof(client_id));
217 static void
218 add_client(struct gotd_client *client)
220 uint64_t slot = client_hash(client->id) % nitems(gotd_clients);
221 STAILQ_INSERT_HEAD(&gotd_clients[slot], client, entry);
222 client_cnt++;
225 static struct gotd_client *
226 find_client(uint32_t client_id)
228 uint64_t slot;
229 struct gotd_client *c;
231 slot = client_hash(client_id) % nitems(gotd_clients);
232 STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
233 if (c->id == client_id)
234 return c;
237 return NULL;
240 static uint32_t
241 get_client_id(void)
243 int duplicate = 0;
244 uint32_t id;
246 do {
247 id = arc4random();
248 duplicate = (find_client(id) != NULL);
249 } while (duplicate || id == 0);
251 return id;
254 static struct gotd_child_proc *
255 get_client_proc(struct gotd_client *client)
257 if (client->repo_read && client->repo_write) {
258 fatalx("uid %d is reading and writing in the same session",
259 client->euid);
260 /* NOTREACHED */
263 if (client->repo_read)
264 return client->repo_read;
265 else if (client->repo_write)
266 return client->repo_write;
267 else {
268 fatal("uid %d is neither reading nor writing", client->euid);
269 /* NOTREACHED */
271 return NULL;
274 static int
275 client_is_reading(struct gotd_client *client)
277 return client->repo_read != NULL;
280 static int
281 client_is_writing(struct gotd_client *client)
283 return client->repo_write != NULL;
286 static const struct got_error *
287 ensure_client_is_reading(struct gotd_client *client)
289 if (!client_is_reading(client)) {
290 return got_error_fmt(GOT_ERR_BAD_PACKET,
291 "uid %d made a read-request but is not reading from "
292 "a repository", client->euid);
295 return NULL;
298 static const struct got_error *
299 ensure_client_is_writing(struct gotd_client *client)
301 if (!client_is_writing(client)) {
302 return got_error_fmt(GOT_ERR_BAD_PACKET,
303 "uid %d made a write-request but is not writing to "
304 "a repository", client->euid);
307 return NULL;
310 static const struct got_error *
311 ensure_client_is_not_writing(struct gotd_client *client)
313 if (client_is_writing(client)) {
314 return got_error_fmt(GOT_ERR_BAD_PACKET,
315 "uid %d made a read-request but is writing to "
316 "a repository", client->euid);
319 return NULL;
322 static const struct got_error *
323 ensure_client_is_not_reading(struct gotd_client *client)
325 if (client_is_reading(client)) {
326 return got_error_fmt(GOT_ERR_BAD_PACKET,
327 "uid %d made a write-request but is reading from "
328 "a repository", client->euid);
331 return NULL;
334 static void
335 disconnect(struct gotd_client *client)
337 struct gotd_imsg_disconnect idisconnect;
338 struct gotd_child_proc *proc = get_client_proc(client);
339 uint64_t slot;
341 log_debug("uid %d: disconnecting", client->euid);
343 idisconnect.client_id = client->id;
344 if (gotd_imsg_compose_event(&proc->iev,
345 GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
346 &idisconnect, sizeof(idisconnect)) == -1)
347 log_warn("imsg compose DISCONNECT");
349 slot = client_hash(client->id) % nitems(gotd_clients);
350 STAILQ_REMOVE(&gotd_clients[slot], client, gotd_client, entry);
351 imsg_clear(&client->iev.ibuf);
352 event_del(&client->iev.ev);
353 evtimer_del(&client->tmo);
354 close(client->fd);
355 if (client->delta_cache_fd != -1)
356 close(client->delta_cache_fd);
357 if (client->packfile_path) {
358 if (unlink(client->packfile_path) == -1 && errno != ENOENT)
359 log_warn("unlink %s: ", client->packfile_path);
360 free(client->packfile_path);
362 if (client->packidx_path) {
363 if (unlink(client->packidx_path) == -1 && errno != ENOENT)
364 log_warn("unlink %s: ", client->packidx_path);
365 free(client->packidx_path);
367 free(client->capabilities);
368 free(client);
369 inflight--;
370 client_cnt--;
373 static void
374 disconnect_on_error(struct gotd_client *client, const struct got_error *err)
376 struct imsgbuf ibuf;
378 log_warnx("uid %d: %s", client->euid, err->msg);
379 if (err->code != GOT_ERR_EOF) {
380 imsg_init(&ibuf, client->fd);
381 gotd_imsg_send_error(&ibuf, 0, PROC_GOTD, err);
382 imsg_clear(&ibuf);
384 disconnect(client);
387 static struct gotd_child_proc *
388 find_proc_by_repo_name(enum gotd_procid proc_id, const char *repo_name)
390 struct gotd_child_proc *proc;
391 int i;
392 size_t namelen;
394 for (i = 0; i < gotd.nprocs; i++) {
395 proc = &gotd.procs[i];
396 if (proc->type != proc_id)
397 continue;
398 namelen = strlen(proc->repo_name);
399 if (strncmp(proc->repo_name, repo_name, namelen) != 0)
400 continue;
401 if (repo_name[namelen] == '\0' ||
402 strcmp(&repo_name[namelen], ".git") == 0)
403 return proc;
406 return NULL;
409 static struct gotd_child_proc *
410 find_proc_by_fd(int fd)
412 struct gotd_child_proc *proc;
413 int i;
415 for (i = 0; i < gotd.nprocs; i++) {
416 proc = &gotd.procs[i];
417 if (proc->iev.ibuf.fd == fd)
418 return proc;
421 return NULL;
424 static const struct got_error *
425 forward_list_refs_request(struct gotd_client *client, struct imsg *imsg)
427 const struct got_error *err;
428 struct gotd_imsg_list_refs ireq;
429 struct gotd_imsg_list_refs_internal ilref;
430 struct gotd_child_proc *proc = NULL;
431 size_t datalen;
432 int fd = -1;
434 log_debug("list-refs request from uid %d", client->euid);
436 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
437 if (datalen != sizeof(ireq))
438 return got_error(GOT_ERR_PRIVSEP_LEN);
440 memcpy(&ireq, imsg->data, datalen);
442 memset(&ilref, 0, sizeof(ilref));
443 ilref.client_id = client->id;
445 if (ireq.client_is_reading) {
446 err = ensure_client_is_not_writing(client);
447 if (err)
448 return err;
449 client->repo_read = find_proc_by_repo_name(PROC_REPO_READ,
450 ireq.repo_name);
451 if (client->repo_read == NULL)
452 return got_error(GOT_ERR_NOT_GIT_REPO);
453 } else {
454 err = ensure_client_is_not_reading(client);
455 if (err)
456 return err;
457 client->repo_write = find_proc_by_repo_name(PROC_REPO_WRITE,
458 ireq.repo_name);
459 if (client->repo_write == NULL)
460 return got_error(GOT_ERR_NOT_GIT_REPO);
463 fd = dup(client->fd);
464 if (fd == -1)
465 return got_error_from_errno("dup");
467 proc = get_client_proc(client);
468 if (gotd_imsg_compose_event(&proc->iev,
469 GOTD_IMSG_LIST_REFS_INTERNAL, PROC_GOTD, fd,
470 &ilref, sizeof(ilref)) == -1) {
471 err = got_error_from_errno("imsg compose WANT");
472 close(fd);
473 return err;
476 return NULL;
479 static const struct got_error *
480 forward_want(struct gotd_client *client, struct imsg *imsg)
482 struct gotd_imsg_want ireq;
483 struct gotd_imsg_want iwant;
484 size_t datalen;
486 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
487 if (datalen != sizeof(ireq))
488 return got_error(GOT_ERR_PRIVSEP_LEN);
490 memcpy(&ireq, imsg->data, datalen);
492 memset(&iwant, 0, sizeof(iwant));
493 memcpy(iwant.object_id, ireq.object_id, SHA1_DIGEST_LENGTH);
494 iwant.client_id = client->id;
496 if (gotd_imsg_compose_event(&client->repo_read->iev, GOTD_IMSG_WANT,
497 PROC_GOTD, -1, &iwant, sizeof(iwant)) == -1)
498 return got_error_from_errno("imsg compose WANT");
500 return NULL;
503 static const struct got_error *
504 forward_ref_update(struct gotd_client *client, struct imsg *imsg)
506 const struct got_error *err = NULL;
507 struct gotd_imsg_ref_update ireq;
508 struct gotd_imsg_ref_update *iref = NULL;
509 size_t datalen;
511 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
512 if (datalen < sizeof(ireq))
513 return got_error(GOT_ERR_PRIVSEP_LEN);
514 memcpy(&ireq, imsg->data, sizeof(ireq));
515 if (datalen != sizeof(ireq) + ireq.name_len)
516 return got_error(GOT_ERR_PRIVSEP_LEN);
518 iref = malloc(datalen);
519 if (iref == NULL)
520 return got_error_from_errno("malloc");
521 memcpy(iref, imsg->data, datalen);
523 iref->client_id = client->id;
524 if (gotd_imsg_compose_event(&client->repo_write->iev,
525 GOTD_IMSG_REF_UPDATE, PROC_GOTD, -1, iref, datalen) == -1)
526 err = got_error_from_errno("imsg compose REF_UPDATE");
527 free(iref);
528 return err;
531 static const struct got_error *
532 forward_have(struct gotd_client *client, struct imsg *imsg)
534 struct gotd_imsg_have ireq;
535 struct gotd_imsg_have ihave;
536 size_t datalen;
538 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
539 if (datalen != sizeof(ireq))
540 return got_error(GOT_ERR_PRIVSEP_LEN);
542 memcpy(&ireq, imsg->data, datalen);
544 memset(&ihave, 0, sizeof(ihave));
545 memcpy(ihave.object_id, ireq.object_id, SHA1_DIGEST_LENGTH);
546 ihave.client_id = client->id;
548 if (gotd_imsg_compose_event(&client->repo_read->iev, GOTD_IMSG_HAVE,
549 PROC_GOTD, -1, &ihave, sizeof(ihave)) == -1)
550 return got_error_from_errno("imsg compose HAVE");
552 return NULL;
555 static int
556 client_has_capability(struct gotd_client *client, const char *capastr)
558 struct gotd_client_capability *capa;
559 size_t i;
561 if (client->ncapabilities == 0)
562 return 0;
564 for (i = 0; i < client->ncapabilities; i++) {
565 capa = &client->capabilities[i];
566 if (strcmp(capa->key, capastr) == 0)
567 return 1;
570 return 0;
573 static const struct got_error *
574 recv_capabilities(struct gotd_client *client, struct imsg *imsg)
576 struct gotd_imsg_capabilities icapas;
577 size_t datalen;
579 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
580 if (datalen != sizeof(icapas))
581 return got_error(GOT_ERR_PRIVSEP_LEN);
582 memcpy(&icapas, imsg->data, sizeof(icapas));
584 client->ncapa_alloc = icapas.ncapabilities;
585 client->capabilities = calloc(client->ncapa_alloc,
586 sizeof(*client->capabilities));
587 if (client->capabilities == NULL) {
588 client->ncapa_alloc = 0;
589 return got_error_from_errno("calloc");
592 log_debug("expecting %zu capabilities from uid %d",
593 client->ncapa_alloc, client->euid);
594 return NULL;
597 static const struct got_error *
598 recv_capability(struct gotd_client *client, struct imsg *imsg)
600 struct gotd_imsg_capability icapa;
601 struct gotd_client_capability *capa;
602 size_t datalen;
603 char *key, *value = NULL;
605 if (client->capabilities == NULL ||
606 client->ncapabilities >= client->ncapa_alloc) {
607 return got_error_msg(GOT_ERR_BAD_REQUEST,
608 "unexpected capability received");
611 memset(&icapa, 0, sizeof(icapa));
613 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
614 if (datalen < sizeof(icapa))
615 return got_error(GOT_ERR_PRIVSEP_LEN);
616 memcpy(&icapa, imsg->data, sizeof(icapa));
618 if (datalen != sizeof(icapa) + icapa.key_len + icapa.value_len)
619 return got_error(GOT_ERR_PRIVSEP_LEN);
621 key = malloc(icapa.key_len + 1);
622 if (key == NULL)
623 return got_error_from_errno("malloc");
624 if (icapa.value_len > 0) {
625 value = malloc(icapa.value_len + 1);
626 if (value == NULL) {
627 free(key);
628 return got_error_from_errno("malloc");
632 memcpy(key, imsg->data + sizeof(icapa), icapa.key_len);
633 key[icapa.key_len] = '\0';
634 if (value) {
635 memcpy(value, imsg->data + sizeof(icapa) + icapa.key_len,
636 icapa.value_len);
637 value[icapa.value_len] = '\0';
640 capa = &client->capabilities[client->ncapabilities++];
641 capa->key = key;
642 capa->value = value;
644 if (value)
645 log_debug("uid %d: capability %s=%s", client->euid, key, value);
646 else
647 log_debug("uid %d: capability %s", client->euid, key);
649 return NULL;
652 static const struct got_error *
653 send_packfile(struct gotd_client *client)
655 const struct got_error *err = NULL;
656 struct gotd_imsg_send_packfile ipack;
657 struct gotd_imsg_packfile_pipe ipipe;
658 int pipe[2];
660 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe) == -1)
661 return got_error_from_errno("socketpair");
663 memset(&ipack, 0, sizeof(ipack));
664 memset(&ipipe, 0, sizeof(ipipe));
666 ipack.client_id = client->id;
667 if (client_has_capability(client, GOT_CAPA_SIDE_BAND_64K))
668 ipack.report_progress = 1;
670 client->delta_cache_fd = got_opentempfd();
671 if (client->delta_cache_fd == -1)
672 return got_error_from_errno("got_opentempfd");
674 if (gotd_imsg_compose_event(&client->repo_read->iev,
675 GOTD_IMSG_SEND_PACKFILE, PROC_GOTD, client->delta_cache_fd,
676 &ipack, sizeof(ipack)) == -1) {
677 err = got_error_from_errno("imsg compose SEND_PACKFILE");
678 close(pipe[0]);
679 close(pipe[1]);
680 return err;
683 ipipe.client_id = client->id;
685 /* Send pack pipe end 0 to repo_read. */
686 if (gotd_imsg_compose_event(&client->repo_read->iev,
687 GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[0],
688 &ipipe, sizeof(ipipe)) == -1) {
689 err = got_error_from_errno("imsg compose PACKFILE_PIPE");
690 close(pipe[1]);
691 return err;
694 /* Send pack pipe end 1 to gotsh(1) (expects just an fd, no data). */
695 if (gotd_imsg_compose_event(&client->iev,
696 GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[1], NULL, 0) == -1)
697 err = got_error_from_errno("imsg compose PACKFILE_PIPE");
699 return err;
702 static const struct got_error *
703 recv_packfile(struct gotd_client *client)
705 const struct got_error *err = NULL;
706 struct gotd_imsg_recv_packfile ipack;
707 struct gotd_imsg_packfile_pipe ipipe;
708 struct gotd_imsg_packidx_file ifile;
709 char *basepath = NULL, *pack_path = NULL, *idx_path = NULL;
710 int packfd = -1, idxfd = -1;
711 int pipe[2] = { -1, -1 };
713 if (client->packfile_path) {
714 return got_error_fmt(GOT_ERR_PRIVSEP_MSG,
715 "uid %d already has a pack file", client->euid);
718 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe) == -1)
719 return got_error_from_errno("socketpair");
721 memset(&ipipe, 0, sizeof(ipipe));
722 ipipe.client_id = client->id;
724 if (gotd_imsg_compose_event(&client->repo_write->iev,
725 GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[0],
726 &ipipe, sizeof(ipipe)) == -1) {
727 err = got_error_from_errno("imsg compose PACKFILE_PIPE");
728 pipe[0] = -1;
729 goto done;
731 pipe[0] = -1;
733 if (gotd_imsg_compose_event(&client->repo_write->iev,
734 GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[1],
735 &ipipe, sizeof(ipipe)) == -1)
736 err = got_error_from_errno("imsg compose PACKFILE_PIPE");
737 pipe[1] = -1;
739 if (asprintf(&basepath, "%s/%s/receiving-from-uid-%d.pack",
740 client->repo_write->chroot_path, GOT_OBJECTS_PACK_DIR,
741 client->euid) == -1) {
742 err = got_error_from_errno("asprintf");
743 goto done;
746 err = got_opentemp_named_fd(&pack_path, &packfd, basepath);
747 if (err)
748 goto done;
750 free(basepath);
751 if (asprintf(&basepath, "%s/%s/receiving-from-uid-%d.idx",
752 client->repo_write->chroot_path, GOT_OBJECTS_PACK_DIR,
753 client->euid) == -1) {
754 err = got_error_from_errno("asprintf");
755 basepath = NULL;
756 goto done;
758 err = got_opentemp_named_fd(&idx_path, &idxfd, basepath);
759 if (err)
760 goto done;
762 memset(&ifile, 0, sizeof(ifile));
763 ifile.client_id = client->id;
764 if (gotd_imsg_compose_event(&client->repo_write->iev,
765 GOTD_IMSG_PACKIDX_FILE, PROC_GOTD, idxfd,
766 &ifile, sizeof(ifile)) == -1) {
767 err = got_error_from_errno("imsg compose PACKIDX_FILE");
768 idxfd = -1;
769 goto done;
771 idxfd = -1;
773 memset(&ipack, 0, sizeof(ipack));
774 ipack.client_id = client->id;
775 if (client_has_capability(client, GOT_CAPA_REPORT_STATUS))
776 ipack.report_status = 1;
778 if (gotd_imsg_compose_event(&client->repo_write->iev,
779 GOTD_IMSG_RECV_PACKFILE, PROC_GOTD, packfd,
780 &ipack, sizeof(ipack)) == -1) {
781 err = got_error_from_errno("imsg compose RECV_PACKFILE");
782 packfd = -1;
783 goto done;
785 packfd = -1;
787 done:
788 free(basepath);
789 if (pipe[0] != -1 && close(pipe[0]) == -1 && err == NULL)
790 err = got_error_from_errno("close");
791 if (pipe[1] != -1 && close(pipe[1]) == -1 && err == NULL)
792 err = got_error_from_errno("close");
793 if (packfd != -1 && close(packfd) == -1 && err == NULL)
794 err = got_error_from_errno("close");
795 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
796 err = got_error_from_errno("close");
797 if (err) {
798 free(pack_path);
799 free(idx_path);
800 } else {
801 client->packfile_path = pack_path;
802 client->packidx_path = idx_path;
804 return err;
807 static void
808 gotd_request(int fd, short events, void *arg)
810 struct gotd_imsgev *iev = arg;
811 struct imsgbuf *ibuf = &iev->ibuf;
812 struct gotd_client *client = iev->handler_arg;
813 const struct got_error *err = NULL;
814 struct imsg imsg;
815 ssize_t n;
817 if (events & EV_WRITE) {
818 while (ibuf->w.queued) {
819 n = msgbuf_write(&ibuf->w);
820 if (n == -1 && errno == EPIPE) {
821 /*
822 * The client has closed its socket.
823 * This can happen when Git clients are
824 * done sending pack file data.
825 */
826 msgbuf_clear(&ibuf->w);
827 continue;
828 } else if (n == -1 && errno != EAGAIN) {
829 err = got_error_from_errno("imsg_flush");
830 disconnect_on_error(client, err);
831 return;
833 if (n == 0) {
834 /* Connection closed. */
835 err = got_error(GOT_ERR_EOF);
836 disconnect_on_error(client, err);
837 return;
842 if ((events & EV_READ) == 0)
843 return;
845 memset(&imsg, 0, sizeof(imsg));
847 while (err == NULL) {
848 err = gotd_imsg_recv(&imsg, ibuf, 0);
849 if (err) {
850 if (err->code == GOT_ERR_PRIVSEP_READ)
851 err = NULL;
852 break;
855 evtimer_del(&client->tmo);
857 switch (imsg.hdr.type) {
858 case GOTD_IMSG_LIST_REFS:
859 if (client->state != GOTD_STATE_EXPECT_LIST_REFS) {
860 err = got_error_msg(GOT_ERR_BAD_REQUEST,
861 "unexpected list-refs request received");
862 break;
864 err = forward_list_refs_request(client, &imsg);
865 if (err)
866 break;
867 client->state = GOTD_STATE_EXPECT_CAPABILITIES;
868 log_debug("uid %d: expecting capabilities",
869 client->euid);
870 break;
871 case GOTD_IMSG_CAPABILITIES:
872 if (client->state != GOTD_STATE_EXPECT_CAPABILITIES) {
873 err = got_error_msg(GOT_ERR_BAD_REQUEST,
874 "unexpected capabilities received");
875 break;
877 log_debug("receiving capabilities from uid %d",
878 client->euid);
879 err = recv_capabilities(client, &imsg);
880 break;
881 case GOTD_IMSG_CAPABILITY:
882 if (client->state != GOTD_STATE_EXPECT_CAPABILITIES) {
883 err = got_error_msg(GOT_ERR_BAD_REQUEST,
884 "unexpected capability received");
885 break;
887 err = recv_capability(client, &imsg);
888 if (err || client->ncapabilities < client->ncapa_alloc)
889 break;
890 if (client_is_reading(client)) {
891 client->state = GOTD_STATE_EXPECT_WANT;
892 log_debug("uid %d: expecting want-lines",
893 client->euid);
894 } else if (client_is_writing(client)) {
895 client->state = GOTD_STATE_EXPECT_REF_UPDATE;
896 log_debug("uid %d: expecting ref-update-lines",
897 client->euid);
898 } else
899 fatalx("client %d is both reading and writing",
900 client->euid);
901 break;
902 case GOTD_IMSG_WANT:
903 if (client->state != GOTD_STATE_EXPECT_WANT) {
904 err = got_error_msg(GOT_ERR_BAD_REQUEST,
905 "unexpected want-line received");
906 break;
908 log_debug("received want-line from uid %d",
909 client->euid);
910 err = ensure_client_is_reading(client);
911 if (err)
912 break;
913 err = forward_want(client, &imsg);
914 break;
915 case GOTD_IMSG_REF_UPDATE:
916 if (client->state != GOTD_STATE_EXPECT_REF_UPDATE) {
917 err = got_error_msg(GOT_ERR_BAD_REQUEST,
918 "unexpected ref-update-line received");
919 break;
921 log_debug("received ref-update-line from uid %d",
922 client->euid);
923 err = ensure_client_is_writing(client);
924 if (err)
925 break;
926 err = forward_ref_update(client, &imsg);
927 if (err)
928 break;
929 client->state = GOTD_STATE_EXPECT_MORE_REF_UPDATES;
930 break;
931 case GOTD_IMSG_HAVE:
932 if (client->state != GOTD_STATE_EXPECT_HAVE) {
933 err = got_error_msg(GOT_ERR_BAD_REQUEST,
934 "unexpected have-line received");
935 break;
937 log_debug("received have-line from uid %d",
938 client->euid);
939 err = ensure_client_is_reading(client);
940 if (err)
941 break;
942 err = forward_have(client, &imsg);
943 if (err)
944 break;
945 break;
946 case GOTD_IMSG_FLUSH:
947 if (client->state == GOTD_STATE_EXPECT_WANT ||
948 client->state == GOTD_STATE_EXPECT_HAVE) {
949 err = ensure_client_is_reading(client);
950 if (err)
951 break;
952 } else if (client->state ==
953 GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
954 err = ensure_client_is_writing(client);
955 if (err)
956 break;
957 } else {
958 err = got_error_msg(GOT_ERR_BAD_REQUEST,
959 "unexpected flush-pkt received");
960 break;
962 log_debug("received flush-pkt from uid %d",
963 client->euid);
964 if (client->state == GOTD_STATE_EXPECT_WANT) {
965 client->state = GOTD_STATE_EXPECT_HAVE;
966 log_debug("uid %d: expecting have-lines",
967 client->euid);
968 } else if (client->state == GOTD_STATE_EXPECT_HAVE) {
969 client->state = GOTD_STATE_EXPECT_DONE;
970 log_debug("uid %d: expecting 'done'",
971 client->euid);
972 } else if (client->state ==
973 GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
974 client->state = GOTD_STATE_EXPECT_PACKFILE;
975 log_debug("uid %d: expecting packfile",
976 client->euid);
977 err = recv_packfile(client);
978 } else {
979 /* should not happen, see above */
980 err = got_error_msg(GOT_ERR_BAD_REQUEST,
981 "unexpected client state");
982 break;
984 break;
985 case GOTD_IMSG_DONE:
986 if (client->state != GOTD_STATE_EXPECT_HAVE &&
987 client->state != GOTD_STATE_EXPECT_DONE) {
988 err = got_error_msg(GOT_ERR_BAD_REQUEST,
989 "unexpected flush-pkt received");
990 break;
992 log_debug("received 'done' from uid %d", client->euid);
993 err = ensure_client_is_reading(client);
994 if (err)
995 break;
996 client->state = GOTD_STATE_DONE;
997 err = send_packfile(client);
998 break;
999 default:
1000 err = got_error(GOT_ERR_PRIVSEP_MSG);
1001 break;
1004 imsg_free(&imsg);
1007 if (err) {
1008 if (err->code != GOT_ERR_EOF ||
1009 client->state != GOTD_STATE_EXPECT_PACKFILE)
1010 disconnect_on_error(client, err);
1011 } else {
1012 gotd_imsg_event_add(&client->iev);
1013 evtimer_add(&client->tmo, &timeout);
1017 static void
1018 gotd_request_timeout(int fd, short events, void *arg)
1020 struct gotd_client *client = arg;
1022 log_debug("disconnecting uid %d due to timeout", client->euid);
1023 disconnect(client);
1026 static void
1027 gotd_accept(int fd, short event, void *arg)
1029 struct sockaddr_storage ss;
1030 struct timeval backoff;
1031 socklen_t len;
1032 int s = -1;
1033 struct gotd_client *client = NULL;
1034 uid_t euid;
1035 gid_t egid;
1037 backoff.tv_sec = 1;
1038 backoff.tv_usec = 0;
1040 if (event_add(&gotd.ev, NULL) == -1) {
1041 log_warn("event_add");
1042 return;
1044 if (event & EV_TIMEOUT)
1045 return;
1047 len = sizeof(ss);
1049 s = accept_reserve(fd, (struct sockaddr *)&ss, &len, GOTD_FD_RESERVE,
1050 &inflight);
1052 if (s == -1) {
1053 switch (errno) {
1054 case EINTR:
1055 case EWOULDBLOCK:
1056 case ECONNABORTED:
1057 return;
1058 case EMFILE:
1059 case ENFILE:
1060 event_del(&gotd.ev);
1061 evtimer_add(&gotd.pause, &backoff);
1062 return;
1063 default:
1064 log_warn("%s: accept", __func__);
1065 return;
1069 if (client_cnt >= GOTD_MAXCLIENTS)
1070 goto err;
1072 if (getpeereid(s, &euid, &egid) == -1) {
1073 log_warn("%s: getpeereid", __func__);
1074 goto err;
1077 client = calloc(1, sizeof(*client));
1078 if (client == NULL) {
1079 log_warn("%s: calloc", __func__);
1080 goto err;
1083 client->state = GOTD_STATE_EXPECT_LIST_REFS;
1084 client->id = get_client_id();
1085 client->fd = s;
1086 s = -1;
1087 client->delta_cache_fd = -1;
1088 client->euid = euid;
1089 client->egid = egid;
1090 client->nref_updates = -1;
1092 imsg_init(&client->iev.ibuf, client->fd);
1093 client->iev.handler = gotd_request;
1094 client->iev.events = EV_READ;
1095 client->iev.handler_arg = client;
1097 event_set(&client->iev.ev, client->fd, EV_READ, gotd_request,
1098 &client->iev);
1099 gotd_imsg_event_add(&client->iev);
1101 evtimer_set(&client->tmo, gotd_request_timeout, client);
1103 add_client(client);
1104 log_debug("%s: new client uid %d connected on fd %d", __func__,
1105 client->euid, client->fd);
1106 return;
1107 err:
1108 inflight--;
1109 if (s != -1)
1110 close(s);
1111 free(client);
1114 static void
1115 gotd_accept_paused(int fd, short event, void *arg)
1117 event_add(&gotd.ev, NULL);
1120 static const char *gotd_proc_names[PROC_MAX] = {
1121 "parent",
1122 "repo_read",
1123 "repo_write"
1126 static struct gotd_child_proc *
1127 get_proc_for_pid(pid_t pid)
1129 struct gotd_child_proc *proc;
1130 int i;
1132 for (i = 0; i < gotd.nprocs; i++) {
1133 proc = &gotd.procs[i];
1134 if (proc->pid == pid)
1135 return proc;
1138 return NULL;
1141 static void
1142 kill_proc(struct gotd_child_proc *proc, int fatal)
1144 if (fatal) {
1145 log_warnx("sending SIGKILL to PID %d", proc->pid);
1146 kill(proc->pid, SIGKILL);
1147 } else
1148 kill(proc->pid, SIGTERM);
1151 static void
1152 gotd_shutdown(void)
1154 pid_t pid;
1155 int status, i;
1156 struct gotd_child_proc *proc;
1158 for (i = 0; i < gotd.nprocs; i++) {
1159 proc = &gotd.procs[i];
1160 msgbuf_clear(&proc->iev.ibuf.w);
1161 close(proc->iev.ibuf.fd);
1162 kill_proc(proc, 0);
1165 log_debug("waiting for children to terminate");
1166 do {
1167 pid = wait(&status);
1168 if (pid == -1) {
1169 if (errno != EINTR && errno != ECHILD)
1170 fatal("wait");
1171 } else if (WIFSIGNALED(status)) {
1172 proc = get_proc_for_pid(pid);
1173 log_warnx("%s %s child process terminated; signal %d",
1174 proc ? gotd_proc_names[proc->type] : "",
1175 proc ? proc->chroot_path : "", WTERMSIG(status));
1177 } while (pid != -1 || (pid == -1 && errno == EINTR));
1179 log_info("terminating");
1180 exit(0);
1183 void
1184 gotd_sighdlr(int sig, short event, void *arg)
1187 * Normal signal handler rules don't apply because libevent
1188 * decouples for us.
1191 switch (sig) {
1192 case SIGHUP:
1193 log_info("%s: ignoring SIGHUP", __func__);
1194 break;
1195 case SIGUSR1:
1196 log_info("%s: ignoring SIGUSR1", __func__);
1197 break;
1198 case SIGTERM:
1199 case SIGINT:
1200 gotd_shutdown();
1201 log_warnx("gotd terminating");
1202 exit(0);
1203 break;
1204 default:
1205 fatalx("unexpected signal");
1209 static const struct got_error *
1210 ensure_proc_is_reading(struct gotd_client *client,
1211 struct gotd_child_proc *proc)
1213 if (!client_is_reading(client)) {
1214 kill_proc(proc, 1);
1215 return got_error_fmt(GOT_ERR_BAD_PACKET,
1216 "PID %d handled a read-request for uid %d but this "
1217 "user is not reading from a repository", proc->pid,
1218 client->euid);
1221 return NULL;
1224 static const struct got_error *
1225 ensure_proc_is_writing(struct gotd_client *client,
1226 struct gotd_child_proc *proc)
1228 if (!client_is_writing(client)) {
1229 kill_proc(proc, 1);
1230 return got_error_fmt(GOT_ERR_BAD_PACKET,
1231 "PID %d handled a write-request for uid %d but this "
1232 "user is not writing to a repository", proc->pid,
1233 client->euid);
1236 return NULL;
1239 static int
1240 verify_imsg_src(struct gotd_client *client, struct gotd_child_proc *proc,
1241 struct imsg *imsg)
1243 const struct got_error *err;
1244 struct gotd_child_proc *client_proc;
1245 int ret = 0;
1247 client_proc = get_client_proc(client);
1248 if (proc->pid != client_proc->pid) {
1249 kill_proc(proc, 1);
1250 log_warnx("received message from PID %d for uid %d, while "
1251 "PID %d is the process serving this user",
1252 proc->pid, client->euid, client_proc->pid);
1253 return 0;
1256 switch (imsg->hdr.type) {
1257 case GOTD_IMSG_ERROR:
1258 ret = 1;
1259 break;
1260 case GOTD_IMSG_PACKFILE_DONE:
1261 err = ensure_proc_is_reading(client, proc);
1262 if (err)
1263 log_warnx("uid %d: %s", client->euid, err->msg);
1264 else
1265 ret = 1;
1266 break;
1267 case GOTD_IMSG_PACKFILE_INSTALL:
1268 case GOTD_IMSG_REF_UPDATES_START:
1269 case GOTD_IMSG_REF_UPDATE:
1270 err = ensure_proc_is_writing(client, proc);
1271 if (err)
1272 log_warnx("uid %d: %s", client->euid, err->msg);
1273 else
1274 ret = 1;
1275 break;
1276 default:
1277 log_debug("%s: unexpected imsg %d", __func__, imsg->hdr.type);
1278 break;
1281 return ret;
1284 static const struct got_error *
1285 recv_packfile_done(uint32_t *client_id, struct imsg *imsg)
1287 struct gotd_imsg_packfile_done idone;
1288 size_t datalen;
1290 log_debug("packfile-done received");
1292 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1293 if (datalen != sizeof(idone))
1294 return got_error(GOT_ERR_PRIVSEP_LEN);
1295 memcpy(&idone, imsg->data, sizeof(idone));
1297 *client_id = idone.client_id;
1298 return NULL;
1301 static const struct got_error *
1302 recv_packfile_install(uint32_t *client_id, struct imsg *imsg)
1304 struct gotd_imsg_packfile_install inst;
1305 size_t datalen;
1307 log_debug("packfile-install received");
1309 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1310 if (datalen != sizeof(inst))
1311 return got_error(GOT_ERR_PRIVSEP_LEN);
1312 memcpy(&inst, imsg->data, sizeof(inst));
1314 *client_id = inst.client_id;
1315 return NULL;
1318 static const struct got_error *
1319 recv_ref_updates_start(uint32_t *client_id, struct imsg *imsg)
1321 struct gotd_imsg_ref_updates_start istart;
1322 size_t datalen;
1324 log_debug("ref-updates-start received");
1326 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1327 if (datalen != sizeof(istart))
1328 return got_error(GOT_ERR_PRIVSEP_LEN);
1329 memcpy(&istart, imsg->data, sizeof(istart));
1331 *client_id = istart.client_id;
1332 return NULL;
1335 static const struct got_error *
1336 recv_ref_update(uint32_t *client_id, struct imsg *imsg)
1338 struct gotd_imsg_ref_update iref;
1339 size_t datalen;
1341 log_debug("ref-update received");
1343 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1344 if (datalen < sizeof(iref))
1345 return got_error(GOT_ERR_PRIVSEP_LEN);
1346 memcpy(&iref, imsg->data, sizeof(iref));
1348 *client_id = iref.client_id;
1349 return NULL;
1352 static const struct got_error *
1353 send_ref_update_ok(struct gotd_client *client,
1354 struct gotd_imsg_ref_update *iref, const char *refname)
1356 struct gotd_imsg_ref_update_ok iok;
1357 struct ibuf *wbuf;
1358 size_t len;
1360 memset(&iok, 0, sizeof(iok));
1361 iok.client_id = client->id;
1362 memcpy(iok.old_id, iref->old_id, SHA1_DIGEST_LENGTH);
1363 memcpy(iok.new_id, iref->new_id, SHA1_DIGEST_LENGTH);
1364 iok.name_len = strlen(refname);
1366 len = sizeof(iok) + iok.name_len;
1367 wbuf = imsg_create(&client->iev.ibuf, GOTD_IMSG_REF_UPDATE_OK,
1368 PROC_GOTD, gotd.pid, len);
1369 if (wbuf == NULL)
1370 return got_error_from_errno("imsg_create REF_UPDATE_OK");
1372 if (imsg_add(wbuf, &iok, sizeof(iok)) == -1)
1373 return got_error_from_errno("imsg_add REF_UPDATE_OK");
1374 if (imsg_add(wbuf, refname, iok.name_len) == -1)
1375 return got_error_from_errno("imsg_add REF_UPDATE_OK");
1377 wbuf->fd = -1;
1378 imsg_close(&client->iev.ibuf, wbuf);
1379 gotd_imsg_event_add(&client->iev);
1380 return NULL;
1383 static void
1384 send_refs_updated(struct gotd_client *client)
1386 if (gotd_imsg_compose_event(&client->iev,
1387 GOTD_IMSG_REFS_UPDATED, PROC_GOTD, -1, NULL, 0) == -1)
1388 log_warn("imsg compose REFS_UPDATED");
1391 static const struct got_error *
1392 send_ref_update_ng(struct gotd_client *client,
1393 struct gotd_imsg_ref_update *iref, const char *refname,
1394 const char *reason)
1396 const struct got_error *ng_err;
1397 struct gotd_imsg_ref_update_ng ing;
1398 struct ibuf *wbuf;
1399 size_t len;
1401 memset(&ing, 0, sizeof(ing));
1402 ing.client_id = client->id;
1403 memcpy(ing.old_id, iref->old_id, SHA1_DIGEST_LENGTH);
1404 memcpy(ing.new_id, iref->new_id, SHA1_DIGEST_LENGTH);
1405 ing.name_len = strlen(refname);
1407 ng_err = got_error_fmt(GOT_ERR_REF_BUSY, "%s", reason);
1408 ing.reason_len = strlen(ng_err->msg);
1410 len = sizeof(ing) + ing.name_len + ing.reason_len;
1411 wbuf = imsg_create(&client->iev.ibuf, GOTD_IMSG_REF_UPDATE_NG,
1412 PROC_GOTD, gotd.pid, len);
1413 if (wbuf == NULL)
1414 return got_error_from_errno("imsg_create REF_UPDATE_NG");
1416 if (imsg_add(wbuf, &ing, sizeof(ing)) == -1)
1417 return got_error_from_errno("imsg_add REF_UPDATE_NG");
1418 if (imsg_add(wbuf, refname, ing.name_len) == -1)
1419 return got_error_from_errno("imsg_add REF_UPDATE_NG");
1420 if (imsg_add(wbuf, ng_err->msg, ing.reason_len) == -1)
1421 return got_error_from_errno("imsg_add REF_UPDATE_NG");
1423 wbuf->fd = -1;
1424 imsg_close(&client->iev.ibuf, wbuf);
1425 gotd_imsg_event_add(&client->iev);
1426 return NULL;
1429 static const struct got_error *
1430 install_pack(struct gotd_client *client, const char *repo_path,
1431 struct imsg *imsg)
1433 const struct got_error *err = NULL;
1434 struct gotd_imsg_packfile_install inst;
1435 char hex[SHA1_DIGEST_STRING_LENGTH];
1436 size_t datalen;
1437 char *packfile_path = NULL, *packidx_path = NULL;
1439 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1440 if (datalen != sizeof(inst))
1441 return got_error(GOT_ERR_PRIVSEP_LEN);
1442 memcpy(&inst, imsg->data, sizeof(inst));
1444 if (client->packfile_path == NULL)
1445 return got_error_msg(GOT_ERR_BAD_REQUEST,
1446 "client has no pack file");
1447 if (client->packidx_path == NULL)
1448 return got_error_msg(GOT_ERR_BAD_REQUEST,
1449 "client has no pack file index");
1451 if (got_sha1_digest_to_str(inst.pack_sha1, hex, sizeof(hex)) == NULL)
1452 return got_error_msg(GOT_ERR_NO_SPACE,
1453 "could not convert pack file SHA1 to hex");
1455 if (asprintf(&packfile_path, "/%s/%s/pack-%s.pack",
1456 repo_path, GOT_OBJECTS_PACK_DIR, hex) == -1) {
1457 err = got_error_from_errno("asprintf");
1458 goto done;
1461 if (asprintf(&packidx_path, "/%s/%s/pack-%s.idx",
1462 repo_path, GOT_OBJECTS_PACK_DIR, hex) == -1) {
1463 err = got_error_from_errno("asprintf");
1464 goto done;
1467 if (rename(client->packfile_path, packfile_path) == -1) {
1468 err = got_error_from_errno3("rename", client->packfile_path,
1469 packfile_path);
1470 goto done;
1473 free(client->packfile_path);
1474 client->packfile_path = NULL;
1476 if (rename(client->packidx_path, packidx_path) == -1) {
1477 err = got_error_from_errno3("rename", client->packidx_path,
1478 packidx_path);
1479 goto done;
1482 free(client->packidx_path);
1483 client->packidx_path = NULL;
1484 done:
1485 free(packfile_path);
1486 free(packidx_path);
1487 return err;
1490 static const struct got_error *
1491 begin_ref_updates(struct gotd_client *client, struct imsg *imsg)
1493 struct gotd_imsg_ref_updates_start istart;
1494 size_t datalen;
1496 if (client->nref_updates != -1)
1497 return got_error(GOT_ERR_PRIVSEP_MSG);
1499 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1500 if (datalen != sizeof(istart))
1501 return got_error(GOT_ERR_PRIVSEP_LEN);
1502 memcpy(&istart, imsg->data, sizeof(istart));
1504 if (istart.nref_updates <= 0)
1505 return got_error(GOT_ERR_PRIVSEP_MSG);
1507 client->nref_updates = istart.nref_updates;
1508 return NULL;
1511 static const struct got_error *
1512 update_ref(struct gotd_client *client, const char *repo_path,
1513 struct imsg *imsg)
1515 const struct got_error *err = NULL;
1516 struct got_repository *repo = NULL;
1517 struct got_reference *ref = NULL;
1518 struct gotd_imsg_ref_update iref;
1519 struct got_object_id old_id, new_id;
1520 struct got_object_id *id = NULL;
1521 struct got_object *obj = NULL;
1522 char *refname = NULL;
1523 size_t datalen;
1524 int locked = 0;
1526 log_debug("update-ref from uid %d", client->euid);
1528 if (client->nref_updates <= 0)
1529 return got_error(GOT_ERR_PRIVSEP_MSG);
1531 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1532 if (datalen < sizeof(iref))
1533 return got_error(GOT_ERR_PRIVSEP_LEN);
1534 memcpy(&iref, imsg->data, sizeof(iref));
1535 if (datalen != sizeof(iref) + iref.name_len)
1536 return got_error(GOT_ERR_PRIVSEP_LEN);
1537 refname = malloc(iref.name_len + 1);
1538 if (refname == NULL)
1539 return got_error_from_errno("malloc");
1540 memcpy(refname, imsg->data + sizeof(iref), iref.name_len);
1541 refname[iref.name_len] = '\0';
1543 log_debug("updating ref %s for uid %d", refname, client->euid);
1545 err = got_repo_open(&repo, repo_path, NULL, NULL);
1546 if (err)
1547 goto done;
1549 memcpy(old_id.sha1, iref.old_id, SHA1_DIGEST_LENGTH);
1550 memcpy(new_id.sha1, iref.new_id, SHA1_DIGEST_LENGTH);
1551 err = got_object_open(&obj, repo, &new_id);
1552 if (err)
1553 goto done;
1555 if (iref.ref_is_new) {
1556 err = got_ref_open(&ref, repo, refname, 0);
1557 if (err) {
1558 if (err->code != GOT_ERR_NOT_REF)
1559 goto done;
1560 err = got_ref_alloc(&ref, refname, &new_id);
1561 if (err)
1562 goto done;
1563 err = got_ref_write(ref, repo); /* will lock/unlock */
1564 if (err)
1565 goto done;
1566 } else {
1567 err = got_error_fmt(GOT_ERR_REF_BUSY,
1568 "%s has been created by someone else "
1569 "while transaction was in progress",
1570 got_ref_get_name(ref));
1571 goto done;
1573 } else {
1574 err = got_ref_open(&ref, repo, refname, 1 /* lock */);
1575 if (err)
1576 goto done;
1577 locked = 1;
1579 err = got_ref_resolve(&id, repo, ref);
1580 if (err)
1581 goto done;
1583 if (got_object_id_cmp(id, &old_id) != 0) {
1584 err = got_error_fmt(GOT_ERR_REF_BUSY,
1585 "%s has been modified by someone else "
1586 "while transaction was in progress",
1587 got_ref_get_name(ref));
1588 goto done;
1591 err = got_ref_change_ref(ref, &new_id);
1592 if (err)
1593 goto done;
1595 err = got_ref_write(ref, repo);
1596 if (err)
1597 goto done;
1599 free(id);
1600 id = NULL;
1602 done:
1603 if (err) {
1604 if (err->code == GOT_ERR_LOCKFILE_TIMEOUT) {
1605 err = got_error_fmt(GOT_ERR_LOCKFILE_TIMEOUT,
1606 "could not acquire exclusive file lock for %s",
1607 refname);
1609 send_ref_update_ng(client, &iref, refname, err->msg);
1610 } else
1611 send_ref_update_ok(client, &iref, refname);
1613 if (client->nref_updates > 0) {
1614 client->nref_updates--;
1615 if (client->nref_updates == 0)
1616 send_refs_updated(client);
1619 if (locked) {
1620 const struct got_error *unlock_err;
1621 unlock_err = got_ref_unlock(ref);
1622 if (unlock_err && err == NULL)
1623 err = unlock_err;
1625 if (ref)
1626 got_ref_close(ref);
1627 if (obj)
1628 got_object_close(obj);
1629 if (repo)
1630 got_repo_close(repo);
1631 free(refname);
1632 free(id);
1633 return err;
1636 static void
1637 gotd_dispatch(int fd, short event, void *arg)
1639 struct gotd_imsgev *iev = arg;
1640 struct imsgbuf *ibuf = &iev->ibuf;
1641 struct gotd_child_proc *proc = NULL;
1642 ssize_t n;
1643 int shut = 0;
1644 struct imsg imsg;
1646 if (event & EV_READ) {
1647 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1648 fatal("imsg_read error");
1649 if (n == 0) {
1650 /* Connection closed. */
1651 shut = 1;
1652 goto done;
1656 if (event & EV_WRITE) {
1657 n = msgbuf_write(&ibuf->w);
1658 if (n == -1 && errno != EAGAIN)
1659 fatal("msgbuf_write");
1660 if (n == 0) {
1661 /* Connection closed. */
1662 shut = 1;
1663 goto done;
1667 proc = find_proc_by_fd(fd);
1668 if (proc == NULL)
1669 fatalx("cannot find child process for fd %d", fd);
1671 for (;;) {
1672 const struct got_error *err = NULL;
1673 struct gotd_client *client = NULL;
1674 uint32_t client_id = 0;
1675 int do_disconnect = 0;
1676 int do_ref_updates = 0, do_ref_update = 0;
1677 int do_packfile_install = 0;
1679 if ((n = imsg_get(ibuf, &imsg)) == -1)
1680 fatal("%s: imsg_get error", __func__);
1681 if (n == 0) /* No more messages. */
1682 break;
1684 switch (imsg.hdr.type) {
1685 case GOTD_IMSG_ERROR:
1686 do_disconnect = 1;
1687 err = gotd_imsg_recv_error(&client_id, &imsg);
1688 break;
1689 case GOTD_IMSG_PACKFILE_DONE:
1690 do_disconnect = 1;
1691 err = recv_packfile_done(&client_id, &imsg);
1692 break;
1693 case GOTD_IMSG_PACKFILE_INSTALL:
1694 err = recv_packfile_install(&client_id, &imsg);
1695 if (err == NULL)
1696 do_packfile_install = 1;
1697 break;
1698 case GOTD_IMSG_REF_UPDATES_START:
1699 err = recv_ref_updates_start(&client_id, &imsg);
1700 if (err == NULL)
1701 do_ref_updates = 1;
1702 break;
1703 case GOTD_IMSG_REF_UPDATE:
1704 err = recv_ref_update(&client_id, &imsg);
1705 if (err == NULL)
1706 do_ref_update = 1;
1707 break;
1708 default:
1709 log_debug("unexpected imsg %d", imsg.hdr.type);
1710 break;
1713 client = find_client(client_id);
1714 if (client == NULL) {
1715 log_warnx("%s: client not found", __func__);
1716 imsg_free(&imsg);
1717 continue;
1720 if (!verify_imsg_src(client, proc, &imsg)) {
1721 log_debug("dropping imsg type %d from PID %d",
1722 imsg.hdr.type, proc->pid);
1723 imsg_free(&imsg);
1724 continue;
1726 if (err)
1727 log_warnx("uid %d: %s", client->euid, err->msg);
1729 if (do_disconnect) {
1730 if (err)
1731 disconnect_on_error(client, err);
1732 else
1733 disconnect(client);
1734 } else if (do_packfile_install)
1735 err = install_pack(client, proc->chroot_path, &imsg);
1736 else if (do_ref_updates)
1737 err = begin_ref_updates(client, &imsg);
1738 else if (do_ref_update)
1739 err = update_ref(client, proc->chroot_path, &imsg);
1741 if (err)
1742 log_warnx("uid %d: %s", client->euid, err->msg);
1743 imsg_free(&imsg);
1745 done:
1746 if (!shut) {
1747 gotd_imsg_event_add(iev);
1748 } else {
1749 /* This pipe is dead. Remove its event handler */
1750 event_del(&iev->ev);
1751 event_loopexit(NULL);
1755 static pid_t
1756 start_child(enum gotd_procid proc_id, const char *chroot_path,
1757 char *argv0, int fd, int daemonize, int verbosity)
1759 char *argv[9];
1760 int argc = 0;
1761 pid_t pid;
1763 switch (pid = fork()) {
1764 case -1:
1765 fatal("cannot fork");
1766 case 0:
1767 break;
1768 default:
1769 close(fd);
1770 return pid;
1773 if (fd != GOTD_SOCK_FILENO) {
1774 if (dup2(fd, GOTD_SOCK_FILENO) == -1)
1775 fatal("cannot setup imsg fd");
1776 } else if (fcntl(fd, F_SETFD, 0) == -1)
1777 fatal("cannot setup imsg fd");
1779 argv[argc++] = argv0;
1780 switch (proc_id) {
1781 case PROC_REPO_READ:
1782 argv[argc++] = (char *)"-R";
1783 break;
1784 case PROC_REPO_WRITE:
1785 argv[argc++] = (char *)"-W";
1786 break;
1787 default:
1788 fatalx("invalid process id %d", proc_id);
1791 argv[argc++] = (char *)"-P";
1792 argv[argc++] = (char *)chroot_path;
1794 if (!daemonize)
1795 argv[argc++] = (char *)"-d";
1796 if (verbosity > 0)
1797 argv[argc++] = (char *)"-v";
1798 if (verbosity > 1)
1799 argv[argc++] = (char *)"-v";
1800 argv[argc++] = NULL;
1802 execvp(argv0, argv);
1803 fatal("execvp");
1806 static void
1807 start_repo_children(struct gotd *gotd, char *argv0, int daemonize,
1808 int verbosity)
1810 struct gotd_repo *repo = NULL;
1811 struct gotd_child_proc *proc;
1812 int i;
1815 * XXX For now, use one reader and one writer per repository.
1816 * This should be changed to N readers + M writers.
1818 gotd->nprocs = gotd->nrepos * 2;
1819 gotd->procs = calloc(gotd->nprocs, sizeof(*gotd->procs));
1820 if (gotd->procs == NULL)
1821 fatal("calloc");
1822 for (i = 0; i < gotd->nprocs; i++) {
1823 if (repo == NULL)
1824 repo = TAILQ_FIRST(&gotd->repos);
1825 proc = &gotd->procs[i];
1826 if (i < gotd->nrepos)
1827 proc->type = PROC_REPO_READ;
1828 else
1829 proc->type = PROC_REPO_WRITE;
1830 if (strlcpy(proc->repo_name, repo->name,
1831 sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
1832 fatalx("repository name too long: %s", repo->name);
1833 log_debug("adding repository %s", repo->name);
1834 if (realpath(repo->path, proc->chroot_path) == NULL)
1835 fatal("%s", repo->path);
1836 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1837 PF_UNSPEC, proc->pipe) == -1)
1838 fatal("socketpair");
1839 proc->pid = start_child(proc->type, proc->chroot_path, argv0,
1840 proc->pipe[1], daemonize, verbosity);
1841 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1842 log_debug("proc %s %s is on fd %d",
1843 gotd_proc_names[proc->type], proc->chroot_path,
1844 proc->pipe[0]);
1845 proc->iev.handler = gotd_dispatch;
1846 proc->iev.events = EV_READ;
1847 proc->iev.handler_arg = NULL;
1848 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1849 gotd_dispatch, &proc->iev);
1851 repo = TAILQ_NEXT(repo, entry);
1855 static void
1856 apply_unveil(void)
1858 struct gotd_repo *repo;
1860 TAILQ_FOREACH(repo, &gotd.repos, entry) {
1861 if (unveil(repo->path, "rwc") == -1)
1862 fatal("unveil %s", repo->path);
1865 if (unveil(GOT_TMPDIR_STR, "rwc") == -1)
1866 fatal("unveil %s", GOT_TMPDIR_STR);
1868 if (unveil(NULL, NULL) == -1)
1869 fatal("unveil");
1872 int
1873 main(int argc, char **argv)
1875 const struct got_error *error = NULL;
1876 int ch, fd = -1, daemonize = 1, verbosity = 0, noaction = 0;
1877 const char *confpath = GOTD_CONF_PATH;
1878 char *argv0 = argv[0];
1879 char title[2048];
1880 gid_t groups[NGROUPS_MAX + 1];
1881 int ngroups = NGROUPS_MAX + 1;
1882 struct passwd *pw = NULL;
1883 struct group *gr = NULL;
1884 char *repo_path = NULL;
1885 enum gotd_procid proc_id = PROC_GOTD;
1886 struct event evsigint, evsigterm, evsighup, evsigusr1;
1887 int *pack_fds = NULL, *temp_fds = NULL;
1889 log_init(1, LOG_DAEMON); /* Log to stderr until daemonized. */
1891 while ((ch = getopt(argc, argv, "df:nP:RvW")) != -1) {
1892 switch (ch) {
1893 case 'd':
1894 daemonize = 0;
1895 break;
1896 case 'f':
1897 confpath = optarg;
1898 break;
1899 case 'n':
1900 noaction = 1;
1901 break;
1902 case 'P':
1903 repo_path = realpath(optarg, NULL);
1904 if (repo_path == NULL)
1905 fatal("realpath '%s'", optarg);
1906 break;
1907 case 'R':
1908 proc_id = PROC_REPO_READ;
1909 break;
1910 case 'v':
1911 if (verbosity < 3)
1912 verbosity++;
1913 break;
1914 case 'W':
1915 proc_id = PROC_REPO_WRITE;
1916 break;
1917 default:
1918 usage();
1922 argc -= optind;
1923 argv += optind;
1925 if (argc != 0)
1926 usage();
1928 if (geteuid())
1929 fatalx("need root privileges");
1931 log_init(daemonize ? 0 : 1, LOG_DAEMON);
1932 log_setverbose(verbosity);
1934 if (parse_config(confpath, proc_id, &gotd) != 0)
1935 return 1;
1937 if (proc_id == PROC_GOTD &&
1938 (gotd.nrepos == 0 || TAILQ_EMPTY(&gotd.repos)))
1939 fatalx("no repository defined in configuration file");
1941 pw = getpwnam(gotd.user_name);
1942 if (pw == NULL)
1943 fatal("getpwuid: user %s not found", gotd.user_name);
1945 if (pw->pw_uid == 0) {
1946 fatalx("cannot run %s as %s: the user running %s "
1947 "must not be the superuser",
1948 getprogname(), pw->pw_name, getprogname());
1951 if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups) == -1)
1952 log_warnx("group membership list truncated");
1954 gr = match_group(groups, ngroups, gotd.unix_group_name);
1955 if (gr == NULL) {
1956 fatalx("cannot start %s: the user running %s "
1957 "must be a secondary member of group %s",
1958 getprogname(), getprogname(), gotd.unix_group_name);
1960 if (gr->gr_gid == pw->pw_gid) {
1961 fatalx("cannot start %s: the user running %s "
1962 "must be a secondary member of group %s, but "
1963 "%s is the user's primary group",
1964 getprogname(), getprogname(), gotd.unix_group_name,
1965 gotd.unix_group_name);
1968 if (proc_id == PROC_GOTD &&
1969 !got_path_is_absolute(gotd.unix_socket_path))
1970 fatalx("bad unix socket path \"%s\": must be an absolute path",
1971 gotd.unix_socket_path);
1973 if (noaction)
1974 return 0;
1976 if (proc_id == PROC_GOTD && verbosity) {
1977 log_info("socket: %s", gotd.unix_socket_path);
1978 log_info("user: %s", pw->pw_name);
1979 log_info("secondary group: %s", gr->gr_name);
1981 fd = unix_socket_listen(gotd.unix_socket_path, pw->pw_uid,
1982 gr->gr_gid);
1983 if (fd == -1) {
1984 fatal("cannot listen on unix socket %s",
1985 gotd.unix_socket_path);
1989 if (proc_id == PROC_GOTD) {
1990 gotd.pid = getpid();
1991 snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
1992 start_repo_children(&gotd, argv0, daemonize, verbosity);
1993 arc4random_buf(&clients_hash_key, sizeof(clients_hash_key));
1994 if (daemonize && daemon(0, 0) == -1)
1995 fatal("daemon");
1996 } else if (proc_id == PROC_REPO_READ || proc_id == PROC_REPO_WRITE) {
1997 error = got_repo_pack_fds_open(&pack_fds);
1998 if (error != NULL)
1999 fatalx("cannot open pack tempfiles: %s", error->msg);
2000 error = got_repo_temp_fds_open(&temp_fds);
2001 if (error != NULL)
2002 fatalx("cannot open pack tempfiles: %s", error->msg);
2003 if (repo_path == NULL)
2004 fatalx("repository path not specified");
2005 snprintf(title, sizeof(title), "%s %s",
2006 gotd_proc_names[proc_id], repo_path);
2007 if (chroot(repo_path) == -1)
2008 fatal("chroot");
2009 if (chdir("/") == -1)
2010 fatal("chdir(\"/\")");
2011 if (daemonize && daemon(1, 0) == -1)
2012 fatal("daemon");
2013 } else
2014 fatal("invalid process id %d", proc_id);
2016 setproctitle("%s", title);
2017 log_procinit(title);
2019 /* Drop root privileges. */
2020 if (setgid(pw->pw_gid) == -1)
2021 fatal("setgid %d failed", pw->pw_gid);
2022 if (setuid(pw->pw_uid) == -1)
2023 fatal("setuid %d failed", pw->pw_uid);
2025 event_init();
2027 switch (proc_id) {
2028 case PROC_GOTD:
2029 #ifndef PROFILE
2030 if (pledge("stdio rpath wpath cpath proc getpw sendfd recvfd "
2031 "fattr flock unix unveil", NULL) == -1)
2032 err(1, "pledge");
2033 #endif
2034 break;
2035 case PROC_REPO_READ:
2036 #ifndef PROFILE
2037 if (pledge("stdio rpath recvfd", NULL) == -1)
2038 err(1, "pledge");
2039 #endif
2040 repo_read_main(title, pack_fds, temp_fds);
2041 /* NOTREACHED */
2042 exit(0);
2043 case PROC_REPO_WRITE:
2044 #ifndef PROFILE
2045 if (pledge("stdio rpath sendfd recvfd", NULL) == -1)
2046 err(1, "pledge");
2047 #endif
2048 repo_write_main(title, pack_fds, temp_fds);
2049 /* NOTREACHED */
2050 exit(0);
2051 default:
2052 fatal("invalid process id %d", proc_id);
2055 if (proc_id != PROC_GOTD)
2056 fatal("invalid process id %d", proc_id);
2058 apply_unveil();
2060 signal_set(&evsigint, SIGINT, gotd_sighdlr, NULL);
2061 signal_set(&evsigterm, SIGTERM, gotd_sighdlr, NULL);
2062 signal_set(&evsighup, SIGHUP, gotd_sighdlr, NULL);
2063 signal_set(&evsigusr1, SIGUSR1, gotd_sighdlr, NULL);
2064 signal(SIGPIPE, SIG_IGN);
2066 signal_add(&evsigint, NULL);
2067 signal_add(&evsigterm, NULL);
2068 signal_add(&evsighup, NULL);
2069 signal_add(&evsigusr1, NULL);
2071 event_set(&gotd.ev, fd, EV_READ | EV_PERSIST, gotd_accept, NULL);
2072 if (event_add(&gotd.ev, NULL))
2073 fatalx("event add");
2074 evtimer_set(&gotd.pause, gotd_accept_paused, NULL);
2076 event_dispatch();
2078 if (fd != -1)
2079 close(fd);
2080 if (pack_fds)
2081 got_repo_pack_fds_close(pack_fds);
2082 free(repo_path);
2083 return 0;