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_sha1.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"
52 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
56 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
59 static const struct got_error *
60 poll_fd(int fd, int events, int timeout)
68 pfd[0].events = events;
73 if (sigemptyset(&sigset) == -1)
74 return got_error_from_errno("sigemptyset");
75 if (sigaddset(&sigset, SIGWINCH) == -1)
76 return got_error_from_errno("sigaddset");
78 n = ppoll(pfd, 1, timeout == INFTIM ? NULL : &ts, &sigset);
80 return got_error_from_errno("ppoll");
82 return got_error(GOT_ERR_TIMEOUT);
83 if (pfd[0].revents & (POLLERR | POLLNVAL))
84 return got_error_from_errno("poll error");
85 if (pfd[0].revents & (events | POLLHUP))
88 return got_error(GOT_ERR_INTERRUPT);
91 static const struct got_error *
92 read_imsg(struct imsgbuf *ibuf)
94 const struct got_error *err;
97 err = poll_fd(ibuf->fd, POLLIN, INFTIM);
103 if (errno == EAGAIN) /* Could be a file-descriptor leak. */
104 return got_error(GOT_ERR_PRIVSEP_NO_FD);
105 return got_error(GOT_ERR_PRIVSEP_READ);
108 return got_error(GOT_ERR_PRIVSEP_PIPE);
113 const struct got_error *
114 got_privsep_wait_for_child(pid_t pid)
118 if (waitpid(pid, &child_status, 0) == -1)
119 return got_error_from_errno("waitpid");
121 if (!WIFEXITED(child_status))
122 return got_error(GOT_ERR_PRIVSEP_DIED);
124 if (WEXITSTATUS(child_status) != 0)
125 return got_error(GOT_ERR_PRIVSEP_EXIT);
130 static const struct got_error *
131 recv_imsg_error(struct imsg *imsg, size_t datalen)
133 struct got_imsg_error *ierr;
135 if (datalen != sizeof(*ierr))
136 return got_error(GOT_ERR_PRIVSEP_LEN);
139 if (ierr->code == GOT_ERR_ERRNO) {
140 static struct got_error serr;
141 serr.code = GOT_ERR_ERRNO;
142 serr.msg = strerror(ierr->errno_code);
146 return got_error(ierr->code);
149 const struct got_error *
150 got_privsep_recv_imsg(struct imsg *imsg, struct imsgbuf *ibuf,
153 const struct got_error *err;
156 n = imsg_get(ibuf, imsg);
158 return got_error_from_errno("imsg_get");
161 err = read_imsg(ibuf);
164 n = imsg_get(ibuf, imsg);
166 return got_error_from_errno("imsg_get");
169 if (imsg->hdr.len < IMSG_HEADER_SIZE + min_datalen)
170 return got_error(GOT_ERR_PRIVSEP_LEN);
172 if (imsg->hdr.type == GOT_IMSG_ERROR) {
173 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
174 return recv_imsg_error(imsg, datalen);
180 /* Attempt to send an error in an imsg. Complain on stderr as a last resort. */
182 got_privsep_send_error(struct imsgbuf *ibuf, const struct got_error *err)
184 const struct got_error *poll_err;
185 struct got_imsg_error ierr;
188 ierr.code = err->code;
189 if (err->code == GOT_ERR_ERRNO)
190 ierr.errno_code = errno;
193 ret = imsg_compose(ibuf, GOT_IMSG_ERROR, 0, 0, -1, &ierr, sizeof(ierr));
195 fprintf(stderr, "%s: error %d \"%s\": imsg_compose: %s\n",
196 getprogname(), err->code, err->msg, strerror(errno));
200 poll_err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
202 fprintf(stderr, "%s: error %d \"%s\": poll: %s\n",
203 getprogname(), err->code, err->msg, poll_err->msg);
207 ret = imsg_flush(ibuf);
209 fprintf(stderr, "%s: error %d \"%s\": imsg_flush: %s\n",
210 getprogname(), err->code, err->msg, strerror(errno));
216 static const struct got_error *
217 flush_imsg(struct imsgbuf *ibuf)
219 const struct got_error *err;
221 err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
225 if (imsg_flush(ibuf) == -1) {
227 return got_error_from_errno("imsg_flush");
233 const struct got_error *
234 got_privsep_flush_imsg(struct imsgbuf *ibuf)
236 return flush_imsg(ibuf);
239 const struct got_error *
240 got_privsep_send_stop(int fd)
242 const struct got_error *err = NULL;
245 imsg_init(&ibuf, fd);
247 if (imsg_compose(&ibuf, GOT_IMSG_STOP, 0, 0, -1, NULL, 0) == -1)
248 return got_error_from_errno("imsg_compose STOP");
250 err = flush_imsg(&ibuf);
254 const struct got_error *
255 got_privsep_send_obj_req(struct imsgbuf *ibuf, int fd,
256 struct got_object_id *id)
258 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_REQUEST, 0, 0, fd,
259 id, sizeof(*id)) == -1)
260 return got_error_from_errno("imsg_compose OBJECT_REQUEST");
262 return flush_imsg(ibuf);
265 const struct got_error *
266 got_privsep_send_raw_obj_req(struct imsgbuf *ibuf, int fd,
267 struct got_object_id *id)
269 if (imsg_compose(ibuf, GOT_IMSG_RAW_OBJECT_REQUEST, 0, 0, fd,
270 id, sizeof(*id)) == -1)
271 return got_error_from_errno("imsg_compose RAW_OBJECT_REQUEST");
273 return flush_imsg(ibuf);
276 const struct got_error *
277 got_privsep_send_raw_obj_outfd(struct imsgbuf *ibuf, int outfd)
279 const struct got_error *err = NULL;
281 if (imsg_compose(ibuf, GOT_IMSG_RAW_OBJECT_OUTFD, 0, 0, outfd, NULL, 0)
283 err = got_error_from_errno("imsg_compose RAW_OBJECT_OUTFD");
288 return flush_imsg(ibuf);
291 const struct got_error *
292 got_privsep_send_raw_obj(struct imsgbuf *ibuf, off_t size, size_t hdrlen,
295 const struct got_error *err = NULL;
296 struct got_imsg_raw_obj iobj;
297 size_t len = sizeof(iobj);
300 iobj.hdrlen = hdrlen;
303 if (data && size + hdrlen <= GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX)
304 len += (size_t)size + hdrlen;
306 wbuf = imsg_create(ibuf, GOT_IMSG_RAW_OBJECT, 0, 0, len);
308 err = got_error_from_errno("imsg_create RAW_OBJECT");
312 if (imsg_add(wbuf, &iobj, sizeof(iobj)) == -1)
313 return got_error_from_errno("imsg_add RAW_OBJECT");
315 if (data && size + hdrlen <= GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX) {
316 if (imsg_add(wbuf, data, size + hdrlen) == -1)
317 return got_error_from_errno("imsg_add RAW_OBJECT");
321 imsg_close(ibuf, wbuf);
323 return flush_imsg(ibuf);
326 const struct got_error *
327 got_privsep_recv_raw_obj(uint8_t **outbuf, off_t *size, size_t *hdrlen,
328 struct imsgbuf *ibuf)
330 const struct got_error *err = NULL;
332 struct got_imsg_raw_obj *iobj;
337 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
341 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
343 switch (imsg.hdr.type) {
344 case GOT_IMSG_RAW_OBJECT:
345 if (datalen < sizeof(*iobj)) {
346 err = got_error(GOT_ERR_PRIVSEP_LEN);
351 *hdrlen = iobj->hdrlen;
353 if (datalen == sizeof(*iobj)) {
354 /* Data has been written to file descriptor. */
358 if (*size + *hdrlen > GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX) {
359 err = got_error(GOT_ERR_PRIVSEP_LEN);
363 *outbuf = malloc(*size + *hdrlen);
364 if (*outbuf == NULL) {
365 err = got_error_from_errno("malloc");
368 memcpy(*outbuf, imsg.data + sizeof(*iobj), *size + *hdrlen);
371 err = got_error(GOT_ERR_PRIVSEP_MSG);
380 const struct got_error *
381 got_privsep_send_commit_req(struct imsgbuf *ibuf, int fd,
382 struct got_object_id *id, int pack_idx)
384 const struct got_error *err = NULL;
385 struct got_imsg_packed_object iobj;
389 if (pack_idx != -1) { /* commit is packed */
391 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
399 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_REQUEST, 0, 0, fd, data, len)
401 err = got_error_from_errno("imsg_compose COMMIT_REQUEST");
406 return flush_imsg(ibuf);
409 const struct got_error *
410 got_privsep_send_tree_req(struct imsgbuf *ibuf, int fd,
411 struct got_object_id *id, int pack_idx)
417 len = sizeof(struct got_imsg_packed_object);
421 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_REQUEST, 0, 0, len);
423 return got_error_from_errno("imsg_create TREE_REQUEST");
425 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1)
426 return got_error_from_errno("imsg_add TREE_REQUEST");
428 if (pack_idx != -1) { /* tree is packed */
429 if (imsg_add(wbuf, &pack_idx, sizeof(pack_idx)) == -1)
430 return got_error_from_errno("imsg_add TREE_REQUEST");
434 imsg_close(ibuf, wbuf);
436 return flush_imsg(ibuf);
439 const struct got_error *
440 got_privsep_send_tag_req(struct imsgbuf *ibuf, int fd,
441 struct got_object_id *id, int pack_idx)
443 struct got_imsg_packed_object iobj;
447 if (pack_idx != -1) { /* tag is packed */
449 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
457 if (imsg_compose(ibuf, GOT_IMSG_TAG_REQUEST, 0, 0, fd, data, len)
459 return got_error_from_errno("imsg_compose TAG_REQUEST");
461 return flush_imsg(ibuf);
464 const struct got_error *
465 got_privsep_send_blob_req(struct imsgbuf *ibuf, int infd,
466 struct got_object_id *id, int pack_idx)
468 const struct got_error *err = NULL;
469 struct got_imsg_packed_object iobj;
473 if (pack_idx != -1) { /* blob is packed */
475 memcpy(iobj.id, id->sha1, 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 memcpy(iobj.id, obj->id.sha1, sizeof(iobj.id));
534 iobj.type = obj->type;
535 iobj.flags = obj->flags;
536 iobj.hdrlen = obj->hdrlen;
537 iobj.size = obj->size;
538 if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
539 iobj.pack_offset = obj->pack_offset;
540 iobj.pack_idx = obj->pack_idx;
543 if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
545 return got_error_from_errno("imsg_compose OBJECT");
547 return flush_imsg(ibuf);
550 const struct got_error *
551 got_privsep_send_fetch_req(struct imsgbuf *ibuf, int fd,
552 struct got_pathlist_head *have_refs, int fetch_all_branches,
553 struct got_pathlist_head *wanted_branches,
554 struct got_pathlist_head *wanted_refs, int list_refs_only, int verbosity)
556 const struct got_error *err = NULL;
559 struct got_pathlist_entry *pe;
560 struct got_imsg_fetch_request fetchreq;
562 memset(&fetchreq, 0, sizeof(fetchreq));
563 fetchreq.fetch_all_branches = fetch_all_branches;
564 fetchreq.list_refs_only = list_refs_only;
565 fetchreq.verbosity = verbosity;
566 TAILQ_FOREACH(pe, have_refs, entry)
567 fetchreq.n_have_refs++;
568 TAILQ_FOREACH(pe, wanted_branches, entry)
569 fetchreq.n_wanted_branches++;
570 TAILQ_FOREACH(pe, wanted_refs, entry)
571 fetchreq.n_wanted_refs++;
572 len = sizeof(struct got_imsg_fetch_request);
573 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
575 return got_error(GOT_ERR_NO_SPACE);
578 if (imsg_compose(ibuf, GOT_IMSG_FETCH_REQUEST, 0, 0, fd,
579 &fetchreq, sizeof(fetchreq)) == -1)
580 return got_error_from_errno(
581 "imsg_compose FETCH_SERVER_PROGRESS");
583 err = flush_imsg(ibuf);
590 TAILQ_FOREACH(pe, have_refs, entry) {
591 const char *name = pe->path;
592 size_t name_len = pe->path_len;
593 struct got_object_id *id = pe->data;
595 len = sizeof(struct got_imsg_fetch_have_ref) + name_len;
596 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_HAVE_REF, 0, 0, len);
598 return got_error_from_errno("imsg_create FETCH_HAVE_REF");
600 /* Keep in sync with struct got_imsg_fetch_have_ref! */
601 if (imsg_add(wbuf, id->sha1, sizeof(id->sha1)) == -1)
602 return got_error_from_errno("imsg_add FETCH_HAVE_REF");
603 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1)
604 return got_error_from_errno("imsg_add FETCH_HAVE_REF");
605 if (imsg_add(wbuf, name, name_len) == -1)
606 return got_error_from_errno("imsg_add FETCH_HAVE_REF");
609 imsg_close(ibuf, wbuf);
610 err = flush_imsg(ibuf);
615 TAILQ_FOREACH(pe, wanted_branches, entry) {
616 const char *name = pe->path;
617 size_t name_len = pe->path_len;
619 len = sizeof(struct got_imsg_fetch_wanted_branch) + name_len;
620 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_WANTED_BRANCH, 0, 0,
623 return got_error_from_errno(
624 "imsg_create FETCH_WANTED_BRANCH");
626 /* Keep in sync with struct got_imsg_fetch_wanted_branch! */
627 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1)
628 return got_error_from_errno(
629 "imsg_add FETCH_WANTED_BRANCH");
630 if (imsg_add(wbuf, name, name_len) == -1)
631 return got_error_from_errno(
632 "imsg_add FETCH_WANTED_BRANCH");
635 imsg_close(ibuf, wbuf);
636 err = flush_imsg(ibuf);
641 TAILQ_FOREACH(pe, wanted_refs, entry) {
642 const char *name = pe->path;
643 size_t name_len = pe->path_len;
645 len = sizeof(struct got_imsg_fetch_wanted_ref) + name_len;
646 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_WANTED_REF, 0, 0,
649 return got_error_from_errno(
650 "imsg_create FETCH_WANTED_REF");
652 /* Keep in sync with struct got_imsg_fetch_wanted_ref! */
653 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1)
654 return got_error_from_errno(
655 "imsg_add FETCH_WANTED_REF");
656 if (imsg_add(wbuf, name, name_len) == -1)
657 return got_error_from_errno(
658 "imsg_add FETCH_WANTED_REF");
661 imsg_close(ibuf, wbuf);
662 err = flush_imsg(ibuf);
672 const struct got_error *
673 got_privsep_send_fetch_outfd(struct imsgbuf *ibuf, int fd)
675 return send_fd(ibuf, GOT_IMSG_FETCH_OUTFD, fd);
678 const struct got_error *
679 got_privsep_recv_fetch_progress(int *done, struct got_object_id **id,
680 char **refname, struct got_pathlist_head *symrefs, char **server_progress,
681 off_t *packfile_size, uint8_t *pack_sha1, struct imsgbuf *ibuf)
683 const struct got_error *err = NULL;
686 struct got_imsg_fetch_symrefs *isymrefs = NULL;
694 *server_progress = NULL;
696 memset(pack_sha1, 0, SHA1_DIGEST_LENGTH);
698 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
702 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
703 switch (imsg.hdr.type) {
705 err = recv_imsg_error(&imsg, datalen);
707 case GOT_IMSG_FETCH_SYMREFS:
708 if (datalen < sizeof(*isymrefs)) {
709 err = got_error(GOT_ERR_PRIVSEP_LEN);
712 if (isymrefs != NULL) {
713 err = got_error(GOT_ERR_PRIVSEP_MSG);
716 isymrefs = (struct got_imsg_fetch_symrefs *)imsg.data;
717 off = sizeof(*isymrefs);
718 remain = datalen - off;
719 for (n = 0; n < isymrefs->nsymrefs; n++) {
720 struct got_imsg_fetch_symref *s;
722 if (remain < sizeof(struct got_imsg_fetch_symref)) {
723 err = got_error(GOT_ERR_PRIVSEP_LEN);
726 s = (struct got_imsg_fetch_symref *)(imsg.data + off);
728 remain -= sizeof(*s);
729 if (remain < s->name_len) {
730 err = got_error(GOT_ERR_PRIVSEP_LEN);
733 name = strndup(imsg.data + off, s->name_len);
735 err = got_error_from_errno("strndup");
739 remain -= s->name_len;
740 if (remain < s->target_len) {
741 err = got_error(GOT_ERR_PRIVSEP_LEN);
745 target = strndup(imsg.data + off, s->target_len);
746 if (target == NULL) {
747 err = got_error_from_errno("strndup");
751 off += s->target_len;
752 remain -= s->target_len;
753 err = got_pathlist_append(symrefs, name, target);
761 case GOT_IMSG_FETCH_REF:
762 if (datalen <= SHA1_DIGEST_LENGTH) {
763 err = got_error(GOT_ERR_PRIVSEP_MSG);
766 *id = malloc(sizeof(**id));
768 err = got_error_from_errno("malloc");
771 memcpy((*id)->sha1, imsg.data, SHA1_DIGEST_LENGTH);
772 *refname = strndup(imsg.data + SHA1_DIGEST_LENGTH,
773 datalen - SHA1_DIGEST_LENGTH);
774 if (*refname == NULL) {
775 err = got_error_from_errno("strndup");
779 case GOT_IMSG_FETCH_SERVER_PROGRESS:
781 err = got_error(GOT_ERR_PRIVSEP_LEN);
784 *server_progress = strndup(imsg.data, datalen);
785 if (*server_progress == NULL) {
786 err = got_error_from_errno("strndup");
789 for (i = 0; i < datalen; i++) {
790 if (!isprint((unsigned char)(*server_progress)[i]) &&
791 !isspace((unsigned char)(*server_progress)[i])) {
792 err = got_error(GOT_ERR_PRIVSEP_MSG);
793 free(*server_progress);
794 *server_progress = NULL;
799 case GOT_IMSG_FETCH_DOWNLOAD_PROGRESS:
800 if (datalen < sizeof(*packfile_size)) {
801 err = got_error(GOT_ERR_PRIVSEP_MSG);
804 memcpy(packfile_size, imsg.data, sizeof(*packfile_size));
806 case GOT_IMSG_FETCH_DONE:
807 if (datalen != SHA1_DIGEST_LENGTH) {
808 err = got_error(GOT_ERR_PRIVSEP_MSG);
811 memcpy(pack_sha1, imsg.data, SHA1_DIGEST_LENGTH);
815 err = got_error(GOT_ERR_PRIVSEP_MSG);
829 static const struct got_error *
830 send_send_ref(const char *name, size_t name_len, struct got_object_id *id,
831 int delete, struct imsgbuf *ibuf)
836 len = sizeof(struct got_imsg_send_ref) + name_len;
837 wbuf = imsg_create(ibuf, GOT_IMSG_SEND_REF, 0, 0, len);
839 return got_error_from_errno("imsg_create SEND_REF");
841 /* Keep in sync with struct got_imsg_send_ref! */
842 if (imsg_add(wbuf, id->sha1, sizeof(id->sha1)) == -1)
843 return got_error_from_errno("imsg_add SEND_REF");
844 if (imsg_add(wbuf, &delete, sizeof(delete)) == -1)
845 return got_error_from_errno("imsg_add SEND_REF");
846 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1)
847 return got_error_from_errno("imsg_add SEND_REF");
848 if (imsg_add(wbuf, name, name_len) == -1)
849 return got_error_from_errno("imsg_add SEND_REF");
852 imsg_close(ibuf, wbuf);
853 return flush_imsg(ibuf);
856 const struct got_error *
857 got_privsep_send_send_req(struct imsgbuf *ibuf, int fd,
858 struct got_pathlist_head *have_refs,
859 struct got_pathlist_head *delete_refs,
862 const struct got_error *err = NULL;
863 struct got_pathlist_entry *pe;
864 struct got_imsg_send_request sendreq;
865 struct got_object_id zero_id;
867 memset(&zero_id, 0, sizeof(zero_id));
868 memset(&sendreq, 0, sizeof(sendreq));
869 sendreq.verbosity = verbosity;
870 TAILQ_FOREACH(pe, have_refs, entry)
872 TAILQ_FOREACH(pe, delete_refs, entry)
874 if (imsg_compose(ibuf, GOT_IMSG_SEND_REQUEST, 0, 0, fd,
875 &sendreq, sizeof(sendreq)) == -1) {
876 err = got_error_from_errno(
877 "imsg_compose FETCH_SERVER_PROGRESS");
881 err = flush_imsg(ibuf);
886 TAILQ_FOREACH(pe, have_refs, entry) {
887 const char *name = pe->path;
888 size_t name_len = pe->path_len;
889 struct got_object_id *id = pe->data;
890 err = send_send_ref(name, name_len, id, 0, ibuf);
895 TAILQ_FOREACH(pe, delete_refs, entry) {
896 const char *name = pe->path;
897 size_t name_len = pe->path_len;
898 err = send_send_ref(name, name_len, &zero_id, 1, ibuf);
903 if (fd != -1 && close(fd) == -1 && err == NULL)
904 err = got_error_from_errno("close");
909 const struct got_error *
910 got_privsep_recv_send_remote_refs(struct got_pathlist_head *remote_refs,
911 struct imsgbuf *ibuf)
913 const struct got_error *err = NULL;
917 struct got_imsg_send_remote_ref iremote_ref;
918 struct got_object_id *id = NULL;
919 char *refname = NULL;
920 struct got_pathlist_entry *new;
923 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
926 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
927 switch (imsg.hdr.type) {
929 err = recv_imsg_error(&imsg, datalen);
931 case GOT_IMSG_SEND_REMOTE_REF:
932 if (datalen < sizeof(iremote_ref)) {
933 err = got_error(GOT_ERR_PRIVSEP_MSG);
936 memcpy(&iremote_ref, imsg.data, sizeof(iremote_ref));
937 if (datalen != sizeof(iremote_ref) +
938 iremote_ref.name_len) {
939 err = got_error(GOT_ERR_PRIVSEP_MSG);
942 id = malloc(sizeof(*id));
944 err = got_error_from_errno("malloc");
947 memcpy(id->sha1, iremote_ref.id, SHA1_DIGEST_LENGTH);
948 refname = strndup(imsg.data + sizeof(iremote_ref),
949 datalen - sizeof(iremote_ref));
950 if (refname == NULL) {
951 err = got_error_from_errno("strndup");
954 err = got_pathlist_insert(&new, remote_refs,
958 if (new == NULL) { /* duplicate which wasn't inserted */
965 case GOT_IMSG_SEND_PACK_REQUEST:
967 err = got_error(GOT_ERR_PRIVSEP_MSG);
970 /* got-send-pack is now waiting for a pack file. */
974 err = got_error(GOT_ERR_PRIVSEP_MSG);
985 const struct got_error *
986 got_privsep_send_packfd(struct imsgbuf *ibuf, int fd)
988 return send_fd(ibuf, GOT_IMSG_SEND_PACKFD, fd);
991 const struct got_error *
992 got_privsep_recv_send_progress(int *done, off_t *bytes_sent,
993 int *success, char **refname, struct imsgbuf *ibuf)
995 const struct got_error *err = NULL;
998 struct got_imsg_send_ref_status iref_status;
1000 /* Do not reset the current value of 'bytes_sent', it accumulates. */
1005 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1009 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1010 switch (imsg.hdr.type) {
1011 case GOT_IMSG_ERROR:
1012 err = recv_imsg_error(&imsg, datalen);
1014 case GOT_IMSG_SEND_UPLOAD_PROGRESS:
1015 if (datalen < sizeof(*bytes_sent)) {
1016 err = got_error(GOT_ERR_PRIVSEP_MSG);
1019 memcpy(bytes_sent, imsg.data, sizeof(*bytes_sent));
1021 case GOT_IMSG_SEND_REF_STATUS:
1022 if (datalen < sizeof(iref_status)) {
1023 err = got_error(GOT_ERR_PRIVSEP_MSG);
1026 memcpy(&iref_status, imsg.data, sizeof(iref_status));
1027 if (datalen != sizeof(iref_status) + iref_status.name_len) {
1028 err = got_error(GOT_ERR_PRIVSEP_MSG);
1031 *success = iref_status.success;
1032 *refname = strndup(imsg.data + sizeof(iref_status),
1033 iref_status.name_len);
1035 case GOT_IMSG_SEND_DONE:
1037 err = got_error(GOT_ERR_PRIVSEP_MSG);
1043 err = got_error(GOT_ERR_PRIVSEP_MSG);
1051 const struct got_error *
1052 got_privsep_send_index_pack_req(struct imsgbuf *ibuf, uint8_t *pack_sha1,
1055 const struct got_error *err = NULL;
1057 /* Keep in sync with struct got_imsg_index_pack_request */
1058 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_REQUEST, 0, 0, fd,
1059 pack_sha1, SHA1_DIGEST_LENGTH) == -1) {
1060 err = got_error_from_errno("imsg_compose INDEX_REQUEST");
1064 return flush_imsg(ibuf);
1067 const struct got_error *
1068 got_privsep_send_index_pack_outfd(struct imsgbuf *ibuf, int fd)
1070 return send_fd(ibuf, GOT_IMSG_IDXPACK_OUTFD, fd);
1073 const struct got_error *
1074 got_privsep_recv_index_progress(int *done, int *nobj_total,
1075 int *nobj_indexed, int *nobj_loose, int *nobj_resolved,
1076 struct imsgbuf *ibuf)
1078 const struct got_error *err = NULL;
1080 struct got_imsg_index_pack_progress *iprogress;
1088 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1092 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1093 switch (imsg.hdr.type) {
1094 case GOT_IMSG_ERROR:
1095 err = recv_imsg_error(&imsg, datalen);
1097 case GOT_IMSG_IDXPACK_PROGRESS:
1098 if (datalen < sizeof(*iprogress)) {
1099 err = got_error(GOT_ERR_PRIVSEP_LEN);
1102 iprogress = (struct got_imsg_index_pack_progress *)imsg.data;
1103 *nobj_total = iprogress->nobj_total;
1104 *nobj_indexed = iprogress->nobj_indexed;
1105 *nobj_loose = iprogress->nobj_loose;
1106 *nobj_resolved = iprogress->nobj_resolved;
1108 case GOT_IMSG_IDXPACK_DONE:
1110 err = got_error(GOT_ERR_PRIVSEP_LEN);
1116 err = got_error(GOT_ERR_PRIVSEP_MSG);
1124 const struct got_error *
1125 got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
1126 struct imsgbuf *ibuf)
1128 struct got_imsg_object *iobj;
1129 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1131 if (datalen != sizeof(*iobj))
1132 return got_error(GOT_ERR_PRIVSEP_LEN);
1135 *obj = calloc(1, sizeof(**obj));
1137 return got_error_from_errno("calloc");
1139 memcpy((*obj)->id.sha1, iobj->id, SHA1_DIGEST_LENGTH);
1140 (*obj)->type = iobj->type;
1141 (*obj)->flags = iobj->flags;
1142 (*obj)->hdrlen = iobj->hdrlen;
1143 (*obj)->size = iobj->size;
1144 /* path_packfile is handled by caller */
1145 if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
1146 (*obj)->pack_offset = iobj->pack_offset;
1147 (*obj)->pack_idx = iobj->pack_idx;
1149 STAILQ_INIT(&(*obj)->deltas.entries);
1153 const struct got_error *
1154 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
1156 const struct got_error *err = NULL;
1158 const size_t min_datalen =
1159 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
1163 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1167 switch (imsg.hdr.type) {
1168 case GOT_IMSG_OBJECT:
1169 err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
1172 err = got_error(GOT_ERR_PRIVSEP_MSG);
1181 static const struct got_error *
1182 send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
1185 const struct got_error *err = NULL;
1186 size_t offset, remain;
1189 remain = logmsg_len;
1190 while (remain > 0) {
1191 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1193 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
1194 commit->logmsg + offset, n) == -1) {
1195 err = got_error_from_errno("imsg_compose "
1200 err = flush_imsg(ibuf);
1211 const struct got_error *
1212 got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
1214 const struct got_error *err = NULL;
1215 struct got_imsg_commit_object *icommit;
1218 struct got_object_qid *qid;
1219 size_t author_len = strlen(commit->author);
1220 size_t committer_len = strlen(commit->committer);
1221 size_t logmsg_len = strlen(commit->logmsg);
1223 total = sizeof(*icommit) + author_len + committer_len +
1224 commit->nparents * SHA1_DIGEST_LENGTH;
1226 buf = malloc(total);
1228 return got_error_from_errno("malloc");
1230 icommit = (struct got_imsg_commit_object *)buf;
1231 memcpy(icommit->tree_id, commit->tree_id->sha1,
1232 sizeof(icommit->tree_id));
1233 icommit->author_len = author_len;
1234 icommit->author_time = commit->author_time;
1235 icommit->author_gmtoff = commit->author_gmtoff;
1236 icommit->committer_len = committer_len;
1237 icommit->committer_time = commit->committer_time;
1238 icommit->committer_gmtoff = commit->committer_gmtoff;
1239 icommit->logmsg_len = logmsg_len;
1240 icommit->nparents = commit->nparents;
1242 len = sizeof(*icommit);
1243 memcpy(buf + len, commit->author, author_len);
1245 memcpy(buf + len, commit->committer, committer_len);
1246 len += committer_len;
1247 STAILQ_FOREACH(qid, &commit->parent_ids, entry) {
1248 memcpy(buf + len, &qid->id, SHA1_DIGEST_LENGTH);
1249 len += SHA1_DIGEST_LENGTH;
1252 if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
1253 err = got_error_from_errno("imsg_compose COMMIT");
1257 if (logmsg_len == 0 ||
1258 logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1259 err = flush_imsg(ibuf);
1263 err = send_commit_logmsg(ibuf, commit, logmsg_len);
1269 static const struct got_error *
1270 get_commit_from_imsg(struct got_commit_object **commit,
1271 struct imsg *imsg, size_t datalen, struct imsgbuf *ibuf)
1273 const struct got_error *err = NULL;
1274 struct got_imsg_commit_object *icommit;
1278 if (datalen < sizeof(*icommit))
1279 return got_error(GOT_ERR_PRIVSEP_LEN);
1281 icommit = imsg->data;
1282 if (datalen != sizeof(*icommit) + icommit->author_len +
1283 icommit->committer_len +
1284 icommit->nparents * SHA1_DIGEST_LENGTH)
1285 return got_error(GOT_ERR_PRIVSEP_LEN);
1287 if (icommit->nparents < 0)
1288 return got_error(GOT_ERR_PRIVSEP_LEN);
1290 len += sizeof(*icommit);
1292 *commit = got_object_commit_alloc_partial();
1293 if (*commit == NULL)
1294 return got_error_from_errno(
1295 "got_object_commit_alloc_partial");
1297 memcpy((*commit)->tree_id->sha1, icommit->tree_id,
1298 SHA1_DIGEST_LENGTH);
1299 (*commit)->author_time = icommit->author_time;
1300 (*commit)->author_gmtoff = icommit->author_gmtoff;
1301 (*commit)->committer_time = icommit->committer_time;
1302 (*commit)->committer_gmtoff = icommit->committer_gmtoff;
1304 (*commit)->author = strndup(imsg->data + len, icommit->author_len);
1305 if ((*commit)->author == NULL) {
1306 err = got_error_from_errno("strndup");
1309 len += icommit->author_len;
1311 (*commit)->committer = strndup(imsg->data + len,
1312 icommit->committer_len);
1313 if ((*commit)->committer == NULL) {
1314 err = got_error_from_errno("strndup");
1317 len += icommit->committer_len;
1319 if (icommit->logmsg_len == 0) {
1320 (*commit)->logmsg = strdup("");
1321 if ((*commit)->logmsg == NULL) {
1322 err = got_error_from_errno("strdup");
1326 size_t offset = 0, remain = icommit->logmsg_len;
1328 (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
1329 if ((*commit)->logmsg == NULL) {
1330 err = got_error_from_errno("malloc");
1333 while (remain > 0) {
1334 struct imsg imsg_log;
1335 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1338 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1342 if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG) {
1343 err = got_error(GOT_ERR_PRIVSEP_MSG);
1347 memcpy((*commit)->logmsg + offset,
1349 imsg_free(&imsg_log);
1353 (*commit)->logmsg[icommit->logmsg_len] = '\0';
1356 for (i = 0; i < icommit->nparents; i++) {
1357 struct got_object_qid *qid;
1359 err = got_object_qid_alloc_partial(&qid);
1362 memcpy(&qid->id, imsg->data + len +
1363 i * SHA1_DIGEST_LENGTH, sizeof(qid->id));
1364 STAILQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
1365 (*commit)->nparents++;
1369 got_object_commit_close(*commit);
1375 const struct got_error *
1376 got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
1378 const struct got_error *err = NULL;
1381 const size_t min_datalen =
1382 MIN(sizeof(struct got_imsg_error),
1383 sizeof(struct got_imsg_commit_object));
1387 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1391 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1393 switch (imsg.hdr.type) {
1394 case GOT_IMSG_COMMIT:
1395 err = get_commit_from_imsg(commit, &imsg, datalen, ibuf);
1398 err = got_error(GOT_ERR_PRIVSEP_MSG);
1407 static const struct got_error *
1408 send_tree_entries_batch(struct imsgbuf *ibuf,
1409 struct got_parsed_tree_entry *entries, int idx0, int idxN, size_t len)
1412 struct got_imsg_tree_entries 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 struct got_parsed_tree_entry *pte = &entries[i];
1426 /* Keep in sync with struct got_imsg_tree_object definition! */
1427 if (imsg_add(wbuf, pte->id, SHA1_DIGEST_LENGTH) == -1)
1428 return got_error_from_errno("imsg_add TREE_ENTRY");
1429 if (imsg_add(wbuf, &pte->mode, sizeof(pte->mode)) == -1)
1430 return got_error_from_errno("imsg_add TREE_ENTRY");
1431 if (imsg_add(wbuf, &pte->namelen, sizeof(pte->namelen)) == -1)
1432 return got_error_from_errno("imsg_add TREE_ENTRY");
1434 /* Remaining bytes are the entry's name. */
1435 if (imsg_add(wbuf, pte->name, pte->namelen) == -1)
1436 return got_error_from_errno("imsg_add TREE_ENTRY");
1440 imsg_close(ibuf, wbuf);
1444 static const struct got_error *
1445 send_tree_entries(struct imsgbuf *ibuf, struct got_parsed_tree_entry *entries,
1448 const struct got_error *err = NULL;
1450 size_t entries_len = sizeof(struct got_imsg_tree_entries);
1453 for (j = 0; j < nentries; j++) {
1454 struct got_parsed_tree_entry *pte = &entries[j];
1455 size_t len = sizeof(*pte) + pte->namelen;
1458 entries_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1459 err = send_tree_entries_batch(ibuf, entries,
1460 i, j - 1, entries_len);
1464 entries_len = sizeof(struct got_imsg_tree_entries);
1471 err = send_tree_entries_batch(ibuf, entries, i, j - 1,
1480 const struct got_error *
1481 got_privsep_send_tree(struct imsgbuf *ibuf,
1482 struct got_parsed_tree_entry *entries, int nentries)
1484 const struct got_error *err = NULL;
1485 struct got_imsg_tree_object itree;
1487 itree.nentries = nentries;
1488 if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
1490 return got_error_from_errno("imsg_compose TREE");
1492 err = send_tree_entries(ibuf, entries, nentries);
1496 return flush_imsg(ibuf);
1500 static const struct got_error *
1501 recv_tree_entries(void *data, size_t datalen, struct got_tree_object *tree,
1504 const struct got_error *err = NULL;
1505 struct got_imsg_tree_entries *ientries;
1506 struct got_tree_entry *te;
1510 if (datalen <= sizeof(*ientries) ||
1511 datalen > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
1512 return got_error(GOT_ERR_PRIVSEP_LEN);
1514 ientries = (struct got_imsg_tree_entries *)data;
1515 if (ientries->nentries > INT_MAX) {
1516 return got_error_msg(GOT_ERR_NO_SPACE,
1517 "too many tree entries");
1520 te_offset = sizeof(*ientries);
1521 for (i = 0; i < ientries->nentries; i++) {
1522 struct got_imsg_tree_entry ite;
1523 const char *te_name;
1524 uint8_t *buf = (uint8_t *)data + te_offset;
1526 if (te_offset >= datalen) {
1527 err = got_error(GOT_ERR_PRIVSEP_LEN);
1531 /* Might not be aligned, size is ~32 bytes. */
1532 memcpy(&ite, buf, sizeof(ite));
1534 if (ite.namelen >= sizeof(te->name)) {
1535 err = got_error(GOT_ERR_PRIVSEP_LEN);
1538 if (te_offset + sizeof(ite) + ite.namelen > datalen) {
1539 err = got_error(GOT_ERR_PRIVSEP_LEN);
1543 if (*nentries >= tree->nentries) {
1544 err = got_error(GOT_ERR_PRIVSEP_LEN);
1547 te = &tree->entries[*nentries];
1548 te_name = buf + sizeof(ite);
1549 memcpy(te->name, te_name, ite.namelen);
1550 te->name[ite.namelen] = '\0';
1551 memcpy(te->id.sha1, ite.id, SHA1_DIGEST_LENGTH);
1552 te->mode = ite.mode;
1553 te->idx = *nentries;
1556 te_offset += sizeof(ite) + ite.namelen;
1562 const struct got_error *
1563 got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
1565 const struct got_error *err = NULL;
1566 const size_t min_datalen =
1567 MIN(sizeof(struct got_imsg_error),
1568 sizeof(struct got_imsg_tree_object));
1569 struct got_imsg_tree_object *itree;
1574 err = read_imsg(ibuf);
1583 n = imsg_get(ibuf, &imsg);
1586 if (nentries < (*tree)->nentries) {
1587 err = read_imsg(ibuf);
1594 err = got_error(GOT_ERR_PRIVSEP_MSG);
1599 if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen) {
1601 err = got_error(GOT_ERR_PRIVSEP_LEN);
1605 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1607 switch (imsg.hdr.type) {
1608 case GOT_IMSG_ERROR:
1609 err = recv_imsg_error(&imsg, datalen);
1612 /* This message should only appear once. */
1613 if (*tree != NULL) {
1614 err = got_error(GOT_ERR_PRIVSEP_MSG);
1617 if (datalen != sizeof(*itree)) {
1618 err = got_error(GOT_ERR_PRIVSEP_LEN);
1622 if (itree->nentries < 0) {
1623 err = got_error(GOT_ERR_PRIVSEP_LEN);
1626 *tree = malloc(sizeof(**tree));
1627 if (*tree == NULL) {
1628 err = got_error_from_errno("malloc");
1631 (*tree)->entries = calloc(itree->nentries,
1632 sizeof(struct got_tree_entry));
1633 if ((*tree)->entries == NULL) {
1634 err = got_error_from_errno("malloc");
1639 (*tree)->nentries = itree->nentries;
1640 (*tree)->refcnt = 0;
1642 case GOT_IMSG_TREE_ENTRIES:
1643 /* This message should be preceeded by GOT_IMSG_TREE. */
1644 if (*tree == NULL) {
1645 err = got_error(GOT_ERR_PRIVSEP_MSG);
1648 err = recv_tree_entries(imsg.data, datalen,
1652 err = got_error(GOT_ERR_PRIVSEP_MSG);
1661 if (*tree && (*tree)->nentries != nentries) {
1663 err = got_error(GOT_ERR_PRIVSEP_LEN);
1664 got_object_tree_close(*tree);
1671 const struct got_error *
1672 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
1673 const uint8_t *data)
1675 struct got_imsg_blob iblob;
1678 iblob.hdrlen = hdrlen;
1683 if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
1684 return got_error(GOT_ERR_NO_SPACE);
1686 buf = malloc(sizeof(iblob) + size);
1688 return got_error_from_errno("malloc");
1690 memcpy(buf, &iblob, sizeof(iblob));
1691 memcpy(buf + sizeof(iblob), data, size);
1692 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
1693 sizeof(iblob) + size) == -1) {
1695 return got_error_from_errno("imsg_compose BLOB");
1699 /* Data has already been written to file descriptor. */
1700 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
1701 sizeof(iblob)) == -1)
1702 return got_error_from_errno("imsg_compose BLOB");
1706 return flush_imsg(ibuf);
1709 const struct got_error *
1710 got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1711 struct imsgbuf *ibuf)
1713 const struct got_error *err = NULL;
1715 struct got_imsg_blob *iblob;
1720 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1724 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1726 switch (imsg.hdr.type) {
1728 if (datalen < sizeof(*iblob)) {
1729 err = got_error(GOT_ERR_PRIVSEP_LEN);
1733 *size = iblob->size;
1734 *hdrlen = iblob->hdrlen;
1736 if (datalen == sizeof(*iblob)) {
1737 /* Data has been written to file descriptor. */
1741 if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX) {
1742 err = got_error(GOT_ERR_PRIVSEP_LEN);
1746 *outbuf = malloc(*size);
1747 if (*outbuf == NULL) {
1748 err = got_error_from_errno("malloc");
1751 memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
1754 err = got_error(GOT_ERR_PRIVSEP_MSG);
1763 static const struct got_error *
1764 send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
1766 const struct got_error *err = NULL;
1767 size_t offset, remain;
1770 remain = tagmsg_len;
1771 while (remain > 0) {
1772 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1774 if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
1775 tag->tagmsg + offset, n) == -1) {
1776 err = got_error_from_errno("imsg_compose TAG_TAGMSG");
1780 err = flush_imsg(ibuf);
1791 const struct got_error *
1792 got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
1794 const struct got_error *err = NULL;
1795 struct got_imsg_tag_object *itag;
1798 size_t tag_len = strlen(tag->tag);
1799 size_t tagger_len = strlen(tag->tagger);
1800 size_t tagmsg_len = strlen(tag->tagmsg);
1802 total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
1804 buf = malloc(total);
1806 return got_error_from_errno("malloc");
1808 itag = (struct got_imsg_tag_object *)buf;
1809 memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
1810 itag->obj_type = tag->obj_type;
1811 itag->tag_len = tag_len;
1812 itag->tagger_len = tagger_len;
1813 itag->tagger_time = tag->tagger_time;
1814 itag->tagger_gmtoff = tag->tagger_gmtoff;
1815 itag->tagmsg_len = tagmsg_len;
1817 len = sizeof(*itag);
1818 memcpy(buf + len, tag->tag, tag_len);
1820 memcpy(buf + len, tag->tagger, tagger_len);
1823 if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
1824 err = got_error_from_errno("imsg_compose TAG");
1828 if (tagmsg_len == 0 ||
1829 tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1830 err = flush_imsg(ibuf);
1834 err = send_tagmsg(ibuf, tag, tagmsg_len);
1840 const struct got_error *
1841 got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
1843 const struct got_error *err = NULL;
1845 struct got_imsg_tag_object *itag;
1846 size_t len, datalen;
1847 const size_t min_datalen =
1848 MIN(sizeof(struct got_imsg_error),
1849 sizeof(struct got_imsg_tag_object));
1853 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1857 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1860 switch (imsg.hdr.type) {
1862 if (datalen < sizeof(*itag)) {
1863 err = got_error(GOT_ERR_PRIVSEP_LEN);
1867 if (datalen != sizeof(*itag) + itag->tag_len +
1869 err = got_error(GOT_ERR_PRIVSEP_LEN);
1872 len += sizeof(*itag);
1874 *tag = calloc(1, sizeof(**tag));
1876 err = got_error_from_errno("calloc");
1880 memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
1882 (*tag)->tag = strndup(imsg.data + len, itag->tag_len);
1883 if ((*tag)->tag == NULL) {
1884 err = got_error_from_errno("strndup");
1887 len += itag->tag_len;
1889 (*tag)->obj_type = itag->obj_type;
1890 (*tag)->tagger_time = itag->tagger_time;
1891 (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1893 (*tag)->tagger = strndup(imsg.data + len, itag->tagger_len);
1894 if ((*tag)->tagger == NULL) {
1895 err = got_error_from_errno("strndup");
1898 len += itag->tagger_len;
1900 if (itag->tagmsg_len == 0) {
1901 (*tag)->tagmsg = strdup("");
1902 if ((*tag)->tagmsg == NULL) {
1903 err = got_error_from_errno("strdup");
1907 size_t offset = 0, remain = itag->tagmsg_len;
1909 (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1910 if ((*tag)->tagmsg == NULL) {
1911 err = got_error_from_errno("malloc");
1914 while (remain > 0) {
1915 struct imsg imsg_log;
1916 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1919 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1923 if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1924 return got_error(GOT_ERR_PRIVSEP_MSG);
1926 memcpy((*tag)->tagmsg + offset, imsg_log.data,
1928 imsg_free(&imsg_log);
1932 (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1937 err = got_error(GOT_ERR_PRIVSEP_MSG);
1946 const struct got_error *
1947 got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1948 struct got_packidx *packidx)
1950 const struct got_error *err = NULL;
1951 struct got_imsg_packidx ipackidx;
1952 struct got_imsg_pack ipack;
1955 ipackidx.len = packidx->len;
1956 ipackidx.packfile_size = pack->filesize;
1957 fd = dup(packidx->fd);
1959 return got_error_from_errno("dup");
1961 if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
1962 sizeof(ipackidx)) == -1) {
1963 err = got_error_from_errno("imsg_compose PACKIDX");
1968 if (strlcpy(ipack.path_packfile, pack->path_packfile,
1969 sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
1970 return got_error(GOT_ERR_NO_SPACE);
1971 ipack.filesize = pack->filesize;
1975 return got_error_from_errno("dup");
1977 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
1979 err = got_error_from_errno("imsg_compose PACK");
1984 return flush_imsg(ibuf);
1987 const struct got_error *
1988 got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
1989 struct got_object_id *id)
1991 struct got_imsg_packed_object iobj;
1994 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
1996 if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
1997 &iobj, sizeof(iobj)) == -1)
1998 return got_error_from_errno("imsg_compose "
1999 "PACKED_OBJECT_REQUEST");
2001 return flush_imsg(ibuf);
2004 const struct got_error *
2005 got_privsep_send_packed_raw_obj_req(struct imsgbuf *ibuf, int idx,
2006 struct got_object_id *id)
2008 struct got_imsg_packed_object iobj;
2011 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
2013 if (imsg_compose(ibuf, GOT_IMSG_PACKED_RAW_OBJECT_REQUEST, 0, 0, -1,
2014 &iobj, sizeof(iobj)) == -1)
2015 return got_error_from_errno("imsg_compose "
2016 "PACKED_OBJECT_REQUEST");
2018 return flush_imsg(ibuf);
2021 const struct got_error *
2022 got_privsep_send_gitconfig_parse_req(struct imsgbuf *ibuf, int fd)
2024 const struct got_error *err = NULL;
2026 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_PARSE_REQUEST, 0, 0, fd,
2028 err = got_error_from_errno("imsg_compose "
2029 "GITCONFIG_PARSE_REQUEST");
2034 return flush_imsg(ibuf);
2037 const struct got_error *
2038 got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *ibuf)
2040 if (imsg_compose(ibuf,
2041 GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST, 0, 0, -1,
2043 return got_error_from_errno("imsg_compose "
2044 "GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST");
2046 return flush_imsg(ibuf);
2049 const struct got_error *
2050 got_privsep_send_gitconfig_repository_extensions_req(struct imsgbuf *ibuf)
2052 if (imsg_compose(ibuf,
2053 GOT_IMSG_GITCONFIG_REPOSITORY_EXTENSIONS_REQUEST, 0, 0, -1,
2055 return got_error_from_errno("imsg_compose "
2056 "GITCONFIG_REPOSITORY_EXTENSIONS_REQUEST");
2058 return flush_imsg(ibuf);
2062 const struct got_error *
2063 got_privsep_send_gitconfig_author_name_req(struct imsgbuf *ibuf)
2065 if (imsg_compose(ibuf,
2066 GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST, 0, 0, -1, NULL, 0) == -1)
2067 return got_error_from_errno("imsg_compose "
2068 "GITCONFIG_AUTHOR_NAME_REQUEST");
2070 return flush_imsg(ibuf);
2073 const struct got_error *
2074 got_privsep_send_gitconfig_author_email_req(struct imsgbuf *ibuf)
2076 if (imsg_compose(ibuf,
2077 GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST, 0, 0, -1, NULL, 0) == -1)
2078 return got_error_from_errno("imsg_compose "
2079 "GITCONFIG_AUTHOR_EMAIL_REQUEST");
2081 return flush_imsg(ibuf);
2084 const struct got_error *
2085 got_privsep_send_gitconfig_remotes_req(struct imsgbuf *ibuf)
2087 if (imsg_compose(ibuf,
2088 GOT_IMSG_GITCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
2089 return got_error_from_errno("imsg_compose "
2090 "GITCONFIG_REMOTE_REQUEST");
2092 return flush_imsg(ibuf);
2095 const struct got_error *
2096 got_privsep_send_gitconfig_owner_req(struct imsgbuf *ibuf)
2098 if (imsg_compose(ibuf,
2099 GOT_IMSG_GITCONFIG_OWNER_REQUEST, 0, 0, -1, NULL, 0) == -1)
2100 return got_error_from_errno("imsg_compose "
2101 "GITCONFIG_OWNER_REQUEST");
2103 return flush_imsg(ibuf);
2106 const struct got_error *
2107 got_privsep_recv_gitconfig_str(char **str, struct imsgbuf *ibuf)
2109 const struct got_error *err = NULL;
2112 const size_t min_datalen = 0;
2116 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2119 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2121 switch (imsg.hdr.type) {
2122 case GOT_IMSG_GITCONFIG_STR_VAL:
2125 /* datalen does not include terminating \0 */
2126 *str = malloc(datalen + 1);
2128 err = got_error_from_errno("malloc");
2131 memcpy(*str, imsg.data, datalen);
2132 (*str)[datalen] = '\0';
2135 err = got_error(GOT_ERR_PRIVSEP_MSG);
2143 const struct got_error *
2144 got_privsep_recv_gitconfig_int(int *val, struct imsgbuf *ibuf)
2146 const struct got_error *err = NULL;
2149 const size_t min_datalen =
2150 MIN(sizeof(struct got_imsg_error), sizeof(int));
2154 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2157 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2159 switch (imsg.hdr.type) {
2160 case GOT_IMSG_GITCONFIG_INT_VAL:
2161 if (datalen != sizeof(*val)) {
2162 err = got_error(GOT_ERR_PRIVSEP_LEN);
2165 memcpy(val, imsg.data, sizeof(*val));
2168 err = got_error(GOT_ERR_PRIVSEP_MSG);
2177 free_remote_data(struct got_remote_repo *remote)
2182 free(remote->fetch_url);
2183 free(remote->send_url);
2184 for (i = 0; i < remote->nfetch_branches; i++)
2185 free(remote->fetch_branches[i]);
2186 free(remote->fetch_branches);
2187 for (i = 0; i < remote->nsend_branches; i++)
2188 free(remote->send_branches[i]);
2189 free(remote->send_branches);
2190 for (i = 0; i < remote->nfetch_refs; i++)
2191 free(remote->fetch_refs[i]);
2192 free(remote->fetch_refs);
2195 const struct got_error *
2196 got_privsep_recv_gitconfig_remotes(struct got_remote_repo **remotes,
2197 int *nremotes, struct imsgbuf *ibuf)
2199 const struct got_error *err = NULL;
2202 struct got_imsg_remotes iremotes;
2203 struct got_imsg_remote iremote;
2207 iremotes.nremotes = 0;
2209 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremotes));
2212 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2214 switch (imsg.hdr.type) {
2215 case GOT_IMSG_GITCONFIG_REMOTES:
2216 if (datalen != sizeof(iremotes)) {
2217 err = got_error(GOT_ERR_PRIVSEP_LEN);
2220 memcpy(&iremotes, imsg.data, sizeof(iremotes));
2221 if (iremotes.nremotes == 0) {
2228 return got_error(GOT_ERR_PRIVSEP_MSG);
2233 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(**remotes));
2234 if (*remotes == NULL)
2235 return got_error_from_errno("recallocarray");
2237 while (*nremotes < iremotes.nremotes) {
2238 struct got_remote_repo *remote;
2240 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremote));
2243 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2245 switch (imsg.hdr.type) {
2246 case GOT_IMSG_GITCONFIG_REMOTE:
2247 remote = &(*remotes)[*nremotes];
2248 memset(remote, 0, sizeof(*remote));
2249 if (datalen < sizeof(iremote)) {
2250 err = got_error(GOT_ERR_PRIVSEP_LEN);
2253 memcpy(&iremote, imsg.data, sizeof(iremote));
2254 if (iremote.name_len == 0 ||
2255 iremote.fetch_url_len == 0 ||
2256 iremote.send_url_len == 0 ||
2257 (sizeof(iremote) + iremote.name_len +
2258 iremote.fetch_url_len + iremote.send_url_len) > datalen) {
2259 err = got_error(GOT_ERR_PRIVSEP_LEN);
2262 remote->name = strndup(imsg.data + sizeof(iremote),
2264 if (remote->name == NULL) {
2265 err = got_error_from_errno("strndup");
2268 remote->fetch_url = strndup(imsg.data + sizeof(iremote) +
2269 iremote.name_len, iremote.fetch_url_len);
2270 if (remote->fetch_url == NULL) {
2271 err = got_error_from_errno("strndup");
2272 free_remote_data(remote);
2275 remote->send_url = strndup(imsg.data + sizeof(iremote) +
2276 iremote.name_len + iremote.fetch_url_len,
2277 iremote.send_url_len);
2278 if (remote->send_url == NULL) {
2279 err = got_error_from_errno("strndup");
2280 free_remote_data(remote);
2283 remote->mirror_references = iremote.mirror_references;
2284 remote->fetch_all_branches = iremote.fetch_all_branches;
2285 remote->nfetch_branches = 0;
2286 remote->fetch_branches = NULL;
2287 remote->nsend_branches = 0;
2288 remote->send_branches = NULL;
2289 remote->nfetch_refs = 0;
2290 remote->fetch_refs = NULL;
2294 err = got_error(GOT_ERR_PRIVSEP_MSG);
2305 for (i = 0; i < *nremotes; i++)
2306 free_remote_data(&(*remotes)[i]);
2314 const struct got_error *
2315 got_privsep_send_gotconfig_parse_req(struct imsgbuf *ibuf, int fd)
2317 const struct got_error *err = NULL;
2319 if (imsg_compose(ibuf, GOT_IMSG_GOTCONFIG_PARSE_REQUEST, 0, 0, fd,
2321 err = got_error_from_errno("imsg_compose "
2322 "GOTCONFIG_PARSE_REQUEST");
2327 return flush_imsg(ibuf);
2330 const struct got_error *
2331 got_privsep_send_gotconfig_author_req(struct imsgbuf *ibuf)
2333 if (imsg_compose(ibuf,
2334 GOT_IMSG_GOTCONFIG_AUTHOR_REQUEST, 0, 0, -1, NULL, 0) == -1)
2335 return got_error_from_errno("imsg_compose "
2336 "GOTCONFIG_AUTHOR_REQUEST");
2338 return flush_imsg(ibuf);
2341 const struct got_error *
2342 got_privsep_send_gotconfig_remotes_req(struct imsgbuf *ibuf)
2344 if (imsg_compose(ibuf,
2345 GOT_IMSG_GOTCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
2346 return got_error_from_errno("imsg_compose "
2347 "GOTCONFIG_REMOTE_REQUEST");
2349 return flush_imsg(ibuf);
2352 const struct got_error *
2353 got_privsep_recv_gotconfig_str(char **str, struct imsgbuf *ibuf)
2355 const struct got_error *err = NULL;
2358 const size_t min_datalen = 0;
2362 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2365 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2367 switch (imsg.hdr.type) {
2368 case GOT_IMSG_ERROR:
2369 err = recv_imsg_error(&imsg, datalen);
2371 case GOT_IMSG_GOTCONFIG_STR_VAL:
2374 /* datalen does not include terminating \0 */
2375 *str = malloc(datalen + 1);
2377 err = got_error_from_errno("malloc");
2380 memcpy(*str, imsg.data, datalen);
2381 (*str)[datalen] = '\0';
2384 err = got_error(GOT_ERR_PRIVSEP_MSG);
2392 const struct got_error *
2393 got_privsep_recv_gotconfig_remotes(struct got_remote_repo **remotes,
2394 int *nremotes, struct imsgbuf *ibuf)
2396 const struct got_error *err = NULL;
2399 struct got_imsg_remotes iremotes;
2400 struct got_imsg_remote iremote;
2401 const size_t min_datalen =
2402 MIN(sizeof(struct got_imsg_error), sizeof(iremotes));
2406 iremotes.nremotes = 0;
2408 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2411 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2413 switch (imsg.hdr.type) {
2414 case GOT_IMSG_ERROR:
2415 err = recv_imsg_error(&imsg, datalen);
2417 case GOT_IMSG_GOTCONFIG_REMOTES:
2418 if (datalen != sizeof(iremotes)) {
2419 err = got_error(GOT_ERR_PRIVSEP_LEN);
2422 memcpy(&iremotes, imsg.data, sizeof(iremotes));
2423 if (iremotes.nremotes == 0) {
2430 return got_error(GOT_ERR_PRIVSEP_MSG);
2435 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(**remotes));
2436 if (*remotes == NULL)
2437 return got_error_from_errno("recallocarray");
2439 while (*nremotes < iremotes.nremotes) {
2440 struct got_remote_repo *remote;
2441 const size_t min_datalen =
2442 MIN(sizeof(struct got_imsg_error), sizeof(iremote));
2445 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2448 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2450 switch (imsg.hdr.type) {
2451 case GOT_IMSG_ERROR:
2452 err = recv_imsg_error(&imsg, datalen);
2454 case GOT_IMSG_GOTCONFIG_REMOTE:
2455 remote = &(*remotes)[*nremotes];
2456 memset(remote, 0, sizeof(*remote));
2457 if (datalen < sizeof(iremote)) {
2458 err = got_error(GOT_ERR_PRIVSEP_LEN);
2461 memcpy(&iremote, imsg.data, sizeof(iremote));
2462 if (iremote.name_len == 0 ||
2463 (iremote.fetch_url_len == 0 &&
2464 iremote.send_url_len == 0) ||
2465 (sizeof(iremote) + iremote.name_len +
2466 iremote.fetch_url_len + iremote.send_url_len) >
2468 err = got_error(GOT_ERR_PRIVSEP_LEN);
2471 remote->name = strndup(imsg.data + sizeof(iremote),
2473 if (remote->name == NULL) {
2474 err = got_error_from_errno("strndup");
2477 remote->fetch_url = strndup(imsg.data +
2478 sizeof(iremote) + iremote.name_len,
2479 iremote.fetch_url_len);
2480 if (remote->fetch_url == NULL) {
2481 err = got_error_from_errno("strndup");
2482 free_remote_data(remote);
2485 remote->send_url = strndup(imsg.data +
2486 sizeof(iremote) + iremote.name_len +
2487 iremote.fetch_url_len, iremote.send_url_len);
2488 if (remote->send_url == NULL) {
2489 err = got_error_from_errno("strndup");
2490 free_remote_data(remote);
2493 remote->mirror_references = iremote.mirror_references;
2494 remote->fetch_all_branches = iremote.fetch_all_branches;
2495 if (iremote.nfetch_branches > 0) {
2496 remote->fetch_branches = recallocarray(NULL, 0,
2497 iremote.nfetch_branches, sizeof(char *));
2498 if (remote->fetch_branches == NULL) {
2499 err = got_error_from_errno("calloc");
2500 free_remote_data(remote);
2504 remote->nfetch_branches = 0;
2505 for (i = 0; i < iremote.nfetch_branches; i++) {
2507 err = got_privsep_recv_gotconfig_str(&branch,
2510 free_remote_data(remote);
2513 remote->fetch_branches[i] = branch;
2514 remote->nfetch_branches++;
2516 if (iremote.nsend_branches > 0) {
2517 remote->send_branches = recallocarray(NULL, 0,
2518 iremote.nsend_branches, sizeof(char *));
2519 if (remote->send_branches == NULL) {
2520 err = got_error_from_errno("calloc");
2521 free_remote_data(remote);
2525 remote->nsend_branches = 0;
2526 for (i = 0; i < iremote.nsend_branches; i++) {
2528 err = got_privsep_recv_gotconfig_str(&branch,
2531 free_remote_data(remote);
2534 remote->send_branches[i] = branch;
2535 remote->nsend_branches++;
2537 if (iremote.nfetch_refs > 0) {
2538 remote->fetch_refs = recallocarray(NULL, 0,
2539 iremote.nfetch_refs, sizeof(char *));
2540 if (remote->fetch_refs == NULL) {
2541 err = got_error_from_errno("calloc");
2542 free_remote_data(remote);
2546 remote->nfetch_refs = 0;
2547 for (i = 0; i < iremote.nfetch_refs; i++) {
2549 err = got_privsep_recv_gotconfig_str(&ref,
2552 free_remote_data(remote);
2555 remote->fetch_refs[i] = ref;
2556 remote->nfetch_refs++;
2561 err = got_error(GOT_ERR_PRIVSEP_MSG);
2572 for (i = 0; i < *nremotes; i++)
2573 free_remote_data(&(*remotes)[i]);
2581 const struct got_error *
2582 got_privsep_send_commit_traversal_request(struct imsgbuf *ibuf,
2583 struct got_object_id *id, int idx, const char *path)
2586 size_t path_len = strlen(path) + 1;
2588 wbuf = imsg_create(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_REQUEST, 0, 0,
2589 sizeof(struct got_imsg_commit_traversal_request) + path_len);
2591 return got_error_from_errno(
2592 "imsg_create COMMIT_TRAVERSAL_REQUEST");
2593 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1)
2594 return got_error_from_errno("imsg_add "
2595 "COMMIT_TRAVERSAL_REQUEST");
2596 if (imsg_add(wbuf, &idx, sizeof(idx)) == -1)
2597 return got_error_from_errno("imsg_add "
2598 "COMMIT_TRAVERSAL_REQUEST");
2599 if (imsg_add(wbuf, path, path_len) == -1)
2600 return got_error_from_errno("imsg_add "
2601 "COMMIT_TRAVERSAL_REQUEST");
2604 imsg_close(ibuf, wbuf);
2606 return flush_imsg(ibuf);
2609 const struct got_error *
2610 got_privsep_recv_traversed_commits(struct got_commit_object **changed_commit,
2611 struct got_object_id **changed_commit_id,
2612 struct got_object_id_queue *commit_ids, struct imsgbuf *ibuf)
2614 const struct got_error *err = NULL;
2616 struct got_imsg_traversed_commits *icommits;
2620 *changed_commit = NULL;
2621 *changed_commit_id = NULL;
2624 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2628 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2629 switch (imsg.hdr.type) {
2630 case GOT_IMSG_TRAVERSED_COMMITS:
2631 icommits = imsg.data;
2632 if (datalen != sizeof(*icommits) +
2633 icommits->ncommits * SHA1_DIGEST_LENGTH) {
2634 err = got_error(GOT_ERR_PRIVSEP_LEN);
2637 for (i = 0; i < icommits->ncommits; i++) {
2638 struct got_object_qid *qid;
2639 uint8_t *sha1 = (uint8_t *)imsg.data +
2640 sizeof(*icommits) + i * SHA1_DIGEST_LENGTH;
2641 err = got_object_qid_alloc_partial(&qid);
2644 memcpy(qid->id.sha1, sha1, SHA1_DIGEST_LENGTH);
2645 STAILQ_INSERT_TAIL(commit_ids, qid, entry);
2647 /* The last commit may contain a change. */
2648 if (i == icommits->ncommits - 1) {
2649 *changed_commit_id =
2650 got_object_id_dup(&qid->id);
2651 if (*changed_commit_id == NULL) {
2652 err = got_error_from_errno(
2653 "got_object_id_dup");
2659 case GOT_IMSG_COMMIT:
2660 if (*changed_commit_id == NULL) {
2661 err = got_error(GOT_ERR_PRIVSEP_MSG);
2664 err = get_commit_from_imsg(changed_commit, &imsg,
2667 case GOT_IMSG_COMMIT_TRAVERSAL_DONE:
2671 err = got_error(GOT_ERR_PRIVSEP_MSG);
2681 got_object_id_queue_free(commit_ids);
2685 const struct got_error *
2686 got_privsep_send_enumerated_tree(size_t *totlen, struct imsgbuf *ibuf,
2687 struct got_object_id *tree_id, const char *path,
2688 struct got_parsed_tree_entry *entries, int nentries)
2690 const struct got_error *err = NULL;
2692 size_t path_len = strlen(path);
2695 msglen = sizeof(struct got_imsg_enumerated_tree) + path_len;
2696 wbuf = imsg_create(ibuf, GOT_IMSG_ENUMERATED_TREE, 0, 0, msglen);
2698 return got_error_from_errno("imsg_create ENUMERATED_TREE");
2700 if (imsg_add(wbuf, tree_id->sha1, SHA1_DIGEST_LENGTH) == -1)
2701 return got_error_from_errno("imsg_add ENUMERATED_TREE");
2702 if (imsg_add(wbuf, &nentries, sizeof(nentries)) == -1)
2703 return got_error_from_errno("imsg_add ENUMERATED_TREE");
2704 if (imsg_add(wbuf, path, path_len) == -1)
2705 return got_error_from_errno("imsg_add ENUMERATED_TREE");
2708 imsg_close(ibuf, wbuf);
2711 err = send_tree_entries(ibuf, entries, nentries);
2716 return flush_imsg(ibuf);
2719 const struct got_error *
2720 got_privsep_send_object_enumeration_request(struct imsgbuf *ibuf)
2722 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_ENUMERATION_REQUEST,
2723 0, 0, -1, NULL, 0) == -1)
2724 return got_error_from_errno("imsg_compose "
2725 "OBJECT_ENUMERATION_REQUEST");
2727 return flush_imsg(ibuf);
2730 const struct got_error *
2731 got_privsep_send_object_enumeration_done(struct imsgbuf *ibuf)
2733 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_ENUMERATION_DONE,
2734 0, 0, -1, NULL, 0) == -1)
2735 return got_error_from_errno("imsg_compose "
2736 "OBJECT_ENUMERATION_DONE");
2738 return flush_imsg(ibuf);
2741 const struct got_error *
2742 got_privsep_send_enumerated_commit(struct imsgbuf *ibuf,
2743 struct got_object_id *id, time_t mtime)
2747 wbuf = imsg_create(ibuf, GOT_IMSG_ENUMERATED_COMMIT, 0, 0,
2748 sizeof(struct got_imsg_enumerated_commit) + SHA1_DIGEST_LENGTH);
2750 return got_error_from_errno("imsg_create ENUMERATED_COMMIT");
2752 /* Keep in sync with struct got_imsg_enumerated_commit! */
2753 if (imsg_add(wbuf, id, SHA1_DIGEST_LENGTH) == -1)
2754 return got_error_from_errno("imsg_add ENUMERATED_COMMIT");
2755 if (imsg_add(wbuf, &mtime, sizeof(mtime)) == -1)
2756 return got_error_from_errno("imsg_add ENUMERATED_COMMIT");
2759 imsg_close(ibuf, wbuf);
2760 /* Don't flush yet, tree entries or ENUMERATION_DONE will follow. */
2764 const struct got_error *
2765 got_privsep_recv_enumerated_objects(struct imsgbuf *ibuf,
2766 got_object_enumerate_commit_cb cb_commit,
2767 got_object_enumerate_tree_cb cb_tree, void *cb_arg,
2768 struct got_repository *repo)
2770 const struct got_error *err = NULL;
2772 struct got_imsg_enumerated_commit *icommit = NULL;
2773 struct got_object_id commit_id;
2774 int have_commit = 0;
2776 struct got_tree_object tree;
2777 struct got_imsg_enumerated_tree *itree;
2778 struct got_object_id tree_id;
2779 char *path = NULL, *canon_path = NULL;
2780 size_t datalen, path_len;
2784 memset(&tree, 0, sizeof(tree));
2787 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2791 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2792 switch (imsg.hdr.type) {
2793 case GOT_IMSG_ENUMERATED_COMMIT:
2794 if (have_commit && nentries != -1) {
2795 err = got_error(GOT_ERR_PRIVSEP_MSG);
2798 if (datalen != sizeof(*icommit)) {
2799 err = got_error(GOT_ERR_PRIVSEP_LEN);
2802 icommit = (struct got_imsg_enumerated_commit *)imsg.data;
2803 memcpy(commit_id.sha1, icommit->id, SHA1_DIGEST_LENGTH);
2804 mtime = icommit->mtime;
2807 case GOT_IMSG_ENUMERATED_TREE:
2808 /* Should be preceeded by GOT_IMSG_ENUMERATED_COMMIT. */
2810 err = got_error(GOT_ERR_PRIVSEP_MSG);
2813 if (datalen < sizeof(*itree)) {
2814 err = got_error(GOT_ERR_PRIVSEP_LEN);
2818 path_len = datalen - sizeof(*itree);
2819 if (path_len == 0) {
2820 err = got_error(GOT_ERR_PRIVSEP_LEN);
2823 memcpy(tree_id.sha1, itree->id, sizeof(tree_id.sha1));
2825 path = malloc(path_len + 1);
2827 err = got_error_from_errno("malloc");
2831 canon_path = malloc(path_len + 1);
2832 if (canon_path == NULL) {
2833 err = got_error_from_errno("malloc");
2836 memcpy(path, (uint8_t *)imsg.data + sizeof(*itree),
2838 path[path_len] = '\0';
2839 if (!got_path_is_absolute(path)) {
2840 err = got_error(GOT_ERR_BAD_PATH);
2843 if (got_path_is_root_dir(path)) {
2844 /* XXX check what got_canonpath() does wrong */
2845 canon_path[0] = '/';
2846 canon_path[1] = '\0';
2848 err = got_canonpath(path, canon_path,
2853 if (strcmp(path, canon_path) != 0) {
2854 err = got_error(GOT_ERR_BAD_PATH);
2857 if (nentries != -1) {
2858 err = got_error(GOT_ERR_PRIVSEP_MSG);
2861 if (itree->nentries < -1) {
2862 err = got_error(GOT_ERR_PRIVSEP_MSG);
2865 if (itree->nentries == -1) {
2866 /* Tree was not found in pack file. */
2868 err = cb_tree(cb_arg, NULL, mtime, &tree_id,
2872 if (itree->nentries > INT_MAX) {
2873 err = got_error(GOT_ERR_PRIVSEP_LEN);
2876 tree.entries = calloc(itree->nentries,
2877 sizeof(struct got_tree_entry));
2878 if (tree.entries == NULL) {
2879 err = got_error_from_errno("calloc");
2882 if (itree->nentries == 0) {
2883 err = cb_tree(cb_arg, &tree, mtime, &tree_id,
2888 /* Prepare for next tree. */
2890 memset(&tree, 0, sizeof(tree));
2893 tree.nentries = itree->nentries;
2897 case GOT_IMSG_TREE_ENTRIES:
2898 /* Should be preceeded by GOT_IMSG_ENUMERATED_TREE. */
2899 if (nentries <= -1) {
2900 err = got_error(GOT_ERR_PRIVSEP_MSG);
2903 err = recv_tree_entries(imsg.data, datalen,
2907 if (tree.nentries == nentries) {
2908 err = cb_tree(cb_arg, &tree, mtime, &tree_id,
2913 /* Prepare for next tree. */
2915 memset(&tree, 0, sizeof(tree));
2919 case GOT_IMSG_TREE_ENUMERATION_DONE:
2920 /* All trees have been found and traversed. */
2921 if (!have_commit || path == NULL || nentries != -1) {
2922 err = got_error(GOT_ERR_PRIVSEP_MSG);
2925 err = cb_commit(cb_arg, mtime, &commit_id, repo);
2930 case GOT_IMSG_OBJECT_ENUMERATION_DONE:
2934 err = got_error(GOT_ERR_PRIVSEP_MSG);
2949 const struct got_error *
2950 got_privsep_send_raw_delta_req(struct imsgbuf *ibuf, int idx,
2951 struct got_object_id *id)
2953 struct got_imsg_raw_delta_request dreq;
2956 memcpy(dreq.id, id->sha1, SHA1_DIGEST_LENGTH);
2958 if (imsg_compose(ibuf, GOT_IMSG_RAW_DELTA_REQUEST, 0, 0, -1,
2959 &dreq, sizeof(dreq)) == -1)
2960 return got_error_from_errno("imsg_compose RAW_DELTA_REQUEST");
2962 return flush_imsg(ibuf);
2965 const struct got_error *
2966 got_privsep_send_raw_delta_outfd(struct imsgbuf *ibuf, int fd)
2968 return send_fd(ibuf, GOT_IMSG_RAW_DELTA_OUTFD, fd);
2971 const struct got_error *
2972 got_privsep_send_raw_delta(struct imsgbuf *ibuf, uint64_t base_size,
2973 uint64_t result_size, off_t delta_size, off_t delta_compressed_size,
2974 off_t delta_offset, off_t delta_out_offset, struct got_object_id *base_id)
2976 struct got_imsg_raw_delta idelta;
2979 idelta.base_size = base_size;
2980 idelta.result_size = result_size;
2981 idelta.delta_size = delta_size;
2982 idelta.delta_compressed_size = delta_compressed_size;
2983 idelta.delta_offset = delta_offset;
2984 idelta.delta_out_offset = delta_out_offset;
2985 memcpy(idelta.base_id, base_id->sha1, SHA1_DIGEST_LENGTH);
2987 ret = imsg_compose(ibuf, GOT_IMSG_RAW_DELTA, 0, 0, -1,
2988 &idelta, sizeof(idelta));
2990 return got_error_from_errno("imsg_compose RAW_DELTA");
2992 return flush_imsg(ibuf);
2995 const struct got_error *
2996 got_privsep_recv_raw_delta(uint64_t *base_size, uint64_t *result_size,
2997 off_t *delta_size, off_t *delta_compressed_size, off_t *delta_offset,
2998 off_t *delta_out_offset, struct got_object_id **base_id, struct imsgbuf *ibuf)
3000 const struct got_error *err = NULL;
3002 struct got_imsg_raw_delta *delta;
3008 *delta_compressed_size = 0;
3010 *delta_out_offset = 0;
3013 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
3017 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
3019 switch (imsg.hdr.type) {
3020 case GOT_IMSG_RAW_DELTA:
3021 if (datalen != sizeof(*delta)) {
3022 err = got_error(GOT_ERR_PRIVSEP_LEN);
3026 *base_size = delta->base_size;
3027 *result_size = delta->result_size;
3028 *delta_size = delta->delta_size;
3029 *delta_compressed_size = delta->delta_compressed_size;
3030 *delta_offset = delta->delta_offset;
3031 *delta_out_offset = delta->delta_out_offset;
3032 *base_id = calloc(1, sizeof(**base_id));
3033 if (*base_id == NULL) {
3034 err = got_error_from_errno("malloc");
3037 memcpy((*base_id)->sha1, delta->base_id, SHA1_DIGEST_LENGTH);
3040 err = got_error(GOT_ERR_PRIVSEP_MSG);
3053 static const struct got_error *
3054 send_idlist(struct imsgbuf *ibuf, struct got_object_id **ids, size_t nids)
3056 const struct got_error *err = NULL;
3057 struct got_imsg_object_idlist idlist;
3061 if (nids > GOT_IMSG_OBJ_ID_LIST_MAX_NIDS)
3062 return got_error(GOT_ERR_NO_SPACE);
3064 wbuf = imsg_create(ibuf, GOT_IMSG_OBJ_ID_LIST, 0, 0,
3065 sizeof(idlist) + nids * sizeof(**ids));
3067 err = got_error_from_errno("imsg_create OBJ_ID_LIST");
3072 if (imsg_add(wbuf, &idlist, sizeof(idlist)) == -1)
3073 return got_error_from_errno("imsg_add OBJ_ID_LIST");
3075 for (i = 0; i < nids; i++) {
3076 struct got_object_id *id = ids[i];
3077 if (imsg_add(wbuf, id, sizeof(*id)) == -1)
3078 return got_error_from_errno("imsg_add OBJ_ID_LIST");
3082 imsg_close(ibuf, wbuf);
3084 return flush_imsg(ibuf);
3087 const struct got_error *
3088 got_privsep_send_object_idlist(struct imsgbuf *ibuf,
3089 struct got_object_id **ids, size_t nids)
3091 const struct got_error *err = NULL;
3092 struct got_object_id *idlist[GOT_IMSG_OBJ_ID_LIST_MAX_NIDS];
3095 for (i = 0; i < nids; i++) {
3096 j = i % nitems(idlist);
3098 if (j >= nitems(idlist) - 1) {
3099 err = send_idlist(ibuf, idlist, j + 1);
3107 err = send_idlist(ibuf, idlist, j + 1);
3115 const struct got_error *
3116 got_privsep_send_object_idlist_done(struct imsgbuf *ibuf)
3118 if (imsg_compose(ibuf, GOT_IMSG_OBJ_ID_LIST_DONE, 0, 0, -1, NULL, 0)
3120 return got_error_from_errno("imsg_compose OBJ_ID_LIST_DONE");
3122 return flush_imsg(ibuf);
3125 const struct got_error *
3126 got_privsep_recv_object_idlist(int *done, struct got_object_id **ids,
3127 size_t *nids, struct imsgbuf *ibuf)
3129 const struct got_error *err = NULL;
3131 struct got_imsg_object_idlist *idlist;
3138 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
3142 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
3143 switch (imsg.hdr.type) {
3144 case GOT_IMSG_OBJ_ID_LIST:
3145 if (datalen < sizeof(*idlist)) {
3146 err = got_error(GOT_ERR_PRIVSEP_LEN);
3150 if (idlist->nids > GOT_IMSG_OBJ_ID_LIST_MAX_NIDS) {
3151 err = got_error(GOT_ERR_PRIVSEP_LEN);
3154 *nids = idlist->nids;
3155 *ids = calloc(*nids, sizeof(**ids));
3157 err = got_error_from_errno("calloc");
3160 memcpy(*ids, (uint8_t *)imsg.data + sizeof(idlist),
3161 *nids * sizeof(**ids));
3163 case GOT_IMSG_OBJ_ID_LIST_DONE:
3167 err = got_error(GOT_ERR_PRIVSEP_MSG);
3176 const struct got_error *
3177 got_privsep_send_delta_reuse_req(struct imsgbuf *ibuf)
3179 if (imsg_compose(ibuf, GOT_IMSG_DELTA_REUSE_REQUEST, 0, 0, -1, NULL, 0)
3181 return got_error_from_errno("imsg_compose DELTA_REUSE_REQUEST");
3183 return flush_imsg(ibuf);
3186 const struct got_error *
3187 got_privsep_send_reused_deltas(struct imsgbuf *ibuf,
3188 struct got_imsg_reused_delta *deltas, size_t ndeltas)
3190 const struct got_error *err = NULL;
3192 struct got_imsg_reused_deltas ideltas;
3195 if (ndeltas > GOT_IMSG_REUSED_DELTAS_MAX_NDELTAS)
3196 return got_error(GOT_ERR_NO_SPACE);
3198 wbuf = imsg_create(ibuf, GOT_IMSG_REUSED_DELTAS, 0, 0,
3199 sizeof(ideltas) + ndeltas * sizeof(*deltas));
3201 err = got_error_from_errno("imsg_create REUSED_DELTAS");
3205 ideltas.ndeltas = ndeltas;
3206 if (imsg_add(wbuf, &ideltas, sizeof(ideltas)) == -1)
3207 return got_error_from_errno("imsg_add REUSED_DELTAS");
3209 for (i = 0; i < ndeltas; i++) {
3210 struct got_imsg_reused_delta *delta = &deltas[i];
3211 if (imsg_add(wbuf, delta, sizeof(*delta)) == -1)
3212 return got_error_from_errno("imsg_add REUSED_DELTAS");
3216 imsg_close(ibuf, wbuf);
3218 return flush_imsg(ibuf);
3221 const struct got_error *
3222 got_privsep_send_reused_deltas_done(struct imsgbuf *ibuf)
3224 if (imsg_compose(ibuf, GOT_IMSG_DELTA_REUSE_DONE, 0, 0, -1, NULL, 0)
3226 return got_error_from_errno("imsg_compose DELTA_REUSE_DONE");
3228 return flush_imsg(ibuf);
3231 const struct got_error *
3232 got_privsep_recv_reused_deltas(int *done, struct got_imsg_reused_delta *deltas,
3233 size_t *ndeltas, struct imsgbuf *ibuf)
3235 const struct got_error *err = NULL;
3237 struct got_imsg_reused_deltas *ideltas;
3243 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
3247 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
3248 switch (imsg.hdr.type) {
3249 case GOT_IMSG_REUSED_DELTAS:
3250 if (datalen < sizeof(*ideltas)) {
3251 err = got_error(GOT_ERR_PRIVSEP_LEN);
3254 ideltas = imsg.data;
3255 if (ideltas->ndeltas > GOT_IMSG_OBJ_ID_LIST_MAX_NIDS) {
3256 err = got_error(GOT_ERR_PRIVSEP_LEN);
3259 *ndeltas = ideltas->ndeltas;
3260 memcpy(deltas, (uint8_t *)imsg.data + sizeof(ideltas),
3261 *ndeltas * sizeof(*deltas));
3263 case GOT_IMSG_DELTA_REUSE_DONE:
3267 err = got_error(GOT_ERR_PRIVSEP_MSG);
3276 const struct got_error *
3277 got_privsep_unveil_exec_helpers(void)
3279 const char *helpers[] = {
3280 GOT_PATH_PROG_READ_PACK,
3281 GOT_PATH_PROG_READ_OBJECT,
3282 GOT_PATH_PROG_READ_COMMIT,
3283 GOT_PATH_PROG_READ_TREE,
3284 GOT_PATH_PROG_READ_BLOB,
3285 GOT_PATH_PROG_READ_TAG,
3286 GOT_PATH_PROG_READ_GITCONFIG,
3287 GOT_PATH_PROG_READ_GOTCONFIG,
3288 GOT_PATH_PROG_READ_PATCH,
3289 GOT_PATH_PROG_FETCH_PACK,
3290 GOT_PATH_PROG_INDEX_PACK,
3291 GOT_PATH_PROG_SEND_PACK,
3295 for (i = 0; i < nitems(helpers); i++) {
3296 if (unveil(helpers[i], "x") == 0)
3298 return got_error_from_errno2("unveil", helpers[i]);
3305 got_privsep_exec_child(int imsg_fds[2], const char *path, const char *repo_path)
3307 if (close(imsg_fds[0]) == -1) {
3308 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
3312 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
3313 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
3317 closefrom(GOT_IMSG_FD_CHILD + 1);
3319 if (execl(path, path, repo_path, (char *)NULL) == -1) {
3320 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,