2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 * Copyright (c) 2020 Ori Bernstein <ori@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/types.h>
19 #include <sys/queue.h>
38 #include "got_object.h"
39 #include "got_error.h"
41 #include "got_repository.h"
43 #include "got_lib_hash.h"
44 #include "got_lib_delta.h"
45 #include "got_lib_inflate.h"
46 #include "got_lib_object.h"
47 #include "got_lib_object_parse.h"
48 #include "got_lib_privsep.h"
49 #include "got_lib_pack.h"
50 #include "got_lib_poll.h"
52 #include "got_privsep.h"
55 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
59 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
62 static const struct got_error *
63 read_imsg(struct imsgbuf *ibuf)
65 const struct got_error *err;
68 err = got_poll_fd(ibuf->fd, POLLIN, INFTIM);
70 if (err->code == GOT_ERR_EOF)
71 return got_error(GOT_ERR_PRIVSEP_PIPE);
77 if (errno == EAGAIN) /* Could be a file-descriptor leak. */
78 return got_error(GOT_ERR_PRIVSEP_NO_FD);
79 return got_error(GOT_ERR_PRIVSEP_READ);
82 return got_error(GOT_ERR_PRIVSEP_PIPE);
87 const struct got_error *
88 got_privsep_wait_for_child(pid_t pid)
92 if (waitpid(pid, &child_status, 0) == -1)
93 return got_error_from_errno("waitpid");
95 if (!WIFEXITED(child_status))
96 return got_error(GOT_ERR_PRIVSEP_DIED);
98 if (WEXITSTATUS(child_status) != 0)
99 return got_error(GOT_ERR_PRIVSEP_EXIT);
104 static const struct got_error *
105 recv_imsg_error(struct imsg *imsg, size_t datalen)
107 struct got_imsg_error *ierr;
109 if (datalen != sizeof(*ierr))
110 return got_error(GOT_ERR_PRIVSEP_LEN);
113 if (ierr->code == GOT_ERR_ERRNO) {
114 static struct got_error serr;
115 serr.code = GOT_ERR_ERRNO;
116 serr.msg = strerror(ierr->errno_code);
120 return got_error(ierr->code);
123 const struct got_error *
124 got_privsep_recv_imsg(struct imsg *imsg, struct imsgbuf *ibuf,
127 const struct got_error *err;
130 n = imsg_get(ibuf, imsg);
132 return got_error_from_errno("imsg_get");
135 err = read_imsg(ibuf);
138 n = imsg_get(ibuf, imsg);
140 return got_error_from_errno("imsg_get");
143 if (imsg->hdr.len < IMSG_HEADER_SIZE + min_datalen)
144 return got_error(GOT_ERR_PRIVSEP_LEN);
146 if (imsg->hdr.type == GOT_IMSG_ERROR) {
147 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
148 return recv_imsg_error(imsg, datalen);
154 /* Attempt to send an error in an imsg. Complain on stderr as a last resort. */
156 got_privsep_send_error(struct imsgbuf *ibuf, const struct got_error *err)
158 const struct got_error *poll_err;
159 struct got_imsg_error ierr;
162 ierr.code = err->code;
163 if (err->code == GOT_ERR_ERRNO)
164 ierr.errno_code = errno;
167 ret = imsg_compose(ibuf, GOT_IMSG_ERROR, 0, 0, -1, &ierr, sizeof(ierr));
169 fprintf(stderr, "%s: error %d \"%s\": imsg_compose: %s\n",
170 getprogname(), err->code, err->msg, strerror(errno));
174 poll_err = got_poll_fd(ibuf->fd, POLLOUT, INFTIM);
176 fprintf(stderr, "%s: error %d \"%s\": poll: %s\n",
177 getprogname(), err->code, err->msg, poll_err->msg);
181 ret = imsg_flush(ibuf);
183 fprintf(stderr, "%s: error %d \"%s\": imsg_flush: %s\n",
184 getprogname(), err->code, err->msg, strerror(errno));
190 static const struct got_error *
191 flush_imsg(struct imsgbuf *ibuf)
193 const struct got_error *err;
195 err = got_poll_fd(ibuf->fd, POLLOUT, INFTIM);
199 if (imsg_flush(ibuf) == -1) {
201 return got_error_from_errno("imsg_flush");
207 const struct got_error *
208 got_privsep_flush_imsg(struct imsgbuf *ibuf)
210 return flush_imsg(ibuf);
213 const struct got_error *
214 got_privsep_send_stop(int fd)
216 const struct got_error *err = NULL;
219 imsg_init(&ibuf, fd);
221 if (imsg_compose(&ibuf, GOT_IMSG_STOP, 0, 0, -1, NULL, 0) == -1)
222 return got_error_from_errno("imsg_compose STOP");
224 err = flush_imsg(&ibuf);
228 const struct got_error *
229 got_privsep_send_obj_req(struct imsgbuf *ibuf, int fd,
230 struct got_object_id *id)
232 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_REQUEST, 0, 0, fd,
233 id, sizeof(*id)) == -1)
234 return got_error_from_errno("imsg_compose OBJECT_REQUEST");
236 return flush_imsg(ibuf);
239 const struct got_error *
240 got_privsep_send_raw_obj_req(struct imsgbuf *ibuf, int fd,
241 struct got_object_id *id)
243 if (imsg_compose(ibuf, GOT_IMSG_RAW_OBJECT_REQUEST, 0, 0, fd,
244 id, sizeof(*id)) == -1)
245 return got_error_from_errno("imsg_compose RAW_OBJECT_REQUEST");
247 return flush_imsg(ibuf);
250 const struct got_error *
251 got_privsep_send_raw_obj_outfd(struct imsgbuf *ibuf, int outfd)
253 const struct got_error *err = NULL;
255 if (imsg_compose(ibuf, GOT_IMSG_RAW_OBJECT_OUTFD, 0, 0, outfd, NULL, 0)
257 err = got_error_from_errno("imsg_compose RAW_OBJECT_OUTFD");
262 return flush_imsg(ibuf);
265 const struct got_error *
266 got_privsep_send_raw_obj(struct imsgbuf *ibuf, off_t size, size_t hdrlen,
269 const struct got_error *err = NULL;
270 struct got_imsg_raw_obj iobj;
271 size_t len = sizeof(iobj);
274 memset(&iobj, 0, sizeof(iobj));
275 iobj.hdrlen = hdrlen;
278 if (data && size + hdrlen <= GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX)
279 len += (size_t)size + hdrlen;
281 wbuf = imsg_create(ibuf, GOT_IMSG_RAW_OBJECT, 0, 0, len);
283 err = got_error_from_errno("imsg_create RAW_OBJECT");
287 if (imsg_add(wbuf, &iobj, sizeof(iobj)) == -1)
288 return got_error_from_errno("imsg_add RAW_OBJECT");
290 if (data && size + hdrlen <= GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX) {
291 if (imsg_add(wbuf, data, size + hdrlen) == -1)
292 return got_error_from_errno("imsg_add RAW_OBJECT");
296 imsg_close(ibuf, wbuf);
298 return flush_imsg(ibuf);
301 const struct got_error *
302 got_privsep_recv_raw_obj(uint8_t **outbuf, off_t *size, size_t *hdrlen,
303 struct imsgbuf *ibuf)
305 const struct got_error *err = NULL;
307 struct got_imsg_raw_obj *iobj;
312 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
316 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
318 switch (imsg.hdr.type) {
319 case GOT_IMSG_RAW_OBJECT:
320 if (datalen < sizeof(*iobj)) {
321 err = got_error(GOT_ERR_PRIVSEP_LEN);
326 *hdrlen = iobj->hdrlen;
328 if (datalen == sizeof(*iobj)) {
329 /* Data has been written to file descriptor. */
334 *size + *hdrlen > GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX) {
335 err = got_error(GOT_ERR_PRIVSEP_LEN);
339 *outbuf = malloc(*size + *hdrlen);
340 if (*outbuf == NULL) {
341 err = got_error_from_errno("malloc");
344 memcpy(*outbuf, imsg.data + sizeof(*iobj), *size + *hdrlen);
347 err = got_error(GOT_ERR_PRIVSEP_MSG);
356 const struct got_error *
357 got_privsep_send_commit_req(struct imsgbuf *ibuf, int fd,
358 struct got_object_id *id, int pack_idx)
360 const struct got_error *err = NULL;
361 struct got_imsg_packed_object iobj;
365 memset(&iobj, 0, sizeof(iobj));
366 if (pack_idx != -1) { /* commit is packed */
368 memcpy(&iobj.id, id, sizeof(iobj.id));
376 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_REQUEST, 0, 0, fd, data, len)
378 err = got_error_from_errno("imsg_compose COMMIT_REQUEST");
383 return flush_imsg(ibuf);
386 const struct got_error *
387 got_privsep_send_tree_req(struct imsgbuf *ibuf, int fd,
388 struct got_object_id *id, int pack_idx)
394 len = sizeof(struct got_imsg_packed_object);
398 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_REQUEST, 0, 0, len);
400 return got_error_from_errno("imsg_create TREE_REQUEST");
402 if (imsg_add(wbuf, id, sizeof(*id)) == -1)
403 return got_error_from_errno("imsg_add TREE_REQUEST");
405 if (pack_idx != -1) { /* tree is packed */
406 if (imsg_add(wbuf, &pack_idx, sizeof(pack_idx)) == -1)
407 return got_error_from_errno("imsg_add TREE_REQUEST");
411 imsg_close(ibuf, wbuf);
413 return flush_imsg(ibuf);
416 const struct got_error *
417 got_privsep_send_tag_req(struct imsgbuf *ibuf, int fd,
418 struct got_object_id *id, int pack_idx)
420 struct got_imsg_packed_object iobj;
424 memset(&iobj, 0, sizeof(iobj));
425 if (pack_idx != -1) { /* tag is packed */
427 memcpy(&iobj.id, id, sizeof(iobj.id));
435 if (imsg_compose(ibuf, GOT_IMSG_TAG_REQUEST, 0, 0, fd, data, len)
437 return got_error_from_errno("imsg_compose TAG_REQUEST");
439 return flush_imsg(ibuf);
442 const struct got_error *
443 got_privsep_send_blob_req(struct imsgbuf *ibuf, int infd,
444 struct got_object_id *id, int pack_idx)
446 const struct got_error *err = NULL;
447 struct got_imsg_packed_object iobj;
451 memset(&iobj, 0, sizeof(iobj));
452 if (pack_idx != -1) { /* blob is packed */
454 memcpy(&iobj.id, id, sizeof(iobj.id));
462 if (imsg_compose(ibuf, GOT_IMSG_BLOB_REQUEST, 0, 0, infd, data, len)
464 err = got_error_from_errno("imsg_compose BLOB_REQUEST");
469 return flush_imsg(ibuf);
472 const struct got_error *
473 got_privsep_send_blob_outfd(struct imsgbuf *ibuf, int outfd)
475 const struct got_error *err = NULL;
477 if (imsg_compose(ibuf, GOT_IMSG_BLOB_OUTFD, 0, 0, outfd, NULL, 0)
479 err = got_error_from_errno("imsg_compose BLOB_OUTFD");
484 return flush_imsg(ibuf);
487 static const struct got_error *
488 send_fd(struct imsgbuf *ibuf, int imsg_code, int fd)
490 const struct got_error *err = NULL;
492 if (imsg_compose(ibuf, imsg_code, 0, 0, fd, NULL, 0) == -1) {
493 err = got_error_from_errno("imsg_compose TMPFD");
498 return flush_imsg(ibuf);
501 const struct got_error *
502 got_privsep_send_tmpfd(struct imsgbuf *ibuf, int fd)
504 return send_fd(ibuf, GOT_IMSG_TMPFD, fd);
507 const struct got_error *
508 got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj)
510 struct got_imsg_object iobj;
512 memset(&iobj, 0, sizeof(iobj));
514 memcpy(&iobj.id, &obj->id, sizeof(iobj.id));
515 iobj.type = obj->type;
516 iobj.flags = obj->flags;
517 iobj.hdrlen = obj->hdrlen;
518 iobj.size = obj->size;
519 if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
520 iobj.pack_offset = obj->pack_offset;
521 iobj.pack_idx = obj->pack_idx;
524 if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
526 return got_error_from_errno("imsg_compose OBJECT");
528 return flush_imsg(ibuf);
531 const struct got_error *
532 got_privsep_send_fetch_req(struct imsgbuf *ibuf, int fd,
533 struct got_pathlist_head *have_refs, int fetch_all_branches,
534 struct got_pathlist_head *wanted_branches,
535 struct got_pathlist_head *wanted_refs, int list_refs_only, int verbosity)
537 const struct got_error *err = NULL;
540 struct got_pathlist_entry *pe;
541 struct got_imsg_fetch_request fetchreq;
543 memset(&fetchreq, 0, sizeof(fetchreq));
544 fetchreq.fetch_all_branches = fetch_all_branches;
545 fetchreq.list_refs_only = list_refs_only;
546 fetchreq.verbosity = verbosity;
547 TAILQ_FOREACH(pe, have_refs, entry)
548 fetchreq.n_have_refs++;
549 TAILQ_FOREACH(pe, wanted_branches, entry)
550 fetchreq.n_wanted_branches++;
551 TAILQ_FOREACH(pe, wanted_refs, entry)
552 fetchreq.n_wanted_refs++;
553 len = sizeof(struct got_imsg_fetch_request);
554 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
556 return got_error(GOT_ERR_NO_SPACE);
559 if (imsg_compose(ibuf, GOT_IMSG_FETCH_REQUEST, 0, 0, fd,
560 &fetchreq, sizeof(fetchreq)) == -1)
561 return got_error_from_errno(
562 "imsg_compose FETCH_SERVER_PROGRESS");
564 err = flush_imsg(ibuf);
571 TAILQ_FOREACH(pe, have_refs, entry) {
572 const char *name = pe->path;
573 size_t name_len = pe->path_len;
574 struct got_object_id *id = pe->data;
576 len = sizeof(struct got_imsg_fetch_have_ref) + name_len;
577 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_HAVE_REF, 0, 0, len);
579 return got_error_from_errno("imsg_create FETCH_HAVE_REF");
581 /* Keep in sync with struct got_imsg_fetch_have_ref! */
582 if (imsg_add(wbuf, id, sizeof(*id)) == -1)
583 return got_error_from_errno("imsg_add FETCH_HAVE_REF");
584 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1)
585 return got_error_from_errno("imsg_add FETCH_HAVE_REF");
586 if (imsg_add(wbuf, name, name_len) == -1)
587 return got_error_from_errno("imsg_add FETCH_HAVE_REF");
590 imsg_close(ibuf, wbuf);
591 err = flush_imsg(ibuf);
596 TAILQ_FOREACH(pe, wanted_branches, entry) {
597 const char *name = pe->path;
598 size_t name_len = pe->path_len;
600 len = sizeof(struct got_imsg_fetch_wanted_branch) + name_len;
601 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_WANTED_BRANCH, 0, 0,
604 return got_error_from_errno(
605 "imsg_create FETCH_WANTED_BRANCH");
607 /* Keep in sync with struct got_imsg_fetch_wanted_branch! */
608 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1)
609 return got_error_from_errno(
610 "imsg_add FETCH_WANTED_BRANCH");
611 if (imsg_add(wbuf, name, name_len) == -1)
612 return got_error_from_errno(
613 "imsg_add FETCH_WANTED_BRANCH");
616 imsg_close(ibuf, wbuf);
617 err = flush_imsg(ibuf);
622 TAILQ_FOREACH(pe, wanted_refs, entry) {
623 const char *name = pe->path;
624 size_t name_len = pe->path_len;
626 len = sizeof(struct got_imsg_fetch_wanted_ref) + name_len;
627 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_WANTED_REF, 0, 0,
630 return got_error_from_errno(
631 "imsg_create FETCH_WANTED_REF");
633 /* Keep in sync with struct got_imsg_fetch_wanted_ref! */
634 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1)
635 return got_error_from_errno(
636 "imsg_add FETCH_WANTED_REF");
637 if (imsg_add(wbuf, name, name_len) == -1)
638 return got_error_from_errno(
639 "imsg_add FETCH_WANTED_REF");
642 imsg_close(ibuf, wbuf);
643 err = flush_imsg(ibuf);
653 const struct got_error *
654 got_privsep_send_fetch_outfd(struct imsgbuf *ibuf, int fd)
656 return send_fd(ibuf, GOT_IMSG_FETCH_OUTFD, fd);
659 const struct got_error *
660 got_privsep_recv_fetch_progress(int *done, struct got_object_id **id,
661 char **refname, struct got_pathlist_head *symrefs, char **server_progress,
662 off_t *packfile_size, uint8_t *pack_hash, struct imsgbuf *ibuf)
664 const struct got_error *err = NULL;
667 struct got_imsg_fetch_symrefs *isymrefs = NULL;
675 *server_progress = NULL;
677 memset(pack_hash, 0, SHA1_DIGEST_LENGTH);
679 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
683 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
684 switch (imsg.hdr.type) {
685 case GOT_IMSG_FETCH_SYMREFS:
686 if (datalen < sizeof(*isymrefs)) {
687 err = got_error(GOT_ERR_PRIVSEP_LEN);
690 if (isymrefs != NULL) {
691 err = got_error(GOT_ERR_PRIVSEP_MSG);
694 isymrefs = (struct got_imsg_fetch_symrefs *)imsg.data;
695 off = sizeof(*isymrefs);
696 remain = datalen - off;
697 for (n = 0; n < isymrefs->nsymrefs; n++) {
698 struct got_imsg_fetch_symref *s;
700 if (remain < sizeof(struct got_imsg_fetch_symref)) {
701 err = got_error(GOT_ERR_PRIVSEP_LEN);
704 s = (struct got_imsg_fetch_symref *)(imsg.data + off);
706 remain -= sizeof(*s);
707 if (remain < s->name_len) {
708 err = got_error(GOT_ERR_PRIVSEP_LEN);
711 name = strndup(imsg.data + off, s->name_len);
713 err = got_error_from_errno("strndup");
717 remain -= s->name_len;
718 if (remain < s->target_len) {
719 err = got_error(GOT_ERR_PRIVSEP_LEN);
723 target = strndup(imsg.data + off, s->target_len);
724 if (target == NULL) {
725 err = got_error_from_errno("strndup");
729 off += s->target_len;
730 remain -= s->target_len;
731 err = got_pathlist_append(symrefs, name, target);
739 case GOT_IMSG_FETCH_REF:
740 if (datalen <= sizeof(**id)) {
741 err = got_error(GOT_ERR_PRIVSEP_MSG);
744 *id = malloc(sizeof(**id));
746 err = got_error_from_errno("malloc");
749 memcpy(*id, imsg.data, sizeof(**id));
750 *refname = strndup(imsg.data + sizeof(**id),
751 datalen - sizeof(**id));
752 if (*refname == NULL) {
753 err = got_error_from_errno("strndup");
757 case GOT_IMSG_FETCH_SERVER_PROGRESS:
759 err = got_error(GOT_ERR_PRIVSEP_LEN);
762 *server_progress = strndup(imsg.data, datalen);
763 if (*server_progress == NULL) {
764 err = got_error_from_errno("strndup");
767 for (i = 0; i < datalen; i++) {
768 if (!isprint((unsigned char)(*server_progress)[i]) &&
769 !isspace((unsigned char)(*server_progress)[i])) {
770 err = got_error(GOT_ERR_PRIVSEP_MSG);
771 free(*server_progress);
772 *server_progress = NULL;
777 case GOT_IMSG_FETCH_DOWNLOAD_PROGRESS:
778 if (datalen < sizeof(*packfile_size)) {
779 err = got_error(GOT_ERR_PRIVSEP_MSG);
782 memcpy(packfile_size, imsg.data, sizeof(*packfile_size));
784 case GOT_IMSG_FETCH_DONE:
785 if (datalen != SHA1_DIGEST_LENGTH) {
786 err = got_error(GOT_ERR_PRIVSEP_MSG);
789 memcpy(pack_hash, imsg.data, SHA1_DIGEST_LENGTH);
793 err = got_error(GOT_ERR_PRIVSEP_MSG);
807 static const struct got_error *
808 send_send_ref(const char *name, size_t name_len, struct got_object_id *id,
809 int delete, struct imsgbuf *ibuf)
814 len = sizeof(struct got_imsg_send_ref) + name_len;
815 wbuf = imsg_create(ibuf, GOT_IMSG_SEND_REF, 0, 0, len);
817 return got_error_from_errno("imsg_create SEND_REF");
819 /* Keep in sync with struct got_imsg_send_ref! */
820 if (imsg_add(wbuf, id, sizeof(*id)) == -1)
821 return got_error_from_errno("imsg_add SEND_REF");
822 if (imsg_add(wbuf, &delete, sizeof(delete)) == -1)
823 return got_error_from_errno("imsg_add SEND_REF");
824 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1)
825 return got_error_from_errno("imsg_add SEND_REF");
826 if (imsg_add(wbuf, name, name_len) == -1)
827 return got_error_from_errno("imsg_add SEND_REF");
830 imsg_close(ibuf, wbuf);
831 return flush_imsg(ibuf);
834 const struct got_error *
835 got_privsep_send_send_req(struct imsgbuf *ibuf, int fd,
836 struct got_pathlist_head *have_refs,
837 struct got_pathlist_head *delete_refs,
840 const struct got_error *err = NULL;
841 struct got_pathlist_entry *pe;
842 struct got_imsg_send_request sendreq;
843 struct got_object_id zero_id;
845 memset(&zero_id, 0, sizeof(zero_id));
846 memset(&sendreq, 0, sizeof(sendreq));
847 sendreq.verbosity = verbosity;
848 TAILQ_FOREACH(pe, have_refs, entry)
850 TAILQ_FOREACH(pe, delete_refs, entry)
852 if (imsg_compose(ibuf, GOT_IMSG_SEND_REQUEST, 0, 0, fd,
853 &sendreq, sizeof(sendreq)) == -1) {
854 err = got_error_from_errno(
855 "imsg_compose FETCH_SERVER_PROGRESS");
859 err = flush_imsg(ibuf);
864 TAILQ_FOREACH(pe, have_refs, entry) {
865 const char *name = pe->path;
866 size_t name_len = pe->path_len;
867 struct got_object_id *id = pe->data;
868 err = send_send_ref(name, name_len, id, 0, ibuf);
873 TAILQ_FOREACH(pe, delete_refs, entry) {
874 const char *name = pe->path;
875 size_t name_len = pe->path_len;
876 err = send_send_ref(name, name_len, &zero_id, 1, ibuf);
881 if (fd != -1 && close(fd) == -1 && err == NULL)
882 err = got_error_from_errno("close");
887 const struct got_error *
888 got_privsep_recv_send_remote_refs(struct got_pathlist_head *remote_refs,
889 struct imsgbuf *ibuf)
891 const struct got_error *err = NULL;
895 struct got_imsg_send_remote_ref iremote_ref;
896 struct got_object_id *id = NULL;
897 char *refname = NULL;
898 struct got_pathlist_entry *new;
901 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
904 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
905 switch (imsg.hdr.type) {
906 case GOT_IMSG_SEND_REMOTE_REF:
907 if (datalen < sizeof(iremote_ref)) {
908 err = got_error(GOT_ERR_PRIVSEP_MSG);
911 memcpy(&iremote_ref, imsg.data, sizeof(iremote_ref));
912 if (datalen != sizeof(iremote_ref) +
913 iremote_ref.name_len) {
914 err = got_error(GOT_ERR_PRIVSEP_MSG);
917 id = malloc(sizeof(*id));
919 err = got_error_from_errno("malloc");
922 memcpy(id, &iremote_ref.id, sizeof(*id));
923 refname = strndup(imsg.data + sizeof(iremote_ref),
924 datalen - sizeof(iremote_ref));
925 if (refname == NULL) {
926 err = got_error_from_errno("strndup");
929 err = got_pathlist_insert(&new, remote_refs,
933 if (new == NULL) { /* duplicate which wasn't inserted */
940 case GOT_IMSG_SEND_PACK_REQUEST:
942 err = got_error(GOT_ERR_PRIVSEP_MSG);
945 /* got-send-pack is now waiting for a pack file. */
949 err = got_error(GOT_ERR_PRIVSEP_MSG);
960 const struct got_error *
961 got_privsep_send_packfd(struct imsgbuf *ibuf, int fd)
963 return send_fd(ibuf, GOT_IMSG_SEND_PACKFD, fd);
966 const struct got_error *
967 got_privsep_recv_send_progress(int *done, off_t *bytes_sent,
968 int *success, char **refname, char **errmsg, struct imsgbuf *ibuf)
970 const struct got_error *err = NULL;
973 struct got_imsg_send_ref_status iref_status;
975 /* Do not reset the current value of 'bytes_sent', it accumulates. */
981 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
985 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
986 switch (imsg.hdr.type) {
987 case GOT_IMSG_SEND_UPLOAD_PROGRESS:
988 if (datalen < sizeof(*bytes_sent)) {
989 err = got_error(GOT_ERR_PRIVSEP_MSG);
992 memcpy(bytes_sent, imsg.data, sizeof(*bytes_sent));
994 case GOT_IMSG_SEND_REF_STATUS:
995 if (datalen < sizeof(iref_status)) {
996 err = got_error(GOT_ERR_PRIVSEP_MSG);
999 memcpy(&iref_status, imsg.data, sizeof(iref_status));
1000 if (datalen != sizeof(iref_status) + iref_status.name_len +
1001 iref_status.errmsg_len) {
1002 err = got_error(GOT_ERR_PRIVSEP_MSG);
1005 *success = iref_status.success;
1006 *refname = strndup(imsg.data + sizeof(iref_status),
1007 iref_status.name_len);
1009 if (iref_status.errmsg_len != 0)
1010 *errmsg = strndup(imsg.data + sizeof(iref_status) +
1011 iref_status.name_len, iref_status.errmsg_len);
1013 case GOT_IMSG_SEND_DONE:
1015 err = got_error(GOT_ERR_PRIVSEP_MSG);
1021 err = got_error(GOT_ERR_PRIVSEP_MSG);
1029 const struct got_error *
1030 got_privsep_send_index_pack_req(struct imsgbuf *ibuf, uint8_t *pack_hash,
1033 const struct got_error *err = NULL;
1035 /* Keep in sync with struct got_imsg_index_pack_request */
1036 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_REQUEST, 0, 0, fd,
1037 pack_hash, SHA1_DIGEST_LENGTH) == -1) {
1038 err = got_error_from_errno("imsg_compose INDEX_REQUEST");
1042 return flush_imsg(ibuf);
1045 const struct got_error *
1046 got_privsep_send_index_pack_outfd(struct imsgbuf *ibuf, int fd)
1048 return send_fd(ibuf, GOT_IMSG_IDXPACK_OUTFD, fd);
1051 const struct got_error *
1052 got_privsep_recv_index_progress(int *done, int *nobj_total,
1053 int *nobj_indexed, int *nobj_loose, int *nobj_resolved,
1054 struct imsgbuf *ibuf)
1056 const struct got_error *err = NULL;
1058 struct got_imsg_index_pack_progress *iprogress;
1066 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1070 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1071 switch (imsg.hdr.type) {
1072 case GOT_IMSG_IDXPACK_PROGRESS:
1073 if (datalen < sizeof(*iprogress)) {
1074 err = got_error(GOT_ERR_PRIVSEP_LEN);
1077 iprogress = (struct got_imsg_index_pack_progress *)imsg.data;
1078 if (iprogress->nobj_total < 0 || iprogress->nobj_indexed < 0 ||
1079 iprogress->nobj_loose < 0 || iprogress->nobj_resolved < 0) {
1080 err = got_error(GOT_ERR_RANGE);
1083 *nobj_total = iprogress->nobj_total;
1084 *nobj_indexed = iprogress->nobj_indexed;
1085 *nobj_loose = iprogress->nobj_loose;
1086 *nobj_resolved = iprogress->nobj_resolved;
1088 case GOT_IMSG_IDXPACK_DONE:
1090 err = got_error(GOT_ERR_PRIVSEP_LEN);
1096 err = got_error(GOT_ERR_PRIVSEP_MSG);
1104 const struct got_error *
1105 got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
1106 struct imsgbuf *ibuf)
1108 struct got_imsg_object *iobj;
1109 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1111 if (datalen != sizeof(*iobj))
1112 return got_error(GOT_ERR_PRIVSEP_LEN);
1115 if (iobj->pack_offset < 0)
1116 return got_error(GOT_ERR_PACK_OFFSET);
1118 *obj = calloc(1, sizeof(**obj));
1120 return got_error_from_errno("calloc");
1122 memcpy(&(*obj)->id, &iobj->id, sizeof(iobj->id));
1123 (*obj)->type = iobj->type;
1124 (*obj)->flags = iobj->flags;
1125 (*obj)->hdrlen = iobj->hdrlen;
1126 (*obj)->size = iobj->size;
1127 /* path_packfile is handled by caller */
1128 if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
1129 (*obj)->pack_offset = iobj->pack_offset;
1130 (*obj)->pack_idx = iobj->pack_idx;
1132 STAILQ_INIT(&(*obj)->deltas.entries);
1136 const struct got_error *
1137 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
1139 const struct got_error *err = NULL;
1141 const size_t min_datalen =
1142 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
1146 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1150 switch (imsg.hdr.type) {
1151 case GOT_IMSG_OBJECT:
1152 err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
1155 err = got_error(GOT_ERR_PRIVSEP_MSG);
1164 static const struct got_error *
1165 send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
1168 const struct got_error *err = NULL;
1169 size_t offset, remain;
1172 remain = logmsg_len;
1173 while (remain > 0) {
1174 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1176 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
1177 commit->logmsg + offset, n) == -1) {
1178 err = got_error_from_errno("imsg_compose "
1183 err = flush_imsg(ibuf);
1194 const struct got_error *
1195 got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
1197 const struct got_error *err = NULL;
1198 struct got_imsg_commit_object *icommit;
1201 struct got_object_qid *qid;
1202 size_t author_len = strlen(commit->author);
1203 size_t committer_len = strlen(commit->committer);
1204 size_t logmsg_len = strlen(commit->logmsg);
1206 total = sizeof(*icommit) + author_len + committer_len +
1207 commit->nparents * sizeof(struct got_object_id);
1209 buf = malloc(total);
1211 return got_error_from_errno("malloc");
1213 icommit = (struct got_imsg_commit_object *)buf;
1214 memcpy(&icommit->tree_id, commit->tree_id, sizeof(icommit->tree_id));
1215 icommit->author_len = author_len;
1216 icommit->author_time = commit->author_time;
1217 icommit->author_gmtoff = commit->author_gmtoff;
1218 icommit->committer_len = committer_len;
1219 icommit->committer_time = commit->committer_time;
1220 icommit->committer_gmtoff = commit->committer_gmtoff;
1221 icommit->logmsg_len = logmsg_len;
1222 icommit->nparents = commit->nparents;
1224 len = sizeof(*icommit);
1225 memcpy(buf + len, commit->author, author_len);
1227 memcpy(buf + len, commit->committer, committer_len);
1228 len += committer_len;
1229 STAILQ_FOREACH(qid, &commit->parent_ids, entry) {
1230 memcpy(buf + len, &qid->id, sizeof(qid->id));
1231 len += sizeof(qid->id);
1234 if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
1235 err = got_error_from_errno("imsg_compose COMMIT");
1239 if (logmsg_len == 0 ||
1240 logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1241 err = flush_imsg(ibuf);
1245 err = send_commit_logmsg(ibuf, commit, logmsg_len);
1251 static const struct got_error *
1252 get_commit_from_imsg(struct got_commit_object **commit,
1253 struct imsg *imsg, size_t datalen, struct imsgbuf *ibuf)
1255 const struct got_error *err = NULL;
1256 struct got_imsg_commit_object *icommit;
1260 if (datalen < sizeof(*icommit))
1261 return got_error(GOT_ERR_PRIVSEP_LEN);
1263 icommit = imsg->data;
1264 if (datalen != sizeof(*icommit) + icommit->author_len +
1265 icommit->committer_len +
1266 icommit->nparents * sizeof(struct got_object_id))
1267 return got_error(GOT_ERR_PRIVSEP_LEN);
1269 if (icommit->nparents < 0)
1270 return got_error(GOT_ERR_PRIVSEP_LEN);
1272 len += sizeof(*icommit);
1274 *commit = got_object_commit_alloc_partial();
1275 if (*commit == NULL)
1276 return got_error_from_errno(
1277 "got_object_commit_alloc_partial");
1279 memcpy((*commit)->tree_id, &icommit->tree_id,
1280 sizeof(icommit->tree_id));
1281 (*commit)->author_time = icommit->author_time;
1282 (*commit)->author_gmtoff = icommit->author_gmtoff;
1283 (*commit)->committer_time = icommit->committer_time;
1284 (*commit)->committer_gmtoff = icommit->committer_gmtoff;
1286 (*commit)->author = strndup(imsg->data + len, icommit->author_len);
1287 if ((*commit)->author == NULL) {
1288 err = got_error_from_errno("strndup");
1291 len += icommit->author_len;
1293 (*commit)->committer = strndup(imsg->data + len,
1294 icommit->committer_len);
1295 if ((*commit)->committer == NULL) {
1296 err = got_error_from_errno("strndup");
1299 len += icommit->committer_len;
1301 if (icommit->logmsg_len == 0) {
1302 (*commit)->logmsg = strdup("");
1303 if ((*commit)->logmsg == NULL) {
1304 err = got_error_from_errno("strdup");
1308 size_t offset = 0, remain = icommit->logmsg_len;
1310 (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
1311 if ((*commit)->logmsg == NULL) {
1312 err = got_error_from_errno("malloc");
1315 while (remain > 0) {
1316 struct imsg imsg_log;
1317 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1320 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1324 if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG) {
1325 err = got_error(GOT_ERR_PRIVSEP_MSG);
1329 memcpy((*commit)->logmsg + offset,
1331 imsg_free(&imsg_log);
1335 (*commit)->logmsg[icommit->logmsg_len] = '\0';
1338 for (i = 0; i < icommit->nparents; i++) {
1339 struct got_object_qid *qid;
1341 err = got_object_qid_alloc_partial(&qid);
1344 memcpy(&qid->id, imsg->data + len +
1345 i * sizeof(qid->id), sizeof(qid->id));
1346 STAILQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
1347 (*commit)->nparents++;
1351 got_object_commit_close(*commit);
1357 const struct got_error *
1358 got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
1360 const struct got_error *err = NULL;
1363 const size_t min_datalen =
1364 MIN(sizeof(struct got_imsg_error),
1365 sizeof(struct got_imsg_commit_object));
1369 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1373 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1375 switch (imsg.hdr.type) {
1376 case GOT_IMSG_COMMIT:
1377 err = get_commit_from_imsg(commit, &imsg, datalen, ibuf);
1380 err = got_error(GOT_ERR_PRIVSEP_MSG);
1389 static const struct got_error *
1390 send_tree_entries_batch(struct imsgbuf *ibuf,
1391 struct got_parsed_tree_entry *entries, int idx0, int idxN, size_t len)
1394 struct got_imsg_tree_entries ientries;
1397 memset(&ientries, 0, sizeof(ientries));
1399 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_ENTRIES, 0, 0, len);
1401 return got_error_from_errno("imsg_create TREE_ENTRY");
1403 ientries.nentries = idxN - idx0 + 1;
1404 if (imsg_add(wbuf, &ientries, sizeof(ientries)) == -1)
1405 return got_error_from_errno("imsg_add TREE_ENTRY");
1407 for (i = idx0; i <= idxN; i++) {
1408 static const char gap[12]; /* for sha1-inside-sha256 alignment */
1409 struct got_parsed_tree_entry *pte = &entries[i];
1410 int algo = GOT_HASH_SHA256;
1412 if (pte->idlen != SHA256_DIGEST_LENGTH)
1413 algo = GOT_HASH_SHA1;
1415 /* Keep in sync with struct got_imsg_tree_entry definition! */
1416 if (imsg_add(wbuf, pte->id, pte->idlen) == -1)
1417 return got_error_from_errno("imsg_add TREE_ENTRY");
1418 if (pte->idlen != SHA256_DIGEST_LENGTH &&
1419 imsg_add(wbuf, gap, sizeof(gap)) == -1)
1420 return got_error_from_errno("imsg_add TREE_ENTRY");
1421 if (imsg_add(wbuf, &algo, sizeof(algo)) == -1)
1422 return got_error_from_errno("imsg_add TREE_ENTRY");
1423 if (imsg_add(wbuf, &pte->mode, sizeof(pte->mode)) == -1)
1424 return got_error_from_errno("imsg_add TREE_ENTRY");
1425 if (imsg_add(wbuf, &pte->namelen, sizeof(pte->namelen)) == -1)
1426 return got_error_from_errno("imsg_add TREE_ENTRY");
1428 /* Remaining bytes are the id and entry's name. */
1429 if (imsg_add(wbuf, pte->name, pte->namelen) == -1)
1430 return got_error_from_errno("imsg_add TREE_ENTRY");
1434 imsg_close(ibuf, wbuf);
1438 static const struct got_error *
1439 send_tree_entries(struct imsgbuf *ibuf, struct got_parsed_tree_entry *entries,
1442 const struct got_error *err = NULL;
1444 size_t entries_len = sizeof(struct got_imsg_tree_entries);
1447 for (j = 0; j < nentries; j++) {
1448 struct got_parsed_tree_entry *pte = &entries[j];
1449 size_t len = SHA256_DIGEST_LENGTH + sizeof(pte->mode) +
1450 sizeof(pte->namelen) + pte->namelen;
1453 entries_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1454 err = send_tree_entries_batch(ibuf, entries,
1455 i, j - 1, entries_len);
1459 entries_len = sizeof(struct got_imsg_tree_entries);
1466 err = send_tree_entries_batch(ibuf, entries, i, j - 1,
1475 const struct got_error *
1476 got_privsep_send_tree(struct imsgbuf *ibuf,
1477 struct got_parsed_tree_entry *entries, int nentries)
1479 const struct got_error *err = NULL;
1480 struct got_imsg_tree_object itree;
1482 memset(&itree, 0, sizeof(itree));
1483 itree.nentries = nentries;
1484 if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
1486 return got_error_from_errno("imsg_compose TREE");
1488 err = send_tree_entries(ibuf, entries, nentries);
1492 return flush_imsg(ibuf);
1496 static const struct got_error *
1497 recv_tree_entries(void *data, size_t datalen, struct got_tree_object *tree,
1500 const struct got_error *err = NULL;
1501 struct got_imsg_tree_entries *ientries;
1502 struct got_tree_entry *te;
1506 if (datalen <= sizeof(*ientries) ||
1507 datalen > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
1508 return got_error(GOT_ERR_PRIVSEP_LEN);
1510 ientries = (struct got_imsg_tree_entries *)data;
1511 if (ientries->nentries > INT_MAX) {
1512 return got_error_msg(GOT_ERR_NO_SPACE,
1513 "too many tree entries");
1516 te_offset = sizeof(*ientries);
1517 for (i = 0; i < ientries->nentries; i++) {
1518 struct got_imsg_tree_entry ite;
1519 const char *te_name;
1520 uint8_t *buf = (uint8_t *)data + te_offset;
1522 if (te_offset >= datalen) {
1523 err = got_error(GOT_ERR_PRIVSEP_LEN);
1527 /* Might not be aligned, size is ~32 bytes. */
1528 memcpy(&ite, buf, sizeof(ite));
1530 if (ite.namelen >= sizeof(te->name)) {
1531 err = got_error(GOT_ERR_PRIVSEP_LEN);
1534 if (te_offset + sizeof(ite) + ite.namelen > datalen) {
1535 err = got_error(GOT_ERR_PRIVSEP_LEN);
1539 if (*nentries >= tree->nentries) {
1540 err = got_error(GOT_ERR_PRIVSEP_LEN);
1543 te = &tree->entries[*nentries];
1544 te_name = buf + sizeof(ite);
1545 memcpy(te->name, te_name, ite.namelen);
1546 te->name[ite.namelen] = '\0';
1547 memcpy(te->id.hash, ite.id, sizeof(te->id.hash));
1548 te->id.algo = ite.algo;
1549 te->mode = ite.mode;
1550 te->idx = *nentries;
1553 te_offset += sizeof(ite) + ite.namelen;
1559 const struct got_error *
1560 got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
1562 const struct got_error *err = NULL;
1563 const size_t min_datalen =
1564 MIN(sizeof(struct got_imsg_error),
1565 sizeof(struct got_imsg_tree_object));
1566 struct got_imsg_tree_object *itree;
1571 while (*tree == NULL || nentries < (*tree)->nentries) {
1575 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1579 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1581 switch (imsg.hdr.type) {
1583 /* This message should only appear once. */
1584 if (*tree != NULL) {
1585 err = got_error(GOT_ERR_PRIVSEP_MSG);
1588 if (datalen != sizeof(*itree)) {
1589 err = got_error(GOT_ERR_PRIVSEP_LEN);
1593 if (itree->nentries < 0) {
1594 err = got_error(GOT_ERR_PRIVSEP_LEN);
1597 *tree = malloc(sizeof(**tree));
1598 if (*tree == NULL) {
1599 err = got_error_from_errno("malloc");
1602 (*tree)->entries = calloc(itree->nentries,
1603 sizeof(struct got_tree_entry));
1604 if ((*tree)->entries == NULL) {
1605 err = got_error_from_errno("malloc");
1610 (*tree)->nentries = itree->nentries;
1611 (*tree)->refcnt = 0;
1613 case GOT_IMSG_TREE_ENTRIES:
1614 /* This message should be preceeded by GOT_IMSG_TREE. */
1615 if (*tree == NULL) {
1616 err = got_error(GOT_ERR_PRIVSEP_MSG);
1619 err = recv_tree_entries(imsg.data, datalen,
1623 err = got_error(GOT_ERR_PRIVSEP_MSG);
1632 if (*tree && (*tree)->nentries != nentries) {
1634 err = got_error(GOT_ERR_PRIVSEP_LEN);
1635 got_object_tree_close(*tree);
1642 const struct got_error *
1643 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
1644 const uint8_t *data)
1646 struct got_imsg_blob iblob;
1648 memset(&iblob, 0, sizeof(iblob));
1650 iblob.hdrlen = hdrlen;
1655 if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
1656 return got_error(GOT_ERR_NO_SPACE);
1658 buf = malloc(sizeof(iblob) + size);
1660 return got_error_from_errno("malloc");
1662 memcpy(buf, &iblob, sizeof(iblob));
1663 memcpy(buf + sizeof(iblob), data, size);
1664 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
1665 sizeof(iblob) + size) == -1) {
1667 return got_error_from_errno("imsg_compose BLOB");
1671 /* Data has already been written to file descriptor. */
1672 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
1673 sizeof(iblob)) == -1)
1674 return got_error_from_errno("imsg_compose BLOB");
1678 return flush_imsg(ibuf);
1681 const struct got_error *
1682 got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1683 struct imsgbuf *ibuf)
1685 const struct got_error *err = NULL;
1687 struct got_imsg_blob *iblob;
1692 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1696 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1698 switch (imsg.hdr.type) {
1700 if (datalen < sizeof(*iblob)) {
1701 err = got_error(GOT_ERR_PRIVSEP_LEN);
1705 *size = iblob->size;
1706 *hdrlen = iblob->hdrlen;
1708 if (datalen == sizeof(*iblob)) {
1709 /* Data has been written to file descriptor. */
1713 if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX ||
1714 *size > datalen + sizeof(*iblob)) {
1715 err = got_error(GOT_ERR_PRIVSEP_LEN);
1719 *outbuf = malloc(*size);
1720 if (*outbuf == NULL) {
1721 err = got_error_from_errno("malloc");
1724 memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
1727 err = got_error(GOT_ERR_PRIVSEP_MSG);
1736 static const struct got_error *
1737 send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
1739 const struct got_error *err = NULL;
1740 size_t offset, remain;
1743 remain = tagmsg_len;
1744 while (remain > 0) {
1745 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1747 if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
1748 tag->tagmsg + offset, n) == -1) {
1749 err = got_error_from_errno("imsg_compose TAG_TAGMSG");
1753 err = flush_imsg(ibuf);
1764 const struct got_error *
1765 got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
1767 const struct got_error *err = NULL;
1768 struct got_imsg_tag_object *itag;
1771 size_t tag_len = strlen(tag->tag);
1772 size_t tagger_len = strlen(tag->tagger);
1773 size_t tagmsg_len = strlen(tag->tagmsg);
1775 total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
1777 buf = malloc(total);
1779 return got_error_from_errno("malloc");
1781 itag = (struct got_imsg_tag_object *)buf;
1782 memcpy(itag->id, tag->id.hash, sizeof(itag->id));
1783 itag->obj_type = tag->obj_type;
1784 itag->tag_len = tag_len;
1785 itag->tagger_len = tagger_len;
1786 itag->tagger_time = tag->tagger_time;
1787 itag->tagger_gmtoff = tag->tagger_gmtoff;
1788 itag->tagmsg_len = tagmsg_len;
1790 len = sizeof(*itag);
1791 memcpy(buf + len, tag->tag, tag_len);
1793 memcpy(buf + len, tag->tagger, tagger_len);
1796 if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
1797 err = got_error_from_errno("imsg_compose TAG");
1801 if (tagmsg_len == 0 ||
1802 tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1803 err = flush_imsg(ibuf);
1807 err = send_tagmsg(ibuf, tag, tagmsg_len);
1813 const struct got_error *
1814 got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
1816 const struct got_error *err = NULL;
1818 struct got_imsg_tag_object *itag;
1819 size_t len, datalen;
1820 const size_t min_datalen =
1821 MIN(sizeof(struct got_imsg_error),
1822 sizeof(struct got_imsg_tag_object));
1826 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1830 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1833 switch (imsg.hdr.type) {
1835 if (datalen < sizeof(*itag)) {
1836 err = got_error(GOT_ERR_PRIVSEP_LEN);
1840 if (datalen != sizeof(*itag) + itag->tag_len +
1842 err = got_error(GOT_ERR_PRIVSEP_LEN);
1845 len += sizeof(*itag);
1847 *tag = calloc(1, sizeof(**tag));
1849 err = got_error_from_errno("calloc");
1853 memcpy((*tag)->id.hash, itag->id, SHA1_DIGEST_LENGTH);
1855 (*tag)->tag = strndup(imsg.data + len, itag->tag_len);
1856 if ((*tag)->tag == NULL) {
1857 err = got_error_from_errno("strndup");
1860 len += itag->tag_len;
1862 (*tag)->obj_type = itag->obj_type;
1863 (*tag)->tagger_time = itag->tagger_time;
1864 (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1866 (*tag)->tagger = strndup(imsg.data + len, itag->tagger_len);
1867 if ((*tag)->tagger == NULL) {
1868 err = got_error_from_errno("strndup");
1871 len += itag->tagger_len;
1873 if (itag->tagmsg_len == 0) {
1874 (*tag)->tagmsg = strdup("");
1875 if ((*tag)->tagmsg == NULL) {
1876 err = got_error_from_errno("strdup");
1880 size_t offset = 0, remain = itag->tagmsg_len;
1882 (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1883 if ((*tag)->tagmsg == NULL) {
1884 err = got_error_from_errno("malloc");
1887 while (remain > 0) {
1888 struct imsg imsg_log;
1889 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1892 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1896 if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1897 return got_error(GOT_ERR_PRIVSEP_MSG);
1899 memcpy((*tag)->tagmsg + offset, imsg_log.data,
1901 imsg_free(&imsg_log);
1905 (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1910 err = got_error(GOT_ERR_PRIVSEP_MSG);
1919 const struct got_error *
1920 got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1921 struct got_packidx *packidx)
1923 const struct got_error *err = NULL;
1924 struct got_imsg_packidx ipackidx;
1925 struct got_imsg_pack ipack;
1928 memset(&ipackidx, 0, sizeof(ipackidx));
1929 memset(&ipack, 0, sizeof(ipack));
1931 ipackidx.len = packidx->len;
1932 ipackidx.packfile_size = pack->filesize;
1933 fd = dup(packidx->fd);
1935 return got_error_from_errno("dup");
1937 if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
1938 sizeof(ipackidx)) == -1) {
1939 err = got_error_from_errno("imsg_compose PACKIDX");
1944 if (strlcpy(ipack.path_packfile, pack->path_packfile,
1945 sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
1946 return got_error(GOT_ERR_NO_SPACE);
1947 ipack.filesize = pack->filesize;
1951 return got_error_from_errno("dup");
1953 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
1955 err = got_error_from_errno("imsg_compose PACK");
1960 return flush_imsg(ibuf);
1963 const struct got_error *
1964 got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
1965 struct got_object_id *id)
1967 struct got_imsg_packed_object iobj;
1969 memset(&iobj, 0, sizeof(iobj));
1971 memcpy(&iobj.id, id, sizeof(iobj.id));
1973 if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
1974 &iobj, sizeof(iobj)) == -1)
1975 return got_error_from_errno("imsg_compose "
1976 "PACKED_OBJECT_REQUEST");
1978 return flush_imsg(ibuf);
1981 const struct got_error *
1982 got_privsep_send_packed_raw_obj_req(struct imsgbuf *ibuf, int idx,
1983 struct got_object_id *id)
1985 struct got_imsg_packed_object iobj;
1987 memset(&iobj, 0, sizeof(iobj));
1989 memcpy(&iobj.id, id, sizeof(iobj.id));
1991 if (imsg_compose(ibuf, GOT_IMSG_PACKED_RAW_OBJECT_REQUEST, 0, 0, -1,
1992 &iobj, sizeof(iobj)) == -1)
1993 return got_error_from_errno("imsg_compose "
1994 "PACKED_OBJECT_REQUEST");
1996 return flush_imsg(ibuf);
1999 const struct got_error *
2000 got_privsep_send_gitconfig_parse_req(struct imsgbuf *ibuf, int fd)
2002 const struct got_error *err = NULL;
2004 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_PARSE_REQUEST, 0, 0, fd,
2006 err = got_error_from_errno("imsg_compose "
2007 "GITCONFIG_PARSE_REQUEST");
2012 return flush_imsg(ibuf);
2015 const struct got_error *
2016 got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *ibuf)
2018 if (imsg_compose(ibuf,
2019 GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST, 0, 0, -1,
2021 return got_error_from_errno("imsg_compose "
2022 "GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST");
2024 return flush_imsg(ibuf);
2027 const struct got_error *
2028 got_privsep_send_gitconfig_repository_extensions_req(struct imsgbuf *ibuf)
2030 if (imsg_compose(ibuf,
2031 GOT_IMSG_GITCONFIG_REPOSITORY_EXTENSIONS_REQUEST, 0, 0, -1,
2033 return got_error_from_errno("imsg_compose "
2034 "GITCONFIG_REPOSITORY_EXTENSIONS_REQUEST");
2036 return flush_imsg(ibuf);
2040 const struct got_error *
2041 got_privsep_send_gitconfig_author_name_req(struct imsgbuf *ibuf)
2043 if (imsg_compose(ibuf,
2044 GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST, 0, 0, -1, NULL, 0) == -1)
2045 return got_error_from_errno("imsg_compose "
2046 "GITCONFIG_AUTHOR_NAME_REQUEST");
2048 return flush_imsg(ibuf);
2051 const struct got_error *
2052 got_privsep_send_gitconfig_author_email_req(struct imsgbuf *ibuf)
2054 if (imsg_compose(ibuf,
2055 GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST, 0, 0, -1, NULL, 0) == -1)
2056 return got_error_from_errno("imsg_compose "
2057 "GITCONFIG_AUTHOR_EMAIL_REQUEST");
2059 return flush_imsg(ibuf);
2062 const struct got_error *
2063 got_privsep_send_gitconfig_remotes_req(struct imsgbuf *ibuf)
2065 if (imsg_compose(ibuf,
2066 GOT_IMSG_GITCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
2067 return got_error_from_errno("imsg_compose "
2068 "GITCONFIG_REMOTE_REQUEST");
2070 return flush_imsg(ibuf);
2073 const struct got_error *
2074 got_privsep_send_gitconfig_owner_req(struct imsgbuf *ibuf)
2076 if (imsg_compose(ibuf,
2077 GOT_IMSG_GITCONFIG_OWNER_REQUEST, 0, 0, -1, NULL, 0) == -1)
2078 return got_error_from_errno("imsg_compose "
2079 "GITCONFIG_OWNER_REQUEST");
2081 return flush_imsg(ibuf);
2084 const struct got_error *
2085 got_privsep_recv_gitconfig_str(char **str, struct imsgbuf *ibuf)
2087 const struct got_error *err = NULL;
2093 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2096 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2098 switch (imsg.hdr.type) {
2099 case GOT_IMSG_GITCONFIG_STR_VAL:
2102 *str = strndup(imsg.data, datalen);
2104 err = got_error_from_errno("strndup");
2109 err = got_error(GOT_ERR_PRIVSEP_MSG);
2117 const struct got_error *
2118 got_privsep_recv_gitconfig_int(int *val, struct imsgbuf *ibuf)
2120 const struct got_error *err = NULL;
2123 const size_t min_datalen =
2124 MIN(sizeof(struct got_imsg_error), sizeof(int));
2128 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2131 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2133 switch (imsg.hdr.type) {
2134 case GOT_IMSG_GITCONFIG_INT_VAL:
2135 if (datalen != sizeof(*val)) {
2136 err = got_error(GOT_ERR_PRIVSEP_LEN);
2139 memcpy(val, imsg.data, sizeof(*val));
2142 err = got_error(GOT_ERR_PRIVSEP_MSG);
2151 free_remote_data(struct got_remote_repo *remote)
2156 free(remote->fetch_url);
2157 free(remote->send_url);
2158 for (i = 0; i < remote->nfetch_branches; i++)
2159 free(remote->fetch_branches[i]);
2160 free(remote->fetch_branches);
2161 for (i = 0; i < remote->nsend_branches; i++)
2162 free(remote->send_branches[i]);
2163 free(remote->send_branches);
2164 for (i = 0; i < remote->nfetch_refs; i++)
2165 free(remote->fetch_refs[i]);
2166 free(remote->fetch_refs);
2169 const struct got_error *
2170 got_privsep_recv_gitconfig_remotes(struct got_remote_repo **remotes,
2171 int *nremotes, struct imsgbuf *ibuf)
2173 const struct got_error *err = NULL;
2176 struct got_imsg_remotes iremotes;
2177 struct got_imsg_remote iremote;
2181 iremotes.nremotes = 0;
2183 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremotes));
2186 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2188 switch (imsg.hdr.type) {
2189 case GOT_IMSG_GITCONFIG_REMOTES:
2190 if (datalen != sizeof(iremotes)) {
2191 err = got_error(GOT_ERR_PRIVSEP_LEN);
2194 memcpy(&iremotes, imsg.data, sizeof(iremotes));
2195 if (iremotes.nremotes == 0) {
2202 return got_error(GOT_ERR_PRIVSEP_MSG);
2207 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(**remotes));
2208 if (*remotes == NULL)
2209 return got_error_from_errno("recallocarray");
2211 while (*nremotes < iremotes.nremotes) {
2212 struct got_remote_repo *remote;
2214 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremote));
2217 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2219 switch (imsg.hdr.type) {
2220 case GOT_IMSG_GITCONFIG_REMOTE:
2221 remote = &(*remotes)[*nremotes];
2222 memset(remote, 0, sizeof(*remote));
2223 if (datalen < sizeof(iremote)) {
2224 err = got_error(GOT_ERR_PRIVSEP_LEN);
2227 memcpy(&iremote, imsg.data, sizeof(iremote));
2228 if (iremote.name_len == 0 ||
2229 iremote.fetch_url_len == 0 ||
2230 iremote.send_url_len == 0 ||
2231 (sizeof(iremote) + iremote.name_len +
2232 iremote.fetch_url_len + iremote.send_url_len) > datalen) {
2233 err = got_error(GOT_ERR_PRIVSEP_LEN);
2236 remote->name = strndup(imsg.data + sizeof(iremote),
2238 if (remote->name == NULL) {
2239 err = got_error_from_errno("strndup");
2242 remote->fetch_url = strndup(imsg.data + sizeof(iremote) +
2243 iremote.name_len, iremote.fetch_url_len);
2244 if (remote->fetch_url == NULL) {
2245 err = got_error_from_errno("strndup");
2246 free_remote_data(remote);
2249 remote->send_url = strndup(imsg.data + sizeof(iremote) +
2250 iremote.name_len + iremote.fetch_url_len,
2251 iremote.send_url_len);
2252 if (remote->send_url == NULL) {
2253 err = got_error_from_errno("strndup");
2254 free_remote_data(remote);
2257 remote->mirror_references = iremote.mirror_references;
2258 remote->fetch_all_branches = iremote.fetch_all_branches;
2259 remote->nfetch_branches = 0;
2260 remote->fetch_branches = NULL;
2261 remote->nsend_branches = 0;
2262 remote->send_branches = NULL;
2263 remote->nfetch_refs = 0;
2264 remote->fetch_refs = NULL;
2268 err = got_error(GOT_ERR_PRIVSEP_MSG);
2279 for (i = 0; i < *nremotes; i++)
2280 free_remote_data(&(*remotes)[i]);
2288 const struct got_error *
2289 got_privsep_send_gotconfig_parse_req(struct imsgbuf *ibuf, int fd)
2291 const struct got_error *err = NULL;
2293 if (imsg_compose(ibuf, GOT_IMSG_GOTCONFIG_PARSE_REQUEST, 0, 0, fd,
2295 err = got_error_from_errno("imsg_compose "
2296 "GOTCONFIG_PARSE_REQUEST");
2301 return flush_imsg(ibuf);
2304 const struct got_error *
2305 got_privsep_send_gotconfig_author_req(struct imsgbuf *ibuf)
2307 if (imsg_compose(ibuf,
2308 GOT_IMSG_GOTCONFIG_AUTHOR_REQUEST, 0, 0, -1, NULL, 0) == -1)
2309 return got_error_from_errno("imsg_compose "
2310 "GOTCONFIG_AUTHOR_REQUEST");
2312 return flush_imsg(ibuf);
2315 const struct got_error *
2316 got_privsep_send_gotconfig_allowed_signers_req(struct imsgbuf *ibuf)
2318 if (imsg_compose(ibuf,
2319 GOT_IMSG_GOTCONFIG_ALLOWEDSIGNERS_REQUEST, 0, 0, -1, NULL, 0) == -1)
2320 return got_error_from_errno("imsg_compose "
2321 "GOTCONFIG_ALLOWEDSIGNERS_REQUEST");
2323 return flush_imsg(ibuf);
2326 const struct got_error *
2327 got_privsep_send_gotconfig_revoked_signers_req(struct imsgbuf *ibuf)
2329 if (imsg_compose(ibuf,
2330 GOT_IMSG_GOTCONFIG_REVOKEDSIGNERS_REQUEST, 0, 0, -1, NULL, 0) == -1)
2331 return got_error_from_errno("imsg_compose "
2332 "GOTCONFIG_REVOKEDSIGNERS_REQUEST");
2334 return flush_imsg(ibuf);
2337 const struct got_error *
2338 got_privsep_send_gotconfig_signer_id_req(struct imsgbuf *ibuf)
2340 if (imsg_compose(ibuf,
2341 GOT_IMSG_GOTCONFIG_SIGNERID_REQUEST, 0, 0, -1, NULL, 0) == -1)
2342 return got_error_from_errno("imsg_compose "
2343 "GOTCONFIG_SIGNERID_REQUEST");
2345 return flush_imsg(ibuf);
2348 const struct got_error *
2349 got_privsep_send_gotconfig_remotes_req(struct imsgbuf *ibuf)
2351 if (imsg_compose(ibuf,
2352 GOT_IMSG_GOTCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
2353 return got_error_from_errno("imsg_compose "
2354 "GOTCONFIG_REMOTE_REQUEST");
2356 return flush_imsg(ibuf);
2359 const struct got_error *
2360 got_privsep_recv_gotconfig_str(char **str, struct imsgbuf *ibuf)
2362 const struct got_error *err = NULL;
2368 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2371 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2373 switch (imsg.hdr.type) {
2374 case GOT_IMSG_GOTCONFIG_STR_VAL:
2377 *str = strndup(imsg.data, datalen);
2379 err = got_error_from_errno("strndup");
2384 err = got_error(GOT_ERR_PRIVSEP_MSG);
2392 const struct got_error *
2393 got_privsep_recv_gotconfig_remotes(struct got_remote_repo **remotes,
2394 int *nremotes, struct imsgbuf *ibuf)
2396 const struct got_error *err = NULL;
2399 struct got_imsg_remotes iremotes;
2400 struct got_imsg_remote iremote;
2401 const size_t min_datalen =
2402 MIN(sizeof(struct got_imsg_error), sizeof(iremotes));
2406 iremotes.nremotes = 0;
2408 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2411 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2413 switch (imsg.hdr.type) {
2414 case GOT_IMSG_GOTCONFIG_REMOTES:
2415 if (datalen != sizeof(iremotes)) {
2416 err = got_error(GOT_ERR_PRIVSEP_LEN);
2419 memcpy(&iremotes, imsg.data, sizeof(iremotes));
2420 if (iremotes.nremotes < 0) {
2421 err = got_error(GOT_ERR_PRIVSEP_LEN);
2424 if (iremotes.nremotes == 0) {
2431 return got_error(GOT_ERR_PRIVSEP_MSG);
2436 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(**remotes));
2437 if (*remotes == NULL)
2438 return got_error_from_errno("recallocarray");
2440 while (*nremotes < iremotes.nremotes) {
2441 struct got_remote_repo *remote;
2442 const size_t min_datalen =
2443 MIN(sizeof(struct got_imsg_error), sizeof(iremote));
2446 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2449 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2451 switch (imsg.hdr.type) {
2452 case GOT_IMSG_GOTCONFIG_REMOTE:
2453 remote = &(*remotes)[*nremotes];
2454 memset(remote, 0, sizeof(*remote));
2455 if (datalen < sizeof(iremote)) {
2456 err = got_error(GOT_ERR_PRIVSEP_LEN);
2459 memcpy(&iremote, imsg.data, sizeof(iremote));
2460 if (iremote.name_len == 0 ||
2461 (iremote.fetch_url_len == 0 &&
2462 iremote.send_url_len == 0) ||
2463 (sizeof(iremote) + iremote.name_len +
2464 iremote.fetch_url_len + iremote.send_url_len) >
2466 err = got_error(GOT_ERR_PRIVSEP_LEN);
2469 remote->name = strndup(imsg.data + sizeof(iremote),
2471 if (remote->name == NULL) {
2472 err = got_error_from_errno("strndup");
2475 remote->fetch_url = strndup(imsg.data +
2476 sizeof(iremote) + iremote.name_len,
2477 iremote.fetch_url_len);
2478 if (remote->fetch_url == NULL) {
2479 err = got_error_from_errno("strndup");
2480 free_remote_data(remote);
2483 remote->send_url = strndup(imsg.data +
2484 sizeof(iremote) + iremote.name_len +
2485 iremote.fetch_url_len, iremote.send_url_len);
2486 if (remote->send_url == NULL) {
2487 err = got_error_from_errno("strndup");
2488 free_remote_data(remote);
2491 remote->mirror_references = iremote.mirror_references;
2492 remote->fetch_all_branches = iremote.fetch_all_branches;
2493 if (iremote.nfetch_branches > 0) {
2494 remote->fetch_branches = recallocarray(NULL, 0,
2495 iremote.nfetch_branches, sizeof(char *));
2496 if (remote->fetch_branches == NULL) {
2497 err = got_error_from_errno("calloc");
2498 free_remote_data(remote);
2502 remote->nfetch_branches = 0;
2503 for (i = 0; i < iremote.nfetch_branches; i++) {
2505 err = got_privsep_recv_gotconfig_str(&branch,
2508 free_remote_data(remote);
2511 remote->fetch_branches[i] = branch;
2512 remote->nfetch_branches++;
2514 if (iremote.nsend_branches > 0) {
2515 remote->send_branches = recallocarray(NULL, 0,
2516 iremote.nsend_branches, sizeof(char *));
2517 if (remote->send_branches == NULL) {
2518 err = got_error_from_errno("calloc");
2519 free_remote_data(remote);
2523 remote->nsend_branches = 0;
2524 for (i = 0; i < iremote.nsend_branches; i++) {
2526 err = got_privsep_recv_gotconfig_str(&branch,
2529 free_remote_data(remote);
2532 remote->send_branches[i] = branch;
2533 remote->nsend_branches++;
2535 if (iremote.nfetch_refs > 0) {
2536 remote->fetch_refs = recallocarray(NULL, 0,
2537 iremote.nfetch_refs, sizeof(char *));
2538 if (remote->fetch_refs == NULL) {
2539 err = got_error_from_errno("calloc");
2540 free_remote_data(remote);
2544 remote->nfetch_refs = 0;
2545 for (i = 0; i < iremote.nfetch_refs; i++) {
2547 err = got_privsep_recv_gotconfig_str(&ref,
2550 free_remote_data(remote);
2553 remote->fetch_refs[i] = ref;
2554 remote->nfetch_refs++;
2559 err = got_error(GOT_ERR_PRIVSEP_MSG);
2570 for (i = 0; i < *nremotes; i++)
2571 free_remote_data(&(*remotes)[i]);
2579 const struct got_error *
2580 got_privsep_send_commit_traversal_request(struct imsgbuf *ibuf,
2581 struct got_object_id *id, int idx, const char *path)
2584 size_t path_len = strlen(path) + 1;
2586 wbuf = imsg_create(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_REQUEST, 0, 0,
2587 sizeof(struct got_imsg_commit_traversal_request) + path_len);
2589 return got_error_from_errno(
2590 "imsg_create COMMIT_TRAVERSAL_REQUEST");
2591 if (imsg_add(wbuf, id->hash, SHA1_DIGEST_LENGTH) == -1)
2592 return got_error_from_errno("imsg_add "
2593 "COMMIT_TRAVERSAL_REQUEST");
2594 if (imsg_add(wbuf, &idx, sizeof(idx)) == -1)
2595 return got_error_from_errno("imsg_add "
2596 "COMMIT_TRAVERSAL_REQUEST");
2597 if (imsg_add(wbuf, path, path_len) == -1)
2598 return got_error_from_errno("imsg_add "
2599 "COMMIT_TRAVERSAL_REQUEST");
2602 imsg_close(ibuf, wbuf);
2604 return flush_imsg(ibuf);
2607 const struct got_error *
2608 got_privsep_recv_traversed_commits(struct got_commit_object **changed_commit,
2609 struct got_object_id **changed_commit_id,
2610 struct got_object_id_queue *commit_ids, struct imsgbuf *ibuf)
2612 const struct got_error *err = NULL;
2614 struct got_imsg_traversed_commits *icommits;
2618 *changed_commit = NULL;
2619 *changed_commit_id = NULL;
2622 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2626 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2627 switch (imsg.hdr.type) {
2628 case GOT_IMSG_TRAVERSED_COMMITS:
2629 icommits = imsg.data;
2630 if (datalen != sizeof(*icommits) +
2631 icommits->ncommits * SHA1_DIGEST_LENGTH) {
2632 err = got_error(GOT_ERR_PRIVSEP_LEN);
2635 for (i = 0; i < icommits->ncommits; i++) {
2636 struct got_object_qid *qid;
2637 uint8_t *hash = (uint8_t *)imsg.data +
2638 sizeof(*icommits) + i * SHA1_DIGEST_LENGTH;
2639 err = got_object_qid_alloc_partial(&qid);
2642 memcpy(qid->id.hash, hash, SHA1_DIGEST_LENGTH);
2643 STAILQ_INSERT_TAIL(commit_ids, qid, entry);
2645 /* The last commit may contain a change. */
2646 if (i == icommits->ncommits - 1) {
2647 *changed_commit_id =
2648 got_object_id_dup(&qid->id);
2649 if (*changed_commit_id == NULL) {
2650 err = got_error_from_errno(
2651 "got_object_id_dup");
2657 case GOT_IMSG_COMMIT:
2658 if (*changed_commit_id == NULL) {
2659 err = got_error(GOT_ERR_PRIVSEP_MSG);
2662 err = get_commit_from_imsg(changed_commit, &imsg,
2665 case GOT_IMSG_COMMIT_TRAVERSAL_DONE:
2669 err = got_error(GOT_ERR_PRIVSEP_MSG);
2679 got_object_id_queue_free(commit_ids);
2683 const struct got_error *
2684 got_privsep_send_enumerated_tree(size_t *totlen, struct imsgbuf *ibuf,
2685 struct got_object_id *tree_id, const char *path,
2686 struct got_parsed_tree_entry *entries, int nentries)
2688 const struct got_error *err = NULL;
2690 size_t path_len = strlen(path);
2693 msglen = sizeof(struct got_imsg_enumerated_tree) + path_len;
2694 wbuf = imsg_create(ibuf, GOT_IMSG_ENUMERATED_TREE, 0, 0, msglen);
2696 return got_error_from_errno("imsg_create ENUMERATED_TREE");
2698 if (imsg_add(wbuf, tree_id->hash, SHA1_DIGEST_LENGTH) == -1)
2699 return got_error_from_errno("imsg_add ENUMERATED_TREE");
2700 if (imsg_add(wbuf, &nentries, sizeof(nentries)) == -1)
2701 return got_error_from_errno("imsg_add ENUMERATED_TREE");
2702 if (imsg_add(wbuf, path, path_len) == -1)
2703 return got_error_from_errno("imsg_add ENUMERATED_TREE");
2706 imsg_close(ibuf, wbuf);
2709 err = send_tree_entries(ibuf, entries, nentries);
2714 return flush_imsg(ibuf);
2717 const struct got_error *
2718 got_privsep_send_object_enumeration_request(struct imsgbuf *ibuf)
2720 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_ENUMERATION_REQUEST,
2721 0, 0, -1, NULL, 0) == -1)
2722 return got_error_from_errno("imsg_compose "
2723 "OBJECT_ENUMERATION_REQUEST");
2725 return flush_imsg(ibuf);
2728 const struct got_error *
2729 got_privsep_send_object_enumeration_done(struct imsgbuf *ibuf)
2731 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_ENUMERATION_DONE,
2732 0, 0, -1, NULL, 0) == -1)
2733 return got_error_from_errno("imsg_compose "
2734 "OBJECT_ENUMERATION_DONE");
2736 return flush_imsg(ibuf);
2739 const struct got_error *
2740 got_privsep_send_object_enumeration_incomplete(struct imsgbuf *ibuf)
2742 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_ENUMERATION_INCOMPLETE,
2743 0, 0, -1, NULL, 0) == -1)
2744 return got_error_from_errno("imsg_compose "
2745 "OBJECT_ENUMERATION_INCOMPLETE");
2747 return flush_imsg(ibuf);
2750 const struct got_error *
2751 got_privsep_send_enumerated_commit(struct imsgbuf *ibuf,
2752 struct got_object_id *id, time_t mtime)
2756 wbuf = imsg_create(ibuf, GOT_IMSG_ENUMERATED_COMMIT, 0, 0,
2757 sizeof(struct got_imsg_enumerated_commit) + SHA1_DIGEST_LENGTH);
2759 return got_error_from_errno("imsg_create ENUMERATED_COMMIT");
2761 /* Keep in sync with struct got_imsg_enumerated_commit! */
2762 if (imsg_add(wbuf, id, SHA1_DIGEST_LENGTH) == -1)
2763 return got_error_from_errno("imsg_add ENUMERATED_COMMIT");
2764 if (imsg_add(wbuf, &mtime, sizeof(mtime)) == -1)
2765 return got_error_from_errno("imsg_add ENUMERATED_COMMIT");
2768 imsg_close(ibuf, wbuf);
2769 /* Don't flush yet, tree entries or ENUMERATION_DONE will follow. */
2773 const struct got_error *
2774 got_privsep_recv_enumerated_objects(int *found_all_objects,
2775 struct imsgbuf *ibuf,
2776 got_object_enumerate_commit_cb cb_commit,
2777 got_object_enumerate_tree_cb cb_tree, void *cb_arg,
2778 struct got_repository *repo)
2780 const struct got_error *err = NULL;
2782 struct got_imsg_enumerated_commit *icommit = NULL;
2783 struct got_object_id commit_id;
2784 int have_commit = 0;
2786 struct got_tree_object tree;
2787 struct got_imsg_enumerated_tree *itree;
2788 struct got_object_id tree_id;
2789 char *path = NULL, *canon_path = NULL;
2790 size_t datalen, path_len;
2794 *found_all_objects = 0;
2795 memset(&tree, 0, sizeof(tree));
2798 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2802 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2803 switch (imsg.hdr.type) {
2804 case GOT_IMSG_ENUMERATED_COMMIT:
2805 if (have_commit && nentries != -1) {
2806 err = got_error(GOT_ERR_PRIVSEP_MSG);
2809 if (datalen != sizeof(*icommit)) {
2810 err = got_error(GOT_ERR_PRIVSEP_LEN);
2813 icommit = (struct got_imsg_enumerated_commit *)imsg.data;
2814 memcpy(commit_id.hash, icommit->id, SHA1_DIGEST_LENGTH);
2815 mtime = icommit->mtime;
2818 case GOT_IMSG_ENUMERATED_TREE:
2819 /* Should be preceeded by GOT_IMSG_ENUMERATED_COMMIT. */
2821 err = got_error(GOT_ERR_PRIVSEP_MSG);
2824 if (datalen < sizeof(*itree)) {
2825 err = got_error(GOT_ERR_PRIVSEP_LEN);
2829 path_len = datalen - sizeof(*itree);
2830 if (path_len == 0) {
2831 err = got_error(GOT_ERR_PRIVSEP_LEN);
2834 memcpy(tree_id.hash, itree->id, sizeof(tree_id.hash));
2836 path = strndup(imsg.data + sizeof(*itree), path_len);
2838 err = got_error_from_errno("strndup");
2842 canon_path = malloc(path_len + 1);
2843 if (canon_path == NULL) {
2844 err = got_error_from_errno("malloc");
2847 if (!got_path_is_absolute(path)) {
2848 err = got_error(GOT_ERR_BAD_PATH);
2851 if (got_path_is_root_dir(path)) {
2852 /* XXX check what got_canonpath() does wrong */
2853 canon_path[0] = '/';
2854 canon_path[1] = '\0';
2856 err = got_canonpath(path, canon_path,
2861 if (strcmp(path, canon_path) != 0) {
2862 err = got_error(GOT_ERR_BAD_PATH);
2865 if (nentries != -1) {
2866 err = got_error(GOT_ERR_PRIVSEP_MSG);
2869 if (itree->nentries < -1) {
2870 err = got_error(GOT_ERR_PRIVSEP_MSG);
2873 if (itree->nentries == -1) {
2874 /* Tree was not found in pack file. */
2875 err = cb_tree(cb_arg, NULL, mtime, &tree_id,
2879 if (itree->nentries > INT_MAX) {
2880 err = got_error(GOT_ERR_PRIVSEP_LEN);
2883 tree.entries = calloc(itree->nentries,
2884 sizeof(struct got_tree_entry));
2885 if (tree.entries == NULL) {
2886 err = got_error_from_errno("calloc");
2889 if (itree->nentries == 0) {
2890 err = cb_tree(cb_arg, &tree, mtime, &tree_id,
2895 /* Prepare for next tree. */
2897 memset(&tree, 0, sizeof(tree));
2900 tree.nentries = itree->nentries;
2904 case GOT_IMSG_TREE_ENTRIES:
2905 /* Should be preceeded by GOT_IMSG_ENUMERATED_TREE. */
2906 if (nentries <= -1) {
2907 err = got_error(GOT_ERR_PRIVSEP_MSG);
2910 err = recv_tree_entries(imsg.data, datalen,
2914 if (tree.nentries == nentries) {
2915 err = cb_tree(cb_arg, &tree, mtime, &tree_id,
2920 /* Prepare for next tree. */
2922 memset(&tree, 0, sizeof(tree));
2926 case GOT_IMSG_TREE_ENUMERATION_DONE:
2927 /* All trees have been found and traversed. */
2928 if (!have_commit || path == NULL || nentries != -1) {
2929 err = got_error(GOT_ERR_PRIVSEP_MSG);
2932 err = cb_commit(cb_arg, mtime, &commit_id, repo);
2937 case GOT_IMSG_OBJECT_ENUMERATION_DONE:
2938 *found_all_objects = 1;
2941 case GOT_IMSG_OBJECT_ENUMERATION_INCOMPLETE:
2945 err = got_error(GOT_ERR_PRIVSEP_MSG);
2960 const struct got_error *
2961 got_privsep_send_raw_delta_req(struct imsgbuf *ibuf, int idx,
2962 struct got_object_id *id)
2964 struct got_imsg_raw_delta_request dreq;
2966 memset(&dreq, 0, sizeof(dreq));
2968 memcpy(&dreq.id, id, sizeof(dreq.id));
2970 if (imsg_compose(ibuf, GOT_IMSG_RAW_DELTA_REQUEST, 0, 0, -1,
2971 &dreq, sizeof(dreq)) == -1)
2972 return got_error_from_errno("imsg_compose RAW_DELTA_REQUEST");
2974 return flush_imsg(ibuf);
2977 const struct got_error *
2978 got_privsep_send_raw_delta_outfd(struct imsgbuf *ibuf, int fd)
2980 return send_fd(ibuf, GOT_IMSG_RAW_DELTA_OUTFD, fd);
2983 const struct got_error *
2984 got_privsep_send_raw_delta(struct imsgbuf *ibuf, uint64_t base_size,
2985 uint64_t result_size, off_t delta_size, off_t delta_compressed_size,
2986 off_t delta_offset, off_t delta_out_offset, struct got_object_id *base_id)
2988 struct got_imsg_raw_delta idelta;
2991 memset(&idelta, 0, sizeof(idelta));
2992 idelta.base_size = base_size;
2993 idelta.result_size = result_size;
2994 idelta.delta_size = delta_size;
2995 idelta.delta_compressed_size = delta_compressed_size;
2996 idelta.delta_offset = delta_offset;
2997 idelta.delta_out_offset = delta_out_offset;
2998 memcpy(&idelta.base_id, &base_id, sizeof(idelta.base_id));
3000 ret = imsg_compose(ibuf, GOT_IMSG_RAW_DELTA, 0, 0, -1,
3001 &idelta, sizeof(idelta));
3003 return got_error_from_errno("imsg_compose RAW_DELTA");
3005 return flush_imsg(ibuf);
3008 const struct got_error *
3009 got_privsep_recv_raw_delta(uint64_t *base_size, uint64_t *result_size,
3010 off_t *delta_size, off_t *delta_compressed_size, off_t *delta_offset,
3011 off_t *delta_out_offset, struct got_object_id **base_id,
3012 struct imsgbuf *ibuf)
3014 const struct got_error *err = NULL;
3016 struct got_imsg_raw_delta *delta;
3022 *delta_compressed_size = 0;
3024 *delta_out_offset = 0;
3027 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
3031 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
3033 switch (imsg.hdr.type) {
3034 case GOT_IMSG_RAW_DELTA:
3035 if (datalen != sizeof(*delta)) {
3036 err = got_error(GOT_ERR_PRIVSEP_LEN);
3040 *base_size = delta->base_size;
3041 *result_size = delta->result_size;
3042 *delta_size = delta->delta_size;
3043 *delta_compressed_size = delta->delta_compressed_size;
3044 *delta_offset = delta->delta_offset;
3045 *delta_out_offset = delta->delta_out_offset;
3046 *base_id = calloc(1, sizeof(**base_id));
3047 if (*base_id == NULL) {
3048 err = got_error_from_errno("malloc");
3051 memcpy(*base_id, &delta->base_id, sizeof(**base_id));
3054 err = got_error(GOT_ERR_PRIVSEP_MSG);
3067 static const struct got_error *
3068 send_idlist(struct imsgbuf *ibuf, struct got_object_id **ids, size_t nids)
3070 const struct got_error *err = NULL;
3071 struct got_imsg_object_idlist idlist;
3075 memset(&idlist, 0, sizeof(idlist));
3077 if (nids > GOT_IMSG_OBJ_ID_LIST_MAX_NIDS)
3078 return got_error(GOT_ERR_NO_SPACE);
3080 wbuf = imsg_create(ibuf, GOT_IMSG_OBJ_ID_LIST, 0, 0,
3081 sizeof(idlist) + nids * sizeof(**ids));
3083 err = got_error_from_errno("imsg_create OBJ_ID_LIST");
3088 if (imsg_add(wbuf, &idlist, sizeof(idlist)) == -1)
3089 return got_error_from_errno("imsg_add OBJ_ID_LIST");
3091 for (i = 0; i < nids; i++) {
3092 struct got_object_id *id = ids[i];
3093 if (imsg_add(wbuf, id, sizeof(*id)) == -1)
3094 return got_error_from_errno("imsg_add OBJ_ID_LIST");
3098 imsg_close(ibuf, wbuf);
3100 return flush_imsg(ibuf);
3103 const struct got_error *
3104 got_privsep_send_object_idlist(struct imsgbuf *ibuf,
3105 struct got_object_id **ids, size_t nids)
3107 const struct got_error *err = NULL;
3108 struct got_object_id *idlist[GOT_IMSG_OBJ_ID_LIST_MAX_NIDS];
3111 for (i = 0; i < nids; i++) {
3112 idlist[i % nitems(idlist)] = ids[i];
3114 if (queued >= nitems(idlist)) {
3115 err = send_idlist(ibuf, idlist, queued);
3123 err = send_idlist(ibuf, idlist, queued);
3131 const struct got_error *
3132 got_privsep_send_object_idlist_done(struct imsgbuf *ibuf)
3134 if (imsg_compose(ibuf, GOT_IMSG_OBJ_ID_LIST_DONE, 0, 0, -1, NULL, 0)
3136 return got_error_from_errno("imsg_compose OBJ_ID_LIST_DONE");
3138 return flush_imsg(ibuf);
3141 const struct got_error *
3142 got_privsep_recv_object_idlist(int *done, struct got_object_id **ids,
3143 size_t *nids, struct imsgbuf *ibuf)
3145 const struct got_error *err = NULL;
3147 struct got_imsg_object_idlist *idlist;
3154 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
3158 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
3159 switch (imsg.hdr.type) {
3160 case GOT_IMSG_OBJ_ID_LIST:
3161 if (datalen < sizeof(*idlist)) {
3162 err = got_error(GOT_ERR_PRIVSEP_LEN);
3166 if (idlist->nids > GOT_IMSG_OBJ_ID_LIST_MAX_NIDS ||
3167 idlist->nids * sizeof(**ids) > datalen - sizeof(*idlist)) {
3168 err = got_error(GOT_ERR_PRIVSEP_LEN);
3171 *nids = idlist->nids;
3172 *ids = calloc(*nids, sizeof(**ids));
3174 err = got_error_from_errno("calloc");
3177 memcpy(*ids, (uint8_t *)imsg.data + sizeof(*idlist),
3178 *nids * sizeof(**ids));
3180 case GOT_IMSG_OBJ_ID_LIST_DONE:
3184 err = got_error(GOT_ERR_PRIVSEP_MSG);
3193 const struct got_error *
3194 got_privsep_send_delta_reuse_req(struct imsgbuf *ibuf)
3196 if (imsg_compose(ibuf, GOT_IMSG_DELTA_REUSE_REQUEST, 0, 0, -1, NULL, 0)
3198 return got_error_from_errno("imsg_compose DELTA_REUSE_REQUEST");
3200 return flush_imsg(ibuf);
3203 const struct got_error *
3204 got_privsep_send_reused_deltas(struct imsgbuf *ibuf,
3205 struct got_imsg_reused_delta *deltas, size_t ndeltas)
3207 const struct got_error *err = NULL;
3209 struct got_imsg_reused_deltas ideltas;
3212 memset(&ideltas, 0, sizeof(ideltas));
3214 if (ndeltas > GOT_IMSG_REUSED_DELTAS_MAX_NDELTAS)
3215 return got_error(GOT_ERR_NO_SPACE);
3217 wbuf = imsg_create(ibuf, GOT_IMSG_REUSED_DELTAS, 0, 0,
3218 sizeof(ideltas) + ndeltas * sizeof(*deltas));
3220 err = got_error_from_errno("imsg_create REUSED_DELTAS");
3224 ideltas.ndeltas = ndeltas;
3225 if (imsg_add(wbuf, &ideltas, sizeof(ideltas)) == -1)
3226 return got_error_from_errno("imsg_add REUSED_DELTAS");
3228 for (i = 0; i < ndeltas; i++) {
3229 struct got_imsg_reused_delta *delta = &deltas[i];
3230 if (imsg_add(wbuf, delta, sizeof(*delta)) == -1)
3231 return got_error_from_errno("imsg_add REUSED_DELTAS");
3235 imsg_close(ibuf, wbuf);
3237 return flush_imsg(ibuf);
3240 const struct got_error *
3241 got_privsep_send_reused_deltas_done(struct imsgbuf *ibuf)
3243 if (imsg_compose(ibuf, GOT_IMSG_DELTA_REUSE_DONE, 0, 0, -1, NULL, 0)
3245 return got_error_from_errno("imsg_compose DELTA_REUSE_DONE");
3247 return flush_imsg(ibuf);
3250 const struct got_error *
3251 got_privsep_recv_reused_deltas(int *done, struct got_imsg_reused_delta *deltas,
3252 size_t *ndeltas, struct imsgbuf *ibuf)
3254 const struct got_error *err = NULL;
3256 struct got_imsg_reused_deltas *ideltas;
3262 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
3266 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
3267 switch (imsg.hdr.type) {
3268 case GOT_IMSG_REUSED_DELTAS:
3269 if (datalen < sizeof(*ideltas)) {
3270 err = got_error(GOT_ERR_PRIVSEP_LEN);
3273 ideltas = imsg.data;
3274 if (ideltas->ndeltas > GOT_IMSG_OBJ_ID_LIST_MAX_NIDS ||
3275 ideltas->ndeltas * sizeof(*deltas) >
3276 datalen - sizeof(*ideltas)) {
3277 err = got_error(GOT_ERR_PRIVSEP_LEN);
3280 *ndeltas = ideltas->ndeltas;
3281 memcpy(deltas, (uint8_t *)imsg.data + sizeof(*ideltas),
3282 *ndeltas * sizeof(*deltas));
3284 case GOT_IMSG_DELTA_REUSE_DONE:
3288 err = got_error(GOT_ERR_PRIVSEP_MSG);
3297 const struct got_error *
3298 got_privsep_init_commit_painting(struct imsgbuf *ibuf)
3300 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_PAINTING_INIT,
3303 return got_error_from_errno("imsg_compose "
3304 "COMMIT_PAINTING_INIT");
3306 return flush_imsg(ibuf);
3309 const struct got_error *
3310 got_privsep_send_painting_request(struct imsgbuf *ibuf, int idx,
3311 struct got_object_id *id, intptr_t color)
3313 struct got_imsg_commit_painting_request ireq;
3315 memset(&ireq, 0, sizeof(ireq));
3316 memcpy(ireq.id, id->hash, sizeof(ireq.id));
3320 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_PAINTING_REQUEST, 0, 0, -1,
3321 &ireq, sizeof(ireq)) == -1)
3322 return got_error_from_errno("imsg_compose "
3323 "COMMIT_PAINTING_REQUEST");
3325 return flush_imsg(ibuf);
3328 static const struct got_error *
3329 send_painted_commits(struct got_object_id_queue *ids, int *nids,
3330 size_t remain, int present_in_pack, struct imsgbuf *ibuf)
3332 const struct got_error *err = NULL;
3333 struct ibuf *wbuf = NULL;
3334 struct got_object_qid *qid;
3339 msglen = MIN(remain, MAX_IMSGSIZE - IMSG_HEADER_SIZE);
3340 ncommits = (msglen - sizeof(struct got_imsg_painted_commits)) /
3341 sizeof(struct got_imsg_painted_commit);
3343 wbuf = imsg_create(ibuf, GOT_IMSG_PAINTED_COMMITS, 0, 0, msglen);
3345 err = got_error_from_errno("imsg_create PAINTED_COMMITS");
3349 /* Keep in sync with struct got_imsg_painted_commits! */
3350 if (imsg_add(wbuf, &ncommits, sizeof(ncommits)) == -1)
3351 return got_error_from_errno("imsg_add PAINTED_COMMITS");
3352 if (imsg_add(wbuf, &present_in_pack, sizeof(present_in_pack)) == -1)
3353 return got_error_from_errno("imsg_add PAINTED_COMMITS");
3355 while (ncommits > 0) {
3356 qid = STAILQ_FIRST(ids);
3357 STAILQ_REMOVE_HEAD(ids, entry);
3360 color = (intptr_t)qid->data;
3362 /* Keep in sync with struct got_imsg_painted_commit! */
3363 if (imsg_add(wbuf, qid->id.hash, SHA1_DIGEST_LENGTH) == -1)
3364 return got_error_from_errno("imsg_add PAINTED_COMMITS");
3365 if (imsg_add(wbuf, &color, sizeof(color)) == -1)
3366 return got_error_from_errno("imsg_add PAINTED_COMMITS");
3368 got_object_qid_free(qid);
3372 imsg_close(ibuf, wbuf);
3374 return flush_imsg(ibuf);
3377 const struct got_error *
3378 got_privsep_send_painted_commits(struct imsgbuf *ibuf,
3379 struct got_object_id_queue *ids, int *nids,
3380 int present_in_pack, int flush)
3382 const struct got_error *err;
3389 remain = (sizeof(struct got_imsg_painted_commits)) +
3390 *nids * sizeof(struct got_imsg_painted_commit);
3391 if (flush || remain >= MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
3392 err = send_painted_commits(ids, nids, remain,
3393 present_in_pack, ibuf);
3397 } while (flush && *nids > 0);
3402 const struct got_error *
3403 got_privsep_send_painting_commits_done(struct imsgbuf *ibuf)
3405 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_PAINTING_DONE,
3408 return got_error_from_errno("imsg_compose "
3409 "COMMIT_PAINTING_DONE");
3411 return flush_imsg(ibuf);
3414 const struct got_error *
3415 got_privsep_recv_painted_commits(struct got_object_id_queue *new_ids,
3416 got_privsep_recv_painted_commit_cb cb, void *cb_arg, struct imsgbuf *ibuf)
3418 const struct got_error *err = NULL;
3420 struct got_imsg_painted_commits icommits;
3421 struct got_imsg_painted_commit icommit;
3426 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
3430 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
3431 if (imsg.hdr.type == GOT_IMSG_COMMIT_PAINTING_DONE)
3433 if (imsg.hdr.type != GOT_IMSG_PAINTED_COMMITS)
3434 return got_error(GOT_ERR_PRIVSEP_MSG);
3436 if (datalen < sizeof(icommits))
3437 return got_error(GOT_ERR_PRIVSEP_LEN);
3438 memcpy(&icommits, imsg.data, sizeof(icommits));
3439 if (icommits.ncommits * sizeof(icommit) < icommits.ncommits ||
3440 datalen < sizeof(icommits) +
3441 icommits.ncommits * sizeof(icommit))
3442 return got_error(GOT_ERR_PRIVSEP_LEN);
3444 for (i = 0; i < icommits.ncommits; i++) {
3446 (uint8_t *)imsg.data + sizeof(icommits) + i * sizeof(icommit),
3449 if (icommits.present_in_pack) {
3450 struct got_object_id id;
3451 memcpy(id.hash, icommit.id, SHA1_DIGEST_LENGTH);
3452 err = cb(cb_arg, &id, icommit.color);
3456 struct got_object_qid *qid;
3457 err = got_object_qid_alloc_partial(&qid);
3460 memcpy(qid->id.hash, icommit.id,
3461 SHA1_DIGEST_LENGTH);
3462 qid->data = (void *)icommit.color;
3463 STAILQ_INSERT_TAIL(new_ids, qid, entry);
3473 const struct got_error *
3474 got_privsep_unveil_exec_helpers(void)
3476 const char *helpers[] = {
3477 GOT_PATH_PROG_READ_PACK,
3478 GOT_PATH_PROG_READ_OBJECT,
3479 GOT_PATH_PROG_READ_COMMIT,
3480 GOT_PATH_PROG_READ_TREE,
3481 GOT_PATH_PROG_READ_BLOB,
3482 GOT_PATH_PROG_READ_TAG,
3483 GOT_PATH_PROG_READ_GITCONFIG,
3484 GOT_PATH_PROG_READ_GOTCONFIG,
3485 GOT_PATH_PROG_READ_PATCH,
3486 GOT_PATH_PROG_FETCH_PACK,
3487 GOT_PATH_PROG_INDEX_PACK,
3488 GOT_PATH_PROG_SEND_PACK,
3492 for (i = 0; i < nitems(helpers); i++) {
3493 if (unveil(helpers[i], "x") == 0)
3495 return got_error_from_errno2("unveil", helpers[i]);
3502 got_privsep_exec_child(int imsg_fds[2], const char *path, const char *repo_path)
3504 if (close(imsg_fds[0]) == -1) {
3505 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
3509 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
3510 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
3514 closefrom(GOT_IMSG_FD_CHILD + 1);
3516 if (execl(path, path, repo_path, (char *)NULL) == -1) {
3517 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,