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>
21 #include <sys/syslimits.h>
36 #include "got_object.h"
37 #include "got_error.h"
39 #include "got_repository.h"
41 #include "got_lib_sha1.h"
42 #include "got_lib_delta.h"
43 #include "got_lib_inflate.h"
44 #include "got_lib_object.h"
45 #include "got_lib_object_parse.h"
46 #include "got_lib_privsep.h"
47 #include "got_lib_pack.h"
50 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
54 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
57 static const struct got_error *
58 poll_fd(int fd, int events, int timeout)
64 pfd[0].events = events;
66 n = poll(pfd, 1, timeout);
68 return got_error_from_errno("poll");
70 return got_error(GOT_ERR_TIMEOUT);
71 if (pfd[0].revents & (POLLERR | POLLNVAL))
72 return got_error_from_errno("poll error");
73 if (pfd[0].revents & (events | POLLHUP))
76 return got_error(GOT_ERR_INTERRUPT);
79 static const struct got_error *
80 read_imsg(struct imsgbuf *ibuf)
82 const struct got_error *err;
85 err = poll_fd(ibuf->fd, POLLIN, INFTIM);
91 if (errno == EAGAIN) /* Could be a file-descriptor leak. */
92 return got_error(GOT_ERR_PRIVSEP_NO_FD);
93 return got_error(GOT_ERR_PRIVSEP_READ);
96 return got_error(GOT_ERR_PRIVSEP_PIPE);
101 const struct got_error *
102 got_privsep_wait_for_child(pid_t pid)
106 if (waitpid(pid, &child_status, 0) == -1)
107 return got_error_from_errno("waitpid");
109 if (!WIFEXITED(child_status))
110 return got_error(GOT_ERR_PRIVSEP_DIED);
112 if (WEXITSTATUS(child_status) != 0)
113 return got_error(GOT_ERR_PRIVSEP_EXIT);
118 static const struct got_error *
119 recv_imsg_error(struct imsg *imsg, size_t datalen)
121 struct got_imsg_error *ierr;
123 if (datalen != sizeof(*ierr))
124 return got_error(GOT_ERR_PRIVSEP_LEN);
127 if (ierr->code == GOT_ERR_ERRNO) {
128 static struct got_error serr;
129 serr.code = GOT_ERR_ERRNO;
130 serr.msg = strerror(ierr->errno_code);
134 return got_error(ierr->code);
137 const struct got_error *
138 got_privsep_recv_imsg(struct imsg *imsg, struct imsgbuf *ibuf,
141 const struct got_error *err;
144 n = imsg_get(ibuf, imsg);
146 return got_error_from_errno("imsg_get");
149 err = read_imsg(ibuf);
152 n = imsg_get(ibuf, imsg);
154 return got_error_from_errno("imsg_get");
157 if (imsg->hdr.len < IMSG_HEADER_SIZE + min_datalen)
158 return got_error(GOT_ERR_PRIVSEP_LEN);
160 if (imsg->hdr.type == GOT_IMSG_ERROR) {
161 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
162 return recv_imsg_error(imsg, datalen);
168 /* Attempt to send an error in an imsg. Complain on stderr as a last resort. */
170 got_privsep_send_error(struct imsgbuf *ibuf, const struct got_error *err)
172 const struct got_error *poll_err;
173 struct got_imsg_error ierr;
176 ierr.code = err->code;
177 if (err->code == GOT_ERR_ERRNO)
178 ierr.errno_code = errno;
181 ret = imsg_compose(ibuf, GOT_IMSG_ERROR, 0, 0, -1, &ierr, sizeof(ierr));
183 fprintf(stderr, "%s: error %d \"%s\": imsg_compose: %s\n",
184 getprogname(), err->code, err->msg, strerror(errno));
188 poll_err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
190 fprintf(stderr, "%s: error %d \"%s\": poll: %s\n",
191 getprogname(), err->code, err->msg, poll_err->msg);
195 ret = imsg_flush(ibuf);
197 fprintf(stderr, "%s: error %d \"%s\": imsg_flush: %s\n",
198 getprogname(), err->code, err->msg, strerror(errno));
203 static const struct got_error *
204 flush_imsg(struct imsgbuf *ibuf)
206 const struct got_error *err;
208 err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
212 if (imsg_flush(ibuf) == -1)
213 return got_error_from_errno("imsg_flush");
218 const struct got_error *
219 got_privsep_send_stop(int fd)
221 const struct got_error *err = NULL;
224 imsg_init(&ibuf, fd);
226 if (imsg_compose(&ibuf, GOT_IMSG_STOP, 0, 0, -1, NULL, 0) == -1)
227 return got_error_from_errno("imsg_compose STOP");
229 err = flush_imsg(&ibuf);
234 const struct got_error *
235 got_privsep_send_obj_req(struct imsgbuf *ibuf, int fd)
237 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_REQUEST, 0, 0, fd, NULL, 0)
239 return got_error_from_errno("imsg_compose OBJECT_REQUEST");
241 return flush_imsg(ibuf);
244 const struct got_error *
245 got_privsep_send_commit_req(struct imsgbuf *ibuf, int fd,
246 struct got_object_id *id, int pack_idx)
248 const struct got_error *err = NULL;
249 struct got_imsg_packed_object iobj, *iobjp;
252 if (id) { /* commit is packed */
254 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
262 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_REQUEST, 0, 0, fd, iobjp, len)
264 err = got_error_from_errno("imsg_compose COMMIT_REQUEST");
269 return flush_imsg(ibuf);
272 const struct got_error *
273 got_privsep_send_tree_req(struct imsgbuf *ibuf, int fd,
274 struct got_object_id *id, int pack_idx)
276 const struct got_error *err = NULL;
278 size_t len = id ? sizeof(struct got_imsg_packed_object) : 0;
280 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_REQUEST, 0, 0, len);
282 return got_error_from_errno("imsg_create TREE_REQUEST");
284 if (id) { /* tree is packed */
285 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
286 err = got_error_from_errno("imsg_add TREE_ENTRY");
291 if (imsg_add(wbuf, &pack_idx, sizeof(pack_idx)) == -1) {
292 err = got_error_from_errno("imsg_add TREE_ENTRY");
299 imsg_close(ibuf, wbuf);
301 return flush_imsg(ibuf);
304 const struct got_error *
305 got_privsep_send_tag_req(struct imsgbuf *ibuf, int fd,
306 struct got_object_id *id, int pack_idx)
308 struct got_imsg_packed_object iobj, *iobjp;
311 if (id) { /* tag is packed */
313 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
321 if (imsg_compose(ibuf, GOT_IMSG_TAG_REQUEST, 0, 0, fd, iobjp, len)
323 return got_error_from_errno("imsg_compose TAG_REQUEST");
325 return flush_imsg(ibuf);
328 const struct got_error *
329 got_privsep_send_blob_req(struct imsgbuf *ibuf, int infd,
330 struct got_object_id *id, int pack_idx)
332 const struct got_error *err = NULL;
333 struct got_imsg_packed_object iobj, *iobjp;
336 if (id) { /* blob is packed */
338 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
346 if (imsg_compose(ibuf, GOT_IMSG_BLOB_REQUEST, 0, 0, infd, iobjp, len)
348 err = got_error_from_errno("imsg_compose BLOB_REQUEST");
353 return flush_imsg(ibuf);
356 const struct got_error *
357 got_privsep_send_blob_outfd(struct imsgbuf *ibuf, int outfd)
359 const struct got_error *err = NULL;
361 if (imsg_compose(ibuf, GOT_IMSG_BLOB_OUTFD, 0, 0, outfd, NULL, 0)
363 err = got_error_from_errno("imsg_compose BLOB_OUTFD");
368 return flush_imsg(ibuf);
371 static const struct got_error *
372 send_fd(struct imsgbuf *ibuf, int imsg_code, int fd)
374 const struct got_error *err = NULL;
376 if (imsg_compose(ibuf, imsg_code, 0, 0, fd, NULL, 0) == -1) {
377 err = got_error_from_errno("imsg_compose TMPFD");
382 return flush_imsg(ibuf);
385 const struct got_error *
386 got_privsep_send_tmpfd(struct imsgbuf *ibuf, int fd)
388 return send_fd(ibuf, GOT_IMSG_TMPFD, fd);
391 const struct got_error *
392 got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj)
394 struct got_imsg_object iobj;
396 memcpy(iobj.id, obj->id.sha1, sizeof(iobj.id));
397 iobj.type = obj->type;
398 iobj.flags = obj->flags;
399 iobj.hdrlen = obj->hdrlen;
400 iobj.size = obj->size;
401 if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
402 iobj.pack_offset = obj->pack_offset;
403 iobj.pack_idx = obj->pack_idx;
406 if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
408 return got_error_from_errno("imsg_compose OBJECT");
410 return flush_imsg(ibuf);
413 const struct got_error *
414 got_privsep_send_fetch_req(struct imsgbuf *ibuf, int fd,
415 struct got_pathlist_head *have_refs, int fetch_all_branches)
417 const struct got_error *err = NULL;
419 size_t len, n_have_refs = 0;
420 struct got_pathlist_entry *pe;
422 len = sizeof(struct got_imsg_fetch_request);
423 TAILQ_FOREACH(pe, have_refs, entry) {
424 len += sizeof(struct got_imsg_fetch_have_ref) + pe->path_len;
427 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
429 return got_error(GOT_ERR_NO_SPACE);
432 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_REQUEST, 0, 0, len);
435 return got_error_from_errno("imsg_create FETCH_REQUEST");
438 /* Keep in sync with struct got_imsg_fetch_request definition! */
439 if (imsg_add(wbuf, &fetch_all_branches, sizeof(fetch_all_branches))
441 err = got_error_from_errno("imsg_add FETCH_REQUEST");
446 if (imsg_add(wbuf, &n_have_refs, sizeof(n_have_refs)) == -1) {
447 err = got_error_from_errno("imsg_add FETCH_REQUEST");
453 TAILQ_FOREACH(pe, have_refs, entry) {
454 const char *name = pe->path;
455 size_t name_len = pe->path_len;
456 struct got_object_id *id = pe->data;
458 /* Keep in sync with struct got_imsg_fetch_have_ref! */
459 if (imsg_add(wbuf, id->sha1, sizeof(id->sha1)) == -1) {
460 err = got_error_from_errno("imsg_add FETCH_REQUEST");
465 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
466 err = got_error_from_errno("imsg_add FETCH_REQUEST");
471 if (imsg_add(wbuf, name, name_len) == -1) {
472 err = got_error_from_errno("imsg_add FETCH_REQUEST");
480 imsg_close(ibuf, wbuf);
481 return flush_imsg(ibuf);
484 const struct got_error *
485 got_privsep_send_fetch_outfd(struct imsgbuf *ibuf, int fd)
487 return send_fd(ibuf, GOT_IMSG_FETCH_OUTFD, fd);
491 const struct got_error *
492 got_privsep_send_fetch_symrefs(struct imsgbuf *ibuf,
493 struct got_pathlist_head *symrefs)
495 const struct got_error *err = NULL;
497 size_t len, nsymrefs = 0;
498 struct got_pathlist_entry *pe;
500 len = sizeof(struct got_imsg_fetch_symrefs);
501 TAILQ_FOREACH(pe, symrefs, entry) {
502 const char *target = pe->data;
503 len += sizeof(struct got_imsg_fetch_symref) +
504 pe->path_len + strlen(target);
508 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
509 return got_error(GOT_ERR_NO_SPACE);
511 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_SYMREFS, 0, 0, len);
513 return got_error_from_errno("imsg_create FETCH_SYMREFS");
515 /* Keep in sync with struct got_imsg_fetch_symrefs definition! */
516 if (imsg_add(wbuf, &nsymrefs, sizeof(nsymrefs)) == -1) {
517 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
522 TAILQ_FOREACH(pe, symrefs, entry) {
523 const char *name = pe->path;
524 size_t name_len = pe->path_len;
525 const char *target = pe->data;
526 size_t target_len = strlen(target);
528 /* Keep in sync with struct got_imsg_fetch_symref definition! */
529 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
530 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
534 if (imsg_add(wbuf, &target_len, sizeof(target_len)) == -1) {
535 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
539 if (imsg_add(wbuf, name, name_len) == -1) {
540 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
544 if (imsg_add(wbuf, target, target_len) == -1) {
545 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
552 imsg_close(ibuf, wbuf);
553 return flush_imsg(ibuf);
556 const struct got_error *
557 got_privsep_send_fetch_ref(struct imsgbuf *ibuf,
558 struct got_object_id *refid, const char *refname)
560 const struct got_error *err = NULL;
562 size_t len, reflen = strlen(refname);
564 len = sizeof(struct got_imsg_fetch_ref) + reflen;
565 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
566 return got_error(GOT_ERR_NO_SPACE);
568 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_REF, 0, 0, len);
570 return got_error_from_errno("imsg_create FETCH_REF");
572 /* Keep in sync with struct got_imsg_fetch_ref definition! */
573 if (imsg_add(wbuf, refid->sha1, SHA1_DIGEST_LENGTH) == -1) {
574 err = got_error_from_errno("imsg_add FETCH_REF");
578 if (imsg_add(wbuf, refname, reflen) == -1) {
579 err = got_error_from_errno("imsg_add FETCH_REF");
585 imsg_close(ibuf, wbuf);
586 return flush_imsg(ibuf);
589 const struct got_error *
590 got_privsep_send_fetch_server_progress(struct imsgbuf *ibuf, const char *msg,
593 if (msglen > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
594 return got_error(GOT_ERR_NO_SPACE);
599 if (imsg_compose(ibuf, GOT_IMSG_FETCH_SERVER_PROGRESS, 0, 0, -1,
601 return got_error_from_errno(
602 "imsg_compose FETCH_SERVER_PROGRESS");
604 return flush_imsg(ibuf);
607 const struct got_error *
608 got_privsep_send_fetch_download_progress(struct imsgbuf *ibuf, off_t bytes)
610 if (imsg_compose(ibuf, GOT_IMSG_FETCH_DOWNLOAD_PROGRESS, 0, 0, -1,
611 &bytes, sizeof(bytes)) == -1)
612 return got_error_from_errno(
613 "imsg_compose FETCH_DOWNLOAD_PROGRESS");
615 return flush_imsg(ibuf);
618 const struct got_error *
619 got_privsep_send_fetch_done(struct imsgbuf *ibuf, struct got_object_id hash)
621 if (imsg_compose(ibuf, GOT_IMSG_FETCH_DONE, 0, 0, -1,
622 hash.sha1, SHA1_DIGEST_LENGTH) == -1)
623 return got_error_from_errno("imsg_compose FETCH");
624 return flush_imsg(ibuf);
628 const struct got_error *
629 got_privsep_recv_fetch_progress(int *done, struct got_object_id **id,
630 char **refname, struct got_pathlist_head *symrefs, char **server_progress,
631 off_t *packfile_size, struct imsgbuf *ibuf)
633 const struct got_error *err = NULL;
636 struct got_imsg_fetch_symrefs *isymrefs = NULL;
644 *server_progress = NULL;
647 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
651 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
652 switch (imsg.hdr.type) {
654 if (datalen < sizeof(struct got_imsg_error)) {
655 err = got_error(GOT_ERR_PRIVSEP_LEN);
658 err = recv_imsg_error(&imsg, datalen);
660 case GOT_IMSG_FETCH_SYMREFS:
661 if (datalen < sizeof(*isymrefs)) {
662 err = got_error(GOT_ERR_PRIVSEP_LEN);
665 if (isymrefs != NULL) {
666 err = got_error(GOT_ERR_PRIVSEP_MSG);
669 isymrefs = (struct got_imsg_fetch_symrefs *)imsg.data;
670 off = sizeof(*isymrefs);
671 remain = datalen - off;
672 for (n = 0; n < isymrefs->nsymrefs; n++) {
673 struct got_imsg_fetch_symref *s;
675 if (remain < sizeof(struct got_imsg_fetch_symref)) {
676 err = got_error(GOT_ERR_PRIVSEP_LEN);
679 s = (struct got_imsg_fetch_symref *)(imsg.data + off);
681 remain -= sizeof(*s);
682 if (remain < s->name_len) {
683 err = got_error(GOT_ERR_PRIVSEP_LEN);
686 name = strndup(imsg.data + off, s->name_len);
688 err = got_error_from_errno("strndup");
692 remain -= s->name_len;
693 if (remain < s->target_len) {
694 err = got_error(GOT_ERR_PRIVSEP_LEN);
698 target = strndup(imsg.data + off, s->target_len);
699 if (target == NULL) {
700 err = got_error_from_errno("strndup");
704 off += s->target_len;
705 remain -= s->target_len;
706 err = got_pathlist_append(symrefs, name, target);
714 case GOT_IMSG_FETCH_REF:
715 if (datalen <= SHA1_DIGEST_LENGTH) {
716 err = got_error(GOT_ERR_PRIVSEP_MSG);
719 *id = malloc(sizeof(**id));
721 err = got_error_from_errno("malloc");
724 memcpy((*id)->sha1, imsg.data, SHA1_DIGEST_LENGTH);
725 *refname = strndup(imsg.data + SHA1_DIGEST_LENGTH,
726 datalen - SHA1_DIGEST_LENGTH);
727 if (*refname == NULL) {
728 err = got_error_from_errno("strndup");
732 case GOT_IMSG_FETCH_SERVER_PROGRESS:
734 err = got_error(GOT_ERR_PRIVSEP_LEN);
737 *server_progress = strndup(imsg.data, datalen);
738 if (*server_progress == NULL) {
739 err = got_error_from_errno("strndup");
742 for (i = 0; i < datalen; i++) {
743 if (!isprint((unsigned char)(*server_progress)[i]) &&
744 !isspace((unsigned char)(*server_progress)[i])) {
745 err = got_error(GOT_ERR_PRIVSEP_MSG);
746 free(*server_progress);
747 *server_progress = NULL;
752 case GOT_IMSG_FETCH_DOWNLOAD_PROGRESS:
753 if (datalen < sizeof(*packfile_size)) {
754 err = got_error(GOT_ERR_PRIVSEP_MSG);
757 memcpy(packfile_size, imsg.data, sizeof(*packfile_size));
759 case GOT_IMSG_FETCH_DONE:
760 *id = malloc(sizeof(**id));
762 err = got_error_from_errno("malloc");
765 if (datalen != SHA1_DIGEST_LENGTH) {
766 err = got_error(GOT_ERR_PRIVSEP_MSG);
769 memcpy((*id)->sha1, imsg.data, SHA1_DIGEST_LENGTH);
773 err = got_error(GOT_ERR_PRIVSEP_MSG);
787 const struct got_error *
788 got_privsep_send_index_pack_req(struct imsgbuf *ibuf, uint8_t *pack_hash,
791 const struct got_error *err = NULL;
793 /* Keep in sync with struct got_imsg_index_pack_request */
794 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_REQUEST, 0, 0, fd,
795 pack_hash, SHA1_DIGEST_LENGTH) == -1) {
796 err = got_error_from_errno("imsg_compose INDEX_REQUEST");
800 return flush_imsg(ibuf);
803 const struct got_error *
804 got_privsep_send_index_pack_outfd(struct imsgbuf *ibuf, int fd)
806 return send_fd(ibuf, GOT_IMSG_IDXPACK_OUTFD, fd);
809 const struct got_error *
810 got_privsep_send_index_pack_progress(struct imsgbuf *ibuf, int nobj_total,
811 int nobj_indexed, int nobj_loose, int nobj_resolved)
813 struct got_imsg_index_pack_progress iprogress;
815 iprogress.nobj_total = nobj_total;
816 iprogress.nobj_indexed = nobj_indexed;
817 iprogress.nobj_loose = nobj_loose;
818 iprogress.nobj_resolved = nobj_resolved;
820 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_PROGRESS, 0, 0, -1,
821 &iprogress, sizeof(iprogress)) == -1)
822 return got_error_from_errno("imsg_compose IDXPACK_PROGRESS");
824 return flush_imsg(ibuf);
827 const struct got_error *
828 got_privsep_send_index_pack_done(struct imsgbuf *ibuf)
830 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_DONE, 0, 0, -1, NULL, 0) == -1)
831 return got_error_from_errno("imsg_compose FETCH");
832 return flush_imsg(ibuf);
835 const struct got_error *
836 got_privsep_recv_index_progress(int *done, int *nobj_total,
837 int *nobj_indexed, int *nobj_loose, int *nobj_resolved,
838 struct imsgbuf *ibuf)
840 const struct got_error *err = NULL;
842 struct got_imsg_index_pack_progress *iprogress;
850 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
854 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
855 switch (imsg.hdr.type) {
857 if (datalen < sizeof(struct got_imsg_error)) {
858 err = got_error(GOT_ERR_PRIVSEP_LEN);
861 err = recv_imsg_error(&imsg, datalen);
863 case GOT_IMSG_IDXPACK_PROGRESS:
864 if (datalen < sizeof(*iprogress)) {
865 err = got_error(GOT_ERR_PRIVSEP_LEN);
868 iprogress = (struct got_imsg_index_pack_progress *)imsg.data;
869 *nobj_total = iprogress->nobj_total;
870 *nobj_indexed = iprogress->nobj_indexed;
871 *nobj_loose = iprogress->nobj_loose;
872 *nobj_resolved = iprogress->nobj_resolved;
874 case GOT_IMSG_IDXPACK_DONE:
876 err = got_error(GOT_ERR_PRIVSEP_LEN);
882 err = got_error(GOT_ERR_PRIVSEP_MSG);
890 const struct got_error *
891 got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
892 struct imsgbuf *ibuf)
894 const struct got_error *err = NULL;
895 struct got_imsg_object *iobj;
896 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
898 if (datalen != sizeof(*iobj))
899 return got_error(GOT_ERR_PRIVSEP_LEN);
902 *obj = calloc(1, sizeof(**obj));
904 return got_error_from_errno("calloc");
906 memcpy((*obj)->id.sha1, iobj->id, SHA1_DIGEST_LENGTH);
907 (*obj)->type = iobj->type;
908 (*obj)->flags = iobj->flags;
909 (*obj)->hdrlen = iobj->hdrlen;
910 (*obj)->size = iobj->size;
911 /* path_packfile is handled by caller */
912 if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
913 (*obj)->pack_offset = iobj->pack_offset;
914 (*obj)->pack_idx = iobj->pack_idx;
920 const struct got_error *
921 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
923 const struct got_error *err = NULL;
925 const size_t min_datalen =
926 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
930 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
934 switch (imsg.hdr.type) {
935 case GOT_IMSG_OBJECT:
936 err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
939 err = got_error(GOT_ERR_PRIVSEP_MSG);
948 static const struct got_error *
949 send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
952 const struct got_error *err = NULL;
953 size_t offset, remain;
958 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
960 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
961 commit->logmsg + offset, n) == -1) {
962 err = got_error_from_errno("imsg_compose "
967 err = flush_imsg(ibuf);
978 const struct got_error *
979 got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
981 const struct got_error *err = NULL;
982 struct got_imsg_commit_object *icommit;
985 struct got_object_qid *qid;
986 size_t author_len = strlen(commit->author);
987 size_t committer_len = strlen(commit->committer);
988 size_t logmsg_len = strlen(commit->logmsg);
990 total = sizeof(*icommit) + author_len + committer_len +
991 commit->nparents * SHA1_DIGEST_LENGTH;
995 return got_error_from_errno("malloc");
997 icommit = (struct got_imsg_commit_object *)buf;
998 memcpy(icommit->tree_id, commit->tree_id->sha1,
999 sizeof(icommit->tree_id));
1000 icommit->author_len = author_len;
1001 icommit->author_time = commit->author_time;
1002 icommit->author_gmtoff = commit->author_gmtoff;
1003 icommit->committer_len = committer_len;
1004 icommit->committer_time = commit->committer_time;
1005 icommit->committer_gmtoff = commit->committer_gmtoff;
1006 icommit->logmsg_len = logmsg_len;
1007 icommit->nparents = commit->nparents;
1009 len = sizeof(*icommit);
1010 memcpy(buf + len, commit->author, author_len);
1012 memcpy(buf + len, commit->committer, committer_len);
1013 len += committer_len;
1014 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
1015 memcpy(buf + len, qid->id, SHA1_DIGEST_LENGTH);
1016 len += SHA1_DIGEST_LENGTH;
1019 if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
1020 err = got_error_from_errno("imsg_compose COMMIT");
1024 if (logmsg_len == 0 ||
1025 logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1026 err = flush_imsg(ibuf);
1030 err = send_commit_logmsg(ibuf, commit, logmsg_len);
1036 static const struct got_error *
1037 get_commit_from_imsg(struct got_commit_object **commit,
1038 struct imsg *imsg, size_t datalen, struct imsgbuf *ibuf)
1040 const struct got_error *err = NULL;
1041 struct got_imsg_commit_object *icommit;
1045 if (datalen < sizeof(*icommit))
1046 return got_error(GOT_ERR_PRIVSEP_LEN);
1048 icommit = imsg->data;
1049 if (datalen != sizeof(*icommit) + icommit->author_len +
1050 icommit->committer_len +
1051 icommit->nparents * SHA1_DIGEST_LENGTH)
1052 return got_error(GOT_ERR_PRIVSEP_LEN);
1054 if (icommit->nparents < 0)
1055 return got_error(GOT_ERR_PRIVSEP_LEN);
1057 len += sizeof(*icommit);
1059 *commit = got_object_commit_alloc_partial();
1060 if (*commit == NULL)
1061 return got_error_from_errno(
1062 "got_object_commit_alloc_partial");
1064 memcpy((*commit)->tree_id->sha1, icommit->tree_id,
1065 SHA1_DIGEST_LENGTH);
1066 (*commit)->author_time = icommit->author_time;
1067 (*commit)->author_gmtoff = icommit->author_gmtoff;
1068 (*commit)->committer_time = icommit->committer_time;
1069 (*commit)->committer_gmtoff = icommit->committer_gmtoff;
1071 if (icommit->author_len == 0) {
1072 (*commit)->author = strdup("");
1073 if ((*commit)->author == NULL) {
1074 err = got_error_from_errno("strdup");
1078 (*commit)->author = malloc(icommit->author_len + 1);
1079 if ((*commit)->author == NULL) {
1080 err = got_error_from_errno("malloc");
1083 memcpy((*commit)->author, imsg->data + len,
1084 icommit->author_len);
1085 (*commit)->author[icommit->author_len] = '\0';
1087 len += icommit->author_len;
1089 if (icommit->committer_len == 0) {
1090 (*commit)->committer = strdup("");
1091 if ((*commit)->committer == NULL) {
1092 err = got_error_from_errno("strdup");
1096 (*commit)->committer =
1097 malloc(icommit->committer_len + 1);
1098 if ((*commit)->committer == NULL) {
1099 err = got_error_from_errno("malloc");
1102 memcpy((*commit)->committer, imsg->data + len,
1103 icommit->committer_len);
1104 (*commit)->committer[icommit->committer_len] = '\0';
1106 len += icommit->committer_len;
1108 if (icommit->logmsg_len == 0) {
1109 (*commit)->logmsg = strdup("");
1110 if ((*commit)->logmsg == NULL) {
1111 err = got_error_from_errno("strdup");
1115 size_t offset = 0, remain = icommit->logmsg_len;
1117 (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
1118 if ((*commit)->logmsg == NULL) {
1119 err = got_error_from_errno("malloc");
1122 while (remain > 0) {
1123 struct imsg imsg_log;
1124 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1127 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1131 if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG) {
1132 err = got_error(GOT_ERR_PRIVSEP_MSG);
1136 memcpy((*commit)->logmsg + offset,
1138 imsg_free(&imsg_log);
1142 (*commit)->logmsg[icommit->logmsg_len] = '\0';
1145 for (i = 0; i < icommit->nparents; i++) {
1146 struct got_object_qid *qid;
1148 err = got_object_qid_alloc_partial(&qid);
1151 memcpy(qid->id, imsg->data + len +
1152 i * SHA1_DIGEST_LENGTH, sizeof(*qid->id));
1153 SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
1154 (*commit)->nparents++;
1158 got_object_commit_close(*commit);
1164 const struct got_error *
1165 got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
1167 const struct got_error *err = NULL;
1170 const size_t min_datalen =
1171 MIN(sizeof(struct got_imsg_error),
1172 sizeof(struct got_imsg_commit_object));
1176 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1180 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1182 switch (imsg.hdr.type) {
1183 case GOT_IMSG_COMMIT:
1184 err = get_commit_from_imsg(commit, &imsg, datalen, ibuf);
1187 err = got_error(GOT_ERR_PRIVSEP_MSG);
1196 const struct got_error *
1197 got_privsep_send_tree(struct imsgbuf *ibuf, struct got_pathlist_head *entries,
1200 const struct got_error *err = NULL;
1201 struct got_imsg_tree_object itree;
1202 struct got_pathlist_entry *pe;
1204 int nimsg; /* number of imsg queued in ibuf */
1206 itree.nentries = nentries;
1207 if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
1209 return got_error_from_errno("imsg_compose TREE");
1211 totlen = sizeof(itree);
1213 TAILQ_FOREACH(pe, entries, entry) {
1214 const char *name = pe->path;
1215 struct got_parsed_tree_entry *pte = pe->data;
1217 size_t namelen = strlen(name);
1218 size_t len = sizeof(struct got_imsg_tree_entry) + namelen;
1220 if (len > MAX_IMSGSIZE)
1221 return got_error(GOT_ERR_NO_SPACE);
1224 if (totlen + len >= MAX_IMSGSIZE - (IMSG_HEADER_SIZE * nimsg)) {
1225 err = flush_imsg(ibuf);
1231 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_ENTRY, 0, 0, len);
1233 return got_error_from_errno("imsg_create TREE_ENTRY");
1235 /* Keep in sync with struct got_imsg_tree_object definition! */
1236 if (imsg_add(wbuf, pte->id, SHA1_DIGEST_LENGTH) == -1) {
1237 err = got_error_from_errno("imsg_add TREE_ENTRY");
1241 if (imsg_add(wbuf, &pte->mode, sizeof(pte->mode)) == -1) {
1242 err = got_error_from_errno("imsg_add TREE_ENTRY");
1247 if (imsg_add(wbuf, name, namelen) == -1) {
1248 err = got_error_from_errno("imsg_add TREE_ENTRY");
1254 imsg_close(ibuf, wbuf);
1259 return flush_imsg(ibuf);
1262 const struct got_error *
1263 got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
1265 const struct got_error *err = NULL;
1266 const size_t min_datalen =
1267 MIN(sizeof(struct got_imsg_error),
1268 sizeof(struct got_imsg_tree_object));
1269 struct got_imsg_tree_object *itree;
1274 err = read_imsg(ibuf);
1282 struct got_imsg_tree_entry *ite;
1283 struct got_tree_entry *te = NULL;
1285 n = imsg_get(ibuf, &imsg);
1287 if (*tree && (*tree)->nentries != nentries)
1292 if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen)
1293 return got_error(GOT_ERR_PRIVSEP_LEN);
1295 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1297 switch (imsg.hdr.type) {
1298 case GOT_IMSG_ERROR:
1299 err = recv_imsg_error(&imsg, datalen);
1302 /* This message should only appear once. */
1303 if (*tree != NULL) {
1304 err = got_error(GOT_ERR_PRIVSEP_MSG);
1307 if (datalen != sizeof(*itree)) {
1308 err = got_error(GOT_ERR_PRIVSEP_LEN);
1312 *tree = malloc(sizeof(**tree));
1313 if (*tree == NULL) {
1314 err = got_error_from_errno("malloc");
1317 (*tree)->entries = calloc(itree->nentries,
1318 sizeof(struct got_tree_entry));
1319 if ((*tree)->entries == NULL) {
1320 err = got_error_from_errno("malloc");
1323 (*tree)->nentries = itree->nentries;
1324 (*tree)->refcnt = 0;
1326 case GOT_IMSG_TREE_ENTRY:
1327 /* This message should be preceeded by GOT_IMSG_TREE. */
1328 if (*tree == NULL) {
1329 err = got_error(GOT_ERR_PRIVSEP_MSG);
1332 if (datalen < sizeof(*ite) || datalen > MAX_IMSGSIZE) {
1333 err = got_error(GOT_ERR_PRIVSEP_LEN);
1337 /* Remaining data contains the entry's name. */
1338 datalen -= sizeof(*ite);
1339 if (datalen == 0 || datalen > MAX_IMSGSIZE) {
1340 err = got_error(GOT_ERR_PRIVSEP_LEN);
1345 if (datalen + 1 > sizeof(te->name)) {
1346 err = got_error(GOT_ERR_NO_SPACE);
1349 te = &(*tree)->entries[nentries];
1350 memcpy(te->name, imsg.data + sizeof(*ite), datalen);
1351 te->name[datalen] = '\0';
1353 memcpy(te->id.sha1, ite->id, SHA1_DIGEST_LENGTH);
1354 te->mode = ite->mode;
1359 err = got_error(GOT_ERR_PRIVSEP_MSG);
1366 if (*tree && (*tree)->nentries != nentries) {
1368 err = got_error(GOT_ERR_PRIVSEP_LEN);
1369 got_object_tree_close(*tree);
1376 const struct got_error *
1377 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
1378 const uint8_t *data)
1380 struct got_imsg_blob iblob;
1383 iblob.hdrlen = hdrlen;
1388 if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
1389 return got_error(GOT_ERR_NO_SPACE);
1391 buf = malloc(sizeof(iblob) + size);
1393 return got_error_from_errno("malloc");
1395 memcpy(buf, &iblob, sizeof(iblob));
1396 memcpy(buf + sizeof(iblob), data, size);
1397 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
1398 sizeof(iblob) + size) == -1) {
1400 return got_error_from_errno("imsg_compose BLOB");
1404 /* Data has already been written to file descriptor. */
1405 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
1406 sizeof(iblob)) == -1)
1407 return got_error_from_errno("imsg_compose BLOB");
1411 return flush_imsg(ibuf);
1414 const struct got_error *
1415 got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1416 struct imsgbuf *ibuf)
1418 const struct got_error *err = NULL;
1420 struct got_imsg_blob *iblob;
1425 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1429 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1431 switch (imsg.hdr.type) {
1433 if (datalen < sizeof(*iblob)) {
1434 err = got_error(GOT_ERR_PRIVSEP_LEN);
1438 *size = iblob->size;
1439 *hdrlen = iblob->hdrlen;
1441 if (datalen == sizeof(*iblob)) {
1442 /* Data has been written to file descriptor. */
1446 if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX) {
1447 err = got_error(GOT_ERR_PRIVSEP_LEN);
1451 *outbuf = malloc(*size);
1452 if (*outbuf == NULL) {
1453 err = got_error_from_errno("malloc");
1456 memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
1459 err = got_error(GOT_ERR_PRIVSEP_MSG);
1468 static const struct got_error *
1469 send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
1471 const struct got_error *err = NULL;
1472 size_t offset, remain;
1475 remain = tagmsg_len;
1476 while (remain > 0) {
1477 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1479 if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
1480 tag->tagmsg + offset, n) == -1) {
1481 err = got_error_from_errno("imsg_compose TAG_TAGMSG");
1485 err = flush_imsg(ibuf);
1496 const struct got_error *
1497 got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
1499 const struct got_error *err = NULL;
1500 struct got_imsg_tag_object *itag;
1503 size_t tag_len = strlen(tag->tag);
1504 size_t tagger_len = strlen(tag->tagger);
1505 size_t tagmsg_len = strlen(tag->tagmsg);
1507 total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
1509 buf = malloc(total);
1511 return got_error_from_errno("malloc");
1513 itag = (struct got_imsg_tag_object *)buf;
1514 memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
1515 itag->obj_type = tag->obj_type;
1516 itag->tag_len = tag_len;
1517 itag->tagger_len = tagger_len;
1518 itag->tagger_time = tag->tagger_time;
1519 itag->tagger_gmtoff = tag->tagger_gmtoff;
1520 itag->tagmsg_len = tagmsg_len;
1522 len = sizeof(*itag);
1523 memcpy(buf + len, tag->tag, tag_len);
1525 memcpy(buf + len, tag->tagger, tagger_len);
1528 if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
1529 err = got_error_from_errno("imsg_compose TAG");
1533 if (tagmsg_len == 0 ||
1534 tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1535 err = flush_imsg(ibuf);
1539 err = send_tagmsg(ibuf, tag, tagmsg_len);
1545 const struct got_error *
1546 got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
1548 const struct got_error *err = NULL;
1550 struct got_imsg_tag_object *itag;
1551 size_t len, datalen;
1552 const size_t min_datalen =
1553 MIN(sizeof(struct got_imsg_error),
1554 sizeof(struct got_imsg_tag_object));
1558 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1562 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1565 switch (imsg.hdr.type) {
1567 if (datalen < sizeof(*itag)) {
1568 err = got_error(GOT_ERR_PRIVSEP_LEN);
1572 if (datalen != sizeof(*itag) + itag->tag_len +
1574 err = got_error(GOT_ERR_PRIVSEP_LEN);
1577 len += sizeof(*itag);
1579 *tag = calloc(1, sizeof(**tag));
1581 err = got_error_from_errno("calloc");
1585 memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
1587 if (itag->tag_len == 0) {
1588 (*tag)->tag = strdup("");
1589 if ((*tag)->tag == NULL) {
1590 err = got_error_from_errno("strdup");
1594 (*tag)->tag = malloc(itag->tag_len + 1);
1595 if ((*tag)->tag == NULL) {
1596 err = got_error_from_errno("malloc");
1599 memcpy((*tag)->tag, imsg.data + len,
1601 (*tag)->tag[itag->tag_len] = '\0';
1603 len += itag->tag_len;
1605 (*tag)->obj_type = itag->obj_type;
1606 (*tag)->tagger_time = itag->tagger_time;
1607 (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1609 if (itag->tagger_len == 0) {
1610 (*tag)->tagger = strdup("");
1611 if ((*tag)->tagger == NULL) {
1612 err = got_error_from_errno("strdup");
1616 (*tag)->tagger = malloc(itag->tagger_len + 1);
1617 if ((*tag)->tagger == NULL) {
1618 err = got_error_from_errno("malloc");
1621 memcpy((*tag)->tagger, imsg.data + len,
1623 (*tag)->tagger[itag->tagger_len] = '\0';
1625 len += itag->tagger_len;
1627 if (itag->tagmsg_len == 0) {
1628 (*tag)->tagmsg = strdup("");
1629 if ((*tag)->tagmsg == NULL) {
1630 err = got_error_from_errno("strdup");
1634 size_t offset = 0, remain = itag->tagmsg_len;
1636 (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1637 if ((*tag)->tagmsg == NULL) {
1638 err = got_error_from_errno("malloc");
1641 while (remain > 0) {
1642 struct imsg imsg_log;
1643 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1646 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1650 if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1651 return got_error(GOT_ERR_PRIVSEP_MSG);
1653 memcpy((*tag)->tagmsg + offset, imsg_log.data,
1655 imsg_free(&imsg_log);
1659 (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1664 err = got_error(GOT_ERR_PRIVSEP_MSG);
1673 const struct got_error *
1674 got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1675 struct got_packidx *packidx)
1677 const struct got_error *err = NULL;
1678 struct got_imsg_packidx ipackidx;
1679 struct got_imsg_pack ipack;
1682 ipackidx.len = packidx->len;
1683 fd = dup(packidx->fd);
1685 return got_error_from_errno("dup");
1687 if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
1688 sizeof(ipackidx)) == -1) {
1689 err = got_error_from_errno("imsg_compose PACKIDX");
1694 if (strlcpy(ipack.path_packfile, pack->path_packfile,
1695 sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
1696 return got_error(GOT_ERR_NO_SPACE);
1697 ipack.filesize = pack->filesize;
1701 return got_error_from_errno("dup");
1703 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
1705 err = got_error_from_errno("imsg_compose PACK");
1710 return flush_imsg(ibuf);
1713 const struct got_error *
1714 got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
1715 struct got_object_id *id)
1717 struct got_imsg_packed_object iobj;
1720 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
1722 if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
1723 &iobj, sizeof(iobj)) == -1)
1724 return got_error_from_errno("imsg_compose "
1725 "PACKED_OBJECT_REQUEST");
1727 return flush_imsg(ibuf);
1730 const struct got_error *
1731 got_privsep_send_gitconfig_parse_req(struct imsgbuf *ibuf, int fd)
1733 const struct got_error *err = NULL;
1735 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_PARSE_REQUEST, 0, 0, fd,
1737 err = got_error_from_errno("imsg_compose "
1738 "GITCONFIG_PARSE_REQUEST");
1743 return flush_imsg(ibuf);
1746 const struct got_error *
1747 got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *ibuf)
1749 if (imsg_compose(ibuf,
1750 GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST, 0, 0, -1,
1752 return got_error_from_errno("imsg_compose "
1753 "GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST");
1755 return flush_imsg(ibuf);
1758 const struct got_error *
1759 got_privsep_send_gitconfig_author_name_req(struct imsgbuf *ibuf)
1761 if (imsg_compose(ibuf,
1762 GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST, 0, 0, -1, NULL, 0) == -1)
1763 return got_error_from_errno("imsg_compose "
1764 "GITCONFIG_AUTHOR_NAME_REQUEST");
1766 return flush_imsg(ibuf);
1769 const struct got_error *
1770 got_privsep_send_gitconfig_author_email_req(struct imsgbuf *ibuf)
1772 if (imsg_compose(ibuf,
1773 GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST, 0, 0, -1, NULL, 0) == -1)
1774 return got_error_from_errno("imsg_compose "
1775 "GITCONFIG_AUTHOR_EMAIL_REQUEST");
1777 return flush_imsg(ibuf);
1780 const struct got_error *
1781 got_privsep_send_gitconfig_remotes_req(struct imsgbuf *ibuf)
1783 if (imsg_compose(ibuf,
1784 GOT_IMSG_GITCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
1785 return got_error_from_errno("imsg_compose "
1786 "GITCONFIG_REMOTE_REQUEST");
1788 return flush_imsg(ibuf);
1791 const struct got_error *
1792 got_privsep_send_gitconfig_owner_req(struct imsgbuf *ibuf)
1794 if (imsg_compose(ibuf,
1795 GOT_IMSG_GITCONFIG_OWNER_REQUEST, 0, 0, -1, NULL, 0) == -1)
1796 return got_error_from_errno("imsg_compose "
1797 "GITCONFIG_OWNER_REQUEST");
1799 return flush_imsg(ibuf);
1802 const struct got_error *
1803 got_privsep_send_gitconfig_str(struct imsgbuf *ibuf, const char *value)
1805 size_t len = value ? strlen(value) + 1 : 0;
1807 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_STR_VAL, 0, 0, -1,
1809 return got_error_from_errno("imsg_compose GITCONFIG_STR_VAL");
1811 return flush_imsg(ibuf);
1814 const struct got_error *
1815 got_privsep_recv_gitconfig_str(char **str, struct imsgbuf *ibuf)
1817 const struct got_error *err = NULL;
1820 const size_t min_datalen = 0;
1824 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1827 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1829 switch (imsg.hdr.type) {
1830 case GOT_IMSG_GITCONFIG_STR_VAL:
1833 *str = malloc(datalen);
1835 err = got_error_from_errno("malloc");
1838 if (strlcpy(*str, imsg.data, datalen) >= datalen)
1839 err = got_error(GOT_ERR_NO_SPACE);
1842 err = got_error(GOT_ERR_PRIVSEP_MSG);
1850 const struct got_error *
1851 got_privsep_send_gitconfig_int(struct imsgbuf *ibuf, int value)
1853 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_INT_VAL, 0, 0, -1,
1854 &value, sizeof(value)) == -1)
1855 return got_error_from_errno("imsg_compose GITCONFIG_INT_VAL");
1857 return flush_imsg(ibuf);
1860 const struct got_error *
1861 got_privsep_recv_gitconfig_int(int *val, struct imsgbuf *ibuf)
1863 const struct got_error *err = NULL;
1866 const size_t min_datalen =
1867 MIN(sizeof(struct got_imsg_error), sizeof(int));
1871 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1874 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1876 switch (imsg.hdr.type) {
1877 case GOT_IMSG_GITCONFIG_INT_VAL:
1878 if (datalen != sizeof(*val)) {
1879 err = got_error(GOT_ERR_PRIVSEP_LEN);
1882 memcpy(val, imsg.data, sizeof(*val));
1885 err = got_error(GOT_ERR_PRIVSEP_MSG);
1893 const struct got_error *
1894 got_privsep_send_gitconfig_remotes(struct imsgbuf *ibuf,
1895 struct got_remote_repo *remotes, int nremotes)
1897 const struct got_error *err = NULL;
1898 struct got_imsg_remotes iremotes;
1901 iremotes.nremotes = nremotes;
1902 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_REMOTES, 0, 0, -1,
1903 &iremotes, sizeof(iremotes)) == -1)
1904 return got_error_from_errno("imsg_compose GITCONFIG_REMOTES");
1906 err = flush_imsg(ibuf);
1911 for (i = 0; i < nremotes; i++) {
1912 struct got_imsg_remote iremote;
1913 size_t len = sizeof(iremote);
1916 iremote.mirror_references = remotes[i].mirror_references;
1917 iremote.name_len = strlen(remotes[i].name);
1918 len += iremote.name_len;
1919 iremote.url_len = strlen(remotes[i].url);
1920 len += iremote.url_len;
1922 wbuf = imsg_create(ibuf, GOT_IMSG_GITCONFIG_REMOTE, 0, 0, len);
1924 return got_error_from_errno(
1925 "imsg_create GITCONFIG_REMOTE");
1927 if (imsg_add(wbuf, &iremote, sizeof(iremote)) == -1) {
1928 err = got_error_from_errno(
1929 "imsg_add GITCONFIG_REMOTE");
1934 if (imsg_add(wbuf, remotes[i].name, iremote.name_len) == -1) {
1935 err = got_error_from_errno(
1936 "imsg_add GITCONFIG_REMOTE");
1940 if (imsg_add(wbuf, remotes[i].url, iremote.url_len) == -1) {
1941 err = got_error_from_errno(
1942 "imsg_add GITCONFIG_REMOTE");
1948 imsg_close(ibuf, wbuf);
1949 err = flush_imsg(ibuf);
1957 const struct got_error *
1958 got_privsep_recv_gitconfig_remotes(struct got_remote_repo **remotes,
1959 int *nremotes, struct imsgbuf *ibuf)
1961 const struct got_error *err = NULL;
1964 struct got_imsg_remotes iremotes;
1965 struct got_imsg_remote iremote;
1969 iremotes.nremotes = 0;
1971 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremotes));
1974 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1976 switch (imsg.hdr.type) {
1977 case GOT_IMSG_GITCONFIG_REMOTES:
1978 if (datalen != sizeof(iremotes)) {
1979 err = got_error(GOT_ERR_PRIVSEP_LEN);
1982 memcpy(&iremotes, imsg.data, sizeof(iremotes));
1983 if (iremotes.nremotes == 0) {
1990 return got_error(GOT_ERR_PRIVSEP_MSG);
1995 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(**remotes));
1996 if (*remotes == NULL)
1997 return got_error_from_errno("recallocarray");
1999 while (*nremotes < iremotes.nremotes) {
2000 struct got_remote_repo *remote;
2002 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremote));
2005 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2007 switch (imsg.hdr.type) {
2008 case GOT_IMSG_GITCONFIG_REMOTE:
2009 remote = &(*remotes)[*nremotes];
2010 if (datalen < sizeof(iremote)) {
2011 err = got_error(GOT_ERR_PRIVSEP_LEN);
2014 memcpy(&iremote, imsg.data, sizeof(iremote));
2015 if (iremote.name_len == 0 || iremote.url_len == 0 ||
2016 (sizeof(iremote) + iremote.name_len +
2017 iremote.url_len) > datalen) {
2018 err = got_error(GOT_ERR_PRIVSEP_LEN);
2021 remote->name = strndup(imsg.data + sizeof(iremote),
2023 if (remote->name == NULL) {
2024 err = got_error_from_errno("strndup");
2027 remote->url = strndup(imsg.data + sizeof(iremote) +
2028 iremote.name_len, iremote.url_len);
2029 if (remote->url == NULL) {
2030 err = got_error_from_errno("strndup");
2034 remote->mirror_references = iremote.mirror_references;
2038 err = got_error(GOT_ERR_PRIVSEP_MSG);
2049 for (i = 0; i < *nremotes; i++) {
2050 free((*remotes)[i].name);
2051 free((*remotes)[i].url);
2060 const struct got_error *
2061 got_privsep_send_commit_traversal_request(struct imsgbuf *ibuf,
2062 struct got_object_id *id, int idx, const char *path)
2064 const struct got_error *err = NULL;
2066 size_t path_len = strlen(path) + 1;
2068 wbuf = imsg_create(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_REQUEST, 0, 0,
2069 sizeof(struct got_imsg_commit_traversal_request) + path_len);
2071 return got_error_from_errno(
2072 "imsg_create COMMIT_TRAVERSAL_REQUEST");
2073 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
2074 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
2078 if (imsg_add(wbuf, &idx, sizeof(idx)) == -1) {
2079 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
2083 if (imsg_add(wbuf, path, path_len) == -1) {
2084 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
2090 imsg_close(ibuf, wbuf);
2092 return flush_imsg(ibuf);
2095 const struct got_error *
2096 got_privsep_send_traversed_commits(struct got_object_id *commit_ids,
2097 size_t ncommits, struct imsgbuf *ibuf)
2099 const struct got_error *err;
2103 wbuf = imsg_create(ibuf, GOT_IMSG_TRAVERSED_COMMITS, 0, 0,
2104 sizeof(struct got_imsg_traversed_commits) +
2105 ncommits * SHA1_DIGEST_LENGTH);
2107 return got_error_from_errno("imsg_create TRAVERSED_COMMITS");
2109 if (imsg_add(wbuf, &ncommits, sizeof(ncommits)) == -1) {
2110 err = got_error_from_errno("imsg_add TRAVERSED_COMMITS");
2114 for (i = 0; i < ncommits; i++) {
2115 struct got_object_id *id = &commit_ids[i];
2116 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
2117 err = got_error_from_errno(
2118 "imsg_add TRAVERSED_COMMITS");
2125 imsg_close(ibuf, wbuf);
2127 return flush_imsg(ibuf);
2130 const struct got_error *
2131 got_privsep_recv_traversed_commits(struct got_commit_object **changed_commit,
2132 struct got_object_id **changed_commit_id,
2133 struct got_object_id_queue *commit_ids, struct imsgbuf *ibuf)
2135 const struct got_error *err = NULL;
2137 struct got_imsg_traversed_commits *icommits;
2141 *changed_commit = NULL;
2142 *changed_commit_id = NULL;
2145 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2149 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2150 switch (imsg.hdr.type) {
2151 case GOT_IMSG_TRAVERSED_COMMITS:
2152 icommits = imsg.data;
2153 if (datalen != sizeof(*icommits) +
2154 icommits->ncommits * SHA1_DIGEST_LENGTH) {
2155 err = got_error(GOT_ERR_PRIVSEP_LEN);
2158 for (i = 0; i < icommits->ncommits; i++) {
2159 struct got_object_qid *qid;
2160 uint8_t *sha1 = (uint8_t *)imsg.data +
2161 sizeof(*icommits) + i * SHA1_DIGEST_LENGTH;
2162 err = got_object_qid_alloc_partial(&qid);
2165 memcpy(qid->id->sha1, sha1, SHA1_DIGEST_LENGTH);
2166 SIMPLEQ_INSERT_TAIL(commit_ids, qid, entry);
2168 /* The last commit may contain a change. */
2169 if (i == icommits->ncommits - 1) {
2170 *changed_commit_id =
2171 got_object_id_dup(qid->id);
2172 if (*changed_commit_id == NULL) {
2173 err = got_error_from_errno(
2174 "got_object_id_dup");
2180 case GOT_IMSG_COMMIT:
2181 if (*changed_commit_id == NULL) {
2182 err = got_error(GOT_ERR_PRIVSEP_MSG);
2185 err = get_commit_from_imsg(changed_commit, &imsg,
2188 case GOT_IMSG_COMMIT_TRAVERSAL_DONE:
2192 err = got_error(GOT_ERR_PRIVSEP_MSG);
2202 got_object_id_queue_free(commit_ids);
2206 const struct got_error *
2207 got_privsep_send_commit_traversal_done(struct imsgbuf *ibuf)
2209 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_DONE, 0, 0, -1,
2211 return got_error_from_errno("imsg_compose TRAVERSAL_DONE");
2213 return flush_imsg(ibuf);
2216 const struct got_error *
2217 got_privsep_unveil_exec_helpers(void)
2219 const char *helpers[] = {
2220 GOT_PATH_PROG_READ_PACK,
2221 GOT_PATH_PROG_READ_OBJECT,
2222 GOT_PATH_PROG_READ_COMMIT,
2223 GOT_PATH_PROG_READ_TREE,
2224 GOT_PATH_PROG_READ_BLOB,
2225 GOT_PATH_PROG_READ_TAG,
2226 GOT_PATH_PROG_READ_GITCONFIG,
2227 GOT_PATH_PROG_FETCH_PACK,
2228 GOT_PATH_PROG_INDEX_PACK,
2232 for (i = 0; i < nitems(helpers); i++) {
2233 if (unveil(helpers[i], "x") == 0)
2235 return got_error_from_errno2("unveil", helpers[i]);
2242 got_privsep_exec_child(int imsg_fds[2], const char *path, const char *repo_path)
2244 if (close(imsg_fds[0]) != 0) {
2245 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2249 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
2250 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2253 if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
2254 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2258 if (execl(path, path, repo_path, (char *)NULL) == -1) {
2259 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,