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(struct got_object_id **refid,
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));
478 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
482 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
483 switch (imsg.hdr.type) {
485 err = recv_imsg_error(&imsg, datalen);
487 case GOT_IMSG_FETCH_PROGRESS:
488 *refid = malloc(sizeof(**refid));
489 if (*refid == NULL) {
490 err = got_error_from_errno("malloc");
493 memcpy((*refid)->sha1, imsg.data, SHA1_DIGEST_LENGTH);
494 if (datalen <= SHA1_DIGEST_LENGTH) {
495 err = got_error(GOT_ERR_PRIVSEP_MSG);
498 *refname = strndup(imsg.data + SHA1_DIGEST_LENGTH,
499 datalen - SHA1_DIGEST_LENGTH);
500 if (*refname == NULL) {
501 err = got_error_from_errno("strndup");
506 err = got_error(GOT_ERR_PRIVSEP_MSG);
520 const struct got_error *
521 got_privsep_wait_fetch_done(struct imsgbuf *ibuf, struct got_object_id *hash)
523 const struct got_error *err = NULL;
526 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
529 if (imsg.hdr.type == GOT_IMSG_FETCH_DONE &&
530 imsg.hdr.len - sizeof(imsg.hdr) == SHA1_DIGEST_LENGTH) {
531 memcpy(hash->sha1, imsg.data, SHA1_DIGEST_LENGTH);
537 return got_error(GOT_ERR_PRIVSEP_MSG);
541 const struct got_error *
542 got_privsep_send_index_pack_req(struct imsgbuf *ibuf, int fd, struct got_object_id *hash)
544 const struct got_error *err = NULL;
546 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_REQUEST, 0, 0, fd,
547 hash->sha1, SHA1_DIGEST_LENGTH) == -1) {
548 err = got_error_from_errno("imsg_compose INDEX_REQUEST");
552 return flush_imsg(ibuf);
555 const struct got_error *
556 got_privsep_send_index_pack_done(struct imsgbuf *ibuf)
558 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_DONE, 0, 0, -1, NULL, 0) == -1)
559 return got_error_from_errno("imsg_compose FETCH");
560 return flush_imsg(ibuf);
563 const struct got_error *
564 got_privsep_wait_index_pack_done(struct imsgbuf *ibuf)
566 const struct got_error *err = NULL;
569 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
572 if (imsg.hdr.type == GOT_IMSG_IDXPACK_DONE)
575 return got_error(GOT_ERR_PRIVSEP_MSG);
579 const struct got_error *
580 got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
581 struct imsgbuf *ibuf)
583 const struct got_error *err = NULL;
584 struct got_imsg_object *iobj;
585 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
587 if (datalen != sizeof(*iobj))
588 return got_error(GOT_ERR_PRIVSEP_LEN);
591 *obj = calloc(1, sizeof(**obj));
593 return got_error_from_errno("calloc");
595 memcpy((*obj)->id.sha1, iobj->id, SHA1_DIGEST_LENGTH);
596 (*obj)->type = iobj->type;
597 (*obj)->flags = iobj->flags;
598 (*obj)->hdrlen = iobj->hdrlen;
599 (*obj)->size = iobj->size;
600 /* path_packfile is handled by caller */
601 if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
602 (*obj)->pack_offset = iobj->pack_offset;
603 (*obj)->pack_idx = iobj->pack_idx;
609 const struct got_error *
610 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
612 const struct got_error *err = NULL;
614 const size_t min_datalen =
615 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
619 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
623 switch (imsg.hdr.type) {
624 case GOT_IMSG_OBJECT:
625 err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
628 err = got_error(GOT_ERR_PRIVSEP_MSG);
637 static const struct got_error *
638 send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
641 const struct got_error *err = NULL;
642 size_t offset, remain;
647 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
649 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
650 commit->logmsg + offset, n) == -1) {
651 err = got_error_from_errno("imsg_compose "
656 err = flush_imsg(ibuf);
667 const struct got_error *
668 got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
670 const struct got_error *err = NULL;
671 struct got_imsg_commit_object *icommit;
674 struct got_object_qid *qid;
675 size_t author_len = strlen(commit->author);
676 size_t committer_len = strlen(commit->committer);
677 size_t logmsg_len = strlen(commit->logmsg);
679 total = sizeof(*icommit) + author_len + committer_len +
680 commit->nparents * SHA1_DIGEST_LENGTH;
684 return got_error_from_errno("malloc");
686 icommit = (struct got_imsg_commit_object *)buf;
687 memcpy(icommit->tree_id, commit->tree_id->sha1,
688 sizeof(icommit->tree_id));
689 icommit->author_len = author_len;
690 icommit->author_time = commit->author_time;
691 icommit->author_gmtoff = commit->author_gmtoff;
692 icommit->committer_len = committer_len;
693 icommit->committer_time = commit->committer_time;
694 icommit->committer_gmtoff = commit->committer_gmtoff;
695 icommit->logmsg_len = logmsg_len;
696 icommit->nparents = commit->nparents;
698 len = sizeof(*icommit);
699 memcpy(buf + len, commit->author, author_len);
701 memcpy(buf + len, commit->committer, committer_len);
702 len += committer_len;
703 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
704 memcpy(buf + len, qid->id, SHA1_DIGEST_LENGTH);
705 len += SHA1_DIGEST_LENGTH;
708 if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
709 err = got_error_from_errno("imsg_compose COMMIT");
713 if (logmsg_len == 0 ||
714 logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
715 err = flush_imsg(ibuf);
719 err = send_commit_logmsg(ibuf, commit, logmsg_len);
725 static const struct got_error *
726 get_commit_from_imsg(struct got_commit_object **commit,
727 struct imsg *imsg, size_t datalen, struct imsgbuf *ibuf)
729 const struct got_error *err = NULL;
730 struct got_imsg_commit_object *icommit;
734 if (datalen < sizeof(*icommit))
735 return got_error(GOT_ERR_PRIVSEP_LEN);
737 icommit = imsg->data;
738 if (datalen != sizeof(*icommit) + icommit->author_len +
739 icommit->committer_len +
740 icommit->nparents * SHA1_DIGEST_LENGTH)
741 return got_error(GOT_ERR_PRIVSEP_LEN);
743 if (icommit->nparents < 0)
744 return got_error(GOT_ERR_PRIVSEP_LEN);
746 len += sizeof(*icommit);
748 *commit = got_object_commit_alloc_partial();
750 return got_error_from_errno(
751 "got_object_commit_alloc_partial");
753 memcpy((*commit)->tree_id->sha1, icommit->tree_id,
755 (*commit)->author_time = icommit->author_time;
756 (*commit)->author_gmtoff = icommit->author_gmtoff;
757 (*commit)->committer_time = icommit->committer_time;
758 (*commit)->committer_gmtoff = icommit->committer_gmtoff;
760 if (icommit->author_len == 0) {
761 (*commit)->author = strdup("");
762 if ((*commit)->author == NULL) {
763 err = got_error_from_errno("strdup");
767 (*commit)->author = malloc(icommit->author_len + 1);
768 if ((*commit)->author == NULL) {
769 err = got_error_from_errno("malloc");
772 memcpy((*commit)->author, imsg->data + len,
773 icommit->author_len);
774 (*commit)->author[icommit->author_len] = '\0';
776 len += icommit->author_len;
778 if (icommit->committer_len == 0) {
779 (*commit)->committer = strdup("");
780 if ((*commit)->committer == NULL) {
781 err = got_error_from_errno("strdup");
785 (*commit)->committer =
786 malloc(icommit->committer_len + 1);
787 if ((*commit)->committer == NULL) {
788 err = got_error_from_errno("malloc");
791 memcpy((*commit)->committer, imsg->data + len,
792 icommit->committer_len);
793 (*commit)->committer[icommit->committer_len] = '\0';
795 len += icommit->committer_len;
797 if (icommit->logmsg_len == 0) {
798 (*commit)->logmsg = strdup("");
799 if ((*commit)->logmsg == NULL) {
800 err = got_error_from_errno("strdup");
804 size_t offset = 0, remain = icommit->logmsg_len;
806 (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
807 if ((*commit)->logmsg == NULL) {
808 err = got_error_from_errno("malloc");
812 struct imsg imsg_log;
813 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
816 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
820 if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG) {
821 err = got_error(GOT_ERR_PRIVSEP_MSG);
825 memcpy((*commit)->logmsg + offset,
827 imsg_free(&imsg_log);
831 (*commit)->logmsg[icommit->logmsg_len] = '\0';
834 for (i = 0; i < icommit->nparents; i++) {
835 struct got_object_qid *qid;
837 err = got_object_qid_alloc_partial(&qid);
840 memcpy(qid->id, imsg->data + len +
841 i * SHA1_DIGEST_LENGTH, sizeof(*qid->id));
842 SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
843 (*commit)->nparents++;
847 got_object_commit_close(*commit);
853 const struct got_error *
854 got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
856 const struct got_error *err = NULL;
859 const size_t min_datalen =
860 MIN(sizeof(struct got_imsg_error),
861 sizeof(struct got_imsg_commit_object));
865 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
869 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
871 switch (imsg.hdr.type) {
872 case GOT_IMSG_COMMIT:
873 err = get_commit_from_imsg(commit, &imsg, datalen, ibuf);
876 err = got_error(GOT_ERR_PRIVSEP_MSG);
885 const struct got_error *
886 got_privsep_send_tree(struct imsgbuf *ibuf, struct got_pathlist_head *entries,
889 const struct got_error *err = NULL;
890 struct got_imsg_tree_object itree;
891 struct got_pathlist_entry *pe;
893 int nimsg; /* number of imsg queued in ibuf */
895 itree.nentries = nentries;
896 if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
898 return got_error_from_errno("imsg_compose TREE");
900 totlen = sizeof(itree);
902 TAILQ_FOREACH(pe, entries, entry) {
903 const char *name = pe->path;
904 struct got_parsed_tree_entry *pte = pe->data;
906 size_t namelen = strlen(name);
907 size_t len = sizeof(struct got_imsg_tree_entry) + namelen;
909 if (len > MAX_IMSGSIZE)
910 return got_error(GOT_ERR_NO_SPACE);
913 if (totlen + len >= MAX_IMSGSIZE - (IMSG_HEADER_SIZE * nimsg)) {
914 err = flush_imsg(ibuf);
920 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_ENTRY, 0, 0, len);
922 return got_error_from_errno("imsg_create TREE_ENTRY");
924 /* Keep in sync with struct got_imsg_tree_object definition! */
925 if (imsg_add(wbuf, pte->id, SHA1_DIGEST_LENGTH) == -1) {
926 err = got_error_from_errno("imsg_add TREE_ENTRY");
930 if (imsg_add(wbuf, &pte->mode, sizeof(pte->mode)) == -1) {
931 err = got_error_from_errno("imsg_add TREE_ENTRY");
936 if (imsg_add(wbuf, name, namelen) == -1) {
937 err = got_error_from_errno("imsg_add TREE_ENTRY");
943 imsg_close(ibuf, wbuf);
948 return flush_imsg(ibuf);
951 const struct got_error *
952 got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
954 const struct got_error *err = NULL;
955 const size_t min_datalen =
956 MIN(sizeof(struct got_imsg_error),
957 sizeof(struct got_imsg_tree_object));
958 struct got_imsg_tree_object *itree;
963 err = read_imsg(ibuf);
971 struct got_imsg_tree_entry *ite;
972 struct got_tree_entry *te = NULL;
974 n = imsg_get(ibuf, &imsg);
976 if (*tree && (*tree)->nentries != nentries)
981 if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen)
982 return got_error(GOT_ERR_PRIVSEP_LEN);
984 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
986 switch (imsg.hdr.type) {
988 err = recv_imsg_error(&imsg, datalen);
991 /* This message should only appear once. */
993 err = got_error(GOT_ERR_PRIVSEP_MSG);
996 if (datalen != sizeof(*itree)) {
997 err = got_error(GOT_ERR_PRIVSEP_LEN);
1001 *tree = malloc(sizeof(**tree));
1002 if (*tree == NULL) {
1003 err = got_error_from_errno("malloc");
1006 (*tree)->entries = calloc(itree->nentries,
1007 sizeof(struct got_tree_entry));
1008 if ((*tree)->entries == NULL) {
1009 err = got_error_from_errno("malloc");
1012 (*tree)->nentries = itree->nentries;
1013 (*tree)->refcnt = 0;
1015 case GOT_IMSG_TREE_ENTRY:
1016 /* This message should be preceeded by GOT_IMSG_TREE. */
1017 if (*tree == NULL) {
1018 err = got_error(GOT_ERR_PRIVSEP_MSG);
1021 if (datalen < sizeof(*ite) || datalen > MAX_IMSGSIZE) {
1022 err = got_error(GOT_ERR_PRIVSEP_LEN);
1026 /* Remaining data contains the entry's name. */
1027 datalen -= sizeof(*ite);
1028 if (datalen == 0 || datalen > MAX_IMSGSIZE) {
1029 err = got_error(GOT_ERR_PRIVSEP_LEN);
1034 if (datalen + 1 > sizeof(te->name)) {
1035 err = got_error(GOT_ERR_NO_SPACE);
1038 te = &(*tree)->entries[nentries];
1039 memcpy(te->name, imsg.data + sizeof(*ite), datalen);
1040 te->name[datalen] = '\0';
1042 memcpy(te->id.sha1, ite->id, SHA1_DIGEST_LENGTH);
1043 te->mode = ite->mode;
1048 err = got_error(GOT_ERR_PRIVSEP_MSG);
1055 if (*tree && (*tree)->nentries != nentries) {
1057 err = got_error(GOT_ERR_PRIVSEP_LEN);
1058 got_object_tree_close(*tree);
1065 const struct got_error *
1066 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
1067 const uint8_t *data)
1069 struct got_imsg_blob iblob;
1072 iblob.hdrlen = hdrlen;
1077 if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
1078 return got_error(GOT_ERR_NO_SPACE);
1080 buf = malloc(sizeof(iblob) + size);
1082 return got_error_from_errno("malloc");
1084 memcpy(buf, &iblob, sizeof(iblob));
1085 memcpy(buf + sizeof(iblob), data, size);
1086 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
1087 sizeof(iblob) + size) == -1) {
1089 return got_error_from_errno("imsg_compose BLOB");
1093 /* Data has already been written to file descriptor. */
1094 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
1095 sizeof(iblob)) == -1)
1096 return got_error_from_errno("imsg_compose BLOB");
1100 return flush_imsg(ibuf);
1103 const struct got_error *
1104 got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1105 struct imsgbuf *ibuf)
1107 const struct got_error *err = NULL;
1109 struct got_imsg_blob *iblob;
1114 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1118 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1120 switch (imsg.hdr.type) {
1122 if (datalen < sizeof(*iblob)) {
1123 err = got_error(GOT_ERR_PRIVSEP_LEN);
1127 *size = iblob->size;
1128 *hdrlen = iblob->hdrlen;
1130 if (datalen == sizeof(*iblob)) {
1131 /* Data has been written to file descriptor. */
1135 if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX) {
1136 err = got_error(GOT_ERR_PRIVSEP_LEN);
1140 *outbuf = malloc(*size);
1141 if (*outbuf == NULL) {
1142 err = got_error_from_errno("malloc");
1145 memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
1148 err = got_error(GOT_ERR_PRIVSEP_MSG);
1157 static const struct got_error *
1158 send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
1160 const struct got_error *err = NULL;
1161 size_t offset, remain;
1164 remain = tagmsg_len;
1165 while (remain > 0) {
1166 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1168 if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
1169 tag->tagmsg + offset, n) == -1) {
1170 err = got_error_from_errno("imsg_compose TAG_TAGMSG");
1174 err = flush_imsg(ibuf);
1185 const struct got_error *
1186 got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
1188 const struct got_error *err = NULL;
1189 struct got_imsg_tag_object *itag;
1192 size_t tag_len = strlen(tag->tag);
1193 size_t tagger_len = strlen(tag->tagger);
1194 size_t tagmsg_len = strlen(tag->tagmsg);
1196 total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
1198 buf = malloc(total);
1200 return got_error_from_errno("malloc");
1202 itag = (struct got_imsg_tag_object *)buf;
1203 memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
1204 itag->obj_type = tag->obj_type;
1205 itag->tag_len = tag_len;
1206 itag->tagger_len = tagger_len;
1207 itag->tagger_time = tag->tagger_time;
1208 itag->tagger_gmtoff = tag->tagger_gmtoff;
1209 itag->tagmsg_len = tagmsg_len;
1211 len = sizeof(*itag);
1212 memcpy(buf + len, tag->tag, tag_len);
1214 memcpy(buf + len, tag->tagger, tagger_len);
1217 if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
1218 err = got_error_from_errno("imsg_compose TAG");
1222 if (tagmsg_len == 0 ||
1223 tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1224 err = flush_imsg(ibuf);
1228 err = send_tagmsg(ibuf, tag, tagmsg_len);
1234 const struct got_error *
1235 got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
1237 const struct got_error *err = NULL;
1239 struct got_imsg_tag_object *itag;
1240 size_t len, datalen;
1241 const size_t min_datalen =
1242 MIN(sizeof(struct got_imsg_error),
1243 sizeof(struct got_imsg_tag_object));
1247 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1251 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1254 switch (imsg.hdr.type) {
1256 if (datalen < sizeof(*itag)) {
1257 err = got_error(GOT_ERR_PRIVSEP_LEN);
1261 if (datalen != sizeof(*itag) + itag->tag_len +
1263 err = got_error(GOT_ERR_PRIVSEP_LEN);
1266 len += sizeof(*itag);
1268 *tag = calloc(1, sizeof(**tag));
1270 err = got_error_from_errno("calloc");
1274 memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
1276 if (itag->tag_len == 0) {
1277 (*tag)->tag = strdup("");
1278 if ((*tag)->tag == NULL) {
1279 err = got_error_from_errno("strdup");
1283 (*tag)->tag = malloc(itag->tag_len + 1);
1284 if ((*tag)->tag == NULL) {
1285 err = got_error_from_errno("malloc");
1288 memcpy((*tag)->tag, imsg.data + len,
1290 (*tag)->tag[itag->tag_len] = '\0';
1292 len += itag->tag_len;
1294 (*tag)->obj_type = itag->obj_type;
1295 (*tag)->tagger_time = itag->tagger_time;
1296 (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1298 if (itag->tagger_len == 0) {
1299 (*tag)->tagger = strdup("");
1300 if ((*tag)->tagger == NULL) {
1301 err = got_error_from_errno("strdup");
1305 (*tag)->tagger = malloc(itag->tagger_len + 1);
1306 if ((*tag)->tagger == NULL) {
1307 err = got_error_from_errno("malloc");
1310 memcpy((*tag)->tagger, imsg.data + len,
1312 (*tag)->tagger[itag->tagger_len] = '\0';
1314 len += itag->tagger_len;
1316 if (itag->tagmsg_len == 0) {
1317 (*tag)->tagmsg = strdup("");
1318 if ((*tag)->tagmsg == NULL) {
1319 err = got_error_from_errno("strdup");
1323 size_t offset = 0, remain = itag->tagmsg_len;
1325 (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1326 if ((*tag)->tagmsg == NULL) {
1327 err = got_error_from_errno("malloc");
1330 while (remain > 0) {
1331 struct imsg imsg_log;
1332 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1335 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1339 if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1340 return got_error(GOT_ERR_PRIVSEP_MSG);
1342 memcpy((*tag)->tagmsg + offset, imsg_log.data,
1344 imsg_free(&imsg_log);
1348 (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1353 err = got_error(GOT_ERR_PRIVSEP_MSG);
1362 const struct got_error *
1363 got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1364 struct got_packidx *packidx)
1366 const struct got_error *err = NULL;
1367 struct got_imsg_packidx ipackidx;
1368 struct got_imsg_pack ipack;
1371 ipackidx.len = packidx->len;
1372 fd = dup(packidx->fd);
1374 return got_error_from_errno("dup");
1376 if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
1377 sizeof(ipackidx)) == -1) {
1378 err = got_error_from_errno("imsg_compose PACKIDX");
1383 if (strlcpy(ipack.path_packfile, pack->path_packfile,
1384 sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
1385 return got_error(GOT_ERR_NO_SPACE);
1386 ipack.filesize = pack->filesize;
1390 return got_error_from_errno("dup");
1392 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
1394 err = got_error_from_errno("imsg_compose PACK");
1399 return flush_imsg(ibuf);
1402 const struct got_error *
1403 got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
1404 struct got_object_id *id)
1406 struct got_imsg_packed_object iobj;
1409 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
1411 if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
1412 &iobj, sizeof(iobj)) == -1)
1413 return got_error_from_errno("imsg_compose "
1414 "PACKED_OBJECT_REQUEST");
1416 return flush_imsg(ibuf);
1419 const struct got_error *
1420 got_privsep_send_gitconfig_parse_req(struct imsgbuf *ibuf, int fd)
1422 const struct got_error *err = NULL;
1424 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_PARSE_REQUEST, 0, 0, fd,
1426 err = got_error_from_errno("imsg_compose "
1427 "GITCONFIG_PARSE_REQUEST");
1432 return flush_imsg(ibuf);
1435 const struct got_error *
1436 got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *ibuf)
1438 if (imsg_compose(ibuf,
1439 GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST, 0, 0, -1,
1441 return got_error_from_errno("imsg_compose "
1442 "GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST");
1444 return flush_imsg(ibuf);
1447 const struct got_error *
1448 got_privsep_send_gitconfig_author_name_req(struct imsgbuf *ibuf)
1450 if (imsg_compose(ibuf,
1451 GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST, 0, 0, -1, NULL, 0) == -1)
1452 return got_error_from_errno("imsg_compose "
1453 "GITCONFIG_AUTHOR_NAME_REQUEST");
1455 return flush_imsg(ibuf);
1458 const struct got_error *
1459 got_privsep_send_gitconfig_author_email_req(struct imsgbuf *ibuf)
1461 if (imsg_compose(ibuf,
1462 GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST, 0, 0, -1, NULL, 0) == -1)
1463 return got_error_from_errno("imsg_compose "
1464 "GITCONFIG_AUTHOR_EMAIL_REQUEST");
1466 return flush_imsg(ibuf);
1469 const struct got_error *
1470 got_privsep_send_gitconfig_remotes_req(struct imsgbuf *ibuf)
1472 if (imsg_compose(ibuf,
1473 GOT_IMSG_GITCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
1474 return got_error_from_errno("imsg_compose "
1475 "GITCONFIG_REMOTE_REQUEST");
1477 return flush_imsg(ibuf);
1480 const struct got_error *
1481 got_privsep_send_gitconfig_owner_req(struct imsgbuf *ibuf)
1483 if (imsg_compose(ibuf,
1484 GOT_IMSG_GITCONFIG_OWNER_REQUEST, 0, 0, -1, NULL, 0) == -1)
1485 return got_error_from_errno("imsg_compose "
1486 "GITCONFIG_OWNER_REQUEST");
1488 return flush_imsg(ibuf);
1491 const struct got_error *
1492 got_privsep_send_gitconfig_str(struct imsgbuf *ibuf, const char *value)
1494 size_t len = value ? strlen(value) + 1 : 0;
1496 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_STR_VAL, 0, 0, -1,
1498 return got_error_from_errno("imsg_compose GITCONFIG_STR_VAL");
1500 return flush_imsg(ibuf);
1503 const struct got_error *
1504 got_privsep_recv_gitconfig_str(char **str, struct imsgbuf *ibuf)
1506 const struct got_error *err = NULL;
1509 const size_t min_datalen = 0;
1513 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1516 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1518 switch (imsg.hdr.type) {
1519 case GOT_IMSG_GITCONFIG_STR_VAL:
1522 *str = malloc(datalen);
1524 err = got_error_from_errno("malloc");
1527 if (strlcpy(*str, imsg.data, datalen) >= datalen)
1528 err = got_error(GOT_ERR_NO_SPACE);
1531 err = got_error(GOT_ERR_PRIVSEP_MSG);
1539 const struct got_error *
1540 got_privsep_send_gitconfig_int(struct imsgbuf *ibuf, int value)
1542 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_INT_VAL, 0, 0, -1,
1543 &value, sizeof(value)) == -1)
1544 return got_error_from_errno("imsg_compose GITCONFIG_INT_VAL");
1546 return flush_imsg(ibuf);
1549 const struct got_error *
1550 got_privsep_recv_gitconfig_int(int *val, struct imsgbuf *ibuf)
1552 const struct got_error *err = NULL;
1555 const size_t min_datalen =
1556 MIN(sizeof(struct got_imsg_error), sizeof(int));
1560 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1563 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1565 switch (imsg.hdr.type) {
1566 case GOT_IMSG_GITCONFIG_INT_VAL:
1567 if (datalen != sizeof(*val)) {
1568 err = got_error(GOT_ERR_PRIVSEP_LEN);
1571 memcpy(val, imsg.data, sizeof(*val));
1574 err = got_error(GOT_ERR_PRIVSEP_MSG);
1582 const struct got_error *
1583 got_privsep_send_gitconfig_remotes(struct imsgbuf *ibuf,
1584 struct got_remote_repo *remotes, int nremotes)
1586 const struct got_error *err = NULL;
1587 struct got_imsg_remotes iremotes;
1590 iremotes.nremotes = nremotes;
1591 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_REMOTES, 0, 0, -1,
1592 &iremotes, sizeof(iremotes)) == -1)
1593 return got_error_from_errno("imsg_compose GITCONFIG_REMOTES");
1595 err = flush_imsg(ibuf);
1600 for (i = 0; i < nremotes; i++) {
1601 struct got_imsg_remote iremote;
1602 size_t len = sizeof(iremote);
1605 iremote.name_len = strlen(remotes[i].name);
1606 len += iremote.name_len;
1607 iremote.url_len = strlen(remotes[i].url);
1608 len += iremote.url_len;
1610 wbuf = imsg_create(ibuf, GOT_IMSG_GITCONFIG_REMOTE, 0, 0, len);
1612 return got_error_from_errno(
1613 "imsg_create GITCONFIG_REMOTE");
1615 if (imsg_add(wbuf, &iremote, sizeof(iremote)) == -1) {
1616 err = got_error_from_errno(
1617 "imsg_add GIITCONFIG_REMOTE");
1622 if (imsg_add(wbuf, remotes[i].name, iremote.name_len) == -1) {
1623 err = got_error_from_errno(
1624 "imsg_add GIITCONFIG_REMOTE");
1628 if (imsg_add(wbuf, remotes[i].url, iremote.url_len) == -1) {
1629 err = got_error_from_errno(
1630 "imsg_add GIITCONFIG_REMOTE");
1636 imsg_close(ibuf, wbuf);
1637 err = flush_imsg(ibuf);
1645 const struct got_error *
1646 got_privsep_recv_gitconfig_remotes(struct got_remote_repo **remotes,
1647 int *nremotes, struct imsgbuf *ibuf)
1649 const struct got_error *err = NULL;
1652 struct got_imsg_remotes iremotes;
1653 struct got_imsg_remote iremote;
1657 iremotes.nremotes = 0;
1659 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremotes));
1662 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1664 switch (imsg.hdr.type) {
1665 case GOT_IMSG_GITCONFIG_REMOTES:
1666 if (datalen != sizeof(iremotes)) {
1667 err = got_error(GOT_ERR_PRIVSEP_LEN);
1670 memcpy(&iremotes, imsg.data, sizeof(iremotes));
1671 if (iremotes.nremotes == 0) {
1678 return got_error(GOT_ERR_PRIVSEP_MSG);
1683 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(iremote));
1684 if (*remotes == NULL)
1685 return got_error_from_errno("recallocarray");
1687 while (*nremotes < iremotes.nremotes) {
1688 struct got_remote_repo *remote;
1690 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremote));
1693 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1695 switch (imsg.hdr.type) {
1696 case GOT_IMSG_GITCONFIG_REMOTE:
1697 remote = &(*remotes)[*nremotes];
1698 if (datalen < sizeof(iremote)) {
1699 err = got_error(GOT_ERR_PRIVSEP_LEN);
1702 memcpy(&iremote, imsg.data, sizeof(iremote));
1703 if (iremote.name_len == 0 || iremote.url_len == 0 ||
1704 (sizeof(iremote) + iremote.name_len +
1705 iremote.url_len) > datalen) {
1706 err = got_error(GOT_ERR_PRIVSEP_LEN);
1709 remote->name = strndup(imsg.data + sizeof(iremote),
1711 if (remote->name == NULL) {
1712 err = got_error_from_errno("strndup");
1715 remote->url = strndup(imsg.data + sizeof(iremote) +
1716 iremote.name_len, iremote.url_len);
1717 if (remote->url == NULL) {
1718 err = got_error_from_errno("strndup");
1725 err = got_error(GOT_ERR_PRIVSEP_MSG);
1736 for (i = 0; i < *nremotes; i++) {
1737 free((*remotes)[i].name);
1738 free((*remotes)[i].url);
1747 const struct got_error *
1748 got_privsep_send_commit_traversal_request(struct imsgbuf *ibuf,
1749 struct got_object_id *id, int idx, const char *path)
1751 const struct got_error *err = NULL;
1753 size_t path_len = strlen(path) + 1;
1755 wbuf = imsg_create(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_REQUEST, 0, 0,
1756 sizeof(struct got_imsg_commit_traversal_request) + path_len);
1758 return got_error_from_errno(
1759 "imsg_create COMMIT_TRAVERSAL_REQUEST");
1760 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
1761 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
1765 if (imsg_add(wbuf, &idx, sizeof(idx)) == -1) {
1766 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
1770 if (imsg_add(wbuf, path, path_len) == -1) {
1771 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
1777 imsg_close(ibuf, wbuf);
1779 return flush_imsg(ibuf);
1782 const struct got_error *
1783 got_privsep_send_traversed_commits(struct got_object_id *commit_ids,
1784 size_t ncommits, struct imsgbuf *ibuf)
1786 const struct got_error *err;
1790 wbuf = imsg_create(ibuf, GOT_IMSG_TRAVERSED_COMMITS, 0, 0,
1791 sizeof(struct got_imsg_traversed_commits) +
1792 ncommits * SHA1_DIGEST_LENGTH);
1794 return got_error_from_errno("imsg_create TRAVERSED_COMMITS");
1796 if (imsg_add(wbuf, &ncommits, sizeof(ncommits)) == -1) {
1797 err = got_error_from_errno("imsg_add TRAVERSED_COMMITS");
1801 for (i = 0; i < ncommits; i++) {
1802 struct got_object_id *id = &commit_ids[i];
1803 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
1804 err = got_error_from_errno(
1805 "imsg_add TRAVERSED_COMMITS");
1812 imsg_close(ibuf, wbuf);
1814 return flush_imsg(ibuf);
1817 const struct got_error *
1818 got_privsep_recv_traversed_commits(struct got_commit_object **changed_commit,
1819 struct got_object_id **changed_commit_id,
1820 struct got_object_id_queue *commit_ids, struct imsgbuf *ibuf)
1822 const struct got_error *err = NULL;
1824 struct got_imsg_traversed_commits *icommits;
1828 *changed_commit = NULL;
1829 *changed_commit_id = NULL;
1832 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1836 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1837 switch (imsg.hdr.type) {
1838 case GOT_IMSG_TRAVERSED_COMMITS:
1839 icommits = imsg.data;
1840 if (datalen != sizeof(*icommits) +
1841 icommits->ncommits * SHA1_DIGEST_LENGTH) {
1842 err = got_error(GOT_ERR_PRIVSEP_LEN);
1845 for (i = 0; i < icommits->ncommits; i++) {
1846 struct got_object_qid *qid;
1847 uint8_t *sha1 = (uint8_t *)imsg.data +
1848 sizeof(*icommits) + i * SHA1_DIGEST_LENGTH;
1849 err = got_object_qid_alloc_partial(&qid);
1852 memcpy(qid->id->sha1, sha1, SHA1_DIGEST_LENGTH);
1853 SIMPLEQ_INSERT_TAIL(commit_ids, qid, entry);
1855 /* The last commit may contain a change. */
1856 if (i == icommits->ncommits - 1) {
1857 *changed_commit_id =
1858 got_object_id_dup(qid->id);
1859 if (*changed_commit_id == NULL) {
1860 err = got_error_from_errno(
1861 "got_object_id_dup");
1867 case GOT_IMSG_COMMIT:
1868 if (*changed_commit_id == NULL) {
1869 err = got_error(GOT_ERR_PRIVSEP_MSG);
1872 err = get_commit_from_imsg(changed_commit, &imsg,
1875 case GOT_IMSG_COMMIT_TRAVERSAL_DONE:
1879 err = got_error(GOT_ERR_PRIVSEP_MSG);
1889 got_object_id_queue_free(commit_ids);
1893 const struct got_error *
1894 got_privsep_send_commit_traversal_done(struct imsgbuf *ibuf)
1896 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_DONE, 0, 0, -1,
1898 return got_error_from_errno("imsg_compose TRAVERSAL_DONE");
1900 return flush_imsg(ibuf);
1903 const struct got_error *
1904 got_privsep_unveil_exec_helpers(void)
1906 const char *helpers[] = {
1907 GOT_PATH_PROG_READ_PACK,
1908 GOT_PATH_PROG_READ_OBJECT,
1909 GOT_PATH_PROG_READ_COMMIT,
1910 GOT_PATH_PROG_READ_TREE,
1911 GOT_PATH_PROG_READ_BLOB,
1912 GOT_PATH_PROG_READ_TAG,
1913 GOT_PATH_PROG_READ_GITCONFIG,
1917 for (i = 0; i < nitems(helpers); i++) {
1918 if (unveil(helpers[i], "x") == 0)
1920 return got_error_from_errno2("unveil", helpers[i]);
1927 got_privsep_exec_child(int imsg_fds[2], const char *path, const char *repo_path)
1929 if (close(imsg_fds[0]) != 0) {
1930 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
1934 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
1935 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
1938 if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
1939 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
1943 if (execl(path, path, repo_path, (char *)NULL) == -1) {
1944 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,