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_batch(struct imsgbuf *ibuf,
1447 struct got_parsed_tree_entry *entries, 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 static const struct got_error *
1483 send_tree_entries(struct imsgbuf *ibuf, struct got_parsed_tree_entry *entries,
1486 const struct got_error *err = NULL;
1488 size_t entries_len = sizeof(struct got_imsg_tree_entries);
1491 for (j = 0; j < nentries; j++) {
1492 struct got_parsed_tree_entry *pte = &entries[j];
1493 size_t len = sizeof(*pte) + pte->namelen;
1496 entries_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1497 err = send_tree_entries_batch(ibuf, entries,
1498 i, j - 1, entries_len);
1502 entries_len = sizeof(struct got_imsg_tree_entries);
1509 err = send_tree_entries_batch(ibuf, entries, i, j - 1,
1518 const struct got_error *
1519 got_privsep_send_tree(struct imsgbuf *ibuf,
1520 struct got_parsed_tree_entry *entries, int nentries)
1522 const struct got_error *err = NULL;
1523 struct got_imsg_tree_object itree;
1525 itree.nentries = nentries;
1526 if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
1528 return got_error_from_errno("imsg_compose TREE");
1530 err = send_tree_entries(ibuf, entries, nentries);
1534 return flush_imsg(ibuf);
1538 static const struct got_error *
1539 recv_tree_entries(void *data, size_t datalen, struct got_tree_object *tree,
1542 const struct got_error *err = NULL;
1543 struct got_imsg_tree_entries *ientries;
1544 struct got_tree_entry *te;
1548 if (datalen <= sizeof(*ientries) ||
1549 datalen > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
1550 return got_error(GOT_ERR_PRIVSEP_LEN);
1552 ientries = (struct got_imsg_tree_entries *)data;
1553 if (ientries->nentries > INT_MAX) {
1554 return got_error_msg(GOT_ERR_NO_SPACE,
1555 "too many tree entries");
1558 te_offset = sizeof(*ientries);
1559 for (i = 0; i < ientries->nentries; i++) {
1560 struct got_imsg_tree_entry ite;
1561 const char *te_name;
1562 uint8_t *buf = (uint8_t *)data + te_offset;
1564 if (te_offset >= datalen) {
1565 err = got_error(GOT_ERR_PRIVSEP_LEN);
1569 /* Might not be aligned, size is ~32 bytes. */
1570 memcpy(&ite, buf, sizeof(ite));
1572 if (ite.namelen >= sizeof(te->name)) {
1573 err = got_error(GOT_ERR_PRIVSEP_LEN);
1576 if (te_offset + sizeof(ite) + ite.namelen > datalen) {
1577 err = got_error(GOT_ERR_PRIVSEP_LEN);
1581 if (*nentries >= tree->nentries) {
1582 err = got_error(GOT_ERR_PRIVSEP_LEN);
1585 te = &tree->entries[*nentries];
1586 te_name = buf + sizeof(ite);
1587 memcpy(te->name, te_name, ite.namelen);
1588 te->name[ite.namelen] = '\0';
1589 memcpy(te->id.sha1, ite.id, SHA1_DIGEST_LENGTH);
1590 te->mode = ite.mode;
1591 te->idx = *nentries;
1594 te_offset += sizeof(ite) + ite.namelen;
1600 const struct got_error *
1601 got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
1603 const struct got_error *err = NULL;
1604 const size_t min_datalen =
1605 MIN(sizeof(struct got_imsg_error),
1606 sizeof(struct got_imsg_tree_object));
1607 struct got_imsg_tree_object *itree;
1612 err = read_imsg(ibuf);
1621 n = imsg_get(ibuf, &imsg);
1624 if (nentries < (*tree)->nentries) {
1625 err = read_imsg(ibuf);
1632 err = got_error(GOT_ERR_PRIVSEP_MSG);
1637 if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen) {
1639 err = got_error(GOT_ERR_PRIVSEP_LEN);
1643 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1645 switch (imsg.hdr.type) {
1646 case GOT_IMSG_ERROR:
1647 err = recv_imsg_error(&imsg, datalen);
1650 /* This message should only appear once. */
1651 if (*tree != NULL) {
1652 err = got_error(GOT_ERR_PRIVSEP_MSG);
1655 if (datalen != sizeof(*itree)) {
1656 err = got_error(GOT_ERR_PRIVSEP_LEN);
1660 if (itree->nentries < 0) {
1661 err = got_error(GOT_ERR_PRIVSEP_LEN);
1664 *tree = malloc(sizeof(**tree));
1665 if (*tree == NULL) {
1666 err = got_error_from_errno("malloc");
1669 (*tree)->entries = calloc(itree->nentries,
1670 sizeof(struct got_tree_entry));
1671 if ((*tree)->entries == NULL) {
1672 err = got_error_from_errno("malloc");
1677 (*tree)->nentries = itree->nentries;
1678 (*tree)->refcnt = 0;
1680 case GOT_IMSG_TREE_ENTRIES:
1681 /* This message should be preceeded by GOT_IMSG_TREE. */
1682 if (*tree == NULL) {
1683 err = got_error(GOT_ERR_PRIVSEP_MSG);
1686 err = recv_tree_entries(imsg.data, datalen,
1690 err = got_error(GOT_ERR_PRIVSEP_MSG);
1699 if (*tree && (*tree)->nentries != nentries) {
1701 err = got_error(GOT_ERR_PRIVSEP_LEN);
1702 got_object_tree_close(*tree);
1709 const struct got_error *
1710 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
1711 const uint8_t *data)
1713 struct got_imsg_blob iblob;
1716 iblob.hdrlen = hdrlen;
1721 if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
1722 return got_error(GOT_ERR_NO_SPACE);
1724 buf = malloc(sizeof(iblob) + size);
1726 return got_error_from_errno("malloc");
1728 memcpy(buf, &iblob, sizeof(iblob));
1729 memcpy(buf + sizeof(iblob), data, size);
1730 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
1731 sizeof(iblob) + size) == -1) {
1733 return got_error_from_errno("imsg_compose BLOB");
1737 /* Data has already been written to file descriptor. */
1738 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
1739 sizeof(iblob)) == -1)
1740 return got_error_from_errno("imsg_compose BLOB");
1744 return flush_imsg(ibuf);
1747 const struct got_error *
1748 got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1749 struct imsgbuf *ibuf)
1751 const struct got_error *err = NULL;
1753 struct got_imsg_blob *iblob;
1758 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1762 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1764 switch (imsg.hdr.type) {
1766 if (datalen < sizeof(*iblob)) {
1767 err = got_error(GOT_ERR_PRIVSEP_LEN);
1771 *size = iblob->size;
1772 *hdrlen = iblob->hdrlen;
1774 if (datalen == sizeof(*iblob)) {
1775 /* Data has been written to file descriptor. */
1779 if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX) {
1780 err = got_error(GOT_ERR_PRIVSEP_LEN);
1784 *outbuf = malloc(*size);
1785 if (*outbuf == NULL) {
1786 err = got_error_from_errno("malloc");
1789 memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
1792 err = got_error(GOT_ERR_PRIVSEP_MSG);
1801 static const struct got_error *
1802 send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
1804 const struct got_error *err = NULL;
1805 size_t offset, remain;
1808 remain = tagmsg_len;
1809 while (remain > 0) {
1810 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1812 if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
1813 tag->tagmsg + offset, n) == -1) {
1814 err = got_error_from_errno("imsg_compose TAG_TAGMSG");
1818 err = flush_imsg(ibuf);
1829 const struct got_error *
1830 got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
1832 const struct got_error *err = NULL;
1833 struct got_imsg_tag_object *itag;
1836 size_t tag_len = strlen(tag->tag);
1837 size_t tagger_len = strlen(tag->tagger);
1838 size_t tagmsg_len = strlen(tag->tagmsg);
1840 total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
1842 buf = malloc(total);
1844 return got_error_from_errno("malloc");
1846 itag = (struct got_imsg_tag_object *)buf;
1847 memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
1848 itag->obj_type = tag->obj_type;
1849 itag->tag_len = tag_len;
1850 itag->tagger_len = tagger_len;
1851 itag->tagger_time = tag->tagger_time;
1852 itag->tagger_gmtoff = tag->tagger_gmtoff;
1853 itag->tagmsg_len = tagmsg_len;
1855 len = sizeof(*itag);
1856 memcpy(buf + len, tag->tag, tag_len);
1858 memcpy(buf + len, tag->tagger, tagger_len);
1861 if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
1862 err = got_error_from_errno("imsg_compose TAG");
1866 if (tagmsg_len == 0 ||
1867 tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1868 err = flush_imsg(ibuf);
1872 err = send_tagmsg(ibuf, tag, tagmsg_len);
1878 const struct got_error *
1879 got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
1881 const struct got_error *err = NULL;
1883 struct got_imsg_tag_object *itag;
1884 size_t len, datalen;
1885 const size_t min_datalen =
1886 MIN(sizeof(struct got_imsg_error),
1887 sizeof(struct got_imsg_tag_object));
1891 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1895 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1898 switch (imsg.hdr.type) {
1900 if (datalen < sizeof(*itag)) {
1901 err = got_error(GOT_ERR_PRIVSEP_LEN);
1905 if (datalen != sizeof(*itag) + itag->tag_len +
1907 err = got_error(GOT_ERR_PRIVSEP_LEN);
1910 len += sizeof(*itag);
1912 *tag = calloc(1, sizeof(**tag));
1914 err = got_error_from_errno("calloc");
1918 memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
1920 if (itag->tag_len == 0) {
1921 (*tag)->tag = strdup("");
1922 if ((*tag)->tag == NULL) {
1923 err = got_error_from_errno("strdup");
1927 (*tag)->tag = malloc(itag->tag_len + 1);
1928 if ((*tag)->tag == NULL) {
1929 err = got_error_from_errno("malloc");
1932 memcpy((*tag)->tag, imsg.data + len,
1934 (*tag)->tag[itag->tag_len] = '\0';
1936 len += itag->tag_len;
1938 (*tag)->obj_type = itag->obj_type;
1939 (*tag)->tagger_time = itag->tagger_time;
1940 (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1942 if (itag->tagger_len == 0) {
1943 (*tag)->tagger = strdup("");
1944 if ((*tag)->tagger == NULL) {
1945 err = got_error_from_errno("strdup");
1949 (*tag)->tagger = malloc(itag->tagger_len + 1);
1950 if ((*tag)->tagger == NULL) {
1951 err = got_error_from_errno("malloc");
1954 memcpy((*tag)->tagger, imsg.data + len,
1956 (*tag)->tagger[itag->tagger_len] = '\0';
1958 len += itag->tagger_len;
1960 if (itag->tagmsg_len == 0) {
1961 (*tag)->tagmsg = strdup("");
1962 if ((*tag)->tagmsg == NULL) {
1963 err = got_error_from_errno("strdup");
1967 size_t offset = 0, remain = itag->tagmsg_len;
1969 (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1970 if ((*tag)->tagmsg == NULL) {
1971 err = got_error_from_errno("malloc");
1974 while (remain > 0) {
1975 struct imsg imsg_log;
1976 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1979 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1983 if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1984 return got_error(GOT_ERR_PRIVSEP_MSG);
1986 memcpy((*tag)->tagmsg + offset, imsg_log.data,
1988 imsg_free(&imsg_log);
1992 (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1997 err = got_error(GOT_ERR_PRIVSEP_MSG);
2006 const struct got_error *
2007 got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
2008 struct got_packidx *packidx)
2010 const struct got_error *err = NULL;
2011 struct got_imsg_packidx ipackidx;
2012 struct got_imsg_pack ipack;
2015 ipackidx.len = packidx->len;
2016 ipackidx.packfile_size = pack->filesize;
2017 fd = dup(packidx->fd);
2019 return got_error_from_errno("dup");
2021 if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
2022 sizeof(ipackidx)) == -1) {
2023 err = got_error_from_errno("imsg_compose PACKIDX");
2028 if (strlcpy(ipack.path_packfile, pack->path_packfile,
2029 sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
2030 return got_error(GOT_ERR_NO_SPACE);
2031 ipack.filesize = pack->filesize;
2035 return got_error_from_errno("dup");
2037 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
2039 err = got_error_from_errno("imsg_compose PACK");
2044 return flush_imsg(ibuf);
2047 const struct got_error *
2048 got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
2049 struct got_object_id *id)
2051 struct got_imsg_packed_object iobj;
2054 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
2056 if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
2057 &iobj, sizeof(iobj)) == -1)
2058 return got_error_from_errno("imsg_compose "
2059 "PACKED_OBJECT_REQUEST");
2061 return flush_imsg(ibuf);
2064 const struct got_error *
2065 got_privsep_send_packed_raw_obj_req(struct imsgbuf *ibuf, int idx,
2066 struct got_object_id *id)
2068 struct got_imsg_packed_object iobj;
2071 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
2073 if (imsg_compose(ibuf, GOT_IMSG_PACKED_RAW_OBJECT_REQUEST, 0, 0, -1,
2074 &iobj, sizeof(iobj)) == -1)
2075 return got_error_from_errno("imsg_compose "
2076 "PACKED_OBJECT_REQUEST");
2078 return flush_imsg(ibuf);
2081 const struct got_error *
2082 got_privsep_send_gitconfig_parse_req(struct imsgbuf *ibuf, int fd)
2084 const struct got_error *err = NULL;
2086 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_PARSE_REQUEST, 0, 0, fd,
2088 err = got_error_from_errno("imsg_compose "
2089 "GITCONFIG_PARSE_REQUEST");
2094 return flush_imsg(ibuf);
2097 const struct got_error *
2098 got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *ibuf)
2100 if (imsg_compose(ibuf,
2101 GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST, 0, 0, -1,
2103 return got_error_from_errno("imsg_compose "
2104 "GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST");
2106 return flush_imsg(ibuf);
2109 const struct got_error *
2110 got_privsep_send_gitconfig_repository_extensions_req(struct imsgbuf *ibuf)
2112 if (imsg_compose(ibuf,
2113 GOT_IMSG_GITCONFIG_REPOSITORY_EXTENSIONS_REQUEST, 0, 0, -1,
2115 return got_error_from_errno("imsg_compose "
2116 "GITCONFIG_REPOSITORY_EXTENSIONS_REQUEST");
2118 return flush_imsg(ibuf);
2122 const struct got_error *
2123 got_privsep_send_gitconfig_author_name_req(struct imsgbuf *ibuf)
2125 if (imsg_compose(ibuf,
2126 GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST, 0, 0, -1, NULL, 0) == -1)
2127 return got_error_from_errno("imsg_compose "
2128 "GITCONFIG_AUTHOR_NAME_REQUEST");
2130 return flush_imsg(ibuf);
2133 const struct got_error *
2134 got_privsep_send_gitconfig_author_email_req(struct imsgbuf *ibuf)
2136 if (imsg_compose(ibuf,
2137 GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST, 0, 0, -1, NULL, 0) == -1)
2138 return got_error_from_errno("imsg_compose "
2139 "GITCONFIG_AUTHOR_EMAIL_REQUEST");
2141 return flush_imsg(ibuf);
2144 const struct got_error *
2145 got_privsep_send_gitconfig_remotes_req(struct imsgbuf *ibuf)
2147 if (imsg_compose(ibuf,
2148 GOT_IMSG_GITCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
2149 return got_error_from_errno("imsg_compose "
2150 "GITCONFIG_REMOTE_REQUEST");
2152 return flush_imsg(ibuf);
2155 const struct got_error *
2156 got_privsep_send_gitconfig_owner_req(struct imsgbuf *ibuf)
2158 if (imsg_compose(ibuf,
2159 GOT_IMSG_GITCONFIG_OWNER_REQUEST, 0, 0, -1, NULL, 0) == -1)
2160 return got_error_from_errno("imsg_compose "
2161 "GITCONFIG_OWNER_REQUEST");
2163 return flush_imsg(ibuf);
2166 const struct got_error *
2167 got_privsep_recv_gitconfig_str(char **str, struct imsgbuf *ibuf)
2169 const struct got_error *err = NULL;
2172 const size_t min_datalen = 0;
2176 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2179 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2181 switch (imsg.hdr.type) {
2182 case GOT_IMSG_GITCONFIG_STR_VAL:
2185 /* datalen does not include terminating \0 */
2186 *str = malloc(datalen + 1);
2188 err = got_error_from_errno("malloc");
2191 memcpy(*str, imsg.data, datalen);
2192 (*str)[datalen] = '\0';
2195 err = got_error(GOT_ERR_PRIVSEP_MSG);
2203 const struct got_error *
2204 got_privsep_recv_gitconfig_int(int *val, struct imsgbuf *ibuf)
2206 const struct got_error *err = NULL;
2209 const size_t min_datalen =
2210 MIN(sizeof(struct got_imsg_error), sizeof(int));
2214 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2217 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2219 switch (imsg.hdr.type) {
2220 case GOT_IMSG_GITCONFIG_INT_VAL:
2221 if (datalen != sizeof(*val)) {
2222 err = got_error(GOT_ERR_PRIVSEP_LEN);
2225 memcpy(val, imsg.data, sizeof(*val));
2228 err = got_error(GOT_ERR_PRIVSEP_MSG);
2237 free_remote_data(struct got_remote_repo *remote)
2242 free(remote->fetch_url);
2243 free(remote->send_url);
2244 for (i = 0; i < remote->nfetch_branches; i++)
2245 free(remote->fetch_branches[i]);
2246 free(remote->fetch_branches);
2247 for (i = 0; i < remote->nsend_branches; i++)
2248 free(remote->send_branches[i]);
2249 free(remote->send_branches);
2250 for (i = 0; i < remote->nfetch_refs; i++)
2251 free(remote->fetch_refs[i]);
2252 free(remote->fetch_refs);
2255 const struct got_error *
2256 got_privsep_recv_gitconfig_remotes(struct got_remote_repo **remotes,
2257 int *nremotes, struct imsgbuf *ibuf)
2259 const struct got_error *err = NULL;
2262 struct got_imsg_remotes iremotes;
2263 struct got_imsg_remote iremote;
2267 iremotes.nremotes = 0;
2269 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremotes));
2272 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2274 switch (imsg.hdr.type) {
2275 case GOT_IMSG_GITCONFIG_REMOTES:
2276 if (datalen != sizeof(iremotes)) {
2277 err = got_error(GOT_ERR_PRIVSEP_LEN);
2280 memcpy(&iremotes, imsg.data, sizeof(iremotes));
2281 if (iremotes.nremotes == 0) {
2288 return got_error(GOT_ERR_PRIVSEP_MSG);
2293 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(**remotes));
2294 if (*remotes == NULL)
2295 return got_error_from_errno("recallocarray");
2297 while (*nremotes < iremotes.nremotes) {
2298 struct got_remote_repo *remote;
2300 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremote));
2303 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2305 switch (imsg.hdr.type) {
2306 case GOT_IMSG_GITCONFIG_REMOTE:
2307 remote = &(*remotes)[*nremotes];
2308 memset(remote, 0, sizeof(*remote));
2309 if (datalen < sizeof(iremote)) {
2310 err = got_error(GOT_ERR_PRIVSEP_LEN);
2313 memcpy(&iremote, imsg.data, sizeof(iremote));
2314 if (iremote.name_len == 0 ||
2315 iremote.fetch_url_len == 0 ||
2316 iremote.send_url_len == 0 ||
2317 (sizeof(iremote) + iremote.name_len +
2318 iremote.fetch_url_len + iremote.send_url_len) > datalen) {
2319 err = got_error(GOT_ERR_PRIVSEP_LEN);
2322 remote->name = strndup(imsg.data + sizeof(iremote),
2324 if (remote->name == NULL) {
2325 err = got_error_from_errno("strndup");
2328 remote->fetch_url = strndup(imsg.data + sizeof(iremote) +
2329 iremote.name_len, iremote.fetch_url_len);
2330 if (remote->fetch_url == NULL) {
2331 err = got_error_from_errno("strndup");
2332 free_remote_data(remote);
2335 remote->send_url = strndup(imsg.data + sizeof(iremote) +
2336 iremote.name_len + iremote.fetch_url_len,
2337 iremote.send_url_len);
2338 if (remote->send_url == NULL) {
2339 err = got_error_from_errno("strndup");
2340 free_remote_data(remote);
2343 remote->mirror_references = iremote.mirror_references;
2344 remote->fetch_all_branches = iremote.fetch_all_branches;
2345 remote->nfetch_branches = 0;
2346 remote->fetch_branches = NULL;
2347 remote->nsend_branches = 0;
2348 remote->send_branches = NULL;
2349 remote->nfetch_refs = 0;
2350 remote->fetch_refs = NULL;
2354 err = got_error(GOT_ERR_PRIVSEP_MSG);
2365 for (i = 0; i < *nremotes; i++)
2366 free_remote_data(&(*remotes)[i]);
2374 const struct got_error *
2375 got_privsep_send_gotconfig_parse_req(struct imsgbuf *ibuf, int fd)
2377 const struct got_error *err = NULL;
2379 if (imsg_compose(ibuf, GOT_IMSG_GOTCONFIG_PARSE_REQUEST, 0, 0, fd,
2381 err = got_error_from_errno("imsg_compose "
2382 "GOTCONFIG_PARSE_REQUEST");
2387 return flush_imsg(ibuf);
2390 const struct got_error *
2391 got_privsep_send_gotconfig_author_req(struct imsgbuf *ibuf)
2393 if (imsg_compose(ibuf,
2394 GOT_IMSG_GOTCONFIG_AUTHOR_REQUEST, 0, 0, -1, NULL, 0) == -1)
2395 return got_error_from_errno("imsg_compose "
2396 "GOTCONFIG_AUTHOR_REQUEST");
2398 return flush_imsg(ibuf);
2401 const struct got_error *
2402 got_privsep_send_gotconfig_remotes_req(struct imsgbuf *ibuf)
2404 if (imsg_compose(ibuf,
2405 GOT_IMSG_GOTCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
2406 return got_error_from_errno("imsg_compose "
2407 "GOTCONFIG_REMOTE_REQUEST");
2409 return flush_imsg(ibuf);
2412 const struct got_error *
2413 got_privsep_recv_gotconfig_str(char **str, struct imsgbuf *ibuf)
2415 const struct got_error *err = NULL;
2418 const size_t min_datalen = 0;
2422 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2425 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2427 switch (imsg.hdr.type) {
2428 case GOT_IMSG_ERROR:
2429 if (datalen < sizeof(struct got_imsg_error)) {
2430 err = got_error(GOT_ERR_PRIVSEP_LEN);
2433 err = recv_imsg_error(&imsg, datalen);
2435 case GOT_IMSG_GOTCONFIG_STR_VAL:
2438 /* datalen does not include terminating \0 */
2439 *str = malloc(datalen + 1);
2441 err = got_error_from_errno("malloc");
2444 memcpy(*str, imsg.data, datalen);
2445 (*str)[datalen] = '\0';
2448 err = got_error(GOT_ERR_PRIVSEP_MSG);
2456 const struct got_error *
2457 got_privsep_recv_gotconfig_remotes(struct got_remote_repo **remotes,
2458 int *nremotes, struct imsgbuf *ibuf)
2460 const struct got_error *err = NULL;
2463 struct got_imsg_remotes iremotes;
2464 struct got_imsg_remote iremote;
2465 const size_t min_datalen =
2466 MIN(sizeof(struct got_imsg_error), sizeof(iremotes));
2470 iremotes.nremotes = 0;
2472 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2475 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2477 switch (imsg.hdr.type) {
2478 case GOT_IMSG_ERROR:
2479 if (datalen < sizeof(struct got_imsg_error)) {
2480 err = got_error(GOT_ERR_PRIVSEP_LEN);
2483 err = recv_imsg_error(&imsg, datalen);
2485 case GOT_IMSG_GOTCONFIG_REMOTES:
2486 if (datalen != sizeof(iremotes)) {
2487 err = got_error(GOT_ERR_PRIVSEP_LEN);
2490 memcpy(&iremotes, imsg.data, sizeof(iremotes));
2491 if (iremotes.nremotes == 0) {
2498 return got_error(GOT_ERR_PRIVSEP_MSG);
2503 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(**remotes));
2504 if (*remotes == NULL)
2505 return got_error_from_errno("recallocarray");
2507 while (*nremotes < iremotes.nremotes) {
2508 struct got_remote_repo *remote;
2509 const size_t min_datalen =
2510 MIN(sizeof(struct got_imsg_error), sizeof(iremote));
2513 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2516 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2518 switch (imsg.hdr.type) {
2519 case GOT_IMSG_ERROR:
2520 if (datalen < sizeof(struct got_imsg_error)) {
2521 err = got_error(GOT_ERR_PRIVSEP_LEN);
2524 err = recv_imsg_error(&imsg, datalen);
2526 case GOT_IMSG_GOTCONFIG_REMOTE:
2527 remote = &(*remotes)[*nremotes];
2528 memset(remote, 0, sizeof(*remote));
2529 if (datalen < sizeof(iremote)) {
2530 err = got_error(GOT_ERR_PRIVSEP_LEN);
2533 memcpy(&iremote, imsg.data, sizeof(iremote));
2534 if (iremote.name_len == 0 ||
2535 (iremote.fetch_url_len == 0 &&
2536 iremote.send_url_len == 0) ||
2537 (sizeof(iremote) + iremote.name_len +
2538 iremote.fetch_url_len + iremote.send_url_len) >
2540 err = got_error(GOT_ERR_PRIVSEP_LEN);
2543 remote->name = strndup(imsg.data + sizeof(iremote),
2545 if (remote->name == NULL) {
2546 err = got_error_from_errno("strndup");
2549 remote->fetch_url = strndup(imsg.data +
2550 sizeof(iremote) + iremote.name_len,
2551 iremote.fetch_url_len);
2552 if (remote->fetch_url == NULL) {
2553 err = got_error_from_errno("strndup");
2554 free_remote_data(remote);
2557 remote->send_url = strndup(imsg.data +
2558 sizeof(iremote) + iremote.name_len +
2559 iremote.fetch_url_len, iremote.send_url_len);
2560 if (remote->send_url == NULL) {
2561 err = got_error_from_errno("strndup");
2562 free_remote_data(remote);
2565 remote->mirror_references = iremote.mirror_references;
2566 remote->fetch_all_branches = iremote.fetch_all_branches;
2567 if (iremote.nfetch_branches > 0) {
2568 remote->fetch_branches = recallocarray(NULL, 0,
2569 iremote.nfetch_branches, sizeof(char *));
2570 if (remote->fetch_branches == NULL) {
2571 err = got_error_from_errno("calloc");
2572 free_remote_data(remote);
2576 remote->nfetch_branches = 0;
2577 for (i = 0; i < iremote.nfetch_branches; i++) {
2579 err = got_privsep_recv_gotconfig_str(&branch,
2582 free_remote_data(remote);
2585 remote->fetch_branches[i] = branch;
2586 remote->nfetch_branches++;
2588 if (iremote.nsend_branches > 0) {
2589 remote->send_branches = recallocarray(NULL, 0,
2590 iremote.nsend_branches, sizeof(char *));
2591 if (remote->send_branches == NULL) {
2592 err = got_error_from_errno("calloc");
2593 free_remote_data(remote);
2597 remote->nsend_branches = 0;
2598 for (i = 0; i < iremote.nsend_branches; i++) {
2600 err = got_privsep_recv_gotconfig_str(&branch,
2603 free_remote_data(remote);
2606 remote->send_branches[i] = branch;
2607 remote->nsend_branches++;
2609 if (iremote.nfetch_refs > 0) {
2610 remote->fetch_refs = recallocarray(NULL, 0,
2611 iremote.nfetch_refs, sizeof(char *));
2612 if (remote->fetch_refs == NULL) {
2613 err = got_error_from_errno("calloc");
2614 free_remote_data(remote);
2618 remote->nfetch_refs = 0;
2619 for (i = 0; i < iremote.nfetch_refs; i++) {
2621 err = got_privsep_recv_gotconfig_str(&ref,
2624 free_remote_data(remote);
2627 remote->fetch_refs[i] = ref;
2628 remote->nfetch_refs++;
2633 err = got_error(GOT_ERR_PRIVSEP_MSG);
2644 for (i = 0; i < *nremotes; i++)
2645 free_remote_data(&(*remotes)[i]);
2653 const struct got_error *
2654 got_privsep_send_commit_traversal_request(struct imsgbuf *ibuf,
2655 struct got_object_id *id, int idx, const char *path)
2658 size_t path_len = strlen(path) + 1;
2660 wbuf = imsg_create(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_REQUEST, 0, 0,
2661 sizeof(struct got_imsg_commit_traversal_request) + path_len);
2663 return got_error_from_errno(
2664 "imsg_create COMMIT_TRAVERSAL_REQUEST");
2665 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1)
2666 return got_error_from_errno("imsg_add "
2667 "COMMIT_TRAVERSAL_REQUEST");
2668 if (imsg_add(wbuf, &idx, sizeof(idx)) == -1)
2669 return got_error_from_errno("imsg_add "
2670 "COMMIT_TRAVERSAL_REQUEST");
2671 if (imsg_add(wbuf, path, path_len) == -1)
2672 return got_error_from_errno("imsg_add "
2673 "COMMIT_TRAVERSAL_REQUEST");
2676 imsg_close(ibuf, wbuf);
2678 return flush_imsg(ibuf);
2681 const struct got_error *
2682 got_privsep_recv_traversed_commits(struct got_commit_object **changed_commit,
2683 struct got_object_id **changed_commit_id,
2684 struct got_object_id_queue *commit_ids, struct imsgbuf *ibuf)
2686 const struct got_error *err = NULL;
2688 struct got_imsg_traversed_commits *icommits;
2692 *changed_commit = NULL;
2693 *changed_commit_id = NULL;
2696 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2700 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2701 switch (imsg.hdr.type) {
2702 case GOT_IMSG_TRAVERSED_COMMITS:
2703 icommits = imsg.data;
2704 if (datalen != sizeof(*icommits) +
2705 icommits->ncommits * SHA1_DIGEST_LENGTH) {
2706 err = got_error(GOT_ERR_PRIVSEP_LEN);
2709 for (i = 0; i < icommits->ncommits; i++) {
2710 struct got_object_qid *qid;
2711 uint8_t *sha1 = (uint8_t *)imsg.data +
2712 sizeof(*icommits) + i * SHA1_DIGEST_LENGTH;
2713 err = got_object_qid_alloc_partial(&qid);
2716 memcpy(qid->id.sha1, sha1, SHA1_DIGEST_LENGTH);
2717 STAILQ_INSERT_TAIL(commit_ids, qid, entry);
2719 /* The last commit may contain a change. */
2720 if (i == icommits->ncommits - 1) {
2721 *changed_commit_id =
2722 got_object_id_dup(&qid->id);
2723 if (*changed_commit_id == NULL) {
2724 err = got_error_from_errno(
2725 "got_object_id_dup");
2731 case GOT_IMSG_COMMIT:
2732 if (*changed_commit_id == NULL) {
2733 err = got_error(GOT_ERR_PRIVSEP_MSG);
2736 err = get_commit_from_imsg(changed_commit, &imsg,
2739 case GOT_IMSG_COMMIT_TRAVERSAL_DONE:
2743 err = got_error(GOT_ERR_PRIVSEP_MSG);
2753 got_object_id_queue_free(commit_ids);
2757 const struct got_error *
2758 got_privsep_send_enumerated_tree(size_t *totlen, struct imsgbuf *ibuf,
2759 struct got_object_id *tree_id, const char *path,
2760 struct got_parsed_tree_entry *entries, int nentries)
2762 const struct got_error *err = NULL;
2764 size_t path_len = strlen(path);
2767 msglen = sizeof(struct got_imsg_enumerated_tree) + path_len;
2768 wbuf = imsg_create(ibuf, GOT_IMSG_ENUMERATED_TREE, 0, 0, msglen);
2770 return got_error_from_errno("imsg_create ENUMERATED_TREE");
2772 if (imsg_add(wbuf, tree_id->sha1, SHA1_DIGEST_LENGTH) == -1)
2773 return got_error_from_errno("imsg_add ENUMERATED_TREE");
2774 if (imsg_add(wbuf, &nentries, sizeof(nentries)) == -1)
2775 return got_error_from_errno("imsg_add ENUMERATED_TREE");
2776 if (imsg_add(wbuf, path, path_len) == -1)
2777 return got_error_from_errno("imsg_add ENUMERATED_TREE");
2780 imsg_close(ibuf, wbuf);
2783 err = send_tree_entries(ibuf, entries, nentries);
2788 return flush_imsg(ibuf);
2791 const struct got_error *
2792 got_privsep_send_object_enumeration_request(struct imsgbuf *ibuf)
2794 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_ENUMERATION_REQUEST,
2795 0, 0, -1, NULL, 0) == -1)
2796 return got_error_from_errno("imsg_compose "
2797 "OBJECT_ENUMERATION_REQUEST");
2799 return flush_imsg(ibuf);
2802 const struct got_error *
2803 got_privsep_send_object_enumeration_done(struct imsgbuf *ibuf)
2805 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_ENUMERATION_DONE,
2806 0, 0, -1, NULL, 0) == -1)
2807 return got_error_from_errno("imsg_compose "
2808 "OBJECT_ENUMERATION_DONE");
2810 return flush_imsg(ibuf);
2813 const struct got_error *
2814 got_privsep_send_enumerated_commit(struct imsgbuf *ibuf,
2815 struct got_object_id *id, time_t mtime)
2819 wbuf = imsg_create(ibuf, GOT_IMSG_ENUMERATED_COMMIT, 0, 0,
2820 sizeof(struct got_imsg_enumerated_commit) + SHA1_DIGEST_LENGTH);
2822 return got_error_from_errno("imsg_create ENUMERATED_COMMIT");
2824 /* Keep in sync with struct got_imsg_enumerated_commit! */
2825 if (imsg_add(wbuf, id, SHA1_DIGEST_LENGTH) == -1)
2826 return got_error_from_errno("imsg_add ENUMERATED_COMMIT");
2827 if (imsg_add(wbuf, &mtime, sizeof(mtime)) == -1)
2828 return got_error_from_errno("imsg_add ENUMERATED_COMMIT");
2831 imsg_close(ibuf, wbuf);
2832 /* Don't flush yet, tree entries or ENUMERATION_DONE will follow. */
2836 const struct got_error *
2837 got_privsep_recv_enumerated_objects(struct imsgbuf *ibuf,
2838 got_object_enumerate_commit_cb cb_commit,
2839 got_object_enumerate_tree_cb cb_tree, void *cb_arg,
2840 struct got_repository *repo)
2842 const struct got_error *err = NULL;
2844 struct got_imsg_enumerated_commit *icommit = NULL;
2845 struct got_object_id commit_id;
2846 int have_commit = 0;
2848 struct got_tree_object tree;
2849 struct got_imsg_enumerated_tree *itree;
2850 struct got_object_id tree_id;
2851 char *path = NULL, *canon_path = NULL;
2852 size_t datalen, path_len;
2856 memset(&tree, 0, sizeof(tree));
2859 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2863 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2864 switch (imsg.hdr.type) {
2865 case GOT_IMSG_ENUMERATED_COMMIT:
2866 if (have_commit && nentries != -1) {
2867 err = got_error(GOT_ERR_PRIVSEP_MSG);
2870 if (datalen != sizeof(*icommit)) {
2871 err = got_error(GOT_ERR_PRIVSEP_LEN);
2874 icommit = (struct got_imsg_enumerated_commit *)imsg.data;
2875 memcpy(commit_id.sha1, icommit->id, SHA1_DIGEST_LENGTH);
2876 mtime = icommit->mtime;
2879 case GOT_IMSG_ENUMERATED_TREE:
2880 /* Should be preceeded by GOT_IMSG_ENUMERATED_COMMIT. */
2882 err = got_error(GOT_ERR_PRIVSEP_MSG);
2885 if (datalen < sizeof(*itree)) {
2886 err = got_error(GOT_ERR_PRIVSEP_LEN);
2890 path_len = datalen - sizeof(*itree);
2891 if (path_len == 0) {
2892 err = got_error(GOT_ERR_PRIVSEP_LEN);
2895 memcpy(tree_id.sha1, itree->id, sizeof(tree_id.sha1));
2897 path = malloc(path_len + 1);
2899 err = got_error_from_errno("malloc");
2903 canon_path = malloc(path_len + 1);
2904 if (canon_path == NULL) {
2905 err = got_error_from_errno("malloc");
2908 memcpy(path, (uint8_t *)imsg.data + sizeof(*itree),
2910 path[path_len] = '\0';
2911 if (!got_path_is_absolute(path)) {
2912 err = got_error(GOT_ERR_BAD_PATH);
2915 if (got_path_is_root_dir(path)) {
2916 /* XXX check what got_canonpath() does wrong */
2917 canon_path[0] = '/';
2918 canon_path[1] = '\0';
2920 err = got_canonpath(path, canon_path,
2925 if (strcmp(path, canon_path) != 0) {
2926 err = got_error(GOT_ERR_BAD_PATH);
2929 if (nentries != -1) {
2930 err = got_error(GOT_ERR_PRIVSEP_MSG);
2933 if (itree->nentries < -1) {
2934 err = got_error(GOT_ERR_PRIVSEP_MSG);
2937 if (itree->nentries == -1) {
2938 /* Tree was not found in pack file. */
2940 err = cb_tree(cb_arg, NULL, mtime, &tree_id,
2944 if (itree->nentries > INT_MAX) {
2945 err = got_error(GOT_ERR_PRIVSEP_LEN);
2948 tree.entries = calloc(itree->nentries,
2949 sizeof(struct got_tree_entry));
2950 if (tree.entries == NULL) {
2951 err = got_error_from_errno("calloc");
2954 if (itree->nentries == 0) {
2955 err = cb_tree(cb_arg, &tree, mtime, &tree_id,
2960 /* Prepare for next tree. */
2962 memset(&tree, 0, sizeof(tree));
2965 tree.nentries = itree->nentries;
2969 case GOT_IMSG_TREE_ENTRIES:
2970 /* Should be preceeded by GOT_IMSG_ENUMERATED_TREE. */
2971 if (nentries <= -1) {
2972 err = got_error(GOT_ERR_PRIVSEP_MSG);
2975 err = recv_tree_entries(imsg.data, datalen,
2979 if (tree.nentries == nentries) {
2980 err = cb_tree(cb_arg, &tree, mtime, &tree_id,
2985 /* Prepare for next tree. */
2987 memset(&tree, 0, sizeof(tree));
2991 case GOT_IMSG_TREE_ENUMERATION_DONE:
2992 /* All trees have been found and traversed. */
2993 if (!have_commit || path == NULL || nentries != -1) {
2994 err = got_error(GOT_ERR_PRIVSEP_MSG);
2997 err = cb_commit(cb_arg, mtime, &commit_id, repo);
3002 case GOT_IMSG_OBJECT_ENUMERATION_DONE:
3006 err = got_error(GOT_ERR_PRIVSEP_MSG);
3021 const struct got_error *
3022 got_privsep_send_raw_delta_req(struct imsgbuf *ibuf, int idx,
3023 struct got_object_id *id)
3025 struct got_imsg_raw_delta_request dreq;
3028 memcpy(dreq.id, id->sha1, SHA1_DIGEST_LENGTH);
3030 if (imsg_compose(ibuf, GOT_IMSG_RAW_DELTA_REQUEST, 0, 0, -1,
3031 &dreq, sizeof(dreq)) == -1)
3032 return got_error_from_errno("imsg_compose RAW_DELTA_REQUEST");
3034 return flush_imsg(ibuf);
3037 const struct got_error *
3038 got_privsep_send_raw_delta_outfd(struct imsgbuf *ibuf, int fd)
3040 return send_fd(ibuf, GOT_IMSG_RAW_DELTA_OUTFD, fd);
3043 const struct got_error *
3044 got_privsep_send_raw_delta(struct imsgbuf *ibuf, uint64_t base_size,
3045 uint64_t result_size, off_t delta_size, off_t delta_compressed_size,
3046 off_t delta_offset, off_t delta_out_offset, struct got_object_id *base_id)
3048 struct got_imsg_raw_delta idelta;
3051 idelta.base_size = base_size;
3052 idelta.result_size = result_size;
3053 idelta.delta_size = delta_size;
3054 idelta.delta_compressed_size = delta_compressed_size;
3055 idelta.delta_offset = delta_offset;
3056 idelta.delta_out_offset = delta_out_offset;
3057 memcpy(idelta.base_id, base_id->sha1, SHA1_DIGEST_LENGTH);
3059 ret = imsg_compose(ibuf, GOT_IMSG_RAW_DELTA, 0, 0, -1,
3060 &idelta, sizeof(idelta));
3062 return got_error_from_errno("imsg_compose RAW_DELTA");
3064 return flush_imsg(ibuf);
3067 const struct got_error *
3068 got_privsep_recv_raw_delta(uint64_t *base_size, uint64_t *result_size,
3069 off_t *delta_size, off_t *delta_compressed_size, off_t *delta_offset,
3070 off_t *delta_out_offset, struct got_object_id **base_id, struct imsgbuf *ibuf)
3072 const struct got_error *err = NULL;
3074 struct got_imsg_raw_delta *delta;
3080 *delta_compressed_size = 0;
3082 *delta_out_offset = 0;
3085 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
3089 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
3091 switch (imsg.hdr.type) {
3092 case GOT_IMSG_RAW_DELTA:
3093 if (datalen != sizeof(*delta)) {
3094 err = got_error(GOT_ERR_PRIVSEP_LEN);
3098 *base_size = delta->base_size;
3099 *result_size = delta->result_size;
3100 *delta_size = delta->delta_size;
3101 *delta_compressed_size = delta->delta_compressed_size;
3102 *delta_offset = delta->delta_offset;
3103 *delta_out_offset = delta->delta_out_offset;
3104 *base_id = calloc(1, sizeof(**base_id));
3105 if (*base_id == NULL) {
3106 err = got_error_from_errno("malloc");
3109 memcpy((*base_id)->sha1, delta->base_id, SHA1_DIGEST_LENGTH);
3112 err = got_error(GOT_ERR_PRIVSEP_MSG);
3125 static const struct got_error *
3126 send_idlist(struct imsgbuf *ibuf, struct got_object_id **ids, size_t nids)
3128 const struct got_error *err = NULL;
3129 struct got_imsg_object_idlist idlist;
3133 if (nids > GOT_IMSG_OBJ_ID_LIST_MAX_NIDS)
3134 return got_error(GOT_ERR_NO_SPACE);
3136 wbuf = imsg_create(ibuf, GOT_IMSG_OBJ_ID_LIST, 0, 0,
3137 sizeof(idlist) + nids * sizeof(**ids));
3139 err = got_error_from_errno("imsg_create OBJ_ID_LIST");
3144 if (imsg_add(wbuf, &idlist, sizeof(idlist)) == -1)
3145 return got_error_from_errno("imsg_add OBJ_ID_LIST");
3147 for (i = 0; i < nids; i++) {
3148 struct got_object_id *id = ids[i];
3149 if (imsg_add(wbuf, id, sizeof(*id)) == -1)
3150 return got_error_from_errno("imsg_add OBJ_ID_LIST");
3154 imsg_close(ibuf, wbuf);
3156 return flush_imsg(ibuf);
3159 const struct got_error *
3160 got_privsep_send_object_idlist(struct imsgbuf *ibuf,
3161 struct got_object_id **ids, size_t nids)
3163 const struct got_error *err = NULL;
3164 struct got_object_id *idlist[GOT_IMSG_OBJ_ID_LIST_MAX_NIDS];
3167 for (i = 0; i < nids; i++) {
3168 j = i % nitems(idlist);
3170 if (j >= nitems(idlist) - 1) {
3171 err = send_idlist(ibuf, idlist, j + 1);
3179 err = send_idlist(ibuf, idlist, j + 1);
3187 const struct got_error *
3188 got_privsep_send_object_idlist_done(struct imsgbuf *ibuf)
3190 if (imsg_compose(ibuf, GOT_IMSG_OBJ_ID_LIST_DONE, 0, 0, -1, NULL, 0)
3192 return got_error_from_errno("imsg_compose OBJ_ID_LIST_DONE");
3194 return flush_imsg(ibuf);
3197 const struct got_error *
3198 got_privsep_recv_object_idlist(int *done, struct got_object_id **ids,
3199 size_t *nids, struct imsgbuf *ibuf)
3201 const struct got_error *err = NULL;
3203 struct got_imsg_object_idlist *idlist;
3210 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
3214 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
3215 switch (imsg.hdr.type) {
3216 case GOT_IMSG_OBJ_ID_LIST:
3217 if (datalen < sizeof(*idlist)) {
3218 err = got_error(GOT_ERR_PRIVSEP_LEN);
3222 if (idlist->nids > GOT_IMSG_OBJ_ID_LIST_MAX_NIDS) {
3223 err = got_error(GOT_ERR_PRIVSEP_LEN);
3226 *nids = idlist->nids;
3227 *ids = calloc(*nids, sizeof(**ids));
3229 err = got_error_from_errno("calloc");
3232 memcpy(*ids, (uint8_t *)imsg.data + sizeof(idlist),
3233 *nids * sizeof(**ids));
3235 case GOT_IMSG_OBJ_ID_LIST_DONE:
3239 err = got_error(GOT_ERR_PRIVSEP_MSG);
3248 const struct got_error *
3249 got_privsep_send_delta_reuse_req(struct imsgbuf *ibuf)
3251 if (imsg_compose(ibuf, GOT_IMSG_DELTA_REUSE_REQUEST, 0, 0, -1, NULL, 0)
3253 return got_error_from_errno("imsg_compose DELTA_REUSE_REQUEST");
3255 return flush_imsg(ibuf);
3258 const struct got_error *
3259 got_privsep_send_reused_deltas(struct imsgbuf *ibuf,
3260 struct got_imsg_reused_delta *deltas, size_t ndeltas)
3262 const struct got_error *err = NULL;
3264 struct got_imsg_reused_deltas ideltas;
3267 if (ndeltas > GOT_IMSG_REUSED_DELTAS_MAX_NDELTAS)
3268 return got_error(GOT_ERR_NO_SPACE);
3270 wbuf = imsg_create(ibuf, GOT_IMSG_REUSED_DELTAS, 0, 0,
3271 sizeof(ideltas) + ndeltas * sizeof(*deltas));
3273 err = got_error_from_errno("imsg_create REUSED_DELTAS");
3277 ideltas.ndeltas = ndeltas;
3278 if (imsg_add(wbuf, &ideltas, sizeof(ideltas)) == -1)
3279 return got_error_from_errno("imsg_add REUSED_DELTAS");
3281 for (i = 0; i < ndeltas; i++) {
3282 struct got_imsg_reused_delta *delta = &deltas[i];
3283 if (imsg_add(wbuf, delta, sizeof(*delta)) == -1)
3284 return got_error_from_errno("imsg_add REUSED_DELTAS");
3288 imsg_close(ibuf, wbuf);
3290 return flush_imsg(ibuf);
3293 const struct got_error *
3294 got_privsep_send_reused_deltas_done(struct imsgbuf *ibuf)
3296 if (imsg_compose(ibuf, GOT_IMSG_DELTA_REUSE_DONE, 0, 0, -1, NULL, 0)
3298 return got_error_from_errno("imsg_compose DELTA_REUSE_DONE");
3300 return flush_imsg(ibuf);
3303 const struct got_error *
3304 got_privsep_recv_reused_deltas(int *done, struct got_imsg_reused_delta *deltas,
3305 size_t *ndeltas, struct imsgbuf *ibuf)
3307 const struct got_error *err = NULL;
3309 struct got_imsg_reused_deltas *ideltas;
3315 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
3319 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
3320 switch (imsg.hdr.type) {
3321 case GOT_IMSG_REUSED_DELTAS:
3322 if (datalen < sizeof(*ideltas)) {
3323 err = got_error(GOT_ERR_PRIVSEP_LEN);
3326 ideltas = imsg.data;
3327 if (ideltas->ndeltas > GOT_IMSG_OBJ_ID_LIST_MAX_NIDS) {
3328 err = got_error(GOT_ERR_PRIVSEP_LEN);
3331 *ndeltas = ideltas->ndeltas;
3332 memcpy(deltas, (uint8_t *)imsg.data + sizeof(ideltas),
3333 *ndeltas * sizeof(*deltas));
3335 case GOT_IMSG_DELTA_REUSE_DONE:
3339 err = got_error(GOT_ERR_PRIVSEP_MSG);
3348 const struct got_error *
3349 got_privsep_unveil_exec_helpers(void)
3351 const char *helpers[] = {
3352 GOT_PATH_PROG_READ_PACK,
3353 GOT_PATH_PROG_READ_OBJECT,
3354 GOT_PATH_PROG_READ_COMMIT,
3355 GOT_PATH_PROG_READ_TREE,
3356 GOT_PATH_PROG_READ_BLOB,
3357 GOT_PATH_PROG_READ_TAG,
3358 GOT_PATH_PROG_READ_GITCONFIG,
3359 GOT_PATH_PROG_READ_GOTCONFIG,
3360 GOT_PATH_PROG_READ_PATCH,
3361 GOT_PATH_PROG_FETCH_PACK,
3362 GOT_PATH_PROG_INDEX_PACK,
3363 GOT_PATH_PROG_SEND_PACK,
3367 for (i = 0; i < nitems(helpers); i++) {
3368 if (unveil(helpers[i], "x") == 0)
3370 return got_error_from_errno2("unveil", helpers[i]);
3377 got_privsep_exec_child(int imsg_fds[2], const char *path, const char *repo_path)
3379 if (close(imsg_fds[0]) == -1) {
3380 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
3384 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
3385 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
3389 closefrom(GOT_IMSG_FD_CHILD + 1);
3391 if (execl(path, path, repo_path, (char *)NULL) == -1) {
3392 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,