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"
63 #include "repo_read.h"
64 #include "repo_write.h"
67 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
71 STAILQ_ENTRY(gotd_client) entry;
72 enum gotd_client_state state;
73 struct gotd_client_capability *capabilities;
79 struct gotd_imsgev iev;
83 struct gotd_child_proc *repo_read;
84 struct gotd_child_proc *repo_write;
85 struct gotd_child_proc *auth;
91 STAILQ_HEAD(gotd_clients, gotd_client);
93 static struct gotd_clients gotd_clients[GOTD_CLIENT_TABLE_SIZE];
94 static SIPHASH_KEY clients_hash_key;
95 volatile int client_cnt;
96 static struct timeval timeout = { 3600, 0 };
97 static struct timeval auth_timeout = { 5, 0 };
98 static struct gotd gotd;
100 void gotd_sighdlr(int sig, short event, void *arg);
101 static void gotd_shutdown(void);
102 static const struct got_error *start_repo_child(struct gotd_client *,
103 enum gotd_procid, struct gotd_repo *, char *, const char *, int, int);
104 static const struct got_error *start_auth_child(struct gotd_client *, int,
105 struct gotd_repo *, char *, const char *, int, int);
106 static void kill_proc(struct gotd_child_proc *, int);
111 fprintf(stderr, "usage: %s [-dv] [-f config-file]\n", getprogname());
116 unix_socket_listen(const char *unix_socket_path, uid_t uid, gid_t gid)
118 struct sockaddr_un sun;
120 mode_t old_umask, mode;
122 fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK| SOCK_CLOEXEC, 0);
128 sun.sun_family = AF_UNIX;
129 if (strlcpy(sun.sun_path, unix_socket_path,
130 sizeof(sun.sun_path)) >= sizeof(sun.sun_path)) {
131 log_warnx("%s: name too long", unix_socket_path);
136 if (unlink(unix_socket_path) == -1) {
137 if (errno != ENOENT) {
138 log_warn("unlink %s", unix_socket_path);
144 old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
145 mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP;
147 if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
148 log_warn("bind: %s", unix_socket_path);
156 if (chmod(unix_socket_path, mode) == -1) {
157 log_warn("chmod %o %s", mode, unix_socket_path);
159 unlink(unix_socket_path);
163 if (chown(unix_socket_path, uid, gid) == -1) {
164 log_warn("chown %s uid=%d gid=%d", unix_socket_path, uid, gid);
166 unlink(unix_socket_path);
170 if (listen(fd, GOTD_UNIX_SOCKET_BACKLOG) == -1) {
173 unlink(unix_socket_path);
180 static struct group *
181 match_group(gid_t *groups, int ngroups, const char *unix_group_name)
186 for (i = 0; i < ngroups; i++) {
187 gr = getgrgid(groups[i]);
189 log_warn("getgrgid %d", groups[i]);
192 if (strcmp(gr->gr_name, unix_group_name) == 0)
200 client_hash(uint32_t client_id)
202 return SipHash24(&clients_hash_key, &client_id, sizeof(client_id));
206 add_client(struct gotd_client *client)
208 uint64_t slot = client_hash(client->id) % nitems(gotd_clients);
209 STAILQ_INSERT_HEAD(&gotd_clients[slot], client, entry);
213 static struct gotd_client *
214 find_client(uint32_t client_id)
217 struct gotd_client *c;
219 slot = client_hash(client_id) % nitems(gotd_clients);
220 STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
221 if (c->id == client_id)
228 static struct gotd_child_proc *
229 get_client_proc(struct gotd_client *client)
231 if (client->repo_read && client->repo_write) {
232 fatalx("uid %d is reading and writing in the same session",
237 if (client->repo_read)
238 return client->repo_read;
239 else if (client->repo_write)
240 return client->repo_write;
245 static struct gotd_client *
246 find_client_by_proc_fd(int fd)
250 for (slot = 0; slot < nitems(gotd_clients); slot++) {
251 struct gotd_client *c;
253 STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
254 struct gotd_child_proc *proc = get_client_proc(c);
255 if (proc && proc->iev.ibuf.fd == fd)
257 if (c->auth && c->auth->iev.ibuf.fd == fd)
266 client_is_reading(struct gotd_client *client)
268 return client->repo_read != NULL;
272 client_is_writing(struct gotd_client *client)
274 return client->repo_write != NULL;
277 static const struct got_error *
278 ensure_client_is_reading(struct gotd_client *client)
280 if (!client_is_reading(client)) {
281 return got_error_fmt(GOT_ERR_BAD_PACKET,
282 "uid %d made a read-request but is not reading from "
283 "a repository", client->euid);
289 static const struct got_error *
290 ensure_client_is_writing(struct gotd_client *client)
292 if (!client_is_writing(client)) {
293 return got_error_fmt(GOT_ERR_BAD_PACKET,
294 "uid %d made a write-request but is not writing to "
295 "a repository", client->euid);
301 static const struct got_error *
302 ensure_client_is_not_writing(struct gotd_client *client)
304 if (client_is_writing(client)) {
305 return got_error_fmt(GOT_ERR_BAD_PACKET,
306 "uid %d made a read-request but is writing to "
307 "a repository", client->euid);
313 static const struct got_error *
314 ensure_client_is_not_reading(struct gotd_client *client)
316 if (client_is_reading(client)) {
317 return got_error_fmt(GOT_ERR_BAD_PACKET,
318 "uid %d made a write-request but is reading from "
319 "a repository", client->euid);
326 wait_for_child(pid_t child_pid)
331 log_debug("waiting for child PID %ld to terminate",
335 pid = waitpid(child_pid, &status, WNOHANG);
337 if (errno != EINTR && errno != ECHILD)
339 } else if (WIFSIGNALED(status)) {
340 log_warnx("child PID %ld terminated; signal %d",
341 (long)pid, WTERMSIG(status));
343 } while (pid != -1 || (pid == -1 && errno == EINTR));
347 kill_auth_proc(struct gotd_client *client)
349 struct gotd_child_proc *proc;
351 if (client->auth == NULL)
357 event_del(&proc->iev.ev);
358 msgbuf_clear(&proc->iev.ibuf.w);
359 close(proc->iev.ibuf.fd);
361 wait_for_child(proc->pid);
366 disconnect(struct gotd_client *client)
368 struct gotd_imsg_disconnect idisconnect;
369 struct gotd_child_proc *proc = get_client_proc(client);
370 struct gotd_child_proc *listen_proc = &gotd.listen_proc;
373 log_debug("uid %d: disconnecting", client->euid);
375 kill_auth_proc(client);
377 idisconnect.client_id = client->id;
379 if (gotd_imsg_compose_event(&proc->iev,
380 GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
381 &idisconnect, sizeof(idisconnect)) == -1)
382 log_warn("imsg compose DISCONNECT");
384 msgbuf_clear(&proc->iev.ibuf.w);
385 close(proc->iev.ibuf.fd);
387 wait_for_child(proc->pid);
392 if (gotd_imsg_compose_event(&listen_proc->iev,
393 GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
394 &idisconnect, sizeof(idisconnect)) == -1)
395 log_warn("imsg compose DISCONNECT");
397 slot = client_hash(client->id) % nitems(gotd_clients);
398 STAILQ_REMOVE(&gotd_clients[slot], client, gotd_client, entry);
399 imsg_clear(&client->iev.ibuf);
400 event_del(&client->iev.ev);
401 evtimer_del(&client->tmo);
403 if (client->delta_cache_fd != -1)
404 close(client->delta_cache_fd);
405 if (client->packfile_path) {
406 if (unlink(client->packfile_path) == -1 && errno != ENOENT)
407 log_warn("unlink %s: ", client->packfile_path);
408 free(client->packfile_path);
410 if (client->packidx_path) {
411 if (unlink(client->packidx_path) == -1 && errno != ENOENT)
412 log_warn("unlink %s: ", client->packidx_path);
413 free(client->packidx_path);
415 free(client->capabilities);
421 disconnect_on_error(struct gotd_client *client, const struct got_error *err)
425 log_warnx("uid %d: %s", client->euid, err->msg);
426 if (err->code != GOT_ERR_EOF) {
427 imsg_init(&ibuf, client->fd);
428 gotd_imsg_send_error(&ibuf, 0, PROC_GOTD, err);
434 static const struct got_error *
435 send_repo_info(struct gotd_imsgev *iev, struct gotd_repo *repo)
437 const struct got_error *err = NULL;
438 struct gotd_imsg_info_repo irepo;
440 memset(&irepo, 0, sizeof(irepo));
442 if (strlcpy(irepo.repo_name, repo->name, sizeof(irepo.repo_name))
443 >= sizeof(irepo.repo_name))
444 return got_error_msg(GOT_ERR_NO_SPACE, "repo name too long");
445 if (strlcpy(irepo.repo_path, repo->path, sizeof(irepo.repo_path))
446 >= sizeof(irepo.repo_path))
447 return got_error_msg(GOT_ERR_NO_SPACE, "repo path too long");
449 if (gotd_imsg_compose_event(iev, GOTD_IMSG_INFO_REPO, PROC_GOTD, -1,
450 &irepo, sizeof(irepo)) == -1) {
451 err = got_error_from_errno("imsg compose INFO_REPO");
459 static const struct got_error *
460 send_capability(struct gotd_client_capability *capa, struct gotd_imsgev* iev)
462 const struct got_error *err = NULL;
463 struct gotd_imsg_capability icapa;
467 memset(&icapa, 0, sizeof(icapa));
469 icapa.key_len = strlen(capa->key);
470 len = sizeof(icapa) + icapa.key_len;
472 icapa.value_len = strlen(capa->value);
473 len += icapa.value_len;
476 wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_CAPABILITY, 0, 0, len);
478 err = got_error_from_errno("imsg_create CAPABILITY");
482 if (imsg_add(wbuf, &icapa, sizeof(icapa)) == -1)
483 return got_error_from_errno("imsg_add CAPABILITY");
484 if (imsg_add(wbuf, capa->key, icapa.key_len) == -1)
485 return got_error_from_errno("imsg_add CAPABILITY");
487 if (imsg_add(wbuf, capa->value, icapa.value_len) == -1)
488 return got_error_from_errno("imsg_add CAPABILITY");
492 imsg_close(&iev->ibuf, wbuf);
494 gotd_imsg_event_add(iev);
499 static const struct got_error *
500 send_client_info(struct gotd_imsgev *iev, struct gotd_client *client)
502 const struct got_error *err = NULL;
503 struct gotd_imsg_info_client iclient;
504 struct gotd_child_proc *proc;
507 memset(&iclient, 0, sizeof(iclient));
508 iclient.euid = client->euid;
509 iclient.egid = client->egid;
511 proc = get_client_proc(client);
513 if (strlcpy(iclient.repo_name, proc->repo_path,
514 sizeof(iclient.repo_name)) >= sizeof(iclient.repo_name)) {
515 return got_error_msg(GOT_ERR_NO_SPACE,
516 "repo name too long");
518 if (client_is_writing(client))
519 iclient.is_writing = 1;
522 iclient.state = client->state;
523 iclient.ncapabilities = client->ncapabilities;
525 if (gotd_imsg_compose_event(iev, GOTD_IMSG_INFO_CLIENT, PROC_GOTD, -1,
526 &iclient, sizeof(iclient)) == -1) {
527 err = got_error_from_errno("imsg compose INFO_CLIENT");
532 for (i = 0; i < client->ncapabilities; i++) {
533 struct gotd_client_capability *capa;
534 capa = &client->capabilities[i];
535 err = send_capability(capa, iev);
543 static const struct got_error *
544 send_info(struct gotd_client *client)
546 const struct got_error *err = NULL;
547 struct gotd_imsg_info info;
549 struct gotd_repo *repo;
552 info.verbosity = gotd.verbosity;
553 info.nrepos = gotd.nrepos;
554 info.nclients = client_cnt - 1;
556 if (gotd_imsg_compose_event(&client->iev, GOTD_IMSG_INFO, PROC_GOTD, -1,
557 &info, sizeof(info)) == -1) {
558 err = got_error_from_errno("imsg compose INFO");
563 TAILQ_FOREACH(repo, &gotd.repos, entry) {
564 err = send_repo_info(&client->iev, repo);
569 for (slot = 0; slot < nitems(gotd_clients); slot++) {
570 struct gotd_client *c;
571 STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
572 if (c->id == client->id)
574 err = send_client_info(&client->iev, c);
583 static const struct got_error *
584 stop_gotd(struct gotd_client *client)
587 if (client->euid != 0)
588 return got_error_set_errno(EPERM, "stop");
595 static struct gotd_repo *
596 find_repo_by_name(const char *repo_name)
598 struct gotd_repo *repo;
601 TAILQ_FOREACH(repo, &gotd.repos, entry) {
602 namelen = strlen(repo->name);
603 if (strncmp(repo->name, repo_name, namelen) != 0)
605 if (repo_name[namelen] == '\0' ||
606 strcmp(&repo_name[namelen], ".git") == 0)
613 static const struct got_error *
614 start_client_session(struct gotd_client *client, struct imsg *imsg)
616 const struct got_error *err;
617 struct gotd_imsg_list_refs ireq;
618 struct gotd_repo *repo = NULL;
621 log_debug("list-refs request from uid %d", client->euid);
623 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
624 if (datalen != sizeof(ireq))
625 return got_error(GOT_ERR_PRIVSEP_LEN);
627 memcpy(&ireq, imsg->data, datalen);
629 if (ireq.client_is_reading) {
630 err = ensure_client_is_not_writing(client);
633 repo = find_repo_by_name(ireq.repo_name);
635 return got_error(GOT_ERR_NOT_GIT_REPO);
636 err = start_auth_child(client, GOTD_AUTH_READ, repo,
637 gotd.argv0, gotd.confpath, gotd.daemonize,
642 err = ensure_client_is_not_reading(client);
645 repo = find_repo_by_name(ireq.repo_name);
647 return got_error(GOT_ERR_NOT_GIT_REPO);
648 err = start_auth_child(client,
649 GOTD_AUTH_READ | GOTD_AUTH_WRITE,
650 repo, gotd.argv0, gotd.confpath, gotd.daemonize,
656 /* Flow continues upon authentication successs/failure or timeout. */
660 static const struct got_error *
661 forward_want(struct gotd_client *client, struct imsg *imsg)
663 struct gotd_imsg_want ireq;
664 struct gotd_imsg_want iwant;
667 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
668 if (datalen != sizeof(ireq))
669 return got_error(GOT_ERR_PRIVSEP_LEN);
671 memcpy(&ireq, imsg->data, datalen);
673 memset(&iwant, 0, sizeof(iwant));
674 memcpy(iwant.object_id, ireq.object_id, SHA1_DIGEST_LENGTH);
675 iwant.client_id = client->id;
677 if (gotd_imsg_compose_event(&client->repo_read->iev, GOTD_IMSG_WANT,
678 PROC_GOTD, -1, &iwant, sizeof(iwant)) == -1)
679 return got_error_from_errno("imsg compose WANT");
684 static const struct got_error *
685 forward_ref_update(struct gotd_client *client, struct imsg *imsg)
687 const struct got_error *err = NULL;
688 struct gotd_imsg_ref_update ireq;
689 struct gotd_imsg_ref_update *iref = NULL;
692 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
693 if (datalen < sizeof(ireq))
694 return got_error(GOT_ERR_PRIVSEP_LEN);
695 memcpy(&ireq, imsg->data, sizeof(ireq));
696 if (datalen != sizeof(ireq) + ireq.name_len)
697 return got_error(GOT_ERR_PRIVSEP_LEN);
699 iref = malloc(datalen);
701 return got_error_from_errno("malloc");
702 memcpy(iref, imsg->data, datalen);
704 iref->client_id = client->id;
705 if (gotd_imsg_compose_event(&client->repo_write->iev,
706 GOTD_IMSG_REF_UPDATE, PROC_GOTD, -1, iref, datalen) == -1)
707 err = got_error_from_errno("imsg compose REF_UPDATE");
712 static const struct got_error *
713 forward_have(struct gotd_client *client, struct imsg *imsg)
715 struct gotd_imsg_have ireq;
716 struct gotd_imsg_have ihave;
719 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
720 if (datalen != sizeof(ireq))
721 return got_error(GOT_ERR_PRIVSEP_LEN);
723 memcpy(&ireq, imsg->data, datalen);
725 memset(&ihave, 0, sizeof(ihave));
726 memcpy(ihave.object_id, ireq.object_id, SHA1_DIGEST_LENGTH);
727 ihave.client_id = client->id;
729 if (gotd_imsg_compose_event(&client->repo_read->iev, GOTD_IMSG_HAVE,
730 PROC_GOTD, -1, &ihave, sizeof(ihave)) == -1)
731 return got_error_from_errno("imsg compose HAVE");
737 client_has_capability(struct gotd_client *client, const char *capastr)
739 struct gotd_client_capability *capa;
742 if (client->ncapabilities == 0)
745 for (i = 0; i < client->ncapabilities; i++) {
746 capa = &client->capabilities[i];
747 if (strcmp(capa->key, capastr) == 0)
754 static const struct got_error *
755 recv_capabilities(struct gotd_client *client, struct imsg *imsg)
757 struct gotd_imsg_capabilities icapas;
760 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
761 if (datalen != sizeof(icapas))
762 return got_error(GOT_ERR_PRIVSEP_LEN);
763 memcpy(&icapas, imsg->data, sizeof(icapas));
765 client->ncapa_alloc = icapas.ncapabilities;
766 client->capabilities = calloc(client->ncapa_alloc,
767 sizeof(*client->capabilities));
768 if (client->capabilities == NULL) {
769 client->ncapa_alloc = 0;
770 return got_error_from_errno("calloc");
773 log_debug("expecting %zu capabilities from uid %d",
774 client->ncapa_alloc, client->euid);
778 static const struct got_error *
779 recv_capability(struct gotd_client *client, struct imsg *imsg)
781 struct gotd_imsg_capability icapa;
782 struct gotd_client_capability *capa;
784 char *key, *value = NULL;
786 if (client->capabilities == NULL ||
787 client->ncapabilities >= client->ncapa_alloc) {
788 return got_error_msg(GOT_ERR_BAD_REQUEST,
789 "unexpected capability received");
792 memset(&icapa, 0, sizeof(icapa));
794 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
795 if (datalen < sizeof(icapa))
796 return got_error(GOT_ERR_PRIVSEP_LEN);
797 memcpy(&icapa, imsg->data, sizeof(icapa));
799 if (datalen != sizeof(icapa) + icapa.key_len + icapa.value_len)
800 return got_error(GOT_ERR_PRIVSEP_LEN);
802 key = malloc(icapa.key_len + 1);
804 return got_error_from_errno("malloc");
805 if (icapa.value_len > 0) {
806 value = malloc(icapa.value_len + 1);
809 return got_error_from_errno("malloc");
813 memcpy(key, imsg->data + sizeof(icapa), icapa.key_len);
814 key[icapa.key_len] = '\0';
816 memcpy(value, imsg->data + sizeof(icapa) + icapa.key_len,
818 value[icapa.value_len] = '\0';
821 capa = &client->capabilities[client->ncapabilities++];
826 log_debug("uid %d: capability %s=%s", client->euid, key, value);
828 log_debug("uid %d: capability %s", client->euid, key);
833 static const struct got_error *
834 send_packfile(struct gotd_client *client)
836 const struct got_error *err = NULL;
837 struct gotd_imsg_send_packfile ipack;
838 struct gotd_imsg_packfile_pipe ipipe;
841 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe) == -1)
842 return got_error_from_errno("socketpair");
844 memset(&ipack, 0, sizeof(ipack));
845 memset(&ipipe, 0, sizeof(ipipe));
847 ipack.client_id = client->id;
848 if (client_has_capability(client, GOT_CAPA_SIDE_BAND_64K))
849 ipack.report_progress = 1;
851 client->delta_cache_fd = got_opentempfd();
852 if (client->delta_cache_fd == -1)
853 return got_error_from_errno("got_opentempfd");
855 if (gotd_imsg_compose_event(&client->repo_read->iev,
856 GOTD_IMSG_SEND_PACKFILE, PROC_GOTD, client->delta_cache_fd,
857 &ipack, sizeof(ipack)) == -1) {
858 err = got_error_from_errno("imsg compose SEND_PACKFILE");
864 ipipe.client_id = client->id;
866 /* Send pack pipe end 0 to repo_read. */
867 if (gotd_imsg_compose_event(&client->repo_read->iev,
868 GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[0],
869 &ipipe, sizeof(ipipe)) == -1) {
870 err = got_error_from_errno("imsg compose PACKFILE_PIPE");
875 /* Send pack pipe end 1 to gotsh(1) (expects just an fd, no data). */
876 if (gotd_imsg_compose_event(&client->iev,
877 GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[1], NULL, 0) == -1)
878 err = got_error_from_errno("imsg compose PACKFILE_PIPE");
883 static const struct got_error *
884 recv_packfile(struct gotd_client *client)
886 const struct got_error *err = NULL;
887 struct gotd_imsg_recv_packfile ipack;
888 struct gotd_imsg_packfile_pipe ipipe;
889 struct gotd_imsg_packidx_file ifile;
890 char *basepath = NULL, *pack_path = NULL, *idx_path = NULL;
891 int packfd = -1, idxfd = -1;
892 int pipe[2] = { -1, -1 };
894 if (client->packfile_path) {
895 return got_error_fmt(GOT_ERR_PRIVSEP_MSG,
896 "uid %d already has a pack file", client->euid);
899 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe) == -1)
900 return got_error_from_errno("socketpair");
902 memset(&ipipe, 0, sizeof(ipipe));
903 ipipe.client_id = client->id;
905 if (gotd_imsg_compose_event(&client->repo_write->iev,
906 GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[0],
907 &ipipe, sizeof(ipipe)) == -1) {
908 err = got_error_from_errno("imsg compose PACKFILE_PIPE");
914 if (gotd_imsg_compose_event(&client->repo_write->iev,
915 GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[1],
916 &ipipe, sizeof(ipipe)) == -1)
917 err = got_error_from_errno("imsg compose PACKFILE_PIPE");
920 if (asprintf(&basepath, "%s/%s/receiving-from-uid-%d.pack",
921 client->repo_write->repo_path, GOT_OBJECTS_PACK_DIR,
922 client->euid) == -1) {
923 err = got_error_from_errno("asprintf");
927 err = got_opentemp_named_fd(&pack_path, &packfd, basepath, "");
932 if (asprintf(&basepath, "%s/%s/receiving-from-uid-%d.idx",
933 client->repo_write->repo_path, GOT_OBJECTS_PACK_DIR,
934 client->euid) == -1) {
935 err = got_error_from_errno("asprintf");
939 err = got_opentemp_named_fd(&idx_path, &idxfd, basepath, "");
943 memset(&ifile, 0, sizeof(ifile));
944 ifile.client_id = client->id;
945 if (gotd_imsg_compose_event(&client->repo_write->iev,
946 GOTD_IMSG_PACKIDX_FILE, PROC_GOTD, idxfd,
947 &ifile, sizeof(ifile)) == -1) {
948 err = got_error_from_errno("imsg compose PACKIDX_FILE");
954 memset(&ipack, 0, sizeof(ipack));
955 ipack.client_id = client->id;
956 if (client_has_capability(client, GOT_CAPA_REPORT_STATUS))
957 ipack.report_status = 1;
959 if (gotd_imsg_compose_event(&client->repo_write->iev,
960 GOTD_IMSG_RECV_PACKFILE, PROC_GOTD, packfd,
961 &ipack, sizeof(ipack)) == -1) {
962 err = got_error_from_errno("imsg compose RECV_PACKFILE");
970 if (pipe[0] != -1 && close(pipe[0]) == -1 && err == NULL)
971 err = got_error_from_errno("close");
972 if (pipe[1] != -1 && close(pipe[1]) == -1 && err == NULL)
973 err = got_error_from_errno("close");
974 if (packfd != -1 && close(packfd) == -1 && err == NULL)
975 err = got_error_from_errno("close");
976 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
977 err = got_error_from_errno("close");
982 client->packfile_path = pack_path;
983 client->packidx_path = idx_path;
989 gotd_request(int fd, short events, void *arg)
991 struct gotd_imsgev *iev = arg;
992 struct imsgbuf *ibuf = &iev->ibuf;
993 struct gotd_client *client = iev->handler_arg;
994 const struct got_error *err = NULL;
998 if (events & EV_WRITE) {
999 while (ibuf->w.queued) {
1000 n = msgbuf_write(&ibuf->w);
1001 if (n == -1 && errno == EPIPE) {
1003 * The client has closed its socket.
1004 * This can happen when Git clients are
1005 * done sending pack file data.
1007 msgbuf_clear(&ibuf->w);
1009 } else if (n == -1 && errno != EAGAIN) {
1010 err = got_error_from_errno("imsg_flush");
1011 disconnect_on_error(client, err);
1015 /* Connection closed. */
1016 err = got_error(GOT_ERR_EOF);
1017 disconnect_on_error(client, err);
1022 /* Disconnect gotctl(8) now that messages have been sent. */
1023 if (!client_is_reading(client) && !client_is_writing(client)) {
1029 if ((events & EV_READ) == 0)
1032 memset(&imsg, 0, sizeof(imsg));
1034 while (err == NULL) {
1035 err = gotd_imsg_recv(&imsg, ibuf, 0);
1037 if (err->code == GOT_ERR_PRIVSEP_READ)
1042 evtimer_del(&client->tmo);
1044 switch (imsg.hdr.type) {
1045 case GOTD_IMSG_INFO:
1046 err = send_info(client);
1048 case GOTD_IMSG_STOP:
1049 err = stop_gotd(client);
1051 case GOTD_IMSG_LIST_REFS:
1052 if (client->state != GOTD_STATE_EXPECT_LIST_REFS) {
1053 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1054 "unexpected list-refs request received");
1057 err = start_client_session(client, &imsg);
1061 case GOTD_IMSG_CAPABILITIES:
1062 if (client->state != GOTD_STATE_EXPECT_CAPABILITIES) {
1063 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1064 "unexpected capabilities received");
1067 log_debug("receiving capabilities from uid %d",
1069 err = recv_capabilities(client, &imsg);
1071 case GOTD_IMSG_CAPABILITY:
1072 if (client->state != GOTD_STATE_EXPECT_CAPABILITIES) {
1073 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1074 "unexpected capability received");
1077 err = recv_capability(client, &imsg);
1078 if (err || client->ncapabilities < client->ncapa_alloc)
1080 if (client_is_reading(client)) {
1081 client->state = GOTD_STATE_EXPECT_WANT;
1082 log_debug("uid %d: expecting want-lines",
1084 } else if (client_is_writing(client)) {
1085 client->state = GOTD_STATE_EXPECT_REF_UPDATE;
1086 log_debug("uid %d: expecting ref-update-lines",
1089 fatalx("client %d is both reading and writing",
1092 case GOTD_IMSG_WANT:
1093 if (client->state != GOTD_STATE_EXPECT_WANT) {
1094 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1095 "unexpected want-line received");
1098 log_debug("received want-line from uid %d",
1100 err = ensure_client_is_reading(client);
1103 err = forward_want(client, &imsg);
1105 case GOTD_IMSG_REF_UPDATE:
1106 if (client->state != GOTD_STATE_EXPECT_REF_UPDATE) {
1107 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1108 "unexpected ref-update-line received");
1111 log_debug("received ref-update-line from uid %d",
1113 err = ensure_client_is_writing(client);
1116 err = forward_ref_update(client, &imsg);
1119 client->state = GOTD_STATE_EXPECT_MORE_REF_UPDATES;
1121 case GOTD_IMSG_HAVE:
1122 if (client->state != GOTD_STATE_EXPECT_HAVE) {
1123 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1124 "unexpected have-line received");
1127 log_debug("received have-line from uid %d",
1129 err = ensure_client_is_reading(client);
1132 err = forward_have(client, &imsg);
1136 case GOTD_IMSG_FLUSH:
1137 if (client->state == GOTD_STATE_EXPECT_WANT ||
1138 client->state == GOTD_STATE_EXPECT_HAVE) {
1139 err = ensure_client_is_reading(client);
1142 } else if (client->state ==
1143 GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1144 err = ensure_client_is_writing(client);
1148 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1149 "unexpected flush-pkt received");
1152 log_debug("received flush-pkt from uid %d",
1154 if (client->state == GOTD_STATE_EXPECT_WANT) {
1155 client->state = GOTD_STATE_EXPECT_HAVE;
1156 log_debug("uid %d: expecting have-lines",
1158 } else if (client->state == GOTD_STATE_EXPECT_HAVE) {
1159 client->state = GOTD_STATE_EXPECT_DONE;
1160 log_debug("uid %d: expecting 'done'",
1162 } else if (client->state ==
1163 GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1164 client->state = GOTD_STATE_EXPECT_PACKFILE;
1165 log_debug("uid %d: expecting packfile",
1167 err = recv_packfile(client);
1169 /* should not happen, see above */
1170 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1171 "unexpected client state");
1175 case GOTD_IMSG_DONE:
1176 if (client->state != GOTD_STATE_EXPECT_HAVE &&
1177 client->state != GOTD_STATE_EXPECT_DONE) {
1178 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1179 "unexpected flush-pkt received");
1182 log_debug("received 'done' from uid %d", client->euid);
1183 err = ensure_client_is_reading(client);
1186 client->state = GOTD_STATE_DONE;
1187 err = send_packfile(client);
1190 err = got_error(GOT_ERR_PRIVSEP_MSG);
1198 if (err->code != GOT_ERR_EOF ||
1199 client->state != GOTD_STATE_EXPECT_PACKFILE)
1200 disconnect_on_error(client, err);
1202 gotd_imsg_event_add(&client->iev);
1203 if (client->state == GOTD_STATE_EXPECT_LIST_REFS)
1204 evtimer_add(&client->tmo, &auth_timeout);
1206 evtimer_add(&client->tmo, &timeout);
1211 gotd_request_timeout(int fd, short events, void *arg)
1213 struct gotd_client *client = arg;
1215 log_debug("disconnecting uid %d due to timeout", client->euid);
1219 static const struct got_error *
1220 recv_connect(uint32_t *client_id, struct imsg *imsg)
1222 const struct got_error *err = NULL;
1223 struct gotd_imsg_connect iconnect;
1226 struct gotd_client *client = NULL;
1232 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1233 if (datalen != sizeof(iconnect))
1234 return got_error(GOT_ERR_PRIVSEP_LEN);
1235 memcpy(&iconnect, imsg->data, sizeof(iconnect));
1239 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1243 if (find_client(iconnect.client_id)) {
1244 err = got_error_msg(GOT_ERR_CLIENT_ID, "duplicate client ID");
1248 if (getpeereid(s, &euid, &egid) == -1) {
1249 err = got_error_from_errno("getpeerid");
1253 client = calloc(1, sizeof(*client));
1254 if (client == NULL) {
1255 err = got_error_from_errno("calloc");
1259 *client_id = iconnect.client_id;
1261 client->state = GOTD_STATE_EXPECT_LIST_REFS;
1262 client->id = iconnect.client_id;
1265 client->delta_cache_fd = -1;
1266 client->euid = euid;
1267 client->egid = egid;
1268 client->nref_updates = -1;
1270 imsg_init(&client->iev.ibuf, client->fd);
1271 client->iev.handler = gotd_request;
1272 client->iev.events = EV_READ;
1273 client->iev.handler_arg = client;
1275 event_set(&client->iev.ev, client->fd, EV_READ, gotd_request,
1277 gotd_imsg_event_add(&client->iev);
1279 evtimer_set(&client->tmo, gotd_request_timeout, client);
1282 log_debug("%s: new client uid %d connected on fd %d", __func__,
1283 client->euid, client->fd);
1286 struct gotd_child_proc *listen_proc = &gotd.listen_proc;
1287 struct gotd_imsg_disconnect idisconnect;
1289 idisconnect.client_id = client->id;
1290 if (gotd_imsg_compose_event(&listen_proc->iev,
1291 GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
1292 &idisconnect, sizeof(idisconnect)) == -1)
1293 log_warn("imsg compose DISCONNECT");
1302 static const char *gotd_proc_names[PROC_MAX] = {
1311 kill_proc(struct gotd_child_proc *proc, int fatal)
1314 log_warnx("sending SIGKILL to PID %d", proc->pid);
1315 kill(proc->pid, SIGKILL);
1317 kill(proc->pid, SIGTERM);
1323 struct gotd_child_proc *proc;
1326 for (slot = 0; slot < nitems(gotd_clients); slot++) {
1327 struct gotd_client *c, *tmp;
1329 STAILQ_FOREACH_SAFE(c, &gotd_clients[slot], entry, tmp)
1333 proc = &gotd.listen_proc;
1334 msgbuf_clear(&proc->iev.ibuf.w);
1335 close(proc->iev.ibuf.fd);
1337 wait_for_child(proc->pid);
1339 log_info("terminating");
1344 gotd_sighdlr(int sig, short event, void *arg)
1347 * Normal signal handler rules don't apply because libevent
1353 log_info("%s: ignoring SIGHUP", __func__);
1356 log_info("%s: ignoring SIGUSR1", __func__);
1361 log_warnx("gotd terminating");
1365 fatalx("unexpected signal");
1369 static const struct got_error *
1370 ensure_proc_is_reading(struct gotd_client *client,
1371 struct gotd_child_proc *proc)
1373 if (!client_is_reading(client)) {
1375 return got_error_fmt(GOT_ERR_BAD_PACKET,
1376 "PID %d handled a read-request for uid %d but this "
1377 "user is not reading from a repository", proc->pid,
1384 static const struct got_error *
1385 ensure_proc_is_writing(struct gotd_client *client,
1386 struct gotd_child_proc *proc)
1388 if (!client_is_writing(client)) {
1390 return got_error_fmt(GOT_ERR_BAD_PACKET,
1391 "PID %d handled a write-request for uid %d but this "
1392 "user is not writing to a repository", proc->pid,
1400 verify_imsg_src(struct gotd_client *client, struct gotd_child_proc *proc,
1403 const struct got_error *err;
1404 struct gotd_child_proc *client_proc;
1407 if (proc->type == PROC_REPO_READ || proc->type == PROC_REPO_WRITE) {
1408 client_proc = get_client_proc(client);
1409 if (client_proc == NULL)
1410 fatalx("no process found for uid %d", client->euid);
1411 if (proc->pid != client_proc->pid) {
1413 log_warnx("received message from PID %d for uid %d, "
1414 "while PID %d is the process serving this user",
1415 proc->pid, client->euid, client_proc->pid);
1420 switch (imsg->hdr.type) {
1421 case GOTD_IMSG_ERROR:
1424 case GOTD_IMSG_CONNECT:
1425 if (proc->type != PROC_LISTEN) {
1426 err = got_error_fmt(GOT_ERR_BAD_PACKET,
1427 "new connection for uid %d from PID %d "
1428 "which is not the listen process",
1429 proc->pid, client->euid);
1433 case GOTD_IMSG_ACCESS_GRANTED:
1434 if (proc->type != PROC_AUTH) {
1435 err = got_error_fmt(GOT_ERR_BAD_PACKET,
1436 "authentication of uid %d from PID %d "
1437 "which is not the auth process",
1438 proc->pid, client->euid);
1442 case GOTD_IMSG_REPO_CHILD_READY:
1443 if (proc->type != PROC_REPO_READ &&
1444 proc->type != PROC_REPO_WRITE) {
1445 err = got_error_fmt(GOT_ERR_BAD_PACKET,
1446 "unexpected \"ready\" signal from PID %d",
1451 case GOTD_IMSG_PACKFILE_DONE:
1452 err = ensure_proc_is_reading(client, proc);
1454 log_warnx("uid %d: %s", client->euid, err->msg);
1458 case GOTD_IMSG_PACKFILE_INSTALL:
1459 case GOTD_IMSG_REF_UPDATES_START:
1460 case GOTD_IMSG_REF_UPDATE:
1461 err = ensure_proc_is_writing(client, proc);
1463 log_warnx("uid %d: %s", client->euid, err->msg);
1468 log_debug("%s: unexpected imsg %d", __func__, imsg->hdr.type);
1475 static const struct got_error *
1476 list_refs_request(struct gotd_client *client, struct gotd_imsgev *iev)
1478 static const struct got_error *err;
1479 struct gotd_imsg_list_refs_internal ilref;
1482 memset(&ilref, 0, sizeof(ilref));
1483 ilref.client_id = client->id;
1485 fd = dup(client->fd);
1487 return got_error_from_errno("dup");
1489 if (gotd_imsg_compose_event(iev, GOTD_IMSG_LIST_REFS_INTERNAL,
1490 PROC_GOTD, fd, &ilref, sizeof(ilref)) == -1) {
1491 err = got_error_from_errno("imsg compose WANT");
1496 client->state = GOTD_STATE_EXPECT_CAPABILITIES;
1497 log_debug("uid %d: expecting capabilities", client->euid);
1501 static const struct got_error *
1502 recv_packfile_done(uint32_t *client_id, struct imsg *imsg)
1504 struct gotd_imsg_packfile_done idone;
1507 log_debug("packfile-done received");
1509 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1510 if (datalen != sizeof(idone))
1511 return got_error(GOT_ERR_PRIVSEP_LEN);
1512 memcpy(&idone, imsg->data, sizeof(idone));
1514 *client_id = idone.client_id;
1518 static const struct got_error *
1519 recv_packfile_install(uint32_t *client_id, struct imsg *imsg)
1521 struct gotd_imsg_packfile_install inst;
1524 log_debug("packfile-install received");
1526 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1527 if (datalen != sizeof(inst))
1528 return got_error(GOT_ERR_PRIVSEP_LEN);
1529 memcpy(&inst, imsg->data, sizeof(inst));
1531 *client_id = inst.client_id;
1535 static const struct got_error *
1536 recv_ref_updates_start(uint32_t *client_id, struct imsg *imsg)
1538 struct gotd_imsg_ref_updates_start istart;
1541 log_debug("ref-updates-start received");
1543 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1544 if (datalen != sizeof(istart))
1545 return got_error(GOT_ERR_PRIVSEP_LEN);
1546 memcpy(&istart, imsg->data, sizeof(istart));
1548 *client_id = istart.client_id;
1552 static const struct got_error *
1553 recv_ref_update(uint32_t *client_id, struct imsg *imsg)
1555 struct gotd_imsg_ref_update iref;
1558 log_debug("ref-update received");
1560 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1561 if (datalen < sizeof(iref))
1562 return got_error(GOT_ERR_PRIVSEP_LEN);
1563 memcpy(&iref, imsg->data, sizeof(iref));
1565 *client_id = iref.client_id;
1569 static const struct got_error *
1570 send_ref_update_ok(struct gotd_client *client,
1571 struct gotd_imsg_ref_update *iref, const char *refname)
1573 struct gotd_imsg_ref_update_ok iok;
1577 memset(&iok, 0, sizeof(iok));
1578 iok.client_id = client->id;
1579 memcpy(iok.old_id, iref->old_id, SHA1_DIGEST_LENGTH);
1580 memcpy(iok.new_id, iref->new_id, SHA1_DIGEST_LENGTH);
1581 iok.name_len = strlen(refname);
1583 len = sizeof(iok) + iok.name_len;
1584 wbuf = imsg_create(&client->iev.ibuf, GOTD_IMSG_REF_UPDATE_OK,
1585 PROC_GOTD, gotd.pid, len);
1587 return got_error_from_errno("imsg_create REF_UPDATE_OK");
1589 if (imsg_add(wbuf, &iok, sizeof(iok)) == -1)
1590 return got_error_from_errno("imsg_add REF_UPDATE_OK");
1591 if (imsg_add(wbuf, refname, iok.name_len) == -1)
1592 return got_error_from_errno("imsg_add REF_UPDATE_OK");
1595 imsg_close(&client->iev.ibuf, wbuf);
1596 gotd_imsg_event_add(&client->iev);
1601 send_refs_updated(struct gotd_client *client)
1603 if (gotd_imsg_compose_event(&client->iev,
1604 GOTD_IMSG_REFS_UPDATED, PROC_GOTD, -1, NULL, 0) == -1)
1605 log_warn("imsg compose REFS_UPDATED");
1608 static const struct got_error *
1609 send_ref_update_ng(struct gotd_client *client,
1610 struct gotd_imsg_ref_update *iref, const char *refname,
1613 const struct got_error *ng_err;
1614 struct gotd_imsg_ref_update_ng ing;
1618 memset(&ing, 0, sizeof(ing));
1619 ing.client_id = client->id;
1620 memcpy(ing.old_id, iref->old_id, SHA1_DIGEST_LENGTH);
1621 memcpy(ing.new_id, iref->new_id, SHA1_DIGEST_LENGTH);
1622 ing.name_len = strlen(refname);
1624 ng_err = got_error_fmt(GOT_ERR_REF_BUSY, "%s", reason);
1625 ing.reason_len = strlen(ng_err->msg);
1627 len = sizeof(ing) + ing.name_len + ing.reason_len;
1628 wbuf = imsg_create(&client->iev.ibuf, GOTD_IMSG_REF_UPDATE_NG,
1629 PROC_GOTD, gotd.pid, len);
1631 return got_error_from_errno("imsg_create REF_UPDATE_NG");
1633 if (imsg_add(wbuf, &ing, sizeof(ing)) == -1)
1634 return got_error_from_errno("imsg_add REF_UPDATE_NG");
1635 if (imsg_add(wbuf, refname, ing.name_len) == -1)
1636 return got_error_from_errno("imsg_add REF_UPDATE_NG");
1637 if (imsg_add(wbuf, ng_err->msg, ing.reason_len) == -1)
1638 return got_error_from_errno("imsg_add REF_UPDATE_NG");
1641 imsg_close(&client->iev.ibuf, wbuf);
1642 gotd_imsg_event_add(&client->iev);
1646 static const struct got_error *
1647 install_pack(struct gotd_client *client, const char *repo_path,
1650 const struct got_error *err = NULL;
1651 struct gotd_imsg_packfile_install inst;
1652 char hex[SHA1_DIGEST_STRING_LENGTH];
1654 char *packfile_path = NULL, *packidx_path = NULL;
1656 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1657 if (datalen != sizeof(inst))
1658 return got_error(GOT_ERR_PRIVSEP_LEN);
1659 memcpy(&inst, imsg->data, sizeof(inst));
1661 if (client->packfile_path == NULL)
1662 return got_error_msg(GOT_ERR_BAD_REQUEST,
1663 "client has no pack file");
1664 if (client->packidx_path == NULL)
1665 return got_error_msg(GOT_ERR_BAD_REQUEST,
1666 "client has no pack file index");
1668 if (got_sha1_digest_to_str(inst.pack_sha1, hex, sizeof(hex)) == NULL)
1669 return got_error_msg(GOT_ERR_NO_SPACE,
1670 "could not convert pack file SHA1 to hex");
1672 if (asprintf(&packfile_path, "/%s/%s/pack-%s.pack",
1673 repo_path, GOT_OBJECTS_PACK_DIR, hex) == -1) {
1674 err = got_error_from_errno("asprintf");
1678 if (asprintf(&packidx_path, "/%s/%s/pack-%s.idx",
1679 repo_path, GOT_OBJECTS_PACK_DIR, hex) == -1) {
1680 err = got_error_from_errno("asprintf");
1684 if (rename(client->packfile_path, packfile_path) == -1) {
1685 err = got_error_from_errno3("rename", client->packfile_path,
1690 free(client->packfile_path);
1691 client->packfile_path = NULL;
1693 if (rename(client->packidx_path, packidx_path) == -1) {
1694 err = got_error_from_errno3("rename", client->packidx_path,
1699 free(client->packidx_path);
1700 client->packidx_path = NULL;
1702 free(packfile_path);
1707 static const struct got_error *
1708 begin_ref_updates(struct gotd_client *client, struct imsg *imsg)
1710 struct gotd_imsg_ref_updates_start istart;
1713 if (client->nref_updates != -1)
1714 return got_error(GOT_ERR_PRIVSEP_MSG);
1716 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1717 if (datalen != sizeof(istart))
1718 return got_error(GOT_ERR_PRIVSEP_LEN);
1719 memcpy(&istart, imsg->data, sizeof(istart));
1721 if (istart.nref_updates <= 0)
1722 return got_error(GOT_ERR_PRIVSEP_MSG);
1724 client->nref_updates = istart.nref_updates;
1728 static const struct got_error *
1729 update_ref(struct gotd_client *client, const char *repo_path,
1732 const struct got_error *err = NULL;
1733 struct got_repository *repo = NULL;
1734 struct got_reference *ref = NULL;
1735 struct gotd_imsg_ref_update iref;
1736 struct got_object_id old_id, new_id;
1737 struct got_object_id *id = NULL;
1738 struct got_object *obj = NULL;
1739 char *refname = NULL;
1743 log_debug("update-ref from uid %d", client->euid);
1745 if (client->nref_updates <= 0)
1746 return got_error(GOT_ERR_PRIVSEP_MSG);
1748 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1749 if (datalen < sizeof(iref))
1750 return got_error(GOT_ERR_PRIVSEP_LEN);
1751 memcpy(&iref, imsg->data, sizeof(iref));
1752 if (datalen != sizeof(iref) + iref.name_len)
1753 return got_error(GOT_ERR_PRIVSEP_LEN);
1754 refname = malloc(iref.name_len + 1);
1755 if (refname == NULL)
1756 return got_error_from_errno("malloc");
1757 memcpy(refname, imsg->data + sizeof(iref), iref.name_len);
1758 refname[iref.name_len] = '\0';
1760 log_debug("updating ref %s for uid %d", refname, client->euid);
1762 err = got_repo_open(&repo, repo_path, NULL, NULL);
1766 memcpy(old_id.sha1, iref.old_id, SHA1_DIGEST_LENGTH);
1767 memcpy(new_id.sha1, iref.new_id, SHA1_DIGEST_LENGTH);
1768 err = got_object_open(&obj, repo, &new_id);
1772 if (iref.ref_is_new) {
1773 err = got_ref_open(&ref, repo, refname, 0);
1775 if (err->code != GOT_ERR_NOT_REF)
1777 err = got_ref_alloc(&ref, refname, &new_id);
1780 err = got_ref_write(ref, repo); /* will lock/unlock */
1784 err = got_error_fmt(GOT_ERR_REF_BUSY,
1785 "%s has been created by someone else "
1786 "while transaction was in progress",
1787 got_ref_get_name(ref));
1791 err = got_ref_open(&ref, repo, refname, 1 /* lock */);
1796 err = got_ref_resolve(&id, repo, ref);
1800 if (got_object_id_cmp(id, &old_id) != 0) {
1801 err = got_error_fmt(GOT_ERR_REF_BUSY,
1802 "%s has been modified by someone else "
1803 "while transaction was in progress",
1804 got_ref_get_name(ref));
1808 err = got_ref_change_ref(ref, &new_id);
1812 err = got_ref_write(ref, repo);
1821 if (err->code == GOT_ERR_LOCKFILE_TIMEOUT) {
1822 err = got_error_fmt(GOT_ERR_LOCKFILE_TIMEOUT,
1823 "could not acquire exclusive file lock for %s",
1826 send_ref_update_ng(client, &iref, refname, err->msg);
1828 send_ref_update_ok(client, &iref, refname);
1830 if (client->nref_updates > 0) {
1831 client->nref_updates--;
1832 if (client->nref_updates == 0)
1833 send_refs_updated(client);
1837 const struct got_error *unlock_err;
1838 unlock_err = got_ref_unlock(ref);
1839 if (unlock_err && err == NULL)
1845 got_object_close(obj);
1847 got_repo_close(repo);
1854 gotd_dispatch_listener(int fd, short event, void *arg)
1856 struct gotd_imsgev *iev = arg;
1857 struct imsgbuf *ibuf = &iev->ibuf;
1858 struct gotd_child_proc *proc = &gotd.listen_proc;
1863 if (proc->iev.ibuf.fd != fd)
1864 fatalx("%s: unexpected fd %d", __func__, fd);
1866 if (event & EV_READ) {
1867 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1868 fatal("imsg_read error");
1870 /* Connection closed. */
1876 if (event & EV_WRITE) {
1877 n = msgbuf_write(&ibuf->w);
1878 if (n == -1 && errno != EAGAIN)
1879 fatal("msgbuf_write");
1881 /* Connection closed. */
1888 const struct got_error *err = NULL;
1889 struct gotd_client *client = NULL;
1890 uint32_t client_id = 0;
1891 int do_disconnect = 0;
1893 if ((n = imsg_get(ibuf, &imsg)) == -1)
1894 fatal("%s: imsg_get error", __func__);
1895 if (n == 0) /* No more messages. */
1898 switch (imsg.hdr.type) {
1899 case GOTD_IMSG_ERROR:
1901 err = gotd_imsg_recv_error(&client_id, &imsg);
1903 case GOTD_IMSG_CONNECT:
1904 err = recv_connect(&client_id, &imsg);
1907 log_debug("unexpected imsg %d", imsg.hdr.type);
1911 client = find_client(client_id);
1912 if (client == NULL) {
1913 log_warnx("%s: client not found", __func__);
1919 log_warnx("uid %d: %s", client->euid, err->msg);
1921 if (do_disconnect) {
1923 disconnect_on_error(client, err);
1932 gotd_imsg_event_add(iev);
1934 /* This pipe is dead. Remove its event handler */
1935 event_del(&iev->ev);
1936 event_loopexit(NULL);
1941 gotd_dispatch_auth_child(int fd, short event, void *arg)
1943 const struct got_error *err = NULL;
1944 struct gotd_imsgev *iev = arg;
1945 struct imsgbuf *ibuf = &iev->ibuf;
1946 struct gotd_client *client;
1947 struct gotd_repo *repo = NULL;
1951 uint32_t client_id = 0;
1952 int do_disconnect = 0;
1953 enum gotd_procid proc_type;
1955 client = find_client_by_proc_fd(fd);
1957 fatalx("cannot find client for fd %d", fd);
1959 if (client->auth == NULL)
1960 fatalx("cannot find auth child process for fd %d", fd);
1962 if (event & EV_READ) {
1963 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1964 fatal("imsg_read error");
1966 /* Connection closed. */
1972 if (event & EV_WRITE) {
1973 n = msgbuf_write(&ibuf->w);
1974 if (n == -1 && errno != EAGAIN)
1975 fatal("msgbuf_write");
1977 /* Connection closed. */
1983 if (client->auth->iev.ibuf.fd != fd)
1984 fatalx("%s: unexpected fd %d", __func__, fd);
1986 if ((n = imsg_get(ibuf, &imsg)) == -1)
1987 fatal("%s: imsg_get error", __func__);
1988 if (n == 0) /* No more messages. */
1991 evtimer_del(&client->tmo);
1993 switch (imsg.hdr.type) {
1994 case GOTD_IMSG_ERROR:
1996 err = gotd_imsg_recv_error(&client_id, &imsg);
1998 case GOTD_IMSG_ACCESS_GRANTED:
2002 log_debug("unexpected imsg %d", imsg.hdr.type);
2006 if (!verify_imsg_src(client, client->auth, &imsg)) {
2008 log_debug("dropping imsg type %d from PID %d",
2009 imsg.hdr.type, client->auth->pid);
2013 if (do_disconnect) {
2015 disconnect_on_error(client, err);
2021 repo = find_repo_by_name(client->auth->repo_name);
2023 err = got_error(GOT_ERR_NOT_GIT_REPO);
2026 kill_auth_proc(client);
2028 log_info("authenticated uid %d for repository %s\n",
2029 client->euid, repo->name);
2031 if (client->required_auth & GOTD_AUTH_WRITE)
2032 proc_type = PROC_REPO_WRITE;
2034 proc_type = PROC_REPO_READ;
2036 err = start_repo_child(client, proc_type, repo,
2037 gotd.argv0, gotd.confpath, gotd.daemonize,
2041 log_warnx("uid %d: %s", client->euid, err->msg);
2043 /* We might have killed the auth process by now. */
2044 if (client->auth != NULL) {
2046 gotd_imsg_event_add(iev);
2048 /* This pipe is dead. Remove its event handler */
2049 event_del(&iev->ev);
2055 gotd_dispatch_repo_child(int fd, short event, void *arg)
2057 struct gotd_imsgev *iev = arg;
2058 struct imsgbuf *ibuf = &iev->ibuf;
2059 struct gotd_child_proc *proc = NULL;
2060 struct gotd_client *client = NULL;
2065 if (event & EV_READ) {
2066 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
2067 fatal("imsg_read error");
2069 /* Connection closed. */
2075 if (event & EV_WRITE) {
2076 n = msgbuf_write(&ibuf->w);
2077 if (n == -1 && errno != EAGAIN)
2078 fatal("msgbuf_write");
2080 /* Connection closed. */
2086 client = find_client_by_proc_fd(fd);
2088 fatalx("cannot find client for fd %d", fd);
2090 proc = get_client_proc(client);
2092 fatalx("cannot find child process for fd %d", fd);
2095 const struct got_error *err = NULL;
2096 uint32_t client_id = 0;
2097 int do_disconnect = 0;
2098 int do_list_refs = 0, do_ref_updates = 0, do_ref_update = 0;
2099 int do_packfile_install = 0;
2101 if ((n = imsg_get(ibuf, &imsg)) == -1)
2102 fatal("%s: imsg_get error", __func__);
2103 if (n == 0) /* No more messages. */
2106 switch (imsg.hdr.type) {
2107 case GOTD_IMSG_ERROR:
2109 err = gotd_imsg_recv_error(&client_id, &imsg);
2111 case GOTD_IMSG_REPO_CHILD_READY:
2114 case GOTD_IMSG_PACKFILE_DONE:
2116 err = recv_packfile_done(&client_id, &imsg);
2118 case GOTD_IMSG_PACKFILE_INSTALL:
2119 err = recv_packfile_install(&client_id, &imsg);
2121 do_packfile_install = 1;
2123 case GOTD_IMSG_REF_UPDATES_START:
2124 err = recv_ref_updates_start(&client_id, &imsg);
2128 case GOTD_IMSG_REF_UPDATE:
2129 err = recv_ref_update(&client_id, &imsg);
2134 log_debug("unexpected imsg %d", imsg.hdr.type);
2138 if (!verify_imsg_src(client, proc, &imsg)) {
2139 log_debug("dropping imsg type %d from PID %d",
2140 imsg.hdr.type, proc->pid);
2145 log_warnx("uid %d: %s", client->euid, err->msg);
2147 if (do_disconnect) {
2149 disconnect_on_error(client, err);
2154 err = list_refs_request(client, iev);
2155 else if (do_packfile_install)
2156 err = install_pack(client, proc->repo_path,
2158 else if (do_ref_updates)
2159 err = begin_ref_updates(client, &imsg);
2160 else if (do_ref_update)
2161 err = update_ref(client, proc->repo_path,
2164 log_warnx("uid %d: %s", client->euid, err->msg);
2170 gotd_imsg_event_add(iev);
2172 /* This pipe is dead. Remove its event handler */
2173 event_del(&iev->ev);
2174 event_loopexit(NULL);
2179 start_child(enum gotd_procid proc_id, const char *repo_path,
2180 char *argv0, const char *confpath, int fd, int daemonize, int verbosity)
2186 switch (pid = fork()) {
2188 fatal("cannot fork");
2196 if (fd != GOTD_FILENO_MSG_PIPE) {
2197 if (dup2(fd, GOTD_FILENO_MSG_PIPE) == -1)
2198 fatal("cannot setup imsg fd");
2199 } else if (fcntl(fd, F_SETFD, 0) == -1)
2200 fatal("cannot setup imsg fd");
2202 argv[argc++] = argv0;
2205 argv[argc++] = (char *)"-L";
2208 argv[argc++] = (char *)"-A";
2210 case PROC_REPO_READ:
2211 argv[argc++] = (char *)"-R";
2213 case PROC_REPO_WRITE:
2214 argv[argc++] = (char *)"-W";
2217 fatalx("invalid process id %d", proc_id);
2220 argv[argc++] = (char *)"-f";
2221 argv[argc++] = (char *)confpath;
2224 argv[argc++] = (char *)"-P";
2225 argv[argc++] = (char *)repo_path;
2229 argv[argc++] = (char *)"-d";
2231 argv[argc++] = (char *)"-v";
2233 argv[argc++] = (char *)"-v";
2234 argv[argc++] = NULL;
2236 execvp(argv0, argv);
2241 start_listener(char *argv0, const char *confpath, int daemonize, int verbosity)
2243 struct gotd_child_proc *proc = &gotd.listen_proc;
2245 proc->type = PROC_LISTEN;
2247 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
2248 PF_UNSPEC, proc->pipe) == -1)
2249 fatal("socketpair");
2251 proc->pid = start_child(proc->type, NULL, argv0, confpath,
2252 proc->pipe[1], daemonize, verbosity);
2253 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
2254 proc->iev.handler = gotd_dispatch_listener;
2255 proc->iev.events = EV_READ;
2256 proc->iev.handler_arg = NULL;
2259 static const struct got_error *
2260 start_repo_child(struct gotd_client *client, enum gotd_procid proc_type,
2261 struct gotd_repo *repo, char *argv0, const char *confpath,
2262 int daemonize, int verbosity)
2264 struct gotd_child_proc *proc;
2266 if (proc_type != PROC_REPO_READ && proc_type != PROC_REPO_WRITE)
2267 return got_error_msg(GOT_ERR_NOT_IMPL, "bad process type");
2269 proc = calloc(1, sizeof(*proc));
2271 return got_error_from_errno("calloc");
2273 proc->type = proc_type;
2274 if (strlcpy(proc->repo_name, repo->name,
2275 sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
2276 fatalx("repository name too long: %s", repo->name);
2277 log_debug("starting %s for repository %s",
2278 proc->type == PROC_REPO_READ ? "reader" : "writer", repo->name);
2279 if (realpath(repo->path, proc->repo_path) == NULL)
2280 fatal("%s", repo->path);
2281 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
2282 PF_UNSPEC, proc->pipe) == -1)
2283 fatal("socketpair");
2284 proc->pid = start_child(proc->type, proc->repo_path, argv0,
2285 confpath, proc->pipe[1], daemonize, verbosity);
2286 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
2287 log_debug("proc %s %s is on fd %d",
2288 gotd_proc_names[proc->type], proc->repo_path,
2290 proc->iev.handler = gotd_dispatch_repo_child;
2291 proc->iev.events = EV_READ;
2292 proc->iev.handler_arg = NULL;
2293 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
2294 gotd_dispatch_repo_child, &proc->iev);
2295 gotd_imsg_event_add(&proc->iev);
2297 if (proc->type == PROC_REPO_READ)
2298 client->repo_read = proc;
2300 client->repo_write = proc;
2305 static const struct got_error *
2306 start_auth_child(struct gotd_client *client, int required_auth,
2307 struct gotd_repo *repo, char *argv0, const char *confpath,
2308 int daemonize, int verbosity)
2310 struct gotd_child_proc *proc;
2311 struct gotd_imsg_auth iauth;
2313 memset(&iauth, 0, sizeof(iauth));
2315 proc = calloc(1, sizeof(*proc));
2317 return got_error_from_errno("calloc");
2319 proc->type = PROC_AUTH;
2320 if (strlcpy(proc->repo_name, repo->name,
2321 sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
2322 fatalx("repository name too long: %s", repo->name);
2323 log_debug("starting auth for uid %d repository %s",
2324 client->euid, repo->name);
2325 if (realpath(repo->path, proc->repo_path) == NULL)
2326 fatal("%s", repo->path);
2327 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
2328 PF_UNSPEC, proc->pipe) == -1)
2329 fatal("socketpair");
2330 proc->pid = start_child(proc->type, proc->repo_path, argv0,
2331 confpath, proc->pipe[1], daemonize, verbosity);
2332 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
2333 log_debug("proc %s %s is on fd %d",
2334 gotd_proc_names[proc->type], proc->repo_path,
2336 proc->iev.handler = gotd_dispatch_auth_child;
2337 proc->iev.events = EV_READ;
2338 proc->iev.handler_arg = NULL;
2339 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
2340 gotd_dispatch_auth_child, &proc->iev);
2341 gotd_imsg_event_add(&proc->iev);
2343 iauth.euid = client->euid;
2344 iauth.egid = client->egid;
2345 iauth.required_auth = required_auth;
2346 iauth.client_id = client->id;
2347 if (gotd_imsg_compose_event(&proc->iev, GOTD_IMSG_AUTHENTICATE,
2348 PROC_GOTD, -1, &iauth, sizeof(iauth)) == -1)
2349 log_warn("imsg compose AUTHENTICATE");
2351 client->auth = proc;
2352 client->required_auth = required_auth;
2357 apply_unveil_repo_readonly(const char *repo_path)
2359 if (unveil(repo_path, "r") == -1)
2360 fatal("unveil %s", repo_path);
2362 if (unveil(NULL, NULL) == -1)
2369 struct gotd_repo *repo;
2371 if (unveil(gotd.argv0, "x") == -1)
2372 fatal("unveil %s", gotd.argv0);
2374 TAILQ_FOREACH(repo, &gotd.repos, entry) {
2375 if (unveil(repo->path, "rwc") == -1)
2376 fatal("unveil %s", repo->path);
2379 if (unveil(GOT_TMPDIR_STR, "rwc") == -1)
2380 fatal("unveil %s", GOT_TMPDIR_STR);
2382 if (unveil(NULL, NULL) == -1)
2387 main(int argc, char **argv)
2389 const struct got_error *error = NULL;
2390 int ch, fd = -1, daemonize = 1, verbosity = 0, noaction = 0;
2391 const char *confpath = GOTD_CONF_PATH;
2392 char *argv0 = argv[0];
2394 gid_t groups[NGROUPS_MAX];
2395 int ngroups = NGROUPS_MAX;
2396 struct passwd *pw = NULL;
2397 struct group *gr = NULL;
2398 char *repo_path = NULL;
2399 enum gotd_procid proc_id = PROC_GOTD;
2400 struct event evsigint, evsigterm, evsighup, evsigusr1;
2401 int *pack_fds = NULL, *temp_fds = NULL;
2403 log_init(1, LOG_DAEMON); /* Log to stderr until daemonized. */
2405 while ((ch = getopt(argc, argv, "Adf:LnP:RvW")) != -1) {
2408 proc_id = PROC_AUTH;
2417 proc_id = PROC_LISTEN;
2423 repo_path = realpath(optarg, NULL);
2424 if (repo_path == NULL)
2425 fatal("realpath '%s'", optarg);
2428 proc_id = PROC_REPO_READ;
2435 proc_id = PROC_REPO_WRITE;
2448 /* Require an absolute path in argv[0] for reliable re-exec. */
2449 if (!got_path_is_absolute(argv0))
2450 fatalx("bad path \"%s\": must be an absolute path", argv0);
2452 if (geteuid() && (proc_id == PROC_GOTD || proc_id == PROC_LISTEN))
2453 fatalx("need root privileges");
2455 log_init(daemonize ? 0 : 1, LOG_DAEMON);
2456 log_setverbose(verbosity);
2458 if (parse_config(confpath, proc_id, &gotd) != 0)
2462 gotd.daemonize = daemonize;
2463 gotd.verbosity = verbosity;
2464 gotd.confpath = confpath;
2466 if (proc_id == PROC_GOTD &&
2467 (gotd.nrepos == 0 || TAILQ_EMPTY(&gotd.repos)))
2468 fatalx("no repository defined in configuration file");
2470 pw = getpwnam(gotd.user_name);
2472 fatalx("user %s not found", gotd.user_name);
2474 if (pw->pw_uid == 0) {
2475 fatalx("cannot run %s as %s: the user running %s "
2476 "must not be the superuser",
2477 getprogname(), pw->pw_name, getprogname());
2480 if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups) == -1)
2481 log_warnx("group membership list truncated");
2483 gr = match_group(groups, ngroups, gotd.unix_group_name);
2485 fatalx("cannot start %s: the user running %s "
2486 "must be a secondary member of group %s",
2487 getprogname(), getprogname(), gotd.unix_group_name);
2489 if (gr->gr_gid == pw->pw_gid) {
2490 fatalx("cannot start %s: the user running %s "
2491 "must be a secondary member of group %s, but "
2492 "%s is the user's primary group",
2493 getprogname(), getprogname(), gotd.unix_group_name,
2494 gotd.unix_group_name);
2497 if (proc_id == PROC_LISTEN &&
2498 !got_path_is_absolute(gotd.unix_socket_path))
2499 fatalx("bad unix socket path \"%s\": must be an absolute path",
2500 gotd.unix_socket_path);
2505 if (proc_id == PROC_GOTD) {
2506 gotd.pid = getpid();
2507 snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
2508 start_listener(argv0, confpath, daemonize, verbosity);
2509 arc4random_buf(&clients_hash_key, sizeof(clients_hash_key));
2510 if (daemonize && daemon(1, 0) == -1)
2512 } else if (proc_id == PROC_LISTEN) {
2513 snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
2515 log_info("socket: %s", gotd.unix_socket_path);
2516 log_info("user: %s", pw->pw_name);
2517 log_info("secondary group: %s", gr->gr_name);
2520 fd = unix_socket_listen(gotd.unix_socket_path, pw->pw_uid,
2523 fatal("cannot listen on unix socket %s",
2524 gotd.unix_socket_path);
2526 if (daemonize && daemon(0, 0) == -1)
2528 } else if (proc_id == PROC_AUTH) {
2529 snprintf(title, sizeof(title), "%s %s",
2530 gotd_proc_names[proc_id], repo_path);
2531 if (daemonize && daemon(0, 0) == -1)
2533 } else if (proc_id == PROC_REPO_READ || proc_id == PROC_REPO_WRITE) {
2534 error = got_repo_pack_fds_open(&pack_fds);
2536 fatalx("cannot open pack tempfiles: %s", error->msg);
2537 error = got_repo_temp_fds_open(&temp_fds);
2539 fatalx("cannot open pack tempfiles: %s", error->msg);
2540 if (repo_path == NULL)
2541 fatalx("repository path not specified");
2542 snprintf(title, sizeof(title), "%s %s",
2543 gotd_proc_names[proc_id], repo_path);
2544 if (daemonize && daemon(0, 0) == -1)
2547 fatal("invalid process id %d", proc_id);
2549 setproctitle("%s", title);
2550 log_procinit(title);
2552 /* Drop root privileges. */
2553 if (setgid(pw->pw_gid) == -1)
2554 fatal("setgid %d failed", pw->pw_gid);
2555 if (setuid(pw->pw_uid) == -1)
2556 fatal("setuid %d failed", pw->pw_uid);
2563 if (pledge("stdio rpath wpath cpath proc exec "
2564 "sendfd recvfd fattr flock unix unveil", NULL) == -1)
2570 if (pledge("stdio sendfd unix", NULL) == -1)
2573 listen_main(title, fd);
2578 if (pledge("stdio getpw", NULL) == -1)
2581 auth_main(title, &gotd.repos, repo_path);
2584 case PROC_REPO_READ:
2586 if (pledge("stdio rpath recvfd unveil", NULL) == -1)
2589 apply_unveil_repo_readonly(repo_path);
2590 repo_read_main(title, repo_path, pack_fds, temp_fds);
2593 case PROC_REPO_WRITE:
2595 if (pledge("stdio rpath recvfd unveil", NULL) == -1)
2598 apply_unveil_repo_readonly(repo_path);
2599 repo_write_main(title, repo_path, pack_fds, temp_fds);
2603 fatal("invalid process id %d", proc_id);
2606 if (proc_id != PROC_GOTD)
2607 fatal("invalid process id %d", proc_id);
2611 signal_set(&evsigint, SIGINT, gotd_sighdlr, NULL);
2612 signal_set(&evsigterm, SIGTERM, gotd_sighdlr, NULL);
2613 signal_set(&evsighup, SIGHUP, gotd_sighdlr, NULL);
2614 signal_set(&evsigusr1, SIGUSR1, gotd_sighdlr, NULL);
2615 signal(SIGPIPE, SIG_IGN);
2617 signal_add(&evsigint, NULL);
2618 signal_add(&evsigterm, NULL);
2619 signal_add(&evsighup, NULL);
2620 signal_add(&evsigusr1, NULL);
2622 gotd_imsg_event_add(&gotd.listen_proc.iev);
2627 got_repo_pack_fds_close(pack_fds);