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 /* Send pack pipe end 0 to repo_write. */
725 if (gotd_imsg_compose_event(&client->repo_write->iev,
726 GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[0],
727 &ipipe, sizeof(ipipe)) == -1) {
728 err = got_error_from_errno("imsg compose PACKFILE_PIPE");
729 pipe[0] = -1;
730 goto done;
732 pipe[0] = -1;
734 /* Send pack pipe end 1 to gotsh(1) (expects just an fd, no data). */
735 if (gotd_imsg_compose_event(&client->iev,
736 GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[1], NULL, 0) == -1)
737 err = got_error_from_errno("imsg compose PACKFILE_PIPE");
738 pipe[1] = -1;
740 if (asprintf(&basepath, "%s/%s/receiving-from-uid-%d.pack",
741 client->repo_write->chroot_path, GOT_OBJECTS_PACK_DIR,
742 client->euid) == -1) {
743 err = got_error_from_errno("asprintf");
744 goto done;
747 err = got_opentemp_named_fd(&pack_path, &packfd, basepath);
748 if (err)
749 goto done;
751 free(basepath);
752 if (asprintf(&basepath, "%s/%s/receiving-from-uid-%d.idx",
753 client->repo_write->chroot_path, GOT_OBJECTS_PACK_DIR,
754 client->euid) == -1) {
755 err = got_error_from_errno("asprintf");
756 basepath = NULL;
757 goto done;
759 err = got_opentemp_named_fd(&idx_path, &idxfd, basepath);
760 if (err)
761 goto done;
763 memset(&ifile, 0, sizeof(ifile));
764 ifile.client_id = client->id;
765 if (gotd_imsg_compose_event(&client->repo_write->iev,
766 GOTD_IMSG_PACKIDX_FILE, PROC_GOTD, idxfd,
767 &ifile, sizeof(ifile)) == -1) {
768 err = got_error_from_errno("imsg compose PACKIDX_FILE");
769 idxfd = -1;
770 goto done;
772 idxfd = -1;
774 memset(&ipack, 0, sizeof(ipack));
775 ipack.client_id = client->id;
776 if (client_has_capability(client, GOT_CAPA_REPORT_STATUS))
777 ipack.report_status = 1;
779 if (gotd_imsg_compose_event(&client->repo_write->iev,
780 GOTD_IMSG_RECV_PACKFILE, PROC_GOTD, packfd,
781 &ipack, sizeof(ipack)) == -1) {
782 err = got_error_from_errno("imsg compose RECV_PACKFILE");
783 packfd = -1;
784 goto done;
786 packfd = -1;
788 done:
789 free(basepath);
790 if (pipe[0] != -1 && close(pipe[0]) == -1 && err == NULL)
791 err = got_error_from_errno("close");
792 if (pipe[1] != -1 && close(pipe[1]) == -1 && err == NULL)
793 err = got_error_from_errno("close");
794 if (packfd != -1 && close(packfd) == -1 && err == NULL)
795 err = got_error_from_errno("close");
796 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
797 err = got_error_from_errno("close");
798 if (err) {
799 free(pack_path);
800 free(idx_path);
801 } else {
802 client->packfile_path = pack_path;
803 client->packidx_path = idx_path;
805 return err;
808 static void
809 gotd_request(int fd, short events, void *arg)
811 struct gotd_imsgev *iev = arg;
812 struct imsgbuf *ibuf = &iev->ibuf;
813 struct gotd_client *client = iev->handler_arg;
814 const struct got_error *err = NULL;
815 struct imsg imsg;
816 ssize_t n;
818 if (events & EV_WRITE) {
819 while (ibuf->w.queued) {
820 n = msgbuf_write(&ibuf->w);
821 if (n == -1 && errno == EPIPE) {
822 /*
823 * The client has closed its socket.
824 * This can happen when Git clients are
825 * done sending pack file data.
826 */
827 msgbuf_clear(&ibuf->w);
828 continue;
829 } else if (n == -1 && errno != EAGAIN) {
830 err = got_error_from_errno("imsg_flush");
831 disconnect_on_error(client, err);
832 return;
834 if (n == 0) {
835 /* Connection closed. */
836 err = got_error(GOT_ERR_EOF);
837 disconnect_on_error(client, err);
838 return;
843 if ((events & EV_READ) == 0)
844 return;
846 memset(&imsg, 0, sizeof(imsg));
848 while (err == NULL) {
849 err = gotd_imsg_recv(&imsg, ibuf, 0);
850 if (err) {
851 if (err->code == GOT_ERR_PRIVSEP_READ)
852 err = NULL;
853 break;
856 evtimer_del(&client->tmo);
858 switch (imsg.hdr.type) {
859 case GOTD_IMSG_LIST_REFS:
860 if (client->state != GOTD_STATE_EXPECT_LIST_REFS) {
861 err = got_error_msg(GOT_ERR_BAD_REQUEST,
862 "unexpected list-refs request received");
863 break;
865 err = forward_list_refs_request(client, &imsg);
866 if (err)
867 break;
868 client->state = GOTD_STATE_EXPECT_CAPABILITIES;
869 log_debug("uid %d: expecting capabilities",
870 client->euid);
871 break;
872 case GOTD_IMSG_CAPABILITIES:
873 if (client->state != GOTD_STATE_EXPECT_CAPABILITIES) {
874 err = got_error_msg(GOT_ERR_BAD_REQUEST,
875 "unexpected capabilities received");
876 break;
878 log_debug("receiving capabilities from uid %d",
879 client->euid);
880 err = recv_capabilities(client, &imsg);
881 break;
882 case GOTD_IMSG_CAPABILITY:
883 if (client->state != GOTD_STATE_EXPECT_CAPABILITIES) {
884 err = got_error_msg(GOT_ERR_BAD_REQUEST,
885 "unexpected capability received");
886 break;
888 err = recv_capability(client, &imsg);
889 if (err || client->ncapabilities < client->ncapa_alloc)
890 break;
891 if (client_is_reading(client)) {
892 client->state = GOTD_STATE_EXPECT_WANT;
893 log_debug("uid %d: expecting want-lines",
894 client->euid);
895 } else if (client_is_writing(client)) {
896 client->state = GOTD_STATE_EXPECT_REF_UPDATE;
897 log_debug("uid %d: expecting ref-update-lines",
898 client->euid);
899 } else
900 fatalx("client %d is both reading and writing",
901 client->euid);
902 break;
903 case GOTD_IMSG_WANT:
904 if (client->state != GOTD_STATE_EXPECT_WANT) {
905 err = got_error_msg(GOT_ERR_BAD_REQUEST,
906 "unexpected want-line received");
907 break;
909 log_debug("received want-line from uid %d",
910 client->euid);
911 err = ensure_client_is_reading(client);
912 if (err)
913 break;
914 err = forward_want(client, &imsg);
915 break;
916 case GOTD_IMSG_REF_UPDATE:
917 if (client->state != GOTD_STATE_EXPECT_REF_UPDATE) {
918 err = got_error_msg(GOT_ERR_BAD_REQUEST,
919 "unexpected ref-update-line received");
920 break;
922 log_debug("received ref-update-line from uid %d",
923 client->euid);
924 err = ensure_client_is_writing(client);
925 if (err)
926 break;
927 err = forward_ref_update(client, &imsg);
928 if (err)
929 break;
930 client->state = GOTD_STATE_EXPECT_MORE_REF_UPDATES;
931 break;
932 case GOTD_IMSG_HAVE:
933 if (client->state != GOTD_STATE_EXPECT_HAVE) {
934 err = got_error_msg(GOT_ERR_BAD_REQUEST,
935 "unexpected have-line received");
936 break;
938 log_debug("received have-line from uid %d",
939 client->euid);
940 err = ensure_client_is_reading(client);
941 if (err)
942 break;
943 err = forward_have(client, &imsg);
944 if (err)
945 break;
946 break;
947 case GOTD_IMSG_FLUSH:
948 if (client->state == GOTD_STATE_EXPECT_WANT ||
949 client->state == GOTD_STATE_EXPECT_HAVE) {
950 err = ensure_client_is_reading(client);
951 if (err)
952 break;
953 } else if (client->state ==
954 GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
955 err = ensure_client_is_writing(client);
956 if (err)
957 break;
958 } else {
959 err = got_error_msg(GOT_ERR_BAD_REQUEST,
960 "unexpected flush-pkt received");
961 break;
963 log_debug("received flush-pkt from uid %d",
964 client->euid);
965 if (client->state == GOTD_STATE_EXPECT_WANT) {
966 client->state = GOTD_STATE_EXPECT_HAVE;
967 log_debug("uid %d: expecting have-lines",
968 client->euid);
969 } else if (client->state == GOTD_STATE_EXPECT_HAVE) {
970 client->state = GOTD_STATE_EXPECT_DONE;
971 log_debug("uid %d: expecting 'done'",
972 client->euid);
973 } else if (client->state ==
974 GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
975 client->state = GOTD_STATE_EXPECT_PACKFILE;
976 log_debug("uid %d: expecting packfile",
977 client->euid);
978 err = recv_packfile(client);
979 } else {
980 /* should not happen, see above */
981 err = got_error_msg(GOT_ERR_BAD_REQUEST,
982 "unexpected client state");
983 break;
985 break;
986 case GOTD_IMSG_DONE:
987 if (client->state != GOTD_STATE_EXPECT_HAVE &&
988 client->state != GOTD_STATE_EXPECT_DONE) {
989 err = got_error_msg(GOT_ERR_BAD_REQUEST,
990 "unexpected flush-pkt received");
991 break;
993 log_debug("received 'done' from uid %d", client->euid);
994 err = ensure_client_is_reading(client);
995 if (err)
996 break;
997 client->state = GOTD_STATE_DONE;
998 err = send_packfile(client);
999 break;
1000 default:
1001 err = got_error(GOT_ERR_PRIVSEP_MSG);
1002 break;
1005 imsg_free(&imsg);
1008 if (err) {
1009 if (err->code != GOT_ERR_EOF ||
1010 client->state != GOTD_STATE_EXPECT_PACKFILE)
1011 disconnect_on_error(client, err);
1012 } else {
1013 gotd_imsg_event_add(&client->iev);
1014 evtimer_add(&client->tmo, &timeout);
1018 static void
1019 gotd_request_timeout(int fd, short events, void *arg)
1021 struct gotd_client *client = arg;
1023 log_debug("disconnecting uid %d due to timeout", client->euid);
1024 disconnect(client);
1027 static void
1028 gotd_accept(int fd, short event, void *arg)
1030 struct sockaddr_storage ss;
1031 struct timeval backoff;
1032 socklen_t len;
1033 int s = -1;
1034 struct gotd_client *client = NULL;
1035 uid_t euid;
1036 gid_t egid;
1038 backoff.tv_sec = 1;
1039 backoff.tv_usec = 0;
1041 if (event_add(&gotd.ev, NULL) == -1) {
1042 log_warn("event_add");
1043 return;
1045 if (event & EV_TIMEOUT)
1046 return;
1048 len = sizeof(ss);
1050 s = accept_reserve(fd, (struct sockaddr *)&ss, &len, GOTD_FD_RESERVE,
1051 &inflight);
1053 if (s == -1) {
1054 switch (errno) {
1055 case EINTR:
1056 case EWOULDBLOCK:
1057 case ECONNABORTED:
1058 return;
1059 case EMFILE:
1060 case ENFILE:
1061 event_del(&gotd.ev);
1062 evtimer_add(&gotd.pause, &backoff);
1063 return;
1064 default:
1065 log_warn("%s: accept", __func__);
1066 return;
1070 if (client_cnt >= GOTD_MAXCLIENTS)
1071 goto err;
1073 if (getpeereid(s, &euid, &egid) == -1) {
1074 log_warn("%s: getpeereid", __func__);
1075 goto err;
1078 client = calloc(1, sizeof(*client));
1079 if (client == NULL) {
1080 log_warn("%s: calloc", __func__);
1081 goto err;
1084 client->state = GOTD_STATE_EXPECT_LIST_REFS;
1085 client->id = get_client_id();
1086 client->fd = s;
1087 s = -1;
1088 client->delta_cache_fd = -1;
1089 client->euid = euid;
1090 client->egid = egid;
1091 client->nref_updates = -1;
1093 imsg_init(&client->iev.ibuf, client->fd);
1094 client->iev.handler = gotd_request;
1095 client->iev.events = EV_READ;
1096 client->iev.handler_arg = client;
1098 event_set(&client->iev.ev, client->fd, EV_READ, gotd_request,
1099 &client->iev);
1100 gotd_imsg_event_add(&client->iev);
1102 evtimer_set(&client->tmo, gotd_request_timeout, client);
1104 add_client(client);
1105 log_debug("%s: new client uid %d connected on fd %d", __func__,
1106 client->euid, client->fd);
1107 return;
1108 err:
1109 inflight--;
1110 if (s != -1)
1111 close(s);
1112 free(client);
1115 static void
1116 gotd_accept_paused(int fd, short event, void *arg)
1118 event_add(&gotd.ev, NULL);
1121 static const char *gotd_proc_names[PROC_MAX] = {
1122 "parent",
1123 "repo_read",
1124 "repo_write"
1127 static struct gotd_child_proc *
1128 get_proc_for_pid(pid_t pid)
1130 struct gotd_child_proc *proc;
1131 int i;
1133 for (i = 0; i < gotd.nprocs; i++) {
1134 proc = &gotd.procs[i];
1135 if (proc->pid == pid)
1136 return proc;
1139 return NULL;
1142 static void
1143 kill_proc(struct gotd_child_proc *proc, int fatal)
1145 if (fatal) {
1146 log_warnx("sending SIGKILL to PID %d", proc->pid);
1147 kill(proc->pid, SIGKILL);
1148 } else
1149 kill(proc->pid, SIGTERM);
1152 static void
1153 gotd_shutdown(void)
1155 pid_t pid;
1156 int status, i;
1157 struct gotd_child_proc *proc;
1159 for (i = 0; i < gotd.nprocs; i++) {
1160 proc = &gotd.procs[i];
1161 msgbuf_clear(&proc->iev.ibuf.w);
1162 close(proc->iev.ibuf.fd);
1163 kill_proc(proc, 0);
1166 log_debug("waiting for children to terminate");
1167 do {
1168 pid = wait(&status);
1169 if (pid == -1) {
1170 if (errno != EINTR && errno != ECHILD)
1171 fatal("wait");
1172 } else if (WIFSIGNALED(status)) {
1173 proc = get_proc_for_pid(pid);
1174 log_warnx("%s %s child process terminated; signal %d",
1175 proc ? gotd_proc_names[proc->type] : "",
1176 proc ? proc->chroot_path : "", WTERMSIG(status));
1178 } while (pid != -1 || (pid == -1 && errno == EINTR));
1180 log_info("terminating");
1181 exit(0);
1184 void
1185 gotd_sighdlr(int sig, short event, void *arg)
1188 * Normal signal handler rules don't apply because libevent
1189 * decouples for us.
1192 switch (sig) {
1193 case SIGHUP:
1194 log_info("%s: ignoring SIGHUP", __func__);
1195 break;
1196 case SIGUSR1:
1197 log_info("%s: ignoring SIGUSR1", __func__);
1198 break;
1199 case SIGTERM:
1200 case SIGINT:
1201 gotd_shutdown();
1202 log_warnx("gotd terminating");
1203 exit(0);
1204 break;
1205 default:
1206 fatalx("unexpected signal");
1210 static const struct got_error *
1211 ensure_proc_is_reading(struct gotd_client *client,
1212 struct gotd_child_proc *proc)
1214 if (!client_is_reading(client)) {
1215 kill_proc(proc, 1);
1216 return got_error_fmt(GOT_ERR_BAD_PACKET,
1217 "PID %d handled a read-request for uid %d but this "
1218 "user is not reading from a repository", proc->pid,
1219 client->euid);
1222 return NULL;
1225 static const struct got_error *
1226 ensure_proc_is_writing(struct gotd_client *client,
1227 struct gotd_child_proc *proc)
1229 if (!client_is_writing(client)) {
1230 kill_proc(proc, 1);
1231 return got_error_fmt(GOT_ERR_BAD_PACKET,
1232 "PID %d handled a write-request for uid %d but this "
1233 "user is not writing to a repository", proc->pid,
1234 client->euid);
1237 return NULL;
1240 static int
1241 verify_imsg_src(struct gotd_client *client, struct gotd_child_proc *proc,
1242 struct imsg *imsg)
1244 const struct got_error *err;
1245 struct gotd_child_proc *client_proc;
1246 int ret = 0;
1248 client_proc = get_client_proc(client);
1249 if (proc->pid != client_proc->pid) {
1250 kill_proc(proc, 1);
1251 log_warnx("received message from PID %d for uid %d, while "
1252 "PID %d is the process serving this user",
1253 proc->pid, client->euid, client_proc->pid);
1254 return 0;
1257 switch (imsg->hdr.type) {
1258 case GOTD_IMSG_ERROR:
1259 ret = 1;
1260 break;
1261 case GOTD_IMSG_PACKFILE_DONE:
1262 err = ensure_proc_is_reading(client, proc);
1263 if (err)
1264 log_warnx("uid %d: %s", client->euid, err->msg);
1265 else
1266 ret = 1;
1267 break;
1268 case GOTD_IMSG_PACKFILE_INSTALL:
1269 case GOTD_IMSG_REF_UPDATES_START:
1270 case GOTD_IMSG_REF_UPDATE:
1271 err = ensure_proc_is_writing(client, proc);
1272 if (err)
1273 log_warnx("uid %d: %s", client->euid, err->msg);
1274 else
1275 ret = 1;
1276 break;
1277 default:
1278 log_debug("%s: unexpected imsg %d", __func__, imsg->hdr.type);
1279 break;
1282 return ret;
1285 static const struct got_error *
1286 recv_packfile_done(uint32_t *client_id, struct imsg *imsg)
1288 struct gotd_imsg_packfile_done idone;
1289 size_t datalen;
1291 log_debug("packfile-done received");
1293 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1294 if (datalen != sizeof(idone))
1295 return got_error(GOT_ERR_PRIVSEP_LEN);
1296 memcpy(&idone, imsg->data, sizeof(idone));
1298 *client_id = idone.client_id;
1299 return NULL;
1302 static const struct got_error *
1303 recv_packfile_install(uint32_t *client_id, struct imsg *imsg)
1305 struct gotd_imsg_packfile_install inst;
1306 size_t datalen;
1308 log_debug("packfile-install received");
1310 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1311 if (datalen != sizeof(inst))
1312 return got_error(GOT_ERR_PRIVSEP_LEN);
1313 memcpy(&inst, imsg->data, sizeof(inst));
1315 *client_id = inst.client_id;
1316 return NULL;
1319 static const struct got_error *
1320 recv_ref_updates_start(uint32_t *client_id, struct imsg *imsg)
1322 struct gotd_imsg_ref_updates_start istart;
1323 size_t datalen;
1325 log_debug("ref-updates-start received");
1327 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1328 if (datalen != sizeof(istart))
1329 return got_error(GOT_ERR_PRIVSEP_LEN);
1330 memcpy(&istart, imsg->data, sizeof(istart));
1332 *client_id = istart.client_id;
1333 return NULL;
1336 static const struct got_error *
1337 recv_ref_update(uint32_t *client_id, struct imsg *imsg)
1339 struct gotd_imsg_ref_update iref;
1340 size_t datalen;
1342 log_debug("ref-update received");
1344 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1345 if (datalen < sizeof(iref))
1346 return got_error(GOT_ERR_PRIVSEP_LEN);
1347 memcpy(&iref, imsg->data, sizeof(iref));
1349 *client_id = iref.client_id;
1350 return NULL;
1353 static const struct got_error *
1354 send_ref_update_ok(struct gotd_client *client,
1355 struct gotd_imsg_ref_update *iref, const char *refname)
1357 struct gotd_imsg_ref_update_ok iok;
1358 struct ibuf *wbuf;
1359 size_t len;
1361 memset(&iok, 0, sizeof(iok));
1362 iok.client_id = client->id;
1363 memcpy(iok.old_id, iref->old_id, SHA1_DIGEST_LENGTH);
1364 memcpy(iok.new_id, iref->new_id, SHA1_DIGEST_LENGTH);
1365 iok.name_len = strlen(refname);
1367 len = sizeof(iok) + iok.name_len;
1368 wbuf = imsg_create(&client->iev.ibuf, GOTD_IMSG_REF_UPDATE_OK,
1369 PROC_GOTD, gotd.pid, len);
1370 if (wbuf == NULL)
1371 return got_error_from_errno("imsg_create REF_UPDATE_OK");
1373 if (imsg_add(wbuf, &iok, sizeof(iok)) == -1)
1374 return got_error_from_errno("imsg_add REF_UPDATE_OK");
1375 if (imsg_add(wbuf, refname, iok.name_len) == -1)
1376 return got_error_from_errno("imsg_add REF_UPDATE_OK");
1378 wbuf->fd = -1;
1379 imsg_close(&client->iev.ibuf, wbuf);
1380 gotd_imsg_event_add(&client->iev);
1381 return NULL;
1384 static void
1385 send_refs_updated(struct gotd_client *client)
1387 if (gotd_imsg_compose_event(&client->iev,
1388 GOTD_IMSG_REFS_UPDATED, PROC_GOTD, -1, NULL, 0) == -1)
1389 log_warn("imsg compose REFS_UPDATED");
1392 static const struct got_error *
1393 send_ref_update_ng(struct gotd_client *client,
1394 struct gotd_imsg_ref_update *iref, const char *refname,
1395 const char *reason)
1397 const struct got_error *ng_err;
1398 struct gotd_imsg_ref_update_ng ing;
1399 struct ibuf *wbuf;
1400 size_t len;
1402 memset(&ing, 0, sizeof(ing));
1403 ing.client_id = client->id;
1404 memcpy(ing.old_id, iref->old_id, SHA1_DIGEST_LENGTH);
1405 memcpy(ing.new_id, iref->new_id, SHA1_DIGEST_LENGTH);
1406 ing.name_len = strlen(refname);
1408 ng_err = got_error_fmt(GOT_ERR_REF_BUSY, "%s", reason);
1409 ing.reason_len = strlen(ng_err->msg);
1411 len = sizeof(ing) + ing.name_len + ing.reason_len;
1412 wbuf = imsg_create(&client->iev.ibuf, GOTD_IMSG_REF_UPDATE_NG,
1413 PROC_GOTD, gotd.pid, len);
1414 if (wbuf == NULL)
1415 return got_error_from_errno("imsg_create REF_UPDATE_NG");
1417 if (imsg_add(wbuf, &ing, sizeof(ing)) == -1)
1418 return got_error_from_errno("imsg_add REF_UPDATE_NG");
1419 if (imsg_add(wbuf, refname, ing.name_len) == -1)
1420 return got_error_from_errno("imsg_add REF_UPDATE_NG");
1421 if (imsg_add(wbuf, ng_err->msg, ing.reason_len) == -1)
1422 return got_error_from_errno("imsg_add REF_UPDATE_NG");
1424 wbuf->fd = -1;
1425 imsg_close(&client->iev.ibuf, wbuf);
1426 gotd_imsg_event_add(&client->iev);
1427 return NULL;
1430 static const struct got_error *
1431 install_pack(struct gotd_client *client, const char *repo_path,
1432 struct imsg *imsg)
1434 const struct got_error *err = NULL;
1435 struct gotd_imsg_packfile_install inst;
1436 char hex[SHA1_DIGEST_STRING_LENGTH];
1437 size_t datalen;
1438 char *packfile_path = NULL, *packidx_path = NULL;
1440 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1441 if (datalen != sizeof(inst))
1442 return got_error(GOT_ERR_PRIVSEP_LEN);
1443 memcpy(&inst, imsg->data, sizeof(inst));
1445 if (client->packfile_path == NULL)
1446 return got_error_msg(GOT_ERR_BAD_REQUEST,
1447 "client has no pack file");
1448 if (client->packidx_path == NULL)
1449 return got_error_msg(GOT_ERR_BAD_REQUEST,
1450 "client has no pack file index");
1452 if (got_sha1_digest_to_str(inst.pack_sha1, hex, sizeof(hex)) == NULL)
1453 return got_error_msg(GOT_ERR_NO_SPACE,
1454 "could not convert pack file SHA1 to hex");
1456 if (asprintf(&packfile_path, "/%s/%s/pack-%s.pack",
1457 repo_path, GOT_OBJECTS_PACK_DIR, hex) == -1) {
1458 err = got_error_from_errno("asprintf");
1459 goto done;
1462 if (asprintf(&packidx_path, "/%s/%s/pack-%s.idx",
1463 repo_path, GOT_OBJECTS_PACK_DIR, hex) == -1) {
1464 err = got_error_from_errno("asprintf");
1465 goto done;
1468 if (rename(client->packfile_path, packfile_path) == -1) {
1469 err = got_error_from_errno3("rename", client->packfile_path,
1470 packfile_path);
1471 goto done;
1474 free(client->packfile_path);
1475 client->packfile_path = NULL;
1477 if (rename(client->packidx_path, packidx_path) == -1) {
1478 err = got_error_from_errno3("rename", client->packidx_path,
1479 packidx_path);
1480 goto done;
1483 free(client->packidx_path);
1484 client->packidx_path = NULL;
1485 done:
1486 free(packfile_path);
1487 free(packidx_path);
1488 return err;
1491 static const struct got_error *
1492 begin_ref_updates(struct gotd_client *client, struct imsg *imsg)
1494 struct gotd_imsg_ref_updates_start istart;
1495 size_t datalen;
1497 if (client->nref_updates != -1)
1498 return got_error(GOT_ERR_PRIVSEP_MSG);
1500 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1501 if (datalen != sizeof(istart))
1502 return got_error(GOT_ERR_PRIVSEP_LEN);
1503 memcpy(&istart, imsg->data, sizeof(istart));
1505 if (istart.nref_updates <= 0)
1506 return got_error(GOT_ERR_PRIVSEP_MSG);
1508 client->nref_updates = istart.nref_updates;
1509 return NULL;
1512 static const struct got_error *
1513 update_ref(struct gotd_client *client, const char *repo_path,
1514 struct imsg *imsg)
1516 const struct got_error *err = NULL;
1517 struct got_repository *repo = NULL;
1518 struct got_reference *ref = NULL;
1519 struct gotd_imsg_ref_update iref;
1520 struct got_object_id old_id, new_id;
1521 struct got_object_id *id = NULL;
1522 struct got_object *obj = NULL;
1523 char *refname = NULL;
1524 size_t datalen;
1525 int locked = 0;
1527 log_debug("update-ref from uid %d", client->euid);
1529 if (client->nref_updates <= 0)
1530 return got_error(GOT_ERR_PRIVSEP_MSG);
1532 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1533 if (datalen < sizeof(iref))
1534 return got_error(GOT_ERR_PRIVSEP_LEN);
1535 memcpy(&iref, imsg->data, sizeof(iref));
1536 if (datalen != sizeof(iref) + iref.name_len)
1537 return got_error(GOT_ERR_PRIVSEP_LEN);
1538 refname = malloc(iref.name_len + 1);
1539 if (refname == NULL)
1540 return got_error_from_errno("malloc");
1541 memcpy(refname, imsg->data + sizeof(iref), iref.name_len);
1542 refname[iref.name_len] = '\0';
1544 log_debug("updating ref %s for uid %d", refname, client->euid);
1546 err = got_repo_open(&repo, repo_path, NULL, NULL);
1547 if (err)
1548 goto done;
1550 memcpy(old_id.sha1, iref.old_id, SHA1_DIGEST_LENGTH);
1551 memcpy(new_id.sha1, iref.new_id, SHA1_DIGEST_LENGTH);
1552 err = got_object_open(&obj, repo, &new_id);
1553 if (err)
1554 goto done;
1556 if (iref.ref_is_new) {
1557 err = got_ref_open(&ref, repo, refname, 0);
1558 if (err) {
1559 if (err->code != GOT_ERR_NOT_REF)
1560 goto done;
1561 err = got_ref_alloc(&ref, refname, &new_id);
1562 if (err)
1563 goto done;
1564 err = got_ref_write(ref, repo); /* will lock/unlock */
1565 if (err)
1566 goto done;
1567 } else {
1568 err = got_error_fmt(GOT_ERR_REF_BUSY,
1569 "%s has been created by someone else "
1570 "while transaction was in progress",
1571 got_ref_get_name(ref));
1572 goto done;
1574 } else {
1575 err = got_ref_open(&ref, repo, refname, 1 /* lock */);
1576 if (err)
1577 goto done;
1578 locked = 1;
1580 err = got_ref_resolve(&id, repo, ref);
1581 if (err)
1582 goto done;
1584 if (got_object_id_cmp(id, &old_id) != 0) {
1585 err = got_error_fmt(GOT_ERR_REF_BUSY,
1586 "%s has been modified by someone else "
1587 "while transaction was in progress",
1588 got_ref_get_name(ref));
1589 goto done;
1592 err = got_ref_change_ref(ref, &new_id);
1593 if (err)
1594 goto done;
1596 err = got_ref_write(ref, repo);
1597 if (err)
1598 goto done;
1600 free(id);
1601 id = NULL;
1603 done:
1604 if (err) {
1605 if (err->code == GOT_ERR_LOCKFILE_TIMEOUT) {
1606 err = got_error_fmt(GOT_ERR_LOCKFILE_TIMEOUT,
1607 "could not acquire exclusive file lock for %s",
1608 refname);
1610 send_ref_update_ng(client, &iref, refname, err->msg);
1611 } else
1612 send_ref_update_ok(client, &iref, refname);
1614 if (client->nref_updates > 0) {
1615 client->nref_updates--;
1616 if (client->nref_updates == 0)
1617 send_refs_updated(client);
1620 if (locked) {
1621 const struct got_error *unlock_err;
1622 unlock_err = got_ref_unlock(ref);
1623 if (unlock_err && err == NULL)
1624 err = unlock_err;
1626 if (ref)
1627 got_ref_close(ref);
1628 if (obj)
1629 got_object_close(obj);
1630 if (repo)
1631 got_repo_close(repo);
1632 free(refname);
1633 free(id);
1634 return err;
1637 static void
1638 gotd_dispatch(int fd, short event, void *arg)
1640 struct gotd_imsgev *iev = arg;
1641 struct imsgbuf *ibuf = &iev->ibuf;
1642 struct gotd_child_proc *proc = NULL;
1643 ssize_t n;
1644 int shut = 0;
1645 struct imsg imsg;
1647 if (event & EV_READ) {
1648 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1649 fatal("imsg_read error");
1650 if (n == 0) {
1651 /* Connection closed. */
1652 shut = 1;
1653 goto done;
1657 if (event & EV_WRITE) {
1658 n = msgbuf_write(&ibuf->w);
1659 if (n == -1 && errno != EAGAIN)
1660 fatal("msgbuf_write");
1661 if (n == 0) {
1662 /* Connection closed. */
1663 shut = 1;
1664 goto done;
1668 proc = find_proc_by_fd(fd);
1669 if (proc == NULL)
1670 fatalx("cannot find child process for fd %d", fd);
1672 for (;;) {
1673 const struct got_error *err = NULL;
1674 struct gotd_client *client = NULL;
1675 uint32_t client_id = 0;
1676 int do_disconnect = 0;
1677 int do_ref_updates = 0, do_ref_update = 0;
1678 int do_packfile_install = 0;
1680 if ((n = imsg_get(ibuf, &imsg)) == -1)
1681 fatal("%s: imsg_get error", __func__);
1682 if (n == 0) /* No more messages. */
1683 break;
1685 switch (imsg.hdr.type) {
1686 case GOTD_IMSG_ERROR:
1687 do_disconnect = 1;
1688 err = gotd_imsg_recv_error(&client_id, &imsg);
1689 break;
1690 case GOTD_IMSG_PACKFILE_DONE:
1691 do_disconnect = 1;
1692 err = recv_packfile_done(&client_id, &imsg);
1693 break;
1694 case GOTD_IMSG_PACKFILE_INSTALL:
1695 err = recv_packfile_install(&client_id, &imsg);
1696 if (err == NULL)
1697 do_packfile_install = 1;
1698 break;
1699 case GOTD_IMSG_REF_UPDATES_START:
1700 err = recv_ref_updates_start(&client_id, &imsg);
1701 if (err == NULL)
1702 do_ref_updates = 1;
1703 break;
1704 case GOTD_IMSG_REF_UPDATE:
1705 err = recv_ref_update(&client_id, &imsg);
1706 if (err == NULL)
1707 do_ref_update = 1;
1708 break;
1709 default:
1710 log_debug("unexpected imsg %d", imsg.hdr.type);
1711 break;
1714 client = find_client(client_id);
1715 if (client == NULL) {
1716 log_warnx("%s: client not found", __func__);
1717 imsg_free(&imsg);
1718 continue;
1721 if (!verify_imsg_src(client, proc, &imsg)) {
1722 log_debug("dropping imsg type %d from PID %d",
1723 imsg.hdr.type, proc->pid);
1724 imsg_free(&imsg);
1725 continue;
1727 if (err)
1728 log_warnx("uid %d: %s", client->euid, err->msg);
1730 if (do_disconnect) {
1731 if (err)
1732 disconnect_on_error(client, err);
1733 else
1734 disconnect(client);
1735 } else if (do_packfile_install)
1736 err = install_pack(client, proc->chroot_path, &imsg);
1737 else if (do_ref_updates)
1738 err = begin_ref_updates(client, &imsg);
1739 else if (do_ref_update)
1740 err = update_ref(client, proc->chroot_path, &imsg);
1742 if (err)
1743 log_warnx("uid %d: %s", client->euid, err->msg);
1744 imsg_free(&imsg);
1746 done:
1747 if (!shut) {
1748 gotd_imsg_event_add(iev);
1749 } else {
1750 /* This pipe is dead. Remove its event handler */
1751 event_del(&iev->ev);
1752 event_loopexit(NULL);
1756 static pid_t
1757 start_child(enum gotd_procid proc_id, const char *chroot_path,
1758 char *argv0, int fd, int daemonize, int verbosity)
1760 char *argv[9];
1761 int argc = 0;
1762 pid_t pid;
1764 switch (pid = fork()) {
1765 case -1:
1766 fatal("cannot fork");
1767 case 0:
1768 break;
1769 default:
1770 close(fd);
1771 return pid;
1774 if (fd != GOTD_SOCK_FILENO) {
1775 if (dup2(fd, GOTD_SOCK_FILENO) == -1)
1776 fatal("cannot setup imsg fd");
1777 } else if (fcntl(fd, F_SETFD, 0) == -1)
1778 fatal("cannot setup imsg fd");
1780 argv[argc++] = argv0;
1781 switch (proc_id) {
1782 case PROC_REPO_READ:
1783 argv[argc++] = (char *)"-R";
1784 break;
1785 case PROC_REPO_WRITE:
1786 argv[argc++] = (char *)"-W";
1787 break;
1788 default:
1789 fatalx("invalid process id %d", proc_id);
1792 argv[argc++] = (char *)"-P";
1793 argv[argc++] = (char *)chroot_path;
1795 if (!daemonize)
1796 argv[argc++] = (char *)"-d";
1797 if (verbosity > 0)
1798 argv[argc++] = (char *)"-v";
1799 if (verbosity > 1)
1800 argv[argc++] = (char *)"-v";
1801 argv[argc++] = NULL;
1803 execvp(argv0, argv);
1804 fatal("execvp");
1807 static void
1808 start_repo_children(struct gotd *gotd, char *argv0, int daemonize,
1809 int verbosity)
1811 struct gotd_repo *repo = NULL;
1812 struct gotd_child_proc *proc;
1813 int i;
1816 * XXX For now, use one reader and one writer per repository.
1817 * This should be changed to N readers + M writers.
1819 gotd->nprocs = gotd->nrepos * 2;
1820 gotd->procs = calloc(gotd->nprocs, sizeof(*gotd->procs));
1821 if (gotd->procs == NULL)
1822 fatal("calloc");
1823 for (i = 0; i < gotd->nprocs; i++) {
1824 if (repo == NULL)
1825 repo = TAILQ_FIRST(&gotd->repos);
1826 proc = &gotd->procs[i];
1827 if (i < gotd->nrepos)
1828 proc->type = PROC_REPO_READ;
1829 else
1830 proc->type = PROC_REPO_WRITE;
1831 if (strlcpy(proc->repo_name, repo->name,
1832 sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
1833 fatalx("repository name too long: %s", repo->name);
1834 log_debug("adding repository %s", repo->name);
1835 if (realpath(repo->path, proc->chroot_path) == NULL)
1836 fatal("%s", repo->path);
1837 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1838 PF_UNSPEC, proc->pipe) == -1)
1839 fatal("socketpair");
1840 proc->pid = start_child(proc->type, proc->chroot_path, argv0,
1841 proc->pipe[1], daemonize, verbosity);
1842 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1843 log_debug("proc %s %s is on fd %d",
1844 gotd_proc_names[proc->type], proc->chroot_path,
1845 proc->pipe[0]);
1846 proc->iev.handler = gotd_dispatch;
1847 proc->iev.events = EV_READ;
1848 proc->iev.handler_arg = NULL;
1849 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1850 gotd_dispatch, &proc->iev);
1852 repo = TAILQ_NEXT(repo, entry);
1856 static void
1857 apply_unveil(void)
1859 struct gotd_repo *repo;
1861 TAILQ_FOREACH(repo, &gotd.repos, entry) {
1862 if (unveil(repo->path, "rwc") == -1)
1863 fatal("unveil %s", repo->path);
1866 if (unveil(GOT_TMPDIR_STR, "rwc") == -1)
1867 fatal("unveil %s", GOT_TMPDIR_STR);
1869 if (unveil(NULL, NULL) == -1)
1870 fatal("unveil");
1873 int
1874 main(int argc, char **argv)
1876 const struct got_error *error = NULL;
1877 int ch, fd = -1, daemonize = 1, verbosity = 0, noaction = 0;
1878 const char *confpath = GOTD_CONF_PATH;
1879 char *argv0 = argv[0];
1880 char title[2048];
1881 gid_t groups[NGROUPS_MAX + 1];
1882 int ngroups = NGROUPS_MAX + 1;
1883 struct passwd *pw = NULL;
1884 struct group *gr = NULL;
1885 char *repo_path = NULL;
1886 enum gotd_procid proc_id = PROC_GOTD;
1887 struct event evsigint, evsigterm, evsighup, evsigusr1;
1888 int *pack_fds = NULL, *temp_fds = NULL;
1890 log_init(1, LOG_DAEMON); /* Log to stderr until daemonized. */
1892 while ((ch = getopt(argc, argv, "df:nP:RvW")) != -1) {
1893 switch (ch) {
1894 case 'd':
1895 daemonize = 0;
1896 break;
1897 case 'f':
1898 confpath = optarg;
1899 break;
1900 case 'n':
1901 noaction = 1;
1902 break;
1903 case 'P':
1904 repo_path = realpath(optarg, NULL);
1905 if (repo_path == NULL)
1906 fatal("realpath '%s'", optarg);
1907 break;
1908 case 'R':
1909 proc_id = PROC_REPO_READ;
1910 break;
1911 case 'v':
1912 if (verbosity < 3)
1913 verbosity++;
1914 break;
1915 case 'W':
1916 proc_id = PROC_REPO_WRITE;
1917 break;
1918 default:
1919 usage();
1923 argc -= optind;
1924 argv += optind;
1926 if (argc != 0)
1927 usage();
1929 if (geteuid())
1930 fatalx("need root privileges");
1932 log_init(daemonize ? 0 : 1, LOG_DAEMON);
1933 log_setverbose(verbosity);
1935 if (parse_config(confpath, proc_id, &gotd) != 0)
1936 return 1;
1938 if (proc_id == PROC_GOTD &&
1939 (gotd.nrepos == 0 || TAILQ_EMPTY(&gotd.repos)))
1940 fatalx("no repository defined in configuration file");
1942 pw = getpwnam(gotd.user_name);
1943 if (pw == NULL)
1944 fatal("getpwuid: user %s not found", gotd.user_name);
1946 if (pw->pw_uid == 0) {
1947 fatalx("cannot run %s as %s: the user running %s "
1948 "must not be the superuser",
1949 getprogname(), pw->pw_name, getprogname());
1952 if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups) == -1)
1953 log_warnx("group membership list truncated");
1955 gr = match_group(groups, ngroups, gotd.unix_group_name);
1956 if (gr == NULL) {
1957 fatalx("cannot start %s: the user running %s "
1958 "must be a secondary member of group %s",
1959 getprogname(), getprogname(), gotd.unix_group_name);
1961 if (gr->gr_gid == pw->pw_gid) {
1962 fatalx("cannot start %s: the user running %s "
1963 "must be a secondary member of group %s, but "
1964 "%s is the user's primary group",
1965 getprogname(), getprogname(), gotd.unix_group_name,
1966 gotd.unix_group_name);
1969 if (proc_id == PROC_GOTD &&
1970 !got_path_is_absolute(gotd.unix_socket_path))
1971 fatalx("bad unix socket path \"%s\": must be an absolute path",
1972 gotd.unix_socket_path);
1974 if (noaction)
1975 return 0;
1977 if (proc_id == PROC_GOTD && verbosity) {
1978 log_info("socket: %s", gotd.unix_socket_path);
1979 log_info("user: %s", pw->pw_name);
1980 log_info("secondary group: %s", gr->gr_name);
1982 fd = unix_socket_listen(gotd.unix_socket_path, pw->pw_uid,
1983 gr->gr_gid);
1984 if (fd == -1) {
1985 fatal("cannot listen on unix socket %s",
1986 gotd.unix_socket_path);
1990 if (proc_id == PROC_GOTD) {
1991 gotd.pid = getpid();
1992 snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
1993 start_repo_children(&gotd, argv0, daemonize, verbosity);
1994 arc4random_buf(&clients_hash_key, sizeof(clients_hash_key));
1995 if (daemonize && daemon(0, 0) == -1)
1996 fatal("daemon");
1997 } else if (proc_id == PROC_REPO_READ || proc_id == PROC_REPO_WRITE) {
1998 error = got_repo_pack_fds_open(&pack_fds);
1999 if (error != NULL)
2000 fatalx("cannot open pack tempfiles: %s", error->msg);
2001 error = got_repo_temp_fds_open(&temp_fds);
2002 if (error != NULL)
2003 fatalx("cannot open pack tempfiles: %s", error->msg);
2004 if (repo_path == NULL)
2005 fatalx("repository path not specified");
2006 snprintf(title, sizeof(title), "%s %s",
2007 gotd_proc_names[proc_id], repo_path);
2008 if (chroot(repo_path) == -1)
2009 fatal("chroot");
2010 if (chdir("/") == -1)
2011 fatal("chdir(\"/\")");
2012 if (daemonize && daemon(1, 0) == -1)
2013 fatal("daemon");
2014 } else
2015 fatal("invalid process id %d", proc_id);
2017 setproctitle("%s", title);
2018 log_procinit(title);
2020 /* Drop root privileges. */
2021 if (setgid(pw->pw_gid) == -1)
2022 fatal("setgid %d failed", pw->pw_gid);
2023 if (setuid(pw->pw_uid) == -1)
2024 fatal("setuid %d failed", pw->pw_uid);
2026 event_init();
2028 switch (proc_id) {
2029 case PROC_GOTD:
2030 #ifndef PROFILE
2031 if (pledge("stdio rpath wpath cpath proc getpw sendfd recvfd "
2032 "fattr flock unix unveil", NULL) == -1)
2033 err(1, "pledge");
2034 #endif
2035 break;
2036 case PROC_REPO_READ:
2037 #ifndef PROFILE
2038 if (pledge("stdio rpath recvfd", NULL) == -1)
2039 err(1, "pledge");
2040 #endif
2041 repo_read_main(title, pack_fds, temp_fds);
2042 /* NOTREACHED */
2043 exit(0);
2044 case PROC_REPO_WRITE:
2045 #ifndef PROFILE
2046 if (pledge("stdio rpath recvfd", NULL) == -1)
2047 err(1, "pledge");
2048 #endif
2049 repo_write_main(title, pack_fds, temp_fds);
2050 /* NOTREACHED */
2051 exit(0);
2052 default:
2053 fatal("invalid process id %d", proc_id);
2056 if (proc_id != PROC_GOTD)
2057 fatal("invalid process id %d", proc_id);
2059 apply_unveil();
2061 signal_set(&evsigint, SIGINT, gotd_sighdlr, NULL);
2062 signal_set(&evsigterm, SIGTERM, gotd_sighdlr, NULL);
2063 signal_set(&evsighup, SIGHUP, gotd_sighdlr, NULL);
2064 signal_set(&evsigusr1, SIGUSR1, gotd_sighdlr, NULL);
2065 signal(SIGPIPE, SIG_IGN);
2067 signal_add(&evsigint, NULL);
2068 signal_add(&evsigterm, NULL);
2069 signal_add(&evsighup, NULL);
2070 signal_add(&evsigusr1, NULL);
2072 event_set(&gotd.ev, fd, EV_READ | EV_PERSIST, gotd_accept, NULL);
2073 if (event_add(&gotd.ev, NULL))
2074 fatalx("event add");
2075 evtimer_set(&gotd.pause, gotd_accept_paused, NULL);
2077 event_dispatch();
2079 if (fd != -1)
2080 close(fd);
2081 if (pack_fds)
2082 got_repo_pack_fds_close(pack_fds);
2083 free(repo_path);
2084 return 0;