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>
38 #include "got_object.h"
39 #include "got_error.h"
41 #include "got_repository.h"
43 #include "got_lib_hash.h"
44 #include "got_lib_delta.h"
45 #include "got_lib_inflate.h"
46 #include "got_lib_object.h"
47 #include "got_lib_object_parse.h"
48 #include "got_lib_object_qid.h"
49 #include "got_lib_privsep.h"
50 #include "got_lib_pack.h"
51 #include "got_lib_poll.h"
53 #include "got_privsep.h"
56 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
60 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
63 static const struct got_error *
64 read_imsg(struct imsgbuf *ibuf)
66 const struct got_error *err;
69 err = got_poll_fd(ibuf->fd, POLLIN, INFTIM);
71 if (err->code == GOT_ERR_EOF)
72 return got_error(GOT_ERR_PRIVSEP_PIPE);
78 if (errno == EAGAIN) /* Could be a file-descriptor leak. */
79 return got_error(GOT_ERR_PRIVSEP_NO_FD);
80 return got_error(GOT_ERR_PRIVSEP_READ);
83 return got_error(GOT_ERR_PRIVSEP_PIPE);
88 const struct got_error *
89 got_privsep_wait_for_child(pid_t pid)
93 if (waitpid(pid, &child_status, 0) == -1)
94 return got_error_from_errno("waitpid");
96 if (!WIFEXITED(child_status))
97 return got_error(GOT_ERR_PRIVSEP_DIED);
99 if (WEXITSTATUS(child_status) != 0)
100 return got_error(GOT_ERR_PRIVSEP_EXIT);
105 static const struct got_error *
106 recv_imsg_error(struct imsg *imsg, size_t datalen)
108 struct got_imsg_error *ierr;
110 if (datalen != sizeof(*ierr))
111 return got_error(GOT_ERR_PRIVSEP_LEN);
114 if (ierr->code == GOT_ERR_ERRNO) {
115 static struct got_error serr;
116 serr.code = GOT_ERR_ERRNO;
117 serr.msg = strerror(ierr->errno_code);
121 return got_error(ierr->code);
124 const struct got_error *
125 got_privsep_recv_imsg(struct imsg *imsg, struct imsgbuf *ibuf,
128 const struct got_error *err;
131 n = imsg_get(ibuf, imsg);
133 return got_error_from_errno("imsg_get");
136 err = read_imsg(ibuf);
139 n = imsg_get(ibuf, imsg);
141 return got_error_from_errno("imsg_get");
144 if (imsg->hdr.len < IMSG_HEADER_SIZE + min_datalen) {
146 return got_error(GOT_ERR_PRIVSEP_LEN);
149 if (imsg->hdr.type == GOT_IMSG_ERROR) {
150 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
151 err = recv_imsg_error(imsg, datalen);
159 /* Attempt to send an error in an imsg. Complain on stderr as a last resort. */
161 got_privsep_send_error(struct imsgbuf *ibuf, const struct got_error *err)
163 const struct got_error *poll_err;
164 struct got_imsg_error ierr;
167 ierr.code = err->code;
168 if (err->code == GOT_ERR_ERRNO)
169 ierr.errno_code = errno;
172 ret = imsg_compose(ibuf, GOT_IMSG_ERROR, 0, 0, -1, &ierr, sizeof(ierr));
174 fprintf(stderr, "%s: error %d \"%s\": imsg_compose: %s\n",
175 getprogname(), err->code, err->msg, strerror(errno));
179 poll_err = got_poll_fd(ibuf->fd, POLLOUT, INFTIM);
181 fprintf(stderr, "%s: error %d \"%s\": poll: %s\n",
182 getprogname(), err->code, err->msg, poll_err->msg);
186 ret = imsg_flush(ibuf);
188 fprintf(stderr, "%s: error %d \"%s\": imsg_flush: %s\n",
189 getprogname(), err->code, err->msg, strerror(errno));
195 static const struct got_error *
196 flush_imsg(struct imsgbuf *ibuf)
198 const struct got_error *err;
200 err = got_poll_fd(ibuf->fd, POLLOUT, INFTIM);
204 if (imsg_flush(ibuf) == -1) {
206 return got_error_from_errno("imsg_flush");
212 const struct got_error *
213 got_privsep_flush_imsg(struct imsgbuf *ibuf)
215 return flush_imsg(ibuf);
218 const struct got_error *
219 got_privsep_send_stop(int fd)
223 imsg_init(&ibuf, fd);
225 if (imsg_compose(&ibuf, GOT_IMSG_STOP, 0, 0, -1, NULL, 0) == -1)
226 return got_error_from_errno("imsg_compose STOP");
228 return flush_imsg(&ibuf);
231 const struct got_error *
232 got_privsep_send_obj_req(struct imsgbuf *ibuf, int fd,
233 struct got_object_id *id)
235 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_REQUEST, 0, 0, fd,
236 id, sizeof(*id)) == -1)
237 return got_error_from_errno("imsg_compose OBJECT_REQUEST");
239 return flush_imsg(ibuf);
242 const struct got_error *
243 got_privsep_send_raw_obj_req(struct imsgbuf *ibuf, int fd,
244 struct got_object_id *id)
246 const struct got_error *err;
248 if (imsg_compose(ibuf, GOT_IMSG_RAW_OBJECT_REQUEST, 0, 0, fd,
249 id, sizeof(*id)) == -1) {
250 err = got_error_from_errno("imsg_compose RAW_OBJECT_REQUEST");
255 return flush_imsg(ibuf);
258 const struct got_error *
259 got_privsep_send_raw_obj_outfd(struct imsgbuf *ibuf, int outfd)
261 const struct got_error *err = NULL;
263 if (imsg_compose(ibuf, GOT_IMSG_RAW_OBJECT_OUTFD, 0, 0, outfd, NULL, 0)
265 err = got_error_from_errno("imsg_compose RAW_OBJECT_OUTFD");
270 return flush_imsg(ibuf);
273 const struct got_error *
274 got_privsep_send_raw_obj(struct imsgbuf *ibuf, off_t size, size_t hdrlen,
277 struct got_imsg_raw_obj iobj;
278 size_t len = sizeof(iobj);
281 memset(&iobj, 0, sizeof(iobj));
282 iobj.hdrlen = hdrlen;
285 if (data && size + hdrlen <= GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX)
286 len += (size_t)size + hdrlen;
288 wbuf = imsg_create(ibuf, GOT_IMSG_RAW_OBJECT, 0, 0, len);
290 return got_error_from_errno("imsg_create RAW_OBJECT");
292 if (imsg_add(wbuf, &iobj, sizeof(iobj)) == -1)
293 return got_error_from_errno("imsg_add RAW_OBJECT");
295 if (data && size + hdrlen <= GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX) {
296 if (imsg_add(wbuf, data, size + hdrlen) == -1)
297 return got_error_from_errno("imsg_add RAW_OBJECT");
300 imsg_close(ibuf, wbuf);
301 return flush_imsg(ibuf);
304 const struct got_error *
305 got_privsep_recv_raw_obj(uint8_t **outbuf, off_t *size, size_t *hdrlen,
306 struct imsgbuf *ibuf)
308 const struct got_error *err = NULL;
310 struct got_imsg_raw_obj *iobj;
315 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
319 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
321 switch (imsg.hdr.type) {
322 case GOT_IMSG_RAW_OBJECT:
323 if (datalen < sizeof(*iobj)) {
324 err = got_error(GOT_ERR_PRIVSEP_LEN);
329 *hdrlen = iobj->hdrlen;
331 if (datalen == sizeof(*iobj)) {
332 /* Data has been written to file descriptor. */
337 *size + *hdrlen > GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX) {
338 err = got_error(GOT_ERR_PRIVSEP_LEN);
342 *outbuf = malloc(*size + *hdrlen);
343 if (*outbuf == NULL) {
344 err = got_error_from_errno("malloc");
347 memcpy(*outbuf, imsg.data + sizeof(*iobj), *size + *hdrlen);
350 err = got_error(GOT_ERR_PRIVSEP_MSG);
359 const struct got_error *
360 got_privsep_send_commit_req(struct imsgbuf *ibuf, int fd,
361 struct got_object_id *id, int pack_idx)
363 const struct got_error *err = NULL;
364 struct got_imsg_packed_object iobj;
368 memset(&iobj, 0, sizeof(iobj));
369 if (pack_idx != -1) { /* commit is packed */
371 memcpy(&iobj.id, id, sizeof(iobj.id));
379 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_REQUEST, 0, 0, fd, data, len)
381 err = got_error_from_errno("imsg_compose COMMIT_REQUEST");
386 return flush_imsg(ibuf);
389 const struct got_error *
390 got_privsep_send_tree_req(struct imsgbuf *ibuf, int fd,
391 struct got_object_id *id, int pack_idx)
393 const struct got_error *err;
398 len = sizeof(struct got_imsg_packed_object);
402 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_REQUEST, 0, 0, len);
404 err = got_error_from_errno("imsg_create TREE_REQUEST");
410 if (imsg_add(wbuf, id, sizeof(*id)) == -1) {
411 err = got_error_from_errno("imsg_add TREE_REQUEST");
417 if (pack_idx != -1) { /* tree is packed */
418 if (imsg_add(wbuf, &pack_idx, sizeof(pack_idx)) == -1) {
419 err = got_error_from_errno("imsg_add TREE_REQUEST");
426 ibuf_fd_set(wbuf, fd);
427 imsg_close(ibuf, wbuf);
429 return flush_imsg(ibuf);
432 const struct got_error *
433 got_privsep_send_tag_req(struct imsgbuf *ibuf, int fd,
434 struct got_object_id *id, int pack_idx)
436 const struct got_error *err;
437 struct got_imsg_packed_object iobj;
441 memset(&iobj, 0, sizeof(iobj));
442 if (pack_idx != -1) { /* tag is packed */
444 memcpy(&iobj.id, id, sizeof(iobj.id));
452 if (imsg_compose(ibuf, GOT_IMSG_TAG_REQUEST, 0, 0, fd, data, len)
454 err = got_error_from_errno("imsg_compose TAG_REQUEST");
460 return flush_imsg(ibuf);
463 const struct got_error *
464 got_privsep_send_blob_req(struct imsgbuf *ibuf, int infd,
465 struct got_object_id *id, int pack_idx)
467 const struct got_error *err = NULL;
468 struct got_imsg_packed_object iobj;
472 memset(&iobj, 0, sizeof(iobj));
473 if (pack_idx != -1) { /* blob is packed */
475 memcpy(&iobj.id, id, sizeof(iobj.id));
483 if (imsg_compose(ibuf, GOT_IMSG_BLOB_REQUEST, 0, 0, infd, data, len)
485 err = got_error_from_errno("imsg_compose BLOB_REQUEST");
490 return flush_imsg(ibuf);
493 const struct got_error *
494 got_privsep_send_blob_outfd(struct imsgbuf *ibuf, int outfd)
496 const struct got_error *err = NULL;
498 if (imsg_compose(ibuf, GOT_IMSG_BLOB_OUTFD, 0, 0, outfd, NULL, 0)
500 err = got_error_from_errno("imsg_compose BLOB_OUTFD");
505 return flush_imsg(ibuf);
508 static const struct got_error *
509 send_fd(struct imsgbuf *ibuf, int imsg_code, int fd)
511 const struct got_error *err = NULL;
513 if (imsg_compose(ibuf, imsg_code, 0, 0, fd, NULL, 0) == -1) {
514 err = got_error_from_errno("imsg_compose TMPFD");
519 return flush_imsg(ibuf);
522 const struct got_error *
523 got_privsep_send_tmpfd(struct imsgbuf *ibuf, int fd)
525 return send_fd(ibuf, GOT_IMSG_TMPFD, fd);
528 const struct got_error *
529 got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj)
531 struct got_imsg_object iobj;
533 memset(&iobj, 0, sizeof(iobj));
535 memcpy(&iobj.id, &obj->id, sizeof(iobj.id));
536 iobj.type = obj->type;
537 iobj.flags = obj->flags;
538 iobj.hdrlen = obj->hdrlen;
539 iobj.size = obj->size;
540 if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
541 iobj.pack_offset = obj->pack_offset;
542 iobj.pack_idx = obj->pack_idx;
545 if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
547 return got_error_from_errno("imsg_compose OBJECT");
549 return flush_imsg(ibuf);
552 const struct got_error *
553 got_privsep_send_fetch_req(struct imsgbuf *ibuf, int fd,
554 struct got_pathlist_head *have_refs, int fetch_all_branches,
555 struct got_pathlist_head *wanted_branches,
556 struct got_pathlist_head *wanted_refs, int list_refs_only,
557 const char *worktree_branch, const char *remote_head,
558 int no_head, int verbosity)
560 const struct got_error *err = NULL;
562 struct got_pathlist_entry *pe;
563 struct got_imsg_fetch_request fetchreq;
564 size_t remote_head_len, worktree_branch_len, len = sizeof(fetchreq);
566 if (worktree_branch) {
567 worktree_branch_len = strlen(worktree_branch);
568 len += worktree_branch_len;
571 remote_head_len = strlen(remote_head);
572 len += remote_head_len;
575 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
577 return got_error(GOT_ERR_NO_SPACE);
580 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_REQUEST, 0, 0, len);
582 err = got_error_from_errno("imsg_create FETCH_HAVE_REF");
587 memset(&fetchreq, 0, sizeof(fetchreq));
588 fetchreq.no_head = no_head;
589 fetchreq.fetch_all_branches = fetch_all_branches;
590 fetchreq.list_refs_only = list_refs_only;
591 fetchreq.verbosity = verbosity;
592 if (worktree_branch != NULL)
593 fetchreq.worktree_branch_len = worktree_branch_len;
594 if (remote_head != NULL)
595 fetchreq.remote_head_len = remote_head_len;
596 TAILQ_FOREACH(pe, have_refs, entry)
597 fetchreq.n_have_refs++;
598 TAILQ_FOREACH(pe, wanted_branches, entry)
599 fetchreq.n_wanted_branches++;
600 TAILQ_FOREACH(pe, wanted_refs, entry)
601 fetchreq.n_wanted_refs++;
602 if (imsg_add(wbuf, &fetchreq, sizeof(fetchreq)) == -1)
603 return got_error_from_errno("imsg_add FETCH_REQUEST");
604 if (worktree_branch) {
605 if (imsg_add(wbuf, worktree_branch, worktree_branch_len)
607 err = got_error_from_errno("imsg_add FETCH_REQUEST");
613 if (imsg_add(wbuf, remote_head, remote_head_len) == -1) {
614 err = got_error_from_errno("imsg_add FETCH_REQUEST");
619 ibuf_fd_set(wbuf, fd);
621 imsg_close(ibuf, wbuf);
623 err = flush_imsg(ibuf);
627 TAILQ_FOREACH(pe, have_refs, entry) {
628 const char *name = pe->path;
629 size_t name_len = pe->path_len;
630 struct got_object_id *id = pe->data;
632 len = sizeof(struct got_imsg_fetch_have_ref) + name_len;
633 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_HAVE_REF, 0, 0, len);
635 return got_error_from_errno("imsg_create FETCH_HAVE_REF");
637 /* Keep in sync with struct got_imsg_fetch_have_ref! */
638 if (imsg_add(wbuf, id, sizeof(*id)) == -1)
639 return got_error_from_errno("imsg_add FETCH_HAVE_REF");
640 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1)
641 return got_error_from_errno("imsg_add FETCH_HAVE_REF");
642 if (imsg_add(wbuf, name, name_len) == -1)
643 return got_error_from_errno("imsg_add FETCH_HAVE_REF");
645 imsg_close(ibuf, wbuf);
646 err = flush_imsg(ibuf);
651 TAILQ_FOREACH(pe, wanted_branches, entry) {
652 const char *name = pe->path;
653 size_t name_len = pe->path_len;
655 len = sizeof(struct got_imsg_fetch_wanted_branch) + name_len;
656 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_WANTED_BRANCH, 0, 0,
659 return got_error_from_errno(
660 "imsg_create FETCH_WANTED_BRANCH");
662 /* Keep in sync with struct got_imsg_fetch_wanted_branch! */
663 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1)
664 return got_error_from_errno(
665 "imsg_add FETCH_WANTED_BRANCH");
666 if (imsg_add(wbuf, name, name_len) == -1)
667 return got_error_from_errno(
668 "imsg_add FETCH_WANTED_BRANCH");
670 imsg_close(ibuf, wbuf);
671 err = flush_imsg(ibuf);
676 TAILQ_FOREACH(pe, wanted_refs, entry) {
677 const char *name = pe->path;
678 size_t name_len = pe->path_len;
680 len = sizeof(struct got_imsg_fetch_wanted_ref) + name_len;
681 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_WANTED_REF, 0, 0,
684 return got_error_from_errno(
685 "imsg_create FETCH_WANTED_REF");
687 /* Keep in sync with struct got_imsg_fetch_wanted_ref! */
688 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1)
689 return got_error_from_errno(
690 "imsg_add FETCH_WANTED_REF");
691 if (imsg_add(wbuf, name, name_len) == -1)
692 return got_error_from_errno(
693 "imsg_add FETCH_WANTED_REF");
695 imsg_close(ibuf, wbuf);
696 err = flush_imsg(ibuf);
704 const struct got_error *
705 got_privsep_send_fetch_outfd(struct imsgbuf *ibuf, int fd)
707 return send_fd(ibuf, GOT_IMSG_FETCH_OUTFD, fd);
710 const struct got_error *
711 got_privsep_recv_fetch_progress(int *done, struct got_object_id **id,
712 char **refname, struct got_pathlist_head *symrefs, char **server_progress,
713 off_t *packfile_size, uint8_t *pack_sha1, struct imsgbuf *ibuf)
715 const struct got_error *err = NULL;
718 struct got_imsg_fetch_symrefs *isymrefs = NULL;
726 *server_progress = NULL;
728 memset(pack_sha1, 0, SHA1_DIGEST_LENGTH);
730 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
734 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
735 switch (imsg.hdr.type) {
736 case GOT_IMSG_FETCH_SYMREFS:
737 if (datalen < sizeof(*isymrefs)) {
738 err = got_error(GOT_ERR_PRIVSEP_LEN);
741 if (isymrefs != NULL) {
742 err = got_error(GOT_ERR_PRIVSEP_MSG);
745 isymrefs = (struct got_imsg_fetch_symrefs *)imsg.data;
746 off = sizeof(*isymrefs);
747 remain = datalen - off;
748 for (n = 0; n < isymrefs->nsymrefs; n++) {
749 struct got_imsg_fetch_symref *s;
751 if (remain < sizeof(struct got_imsg_fetch_symref)) {
752 err = got_error(GOT_ERR_PRIVSEP_LEN);
755 s = (struct got_imsg_fetch_symref *)(imsg.data + off);
757 remain -= sizeof(*s);
758 if (remain < s->name_len) {
759 err = got_error(GOT_ERR_PRIVSEP_LEN);
762 name = strndup(imsg.data + off, s->name_len);
764 err = got_error_from_errno("strndup");
768 remain -= s->name_len;
769 if (remain < s->target_len) {
770 err = got_error(GOT_ERR_PRIVSEP_LEN);
774 target = strndup(imsg.data + off, s->target_len);
775 if (target == NULL) {
776 err = got_error_from_errno("strndup");
780 off += s->target_len;
781 remain -= s->target_len;
782 err = got_pathlist_append(symrefs, name, target);
790 case GOT_IMSG_FETCH_REF:
791 if (datalen <= sizeof(**id)) {
792 err = got_error(GOT_ERR_PRIVSEP_MSG);
795 *id = malloc(sizeof(**id));
797 err = got_error_from_errno("malloc");
800 memcpy(*id, imsg.data, sizeof(**id));
801 *refname = strndup(imsg.data + sizeof(**id),
802 datalen - sizeof(**id));
803 if (*refname == NULL) {
804 err = got_error_from_errno("strndup");
808 case GOT_IMSG_FETCH_SERVER_PROGRESS:
810 err = got_error(GOT_ERR_PRIVSEP_LEN);
813 *server_progress = strndup(imsg.data, datalen);
814 if (*server_progress == NULL) {
815 err = got_error_from_errno("strndup");
818 for (i = 0; i < datalen; i++) {
819 if (!isprint((unsigned char)(*server_progress)[i]) &&
820 !isspace((unsigned char)(*server_progress)[i])) {
821 err = got_error(GOT_ERR_PRIVSEP_MSG);
822 free(*server_progress);
823 *server_progress = NULL;
828 case GOT_IMSG_FETCH_DOWNLOAD_PROGRESS:
829 if (datalen < sizeof(*packfile_size)) {
830 err = got_error(GOT_ERR_PRIVSEP_MSG);
833 memcpy(packfile_size, imsg.data, sizeof(*packfile_size));
835 case GOT_IMSG_FETCH_DONE:
836 if (datalen != SHA1_DIGEST_LENGTH) {
837 err = got_error(GOT_ERR_PRIVSEP_MSG);
840 memcpy(pack_sha1, imsg.data, SHA1_DIGEST_LENGTH);
844 err = got_error(GOT_ERR_PRIVSEP_MSG);
858 static const struct got_error *
859 send_send_ref(const char *name, size_t name_len, struct got_object_id *id,
860 int delete, struct imsgbuf *ibuf)
865 len = sizeof(struct got_imsg_send_ref) + name_len;
866 wbuf = imsg_create(ibuf, GOT_IMSG_SEND_REF, 0, 0, len);
868 return got_error_from_errno("imsg_create SEND_REF");
870 /* Keep in sync with struct got_imsg_send_ref! */
871 if (imsg_add(wbuf, id, sizeof(*id)) == -1)
872 return got_error_from_errno("imsg_add SEND_REF");
873 if (imsg_add(wbuf, &delete, sizeof(delete)) == -1)
874 return got_error_from_errno("imsg_add SEND_REF");
875 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1)
876 return got_error_from_errno("imsg_add SEND_REF");
877 if (imsg_add(wbuf, name, name_len) == -1)
878 return got_error_from_errno("imsg_add SEND_REF");
880 imsg_close(ibuf, wbuf);
881 return flush_imsg(ibuf);
884 const struct got_error *
885 got_privsep_send_send_req(struct imsgbuf *ibuf, int fd,
886 struct got_pathlist_head *have_refs,
887 struct got_pathlist_head *delete_refs,
890 const struct got_error *err = NULL;
891 struct got_pathlist_entry *pe;
892 struct got_imsg_send_request sendreq;
893 struct got_object_id zero_id;
895 memset(&zero_id, 0, sizeof(zero_id));
896 memset(&sendreq, 0, sizeof(sendreq));
897 sendreq.verbosity = verbosity;
898 TAILQ_FOREACH(pe, have_refs, entry)
900 TAILQ_FOREACH(pe, delete_refs, entry)
902 if (imsg_compose(ibuf, GOT_IMSG_SEND_REQUEST, 0, 0, fd,
903 &sendreq, sizeof(sendreq)) == -1) {
904 err = got_error_from_errno(
905 "imsg_compose FETCH_SERVER_PROGRESS");
910 err = flush_imsg(ibuf);
914 TAILQ_FOREACH(pe, have_refs, entry) {
915 const char *name = pe->path;
916 size_t name_len = pe->path_len;
917 struct got_object_id *id = pe->data;
918 err = send_send_ref(name, name_len, id, 0, ibuf);
923 TAILQ_FOREACH(pe, delete_refs, entry) {
924 const char *name = pe->path;
925 size_t name_len = pe->path_len;
926 err = send_send_ref(name, name_len, &zero_id, 1, ibuf);
931 if (fd != -1 && close(fd) == -1 && err == NULL)
932 err = got_error_from_errno("close");
936 const struct got_error *
937 got_privsep_recv_send_remote_refs(struct got_pathlist_head *remote_refs,
938 struct imsgbuf *ibuf)
940 const struct got_error *err = NULL;
944 struct got_imsg_send_remote_ref iremote_ref;
945 struct got_object_id *id = NULL;
946 char *refname = NULL;
947 struct got_pathlist_entry *new;
950 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
953 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
954 switch (imsg.hdr.type) {
955 case GOT_IMSG_SEND_REMOTE_REF:
956 if (datalen < sizeof(iremote_ref)) {
957 err = got_error(GOT_ERR_PRIVSEP_MSG);
960 memcpy(&iremote_ref, imsg.data, sizeof(iremote_ref));
961 if (datalen != sizeof(iremote_ref) +
962 iremote_ref.name_len) {
963 err = got_error(GOT_ERR_PRIVSEP_MSG);
966 id = malloc(sizeof(*id));
968 err = got_error_from_errno("malloc");
971 memcpy(id, &iremote_ref.id, sizeof(*id));
972 refname = strndup(imsg.data + sizeof(iremote_ref),
973 datalen - sizeof(iremote_ref));
974 if (refname == NULL) {
975 err = got_error_from_errno("strndup");
978 err = got_pathlist_insert(&new, remote_refs,
982 if (new == NULL) { /* duplicate which wasn't inserted */
989 case GOT_IMSG_SEND_PACK_REQUEST:
991 err = got_error(GOT_ERR_PRIVSEP_MSG);
994 /* got-send-pack is now waiting for a pack file. */
998 err = got_error(GOT_ERR_PRIVSEP_MSG);
1009 const struct got_error *
1010 got_privsep_send_packfd(struct imsgbuf *ibuf, int fd)
1012 return send_fd(ibuf, GOT_IMSG_SEND_PACKFD, fd);
1015 const struct got_error *
1016 got_privsep_recv_send_progress(int *done, off_t *bytes_sent,
1017 int *success, char **refname, char **errmsg, struct imsgbuf *ibuf)
1019 const struct got_error *err = NULL;
1022 struct got_imsg_send_ref_status iref_status;
1024 /* Do not reset the current value of 'bytes_sent', it accumulates. */
1030 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1034 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1035 switch (imsg.hdr.type) {
1036 case GOT_IMSG_SEND_UPLOAD_PROGRESS:
1037 if (datalen < sizeof(*bytes_sent)) {
1038 err = got_error(GOT_ERR_PRIVSEP_MSG);
1041 memcpy(bytes_sent, imsg.data, sizeof(*bytes_sent));
1043 case GOT_IMSG_SEND_REF_STATUS:
1044 if (datalen < sizeof(iref_status)) {
1045 err = got_error(GOT_ERR_PRIVSEP_MSG);
1048 memcpy(&iref_status, imsg.data, sizeof(iref_status));
1049 if (datalen != sizeof(iref_status) + iref_status.name_len +
1050 iref_status.errmsg_len) {
1051 err = got_error(GOT_ERR_PRIVSEP_MSG);
1054 *success = iref_status.success;
1055 *refname = strndup(imsg.data + sizeof(iref_status),
1056 iref_status.name_len);
1058 if (iref_status.errmsg_len != 0)
1059 *errmsg = strndup(imsg.data + sizeof(iref_status) +
1060 iref_status.name_len, iref_status.errmsg_len);
1062 case GOT_IMSG_SEND_DONE:
1064 err = got_error(GOT_ERR_PRIVSEP_MSG);
1070 err = got_error(GOT_ERR_PRIVSEP_MSG);
1078 const struct got_error *
1079 got_privsep_send_index_pack_req(struct imsgbuf *ibuf, uint8_t *pack_sha1,
1082 const struct got_error *err = NULL;
1084 /* Keep in sync with struct got_imsg_index_pack_request */
1085 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_REQUEST, 0, 0, fd,
1086 pack_sha1, SHA1_DIGEST_LENGTH) == -1) {
1087 err = got_error_from_errno("imsg_compose INDEX_REQUEST");
1091 return flush_imsg(ibuf);
1094 const struct got_error *
1095 got_privsep_send_index_pack_outfd(struct imsgbuf *ibuf, int fd)
1097 return send_fd(ibuf, GOT_IMSG_IDXPACK_OUTFD, fd);
1100 const struct got_error *
1101 got_privsep_recv_index_progress(int *done, int *nobj_total,
1102 int *nobj_indexed, int *nobj_loose, int *nobj_resolved,
1103 struct imsgbuf *ibuf)
1105 const struct got_error *err = NULL;
1107 struct got_imsg_index_pack_progress *iprogress;
1115 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1119 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1120 switch (imsg.hdr.type) {
1121 case GOT_IMSG_IDXPACK_PROGRESS:
1122 if (datalen < sizeof(*iprogress)) {
1123 err = got_error(GOT_ERR_PRIVSEP_LEN);
1126 iprogress = (struct got_imsg_index_pack_progress *)imsg.data;
1127 if (iprogress->nobj_total < 0 || iprogress->nobj_indexed < 0 ||
1128 iprogress->nobj_loose < 0 || iprogress->nobj_resolved < 0) {
1129 err = got_error(GOT_ERR_RANGE);
1132 *nobj_total = iprogress->nobj_total;
1133 *nobj_indexed = iprogress->nobj_indexed;
1134 *nobj_loose = iprogress->nobj_loose;
1135 *nobj_resolved = iprogress->nobj_resolved;
1137 case GOT_IMSG_IDXPACK_DONE:
1139 err = got_error(GOT_ERR_PRIVSEP_LEN);
1145 err = got_error(GOT_ERR_PRIVSEP_MSG);
1153 const struct got_error *
1154 got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
1155 struct imsgbuf *ibuf)
1157 struct got_imsg_object *iobj;
1158 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1160 if (datalen != sizeof(*iobj))
1161 return got_error(GOT_ERR_PRIVSEP_LEN);
1164 if (iobj->pack_offset < 0)
1165 return got_error(GOT_ERR_PACK_OFFSET);
1167 *obj = calloc(1, sizeof(**obj));
1169 return got_error_from_errno("calloc");
1171 memcpy(&(*obj)->id, &iobj->id, sizeof(iobj->id));
1172 (*obj)->type = iobj->type;
1173 (*obj)->flags = iobj->flags;
1174 (*obj)->hdrlen = iobj->hdrlen;
1175 (*obj)->size = iobj->size;
1176 /* path_packfile is handled by caller */
1177 if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
1178 (*obj)->pack_offset = iobj->pack_offset;
1179 (*obj)->pack_idx = iobj->pack_idx;
1181 STAILQ_INIT(&(*obj)->deltas.entries);
1185 const struct got_error *
1186 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
1188 const struct got_error *err = NULL;
1190 const size_t min_datalen =
1191 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
1195 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1199 switch (imsg.hdr.type) {
1200 case GOT_IMSG_OBJECT:
1201 err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
1204 err = got_error(GOT_ERR_PRIVSEP_MSG);
1213 static const struct got_error *
1214 send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
1217 const struct got_error *err = NULL;
1218 size_t offset, remain;
1221 remain = logmsg_len;
1222 while (remain > 0) {
1223 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1225 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
1226 commit->logmsg + offset, n) == -1) {
1227 err = got_error_from_errno("imsg_compose "
1232 err = flush_imsg(ibuf);
1243 const struct got_error *
1244 got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
1246 const struct got_error *err = NULL;
1247 struct got_imsg_commit_object *icommit;
1250 struct got_object_qid *qid;
1251 size_t author_len = strlen(commit->author);
1252 size_t committer_len = strlen(commit->committer);
1253 size_t logmsg_len = strlen(commit->logmsg);
1255 total = sizeof(*icommit) + author_len + committer_len +
1256 commit->nparents * sizeof(struct got_object_id);
1258 buf = malloc(total);
1260 return got_error_from_errno("malloc");
1262 icommit = (struct got_imsg_commit_object *)buf;
1263 memcpy(&icommit->tree_id, commit->tree_id, sizeof(icommit->tree_id));
1264 icommit->author_len = author_len;
1265 icommit->author_time = commit->author_time;
1266 icommit->author_gmtoff = commit->author_gmtoff;
1267 icommit->committer_len = committer_len;
1268 icommit->committer_time = commit->committer_time;
1269 icommit->committer_gmtoff = commit->committer_gmtoff;
1270 icommit->logmsg_len = logmsg_len;
1271 icommit->nparents = commit->nparents;
1273 len = sizeof(*icommit);
1274 memcpy(buf + len, commit->author, author_len);
1276 memcpy(buf + len, commit->committer, committer_len);
1277 len += committer_len;
1278 STAILQ_FOREACH(qid, &commit->parent_ids, entry) {
1279 memcpy(buf + len, &qid->id, sizeof(qid->id));
1280 len += sizeof(qid->id);
1283 if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
1284 err = got_error_from_errno("imsg_compose COMMIT");
1288 if (logmsg_len == 0 ||
1289 logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1290 err = flush_imsg(ibuf);
1294 err = send_commit_logmsg(ibuf, commit, logmsg_len);
1300 static const struct got_error *
1301 get_commit_from_imsg(struct got_commit_object **commit,
1302 struct imsg *imsg, size_t datalen, struct imsgbuf *ibuf)
1304 const struct got_error *err = NULL;
1305 struct got_imsg_commit_object *icommit;
1309 if (datalen < sizeof(*icommit))
1310 return got_error(GOT_ERR_PRIVSEP_LEN);
1312 icommit = imsg->data;
1313 if (datalen != sizeof(*icommit) + icommit->author_len +
1314 icommit->committer_len +
1315 icommit->nparents * sizeof(struct got_object_id))
1316 return got_error(GOT_ERR_PRIVSEP_LEN);
1318 if (icommit->nparents < 0)
1319 return got_error(GOT_ERR_PRIVSEP_LEN);
1321 len += sizeof(*icommit);
1323 *commit = got_object_commit_alloc_partial();
1324 if (*commit == NULL)
1325 return got_error_from_errno(
1326 "got_object_commit_alloc_partial");
1328 memcpy((*commit)->tree_id, &icommit->tree_id,
1329 sizeof(icommit->tree_id));
1330 (*commit)->author_time = icommit->author_time;
1331 (*commit)->author_gmtoff = icommit->author_gmtoff;
1332 (*commit)->committer_time = icommit->committer_time;
1333 (*commit)->committer_gmtoff = icommit->committer_gmtoff;
1335 (*commit)->author = strndup(imsg->data + len, icommit->author_len);
1336 if ((*commit)->author == NULL) {
1337 err = got_error_from_errno("strndup");
1340 len += icommit->author_len;
1342 (*commit)->committer = strndup(imsg->data + len,
1343 icommit->committer_len);
1344 if ((*commit)->committer == NULL) {
1345 err = got_error_from_errno("strndup");
1348 len += icommit->committer_len;
1350 if (icommit->logmsg_len == 0) {
1351 (*commit)->logmsg = strdup("");
1352 if ((*commit)->logmsg == NULL) {
1353 err = got_error_from_errno("strdup");
1357 size_t offset = 0, remain = icommit->logmsg_len;
1359 (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
1360 if ((*commit)->logmsg == NULL) {
1361 err = got_error_from_errno("malloc");
1364 while (remain > 0) {
1365 struct imsg imsg_log;
1366 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1369 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1373 if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG) {
1374 err = got_error(GOT_ERR_PRIVSEP_MSG);
1378 memcpy((*commit)->logmsg + offset,
1380 imsg_free(&imsg_log);
1384 (*commit)->logmsg[icommit->logmsg_len] = '\0';
1387 for (i = 0; i < icommit->nparents; i++) {
1388 struct got_object_qid *qid;
1390 err = got_object_qid_alloc_partial(&qid);
1393 memcpy(&qid->id, imsg->data + len +
1394 i * sizeof(qid->id), sizeof(qid->id));
1395 STAILQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
1396 (*commit)->nparents++;
1400 got_object_commit_close(*commit);
1406 const struct got_error *
1407 got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
1409 const struct got_error *err = NULL;
1412 const size_t min_datalen =
1413 MIN(sizeof(struct got_imsg_error),
1414 sizeof(struct got_imsg_commit_object));
1418 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1422 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1424 switch (imsg.hdr.type) {
1425 case GOT_IMSG_COMMIT:
1426 err = get_commit_from_imsg(commit, &imsg, datalen, ibuf);
1429 err = got_error(GOT_ERR_PRIVSEP_MSG);
1438 static const struct got_error *
1439 send_tree_entries_batch(struct imsgbuf *ibuf,
1440 struct got_parsed_tree_entry *entries, int idx0, int idxN, size_t len)
1443 struct got_imsg_tree_entries ientries;
1446 memset(&ientries, 0, sizeof(ientries));
1448 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_ENTRIES, 0, 0, len);
1450 return got_error_from_errno("imsg_create TREE_ENTRY");
1452 ientries.nentries = idxN - idx0 + 1;
1453 if (imsg_add(wbuf, &ientries, sizeof(ientries)) == -1)
1454 return got_error_from_errno("imsg_add TREE_ENTRY");
1456 for (i = idx0; i <= idxN; i++) {
1457 struct got_parsed_tree_entry *pte = &entries[i];
1459 /* Keep in sync with struct got_imsg_tree_entry definition! */
1460 if (imsg_add(wbuf, pte->id, SHA1_DIGEST_LENGTH) == -1)
1461 return got_error_from_errno("imsg_add TREE_ENTRY");
1462 if (imsg_add(wbuf, &pte->mode, sizeof(pte->mode)) == -1)
1463 return got_error_from_errno("imsg_add TREE_ENTRY");
1464 if (imsg_add(wbuf, &pte->namelen, sizeof(pte->namelen)) == -1)
1465 return got_error_from_errno("imsg_add TREE_ENTRY");
1467 /* Remaining bytes are the entry's name. */
1468 if (imsg_add(wbuf, pte->name, pte->namelen) == -1)
1469 return got_error_from_errno("imsg_add TREE_ENTRY");
1472 imsg_close(ibuf, wbuf);
1476 static const struct got_error *
1477 send_tree_entries(struct imsgbuf *ibuf, struct got_parsed_tree_entry *entries,
1480 const struct got_error *err = NULL;
1482 size_t entries_len = sizeof(struct got_imsg_tree_entries);
1485 for (j = 0; j < nentries; j++) {
1486 struct got_parsed_tree_entry *pte = &entries[j];
1487 size_t len = sizeof(struct got_imsg_tree_entry) + pte->namelen;
1490 entries_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1491 err = send_tree_entries_batch(ibuf, entries,
1492 i, j - 1, entries_len);
1496 entries_len = sizeof(struct got_imsg_tree_entries);
1503 err = send_tree_entries_batch(ibuf, entries, i, j - 1,
1512 const struct got_error *
1513 got_privsep_send_tree(struct imsgbuf *ibuf,
1514 struct got_parsed_tree_entry *entries, int nentries)
1516 const struct got_error *err = NULL;
1517 struct got_imsg_tree_object itree;
1519 memset(&itree, 0, sizeof(itree));
1520 itree.nentries = nentries;
1521 if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
1523 return got_error_from_errno("imsg_compose TREE");
1525 err = send_tree_entries(ibuf, entries, nentries);
1529 return flush_imsg(ibuf);
1533 static const struct got_error *
1534 recv_tree_entries(void *data, size_t datalen, struct got_tree_object *tree,
1537 const struct got_error *err = NULL;
1538 struct got_imsg_tree_entries *ientries;
1539 struct got_tree_entry *te;
1543 if (datalen <= sizeof(*ientries) ||
1544 datalen > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
1545 return got_error(GOT_ERR_PRIVSEP_LEN);
1547 ientries = (struct got_imsg_tree_entries *)data;
1548 if (ientries->nentries > INT_MAX) {
1549 return got_error_msg(GOT_ERR_NO_SPACE,
1550 "too many tree entries");
1553 te_offset = sizeof(*ientries);
1554 for (i = 0; i < ientries->nentries; i++) {
1555 struct got_imsg_tree_entry ite;
1556 const char *te_name;
1557 uint8_t *buf = (uint8_t *)data + te_offset;
1559 if (te_offset >= datalen) {
1560 err = got_error(GOT_ERR_PRIVSEP_LEN);
1564 /* Might not be aligned, size is ~32 bytes. */
1565 memcpy(&ite, buf, sizeof(ite));
1567 if (ite.namelen >= sizeof(te->name)) {
1568 err = got_error(GOT_ERR_PRIVSEP_LEN);
1571 if (te_offset + sizeof(ite) + ite.namelen > datalen) {
1572 err = got_error(GOT_ERR_PRIVSEP_LEN);
1576 if (*nentries >= tree->nentries) {
1577 err = got_error(GOT_ERR_PRIVSEP_LEN);
1580 te = &tree->entries[*nentries];
1581 te_name = buf + sizeof(ite);
1582 memcpy(te->name, te_name, ite.namelen);
1583 te->name[ite.namelen] = '\0';
1584 memcpy(te->id.sha1, ite.id, SHA1_DIGEST_LENGTH);
1585 te->mode = ite.mode;
1586 te->idx = *nentries;
1589 te_offset += sizeof(ite) + ite.namelen;
1595 const struct got_error *
1596 got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
1598 const struct got_error *err = NULL;
1599 const size_t min_datalen =
1600 MIN(sizeof(struct got_imsg_error),
1601 sizeof(struct got_imsg_tree_object));
1602 struct got_imsg_tree_object *itree;
1607 while (*tree == NULL || nentries < (*tree)->nentries) {
1611 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1615 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1617 switch (imsg.hdr.type) {
1619 /* This message should only appear once. */
1620 if (*tree != NULL) {
1621 err = got_error(GOT_ERR_PRIVSEP_MSG);
1624 if (datalen != sizeof(*itree)) {
1625 err = got_error(GOT_ERR_PRIVSEP_LEN);
1629 if (itree->nentries < 0) {
1630 err = got_error(GOT_ERR_PRIVSEP_LEN);
1633 *tree = malloc(sizeof(**tree));
1634 if (*tree == NULL) {
1635 err = got_error_from_errno("malloc");
1638 (*tree)->entries = calloc(itree->nentries,
1639 sizeof(struct got_tree_entry));
1640 if ((*tree)->entries == NULL) {
1641 err = got_error_from_errno("malloc");
1646 (*tree)->nentries = itree->nentries;
1647 (*tree)->refcnt = 0;
1649 case GOT_IMSG_TREE_ENTRIES:
1650 /* This message should be preceeded by GOT_IMSG_TREE. */
1651 if (*tree == NULL) {
1652 err = got_error(GOT_ERR_PRIVSEP_MSG);
1655 err = recv_tree_entries(imsg.data, datalen,
1659 err = got_error(GOT_ERR_PRIVSEP_MSG);
1668 if (*tree && (*tree)->nentries != nentries) {
1670 err = got_error(GOT_ERR_PRIVSEP_LEN);
1671 got_object_tree_close(*tree);
1678 const struct got_error *
1679 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
1680 const uint8_t *data)
1682 struct got_imsg_blob iblob;
1684 memset(&iblob, 0, sizeof(iblob));
1686 iblob.hdrlen = hdrlen;
1691 if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
1692 return got_error(GOT_ERR_NO_SPACE);
1694 buf = malloc(sizeof(iblob) + size);
1696 return got_error_from_errno("malloc");
1698 memcpy(buf, &iblob, sizeof(iblob));
1699 memcpy(buf + sizeof(iblob), data, size);
1700 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
1701 sizeof(iblob) + size) == -1) {
1703 return got_error_from_errno("imsg_compose BLOB");
1707 /* Data has already been written to file descriptor. */
1708 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
1709 sizeof(iblob)) == -1)
1710 return got_error_from_errno("imsg_compose BLOB");
1714 return flush_imsg(ibuf);
1717 const struct got_error *
1718 got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1719 struct imsgbuf *ibuf)
1721 const struct got_error *err = NULL;
1723 struct got_imsg_blob *iblob;
1728 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1732 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1734 switch (imsg.hdr.type) {
1736 if (datalen < sizeof(*iblob)) {
1737 err = got_error(GOT_ERR_PRIVSEP_LEN);
1741 *size = iblob->size;
1742 *hdrlen = iblob->hdrlen;
1744 if (datalen == sizeof(*iblob)) {
1745 /* Data has been written to file descriptor. */
1749 if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX ||
1750 *size > datalen + sizeof(*iblob)) {
1751 err = got_error(GOT_ERR_PRIVSEP_LEN);
1755 *outbuf = malloc(*size);
1756 if (*outbuf == NULL) {
1757 err = got_error_from_errno("malloc");
1760 memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
1763 err = got_error(GOT_ERR_PRIVSEP_MSG);
1772 static const struct got_error *
1773 send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
1775 const struct got_error *err = NULL;
1776 size_t offset, remain;
1779 remain = tagmsg_len;
1780 while (remain > 0) {
1781 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1783 if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
1784 tag->tagmsg + offset, n) == -1) {
1785 err = got_error_from_errno("imsg_compose TAG_TAGMSG");
1789 err = flush_imsg(ibuf);
1800 const struct got_error *
1801 got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
1803 const struct got_error *err = NULL;
1804 struct got_imsg_tag_object *itag;
1807 size_t tag_len = strlen(tag->tag);
1808 size_t tagger_len = strlen(tag->tagger);
1809 size_t tagmsg_len = strlen(tag->tagmsg);
1811 total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
1813 buf = malloc(total);
1815 return got_error_from_errno("malloc");
1817 itag = (struct got_imsg_tag_object *)buf;
1818 memcpy(&itag->id, &tag->id, sizeof(itag->id));
1819 itag->obj_type = tag->obj_type;
1820 itag->tag_len = tag_len;
1821 itag->tagger_len = tagger_len;
1822 itag->tagger_time = tag->tagger_time;
1823 itag->tagger_gmtoff = tag->tagger_gmtoff;
1824 itag->tagmsg_len = tagmsg_len;
1826 len = sizeof(*itag);
1827 memcpy(buf + len, tag->tag, tag_len);
1829 memcpy(buf + len, tag->tagger, tagger_len);
1832 if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
1833 err = got_error_from_errno("imsg_compose TAG");
1837 if (tagmsg_len == 0 ||
1838 tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1839 err = flush_imsg(ibuf);
1843 err = send_tagmsg(ibuf, tag, tagmsg_len);
1849 const struct got_error *
1850 got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
1852 const struct got_error *err = NULL;
1854 struct got_imsg_tag_object *itag;
1855 size_t len, datalen;
1856 const size_t min_datalen =
1857 MIN(sizeof(struct got_imsg_error),
1858 sizeof(struct got_imsg_tag_object));
1862 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1866 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1869 switch (imsg.hdr.type) {
1871 if (datalen < sizeof(*itag)) {
1872 err = got_error(GOT_ERR_PRIVSEP_LEN);
1876 if (datalen != sizeof(*itag) + itag->tag_len +
1878 err = got_error(GOT_ERR_PRIVSEP_LEN);
1881 len += sizeof(*itag);
1883 *tag = calloc(1, sizeof(**tag));
1885 err = got_error_from_errno("calloc");
1889 memcpy(&(*tag)->id, &itag->id, sizeof(itag->id));
1891 (*tag)->tag = strndup(imsg.data + len, itag->tag_len);
1892 if ((*tag)->tag == NULL) {
1893 err = got_error_from_errno("strndup");
1896 len += itag->tag_len;
1898 (*tag)->obj_type = itag->obj_type;
1899 (*tag)->tagger_time = itag->tagger_time;
1900 (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1902 (*tag)->tagger = strndup(imsg.data + len, itag->tagger_len);
1903 if ((*tag)->tagger == NULL) {
1904 err = got_error_from_errno("strndup");
1907 len += itag->tagger_len;
1909 if (itag->tagmsg_len == 0) {
1910 (*tag)->tagmsg = strdup("");
1911 if ((*tag)->tagmsg == NULL) {
1912 err = got_error_from_errno("strdup");
1916 size_t offset = 0, remain = itag->tagmsg_len;
1918 (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1919 if ((*tag)->tagmsg == NULL) {
1920 err = got_error_from_errno("malloc");
1923 while (remain > 0) {
1924 struct imsg imsg_log;
1925 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1928 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1932 if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1933 return got_error(GOT_ERR_PRIVSEP_MSG);
1935 memcpy((*tag)->tagmsg + offset, imsg_log.data,
1937 imsg_free(&imsg_log);
1941 (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1946 err = got_error(GOT_ERR_PRIVSEP_MSG);
1955 const struct got_error *
1956 got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1957 struct got_packidx *packidx)
1959 const struct got_error *err = NULL;
1960 struct got_imsg_packidx ipackidx;
1961 struct got_imsg_pack ipack;
1964 memset(&ipackidx, 0, sizeof(ipackidx));
1965 memset(&ipack, 0, sizeof(ipack));
1967 ipackidx.len = packidx->len;
1968 ipackidx.packfile_size = pack->filesize;
1969 fd = dup(packidx->fd);
1971 return got_error_from_errno("dup");
1973 if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
1974 sizeof(ipackidx)) == -1) {
1975 err = got_error_from_errno("imsg_compose PACKIDX");
1980 if (strlcpy(ipack.path_packfile, pack->path_packfile,
1981 sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
1982 return got_error(GOT_ERR_NO_SPACE);
1983 ipack.filesize = pack->filesize;
1987 return got_error_from_errno("dup");
1989 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
1991 err = got_error_from_errno("imsg_compose PACK");
1996 return flush_imsg(ibuf);
1999 const struct got_error *
2000 got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
2001 struct got_object_id *id)
2003 struct got_imsg_packed_object iobj;
2005 memset(&iobj, 0, sizeof(iobj));
2007 memcpy(&iobj.id, id, sizeof(iobj.id));
2009 if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
2010 &iobj, sizeof(iobj)) == -1)
2011 return got_error_from_errno("imsg_compose "
2012 "PACKED_OBJECT_REQUEST");
2014 return flush_imsg(ibuf);
2017 const struct got_error *
2018 got_privsep_send_packed_raw_obj_req(struct imsgbuf *ibuf, int idx,
2019 struct got_object_id *id)
2021 struct got_imsg_packed_object iobj;
2023 memset(&iobj, 0, sizeof(iobj));
2025 memcpy(&iobj.id, id, sizeof(iobj.id));
2027 if (imsg_compose(ibuf, GOT_IMSG_PACKED_RAW_OBJECT_REQUEST, 0, 0, -1,
2028 &iobj, sizeof(iobj)) == -1)
2029 return got_error_from_errno("imsg_compose "
2030 "PACKED_OBJECT_REQUEST");
2032 return flush_imsg(ibuf);
2035 const struct got_error *
2036 got_privsep_send_gitconfig_parse_req(struct imsgbuf *ibuf, int fd)
2038 const struct got_error *err = NULL;
2040 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_PARSE_REQUEST, 0, 0, fd,
2042 err = got_error_from_errno("imsg_compose "
2043 "GITCONFIG_PARSE_REQUEST");
2048 return flush_imsg(ibuf);
2051 const struct got_error *
2052 got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *ibuf)
2054 if (imsg_compose(ibuf,
2055 GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST, 0, 0, -1,
2057 return got_error_from_errno("imsg_compose "
2058 "GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST");
2060 return flush_imsg(ibuf);
2063 const struct got_error *
2064 got_privsep_send_gitconfig_repository_extensions_req(struct imsgbuf *ibuf)
2066 if (imsg_compose(ibuf,
2067 GOT_IMSG_GITCONFIG_REPOSITORY_EXTENSIONS_REQUEST, 0, 0, -1,
2069 return got_error_from_errno("imsg_compose "
2070 "GITCONFIG_REPOSITORY_EXTENSIONS_REQUEST");
2072 return flush_imsg(ibuf);
2076 const struct got_error *
2077 got_privsep_send_gitconfig_author_name_req(struct imsgbuf *ibuf)
2079 if (imsg_compose(ibuf,
2080 GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST, 0, 0, -1, NULL, 0) == -1)
2081 return got_error_from_errno("imsg_compose "
2082 "GITCONFIG_AUTHOR_NAME_REQUEST");
2084 return flush_imsg(ibuf);
2087 const struct got_error *
2088 got_privsep_send_gitconfig_author_email_req(struct imsgbuf *ibuf)
2090 if (imsg_compose(ibuf,
2091 GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST, 0, 0, -1, NULL, 0) == -1)
2092 return got_error_from_errno("imsg_compose "
2093 "GITCONFIG_AUTHOR_EMAIL_REQUEST");
2095 return flush_imsg(ibuf);
2098 const struct got_error *
2099 got_privsep_send_gitconfig_remotes_req(struct imsgbuf *ibuf)
2101 if (imsg_compose(ibuf,
2102 GOT_IMSG_GITCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
2103 return got_error_from_errno("imsg_compose "
2104 "GITCONFIG_REMOTE_REQUEST");
2106 return flush_imsg(ibuf);
2109 const struct got_error *
2110 got_privsep_send_gitconfig_owner_req(struct imsgbuf *ibuf)
2112 if (imsg_compose(ibuf,
2113 GOT_IMSG_GITCONFIG_OWNER_REQUEST, 0, 0, -1, NULL, 0) == -1)
2114 return got_error_from_errno("imsg_compose "
2115 "GITCONFIG_OWNER_REQUEST");
2117 return flush_imsg(ibuf);
2120 const struct got_error *
2121 got_privsep_recv_gitconfig_str(char **str, struct imsgbuf *ibuf)
2123 const struct got_error *err = NULL;
2129 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2132 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2134 switch (imsg.hdr.type) {
2135 case GOT_IMSG_GITCONFIG_STR_VAL:
2138 *str = strndup(imsg.data, datalen);
2140 err = got_error_from_errno("strndup");
2145 err = got_error(GOT_ERR_PRIVSEP_MSG);
2153 const struct got_error *
2154 got_privsep_recv_gitconfig_pair(char **key, char **val, struct imsgbuf *ibuf)
2156 const struct got_error *err = NULL;
2157 struct got_imsg_gitconfig_pair p;
2164 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2169 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2171 if (imsg.hdr.type != GOT_IMSG_GITCONFIG_PAIR) {
2172 err = got_error(GOT_ERR_PRIVSEP_MSG);
2176 if (datalen < sizeof(p)) {
2177 err = got_error(GOT_ERR_PRIVSEP_LEN);
2181 memcpy(&p, data, sizeof(p));
2184 if (datalen != sizeof(p) + p.klen + p.vlen) {
2185 err = got_error(GOT_ERR_PRIVSEP_LEN);
2189 *key = strndup(data, p.klen);
2191 err = got_error_from_errno("strndup");
2196 *val = strndup(data, p.vlen);
2198 err = got_error_from_errno("strndup");
2207 const struct got_error *
2208 got_privsep_recv_gitconfig_int(int *val, struct imsgbuf *ibuf)
2210 const struct got_error *err = NULL;
2213 const size_t min_datalen =
2214 MIN(sizeof(struct got_imsg_error), sizeof(int));
2218 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2221 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2223 switch (imsg.hdr.type) {
2224 case GOT_IMSG_GITCONFIG_INT_VAL:
2225 if (datalen != sizeof(*val)) {
2226 err = got_error(GOT_ERR_PRIVSEP_LEN);
2229 memcpy(val, imsg.data, sizeof(*val));
2232 err = got_error(GOT_ERR_PRIVSEP_MSG);
2241 free_remote_data(struct got_remote_repo *remote)
2246 free(remote->fetch_url);
2247 free(remote->send_url);
2248 for (i = 0; i < remote->nfetch_branches; i++)
2249 free(remote->fetch_branches[i]);
2250 free(remote->fetch_branches);
2251 for (i = 0; i < remote->nsend_branches; i++)
2252 free(remote->send_branches[i]);
2253 free(remote->send_branches);
2254 for (i = 0; i < remote->nfetch_refs; i++)
2255 free(remote->fetch_refs[i]);
2256 free(remote->fetch_refs);
2259 const struct got_error *
2260 got_privsep_recv_gitconfig_remotes(struct got_remote_repo **remotes,
2261 int *nremotes, struct imsgbuf *ibuf)
2263 const struct got_error *err = NULL;
2266 struct got_imsg_remotes iremotes;
2267 struct got_imsg_remote iremote;
2271 iremotes.nremotes = 0;
2273 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremotes));
2276 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2278 switch (imsg.hdr.type) {
2279 case GOT_IMSG_GITCONFIG_REMOTES:
2280 if (datalen != sizeof(iremotes)) {
2282 return got_error(GOT_ERR_PRIVSEP_LEN);
2284 memcpy(&iremotes, imsg.data, sizeof(iremotes));
2285 if (iremotes.nremotes == 0) {
2292 return got_error(GOT_ERR_PRIVSEP_MSG);
2297 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(**remotes));
2298 if (*remotes == NULL)
2299 return got_error_from_errno("recallocarray");
2301 while (*nremotes < iremotes.nremotes) {
2302 struct got_remote_repo *remote;
2304 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremote));
2307 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2309 switch (imsg.hdr.type) {
2310 case GOT_IMSG_GITCONFIG_REMOTE:
2311 remote = &(*remotes)[*nremotes];
2312 memset(remote, 0, sizeof(*remote));
2313 if (datalen < sizeof(iremote)) {
2314 err = got_error(GOT_ERR_PRIVSEP_LEN);
2317 memcpy(&iremote, imsg.data, sizeof(iremote));
2318 if (iremote.name_len == 0 ||
2319 iremote.fetch_url_len == 0 ||
2320 iremote.send_url_len == 0 ||
2321 (sizeof(iremote) + iremote.name_len +
2322 iremote.fetch_url_len + iremote.send_url_len) > datalen) {
2323 err = got_error(GOT_ERR_PRIVSEP_LEN);
2326 remote->name = strndup(imsg.data + sizeof(iremote),
2328 if (remote->name == NULL) {
2329 err = got_error_from_errno("strndup");
2332 remote->fetch_url = strndup(imsg.data + sizeof(iremote) +
2333 iremote.name_len, iremote.fetch_url_len);
2334 if (remote->fetch_url == NULL) {
2335 err = got_error_from_errno("strndup");
2336 free_remote_data(remote);
2339 remote->send_url = strndup(imsg.data + sizeof(iremote) +
2340 iremote.name_len + iremote.fetch_url_len,
2341 iremote.send_url_len);
2342 if (remote->send_url == NULL) {
2343 err = got_error_from_errno("strndup");
2344 free_remote_data(remote);
2347 remote->mirror_references = iremote.mirror_references;
2348 remote->fetch_all_branches = iremote.fetch_all_branches;
2349 remote->nfetch_branches = 0;
2350 remote->fetch_branches = NULL;
2351 remote->nsend_branches = 0;
2352 remote->send_branches = NULL;
2353 remote->nfetch_refs = 0;
2354 remote->fetch_refs = NULL;
2358 err = got_error(GOT_ERR_PRIVSEP_MSG);
2369 for (i = 0; i < *nremotes; i++)
2370 free_remote_data(&(*remotes)[i]);
2378 const struct got_error *
2379 got_privsep_send_gotconfig_parse_req(struct imsgbuf *ibuf, int fd)
2381 const struct got_error *err = NULL;
2383 if (imsg_compose(ibuf, GOT_IMSG_GOTCONFIG_PARSE_REQUEST, 0, 0, fd,
2385 err = got_error_from_errno("imsg_compose "
2386 "GOTCONFIG_PARSE_REQUEST");
2391 return flush_imsg(ibuf);
2394 const struct got_error *
2395 got_privsep_send_gotconfig_author_req(struct imsgbuf *ibuf)
2397 if (imsg_compose(ibuf,
2398 GOT_IMSG_GOTCONFIG_AUTHOR_REQUEST, 0, 0, -1, NULL, 0) == -1)
2399 return got_error_from_errno("imsg_compose "
2400 "GOTCONFIG_AUTHOR_REQUEST");
2402 return flush_imsg(ibuf);
2405 const struct got_error *
2406 got_privsep_send_gotconfig_allowed_signers_req(struct imsgbuf *ibuf)
2408 if (imsg_compose(ibuf,
2409 GOT_IMSG_GOTCONFIG_ALLOWEDSIGNERS_REQUEST, 0, 0, -1, NULL, 0) == -1)
2410 return got_error_from_errno("imsg_compose "
2411 "GOTCONFIG_ALLOWEDSIGNERS_REQUEST");
2413 return flush_imsg(ibuf);
2416 const struct got_error *
2417 got_privsep_send_gotconfig_revoked_signers_req(struct imsgbuf *ibuf)
2419 if (imsg_compose(ibuf,
2420 GOT_IMSG_GOTCONFIG_REVOKEDSIGNERS_REQUEST, 0, 0, -1, NULL, 0) == -1)
2421 return got_error_from_errno("imsg_compose "
2422 "GOTCONFIG_REVOKEDSIGNERS_REQUEST");
2424 return flush_imsg(ibuf);
2427 const struct got_error *
2428 got_privsep_send_gotconfig_signer_id_req(struct imsgbuf *ibuf)
2430 if (imsg_compose(ibuf,
2431 GOT_IMSG_GOTCONFIG_SIGNERID_REQUEST, 0, 0, -1, NULL, 0) == -1)
2432 return got_error_from_errno("imsg_compose "
2433 "GOTCONFIG_SIGNERID_REQUEST");
2435 return flush_imsg(ibuf);
2438 const struct got_error *
2439 got_privsep_send_gotconfig_remotes_req(struct imsgbuf *ibuf)
2441 if (imsg_compose(ibuf,
2442 GOT_IMSG_GOTCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
2443 return got_error_from_errno("imsg_compose "
2444 "GOTCONFIG_REMOTE_REQUEST");
2446 return flush_imsg(ibuf);
2449 const struct got_error *
2450 got_privsep_recv_gotconfig_str(char **str, struct imsgbuf *ibuf)
2452 const struct got_error *err = NULL;
2458 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2461 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2463 switch (imsg.hdr.type) {
2464 case GOT_IMSG_GOTCONFIG_STR_VAL:
2467 *str = strndup(imsg.data, datalen);
2469 err = got_error_from_errno("strndup");
2474 err = got_error(GOT_ERR_PRIVSEP_MSG);
2482 const struct got_error *
2483 got_privsep_recv_gotconfig_remotes(struct got_remote_repo **remotes,
2484 int *nremotes, struct imsgbuf *ibuf)
2486 const struct got_error *err = NULL;
2489 struct got_imsg_remotes iremotes;
2490 struct got_imsg_remote iremote;
2491 const size_t min_datalen =
2492 MIN(sizeof(struct got_imsg_error), sizeof(iremotes));
2496 iremotes.nremotes = 0;
2498 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2501 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2503 switch (imsg.hdr.type) {
2504 case GOT_IMSG_GOTCONFIG_REMOTES:
2505 if (datalen != sizeof(iremotes)) {
2506 err = got_error(GOT_ERR_PRIVSEP_LEN);
2509 memcpy(&iremotes, imsg.data, sizeof(iremotes));
2510 if (iremotes.nremotes < 0) {
2511 err = got_error(GOT_ERR_PRIVSEP_LEN);
2514 if (iremotes.nremotes == 0) {
2521 return got_error(GOT_ERR_PRIVSEP_MSG);
2526 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(**remotes));
2527 if (*remotes == NULL)
2528 return got_error_from_errno("recallocarray");
2530 while (*nremotes < iremotes.nremotes) {
2531 struct got_remote_repo *remote;
2532 const size_t min_datalen =
2533 MIN(sizeof(struct got_imsg_error), sizeof(iremote));
2536 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2539 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2541 switch (imsg.hdr.type) {
2542 case GOT_IMSG_GOTCONFIG_REMOTE:
2543 remote = &(*remotes)[*nremotes];
2544 memset(remote, 0, sizeof(*remote));
2545 if (datalen < sizeof(iremote)) {
2546 err = got_error(GOT_ERR_PRIVSEP_LEN);
2549 memcpy(&iremote, imsg.data, sizeof(iremote));
2550 if (iremote.name_len == 0 ||
2551 (iremote.fetch_url_len == 0 &&
2552 iremote.send_url_len == 0) ||
2553 (sizeof(iremote) + iremote.name_len +
2554 iremote.fetch_url_len + iremote.send_url_len) >
2556 err = got_error(GOT_ERR_PRIVSEP_LEN);
2559 remote->name = strndup(imsg.data + sizeof(iremote),
2561 if (remote->name == NULL) {
2562 err = got_error_from_errno("strndup");
2565 remote->fetch_url = strndup(imsg.data +
2566 sizeof(iremote) + iremote.name_len,
2567 iremote.fetch_url_len);
2568 if (remote->fetch_url == NULL) {
2569 err = got_error_from_errno("strndup");
2570 free_remote_data(remote);
2573 remote->send_url = strndup(imsg.data +
2574 sizeof(iremote) + iremote.name_len +
2575 iremote.fetch_url_len, iremote.send_url_len);
2576 if (remote->send_url == NULL) {
2577 err = got_error_from_errno("strndup");
2578 free_remote_data(remote);
2581 remote->mirror_references = iremote.mirror_references;
2582 remote->fetch_all_branches = iremote.fetch_all_branches;
2583 if (iremote.nfetch_branches > 0) {
2584 remote->fetch_branches = recallocarray(NULL, 0,
2585 iremote.nfetch_branches, sizeof(char *));
2586 if (remote->fetch_branches == NULL) {
2587 err = got_error_from_errno("calloc");
2588 free_remote_data(remote);
2592 remote->nfetch_branches = 0;
2593 for (i = 0; i < iremote.nfetch_branches; i++) {
2595 err = got_privsep_recv_gotconfig_str(&branch,
2598 free_remote_data(remote);
2601 remote->fetch_branches[i] = branch;
2602 remote->nfetch_branches++;
2604 if (iremote.nsend_branches > 0) {
2605 remote->send_branches = recallocarray(NULL, 0,
2606 iremote.nsend_branches, sizeof(char *));
2607 if (remote->send_branches == NULL) {
2608 err = got_error_from_errno("calloc");
2609 free_remote_data(remote);
2613 remote->nsend_branches = 0;
2614 for (i = 0; i < iremote.nsend_branches; i++) {
2616 err = got_privsep_recv_gotconfig_str(&branch,
2619 free_remote_data(remote);
2622 remote->send_branches[i] = branch;
2623 remote->nsend_branches++;
2625 if (iremote.nfetch_refs > 0) {
2626 remote->fetch_refs = recallocarray(NULL, 0,
2627 iremote.nfetch_refs, sizeof(char *));
2628 if (remote->fetch_refs == NULL) {
2629 err = got_error_from_errno("calloc");
2630 free_remote_data(remote);
2634 remote->nfetch_refs = 0;
2635 for (i = 0; i < iremote.nfetch_refs; i++) {
2637 err = got_privsep_recv_gotconfig_str(&ref,
2640 free_remote_data(remote);
2643 remote->fetch_refs[i] = ref;
2644 remote->nfetch_refs++;
2649 err = got_error(GOT_ERR_PRIVSEP_MSG);
2660 for (i = 0; i < *nremotes; i++)
2661 free_remote_data(&(*remotes)[i]);
2669 const struct got_error *
2670 got_privsep_send_commit_traversal_request(struct imsgbuf *ibuf,
2671 struct got_object_id *id, int idx, const char *path)
2674 size_t path_len = strlen(path);
2676 wbuf = imsg_create(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_REQUEST, 0, 0,
2677 sizeof(struct got_imsg_commit_traversal_request) + path_len);
2679 return got_error_from_errno(
2680 "imsg_create COMMIT_TRAVERSAL_REQUEST");
2682 * Keep in sync with struct got_imsg_commit_traversal_request
2683 * and struct got_imsg_packed_object.
2685 if (imsg_add(wbuf, id, sizeof(*id)) == -1)
2686 return got_error_from_errno("imsg_add "
2687 "COMMIT_TRAVERSAL_REQUEST");
2688 if (imsg_add(wbuf, &idx, sizeof(idx)) == -1)
2689 return got_error_from_errno("imsg_add "
2690 "COMMIT_TRAVERSAL_REQUEST");
2691 if (imsg_add(wbuf, &path_len, sizeof(path_len)) == -1)
2692 return got_error_from_errno("imsg_add "
2693 "COMMIT_TRAVERSAL_REQUEST");
2694 if (imsg_add(wbuf, path, path_len) == -1)
2695 return got_error_from_errno("imsg_add "
2696 "COMMIT_TRAVERSAL_REQUEST");
2698 imsg_close(ibuf, wbuf);
2699 return flush_imsg(ibuf);
2702 const struct got_error *
2703 got_privsep_recv_traversed_commits(struct got_commit_object **changed_commit,
2704 struct got_object_id **changed_commit_id,
2705 struct got_object_id_queue *commit_ids, struct imsgbuf *ibuf)
2707 const struct got_error *err = NULL;
2709 struct got_imsg_traversed_commits *icommits;
2710 struct got_object_id *ids;
2714 *changed_commit = NULL;
2715 *changed_commit_id = NULL;
2718 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2722 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2723 switch (imsg.hdr.type) {
2724 case GOT_IMSG_TRAVERSED_COMMITS:
2725 icommits = imsg.data;
2726 if (datalen != sizeof(*icommits) +
2727 icommits->ncommits * sizeof(*ids)) {
2728 err = got_error(GOT_ERR_PRIVSEP_LEN);
2731 ids = imsg.data + sizeof(*icommits);
2732 for (i = 0; i < icommits->ncommits; i++) {
2733 struct got_object_qid *qid;
2735 err = got_object_qid_alloc_partial(&qid);
2738 memcpy(&qid->id, &ids[i], sizeof(ids[i]));
2739 STAILQ_INSERT_TAIL(commit_ids, qid, entry);
2741 /* The last commit may contain a change. */
2742 if (i == icommits->ncommits - 1) {
2743 *changed_commit_id =
2744 got_object_id_dup(&qid->id);
2745 if (*changed_commit_id == NULL) {
2746 err = got_error_from_errno(
2747 "got_object_id_dup");
2753 case GOT_IMSG_COMMIT:
2754 if (*changed_commit_id == NULL) {
2755 err = got_error(GOT_ERR_PRIVSEP_MSG);
2758 err = get_commit_from_imsg(changed_commit, &imsg,
2761 case GOT_IMSG_COMMIT_TRAVERSAL_DONE:
2765 err = got_error(GOT_ERR_PRIVSEP_MSG);
2775 got_object_id_queue_free(commit_ids);
2779 const struct got_error *
2780 got_privsep_send_enumerated_tree(size_t *totlen, struct imsgbuf *ibuf,
2781 struct got_object_id *tree_id, const char *path,
2782 struct got_parsed_tree_entry *entries, int nentries)
2784 const struct got_error *err = NULL;
2786 size_t path_len = strlen(path);
2789 msglen = sizeof(struct got_imsg_enumerated_tree) + path_len;
2790 wbuf = imsg_create(ibuf, GOT_IMSG_ENUMERATED_TREE, 0, 0, msglen);
2792 return got_error_from_errno("imsg_create ENUMERATED_TREE");
2794 if (imsg_add(wbuf, tree_id->sha1, SHA1_DIGEST_LENGTH) == -1)
2795 return got_error_from_errno("imsg_add ENUMERATED_TREE");
2796 if (imsg_add(wbuf, &nentries, sizeof(nentries)) == -1)
2797 return got_error_from_errno("imsg_add ENUMERATED_TREE");
2798 if (imsg_add(wbuf, path, path_len) == -1)
2799 return got_error_from_errno("imsg_add ENUMERATED_TREE");
2801 imsg_close(ibuf, wbuf);
2804 err = send_tree_entries(ibuf, entries, nentries);
2809 return flush_imsg(ibuf);
2812 const struct got_error *
2813 got_privsep_send_object_enumeration_request(struct imsgbuf *ibuf)
2815 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_ENUMERATION_REQUEST,
2816 0, 0, -1, NULL, 0) == -1)
2817 return got_error_from_errno("imsg_compose "
2818 "OBJECT_ENUMERATION_REQUEST");
2820 return flush_imsg(ibuf);
2823 const struct got_error *
2824 got_privsep_send_object_enumeration_done(struct imsgbuf *ibuf)
2826 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_ENUMERATION_DONE,
2827 0, 0, -1, NULL, 0) == -1)
2828 return got_error_from_errno("imsg_compose "
2829 "OBJECT_ENUMERATION_DONE");
2831 return flush_imsg(ibuf);
2834 const struct got_error *
2835 got_privsep_send_object_enumeration_incomplete(struct imsgbuf *ibuf)
2837 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_ENUMERATION_INCOMPLETE,
2838 0, 0, -1, NULL, 0) == -1)
2839 return got_error_from_errno("imsg_compose "
2840 "OBJECT_ENUMERATION_INCOMPLETE");
2842 return flush_imsg(ibuf);
2845 const struct got_error *
2846 got_privsep_send_enumerated_commit(struct imsgbuf *ibuf,
2847 struct got_object_id *id, time_t mtime)
2851 wbuf = imsg_create(ibuf, GOT_IMSG_ENUMERATED_COMMIT, 0, 0,
2852 sizeof(struct got_imsg_enumerated_commit) + SHA1_DIGEST_LENGTH);
2854 return got_error_from_errno("imsg_create ENUMERATED_COMMIT");
2856 /* Keep in sync with struct got_imsg_enumerated_commit! */
2857 if (imsg_add(wbuf, id, SHA1_DIGEST_LENGTH) == -1)
2858 return got_error_from_errno("imsg_add ENUMERATED_COMMIT");
2859 if (imsg_add(wbuf, &mtime, sizeof(mtime)) == -1)
2860 return got_error_from_errno("imsg_add ENUMERATED_COMMIT");
2862 imsg_close(ibuf, wbuf);
2863 /* Don't flush yet, tree entries or ENUMERATION_DONE will follow. */
2867 const struct got_error *
2868 got_privsep_recv_enumerated_objects(int *found_all_objects,
2869 struct imsgbuf *ibuf,
2870 got_object_enumerate_commit_cb cb_commit,
2871 got_object_enumerate_tree_cb cb_tree, void *cb_arg,
2872 struct got_repository *repo)
2874 const struct got_error *err = NULL;
2876 struct got_imsg_enumerated_commit *icommit = NULL;
2877 struct got_object_id commit_id;
2878 int have_commit = 0;
2880 struct got_tree_object tree;
2881 struct got_imsg_enumerated_tree *itree;
2882 struct got_object_id tree_id;
2883 char *path = NULL, *canon_path = NULL;
2884 size_t datalen, path_len;
2888 *found_all_objects = 0;
2889 memset(&tree, 0, sizeof(tree));
2892 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2896 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2897 switch (imsg.hdr.type) {
2898 case GOT_IMSG_ENUMERATED_COMMIT:
2899 if (have_commit && nentries != -1) {
2900 err = got_error(GOT_ERR_PRIVSEP_MSG);
2903 if (datalen != sizeof(*icommit)) {
2904 err = got_error(GOT_ERR_PRIVSEP_LEN);
2907 icommit = (struct got_imsg_enumerated_commit *)imsg.data;
2908 memcpy(commit_id.sha1, icommit->id, SHA1_DIGEST_LENGTH);
2909 mtime = icommit->mtime;
2912 case GOT_IMSG_ENUMERATED_TREE:
2913 /* Should be preceeded by GOT_IMSG_ENUMERATED_COMMIT. */
2915 err = got_error(GOT_ERR_PRIVSEP_MSG);
2918 if (datalen < sizeof(*itree)) {
2919 err = got_error(GOT_ERR_PRIVSEP_LEN);
2923 path_len = datalen - sizeof(*itree);
2924 if (path_len == 0) {
2925 err = got_error(GOT_ERR_PRIVSEP_LEN);
2928 memcpy(tree_id.sha1, itree->id, sizeof(tree_id.sha1));
2930 path = strndup(imsg.data + sizeof(*itree), path_len);
2932 err = got_error_from_errno("strndup");
2936 canon_path = malloc(path_len + 1);
2937 if (canon_path == NULL) {
2938 err = got_error_from_errno("malloc");
2941 if (!got_path_is_absolute(path)) {
2942 err = got_error(GOT_ERR_BAD_PATH);
2945 if (got_path_is_root_dir(path)) {
2946 /* XXX check what got_canonpath() does wrong */
2947 canon_path[0] = '/';
2948 canon_path[1] = '\0';
2950 err = got_canonpath(path, canon_path,
2955 if (strcmp(path, canon_path) != 0) {
2956 err = got_error(GOT_ERR_BAD_PATH);
2959 if (nentries != -1) {
2960 err = got_error(GOT_ERR_PRIVSEP_MSG);
2963 if (itree->nentries < -1) {
2964 err = got_error(GOT_ERR_PRIVSEP_MSG);
2967 if (itree->nentries == -1) {
2968 /* Tree was not found in pack file. */
2969 err = cb_tree(cb_arg, NULL, mtime, &tree_id,
2973 if (itree->nentries > INT_MAX) {
2974 err = got_error(GOT_ERR_PRIVSEP_LEN);
2977 tree.entries = calloc(itree->nentries,
2978 sizeof(struct got_tree_entry));
2979 if (tree.entries == NULL) {
2980 err = got_error_from_errno("calloc");
2983 if (itree->nentries == 0) {
2984 err = cb_tree(cb_arg, &tree, mtime, &tree_id,
2989 /* Prepare for next tree. */
2991 memset(&tree, 0, sizeof(tree));
2994 tree.nentries = itree->nentries;
2998 case GOT_IMSG_TREE_ENTRIES:
2999 /* Should be preceeded by GOT_IMSG_ENUMERATED_TREE. */
3000 if (nentries <= -1) {
3001 err = got_error(GOT_ERR_PRIVSEP_MSG);
3004 err = recv_tree_entries(imsg.data, datalen,
3008 if (tree.nentries == nentries) {
3009 err = cb_tree(cb_arg, &tree, mtime, &tree_id,
3014 /* Prepare for next tree. */
3016 memset(&tree, 0, sizeof(tree));
3020 case GOT_IMSG_TREE_ENUMERATION_DONE:
3021 /* All trees have been found and traversed. */
3022 if (!have_commit || path == NULL || nentries != -1) {
3023 err = got_error(GOT_ERR_PRIVSEP_MSG);
3026 err = cb_commit(cb_arg, mtime, &commit_id, repo);
3031 case GOT_IMSG_OBJECT_ENUMERATION_DONE:
3032 *found_all_objects = 1;
3035 case GOT_IMSG_OBJECT_ENUMERATION_INCOMPLETE:
3039 err = got_error(GOT_ERR_PRIVSEP_MSG);
3054 const struct got_error *
3055 got_privsep_send_raw_delta_req(struct imsgbuf *ibuf, int idx,
3056 struct got_object_id *id)
3058 struct got_imsg_raw_delta_request dreq;
3060 memset(&dreq, 0, sizeof(dreq));
3062 memcpy(&dreq.id, id, sizeof(dreq.id));
3064 if (imsg_compose(ibuf, GOT_IMSG_RAW_DELTA_REQUEST, 0, 0, -1,
3065 &dreq, sizeof(dreq)) == -1)
3066 return got_error_from_errno("imsg_compose RAW_DELTA_REQUEST");
3068 return flush_imsg(ibuf);
3071 const struct got_error *
3072 got_privsep_send_raw_delta_outfd(struct imsgbuf *ibuf, int fd)
3074 return send_fd(ibuf, GOT_IMSG_RAW_DELTA_OUTFD, fd);
3077 const struct got_error *
3078 got_privsep_send_raw_delta(struct imsgbuf *ibuf, uint64_t base_size,
3079 uint64_t result_size, off_t delta_size, off_t delta_compressed_size,
3080 off_t delta_offset, off_t delta_out_offset, struct got_object_id *base_id)
3082 struct got_imsg_raw_delta idelta;
3085 memset(&idelta, 0, sizeof(idelta));
3086 idelta.base_size = base_size;
3087 idelta.result_size = result_size;
3088 idelta.delta_size = delta_size;
3089 idelta.delta_compressed_size = delta_compressed_size;
3090 idelta.delta_offset = delta_offset;
3091 idelta.delta_out_offset = delta_out_offset;
3092 memcpy(&idelta.base_id, &base_id, sizeof(idelta.base_id));
3094 ret = imsg_compose(ibuf, GOT_IMSG_RAW_DELTA, 0, 0, -1,
3095 &idelta, sizeof(idelta));
3097 return got_error_from_errno("imsg_compose RAW_DELTA");
3099 return flush_imsg(ibuf);
3102 const struct got_error *
3103 got_privsep_recv_raw_delta(uint64_t *base_size, uint64_t *result_size,
3104 off_t *delta_size, off_t *delta_compressed_size, off_t *delta_offset,
3105 off_t *delta_out_offset, struct got_object_id **base_id,
3106 struct imsgbuf *ibuf)
3108 const struct got_error *err = NULL;
3110 struct got_imsg_raw_delta *delta;
3116 *delta_compressed_size = 0;
3118 *delta_out_offset = 0;
3121 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
3125 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
3127 switch (imsg.hdr.type) {
3128 case GOT_IMSG_RAW_DELTA:
3129 if (datalen != sizeof(*delta)) {
3130 err = got_error(GOT_ERR_PRIVSEP_LEN);
3134 *base_size = delta->base_size;
3135 *result_size = delta->result_size;
3136 *delta_size = delta->delta_size;
3137 *delta_compressed_size = delta->delta_compressed_size;
3138 *delta_offset = delta->delta_offset;
3139 *delta_out_offset = delta->delta_out_offset;
3140 *base_id = calloc(1, sizeof(**base_id));
3141 if (*base_id == NULL) {
3142 err = got_error_from_errno("malloc");
3145 memcpy(*base_id, &delta->base_id, sizeof(**base_id));
3148 err = got_error(GOT_ERR_PRIVSEP_MSG);
3161 static const struct got_error *
3162 send_idlist(struct imsgbuf *ibuf, struct got_object_id **ids, size_t nids)
3164 const struct got_error *err = NULL;
3165 struct got_imsg_object_idlist idlist;
3169 memset(&idlist, 0, sizeof(idlist));
3171 if (nids > GOT_IMSG_OBJ_ID_LIST_MAX_NIDS)
3172 return got_error(GOT_ERR_NO_SPACE);
3174 wbuf = imsg_create(ibuf, GOT_IMSG_OBJ_ID_LIST, 0, 0,
3175 sizeof(idlist) + nids * sizeof(**ids));
3177 err = got_error_from_errno("imsg_create OBJ_ID_LIST");
3182 if (imsg_add(wbuf, &idlist, sizeof(idlist)) == -1)
3183 return got_error_from_errno("imsg_add OBJ_ID_LIST");
3185 for (i = 0; i < nids; i++) {
3186 struct got_object_id *id = ids[i];
3187 if (imsg_add(wbuf, id, sizeof(*id)) == -1)
3188 return got_error_from_errno("imsg_add OBJ_ID_LIST");
3191 imsg_close(ibuf, wbuf);
3193 return flush_imsg(ibuf);
3196 const struct got_error *
3197 got_privsep_send_object_idlist(struct imsgbuf *ibuf,
3198 struct got_object_id **ids, size_t nids)
3200 const struct got_error *err = NULL;
3201 struct got_object_id *idlist[GOT_IMSG_OBJ_ID_LIST_MAX_NIDS];
3204 for (i = 0; i < nids; i++) {
3205 idlist[i % nitems(idlist)] = ids[i];
3207 if (queued >= nitems(idlist)) {
3208 err = send_idlist(ibuf, idlist, queued);
3216 err = send_idlist(ibuf, idlist, queued);
3224 const struct got_error *
3225 got_privsep_send_object_idlist_done(struct imsgbuf *ibuf)
3227 if (imsg_compose(ibuf, GOT_IMSG_OBJ_ID_LIST_DONE, 0, 0, -1, NULL, 0)
3229 return got_error_from_errno("imsg_compose OBJ_ID_LIST_DONE");
3231 return flush_imsg(ibuf);
3234 const struct got_error *
3235 got_privsep_recv_object_idlist(int *done, struct got_object_id **ids,
3236 size_t *nids, struct imsgbuf *ibuf)
3238 const struct got_error *err = NULL;
3240 struct got_imsg_object_idlist *idlist;
3247 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
3251 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
3252 switch (imsg.hdr.type) {
3253 case GOT_IMSG_OBJ_ID_LIST:
3254 if (datalen < sizeof(*idlist)) {
3255 err = got_error(GOT_ERR_PRIVSEP_LEN);
3259 if (idlist->nids > GOT_IMSG_OBJ_ID_LIST_MAX_NIDS ||
3260 idlist->nids * sizeof(**ids) > datalen - sizeof(*idlist)) {
3261 err = got_error(GOT_ERR_PRIVSEP_LEN);
3264 *nids = idlist->nids;
3265 *ids = calloc(*nids, sizeof(**ids));
3267 err = got_error_from_errno("calloc");
3270 memcpy(*ids, (uint8_t *)imsg.data + sizeof(*idlist),
3271 *nids * sizeof(**ids));
3273 case GOT_IMSG_OBJ_ID_LIST_DONE:
3277 err = got_error(GOT_ERR_PRIVSEP_MSG);
3286 const struct got_error *
3287 got_privsep_send_delta_reuse_req(struct imsgbuf *ibuf)
3289 if (imsg_compose(ibuf, GOT_IMSG_DELTA_REUSE_REQUEST, 0, 0, -1, NULL, 0)
3291 return got_error_from_errno("imsg_compose DELTA_REUSE_REQUEST");
3293 return flush_imsg(ibuf);
3296 const struct got_error *
3297 got_privsep_send_reused_deltas(struct imsgbuf *ibuf,
3298 struct got_imsg_reused_delta *deltas, size_t ndeltas)
3300 const struct got_error *err = NULL;
3302 struct got_imsg_reused_deltas ideltas;
3305 memset(&ideltas, 0, sizeof(ideltas));
3307 if (ndeltas > GOT_IMSG_REUSED_DELTAS_MAX_NDELTAS)
3308 return got_error(GOT_ERR_NO_SPACE);
3310 wbuf = imsg_create(ibuf, GOT_IMSG_REUSED_DELTAS, 0, 0,
3311 sizeof(ideltas) + ndeltas * sizeof(*deltas));
3313 err = got_error_from_errno("imsg_create REUSED_DELTAS");
3317 ideltas.ndeltas = ndeltas;
3318 if (imsg_add(wbuf, &ideltas, sizeof(ideltas)) == -1)
3319 return got_error_from_errno("imsg_add REUSED_DELTAS");
3321 for (i = 0; i < ndeltas; i++) {
3322 struct got_imsg_reused_delta *delta = &deltas[i];
3323 if (imsg_add(wbuf, delta, sizeof(*delta)) == -1)
3324 return got_error_from_errno("imsg_add REUSED_DELTAS");
3327 imsg_close(ibuf, wbuf);
3329 return flush_imsg(ibuf);
3332 const struct got_error *
3333 got_privsep_send_reused_deltas_done(struct imsgbuf *ibuf)
3335 if (imsg_compose(ibuf, GOT_IMSG_DELTA_REUSE_DONE, 0, 0, -1, NULL, 0)
3337 return got_error_from_errno("imsg_compose DELTA_REUSE_DONE");
3339 return flush_imsg(ibuf);
3342 const struct got_error *
3343 got_privsep_recv_reused_deltas(int *done, struct got_imsg_reused_delta *deltas,
3344 size_t *ndeltas, struct imsgbuf *ibuf)
3346 const struct got_error *err = NULL;
3348 struct got_imsg_reused_deltas *ideltas;
3354 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
3358 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
3359 switch (imsg.hdr.type) {
3360 case GOT_IMSG_REUSED_DELTAS:
3361 if (datalen < sizeof(*ideltas)) {
3362 err = got_error(GOT_ERR_PRIVSEP_LEN);
3365 ideltas = imsg.data;
3366 if (ideltas->ndeltas > GOT_IMSG_OBJ_ID_LIST_MAX_NIDS ||
3367 ideltas->ndeltas * sizeof(*deltas) >
3368 datalen - sizeof(*ideltas)) {
3369 err = got_error(GOT_ERR_PRIVSEP_LEN);
3372 *ndeltas = ideltas->ndeltas;
3373 memcpy(deltas, (uint8_t *)imsg.data + sizeof(*ideltas),
3374 *ndeltas * sizeof(*deltas));
3376 case GOT_IMSG_DELTA_REUSE_DONE:
3380 err = got_error(GOT_ERR_PRIVSEP_MSG);
3389 const struct got_error *
3390 got_privsep_init_commit_painting(struct imsgbuf *ibuf)
3392 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_PAINTING_INIT,
3395 return got_error_from_errno("imsg_compose "
3396 "COMMIT_PAINTING_INIT");
3398 return flush_imsg(ibuf);
3401 const struct got_error *
3402 got_privsep_send_painting_request(struct imsgbuf *ibuf, int idx,
3403 struct got_object_id *id, intptr_t color)
3405 struct got_imsg_commit_painting_request ireq;
3407 memset(&ireq, 0, sizeof(ireq));
3408 memcpy(&ireq.id, id, sizeof(ireq.id));
3412 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_PAINTING_REQUEST, 0, 0, -1,
3413 &ireq, sizeof(ireq)) == -1)
3414 return got_error_from_errno("imsg_compose "
3415 "COMMIT_PAINTING_REQUEST");
3417 return flush_imsg(ibuf);
3420 static const struct got_error *
3421 send_painted_commits(struct got_object_id_queue *ids, int *nids,
3422 size_t remain, int present_in_pack, struct imsgbuf *ibuf)
3424 const struct got_error *err = NULL;
3425 struct ibuf *wbuf = NULL;
3426 struct got_object_qid *qid;
3431 msglen = MIN(remain, MAX_IMSGSIZE - IMSG_HEADER_SIZE);
3432 ncommits = (msglen - sizeof(struct got_imsg_painted_commits)) /
3433 sizeof(struct got_imsg_painted_commit);
3435 wbuf = imsg_create(ibuf, GOT_IMSG_PAINTED_COMMITS, 0, 0, msglen);
3437 err = got_error_from_errno("imsg_create PAINTED_COMMITS");
3441 /* Keep in sync with struct got_imsg_painted_commits! */
3442 if (imsg_add(wbuf, &ncommits, sizeof(ncommits)) == -1)
3443 return got_error_from_errno("imsg_add PAINTED_COMMITS");
3444 if (imsg_add(wbuf, &present_in_pack, sizeof(present_in_pack)) == -1)
3445 return got_error_from_errno("imsg_add PAINTED_COMMITS");
3447 while (ncommits > 0) {
3448 qid = STAILQ_FIRST(ids);
3449 STAILQ_REMOVE_HEAD(ids, entry);
3452 color = (intptr_t)qid->data;
3454 /* Keep in sync with struct got_imsg_painted_commit! */
3455 if (imsg_add(wbuf, qid->id.sha1, SHA1_DIGEST_LENGTH) == -1)
3456 return got_error_from_errno("imsg_add PAINTED_COMMITS");
3457 if (imsg_add(wbuf, &color, sizeof(color)) == -1)
3458 return got_error_from_errno("imsg_add PAINTED_COMMITS");
3460 got_object_qid_free(qid);
3463 imsg_close(ibuf, wbuf);
3464 return flush_imsg(ibuf);
3467 const struct got_error *
3468 got_privsep_send_painted_commits(struct imsgbuf *ibuf,
3469 struct got_object_id_queue *ids, int *nids,
3470 int present_in_pack, int flush)
3472 const struct got_error *err;
3479 remain = (sizeof(struct got_imsg_painted_commits)) +
3480 *nids * sizeof(struct got_imsg_painted_commit);
3481 if (flush || remain >= MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
3482 err = send_painted_commits(ids, nids, remain,
3483 present_in_pack, ibuf);
3487 } while (flush && *nids > 0);
3492 const struct got_error *
3493 got_privsep_send_painting_commits_done(struct imsgbuf *ibuf)
3495 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_PAINTING_DONE,
3498 return got_error_from_errno("imsg_compose "
3499 "COMMIT_PAINTING_DONE");
3501 return flush_imsg(ibuf);
3504 const struct got_error *
3505 got_privsep_recv_painted_commits(struct got_object_id_queue *new_ids,
3506 got_privsep_recv_painted_commit_cb cb, void *cb_arg, struct imsgbuf *ibuf)
3508 const struct got_error *err = NULL;
3510 struct got_imsg_painted_commits icommits;
3511 struct got_imsg_painted_commit icommit;
3516 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
3520 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
3521 if (imsg.hdr.type == GOT_IMSG_COMMIT_PAINTING_DONE) {
3525 if (imsg.hdr.type != GOT_IMSG_PAINTED_COMMITS){
3527 return got_error(GOT_ERR_PRIVSEP_MSG);
3530 if (datalen < sizeof(icommits)){
3532 return got_error(GOT_ERR_PRIVSEP_LEN);
3534 memcpy(&icommits, imsg.data, sizeof(icommits));
3535 if (icommits.ncommits * sizeof(icommit) < icommits.ncommits ||
3536 datalen < sizeof(icommits) +
3537 icommits.ncommits * sizeof(icommit)){
3539 return got_error(GOT_ERR_PRIVSEP_LEN);
3542 for (i = 0; i < icommits.ncommits; i++) {
3544 (uint8_t *)imsg.data + sizeof(icommits) + i * sizeof(icommit),
3547 if (icommits.present_in_pack) {
3548 struct got_object_id id;
3549 memcpy(id.sha1, icommit.id, SHA1_DIGEST_LENGTH);
3550 err = cb(cb_arg, &id, icommit.color);
3554 struct got_object_qid *qid;
3555 err = got_object_qid_alloc_partial(&qid);
3558 memcpy(qid->id.sha1, icommit.id,
3559 SHA1_DIGEST_LENGTH);
3560 qid->data = (void *)icommit.color;
3561 STAILQ_INSERT_TAIL(new_ids, qid, entry);
3571 const struct got_error *
3572 got_privsep_unveil_exec_helpers(void)
3574 const char *helpers[] = {
3575 GOT_PATH_PROG_READ_PACK,
3576 GOT_PATH_PROG_READ_OBJECT,
3577 GOT_PATH_PROG_READ_COMMIT,
3578 GOT_PATH_PROG_READ_TREE,
3579 GOT_PATH_PROG_READ_BLOB,
3580 GOT_PATH_PROG_READ_TAG,
3581 GOT_PATH_PROG_READ_GITCONFIG,
3582 GOT_PATH_PROG_READ_GOTCONFIG,
3583 GOT_PATH_PROG_READ_PATCH,
3584 GOT_PATH_PROG_FETCH_PACK,
3585 GOT_PATH_PROG_INDEX_PACK,
3586 GOT_PATH_PROG_SEND_PACK,
3590 for (i = 0; i < nitems(helpers); i++) {
3591 if (unveil(helpers[i], "x") == 0)
3593 return got_error_from_errno2("unveil", helpers[i]);
3600 got_privsep_exec_child(int imsg_fds[2], const char *path, const char *repo_path)
3602 if (close(imsg_fds[0]) == -1) {
3603 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
3607 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
3608 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
3612 closefrom(GOT_IMSG_FD_CHILD + 1);
3614 if (execl(path, path, repo_path, (char *)NULL) == -1) {
3615 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,