2 * Copyright (c) 2022, 2023 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/types.h>
18 #include <sys/queue.h>
19 #include <sys/socket.h>
34 #include "got_error.h"
35 #include "got_repository.h"
36 #include "got_object.h"
38 #include "got_reference.h"
39 #include "got_opentemp.h"
41 #include "got_lib_hash.h"
42 #include "got_lib_delta.h"
43 #include "got_lib_object.h"
44 #include "got_lib_object_cache.h"
45 #include "got_lib_pack.h"
46 #include "got_lib_repository.h"
47 #include "got_lib_gitproto.h"
54 static struct gotd_session {
57 struct got_repository *repo;
60 struct gotd_imsgev parent_iev;
61 struct timeval request_timeout;
62 enum gotd_procid proc_id;
65 static struct gotd_session_client {
66 enum gotd_session_state state;
68 struct gotd_client_capability *capabilities;
74 struct gotd_imsgev iev;
75 struct gotd_imsgev repo_child_iev;
83 } gotd_session_client;
85 void gotd_session_sighdlr(int sig, short event, void *arg);
86 static void gotd_session_shutdown(void);
89 disconnect(struct gotd_session_client *client)
91 log_debug("uid %d: disconnecting", client->euid);
93 if (gotd_imsg_compose_event(&gotd_session.parent_iev,
94 GOTD_IMSG_DISCONNECT, gotd_session.proc_id, -1, NULL, 0) == -1)
95 log_warn("imsg compose DISCONNECT");
97 imsg_clear(&client->repo_child_iev.ibuf);
98 event_del(&client->repo_child_iev.ev);
99 evtimer_del(&client->tmo);
101 if (client->delta_cache_fd != -1)
102 close(client->delta_cache_fd);
103 if (client->packfile_path) {
104 if (unlink(client->packfile_path) == -1 && errno != ENOENT)
105 log_warn("unlink %s: ", client->packfile_path);
106 free(client->packfile_path);
108 if (client->packidx_path) {
109 if (unlink(client->packidx_path) == -1 && errno != ENOENT)
110 log_warn("unlink %s: ", client->packidx_path);
111 free(client->packidx_path);
113 free(client->capabilities);
115 gotd_session_shutdown();
119 disconnect_on_error(struct gotd_session_client *client,
120 const struct got_error *err)
124 log_warnx("uid %d: %s", client->euid, err->msg);
125 if (err->code != GOT_ERR_EOF) {
126 imsg_init(&ibuf, client->fd);
127 gotd_imsg_send_error(&ibuf, 0, gotd_session.proc_id, err);
135 gotd_request_timeout(int fd, short events, void *arg)
137 struct gotd_session_client *client = arg;
139 log_debug("disconnecting uid %d due to timeout", client->euid);
144 gotd_session_sighdlr(int sig, short event, void *arg)
147 * Normal signal handler rules don't apply because libevent
153 log_info("%s: ignoring SIGHUP", __func__);
156 log_info("%s: ignoring SIGUSR1", __func__);
160 gotd_session_shutdown();
164 fatalx("unexpected signal");
168 static const struct got_error *
169 recv_packfile_done(uint32_t *client_id, struct imsg *imsg)
171 struct gotd_imsg_packfile_done idone;
174 log_debug("packfile-done received");
176 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
177 if (datalen != sizeof(idone))
178 return got_error(GOT_ERR_PRIVSEP_LEN);
179 memcpy(&idone, imsg->data, sizeof(idone));
181 *client_id = idone.client_id;
185 static const struct got_error *
186 recv_packfile_install(uint32_t *client_id, struct imsg *imsg)
188 struct gotd_imsg_packfile_install inst;
191 log_debug("packfile-install received");
193 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
194 if (datalen != sizeof(inst))
195 return got_error(GOT_ERR_PRIVSEP_LEN);
196 memcpy(&inst, imsg->data, sizeof(inst));
198 *client_id = inst.client_id;
202 static const struct got_error *
203 recv_ref_updates_start(uint32_t *client_id, struct imsg *imsg)
205 struct gotd_imsg_ref_updates_start istart;
208 log_debug("ref-updates-start received");
210 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
211 if (datalen != sizeof(istart))
212 return got_error(GOT_ERR_PRIVSEP_LEN);
213 memcpy(&istart, imsg->data, sizeof(istart));
215 *client_id = istart.client_id;
219 static const struct got_error *
220 recv_ref_update(uint32_t *client_id, struct imsg *imsg)
222 struct gotd_imsg_ref_update iref;
225 log_debug("ref-update received");
227 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
228 if (datalen < sizeof(iref))
229 return got_error(GOT_ERR_PRIVSEP_LEN);
230 memcpy(&iref, imsg->data, sizeof(iref));
232 *client_id = iref.client_id;
236 static const struct got_error *
237 send_ref_update_ok(struct gotd_session_client *client,
238 struct gotd_imsg_ref_update *iref, const char *refname)
240 struct gotd_imsg_ref_update_ok iok;
241 struct gotd_imsgev *iev = &client->iev;
245 memset(&iok, 0, sizeof(iok));
246 iok.client_id = client->id;
247 memcpy(iok.old_id, iref->old_id, SHA1_DIGEST_LENGTH);
248 memcpy(iok.new_id, iref->new_id, SHA1_DIGEST_LENGTH);
249 iok.name_len = strlen(refname);
251 len = sizeof(iok) + iok.name_len;
252 wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_REF_UPDATE_OK,
253 gotd_session.proc_id, gotd_session.pid, len);
255 return got_error_from_errno("imsg_create REF_UPDATE_OK");
257 if (imsg_add(wbuf, &iok, sizeof(iok)) == -1)
258 return got_error_from_errno("imsg_add REF_UPDATE_OK");
259 if (imsg_add(wbuf, refname, iok.name_len) == -1)
260 return got_error_from_errno("imsg_add REF_UPDATE_OK");
263 imsg_close(&iev->ibuf, wbuf);
264 gotd_imsg_event_add(iev);
269 send_refs_updated(struct gotd_session_client *client)
271 if (gotd_imsg_compose_event(&client->iev, GOTD_IMSG_REFS_UPDATED,
272 gotd_session.proc_id, -1, NULL, 0) == -1)
273 log_warn("imsg compose REFS_UPDATED");
276 static const struct got_error *
277 send_ref_update_ng(struct gotd_session_client *client,
278 struct gotd_imsg_ref_update *iref, const char *refname,
281 const struct got_error *ng_err;
282 struct gotd_imsg_ref_update_ng ing;
283 struct gotd_imsgev *iev = &client->iev;
287 memset(&ing, 0, sizeof(ing));
288 ing.client_id = client->id;
289 memcpy(ing.old_id, iref->old_id, SHA1_DIGEST_LENGTH);
290 memcpy(ing.new_id, iref->new_id, SHA1_DIGEST_LENGTH);
291 ing.name_len = strlen(refname);
293 ng_err = got_error_fmt(GOT_ERR_REF_BUSY, "%s", reason);
294 ing.reason_len = strlen(ng_err->msg);
296 len = sizeof(ing) + ing.name_len + ing.reason_len;
297 wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_REF_UPDATE_NG,
298 gotd_session.proc_id, gotd_session.pid, len);
300 return got_error_from_errno("imsg_create REF_UPDATE_NG");
302 if (imsg_add(wbuf, &ing, sizeof(ing)) == -1)
303 return got_error_from_errno("imsg_add REF_UPDATE_NG");
304 if (imsg_add(wbuf, refname, ing.name_len) == -1)
305 return got_error_from_errno("imsg_add REF_UPDATE_NG");
306 if (imsg_add(wbuf, ng_err->msg, ing.reason_len) == -1)
307 return got_error_from_errno("imsg_add REF_UPDATE_NG");
310 imsg_close(&iev->ibuf, wbuf);
311 gotd_imsg_event_add(iev);
315 static const struct got_error *
316 install_pack(struct gotd_session_client *client, const char *repo_path,
319 const struct got_error *err = NULL;
320 struct gotd_imsg_packfile_install inst;
321 char hex[SHA1_DIGEST_STRING_LENGTH];
323 char *packfile_path = NULL, *packidx_path = NULL;
325 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
326 if (datalen != sizeof(inst))
327 return got_error(GOT_ERR_PRIVSEP_LEN);
328 memcpy(&inst, imsg->data, sizeof(inst));
330 if (client->packfile_path == NULL)
331 return got_error_msg(GOT_ERR_BAD_REQUEST,
332 "client has no pack file");
333 if (client->packidx_path == NULL)
334 return got_error_msg(GOT_ERR_BAD_REQUEST,
335 "client has no pack file index");
337 if (got_sha1_digest_to_str(inst.pack_sha1, hex, sizeof(hex)) == NULL)
338 return got_error_msg(GOT_ERR_NO_SPACE,
339 "could not convert pack file SHA1 to hex");
341 if (asprintf(&packfile_path, "/%s/%s/pack-%s.pack",
342 repo_path, GOT_OBJECTS_PACK_DIR, hex) == -1) {
343 err = got_error_from_errno("asprintf");
347 if (asprintf(&packidx_path, "/%s/%s/pack-%s.idx",
348 repo_path, GOT_OBJECTS_PACK_DIR, hex) == -1) {
349 err = got_error_from_errno("asprintf");
353 if (rename(client->packfile_path, packfile_path) == -1) {
354 err = got_error_from_errno3("rename", client->packfile_path,
359 free(client->packfile_path);
360 client->packfile_path = NULL;
362 if (rename(client->packidx_path, packidx_path) == -1) {
363 err = got_error_from_errno3("rename", client->packidx_path,
368 free(client->packidx_path);
369 client->packidx_path = NULL;
376 static const struct got_error *
377 begin_ref_updates(struct gotd_session_client *client, struct imsg *imsg)
379 struct gotd_imsg_ref_updates_start istart;
382 if (client->nref_updates != -1)
383 return got_error(GOT_ERR_PRIVSEP_MSG);
385 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
386 if (datalen != sizeof(istart))
387 return got_error(GOT_ERR_PRIVSEP_LEN);
388 memcpy(&istart, imsg->data, sizeof(istart));
390 if (istart.nref_updates <= 0)
391 return got_error(GOT_ERR_PRIVSEP_MSG);
393 client->nref_updates = istart.nref_updates;
397 static const struct got_error *
398 update_ref(int *shut, struct gotd_session_client *client,
399 const char *repo_path, struct imsg *imsg)
401 const struct got_error *err = NULL;
402 struct got_repository *repo = NULL;
403 struct got_reference *ref = NULL;
404 struct gotd_imsg_ref_update iref;
405 struct got_object_id old_id, new_id;
406 struct got_object_id *id = NULL;
407 struct got_object *obj = NULL;
408 char *refname = NULL;
411 char hex1[SHA1_DIGEST_STRING_LENGTH];
412 char hex2[SHA1_DIGEST_STRING_LENGTH];
414 log_debug("update-ref from uid %d", client->euid);
416 if (client->nref_updates <= 0)
417 return got_error(GOT_ERR_PRIVSEP_MSG);
419 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
420 if (datalen < sizeof(iref))
421 return got_error(GOT_ERR_PRIVSEP_LEN);
422 memcpy(&iref, imsg->data, sizeof(iref));
423 if (datalen != sizeof(iref) + iref.name_len)
424 return got_error(GOT_ERR_PRIVSEP_LEN);
425 refname = strndup(imsg->data + sizeof(iref), iref.name_len);
427 return got_error_from_errno("strndup");
429 log_debug("updating ref %s for uid %d", refname, client->euid);
431 err = got_repo_open(&repo, repo_path, NULL, NULL);
435 memcpy(old_id.sha1, iref.old_id, SHA1_DIGEST_LENGTH);
436 memcpy(new_id.sha1, iref.new_id, SHA1_DIGEST_LENGTH);
437 err = got_object_open(&obj, repo,
438 iref.delete_ref ? &old_id : &new_id);
442 if (iref.ref_is_new) {
443 err = got_ref_open(&ref, repo, refname, 0);
445 if (err->code != GOT_ERR_NOT_REF)
447 err = got_ref_alloc(&ref, refname, &new_id);
450 err = got_ref_write(ref, repo); /* will lock/unlock */
454 err = got_ref_resolve(&id, repo, ref);
457 got_object_id_hex(&new_id, hex1, sizeof(hex1));
458 got_object_id_hex(id, hex2, sizeof(hex2));
459 err = got_error_fmt(GOT_ERR_REF_BUSY,
460 "Addition %s: %s failed; %s: %s has been "
461 "created by someone else while transaction "
463 got_ref_get_name(ref), hex1,
464 got_ref_get_name(ref), hex2);
467 } else if (iref.delete_ref) {
468 err = got_ref_open(&ref, repo, refname, 1 /* lock */);
473 err = got_ref_resolve(&id, repo, ref);
477 if (got_object_id_cmp(id, &old_id) != 0) {
478 got_object_id_hex(&old_id, hex1, sizeof(hex1));
479 got_object_id_hex(id, hex2, sizeof(hex2));
480 err = got_error_fmt(GOT_ERR_REF_BUSY,
481 "Deletion %s: %s failed; %s: %s has been "
482 "created by someone else while transaction "
484 got_ref_get_name(ref), hex1,
485 got_ref_get_name(ref), hex2);
489 err = got_ref_delete(ref, repo);
496 err = got_ref_open(&ref, repo, refname, 1 /* lock */);
501 err = got_ref_resolve(&id, repo, ref);
505 if (got_object_id_cmp(id, &old_id) != 0) {
506 got_object_id_hex(&old_id, hex1, sizeof(hex1));
507 got_object_id_hex(id, hex2, sizeof(hex2));
508 err = got_error_fmt(GOT_ERR_REF_BUSY,
509 "Update %s: %s failed; %s: %s has been "
510 "created by someone else while transaction "
512 got_ref_get_name(ref), hex1,
513 got_ref_get_name(ref), hex2);
517 if (got_object_id_cmp(&new_id, &old_id) != 0) {
518 err = got_ref_change_ref(ref, &new_id);
522 err = got_ref_write(ref, repo);
532 if (err->code == GOT_ERR_LOCKFILE_TIMEOUT) {
533 err = got_error_fmt(GOT_ERR_LOCKFILE_TIMEOUT,
534 "could not acquire exclusive file lock for %s",
537 send_ref_update_ng(client, &iref, refname, err->msg);
539 send_ref_update_ok(client, &iref, refname);
541 if (client->nref_updates > 0) {
542 client->nref_updates--;
543 if (client->nref_updates == 0) {
544 send_refs_updated(client);
550 const struct got_error *unlock_err;
551 unlock_err = got_ref_unlock(ref);
552 if (unlock_err && err == NULL)
558 got_object_close(obj);
560 got_repo_close(repo);
567 session_dispatch_repo_child(int fd, short event, void *arg)
569 struct gotd_imsgev *iev = arg;
570 struct imsgbuf *ibuf = &iev->ibuf;
571 struct gotd_session_client *client = &gotd_session_client;
576 if (event & EV_READ) {
577 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
578 fatal("imsg_read error");
580 /* Connection closed. */
586 if (event & EV_WRITE) {
587 n = msgbuf_write(&ibuf->w);
588 if (n == -1 && errno != EAGAIN)
589 fatal("msgbuf_write");
591 /* Connection closed. */
598 const struct got_error *err = NULL;
599 uint32_t client_id = 0;
600 int do_disconnect = 0;
601 int do_ref_updates = 0, do_ref_update = 0;
602 int do_packfile_install = 0;
604 if ((n = imsg_get(ibuf, &imsg)) == -1)
605 fatal("%s: imsg_get error", __func__);
606 if (n == 0) /* No more messages. */
609 switch (imsg.hdr.type) {
610 case GOTD_IMSG_ERROR:
612 err = gotd_imsg_recv_error(&client_id, &imsg);
614 case GOTD_IMSG_PACKFILE_DONE:
616 err = recv_packfile_done(&client_id, &imsg);
618 case GOTD_IMSG_PACKFILE_INSTALL:
619 err = recv_packfile_install(&client_id, &imsg);
621 do_packfile_install = 1;
623 case GOTD_IMSG_REF_UPDATES_START:
624 err = recv_ref_updates_start(&client_id, &imsg);
628 case GOTD_IMSG_REF_UPDATE:
629 err = recv_ref_update(&client_id, &imsg);
634 log_debug("unexpected imsg %d", imsg.hdr.type);
640 disconnect_on_error(client, err);
644 if (do_packfile_install)
645 err = install_pack(client,
646 gotd_session.repo->path, &imsg);
647 else if (do_ref_updates)
648 err = begin_ref_updates(client, &imsg);
649 else if (do_ref_update)
650 err = update_ref(&shut, client,
651 gotd_session.repo->path, &imsg);
653 log_warnx("uid %d: %s", client->euid, err->msg);
659 gotd_imsg_event_add(iev);
661 /* This pipe is dead. Remove its event handler */
663 event_loopexit(NULL);
667 static const struct got_error *
668 recv_capabilities(struct gotd_session_client *client, struct imsg *imsg)
670 struct gotd_imsg_capabilities icapas;
673 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
674 if (datalen != sizeof(icapas))
675 return got_error(GOT_ERR_PRIVSEP_LEN);
676 memcpy(&icapas, imsg->data, sizeof(icapas));
678 client->ncapa_alloc = icapas.ncapabilities;
679 client->capabilities = calloc(client->ncapa_alloc,
680 sizeof(*client->capabilities));
681 if (client->capabilities == NULL) {
682 client->ncapa_alloc = 0;
683 return got_error_from_errno("calloc");
686 log_debug("expecting %zu capabilities from uid %d",
687 client->ncapa_alloc, client->euid);
691 static const struct got_error *
692 recv_capability(struct gotd_session_client *client, struct imsg *imsg)
694 struct gotd_imsg_capability icapa;
695 struct gotd_client_capability *capa;
697 char *key, *value = NULL;
699 if (client->capabilities == NULL ||
700 client->ncapabilities >= client->ncapa_alloc) {
701 return got_error_msg(GOT_ERR_BAD_REQUEST,
702 "unexpected capability received");
705 memset(&icapa, 0, sizeof(icapa));
707 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
708 if (datalen < sizeof(icapa))
709 return got_error(GOT_ERR_PRIVSEP_LEN);
710 memcpy(&icapa, imsg->data, sizeof(icapa));
712 if (datalen != sizeof(icapa) + icapa.key_len + icapa.value_len)
713 return got_error(GOT_ERR_PRIVSEP_LEN);
715 key = strndup(imsg->data + sizeof(icapa), icapa.key_len);
717 return got_error_from_errno("strndup");
718 if (icapa.value_len > 0) {
719 value = strndup(imsg->data + sizeof(icapa) + icapa.key_len,
723 return got_error_from_errno("strndup");
727 capa = &client->capabilities[client->ncapabilities++];
732 log_debug("uid %d: capability %s=%s", client->euid, key, value);
734 log_debug("uid %d: capability %s", client->euid, key);
739 static const struct got_error *
740 ensure_client_is_reading(struct gotd_session_client *client)
742 if (client->is_writing) {
743 return got_error_fmt(GOT_ERR_BAD_PACKET,
744 "uid %d made a read-request but is not reading from "
745 "a repository", client->euid);
751 static const struct got_error *
752 ensure_client_is_writing(struct gotd_session_client *client)
754 if (!client->is_writing) {
755 return got_error_fmt(GOT_ERR_BAD_PACKET,
756 "uid %d made a write-request but is not writing to "
757 "a repository", client->euid);
763 static const struct got_error *
764 forward_want(struct gotd_session_client *client, struct imsg *imsg)
766 struct gotd_imsg_want ireq;
767 struct gotd_imsg_want iwant;
770 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
771 if (datalen != sizeof(ireq))
772 return got_error(GOT_ERR_PRIVSEP_LEN);
774 memcpy(&ireq, imsg->data, datalen);
776 memset(&iwant, 0, sizeof(iwant));
777 memcpy(iwant.object_id, ireq.object_id, SHA1_DIGEST_LENGTH);
778 iwant.client_id = client->id;
780 if (gotd_imsg_compose_event(&client->repo_child_iev, GOTD_IMSG_WANT,
781 gotd_session.proc_id, -1, &iwant, sizeof(iwant)) == -1)
782 return got_error_from_errno("imsg compose WANT");
787 static const struct got_error *
788 forward_ref_update(struct gotd_session_client *client, struct imsg *imsg)
790 const struct got_error *err = NULL;
791 struct gotd_imsg_ref_update ireq;
792 struct gotd_imsg_ref_update *iref = NULL;
795 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
796 if (datalen < sizeof(ireq))
797 return got_error(GOT_ERR_PRIVSEP_LEN);
798 memcpy(&ireq, imsg->data, sizeof(ireq));
799 if (datalen != sizeof(ireq) + ireq.name_len)
800 return got_error(GOT_ERR_PRIVSEP_LEN);
802 iref = malloc(datalen);
804 return got_error_from_errno("malloc");
805 memcpy(iref, imsg->data, datalen);
807 iref->client_id = client->id;
808 if (gotd_imsg_compose_event(&client->repo_child_iev,
809 GOTD_IMSG_REF_UPDATE, gotd_session.proc_id, -1,
810 iref, datalen) == -1)
811 err = got_error_from_errno("imsg compose REF_UPDATE");
816 static const struct got_error *
817 forward_have(struct gotd_session_client *client, struct imsg *imsg)
819 struct gotd_imsg_have ireq;
820 struct gotd_imsg_have ihave;
823 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
824 if (datalen != sizeof(ireq))
825 return got_error(GOT_ERR_PRIVSEP_LEN);
827 memcpy(&ireq, imsg->data, datalen);
829 memset(&ihave, 0, sizeof(ihave));
830 memcpy(ihave.object_id, ireq.object_id, SHA1_DIGEST_LENGTH);
831 ihave.client_id = client->id;
833 if (gotd_imsg_compose_event(&client->repo_child_iev, GOTD_IMSG_HAVE,
834 gotd_session.proc_id, -1, &ihave, sizeof(ihave)) == -1)
835 return got_error_from_errno("imsg compose HAVE");
841 client_has_capability(struct gotd_session_client *client, const char *capastr)
843 struct gotd_client_capability *capa;
846 if (client->ncapabilities == 0)
849 for (i = 0; i < client->ncapabilities; i++) {
850 capa = &client->capabilities[i];
851 if (strcmp(capa->key, capastr) == 0)
858 static const struct got_error *
859 recv_packfile(struct gotd_session_client *client)
861 const struct got_error *err = NULL;
862 struct gotd_imsg_recv_packfile ipack;
863 struct gotd_imsg_packfile_pipe ipipe;
864 struct gotd_imsg_packidx_file ifile;
865 char *basepath = NULL, *pack_path = NULL, *idx_path = NULL;
866 int packfd = -1, idxfd = -1;
867 int pipe[2] = { -1, -1 };
869 if (client->packfile_path) {
870 return got_error_fmt(GOT_ERR_PRIVSEP_MSG,
871 "uid %d already has a pack file", client->euid);
874 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe) == -1)
875 return got_error_from_errno("socketpair");
877 memset(&ipipe, 0, sizeof(ipipe));
878 ipipe.client_id = client->id;
880 /* Send pack pipe end 0 to repo child process. */
881 if (gotd_imsg_compose_event(&client->repo_child_iev,
882 GOTD_IMSG_PACKFILE_PIPE, gotd_session.proc_id, pipe[0],
883 &ipipe, sizeof(ipipe)) == -1) {
884 err = got_error_from_errno("imsg compose PACKFILE_PIPE");
890 /* Send pack pipe end 1 to gotsh(1) (expects just an fd, no data). */
891 if (gotd_imsg_compose_event(&client->iev,
892 GOTD_IMSG_PACKFILE_PIPE, gotd_session.proc_id, pipe[1],
894 err = got_error_from_errno("imsg compose PACKFILE_PIPE");
897 if (asprintf(&basepath, "%s/%s/receiving-from-uid-%d.pack",
898 got_repo_get_path(gotd_session.repo), GOT_OBJECTS_PACK_DIR,
899 client->euid) == -1) {
900 err = got_error_from_errno("asprintf");
904 err = got_opentemp_named_fd(&pack_path, &packfd, basepath, "");
907 if (fchmod(packfd, GOT_DEFAULT_PACK_MODE) == -1) {
908 err = got_error_from_errno2("fchmod", pack_path);
913 if (asprintf(&basepath, "%s/%s/receiving-from-uid-%d.idx",
914 got_repo_get_path(gotd_session.repo), GOT_OBJECTS_PACK_DIR,
915 client->euid) == -1) {
916 err = got_error_from_errno("asprintf");
920 err = got_opentemp_named_fd(&idx_path, &idxfd, basepath, "");
923 if (fchmod(idxfd, GOT_DEFAULT_PACK_MODE) == -1) {
924 err = got_error_from_errno2("fchmod", idx_path);
928 memset(&ifile, 0, sizeof(ifile));
929 ifile.client_id = client->id;
930 if (gotd_imsg_compose_event(&client->repo_child_iev,
931 GOTD_IMSG_PACKIDX_FILE, gotd_session.proc_id,
932 idxfd, &ifile, sizeof(ifile)) == -1) {
933 err = got_error_from_errno("imsg compose PACKIDX_FILE");
939 memset(&ipack, 0, sizeof(ipack));
940 ipack.client_id = client->id;
941 if (client_has_capability(client, GOT_CAPA_REPORT_STATUS))
942 ipack.report_status = 1;
944 if (gotd_imsg_compose_event(&client->repo_child_iev,
945 GOTD_IMSG_RECV_PACKFILE, gotd_session.proc_id, packfd,
946 &ipack, sizeof(ipack)) == -1) {
947 err = got_error_from_errno("imsg compose RECV_PACKFILE");
955 if (pipe[0] != -1 && close(pipe[0]) == -1 && err == NULL)
956 err = got_error_from_errno("close");
957 if (pipe[1] != -1 && close(pipe[1]) == -1 && err == NULL)
958 err = got_error_from_errno("close");
959 if (packfd != -1 && close(packfd) == -1 && err == NULL)
960 err = got_error_from_errno("close");
961 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
962 err = got_error_from_errno("close");
967 client->packfile_path = pack_path;
968 client->packidx_path = idx_path;
973 static const struct got_error *
974 send_packfile(struct gotd_session_client *client)
976 const struct got_error *err = NULL;
977 struct gotd_imsg_send_packfile ipack;
978 struct gotd_imsg_packfile_pipe ipipe;
981 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe) == -1)
982 return got_error_from_errno("socketpair");
984 memset(&ipack, 0, sizeof(ipack));
985 memset(&ipipe, 0, sizeof(ipipe));
987 ipack.client_id = client->id;
988 if (client_has_capability(client, GOT_CAPA_SIDE_BAND_64K))
989 ipack.report_progress = 1;
991 client->delta_cache_fd = got_opentempfd();
992 if (client->delta_cache_fd == -1)
993 return got_error_from_errno("got_opentempfd");
995 if (gotd_imsg_compose_event(&client->repo_child_iev,
996 GOTD_IMSG_SEND_PACKFILE, PROC_GOTD, client->delta_cache_fd,
997 &ipack, sizeof(ipack)) == -1) {
998 err = got_error_from_errno("imsg compose SEND_PACKFILE");
1004 ipipe.client_id = client->id;
1006 /* Send pack pipe end 0 to repo child process. */
1007 if (gotd_imsg_compose_event(&client->repo_child_iev,
1008 GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD,
1009 pipe[0], &ipipe, sizeof(ipipe)) == -1) {
1010 err = got_error_from_errno("imsg compose PACKFILE_PIPE");
1015 /* Send pack pipe end 1 to gotsh(1) (expects just an fd, no data). */
1016 if (gotd_imsg_compose_event(&client->iev,
1017 GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[1], NULL, 0) == -1)
1018 err = got_error_from_errno("imsg compose PACKFILE_PIPE");
1024 session_dispatch_client(int fd, short events, void *arg)
1026 struct gotd_imsgev *iev = arg;
1027 struct imsgbuf *ibuf = &iev->ibuf;
1028 struct gotd_session_client *client = &gotd_session_client;
1029 const struct got_error *err = NULL;
1033 if (events & EV_WRITE) {
1034 while (ibuf->w.queued) {
1035 n = msgbuf_write(&ibuf->w);
1036 if (n == -1 && errno == EPIPE) {
1038 * The client has closed its socket.
1039 * This can happen when Git clients are
1040 * done sending pack file data.
1042 msgbuf_clear(&ibuf->w);
1044 } else if (n == -1 && errno != EAGAIN) {
1045 err = got_error_from_errno("imsg_flush");
1046 disconnect_on_error(client, err);
1050 /* Connection closed. */
1051 err = got_error(GOT_ERR_EOF);
1052 disconnect_on_error(client, err);
1058 if ((events & EV_READ) == 0)
1061 memset(&imsg, 0, sizeof(imsg));
1063 while (err == NULL) {
1064 err = gotd_imsg_recv(&imsg, ibuf, 0);
1066 if (err->code == GOT_ERR_PRIVSEP_READ)
1068 else if (err->code == GOT_ERR_EOF &&
1069 client->state == GOTD_STATE_EXPECT_CAPABILITIES) {
1071 * The client has closed its socket before
1072 * sending its capability announcement.
1073 * This can happen when Git clients have
1074 * no ref-updates to send.
1076 disconnect_on_error(client, err);
1082 evtimer_del(&client->tmo);
1084 switch (imsg.hdr.type) {
1085 case GOTD_IMSG_CAPABILITIES:
1086 if (client->state != GOTD_STATE_EXPECT_CAPABILITIES) {
1087 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1088 "unexpected capabilities received");
1091 log_debug("receiving capabilities from uid %d",
1093 err = recv_capabilities(client, &imsg);
1095 case GOTD_IMSG_CAPABILITY:
1096 if (client->state != GOTD_STATE_EXPECT_CAPABILITIES) {
1097 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1098 "unexpected capability received");
1101 err = recv_capability(client, &imsg);
1102 if (err || client->ncapabilities < client->ncapa_alloc)
1104 if (!client->is_writing) {
1105 client->state = GOTD_STATE_EXPECT_WANT;
1106 client->accept_flush_pkt = 1;
1107 log_debug("uid %d: expecting want-lines",
1109 } else if (client->is_writing) {
1110 client->state = GOTD_STATE_EXPECT_REF_UPDATE;
1111 client->accept_flush_pkt = 1;
1112 log_debug("uid %d: expecting ref-update-lines",
1115 fatalx("client %d is both reading and writing",
1118 case GOTD_IMSG_WANT:
1119 if (client->state != GOTD_STATE_EXPECT_WANT) {
1120 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1121 "unexpected want-line received");
1124 log_debug("received want-line from uid %d",
1126 err = ensure_client_is_reading(client);
1129 client->accept_flush_pkt = 1;
1130 err = forward_want(client, &imsg);
1132 case GOTD_IMSG_REF_UPDATE:
1133 if (client->state != GOTD_STATE_EXPECT_REF_UPDATE &&
1135 GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1136 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1137 "unexpected ref-update-line received");
1140 log_debug("received ref-update-line from uid %d",
1142 err = ensure_client_is_writing(client);
1145 err = forward_ref_update(client, &imsg);
1148 client->state = GOTD_STATE_EXPECT_MORE_REF_UPDATES;
1149 client->accept_flush_pkt = 1;
1151 case GOTD_IMSG_HAVE:
1152 if (client->state != GOTD_STATE_EXPECT_HAVE) {
1153 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1154 "unexpected have-line received");
1157 log_debug("received have-line from uid %d",
1159 err = ensure_client_is_reading(client);
1162 err = forward_have(client, &imsg);
1165 client->accept_flush_pkt = 1;
1167 case GOTD_IMSG_FLUSH:
1168 if (client->state == GOTD_STATE_EXPECT_WANT ||
1169 client->state == GOTD_STATE_EXPECT_HAVE) {
1170 err = ensure_client_is_reading(client);
1173 } else if (client->state ==
1174 GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1175 err = ensure_client_is_writing(client);
1178 } else if (client->state != GOTD_STATE_EXPECT_DONE) {
1179 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1180 "unexpected flush-pkt received");
1183 if (!client->accept_flush_pkt) {
1184 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1185 "unexpected flush-pkt received");
1190 * Accept just one flush packet at a time.
1191 * Future client state transitions will set this flag
1192 * again if another flush packet is expected.
1194 client->accept_flush_pkt = 0;
1196 log_debug("received flush-pkt from uid %d",
1198 if (client->state == GOTD_STATE_EXPECT_WANT) {
1199 client->state = GOTD_STATE_EXPECT_HAVE;
1200 log_debug("uid %d: expecting have-lines",
1202 } else if (client->state == GOTD_STATE_EXPECT_HAVE) {
1203 client->state = GOTD_STATE_EXPECT_DONE;
1204 client->accept_flush_pkt = 1;
1205 log_debug("uid %d: expecting 'done'",
1207 } else if (client->state ==
1208 GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1209 client->state = GOTD_STATE_EXPECT_PACKFILE;
1210 log_debug("uid %d: expecting packfile",
1212 err = recv_packfile(client);
1213 } else if (client->state != GOTD_STATE_EXPECT_DONE) {
1214 /* should not happen, see above */
1215 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1216 "unexpected client state");
1220 case GOTD_IMSG_DONE:
1221 if (client->state != GOTD_STATE_EXPECT_HAVE &&
1222 client->state != GOTD_STATE_EXPECT_DONE) {
1223 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1224 "unexpected flush-pkt received");
1227 log_debug("received 'done' from uid %d", client->euid);
1228 err = ensure_client_is_reading(client);
1231 client->state = GOTD_STATE_DONE;
1232 client->accept_flush_pkt = 1;
1233 err = send_packfile(client);
1236 log_debug("unexpected imsg %d", imsg.hdr.type);
1237 err = got_error(GOT_ERR_PRIVSEP_MSG);
1245 if (err->code != GOT_ERR_EOF ||
1246 client->state != GOTD_STATE_EXPECT_PACKFILE)
1247 disconnect_on_error(client, err);
1249 gotd_imsg_event_add(iev);
1250 evtimer_add(&client->tmo, &gotd_session.request_timeout);
1254 static const struct got_error *
1255 list_refs_request(void)
1257 static const struct got_error *err;
1258 struct gotd_session_client *client = &gotd_session_client;
1259 struct gotd_imsgev *iev = &client->repo_child_iev;
1260 struct gotd_imsg_list_refs_internal ilref;
1263 if (client->state != GOTD_STATE_EXPECT_LIST_REFS)
1264 return got_error(GOT_ERR_PRIVSEP_MSG);
1266 memset(&ilref, 0, sizeof(ilref));
1267 ilref.client_id = client->id;
1269 fd = dup(client->fd);
1271 return got_error_from_errno("dup");
1273 if (gotd_imsg_compose_event(iev, GOTD_IMSG_LIST_REFS_INTERNAL,
1274 gotd_session.proc_id, fd, &ilref, sizeof(ilref)) == -1) {
1275 err = got_error_from_errno("imsg compose LIST_REFS_INTERNAL");
1280 client->state = GOTD_STATE_EXPECT_CAPABILITIES;
1281 log_debug("uid %d: expecting capabilities", client->euid);
1285 static const struct got_error *
1286 recv_connect(struct imsg *imsg)
1288 struct gotd_session_client *client = &gotd_session_client;
1289 struct gotd_imsg_connect iconnect;
1292 if (client->state != GOTD_STATE_EXPECT_LIST_REFS)
1293 return got_error(GOT_ERR_PRIVSEP_MSG);
1295 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1296 if (datalen != sizeof(iconnect))
1297 return got_error(GOT_ERR_PRIVSEP_LEN);
1298 memcpy(&iconnect, imsg->data, sizeof(iconnect));
1301 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1303 client->fd = imsg->fd;
1304 client->euid = iconnect.euid;
1305 client->egid = iconnect.egid;
1307 imsg_init(&client->iev.ibuf, client->fd);
1308 client->iev.handler = session_dispatch_client;
1309 client->iev.events = EV_READ;
1310 client->iev.handler_arg = NULL;
1311 event_set(&client->iev.ev, client->iev.ibuf.fd, EV_READ,
1312 session_dispatch_client, &client->iev);
1313 gotd_imsg_event_add(&client->iev);
1314 evtimer_set(&client->tmo, gotd_request_timeout, client);
1319 static const struct got_error *
1320 recv_repo_child(struct imsg *imsg)
1322 struct gotd_imsg_connect_repo_child ichild;
1323 struct gotd_session_client *client = &gotd_session_client;
1326 if (client->state != GOTD_STATE_EXPECT_LIST_REFS)
1327 return got_error(GOT_ERR_PRIVSEP_MSG);
1329 /* We should already have received a pipe to the listener. */
1330 if (client->fd == -1)
1331 return got_error(GOT_ERR_PRIVSEP_MSG);
1333 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1334 if (datalen != sizeof(ichild))
1335 return got_error(GOT_ERR_PRIVSEP_LEN);
1337 memcpy(&ichild, imsg->data, sizeof(ichild));
1339 client->id = ichild.client_id;
1340 if (ichild.proc_id == PROC_REPO_WRITE)
1341 client->is_writing = 1;
1342 else if (ichild.proc_id == PROC_REPO_READ)
1343 client->is_writing = 0;
1345 return got_error_msg(GOT_ERR_PRIVSEP_MSG,
1346 "bad child process type");
1349 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1351 imsg_init(&client->repo_child_iev.ibuf, imsg->fd);
1352 client->repo_child_iev.handler = session_dispatch_repo_child;
1353 client->repo_child_iev.events = EV_READ;
1354 client->repo_child_iev.handler_arg = NULL;
1355 event_set(&client->repo_child_iev.ev, client->repo_child_iev.ibuf.fd,
1356 EV_READ, session_dispatch_repo_child, &client->repo_child_iev);
1357 gotd_imsg_event_add(&client->repo_child_iev);
1359 /* The "recvfd" pledge promise is no longer needed. */
1360 if (pledge("stdio rpath wpath cpath sendfd fattr flock", NULL) == -1)
1367 session_dispatch(int fd, short event, void *arg)
1369 struct gotd_imsgev *iev = arg;
1370 struct imsgbuf *ibuf = &iev->ibuf;
1371 struct gotd_session_client *client = &gotd_session_client;
1376 if (event & EV_READ) {
1377 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1378 fatal("imsg_read error");
1380 /* Connection closed. */
1386 if (event & EV_WRITE) {
1387 n = msgbuf_write(&ibuf->w);
1388 if (n == -1 && errno != EAGAIN)
1389 fatal("msgbuf_write");
1391 /* Connection closed. */
1398 const struct got_error *err = NULL;
1399 uint32_t client_id = 0;
1400 int do_disconnect = 0, do_list_refs = 0;
1402 if ((n = imsg_get(ibuf, &imsg)) == -1)
1403 fatal("%s: imsg_get error", __func__);
1404 if (n == 0) /* No more messages. */
1407 switch (imsg.hdr.type) {
1408 case GOTD_IMSG_ERROR:
1410 err = gotd_imsg_recv_error(&client_id, &imsg);
1412 case GOTD_IMSG_CONNECT:
1413 err = recv_connect(&imsg);
1415 case GOTD_IMSG_DISCONNECT:
1418 case GOTD_IMSG_CONNECT_REPO_CHILD:
1419 err = recv_repo_child(&imsg);
1425 log_debug("unexpected imsg %d", imsg.hdr.type);
1430 if (do_disconnect) {
1432 disconnect_on_error(client, err);
1435 } else if (do_list_refs)
1436 err = list_refs_request();
1439 log_warnx("uid %d: %s", client->euid, err->msg);
1443 gotd_imsg_event_add(iev);
1445 /* This pipe is dead. Remove its event handler */
1446 event_del(&iev->ev);
1447 event_loopexit(NULL);
1452 session_main(const char *title, const char *repo_path,
1453 int *pack_fds, int *temp_fds, struct timeval *request_timeout,
1454 enum gotd_procid proc_id)
1456 const struct got_error *err = NULL;
1457 struct event evsigint, evsigterm, evsighup, evsigusr1;
1459 gotd_session.title = title;
1460 gotd_session.pid = getpid();
1461 gotd_session.pack_fds = pack_fds;
1462 gotd_session.temp_fds = temp_fds;
1463 memcpy(&gotd_session.request_timeout, request_timeout,
1464 sizeof(gotd_session.request_timeout));
1465 gotd_session.proc_id = proc_id;
1467 err = got_repo_open(&gotd_session.repo, repo_path, NULL, pack_fds);
1470 if (!got_repo_is_bare(gotd_session.repo)) {
1471 err = got_error_msg(GOT_ERR_NOT_GIT_REPO,
1472 "bare git repository required");
1476 got_repo_temp_fds_set(gotd_session.repo, temp_fds);
1478 signal_set(&evsigint, SIGINT, gotd_session_sighdlr, NULL);
1479 signal_set(&evsigterm, SIGTERM, gotd_session_sighdlr, NULL);
1480 signal_set(&evsighup, SIGHUP, gotd_session_sighdlr, NULL);
1481 signal_set(&evsigusr1, SIGUSR1, gotd_session_sighdlr, NULL);
1482 signal(SIGPIPE, SIG_IGN);
1484 signal_add(&evsigint, NULL);
1485 signal_add(&evsigterm, NULL);
1486 signal_add(&evsighup, NULL);
1487 signal_add(&evsigusr1, NULL);
1489 gotd_session_client.state = GOTD_STATE_EXPECT_LIST_REFS;
1490 gotd_session_client.fd = -1;
1491 gotd_session_client.nref_updates = -1;
1492 gotd_session_client.delta_cache_fd = -1;
1493 gotd_session_client.accept_flush_pkt = 1;
1495 imsg_init(&gotd_session.parent_iev.ibuf, GOTD_FILENO_MSG_PIPE);
1496 gotd_session.parent_iev.handler = session_dispatch;
1497 gotd_session.parent_iev.events = EV_READ;
1498 gotd_session.parent_iev.handler_arg = NULL;
1499 event_set(&gotd_session.parent_iev.ev, gotd_session.parent_iev.ibuf.fd,
1500 EV_READ, session_dispatch, &gotd_session.parent_iev);
1501 if (gotd_imsg_compose_event(&gotd_session.parent_iev,
1502 GOTD_IMSG_CLIENT_SESSION_READY, gotd_session.proc_id,
1503 -1, NULL, 0) == -1) {
1504 err = got_error_from_errno("imsg compose CLIENT_SESSION_READY");
1511 log_warnx("%s: %s", title, err->msg);
1512 gotd_session_shutdown();
1516 gotd_session_shutdown(void)
1518 log_debug("shutting down");
1519 if (gotd_session.repo)
1520 got_repo_close(gotd_session.repo);
1521 got_repo_pack_fds_close(gotd_session.pack_fds);
1522 got_repo_temp_fds_close(gotd_session.temp_fds);