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 return got_error_from_errno("imsg_add RAW_OBJECT");
314 if (data && size + hdrlen <= GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX) {
315 if (imsg_add(wbuf, data, size + hdrlen) == -1)
316 return got_error_from_errno("imsg_add RAW_OBJECT");
320 imsg_close(ibuf, wbuf);
322 return flush_imsg(ibuf);
325 const struct got_error *
326 got_privsep_recv_raw_obj(uint8_t **outbuf, off_t *size, size_t *hdrlen,
327 struct imsgbuf *ibuf)
329 const struct got_error *err = NULL;
331 struct got_imsg_raw_obj *iobj;
336 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
340 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
342 switch (imsg.hdr.type) {
343 case GOT_IMSG_RAW_OBJECT:
344 if (datalen < sizeof(*iobj)) {
345 err = got_error(GOT_ERR_PRIVSEP_LEN);
350 *hdrlen = iobj->hdrlen;
352 if (datalen == sizeof(*iobj)) {
353 /* Data has been written to file descriptor. */
357 if (*size + *hdrlen > GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX) {
358 err = got_error(GOT_ERR_PRIVSEP_LEN);
362 *outbuf = malloc(*size + *hdrlen);
363 if (*outbuf == NULL) {
364 err = got_error_from_errno("malloc");
367 memcpy(*outbuf, imsg.data + sizeof(*iobj), *size + *hdrlen);
370 err = got_error(GOT_ERR_PRIVSEP_MSG);
379 const struct got_error *
380 got_privsep_send_commit_req(struct imsgbuf *ibuf, int fd,
381 struct got_object_id *id, int pack_idx)
383 const struct got_error *err = NULL;
384 struct got_imsg_packed_object iobj;
388 if (pack_idx != -1) { /* commit is packed */
390 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
398 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_REQUEST, 0, 0, fd, data, len)
400 err = got_error_from_errno("imsg_compose COMMIT_REQUEST");
405 return flush_imsg(ibuf);
408 const struct got_error *
409 got_privsep_send_tree_req(struct imsgbuf *ibuf, int fd,
410 struct got_object_id *id, int pack_idx)
416 len = sizeof(struct got_imsg_packed_object);
420 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_REQUEST, 0, 0, len);
422 return got_error_from_errno("imsg_create TREE_REQUEST");
424 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1)
425 return got_error_from_errno("imsg_add TREE_REQUEST");
427 if (pack_idx != -1) { /* tree is packed */
428 if (imsg_add(wbuf, &pack_idx, sizeof(pack_idx)) == -1)
429 return got_error_from_errno("imsg_add TREE_REQUEST");
433 imsg_close(ibuf, wbuf);
435 return flush_imsg(ibuf);
438 const struct got_error *
439 got_privsep_send_tag_req(struct imsgbuf *ibuf, int fd,
440 struct got_object_id *id, int pack_idx)
442 struct got_imsg_packed_object iobj;
446 if (pack_idx != -1) { /* tag is packed */
448 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
456 if (imsg_compose(ibuf, GOT_IMSG_TAG_REQUEST, 0, 0, fd, data, len)
458 return got_error_from_errno("imsg_compose TAG_REQUEST");
460 return flush_imsg(ibuf);
463 const struct got_error *
464 got_privsep_send_blob_req(struct imsgbuf *ibuf, int infd,
465 struct got_object_id *id, int pack_idx)
467 const struct got_error *err = NULL;
468 struct got_imsg_packed_object iobj;
472 if (pack_idx != -1) { /* blob is packed */
474 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
482 if (imsg_compose(ibuf, GOT_IMSG_BLOB_REQUEST, 0, 0, infd, data, len)
484 err = got_error_from_errno("imsg_compose BLOB_REQUEST");
489 return flush_imsg(ibuf);
492 const struct got_error *
493 got_privsep_send_blob_outfd(struct imsgbuf *ibuf, int outfd)
495 const struct got_error *err = NULL;
497 if (imsg_compose(ibuf, GOT_IMSG_BLOB_OUTFD, 0, 0, outfd, NULL, 0)
499 err = got_error_from_errno("imsg_compose BLOB_OUTFD");
504 return flush_imsg(ibuf);
507 static const struct got_error *
508 send_fd(struct imsgbuf *ibuf, int imsg_code, int fd)
510 const struct got_error *err = NULL;
512 if (imsg_compose(ibuf, imsg_code, 0, 0, fd, NULL, 0) == -1) {
513 err = got_error_from_errno("imsg_compose TMPFD");
518 return flush_imsg(ibuf);
521 const struct got_error *
522 got_privsep_send_tmpfd(struct imsgbuf *ibuf, int fd)
524 return send_fd(ibuf, GOT_IMSG_TMPFD, fd);
527 const struct got_error *
528 got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj)
530 struct got_imsg_object iobj;
532 memcpy(iobj.id, obj->id.sha1, sizeof(iobj.id));
533 iobj.type = obj->type;
534 iobj.flags = obj->flags;
535 iobj.hdrlen = obj->hdrlen;
536 iobj.size = obj->size;
537 if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
538 iobj.pack_offset = obj->pack_offset;
539 iobj.pack_idx = obj->pack_idx;
542 if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
544 return got_error_from_errno("imsg_compose OBJECT");
546 return flush_imsg(ibuf);
549 const struct got_error *
550 got_privsep_send_fetch_req(struct imsgbuf *ibuf, int fd,
551 struct got_pathlist_head *have_refs, int fetch_all_branches,
552 struct got_pathlist_head *wanted_branches,
553 struct got_pathlist_head *wanted_refs, int list_refs_only, int verbosity)
555 const struct got_error *err = NULL;
558 struct got_pathlist_entry *pe;
559 struct got_imsg_fetch_request fetchreq;
561 memset(&fetchreq, 0, sizeof(fetchreq));
562 fetchreq.fetch_all_branches = fetch_all_branches;
563 fetchreq.list_refs_only = list_refs_only;
564 fetchreq.verbosity = verbosity;
565 TAILQ_FOREACH(pe, have_refs, entry)
566 fetchreq.n_have_refs++;
567 TAILQ_FOREACH(pe, wanted_branches, entry)
568 fetchreq.n_wanted_branches++;
569 TAILQ_FOREACH(pe, wanted_refs, entry)
570 fetchreq.n_wanted_refs++;
571 len = sizeof(struct got_imsg_fetch_request);
572 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
574 return got_error(GOT_ERR_NO_SPACE);
577 if (imsg_compose(ibuf, GOT_IMSG_FETCH_REQUEST, 0, 0, fd,
578 &fetchreq, sizeof(fetchreq)) == -1)
579 return got_error_from_errno(
580 "imsg_compose FETCH_SERVER_PROGRESS");
582 err = flush_imsg(ibuf);
589 TAILQ_FOREACH(pe, have_refs, entry) {
590 const char *name = pe->path;
591 size_t name_len = pe->path_len;
592 struct got_object_id *id = pe->data;
594 len = sizeof(struct got_imsg_fetch_have_ref) + name_len;
595 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_HAVE_REF, 0, 0, len);
597 return got_error_from_errno("imsg_create FETCH_HAVE_REF");
599 /* Keep in sync with struct got_imsg_fetch_have_ref! */
600 if (imsg_add(wbuf, id->sha1, sizeof(id->sha1)) == -1)
601 return got_error_from_errno("imsg_add FETCH_HAVE_REF");
602 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1)
603 return got_error_from_errno("imsg_add FETCH_HAVE_REF");
604 if (imsg_add(wbuf, name, name_len) == -1)
605 return got_error_from_errno("imsg_add FETCH_HAVE_REF");
608 imsg_close(ibuf, wbuf);
609 err = flush_imsg(ibuf);
614 TAILQ_FOREACH(pe, wanted_branches, entry) {
615 const char *name = pe->path;
616 size_t name_len = pe->path_len;
618 len = sizeof(struct got_imsg_fetch_wanted_branch) + name_len;
619 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_WANTED_BRANCH, 0, 0,
622 return got_error_from_errno(
623 "imsg_create FETCH_WANTED_BRANCH");
625 /* Keep in sync with struct got_imsg_fetch_wanted_branch! */
626 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1)
627 return got_error_from_errno(
628 "imsg_add FETCH_WANTED_BRANCH");
629 if (imsg_add(wbuf, name, name_len) == -1)
630 return got_error_from_errno(
631 "imsg_add FETCH_WANTED_BRANCH");
634 imsg_close(ibuf, wbuf);
635 err = flush_imsg(ibuf);
640 TAILQ_FOREACH(pe, wanted_refs, entry) {
641 const char *name = pe->path;
642 size_t name_len = pe->path_len;
644 len = sizeof(struct got_imsg_fetch_wanted_ref) + name_len;
645 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_WANTED_REF, 0, 0,
648 return got_error_from_errno(
649 "imsg_create FETCH_WANTED_REF");
651 /* Keep in sync with struct got_imsg_fetch_wanted_ref! */
652 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1)
653 return got_error_from_errno(
654 "imsg_add FETCH_WANTED_REF");
655 if (imsg_add(wbuf, name, name_len) == -1)
656 return got_error_from_errno(
657 "imsg_add FETCH_WANTED_REF");
660 imsg_close(ibuf, wbuf);
661 err = flush_imsg(ibuf);
671 const struct got_error *
672 got_privsep_send_fetch_outfd(struct imsgbuf *ibuf, int fd)
674 return send_fd(ibuf, GOT_IMSG_FETCH_OUTFD, fd);
677 const struct got_error *
678 got_privsep_recv_fetch_progress(int *done, struct got_object_id **id,
679 char **refname, struct got_pathlist_head *symrefs, char **server_progress,
680 off_t *packfile_size, uint8_t *pack_sha1, struct imsgbuf *ibuf)
682 const struct got_error *err = NULL;
685 struct got_imsg_fetch_symrefs *isymrefs = NULL;
693 *server_progress = NULL;
695 memset(pack_sha1, 0, SHA1_DIGEST_LENGTH);
697 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
701 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
702 switch (imsg.hdr.type) {
704 if (datalen < sizeof(struct got_imsg_error)) {
705 err = got_error(GOT_ERR_PRIVSEP_LEN);
708 err = recv_imsg_error(&imsg, datalen);
710 case GOT_IMSG_FETCH_SYMREFS:
711 if (datalen < sizeof(*isymrefs)) {
712 err = got_error(GOT_ERR_PRIVSEP_LEN);
715 if (isymrefs != NULL) {
716 err = got_error(GOT_ERR_PRIVSEP_MSG);
719 isymrefs = (struct got_imsg_fetch_symrefs *)imsg.data;
720 off = sizeof(*isymrefs);
721 remain = datalen - off;
722 for (n = 0; n < isymrefs->nsymrefs; n++) {
723 struct got_imsg_fetch_symref *s;
725 if (remain < sizeof(struct got_imsg_fetch_symref)) {
726 err = got_error(GOT_ERR_PRIVSEP_LEN);
729 s = (struct got_imsg_fetch_symref *)(imsg.data + off);
731 remain -= sizeof(*s);
732 if (remain < s->name_len) {
733 err = got_error(GOT_ERR_PRIVSEP_LEN);
736 name = strndup(imsg.data + off, s->name_len);
738 err = got_error_from_errno("strndup");
742 remain -= s->name_len;
743 if (remain < s->target_len) {
744 err = got_error(GOT_ERR_PRIVSEP_LEN);
748 target = strndup(imsg.data + off, s->target_len);
749 if (target == NULL) {
750 err = got_error_from_errno("strndup");
754 off += s->target_len;
755 remain -= s->target_len;
756 err = got_pathlist_append(symrefs, name, target);
764 case GOT_IMSG_FETCH_REF:
765 if (datalen <= SHA1_DIGEST_LENGTH) {
766 err = got_error(GOT_ERR_PRIVSEP_MSG);
769 *id = malloc(sizeof(**id));
771 err = got_error_from_errno("malloc");
774 memcpy((*id)->sha1, imsg.data, SHA1_DIGEST_LENGTH);
775 *refname = strndup(imsg.data + SHA1_DIGEST_LENGTH,
776 datalen - SHA1_DIGEST_LENGTH);
777 if (*refname == NULL) {
778 err = got_error_from_errno("strndup");
782 case GOT_IMSG_FETCH_SERVER_PROGRESS:
784 err = got_error(GOT_ERR_PRIVSEP_LEN);
787 *server_progress = strndup(imsg.data, datalen);
788 if (*server_progress == NULL) {
789 err = got_error_from_errno("strndup");
792 for (i = 0; i < datalen; i++) {
793 if (!isprint((unsigned char)(*server_progress)[i]) &&
794 !isspace((unsigned char)(*server_progress)[i])) {
795 err = got_error(GOT_ERR_PRIVSEP_MSG);
796 free(*server_progress);
797 *server_progress = NULL;
802 case GOT_IMSG_FETCH_DOWNLOAD_PROGRESS:
803 if (datalen < sizeof(*packfile_size)) {
804 err = got_error(GOT_ERR_PRIVSEP_MSG);
807 memcpy(packfile_size, imsg.data, sizeof(*packfile_size));
809 case GOT_IMSG_FETCH_DONE:
810 if (datalen != SHA1_DIGEST_LENGTH) {
811 err = got_error(GOT_ERR_PRIVSEP_MSG);
814 memcpy(pack_sha1, imsg.data, SHA1_DIGEST_LENGTH);
818 err = got_error(GOT_ERR_PRIVSEP_MSG);
832 static const struct got_error *
833 send_send_ref(const char *name, size_t name_len, struct got_object_id *id,
834 int delete, struct imsgbuf *ibuf)
839 len = sizeof(struct got_imsg_send_ref) + name_len;
840 wbuf = imsg_create(ibuf, GOT_IMSG_SEND_REF, 0, 0, len);
842 return got_error_from_errno("imsg_create SEND_REF");
844 /* Keep in sync with struct got_imsg_send_ref! */
845 if (imsg_add(wbuf, id->sha1, sizeof(id->sha1)) == -1)
846 return got_error_from_errno("imsg_add SEND_REF");
847 if (imsg_add(wbuf, &delete, sizeof(delete)) == -1)
848 return got_error_from_errno("imsg_add SEND_REF");
849 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1)
850 return got_error_from_errno("imsg_add SEND_REF");
851 if (imsg_add(wbuf, name, name_len) == -1)
852 return got_error_from_errno("imsg_add SEND_REF");
855 imsg_close(ibuf, wbuf);
856 return flush_imsg(ibuf);
859 const struct got_error *
860 got_privsep_send_send_req(struct imsgbuf *ibuf, int fd,
861 struct got_pathlist_head *have_refs,
862 struct got_pathlist_head *delete_refs,
865 const struct got_error *err = NULL;
866 struct got_pathlist_entry *pe;
867 struct got_imsg_send_request sendreq;
868 struct got_object_id zero_id;
870 memset(&zero_id, 0, sizeof(zero_id));
871 memset(&sendreq, 0, sizeof(sendreq));
872 sendreq.verbosity = verbosity;
873 TAILQ_FOREACH(pe, have_refs, entry)
875 TAILQ_FOREACH(pe, delete_refs, entry)
877 if (imsg_compose(ibuf, GOT_IMSG_SEND_REQUEST, 0, 0, fd,
878 &sendreq, sizeof(sendreq)) == -1) {
879 err = got_error_from_errno(
880 "imsg_compose FETCH_SERVER_PROGRESS");
884 err = flush_imsg(ibuf);
889 TAILQ_FOREACH(pe, have_refs, entry) {
890 const char *name = pe->path;
891 size_t name_len = pe->path_len;
892 struct got_object_id *id = pe->data;
893 err = send_send_ref(name, name_len, id, 0, ibuf);
898 TAILQ_FOREACH(pe, delete_refs, entry) {
899 const char *name = pe->path;
900 size_t name_len = pe->path_len;
901 err = send_send_ref(name, name_len, &zero_id, 1, ibuf);
906 if (fd != -1 && close(fd) == -1 && err == NULL)
907 err = got_error_from_errno("close");
912 const struct got_error *
913 got_privsep_recv_send_remote_refs(struct got_pathlist_head *remote_refs,
914 struct imsgbuf *ibuf)
916 const struct got_error *err = NULL;
920 struct got_imsg_send_remote_ref iremote_ref;
921 struct got_object_id *id = NULL;
922 char *refname = NULL;
923 struct got_pathlist_entry *new;
926 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
929 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
930 switch (imsg.hdr.type) {
932 if (datalen < sizeof(struct got_imsg_error)) {
933 err = got_error(GOT_ERR_PRIVSEP_LEN);
936 err = recv_imsg_error(&imsg, datalen);
938 case GOT_IMSG_SEND_REMOTE_REF:
939 if (datalen < sizeof(iremote_ref)) {
940 err = got_error(GOT_ERR_PRIVSEP_MSG);
943 memcpy(&iremote_ref, imsg.data, sizeof(iremote_ref));
944 if (datalen != sizeof(iremote_ref) +
945 iremote_ref.name_len) {
946 err = got_error(GOT_ERR_PRIVSEP_MSG);
949 id = malloc(sizeof(*id));
951 err = got_error_from_errno("malloc");
954 memcpy(id->sha1, iremote_ref.id, SHA1_DIGEST_LENGTH);
955 refname = strndup(imsg.data + sizeof(iremote_ref),
956 datalen - sizeof(iremote_ref));
957 if (refname == NULL) {
958 err = got_error_from_errno("strndup");
961 err = got_pathlist_insert(&new, remote_refs,
965 if (new == NULL) { /* duplicate which wasn't inserted */
972 case GOT_IMSG_SEND_PACK_REQUEST:
974 err = got_error(GOT_ERR_PRIVSEP_MSG);
977 /* got-send-pack is now waiting for a pack file. */
981 err = got_error(GOT_ERR_PRIVSEP_MSG);
992 const struct got_error *
993 got_privsep_send_packfd(struct imsgbuf *ibuf, int fd)
995 return send_fd(ibuf, GOT_IMSG_SEND_PACKFD, fd);
998 const struct got_error *
999 got_privsep_recv_send_progress(int *done, off_t *bytes_sent,
1000 int *success, char **refname, struct imsgbuf *ibuf)
1002 const struct got_error *err = NULL;
1005 struct got_imsg_send_ref_status iref_status;
1007 /* Do not reset the current value of 'bytes_sent', it accumulates. */
1012 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1016 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1017 switch (imsg.hdr.type) {
1018 case GOT_IMSG_ERROR:
1019 if (datalen < sizeof(struct got_imsg_error)) {
1020 err = got_error(GOT_ERR_PRIVSEP_LEN);
1023 err = recv_imsg_error(&imsg, datalen);
1025 case GOT_IMSG_SEND_UPLOAD_PROGRESS:
1026 if (datalen < sizeof(*bytes_sent)) {
1027 err = got_error(GOT_ERR_PRIVSEP_MSG);
1030 memcpy(bytes_sent, imsg.data, sizeof(*bytes_sent));
1032 case GOT_IMSG_SEND_REF_STATUS:
1033 if (datalen < sizeof(iref_status)) {
1034 err = got_error(GOT_ERR_PRIVSEP_MSG);
1037 memcpy(&iref_status, imsg.data, sizeof(iref_status));
1038 if (datalen != sizeof(iref_status) + iref_status.name_len) {
1039 err = got_error(GOT_ERR_PRIVSEP_MSG);
1042 *success = iref_status.success;
1043 *refname = strndup(imsg.data + sizeof(iref_status),
1044 iref_status.name_len);
1046 case GOT_IMSG_SEND_DONE:
1048 err = got_error(GOT_ERR_PRIVSEP_MSG);
1054 err = got_error(GOT_ERR_PRIVSEP_MSG);
1062 const struct got_error *
1063 got_privsep_send_index_pack_req(struct imsgbuf *ibuf, uint8_t *pack_sha1,
1066 const struct got_error *err = NULL;
1068 /* Keep in sync with struct got_imsg_index_pack_request */
1069 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_REQUEST, 0, 0, fd,
1070 pack_sha1, SHA1_DIGEST_LENGTH) == -1) {
1071 err = got_error_from_errno("imsg_compose INDEX_REQUEST");
1075 return flush_imsg(ibuf);
1078 const struct got_error *
1079 got_privsep_send_index_pack_outfd(struct imsgbuf *ibuf, int fd)
1081 return send_fd(ibuf, GOT_IMSG_IDXPACK_OUTFD, fd);
1084 const struct got_error *
1085 got_privsep_recv_index_progress(int *done, int *nobj_total,
1086 int *nobj_indexed, int *nobj_loose, int *nobj_resolved,
1087 struct imsgbuf *ibuf)
1089 const struct got_error *err = NULL;
1091 struct got_imsg_index_pack_progress *iprogress;
1099 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1103 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1104 switch (imsg.hdr.type) {
1105 case GOT_IMSG_ERROR:
1106 if (datalen < sizeof(struct got_imsg_error)) {
1107 err = got_error(GOT_ERR_PRIVSEP_LEN);
1110 err = recv_imsg_error(&imsg, datalen);
1112 case GOT_IMSG_IDXPACK_PROGRESS:
1113 if (datalen < sizeof(*iprogress)) {
1114 err = got_error(GOT_ERR_PRIVSEP_LEN);
1117 iprogress = (struct got_imsg_index_pack_progress *)imsg.data;
1118 *nobj_total = iprogress->nobj_total;
1119 *nobj_indexed = iprogress->nobj_indexed;
1120 *nobj_loose = iprogress->nobj_loose;
1121 *nobj_resolved = iprogress->nobj_resolved;
1123 case GOT_IMSG_IDXPACK_DONE:
1125 err = got_error(GOT_ERR_PRIVSEP_LEN);
1131 err = got_error(GOT_ERR_PRIVSEP_MSG);
1139 const struct got_error *
1140 got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
1141 struct imsgbuf *ibuf)
1143 struct got_imsg_object *iobj;
1144 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1146 if (datalen != sizeof(*iobj))
1147 return got_error(GOT_ERR_PRIVSEP_LEN);
1150 *obj = calloc(1, sizeof(**obj));
1152 return got_error_from_errno("calloc");
1154 memcpy((*obj)->id.sha1, iobj->id, SHA1_DIGEST_LENGTH);
1155 (*obj)->type = iobj->type;
1156 (*obj)->flags = iobj->flags;
1157 (*obj)->hdrlen = iobj->hdrlen;
1158 (*obj)->size = iobj->size;
1159 /* path_packfile is handled by caller */
1160 if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
1161 (*obj)->pack_offset = iobj->pack_offset;
1162 (*obj)->pack_idx = iobj->pack_idx;
1164 STAILQ_INIT(&(*obj)->deltas.entries);
1168 const struct got_error *
1169 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
1171 const struct got_error *err = NULL;
1173 const size_t min_datalen =
1174 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
1178 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1182 switch (imsg.hdr.type) {
1183 case GOT_IMSG_OBJECT:
1184 err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
1187 err = got_error(GOT_ERR_PRIVSEP_MSG);
1196 static const struct got_error *
1197 send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
1200 const struct got_error *err = NULL;
1201 size_t offset, remain;
1204 remain = logmsg_len;
1205 while (remain > 0) {
1206 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1208 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
1209 commit->logmsg + offset, n) == -1) {
1210 err = got_error_from_errno("imsg_compose "
1215 err = flush_imsg(ibuf);
1226 const struct got_error *
1227 got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
1229 const struct got_error *err = NULL;
1230 struct got_imsg_commit_object *icommit;
1233 struct got_object_qid *qid;
1234 size_t author_len = strlen(commit->author);
1235 size_t committer_len = strlen(commit->committer);
1236 size_t logmsg_len = strlen(commit->logmsg);
1238 total = sizeof(*icommit) + author_len + committer_len +
1239 commit->nparents * SHA1_DIGEST_LENGTH;
1241 buf = malloc(total);
1243 return got_error_from_errno("malloc");
1245 icommit = (struct got_imsg_commit_object *)buf;
1246 memcpy(icommit->tree_id, commit->tree_id->sha1,
1247 sizeof(icommit->tree_id));
1248 icommit->author_len = author_len;
1249 icommit->author_time = commit->author_time;
1250 icommit->author_gmtoff = commit->author_gmtoff;
1251 icommit->committer_len = committer_len;
1252 icommit->committer_time = commit->committer_time;
1253 icommit->committer_gmtoff = commit->committer_gmtoff;
1254 icommit->logmsg_len = logmsg_len;
1255 icommit->nparents = commit->nparents;
1257 len = sizeof(*icommit);
1258 memcpy(buf + len, commit->author, author_len);
1260 memcpy(buf + len, commit->committer, committer_len);
1261 len += committer_len;
1262 STAILQ_FOREACH(qid, &commit->parent_ids, entry) {
1263 memcpy(buf + len, &qid->id, SHA1_DIGEST_LENGTH);
1264 len += SHA1_DIGEST_LENGTH;
1267 if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
1268 err = got_error_from_errno("imsg_compose COMMIT");
1272 if (logmsg_len == 0 ||
1273 logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1274 err = flush_imsg(ibuf);
1278 err = send_commit_logmsg(ibuf, commit, logmsg_len);
1284 static const struct got_error *
1285 get_commit_from_imsg(struct got_commit_object **commit,
1286 struct imsg *imsg, size_t datalen, struct imsgbuf *ibuf)
1288 const struct got_error *err = NULL;
1289 struct got_imsg_commit_object *icommit;
1293 if (datalen < sizeof(*icommit))
1294 return got_error(GOT_ERR_PRIVSEP_LEN);
1296 icommit = imsg->data;
1297 if (datalen != sizeof(*icommit) + icommit->author_len +
1298 icommit->committer_len +
1299 icommit->nparents * SHA1_DIGEST_LENGTH)
1300 return got_error(GOT_ERR_PRIVSEP_LEN);
1302 if (icommit->nparents < 0)
1303 return got_error(GOT_ERR_PRIVSEP_LEN);
1305 len += sizeof(*icommit);
1307 *commit = got_object_commit_alloc_partial();
1308 if (*commit == NULL)
1309 return got_error_from_errno(
1310 "got_object_commit_alloc_partial");
1312 memcpy((*commit)->tree_id->sha1, icommit->tree_id,
1313 SHA1_DIGEST_LENGTH);
1314 (*commit)->author_time = icommit->author_time;
1315 (*commit)->author_gmtoff = icommit->author_gmtoff;
1316 (*commit)->committer_time = icommit->committer_time;
1317 (*commit)->committer_gmtoff = icommit->committer_gmtoff;
1319 if (icommit->author_len == 0) {
1320 (*commit)->author = strdup("");
1321 if ((*commit)->author == NULL) {
1322 err = got_error_from_errno("strdup");
1326 (*commit)->author = malloc(icommit->author_len + 1);
1327 if ((*commit)->author == NULL) {
1328 err = got_error_from_errno("malloc");
1331 memcpy((*commit)->author, imsg->data + len,
1332 icommit->author_len);
1333 (*commit)->author[icommit->author_len] = '\0';
1335 len += icommit->author_len;
1337 if (icommit->committer_len == 0) {
1338 (*commit)->committer = strdup("");
1339 if ((*commit)->committer == NULL) {
1340 err = got_error_from_errno("strdup");
1344 (*commit)->committer =
1345 malloc(icommit->committer_len + 1);
1346 if ((*commit)->committer == NULL) {
1347 err = got_error_from_errno("malloc");
1350 memcpy((*commit)->committer, imsg->data + len,
1351 icommit->committer_len);
1352 (*commit)->committer[icommit->committer_len] = '\0';
1354 len += icommit->committer_len;
1356 if (icommit->logmsg_len == 0) {
1357 (*commit)->logmsg = strdup("");
1358 if ((*commit)->logmsg == NULL) {
1359 err = got_error_from_errno("strdup");
1363 size_t offset = 0, remain = icommit->logmsg_len;
1365 (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
1366 if ((*commit)->logmsg == NULL) {
1367 err = got_error_from_errno("malloc");
1370 while (remain > 0) {
1371 struct imsg imsg_log;
1372 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1375 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1379 if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG) {
1380 err = got_error(GOT_ERR_PRIVSEP_MSG);
1384 memcpy((*commit)->logmsg + offset,
1386 imsg_free(&imsg_log);
1390 (*commit)->logmsg[icommit->logmsg_len] = '\0';
1393 for (i = 0; i < icommit->nparents; i++) {
1394 struct got_object_qid *qid;
1396 err = got_object_qid_alloc_partial(&qid);
1399 memcpy(&qid->id, imsg->data + len +
1400 i * SHA1_DIGEST_LENGTH, sizeof(qid->id));
1401 STAILQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
1402 (*commit)->nparents++;
1406 got_object_commit_close(*commit);
1412 const struct got_error *
1413 got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
1415 const struct got_error *err = NULL;
1418 const size_t min_datalen =
1419 MIN(sizeof(struct got_imsg_error),
1420 sizeof(struct got_imsg_commit_object));
1424 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1428 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1430 switch (imsg.hdr.type) {
1431 case GOT_IMSG_COMMIT:
1432 err = get_commit_from_imsg(commit, &imsg, datalen, ibuf);
1435 err = got_error(GOT_ERR_PRIVSEP_MSG);
1444 static const struct got_error *
1445 send_tree_entries(struct imsgbuf *ibuf, struct got_parsed_tree_entry *entries,
1446 int idx0, int idxN, size_t len)
1449 struct got_imsg_tree_entries ientries;
1452 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_ENTRIES, 0, 0, len);
1454 return got_error_from_errno("imsg_create TREE_ENTRY");
1456 ientries.nentries = idxN - idx0 + 1;
1457 if (imsg_add(wbuf, &ientries, sizeof(ientries)) == -1)
1458 return got_error_from_errno("imsg_add TREE_ENTRY");
1460 for (i = idx0; i <= idxN; i++) {
1461 struct got_parsed_tree_entry *pte = &entries[i];
1463 /* Keep in sync with struct got_imsg_tree_object definition! */
1464 if (imsg_add(wbuf, pte->id, SHA1_DIGEST_LENGTH) == -1)
1465 return got_error_from_errno("imsg_add TREE_ENTRY");
1466 if (imsg_add(wbuf, &pte->mode, sizeof(pte->mode)) == -1)
1467 return got_error_from_errno("imsg_add TREE_ENTRY");
1468 if (imsg_add(wbuf, &pte->namelen, sizeof(pte->namelen)) == -1)
1469 return got_error_from_errno("imsg_add TREE_ENTRY");
1471 /* Remaining bytes are the entry's name. */
1472 if (imsg_add(wbuf, pte->name, pte->namelen) == -1)
1473 return got_error_from_errno("imsg_add TREE_ENTRY");
1477 imsg_close(ibuf, wbuf);
1481 const struct got_error *
1482 got_privsep_send_tree(struct imsgbuf *ibuf,
1483 struct got_parsed_tree_entry *entries, int nentries)
1485 const struct got_error *err = NULL;
1486 struct got_imsg_tree_object itree;
1490 itree.nentries = nentries;
1491 if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
1493 return got_error_from_errno("imsg_compose TREE");
1495 entries_len = sizeof(struct got_imsg_tree_entries);
1497 for (j = 0; j < nentries; j++) {
1498 struct got_parsed_tree_entry *pte = &entries[j];
1499 size_t len = sizeof(*pte) + pte->namelen;
1502 entries_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1503 err = send_tree_entries(ibuf, entries,
1504 i, j - 1, entries_len);
1508 entries_len = sizeof(struct got_imsg_tree_entries);
1515 err = send_tree_entries(ibuf, entries, i, j - 1, entries_len);
1520 return flush_imsg(ibuf);
1523 const struct got_error *
1524 got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
1526 const struct got_error *err = NULL;
1527 const size_t min_datalen =
1528 MIN(sizeof(struct got_imsg_error),
1529 sizeof(struct got_imsg_tree_object));
1530 struct got_imsg_tree_object *itree;
1536 err = read_imsg(ibuf);
1544 struct got_imsg_tree_entries *ientries;
1545 struct got_tree_entry *te = NULL;
1548 n = imsg_get(ibuf, &imsg);
1551 if (nentries < (*tree)->nentries) {
1552 err = read_imsg(ibuf);
1559 err = got_error(GOT_ERR_PRIVSEP_MSG);
1564 if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen) {
1566 err = got_error(GOT_ERR_PRIVSEP_LEN);
1570 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1572 switch (imsg.hdr.type) {
1573 case GOT_IMSG_ERROR:
1574 err = recv_imsg_error(&imsg, datalen);
1577 /* This message should only appear once. */
1578 if (*tree != NULL) {
1579 err = got_error(GOT_ERR_PRIVSEP_MSG);
1582 if (datalen != sizeof(*itree)) {
1583 err = got_error(GOT_ERR_PRIVSEP_LEN);
1587 if (itree->nentries < 0) {
1588 err = got_error(GOT_ERR_PRIVSEP_LEN);
1591 *tree = malloc(sizeof(**tree));
1592 if (*tree == NULL) {
1593 err = got_error_from_errno("malloc");
1596 (*tree)->entries = calloc(itree->nentries,
1597 sizeof(struct got_tree_entry));
1598 if ((*tree)->entries == NULL) {
1599 err = got_error_from_errno("malloc");
1604 (*tree)->nentries = itree->nentries;
1605 (*tree)->refcnt = 0;
1607 case GOT_IMSG_TREE_ENTRIES:
1608 /* This message should be preceeded by GOT_IMSG_TREE. */
1609 if (*tree == NULL) {
1610 err = got_error(GOT_ERR_PRIVSEP_MSG);
1613 if (datalen <= sizeof(*ientries) ||
1614 datalen > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1615 err = got_error(GOT_ERR_PRIVSEP_LEN);
1619 ientries = imsg.data;
1620 if (ientries->nentries > INT_MAX) {
1621 err = got_error_msg(GOT_ERR_NO_SPACE,
1622 "too many tree entries");
1625 te_offset = sizeof(*ientries);
1626 for (i = 0; i < ientries->nentries; i++) {
1627 struct got_imsg_tree_entry ite;
1628 const char *te_name;
1629 uint8_t *buf = imsg.data + te_offset;
1631 if (te_offset >= datalen) {
1632 err = got_error(GOT_ERR_PRIVSEP_LEN);
1636 /* Might not be aligned, size is ~32 bytes. */
1637 memcpy(&ite, buf, sizeof(ite));
1639 if (ite.namelen >= sizeof(te->name)) {
1640 err = got_error(GOT_ERR_PRIVSEP_LEN);
1643 if (te_offset + sizeof(ite) + ite.namelen >
1645 err = got_error(GOT_ERR_PRIVSEP_LEN);
1648 if (nentries >= (*tree)->nentries) {
1649 err = got_error(GOT_ERR_PRIVSEP_LEN);
1652 te = &(*tree)->entries[nentries];
1653 te_name = buf + sizeof(ite);
1654 memcpy(te->name, te_name, ite.namelen);
1655 te->name[ite.namelen] = '\0';
1656 memcpy(te->id.sha1, ite.id, SHA1_DIGEST_LENGTH);
1657 te->mode = ite.mode;
1661 te_offset += sizeof(ite) + ite.namelen;
1665 err = got_error(GOT_ERR_PRIVSEP_MSG);
1674 if (*tree && (*tree)->nentries != nentries) {
1676 err = got_error(GOT_ERR_PRIVSEP_LEN);
1677 got_object_tree_close(*tree);
1684 const struct got_error *
1685 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
1686 const uint8_t *data)
1688 struct got_imsg_blob iblob;
1691 iblob.hdrlen = hdrlen;
1696 if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
1697 return got_error(GOT_ERR_NO_SPACE);
1699 buf = malloc(sizeof(iblob) + size);
1701 return got_error_from_errno("malloc");
1703 memcpy(buf, &iblob, sizeof(iblob));
1704 memcpy(buf + sizeof(iblob), data, size);
1705 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
1706 sizeof(iblob) + size) == -1) {
1708 return got_error_from_errno("imsg_compose BLOB");
1712 /* Data has already been written to file descriptor. */
1713 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
1714 sizeof(iblob)) == -1)
1715 return got_error_from_errno("imsg_compose BLOB");
1719 return flush_imsg(ibuf);
1722 const struct got_error *
1723 got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1724 struct imsgbuf *ibuf)
1726 const struct got_error *err = NULL;
1728 struct got_imsg_blob *iblob;
1733 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1737 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1739 switch (imsg.hdr.type) {
1741 if (datalen < sizeof(*iblob)) {
1742 err = got_error(GOT_ERR_PRIVSEP_LEN);
1746 *size = iblob->size;
1747 *hdrlen = iblob->hdrlen;
1749 if (datalen == sizeof(*iblob)) {
1750 /* Data has been written to file descriptor. */
1754 if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX) {
1755 err = got_error(GOT_ERR_PRIVSEP_LEN);
1759 *outbuf = malloc(*size);
1760 if (*outbuf == NULL) {
1761 err = got_error_from_errno("malloc");
1764 memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
1767 err = got_error(GOT_ERR_PRIVSEP_MSG);
1776 static const struct got_error *
1777 send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
1779 const struct got_error *err = NULL;
1780 size_t offset, remain;
1783 remain = tagmsg_len;
1784 while (remain > 0) {
1785 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1787 if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
1788 tag->tagmsg + offset, n) == -1) {
1789 err = got_error_from_errno("imsg_compose TAG_TAGMSG");
1793 err = flush_imsg(ibuf);
1804 const struct got_error *
1805 got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
1807 const struct got_error *err = NULL;
1808 struct got_imsg_tag_object *itag;
1811 size_t tag_len = strlen(tag->tag);
1812 size_t tagger_len = strlen(tag->tagger);
1813 size_t tagmsg_len = strlen(tag->tagmsg);
1815 total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
1817 buf = malloc(total);
1819 return got_error_from_errno("malloc");
1821 itag = (struct got_imsg_tag_object *)buf;
1822 memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
1823 itag->obj_type = tag->obj_type;
1824 itag->tag_len = tag_len;
1825 itag->tagger_len = tagger_len;
1826 itag->tagger_time = tag->tagger_time;
1827 itag->tagger_gmtoff = tag->tagger_gmtoff;
1828 itag->tagmsg_len = tagmsg_len;
1830 len = sizeof(*itag);
1831 memcpy(buf + len, tag->tag, tag_len);
1833 memcpy(buf + len, tag->tagger, tagger_len);
1836 if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
1837 err = got_error_from_errno("imsg_compose TAG");
1841 if (tagmsg_len == 0 ||
1842 tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1843 err = flush_imsg(ibuf);
1847 err = send_tagmsg(ibuf, tag, tagmsg_len);
1853 const struct got_error *
1854 got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
1856 const struct got_error *err = NULL;
1858 struct got_imsg_tag_object *itag;
1859 size_t len, datalen;
1860 const size_t min_datalen =
1861 MIN(sizeof(struct got_imsg_error),
1862 sizeof(struct got_imsg_tag_object));
1866 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1870 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1873 switch (imsg.hdr.type) {
1875 if (datalen < sizeof(*itag)) {
1876 err = got_error(GOT_ERR_PRIVSEP_LEN);
1880 if (datalen != sizeof(*itag) + itag->tag_len +
1882 err = got_error(GOT_ERR_PRIVSEP_LEN);
1885 len += sizeof(*itag);
1887 *tag = calloc(1, sizeof(**tag));
1889 err = got_error_from_errno("calloc");
1893 memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
1895 if (itag->tag_len == 0) {
1896 (*tag)->tag = strdup("");
1897 if ((*tag)->tag == NULL) {
1898 err = got_error_from_errno("strdup");
1902 (*tag)->tag = malloc(itag->tag_len + 1);
1903 if ((*tag)->tag == NULL) {
1904 err = got_error_from_errno("malloc");
1907 memcpy((*tag)->tag, imsg.data + len,
1909 (*tag)->tag[itag->tag_len] = '\0';
1911 len += itag->tag_len;
1913 (*tag)->obj_type = itag->obj_type;
1914 (*tag)->tagger_time = itag->tagger_time;
1915 (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1917 if (itag->tagger_len == 0) {
1918 (*tag)->tagger = strdup("");
1919 if ((*tag)->tagger == NULL) {
1920 err = got_error_from_errno("strdup");
1924 (*tag)->tagger = malloc(itag->tagger_len + 1);
1925 if ((*tag)->tagger == NULL) {
1926 err = got_error_from_errno("malloc");
1929 memcpy((*tag)->tagger, imsg.data + len,
1931 (*tag)->tagger[itag->tagger_len] = '\0';
1933 len += itag->tagger_len;
1935 if (itag->tagmsg_len == 0) {
1936 (*tag)->tagmsg = strdup("");
1937 if ((*tag)->tagmsg == NULL) {
1938 err = got_error_from_errno("strdup");
1942 size_t offset = 0, remain = itag->tagmsg_len;
1944 (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1945 if ((*tag)->tagmsg == NULL) {
1946 err = got_error_from_errno("malloc");
1949 while (remain > 0) {
1950 struct imsg imsg_log;
1951 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1954 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1958 if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1959 return got_error(GOT_ERR_PRIVSEP_MSG);
1961 memcpy((*tag)->tagmsg + offset, imsg_log.data,
1963 imsg_free(&imsg_log);
1967 (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1972 err = got_error(GOT_ERR_PRIVSEP_MSG);
1981 const struct got_error *
1982 got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1983 struct got_packidx *packidx)
1985 const struct got_error *err = NULL;
1986 struct got_imsg_packidx ipackidx;
1987 struct got_imsg_pack ipack;
1990 ipackidx.len = packidx->len;
1991 ipackidx.packfile_size = pack->filesize;
1992 fd = dup(packidx->fd);
1994 return got_error_from_errno("dup");
1996 if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
1997 sizeof(ipackidx)) == -1) {
1998 err = got_error_from_errno("imsg_compose PACKIDX");
2003 if (strlcpy(ipack.path_packfile, pack->path_packfile,
2004 sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
2005 return got_error(GOT_ERR_NO_SPACE);
2006 ipack.filesize = pack->filesize;
2010 return got_error_from_errno("dup");
2012 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
2014 err = got_error_from_errno("imsg_compose PACK");
2019 return flush_imsg(ibuf);
2022 const struct got_error *
2023 got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
2024 struct got_object_id *id)
2026 struct got_imsg_packed_object iobj;
2029 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
2031 if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
2032 &iobj, sizeof(iobj)) == -1)
2033 return got_error_from_errno("imsg_compose "
2034 "PACKED_OBJECT_REQUEST");
2036 return flush_imsg(ibuf);
2039 const struct got_error *
2040 got_privsep_send_packed_raw_obj_req(struct imsgbuf *ibuf, int idx,
2041 struct got_object_id *id)
2043 struct got_imsg_packed_object iobj;
2046 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
2048 if (imsg_compose(ibuf, GOT_IMSG_PACKED_RAW_OBJECT_REQUEST, 0, 0, -1,
2049 &iobj, sizeof(iobj)) == -1)
2050 return got_error_from_errno("imsg_compose "
2051 "PACKED_OBJECT_REQUEST");
2053 return flush_imsg(ibuf);
2056 const struct got_error *
2057 got_privsep_send_gitconfig_parse_req(struct imsgbuf *ibuf, int fd)
2059 const struct got_error *err = NULL;
2061 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_PARSE_REQUEST, 0, 0, fd,
2063 err = got_error_from_errno("imsg_compose "
2064 "GITCONFIG_PARSE_REQUEST");
2069 return flush_imsg(ibuf);
2072 const struct got_error *
2073 got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *ibuf)
2075 if (imsg_compose(ibuf,
2076 GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST, 0, 0, -1,
2078 return got_error_from_errno("imsg_compose "
2079 "GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST");
2081 return flush_imsg(ibuf);
2084 const struct got_error *
2085 got_privsep_send_gitconfig_repository_extensions_req(struct imsgbuf *ibuf)
2087 if (imsg_compose(ibuf,
2088 GOT_IMSG_GITCONFIG_REPOSITORY_EXTENSIONS_REQUEST, 0, 0, -1,
2090 return got_error_from_errno("imsg_compose "
2091 "GITCONFIG_REPOSITORY_EXTENSIONS_REQUEST");
2093 return flush_imsg(ibuf);
2097 const struct got_error *
2098 got_privsep_send_gitconfig_author_name_req(struct imsgbuf *ibuf)
2100 if (imsg_compose(ibuf,
2101 GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST, 0, 0, -1, NULL, 0) == -1)
2102 return got_error_from_errno("imsg_compose "
2103 "GITCONFIG_AUTHOR_NAME_REQUEST");
2105 return flush_imsg(ibuf);
2108 const struct got_error *
2109 got_privsep_send_gitconfig_author_email_req(struct imsgbuf *ibuf)
2111 if (imsg_compose(ibuf,
2112 GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST, 0, 0, -1, NULL, 0) == -1)
2113 return got_error_from_errno("imsg_compose "
2114 "GITCONFIG_AUTHOR_EMAIL_REQUEST");
2116 return flush_imsg(ibuf);
2119 const struct got_error *
2120 got_privsep_send_gitconfig_remotes_req(struct imsgbuf *ibuf)
2122 if (imsg_compose(ibuf,
2123 GOT_IMSG_GITCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
2124 return got_error_from_errno("imsg_compose "
2125 "GITCONFIG_REMOTE_REQUEST");
2127 return flush_imsg(ibuf);
2130 const struct got_error *
2131 got_privsep_send_gitconfig_owner_req(struct imsgbuf *ibuf)
2133 if (imsg_compose(ibuf,
2134 GOT_IMSG_GITCONFIG_OWNER_REQUEST, 0, 0, -1, NULL, 0) == -1)
2135 return got_error_from_errno("imsg_compose "
2136 "GITCONFIG_OWNER_REQUEST");
2138 return flush_imsg(ibuf);
2141 const struct got_error *
2142 got_privsep_recv_gitconfig_str(char **str, struct imsgbuf *ibuf)
2144 const struct got_error *err = NULL;
2147 const size_t min_datalen = 0;
2151 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2154 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2156 switch (imsg.hdr.type) {
2157 case GOT_IMSG_GITCONFIG_STR_VAL:
2160 /* datalen does not include terminating \0 */
2161 *str = malloc(datalen + 1);
2163 err = got_error_from_errno("malloc");
2166 memcpy(*str, imsg.data, datalen);
2167 (*str)[datalen] = '\0';
2170 err = got_error(GOT_ERR_PRIVSEP_MSG);
2178 const struct got_error *
2179 got_privsep_recv_gitconfig_int(int *val, struct imsgbuf *ibuf)
2181 const struct got_error *err = NULL;
2184 const size_t min_datalen =
2185 MIN(sizeof(struct got_imsg_error), sizeof(int));
2189 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2192 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2194 switch (imsg.hdr.type) {
2195 case GOT_IMSG_GITCONFIG_INT_VAL:
2196 if (datalen != sizeof(*val)) {
2197 err = got_error(GOT_ERR_PRIVSEP_LEN);
2200 memcpy(val, imsg.data, sizeof(*val));
2203 err = got_error(GOT_ERR_PRIVSEP_MSG);
2212 free_remote_data(struct got_remote_repo *remote)
2217 free(remote->fetch_url);
2218 free(remote->send_url);
2219 for (i = 0; i < remote->nfetch_branches; i++)
2220 free(remote->fetch_branches[i]);
2221 free(remote->fetch_branches);
2222 for (i = 0; i < remote->nsend_branches; i++)
2223 free(remote->send_branches[i]);
2224 free(remote->send_branches);
2225 for (i = 0; i < remote->nfetch_refs; i++)
2226 free(remote->fetch_refs[i]);
2227 free(remote->fetch_refs);
2230 const struct got_error *
2231 got_privsep_recv_gitconfig_remotes(struct got_remote_repo **remotes,
2232 int *nremotes, struct imsgbuf *ibuf)
2234 const struct got_error *err = NULL;
2237 struct got_imsg_remotes iremotes;
2238 struct got_imsg_remote iremote;
2242 iremotes.nremotes = 0;
2244 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremotes));
2247 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2249 switch (imsg.hdr.type) {
2250 case GOT_IMSG_GITCONFIG_REMOTES:
2251 if (datalen != sizeof(iremotes)) {
2252 err = got_error(GOT_ERR_PRIVSEP_LEN);
2255 memcpy(&iremotes, imsg.data, sizeof(iremotes));
2256 if (iremotes.nremotes == 0) {
2263 return got_error(GOT_ERR_PRIVSEP_MSG);
2268 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(**remotes));
2269 if (*remotes == NULL)
2270 return got_error_from_errno("recallocarray");
2272 while (*nremotes < iremotes.nremotes) {
2273 struct got_remote_repo *remote;
2275 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremote));
2278 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2280 switch (imsg.hdr.type) {
2281 case GOT_IMSG_GITCONFIG_REMOTE:
2282 remote = &(*remotes)[*nremotes];
2283 memset(remote, 0, sizeof(*remote));
2284 if (datalen < sizeof(iremote)) {
2285 err = got_error(GOT_ERR_PRIVSEP_LEN);
2288 memcpy(&iremote, imsg.data, sizeof(iremote));
2289 if (iremote.name_len == 0 ||
2290 iremote.fetch_url_len == 0 ||
2291 iremote.send_url_len == 0 ||
2292 (sizeof(iremote) + iremote.name_len +
2293 iremote.fetch_url_len + iremote.send_url_len) > datalen) {
2294 err = got_error(GOT_ERR_PRIVSEP_LEN);
2297 remote->name = strndup(imsg.data + sizeof(iremote),
2299 if (remote->name == NULL) {
2300 err = got_error_from_errno("strndup");
2303 remote->fetch_url = strndup(imsg.data + sizeof(iremote) +
2304 iremote.name_len, iremote.fetch_url_len);
2305 if (remote->fetch_url == NULL) {
2306 err = got_error_from_errno("strndup");
2307 free_remote_data(remote);
2310 remote->send_url = strndup(imsg.data + sizeof(iremote) +
2311 iremote.name_len + iremote.fetch_url_len,
2312 iremote.send_url_len);
2313 if (remote->send_url == NULL) {
2314 err = got_error_from_errno("strndup");
2315 free_remote_data(remote);
2318 remote->mirror_references = iremote.mirror_references;
2319 remote->fetch_all_branches = iremote.fetch_all_branches;
2320 remote->nfetch_branches = 0;
2321 remote->fetch_branches = NULL;
2322 remote->nsend_branches = 0;
2323 remote->send_branches = NULL;
2324 remote->nfetch_refs = 0;
2325 remote->fetch_refs = NULL;
2329 err = got_error(GOT_ERR_PRIVSEP_MSG);
2340 for (i = 0; i < *nremotes; i++)
2341 free_remote_data(&(*remotes)[i]);
2349 const struct got_error *
2350 got_privsep_send_gotconfig_parse_req(struct imsgbuf *ibuf, int fd)
2352 const struct got_error *err = NULL;
2354 if (imsg_compose(ibuf, GOT_IMSG_GOTCONFIG_PARSE_REQUEST, 0, 0, fd,
2356 err = got_error_from_errno("imsg_compose "
2357 "GOTCONFIG_PARSE_REQUEST");
2362 return flush_imsg(ibuf);
2365 const struct got_error *
2366 got_privsep_send_gotconfig_author_req(struct imsgbuf *ibuf)
2368 if (imsg_compose(ibuf,
2369 GOT_IMSG_GOTCONFIG_AUTHOR_REQUEST, 0, 0, -1, NULL, 0) == -1)
2370 return got_error_from_errno("imsg_compose "
2371 "GOTCONFIG_AUTHOR_REQUEST");
2373 return flush_imsg(ibuf);
2376 const struct got_error *
2377 got_privsep_send_gotconfig_remotes_req(struct imsgbuf *ibuf)
2379 if (imsg_compose(ibuf,
2380 GOT_IMSG_GOTCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
2381 return got_error_from_errno("imsg_compose "
2382 "GOTCONFIG_REMOTE_REQUEST");
2384 return flush_imsg(ibuf);
2387 const struct got_error *
2388 got_privsep_recv_gotconfig_str(char **str, struct imsgbuf *ibuf)
2390 const struct got_error *err = NULL;
2393 const size_t min_datalen = 0;
2397 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2400 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2402 switch (imsg.hdr.type) {
2403 case GOT_IMSG_ERROR:
2404 if (datalen < sizeof(struct got_imsg_error)) {
2405 err = got_error(GOT_ERR_PRIVSEP_LEN);
2408 err = recv_imsg_error(&imsg, datalen);
2410 case GOT_IMSG_GOTCONFIG_STR_VAL:
2413 /* datalen does not include terminating \0 */
2414 *str = malloc(datalen + 1);
2416 err = got_error_from_errno("malloc");
2419 memcpy(*str, imsg.data, datalen);
2420 (*str)[datalen] = '\0';
2423 err = got_error(GOT_ERR_PRIVSEP_MSG);
2431 const struct got_error *
2432 got_privsep_recv_gotconfig_remotes(struct got_remote_repo **remotes,
2433 int *nremotes, struct imsgbuf *ibuf)
2435 const struct got_error *err = NULL;
2438 struct got_imsg_remotes iremotes;
2439 struct got_imsg_remote iremote;
2440 const size_t min_datalen =
2441 MIN(sizeof(struct got_imsg_error), sizeof(iremotes));
2445 iremotes.nremotes = 0;
2447 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2450 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2452 switch (imsg.hdr.type) {
2453 case GOT_IMSG_ERROR:
2454 if (datalen < sizeof(struct got_imsg_error)) {
2455 err = got_error(GOT_ERR_PRIVSEP_LEN);
2458 err = recv_imsg_error(&imsg, datalen);
2460 case GOT_IMSG_GOTCONFIG_REMOTES:
2461 if (datalen != sizeof(iremotes)) {
2462 err = got_error(GOT_ERR_PRIVSEP_LEN);
2465 memcpy(&iremotes, imsg.data, sizeof(iremotes));
2466 if (iremotes.nremotes == 0) {
2473 return got_error(GOT_ERR_PRIVSEP_MSG);
2478 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(**remotes));
2479 if (*remotes == NULL)
2480 return got_error_from_errno("recallocarray");
2482 while (*nremotes < iremotes.nremotes) {
2483 struct got_remote_repo *remote;
2484 const size_t min_datalen =
2485 MIN(sizeof(struct got_imsg_error), sizeof(iremote));
2488 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2491 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2493 switch (imsg.hdr.type) {
2494 case GOT_IMSG_ERROR:
2495 if (datalen < sizeof(struct got_imsg_error)) {
2496 err = got_error(GOT_ERR_PRIVSEP_LEN);
2499 err = recv_imsg_error(&imsg, datalen);
2501 case GOT_IMSG_GOTCONFIG_REMOTE:
2502 remote = &(*remotes)[*nremotes];
2503 memset(remote, 0, sizeof(*remote));
2504 if (datalen < sizeof(iremote)) {
2505 err = got_error(GOT_ERR_PRIVSEP_LEN);
2508 memcpy(&iremote, imsg.data, sizeof(iremote));
2509 if (iremote.name_len == 0 ||
2510 (iremote.fetch_url_len == 0 &&
2511 iremote.send_url_len == 0) ||
2512 (sizeof(iremote) + iremote.name_len +
2513 iremote.fetch_url_len + iremote.send_url_len) >
2515 err = got_error(GOT_ERR_PRIVSEP_LEN);
2518 remote->name = strndup(imsg.data + sizeof(iremote),
2520 if (remote->name == NULL) {
2521 err = got_error_from_errno("strndup");
2524 remote->fetch_url = strndup(imsg.data +
2525 sizeof(iremote) + iremote.name_len,
2526 iremote.fetch_url_len);
2527 if (remote->fetch_url == NULL) {
2528 err = got_error_from_errno("strndup");
2529 free_remote_data(remote);
2532 remote->send_url = strndup(imsg.data +
2533 sizeof(iremote) + iremote.name_len +
2534 iremote.fetch_url_len, iremote.send_url_len);
2535 if (remote->send_url == NULL) {
2536 err = got_error_from_errno("strndup");
2537 free_remote_data(remote);
2540 remote->mirror_references = iremote.mirror_references;
2541 remote->fetch_all_branches = iremote.fetch_all_branches;
2542 if (iremote.nfetch_branches > 0) {
2543 remote->fetch_branches = recallocarray(NULL, 0,
2544 iremote.nfetch_branches, sizeof(char *));
2545 if (remote->fetch_branches == NULL) {
2546 err = got_error_from_errno("calloc");
2547 free_remote_data(remote);
2551 remote->nfetch_branches = 0;
2552 for (i = 0; i < iremote.nfetch_branches; i++) {
2554 err = got_privsep_recv_gotconfig_str(&branch,
2557 free_remote_data(remote);
2560 remote->fetch_branches[i] = branch;
2561 remote->nfetch_branches++;
2563 if (iremote.nsend_branches > 0) {
2564 remote->send_branches = recallocarray(NULL, 0,
2565 iremote.nsend_branches, sizeof(char *));
2566 if (remote->send_branches == NULL) {
2567 err = got_error_from_errno("calloc");
2568 free_remote_data(remote);
2572 remote->nsend_branches = 0;
2573 for (i = 0; i < iremote.nsend_branches; i++) {
2575 err = got_privsep_recv_gotconfig_str(&branch,
2578 free_remote_data(remote);
2581 remote->send_branches[i] = branch;
2582 remote->nsend_branches++;
2584 if (iremote.nfetch_refs > 0) {
2585 remote->fetch_refs = recallocarray(NULL, 0,
2586 iremote.nfetch_refs, sizeof(char *));
2587 if (remote->fetch_refs == NULL) {
2588 err = got_error_from_errno("calloc");
2589 free_remote_data(remote);
2593 remote->nfetch_refs = 0;
2594 for (i = 0; i < iremote.nfetch_refs; i++) {
2596 err = got_privsep_recv_gotconfig_str(&ref,
2599 free_remote_data(remote);
2602 remote->fetch_refs[i] = ref;
2603 remote->nfetch_refs++;
2608 err = got_error(GOT_ERR_PRIVSEP_MSG);
2619 for (i = 0; i < *nremotes; i++)
2620 free_remote_data(&(*remotes)[i]);
2628 const struct got_error *
2629 got_privsep_send_commit_traversal_request(struct imsgbuf *ibuf,
2630 struct got_object_id *id, int idx, const char *path)
2633 size_t path_len = strlen(path) + 1;
2635 wbuf = imsg_create(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_REQUEST, 0, 0,
2636 sizeof(struct got_imsg_commit_traversal_request) + path_len);
2638 return got_error_from_errno(
2639 "imsg_create COMMIT_TRAVERSAL_REQUEST");
2640 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1)
2641 return got_error_from_errno("imsg_add "
2642 "COMMIT_TRAVERSAL_REQUEST");
2643 if (imsg_add(wbuf, &idx, sizeof(idx)) == -1)
2644 return got_error_from_errno("imsg_add "
2645 "COMMIT_TRAVERSAL_REQUEST");
2646 if (imsg_add(wbuf, path, path_len) == -1)
2647 return got_error_from_errno("imsg_add "
2648 "COMMIT_TRAVERSAL_REQUEST");
2651 imsg_close(ibuf, wbuf);
2653 return flush_imsg(ibuf);
2656 const struct got_error *
2657 got_privsep_recv_traversed_commits(struct got_commit_object **changed_commit,
2658 struct got_object_id **changed_commit_id,
2659 struct got_object_id_queue *commit_ids, struct imsgbuf *ibuf)
2661 const struct got_error *err = NULL;
2663 struct got_imsg_traversed_commits *icommits;
2667 *changed_commit = NULL;
2668 *changed_commit_id = NULL;
2671 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2675 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2676 switch (imsg.hdr.type) {
2677 case GOT_IMSG_TRAVERSED_COMMITS:
2678 icommits = imsg.data;
2679 if (datalen != sizeof(*icommits) +
2680 icommits->ncommits * SHA1_DIGEST_LENGTH) {
2681 err = got_error(GOT_ERR_PRIVSEP_LEN);
2684 for (i = 0; i < icommits->ncommits; i++) {
2685 struct got_object_qid *qid;
2686 uint8_t *sha1 = (uint8_t *)imsg.data +
2687 sizeof(*icommits) + i * SHA1_DIGEST_LENGTH;
2688 err = got_object_qid_alloc_partial(&qid);
2691 memcpy(qid->id.sha1, sha1, SHA1_DIGEST_LENGTH);
2692 STAILQ_INSERT_TAIL(commit_ids, qid, entry);
2694 /* The last commit may contain a change. */
2695 if (i == icommits->ncommits - 1) {
2696 *changed_commit_id =
2697 got_object_id_dup(&qid->id);
2698 if (*changed_commit_id == NULL) {
2699 err = got_error_from_errno(
2700 "got_object_id_dup");
2706 case GOT_IMSG_COMMIT:
2707 if (*changed_commit_id == NULL) {
2708 err = got_error(GOT_ERR_PRIVSEP_MSG);
2711 err = get_commit_from_imsg(changed_commit, &imsg,
2714 case GOT_IMSG_COMMIT_TRAVERSAL_DONE:
2718 err = got_error(GOT_ERR_PRIVSEP_MSG);
2728 got_object_id_queue_free(commit_ids);
2732 const struct got_error *
2733 got_privsep_send_raw_delta_req(struct imsgbuf *ibuf, int idx,
2734 struct got_object_id *id)
2736 struct got_imsg_raw_delta_request dreq;
2739 memcpy(dreq.id, id->sha1, SHA1_DIGEST_LENGTH);
2741 if (imsg_compose(ibuf, GOT_IMSG_RAW_DELTA_REQUEST, 0, 0, -1,
2742 &dreq, sizeof(dreq)) == -1)
2743 return got_error_from_errno("imsg_compose RAW_DELTA_REQUEST");
2745 return flush_imsg(ibuf);
2748 const struct got_error *
2749 got_privsep_send_raw_delta_outfd(struct imsgbuf *ibuf, int fd)
2751 return send_fd(ibuf, GOT_IMSG_RAW_DELTA_OUTFD, fd);
2754 const struct got_error *
2755 got_privsep_send_raw_delta(struct imsgbuf *ibuf, uint64_t base_size,
2756 uint64_t result_size, off_t delta_size, off_t delta_compressed_size,
2757 off_t delta_offset, off_t delta_out_offset, struct got_object_id *base_id)
2759 struct got_imsg_raw_delta idelta;
2762 idelta.base_size = base_size;
2763 idelta.result_size = result_size;
2764 idelta.delta_size = delta_size;
2765 idelta.delta_compressed_size = delta_compressed_size;
2766 idelta.delta_offset = delta_offset;
2767 idelta.delta_out_offset = delta_out_offset;
2768 memcpy(idelta.base_id, base_id->sha1, SHA1_DIGEST_LENGTH);
2770 ret = imsg_compose(ibuf, GOT_IMSG_RAW_DELTA, 0, 0, -1,
2771 &idelta, sizeof(idelta));
2773 return got_error_from_errno("imsg_compose RAW_DELTA");
2775 return flush_imsg(ibuf);
2778 const struct got_error *
2779 got_privsep_recv_raw_delta(uint64_t *base_size, uint64_t *result_size,
2780 off_t *delta_size, off_t *delta_compressed_size, off_t *delta_offset,
2781 off_t *delta_out_offset, struct got_object_id **base_id, struct imsgbuf *ibuf)
2783 const struct got_error *err = NULL;
2785 struct got_imsg_raw_delta *delta;
2791 *delta_compressed_size = 0;
2793 *delta_out_offset = 0;
2796 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2800 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2802 switch (imsg.hdr.type) {
2803 case GOT_IMSG_RAW_DELTA:
2804 if (datalen != sizeof(*delta)) {
2805 err = got_error(GOT_ERR_PRIVSEP_LEN);
2809 *base_size = delta->base_size;
2810 *result_size = delta->result_size;
2811 *delta_size = delta->delta_size;
2812 *delta_compressed_size = delta->delta_compressed_size;
2813 *delta_offset = delta->delta_offset;
2814 *delta_out_offset = delta->delta_out_offset;
2815 *base_id = calloc(1, sizeof(**base_id));
2816 if (*base_id == NULL) {
2817 err = got_error_from_errno("malloc");
2820 memcpy((*base_id)->sha1, delta->base_id, SHA1_DIGEST_LENGTH);
2823 err = got_error(GOT_ERR_PRIVSEP_MSG);
2836 const struct got_error *
2837 got_privsep_send_object_idlist(struct imsgbuf *ibuf,
2838 struct got_object_id **ids, size_t nids)
2840 const struct got_error *err = NULL;
2841 struct got_imsg_object_idlist idlist;
2845 if (nids > GOT_IMSG_OBJ_ID_LIST_MAX_NIDS)
2846 return got_error(GOT_ERR_NO_SPACE);
2848 wbuf = imsg_create(ibuf, GOT_IMSG_OBJ_ID_LIST, 0, 0,
2849 sizeof(idlist) + nids * sizeof(**ids));
2851 err = got_error_from_errno("imsg_create OBJ_ID_LIST");
2856 if (imsg_add(wbuf, &idlist, sizeof(idlist)) == -1)
2857 return got_error_from_errno("imsg_add OBJ_ID_LIST");
2859 for (i = 0; i < nids; i++) {
2860 struct got_object_id *id = ids[i];
2861 if (imsg_add(wbuf, id, sizeof(*id)) == -1)
2862 return got_error_from_errno("imsg_add OBJ_ID_LIST");
2866 imsg_close(ibuf, wbuf);
2868 return flush_imsg(ibuf);
2871 const struct got_error *
2872 got_privsep_send_object_idlist_done(struct imsgbuf *ibuf)
2874 if (imsg_compose(ibuf, GOT_IMSG_OBJ_ID_LIST_DONE, 0, 0, -1, NULL, 0)
2876 return got_error_from_errno("imsg_compose OBJ_ID_LIST_DONE");
2878 return flush_imsg(ibuf);
2881 const struct got_error *
2882 got_privsep_recv_object_idlist(int *done, struct got_object_id **ids,
2883 size_t *nids, struct imsgbuf *ibuf)
2885 const struct got_error *err = NULL;
2887 struct got_imsg_object_idlist *idlist;
2894 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2898 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2899 switch (imsg.hdr.type) {
2900 case GOT_IMSG_OBJ_ID_LIST:
2901 if (datalen < sizeof(*idlist)) {
2902 err = got_error(GOT_ERR_PRIVSEP_LEN);
2906 if (idlist->nids > GOT_IMSG_OBJ_ID_LIST_MAX_NIDS) {
2907 err = got_error(GOT_ERR_PRIVSEP_LEN);
2910 *nids = idlist->nids;
2911 *ids = calloc(*nids, sizeof(**ids));
2913 err = got_error_from_errno("calloc");
2916 memcpy(*ids, (uint8_t *)imsg.data + sizeof(idlist),
2917 *nids * sizeof(**ids));
2919 case GOT_IMSG_OBJ_ID_LIST_DONE:
2923 err = got_error(GOT_ERR_PRIVSEP_MSG);
2932 const struct got_error *
2933 got_privsep_send_delta_reuse_req(struct imsgbuf *ibuf)
2935 if (imsg_compose(ibuf, GOT_IMSG_DELTA_REUSE_REQUEST, 0, 0, -1, NULL, 0)
2937 return got_error_from_errno("imsg_compose DELTA_REUSE_REQUEST");
2939 return flush_imsg(ibuf);
2942 const struct got_error *
2943 got_privsep_send_reused_deltas(struct imsgbuf *ibuf,
2944 struct got_imsg_reused_delta *deltas, size_t ndeltas)
2946 const struct got_error *err = NULL;
2948 struct got_imsg_reused_deltas ideltas;
2951 if (ndeltas > GOT_IMSG_REUSED_DELTAS_MAX_NDELTAS)
2952 return got_error(GOT_ERR_NO_SPACE);
2954 wbuf = imsg_create(ibuf, GOT_IMSG_REUSED_DELTAS, 0, 0,
2955 sizeof(ideltas) + ndeltas * sizeof(*deltas));
2957 err = got_error_from_errno("imsg_create REUSED_DELTAS");
2961 ideltas.ndeltas = ndeltas;
2962 if (imsg_add(wbuf, &ideltas, sizeof(ideltas)) == -1)
2963 return got_error_from_errno("imsg_add REUSED_DELTAS");
2965 for (i = 0; i < ndeltas; i++) {
2966 struct got_imsg_reused_delta *delta = &deltas[i];
2967 if (imsg_add(wbuf, delta, sizeof(*delta)) == -1)
2968 return got_error_from_errno("imsg_add REUSED_DELTAS");
2972 imsg_close(ibuf, wbuf);
2974 return flush_imsg(ibuf);
2977 const struct got_error *
2978 got_privsep_send_reused_deltas_done(struct imsgbuf *ibuf)
2980 if (imsg_compose(ibuf, GOT_IMSG_DELTA_REUSE_DONE, 0, 0, -1, NULL, 0)
2982 return got_error_from_errno("imsg_compose DELTA_REUSE_DONE");
2984 return flush_imsg(ibuf);
2987 const struct got_error *
2988 got_privsep_recv_reused_deltas(int *done, struct got_imsg_reused_delta *deltas,
2989 size_t *ndeltas, struct imsgbuf *ibuf)
2991 const struct got_error *err = NULL;
2993 struct got_imsg_reused_deltas *ideltas;
2999 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
3003 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
3004 switch (imsg.hdr.type) {
3005 case GOT_IMSG_REUSED_DELTAS:
3006 if (datalen < sizeof(*ideltas)) {
3007 err = got_error(GOT_ERR_PRIVSEP_LEN);
3010 ideltas = imsg.data;
3011 if (ideltas->ndeltas > GOT_IMSG_OBJ_ID_LIST_MAX_NIDS) {
3012 err = got_error(GOT_ERR_PRIVSEP_LEN);
3015 *ndeltas = ideltas->ndeltas;
3016 memcpy(deltas, (uint8_t *)imsg.data + sizeof(ideltas),
3017 *ndeltas * sizeof(*deltas));
3019 case GOT_IMSG_DELTA_REUSE_DONE:
3023 err = got_error(GOT_ERR_PRIVSEP_MSG);
3032 const struct got_error *
3033 got_privsep_unveil_exec_helpers(void)
3035 const char *helpers[] = {
3036 GOT_PATH_PROG_READ_PACK,
3037 GOT_PATH_PROG_READ_OBJECT,
3038 GOT_PATH_PROG_READ_COMMIT,
3039 GOT_PATH_PROG_READ_TREE,
3040 GOT_PATH_PROG_READ_BLOB,
3041 GOT_PATH_PROG_READ_TAG,
3042 GOT_PATH_PROG_READ_GITCONFIG,
3043 GOT_PATH_PROG_READ_GOTCONFIG,
3044 GOT_PATH_PROG_READ_PATCH,
3045 GOT_PATH_PROG_FETCH_PACK,
3046 GOT_PATH_PROG_INDEX_PACK,
3047 GOT_PATH_PROG_SEND_PACK,
3051 for (i = 0; i < nitems(helpers); i++) {
3052 if (unveil(helpers[i], "x") == 0)
3054 return got_error_from_errno2("unveil", helpers[i]);
3061 got_privsep_exec_child(int imsg_fds[2], const char *path, const char *repo_path)
3063 if (close(imsg_fds[0]) == -1) {
3064 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
3068 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
3069 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
3073 closefrom(GOT_IMSG_FD_CHILD + 1);
3075 if (execl(path, path, repo_path, (char *)NULL) == -1) {
3076 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,