2 * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
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.
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.
17 #include <sys/queue.h>
20 #include <sys/types.h>
22 #include <sys/socket.h>
44 #include "got_error.h"
45 #include "got_opentemp.h"
47 #include "got_repository.h"
48 #include "got_object.h"
49 #include "got_reference.h"
51 #include "got_lib_delta.h"
52 #include "got_lib_object.h"
53 #include "got_lib_object_cache.h"
54 #include "got_lib_sha1.h"
55 #include "got_lib_gitproto.h"
56 #include "got_lib_pack.h"
57 #include "got_lib_repository.h"
62 #include "repo_read.h"
63 #include "repo_write.h"
66 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
70 STAILQ_ENTRY(gotd_client) entry;
71 enum gotd_client_state state;
72 struct gotd_client_capability *capabilities;
78 struct gotd_imsgev iev;
82 struct gotd_child_proc *repo_read;
83 struct gotd_child_proc *repo_write;
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 };
95 static struct gotd gotd;
97 void gotd_sighdlr(int sig, short event, void *arg);
98 static void gotd_shutdown(void);
103 fprintf(stderr, "usage: %s [-dv] [-f config-file]\n", getprogname());
108 unix_socket_listen(const char *unix_socket_path, uid_t uid, gid_t gid)
110 struct sockaddr_un sun;
112 mode_t old_umask, mode;
114 fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK| SOCK_CLOEXEC, 0);
120 sun.sun_family = AF_UNIX;
121 if (strlcpy(sun.sun_path, unix_socket_path,
122 sizeof(sun.sun_path)) >= sizeof(sun.sun_path)) {
123 log_warnx("%s: name too long", unix_socket_path);
128 if (unlink(unix_socket_path) == -1) {
129 if (errno != ENOENT) {
130 log_warn("unlink %s", unix_socket_path);
136 old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
137 mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP;
139 if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
140 log_warn("bind: %s", unix_socket_path);
148 if (chmod(unix_socket_path, mode) == -1) {
149 log_warn("chmod %o %s", mode, unix_socket_path);
151 unlink(unix_socket_path);
155 if (chown(unix_socket_path, uid, gid) == -1) {
156 log_warn("chown %s uid=%d gid=%d", unix_socket_path, uid, gid);
158 unlink(unix_socket_path);
162 if (listen(fd, GOTD_UNIX_SOCKET_BACKLOG) == -1) {
165 unlink(unix_socket_path);
172 static struct group *
173 match_group(gid_t *groups, int ngroups, const char *unix_group_name)
178 for (i = 0; i < ngroups; i++) {
179 gr = getgrgid(groups[i]);
181 log_warn("getgrgid %d", groups[i]);
184 if (strcmp(gr->gr_name, unix_group_name) == 0)
192 accept_reserve(int fd, struct sockaddr *addr, socklen_t *addrlen,
193 int reserve, volatile int *counter)
197 if (getdtablecount() + reserve +
198 ((*counter + 1) * GOTD_FD_NEEDED) >= getdtablesize()) {
199 log_debug("inflight fds exceeded");
204 if ((ret = accept4(fd, addr, addrlen,
205 SOCK_NONBLOCK | SOCK_CLOEXEC)) > -1) {
213 client_hash(uint32_t client_id)
215 return SipHash24(&clients_hash_key, &client_id, sizeof(client_id));
219 add_client(struct gotd_client *client)
221 uint64_t slot = client_hash(client->id) % nitems(gotd_clients);
222 STAILQ_INSERT_HEAD(&gotd_clients[slot], client, entry);
226 static struct gotd_client *
227 find_client(uint32_t client_id)
230 struct gotd_client *c;
232 slot = client_hash(client_id) % nitems(gotd_clients);
233 STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
234 if (c->id == client_id)
249 duplicate = (find_client(id) != NULL);
250 } while (duplicate || id == 0);
255 static struct gotd_child_proc *
256 get_client_proc(struct gotd_client *client)
258 if (client->repo_read && client->repo_write) {
259 fatalx("uid %d is reading and writing in the same session",
264 if (client->repo_read)
265 return client->repo_read;
266 else if (client->repo_write)
267 return client->repo_write;
273 client_is_reading(struct gotd_client *client)
275 return client->repo_read != NULL;
279 client_is_writing(struct gotd_client *client)
281 return client->repo_write != NULL;
284 static const struct got_error *
285 ensure_client_is_reading(struct gotd_client *client)
287 if (!client_is_reading(client)) {
288 return got_error_fmt(GOT_ERR_BAD_PACKET,
289 "uid %d made a read-request but is not reading from "
290 "a repository", client->euid);
296 static const struct got_error *
297 ensure_client_is_writing(struct gotd_client *client)
299 if (!client_is_writing(client)) {
300 return got_error_fmt(GOT_ERR_BAD_PACKET,
301 "uid %d made a write-request but is not writing to "
302 "a repository", client->euid);
308 static const struct got_error *
309 ensure_client_is_not_writing(struct gotd_client *client)
311 if (client_is_writing(client)) {
312 return got_error_fmt(GOT_ERR_BAD_PACKET,
313 "uid %d made a read-request but is writing to "
314 "a repository", client->euid);
320 static const struct got_error *
321 ensure_client_is_not_reading(struct gotd_client *client)
323 if (client_is_reading(client)) {
324 return got_error_fmt(GOT_ERR_BAD_PACKET,
325 "uid %d made a write-request but is reading from "
326 "a repository", client->euid);
333 disconnect(struct gotd_client *client)
335 struct gotd_imsg_disconnect idisconnect;
336 struct gotd_child_proc *proc = get_client_proc(client);
339 log_debug("uid %d: disconnecting", client->euid);
341 idisconnect.client_id = client->id;
343 if (gotd_imsg_compose_event(&proc->iev,
344 GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
345 &idisconnect, sizeof(idisconnect)) == -1)
346 log_warn("imsg compose DISCONNECT");
348 slot = client_hash(client->id) % nitems(gotd_clients);
349 STAILQ_REMOVE(&gotd_clients[slot], client, gotd_client, entry);
350 imsg_clear(&client->iev.ibuf);
351 event_del(&client->iev.ev);
352 evtimer_del(&client->tmo);
354 if (client->delta_cache_fd != -1)
355 close(client->delta_cache_fd);
356 if (client->packfile_path) {
357 if (unlink(client->packfile_path) == -1 && errno != ENOENT)
358 log_warn("unlink %s: ", client->packfile_path);
359 free(client->packfile_path);
361 if (client->packidx_path) {
362 if (unlink(client->packidx_path) == -1 && errno != ENOENT)
363 log_warn("unlink %s: ", client->packidx_path);
364 free(client->packidx_path);
366 free(client->capabilities);
373 disconnect_on_error(struct gotd_client *client, const struct got_error *err)
377 log_warnx("uid %d: %s", client->euid, err->msg);
378 if (err->code != GOT_ERR_EOF) {
379 imsg_init(&ibuf, client->fd);
380 gotd_imsg_send_error(&ibuf, 0, PROC_GOTD, err);
386 static const struct got_error *
387 send_repo_info(struct gotd_imsgev *iev, struct gotd_repo *repo)
389 const struct got_error *err = NULL;
390 struct gotd_imsg_info_repo irepo;
392 memset(&irepo, 0, sizeof(irepo));
394 if (strlcpy(irepo.repo_name, repo->name, sizeof(irepo.repo_name))
395 >= sizeof(irepo.repo_name))
396 return got_error_msg(GOT_ERR_NO_SPACE, "repo name too long");
397 if (strlcpy(irepo.repo_path, repo->path, sizeof(irepo.repo_path))
398 >= sizeof(irepo.repo_path))
399 return got_error_msg(GOT_ERR_NO_SPACE, "repo path too long");
401 if (gotd_imsg_compose_event(iev, GOTD_IMSG_INFO_REPO, PROC_GOTD, -1,
402 &irepo, sizeof(irepo)) == -1) {
403 err = got_error_from_errno("imsg compose INFO_REPO");
411 static const struct got_error *
412 send_capability(struct gotd_client_capability *capa, struct gotd_imsgev* iev)
414 const struct got_error *err = NULL;
415 struct gotd_imsg_capability icapa;
419 memset(&icapa, 0, sizeof(icapa));
421 icapa.key_len = strlen(capa->key);
422 len = sizeof(icapa) + icapa.key_len;
424 icapa.value_len = strlen(capa->value);
425 len += icapa.value_len;
428 wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_CAPABILITY, 0, 0, len);
430 err = got_error_from_errno("imsg_create CAPABILITY");
434 if (imsg_add(wbuf, &icapa, sizeof(icapa)) == -1)
435 return got_error_from_errno("imsg_add CAPABILITY");
436 if (imsg_add(wbuf, capa->key, icapa.key_len) == -1)
437 return got_error_from_errno("imsg_add CAPABILITY");
439 if (imsg_add(wbuf, capa->value, icapa.value_len) == -1)
440 return got_error_from_errno("imsg_add CAPABILITY");
444 imsg_close(&iev->ibuf, wbuf);
446 gotd_imsg_event_add(iev);
451 static const struct got_error *
452 send_client_info(struct gotd_imsgev *iev, struct gotd_client *client)
454 const struct got_error *err = NULL;
455 struct gotd_imsg_info_client iclient;
456 struct gotd_child_proc *proc;
459 memset(&iclient, 0, sizeof(iclient));
460 iclient.euid = client->euid;
461 iclient.egid = client->egid;
463 proc = get_client_proc(client);
465 if (strlcpy(iclient.repo_name, proc->chroot_path,
466 sizeof(iclient.repo_name)) >= sizeof(iclient.repo_name)) {
467 return got_error_msg(GOT_ERR_NO_SPACE,
468 "repo name too long");
470 if (client_is_writing(client))
471 iclient.is_writing = 1;
474 iclient.state = client->state;
475 iclient.ncapabilities = client->ncapabilities;
477 if (gotd_imsg_compose_event(iev, GOTD_IMSG_INFO_CLIENT, PROC_GOTD, -1,
478 &iclient, sizeof(iclient)) == -1) {
479 err = got_error_from_errno("imsg compose INFO_CLIENT");
484 for (i = 0; i < client->ncapabilities; i++) {
485 struct gotd_client_capability *capa;
486 capa = &client->capabilities[i];
487 err = send_capability(capa, iev);
495 static const struct got_error *
496 send_info(struct gotd_client *client)
498 const struct got_error *err = NULL;
499 struct gotd_imsg_info info;
501 struct gotd_repo *repo;
504 info.verbosity = gotd.verbosity;
505 info.nrepos = gotd.nrepos;
506 info.nclients = client_cnt - 1;
508 if (gotd_imsg_compose_event(&client->iev, GOTD_IMSG_INFO, PROC_GOTD, -1,
509 &info, sizeof(info)) == -1) {
510 err = got_error_from_errno("imsg compose INFO");
515 TAILQ_FOREACH(repo, &gotd.repos, entry) {
516 err = send_repo_info(&client->iev, repo);
521 for (slot = 0; slot < nitems(gotd_clients); slot++) {
522 struct gotd_client *c;
523 STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
524 if (c->id == client->id)
526 err = send_client_info(&client->iev, c);
535 static const struct got_error *
536 stop_gotd(struct gotd_client *client)
539 if (client->euid != 0)
540 return got_error_set_errno(EPERM, "stop");
547 static struct gotd_repo *
548 find_repo_by_name(const char *repo_name)
550 struct gotd_repo *repo;
553 TAILQ_FOREACH(repo, &gotd.repos, entry) {
554 namelen = strlen(repo->name);
555 if (strncmp(repo->name, repo_name, namelen) != 0)
557 if (repo_name[namelen] == '\0' ||
558 strcmp(&repo_name[namelen], ".git") == 0)
565 static struct gotd_child_proc *
566 find_proc_by_repo_name(enum gotd_procid proc_id, const char *repo_name)
568 struct gotd_child_proc *proc;
572 for (i = 0; i < gotd.nprocs; i++) {
573 proc = &gotd.procs[i];
574 if (proc->type != proc_id)
576 namelen = strlen(proc->repo_name);
577 if (strncmp(proc->repo_name, repo_name, namelen) != 0)
579 if (repo_name[namelen] == '\0' ||
580 strcmp(&repo_name[namelen], ".git") == 0)
587 static struct gotd_child_proc *
588 find_proc_by_fd(int fd)
590 struct gotd_child_proc *proc;
593 for (i = 0; i < gotd.nprocs; i++) {
594 proc = &gotd.procs[i];
595 if (proc->iev.ibuf.fd == fd)
602 static const struct got_error *
603 forward_list_refs_request(struct gotd_client *client, struct imsg *imsg)
605 const struct got_error *err;
606 struct gotd_imsg_list_refs ireq;
607 struct gotd_imsg_list_refs_internal ilref;
608 struct gotd_repo *repo = NULL;
609 struct gotd_child_proc *proc = NULL;
613 log_debug("list-refs request from uid %d", client->euid);
615 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
616 if (datalen != sizeof(ireq))
617 return got_error(GOT_ERR_PRIVSEP_LEN);
619 memcpy(&ireq, imsg->data, datalen);
621 memset(&ilref, 0, sizeof(ilref));
622 ilref.client_id = client->id;
624 if (ireq.client_is_reading) {
625 err = ensure_client_is_not_writing(client);
628 repo = find_repo_by_name(ireq.repo_name);
630 return got_error(GOT_ERR_NOT_GIT_REPO);
631 err = gotd_auth_check(&repo->rules, repo->name,
632 client->euid, client->egid, GOTD_AUTH_READ);
635 client->repo_read = find_proc_by_repo_name(PROC_REPO_READ,
637 if (client->repo_read == NULL)
638 return got_error(GOT_ERR_NOT_GIT_REPO);
640 err = ensure_client_is_not_reading(client);
643 repo = find_repo_by_name(ireq.repo_name);
645 return got_error(GOT_ERR_NOT_GIT_REPO);
646 err = gotd_auth_check(&repo->rules, repo->name, client->euid,
647 client->egid, GOTD_AUTH_READ | GOTD_AUTH_WRITE);
650 client->repo_write = find_proc_by_repo_name(PROC_REPO_WRITE,
652 if (client->repo_write == NULL)
653 return got_error(GOT_ERR_NOT_GIT_REPO);
656 fd = dup(client->fd);
658 return got_error_from_errno("dup");
660 proc = get_client_proc(client);
662 fatalx("no process found for uid %d", client->euid);
663 if (gotd_imsg_compose_event(&proc->iev,
664 GOTD_IMSG_LIST_REFS_INTERNAL, PROC_GOTD, fd,
665 &ilref, sizeof(ilref)) == -1) {
666 err = got_error_from_errno("imsg compose WANT");
674 static const struct got_error *
675 forward_want(struct gotd_client *client, struct imsg *imsg)
677 struct gotd_imsg_want ireq;
678 struct gotd_imsg_want iwant;
681 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
682 if (datalen != sizeof(ireq))
683 return got_error(GOT_ERR_PRIVSEP_LEN);
685 memcpy(&ireq, imsg->data, datalen);
687 memset(&iwant, 0, sizeof(iwant));
688 memcpy(iwant.object_id, ireq.object_id, SHA1_DIGEST_LENGTH);
689 iwant.client_id = client->id;
691 if (gotd_imsg_compose_event(&client->repo_read->iev, GOTD_IMSG_WANT,
692 PROC_GOTD, -1, &iwant, sizeof(iwant)) == -1)
693 return got_error_from_errno("imsg compose WANT");
698 static const struct got_error *
699 forward_ref_update(struct gotd_client *client, struct imsg *imsg)
701 const struct got_error *err = NULL;
702 struct gotd_imsg_ref_update ireq;
703 struct gotd_imsg_ref_update *iref = NULL;
706 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
707 if (datalen < sizeof(ireq))
708 return got_error(GOT_ERR_PRIVSEP_LEN);
709 memcpy(&ireq, imsg->data, sizeof(ireq));
710 if (datalen != sizeof(ireq) + ireq.name_len)
711 return got_error(GOT_ERR_PRIVSEP_LEN);
713 iref = malloc(datalen);
715 return got_error_from_errno("malloc");
716 memcpy(iref, imsg->data, datalen);
718 iref->client_id = client->id;
719 if (gotd_imsg_compose_event(&client->repo_write->iev,
720 GOTD_IMSG_REF_UPDATE, PROC_GOTD, -1, iref, datalen) == -1)
721 err = got_error_from_errno("imsg compose REF_UPDATE");
726 static const struct got_error *
727 forward_have(struct gotd_client *client, struct imsg *imsg)
729 struct gotd_imsg_have ireq;
730 struct gotd_imsg_have ihave;
733 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
734 if (datalen != sizeof(ireq))
735 return got_error(GOT_ERR_PRIVSEP_LEN);
737 memcpy(&ireq, imsg->data, datalen);
739 memset(&ihave, 0, sizeof(ihave));
740 memcpy(ihave.object_id, ireq.object_id, SHA1_DIGEST_LENGTH);
741 ihave.client_id = client->id;
743 if (gotd_imsg_compose_event(&client->repo_read->iev, GOTD_IMSG_HAVE,
744 PROC_GOTD, -1, &ihave, sizeof(ihave)) == -1)
745 return got_error_from_errno("imsg compose HAVE");
751 client_has_capability(struct gotd_client *client, const char *capastr)
753 struct gotd_client_capability *capa;
756 if (client->ncapabilities == 0)
759 for (i = 0; i < client->ncapabilities; i++) {
760 capa = &client->capabilities[i];
761 if (strcmp(capa->key, capastr) == 0)
768 static const struct got_error *
769 recv_capabilities(struct gotd_client *client, struct imsg *imsg)
771 struct gotd_imsg_capabilities icapas;
774 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
775 if (datalen != sizeof(icapas))
776 return got_error(GOT_ERR_PRIVSEP_LEN);
777 memcpy(&icapas, imsg->data, sizeof(icapas));
779 client->ncapa_alloc = icapas.ncapabilities;
780 client->capabilities = calloc(client->ncapa_alloc,
781 sizeof(*client->capabilities));
782 if (client->capabilities == NULL) {
783 client->ncapa_alloc = 0;
784 return got_error_from_errno("calloc");
787 log_debug("expecting %zu capabilities from uid %d",
788 client->ncapa_alloc, client->euid);
792 static const struct got_error *
793 recv_capability(struct gotd_client *client, struct imsg *imsg)
795 struct gotd_imsg_capability icapa;
796 struct gotd_client_capability *capa;
798 char *key, *value = NULL;
800 if (client->capabilities == NULL ||
801 client->ncapabilities >= client->ncapa_alloc) {
802 return got_error_msg(GOT_ERR_BAD_REQUEST,
803 "unexpected capability received");
806 memset(&icapa, 0, sizeof(icapa));
808 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
809 if (datalen < sizeof(icapa))
810 return got_error(GOT_ERR_PRIVSEP_LEN);
811 memcpy(&icapa, imsg->data, sizeof(icapa));
813 if (datalen != sizeof(icapa) + icapa.key_len + icapa.value_len)
814 return got_error(GOT_ERR_PRIVSEP_LEN);
816 key = malloc(icapa.key_len + 1);
818 return got_error_from_errno("malloc");
819 if (icapa.value_len > 0) {
820 value = malloc(icapa.value_len + 1);
823 return got_error_from_errno("malloc");
827 memcpy(key, imsg->data + sizeof(icapa), icapa.key_len);
828 key[icapa.key_len] = '\0';
830 memcpy(value, imsg->data + sizeof(icapa) + icapa.key_len,
832 value[icapa.value_len] = '\0';
835 capa = &client->capabilities[client->ncapabilities++];
840 log_debug("uid %d: capability %s=%s", client->euid, key, value);
842 log_debug("uid %d: capability %s", client->euid, key);
847 static const struct got_error *
848 send_packfile(struct gotd_client *client)
850 const struct got_error *err = NULL;
851 struct gotd_imsg_send_packfile ipack;
852 struct gotd_imsg_packfile_pipe ipipe;
855 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe) == -1)
856 return got_error_from_errno("socketpair");
858 memset(&ipack, 0, sizeof(ipack));
859 memset(&ipipe, 0, sizeof(ipipe));
861 ipack.client_id = client->id;
862 if (client_has_capability(client, GOT_CAPA_SIDE_BAND_64K))
863 ipack.report_progress = 1;
865 client->delta_cache_fd = got_opentempfd();
866 if (client->delta_cache_fd == -1)
867 return got_error_from_errno("got_opentempfd");
869 if (gotd_imsg_compose_event(&client->repo_read->iev,
870 GOTD_IMSG_SEND_PACKFILE, PROC_GOTD, client->delta_cache_fd,
871 &ipack, sizeof(ipack)) == -1) {
872 err = got_error_from_errno("imsg compose SEND_PACKFILE");
878 ipipe.client_id = client->id;
880 /* Send pack pipe end 0 to repo_read. */
881 if (gotd_imsg_compose_event(&client->repo_read->iev,
882 GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[0],
883 &ipipe, sizeof(ipipe)) == -1) {
884 err = got_error_from_errno("imsg compose PACKFILE_PIPE");
889 /* Send pack pipe end 1 to gotsh(1) (expects just an fd, no data). */
890 if (gotd_imsg_compose_event(&client->iev,
891 GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[1], NULL, 0) == -1)
892 err = got_error_from_errno("imsg compose PACKFILE_PIPE");
897 static const struct got_error *
898 recv_packfile(struct gotd_client *client)
900 const struct got_error *err = NULL;
901 struct gotd_imsg_recv_packfile ipack;
902 struct gotd_imsg_packfile_pipe ipipe;
903 struct gotd_imsg_packidx_file ifile;
904 char *basepath = NULL, *pack_path = NULL, *idx_path = NULL;
905 int packfd = -1, idxfd = -1;
906 int pipe[2] = { -1, -1 };
908 if (client->packfile_path) {
909 return got_error_fmt(GOT_ERR_PRIVSEP_MSG,
910 "uid %d already has a pack file", client->euid);
913 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe) == -1)
914 return got_error_from_errno("socketpair");
916 memset(&ipipe, 0, sizeof(ipipe));
917 ipipe.client_id = client->id;
919 if (gotd_imsg_compose_event(&client->repo_write->iev,
920 GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[0],
921 &ipipe, sizeof(ipipe)) == -1) {
922 err = got_error_from_errno("imsg compose PACKFILE_PIPE");
928 if (gotd_imsg_compose_event(&client->repo_write->iev,
929 GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[1],
930 &ipipe, sizeof(ipipe)) == -1)
931 err = got_error_from_errno("imsg compose PACKFILE_PIPE");
934 if (asprintf(&basepath, "%s/%s/receiving-from-uid-%d.pack",
935 client->repo_write->chroot_path, GOT_OBJECTS_PACK_DIR,
936 client->euid) == -1) {
937 err = got_error_from_errno("asprintf");
941 err = got_opentemp_named_fd(&pack_path, &packfd, basepath, "");
946 if (asprintf(&basepath, "%s/%s/receiving-from-uid-%d.idx",
947 client->repo_write->chroot_path, GOT_OBJECTS_PACK_DIR,
948 client->euid) == -1) {
949 err = got_error_from_errno("asprintf");
953 err = got_opentemp_named_fd(&idx_path, &idxfd, basepath, "");
957 memset(&ifile, 0, sizeof(ifile));
958 ifile.client_id = client->id;
959 if (gotd_imsg_compose_event(&client->repo_write->iev,
960 GOTD_IMSG_PACKIDX_FILE, PROC_GOTD, idxfd,
961 &ifile, sizeof(ifile)) == -1) {
962 err = got_error_from_errno("imsg compose PACKIDX_FILE");
968 memset(&ipack, 0, sizeof(ipack));
969 ipack.client_id = client->id;
970 if (client_has_capability(client, GOT_CAPA_REPORT_STATUS))
971 ipack.report_status = 1;
973 if (gotd_imsg_compose_event(&client->repo_write->iev,
974 GOTD_IMSG_RECV_PACKFILE, PROC_GOTD, packfd,
975 &ipack, sizeof(ipack)) == -1) {
976 err = got_error_from_errno("imsg compose RECV_PACKFILE");
984 if (pipe[0] != -1 && close(pipe[0]) == -1 && err == NULL)
985 err = got_error_from_errno("close");
986 if (pipe[1] != -1 && close(pipe[1]) == -1 && err == NULL)
987 err = got_error_from_errno("close");
988 if (packfd != -1 && close(packfd) == -1 && err == NULL)
989 err = got_error_from_errno("close");
990 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
991 err = got_error_from_errno("close");
996 client->packfile_path = pack_path;
997 client->packidx_path = idx_path;
1003 gotd_request(int fd, short events, void *arg)
1005 struct gotd_imsgev *iev = arg;
1006 struct imsgbuf *ibuf = &iev->ibuf;
1007 struct gotd_client *client = iev->handler_arg;
1008 const struct got_error *err = NULL;
1012 if (events & EV_WRITE) {
1013 while (ibuf->w.queued) {
1014 n = msgbuf_write(&ibuf->w);
1015 if (n == -1 && errno == EPIPE) {
1017 * The client has closed its socket.
1018 * This can happen when Git clients are
1019 * done sending pack file data.
1021 msgbuf_clear(&ibuf->w);
1023 } else if (n == -1 && errno != EAGAIN) {
1024 err = got_error_from_errno("imsg_flush");
1025 disconnect_on_error(client, err);
1029 /* Connection closed. */
1030 err = got_error(GOT_ERR_EOF);
1031 disconnect_on_error(client, err);
1036 /* Disconnect gotctl(8) now that messages have been sent. */
1037 if (!client_is_reading(client) && !client_is_writing(client)) {
1043 if ((events & EV_READ) == 0)
1046 memset(&imsg, 0, sizeof(imsg));
1048 while (err == NULL) {
1049 err = gotd_imsg_recv(&imsg, ibuf, 0);
1051 if (err->code == GOT_ERR_PRIVSEP_READ)
1056 evtimer_del(&client->tmo);
1058 switch (imsg.hdr.type) {
1059 case GOTD_IMSG_INFO:
1060 err = send_info(client);
1062 case GOTD_IMSG_STOP:
1063 err = stop_gotd(client);
1065 case GOTD_IMSG_LIST_REFS:
1066 if (client->state != GOTD_STATE_EXPECT_LIST_REFS) {
1067 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1068 "unexpected list-refs request received");
1071 err = forward_list_refs_request(client, &imsg);
1074 client->state = GOTD_STATE_EXPECT_CAPABILITIES;
1075 log_debug("uid %d: expecting capabilities",
1078 case GOTD_IMSG_CAPABILITIES:
1079 if (client->state != GOTD_STATE_EXPECT_CAPABILITIES) {
1080 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1081 "unexpected capabilities received");
1084 log_debug("receiving capabilities from uid %d",
1086 err = recv_capabilities(client, &imsg);
1088 case GOTD_IMSG_CAPABILITY:
1089 if (client->state != GOTD_STATE_EXPECT_CAPABILITIES) {
1090 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1091 "unexpected capability received");
1094 err = recv_capability(client, &imsg);
1095 if (err || client->ncapabilities < client->ncapa_alloc)
1097 if (client_is_reading(client)) {
1098 client->state = GOTD_STATE_EXPECT_WANT;
1099 log_debug("uid %d: expecting want-lines",
1101 } else if (client_is_writing(client)) {
1102 client->state = GOTD_STATE_EXPECT_REF_UPDATE;
1103 log_debug("uid %d: expecting ref-update-lines",
1106 fatalx("client %d is both reading and writing",
1109 case GOTD_IMSG_WANT:
1110 if (client->state != GOTD_STATE_EXPECT_WANT) {
1111 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1112 "unexpected want-line received");
1115 log_debug("received want-line from uid %d",
1117 err = ensure_client_is_reading(client);
1120 err = forward_want(client, &imsg);
1122 case GOTD_IMSG_REF_UPDATE:
1123 if (client->state != GOTD_STATE_EXPECT_REF_UPDATE) {
1124 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1125 "unexpected ref-update-line received");
1128 log_debug("received ref-update-line from uid %d",
1130 err = ensure_client_is_writing(client);
1133 err = forward_ref_update(client, &imsg);
1136 client->state = GOTD_STATE_EXPECT_MORE_REF_UPDATES;
1138 case GOTD_IMSG_HAVE:
1139 if (client->state != GOTD_STATE_EXPECT_HAVE) {
1140 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1141 "unexpected have-line received");
1144 log_debug("received have-line from uid %d",
1146 err = ensure_client_is_reading(client);
1149 err = forward_have(client, &imsg);
1153 case GOTD_IMSG_FLUSH:
1154 if (client->state == GOTD_STATE_EXPECT_WANT ||
1155 client->state == GOTD_STATE_EXPECT_HAVE) {
1156 err = ensure_client_is_reading(client);
1159 } else if (client->state ==
1160 GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1161 err = ensure_client_is_writing(client);
1165 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1166 "unexpected flush-pkt received");
1169 log_debug("received flush-pkt from uid %d",
1171 if (client->state == GOTD_STATE_EXPECT_WANT) {
1172 client->state = GOTD_STATE_EXPECT_HAVE;
1173 log_debug("uid %d: expecting have-lines",
1175 } else if (client->state == GOTD_STATE_EXPECT_HAVE) {
1176 client->state = GOTD_STATE_EXPECT_DONE;
1177 log_debug("uid %d: expecting 'done'",
1179 } else if (client->state ==
1180 GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1181 client->state = GOTD_STATE_EXPECT_PACKFILE;
1182 log_debug("uid %d: expecting packfile",
1184 err = recv_packfile(client);
1186 /* should not happen, see above */
1187 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1188 "unexpected client state");
1192 case GOTD_IMSG_DONE:
1193 if (client->state != GOTD_STATE_EXPECT_HAVE &&
1194 client->state != GOTD_STATE_EXPECT_DONE) {
1195 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1196 "unexpected flush-pkt received");
1199 log_debug("received 'done' from uid %d", client->euid);
1200 err = ensure_client_is_reading(client);
1203 client->state = GOTD_STATE_DONE;
1204 err = send_packfile(client);
1207 err = got_error(GOT_ERR_PRIVSEP_MSG);
1215 if (err->code != GOT_ERR_EOF ||
1216 client->state != GOTD_STATE_EXPECT_PACKFILE)
1217 disconnect_on_error(client, err);
1219 gotd_imsg_event_add(&client->iev);
1220 evtimer_add(&client->tmo, &timeout);
1225 gotd_request_timeout(int fd, short events, void *arg)
1227 struct gotd_client *client = arg;
1229 log_debug("disconnecting uid %d due to timeout", client->euid);
1234 gotd_accept(int fd, short event, void *arg)
1236 struct sockaddr_storage ss;
1237 struct timeval backoff;
1240 struct gotd_client *client = NULL;
1245 backoff.tv_usec = 0;
1247 if (event_add(&gotd.ev, NULL) == -1) {
1248 log_warn("event_add");
1251 if (event & EV_TIMEOUT)
1256 s = accept_reserve(fd, (struct sockaddr *)&ss, &len, GOTD_FD_RESERVE,
1267 event_del(&gotd.ev);
1268 evtimer_add(&gotd.pause, &backoff);
1271 log_warn("%s: accept", __func__);
1276 if (client_cnt >= GOTD_MAXCLIENTS)
1279 if (getpeereid(s, &euid, &egid) == -1) {
1280 log_warn("%s: getpeereid", __func__);
1284 client = calloc(1, sizeof(*client));
1285 if (client == NULL) {
1286 log_warn("%s: calloc", __func__);
1290 client->state = GOTD_STATE_EXPECT_LIST_REFS;
1291 client->id = get_client_id();
1294 client->delta_cache_fd = -1;
1295 client->euid = euid;
1296 client->egid = egid;
1297 client->nref_updates = -1;
1299 imsg_init(&client->iev.ibuf, client->fd);
1300 client->iev.handler = gotd_request;
1301 client->iev.events = EV_READ;
1302 client->iev.handler_arg = client;
1304 event_set(&client->iev.ev, client->fd, EV_READ, gotd_request,
1306 gotd_imsg_event_add(&client->iev);
1308 evtimer_set(&client->tmo, gotd_request_timeout, client);
1311 log_debug("%s: new client uid %d connected on fd %d", __func__,
1312 client->euid, client->fd);
1322 gotd_accept_paused(int fd, short event, void *arg)
1324 event_add(&gotd.ev, NULL);
1327 static const char *gotd_proc_names[PROC_MAX] = {
1333 static struct gotd_child_proc *
1334 get_proc_for_pid(pid_t pid)
1336 struct gotd_child_proc *proc;
1339 for (i = 0; i < gotd.nprocs; i++) {
1340 proc = &gotd.procs[i];
1341 if (proc->pid == pid)
1349 kill_proc(struct gotd_child_proc *proc, int fatal)
1352 log_warnx("sending SIGKILL to PID %d", proc->pid);
1353 kill(proc->pid, SIGKILL);
1355 kill(proc->pid, SIGTERM);
1363 struct gotd_child_proc *proc;
1365 for (i = 0; i < gotd.nprocs; i++) {
1366 proc = &gotd.procs[i];
1367 msgbuf_clear(&proc->iev.ibuf.w);
1368 close(proc->iev.ibuf.fd);
1372 log_debug("waiting for children to terminate");
1374 pid = wait(&status);
1376 if (errno != EINTR && errno != ECHILD)
1378 } else if (WIFSIGNALED(status)) {
1379 proc = get_proc_for_pid(pid);
1380 log_warnx("%s %s child process terminated; signal %d",
1381 proc ? gotd_proc_names[proc->type] : "",
1382 proc ? proc->chroot_path : "", WTERMSIG(status));
1384 } while (pid != -1 || (pid == -1 && errno == EINTR));
1386 log_info("terminating");
1391 gotd_sighdlr(int sig, short event, void *arg)
1394 * Normal signal handler rules don't apply because libevent
1400 log_info("%s: ignoring SIGHUP", __func__);
1403 log_info("%s: ignoring SIGUSR1", __func__);
1408 log_warnx("gotd terminating");
1412 fatalx("unexpected signal");
1416 static const struct got_error *
1417 ensure_proc_is_reading(struct gotd_client *client,
1418 struct gotd_child_proc *proc)
1420 if (!client_is_reading(client)) {
1422 return got_error_fmt(GOT_ERR_BAD_PACKET,
1423 "PID %d handled a read-request for uid %d but this "
1424 "user is not reading from a repository", proc->pid,
1431 static const struct got_error *
1432 ensure_proc_is_writing(struct gotd_client *client,
1433 struct gotd_child_proc *proc)
1435 if (!client_is_writing(client)) {
1437 return got_error_fmt(GOT_ERR_BAD_PACKET,
1438 "PID %d handled a write-request for uid %d but this "
1439 "user is not writing to a repository", proc->pid,
1447 verify_imsg_src(struct gotd_client *client, struct gotd_child_proc *proc,
1450 const struct got_error *err;
1451 struct gotd_child_proc *client_proc;
1454 client_proc = get_client_proc(client);
1455 if (client_proc == NULL)
1456 fatalx("no process found for uid %d", client->euid);
1458 if (proc->pid != client_proc->pid) {
1460 log_warnx("received message from PID %d for uid %d, while "
1461 "PID %d is the process serving this user",
1462 proc->pid, client->euid, client_proc->pid);
1466 switch (imsg->hdr.type) {
1467 case GOTD_IMSG_ERROR:
1470 case GOTD_IMSG_PACKFILE_DONE:
1471 err = ensure_proc_is_reading(client, proc);
1473 log_warnx("uid %d: %s", client->euid, err->msg);
1477 case GOTD_IMSG_PACKFILE_INSTALL:
1478 case GOTD_IMSG_REF_UPDATES_START:
1479 case GOTD_IMSG_REF_UPDATE:
1480 err = ensure_proc_is_writing(client, proc);
1482 log_warnx("uid %d: %s", client->euid, err->msg);
1487 log_debug("%s: unexpected imsg %d", __func__, imsg->hdr.type);
1494 static const struct got_error *
1495 recv_packfile_done(uint32_t *client_id, struct imsg *imsg)
1497 struct gotd_imsg_packfile_done idone;
1500 log_debug("packfile-done received");
1502 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1503 if (datalen != sizeof(idone))
1504 return got_error(GOT_ERR_PRIVSEP_LEN);
1505 memcpy(&idone, imsg->data, sizeof(idone));
1507 *client_id = idone.client_id;
1511 static const struct got_error *
1512 recv_packfile_install(uint32_t *client_id, struct imsg *imsg)
1514 struct gotd_imsg_packfile_install inst;
1517 log_debug("packfile-install received");
1519 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1520 if (datalen != sizeof(inst))
1521 return got_error(GOT_ERR_PRIVSEP_LEN);
1522 memcpy(&inst, imsg->data, sizeof(inst));
1524 *client_id = inst.client_id;
1528 static const struct got_error *
1529 recv_ref_updates_start(uint32_t *client_id, struct imsg *imsg)
1531 struct gotd_imsg_ref_updates_start istart;
1534 log_debug("ref-updates-start received");
1536 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1537 if (datalen != sizeof(istart))
1538 return got_error(GOT_ERR_PRIVSEP_LEN);
1539 memcpy(&istart, imsg->data, sizeof(istart));
1541 *client_id = istart.client_id;
1545 static const struct got_error *
1546 recv_ref_update(uint32_t *client_id, struct imsg *imsg)
1548 struct gotd_imsg_ref_update iref;
1551 log_debug("ref-update received");
1553 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1554 if (datalen < sizeof(iref))
1555 return got_error(GOT_ERR_PRIVSEP_LEN);
1556 memcpy(&iref, imsg->data, sizeof(iref));
1558 *client_id = iref.client_id;
1562 static const struct got_error *
1563 send_ref_update_ok(struct gotd_client *client,
1564 struct gotd_imsg_ref_update *iref, const char *refname)
1566 struct gotd_imsg_ref_update_ok iok;
1570 memset(&iok, 0, sizeof(iok));
1571 iok.client_id = client->id;
1572 memcpy(iok.old_id, iref->old_id, SHA1_DIGEST_LENGTH);
1573 memcpy(iok.new_id, iref->new_id, SHA1_DIGEST_LENGTH);
1574 iok.name_len = strlen(refname);
1576 len = sizeof(iok) + iok.name_len;
1577 wbuf = imsg_create(&client->iev.ibuf, GOTD_IMSG_REF_UPDATE_OK,
1578 PROC_GOTD, gotd.pid, len);
1580 return got_error_from_errno("imsg_create REF_UPDATE_OK");
1582 if (imsg_add(wbuf, &iok, sizeof(iok)) == -1)
1583 return got_error_from_errno("imsg_add REF_UPDATE_OK");
1584 if (imsg_add(wbuf, refname, iok.name_len) == -1)
1585 return got_error_from_errno("imsg_add REF_UPDATE_OK");
1588 imsg_close(&client->iev.ibuf, wbuf);
1589 gotd_imsg_event_add(&client->iev);
1594 send_refs_updated(struct gotd_client *client)
1596 if (gotd_imsg_compose_event(&client->iev,
1597 GOTD_IMSG_REFS_UPDATED, PROC_GOTD, -1, NULL, 0) == -1)
1598 log_warn("imsg compose REFS_UPDATED");
1601 static const struct got_error *
1602 send_ref_update_ng(struct gotd_client *client,
1603 struct gotd_imsg_ref_update *iref, const char *refname,
1606 const struct got_error *ng_err;
1607 struct gotd_imsg_ref_update_ng ing;
1611 memset(&ing, 0, sizeof(ing));
1612 ing.client_id = client->id;
1613 memcpy(ing.old_id, iref->old_id, SHA1_DIGEST_LENGTH);
1614 memcpy(ing.new_id, iref->new_id, SHA1_DIGEST_LENGTH);
1615 ing.name_len = strlen(refname);
1617 ng_err = got_error_fmt(GOT_ERR_REF_BUSY, "%s", reason);
1618 ing.reason_len = strlen(ng_err->msg);
1620 len = sizeof(ing) + ing.name_len + ing.reason_len;
1621 wbuf = imsg_create(&client->iev.ibuf, GOTD_IMSG_REF_UPDATE_NG,
1622 PROC_GOTD, gotd.pid, len);
1624 return got_error_from_errno("imsg_create REF_UPDATE_NG");
1626 if (imsg_add(wbuf, &ing, sizeof(ing)) == -1)
1627 return got_error_from_errno("imsg_add REF_UPDATE_NG");
1628 if (imsg_add(wbuf, refname, ing.name_len) == -1)
1629 return got_error_from_errno("imsg_add REF_UPDATE_NG");
1630 if (imsg_add(wbuf, ng_err->msg, ing.reason_len) == -1)
1631 return got_error_from_errno("imsg_add REF_UPDATE_NG");
1634 imsg_close(&client->iev.ibuf, wbuf);
1635 gotd_imsg_event_add(&client->iev);
1639 static const struct got_error *
1640 install_pack(struct gotd_client *client, const char *repo_path,
1643 const struct got_error *err = NULL;
1644 struct gotd_imsg_packfile_install inst;
1645 char hex[SHA1_DIGEST_STRING_LENGTH];
1647 char *packfile_path = NULL, *packidx_path = NULL;
1649 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1650 if (datalen != sizeof(inst))
1651 return got_error(GOT_ERR_PRIVSEP_LEN);
1652 memcpy(&inst, imsg->data, sizeof(inst));
1654 if (client->packfile_path == NULL)
1655 return got_error_msg(GOT_ERR_BAD_REQUEST,
1656 "client has no pack file");
1657 if (client->packidx_path == NULL)
1658 return got_error_msg(GOT_ERR_BAD_REQUEST,
1659 "client has no pack file index");
1661 if (got_sha1_digest_to_str(inst.pack_sha1, hex, sizeof(hex)) == NULL)
1662 return got_error_msg(GOT_ERR_NO_SPACE,
1663 "could not convert pack file SHA1 to hex");
1665 if (asprintf(&packfile_path, "/%s/%s/pack-%s.pack",
1666 repo_path, GOT_OBJECTS_PACK_DIR, hex) == -1) {
1667 err = got_error_from_errno("asprintf");
1671 if (asprintf(&packidx_path, "/%s/%s/pack-%s.idx",
1672 repo_path, GOT_OBJECTS_PACK_DIR, hex) == -1) {
1673 err = got_error_from_errno("asprintf");
1677 if (rename(client->packfile_path, packfile_path) == -1) {
1678 err = got_error_from_errno3("rename", client->packfile_path,
1683 free(client->packfile_path);
1684 client->packfile_path = NULL;
1686 if (rename(client->packidx_path, packidx_path) == -1) {
1687 err = got_error_from_errno3("rename", client->packidx_path,
1692 free(client->packidx_path);
1693 client->packidx_path = NULL;
1695 free(packfile_path);
1700 static const struct got_error *
1701 begin_ref_updates(struct gotd_client *client, struct imsg *imsg)
1703 struct gotd_imsg_ref_updates_start istart;
1706 if (client->nref_updates != -1)
1707 return got_error(GOT_ERR_PRIVSEP_MSG);
1709 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1710 if (datalen != sizeof(istart))
1711 return got_error(GOT_ERR_PRIVSEP_LEN);
1712 memcpy(&istart, imsg->data, sizeof(istart));
1714 if (istart.nref_updates <= 0)
1715 return got_error(GOT_ERR_PRIVSEP_MSG);
1717 client->nref_updates = istart.nref_updates;
1721 static const struct got_error *
1722 update_ref(struct gotd_client *client, const char *repo_path,
1725 const struct got_error *err = NULL;
1726 struct got_repository *repo = NULL;
1727 struct got_reference *ref = NULL;
1728 struct gotd_imsg_ref_update iref;
1729 struct got_object_id old_id, new_id;
1730 struct got_object_id *id = NULL;
1731 struct got_object *obj = NULL;
1732 char *refname = NULL;
1736 log_debug("update-ref from uid %d", client->euid);
1738 if (client->nref_updates <= 0)
1739 return got_error(GOT_ERR_PRIVSEP_MSG);
1741 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1742 if (datalen < sizeof(iref))
1743 return got_error(GOT_ERR_PRIVSEP_LEN);
1744 memcpy(&iref, imsg->data, sizeof(iref));
1745 if (datalen != sizeof(iref) + iref.name_len)
1746 return got_error(GOT_ERR_PRIVSEP_LEN);
1747 refname = malloc(iref.name_len + 1);
1748 if (refname == NULL)
1749 return got_error_from_errno("malloc");
1750 memcpy(refname, imsg->data + sizeof(iref), iref.name_len);
1751 refname[iref.name_len] = '\0';
1753 log_debug("updating ref %s for uid %d", refname, client->euid);
1755 err = got_repo_open(&repo, repo_path, NULL, NULL);
1759 memcpy(old_id.sha1, iref.old_id, SHA1_DIGEST_LENGTH);
1760 memcpy(new_id.sha1, iref.new_id, SHA1_DIGEST_LENGTH);
1761 err = got_object_open(&obj, repo, &new_id);
1765 if (iref.ref_is_new) {
1766 err = got_ref_open(&ref, repo, refname, 0);
1768 if (err->code != GOT_ERR_NOT_REF)
1770 err = got_ref_alloc(&ref, refname, &new_id);
1773 err = got_ref_write(ref, repo); /* will lock/unlock */
1777 err = got_error_fmt(GOT_ERR_REF_BUSY,
1778 "%s has been created by someone else "
1779 "while transaction was in progress",
1780 got_ref_get_name(ref));
1784 err = got_ref_open(&ref, repo, refname, 1 /* lock */);
1789 err = got_ref_resolve(&id, repo, ref);
1793 if (got_object_id_cmp(id, &old_id) != 0) {
1794 err = got_error_fmt(GOT_ERR_REF_BUSY,
1795 "%s has been modified by someone else "
1796 "while transaction was in progress",
1797 got_ref_get_name(ref));
1801 err = got_ref_change_ref(ref, &new_id);
1805 err = got_ref_write(ref, repo);
1814 if (err->code == GOT_ERR_LOCKFILE_TIMEOUT) {
1815 err = got_error_fmt(GOT_ERR_LOCKFILE_TIMEOUT,
1816 "could not acquire exclusive file lock for %s",
1819 send_ref_update_ng(client, &iref, refname, err->msg);
1821 send_ref_update_ok(client, &iref, refname);
1823 if (client->nref_updates > 0) {
1824 client->nref_updates--;
1825 if (client->nref_updates == 0)
1826 send_refs_updated(client);
1830 const struct got_error *unlock_err;
1831 unlock_err = got_ref_unlock(ref);
1832 if (unlock_err && err == NULL)
1838 got_object_close(obj);
1840 got_repo_close(repo);
1847 gotd_dispatch(int fd, short event, void *arg)
1849 struct gotd_imsgev *iev = arg;
1850 struct imsgbuf *ibuf = &iev->ibuf;
1851 struct gotd_child_proc *proc = NULL;
1856 if (event & EV_READ) {
1857 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1858 fatal("imsg_read error");
1860 /* Connection closed. */
1866 if (event & EV_WRITE) {
1867 n = msgbuf_write(&ibuf->w);
1868 if (n == -1 && errno != EAGAIN)
1869 fatal("msgbuf_write");
1871 /* Connection closed. */
1877 proc = find_proc_by_fd(fd);
1879 fatalx("cannot find child process for fd %d", fd);
1882 const struct got_error *err = NULL;
1883 struct gotd_client *client = NULL;
1884 uint32_t client_id = 0;
1885 int do_disconnect = 0;
1886 int do_ref_updates = 0, do_ref_update = 0;
1887 int do_packfile_install = 0;
1889 if ((n = imsg_get(ibuf, &imsg)) == -1)
1890 fatal("%s: imsg_get error", __func__);
1891 if (n == 0) /* No more messages. */
1894 switch (imsg.hdr.type) {
1895 case GOTD_IMSG_ERROR:
1897 err = gotd_imsg_recv_error(&client_id, &imsg);
1899 case GOTD_IMSG_PACKFILE_DONE:
1901 err = recv_packfile_done(&client_id, &imsg);
1903 case GOTD_IMSG_PACKFILE_INSTALL:
1904 err = recv_packfile_install(&client_id, &imsg);
1906 do_packfile_install = 1;
1908 case GOTD_IMSG_REF_UPDATES_START:
1909 err = recv_ref_updates_start(&client_id, &imsg);
1913 case GOTD_IMSG_REF_UPDATE:
1914 err = recv_ref_update(&client_id, &imsg);
1919 log_debug("unexpected imsg %d", imsg.hdr.type);
1923 client = find_client(client_id);
1924 if (client == NULL) {
1925 log_warnx("%s: client not found", __func__);
1930 if (!verify_imsg_src(client, proc, &imsg)) {
1931 log_debug("dropping imsg type %d from PID %d",
1932 imsg.hdr.type, proc->pid);
1937 log_warnx("uid %d: %s", client->euid, err->msg);
1939 if (do_disconnect) {
1941 disconnect_on_error(client, err);
1945 if (do_packfile_install)
1946 err = install_pack(client, proc->chroot_path,
1948 else if (do_ref_updates)
1949 err = begin_ref_updates(client, &imsg);
1950 else if (do_ref_update)
1951 err = update_ref(client, proc->chroot_path,
1954 log_warnx("uid %d: %s", client->euid, err->msg);
1960 gotd_imsg_event_add(iev);
1962 /* This pipe is dead. Remove its event handler */
1963 event_del(&iev->ev);
1964 event_loopexit(NULL);
1969 start_child(enum gotd_procid proc_id, const char *chroot_path,
1970 char *argv0, const char *confpath, int fd, int daemonize, int verbosity)
1976 switch (pid = fork()) {
1978 fatal("cannot fork");
1986 if (fd != GOTD_FILENO_MSG_PIPE) {
1987 if (dup2(fd, GOTD_FILENO_MSG_PIPE) == -1)
1988 fatal("cannot setup imsg fd");
1989 } else if (fcntl(fd, F_SETFD, 0) == -1)
1990 fatal("cannot setup imsg fd");
1992 argv[argc++] = argv0;
1994 case PROC_REPO_READ:
1995 argv[argc++] = (char *)"-R";
1997 case PROC_REPO_WRITE:
1998 argv[argc++] = (char *)"-W";
2001 fatalx("invalid process id %d", proc_id);
2004 argv[argc++] = (char *)"-f";
2005 argv[argc++] = (char *)confpath;
2007 argv[argc++] = (char *)"-P";
2008 argv[argc++] = (char *)chroot_path;
2011 argv[argc++] = (char *)"-d";
2013 argv[argc++] = (char *)"-v";
2015 argv[argc++] = (char *)"-v";
2016 argv[argc++] = NULL;
2018 execvp(argv0, argv);
2023 start_repo_children(struct gotd *gotd, char *argv0, const char *confpath,
2024 int daemonize, int verbosity)
2026 struct gotd_repo *repo = NULL;
2027 struct gotd_child_proc *proc;
2031 * XXX For now, use one reader and one writer per repository.
2032 * This should be changed to N readers + M writers.
2034 gotd->nprocs = gotd->nrepos * 2;
2035 gotd->procs = calloc(gotd->nprocs, sizeof(*gotd->procs));
2036 if (gotd->procs == NULL)
2038 for (i = 0; i < gotd->nprocs; i++) {
2040 repo = TAILQ_FIRST(&gotd->repos);
2041 proc = &gotd->procs[i];
2042 if (i < gotd->nrepos)
2043 proc->type = PROC_REPO_READ;
2045 proc->type = PROC_REPO_WRITE;
2046 if (strlcpy(proc->repo_name, repo->name,
2047 sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
2048 fatalx("repository name too long: %s", repo->name);
2049 log_debug("adding repository %s", repo->name);
2050 if (realpath(repo->path, proc->chroot_path) == NULL)
2051 fatal("%s", repo->path);
2052 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
2053 PF_UNSPEC, proc->pipe) == -1)
2054 fatal("socketpair");
2055 proc->pid = start_child(proc->type, proc->chroot_path, argv0,
2056 confpath, proc->pipe[1], daemonize, verbosity);
2057 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
2058 log_debug("proc %s %s is on fd %d",
2059 gotd_proc_names[proc->type], proc->chroot_path,
2061 proc->iev.handler = gotd_dispatch;
2062 proc->iev.events = EV_READ;
2063 proc->iev.handler_arg = NULL;
2064 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
2065 gotd_dispatch, &proc->iev);
2067 repo = TAILQ_NEXT(repo, entry);
2074 struct gotd_repo *repo;
2076 TAILQ_FOREACH(repo, &gotd.repos, entry) {
2077 if (unveil(repo->path, "rwc") == -1)
2078 fatal("unveil %s", repo->path);
2081 if (unveil(GOT_TMPDIR_STR, "rwc") == -1)
2082 fatal("unveil %s", GOT_TMPDIR_STR);
2084 if (unveil(NULL, NULL) == -1)
2089 main(int argc, char **argv)
2091 const struct got_error *error = NULL;
2092 int ch, fd = -1, daemonize = 1, verbosity = 0, noaction = 0;
2093 const char *confpath = GOTD_CONF_PATH;
2094 char *argv0 = argv[0];
2096 gid_t groups[NGROUPS_MAX];
2097 int ngroups = NGROUPS_MAX;
2098 struct passwd *pw = NULL;
2099 struct group *gr = NULL;
2100 char *repo_path = NULL;
2101 enum gotd_procid proc_id = PROC_GOTD;
2102 struct event evsigint, evsigterm, evsighup, evsigusr1;
2103 int *pack_fds = NULL, *temp_fds = NULL;
2105 log_init(1, LOG_DAEMON); /* Log to stderr until daemonized. */
2107 while ((ch = getopt(argc, argv, "df:nP:RvW")) != -1) {
2119 repo_path = realpath(optarg, NULL);
2120 if (repo_path == NULL)
2121 fatal("realpath '%s'", optarg);
2124 proc_id = PROC_REPO_READ;
2131 proc_id = PROC_REPO_WRITE;
2145 fatalx("need root privileges");
2147 log_init(daemonize ? 0 : 1, LOG_DAEMON);
2148 log_setverbose(verbosity);
2150 if (parse_config(confpath, proc_id, &gotd) != 0)
2153 if (proc_id == PROC_GOTD &&
2154 (gotd.nrepos == 0 || TAILQ_EMPTY(&gotd.repos)))
2155 fatalx("no repository defined in configuration file");
2157 pw = getpwnam(gotd.user_name);
2159 fatal("getpwuid: user %s not found", gotd.user_name);
2161 if (pw->pw_uid == 0) {
2162 fatalx("cannot run %s as %s: the user running %s "
2163 "must not be the superuser",
2164 getprogname(), pw->pw_name, getprogname());
2167 if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups) == -1)
2168 log_warnx("group membership list truncated");
2170 gr = match_group(groups, ngroups, gotd.unix_group_name);
2172 fatalx("cannot start %s: the user running %s "
2173 "must be a secondary member of group %s",
2174 getprogname(), getprogname(), gotd.unix_group_name);
2176 if (gr->gr_gid == pw->pw_gid) {
2177 fatalx("cannot start %s: the user running %s "
2178 "must be a secondary member of group %s, but "
2179 "%s is the user's primary group",
2180 getprogname(), getprogname(), gotd.unix_group_name,
2181 gotd.unix_group_name);
2184 if (proc_id == PROC_GOTD &&
2185 !got_path_is_absolute(gotd.unix_socket_path))
2186 fatalx("bad unix socket path \"%s\": must be an absolute path",
2187 gotd.unix_socket_path);
2192 if (proc_id == PROC_GOTD) {
2194 log_info("socket: %s", gotd.unix_socket_path);
2195 log_info("user: %s", pw->pw_name);
2196 log_info("secondary group: %s", gr->gr_name);
2199 fd = unix_socket_listen(gotd.unix_socket_path, pw->pw_uid,
2202 fatal("cannot listen on unix socket %s",
2203 gotd.unix_socket_path);
2207 if (proc_id == PROC_GOTD) {
2208 gotd.pid = getpid();
2209 snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
2210 start_repo_children(&gotd, argv0, confpath, daemonize,
2212 arc4random_buf(&clients_hash_key, sizeof(clients_hash_key));
2213 if (daemonize && daemon(0, 0) == -1)
2215 } else if (proc_id == PROC_REPO_READ || proc_id == PROC_REPO_WRITE) {
2216 error = got_repo_pack_fds_open(&pack_fds);
2218 fatalx("cannot open pack tempfiles: %s", error->msg);
2219 error = got_repo_temp_fds_open(&temp_fds);
2221 fatalx("cannot open pack tempfiles: %s", error->msg);
2222 if (repo_path == NULL)
2223 fatalx("repository path not specified");
2224 snprintf(title, sizeof(title), "%s %s",
2225 gotd_proc_names[proc_id], repo_path);
2226 if (chroot(repo_path) == -1)
2228 if (chdir("/") == -1)
2229 fatal("chdir(\"/\")");
2230 if (daemonize && daemon(1, 0) == -1)
2233 fatal("invalid process id %d", proc_id);
2235 setproctitle("%s", title);
2236 log_procinit(title);
2238 /* Drop root privileges. */
2239 if (setgid(pw->pw_gid) == -1)
2240 fatal("setgid %d failed", pw->pw_gid);
2241 if (setuid(pw->pw_uid) == -1)
2242 fatal("setuid %d failed", pw->pw_uid);
2249 if (pledge("stdio rpath wpath cpath proc getpw sendfd recvfd "
2250 "fattr flock unix unveil", NULL) == -1)
2254 case PROC_REPO_READ:
2256 if (pledge("stdio rpath recvfd", NULL) == -1)
2259 repo_read_main(title, pack_fds, temp_fds);
2262 case PROC_REPO_WRITE:
2264 if (pledge("stdio rpath sendfd recvfd", NULL) == -1)
2267 repo_write_main(title, pack_fds, temp_fds);
2271 fatal("invalid process id %d", proc_id);
2274 if (proc_id != PROC_GOTD)
2275 fatal("invalid process id %d", proc_id);
2279 signal_set(&evsigint, SIGINT, gotd_sighdlr, NULL);
2280 signal_set(&evsigterm, SIGTERM, gotd_sighdlr, NULL);
2281 signal_set(&evsighup, SIGHUP, gotd_sighdlr, NULL);
2282 signal_set(&evsigusr1, SIGUSR1, gotd_sighdlr, NULL);
2283 signal(SIGPIPE, SIG_IGN);
2285 signal_add(&evsigint, NULL);
2286 signal_add(&evsigterm, NULL);
2287 signal_add(&evsighup, NULL);
2288 signal_add(&evsigusr1, NULL);
2290 event_set(&gotd.ev, fd, EV_READ | EV_PERSIST, gotd_accept, NULL);
2291 if (event_add(&gotd.ev, NULL))
2292 fatalx("event add");
2293 evtimer_set(&gotd.pause, gotd_accept_paused, NULL);
2300 got_repo_pack_fds_close(pack_fds);