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>
37 #include "got_object.h"
38 #include "got_error.h"
40 #include "got_repository.h"
42 #include "got_lib_sha1.h"
43 #include "got_lib_delta.h"
44 #include "got_lib_inflate.h"
45 #include "got_lib_object.h"
46 #include "got_lib_object_parse.h"
47 #include "got_lib_privsep.h"
48 #include "got_lib_pack.h"
51 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
55 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
58 static const struct got_error *
59 poll_fd(int fd, int events, int timeout)
65 pfd[0].events = events;
67 n = poll(pfd, 1, timeout);
69 return got_error_from_errno("poll");
71 return got_error(GOT_ERR_TIMEOUT);
72 if (pfd[0].revents & (POLLERR | POLLNVAL))
73 return got_error_from_errno("poll error");
74 if (pfd[0].revents & (events | POLLHUP))
77 return got_error(GOT_ERR_INTERRUPT);
80 static const struct got_error *
81 read_imsg(struct imsgbuf *ibuf)
83 const struct got_error *err;
86 err = poll_fd(ibuf->fd, POLLIN, INFTIM);
92 if (errno == EAGAIN) /* Could be a file-descriptor leak. */
93 return got_error(GOT_ERR_PRIVSEP_NO_FD);
94 return got_error(GOT_ERR_PRIVSEP_READ);
97 return got_error(GOT_ERR_PRIVSEP_PIPE);
102 const struct got_error *
103 got_privsep_wait_for_child(pid_t pid)
107 if (waitpid(pid, &child_status, 0) == -1)
108 return got_error_from_errno("waitpid");
110 if (!WIFEXITED(child_status))
111 return got_error(GOT_ERR_PRIVSEP_DIED);
113 if (WEXITSTATUS(child_status) != 0)
114 return got_error(GOT_ERR_PRIVSEP_EXIT);
119 static const struct got_error *
120 recv_imsg_error(struct imsg *imsg, size_t datalen)
122 struct got_imsg_error *ierr;
124 if (datalen != sizeof(*ierr))
125 return got_error(GOT_ERR_PRIVSEP_LEN);
128 if (ierr->code == GOT_ERR_ERRNO) {
129 static struct got_error serr;
130 serr.code = GOT_ERR_ERRNO;
131 serr.msg = strerror(ierr->errno_code);
135 return got_error(ierr->code);
138 const struct got_error *
139 got_privsep_recv_imsg(struct imsg *imsg, struct imsgbuf *ibuf,
142 const struct got_error *err;
145 n = imsg_get(ibuf, imsg);
147 return got_error_from_errno("imsg_get");
150 err = read_imsg(ibuf);
153 n = imsg_get(ibuf, imsg);
155 return got_error_from_errno("imsg_get");
158 if (imsg->hdr.len < IMSG_HEADER_SIZE + min_datalen)
159 return got_error(GOT_ERR_PRIVSEP_LEN);
161 if (imsg->hdr.type == GOT_IMSG_ERROR) {
162 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
163 return recv_imsg_error(imsg, datalen);
169 /* Attempt to send an error in an imsg. Complain on stderr as a last resort. */
171 got_privsep_send_error(struct imsgbuf *ibuf, const struct got_error *err)
173 const struct got_error *poll_err;
174 struct got_imsg_error ierr;
177 ierr.code = err->code;
178 if (err->code == GOT_ERR_ERRNO)
179 ierr.errno_code = errno;
182 ret = imsg_compose(ibuf, GOT_IMSG_ERROR, 0, 0, -1, &ierr, sizeof(ierr));
184 fprintf(stderr, "%s: error %d \"%s\": imsg_compose: %s\n",
185 getprogname(), err->code, err->msg, strerror(errno));
189 poll_err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
191 fprintf(stderr, "%s: error %d \"%s\": poll: %s\n",
192 getprogname(), err->code, err->msg, poll_err->msg);
196 ret = imsg_flush(ibuf);
198 fprintf(stderr, "%s: error %d \"%s\": imsg_flush: %s\n",
199 getprogname(), err->code, err->msg, strerror(errno));
204 static const struct got_error *
205 flush_imsg(struct imsgbuf *ibuf)
207 const struct got_error *err;
209 err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
213 if (imsg_flush(ibuf) == -1)
214 return got_error_from_errno("imsg_flush");
219 const struct got_error *
220 got_privsep_flush_imsg(struct imsgbuf *ibuf)
222 return flush_imsg(ibuf);
225 const struct got_error *
226 got_privsep_send_stop(int fd)
228 const struct got_error *err = NULL;
231 imsg_init(&ibuf, fd);
233 if (imsg_compose(&ibuf, GOT_IMSG_STOP, 0, 0, -1, NULL, 0) == -1)
234 return got_error_from_errno("imsg_compose STOP");
236 err = flush_imsg(&ibuf);
241 const struct got_error *
242 got_privsep_send_obj_req(struct imsgbuf *ibuf, int fd)
244 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_REQUEST, 0, 0, fd, NULL, 0)
246 return got_error_from_errno("imsg_compose OBJECT_REQUEST");
248 return flush_imsg(ibuf);
251 const struct got_error *
252 got_privsep_send_raw_obj_req(struct imsgbuf *ibuf, int fd)
254 if (imsg_compose(ibuf, GOT_IMSG_RAW_OBJECT_REQUEST, 0, 0, fd, NULL, 0)
256 return got_error_from_errno("imsg_compose RAW_OBJECT_REQUEST");
258 return flush_imsg(ibuf);
261 const struct got_error *
262 got_privsep_send_raw_obj_outfd(struct imsgbuf *ibuf, int outfd)
264 const struct got_error *err = NULL;
266 if (imsg_compose(ibuf, GOT_IMSG_RAW_OBJECT_OUTFD, 0, 0, outfd, NULL, 0)
268 err = got_error_from_errno("imsg_compose RAW_OBJECT_OUTFD");
273 return flush_imsg(ibuf);
276 const struct got_error *
277 got_privsep_send_raw_obj(struct imsgbuf *ibuf, off_t size, size_t hdrlen,
280 const struct got_error *err = NULL;
281 struct got_imsg_raw_obj iobj;
282 size_t len = sizeof(iobj);
285 iobj.hdrlen = hdrlen;
288 if (data && size <= GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX)
291 wbuf = imsg_create(ibuf, GOT_IMSG_RAW_OBJECT, 0, 0, len);
293 err = got_error_from_errno("imsg_create RAW_OBJECT");
297 if (imsg_add(wbuf, &iobj, sizeof(iobj)) == -1) {
298 err = got_error_from_errno("imsg_add RAW_OBJECT");
303 if (data && size <= GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX) {
304 if (imsg_add(wbuf, data, size) == -1) {
305 err = got_error_from_errno("imsg_add RAW_OBJECT");
312 imsg_close(ibuf, wbuf);
314 return flush_imsg(ibuf);
317 const struct got_error *
318 got_privsep_recv_raw_obj(uint8_t **outbuf, off_t *size, size_t *hdrlen,
319 struct imsgbuf *ibuf)
321 const struct got_error *err = NULL;
323 struct got_imsg_raw_obj *iobj;
328 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
332 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
334 switch (imsg.hdr.type) {
335 case GOT_IMSG_RAW_OBJECT:
336 if (datalen < sizeof(*iobj)) {
337 err = got_error(GOT_ERR_PRIVSEP_LEN);
342 *hdrlen = iobj->hdrlen;
344 if (datalen == sizeof(*iobj)) {
345 /* Data has been written to file descriptor. */
349 if (*size > GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX) {
350 err = got_error(GOT_ERR_PRIVSEP_LEN);
354 *outbuf = malloc(*size);
355 if (*outbuf == NULL) {
356 err = got_error_from_errno("malloc");
359 memcpy(*outbuf, imsg.data + sizeof(*iobj), *size);
362 err = got_error(GOT_ERR_PRIVSEP_MSG);
371 const struct got_error *
372 got_privsep_send_commit_req(struct imsgbuf *ibuf, int fd,
373 struct got_object_id *id, int pack_idx)
375 const struct got_error *err = NULL;
376 struct got_imsg_packed_object iobj, *iobjp;
379 if (id) { /* commit is packed */
381 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
389 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_REQUEST, 0, 0, fd, iobjp, len)
391 err = got_error_from_errno("imsg_compose COMMIT_REQUEST");
396 return flush_imsg(ibuf);
399 const struct got_error *
400 got_privsep_send_tree_req(struct imsgbuf *ibuf, int fd,
401 struct got_object_id *id, int pack_idx)
403 const struct got_error *err = NULL;
405 size_t len = id ? sizeof(struct got_imsg_packed_object) : 0;
407 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_REQUEST, 0, 0, len);
409 return got_error_from_errno("imsg_create TREE_REQUEST");
411 if (id) { /* tree is packed */
412 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
413 err = got_error_from_errno("imsg_add TREE_ENTRY");
418 if (imsg_add(wbuf, &pack_idx, sizeof(pack_idx)) == -1) {
419 err = got_error_from_errno("imsg_add TREE_ENTRY");
426 imsg_close(ibuf, wbuf);
428 return flush_imsg(ibuf);
431 const struct got_error *
432 got_privsep_send_tag_req(struct imsgbuf *ibuf, int fd,
433 struct got_object_id *id, int pack_idx)
435 struct got_imsg_packed_object iobj, *iobjp;
438 if (id) { /* tag is packed */
440 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
448 if (imsg_compose(ibuf, GOT_IMSG_TAG_REQUEST, 0, 0, fd, iobjp, len)
450 return got_error_from_errno("imsg_compose TAG_REQUEST");
452 return flush_imsg(ibuf);
455 const struct got_error *
456 got_privsep_send_blob_req(struct imsgbuf *ibuf, int infd,
457 struct got_object_id *id, int pack_idx)
459 const struct got_error *err = NULL;
460 struct got_imsg_packed_object iobj, *iobjp;
463 if (id) { /* blob is packed */
465 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
473 if (imsg_compose(ibuf, GOT_IMSG_BLOB_REQUEST, 0, 0, infd, iobjp, len)
475 err = got_error_from_errno("imsg_compose BLOB_REQUEST");
480 return flush_imsg(ibuf);
483 const struct got_error *
484 got_privsep_send_blob_outfd(struct imsgbuf *ibuf, int outfd)
486 const struct got_error *err = NULL;
488 if (imsg_compose(ibuf, GOT_IMSG_BLOB_OUTFD, 0, 0, outfd, NULL, 0)
490 err = got_error_from_errno("imsg_compose BLOB_OUTFD");
495 return flush_imsg(ibuf);
498 static const struct got_error *
499 send_fd(struct imsgbuf *ibuf, int imsg_code, int fd)
501 const struct got_error *err = NULL;
503 if (imsg_compose(ibuf, imsg_code, 0, 0, fd, NULL, 0) == -1) {
504 err = got_error_from_errno("imsg_compose TMPFD");
509 return flush_imsg(ibuf);
512 const struct got_error *
513 got_privsep_send_tmpfd(struct imsgbuf *ibuf, int fd)
515 return send_fd(ibuf, GOT_IMSG_TMPFD, fd);
518 const struct got_error *
519 got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj)
521 struct got_imsg_object iobj;
523 memcpy(iobj.id, obj->id.sha1, sizeof(iobj.id));
524 iobj.type = obj->type;
525 iobj.flags = obj->flags;
526 iobj.hdrlen = obj->hdrlen;
527 iobj.size = obj->size;
528 if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
529 iobj.pack_offset = obj->pack_offset;
530 iobj.pack_idx = obj->pack_idx;
533 if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
535 return got_error_from_errno("imsg_compose OBJECT");
537 return flush_imsg(ibuf);
540 const struct got_error *
541 got_privsep_send_fetch_req(struct imsgbuf *ibuf, int fd,
542 struct got_pathlist_head *have_refs, int fetch_all_branches,
543 struct got_pathlist_head *wanted_branches,
544 struct got_pathlist_head *wanted_refs, int list_refs_only, int verbosity)
546 const struct got_error *err = NULL;
549 struct got_pathlist_entry *pe;
550 struct got_imsg_fetch_request fetchreq;
552 memset(&fetchreq, 0, sizeof(fetchreq));
553 fetchreq.fetch_all_branches = fetch_all_branches;
554 fetchreq.list_refs_only = list_refs_only;
555 fetchreq.verbosity = verbosity;
556 TAILQ_FOREACH(pe, have_refs, entry)
557 fetchreq.n_have_refs++;
558 TAILQ_FOREACH(pe, wanted_branches, entry)
559 fetchreq.n_wanted_branches++;
560 TAILQ_FOREACH(pe, wanted_refs, entry)
561 fetchreq.n_wanted_refs++;
562 len = sizeof(struct got_imsg_fetch_request);
563 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
565 return got_error(GOT_ERR_NO_SPACE);
568 if (imsg_compose(ibuf, GOT_IMSG_FETCH_REQUEST, 0, 0, fd,
569 &fetchreq, sizeof(fetchreq)) == -1)
570 return got_error_from_errno(
571 "imsg_compose FETCH_SERVER_PROGRESS");
573 err = flush_imsg(ibuf);
580 TAILQ_FOREACH(pe, have_refs, entry) {
581 const char *name = pe->path;
582 size_t name_len = pe->path_len;
583 struct got_object_id *id = pe->data;
585 len = sizeof(struct got_imsg_fetch_have_ref) + name_len;
586 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_HAVE_REF, 0, 0, len);
588 return got_error_from_errno("imsg_create FETCH_HAVE_REF");
590 /* Keep in sync with struct got_imsg_fetch_have_ref! */
591 if (imsg_add(wbuf, id->sha1, sizeof(id->sha1)) == -1) {
592 err = got_error_from_errno("imsg_add FETCH_HAVE_REF");
596 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
597 err = got_error_from_errno("imsg_add FETCH_HAVE_REF");
601 if (imsg_add(wbuf, name, name_len) == -1) {
602 err = got_error_from_errno("imsg_add FETCH_HAVE_REF");
608 imsg_close(ibuf, wbuf);
609 err = flush_imsg(ibuf);
614 TAILQ_FOREACH(pe, wanted_branches, entry) {
615 const char *name = pe->path;
616 size_t name_len = pe->path_len;
618 len = sizeof(struct got_imsg_fetch_wanted_branch) + name_len;
619 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_WANTED_BRANCH, 0, 0,
622 return got_error_from_errno(
623 "imsg_create FETCH_WANTED_BRANCH");
625 /* Keep in sync with struct got_imsg_fetch_wanted_branch! */
626 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
627 err = got_error_from_errno(
628 "imsg_add FETCH_WANTED_BRANCH");
632 if (imsg_add(wbuf, name, name_len) == -1) {
633 err = got_error_from_errno(
634 "imsg_add FETCH_WANTED_BRANCH");
640 imsg_close(ibuf, wbuf);
641 err = flush_imsg(ibuf);
646 TAILQ_FOREACH(pe, wanted_refs, entry) {
647 const char *name = pe->path;
648 size_t name_len = pe->path_len;
650 len = sizeof(struct got_imsg_fetch_wanted_ref) + name_len;
651 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_WANTED_REF, 0, 0,
654 return got_error_from_errno(
655 "imsg_create FETCH_WANTED_REF");
657 /* Keep in sync with struct got_imsg_fetch_wanted_ref! */
658 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
659 err = got_error_from_errno(
660 "imsg_add FETCH_WANTED_REF");
664 if (imsg_add(wbuf, name, name_len) == -1) {
665 err = got_error_from_errno(
666 "imsg_add FETCH_WANTED_REF");
672 imsg_close(ibuf, wbuf);
673 err = flush_imsg(ibuf);
683 const struct got_error *
684 got_privsep_send_fetch_outfd(struct imsgbuf *ibuf, int fd)
686 return send_fd(ibuf, GOT_IMSG_FETCH_OUTFD, fd);
689 const struct got_error *
690 got_privsep_recv_fetch_progress(int *done, struct got_object_id **id,
691 char **refname, struct got_pathlist_head *symrefs, char **server_progress,
692 off_t *packfile_size, uint8_t *pack_sha1, struct imsgbuf *ibuf)
694 const struct got_error *err = NULL;
697 struct got_imsg_fetch_symrefs *isymrefs = NULL;
705 *server_progress = NULL;
707 memset(pack_sha1, 0, SHA1_DIGEST_LENGTH);
709 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
713 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
714 switch (imsg.hdr.type) {
716 if (datalen < sizeof(struct got_imsg_error)) {
717 err = got_error(GOT_ERR_PRIVSEP_LEN);
720 err = recv_imsg_error(&imsg, datalen);
722 case GOT_IMSG_FETCH_SYMREFS:
723 if (datalen < sizeof(*isymrefs)) {
724 err = got_error(GOT_ERR_PRIVSEP_LEN);
727 if (isymrefs != NULL) {
728 err = got_error(GOT_ERR_PRIVSEP_MSG);
731 isymrefs = (struct got_imsg_fetch_symrefs *)imsg.data;
732 off = sizeof(*isymrefs);
733 remain = datalen - off;
734 for (n = 0; n < isymrefs->nsymrefs; n++) {
735 struct got_imsg_fetch_symref *s;
737 if (remain < sizeof(struct got_imsg_fetch_symref)) {
738 err = got_error(GOT_ERR_PRIVSEP_LEN);
741 s = (struct got_imsg_fetch_symref *)(imsg.data + off);
743 remain -= sizeof(*s);
744 if (remain < s->name_len) {
745 err = got_error(GOT_ERR_PRIVSEP_LEN);
748 name = strndup(imsg.data + off, s->name_len);
750 err = got_error_from_errno("strndup");
754 remain -= s->name_len;
755 if (remain < s->target_len) {
756 err = got_error(GOT_ERR_PRIVSEP_LEN);
760 target = strndup(imsg.data + off, s->target_len);
761 if (target == NULL) {
762 err = got_error_from_errno("strndup");
766 off += s->target_len;
767 remain -= s->target_len;
768 err = got_pathlist_append(symrefs, name, target);
776 case GOT_IMSG_FETCH_REF:
777 if (datalen <= SHA1_DIGEST_LENGTH) {
778 err = got_error(GOT_ERR_PRIVSEP_MSG);
781 *id = malloc(sizeof(**id));
783 err = got_error_from_errno("malloc");
786 memcpy((*id)->sha1, imsg.data, SHA1_DIGEST_LENGTH);
787 *refname = strndup(imsg.data + SHA1_DIGEST_LENGTH,
788 datalen - SHA1_DIGEST_LENGTH);
789 if (*refname == NULL) {
790 err = got_error_from_errno("strndup");
794 case GOT_IMSG_FETCH_SERVER_PROGRESS:
796 err = got_error(GOT_ERR_PRIVSEP_LEN);
799 *server_progress = strndup(imsg.data, datalen);
800 if (*server_progress == NULL) {
801 err = got_error_from_errno("strndup");
804 for (i = 0; i < datalen; i++) {
805 if (!isprint((unsigned char)(*server_progress)[i]) &&
806 !isspace((unsigned char)(*server_progress)[i])) {
807 err = got_error(GOT_ERR_PRIVSEP_MSG);
808 free(*server_progress);
809 *server_progress = NULL;
814 case GOT_IMSG_FETCH_DOWNLOAD_PROGRESS:
815 if (datalen < sizeof(*packfile_size)) {
816 err = got_error(GOT_ERR_PRIVSEP_MSG);
819 memcpy(packfile_size, imsg.data, sizeof(*packfile_size));
821 case GOT_IMSG_FETCH_DONE:
822 if (datalen != SHA1_DIGEST_LENGTH) {
823 err = got_error(GOT_ERR_PRIVSEP_MSG);
826 memcpy(pack_sha1, imsg.data, SHA1_DIGEST_LENGTH);
830 err = got_error(GOT_ERR_PRIVSEP_MSG);
844 const struct got_error *
845 got_privsep_send_index_pack_req(struct imsgbuf *ibuf, uint8_t *pack_sha1,
848 const struct got_error *err = NULL;
850 /* Keep in sync with struct got_imsg_index_pack_request */
851 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_REQUEST, 0, 0, fd,
852 pack_sha1, SHA1_DIGEST_LENGTH) == -1) {
853 err = got_error_from_errno("imsg_compose INDEX_REQUEST");
857 return flush_imsg(ibuf);
860 const struct got_error *
861 got_privsep_send_index_pack_outfd(struct imsgbuf *ibuf, int fd)
863 return send_fd(ibuf, GOT_IMSG_IDXPACK_OUTFD, fd);
866 const struct got_error *
867 got_privsep_recv_index_progress(int *done, int *nobj_total,
868 int *nobj_indexed, int *nobj_loose, int *nobj_resolved,
869 struct imsgbuf *ibuf)
871 const struct got_error *err = NULL;
873 struct got_imsg_index_pack_progress *iprogress;
881 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
885 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
886 switch (imsg.hdr.type) {
888 if (datalen < sizeof(struct got_imsg_error)) {
889 err = got_error(GOT_ERR_PRIVSEP_LEN);
892 err = recv_imsg_error(&imsg, datalen);
894 case GOT_IMSG_IDXPACK_PROGRESS:
895 if (datalen < sizeof(*iprogress)) {
896 err = got_error(GOT_ERR_PRIVSEP_LEN);
899 iprogress = (struct got_imsg_index_pack_progress *)imsg.data;
900 *nobj_total = iprogress->nobj_total;
901 *nobj_indexed = iprogress->nobj_indexed;
902 *nobj_loose = iprogress->nobj_loose;
903 *nobj_resolved = iprogress->nobj_resolved;
905 case GOT_IMSG_IDXPACK_DONE:
907 err = got_error(GOT_ERR_PRIVSEP_LEN);
913 err = got_error(GOT_ERR_PRIVSEP_MSG);
921 const struct got_error *
922 got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
923 struct imsgbuf *ibuf)
925 const struct got_error *err = NULL;
926 struct got_imsg_object *iobj;
927 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
929 if (datalen != sizeof(*iobj))
930 return got_error(GOT_ERR_PRIVSEP_LEN);
933 *obj = calloc(1, sizeof(**obj));
935 return got_error_from_errno("calloc");
937 memcpy((*obj)->id.sha1, iobj->id, SHA1_DIGEST_LENGTH);
938 (*obj)->type = iobj->type;
939 (*obj)->flags = iobj->flags;
940 (*obj)->hdrlen = iobj->hdrlen;
941 (*obj)->size = iobj->size;
942 /* path_packfile is handled by caller */
943 if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
944 (*obj)->pack_offset = iobj->pack_offset;
945 (*obj)->pack_idx = iobj->pack_idx;
951 const struct got_error *
952 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
954 const struct got_error *err = NULL;
956 const size_t min_datalen =
957 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
961 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
965 switch (imsg.hdr.type) {
966 case GOT_IMSG_OBJECT:
967 err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
970 err = got_error(GOT_ERR_PRIVSEP_MSG);
979 static const struct got_error *
980 send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
983 const struct got_error *err = NULL;
984 size_t offset, remain;
989 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
991 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
992 commit->logmsg + offset, n) == -1) {
993 err = got_error_from_errno("imsg_compose "
998 err = flush_imsg(ibuf);
1009 const struct got_error *
1010 got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
1012 const struct got_error *err = NULL;
1013 struct got_imsg_commit_object *icommit;
1016 struct got_object_qid *qid;
1017 size_t author_len = strlen(commit->author);
1018 size_t committer_len = strlen(commit->committer);
1019 size_t logmsg_len = strlen(commit->logmsg);
1021 total = sizeof(*icommit) + author_len + committer_len +
1022 commit->nparents * SHA1_DIGEST_LENGTH;
1024 buf = malloc(total);
1026 return got_error_from_errno("malloc");
1028 icommit = (struct got_imsg_commit_object *)buf;
1029 memcpy(icommit->tree_id, commit->tree_id->sha1,
1030 sizeof(icommit->tree_id));
1031 icommit->author_len = author_len;
1032 icommit->author_time = commit->author_time;
1033 icommit->author_gmtoff = commit->author_gmtoff;
1034 icommit->committer_len = committer_len;
1035 icommit->committer_time = commit->committer_time;
1036 icommit->committer_gmtoff = commit->committer_gmtoff;
1037 icommit->logmsg_len = logmsg_len;
1038 icommit->nparents = commit->nparents;
1040 len = sizeof(*icommit);
1041 memcpy(buf + len, commit->author, author_len);
1043 memcpy(buf + len, commit->committer, committer_len);
1044 len += committer_len;
1045 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
1046 memcpy(buf + len, qid->id, SHA1_DIGEST_LENGTH);
1047 len += SHA1_DIGEST_LENGTH;
1050 if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
1051 err = got_error_from_errno("imsg_compose COMMIT");
1055 if (logmsg_len == 0 ||
1056 logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1057 err = flush_imsg(ibuf);
1061 err = send_commit_logmsg(ibuf, commit, logmsg_len);
1067 static const struct got_error *
1068 get_commit_from_imsg(struct got_commit_object **commit,
1069 struct imsg *imsg, size_t datalen, struct imsgbuf *ibuf)
1071 const struct got_error *err = NULL;
1072 struct got_imsg_commit_object *icommit;
1076 if (datalen < sizeof(*icommit))
1077 return got_error(GOT_ERR_PRIVSEP_LEN);
1079 icommit = imsg->data;
1080 if (datalen != sizeof(*icommit) + icommit->author_len +
1081 icommit->committer_len +
1082 icommit->nparents * SHA1_DIGEST_LENGTH)
1083 return got_error(GOT_ERR_PRIVSEP_LEN);
1085 if (icommit->nparents < 0)
1086 return got_error(GOT_ERR_PRIVSEP_LEN);
1088 len += sizeof(*icommit);
1090 *commit = got_object_commit_alloc_partial();
1091 if (*commit == NULL)
1092 return got_error_from_errno(
1093 "got_object_commit_alloc_partial");
1095 memcpy((*commit)->tree_id->sha1, icommit->tree_id,
1096 SHA1_DIGEST_LENGTH);
1097 (*commit)->author_time = icommit->author_time;
1098 (*commit)->author_gmtoff = icommit->author_gmtoff;
1099 (*commit)->committer_time = icommit->committer_time;
1100 (*commit)->committer_gmtoff = icommit->committer_gmtoff;
1102 if (icommit->author_len == 0) {
1103 (*commit)->author = strdup("");
1104 if ((*commit)->author == NULL) {
1105 err = got_error_from_errno("strdup");
1109 (*commit)->author = malloc(icommit->author_len + 1);
1110 if ((*commit)->author == NULL) {
1111 err = got_error_from_errno("malloc");
1114 memcpy((*commit)->author, imsg->data + len,
1115 icommit->author_len);
1116 (*commit)->author[icommit->author_len] = '\0';
1118 len += icommit->author_len;
1120 if (icommit->committer_len == 0) {
1121 (*commit)->committer = strdup("");
1122 if ((*commit)->committer == NULL) {
1123 err = got_error_from_errno("strdup");
1127 (*commit)->committer =
1128 malloc(icommit->committer_len + 1);
1129 if ((*commit)->committer == NULL) {
1130 err = got_error_from_errno("malloc");
1133 memcpy((*commit)->committer, imsg->data + len,
1134 icommit->committer_len);
1135 (*commit)->committer[icommit->committer_len] = '\0';
1137 len += icommit->committer_len;
1139 if (icommit->logmsg_len == 0) {
1140 (*commit)->logmsg = strdup("");
1141 if ((*commit)->logmsg == NULL) {
1142 err = got_error_from_errno("strdup");
1146 size_t offset = 0, remain = icommit->logmsg_len;
1148 (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
1149 if ((*commit)->logmsg == NULL) {
1150 err = got_error_from_errno("malloc");
1153 while (remain > 0) {
1154 struct imsg imsg_log;
1155 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1158 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1162 if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG) {
1163 err = got_error(GOT_ERR_PRIVSEP_MSG);
1167 memcpy((*commit)->logmsg + offset,
1169 imsg_free(&imsg_log);
1173 (*commit)->logmsg[icommit->logmsg_len] = '\0';
1176 for (i = 0; i < icommit->nparents; i++) {
1177 struct got_object_qid *qid;
1179 err = got_object_qid_alloc_partial(&qid);
1182 memcpy(qid->id, imsg->data + len +
1183 i * SHA1_DIGEST_LENGTH, sizeof(*qid->id));
1184 SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
1185 (*commit)->nparents++;
1189 got_object_commit_close(*commit);
1195 const struct got_error *
1196 got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
1198 const struct got_error *err = NULL;
1201 const size_t min_datalen =
1202 MIN(sizeof(struct got_imsg_error),
1203 sizeof(struct got_imsg_commit_object));
1207 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1211 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1213 switch (imsg.hdr.type) {
1214 case GOT_IMSG_COMMIT:
1215 err = get_commit_from_imsg(commit, &imsg, datalen, ibuf);
1218 err = got_error(GOT_ERR_PRIVSEP_MSG);
1227 const struct got_error *
1228 got_privsep_send_tree(struct imsgbuf *ibuf, struct got_pathlist_head *entries,
1231 const struct got_error *err = NULL;
1232 struct got_imsg_tree_object itree;
1233 struct got_pathlist_entry *pe;
1235 int nimsg; /* number of imsg queued in ibuf */
1237 itree.nentries = nentries;
1238 if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
1240 return got_error_from_errno("imsg_compose TREE");
1242 totlen = sizeof(itree);
1244 TAILQ_FOREACH(pe, entries, entry) {
1245 const char *name = pe->path;
1246 struct got_parsed_tree_entry *pte = pe->data;
1248 size_t namelen = strlen(name);
1249 size_t len = sizeof(struct got_imsg_tree_entry) + namelen;
1251 if (len > MAX_IMSGSIZE)
1252 return got_error(GOT_ERR_NO_SPACE);
1255 if (totlen + len >= MAX_IMSGSIZE - (IMSG_HEADER_SIZE * nimsg)) {
1256 err = flush_imsg(ibuf);
1262 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_ENTRY, 0, 0, len);
1264 return got_error_from_errno("imsg_create TREE_ENTRY");
1266 /* Keep in sync with struct got_imsg_tree_object definition! */
1267 if (imsg_add(wbuf, pte->id, SHA1_DIGEST_LENGTH) == -1) {
1268 err = got_error_from_errno("imsg_add TREE_ENTRY");
1272 if (imsg_add(wbuf, &pte->mode, sizeof(pte->mode)) == -1) {
1273 err = got_error_from_errno("imsg_add TREE_ENTRY");
1278 if (imsg_add(wbuf, name, namelen) == -1) {
1279 err = got_error_from_errno("imsg_add TREE_ENTRY");
1285 imsg_close(ibuf, wbuf);
1290 return flush_imsg(ibuf);
1293 const struct got_error *
1294 got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
1296 const struct got_error *err = NULL;
1297 const size_t min_datalen =
1298 MIN(sizeof(struct got_imsg_error),
1299 sizeof(struct got_imsg_tree_object));
1300 struct got_imsg_tree_object *itree;
1305 err = read_imsg(ibuf);
1313 struct got_imsg_tree_entry *ite;
1314 struct got_tree_entry *te = NULL;
1316 n = imsg_get(ibuf, &imsg);
1318 if (*tree && (*tree)->nentries != nentries)
1323 if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen) {
1325 err = got_error(GOT_ERR_PRIVSEP_LEN);
1329 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1331 switch (imsg.hdr.type) {
1332 case GOT_IMSG_ERROR:
1333 err = recv_imsg_error(&imsg, datalen);
1336 /* This message should only appear once. */
1337 if (*tree != NULL) {
1338 err = got_error(GOT_ERR_PRIVSEP_MSG);
1341 if (datalen != sizeof(*itree)) {
1342 err = got_error(GOT_ERR_PRIVSEP_LEN);
1346 *tree = malloc(sizeof(**tree));
1347 if (*tree == NULL) {
1348 err = got_error_from_errno("malloc");
1351 (*tree)->entries = calloc(itree->nentries,
1352 sizeof(struct got_tree_entry));
1353 if ((*tree)->entries == NULL) {
1354 err = got_error_from_errno("malloc");
1357 (*tree)->nentries = itree->nentries;
1358 (*tree)->refcnt = 0;
1360 case GOT_IMSG_TREE_ENTRY:
1361 /* This message should be preceeded by GOT_IMSG_TREE. */
1362 if (*tree == NULL) {
1363 err = got_error(GOT_ERR_PRIVSEP_MSG);
1366 if (datalen < sizeof(*ite) || datalen > MAX_IMSGSIZE) {
1367 err = got_error(GOT_ERR_PRIVSEP_LEN);
1371 /* Remaining data contains the entry's name. */
1372 datalen -= sizeof(*ite);
1373 if (datalen == 0 || datalen > MAX_IMSGSIZE) {
1374 err = got_error(GOT_ERR_PRIVSEP_LEN);
1379 if (datalen + 1 > sizeof(te->name)) {
1380 err = got_error(GOT_ERR_NO_SPACE);
1383 te = &(*tree)->entries[nentries];
1384 memcpy(te->name, imsg.data + sizeof(*ite), datalen);
1385 te->name[datalen] = '\0';
1387 memcpy(te->id.sha1, ite->id, SHA1_DIGEST_LENGTH);
1388 te->mode = ite->mode;
1393 err = got_error(GOT_ERR_PRIVSEP_MSG);
1402 if (*tree && (*tree)->nentries != nentries) {
1404 err = got_error(GOT_ERR_PRIVSEP_LEN);
1405 got_object_tree_close(*tree);
1412 const struct got_error *
1413 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
1414 const uint8_t *data)
1416 struct got_imsg_blob iblob;
1419 iblob.hdrlen = hdrlen;
1424 if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
1425 return got_error(GOT_ERR_NO_SPACE);
1427 buf = malloc(sizeof(iblob) + size);
1429 return got_error_from_errno("malloc");
1431 memcpy(buf, &iblob, sizeof(iblob));
1432 memcpy(buf + sizeof(iblob), data, size);
1433 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
1434 sizeof(iblob) + size) == -1) {
1436 return got_error_from_errno("imsg_compose BLOB");
1440 /* Data has already been written to file descriptor. */
1441 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
1442 sizeof(iblob)) == -1)
1443 return got_error_from_errno("imsg_compose BLOB");
1447 return flush_imsg(ibuf);
1450 const struct got_error *
1451 got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1452 struct imsgbuf *ibuf)
1454 const struct got_error *err = NULL;
1456 struct got_imsg_blob *iblob;
1461 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1465 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1467 switch (imsg.hdr.type) {
1469 if (datalen < sizeof(*iblob)) {
1470 err = got_error(GOT_ERR_PRIVSEP_LEN);
1474 *size = iblob->size;
1475 *hdrlen = iblob->hdrlen;
1477 if (datalen == sizeof(*iblob)) {
1478 /* Data has been written to file descriptor. */
1482 if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX) {
1483 err = got_error(GOT_ERR_PRIVSEP_LEN);
1487 *outbuf = malloc(*size);
1488 if (*outbuf == NULL) {
1489 err = got_error_from_errno("malloc");
1492 memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
1495 err = got_error(GOT_ERR_PRIVSEP_MSG);
1504 static const struct got_error *
1505 send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
1507 const struct got_error *err = NULL;
1508 size_t offset, remain;
1511 remain = tagmsg_len;
1512 while (remain > 0) {
1513 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1515 if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
1516 tag->tagmsg + offset, n) == -1) {
1517 err = got_error_from_errno("imsg_compose TAG_TAGMSG");
1521 err = flush_imsg(ibuf);
1532 const struct got_error *
1533 got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
1535 const struct got_error *err = NULL;
1536 struct got_imsg_tag_object *itag;
1539 size_t tag_len = strlen(tag->tag);
1540 size_t tagger_len = strlen(tag->tagger);
1541 size_t tagmsg_len = strlen(tag->tagmsg);
1543 total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
1545 buf = malloc(total);
1547 return got_error_from_errno("malloc");
1549 itag = (struct got_imsg_tag_object *)buf;
1550 memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
1551 itag->obj_type = tag->obj_type;
1552 itag->tag_len = tag_len;
1553 itag->tagger_len = tagger_len;
1554 itag->tagger_time = tag->tagger_time;
1555 itag->tagger_gmtoff = tag->tagger_gmtoff;
1556 itag->tagmsg_len = tagmsg_len;
1558 len = sizeof(*itag);
1559 memcpy(buf + len, tag->tag, tag_len);
1561 memcpy(buf + len, tag->tagger, tagger_len);
1564 if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
1565 err = got_error_from_errno("imsg_compose TAG");
1569 if (tagmsg_len == 0 ||
1570 tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1571 err = flush_imsg(ibuf);
1575 err = send_tagmsg(ibuf, tag, tagmsg_len);
1581 const struct got_error *
1582 got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
1584 const struct got_error *err = NULL;
1586 struct got_imsg_tag_object *itag;
1587 size_t len, datalen;
1588 const size_t min_datalen =
1589 MIN(sizeof(struct got_imsg_error),
1590 sizeof(struct got_imsg_tag_object));
1594 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1598 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1601 switch (imsg.hdr.type) {
1603 if (datalen < sizeof(*itag)) {
1604 err = got_error(GOT_ERR_PRIVSEP_LEN);
1608 if (datalen != sizeof(*itag) + itag->tag_len +
1610 err = got_error(GOT_ERR_PRIVSEP_LEN);
1613 len += sizeof(*itag);
1615 *tag = calloc(1, sizeof(**tag));
1617 err = got_error_from_errno("calloc");
1621 memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
1623 if (itag->tag_len == 0) {
1624 (*tag)->tag = strdup("");
1625 if ((*tag)->tag == NULL) {
1626 err = got_error_from_errno("strdup");
1630 (*tag)->tag = malloc(itag->tag_len + 1);
1631 if ((*tag)->tag == NULL) {
1632 err = got_error_from_errno("malloc");
1635 memcpy((*tag)->tag, imsg.data + len,
1637 (*tag)->tag[itag->tag_len] = '\0';
1639 len += itag->tag_len;
1641 (*tag)->obj_type = itag->obj_type;
1642 (*tag)->tagger_time = itag->tagger_time;
1643 (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1645 if (itag->tagger_len == 0) {
1646 (*tag)->tagger = strdup("");
1647 if ((*tag)->tagger == NULL) {
1648 err = got_error_from_errno("strdup");
1652 (*tag)->tagger = malloc(itag->tagger_len + 1);
1653 if ((*tag)->tagger == NULL) {
1654 err = got_error_from_errno("malloc");
1657 memcpy((*tag)->tagger, imsg.data + len,
1659 (*tag)->tagger[itag->tagger_len] = '\0';
1661 len += itag->tagger_len;
1663 if (itag->tagmsg_len == 0) {
1664 (*tag)->tagmsg = strdup("");
1665 if ((*tag)->tagmsg == NULL) {
1666 err = got_error_from_errno("strdup");
1670 size_t offset = 0, remain = itag->tagmsg_len;
1672 (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1673 if ((*tag)->tagmsg == NULL) {
1674 err = got_error_from_errno("malloc");
1677 while (remain > 0) {
1678 struct imsg imsg_log;
1679 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1682 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1686 if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1687 return got_error(GOT_ERR_PRIVSEP_MSG);
1689 memcpy((*tag)->tagmsg + offset, imsg_log.data,
1691 imsg_free(&imsg_log);
1695 (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1700 err = got_error(GOT_ERR_PRIVSEP_MSG);
1709 const struct got_error *
1710 got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1711 struct got_packidx *packidx)
1713 const struct got_error *err = NULL;
1714 struct got_imsg_packidx ipackidx;
1715 struct got_imsg_pack ipack;
1718 ipackidx.len = packidx->len;
1719 fd = dup(packidx->fd);
1721 return got_error_from_errno("dup");
1723 if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
1724 sizeof(ipackidx)) == -1) {
1725 err = got_error_from_errno("imsg_compose PACKIDX");
1730 if (strlcpy(ipack.path_packfile, pack->path_packfile,
1731 sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
1732 return got_error(GOT_ERR_NO_SPACE);
1733 ipack.filesize = pack->filesize;
1737 return got_error_from_errno("dup");
1739 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
1741 err = got_error_from_errno("imsg_compose PACK");
1746 return flush_imsg(ibuf);
1749 const struct got_error *
1750 got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
1751 struct got_object_id *id)
1753 struct got_imsg_packed_object iobj;
1756 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
1758 if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
1759 &iobj, sizeof(iobj)) == -1)
1760 return got_error_from_errno("imsg_compose "
1761 "PACKED_OBJECT_REQUEST");
1763 return flush_imsg(ibuf);
1766 const struct got_error *
1767 got_privsep_send_packed_raw_obj_req(struct imsgbuf *ibuf, int idx,
1768 struct got_object_id *id)
1770 struct got_imsg_packed_object iobj;
1773 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
1775 if (imsg_compose(ibuf, GOT_IMSG_PACKED_RAW_OBJECT_REQUEST, 0, 0, -1,
1776 &iobj, sizeof(iobj)) == -1)
1777 return got_error_from_errno("imsg_compose "
1778 "PACKED_OBJECT_REQUEST");
1780 return flush_imsg(ibuf);
1783 const struct got_error *
1784 got_privsep_send_gitconfig_parse_req(struct imsgbuf *ibuf, int fd)
1786 const struct got_error *err = NULL;
1788 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_PARSE_REQUEST, 0, 0, fd,
1790 err = got_error_from_errno("imsg_compose "
1791 "GITCONFIG_PARSE_REQUEST");
1796 return flush_imsg(ibuf);
1799 const struct got_error *
1800 got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *ibuf)
1802 if (imsg_compose(ibuf,
1803 GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST, 0, 0, -1,
1805 return got_error_from_errno("imsg_compose "
1806 "GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST");
1808 return flush_imsg(ibuf);
1811 const struct got_error *
1812 got_privsep_send_gitconfig_repository_extensions_req(struct imsgbuf *ibuf)
1814 if (imsg_compose(ibuf,
1815 GOT_IMSG_GITCONFIG_REPOSITORY_EXTENSIONS_REQUEST, 0, 0, -1,
1817 return got_error_from_errno("imsg_compose "
1818 "GITCONFIG_REPOSITORY_EXTENSIONS_REQUEST");
1820 return flush_imsg(ibuf);
1824 const struct got_error *
1825 got_privsep_send_gitconfig_author_name_req(struct imsgbuf *ibuf)
1827 if (imsg_compose(ibuf,
1828 GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST, 0, 0, -1, NULL, 0) == -1)
1829 return got_error_from_errno("imsg_compose "
1830 "GITCONFIG_AUTHOR_NAME_REQUEST");
1832 return flush_imsg(ibuf);
1835 const struct got_error *
1836 got_privsep_send_gitconfig_author_email_req(struct imsgbuf *ibuf)
1838 if (imsg_compose(ibuf,
1839 GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST, 0, 0, -1, NULL, 0) == -1)
1840 return got_error_from_errno("imsg_compose "
1841 "GITCONFIG_AUTHOR_EMAIL_REQUEST");
1843 return flush_imsg(ibuf);
1846 const struct got_error *
1847 got_privsep_send_gitconfig_remotes_req(struct imsgbuf *ibuf)
1849 if (imsg_compose(ibuf,
1850 GOT_IMSG_GITCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
1851 return got_error_from_errno("imsg_compose "
1852 "GITCONFIG_REMOTE_REQUEST");
1854 return flush_imsg(ibuf);
1857 const struct got_error *
1858 got_privsep_send_gitconfig_owner_req(struct imsgbuf *ibuf)
1860 if (imsg_compose(ibuf,
1861 GOT_IMSG_GITCONFIG_OWNER_REQUEST, 0, 0, -1, NULL, 0) == -1)
1862 return got_error_from_errno("imsg_compose "
1863 "GITCONFIG_OWNER_REQUEST");
1865 return flush_imsg(ibuf);
1868 const struct got_error *
1869 got_privsep_recv_gitconfig_str(char **str, struct imsgbuf *ibuf)
1871 const struct got_error *err = NULL;
1874 const size_t min_datalen = 0;
1878 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1881 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1883 switch (imsg.hdr.type) {
1884 case GOT_IMSG_GITCONFIG_STR_VAL:
1887 /* datalen does not include terminating \0 */
1888 *str = malloc(datalen + 1);
1890 err = got_error_from_errno("malloc");
1893 memcpy(*str, imsg.data, datalen);
1894 (*str)[datalen] = '\0';
1897 err = got_error(GOT_ERR_PRIVSEP_MSG);
1905 const struct got_error *
1906 got_privsep_recv_gitconfig_int(int *val, struct imsgbuf *ibuf)
1908 const struct got_error *err = NULL;
1911 const size_t min_datalen =
1912 MIN(sizeof(struct got_imsg_error), sizeof(int));
1916 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1919 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1921 switch (imsg.hdr.type) {
1922 case GOT_IMSG_GITCONFIG_INT_VAL:
1923 if (datalen != sizeof(*val)) {
1924 err = got_error(GOT_ERR_PRIVSEP_LEN);
1927 memcpy(val, imsg.data, sizeof(*val));
1930 err = got_error(GOT_ERR_PRIVSEP_MSG);
1939 free_remote_data(struct got_remote_repo *remote)
1945 for (i = 0; i < remote->nbranches; i++)
1946 free(remote->branches[i]);
1947 free(remote->branches);
1948 for (i = 0; i < remote->nrefs; i++)
1949 free(remote->refs[i]);
1953 const struct got_error *
1954 got_privsep_recv_gitconfig_remotes(struct got_remote_repo **remotes,
1955 int *nremotes, struct imsgbuf *ibuf)
1957 const struct got_error *err = NULL;
1960 struct got_imsg_remotes iremotes;
1961 struct got_imsg_remote iremote;
1965 iremotes.nremotes = 0;
1967 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremotes));
1970 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1972 switch (imsg.hdr.type) {
1973 case GOT_IMSG_GITCONFIG_REMOTES:
1974 if (datalen != sizeof(iremotes)) {
1975 err = got_error(GOT_ERR_PRIVSEP_LEN);
1978 memcpy(&iremotes, imsg.data, sizeof(iremotes));
1979 if (iremotes.nremotes == 0) {
1986 return got_error(GOT_ERR_PRIVSEP_MSG);
1991 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(**remotes));
1992 if (*remotes == NULL)
1993 return got_error_from_errno("recallocarray");
1995 while (*nremotes < iremotes.nremotes) {
1996 struct got_remote_repo *remote;
1998 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremote));
2001 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2003 switch (imsg.hdr.type) {
2004 case GOT_IMSG_GITCONFIG_REMOTE:
2005 remote = &(*remotes)[*nremotes];
2006 memset(remote, 0, sizeof(*remote));
2007 if (datalen < sizeof(iremote)) {
2008 err = got_error(GOT_ERR_PRIVSEP_LEN);
2011 memcpy(&iremote, imsg.data, sizeof(iremote));
2012 if (iremote.name_len == 0 || iremote.url_len == 0 ||
2013 (sizeof(iremote) + iremote.name_len +
2014 iremote.url_len) > datalen) {
2015 err = got_error(GOT_ERR_PRIVSEP_LEN);
2018 remote->name = strndup(imsg.data + sizeof(iremote),
2020 if (remote->name == NULL) {
2021 err = got_error_from_errno("strndup");
2024 remote->url = strndup(imsg.data + sizeof(iremote) +
2025 iremote.name_len, iremote.url_len);
2026 if (remote->url == NULL) {
2027 err = got_error_from_errno("strndup");
2028 free_remote_data(remote);
2031 remote->mirror_references = iremote.mirror_references;
2032 remote->fetch_all_branches = iremote.fetch_all_branches;
2033 remote->nbranches = 0;
2034 remote->branches = NULL;
2036 remote->refs = NULL;
2040 err = got_error(GOT_ERR_PRIVSEP_MSG);
2051 for (i = 0; i < *nremotes; i++)
2052 free_remote_data(&(*remotes)[i]);
2060 const struct got_error *
2061 got_privsep_send_gotconfig_parse_req(struct imsgbuf *ibuf, int fd)
2063 const struct got_error *err = NULL;
2065 if (imsg_compose(ibuf, GOT_IMSG_GOTCONFIG_PARSE_REQUEST, 0, 0, fd,
2067 err = got_error_from_errno("imsg_compose "
2068 "GOTCONFIG_PARSE_REQUEST");
2073 return flush_imsg(ibuf);
2076 const struct got_error *
2077 got_privsep_send_gotconfig_author_req(struct imsgbuf *ibuf)
2079 if (imsg_compose(ibuf,
2080 GOT_IMSG_GOTCONFIG_AUTHOR_REQUEST, 0, 0, -1, NULL, 0) == -1)
2081 return got_error_from_errno("imsg_compose "
2082 "GOTCONFIG_AUTHOR_REQUEST");
2084 return flush_imsg(ibuf);
2087 const struct got_error *
2088 got_privsep_send_gotconfig_remotes_req(struct imsgbuf *ibuf)
2090 if (imsg_compose(ibuf,
2091 GOT_IMSG_GOTCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
2092 return got_error_from_errno("imsg_compose "
2093 "GOTCONFIG_REMOTE_REQUEST");
2095 return flush_imsg(ibuf);
2098 const struct got_error *
2099 got_privsep_recv_gotconfig_str(char **str, struct imsgbuf *ibuf)
2101 const struct got_error *err = NULL;
2104 const size_t min_datalen = 0;
2108 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2111 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2113 switch (imsg.hdr.type) {
2114 case GOT_IMSG_ERROR:
2115 if (datalen < sizeof(struct got_imsg_error)) {
2116 err = got_error(GOT_ERR_PRIVSEP_LEN);
2119 err = recv_imsg_error(&imsg, datalen);
2121 case GOT_IMSG_GOTCONFIG_STR_VAL:
2124 /* datalen does not include terminating \0 */
2125 *str = malloc(datalen + 1);
2127 err = got_error_from_errno("malloc");
2130 memcpy(*str, imsg.data, datalen);
2131 (*str)[datalen] = '\0';
2134 err = got_error(GOT_ERR_PRIVSEP_MSG);
2143 const struct got_error *
2144 got_privsep_recv_gotconfig_remotes(struct got_remote_repo **remotes,
2145 int *nremotes, struct imsgbuf *ibuf)
2147 const struct got_error *err = NULL;
2150 struct got_imsg_remotes iremotes;
2151 struct got_imsg_remote iremote;
2152 const size_t min_datalen =
2153 MIN(sizeof(struct got_imsg_error), sizeof(iremotes));
2157 iremotes.nremotes = 0;
2159 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2162 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2164 switch (imsg.hdr.type) {
2165 case GOT_IMSG_ERROR:
2166 if (datalen < sizeof(struct got_imsg_error)) {
2167 err = got_error(GOT_ERR_PRIVSEP_LEN);
2170 err = recv_imsg_error(&imsg, datalen);
2172 case GOT_IMSG_GOTCONFIG_REMOTES:
2173 if (datalen != sizeof(iremotes)) {
2174 err = got_error(GOT_ERR_PRIVSEP_LEN);
2177 memcpy(&iremotes, imsg.data, sizeof(iremotes));
2178 if (iremotes.nremotes == 0) {
2185 return got_error(GOT_ERR_PRIVSEP_MSG);
2190 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(**remotes));
2191 if (*remotes == NULL)
2192 return got_error_from_errno("recallocarray");
2194 while (*nremotes < iremotes.nremotes) {
2195 struct got_remote_repo *remote;
2196 const size_t min_datalen =
2197 MIN(sizeof(struct got_imsg_error), sizeof(iremote));
2200 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2203 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2205 switch (imsg.hdr.type) {
2206 case GOT_IMSG_ERROR:
2207 if (datalen < sizeof(struct got_imsg_error)) {
2208 err = got_error(GOT_ERR_PRIVSEP_LEN);
2211 err = recv_imsg_error(&imsg, datalen);
2213 case GOT_IMSG_GOTCONFIG_REMOTE:
2214 remote = &(*remotes)[*nremotes];
2215 memset(remote, 0, sizeof(*remote));
2216 if (datalen < sizeof(iremote)) {
2217 err = got_error(GOT_ERR_PRIVSEP_LEN);
2220 memcpy(&iremote, imsg.data, sizeof(iremote));
2221 if (iremote.name_len == 0 || iremote.url_len == 0 ||
2222 (sizeof(iremote) + iremote.name_len +
2223 iremote.url_len) > datalen) {
2224 err = got_error(GOT_ERR_PRIVSEP_LEN);
2227 remote->name = strndup(imsg.data + sizeof(iremote),
2229 if (remote->name == NULL) {
2230 err = got_error_from_errno("strndup");
2233 remote->url = strndup(imsg.data + sizeof(iremote) +
2234 iremote.name_len, iremote.url_len);
2235 if (remote->url == NULL) {
2236 err = got_error_from_errno("strndup");
2237 free_remote_data(remote);
2240 remote->mirror_references = iremote.mirror_references;
2241 remote->fetch_all_branches = iremote.fetch_all_branches;
2242 if (iremote.nbranches > 0) {
2243 remote->branches = recallocarray(NULL, 0,
2244 iremote.nbranches, sizeof(char *));
2245 if (remote->branches == NULL) {
2246 err = got_error_from_errno("calloc");
2247 free_remote_data(remote);
2251 remote->nbranches = 0;
2252 for (i = 0; i < iremote.nbranches; i++) {
2254 err = got_privsep_recv_gotconfig_str(&branch,
2257 free_remote_data(remote);
2260 remote->branches[i] = branch;
2261 remote->nbranches++;
2263 if (iremote.nrefs > 0) {
2264 remote->refs = recallocarray(NULL, 0,
2265 iremote.nrefs, sizeof(char *));
2266 if (remote->refs == NULL) {
2267 err = got_error_from_errno("calloc");
2268 free_remote_data(remote);
2273 for (i = 0; i < iremote.nrefs; i++) {
2275 err = got_privsep_recv_gotconfig_str(&ref,
2278 free_remote_data(remote);
2281 remote->refs[i] = ref;
2287 err = got_error(GOT_ERR_PRIVSEP_MSG);
2298 for (i = 0; i < *nremotes; i++)
2299 free_remote_data(&(*remotes)[i]);
2307 const struct got_error *
2308 got_privsep_send_commit_traversal_request(struct imsgbuf *ibuf,
2309 struct got_object_id *id, int idx, const char *path)
2311 const struct got_error *err = NULL;
2313 size_t path_len = strlen(path) + 1;
2315 wbuf = imsg_create(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_REQUEST, 0, 0,
2316 sizeof(struct got_imsg_commit_traversal_request) + path_len);
2318 return got_error_from_errno(
2319 "imsg_create COMMIT_TRAVERSAL_REQUEST");
2320 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
2321 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
2325 if (imsg_add(wbuf, &idx, sizeof(idx)) == -1) {
2326 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
2330 if (imsg_add(wbuf, path, path_len) == -1) {
2331 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
2337 imsg_close(ibuf, wbuf);
2339 return flush_imsg(ibuf);
2342 const struct got_error *
2343 got_privsep_recv_traversed_commits(struct got_commit_object **changed_commit,
2344 struct got_object_id **changed_commit_id,
2345 struct got_object_id_queue *commit_ids, struct imsgbuf *ibuf)
2347 const struct got_error *err = NULL;
2349 struct got_imsg_traversed_commits *icommits;
2353 *changed_commit = NULL;
2354 *changed_commit_id = NULL;
2357 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2361 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2362 switch (imsg.hdr.type) {
2363 case GOT_IMSG_TRAVERSED_COMMITS:
2364 icommits = imsg.data;
2365 if (datalen != sizeof(*icommits) +
2366 icommits->ncommits * SHA1_DIGEST_LENGTH) {
2367 err = got_error(GOT_ERR_PRIVSEP_LEN);
2370 for (i = 0; i < icommits->ncommits; i++) {
2371 struct got_object_qid *qid;
2372 uint8_t *sha1 = (uint8_t *)imsg.data +
2373 sizeof(*icommits) + i * SHA1_DIGEST_LENGTH;
2374 err = got_object_qid_alloc_partial(&qid);
2377 memcpy(qid->id->sha1, sha1, SHA1_DIGEST_LENGTH);
2378 SIMPLEQ_INSERT_TAIL(commit_ids, qid, entry);
2380 /* The last commit may contain a change. */
2381 if (i == icommits->ncommits - 1) {
2382 *changed_commit_id =
2383 got_object_id_dup(qid->id);
2384 if (*changed_commit_id == NULL) {
2385 err = got_error_from_errno(
2386 "got_object_id_dup");
2392 case GOT_IMSG_COMMIT:
2393 if (*changed_commit_id == NULL) {
2394 err = got_error(GOT_ERR_PRIVSEP_MSG);
2397 err = get_commit_from_imsg(changed_commit, &imsg,
2400 case GOT_IMSG_COMMIT_TRAVERSAL_DONE:
2404 err = got_error(GOT_ERR_PRIVSEP_MSG);
2414 got_object_id_queue_free(commit_ids);
2418 const struct got_error *
2419 got_privsep_unveil_exec_helpers(void)
2421 const char *helpers[] = {
2422 GOT_PATH_PROG_READ_PACK,
2423 GOT_PATH_PROG_READ_OBJECT,
2424 GOT_PATH_PROG_READ_COMMIT,
2425 GOT_PATH_PROG_READ_TREE,
2426 GOT_PATH_PROG_READ_BLOB,
2427 GOT_PATH_PROG_READ_TAG,
2428 GOT_PATH_PROG_READ_GITCONFIG,
2429 GOT_PATH_PROG_READ_GOTCONFIG,
2430 GOT_PATH_PROG_FETCH_PACK,
2431 GOT_PATH_PROG_INDEX_PACK,
2435 for (i = 0; i < nitems(helpers); i++) {
2436 if (unveil(helpers[i], "x") == 0)
2438 return got_error_from_errno2("unveil", helpers[i]);
2445 got_privsep_exec_child(int imsg_fds[2], const char *path, const char *repo_path)
2447 if (close(imsg_fds[0]) == -1) {
2448 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2452 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
2453 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2456 if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
2457 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2461 if (execl(path, path, repo_path, (char *)NULL) == -1) {
2462 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,