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, "%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 if (gotd_imsg_compose_event(&client->repo_read->iev,
686 GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[0],
687 &ipipe, sizeof(ipipe)) == -1) {
688 err = got_error_from_errno("imsg compose PACKFILE_PIPE");
689 close(pipe[1]);
690 return err;
693 if (gotd_imsg_compose_event(&client->repo_read->iev,
694 GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[1],
695 &ipipe, sizeof(ipipe)) == -1)
696 err = got_error_from_errno("imsg compose PACKFILE_PIPE");
698 return err;
701 static const struct got_error *
702 recv_packfile(struct gotd_client *client)
704 const struct got_error *err = NULL;
705 struct gotd_imsg_recv_packfile ipack;
706 struct gotd_imsg_packfile_pipe ipipe;
707 struct gotd_imsg_packidx_file ifile;
708 char *basepath = NULL, *pack_path = NULL, *idx_path = NULL;
709 int packfd = -1, idxfd = -1;
710 int pipe[2] = { -1, -1 };
712 if (client->packfile_path) {
713 return got_error_fmt(GOT_ERR_PRIVSEP_MSG,
714 "uid %d already has a pack file", client->euid);
717 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe) == -1)
718 return got_error_from_errno("socketpair");
720 memset(&ipipe, 0, sizeof(ipipe));
721 ipipe.client_id = client->id;
723 if (gotd_imsg_compose_event(&client->repo_write->iev,
724 GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[0],
725 &ipipe, sizeof(ipipe)) == -1) {
726 err = got_error_from_errno("imsg compose PACKFILE_PIPE");
727 pipe[0] = -1;
728 goto done;
730 pipe[0] = -1;
732 if (gotd_imsg_compose_event(&client->repo_write->iev,
733 GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[1],
734 &ipipe, sizeof(ipipe)) == -1)
735 err = got_error_from_errno("imsg compose PACKFILE_PIPE");
736 pipe[1] = -1;
738 if (asprintf(&basepath, "%s/%s/receiving-from-uid-%d.pack",
739 client->repo_write->chroot_path, GOT_OBJECTS_PACK_DIR,
740 client->euid) == -1) {
741 err = got_error_from_errno("asprintf");
742 goto done;
745 err = got_opentemp_named_fd(&pack_path, &packfd, basepath);
746 if (err)
747 goto done;
749 free(basepath);
750 if (asprintf(&basepath, "%s/%s/receiving-from-uid-%d.idx",
751 client->repo_write->chroot_path, GOT_OBJECTS_PACK_DIR,
752 client->euid) == -1) {
753 err = got_error_from_errno("asprintf");
754 basepath = NULL;
755 goto done;
757 err = got_opentemp_named_fd(&idx_path, &idxfd, basepath);
758 if (err)
759 goto done;
761 memset(&ifile, 0, sizeof(ifile));
762 ifile.client_id = client->id;
763 if (gotd_imsg_compose_event(&client->repo_write->iev,
764 GOTD_IMSG_PACKIDX_FILE, PROC_GOTD, idxfd,
765 &ifile, sizeof(ifile)) == -1) {
766 err = got_error_from_errno("imsg compose PACKIDX_FILE");
767 idxfd = -1;
768 goto done;
770 idxfd = -1;
772 memset(&ipack, 0, sizeof(ipack));
773 ipack.client_id = client->id;
774 if (client_has_capability(client, GOT_CAPA_REPORT_STATUS))
775 ipack.report_status = 1;
777 if (gotd_imsg_compose_event(&client->repo_write->iev,
778 GOTD_IMSG_RECV_PACKFILE, PROC_GOTD, packfd,
779 &ipack, sizeof(ipack)) == -1) {
780 err = got_error_from_errno("imsg compose RECV_PACKFILE");
781 packfd = -1;
782 goto done;
784 packfd = -1;
786 done:
787 free(basepath);
788 if (pipe[0] != -1 && close(pipe[0]) == -1 && err == NULL)
789 err = got_error_from_errno("close");
790 if (pipe[1] != -1 && close(pipe[1]) == -1 && err == NULL)
791 err = got_error_from_errno("close");
792 if (packfd != -1 && close(packfd) == -1 && err == NULL)
793 err = got_error_from_errno("close");
794 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
795 err = got_error_from_errno("close");
796 if (err) {
797 free(pack_path);
798 free(idx_path);
799 } else {
800 client->packfile_path = pack_path;
801 client->packidx_path = idx_path;
803 return err;
806 static void
807 gotd_request(int fd, short events, void *arg)
809 struct gotd_imsgev *iev = arg;
810 struct imsgbuf *ibuf = &iev->ibuf;
811 struct gotd_client *client = iev->handler_arg;
812 const struct got_error *err = NULL;
813 struct imsg imsg;
814 ssize_t n;
816 if (events & EV_WRITE) {
817 while (ibuf->w.queued) {
818 n = msgbuf_write(&ibuf->w);
819 if (n == -1 && errno == EPIPE) {
820 /*
821 * The client has closed its socket.
822 * This can happen when Git clients are
823 * done sending pack file data.
824 */
825 msgbuf_clear(&ibuf->w);
826 continue;
827 } else if (n == -1 && errno != EAGAIN) {
828 err = got_error_from_errno("imsg_flush");
829 disconnect_on_error(client, err);
830 return;
832 if (n == 0) {
833 /* Connection closed. */
834 err = got_error(GOT_ERR_EOF);
835 disconnect_on_error(client, err);
836 return;
841 if ((events & EV_READ) == 0)
842 return;
844 memset(&imsg, 0, sizeof(imsg));
846 while (err == NULL) {
847 err = gotd_imsg_recv(&imsg, ibuf, 0);
848 if (err) {
849 if (err->code == GOT_ERR_PRIVSEP_READ)
850 err = NULL;
851 break;
854 evtimer_del(&client->tmo);
856 switch (imsg.hdr.type) {
857 case GOTD_IMSG_LIST_REFS:
858 if (client->state != GOTD_STATE_EXPECT_LIST_REFS) {
859 err = got_error_msg(GOT_ERR_BAD_REQUEST,
860 "unexpected list-refs request received");
861 break;
863 err = forward_list_refs_request(client, &imsg);
864 if (err)
865 break;
866 client->state = GOTD_STATE_EXPECT_CAPABILITIES;
867 log_debug("uid %d: expecting capabilities",
868 client->euid);
869 break;
870 case GOTD_IMSG_CAPABILITIES:
871 if (client->state != GOTD_STATE_EXPECT_CAPABILITIES) {
872 err = got_error_msg(GOT_ERR_BAD_REQUEST,
873 "unexpected capabilities received");
874 break;
876 log_debug("receiving capabilities from uid %d",
877 client->euid);
878 err = recv_capabilities(client, &imsg);
879 break;
880 case GOTD_IMSG_CAPABILITY:
881 if (client->state != GOTD_STATE_EXPECT_CAPABILITIES) {
882 err = got_error_msg(GOT_ERR_BAD_REQUEST,
883 "unexpected capability received");
884 break;
886 err = recv_capability(client, &imsg);
887 if (err || client->ncapabilities < client->ncapa_alloc)
888 break;
889 if (client_is_reading(client)) {
890 client->state = GOTD_STATE_EXPECT_WANT;
891 log_debug("uid %d: expecting want-lines",
892 client->euid);
893 } else if (client_is_writing(client)) {
894 client->state = GOTD_STATE_EXPECT_REF_UPDATE;
895 log_debug("uid %d: expecting ref-update-lines",
896 client->euid);
897 } else
898 fatalx("client %d is both reading and writing",
899 client->euid);
900 break;
901 case GOTD_IMSG_WANT:
902 if (client->state != GOTD_STATE_EXPECT_WANT) {
903 err = got_error_msg(GOT_ERR_BAD_REQUEST,
904 "unexpected want-line received");
905 break;
907 log_debug("received want-line from uid %d",
908 client->euid);
909 err = ensure_client_is_reading(client);
910 if (err)
911 break;
912 err = forward_want(client, &imsg);
913 break;
914 case GOTD_IMSG_REF_UPDATE:
915 if (client->state != GOTD_STATE_EXPECT_REF_UPDATE) {
916 err = got_error_msg(GOT_ERR_BAD_REQUEST,
917 "unexpected ref-update-line received");
918 break;
920 log_debug("received ref-update-line from uid %d",
921 client->euid);
922 err = ensure_client_is_writing(client);
923 if (err)
924 break;
925 err = forward_ref_update(client, &imsg);
926 if (err)
927 break;
928 client->state = GOTD_STATE_EXPECT_MORE_REF_UPDATES;
929 break;
930 case GOTD_IMSG_HAVE:
931 if (client->state != GOTD_STATE_EXPECT_HAVE) {
932 err = got_error_msg(GOT_ERR_BAD_REQUEST,
933 "unexpected have-line received");
934 break;
936 log_debug("received have-line from uid %d",
937 client->euid);
938 err = ensure_client_is_reading(client);
939 if (err)
940 break;
941 err = forward_have(client, &imsg);
942 if (err)
943 break;
944 break;
945 case GOTD_IMSG_FLUSH:
946 if (client->state == GOTD_STATE_EXPECT_WANT ||
947 client->state == GOTD_STATE_EXPECT_HAVE) {
948 err = ensure_client_is_reading(client);
949 if (err)
950 break;
951 } else if (client->state ==
952 GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
953 err = ensure_client_is_writing(client);
954 if (err)
955 break;
956 } else {
957 err = got_error_msg(GOT_ERR_BAD_REQUEST,
958 "unexpected flush-pkt received");
959 break;
961 log_debug("received flush-pkt from uid %d",
962 client->euid);
963 if (client->state == GOTD_STATE_EXPECT_WANT) {
964 client->state = GOTD_STATE_EXPECT_HAVE;
965 log_debug("uid %d: expecting have-lines",
966 client->euid);
967 } else if (client->state == GOTD_STATE_EXPECT_HAVE) {
968 client->state = GOTD_STATE_EXPECT_DONE;
969 log_debug("uid %d: expecting 'done'",
970 client->euid);
971 } else if (client->state ==
972 GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
973 client->state = GOTD_STATE_EXPECT_PACKFILE;
974 log_debug("uid %d: expecting packfile",
975 client->euid);
976 err = recv_packfile(client);
977 } else {
978 /* should not happen, see above */
979 err = got_error_msg(GOT_ERR_BAD_REQUEST,
980 "unexpected client state");
981 break;
983 break;
984 case GOTD_IMSG_DONE:
985 if (client->state != GOTD_STATE_EXPECT_HAVE &&
986 client->state != GOTD_STATE_EXPECT_DONE) {
987 err = got_error_msg(GOT_ERR_BAD_REQUEST,
988 "unexpected flush-pkt received");
989 break;
991 log_debug("received 'done' from uid %d", client->euid);
992 err = ensure_client_is_reading(client);
993 if (err)
994 break;
995 client->state = GOTD_STATE_DONE;
996 err = send_packfile(client);
997 break;
998 default:
999 err = got_error(GOT_ERR_PRIVSEP_MSG);
1000 break;
1003 imsg_free(&imsg);
1006 if (err) {
1007 if (err->code != GOT_ERR_EOF ||
1008 client->state != GOTD_STATE_EXPECT_PACKFILE)
1009 disconnect_on_error(client, err);
1010 } else {
1011 gotd_imsg_event_add(&client->iev);
1012 evtimer_add(&client->tmo, &timeout);
1016 static void
1017 gotd_request_timeout(int fd, short events, void *arg)
1019 struct gotd_client *client = arg;
1021 log_debug("disconnecting uid %d due to timeout", client->euid);
1022 disconnect(client);
1025 static void
1026 gotd_accept(int fd, short event, void *arg)
1028 struct sockaddr_storage ss;
1029 struct timeval backoff;
1030 socklen_t len;
1031 int s = -1;
1032 struct gotd_client *client = NULL;
1033 uid_t euid;
1034 gid_t egid;
1036 backoff.tv_sec = 1;
1037 backoff.tv_usec = 0;
1039 if (event_add(&gotd.ev, NULL) == -1) {
1040 log_warn("event_add");
1041 return;
1043 if (event & EV_TIMEOUT)
1044 return;
1046 len = sizeof(ss);
1048 s = accept_reserve(fd, (struct sockaddr *)&ss, &len, GOTD_FD_RESERVE,
1049 &inflight);
1051 if (s == -1) {
1052 switch (errno) {
1053 case EINTR:
1054 case EWOULDBLOCK:
1055 case ECONNABORTED:
1056 return;
1057 case EMFILE:
1058 case ENFILE:
1059 event_del(&gotd.ev);
1060 evtimer_add(&gotd.pause, &backoff);
1061 return;
1062 default:
1063 log_warn("%s: accept", __func__);
1064 return;
1068 if (client_cnt >= GOTD_MAXCLIENTS)
1069 goto err;
1071 if (getpeereid(s, &euid, &egid) == -1) {
1072 log_warn("%s: getpeereid", __func__);
1073 goto err;
1076 client = calloc(1, sizeof(*client));
1077 if (client == NULL) {
1078 log_warn("%s: calloc", __func__);
1079 goto err;
1082 client->state = GOTD_STATE_EXPECT_LIST_REFS;
1083 client->id = get_client_id();
1084 client->fd = s;
1085 s = -1;
1086 client->delta_cache_fd = -1;
1087 client->euid = euid;
1088 client->egid = egid;
1089 client->nref_updates = -1;
1091 imsg_init(&client->iev.ibuf, client->fd);
1092 client->iev.handler = gotd_request;
1093 client->iev.events = EV_READ;
1094 client->iev.handler_arg = client;
1096 event_set(&client->iev.ev, client->fd, EV_READ, gotd_request,
1097 &client->iev);
1098 gotd_imsg_event_add(&client->iev);
1100 evtimer_set(&client->tmo, gotd_request_timeout, client);
1102 add_client(client);
1103 log_debug("%s: new client uid %d connected on fd %d", __func__,
1104 client->euid, client->fd);
1105 return;
1106 err:
1107 inflight--;
1108 if (s != -1)
1109 close(s);
1110 free(client);
1113 static void
1114 gotd_accept_paused(int fd, short event, void *arg)
1116 event_add(&gotd.ev, NULL);
1119 static const char *gotd_proc_names[PROC_MAX] = {
1120 "parent",
1121 "repo_read",
1122 "repo_write"
1125 static struct gotd_child_proc *
1126 get_proc_for_pid(pid_t pid)
1128 struct gotd_child_proc *proc;
1129 int i;
1131 for (i = 0; i < gotd.nprocs; i++) {
1132 proc = &gotd.procs[i];
1133 if (proc->pid == pid)
1134 return proc;
1137 return NULL;
1140 static void
1141 kill_proc(struct gotd_child_proc *proc, int fatal)
1143 if (fatal) {
1144 log_warnx("sending SIGKILL to PID %d", proc->pid);
1145 kill(proc->pid, SIGKILL);
1146 } else
1147 kill(proc->pid, SIGTERM);
1150 static void
1151 gotd_shutdown(void)
1153 pid_t pid;
1154 int status, i;
1155 struct gotd_child_proc *proc;
1157 for (i = 0; i < gotd.nprocs; i++) {
1158 proc = &gotd.procs[i];
1159 msgbuf_clear(&proc->iev.ibuf.w);
1160 close(proc->iev.ibuf.fd);
1161 kill_proc(proc, 0);
1164 log_debug("waiting for children to terminate");
1165 do {
1166 pid = wait(&status);
1167 if (pid == -1) {
1168 if (errno != EINTR && errno != ECHILD)
1169 fatal("wait");
1170 } else if (WIFSIGNALED(status)) {
1171 proc = get_proc_for_pid(pid);
1172 log_warnx("%s %s child process terminated; signal %d",
1173 proc ? gotd_proc_names[proc->type] : "",
1174 proc ? proc->chroot_path : "", WTERMSIG(status));
1176 } while (pid != -1 || (pid == -1 && errno == EINTR));
1178 log_info("terminating");
1179 exit(0);
1182 void
1183 gotd_sighdlr(int sig, short event, void *arg)
1186 * Normal signal handler rules don't apply because libevent
1187 * decouples for us.
1190 switch (sig) {
1191 case SIGHUP:
1192 log_info("%s: ignoring SIGHUP", __func__);
1193 break;
1194 case SIGUSR1:
1195 log_info("%s: ignoring SIGUSR1", __func__);
1196 break;
1197 case SIGTERM:
1198 case SIGINT:
1199 gotd_shutdown();
1200 log_warnx("gotd terminating");
1201 exit(0);
1202 break;
1203 default:
1204 fatalx("unexpected signal");
1208 static const struct got_error *
1209 ensure_proc_is_reading(struct gotd_client *client,
1210 struct gotd_child_proc *proc)
1212 if (!client_is_reading(client)) {
1213 kill_proc(proc, 1);
1214 return got_error_fmt(GOT_ERR_BAD_PACKET,
1215 "PID %d handled a read-request for uid %d but this "
1216 "user is not reading from a repository", proc->pid,
1217 client->euid);
1220 return NULL;
1223 static const struct got_error *
1224 ensure_proc_is_writing(struct gotd_client *client,
1225 struct gotd_child_proc *proc)
1227 if (!client_is_writing(client)) {
1228 kill_proc(proc, 1);
1229 return got_error_fmt(GOT_ERR_BAD_PACKET,
1230 "PID %d handled a write-request for uid %d but this "
1231 "user is not writing to a repository", proc->pid,
1232 client->euid);
1235 return NULL;
1238 static int
1239 verify_imsg_src(struct gotd_client *client, struct gotd_child_proc *proc,
1240 struct imsg *imsg)
1242 const struct got_error *err;
1243 struct gotd_child_proc *client_proc;
1244 int ret = 0;
1246 client_proc = get_client_proc(client);
1247 if (proc->pid != client_proc->pid) {
1248 kill_proc(proc, 1);
1249 log_warnx("received message from PID %d for uid %d, while "
1250 "PID %d is the process serving this user",
1251 proc->pid, client->euid, client_proc->pid);
1252 return 0;
1255 switch (imsg->hdr.type) {
1256 case GOTD_IMSG_ERROR:
1257 ret = 1;
1258 break;
1259 case GOTD_IMSG_PACKFILE_DONE:
1260 err = ensure_proc_is_reading(client, proc);
1261 if (err)
1262 log_warnx("uid %d: %s", client->euid, err->msg);
1263 else
1264 ret = 1;
1265 break;
1266 case GOTD_IMSG_PACKFILE_INSTALL:
1267 case GOTD_IMSG_REF_UPDATES_START:
1268 case GOTD_IMSG_REF_UPDATE:
1269 err = ensure_proc_is_writing(client, proc);
1270 if (err)
1271 log_warnx("uid %d: %s", client->euid, err->msg);
1272 else
1273 ret = 1;
1274 break;
1275 default:
1276 log_debug("%s: unexpected imsg %d", __func__, imsg->hdr.type);
1277 break;
1280 return ret;
1283 static const struct got_error *
1284 recv_packfile_done(uint32_t *client_id, struct imsg *imsg)
1286 struct gotd_imsg_packfile_done idone;
1287 size_t datalen;
1289 log_debug("packfile-done received");
1291 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1292 if (datalen != sizeof(idone))
1293 return got_error(GOT_ERR_PRIVSEP_LEN);
1294 memcpy(&idone, imsg->data, sizeof(idone));
1296 *client_id = idone.client_id;
1297 return NULL;
1300 static const struct got_error *
1301 recv_packfile_install(uint32_t *client_id, struct imsg *imsg)
1303 struct gotd_imsg_packfile_install inst;
1304 size_t datalen;
1306 log_debug("packfile-install received");
1308 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1309 if (datalen != sizeof(inst))
1310 return got_error(GOT_ERR_PRIVSEP_LEN);
1311 memcpy(&inst, imsg->data, sizeof(inst));
1313 *client_id = inst.client_id;
1314 return NULL;
1317 static const struct got_error *
1318 recv_ref_updates_start(uint32_t *client_id, struct imsg *imsg)
1320 struct gotd_imsg_ref_updates_start istart;
1321 size_t datalen;
1323 log_debug("ref-updates-start received");
1325 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1326 if (datalen != sizeof(istart))
1327 return got_error(GOT_ERR_PRIVSEP_LEN);
1328 memcpy(&istart, imsg->data, sizeof(istart));
1330 *client_id = istart.client_id;
1331 return NULL;
1334 static const struct got_error *
1335 recv_ref_update(uint32_t *client_id, struct imsg *imsg)
1337 struct gotd_imsg_ref_update iref;
1338 size_t datalen;
1340 log_debug("ref-update received");
1342 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1343 if (datalen < sizeof(iref))
1344 return got_error(GOT_ERR_PRIVSEP_LEN);
1345 memcpy(&iref, imsg->data, sizeof(iref));
1347 *client_id = iref.client_id;
1348 return NULL;
1351 static const struct got_error *
1352 send_ref_update_ok(struct gotd_client *client,
1353 struct gotd_imsg_ref_update *iref, const char *refname)
1355 struct gotd_imsg_ref_update_ok iok;
1356 struct ibuf *wbuf;
1357 size_t len;
1359 memset(&iok, 0, sizeof(iok));
1360 iok.client_id = client->id;
1361 memcpy(iok.old_id, iref->old_id, SHA1_DIGEST_LENGTH);
1362 memcpy(iok.new_id, iref->new_id, SHA1_DIGEST_LENGTH);
1363 iok.name_len = strlen(refname);
1365 len = sizeof(iok) + iok.name_len;
1366 wbuf = imsg_create(&client->iev.ibuf, GOTD_IMSG_REF_UPDATE_OK,
1367 PROC_GOTD, gotd.pid, len);
1368 if (wbuf == NULL)
1369 return got_error_from_errno("imsg_create REF_UPDATE_OK");
1371 if (imsg_add(wbuf, &iok, sizeof(iok)) == -1)
1372 return got_error_from_errno("imsg_add REF_UPDATE_OK");
1373 if (imsg_add(wbuf, refname, iok.name_len) == -1)
1374 return got_error_from_errno("imsg_add REF_UPDATE_OK");
1376 wbuf->fd = -1;
1377 imsg_close(&client->iev.ibuf, wbuf);
1378 gotd_imsg_event_add(&client->iev);
1379 return NULL;
1382 static void
1383 send_refs_updated(struct gotd_client *client)
1385 if (gotd_imsg_compose_event(&client->iev,
1386 GOTD_IMSG_REFS_UPDATED, PROC_GOTD, -1, NULL, 0) == -1)
1387 log_warn("imsg compose REFS_UPDATED");
1390 static const struct got_error *
1391 send_ref_update_ng(struct gotd_client *client,
1392 struct gotd_imsg_ref_update *iref, const char *refname,
1393 const char *reason)
1395 const struct got_error *ng_err;
1396 struct gotd_imsg_ref_update_ng ing;
1397 struct ibuf *wbuf;
1398 size_t len;
1400 memset(&ing, 0, sizeof(ing));
1401 ing.client_id = client->id;
1402 memcpy(ing.old_id, iref->old_id, SHA1_DIGEST_LENGTH);
1403 memcpy(ing.new_id, iref->new_id, SHA1_DIGEST_LENGTH);
1404 ing.name_len = strlen(refname);
1406 ng_err = got_error_fmt(GOT_ERR_REF_BUSY, "%s", reason);
1407 ing.reason_len = strlen(ng_err->msg);
1409 len = sizeof(ing) + ing.name_len + ing.reason_len;
1410 wbuf = imsg_create(&client->iev.ibuf, GOTD_IMSG_REF_UPDATE_NG,
1411 PROC_GOTD, gotd.pid, len);
1412 if (wbuf == NULL)
1413 return got_error_from_errno("imsg_create REF_UPDATE_NG");
1415 if (imsg_add(wbuf, &ing, sizeof(ing)) == -1)
1416 return got_error_from_errno("imsg_add REF_UPDATE_NG");
1417 if (imsg_add(wbuf, refname, ing.name_len) == -1)
1418 return got_error_from_errno("imsg_add REF_UPDATE_NG");
1419 if (imsg_add(wbuf, ng_err->msg, ing.reason_len) == -1)
1420 return got_error_from_errno("imsg_add REF_UPDATE_NG");
1422 wbuf->fd = -1;
1423 imsg_close(&client->iev.ibuf, wbuf);
1424 gotd_imsg_event_add(&client->iev);
1425 return NULL;
1428 static const struct got_error *
1429 install_pack(struct gotd_client *client, const char *repo_path,
1430 struct imsg *imsg)
1432 const struct got_error *err = NULL;
1433 struct gotd_imsg_packfile_install inst;
1434 char hex[SHA1_DIGEST_STRING_LENGTH];
1435 size_t datalen;
1436 char *packfile_path = NULL, *packidx_path = NULL;
1438 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1439 if (datalen != sizeof(inst))
1440 return got_error(GOT_ERR_PRIVSEP_LEN);
1441 memcpy(&inst, imsg->data, sizeof(inst));
1443 if (client->packfile_path == NULL)
1444 return got_error_msg(GOT_ERR_BAD_REQUEST,
1445 "client has no pack file");
1446 if (client->packidx_path == NULL)
1447 return got_error_msg(GOT_ERR_BAD_REQUEST,
1448 "client has no pack file index");
1450 if (got_sha1_digest_to_str(inst.pack_sha1, hex, sizeof(hex)) == NULL)
1451 return got_error_msg(GOT_ERR_NO_SPACE,
1452 "could not convert pack file SHA1 to hex");
1454 if (asprintf(&packfile_path, "/%s/%s/pack-%s.pack",
1455 repo_path, GOT_OBJECTS_PACK_DIR, hex) == -1) {
1456 err = got_error_from_errno("asprintf");
1457 goto done;
1460 if (asprintf(&packidx_path, "/%s/%s/pack-%s.idx",
1461 repo_path, GOT_OBJECTS_PACK_DIR, hex) == -1) {
1462 err = got_error_from_errno("asprintf");
1463 goto done;
1466 if (rename(client->packfile_path, packfile_path) == -1) {
1467 err = got_error_from_errno3("rename", client->packfile_path,
1468 packfile_path);
1469 goto done;
1472 free(client->packfile_path);
1473 client->packfile_path = NULL;
1475 if (rename(client->packidx_path, packidx_path) == -1) {
1476 err = got_error_from_errno3("rename", client->packidx_path,
1477 packidx_path);
1478 goto done;
1481 free(client->packidx_path);
1482 client->packidx_path = NULL;
1483 done:
1484 free(packfile_path);
1485 free(packidx_path);
1486 return err;
1489 static const struct got_error *
1490 begin_ref_updates(struct gotd_client *client, struct imsg *imsg)
1492 struct gotd_imsg_ref_updates_start istart;
1493 size_t datalen;
1495 if (client->nref_updates != -1)
1496 return got_error(GOT_ERR_PRIVSEP_MSG);
1498 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1499 if (datalen != sizeof(istart))
1500 return got_error(GOT_ERR_PRIVSEP_LEN);
1501 memcpy(&istart, imsg->data, sizeof(istart));
1503 if (istart.nref_updates <= 0)
1504 return got_error(GOT_ERR_PRIVSEP_MSG);
1506 client->nref_updates = istart.nref_updates;
1507 return NULL;
1510 static const struct got_error *
1511 update_ref(struct gotd_client *client, const char *repo_path,
1512 struct imsg *imsg)
1514 const struct got_error *err = NULL;
1515 struct got_repository *repo = NULL;
1516 struct got_reference *ref = NULL;
1517 struct gotd_imsg_ref_update iref;
1518 struct got_object_id old_id, new_id;
1519 struct got_object_id *id = NULL;
1520 struct got_object *obj = NULL;
1521 char *refname = NULL;
1522 size_t datalen;
1523 int locked = 0;
1525 log_debug("update-ref from uid %d", client->euid);
1527 if (client->nref_updates <= 0)
1528 return got_error(GOT_ERR_PRIVSEP_MSG);
1530 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1531 if (datalen < sizeof(iref))
1532 return got_error(GOT_ERR_PRIVSEP_LEN);
1533 memcpy(&iref, imsg->data, sizeof(iref));
1534 if (datalen != sizeof(iref) + iref.name_len)
1535 return got_error(GOT_ERR_PRIVSEP_LEN);
1536 refname = malloc(iref.name_len + 1);
1537 if (refname == NULL)
1538 return got_error_from_errno("malloc");
1539 memcpy(refname, imsg->data + sizeof(iref), iref.name_len);
1540 refname[iref.name_len] = '\0';
1542 log_debug("updating ref %s for uid %d", refname, client->euid);
1544 err = got_repo_open(&repo, repo_path, NULL, NULL);
1545 if (err)
1546 goto done;
1548 memcpy(old_id.sha1, iref.old_id, SHA1_DIGEST_LENGTH);
1549 memcpy(new_id.sha1, iref.new_id, SHA1_DIGEST_LENGTH);
1550 err = got_object_open(&obj, repo, &new_id);
1551 if (err)
1552 goto done;
1554 if (iref.ref_is_new) {
1555 err = got_ref_open(&ref, repo, refname, 0);
1556 if (err) {
1557 if (err->code != GOT_ERR_NOT_REF)
1558 goto done;
1559 err = got_ref_alloc(&ref, refname, &new_id);
1560 if (err)
1561 goto done;
1562 err = got_ref_write(ref, repo); /* will lock/unlock */
1563 if (err)
1564 goto done;
1565 } else {
1566 err = got_error_fmt(GOT_ERR_REF_BUSY,
1567 "%s has been created by someone else "
1568 "while transaction was in progress",
1569 got_ref_get_name(ref));
1570 goto done;
1572 } else {
1573 err = got_ref_open(&ref, repo, refname, 1 /* lock */);
1574 if (err)
1575 goto done;
1576 locked = 1;
1578 err = got_ref_resolve(&id, repo, ref);
1579 if (err)
1580 goto done;
1582 if (got_object_id_cmp(id, &old_id) != 0) {
1583 err = got_error_fmt(GOT_ERR_REF_BUSY,
1584 "%s has been modified by someone else "
1585 "while transaction was in progress",
1586 got_ref_get_name(ref));
1587 goto done;
1590 err = got_ref_change_ref(ref, &new_id);
1591 if (err)
1592 goto done;
1594 err = got_ref_write(ref, repo);
1595 if (err)
1596 goto done;
1598 free(id);
1599 id = NULL;
1601 done:
1602 if (err) {
1603 if (err->code == GOT_ERR_LOCKFILE_TIMEOUT) {
1604 err = got_error_fmt(GOT_ERR_LOCKFILE_TIMEOUT,
1605 "could not acquire exclusive file lock for %s",
1606 refname);
1608 send_ref_update_ng(client, &iref, refname, err->msg);
1609 } else
1610 send_ref_update_ok(client, &iref, refname);
1612 if (client->nref_updates > 0) {
1613 client->nref_updates--;
1614 if (client->nref_updates == 0)
1615 send_refs_updated(client);
1618 if (locked) {
1619 const struct got_error *unlock_err;
1620 unlock_err = got_ref_unlock(ref);
1621 if (unlock_err && err == NULL)
1622 err = unlock_err;
1624 if (ref)
1625 got_ref_close(ref);
1626 if (obj)
1627 got_object_close(obj);
1628 if (repo)
1629 got_repo_close(repo);
1630 free(refname);
1631 free(id);
1632 return err;
1635 static void
1636 gotd_dispatch(int fd, short event, void *arg)
1638 struct gotd_imsgev *iev = arg;
1639 struct imsgbuf *ibuf = &iev->ibuf;
1640 struct gotd_child_proc *proc = NULL;
1641 ssize_t n;
1642 int shut = 0;
1643 struct imsg imsg;
1645 if (event & EV_READ) {
1646 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1647 fatal("imsg_read error");
1648 if (n == 0) {
1649 /* Connection closed. */
1650 shut = 1;
1651 goto done;
1655 if (event & EV_WRITE) {
1656 n = msgbuf_write(&ibuf->w);
1657 if (n == -1 && errno != EAGAIN)
1658 fatal("msgbuf_write");
1659 if (n == 0) {
1660 /* Connection closed. */
1661 shut = 1;
1662 goto done;
1666 proc = find_proc_by_fd(fd);
1667 if (proc == NULL)
1668 fatalx("cannot find child process for fd %d", fd);
1670 for (;;) {
1671 const struct got_error *err = NULL;
1672 struct gotd_client *client = NULL;
1673 uint32_t client_id = 0;
1674 int do_disconnect = 0;
1675 int do_ref_updates = 0, do_ref_update = 0;
1676 int do_packfile_install = 0;
1678 if ((n = imsg_get(ibuf, &imsg)) == -1)
1679 fatal("%s: imsg_get error", __func__);
1680 if (n == 0) /* No more messages. */
1681 break;
1683 switch (imsg.hdr.type) {
1684 case GOTD_IMSG_ERROR:
1685 do_disconnect = 1;
1686 err = gotd_imsg_recv_error(&client_id, &imsg);
1687 break;
1688 case GOTD_IMSG_PACKFILE_DONE:
1689 do_disconnect = 1;
1690 err = recv_packfile_done(&client_id, &imsg);
1691 break;
1692 case GOTD_IMSG_PACKFILE_INSTALL:
1693 err = recv_packfile_install(&client_id, &imsg);
1694 if (err == NULL)
1695 do_packfile_install = 1;
1696 break;
1697 case GOTD_IMSG_REF_UPDATES_START:
1698 err = recv_ref_updates_start(&client_id, &imsg);
1699 if (err == NULL)
1700 do_ref_updates = 1;
1701 break;
1702 case GOTD_IMSG_REF_UPDATE:
1703 err = recv_ref_update(&client_id, &imsg);
1704 if (err == NULL)
1705 do_ref_update = 1;
1706 break;
1707 default:
1708 log_debug("unexpected imsg %d", imsg.hdr.type);
1709 break;
1712 client = find_client(client_id);
1713 if (client == NULL) {
1714 log_warnx("%s: client not found", __func__);
1715 imsg_free(&imsg);
1716 continue;
1719 if (!verify_imsg_src(client, proc, &imsg)) {
1720 log_debug("dropping imsg type %d from PID %d",
1721 imsg.hdr.type, proc->pid);
1722 imsg_free(&imsg);
1723 continue;
1725 if (err)
1726 log_warnx("uid %d: %s", client->euid, err->msg);
1728 if (do_disconnect) {
1729 if (err)
1730 disconnect_on_error(client, err);
1731 else
1732 disconnect(client);
1733 } else if (do_packfile_install)
1734 err = install_pack(client, proc->chroot_path, &imsg);
1735 else if (do_ref_updates)
1736 err = begin_ref_updates(client, &imsg);
1737 else if (do_ref_update)
1738 err = update_ref(client, proc->chroot_path, &imsg);
1740 if (err)
1741 log_warnx("uid %d: %s", client->euid, err->msg);
1742 imsg_free(&imsg);
1744 done:
1745 if (!shut) {
1746 gotd_imsg_event_add(iev);
1747 } else {
1748 /* This pipe is dead. Remove its event handler */
1749 event_del(&iev->ev);
1750 event_loopexit(NULL);
1754 static pid_t
1755 start_child(enum gotd_procid proc_id, const char *chroot_path,
1756 char *argv0, int fd, int daemonize, int verbosity)
1758 char *argv[9];
1759 int argc = 0;
1760 pid_t pid;
1762 switch (pid = fork()) {
1763 case -1:
1764 fatal("cannot fork");
1765 case 0:
1766 break;
1767 default:
1768 close(fd);
1769 return pid;
1772 if (fd != GOTD_SOCK_FILENO) {
1773 if (dup2(fd, GOTD_SOCK_FILENO) == -1)
1774 fatal("cannot setup imsg fd");
1775 } else if (fcntl(fd, F_SETFD, 0) == -1)
1776 fatal("cannot setup imsg fd");
1778 argv[argc++] = argv0;
1779 switch (proc_id) {
1780 case PROC_REPO_READ:
1781 argv[argc++] = (char *)"-R";
1782 break;
1783 case PROC_REPO_WRITE:
1784 argv[argc++] = (char *)"-W";
1785 break;
1786 default:
1787 fatalx("invalid process id %d", proc_id);
1790 argv[argc++] = (char *)"-P";
1791 argv[argc++] = (char *)chroot_path;
1793 if (!daemonize)
1794 argv[argc++] = (char *)"-d";
1795 if (verbosity > 0)
1796 argv[argc++] = (char *)"-v";
1797 if (verbosity > 1)
1798 argv[argc++] = (char *)"-v";
1799 argv[argc++] = NULL;
1801 execvp(argv0, argv);
1802 fatal("execvp");
1805 static void
1806 start_repo_children(struct gotd *gotd, char *argv0, int daemonize,
1807 int verbosity)
1809 struct gotd_repo *repo = NULL;
1810 struct gotd_child_proc *proc;
1811 int i;
1814 * XXX For now, use one reader and one writer per repository.
1815 * This should be changed to N readers + M writers.
1817 gotd->nprocs = gotd->nrepos * 2;
1818 gotd->procs = calloc(gotd->nprocs, sizeof(*gotd->procs));
1819 if (gotd->procs == NULL)
1820 fatal("calloc");
1821 for (i = 0; i < gotd->nprocs; i++) {
1822 if (repo == NULL)
1823 repo = TAILQ_FIRST(&gotd->repos);
1824 proc = &gotd->procs[i];
1825 if (i < gotd->nrepos)
1826 proc->type = PROC_REPO_READ;
1827 else
1828 proc->type = PROC_REPO_WRITE;
1829 if (strlcpy(proc->repo_name, repo->name,
1830 sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
1831 fatalx("repository name too long: %s", repo->name);
1832 log_debug("adding repository %s", repo->name);
1833 if (realpath(repo->path, proc->chroot_path) == NULL)
1834 fatal("%s", repo->path);
1835 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1836 PF_UNSPEC, proc->pipe) == -1)
1837 fatal("socketpair");
1838 proc->pid = start_child(proc->type, proc->chroot_path, argv0,
1839 proc->pipe[1], daemonize, verbosity);
1840 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1841 log_debug("proc %s %s is on fd %d",
1842 gotd_proc_names[proc->type], proc->chroot_path,
1843 proc->pipe[0]);
1844 proc->iev.handler = gotd_dispatch;
1845 proc->iev.events = EV_READ;
1846 proc->iev.handler_arg = NULL;
1847 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1848 gotd_dispatch, &proc->iev);
1850 repo = TAILQ_NEXT(repo, entry);
1854 static void
1855 apply_unveil(void)
1857 struct gotd_repo *repo;
1859 TAILQ_FOREACH(repo, &gotd.repos, entry) {
1860 if (unveil(repo->path, "rwc") == -1)
1861 fatal("unveil %s", repo->path);
1864 if (unveil(GOT_TMPDIR_STR, "rwc") == -1)
1865 fatal("unveil %s", GOT_TMPDIR_STR);
1867 if (unveil(NULL, NULL) == -1)
1868 fatal("unveil");
1871 int
1872 main(int argc, char **argv)
1874 const struct got_error *error = NULL;
1875 int ch, fd = -1, daemonize = 1, verbosity = 0, noaction = 0;
1876 const char *confpath = GOTD_CONF_PATH;
1877 char *argv0 = argv[0];
1878 char title[2048];
1879 gid_t groups[NGROUPS_MAX + 1];
1880 int ngroups = NGROUPS_MAX + 1;
1881 struct passwd *pw = NULL;
1882 struct group *gr = NULL;
1883 char *repo_path = NULL;
1884 enum gotd_procid proc_id = PROC_GOTD;
1885 struct event evsigint, evsigterm, evsighup, evsigusr1;
1886 int *pack_fds = NULL, *temp_fds = NULL;
1888 log_init(1, LOG_DAEMON); /* Log to stderr until daemonized. */
1890 while ((ch = getopt(argc, argv, "df:nvRWP:")) != -1) {
1891 switch (ch) {
1892 case 'd':
1893 daemonize = 0;
1894 break;
1895 case 'f':
1896 confpath = optarg;
1897 break;
1898 case 'n':
1899 noaction = 1;
1900 break;
1901 case 'v':
1902 if (verbosity < 3)
1903 verbosity++;
1904 break;
1905 case 'R':
1906 proc_id = PROC_REPO_READ;
1907 break;
1908 case 'W':
1909 proc_id = PROC_REPO_WRITE;
1910 break;
1911 case 'P':
1912 repo_path = realpath(optarg, NULL);
1913 if (repo_path == NULL)
1914 fatal("realpath '%s'", optarg);
1915 break;
1916 default:
1917 usage();
1921 argc -= optind;
1922 argv += optind;
1924 if (argc != 0)
1925 usage();
1927 if (geteuid())
1928 fatalx("need root privileges");
1930 log_init(daemonize ? 0 : 1, LOG_DAEMON);
1931 log_setverbose(verbosity);
1933 if (parse_config(confpath, proc_id, &gotd) != 0)
1934 return 1;
1936 if (proc_id == PROC_GOTD &&
1937 (gotd.nrepos == 0 || TAILQ_EMPTY(&gotd.repos)))
1938 fatalx("no repository defined in configuration file");
1940 pw = getpwnam(gotd.user_name);
1941 if (pw == NULL)
1942 fatal("getpwuid: user %s not found", gotd.user_name);
1944 if (pw->pw_uid == 0) {
1945 fatalx("cannot run %s as %s: the user running %s "
1946 "must not be the superuser",
1947 getprogname(), pw->pw_name, getprogname());
1950 if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups) == -1)
1951 log_warnx("group membership list truncated");
1953 gr = match_group(groups, ngroups, gotd.unix_group_name);
1954 if (gr == NULL) {
1955 fatalx("cannot start %s: the user running %s "
1956 "must be a secondary member of group %s",
1957 getprogname(), getprogname(), gotd.unix_group_name);
1959 if (gr->gr_gid == pw->pw_gid) {
1960 fatalx("cannot start %s: the user running %s "
1961 "must be a secondary member of group %s, but "
1962 "%s is the user's primary group",
1963 getprogname(), getprogname(), gotd.unix_group_name,
1964 gotd.unix_group_name);
1967 if (proc_id == PROC_GOTD &&
1968 !got_path_is_absolute(gotd.unix_socket_path))
1969 fatalx("bad unix socket path \"%s\": must be an absolute path",
1970 gotd.unix_socket_path);
1972 if (noaction)
1973 return 0;
1975 if (proc_id == PROC_GOTD && verbosity) {
1976 log_info("socket: %s", gotd.unix_socket_path);
1977 log_info("user: %s", pw->pw_name);
1978 log_info("secondary group: %s", gr->gr_name);
1980 fd = unix_socket_listen(gotd.unix_socket_path, pw->pw_uid,
1981 gr->gr_gid);
1982 if (fd == -1) {
1983 fatal("cannot listen on unix socket %s",
1984 gotd.unix_socket_path);
1988 if (proc_id == PROC_GOTD) {
1989 gotd.pid = getpid();
1990 snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
1991 start_repo_children(&gotd, argv0, daemonize, verbosity);
1992 arc4random_buf(&clients_hash_key, sizeof(clients_hash_key));
1993 if (daemonize && daemon(0, 0) == -1)
1994 fatal("daemon");
1995 } else if (proc_id == PROC_REPO_READ || proc_id == PROC_REPO_WRITE) {
1996 error = got_repo_pack_fds_open(&pack_fds);
1997 if (error != NULL)
1998 fatalx("cannot open pack tempfiles: %s", error->msg);
1999 error = got_repo_temp_fds_open(&temp_fds);
2000 if (error != NULL)
2001 fatalx("cannot open pack tempfiles: %s", error->msg);
2002 if (repo_path == NULL)
2003 fatalx("repository path not specified");
2004 snprintf(title, sizeof(title), "%s %s",
2005 gotd_proc_names[proc_id], repo_path);
2006 if (chroot(repo_path) == -1)
2007 fatal("chroot");
2008 if (chdir("/") == -1)
2009 fatal("chdir(\"/\")");
2010 if (daemonize && daemon(1, 0) == -1)
2011 fatal("daemon");
2012 } else
2013 fatal("invalid process id %d", proc_id);
2015 setproctitle("%s", title);
2016 log_procinit(title);
2018 /* Drop root privileges. */
2019 if (setgid(pw->pw_gid) == -1)
2020 fatal("setgid %d failed", pw->pw_gid);
2021 if (setuid(pw->pw_uid) == -1)
2022 fatal("setuid %d failed", pw->pw_uid);
2024 event_init();
2026 switch (proc_id) {
2027 case PROC_GOTD:
2028 #ifndef PROFILE
2029 if (pledge("stdio rpath wpath cpath proc getpw sendfd recvfd "
2030 "fattr flock unix unveil", NULL) == -1)
2031 err(1, "pledge");
2032 #endif
2033 break;
2034 case PROC_REPO_READ:
2035 #ifndef PROFILE
2036 if (pledge("stdio rpath sendfd recvfd", NULL) == -1)
2037 err(1, "pledge");
2038 #endif
2039 repo_read_main(title, pack_fds, temp_fds);
2040 /* NOTREACHED */
2041 exit(0);
2042 case PROC_REPO_WRITE:
2043 #ifndef PROFILE
2044 if (pledge("stdio rpath sendfd recvfd", NULL) == -1)
2045 err(1, "pledge");
2046 #endif
2047 repo_write_main(title, pack_fds, temp_fds);
2048 /* NOTREACHED */
2049 exit(0);
2050 default:
2051 fatal("invalid process id %d", proc_id);
2054 if (proc_id != PROC_GOTD)
2055 fatal("invalid process id %d", proc_id);
2057 apply_unveil();
2059 signal_set(&evsigint, SIGINT, gotd_sighdlr, NULL);
2060 signal_set(&evsigterm, SIGTERM, gotd_sighdlr, NULL);
2061 signal_set(&evsighup, SIGHUP, gotd_sighdlr, NULL);
2062 signal_set(&evsigusr1, SIGUSR1, gotd_sighdlr, NULL);
2063 signal(SIGPIPE, SIG_IGN);
2065 signal_add(&evsigint, NULL);
2066 signal_add(&evsigterm, NULL);
2067 signal_add(&evsighup, NULL);
2068 signal_add(&evsigusr1, NULL);
2070 event_set(&gotd.ev, fd, EV_READ | EV_PERSIST, gotd_accept, NULL);
2071 if (event_add(&gotd.ev, NULL))
2072 fatalx("event add");
2073 evtimer_set(&gotd.pause, gotd_accept_paused, NULL);
2075 event_dispatch();
2077 if (fd != -1)
2078 close(fd);
2079 if (pack_fds)
2080 got_repo_pack_fds_close(pack_fds);
2081 free(repo_path);
2082 return 0;