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_privsep.h"
49 #include "got_lib_pack.h"
50 #include "got_lib_poll.h"
52 #include "got_privsep.h"
55 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
59 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
62 static const struct got_error *
63 read_imsg(struct imsgbuf *ibuf)
65 const struct got_error *err;
68 err = got_poll_fd(ibuf->fd, POLLIN, INFTIM);
70 if (err->code == GOT_ERR_EOF)
71 return got_error(GOT_ERR_PRIVSEP_PIPE);
77 if (errno == EAGAIN) /* Could be a file-descriptor leak. */
78 return got_error(GOT_ERR_PRIVSEP_NO_FD);
79 return got_error(GOT_ERR_PRIVSEP_READ);
82 return got_error(GOT_ERR_PRIVSEP_PIPE);
87 const struct got_error *
88 got_privsep_wait_for_child(pid_t pid)
92 if (waitpid(pid, &child_status, 0) == -1)
93 return got_error_from_errno("waitpid");
95 if (!WIFEXITED(child_status))
96 return got_error(GOT_ERR_PRIVSEP_DIED);
98 if (WEXITSTATUS(child_status) != 0)
99 return got_error(GOT_ERR_PRIVSEP_EXIT);
104 static const struct got_error *
105 recv_imsg_error(struct imsg *imsg, size_t datalen)
107 struct got_imsg_error *ierr;
109 if (datalen != sizeof(*ierr))
110 return got_error(GOT_ERR_PRIVSEP_LEN);
113 if (ierr->code == GOT_ERR_ERRNO) {
114 static struct got_error serr;
115 serr.code = GOT_ERR_ERRNO;
116 serr.msg = strerror(ierr->errno_code);
120 return got_error(ierr->code);
123 const struct got_error *
124 got_privsep_recv_imsg(struct imsg *imsg, struct imsgbuf *ibuf,
127 const struct got_error *err;
130 n = imsg_get(ibuf, imsg);
132 return got_error_from_errno("imsg_get");
135 err = read_imsg(ibuf);
138 n = imsg_get(ibuf, imsg);
140 return got_error_from_errno("imsg_get");
143 if (imsg->hdr.len < IMSG_HEADER_SIZE + min_datalen)
144 return got_error(GOT_ERR_PRIVSEP_LEN);
146 if (imsg->hdr.type == GOT_IMSG_ERROR) {
147 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
148 return recv_imsg_error(imsg, datalen);
154 /* Attempt to send an error in an imsg. Complain on stderr as a last resort. */
156 got_privsep_send_error(struct imsgbuf *ibuf, const struct got_error *err)
158 const struct got_error *poll_err;
159 struct got_imsg_error ierr;
162 ierr.code = err->code;
163 if (err->code == GOT_ERR_ERRNO)
164 ierr.errno_code = errno;
167 ret = imsg_compose(ibuf, GOT_IMSG_ERROR, 0, 0, -1, &ierr, sizeof(ierr));
169 fprintf(stderr, "%s: error %d \"%s\": imsg_compose: %s\n",
170 getprogname(), err->code, err->msg, strerror(errno));
174 poll_err = got_poll_fd(ibuf->fd, POLLOUT, INFTIM);
176 fprintf(stderr, "%s: error %d \"%s\": poll: %s\n",
177 getprogname(), err->code, err->msg, poll_err->msg);
181 ret = imsg_flush(ibuf);
183 fprintf(stderr, "%s: error %d \"%s\": imsg_flush: %s\n",
184 getprogname(), err->code, err->msg, strerror(errno));
190 static const struct got_error *
191 flush_imsg(struct imsgbuf *ibuf)
193 const struct got_error *err;
195 err = got_poll_fd(ibuf->fd, POLLOUT, INFTIM);
199 if (imsg_flush(ibuf) == -1) {
201 return got_error_from_errno("imsg_flush");
207 const struct got_error *
208 got_privsep_flush_imsg(struct imsgbuf *ibuf)
210 return flush_imsg(ibuf);
213 const struct got_error *
214 got_privsep_send_stop(int fd)
216 const struct got_error *err = NULL;
219 imsg_init(&ibuf, fd);
221 if (imsg_compose(&ibuf, GOT_IMSG_STOP, 0, 0, -1, NULL, 0) == -1)
222 return got_error_from_errno("imsg_compose STOP");
224 err = flush_imsg(&ibuf);
228 const struct got_error *
229 got_privsep_send_obj_req(struct imsgbuf *ibuf, int fd,
230 struct got_object_id *id)
232 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_REQUEST, 0, 0, fd,
233 id, sizeof(*id)) == -1)
234 return got_error_from_errno("imsg_compose OBJECT_REQUEST");
236 return flush_imsg(ibuf);
239 const struct got_error *
240 got_privsep_send_raw_obj_req(struct imsgbuf *ibuf, int fd,
241 struct got_object_id *id)
243 if (imsg_compose(ibuf, GOT_IMSG_RAW_OBJECT_REQUEST, 0, 0, fd,
244 id, sizeof(*id)) == -1)
245 return got_error_from_errno("imsg_compose RAW_OBJECT_REQUEST");
247 return flush_imsg(ibuf);
250 const struct got_error *
251 got_privsep_send_raw_obj_outfd(struct imsgbuf *ibuf, int outfd)
253 const struct got_error *err = NULL;
255 if (imsg_compose(ibuf, GOT_IMSG_RAW_OBJECT_OUTFD, 0, 0, outfd, NULL, 0)
257 err = got_error_from_errno("imsg_compose RAW_OBJECT_OUTFD");
262 return flush_imsg(ibuf);
265 const struct got_error *
266 got_privsep_send_raw_obj(struct imsgbuf *ibuf, off_t size, size_t hdrlen,
269 const struct got_error *err = NULL;
270 struct got_imsg_raw_obj iobj;
271 size_t len = sizeof(iobj);
274 memset(&iobj, 0, sizeof(iobj));
275 iobj.hdrlen = hdrlen;
278 if (data && size + hdrlen <= GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX)
279 len += (size_t)size + hdrlen;
281 wbuf = imsg_create(ibuf, GOT_IMSG_RAW_OBJECT, 0, 0, len);
283 err = got_error_from_errno("imsg_create RAW_OBJECT");
287 if (imsg_add(wbuf, &iobj, sizeof(iobj)) == -1)
288 return got_error_from_errno("imsg_add RAW_OBJECT");
290 if (data && size + hdrlen <= GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX) {
291 if (imsg_add(wbuf, data, size + hdrlen) == -1)
292 return got_error_from_errno("imsg_add RAW_OBJECT");
296 imsg_close(ibuf, wbuf);
298 return flush_imsg(ibuf);
301 const struct got_error *
302 got_privsep_recv_raw_obj(uint8_t **outbuf, off_t *size, size_t *hdrlen,
303 struct imsgbuf *ibuf)
305 const struct got_error *err = NULL;
307 struct got_imsg_raw_obj *iobj;
312 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
316 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
318 switch (imsg.hdr.type) {
319 case GOT_IMSG_RAW_OBJECT:
320 if (datalen < sizeof(*iobj)) {
321 err = got_error(GOT_ERR_PRIVSEP_LEN);
326 *hdrlen = iobj->hdrlen;
328 if (datalen == sizeof(*iobj)) {
329 /* Data has been written to file descriptor. */
334 *size + *hdrlen > GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX) {
335 err = got_error(GOT_ERR_PRIVSEP_LEN);
339 *outbuf = malloc(*size + *hdrlen);
340 if (*outbuf == NULL) {
341 err = got_error_from_errno("malloc");
344 memcpy(*outbuf, imsg.data + sizeof(*iobj), *size + *hdrlen);
347 err = got_error(GOT_ERR_PRIVSEP_MSG);
356 const struct got_error *
357 got_privsep_send_commit_req(struct imsgbuf *ibuf, int fd,
358 struct got_object_id *id, int pack_idx)
360 const struct got_error *err = NULL;
361 struct got_imsg_packed_object iobj;
365 memset(&iobj, 0, sizeof(iobj));
366 if (pack_idx != -1) { /* commit is packed */
368 memcpy(&iobj.id, id, sizeof(iobj.id));
376 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_REQUEST, 0, 0, fd, data, len)
378 err = got_error_from_errno("imsg_compose COMMIT_REQUEST");
383 return flush_imsg(ibuf);
386 const struct got_error *
387 got_privsep_send_tree_req(struct imsgbuf *ibuf, int fd,
388 struct got_object_id *id, int pack_idx)
394 len = sizeof(struct got_imsg_packed_object);
398 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_REQUEST, 0, 0, len);
400 return got_error_from_errno("imsg_create TREE_REQUEST");
402 if (imsg_add(wbuf, id, sizeof(*id)) == -1)
403 return got_error_from_errno("imsg_add TREE_REQUEST");
405 if (pack_idx != -1) { /* tree is packed */
406 if (imsg_add(wbuf, &pack_idx, sizeof(pack_idx)) == -1)
407 return got_error_from_errno("imsg_add TREE_REQUEST");
411 imsg_close(ibuf, wbuf);
413 return flush_imsg(ibuf);
416 const struct got_error *
417 got_privsep_send_tag_req(struct imsgbuf *ibuf, int fd,
418 struct got_object_id *id, int pack_idx)
420 struct got_imsg_packed_object iobj;
424 memset(&iobj, 0, sizeof(iobj));
425 if (pack_idx != -1) { /* tag is packed */
427 memcpy(&iobj.id, id, sizeof(iobj.id));
435 if (imsg_compose(ibuf, GOT_IMSG_TAG_REQUEST, 0, 0, fd, data, len)
437 return got_error_from_errno("imsg_compose TAG_REQUEST");
439 return flush_imsg(ibuf);
442 const struct got_error *
443 got_privsep_send_blob_req(struct imsgbuf *ibuf, int infd,
444 struct got_object_id *id, int pack_idx)
446 const struct got_error *err = NULL;
447 struct got_imsg_packed_object iobj;
451 memset(&iobj, 0, sizeof(iobj));
452 if (pack_idx != -1) { /* blob is packed */
454 memcpy(&iobj.id, id, sizeof(iobj.id));
462 if (imsg_compose(ibuf, GOT_IMSG_BLOB_REQUEST, 0, 0, infd, data, len)
464 err = got_error_from_errno("imsg_compose BLOB_REQUEST");
469 return flush_imsg(ibuf);
472 const struct got_error *
473 got_privsep_send_blob_outfd(struct imsgbuf *ibuf, int outfd)
475 const struct got_error *err = NULL;
477 if (imsg_compose(ibuf, GOT_IMSG_BLOB_OUTFD, 0, 0, outfd, NULL, 0)
479 err = got_error_from_errno("imsg_compose BLOB_OUTFD");
484 return flush_imsg(ibuf);
487 static const struct got_error *
488 send_fd(struct imsgbuf *ibuf, int imsg_code, int fd)
490 const struct got_error *err = NULL;
492 if (imsg_compose(ibuf, imsg_code, 0, 0, fd, NULL, 0) == -1) {
493 err = got_error_from_errno("imsg_compose TMPFD");
498 return flush_imsg(ibuf);
501 const struct got_error *
502 got_privsep_send_tmpfd(struct imsgbuf *ibuf, int fd)
504 return send_fd(ibuf, GOT_IMSG_TMPFD, fd);
507 const struct got_error *
508 got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj)
510 struct got_imsg_object iobj;
512 memset(&iobj, 0, sizeof(iobj));
514 memcpy(&iobj.id, &obj->id, sizeof(iobj.id));
515 iobj.type = obj->type;
516 iobj.flags = obj->flags;
517 iobj.hdrlen = obj->hdrlen;
518 iobj.size = obj->size;
519 if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
520 iobj.pack_offset = obj->pack_offset;
521 iobj.pack_idx = obj->pack_idx;
524 if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
526 return got_error_from_errno("imsg_compose OBJECT");
528 return flush_imsg(ibuf);
531 const struct got_error *
532 got_privsep_send_fetch_req(struct imsgbuf *ibuf, int fd,
533 struct got_pathlist_head *have_refs, int fetch_all_branches,
534 struct got_pathlist_head *wanted_branches,
535 struct got_pathlist_head *wanted_refs, int list_refs_only,
536 const char *worktree_branch, int verbosity)
538 const struct got_error *err = NULL;
540 size_t len, worktree_branch_len;
541 struct got_pathlist_entry *pe;
542 struct got_imsg_fetch_request fetchreq;
544 if (worktree_branch) {
545 worktree_branch_len = strlen(worktree_branch);
546 len = sizeof(fetchreq) + worktree_branch_len;
548 len = sizeof(fetchreq);
550 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
552 return got_error(GOT_ERR_NO_SPACE);
555 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_REQUEST, 0, 0, len);
557 return got_error_from_errno("imsg_create FETCH_HAVE_REF");
559 memset(&fetchreq, 0, sizeof(fetchreq));
560 fetchreq.fetch_all_branches = fetch_all_branches;
561 fetchreq.list_refs_only = list_refs_only;
562 fetchreq.verbosity = verbosity;
563 if (worktree_branch != NULL)
564 fetchreq.worktree_branch_len = worktree_branch_len;
565 TAILQ_FOREACH(pe, have_refs, entry)
566 fetchreq.n_have_refs++;
567 TAILQ_FOREACH(pe, wanted_branches, entry)
568 fetchreq.n_wanted_branches++;
569 TAILQ_FOREACH(pe, wanted_refs, entry)
570 fetchreq.n_wanted_refs++;
571 if (imsg_add(wbuf, &fetchreq, sizeof(fetchreq)) == -1)
572 return got_error_from_errno("imsg_add FETCH_REQUEST");
573 if (worktree_branch) {
574 if (imsg_add(wbuf, worktree_branch, worktree_branch_len) == -1)
575 return got_error_from_errno("imsg_add FETCH_REQUEST");
578 imsg_close(ibuf, wbuf);
580 err = flush_imsg(ibuf);
587 TAILQ_FOREACH(pe, have_refs, entry) {
588 const char *name = pe->path;
589 size_t name_len = pe->path_len;
590 struct got_object_id *id = pe->data;
592 len = sizeof(struct got_imsg_fetch_have_ref) + name_len;
593 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_HAVE_REF, 0, 0, len);
595 return got_error_from_errno("imsg_create FETCH_HAVE_REF");
597 /* Keep in sync with struct got_imsg_fetch_have_ref! */
598 if (imsg_add(wbuf, id, sizeof(*id)) == -1)
599 return got_error_from_errno("imsg_add FETCH_HAVE_REF");
600 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1)
601 return got_error_from_errno("imsg_add FETCH_HAVE_REF");
602 if (imsg_add(wbuf, name, name_len) == -1)
603 return got_error_from_errno("imsg_add FETCH_HAVE_REF");
606 imsg_close(ibuf, wbuf);
607 err = flush_imsg(ibuf);
612 TAILQ_FOREACH(pe, wanted_branches, entry) {
613 const char *name = pe->path;
614 size_t name_len = pe->path_len;
616 len = sizeof(struct got_imsg_fetch_wanted_branch) + name_len;
617 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_WANTED_BRANCH, 0, 0,
620 return got_error_from_errno(
621 "imsg_create FETCH_WANTED_BRANCH");
623 /* Keep in sync with struct got_imsg_fetch_wanted_branch! */
624 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1)
625 return got_error_from_errno(
626 "imsg_add FETCH_WANTED_BRANCH");
627 if (imsg_add(wbuf, name, name_len) == -1)
628 return got_error_from_errno(
629 "imsg_add FETCH_WANTED_BRANCH");
632 imsg_close(ibuf, wbuf);
633 err = flush_imsg(ibuf);
638 TAILQ_FOREACH(pe, wanted_refs, entry) {
639 const char *name = pe->path;
640 size_t name_len = pe->path_len;
642 len = sizeof(struct got_imsg_fetch_wanted_ref) + name_len;
643 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_WANTED_REF, 0, 0,
646 return got_error_from_errno(
647 "imsg_create FETCH_WANTED_REF");
649 /* Keep in sync with struct got_imsg_fetch_wanted_ref! */
650 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1)
651 return got_error_from_errno(
652 "imsg_add FETCH_WANTED_REF");
653 if (imsg_add(wbuf, name, name_len) == -1)
654 return got_error_from_errno(
655 "imsg_add FETCH_WANTED_REF");
658 imsg_close(ibuf, wbuf);
659 err = flush_imsg(ibuf);
669 const struct got_error *
670 got_privsep_send_fetch_outfd(struct imsgbuf *ibuf, int fd)
672 return send_fd(ibuf, GOT_IMSG_FETCH_OUTFD, fd);
675 const struct got_error *
676 got_privsep_recv_fetch_progress(int *done, struct got_object_id **id,
677 char **refname, struct got_pathlist_head *symrefs, char **server_progress,
678 off_t *packfile_size, uint8_t *pack_sha1, struct imsgbuf *ibuf)
680 const struct got_error *err = NULL;
683 struct got_imsg_fetch_symrefs *isymrefs = NULL;
691 *server_progress = NULL;
693 memset(pack_sha1, 0, SHA1_DIGEST_LENGTH);
695 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
699 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
700 switch (imsg.hdr.type) {
701 case GOT_IMSG_FETCH_SYMREFS:
702 if (datalen < sizeof(*isymrefs)) {
703 err = got_error(GOT_ERR_PRIVSEP_LEN);
706 if (isymrefs != NULL) {
707 err = got_error(GOT_ERR_PRIVSEP_MSG);
710 isymrefs = (struct got_imsg_fetch_symrefs *)imsg.data;
711 off = sizeof(*isymrefs);
712 remain = datalen - off;
713 for (n = 0; n < isymrefs->nsymrefs; n++) {
714 struct got_imsg_fetch_symref *s;
716 if (remain < sizeof(struct got_imsg_fetch_symref)) {
717 err = got_error(GOT_ERR_PRIVSEP_LEN);
720 s = (struct got_imsg_fetch_symref *)(imsg.data + off);
722 remain -= sizeof(*s);
723 if (remain < s->name_len) {
724 err = got_error(GOT_ERR_PRIVSEP_LEN);
727 name = strndup(imsg.data + off, s->name_len);
729 err = got_error_from_errno("strndup");
733 remain -= s->name_len;
734 if (remain < s->target_len) {
735 err = got_error(GOT_ERR_PRIVSEP_LEN);
739 target = strndup(imsg.data + off, s->target_len);
740 if (target == NULL) {
741 err = got_error_from_errno("strndup");
745 off += s->target_len;
746 remain -= s->target_len;
747 err = got_pathlist_append(symrefs, name, target);
755 case GOT_IMSG_FETCH_REF:
756 if (datalen <= sizeof(**id)) {
757 err = got_error(GOT_ERR_PRIVSEP_MSG);
760 *id = malloc(sizeof(**id));
762 err = got_error_from_errno("malloc");
765 memcpy(*id, imsg.data, sizeof(**id));
766 *refname = strndup(imsg.data + sizeof(**id),
767 datalen - sizeof(**id));
768 if (*refname == NULL) {
769 err = got_error_from_errno("strndup");
773 case GOT_IMSG_FETCH_SERVER_PROGRESS:
775 err = got_error(GOT_ERR_PRIVSEP_LEN);
778 *server_progress = strndup(imsg.data, datalen);
779 if (*server_progress == NULL) {
780 err = got_error_from_errno("strndup");
783 for (i = 0; i < datalen; i++) {
784 if (!isprint((unsigned char)(*server_progress)[i]) &&
785 !isspace((unsigned char)(*server_progress)[i])) {
786 err = got_error(GOT_ERR_PRIVSEP_MSG);
787 free(*server_progress);
788 *server_progress = NULL;
793 case GOT_IMSG_FETCH_DOWNLOAD_PROGRESS:
794 if (datalen < sizeof(*packfile_size)) {
795 err = got_error(GOT_ERR_PRIVSEP_MSG);
798 memcpy(packfile_size, imsg.data, sizeof(*packfile_size));
800 case GOT_IMSG_FETCH_DONE:
801 if (datalen != SHA1_DIGEST_LENGTH) {
802 err = got_error(GOT_ERR_PRIVSEP_MSG);
805 memcpy(pack_sha1, imsg.data, SHA1_DIGEST_LENGTH);
809 err = got_error(GOT_ERR_PRIVSEP_MSG);
823 static const struct got_error *
824 send_send_ref(const char *name, size_t name_len, struct got_object_id *id,
825 int delete, struct imsgbuf *ibuf)
830 len = sizeof(struct got_imsg_send_ref) + name_len;
831 wbuf = imsg_create(ibuf, GOT_IMSG_SEND_REF, 0, 0, len);
833 return got_error_from_errno("imsg_create SEND_REF");
835 /* Keep in sync with struct got_imsg_send_ref! */
836 if (imsg_add(wbuf, id, sizeof(*id)) == -1)
837 return got_error_from_errno("imsg_add SEND_REF");
838 if (imsg_add(wbuf, &delete, sizeof(delete)) == -1)
839 return got_error_from_errno("imsg_add SEND_REF");
840 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1)
841 return got_error_from_errno("imsg_add SEND_REF");
842 if (imsg_add(wbuf, name, name_len) == -1)
843 return got_error_from_errno("imsg_add SEND_REF");
846 imsg_close(ibuf, wbuf);
847 return flush_imsg(ibuf);
850 const struct got_error *
851 got_privsep_send_send_req(struct imsgbuf *ibuf, int fd,
852 struct got_pathlist_head *have_refs,
853 struct got_pathlist_head *delete_refs,
856 const struct got_error *err = NULL;
857 struct got_pathlist_entry *pe;
858 struct got_imsg_send_request sendreq;
859 struct got_object_id zero_id;
861 memset(&zero_id, 0, sizeof(zero_id));
862 memset(&sendreq, 0, sizeof(sendreq));
863 sendreq.verbosity = verbosity;
864 TAILQ_FOREACH(pe, have_refs, entry)
866 TAILQ_FOREACH(pe, delete_refs, entry)
868 if (imsg_compose(ibuf, GOT_IMSG_SEND_REQUEST, 0, 0, fd,
869 &sendreq, sizeof(sendreq)) == -1) {
870 err = got_error_from_errno(
871 "imsg_compose FETCH_SERVER_PROGRESS");
875 err = flush_imsg(ibuf);
880 TAILQ_FOREACH(pe, have_refs, entry) {
881 const char *name = pe->path;
882 size_t name_len = pe->path_len;
883 struct got_object_id *id = pe->data;
884 err = send_send_ref(name, name_len, id, 0, ibuf);
889 TAILQ_FOREACH(pe, delete_refs, entry) {
890 const char *name = pe->path;
891 size_t name_len = pe->path_len;
892 err = send_send_ref(name, name_len, &zero_id, 1, ibuf);
897 if (fd != -1 && close(fd) == -1 && err == NULL)
898 err = got_error_from_errno("close");
903 const struct got_error *
904 got_privsep_recv_send_remote_refs(struct got_pathlist_head *remote_refs,
905 struct imsgbuf *ibuf)
907 const struct got_error *err = NULL;
911 struct got_imsg_send_remote_ref iremote_ref;
912 struct got_object_id *id = NULL;
913 char *refname = NULL;
914 struct got_pathlist_entry *new;
917 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
920 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
921 switch (imsg.hdr.type) {
922 case GOT_IMSG_SEND_REMOTE_REF:
923 if (datalen < sizeof(iremote_ref)) {
924 err = got_error(GOT_ERR_PRIVSEP_MSG);
927 memcpy(&iremote_ref, imsg.data, sizeof(iremote_ref));
928 if (datalen != sizeof(iremote_ref) +
929 iremote_ref.name_len) {
930 err = got_error(GOT_ERR_PRIVSEP_MSG);
933 id = calloc(1, sizeof(*id));
935 err = got_error_from_errno("malloc");
938 memcpy(id, &iremote_ref.id, sizeof(*id));
939 refname = strndup(imsg.data + sizeof(iremote_ref),
940 datalen - sizeof(iremote_ref));
941 if (refname == NULL) {
942 err = got_error_from_errno("strndup");
945 err = got_pathlist_insert(&new, remote_refs,
949 if (new == NULL) { /* duplicate which wasn't inserted */
956 case GOT_IMSG_SEND_PACK_REQUEST:
958 err = got_error(GOT_ERR_PRIVSEP_MSG);
961 /* got-send-pack is now waiting for a pack file. */
965 err = got_error(GOT_ERR_PRIVSEP_MSG);
976 const struct got_error *
977 got_privsep_send_packfd(struct imsgbuf *ibuf, int fd)
979 return send_fd(ibuf, GOT_IMSG_SEND_PACKFD, fd);
982 const struct got_error *
983 got_privsep_recv_send_progress(int *done, off_t *bytes_sent,
984 int *success, char **refname, char **errmsg, struct imsgbuf *ibuf)
986 const struct got_error *err = NULL;
989 struct got_imsg_send_ref_status iref_status;
991 /* Do not reset the current value of 'bytes_sent', it accumulates. */
997 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1001 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1002 switch (imsg.hdr.type) {
1003 case GOT_IMSG_SEND_UPLOAD_PROGRESS:
1004 if (datalen < sizeof(*bytes_sent)) {
1005 err = got_error(GOT_ERR_PRIVSEP_MSG);
1008 memcpy(bytes_sent, imsg.data, sizeof(*bytes_sent));
1010 case GOT_IMSG_SEND_REF_STATUS:
1011 if (datalen < sizeof(iref_status)) {
1012 err = got_error(GOT_ERR_PRIVSEP_MSG);
1015 memcpy(&iref_status, imsg.data, sizeof(iref_status));
1016 if (datalen != sizeof(iref_status) + iref_status.name_len +
1017 iref_status.errmsg_len) {
1018 err = got_error(GOT_ERR_PRIVSEP_MSG);
1021 *success = iref_status.success;
1022 *refname = strndup(imsg.data + sizeof(iref_status),
1023 iref_status.name_len);
1025 if (iref_status.errmsg_len != 0)
1026 *errmsg = strndup(imsg.data + sizeof(iref_status) +
1027 iref_status.name_len, iref_status.errmsg_len);
1029 case GOT_IMSG_SEND_DONE:
1031 err = got_error(GOT_ERR_PRIVSEP_MSG);
1037 err = got_error(GOT_ERR_PRIVSEP_MSG);
1045 const struct got_error *
1046 got_privsep_send_index_pack_req(struct imsgbuf *ibuf, uint8_t *pack_sha1,
1049 const struct got_error *err = NULL;
1051 /* Keep in sync with struct got_imsg_index_pack_request */
1052 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_REQUEST, 0, 0, fd,
1053 pack_sha1, SHA1_DIGEST_LENGTH) == -1) {
1054 err = got_error_from_errno("imsg_compose INDEX_REQUEST");
1058 return flush_imsg(ibuf);
1061 const struct got_error *
1062 got_privsep_send_index_pack_outfd(struct imsgbuf *ibuf, int fd)
1064 return send_fd(ibuf, GOT_IMSG_IDXPACK_OUTFD, fd);
1067 const struct got_error *
1068 got_privsep_recv_index_progress(int *done, int *nobj_total,
1069 int *nobj_indexed, int *nobj_loose, int *nobj_resolved,
1070 struct imsgbuf *ibuf)
1072 const struct got_error *err = NULL;
1074 struct got_imsg_index_pack_progress *iprogress;
1082 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1086 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1087 switch (imsg.hdr.type) {
1088 case GOT_IMSG_IDXPACK_PROGRESS:
1089 if (datalen < sizeof(*iprogress)) {
1090 err = got_error(GOT_ERR_PRIVSEP_LEN);
1093 iprogress = (struct got_imsg_index_pack_progress *)imsg.data;
1094 if (iprogress->nobj_total < 0 || iprogress->nobj_indexed < 0 ||
1095 iprogress->nobj_loose < 0 || iprogress->nobj_resolved < 0) {
1096 err = got_error(GOT_ERR_RANGE);
1099 *nobj_total = iprogress->nobj_total;
1100 *nobj_indexed = iprogress->nobj_indexed;
1101 *nobj_loose = iprogress->nobj_loose;
1102 *nobj_resolved = iprogress->nobj_resolved;
1104 case GOT_IMSG_IDXPACK_DONE:
1106 err = got_error(GOT_ERR_PRIVSEP_LEN);
1112 err = got_error(GOT_ERR_PRIVSEP_MSG);
1120 const struct got_error *
1121 got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
1122 struct imsgbuf *ibuf)
1124 struct got_imsg_object *iobj;
1125 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1127 if (datalen != sizeof(*iobj))
1128 return got_error(GOT_ERR_PRIVSEP_LEN);
1131 if (iobj->pack_offset < 0)
1132 return got_error(GOT_ERR_PACK_OFFSET);
1134 *obj = calloc(1, sizeof(**obj));
1136 return got_error_from_errno("calloc");
1138 memcpy(&(*obj)->id, &iobj->id, sizeof(iobj->id));
1139 (*obj)->type = iobj->type;
1140 (*obj)->flags = iobj->flags;
1141 (*obj)->hdrlen = iobj->hdrlen;
1142 (*obj)->size = iobj->size;
1143 /* path_packfile is handled by caller */
1144 if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
1145 (*obj)->pack_offset = iobj->pack_offset;
1146 (*obj)->pack_idx = iobj->pack_idx;
1148 STAILQ_INIT(&(*obj)->deltas.entries);
1152 const struct got_error *
1153 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
1155 const struct got_error *err = NULL;
1157 const size_t min_datalen =
1158 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
1162 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1166 switch (imsg.hdr.type) {
1167 case GOT_IMSG_OBJECT:
1168 err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
1171 err = got_error(GOT_ERR_PRIVSEP_MSG);
1180 static const struct got_error *
1181 send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
1184 const struct got_error *err = NULL;
1185 size_t offset, remain;
1188 remain = logmsg_len;
1189 while (remain > 0) {
1190 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1192 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
1193 commit->logmsg + offset, n) == -1) {
1194 err = got_error_from_errno("imsg_compose "
1199 err = flush_imsg(ibuf);
1210 const struct got_error *
1211 got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
1213 const struct got_error *err = NULL;
1214 struct got_imsg_commit_object *icommit;
1217 struct got_object_qid *qid;
1218 size_t author_len = strlen(commit->author);
1219 size_t committer_len = strlen(commit->committer);
1220 size_t logmsg_len = strlen(commit->logmsg);
1222 total = sizeof(*icommit) + author_len + committer_len +
1223 commit->nparents * sizeof(struct got_object_id);
1225 buf = malloc(total);
1227 return got_error_from_errno("malloc");
1229 icommit = (struct got_imsg_commit_object *)buf;
1230 memcpy(&icommit->tree_id, commit->tree_id, sizeof(icommit->tree_id));
1231 icommit->author_len = author_len;
1232 icommit->author_time = commit->author_time;
1233 icommit->author_gmtoff = commit->author_gmtoff;
1234 icommit->committer_len = committer_len;
1235 icommit->committer_time = commit->committer_time;
1236 icommit->committer_gmtoff = commit->committer_gmtoff;
1237 icommit->logmsg_len = logmsg_len;
1238 icommit->nparents = commit->nparents;
1240 len = sizeof(*icommit);
1241 memcpy(buf + len, commit->author, author_len);
1243 memcpy(buf + len, commit->committer, committer_len);
1244 len += committer_len;
1245 STAILQ_FOREACH(qid, &commit->parent_ids, entry) {
1246 memcpy(buf + len, &qid->id, sizeof(qid->id));
1247 len += sizeof(qid->id);
1250 if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
1251 err = got_error_from_errno("imsg_compose COMMIT");
1255 if (logmsg_len == 0 ||
1256 logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1257 err = flush_imsg(ibuf);
1261 err = send_commit_logmsg(ibuf, commit, logmsg_len);
1267 static const struct got_error *
1268 get_commit_from_imsg(struct got_commit_object **commit,
1269 struct imsg *imsg, size_t datalen, struct imsgbuf *ibuf)
1271 const struct got_error *err = NULL;
1272 struct got_imsg_commit_object *icommit;
1276 if (datalen < sizeof(*icommit))
1277 return got_error(GOT_ERR_PRIVSEP_LEN);
1279 icommit = imsg->data;
1280 if (datalen != sizeof(*icommit) + icommit->author_len +
1281 icommit->committer_len +
1282 icommit->nparents * sizeof(struct got_object_id))
1283 return got_error(GOT_ERR_PRIVSEP_LEN);
1285 if (icommit->nparents < 0)
1286 return got_error(GOT_ERR_PRIVSEP_LEN);
1288 len += sizeof(*icommit);
1290 *commit = got_object_commit_alloc_partial();
1291 if (*commit == NULL)
1292 return got_error_from_errno(
1293 "got_object_commit_alloc_partial");
1295 memcpy((*commit)->tree_id, &icommit->tree_id,
1296 sizeof(icommit->tree_id));
1297 (*commit)->author_time = icommit->author_time;
1298 (*commit)->author_gmtoff = icommit->author_gmtoff;
1299 (*commit)->committer_time = icommit->committer_time;
1300 (*commit)->committer_gmtoff = icommit->committer_gmtoff;
1302 (*commit)->author = strndup(imsg->data + len, icommit->author_len);
1303 if ((*commit)->author == NULL) {
1304 err = got_error_from_errno("strndup");
1307 len += icommit->author_len;
1309 (*commit)->committer = strndup(imsg->data + len,
1310 icommit->committer_len);
1311 if ((*commit)->committer == NULL) {
1312 err = got_error_from_errno("strndup");
1315 len += icommit->committer_len;
1317 if (icommit->logmsg_len == 0) {
1318 (*commit)->logmsg = strdup("");
1319 if ((*commit)->logmsg == NULL) {
1320 err = got_error_from_errno("strdup");
1324 size_t offset = 0, remain = icommit->logmsg_len;
1326 (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
1327 if ((*commit)->logmsg == NULL) {
1328 err = got_error_from_errno("malloc");
1331 while (remain > 0) {
1332 struct imsg imsg_log;
1333 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1336 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1340 if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG) {
1341 err = got_error(GOT_ERR_PRIVSEP_MSG);
1345 memcpy((*commit)->logmsg + offset,
1347 imsg_free(&imsg_log);
1351 (*commit)->logmsg[icommit->logmsg_len] = '\0';
1354 for (i = 0; i < icommit->nparents; i++) {
1355 struct got_object_qid *qid;
1357 err = got_object_qid_alloc_partial(&qid);
1360 memcpy(&qid->id, imsg->data + len +
1361 i * sizeof(qid->id), sizeof(qid->id));
1362 STAILQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
1363 (*commit)->nparents++;
1367 got_object_commit_close(*commit);
1373 const struct got_error *
1374 got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
1376 const struct got_error *err = NULL;
1379 const size_t min_datalen =
1380 MIN(sizeof(struct got_imsg_error),
1381 sizeof(struct got_imsg_commit_object));
1385 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1389 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1391 switch (imsg.hdr.type) {
1392 case GOT_IMSG_COMMIT:
1393 err = get_commit_from_imsg(commit, &imsg, datalen, ibuf);
1396 err = got_error(GOT_ERR_PRIVSEP_MSG);
1405 static const struct got_error *
1406 send_tree_entries_batch(struct imsgbuf *ibuf,
1407 struct got_parsed_tree_entry *entries, int idx0, int idxN, size_t len)
1410 struct got_imsg_tree_entries ientries;
1413 memset(&ientries, 0, sizeof(ientries));
1415 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_ENTRIES, 0, 0, len);
1417 return got_error_from_errno("imsg_create TREE_ENTRY");
1419 ientries.nentries = idxN - idx0 + 1;
1420 if (imsg_add(wbuf, &ientries, sizeof(ientries)) == -1)
1421 return got_error_from_errno("imsg_add TREE_ENTRY");
1423 for (i = idx0; i <= idxN; i++) {
1424 static const char gap[12]; /* for sha1 inside sha256 align. */
1425 struct got_parsed_tree_entry *pte = &entries[i];
1427 /* Keep in sync with struct got_imsg_tree_entry definition! */
1428 if (imsg_add(wbuf, pte->id, pte->idlen) == -1)
1429 return got_error_from_errno("imsg_add TREE_ENTRY");
1430 if (pte->idlen != SHA256_DIGEST_LENGTH &&
1431 imsg_add(wbuf, gap, sizeof(gap)) == -1)
1432 return got_error_from_errno("imsg_add TREE_ENTRY");
1433 if (imsg_add(wbuf, &pte->algo, sizeof(pte->algo)) == -1)
1434 return got_error_from_errno("imsg_add TREE_ENTRY");
1435 if (imsg_add(wbuf, &pte->mode, sizeof(pte->mode)) == -1)
1436 return got_error_from_errno("imsg_add TREE_ENTRY");
1437 if (imsg_add(wbuf, &pte->namelen, sizeof(pte->namelen)) == -1)
1438 return got_error_from_errno("imsg_add TREE_ENTRY");
1440 /* Remaining bytes are the entry's name. */
1441 if (imsg_add(wbuf, pte->name, pte->namelen) == -1)
1442 return got_error_from_errno("imsg_add TREE_ENTRY");
1446 imsg_close(ibuf, wbuf);
1450 static const struct got_error *
1451 send_tree_entries(struct imsgbuf *ibuf, struct got_parsed_tree_entry *entries,
1454 const struct got_error *err = NULL;
1456 size_t entries_len = sizeof(struct got_imsg_tree_entries);
1459 for (j = 0; j < nentries; j++) {
1460 struct got_parsed_tree_entry *pte = &entries[j];
1461 size_t len = sizeof(struct got_imsg_tree_entry)
1465 entries_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1466 err = send_tree_entries_batch(ibuf, entries,
1467 i, j - 1, entries_len);
1471 entries_len = sizeof(struct got_imsg_tree_entries);
1478 err = send_tree_entries_batch(ibuf, entries, i, j - 1,
1487 const struct got_error *
1488 got_privsep_send_tree(struct imsgbuf *ibuf,
1489 struct got_parsed_tree_entry *entries, int nentries)
1491 const struct got_error *err = NULL;
1492 struct got_imsg_tree_object itree;
1494 memset(&itree, 0, sizeof(itree));
1495 itree.nentries = nentries;
1496 if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
1498 return got_error_from_errno("imsg_compose TREE");
1500 err = send_tree_entries(ibuf, entries, nentries);
1504 return flush_imsg(ibuf);
1508 static const struct got_error *
1509 recv_tree_entries(void *data, size_t datalen, struct got_tree_object *tree,
1512 const struct got_error *err = NULL;
1513 struct got_imsg_tree_entries *ientries;
1514 struct got_tree_entry *te;
1518 if (datalen <= sizeof(*ientries) ||
1519 datalen > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
1520 return got_error(GOT_ERR_PRIVSEP_LEN);
1522 ientries = (struct got_imsg_tree_entries *)data;
1523 if (ientries->nentries > INT_MAX) {
1524 return got_error_msg(GOT_ERR_NO_SPACE,
1525 "too many tree entries");
1528 te_offset = sizeof(*ientries);
1529 for (i = 0; i < ientries->nentries; i++) {
1530 struct got_imsg_tree_entry ite;
1531 const char *te_name;
1532 uint8_t *buf = (uint8_t *)data + te_offset;
1534 if (te_offset >= datalen) {
1535 err = got_error(GOT_ERR_PRIVSEP_LEN);
1539 /* Might not be aligned, size is ~32 bytes. */
1540 memcpy(&ite, buf, sizeof(ite));
1542 if (ite.namelen >= sizeof(te->name)) {
1543 err = got_error(GOT_ERR_PRIVSEP_LEN);
1546 if (te_offset + sizeof(ite) + ite.namelen > datalen) {
1547 err = got_error(GOT_ERR_PRIVSEP_LEN);
1551 if (*nentries >= tree->nentries) {
1552 err = got_error(GOT_ERR_PRIVSEP_LEN);
1555 te = &tree->entries[*nentries];
1556 te_name = buf + sizeof(ite);
1557 memcpy(te->name, te_name, ite.namelen);
1558 te->name[ite.namelen] = '\0';
1559 memcpy(te->id.hash, ite.id, got_hash_digest_length(ite.algo));
1560 te->id.algo = ite.algo;
1561 te->mode = ite.mode;
1562 te->idx = *nentries;
1565 te_offset += sizeof(ite) + ite.namelen;
1571 const struct got_error *
1572 got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
1574 const struct got_error *err = NULL;
1575 const size_t min_datalen =
1576 MIN(sizeof(struct got_imsg_error),
1577 sizeof(struct got_imsg_tree_object));
1578 struct got_imsg_tree_object *itree;
1583 while (*tree == NULL || nentries < (*tree)->nentries) {
1587 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1591 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1593 switch (imsg.hdr.type) {
1595 /* This message should only appear once. */
1596 if (*tree != NULL) {
1597 err = got_error(GOT_ERR_PRIVSEP_MSG);
1600 if (datalen != sizeof(*itree)) {
1601 err = got_error(GOT_ERR_PRIVSEP_LEN);
1605 if (itree->nentries < 0) {
1606 err = got_error(GOT_ERR_PRIVSEP_LEN);
1609 *tree = malloc(sizeof(**tree));
1610 if (*tree == NULL) {
1611 err = got_error_from_errno("malloc");
1614 (*tree)->entries = calloc(itree->nentries,
1615 sizeof(struct got_tree_entry));
1616 if ((*tree)->entries == NULL) {
1617 err = got_error_from_errno("malloc");
1622 (*tree)->nentries = itree->nentries;
1623 (*tree)->refcnt = 0;
1625 case GOT_IMSG_TREE_ENTRIES:
1626 /* This message should be preceeded by GOT_IMSG_TREE. */
1627 if (*tree == NULL) {
1628 err = got_error(GOT_ERR_PRIVSEP_MSG);
1631 err = recv_tree_entries(imsg.data, datalen,
1635 err = got_error(GOT_ERR_PRIVSEP_MSG);
1644 if (*tree && (*tree)->nentries != nentries) {
1646 err = got_error(GOT_ERR_PRIVSEP_LEN);
1647 got_object_tree_close(*tree);
1654 const struct got_error *
1655 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
1656 const uint8_t *data)
1658 struct got_imsg_blob iblob;
1660 memset(&iblob, 0, sizeof(iblob));
1662 iblob.hdrlen = hdrlen;
1667 if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
1668 return got_error(GOT_ERR_NO_SPACE);
1670 buf = malloc(sizeof(iblob) + size);
1672 return got_error_from_errno("malloc");
1674 memcpy(buf, &iblob, sizeof(iblob));
1675 memcpy(buf + sizeof(iblob), data, size);
1676 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
1677 sizeof(iblob) + size) == -1) {
1679 return got_error_from_errno("imsg_compose BLOB");
1683 /* Data has already been written to file descriptor. */
1684 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
1685 sizeof(iblob)) == -1)
1686 return got_error_from_errno("imsg_compose BLOB");
1690 return flush_imsg(ibuf);
1693 const struct got_error *
1694 got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1695 struct imsgbuf *ibuf)
1697 const struct got_error *err = NULL;
1699 struct got_imsg_blob *iblob;
1704 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1708 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1710 switch (imsg.hdr.type) {
1712 if (datalen < sizeof(*iblob)) {
1713 err = got_error(GOT_ERR_PRIVSEP_LEN);
1717 *size = iblob->size;
1718 *hdrlen = iblob->hdrlen;
1720 if (datalen == sizeof(*iblob)) {
1721 /* Data has been written to file descriptor. */
1725 if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX ||
1726 *size > datalen + sizeof(*iblob)) {
1727 err = got_error(GOT_ERR_PRIVSEP_LEN);
1731 *outbuf = malloc(*size);
1732 if (*outbuf == NULL) {
1733 err = got_error_from_errno("malloc");
1736 memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
1739 err = got_error(GOT_ERR_PRIVSEP_MSG);
1748 static const struct got_error *
1749 send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
1751 const struct got_error *err = NULL;
1752 size_t offset, remain;
1755 remain = tagmsg_len;
1756 while (remain > 0) {
1757 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1759 if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
1760 tag->tagmsg + offset, n) == -1) {
1761 err = got_error_from_errno("imsg_compose TAG_TAGMSG");
1765 err = flush_imsg(ibuf);
1776 const struct got_error *
1777 got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
1779 const struct got_error *err = NULL;
1780 struct got_imsg_tag_object *itag;
1783 size_t tag_len = strlen(tag->tag);
1784 size_t tagger_len = strlen(tag->tagger);
1785 size_t tagmsg_len = strlen(tag->tagmsg);
1787 total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
1789 buf = malloc(total);
1791 return got_error_from_errno("malloc");
1793 itag = (struct got_imsg_tag_object *)buf;
1794 memcpy(itag->id, tag->id.hash, sizeof(itag->id));
1795 itag->obj_type = tag->obj_type;
1796 itag->tag_len = tag_len;
1797 itag->tagger_len = tagger_len;
1798 itag->tagger_time = tag->tagger_time;
1799 itag->tagger_gmtoff = tag->tagger_gmtoff;
1800 itag->tagmsg_len = tagmsg_len;
1802 len = sizeof(*itag);
1803 memcpy(buf + len, tag->tag, tag_len);
1805 memcpy(buf + len, tag->tagger, tagger_len);
1808 if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
1809 err = got_error_from_errno("imsg_compose TAG");
1813 if (tagmsg_len == 0 ||
1814 tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1815 err = flush_imsg(ibuf);
1819 err = send_tagmsg(ibuf, tag, tagmsg_len);
1825 const struct got_error *
1826 got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
1828 const struct got_error *err = NULL;
1830 struct got_imsg_tag_object *itag;
1831 size_t len, datalen;
1832 const size_t min_datalen =
1833 MIN(sizeof(struct got_imsg_error),
1834 sizeof(struct got_imsg_tag_object));
1838 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1842 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1845 switch (imsg.hdr.type) {
1847 if (datalen < sizeof(*itag)) {
1848 err = got_error(GOT_ERR_PRIVSEP_LEN);
1852 if (datalen != sizeof(*itag) + itag->tag_len +
1854 err = got_error(GOT_ERR_PRIVSEP_LEN);
1857 len += sizeof(*itag);
1859 *tag = calloc(1, sizeof(**tag));
1861 err = got_error_from_errno("calloc");
1865 memcpy((*tag)->id.hash, itag->id, SHA1_DIGEST_LENGTH);
1867 (*tag)->tag = strndup(imsg.data + len, itag->tag_len);
1868 if ((*tag)->tag == NULL) {
1869 err = got_error_from_errno("strndup");
1872 len += itag->tag_len;
1874 (*tag)->obj_type = itag->obj_type;
1875 (*tag)->tagger_time = itag->tagger_time;
1876 (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1878 (*tag)->tagger = strndup(imsg.data + len, itag->tagger_len);
1879 if ((*tag)->tagger == NULL) {
1880 err = got_error_from_errno("strndup");
1883 len += itag->tagger_len;
1885 if (itag->tagmsg_len == 0) {
1886 (*tag)->tagmsg = strdup("");
1887 if ((*tag)->tagmsg == NULL) {
1888 err = got_error_from_errno("strdup");
1892 size_t offset = 0, remain = itag->tagmsg_len;
1894 (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1895 if ((*tag)->tagmsg == NULL) {
1896 err = got_error_from_errno("malloc");
1899 while (remain > 0) {
1900 struct imsg imsg_log;
1901 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1904 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1908 if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1909 return got_error(GOT_ERR_PRIVSEP_MSG);
1911 memcpy((*tag)->tagmsg + offset, imsg_log.data,
1913 imsg_free(&imsg_log);
1917 (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1922 err = got_error(GOT_ERR_PRIVSEP_MSG);
1931 const struct got_error *
1932 got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1933 struct got_packidx *packidx)
1935 const struct got_error *err = NULL;
1936 struct got_imsg_packidx ipackidx;
1937 struct got_imsg_pack ipack;
1940 memset(&ipackidx, 0, sizeof(ipackidx));
1941 memset(&ipack, 0, sizeof(ipack));
1943 ipackidx.len = packidx->len;
1944 ipackidx.packfile_size = pack->filesize;
1945 ipackidx.algo = packidx->algo;
1946 fd = dup(packidx->fd);
1948 return got_error_from_errno("dup");
1950 if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
1951 sizeof(ipackidx)) == -1) {
1952 err = got_error_from_errno("imsg_compose PACKIDX");
1957 if (strlcpy(ipack.path_packfile, pack->path_packfile,
1958 sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
1959 return got_error(GOT_ERR_NO_SPACE);
1960 ipack.filesize = pack->filesize;
1961 ipack.algo = pack->algo;
1965 return got_error_from_errno("dup");
1967 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
1969 err = got_error_from_errno("imsg_compose PACK");
1974 return flush_imsg(ibuf);
1977 const struct got_error *
1978 got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
1979 struct got_object_id *id)
1981 struct got_imsg_packed_object iobj;
1983 memset(&iobj, 0, sizeof(iobj));
1985 memcpy(&iobj.id, id, sizeof(iobj.id));
1987 if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
1988 &iobj, sizeof(iobj)) == -1)
1989 return got_error_from_errno("imsg_compose "
1990 "PACKED_OBJECT_REQUEST");
1992 return flush_imsg(ibuf);
1995 const struct got_error *
1996 got_privsep_send_packed_raw_obj_req(struct imsgbuf *ibuf, int idx,
1997 struct got_object_id *id)
1999 struct got_imsg_packed_object iobj;
2001 memset(&iobj, 0, sizeof(iobj));
2003 memcpy(&iobj.id, id, sizeof(iobj.id));
2005 if (imsg_compose(ibuf, GOT_IMSG_PACKED_RAW_OBJECT_REQUEST, 0, 0, -1,
2006 &iobj, sizeof(iobj)) == -1)
2007 return got_error_from_errno("imsg_compose "
2008 "PACKED_OBJECT_REQUEST");
2010 return flush_imsg(ibuf);
2013 const struct got_error *
2014 got_privsep_send_gitconfig_parse_req(struct imsgbuf *ibuf, int fd)
2016 const struct got_error *err = NULL;
2018 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_PARSE_REQUEST, 0, 0, fd,
2020 err = got_error_from_errno("imsg_compose "
2021 "GITCONFIG_PARSE_REQUEST");
2026 return flush_imsg(ibuf);
2029 const struct got_error *
2030 got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *ibuf)
2032 if (imsg_compose(ibuf,
2033 GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST, 0, 0, -1,
2035 return got_error_from_errno("imsg_compose "
2036 "GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST");
2038 return flush_imsg(ibuf);
2041 const struct got_error *
2042 got_privsep_send_gitconfig_repository_extensions_req(struct imsgbuf *ibuf)
2044 if (imsg_compose(ibuf,
2045 GOT_IMSG_GITCONFIG_REPOSITORY_EXTENSIONS_REQUEST, 0, 0, -1,
2047 return got_error_from_errno("imsg_compose "
2048 "GITCONFIG_REPOSITORY_EXTENSIONS_REQUEST");
2050 return flush_imsg(ibuf);
2054 const struct got_error *
2055 got_privsep_send_gitconfig_author_name_req(struct imsgbuf *ibuf)
2057 if (imsg_compose(ibuf,
2058 GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST, 0, 0, -1, NULL, 0) == -1)
2059 return got_error_from_errno("imsg_compose "
2060 "GITCONFIG_AUTHOR_NAME_REQUEST");
2062 return flush_imsg(ibuf);
2065 const struct got_error *
2066 got_privsep_send_gitconfig_author_email_req(struct imsgbuf *ibuf)
2068 if (imsg_compose(ibuf,
2069 GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST, 0, 0, -1, NULL, 0) == -1)
2070 return got_error_from_errno("imsg_compose "
2071 "GITCONFIG_AUTHOR_EMAIL_REQUEST");
2073 return flush_imsg(ibuf);
2076 const struct got_error *
2077 got_privsep_send_gitconfig_remotes_req(struct imsgbuf *ibuf)
2079 if (imsg_compose(ibuf,
2080 GOT_IMSG_GITCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
2081 return got_error_from_errno("imsg_compose "
2082 "GITCONFIG_REMOTE_REQUEST");
2084 return flush_imsg(ibuf);
2087 const struct got_error *
2088 got_privsep_send_gitconfig_owner_req(struct imsgbuf *ibuf)
2090 if (imsg_compose(ibuf,
2091 GOT_IMSG_GITCONFIG_OWNER_REQUEST, 0, 0, -1, NULL, 0) == -1)
2092 return got_error_from_errno("imsg_compose "
2093 "GITCONFIG_OWNER_REQUEST");
2095 return flush_imsg(ibuf);
2098 const struct got_error *
2099 got_privsep_recv_gitconfig_str(char **str, struct imsgbuf *ibuf)
2101 const struct got_error *err = NULL;
2107 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2110 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2112 switch (imsg.hdr.type) {
2113 case GOT_IMSG_GITCONFIG_STR_VAL:
2116 *str = strndup(imsg.data, datalen);
2118 err = got_error_from_errno("strndup");
2123 err = got_error(GOT_ERR_PRIVSEP_MSG);
2131 const struct got_error *
2132 got_privsep_recv_gitconfig_pair(char **key, char **val, struct imsgbuf *ibuf)
2134 const struct got_error *err = NULL;
2135 struct got_imsg_gitconfig_pair p;
2142 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2147 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2149 if (imsg.hdr.type != GOT_IMSG_GITCONFIG_PAIR) {
2150 err = got_error(GOT_ERR_PRIVSEP_MSG);
2154 if (datalen < sizeof(p)) {
2155 err = got_error(GOT_ERR_PRIVSEP_LEN);
2159 memcpy(&p, data, sizeof(p));
2162 if (datalen != sizeof(p) + p.klen + p.vlen) {
2163 err = got_error(GOT_ERR_PRIVSEP_LEN);
2167 *key = strndup(data, p.klen);
2169 err = got_error_from_errno("strndup");
2174 *val = strndup(data, p.vlen);
2176 err = got_error_from_errno("strndup");
2185 const struct got_error *
2186 got_privsep_recv_gitconfig_int(int *val, struct imsgbuf *ibuf)
2188 const struct got_error *err = NULL;
2191 const size_t min_datalen =
2192 MIN(sizeof(struct got_imsg_error), sizeof(int));
2196 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2199 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2201 switch (imsg.hdr.type) {
2202 case GOT_IMSG_GITCONFIG_INT_VAL:
2203 if (datalen != sizeof(*val)) {
2204 err = got_error(GOT_ERR_PRIVSEP_LEN);
2207 memcpy(val, imsg.data, sizeof(*val));
2210 err = got_error(GOT_ERR_PRIVSEP_MSG);
2219 free_remote_data(struct got_remote_repo *remote)
2224 free(remote->fetch_url);
2225 free(remote->send_url);
2226 for (i = 0; i < remote->nfetch_branches; i++)
2227 free(remote->fetch_branches[i]);
2228 free(remote->fetch_branches);
2229 for (i = 0; i < remote->nsend_branches; i++)
2230 free(remote->send_branches[i]);
2231 free(remote->send_branches);
2232 for (i = 0; i < remote->nfetch_refs; i++)
2233 free(remote->fetch_refs[i]);
2234 free(remote->fetch_refs);
2237 const struct got_error *
2238 got_privsep_recv_gitconfig_remotes(struct got_remote_repo **remotes,
2239 int *nremotes, struct imsgbuf *ibuf)
2241 const struct got_error *err = NULL;
2244 struct got_imsg_remotes iremotes;
2245 struct got_imsg_remote iremote;
2249 iremotes.nremotes = 0;
2251 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremotes));
2254 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2256 switch (imsg.hdr.type) {
2257 case GOT_IMSG_GITCONFIG_REMOTES:
2258 if (datalen != sizeof(iremotes)) {
2259 err = got_error(GOT_ERR_PRIVSEP_LEN);
2262 memcpy(&iremotes, imsg.data, sizeof(iremotes));
2263 if (iremotes.nremotes == 0) {
2270 return got_error(GOT_ERR_PRIVSEP_MSG);
2275 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(**remotes));
2276 if (*remotes == NULL)
2277 return got_error_from_errno("recallocarray");
2279 while (*nremotes < iremotes.nremotes) {
2280 struct got_remote_repo *remote;
2282 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremote));
2285 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2287 switch (imsg.hdr.type) {
2288 case GOT_IMSG_GITCONFIG_REMOTE:
2289 remote = &(*remotes)[*nremotes];
2290 memset(remote, 0, sizeof(*remote));
2291 if (datalen < sizeof(iremote)) {
2292 err = got_error(GOT_ERR_PRIVSEP_LEN);
2295 memcpy(&iremote, imsg.data, sizeof(iremote));
2296 if (iremote.name_len == 0 ||
2297 iremote.fetch_url_len == 0 ||
2298 iremote.send_url_len == 0 ||
2299 (sizeof(iremote) + iremote.name_len +
2300 iremote.fetch_url_len + iremote.send_url_len) > datalen) {
2301 err = got_error(GOT_ERR_PRIVSEP_LEN);
2304 remote->name = strndup(imsg.data + sizeof(iremote),
2306 if (remote->name == NULL) {
2307 err = got_error_from_errno("strndup");
2310 remote->fetch_url = strndup(imsg.data + sizeof(iremote) +
2311 iremote.name_len, iremote.fetch_url_len);
2312 if (remote->fetch_url == NULL) {
2313 err = got_error_from_errno("strndup");
2314 free_remote_data(remote);
2317 remote->send_url = strndup(imsg.data + sizeof(iremote) +
2318 iremote.name_len + iremote.fetch_url_len,
2319 iremote.send_url_len);
2320 if (remote->send_url == NULL) {
2321 err = got_error_from_errno("strndup");
2322 free_remote_data(remote);
2325 remote->mirror_references = iremote.mirror_references;
2326 remote->fetch_all_branches = iremote.fetch_all_branches;
2327 remote->nfetch_branches = 0;
2328 remote->fetch_branches = NULL;
2329 remote->nsend_branches = 0;
2330 remote->send_branches = NULL;
2331 remote->nfetch_refs = 0;
2332 remote->fetch_refs = NULL;
2336 err = got_error(GOT_ERR_PRIVSEP_MSG);
2347 for (i = 0; i < *nremotes; i++)
2348 free_remote_data(&(*remotes)[i]);
2356 const struct got_error *
2357 got_privsep_send_gotconfig_parse_req(struct imsgbuf *ibuf, int fd)
2359 const struct got_error *err = NULL;
2361 if (imsg_compose(ibuf, GOT_IMSG_GOTCONFIG_PARSE_REQUEST, 0, 0, fd,
2363 err = got_error_from_errno("imsg_compose "
2364 "GOTCONFIG_PARSE_REQUEST");
2369 return flush_imsg(ibuf);
2372 const struct got_error *
2373 got_privsep_send_gotconfig_author_req(struct imsgbuf *ibuf)
2375 if (imsg_compose(ibuf,
2376 GOT_IMSG_GOTCONFIG_AUTHOR_REQUEST, 0, 0, -1, NULL, 0) == -1)
2377 return got_error_from_errno("imsg_compose "
2378 "GOTCONFIG_AUTHOR_REQUEST");
2380 return flush_imsg(ibuf);
2383 const struct got_error *
2384 got_privsep_send_gotconfig_allowed_signers_req(struct imsgbuf *ibuf)
2386 if (imsg_compose(ibuf,
2387 GOT_IMSG_GOTCONFIG_ALLOWEDSIGNERS_REQUEST, 0, 0, -1, NULL, 0) == -1)
2388 return got_error_from_errno("imsg_compose "
2389 "GOTCONFIG_ALLOWEDSIGNERS_REQUEST");
2391 return flush_imsg(ibuf);
2394 const struct got_error *
2395 got_privsep_send_gotconfig_revoked_signers_req(struct imsgbuf *ibuf)
2397 if (imsg_compose(ibuf,
2398 GOT_IMSG_GOTCONFIG_REVOKEDSIGNERS_REQUEST, 0, 0, -1, NULL, 0) == -1)
2399 return got_error_from_errno("imsg_compose "
2400 "GOTCONFIG_REVOKEDSIGNERS_REQUEST");
2402 return flush_imsg(ibuf);
2405 const struct got_error *
2406 got_privsep_send_gotconfig_signer_id_req(struct imsgbuf *ibuf)
2408 if (imsg_compose(ibuf,
2409 GOT_IMSG_GOTCONFIG_SIGNERID_REQUEST, 0, 0, -1, NULL, 0) == -1)
2410 return got_error_from_errno("imsg_compose "
2411 "GOTCONFIG_SIGNERID_REQUEST");
2413 return flush_imsg(ibuf);
2416 const struct got_error *
2417 got_privsep_send_gotconfig_remotes_req(struct imsgbuf *ibuf)
2419 if (imsg_compose(ibuf,
2420 GOT_IMSG_GOTCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
2421 return got_error_from_errno("imsg_compose "
2422 "GOTCONFIG_REMOTE_REQUEST");
2424 return flush_imsg(ibuf);
2427 const struct got_error *
2428 got_privsep_recv_gotconfig_str(char **str, struct imsgbuf *ibuf)
2430 const struct got_error *err = NULL;
2436 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2439 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2441 switch (imsg.hdr.type) {
2442 case GOT_IMSG_GOTCONFIG_STR_VAL:
2445 *str = strndup(imsg.data, datalen);
2447 err = got_error_from_errno("strndup");
2452 err = got_error(GOT_ERR_PRIVSEP_MSG);
2460 const struct got_error *
2461 got_privsep_recv_gotconfig_remotes(struct got_remote_repo **remotes,
2462 int *nremotes, struct imsgbuf *ibuf)
2464 const struct got_error *err = NULL;
2467 struct got_imsg_remotes iremotes;
2468 struct got_imsg_remote iremote;
2469 const size_t min_datalen =
2470 MIN(sizeof(struct got_imsg_error), sizeof(iremotes));
2474 iremotes.nremotes = 0;
2476 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2479 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2481 switch (imsg.hdr.type) {
2482 case GOT_IMSG_GOTCONFIG_REMOTES:
2483 if (datalen != sizeof(iremotes)) {
2484 err = got_error(GOT_ERR_PRIVSEP_LEN);
2487 memcpy(&iremotes, imsg.data, sizeof(iremotes));
2488 if (iremotes.nremotes < 0) {
2489 err = got_error(GOT_ERR_PRIVSEP_LEN);
2492 if (iremotes.nremotes == 0) {
2499 return got_error(GOT_ERR_PRIVSEP_MSG);
2504 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(**remotes));
2505 if (*remotes == NULL)
2506 return got_error_from_errno("recallocarray");
2508 while (*nremotes < iremotes.nremotes) {
2509 struct got_remote_repo *remote;
2510 const size_t min_datalen =
2511 MIN(sizeof(struct got_imsg_error), sizeof(iremote));
2514 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2517 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2519 switch (imsg.hdr.type) {
2520 case GOT_IMSG_GOTCONFIG_REMOTE:
2521 remote = &(*remotes)[*nremotes];
2522 memset(remote, 0, sizeof(*remote));
2523 if (datalen < sizeof(iremote)) {
2524 err = got_error(GOT_ERR_PRIVSEP_LEN);
2527 memcpy(&iremote, imsg.data, sizeof(iremote));
2528 if (iremote.name_len == 0 ||
2529 (iremote.fetch_url_len == 0 &&
2530 iremote.send_url_len == 0) ||
2531 (sizeof(iremote) + iremote.name_len +
2532 iremote.fetch_url_len + iremote.send_url_len) >
2534 err = got_error(GOT_ERR_PRIVSEP_LEN);
2537 remote->name = strndup(imsg.data + sizeof(iremote),
2539 if (remote->name == NULL) {
2540 err = got_error_from_errno("strndup");
2543 remote->fetch_url = strndup(imsg.data +
2544 sizeof(iremote) + iremote.name_len,
2545 iremote.fetch_url_len);
2546 if (remote->fetch_url == NULL) {
2547 err = got_error_from_errno("strndup");
2548 free_remote_data(remote);
2551 remote->send_url = strndup(imsg.data +
2552 sizeof(iremote) + iremote.name_len +
2553 iremote.fetch_url_len, iremote.send_url_len);
2554 if (remote->send_url == NULL) {
2555 err = got_error_from_errno("strndup");
2556 free_remote_data(remote);
2559 remote->mirror_references = iremote.mirror_references;
2560 remote->fetch_all_branches = iremote.fetch_all_branches;
2561 if (iremote.nfetch_branches > 0) {
2562 remote->fetch_branches = recallocarray(NULL, 0,
2563 iremote.nfetch_branches, sizeof(char *));
2564 if (remote->fetch_branches == NULL) {
2565 err = got_error_from_errno("calloc");
2566 free_remote_data(remote);
2570 remote->nfetch_branches = 0;
2571 for (i = 0; i < iremote.nfetch_branches; i++) {
2573 err = got_privsep_recv_gotconfig_str(&branch,
2576 free_remote_data(remote);
2579 remote->fetch_branches[i] = branch;
2580 remote->nfetch_branches++;
2582 if (iremote.nsend_branches > 0) {
2583 remote->send_branches = recallocarray(NULL, 0,
2584 iremote.nsend_branches, sizeof(char *));
2585 if (remote->send_branches == NULL) {
2586 err = got_error_from_errno("calloc");
2587 free_remote_data(remote);
2591 remote->nsend_branches = 0;
2592 for (i = 0; i < iremote.nsend_branches; i++) {
2594 err = got_privsep_recv_gotconfig_str(&branch,
2597 free_remote_data(remote);
2600 remote->send_branches[i] = branch;
2601 remote->nsend_branches++;
2603 if (iremote.nfetch_refs > 0) {
2604 remote->fetch_refs = recallocarray(NULL, 0,
2605 iremote.nfetch_refs, sizeof(char *));
2606 if (remote->fetch_refs == NULL) {
2607 err = got_error_from_errno("calloc");
2608 free_remote_data(remote);
2612 remote->nfetch_refs = 0;
2613 for (i = 0; i < iremote.nfetch_refs; i++) {
2615 err = got_privsep_recv_gotconfig_str(&ref,
2618 free_remote_data(remote);
2621 remote->fetch_refs[i] = ref;
2622 remote->nfetch_refs++;
2627 err = got_error(GOT_ERR_PRIVSEP_MSG);
2638 for (i = 0; i < *nremotes; i++)
2639 free_remote_data(&(*remotes)[i]);
2647 const struct got_error *
2648 got_privsep_send_commit_traversal_request(struct imsgbuf *ibuf,
2649 struct got_object_id *id, int idx, const char *path)
2652 size_t path_len = strlen(path) + 1;
2654 wbuf = imsg_create(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_REQUEST, 0, 0,
2655 sizeof(struct got_imsg_commit_traversal_request) + path_len);
2657 return got_error_from_errno(
2658 "imsg_create COMMIT_TRAVERSAL_REQUEST");
2659 if (imsg_add(wbuf, id, sizeof(*id)) == -1)
2660 return got_error_from_errno("imsg_add "
2661 "COMMIT_TRAVERSAL_REQUEST");
2662 if (imsg_add(wbuf, &idx, sizeof(idx)) == -1)
2663 return got_error_from_errno("imsg_add "
2664 "COMMIT_TRAVERSAL_REQUEST");
2665 if (imsg_add(wbuf, path, path_len) == -1)
2666 return got_error_from_errno("imsg_add "
2667 "COMMIT_TRAVERSAL_REQUEST");
2670 imsg_close(ibuf, wbuf);
2672 return flush_imsg(ibuf);
2675 const struct got_error *
2676 got_privsep_recv_traversed_commits(struct got_commit_object **changed_commit,
2677 struct got_object_id **changed_commit_id,
2678 struct got_object_id_queue *commit_ids, struct imsgbuf *ibuf)
2680 const struct got_error *err = NULL;
2682 struct got_imsg_traversed_commits *icommits;
2686 *changed_commit = NULL;
2687 *changed_commit_id = NULL;
2690 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2694 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2695 switch (imsg.hdr.type) {
2696 case GOT_IMSG_TRAVERSED_COMMITS:
2697 icommits = imsg.data;
2698 if (datalen != sizeof(*icommits) +
2699 icommits->ncommits * SHA1_DIGEST_LENGTH) {
2700 err = got_error(GOT_ERR_PRIVSEP_LEN);
2703 for (i = 0; i < icommits->ncommits; i++) {
2704 struct got_object_qid *qid;
2705 uint8_t *sha1 = (uint8_t *)imsg.data +
2706 sizeof(*icommits) + i * SHA1_DIGEST_LENGTH;
2707 err = got_object_qid_alloc_partial(&qid);
2710 memcpy(qid->id.hash, sha1, SHA1_DIGEST_LENGTH);
2711 STAILQ_INSERT_TAIL(commit_ids, qid, entry);
2713 /* The last commit may contain a change. */
2714 if (i == icommits->ncommits - 1) {
2715 *changed_commit_id =
2716 got_object_id_dup(&qid->id);
2717 if (*changed_commit_id == NULL) {
2718 err = got_error_from_errno(
2719 "got_object_id_dup");
2725 case GOT_IMSG_COMMIT:
2726 if (*changed_commit_id == NULL) {
2727 err = got_error(GOT_ERR_PRIVSEP_MSG);
2730 err = get_commit_from_imsg(changed_commit, &imsg,
2733 case GOT_IMSG_COMMIT_TRAVERSAL_DONE:
2737 err = got_error(GOT_ERR_PRIVSEP_MSG);
2747 got_object_id_queue_free(commit_ids);
2751 const struct got_error *
2752 got_privsep_send_enumerated_tree(size_t *totlen, struct imsgbuf *ibuf,
2753 struct got_object_id *tree_id, const char *path,
2754 struct got_parsed_tree_entry *entries, int nentries)
2756 const struct got_error *err = NULL;
2758 size_t path_len = strlen(path);
2761 msglen = sizeof(struct got_imsg_enumerated_tree) + path_len;
2762 wbuf = imsg_create(ibuf, GOT_IMSG_ENUMERATED_TREE, 0, 0, msglen);
2764 return got_error_from_errno("imsg_create ENUMERATED_TREE");
2766 if (imsg_add(wbuf, tree_id->hash, SHA1_DIGEST_LENGTH) == -1)
2767 return got_error_from_errno("imsg_add ENUMERATED_TREE");
2768 if (imsg_add(wbuf, &nentries, sizeof(nentries)) == -1)
2769 return got_error_from_errno("imsg_add ENUMERATED_TREE");
2770 if (imsg_add(wbuf, path, path_len) == -1)
2771 return got_error_from_errno("imsg_add ENUMERATED_TREE");
2774 imsg_close(ibuf, wbuf);
2777 err = send_tree_entries(ibuf, entries, nentries);
2782 return flush_imsg(ibuf);
2785 const struct got_error *
2786 got_privsep_send_object_enumeration_request(struct imsgbuf *ibuf)
2788 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_ENUMERATION_REQUEST,
2789 0, 0, -1, NULL, 0) == -1)
2790 return got_error_from_errno("imsg_compose "
2791 "OBJECT_ENUMERATION_REQUEST");
2793 return flush_imsg(ibuf);
2796 const struct got_error *
2797 got_privsep_send_object_enumeration_done(struct imsgbuf *ibuf)
2799 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_ENUMERATION_DONE,
2800 0, 0, -1, NULL, 0) == -1)
2801 return got_error_from_errno("imsg_compose "
2802 "OBJECT_ENUMERATION_DONE");
2804 return flush_imsg(ibuf);
2807 const struct got_error *
2808 got_privsep_send_object_enumeration_incomplete(struct imsgbuf *ibuf)
2810 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_ENUMERATION_INCOMPLETE,
2811 0, 0, -1, NULL, 0) == -1)
2812 return got_error_from_errno("imsg_compose "
2813 "OBJECT_ENUMERATION_INCOMPLETE");
2815 return flush_imsg(ibuf);
2818 const struct got_error *
2819 got_privsep_send_enumerated_commit(struct imsgbuf *ibuf,
2820 struct got_object_id *id, time_t mtime)
2824 wbuf = imsg_create(ibuf, GOT_IMSG_ENUMERATED_COMMIT, 0, 0,
2825 sizeof(struct got_imsg_enumerated_commit) + SHA1_DIGEST_LENGTH);
2827 return got_error_from_errno("imsg_create ENUMERATED_COMMIT");
2829 /* Keep in sync with struct got_imsg_enumerated_commit! */
2830 if (imsg_add(wbuf, id, SHA1_DIGEST_LENGTH) == -1)
2831 return got_error_from_errno("imsg_add ENUMERATED_COMMIT");
2832 if (imsg_add(wbuf, &mtime, sizeof(mtime)) == -1)
2833 return got_error_from_errno("imsg_add ENUMERATED_COMMIT");
2836 imsg_close(ibuf, wbuf);
2837 /* Don't flush yet, tree entries or ENUMERATION_DONE will follow. */
2841 const struct got_error *
2842 got_privsep_recv_enumerated_objects(int *found_all_objects,
2843 struct imsgbuf *ibuf,
2844 got_object_enumerate_commit_cb cb_commit,
2845 got_object_enumerate_tree_cb cb_tree, void *cb_arg,
2846 struct got_repository *repo)
2848 const struct got_error *err = NULL;
2850 struct got_imsg_enumerated_commit *icommit = NULL;
2851 struct got_object_id commit_id;
2852 int have_commit = 0;
2854 struct got_tree_object tree;
2855 struct got_imsg_enumerated_tree *itree;
2856 struct got_object_id tree_id;
2857 char *path = NULL, *canon_path = NULL;
2858 size_t datalen, path_len;
2862 *found_all_objects = 0;
2863 memset(&tree, 0, sizeof(tree));
2866 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2870 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2871 switch (imsg.hdr.type) {
2872 case GOT_IMSG_ENUMERATED_COMMIT:
2873 if (have_commit && nentries != -1) {
2874 err = got_error(GOT_ERR_PRIVSEP_MSG);
2877 if (datalen != sizeof(*icommit)) {
2878 err = got_error(GOT_ERR_PRIVSEP_LEN);
2881 icommit = (struct got_imsg_enumerated_commit *)imsg.data;
2882 memset(&commit_id, 0, sizeof(commit_id));
2883 memcpy(commit_id.hash, icommit->id, SHA1_DIGEST_LENGTH);
2884 mtime = icommit->mtime;
2887 case GOT_IMSG_ENUMERATED_TREE:
2888 /* Should be preceeded by GOT_IMSG_ENUMERATED_COMMIT. */
2890 err = got_error(GOT_ERR_PRIVSEP_MSG);
2893 if (datalen < sizeof(*itree)) {
2894 err = got_error(GOT_ERR_PRIVSEP_LEN);
2898 path_len = datalen - sizeof(*itree);
2899 if (path_len == 0) {
2900 err = got_error(GOT_ERR_PRIVSEP_LEN);
2903 memset(&tree_id, 0, sizeof(tree_id));
2904 memcpy(tree_id.hash, itree->id, SHA1_DIGEST_LENGTH);
2906 path = strndup(imsg.data + sizeof(*itree), path_len);
2908 err = got_error_from_errno("strndup");
2912 canon_path = malloc(path_len + 1);
2913 if (canon_path == NULL) {
2914 err = got_error_from_errno("malloc");
2917 if (!got_path_is_absolute(path)) {
2918 err = got_error(GOT_ERR_BAD_PATH);
2921 if (got_path_is_root_dir(path)) {
2922 /* XXX check what got_canonpath() does wrong */
2923 canon_path[0] = '/';
2924 canon_path[1] = '\0';
2926 err = got_canonpath(path, canon_path,
2931 if (strcmp(path, canon_path) != 0) {
2932 err = got_error(GOT_ERR_BAD_PATH);
2935 if (nentries != -1) {
2936 err = got_error(GOT_ERR_PRIVSEP_MSG);
2939 if (itree->nentries < -1) {
2940 err = got_error(GOT_ERR_PRIVSEP_MSG);
2943 if (itree->nentries == -1) {
2944 /* Tree was not found in pack file. */
2945 err = cb_tree(cb_arg, NULL, mtime, &tree_id,
2949 if (itree->nentries > INT_MAX) {
2950 err = got_error(GOT_ERR_PRIVSEP_LEN);
2953 tree.entries = calloc(itree->nentries,
2954 sizeof(struct got_tree_entry));
2955 if (tree.entries == NULL) {
2956 err = got_error_from_errno("calloc");
2959 if (itree->nentries == 0) {
2960 err = cb_tree(cb_arg, &tree, mtime, &tree_id,
2965 /* Prepare for next tree. */
2967 memset(&tree, 0, sizeof(tree));
2970 tree.nentries = itree->nentries;
2974 case GOT_IMSG_TREE_ENTRIES:
2975 /* Should be preceeded by GOT_IMSG_ENUMERATED_TREE. */
2976 if (nentries <= -1) {
2977 err = got_error(GOT_ERR_PRIVSEP_MSG);
2980 err = recv_tree_entries(imsg.data, datalen,
2984 if (tree.nentries == nentries) {
2985 err = cb_tree(cb_arg, &tree, mtime, &tree_id,
2990 /* Prepare for next tree. */
2992 memset(&tree, 0, sizeof(tree));
2996 case GOT_IMSG_TREE_ENUMERATION_DONE:
2997 /* All trees have been found and traversed. */
2998 if (!have_commit || path == NULL || nentries != -1) {
2999 err = got_error(GOT_ERR_PRIVSEP_MSG);
3002 err = cb_commit(cb_arg, mtime, &commit_id, repo);
3007 case GOT_IMSG_OBJECT_ENUMERATION_DONE:
3008 *found_all_objects = 1;
3011 case GOT_IMSG_OBJECT_ENUMERATION_INCOMPLETE:
3015 err = got_error(GOT_ERR_PRIVSEP_MSG);
3030 const struct got_error *
3031 got_privsep_send_raw_delta_req(struct imsgbuf *ibuf, int idx,
3032 struct got_object_id *id)
3034 struct got_imsg_raw_delta_request dreq;
3036 memset(&dreq, 0, sizeof(dreq));
3038 memcpy(&dreq.id, id, sizeof(dreq.id));
3040 if (imsg_compose(ibuf, GOT_IMSG_RAW_DELTA_REQUEST, 0, 0, -1,
3041 &dreq, sizeof(dreq)) == -1)
3042 return got_error_from_errno("imsg_compose RAW_DELTA_REQUEST");
3044 return flush_imsg(ibuf);
3047 const struct got_error *
3048 got_privsep_send_raw_delta_outfd(struct imsgbuf *ibuf, int fd)
3050 return send_fd(ibuf, GOT_IMSG_RAW_DELTA_OUTFD, fd);
3053 const struct got_error *
3054 got_privsep_send_raw_delta(struct imsgbuf *ibuf, uint64_t base_size,
3055 uint64_t result_size, off_t delta_size, off_t delta_compressed_size,
3056 off_t delta_offset, off_t delta_out_offset, struct got_object_id *base_id)
3058 struct got_imsg_raw_delta idelta;
3061 memset(&idelta, 0, sizeof(idelta));
3062 idelta.base_size = base_size;
3063 idelta.result_size = result_size;
3064 idelta.delta_size = delta_size;
3065 idelta.delta_compressed_size = delta_compressed_size;
3066 idelta.delta_offset = delta_offset;
3067 idelta.delta_out_offset = delta_out_offset;
3068 memcpy(&idelta.base_id, &base_id, sizeof(idelta.base_id));
3070 ret = imsg_compose(ibuf, GOT_IMSG_RAW_DELTA, 0, 0, -1,
3071 &idelta, sizeof(idelta));
3073 return got_error_from_errno("imsg_compose RAW_DELTA");
3075 return flush_imsg(ibuf);
3078 const struct got_error *
3079 got_privsep_recv_raw_delta(uint64_t *base_size, uint64_t *result_size,
3080 off_t *delta_size, off_t *delta_compressed_size, off_t *delta_offset,
3081 off_t *delta_out_offset, struct got_object_id **base_id,
3082 struct imsgbuf *ibuf)
3084 const struct got_error *err = NULL;
3086 struct got_imsg_raw_delta *delta;
3092 *delta_compressed_size = 0;
3094 *delta_out_offset = 0;
3097 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
3101 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
3103 switch (imsg.hdr.type) {
3104 case GOT_IMSG_RAW_DELTA:
3105 if (datalen != sizeof(*delta)) {
3106 err = got_error(GOT_ERR_PRIVSEP_LEN);
3110 *base_size = delta->base_size;
3111 *result_size = delta->result_size;
3112 *delta_size = delta->delta_size;
3113 *delta_compressed_size = delta->delta_compressed_size;
3114 *delta_offset = delta->delta_offset;
3115 *delta_out_offset = delta->delta_out_offset;
3116 *base_id = calloc(1, sizeof(**base_id));
3117 if (*base_id == NULL) {
3118 err = got_error_from_errno("malloc");
3121 memcpy(*base_id, &delta->base_id, sizeof(**base_id));
3124 err = got_error(GOT_ERR_PRIVSEP_MSG);
3137 static const struct got_error *
3138 send_idlist(struct imsgbuf *ibuf, struct got_object_id **ids, size_t nids)
3140 const struct got_error *err = NULL;
3141 struct got_imsg_object_idlist idlist;
3145 memset(&idlist, 0, sizeof(idlist));
3147 if (nids > GOT_IMSG_OBJ_ID_LIST_MAX_NIDS)
3148 return got_error(GOT_ERR_NO_SPACE);
3150 wbuf = imsg_create(ibuf, GOT_IMSG_OBJ_ID_LIST, 0, 0,
3151 sizeof(idlist) + nids * sizeof(**ids));
3153 err = got_error_from_errno("imsg_create OBJ_ID_LIST");
3158 if (imsg_add(wbuf, &idlist, sizeof(idlist)) == -1)
3159 return got_error_from_errno("imsg_add OBJ_ID_LIST");
3161 for (i = 0; i < nids; i++) {
3162 struct got_object_id *id = ids[i];
3163 if (imsg_add(wbuf, id, sizeof(*id)) == -1)
3164 return got_error_from_errno("imsg_add OBJ_ID_LIST");
3168 imsg_close(ibuf, wbuf);
3170 return flush_imsg(ibuf);
3173 const struct got_error *
3174 got_privsep_send_object_idlist(struct imsgbuf *ibuf,
3175 struct got_object_id **ids, size_t nids)
3177 const struct got_error *err = NULL;
3178 struct got_object_id *idlist[GOT_IMSG_OBJ_ID_LIST_MAX_NIDS];
3181 for (i = 0; i < nids; i++) {
3182 idlist[i % nitems(idlist)] = ids[i];
3184 if (queued >= nitems(idlist)) {
3185 err = send_idlist(ibuf, idlist, queued);
3193 err = send_idlist(ibuf, idlist, queued);
3201 const struct got_error *
3202 got_privsep_send_object_idlist_done(struct imsgbuf *ibuf)
3204 if (imsg_compose(ibuf, GOT_IMSG_OBJ_ID_LIST_DONE, 0, 0, -1, NULL, 0)
3206 return got_error_from_errno("imsg_compose OBJ_ID_LIST_DONE");
3208 return flush_imsg(ibuf);
3211 const struct got_error *
3212 got_privsep_recv_object_idlist(int *done, struct got_object_id **ids,
3213 size_t *nids, struct imsgbuf *ibuf)
3215 const struct got_error *err = NULL;
3217 struct got_imsg_object_idlist *idlist;
3224 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
3228 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
3229 switch (imsg.hdr.type) {
3230 case GOT_IMSG_OBJ_ID_LIST:
3231 if (datalen < sizeof(*idlist)) {
3232 err = got_error(GOT_ERR_PRIVSEP_LEN);
3236 if (idlist->nids > GOT_IMSG_OBJ_ID_LIST_MAX_NIDS ||
3237 idlist->nids * sizeof(**ids) > datalen - sizeof(*idlist)) {
3238 err = got_error(GOT_ERR_PRIVSEP_LEN);
3241 *nids = idlist->nids;
3242 *ids = calloc(*nids, sizeof(**ids));
3244 err = got_error_from_errno("calloc");
3247 memcpy(*ids, (uint8_t *)imsg.data + sizeof(*idlist),
3248 *nids * sizeof(**ids));
3250 case GOT_IMSG_OBJ_ID_LIST_DONE:
3254 err = got_error(GOT_ERR_PRIVSEP_MSG);
3263 const struct got_error *
3264 got_privsep_send_delta_reuse_req(struct imsgbuf *ibuf)
3266 if (imsg_compose(ibuf, GOT_IMSG_DELTA_REUSE_REQUEST, 0, 0, -1, NULL, 0)
3268 return got_error_from_errno("imsg_compose DELTA_REUSE_REQUEST");
3270 return flush_imsg(ibuf);
3273 const struct got_error *
3274 got_privsep_send_reused_deltas(struct imsgbuf *ibuf,
3275 struct got_imsg_reused_delta *deltas, size_t ndeltas)
3277 const struct got_error *err = NULL;
3279 struct got_imsg_reused_deltas ideltas;
3282 memset(&ideltas, 0, sizeof(ideltas));
3284 if (ndeltas > GOT_IMSG_REUSED_DELTAS_MAX_NDELTAS)
3285 return got_error(GOT_ERR_NO_SPACE);
3287 wbuf = imsg_create(ibuf, GOT_IMSG_REUSED_DELTAS, 0, 0,
3288 sizeof(ideltas) + ndeltas * sizeof(*deltas));
3290 err = got_error_from_errno("imsg_create REUSED_DELTAS");
3294 ideltas.ndeltas = ndeltas;
3295 if (imsg_add(wbuf, &ideltas, sizeof(ideltas)) == -1)
3296 return got_error_from_errno("imsg_add REUSED_DELTAS");
3298 for (i = 0; i < ndeltas; i++) {
3299 struct got_imsg_reused_delta *delta = &deltas[i];
3300 if (imsg_add(wbuf, delta, sizeof(*delta)) == -1)
3301 return got_error_from_errno("imsg_add REUSED_DELTAS");
3305 imsg_close(ibuf, wbuf);
3307 return flush_imsg(ibuf);
3310 const struct got_error *
3311 got_privsep_send_reused_deltas_done(struct imsgbuf *ibuf)
3313 if (imsg_compose(ibuf, GOT_IMSG_DELTA_REUSE_DONE, 0, 0, -1, NULL, 0)
3315 return got_error_from_errno("imsg_compose DELTA_REUSE_DONE");
3317 return flush_imsg(ibuf);
3320 const struct got_error *
3321 got_privsep_recv_reused_deltas(int *done, struct got_imsg_reused_delta *deltas,
3322 size_t *ndeltas, struct imsgbuf *ibuf)
3324 const struct got_error *err = NULL;
3326 struct got_imsg_reused_deltas *ideltas;
3332 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
3336 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
3337 switch (imsg.hdr.type) {
3338 case GOT_IMSG_REUSED_DELTAS:
3339 if (datalen < sizeof(*ideltas)) {
3340 err = got_error(GOT_ERR_PRIVSEP_LEN);
3343 ideltas = imsg.data;
3344 if (ideltas->ndeltas > GOT_IMSG_OBJ_ID_LIST_MAX_NIDS ||
3345 ideltas->ndeltas * sizeof(*deltas) >
3346 datalen - sizeof(*ideltas)) {
3347 err = got_error(GOT_ERR_PRIVSEP_LEN);
3350 *ndeltas = ideltas->ndeltas;
3351 memcpy(deltas, (uint8_t *)imsg.data + sizeof(*ideltas),
3352 *ndeltas * sizeof(*deltas));
3354 case GOT_IMSG_DELTA_REUSE_DONE:
3358 err = got_error(GOT_ERR_PRIVSEP_MSG);
3367 const struct got_error *
3368 got_privsep_init_commit_painting(struct imsgbuf *ibuf)
3370 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_PAINTING_INIT,
3373 return got_error_from_errno("imsg_compose "
3374 "COMMIT_PAINTING_INIT");
3376 return flush_imsg(ibuf);
3379 const struct got_error *
3380 got_privsep_send_painting_request(struct imsgbuf *ibuf, int idx,
3381 struct got_object_id *id, intptr_t color)
3383 struct got_imsg_commit_painting_request ireq;
3385 memset(&ireq, 0, sizeof(ireq));
3386 memcpy(ireq.id, id->hash, sizeof(ireq.id));
3390 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_PAINTING_REQUEST, 0, 0, -1,
3391 &ireq, sizeof(ireq)) == -1)
3392 return got_error_from_errno("imsg_compose "
3393 "COMMIT_PAINTING_REQUEST");
3395 return flush_imsg(ibuf);
3398 static const struct got_error *
3399 send_painted_commits(struct got_object_id_queue *ids, int *nids,
3400 size_t remain, int present_in_pack, struct imsgbuf *ibuf)
3402 const struct got_error *err = NULL;
3403 struct ibuf *wbuf = NULL;
3404 struct got_object_qid *qid;
3409 msglen = MIN(remain, MAX_IMSGSIZE - IMSG_HEADER_SIZE);
3410 ncommits = (msglen - sizeof(struct got_imsg_painted_commits)) /
3411 sizeof(struct got_imsg_painted_commit);
3413 wbuf = imsg_create(ibuf, GOT_IMSG_PAINTED_COMMITS, 0, 0, msglen);
3415 err = got_error_from_errno("imsg_create PAINTED_COMMITS");
3419 /* Keep in sync with struct got_imsg_painted_commits! */
3420 if (imsg_add(wbuf, &ncommits, sizeof(ncommits)) == -1)
3421 return got_error_from_errno("imsg_add PAINTED_COMMITS");
3422 if (imsg_add(wbuf, &present_in_pack, sizeof(present_in_pack)) == -1)
3423 return got_error_from_errno("imsg_add PAINTED_COMMITS");
3425 while (ncommits > 0) {
3426 qid = STAILQ_FIRST(ids);
3427 STAILQ_REMOVE_HEAD(ids, entry);
3430 color = (intptr_t)qid->data;
3432 /* Keep in sync with struct got_imsg_painted_commit! */
3433 if (imsg_add(wbuf, qid->id.hash, SHA1_DIGEST_LENGTH) == -1)
3434 return got_error_from_errno("imsg_add PAINTED_COMMITS");
3435 if (imsg_add(wbuf, &color, sizeof(color)) == -1)
3436 return got_error_from_errno("imsg_add PAINTED_COMMITS");
3438 got_object_qid_free(qid);
3442 imsg_close(ibuf, wbuf);
3444 return flush_imsg(ibuf);
3447 const struct got_error *
3448 got_privsep_send_painted_commits(struct imsgbuf *ibuf,
3449 struct got_object_id_queue *ids, int *nids,
3450 int present_in_pack, int flush)
3452 const struct got_error *err;
3459 remain = (sizeof(struct got_imsg_painted_commits)) +
3460 *nids * sizeof(struct got_imsg_painted_commit);
3461 if (flush || remain >= MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
3462 err = send_painted_commits(ids, nids, remain,
3463 present_in_pack, ibuf);
3467 } while (flush && *nids > 0);
3472 const struct got_error *
3473 got_privsep_send_painting_commits_done(struct imsgbuf *ibuf)
3475 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_PAINTING_DONE,
3478 return got_error_from_errno("imsg_compose "
3479 "COMMIT_PAINTING_DONE");
3481 return flush_imsg(ibuf);
3484 const struct got_error *
3485 got_privsep_recv_painted_commits(struct got_object_id_queue *new_ids,
3486 got_privsep_recv_painted_commit_cb cb, void *cb_arg, struct imsgbuf *ibuf)
3488 const struct got_error *err = NULL;
3490 struct got_imsg_painted_commits icommits;
3491 struct got_imsg_painted_commit icommit;
3496 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
3500 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
3501 if (imsg.hdr.type == GOT_IMSG_COMMIT_PAINTING_DONE)
3503 if (imsg.hdr.type != GOT_IMSG_PAINTED_COMMITS)
3504 return got_error(GOT_ERR_PRIVSEP_MSG);
3506 if (datalen < sizeof(icommits))
3507 return got_error(GOT_ERR_PRIVSEP_LEN);
3508 memcpy(&icommits, imsg.data, sizeof(icommits));
3509 if (icommits.ncommits * sizeof(icommit) < icommits.ncommits ||
3510 datalen < sizeof(icommits) +
3511 icommits.ncommits * sizeof(icommit))
3512 return got_error(GOT_ERR_PRIVSEP_LEN);
3514 for (i = 0; i < icommits.ncommits; i++) {
3516 (uint8_t *)imsg.data + sizeof(icommits) + i * sizeof(icommit),
3519 if (icommits.present_in_pack) {
3520 struct got_object_id id;
3522 memset(&id, 0, sizeof(id));
3523 memcpy(id.hash, icommit.id, SHA1_DIGEST_LENGTH);
3524 err = cb(cb_arg, &id, icommit.color);
3528 struct got_object_qid *qid;
3529 err = got_object_qid_alloc_partial(&qid);
3532 memcpy(qid->id.hash, icommit.id,
3533 SHA1_DIGEST_LENGTH);
3534 qid->data = (void *)icommit.color;
3535 STAILQ_INSERT_TAIL(new_ids, qid, entry);
3545 const struct got_error *
3546 got_privsep_unveil_exec_helpers(void)
3548 const char *helpers[] = {
3549 GOT_PATH_PROG_READ_PACK,
3550 GOT_PATH_PROG_READ_OBJECT,
3551 GOT_PATH_PROG_READ_COMMIT,
3552 GOT_PATH_PROG_READ_TREE,
3553 GOT_PATH_PROG_READ_BLOB,
3554 GOT_PATH_PROG_READ_TAG,
3555 GOT_PATH_PROG_READ_GITCONFIG,
3556 GOT_PATH_PROG_READ_GOTCONFIG,
3557 GOT_PATH_PROG_READ_PATCH,
3558 GOT_PATH_PROG_FETCH_PACK,
3559 GOT_PATH_PROG_INDEX_PACK,
3560 GOT_PATH_PROG_SEND_PACK,
3564 for (i = 0; i < nitems(helpers); i++) {
3565 if (unveil(helpers[i], "x") == 0)
3567 return got_error_from_errno2("unveil", helpers[i]);
3574 got_privsep_exec_child(int imsg_fds[2], const char *path, const char *repo_path)
3576 if (close(imsg_fds[0]) == -1) {
3577 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
3581 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
3582 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
3586 closefrom(GOT_IMSG_FD_CHILD + 1);
3588 if (execl(path, path, repo_path, (char *)NULL) == -1) {
3589 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,