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>
35 #include "got_compat.h"
37 #include "got_object.h"
38 #include "got_error.h"
40 #include "got_repository.h"
42 #include "got_lib_sha1.h"
43 #include "got_lib_delta.h"
44 #include "got_lib_inflate.h"
45 #include "got_lib_object.h"
46 #include "got_lib_object_parse.h"
47 #include "got_lib_privsep.h"
48 #include "got_lib_pack.h"
49 #include "got_lib_poll.h"
51 #include "got_privsep.h"
54 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
58 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
61 static const struct got_error *
62 read_imsg(struct imsgbuf *ibuf)
64 const struct got_error *err;
67 err = got_poll_fd(ibuf->fd, POLLIN, INFTIM);
69 if (err->code == GOT_ERR_EOF)
70 return got_error(GOT_ERR_PRIVSEP_PIPE);
76 if (errno == EAGAIN) /* Could be a file-descriptor leak. */
77 return got_error(GOT_ERR_PRIVSEP_NO_FD);
78 return got_error(GOT_ERR_PRIVSEP_READ);
81 return got_error(GOT_ERR_PRIVSEP_PIPE);
86 const struct got_error *
87 got_privsep_wait_for_child(pid_t pid)
91 if (waitpid(pid, &child_status, 0) == -1)
92 return got_error_from_errno("waitpid");
94 if (!WIFEXITED(child_status))
95 return got_error(GOT_ERR_PRIVSEP_DIED);
97 if (WEXITSTATUS(child_status) != 0)
98 return got_error(GOT_ERR_PRIVSEP_EXIT);
103 static const struct got_error *
104 recv_imsg_error(struct imsg *imsg, size_t datalen)
106 struct got_imsg_error *ierr;
108 if (datalen != sizeof(*ierr))
109 return got_error(GOT_ERR_PRIVSEP_LEN);
112 if (ierr->code == GOT_ERR_ERRNO) {
113 static struct got_error serr;
114 serr.code = GOT_ERR_ERRNO;
115 serr.msg = strerror(ierr->errno_code);
119 return got_error(ierr->code);
122 const struct got_error *
123 got_privsep_recv_imsg(struct imsg *imsg, struct imsgbuf *ibuf,
126 const struct got_error *err;
129 n = imsg_get(ibuf, imsg);
131 return got_error_from_errno("imsg_get");
134 err = read_imsg(ibuf);
137 n = imsg_get(ibuf, imsg);
139 return got_error_from_errno("imsg_get");
142 if (imsg->hdr.len < IMSG_HEADER_SIZE + min_datalen)
143 return got_error(GOT_ERR_PRIVSEP_LEN);
145 if (imsg->hdr.type == GOT_IMSG_ERROR) {
146 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
147 return recv_imsg_error(imsg, datalen);
153 /* Attempt to send an error in an imsg. Complain on stderr as a last resort. */
155 got_privsep_send_error(struct imsgbuf *ibuf, const struct got_error *err)
157 const struct got_error *poll_err;
158 struct got_imsg_error ierr;
161 ierr.code = err->code;
162 if (err->code == GOT_ERR_ERRNO)
163 ierr.errno_code = errno;
166 ret = imsg_compose(ibuf, GOT_IMSG_ERROR, 0, 0, -1, &ierr, sizeof(ierr));
168 fprintf(stderr, "%s: error %d \"%s\": imsg_compose: %s\n",
169 getprogname(), err->code, err->msg, strerror(errno));
173 poll_err = got_poll_fd(ibuf->fd, POLLOUT, INFTIM);
175 fprintf(stderr, "%s: error %d \"%s\": poll: %s\n",
176 getprogname(), err->code, err->msg, poll_err->msg);
180 ret = imsg_flush(ibuf);
182 fprintf(stderr, "%s: error %d \"%s\": imsg_flush: %s\n",
183 getprogname(), err->code, err->msg, strerror(errno));
189 static const struct got_error *
190 flush_imsg(struct imsgbuf *ibuf)
192 const struct got_error *err;
194 err = got_poll_fd(ibuf->fd, POLLOUT, INFTIM);
198 if (imsg_flush(ibuf) == -1) {
200 return got_error_from_errno("imsg_flush");
206 const struct got_error *
207 got_privsep_flush_imsg(struct imsgbuf *ibuf)
209 return flush_imsg(ibuf);
212 const struct got_error *
213 got_privsep_send_stop(int fd)
215 const struct got_error *err = NULL;
218 imsg_init(&ibuf, fd);
220 if (imsg_compose(&ibuf, GOT_IMSG_STOP, 0, 0, -1, NULL, 0) == -1)
221 return got_error_from_errno("imsg_compose STOP");
223 err = flush_imsg(&ibuf);
227 const struct got_error *
228 got_privsep_send_obj_req(struct imsgbuf *ibuf, int fd,
229 struct got_object_id *id)
231 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_REQUEST, 0, 0, fd,
232 id, sizeof(*id)) == -1)
233 return got_error_from_errno("imsg_compose OBJECT_REQUEST");
235 return flush_imsg(ibuf);
238 const struct got_error *
239 got_privsep_send_raw_obj_req(struct imsgbuf *ibuf, int fd,
240 struct got_object_id *id)
242 if (imsg_compose(ibuf, GOT_IMSG_RAW_OBJECT_REQUEST, 0, 0, fd,
243 id, sizeof(*id)) == -1)
244 return got_error_from_errno("imsg_compose RAW_OBJECT_REQUEST");
246 return flush_imsg(ibuf);
249 const struct got_error *
250 got_privsep_send_raw_obj_outfd(struct imsgbuf *ibuf, int outfd)
252 const struct got_error *err = NULL;
254 if (imsg_compose(ibuf, GOT_IMSG_RAW_OBJECT_OUTFD, 0, 0, outfd, NULL, 0)
256 err = got_error_from_errno("imsg_compose RAW_OBJECT_OUTFD");
261 return flush_imsg(ibuf);
264 const struct got_error *
265 got_privsep_send_raw_obj(struct imsgbuf *ibuf, off_t size, size_t hdrlen,
268 const struct got_error *err = NULL;
269 struct got_imsg_raw_obj iobj;
270 size_t len = sizeof(iobj);
273 memset(&iobj, 0, sizeof(iobj));
274 iobj.hdrlen = hdrlen;
277 if (data && size + hdrlen <= GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX)
278 len += (size_t)size + hdrlen;
280 wbuf = imsg_create(ibuf, GOT_IMSG_RAW_OBJECT, 0, 0, len);
282 err = got_error_from_errno("imsg_create RAW_OBJECT");
286 if (imsg_add(wbuf, &iobj, sizeof(iobj)) == -1)
287 return got_error_from_errno("imsg_add RAW_OBJECT");
289 if (data && size + hdrlen <= GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX) {
290 if (imsg_add(wbuf, data, size + hdrlen) == -1)
291 return got_error_from_errno("imsg_add RAW_OBJECT");
295 imsg_close(ibuf, wbuf);
297 return flush_imsg(ibuf);
300 const struct got_error *
301 got_privsep_recv_raw_obj(uint8_t **outbuf, off_t *size, size_t *hdrlen,
302 struct imsgbuf *ibuf)
304 const struct got_error *err = NULL;
306 struct got_imsg_raw_obj *iobj;
311 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
315 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
317 switch (imsg.hdr.type) {
318 case GOT_IMSG_RAW_OBJECT:
319 if (datalen < sizeof(*iobj)) {
320 err = got_error(GOT_ERR_PRIVSEP_LEN);
325 *hdrlen = iobj->hdrlen;
327 if (datalen == sizeof(*iobj)) {
328 /* Data has been written to file descriptor. */
333 *size + *hdrlen > GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX) {
334 err = got_error(GOT_ERR_PRIVSEP_LEN);
338 *outbuf = malloc(*size + *hdrlen);
339 if (*outbuf == NULL) {
340 err = got_error_from_errno("malloc");
343 memcpy(*outbuf, imsg.data + sizeof(*iobj), *size + *hdrlen);
346 err = got_error(GOT_ERR_PRIVSEP_MSG);
355 const struct got_error *
356 got_privsep_send_commit_req(struct imsgbuf *ibuf, int fd,
357 struct got_object_id *id, int pack_idx)
359 const struct got_error *err = NULL;
360 struct got_imsg_packed_object iobj;
364 memset(&iobj, 0, sizeof(iobj));
365 if (pack_idx != -1) { /* commit is packed */
367 memcpy(&iobj.id, id, sizeof(iobj.id));
375 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_REQUEST, 0, 0, fd, data, len)
377 err = got_error_from_errno("imsg_compose COMMIT_REQUEST");
382 return flush_imsg(ibuf);
385 const struct got_error *
386 got_privsep_send_tree_req(struct imsgbuf *ibuf, int fd,
387 struct got_object_id *id, int pack_idx)
393 len = sizeof(struct got_imsg_packed_object);
397 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_REQUEST, 0, 0, len);
399 return got_error_from_errno("imsg_create TREE_REQUEST");
401 if (imsg_add(wbuf, id, sizeof(*id)) == -1)
402 return got_error_from_errno("imsg_add TREE_REQUEST");
404 if (pack_idx != -1) { /* tree is packed */
405 if (imsg_add(wbuf, &pack_idx, sizeof(pack_idx)) == -1)
406 return got_error_from_errno("imsg_add TREE_REQUEST");
410 imsg_close(ibuf, wbuf);
412 return flush_imsg(ibuf);
415 const struct got_error *
416 got_privsep_send_tag_req(struct imsgbuf *ibuf, int fd,
417 struct got_object_id *id, int pack_idx)
419 struct got_imsg_packed_object iobj;
423 memset(&iobj, 0, sizeof(iobj));
424 if (pack_idx != -1) { /* tag is packed */
426 memcpy(&iobj.id, id, sizeof(iobj.id));
434 if (imsg_compose(ibuf, GOT_IMSG_TAG_REQUEST, 0, 0, fd, data, len)
436 return got_error_from_errno("imsg_compose TAG_REQUEST");
438 return flush_imsg(ibuf);
441 const struct got_error *
442 got_privsep_send_blob_req(struct imsgbuf *ibuf, int infd,
443 struct got_object_id *id, int pack_idx)
445 const struct got_error *err = NULL;
446 struct got_imsg_packed_object iobj;
450 memset(&iobj, 0, sizeof(iobj));
451 if (pack_idx != -1) { /* blob is packed */
453 memcpy(&iobj.id, id, sizeof(iobj.id));
461 if (imsg_compose(ibuf, GOT_IMSG_BLOB_REQUEST, 0, 0, infd, data, len)
463 err = got_error_from_errno("imsg_compose BLOB_REQUEST");
468 return flush_imsg(ibuf);
471 const struct got_error *
472 got_privsep_send_blob_outfd(struct imsgbuf *ibuf, int outfd)
474 const struct got_error *err = NULL;
476 if (imsg_compose(ibuf, GOT_IMSG_BLOB_OUTFD, 0, 0, outfd, NULL, 0)
478 err = got_error_from_errno("imsg_compose BLOB_OUTFD");
483 return flush_imsg(ibuf);
486 static const struct got_error *
487 send_fd(struct imsgbuf *ibuf, int imsg_code, int fd)
489 const struct got_error *err = NULL;
491 if (imsg_compose(ibuf, imsg_code, 0, 0, fd, NULL, 0) == -1) {
492 err = got_error_from_errno("imsg_compose TMPFD");
497 return flush_imsg(ibuf);
500 const struct got_error *
501 got_privsep_send_tmpfd(struct imsgbuf *ibuf, int fd)
503 return send_fd(ibuf, GOT_IMSG_TMPFD, fd);
506 const struct got_error *
507 got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj)
509 struct got_imsg_object iobj;
511 memset(&iobj, 0, sizeof(iobj));
513 memcpy(&iobj.id, &obj->id, sizeof(iobj.id));
514 iobj.type = obj->type;
515 iobj.flags = obj->flags;
516 iobj.hdrlen = obj->hdrlen;
517 iobj.size = obj->size;
518 if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
519 iobj.pack_offset = obj->pack_offset;
520 iobj.pack_idx = obj->pack_idx;
523 if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
525 return got_error_from_errno("imsg_compose OBJECT");
527 return flush_imsg(ibuf);
530 const struct got_error *
531 got_privsep_send_fetch_req(struct imsgbuf *ibuf, int fd,
532 struct got_pathlist_head *have_refs, int fetch_all_branches,
533 struct got_pathlist_head *wanted_branches,
534 struct got_pathlist_head *wanted_refs, int list_refs_only,
535 const char *worktree_branch, int no_head, int verbosity)
537 const struct got_error *err = NULL;
539 size_t len, worktree_branch_len;
540 struct got_pathlist_entry *pe;
541 struct got_imsg_fetch_request fetchreq;
543 if (worktree_branch) {
544 worktree_branch_len = strlen(worktree_branch);
545 len = sizeof(fetchreq) + worktree_branch_len;
547 len = sizeof(fetchreq);
549 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
551 return got_error(GOT_ERR_NO_SPACE);
554 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_REQUEST, 0, 0, len);
556 return got_error_from_errno("imsg_create FETCH_HAVE_REF");
558 memset(&fetchreq, 0, sizeof(fetchreq));
559 fetchreq.no_head = no_head;
560 fetchreq.fetch_all_branches = fetch_all_branches;
561 fetchreq.list_refs_only = list_refs_only;
562 fetchreq.verbosity = verbosity;
563 if (worktree_branch != NULL)
564 fetchreq.worktree_branch_len = worktree_branch_len;
565 TAILQ_FOREACH(pe, have_refs, entry)
566 fetchreq.n_have_refs++;
567 TAILQ_FOREACH(pe, wanted_branches, entry)
568 fetchreq.n_wanted_branches++;
569 TAILQ_FOREACH(pe, wanted_refs, entry)
570 fetchreq.n_wanted_refs++;
571 if (imsg_add(wbuf, &fetchreq, sizeof(fetchreq)) == -1)
572 return got_error_from_errno("imsg_add FETCH_REQUEST");
573 if (worktree_branch) {
574 if (imsg_add(wbuf, worktree_branch, worktree_branch_len) == -1)
575 return got_error_from_errno("imsg_add FETCH_REQUEST");
578 imsg_close(ibuf, wbuf);
580 err = flush_imsg(ibuf);
587 TAILQ_FOREACH(pe, have_refs, entry) {
588 const char *name = pe->path;
589 size_t name_len = pe->path_len;
590 struct got_object_id *id = pe->data;
592 len = sizeof(struct got_imsg_fetch_have_ref) + name_len;
593 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_HAVE_REF, 0, 0, len);
595 return got_error_from_errno("imsg_create FETCH_HAVE_REF");
597 /* Keep in sync with struct got_imsg_fetch_have_ref! */
598 if (imsg_add(wbuf, id, sizeof(*id)) == -1)
599 return got_error_from_errno("imsg_add FETCH_HAVE_REF");
600 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1)
601 return got_error_from_errno("imsg_add FETCH_HAVE_REF");
602 if (imsg_add(wbuf, name, name_len) == -1)
603 return got_error_from_errno("imsg_add FETCH_HAVE_REF");
606 imsg_close(ibuf, wbuf);
607 err = flush_imsg(ibuf);
612 TAILQ_FOREACH(pe, wanted_branches, entry) {
613 const char *name = pe->path;
614 size_t name_len = pe->path_len;
616 len = sizeof(struct got_imsg_fetch_wanted_branch) + name_len;
617 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_WANTED_BRANCH, 0, 0,
620 return got_error_from_errno(
621 "imsg_create FETCH_WANTED_BRANCH");
623 /* Keep in sync with struct got_imsg_fetch_wanted_branch! */
624 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1)
625 return got_error_from_errno(
626 "imsg_add FETCH_WANTED_BRANCH");
627 if (imsg_add(wbuf, name, name_len) == -1)
628 return got_error_from_errno(
629 "imsg_add FETCH_WANTED_BRANCH");
632 imsg_close(ibuf, wbuf);
633 err = flush_imsg(ibuf);
638 TAILQ_FOREACH(pe, wanted_refs, entry) {
639 const char *name = pe->path;
640 size_t name_len = pe->path_len;
642 len = sizeof(struct got_imsg_fetch_wanted_ref) + name_len;
643 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_WANTED_REF, 0, 0,
646 return got_error_from_errno(
647 "imsg_create FETCH_WANTED_REF");
649 /* Keep in sync with struct got_imsg_fetch_wanted_ref! */
650 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1)
651 return got_error_from_errno(
652 "imsg_add FETCH_WANTED_REF");
653 if (imsg_add(wbuf, name, name_len) == -1)
654 return got_error_from_errno(
655 "imsg_add FETCH_WANTED_REF");
658 imsg_close(ibuf, wbuf);
659 err = flush_imsg(ibuf);
669 const struct got_error *
670 got_privsep_send_fetch_outfd(struct imsgbuf *ibuf, int fd)
672 return send_fd(ibuf, GOT_IMSG_FETCH_OUTFD, fd);
675 const struct got_error *
676 got_privsep_recv_fetch_progress(int *done, struct got_object_id **id,
677 char **refname, struct got_pathlist_head *symrefs, char **server_progress,
678 off_t *packfile_size, uint8_t *pack_sha1, struct imsgbuf *ibuf)
680 const struct got_error *err = NULL;
683 struct got_imsg_fetch_symrefs *isymrefs = NULL;
691 *server_progress = NULL;
693 memset(pack_sha1, 0, SHA1_DIGEST_LENGTH);
695 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
699 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
700 switch (imsg.hdr.type) {
701 case GOT_IMSG_FETCH_SYMREFS:
702 if (datalen < sizeof(*isymrefs)) {
703 err = got_error(GOT_ERR_PRIVSEP_LEN);
706 if (isymrefs != NULL) {
707 err = got_error(GOT_ERR_PRIVSEP_MSG);
710 isymrefs = (struct got_imsg_fetch_symrefs *)imsg.data;
711 off = sizeof(*isymrefs);
712 remain = datalen - off;
713 for (n = 0; n < isymrefs->nsymrefs; n++) {
714 struct got_imsg_fetch_symref *s;
716 if (remain < sizeof(struct got_imsg_fetch_symref)) {
717 err = got_error(GOT_ERR_PRIVSEP_LEN);
720 s = (struct got_imsg_fetch_symref *)(imsg.data + off);
722 remain -= sizeof(*s);
723 if (remain < s->name_len) {
724 err = got_error(GOT_ERR_PRIVSEP_LEN);
727 name = strndup(imsg.data + off, s->name_len);
729 err = got_error_from_errno("strndup");
733 remain -= s->name_len;
734 if (remain < s->target_len) {
735 err = got_error(GOT_ERR_PRIVSEP_LEN);
739 target = strndup(imsg.data + off, s->target_len);
740 if (target == NULL) {
741 err = got_error_from_errno("strndup");
745 off += s->target_len;
746 remain -= s->target_len;
747 err = got_pathlist_append(symrefs, name, target);
755 case GOT_IMSG_FETCH_REF:
756 if (datalen <= sizeof(**id)) {
757 err = got_error(GOT_ERR_PRIVSEP_MSG);
760 *id = malloc(sizeof(**id));
762 err = got_error_from_errno("malloc");
765 memcpy(*id, imsg.data, sizeof(**id));
766 *refname = strndup(imsg.data + sizeof(**id),
767 datalen - sizeof(**id));
768 if (*refname == NULL) {
769 err = got_error_from_errno("strndup");
773 case GOT_IMSG_FETCH_SERVER_PROGRESS:
775 err = got_error(GOT_ERR_PRIVSEP_LEN);
778 *server_progress = strndup(imsg.data, datalen);
779 if (*server_progress == NULL) {
780 err = got_error_from_errno("strndup");
783 for (i = 0; i < datalen; i++) {
784 if (!isprint((unsigned char)(*server_progress)[i]) &&
785 !isspace((unsigned char)(*server_progress)[i])) {
786 err = got_error(GOT_ERR_PRIVSEP_MSG);
787 free(*server_progress);
788 *server_progress = NULL;
793 case GOT_IMSG_FETCH_DOWNLOAD_PROGRESS:
794 if (datalen < sizeof(*packfile_size)) {
795 err = got_error(GOT_ERR_PRIVSEP_MSG);
798 memcpy(packfile_size, imsg.data, sizeof(*packfile_size));
800 case GOT_IMSG_FETCH_DONE:
801 if (datalen != SHA1_DIGEST_LENGTH) {
802 err = got_error(GOT_ERR_PRIVSEP_MSG);
805 memcpy(pack_sha1, imsg.data, SHA1_DIGEST_LENGTH);
809 err = got_error(GOT_ERR_PRIVSEP_MSG);
823 static const struct got_error *
824 send_send_ref(const char *name, size_t name_len, struct got_object_id *id,
825 int delete, struct imsgbuf *ibuf)
830 len = sizeof(struct got_imsg_send_ref) + name_len;
831 wbuf = imsg_create(ibuf, GOT_IMSG_SEND_REF, 0, 0, len);
833 return got_error_from_errno("imsg_create SEND_REF");
835 /* Keep in sync with struct got_imsg_send_ref! */
836 if (imsg_add(wbuf, id, sizeof(*id)) == -1)
837 return got_error_from_errno("imsg_add SEND_REF");
838 if (imsg_add(wbuf, &delete, sizeof(delete)) == -1)
839 return got_error_from_errno("imsg_add SEND_REF");
840 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1)
841 return got_error_from_errno("imsg_add SEND_REF");
842 if (imsg_add(wbuf, name, name_len) == -1)
843 return got_error_from_errno("imsg_add SEND_REF");
846 imsg_close(ibuf, wbuf);
847 return flush_imsg(ibuf);
850 const struct got_error *
851 got_privsep_send_send_req(struct imsgbuf *ibuf, int fd,
852 struct got_pathlist_head *have_refs,
853 struct got_pathlist_head *delete_refs,
856 const struct got_error *err = NULL;
857 struct got_pathlist_entry *pe;
858 struct got_imsg_send_request sendreq;
859 struct got_object_id zero_id;
861 memset(&zero_id, 0, sizeof(zero_id));
862 memset(&sendreq, 0, sizeof(sendreq));
863 sendreq.verbosity = verbosity;
864 TAILQ_FOREACH(pe, have_refs, entry)
866 TAILQ_FOREACH(pe, delete_refs, entry)
868 if (imsg_compose(ibuf, GOT_IMSG_SEND_REQUEST, 0, 0, fd,
869 &sendreq, sizeof(sendreq)) == -1) {
870 err = got_error_from_errno(
871 "imsg_compose FETCH_SERVER_PROGRESS");
875 err = flush_imsg(ibuf);
880 TAILQ_FOREACH(pe, have_refs, entry) {
881 const char *name = pe->path;
882 size_t name_len = pe->path_len;
883 struct got_object_id *id = pe->data;
884 err = send_send_ref(name, name_len, id, 0, ibuf);
889 TAILQ_FOREACH(pe, delete_refs, entry) {
890 const char *name = pe->path;
891 size_t name_len = pe->path_len;
892 err = send_send_ref(name, name_len, &zero_id, 1, ibuf);
897 if (fd != -1 && close(fd) == -1 && err == NULL)
898 err = got_error_from_errno("close");
903 const struct got_error *
904 got_privsep_recv_send_remote_refs(struct got_pathlist_head *remote_refs,
905 struct imsgbuf *ibuf)
907 const struct got_error *err = NULL;
911 struct got_imsg_send_remote_ref iremote_ref;
912 struct got_object_id *id = NULL;
913 char *refname = NULL;
914 struct got_pathlist_entry *new;
917 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
920 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
921 switch (imsg.hdr.type) {
922 case GOT_IMSG_SEND_REMOTE_REF:
923 if (datalen < sizeof(iremote_ref)) {
924 err = got_error(GOT_ERR_PRIVSEP_MSG);
927 memcpy(&iremote_ref, imsg.data, sizeof(iremote_ref));
928 if (datalen != sizeof(iremote_ref) +
929 iremote_ref.name_len) {
930 err = got_error(GOT_ERR_PRIVSEP_MSG);
933 id = malloc(sizeof(*id));
935 err = got_error_from_errno("malloc");
938 memcpy(id, &iremote_ref.id, sizeof(*id));
939 refname = strndup(imsg.data + sizeof(iremote_ref),
940 datalen - sizeof(iremote_ref));
941 if (refname == NULL) {
942 err = got_error_from_errno("strndup");
945 err = got_pathlist_insert(&new, remote_refs,
949 if (new == NULL) { /* duplicate which wasn't inserted */
956 case GOT_IMSG_SEND_PACK_REQUEST:
958 err = got_error(GOT_ERR_PRIVSEP_MSG);
961 /* got-send-pack is now waiting for a pack file. */
965 err = got_error(GOT_ERR_PRIVSEP_MSG);
976 const struct got_error *
977 got_privsep_send_packfd(struct imsgbuf *ibuf, int fd)
979 return send_fd(ibuf, GOT_IMSG_SEND_PACKFD, fd);
982 const struct got_error *
983 got_privsep_recv_send_progress(int *done, off_t *bytes_sent,
984 int *success, char **refname, char **errmsg, struct imsgbuf *ibuf)
986 const struct got_error *err = NULL;
989 struct got_imsg_send_ref_status iref_status;
991 /* Do not reset the current value of 'bytes_sent', it accumulates. */
997 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1001 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1002 switch (imsg.hdr.type) {
1003 case GOT_IMSG_SEND_UPLOAD_PROGRESS:
1004 if (datalen < sizeof(*bytes_sent)) {
1005 err = got_error(GOT_ERR_PRIVSEP_MSG);
1008 memcpy(bytes_sent, imsg.data, sizeof(*bytes_sent));
1010 case GOT_IMSG_SEND_REF_STATUS:
1011 if (datalen < sizeof(iref_status)) {
1012 err = got_error(GOT_ERR_PRIVSEP_MSG);
1015 memcpy(&iref_status, imsg.data, sizeof(iref_status));
1016 if (datalen != sizeof(iref_status) + iref_status.name_len +
1017 iref_status.errmsg_len) {
1018 err = got_error(GOT_ERR_PRIVSEP_MSG);
1021 *success = iref_status.success;
1022 *refname = strndup(imsg.data + sizeof(iref_status),
1023 iref_status.name_len);
1025 if (iref_status.errmsg_len != 0)
1026 *errmsg = strndup(imsg.data + sizeof(iref_status) +
1027 iref_status.name_len, iref_status.errmsg_len);
1029 case GOT_IMSG_SEND_DONE:
1031 err = got_error(GOT_ERR_PRIVSEP_MSG);
1037 err = got_error(GOT_ERR_PRIVSEP_MSG);
1045 const struct got_error *
1046 got_privsep_send_index_pack_req(struct imsgbuf *ibuf, uint8_t *pack_sha1,
1049 const struct got_error *err = NULL;
1051 /* Keep in sync with struct got_imsg_index_pack_request */
1052 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_REQUEST, 0, 0, fd,
1053 pack_sha1, SHA1_DIGEST_LENGTH) == -1) {
1054 err = got_error_from_errno("imsg_compose INDEX_REQUEST");
1058 return flush_imsg(ibuf);
1061 const struct got_error *
1062 got_privsep_send_index_pack_outfd(struct imsgbuf *ibuf, int fd)
1064 return send_fd(ibuf, GOT_IMSG_IDXPACK_OUTFD, fd);
1067 const struct got_error *
1068 got_privsep_recv_index_progress(int *done, int *nobj_total,
1069 int *nobj_indexed, int *nobj_loose, int *nobj_resolved,
1070 struct imsgbuf *ibuf)
1072 const struct got_error *err = NULL;
1074 struct got_imsg_index_pack_progress *iprogress;
1082 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1086 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1087 switch (imsg.hdr.type) {
1088 case GOT_IMSG_IDXPACK_PROGRESS:
1089 if (datalen < sizeof(*iprogress)) {
1090 err = got_error(GOT_ERR_PRIVSEP_LEN);
1093 iprogress = (struct got_imsg_index_pack_progress *)imsg.data;
1094 if (iprogress->nobj_total < 0 || iprogress->nobj_indexed < 0 ||
1095 iprogress->nobj_loose < 0 || iprogress->nobj_resolved < 0) {
1096 err = got_error(GOT_ERR_RANGE);
1099 *nobj_total = iprogress->nobj_total;
1100 *nobj_indexed = iprogress->nobj_indexed;
1101 *nobj_loose = iprogress->nobj_loose;
1102 *nobj_resolved = iprogress->nobj_resolved;
1104 case GOT_IMSG_IDXPACK_DONE:
1106 err = got_error(GOT_ERR_PRIVSEP_LEN);
1112 err = got_error(GOT_ERR_PRIVSEP_MSG);
1120 const struct got_error *
1121 got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
1122 struct imsgbuf *ibuf)
1124 struct got_imsg_object *iobj;
1125 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1127 if (datalen != sizeof(*iobj))
1128 return got_error(GOT_ERR_PRIVSEP_LEN);
1131 if (iobj->pack_offset < 0)
1132 return got_error(GOT_ERR_PACK_OFFSET);
1134 *obj = calloc(1, sizeof(**obj));
1136 return got_error_from_errno("calloc");
1138 memcpy(&(*obj)->id, &iobj->id, sizeof(iobj->id));
1139 (*obj)->type = iobj->type;
1140 (*obj)->flags = iobj->flags;
1141 (*obj)->hdrlen = iobj->hdrlen;
1142 (*obj)->size = iobj->size;
1143 /* path_packfile is handled by caller */
1144 if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
1145 (*obj)->pack_offset = iobj->pack_offset;
1146 (*obj)->pack_idx = iobj->pack_idx;
1148 STAILQ_INIT(&(*obj)->deltas.entries);
1152 const struct got_error *
1153 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
1155 const struct got_error *err = NULL;
1157 const size_t min_datalen =
1158 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
1162 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1166 switch (imsg.hdr.type) {
1167 case GOT_IMSG_OBJECT:
1168 err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
1171 err = got_error(GOT_ERR_PRIVSEP_MSG);
1180 static const struct got_error *
1181 send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
1184 const struct got_error *err = NULL;
1185 size_t offset, remain;
1188 remain = logmsg_len;
1189 while (remain > 0) {
1190 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1192 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
1193 commit->logmsg + offset, n) == -1) {
1194 err = got_error_from_errno("imsg_compose "
1199 err = flush_imsg(ibuf);
1210 const struct got_error *
1211 got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
1213 const struct got_error *err = NULL;
1214 struct got_imsg_commit_object *icommit;
1217 struct got_object_qid *qid;
1218 size_t author_len = strlen(commit->author);
1219 size_t committer_len = strlen(commit->committer);
1220 size_t logmsg_len = strlen(commit->logmsg);
1222 total = sizeof(*icommit) + author_len + committer_len +
1223 commit->nparents * sizeof(struct got_object_id);
1225 buf = malloc(total);
1227 return got_error_from_errno("malloc");
1229 icommit = (struct got_imsg_commit_object *)buf;
1230 memcpy(&icommit->tree_id, commit->tree_id, sizeof(icommit->tree_id));
1231 icommit->author_len = author_len;
1232 icommit->author_time = commit->author_time;
1233 icommit->author_gmtoff = commit->author_gmtoff;
1234 icommit->committer_len = committer_len;
1235 icommit->committer_time = commit->committer_time;
1236 icommit->committer_gmtoff = commit->committer_gmtoff;
1237 icommit->logmsg_len = logmsg_len;
1238 icommit->nparents = commit->nparents;
1240 len = sizeof(*icommit);
1241 memcpy(buf + len, commit->author, author_len);
1243 memcpy(buf + len, commit->committer, committer_len);
1244 len += committer_len;
1245 STAILQ_FOREACH(qid, &commit->parent_ids, entry) {
1246 memcpy(buf + len, &qid->id, sizeof(qid->id));
1247 len += sizeof(qid->id);
1250 if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
1251 err = got_error_from_errno("imsg_compose COMMIT");
1255 if (logmsg_len == 0 ||
1256 logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1257 err = flush_imsg(ibuf);
1261 err = send_commit_logmsg(ibuf, commit, logmsg_len);
1267 static const struct got_error *
1268 get_commit_from_imsg(struct got_commit_object **commit,
1269 struct imsg *imsg, size_t datalen, struct imsgbuf *ibuf)
1271 const struct got_error *err = NULL;
1272 struct got_imsg_commit_object *icommit;
1276 if (datalen < sizeof(*icommit))
1277 return got_error(GOT_ERR_PRIVSEP_LEN);
1279 icommit = imsg->data;
1280 if (datalen != sizeof(*icommit) + icommit->author_len +
1281 icommit->committer_len +
1282 icommit->nparents * sizeof(struct got_object_id))
1283 return got_error(GOT_ERR_PRIVSEP_LEN);
1285 if (icommit->nparents < 0)
1286 return got_error(GOT_ERR_PRIVSEP_LEN);
1288 len += sizeof(*icommit);
1290 *commit = got_object_commit_alloc_partial();
1291 if (*commit == NULL)
1292 return got_error_from_errno(
1293 "got_object_commit_alloc_partial");
1295 memcpy((*commit)->tree_id, &icommit->tree_id,
1296 sizeof(icommit->tree_id));
1297 (*commit)->author_time = icommit->author_time;
1298 (*commit)->author_gmtoff = icommit->author_gmtoff;
1299 (*commit)->committer_time = icommit->committer_time;
1300 (*commit)->committer_gmtoff = icommit->committer_gmtoff;
1302 (*commit)->author = strndup(imsg->data + len, icommit->author_len);
1303 if ((*commit)->author == NULL) {
1304 err = got_error_from_errno("strndup");
1307 len += icommit->author_len;
1309 (*commit)->committer = strndup(imsg->data + len,
1310 icommit->committer_len);
1311 if ((*commit)->committer == NULL) {
1312 err = got_error_from_errno("strndup");
1315 len += icommit->committer_len;
1317 if (icommit->logmsg_len == 0) {
1318 (*commit)->logmsg = strdup("");
1319 if ((*commit)->logmsg == NULL) {
1320 err = got_error_from_errno("strdup");
1324 size_t offset = 0, remain = icommit->logmsg_len;
1326 (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
1327 if ((*commit)->logmsg == NULL) {
1328 err = got_error_from_errno("malloc");
1331 while (remain > 0) {
1332 struct imsg imsg_log;
1333 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1336 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1340 if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG) {
1341 err = got_error(GOT_ERR_PRIVSEP_MSG);
1345 memcpy((*commit)->logmsg + offset,
1347 imsg_free(&imsg_log);
1351 (*commit)->logmsg[icommit->logmsg_len] = '\0';
1354 for (i = 0; i < icommit->nparents; i++) {
1355 struct got_object_qid *qid;
1357 err = got_object_qid_alloc_partial(&qid);
1360 memcpy(&qid->id, imsg->data + len +
1361 i * sizeof(qid->id), sizeof(qid->id));
1362 STAILQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
1363 (*commit)->nparents++;
1367 got_object_commit_close(*commit);
1373 const struct got_error *
1374 got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
1376 const struct got_error *err = NULL;
1379 const size_t min_datalen =
1380 MIN(sizeof(struct got_imsg_error),
1381 sizeof(struct got_imsg_commit_object));
1385 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1389 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1391 switch (imsg.hdr.type) {
1392 case GOT_IMSG_COMMIT:
1393 err = get_commit_from_imsg(commit, &imsg, datalen, ibuf);
1396 err = got_error(GOT_ERR_PRIVSEP_MSG);
1405 static const struct got_error *
1406 send_tree_entries_batch(struct imsgbuf *ibuf,
1407 struct got_parsed_tree_entry *entries, int idx0, int idxN, size_t len)
1410 struct got_imsg_tree_entries ientries;
1413 memset(&ientries, 0, sizeof(ientries));
1415 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_ENTRIES, 0, 0, len);
1417 return got_error_from_errno("imsg_create TREE_ENTRY");
1419 ientries.nentries = idxN - idx0 + 1;
1420 if (imsg_add(wbuf, &ientries, sizeof(ientries)) == -1)
1421 return got_error_from_errno("imsg_add TREE_ENTRY");
1423 for (i = idx0; i <= idxN; i++) {
1424 struct got_parsed_tree_entry *pte = &entries[i];
1426 /* Keep in sync with struct got_imsg_tree_entry definition! */
1427 if (imsg_add(wbuf, pte->id, SHA1_DIGEST_LENGTH) == -1)
1428 return got_error_from_errno("imsg_add TREE_ENTRY");
1429 if (imsg_add(wbuf, &pte->mode, sizeof(pte->mode)) == -1)
1430 return got_error_from_errno("imsg_add TREE_ENTRY");
1431 if (imsg_add(wbuf, &pte->namelen, sizeof(pte->namelen)) == -1)
1432 return got_error_from_errno("imsg_add TREE_ENTRY");
1434 /* Remaining bytes are the entry's name. */
1435 if (imsg_add(wbuf, pte->name, pte->namelen) == -1)
1436 return got_error_from_errno("imsg_add TREE_ENTRY");
1440 imsg_close(ibuf, wbuf);
1444 static const struct got_error *
1445 send_tree_entries(struct imsgbuf *ibuf, struct got_parsed_tree_entry *entries,
1448 const struct got_error *err = NULL;
1450 size_t entries_len = sizeof(struct got_imsg_tree_entries);
1453 for (j = 0; j < nentries; j++) {
1454 struct got_parsed_tree_entry *pte = &entries[j];
1455 size_t len = SHA1_DIGEST_LENGTH + sizeof(pte->mode) +
1456 sizeof(pte->namelen) + pte->namelen;
1459 entries_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1460 err = send_tree_entries_batch(ibuf, entries,
1461 i, j - 1, entries_len);
1465 entries_len = sizeof(struct got_imsg_tree_entries);
1472 err = send_tree_entries_batch(ibuf, entries, i, j - 1,
1481 const struct got_error *
1482 got_privsep_send_tree(struct imsgbuf *ibuf,
1483 struct got_parsed_tree_entry *entries, int nentries)
1485 const struct got_error *err = NULL;
1486 struct got_imsg_tree_object itree;
1488 memset(&itree, 0, sizeof(itree));
1489 itree.nentries = nentries;
1490 if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
1492 return got_error_from_errno("imsg_compose TREE");
1494 err = send_tree_entries(ibuf, entries, nentries);
1498 return flush_imsg(ibuf);
1502 static const struct got_error *
1503 recv_tree_entries(void *data, size_t datalen, struct got_tree_object *tree,
1506 const struct got_error *err = NULL;
1507 struct got_imsg_tree_entries *ientries;
1508 struct got_tree_entry *te;
1512 if (datalen <= sizeof(*ientries) ||
1513 datalen > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
1514 return got_error(GOT_ERR_PRIVSEP_LEN);
1516 ientries = (struct got_imsg_tree_entries *)data;
1517 if (ientries->nentries > INT_MAX) {
1518 return got_error_msg(GOT_ERR_NO_SPACE,
1519 "too many tree entries");
1522 te_offset = sizeof(*ientries);
1523 for (i = 0; i < ientries->nentries; i++) {
1524 struct got_imsg_tree_entry ite;
1525 const char *te_name;
1526 uint8_t *buf = (uint8_t *)data + te_offset;
1528 if (te_offset >= datalen) {
1529 err = got_error(GOT_ERR_PRIVSEP_LEN);
1533 /* Might not be aligned, size is ~32 bytes. */
1534 memcpy(&ite, buf, sizeof(ite));
1536 if (ite.namelen >= sizeof(te->name)) {
1537 err = got_error(GOT_ERR_PRIVSEP_LEN);
1540 if (te_offset + sizeof(ite) + ite.namelen > datalen) {
1541 err = got_error(GOT_ERR_PRIVSEP_LEN);
1545 if (*nentries >= tree->nentries) {
1546 err = got_error(GOT_ERR_PRIVSEP_LEN);
1549 te = &tree->entries[*nentries];
1550 te_name = buf + sizeof(ite);
1551 memcpy(te->name, te_name, ite.namelen);
1552 te->name[ite.namelen] = '\0';
1553 memcpy(te->id.sha1, ite.id, SHA1_DIGEST_LENGTH);
1554 te->mode = ite.mode;
1555 te->idx = *nentries;
1558 te_offset += sizeof(ite) + ite.namelen;
1564 const struct got_error *
1565 got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
1567 const struct got_error *err = NULL;
1568 const size_t min_datalen =
1569 MIN(sizeof(struct got_imsg_error),
1570 sizeof(struct got_imsg_tree_object));
1571 struct got_imsg_tree_object *itree;
1576 while (*tree == NULL || nentries < (*tree)->nentries) {
1580 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1584 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1586 switch (imsg.hdr.type) {
1588 /* This message should only appear once. */
1589 if (*tree != NULL) {
1590 err = got_error(GOT_ERR_PRIVSEP_MSG);
1593 if (datalen != sizeof(*itree)) {
1594 err = got_error(GOT_ERR_PRIVSEP_LEN);
1598 if (itree->nentries < 0) {
1599 err = got_error(GOT_ERR_PRIVSEP_LEN);
1602 *tree = malloc(sizeof(**tree));
1603 if (*tree == NULL) {
1604 err = got_error_from_errno("malloc");
1607 (*tree)->entries = calloc(itree->nentries,
1608 sizeof(struct got_tree_entry));
1609 if ((*tree)->entries == NULL) {
1610 err = got_error_from_errno("malloc");
1615 (*tree)->nentries = itree->nentries;
1616 (*tree)->refcnt = 0;
1618 case GOT_IMSG_TREE_ENTRIES:
1619 /* This message should be preceeded by GOT_IMSG_TREE. */
1620 if (*tree == NULL) {
1621 err = got_error(GOT_ERR_PRIVSEP_MSG);
1624 err = recv_tree_entries(imsg.data, datalen,
1628 err = got_error(GOT_ERR_PRIVSEP_MSG);
1637 if (*tree && (*tree)->nentries != nentries) {
1639 err = got_error(GOT_ERR_PRIVSEP_LEN);
1640 got_object_tree_close(*tree);
1647 const struct got_error *
1648 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
1649 const uint8_t *data)
1651 struct got_imsg_blob iblob;
1653 memset(&iblob, 0, sizeof(iblob));
1655 iblob.hdrlen = hdrlen;
1660 if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
1661 return got_error(GOT_ERR_NO_SPACE);
1663 buf = malloc(sizeof(iblob) + size);
1665 return got_error_from_errno("malloc");
1667 memcpy(buf, &iblob, sizeof(iblob));
1668 memcpy(buf + sizeof(iblob), data, size);
1669 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
1670 sizeof(iblob) + size) == -1) {
1672 return got_error_from_errno("imsg_compose BLOB");
1676 /* Data has already been written to file descriptor. */
1677 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
1678 sizeof(iblob)) == -1)
1679 return got_error_from_errno("imsg_compose BLOB");
1683 return flush_imsg(ibuf);
1686 const struct got_error *
1687 got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1688 struct imsgbuf *ibuf)
1690 const struct got_error *err = NULL;
1692 struct got_imsg_blob *iblob;
1697 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1701 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1703 switch (imsg.hdr.type) {
1705 if (datalen < sizeof(*iblob)) {
1706 err = got_error(GOT_ERR_PRIVSEP_LEN);
1710 *size = iblob->size;
1711 *hdrlen = iblob->hdrlen;
1713 if (datalen == sizeof(*iblob)) {
1714 /* Data has been written to file descriptor. */
1718 if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX ||
1719 *size > datalen + sizeof(*iblob)) {
1720 err = got_error(GOT_ERR_PRIVSEP_LEN);
1724 *outbuf = malloc(*size);
1725 if (*outbuf == NULL) {
1726 err = got_error_from_errno("malloc");
1729 memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
1732 err = got_error(GOT_ERR_PRIVSEP_MSG);
1741 static const struct got_error *
1742 send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
1744 const struct got_error *err = NULL;
1745 size_t offset, remain;
1748 remain = tagmsg_len;
1749 while (remain > 0) {
1750 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1752 if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
1753 tag->tagmsg + offset, n) == -1) {
1754 err = got_error_from_errno("imsg_compose TAG_TAGMSG");
1758 err = flush_imsg(ibuf);
1769 const struct got_error *
1770 got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
1772 const struct got_error *err = NULL;
1773 struct got_imsg_tag_object *itag;
1776 size_t tag_len = strlen(tag->tag);
1777 size_t tagger_len = strlen(tag->tagger);
1778 size_t tagmsg_len = strlen(tag->tagmsg);
1780 total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
1782 buf = malloc(total);
1784 return got_error_from_errno("malloc");
1786 itag = (struct got_imsg_tag_object *)buf;
1787 memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
1788 itag->obj_type = tag->obj_type;
1789 itag->tag_len = tag_len;
1790 itag->tagger_len = tagger_len;
1791 itag->tagger_time = tag->tagger_time;
1792 itag->tagger_gmtoff = tag->tagger_gmtoff;
1793 itag->tagmsg_len = tagmsg_len;
1795 len = sizeof(*itag);
1796 memcpy(buf + len, tag->tag, tag_len);
1798 memcpy(buf + len, tag->tagger, tagger_len);
1801 if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
1802 err = got_error_from_errno("imsg_compose TAG");
1806 if (tagmsg_len == 0 ||
1807 tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1808 err = flush_imsg(ibuf);
1812 err = send_tagmsg(ibuf, tag, tagmsg_len);
1818 const struct got_error *
1819 got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
1821 const struct got_error *err = NULL;
1823 struct got_imsg_tag_object *itag;
1824 size_t len, datalen;
1825 const size_t min_datalen =
1826 MIN(sizeof(struct got_imsg_error),
1827 sizeof(struct got_imsg_tag_object));
1831 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1835 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1838 switch (imsg.hdr.type) {
1840 if (datalen < sizeof(*itag)) {
1841 err = got_error(GOT_ERR_PRIVSEP_LEN);
1845 if (datalen != sizeof(*itag) + itag->tag_len +
1847 err = got_error(GOT_ERR_PRIVSEP_LEN);
1850 len += sizeof(*itag);
1852 *tag = calloc(1, sizeof(**tag));
1854 err = got_error_from_errno("calloc");
1858 memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
1860 (*tag)->tag = strndup(imsg.data + len, itag->tag_len);
1861 if ((*tag)->tag == NULL) {
1862 err = got_error_from_errno("strndup");
1865 len += itag->tag_len;
1867 (*tag)->obj_type = itag->obj_type;
1868 (*tag)->tagger_time = itag->tagger_time;
1869 (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1871 (*tag)->tagger = strndup(imsg.data + len, itag->tagger_len);
1872 if ((*tag)->tagger == NULL) {
1873 err = got_error_from_errno("strndup");
1876 len += itag->tagger_len;
1878 if (itag->tagmsg_len == 0) {
1879 (*tag)->tagmsg = strdup("");
1880 if ((*tag)->tagmsg == NULL) {
1881 err = got_error_from_errno("strdup");
1885 size_t offset = 0, remain = itag->tagmsg_len;
1887 (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1888 if ((*tag)->tagmsg == NULL) {
1889 err = got_error_from_errno("malloc");
1892 while (remain > 0) {
1893 struct imsg imsg_log;
1894 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1897 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1901 if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1902 return got_error(GOT_ERR_PRIVSEP_MSG);
1904 memcpy((*tag)->tagmsg + offset, imsg_log.data,
1906 imsg_free(&imsg_log);
1910 (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1915 err = got_error(GOT_ERR_PRIVSEP_MSG);
1924 const struct got_error *
1925 got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1926 struct got_packidx *packidx)
1928 const struct got_error *err = NULL;
1929 struct got_imsg_packidx ipackidx;
1930 struct got_imsg_pack ipack;
1933 memset(&ipackidx, 0, sizeof(ipackidx));
1934 memset(&ipack, 0, sizeof(ipack));
1936 ipackidx.len = packidx->len;
1937 ipackidx.packfile_size = pack->filesize;
1938 fd = dup(packidx->fd);
1940 return got_error_from_errno("dup");
1942 if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
1943 sizeof(ipackidx)) == -1) {
1944 err = got_error_from_errno("imsg_compose PACKIDX");
1949 if (strlcpy(ipack.path_packfile, pack->path_packfile,
1950 sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
1951 return got_error(GOT_ERR_NO_SPACE);
1952 ipack.filesize = pack->filesize;
1956 return got_error_from_errno("dup");
1958 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
1960 err = got_error_from_errno("imsg_compose PACK");
1965 return flush_imsg(ibuf);
1968 const struct got_error *
1969 got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
1970 struct got_object_id *id)
1972 struct got_imsg_packed_object iobj;
1974 memset(&iobj, 0, sizeof(iobj));
1976 memcpy(&iobj.id, id, sizeof(iobj.id));
1978 if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
1979 &iobj, sizeof(iobj)) == -1)
1980 return got_error_from_errno("imsg_compose "
1981 "PACKED_OBJECT_REQUEST");
1983 return flush_imsg(ibuf);
1986 const struct got_error *
1987 got_privsep_send_packed_raw_obj_req(struct imsgbuf *ibuf, int idx,
1988 struct got_object_id *id)
1990 struct got_imsg_packed_object iobj;
1992 memset(&iobj, 0, sizeof(iobj));
1994 memcpy(&iobj.id, id, sizeof(iobj.id));
1996 if (imsg_compose(ibuf, GOT_IMSG_PACKED_RAW_OBJECT_REQUEST, 0, 0, -1,
1997 &iobj, sizeof(iobj)) == -1)
1998 return got_error_from_errno("imsg_compose "
1999 "PACKED_OBJECT_REQUEST");
2001 return flush_imsg(ibuf);
2004 const struct got_error *
2005 got_privsep_send_gitconfig_parse_req(struct imsgbuf *ibuf, int fd)
2007 const struct got_error *err = NULL;
2009 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_PARSE_REQUEST, 0, 0, fd,
2011 err = got_error_from_errno("imsg_compose "
2012 "GITCONFIG_PARSE_REQUEST");
2017 return flush_imsg(ibuf);
2020 const struct got_error *
2021 got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *ibuf)
2023 if (imsg_compose(ibuf,
2024 GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST, 0, 0, -1,
2026 return got_error_from_errno("imsg_compose "
2027 "GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST");
2029 return flush_imsg(ibuf);
2032 const struct got_error *
2033 got_privsep_send_gitconfig_repository_extensions_req(struct imsgbuf *ibuf)
2035 if (imsg_compose(ibuf,
2036 GOT_IMSG_GITCONFIG_REPOSITORY_EXTENSIONS_REQUEST, 0, 0, -1,
2038 return got_error_from_errno("imsg_compose "
2039 "GITCONFIG_REPOSITORY_EXTENSIONS_REQUEST");
2041 return flush_imsg(ibuf);
2045 const struct got_error *
2046 got_privsep_send_gitconfig_author_name_req(struct imsgbuf *ibuf)
2048 if (imsg_compose(ibuf,
2049 GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST, 0, 0, -1, NULL, 0) == -1)
2050 return got_error_from_errno("imsg_compose "
2051 "GITCONFIG_AUTHOR_NAME_REQUEST");
2053 return flush_imsg(ibuf);
2056 const struct got_error *
2057 got_privsep_send_gitconfig_author_email_req(struct imsgbuf *ibuf)
2059 if (imsg_compose(ibuf,
2060 GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST, 0, 0, -1, NULL, 0) == -1)
2061 return got_error_from_errno("imsg_compose "
2062 "GITCONFIG_AUTHOR_EMAIL_REQUEST");
2064 return flush_imsg(ibuf);
2067 const struct got_error *
2068 got_privsep_send_gitconfig_remotes_req(struct imsgbuf *ibuf)
2070 if (imsg_compose(ibuf,
2071 GOT_IMSG_GITCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
2072 return got_error_from_errno("imsg_compose "
2073 "GITCONFIG_REMOTE_REQUEST");
2075 return flush_imsg(ibuf);
2078 const struct got_error *
2079 got_privsep_send_gitconfig_owner_req(struct imsgbuf *ibuf)
2081 if (imsg_compose(ibuf,
2082 GOT_IMSG_GITCONFIG_OWNER_REQUEST, 0, 0, -1, NULL, 0) == -1)
2083 return got_error_from_errno("imsg_compose "
2084 "GITCONFIG_OWNER_REQUEST");
2086 return flush_imsg(ibuf);
2089 const struct got_error *
2090 got_privsep_recv_gitconfig_str(char **str, struct imsgbuf *ibuf)
2092 const struct got_error *err = NULL;
2098 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2101 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2103 switch (imsg.hdr.type) {
2104 case GOT_IMSG_GITCONFIG_STR_VAL:
2107 *str = strndup(imsg.data, datalen);
2109 err = got_error_from_errno("strndup");
2114 err = got_error(GOT_ERR_PRIVSEP_MSG);
2122 const struct got_error *
2123 got_privsep_recv_gitconfig_pair(char **key, char **val, struct imsgbuf *ibuf)
2125 const struct got_error *err = NULL;
2126 struct got_imsg_gitconfig_pair p;
2133 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2138 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2140 if (imsg.hdr.type != GOT_IMSG_GITCONFIG_PAIR) {
2141 err = got_error(GOT_ERR_PRIVSEP_MSG);
2145 if (datalen < sizeof(p)) {
2146 err = got_error(GOT_ERR_PRIVSEP_LEN);
2150 memcpy(&p, data, sizeof(p));
2153 if (datalen != sizeof(p) + p.klen + p.vlen) {
2154 err = got_error(GOT_ERR_PRIVSEP_LEN);
2158 *key = strndup(data, p.klen);
2160 err = got_error_from_errno("strndup");
2165 *val = strndup(data, p.vlen);
2167 err = got_error_from_errno("strndup");
2176 const struct got_error *
2177 got_privsep_recv_gitconfig_int(int *val, struct imsgbuf *ibuf)
2179 const struct got_error *err = NULL;
2182 const size_t min_datalen =
2183 MIN(sizeof(struct got_imsg_error), sizeof(int));
2187 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2190 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2192 switch (imsg.hdr.type) {
2193 case GOT_IMSG_GITCONFIG_INT_VAL:
2194 if (datalen != sizeof(*val)) {
2195 err = got_error(GOT_ERR_PRIVSEP_LEN);
2198 memcpy(val, imsg.data, sizeof(*val));
2201 err = got_error(GOT_ERR_PRIVSEP_MSG);
2210 free_remote_data(struct got_remote_repo *remote)
2215 free(remote->fetch_url);
2216 free(remote->send_url);
2217 for (i = 0; i < remote->nfetch_branches; i++)
2218 free(remote->fetch_branches[i]);
2219 free(remote->fetch_branches);
2220 for (i = 0; i < remote->nsend_branches; i++)
2221 free(remote->send_branches[i]);
2222 free(remote->send_branches);
2223 for (i = 0; i < remote->nfetch_refs; i++)
2224 free(remote->fetch_refs[i]);
2225 free(remote->fetch_refs);
2228 const struct got_error *
2229 got_privsep_recv_gitconfig_remotes(struct got_remote_repo **remotes,
2230 int *nremotes, struct imsgbuf *ibuf)
2232 const struct got_error *err = NULL;
2235 struct got_imsg_remotes iremotes;
2236 struct got_imsg_remote iremote;
2240 iremotes.nremotes = 0;
2242 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremotes));
2245 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2247 switch (imsg.hdr.type) {
2248 case GOT_IMSG_GITCONFIG_REMOTES:
2249 if (datalen != sizeof(iremotes)) {
2250 err = got_error(GOT_ERR_PRIVSEP_LEN);
2253 memcpy(&iremotes, imsg.data, sizeof(iremotes));
2254 if (iremotes.nremotes == 0) {
2261 return got_error(GOT_ERR_PRIVSEP_MSG);
2266 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(**remotes));
2267 if (*remotes == NULL)
2268 return got_error_from_errno("recallocarray");
2270 while (*nremotes < iremotes.nremotes) {
2271 struct got_remote_repo *remote;
2273 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremote));
2276 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2278 switch (imsg.hdr.type) {
2279 case GOT_IMSG_GITCONFIG_REMOTE:
2280 remote = &(*remotes)[*nremotes];
2281 memset(remote, 0, sizeof(*remote));
2282 if (datalen < sizeof(iremote)) {
2283 err = got_error(GOT_ERR_PRIVSEP_LEN);
2286 memcpy(&iremote, imsg.data, sizeof(iremote));
2287 if (iremote.name_len == 0 ||
2288 iremote.fetch_url_len == 0 ||
2289 iremote.send_url_len == 0 ||
2290 (sizeof(iremote) + iremote.name_len +
2291 iremote.fetch_url_len + iremote.send_url_len) > datalen) {
2292 err = got_error(GOT_ERR_PRIVSEP_LEN);
2295 remote->name = strndup(imsg.data + sizeof(iremote),
2297 if (remote->name == NULL) {
2298 err = got_error_from_errno("strndup");
2301 remote->fetch_url = strndup(imsg.data + sizeof(iremote) +
2302 iremote.name_len, iremote.fetch_url_len);
2303 if (remote->fetch_url == NULL) {
2304 err = got_error_from_errno("strndup");
2305 free_remote_data(remote);
2308 remote->send_url = strndup(imsg.data + sizeof(iremote) +
2309 iremote.name_len + iremote.fetch_url_len,
2310 iremote.send_url_len);
2311 if (remote->send_url == NULL) {
2312 err = got_error_from_errno("strndup");
2313 free_remote_data(remote);
2316 remote->mirror_references = iremote.mirror_references;
2317 remote->fetch_all_branches = iremote.fetch_all_branches;
2318 remote->nfetch_branches = 0;
2319 remote->fetch_branches = NULL;
2320 remote->nsend_branches = 0;
2321 remote->send_branches = NULL;
2322 remote->nfetch_refs = 0;
2323 remote->fetch_refs = NULL;
2327 err = got_error(GOT_ERR_PRIVSEP_MSG);
2338 for (i = 0; i < *nremotes; i++)
2339 free_remote_data(&(*remotes)[i]);
2347 const struct got_error *
2348 got_privsep_send_gotconfig_parse_req(struct imsgbuf *ibuf, int fd)
2350 const struct got_error *err = NULL;
2352 if (imsg_compose(ibuf, GOT_IMSG_GOTCONFIG_PARSE_REQUEST, 0, 0, fd,
2354 err = got_error_from_errno("imsg_compose "
2355 "GOTCONFIG_PARSE_REQUEST");
2360 return flush_imsg(ibuf);
2363 const struct got_error *
2364 got_privsep_send_gotconfig_author_req(struct imsgbuf *ibuf)
2366 if (imsg_compose(ibuf,
2367 GOT_IMSG_GOTCONFIG_AUTHOR_REQUEST, 0, 0, -1, NULL, 0) == -1)
2368 return got_error_from_errno("imsg_compose "
2369 "GOTCONFIG_AUTHOR_REQUEST");
2371 return flush_imsg(ibuf);
2374 const struct got_error *
2375 got_privsep_send_gotconfig_allowed_signers_req(struct imsgbuf *ibuf)
2377 if (imsg_compose(ibuf,
2378 GOT_IMSG_GOTCONFIG_ALLOWEDSIGNERS_REQUEST, 0, 0, -1, NULL, 0) == -1)
2379 return got_error_from_errno("imsg_compose "
2380 "GOTCONFIG_ALLOWEDSIGNERS_REQUEST");
2382 return flush_imsg(ibuf);
2385 const struct got_error *
2386 got_privsep_send_gotconfig_revoked_signers_req(struct imsgbuf *ibuf)
2388 if (imsg_compose(ibuf,
2389 GOT_IMSG_GOTCONFIG_REVOKEDSIGNERS_REQUEST, 0, 0, -1, NULL, 0) == -1)
2390 return got_error_from_errno("imsg_compose "
2391 "GOTCONFIG_REVOKEDSIGNERS_REQUEST");
2393 return flush_imsg(ibuf);
2396 const struct got_error *
2397 got_privsep_send_gotconfig_signer_id_req(struct imsgbuf *ibuf)
2399 if (imsg_compose(ibuf,
2400 GOT_IMSG_GOTCONFIG_SIGNERID_REQUEST, 0, 0, -1, NULL, 0) == -1)
2401 return got_error_from_errno("imsg_compose "
2402 "GOTCONFIG_SIGNERID_REQUEST");
2404 return flush_imsg(ibuf);
2407 const struct got_error *
2408 got_privsep_send_gotconfig_remotes_req(struct imsgbuf *ibuf)
2410 if (imsg_compose(ibuf,
2411 GOT_IMSG_GOTCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
2412 return got_error_from_errno("imsg_compose "
2413 "GOTCONFIG_REMOTE_REQUEST");
2415 return flush_imsg(ibuf);
2418 const struct got_error *
2419 got_privsep_recv_gotconfig_str(char **str, struct imsgbuf *ibuf)
2421 const struct got_error *err = NULL;
2427 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2430 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2432 switch (imsg.hdr.type) {
2433 case GOT_IMSG_GOTCONFIG_STR_VAL:
2436 *str = strndup(imsg.data, datalen);
2438 err = got_error_from_errno("strndup");
2443 err = got_error(GOT_ERR_PRIVSEP_MSG);
2451 const struct got_error *
2452 got_privsep_recv_gotconfig_remotes(struct got_remote_repo **remotes,
2453 int *nremotes, struct imsgbuf *ibuf)
2455 const struct got_error *err = NULL;
2458 struct got_imsg_remotes iremotes;
2459 struct got_imsg_remote iremote;
2460 const size_t min_datalen =
2461 MIN(sizeof(struct got_imsg_error), sizeof(iremotes));
2465 iremotes.nremotes = 0;
2467 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2470 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2472 switch (imsg.hdr.type) {
2473 case GOT_IMSG_GOTCONFIG_REMOTES:
2474 if (datalen != sizeof(iremotes)) {
2475 err = got_error(GOT_ERR_PRIVSEP_LEN);
2478 memcpy(&iremotes, imsg.data, sizeof(iremotes));
2479 if (iremotes.nremotes < 0) {
2480 err = got_error(GOT_ERR_PRIVSEP_LEN);
2483 if (iremotes.nremotes == 0) {
2490 return got_error(GOT_ERR_PRIVSEP_MSG);
2495 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(**remotes));
2496 if (*remotes == NULL)
2497 return got_error_from_errno("recallocarray");
2499 while (*nremotes < iremotes.nremotes) {
2500 struct got_remote_repo *remote;
2501 const size_t min_datalen =
2502 MIN(sizeof(struct got_imsg_error), sizeof(iremote));
2505 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2508 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2510 switch (imsg.hdr.type) {
2511 case GOT_IMSG_GOTCONFIG_REMOTE:
2512 remote = &(*remotes)[*nremotes];
2513 memset(remote, 0, sizeof(*remote));
2514 if (datalen < sizeof(iremote)) {
2515 err = got_error(GOT_ERR_PRIVSEP_LEN);
2518 memcpy(&iremote, imsg.data, sizeof(iremote));
2519 if (iremote.name_len == 0 ||
2520 (iremote.fetch_url_len == 0 &&
2521 iremote.send_url_len == 0) ||
2522 (sizeof(iremote) + iremote.name_len +
2523 iremote.fetch_url_len + iremote.send_url_len) >
2525 err = got_error(GOT_ERR_PRIVSEP_LEN);
2528 remote->name = strndup(imsg.data + sizeof(iremote),
2530 if (remote->name == NULL) {
2531 err = got_error_from_errno("strndup");
2534 remote->fetch_url = strndup(imsg.data +
2535 sizeof(iremote) + iremote.name_len,
2536 iremote.fetch_url_len);
2537 if (remote->fetch_url == NULL) {
2538 err = got_error_from_errno("strndup");
2539 free_remote_data(remote);
2542 remote->send_url = strndup(imsg.data +
2543 sizeof(iremote) + iremote.name_len +
2544 iremote.fetch_url_len, iremote.send_url_len);
2545 if (remote->send_url == NULL) {
2546 err = got_error_from_errno("strndup");
2547 free_remote_data(remote);
2550 remote->mirror_references = iremote.mirror_references;
2551 remote->fetch_all_branches = iremote.fetch_all_branches;
2552 if (iremote.nfetch_branches > 0) {
2553 remote->fetch_branches = recallocarray(NULL, 0,
2554 iremote.nfetch_branches, sizeof(char *));
2555 if (remote->fetch_branches == NULL) {
2556 err = got_error_from_errno("calloc");
2557 free_remote_data(remote);
2561 remote->nfetch_branches = 0;
2562 for (i = 0; i < iremote.nfetch_branches; i++) {
2564 err = got_privsep_recv_gotconfig_str(&branch,
2567 free_remote_data(remote);
2570 remote->fetch_branches[i] = branch;
2571 remote->nfetch_branches++;
2573 if (iremote.nsend_branches > 0) {
2574 remote->send_branches = recallocarray(NULL, 0,
2575 iremote.nsend_branches, sizeof(char *));
2576 if (remote->send_branches == NULL) {
2577 err = got_error_from_errno("calloc");
2578 free_remote_data(remote);
2582 remote->nsend_branches = 0;
2583 for (i = 0; i < iremote.nsend_branches; i++) {
2585 err = got_privsep_recv_gotconfig_str(&branch,
2588 free_remote_data(remote);
2591 remote->send_branches[i] = branch;
2592 remote->nsend_branches++;
2594 if (iremote.nfetch_refs > 0) {
2595 remote->fetch_refs = recallocarray(NULL, 0,
2596 iremote.nfetch_refs, sizeof(char *));
2597 if (remote->fetch_refs == NULL) {
2598 err = got_error_from_errno("calloc");
2599 free_remote_data(remote);
2603 remote->nfetch_refs = 0;
2604 for (i = 0; i < iremote.nfetch_refs; i++) {
2606 err = got_privsep_recv_gotconfig_str(&ref,
2609 free_remote_data(remote);
2612 remote->fetch_refs[i] = ref;
2613 remote->nfetch_refs++;
2618 err = got_error(GOT_ERR_PRIVSEP_MSG);
2629 for (i = 0; i < *nremotes; i++)
2630 free_remote_data(&(*remotes)[i]);
2638 const struct got_error *
2639 got_privsep_send_commit_traversal_request(struct imsgbuf *ibuf,
2640 struct got_object_id *id, int idx, const char *path)
2643 size_t path_len = strlen(path) + 1;
2645 wbuf = imsg_create(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_REQUEST, 0, 0,
2646 sizeof(struct got_imsg_commit_traversal_request) + path_len);
2648 return got_error_from_errno(
2649 "imsg_create COMMIT_TRAVERSAL_REQUEST");
2650 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1)
2651 return got_error_from_errno("imsg_add "
2652 "COMMIT_TRAVERSAL_REQUEST");
2653 if (imsg_add(wbuf, &idx, sizeof(idx)) == -1)
2654 return got_error_from_errno("imsg_add "
2655 "COMMIT_TRAVERSAL_REQUEST");
2656 if (imsg_add(wbuf, path, path_len) == -1)
2657 return got_error_from_errno("imsg_add "
2658 "COMMIT_TRAVERSAL_REQUEST");
2661 imsg_close(ibuf, wbuf);
2663 return flush_imsg(ibuf);
2666 const struct got_error *
2667 got_privsep_recv_traversed_commits(struct got_commit_object **changed_commit,
2668 struct got_object_id **changed_commit_id,
2669 struct got_object_id_queue *commit_ids, struct imsgbuf *ibuf)
2671 const struct got_error *err = NULL;
2673 struct got_imsg_traversed_commits *icommits;
2677 *changed_commit = NULL;
2678 *changed_commit_id = NULL;
2681 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2685 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2686 switch (imsg.hdr.type) {
2687 case GOT_IMSG_TRAVERSED_COMMITS:
2688 icommits = imsg.data;
2689 if (datalen != sizeof(*icommits) +
2690 icommits->ncommits * SHA1_DIGEST_LENGTH) {
2691 err = got_error(GOT_ERR_PRIVSEP_LEN);
2694 for (i = 0; i < icommits->ncommits; i++) {
2695 struct got_object_qid *qid;
2696 uint8_t *sha1 = (uint8_t *)imsg.data +
2697 sizeof(*icommits) + i * SHA1_DIGEST_LENGTH;
2698 err = got_object_qid_alloc_partial(&qid);
2701 memcpy(qid->id.sha1, sha1, SHA1_DIGEST_LENGTH);
2702 STAILQ_INSERT_TAIL(commit_ids, qid, entry);
2704 /* The last commit may contain a change. */
2705 if (i == icommits->ncommits - 1) {
2706 *changed_commit_id =
2707 got_object_id_dup(&qid->id);
2708 if (*changed_commit_id == NULL) {
2709 err = got_error_from_errno(
2710 "got_object_id_dup");
2716 case GOT_IMSG_COMMIT:
2717 if (*changed_commit_id == NULL) {
2718 err = got_error(GOT_ERR_PRIVSEP_MSG);
2721 err = get_commit_from_imsg(changed_commit, &imsg,
2724 case GOT_IMSG_COMMIT_TRAVERSAL_DONE:
2728 err = got_error(GOT_ERR_PRIVSEP_MSG);
2738 got_object_id_queue_free(commit_ids);
2742 const struct got_error *
2743 got_privsep_send_enumerated_tree(size_t *totlen, struct imsgbuf *ibuf,
2744 struct got_object_id *tree_id, const char *path,
2745 struct got_parsed_tree_entry *entries, int nentries)
2747 const struct got_error *err = NULL;
2749 size_t path_len = strlen(path);
2752 msglen = sizeof(struct got_imsg_enumerated_tree) + path_len;
2753 wbuf = imsg_create(ibuf, GOT_IMSG_ENUMERATED_TREE, 0, 0, msglen);
2755 return got_error_from_errno("imsg_create ENUMERATED_TREE");
2757 if (imsg_add(wbuf, tree_id->sha1, SHA1_DIGEST_LENGTH) == -1)
2758 return got_error_from_errno("imsg_add ENUMERATED_TREE");
2759 if (imsg_add(wbuf, &nentries, sizeof(nentries)) == -1)
2760 return got_error_from_errno("imsg_add ENUMERATED_TREE");
2761 if (imsg_add(wbuf, path, path_len) == -1)
2762 return got_error_from_errno("imsg_add ENUMERATED_TREE");
2765 imsg_close(ibuf, wbuf);
2768 err = send_tree_entries(ibuf, entries, nentries);
2773 return flush_imsg(ibuf);
2776 const struct got_error *
2777 got_privsep_send_object_enumeration_request(struct imsgbuf *ibuf)
2779 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_ENUMERATION_REQUEST,
2780 0, 0, -1, NULL, 0) == -1)
2781 return got_error_from_errno("imsg_compose "
2782 "OBJECT_ENUMERATION_REQUEST");
2784 return flush_imsg(ibuf);
2787 const struct got_error *
2788 got_privsep_send_object_enumeration_done(struct imsgbuf *ibuf)
2790 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_ENUMERATION_DONE,
2791 0, 0, -1, NULL, 0) == -1)
2792 return got_error_from_errno("imsg_compose "
2793 "OBJECT_ENUMERATION_DONE");
2795 return flush_imsg(ibuf);
2798 const struct got_error *
2799 got_privsep_send_object_enumeration_incomplete(struct imsgbuf *ibuf)
2801 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_ENUMERATION_INCOMPLETE,
2802 0, 0, -1, NULL, 0) == -1)
2803 return got_error_from_errno("imsg_compose "
2804 "OBJECT_ENUMERATION_INCOMPLETE");
2806 return flush_imsg(ibuf);
2809 const struct got_error *
2810 got_privsep_send_enumerated_commit(struct imsgbuf *ibuf,
2811 struct got_object_id *id, time_t mtime)
2815 wbuf = imsg_create(ibuf, GOT_IMSG_ENUMERATED_COMMIT, 0, 0,
2816 sizeof(struct got_imsg_enumerated_commit) + SHA1_DIGEST_LENGTH);
2818 return got_error_from_errno("imsg_create ENUMERATED_COMMIT");
2820 /* Keep in sync with struct got_imsg_enumerated_commit! */
2821 if (imsg_add(wbuf, id, SHA1_DIGEST_LENGTH) == -1)
2822 return got_error_from_errno("imsg_add ENUMERATED_COMMIT");
2823 if (imsg_add(wbuf, &mtime, sizeof(mtime)) == -1)
2824 return got_error_from_errno("imsg_add ENUMERATED_COMMIT");
2827 imsg_close(ibuf, wbuf);
2828 /* Don't flush yet, tree entries or ENUMERATION_DONE will follow. */
2832 const struct got_error *
2833 got_privsep_recv_enumerated_objects(int *found_all_objects,
2834 struct imsgbuf *ibuf,
2835 got_object_enumerate_commit_cb cb_commit,
2836 got_object_enumerate_tree_cb cb_tree, void *cb_arg,
2837 struct got_repository *repo)
2839 const struct got_error *err = NULL;
2841 struct got_imsg_enumerated_commit *icommit = NULL;
2842 struct got_object_id commit_id;
2843 int have_commit = 0;
2845 struct got_tree_object tree;
2846 struct got_imsg_enumerated_tree *itree;
2847 struct got_object_id tree_id;
2848 char *path = NULL, *canon_path = NULL;
2849 size_t datalen, path_len;
2853 *found_all_objects = 0;
2854 memset(&tree, 0, sizeof(tree));
2857 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2861 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2862 switch (imsg.hdr.type) {
2863 case GOT_IMSG_ENUMERATED_COMMIT:
2864 if (have_commit && nentries != -1) {
2865 err = got_error(GOT_ERR_PRIVSEP_MSG);
2868 if (datalen != sizeof(*icommit)) {
2869 err = got_error(GOT_ERR_PRIVSEP_LEN);
2872 icommit = (struct got_imsg_enumerated_commit *)imsg.data;
2873 memcpy(commit_id.sha1, icommit->id, SHA1_DIGEST_LENGTH);
2874 mtime = icommit->mtime;
2877 case GOT_IMSG_ENUMERATED_TREE:
2878 /* Should be preceeded by GOT_IMSG_ENUMERATED_COMMIT. */
2880 err = got_error(GOT_ERR_PRIVSEP_MSG);
2883 if (datalen < sizeof(*itree)) {
2884 err = got_error(GOT_ERR_PRIVSEP_LEN);
2888 path_len = datalen - sizeof(*itree);
2889 if (path_len == 0) {
2890 err = got_error(GOT_ERR_PRIVSEP_LEN);
2893 memcpy(tree_id.sha1, itree->id, sizeof(tree_id.sha1));
2895 path = strndup(imsg.data + sizeof(*itree), path_len);
2897 err = got_error_from_errno("strndup");
2901 canon_path = malloc(path_len + 1);
2902 if (canon_path == NULL) {
2903 err = got_error_from_errno("malloc");
2906 if (!got_path_is_absolute(path)) {
2907 err = got_error(GOT_ERR_BAD_PATH);
2910 if (got_path_is_root_dir(path)) {
2911 /* XXX check what got_canonpath() does wrong */
2912 canon_path[0] = '/';
2913 canon_path[1] = '\0';
2915 err = got_canonpath(path, canon_path,
2920 if (strcmp(path, canon_path) != 0) {
2921 err = got_error(GOT_ERR_BAD_PATH);
2924 if (nentries != -1) {
2925 err = got_error(GOT_ERR_PRIVSEP_MSG);
2928 if (itree->nentries < -1) {
2929 err = got_error(GOT_ERR_PRIVSEP_MSG);
2932 if (itree->nentries == -1) {
2933 /* Tree was not found in pack file. */
2934 err = cb_tree(cb_arg, NULL, mtime, &tree_id,
2938 if (itree->nentries > INT_MAX) {
2939 err = got_error(GOT_ERR_PRIVSEP_LEN);
2942 tree.entries = calloc(itree->nentries,
2943 sizeof(struct got_tree_entry));
2944 if (tree.entries == NULL) {
2945 err = got_error_from_errno("calloc");
2948 if (itree->nentries == 0) {
2949 err = cb_tree(cb_arg, &tree, mtime, &tree_id,
2954 /* Prepare for next tree. */
2956 memset(&tree, 0, sizeof(tree));
2959 tree.nentries = itree->nentries;
2963 case GOT_IMSG_TREE_ENTRIES:
2964 /* Should be preceeded by GOT_IMSG_ENUMERATED_TREE. */
2965 if (nentries <= -1) {
2966 err = got_error(GOT_ERR_PRIVSEP_MSG);
2969 err = recv_tree_entries(imsg.data, datalen,
2973 if (tree.nentries == nentries) {
2974 err = cb_tree(cb_arg, &tree, mtime, &tree_id,
2979 /* Prepare for next tree. */
2981 memset(&tree, 0, sizeof(tree));
2985 case GOT_IMSG_TREE_ENUMERATION_DONE:
2986 /* All trees have been found and traversed. */
2987 if (!have_commit || path == NULL || nentries != -1) {
2988 err = got_error(GOT_ERR_PRIVSEP_MSG);
2991 err = cb_commit(cb_arg, mtime, &commit_id, repo);
2996 case GOT_IMSG_OBJECT_ENUMERATION_DONE:
2997 *found_all_objects = 1;
3000 case GOT_IMSG_OBJECT_ENUMERATION_INCOMPLETE:
3004 err = got_error(GOT_ERR_PRIVSEP_MSG);
3019 const struct got_error *
3020 got_privsep_send_raw_delta_req(struct imsgbuf *ibuf, int idx,
3021 struct got_object_id *id)
3023 struct got_imsg_raw_delta_request dreq;
3025 memset(&dreq, 0, sizeof(dreq));
3027 memcpy(&dreq.id, id, sizeof(dreq.id));
3029 if (imsg_compose(ibuf, GOT_IMSG_RAW_DELTA_REQUEST, 0, 0, -1,
3030 &dreq, sizeof(dreq)) == -1)
3031 return got_error_from_errno("imsg_compose RAW_DELTA_REQUEST");
3033 return flush_imsg(ibuf);
3036 const struct got_error *
3037 got_privsep_send_raw_delta_outfd(struct imsgbuf *ibuf, int fd)
3039 return send_fd(ibuf, GOT_IMSG_RAW_DELTA_OUTFD, fd);
3042 const struct got_error *
3043 got_privsep_send_raw_delta(struct imsgbuf *ibuf, uint64_t base_size,
3044 uint64_t result_size, off_t delta_size, off_t delta_compressed_size,
3045 off_t delta_offset, off_t delta_out_offset, struct got_object_id *base_id)
3047 struct got_imsg_raw_delta idelta;
3050 memset(&idelta, 0, sizeof(idelta));
3051 idelta.base_size = base_size;
3052 idelta.result_size = result_size;
3053 idelta.delta_size = delta_size;
3054 idelta.delta_compressed_size = delta_compressed_size;
3055 idelta.delta_offset = delta_offset;
3056 idelta.delta_out_offset = delta_out_offset;
3057 memcpy(&idelta.base_id, &base_id, sizeof(idelta.base_id));
3059 ret = imsg_compose(ibuf, GOT_IMSG_RAW_DELTA, 0, 0, -1,
3060 &idelta, sizeof(idelta));
3062 return got_error_from_errno("imsg_compose RAW_DELTA");
3064 return flush_imsg(ibuf);
3067 const struct got_error *
3068 got_privsep_recv_raw_delta(uint64_t *base_size, uint64_t *result_size,
3069 off_t *delta_size, off_t *delta_compressed_size, off_t *delta_offset,
3070 off_t *delta_out_offset, struct got_object_id **base_id,
3071 struct imsgbuf *ibuf)
3073 const struct got_error *err = NULL;
3075 struct got_imsg_raw_delta *delta;
3081 *delta_compressed_size = 0;
3083 *delta_out_offset = 0;
3086 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
3090 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
3092 switch (imsg.hdr.type) {
3093 case GOT_IMSG_RAW_DELTA:
3094 if (datalen != sizeof(*delta)) {
3095 err = got_error(GOT_ERR_PRIVSEP_LEN);
3099 *base_size = delta->base_size;
3100 *result_size = delta->result_size;
3101 *delta_size = delta->delta_size;
3102 *delta_compressed_size = delta->delta_compressed_size;
3103 *delta_offset = delta->delta_offset;
3104 *delta_out_offset = delta->delta_out_offset;
3105 *base_id = calloc(1, sizeof(**base_id));
3106 if (*base_id == NULL) {
3107 err = got_error_from_errno("malloc");
3110 memcpy(*base_id, &delta->base_id, sizeof(**base_id));
3113 err = got_error(GOT_ERR_PRIVSEP_MSG);
3126 static const struct got_error *
3127 send_idlist(struct imsgbuf *ibuf, struct got_object_id **ids, size_t nids)
3129 const struct got_error *err = NULL;
3130 struct got_imsg_object_idlist idlist;
3134 memset(&idlist, 0, sizeof(idlist));
3136 if (nids > GOT_IMSG_OBJ_ID_LIST_MAX_NIDS)
3137 return got_error(GOT_ERR_NO_SPACE);
3139 wbuf = imsg_create(ibuf, GOT_IMSG_OBJ_ID_LIST, 0, 0,
3140 sizeof(idlist) + nids * sizeof(**ids));
3142 err = got_error_from_errno("imsg_create OBJ_ID_LIST");
3147 if (imsg_add(wbuf, &idlist, sizeof(idlist)) == -1)
3148 return got_error_from_errno("imsg_add OBJ_ID_LIST");
3150 for (i = 0; i < nids; i++) {
3151 struct got_object_id *id = ids[i];
3152 if (imsg_add(wbuf, id, sizeof(*id)) == -1)
3153 return got_error_from_errno("imsg_add OBJ_ID_LIST");
3157 imsg_close(ibuf, wbuf);
3159 return flush_imsg(ibuf);
3162 const struct got_error *
3163 got_privsep_send_object_idlist(struct imsgbuf *ibuf,
3164 struct got_object_id **ids, size_t nids)
3166 const struct got_error *err = NULL;
3167 struct got_object_id *idlist[GOT_IMSG_OBJ_ID_LIST_MAX_NIDS];
3170 for (i = 0; i < nids; i++) {
3171 idlist[i % nitems(idlist)] = ids[i];
3173 if (queued >= nitems(idlist)) {
3174 err = send_idlist(ibuf, idlist, queued);
3182 err = send_idlist(ibuf, idlist, queued);
3190 const struct got_error *
3191 got_privsep_send_object_idlist_done(struct imsgbuf *ibuf)
3193 if (imsg_compose(ibuf, GOT_IMSG_OBJ_ID_LIST_DONE, 0, 0, -1, NULL, 0)
3195 return got_error_from_errno("imsg_compose OBJ_ID_LIST_DONE");
3197 return flush_imsg(ibuf);
3200 const struct got_error *
3201 got_privsep_recv_object_idlist(int *done, struct got_object_id **ids,
3202 size_t *nids, struct imsgbuf *ibuf)
3204 const struct got_error *err = NULL;
3206 struct got_imsg_object_idlist *idlist;
3213 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
3217 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
3218 switch (imsg.hdr.type) {
3219 case GOT_IMSG_OBJ_ID_LIST:
3220 if (datalen < sizeof(*idlist)) {
3221 err = got_error(GOT_ERR_PRIVSEP_LEN);
3225 if (idlist->nids > GOT_IMSG_OBJ_ID_LIST_MAX_NIDS ||
3226 idlist->nids * sizeof(**ids) > datalen - sizeof(*idlist)) {
3227 err = got_error(GOT_ERR_PRIVSEP_LEN);
3230 *nids = idlist->nids;
3231 *ids = calloc(*nids, sizeof(**ids));
3233 err = got_error_from_errno("calloc");
3236 memcpy(*ids, (uint8_t *)imsg.data + sizeof(*idlist),
3237 *nids * sizeof(**ids));
3239 case GOT_IMSG_OBJ_ID_LIST_DONE:
3243 err = got_error(GOT_ERR_PRIVSEP_MSG);
3252 const struct got_error *
3253 got_privsep_send_delta_reuse_req(struct imsgbuf *ibuf)
3255 if (imsg_compose(ibuf, GOT_IMSG_DELTA_REUSE_REQUEST, 0, 0, -1, NULL, 0)
3257 return got_error_from_errno("imsg_compose DELTA_REUSE_REQUEST");
3259 return flush_imsg(ibuf);
3262 const struct got_error *
3263 got_privsep_send_reused_deltas(struct imsgbuf *ibuf,
3264 struct got_imsg_reused_delta *deltas, size_t ndeltas)
3266 const struct got_error *err = NULL;
3268 struct got_imsg_reused_deltas ideltas;
3271 memset(&ideltas, 0, sizeof(ideltas));
3273 if (ndeltas > GOT_IMSG_REUSED_DELTAS_MAX_NDELTAS)
3274 return got_error(GOT_ERR_NO_SPACE);
3276 wbuf = imsg_create(ibuf, GOT_IMSG_REUSED_DELTAS, 0, 0,
3277 sizeof(ideltas) + ndeltas * sizeof(*deltas));
3279 err = got_error_from_errno("imsg_create REUSED_DELTAS");
3283 ideltas.ndeltas = ndeltas;
3284 if (imsg_add(wbuf, &ideltas, sizeof(ideltas)) == -1)
3285 return got_error_from_errno("imsg_add REUSED_DELTAS");
3287 for (i = 0; i < ndeltas; i++) {
3288 struct got_imsg_reused_delta *delta = &deltas[i];
3289 if (imsg_add(wbuf, delta, sizeof(*delta)) == -1)
3290 return got_error_from_errno("imsg_add REUSED_DELTAS");
3294 imsg_close(ibuf, wbuf);
3296 return flush_imsg(ibuf);
3299 const struct got_error *
3300 got_privsep_send_reused_deltas_done(struct imsgbuf *ibuf)
3302 if (imsg_compose(ibuf, GOT_IMSG_DELTA_REUSE_DONE, 0, 0, -1, NULL, 0)
3304 return got_error_from_errno("imsg_compose DELTA_REUSE_DONE");
3306 return flush_imsg(ibuf);
3309 const struct got_error *
3310 got_privsep_recv_reused_deltas(int *done, struct got_imsg_reused_delta *deltas,
3311 size_t *ndeltas, struct imsgbuf *ibuf)
3313 const struct got_error *err = NULL;
3315 struct got_imsg_reused_deltas *ideltas;
3321 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
3325 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
3326 switch (imsg.hdr.type) {
3327 case GOT_IMSG_REUSED_DELTAS:
3328 if (datalen < sizeof(*ideltas)) {
3329 err = got_error(GOT_ERR_PRIVSEP_LEN);
3332 ideltas = imsg.data;
3333 if (ideltas->ndeltas > GOT_IMSG_OBJ_ID_LIST_MAX_NIDS ||
3334 ideltas->ndeltas * sizeof(*deltas) >
3335 datalen - sizeof(*ideltas)) {
3336 err = got_error(GOT_ERR_PRIVSEP_LEN);
3339 *ndeltas = ideltas->ndeltas;
3340 memcpy(deltas, (uint8_t *)imsg.data + sizeof(*ideltas),
3341 *ndeltas * sizeof(*deltas));
3343 case GOT_IMSG_DELTA_REUSE_DONE:
3347 err = got_error(GOT_ERR_PRIVSEP_MSG);
3356 const struct got_error *
3357 got_privsep_init_commit_painting(struct imsgbuf *ibuf)
3359 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_PAINTING_INIT,
3362 return got_error_from_errno("imsg_compose "
3363 "COMMIT_PAINTING_INIT");
3365 return flush_imsg(ibuf);
3368 const struct got_error *
3369 got_privsep_send_painting_request(struct imsgbuf *ibuf, int idx,
3370 struct got_object_id *id, intptr_t color)
3372 struct got_imsg_commit_painting_request ireq;
3374 memset(&ireq, 0, sizeof(ireq));
3375 memcpy(ireq.id, id->sha1, sizeof(ireq.id));
3379 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_PAINTING_REQUEST, 0, 0, -1,
3380 &ireq, sizeof(ireq)) == -1)
3381 return got_error_from_errno("imsg_compose "
3382 "COMMIT_PAINTING_REQUEST");
3384 return flush_imsg(ibuf);
3387 static const struct got_error *
3388 send_painted_commits(struct got_object_id_queue *ids, int *nids,
3389 size_t remain, int present_in_pack, struct imsgbuf *ibuf)
3391 const struct got_error *err = NULL;
3392 struct ibuf *wbuf = NULL;
3393 struct got_object_qid *qid;
3398 msglen = MIN(remain, MAX_IMSGSIZE - IMSG_HEADER_SIZE);
3399 ncommits = (msglen - sizeof(struct got_imsg_painted_commits)) /
3400 sizeof(struct got_imsg_painted_commit);
3402 wbuf = imsg_create(ibuf, GOT_IMSG_PAINTED_COMMITS, 0, 0, msglen);
3404 err = got_error_from_errno("imsg_create PAINTED_COMMITS");
3408 /* Keep in sync with struct got_imsg_painted_commits! */
3409 if (imsg_add(wbuf, &ncommits, sizeof(ncommits)) == -1)
3410 return got_error_from_errno("imsg_add PAINTED_COMMITS");
3411 if (imsg_add(wbuf, &present_in_pack, sizeof(present_in_pack)) == -1)
3412 return got_error_from_errno("imsg_add PAINTED_COMMITS");
3414 while (ncommits > 0) {
3415 qid = STAILQ_FIRST(ids);
3416 STAILQ_REMOVE_HEAD(ids, entry);
3419 color = (intptr_t)qid->data;
3421 /* Keep in sync with struct got_imsg_painted_commit! */
3422 if (imsg_add(wbuf, qid->id.sha1, SHA1_DIGEST_LENGTH) == -1)
3423 return got_error_from_errno("imsg_add PAINTED_COMMITS");
3424 if (imsg_add(wbuf, &color, sizeof(color)) == -1)
3425 return got_error_from_errno("imsg_add PAINTED_COMMITS");
3427 got_object_qid_free(qid);
3431 imsg_close(ibuf, wbuf);
3433 return flush_imsg(ibuf);
3436 const struct got_error *
3437 got_privsep_send_painted_commits(struct imsgbuf *ibuf,
3438 struct got_object_id_queue *ids, int *nids,
3439 int present_in_pack, int flush)
3441 const struct got_error *err;
3448 remain = (sizeof(struct got_imsg_painted_commits)) +
3449 *nids * sizeof(struct got_imsg_painted_commit);
3450 if (flush || remain >= MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
3451 err = send_painted_commits(ids, nids, remain,
3452 present_in_pack, ibuf);
3456 } while (flush && *nids > 0);
3461 const struct got_error *
3462 got_privsep_send_painting_commits_done(struct imsgbuf *ibuf)
3464 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_PAINTING_DONE,
3467 return got_error_from_errno("imsg_compose "
3468 "COMMIT_PAINTING_DONE");
3470 return flush_imsg(ibuf);
3473 const struct got_error *
3474 got_privsep_recv_painted_commits(struct got_object_id_queue *new_ids,
3475 got_privsep_recv_painted_commit_cb cb, void *cb_arg, struct imsgbuf *ibuf)
3477 const struct got_error *err = NULL;
3479 struct got_imsg_painted_commits icommits;
3480 struct got_imsg_painted_commit icommit;
3485 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
3489 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
3490 if (imsg.hdr.type == GOT_IMSG_COMMIT_PAINTING_DONE)
3492 if (imsg.hdr.type != GOT_IMSG_PAINTED_COMMITS)
3493 return got_error(GOT_ERR_PRIVSEP_MSG);
3495 if (datalen < sizeof(icommits))
3496 return got_error(GOT_ERR_PRIVSEP_LEN);
3497 memcpy(&icommits, imsg.data, sizeof(icommits));
3498 if (icommits.ncommits * sizeof(icommit) < icommits.ncommits ||
3499 datalen < sizeof(icommits) +
3500 icommits.ncommits * sizeof(icommit))
3501 return got_error(GOT_ERR_PRIVSEP_LEN);
3503 for (i = 0; i < icommits.ncommits; i++) {
3505 (uint8_t *)imsg.data + sizeof(icommits) + i * sizeof(icommit),
3508 if (icommits.present_in_pack) {
3509 struct got_object_id id;
3510 memcpy(id.sha1, icommit.id, SHA1_DIGEST_LENGTH);
3511 err = cb(cb_arg, &id, icommit.color);
3515 struct got_object_qid *qid;
3516 err = got_object_qid_alloc_partial(&qid);
3519 memcpy(qid->id.sha1, icommit.id,
3520 SHA1_DIGEST_LENGTH);
3521 qid->data = (void *)icommit.color;
3522 STAILQ_INSERT_TAIL(new_ids, qid, entry);
3532 const struct got_error *
3533 got_privsep_unveil_exec_helpers(void)
3535 const char *helpers[] = {
3536 GOT_PATH_PROG_READ_PACK,
3537 GOT_PATH_PROG_READ_OBJECT,
3538 GOT_PATH_PROG_READ_COMMIT,
3539 GOT_PATH_PROG_READ_TREE,
3540 GOT_PATH_PROG_READ_BLOB,
3541 GOT_PATH_PROG_READ_TAG,
3542 GOT_PATH_PROG_READ_GITCONFIG,
3543 GOT_PATH_PROG_READ_GOTCONFIG,
3544 GOT_PATH_PROG_READ_PATCH,
3545 GOT_PATH_PROG_FETCH_PACK,
3546 GOT_PATH_PROG_INDEX_PACK,
3547 GOT_PATH_PROG_SEND_PACK,
3551 for (i = 0; i < nitems(helpers); i++) {
3552 if (unveil(helpers[i], "x") == 0)
3554 return got_error_from_errno2("unveil", helpers[i]);
3561 got_privsep_exec_child(int imsg_fds[2], const char *path, const char *repo_path)
3563 if (close(imsg_fds[0]) == -1) {
3564 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
3568 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
3569 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
3573 closefrom(GOT_IMSG_FD_CHILD + 1);
3575 if (execl(path, path, repo_path, (char *)NULL) == -1) {
3576 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,