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, uint8_t *pack_sha1, struct imsgbuf *ibuf)
573 const struct got_error *err = NULL;
576 struct got_imsg_fetch_symrefs *isymrefs = NULL;
584 *server_progress = NULL;
586 memset(pack_sha1, 0, SHA1_DIGEST_LENGTH);
588 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
592 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
593 switch (imsg.hdr.type) {
595 if (datalen < sizeof(struct got_imsg_error)) {
596 err = got_error(GOT_ERR_PRIVSEP_LEN);
599 err = recv_imsg_error(&imsg, datalen);
601 case GOT_IMSG_FETCH_SYMREFS:
602 if (datalen < sizeof(*isymrefs)) {
603 err = got_error(GOT_ERR_PRIVSEP_LEN);
606 if (isymrefs != NULL) {
607 err = got_error(GOT_ERR_PRIVSEP_MSG);
610 isymrefs = (struct got_imsg_fetch_symrefs *)imsg.data;
611 off = sizeof(*isymrefs);
612 remain = datalen - off;
613 for (n = 0; n < isymrefs->nsymrefs; n++) {
614 struct got_imsg_fetch_symref *s;
616 if (remain < sizeof(struct got_imsg_fetch_symref)) {
617 err = got_error(GOT_ERR_PRIVSEP_LEN);
620 s = (struct got_imsg_fetch_symref *)(imsg.data + off);
622 remain -= sizeof(*s);
623 if (remain < s->name_len) {
624 err = got_error(GOT_ERR_PRIVSEP_LEN);
627 name = strndup(imsg.data + off, s->name_len);
629 err = got_error_from_errno("strndup");
633 remain -= s->name_len;
634 if (remain < s->target_len) {
635 err = got_error(GOT_ERR_PRIVSEP_LEN);
639 target = strndup(imsg.data + off, s->target_len);
640 if (target == NULL) {
641 err = got_error_from_errno("strndup");
645 off += s->target_len;
646 remain -= s->target_len;
647 err = got_pathlist_append(symrefs, name, target);
655 case GOT_IMSG_FETCH_REF:
656 if (datalen <= SHA1_DIGEST_LENGTH) {
657 err = got_error(GOT_ERR_PRIVSEP_MSG);
660 *id = malloc(sizeof(**id));
662 err = got_error_from_errno("malloc");
665 memcpy((*id)->sha1, imsg.data, SHA1_DIGEST_LENGTH);
666 *refname = strndup(imsg.data + SHA1_DIGEST_LENGTH,
667 datalen - SHA1_DIGEST_LENGTH);
668 if (*refname == NULL) {
669 err = got_error_from_errno("strndup");
673 case GOT_IMSG_FETCH_SERVER_PROGRESS:
675 err = got_error(GOT_ERR_PRIVSEP_LEN);
678 *server_progress = strndup(imsg.data, datalen);
679 if (*server_progress == NULL) {
680 err = got_error_from_errno("strndup");
683 for (i = 0; i < datalen; i++) {
684 if (!isprint((unsigned char)(*server_progress)[i]) &&
685 !isspace((unsigned char)(*server_progress)[i])) {
686 err = got_error(GOT_ERR_PRIVSEP_MSG);
687 free(*server_progress);
688 *server_progress = NULL;
693 case GOT_IMSG_FETCH_DOWNLOAD_PROGRESS:
694 if (datalen < sizeof(*packfile_size)) {
695 err = got_error(GOT_ERR_PRIVSEP_MSG);
698 memcpy(packfile_size, imsg.data, sizeof(*packfile_size));
700 case GOT_IMSG_FETCH_DONE:
701 if (datalen != SHA1_DIGEST_LENGTH) {
702 err = got_error(GOT_ERR_PRIVSEP_MSG);
705 memcpy(pack_sha1, imsg.data, SHA1_DIGEST_LENGTH);
709 err = got_error(GOT_ERR_PRIVSEP_MSG);
723 const struct got_error *
724 got_privsep_send_index_pack_req(struct imsgbuf *ibuf, uint8_t *pack_sha1,
727 const struct got_error *err = NULL;
729 /* Keep in sync with struct got_imsg_index_pack_request */
730 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_REQUEST, 0, 0, fd,
731 pack_sha1, SHA1_DIGEST_LENGTH) == -1) {
732 err = got_error_from_errno("imsg_compose INDEX_REQUEST");
736 return flush_imsg(ibuf);
739 const struct got_error *
740 got_privsep_send_index_pack_outfd(struct imsgbuf *ibuf, int fd)
742 return send_fd(ibuf, GOT_IMSG_IDXPACK_OUTFD, fd);
745 const struct got_error *
746 got_privsep_recv_index_progress(int *done, int *nobj_total,
747 int *nobj_indexed, int *nobj_loose, int *nobj_resolved,
748 struct imsgbuf *ibuf)
750 const struct got_error *err = NULL;
752 struct got_imsg_index_pack_progress *iprogress;
760 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
764 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
765 switch (imsg.hdr.type) {
767 if (datalen < sizeof(struct got_imsg_error)) {
768 err = got_error(GOT_ERR_PRIVSEP_LEN);
771 err = recv_imsg_error(&imsg, datalen);
773 case GOT_IMSG_IDXPACK_PROGRESS:
774 if (datalen < sizeof(*iprogress)) {
775 err = got_error(GOT_ERR_PRIVSEP_LEN);
778 iprogress = (struct got_imsg_index_pack_progress *)imsg.data;
779 *nobj_total = iprogress->nobj_total;
780 *nobj_indexed = iprogress->nobj_indexed;
781 *nobj_loose = iprogress->nobj_loose;
782 *nobj_resolved = iprogress->nobj_resolved;
784 case GOT_IMSG_IDXPACK_DONE:
786 err = got_error(GOT_ERR_PRIVSEP_LEN);
792 err = got_error(GOT_ERR_PRIVSEP_MSG);
800 const struct got_error *
801 got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
802 struct imsgbuf *ibuf)
804 const struct got_error *err = NULL;
805 struct got_imsg_object *iobj;
806 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
808 if (datalen != sizeof(*iobj))
809 return got_error(GOT_ERR_PRIVSEP_LEN);
812 *obj = calloc(1, sizeof(**obj));
814 return got_error_from_errno("calloc");
816 memcpy((*obj)->id.sha1, iobj->id, SHA1_DIGEST_LENGTH);
817 (*obj)->type = iobj->type;
818 (*obj)->flags = iobj->flags;
819 (*obj)->hdrlen = iobj->hdrlen;
820 (*obj)->size = iobj->size;
821 /* path_packfile is handled by caller */
822 if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
823 (*obj)->pack_offset = iobj->pack_offset;
824 (*obj)->pack_idx = iobj->pack_idx;
830 const struct got_error *
831 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
833 const struct got_error *err = NULL;
835 const size_t min_datalen =
836 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
840 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
844 switch (imsg.hdr.type) {
845 case GOT_IMSG_OBJECT:
846 err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
849 err = got_error(GOT_ERR_PRIVSEP_MSG);
858 static const struct got_error *
859 send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
862 const struct got_error *err = NULL;
863 size_t offset, remain;
868 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
870 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
871 commit->logmsg + offset, n) == -1) {
872 err = got_error_from_errno("imsg_compose "
877 err = flush_imsg(ibuf);
888 const struct got_error *
889 got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
891 const struct got_error *err = NULL;
892 struct got_imsg_commit_object *icommit;
895 struct got_object_qid *qid;
896 size_t author_len = strlen(commit->author);
897 size_t committer_len = strlen(commit->committer);
898 size_t logmsg_len = strlen(commit->logmsg);
900 total = sizeof(*icommit) + author_len + committer_len +
901 commit->nparents * SHA1_DIGEST_LENGTH;
905 return got_error_from_errno("malloc");
907 icommit = (struct got_imsg_commit_object *)buf;
908 memcpy(icommit->tree_id, commit->tree_id->sha1,
909 sizeof(icommit->tree_id));
910 icommit->author_len = author_len;
911 icommit->author_time = commit->author_time;
912 icommit->author_gmtoff = commit->author_gmtoff;
913 icommit->committer_len = committer_len;
914 icommit->committer_time = commit->committer_time;
915 icommit->committer_gmtoff = commit->committer_gmtoff;
916 icommit->logmsg_len = logmsg_len;
917 icommit->nparents = commit->nparents;
919 len = sizeof(*icommit);
920 memcpy(buf + len, commit->author, author_len);
922 memcpy(buf + len, commit->committer, committer_len);
923 len += committer_len;
924 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
925 memcpy(buf + len, qid->id, SHA1_DIGEST_LENGTH);
926 len += SHA1_DIGEST_LENGTH;
929 if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
930 err = got_error_from_errno("imsg_compose COMMIT");
934 if (logmsg_len == 0 ||
935 logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
936 err = flush_imsg(ibuf);
940 err = send_commit_logmsg(ibuf, commit, logmsg_len);
946 static const struct got_error *
947 get_commit_from_imsg(struct got_commit_object **commit,
948 struct imsg *imsg, size_t datalen, struct imsgbuf *ibuf)
950 const struct got_error *err = NULL;
951 struct got_imsg_commit_object *icommit;
955 if (datalen < sizeof(*icommit))
956 return got_error(GOT_ERR_PRIVSEP_LEN);
958 icommit = imsg->data;
959 if (datalen != sizeof(*icommit) + icommit->author_len +
960 icommit->committer_len +
961 icommit->nparents * SHA1_DIGEST_LENGTH)
962 return got_error(GOT_ERR_PRIVSEP_LEN);
964 if (icommit->nparents < 0)
965 return got_error(GOT_ERR_PRIVSEP_LEN);
967 len += sizeof(*icommit);
969 *commit = got_object_commit_alloc_partial();
971 return got_error_from_errno(
972 "got_object_commit_alloc_partial");
974 memcpy((*commit)->tree_id->sha1, icommit->tree_id,
976 (*commit)->author_time = icommit->author_time;
977 (*commit)->author_gmtoff = icommit->author_gmtoff;
978 (*commit)->committer_time = icommit->committer_time;
979 (*commit)->committer_gmtoff = icommit->committer_gmtoff;
981 if (icommit->author_len == 0) {
982 (*commit)->author = strdup("");
983 if ((*commit)->author == NULL) {
984 err = got_error_from_errno("strdup");
988 (*commit)->author = malloc(icommit->author_len + 1);
989 if ((*commit)->author == NULL) {
990 err = got_error_from_errno("malloc");
993 memcpy((*commit)->author, imsg->data + len,
994 icommit->author_len);
995 (*commit)->author[icommit->author_len] = '\0';
997 len += icommit->author_len;
999 if (icommit->committer_len == 0) {
1000 (*commit)->committer = strdup("");
1001 if ((*commit)->committer == NULL) {
1002 err = got_error_from_errno("strdup");
1006 (*commit)->committer =
1007 malloc(icommit->committer_len + 1);
1008 if ((*commit)->committer == NULL) {
1009 err = got_error_from_errno("malloc");
1012 memcpy((*commit)->committer, imsg->data + len,
1013 icommit->committer_len);
1014 (*commit)->committer[icommit->committer_len] = '\0';
1016 len += icommit->committer_len;
1018 if (icommit->logmsg_len == 0) {
1019 (*commit)->logmsg = strdup("");
1020 if ((*commit)->logmsg == NULL) {
1021 err = got_error_from_errno("strdup");
1025 size_t offset = 0, remain = icommit->logmsg_len;
1027 (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
1028 if ((*commit)->logmsg == NULL) {
1029 err = got_error_from_errno("malloc");
1032 while (remain > 0) {
1033 struct imsg imsg_log;
1034 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1037 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1041 if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG) {
1042 err = got_error(GOT_ERR_PRIVSEP_MSG);
1046 memcpy((*commit)->logmsg + offset,
1048 imsg_free(&imsg_log);
1052 (*commit)->logmsg[icommit->logmsg_len] = '\0';
1055 for (i = 0; i < icommit->nparents; i++) {
1056 struct got_object_qid *qid;
1058 err = got_object_qid_alloc_partial(&qid);
1061 memcpy(qid->id, imsg->data + len +
1062 i * SHA1_DIGEST_LENGTH, sizeof(*qid->id));
1063 SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
1064 (*commit)->nparents++;
1068 got_object_commit_close(*commit);
1074 const struct got_error *
1075 got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
1077 const struct got_error *err = NULL;
1080 const size_t min_datalen =
1081 MIN(sizeof(struct got_imsg_error),
1082 sizeof(struct got_imsg_commit_object));
1086 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1090 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1092 switch (imsg.hdr.type) {
1093 case GOT_IMSG_COMMIT:
1094 err = get_commit_from_imsg(commit, &imsg, datalen, ibuf);
1097 err = got_error(GOT_ERR_PRIVSEP_MSG);
1106 const struct got_error *
1107 got_privsep_send_tree(struct imsgbuf *ibuf, struct got_pathlist_head *entries,
1110 const struct got_error *err = NULL;
1111 struct got_imsg_tree_object itree;
1112 struct got_pathlist_entry *pe;
1114 int nimsg; /* number of imsg queued in ibuf */
1116 itree.nentries = nentries;
1117 if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
1119 return got_error_from_errno("imsg_compose TREE");
1121 totlen = sizeof(itree);
1123 TAILQ_FOREACH(pe, entries, entry) {
1124 const char *name = pe->path;
1125 struct got_parsed_tree_entry *pte = pe->data;
1127 size_t namelen = strlen(name);
1128 size_t len = sizeof(struct got_imsg_tree_entry) + namelen;
1130 if (len > MAX_IMSGSIZE)
1131 return got_error(GOT_ERR_NO_SPACE);
1134 if (totlen + len >= MAX_IMSGSIZE - (IMSG_HEADER_SIZE * nimsg)) {
1135 err = flush_imsg(ibuf);
1141 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_ENTRY, 0, 0, len);
1143 return got_error_from_errno("imsg_create TREE_ENTRY");
1145 /* Keep in sync with struct got_imsg_tree_object definition! */
1146 if (imsg_add(wbuf, pte->id, SHA1_DIGEST_LENGTH) == -1) {
1147 err = got_error_from_errno("imsg_add TREE_ENTRY");
1151 if (imsg_add(wbuf, &pte->mode, sizeof(pte->mode)) == -1) {
1152 err = got_error_from_errno("imsg_add TREE_ENTRY");
1157 if (imsg_add(wbuf, name, namelen) == -1) {
1158 err = got_error_from_errno("imsg_add TREE_ENTRY");
1164 imsg_close(ibuf, wbuf);
1169 return flush_imsg(ibuf);
1172 const struct got_error *
1173 got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
1175 const struct got_error *err = NULL;
1176 const size_t min_datalen =
1177 MIN(sizeof(struct got_imsg_error),
1178 sizeof(struct got_imsg_tree_object));
1179 struct got_imsg_tree_object *itree;
1184 err = read_imsg(ibuf);
1192 struct got_imsg_tree_entry *ite;
1193 struct got_tree_entry *te = NULL;
1195 n = imsg_get(ibuf, &imsg);
1197 if (*tree && (*tree)->nentries != nentries)
1202 if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen)
1203 return got_error(GOT_ERR_PRIVSEP_LEN);
1205 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1207 switch (imsg.hdr.type) {
1208 case GOT_IMSG_ERROR:
1209 err = recv_imsg_error(&imsg, datalen);
1212 /* This message should only appear once. */
1213 if (*tree != NULL) {
1214 err = got_error(GOT_ERR_PRIVSEP_MSG);
1217 if (datalen != sizeof(*itree)) {
1218 err = got_error(GOT_ERR_PRIVSEP_LEN);
1222 *tree = malloc(sizeof(**tree));
1223 if (*tree == NULL) {
1224 err = got_error_from_errno("malloc");
1227 (*tree)->entries = calloc(itree->nentries,
1228 sizeof(struct got_tree_entry));
1229 if ((*tree)->entries == NULL) {
1230 err = got_error_from_errno("malloc");
1233 (*tree)->nentries = itree->nentries;
1234 (*tree)->refcnt = 0;
1236 case GOT_IMSG_TREE_ENTRY:
1237 /* This message should be preceeded by GOT_IMSG_TREE. */
1238 if (*tree == NULL) {
1239 err = got_error(GOT_ERR_PRIVSEP_MSG);
1242 if (datalen < sizeof(*ite) || datalen > MAX_IMSGSIZE) {
1243 err = got_error(GOT_ERR_PRIVSEP_LEN);
1247 /* Remaining data contains the entry's name. */
1248 datalen -= sizeof(*ite);
1249 if (datalen == 0 || datalen > MAX_IMSGSIZE) {
1250 err = got_error(GOT_ERR_PRIVSEP_LEN);
1255 if (datalen + 1 > sizeof(te->name)) {
1256 err = got_error(GOT_ERR_NO_SPACE);
1259 te = &(*tree)->entries[nentries];
1260 memcpy(te->name, imsg.data + sizeof(*ite), datalen);
1261 te->name[datalen] = '\0';
1263 memcpy(te->id.sha1, ite->id, SHA1_DIGEST_LENGTH);
1264 te->mode = ite->mode;
1269 err = got_error(GOT_ERR_PRIVSEP_MSG);
1276 if (*tree && (*tree)->nentries != nentries) {
1278 err = got_error(GOT_ERR_PRIVSEP_LEN);
1279 got_object_tree_close(*tree);
1286 const struct got_error *
1287 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
1288 const uint8_t *data)
1290 struct got_imsg_blob iblob;
1293 iblob.hdrlen = hdrlen;
1298 if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
1299 return got_error(GOT_ERR_NO_SPACE);
1301 buf = malloc(sizeof(iblob) + size);
1303 return got_error_from_errno("malloc");
1305 memcpy(buf, &iblob, sizeof(iblob));
1306 memcpy(buf + sizeof(iblob), data, size);
1307 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
1308 sizeof(iblob) + size) == -1) {
1310 return got_error_from_errno("imsg_compose BLOB");
1314 /* Data has already been written to file descriptor. */
1315 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
1316 sizeof(iblob)) == -1)
1317 return got_error_from_errno("imsg_compose BLOB");
1321 return flush_imsg(ibuf);
1324 const struct got_error *
1325 got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1326 struct imsgbuf *ibuf)
1328 const struct got_error *err = NULL;
1330 struct got_imsg_blob *iblob;
1335 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1339 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1341 switch (imsg.hdr.type) {
1343 if (datalen < sizeof(*iblob)) {
1344 err = got_error(GOT_ERR_PRIVSEP_LEN);
1348 *size = iblob->size;
1349 *hdrlen = iblob->hdrlen;
1351 if (datalen == sizeof(*iblob)) {
1352 /* Data has been written to file descriptor. */
1356 if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX) {
1357 err = got_error(GOT_ERR_PRIVSEP_LEN);
1361 *outbuf = malloc(*size);
1362 if (*outbuf == NULL) {
1363 err = got_error_from_errno("malloc");
1366 memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
1369 err = got_error(GOT_ERR_PRIVSEP_MSG);
1378 static const struct got_error *
1379 send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
1381 const struct got_error *err = NULL;
1382 size_t offset, remain;
1385 remain = tagmsg_len;
1386 while (remain > 0) {
1387 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1389 if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
1390 tag->tagmsg + offset, n) == -1) {
1391 err = got_error_from_errno("imsg_compose TAG_TAGMSG");
1395 err = flush_imsg(ibuf);
1406 const struct got_error *
1407 got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
1409 const struct got_error *err = NULL;
1410 struct got_imsg_tag_object *itag;
1413 size_t tag_len = strlen(tag->tag);
1414 size_t tagger_len = strlen(tag->tagger);
1415 size_t tagmsg_len = strlen(tag->tagmsg);
1417 total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
1419 buf = malloc(total);
1421 return got_error_from_errno("malloc");
1423 itag = (struct got_imsg_tag_object *)buf;
1424 memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
1425 itag->obj_type = tag->obj_type;
1426 itag->tag_len = tag_len;
1427 itag->tagger_len = tagger_len;
1428 itag->tagger_time = tag->tagger_time;
1429 itag->tagger_gmtoff = tag->tagger_gmtoff;
1430 itag->tagmsg_len = tagmsg_len;
1432 len = sizeof(*itag);
1433 memcpy(buf + len, tag->tag, tag_len);
1435 memcpy(buf + len, tag->tagger, tagger_len);
1438 if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
1439 err = got_error_from_errno("imsg_compose TAG");
1443 if (tagmsg_len == 0 ||
1444 tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1445 err = flush_imsg(ibuf);
1449 err = send_tagmsg(ibuf, tag, tagmsg_len);
1455 const struct got_error *
1456 got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
1458 const struct got_error *err = NULL;
1460 struct got_imsg_tag_object *itag;
1461 size_t len, datalen;
1462 const size_t min_datalen =
1463 MIN(sizeof(struct got_imsg_error),
1464 sizeof(struct got_imsg_tag_object));
1468 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1472 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1475 switch (imsg.hdr.type) {
1477 if (datalen < sizeof(*itag)) {
1478 err = got_error(GOT_ERR_PRIVSEP_LEN);
1482 if (datalen != sizeof(*itag) + itag->tag_len +
1484 err = got_error(GOT_ERR_PRIVSEP_LEN);
1487 len += sizeof(*itag);
1489 *tag = calloc(1, sizeof(**tag));
1491 err = got_error_from_errno("calloc");
1495 memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
1497 if (itag->tag_len == 0) {
1498 (*tag)->tag = strdup("");
1499 if ((*tag)->tag == NULL) {
1500 err = got_error_from_errno("strdup");
1504 (*tag)->tag = malloc(itag->tag_len + 1);
1505 if ((*tag)->tag == NULL) {
1506 err = got_error_from_errno("malloc");
1509 memcpy((*tag)->tag, imsg.data + len,
1511 (*tag)->tag[itag->tag_len] = '\0';
1513 len += itag->tag_len;
1515 (*tag)->obj_type = itag->obj_type;
1516 (*tag)->tagger_time = itag->tagger_time;
1517 (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1519 if (itag->tagger_len == 0) {
1520 (*tag)->tagger = strdup("");
1521 if ((*tag)->tagger == NULL) {
1522 err = got_error_from_errno("strdup");
1526 (*tag)->tagger = malloc(itag->tagger_len + 1);
1527 if ((*tag)->tagger == NULL) {
1528 err = got_error_from_errno("malloc");
1531 memcpy((*tag)->tagger, imsg.data + len,
1533 (*tag)->tagger[itag->tagger_len] = '\0';
1535 len += itag->tagger_len;
1537 if (itag->tagmsg_len == 0) {
1538 (*tag)->tagmsg = strdup("");
1539 if ((*tag)->tagmsg == NULL) {
1540 err = got_error_from_errno("strdup");
1544 size_t offset = 0, remain = itag->tagmsg_len;
1546 (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1547 if ((*tag)->tagmsg == NULL) {
1548 err = got_error_from_errno("malloc");
1551 while (remain > 0) {
1552 struct imsg imsg_log;
1553 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1556 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1560 if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1561 return got_error(GOT_ERR_PRIVSEP_MSG);
1563 memcpy((*tag)->tagmsg + offset, imsg_log.data,
1565 imsg_free(&imsg_log);
1569 (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1574 err = got_error(GOT_ERR_PRIVSEP_MSG);
1583 const struct got_error *
1584 got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1585 struct got_packidx *packidx)
1587 const struct got_error *err = NULL;
1588 struct got_imsg_packidx ipackidx;
1589 struct got_imsg_pack ipack;
1592 ipackidx.len = packidx->len;
1593 fd = dup(packidx->fd);
1595 return got_error_from_errno("dup");
1597 if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
1598 sizeof(ipackidx)) == -1) {
1599 err = got_error_from_errno("imsg_compose PACKIDX");
1604 if (strlcpy(ipack.path_packfile, pack->path_packfile,
1605 sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
1606 return got_error(GOT_ERR_NO_SPACE);
1607 ipack.filesize = pack->filesize;
1611 return got_error_from_errno("dup");
1613 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
1615 err = got_error_from_errno("imsg_compose PACK");
1620 return flush_imsg(ibuf);
1623 const struct got_error *
1624 got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
1625 struct got_object_id *id)
1627 struct got_imsg_packed_object iobj;
1630 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
1632 if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
1633 &iobj, sizeof(iobj)) == -1)
1634 return got_error_from_errno("imsg_compose "
1635 "PACKED_OBJECT_REQUEST");
1637 return flush_imsg(ibuf);
1640 const struct got_error *
1641 got_privsep_send_gitconfig_parse_req(struct imsgbuf *ibuf, int fd)
1643 const struct got_error *err = NULL;
1645 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_PARSE_REQUEST, 0, 0, fd,
1647 err = got_error_from_errno("imsg_compose "
1648 "GITCONFIG_PARSE_REQUEST");
1653 return flush_imsg(ibuf);
1656 const struct got_error *
1657 got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *ibuf)
1659 if (imsg_compose(ibuf,
1660 GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST, 0, 0, -1,
1662 return got_error_from_errno("imsg_compose "
1663 "GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST");
1665 return flush_imsg(ibuf);
1668 const struct got_error *
1669 got_privsep_send_gitconfig_author_name_req(struct imsgbuf *ibuf)
1671 if (imsg_compose(ibuf,
1672 GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST, 0, 0, -1, NULL, 0) == -1)
1673 return got_error_from_errno("imsg_compose "
1674 "GITCONFIG_AUTHOR_NAME_REQUEST");
1676 return flush_imsg(ibuf);
1679 const struct got_error *
1680 got_privsep_send_gitconfig_author_email_req(struct imsgbuf *ibuf)
1682 if (imsg_compose(ibuf,
1683 GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST, 0, 0, -1, NULL, 0) == -1)
1684 return got_error_from_errno("imsg_compose "
1685 "GITCONFIG_AUTHOR_EMAIL_REQUEST");
1687 return flush_imsg(ibuf);
1690 const struct got_error *
1691 got_privsep_send_gitconfig_remotes_req(struct imsgbuf *ibuf)
1693 if (imsg_compose(ibuf,
1694 GOT_IMSG_GITCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
1695 return got_error_from_errno("imsg_compose "
1696 "GITCONFIG_REMOTE_REQUEST");
1698 return flush_imsg(ibuf);
1701 const struct got_error *
1702 got_privsep_send_gitconfig_owner_req(struct imsgbuf *ibuf)
1704 if (imsg_compose(ibuf,
1705 GOT_IMSG_GITCONFIG_OWNER_REQUEST, 0, 0, -1, NULL, 0) == -1)
1706 return got_error_from_errno("imsg_compose "
1707 "GITCONFIG_OWNER_REQUEST");
1709 return flush_imsg(ibuf);
1712 const struct got_error *
1713 got_privsep_recv_gitconfig_str(char **str, struct imsgbuf *ibuf)
1715 const struct got_error *err = NULL;
1718 const size_t min_datalen = 0;
1722 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1725 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1727 switch (imsg.hdr.type) {
1728 case GOT_IMSG_GITCONFIG_STR_VAL:
1731 *str = malloc(datalen);
1733 err = got_error_from_errno("malloc");
1736 if (strlcpy(*str, imsg.data, datalen) >= datalen)
1737 err = got_error(GOT_ERR_NO_SPACE);
1740 err = got_error(GOT_ERR_PRIVSEP_MSG);
1748 const struct got_error *
1749 got_privsep_recv_gitconfig_int(int *val, struct imsgbuf *ibuf)
1751 const struct got_error *err = NULL;
1754 const size_t min_datalen =
1755 MIN(sizeof(struct got_imsg_error), sizeof(int));
1759 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1762 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1764 switch (imsg.hdr.type) {
1765 case GOT_IMSG_GITCONFIG_INT_VAL:
1766 if (datalen != sizeof(*val)) {
1767 err = got_error(GOT_ERR_PRIVSEP_LEN);
1770 memcpy(val, imsg.data, sizeof(*val));
1773 err = got_error(GOT_ERR_PRIVSEP_MSG);
1781 const struct got_error *
1782 got_privsep_recv_gitconfig_remotes(struct got_remote_repo **remotes,
1783 int *nremotes, struct imsgbuf *ibuf)
1785 const struct got_error *err = NULL;
1788 struct got_imsg_remotes iremotes;
1789 struct got_imsg_remote iremote;
1793 iremotes.nremotes = 0;
1795 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremotes));
1798 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1800 switch (imsg.hdr.type) {
1801 case GOT_IMSG_GITCONFIG_REMOTES:
1802 if (datalen != sizeof(iremotes)) {
1803 err = got_error(GOT_ERR_PRIVSEP_LEN);
1806 memcpy(&iremotes, imsg.data, sizeof(iremotes));
1807 if (iremotes.nremotes == 0) {
1814 return got_error(GOT_ERR_PRIVSEP_MSG);
1819 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(**remotes));
1820 if (*remotes == NULL)
1821 return got_error_from_errno("recallocarray");
1823 while (*nremotes < iremotes.nremotes) {
1824 struct got_remote_repo *remote;
1826 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremote));
1829 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1831 switch (imsg.hdr.type) {
1832 case GOT_IMSG_GITCONFIG_REMOTE:
1833 remote = &(*remotes)[*nremotes];
1834 if (datalen < sizeof(iremote)) {
1835 err = got_error(GOT_ERR_PRIVSEP_LEN);
1838 memcpy(&iremote, imsg.data, sizeof(iremote));
1839 if (iremote.name_len == 0 || iremote.url_len == 0 ||
1840 (sizeof(iremote) + iremote.name_len +
1841 iremote.url_len) > datalen) {
1842 err = got_error(GOT_ERR_PRIVSEP_LEN);
1845 remote->name = strndup(imsg.data + sizeof(iremote),
1847 if (remote->name == NULL) {
1848 err = got_error_from_errno("strndup");
1851 remote->url = strndup(imsg.data + sizeof(iremote) +
1852 iremote.name_len, iremote.url_len);
1853 if (remote->url == NULL) {
1854 err = got_error_from_errno("strndup");
1858 remote->mirror_references = iremote.mirror_references;
1862 err = got_error(GOT_ERR_PRIVSEP_MSG);
1873 for (i = 0; i < *nremotes; i++) {
1874 free((*remotes)[i].name);
1875 free((*remotes)[i].url);
1884 const struct got_error *
1885 got_privsep_send_commit_traversal_request(struct imsgbuf *ibuf,
1886 struct got_object_id *id, int idx, const char *path)
1888 const struct got_error *err = NULL;
1890 size_t path_len = strlen(path) + 1;
1892 wbuf = imsg_create(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_REQUEST, 0, 0,
1893 sizeof(struct got_imsg_commit_traversal_request) + path_len);
1895 return got_error_from_errno(
1896 "imsg_create COMMIT_TRAVERSAL_REQUEST");
1897 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
1898 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
1902 if (imsg_add(wbuf, &idx, sizeof(idx)) == -1) {
1903 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
1907 if (imsg_add(wbuf, path, path_len) == -1) {
1908 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
1914 imsg_close(ibuf, wbuf);
1916 return flush_imsg(ibuf);
1919 const struct got_error *
1920 got_privsep_recv_traversed_commits(struct got_commit_object **changed_commit,
1921 struct got_object_id **changed_commit_id,
1922 struct got_object_id_queue *commit_ids, struct imsgbuf *ibuf)
1924 const struct got_error *err = NULL;
1926 struct got_imsg_traversed_commits *icommits;
1930 *changed_commit = NULL;
1931 *changed_commit_id = NULL;
1934 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1938 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1939 switch (imsg.hdr.type) {
1940 case GOT_IMSG_TRAVERSED_COMMITS:
1941 icommits = imsg.data;
1942 if (datalen != sizeof(*icommits) +
1943 icommits->ncommits * SHA1_DIGEST_LENGTH) {
1944 err = got_error(GOT_ERR_PRIVSEP_LEN);
1947 for (i = 0; i < icommits->ncommits; i++) {
1948 struct got_object_qid *qid;
1949 uint8_t *sha1 = (uint8_t *)imsg.data +
1950 sizeof(*icommits) + i * SHA1_DIGEST_LENGTH;
1951 err = got_object_qid_alloc_partial(&qid);
1954 memcpy(qid->id->sha1, sha1, SHA1_DIGEST_LENGTH);
1955 SIMPLEQ_INSERT_TAIL(commit_ids, qid, entry);
1957 /* The last commit may contain a change. */
1958 if (i == icommits->ncommits - 1) {
1959 *changed_commit_id =
1960 got_object_id_dup(qid->id);
1961 if (*changed_commit_id == NULL) {
1962 err = got_error_from_errno(
1963 "got_object_id_dup");
1969 case GOT_IMSG_COMMIT:
1970 if (*changed_commit_id == NULL) {
1971 err = got_error(GOT_ERR_PRIVSEP_MSG);
1974 err = get_commit_from_imsg(changed_commit, &imsg,
1977 case GOT_IMSG_COMMIT_TRAVERSAL_DONE:
1981 err = got_error(GOT_ERR_PRIVSEP_MSG);
1991 got_object_id_queue_free(commit_ids);
1995 const struct got_error *
1996 got_privsep_unveil_exec_helpers(void)
1998 const char *helpers[] = {
1999 GOT_PATH_PROG_READ_PACK,
2000 GOT_PATH_PROG_READ_OBJECT,
2001 GOT_PATH_PROG_READ_COMMIT,
2002 GOT_PATH_PROG_READ_TREE,
2003 GOT_PATH_PROG_READ_BLOB,
2004 GOT_PATH_PROG_READ_TAG,
2005 GOT_PATH_PROG_READ_GITCONFIG,
2006 GOT_PATH_PROG_FETCH_PACK,
2007 GOT_PATH_PROG_INDEX_PACK,
2011 for (i = 0; i < nitems(helpers); i++) {
2012 if (unveil(helpers[i], "x") == 0)
2014 return got_error_from_errno2("unveil", helpers[i]);
2021 got_privsep_exec_child(int imsg_fds[2], const char *path, const char *repo_path)
2023 if (close(imsg_fds[0]) != 0) {
2024 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2028 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
2029 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2032 if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
2033 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2037 if (execl(path, path, repo_path, (char *)NULL) == -1) {
2038 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,