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;
2115 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2118 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2120 switch (imsg.hdr.type) {
2121 case GOT_IMSG_GITCONFIG_STR_VAL:
2124 /* datalen does not include terminating \0 */
2125 *str = malloc(datalen + 1);
2127 err = got_error_from_errno("malloc");
2130 memcpy(*str, imsg.data, datalen);
2131 (*str)[datalen] = '\0';
2134 err = got_error(GOT_ERR_PRIVSEP_MSG);
2142 const struct got_error *
2143 got_privsep_recv_gitconfig_int(int *val, struct imsgbuf *ibuf)
2145 const struct got_error *err = NULL;
2148 const size_t min_datalen =
2149 MIN(sizeof(struct got_imsg_error), sizeof(int));
2153 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2156 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2158 switch (imsg.hdr.type) {
2159 case GOT_IMSG_GITCONFIG_INT_VAL:
2160 if (datalen != sizeof(*val)) {
2161 err = got_error(GOT_ERR_PRIVSEP_LEN);
2164 memcpy(val, imsg.data, sizeof(*val));
2167 err = got_error(GOT_ERR_PRIVSEP_MSG);
2176 free_remote_data(struct got_remote_repo *remote)
2181 free(remote->fetch_url);
2182 free(remote->send_url);
2183 for (i = 0; i < remote->nfetch_branches; i++)
2184 free(remote->fetch_branches[i]);
2185 free(remote->fetch_branches);
2186 for (i = 0; i < remote->nsend_branches; i++)
2187 free(remote->send_branches[i]);
2188 free(remote->send_branches);
2189 for (i = 0; i < remote->nfetch_refs; i++)
2190 free(remote->fetch_refs[i]);
2191 free(remote->fetch_refs);
2194 const struct got_error *
2195 got_privsep_recv_gitconfig_remotes(struct got_remote_repo **remotes,
2196 int *nremotes, struct imsgbuf *ibuf)
2198 const struct got_error *err = NULL;
2201 struct got_imsg_remotes iremotes;
2202 struct got_imsg_remote iremote;
2206 iremotes.nremotes = 0;
2208 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremotes));
2211 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2213 switch (imsg.hdr.type) {
2214 case GOT_IMSG_GITCONFIG_REMOTES:
2215 if (datalen != sizeof(iremotes)) {
2216 err = got_error(GOT_ERR_PRIVSEP_LEN);
2219 memcpy(&iremotes, imsg.data, sizeof(iremotes));
2220 if (iremotes.nremotes == 0) {
2227 return got_error(GOT_ERR_PRIVSEP_MSG);
2232 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(**remotes));
2233 if (*remotes == NULL)
2234 return got_error_from_errno("recallocarray");
2236 while (*nremotes < iremotes.nremotes) {
2237 struct got_remote_repo *remote;
2239 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremote));
2242 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2244 switch (imsg.hdr.type) {
2245 case GOT_IMSG_GITCONFIG_REMOTE:
2246 remote = &(*remotes)[*nremotes];
2247 memset(remote, 0, sizeof(*remote));
2248 if (datalen < sizeof(iremote)) {
2249 err = got_error(GOT_ERR_PRIVSEP_LEN);
2252 memcpy(&iremote, imsg.data, sizeof(iremote));
2253 if (iremote.name_len == 0 ||
2254 iremote.fetch_url_len == 0 ||
2255 iremote.send_url_len == 0 ||
2256 (sizeof(iremote) + iremote.name_len +
2257 iremote.fetch_url_len + iremote.send_url_len) > datalen) {
2258 err = got_error(GOT_ERR_PRIVSEP_LEN);
2261 remote->name = strndup(imsg.data + sizeof(iremote),
2263 if (remote->name == NULL) {
2264 err = got_error_from_errno("strndup");
2267 remote->fetch_url = strndup(imsg.data + sizeof(iremote) +
2268 iremote.name_len, iremote.fetch_url_len);
2269 if (remote->fetch_url == NULL) {
2270 err = got_error_from_errno("strndup");
2271 free_remote_data(remote);
2274 remote->send_url = strndup(imsg.data + sizeof(iremote) +
2275 iremote.name_len + iremote.fetch_url_len,
2276 iremote.send_url_len);
2277 if (remote->send_url == NULL) {
2278 err = got_error_from_errno("strndup");
2279 free_remote_data(remote);
2282 remote->mirror_references = iremote.mirror_references;
2283 remote->fetch_all_branches = iremote.fetch_all_branches;
2284 remote->nfetch_branches = 0;
2285 remote->fetch_branches = NULL;
2286 remote->nsend_branches = 0;
2287 remote->send_branches = NULL;
2288 remote->nfetch_refs = 0;
2289 remote->fetch_refs = NULL;
2293 err = got_error(GOT_ERR_PRIVSEP_MSG);
2304 for (i = 0; i < *nremotes; i++)
2305 free_remote_data(&(*remotes)[i]);
2313 const struct got_error *
2314 got_privsep_send_gotconfig_parse_req(struct imsgbuf *ibuf, int fd)
2316 const struct got_error *err = NULL;
2318 if (imsg_compose(ibuf, GOT_IMSG_GOTCONFIG_PARSE_REQUEST, 0, 0, fd,
2320 err = got_error_from_errno("imsg_compose "
2321 "GOTCONFIG_PARSE_REQUEST");
2326 return flush_imsg(ibuf);
2329 const struct got_error *
2330 got_privsep_send_gotconfig_author_req(struct imsgbuf *ibuf)
2332 if (imsg_compose(ibuf,
2333 GOT_IMSG_GOTCONFIG_AUTHOR_REQUEST, 0, 0, -1, NULL, 0) == -1)
2334 return got_error_from_errno("imsg_compose "
2335 "GOTCONFIG_AUTHOR_REQUEST");
2337 return flush_imsg(ibuf);
2340 const struct got_error *
2341 got_privsep_send_gotconfig_remotes_req(struct imsgbuf *ibuf)
2343 if (imsg_compose(ibuf,
2344 GOT_IMSG_GOTCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
2345 return got_error_from_errno("imsg_compose "
2346 "GOTCONFIG_REMOTE_REQUEST");
2348 return flush_imsg(ibuf);
2351 const struct got_error *
2352 got_privsep_recv_gotconfig_str(char **str, struct imsgbuf *ibuf)
2354 const struct got_error *err = NULL;
2360 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2363 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2365 switch (imsg.hdr.type) {
2366 case GOT_IMSG_ERROR:
2367 err = recv_imsg_error(&imsg, datalen);
2369 case GOT_IMSG_GOTCONFIG_STR_VAL:
2372 /* datalen does not include terminating \0 */
2373 *str = malloc(datalen + 1);
2375 err = got_error_from_errno("malloc");
2378 memcpy(*str, imsg.data, datalen);
2379 (*str)[datalen] = '\0';
2382 err = got_error(GOT_ERR_PRIVSEP_MSG);
2390 const struct got_error *
2391 got_privsep_recv_gotconfig_remotes(struct got_remote_repo **remotes,
2392 int *nremotes, struct imsgbuf *ibuf)
2394 const struct got_error *err = NULL;
2397 struct got_imsg_remotes iremotes;
2398 struct got_imsg_remote iremote;
2399 const size_t min_datalen =
2400 MIN(sizeof(struct got_imsg_error), sizeof(iremotes));
2404 iremotes.nremotes = 0;
2406 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2409 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2411 switch (imsg.hdr.type) {
2412 case GOT_IMSG_ERROR:
2413 err = recv_imsg_error(&imsg, datalen);
2415 case GOT_IMSG_GOTCONFIG_REMOTES:
2416 if (datalen != sizeof(iremotes)) {
2417 err = got_error(GOT_ERR_PRIVSEP_LEN);
2420 memcpy(&iremotes, imsg.data, sizeof(iremotes));
2421 if (iremotes.nremotes == 0) {
2428 return got_error(GOT_ERR_PRIVSEP_MSG);
2433 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(**remotes));
2434 if (*remotes == NULL)
2435 return got_error_from_errno("recallocarray");
2437 while (*nremotes < iremotes.nremotes) {
2438 struct got_remote_repo *remote;
2439 const size_t min_datalen =
2440 MIN(sizeof(struct got_imsg_error), sizeof(iremote));
2443 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2446 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2448 switch (imsg.hdr.type) {
2449 case GOT_IMSG_ERROR:
2450 err = recv_imsg_error(&imsg, datalen);
2452 case GOT_IMSG_GOTCONFIG_REMOTE:
2453 remote = &(*remotes)[*nremotes];
2454 memset(remote, 0, sizeof(*remote));
2455 if (datalen < sizeof(iremote)) {
2456 err = got_error(GOT_ERR_PRIVSEP_LEN);
2459 memcpy(&iremote, imsg.data, sizeof(iremote));
2460 if (iremote.name_len == 0 ||
2461 (iremote.fetch_url_len == 0 &&
2462 iremote.send_url_len == 0) ||
2463 (sizeof(iremote) + iremote.name_len +
2464 iremote.fetch_url_len + iremote.send_url_len) >
2466 err = got_error(GOT_ERR_PRIVSEP_LEN);
2469 remote->name = strndup(imsg.data + sizeof(iremote),
2471 if (remote->name == NULL) {
2472 err = got_error_from_errno("strndup");
2475 remote->fetch_url = strndup(imsg.data +
2476 sizeof(iremote) + iremote.name_len,
2477 iremote.fetch_url_len);
2478 if (remote->fetch_url == NULL) {
2479 err = got_error_from_errno("strndup");
2480 free_remote_data(remote);
2483 remote->send_url = strndup(imsg.data +
2484 sizeof(iremote) + iremote.name_len +
2485 iremote.fetch_url_len, iremote.send_url_len);
2486 if (remote->send_url == NULL) {
2487 err = got_error_from_errno("strndup");
2488 free_remote_data(remote);
2491 remote->mirror_references = iremote.mirror_references;
2492 remote->fetch_all_branches = iremote.fetch_all_branches;
2493 if (iremote.nfetch_branches > 0) {
2494 remote->fetch_branches = recallocarray(NULL, 0,
2495 iremote.nfetch_branches, sizeof(char *));
2496 if (remote->fetch_branches == NULL) {
2497 err = got_error_from_errno("calloc");
2498 free_remote_data(remote);
2502 remote->nfetch_branches = 0;
2503 for (i = 0; i < iremote.nfetch_branches; i++) {
2505 err = got_privsep_recv_gotconfig_str(&branch,
2508 free_remote_data(remote);
2511 remote->fetch_branches[i] = branch;
2512 remote->nfetch_branches++;
2514 if (iremote.nsend_branches > 0) {
2515 remote->send_branches = recallocarray(NULL, 0,
2516 iremote.nsend_branches, sizeof(char *));
2517 if (remote->send_branches == NULL) {
2518 err = got_error_from_errno("calloc");
2519 free_remote_data(remote);
2523 remote->nsend_branches = 0;
2524 for (i = 0; i < iremote.nsend_branches; i++) {
2526 err = got_privsep_recv_gotconfig_str(&branch,
2529 free_remote_data(remote);
2532 remote->send_branches[i] = branch;
2533 remote->nsend_branches++;
2535 if (iremote.nfetch_refs > 0) {
2536 remote->fetch_refs = recallocarray(NULL, 0,
2537 iremote.nfetch_refs, sizeof(char *));
2538 if (remote->fetch_refs == NULL) {
2539 err = got_error_from_errno("calloc");
2540 free_remote_data(remote);
2544 remote->nfetch_refs = 0;
2545 for (i = 0; i < iremote.nfetch_refs; i++) {
2547 err = got_privsep_recv_gotconfig_str(&ref,
2550 free_remote_data(remote);
2553 remote->fetch_refs[i] = ref;
2554 remote->nfetch_refs++;
2559 err = got_error(GOT_ERR_PRIVSEP_MSG);
2570 for (i = 0; i < *nremotes; i++)
2571 free_remote_data(&(*remotes)[i]);
2579 const struct got_error *
2580 got_privsep_send_commit_traversal_request(struct imsgbuf *ibuf,
2581 struct got_object_id *id, int idx, const char *path)
2584 size_t path_len = strlen(path) + 1;
2586 wbuf = imsg_create(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_REQUEST, 0, 0,
2587 sizeof(struct got_imsg_commit_traversal_request) + path_len);
2589 return got_error_from_errno(
2590 "imsg_create COMMIT_TRAVERSAL_REQUEST");
2591 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1)
2592 return got_error_from_errno("imsg_add "
2593 "COMMIT_TRAVERSAL_REQUEST");
2594 if (imsg_add(wbuf, &idx, sizeof(idx)) == -1)
2595 return got_error_from_errno("imsg_add "
2596 "COMMIT_TRAVERSAL_REQUEST");
2597 if (imsg_add(wbuf, path, path_len) == -1)
2598 return got_error_from_errno("imsg_add "
2599 "COMMIT_TRAVERSAL_REQUEST");
2602 imsg_close(ibuf, wbuf);
2604 return flush_imsg(ibuf);
2607 const struct got_error *
2608 got_privsep_recv_traversed_commits(struct got_commit_object **changed_commit,
2609 struct got_object_id **changed_commit_id,
2610 struct got_object_id_queue *commit_ids, struct imsgbuf *ibuf)
2612 const struct got_error *err = NULL;
2614 struct got_imsg_traversed_commits *icommits;
2618 *changed_commit = NULL;
2619 *changed_commit_id = NULL;
2622 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2626 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2627 switch (imsg.hdr.type) {
2628 case GOT_IMSG_TRAVERSED_COMMITS:
2629 icommits = imsg.data;
2630 if (datalen != sizeof(*icommits) +
2631 icommits->ncommits * SHA1_DIGEST_LENGTH) {
2632 err = got_error(GOT_ERR_PRIVSEP_LEN);
2635 for (i = 0; i < icommits->ncommits; i++) {
2636 struct got_object_qid *qid;
2637 uint8_t *sha1 = (uint8_t *)imsg.data +
2638 sizeof(*icommits) + i * SHA1_DIGEST_LENGTH;
2639 err = got_object_qid_alloc_partial(&qid);
2642 memcpy(qid->id.sha1, sha1, SHA1_DIGEST_LENGTH);
2643 STAILQ_INSERT_TAIL(commit_ids, qid, entry);
2645 /* The last commit may contain a change. */
2646 if (i == icommits->ncommits - 1) {
2647 *changed_commit_id =
2648 got_object_id_dup(&qid->id);
2649 if (*changed_commit_id == NULL) {
2650 err = got_error_from_errno(
2651 "got_object_id_dup");
2657 case GOT_IMSG_COMMIT:
2658 if (*changed_commit_id == NULL) {
2659 err = got_error(GOT_ERR_PRIVSEP_MSG);
2662 err = get_commit_from_imsg(changed_commit, &imsg,
2665 case GOT_IMSG_COMMIT_TRAVERSAL_DONE:
2669 err = got_error(GOT_ERR_PRIVSEP_MSG);
2679 got_object_id_queue_free(commit_ids);
2683 const struct got_error *
2684 got_privsep_send_enumerated_tree(size_t *totlen, struct imsgbuf *ibuf,
2685 struct got_object_id *tree_id, const char *path,
2686 struct got_parsed_tree_entry *entries, int nentries)
2688 const struct got_error *err = NULL;
2690 size_t path_len = strlen(path);
2693 msglen = sizeof(struct got_imsg_enumerated_tree) + path_len;
2694 wbuf = imsg_create(ibuf, GOT_IMSG_ENUMERATED_TREE, 0, 0, msglen);
2696 return got_error_from_errno("imsg_create ENUMERATED_TREE");
2698 if (imsg_add(wbuf, tree_id->sha1, SHA1_DIGEST_LENGTH) == -1)
2699 return got_error_from_errno("imsg_add ENUMERATED_TREE");
2700 if (imsg_add(wbuf, &nentries, sizeof(nentries)) == -1)
2701 return got_error_from_errno("imsg_add ENUMERATED_TREE");
2702 if (imsg_add(wbuf, path, path_len) == -1)
2703 return got_error_from_errno("imsg_add ENUMERATED_TREE");
2706 imsg_close(ibuf, wbuf);
2709 err = send_tree_entries(ibuf, entries, nentries);
2714 return flush_imsg(ibuf);
2717 const struct got_error *
2718 got_privsep_send_object_enumeration_request(struct imsgbuf *ibuf)
2720 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_ENUMERATION_REQUEST,
2721 0, 0, -1, NULL, 0) == -1)
2722 return got_error_from_errno("imsg_compose "
2723 "OBJECT_ENUMERATION_REQUEST");
2725 return flush_imsg(ibuf);
2728 const struct got_error *
2729 got_privsep_send_object_enumeration_done(struct imsgbuf *ibuf)
2731 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_ENUMERATION_DONE,
2732 0, 0, -1, NULL, 0) == -1)
2733 return got_error_from_errno("imsg_compose "
2734 "OBJECT_ENUMERATION_DONE");
2736 return flush_imsg(ibuf);
2739 const struct got_error *
2740 got_privsep_send_enumerated_commit(struct imsgbuf *ibuf,
2741 struct got_object_id *id, time_t mtime)
2745 wbuf = imsg_create(ibuf, GOT_IMSG_ENUMERATED_COMMIT, 0, 0,
2746 sizeof(struct got_imsg_enumerated_commit) + SHA1_DIGEST_LENGTH);
2748 return got_error_from_errno("imsg_create ENUMERATED_COMMIT");
2750 /* Keep in sync with struct got_imsg_enumerated_commit! */
2751 if (imsg_add(wbuf, id, SHA1_DIGEST_LENGTH) == -1)
2752 return got_error_from_errno("imsg_add ENUMERATED_COMMIT");
2753 if (imsg_add(wbuf, &mtime, sizeof(mtime)) == -1)
2754 return got_error_from_errno("imsg_add ENUMERATED_COMMIT");
2757 imsg_close(ibuf, wbuf);
2758 /* Don't flush yet, tree entries or ENUMERATION_DONE will follow. */
2762 const struct got_error *
2763 got_privsep_recv_enumerated_objects(struct imsgbuf *ibuf,
2764 got_object_enumerate_commit_cb cb_commit,
2765 got_object_enumerate_tree_cb cb_tree, void *cb_arg,
2766 struct got_repository *repo)
2768 const struct got_error *err = NULL;
2770 struct got_imsg_enumerated_commit *icommit = NULL;
2771 struct got_object_id commit_id;
2772 int have_commit = 0;
2774 struct got_tree_object tree;
2775 struct got_imsg_enumerated_tree *itree;
2776 struct got_object_id tree_id;
2777 char *path = NULL, *canon_path = NULL;
2778 size_t datalen, path_len;
2782 memset(&tree, 0, sizeof(tree));
2785 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2789 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2790 switch (imsg.hdr.type) {
2791 case GOT_IMSG_ENUMERATED_COMMIT:
2792 if (have_commit && nentries != -1) {
2793 err = got_error(GOT_ERR_PRIVSEP_MSG);
2796 if (datalen != sizeof(*icommit)) {
2797 err = got_error(GOT_ERR_PRIVSEP_LEN);
2800 icommit = (struct got_imsg_enumerated_commit *)imsg.data;
2801 memcpy(commit_id.sha1, icommit->id, SHA1_DIGEST_LENGTH);
2802 mtime = icommit->mtime;
2805 case GOT_IMSG_ENUMERATED_TREE:
2806 /* Should be preceeded by GOT_IMSG_ENUMERATED_COMMIT. */
2808 err = got_error(GOT_ERR_PRIVSEP_MSG);
2811 if (datalen < sizeof(*itree)) {
2812 err = got_error(GOT_ERR_PRIVSEP_LEN);
2816 path_len = datalen - sizeof(*itree);
2817 if (path_len == 0) {
2818 err = got_error(GOT_ERR_PRIVSEP_LEN);
2821 memcpy(tree_id.sha1, itree->id, sizeof(tree_id.sha1));
2823 path = malloc(path_len + 1);
2825 err = got_error_from_errno("malloc");
2829 canon_path = malloc(path_len + 1);
2830 if (canon_path == NULL) {
2831 err = got_error_from_errno("malloc");
2834 memcpy(path, (uint8_t *)imsg.data + sizeof(*itree),
2836 path[path_len] = '\0';
2837 if (!got_path_is_absolute(path)) {
2838 err = got_error(GOT_ERR_BAD_PATH);
2841 if (got_path_is_root_dir(path)) {
2842 /* XXX check what got_canonpath() does wrong */
2843 canon_path[0] = '/';
2844 canon_path[1] = '\0';
2846 err = got_canonpath(path, canon_path,
2851 if (strcmp(path, canon_path) != 0) {
2852 err = got_error(GOT_ERR_BAD_PATH);
2855 if (nentries != -1) {
2856 err = got_error(GOT_ERR_PRIVSEP_MSG);
2859 if (itree->nentries < -1) {
2860 err = got_error(GOT_ERR_PRIVSEP_MSG);
2863 if (itree->nentries == -1) {
2864 /* Tree was not found in pack file. */
2866 err = cb_tree(cb_arg, NULL, mtime, &tree_id,
2870 if (itree->nentries > INT_MAX) {
2871 err = got_error(GOT_ERR_PRIVSEP_LEN);
2874 tree.entries = calloc(itree->nentries,
2875 sizeof(struct got_tree_entry));
2876 if (tree.entries == NULL) {
2877 err = got_error_from_errno("calloc");
2880 if (itree->nentries == 0) {
2881 err = cb_tree(cb_arg, &tree, mtime, &tree_id,
2886 /* Prepare for next tree. */
2888 memset(&tree, 0, sizeof(tree));
2891 tree.nentries = itree->nentries;
2895 case GOT_IMSG_TREE_ENTRIES:
2896 /* Should be preceeded by GOT_IMSG_ENUMERATED_TREE. */
2897 if (nentries <= -1) {
2898 err = got_error(GOT_ERR_PRIVSEP_MSG);
2901 err = recv_tree_entries(imsg.data, datalen,
2905 if (tree.nentries == nentries) {
2906 err = cb_tree(cb_arg, &tree, mtime, &tree_id,
2911 /* Prepare for next tree. */
2913 memset(&tree, 0, sizeof(tree));
2917 case GOT_IMSG_TREE_ENUMERATION_DONE:
2918 /* All trees have been found and traversed. */
2919 if (!have_commit || path == NULL || nentries != -1) {
2920 err = got_error(GOT_ERR_PRIVSEP_MSG);
2923 err = cb_commit(cb_arg, mtime, &commit_id, repo);
2928 case GOT_IMSG_OBJECT_ENUMERATION_DONE:
2932 err = got_error(GOT_ERR_PRIVSEP_MSG);
2947 const struct got_error *
2948 got_privsep_send_raw_delta_req(struct imsgbuf *ibuf, int idx,
2949 struct got_object_id *id)
2951 struct got_imsg_raw_delta_request dreq;
2954 memcpy(dreq.id, id->sha1, SHA1_DIGEST_LENGTH);
2956 if (imsg_compose(ibuf, GOT_IMSG_RAW_DELTA_REQUEST, 0, 0, -1,
2957 &dreq, sizeof(dreq)) == -1)
2958 return got_error_from_errno("imsg_compose RAW_DELTA_REQUEST");
2960 return flush_imsg(ibuf);
2963 const struct got_error *
2964 got_privsep_send_raw_delta_outfd(struct imsgbuf *ibuf, int fd)
2966 return send_fd(ibuf, GOT_IMSG_RAW_DELTA_OUTFD, fd);
2969 const struct got_error *
2970 got_privsep_send_raw_delta(struct imsgbuf *ibuf, uint64_t base_size,
2971 uint64_t result_size, off_t delta_size, off_t delta_compressed_size,
2972 off_t delta_offset, off_t delta_out_offset, struct got_object_id *base_id)
2974 struct got_imsg_raw_delta idelta;
2977 idelta.base_size = base_size;
2978 idelta.result_size = result_size;
2979 idelta.delta_size = delta_size;
2980 idelta.delta_compressed_size = delta_compressed_size;
2981 idelta.delta_offset = delta_offset;
2982 idelta.delta_out_offset = delta_out_offset;
2983 memcpy(idelta.base_id, base_id->sha1, SHA1_DIGEST_LENGTH);
2985 ret = imsg_compose(ibuf, GOT_IMSG_RAW_DELTA, 0, 0, -1,
2986 &idelta, sizeof(idelta));
2988 return got_error_from_errno("imsg_compose RAW_DELTA");
2990 return flush_imsg(ibuf);
2993 const struct got_error *
2994 got_privsep_recv_raw_delta(uint64_t *base_size, uint64_t *result_size,
2995 off_t *delta_size, off_t *delta_compressed_size, off_t *delta_offset,
2996 off_t *delta_out_offset, struct got_object_id **base_id, struct imsgbuf *ibuf)
2998 const struct got_error *err = NULL;
3000 struct got_imsg_raw_delta *delta;
3006 *delta_compressed_size = 0;
3008 *delta_out_offset = 0;
3011 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
3015 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
3017 switch (imsg.hdr.type) {
3018 case GOT_IMSG_RAW_DELTA:
3019 if (datalen != sizeof(*delta)) {
3020 err = got_error(GOT_ERR_PRIVSEP_LEN);
3024 *base_size = delta->base_size;
3025 *result_size = delta->result_size;
3026 *delta_size = delta->delta_size;
3027 *delta_compressed_size = delta->delta_compressed_size;
3028 *delta_offset = delta->delta_offset;
3029 *delta_out_offset = delta->delta_out_offset;
3030 *base_id = calloc(1, sizeof(**base_id));
3031 if (*base_id == NULL) {
3032 err = got_error_from_errno("malloc");
3035 memcpy((*base_id)->sha1, delta->base_id, SHA1_DIGEST_LENGTH);
3038 err = got_error(GOT_ERR_PRIVSEP_MSG);
3051 static const struct got_error *
3052 send_idlist(struct imsgbuf *ibuf, struct got_object_id **ids, size_t nids)
3054 const struct got_error *err = NULL;
3055 struct got_imsg_object_idlist idlist;
3059 if (nids > GOT_IMSG_OBJ_ID_LIST_MAX_NIDS)
3060 return got_error(GOT_ERR_NO_SPACE);
3062 wbuf = imsg_create(ibuf, GOT_IMSG_OBJ_ID_LIST, 0, 0,
3063 sizeof(idlist) + nids * sizeof(**ids));
3065 err = got_error_from_errno("imsg_create OBJ_ID_LIST");
3070 if (imsg_add(wbuf, &idlist, sizeof(idlist)) == -1)
3071 return got_error_from_errno("imsg_add OBJ_ID_LIST");
3073 for (i = 0; i < nids; i++) {
3074 struct got_object_id *id = ids[i];
3075 if (imsg_add(wbuf, id, sizeof(*id)) == -1)
3076 return got_error_from_errno("imsg_add OBJ_ID_LIST");
3080 imsg_close(ibuf, wbuf);
3082 return flush_imsg(ibuf);
3085 const struct got_error *
3086 got_privsep_send_object_idlist(struct imsgbuf *ibuf,
3087 struct got_object_id **ids, size_t nids)
3089 const struct got_error *err = NULL;
3090 struct got_object_id *idlist[GOT_IMSG_OBJ_ID_LIST_MAX_NIDS];
3093 for (i = 0; i < nids; i++) {
3094 j = i % nitems(idlist);
3096 if (j >= nitems(idlist) - 1) {
3097 err = send_idlist(ibuf, idlist, j + 1);
3105 err = send_idlist(ibuf, idlist, j + 1);
3113 const struct got_error *
3114 got_privsep_send_object_idlist_done(struct imsgbuf *ibuf)
3116 if (imsg_compose(ibuf, GOT_IMSG_OBJ_ID_LIST_DONE, 0, 0, -1, NULL, 0)
3118 return got_error_from_errno("imsg_compose OBJ_ID_LIST_DONE");
3120 return flush_imsg(ibuf);
3123 const struct got_error *
3124 got_privsep_recv_object_idlist(int *done, struct got_object_id **ids,
3125 size_t *nids, struct imsgbuf *ibuf)
3127 const struct got_error *err = NULL;
3129 struct got_imsg_object_idlist *idlist;
3136 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
3140 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
3141 switch (imsg.hdr.type) {
3142 case GOT_IMSG_OBJ_ID_LIST:
3143 if (datalen < sizeof(*idlist)) {
3144 err = got_error(GOT_ERR_PRIVSEP_LEN);
3148 if (idlist->nids > GOT_IMSG_OBJ_ID_LIST_MAX_NIDS) {
3149 err = got_error(GOT_ERR_PRIVSEP_LEN);
3152 *nids = idlist->nids;
3153 *ids = calloc(*nids, sizeof(**ids));
3155 err = got_error_from_errno("calloc");
3158 memcpy(*ids, (uint8_t *)imsg.data + sizeof(idlist),
3159 *nids * sizeof(**ids));
3161 case GOT_IMSG_OBJ_ID_LIST_DONE:
3165 err = got_error(GOT_ERR_PRIVSEP_MSG);
3174 const struct got_error *
3175 got_privsep_send_delta_reuse_req(struct imsgbuf *ibuf)
3177 if (imsg_compose(ibuf, GOT_IMSG_DELTA_REUSE_REQUEST, 0, 0, -1, NULL, 0)
3179 return got_error_from_errno("imsg_compose DELTA_REUSE_REQUEST");
3181 return flush_imsg(ibuf);
3184 const struct got_error *
3185 got_privsep_send_reused_deltas(struct imsgbuf *ibuf,
3186 struct got_imsg_reused_delta *deltas, size_t ndeltas)
3188 const struct got_error *err = NULL;
3190 struct got_imsg_reused_deltas ideltas;
3193 if (ndeltas > GOT_IMSG_REUSED_DELTAS_MAX_NDELTAS)
3194 return got_error(GOT_ERR_NO_SPACE);
3196 wbuf = imsg_create(ibuf, GOT_IMSG_REUSED_DELTAS, 0, 0,
3197 sizeof(ideltas) + ndeltas * sizeof(*deltas));
3199 err = got_error_from_errno("imsg_create REUSED_DELTAS");
3203 ideltas.ndeltas = ndeltas;
3204 if (imsg_add(wbuf, &ideltas, sizeof(ideltas)) == -1)
3205 return got_error_from_errno("imsg_add REUSED_DELTAS");
3207 for (i = 0; i < ndeltas; i++) {
3208 struct got_imsg_reused_delta *delta = &deltas[i];
3209 if (imsg_add(wbuf, delta, sizeof(*delta)) == -1)
3210 return got_error_from_errno("imsg_add REUSED_DELTAS");
3214 imsg_close(ibuf, wbuf);
3216 return flush_imsg(ibuf);
3219 const struct got_error *
3220 got_privsep_send_reused_deltas_done(struct imsgbuf *ibuf)
3222 if (imsg_compose(ibuf, GOT_IMSG_DELTA_REUSE_DONE, 0, 0, -1, NULL, 0)
3224 return got_error_from_errno("imsg_compose DELTA_REUSE_DONE");
3226 return flush_imsg(ibuf);
3229 const struct got_error *
3230 got_privsep_recv_reused_deltas(int *done, struct got_imsg_reused_delta *deltas,
3231 size_t *ndeltas, struct imsgbuf *ibuf)
3233 const struct got_error *err = NULL;
3235 struct got_imsg_reused_deltas *ideltas;
3241 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
3245 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
3246 switch (imsg.hdr.type) {
3247 case GOT_IMSG_REUSED_DELTAS:
3248 if (datalen < sizeof(*ideltas)) {
3249 err = got_error(GOT_ERR_PRIVSEP_LEN);
3252 ideltas = imsg.data;
3253 if (ideltas->ndeltas > GOT_IMSG_OBJ_ID_LIST_MAX_NIDS) {
3254 err = got_error(GOT_ERR_PRIVSEP_LEN);
3257 *ndeltas = ideltas->ndeltas;
3258 memcpy(deltas, (uint8_t *)imsg.data + sizeof(ideltas),
3259 *ndeltas * sizeof(*deltas));
3261 case GOT_IMSG_DELTA_REUSE_DONE:
3265 err = got_error(GOT_ERR_PRIVSEP_MSG);
3274 const struct got_error *
3275 got_privsep_unveil_exec_helpers(void)
3277 const char *helpers[] = {
3278 GOT_PATH_PROG_READ_PACK,
3279 GOT_PATH_PROG_READ_OBJECT,
3280 GOT_PATH_PROG_READ_COMMIT,
3281 GOT_PATH_PROG_READ_TREE,
3282 GOT_PATH_PROG_READ_BLOB,
3283 GOT_PATH_PROG_READ_TAG,
3284 GOT_PATH_PROG_READ_GITCONFIG,
3285 GOT_PATH_PROG_READ_GOTCONFIG,
3286 GOT_PATH_PROG_READ_PATCH,
3287 GOT_PATH_PROG_FETCH_PACK,
3288 GOT_PATH_PROG_INDEX_PACK,
3289 GOT_PATH_PROG_SEND_PACK,
3293 for (i = 0; i < nitems(helpers); i++) {
3294 if (unveil(helpers[i], "x") == 0)
3296 return got_error_from_errno2("unveil", helpers[i]);
3303 got_privsep_exec_child(int imsg_fds[2], const char *path, const char *repo_path)
3305 if (close(imsg_fds[0]) == -1) {
3306 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
3310 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
3311 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
3315 closefrom(GOT_IMSG_FD_CHILD + 1);
3317 if (execl(path, path, repo_path, (char *)NULL) == -1) {
3318 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,