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>
36 #include "got_object.h"
37 #include "got_error.h"
39 #include "got_repository.h"
41 #include "got_lib_sha1.h"
42 #include "got_lib_delta.h"
43 #include "got_lib_inflate.h"
44 #include "got_lib_object.h"
45 #include "got_lib_object_parse.h"
46 #include "got_lib_privsep.h"
47 #include "got_lib_pack.h"
50 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
54 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
57 static const struct got_error *
58 poll_fd(int fd, int events, int timeout)
64 pfd[0].events = events;
66 n = poll(pfd, 1, timeout);
68 return got_error_from_errno("poll");
70 return got_error(GOT_ERR_TIMEOUT);
71 if (pfd[0].revents & (POLLERR | POLLNVAL))
72 return got_error_from_errno("poll error");
73 if (pfd[0].revents & (events | POLLHUP))
76 return got_error(GOT_ERR_INTERRUPT);
79 static const struct got_error *
80 read_imsg(struct imsgbuf *ibuf)
82 const struct got_error *err;
85 err = poll_fd(ibuf->fd, POLLIN, INFTIM);
91 if (errno == EAGAIN) /* Could be a file-descriptor leak. */
92 return got_error(GOT_ERR_PRIVSEP_NO_FD);
93 return got_error(GOT_ERR_PRIVSEP_READ);
96 return got_error(GOT_ERR_PRIVSEP_PIPE);
101 const struct got_error *
102 got_privsep_wait_for_child(pid_t pid)
106 if (waitpid(pid, &child_status, 0) == -1)
107 return got_error_from_errno("waitpid");
109 if (!WIFEXITED(child_status))
110 return got_error(GOT_ERR_PRIVSEP_DIED);
112 if (WEXITSTATUS(child_status) != 0)
113 return got_error(GOT_ERR_PRIVSEP_EXIT);
118 static const struct got_error *
119 recv_imsg_error(struct imsg *imsg, size_t datalen)
121 struct got_imsg_error *ierr;
123 if (datalen != sizeof(*ierr))
124 return got_error(GOT_ERR_PRIVSEP_LEN);
127 if (ierr->code == GOT_ERR_ERRNO) {
128 static struct got_error serr;
129 serr.code = GOT_ERR_ERRNO;
130 serr.msg = strerror(ierr->errno_code);
134 return got_error(ierr->code);
137 const struct got_error *
138 got_privsep_recv_imsg(struct imsg *imsg, struct imsgbuf *ibuf,
141 const struct got_error *err;
144 n = imsg_get(ibuf, imsg);
146 return got_error_from_errno("imsg_get");
149 err = read_imsg(ibuf);
152 n = imsg_get(ibuf, imsg);
154 return got_error_from_errno("imsg_get");
157 if (imsg->hdr.len < IMSG_HEADER_SIZE + min_datalen)
158 return got_error(GOT_ERR_PRIVSEP_LEN);
160 if (imsg->hdr.type == GOT_IMSG_ERROR) {
161 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
162 return recv_imsg_error(imsg, datalen);
168 /* Attempt to send an error in an imsg. Complain on stderr as a last resort. */
170 got_privsep_send_error(struct imsgbuf *ibuf, const struct got_error *err)
172 const struct got_error *poll_err;
173 struct got_imsg_error ierr;
176 ierr.code = err->code;
177 if (err->code == GOT_ERR_ERRNO)
178 ierr.errno_code = errno;
181 ret = imsg_compose(ibuf, GOT_IMSG_ERROR, 0, 0, -1, &ierr, sizeof(ierr));
183 fprintf(stderr, "%s: error %d \"%s\": imsg_compose: %s\n",
184 getprogname(), err->code, err->msg, strerror(errno));
188 poll_err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
190 fprintf(stderr, "%s: error %d \"%s\": poll: %s\n",
191 getprogname(), err->code, err->msg, poll_err->msg);
195 ret = imsg_flush(ibuf);
197 fprintf(stderr, "%s: error %d \"%s\": imsg_flush: %s\n",
198 getprogname(), err->code, err->msg, strerror(errno));
203 static const struct got_error *
204 flush_imsg(struct imsgbuf *ibuf)
206 const struct got_error *err;
208 err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
212 if (imsg_flush(ibuf) == -1)
213 return got_error_from_errno("imsg_flush");
218 const struct got_error *
219 got_privsep_flush_imsg(struct imsgbuf *ibuf)
221 return flush_imsg(ibuf);
224 const struct got_error *
225 got_privsep_send_stop(int fd)
227 const struct got_error *err = NULL;
230 imsg_init(&ibuf, fd);
232 if (imsg_compose(&ibuf, GOT_IMSG_STOP, 0, 0, -1, NULL, 0) == -1)
233 return got_error_from_errno("imsg_compose STOP");
235 err = flush_imsg(&ibuf);
240 const struct got_error *
241 got_privsep_send_obj_req(struct imsgbuf *ibuf, int fd)
243 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_REQUEST, 0, 0, fd, NULL, 0)
245 return got_error_from_errno("imsg_compose OBJECT_REQUEST");
247 return flush_imsg(ibuf);
250 const struct got_error *
251 got_privsep_send_commit_req(struct imsgbuf *ibuf, int fd,
252 struct got_object_id *id, int pack_idx)
254 const struct got_error *err = NULL;
255 struct got_imsg_packed_object iobj, *iobjp;
258 if (id) { /* commit is packed */
260 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
268 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_REQUEST, 0, 0, fd, iobjp, len)
270 err = got_error_from_errno("imsg_compose COMMIT_REQUEST");
275 return flush_imsg(ibuf);
278 const struct got_error *
279 got_privsep_send_tree_req(struct imsgbuf *ibuf, int fd,
280 struct got_object_id *id, int pack_idx)
282 const struct got_error *err = NULL;
284 size_t len = id ? sizeof(struct got_imsg_packed_object) : 0;
286 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_REQUEST, 0, 0, len);
288 return got_error_from_errno("imsg_create TREE_REQUEST");
290 if (id) { /* tree is packed */
291 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
292 err = got_error_from_errno("imsg_add TREE_ENTRY");
297 if (imsg_add(wbuf, &pack_idx, sizeof(pack_idx)) == -1) {
298 err = got_error_from_errno("imsg_add TREE_ENTRY");
305 imsg_close(ibuf, wbuf);
307 return flush_imsg(ibuf);
310 const struct got_error *
311 got_privsep_send_tag_req(struct imsgbuf *ibuf, int fd,
312 struct got_object_id *id, int pack_idx)
314 struct got_imsg_packed_object iobj, *iobjp;
317 if (id) { /* tag is packed */
319 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
327 if (imsg_compose(ibuf, GOT_IMSG_TAG_REQUEST, 0, 0, fd, iobjp, len)
329 return got_error_from_errno("imsg_compose TAG_REQUEST");
331 return flush_imsg(ibuf);
334 const struct got_error *
335 got_privsep_send_blob_req(struct imsgbuf *ibuf, int infd,
336 struct got_object_id *id, int pack_idx)
338 const struct got_error *err = NULL;
339 struct got_imsg_packed_object iobj, *iobjp;
342 if (id) { /* blob is packed */
344 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
352 if (imsg_compose(ibuf, GOT_IMSG_BLOB_REQUEST, 0, 0, infd, iobjp, len)
354 err = got_error_from_errno("imsg_compose BLOB_REQUEST");
359 return flush_imsg(ibuf);
362 const struct got_error *
363 got_privsep_send_blob_outfd(struct imsgbuf *ibuf, int outfd)
365 const struct got_error *err = NULL;
367 if (imsg_compose(ibuf, GOT_IMSG_BLOB_OUTFD, 0, 0, outfd, NULL, 0)
369 err = got_error_from_errno("imsg_compose BLOB_OUTFD");
374 return flush_imsg(ibuf);
377 static const struct got_error *
378 send_fd(struct imsgbuf *ibuf, int imsg_code, int fd)
380 const struct got_error *err = NULL;
382 if (imsg_compose(ibuf, imsg_code, 0, 0, fd, NULL, 0) == -1) {
383 err = got_error_from_errno("imsg_compose TMPFD");
388 return flush_imsg(ibuf);
391 const struct got_error *
392 got_privsep_send_tmpfd(struct imsgbuf *ibuf, int fd)
394 return send_fd(ibuf, GOT_IMSG_TMPFD, fd);
397 const struct got_error *
398 got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj)
400 struct got_imsg_object iobj;
402 memcpy(iobj.id, obj->id.sha1, sizeof(iobj.id));
403 iobj.type = obj->type;
404 iobj.flags = obj->flags;
405 iobj.hdrlen = obj->hdrlen;
406 iobj.size = obj->size;
407 if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
408 iobj.pack_offset = obj->pack_offset;
409 iobj.pack_idx = obj->pack_idx;
412 if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
414 return got_error_from_errno("imsg_compose OBJECT");
416 return flush_imsg(ibuf);
419 const struct got_error *
420 got_privsep_send_fetch_req(struct imsgbuf *ibuf, int fd,
421 struct got_pathlist_head *have_refs, int fetch_all_branches,
422 struct got_pathlist_head *wanted_branches,
423 struct got_pathlist_head *wanted_refs, int list_refs_only, int verbosity)
425 const struct got_error *err = NULL;
428 struct got_pathlist_entry *pe;
429 struct got_imsg_fetch_request fetchreq;
431 memset(&fetchreq, 0, sizeof(fetchreq));
432 fetchreq.fetch_all_branches = fetch_all_branches;
433 fetchreq.list_refs_only = list_refs_only;
434 fetchreq.verbosity = verbosity;
435 TAILQ_FOREACH(pe, have_refs, entry)
436 fetchreq.n_have_refs++;
437 TAILQ_FOREACH(pe, wanted_branches, entry)
438 fetchreq.n_wanted_branches++;
439 TAILQ_FOREACH(pe, wanted_refs, entry)
440 fetchreq.n_wanted_refs++;
441 len = sizeof(struct got_imsg_fetch_request);
442 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
444 return got_error(GOT_ERR_NO_SPACE);
447 if (imsg_compose(ibuf, GOT_IMSG_FETCH_REQUEST, 0, 0, fd,
448 &fetchreq, sizeof(fetchreq)) == -1)
449 return got_error_from_errno(
450 "imsg_compose FETCH_SERVER_PROGRESS");
452 err = flush_imsg(ibuf);
459 TAILQ_FOREACH(pe, have_refs, entry) {
460 const char *name = pe->path;
461 size_t name_len = pe->path_len;
462 struct got_object_id *id = pe->data;
464 len = sizeof(struct got_imsg_fetch_have_ref) + name_len;
465 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_HAVE_REF, 0, 0, len);
467 return got_error_from_errno("imsg_create FETCH_HAVE_REF");
469 /* Keep in sync with struct got_imsg_fetch_have_ref! */
470 if (imsg_add(wbuf, id->sha1, sizeof(id->sha1)) == -1) {
471 err = got_error_from_errno("imsg_add FETCH_HAVE_REF");
475 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
476 err = got_error_from_errno("imsg_add FETCH_HAVE_REF");
480 if (imsg_add(wbuf, name, name_len) == -1) {
481 err = got_error_from_errno("imsg_add FETCH_HAVE_REF");
487 imsg_close(ibuf, wbuf);
488 err = flush_imsg(ibuf);
493 TAILQ_FOREACH(pe, wanted_branches, entry) {
494 const char *name = pe->path;
495 size_t name_len = pe->path_len;
497 len = sizeof(struct got_imsg_fetch_wanted_branch) + name_len;
498 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_WANTED_BRANCH, 0, 0,
501 return got_error_from_errno(
502 "imsg_create FETCH_WANTED_BRANCH");
504 /* Keep in sync with struct got_imsg_fetch_wanted_branch! */
505 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
506 err = got_error_from_errno(
507 "imsg_add FETCH_WANTED_BRANCH");
511 if (imsg_add(wbuf, name, name_len) == -1) {
512 err = got_error_from_errno(
513 "imsg_add FETCH_WANTED_BRANCH");
519 imsg_close(ibuf, wbuf);
520 err = flush_imsg(ibuf);
525 TAILQ_FOREACH(pe, wanted_refs, entry) {
526 const char *name = pe->path;
527 size_t name_len = pe->path_len;
529 len = sizeof(struct got_imsg_fetch_wanted_ref) + name_len;
530 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_WANTED_REF, 0, 0,
533 return got_error_from_errno(
534 "imsg_create FETCH_WANTED_REF");
536 /* Keep in sync with struct got_imsg_fetch_wanted_ref! */
537 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
538 err = got_error_from_errno(
539 "imsg_add FETCH_WANTED_REF");
543 if (imsg_add(wbuf, name, name_len) == -1) {
544 err = got_error_from_errno(
545 "imsg_add FETCH_WANTED_REF");
551 imsg_close(ibuf, wbuf);
552 err = flush_imsg(ibuf);
562 const struct got_error *
563 got_privsep_send_fetch_outfd(struct imsgbuf *ibuf, int fd)
565 return send_fd(ibuf, GOT_IMSG_FETCH_OUTFD, fd);
568 const struct got_error *
569 got_privsep_recv_fetch_progress(int *done, struct got_object_id **id,
570 char **refname, struct got_pathlist_head *symrefs, char **server_progress,
571 off_t *packfile_size, struct imsgbuf *ibuf)
573 const struct got_error *err = NULL;
576 struct got_imsg_fetch_symrefs *isymrefs = NULL;
584 *server_progress = NULL;
587 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
591 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
592 switch (imsg.hdr.type) {
594 if (datalen < sizeof(struct got_imsg_error)) {
595 err = got_error(GOT_ERR_PRIVSEP_LEN);
598 err = recv_imsg_error(&imsg, datalen);
600 case GOT_IMSG_FETCH_SYMREFS:
601 if (datalen < sizeof(*isymrefs)) {
602 err = got_error(GOT_ERR_PRIVSEP_LEN);
605 if (isymrefs != NULL) {
606 err = got_error(GOT_ERR_PRIVSEP_MSG);
609 isymrefs = (struct got_imsg_fetch_symrefs *)imsg.data;
610 off = sizeof(*isymrefs);
611 remain = datalen - off;
612 for (n = 0; n < isymrefs->nsymrefs; n++) {
613 struct got_imsg_fetch_symref *s;
615 if (remain < sizeof(struct got_imsg_fetch_symref)) {
616 err = got_error(GOT_ERR_PRIVSEP_LEN);
619 s = (struct got_imsg_fetch_symref *)(imsg.data + off);
621 remain -= sizeof(*s);
622 if (remain < s->name_len) {
623 err = got_error(GOT_ERR_PRIVSEP_LEN);
626 name = strndup(imsg.data + off, s->name_len);
628 err = got_error_from_errno("strndup");
632 remain -= s->name_len;
633 if (remain < s->target_len) {
634 err = got_error(GOT_ERR_PRIVSEP_LEN);
638 target = strndup(imsg.data + off, s->target_len);
639 if (target == NULL) {
640 err = got_error_from_errno("strndup");
644 off += s->target_len;
645 remain -= s->target_len;
646 err = got_pathlist_append(symrefs, name, target);
654 case GOT_IMSG_FETCH_REF:
655 if (datalen <= SHA1_DIGEST_LENGTH) {
656 err = got_error(GOT_ERR_PRIVSEP_MSG);
659 *id = malloc(sizeof(**id));
661 err = got_error_from_errno("malloc");
664 memcpy((*id)->sha1, imsg.data, SHA1_DIGEST_LENGTH);
665 *refname = strndup(imsg.data + SHA1_DIGEST_LENGTH,
666 datalen - SHA1_DIGEST_LENGTH);
667 if (*refname == NULL) {
668 err = got_error_from_errno("strndup");
672 case GOT_IMSG_FETCH_SERVER_PROGRESS:
674 err = got_error(GOT_ERR_PRIVSEP_LEN);
677 *server_progress = strndup(imsg.data, datalen);
678 if (*server_progress == NULL) {
679 err = got_error_from_errno("strndup");
682 for (i = 0; i < datalen; i++) {
683 if (!isprint((unsigned char)(*server_progress)[i]) &&
684 !isspace((unsigned char)(*server_progress)[i])) {
685 err = got_error(GOT_ERR_PRIVSEP_MSG);
686 free(*server_progress);
687 *server_progress = NULL;
692 case GOT_IMSG_FETCH_DOWNLOAD_PROGRESS:
693 if (datalen < sizeof(*packfile_size)) {
694 err = got_error(GOT_ERR_PRIVSEP_MSG);
697 memcpy(packfile_size, imsg.data, sizeof(*packfile_size));
699 case GOT_IMSG_FETCH_DONE:
700 *id = malloc(sizeof(**id));
702 err = got_error_from_errno("malloc");
705 if (datalen != SHA1_DIGEST_LENGTH) {
706 err = got_error(GOT_ERR_PRIVSEP_MSG);
709 memcpy((*id)->sha1, imsg.data, SHA1_DIGEST_LENGTH);
713 err = got_error(GOT_ERR_PRIVSEP_MSG);
727 const struct got_error *
728 got_privsep_send_index_pack_req(struct imsgbuf *ibuf, uint8_t *pack_hash,
731 const struct got_error *err = NULL;
733 /* Keep in sync with struct got_imsg_index_pack_request */
734 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_REQUEST, 0, 0, fd,
735 pack_hash, SHA1_DIGEST_LENGTH) == -1) {
736 err = got_error_from_errno("imsg_compose INDEX_REQUEST");
740 return flush_imsg(ibuf);
743 const struct got_error *
744 got_privsep_send_index_pack_outfd(struct imsgbuf *ibuf, int fd)
746 return send_fd(ibuf, GOT_IMSG_IDXPACK_OUTFD, fd);
749 const struct got_error *
750 got_privsep_recv_index_progress(int *done, int *nobj_total,
751 int *nobj_indexed, int *nobj_loose, int *nobj_resolved,
752 struct imsgbuf *ibuf)
754 const struct got_error *err = NULL;
756 struct got_imsg_index_pack_progress *iprogress;
764 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
768 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
769 switch (imsg.hdr.type) {
771 if (datalen < sizeof(struct got_imsg_error)) {
772 err = got_error(GOT_ERR_PRIVSEP_LEN);
775 err = recv_imsg_error(&imsg, datalen);
777 case GOT_IMSG_IDXPACK_PROGRESS:
778 if (datalen < sizeof(*iprogress)) {
779 err = got_error(GOT_ERR_PRIVSEP_LEN);
782 iprogress = (struct got_imsg_index_pack_progress *)imsg.data;
783 *nobj_total = iprogress->nobj_total;
784 *nobj_indexed = iprogress->nobj_indexed;
785 *nobj_loose = iprogress->nobj_loose;
786 *nobj_resolved = iprogress->nobj_resolved;
788 case GOT_IMSG_IDXPACK_DONE:
790 err = got_error(GOT_ERR_PRIVSEP_LEN);
796 err = got_error(GOT_ERR_PRIVSEP_MSG);
804 const struct got_error *
805 got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
806 struct imsgbuf *ibuf)
808 const struct got_error *err = NULL;
809 struct got_imsg_object *iobj;
810 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
812 if (datalen != sizeof(*iobj))
813 return got_error(GOT_ERR_PRIVSEP_LEN);
816 *obj = calloc(1, sizeof(**obj));
818 return got_error_from_errno("calloc");
820 memcpy((*obj)->id.sha1, iobj->id, SHA1_DIGEST_LENGTH);
821 (*obj)->type = iobj->type;
822 (*obj)->flags = iobj->flags;
823 (*obj)->hdrlen = iobj->hdrlen;
824 (*obj)->size = iobj->size;
825 /* path_packfile is handled by caller */
826 if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
827 (*obj)->pack_offset = iobj->pack_offset;
828 (*obj)->pack_idx = iobj->pack_idx;
834 const struct got_error *
835 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
837 const struct got_error *err = NULL;
839 const size_t min_datalen =
840 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
844 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
848 switch (imsg.hdr.type) {
849 case GOT_IMSG_OBJECT:
850 err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
853 err = got_error(GOT_ERR_PRIVSEP_MSG);
862 static const struct got_error *
863 send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
866 const struct got_error *err = NULL;
867 size_t offset, remain;
872 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
874 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
875 commit->logmsg + offset, n) == -1) {
876 err = got_error_from_errno("imsg_compose "
881 err = flush_imsg(ibuf);
892 const struct got_error *
893 got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
895 const struct got_error *err = NULL;
896 struct got_imsg_commit_object *icommit;
899 struct got_object_qid *qid;
900 size_t author_len = strlen(commit->author);
901 size_t committer_len = strlen(commit->committer);
902 size_t logmsg_len = strlen(commit->logmsg);
904 total = sizeof(*icommit) + author_len + committer_len +
905 commit->nparents * SHA1_DIGEST_LENGTH;
909 return got_error_from_errno("malloc");
911 icommit = (struct got_imsg_commit_object *)buf;
912 memcpy(icommit->tree_id, commit->tree_id->sha1,
913 sizeof(icommit->tree_id));
914 icommit->author_len = author_len;
915 icommit->author_time = commit->author_time;
916 icommit->author_gmtoff = commit->author_gmtoff;
917 icommit->committer_len = committer_len;
918 icommit->committer_time = commit->committer_time;
919 icommit->committer_gmtoff = commit->committer_gmtoff;
920 icommit->logmsg_len = logmsg_len;
921 icommit->nparents = commit->nparents;
923 len = sizeof(*icommit);
924 memcpy(buf + len, commit->author, author_len);
926 memcpy(buf + len, commit->committer, committer_len);
927 len += committer_len;
928 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
929 memcpy(buf + len, qid->id, SHA1_DIGEST_LENGTH);
930 len += SHA1_DIGEST_LENGTH;
933 if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
934 err = got_error_from_errno("imsg_compose COMMIT");
938 if (logmsg_len == 0 ||
939 logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
940 err = flush_imsg(ibuf);
944 err = send_commit_logmsg(ibuf, commit, logmsg_len);
950 static const struct got_error *
951 get_commit_from_imsg(struct got_commit_object **commit,
952 struct imsg *imsg, size_t datalen, struct imsgbuf *ibuf)
954 const struct got_error *err = NULL;
955 struct got_imsg_commit_object *icommit;
959 if (datalen < sizeof(*icommit))
960 return got_error(GOT_ERR_PRIVSEP_LEN);
962 icommit = imsg->data;
963 if (datalen != sizeof(*icommit) + icommit->author_len +
964 icommit->committer_len +
965 icommit->nparents * SHA1_DIGEST_LENGTH)
966 return got_error(GOT_ERR_PRIVSEP_LEN);
968 if (icommit->nparents < 0)
969 return got_error(GOT_ERR_PRIVSEP_LEN);
971 len += sizeof(*icommit);
973 *commit = got_object_commit_alloc_partial();
975 return got_error_from_errno(
976 "got_object_commit_alloc_partial");
978 memcpy((*commit)->tree_id->sha1, icommit->tree_id,
980 (*commit)->author_time = icommit->author_time;
981 (*commit)->author_gmtoff = icommit->author_gmtoff;
982 (*commit)->committer_time = icommit->committer_time;
983 (*commit)->committer_gmtoff = icommit->committer_gmtoff;
985 if (icommit->author_len == 0) {
986 (*commit)->author = strdup("");
987 if ((*commit)->author == NULL) {
988 err = got_error_from_errno("strdup");
992 (*commit)->author = malloc(icommit->author_len + 1);
993 if ((*commit)->author == NULL) {
994 err = got_error_from_errno("malloc");
997 memcpy((*commit)->author, imsg->data + len,
998 icommit->author_len);
999 (*commit)->author[icommit->author_len] = '\0';
1001 len += icommit->author_len;
1003 if (icommit->committer_len == 0) {
1004 (*commit)->committer = strdup("");
1005 if ((*commit)->committer == NULL) {
1006 err = got_error_from_errno("strdup");
1010 (*commit)->committer =
1011 malloc(icommit->committer_len + 1);
1012 if ((*commit)->committer == NULL) {
1013 err = got_error_from_errno("malloc");
1016 memcpy((*commit)->committer, imsg->data + len,
1017 icommit->committer_len);
1018 (*commit)->committer[icommit->committer_len] = '\0';
1020 len += icommit->committer_len;
1022 if (icommit->logmsg_len == 0) {
1023 (*commit)->logmsg = strdup("");
1024 if ((*commit)->logmsg == NULL) {
1025 err = got_error_from_errno("strdup");
1029 size_t offset = 0, remain = icommit->logmsg_len;
1031 (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
1032 if ((*commit)->logmsg == NULL) {
1033 err = got_error_from_errno("malloc");
1036 while (remain > 0) {
1037 struct imsg imsg_log;
1038 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1041 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1045 if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG) {
1046 err = got_error(GOT_ERR_PRIVSEP_MSG);
1050 memcpy((*commit)->logmsg + offset,
1052 imsg_free(&imsg_log);
1056 (*commit)->logmsg[icommit->logmsg_len] = '\0';
1059 for (i = 0; i < icommit->nparents; i++) {
1060 struct got_object_qid *qid;
1062 err = got_object_qid_alloc_partial(&qid);
1065 memcpy(qid->id, imsg->data + len +
1066 i * SHA1_DIGEST_LENGTH, sizeof(*qid->id));
1067 SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
1068 (*commit)->nparents++;
1072 got_object_commit_close(*commit);
1078 const struct got_error *
1079 got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
1081 const struct got_error *err = NULL;
1084 const size_t min_datalen =
1085 MIN(sizeof(struct got_imsg_error),
1086 sizeof(struct got_imsg_commit_object));
1090 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1094 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1096 switch (imsg.hdr.type) {
1097 case GOT_IMSG_COMMIT:
1098 err = get_commit_from_imsg(commit, &imsg, datalen, ibuf);
1101 err = got_error(GOT_ERR_PRIVSEP_MSG);
1110 const struct got_error *
1111 got_privsep_send_tree(struct imsgbuf *ibuf, struct got_pathlist_head *entries,
1114 const struct got_error *err = NULL;
1115 struct got_imsg_tree_object itree;
1116 struct got_pathlist_entry *pe;
1118 int nimsg; /* number of imsg queued in ibuf */
1120 itree.nentries = nentries;
1121 if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
1123 return got_error_from_errno("imsg_compose TREE");
1125 totlen = sizeof(itree);
1127 TAILQ_FOREACH(pe, entries, entry) {
1128 const char *name = pe->path;
1129 struct got_parsed_tree_entry *pte = pe->data;
1131 size_t namelen = strlen(name);
1132 size_t len = sizeof(struct got_imsg_tree_entry) + namelen;
1134 if (len > MAX_IMSGSIZE)
1135 return got_error(GOT_ERR_NO_SPACE);
1138 if (totlen + len >= MAX_IMSGSIZE - (IMSG_HEADER_SIZE * nimsg)) {
1139 err = flush_imsg(ibuf);
1145 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_ENTRY, 0, 0, len);
1147 return got_error_from_errno("imsg_create TREE_ENTRY");
1149 /* Keep in sync with struct got_imsg_tree_object definition! */
1150 if (imsg_add(wbuf, pte->id, SHA1_DIGEST_LENGTH) == -1) {
1151 err = got_error_from_errno("imsg_add TREE_ENTRY");
1155 if (imsg_add(wbuf, &pte->mode, sizeof(pte->mode)) == -1) {
1156 err = got_error_from_errno("imsg_add TREE_ENTRY");
1161 if (imsg_add(wbuf, name, namelen) == -1) {
1162 err = got_error_from_errno("imsg_add TREE_ENTRY");
1168 imsg_close(ibuf, wbuf);
1173 return flush_imsg(ibuf);
1176 const struct got_error *
1177 got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
1179 const struct got_error *err = NULL;
1180 const size_t min_datalen =
1181 MIN(sizeof(struct got_imsg_error),
1182 sizeof(struct got_imsg_tree_object));
1183 struct got_imsg_tree_object *itree;
1188 err = read_imsg(ibuf);
1196 struct got_imsg_tree_entry *ite;
1197 struct got_tree_entry *te = NULL;
1199 n = imsg_get(ibuf, &imsg);
1201 if (*tree && (*tree)->nentries != nentries)
1206 if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen)
1207 return got_error(GOT_ERR_PRIVSEP_LEN);
1209 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1211 switch (imsg.hdr.type) {
1212 case GOT_IMSG_ERROR:
1213 err = recv_imsg_error(&imsg, datalen);
1216 /* This message should only appear once. */
1217 if (*tree != NULL) {
1218 err = got_error(GOT_ERR_PRIVSEP_MSG);
1221 if (datalen != sizeof(*itree)) {
1222 err = got_error(GOT_ERR_PRIVSEP_LEN);
1226 *tree = malloc(sizeof(**tree));
1227 if (*tree == NULL) {
1228 err = got_error_from_errno("malloc");
1231 (*tree)->entries = calloc(itree->nentries,
1232 sizeof(struct got_tree_entry));
1233 if ((*tree)->entries == NULL) {
1234 err = got_error_from_errno("malloc");
1237 (*tree)->nentries = itree->nentries;
1238 (*tree)->refcnt = 0;
1240 case GOT_IMSG_TREE_ENTRY:
1241 /* This message should be preceeded by GOT_IMSG_TREE. */
1242 if (*tree == NULL) {
1243 err = got_error(GOT_ERR_PRIVSEP_MSG);
1246 if (datalen < sizeof(*ite) || datalen > MAX_IMSGSIZE) {
1247 err = got_error(GOT_ERR_PRIVSEP_LEN);
1251 /* Remaining data contains the entry's name. */
1252 datalen -= sizeof(*ite);
1253 if (datalen == 0 || datalen > MAX_IMSGSIZE) {
1254 err = got_error(GOT_ERR_PRIVSEP_LEN);
1259 if (datalen + 1 > sizeof(te->name)) {
1260 err = got_error(GOT_ERR_NO_SPACE);
1263 te = &(*tree)->entries[nentries];
1264 memcpy(te->name, imsg.data + sizeof(*ite), datalen);
1265 te->name[datalen] = '\0';
1267 memcpy(te->id.sha1, ite->id, SHA1_DIGEST_LENGTH);
1268 te->mode = ite->mode;
1273 err = got_error(GOT_ERR_PRIVSEP_MSG);
1280 if (*tree && (*tree)->nentries != nentries) {
1282 err = got_error(GOT_ERR_PRIVSEP_LEN);
1283 got_object_tree_close(*tree);
1290 const struct got_error *
1291 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
1292 const uint8_t *data)
1294 struct got_imsg_blob iblob;
1297 iblob.hdrlen = hdrlen;
1302 if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
1303 return got_error(GOT_ERR_NO_SPACE);
1305 buf = malloc(sizeof(iblob) + size);
1307 return got_error_from_errno("malloc");
1309 memcpy(buf, &iblob, sizeof(iblob));
1310 memcpy(buf + sizeof(iblob), data, size);
1311 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
1312 sizeof(iblob) + size) == -1) {
1314 return got_error_from_errno("imsg_compose BLOB");
1318 /* Data has already been written to file descriptor. */
1319 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
1320 sizeof(iblob)) == -1)
1321 return got_error_from_errno("imsg_compose BLOB");
1325 return flush_imsg(ibuf);
1328 const struct got_error *
1329 got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1330 struct imsgbuf *ibuf)
1332 const struct got_error *err = NULL;
1334 struct got_imsg_blob *iblob;
1339 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1343 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1345 switch (imsg.hdr.type) {
1347 if (datalen < sizeof(*iblob)) {
1348 err = got_error(GOT_ERR_PRIVSEP_LEN);
1352 *size = iblob->size;
1353 *hdrlen = iblob->hdrlen;
1355 if (datalen == sizeof(*iblob)) {
1356 /* Data has been written to file descriptor. */
1360 if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX) {
1361 err = got_error(GOT_ERR_PRIVSEP_LEN);
1365 *outbuf = malloc(*size);
1366 if (*outbuf == NULL) {
1367 err = got_error_from_errno("malloc");
1370 memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
1373 err = got_error(GOT_ERR_PRIVSEP_MSG);
1382 static const struct got_error *
1383 send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
1385 const struct got_error *err = NULL;
1386 size_t offset, remain;
1389 remain = tagmsg_len;
1390 while (remain > 0) {
1391 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1393 if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
1394 tag->tagmsg + offset, n) == -1) {
1395 err = got_error_from_errno("imsg_compose TAG_TAGMSG");
1399 err = flush_imsg(ibuf);
1410 const struct got_error *
1411 got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
1413 const struct got_error *err = NULL;
1414 struct got_imsg_tag_object *itag;
1417 size_t tag_len = strlen(tag->tag);
1418 size_t tagger_len = strlen(tag->tagger);
1419 size_t tagmsg_len = strlen(tag->tagmsg);
1421 total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
1423 buf = malloc(total);
1425 return got_error_from_errno("malloc");
1427 itag = (struct got_imsg_tag_object *)buf;
1428 memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
1429 itag->obj_type = tag->obj_type;
1430 itag->tag_len = tag_len;
1431 itag->tagger_len = tagger_len;
1432 itag->tagger_time = tag->tagger_time;
1433 itag->tagger_gmtoff = tag->tagger_gmtoff;
1434 itag->tagmsg_len = tagmsg_len;
1436 len = sizeof(*itag);
1437 memcpy(buf + len, tag->tag, tag_len);
1439 memcpy(buf + len, tag->tagger, tagger_len);
1442 if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
1443 err = got_error_from_errno("imsg_compose TAG");
1447 if (tagmsg_len == 0 ||
1448 tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1449 err = flush_imsg(ibuf);
1453 err = send_tagmsg(ibuf, tag, tagmsg_len);
1459 const struct got_error *
1460 got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
1462 const struct got_error *err = NULL;
1464 struct got_imsg_tag_object *itag;
1465 size_t len, datalen;
1466 const size_t min_datalen =
1467 MIN(sizeof(struct got_imsg_error),
1468 sizeof(struct got_imsg_tag_object));
1472 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1476 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1479 switch (imsg.hdr.type) {
1481 if (datalen < sizeof(*itag)) {
1482 err = got_error(GOT_ERR_PRIVSEP_LEN);
1486 if (datalen != sizeof(*itag) + itag->tag_len +
1488 err = got_error(GOT_ERR_PRIVSEP_LEN);
1491 len += sizeof(*itag);
1493 *tag = calloc(1, sizeof(**tag));
1495 err = got_error_from_errno("calloc");
1499 memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
1501 if (itag->tag_len == 0) {
1502 (*tag)->tag = strdup("");
1503 if ((*tag)->tag == NULL) {
1504 err = got_error_from_errno("strdup");
1508 (*tag)->tag = malloc(itag->tag_len + 1);
1509 if ((*tag)->tag == NULL) {
1510 err = got_error_from_errno("malloc");
1513 memcpy((*tag)->tag, imsg.data + len,
1515 (*tag)->tag[itag->tag_len] = '\0';
1517 len += itag->tag_len;
1519 (*tag)->obj_type = itag->obj_type;
1520 (*tag)->tagger_time = itag->tagger_time;
1521 (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1523 if (itag->tagger_len == 0) {
1524 (*tag)->tagger = strdup("");
1525 if ((*tag)->tagger == NULL) {
1526 err = got_error_from_errno("strdup");
1530 (*tag)->tagger = malloc(itag->tagger_len + 1);
1531 if ((*tag)->tagger == NULL) {
1532 err = got_error_from_errno("malloc");
1535 memcpy((*tag)->tagger, imsg.data + len,
1537 (*tag)->tagger[itag->tagger_len] = '\0';
1539 len += itag->tagger_len;
1541 if (itag->tagmsg_len == 0) {
1542 (*tag)->tagmsg = strdup("");
1543 if ((*tag)->tagmsg == NULL) {
1544 err = got_error_from_errno("strdup");
1548 size_t offset = 0, remain = itag->tagmsg_len;
1550 (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1551 if ((*tag)->tagmsg == NULL) {
1552 err = got_error_from_errno("malloc");
1555 while (remain > 0) {
1556 struct imsg imsg_log;
1557 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1560 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1564 if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1565 return got_error(GOT_ERR_PRIVSEP_MSG);
1567 memcpy((*tag)->tagmsg + offset, imsg_log.data,
1569 imsg_free(&imsg_log);
1573 (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1578 err = got_error(GOT_ERR_PRIVSEP_MSG);
1587 const struct got_error *
1588 got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1589 struct got_packidx *packidx)
1591 const struct got_error *err = NULL;
1592 struct got_imsg_packidx ipackidx;
1593 struct got_imsg_pack ipack;
1596 ipackidx.len = packidx->len;
1597 fd = dup(packidx->fd);
1599 return got_error_from_errno("dup");
1601 if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
1602 sizeof(ipackidx)) == -1) {
1603 err = got_error_from_errno("imsg_compose PACKIDX");
1608 if (strlcpy(ipack.path_packfile, pack->path_packfile,
1609 sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
1610 return got_error(GOT_ERR_NO_SPACE);
1611 ipack.filesize = pack->filesize;
1615 return got_error_from_errno("dup");
1617 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
1619 err = got_error_from_errno("imsg_compose PACK");
1624 return flush_imsg(ibuf);
1627 const struct got_error *
1628 got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
1629 struct got_object_id *id)
1631 struct got_imsg_packed_object iobj;
1634 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
1636 if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
1637 &iobj, sizeof(iobj)) == -1)
1638 return got_error_from_errno("imsg_compose "
1639 "PACKED_OBJECT_REQUEST");
1641 return flush_imsg(ibuf);
1644 const struct got_error *
1645 got_privsep_send_gitconfig_parse_req(struct imsgbuf *ibuf, int fd)
1647 const struct got_error *err = NULL;
1649 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_PARSE_REQUEST, 0, 0, fd,
1651 err = got_error_from_errno("imsg_compose "
1652 "GITCONFIG_PARSE_REQUEST");
1657 return flush_imsg(ibuf);
1660 const struct got_error *
1661 got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *ibuf)
1663 if (imsg_compose(ibuf,
1664 GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST, 0, 0, -1,
1666 return got_error_from_errno("imsg_compose "
1667 "GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST");
1669 return flush_imsg(ibuf);
1672 const struct got_error *
1673 got_privsep_send_gitconfig_author_name_req(struct imsgbuf *ibuf)
1675 if (imsg_compose(ibuf,
1676 GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST, 0, 0, -1, NULL, 0) == -1)
1677 return got_error_from_errno("imsg_compose "
1678 "GITCONFIG_AUTHOR_NAME_REQUEST");
1680 return flush_imsg(ibuf);
1683 const struct got_error *
1684 got_privsep_send_gitconfig_author_email_req(struct imsgbuf *ibuf)
1686 if (imsg_compose(ibuf,
1687 GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST, 0, 0, -1, NULL, 0) == -1)
1688 return got_error_from_errno("imsg_compose "
1689 "GITCONFIG_AUTHOR_EMAIL_REQUEST");
1691 return flush_imsg(ibuf);
1694 const struct got_error *
1695 got_privsep_send_gitconfig_remotes_req(struct imsgbuf *ibuf)
1697 if (imsg_compose(ibuf,
1698 GOT_IMSG_GITCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
1699 return got_error_from_errno("imsg_compose "
1700 "GITCONFIG_REMOTE_REQUEST");
1702 return flush_imsg(ibuf);
1705 const struct got_error *
1706 got_privsep_send_gitconfig_owner_req(struct imsgbuf *ibuf)
1708 if (imsg_compose(ibuf,
1709 GOT_IMSG_GITCONFIG_OWNER_REQUEST, 0, 0, -1, NULL, 0) == -1)
1710 return got_error_from_errno("imsg_compose "
1711 "GITCONFIG_OWNER_REQUEST");
1713 return flush_imsg(ibuf);
1716 const struct got_error *
1717 got_privsep_recv_gitconfig_str(char **str, struct imsgbuf *ibuf)
1719 const struct got_error *err = NULL;
1722 const size_t min_datalen = 0;
1726 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1729 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1731 switch (imsg.hdr.type) {
1732 case GOT_IMSG_GITCONFIG_STR_VAL:
1735 *str = malloc(datalen);
1737 err = got_error_from_errno("malloc");
1740 if (strlcpy(*str, imsg.data, datalen) >= datalen)
1741 err = got_error(GOT_ERR_NO_SPACE);
1744 err = got_error(GOT_ERR_PRIVSEP_MSG);
1752 const struct got_error *
1753 got_privsep_recv_gitconfig_int(int *val, struct imsgbuf *ibuf)
1755 const struct got_error *err = NULL;
1758 const size_t min_datalen =
1759 MIN(sizeof(struct got_imsg_error), sizeof(int));
1763 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1766 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1768 switch (imsg.hdr.type) {
1769 case GOT_IMSG_GITCONFIG_INT_VAL:
1770 if (datalen != sizeof(*val)) {
1771 err = got_error(GOT_ERR_PRIVSEP_LEN);
1774 memcpy(val, imsg.data, sizeof(*val));
1777 err = got_error(GOT_ERR_PRIVSEP_MSG);
1785 const struct got_error *
1786 got_privsep_recv_gitconfig_remotes(struct got_remote_repo **remotes,
1787 int *nremotes, struct imsgbuf *ibuf)
1789 const struct got_error *err = NULL;
1792 struct got_imsg_remotes iremotes;
1793 struct got_imsg_remote iremote;
1797 iremotes.nremotes = 0;
1799 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremotes));
1802 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1804 switch (imsg.hdr.type) {
1805 case GOT_IMSG_GITCONFIG_REMOTES:
1806 if (datalen != sizeof(iremotes)) {
1807 err = got_error(GOT_ERR_PRIVSEP_LEN);
1810 memcpy(&iremotes, imsg.data, sizeof(iremotes));
1811 if (iremotes.nremotes == 0) {
1818 return got_error(GOT_ERR_PRIVSEP_MSG);
1823 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(**remotes));
1824 if (*remotes == NULL)
1825 return got_error_from_errno("recallocarray");
1827 while (*nremotes < iremotes.nremotes) {
1828 struct got_remote_repo *remote;
1830 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremote));
1833 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1835 switch (imsg.hdr.type) {
1836 case GOT_IMSG_GITCONFIG_REMOTE:
1837 remote = &(*remotes)[*nremotes];
1838 if (datalen < sizeof(iremote)) {
1839 err = got_error(GOT_ERR_PRIVSEP_LEN);
1842 memcpy(&iremote, imsg.data, sizeof(iremote));
1843 if (iremote.name_len == 0 || iremote.url_len == 0 ||
1844 (sizeof(iremote) + iremote.name_len +
1845 iremote.url_len) > datalen) {
1846 err = got_error(GOT_ERR_PRIVSEP_LEN);
1849 remote->name = strndup(imsg.data + sizeof(iremote),
1851 if (remote->name == NULL) {
1852 err = got_error_from_errno("strndup");
1855 remote->url = strndup(imsg.data + sizeof(iremote) +
1856 iremote.name_len, iremote.url_len);
1857 if (remote->url == NULL) {
1858 err = got_error_from_errno("strndup");
1862 remote->mirror_references = iremote.mirror_references;
1866 err = got_error(GOT_ERR_PRIVSEP_MSG);
1877 for (i = 0; i < *nremotes; i++) {
1878 free((*remotes)[i].name);
1879 free((*remotes)[i].url);
1888 const struct got_error *
1889 got_privsep_send_commit_traversal_request(struct imsgbuf *ibuf,
1890 struct got_object_id *id, int idx, const char *path)
1892 const struct got_error *err = NULL;
1894 size_t path_len = strlen(path) + 1;
1896 wbuf = imsg_create(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_REQUEST, 0, 0,
1897 sizeof(struct got_imsg_commit_traversal_request) + path_len);
1899 return got_error_from_errno(
1900 "imsg_create COMMIT_TRAVERSAL_REQUEST");
1901 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
1902 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
1906 if (imsg_add(wbuf, &idx, sizeof(idx)) == -1) {
1907 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
1911 if (imsg_add(wbuf, path, path_len) == -1) {
1912 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
1918 imsg_close(ibuf, wbuf);
1920 return flush_imsg(ibuf);
1923 const struct got_error *
1924 got_privsep_recv_traversed_commits(struct got_commit_object **changed_commit,
1925 struct got_object_id **changed_commit_id,
1926 struct got_object_id_queue *commit_ids, struct imsgbuf *ibuf)
1928 const struct got_error *err = NULL;
1930 struct got_imsg_traversed_commits *icommits;
1934 *changed_commit = NULL;
1935 *changed_commit_id = NULL;
1938 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1942 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1943 switch (imsg.hdr.type) {
1944 case GOT_IMSG_TRAVERSED_COMMITS:
1945 icommits = imsg.data;
1946 if (datalen != sizeof(*icommits) +
1947 icommits->ncommits * SHA1_DIGEST_LENGTH) {
1948 err = got_error(GOT_ERR_PRIVSEP_LEN);
1951 for (i = 0; i < icommits->ncommits; i++) {
1952 struct got_object_qid *qid;
1953 uint8_t *sha1 = (uint8_t *)imsg.data +
1954 sizeof(*icommits) + i * SHA1_DIGEST_LENGTH;
1955 err = got_object_qid_alloc_partial(&qid);
1958 memcpy(qid->id->sha1, sha1, SHA1_DIGEST_LENGTH);
1959 SIMPLEQ_INSERT_TAIL(commit_ids, qid, entry);
1961 /* The last commit may contain a change. */
1962 if (i == icommits->ncommits - 1) {
1963 *changed_commit_id =
1964 got_object_id_dup(qid->id);
1965 if (*changed_commit_id == NULL) {
1966 err = got_error_from_errno(
1967 "got_object_id_dup");
1973 case GOT_IMSG_COMMIT:
1974 if (*changed_commit_id == NULL) {
1975 err = got_error(GOT_ERR_PRIVSEP_MSG);
1978 err = get_commit_from_imsg(changed_commit, &imsg,
1981 case GOT_IMSG_COMMIT_TRAVERSAL_DONE:
1985 err = got_error(GOT_ERR_PRIVSEP_MSG);
1995 got_object_id_queue_free(commit_ids);
1999 const struct got_error *
2000 got_privsep_unveil_exec_helpers(void)
2002 const char *helpers[] = {
2003 GOT_PATH_PROG_READ_PACK,
2004 GOT_PATH_PROG_READ_OBJECT,
2005 GOT_PATH_PROG_READ_COMMIT,
2006 GOT_PATH_PROG_READ_TREE,
2007 GOT_PATH_PROG_READ_BLOB,
2008 GOT_PATH_PROG_READ_TAG,
2009 GOT_PATH_PROG_READ_GITCONFIG,
2010 GOT_PATH_PROG_FETCH_PACK,
2011 GOT_PATH_PROG_INDEX_PACK,
2015 for (i = 0; i < nitems(helpers); i++) {
2016 if (unveil(helpers[i], "x") == 0)
2018 return got_error_from_errno2("unveil", helpers[i]);
2025 got_privsep_exec_child(int imsg_fds[2], const char *path, const char *repo_path)
2027 if (close(imsg_fds[0]) != 0) {
2028 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2032 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
2033 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2036 if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
2037 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2041 if (execl(path, path, repo_path, (char *)NULL) == -1) {
2042 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,