2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 * Copyright (c) 2020 Ori Bernstein <ori@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/types.h>
19 #include <sys/queue.h>
38 #include "got_object.h"
39 #include "got_error.h"
41 #include "got_repository.h"
43 #include "got_lib_sha1.h"
44 #include "got_lib_delta.h"
45 #include "got_lib_inflate.h"
46 #include "got_lib_object.h"
47 #include "got_lib_object_parse.h"
48 #include "got_lib_privsep.h"
49 #include "got_lib_pack.h"
52 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
56 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
59 static const struct got_error *
60 poll_fd(int fd, int events, int timeout)
68 pfd[0].events = events;
73 if (sigemptyset(&sigset) == -1)
74 return got_error_from_errno("sigemptyset");
75 if (sigaddset(&sigset, SIGWINCH) == -1)
76 return got_error_from_errno("sigaddset");
78 n = ppoll(pfd, 1, timeout == INFTIM ? NULL : &ts, &sigset);
80 return got_error_from_errno("ppoll");
82 return got_error(GOT_ERR_TIMEOUT);
83 if (pfd[0].revents & (POLLERR | POLLNVAL))
84 return got_error_from_errno("poll error");
85 if (pfd[0].revents & (events | POLLHUP))
88 return got_error(GOT_ERR_INTERRUPT);
91 static const struct got_error *
92 read_imsg(struct imsgbuf *ibuf)
94 const struct got_error *err;
97 err = poll_fd(ibuf->fd, POLLIN, INFTIM);
103 if (errno == EAGAIN) /* Could be a file-descriptor leak. */
104 return got_error(GOT_ERR_PRIVSEP_NO_FD);
105 return got_error(GOT_ERR_PRIVSEP_READ);
108 return got_error(GOT_ERR_PRIVSEP_PIPE);
113 const struct got_error *
114 got_privsep_wait_for_child(pid_t pid)
118 if (waitpid(pid, &child_status, 0) == -1)
119 return got_error_from_errno("waitpid");
121 if (!WIFEXITED(child_status))
122 return got_error(GOT_ERR_PRIVSEP_DIED);
124 if (WEXITSTATUS(child_status) != 0)
125 return got_error(GOT_ERR_PRIVSEP_EXIT);
130 static const struct got_error *
131 recv_imsg_error(struct imsg *imsg, size_t datalen)
133 struct got_imsg_error *ierr;
135 if (datalen != sizeof(*ierr))
136 return got_error(GOT_ERR_PRIVSEP_LEN);
139 if (ierr->code == GOT_ERR_ERRNO) {
140 static struct got_error serr;
141 serr.code = GOT_ERR_ERRNO;
142 serr.msg = strerror(ierr->errno_code);
146 return got_error(ierr->code);
149 const struct got_error *
150 got_privsep_recv_imsg(struct imsg *imsg, struct imsgbuf *ibuf,
153 const struct got_error *err;
156 n = imsg_get(ibuf, imsg);
158 return got_error_from_errno("imsg_get");
161 err = read_imsg(ibuf);
164 n = imsg_get(ibuf, imsg);
166 return got_error_from_errno("imsg_get");
169 if (imsg->hdr.len < IMSG_HEADER_SIZE + min_datalen)
170 return got_error(GOT_ERR_PRIVSEP_LEN);
172 if (imsg->hdr.type == GOT_IMSG_ERROR) {
173 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
174 return recv_imsg_error(imsg, datalen);
180 /* Attempt to send an error in an imsg. Complain on stderr as a last resort. */
182 got_privsep_send_error(struct imsgbuf *ibuf, const struct got_error *err)
184 const struct got_error *poll_err;
185 struct got_imsg_error ierr;
188 ierr.code = err->code;
189 if (err->code == GOT_ERR_ERRNO)
190 ierr.errno_code = errno;
193 ret = imsg_compose(ibuf, GOT_IMSG_ERROR, 0, 0, -1, &ierr, sizeof(ierr));
195 fprintf(stderr, "%s: error %d \"%s\": imsg_compose: %s\n",
196 getprogname(), err->code, err->msg, strerror(errno));
200 poll_err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
202 fprintf(stderr, "%s: error %d \"%s\": poll: %s\n",
203 getprogname(), err->code, err->msg, poll_err->msg);
207 ret = imsg_flush(ibuf);
209 fprintf(stderr, "%s: error %d \"%s\": imsg_flush: %s\n",
210 getprogname(), err->code, err->msg, strerror(errno));
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)
225 return got_error_from_errno("imsg_flush");
230 const struct got_error *
231 got_privsep_flush_imsg(struct imsgbuf *ibuf)
233 return flush_imsg(ibuf);
236 const struct got_error *
237 got_privsep_send_stop(int fd)
239 const struct got_error *err = NULL;
242 imsg_init(&ibuf, fd);
244 if (imsg_compose(&ibuf, GOT_IMSG_STOP, 0, 0, -1, NULL, 0) == -1)
245 return got_error_from_errno("imsg_compose STOP");
247 err = flush_imsg(&ibuf);
252 const struct got_error *
253 got_privsep_send_obj_req(struct imsgbuf *ibuf, int fd)
255 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_REQUEST, 0, 0, fd, NULL, 0)
257 return got_error_from_errno("imsg_compose OBJECT_REQUEST");
259 return flush_imsg(ibuf);
262 const struct got_error *
263 got_privsep_send_raw_obj_req(struct imsgbuf *ibuf, int fd)
265 if (imsg_compose(ibuf, GOT_IMSG_RAW_OBJECT_REQUEST, 0, 0, fd, NULL, 0)
267 return got_error_from_errno("imsg_compose RAW_OBJECT_REQUEST");
269 return flush_imsg(ibuf);
272 const struct got_error *
273 got_privsep_send_raw_obj_outfd(struct imsgbuf *ibuf, int outfd)
275 const struct got_error *err = NULL;
277 if (imsg_compose(ibuf, GOT_IMSG_RAW_OBJECT_OUTFD, 0, 0, outfd, NULL, 0)
279 err = got_error_from_errno("imsg_compose RAW_OBJECT_OUTFD");
284 return flush_imsg(ibuf);
287 const struct got_error *
288 got_privsep_send_raw_obj(struct imsgbuf *ibuf, off_t size, size_t hdrlen,
291 const struct got_error *err = NULL;
292 struct got_imsg_raw_obj iobj;
293 size_t len = sizeof(iobj);
296 iobj.hdrlen = hdrlen;
299 if (data && size + hdrlen <= GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX)
300 len += (size_t)size + hdrlen;
302 wbuf = imsg_create(ibuf, GOT_IMSG_RAW_OBJECT, 0, 0, len);
304 err = got_error_from_errno("imsg_create RAW_OBJECT");
308 if (imsg_add(wbuf, &iobj, sizeof(iobj)) == -1) {
309 err = 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 err = got_error_from_errno("imsg_add RAW_OBJECT");
323 imsg_close(ibuf, wbuf);
325 return flush_imsg(ibuf);
328 const struct got_error *
329 got_privsep_recv_raw_obj(uint8_t **outbuf, off_t *size, size_t *hdrlen,
330 struct imsgbuf *ibuf)
332 const struct got_error *err = NULL;
334 struct got_imsg_raw_obj *iobj;
339 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
343 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
345 switch (imsg.hdr.type) {
346 case GOT_IMSG_RAW_OBJECT:
347 if (datalen < sizeof(*iobj)) {
348 err = got_error(GOT_ERR_PRIVSEP_LEN);
353 *hdrlen = iobj->hdrlen;
355 if (datalen == sizeof(*iobj)) {
356 /* Data has been written to file descriptor. */
360 if (*size + *hdrlen > GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX) {
361 err = got_error(GOT_ERR_PRIVSEP_LEN);
365 *outbuf = malloc(*size + *hdrlen);
366 if (*outbuf == NULL) {
367 err = got_error_from_errno("malloc");
370 memcpy(*outbuf, imsg.data + sizeof(*iobj), *size + *hdrlen);
373 err = got_error(GOT_ERR_PRIVSEP_MSG);
382 const struct got_error *
383 got_privsep_send_commit_req(struct imsgbuf *ibuf, int fd,
384 struct got_object_id *id, int pack_idx)
386 const struct got_error *err = NULL;
387 struct got_imsg_packed_object iobj, *iobjp;
390 if (id) { /* commit is packed */
392 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
400 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_REQUEST, 0, 0, fd, iobjp, len)
402 err = got_error_from_errno("imsg_compose COMMIT_REQUEST");
407 return flush_imsg(ibuf);
410 const struct got_error *
411 got_privsep_send_tree_req(struct imsgbuf *ibuf, int fd,
412 struct got_object_id *id, int pack_idx)
414 const struct got_error *err = NULL;
416 size_t len = id ? sizeof(struct got_imsg_packed_object) : 0;
418 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_REQUEST, 0, 0, len);
420 return got_error_from_errno("imsg_create TREE_REQUEST");
422 if (id) { /* tree is packed */
423 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
424 err = got_error_from_errno("imsg_add TREE_ENTRY");
429 if (imsg_add(wbuf, &pack_idx, sizeof(pack_idx)) == -1) {
430 err = got_error_from_errno("imsg_add TREE_ENTRY");
437 imsg_close(ibuf, wbuf);
439 return flush_imsg(ibuf);
442 const struct got_error *
443 got_privsep_send_tag_req(struct imsgbuf *ibuf, int fd,
444 struct got_object_id *id, int pack_idx)
446 struct got_imsg_packed_object iobj, *iobjp;
449 if (id) { /* tag is packed */
451 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
459 if (imsg_compose(ibuf, GOT_IMSG_TAG_REQUEST, 0, 0, fd, iobjp, len)
461 return got_error_from_errno("imsg_compose TAG_REQUEST");
463 return flush_imsg(ibuf);
466 const struct got_error *
467 got_privsep_send_blob_req(struct imsgbuf *ibuf, int infd,
468 struct got_object_id *id, int pack_idx)
470 const struct got_error *err = NULL;
471 struct got_imsg_packed_object iobj, *iobjp;
474 if (id) { /* blob is packed */
476 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
484 if (imsg_compose(ibuf, GOT_IMSG_BLOB_REQUEST, 0, 0, infd, iobjp, len)
486 err = got_error_from_errno("imsg_compose BLOB_REQUEST");
491 return flush_imsg(ibuf);
494 const struct got_error *
495 got_privsep_send_blob_outfd(struct imsgbuf *ibuf, int outfd)
497 const struct got_error *err = NULL;
499 if (imsg_compose(ibuf, GOT_IMSG_BLOB_OUTFD, 0, 0, outfd, NULL, 0)
501 err = got_error_from_errno("imsg_compose BLOB_OUTFD");
506 return flush_imsg(ibuf);
509 static const struct got_error *
510 send_fd(struct imsgbuf *ibuf, int imsg_code, int fd)
512 const struct got_error *err = NULL;
514 if (imsg_compose(ibuf, imsg_code, 0, 0, fd, NULL, 0) == -1) {
515 err = got_error_from_errno("imsg_compose TMPFD");
520 return flush_imsg(ibuf);
523 const struct got_error *
524 got_privsep_send_tmpfd(struct imsgbuf *ibuf, int fd)
526 return send_fd(ibuf, GOT_IMSG_TMPFD, fd);
529 const struct got_error *
530 got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj)
532 struct got_imsg_object iobj;
534 memcpy(iobj.id, obj->id.sha1, sizeof(iobj.id));
535 iobj.type = obj->type;
536 iobj.flags = obj->flags;
537 iobj.hdrlen = obj->hdrlen;
538 iobj.size = obj->size;
539 if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
540 iobj.pack_offset = obj->pack_offset;
541 iobj.pack_idx = obj->pack_idx;
544 if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
546 return got_error_from_errno("imsg_compose OBJECT");
548 return flush_imsg(ibuf);
551 const struct got_error *
552 got_privsep_send_fetch_req(struct imsgbuf *ibuf, int fd,
553 struct got_pathlist_head *have_refs, int fetch_all_branches,
554 struct got_pathlist_head *wanted_branches,
555 struct got_pathlist_head *wanted_refs, int list_refs_only, int verbosity)
557 const struct got_error *err = NULL;
560 struct got_pathlist_entry *pe;
561 struct got_imsg_fetch_request fetchreq;
563 memset(&fetchreq, 0, sizeof(fetchreq));
564 fetchreq.fetch_all_branches = fetch_all_branches;
565 fetchreq.list_refs_only = list_refs_only;
566 fetchreq.verbosity = verbosity;
567 TAILQ_FOREACH(pe, have_refs, entry)
568 fetchreq.n_have_refs++;
569 TAILQ_FOREACH(pe, wanted_branches, entry)
570 fetchreq.n_wanted_branches++;
571 TAILQ_FOREACH(pe, wanted_refs, entry)
572 fetchreq.n_wanted_refs++;
573 len = sizeof(struct got_imsg_fetch_request);
574 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
576 return got_error(GOT_ERR_NO_SPACE);
579 if (imsg_compose(ibuf, GOT_IMSG_FETCH_REQUEST, 0, 0, fd,
580 &fetchreq, sizeof(fetchreq)) == -1)
581 return got_error_from_errno(
582 "imsg_compose FETCH_SERVER_PROGRESS");
584 err = flush_imsg(ibuf);
591 TAILQ_FOREACH(pe, have_refs, entry) {
592 const char *name = pe->path;
593 size_t name_len = pe->path_len;
594 struct got_object_id *id = pe->data;
596 len = sizeof(struct got_imsg_fetch_have_ref) + name_len;
597 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_HAVE_REF, 0, 0, len);
599 return got_error_from_errno("imsg_create FETCH_HAVE_REF");
601 /* Keep in sync with struct got_imsg_fetch_have_ref! */
602 if (imsg_add(wbuf, id->sha1, sizeof(id->sha1)) == -1) {
603 err = got_error_from_errno("imsg_add FETCH_HAVE_REF");
607 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
608 err = got_error_from_errno("imsg_add FETCH_HAVE_REF");
612 if (imsg_add(wbuf, name, name_len) == -1) {
613 err = got_error_from_errno("imsg_add FETCH_HAVE_REF");
619 imsg_close(ibuf, wbuf);
620 err = flush_imsg(ibuf);
625 TAILQ_FOREACH(pe, wanted_branches, entry) {
626 const char *name = pe->path;
627 size_t name_len = pe->path_len;
629 len = sizeof(struct got_imsg_fetch_wanted_branch) + name_len;
630 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_WANTED_BRANCH, 0, 0,
633 return got_error_from_errno(
634 "imsg_create FETCH_WANTED_BRANCH");
636 /* Keep in sync with struct got_imsg_fetch_wanted_branch! */
637 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
638 err = got_error_from_errno(
639 "imsg_add FETCH_WANTED_BRANCH");
643 if (imsg_add(wbuf, name, name_len) == -1) {
644 err = got_error_from_errno(
645 "imsg_add FETCH_WANTED_BRANCH");
651 imsg_close(ibuf, wbuf);
652 err = flush_imsg(ibuf);
657 TAILQ_FOREACH(pe, wanted_refs, entry) {
658 const char *name = pe->path;
659 size_t name_len = pe->path_len;
661 len = sizeof(struct got_imsg_fetch_wanted_ref) + name_len;
662 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_WANTED_REF, 0, 0,
665 return got_error_from_errno(
666 "imsg_create FETCH_WANTED_REF");
668 /* Keep in sync with struct got_imsg_fetch_wanted_ref! */
669 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
670 err = got_error_from_errno(
671 "imsg_add FETCH_WANTED_REF");
675 if (imsg_add(wbuf, name, name_len) == -1) {
676 err = got_error_from_errno(
677 "imsg_add FETCH_WANTED_REF");
683 imsg_close(ibuf, wbuf);
684 err = flush_imsg(ibuf);
694 const struct got_error *
695 got_privsep_send_fetch_outfd(struct imsgbuf *ibuf, int fd)
697 return send_fd(ibuf, GOT_IMSG_FETCH_OUTFD, fd);
700 const struct got_error *
701 got_privsep_recv_fetch_progress(int *done, struct got_object_id **id,
702 char **refname, struct got_pathlist_head *symrefs, char **server_progress,
703 off_t *packfile_size, uint8_t *pack_sha1, struct imsgbuf *ibuf)
705 const struct got_error *err = NULL;
708 struct got_imsg_fetch_symrefs *isymrefs = NULL;
716 *server_progress = NULL;
718 memset(pack_sha1, 0, SHA1_DIGEST_LENGTH);
720 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
724 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
725 switch (imsg.hdr.type) {
727 if (datalen < sizeof(struct got_imsg_error)) {
728 err = got_error(GOT_ERR_PRIVSEP_LEN);
731 err = recv_imsg_error(&imsg, datalen);
733 case GOT_IMSG_FETCH_SYMREFS:
734 if (datalen < sizeof(*isymrefs)) {
735 err = got_error(GOT_ERR_PRIVSEP_LEN);
738 if (isymrefs != NULL) {
739 err = got_error(GOT_ERR_PRIVSEP_MSG);
742 isymrefs = (struct got_imsg_fetch_symrefs *)imsg.data;
743 off = sizeof(*isymrefs);
744 remain = datalen - off;
745 for (n = 0; n < isymrefs->nsymrefs; n++) {
746 struct got_imsg_fetch_symref *s;
748 if (remain < sizeof(struct got_imsg_fetch_symref)) {
749 err = got_error(GOT_ERR_PRIVSEP_LEN);
752 s = (struct got_imsg_fetch_symref *)(imsg.data + off);
754 remain -= sizeof(*s);
755 if (remain < s->name_len) {
756 err = got_error(GOT_ERR_PRIVSEP_LEN);
759 name = strndup(imsg.data + off, s->name_len);
761 err = got_error_from_errno("strndup");
765 remain -= s->name_len;
766 if (remain < s->target_len) {
767 err = got_error(GOT_ERR_PRIVSEP_LEN);
771 target = strndup(imsg.data + off, s->target_len);
772 if (target == NULL) {
773 err = got_error_from_errno("strndup");
777 off += s->target_len;
778 remain -= s->target_len;
779 err = got_pathlist_append(symrefs, name, target);
787 case GOT_IMSG_FETCH_REF:
788 if (datalen <= SHA1_DIGEST_LENGTH) {
789 err = got_error(GOT_ERR_PRIVSEP_MSG);
792 *id = malloc(sizeof(**id));
794 err = got_error_from_errno("malloc");
797 memcpy((*id)->sha1, imsg.data, SHA1_DIGEST_LENGTH);
798 *refname = strndup(imsg.data + SHA1_DIGEST_LENGTH,
799 datalen - SHA1_DIGEST_LENGTH);
800 if (*refname == NULL) {
801 err = got_error_from_errno("strndup");
805 case GOT_IMSG_FETCH_SERVER_PROGRESS:
807 err = got_error(GOT_ERR_PRIVSEP_LEN);
810 *server_progress = strndup(imsg.data, datalen);
811 if (*server_progress == NULL) {
812 err = got_error_from_errno("strndup");
815 for (i = 0; i < datalen; i++) {
816 if (!isprint((unsigned char)(*server_progress)[i]) &&
817 !isspace((unsigned char)(*server_progress)[i])) {
818 err = got_error(GOT_ERR_PRIVSEP_MSG);
819 free(*server_progress);
820 *server_progress = NULL;
825 case GOT_IMSG_FETCH_DOWNLOAD_PROGRESS:
826 if (datalen < sizeof(*packfile_size)) {
827 err = got_error(GOT_ERR_PRIVSEP_MSG);
830 memcpy(packfile_size, imsg.data, sizeof(*packfile_size));
832 case GOT_IMSG_FETCH_DONE:
833 if (datalen != SHA1_DIGEST_LENGTH) {
834 err = got_error(GOT_ERR_PRIVSEP_MSG);
837 memcpy(pack_sha1, imsg.data, SHA1_DIGEST_LENGTH);
841 err = got_error(GOT_ERR_PRIVSEP_MSG);
855 const struct got_error *
856 got_privsep_send_index_pack_req(struct imsgbuf *ibuf, uint8_t *pack_sha1,
859 const struct got_error *err = NULL;
861 /* Keep in sync with struct got_imsg_index_pack_request */
862 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_REQUEST, 0, 0, fd,
863 pack_sha1, SHA1_DIGEST_LENGTH) == -1) {
864 err = got_error_from_errno("imsg_compose INDEX_REQUEST");
868 return flush_imsg(ibuf);
871 const struct got_error *
872 got_privsep_send_index_pack_outfd(struct imsgbuf *ibuf, int fd)
874 return send_fd(ibuf, GOT_IMSG_IDXPACK_OUTFD, fd);
877 const struct got_error *
878 got_privsep_recv_index_progress(int *done, int *nobj_total,
879 int *nobj_indexed, int *nobj_loose, int *nobj_resolved,
880 struct imsgbuf *ibuf)
882 const struct got_error *err = NULL;
884 struct got_imsg_index_pack_progress *iprogress;
892 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
896 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
897 switch (imsg.hdr.type) {
899 if (datalen < sizeof(struct got_imsg_error)) {
900 err = got_error(GOT_ERR_PRIVSEP_LEN);
903 err = recv_imsg_error(&imsg, datalen);
905 case GOT_IMSG_IDXPACK_PROGRESS:
906 if (datalen < sizeof(*iprogress)) {
907 err = got_error(GOT_ERR_PRIVSEP_LEN);
910 iprogress = (struct got_imsg_index_pack_progress *)imsg.data;
911 *nobj_total = iprogress->nobj_total;
912 *nobj_indexed = iprogress->nobj_indexed;
913 *nobj_loose = iprogress->nobj_loose;
914 *nobj_resolved = iprogress->nobj_resolved;
916 case GOT_IMSG_IDXPACK_DONE:
918 err = got_error(GOT_ERR_PRIVSEP_LEN);
924 err = got_error(GOT_ERR_PRIVSEP_MSG);
932 const struct got_error *
933 got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
934 struct imsgbuf *ibuf)
936 const struct got_error *err = NULL;
937 struct got_imsg_object *iobj;
938 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
940 if (datalen != sizeof(*iobj))
941 return got_error(GOT_ERR_PRIVSEP_LEN);
944 *obj = calloc(1, sizeof(**obj));
946 return got_error_from_errno("calloc");
948 memcpy((*obj)->id.sha1, iobj->id, SHA1_DIGEST_LENGTH);
949 (*obj)->type = iobj->type;
950 (*obj)->flags = iobj->flags;
951 (*obj)->hdrlen = iobj->hdrlen;
952 (*obj)->size = iobj->size;
953 /* path_packfile is handled by caller */
954 if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
955 (*obj)->pack_offset = iobj->pack_offset;
956 (*obj)->pack_idx = iobj->pack_idx;
962 const struct got_error *
963 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
965 const struct got_error *err = NULL;
967 const size_t min_datalen =
968 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
972 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
976 switch (imsg.hdr.type) {
977 case GOT_IMSG_OBJECT:
978 err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
981 err = got_error(GOT_ERR_PRIVSEP_MSG);
990 static const struct got_error *
991 send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
994 const struct got_error *err = NULL;
995 size_t offset, remain;
1000 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1002 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
1003 commit->logmsg + offset, n) == -1) {
1004 err = got_error_from_errno("imsg_compose "
1009 err = flush_imsg(ibuf);
1020 const struct got_error *
1021 got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
1023 const struct got_error *err = NULL;
1024 struct got_imsg_commit_object *icommit;
1027 struct got_object_qid *qid;
1028 size_t author_len = strlen(commit->author);
1029 size_t committer_len = strlen(commit->committer);
1030 size_t logmsg_len = strlen(commit->logmsg);
1032 total = sizeof(*icommit) + author_len + committer_len +
1033 commit->nparents * SHA1_DIGEST_LENGTH;
1035 buf = malloc(total);
1037 return got_error_from_errno("malloc");
1039 icommit = (struct got_imsg_commit_object *)buf;
1040 memcpy(icommit->tree_id, commit->tree_id->sha1,
1041 sizeof(icommit->tree_id));
1042 icommit->author_len = author_len;
1043 icommit->author_time = commit->author_time;
1044 icommit->author_gmtoff = commit->author_gmtoff;
1045 icommit->committer_len = committer_len;
1046 icommit->committer_time = commit->committer_time;
1047 icommit->committer_gmtoff = commit->committer_gmtoff;
1048 icommit->logmsg_len = logmsg_len;
1049 icommit->nparents = commit->nparents;
1051 len = sizeof(*icommit);
1052 memcpy(buf + len, commit->author, author_len);
1054 memcpy(buf + len, commit->committer, committer_len);
1055 len += committer_len;
1056 STAILQ_FOREACH(qid, &commit->parent_ids, entry) {
1057 memcpy(buf + len, qid->id, SHA1_DIGEST_LENGTH);
1058 len += SHA1_DIGEST_LENGTH;
1061 if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
1062 err = got_error_from_errno("imsg_compose COMMIT");
1066 if (logmsg_len == 0 ||
1067 logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1068 err = flush_imsg(ibuf);
1072 err = send_commit_logmsg(ibuf, commit, logmsg_len);
1078 static const struct got_error *
1079 get_commit_from_imsg(struct got_commit_object **commit,
1080 struct imsg *imsg, size_t datalen, struct imsgbuf *ibuf)
1082 const struct got_error *err = NULL;
1083 struct got_imsg_commit_object *icommit;
1087 if (datalen < sizeof(*icommit))
1088 return got_error(GOT_ERR_PRIVSEP_LEN);
1090 icommit = imsg->data;
1091 if (datalen != sizeof(*icommit) + icommit->author_len +
1092 icommit->committer_len +
1093 icommit->nparents * SHA1_DIGEST_LENGTH)
1094 return got_error(GOT_ERR_PRIVSEP_LEN);
1096 if (icommit->nparents < 0)
1097 return got_error(GOT_ERR_PRIVSEP_LEN);
1099 len += sizeof(*icommit);
1101 *commit = got_object_commit_alloc_partial();
1102 if (*commit == NULL)
1103 return got_error_from_errno(
1104 "got_object_commit_alloc_partial");
1106 memcpy((*commit)->tree_id->sha1, icommit->tree_id,
1107 SHA1_DIGEST_LENGTH);
1108 (*commit)->author_time = icommit->author_time;
1109 (*commit)->author_gmtoff = icommit->author_gmtoff;
1110 (*commit)->committer_time = icommit->committer_time;
1111 (*commit)->committer_gmtoff = icommit->committer_gmtoff;
1113 if (icommit->author_len == 0) {
1114 (*commit)->author = strdup("");
1115 if ((*commit)->author == NULL) {
1116 err = got_error_from_errno("strdup");
1120 (*commit)->author = malloc(icommit->author_len + 1);
1121 if ((*commit)->author == NULL) {
1122 err = got_error_from_errno("malloc");
1125 memcpy((*commit)->author, imsg->data + len,
1126 icommit->author_len);
1127 (*commit)->author[icommit->author_len] = '\0';
1129 len += icommit->author_len;
1131 if (icommit->committer_len == 0) {
1132 (*commit)->committer = strdup("");
1133 if ((*commit)->committer == NULL) {
1134 err = got_error_from_errno("strdup");
1138 (*commit)->committer =
1139 malloc(icommit->committer_len + 1);
1140 if ((*commit)->committer == NULL) {
1141 err = got_error_from_errno("malloc");
1144 memcpy((*commit)->committer, imsg->data + len,
1145 icommit->committer_len);
1146 (*commit)->committer[icommit->committer_len] = '\0';
1148 len += icommit->committer_len;
1150 if (icommit->logmsg_len == 0) {
1151 (*commit)->logmsg = strdup("");
1152 if ((*commit)->logmsg == NULL) {
1153 err = got_error_from_errno("strdup");
1157 size_t offset = 0, remain = icommit->logmsg_len;
1159 (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
1160 if ((*commit)->logmsg == NULL) {
1161 err = got_error_from_errno("malloc");
1164 while (remain > 0) {
1165 struct imsg imsg_log;
1166 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1169 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1173 if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG) {
1174 err = got_error(GOT_ERR_PRIVSEP_MSG);
1178 memcpy((*commit)->logmsg + offset,
1180 imsg_free(&imsg_log);
1184 (*commit)->logmsg[icommit->logmsg_len] = '\0';
1187 for (i = 0; i < icommit->nparents; i++) {
1188 struct got_object_qid *qid;
1190 err = got_object_qid_alloc_partial(&qid);
1193 memcpy(qid->id, imsg->data + len +
1194 i * SHA1_DIGEST_LENGTH, sizeof(*qid->id));
1195 STAILQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
1196 (*commit)->nparents++;
1200 got_object_commit_close(*commit);
1206 const struct got_error *
1207 got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
1209 const struct got_error *err = NULL;
1212 const size_t min_datalen =
1213 MIN(sizeof(struct got_imsg_error),
1214 sizeof(struct got_imsg_commit_object));
1218 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1222 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1224 switch (imsg.hdr.type) {
1225 case GOT_IMSG_COMMIT:
1226 err = get_commit_from_imsg(commit, &imsg, datalen, ibuf);
1229 err = got_error(GOT_ERR_PRIVSEP_MSG);
1238 const struct got_error *
1239 got_privsep_send_tree(struct imsgbuf *ibuf, struct got_pathlist_head *entries,
1242 const struct got_error *err = NULL;
1243 struct got_imsg_tree_object itree;
1244 struct got_pathlist_entry *pe;
1246 int nimsg; /* number of imsg queued in ibuf */
1248 itree.nentries = nentries;
1249 if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
1251 return got_error_from_errno("imsg_compose TREE");
1253 totlen = sizeof(itree);
1255 TAILQ_FOREACH(pe, entries, entry) {
1256 const char *name = pe->path;
1257 struct got_parsed_tree_entry *pte = pe->data;
1259 size_t namelen = strlen(name);
1260 size_t len = sizeof(struct got_imsg_tree_entry) + namelen;
1262 if (len > MAX_IMSGSIZE)
1263 return got_error(GOT_ERR_NO_SPACE);
1266 if (totlen + len >= MAX_IMSGSIZE - (IMSG_HEADER_SIZE * nimsg)) {
1267 err = flush_imsg(ibuf);
1273 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_ENTRY, 0, 0, len);
1275 return got_error_from_errno("imsg_create TREE_ENTRY");
1277 /* Keep in sync with struct got_imsg_tree_object definition! */
1278 if (imsg_add(wbuf, pte->id, SHA1_DIGEST_LENGTH) == -1) {
1279 err = got_error_from_errno("imsg_add TREE_ENTRY");
1283 if (imsg_add(wbuf, &pte->mode, sizeof(pte->mode)) == -1) {
1284 err = got_error_from_errno("imsg_add TREE_ENTRY");
1289 if (imsg_add(wbuf, name, namelen) == -1) {
1290 err = got_error_from_errno("imsg_add TREE_ENTRY");
1296 imsg_close(ibuf, wbuf);
1301 return flush_imsg(ibuf);
1304 const struct got_error *
1305 got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
1307 const struct got_error *err = NULL;
1308 const size_t min_datalen =
1309 MIN(sizeof(struct got_imsg_error),
1310 sizeof(struct got_imsg_tree_object));
1311 struct got_imsg_tree_object *itree;
1316 err = read_imsg(ibuf);
1324 struct got_imsg_tree_entry *ite;
1325 struct got_tree_entry *te = NULL;
1327 n = imsg_get(ibuf, &imsg);
1329 if (*tree && (*tree)->nentries != nentries)
1334 if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen) {
1336 err = got_error(GOT_ERR_PRIVSEP_LEN);
1340 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1342 switch (imsg.hdr.type) {
1343 case GOT_IMSG_ERROR:
1344 err = recv_imsg_error(&imsg, datalen);
1347 /* This message should only appear once. */
1348 if (*tree != NULL) {
1349 err = got_error(GOT_ERR_PRIVSEP_MSG);
1352 if (datalen != sizeof(*itree)) {
1353 err = got_error(GOT_ERR_PRIVSEP_LEN);
1357 *tree = malloc(sizeof(**tree));
1358 if (*tree == NULL) {
1359 err = got_error_from_errno("malloc");
1362 (*tree)->entries = calloc(itree->nentries,
1363 sizeof(struct got_tree_entry));
1364 if ((*tree)->entries == NULL) {
1365 err = got_error_from_errno("malloc");
1368 (*tree)->nentries = itree->nentries;
1369 (*tree)->refcnt = 0;
1371 case GOT_IMSG_TREE_ENTRY:
1372 /* This message should be preceeded by GOT_IMSG_TREE. */
1373 if (*tree == NULL) {
1374 err = got_error(GOT_ERR_PRIVSEP_MSG);
1377 if (datalen < sizeof(*ite) || datalen > MAX_IMSGSIZE) {
1378 err = got_error(GOT_ERR_PRIVSEP_LEN);
1382 /* Remaining data contains the entry's name. */
1383 datalen -= sizeof(*ite);
1384 if (datalen == 0 || datalen > MAX_IMSGSIZE) {
1385 err = got_error(GOT_ERR_PRIVSEP_LEN);
1390 if (datalen + 1 > sizeof(te->name)) {
1391 err = got_error(GOT_ERR_NO_SPACE);
1394 te = &(*tree)->entries[nentries];
1395 memcpy(te->name, imsg.data + sizeof(*ite), datalen);
1396 te->name[datalen] = '\0';
1398 memcpy(te->id.sha1, ite->id, SHA1_DIGEST_LENGTH);
1399 te->mode = ite->mode;
1404 err = got_error(GOT_ERR_PRIVSEP_MSG);
1413 if (*tree && (*tree)->nentries != nentries) {
1415 err = got_error(GOT_ERR_PRIVSEP_LEN);
1416 got_object_tree_close(*tree);
1423 const struct got_error *
1424 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
1425 const uint8_t *data)
1427 struct got_imsg_blob iblob;
1430 iblob.hdrlen = hdrlen;
1435 if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
1436 return got_error(GOT_ERR_NO_SPACE);
1438 buf = malloc(sizeof(iblob) + size);
1440 return got_error_from_errno("malloc");
1442 memcpy(buf, &iblob, sizeof(iblob));
1443 memcpy(buf + sizeof(iblob), data, size);
1444 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
1445 sizeof(iblob) + size) == -1) {
1447 return got_error_from_errno("imsg_compose BLOB");
1451 /* Data has already been written to file descriptor. */
1452 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
1453 sizeof(iblob)) == -1)
1454 return got_error_from_errno("imsg_compose BLOB");
1458 return flush_imsg(ibuf);
1461 const struct got_error *
1462 got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1463 struct imsgbuf *ibuf)
1465 const struct got_error *err = NULL;
1467 struct got_imsg_blob *iblob;
1472 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1476 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1478 switch (imsg.hdr.type) {
1480 if (datalen < sizeof(*iblob)) {
1481 err = got_error(GOT_ERR_PRIVSEP_LEN);
1485 *size = iblob->size;
1486 *hdrlen = iblob->hdrlen;
1488 if (datalen == sizeof(*iblob)) {
1489 /* Data has been written to file descriptor. */
1493 if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX) {
1494 err = got_error(GOT_ERR_PRIVSEP_LEN);
1498 *outbuf = malloc(*size);
1499 if (*outbuf == NULL) {
1500 err = got_error_from_errno("malloc");
1503 memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
1506 err = got_error(GOT_ERR_PRIVSEP_MSG);
1515 static const struct got_error *
1516 send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
1518 const struct got_error *err = NULL;
1519 size_t offset, remain;
1522 remain = tagmsg_len;
1523 while (remain > 0) {
1524 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1526 if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
1527 tag->tagmsg + offset, n) == -1) {
1528 err = got_error_from_errno("imsg_compose TAG_TAGMSG");
1532 err = flush_imsg(ibuf);
1543 const struct got_error *
1544 got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
1546 const struct got_error *err = NULL;
1547 struct got_imsg_tag_object *itag;
1550 size_t tag_len = strlen(tag->tag);
1551 size_t tagger_len = strlen(tag->tagger);
1552 size_t tagmsg_len = strlen(tag->tagmsg);
1554 total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
1556 buf = malloc(total);
1558 return got_error_from_errno("malloc");
1560 itag = (struct got_imsg_tag_object *)buf;
1561 memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
1562 itag->obj_type = tag->obj_type;
1563 itag->tag_len = tag_len;
1564 itag->tagger_len = tagger_len;
1565 itag->tagger_time = tag->tagger_time;
1566 itag->tagger_gmtoff = tag->tagger_gmtoff;
1567 itag->tagmsg_len = tagmsg_len;
1569 len = sizeof(*itag);
1570 memcpy(buf + len, tag->tag, tag_len);
1572 memcpy(buf + len, tag->tagger, tagger_len);
1575 if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
1576 err = got_error_from_errno("imsg_compose TAG");
1580 if (tagmsg_len == 0 ||
1581 tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1582 err = flush_imsg(ibuf);
1586 err = send_tagmsg(ibuf, tag, tagmsg_len);
1592 const struct got_error *
1593 got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
1595 const struct got_error *err = NULL;
1597 struct got_imsg_tag_object *itag;
1598 size_t len, datalen;
1599 const size_t min_datalen =
1600 MIN(sizeof(struct got_imsg_error),
1601 sizeof(struct got_imsg_tag_object));
1605 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1609 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1612 switch (imsg.hdr.type) {
1614 if (datalen < sizeof(*itag)) {
1615 err = got_error(GOT_ERR_PRIVSEP_LEN);
1619 if (datalen != sizeof(*itag) + itag->tag_len +
1621 err = got_error(GOT_ERR_PRIVSEP_LEN);
1624 len += sizeof(*itag);
1626 *tag = calloc(1, sizeof(**tag));
1628 err = got_error_from_errno("calloc");
1632 memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
1634 if (itag->tag_len == 0) {
1635 (*tag)->tag = strdup("");
1636 if ((*tag)->tag == NULL) {
1637 err = got_error_from_errno("strdup");
1641 (*tag)->tag = malloc(itag->tag_len + 1);
1642 if ((*tag)->tag == NULL) {
1643 err = got_error_from_errno("malloc");
1646 memcpy((*tag)->tag, imsg.data + len,
1648 (*tag)->tag[itag->tag_len] = '\0';
1650 len += itag->tag_len;
1652 (*tag)->obj_type = itag->obj_type;
1653 (*tag)->tagger_time = itag->tagger_time;
1654 (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1656 if (itag->tagger_len == 0) {
1657 (*tag)->tagger = strdup("");
1658 if ((*tag)->tagger == NULL) {
1659 err = got_error_from_errno("strdup");
1663 (*tag)->tagger = malloc(itag->tagger_len + 1);
1664 if ((*tag)->tagger == NULL) {
1665 err = got_error_from_errno("malloc");
1668 memcpy((*tag)->tagger, imsg.data + len,
1670 (*tag)->tagger[itag->tagger_len] = '\0';
1672 len += itag->tagger_len;
1674 if (itag->tagmsg_len == 0) {
1675 (*tag)->tagmsg = strdup("");
1676 if ((*tag)->tagmsg == NULL) {
1677 err = got_error_from_errno("strdup");
1681 size_t offset = 0, remain = itag->tagmsg_len;
1683 (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1684 if ((*tag)->tagmsg == NULL) {
1685 err = got_error_from_errno("malloc");
1688 while (remain > 0) {
1689 struct imsg imsg_log;
1690 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1693 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1697 if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1698 return got_error(GOT_ERR_PRIVSEP_MSG);
1700 memcpy((*tag)->tagmsg + offset, imsg_log.data,
1702 imsg_free(&imsg_log);
1706 (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1711 err = got_error(GOT_ERR_PRIVSEP_MSG);
1720 const struct got_error *
1721 got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1722 struct got_packidx *packidx)
1724 const struct got_error *err = NULL;
1725 struct got_imsg_packidx ipackidx;
1726 struct got_imsg_pack ipack;
1729 ipackidx.len = packidx->len;
1730 fd = dup(packidx->fd);
1732 return got_error_from_errno("dup");
1734 if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
1735 sizeof(ipackidx)) == -1) {
1736 err = got_error_from_errno("imsg_compose PACKIDX");
1741 if (strlcpy(ipack.path_packfile, pack->path_packfile,
1742 sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
1743 return got_error(GOT_ERR_NO_SPACE);
1744 ipack.filesize = pack->filesize;
1748 return got_error_from_errno("dup");
1750 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
1752 err = got_error_from_errno("imsg_compose PACK");
1757 return flush_imsg(ibuf);
1760 const struct got_error *
1761 got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
1762 struct got_object_id *id)
1764 struct got_imsg_packed_object iobj;
1767 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
1769 if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
1770 &iobj, sizeof(iobj)) == -1)
1771 return got_error_from_errno("imsg_compose "
1772 "PACKED_OBJECT_REQUEST");
1774 return flush_imsg(ibuf);
1777 const struct got_error *
1778 got_privsep_send_packed_raw_obj_req(struct imsgbuf *ibuf, int idx,
1779 struct got_object_id *id)
1781 struct got_imsg_packed_object iobj;
1784 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
1786 if (imsg_compose(ibuf, GOT_IMSG_PACKED_RAW_OBJECT_REQUEST, 0, 0, -1,
1787 &iobj, sizeof(iobj)) == -1)
1788 return got_error_from_errno("imsg_compose "
1789 "PACKED_OBJECT_REQUEST");
1791 return flush_imsg(ibuf);
1794 const struct got_error *
1795 got_privsep_send_gitconfig_parse_req(struct imsgbuf *ibuf, int fd)
1797 const struct got_error *err = NULL;
1799 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_PARSE_REQUEST, 0, 0, fd,
1801 err = got_error_from_errno("imsg_compose "
1802 "GITCONFIG_PARSE_REQUEST");
1807 return flush_imsg(ibuf);
1810 const struct got_error *
1811 got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *ibuf)
1813 if (imsg_compose(ibuf,
1814 GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST, 0, 0, -1,
1816 return got_error_from_errno("imsg_compose "
1817 "GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST");
1819 return flush_imsg(ibuf);
1822 const struct got_error *
1823 got_privsep_send_gitconfig_repository_extensions_req(struct imsgbuf *ibuf)
1825 if (imsg_compose(ibuf,
1826 GOT_IMSG_GITCONFIG_REPOSITORY_EXTENSIONS_REQUEST, 0, 0, -1,
1828 return got_error_from_errno("imsg_compose "
1829 "GITCONFIG_REPOSITORY_EXTENSIONS_REQUEST");
1831 return flush_imsg(ibuf);
1835 const struct got_error *
1836 got_privsep_send_gitconfig_author_name_req(struct imsgbuf *ibuf)
1838 if (imsg_compose(ibuf,
1839 GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST, 0, 0, -1, NULL, 0) == -1)
1840 return got_error_from_errno("imsg_compose "
1841 "GITCONFIG_AUTHOR_NAME_REQUEST");
1843 return flush_imsg(ibuf);
1846 const struct got_error *
1847 got_privsep_send_gitconfig_author_email_req(struct imsgbuf *ibuf)
1849 if (imsg_compose(ibuf,
1850 GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST, 0, 0, -1, NULL, 0) == -1)
1851 return got_error_from_errno("imsg_compose "
1852 "GITCONFIG_AUTHOR_EMAIL_REQUEST");
1854 return flush_imsg(ibuf);
1857 const struct got_error *
1858 got_privsep_send_gitconfig_remotes_req(struct imsgbuf *ibuf)
1860 if (imsg_compose(ibuf,
1861 GOT_IMSG_GITCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
1862 return got_error_from_errno("imsg_compose "
1863 "GITCONFIG_REMOTE_REQUEST");
1865 return flush_imsg(ibuf);
1868 const struct got_error *
1869 got_privsep_send_gitconfig_owner_req(struct imsgbuf *ibuf)
1871 if (imsg_compose(ibuf,
1872 GOT_IMSG_GITCONFIG_OWNER_REQUEST, 0, 0, -1, NULL, 0) == -1)
1873 return got_error_from_errno("imsg_compose "
1874 "GITCONFIG_OWNER_REQUEST");
1876 return flush_imsg(ibuf);
1879 const struct got_error *
1880 got_privsep_recv_gitconfig_str(char **str, struct imsgbuf *ibuf)
1882 const struct got_error *err = NULL;
1885 const size_t min_datalen = 0;
1889 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1892 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1894 switch (imsg.hdr.type) {
1895 case GOT_IMSG_GITCONFIG_STR_VAL:
1898 /* datalen does not include terminating \0 */
1899 *str = malloc(datalen + 1);
1901 err = got_error_from_errno("malloc");
1904 memcpy(*str, imsg.data, datalen);
1905 (*str)[datalen] = '\0';
1908 err = got_error(GOT_ERR_PRIVSEP_MSG);
1916 const struct got_error *
1917 got_privsep_recv_gitconfig_int(int *val, struct imsgbuf *ibuf)
1919 const struct got_error *err = NULL;
1922 const size_t min_datalen =
1923 MIN(sizeof(struct got_imsg_error), sizeof(int));
1927 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1930 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1932 switch (imsg.hdr.type) {
1933 case GOT_IMSG_GITCONFIG_INT_VAL:
1934 if (datalen != sizeof(*val)) {
1935 err = got_error(GOT_ERR_PRIVSEP_LEN);
1938 memcpy(val, imsg.data, sizeof(*val));
1941 err = got_error(GOT_ERR_PRIVSEP_MSG);
1950 free_remote_data(struct got_remote_repo *remote)
1956 for (i = 0; i < remote->nbranches; i++)
1957 free(remote->branches[i]);
1958 free(remote->branches);
1959 for (i = 0; i < remote->nrefs; i++)
1960 free(remote->refs[i]);
1964 const struct got_error *
1965 got_privsep_recv_gitconfig_remotes(struct got_remote_repo **remotes,
1966 int *nremotes, struct imsgbuf *ibuf)
1968 const struct got_error *err = NULL;
1971 struct got_imsg_remotes iremotes;
1972 struct got_imsg_remote iremote;
1976 iremotes.nremotes = 0;
1978 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremotes));
1981 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1983 switch (imsg.hdr.type) {
1984 case GOT_IMSG_GITCONFIG_REMOTES:
1985 if (datalen != sizeof(iremotes)) {
1986 err = got_error(GOT_ERR_PRIVSEP_LEN);
1989 memcpy(&iremotes, imsg.data, sizeof(iremotes));
1990 if (iremotes.nremotes == 0) {
1997 return got_error(GOT_ERR_PRIVSEP_MSG);
2002 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(**remotes));
2003 if (*remotes == NULL)
2004 return got_error_from_errno("recallocarray");
2006 while (*nremotes < iremotes.nremotes) {
2007 struct got_remote_repo *remote;
2009 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremote));
2012 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2014 switch (imsg.hdr.type) {
2015 case GOT_IMSG_GITCONFIG_REMOTE:
2016 remote = &(*remotes)[*nremotes];
2017 memset(remote, 0, sizeof(*remote));
2018 if (datalen < sizeof(iremote)) {
2019 err = got_error(GOT_ERR_PRIVSEP_LEN);
2022 memcpy(&iremote, imsg.data, sizeof(iremote));
2023 if (iremote.name_len == 0 || iremote.url_len == 0 ||
2024 (sizeof(iremote) + iremote.name_len +
2025 iremote.url_len) > datalen) {
2026 err = got_error(GOT_ERR_PRIVSEP_LEN);
2029 remote->name = strndup(imsg.data + sizeof(iremote),
2031 if (remote->name == NULL) {
2032 err = got_error_from_errno("strndup");
2035 remote->url = strndup(imsg.data + sizeof(iremote) +
2036 iremote.name_len, iremote.url_len);
2037 if (remote->url == NULL) {
2038 err = got_error_from_errno("strndup");
2039 free_remote_data(remote);
2042 remote->mirror_references = iremote.mirror_references;
2043 remote->fetch_all_branches = iremote.fetch_all_branches;
2044 remote->nbranches = 0;
2045 remote->branches = NULL;
2047 remote->refs = NULL;
2051 err = got_error(GOT_ERR_PRIVSEP_MSG);
2062 for (i = 0; i < *nremotes; i++)
2063 free_remote_data(&(*remotes)[i]);
2071 const struct got_error *
2072 got_privsep_send_gotconfig_parse_req(struct imsgbuf *ibuf, int fd)
2074 const struct got_error *err = NULL;
2076 if (imsg_compose(ibuf, GOT_IMSG_GOTCONFIG_PARSE_REQUEST, 0, 0, fd,
2078 err = got_error_from_errno("imsg_compose "
2079 "GOTCONFIG_PARSE_REQUEST");
2084 return flush_imsg(ibuf);
2087 const struct got_error *
2088 got_privsep_send_gotconfig_author_req(struct imsgbuf *ibuf)
2090 if (imsg_compose(ibuf,
2091 GOT_IMSG_GOTCONFIG_AUTHOR_REQUEST, 0, 0, -1, NULL, 0) == -1)
2092 return got_error_from_errno("imsg_compose "
2093 "GOTCONFIG_AUTHOR_REQUEST");
2095 return flush_imsg(ibuf);
2098 const struct got_error *
2099 got_privsep_send_gotconfig_remotes_req(struct imsgbuf *ibuf)
2101 if (imsg_compose(ibuf,
2102 GOT_IMSG_GOTCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
2103 return got_error_from_errno("imsg_compose "
2104 "GOTCONFIG_REMOTE_REQUEST");
2106 return flush_imsg(ibuf);
2109 const struct got_error *
2110 got_privsep_recv_gotconfig_str(char **str, struct imsgbuf *ibuf)
2112 const struct got_error *err = NULL;
2115 const size_t min_datalen = 0;
2119 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2122 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2124 switch (imsg.hdr.type) {
2125 case GOT_IMSG_ERROR:
2126 if (datalen < sizeof(struct got_imsg_error)) {
2127 err = got_error(GOT_ERR_PRIVSEP_LEN);
2130 err = recv_imsg_error(&imsg, datalen);
2132 case GOT_IMSG_GOTCONFIG_STR_VAL:
2135 /* datalen does not include terminating \0 */
2136 *str = malloc(datalen + 1);
2138 err = got_error_from_errno("malloc");
2141 memcpy(*str, imsg.data, datalen);
2142 (*str)[datalen] = '\0';
2145 err = got_error(GOT_ERR_PRIVSEP_MSG);
2154 const struct got_error *
2155 got_privsep_recv_gotconfig_remotes(struct got_remote_repo **remotes,
2156 int *nremotes, struct imsgbuf *ibuf)
2158 const struct got_error *err = NULL;
2161 struct got_imsg_remotes iremotes;
2162 struct got_imsg_remote iremote;
2163 const size_t min_datalen =
2164 MIN(sizeof(struct got_imsg_error), sizeof(iremotes));
2168 iremotes.nremotes = 0;
2170 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2173 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2175 switch (imsg.hdr.type) {
2176 case GOT_IMSG_ERROR:
2177 if (datalen < sizeof(struct got_imsg_error)) {
2178 err = got_error(GOT_ERR_PRIVSEP_LEN);
2181 err = recv_imsg_error(&imsg, datalen);
2183 case GOT_IMSG_GOTCONFIG_REMOTES:
2184 if (datalen != sizeof(iremotes)) {
2185 err = got_error(GOT_ERR_PRIVSEP_LEN);
2188 memcpy(&iremotes, imsg.data, sizeof(iremotes));
2189 if (iremotes.nremotes == 0) {
2196 return got_error(GOT_ERR_PRIVSEP_MSG);
2201 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(**remotes));
2202 if (*remotes == NULL)
2203 return got_error_from_errno("recallocarray");
2205 while (*nremotes < iremotes.nremotes) {
2206 struct got_remote_repo *remote;
2207 const size_t min_datalen =
2208 MIN(sizeof(struct got_imsg_error), sizeof(iremote));
2211 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2214 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2216 switch (imsg.hdr.type) {
2217 case GOT_IMSG_ERROR:
2218 if (datalen < sizeof(struct got_imsg_error)) {
2219 err = got_error(GOT_ERR_PRIVSEP_LEN);
2222 err = recv_imsg_error(&imsg, datalen);
2224 case GOT_IMSG_GOTCONFIG_REMOTE:
2225 remote = &(*remotes)[*nremotes];
2226 memset(remote, 0, sizeof(*remote));
2227 if (datalen < sizeof(iremote)) {
2228 err = got_error(GOT_ERR_PRIVSEP_LEN);
2231 memcpy(&iremote, imsg.data, sizeof(iremote));
2232 if (iremote.name_len == 0 || iremote.url_len == 0 ||
2233 (sizeof(iremote) + iremote.name_len +
2234 iremote.url_len) > datalen) {
2235 err = got_error(GOT_ERR_PRIVSEP_LEN);
2238 remote->name = strndup(imsg.data + sizeof(iremote),
2240 if (remote->name == NULL) {
2241 err = got_error_from_errno("strndup");
2244 remote->url = strndup(imsg.data + sizeof(iremote) +
2245 iremote.name_len, iremote.url_len);
2246 if (remote->url == NULL) {
2247 err = got_error_from_errno("strndup");
2248 free_remote_data(remote);
2251 remote->mirror_references = iremote.mirror_references;
2252 remote->fetch_all_branches = iremote.fetch_all_branches;
2253 if (iremote.nbranches > 0) {
2254 remote->branches = recallocarray(NULL, 0,
2255 iremote.nbranches, sizeof(char *));
2256 if (remote->branches == NULL) {
2257 err = got_error_from_errno("calloc");
2258 free_remote_data(remote);
2262 remote->nbranches = 0;
2263 for (i = 0; i < iremote.nbranches; i++) {
2265 err = got_privsep_recv_gotconfig_str(&branch,
2268 free_remote_data(remote);
2271 remote->branches[i] = branch;
2272 remote->nbranches++;
2274 if (iremote.nrefs > 0) {
2275 remote->refs = recallocarray(NULL, 0,
2276 iremote.nrefs, sizeof(char *));
2277 if (remote->refs == NULL) {
2278 err = got_error_from_errno("calloc");
2279 free_remote_data(remote);
2284 for (i = 0; i < iremote.nrefs; i++) {
2286 err = got_privsep_recv_gotconfig_str(&ref,
2289 free_remote_data(remote);
2292 remote->refs[i] = ref;
2298 err = got_error(GOT_ERR_PRIVSEP_MSG);
2309 for (i = 0; i < *nremotes; i++)
2310 free_remote_data(&(*remotes)[i]);
2318 const struct got_error *
2319 got_privsep_send_commit_traversal_request(struct imsgbuf *ibuf,
2320 struct got_object_id *id, int idx, const char *path)
2322 const struct got_error *err = NULL;
2324 size_t path_len = strlen(path) + 1;
2326 wbuf = imsg_create(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_REQUEST, 0, 0,
2327 sizeof(struct got_imsg_commit_traversal_request) + path_len);
2329 return got_error_from_errno(
2330 "imsg_create COMMIT_TRAVERSAL_REQUEST");
2331 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
2332 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
2336 if (imsg_add(wbuf, &idx, sizeof(idx)) == -1) {
2337 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
2341 if (imsg_add(wbuf, path, path_len) == -1) {
2342 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
2348 imsg_close(ibuf, wbuf);
2350 return flush_imsg(ibuf);
2353 const struct got_error *
2354 got_privsep_recv_traversed_commits(struct got_commit_object **changed_commit,
2355 struct got_object_id **changed_commit_id,
2356 struct got_object_id_queue *commit_ids, struct imsgbuf *ibuf)
2358 const struct got_error *err = NULL;
2360 struct got_imsg_traversed_commits *icommits;
2364 *changed_commit = NULL;
2365 *changed_commit_id = NULL;
2368 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2372 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2373 switch (imsg.hdr.type) {
2374 case GOT_IMSG_TRAVERSED_COMMITS:
2375 icommits = imsg.data;
2376 if (datalen != sizeof(*icommits) +
2377 icommits->ncommits * SHA1_DIGEST_LENGTH) {
2378 err = got_error(GOT_ERR_PRIVSEP_LEN);
2381 for (i = 0; i < icommits->ncommits; i++) {
2382 struct got_object_qid *qid;
2383 uint8_t *sha1 = (uint8_t *)imsg.data +
2384 sizeof(*icommits) + i * SHA1_DIGEST_LENGTH;
2385 err = got_object_qid_alloc_partial(&qid);
2388 memcpy(qid->id->sha1, sha1, SHA1_DIGEST_LENGTH);
2389 STAILQ_INSERT_TAIL(commit_ids, qid, entry);
2391 /* The last commit may contain a change. */
2392 if (i == icommits->ncommits - 1) {
2393 *changed_commit_id =
2394 got_object_id_dup(qid->id);
2395 if (*changed_commit_id == NULL) {
2396 err = got_error_from_errno(
2397 "got_object_id_dup");
2403 case GOT_IMSG_COMMIT:
2404 if (*changed_commit_id == NULL) {
2405 err = got_error(GOT_ERR_PRIVSEP_MSG);
2408 err = get_commit_from_imsg(changed_commit, &imsg,
2411 case GOT_IMSG_COMMIT_TRAVERSAL_DONE:
2415 err = got_error(GOT_ERR_PRIVSEP_MSG);
2425 got_object_id_queue_free(commit_ids);
2429 const struct got_error *
2430 got_privsep_unveil_exec_helpers(void)
2432 const char *helpers[] = {
2433 GOT_PATH_PROG_READ_PACK,
2434 GOT_PATH_PROG_READ_OBJECT,
2435 GOT_PATH_PROG_READ_COMMIT,
2436 GOT_PATH_PROG_READ_TREE,
2437 GOT_PATH_PROG_READ_BLOB,
2438 GOT_PATH_PROG_READ_TAG,
2439 GOT_PATH_PROG_READ_GITCONFIG,
2440 GOT_PATH_PROG_READ_GOTCONFIG,
2441 GOT_PATH_PROG_FETCH_PACK,
2442 GOT_PATH_PROG_INDEX_PACK,
2446 for (i = 0; i < nitems(helpers); i++) {
2447 if (unveil(helpers[i], "x") == 0)
2449 return got_error_from_errno2("unveil", helpers[i]);
2456 got_privsep_exec_child(int imsg_fds[2], const char *path, const char *repo_path)
2458 if (close(imsg_fds[0]) == -1) {
2459 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2463 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
2464 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2467 if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
2468 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2472 if (execl(path, path, repo_path, (char *)NULL) == -1) {
2473 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,