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>
21 #include <sys/syslimits.h>
35 #include "got_object.h"
36 #include "got_error.h"
38 #include "got_repository.h"
40 #include "got_lib_sha1.h"
41 #include "got_lib_delta.h"
42 #include "got_lib_inflate.h"
43 #include "got_lib_object.h"
44 #include "got_lib_object_parse.h"
45 #include "got_lib_privsep.h"
46 #include "got_lib_pack.h"
49 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
53 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
56 static const struct got_error *
57 poll_fd(int fd, int events, int timeout)
63 pfd[0].events = events;
65 n = poll(pfd, 1, timeout);
67 return got_error_from_errno("poll");
69 return got_error(GOT_ERR_TIMEOUT);
70 if (pfd[0].revents & (POLLERR | POLLNVAL))
71 return got_error_from_errno("poll error");
72 if (pfd[0].revents & (events | POLLHUP))
75 return got_error(GOT_ERR_INTERRUPT);
78 static const struct got_error *
79 read_imsg(struct imsgbuf *ibuf)
81 const struct got_error *err;
84 err = poll_fd(ibuf->fd, POLLIN, INFTIM);
90 if (errno == EAGAIN) /* Could be a file-descriptor leak. */
91 return got_error(GOT_ERR_PRIVSEP_NO_FD);
92 return got_error(GOT_ERR_PRIVSEP_READ);
95 return got_error(GOT_ERR_PRIVSEP_PIPE);
100 const struct got_error *
101 got_privsep_wait_for_child(pid_t pid)
105 if (waitpid(pid, &child_status, 0) == -1)
106 return got_error_from_errno("waitpid");
108 if (!WIFEXITED(child_status))
109 return got_error(GOT_ERR_PRIVSEP_DIED);
111 if (WEXITSTATUS(child_status) != 0)
112 return got_error(GOT_ERR_PRIVSEP_EXIT);
117 static const struct got_error *
118 recv_imsg_error(struct imsg *imsg, size_t datalen)
120 struct got_imsg_error *ierr;
122 if (datalen != sizeof(*ierr))
123 return got_error(GOT_ERR_PRIVSEP_LEN);
126 if (ierr->code == GOT_ERR_ERRNO) {
127 static struct got_error serr;
128 serr.code = GOT_ERR_ERRNO;
129 serr.msg = strerror(ierr->errno_code);
133 return got_error(ierr->code);
136 const struct got_error *
137 got_privsep_recv_imsg(struct imsg *imsg, struct imsgbuf *ibuf,
140 const struct got_error *err;
143 n = imsg_get(ibuf, imsg);
145 return got_error_from_errno("imsg_get");
148 err = read_imsg(ibuf);
151 n = imsg_get(ibuf, imsg);
153 return got_error_from_errno("imsg_get");
156 if (imsg->hdr.len < IMSG_HEADER_SIZE + min_datalen)
157 return got_error(GOT_ERR_PRIVSEP_LEN);
159 if (imsg->hdr.type == GOT_IMSG_ERROR) {
160 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
161 return recv_imsg_error(imsg, datalen);
167 /* Attempt to send an error in an imsg. Complain on stderr as a last resort. */
169 got_privsep_send_error(struct imsgbuf *ibuf, const struct got_error *err)
171 const struct got_error *poll_err;
172 struct got_imsg_error ierr;
175 ierr.code = err->code;
176 if (err->code == GOT_ERR_ERRNO)
177 ierr.errno_code = errno;
180 ret = imsg_compose(ibuf, GOT_IMSG_ERROR, 0, 0, -1, &ierr, sizeof(ierr));
182 fprintf(stderr, "%s: error %d \"%s\": imsg_compose: %s\n",
183 getprogname(), err->code, err->msg, strerror(errno));
187 poll_err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
189 fprintf(stderr, "%s: error %d \"%s\": poll: %s\n",
190 getprogname(), err->code, err->msg, poll_err->msg);
194 ret = imsg_flush(ibuf);
196 fprintf(stderr, "%s: error %d \"%s\": imsg_flush: %s\n",
197 getprogname(), err->code, err->msg, strerror(errno));
202 static const struct got_error *
203 flush_imsg(struct imsgbuf *ibuf)
205 const struct got_error *err;
207 err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
211 if (imsg_flush(ibuf) == -1)
212 return got_error_from_errno("imsg_flush");
217 const struct got_error *
218 got_privsep_send_stop(int fd)
220 const struct got_error *err = NULL;
223 imsg_init(&ibuf, fd);
225 if (imsg_compose(&ibuf, GOT_IMSG_STOP, 0, 0, -1, NULL, 0) == -1)
226 return got_error_from_errno("imsg_compose STOP");
228 err = flush_imsg(&ibuf);
233 const struct got_error *
234 got_privsep_send_ack(struct imsgbuf *ibuf)
236 if (imsg_compose(ibuf, GOT_IMSG_ACK, 0, 0, -1, NULL, 0) == -1)
237 return got_error_from_errno("imsg_compose ACK");
238 return flush_imsg(ibuf);
241 const struct got_error *
242 got_privsep_wait_ack(struct imsgbuf *ibuf)
244 const struct got_error *err = NULL;
247 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
250 if (imsg.hdr.type == GOT_IMSG_ACK && imsg.hdr.len - IMSG_HEADER_SIZE == 0)
253 return got_error(GOT_ERR_PRIVSEP_MSG);
258 const struct got_error *
259 got_privsep_send_obj_req(struct imsgbuf *ibuf, int fd)
261 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_REQUEST, 0, 0, fd, NULL, 0)
263 return got_error_from_errno("imsg_compose OBJECT_REQUEST");
265 return flush_imsg(ibuf);
268 const struct got_error *
269 got_privsep_send_commit_req(struct imsgbuf *ibuf, int fd,
270 struct got_object_id *id, int pack_idx)
272 const struct got_error *err = NULL;
273 struct got_imsg_packed_object iobj, *iobjp;
276 if (id) { /* commit is packed */
278 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
286 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_REQUEST, 0, 0, fd, iobjp, len)
288 err = got_error_from_errno("imsg_compose COMMIT_REQUEST");
293 return flush_imsg(ibuf);
296 const struct got_error *
297 got_privsep_send_tree_req(struct imsgbuf *ibuf, int fd,
298 struct got_object_id *id, int pack_idx)
300 const struct got_error *err = NULL;
302 size_t len = id ? sizeof(struct got_imsg_packed_object) : 0;
304 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_REQUEST, 0, 0, len);
306 return got_error_from_errno("imsg_create TREE_REQUEST");
308 if (id) { /* tree is packed */
309 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
310 err = got_error_from_errno("imsg_add TREE_ENTRY");
315 if (imsg_add(wbuf, &pack_idx, sizeof(pack_idx)) == -1) {
316 err = got_error_from_errno("imsg_add TREE_ENTRY");
323 imsg_close(ibuf, wbuf);
325 return flush_imsg(ibuf);
328 const struct got_error *
329 got_privsep_send_tag_req(struct imsgbuf *ibuf, int fd,
330 struct got_object_id *id, int pack_idx)
332 struct got_imsg_packed_object iobj, *iobjp;
335 if (id) { /* tag is packed */
337 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
345 if (imsg_compose(ibuf, GOT_IMSG_TAG_REQUEST, 0, 0, fd, iobjp, len)
347 return got_error_from_errno("imsg_compose TAG_REQUEST");
349 return flush_imsg(ibuf);
352 const struct got_error *
353 got_privsep_send_blob_req(struct imsgbuf *ibuf, int infd,
354 struct got_object_id *id, int pack_idx)
356 const struct got_error *err = NULL;
357 struct got_imsg_packed_object iobj, *iobjp;
360 if (id) { /* blob is packed */
362 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
370 if (imsg_compose(ibuf, GOT_IMSG_BLOB_REQUEST, 0, 0, infd, iobjp, len)
372 err = got_error_from_errno("imsg_compose BLOB_REQUEST");
377 return flush_imsg(ibuf);
380 const struct got_error *
381 got_privsep_send_blob_outfd(struct imsgbuf *ibuf, int outfd)
383 const struct got_error *err = NULL;
385 if (imsg_compose(ibuf, GOT_IMSG_BLOB_OUTFD, 0, 0, outfd, NULL, 0)
387 err = got_error_from_errno("imsg_compose BLOB_OUTFD");
392 return flush_imsg(ibuf);
395 const struct got_error *
396 got_privsep_send_tmpfd(struct imsgbuf *ibuf, int fd)
398 const struct got_error *err = NULL;
400 if (imsg_compose(ibuf, GOT_IMSG_TMPFD, 0, 0, fd, NULL, 0)
402 err = got_error_from_errno("imsg_compose TMPFD");
407 return flush_imsg(ibuf);
410 const struct got_error *
411 got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj)
413 struct got_imsg_object iobj;
415 memcpy(iobj.id, obj->id.sha1, sizeof(iobj.id));
416 iobj.type = obj->type;
417 iobj.flags = obj->flags;
418 iobj.hdrlen = obj->hdrlen;
419 iobj.size = obj->size;
420 if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
421 iobj.pack_offset = obj->pack_offset;
422 iobj.pack_idx = obj->pack_idx;
425 if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
427 return got_error_from_errno("imsg_compose OBJECT");
429 return flush_imsg(ibuf);
432 const struct got_error *
433 got_privsep_send_fetch_req(struct imsgbuf *ibuf, int fd)
435 const struct got_error *err = NULL;
437 if (imsg_compose(ibuf, GOT_IMSG_FETCH_REQUEST, 0, 0, fd,
439 err = got_error_from_errno("imsg_compose FETCH_REQUEST");
443 return flush_imsg(ibuf);
446 const struct got_error *
447 got_privsep_send_fetch_done(struct imsgbuf *ibuf, struct got_object_id hash)
449 if (imsg_compose(ibuf, GOT_IMSG_FETCH_DONE, 0, 0, -1,
450 hash.sha1, SHA1_DIGEST_LENGTH) == -1)
451 return got_error_from_errno("imsg_compose FETCH");
452 return flush_imsg(ibuf);
455 const struct got_error *
456 got_privsep_wait_fetch_done(struct imsgbuf *ibuf, struct got_object_id *hash)
458 const struct got_error *err = NULL;
461 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
464 if (imsg.hdr.type == GOT_IMSG_FETCH_DONE &&
465 imsg.hdr.len - sizeof(imsg.hdr) == SHA1_DIGEST_LENGTH)
468 return got_error(GOT_ERR_PRIVSEP_MSG);
473 const struct got_error *
474 got_privsep_send_index_pack_req(struct imsgbuf *ibuf, int fd, struct got_object_id hash)
476 const struct got_error *err = NULL;
478 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_REQUEST, 0, 0, fd,
479 hash.sha1, SHA1_DIGEST_LENGTH) == -1) {
480 err = got_error_from_errno("imsg_compose INDEX_REQUEST");
484 return flush_imsg(ibuf);
487 const struct got_error *
488 got_privsep_send_index_pack_done(struct imsgbuf *ibuf)
490 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_DONE, 0, 0, -1, NULL, 0) == -1)
491 return got_error_from_errno("imsg_compose FETCH");
492 return flush_imsg(ibuf);
495 const struct got_error *
496 got_privsep_wait_index_pack_done(struct imsgbuf *ibuf)
498 const struct got_error *err = NULL;
501 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
504 if (imsg.hdr.type == GOT_IMSG_IDXPACK_DONE)
507 return got_error(GOT_ERR_PRIVSEP_MSG);
511 const struct got_error *
512 got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
513 struct imsgbuf *ibuf)
515 const struct got_error *err = NULL;
516 struct got_imsg_object *iobj;
517 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
519 if (datalen != sizeof(*iobj))
520 return got_error(GOT_ERR_PRIVSEP_LEN);
523 *obj = calloc(1, sizeof(**obj));
525 return got_error_from_errno("calloc");
527 memcpy((*obj)->id.sha1, iobj->id, SHA1_DIGEST_LENGTH);
528 (*obj)->type = iobj->type;
529 (*obj)->flags = iobj->flags;
530 (*obj)->hdrlen = iobj->hdrlen;
531 (*obj)->size = iobj->size;
532 /* path_packfile is handled by caller */
533 if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
534 (*obj)->pack_offset = iobj->pack_offset;
535 (*obj)->pack_idx = iobj->pack_idx;
541 const struct got_error *
542 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
544 const struct got_error *err = NULL;
546 const size_t min_datalen =
547 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
551 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
555 switch (imsg.hdr.type) {
556 case GOT_IMSG_OBJECT:
557 err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
560 err = got_error(GOT_ERR_PRIVSEP_MSG);
569 static const struct got_error *
570 send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
573 const struct got_error *err = NULL;
574 size_t offset, remain;
579 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
581 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
582 commit->logmsg + offset, n) == -1) {
583 err = got_error_from_errno("imsg_compose "
588 err = flush_imsg(ibuf);
599 const struct got_error *
600 got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
602 const struct got_error *err = NULL;
603 struct got_imsg_commit_object *icommit;
606 struct got_object_qid *qid;
607 size_t author_len = strlen(commit->author);
608 size_t committer_len = strlen(commit->committer);
609 size_t logmsg_len = strlen(commit->logmsg);
611 total = sizeof(*icommit) + author_len + committer_len +
612 commit->nparents * SHA1_DIGEST_LENGTH;
616 return got_error_from_errno("malloc");
618 icommit = (struct got_imsg_commit_object *)buf;
619 memcpy(icommit->tree_id, commit->tree_id->sha1,
620 sizeof(icommit->tree_id));
621 icommit->author_len = author_len;
622 icommit->author_time = commit->author_time;
623 icommit->author_gmtoff = commit->author_gmtoff;
624 icommit->committer_len = committer_len;
625 icommit->committer_time = commit->committer_time;
626 icommit->committer_gmtoff = commit->committer_gmtoff;
627 icommit->logmsg_len = logmsg_len;
628 icommit->nparents = commit->nparents;
630 len = sizeof(*icommit);
631 memcpy(buf + len, commit->author, author_len);
633 memcpy(buf + len, commit->committer, committer_len);
634 len += committer_len;
635 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
636 memcpy(buf + len, qid->id, SHA1_DIGEST_LENGTH);
637 len += SHA1_DIGEST_LENGTH;
640 if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
641 err = got_error_from_errno("imsg_compose COMMIT");
645 if (logmsg_len == 0 ||
646 logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
647 err = flush_imsg(ibuf);
651 err = send_commit_logmsg(ibuf, commit, logmsg_len);
657 static const struct got_error *
658 get_commit_from_imsg(struct got_commit_object **commit,
659 struct imsg *imsg, size_t datalen, struct imsgbuf *ibuf)
661 const struct got_error *err = NULL;
662 struct got_imsg_commit_object *icommit;
666 if (datalen < sizeof(*icommit))
667 return got_error(GOT_ERR_PRIVSEP_LEN);
669 icommit = imsg->data;
670 if (datalen != sizeof(*icommit) + icommit->author_len +
671 icommit->committer_len +
672 icommit->nparents * SHA1_DIGEST_LENGTH)
673 return got_error(GOT_ERR_PRIVSEP_LEN);
675 if (icommit->nparents < 0)
676 return got_error(GOT_ERR_PRIVSEP_LEN);
678 len += sizeof(*icommit);
680 *commit = got_object_commit_alloc_partial();
682 return got_error_from_errno(
683 "got_object_commit_alloc_partial");
685 memcpy((*commit)->tree_id->sha1, icommit->tree_id,
687 (*commit)->author_time = icommit->author_time;
688 (*commit)->author_gmtoff = icommit->author_gmtoff;
689 (*commit)->committer_time = icommit->committer_time;
690 (*commit)->committer_gmtoff = icommit->committer_gmtoff;
692 if (icommit->author_len == 0) {
693 (*commit)->author = strdup("");
694 if ((*commit)->author == NULL) {
695 err = got_error_from_errno("strdup");
699 (*commit)->author = malloc(icommit->author_len + 1);
700 if ((*commit)->author == NULL) {
701 err = got_error_from_errno("malloc");
704 memcpy((*commit)->author, imsg->data + len,
705 icommit->author_len);
706 (*commit)->author[icommit->author_len] = '\0';
708 len += icommit->author_len;
710 if (icommit->committer_len == 0) {
711 (*commit)->committer = strdup("");
712 if ((*commit)->committer == NULL) {
713 err = got_error_from_errno("strdup");
717 (*commit)->committer =
718 malloc(icommit->committer_len + 1);
719 if ((*commit)->committer == NULL) {
720 err = got_error_from_errno("malloc");
723 memcpy((*commit)->committer, imsg->data + len,
724 icommit->committer_len);
725 (*commit)->committer[icommit->committer_len] = '\0';
727 len += icommit->committer_len;
729 if (icommit->logmsg_len == 0) {
730 (*commit)->logmsg = strdup("");
731 if ((*commit)->logmsg == NULL) {
732 err = got_error_from_errno("strdup");
736 size_t offset = 0, remain = icommit->logmsg_len;
738 (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
739 if ((*commit)->logmsg == NULL) {
740 err = got_error_from_errno("malloc");
744 struct imsg imsg_log;
745 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
748 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
752 if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG) {
753 err = got_error(GOT_ERR_PRIVSEP_MSG);
757 memcpy((*commit)->logmsg + offset,
759 imsg_free(&imsg_log);
763 (*commit)->logmsg[icommit->logmsg_len] = '\0';
766 for (i = 0; i < icommit->nparents; i++) {
767 struct got_object_qid *qid;
769 err = got_object_qid_alloc_partial(&qid);
772 memcpy(qid->id, imsg->data + len +
773 i * SHA1_DIGEST_LENGTH, sizeof(*qid->id));
774 SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
775 (*commit)->nparents++;
779 got_object_commit_close(*commit);
785 const struct got_error *
786 got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
788 const struct got_error *err = NULL;
791 const size_t min_datalen =
792 MIN(sizeof(struct got_imsg_error),
793 sizeof(struct got_imsg_commit_object));
797 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
801 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
803 switch (imsg.hdr.type) {
804 case GOT_IMSG_COMMIT:
805 err = get_commit_from_imsg(commit, &imsg, datalen, ibuf);
808 err = got_error(GOT_ERR_PRIVSEP_MSG);
817 const struct got_error *
818 got_privsep_send_tree(struct imsgbuf *ibuf, struct got_pathlist_head *entries,
821 const struct got_error *err = NULL;
822 struct got_imsg_tree_object itree;
823 struct got_pathlist_entry *pe;
825 int nimsg; /* number of imsg queued in ibuf */
827 itree.nentries = nentries;
828 if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
830 return got_error_from_errno("imsg_compose TREE");
832 totlen = sizeof(itree);
834 TAILQ_FOREACH(pe, entries, entry) {
835 const char *name = pe->path;
836 struct got_parsed_tree_entry *pte = pe->data;
838 size_t namelen = strlen(name);
839 size_t len = sizeof(struct got_imsg_tree_entry) + namelen;
841 if (len > MAX_IMSGSIZE)
842 return got_error(GOT_ERR_NO_SPACE);
845 if (totlen + len >= MAX_IMSGSIZE - (IMSG_HEADER_SIZE * nimsg)) {
846 err = flush_imsg(ibuf);
852 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_ENTRY, 0, 0, len);
854 return got_error_from_errno("imsg_create TREE_ENTRY");
856 /* Keep in sync with struct got_imsg_tree_object definition! */
857 if (imsg_add(wbuf, pte->id, SHA1_DIGEST_LENGTH) == -1) {
858 err = got_error_from_errno("imsg_add TREE_ENTRY");
862 if (imsg_add(wbuf, &pte->mode, sizeof(pte->mode)) == -1) {
863 err = got_error_from_errno("imsg_add TREE_ENTRY");
868 if (imsg_add(wbuf, name, namelen) == -1) {
869 err = got_error_from_errno("imsg_add TREE_ENTRY");
875 imsg_close(ibuf, wbuf);
880 return flush_imsg(ibuf);
883 const struct got_error *
884 got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
886 const struct got_error *err = NULL;
887 const size_t min_datalen =
888 MIN(sizeof(struct got_imsg_error),
889 sizeof(struct got_imsg_tree_object));
890 struct got_imsg_tree_object *itree;
895 err = read_imsg(ibuf);
903 struct got_imsg_tree_entry *ite;
904 struct got_tree_entry *te = NULL;
906 n = imsg_get(ibuf, &imsg);
908 if (*tree && (*tree)->nentries != nentries)
913 if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen)
914 return got_error(GOT_ERR_PRIVSEP_LEN);
916 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
918 switch (imsg.hdr.type) {
920 err = recv_imsg_error(&imsg, datalen);
923 /* This message should only appear once. */
925 err = got_error(GOT_ERR_PRIVSEP_MSG);
928 if (datalen != sizeof(*itree)) {
929 err = got_error(GOT_ERR_PRIVSEP_LEN);
933 *tree = malloc(sizeof(**tree));
935 err = got_error_from_errno("malloc");
938 (*tree)->entries = calloc(itree->nentries,
939 sizeof(struct got_tree_entry));
940 if ((*tree)->entries == NULL) {
941 err = got_error_from_errno("malloc");
944 (*tree)->nentries = itree->nentries;
947 case GOT_IMSG_TREE_ENTRY:
948 /* This message should be preceeded by GOT_IMSG_TREE. */
950 err = got_error(GOT_ERR_PRIVSEP_MSG);
953 if (datalen < sizeof(*ite) || datalen > MAX_IMSGSIZE) {
954 err = got_error(GOT_ERR_PRIVSEP_LEN);
958 /* Remaining data contains the entry's name. */
959 datalen -= sizeof(*ite);
960 if (datalen == 0 || datalen > MAX_IMSGSIZE) {
961 err = got_error(GOT_ERR_PRIVSEP_LEN);
966 if (datalen + 1 > sizeof(te->name)) {
967 err = got_error(GOT_ERR_NO_SPACE);
970 te = &(*tree)->entries[nentries];
971 memcpy(te->name, imsg.data + sizeof(*ite), datalen);
972 te->name[datalen] = '\0';
974 memcpy(te->id.sha1, ite->id, SHA1_DIGEST_LENGTH);
975 te->mode = ite->mode;
980 err = got_error(GOT_ERR_PRIVSEP_MSG);
987 if (*tree && (*tree)->nentries != nentries) {
989 err = got_error(GOT_ERR_PRIVSEP_LEN);
990 got_object_tree_close(*tree);
997 const struct got_error *
998 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
1001 struct got_imsg_blob iblob;
1004 iblob.hdrlen = hdrlen;
1009 if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
1010 return got_error(GOT_ERR_NO_SPACE);
1012 buf = malloc(sizeof(iblob) + size);
1014 return got_error_from_errno("malloc");
1016 memcpy(buf, &iblob, sizeof(iblob));
1017 memcpy(buf + sizeof(iblob), data, size);
1018 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
1019 sizeof(iblob) + size) == -1) {
1021 return got_error_from_errno("imsg_compose BLOB");
1025 /* Data has already been written to file descriptor. */
1026 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
1027 sizeof(iblob)) == -1)
1028 return got_error_from_errno("imsg_compose BLOB");
1032 return flush_imsg(ibuf);
1035 const struct got_error *
1036 got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1037 struct imsgbuf *ibuf)
1039 const struct got_error *err = NULL;
1041 struct got_imsg_blob *iblob;
1046 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1050 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1052 switch (imsg.hdr.type) {
1054 if (datalen < sizeof(*iblob)) {
1055 err = got_error(GOT_ERR_PRIVSEP_LEN);
1059 *size = iblob->size;
1060 *hdrlen = iblob->hdrlen;
1062 if (datalen == sizeof(*iblob)) {
1063 /* Data has been written to file descriptor. */
1067 if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX) {
1068 err = got_error(GOT_ERR_PRIVSEP_LEN);
1072 *outbuf = malloc(*size);
1073 if (*outbuf == NULL) {
1074 err = got_error_from_errno("malloc");
1077 memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
1080 err = got_error(GOT_ERR_PRIVSEP_MSG);
1089 static const struct got_error *
1090 send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
1092 const struct got_error *err = NULL;
1093 size_t offset, remain;
1096 remain = tagmsg_len;
1097 while (remain > 0) {
1098 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1100 if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
1101 tag->tagmsg + offset, n) == -1) {
1102 err = got_error_from_errno("imsg_compose TAG_TAGMSG");
1106 err = flush_imsg(ibuf);
1117 const struct got_error *
1118 got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
1120 const struct got_error *err = NULL;
1121 struct got_imsg_tag_object *itag;
1124 size_t tag_len = strlen(tag->tag);
1125 size_t tagger_len = strlen(tag->tagger);
1126 size_t tagmsg_len = strlen(tag->tagmsg);
1128 total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
1130 buf = malloc(total);
1132 return got_error_from_errno("malloc");
1134 itag = (struct got_imsg_tag_object *)buf;
1135 memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
1136 itag->obj_type = tag->obj_type;
1137 itag->tag_len = tag_len;
1138 itag->tagger_len = tagger_len;
1139 itag->tagger_time = tag->tagger_time;
1140 itag->tagger_gmtoff = tag->tagger_gmtoff;
1141 itag->tagmsg_len = tagmsg_len;
1143 len = sizeof(*itag);
1144 memcpy(buf + len, tag->tag, tag_len);
1146 memcpy(buf + len, tag->tagger, tagger_len);
1149 if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
1150 err = got_error_from_errno("imsg_compose TAG");
1154 if (tagmsg_len == 0 ||
1155 tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1156 err = flush_imsg(ibuf);
1160 err = send_tagmsg(ibuf, tag, tagmsg_len);
1166 const struct got_error *
1167 got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
1169 const struct got_error *err = NULL;
1171 struct got_imsg_tag_object *itag;
1172 size_t len, datalen;
1173 const size_t min_datalen =
1174 MIN(sizeof(struct got_imsg_error),
1175 sizeof(struct got_imsg_tag_object));
1179 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1183 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1186 switch (imsg.hdr.type) {
1188 if (datalen < sizeof(*itag)) {
1189 err = got_error(GOT_ERR_PRIVSEP_LEN);
1193 if (datalen != sizeof(*itag) + itag->tag_len +
1195 err = got_error(GOT_ERR_PRIVSEP_LEN);
1198 len += sizeof(*itag);
1200 *tag = calloc(1, sizeof(**tag));
1202 err = got_error_from_errno("calloc");
1206 memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
1208 if (itag->tag_len == 0) {
1209 (*tag)->tag = strdup("");
1210 if ((*tag)->tag == NULL) {
1211 err = got_error_from_errno("strdup");
1215 (*tag)->tag = malloc(itag->tag_len + 1);
1216 if ((*tag)->tag == NULL) {
1217 err = got_error_from_errno("malloc");
1220 memcpy((*tag)->tag, imsg.data + len,
1222 (*tag)->tag[itag->tag_len] = '\0';
1224 len += itag->tag_len;
1226 (*tag)->obj_type = itag->obj_type;
1227 (*tag)->tagger_time = itag->tagger_time;
1228 (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1230 if (itag->tagger_len == 0) {
1231 (*tag)->tagger = strdup("");
1232 if ((*tag)->tagger == NULL) {
1233 err = got_error_from_errno("strdup");
1237 (*tag)->tagger = malloc(itag->tagger_len + 1);
1238 if ((*tag)->tagger == NULL) {
1239 err = got_error_from_errno("malloc");
1242 memcpy((*tag)->tagger, imsg.data + len,
1244 (*tag)->tagger[itag->tagger_len] = '\0';
1246 len += itag->tagger_len;
1248 if (itag->tagmsg_len == 0) {
1249 (*tag)->tagmsg = strdup("");
1250 if ((*tag)->tagmsg == NULL) {
1251 err = got_error_from_errno("strdup");
1255 size_t offset = 0, remain = itag->tagmsg_len;
1257 (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1258 if ((*tag)->tagmsg == NULL) {
1259 err = got_error_from_errno("malloc");
1262 while (remain > 0) {
1263 struct imsg imsg_log;
1264 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1267 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1271 if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1272 return got_error(GOT_ERR_PRIVSEP_MSG);
1274 memcpy((*tag)->tagmsg + offset, imsg_log.data,
1276 imsg_free(&imsg_log);
1280 (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1285 err = got_error(GOT_ERR_PRIVSEP_MSG);
1294 const struct got_error *
1295 got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1296 struct got_packidx *packidx)
1298 const struct got_error *err = NULL;
1299 struct got_imsg_packidx ipackidx;
1300 struct got_imsg_pack ipack;
1303 ipackidx.len = packidx->len;
1304 fd = dup(packidx->fd);
1306 return got_error_from_errno("dup");
1308 if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
1309 sizeof(ipackidx)) == -1) {
1310 err = got_error_from_errno("imsg_compose PACKIDX");
1315 if (strlcpy(ipack.path_packfile, pack->path_packfile,
1316 sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
1317 return got_error(GOT_ERR_NO_SPACE);
1318 ipack.filesize = pack->filesize;
1322 return got_error_from_errno("dup");
1324 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
1326 err = got_error_from_errno("imsg_compose PACK");
1331 return flush_imsg(ibuf);
1334 const struct got_error *
1335 got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
1336 struct got_object_id *id)
1338 struct got_imsg_packed_object iobj;
1341 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
1343 if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
1344 &iobj, sizeof(iobj)) == -1)
1345 return got_error_from_errno("imsg_compose "
1346 "PACKED_OBJECT_REQUEST");
1348 return flush_imsg(ibuf);
1351 const struct got_error *
1352 got_privsep_send_gitconfig_parse_req(struct imsgbuf *ibuf, int fd)
1354 const struct got_error *err = NULL;
1356 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_PARSE_REQUEST, 0, 0, fd,
1358 err = got_error_from_errno("imsg_compose "
1359 "GITCONFIG_PARSE_REQUEST");
1364 return flush_imsg(ibuf);
1367 const struct got_error *
1368 got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *ibuf)
1370 if (imsg_compose(ibuf,
1371 GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST, 0, 0, -1,
1373 return got_error_from_errno("imsg_compose "
1374 "GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST");
1376 return flush_imsg(ibuf);
1379 const struct got_error *
1380 got_privsep_send_gitconfig_author_name_req(struct imsgbuf *ibuf)
1382 if (imsg_compose(ibuf,
1383 GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST, 0, 0, -1, NULL, 0) == -1)
1384 return got_error_from_errno("imsg_compose "
1385 "GITCONFIG_AUTHOR_NAME_REQUEST");
1387 return flush_imsg(ibuf);
1390 const struct got_error *
1391 got_privsep_send_gitconfig_author_email_req(struct imsgbuf *ibuf)
1393 if (imsg_compose(ibuf,
1394 GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST, 0, 0, -1, NULL, 0) == -1)
1395 return got_error_from_errno("imsg_compose "
1396 "GITCONFIG_AUTHOR_EMAIL_REQUEST");
1398 return flush_imsg(ibuf);
1401 const struct got_error *
1402 got_privsep_send_gitconfig_remotes_req(struct imsgbuf *ibuf)
1404 if (imsg_compose(ibuf,
1405 GOT_IMSG_GITCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
1406 return got_error_from_errno("imsg_compose "
1407 "GITCONFIG_REMOTE_REQUEST");
1409 return flush_imsg(ibuf);
1412 const struct got_error *
1413 got_privsep_send_gitconfig_owner_req(struct imsgbuf *ibuf)
1415 if (imsg_compose(ibuf,
1416 GOT_IMSG_GITCONFIG_OWNER_REQUEST, 0, 0, -1, NULL, 0) == -1)
1417 return got_error_from_errno("imsg_compose "
1418 "GITCONFIG_OWNER_REQUEST");
1420 return flush_imsg(ibuf);
1423 const struct got_error *
1424 got_privsep_send_gitconfig_str(struct imsgbuf *ibuf, const char *value)
1426 size_t len = value ? strlen(value) + 1 : 0;
1428 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_STR_VAL, 0, 0, -1,
1430 return got_error_from_errno("imsg_compose GITCONFIG_STR_VAL");
1432 return flush_imsg(ibuf);
1435 const struct got_error *
1436 got_privsep_recv_gitconfig_str(char **str, struct imsgbuf *ibuf)
1438 const struct got_error *err = NULL;
1441 const size_t min_datalen = 0;
1445 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1448 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1450 switch (imsg.hdr.type) {
1451 case GOT_IMSG_GITCONFIG_STR_VAL:
1454 *str = malloc(datalen);
1456 err = got_error_from_errno("malloc");
1459 if (strlcpy(*str, imsg.data, datalen) >= datalen)
1460 err = got_error(GOT_ERR_NO_SPACE);
1463 err = got_error(GOT_ERR_PRIVSEP_MSG);
1471 const struct got_error *
1472 got_privsep_send_gitconfig_int(struct imsgbuf *ibuf, int value)
1474 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_INT_VAL, 0, 0, -1,
1475 &value, sizeof(value)) == -1)
1476 return got_error_from_errno("imsg_compose GITCONFIG_INT_VAL");
1478 return flush_imsg(ibuf);
1481 const struct got_error *
1482 got_privsep_recv_gitconfig_int(int *val, struct imsgbuf *ibuf)
1484 const struct got_error *err = NULL;
1487 const size_t min_datalen =
1488 MIN(sizeof(struct got_imsg_error), sizeof(int));
1492 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1495 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1497 switch (imsg.hdr.type) {
1498 case GOT_IMSG_GITCONFIG_INT_VAL:
1499 if (datalen != sizeof(*val)) {
1500 err = got_error(GOT_ERR_PRIVSEP_LEN);
1503 memcpy(val, imsg.data, sizeof(*val));
1506 err = got_error(GOT_ERR_PRIVSEP_MSG);
1514 const struct got_error *
1515 got_privsep_send_gitconfig_remotes(struct imsgbuf *ibuf,
1516 struct got_remote_repo *remotes, int nremotes)
1518 const struct got_error *err = NULL;
1519 struct got_imsg_remotes iremotes;
1522 iremotes.nremotes = nremotes;
1523 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_REMOTES, 0, 0, -1,
1524 &iremotes, sizeof(iremotes)) == -1)
1525 return got_error_from_errno("imsg_compose GITCONFIG_REMOTES");
1527 err = flush_imsg(ibuf);
1532 for (i = 0; i < nremotes; i++) {
1533 struct got_imsg_remote iremote;
1534 size_t len = sizeof(iremote);
1537 iremote.name_len = strlen(remotes[i].name);
1538 len += iremote.name_len;
1539 iremote.url_len = strlen(remotes[i].url);
1540 len += iremote.url_len;
1542 wbuf = imsg_create(ibuf, GOT_IMSG_GITCONFIG_REMOTE, 0, 0, len);
1544 return got_error_from_errno(
1545 "imsg_create GITCONFIG_REMOTE");
1547 if (imsg_add(wbuf, &iremote, sizeof(iremote)) == -1) {
1548 err = got_error_from_errno(
1549 "imsg_add GIITCONFIG_REMOTE");
1554 if (imsg_add(wbuf, remotes[i].name, iremote.name_len) == -1) {
1555 err = got_error_from_errno(
1556 "imsg_add GIITCONFIG_REMOTE");
1560 if (imsg_add(wbuf, remotes[i].url, iremote.url_len) == -1) {
1561 err = got_error_from_errno(
1562 "imsg_add GIITCONFIG_REMOTE");
1568 imsg_close(ibuf, wbuf);
1569 err = flush_imsg(ibuf);
1577 const struct got_error *
1578 got_privsep_recv_gitconfig_remotes(struct got_remote_repo **remotes,
1579 int *nremotes, struct imsgbuf *ibuf)
1581 const struct got_error *err = NULL;
1584 struct got_imsg_remotes iremotes;
1585 struct got_imsg_remote iremote;
1589 iremotes.nremotes = 0;
1591 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremotes));
1594 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1596 switch (imsg.hdr.type) {
1597 case GOT_IMSG_GITCONFIG_REMOTES:
1598 if (datalen != sizeof(iremotes)) {
1599 err = got_error(GOT_ERR_PRIVSEP_LEN);
1602 memcpy(&iremotes, imsg.data, sizeof(iremotes));
1603 if (iremotes.nremotes == 0) {
1610 return got_error(GOT_ERR_PRIVSEP_MSG);
1615 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(iremote));
1616 if (*remotes == NULL)
1617 return got_error_from_errno("recallocarray");
1619 while (*nremotes < iremotes.nremotes) {
1620 struct got_remote_repo *remote;
1622 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremote));
1625 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1627 switch (imsg.hdr.type) {
1628 case GOT_IMSG_GITCONFIG_REMOTE:
1629 remote = &(*remotes)[*nremotes];
1630 if (datalen < sizeof(iremote)) {
1631 err = got_error(GOT_ERR_PRIVSEP_LEN);
1634 memcpy(&iremote, imsg.data, sizeof(iremote));
1635 if (iremote.name_len == 0 || iremote.url_len == 0 ||
1636 (sizeof(iremote) + iremote.name_len +
1637 iremote.url_len) > datalen) {
1638 err = got_error(GOT_ERR_PRIVSEP_LEN);
1641 remote->name = strndup(imsg.data + sizeof(iremote),
1643 if (remote->name == NULL) {
1644 err = got_error_from_errno("strndup");
1647 remote->url = strndup(imsg.data + sizeof(iremote) +
1648 iremote.name_len, iremote.url_len);
1649 if (remote->url == NULL) {
1650 err = got_error_from_errno("strndup");
1657 err = got_error(GOT_ERR_PRIVSEP_MSG);
1668 for (i = 0; i < *nremotes; i++) {
1669 free((*remotes)[i].name);
1670 free((*remotes)[i].url);
1679 const struct got_error *
1680 got_privsep_send_commit_traversal_request(struct imsgbuf *ibuf,
1681 struct got_object_id *id, int idx, const char *path)
1683 const struct got_error *err = NULL;
1685 size_t path_len = strlen(path) + 1;
1687 wbuf = imsg_create(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_REQUEST, 0, 0,
1688 sizeof(struct got_imsg_commit_traversal_request) + path_len);
1690 return got_error_from_errno(
1691 "imsg_create COMMIT_TRAVERSAL_REQUEST");
1692 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
1693 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
1697 if (imsg_add(wbuf, &idx, sizeof(idx)) == -1) {
1698 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
1702 if (imsg_add(wbuf, path, path_len) == -1) {
1703 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
1709 imsg_close(ibuf, wbuf);
1711 return flush_imsg(ibuf);
1714 const struct got_error *
1715 got_privsep_send_traversed_commits(struct got_object_id *commit_ids,
1716 size_t ncommits, struct imsgbuf *ibuf)
1718 const struct got_error *err;
1722 wbuf = imsg_create(ibuf, GOT_IMSG_TRAVERSED_COMMITS, 0, 0,
1723 sizeof(struct got_imsg_traversed_commits) +
1724 ncommits * SHA1_DIGEST_LENGTH);
1726 return got_error_from_errno("imsg_create TRAVERSED_COMMITS");
1728 if (imsg_add(wbuf, &ncommits, sizeof(ncommits)) == -1) {
1729 err = got_error_from_errno("imsg_add TRAVERSED_COMMITS");
1733 for (i = 0; i < ncommits; i++) {
1734 struct got_object_id *id = &commit_ids[i];
1735 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
1736 err = got_error_from_errno(
1737 "imsg_add TRAVERSED_COMMITS");
1744 imsg_close(ibuf, wbuf);
1746 return flush_imsg(ibuf);
1749 const struct got_error *
1750 got_privsep_recv_traversed_commits(struct got_commit_object **changed_commit,
1751 struct got_object_id **changed_commit_id,
1752 struct got_object_id_queue *commit_ids, struct imsgbuf *ibuf)
1754 const struct got_error *err = NULL;
1756 struct got_imsg_traversed_commits *icommits;
1760 *changed_commit = NULL;
1761 *changed_commit_id = NULL;
1764 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1768 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1769 switch (imsg.hdr.type) {
1770 case GOT_IMSG_TRAVERSED_COMMITS:
1771 icommits = imsg.data;
1772 if (datalen != sizeof(*icommits) +
1773 icommits->ncommits * SHA1_DIGEST_LENGTH) {
1774 err = got_error(GOT_ERR_PRIVSEP_LEN);
1777 for (i = 0; i < icommits->ncommits; i++) {
1778 struct got_object_qid *qid;
1779 uint8_t *sha1 = (uint8_t *)imsg.data +
1780 sizeof(*icommits) + i * SHA1_DIGEST_LENGTH;
1781 err = got_object_qid_alloc_partial(&qid);
1784 memcpy(qid->id->sha1, sha1, SHA1_DIGEST_LENGTH);
1785 SIMPLEQ_INSERT_TAIL(commit_ids, qid, entry);
1787 /* The last commit may contain a change. */
1788 if (i == icommits->ncommits - 1) {
1789 *changed_commit_id =
1790 got_object_id_dup(qid->id);
1791 if (*changed_commit_id == NULL) {
1792 err = got_error_from_errno(
1793 "got_object_id_dup");
1799 case GOT_IMSG_COMMIT:
1800 if (*changed_commit_id == NULL) {
1801 err = got_error(GOT_ERR_PRIVSEP_MSG);
1804 err = get_commit_from_imsg(changed_commit, &imsg,
1807 case GOT_IMSG_COMMIT_TRAVERSAL_DONE:
1811 err = got_error(GOT_ERR_PRIVSEP_MSG);
1821 got_object_id_queue_free(commit_ids);
1825 const struct got_error *
1826 got_privsep_send_commit_traversal_done(struct imsgbuf *ibuf)
1828 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_DONE, 0, 0, -1,
1830 return got_error_from_errno("imsg_compose TRAVERSAL_DONE");
1832 return flush_imsg(ibuf);
1835 const struct got_error *
1836 got_privsep_unveil_exec_helpers(void)
1838 const char *helpers[] = {
1839 GOT_PATH_PROG_READ_PACK,
1840 GOT_PATH_PROG_READ_OBJECT,
1841 GOT_PATH_PROG_READ_COMMIT,
1842 GOT_PATH_PROG_READ_TREE,
1843 GOT_PATH_PROG_READ_BLOB,
1844 GOT_PATH_PROG_READ_TAG,
1845 GOT_PATH_PROG_READ_GITCONFIG,
1849 for (i = 0; i < nitems(helpers); i++) {
1850 if (unveil(helpers[i], "x") == 0)
1852 return got_error_from_errno2("unveil", helpers[i]);
1859 got_privsep_exec_child(int imsg_fds[2], const char *path, const char *repo_path)
1861 if (close(imsg_fds[0]) != 0) {
1862 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
1866 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
1867 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
1870 if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
1871 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
1875 if (execl(path, path, repo_path, (char *)NULL) == -1) {
1876 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,