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_progress(struct imsgbuf *ibuf,
423 struct got_object_id *refid, const char *refname)
425 const struct got_error *err = NULL;
427 size_t len, reflen = strlen(refname);
429 len = sizeof(struct got_imsg_fetch_progress) + reflen;
430 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
431 return got_error(GOT_ERR_NO_SPACE);
433 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_PROGRESS, 0, 0, len);
435 return got_error_from_errno("imsg_create FETCH_PROGRESS");
437 /* Keep in sync with struct got_imsg_fetch_progress definition! */
438 if (imsg_add(wbuf, refid->sha1, SHA1_DIGEST_LENGTH) == -1) {
439 err = got_error_from_errno("imsg_add FETCH_PROGRESS");
443 if (imsg_add(wbuf, refname, reflen) == -1) {
444 err = got_error_from_errno("imsg_add FETCH_PROGRESS");
450 imsg_close(ibuf, wbuf);
451 return flush_imsg(ibuf);
454 const struct got_error *
455 got_privsep_send_fetch_done(struct imsgbuf *ibuf, struct got_object_id hash)
457 if (imsg_compose(ibuf, GOT_IMSG_FETCH_DONE, 0, 0, -1,
458 hash.sha1, SHA1_DIGEST_LENGTH) == -1)
459 return got_error_from_errno("imsg_compose FETCH");
460 return flush_imsg(ibuf);
464 const struct got_error *
465 got_privsep_recv_fetch_progress(int *done, struct got_object_id **id,
466 char **refname, struct imsgbuf *ibuf)
468 const struct got_error *err = NULL;
471 const size_t min_datalen =
472 MIN(sizeof(struct got_imsg_error),
473 sizeof(struct got_imsg_fetch_progress));
479 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
483 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
484 switch (imsg.hdr.type) {
486 err = recv_imsg_error(&imsg, datalen);
488 case GOT_IMSG_FETCH_PROGRESS:
489 *id = malloc(sizeof(**id));
491 err = got_error_from_errno("malloc");
494 memcpy((*id)->sha1, imsg.data, SHA1_DIGEST_LENGTH);
495 if (datalen <= SHA1_DIGEST_LENGTH) {
496 err = got_error(GOT_ERR_PRIVSEP_MSG);
499 *refname = strndup(imsg.data + SHA1_DIGEST_LENGTH,
500 datalen - SHA1_DIGEST_LENGTH);
501 if (*refname == NULL) {
502 err = got_error_from_errno("strndup");
506 case GOT_IMSG_FETCH_DONE:
507 *id = malloc(sizeof(**id));
509 err = got_error_from_errno("malloc");
512 if (datalen != SHA1_DIGEST_LENGTH) {
513 err = got_error(GOT_ERR_PRIVSEP_MSG);
516 memcpy((*id)->sha1, imsg.data, SHA1_DIGEST_LENGTH);
520 err = got_error(GOT_ERR_PRIVSEP_MSG);
534 const struct got_error *
535 got_privsep_send_index_pack_req(struct imsgbuf *ibuf, int fd, struct got_object_id *hash)
537 const struct got_error *err = NULL;
539 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_REQUEST, 0, 0, fd,
540 hash->sha1, SHA1_DIGEST_LENGTH) == -1) {
541 err = got_error_from_errno("imsg_compose INDEX_REQUEST");
545 return flush_imsg(ibuf);
548 const struct got_error *
549 got_privsep_send_index_pack_done(struct imsgbuf *ibuf)
551 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_DONE, 0, 0, -1, NULL, 0) == -1)
552 return got_error_from_errno("imsg_compose FETCH");
553 return flush_imsg(ibuf);
556 const struct got_error *
557 got_privsep_wait_index_pack_done(struct imsgbuf *ibuf)
559 const struct got_error *err = NULL;
562 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
565 if (imsg.hdr.type == GOT_IMSG_IDXPACK_DONE)
568 return got_error(GOT_ERR_PRIVSEP_MSG);
572 const struct got_error *
573 got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
574 struct imsgbuf *ibuf)
576 const struct got_error *err = NULL;
577 struct got_imsg_object *iobj;
578 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
580 if (datalen != sizeof(*iobj))
581 return got_error(GOT_ERR_PRIVSEP_LEN);
584 *obj = calloc(1, sizeof(**obj));
586 return got_error_from_errno("calloc");
588 memcpy((*obj)->id.sha1, iobj->id, SHA1_DIGEST_LENGTH);
589 (*obj)->type = iobj->type;
590 (*obj)->flags = iobj->flags;
591 (*obj)->hdrlen = iobj->hdrlen;
592 (*obj)->size = iobj->size;
593 /* path_packfile is handled by caller */
594 if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
595 (*obj)->pack_offset = iobj->pack_offset;
596 (*obj)->pack_idx = iobj->pack_idx;
602 const struct got_error *
603 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
605 const struct got_error *err = NULL;
607 const size_t min_datalen =
608 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
612 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
616 switch (imsg.hdr.type) {
617 case GOT_IMSG_OBJECT:
618 err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
621 err = got_error(GOT_ERR_PRIVSEP_MSG);
630 static const struct got_error *
631 send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
634 const struct got_error *err = NULL;
635 size_t offset, remain;
640 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
642 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
643 commit->logmsg + offset, n) == -1) {
644 err = got_error_from_errno("imsg_compose "
649 err = flush_imsg(ibuf);
660 const struct got_error *
661 got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
663 const struct got_error *err = NULL;
664 struct got_imsg_commit_object *icommit;
667 struct got_object_qid *qid;
668 size_t author_len = strlen(commit->author);
669 size_t committer_len = strlen(commit->committer);
670 size_t logmsg_len = strlen(commit->logmsg);
672 total = sizeof(*icommit) + author_len + committer_len +
673 commit->nparents * SHA1_DIGEST_LENGTH;
677 return got_error_from_errno("malloc");
679 icommit = (struct got_imsg_commit_object *)buf;
680 memcpy(icommit->tree_id, commit->tree_id->sha1,
681 sizeof(icommit->tree_id));
682 icommit->author_len = author_len;
683 icommit->author_time = commit->author_time;
684 icommit->author_gmtoff = commit->author_gmtoff;
685 icommit->committer_len = committer_len;
686 icommit->committer_time = commit->committer_time;
687 icommit->committer_gmtoff = commit->committer_gmtoff;
688 icommit->logmsg_len = logmsg_len;
689 icommit->nparents = commit->nparents;
691 len = sizeof(*icommit);
692 memcpy(buf + len, commit->author, author_len);
694 memcpy(buf + len, commit->committer, committer_len);
695 len += committer_len;
696 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
697 memcpy(buf + len, qid->id, SHA1_DIGEST_LENGTH);
698 len += SHA1_DIGEST_LENGTH;
701 if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
702 err = got_error_from_errno("imsg_compose COMMIT");
706 if (logmsg_len == 0 ||
707 logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
708 err = flush_imsg(ibuf);
712 err = send_commit_logmsg(ibuf, commit, logmsg_len);
718 static const struct got_error *
719 get_commit_from_imsg(struct got_commit_object **commit,
720 struct imsg *imsg, size_t datalen, struct imsgbuf *ibuf)
722 const struct got_error *err = NULL;
723 struct got_imsg_commit_object *icommit;
727 if (datalen < sizeof(*icommit))
728 return got_error(GOT_ERR_PRIVSEP_LEN);
730 icommit = imsg->data;
731 if (datalen != sizeof(*icommit) + icommit->author_len +
732 icommit->committer_len +
733 icommit->nparents * SHA1_DIGEST_LENGTH)
734 return got_error(GOT_ERR_PRIVSEP_LEN);
736 if (icommit->nparents < 0)
737 return got_error(GOT_ERR_PRIVSEP_LEN);
739 len += sizeof(*icommit);
741 *commit = got_object_commit_alloc_partial();
743 return got_error_from_errno(
744 "got_object_commit_alloc_partial");
746 memcpy((*commit)->tree_id->sha1, icommit->tree_id,
748 (*commit)->author_time = icommit->author_time;
749 (*commit)->author_gmtoff = icommit->author_gmtoff;
750 (*commit)->committer_time = icommit->committer_time;
751 (*commit)->committer_gmtoff = icommit->committer_gmtoff;
753 if (icommit->author_len == 0) {
754 (*commit)->author = strdup("");
755 if ((*commit)->author == NULL) {
756 err = got_error_from_errno("strdup");
760 (*commit)->author = malloc(icommit->author_len + 1);
761 if ((*commit)->author == NULL) {
762 err = got_error_from_errno("malloc");
765 memcpy((*commit)->author, imsg->data + len,
766 icommit->author_len);
767 (*commit)->author[icommit->author_len] = '\0';
769 len += icommit->author_len;
771 if (icommit->committer_len == 0) {
772 (*commit)->committer = strdup("");
773 if ((*commit)->committer == NULL) {
774 err = got_error_from_errno("strdup");
778 (*commit)->committer =
779 malloc(icommit->committer_len + 1);
780 if ((*commit)->committer == NULL) {
781 err = got_error_from_errno("malloc");
784 memcpy((*commit)->committer, imsg->data + len,
785 icommit->committer_len);
786 (*commit)->committer[icommit->committer_len] = '\0';
788 len += icommit->committer_len;
790 if (icommit->logmsg_len == 0) {
791 (*commit)->logmsg = strdup("");
792 if ((*commit)->logmsg == NULL) {
793 err = got_error_from_errno("strdup");
797 size_t offset = 0, remain = icommit->logmsg_len;
799 (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
800 if ((*commit)->logmsg == NULL) {
801 err = got_error_from_errno("malloc");
805 struct imsg imsg_log;
806 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
809 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
813 if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG) {
814 err = got_error(GOT_ERR_PRIVSEP_MSG);
818 memcpy((*commit)->logmsg + offset,
820 imsg_free(&imsg_log);
824 (*commit)->logmsg[icommit->logmsg_len] = '\0';
827 for (i = 0; i < icommit->nparents; i++) {
828 struct got_object_qid *qid;
830 err = got_object_qid_alloc_partial(&qid);
833 memcpy(qid->id, imsg->data + len +
834 i * SHA1_DIGEST_LENGTH, sizeof(*qid->id));
835 SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
836 (*commit)->nparents++;
840 got_object_commit_close(*commit);
846 const struct got_error *
847 got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
849 const struct got_error *err = NULL;
852 const size_t min_datalen =
853 MIN(sizeof(struct got_imsg_error),
854 sizeof(struct got_imsg_commit_object));
858 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
862 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
864 switch (imsg.hdr.type) {
865 case GOT_IMSG_COMMIT:
866 err = get_commit_from_imsg(commit, &imsg, datalen, ibuf);
869 err = got_error(GOT_ERR_PRIVSEP_MSG);
878 const struct got_error *
879 got_privsep_send_tree(struct imsgbuf *ibuf, struct got_pathlist_head *entries,
882 const struct got_error *err = NULL;
883 struct got_imsg_tree_object itree;
884 struct got_pathlist_entry *pe;
886 int nimsg; /* number of imsg queued in ibuf */
888 itree.nentries = nentries;
889 if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
891 return got_error_from_errno("imsg_compose TREE");
893 totlen = sizeof(itree);
895 TAILQ_FOREACH(pe, entries, entry) {
896 const char *name = pe->path;
897 struct got_parsed_tree_entry *pte = pe->data;
899 size_t namelen = strlen(name);
900 size_t len = sizeof(struct got_imsg_tree_entry) + namelen;
902 if (len > MAX_IMSGSIZE)
903 return got_error(GOT_ERR_NO_SPACE);
906 if (totlen + len >= MAX_IMSGSIZE - (IMSG_HEADER_SIZE * nimsg)) {
907 err = flush_imsg(ibuf);
913 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_ENTRY, 0, 0, len);
915 return got_error_from_errno("imsg_create TREE_ENTRY");
917 /* Keep in sync with struct got_imsg_tree_object definition! */
918 if (imsg_add(wbuf, pte->id, SHA1_DIGEST_LENGTH) == -1) {
919 err = got_error_from_errno("imsg_add TREE_ENTRY");
923 if (imsg_add(wbuf, &pte->mode, sizeof(pte->mode)) == -1) {
924 err = got_error_from_errno("imsg_add TREE_ENTRY");
929 if (imsg_add(wbuf, name, namelen) == -1) {
930 err = got_error_from_errno("imsg_add TREE_ENTRY");
936 imsg_close(ibuf, wbuf);
941 return flush_imsg(ibuf);
944 const struct got_error *
945 got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
947 const struct got_error *err = NULL;
948 const size_t min_datalen =
949 MIN(sizeof(struct got_imsg_error),
950 sizeof(struct got_imsg_tree_object));
951 struct got_imsg_tree_object *itree;
956 err = read_imsg(ibuf);
964 struct got_imsg_tree_entry *ite;
965 struct got_tree_entry *te = NULL;
967 n = imsg_get(ibuf, &imsg);
969 if (*tree && (*tree)->nentries != nentries)
974 if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen)
975 return got_error(GOT_ERR_PRIVSEP_LEN);
977 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
979 switch (imsg.hdr.type) {
981 err = recv_imsg_error(&imsg, datalen);
984 /* This message should only appear once. */
986 err = got_error(GOT_ERR_PRIVSEP_MSG);
989 if (datalen != sizeof(*itree)) {
990 err = got_error(GOT_ERR_PRIVSEP_LEN);
994 *tree = malloc(sizeof(**tree));
996 err = got_error_from_errno("malloc");
999 (*tree)->entries = calloc(itree->nentries,
1000 sizeof(struct got_tree_entry));
1001 if ((*tree)->entries == NULL) {
1002 err = got_error_from_errno("malloc");
1005 (*tree)->nentries = itree->nentries;
1006 (*tree)->refcnt = 0;
1008 case GOT_IMSG_TREE_ENTRY:
1009 /* This message should be preceeded by GOT_IMSG_TREE. */
1010 if (*tree == NULL) {
1011 err = got_error(GOT_ERR_PRIVSEP_MSG);
1014 if (datalen < sizeof(*ite) || datalen > MAX_IMSGSIZE) {
1015 err = got_error(GOT_ERR_PRIVSEP_LEN);
1019 /* Remaining data contains the entry's name. */
1020 datalen -= sizeof(*ite);
1021 if (datalen == 0 || datalen > MAX_IMSGSIZE) {
1022 err = got_error(GOT_ERR_PRIVSEP_LEN);
1027 if (datalen + 1 > sizeof(te->name)) {
1028 err = got_error(GOT_ERR_NO_SPACE);
1031 te = &(*tree)->entries[nentries];
1032 memcpy(te->name, imsg.data + sizeof(*ite), datalen);
1033 te->name[datalen] = '\0';
1035 memcpy(te->id.sha1, ite->id, SHA1_DIGEST_LENGTH);
1036 te->mode = ite->mode;
1041 err = got_error(GOT_ERR_PRIVSEP_MSG);
1048 if (*tree && (*tree)->nentries != nentries) {
1050 err = got_error(GOT_ERR_PRIVSEP_LEN);
1051 got_object_tree_close(*tree);
1058 const struct got_error *
1059 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
1060 const uint8_t *data)
1062 struct got_imsg_blob iblob;
1065 iblob.hdrlen = hdrlen;
1070 if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
1071 return got_error(GOT_ERR_NO_SPACE);
1073 buf = malloc(sizeof(iblob) + size);
1075 return got_error_from_errno("malloc");
1077 memcpy(buf, &iblob, sizeof(iblob));
1078 memcpy(buf + sizeof(iblob), data, size);
1079 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
1080 sizeof(iblob) + size) == -1) {
1082 return got_error_from_errno("imsg_compose BLOB");
1086 /* Data has already been written to file descriptor. */
1087 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
1088 sizeof(iblob)) == -1)
1089 return got_error_from_errno("imsg_compose BLOB");
1093 return flush_imsg(ibuf);
1096 const struct got_error *
1097 got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1098 struct imsgbuf *ibuf)
1100 const struct got_error *err = NULL;
1102 struct got_imsg_blob *iblob;
1107 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1111 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1113 switch (imsg.hdr.type) {
1115 if (datalen < sizeof(*iblob)) {
1116 err = got_error(GOT_ERR_PRIVSEP_LEN);
1120 *size = iblob->size;
1121 *hdrlen = iblob->hdrlen;
1123 if (datalen == sizeof(*iblob)) {
1124 /* Data has been written to file descriptor. */
1128 if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX) {
1129 err = got_error(GOT_ERR_PRIVSEP_LEN);
1133 *outbuf = malloc(*size);
1134 if (*outbuf == NULL) {
1135 err = got_error_from_errno("malloc");
1138 memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
1141 err = got_error(GOT_ERR_PRIVSEP_MSG);
1150 static const struct got_error *
1151 send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
1153 const struct got_error *err = NULL;
1154 size_t offset, remain;
1157 remain = tagmsg_len;
1158 while (remain > 0) {
1159 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1161 if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
1162 tag->tagmsg + offset, n) == -1) {
1163 err = got_error_from_errno("imsg_compose TAG_TAGMSG");
1167 err = flush_imsg(ibuf);
1178 const struct got_error *
1179 got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
1181 const struct got_error *err = NULL;
1182 struct got_imsg_tag_object *itag;
1185 size_t tag_len = strlen(tag->tag);
1186 size_t tagger_len = strlen(tag->tagger);
1187 size_t tagmsg_len = strlen(tag->tagmsg);
1189 total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
1191 buf = malloc(total);
1193 return got_error_from_errno("malloc");
1195 itag = (struct got_imsg_tag_object *)buf;
1196 memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
1197 itag->obj_type = tag->obj_type;
1198 itag->tag_len = tag_len;
1199 itag->tagger_len = tagger_len;
1200 itag->tagger_time = tag->tagger_time;
1201 itag->tagger_gmtoff = tag->tagger_gmtoff;
1202 itag->tagmsg_len = tagmsg_len;
1204 len = sizeof(*itag);
1205 memcpy(buf + len, tag->tag, tag_len);
1207 memcpy(buf + len, tag->tagger, tagger_len);
1210 if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
1211 err = got_error_from_errno("imsg_compose TAG");
1215 if (tagmsg_len == 0 ||
1216 tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1217 err = flush_imsg(ibuf);
1221 err = send_tagmsg(ibuf, tag, tagmsg_len);
1227 const struct got_error *
1228 got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
1230 const struct got_error *err = NULL;
1232 struct got_imsg_tag_object *itag;
1233 size_t len, datalen;
1234 const size_t min_datalen =
1235 MIN(sizeof(struct got_imsg_error),
1236 sizeof(struct got_imsg_tag_object));
1240 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1244 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1247 switch (imsg.hdr.type) {
1249 if (datalen < sizeof(*itag)) {
1250 err = got_error(GOT_ERR_PRIVSEP_LEN);
1254 if (datalen != sizeof(*itag) + itag->tag_len +
1256 err = got_error(GOT_ERR_PRIVSEP_LEN);
1259 len += sizeof(*itag);
1261 *tag = calloc(1, sizeof(**tag));
1263 err = got_error_from_errno("calloc");
1267 memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
1269 if (itag->tag_len == 0) {
1270 (*tag)->tag = strdup("");
1271 if ((*tag)->tag == NULL) {
1272 err = got_error_from_errno("strdup");
1276 (*tag)->tag = malloc(itag->tag_len + 1);
1277 if ((*tag)->tag == NULL) {
1278 err = got_error_from_errno("malloc");
1281 memcpy((*tag)->tag, imsg.data + len,
1283 (*tag)->tag[itag->tag_len] = '\0';
1285 len += itag->tag_len;
1287 (*tag)->obj_type = itag->obj_type;
1288 (*tag)->tagger_time = itag->tagger_time;
1289 (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1291 if (itag->tagger_len == 0) {
1292 (*tag)->tagger = strdup("");
1293 if ((*tag)->tagger == NULL) {
1294 err = got_error_from_errno("strdup");
1298 (*tag)->tagger = malloc(itag->tagger_len + 1);
1299 if ((*tag)->tagger == NULL) {
1300 err = got_error_from_errno("malloc");
1303 memcpy((*tag)->tagger, imsg.data + len,
1305 (*tag)->tagger[itag->tagger_len] = '\0';
1307 len += itag->tagger_len;
1309 if (itag->tagmsg_len == 0) {
1310 (*tag)->tagmsg = strdup("");
1311 if ((*tag)->tagmsg == NULL) {
1312 err = got_error_from_errno("strdup");
1316 size_t offset = 0, remain = itag->tagmsg_len;
1318 (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1319 if ((*tag)->tagmsg == NULL) {
1320 err = got_error_from_errno("malloc");
1323 while (remain > 0) {
1324 struct imsg imsg_log;
1325 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1328 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1332 if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1333 return got_error(GOT_ERR_PRIVSEP_MSG);
1335 memcpy((*tag)->tagmsg + offset, imsg_log.data,
1337 imsg_free(&imsg_log);
1341 (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1346 err = got_error(GOT_ERR_PRIVSEP_MSG);
1355 const struct got_error *
1356 got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1357 struct got_packidx *packidx)
1359 const struct got_error *err = NULL;
1360 struct got_imsg_packidx ipackidx;
1361 struct got_imsg_pack ipack;
1364 ipackidx.len = packidx->len;
1365 fd = dup(packidx->fd);
1367 return got_error_from_errno("dup");
1369 if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
1370 sizeof(ipackidx)) == -1) {
1371 err = got_error_from_errno("imsg_compose PACKIDX");
1376 if (strlcpy(ipack.path_packfile, pack->path_packfile,
1377 sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
1378 return got_error(GOT_ERR_NO_SPACE);
1379 ipack.filesize = pack->filesize;
1383 return got_error_from_errno("dup");
1385 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
1387 err = got_error_from_errno("imsg_compose PACK");
1392 return flush_imsg(ibuf);
1395 const struct got_error *
1396 got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
1397 struct got_object_id *id)
1399 struct got_imsg_packed_object iobj;
1402 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
1404 if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
1405 &iobj, sizeof(iobj)) == -1)
1406 return got_error_from_errno("imsg_compose "
1407 "PACKED_OBJECT_REQUEST");
1409 return flush_imsg(ibuf);
1412 const struct got_error *
1413 got_privsep_send_gitconfig_parse_req(struct imsgbuf *ibuf, int fd)
1415 const struct got_error *err = NULL;
1417 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_PARSE_REQUEST, 0, 0, fd,
1419 err = got_error_from_errno("imsg_compose "
1420 "GITCONFIG_PARSE_REQUEST");
1425 return flush_imsg(ibuf);
1428 const struct got_error *
1429 got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *ibuf)
1431 if (imsg_compose(ibuf,
1432 GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST, 0, 0, -1,
1434 return got_error_from_errno("imsg_compose "
1435 "GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST");
1437 return flush_imsg(ibuf);
1440 const struct got_error *
1441 got_privsep_send_gitconfig_author_name_req(struct imsgbuf *ibuf)
1443 if (imsg_compose(ibuf,
1444 GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST, 0, 0, -1, NULL, 0) == -1)
1445 return got_error_from_errno("imsg_compose "
1446 "GITCONFIG_AUTHOR_NAME_REQUEST");
1448 return flush_imsg(ibuf);
1451 const struct got_error *
1452 got_privsep_send_gitconfig_author_email_req(struct imsgbuf *ibuf)
1454 if (imsg_compose(ibuf,
1455 GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST, 0, 0, -1, NULL, 0) == -1)
1456 return got_error_from_errno("imsg_compose "
1457 "GITCONFIG_AUTHOR_EMAIL_REQUEST");
1459 return flush_imsg(ibuf);
1462 const struct got_error *
1463 got_privsep_send_gitconfig_remotes_req(struct imsgbuf *ibuf)
1465 if (imsg_compose(ibuf,
1466 GOT_IMSG_GITCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
1467 return got_error_from_errno("imsg_compose "
1468 "GITCONFIG_REMOTE_REQUEST");
1470 return flush_imsg(ibuf);
1473 const struct got_error *
1474 got_privsep_send_gitconfig_owner_req(struct imsgbuf *ibuf)
1476 if (imsg_compose(ibuf,
1477 GOT_IMSG_GITCONFIG_OWNER_REQUEST, 0, 0, -1, NULL, 0) == -1)
1478 return got_error_from_errno("imsg_compose "
1479 "GITCONFIG_OWNER_REQUEST");
1481 return flush_imsg(ibuf);
1484 const struct got_error *
1485 got_privsep_send_gitconfig_str(struct imsgbuf *ibuf, const char *value)
1487 size_t len = value ? strlen(value) + 1 : 0;
1489 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_STR_VAL, 0, 0, -1,
1491 return got_error_from_errno("imsg_compose GITCONFIG_STR_VAL");
1493 return flush_imsg(ibuf);
1496 const struct got_error *
1497 got_privsep_recv_gitconfig_str(char **str, struct imsgbuf *ibuf)
1499 const struct got_error *err = NULL;
1502 const size_t min_datalen = 0;
1506 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1509 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1511 switch (imsg.hdr.type) {
1512 case GOT_IMSG_GITCONFIG_STR_VAL:
1515 *str = malloc(datalen);
1517 err = got_error_from_errno("malloc");
1520 if (strlcpy(*str, imsg.data, datalen) >= datalen)
1521 err = got_error(GOT_ERR_NO_SPACE);
1524 err = got_error(GOT_ERR_PRIVSEP_MSG);
1532 const struct got_error *
1533 got_privsep_send_gitconfig_int(struct imsgbuf *ibuf, int value)
1535 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_INT_VAL, 0, 0, -1,
1536 &value, sizeof(value)) == -1)
1537 return got_error_from_errno("imsg_compose GITCONFIG_INT_VAL");
1539 return flush_imsg(ibuf);
1542 const struct got_error *
1543 got_privsep_recv_gitconfig_int(int *val, struct imsgbuf *ibuf)
1545 const struct got_error *err = NULL;
1548 const size_t min_datalen =
1549 MIN(sizeof(struct got_imsg_error), sizeof(int));
1553 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1556 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1558 switch (imsg.hdr.type) {
1559 case GOT_IMSG_GITCONFIG_INT_VAL:
1560 if (datalen != sizeof(*val)) {
1561 err = got_error(GOT_ERR_PRIVSEP_LEN);
1564 memcpy(val, imsg.data, sizeof(*val));
1567 err = got_error(GOT_ERR_PRIVSEP_MSG);
1575 const struct got_error *
1576 got_privsep_send_gitconfig_remotes(struct imsgbuf *ibuf,
1577 struct got_remote_repo *remotes, int nremotes)
1579 const struct got_error *err = NULL;
1580 struct got_imsg_remotes iremotes;
1583 iremotes.nremotes = nremotes;
1584 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_REMOTES, 0, 0, -1,
1585 &iremotes, sizeof(iremotes)) == -1)
1586 return got_error_from_errno("imsg_compose GITCONFIG_REMOTES");
1588 err = flush_imsg(ibuf);
1593 for (i = 0; i < nremotes; i++) {
1594 struct got_imsg_remote iremote;
1595 size_t len = sizeof(iremote);
1598 iremote.name_len = strlen(remotes[i].name);
1599 len += iremote.name_len;
1600 iremote.url_len = strlen(remotes[i].url);
1601 len += iremote.url_len;
1603 wbuf = imsg_create(ibuf, GOT_IMSG_GITCONFIG_REMOTE, 0, 0, len);
1605 return got_error_from_errno(
1606 "imsg_create GITCONFIG_REMOTE");
1608 if (imsg_add(wbuf, &iremote, sizeof(iremote)) == -1) {
1609 err = got_error_from_errno(
1610 "imsg_add GIITCONFIG_REMOTE");
1615 if (imsg_add(wbuf, remotes[i].name, iremote.name_len) == -1) {
1616 err = got_error_from_errno(
1617 "imsg_add GIITCONFIG_REMOTE");
1621 if (imsg_add(wbuf, remotes[i].url, iremote.url_len) == -1) {
1622 err = got_error_from_errno(
1623 "imsg_add GIITCONFIG_REMOTE");
1629 imsg_close(ibuf, wbuf);
1630 err = flush_imsg(ibuf);
1638 const struct got_error *
1639 got_privsep_recv_gitconfig_remotes(struct got_remote_repo **remotes,
1640 int *nremotes, struct imsgbuf *ibuf)
1642 const struct got_error *err = NULL;
1645 struct got_imsg_remotes iremotes;
1646 struct got_imsg_remote iremote;
1650 iremotes.nremotes = 0;
1652 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremotes));
1655 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1657 switch (imsg.hdr.type) {
1658 case GOT_IMSG_GITCONFIG_REMOTES:
1659 if (datalen != sizeof(iremotes)) {
1660 err = got_error(GOT_ERR_PRIVSEP_LEN);
1663 memcpy(&iremotes, imsg.data, sizeof(iremotes));
1664 if (iremotes.nremotes == 0) {
1671 return got_error(GOT_ERR_PRIVSEP_MSG);
1676 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(iremote));
1677 if (*remotes == NULL)
1678 return got_error_from_errno("recallocarray");
1680 while (*nremotes < iremotes.nremotes) {
1681 struct got_remote_repo *remote;
1683 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremote));
1686 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1688 switch (imsg.hdr.type) {
1689 case GOT_IMSG_GITCONFIG_REMOTE:
1690 remote = &(*remotes)[*nremotes];
1691 if (datalen < sizeof(iremote)) {
1692 err = got_error(GOT_ERR_PRIVSEP_LEN);
1695 memcpy(&iremote, imsg.data, sizeof(iremote));
1696 if (iremote.name_len == 0 || iremote.url_len == 0 ||
1697 (sizeof(iremote) + iremote.name_len +
1698 iremote.url_len) > datalen) {
1699 err = got_error(GOT_ERR_PRIVSEP_LEN);
1702 remote->name = strndup(imsg.data + sizeof(iremote),
1704 if (remote->name == NULL) {
1705 err = got_error_from_errno("strndup");
1708 remote->url = strndup(imsg.data + sizeof(iremote) +
1709 iremote.name_len, iremote.url_len);
1710 if (remote->url == NULL) {
1711 err = got_error_from_errno("strndup");
1718 err = got_error(GOT_ERR_PRIVSEP_MSG);
1729 for (i = 0; i < *nremotes; i++) {
1730 free((*remotes)[i].name);
1731 free((*remotes)[i].url);
1740 const struct got_error *
1741 got_privsep_send_commit_traversal_request(struct imsgbuf *ibuf,
1742 struct got_object_id *id, int idx, const char *path)
1744 const struct got_error *err = NULL;
1746 size_t path_len = strlen(path) + 1;
1748 wbuf = imsg_create(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_REQUEST, 0, 0,
1749 sizeof(struct got_imsg_commit_traversal_request) + path_len);
1751 return got_error_from_errno(
1752 "imsg_create COMMIT_TRAVERSAL_REQUEST");
1753 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
1754 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
1758 if (imsg_add(wbuf, &idx, sizeof(idx)) == -1) {
1759 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
1763 if (imsg_add(wbuf, path, path_len) == -1) {
1764 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
1770 imsg_close(ibuf, wbuf);
1772 return flush_imsg(ibuf);
1775 const struct got_error *
1776 got_privsep_send_traversed_commits(struct got_object_id *commit_ids,
1777 size_t ncommits, struct imsgbuf *ibuf)
1779 const struct got_error *err;
1783 wbuf = imsg_create(ibuf, GOT_IMSG_TRAVERSED_COMMITS, 0, 0,
1784 sizeof(struct got_imsg_traversed_commits) +
1785 ncommits * SHA1_DIGEST_LENGTH);
1787 return got_error_from_errno("imsg_create TRAVERSED_COMMITS");
1789 if (imsg_add(wbuf, &ncommits, sizeof(ncommits)) == -1) {
1790 err = got_error_from_errno("imsg_add TRAVERSED_COMMITS");
1794 for (i = 0; i < ncommits; i++) {
1795 struct got_object_id *id = &commit_ids[i];
1796 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
1797 err = got_error_from_errno(
1798 "imsg_add TRAVERSED_COMMITS");
1805 imsg_close(ibuf, wbuf);
1807 return flush_imsg(ibuf);
1810 const struct got_error *
1811 got_privsep_recv_traversed_commits(struct got_commit_object **changed_commit,
1812 struct got_object_id **changed_commit_id,
1813 struct got_object_id_queue *commit_ids, struct imsgbuf *ibuf)
1815 const struct got_error *err = NULL;
1817 struct got_imsg_traversed_commits *icommits;
1821 *changed_commit = NULL;
1822 *changed_commit_id = NULL;
1825 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1829 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1830 switch (imsg.hdr.type) {
1831 case GOT_IMSG_TRAVERSED_COMMITS:
1832 icommits = imsg.data;
1833 if (datalen != sizeof(*icommits) +
1834 icommits->ncommits * SHA1_DIGEST_LENGTH) {
1835 err = got_error(GOT_ERR_PRIVSEP_LEN);
1838 for (i = 0; i < icommits->ncommits; i++) {
1839 struct got_object_qid *qid;
1840 uint8_t *sha1 = (uint8_t *)imsg.data +
1841 sizeof(*icommits) + i * SHA1_DIGEST_LENGTH;
1842 err = got_object_qid_alloc_partial(&qid);
1845 memcpy(qid->id->sha1, sha1, SHA1_DIGEST_LENGTH);
1846 SIMPLEQ_INSERT_TAIL(commit_ids, qid, entry);
1848 /* The last commit may contain a change. */
1849 if (i == icommits->ncommits - 1) {
1850 *changed_commit_id =
1851 got_object_id_dup(qid->id);
1852 if (*changed_commit_id == NULL) {
1853 err = got_error_from_errno(
1854 "got_object_id_dup");
1860 case GOT_IMSG_COMMIT:
1861 if (*changed_commit_id == NULL) {
1862 err = got_error(GOT_ERR_PRIVSEP_MSG);
1865 err = get_commit_from_imsg(changed_commit, &imsg,
1868 case GOT_IMSG_COMMIT_TRAVERSAL_DONE:
1872 err = got_error(GOT_ERR_PRIVSEP_MSG);
1882 got_object_id_queue_free(commit_ids);
1886 const struct got_error *
1887 got_privsep_send_commit_traversal_done(struct imsgbuf *ibuf)
1889 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_DONE, 0, 0, -1,
1891 return got_error_from_errno("imsg_compose TRAVERSAL_DONE");
1893 return flush_imsg(ibuf);
1896 const struct got_error *
1897 got_privsep_unveil_exec_helpers(void)
1899 const char *helpers[] = {
1900 GOT_PATH_PROG_READ_PACK,
1901 GOT_PATH_PROG_READ_OBJECT,
1902 GOT_PATH_PROG_READ_COMMIT,
1903 GOT_PATH_PROG_READ_TREE,
1904 GOT_PATH_PROG_READ_BLOB,
1905 GOT_PATH_PROG_READ_TAG,
1906 GOT_PATH_PROG_READ_GITCONFIG,
1910 for (i = 0; i < nitems(helpers); i++) {
1911 if (unveil(helpers[i], "x") == 0)
1913 return got_error_from_errno2("unveil", helpers[i]);
1920 got_privsep_exec_child(int imsg_fds[2], const char *path, const char *repo_path)
1922 if (close(imsg_fds[0]) != 0) {
1923 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
1927 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
1928 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
1931 if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
1932 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
1936 if (execl(path, path, repo_path, (char *)NULL) == -1) {
1937 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,