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 if (datalen < sizeof(struct got_imsg_error)) {
706 err = got_error(GOT_ERR_PRIVSEP_LEN);
709 err = recv_imsg_error(&imsg, datalen);
711 case GOT_IMSG_FETCH_SYMREFS:
712 if (datalen < sizeof(*isymrefs)) {
713 err = got_error(GOT_ERR_PRIVSEP_LEN);
716 if (isymrefs != NULL) {
717 err = got_error(GOT_ERR_PRIVSEP_MSG);
720 isymrefs = (struct got_imsg_fetch_symrefs *)imsg.data;
721 off = sizeof(*isymrefs);
722 remain = datalen - off;
723 for (n = 0; n < isymrefs->nsymrefs; n++) {
724 struct got_imsg_fetch_symref *s;
726 if (remain < sizeof(struct got_imsg_fetch_symref)) {
727 err = got_error(GOT_ERR_PRIVSEP_LEN);
730 s = (struct got_imsg_fetch_symref *)(imsg.data + off);
732 remain -= sizeof(*s);
733 if (remain < s->name_len) {
734 err = got_error(GOT_ERR_PRIVSEP_LEN);
737 name = strndup(imsg.data + off, s->name_len);
739 err = got_error_from_errno("strndup");
743 remain -= s->name_len;
744 if (remain < s->target_len) {
745 err = got_error(GOT_ERR_PRIVSEP_LEN);
749 target = strndup(imsg.data + off, s->target_len);
750 if (target == NULL) {
751 err = got_error_from_errno("strndup");
755 off += s->target_len;
756 remain -= s->target_len;
757 err = got_pathlist_append(symrefs, name, target);
765 case GOT_IMSG_FETCH_REF:
766 if (datalen <= SHA1_DIGEST_LENGTH) {
767 err = got_error(GOT_ERR_PRIVSEP_MSG);
770 *id = malloc(sizeof(**id));
772 err = got_error_from_errno("malloc");
775 memcpy((*id)->sha1, imsg.data, SHA1_DIGEST_LENGTH);
776 *refname = strndup(imsg.data + SHA1_DIGEST_LENGTH,
777 datalen - SHA1_DIGEST_LENGTH);
778 if (*refname == NULL) {
779 err = got_error_from_errno("strndup");
783 case GOT_IMSG_FETCH_SERVER_PROGRESS:
785 err = got_error(GOT_ERR_PRIVSEP_LEN);
788 *server_progress = strndup(imsg.data, datalen);
789 if (*server_progress == NULL) {
790 err = got_error_from_errno("strndup");
793 for (i = 0; i < datalen; i++) {
794 if (!isprint((unsigned char)(*server_progress)[i]) &&
795 !isspace((unsigned char)(*server_progress)[i])) {
796 err = got_error(GOT_ERR_PRIVSEP_MSG);
797 free(*server_progress);
798 *server_progress = NULL;
803 case GOT_IMSG_FETCH_DOWNLOAD_PROGRESS:
804 if (datalen < sizeof(*packfile_size)) {
805 err = got_error(GOT_ERR_PRIVSEP_MSG);
808 memcpy(packfile_size, imsg.data, sizeof(*packfile_size));
810 case GOT_IMSG_FETCH_DONE:
811 if (datalen != SHA1_DIGEST_LENGTH) {
812 err = got_error(GOT_ERR_PRIVSEP_MSG);
815 memcpy(pack_sha1, imsg.data, SHA1_DIGEST_LENGTH);
819 err = got_error(GOT_ERR_PRIVSEP_MSG);
833 static const struct got_error *
834 send_send_ref(const char *name, size_t name_len, struct got_object_id *id,
835 int delete, struct imsgbuf *ibuf)
840 len = sizeof(struct got_imsg_send_ref) + name_len;
841 wbuf = imsg_create(ibuf, GOT_IMSG_SEND_REF, 0, 0, len);
843 return got_error_from_errno("imsg_create SEND_REF");
845 /* Keep in sync with struct got_imsg_send_ref! */
846 if (imsg_add(wbuf, id->sha1, sizeof(id->sha1)) == -1)
847 return got_error_from_errno("imsg_add SEND_REF");
848 if (imsg_add(wbuf, &delete, sizeof(delete)) == -1)
849 return got_error_from_errno("imsg_add SEND_REF");
850 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1)
851 return got_error_from_errno("imsg_add SEND_REF");
852 if (imsg_add(wbuf, name, name_len) == -1)
853 return got_error_from_errno("imsg_add SEND_REF");
856 imsg_close(ibuf, wbuf);
857 return flush_imsg(ibuf);
860 const struct got_error *
861 got_privsep_send_send_req(struct imsgbuf *ibuf, int fd,
862 struct got_pathlist_head *have_refs,
863 struct got_pathlist_head *delete_refs,
866 const struct got_error *err = NULL;
867 struct got_pathlist_entry *pe;
868 struct got_imsg_send_request sendreq;
869 struct got_object_id zero_id;
871 memset(&zero_id, 0, sizeof(zero_id));
872 memset(&sendreq, 0, sizeof(sendreq));
873 sendreq.verbosity = verbosity;
874 TAILQ_FOREACH(pe, have_refs, entry)
876 TAILQ_FOREACH(pe, delete_refs, entry)
878 if (imsg_compose(ibuf, GOT_IMSG_SEND_REQUEST, 0, 0, fd,
879 &sendreq, sizeof(sendreq)) == -1) {
880 err = got_error_from_errno(
881 "imsg_compose FETCH_SERVER_PROGRESS");
885 err = flush_imsg(ibuf);
890 TAILQ_FOREACH(pe, have_refs, entry) {
891 const char *name = pe->path;
892 size_t name_len = pe->path_len;
893 struct got_object_id *id = pe->data;
894 err = send_send_ref(name, name_len, id, 0, ibuf);
899 TAILQ_FOREACH(pe, delete_refs, entry) {
900 const char *name = pe->path;
901 size_t name_len = pe->path_len;
902 err = send_send_ref(name, name_len, &zero_id, 1, ibuf);
907 if (fd != -1 && close(fd) == -1 && err == NULL)
908 err = got_error_from_errno("close");
913 const struct got_error *
914 got_privsep_recv_send_remote_refs(struct got_pathlist_head *remote_refs,
915 struct imsgbuf *ibuf)
917 const struct got_error *err = NULL;
921 struct got_imsg_send_remote_ref iremote_ref;
922 struct got_object_id *id = NULL;
923 char *refname = NULL;
924 struct got_pathlist_entry *new;
927 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
930 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
931 switch (imsg.hdr.type) {
933 if (datalen < sizeof(struct got_imsg_error)) {
934 err = got_error(GOT_ERR_PRIVSEP_LEN);
937 err = recv_imsg_error(&imsg, datalen);
939 case GOT_IMSG_SEND_REMOTE_REF:
940 if (datalen < sizeof(iremote_ref)) {
941 err = got_error(GOT_ERR_PRIVSEP_MSG);
944 memcpy(&iremote_ref, imsg.data, sizeof(iremote_ref));
945 if (datalen != sizeof(iremote_ref) +
946 iremote_ref.name_len) {
947 err = got_error(GOT_ERR_PRIVSEP_MSG);
950 id = malloc(sizeof(*id));
952 err = got_error_from_errno("malloc");
955 memcpy(id->sha1, iremote_ref.id, SHA1_DIGEST_LENGTH);
956 refname = strndup(imsg.data + sizeof(iremote_ref),
957 datalen - sizeof(iremote_ref));
958 if (refname == NULL) {
959 err = got_error_from_errno("strndup");
962 err = got_pathlist_insert(&new, remote_refs,
966 if (new == NULL) { /* duplicate which wasn't inserted */
973 case GOT_IMSG_SEND_PACK_REQUEST:
975 err = got_error(GOT_ERR_PRIVSEP_MSG);
978 /* got-send-pack is now waiting for a pack file. */
982 err = got_error(GOT_ERR_PRIVSEP_MSG);
993 const struct got_error *
994 got_privsep_send_packfd(struct imsgbuf *ibuf, int fd)
996 return send_fd(ibuf, GOT_IMSG_SEND_PACKFD, fd);
999 const struct got_error *
1000 got_privsep_recv_send_progress(int *done, off_t *bytes_sent,
1001 int *success, char **refname, struct imsgbuf *ibuf)
1003 const struct got_error *err = NULL;
1006 struct got_imsg_send_ref_status iref_status;
1008 /* Do not reset the current value of 'bytes_sent', it accumulates. */
1013 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1017 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1018 switch (imsg.hdr.type) {
1019 case GOT_IMSG_ERROR:
1020 if (datalen < sizeof(struct got_imsg_error)) {
1021 err = got_error(GOT_ERR_PRIVSEP_LEN);
1024 err = recv_imsg_error(&imsg, datalen);
1026 case GOT_IMSG_SEND_UPLOAD_PROGRESS:
1027 if (datalen < sizeof(*bytes_sent)) {
1028 err = got_error(GOT_ERR_PRIVSEP_MSG);
1031 memcpy(bytes_sent, imsg.data, sizeof(*bytes_sent));
1033 case GOT_IMSG_SEND_REF_STATUS:
1034 if (datalen < sizeof(iref_status)) {
1035 err = got_error(GOT_ERR_PRIVSEP_MSG);
1038 memcpy(&iref_status, imsg.data, sizeof(iref_status));
1039 if (datalen != sizeof(iref_status) + iref_status.name_len) {
1040 err = got_error(GOT_ERR_PRIVSEP_MSG);
1043 *success = iref_status.success;
1044 *refname = strndup(imsg.data + sizeof(iref_status),
1045 iref_status.name_len);
1047 case GOT_IMSG_SEND_DONE:
1049 err = got_error(GOT_ERR_PRIVSEP_MSG);
1055 err = got_error(GOT_ERR_PRIVSEP_MSG);
1063 const struct got_error *
1064 got_privsep_send_index_pack_req(struct imsgbuf *ibuf, uint8_t *pack_sha1,
1067 const struct got_error *err = NULL;
1069 /* Keep in sync with struct got_imsg_index_pack_request */
1070 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_REQUEST, 0, 0, fd,
1071 pack_sha1, SHA1_DIGEST_LENGTH) == -1) {
1072 err = got_error_from_errno("imsg_compose INDEX_REQUEST");
1076 return flush_imsg(ibuf);
1079 const struct got_error *
1080 got_privsep_send_index_pack_outfd(struct imsgbuf *ibuf, int fd)
1082 return send_fd(ibuf, GOT_IMSG_IDXPACK_OUTFD, fd);
1085 const struct got_error *
1086 got_privsep_recv_index_progress(int *done, int *nobj_total,
1087 int *nobj_indexed, int *nobj_loose, int *nobj_resolved,
1088 struct imsgbuf *ibuf)
1090 const struct got_error *err = NULL;
1092 struct got_imsg_index_pack_progress *iprogress;
1100 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1104 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1105 switch (imsg.hdr.type) {
1106 case GOT_IMSG_ERROR:
1107 if (datalen < sizeof(struct got_imsg_error)) {
1108 err = got_error(GOT_ERR_PRIVSEP_LEN);
1111 err = recv_imsg_error(&imsg, datalen);
1113 case GOT_IMSG_IDXPACK_PROGRESS:
1114 if (datalen < sizeof(*iprogress)) {
1115 err = got_error(GOT_ERR_PRIVSEP_LEN);
1118 iprogress = (struct got_imsg_index_pack_progress *)imsg.data;
1119 *nobj_total = iprogress->nobj_total;
1120 *nobj_indexed = iprogress->nobj_indexed;
1121 *nobj_loose = iprogress->nobj_loose;
1122 *nobj_resolved = iprogress->nobj_resolved;
1124 case GOT_IMSG_IDXPACK_DONE:
1126 err = got_error(GOT_ERR_PRIVSEP_LEN);
1132 err = got_error(GOT_ERR_PRIVSEP_MSG);
1140 const struct got_error *
1141 got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
1142 struct imsgbuf *ibuf)
1144 struct got_imsg_object *iobj;
1145 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1147 if (datalen != sizeof(*iobj))
1148 return got_error(GOT_ERR_PRIVSEP_LEN);
1151 *obj = calloc(1, sizeof(**obj));
1153 return got_error_from_errno("calloc");
1155 memcpy((*obj)->id.sha1, iobj->id, SHA1_DIGEST_LENGTH);
1156 (*obj)->type = iobj->type;
1157 (*obj)->flags = iobj->flags;
1158 (*obj)->hdrlen = iobj->hdrlen;
1159 (*obj)->size = iobj->size;
1160 /* path_packfile is handled by caller */
1161 if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
1162 (*obj)->pack_offset = iobj->pack_offset;
1163 (*obj)->pack_idx = iobj->pack_idx;
1165 STAILQ_INIT(&(*obj)->deltas.entries);
1169 const struct got_error *
1170 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
1172 const struct got_error *err = NULL;
1174 const size_t min_datalen =
1175 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
1179 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1183 switch (imsg.hdr.type) {
1184 case GOT_IMSG_OBJECT:
1185 err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
1188 err = got_error(GOT_ERR_PRIVSEP_MSG);
1197 static const struct got_error *
1198 send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
1201 const struct got_error *err = NULL;
1202 size_t offset, remain;
1205 remain = logmsg_len;
1206 while (remain > 0) {
1207 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1209 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
1210 commit->logmsg + offset, n) == -1) {
1211 err = got_error_from_errno("imsg_compose "
1216 err = flush_imsg(ibuf);
1227 const struct got_error *
1228 got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
1230 const struct got_error *err = NULL;
1231 struct got_imsg_commit_object *icommit;
1234 struct got_object_qid *qid;
1235 size_t author_len = strlen(commit->author);
1236 size_t committer_len = strlen(commit->committer);
1237 size_t logmsg_len = strlen(commit->logmsg);
1239 total = sizeof(*icommit) + author_len + committer_len +
1240 commit->nparents * SHA1_DIGEST_LENGTH;
1242 buf = malloc(total);
1244 return got_error_from_errno("malloc");
1246 icommit = (struct got_imsg_commit_object *)buf;
1247 memcpy(icommit->tree_id, commit->tree_id->sha1,
1248 sizeof(icommit->tree_id));
1249 icommit->author_len = author_len;
1250 icommit->author_time = commit->author_time;
1251 icommit->author_gmtoff = commit->author_gmtoff;
1252 icommit->committer_len = committer_len;
1253 icommit->committer_time = commit->committer_time;
1254 icommit->committer_gmtoff = commit->committer_gmtoff;
1255 icommit->logmsg_len = logmsg_len;
1256 icommit->nparents = commit->nparents;
1258 len = sizeof(*icommit);
1259 memcpy(buf + len, commit->author, author_len);
1261 memcpy(buf + len, commit->committer, committer_len);
1262 len += committer_len;
1263 STAILQ_FOREACH(qid, &commit->parent_ids, entry) {
1264 memcpy(buf + len, &qid->id, SHA1_DIGEST_LENGTH);
1265 len += SHA1_DIGEST_LENGTH;
1268 if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
1269 err = got_error_from_errno("imsg_compose COMMIT");
1273 if (logmsg_len == 0 ||
1274 logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1275 err = flush_imsg(ibuf);
1279 err = send_commit_logmsg(ibuf, commit, logmsg_len);
1285 static const struct got_error *
1286 get_commit_from_imsg(struct got_commit_object **commit,
1287 struct imsg *imsg, size_t datalen, struct imsgbuf *ibuf)
1289 const struct got_error *err = NULL;
1290 struct got_imsg_commit_object *icommit;
1294 if (datalen < sizeof(*icommit))
1295 return got_error(GOT_ERR_PRIVSEP_LEN);
1297 icommit = imsg->data;
1298 if (datalen != sizeof(*icommit) + icommit->author_len +
1299 icommit->committer_len +
1300 icommit->nparents * SHA1_DIGEST_LENGTH)
1301 return got_error(GOT_ERR_PRIVSEP_LEN);
1303 if (icommit->nparents < 0)
1304 return got_error(GOT_ERR_PRIVSEP_LEN);
1306 len += sizeof(*icommit);
1308 *commit = got_object_commit_alloc_partial();
1309 if (*commit == NULL)
1310 return got_error_from_errno(
1311 "got_object_commit_alloc_partial");
1313 memcpy((*commit)->tree_id->sha1, icommit->tree_id,
1314 SHA1_DIGEST_LENGTH);
1315 (*commit)->author_time = icommit->author_time;
1316 (*commit)->author_gmtoff = icommit->author_gmtoff;
1317 (*commit)->committer_time = icommit->committer_time;
1318 (*commit)->committer_gmtoff = icommit->committer_gmtoff;
1320 if (icommit->author_len == 0) {
1321 (*commit)->author = strdup("");
1322 if ((*commit)->author == NULL) {
1323 err = got_error_from_errno("strdup");
1327 (*commit)->author = malloc(icommit->author_len + 1);
1328 if ((*commit)->author == NULL) {
1329 err = got_error_from_errno("malloc");
1332 memcpy((*commit)->author, imsg->data + len,
1333 icommit->author_len);
1334 (*commit)->author[icommit->author_len] = '\0';
1336 len += icommit->author_len;
1338 if (icommit->committer_len == 0) {
1339 (*commit)->committer = strdup("");
1340 if ((*commit)->committer == NULL) {
1341 err = got_error_from_errno("strdup");
1345 (*commit)->committer =
1346 malloc(icommit->committer_len + 1);
1347 if ((*commit)->committer == NULL) {
1348 err = got_error_from_errno("malloc");
1351 memcpy((*commit)->committer, imsg->data + len,
1352 icommit->committer_len);
1353 (*commit)->committer[icommit->committer_len] = '\0';
1355 len += icommit->committer_len;
1357 if (icommit->logmsg_len == 0) {
1358 (*commit)->logmsg = strdup("");
1359 if ((*commit)->logmsg == NULL) {
1360 err = got_error_from_errno("strdup");
1364 size_t offset = 0, remain = icommit->logmsg_len;
1366 (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
1367 if ((*commit)->logmsg == NULL) {
1368 err = got_error_from_errno("malloc");
1371 while (remain > 0) {
1372 struct imsg imsg_log;
1373 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1376 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1380 if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG) {
1381 err = got_error(GOT_ERR_PRIVSEP_MSG);
1385 memcpy((*commit)->logmsg + offset,
1387 imsg_free(&imsg_log);
1391 (*commit)->logmsg[icommit->logmsg_len] = '\0';
1394 for (i = 0; i < icommit->nparents; i++) {
1395 struct got_object_qid *qid;
1397 err = got_object_qid_alloc_partial(&qid);
1400 memcpy(&qid->id, imsg->data + len +
1401 i * SHA1_DIGEST_LENGTH, sizeof(qid->id));
1402 STAILQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
1403 (*commit)->nparents++;
1407 got_object_commit_close(*commit);
1413 const struct got_error *
1414 got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
1416 const struct got_error *err = NULL;
1419 const size_t min_datalen =
1420 MIN(sizeof(struct got_imsg_error),
1421 sizeof(struct got_imsg_commit_object));
1425 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1429 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1431 switch (imsg.hdr.type) {
1432 case GOT_IMSG_COMMIT:
1433 err = get_commit_from_imsg(commit, &imsg, datalen, ibuf);
1436 err = got_error(GOT_ERR_PRIVSEP_MSG);
1445 static const struct got_error *
1446 send_tree_entries(struct imsgbuf *ibuf, struct got_parsed_tree_entry *entries,
1447 int idx0, int idxN, size_t len)
1450 struct got_imsg_tree_entries ientries;
1453 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_ENTRIES, 0, 0, len);
1455 return got_error_from_errno("imsg_create TREE_ENTRY");
1457 ientries.nentries = idxN - idx0 + 1;
1458 if (imsg_add(wbuf, &ientries, sizeof(ientries)) == -1)
1459 return got_error_from_errno("imsg_add TREE_ENTRY");
1461 for (i = idx0; i <= idxN; i++) {
1462 struct got_parsed_tree_entry *pte = &entries[i];
1464 /* Keep in sync with struct got_imsg_tree_object definition! */
1465 if (imsg_add(wbuf, pte->id, SHA1_DIGEST_LENGTH) == -1)
1466 return got_error_from_errno("imsg_add TREE_ENTRY");
1467 if (imsg_add(wbuf, &pte->mode, sizeof(pte->mode)) == -1)
1468 return got_error_from_errno("imsg_add TREE_ENTRY");
1469 if (imsg_add(wbuf, &pte->namelen, sizeof(pte->namelen)) == -1)
1470 return got_error_from_errno("imsg_add TREE_ENTRY");
1472 /* Remaining bytes are the entry's name. */
1473 if (imsg_add(wbuf, pte->name, pte->namelen) == -1)
1474 return got_error_from_errno("imsg_add TREE_ENTRY");
1478 imsg_close(ibuf, wbuf);
1482 const struct got_error *
1483 got_privsep_send_tree(struct imsgbuf *ibuf,
1484 struct got_parsed_tree_entry *entries, int nentries)
1486 const struct got_error *err = NULL;
1487 struct got_imsg_tree_object itree;
1491 itree.nentries = nentries;
1492 if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
1494 return got_error_from_errno("imsg_compose TREE");
1496 entries_len = sizeof(struct got_imsg_tree_entries);
1498 for (j = 0; j < nentries; j++) {
1499 struct got_parsed_tree_entry *pte = &entries[j];
1500 size_t len = sizeof(*pte) + pte->namelen;
1503 entries_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1504 err = send_tree_entries(ibuf, entries,
1505 i, j - 1, entries_len);
1509 entries_len = sizeof(struct got_imsg_tree_entries);
1516 err = send_tree_entries(ibuf, entries, i, j - 1, entries_len);
1521 return flush_imsg(ibuf);
1524 const struct got_error *
1525 got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
1527 const struct got_error *err = NULL;
1528 const size_t min_datalen =
1529 MIN(sizeof(struct got_imsg_error),
1530 sizeof(struct got_imsg_tree_object));
1531 struct got_imsg_tree_object *itree;
1537 err = read_imsg(ibuf);
1545 struct got_imsg_tree_entries *ientries;
1546 struct got_tree_entry *te = NULL;
1549 n = imsg_get(ibuf, &imsg);
1552 if (nentries < (*tree)->nentries) {
1553 err = read_imsg(ibuf);
1560 err = got_error(GOT_ERR_PRIVSEP_MSG);
1565 if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen) {
1567 err = got_error(GOT_ERR_PRIVSEP_LEN);
1571 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1573 switch (imsg.hdr.type) {
1574 case GOT_IMSG_ERROR:
1575 err = recv_imsg_error(&imsg, datalen);
1578 /* This message should only appear once. */
1579 if (*tree != NULL) {
1580 err = got_error(GOT_ERR_PRIVSEP_MSG);
1583 if (datalen != sizeof(*itree)) {
1584 err = got_error(GOT_ERR_PRIVSEP_LEN);
1588 if (itree->nentries < 0) {
1589 err = got_error(GOT_ERR_PRIVSEP_LEN);
1592 *tree = malloc(sizeof(**tree));
1593 if (*tree == NULL) {
1594 err = got_error_from_errno("malloc");
1597 (*tree)->entries = calloc(itree->nentries,
1598 sizeof(struct got_tree_entry));
1599 if ((*tree)->entries == NULL) {
1600 err = got_error_from_errno("malloc");
1605 (*tree)->nentries = itree->nentries;
1606 (*tree)->refcnt = 0;
1608 case GOT_IMSG_TREE_ENTRIES:
1609 /* This message should be preceeded by GOT_IMSG_TREE. */
1610 if (*tree == NULL) {
1611 err = got_error(GOT_ERR_PRIVSEP_MSG);
1614 if (datalen <= sizeof(*ientries) ||
1615 datalen > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1616 err = got_error(GOT_ERR_PRIVSEP_LEN);
1620 ientries = imsg.data;
1621 if (ientries->nentries > INT_MAX) {
1622 err = got_error_msg(GOT_ERR_NO_SPACE,
1623 "too many tree entries");
1626 te_offset = sizeof(*ientries);
1627 for (i = 0; i < ientries->nentries; i++) {
1628 struct got_imsg_tree_entry ite;
1629 const char *te_name;
1630 uint8_t *buf = imsg.data + te_offset;
1632 if (te_offset >= datalen) {
1633 err = got_error(GOT_ERR_PRIVSEP_LEN);
1637 /* Might not be aligned, size is ~32 bytes. */
1638 memcpy(&ite, buf, sizeof(ite));
1640 if (ite.namelen >= sizeof(te->name)) {
1641 err = got_error(GOT_ERR_PRIVSEP_LEN);
1644 if (te_offset + sizeof(ite) + ite.namelen >
1646 err = got_error(GOT_ERR_PRIVSEP_LEN);
1649 if (nentries >= (*tree)->nentries) {
1650 err = got_error(GOT_ERR_PRIVSEP_LEN);
1653 te = &(*tree)->entries[nentries];
1654 te_name = buf + sizeof(ite);
1655 memcpy(te->name, te_name, ite.namelen);
1656 te->name[ite.namelen] = '\0';
1657 memcpy(te->id.sha1, ite.id, SHA1_DIGEST_LENGTH);
1658 te->mode = ite.mode;
1662 te_offset += sizeof(ite) + ite.namelen;
1666 err = got_error(GOT_ERR_PRIVSEP_MSG);
1675 if (*tree && (*tree)->nentries != nentries) {
1677 err = got_error(GOT_ERR_PRIVSEP_LEN);
1678 got_object_tree_close(*tree);
1685 const struct got_error *
1686 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
1687 const uint8_t *data)
1689 struct got_imsg_blob iblob;
1692 iblob.hdrlen = hdrlen;
1697 if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
1698 return got_error(GOT_ERR_NO_SPACE);
1700 buf = malloc(sizeof(iblob) + size);
1702 return got_error_from_errno("malloc");
1704 memcpy(buf, &iblob, sizeof(iblob));
1705 memcpy(buf + sizeof(iblob), data, size);
1706 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
1707 sizeof(iblob) + size) == -1) {
1709 return got_error_from_errno("imsg_compose BLOB");
1713 /* Data has already been written to file descriptor. */
1714 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
1715 sizeof(iblob)) == -1)
1716 return got_error_from_errno("imsg_compose BLOB");
1720 return flush_imsg(ibuf);
1723 const struct got_error *
1724 got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1725 struct imsgbuf *ibuf)
1727 const struct got_error *err = NULL;
1729 struct got_imsg_blob *iblob;
1734 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1738 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1740 switch (imsg.hdr.type) {
1742 if (datalen < sizeof(*iblob)) {
1743 err = got_error(GOT_ERR_PRIVSEP_LEN);
1747 *size = iblob->size;
1748 *hdrlen = iblob->hdrlen;
1750 if (datalen == sizeof(*iblob)) {
1751 /* Data has been written to file descriptor. */
1755 if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX) {
1756 err = got_error(GOT_ERR_PRIVSEP_LEN);
1760 *outbuf = malloc(*size);
1761 if (*outbuf == NULL) {
1762 err = got_error_from_errno("malloc");
1765 memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
1768 err = got_error(GOT_ERR_PRIVSEP_MSG);
1777 static const struct got_error *
1778 send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
1780 const struct got_error *err = NULL;
1781 size_t offset, remain;
1784 remain = tagmsg_len;
1785 while (remain > 0) {
1786 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1788 if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
1789 tag->tagmsg + offset, n) == -1) {
1790 err = got_error_from_errno("imsg_compose TAG_TAGMSG");
1794 err = flush_imsg(ibuf);
1805 const struct got_error *
1806 got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
1808 const struct got_error *err = NULL;
1809 struct got_imsg_tag_object *itag;
1812 size_t tag_len = strlen(tag->tag);
1813 size_t tagger_len = strlen(tag->tagger);
1814 size_t tagmsg_len = strlen(tag->tagmsg);
1816 total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
1818 buf = malloc(total);
1820 return got_error_from_errno("malloc");
1822 itag = (struct got_imsg_tag_object *)buf;
1823 memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
1824 itag->obj_type = tag->obj_type;
1825 itag->tag_len = tag_len;
1826 itag->tagger_len = tagger_len;
1827 itag->tagger_time = tag->tagger_time;
1828 itag->tagger_gmtoff = tag->tagger_gmtoff;
1829 itag->tagmsg_len = tagmsg_len;
1831 len = sizeof(*itag);
1832 memcpy(buf + len, tag->tag, tag_len);
1834 memcpy(buf + len, tag->tagger, tagger_len);
1837 if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
1838 err = got_error_from_errno("imsg_compose TAG");
1842 if (tagmsg_len == 0 ||
1843 tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1844 err = flush_imsg(ibuf);
1848 err = send_tagmsg(ibuf, tag, tagmsg_len);
1854 const struct got_error *
1855 got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
1857 const struct got_error *err = NULL;
1859 struct got_imsg_tag_object *itag;
1860 size_t len, datalen;
1861 const size_t min_datalen =
1862 MIN(sizeof(struct got_imsg_error),
1863 sizeof(struct got_imsg_tag_object));
1867 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1871 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1874 switch (imsg.hdr.type) {
1876 if (datalen < sizeof(*itag)) {
1877 err = got_error(GOT_ERR_PRIVSEP_LEN);
1881 if (datalen != sizeof(*itag) + itag->tag_len +
1883 err = got_error(GOT_ERR_PRIVSEP_LEN);
1886 len += sizeof(*itag);
1888 *tag = calloc(1, sizeof(**tag));
1890 err = got_error_from_errno("calloc");
1894 memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
1896 if (itag->tag_len == 0) {
1897 (*tag)->tag = strdup("");
1898 if ((*tag)->tag == NULL) {
1899 err = got_error_from_errno("strdup");
1903 (*tag)->tag = malloc(itag->tag_len + 1);
1904 if ((*tag)->tag == NULL) {
1905 err = got_error_from_errno("malloc");
1908 memcpy((*tag)->tag, imsg.data + len,
1910 (*tag)->tag[itag->tag_len] = '\0';
1912 len += itag->tag_len;
1914 (*tag)->obj_type = itag->obj_type;
1915 (*tag)->tagger_time = itag->tagger_time;
1916 (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1918 if (itag->tagger_len == 0) {
1919 (*tag)->tagger = strdup("");
1920 if ((*tag)->tagger == NULL) {
1921 err = got_error_from_errno("strdup");
1925 (*tag)->tagger = malloc(itag->tagger_len + 1);
1926 if ((*tag)->tagger == NULL) {
1927 err = got_error_from_errno("malloc");
1930 memcpy((*tag)->tagger, imsg.data + len,
1932 (*tag)->tagger[itag->tagger_len] = '\0';
1934 len += itag->tagger_len;
1936 if (itag->tagmsg_len == 0) {
1937 (*tag)->tagmsg = strdup("");
1938 if ((*tag)->tagmsg == NULL) {
1939 err = got_error_from_errno("strdup");
1943 size_t offset = 0, remain = itag->tagmsg_len;
1945 (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1946 if ((*tag)->tagmsg == NULL) {
1947 err = got_error_from_errno("malloc");
1950 while (remain > 0) {
1951 struct imsg imsg_log;
1952 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1955 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1959 if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1960 return got_error(GOT_ERR_PRIVSEP_MSG);
1962 memcpy((*tag)->tagmsg + offset, imsg_log.data,
1964 imsg_free(&imsg_log);
1968 (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1973 err = got_error(GOT_ERR_PRIVSEP_MSG);
1982 const struct got_error *
1983 got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1984 struct got_packidx *packidx)
1986 const struct got_error *err = NULL;
1987 struct got_imsg_packidx ipackidx;
1988 struct got_imsg_pack ipack;
1991 ipackidx.len = packidx->len;
1992 ipackidx.packfile_size = pack->filesize;
1993 fd = dup(packidx->fd);
1995 return got_error_from_errno("dup");
1997 if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
1998 sizeof(ipackidx)) == -1) {
1999 err = got_error_from_errno("imsg_compose PACKIDX");
2004 if (strlcpy(ipack.path_packfile, pack->path_packfile,
2005 sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
2006 return got_error(GOT_ERR_NO_SPACE);
2007 ipack.filesize = pack->filesize;
2011 return got_error_from_errno("dup");
2013 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
2015 err = got_error_from_errno("imsg_compose PACK");
2020 return flush_imsg(ibuf);
2023 const struct got_error *
2024 got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
2025 struct got_object_id *id)
2027 struct got_imsg_packed_object iobj;
2030 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
2032 if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
2033 &iobj, sizeof(iobj)) == -1)
2034 return got_error_from_errno("imsg_compose "
2035 "PACKED_OBJECT_REQUEST");
2037 return flush_imsg(ibuf);
2040 const struct got_error *
2041 got_privsep_send_packed_raw_obj_req(struct imsgbuf *ibuf, int idx,
2042 struct got_object_id *id)
2044 struct got_imsg_packed_object iobj;
2047 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
2049 if (imsg_compose(ibuf, GOT_IMSG_PACKED_RAW_OBJECT_REQUEST, 0, 0, -1,
2050 &iobj, sizeof(iobj)) == -1)
2051 return got_error_from_errno("imsg_compose "
2052 "PACKED_OBJECT_REQUEST");
2054 return flush_imsg(ibuf);
2057 const struct got_error *
2058 got_privsep_send_gitconfig_parse_req(struct imsgbuf *ibuf, int fd)
2060 const struct got_error *err = NULL;
2062 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_PARSE_REQUEST, 0, 0, fd,
2064 err = got_error_from_errno("imsg_compose "
2065 "GITCONFIG_PARSE_REQUEST");
2070 return flush_imsg(ibuf);
2073 const struct got_error *
2074 got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *ibuf)
2076 if (imsg_compose(ibuf,
2077 GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST, 0, 0, -1,
2079 return got_error_from_errno("imsg_compose "
2080 "GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST");
2082 return flush_imsg(ibuf);
2085 const struct got_error *
2086 got_privsep_send_gitconfig_repository_extensions_req(struct imsgbuf *ibuf)
2088 if (imsg_compose(ibuf,
2089 GOT_IMSG_GITCONFIG_REPOSITORY_EXTENSIONS_REQUEST, 0, 0, -1,
2091 return got_error_from_errno("imsg_compose "
2092 "GITCONFIG_REPOSITORY_EXTENSIONS_REQUEST");
2094 return flush_imsg(ibuf);
2098 const struct got_error *
2099 got_privsep_send_gitconfig_author_name_req(struct imsgbuf *ibuf)
2101 if (imsg_compose(ibuf,
2102 GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST, 0, 0, -1, NULL, 0) == -1)
2103 return got_error_from_errno("imsg_compose "
2104 "GITCONFIG_AUTHOR_NAME_REQUEST");
2106 return flush_imsg(ibuf);
2109 const struct got_error *
2110 got_privsep_send_gitconfig_author_email_req(struct imsgbuf *ibuf)
2112 if (imsg_compose(ibuf,
2113 GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST, 0, 0, -1, NULL, 0) == -1)
2114 return got_error_from_errno("imsg_compose "
2115 "GITCONFIG_AUTHOR_EMAIL_REQUEST");
2117 return flush_imsg(ibuf);
2120 const struct got_error *
2121 got_privsep_send_gitconfig_remotes_req(struct imsgbuf *ibuf)
2123 if (imsg_compose(ibuf,
2124 GOT_IMSG_GITCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
2125 return got_error_from_errno("imsg_compose "
2126 "GITCONFIG_REMOTE_REQUEST");
2128 return flush_imsg(ibuf);
2131 const struct got_error *
2132 got_privsep_send_gitconfig_owner_req(struct imsgbuf *ibuf)
2134 if (imsg_compose(ibuf,
2135 GOT_IMSG_GITCONFIG_OWNER_REQUEST, 0, 0, -1, NULL, 0) == -1)
2136 return got_error_from_errno("imsg_compose "
2137 "GITCONFIG_OWNER_REQUEST");
2139 return flush_imsg(ibuf);
2142 const struct got_error *
2143 got_privsep_recv_gitconfig_str(char **str, struct imsgbuf *ibuf)
2145 const struct got_error *err = NULL;
2148 const size_t min_datalen = 0;
2152 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2155 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2157 switch (imsg.hdr.type) {
2158 case GOT_IMSG_GITCONFIG_STR_VAL:
2161 /* datalen does not include terminating \0 */
2162 *str = malloc(datalen + 1);
2164 err = got_error_from_errno("malloc");
2167 memcpy(*str, imsg.data, datalen);
2168 (*str)[datalen] = '\0';
2171 err = got_error(GOT_ERR_PRIVSEP_MSG);
2179 const struct got_error *
2180 got_privsep_recv_gitconfig_int(int *val, struct imsgbuf *ibuf)
2182 const struct got_error *err = NULL;
2185 const size_t min_datalen =
2186 MIN(sizeof(struct got_imsg_error), sizeof(int));
2190 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2193 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2195 switch (imsg.hdr.type) {
2196 case GOT_IMSG_GITCONFIG_INT_VAL:
2197 if (datalen != sizeof(*val)) {
2198 err = got_error(GOT_ERR_PRIVSEP_LEN);
2201 memcpy(val, imsg.data, sizeof(*val));
2204 err = got_error(GOT_ERR_PRIVSEP_MSG);
2213 free_remote_data(struct got_remote_repo *remote)
2218 free(remote->fetch_url);
2219 free(remote->send_url);
2220 for (i = 0; i < remote->nfetch_branches; i++)
2221 free(remote->fetch_branches[i]);
2222 free(remote->fetch_branches);
2223 for (i = 0; i < remote->nsend_branches; i++)
2224 free(remote->send_branches[i]);
2225 free(remote->send_branches);
2226 for (i = 0; i < remote->nfetch_refs; i++)
2227 free(remote->fetch_refs[i]);
2228 free(remote->fetch_refs);
2231 const struct got_error *
2232 got_privsep_recv_gitconfig_remotes(struct got_remote_repo **remotes,
2233 int *nremotes, struct imsgbuf *ibuf)
2235 const struct got_error *err = NULL;
2238 struct got_imsg_remotes iremotes;
2239 struct got_imsg_remote iremote;
2243 iremotes.nremotes = 0;
2245 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremotes));
2248 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2250 switch (imsg.hdr.type) {
2251 case GOT_IMSG_GITCONFIG_REMOTES:
2252 if (datalen != sizeof(iremotes)) {
2253 err = got_error(GOT_ERR_PRIVSEP_LEN);
2256 memcpy(&iremotes, imsg.data, sizeof(iremotes));
2257 if (iremotes.nremotes == 0) {
2264 return got_error(GOT_ERR_PRIVSEP_MSG);
2269 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(**remotes));
2270 if (*remotes == NULL)
2271 return got_error_from_errno("recallocarray");
2273 while (*nremotes < iremotes.nremotes) {
2274 struct got_remote_repo *remote;
2276 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremote));
2279 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2281 switch (imsg.hdr.type) {
2282 case GOT_IMSG_GITCONFIG_REMOTE:
2283 remote = &(*remotes)[*nremotes];
2284 memset(remote, 0, sizeof(*remote));
2285 if (datalen < sizeof(iremote)) {
2286 err = got_error(GOT_ERR_PRIVSEP_LEN);
2289 memcpy(&iremote, imsg.data, sizeof(iremote));
2290 if (iremote.name_len == 0 ||
2291 iremote.fetch_url_len == 0 ||
2292 iremote.send_url_len == 0 ||
2293 (sizeof(iremote) + iremote.name_len +
2294 iremote.fetch_url_len + iremote.send_url_len) > datalen) {
2295 err = got_error(GOT_ERR_PRIVSEP_LEN);
2298 remote->name = strndup(imsg.data + sizeof(iremote),
2300 if (remote->name == NULL) {
2301 err = got_error_from_errno("strndup");
2304 remote->fetch_url = strndup(imsg.data + sizeof(iremote) +
2305 iremote.name_len, iremote.fetch_url_len);
2306 if (remote->fetch_url == NULL) {
2307 err = got_error_from_errno("strndup");
2308 free_remote_data(remote);
2311 remote->send_url = strndup(imsg.data + sizeof(iremote) +
2312 iremote.name_len + iremote.fetch_url_len,
2313 iremote.send_url_len);
2314 if (remote->send_url == NULL) {
2315 err = got_error_from_errno("strndup");
2316 free_remote_data(remote);
2319 remote->mirror_references = iremote.mirror_references;
2320 remote->fetch_all_branches = iremote.fetch_all_branches;
2321 remote->nfetch_branches = 0;
2322 remote->fetch_branches = NULL;
2323 remote->nsend_branches = 0;
2324 remote->send_branches = NULL;
2325 remote->nfetch_refs = 0;
2326 remote->fetch_refs = NULL;
2330 err = got_error(GOT_ERR_PRIVSEP_MSG);
2341 for (i = 0; i < *nremotes; i++)
2342 free_remote_data(&(*remotes)[i]);
2350 const struct got_error *
2351 got_privsep_send_gotconfig_parse_req(struct imsgbuf *ibuf, int fd)
2353 const struct got_error *err = NULL;
2355 if (imsg_compose(ibuf, GOT_IMSG_GOTCONFIG_PARSE_REQUEST, 0, 0, fd,
2357 err = got_error_from_errno("imsg_compose "
2358 "GOTCONFIG_PARSE_REQUEST");
2363 return flush_imsg(ibuf);
2366 const struct got_error *
2367 got_privsep_send_gotconfig_author_req(struct imsgbuf *ibuf)
2369 if (imsg_compose(ibuf,
2370 GOT_IMSG_GOTCONFIG_AUTHOR_REQUEST, 0, 0, -1, NULL, 0) == -1)
2371 return got_error_from_errno("imsg_compose "
2372 "GOTCONFIG_AUTHOR_REQUEST");
2374 return flush_imsg(ibuf);
2377 const struct got_error *
2378 got_privsep_send_gotconfig_remotes_req(struct imsgbuf *ibuf)
2380 if (imsg_compose(ibuf,
2381 GOT_IMSG_GOTCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
2382 return got_error_from_errno("imsg_compose "
2383 "GOTCONFIG_REMOTE_REQUEST");
2385 return flush_imsg(ibuf);
2388 const struct got_error *
2389 got_privsep_recv_gotconfig_str(char **str, struct imsgbuf *ibuf)
2391 const struct got_error *err = NULL;
2394 const size_t min_datalen = 0;
2398 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2401 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2403 switch (imsg.hdr.type) {
2404 case GOT_IMSG_ERROR:
2405 if (datalen < sizeof(struct got_imsg_error)) {
2406 err = got_error(GOT_ERR_PRIVSEP_LEN);
2409 err = recv_imsg_error(&imsg, datalen);
2411 case GOT_IMSG_GOTCONFIG_STR_VAL:
2414 /* datalen does not include terminating \0 */
2415 *str = malloc(datalen + 1);
2417 err = got_error_from_errno("malloc");
2420 memcpy(*str, imsg.data, datalen);
2421 (*str)[datalen] = '\0';
2424 err = got_error(GOT_ERR_PRIVSEP_MSG);
2432 const struct got_error *
2433 got_privsep_recv_gotconfig_remotes(struct got_remote_repo **remotes,
2434 int *nremotes, struct imsgbuf *ibuf)
2436 const struct got_error *err = NULL;
2439 struct got_imsg_remotes iremotes;
2440 struct got_imsg_remote iremote;
2441 const size_t min_datalen =
2442 MIN(sizeof(struct got_imsg_error), sizeof(iremotes));
2446 iremotes.nremotes = 0;
2448 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2451 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2453 switch (imsg.hdr.type) {
2454 case GOT_IMSG_ERROR:
2455 if (datalen < sizeof(struct got_imsg_error)) {
2456 err = got_error(GOT_ERR_PRIVSEP_LEN);
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 if (datalen < sizeof(struct got_imsg_error)) {
2497 err = got_error(GOT_ERR_PRIVSEP_LEN);
2500 err = recv_imsg_error(&imsg, datalen);
2502 case GOT_IMSG_GOTCONFIG_REMOTE:
2503 remote = &(*remotes)[*nremotes];
2504 memset(remote, 0, sizeof(*remote));
2505 if (datalen < sizeof(iremote)) {
2506 err = got_error(GOT_ERR_PRIVSEP_LEN);
2509 memcpy(&iremote, imsg.data, sizeof(iremote));
2510 if (iremote.name_len == 0 ||
2511 (iremote.fetch_url_len == 0 &&
2512 iremote.send_url_len == 0) ||
2513 (sizeof(iremote) + iremote.name_len +
2514 iremote.fetch_url_len + iremote.send_url_len) >
2516 err = got_error(GOT_ERR_PRIVSEP_LEN);
2519 remote->name = strndup(imsg.data + sizeof(iremote),
2521 if (remote->name == NULL) {
2522 err = got_error_from_errno("strndup");
2525 remote->fetch_url = strndup(imsg.data +
2526 sizeof(iremote) + iremote.name_len,
2527 iremote.fetch_url_len);
2528 if (remote->fetch_url == NULL) {
2529 err = got_error_from_errno("strndup");
2530 free_remote_data(remote);
2533 remote->send_url = strndup(imsg.data +
2534 sizeof(iremote) + iremote.name_len +
2535 iremote.fetch_url_len, iremote.send_url_len);
2536 if (remote->send_url == NULL) {
2537 err = got_error_from_errno("strndup");
2538 free_remote_data(remote);
2541 remote->mirror_references = iremote.mirror_references;
2542 remote->fetch_all_branches = iremote.fetch_all_branches;
2543 if (iremote.nfetch_branches > 0) {
2544 remote->fetch_branches = recallocarray(NULL, 0,
2545 iremote.nfetch_branches, sizeof(char *));
2546 if (remote->fetch_branches == NULL) {
2547 err = got_error_from_errno("calloc");
2548 free_remote_data(remote);
2552 remote->nfetch_branches = 0;
2553 for (i = 0; i < iremote.nfetch_branches; i++) {
2555 err = got_privsep_recv_gotconfig_str(&branch,
2558 free_remote_data(remote);
2561 remote->fetch_branches[i] = branch;
2562 remote->nfetch_branches++;
2564 if (iremote.nsend_branches > 0) {
2565 remote->send_branches = recallocarray(NULL, 0,
2566 iremote.nsend_branches, sizeof(char *));
2567 if (remote->send_branches == NULL) {
2568 err = got_error_from_errno("calloc");
2569 free_remote_data(remote);
2573 remote->nsend_branches = 0;
2574 for (i = 0; i < iremote.nsend_branches; i++) {
2576 err = got_privsep_recv_gotconfig_str(&branch,
2579 free_remote_data(remote);
2582 remote->send_branches[i] = branch;
2583 remote->nsend_branches++;
2585 if (iremote.nfetch_refs > 0) {
2586 remote->fetch_refs = recallocarray(NULL, 0,
2587 iremote.nfetch_refs, sizeof(char *));
2588 if (remote->fetch_refs == NULL) {
2589 err = got_error_from_errno("calloc");
2590 free_remote_data(remote);
2594 remote->nfetch_refs = 0;
2595 for (i = 0; i < iremote.nfetch_refs; i++) {
2597 err = got_privsep_recv_gotconfig_str(&ref,
2600 free_remote_data(remote);
2603 remote->fetch_refs[i] = ref;
2604 remote->nfetch_refs++;
2609 err = got_error(GOT_ERR_PRIVSEP_MSG);
2620 for (i = 0; i < *nremotes; i++)
2621 free_remote_data(&(*remotes)[i]);
2629 const struct got_error *
2630 got_privsep_send_commit_traversal_request(struct imsgbuf *ibuf,
2631 struct got_object_id *id, int idx, const char *path)
2634 size_t path_len = strlen(path) + 1;
2636 wbuf = imsg_create(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_REQUEST, 0, 0,
2637 sizeof(struct got_imsg_commit_traversal_request) + path_len);
2639 return got_error_from_errno(
2640 "imsg_create COMMIT_TRAVERSAL_REQUEST");
2641 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1)
2642 return got_error_from_errno("imsg_add "
2643 "COMMIT_TRAVERSAL_REQUEST");
2644 if (imsg_add(wbuf, &idx, sizeof(idx)) == -1)
2645 return got_error_from_errno("imsg_add "
2646 "COMMIT_TRAVERSAL_REQUEST");
2647 if (imsg_add(wbuf, path, path_len) == -1)
2648 return got_error_from_errno("imsg_add "
2649 "COMMIT_TRAVERSAL_REQUEST");
2652 imsg_close(ibuf, wbuf);
2654 return flush_imsg(ibuf);
2657 const struct got_error *
2658 got_privsep_recv_traversed_commits(struct got_commit_object **changed_commit,
2659 struct got_object_id **changed_commit_id,
2660 struct got_object_id_queue *commit_ids, struct imsgbuf *ibuf)
2662 const struct got_error *err = NULL;
2664 struct got_imsg_traversed_commits *icommits;
2668 *changed_commit = NULL;
2669 *changed_commit_id = NULL;
2672 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2676 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2677 switch (imsg.hdr.type) {
2678 case GOT_IMSG_TRAVERSED_COMMITS:
2679 icommits = imsg.data;
2680 if (datalen != sizeof(*icommits) +
2681 icommits->ncommits * SHA1_DIGEST_LENGTH) {
2682 err = got_error(GOT_ERR_PRIVSEP_LEN);
2685 for (i = 0; i < icommits->ncommits; i++) {
2686 struct got_object_qid *qid;
2687 uint8_t *sha1 = (uint8_t *)imsg.data +
2688 sizeof(*icommits) + i * SHA1_DIGEST_LENGTH;
2689 err = got_object_qid_alloc_partial(&qid);
2692 memcpy(qid->id.sha1, sha1, SHA1_DIGEST_LENGTH);
2693 STAILQ_INSERT_TAIL(commit_ids, qid, entry);
2695 /* The last commit may contain a change. */
2696 if (i == icommits->ncommits - 1) {
2697 *changed_commit_id =
2698 got_object_id_dup(&qid->id);
2699 if (*changed_commit_id == NULL) {
2700 err = got_error_from_errno(
2701 "got_object_id_dup");
2707 case GOT_IMSG_COMMIT:
2708 if (*changed_commit_id == NULL) {
2709 err = got_error(GOT_ERR_PRIVSEP_MSG);
2712 err = get_commit_from_imsg(changed_commit, &imsg,
2715 case GOT_IMSG_COMMIT_TRAVERSAL_DONE:
2719 err = got_error(GOT_ERR_PRIVSEP_MSG);
2729 got_object_id_queue_free(commit_ids);
2733 const struct got_error *
2734 got_privsep_send_raw_delta_req(struct imsgbuf *ibuf, int idx,
2735 struct got_object_id *id)
2737 struct got_imsg_raw_delta_request dreq;
2740 memcpy(dreq.id, id->sha1, SHA1_DIGEST_LENGTH);
2742 if (imsg_compose(ibuf, GOT_IMSG_RAW_DELTA_REQUEST, 0, 0, -1,
2743 &dreq, sizeof(dreq)) == -1)
2744 return got_error_from_errno("imsg_compose RAW_DELTA_REQUEST");
2746 return flush_imsg(ibuf);
2749 const struct got_error *
2750 got_privsep_send_raw_delta_outfd(struct imsgbuf *ibuf, int fd)
2752 return send_fd(ibuf, GOT_IMSG_RAW_DELTA_OUTFD, fd);
2755 const struct got_error *
2756 got_privsep_send_raw_delta(struct imsgbuf *ibuf, uint64_t base_size,
2757 uint64_t result_size, off_t delta_size, off_t delta_compressed_size,
2758 off_t delta_offset, off_t delta_out_offset, struct got_object_id *base_id)
2760 struct got_imsg_raw_delta idelta;
2763 idelta.base_size = base_size;
2764 idelta.result_size = result_size;
2765 idelta.delta_size = delta_size;
2766 idelta.delta_compressed_size = delta_compressed_size;
2767 idelta.delta_offset = delta_offset;
2768 idelta.delta_out_offset = delta_out_offset;
2769 memcpy(idelta.base_id, base_id->sha1, SHA1_DIGEST_LENGTH);
2771 ret = imsg_compose(ibuf, GOT_IMSG_RAW_DELTA, 0, 0, -1,
2772 &idelta, sizeof(idelta));
2774 return got_error_from_errno("imsg_compose RAW_DELTA");
2776 return flush_imsg(ibuf);
2779 const struct got_error *
2780 got_privsep_recv_raw_delta(uint64_t *base_size, uint64_t *result_size,
2781 off_t *delta_size, off_t *delta_compressed_size, off_t *delta_offset,
2782 off_t *delta_out_offset, struct got_object_id **base_id, struct imsgbuf *ibuf)
2784 const struct got_error *err = NULL;
2786 struct got_imsg_raw_delta *delta;
2792 *delta_compressed_size = 0;
2794 *delta_out_offset = 0;
2797 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2801 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2803 switch (imsg.hdr.type) {
2804 case GOT_IMSG_RAW_DELTA:
2805 if (datalen != sizeof(*delta)) {
2806 err = got_error(GOT_ERR_PRIVSEP_LEN);
2810 *base_size = delta->base_size;
2811 *result_size = delta->result_size;
2812 *delta_size = delta->delta_size;
2813 *delta_compressed_size = delta->delta_compressed_size;
2814 *delta_offset = delta->delta_offset;
2815 *delta_out_offset = delta->delta_out_offset;
2816 *base_id = calloc(1, sizeof(**base_id));
2817 if (*base_id == NULL) {
2818 err = got_error_from_errno("malloc");
2821 memcpy((*base_id)->sha1, delta->base_id, SHA1_DIGEST_LENGTH);
2824 err = got_error(GOT_ERR_PRIVSEP_MSG);
2837 const struct got_error *
2838 got_privsep_send_object_idlist(struct imsgbuf *ibuf,
2839 struct got_object_id **ids, size_t nids)
2841 const struct got_error *err = NULL;
2842 struct got_imsg_object_idlist idlist;
2846 if (nids > GOT_IMSG_OBJ_ID_LIST_MAX_NIDS)
2847 return got_error(GOT_ERR_NO_SPACE);
2849 wbuf = imsg_create(ibuf, GOT_IMSG_OBJ_ID_LIST, 0, 0,
2850 sizeof(idlist) + nids * sizeof(**ids));
2852 err = got_error_from_errno("imsg_create OBJ_ID_LIST");
2857 if (imsg_add(wbuf, &idlist, sizeof(idlist)) == -1)
2858 return got_error_from_errno("imsg_add OBJ_ID_LIST");
2860 for (i = 0; i < nids; i++) {
2861 struct got_object_id *id = ids[i];
2862 if (imsg_add(wbuf, id, sizeof(*id)) == -1)
2863 return got_error_from_errno("imsg_add OBJ_ID_LIST");
2867 imsg_close(ibuf, wbuf);
2869 return flush_imsg(ibuf);
2872 const struct got_error *
2873 got_privsep_send_object_idlist_done(struct imsgbuf *ibuf)
2875 if (imsg_compose(ibuf, GOT_IMSG_OBJ_ID_LIST_DONE, 0, 0, -1, NULL, 0)
2877 return got_error_from_errno("imsg_compose OBJ_ID_LIST_DONE");
2879 return flush_imsg(ibuf);
2882 const struct got_error *
2883 got_privsep_recv_object_idlist(int *done, struct got_object_id **ids,
2884 size_t *nids, struct imsgbuf *ibuf)
2886 const struct got_error *err = NULL;
2888 struct got_imsg_object_idlist *idlist;
2895 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2899 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2900 switch (imsg.hdr.type) {
2901 case GOT_IMSG_OBJ_ID_LIST:
2902 if (datalen < sizeof(*idlist)) {
2903 err = got_error(GOT_ERR_PRIVSEP_LEN);
2907 if (idlist->nids > GOT_IMSG_OBJ_ID_LIST_MAX_NIDS) {
2908 err = got_error(GOT_ERR_PRIVSEP_LEN);
2911 *nids = idlist->nids;
2912 *ids = calloc(*nids, sizeof(**ids));
2914 err = got_error_from_errno("calloc");
2917 memcpy(*ids, (uint8_t *)imsg.data + sizeof(idlist),
2918 *nids * sizeof(**ids));
2920 case GOT_IMSG_OBJ_ID_LIST_DONE:
2924 err = got_error(GOT_ERR_PRIVSEP_MSG);
2933 const struct got_error *
2934 got_privsep_send_delta_reuse_req(struct imsgbuf *ibuf)
2936 if (imsg_compose(ibuf, GOT_IMSG_DELTA_REUSE_REQUEST, 0, 0, -1, NULL, 0)
2938 return got_error_from_errno("imsg_compose DELTA_REUSE_REQUEST");
2940 return flush_imsg(ibuf);
2943 const struct got_error *
2944 got_privsep_send_reused_deltas(struct imsgbuf *ibuf,
2945 struct got_imsg_reused_delta *deltas, size_t ndeltas)
2947 const struct got_error *err = NULL;
2949 struct got_imsg_reused_deltas ideltas;
2952 if (ndeltas > GOT_IMSG_REUSED_DELTAS_MAX_NDELTAS)
2953 return got_error(GOT_ERR_NO_SPACE);
2955 wbuf = imsg_create(ibuf, GOT_IMSG_REUSED_DELTAS, 0, 0,
2956 sizeof(ideltas) + ndeltas * sizeof(*deltas));
2958 err = got_error_from_errno("imsg_create REUSED_DELTAS");
2962 ideltas.ndeltas = ndeltas;
2963 if (imsg_add(wbuf, &ideltas, sizeof(ideltas)) == -1)
2964 return got_error_from_errno("imsg_add REUSED_DELTAS");
2966 for (i = 0; i < ndeltas; i++) {
2967 struct got_imsg_reused_delta *delta = &deltas[i];
2968 if (imsg_add(wbuf, delta, sizeof(*delta)) == -1)
2969 return got_error_from_errno("imsg_add REUSED_DELTAS");
2973 imsg_close(ibuf, wbuf);
2975 return flush_imsg(ibuf);
2978 const struct got_error *
2979 got_privsep_send_reused_deltas_done(struct imsgbuf *ibuf)
2981 if (imsg_compose(ibuf, GOT_IMSG_DELTA_REUSE_DONE, 0, 0, -1, NULL, 0)
2983 return got_error_from_errno("imsg_compose DELTA_REUSE_DONE");
2985 return flush_imsg(ibuf);
2988 const struct got_error *
2989 got_privsep_recv_reused_deltas(int *done, struct got_imsg_reused_delta *deltas,
2990 size_t *ndeltas, struct imsgbuf *ibuf)
2992 const struct got_error *err = NULL;
2994 struct got_imsg_reused_deltas *ideltas;
3000 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
3004 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
3005 switch (imsg.hdr.type) {
3006 case GOT_IMSG_REUSED_DELTAS:
3007 if (datalen < sizeof(*ideltas)) {
3008 err = got_error(GOT_ERR_PRIVSEP_LEN);
3011 ideltas = imsg.data;
3012 if (ideltas->ndeltas > GOT_IMSG_OBJ_ID_LIST_MAX_NIDS) {
3013 err = got_error(GOT_ERR_PRIVSEP_LEN);
3016 *ndeltas = ideltas->ndeltas;
3017 memcpy(deltas, (uint8_t *)imsg.data + sizeof(ideltas),
3018 *ndeltas * sizeof(*deltas));
3020 case GOT_IMSG_DELTA_REUSE_DONE:
3024 err = got_error(GOT_ERR_PRIVSEP_MSG);
3033 const struct got_error *
3034 got_privsep_unveil_exec_helpers(void)
3036 const char *helpers[] = {
3037 GOT_PATH_PROG_READ_PACK,
3038 GOT_PATH_PROG_READ_OBJECT,
3039 GOT_PATH_PROG_READ_COMMIT,
3040 GOT_PATH_PROG_READ_TREE,
3041 GOT_PATH_PROG_READ_BLOB,
3042 GOT_PATH_PROG_READ_TAG,
3043 GOT_PATH_PROG_READ_GITCONFIG,
3044 GOT_PATH_PROG_READ_GOTCONFIG,
3045 GOT_PATH_PROG_READ_PATCH,
3046 GOT_PATH_PROG_FETCH_PACK,
3047 GOT_PATH_PROG_INDEX_PACK,
3048 GOT_PATH_PROG_SEND_PACK,
3052 for (i = 0; i < nitems(helpers); i++) {
3053 if (unveil(helpers[i], "x") == 0)
3055 return got_error_from_errno2("unveil", helpers[i]);
3062 got_privsep_exec_child(int imsg_fds[2], const char *path, const char *repo_path)
3064 if (close(imsg_fds[0]) == -1) {
3065 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
3069 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
3070 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
3074 closefrom(GOT_IMSG_FD_CHILD + 1);
3076 if (execl(path, path, repo_path, (char *)NULL) == -1) {
3077 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,