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>
35 #include "got_compat.h"
37 #include "got_object.h"
38 #include "got_error.h"
40 #include "got_repository.h"
42 #include "got_lib_sha1.h"
43 #include "got_lib_delta.h"
44 #include "got_lib_inflate.h"
45 #include "got_lib_object.h"
46 #include "got_lib_object_parse.h"
47 #include "got_lib_privsep.h"
48 #include "got_lib_pack.h"
51 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
55 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
58 static const struct got_error *
59 poll_fd(int fd, int events, int timeout)
67 pfd[0].events = events;
72 if (sigemptyset(&sigset) == -1)
73 return got_error_from_errno("sigemptyset");
74 if (sigaddset(&sigset, SIGWINCH) == -1)
75 return got_error_from_errno("sigaddset");
77 n = ppoll(pfd, 1, timeout == INFTIM ? NULL : &ts, &sigset);
79 return got_error_from_errno("ppoll");
81 return got_error(GOT_ERR_TIMEOUT);
82 if (pfd[0].revents & (POLLERR | POLLNVAL))
83 return got_error_from_errno("poll error");
84 if (pfd[0].revents & (events | POLLHUP))
87 return got_error(GOT_ERR_INTERRUPT);
90 static const struct got_error *
91 read_imsg(struct imsgbuf *ibuf)
93 const struct got_error *err;
96 err = poll_fd(ibuf->fd, POLLIN, INFTIM);
102 if (errno == EAGAIN) /* Could be a file-descriptor leak. */
103 return got_error(GOT_ERR_PRIVSEP_NO_FD);
104 return got_error(GOT_ERR_PRIVSEP_READ);
107 return got_error(GOT_ERR_PRIVSEP_PIPE);
112 const struct got_error *
113 got_privsep_wait_for_child(pid_t pid)
117 if (waitpid(pid, &child_status, 0) == -1)
118 return got_error_from_errno("waitpid");
120 if (!WIFEXITED(child_status))
121 return got_error(GOT_ERR_PRIVSEP_DIED);
123 if (WEXITSTATUS(child_status) != 0)
124 return got_error(GOT_ERR_PRIVSEP_EXIT);
129 static const struct got_error *
130 recv_imsg_error(struct imsg *imsg, size_t datalen)
132 struct got_imsg_error *ierr;
134 if (datalen != sizeof(*ierr))
135 return got_error(GOT_ERR_PRIVSEP_LEN);
138 if (ierr->code == GOT_ERR_ERRNO) {
139 static struct got_error serr;
140 serr.code = GOT_ERR_ERRNO;
141 serr.msg = strerror(ierr->errno_code);
145 return got_error(ierr->code);
148 const struct got_error *
149 got_privsep_recv_imsg(struct imsg *imsg, struct imsgbuf *ibuf,
152 const struct got_error *err;
155 n = imsg_get(ibuf, imsg);
157 return got_error_from_errno("imsg_get");
160 err = read_imsg(ibuf);
163 n = imsg_get(ibuf, imsg);
165 return got_error_from_errno("imsg_get");
168 if (imsg->hdr.len < IMSG_HEADER_SIZE + min_datalen)
169 return got_error(GOT_ERR_PRIVSEP_LEN);
171 if (imsg->hdr.type == GOT_IMSG_ERROR) {
172 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
173 return recv_imsg_error(imsg, datalen);
179 /* Attempt to send an error in an imsg. Complain on stderr as a last resort. */
181 got_privsep_send_error(struct imsgbuf *ibuf, const struct got_error *err)
183 const struct got_error *poll_err;
184 struct got_imsg_error ierr;
187 ierr.code = err->code;
188 if (err->code == GOT_ERR_ERRNO)
189 ierr.errno_code = errno;
192 ret = imsg_compose(ibuf, GOT_IMSG_ERROR, 0, 0, -1, &ierr, sizeof(ierr));
194 fprintf(stderr, "%s: error %d \"%s\": imsg_compose: %s\n",
195 getprogname(), err->code, err->msg, strerror(errno));
199 poll_err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
201 fprintf(stderr, "%s: error %d \"%s\": poll: %s\n",
202 getprogname(), err->code, err->msg, poll_err->msg);
206 ret = imsg_flush(ibuf);
208 fprintf(stderr, "%s: error %d \"%s\": imsg_flush: %s\n",
209 getprogname(), err->code, err->msg, strerror(errno));
215 static const struct got_error *
216 flush_imsg(struct imsgbuf *ibuf)
218 const struct got_error *err;
220 err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
224 if (imsg_flush(ibuf) == -1) {
226 return got_error_from_errno("imsg_flush");
232 const struct got_error *
233 got_privsep_flush_imsg(struct imsgbuf *ibuf)
235 return flush_imsg(ibuf);
238 const struct got_error *
239 got_privsep_send_stop(int fd)
241 const struct got_error *err = NULL;
244 imsg_init(&ibuf, fd);
246 if (imsg_compose(&ibuf, GOT_IMSG_STOP, 0, 0, -1, NULL, 0) == -1)
247 return got_error_from_errno("imsg_compose STOP");
249 err = flush_imsg(&ibuf);
253 const struct got_error *
254 got_privsep_send_obj_req(struct imsgbuf *ibuf, int fd,
255 struct got_object_id *id)
257 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_REQUEST, 0, 0, fd,
258 id, sizeof(*id)) == -1)
259 return got_error_from_errno("imsg_compose OBJECT_REQUEST");
261 return flush_imsg(ibuf);
264 const struct got_error *
265 got_privsep_send_raw_obj_req(struct imsgbuf *ibuf, int fd,
266 struct got_object_id *id)
268 if (imsg_compose(ibuf, GOT_IMSG_RAW_OBJECT_REQUEST, 0, 0, fd,
269 id, sizeof(*id)) == -1)
270 return got_error_from_errno("imsg_compose RAW_OBJECT_REQUEST");
272 return flush_imsg(ibuf);
275 const struct got_error *
276 got_privsep_send_raw_obj_outfd(struct imsgbuf *ibuf, int outfd)
278 const struct got_error *err = NULL;
280 if (imsg_compose(ibuf, GOT_IMSG_RAW_OBJECT_OUTFD, 0, 0, outfd, NULL, 0)
282 err = got_error_from_errno("imsg_compose RAW_OBJECT_OUTFD");
287 return flush_imsg(ibuf);
290 const struct got_error *
291 got_privsep_send_raw_obj(struct imsgbuf *ibuf, off_t size, size_t hdrlen,
294 const struct got_error *err = NULL;
295 struct got_imsg_raw_obj iobj;
296 size_t len = sizeof(iobj);
299 iobj.hdrlen = hdrlen;
302 if (data && size + hdrlen <= GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX)
303 len += (size_t)size + hdrlen;
305 wbuf = imsg_create(ibuf, GOT_IMSG_RAW_OBJECT, 0, 0, len);
307 err = got_error_from_errno("imsg_create RAW_OBJECT");
311 if (imsg_add(wbuf, &iobj, sizeof(iobj)) == -1) {
312 err = got_error_from_errno("imsg_add RAW_OBJECT");
317 if (data && size + hdrlen <= GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX) {
318 if (imsg_add(wbuf, data, size + hdrlen) == -1) {
319 err = got_error_from_errno("imsg_add RAW_OBJECT");
326 imsg_close(ibuf, wbuf);
328 return flush_imsg(ibuf);
331 const struct got_error *
332 got_privsep_recv_raw_obj(uint8_t **outbuf, off_t *size, size_t *hdrlen,
333 struct imsgbuf *ibuf)
335 const struct got_error *err = NULL;
337 struct got_imsg_raw_obj *iobj;
342 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
346 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
348 switch (imsg.hdr.type) {
349 case GOT_IMSG_RAW_OBJECT:
350 if (datalen < sizeof(*iobj)) {
351 err = got_error(GOT_ERR_PRIVSEP_LEN);
356 *hdrlen = iobj->hdrlen;
358 if (datalen == sizeof(*iobj)) {
359 /* Data has been written to file descriptor. */
363 if (*size + *hdrlen > GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX) {
364 err = got_error(GOT_ERR_PRIVSEP_LEN);
368 *outbuf = malloc(*size + *hdrlen);
369 if (*outbuf == NULL) {
370 err = got_error_from_errno("malloc");
373 memcpy(*outbuf, imsg.data + sizeof(*iobj), *size + *hdrlen);
376 err = got_error(GOT_ERR_PRIVSEP_MSG);
385 const struct got_error *
386 got_privsep_send_commit_req(struct imsgbuf *ibuf, int fd,
387 struct got_object_id *id, int pack_idx)
389 const struct got_error *err = NULL;
390 struct got_imsg_packed_object iobj;
394 if (pack_idx != -1) { /* commit is packed */
396 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
404 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_REQUEST, 0, 0, fd, data, len)
406 err = got_error_from_errno("imsg_compose COMMIT_REQUEST");
411 return flush_imsg(ibuf);
414 const struct got_error *
415 got_privsep_send_tree_req(struct imsgbuf *ibuf, int fd,
416 struct got_object_id *id, int pack_idx)
418 const struct got_error *err = NULL;
423 len = sizeof(struct got_imsg_packed_object);
427 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_REQUEST, 0, 0, len);
429 return got_error_from_errno("imsg_create TREE_REQUEST");
431 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
432 err = got_error_from_errno("imsg_add TREE_REQUEST");
437 if (pack_idx != -1) { /* tree is packed */
438 if (imsg_add(wbuf, &pack_idx, sizeof(pack_idx)) == -1) {
439 err = got_error_from_errno("imsg_add TREE_REQUEST");
446 imsg_close(ibuf, wbuf);
448 return flush_imsg(ibuf);
451 const struct got_error *
452 got_privsep_send_tag_req(struct imsgbuf *ibuf, int fd,
453 struct got_object_id *id, int pack_idx)
455 struct got_imsg_packed_object iobj;
459 if (pack_idx != -1) { /* tag is packed */
461 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
469 if (imsg_compose(ibuf, GOT_IMSG_TAG_REQUEST, 0, 0, fd, data, len)
471 return got_error_from_errno("imsg_compose TAG_REQUEST");
473 return flush_imsg(ibuf);
476 const struct got_error *
477 got_privsep_send_blob_req(struct imsgbuf *ibuf, int infd,
478 struct got_object_id *id, int pack_idx)
480 const struct got_error *err = NULL;
481 struct got_imsg_packed_object iobj;
485 if (pack_idx != -1) { /* blob is packed */
487 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
495 if (imsg_compose(ibuf, GOT_IMSG_BLOB_REQUEST, 0, 0, infd, data, len)
497 err = got_error_from_errno("imsg_compose BLOB_REQUEST");
502 return flush_imsg(ibuf);
505 const struct got_error *
506 got_privsep_send_blob_outfd(struct imsgbuf *ibuf, int outfd)
508 const struct got_error *err = NULL;
510 if (imsg_compose(ibuf, GOT_IMSG_BLOB_OUTFD, 0, 0, outfd, NULL, 0)
512 err = got_error_from_errno("imsg_compose BLOB_OUTFD");
517 return flush_imsg(ibuf);
520 static const struct got_error *
521 send_fd(struct imsgbuf *ibuf, int imsg_code, int fd)
523 const struct got_error *err = NULL;
525 if (imsg_compose(ibuf, imsg_code, 0, 0, fd, NULL, 0) == -1) {
526 err = got_error_from_errno("imsg_compose TMPFD");
531 return flush_imsg(ibuf);
534 const struct got_error *
535 got_privsep_send_tmpfd(struct imsgbuf *ibuf, int fd)
537 return send_fd(ibuf, GOT_IMSG_TMPFD, fd);
540 const struct got_error *
541 got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj)
543 struct got_imsg_object iobj;
545 memcpy(iobj.id, obj->id.sha1, sizeof(iobj.id));
546 iobj.type = obj->type;
547 iobj.flags = obj->flags;
548 iobj.hdrlen = obj->hdrlen;
549 iobj.size = obj->size;
550 if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
551 iobj.pack_offset = obj->pack_offset;
552 iobj.pack_idx = obj->pack_idx;
555 if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
557 return got_error_from_errno("imsg_compose OBJECT");
559 return flush_imsg(ibuf);
562 const struct got_error *
563 got_privsep_send_fetch_req(struct imsgbuf *ibuf, int fd,
564 struct got_pathlist_head *have_refs, int fetch_all_branches,
565 struct got_pathlist_head *wanted_branches,
566 struct got_pathlist_head *wanted_refs, int list_refs_only, int verbosity)
568 const struct got_error *err = NULL;
571 struct got_pathlist_entry *pe;
572 struct got_imsg_fetch_request fetchreq;
574 memset(&fetchreq, 0, sizeof(fetchreq));
575 fetchreq.fetch_all_branches = fetch_all_branches;
576 fetchreq.list_refs_only = list_refs_only;
577 fetchreq.verbosity = verbosity;
578 TAILQ_FOREACH(pe, have_refs, entry)
579 fetchreq.n_have_refs++;
580 TAILQ_FOREACH(pe, wanted_branches, entry)
581 fetchreq.n_wanted_branches++;
582 TAILQ_FOREACH(pe, wanted_refs, entry)
583 fetchreq.n_wanted_refs++;
584 len = sizeof(struct got_imsg_fetch_request);
585 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
587 return got_error(GOT_ERR_NO_SPACE);
590 if (imsg_compose(ibuf, GOT_IMSG_FETCH_REQUEST, 0, 0, fd,
591 &fetchreq, sizeof(fetchreq)) == -1)
592 return got_error_from_errno(
593 "imsg_compose FETCH_SERVER_PROGRESS");
595 err = flush_imsg(ibuf);
602 TAILQ_FOREACH(pe, have_refs, entry) {
603 const char *name = pe->path;
604 size_t name_len = pe->path_len;
605 struct got_object_id *id = pe->data;
607 len = sizeof(struct got_imsg_fetch_have_ref) + name_len;
608 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_HAVE_REF, 0, 0, len);
610 return got_error_from_errno("imsg_create FETCH_HAVE_REF");
612 /* Keep in sync with struct got_imsg_fetch_have_ref! */
613 if (imsg_add(wbuf, id->sha1, sizeof(id->sha1)) == -1) {
614 err = got_error_from_errno("imsg_add FETCH_HAVE_REF");
618 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
619 err = got_error_from_errno("imsg_add FETCH_HAVE_REF");
623 if (imsg_add(wbuf, name, name_len) == -1) {
624 err = got_error_from_errno("imsg_add FETCH_HAVE_REF");
630 imsg_close(ibuf, wbuf);
631 err = flush_imsg(ibuf);
636 TAILQ_FOREACH(pe, wanted_branches, entry) {
637 const char *name = pe->path;
638 size_t name_len = pe->path_len;
640 len = sizeof(struct got_imsg_fetch_wanted_branch) + name_len;
641 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_WANTED_BRANCH, 0, 0,
644 return got_error_from_errno(
645 "imsg_create FETCH_WANTED_BRANCH");
647 /* Keep in sync with struct got_imsg_fetch_wanted_branch! */
648 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
649 err = got_error_from_errno(
650 "imsg_add FETCH_WANTED_BRANCH");
654 if (imsg_add(wbuf, name, name_len) == -1) {
655 err = got_error_from_errno(
656 "imsg_add FETCH_WANTED_BRANCH");
662 imsg_close(ibuf, wbuf);
663 err = flush_imsg(ibuf);
668 TAILQ_FOREACH(pe, wanted_refs, entry) {
669 const char *name = pe->path;
670 size_t name_len = pe->path_len;
672 len = sizeof(struct got_imsg_fetch_wanted_ref) + name_len;
673 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_WANTED_REF, 0, 0,
676 return got_error_from_errno(
677 "imsg_create FETCH_WANTED_REF");
679 /* Keep in sync with struct got_imsg_fetch_wanted_ref! */
680 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
681 err = got_error_from_errno(
682 "imsg_add FETCH_WANTED_REF");
686 if (imsg_add(wbuf, name, name_len) == -1) {
687 err = got_error_from_errno(
688 "imsg_add FETCH_WANTED_REF");
694 imsg_close(ibuf, wbuf);
695 err = flush_imsg(ibuf);
705 const struct got_error *
706 got_privsep_send_fetch_outfd(struct imsgbuf *ibuf, int fd)
708 return send_fd(ibuf, GOT_IMSG_FETCH_OUTFD, fd);
711 const struct got_error *
712 got_privsep_recv_fetch_progress(int *done, struct got_object_id **id,
713 char **refname, struct got_pathlist_head *symrefs, char **server_progress,
714 off_t *packfile_size, uint8_t *pack_sha1, struct imsgbuf *ibuf)
716 const struct got_error *err = NULL;
719 struct got_imsg_fetch_symrefs *isymrefs = NULL;
727 *server_progress = NULL;
729 memset(pack_sha1, 0, SHA1_DIGEST_LENGTH);
731 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
735 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
736 switch (imsg.hdr.type) {
738 if (datalen < sizeof(struct got_imsg_error)) {
739 err = got_error(GOT_ERR_PRIVSEP_LEN);
742 err = recv_imsg_error(&imsg, datalen);
744 case GOT_IMSG_FETCH_SYMREFS:
745 if (datalen < sizeof(*isymrefs)) {
746 err = got_error(GOT_ERR_PRIVSEP_LEN);
749 if (isymrefs != NULL) {
750 err = got_error(GOT_ERR_PRIVSEP_MSG);
753 isymrefs = (struct got_imsg_fetch_symrefs *)imsg.data;
754 off = sizeof(*isymrefs);
755 remain = datalen - off;
756 for (n = 0; n < isymrefs->nsymrefs; n++) {
757 struct got_imsg_fetch_symref *s;
759 if (remain < sizeof(struct got_imsg_fetch_symref)) {
760 err = got_error(GOT_ERR_PRIVSEP_LEN);
763 s = (struct got_imsg_fetch_symref *)(imsg.data + off);
765 remain -= sizeof(*s);
766 if (remain < s->name_len) {
767 err = got_error(GOT_ERR_PRIVSEP_LEN);
770 name = strndup(imsg.data + off, s->name_len);
772 err = got_error_from_errno("strndup");
776 remain -= s->name_len;
777 if (remain < s->target_len) {
778 err = got_error(GOT_ERR_PRIVSEP_LEN);
782 target = strndup(imsg.data + off, s->target_len);
783 if (target == NULL) {
784 err = got_error_from_errno("strndup");
788 off += s->target_len;
789 remain -= s->target_len;
790 err = got_pathlist_append(symrefs, name, target);
798 case GOT_IMSG_FETCH_REF:
799 if (datalen <= SHA1_DIGEST_LENGTH) {
800 err = got_error(GOT_ERR_PRIVSEP_MSG);
803 *id = malloc(sizeof(**id));
805 err = got_error_from_errno("malloc");
808 memcpy((*id)->sha1, imsg.data, SHA1_DIGEST_LENGTH);
809 *refname = strndup(imsg.data + SHA1_DIGEST_LENGTH,
810 datalen - SHA1_DIGEST_LENGTH);
811 if (*refname == NULL) {
812 err = got_error_from_errno("strndup");
816 case GOT_IMSG_FETCH_SERVER_PROGRESS:
818 err = got_error(GOT_ERR_PRIVSEP_LEN);
821 *server_progress = strndup(imsg.data, datalen);
822 if (*server_progress == NULL) {
823 err = got_error_from_errno("strndup");
826 for (i = 0; i < datalen; i++) {
827 if (!isprint((unsigned char)(*server_progress)[i]) &&
828 !isspace((unsigned char)(*server_progress)[i])) {
829 err = got_error(GOT_ERR_PRIVSEP_MSG);
830 free(*server_progress);
831 *server_progress = NULL;
836 case GOT_IMSG_FETCH_DOWNLOAD_PROGRESS:
837 if (datalen < sizeof(*packfile_size)) {
838 err = got_error(GOT_ERR_PRIVSEP_MSG);
841 memcpy(packfile_size, imsg.data, sizeof(*packfile_size));
843 case GOT_IMSG_FETCH_DONE:
844 if (datalen != SHA1_DIGEST_LENGTH) {
845 err = got_error(GOT_ERR_PRIVSEP_MSG);
848 memcpy(pack_sha1, imsg.data, SHA1_DIGEST_LENGTH);
852 err = got_error(GOT_ERR_PRIVSEP_MSG);
866 static const struct got_error *
867 send_send_ref(const char *name, size_t name_len, struct got_object_id *id,
868 int delete, struct imsgbuf *ibuf)
870 const struct got_error *err = NULL;
874 len = sizeof(struct got_imsg_send_ref) + name_len;
875 wbuf = imsg_create(ibuf, GOT_IMSG_SEND_REF, 0, 0, len);
877 return got_error_from_errno("imsg_create SEND_REF");
879 /* Keep in sync with struct got_imsg_send_ref! */
880 if (imsg_add(wbuf, id->sha1, sizeof(id->sha1)) == -1) {
881 err = got_error_from_errno("imsg_add SEND_REF");
885 if (imsg_add(wbuf, &delete, sizeof(delete)) == -1) {
886 err = got_error_from_errno("imsg_add SEND_REF");
890 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
891 err = got_error_from_errno("imsg_add SEND_REF");
895 if (imsg_add(wbuf, name, name_len) == -1) {
896 err = got_error_from_errno("imsg_add SEND_REF");
902 imsg_close(ibuf, wbuf);
903 return flush_imsg(ibuf);
906 const struct got_error *
907 got_privsep_send_send_req(struct imsgbuf *ibuf, int fd,
908 struct got_pathlist_head *have_refs,
909 struct got_pathlist_head *delete_refs,
912 const struct got_error *err = NULL;
913 struct got_pathlist_entry *pe;
914 struct got_imsg_send_request sendreq;
915 struct got_object_id zero_id;
917 memset(&zero_id, 0, sizeof(zero_id));
918 memset(&sendreq, 0, sizeof(sendreq));
919 sendreq.verbosity = verbosity;
920 TAILQ_FOREACH(pe, have_refs, entry)
922 TAILQ_FOREACH(pe, delete_refs, entry)
924 if (imsg_compose(ibuf, GOT_IMSG_SEND_REQUEST, 0, 0, fd,
925 &sendreq, sizeof(sendreq)) == -1) {
926 err = got_error_from_errno(
927 "imsg_compose FETCH_SERVER_PROGRESS");
931 err = flush_imsg(ibuf);
936 TAILQ_FOREACH(pe, have_refs, entry) {
937 const char *name = pe->path;
938 size_t name_len = pe->path_len;
939 struct got_object_id *id = pe->data;
940 err = send_send_ref(name, name_len, id, 0, ibuf);
945 TAILQ_FOREACH(pe, delete_refs, entry) {
946 const char *name = pe->path;
947 size_t name_len = pe->path_len;
948 err = send_send_ref(name, name_len, &zero_id, 1, ibuf);
953 if (fd != -1 && close(fd) == -1 && err == NULL)
954 err = got_error_from_errno("close");
959 const struct got_error *
960 got_privsep_recv_send_remote_refs(struct got_pathlist_head *remote_refs,
961 struct imsgbuf *ibuf)
963 const struct got_error *err = NULL;
967 struct got_imsg_send_remote_ref iremote_ref;
968 struct got_object_id *id = NULL;
969 char *refname = NULL;
970 struct got_pathlist_entry *new;
973 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
976 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
977 switch (imsg.hdr.type) {
979 if (datalen < sizeof(struct got_imsg_error)) {
980 err = got_error(GOT_ERR_PRIVSEP_LEN);
983 err = recv_imsg_error(&imsg, datalen);
985 case GOT_IMSG_SEND_REMOTE_REF:
986 if (datalen < sizeof(iremote_ref)) {
987 err = got_error(GOT_ERR_PRIVSEP_MSG);
990 memcpy(&iremote_ref, imsg.data, sizeof(iremote_ref));
991 if (datalen != sizeof(iremote_ref) +
992 iremote_ref.name_len) {
993 err = got_error(GOT_ERR_PRIVSEP_MSG);
996 id = malloc(sizeof(*id));
998 err = got_error_from_errno("malloc");
1001 memcpy(id->sha1, iremote_ref.id, SHA1_DIGEST_LENGTH);
1002 refname = strndup(imsg.data + sizeof(iremote_ref),
1003 datalen - sizeof(iremote_ref));
1004 if (refname == NULL) {
1005 err = got_error_from_errno("strndup");
1008 err = got_pathlist_insert(&new, remote_refs,
1012 if (new == NULL) { /* duplicate which wasn't inserted */
1019 case GOT_IMSG_SEND_PACK_REQUEST:
1021 err = got_error(GOT_ERR_PRIVSEP_MSG);
1024 /* got-send-pack is now waiting for a pack file. */
1028 err = got_error(GOT_ERR_PRIVSEP_MSG);
1039 const struct got_error *
1040 got_privsep_send_packfd(struct imsgbuf *ibuf, int fd)
1042 return send_fd(ibuf, GOT_IMSG_SEND_PACKFD, fd);
1045 const struct got_error *
1046 got_privsep_recv_send_progress(int *done, off_t *bytes_sent,
1047 int *success, char **refname, struct imsgbuf *ibuf)
1049 const struct got_error *err = NULL;
1052 struct got_imsg_send_ref_status iref_status;
1054 /* Do not reset the current value of 'bytes_sent', it accumulates. */
1059 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1063 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1064 switch (imsg.hdr.type) {
1065 case GOT_IMSG_ERROR:
1066 if (datalen < sizeof(struct got_imsg_error)) {
1067 err = got_error(GOT_ERR_PRIVSEP_LEN);
1070 err = recv_imsg_error(&imsg, datalen);
1072 case GOT_IMSG_SEND_UPLOAD_PROGRESS:
1073 if (datalen < sizeof(*bytes_sent)) {
1074 err = got_error(GOT_ERR_PRIVSEP_MSG);
1077 memcpy(bytes_sent, imsg.data, sizeof(*bytes_sent));
1079 case GOT_IMSG_SEND_REF_STATUS:
1080 if (datalen < sizeof(iref_status)) {
1081 err = got_error(GOT_ERR_PRIVSEP_MSG);
1084 memcpy(&iref_status, imsg.data, sizeof(iref_status));
1085 if (datalen != sizeof(iref_status) + iref_status.name_len) {
1086 err = got_error(GOT_ERR_PRIVSEP_MSG);
1089 *success = iref_status.success;
1090 *refname = strndup(imsg.data + sizeof(iref_status),
1091 iref_status.name_len);
1093 case GOT_IMSG_SEND_DONE:
1095 err = got_error(GOT_ERR_PRIVSEP_MSG);
1101 err = got_error(GOT_ERR_PRIVSEP_MSG);
1109 const struct got_error *
1110 got_privsep_send_index_pack_req(struct imsgbuf *ibuf, uint8_t *pack_sha1,
1113 const struct got_error *err = NULL;
1115 /* Keep in sync with struct got_imsg_index_pack_request */
1116 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_REQUEST, 0, 0, fd,
1117 pack_sha1, SHA1_DIGEST_LENGTH) == -1) {
1118 err = got_error_from_errno("imsg_compose INDEX_REQUEST");
1122 return flush_imsg(ibuf);
1125 const struct got_error *
1126 got_privsep_send_index_pack_outfd(struct imsgbuf *ibuf, int fd)
1128 return send_fd(ibuf, GOT_IMSG_IDXPACK_OUTFD, fd);
1131 const struct got_error *
1132 got_privsep_recv_index_progress(int *done, int *nobj_total,
1133 int *nobj_indexed, int *nobj_loose, int *nobj_resolved,
1134 struct imsgbuf *ibuf)
1136 const struct got_error *err = NULL;
1138 struct got_imsg_index_pack_progress *iprogress;
1146 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1150 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1151 switch (imsg.hdr.type) {
1152 case GOT_IMSG_ERROR:
1153 if (datalen < sizeof(struct got_imsg_error)) {
1154 err = got_error(GOT_ERR_PRIVSEP_LEN);
1157 err = recv_imsg_error(&imsg, datalen);
1159 case GOT_IMSG_IDXPACK_PROGRESS:
1160 if (datalen < sizeof(*iprogress)) {
1161 err = got_error(GOT_ERR_PRIVSEP_LEN);
1164 iprogress = (struct got_imsg_index_pack_progress *)imsg.data;
1165 *nobj_total = iprogress->nobj_total;
1166 *nobj_indexed = iprogress->nobj_indexed;
1167 *nobj_loose = iprogress->nobj_loose;
1168 *nobj_resolved = iprogress->nobj_resolved;
1170 case GOT_IMSG_IDXPACK_DONE:
1172 err = got_error(GOT_ERR_PRIVSEP_LEN);
1178 err = got_error(GOT_ERR_PRIVSEP_MSG);
1186 const struct got_error *
1187 got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
1188 struct imsgbuf *ibuf)
1190 struct got_imsg_object *iobj;
1191 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1193 if (datalen != sizeof(*iobj))
1194 return got_error(GOT_ERR_PRIVSEP_LEN);
1197 *obj = calloc(1, sizeof(**obj));
1199 return got_error_from_errno("calloc");
1201 memcpy((*obj)->id.sha1, iobj->id, SHA1_DIGEST_LENGTH);
1202 (*obj)->type = iobj->type;
1203 (*obj)->flags = iobj->flags;
1204 (*obj)->hdrlen = iobj->hdrlen;
1205 (*obj)->size = iobj->size;
1206 /* path_packfile is handled by caller */
1207 if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
1208 (*obj)->pack_offset = iobj->pack_offset;
1209 (*obj)->pack_idx = iobj->pack_idx;
1211 STAILQ_INIT(&(*obj)->deltas.entries);
1215 const struct got_error *
1216 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
1218 const struct got_error *err = NULL;
1220 const size_t min_datalen =
1221 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
1225 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1229 switch (imsg.hdr.type) {
1230 case GOT_IMSG_OBJECT:
1231 err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
1234 err = got_error(GOT_ERR_PRIVSEP_MSG);
1243 static const struct got_error *
1244 send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
1247 const struct got_error *err = NULL;
1248 size_t offset, remain;
1251 remain = logmsg_len;
1252 while (remain > 0) {
1253 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1255 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
1256 commit->logmsg + offset, n) == -1) {
1257 err = got_error_from_errno("imsg_compose "
1262 err = flush_imsg(ibuf);
1273 const struct got_error *
1274 got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
1276 const struct got_error *err = NULL;
1277 struct got_imsg_commit_object *icommit;
1280 struct got_object_qid *qid;
1281 size_t author_len = strlen(commit->author);
1282 size_t committer_len = strlen(commit->committer);
1283 size_t logmsg_len = strlen(commit->logmsg);
1285 total = sizeof(*icommit) + author_len + committer_len +
1286 commit->nparents * SHA1_DIGEST_LENGTH;
1288 buf = malloc(total);
1290 return got_error_from_errno("malloc");
1292 icommit = (struct got_imsg_commit_object *)buf;
1293 memcpy(icommit->tree_id, commit->tree_id->sha1,
1294 sizeof(icommit->tree_id));
1295 icommit->author_len = author_len;
1296 icommit->author_time = commit->author_time;
1297 icommit->author_gmtoff = commit->author_gmtoff;
1298 icommit->committer_len = committer_len;
1299 icommit->committer_time = commit->committer_time;
1300 icommit->committer_gmtoff = commit->committer_gmtoff;
1301 icommit->logmsg_len = logmsg_len;
1302 icommit->nparents = commit->nparents;
1304 len = sizeof(*icommit);
1305 memcpy(buf + len, commit->author, author_len);
1307 memcpy(buf + len, commit->committer, committer_len);
1308 len += committer_len;
1309 STAILQ_FOREACH(qid, &commit->parent_ids, entry) {
1310 memcpy(buf + len, &qid->id, SHA1_DIGEST_LENGTH);
1311 len += SHA1_DIGEST_LENGTH;
1314 if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
1315 err = got_error_from_errno("imsg_compose COMMIT");
1319 if (logmsg_len == 0 ||
1320 logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1321 err = flush_imsg(ibuf);
1325 err = send_commit_logmsg(ibuf, commit, logmsg_len);
1331 static const struct got_error *
1332 get_commit_from_imsg(struct got_commit_object **commit,
1333 struct imsg *imsg, size_t datalen, struct imsgbuf *ibuf)
1335 const struct got_error *err = NULL;
1336 struct got_imsg_commit_object *icommit;
1340 if (datalen < sizeof(*icommit))
1341 return got_error(GOT_ERR_PRIVSEP_LEN);
1343 icommit = imsg->data;
1344 if (datalen != sizeof(*icommit) + icommit->author_len +
1345 icommit->committer_len +
1346 icommit->nparents * SHA1_DIGEST_LENGTH)
1347 return got_error(GOT_ERR_PRIVSEP_LEN);
1349 if (icommit->nparents < 0)
1350 return got_error(GOT_ERR_PRIVSEP_LEN);
1352 len += sizeof(*icommit);
1354 *commit = got_object_commit_alloc_partial();
1355 if (*commit == NULL)
1356 return got_error_from_errno(
1357 "got_object_commit_alloc_partial");
1359 memcpy((*commit)->tree_id->sha1, icommit->tree_id,
1360 SHA1_DIGEST_LENGTH);
1361 (*commit)->author_time = icommit->author_time;
1362 (*commit)->author_gmtoff = icommit->author_gmtoff;
1363 (*commit)->committer_time = icommit->committer_time;
1364 (*commit)->committer_gmtoff = icommit->committer_gmtoff;
1366 if (icommit->author_len == 0) {
1367 (*commit)->author = strdup("");
1368 if ((*commit)->author == NULL) {
1369 err = got_error_from_errno("strdup");
1373 (*commit)->author = malloc(icommit->author_len + 1);
1374 if ((*commit)->author == NULL) {
1375 err = got_error_from_errno("malloc");
1378 memcpy((*commit)->author, imsg->data + len,
1379 icommit->author_len);
1380 (*commit)->author[icommit->author_len] = '\0';
1382 len += icommit->author_len;
1384 if (icommit->committer_len == 0) {
1385 (*commit)->committer = strdup("");
1386 if ((*commit)->committer == NULL) {
1387 err = got_error_from_errno("strdup");
1391 (*commit)->committer =
1392 malloc(icommit->committer_len + 1);
1393 if ((*commit)->committer == NULL) {
1394 err = got_error_from_errno("malloc");
1397 memcpy((*commit)->committer, imsg->data + len,
1398 icommit->committer_len);
1399 (*commit)->committer[icommit->committer_len] = '\0';
1401 len += icommit->committer_len;
1403 if (icommit->logmsg_len == 0) {
1404 (*commit)->logmsg = strdup("");
1405 if ((*commit)->logmsg == NULL) {
1406 err = got_error_from_errno("strdup");
1410 size_t offset = 0, remain = icommit->logmsg_len;
1412 (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
1413 if ((*commit)->logmsg == NULL) {
1414 err = got_error_from_errno("malloc");
1417 while (remain > 0) {
1418 struct imsg imsg_log;
1419 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1422 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1426 if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG) {
1427 err = got_error(GOT_ERR_PRIVSEP_MSG);
1431 memcpy((*commit)->logmsg + offset,
1433 imsg_free(&imsg_log);
1437 (*commit)->logmsg[icommit->logmsg_len] = '\0';
1440 for (i = 0; i < icommit->nparents; i++) {
1441 struct got_object_qid *qid;
1443 err = got_object_qid_alloc_partial(&qid);
1446 memcpy(&qid->id, imsg->data + len +
1447 i * SHA1_DIGEST_LENGTH, sizeof(qid->id));
1448 STAILQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
1449 (*commit)->nparents++;
1453 got_object_commit_close(*commit);
1459 const struct got_error *
1460 got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
1462 const struct got_error *err = NULL;
1465 const size_t min_datalen =
1466 MIN(sizeof(struct got_imsg_error),
1467 sizeof(struct got_imsg_commit_object));
1471 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1475 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1477 switch (imsg.hdr.type) {
1478 case GOT_IMSG_COMMIT:
1479 err = get_commit_from_imsg(commit, &imsg, datalen, ibuf);
1482 err = got_error(GOT_ERR_PRIVSEP_MSG);
1491 static const struct got_error *
1492 send_tree_entries(struct imsgbuf *ibuf, struct got_parsed_tree_entry *entries,
1493 int idx0, int idxN, size_t len)
1495 static const struct got_error *err;
1497 struct got_imsg_tree_entries ientries;
1500 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_ENTRIES, 0, 0, len);
1502 return got_error_from_errno("imsg_create TREE_ENTRY");
1504 ientries.nentries = idxN - idx0 + 1;
1505 if (imsg_add(wbuf, &ientries, sizeof(ientries)) == -1) {
1506 err = got_error_from_errno("imsg_add TREE_ENTRY");
1511 for (i = idx0; i <= idxN; i++) {
1512 struct got_parsed_tree_entry *pte = &entries[i];
1514 /* Keep in sync with struct got_imsg_tree_object definition! */
1515 if (imsg_add(wbuf, pte->id, SHA1_DIGEST_LENGTH) == -1) {
1516 err = got_error_from_errno("imsg_add TREE_ENTRY");
1520 if (imsg_add(wbuf, &pte->mode, sizeof(pte->mode)) == -1) {
1521 err = got_error_from_errno("imsg_add TREE_ENTRY");
1525 if (imsg_add(wbuf, &pte->namelen, sizeof(pte->namelen)) == -1) {
1526 err = got_error_from_errno("imsg_add TREE_ENTRY");
1531 /* Remaining bytes are the entry's name. */
1532 if (imsg_add(wbuf, pte->name, pte->namelen) == -1) {
1533 err = got_error_from_errno("imsg_add TREE_ENTRY");
1540 imsg_close(ibuf, wbuf);
1544 const struct got_error *
1545 got_privsep_send_tree(struct imsgbuf *ibuf,
1546 struct got_parsed_tree_entry *entries, int nentries)
1548 const struct got_error *err = NULL;
1549 struct got_imsg_tree_object itree;
1553 itree.nentries = nentries;
1554 if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
1556 return got_error_from_errno("imsg_compose TREE");
1558 entries_len = sizeof(struct got_imsg_tree_entries);
1560 for (j = 0; j < nentries; j++) {
1561 struct got_parsed_tree_entry *pte = &entries[j];
1562 size_t len = sizeof(*pte) + pte->namelen;
1565 entries_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1566 err = send_tree_entries(ibuf, entries,
1567 i, j - 1, entries_len);
1571 entries_len = sizeof(struct got_imsg_tree_entries);
1578 err = send_tree_entries(ibuf, entries, i, j - 1, entries_len);
1583 return flush_imsg(ibuf);
1586 const struct got_error *
1587 got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
1589 const struct got_error *err = NULL;
1590 const size_t min_datalen =
1591 MIN(sizeof(struct got_imsg_error),
1592 sizeof(struct got_imsg_tree_object));
1593 struct got_imsg_tree_object *itree;
1599 err = read_imsg(ibuf);
1607 struct got_imsg_tree_entries *ientries;
1608 struct got_tree_entry *te = NULL;
1611 n = imsg_get(ibuf, &imsg);
1614 if (nentries < (*tree)->nentries) {
1615 err = read_imsg(ibuf);
1622 err = got_error(GOT_ERR_PRIVSEP_MSG);
1627 if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen) {
1629 err = got_error(GOT_ERR_PRIVSEP_LEN);
1633 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1635 switch (imsg.hdr.type) {
1636 case GOT_IMSG_ERROR:
1637 err = recv_imsg_error(&imsg, datalen);
1640 /* This message should only appear once. */
1641 if (*tree != NULL) {
1642 err = got_error(GOT_ERR_PRIVSEP_MSG);
1645 if (datalen != sizeof(*itree)) {
1646 err = got_error(GOT_ERR_PRIVSEP_LEN);
1650 if (itree->nentries < 0) {
1651 err = got_error(GOT_ERR_PRIVSEP_LEN);
1654 *tree = malloc(sizeof(**tree));
1655 if (*tree == NULL) {
1656 err = got_error_from_errno("malloc");
1659 (*tree)->entries = calloc(itree->nentries,
1660 sizeof(struct got_tree_entry));
1661 if ((*tree)->entries == NULL) {
1662 err = got_error_from_errno("malloc");
1667 (*tree)->nentries = itree->nentries;
1668 (*tree)->refcnt = 0;
1670 case GOT_IMSG_TREE_ENTRIES:
1671 /* This message should be preceeded by GOT_IMSG_TREE. */
1672 if (*tree == NULL) {
1673 err = got_error(GOT_ERR_PRIVSEP_MSG);
1676 if (datalen <= sizeof(*ientries) ||
1677 datalen > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1678 err = got_error(GOT_ERR_PRIVSEP_LEN);
1682 ientries = imsg.data;
1683 if (ientries->nentries > INT_MAX) {
1684 err = got_error_msg(GOT_ERR_NO_SPACE,
1685 "too many tree entries");
1688 te_offset = sizeof(*ientries);
1689 for (i = 0; i < ientries->nentries; i++) {
1690 struct got_imsg_tree_entry ite;
1691 const char *te_name;
1692 uint8_t *buf = imsg.data + te_offset;
1694 if (te_offset >= datalen) {
1695 err = got_error(GOT_ERR_PRIVSEP_LEN);
1699 /* Might not be aligned, size is ~32 bytes. */
1700 memcpy(&ite, buf, sizeof(ite));
1702 if (ite.namelen >= sizeof(te->name)) {
1703 err = got_error(GOT_ERR_PRIVSEP_LEN);
1706 if (te_offset + sizeof(ite) + ite.namelen >
1708 err = got_error(GOT_ERR_PRIVSEP_LEN);
1711 if (nentries >= (*tree)->nentries) {
1712 err = got_error(GOT_ERR_PRIVSEP_LEN);
1715 te = &(*tree)->entries[nentries];
1716 te_name = buf + sizeof(ite);
1717 memcpy(te->name, te_name, ite.namelen);
1718 te->name[ite.namelen] = '\0';
1719 memcpy(te->id.sha1, ite.id, SHA1_DIGEST_LENGTH);
1720 te->mode = ite.mode;
1724 te_offset += sizeof(ite) + ite.namelen;
1728 err = got_error(GOT_ERR_PRIVSEP_MSG);
1737 if (*tree && (*tree)->nentries != nentries) {
1739 err = got_error(GOT_ERR_PRIVSEP_LEN);
1740 got_object_tree_close(*tree);
1747 const struct got_error *
1748 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
1749 const uint8_t *data)
1751 struct got_imsg_blob iblob;
1754 iblob.hdrlen = hdrlen;
1759 if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
1760 return got_error(GOT_ERR_NO_SPACE);
1762 buf = malloc(sizeof(iblob) + size);
1764 return got_error_from_errno("malloc");
1766 memcpy(buf, &iblob, sizeof(iblob));
1767 memcpy(buf + sizeof(iblob), data, size);
1768 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
1769 sizeof(iblob) + size) == -1) {
1771 return got_error_from_errno("imsg_compose BLOB");
1775 /* Data has already been written to file descriptor. */
1776 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
1777 sizeof(iblob)) == -1)
1778 return got_error_from_errno("imsg_compose BLOB");
1782 return flush_imsg(ibuf);
1785 const struct got_error *
1786 got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1787 struct imsgbuf *ibuf)
1789 const struct got_error *err = NULL;
1791 struct got_imsg_blob *iblob;
1796 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1800 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1802 switch (imsg.hdr.type) {
1804 if (datalen < sizeof(*iblob)) {
1805 err = got_error(GOT_ERR_PRIVSEP_LEN);
1809 *size = iblob->size;
1810 *hdrlen = iblob->hdrlen;
1812 if (datalen == sizeof(*iblob)) {
1813 /* Data has been written to file descriptor. */
1817 if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX) {
1818 err = got_error(GOT_ERR_PRIVSEP_LEN);
1822 *outbuf = malloc(*size);
1823 if (*outbuf == NULL) {
1824 err = got_error_from_errno("malloc");
1827 memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
1830 err = got_error(GOT_ERR_PRIVSEP_MSG);
1839 static const struct got_error *
1840 send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
1842 const struct got_error *err = NULL;
1843 size_t offset, remain;
1846 remain = tagmsg_len;
1847 while (remain > 0) {
1848 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1850 if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
1851 tag->tagmsg + offset, n) == -1) {
1852 err = got_error_from_errno("imsg_compose TAG_TAGMSG");
1856 err = flush_imsg(ibuf);
1867 const struct got_error *
1868 got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
1870 const struct got_error *err = NULL;
1871 struct got_imsg_tag_object *itag;
1874 size_t tag_len = strlen(tag->tag);
1875 size_t tagger_len = strlen(tag->tagger);
1876 size_t tagmsg_len = strlen(tag->tagmsg);
1878 total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
1880 buf = malloc(total);
1882 return got_error_from_errno("malloc");
1884 itag = (struct got_imsg_tag_object *)buf;
1885 memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
1886 itag->obj_type = tag->obj_type;
1887 itag->tag_len = tag_len;
1888 itag->tagger_len = tagger_len;
1889 itag->tagger_time = tag->tagger_time;
1890 itag->tagger_gmtoff = tag->tagger_gmtoff;
1891 itag->tagmsg_len = tagmsg_len;
1893 len = sizeof(*itag);
1894 memcpy(buf + len, tag->tag, tag_len);
1896 memcpy(buf + len, tag->tagger, tagger_len);
1899 if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
1900 err = got_error_from_errno("imsg_compose TAG");
1904 if (tagmsg_len == 0 ||
1905 tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1906 err = flush_imsg(ibuf);
1910 err = send_tagmsg(ibuf, tag, tagmsg_len);
1916 const struct got_error *
1917 got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
1919 const struct got_error *err = NULL;
1921 struct got_imsg_tag_object *itag;
1922 size_t len, datalen;
1923 const size_t min_datalen =
1924 MIN(sizeof(struct got_imsg_error),
1925 sizeof(struct got_imsg_tag_object));
1929 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1933 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1936 switch (imsg.hdr.type) {
1938 if (datalen < sizeof(*itag)) {
1939 err = got_error(GOT_ERR_PRIVSEP_LEN);
1943 if (datalen != sizeof(*itag) + itag->tag_len +
1945 err = got_error(GOT_ERR_PRIVSEP_LEN);
1948 len += sizeof(*itag);
1950 *tag = calloc(1, sizeof(**tag));
1952 err = got_error_from_errno("calloc");
1956 memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
1958 if (itag->tag_len == 0) {
1959 (*tag)->tag = strdup("");
1960 if ((*tag)->tag == NULL) {
1961 err = got_error_from_errno("strdup");
1965 (*tag)->tag = malloc(itag->tag_len + 1);
1966 if ((*tag)->tag == NULL) {
1967 err = got_error_from_errno("malloc");
1970 memcpy((*tag)->tag, imsg.data + len,
1972 (*tag)->tag[itag->tag_len] = '\0';
1974 len += itag->tag_len;
1976 (*tag)->obj_type = itag->obj_type;
1977 (*tag)->tagger_time = itag->tagger_time;
1978 (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1980 if (itag->tagger_len == 0) {
1981 (*tag)->tagger = strdup("");
1982 if ((*tag)->tagger == NULL) {
1983 err = got_error_from_errno("strdup");
1987 (*tag)->tagger = malloc(itag->tagger_len + 1);
1988 if ((*tag)->tagger == NULL) {
1989 err = got_error_from_errno("malloc");
1992 memcpy((*tag)->tagger, imsg.data + len,
1994 (*tag)->tagger[itag->tagger_len] = '\0';
1996 len += itag->tagger_len;
1998 if (itag->tagmsg_len == 0) {
1999 (*tag)->tagmsg = strdup("");
2000 if ((*tag)->tagmsg == NULL) {
2001 err = got_error_from_errno("strdup");
2005 size_t offset = 0, remain = itag->tagmsg_len;
2007 (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
2008 if ((*tag)->tagmsg == NULL) {
2009 err = got_error_from_errno("malloc");
2012 while (remain > 0) {
2013 struct imsg imsg_log;
2014 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
2017 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
2021 if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
2022 return got_error(GOT_ERR_PRIVSEP_MSG);
2024 memcpy((*tag)->tagmsg + offset, imsg_log.data,
2026 imsg_free(&imsg_log);
2030 (*tag)->tagmsg[itag->tagmsg_len] = '\0';
2035 err = got_error(GOT_ERR_PRIVSEP_MSG);
2044 const struct got_error *
2045 got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
2046 struct got_packidx *packidx)
2048 const struct got_error *err = NULL;
2049 struct got_imsg_packidx ipackidx;
2050 struct got_imsg_pack ipack;
2053 ipackidx.len = packidx->len;
2054 ipackidx.packfile_size = pack->filesize;
2055 fd = dup(packidx->fd);
2057 return got_error_from_errno("dup");
2059 if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
2060 sizeof(ipackidx)) == -1) {
2061 err = got_error_from_errno("imsg_compose PACKIDX");
2066 if (strlcpy(ipack.path_packfile, pack->path_packfile,
2067 sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
2068 return got_error(GOT_ERR_NO_SPACE);
2069 ipack.filesize = pack->filesize;
2073 return got_error_from_errno("dup");
2075 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
2077 err = got_error_from_errno("imsg_compose PACK");
2082 return flush_imsg(ibuf);
2085 const struct got_error *
2086 got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
2087 struct got_object_id *id)
2089 struct got_imsg_packed_object iobj;
2092 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
2094 if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
2095 &iobj, sizeof(iobj)) == -1)
2096 return got_error_from_errno("imsg_compose "
2097 "PACKED_OBJECT_REQUEST");
2099 return flush_imsg(ibuf);
2102 const struct got_error *
2103 got_privsep_send_packed_raw_obj_req(struct imsgbuf *ibuf, int idx,
2104 struct got_object_id *id)
2106 struct got_imsg_packed_object iobj;
2109 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
2111 if (imsg_compose(ibuf, GOT_IMSG_PACKED_RAW_OBJECT_REQUEST, 0, 0, -1,
2112 &iobj, sizeof(iobj)) == -1)
2113 return got_error_from_errno("imsg_compose "
2114 "PACKED_OBJECT_REQUEST");
2116 return flush_imsg(ibuf);
2119 const struct got_error *
2120 got_privsep_send_gitconfig_parse_req(struct imsgbuf *ibuf, int fd)
2122 const struct got_error *err = NULL;
2124 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_PARSE_REQUEST, 0, 0, fd,
2126 err = got_error_from_errno("imsg_compose "
2127 "GITCONFIG_PARSE_REQUEST");
2132 return flush_imsg(ibuf);
2135 const struct got_error *
2136 got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *ibuf)
2138 if (imsg_compose(ibuf,
2139 GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST, 0, 0, -1,
2141 return got_error_from_errno("imsg_compose "
2142 "GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST");
2144 return flush_imsg(ibuf);
2147 const struct got_error *
2148 got_privsep_send_gitconfig_repository_extensions_req(struct imsgbuf *ibuf)
2150 if (imsg_compose(ibuf,
2151 GOT_IMSG_GITCONFIG_REPOSITORY_EXTENSIONS_REQUEST, 0, 0, -1,
2153 return got_error_from_errno("imsg_compose "
2154 "GITCONFIG_REPOSITORY_EXTENSIONS_REQUEST");
2156 return flush_imsg(ibuf);
2160 const struct got_error *
2161 got_privsep_send_gitconfig_author_name_req(struct imsgbuf *ibuf)
2163 if (imsg_compose(ibuf,
2164 GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST, 0, 0, -1, NULL, 0) == -1)
2165 return got_error_from_errno("imsg_compose "
2166 "GITCONFIG_AUTHOR_NAME_REQUEST");
2168 return flush_imsg(ibuf);
2171 const struct got_error *
2172 got_privsep_send_gitconfig_author_email_req(struct imsgbuf *ibuf)
2174 if (imsg_compose(ibuf,
2175 GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST, 0, 0, -1, NULL, 0) == -1)
2176 return got_error_from_errno("imsg_compose "
2177 "GITCONFIG_AUTHOR_EMAIL_REQUEST");
2179 return flush_imsg(ibuf);
2182 const struct got_error *
2183 got_privsep_send_gitconfig_remotes_req(struct imsgbuf *ibuf)
2185 if (imsg_compose(ibuf,
2186 GOT_IMSG_GITCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
2187 return got_error_from_errno("imsg_compose "
2188 "GITCONFIG_REMOTE_REQUEST");
2190 return flush_imsg(ibuf);
2193 const struct got_error *
2194 got_privsep_send_gitconfig_owner_req(struct imsgbuf *ibuf)
2196 if (imsg_compose(ibuf,
2197 GOT_IMSG_GITCONFIG_OWNER_REQUEST, 0, 0, -1, NULL, 0) == -1)
2198 return got_error_from_errno("imsg_compose "
2199 "GITCONFIG_OWNER_REQUEST");
2201 return flush_imsg(ibuf);
2204 const struct got_error *
2205 got_privsep_recv_gitconfig_str(char **str, struct imsgbuf *ibuf)
2207 const struct got_error *err = NULL;
2210 const size_t min_datalen = 0;
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_STR_VAL:
2223 /* datalen does not include terminating \0 */
2224 *str = malloc(datalen + 1);
2226 err = got_error_from_errno("malloc");
2229 memcpy(*str, imsg.data, datalen);
2230 (*str)[datalen] = '\0';
2233 err = got_error(GOT_ERR_PRIVSEP_MSG);
2241 const struct got_error *
2242 got_privsep_recv_gitconfig_int(int *val, struct imsgbuf *ibuf)
2244 const struct got_error *err = NULL;
2247 const size_t min_datalen =
2248 MIN(sizeof(struct got_imsg_error), sizeof(int));
2252 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2255 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2257 switch (imsg.hdr.type) {
2258 case GOT_IMSG_GITCONFIG_INT_VAL:
2259 if (datalen != sizeof(*val)) {
2260 err = got_error(GOT_ERR_PRIVSEP_LEN);
2263 memcpy(val, imsg.data, sizeof(*val));
2266 err = got_error(GOT_ERR_PRIVSEP_MSG);
2275 free_remote_data(struct got_remote_repo *remote)
2280 free(remote->fetch_url);
2281 free(remote->send_url);
2282 for (i = 0; i < remote->nfetch_branches; i++)
2283 free(remote->fetch_branches[i]);
2284 free(remote->fetch_branches);
2285 for (i = 0; i < remote->nsend_branches; i++)
2286 free(remote->send_branches[i]);
2287 free(remote->send_branches);
2288 for (i = 0; i < remote->nfetch_refs; i++)
2289 free(remote->fetch_refs[i]);
2290 free(remote->fetch_refs);
2293 const struct got_error *
2294 got_privsep_recv_gitconfig_remotes(struct got_remote_repo **remotes,
2295 int *nremotes, struct imsgbuf *ibuf)
2297 const struct got_error *err = NULL;
2300 struct got_imsg_remotes iremotes;
2301 struct got_imsg_remote iremote;
2305 iremotes.nremotes = 0;
2307 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremotes));
2310 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2312 switch (imsg.hdr.type) {
2313 case GOT_IMSG_GITCONFIG_REMOTES:
2314 if (datalen != sizeof(iremotes)) {
2315 err = got_error(GOT_ERR_PRIVSEP_LEN);
2318 memcpy(&iremotes, imsg.data, sizeof(iremotes));
2319 if (iremotes.nremotes == 0) {
2326 return got_error(GOT_ERR_PRIVSEP_MSG);
2331 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(**remotes));
2332 if (*remotes == NULL)
2333 return got_error_from_errno("recallocarray");
2335 while (*nremotes < iremotes.nremotes) {
2336 struct got_remote_repo *remote;
2338 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremote));
2341 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2343 switch (imsg.hdr.type) {
2344 case GOT_IMSG_GITCONFIG_REMOTE:
2345 remote = &(*remotes)[*nremotes];
2346 memset(remote, 0, sizeof(*remote));
2347 if (datalen < sizeof(iremote)) {
2348 err = got_error(GOT_ERR_PRIVSEP_LEN);
2351 memcpy(&iremote, imsg.data, sizeof(iremote));
2352 if (iremote.name_len == 0 ||
2353 iremote.fetch_url_len == 0 ||
2354 iremote.send_url_len == 0 ||
2355 (sizeof(iremote) + iremote.name_len +
2356 iremote.fetch_url_len + iremote.send_url_len) > datalen) {
2357 err = got_error(GOT_ERR_PRIVSEP_LEN);
2360 remote->name = strndup(imsg.data + sizeof(iremote),
2362 if (remote->name == NULL) {
2363 err = got_error_from_errno("strndup");
2366 remote->fetch_url = strndup(imsg.data + sizeof(iremote) +
2367 iremote.name_len, iremote.fetch_url_len);
2368 if (remote->fetch_url == NULL) {
2369 err = got_error_from_errno("strndup");
2370 free_remote_data(remote);
2373 remote->send_url = strndup(imsg.data + sizeof(iremote) +
2374 iremote.name_len + iremote.fetch_url_len,
2375 iremote.send_url_len);
2376 if (remote->send_url == NULL) {
2377 err = got_error_from_errno("strndup");
2378 free_remote_data(remote);
2381 remote->mirror_references = iremote.mirror_references;
2382 remote->fetch_all_branches = iremote.fetch_all_branches;
2383 remote->nfetch_branches = 0;
2384 remote->fetch_branches = NULL;
2385 remote->nsend_branches = 0;
2386 remote->send_branches = NULL;
2387 remote->nfetch_refs = 0;
2388 remote->fetch_refs = NULL;
2392 err = got_error(GOT_ERR_PRIVSEP_MSG);
2403 for (i = 0; i < *nremotes; i++)
2404 free_remote_data(&(*remotes)[i]);
2412 const struct got_error *
2413 got_privsep_send_gotconfig_parse_req(struct imsgbuf *ibuf, int fd)
2415 const struct got_error *err = NULL;
2417 if (imsg_compose(ibuf, GOT_IMSG_GOTCONFIG_PARSE_REQUEST, 0, 0, fd,
2419 err = got_error_from_errno("imsg_compose "
2420 "GOTCONFIG_PARSE_REQUEST");
2425 return flush_imsg(ibuf);
2428 const struct got_error *
2429 got_privsep_send_gotconfig_author_req(struct imsgbuf *ibuf)
2431 if (imsg_compose(ibuf,
2432 GOT_IMSG_GOTCONFIG_AUTHOR_REQUEST, 0, 0, -1, NULL, 0) == -1)
2433 return got_error_from_errno("imsg_compose "
2434 "GOTCONFIG_AUTHOR_REQUEST");
2436 return flush_imsg(ibuf);
2439 const struct got_error *
2440 got_privsep_send_gotconfig_remotes_req(struct imsgbuf *ibuf)
2442 if (imsg_compose(ibuf,
2443 GOT_IMSG_GOTCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
2444 return got_error_from_errno("imsg_compose "
2445 "GOTCONFIG_REMOTE_REQUEST");
2447 return flush_imsg(ibuf);
2450 const struct got_error *
2451 got_privsep_recv_gotconfig_str(char **str, struct imsgbuf *ibuf)
2453 const struct got_error *err = NULL;
2456 const size_t min_datalen = 0;
2460 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2463 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2465 switch (imsg.hdr.type) {
2466 case GOT_IMSG_ERROR:
2467 if (datalen < sizeof(struct got_imsg_error)) {
2468 err = got_error(GOT_ERR_PRIVSEP_LEN);
2471 err = recv_imsg_error(&imsg, datalen);
2473 case GOT_IMSG_GOTCONFIG_STR_VAL:
2476 /* datalen does not include terminating \0 */
2477 *str = malloc(datalen + 1);
2479 err = got_error_from_errno("malloc");
2482 memcpy(*str, imsg.data, datalen);
2483 (*str)[datalen] = '\0';
2486 err = got_error(GOT_ERR_PRIVSEP_MSG);
2494 const struct got_error *
2495 got_privsep_recv_gotconfig_remotes(struct got_remote_repo **remotes,
2496 int *nremotes, struct imsgbuf *ibuf)
2498 const struct got_error *err = NULL;
2501 struct got_imsg_remotes iremotes;
2502 struct got_imsg_remote iremote;
2503 const size_t min_datalen =
2504 MIN(sizeof(struct got_imsg_error), sizeof(iremotes));
2508 iremotes.nremotes = 0;
2510 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2513 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2515 switch (imsg.hdr.type) {
2516 case GOT_IMSG_ERROR:
2517 if (datalen < sizeof(struct got_imsg_error)) {
2518 err = got_error(GOT_ERR_PRIVSEP_LEN);
2521 err = recv_imsg_error(&imsg, datalen);
2523 case GOT_IMSG_GOTCONFIG_REMOTES:
2524 if (datalen != sizeof(iremotes)) {
2525 err = got_error(GOT_ERR_PRIVSEP_LEN);
2528 memcpy(&iremotes, imsg.data, sizeof(iremotes));
2529 if (iremotes.nremotes == 0) {
2536 return got_error(GOT_ERR_PRIVSEP_MSG);
2541 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(**remotes));
2542 if (*remotes == NULL)
2543 return got_error_from_errno("recallocarray");
2545 while (*nremotes < iremotes.nremotes) {
2546 struct got_remote_repo *remote;
2547 const size_t min_datalen =
2548 MIN(sizeof(struct got_imsg_error), sizeof(iremote));
2551 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2554 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2556 switch (imsg.hdr.type) {
2557 case GOT_IMSG_ERROR:
2558 if (datalen < sizeof(struct got_imsg_error)) {
2559 err = got_error(GOT_ERR_PRIVSEP_LEN);
2562 err = recv_imsg_error(&imsg, datalen);
2564 case GOT_IMSG_GOTCONFIG_REMOTE:
2565 remote = &(*remotes)[*nremotes];
2566 memset(remote, 0, sizeof(*remote));
2567 if (datalen < sizeof(iremote)) {
2568 err = got_error(GOT_ERR_PRIVSEP_LEN);
2571 memcpy(&iremote, imsg.data, sizeof(iremote));
2572 if (iremote.name_len == 0 ||
2573 (iremote.fetch_url_len == 0 &&
2574 iremote.send_url_len == 0) ||
2575 (sizeof(iremote) + iremote.name_len +
2576 iremote.fetch_url_len + iremote.send_url_len) >
2578 err = got_error(GOT_ERR_PRIVSEP_LEN);
2581 remote->name = strndup(imsg.data + sizeof(iremote),
2583 if (remote->name == NULL) {
2584 err = got_error_from_errno("strndup");
2587 remote->fetch_url = strndup(imsg.data +
2588 sizeof(iremote) + iremote.name_len,
2589 iremote.fetch_url_len);
2590 if (remote->fetch_url == NULL) {
2591 err = got_error_from_errno("strndup");
2592 free_remote_data(remote);
2595 remote->send_url = strndup(imsg.data +
2596 sizeof(iremote) + iremote.name_len +
2597 iremote.fetch_url_len, iremote.send_url_len);
2598 if (remote->send_url == NULL) {
2599 err = got_error_from_errno("strndup");
2600 free_remote_data(remote);
2603 remote->mirror_references = iremote.mirror_references;
2604 remote->fetch_all_branches = iremote.fetch_all_branches;
2605 if (iremote.nfetch_branches > 0) {
2606 remote->fetch_branches = recallocarray(NULL, 0,
2607 iremote.nfetch_branches, sizeof(char *));
2608 if (remote->fetch_branches == NULL) {
2609 err = got_error_from_errno("calloc");
2610 free_remote_data(remote);
2614 remote->nfetch_branches = 0;
2615 for (i = 0; i < iremote.nfetch_branches; i++) {
2617 err = got_privsep_recv_gotconfig_str(&branch,
2620 free_remote_data(remote);
2623 remote->fetch_branches[i] = branch;
2624 remote->nfetch_branches++;
2626 if (iremote.nsend_branches > 0) {
2627 remote->send_branches = recallocarray(NULL, 0,
2628 iremote.nsend_branches, sizeof(char *));
2629 if (remote->send_branches == NULL) {
2630 err = got_error_from_errno("calloc");
2631 free_remote_data(remote);
2635 remote->nsend_branches = 0;
2636 for (i = 0; i < iremote.nsend_branches; i++) {
2638 err = got_privsep_recv_gotconfig_str(&branch,
2641 free_remote_data(remote);
2644 remote->send_branches[i] = branch;
2645 remote->nsend_branches++;
2647 if (iremote.nfetch_refs > 0) {
2648 remote->fetch_refs = recallocarray(NULL, 0,
2649 iremote.nfetch_refs, sizeof(char *));
2650 if (remote->fetch_refs == NULL) {
2651 err = got_error_from_errno("calloc");
2652 free_remote_data(remote);
2656 remote->nfetch_refs = 0;
2657 for (i = 0; i < iremote.nfetch_refs; i++) {
2659 err = got_privsep_recv_gotconfig_str(&ref,
2662 free_remote_data(remote);
2665 remote->fetch_refs[i] = ref;
2666 remote->nfetch_refs++;
2671 err = got_error(GOT_ERR_PRIVSEP_MSG);
2682 for (i = 0; i < *nremotes; i++)
2683 free_remote_data(&(*remotes)[i]);
2691 const struct got_error *
2692 got_privsep_send_commit_traversal_request(struct imsgbuf *ibuf,
2693 struct got_object_id *id, int idx, const char *path)
2695 const struct got_error *err = NULL;
2697 size_t path_len = strlen(path) + 1;
2699 wbuf = imsg_create(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_REQUEST, 0, 0,
2700 sizeof(struct got_imsg_commit_traversal_request) + path_len);
2702 return got_error_from_errno(
2703 "imsg_create COMMIT_TRAVERSAL_REQUEST");
2704 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
2705 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
2709 if (imsg_add(wbuf, &idx, sizeof(idx)) == -1) {
2710 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
2714 if (imsg_add(wbuf, path, path_len) == -1) {
2715 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
2721 imsg_close(ibuf, wbuf);
2723 return flush_imsg(ibuf);
2726 const struct got_error *
2727 got_privsep_recv_traversed_commits(struct got_commit_object **changed_commit,
2728 struct got_object_id **changed_commit_id,
2729 struct got_object_id_queue *commit_ids, struct imsgbuf *ibuf)
2731 const struct got_error *err = NULL;
2733 struct got_imsg_traversed_commits *icommits;
2737 *changed_commit = NULL;
2738 *changed_commit_id = NULL;
2741 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2745 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2746 switch (imsg.hdr.type) {
2747 case GOT_IMSG_TRAVERSED_COMMITS:
2748 icommits = imsg.data;
2749 if (datalen != sizeof(*icommits) +
2750 icommits->ncommits * SHA1_DIGEST_LENGTH) {
2751 err = got_error(GOT_ERR_PRIVSEP_LEN);
2754 for (i = 0; i < icommits->ncommits; i++) {
2755 struct got_object_qid *qid;
2756 uint8_t *sha1 = (uint8_t *)imsg.data +
2757 sizeof(*icommits) + i * SHA1_DIGEST_LENGTH;
2758 err = got_object_qid_alloc_partial(&qid);
2761 memcpy(qid->id.sha1, sha1, SHA1_DIGEST_LENGTH);
2762 STAILQ_INSERT_TAIL(commit_ids, qid, entry);
2764 /* The last commit may contain a change. */
2765 if (i == icommits->ncommits - 1) {
2766 *changed_commit_id =
2767 got_object_id_dup(&qid->id);
2768 if (*changed_commit_id == NULL) {
2769 err = got_error_from_errno(
2770 "got_object_id_dup");
2776 case GOT_IMSG_COMMIT:
2777 if (*changed_commit_id == NULL) {
2778 err = got_error(GOT_ERR_PRIVSEP_MSG);
2781 err = get_commit_from_imsg(changed_commit, &imsg,
2784 case GOT_IMSG_COMMIT_TRAVERSAL_DONE:
2788 err = got_error(GOT_ERR_PRIVSEP_MSG);
2798 got_object_id_queue_free(commit_ids);
2802 const struct got_error *
2803 got_privsep_send_raw_delta_req(struct imsgbuf *ibuf, int idx,
2804 struct got_object_id *id)
2806 struct got_imsg_raw_delta_request dreq;
2809 memcpy(dreq.id, id->sha1, SHA1_DIGEST_LENGTH);
2811 if (imsg_compose(ibuf, GOT_IMSG_RAW_DELTA_REQUEST, 0, 0, -1,
2812 &dreq, sizeof(dreq)) == -1)
2813 return got_error_from_errno("imsg_compose RAW_DELTA_REQUEST");
2815 return flush_imsg(ibuf);
2818 const struct got_error *
2819 got_privsep_send_raw_delta_outfd(struct imsgbuf *ibuf, int fd)
2821 return send_fd(ibuf, GOT_IMSG_RAW_DELTA_OUTFD, fd);
2824 const struct got_error *
2825 got_privsep_send_raw_delta(struct imsgbuf *ibuf, uint64_t base_size,
2826 uint64_t result_size, off_t delta_size, off_t delta_compressed_size,
2827 off_t delta_offset, off_t delta_out_offset, struct got_object_id *base_id)
2829 struct got_imsg_raw_delta idelta;
2832 idelta.base_size = base_size;
2833 idelta.result_size = result_size;
2834 idelta.delta_size = delta_size;
2835 idelta.delta_compressed_size = delta_compressed_size;
2836 idelta.delta_offset = delta_offset;
2837 idelta.delta_out_offset = delta_out_offset;
2838 memcpy(idelta.base_id, base_id->sha1, SHA1_DIGEST_LENGTH);
2840 ret = imsg_compose(ibuf, GOT_IMSG_RAW_DELTA, 0, 0, -1,
2841 &idelta, sizeof(idelta));
2843 return got_error_from_errno("imsg_compose RAW_DELTA");
2845 return flush_imsg(ibuf);
2848 const struct got_error *
2849 got_privsep_recv_raw_delta(uint64_t *base_size, uint64_t *result_size,
2850 off_t *delta_size, off_t *delta_compressed_size, off_t *delta_offset,
2851 off_t *delta_out_offset, struct got_object_id **base_id, struct imsgbuf *ibuf)
2853 const struct got_error *err = NULL;
2855 struct got_imsg_raw_delta *delta;
2861 *delta_compressed_size = 0;
2863 *delta_out_offset = 0;
2866 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2870 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2872 switch (imsg.hdr.type) {
2873 case GOT_IMSG_RAW_DELTA:
2874 if (datalen != sizeof(*delta)) {
2875 err = got_error(GOT_ERR_PRIVSEP_LEN);
2879 *base_size = delta->base_size;
2880 *result_size = delta->result_size;
2881 *delta_size = delta->delta_size;
2882 *delta_compressed_size = delta->delta_compressed_size;
2883 *delta_offset = delta->delta_offset;
2884 *delta_out_offset = delta->delta_out_offset;
2885 *base_id = calloc(1, sizeof(**base_id));
2886 if (*base_id == NULL) {
2887 err = got_error_from_errno("malloc");
2890 memcpy((*base_id)->sha1, delta->base_id, SHA1_DIGEST_LENGTH);
2893 err = got_error(GOT_ERR_PRIVSEP_MSG);
2906 const struct got_error *
2907 got_privsep_send_object_idlist(struct imsgbuf *ibuf,
2908 struct got_object_id **ids, size_t nids)
2910 const struct got_error *err = NULL;
2911 struct got_imsg_object_idlist idlist;
2915 if (nids > GOT_IMSG_OBJ_ID_LIST_MAX_NIDS)
2916 return got_error(GOT_ERR_NO_SPACE);
2918 wbuf = imsg_create(ibuf, GOT_IMSG_OBJ_ID_LIST, 0, 0,
2919 sizeof(idlist) + nids * sizeof(**ids));
2921 err = got_error_from_errno("imsg_create OBJ_ID_LIST");
2926 if (imsg_add(wbuf, &idlist, sizeof(idlist)) == -1) {
2927 err = got_error_from_errno("imsg_add OBJ_ID_LIST");
2932 for (i = 0; i < nids; i++) {
2933 struct got_object_id *id = ids[i];
2934 if (imsg_add(wbuf, id, sizeof(*id)) == -1) {
2935 err = got_error_from_errno("imsg_add OBJ_ID_LIST");
2942 imsg_close(ibuf, wbuf);
2944 return flush_imsg(ibuf);
2947 const struct got_error *
2948 got_privsep_send_object_idlist_done(struct imsgbuf *ibuf)
2950 if (imsg_compose(ibuf, GOT_IMSG_OBJ_ID_LIST_DONE, 0, 0, -1, NULL, 0)
2952 return got_error_from_errno("imsg_compose OBJ_ID_LIST_DONE");
2954 return flush_imsg(ibuf);
2957 const struct got_error *
2958 got_privsep_recv_object_idlist(int *done, struct got_object_id **ids,
2959 size_t *nids, struct imsgbuf *ibuf)
2961 const struct got_error *err = NULL;
2963 struct got_imsg_object_idlist *idlist;
2970 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2974 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2975 switch (imsg.hdr.type) {
2976 case GOT_IMSG_OBJ_ID_LIST:
2977 if (datalen < sizeof(*idlist)) {
2978 err = got_error(GOT_ERR_PRIVSEP_LEN);
2982 if (idlist->nids > GOT_IMSG_OBJ_ID_LIST_MAX_NIDS) {
2983 err = got_error(GOT_ERR_PRIVSEP_LEN);
2986 *nids = idlist->nids;
2987 *ids = calloc(*nids, sizeof(**ids));
2989 err = got_error_from_errno("calloc");
2992 memcpy(*ids, (uint8_t *)imsg.data + sizeof(idlist),
2993 *nids * sizeof(**ids));
2995 case GOT_IMSG_OBJ_ID_LIST_DONE:
2999 err = got_error(GOT_ERR_PRIVSEP_MSG);
3008 const struct got_error *
3009 got_privsep_send_delta_reuse_req(struct imsgbuf *ibuf)
3011 if (imsg_compose(ibuf, GOT_IMSG_DELTA_REUSE_REQUEST, 0, 0, -1, NULL, 0)
3013 return got_error_from_errno("imsg_compose DELTA_REUSE_REQUEST");
3015 return flush_imsg(ibuf);
3018 const struct got_error *
3019 got_privsep_send_reused_deltas(struct imsgbuf *ibuf,
3020 struct got_imsg_reused_delta *deltas, size_t ndeltas)
3022 const struct got_error *err = NULL;
3024 struct got_imsg_reused_deltas ideltas;
3027 if (ndeltas > GOT_IMSG_REUSED_DELTAS_MAX_NDELTAS)
3028 return got_error(GOT_ERR_NO_SPACE);
3030 wbuf = imsg_create(ibuf, GOT_IMSG_REUSED_DELTAS, 0, 0,
3031 sizeof(ideltas) + ndeltas * sizeof(*deltas));
3033 err = got_error_from_errno("imsg_create REUSED_DELTAS");
3037 ideltas.ndeltas = ndeltas;
3038 if (imsg_add(wbuf, &ideltas, sizeof(ideltas)) == -1) {
3039 err = got_error_from_errno("imsg_add REUSED_DELTAS");
3044 for (i = 0; i < ndeltas; i++) {
3045 struct got_imsg_reused_delta *delta = &deltas[i];
3046 if (imsg_add(wbuf, delta, sizeof(*delta)) == -1) {
3047 err = got_error_from_errno("imsg_add REUSED_DELTAS");
3054 imsg_close(ibuf, wbuf);
3056 return flush_imsg(ibuf);
3059 const struct got_error *
3060 got_privsep_send_reused_deltas_done(struct imsgbuf *ibuf)
3062 if (imsg_compose(ibuf, GOT_IMSG_DELTA_REUSE_DONE, 0, 0, -1, NULL, 0)
3064 return got_error_from_errno("imsg_compose DELTA_REUSE_DONE");
3066 return flush_imsg(ibuf);
3069 const struct got_error *
3070 got_privsep_recv_reused_deltas(int *done, struct got_imsg_reused_delta *deltas,
3071 size_t *ndeltas, struct imsgbuf *ibuf)
3073 const struct got_error *err = NULL;
3075 struct got_imsg_reused_deltas *ideltas;
3081 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
3085 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
3086 switch (imsg.hdr.type) {
3087 case GOT_IMSG_REUSED_DELTAS:
3088 if (datalen < sizeof(*ideltas)) {
3089 err = got_error(GOT_ERR_PRIVSEP_LEN);
3092 ideltas = imsg.data;
3093 if (ideltas->ndeltas > GOT_IMSG_OBJ_ID_LIST_MAX_NIDS) {
3094 err = got_error(GOT_ERR_PRIVSEP_LEN);
3097 *ndeltas = ideltas->ndeltas;
3098 memcpy(deltas, (uint8_t *)imsg.data + sizeof(ideltas),
3099 *ndeltas * sizeof(*deltas));
3101 case GOT_IMSG_DELTA_REUSE_DONE:
3105 err = got_error(GOT_ERR_PRIVSEP_MSG);
3114 const struct got_error *
3115 got_privsep_unveil_exec_helpers(void)
3117 const char *helpers[] = {
3118 GOT_PATH_PROG_READ_PACK,
3119 GOT_PATH_PROG_READ_OBJECT,
3120 GOT_PATH_PROG_READ_COMMIT,
3121 GOT_PATH_PROG_READ_TREE,
3122 GOT_PATH_PROG_READ_BLOB,
3123 GOT_PATH_PROG_READ_TAG,
3124 GOT_PATH_PROG_READ_GITCONFIG,
3125 GOT_PATH_PROG_READ_GOTCONFIG,
3126 GOT_PATH_PROG_READ_PATCH,
3127 GOT_PATH_PROG_FETCH_PACK,
3128 GOT_PATH_PROG_INDEX_PACK,
3129 GOT_PATH_PROG_SEND_PACK,
3133 for (i = 0; i < nitems(helpers); i++) {
3134 if (unveil(helpers[i], "x") == 0)
3136 return got_error_from_errno2("unveil", helpers[i]);
3143 got_privsep_exec_child(int imsg_fds[2], const char *path, const char *repo_path)
3145 if (close(imsg_fds[0]) == -1) {
3146 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
3150 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
3151 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
3155 closefrom(GOT_IMSG_FD_CHILD + 1);
3157 if (execl(path, path, repo_path, (char *)NULL) == -1) {
3158 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,