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 if (icommit->author_len == 0) {
1305 (*commit)->author = strdup("");
1306 if ((*commit)->author == NULL) {
1307 err = got_error_from_errno("strdup");
1311 (*commit)->author = malloc(icommit->author_len + 1);
1312 if ((*commit)->author == NULL) {
1313 err = got_error_from_errno("malloc");
1316 memcpy((*commit)->author, imsg->data + len,
1317 icommit->author_len);
1318 (*commit)->author[icommit->author_len] = '\0';
1320 len += icommit->author_len;
1322 if (icommit->committer_len == 0) {
1323 (*commit)->committer = strdup("");
1324 if ((*commit)->committer == NULL) {
1325 err = got_error_from_errno("strdup");
1329 (*commit)->committer =
1330 malloc(icommit->committer_len + 1);
1331 if ((*commit)->committer == NULL) {
1332 err = got_error_from_errno("malloc");
1335 memcpy((*commit)->committer, imsg->data + len,
1336 icommit->committer_len);
1337 (*commit)->committer[icommit->committer_len] = '\0';
1339 len += icommit->committer_len;
1341 if (icommit->logmsg_len == 0) {
1342 (*commit)->logmsg = strdup("");
1343 if ((*commit)->logmsg == NULL) {
1344 err = got_error_from_errno("strdup");
1348 size_t offset = 0, remain = icommit->logmsg_len;
1350 (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
1351 if ((*commit)->logmsg == NULL) {
1352 err = got_error_from_errno("malloc");
1355 while (remain > 0) {
1356 struct imsg imsg_log;
1357 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1360 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1364 if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG) {
1365 err = got_error(GOT_ERR_PRIVSEP_MSG);
1369 memcpy((*commit)->logmsg + offset,
1371 imsg_free(&imsg_log);
1375 (*commit)->logmsg[icommit->logmsg_len] = '\0';
1378 for (i = 0; i < icommit->nparents; i++) {
1379 struct got_object_qid *qid;
1381 err = got_object_qid_alloc_partial(&qid);
1384 memcpy(&qid->id, imsg->data + len +
1385 i * SHA1_DIGEST_LENGTH, sizeof(qid->id));
1386 STAILQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
1387 (*commit)->nparents++;
1391 got_object_commit_close(*commit);
1397 const struct got_error *
1398 got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
1400 const struct got_error *err = NULL;
1403 const size_t min_datalen =
1404 MIN(sizeof(struct got_imsg_error),
1405 sizeof(struct got_imsg_commit_object));
1409 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1413 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1415 switch (imsg.hdr.type) {
1416 case GOT_IMSG_COMMIT:
1417 err = get_commit_from_imsg(commit, &imsg, datalen, ibuf);
1420 err = got_error(GOT_ERR_PRIVSEP_MSG);
1429 static const struct got_error *
1430 send_tree_entries_batch(struct imsgbuf *ibuf,
1431 struct got_parsed_tree_entry *entries, int idx0, int idxN, size_t len)
1434 struct got_imsg_tree_entries ientries;
1437 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_ENTRIES, 0, 0, len);
1439 return got_error_from_errno("imsg_create TREE_ENTRY");
1441 ientries.nentries = idxN - idx0 + 1;
1442 if (imsg_add(wbuf, &ientries, sizeof(ientries)) == -1)
1443 return got_error_from_errno("imsg_add TREE_ENTRY");
1445 for (i = idx0; i <= idxN; i++) {
1446 struct got_parsed_tree_entry *pte = &entries[i];
1448 /* Keep in sync with struct got_imsg_tree_object definition! */
1449 if (imsg_add(wbuf, pte->id, SHA1_DIGEST_LENGTH) == -1)
1450 return got_error_from_errno("imsg_add TREE_ENTRY");
1451 if (imsg_add(wbuf, &pte->mode, sizeof(pte->mode)) == -1)
1452 return got_error_from_errno("imsg_add TREE_ENTRY");
1453 if (imsg_add(wbuf, &pte->namelen, sizeof(pte->namelen)) == -1)
1454 return got_error_from_errno("imsg_add TREE_ENTRY");
1456 /* Remaining bytes are the entry's name. */
1457 if (imsg_add(wbuf, pte->name, pte->namelen) == -1)
1458 return got_error_from_errno("imsg_add TREE_ENTRY");
1462 imsg_close(ibuf, wbuf);
1466 static const struct got_error *
1467 send_tree_entries(struct imsgbuf *ibuf, struct got_parsed_tree_entry *entries,
1470 const struct got_error *err = NULL;
1472 size_t entries_len = sizeof(struct got_imsg_tree_entries);
1475 for (j = 0; j < nentries; j++) {
1476 struct got_parsed_tree_entry *pte = &entries[j];
1477 size_t len = sizeof(*pte) + pte->namelen;
1480 entries_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1481 err = send_tree_entries_batch(ibuf, entries,
1482 i, j - 1, entries_len);
1486 entries_len = sizeof(struct got_imsg_tree_entries);
1493 err = send_tree_entries_batch(ibuf, entries, i, j - 1,
1502 const struct got_error *
1503 got_privsep_send_tree(struct imsgbuf *ibuf,
1504 struct got_parsed_tree_entry *entries, int nentries)
1506 const struct got_error *err = NULL;
1507 struct got_imsg_tree_object itree;
1509 itree.nentries = nentries;
1510 if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
1512 return got_error_from_errno("imsg_compose TREE");
1514 err = send_tree_entries(ibuf, entries, nentries);
1518 return flush_imsg(ibuf);
1522 static const struct got_error *
1523 recv_tree_entries(void *data, size_t datalen, struct got_tree_object *tree,
1526 const struct got_error *err = NULL;
1527 struct got_imsg_tree_entries *ientries;
1528 struct got_tree_entry *te;
1532 if (datalen <= sizeof(*ientries) ||
1533 datalen > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
1534 return got_error(GOT_ERR_PRIVSEP_LEN);
1536 ientries = (struct got_imsg_tree_entries *)data;
1537 if (ientries->nentries > INT_MAX) {
1538 return got_error_msg(GOT_ERR_NO_SPACE,
1539 "too many tree entries");
1542 te_offset = sizeof(*ientries);
1543 for (i = 0; i < ientries->nentries; i++) {
1544 struct got_imsg_tree_entry ite;
1545 const char *te_name;
1546 uint8_t *buf = (uint8_t *)data + te_offset;
1548 if (te_offset >= datalen) {
1549 err = got_error(GOT_ERR_PRIVSEP_LEN);
1553 /* Might not be aligned, size is ~32 bytes. */
1554 memcpy(&ite, buf, sizeof(ite));
1556 if (ite.namelen >= sizeof(te->name)) {
1557 err = got_error(GOT_ERR_PRIVSEP_LEN);
1560 if (te_offset + sizeof(ite) + ite.namelen > datalen) {
1561 err = got_error(GOT_ERR_PRIVSEP_LEN);
1565 if (*nentries >= tree->nentries) {
1566 err = got_error(GOT_ERR_PRIVSEP_LEN);
1569 te = &tree->entries[*nentries];
1570 te_name = buf + sizeof(ite);
1571 memcpy(te->name, te_name, ite.namelen);
1572 te->name[ite.namelen] = '\0';
1573 memcpy(te->id.sha1, ite.id, SHA1_DIGEST_LENGTH);
1574 te->mode = ite.mode;
1575 te->idx = *nentries;
1578 te_offset += sizeof(ite) + ite.namelen;
1584 const struct got_error *
1585 got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
1587 const struct got_error *err = NULL;
1588 const size_t min_datalen =
1589 MIN(sizeof(struct got_imsg_error),
1590 sizeof(struct got_imsg_tree_object));
1591 struct got_imsg_tree_object *itree;
1596 err = read_imsg(ibuf);
1605 n = imsg_get(ibuf, &imsg);
1608 if (nentries < (*tree)->nentries) {
1609 err = read_imsg(ibuf);
1616 err = got_error(GOT_ERR_PRIVSEP_MSG);
1621 if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen) {
1623 err = got_error(GOT_ERR_PRIVSEP_LEN);
1627 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1629 switch (imsg.hdr.type) {
1630 case GOT_IMSG_ERROR:
1631 err = recv_imsg_error(&imsg, datalen);
1634 /* This message should only appear once. */
1635 if (*tree != NULL) {
1636 err = got_error(GOT_ERR_PRIVSEP_MSG);
1639 if (datalen != sizeof(*itree)) {
1640 err = got_error(GOT_ERR_PRIVSEP_LEN);
1644 if (itree->nentries < 0) {
1645 err = got_error(GOT_ERR_PRIVSEP_LEN);
1648 *tree = malloc(sizeof(**tree));
1649 if (*tree == NULL) {
1650 err = got_error_from_errno("malloc");
1653 (*tree)->entries = calloc(itree->nentries,
1654 sizeof(struct got_tree_entry));
1655 if ((*tree)->entries == NULL) {
1656 err = got_error_from_errno("malloc");
1661 (*tree)->nentries = itree->nentries;
1662 (*tree)->refcnt = 0;
1664 case GOT_IMSG_TREE_ENTRIES:
1665 /* This message should be preceeded by GOT_IMSG_TREE. */
1666 if (*tree == NULL) {
1667 err = got_error(GOT_ERR_PRIVSEP_MSG);
1670 err = recv_tree_entries(imsg.data, datalen,
1674 err = got_error(GOT_ERR_PRIVSEP_MSG);
1683 if (*tree && (*tree)->nentries != nentries) {
1685 err = got_error(GOT_ERR_PRIVSEP_LEN);
1686 got_object_tree_close(*tree);
1693 const struct got_error *
1694 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
1695 const uint8_t *data)
1697 struct got_imsg_blob iblob;
1700 iblob.hdrlen = hdrlen;
1705 if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
1706 return got_error(GOT_ERR_NO_SPACE);
1708 buf = malloc(sizeof(iblob) + size);
1710 return got_error_from_errno("malloc");
1712 memcpy(buf, &iblob, sizeof(iblob));
1713 memcpy(buf + sizeof(iblob), data, size);
1714 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
1715 sizeof(iblob) + size) == -1) {
1717 return got_error_from_errno("imsg_compose BLOB");
1721 /* Data has already been written to file descriptor. */
1722 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
1723 sizeof(iblob)) == -1)
1724 return got_error_from_errno("imsg_compose BLOB");
1728 return flush_imsg(ibuf);
1731 const struct got_error *
1732 got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1733 struct imsgbuf *ibuf)
1735 const struct got_error *err = NULL;
1737 struct got_imsg_blob *iblob;
1742 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1746 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1748 switch (imsg.hdr.type) {
1750 if (datalen < sizeof(*iblob)) {
1751 err = got_error(GOT_ERR_PRIVSEP_LEN);
1755 *size = iblob->size;
1756 *hdrlen = iblob->hdrlen;
1758 if (datalen == sizeof(*iblob)) {
1759 /* Data has been written to file descriptor. */
1763 if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX) {
1764 err = got_error(GOT_ERR_PRIVSEP_LEN);
1768 *outbuf = malloc(*size);
1769 if (*outbuf == NULL) {
1770 err = got_error_from_errno("malloc");
1773 memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
1776 err = got_error(GOT_ERR_PRIVSEP_MSG);
1785 static const struct got_error *
1786 send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
1788 const struct got_error *err = NULL;
1789 size_t offset, remain;
1792 remain = tagmsg_len;
1793 while (remain > 0) {
1794 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1796 if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
1797 tag->tagmsg + offset, n) == -1) {
1798 err = got_error_from_errno("imsg_compose TAG_TAGMSG");
1802 err = flush_imsg(ibuf);
1813 const struct got_error *
1814 got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
1816 const struct got_error *err = NULL;
1817 struct got_imsg_tag_object *itag;
1820 size_t tag_len = strlen(tag->tag);
1821 size_t tagger_len = strlen(tag->tagger);
1822 size_t tagmsg_len = strlen(tag->tagmsg);
1824 total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
1826 buf = malloc(total);
1828 return got_error_from_errno("malloc");
1830 itag = (struct got_imsg_tag_object *)buf;
1831 memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
1832 itag->obj_type = tag->obj_type;
1833 itag->tag_len = tag_len;
1834 itag->tagger_len = tagger_len;
1835 itag->tagger_time = tag->tagger_time;
1836 itag->tagger_gmtoff = tag->tagger_gmtoff;
1837 itag->tagmsg_len = tagmsg_len;
1839 len = sizeof(*itag);
1840 memcpy(buf + len, tag->tag, tag_len);
1842 memcpy(buf + len, tag->tagger, tagger_len);
1845 if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
1846 err = got_error_from_errno("imsg_compose TAG");
1850 if (tagmsg_len == 0 ||
1851 tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1852 err = flush_imsg(ibuf);
1856 err = send_tagmsg(ibuf, tag, tagmsg_len);
1862 const struct got_error *
1863 got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
1865 const struct got_error *err = NULL;
1867 struct got_imsg_tag_object *itag;
1868 size_t len, datalen;
1869 const size_t min_datalen =
1870 MIN(sizeof(struct got_imsg_error),
1871 sizeof(struct got_imsg_tag_object));
1875 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1879 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1882 switch (imsg.hdr.type) {
1884 if (datalen < sizeof(*itag)) {
1885 err = got_error(GOT_ERR_PRIVSEP_LEN);
1889 if (datalen != sizeof(*itag) + itag->tag_len +
1891 err = got_error(GOT_ERR_PRIVSEP_LEN);
1894 len += sizeof(*itag);
1896 *tag = calloc(1, sizeof(**tag));
1898 err = got_error_from_errno("calloc");
1902 memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
1904 if (itag->tag_len == 0) {
1905 (*tag)->tag = strdup("");
1906 if ((*tag)->tag == NULL) {
1907 err = got_error_from_errno("strdup");
1911 (*tag)->tag = malloc(itag->tag_len + 1);
1912 if ((*tag)->tag == NULL) {
1913 err = got_error_from_errno("malloc");
1916 memcpy((*tag)->tag, imsg.data + len,
1918 (*tag)->tag[itag->tag_len] = '\0';
1920 len += itag->tag_len;
1922 (*tag)->obj_type = itag->obj_type;
1923 (*tag)->tagger_time = itag->tagger_time;
1924 (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1926 if (itag->tagger_len == 0) {
1927 (*tag)->tagger = strdup("");
1928 if ((*tag)->tagger == NULL) {
1929 err = got_error_from_errno("strdup");
1933 (*tag)->tagger = malloc(itag->tagger_len + 1);
1934 if ((*tag)->tagger == NULL) {
1935 err = got_error_from_errno("malloc");
1938 memcpy((*tag)->tagger, imsg.data + len,
1940 (*tag)->tagger[itag->tagger_len] = '\0';
1942 len += itag->tagger_len;
1944 if (itag->tagmsg_len == 0) {
1945 (*tag)->tagmsg = strdup("");
1946 if ((*tag)->tagmsg == NULL) {
1947 err = got_error_from_errno("strdup");
1951 size_t offset = 0, remain = itag->tagmsg_len;
1953 (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1954 if ((*tag)->tagmsg == NULL) {
1955 err = got_error_from_errno("malloc");
1958 while (remain > 0) {
1959 struct imsg imsg_log;
1960 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1963 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1967 if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1968 return got_error(GOT_ERR_PRIVSEP_MSG);
1970 memcpy((*tag)->tagmsg + offset, imsg_log.data,
1972 imsg_free(&imsg_log);
1976 (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1981 err = got_error(GOT_ERR_PRIVSEP_MSG);
1990 const struct got_error *
1991 got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1992 struct got_packidx *packidx)
1994 const struct got_error *err = NULL;
1995 struct got_imsg_packidx ipackidx;
1996 struct got_imsg_pack ipack;
1999 ipackidx.len = packidx->len;
2000 ipackidx.packfile_size = pack->filesize;
2001 fd = dup(packidx->fd);
2003 return got_error_from_errno("dup");
2005 if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
2006 sizeof(ipackidx)) == -1) {
2007 err = got_error_from_errno("imsg_compose PACKIDX");
2012 if (strlcpy(ipack.path_packfile, pack->path_packfile,
2013 sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
2014 return got_error(GOT_ERR_NO_SPACE);
2015 ipack.filesize = pack->filesize;
2019 return got_error_from_errno("dup");
2021 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
2023 err = got_error_from_errno("imsg_compose PACK");
2028 return flush_imsg(ibuf);
2031 const struct got_error *
2032 got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
2033 struct got_object_id *id)
2035 struct got_imsg_packed_object iobj;
2038 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
2040 if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
2041 &iobj, sizeof(iobj)) == -1)
2042 return got_error_from_errno("imsg_compose "
2043 "PACKED_OBJECT_REQUEST");
2045 return flush_imsg(ibuf);
2048 const struct got_error *
2049 got_privsep_send_packed_raw_obj_req(struct imsgbuf *ibuf, int idx,
2050 struct got_object_id *id)
2052 struct got_imsg_packed_object iobj;
2055 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
2057 if (imsg_compose(ibuf, GOT_IMSG_PACKED_RAW_OBJECT_REQUEST, 0, 0, -1,
2058 &iobj, sizeof(iobj)) == -1)
2059 return got_error_from_errno("imsg_compose "
2060 "PACKED_OBJECT_REQUEST");
2062 return flush_imsg(ibuf);
2065 const struct got_error *
2066 got_privsep_send_gitconfig_parse_req(struct imsgbuf *ibuf, int fd)
2068 const struct got_error *err = NULL;
2070 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_PARSE_REQUEST, 0, 0, fd,
2072 err = got_error_from_errno("imsg_compose "
2073 "GITCONFIG_PARSE_REQUEST");
2078 return flush_imsg(ibuf);
2081 const struct got_error *
2082 got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *ibuf)
2084 if (imsg_compose(ibuf,
2085 GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST, 0, 0, -1,
2087 return got_error_from_errno("imsg_compose "
2088 "GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST");
2090 return flush_imsg(ibuf);
2093 const struct got_error *
2094 got_privsep_send_gitconfig_repository_extensions_req(struct imsgbuf *ibuf)
2096 if (imsg_compose(ibuf,
2097 GOT_IMSG_GITCONFIG_REPOSITORY_EXTENSIONS_REQUEST, 0, 0, -1,
2099 return got_error_from_errno("imsg_compose "
2100 "GITCONFIG_REPOSITORY_EXTENSIONS_REQUEST");
2102 return flush_imsg(ibuf);
2106 const struct got_error *
2107 got_privsep_send_gitconfig_author_name_req(struct imsgbuf *ibuf)
2109 if (imsg_compose(ibuf,
2110 GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST, 0, 0, -1, NULL, 0) == -1)
2111 return got_error_from_errno("imsg_compose "
2112 "GITCONFIG_AUTHOR_NAME_REQUEST");
2114 return flush_imsg(ibuf);
2117 const struct got_error *
2118 got_privsep_send_gitconfig_author_email_req(struct imsgbuf *ibuf)
2120 if (imsg_compose(ibuf,
2121 GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST, 0, 0, -1, NULL, 0) == -1)
2122 return got_error_from_errno("imsg_compose "
2123 "GITCONFIG_AUTHOR_EMAIL_REQUEST");
2125 return flush_imsg(ibuf);
2128 const struct got_error *
2129 got_privsep_send_gitconfig_remotes_req(struct imsgbuf *ibuf)
2131 if (imsg_compose(ibuf,
2132 GOT_IMSG_GITCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
2133 return got_error_from_errno("imsg_compose "
2134 "GITCONFIG_REMOTE_REQUEST");
2136 return flush_imsg(ibuf);
2139 const struct got_error *
2140 got_privsep_send_gitconfig_owner_req(struct imsgbuf *ibuf)
2142 if (imsg_compose(ibuf,
2143 GOT_IMSG_GITCONFIG_OWNER_REQUEST, 0, 0, -1, NULL, 0) == -1)
2144 return got_error_from_errno("imsg_compose "
2145 "GITCONFIG_OWNER_REQUEST");
2147 return flush_imsg(ibuf);
2150 const struct got_error *
2151 got_privsep_recv_gitconfig_str(char **str, struct imsgbuf *ibuf)
2153 const struct got_error *err = NULL;
2156 const size_t min_datalen = 0;
2160 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2163 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2165 switch (imsg.hdr.type) {
2166 case GOT_IMSG_GITCONFIG_STR_VAL:
2169 /* datalen does not include terminating \0 */
2170 *str = malloc(datalen + 1);
2172 err = got_error_from_errno("malloc");
2175 memcpy(*str, imsg.data, datalen);
2176 (*str)[datalen] = '\0';
2179 err = got_error(GOT_ERR_PRIVSEP_MSG);
2187 const struct got_error *
2188 got_privsep_recv_gitconfig_int(int *val, struct imsgbuf *ibuf)
2190 const struct got_error *err = NULL;
2193 const size_t min_datalen =
2194 MIN(sizeof(struct got_imsg_error), sizeof(int));
2198 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2201 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2203 switch (imsg.hdr.type) {
2204 case GOT_IMSG_GITCONFIG_INT_VAL:
2205 if (datalen != sizeof(*val)) {
2206 err = got_error(GOT_ERR_PRIVSEP_LEN);
2209 memcpy(val, imsg.data, sizeof(*val));
2212 err = got_error(GOT_ERR_PRIVSEP_MSG);
2221 free_remote_data(struct got_remote_repo *remote)
2226 free(remote->fetch_url);
2227 free(remote->send_url);
2228 for (i = 0; i < remote->nfetch_branches; i++)
2229 free(remote->fetch_branches[i]);
2230 free(remote->fetch_branches);
2231 for (i = 0; i < remote->nsend_branches; i++)
2232 free(remote->send_branches[i]);
2233 free(remote->send_branches);
2234 for (i = 0; i < remote->nfetch_refs; i++)
2235 free(remote->fetch_refs[i]);
2236 free(remote->fetch_refs);
2239 const struct got_error *
2240 got_privsep_recv_gitconfig_remotes(struct got_remote_repo **remotes,
2241 int *nremotes, struct imsgbuf *ibuf)
2243 const struct got_error *err = NULL;
2246 struct got_imsg_remotes iremotes;
2247 struct got_imsg_remote iremote;
2251 iremotes.nremotes = 0;
2253 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremotes));
2256 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2258 switch (imsg.hdr.type) {
2259 case GOT_IMSG_GITCONFIG_REMOTES:
2260 if (datalen != sizeof(iremotes)) {
2261 err = got_error(GOT_ERR_PRIVSEP_LEN);
2264 memcpy(&iremotes, imsg.data, sizeof(iremotes));
2265 if (iremotes.nremotes == 0) {
2272 return got_error(GOT_ERR_PRIVSEP_MSG);
2277 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(**remotes));
2278 if (*remotes == NULL)
2279 return got_error_from_errno("recallocarray");
2281 while (*nremotes < iremotes.nremotes) {
2282 struct got_remote_repo *remote;
2284 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremote));
2287 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2289 switch (imsg.hdr.type) {
2290 case GOT_IMSG_GITCONFIG_REMOTE:
2291 remote = &(*remotes)[*nremotes];
2292 memset(remote, 0, sizeof(*remote));
2293 if (datalen < sizeof(iremote)) {
2294 err = got_error(GOT_ERR_PRIVSEP_LEN);
2297 memcpy(&iremote, imsg.data, sizeof(iremote));
2298 if (iremote.name_len == 0 ||
2299 iremote.fetch_url_len == 0 ||
2300 iremote.send_url_len == 0 ||
2301 (sizeof(iremote) + iremote.name_len +
2302 iremote.fetch_url_len + iremote.send_url_len) > datalen) {
2303 err = got_error(GOT_ERR_PRIVSEP_LEN);
2306 remote->name = strndup(imsg.data + sizeof(iremote),
2308 if (remote->name == NULL) {
2309 err = got_error_from_errno("strndup");
2312 remote->fetch_url = strndup(imsg.data + sizeof(iremote) +
2313 iremote.name_len, iremote.fetch_url_len);
2314 if (remote->fetch_url == NULL) {
2315 err = got_error_from_errno("strndup");
2316 free_remote_data(remote);
2319 remote->send_url = strndup(imsg.data + sizeof(iremote) +
2320 iremote.name_len + iremote.fetch_url_len,
2321 iremote.send_url_len);
2322 if (remote->send_url == NULL) {
2323 err = got_error_from_errno("strndup");
2324 free_remote_data(remote);
2327 remote->mirror_references = iremote.mirror_references;
2328 remote->fetch_all_branches = iremote.fetch_all_branches;
2329 remote->nfetch_branches = 0;
2330 remote->fetch_branches = NULL;
2331 remote->nsend_branches = 0;
2332 remote->send_branches = NULL;
2333 remote->nfetch_refs = 0;
2334 remote->fetch_refs = NULL;
2338 err = got_error(GOT_ERR_PRIVSEP_MSG);
2349 for (i = 0; i < *nremotes; i++)
2350 free_remote_data(&(*remotes)[i]);
2358 const struct got_error *
2359 got_privsep_send_gotconfig_parse_req(struct imsgbuf *ibuf, int fd)
2361 const struct got_error *err = NULL;
2363 if (imsg_compose(ibuf, GOT_IMSG_GOTCONFIG_PARSE_REQUEST, 0, 0, fd,
2365 err = got_error_from_errno("imsg_compose "
2366 "GOTCONFIG_PARSE_REQUEST");
2371 return flush_imsg(ibuf);
2374 const struct got_error *
2375 got_privsep_send_gotconfig_author_req(struct imsgbuf *ibuf)
2377 if (imsg_compose(ibuf,
2378 GOT_IMSG_GOTCONFIG_AUTHOR_REQUEST, 0, 0, -1, NULL, 0) == -1)
2379 return got_error_from_errno("imsg_compose "
2380 "GOTCONFIG_AUTHOR_REQUEST");
2382 return flush_imsg(ibuf);
2385 const struct got_error *
2386 got_privsep_send_gotconfig_remotes_req(struct imsgbuf *ibuf)
2388 if (imsg_compose(ibuf,
2389 GOT_IMSG_GOTCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
2390 return got_error_from_errno("imsg_compose "
2391 "GOTCONFIG_REMOTE_REQUEST");
2393 return flush_imsg(ibuf);
2396 const struct got_error *
2397 got_privsep_recv_gotconfig_str(char **str, struct imsgbuf *ibuf)
2399 const struct got_error *err = NULL;
2402 const size_t min_datalen = 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_STR_VAL:
2418 /* datalen does not include terminating \0 */
2419 *str = malloc(datalen + 1);
2421 err = got_error_from_errno("malloc");
2424 memcpy(*str, imsg.data, datalen);
2425 (*str)[datalen] = '\0';
2428 err = got_error(GOT_ERR_PRIVSEP_MSG);
2436 const struct got_error *
2437 got_privsep_recv_gotconfig_remotes(struct got_remote_repo **remotes,
2438 int *nremotes, struct imsgbuf *ibuf)
2440 const struct got_error *err = NULL;
2443 struct got_imsg_remotes iremotes;
2444 struct got_imsg_remote iremote;
2445 const size_t min_datalen =
2446 MIN(sizeof(struct got_imsg_error), sizeof(iremotes));
2450 iremotes.nremotes = 0;
2452 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2455 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2457 switch (imsg.hdr.type) {
2458 case GOT_IMSG_ERROR:
2459 err = recv_imsg_error(&imsg, datalen);
2461 case GOT_IMSG_GOTCONFIG_REMOTES:
2462 if (datalen != sizeof(iremotes)) {
2463 err = got_error(GOT_ERR_PRIVSEP_LEN);
2466 memcpy(&iremotes, imsg.data, sizeof(iremotes));
2467 if (iremotes.nremotes == 0) {
2474 return got_error(GOT_ERR_PRIVSEP_MSG);
2479 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(**remotes));
2480 if (*remotes == NULL)
2481 return got_error_from_errno("recallocarray");
2483 while (*nremotes < iremotes.nremotes) {
2484 struct got_remote_repo *remote;
2485 const size_t min_datalen =
2486 MIN(sizeof(struct got_imsg_error), sizeof(iremote));
2489 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2492 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2494 switch (imsg.hdr.type) {
2495 case GOT_IMSG_ERROR:
2496 err = recv_imsg_error(&imsg, datalen);
2498 case GOT_IMSG_GOTCONFIG_REMOTE:
2499 remote = &(*remotes)[*nremotes];
2500 memset(remote, 0, sizeof(*remote));
2501 if (datalen < sizeof(iremote)) {
2502 err = got_error(GOT_ERR_PRIVSEP_LEN);
2505 memcpy(&iremote, imsg.data, sizeof(iremote));
2506 if (iremote.name_len == 0 ||
2507 (iremote.fetch_url_len == 0 &&
2508 iremote.send_url_len == 0) ||
2509 (sizeof(iremote) + iremote.name_len +
2510 iremote.fetch_url_len + iremote.send_url_len) >
2512 err = got_error(GOT_ERR_PRIVSEP_LEN);
2515 remote->name = strndup(imsg.data + sizeof(iremote),
2517 if (remote->name == NULL) {
2518 err = got_error_from_errno("strndup");
2521 remote->fetch_url = strndup(imsg.data +
2522 sizeof(iremote) + iremote.name_len,
2523 iremote.fetch_url_len);
2524 if (remote->fetch_url == NULL) {
2525 err = got_error_from_errno("strndup");
2526 free_remote_data(remote);
2529 remote->send_url = strndup(imsg.data +
2530 sizeof(iremote) + iremote.name_len +
2531 iremote.fetch_url_len, iremote.send_url_len);
2532 if (remote->send_url == NULL) {
2533 err = got_error_from_errno("strndup");
2534 free_remote_data(remote);
2537 remote->mirror_references = iremote.mirror_references;
2538 remote->fetch_all_branches = iremote.fetch_all_branches;
2539 if (iremote.nfetch_branches > 0) {
2540 remote->fetch_branches = recallocarray(NULL, 0,
2541 iremote.nfetch_branches, sizeof(char *));
2542 if (remote->fetch_branches == NULL) {
2543 err = got_error_from_errno("calloc");
2544 free_remote_data(remote);
2548 remote->nfetch_branches = 0;
2549 for (i = 0; i < iremote.nfetch_branches; i++) {
2551 err = got_privsep_recv_gotconfig_str(&branch,
2554 free_remote_data(remote);
2557 remote->fetch_branches[i] = branch;
2558 remote->nfetch_branches++;
2560 if (iremote.nsend_branches > 0) {
2561 remote->send_branches = recallocarray(NULL, 0,
2562 iremote.nsend_branches, sizeof(char *));
2563 if (remote->send_branches == NULL) {
2564 err = got_error_from_errno("calloc");
2565 free_remote_data(remote);
2569 remote->nsend_branches = 0;
2570 for (i = 0; i < iremote.nsend_branches; i++) {
2572 err = got_privsep_recv_gotconfig_str(&branch,
2575 free_remote_data(remote);
2578 remote->send_branches[i] = branch;
2579 remote->nsend_branches++;
2581 if (iremote.nfetch_refs > 0) {
2582 remote->fetch_refs = recallocarray(NULL, 0,
2583 iremote.nfetch_refs, sizeof(char *));
2584 if (remote->fetch_refs == NULL) {
2585 err = got_error_from_errno("calloc");
2586 free_remote_data(remote);
2590 remote->nfetch_refs = 0;
2591 for (i = 0; i < iremote.nfetch_refs; i++) {
2593 err = got_privsep_recv_gotconfig_str(&ref,
2596 free_remote_data(remote);
2599 remote->fetch_refs[i] = ref;
2600 remote->nfetch_refs++;
2605 err = got_error(GOT_ERR_PRIVSEP_MSG);
2616 for (i = 0; i < *nremotes; i++)
2617 free_remote_data(&(*remotes)[i]);
2625 const struct got_error *
2626 got_privsep_send_commit_traversal_request(struct imsgbuf *ibuf,
2627 struct got_object_id *id, int idx, const char *path)
2630 size_t path_len = strlen(path) + 1;
2632 wbuf = imsg_create(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_REQUEST, 0, 0,
2633 sizeof(struct got_imsg_commit_traversal_request) + path_len);
2635 return got_error_from_errno(
2636 "imsg_create COMMIT_TRAVERSAL_REQUEST");
2637 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1)
2638 return got_error_from_errno("imsg_add "
2639 "COMMIT_TRAVERSAL_REQUEST");
2640 if (imsg_add(wbuf, &idx, sizeof(idx)) == -1)
2641 return got_error_from_errno("imsg_add "
2642 "COMMIT_TRAVERSAL_REQUEST");
2643 if (imsg_add(wbuf, path, path_len) == -1)
2644 return got_error_from_errno("imsg_add "
2645 "COMMIT_TRAVERSAL_REQUEST");
2648 imsg_close(ibuf, wbuf);
2650 return flush_imsg(ibuf);
2653 const struct got_error *
2654 got_privsep_recv_traversed_commits(struct got_commit_object **changed_commit,
2655 struct got_object_id **changed_commit_id,
2656 struct got_object_id_queue *commit_ids, struct imsgbuf *ibuf)
2658 const struct got_error *err = NULL;
2660 struct got_imsg_traversed_commits *icommits;
2664 *changed_commit = NULL;
2665 *changed_commit_id = NULL;
2668 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2672 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2673 switch (imsg.hdr.type) {
2674 case GOT_IMSG_TRAVERSED_COMMITS:
2675 icommits = imsg.data;
2676 if (datalen != sizeof(*icommits) +
2677 icommits->ncommits * SHA1_DIGEST_LENGTH) {
2678 err = got_error(GOT_ERR_PRIVSEP_LEN);
2681 for (i = 0; i < icommits->ncommits; i++) {
2682 struct got_object_qid *qid;
2683 uint8_t *sha1 = (uint8_t *)imsg.data +
2684 sizeof(*icommits) + i * SHA1_DIGEST_LENGTH;
2685 err = got_object_qid_alloc_partial(&qid);
2688 memcpy(qid->id.sha1, sha1, SHA1_DIGEST_LENGTH);
2689 STAILQ_INSERT_TAIL(commit_ids, qid, entry);
2691 /* The last commit may contain a change. */
2692 if (i == icommits->ncommits - 1) {
2693 *changed_commit_id =
2694 got_object_id_dup(&qid->id);
2695 if (*changed_commit_id == NULL) {
2696 err = got_error_from_errno(
2697 "got_object_id_dup");
2703 case GOT_IMSG_COMMIT:
2704 if (*changed_commit_id == NULL) {
2705 err = got_error(GOT_ERR_PRIVSEP_MSG);
2708 err = get_commit_from_imsg(changed_commit, &imsg,
2711 case GOT_IMSG_COMMIT_TRAVERSAL_DONE:
2715 err = got_error(GOT_ERR_PRIVSEP_MSG);
2725 got_object_id_queue_free(commit_ids);
2729 const struct got_error *
2730 got_privsep_send_enumerated_tree(size_t *totlen, struct imsgbuf *ibuf,
2731 struct got_object_id *tree_id, const char *path,
2732 struct got_parsed_tree_entry *entries, int nentries)
2734 const struct got_error *err = NULL;
2736 size_t path_len = strlen(path);
2739 msglen = sizeof(struct got_imsg_enumerated_tree) + path_len;
2740 wbuf = imsg_create(ibuf, GOT_IMSG_ENUMERATED_TREE, 0, 0, msglen);
2742 return got_error_from_errno("imsg_create ENUMERATED_TREE");
2744 if (imsg_add(wbuf, tree_id->sha1, SHA1_DIGEST_LENGTH) == -1)
2745 return got_error_from_errno("imsg_add ENUMERATED_TREE");
2746 if (imsg_add(wbuf, &nentries, sizeof(nentries)) == -1)
2747 return got_error_from_errno("imsg_add ENUMERATED_TREE");
2748 if (imsg_add(wbuf, path, path_len) == -1)
2749 return got_error_from_errno("imsg_add ENUMERATED_TREE");
2752 imsg_close(ibuf, wbuf);
2755 err = send_tree_entries(ibuf, entries, nentries);
2760 return flush_imsg(ibuf);
2763 const struct got_error *
2764 got_privsep_send_object_enumeration_request(struct imsgbuf *ibuf)
2766 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_ENUMERATION_REQUEST,
2767 0, 0, -1, NULL, 0) == -1)
2768 return got_error_from_errno("imsg_compose "
2769 "OBJECT_ENUMERATION_REQUEST");
2771 return flush_imsg(ibuf);
2774 const struct got_error *
2775 got_privsep_send_object_enumeration_done(struct imsgbuf *ibuf)
2777 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_ENUMERATION_DONE,
2778 0, 0, -1, NULL, 0) == -1)
2779 return got_error_from_errno("imsg_compose "
2780 "OBJECT_ENUMERATION_DONE");
2782 return flush_imsg(ibuf);
2785 const struct got_error *
2786 got_privsep_send_enumerated_commit(struct imsgbuf *ibuf,
2787 struct got_object_id *id, time_t mtime)
2791 wbuf = imsg_create(ibuf, GOT_IMSG_ENUMERATED_COMMIT, 0, 0,
2792 sizeof(struct got_imsg_enumerated_commit) + SHA1_DIGEST_LENGTH);
2794 return got_error_from_errno("imsg_create ENUMERATED_COMMIT");
2796 /* Keep in sync with struct got_imsg_enumerated_commit! */
2797 if (imsg_add(wbuf, id, SHA1_DIGEST_LENGTH) == -1)
2798 return got_error_from_errno("imsg_add ENUMERATED_COMMIT");
2799 if (imsg_add(wbuf, &mtime, sizeof(mtime)) == -1)
2800 return got_error_from_errno("imsg_add ENUMERATED_COMMIT");
2803 imsg_close(ibuf, wbuf);
2804 /* Don't flush yet, tree entries or ENUMERATION_DONE will follow. */
2808 const struct got_error *
2809 got_privsep_recv_enumerated_objects(struct imsgbuf *ibuf,
2810 got_object_enumerate_commit_cb cb_commit,
2811 got_object_enumerate_tree_cb cb_tree, void *cb_arg,
2812 struct got_repository *repo)
2814 const struct got_error *err = NULL;
2816 struct got_imsg_enumerated_commit *icommit = NULL;
2817 struct got_object_id commit_id;
2818 int have_commit = 0;
2820 struct got_tree_object tree;
2821 struct got_imsg_enumerated_tree *itree;
2822 struct got_object_id tree_id;
2823 char *path = NULL, *canon_path = NULL;
2824 size_t datalen, path_len;
2828 memset(&tree, 0, sizeof(tree));
2831 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2835 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2836 switch (imsg.hdr.type) {
2837 case GOT_IMSG_ENUMERATED_COMMIT:
2838 if (have_commit && nentries != -1) {
2839 err = got_error(GOT_ERR_PRIVSEP_MSG);
2842 if (datalen != sizeof(*icommit)) {
2843 err = got_error(GOT_ERR_PRIVSEP_LEN);
2846 icommit = (struct got_imsg_enumerated_commit *)imsg.data;
2847 memcpy(commit_id.sha1, icommit->id, SHA1_DIGEST_LENGTH);
2848 mtime = icommit->mtime;
2851 case GOT_IMSG_ENUMERATED_TREE:
2852 /* Should be preceeded by GOT_IMSG_ENUMERATED_COMMIT. */
2854 err = got_error(GOT_ERR_PRIVSEP_MSG);
2857 if (datalen < sizeof(*itree)) {
2858 err = got_error(GOT_ERR_PRIVSEP_LEN);
2862 path_len = datalen - sizeof(*itree);
2863 if (path_len == 0) {
2864 err = got_error(GOT_ERR_PRIVSEP_LEN);
2867 memcpy(tree_id.sha1, itree->id, sizeof(tree_id.sha1));
2869 path = malloc(path_len + 1);
2871 err = got_error_from_errno("malloc");
2875 canon_path = malloc(path_len + 1);
2876 if (canon_path == NULL) {
2877 err = got_error_from_errno("malloc");
2880 memcpy(path, (uint8_t *)imsg.data + sizeof(*itree),
2882 path[path_len] = '\0';
2883 if (!got_path_is_absolute(path)) {
2884 err = got_error(GOT_ERR_BAD_PATH);
2887 if (got_path_is_root_dir(path)) {
2888 /* XXX check what got_canonpath() does wrong */
2889 canon_path[0] = '/';
2890 canon_path[1] = '\0';
2892 err = got_canonpath(path, canon_path,
2897 if (strcmp(path, canon_path) != 0) {
2898 err = got_error(GOT_ERR_BAD_PATH);
2901 if (nentries != -1) {
2902 err = got_error(GOT_ERR_PRIVSEP_MSG);
2905 if (itree->nentries < -1) {
2906 err = got_error(GOT_ERR_PRIVSEP_MSG);
2909 if (itree->nentries == -1) {
2910 /* Tree was not found in pack file. */
2912 err = cb_tree(cb_arg, NULL, mtime, &tree_id,
2916 if (itree->nentries > INT_MAX) {
2917 err = got_error(GOT_ERR_PRIVSEP_LEN);
2920 tree.entries = calloc(itree->nentries,
2921 sizeof(struct got_tree_entry));
2922 if (tree.entries == NULL) {
2923 err = got_error_from_errno("calloc");
2926 if (itree->nentries == 0) {
2927 err = cb_tree(cb_arg, &tree, mtime, &tree_id,
2932 /* Prepare for next tree. */
2934 memset(&tree, 0, sizeof(tree));
2937 tree.nentries = itree->nentries;
2941 case GOT_IMSG_TREE_ENTRIES:
2942 /* Should be preceeded by GOT_IMSG_ENUMERATED_TREE. */
2943 if (nentries <= -1) {
2944 err = got_error(GOT_ERR_PRIVSEP_MSG);
2947 err = recv_tree_entries(imsg.data, datalen,
2951 if (tree.nentries == nentries) {
2952 err = cb_tree(cb_arg, &tree, mtime, &tree_id,
2957 /* Prepare for next tree. */
2959 memset(&tree, 0, sizeof(tree));
2963 case GOT_IMSG_TREE_ENUMERATION_DONE:
2964 /* All trees have been found and traversed. */
2965 if (!have_commit || path == NULL || nentries != -1) {
2966 err = got_error(GOT_ERR_PRIVSEP_MSG);
2969 err = cb_commit(cb_arg, mtime, &commit_id, repo);
2974 case GOT_IMSG_OBJECT_ENUMERATION_DONE:
2978 err = got_error(GOT_ERR_PRIVSEP_MSG);
2993 const struct got_error *
2994 got_privsep_send_raw_delta_req(struct imsgbuf *ibuf, int idx,
2995 struct got_object_id *id)
2997 struct got_imsg_raw_delta_request dreq;
3000 memcpy(dreq.id, id->sha1, SHA1_DIGEST_LENGTH);
3002 if (imsg_compose(ibuf, GOT_IMSG_RAW_DELTA_REQUEST, 0, 0, -1,
3003 &dreq, sizeof(dreq)) == -1)
3004 return got_error_from_errno("imsg_compose RAW_DELTA_REQUEST");
3006 return flush_imsg(ibuf);
3009 const struct got_error *
3010 got_privsep_send_raw_delta_outfd(struct imsgbuf *ibuf, int fd)
3012 return send_fd(ibuf, GOT_IMSG_RAW_DELTA_OUTFD, fd);
3015 const struct got_error *
3016 got_privsep_send_raw_delta(struct imsgbuf *ibuf, uint64_t base_size,
3017 uint64_t result_size, off_t delta_size, off_t delta_compressed_size,
3018 off_t delta_offset, off_t delta_out_offset, struct got_object_id *base_id)
3020 struct got_imsg_raw_delta idelta;
3023 idelta.base_size = base_size;
3024 idelta.result_size = result_size;
3025 idelta.delta_size = delta_size;
3026 idelta.delta_compressed_size = delta_compressed_size;
3027 idelta.delta_offset = delta_offset;
3028 idelta.delta_out_offset = delta_out_offset;
3029 memcpy(idelta.base_id, base_id->sha1, SHA1_DIGEST_LENGTH);
3031 ret = imsg_compose(ibuf, GOT_IMSG_RAW_DELTA, 0, 0, -1,
3032 &idelta, sizeof(idelta));
3034 return got_error_from_errno("imsg_compose RAW_DELTA");
3036 return flush_imsg(ibuf);
3039 const struct got_error *
3040 got_privsep_recv_raw_delta(uint64_t *base_size, uint64_t *result_size,
3041 off_t *delta_size, off_t *delta_compressed_size, off_t *delta_offset,
3042 off_t *delta_out_offset, struct got_object_id **base_id, struct imsgbuf *ibuf)
3044 const struct got_error *err = NULL;
3046 struct got_imsg_raw_delta *delta;
3052 *delta_compressed_size = 0;
3054 *delta_out_offset = 0;
3057 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
3061 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
3063 switch (imsg.hdr.type) {
3064 case GOT_IMSG_RAW_DELTA:
3065 if (datalen != sizeof(*delta)) {
3066 err = got_error(GOT_ERR_PRIVSEP_LEN);
3070 *base_size = delta->base_size;
3071 *result_size = delta->result_size;
3072 *delta_size = delta->delta_size;
3073 *delta_compressed_size = delta->delta_compressed_size;
3074 *delta_offset = delta->delta_offset;
3075 *delta_out_offset = delta->delta_out_offset;
3076 *base_id = calloc(1, sizeof(**base_id));
3077 if (*base_id == NULL) {
3078 err = got_error_from_errno("malloc");
3081 memcpy((*base_id)->sha1, delta->base_id, SHA1_DIGEST_LENGTH);
3084 err = got_error(GOT_ERR_PRIVSEP_MSG);
3097 static const struct got_error *
3098 send_idlist(struct imsgbuf *ibuf, struct got_object_id **ids, size_t nids)
3100 const struct got_error *err = NULL;
3101 struct got_imsg_object_idlist idlist;
3105 if (nids > GOT_IMSG_OBJ_ID_LIST_MAX_NIDS)
3106 return got_error(GOT_ERR_NO_SPACE);
3108 wbuf = imsg_create(ibuf, GOT_IMSG_OBJ_ID_LIST, 0, 0,
3109 sizeof(idlist) + nids * sizeof(**ids));
3111 err = got_error_from_errno("imsg_create OBJ_ID_LIST");
3116 if (imsg_add(wbuf, &idlist, sizeof(idlist)) == -1)
3117 return got_error_from_errno("imsg_add OBJ_ID_LIST");
3119 for (i = 0; i < nids; i++) {
3120 struct got_object_id *id = ids[i];
3121 if (imsg_add(wbuf, id, sizeof(*id)) == -1)
3122 return got_error_from_errno("imsg_add OBJ_ID_LIST");
3126 imsg_close(ibuf, wbuf);
3128 return flush_imsg(ibuf);
3131 const struct got_error *
3132 got_privsep_send_object_idlist(struct imsgbuf *ibuf,
3133 struct got_object_id **ids, size_t nids)
3135 const struct got_error *err = NULL;
3136 struct got_object_id *idlist[GOT_IMSG_OBJ_ID_LIST_MAX_NIDS];
3139 for (i = 0; i < nids; i++) {
3140 j = i % nitems(idlist);
3142 if (j >= nitems(idlist) - 1) {
3143 err = send_idlist(ibuf, idlist, j + 1);
3151 err = send_idlist(ibuf, idlist, j + 1);
3159 const struct got_error *
3160 got_privsep_send_object_idlist_done(struct imsgbuf *ibuf)
3162 if (imsg_compose(ibuf, GOT_IMSG_OBJ_ID_LIST_DONE, 0, 0, -1, NULL, 0)
3164 return got_error_from_errno("imsg_compose OBJ_ID_LIST_DONE");
3166 return flush_imsg(ibuf);
3169 const struct got_error *
3170 got_privsep_recv_object_idlist(int *done, struct got_object_id **ids,
3171 size_t *nids, struct imsgbuf *ibuf)
3173 const struct got_error *err = NULL;
3175 struct got_imsg_object_idlist *idlist;
3182 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
3186 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
3187 switch (imsg.hdr.type) {
3188 case GOT_IMSG_OBJ_ID_LIST:
3189 if (datalen < sizeof(*idlist)) {
3190 err = got_error(GOT_ERR_PRIVSEP_LEN);
3194 if (idlist->nids > GOT_IMSG_OBJ_ID_LIST_MAX_NIDS) {
3195 err = got_error(GOT_ERR_PRIVSEP_LEN);
3198 *nids = idlist->nids;
3199 *ids = calloc(*nids, sizeof(**ids));
3201 err = got_error_from_errno("calloc");
3204 memcpy(*ids, (uint8_t *)imsg.data + sizeof(idlist),
3205 *nids * sizeof(**ids));
3207 case GOT_IMSG_OBJ_ID_LIST_DONE:
3211 err = got_error(GOT_ERR_PRIVSEP_MSG);
3220 const struct got_error *
3221 got_privsep_send_delta_reuse_req(struct imsgbuf *ibuf)
3223 if (imsg_compose(ibuf, GOT_IMSG_DELTA_REUSE_REQUEST, 0, 0, -1, NULL, 0)
3225 return got_error_from_errno("imsg_compose DELTA_REUSE_REQUEST");
3227 return flush_imsg(ibuf);
3230 const struct got_error *
3231 got_privsep_send_reused_deltas(struct imsgbuf *ibuf,
3232 struct got_imsg_reused_delta *deltas, size_t ndeltas)
3234 const struct got_error *err = NULL;
3236 struct got_imsg_reused_deltas ideltas;
3239 if (ndeltas > GOT_IMSG_REUSED_DELTAS_MAX_NDELTAS)
3240 return got_error(GOT_ERR_NO_SPACE);
3242 wbuf = imsg_create(ibuf, GOT_IMSG_REUSED_DELTAS, 0, 0,
3243 sizeof(ideltas) + ndeltas * sizeof(*deltas));
3245 err = got_error_from_errno("imsg_create REUSED_DELTAS");
3249 ideltas.ndeltas = ndeltas;
3250 if (imsg_add(wbuf, &ideltas, sizeof(ideltas)) == -1)
3251 return got_error_from_errno("imsg_add REUSED_DELTAS");
3253 for (i = 0; i < ndeltas; i++) {
3254 struct got_imsg_reused_delta *delta = &deltas[i];
3255 if (imsg_add(wbuf, delta, sizeof(*delta)) == -1)
3256 return got_error_from_errno("imsg_add REUSED_DELTAS");
3260 imsg_close(ibuf, wbuf);
3262 return flush_imsg(ibuf);
3265 const struct got_error *
3266 got_privsep_send_reused_deltas_done(struct imsgbuf *ibuf)
3268 if (imsg_compose(ibuf, GOT_IMSG_DELTA_REUSE_DONE, 0, 0, -1, NULL, 0)
3270 return got_error_from_errno("imsg_compose DELTA_REUSE_DONE");
3272 return flush_imsg(ibuf);
3275 const struct got_error *
3276 got_privsep_recv_reused_deltas(int *done, struct got_imsg_reused_delta *deltas,
3277 size_t *ndeltas, struct imsgbuf *ibuf)
3279 const struct got_error *err = NULL;
3281 struct got_imsg_reused_deltas *ideltas;
3287 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
3291 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
3292 switch (imsg.hdr.type) {
3293 case GOT_IMSG_REUSED_DELTAS:
3294 if (datalen < sizeof(*ideltas)) {
3295 err = got_error(GOT_ERR_PRIVSEP_LEN);
3298 ideltas = imsg.data;
3299 if (ideltas->ndeltas > GOT_IMSG_OBJ_ID_LIST_MAX_NIDS) {
3300 err = got_error(GOT_ERR_PRIVSEP_LEN);
3303 *ndeltas = ideltas->ndeltas;
3304 memcpy(deltas, (uint8_t *)imsg.data + sizeof(ideltas),
3305 *ndeltas * sizeof(*deltas));
3307 case GOT_IMSG_DELTA_REUSE_DONE:
3311 err = got_error(GOT_ERR_PRIVSEP_MSG);
3320 const struct got_error *
3321 got_privsep_unveil_exec_helpers(void)
3323 const char *helpers[] = {
3324 GOT_PATH_PROG_READ_PACK,
3325 GOT_PATH_PROG_READ_OBJECT,
3326 GOT_PATH_PROG_READ_COMMIT,
3327 GOT_PATH_PROG_READ_TREE,
3328 GOT_PATH_PROG_READ_BLOB,
3329 GOT_PATH_PROG_READ_TAG,
3330 GOT_PATH_PROG_READ_GITCONFIG,
3331 GOT_PATH_PROG_READ_GOTCONFIG,
3332 GOT_PATH_PROG_READ_PATCH,
3333 GOT_PATH_PROG_FETCH_PACK,
3334 GOT_PATH_PROG_INDEX_PACK,
3335 GOT_PATH_PROG_SEND_PACK,
3339 for (i = 0; i < nitems(helpers); i++) {
3340 if (unveil(helpers[i], "x") == 0)
3342 return got_error_from_errno2("unveil", helpers[i]);
3349 got_privsep_exec_child(int imsg_fds[2], const char *path, const char *repo_path)
3351 if (close(imsg_fds[0]) == -1) {
3352 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
3356 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
3357 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
3361 closefrom(GOT_IMSG_FD_CHILD + 1);
3363 if (execl(path, path, repo_path, (char *)NULL) == -1) {
3364 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,