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 >
1578 err = got_error(GOT_ERR_PRIVSEP_LEN);
1582 if (*nentries >= tree->nentries) {
1583 err = got_error(GOT_ERR_PRIVSEP_LEN);
1586 te = &tree->entries[*nentries];
1587 te_name = buf + sizeof(ite);
1588 memcpy(te->name, te_name, ite.namelen);
1589 te->name[ite.namelen] = '\0';
1590 memcpy(te->id.sha1, ite.id, SHA1_DIGEST_LENGTH);
1591 te->mode = ite.mode;
1592 te->idx = *nentries;
1595 te_offset += sizeof(ite) + ite.namelen;
1601 const struct got_error *
1602 got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
1604 const struct got_error *err = NULL;
1605 const size_t min_datalen =
1606 MIN(sizeof(struct got_imsg_error),
1607 sizeof(struct got_imsg_tree_object));
1608 struct got_imsg_tree_object *itree;
1613 err = read_imsg(ibuf);
1622 n = imsg_get(ibuf, &imsg);
1625 if (nentries < (*tree)->nentries) {
1626 err = read_imsg(ibuf);
1633 err = got_error(GOT_ERR_PRIVSEP_MSG);
1638 if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen) {
1640 err = got_error(GOT_ERR_PRIVSEP_LEN);
1644 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1646 switch (imsg.hdr.type) {
1647 case GOT_IMSG_ERROR:
1648 err = recv_imsg_error(&imsg, datalen);
1651 /* This message should only appear once. */
1652 if (*tree != NULL) {
1653 err = got_error(GOT_ERR_PRIVSEP_MSG);
1656 if (datalen != sizeof(*itree)) {
1657 err = got_error(GOT_ERR_PRIVSEP_LEN);
1661 if (itree->nentries < 0) {
1662 err = got_error(GOT_ERR_PRIVSEP_LEN);
1665 *tree = malloc(sizeof(**tree));
1666 if (*tree == NULL) {
1667 err = got_error_from_errno("malloc");
1670 (*tree)->entries = calloc(itree->nentries,
1671 sizeof(struct got_tree_entry));
1672 if ((*tree)->entries == NULL) {
1673 err = got_error_from_errno("malloc");
1678 (*tree)->nentries = itree->nentries;
1679 (*tree)->refcnt = 0;
1681 case GOT_IMSG_TREE_ENTRIES:
1682 /* This message should be preceeded by GOT_IMSG_TREE. */
1683 if (*tree == NULL) {
1684 err = got_error(GOT_ERR_PRIVSEP_MSG);
1687 err = recv_tree_entries(imsg.data, datalen,
1691 err = got_error(GOT_ERR_PRIVSEP_MSG);
1700 if (*tree && (*tree)->nentries != nentries) {
1702 err = got_error(GOT_ERR_PRIVSEP_LEN);
1703 got_object_tree_close(*tree);
1710 const struct got_error *
1711 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
1712 const uint8_t *data)
1714 struct got_imsg_blob iblob;
1717 iblob.hdrlen = hdrlen;
1722 if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
1723 return got_error(GOT_ERR_NO_SPACE);
1725 buf = malloc(sizeof(iblob) + size);
1727 return got_error_from_errno("malloc");
1729 memcpy(buf, &iblob, sizeof(iblob));
1730 memcpy(buf + sizeof(iblob), data, size);
1731 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
1732 sizeof(iblob) + size) == -1) {
1734 return got_error_from_errno("imsg_compose BLOB");
1738 /* Data has already been written to file descriptor. */
1739 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
1740 sizeof(iblob)) == -1)
1741 return got_error_from_errno("imsg_compose BLOB");
1745 return flush_imsg(ibuf);
1748 const struct got_error *
1749 got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1750 struct imsgbuf *ibuf)
1752 const struct got_error *err = NULL;
1754 struct got_imsg_blob *iblob;
1759 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1763 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1765 switch (imsg.hdr.type) {
1767 if (datalen < sizeof(*iblob)) {
1768 err = got_error(GOT_ERR_PRIVSEP_LEN);
1772 *size = iblob->size;
1773 *hdrlen = iblob->hdrlen;
1775 if (datalen == sizeof(*iblob)) {
1776 /* Data has been written to file descriptor. */
1780 if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX) {
1781 err = got_error(GOT_ERR_PRIVSEP_LEN);
1785 *outbuf = malloc(*size);
1786 if (*outbuf == NULL) {
1787 err = got_error_from_errno("malloc");
1790 memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
1793 err = got_error(GOT_ERR_PRIVSEP_MSG);
1802 static const struct got_error *
1803 send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
1805 const struct got_error *err = NULL;
1806 size_t offset, remain;
1809 remain = tagmsg_len;
1810 while (remain > 0) {
1811 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1813 if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
1814 tag->tagmsg + offset, n) == -1) {
1815 err = got_error_from_errno("imsg_compose TAG_TAGMSG");
1819 err = flush_imsg(ibuf);
1830 const struct got_error *
1831 got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
1833 const struct got_error *err = NULL;
1834 struct got_imsg_tag_object *itag;
1837 size_t tag_len = strlen(tag->tag);
1838 size_t tagger_len = strlen(tag->tagger);
1839 size_t tagmsg_len = strlen(tag->tagmsg);
1841 total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
1843 buf = malloc(total);
1845 return got_error_from_errno("malloc");
1847 itag = (struct got_imsg_tag_object *)buf;
1848 memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
1849 itag->obj_type = tag->obj_type;
1850 itag->tag_len = tag_len;
1851 itag->tagger_len = tagger_len;
1852 itag->tagger_time = tag->tagger_time;
1853 itag->tagger_gmtoff = tag->tagger_gmtoff;
1854 itag->tagmsg_len = tagmsg_len;
1856 len = sizeof(*itag);
1857 memcpy(buf + len, tag->tag, tag_len);
1859 memcpy(buf + len, tag->tagger, tagger_len);
1862 if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
1863 err = got_error_from_errno("imsg_compose TAG");
1867 if (tagmsg_len == 0 ||
1868 tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1869 err = flush_imsg(ibuf);
1873 err = send_tagmsg(ibuf, tag, tagmsg_len);
1879 const struct got_error *
1880 got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
1882 const struct got_error *err = NULL;
1884 struct got_imsg_tag_object *itag;
1885 size_t len, datalen;
1886 const size_t min_datalen =
1887 MIN(sizeof(struct got_imsg_error),
1888 sizeof(struct got_imsg_tag_object));
1892 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1896 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1899 switch (imsg.hdr.type) {
1901 if (datalen < sizeof(*itag)) {
1902 err = got_error(GOT_ERR_PRIVSEP_LEN);
1906 if (datalen != sizeof(*itag) + itag->tag_len +
1908 err = got_error(GOT_ERR_PRIVSEP_LEN);
1911 len += sizeof(*itag);
1913 *tag = calloc(1, sizeof(**tag));
1915 err = got_error_from_errno("calloc");
1919 memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
1921 if (itag->tag_len == 0) {
1922 (*tag)->tag = strdup("");
1923 if ((*tag)->tag == NULL) {
1924 err = got_error_from_errno("strdup");
1928 (*tag)->tag = malloc(itag->tag_len + 1);
1929 if ((*tag)->tag == NULL) {
1930 err = got_error_from_errno("malloc");
1933 memcpy((*tag)->tag, imsg.data + len,
1935 (*tag)->tag[itag->tag_len] = '\0';
1937 len += itag->tag_len;
1939 (*tag)->obj_type = itag->obj_type;
1940 (*tag)->tagger_time = itag->tagger_time;
1941 (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1943 if (itag->tagger_len == 0) {
1944 (*tag)->tagger = strdup("");
1945 if ((*tag)->tagger == NULL) {
1946 err = got_error_from_errno("strdup");
1950 (*tag)->tagger = malloc(itag->tagger_len + 1);
1951 if ((*tag)->tagger == NULL) {
1952 err = got_error_from_errno("malloc");
1955 memcpy((*tag)->tagger, imsg.data + len,
1957 (*tag)->tagger[itag->tagger_len] = '\0';
1959 len += itag->tagger_len;
1961 if (itag->tagmsg_len == 0) {
1962 (*tag)->tagmsg = strdup("");
1963 if ((*tag)->tagmsg == NULL) {
1964 err = got_error_from_errno("strdup");
1968 size_t offset = 0, remain = itag->tagmsg_len;
1970 (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1971 if ((*tag)->tagmsg == NULL) {
1972 err = got_error_from_errno("malloc");
1975 while (remain > 0) {
1976 struct imsg imsg_log;
1977 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1980 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1984 if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1985 return got_error(GOT_ERR_PRIVSEP_MSG);
1987 memcpy((*tag)->tagmsg + offset, imsg_log.data,
1989 imsg_free(&imsg_log);
1993 (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1998 err = got_error(GOT_ERR_PRIVSEP_MSG);
2007 const struct got_error *
2008 got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
2009 struct got_packidx *packidx)
2011 const struct got_error *err = NULL;
2012 struct got_imsg_packidx ipackidx;
2013 struct got_imsg_pack ipack;
2016 ipackidx.len = packidx->len;
2017 ipackidx.packfile_size = pack->filesize;
2018 fd = dup(packidx->fd);
2020 return got_error_from_errno("dup");
2022 if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
2023 sizeof(ipackidx)) == -1) {
2024 err = got_error_from_errno("imsg_compose PACKIDX");
2029 if (strlcpy(ipack.path_packfile, pack->path_packfile,
2030 sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
2031 return got_error(GOT_ERR_NO_SPACE);
2032 ipack.filesize = pack->filesize;
2036 return got_error_from_errno("dup");
2038 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
2040 err = got_error_from_errno("imsg_compose PACK");
2045 return flush_imsg(ibuf);
2048 const struct got_error *
2049 got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
2050 struct got_object_id *id)
2052 struct got_imsg_packed_object iobj;
2055 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
2057 if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
2058 &iobj, sizeof(iobj)) == -1)
2059 return got_error_from_errno("imsg_compose "
2060 "PACKED_OBJECT_REQUEST");
2062 return flush_imsg(ibuf);
2065 const struct got_error *
2066 got_privsep_send_packed_raw_obj_req(struct imsgbuf *ibuf, int idx,
2067 struct got_object_id *id)
2069 struct got_imsg_packed_object iobj;
2072 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
2074 if (imsg_compose(ibuf, GOT_IMSG_PACKED_RAW_OBJECT_REQUEST, 0, 0, -1,
2075 &iobj, sizeof(iobj)) == -1)
2076 return got_error_from_errno("imsg_compose "
2077 "PACKED_OBJECT_REQUEST");
2079 return flush_imsg(ibuf);
2082 const struct got_error *
2083 got_privsep_send_gitconfig_parse_req(struct imsgbuf *ibuf, int fd)
2085 const struct got_error *err = NULL;
2087 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_PARSE_REQUEST, 0, 0, fd,
2089 err = got_error_from_errno("imsg_compose "
2090 "GITCONFIG_PARSE_REQUEST");
2095 return flush_imsg(ibuf);
2098 const struct got_error *
2099 got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *ibuf)
2101 if (imsg_compose(ibuf,
2102 GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST, 0, 0, -1,
2104 return got_error_from_errno("imsg_compose "
2105 "GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST");
2107 return flush_imsg(ibuf);
2110 const struct got_error *
2111 got_privsep_send_gitconfig_repository_extensions_req(struct imsgbuf *ibuf)
2113 if (imsg_compose(ibuf,
2114 GOT_IMSG_GITCONFIG_REPOSITORY_EXTENSIONS_REQUEST, 0, 0, -1,
2116 return got_error_from_errno("imsg_compose "
2117 "GITCONFIG_REPOSITORY_EXTENSIONS_REQUEST");
2119 return flush_imsg(ibuf);
2123 const struct got_error *
2124 got_privsep_send_gitconfig_author_name_req(struct imsgbuf *ibuf)
2126 if (imsg_compose(ibuf,
2127 GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST, 0, 0, -1, NULL, 0) == -1)
2128 return got_error_from_errno("imsg_compose "
2129 "GITCONFIG_AUTHOR_NAME_REQUEST");
2131 return flush_imsg(ibuf);
2134 const struct got_error *
2135 got_privsep_send_gitconfig_author_email_req(struct imsgbuf *ibuf)
2137 if (imsg_compose(ibuf,
2138 GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST, 0, 0, -1, NULL, 0) == -1)
2139 return got_error_from_errno("imsg_compose "
2140 "GITCONFIG_AUTHOR_EMAIL_REQUEST");
2142 return flush_imsg(ibuf);
2145 const struct got_error *
2146 got_privsep_send_gitconfig_remotes_req(struct imsgbuf *ibuf)
2148 if (imsg_compose(ibuf,
2149 GOT_IMSG_GITCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
2150 return got_error_from_errno("imsg_compose "
2151 "GITCONFIG_REMOTE_REQUEST");
2153 return flush_imsg(ibuf);
2156 const struct got_error *
2157 got_privsep_send_gitconfig_owner_req(struct imsgbuf *ibuf)
2159 if (imsg_compose(ibuf,
2160 GOT_IMSG_GITCONFIG_OWNER_REQUEST, 0, 0, -1, NULL, 0) == -1)
2161 return got_error_from_errno("imsg_compose "
2162 "GITCONFIG_OWNER_REQUEST");
2164 return flush_imsg(ibuf);
2167 const struct got_error *
2168 got_privsep_recv_gitconfig_str(char **str, struct imsgbuf *ibuf)
2170 const struct got_error *err = NULL;
2173 const size_t min_datalen = 0;
2177 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2180 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2182 switch (imsg.hdr.type) {
2183 case GOT_IMSG_GITCONFIG_STR_VAL:
2186 /* datalen does not include terminating \0 */
2187 *str = malloc(datalen + 1);
2189 err = got_error_from_errno("malloc");
2192 memcpy(*str, imsg.data, datalen);
2193 (*str)[datalen] = '\0';
2196 err = got_error(GOT_ERR_PRIVSEP_MSG);
2204 const struct got_error *
2205 got_privsep_recv_gitconfig_int(int *val, struct imsgbuf *ibuf)
2207 const struct got_error *err = NULL;
2210 const size_t min_datalen =
2211 MIN(sizeof(struct got_imsg_error), sizeof(int));
2215 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2218 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2220 switch (imsg.hdr.type) {
2221 case GOT_IMSG_GITCONFIG_INT_VAL:
2222 if (datalen != sizeof(*val)) {
2223 err = got_error(GOT_ERR_PRIVSEP_LEN);
2226 memcpy(val, imsg.data, sizeof(*val));
2229 err = got_error(GOT_ERR_PRIVSEP_MSG);
2238 free_remote_data(struct got_remote_repo *remote)
2243 free(remote->fetch_url);
2244 free(remote->send_url);
2245 for (i = 0; i < remote->nfetch_branches; i++)
2246 free(remote->fetch_branches[i]);
2247 free(remote->fetch_branches);
2248 for (i = 0; i < remote->nsend_branches; i++)
2249 free(remote->send_branches[i]);
2250 free(remote->send_branches);
2251 for (i = 0; i < remote->nfetch_refs; i++)
2252 free(remote->fetch_refs[i]);
2253 free(remote->fetch_refs);
2256 const struct got_error *
2257 got_privsep_recv_gitconfig_remotes(struct got_remote_repo **remotes,
2258 int *nremotes, struct imsgbuf *ibuf)
2260 const struct got_error *err = NULL;
2263 struct got_imsg_remotes iremotes;
2264 struct got_imsg_remote iremote;
2268 iremotes.nremotes = 0;
2270 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremotes));
2273 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2275 switch (imsg.hdr.type) {
2276 case GOT_IMSG_GITCONFIG_REMOTES:
2277 if (datalen != sizeof(iremotes)) {
2278 err = got_error(GOT_ERR_PRIVSEP_LEN);
2281 memcpy(&iremotes, imsg.data, sizeof(iremotes));
2282 if (iremotes.nremotes == 0) {
2289 return got_error(GOT_ERR_PRIVSEP_MSG);
2294 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(**remotes));
2295 if (*remotes == NULL)
2296 return got_error_from_errno("recallocarray");
2298 while (*nremotes < iremotes.nremotes) {
2299 struct got_remote_repo *remote;
2301 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremote));
2304 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2306 switch (imsg.hdr.type) {
2307 case GOT_IMSG_GITCONFIG_REMOTE:
2308 remote = &(*remotes)[*nremotes];
2309 memset(remote, 0, sizeof(*remote));
2310 if (datalen < sizeof(iremote)) {
2311 err = got_error(GOT_ERR_PRIVSEP_LEN);
2314 memcpy(&iremote, imsg.data, sizeof(iremote));
2315 if (iremote.name_len == 0 ||
2316 iremote.fetch_url_len == 0 ||
2317 iremote.send_url_len == 0 ||
2318 (sizeof(iremote) + iremote.name_len +
2319 iremote.fetch_url_len + iremote.send_url_len) > datalen) {
2320 err = got_error(GOT_ERR_PRIVSEP_LEN);
2323 remote->name = strndup(imsg.data + sizeof(iremote),
2325 if (remote->name == NULL) {
2326 err = got_error_from_errno("strndup");
2329 remote->fetch_url = strndup(imsg.data + sizeof(iremote) +
2330 iremote.name_len, iremote.fetch_url_len);
2331 if (remote->fetch_url == NULL) {
2332 err = got_error_from_errno("strndup");
2333 free_remote_data(remote);
2336 remote->send_url = strndup(imsg.data + sizeof(iremote) +
2337 iremote.name_len + iremote.fetch_url_len,
2338 iremote.send_url_len);
2339 if (remote->send_url == NULL) {
2340 err = got_error_from_errno("strndup");
2341 free_remote_data(remote);
2344 remote->mirror_references = iremote.mirror_references;
2345 remote->fetch_all_branches = iremote.fetch_all_branches;
2346 remote->nfetch_branches = 0;
2347 remote->fetch_branches = NULL;
2348 remote->nsend_branches = 0;
2349 remote->send_branches = NULL;
2350 remote->nfetch_refs = 0;
2351 remote->fetch_refs = NULL;
2355 err = got_error(GOT_ERR_PRIVSEP_MSG);
2366 for (i = 0; i < *nremotes; i++)
2367 free_remote_data(&(*remotes)[i]);
2375 const struct got_error *
2376 got_privsep_send_gotconfig_parse_req(struct imsgbuf *ibuf, int fd)
2378 const struct got_error *err = NULL;
2380 if (imsg_compose(ibuf, GOT_IMSG_GOTCONFIG_PARSE_REQUEST, 0, 0, fd,
2382 err = got_error_from_errno("imsg_compose "
2383 "GOTCONFIG_PARSE_REQUEST");
2388 return flush_imsg(ibuf);
2391 const struct got_error *
2392 got_privsep_send_gotconfig_author_req(struct imsgbuf *ibuf)
2394 if (imsg_compose(ibuf,
2395 GOT_IMSG_GOTCONFIG_AUTHOR_REQUEST, 0, 0, -1, NULL, 0) == -1)
2396 return got_error_from_errno("imsg_compose "
2397 "GOTCONFIG_AUTHOR_REQUEST");
2399 return flush_imsg(ibuf);
2402 const struct got_error *
2403 got_privsep_send_gotconfig_remotes_req(struct imsgbuf *ibuf)
2405 if (imsg_compose(ibuf,
2406 GOT_IMSG_GOTCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
2407 return got_error_from_errno("imsg_compose "
2408 "GOTCONFIG_REMOTE_REQUEST");
2410 return flush_imsg(ibuf);
2413 const struct got_error *
2414 got_privsep_recv_gotconfig_str(char **str, struct imsgbuf *ibuf)
2416 const struct got_error *err = NULL;
2419 const size_t min_datalen = 0;
2423 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2426 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2428 switch (imsg.hdr.type) {
2429 case GOT_IMSG_ERROR:
2430 if (datalen < sizeof(struct got_imsg_error)) {
2431 err = got_error(GOT_ERR_PRIVSEP_LEN);
2434 err = recv_imsg_error(&imsg, datalen);
2436 case GOT_IMSG_GOTCONFIG_STR_VAL:
2439 /* datalen does not include terminating \0 */
2440 *str = malloc(datalen + 1);
2442 err = got_error_from_errno("malloc");
2445 memcpy(*str, imsg.data, datalen);
2446 (*str)[datalen] = '\0';
2449 err = got_error(GOT_ERR_PRIVSEP_MSG);
2457 const struct got_error *
2458 got_privsep_recv_gotconfig_remotes(struct got_remote_repo **remotes,
2459 int *nremotes, struct imsgbuf *ibuf)
2461 const struct got_error *err = NULL;
2464 struct got_imsg_remotes iremotes;
2465 struct got_imsg_remote iremote;
2466 const size_t min_datalen =
2467 MIN(sizeof(struct got_imsg_error), sizeof(iremotes));
2471 iremotes.nremotes = 0;
2473 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2476 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2478 switch (imsg.hdr.type) {
2479 case GOT_IMSG_ERROR:
2480 if (datalen < sizeof(struct got_imsg_error)) {
2481 err = got_error(GOT_ERR_PRIVSEP_LEN);
2484 err = recv_imsg_error(&imsg, datalen);
2486 case GOT_IMSG_GOTCONFIG_REMOTES:
2487 if (datalen != sizeof(iremotes)) {
2488 err = got_error(GOT_ERR_PRIVSEP_LEN);
2491 memcpy(&iremotes, imsg.data, sizeof(iremotes));
2492 if (iremotes.nremotes == 0) {
2499 return got_error(GOT_ERR_PRIVSEP_MSG);
2504 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(**remotes));
2505 if (*remotes == NULL)
2506 return got_error_from_errno("recallocarray");
2508 while (*nremotes < iremotes.nremotes) {
2509 struct got_remote_repo *remote;
2510 const size_t min_datalen =
2511 MIN(sizeof(struct got_imsg_error), sizeof(iremote));
2514 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2517 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2519 switch (imsg.hdr.type) {
2520 case GOT_IMSG_ERROR:
2521 if (datalen < sizeof(struct got_imsg_error)) {
2522 err = got_error(GOT_ERR_PRIVSEP_LEN);
2525 err = recv_imsg_error(&imsg, datalen);
2527 case GOT_IMSG_GOTCONFIG_REMOTE:
2528 remote = &(*remotes)[*nremotes];
2529 memset(remote, 0, sizeof(*remote));
2530 if (datalen < sizeof(iremote)) {
2531 err = got_error(GOT_ERR_PRIVSEP_LEN);
2534 memcpy(&iremote, imsg.data, sizeof(iremote));
2535 if (iremote.name_len == 0 ||
2536 (iremote.fetch_url_len == 0 &&
2537 iremote.send_url_len == 0) ||
2538 (sizeof(iremote) + iremote.name_len +
2539 iremote.fetch_url_len + iremote.send_url_len) >
2541 err = got_error(GOT_ERR_PRIVSEP_LEN);
2544 remote->name = strndup(imsg.data + sizeof(iremote),
2546 if (remote->name == NULL) {
2547 err = got_error_from_errno("strndup");
2550 remote->fetch_url = strndup(imsg.data +
2551 sizeof(iremote) + iremote.name_len,
2552 iremote.fetch_url_len);
2553 if (remote->fetch_url == NULL) {
2554 err = got_error_from_errno("strndup");
2555 free_remote_data(remote);
2558 remote->send_url = strndup(imsg.data +
2559 sizeof(iremote) + iremote.name_len +
2560 iremote.fetch_url_len, iremote.send_url_len);
2561 if (remote->send_url == NULL) {
2562 err = got_error_from_errno("strndup");
2563 free_remote_data(remote);
2566 remote->mirror_references = iremote.mirror_references;
2567 remote->fetch_all_branches = iremote.fetch_all_branches;
2568 if (iremote.nfetch_branches > 0) {
2569 remote->fetch_branches = recallocarray(NULL, 0,
2570 iremote.nfetch_branches, sizeof(char *));
2571 if (remote->fetch_branches == NULL) {
2572 err = got_error_from_errno("calloc");
2573 free_remote_data(remote);
2577 remote->nfetch_branches = 0;
2578 for (i = 0; i < iremote.nfetch_branches; i++) {
2580 err = got_privsep_recv_gotconfig_str(&branch,
2583 free_remote_data(remote);
2586 remote->fetch_branches[i] = branch;
2587 remote->nfetch_branches++;
2589 if (iremote.nsend_branches > 0) {
2590 remote->send_branches = recallocarray(NULL, 0,
2591 iremote.nsend_branches, sizeof(char *));
2592 if (remote->send_branches == NULL) {
2593 err = got_error_from_errno("calloc");
2594 free_remote_data(remote);
2598 remote->nsend_branches = 0;
2599 for (i = 0; i < iremote.nsend_branches; i++) {
2601 err = got_privsep_recv_gotconfig_str(&branch,
2604 free_remote_data(remote);
2607 remote->send_branches[i] = branch;
2608 remote->nsend_branches++;
2610 if (iremote.nfetch_refs > 0) {
2611 remote->fetch_refs = recallocarray(NULL, 0,
2612 iremote.nfetch_refs, sizeof(char *));
2613 if (remote->fetch_refs == NULL) {
2614 err = got_error_from_errno("calloc");
2615 free_remote_data(remote);
2619 remote->nfetch_refs = 0;
2620 for (i = 0; i < iremote.nfetch_refs; i++) {
2622 err = got_privsep_recv_gotconfig_str(&ref,
2625 free_remote_data(remote);
2628 remote->fetch_refs[i] = ref;
2629 remote->nfetch_refs++;
2634 err = got_error(GOT_ERR_PRIVSEP_MSG);
2645 for (i = 0; i < *nremotes; i++)
2646 free_remote_data(&(*remotes)[i]);
2654 const struct got_error *
2655 got_privsep_send_commit_traversal_request(struct imsgbuf *ibuf,
2656 struct got_object_id *id, int idx, const char *path)
2659 size_t path_len = strlen(path) + 1;
2661 wbuf = imsg_create(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_REQUEST, 0, 0,
2662 sizeof(struct got_imsg_commit_traversal_request) + path_len);
2664 return got_error_from_errno(
2665 "imsg_create COMMIT_TRAVERSAL_REQUEST");
2666 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1)
2667 return got_error_from_errno("imsg_add "
2668 "COMMIT_TRAVERSAL_REQUEST");
2669 if (imsg_add(wbuf, &idx, sizeof(idx)) == -1)
2670 return got_error_from_errno("imsg_add "
2671 "COMMIT_TRAVERSAL_REQUEST");
2672 if (imsg_add(wbuf, path, path_len) == -1)
2673 return got_error_from_errno("imsg_add "
2674 "COMMIT_TRAVERSAL_REQUEST");
2677 imsg_close(ibuf, wbuf);
2679 return flush_imsg(ibuf);
2682 const struct got_error *
2683 got_privsep_recv_traversed_commits(struct got_commit_object **changed_commit,
2684 struct got_object_id **changed_commit_id,
2685 struct got_object_id_queue *commit_ids, struct imsgbuf *ibuf)
2687 const struct got_error *err = NULL;
2689 struct got_imsg_traversed_commits *icommits;
2693 *changed_commit = NULL;
2694 *changed_commit_id = NULL;
2697 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2701 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2702 switch (imsg.hdr.type) {
2703 case GOT_IMSG_TRAVERSED_COMMITS:
2704 icommits = imsg.data;
2705 if (datalen != sizeof(*icommits) +
2706 icommits->ncommits * SHA1_DIGEST_LENGTH) {
2707 err = got_error(GOT_ERR_PRIVSEP_LEN);
2710 for (i = 0; i < icommits->ncommits; i++) {
2711 struct got_object_qid *qid;
2712 uint8_t *sha1 = (uint8_t *)imsg.data +
2713 sizeof(*icommits) + i * SHA1_DIGEST_LENGTH;
2714 err = got_object_qid_alloc_partial(&qid);
2717 memcpy(qid->id.sha1, sha1, SHA1_DIGEST_LENGTH);
2718 STAILQ_INSERT_TAIL(commit_ids, qid, entry);
2720 /* The last commit may contain a change. */
2721 if (i == icommits->ncommits - 1) {
2722 *changed_commit_id =
2723 got_object_id_dup(&qid->id);
2724 if (*changed_commit_id == NULL) {
2725 err = got_error_from_errno(
2726 "got_object_id_dup");
2732 case GOT_IMSG_COMMIT:
2733 if (*changed_commit_id == NULL) {
2734 err = got_error(GOT_ERR_PRIVSEP_MSG);
2737 err = get_commit_from_imsg(changed_commit, &imsg,
2740 case GOT_IMSG_COMMIT_TRAVERSAL_DONE:
2744 err = got_error(GOT_ERR_PRIVSEP_MSG);
2754 got_object_id_queue_free(commit_ids);
2758 const struct got_error *
2759 got_privsep_send_enumerated_tree(size_t *totlen, struct imsgbuf *ibuf,
2760 struct got_object_id *tree_id, const char *path,
2761 struct got_parsed_tree_entry *entries, int nentries)
2763 const struct got_error *err = NULL;
2765 size_t path_len = strlen(path);
2768 msglen = sizeof(struct got_imsg_enumerated_tree) + path_len;
2769 wbuf = imsg_create(ibuf, GOT_IMSG_ENUMERATED_TREE, 0, 0, msglen);
2771 return got_error_from_errno("imsg_create ENUMERATED_TREE");
2773 if (imsg_add(wbuf, tree_id->sha1, SHA1_DIGEST_LENGTH) == -1)
2774 return got_error_from_errno("imsg_add ENUMERATED_TREE");
2775 if (imsg_add(wbuf, &nentries, sizeof(nentries)) == -1)
2776 return got_error_from_errno("imsg_add ENUMERATED_TREE");
2777 if (imsg_add(wbuf, path, path_len) == -1)
2778 return got_error_from_errno("imsg_add ENUMERATED_TREE");
2781 imsg_close(ibuf, wbuf);
2784 err = send_tree_entries(ibuf, entries, nentries);
2789 return flush_imsg(ibuf);
2792 const struct got_error *
2793 got_privsep_send_object_enumeration_request(struct imsgbuf *ibuf)
2795 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_ENUMERATION_REQUEST,
2796 0, 0, -1, NULL, 0) == -1)
2797 return got_error_from_errno("imsg_compose "
2798 "OBJECT_ENUMERATION_REQUEST");
2800 return flush_imsg(ibuf);
2803 const struct got_error *
2804 got_privsep_send_object_enumeration_done(struct imsgbuf *ibuf)
2806 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_ENUMERATION_DONE,
2807 0, 0, -1, NULL, 0) == -1)
2808 return got_error_from_errno("imsg_compose "
2809 "OBJECT_ENUMERATION_DONE");
2811 return flush_imsg(ibuf);
2814 const struct got_error *
2815 got_privsep_send_enumerated_commit(struct imsgbuf *ibuf,
2816 struct got_object_id *id, time_t mtime)
2820 wbuf = imsg_create(ibuf, GOT_IMSG_ENUMERATED_COMMIT, 0, 0,
2821 sizeof(struct got_imsg_enumerated_commit) + SHA1_DIGEST_LENGTH);
2823 return got_error_from_errno("imsg_create ENUMERATED_COMMIT");
2825 /* Keep in sync with struct got_imsg_enumerated_commit! */
2826 if (imsg_add(wbuf, id, SHA1_DIGEST_LENGTH) == -1)
2827 return got_error_from_errno("imsg_add ENUMERATED_COMMIT");
2828 if (imsg_add(wbuf, &mtime, sizeof(mtime)) == -1)
2829 return got_error_from_errno("imsg_add ENUMERATED_COMMIT");
2832 imsg_close(ibuf, wbuf);
2833 /* Don't flush yet, tree entries or ENUMERATION_DONE will follow. */
2837 const struct got_error *
2838 got_privsep_recv_enumerated_objects(struct imsgbuf *ibuf,
2839 got_object_enumerate_commit_cb cb_commit,
2840 got_object_enumerate_tree_cb cb_tree, void *cb_arg,
2841 struct got_repository *repo)
2843 const struct got_error *err = NULL;
2845 struct got_imsg_enumerated_commit *icommit = NULL;
2846 struct got_object_id commit_id;
2847 int have_commit = 0;
2849 struct got_tree_object tree;
2850 struct got_imsg_enumerated_tree *itree;
2851 struct got_object_id tree_id;
2852 char *path = NULL, *canon_path = NULL;
2853 size_t datalen, path_len;
2857 memset(&tree, 0, sizeof(tree));
2860 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2864 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2865 switch (imsg.hdr.type) {
2866 case GOT_IMSG_ENUMERATED_COMMIT:
2867 if (have_commit && nentries != -1) {
2868 err = got_error(GOT_ERR_PRIVSEP_MSG);
2871 if (datalen != sizeof(*icommit)) {
2872 err = got_error(GOT_ERR_PRIVSEP_LEN);
2875 icommit = (struct got_imsg_enumerated_commit *)imsg.data;
2876 memcpy(commit_id.sha1, icommit->id, SHA1_DIGEST_LENGTH);
2877 mtime = icommit->mtime;
2878 err = cb_commit(cb_arg, mtime, &commit_id, repo);
2883 case GOT_IMSG_ENUMERATED_TREE:
2884 /* Should be preceeded by GOT_IMSG_ENUMERATED_COMMIT. */
2886 err = got_error(GOT_ERR_PRIVSEP_MSG);
2889 if (datalen < sizeof(*itree)) {
2890 err = got_error(GOT_ERR_PRIVSEP_LEN);
2894 path_len = datalen - sizeof(*itree);
2895 if (path_len == 0) {
2896 err = got_error(GOT_ERR_PRIVSEP_LEN);
2899 memcpy(tree_id.sha1, itree->id, sizeof(tree_id.sha1));
2901 path = malloc(path_len + 1);
2903 err = got_error_from_errno("malloc");
2907 canon_path = malloc(path_len + 1);
2908 if (canon_path == NULL) {
2909 err = got_error_from_errno("malloc");
2912 memcpy(path, (uint8_t *)imsg.data + sizeof(*itree),
2914 path[path_len] = '\0';
2915 if (!got_path_is_absolute(path)) {
2916 err = got_error(GOT_ERR_BAD_PATH);
2919 if (got_path_is_root_dir(path)) {
2920 /* XXX check what got_canonpath() does wrong */
2921 canon_path[0] = '/';
2922 canon_path[1] = '\0';
2924 err = got_canonpath(path, canon_path,
2929 if (strcmp(path, canon_path) != 0) {
2930 err = got_error(GOT_ERR_BAD_PATH);
2933 if (nentries != -1) {
2934 err = got_error(GOT_ERR_PRIVSEP_MSG);
2937 if (itree->nentries < -1) {
2938 err = got_error(GOT_ERR_PRIVSEP_MSG);
2941 if (itree->nentries == -1) {
2942 /* Tree was not found in pack file. */
2943 err = cb_tree(cb_arg, NULL, mtime, &tree_id,
2947 if (itree->nentries > INT_MAX) {
2948 err = got_error(GOT_ERR_PRIVSEP_LEN);
2951 tree.entries = calloc(itree->nentries,
2952 sizeof(struct got_tree_entry));
2953 if (tree.entries == NULL) {
2954 err = got_error_from_errno("calloc");
2957 if (itree->nentries == 0) {
2958 err = cb_tree(cb_arg, &tree, mtime, &tree_id,
2963 /* Prepare for next tree. */
2965 memset(&tree, 0, sizeof(tree));
2968 tree.nentries = itree->nentries;
2972 case GOT_IMSG_TREE_ENTRIES:
2973 /* Should be preceeded by GOT_IMSG_ENUMERATED_TREE. */
2974 if (nentries <= -1) {
2975 err = got_error(GOT_ERR_PRIVSEP_MSG);
2978 err = recv_tree_entries(imsg.data, datalen,
2982 if (tree.nentries == nentries) {
2983 err = cb_tree(cb_arg, &tree, mtime, &tree_id,
2988 /* Prepare for next tree. */
2990 memset(&tree, 0, sizeof(tree));
2994 case GOT_IMSG_TREE_ENUMERATION_DONE:
2995 /* All trees have been found and traversed. */
2996 if (path == NULL || nentries != -1) {
2997 err = got_error(GOT_ERR_PRIVSEP_MSG);
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,