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) {
1204 err = got_error(GOT_ERR_PRIVSEP_LEN);
1208 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1210 switch (imsg.hdr.type) {
1211 case GOT_IMSG_ERROR:
1212 err = recv_imsg_error(&imsg, datalen);
1215 /* This message should only appear once. */
1216 if (*tree != NULL) {
1217 err = got_error(GOT_ERR_PRIVSEP_MSG);
1220 if (datalen != sizeof(*itree)) {
1221 err = got_error(GOT_ERR_PRIVSEP_LEN);
1225 *tree = malloc(sizeof(**tree));
1226 if (*tree == NULL) {
1227 err = got_error_from_errno("malloc");
1230 (*tree)->entries = calloc(itree->nentries,
1231 sizeof(struct got_tree_entry));
1232 if ((*tree)->entries == NULL) {
1233 err = got_error_from_errno("malloc");
1236 (*tree)->nentries = itree->nentries;
1237 (*tree)->refcnt = 0;
1239 case GOT_IMSG_TREE_ENTRY:
1240 /* This message should be preceeded by GOT_IMSG_TREE. */
1241 if (*tree == NULL) {
1242 err = got_error(GOT_ERR_PRIVSEP_MSG);
1245 if (datalen < sizeof(*ite) || datalen > MAX_IMSGSIZE) {
1246 err = got_error(GOT_ERR_PRIVSEP_LEN);
1250 /* Remaining data contains the entry's name. */
1251 datalen -= sizeof(*ite);
1252 if (datalen == 0 || datalen > MAX_IMSGSIZE) {
1253 err = got_error(GOT_ERR_PRIVSEP_LEN);
1258 if (datalen + 1 > sizeof(te->name)) {
1259 err = got_error(GOT_ERR_NO_SPACE);
1262 te = &(*tree)->entries[nentries];
1263 memcpy(te->name, imsg.data + sizeof(*ite), datalen);
1264 te->name[datalen] = '\0';
1266 memcpy(te->id.sha1, ite->id, SHA1_DIGEST_LENGTH);
1267 te->mode = ite->mode;
1272 err = got_error(GOT_ERR_PRIVSEP_MSG);
1281 if (*tree && (*tree)->nentries != nentries) {
1283 err = got_error(GOT_ERR_PRIVSEP_LEN);
1284 got_object_tree_close(*tree);
1291 const struct got_error *
1292 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
1293 const uint8_t *data)
1295 struct got_imsg_blob iblob;
1298 iblob.hdrlen = hdrlen;
1303 if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
1304 return got_error(GOT_ERR_NO_SPACE);
1306 buf = malloc(sizeof(iblob) + size);
1308 return got_error_from_errno("malloc");
1310 memcpy(buf, &iblob, sizeof(iblob));
1311 memcpy(buf + sizeof(iblob), data, size);
1312 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
1313 sizeof(iblob) + size) == -1) {
1315 return got_error_from_errno("imsg_compose BLOB");
1319 /* Data has already been written to file descriptor. */
1320 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
1321 sizeof(iblob)) == -1)
1322 return got_error_from_errno("imsg_compose BLOB");
1326 return flush_imsg(ibuf);
1329 const struct got_error *
1330 got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1331 struct imsgbuf *ibuf)
1333 const struct got_error *err = NULL;
1335 struct got_imsg_blob *iblob;
1340 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1344 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1346 switch (imsg.hdr.type) {
1348 if (datalen < sizeof(*iblob)) {
1349 err = got_error(GOT_ERR_PRIVSEP_LEN);
1353 *size = iblob->size;
1354 *hdrlen = iblob->hdrlen;
1356 if (datalen == sizeof(*iblob)) {
1357 /* Data has been written to file descriptor. */
1361 if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX) {
1362 err = got_error(GOT_ERR_PRIVSEP_LEN);
1366 *outbuf = malloc(*size);
1367 if (*outbuf == NULL) {
1368 err = got_error_from_errno("malloc");
1371 memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
1374 err = got_error(GOT_ERR_PRIVSEP_MSG);
1383 static const struct got_error *
1384 send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
1386 const struct got_error *err = NULL;
1387 size_t offset, remain;
1390 remain = tagmsg_len;
1391 while (remain > 0) {
1392 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1394 if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
1395 tag->tagmsg + offset, n) == -1) {
1396 err = got_error_from_errno("imsg_compose TAG_TAGMSG");
1400 err = flush_imsg(ibuf);
1411 const struct got_error *
1412 got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
1414 const struct got_error *err = NULL;
1415 struct got_imsg_tag_object *itag;
1418 size_t tag_len = strlen(tag->tag);
1419 size_t tagger_len = strlen(tag->tagger);
1420 size_t tagmsg_len = strlen(tag->tagmsg);
1422 total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
1424 buf = malloc(total);
1426 return got_error_from_errno("malloc");
1428 itag = (struct got_imsg_tag_object *)buf;
1429 memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
1430 itag->obj_type = tag->obj_type;
1431 itag->tag_len = tag_len;
1432 itag->tagger_len = tagger_len;
1433 itag->tagger_time = tag->tagger_time;
1434 itag->tagger_gmtoff = tag->tagger_gmtoff;
1435 itag->tagmsg_len = tagmsg_len;
1437 len = sizeof(*itag);
1438 memcpy(buf + len, tag->tag, tag_len);
1440 memcpy(buf + len, tag->tagger, tagger_len);
1443 if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
1444 err = got_error_from_errno("imsg_compose TAG");
1448 if (tagmsg_len == 0 ||
1449 tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1450 err = flush_imsg(ibuf);
1454 err = send_tagmsg(ibuf, tag, tagmsg_len);
1460 const struct got_error *
1461 got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
1463 const struct got_error *err = NULL;
1465 struct got_imsg_tag_object *itag;
1466 size_t len, datalen;
1467 const size_t min_datalen =
1468 MIN(sizeof(struct got_imsg_error),
1469 sizeof(struct got_imsg_tag_object));
1473 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1477 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1480 switch (imsg.hdr.type) {
1482 if (datalen < sizeof(*itag)) {
1483 err = got_error(GOT_ERR_PRIVSEP_LEN);
1487 if (datalen != sizeof(*itag) + itag->tag_len +
1489 err = got_error(GOT_ERR_PRIVSEP_LEN);
1492 len += sizeof(*itag);
1494 *tag = calloc(1, sizeof(**tag));
1496 err = got_error_from_errno("calloc");
1500 memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
1502 if (itag->tag_len == 0) {
1503 (*tag)->tag = strdup("");
1504 if ((*tag)->tag == NULL) {
1505 err = got_error_from_errno("strdup");
1509 (*tag)->tag = malloc(itag->tag_len + 1);
1510 if ((*tag)->tag == NULL) {
1511 err = got_error_from_errno("malloc");
1514 memcpy((*tag)->tag, imsg.data + len,
1516 (*tag)->tag[itag->tag_len] = '\0';
1518 len += itag->tag_len;
1520 (*tag)->obj_type = itag->obj_type;
1521 (*tag)->tagger_time = itag->tagger_time;
1522 (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1524 if (itag->tagger_len == 0) {
1525 (*tag)->tagger = strdup("");
1526 if ((*tag)->tagger == NULL) {
1527 err = got_error_from_errno("strdup");
1531 (*tag)->tagger = malloc(itag->tagger_len + 1);
1532 if ((*tag)->tagger == NULL) {
1533 err = got_error_from_errno("malloc");
1536 memcpy((*tag)->tagger, imsg.data + len,
1538 (*tag)->tagger[itag->tagger_len] = '\0';
1540 len += itag->tagger_len;
1542 if (itag->tagmsg_len == 0) {
1543 (*tag)->tagmsg = strdup("");
1544 if ((*tag)->tagmsg == NULL) {
1545 err = got_error_from_errno("strdup");
1549 size_t offset = 0, remain = itag->tagmsg_len;
1551 (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1552 if ((*tag)->tagmsg == NULL) {
1553 err = got_error_from_errno("malloc");
1556 while (remain > 0) {
1557 struct imsg imsg_log;
1558 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1561 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1565 if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1566 return got_error(GOT_ERR_PRIVSEP_MSG);
1568 memcpy((*tag)->tagmsg + offset, imsg_log.data,
1570 imsg_free(&imsg_log);
1574 (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1579 err = got_error(GOT_ERR_PRIVSEP_MSG);
1588 const struct got_error *
1589 got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1590 struct got_packidx *packidx)
1592 const struct got_error *err = NULL;
1593 struct got_imsg_packidx ipackidx;
1594 struct got_imsg_pack ipack;
1597 ipackidx.len = packidx->len;
1598 fd = dup(packidx->fd);
1600 return got_error_from_errno("dup");
1602 if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
1603 sizeof(ipackidx)) == -1) {
1604 err = got_error_from_errno("imsg_compose PACKIDX");
1609 if (strlcpy(ipack.path_packfile, pack->path_packfile,
1610 sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
1611 return got_error(GOT_ERR_NO_SPACE);
1612 ipack.filesize = pack->filesize;
1616 return got_error_from_errno("dup");
1618 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
1620 err = got_error_from_errno("imsg_compose PACK");
1625 return flush_imsg(ibuf);
1628 const struct got_error *
1629 got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
1630 struct got_object_id *id)
1632 struct got_imsg_packed_object iobj;
1635 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
1637 if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
1638 &iobj, sizeof(iobj)) == -1)
1639 return got_error_from_errno("imsg_compose "
1640 "PACKED_OBJECT_REQUEST");
1642 return flush_imsg(ibuf);
1645 const struct got_error *
1646 got_privsep_send_gitconfig_parse_req(struct imsgbuf *ibuf, int fd)
1648 const struct got_error *err = NULL;
1650 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_PARSE_REQUEST, 0, 0, fd,
1652 err = got_error_from_errno("imsg_compose "
1653 "GITCONFIG_PARSE_REQUEST");
1658 return flush_imsg(ibuf);
1661 const struct got_error *
1662 got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *ibuf)
1664 if (imsg_compose(ibuf,
1665 GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST, 0, 0, -1,
1667 return got_error_from_errno("imsg_compose "
1668 "GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST");
1670 return flush_imsg(ibuf);
1673 const struct got_error *
1674 got_privsep_send_gitconfig_author_name_req(struct imsgbuf *ibuf)
1676 if (imsg_compose(ibuf,
1677 GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST, 0, 0, -1, NULL, 0) == -1)
1678 return got_error_from_errno("imsg_compose "
1679 "GITCONFIG_AUTHOR_NAME_REQUEST");
1681 return flush_imsg(ibuf);
1684 const struct got_error *
1685 got_privsep_send_gitconfig_author_email_req(struct imsgbuf *ibuf)
1687 if (imsg_compose(ibuf,
1688 GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST, 0, 0, -1, NULL, 0) == -1)
1689 return got_error_from_errno("imsg_compose "
1690 "GITCONFIG_AUTHOR_EMAIL_REQUEST");
1692 return flush_imsg(ibuf);
1695 const struct got_error *
1696 got_privsep_send_gitconfig_remotes_req(struct imsgbuf *ibuf)
1698 if (imsg_compose(ibuf,
1699 GOT_IMSG_GITCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
1700 return got_error_from_errno("imsg_compose "
1701 "GITCONFIG_REMOTE_REQUEST");
1703 return flush_imsg(ibuf);
1706 const struct got_error *
1707 got_privsep_send_gitconfig_owner_req(struct imsgbuf *ibuf)
1709 if (imsg_compose(ibuf,
1710 GOT_IMSG_GITCONFIG_OWNER_REQUEST, 0, 0, -1, NULL, 0) == -1)
1711 return got_error_from_errno("imsg_compose "
1712 "GITCONFIG_OWNER_REQUEST");
1714 return flush_imsg(ibuf);
1717 const struct got_error *
1718 got_privsep_recv_gitconfig_str(char **str, struct imsgbuf *ibuf)
1720 const struct got_error *err = NULL;
1723 const size_t min_datalen = 0;
1727 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1730 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1732 switch (imsg.hdr.type) {
1733 case GOT_IMSG_GITCONFIG_STR_VAL:
1736 *str = malloc(datalen);
1738 err = got_error_from_errno("malloc");
1741 if (strlcpy(*str, imsg.data, datalen) >= datalen)
1742 err = got_error(GOT_ERR_NO_SPACE);
1745 err = got_error(GOT_ERR_PRIVSEP_MSG);
1753 const struct got_error *
1754 got_privsep_recv_gitconfig_int(int *val, struct imsgbuf *ibuf)
1756 const struct got_error *err = NULL;
1759 const size_t min_datalen =
1760 MIN(sizeof(struct got_imsg_error), sizeof(int));
1764 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1767 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1769 switch (imsg.hdr.type) {
1770 case GOT_IMSG_GITCONFIG_INT_VAL:
1771 if (datalen != sizeof(*val)) {
1772 err = got_error(GOT_ERR_PRIVSEP_LEN);
1775 memcpy(val, imsg.data, sizeof(*val));
1778 err = got_error(GOT_ERR_PRIVSEP_MSG);
1786 const struct got_error *
1787 got_privsep_recv_gitconfig_remotes(struct got_remote_repo **remotes,
1788 int *nremotes, struct imsgbuf *ibuf)
1790 const struct got_error *err = NULL;
1793 struct got_imsg_remotes iremotes;
1794 struct got_imsg_remote iremote;
1798 iremotes.nremotes = 0;
1800 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremotes));
1803 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1805 switch (imsg.hdr.type) {
1806 case GOT_IMSG_GITCONFIG_REMOTES:
1807 if (datalen != sizeof(iremotes)) {
1808 err = got_error(GOT_ERR_PRIVSEP_LEN);
1811 memcpy(&iremotes, imsg.data, sizeof(iremotes));
1812 if (iremotes.nremotes == 0) {
1819 return got_error(GOT_ERR_PRIVSEP_MSG);
1824 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(**remotes));
1825 if (*remotes == NULL)
1826 return got_error_from_errno("recallocarray");
1828 while (*nremotes < iremotes.nremotes) {
1829 struct got_remote_repo *remote;
1831 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremote));
1834 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1836 switch (imsg.hdr.type) {
1837 case GOT_IMSG_GITCONFIG_REMOTE:
1838 remote = &(*remotes)[*nremotes];
1839 if (datalen < sizeof(iremote)) {
1840 err = got_error(GOT_ERR_PRIVSEP_LEN);
1843 memcpy(&iremote, imsg.data, sizeof(iremote));
1844 if (iremote.name_len == 0 || iremote.url_len == 0 ||
1845 (sizeof(iremote) + iremote.name_len +
1846 iremote.url_len) > datalen) {
1847 err = got_error(GOT_ERR_PRIVSEP_LEN);
1850 remote->name = strndup(imsg.data + sizeof(iremote),
1852 if (remote->name == NULL) {
1853 err = got_error_from_errno("strndup");
1856 remote->url = strndup(imsg.data + sizeof(iremote) +
1857 iremote.name_len, iremote.url_len);
1858 if (remote->url == NULL) {
1859 err = got_error_from_errno("strndup");
1863 remote->mirror_references = iremote.mirror_references;
1867 err = got_error(GOT_ERR_PRIVSEP_MSG);
1878 for (i = 0; i < *nremotes; i++) {
1879 free((*remotes)[i].name);
1880 free((*remotes)[i].url);
1889 const struct got_error *
1890 got_privsep_send_commit_traversal_request(struct imsgbuf *ibuf,
1891 struct got_object_id *id, int idx, const char *path)
1893 const struct got_error *err = NULL;
1895 size_t path_len = strlen(path) + 1;
1897 wbuf = imsg_create(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_REQUEST, 0, 0,
1898 sizeof(struct got_imsg_commit_traversal_request) + path_len);
1900 return got_error_from_errno(
1901 "imsg_create COMMIT_TRAVERSAL_REQUEST");
1902 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
1903 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
1907 if (imsg_add(wbuf, &idx, sizeof(idx)) == -1) {
1908 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
1912 if (imsg_add(wbuf, path, path_len) == -1) {
1913 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
1919 imsg_close(ibuf, wbuf);
1921 return flush_imsg(ibuf);
1924 const struct got_error *
1925 got_privsep_recv_traversed_commits(struct got_commit_object **changed_commit,
1926 struct got_object_id **changed_commit_id,
1927 struct got_object_id_queue *commit_ids, struct imsgbuf *ibuf)
1929 const struct got_error *err = NULL;
1931 struct got_imsg_traversed_commits *icommits;
1935 *changed_commit = NULL;
1936 *changed_commit_id = NULL;
1939 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1943 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1944 switch (imsg.hdr.type) {
1945 case GOT_IMSG_TRAVERSED_COMMITS:
1946 icommits = imsg.data;
1947 if (datalen != sizeof(*icommits) +
1948 icommits->ncommits * SHA1_DIGEST_LENGTH) {
1949 err = got_error(GOT_ERR_PRIVSEP_LEN);
1952 for (i = 0; i < icommits->ncommits; i++) {
1953 struct got_object_qid *qid;
1954 uint8_t *sha1 = (uint8_t *)imsg.data +
1955 sizeof(*icommits) + i * SHA1_DIGEST_LENGTH;
1956 err = got_object_qid_alloc_partial(&qid);
1959 memcpy(qid->id->sha1, sha1, SHA1_DIGEST_LENGTH);
1960 SIMPLEQ_INSERT_TAIL(commit_ids, qid, entry);
1962 /* The last commit may contain a change. */
1963 if (i == icommits->ncommits - 1) {
1964 *changed_commit_id =
1965 got_object_id_dup(qid->id);
1966 if (*changed_commit_id == NULL) {
1967 err = got_error_from_errno(
1968 "got_object_id_dup");
1974 case GOT_IMSG_COMMIT:
1975 if (*changed_commit_id == NULL) {
1976 err = got_error(GOT_ERR_PRIVSEP_MSG);
1979 err = get_commit_from_imsg(changed_commit, &imsg,
1982 case GOT_IMSG_COMMIT_TRAVERSAL_DONE:
1986 err = got_error(GOT_ERR_PRIVSEP_MSG);
1996 got_object_id_queue_free(commit_ids);
2000 const struct got_error *
2001 got_privsep_unveil_exec_helpers(void)
2003 const char *helpers[] = {
2004 GOT_PATH_PROG_READ_PACK,
2005 GOT_PATH_PROG_READ_OBJECT,
2006 GOT_PATH_PROG_READ_COMMIT,
2007 GOT_PATH_PROG_READ_TREE,
2008 GOT_PATH_PROG_READ_BLOB,
2009 GOT_PATH_PROG_READ_TAG,
2010 GOT_PATH_PROG_READ_GITCONFIG,
2011 GOT_PATH_PROG_FETCH_PACK,
2012 GOT_PATH_PROG_INDEX_PACK,
2016 for (i = 0; i < nitems(helpers); i++) {
2017 if (unveil(helpers[i], "x") == 0)
2019 return got_error_from_errno2("unveil", helpers[i]);
2026 got_privsep_exec_child(int imsg_fds[2], const char *path, const char *repo_path)
2028 if (close(imsg_fds[0]) != 0) {
2029 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2033 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
2034 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2037 if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
2038 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2042 if (execl(path, path, repo_path, (char *)NULL) == -1) {
2043 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,