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. */
359 *size + *hdrlen > GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX) {
360 err = got_error(GOT_ERR_PRIVSEP_LEN);
364 *outbuf = malloc(*size + *hdrlen);
365 if (*outbuf == NULL) {
366 err = got_error_from_errno("malloc");
369 memcpy(*outbuf, imsg.data + sizeof(*iobj), *size + *hdrlen);
372 err = got_error(GOT_ERR_PRIVSEP_MSG);
381 const struct got_error *
382 got_privsep_send_commit_req(struct imsgbuf *ibuf, int fd,
383 struct got_object_id *id, int pack_idx)
385 const struct got_error *err = NULL;
386 struct got_imsg_packed_object iobj;
390 if (pack_idx != -1) { /* commit is packed */
392 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
400 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_REQUEST, 0, 0, fd, data, len)
402 err = got_error_from_errno("imsg_compose COMMIT_REQUEST");
407 return flush_imsg(ibuf);
410 const struct got_error *
411 got_privsep_send_tree_req(struct imsgbuf *ibuf, int fd,
412 struct got_object_id *id, int pack_idx)
418 len = sizeof(struct got_imsg_packed_object);
422 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_REQUEST, 0, 0, len);
424 return got_error_from_errno("imsg_create TREE_REQUEST");
426 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1)
427 return got_error_from_errno("imsg_add TREE_REQUEST");
429 if (pack_idx != -1) { /* tree is packed */
430 if (imsg_add(wbuf, &pack_idx, sizeof(pack_idx)) == -1)
431 return got_error_from_errno("imsg_add TREE_REQUEST");
435 imsg_close(ibuf, wbuf);
437 return flush_imsg(ibuf);
440 const struct got_error *
441 got_privsep_send_tag_req(struct imsgbuf *ibuf, int fd,
442 struct got_object_id *id, int pack_idx)
444 struct got_imsg_packed_object iobj;
448 if (pack_idx != -1) { /* tag is packed */
450 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
458 if (imsg_compose(ibuf, GOT_IMSG_TAG_REQUEST, 0, 0, fd, data, len)
460 return got_error_from_errno("imsg_compose TAG_REQUEST");
462 return flush_imsg(ibuf);
465 const struct got_error *
466 got_privsep_send_blob_req(struct imsgbuf *ibuf, int infd,
467 struct got_object_id *id, int pack_idx)
469 const struct got_error *err = NULL;
470 struct got_imsg_packed_object iobj;
474 if (pack_idx != -1) { /* blob is packed */
476 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
484 if (imsg_compose(ibuf, GOT_IMSG_BLOB_REQUEST, 0, 0, infd, data, len)
486 err = got_error_from_errno("imsg_compose BLOB_REQUEST");
491 return flush_imsg(ibuf);
494 const struct got_error *
495 got_privsep_send_blob_outfd(struct imsgbuf *ibuf, int outfd)
497 const struct got_error *err = NULL;
499 if (imsg_compose(ibuf, GOT_IMSG_BLOB_OUTFD, 0, 0, outfd, NULL, 0)
501 err = got_error_from_errno("imsg_compose BLOB_OUTFD");
506 return flush_imsg(ibuf);
509 static const struct got_error *
510 send_fd(struct imsgbuf *ibuf, int imsg_code, int fd)
512 const struct got_error *err = NULL;
514 if (imsg_compose(ibuf, imsg_code, 0, 0, fd, NULL, 0) == -1) {
515 err = got_error_from_errno("imsg_compose TMPFD");
520 return flush_imsg(ibuf);
523 const struct got_error *
524 got_privsep_send_tmpfd(struct imsgbuf *ibuf, int fd)
526 return send_fd(ibuf, GOT_IMSG_TMPFD, fd);
529 const struct got_error *
530 got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj)
532 struct got_imsg_object iobj;
534 memcpy(iobj.id, obj->id.sha1, sizeof(iobj.id));
535 iobj.type = obj->type;
536 iobj.flags = obj->flags;
537 iobj.hdrlen = obj->hdrlen;
538 iobj.size = obj->size;
539 if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
540 iobj.pack_offset = obj->pack_offset;
541 iobj.pack_idx = obj->pack_idx;
544 if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
546 return got_error_from_errno("imsg_compose OBJECT");
548 return flush_imsg(ibuf);
551 const struct got_error *
552 got_privsep_send_fetch_req(struct imsgbuf *ibuf, int fd,
553 struct got_pathlist_head *have_refs, int fetch_all_branches,
554 struct got_pathlist_head *wanted_branches,
555 struct got_pathlist_head *wanted_refs, int list_refs_only, int verbosity)
557 const struct got_error *err = NULL;
560 struct got_pathlist_entry *pe;
561 struct got_imsg_fetch_request fetchreq;
563 memset(&fetchreq, 0, sizeof(fetchreq));
564 fetchreq.fetch_all_branches = fetch_all_branches;
565 fetchreq.list_refs_only = list_refs_only;
566 fetchreq.verbosity = verbosity;
567 TAILQ_FOREACH(pe, have_refs, entry)
568 fetchreq.n_have_refs++;
569 TAILQ_FOREACH(pe, wanted_branches, entry)
570 fetchreq.n_wanted_branches++;
571 TAILQ_FOREACH(pe, wanted_refs, entry)
572 fetchreq.n_wanted_refs++;
573 len = sizeof(struct got_imsg_fetch_request);
574 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
576 return got_error(GOT_ERR_NO_SPACE);
579 if (imsg_compose(ibuf, GOT_IMSG_FETCH_REQUEST, 0, 0, fd,
580 &fetchreq, sizeof(fetchreq)) == -1)
581 return got_error_from_errno(
582 "imsg_compose FETCH_SERVER_PROGRESS");
584 err = flush_imsg(ibuf);
591 TAILQ_FOREACH(pe, have_refs, entry) {
592 const char *name = pe->path;
593 size_t name_len = pe->path_len;
594 struct got_object_id *id = pe->data;
596 len = sizeof(struct got_imsg_fetch_have_ref) + name_len;
597 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_HAVE_REF, 0, 0, len);
599 return got_error_from_errno("imsg_create FETCH_HAVE_REF");
601 /* Keep in sync with struct got_imsg_fetch_have_ref! */
602 if (imsg_add(wbuf, id->sha1, sizeof(id->sha1)) == -1)
603 return got_error_from_errno("imsg_add FETCH_HAVE_REF");
604 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1)
605 return got_error_from_errno("imsg_add FETCH_HAVE_REF");
606 if (imsg_add(wbuf, name, name_len) == -1)
607 return got_error_from_errno("imsg_add FETCH_HAVE_REF");
610 imsg_close(ibuf, wbuf);
611 err = flush_imsg(ibuf);
616 TAILQ_FOREACH(pe, wanted_branches, entry) {
617 const char *name = pe->path;
618 size_t name_len = pe->path_len;
620 len = sizeof(struct got_imsg_fetch_wanted_branch) + name_len;
621 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_WANTED_BRANCH, 0, 0,
624 return got_error_from_errno(
625 "imsg_create FETCH_WANTED_BRANCH");
627 /* Keep in sync with struct got_imsg_fetch_wanted_branch! */
628 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1)
629 return got_error_from_errno(
630 "imsg_add FETCH_WANTED_BRANCH");
631 if (imsg_add(wbuf, name, name_len) == -1)
632 return got_error_from_errno(
633 "imsg_add FETCH_WANTED_BRANCH");
636 imsg_close(ibuf, wbuf);
637 err = flush_imsg(ibuf);
642 TAILQ_FOREACH(pe, wanted_refs, entry) {
643 const char *name = pe->path;
644 size_t name_len = pe->path_len;
646 len = sizeof(struct got_imsg_fetch_wanted_ref) + name_len;
647 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_WANTED_REF, 0, 0,
650 return got_error_from_errno(
651 "imsg_create FETCH_WANTED_REF");
653 /* Keep in sync with struct got_imsg_fetch_wanted_ref! */
654 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1)
655 return got_error_from_errno(
656 "imsg_add FETCH_WANTED_REF");
657 if (imsg_add(wbuf, name, name_len) == -1)
658 return got_error_from_errno(
659 "imsg_add FETCH_WANTED_REF");
662 imsg_close(ibuf, wbuf);
663 err = flush_imsg(ibuf);
673 const struct got_error *
674 got_privsep_send_fetch_outfd(struct imsgbuf *ibuf, int fd)
676 return send_fd(ibuf, GOT_IMSG_FETCH_OUTFD, fd);
679 const struct got_error *
680 got_privsep_recv_fetch_progress(int *done, struct got_object_id **id,
681 char **refname, struct got_pathlist_head *symrefs, char **server_progress,
682 off_t *packfile_size, uint8_t *pack_sha1, struct imsgbuf *ibuf)
684 const struct got_error *err = NULL;
687 struct got_imsg_fetch_symrefs *isymrefs = NULL;
695 *server_progress = NULL;
697 memset(pack_sha1, 0, SHA1_DIGEST_LENGTH);
699 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
703 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
704 switch (imsg.hdr.type) {
706 err = recv_imsg_error(&imsg, datalen);
708 case GOT_IMSG_FETCH_SYMREFS:
709 if (datalen < sizeof(*isymrefs)) {
710 err = got_error(GOT_ERR_PRIVSEP_LEN);
713 if (isymrefs != NULL) {
714 err = got_error(GOT_ERR_PRIVSEP_MSG);
717 isymrefs = (struct got_imsg_fetch_symrefs *)imsg.data;
718 off = sizeof(*isymrefs);
719 remain = datalen - off;
720 for (n = 0; n < isymrefs->nsymrefs; n++) {
721 struct got_imsg_fetch_symref *s;
723 if (remain < sizeof(struct got_imsg_fetch_symref)) {
724 err = got_error(GOT_ERR_PRIVSEP_LEN);
727 s = (struct got_imsg_fetch_symref *)(imsg.data + off);
729 remain -= sizeof(*s);
730 if (remain < s->name_len) {
731 err = got_error(GOT_ERR_PRIVSEP_LEN);
734 name = strndup(imsg.data + off, s->name_len);
736 err = got_error_from_errno("strndup");
740 remain -= s->name_len;
741 if (remain < s->target_len) {
742 err = got_error(GOT_ERR_PRIVSEP_LEN);
746 target = strndup(imsg.data + off, s->target_len);
747 if (target == NULL) {
748 err = got_error_from_errno("strndup");
752 off += s->target_len;
753 remain -= s->target_len;
754 err = got_pathlist_append(symrefs, name, target);
762 case GOT_IMSG_FETCH_REF:
763 if (datalen <= SHA1_DIGEST_LENGTH) {
764 err = got_error(GOT_ERR_PRIVSEP_MSG);
767 *id = malloc(sizeof(**id));
769 err = got_error_from_errno("malloc");
772 memcpy((*id)->sha1, imsg.data, SHA1_DIGEST_LENGTH);
773 *refname = strndup(imsg.data + SHA1_DIGEST_LENGTH,
774 datalen - SHA1_DIGEST_LENGTH);
775 if (*refname == NULL) {
776 err = got_error_from_errno("strndup");
780 case GOT_IMSG_FETCH_SERVER_PROGRESS:
782 err = got_error(GOT_ERR_PRIVSEP_LEN);
785 *server_progress = strndup(imsg.data, datalen);
786 if (*server_progress == NULL) {
787 err = got_error_from_errno("strndup");
790 for (i = 0; i < datalen; i++) {
791 if (!isprint((unsigned char)(*server_progress)[i]) &&
792 !isspace((unsigned char)(*server_progress)[i])) {
793 err = got_error(GOT_ERR_PRIVSEP_MSG);
794 free(*server_progress);
795 *server_progress = NULL;
800 case GOT_IMSG_FETCH_DOWNLOAD_PROGRESS:
801 if (datalen < sizeof(*packfile_size)) {
802 err = got_error(GOT_ERR_PRIVSEP_MSG);
805 memcpy(packfile_size, imsg.data, sizeof(*packfile_size));
807 case GOT_IMSG_FETCH_DONE:
808 if (datalen != SHA1_DIGEST_LENGTH) {
809 err = got_error(GOT_ERR_PRIVSEP_MSG);
812 memcpy(pack_sha1, imsg.data, SHA1_DIGEST_LENGTH);
816 err = got_error(GOT_ERR_PRIVSEP_MSG);
830 static const struct got_error *
831 send_send_ref(const char *name, size_t name_len, struct got_object_id *id,
832 int delete, struct imsgbuf *ibuf)
837 len = sizeof(struct got_imsg_send_ref) + name_len;
838 wbuf = imsg_create(ibuf, GOT_IMSG_SEND_REF, 0, 0, len);
840 return got_error_from_errno("imsg_create SEND_REF");
842 /* Keep in sync with struct got_imsg_send_ref! */
843 if (imsg_add(wbuf, id->sha1, sizeof(id->sha1)) == -1)
844 return got_error_from_errno("imsg_add SEND_REF");
845 if (imsg_add(wbuf, &delete, sizeof(delete)) == -1)
846 return got_error_from_errno("imsg_add SEND_REF");
847 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1)
848 return got_error_from_errno("imsg_add SEND_REF");
849 if (imsg_add(wbuf, name, name_len) == -1)
850 return got_error_from_errno("imsg_add SEND_REF");
853 imsg_close(ibuf, wbuf);
854 return flush_imsg(ibuf);
857 const struct got_error *
858 got_privsep_send_send_req(struct imsgbuf *ibuf, int fd,
859 struct got_pathlist_head *have_refs,
860 struct got_pathlist_head *delete_refs,
863 const struct got_error *err = NULL;
864 struct got_pathlist_entry *pe;
865 struct got_imsg_send_request sendreq;
866 struct got_object_id zero_id;
868 memset(&zero_id, 0, sizeof(zero_id));
869 memset(&sendreq, 0, sizeof(sendreq));
870 sendreq.verbosity = verbosity;
871 TAILQ_FOREACH(pe, have_refs, entry)
873 TAILQ_FOREACH(pe, delete_refs, entry)
875 if (imsg_compose(ibuf, GOT_IMSG_SEND_REQUEST, 0, 0, fd,
876 &sendreq, sizeof(sendreq)) == -1) {
877 err = got_error_from_errno(
878 "imsg_compose FETCH_SERVER_PROGRESS");
882 err = flush_imsg(ibuf);
887 TAILQ_FOREACH(pe, have_refs, entry) {
888 const char *name = pe->path;
889 size_t name_len = pe->path_len;
890 struct got_object_id *id = pe->data;
891 err = send_send_ref(name, name_len, id, 0, ibuf);
896 TAILQ_FOREACH(pe, delete_refs, entry) {
897 const char *name = pe->path;
898 size_t name_len = pe->path_len;
899 err = send_send_ref(name, name_len, &zero_id, 1, ibuf);
904 if (fd != -1 && close(fd) == -1 && err == NULL)
905 err = got_error_from_errno("close");
910 const struct got_error *
911 got_privsep_recv_send_remote_refs(struct got_pathlist_head *remote_refs,
912 struct imsgbuf *ibuf)
914 const struct got_error *err = NULL;
918 struct got_imsg_send_remote_ref iremote_ref;
919 struct got_object_id *id = NULL;
920 char *refname = NULL;
921 struct got_pathlist_entry *new;
924 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
927 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
928 switch (imsg.hdr.type) {
930 err = recv_imsg_error(&imsg, datalen);
932 case GOT_IMSG_SEND_REMOTE_REF:
933 if (datalen < sizeof(iremote_ref)) {
934 err = got_error(GOT_ERR_PRIVSEP_MSG);
937 memcpy(&iremote_ref, imsg.data, sizeof(iremote_ref));
938 if (datalen != sizeof(iremote_ref) +
939 iremote_ref.name_len) {
940 err = got_error(GOT_ERR_PRIVSEP_MSG);
943 id = malloc(sizeof(*id));
945 err = got_error_from_errno("malloc");
948 memcpy(id->sha1, iremote_ref.id, SHA1_DIGEST_LENGTH);
949 refname = strndup(imsg.data + sizeof(iremote_ref),
950 datalen - sizeof(iremote_ref));
951 if (refname == NULL) {
952 err = got_error_from_errno("strndup");
955 err = got_pathlist_insert(&new, remote_refs,
959 if (new == NULL) { /* duplicate which wasn't inserted */
966 case GOT_IMSG_SEND_PACK_REQUEST:
968 err = got_error(GOT_ERR_PRIVSEP_MSG);
971 /* got-send-pack is now waiting for a pack file. */
975 err = got_error(GOT_ERR_PRIVSEP_MSG);
986 const struct got_error *
987 got_privsep_send_packfd(struct imsgbuf *ibuf, int fd)
989 return send_fd(ibuf, GOT_IMSG_SEND_PACKFD, fd);
992 const struct got_error *
993 got_privsep_recv_send_progress(int *done, off_t *bytes_sent,
994 int *success, char **refname, struct imsgbuf *ibuf)
996 const struct got_error *err = NULL;
999 struct got_imsg_send_ref_status iref_status;
1001 /* Do not reset the current value of 'bytes_sent', it accumulates. */
1006 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1010 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1011 switch (imsg.hdr.type) {
1012 case GOT_IMSG_ERROR:
1013 err = recv_imsg_error(&imsg, datalen);
1015 case GOT_IMSG_SEND_UPLOAD_PROGRESS:
1016 if (datalen < sizeof(*bytes_sent)) {
1017 err = got_error(GOT_ERR_PRIVSEP_MSG);
1020 memcpy(bytes_sent, imsg.data, sizeof(*bytes_sent));
1022 case GOT_IMSG_SEND_REF_STATUS:
1023 if (datalen < sizeof(iref_status)) {
1024 err = got_error(GOT_ERR_PRIVSEP_MSG);
1027 memcpy(&iref_status, imsg.data, sizeof(iref_status));
1028 if (datalen != sizeof(iref_status) + iref_status.name_len) {
1029 err = got_error(GOT_ERR_PRIVSEP_MSG);
1032 *success = iref_status.success;
1033 *refname = strndup(imsg.data + sizeof(iref_status),
1034 iref_status.name_len);
1036 case GOT_IMSG_SEND_DONE:
1038 err = got_error(GOT_ERR_PRIVSEP_MSG);
1044 err = got_error(GOT_ERR_PRIVSEP_MSG);
1052 const struct got_error *
1053 got_privsep_send_index_pack_req(struct imsgbuf *ibuf, uint8_t *pack_sha1,
1056 const struct got_error *err = NULL;
1058 /* Keep in sync with struct got_imsg_index_pack_request */
1059 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_REQUEST, 0, 0, fd,
1060 pack_sha1, SHA1_DIGEST_LENGTH) == -1) {
1061 err = got_error_from_errno("imsg_compose INDEX_REQUEST");
1065 return flush_imsg(ibuf);
1068 const struct got_error *
1069 got_privsep_send_index_pack_outfd(struct imsgbuf *ibuf, int fd)
1071 return send_fd(ibuf, GOT_IMSG_IDXPACK_OUTFD, fd);
1074 const struct got_error *
1075 got_privsep_recv_index_progress(int *done, int *nobj_total,
1076 int *nobj_indexed, int *nobj_loose, int *nobj_resolved,
1077 struct imsgbuf *ibuf)
1079 const struct got_error *err = NULL;
1081 struct got_imsg_index_pack_progress *iprogress;
1089 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1093 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1094 switch (imsg.hdr.type) {
1095 case GOT_IMSG_ERROR:
1096 err = recv_imsg_error(&imsg, datalen);
1098 case GOT_IMSG_IDXPACK_PROGRESS:
1099 if (datalen < sizeof(*iprogress)) {
1100 err = got_error(GOT_ERR_PRIVSEP_LEN);
1103 iprogress = (struct got_imsg_index_pack_progress *)imsg.data;
1104 if (iprogress->nobj_total < 0 || iprogress->nobj_indexed < 0 ||
1105 iprogress->nobj_loose < 0 || iprogress->nobj_resolved < 0) {
1106 err = got_error(GOT_ERR_RANGE);
1109 *nobj_total = iprogress->nobj_total;
1110 *nobj_indexed = iprogress->nobj_indexed;
1111 *nobj_loose = iprogress->nobj_loose;
1112 *nobj_resolved = iprogress->nobj_resolved;
1114 case GOT_IMSG_IDXPACK_DONE:
1116 err = got_error(GOT_ERR_PRIVSEP_LEN);
1122 err = got_error(GOT_ERR_PRIVSEP_MSG);
1130 const struct got_error *
1131 got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
1132 struct imsgbuf *ibuf)
1134 struct got_imsg_object *iobj;
1135 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1137 if (datalen != sizeof(*iobj))
1138 return got_error(GOT_ERR_PRIVSEP_LEN);
1141 if (iobj->pack_offset < 0)
1142 return got_error(GOT_ERR_PACK_OFFSET);
1144 *obj = calloc(1, sizeof(**obj));
1146 return got_error_from_errno("calloc");
1148 memcpy((*obj)->id.sha1, iobj->id, SHA1_DIGEST_LENGTH);
1149 (*obj)->type = iobj->type;
1150 (*obj)->flags = iobj->flags;
1151 (*obj)->hdrlen = iobj->hdrlen;
1152 (*obj)->size = iobj->size;
1153 /* path_packfile is handled by caller */
1154 if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
1155 (*obj)->pack_offset = iobj->pack_offset;
1156 (*obj)->pack_idx = iobj->pack_idx;
1158 STAILQ_INIT(&(*obj)->deltas.entries);
1162 const struct got_error *
1163 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
1165 const struct got_error *err = NULL;
1167 const size_t min_datalen =
1168 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
1172 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1176 switch (imsg.hdr.type) {
1177 case GOT_IMSG_OBJECT:
1178 err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
1181 err = got_error(GOT_ERR_PRIVSEP_MSG);
1190 static const struct got_error *
1191 send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
1194 const struct got_error *err = NULL;
1195 size_t offset, remain;
1198 remain = logmsg_len;
1199 while (remain > 0) {
1200 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1202 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
1203 commit->logmsg + offset, n) == -1) {
1204 err = got_error_from_errno("imsg_compose "
1209 err = flush_imsg(ibuf);
1220 const struct got_error *
1221 got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
1223 const struct got_error *err = NULL;
1224 struct got_imsg_commit_object *icommit;
1227 struct got_object_qid *qid;
1228 size_t author_len = strlen(commit->author);
1229 size_t committer_len = strlen(commit->committer);
1230 size_t logmsg_len = strlen(commit->logmsg);
1232 total = sizeof(*icommit) + author_len + committer_len +
1233 commit->nparents * SHA1_DIGEST_LENGTH;
1235 buf = malloc(total);
1237 return got_error_from_errno("malloc");
1239 icommit = (struct got_imsg_commit_object *)buf;
1240 memcpy(icommit->tree_id, commit->tree_id->sha1,
1241 sizeof(icommit->tree_id));
1242 icommit->author_len = author_len;
1243 icommit->author_time = commit->author_time;
1244 icommit->author_gmtoff = commit->author_gmtoff;
1245 icommit->committer_len = committer_len;
1246 icommit->committer_time = commit->committer_time;
1247 icommit->committer_gmtoff = commit->committer_gmtoff;
1248 icommit->logmsg_len = logmsg_len;
1249 icommit->nparents = commit->nparents;
1251 len = sizeof(*icommit);
1252 memcpy(buf + len, commit->author, author_len);
1254 memcpy(buf + len, commit->committer, committer_len);
1255 len += committer_len;
1256 STAILQ_FOREACH(qid, &commit->parent_ids, entry) {
1257 memcpy(buf + len, &qid->id, SHA1_DIGEST_LENGTH);
1258 len += SHA1_DIGEST_LENGTH;
1261 if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
1262 err = got_error_from_errno("imsg_compose COMMIT");
1266 if (logmsg_len == 0 ||
1267 logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1268 err = flush_imsg(ibuf);
1272 err = send_commit_logmsg(ibuf, commit, logmsg_len);
1278 static const struct got_error *
1279 get_commit_from_imsg(struct got_commit_object **commit,
1280 struct imsg *imsg, size_t datalen, struct imsgbuf *ibuf)
1282 const struct got_error *err = NULL;
1283 struct got_imsg_commit_object *icommit;
1287 if (datalen < sizeof(*icommit))
1288 return got_error(GOT_ERR_PRIVSEP_LEN);
1290 icommit = imsg->data;
1291 if (datalen != sizeof(*icommit) + icommit->author_len +
1292 icommit->committer_len +
1293 icommit->nparents * SHA1_DIGEST_LENGTH)
1294 return got_error(GOT_ERR_PRIVSEP_LEN);
1296 if (icommit->nparents < 0)
1297 return got_error(GOT_ERR_PRIVSEP_LEN);
1299 len += sizeof(*icommit);
1301 *commit = got_object_commit_alloc_partial();
1302 if (*commit == NULL)
1303 return got_error_from_errno(
1304 "got_object_commit_alloc_partial");
1306 memcpy((*commit)->tree_id->sha1, icommit->tree_id,
1307 SHA1_DIGEST_LENGTH);
1308 (*commit)->author_time = icommit->author_time;
1309 (*commit)->author_gmtoff = icommit->author_gmtoff;
1310 (*commit)->committer_time = icommit->committer_time;
1311 (*commit)->committer_gmtoff = icommit->committer_gmtoff;
1313 (*commit)->author = strndup(imsg->data + len, icommit->author_len);
1314 if ((*commit)->author == NULL) {
1315 err = got_error_from_errno("strndup");
1318 len += icommit->author_len;
1320 (*commit)->committer = strndup(imsg->data + len,
1321 icommit->committer_len);
1322 if ((*commit)->committer == NULL) {
1323 err = got_error_from_errno("strndup");
1326 len += icommit->committer_len;
1328 if (icommit->logmsg_len == 0) {
1329 (*commit)->logmsg = strdup("");
1330 if ((*commit)->logmsg == NULL) {
1331 err = got_error_from_errno("strdup");
1335 size_t offset = 0, remain = icommit->logmsg_len;
1337 (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
1338 if ((*commit)->logmsg == NULL) {
1339 err = got_error_from_errno("malloc");
1342 while (remain > 0) {
1343 struct imsg imsg_log;
1344 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1347 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1351 if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG) {
1352 err = got_error(GOT_ERR_PRIVSEP_MSG);
1356 memcpy((*commit)->logmsg + offset,
1358 imsg_free(&imsg_log);
1362 (*commit)->logmsg[icommit->logmsg_len] = '\0';
1365 for (i = 0; i < icommit->nparents; i++) {
1366 struct got_object_qid *qid;
1368 err = got_object_qid_alloc_partial(&qid);
1371 memcpy(&qid->id, imsg->data + len +
1372 i * SHA1_DIGEST_LENGTH, sizeof(qid->id));
1373 STAILQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
1374 (*commit)->nparents++;
1378 got_object_commit_close(*commit);
1384 const struct got_error *
1385 got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
1387 const struct got_error *err = NULL;
1390 const size_t min_datalen =
1391 MIN(sizeof(struct got_imsg_error),
1392 sizeof(struct got_imsg_commit_object));
1396 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1400 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1402 switch (imsg.hdr.type) {
1403 case GOT_IMSG_COMMIT:
1404 err = get_commit_from_imsg(commit, &imsg, datalen, ibuf);
1407 err = got_error(GOT_ERR_PRIVSEP_MSG);
1416 static const struct got_error *
1417 send_tree_entries_batch(struct imsgbuf *ibuf,
1418 struct got_parsed_tree_entry *entries, int idx0, int idxN, size_t len)
1421 struct got_imsg_tree_entries ientries;
1424 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_ENTRIES, 0, 0, len);
1426 return got_error_from_errno("imsg_create TREE_ENTRY");
1428 ientries.nentries = idxN - idx0 + 1;
1429 if (imsg_add(wbuf, &ientries, sizeof(ientries)) == -1)
1430 return got_error_from_errno("imsg_add TREE_ENTRY");
1432 for (i = idx0; i <= idxN; i++) {
1433 struct got_parsed_tree_entry *pte = &entries[i];
1435 /* Keep in sync with struct got_imsg_tree_object definition! */
1436 if (imsg_add(wbuf, pte->id, SHA1_DIGEST_LENGTH) == -1)
1437 return got_error_from_errno("imsg_add TREE_ENTRY");
1438 if (imsg_add(wbuf, &pte->mode, sizeof(pte->mode)) == -1)
1439 return got_error_from_errno("imsg_add TREE_ENTRY");
1440 if (imsg_add(wbuf, &pte->namelen, sizeof(pte->namelen)) == -1)
1441 return got_error_from_errno("imsg_add TREE_ENTRY");
1443 /* Remaining bytes are the entry's name. */
1444 if (imsg_add(wbuf, pte->name, pte->namelen) == -1)
1445 return got_error_from_errno("imsg_add TREE_ENTRY");
1449 imsg_close(ibuf, wbuf);
1453 static const struct got_error *
1454 send_tree_entries(struct imsgbuf *ibuf, struct got_parsed_tree_entry *entries,
1457 const struct got_error *err = NULL;
1459 size_t entries_len = sizeof(struct got_imsg_tree_entries);
1462 for (j = 0; j < nentries; j++) {
1463 struct got_parsed_tree_entry *pte = &entries[j];
1464 size_t len = sizeof(*pte) + pte->namelen;
1467 entries_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1468 err = send_tree_entries_batch(ibuf, entries,
1469 i, j - 1, entries_len);
1473 entries_len = sizeof(struct got_imsg_tree_entries);
1480 err = send_tree_entries_batch(ibuf, entries, i, j - 1,
1489 const struct got_error *
1490 got_privsep_send_tree(struct imsgbuf *ibuf,
1491 struct got_parsed_tree_entry *entries, int nentries)
1493 const struct got_error *err = NULL;
1494 struct got_imsg_tree_object itree;
1496 itree.nentries = nentries;
1497 if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
1499 return got_error_from_errno("imsg_compose TREE");
1501 err = send_tree_entries(ibuf, entries, nentries);
1505 return flush_imsg(ibuf);
1509 static const struct got_error *
1510 recv_tree_entries(void *data, size_t datalen, struct got_tree_object *tree,
1513 const struct got_error *err = NULL;
1514 struct got_imsg_tree_entries *ientries;
1515 struct got_tree_entry *te;
1519 if (datalen <= sizeof(*ientries) ||
1520 datalen > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
1521 return got_error(GOT_ERR_PRIVSEP_LEN);
1523 ientries = (struct got_imsg_tree_entries *)data;
1524 if (ientries->nentries > INT_MAX) {
1525 return got_error_msg(GOT_ERR_NO_SPACE,
1526 "too many tree entries");
1529 te_offset = sizeof(*ientries);
1530 for (i = 0; i < ientries->nentries; i++) {
1531 struct got_imsg_tree_entry ite;
1532 const char *te_name;
1533 uint8_t *buf = (uint8_t *)data + te_offset;
1535 if (te_offset >= datalen) {
1536 err = got_error(GOT_ERR_PRIVSEP_LEN);
1540 /* Might not be aligned, size is ~32 bytes. */
1541 memcpy(&ite, buf, sizeof(ite));
1543 if (ite.namelen >= sizeof(te->name)) {
1544 err = got_error(GOT_ERR_PRIVSEP_LEN);
1547 if (te_offset + sizeof(ite) + ite.namelen > datalen) {
1548 err = got_error(GOT_ERR_PRIVSEP_LEN);
1552 if (*nentries >= tree->nentries) {
1553 err = got_error(GOT_ERR_PRIVSEP_LEN);
1556 te = &tree->entries[*nentries];
1557 te_name = buf + sizeof(ite);
1558 memcpy(te->name, te_name, ite.namelen);
1559 te->name[ite.namelen] = '\0';
1560 memcpy(te->id.sha1, ite.id, SHA1_DIGEST_LENGTH);
1561 te->mode = ite.mode;
1562 te->idx = *nentries;
1565 te_offset += sizeof(ite) + ite.namelen;
1571 const struct got_error *
1572 got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
1574 const struct got_error *err = NULL;
1575 const size_t min_datalen =
1576 MIN(sizeof(struct got_imsg_error),
1577 sizeof(struct got_imsg_tree_object));
1578 struct got_imsg_tree_object *itree;
1583 err = read_imsg(ibuf);
1592 n = imsg_get(ibuf, &imsg);
1595 if (nentries < (*tree)->nentries) {
1596 err = read_imsg(ibuf);
1603 err = got_error(GOT_ERR_PRIVSEP_MSG);
1608 if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen) {
1610 err = got_error(GOT_ERR_PRIVSEP_LEN);
1614 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1616 switch (imsg.hdr.type) {
1617 case GOT_IMSG_ERROR:
1618 err = recv_imsg_error(&imsg, datalen);
1621 /* This message should only appear once. */
1622 if (*tree != NULL) {
1623 err = got_error(GOT_ERR_PRIVSEP_MSG);
1626 if (datalen != sizeof(*itree)) {
1627 err = got_error(GOT_ERR_PRIVSEP_LEN);
1631 if (itree->nentries < 0) {
1632 err = got_error(GOT_ERR_PRIVSEP_LEN);
1635 *tree = malloc(sizeof(**tree));
1636 if (*tree == NULL) {
1637 err = got_error_from_errno("malloc");
1640 (*tree)->entries = calloc(itree->nentries,
1641 sizeof(struct got_tree_entry));
1642 if ((*tree)->entries == NULL) {
1643 err = got_error_from_errno("malloc");
1648 (*tree)->nentries = itree->nentries;
1649 (*tree)->refcnt = 0;
1651 case GOT_IMSG_TREE_ENTRIES:
1652 /* This message should be preceeded by GOT_IMSG_TREE. */
1653 if (*tree == NULL) {
1654 err = got_error(GOT_ERR_PRIVSEP_MSG);
1657 err = recv_tree_entries(imsg.data, datalen,
1661 err = got_error(GOT_ERR_PRIVSEP_MSG);
1670 if (*tree && (*tree)->nentries != nentries) {
1672 err = got_error(GOT_ERR_PRIVSEP_LEN);
1673 got_object_tree_close(*tree);
1680 const struct got_error *
1681 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
1682 const uint8_t *data)
1684 struct got_imsg_blob iblob;
1687 iblob.hdrlen = hdrlen;
1692 if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
1693 return got_error(GOT_ERR_NO_SPACE);
1695 buf = malloc(sizeof(iblob) + size);
1697 return got_error_from_errno("malloc");
1699 memcpy(buf, &iblob, sizeof(iblob));
1700 memcpy(buf + sizeof(iblob), data, size);
1701 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
1702 sizeof(iblob) + size) == -1) {
1704 return got_error_from_errno("imsg_compose BLOB");
1708 /* Data has already been written to file descriptor. */
1709 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
1710 sizeof(iblob)) == -1)
1711 return got_error_from_errno("imsg_compose BLOB");
1715 return flush_imsg(ibuf);
1718 const struct got_error *
1719 got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1720 struct imsgbuf *ibuf)
1722 const struct got_error *err = NULL;
1724 struct got_imsg_blob *iblob;
1729 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1733 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1735 switch (imsg.hdr.type) {
1737 if (datalen < sizeof(*iblob)) {
1738 err = got_error(GOT_ERR_PRIVSEP_LEN);
1742 *size = iblob->size;
1743 *hdrlen = iblob->hdrlen;
1745 if (datalen == sizeof(*iblob)) {
1746 /* Data has been written to file descriptor. */
1750 if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX ||
1751 *size > datalen + sizeof(*iblob)) {
1752 err = got_error(GOT_ERR_PRIVSEP_LEN);
1756 *outbuf = malloc(*size);
1757 if (*outbuf == NULL) {
1758 err = got_error_from_errno("malloc");
1761 memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
1764 err = got_error(GOT_ERR_PRIVSEP_MSG);
1773 static const struct got_error *
1774 send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
1776 const struct got_error *err = NULL;
1777 size_t offset, remain;
1780 remain = tagmsg_len;
1781 while (remain > 0) {
1782 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1784 if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
1785 tag->tagmsg + offset, n) == -1) {
1786 err = got_error_from_errno("imsg_compose TAG_TAGMSG");
1790 err = flush_imsg(ibuf);
1801 const struct got_error *
1802 got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
1804 const struct got_error *err = NULL;
1805 struct got_imsg_tag_object *itag;
1808 size_t tag_len = strlen(tag->tag);
1809 size_t tagger_len = strlen(tag->tagger);
1810 size_t tagmsg_len = strlen(tag->tagmsg);
1812 total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
1814 buf = malloc(total);
1816 return got_error_from_errno("malloc");
1818 itag = (struct got_imsg_tag_object *)buf;
1819 memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
1820 itag->obj_type = tag->obj_type;
1821 itag->tag_len = tag_len;
1822 itag->tagger_len = tagger_len;
1823 itag->tagger_time = tag->tagger_time;
1824 itag->tagger_gmtoff = tag->tagger_gmtoff;
1825 itag->tagmsg_len = tagmsg_len;
1827 len = sizeof(*itag);
1828 memcpy(buf + len, tag->tag, tag_len);
1830 memcpy(buf + len, tag->tagger, tagger_len);
1833 if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
1834 err = got_error_from_errno("imsg_compose TAG");
1838 if (tagmsg_len == 0 ||
1839 tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1840 err = flush_imsg(ibuf);
1844 err = send_tagmsg(ibuf, tag, tagmsg_len);
1850 const struct got_error *
1851 got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
1853 const struct got_error *err = NULL;
1855 struct got_imsg_tag_object *itag;
1856 size_t len, datalen;
1857 const size_t min_datalen =
1858 MIN(sizeof(struct got_imsg_error),
1859 sizeof(struct got_imsg_tag_object));
1863 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1867 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1870 switch (imsg.hdr.type) {
1872 if (datalen < sizeof(*itag)) {
1873 err = got_error(GOT_ERR_PRIVSEP_LEN);
1877 if (datalen != sizeof(*itag) + itag->tag_len +
1879 err = got_error(GOT_ERR_PRIVSEP_LEN);
1882 len += sizeof(*itag);
1884 *tag = calloc(1, sizeof(**tag));
1886 err = got_error_from_errno("calloc");
1890 memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
1892 (*tag)->tag = strndup(imsg.data + len, itag->tag_len);
1893 if ((*tag)->tag == NULL) {
1894 err = got_error_from_errno("strndup");
1897 len += itag->tag_len;
1899 (*tag)->obj_type = itag->obj_type;
1900 (*tag)->tagger_time = itag->tagger_time;
1901 (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1903 (*tag)->tagger = strndup(imsg.data + len, itag->tagger_len);
1904 if ((*tag)->tagger == NULL) {
1905 err = got_error_from_errno("strndup");
1908 len += itag->tagger_len;
1910 if (itag->tagmsg_len == 0) {
1911 (*tag)->tagmsg = strdup("");
1912 if ((*tag)->tagmsg == NULL) {
1913 err = got_error_from_errno("strdup");
1917 size_t offset = 0, remain = itag->tagmsg_len;
1919 (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1920 if ((*tag)->tagmsg == NULL) {
1921 err = got_error_from_errno("malloc");
1924 while (remain > 0) {
1925 struct imsg imsg_log;
1926 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1929 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1933 if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1934 return got_error(GOT_ERR_PRIVSEP_MSG);
1936 memcpy((*tag)->tagmsg + offset, imsg_log.data,
1938 imsg_free(&imsg_log);
1942 (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1947 err = got_error(GOT_ERR_PRIVSEP_MSG);
1956 const struct got_error *
1957 got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1958 struct got_packidx *packidx)
1960 const struct got_error *err = NULL;
1961 struct got_imsg_packidx ipackidx;
1962 struct got_imsg_pack ipack;
1965 ipackidx.len = packidx->len;
1966 ipackidx.packfile_size = pack->filesize;
1967 fd = dup(packidx->fd);
1969 return got_error_from_errno("dup");
1971 if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
1972 sizeof(ipackidx)) == -1) {
1973 err = got_error_from_errno("imsg_compose PACKIDX");
1978 if (strlcpy(ipack.path_packfile, pack->path_packfile,
1979 sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
1980 return got_error(GOT_ERR_NO_SPACE);
1981 ipack.filesize = pack->filesize;
1985 return got_error_from_errno("dup");
1987 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
1989 err = got_error_from_errno("imsg_compose PACK");
1994 return flush_imsg(ibuf);
1997 const struct got_error *
1998 got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
1999 struct got_object_id *id)
2001 struct got_imsg_packed_object iobj;
2004 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
2006 if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
2007 &iobj, sizeof(iobj)) == -1)
2008 return got_error_from_errno("imsg_compose "
2009 "PACKED_OBJECT_REQUEST");
2011 return flush_imsg(ibuf);
2014 const struct got_error *
2015 got_privsep_send_packed_raw_obj_req(struct imsgbuf *ibuf, int idx,
2016 struct got_object_id *id)
2018 struct got_imsg_packed_object iobj;
2021 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
2023 if (imsg_compose(ibuf, GOT_IMSG_PACKED_RAW_OBJECT_REQUEST, 0, 0, -1,
2024 &iobj, sizeof(iobj)) == -1)
2025 return got_error_from_errno("imsg_compose "
2026 "PACKED_OBJECT_REQUEST");
2028 return flush_imsg(ibuf);
2031 const struct got_error *
2032 got_privsep_send_gitconfig_parse_req(struct imsgbuf *ibuf, int fd)
2034 const struct got_error *err = NULL;
2036 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_PARSE_REQUEST, 0, 0, fd,
2038 err = got_error_from_errno("imsg_compose "
2039 "GITCONFIG_PARSE_REQUEST");
2044 return flush_imsg(ibuf);
2047 const struct got_error *
2048 got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *ibuf)
2050 if (imsg_compose(ibuf,
2051 GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST, 0, 0, -1,
2053 return got_error_from_errno("imsg_compose "
2054 "GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST");
2056 return flush_imsg(ibuf);
2059 const struct got_error *
2060 got_privsep_send_gitconfig_repository_extensions_req(struct imsgbuf *ibuf)
2062 if (imsg_compose(ibuf,
2063 GOT_IMSG_GITCONFIG_REPOSITORY_EXTENSIONS_REQUEST, 0, 0, -1,
2065 return got_error_from_errno("imsg_compose "
2066 "GITCONFIG_REPOSITORY_EXTENSIONS_REQUEST");
2068 return flush_imsg(ibuf);
2072 const struct got_error *
2073 got_privsep_send_gitconfig_author_name_req(struct imsgbuf *ibuf)
2075 if (imsg_compose(ibuf,
2076 GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST, 0, 0, -1, NULL, 0) == -1)
2077 return got_error_from_errno("imsg_compose "
2078 "GITCONFIG_AUTHOR_NAME_REQUEST");
2080 return flush_imsg(ibuf);
2083 const struct got_error *
2084 got_privsep_send_gitconfig_author_email_req(struct imsgbuf *ibuf)
2086 if (imsg_compose(ibuf,
2087 GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST, 0, 0, -1, NULL, 0) == -1)
2088 return got_error_from_errno("imsg_compose "
2089 "GITCONFIG_AUTHOR_EMAIL_REQUEST");
2091 return flush_imsg(ibuf);
2094 const struct got_error *
2095 got_privsep_send_gitconfig_remotes_req(struct imsgbuf *ibuf)
2097 if (imsg_compose(ibuf,
2098 GOT_IMSG_GITCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
2099 return got_error_from_errno("imsg_compose "
2100 "GITCONFIG_REMOTE_REQUEST");
2102 return flush_imsg(ibuf);
2105 const struct got_error *
2106 got_privsep_send_gitconfig_owner_req(struct imsgbuf *ibuf)
2108 if (imsg_compose(ibuf,
2109 GOT_IMSG_GITCONFIG_OWNER_REQUEST, 0, 0, -1, NULL, 0) == -1)
2110 return got_error_from_errno("imsg_compose "
2111 "GITCONFIG_OWNER_REQUEST");
2113 return flush_imsg(ibuf);
2116 const struct got_error *
2117 got_privsep_recv_gitconfig_str(char **str, struct imsgbuf *ibuf)
2119 const struct got_error *err = NULL;
2125 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2128 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2130 switch (imsg.hdr.type) {
2131 case GOT_IMSG_GITCONFIG_STR_VAL:
2134 /* datalen does not include terminating \0 */
2135 *str = malloc(datalen + 1);
2137 err = got_error_from_errno("malloc");
2140 memcpy(*str, imsg.data, datalen);
2141 (*str)[datalen] = '\0';
2144 err = got_error(GOT_ERR_PRIVSEP_MSG);
2152 const struct got_error *
2153 got_privsep_recv_gitconfig_int(int *val, struct imsgbuf *ibuf)
2155 const struct got_error *err = NULL;
2158 const size_t min_datalen =
2159 MIN(sizeof(struct got_imsg_error), sizeof(int));
2163 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2166 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2168 switch (imsg.hdr.type) {
2169 case GOT_IMSG_GITCONFIG_INT_VAL:
2170 if (datalen != sizeof(*val)) {
2171 err = got_error(GOT_ERR_PRIVSEP_LEN);
2174 memcpy(val, imsg.data, sizeof(*val));
2177 err = got_error(GOT_ERR_PRIVSEP_MSG);
2186 free_remote_data(struct got_remote_repo *remote)
2191 free(remote->fetch_url);
2192 free(remote->send_url);
2193 for (i = 0; i < remote->nfetch_branches; i++)
2194 free(remote->fetch_branches[i]);
2195 free(remote->fetch_branches);
2196 for (i = 0; i < remote->nsend_branches; i++)
2197 free(remote->send_branches[i]);
2198 free(remote->send_branches);
2199 for (i = 0; i < remote->nfetch_refs; i++)
2200 free(remote->fetch_refs[i]);
2201 free(remote->fetch_refs);
2204 const struct got_error *
2205 got_privsep_recv_gitconfig_remotes(struct got_remote_repo **remotes,
2206 int *nremotes, struct imsgbuf *ibuf)
2208 const struct got_error *err = NULL;
2211 struct got_imsg_remotes iremotes;
2212 struct got_imsg_remote iremote;
2216 iremotes.nremotes = 0;
2218 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremotes));
2221 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2223 switch (imsg.hdr.type) {
2224 case GOT_IMSG_GITCONFIG_REMOTES:
2225 if (datalen != sizeof(iremotes)) {
2226 err = got_error(GOT_ERR_PRIVSEP_LEN);
2229 memcpy(&iremotes, imsg.data, sizeof(iremotes));
2230 if (iremotes.nremotes == 0) {
2237 return got_error(GOT_ERR_PRIVSEP_MSG);
2242 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(**remotes));
2243 if (*remotes == NULL)
2244 return got_error_from_errno("recallocarray");
2246 while (*nremotes < iremotes.nremotes) {
2247 struct got_remote_repo *remote;
2249 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremote));
2252 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2254 switch (imsg.hdr.type) {
2255 case GOT_IMSG_GITCONFIG_REMOTE:
2256 remote = &(*remotes)[*nremotes];
2257 memset(remote, 0, sizeof(*remote));
2258 if (datalen < sizeof(iremote)) {
2259 err = got_error(GOT_ERR_PRIVSEP_LEN);
2262 memcpy(&iremote, imsg.data, sizeof(iremote));
2263 if (iremote.name_len == 0 ||
2264 iremote.fetch_url_len == 0 ||
2265 iremote.send_url_len == 0 ||
2266 (sizeof(iremote) + iremote.name_len +
2267 iremote.fetch_url_len + iremote.send_url_len) > datalen) {
2268 err = got_error(GOT_ERR_PRIVSEP_LEN);
2271 remote->name = strndup(imsg.data + sizeof(iremote),
2273 if (remote->name == NULL) {
2274 err = got_error_from_errno("strndup");
2277 remote->fetch_url = strndup(imsg.data + sizeof(iremote) +
2278 iremote.name_len, iremote.fetch_url_len);
2279 if (remote->fetch_url == NULL) {
2280 err = got_error_from_errno("strndup");
2281 free_remote_data(remote);
2284 remote->send_url = strndup(imsg.data + sizeof(iremote) +
2285 iremote.name_len + iremote.fetch_url_len,
2286 iremote.send_url_len);
2287 if (remote->send_url == NULL) {
2288 err = got_error_from_errno("strndup");
2289 free_remote_data(remote);
2292 remote->mirror_references = iremote.mirror_references;
2293 remote->fetch_all_branches = iremote.fetch_all_branches;
2294 remote->nfetch_branches = 0;
2295 remote->fetch_branches = NULL;
2296 remote->nsend_branches = 0;
2297 remote->send_branches = NULL;
2298 remote->nfetch_refs = 0;
2299 remote->fetch_refs = NULL;
2303 err = got_error(GOT_ERR_PRIVSEP_MSG);
2314 for (i = 0; i < *nremotes; i++)
2315 free_remote_data(&(*remotes)[i]);
2323 const struct got_error *
2324 got_privsep_send_gotconfig_parse_req(struct imsgbuf *ibuf, int fd)
2326 const struct got_error *err = NULL;
2328 if (imsg_compose(ibuf, GOT_IMSG_GOTCONFIG_PARSE_REQUEST, 0, 0, fd,
2330 err = got_error_from_errno("imsg_compose "
2331 "GOTCONFIG_PARSE_REQUEST");
2336 return flush_imsg(ibuf);
2339 const struct got_error *
2340 got_privsep_send_gotconfig_author_req(struct imsgbuf *ibuf)
2342 if (imsg_compose(ibuf,
2343 GOT_IMSG_GOTCONFIG_AUTHOR_REQUEST, 0, 0, -1, NULL, 0) == -1)
2344 return got_error_from_errno("imsg_compose "
2345 "GOTCONFIG_AUTHOR_REQUEST");
2347 return flush_imsg(ibuf);
2350 const struct got_error *
2351 got_privsep_send_gotconfig_remotes_req(struct imsgbuf *ibuf)
2353 if (imsg_compose(ibuf,
2354 GOT_IMSG_GOTCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
2355 return got_error_from_errno("imsg_compose "
2356 "GOTCONFIG_REMOTE_REQUEST");
2358 return flush_imsg(ibuf);
2361 const struct got_error *
2362 got_privsep_recv_gotconfig_str(char **str, struct imsgbuf *ibuf)
2364 const struct got_error *err = NULL;
2370 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2373 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2375 switch (imsg.hdr.type) {
2376 case GOT_IMSG_ERROR:
2377 err = recv_imsg_error(&imsg, datalen);
2379 case GOT_IMSG_GOTCONFIG_STR_VAL:
2382 /* datalen does not include terminating \0 */
2383 *str = malloc(datalen + 1);
2385 err = got_error_from_errno("malloc");
2388 memcpy(*str, imsg.data, datalen);
2389 (*str)[datalen] = '\0';
2392 err = got_error(GOT_ERR_PRIVSEP_MSG);
2400 const struct got_error *
2401 got_privsep_recv_gotconfig_remotes(struct got_remote_repo **remotes,
2402 int *nremotes, struct imsgbuf *ibuf)
2404 const struct got_error *err = NULL;
2407 struct got_imsg_remotes iremotes;
2408 struct got_imsg_remote iremote;
2409 const size_t min_datalen =
2410 MIN(sizeof(struct got_imsg_error), sizeof(iremotes));
2414 iremotes.nremotes = 0;
2416 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2419 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2421 switch (imsg.hdr.type) {
2422 case GOT_IMSG_ERROR:
2423 err = recv_imsg_error(&imsg, datalen);
2425 case GOT_IMSG_GOTCONFIG_REMOTES:
2426 if (datalen != sizeof(iremotes)) {
2427 err = got_error(GOT_ERR_PRIVSEP_LEN);
2430 memcpy(&iremotes, imsg.data, sizeof(iremotes));
2431 if (iremotes.nremotes < 0) {
2432 err = got_error(GOT_ERR_PRIVSEP_LEN);
2435 if (iremotes.nremotes == 0) {
2442 return got_error(GOT_ERR_PRIVSEP_MSG);
2447 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(**remotes));
2448 if (*remotes == NULL)
2449 return got_error_from_errno("recallocarray");
2451 while (*nremotes < iremotes.nremotes) {
2452 struct got_remote_repo *remote;
2453 const size_t min_datalen =
2454 MIN(sizeof(struct got_imsg_error), sizeof(iremote));
2457 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2460 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2462 switch (imsg.hdr.type) {
2463 case GOT_IMSG_ERROR:
2464 err = recv_imsg_error(&imsg, datalen);
2466 case GOT_IMSG_GOTCONFIG_REMOTE:
2467 remote = &(*remotes)[*nremotes];
2468 memset(remote, 0, sizeof(*remote));
2469 if (datalen < sizeof(iremote)) {
2470 err = got_error(GOT_ERR_PRIVSEP_LEN);
2473 memcpy(&iremote, imsg.data, sizeof(iremote));
2474 if (iremote.name_len == 0 ||
2475 (iremote.fetch_url_len == 0 &&
2476 iremote.send_url_len == 0) ||
2477 (sizeof(iremote) + iremote.name_len +
2478 iremote.fetch_url_len + iremote.send_url_len) >
2480 err = got_error(GOT_ERR_PRIVSEP_LEN);
2483 remote->name = strndup(imsg.data + sizeof(iremote),
2485 if (remote->name == NULL) {
2486 err = got_error_from_errno("strndup");
2489 remote->fetch_url = strndup(imsg.data +
2490 sizeof(iremote) + iremote.name_len,
2491 iremote.fetch_url_len);
2492 if (remote->fetch_url == NULL) {
2493 err = got_error_from_errno("strndup");
2494 free_remote_data(remote);
2497 remote->send_url = strndup(imsg.data +
2498 sizeof(iremote) + iremote.name_len +
2499 iremote.fetch_url_len, iremote.send_url_len);
2500 if (remote->send_url == NULL) {
2501 err = got_error_from_errno("strndup");
2502 free_remote_data(remote);
2505 remote->mirror_references = iremote.mirror_references;
2506 remote->fetch_all_branches = iremote.fetch_all_branches;
2507 if (iremote.nfetch_branches > 0) {
2508 remote->fetch_branches = recallocarray(NULL, 0,
2509 iremote.nfetch_branches, sizeof(char *));
2510 if (remote->fetch_branches == NULL) {
2511 err = got_error_from_errno("calloc");
2512 free_remote_data(remote);
2516 remote->nfetch_branches = 0;
2517 for (i = 0; i < iremote.nfetch_branches; i++) {
2519 err = got_privsep_recv_gotconfig_str(&branch,
2522 free_remote_data(remote);
2525 remote->fetch_branches[i] = branch;
2526 remote->nfetch_branches++;
2528 if (iremote.nsend_branches > 0) {
2529 remote->send_branches = recallocarray(NULL, 0,
2530 iremote.nsend_branches, sizeof(char *));
2531 if (remote->send_branches == NULL) {
2532 err = got_error_from_errno("calloc");
2533 free_remote_data(remote);
2537 remote->nsend_branches = 0;
2538 for (i = 0; i < iremote.nsend_branches; i++) {
2540 err = got_privsep_recv_gotconfig_str(&branch,
2543 free_remote_data(remote);
2546 remote->send_branches[i] = branch;
2547 remote->nsend_branches++;
2549 if (iremote.nfetch_refs > 0) {
2550 remote->fetch_refs = recallocarray(NULL, 0,
2551 iremote.nfetch_refs, sizeof(char *));
2552 if (remote->fetch_refs == NULL) {
2553 err = got_error_from_errno("calloc");
2554 free_remote_data(remote);
2558 remote->nfetch_refs = 0;
2559 for (i = 0; i < iremote.nfetch_refs; i++) {
2561 err = got_privsep_recv_gotconfig_str(&ref,
2564 free_remote_data(remote);
2567 remote->fetch_refs[i] = ref;
2568 remote->nfetch_refs++;
2573 err = got_error(GOT_ERR_PRIVSEP_MSG);
2584 for (i = 0; i < *nremotes; i++)
2585 free_remote_data(&(*remotes)[i]);
2593 const struct got_error *
2594 got_privsep_send_commit_traversal_request(struct imsgbuf *ibuf,
2595 struct got_object_id *id, int idx, const char *path)
2598 size_t path_len = strlen(path) + 1;
2600 wbuf = imsg_create(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_REQUEST, 0, 0,
2601 sizeof(struct got_imsg_commit_traversal_request) + path_len);
2603 return got_error_from_errno(
2604 "imsg_create COMMIT_TRAVERSAL_REQUEST");
2605 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1)
2606 return got_error_from_errno("imsg_add "
2607 "COMMIT_TRAVERSAL_REQUEST");
2608 if (imsg_add(wbuf, &idx, sizeof(idx)) == -1)
2609 return got_error_from_errno("imsg_add "
2610 "COMMIT_TRAVERSAL_REQUEST");
2611 if (imsg_add(wbuf, path, path_len) == -1)
2612 return got_error_from_errno("imsg_add "
2613 "COMMIT_TRAVERSAL_REQUEST");
2616 imsg_close(ibuf, wbuf);
2618 return flush_imsg(ibuf);
2621 const struct got_error *
2622 got_privsep_recv_traversed_commits(struct got_commit_object **changed_commit,
2623 struct got_object_id **changed_commit_id,
2624 struct got_object_id_queue *commit_ids, struct imsgbuf *ibuf)
2626 const struct got_error *err = NULL;
2628 struct got_imsg_traversed_commits *icommits;
2632 *changed_commit = NULL;
2633 *changed_commit_id = NULL;
2636 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2640 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2641 switch (imsg.hdr.type) {
2642 case GOT_IMSG_TRAVERSED_COMMITS:
2643 icommits = imsg.data;
2644 if (datalen != sizeof(*icommits) +
2645 icommits->ncommits * SHA1_DIGEST_LENGTH) {
2646 err = got_error(GOT_ERR_PRIVSEP_LEN);
2649 for (i = 0; i < icommits->ncommits; i++) {
2650 struct got_object_qid *qid;
2651 uint8_t *sha1 = (uint8_t *)imsg.data +
2652 sizeof(*icommits) + i * SHA1_DIGEST_LENGTH;
2653 err = got_object_qid_alloc_partial(&qid);
2656 memcpy(qid->id.sha1, sha1, SHA1_DIGEST_LENGTH);
2657 STAILQ_INSERT_TAIL(commit_ids, qid, entry);
2659 /* The last commit may contain a change. */
2660 if (i == icommits->ncommits - 1) {
2661 *changed_commit_id =
2662 got_object_id_dup(&qid->id);
2663 if (*changed_commit_id == NULL) {
2664 err = got_error_from_errno(
2665 "got_object_id_dup");
2671 case GOT_IMSG_COMMIT:
2672 if (*changed_commit_id == NULL) {
2673 err = got_error(GOT_ERR_PRIVSEP_MSG);
2676 err = get_commit_from_imsg(changed_commit, &imsg,
2679 case GOT_IMSG_COMMIT_TRAVERSAL_DONE:
2683 err = got_error(GOT_ERR_PRIVSEP_MSG);
2693 got_object_id_queue_free(commit_ids);
2697 const struct got_error *
2698 got_privsep_send_enumerated_tree(size_t *totlen, struct imsgbuf *ibuf,
2699 struct got_object_id *tree_id, const char *path,
2700 struct got_parsed_tree_entry *entries, int nentries)
2702 const struct got_error *err = NULL;
2704 size_t path_len = strlen(path);
2707 msglen = sizeof(struct got_imsg_enumerated_tree) + path_len;
2708 wbuf = imsg_create(ibuf, GOT_IMSG_ENUMERATED_TREE, 0, 0, msglen);
2710 return got_error_from_errno("imsg_create ENUMERATED_TREE");
2712 if (imsg_add(wbuf, tree_id->sha1, SHA1_DIGEST_LENGTH) == -1)
2713 return got_error_from_errno("imsg_add ENUMERATED_TREE");
2714 if (imsg_add(wbuf, &nentries, sizeof(nentries)) == -1)
2715 return got_error_from_errno("imsg_add ENUMERATED_TREE");
2716 if (imsg_add(wbuf, path, path_len) == -1)
2717 return got_error_from_errno("imsg_add ENUMERATED_TREE");
2720 imsg_close(ibuf, wbuf);
2723 err = send_tree_entries(ibuf, entries, nentries);
2728 return flush_imsg(ibuf);
2731 const struct got_error *
2732 got_privsep_send_object_enumeration_request(struct imsgbuf *ibuf)
2734 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_ENUMERATION_REQUEST,
2735 0, 0, -1, NULL, 0) == -1)
2736 return got_error_from_errno("imsg_compose "
2737 "OBJECT_ENUMERATION_REQUEST");
2739 return flush_imsg(ibuf);
2742 const struct got_error *
2743 got_privsep_send_object_enumeration_done(struct imsgbuf *ibuf)
2745 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_ENUMERATION_DONE,
2746 0, 0, -1, NULL, 0) == -1)
2747 return got_error_from_errno("imsg_compose "
2748 "OBJECT_ENUMERATION_DONE");
2750 return flush_imsg(ibuf);
2753 const struct got_error *
2754 got_privsep_send_object_enumeration_incomplete(struct imsgbuf *ibuf)
2756 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_ENUMERATION_INCOMPLETE,
2757 0, 0, -1, NULL, 0) == -1)
2758 return got_error_from_errno("imsg_compose "
2759 "OBJECT_ENUMERATION_INCOMPLETE");
2761 return flush_imsg(ibuf);
2764 const struct got_error *
2765 got_privsep_send_enumerated_commit(struct imsgbuf *ibuf,
2766 struct got_object_id *id, time_t mtime)
2770 wbuf = imsg_create(ibuf, GOT_IMSG_ENUMERATED_COMMIT, 0, 0,
2771 sizeof(struct got_imsg_enumerated_commit) + SHA1_DIGEST_LENGTH);
2773 return got_error_from_errno("imsg_create ENUMERATED_COMMIT");
2775 /* Keep in sync with struct got_imsg_enumerated_commit! */
2776 if (imsg_add(wbuf, id, SHA1_DIGEST_LENGTH) == -1)
2777 return got_error_from_errno("imsg_add ENUMERATED_COMMIT");
2778 if (imsg_add(wbuf, &mtime, sizeof(mtime)) == -1)
2779 return got_error_from_errno("imsg_add ENUMERATED_COMMIT");
2782 imsg_close(ibuf, wbuf);
2783 /* Don't flush yet, tree entries or ENUMERATION_DONE will follow. */
2787 const struct got_error *
2788 got_privsep_recv_enumerated_objects(int *found_all_objects,
2789 struct imsgbuf *ibuf,
2790 got_object_enumerate_commit_cb cb_commit,
2791 got_object_enumerate_tree_cb cb_tree, void *cb_arg,
2792 struct got_repository *repo)
2794 const struct got_error *err = NULL;
2796 struct got_imsg_enumerated_commit *icommit = NULL;
2797 struct got_object_id commit_id;
2798 int have_commit = 0;
2800 struct got_tree_object tree;
2801 struct got_imsg_enumerated_tree *itree;
2802 struct got_object_id tree_id;
2803 char *path = NULL, *canon_path = NULL;
2804 size_t datalen, path_len;
2808 *found_all_objects = 0;
2809 memset(&tree, 0, sizeof(tree));
2812 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2816 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2817 switch (imsg.hdr.type) {
2818 case GOT_IMSG_ENUMERATED_COMMIT:
2819 if (have_commit && nentries != -1) {
2820 err = got_error(GOT_ERR_PRIVSEP_MSG);
2823 if (datalen != sizeof(*icommit)) {
2824 err = got_error(GOT_ERR_PRIVSEP_LEN);
2827 icommit = (struct got_imsg_enumerated_commit *)imsg.data;
2828 memcpy(commit_id.sha1, icommit->id, SHA1_DIGEST_LENGTH);
2829 mtime = icommit->mtime;
2832 case GOT_IMSG_ENUMERATED_TREE:
2833 /* Should be preceeded by GOT_IMSG_ENUMERATED_COMMIT. */
2835 err = got_error(GOT_ERR_PRIVSEP_MSG);
2838 if (datalen < sizeof(*itree)) {
2839 err = got_error(GOT_ERR_PRIVSEP_LEN);
2843 path_len = datalen - sizeof(*itree);
2844 if (path_len == 0) {
2845 err = got_error(GOT_ERR_PRIVSEP_LEN);
2848 memcpy(tree_id.sha1, itree->id, sizeof(tree_id.sha1));
2850 path = malloc(path_len + 1);
2852 err = got_error_from_errno("malloc");
2856 canon_path = malloc(path_len + 1);
2857 if (canon_path == NULL) {
2858 err = got_error_from_errno("malloc");
2861 memcpy(path, (uint8_t *)imsg.data + sizeof(*itree),
2863 path[path_len] = '\0';
2864 if (!got_path_is_absolute(path)) {
2865 err = got_error(GOT_ERR_BAD_PATH);
2868 if (got_path_is_root_dir(path)) {
2869 /* XXX check what got_canonpath() does wrong */
2870 canon_path[0] = '/';
2871 canon_path[1] = '\0';
2873 err = got_canonpath(path, canon_path,
2878 if (strcmp(path, canon_path) != 0) {
2879 err = got_error(GOT_ERR_BAD_PATH);
2882 if (nentries != -1) {
2883 err = got_error(GOT_ERR_PRIVSEP_MSG);
2886 if (itree->nentries < -1) {
2887 err = got_error(GOT_ERR_PRIVSEP_MSG);
2890 if (itree->nentries == -1) {
2891 /* Tree was not found in pack file. */
2893 err = cb_tree(cb_arg, NULL, mtime, &tree_id,
2897 if (itree->nentries > INT_MAX) {
2898 err = got_error(GOT_ERR_PRIVSEP_LEN);
2901 tree.entries = calloc(itree->nentries,
2902 sizeof(struct got_tree_entry));
2903 if (tree.entries == NULL) {
2904 err = got_error_from_errno("calloc");
2907 if (itree->nentries == 0) {
2908 err = cb_tree(cb_arg, &tree, mtime, &tree_id,
2913 /* Prepare for next tree. */
2915 memset(&tree, 0, sizeof(tree));
2918 tree.nentries = itree->nentries;
2922 case GOT_IMSG_TREE_ENTRIES:
2923 /* Should be preceeded by GOT_IMSG_ENUMERATED_TREE. */
2924 if (nentries <= -1) {
2925 err = got_error(GOT_ERR_PRIVSEP_MSG);
2928 err = recv_tree_entries(imsg.data, datalen,
2932 if (tree.nentries == nentries) {
2933 err = cb_tree(cb_arg, &tree, mtime, &tree_id,
2938 /* Prepare for next tree. */
2940 memset(&tree, 0, sizeof(tree));
2944 case GOT_IMSG_TREE_ENUMERATION_DONE:
2945 /* All trees have been found and traversed. */
2946 if (!have_commit || path == NULL || nentries != -1) {
2947 err = got_error(GOT_ERR_PRIVSEP_MSG);
2950 err = cb_commit(cb_arg, mtime, &commit_id, repo);
2955 case GOT_IMSG_OBJECT_ENUMERATION_DONE:
2956 *found_all_objects = 1;
2959 case GOT_IMSG_OBJECT_ENUMERATION_INCOMPLETE:
2963 err = got_error(GOT_ERR_PRIVSEP_MSG);
2978 const struct got_error *
2979 got_privsep_send_raw_delta_req(struct imsgbuf *ibuf, int idx,
2980 struct got_object_id *id)
2982 struct got_imsg_raw_delta_request dreq;
2985 memcpy(dreq.id, id->sha1, SHA1_DIGEST_LENGTH);
2987 if (imsg_compose(ibuf, GOT_IMSG_RAW_DELTA_REQUEST, 0, 0, -1,
2988 &dreq, sizeof(dreq)) == -1)
2989 return got_error_from_errno("imsg_compose RAW_DELTA_REQUEST");
2991 return flush_imsg(ibuf);
2994 const struct got_error *
2995 got_privsep_send_raw_delta_outfd(struct imsgbuf *ibuf, int fd)
2997 return send_fd(ibuf, GOT_IMSG_RAW_DELTA_OUTFD, fd);
3000 const struct got_error *
3001 got_privsep_send_raw_delta(struct imsgbuf *ibuf, uint64_t base_size,
3002 uint64_t result_size, off_t delta_size, off_t delta_compressed_size,
3003 off_t delta_offset, off_t delta_out_offset, struct got_object_id *base_id)
3005 struct got_imsg_raw_delta idelta;
3008 idelta.base_size = base_size;
3009 idelta.result_size = result_size;
3010 idelta.delta_size = delta_size;
3011 idelta.delta_compressed_size = delta_compressed_size;
3012 idelta.delta_offset = delta_offset;
3013 idelta.delta_out_offset = delta_out_offset;
3014 memcpy(idelta.base_id, base_id->sha1, SHA1_DIGEST_LENGTH);
3016 ret = imsg_compose(ibuf, GOT_IMSG_RAW_DELTA, 0, 0, -1,
3017 &idelta, sizeof(idelta));
3019 return got_error_from_errno("imsg_compose RAW_DELTA");
3021 return flush_imsg(ibuf);
3024 const struct got_error *
3025 got_privsep_recv_raw_delta(uint64_t *base_size, uint64_t *result_size,
3026 off_t *delta_size, off_t *delta_compressed_size, off_t *delta_offset,
3027 off_t *delta_out_offset, struct got_object_id **base_id, struct imsgbuf *ibuf)
3029 const struct got_error *err = NULL;
3031 struct got_imsg_raw_delta *delta;
3037 *delta_compressed_size = 0;
3039 *delta_out_offset = 0;
3042 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
3046 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
3048 switch (imsg.hdr.type) {
3049 case GOT_IMSG_RAW_DELTA:
3050 if (datalen != sizeof(*delta)) {
3051 err = got_error(GOT_ERR_PRIVSEP_LEN);
3055 *base_size = delta->base_size;
3056 *result_size = delta->result_size;
3057 *delta_size = delta->delta_size;
3058 *delta_compressed_size = delta->delta_compressed_size;
3059 *delta_offset = delta->delta_offset;
3060 *delta_out_offset = delta->delta_out_offset;
3061 *base_id = calloc(1, sizeof(**base_id));
3062 if (*base_id == NULL) {
3063 err = got_error_from_errno("malloc");
3066 memcpy((*base_id)->sha1, delta->base_id, SHA1_DIGEST_LENGTH);
3069 err = got_error(GOT_ERR_PRIVSEP_MSG);
3082 static const struct got_error *
3083 send_idlist(struct imsgbuf *ibuf, struct got_object_id **ids, size_t nids)
3085 const struct got_error *err = NULL;
3086 struct got_imsg_object_idlist idlist;
3090 if (nids > GOT_IMSG_OBJ_ID_LIST_MAX_NIDS)
3091 return got_error(GOT_ERR_NO_SPACE);
3093 wbuf = imsg_create(ibuf, GOT_IMSG_OBJ_ID_LIST, 0, 0,
3094 sizeof(idlist) + nids * sizeof(**ids));
3096 err = got_error_from_errno("imsg_create OBJ_ID_LIST");
3101 if (imsg_add(wbuf, &idlist, sizeof(idlist)) == -1)
3102 return got_error_from_errno("imsg_add OBJ_ID_LIST");
3104 for (i = 0; i < nids; i++) {
3105 struct got_object_id *id = ids[i];
3106 if (imsg_add(wbuf, id, sizeof(*id)) == -1)
3107 return got_error_from_errno("imsg_add OBJ_ID_LIST");
3111 imsg_close(ibuf, wbuf);
3113 return flush_imsg(ibuf);
3116 const struct got_error *
3117 got_privsep_send_object_idlist(struct imsgbuf *ibuf,
3118 struct got_object_id **ids, size_t nids)
3120 const struct got_error *err = NULL;
3121 struct got_object_id *idlist[GOT_IMSG_OBJ_ID_LIST_MAX_NIDS];
3124 for (i = 0; i < nids; i++) {
3125 idlist[i % nitems(idlist)] = ids[i];
3127 if (queued >= nitems(idlist)) {
3128 err = send_idlist(ibuf, idlist, queued);
3136 err = send_idlist(ibuf, idlist, queued);
3144 const struct got_error *
3145 got_privsep_send_object_idlist_done(struct imsgbuf *ibuf)
3147 if (imsg_compose(ibuf, GOT_IMSG_OBJ_ID_LIST_DONE, 0, 0, -1, NULL, 0)
3149 return got_error_from_errno("imsg_compose OBJ_ID_LIST_DONE");
3151 return flush_imsg(ibuf);
3154 const struct got_error *
3155 got_privsep_recv_object_idlist(int *done, struct got_object_id **ids,
3156 size_t *nids, struct imsgbuf *ibuf)
3158 const struct got_error *err = NULL;
3160 struct got_imsg_object_idlist *idlist;
3167 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
3171 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
3172 switch (imsg.hdr.type) {
3173 case GOT_IMSG_OBJ_ID_LIST:
3174 if (datalen < sizeof(*idlist)) {
3175 err = got_error(GOT_ERR_PRIVSEP_LEN);
3179 if (idlist->nids > GOT_IMSG_OBJ_ID_LIST_MAX_NIDS ||
3180 idlist->nids * sizeof(**ids) > datalen - sizeof(*idlist)) {
3181 err = got_error(GOT_ERR_PRIVSEP_LEN);
3184 *nids = idlist->nids;
3185 *ids = calloc(*nids, sizeof(**ids));
3187 err = got_error_from_errno("calloc");
3190 memcpy(*ids, (uint8_t *)imsg.data + sizeof(*idlist),
3191 *nids * sizeof(**ids));
3193 case GOT_IMSG_OBJ_ID_LIST_DONE:
3197 err = got_error(GOT_ERR_PRIVSEP_MSG);
3206 const struct got_error *
3207 got_privsep_send_delta_reuse_req(struct imsgbuf *ibuf)
3209 if (imsg_compose(ibuf, GOT_IMSG_DELTA_REUSE_REQUEST, 0, 0, -1, NULL, 0)
3211 return got_error_from_errno("imsg_compose DELTA_REUSE_REQUEST");
3213 return flush_imsg(ibuf);
3216 const struct got_error *
3217 got_privsep_send_reused_deltas(struct imsgbuf *ibuf,
3218 struct got_imsg_reused_delta *deltas, size_t ndeltas)
3220 const struct got_error *err = NULL;
3222 struct got_imsg_reused_deltas ideltas;
3225 if (ndeltas > GOT_IMSG_REUSED_DELTAS_MAX_NDELTAS)
3226 return got_error(GOT_ERR_NO_SPACE);
3228 wbuf = imsg_create(ibuf, GOT_IMSG_REUSED_DELTAS, 0, 0,
3229 sizeof(ideltas) + ndeltas * sizeof(*deltas));
3231 err = got_error_from_errno("imsg_create REUSED_DELTAS");
3235 ideltas.ndeltas = ndeltas;
3236 if (imsg_add(wbuf, &ideltas, sizeof(ideltas)) == -1)
3237 return got_error_from_errno("imsg_add REUSED_DELTAS");
3239 for (i = 0; i < ndeltas; i++) {
3240 struct got_imsg_reused_delta *delta = &deltas[i];
3241 if (imsg_add(wbuf, delta, sizeof(*delta)) == -1)
3242 return got_error_from_errno("imsg_add REUSED_DELTAS");
3246 imsg_close(ibuf, wbuf);
3248 return flush_imsg(ibuf);
3251 const struct got_error *
3252 got_privsep_send_reused_deltas_done(struct imsgbuf *ibuf)
3254 if (imsg_compose(ibuf, GOT_IMSG_DELTA_REUSE_DONE, 0, 0, -1, NULL, 0)
3256 return got_error_from_errno("imsg_compose DELTA_REUSE_DONE");
3258 return flush_imsg(ibuf);
3261 const struct got_error *
3262 got_privsep_recv_reused_deltas(int *done, struct got_imsg_reused_delta *deltas,
3263 size_t *ndeltas, struct imsgbuf *ibuf)
3265 const struct got_error *err = NULL;
3267 struct got_imsg_reused_deltas *ideltas;
3273 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
3277 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
3278 switch (imsg.hdr.type) {
3279 case GOT_IMSG_REUSED_DELTAS:
3280 if (datalen < sizeof(*ideltas)) {
3281 err = got_error(GOT_ERR_PRIVSEP_LEN);
3284 ideltas = imsg.data;
3285 if (ideltas->ndeltas > GOT_IMSG_OBJ_ID_LIST_MAX_NIDS ||
3286 ideltas->ndeltas * sizeof(*deltas) >
3287 datalen - sizeof(*ideltas)) {
3288 err = got_error(GOT_ERR_PRIVSEP_LEN);
3291 *ndeltas = ideltas->ndeltas;
3292 memcpy(deltas, (uint8_t *)imsg.data + sizeof(*ideltas),
3293 *ndeltas * sizeof(*deltas));
3295 case GOT_IMSG_DELTA_REUSE_DONE:
3299 err = got_error(GOT_ERR_PRIVSEP_MSG);
3308 const struct got_error *
3309 got_privsep_unveil_exec_helpers(void)
3311 const char *helpers[] = {
3312 GOT_PATH_PROG_READ_PACK,
3313 GOT_PATH_PROG_READ_OBJECT,
3314 GOT_PATH_PROG_READ_COMMIT,
3315 GOT_PATH_PROG_READ_TREE,
3316 GOT_PATH_PROG_READ_BLOB,
3317 GOT_PATH_PROG_READ_TAG,
3318 GOT_PATH_PROG_READ_GITCONFIG,
3319 GOT_PATH_PROG_READ_GOTCONFIG,
3320 GOT_PATH_PROG_READ_PATCH,
3321 GOT_PATH_PROG_FETCH_PACK,
3322 GOT_PATH_PROG_INDEX_PACK,
3323 GOT_PATH_PROG_SEND_PACK,
3327 for (i = 0; i < nitems(helpers); i++) {
3328 if (unveil(helpers[i], "x") == 0)
3330 return got_error_from_errno2("unveil", helpers[i]);
3337 got_privsep_exec_child(int imsg_fds[2], const char *path, const char *repo_path)
3339 if (close(imsg_fds[0]) == -1) {
3340 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
3344 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
3345 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
3349 closefrom(GOT_IMSG_FD_CHILD + 1);
3351 if (execl(path, path, repo_path, (char *)NULL) == -1) {
3352 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,