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_obj_req(struct imsgbuf *ibuf, int fd)
236 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_REQUEST, 0, 0, fd, NULL, 0)
238 return got_error_from_errno("imsg_compose OBJECT_REQUEST");
240 return flush_imsg(ibuf);
243 const struct got_error *
244 got_privsep_send_commit_req(struct imsgbuf *ibuf, int fd,
245 struct got_object_id *id, int pack_idx)
247 const struct got_error *err = NULL;
248 struct got_imsg_packed_object iobj, *iobjp;
251 if (id) { /* commit is packed */
253 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
261 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_REQUEST, 0, 0, fd, iobjp, len)
263 err = got_error_from_errno("imsg_compose COMMIT_REQUEST");
268 return flush_imsg(ibuf);
271 const struct got_error *
272 got_privsep_send_tree_req(struct imsgbuf *ibuf, int fd,
273 struct got_object_id *id, int pack_idx)
275 const struct got_error *err = NULL;
277 size_t len = id ? sizeof(struct got_imsg_packed_object) : 0;
279 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_REQUEST, 0, 0, len);
281 return got_error_from_errno("imsg_create TREE_REQUEST");
283 if (id) { /* tree is packed */
284 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
285 err = got_error_from_errno("imsg_add TREE_ENTRY");
290 if (imsg_add(wbuf, &pack_idx, sizeof(pack_idx)) == -1) {
291 err = got_error_from_errno("imsg_add TREE_ENTRY");
298 imsg_close(ibuf, wbuf);
300 return flush_imsg(ibuf);
303 const struct got_error *
304 got_privsep_send_tag_req(struct imsgbuf *ibuf, int fd,
305 struct got_object_id *id, int pack_idx)
307 struct got_imsg_packed_object iobj, *iobjp;
310 if (id) { /* tag is packed */
312 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
320 if (imsg_compose(ibuf, GOT_IMSG_TAG_REQUEST, 0, 0, fd, iobjp, len)
322 return got_error_from_errno("imsg_compose TAG_REQUEST");
324 return flush_imsg(ibuf);
327 const struct got_error *
328 got_privsep_send_blob_req(struct imsgbuf *ibuf, int infd,
329 struct got_object_id *id, int pack_idx)
331 const struct got_error *err = NULL;
332 struct got_imsg_packed_object iobj, *iobjp;
335 if (id) { /* blob is packed */
337 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
345 if (imsg_compose(ibuf, GOT_IMSG_BLOB_REQUEST, 0, 0, infd, iobjp, len)
347 err = got_error_from_errno("imsg_compose BLOB_REQUEST");
352 return flush_imsg(ibuf);
355 const struct got_error *
356 got_privsep_send_blob_outfd(struct imsgbuf *ibuf, int outfd)
358 const struct got_error *err = NULL;
360 if (imsg_compose(ibuf, GOT_IMSG_BLOB_OUTFD, 0, 0, outfd, NULL, 0)
362 err = got_error_from_errno("imsg_compose BLOB_OUTFD");
367 return flush_imsg(ibuf);
370 const struct got_error *
371 got_privsep_send_tmpfd(struct imsgbuf *ibuf, int fd)
373 const struct got_error *err = NULL;
375 if (imsg_compose(ibuf, GOT_IMSG_TMPFD, 0, 0, fd, NULL, 0)
377 err = got_error_from_errno("imsg_compose TMPFD");
382 return flush_imsg(ibuf);
385 const struct got_error *
386 got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj)
388 struct got_imsg_object iobj;
390 memcpy(iobj.id, obj->id.sha1, sizeof(iobj.id));
391 iobj.type = obj->type;
392 iobj.flags = obj->flags;
393 iobj.hdrlen = obj->hdrlen;
394 iobj.size = obj->size;
395 if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
396 iobj.pack_offset = obj->pack_offset;
397 iobj.pack_idx = obj->pack_idx;
400 if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
402 return got_error_from_errno("imsg_compose OBJECT");
404 return flush_imsg(ibuf);
407 const struct got_error *
408 got_privsep_send_fetch_req(struct imsgbuf *ibuf, int fd)
410 const struct got_error *err = NULL;
412 if (imsg_compose(ibuf, GOT_IMSG_FETCH_REQUEST, 0, 0, fd,
414 err = got_error_from_errno("imsg_compose FETCH_REQUEST");
418 return flush_imsg(ibuf);
421 const struct got_error *
422 got_privsep_send_fetch_done(struct imsgbuf *ibuf, struct got_object_id hash)
424 if (imsg_compose(ibuf, GOT_IMSG_FETCH_DONE, 0, 0, -1,
425 hash.sha1, SHA1_DIGEST_LENGTH) == -1)
426 return got_error_from_errno("imsg_compose FETCH");
427 return flush_imsg(ibuf);
430 const struct got_error *
431 got_privsep_wait_fetch_done(struct imsgbuf *ibuf, struct got_object_id *hash)
433 const struct got_error *err = NULL;
436 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
439 if (imsg.hdr.type == GOT_IMSG_FETCH_DONE &&
440 imsg.hdr.len - sizeof(imsg.hdr) == SHA1_DIGEST_LENGTH) {
441 memcpy(hash->sha1, imsg.data, SHA1_DIGEST_LENGTH);
447 return got_error(GOT_ERR_PRIVSEP_MSG);
451 const struct got_error *
452 got_privsep_send_index_pack_req(struct imsgbuf *ibuf, int fd, struct got_object_id *hash)
454 const struct got_error *err = NULL;
456 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_REQUEST, 0, 0, fd,
457 hash->sha1, SHA1_DIGEST_LENGTH) == -1) {
458 err = got_error_from_errno("imsg_compose INDEX_REQUEST");
462 return flush_imsg(ibuf);
465 const struct got_error *
466 got_privsep_send_index_pack_done(struct imsgbuf *ibuf)
468 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_DONE, 0, 0, -1, NULL, 0) == -1)
469 return got_error_from_errno("imsg_compose FETCH");
470 return flush_imsg(ibuf);
473 const struct got_error *
474 got_privsep_wait_index_pack_done(struct imsgbuf *ibuf)
476 const struct got_error *err = NULL;
479 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
482 if (imsg.hdr.type == GOT_IMSG_IDXPACK_DONE)
485 return got_error(GOT_ERR_PRIVSEP_MSG);
489 const struct got_error *
490 got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
491 struct imsgbuf *ibuf)
493 const struct got_error *err = NULL;
494 struct got_imsg_object *iobj;
495 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
497 if (datalen != sizeof(*iobj))
498 return got_error(GOT_ERR_PRIVSEP_LEN);
501 *obj = calloc(1, sizeof(**obj));
503 return got_error_from_errno("calloc");
505 memcpy((*obj)->id.sha1, iobj->id, SHA1_DIGEST_LENGTH);
506 (*obj)->type = iobj->type;
507 (*obj)->flags = iobj->flags;
508 (*obj)->hdrlen = iobj->hdrlen;
509 (*obj)->size = iobj->size;
510 /* path_packfile is handled by caller */
511 if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
512 (*obj)->pack_offset = iobj->pack_offset;
513 (*obj)->pack_idx = iobj->pack_idx;
519 const struct got_error *
520 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
522 const struct got_error *err = NULL;
524 const size_t min_datalen =
525 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
529 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
533 switch (imsg.hdr.type) {
534 case GOT_IMSG_OBJECT:
535 err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
538 err = got_error(GOT_ERR_PRIVSEP_MSG);
547 static const struct got_error *
548 send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
551 const struct got_error *err = NULL;
552 size_t offset, remain;
557 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
559 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
560 commit->logmsg + offset, n) == -1) {
561 err = got_error_from_errno("imsg_compose "
566 err = flush_imsg(ibuf);
577 const struct got_error *
578 got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
580 const struct got_error *err = NULL;
581 struct got_imsg_commit_object *icommit;
584 struct got_object_qid *qid;
585 size_t author_len = strlen(commit->author);
586 size_t committer_len = strlen(commit->committer);
587 size_t logmsg_len = strlen(commit->logmsg);
589 total = sizeof(*icommit) + author_len + committer_len +
590 commit->nparents * SHA1_DIGEST_LENGTH;
594 return got_error_from_errno("malloc");
596 icommit = (struct got_imsg_commit_object *)buf;
597 memcpy(icommit->tree_id, commit->tree_id->sha1,
598 sizeof(icommit->tree_id));
599 icommit->author_len = author_len;
600 icommit->author_time = commit->author_time;
601 icommit->author_gmtoff = commit->author_gmtoff;
602 icommit->committer_len = committer_len;
603 icommit->committer_time = commit->committer_time;
604 icommit->committer_gmtoff = commit->committer_gmtoff;
605 icommit->logmsg_len = logmsg_len;
606 icommit->nparents = commit->nparents;
608 len = sizeof(*icommit);
609 memcpy(buf + len, commit->author, author_len);
611 memcpy(buf + len, commit->committer, committer_len);
612 len += committer_len;
613 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
614 memcpy(buf + len, qid->id, SHA1_DIGEST_LENGTH);
615 len += SHA1_DIGEST_LENGTH;
618 if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
619 err = got_error_from_errno("imsg_compose COMMIT");
623 if (logmsg_len == 0 ||
624 logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
625 err = flush_imsg(ibuf);
629 err = send_commit_logmsg(ibuf, commit, logmsg_len);
635 static const struct got_error *
636 get_commit_from_imsg(struct got_commit_object **commit,
637 struct imsg *imsg, size_t datalen, struct imsgbuf *ibuf)
639 const struct got_error *err = NULL;
640 struct got_imsg_commit_object *icommit;
644 if (datalen < sizeof(*icommit))
645 return got_error(GOT_ERR_PRIVSEP_LEN);
647 icommit = imsg->data;
648 if (datalen != sizeof(*icommit) + icommit->author_len +
649 icommit->committer_len +
650 icommit->nparents * SHA1_DIGEST_LENGTH)
651 return got_error(GOT_ERR_PRIVSEP_LEN);
653 if (icommit->nparents < 0)
654 return got_error(GOT_ERR_PRIVSEP_LEN);
656 len += sizeof(*icommit);
658 *commit = got_object_commit_alloc_partial();
660 return got_error_from_errno(
661 "got_object_commit_alloc_partial");
663 memcpy((*commit)->tree_id->sha1, icommit->tree_id,
665 (*commit)->author_time = icommit->author_time;
666 (*commit)->author_gmtoff = icommit->author_gmtoff;
667 (*commit)->committer_time = icommit->committer_time;
668 (*commit)->committer_gmtoff = icommit->committer_gmtoff;
670 if (icommit->author_len == 0) {
671 (*commit)->author = strdup("");
672 if ((*commit)->author == NULL) {
673 err = got_error_from_errno("strdup");
677 (*commit)->author = malloc(icommit->author_len + 1);
678 if ((*commit)->author == NULL) {
679 err = got_error_from_errno("malloc");
682 memcpy((*commit)->author, imsg->data + len,
683 icommit->author_len);
684 (*commit)->author[icommit->author_len] = '\0';
686 len += icommit->author_len;
688 if (icommit->committer_len == 0) {
689 (*commit)->committer = strdup("");
690 if ((*commit)->committer == NULL) {
691 err = got_error_from_errno("strdup");
695 (*commit)->committer =
696 malloc(icommit->committer_len + 1);
697 if ((*commit)->committer == NULL) {
698 err = got_error_from_errno("malloc");
701 memcpy((*commit)->committer, imsg->data + len,
702 icommit->committer_len);
703 (*commit)->committer[icommit->committer_len] = '\0';
705 len += icommit->committer_len;
707 if (icommit->logmsg_len == 0) {
708 (*commit)->logmsg = strdup("");
709 if ((*commit)->logmsg == NULL) {
710 err = got_error_from_errno("strdup");
714 size_t offset = 0, remain = icommit->logmsg_len;
716 (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
717 if ((*commit)->logmsg == NULL) {
718 err = got_error_from_errno("malloc");
722 struct imsg imsg_log;
723 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
726 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
730 if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG) {
731 err = got_error(GOT_ERR_PRIVSEP_MSG);
735 memcpy((*commit)->logmsg + offset,
737 imsg_free(&imsg_log);
741 (*commit)->logmsg[icommit->logmsg_len] = '\0';
744 for (i = 0; i < icommit->nparents; i++) {
745 struct got_object_qid *qid;
747 err = got_object_qid_alloc_partial(&qid);
750 memcpy(qid->id, imsg->data + len +
751 i * SHA1_DIGEST_LENGTH, sizeof(*qid->id));
752 SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
753 (*commit)->nparents++;
757 got_object_commit_close(*commit);
763 const struct got_error *
764 got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
766 const struct got_error *err = NULL;
769 const size_t min_datalen =
770 MIN(sizeof(struct got_imsg_error),
771 sizeof(struct got_imsg_commit_object));
775 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
779 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
781 switch (imsg.hdr.type) {
782 case GOT_IMSG_COMMIT:
783 err = get_commit_from_imsg(commit, &imsg, datalen, ibuf);
786 err = got_error(GOT_ERR_PRIVSEP_MSG);
795 const struct got_error *
796 got_privsep_send_tree(struct imsgbuf *ibuf, struct got_pathlist_head *entries,
799 const struct got_error *err = NULL;
800 struct got_imsg_tree_object itree;
801 struct got_pathlist_entry *pe;
803 int nimsg; /* number of imsg queued in ibuf */
805 itree.nentries = nentries;
806 if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
808 return got_error_from_errno("imsg_compose TREE");
810 totlen = sizeof(itree);
812 TAILQ_FOREACH(pe, entries, entry) {
813 const char *name = pe->path;
814 struct got_parsed_tree_entry *pte = pe->data;
816 size_t namelen = strlen(name);
817 size_t len = sizeof(struct got_imsg_tree_entry) + namelen;
819 if (len > MAX_IMSGSIZE)
820 return got_error(GOT_ERR_NO_SPACE);
823 if (totlen + len >= MAX_IMSGSIZE - (IMSG_HEADER_SIZE * nimsg)) {
824 err = flush_imsg(ibuf);
830 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_ENTRY, 0, 0, len);
832 return got_error_from_errno("imsg_create TREE_ENTRY");
834 /* Keep in sync with struct got_imsg_tree_object definition! */
835 if (imsg_add(wbuf, pte->id, SHA1_DIGEST_LENGTH) == -1) {
836 err = got_error_from_errno("imsg_add TREE_ENTRY");
840 if (imsg_add(wbuf, &pte->mode, sizeof(pte->mode)) == -1) {
841 err = got_error_from_errno("imsg_add TREE_ENTRY");
846 if (imsg_add(wbuf, name, namelen) == -1) {
847 err = got_error_from_errno("imsg_add TREE_ENTRY");
853 imsg_close(ibuf, wbuf);
858 return flush_imsg(ibuf);
861 const struct got_error *
862 got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
864 const struct got_error *err = NULL;
865 const size_t min_datalen =
866 MIN(sizeof(struct got_imsg_error),
867 sizeof(struct got_imsg_tree_object));
868 struct got_imsg_tree_object *itree;
873 err = read_imsg(ibuf);
881 struct got_imsg_tree_entry *ite;
882 struct got_tree_entry *te = NULL;
884 n = imsg_get(ibuf, &imsg);
886 if (*tree && (*tree)->nentries != nentries)
891 if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen)
892 return got_error(GOT_ERR_PRIVSEP_LEN);
894 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
896 switch (imsg.hdr.type) {
898 err = recv_imsg_error(&imsg, datalen);
901 /* This message should only appear once. */
903 err = got_error(GOT_ERR_PRIVSEP_MSG);
906 if (datalen != sizeof(*itree)) {
907 err = got_error(GOT_ERR_PRIVSEP_LEN);
911 *tree = malloc(sizeof(**tree));
913 err = got_error_from_errno("malloc");
916 (*tree)->entries = calloc(itree->nentries,
917 sizeof(struct got_tree_entry));
918 if ((*tree)->entries == NULL) {
919 err = got_error_from_errno("malloc");
922 (*tree)->nentries = itree->nentries;
925 case GOT_IMSG_TREE_ENTRY:
926 /* This message should be preceeded by GOT_IMSG_TREE. */
928 err = got_error(GOT_ERR_PRIVSEP_MSG);
931 if (datalen < sizeof(*ite) || datalen > MAX_IMSGSIZE) {
932 err = got_error(GOT_ERR_PRIVSEP_LEN);
936 /* Remaining data contains the entry's name. */
937 datalen -= sizeof(*ite);
938 if (datalen == 0 || datalen > MAX_IMSGSIZE) {
939 err = got_error(GOT_ERR_PRIVSEP_LEN);
944 if (datalen + 1 > sizeof(te->name)) {
945 err = got_error(GOT_ERR_NO_SPACE);
948 te = &(*tree)->entries[nentries];
949 memcpy(te->name, imsg.data + sizeof(*ite), datalen);
950 te->name[datalen] = '\0';
952 memcpy(te->id.sha1, ite->id, SHA1_DIGEST_LENGTH);
953 te->mode = ite->mode;
958 err = got_error(GOT_ERR_PRIVSEP_MSG);
965 if (*tree && (*tree)->nentries != nentries) {
967 err = got_error(GOT_ERR_PRIVSEP_LEN);
968 got_object_tree_close(*tree);
975 const struct got_error *
976 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
979 struct got_imsg_blob iblob;
982 iblob.hdrlen = hdrlen;
987 if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
988 return got_error(GOT_ERR_NO_SPACE);
990 buf = malloc(sizeof(iblob) + size);
992 return got_error_from_errno("malloc");
994 memcpy(buf, &iblob, sizeof(iblob));
995 memcpy(buf + sizeof(iblob), data, size);
996 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
997 sizeof(iblob) + size) == -1) {
999 return got_error_from_errno("imsg_compose BLOB");
1003 /* Data has already been written to file descriptor. */
1004 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
1005 sizeof(iblob)) == -1)
1006 return got_error_from_errno("imsg_compose BLOB");
1010 return flush_imsg(ibuf);
1013 const struct got_error *
1014 got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1015 struct imsgbuf *ibuf)
1017 const struct got_error *err = NULL;
1019 struct got_imsg_blob *iblob;
1024 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1028 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1030 switch (imsg.hdr.type) {
1032 if (datalen < sizeof(*iblob)) {
1033 err = got_error(GOT_ERR_PRIVSEP_LEN);
1037 *size = iblob->size;
1038 *hdrlen = iblob->hdrlen;
1040 if (datalen == sizeof(*iblob)) {
1041 /* Data has been written to file descriptor. */
1045 if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX) {
1046 err = got_error(GOT_ERR_PRIVSEP_LEN);
1050 *outbuf = malloc(*size);
1051 if (*outbuf == NULL) {
1052 err = got_error_from_errno("malloc");
1055 memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
1058 err = got_error(GOT_ERR_PRIVSEP_MSG);
1067 static const struct got_error *
1068 send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
1070 const struct got_error *err = NULL;
1071 size_t offset, remain;
1074 remain = tagmsg_len;
1075 while (remain > 0) {
1076 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1078 if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
1079 tag->tagmsg + offset, n) == -1) {
1080 err = got_error_from_errno("imsg_compose TAG_TAGMSG");
1084 err = flush_imsg(ibuf);
1095 const struct got_error *
1096 got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
1098 const struct got_error *err = NULL;
1099 struct got_imsg_tag_object *itag;
1102 size_t tag_len = strlen(tag->tag);
1103 size_t tagger_len = strlen(tag->tagger);
1104 size_t tagmsg_len = strlen(tag->tagmsg);
1106 total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
1108 buf = malloc(total);
1110 return got_error_from_errno("malloc");
1112 itag = (struct got_imsg_tag_object *)buf;
1113 memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
1114 itag->obj_type = tag->obj_type;
1115 itag->tag_len = tag_len;
1116 itag->tagger_len = tagger_len;
1117 itag->tagger_time = tag->tagger_time;
1118 itag->tagger_gmtoff = tag->tagger_gmtoff;
1119 itag->tagmsg_len = tagmsg_len;
1121 len = sizeof(*itag);
1122 memcpy(buf + len, tag->tag, tag_len);
1124 memcpy(buf + len, tag->tagger, tagger_len);
1127 if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
1128 err = got_error_from_errno("imsg_compose TAG");
1132 if (tagmsg_len == 0 ||
1133 tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1134 err = flush_imsg(ibuf);
1138 err = send_tagmsg(ibuf, tag, tagmsg_len);
1144 const struct got_error *
1145 got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
1147 const struct got_error *err = NULL;
1149 struct got_imsg_tag_object *itag;
1150 size_t len, datalen;
1151 const size_t min_datalen =
1152 MIN(sizeof(struct got_imsg_error),
1153 sizeof(struct got_imsg_tag_object));
1157 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1161 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1164 switch (imsg.hdr.type) {
1166 if (datalen < sizeof(*itag)) {
1167 err = got_error(GOT_ERR_PRIVSEP_LEN);
1171 if (datalen != sizeof(*itag) + itag->tag_len +
1173 err = got_error(GOT_ERR_PRIVSEP_LEN);
1176 len += sizeof(*itag);
1178 *tag = calloc(1, sizeof(**tag));
1180 err = got_error_from_errno("calloc");
1184 memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
1186 if (itag->tag_len == 0) {
1187 (*tag)->tag = strdup("");
1188 if ((*tag)->tag == NULL) {
1189 err = got_error_from_errno("strdup");
1193 (*tag)->tag = malloc(itag->tag_len + 1);
1194 if ((*tag)->tag == NULL) {
1195 err = got_error_from_errno("malloc");
1198 memcpy((*tag)->tag, imsg.data + len,
1200 (*tag)->tag[itag->tag_len] = '\0';
1202 len += itag->tag_len;
1204 (*tag)->obj_type = itag->obj_type;
1205 (*tag)->tagger_time = itag->tagger_time;
1206 (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1208 if (itag->tagger_len == 0) {
1209 (*tag)->tagger = strdup("");
1210 if ((*tag)->tagger == NULL) {
1211 err = got_error_from_errno("strdup");
1215 (*tag)->tagger = malloc(itag->tagger_len + 1);
1216 if ((*tag)->tagger == NULL) {
1217 err = got_error_from_errno("malloc");
1220 memcpy((*tag)->tagger, imsg.data + len,
1222 (*tag)->tagger[itag->tagger_len] = '\0';
1224 len += itag->tagger_len;
1226 if (itag->tagmsg_len == 0) {
1227 (*tag)->tagmsg = strdup("");
1228 if ((*tag)->tagmsg == NULL) {
1229 err = got_error_from_errno("strdup");
1233 size_t offset = 0, remain = itag->tagmsg_len;
1235 (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1236 if ((*tag)->tagmsg == NULL) {
1237 err = got_error_from_errno("malloc");
1240 while (remain > 0) {
1241 struct imsg imsg_log;
1242 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1245 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1249 if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1250 return got_error(GOT_ERR_PRIVSEP_MSG);
1252 memcpy((*tag)->tagmsg + offset, imsg_log.data,
1254 imsg_free(&imsg_log);
1258 (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1263 err = got_error(GOT_ERR_PRIVSEP_MSG);
1272 const struct got_error *
1273 got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1274 struct got_packidx *packidx)
1276 const struct got_error *err = NULL;
1277 struct got_imsg_packidx ipackidx;
1278 struct got_imsg_pack ipack;
1281 ipackidx.len = packidx->len;
1282 fd = dup(packidx->fd);
1284 return got_error_from_errno("dup");
1286 if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
1287 sizeof(ipackidx)) == -1) {
1288 err = got_error_from_errno("imsg_compose PACKIDX");
1293 if (strlcpy(ipack.path_packfile, pack->path_packfile,
1294 sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
1295 return got_error(GOT_ERR_NO_SPACE);
1296 ipack.filesize = pack->filesize;
1300 return got_error_from_errno("dup");
1302 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
1304 err = got_error_from_errno("imsg_compose PACK");
1309 return flush_imsg(ibuf);
1312 const struct got_error *
1313 got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
1314 struct got_object_id *id)
1316 struct got_imsg_packed_object iobj;
1319 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
1321 if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
1322 &iobj, sizeof(iobj)) == -1)
1323 return got_error_from_errno("imsg_compose "
1324 "PACKED_OBJECT_REQUEST");
1326 return flush_imsg(ibuf);
1329 const struct got_error *
1330 got_privsep_send_gitconfig_parse_req(struct imsgbuf *ibuf, int fd)
1332 const struct got_error *err = NULL;
1334 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_PARSE_REQUEST, 0, 0, fd,
1336 err = got_error_from_errno("imsg_compose "
1337 "GITCONFIG_PARSE_REQUEST");
1342 return flush_imsg(ibuf);
1345 const struct got_error *
1346 got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *ibuf)
1348 if (imsg_compose(ibuf,
1349 GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST, 0, 0, -1,
1351 return got_error_from_errno("imsg_compose "
1352 "GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST");
1354 return flush_imsg(ibuf);
1357 const struct got_error *
1358 got_privsep_send_gitconfig_author_name_req(struct imsgbuf *ibuf)
1360 if (imsg_compose(ibuf,
1361 GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST, 0, 0, -1, NULL, 0) == -1)
1362 return got_error_from_errno("imsg_compose "
1363 "GITCONFIG_AUTHOR_NAME_REQUEST");
1365 return flush_imsg(ibuf);
1368 const struct got_error *
1369 got_privsep_send_gitconfig_author_email_req(struct imsgbuf *ibuf)
1371 if (imsg_compose(ibuf,
1372 GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST, 0, 0, -1, NULL, 0) == -1)
1373 return got_error_from_errno("imsg_compose "
1374 "GITCONFIG_AUTHOR_EMAIL_REQUEST");
1376 return flush_imsg(ibuf);
1379 const struct got_error *
1380 got_privsep_send_gitconfig_remotes_req(struct imsgbuf *ibuf)
1382 if (imsg_compose(ibuf,
1383 GOT_IMSG_GITCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
1384 return got_error_from_errno("imsg_compose "
1385 "GITCONFIG_REMOTE_REQUEST");
1387 return flush_imsg(ibuf);
1390 const struct got_error *
1391 got_privsep_send_gitconfig_owner_req(struct imsgbuf *ibuf)
1393 if (imsg_compose(ibuf,
1394 GOT_IMSG_GITCONFIG_OWNER_REQUEST, 0, 0, -1, NULL, 0) == -1)
1395 return got_error_from_errno("imsg_compose "
1396 "GITCONFIG_OWNER_REQUEST");
1398 return flush_imsg(ibuf);
1401 const struct got_error *
1402 got_privsep_send_gitconfig_str(struct imsgbuf *ibuf, const char *value)
1404 size_t len = value ? strlen(value) + 1 : 0;
1406 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_STR_VAL, 0, 0, -1,
1408 return got_error_from_errno("imsg_compose GITCONFIG_STR_VAL");
1410 return flush_imsg(ibuf);
1413 const struct got_error *
1414 got_privsep_recv_gitconfig_str(char **str, struct imsgbuf *ibuf)
1416 const struct got_error *err = NULL;
1419 const size_t min_datalen = 0;
1423 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1426 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1428 switch (imsg.hdr.type) {
1429 case GOT_IMSG_GITCONFIG_STR_VAL:
1432 *str = malloc(datalen);
1434 err = got_error_from_errno("malloc");
1437 if (strlcpy(*str, imsg.data, datalen) >= datalen)
1438 err = got_error(GOT_ERR_NO_SPACE);
1441 err = got_error(GOT_ERR_PRIVSEP_MSG);
1449 const struct got_error *
1450 got_privsep_send_gitconfig_int(struct imsgbuf *ibuf, int value)
1452 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_INT_VAL, 0, 0, -1,
1453 &value, sizeof(value)) == -1)
1454 return got_error_from_errno("imsg_compose GITCONFIG_INT_VAL");
1456 return flush_imsg(ibuf);
1459 const struct got_error *
1460 got_privsep_recv_gitconfig_int(int *val, struct imsgbuf *ibuf)
1462 const struct got_error *err = NULL;
1465 const size_t min_datalen =
1466 MIN(sizeof(struct got_imsg_error), sizeof(int));
1470 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1473 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1475 switch (imsg.hdr.type) {
1476 case GOT_IMSG_GITCONFIG_INT_VAL:
1477 if (datalen != sizeof(*val)) {
1478 err = got_error(GOT_ERR_PRIVSEP_LEN);
1481 memcpy(val, imsg.data, sizeof(*val));
1484 err = got_error(GOT_ERR_PRIVSEP_MSG);
1492 const struct got_error *
1493 got_privsep_send_gitconfig_remotes(struct imsgbuf *ibuf,
1494 struct got_remote_repo *remotes, int nremotes)
1496 const struct got_error *err = NULL;
1497 struct got_imsg_remotes iremotes;
1500 iremotes.nremotes = nremotes;
1501 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_REMOTES, 0, 0, -1,
1502 &iremotes, sizeof(iremotes)) == -1)
1503 return got_error_from_errno("imsg_compose GITCONFIG_REMOTES");
1505 err = flush_imsg(ibuf);
1510 for (i = 0; i < nremotes; i++) {
1511 struct got_imsg_remote iremote;
1512 size_t len = sizeof(iremote);
1515 iremote.name_len = strlen(remotes[i].name);
1516 len += iremote.name_len;
1517 iremote.url_len = strlen(remotes[i].url);
1518 len += iremote.url_len;
1520 wbuf = imsg_create(ibuf, GOT_IMSG_GITCONFIG_REMOTE, 0, 0, len);
1522 return got_error_from_errno(
1523 "imsg_create GITCONFIG_REMOTE");
1525 if (imsg_add(wbuf, &iremote, sizeof(iremote)) == -1) {
1526 err = got_error_from_errno(
1527 "imsg_add GIITCONFIG_REMOTE");
1532 if (imsg_add(wbuf, remotes[i].name, iremote.name_len) == -1) {
1533 err = got_error_from_errno(
1534 "imsg_add GIITCONFIG_REMOTE");
1538 if (imsg_add(wbuf, remotes[i].url, iremote.url_len) == -1) {
1539 err = got_error_from_errno(
1540 "imsg_add GIITCONFIG_REMOTE");
1546 imsg_close(ibuf, wbuf);
1547 err = flush_imsg(ibuf);
1555 const struct got_error *
1556 got_privsep_recv_gitconfig_remotes(struct got_remote_repo **remotes,
1557 int *nremotes, struct imsgbuf *ibuf)
1559 const struct got_error *err = NULL;
1562 struct got_imsg_remotes iremotes;
1563 struct got_imsg_remote iremote;
1567 iremotes.nremotes = 0;
1569 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremotes));
1572 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1574 switch (imsg.hdr.type) {
1575 case GOT_IMSG_GITCONFIG_REMOTES:
1576 if (datalen != sizeof(iremotes)) {
1577 err = got_error(GOT_ERR_PRIVSEP_LEN);
1580 memcpy(&iremotes, imsg.data, sizeof(iremotes));
1581 if (iremotes.nremotes == 0) {
1588 return got_error(GOT_ERR_PRIVSEP_MSG);
1593 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(iremote));
1594 if (*remotes == NULL)
1595 return got_error_from_errno("recallocarray");
1597 while (*nremotes < iremotes.nremotes) {
1598 struct got_remote_repo *remote;
1600 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremote));
1603 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1605 switch (imsg.hdr.type) {
1606 case GOT_IMSG_GITCONFIG_REMOTE:
1607 remote = &(*remotes)[*nremotes];
1608 if (datalen < sizeof(iremote)) {
1609 err = got_error(GOT_ERR_PRIVSEP_LEN);
1612 memcpy(&iremote, imsg.data, sizeof(iremote));
1613 if (iremote.name_len == 0 || iremote.url_len == 0 ||
1614 (sizeof(iremote) + iremote.name_len +
1615 iremote.url_len) > datalen) {
1616 err = got_error(GOT_ERR_PRIVSEP_LEN);
1619 remote->name = strndup(imsg.data + sizeof(iremote),
1621 if (remote->name == NULL) {
1622 err = got_error_from_errno("strndup");
1625 remote->url = strndup(imsg.data + sizeof(iremote) +
1626 iremote.name_len, iremote.url_len);
1627 if (remote->url == NULL) {
1628 err = got_error_from_errno("strndup");
1635 err = got_error(GOT_ERR_PRIVSEP_MSG);
1646 for (i = 0; i < *nremotes; i++) {
1647 free((*remotes)[i].name);
1648 free((*remotes)[i].url);
1657 const struct got_error *
1658 got_privsep_send_commit_traversal_request(struct imsgbuf *ibuf,
1659 struct got_object_id *id, int idx, const char *path)
1661 const struct got_error *err = NULL;
1663 size_t path_len = strlen(path) + 1;
1665 wbuf = imsg_create(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_REQUEST, 0, 0,
1666 sizeof(struct got_imsg_commit_traversal_request) + path_len);
1668 return got_error_from_errno(
1669 "imsg_create COMMIT_TRAVERSAL_REQUEST");
1670 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
1671 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
1675 if (imsg_add(wbuf, &idx, sizeof(idx)) == -1) {
1676 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
1680 if (imsg_add(wbuf, path, path_len) == -1) {
1681 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
1687 imsg_close(ibuf, wbuf);
1689 return flush_imsg(ibuf);
1692 const struct got_error *
1693 got_privsep_send_traversed_commits(struct got_object_id *commit_ids,
1694 size_t ncommits, struct imsgbuf *ibuf)
1696 const struct got_error *err;
1700 wbuf = imsg_create(ibuf, GOT_IMSG_TRAVERSED_COMMITS, 0, 0,
1701 sizeof(struct got_imsg_traversed_commits) +
1702 ncommits * SHA1_DIGEST_LENGTH);
1704 return got_error_from_errno("imsg_create TRAVERSED_COMMITS");
1706 if (imsg_add(wbuf, &ncommits, sizeof(ncommits)) == -1) {
1707 err = got_error_from_errno("imsg_add TRAVERSED_COMMITS");
1711 for (i = 0; i < ncommits; i++) {
1712 struct got_object_id *id = &commit_ids[i];
1713 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
1714 err = got_error_from_errno(
1715 "imsg_add TRAVERSED_COMMITS");
1722 imsg_close(ibuf, wbuf);
1724 return flush_imsg(ibuf);
1727 const struct got_error *
1728 got_privsep_recv_traversed_commits(struct got_commit_object **changed_commit,
1729 struct got_object_id **changed_commit_id,
1730 struct got_object_id_queue *commit_ids, struct imsgbuf *ibuf)
1732 const struct got_error *err = NULL;
1734 struct got_imsg_traversed_commits *icommits;
1738 *changed_commit = NULL;
1739 *changed_commit_id = NULL;
1742 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1746 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1747 switch (imsg.hdr.type) {
1748 case GOT_IMSG_TRAVERSED_COMMITS:
1749 icommits = imsg.data;
1750 if (datalen != sizeof(*icommits) +
1751 icommits->ncommits * SHA1_DIGEST_LENGTH) {
1752 err = got_error(GOT_ERR_PRIVSEP_LEN);
1755 for (i = 0; i < icommits->ncommits; i++) {
1756 struct got_object_qid *qid;
1757 uint8_t *sha1 = (uint8_t *)imsg.data +
1758 sizeof(*icommits) + i * SHA1_DIGEST_LENGTH;
1759 err = got_object_qid_alloc_partial(&qid);
1762 memcpy(qid->id->sha1, sha1, SHA1_DIGEST_LENGTH);
1763 SIMPLEQ_INSERT_TAIL(commit_ids, qid, entry);
1765 /* The last commit may contain a change. */
1766 if (i == icommits->ncommits - 1) {
1767 *changed_commit_id =
1768 got_object_id_dup(qid->id);
1769 if (*changed_commit_id == NULL) {
1770 err = got_error_from_errno(
1771 "got_object_id_dup");
1777 case GOT_IMSG_COMMIT:
1778 if (*changed_commit_id == NULL) {
1779 err = got_error(GOT_ERR_PRIVSEP_MSG);
1782 err = get_commit_from_imsg(changed_commit, &imsg,
1785 case GOT_IMSG_COMMIT_TRAVERSAL_DONE:
1789 err = got_error(GOT_ERR_PRIVSEP_MSG);
1799 got_object_id_queue_free(commit_ids);
1803 const struct got_error *
1804 got_privsep_send_commit_traversal_done(struct imsgbuf *ibuf)
1806 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_DONE, 0, 0, -1,
1808 return got_error_from_errno("imsg_compose TRAVERSAL_DONE");
1810 return flush_imsg(ibuf);
1813 const struct got_error *
1814 got_privsep_unveil_exec_helpers(void)
1816 const char *helpers[] = {
1817 GOT_PATH_PROG_READ_PACK,
1818 GOT_PATH_PROG_READ_OBJECT,
1819 GOT_PATH_PROG_READ_COMMIT,
1820 GOT_PATH_PROG_READ_TREE,
1821 GOT_PATH_PROG_READ_BLOB,
1822 GOT_PATH_PROG_READ_TAG,
1823 GOT_PATH_PROG_READ_GITCONFIG,
1827 for (i = 0; i < nitems(helpers); i++) {
1828 if (unveil(helpers[i], "x") == 0)
1830 return got_error_from_errno2("unveil", helpers[i]);
1837 got_privsep_exec_child(int imsg_fds[2], const char *path, const char *repo_path)
1839 if (close(imsg_fds[0]) != 0) {
1840 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
1844 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
1845 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
1848 if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
1849 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
1853 if (execl(path, path, repo_path, (char *)NULL) == -1) {
1854 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,