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,
416 struct got_pathlist_head *wanted_branches, int list_refs_only,
419 const struct got_error *err = NULL;
422 struct got_pathlist_entry *pe;
423 struct got_imsg_fetch_request fetchreq;
425 memset(&fetchreq, 0, sizeof(fetchreq));
426 fetchreq.fetch_all_branches = fetch_all_branches;
427 fetchreq.list_refs_only = list_refs_only;
428 fetchreq.verbosity = verbosity;
429 TAILQ_FOREACH(pe, have_refs, entry)
430 fetchreq.n_have_refs++;
431 TAILQ_FOREACH(pe, wanted_branches, entry)
432 fetchreq.n_wanted_branches++;
433 len = sizeof(struct got_imsg_fetch_request);
434 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
436 return got_error(GOT_ERR_NO_SPACE);
439 if (imsg_compose(ibuf, GOT_IMSG_FETCH_REQUEST, 0, 0, fd,
440 &fetchreq, sizeof(fetchreq)) == -1)
441 return got_error_from_errno(
442 "imsg_compose FETCH_SERVER_PROGRESS");
444 err = flush_imsg(ibuf);
451 TAILQ_FOREACH(pe, have_refs, entry) {
452 const char *name = pe->path;
453 size_t name_len = pe->path_len;
454 struct got_object_id *id = pe->data;
456 len = sizeof(struct got_imsg_fetch_have_ref) + name_len;
457 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_HAVE_REF, 0, 0, len);
459 return got_error_from_errno("imsg_create FETCH_HAVE_REF");
461 /* Keep in sync with struct got_imsg_fetch_have_ref! */
462 if (imsg_add(wbuf, id->sha1, sizeof(id->sha1)) == -1) {
463 err = got_error_from_errno("imsg_add FETCH_HAVE_REF");
467 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
468 err = got_error_from_errno("imsg_add FETCH_HAVE_REF");
472 if (imsg_add(wbuf, name, name_len) == -1) {
473 err = got_error_from_errno("imsg_add FETCH_HAVE_REF");
479 imsg_close(ibuf, wbuf);
480 err = flush_imsg(ibuf);
485 TAILQ_FOREACH(pe, wanted_branches, entry) {
486 const char *name = pe->path;
487 size_t name_len = pe->path_len;
489 len = sizeof(struct got_imsg_fetch_wanted_branch) + name_len;
490 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_WANTED_BRANCH, 0, 0,
493 return got_error_from_errno(
494 "imsg_create FETCH_WANTED_BRANCH");
496 /* Keep in sync with struct got_imsg_fetch_wanted_branch! */
497 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
498 err = got_error_from_errno(
499 "imsg_add FETCH_WANTED_BRANCH");
503 if (imsg_add(wbuf, name, name_len) == -1) {
504 err = got_error_from_errno(
505 "imsg_add FETCH_WANTED_BRANCH");
511 imsg_close(ibuf, wbuf);
512 err = flush_imsg(ibuf);
521 const struct got_error *
522 got_privsep_send_fetch_outfd(struct imsgbuf *ibuf, int fd)
524 return send_fd(ibuf, GOT_IMSG_FETCH_OUTFD, fd);
528 const struct got_error *
529 got_privsep_send_fetch_symrefs(struct imsgbuf *ibuf,
530 struct got_pathlist_head *symrefs)
532 const struct got_error *err = NULL;
534 size_t len, nsymrefs = 0;
535 struct got_pathlist_entry *pe;
537 len = sizeof(struct got_imsg_fetch_symrefs);
538 TAILQ_FOREACH(pe, symrefs, entry) {
539 const char *target = pe->data;
540 len += sizeof(struct got_imsg_fetch_symref) +
541 pe->path_len + strlen(target);
545 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
546 return got_error(GOT_ERR_NO_SPACE);
548 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_SYMREFS, 0, 0, len);
550 return got_error_from_errno("imsg_create FETCH_SYMREFS");
552 /* Keep in sync with struct got_imsg_fetch_symrefs definition! */
553 if (imsg_add(wbuf, &nsymrefs, sizeof(nsymrefs)) == -1) {
554 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
559 TAILQ_FOREACH(pe, symrefs, entry) {
560 const char *name = pe->path;
561 size_t name_len = pe->path_len;
562 const char *target = pe->data;
563 size_t target_len = strlen(target);
565 /* Keep in sync with struct got_imsg_fetch_symref definition! */
566 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
567 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
571 if (imsg_add(wbuf, &target_len, sizeof(target_len)) == -1) {
572 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
576 if (imsg_add(wbuf, name, name_len) == -1) {
577 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
581 if (imsg_add(wbuf, target, target_len) == -1) {
582 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
589 imsg_close(ibuf, wbuf);
590 return flush_imsg(ibuf);
593 const struct got_error *
594 got_privsep_send_fetch_ref(struct imsgbuf *ibuf,
595 struct got_object_id *refid, const char *refname)
597 const struct got_error *err = NULL;
599 size_t len, reflen = strlen(refname);
601 len = sizeof(struct got_imsg_fetch_ref) + reflen;
602 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
603 return got_error(GOT_ERR_NO_SPACE);
605 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_REF, 0, 0, len);
607 return got_error_from_errno("imsg_create FETCH_REF");
609 /* Keep in sync with struct got_imsg_fetch_ref definition! */
610 if (imsg_add(wbuf, refid->sha1, SHA1_DIGEST_LENGTH) == -1) {
611 err = got_error_from_errno("imsg_add FETCH_REF");
615 if (imsg_add(wbuf, refname, reflen) == -1) {
616 err = got_error_from_errno("imsg_add FETCH_REF");
622 imsg_close(ibuf, wbuf);
623 return flush_imsg(ibuf);
626 const struct got_error *
627 got_privsep_send_fetch_server_progress(struct imsgbuf *ibuf, const char *msg,
630 if (msglen > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
631 return got_error(GOT_ERR_NO_SPACE);
636 if (imsg_compose(ibuf, GOT_IMSG_FETCH_SERVER_PROGRESS, 0, 0, -1,
638 return got_error_from_errno(
639 "imsg_compose FETCH_SERVER_PROGRESS");
641 return flush_imsg(ibuf);
644 const struct got_error *
645 got_privsep_send_fetch_download_progress(struct imsgbuf *ibuf, off_t bytes)
647 if (imsg_compose(ibuf, GOT_IMSG_FETCH_DOWNLOAD_PROGRESS, 0, 0, -1,
648 &bytes, sizeof(bytes)) == -1)
649 return got_error_from_errno(
650 "imsg_compose FETCH_DOWNLOAD_PROGRESS");
652 return flush_imsg(ibuf);
655 const struct got_error *
656 got_privsep_send_fetch_done(struct imsgbuf *ibuf, struct got_object_id hash)
658 if (imsg_compose(ibuf, GOT_IMSG_FETCH_DONE, 0, 0, -1,
659 hash.sha1, SHA1_DIGEST_LENGTH) == -1)
660 return got_error_from_errno("imsg_compose FETCH");
661 return flush_imsg(ibuf);
665 const struct got_error *
666 got_privsep_recv_fetch_progress(int *done, struct got_object_id **id,
667 char **refname, struct got_pathlist_head *symrefs, char **server_progress,
668 off_t *packfile_size, struct imsgbuf *ibuf)
670 const struct got_error *err = NULL;
673 struct got_imsg_fetch_symrefs *isymrefs = NULL;
681 *server_progress = NULL;
684 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
688 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
689 switch (imsg.hdr.type) {
691 if (datalen < sizeof(struct got_imsg_error)) {
692 err = got_error(GOT_ERR_PRIVSEP_LEN);
695 err = recv_imsg_error(&imsg, datalen);
697 case GOT_IMSG_FETCH_SYMREFS:
698 if (datalen < sizeof(*isymrefs)) {
699 err = got_error(GOT_ERR_PRIVSEP_LEN);
702 if (isymrefs != NULL) {
703 err = got_error(GOT_ERR_PRIVSEP_MSG);
706 isymrefs = (struct got_imsg_fetch_symrefs *)imsg.data;
707 off = sizeof(*isymrefs);
708 remain = datalen - off;
709 for (n = 0; n < isymrefs->nsymrefs; n++) {
710 struct got_imsg_fetch_symref *s;
712 if (remain < sizeof(struct got_imsg_fetch_symref)) {
713 err = got_error(GOT_ERR_PRIVSEP_LEN);
716 s = (struct got_imsg_fetch_symref *)(imsg.data + off);
718 remain -= sizeof(*s);
719 if (remain < s->name_len) {
720 err = got_error(GOT_ERR_PRIVSEP_LEN);
723 name = strndup(imsg.data + off, s->name_len);
725 err = got_error_from_errno("strndup");
729 remain -= s->name_len;
730 if (remain < s->target_len) {
731 err = got_error(GOT_ERR_PRIVSEP_LEN);
735 target = strndup(imsg.data + off, s->target_len);
736 if (target == NULL) {
737 err = got_error_from_errno("strndup");
741 off += s->target_len;
742 remain -= s->target_len;
743 err = got_pathlist_append(symrefs, name, target);
751 case GOT_IMSG_FETCH_REF:
752 if (datalen <= SHA1_DIGEST_LENGTH) {
753 err = got_error(GOT_ERR_PRIVSEP_MSG);
756 *id = malloc(sizeof(**id));
758 err = got_error_from_errno("malloc");
761 memcpy((*id)->sha1, imsg.data, SHA1_DIGEST_LENGTH);
762 *refname = strndup(imsg.data + SHA1_DIGEST_LENGTH,
763 datalen - SHA1_DIGEST_LENGTH);
764 if (*refname == NULL) {
765 err = got_error_from_errno("strndup");
769 case GOT_IMSG_FETCH_SERVER_PROGRESS:
771 err = got_error(GOT_ERR_PRIVSEP_LEN);
774 *server_progress = strndup(imsg.data, datalen);
775 if (*server_progress == NULL) {
776 err = got_error_from_errno("strndup");
779 for (i = 0; i < datalen; i++) {
780 if (!isprint((unsigned char)(*server_progress)[i]) &&
781 !isspace((unsigned char)(*server_progress)[i])) {
782 err = got_error(GOT_ERR_PRIVSEP_MSG);
783 free(*server_progress);
784 *server_progress = NULL;
789 case GOT_IMSG_FETCH_DOWNLOAD_PROGRESS:
790 if (datalen < sizeof(*packfile_size)) {
791 err = got_error(GOT_ERR_PRIVSEP_MSG);
794 memcpy(packfile_size, imsg.data, sizeof(*packfile_size));
796 case GOT_IMSG_FETCH_DONE:
797 *id = malloc(sizeof(**id));
799 err = got_error_from_errno("malloc");
802 if (datalen != SHA1_DIGEST_LENGTH) {
803 err = got_error(GOT_ERR_PRIVSEP_MSG);
806 memcpy((*id)->sha1, imsg.data, SHA1_DIGEST_LENGTH);
810 err = got_error(GOT_ERR_PRIVSEP_MSG);
824 const struct got_error *
825 got_privsep_send_index_pack_req(struct imsgbuf *ibuf, uint8_t *pack_hash,
828 const struct got_error *err = NULL;
830 /* Keep in sync with struct got_imsg_index_pack_request */
831 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_REQUEST, 0, 0, fd,
832 pack_hash, SHA1_DIGEST_LENGTH) == -1) {
833 err = got_error_from_errno("imsg_compose INDEX_REQUEST");
837 return flush_imsg(ibuf);
840 const struct got_error *
841 got_privsep_send_index_pack_outfd(struct imsgbuf *ibuf, int fd)
843 return send_fd(ibuf, GOT_IMSG_IDXPACK_OUTFD, fd);
846 const struct got_error *
847 got_privsep_send_index_pack_progress(struct imsgbuf *ibuf, int nobj_total,
848 int nobj_indexed, int nobj_loose, int nobj_resolved)
850 struct got_imsg_index_pack_progress iprogress;
852 iprogress.nobj_total = nobj_total;
853 iprogress.nobj_indexed = nobj_indexed;
854 iprogress.nobj_loose = nobj_loose;
855 iprogress.nobj_resolved = nobj_resolved;
857 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_PROGRESS, 0, 0, -1,
858 &iprogress, sizeof(iprogress)) == -1)
859 return got_error_from_errno("imsg_compose IDXPACK_PROGRESS");
861 return flush_imsg(ibuf);
864 const struct got_error *
865 got_privsep_send_index_pack_done(struct imsgbuf *ibuf)
867 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_DONE, 0, 0, -1, NULL, 0) == -1)
868 return got_error_from_errno("imsg_compose FETCH");
869 return flush_imsg(ibuf);
872 const struct got_error *
873 got_privsep_recv_index_progress(int *done, int *nobj_total,
874 int *nobj_indexed, int *nobj_loose, int *nobj_resolved,
875 struct imsgbuf *ibuf)
877 const struct got_error *err = NULL;
879 struct got_imsg_index_pack_progress *iprogress;
887 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
891 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
892 switch (imsg.hdr.type) {
894 if (datalen < sizeof(struct got_imsg_error)) {
895 err = got_error(GOT_ERR_PRIVSEP_LEN);
898 err = recv_imsg_error(&imsg, datalen);
900 case GOT_IMSG_IDXPACK_PROGRESS:
901 if (datalen < sizeof(*iprogress)) {
902 err = got_error(GOT_ERR_PRIVSEP_LEN);
905 iprogress = (struct got_imsg_index_pack_progress *)imsg.data;
906 *nobj_total = iprogress->nobj_total;
907 *nobj_indexed = iprogress->nobj_indexed;
908 *nobj_loose = iprogress->nobj_loose;
909 *nobj_resolved = iprogress->nobj_resolved;
911 case GOT_IMSG_IDXPACK_DONE:
913 err = got_error(GOT_ERR_PRIVSEP_LEN);
919 err = got_error(GOT_ERR_PRIVSEP_MSG);
927 const struct got_error *
928 got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
929 struct imsgbuf *ibuf)
931 const struct got_error *err = NULL;
932 struct got_imsg_object *iobj;
933 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
935 if (datalen != sizeof(*iobj))
936 return got_error(GOT_ERR_PRIVSEP_LEN);
939 *obj = calloc(1, sizeof(**obj));
941 return got_error_from_errno("calloc");
943 memcpy((*obj)->id.sha1, iobj->id, SHA1_DIGEST_LENGTH);
944 (*obj)->type = iobj->type;
945 (*obj)->flags = iobj->flags;
946 (*obj)->hdrlen = iobj->hdrlen;
947 (*obj)->size = iobj->size;
948 /* path_packfile is handled by caller */
949 if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
950 (*obj)->pack_offset = iobj->pack_offset;
951 (*obj)->pack_idx = iobj->pack_idx;
957 const struct got_error *
958 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
960 const struct got_error *err = NULL;
962 const size_t min_datalen =
963 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
967 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
971 switch (imsg.hdr.type) {
972 case GOT_IMSG_OBJECT:
973 err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
976 err = got_error(GOT_ERR_PRIVSEP_MSG);
985 static const struct got_error *
986 send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
989 const struct got_error *err = NULL;
990 size_t offset, remain;
995 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
997 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
998 commit->logmsg + offset, n) == -1) {
999 err = got_error_from_errno("imsg_compose "
1004 err = flush_imsg(ibuf);
1015 const struct got_error *
1016 got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
1018 const struct got_error *err = NULL;
1019 struct got_imsg_commit_object *icommit;
1022 struct got_object_qid *qid;
1023 size_t author_len = strlen(commit->author);
1024 size_t committer_len = strlen(commit->committer);
1025 size_t logmsg_len = strlen(commit->logmsg);
1027 total = sizeof(*icommit) + author_len + committer_len +
1028 commit->nparents * SHA1_DIGEST_LENGTH;
1030 buf = malloc(total);
1032 return got_error_from_errno("malloc");
1034 icommit = (struct got_imsg_commit_object *)buf;
1035 memcpy(icommit->tree_id, commit->tree_id->sha1,
1036 sizeof(icommit->tree_id));
1037 icommit->author_len = author_len;
1038 icommit->author_time = commit->author_time;
1039 icommit->author_gmtoff = commit->author_gmtoff;
1040 icommit->committer_len = committer_len;
1041 icommit->committer_time = commit->committer_time;
1042 icommit->committer_gmtoff = commit->committer_gmtoff;
1043 icommit->logmsg_len = logmsg_len;
1044 icommit->nparents = commit->nparents;
1046 len = sizeof(*icommit);
1047 memcpy(buf + len, commit->author, author_len);
1049 memcpy(buf + len, commit->committer, committer_len);
1050 len += committer_len;
1051 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
1052 memcpy(buf + len, qid->id, SHA1_DIGEST_LENGTH);
1053 len += SHA1_DIGEST_LENGTH;
1056 if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
1057 err = got_error_from_errno("imsg_compose COMMIT");
1061 if (logmsg_len == 0 ||
1062 logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1063 err = flush_imsg(ibuf);
1067 err = send_commit_logmsg(ibuf, commit, logmsg_len);
1073 static const struct got_error *
1074 get_commit_from_imsg(struct got_commit_object **commit,
1075 struct imsg *imsg, size_t datalen, struct imsgbuf *ibuf)
1077 const struct got_error *err = NULL;
1078 struct got_imsg_commit_object *icommit;
1082 if (datalen < sizeof(*icommit))
1083 return got_error(GOT_ERR_PRIVSEP_LEN);
1085 icommit = imsg->data;
1086 if (datalen != sizeof(*icommit) + icommit->author_len +
1087 icommit->committer_len +
1088 icommit->nparents * SHA1_DIGEST_LENGTH)
1089 return got_error(GOT_ERR_PRIVSEP_LEN);
1091 if (icommit->nparents < 0)
1092 return got_error(GOT_ERR_PRIVSEP_LEN);
1094 len += sizeof(*icommit);
1096 *commit = got_object_commit_alloc_partial();
1097 if (*commit == NULL)
1098 return got_error_from_errno(
1099 "got_object_commit_alloc_partial");
1101 memcpy((*commit)->tree_id->sha1, icommit->tree_id,
1102 SHA1_DIGEST_LENGTH);
1103 (*commit)->author_time = icommit->author_time;
1104 (*commit)->author_gmtoff = icommit->author_gmtoff;
1105 (*commit)->committer_time = icommit->committer_time;
1106 (*commit)->committer_gmtoff = icommit->committer_gmtoff;
1108 if (icommit->author_len == 0) {
1109 (*commit)->author = strdup("");
1110 if ((*commit)->author == NULL) {
1111 err = got_error_from_errno("strdup");
1115 (*commit)->author = malloc(icommit->author_len + 1);
1116 if ((*commit)->author == NULL) {
1117 err = got_error_from_errno("malloc");
1120 memcpy((*commit)->author, imsg->data + len,
1121 icommit->author_len);
1122 (*commit)->author[icommit->author_len] = '\0';
1124 len += icommit->author_len;
1126 if (icommit->committer_len == 0) {
1127 (*commit)->committer = strdup("");
1128 if ((*commit)->committer == NULL) {
1129 err = got_error_from_errno("strdup");
1133 (*commit)->committer =
1134 malloc(icommit->committer_len + 1);
1135 if ((*commit)->committer == NULL) {
1136 err = got_error_from_errno("malloc");
1139 memcpy((*commit)->committer, imsg->data + len,
1140 icommit->committer_len);
1141 (*commit)->committer[icommit->committer_len] = '\0';
1143 len += icommit->committer_len;
1145 if (icommit->logmsg_len == 0) {
1146 (*commit)->logmsg = strdup("");
1147 if ((*commit)->logmsg == NULL) {
1148 err = got_error_from_errno("strdup");
1152 size_t offset = 0, remain = icommit->logmsg_len;
1154 (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
1155 if ((*commit)->logmsg == NULL) {
1156 err = got_error_from_errno("malloc");
1159 while (remain > 0) {
1160 struct imsg imsg_log;
1161 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1164 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1168 if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG) {
1169 err = got_error(GOT_ERR_PRIVSEP_MSG);
1173 memcpy((*commit)->logmsg + offset,
1175 imsg_free(&imsg_log);
1179 (*commit)->logmsg[icommit->logmsg_len] = '\0';
1182 for (i = 0; i < icommit->nparents; i++) {
1183 struct got_object_qid *qid;
1185 err = got_object_qid_alloc_partial(&qid);
1188 memcpy(qid->id, imsg->data + len +
1189 i * SHA1_DIGEST_LENGTH, sizeof(*qid->id));
1190 SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
1191 (*commit)->nparents++;
1195 got_object_commit_close(*commit);
1201 const struct got_error *
1202 got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
1204 const struct got_error *err = NULL;
1207 const size_t min_datalen =
1208 MIN(sizeof(struct got_imsg_error),
1209 sizeof(struct got_imsg_commit_object));
1213 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1217 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1219 switch (imsg.hdr.type) {
1220 case GOT_IMSG_COMMIT:
1221 err = get_commit_from_imsg(commit, &imsg, datalen, ibuf);
1224 err = got_error(GOT_ERR_PRIVSEP_MSG);
1233 const struct got_error *
1234 got_privsep_send_tree(struct imsgbuf *ibuf, struct got_pathlist_head *entries,
1237 const struct got_error *err = NULL;
1238 struct got_imsg_tree_object itree;
1239 struct got_pathlist_entry *pe;
1241 int nimsg; /* number of imsg queued in ibuf */
1243 itree.nentries = nentries;
1244 if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
1246 return got_error_from_errno("imsg_compose TREE");
1248 totlen = sizeof(itree);
1250 TAILQ_FOREACH(pe, entries, entry) {
1251 const char *name = pe->path;
1252 struct got_parsed_tree_entry *pte = pe->data;
1254 size_t namelen = strlen(name);
1255 size_t len = sizeof(struct got_imsg_tree_entry) + namelen;
1257 if (len > MAX_IMSGSIZE)
1258 return got_error(GOT_ERR_NO_SPACE);
1261 if (totlen + len >= MAX_IMSGSIZE - (IMSG_HEADER_SIZE * nimsg)) {
1262 err = flush_imsg(ibuf);
1268 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_ENTRY, 0, 0, len);
1270 return got_error_from_errno("imsg_create TREE_ENTRY");
1272 /* Keep in sync with struct got_imsg_tree_object definition! */
1273 if (imsg_add(wbuf, pte->id, SHA1_DIGEST_LENGTH) == -1) {
1274 err = got_error_from_errno("imsg_add TREE_ENTRY");
1278 if (imsg_add(wbuf, &pte->mode, sizeof(pte->mode)) == -1) {
1279 err = got_error_from_errno("imsg_add TREE_ENTRY");
1284 if (imsg_add(wbuf, name, namelen) == -1) {
1285 err = got_error_from_errno("imsg_add TREE_ENTRY");
1291 imsg_close(ibuf, wbuf);
1296 return flush_imsg(ibuf);
1299 const struct got_error *
1300 got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
1302 const struct got_error *err = NULL;
1303 const size_t min_datalen =
1304 MIN(sizeof(struct got_imsg_error),
1305 sizeof(struct got_imsg_tree_object));
1306 struct got_imsg_tree_object *itree;
1311 err = read_imsg(ibuf);
1319 struct got_imsg_tree_entry *ite;
1320 struct got_tree_entry *te = NULL;
1322 n = imsg_get(ibuf, &imsg);
1324 if (*tree && (*tree)->nentries != nentries)
1329 if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen)
1330 return got_error(GOT_ERR_PRIVSEP_LEN);
1332 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1334 switch (imsg.hdr.type) {
1335 case GOT_IMSG_ERROR:
1336 err = recv_imsg_error(&imsg, datalen);
1339 /* This message should only appear once. */
1340 if (*tree != NULL) {
1341 err = got_error(GOT_ERR_PRIVSEP_MSG);
1344 if (datalen != sizeof(*itree)) {
1345 err = got_error(GOT_ERR_PRIVSEP_LEN);
1349 *tree = malloc(sizeof(**tree));
1350 if (*tree == NULL) {
1351 err = got_error_from_errno("malloc");
1354 (*tree)->entries = calloc(itree->nentries,
1355 sizeof(struct got_tree_entry));
1356 if ((*tree)->entries == NULL) {
1357 err = got_error_from_errno("malloc");
1360 (*tree)->nentries = itree->nentries;
1361 (*tree)->refcnt = 0;
1363 case GOT_IMSG_TREE_ENTRY:
1364 /* This message should be preceeded by GOT_IMSG_TREE. */
1365 if (*tree == NULL) {
1366 err = got_error(GOT_ERR_PRIVSEP_MSG);
1369 if (datalen < sizeof(*ite) || datalen > MAX_IMSGSIZE) {
1370 err = got_error(GOT_ERR_PRIVSEP_LEN);
1374 /* Remaining data contains the entry's name. */
1375 datalen -= sizeof(*ite);
1376 if (datalen == 0 || datalen > MAX_IMSGSIZE) {
1377 err = got_error(GOT_ERR_PRIVSEP_LEN);
1382 if (datalen + 1 > sizeof(te->name)) {
1383 err = got_error(GOT_ERR_NO_SPACE);
1386 te = &(*tree)->entries[nentries];
1387 memcpy(te->name, imsg.data + sizeof(*ite), datalen);
1388 te->name[datalen] = '\0';
1390 memcpy(te->id.sha1, ite->id, SHA1_DIGEST_LENGTH);
1391 te->mode = ite->mode;
1396 err = got_error(GOT_ERR_PRIVSEP_MSG);
1403 if (*tree && (*tree)->nentries != nentries) {
1405 err = got_error(GOT_ERR_PRIVSEP_LEN);
1406 got_object_tree_close(*tree);
1413 const struct got_error *
1414 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
1415 const uint8_t *data)
1417 struct got_imsg_blob iblob;
1420 iblob.hdrlen = hdrlen;
1425 if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
1426 return got_error(GOT_ERR_NO_SPACE);
1428 buf = malloc(sizeof(iblob) + size);
1430 return got_error_from_errno("malloc");
1432 memcpy(buf, &iblob, sizeof(iblob));
1433 memcpy(buf + sizeof(iblob), data, size);
1434 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
1435 sizeof(iblob) + size) == -1) {
1437 return got_error_from_errno("imsg_compose BLOB");
1441 /* Data has already been written to file descriptor. */
1442 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
1443 sizeof(iblob)) == -1)
1444 return got_error_from_errno("imsg_compose BLOB");
1448 return flush_imsg(ibuf);
1451 const struct got_error *
1452 got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1453 struct imsgbuf *ibuf)
1455 const struct got_error *err = NULL;
1457 struct got_imsg_blob *iblob;
1462 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1466 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1468 switch (imsg.hdr.type) {
1470 if (datalen < sizeof(*iblob)) {
1471 err = got_error(GOT_ERR_PRIVSEP_LEN);
1475 *size = iblob->size;
1476 *hdrlen = iblob->hdrlen;
1478 if (datalen == sizeof(*iblob)) {
1479 /* Data has been written to file descriptor. */
1483 if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX) {
1484 err = got_error(GOT_ERR_PRIVSEP_LEN);
1488 *outbuf = malloc(*size);
1489 if (*outbuf == NULL) {
1490 err = got_error_from_errno("malloc");
1493 memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
1496 err = got_error(GOT_ERR_PRIVSEP_MSG);
1505 static const struct got_error *
1506 send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
1508 const struct got_error *err = NULL;
1509 size_t offset, remain;
1512 remain = tagmsg_len;
1513 while (remain > 0) {
1514 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1516 if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
1517 tag->tagmsg + offset, n) == -1) {
1518 err = got_error_from_errno("imsg_compose TAG_TAGMSG");
1522 err = flush_imsg(ibuf);
1533 const struct got_error *
1534 got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
1536 const struct got_error *err = NULL;
1537 struct got_imsg_tag_object *itag;
1540 size_t tag_len = strlen(tag->tag);
1541 size_t tagger_len = strlen(tag->tagger);
1542 size_t tagmsg_len = strlen(tag->tagmsg);
1544 total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
1546 buf = malloc(total);
1548 return got_error_from_errno("malloc");
1550 itag = (struct got_imsg_tag_object *)buf;
1551 memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
1552 itag->obj_type = tag->obj_type;
1553 itag->tag_len = tag_len;
1554 itag->tagger_len = tagger_len;
1555 itag->tagger_time = tag->tagger_time;
1556 itag->tagger_gmtoff = tag->tagger_gmtoff;
1557 itag->tagmsg_len = tagmsg_len;
1559 len = sizeof(*itag);
1560 memcpy(buf + len, tag->tag, tag_len);
1562 memcpy(buf + len, tag->tagger, tagger_len);
1565 if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
1566 err = got_error_from_errno("imsg_compose TAG");
1570 if (tagmsg_len == 0 ||
1571 tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1572 err = flush_imsg(ibuf);
1576 err = send_tagmsg(ibuf, tag, tagmsg_len);
1582 const struct got_error *
1583 got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
1585 const struct got_error *err = NULL;
1587 struct got_imsg_tag_object *itag;
1588 size_t len, datalen;
1589 const size_t min_datalen =
1590 MIN(sizeof(struct got_imsg_error),
1591 sizeof(struct got_imsg_tag_object));
1595 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1599 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1602 switch (imsg.hdr.type) {
1604 if (datalen < sizeof(*itag)) {
1605 err = got_error(GOT_ERR_PRIVSEP_LEN);
1609 if (datalen != sizeof(*itag) + itag->tag_len +
1611 err = got_error(GOT_ERR_PRIVSEP_LEN);
1614 len += sizeof(*itag);
1616 *tag = calloc(1, sizeof(**tag));
1618 err = got_error_from_errno("calloc");
1622 memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
1624 if (itag->tag_len == 0) {
1625 (*tag)->tag = strdup("");
1626 if ((*tag)->tag == NULL) {
1627 err = got_error_from_errno("strdup");
1631 (*tag)->tag = malloc(itag->tag_len + 1);
1632 if ((*tag)->tag == NULL) {
1633 err = got_error_from_errno("malloc");
1636 memcpy((*tag)->tag, imsg.data + len,
1638 (*tag)->tag[itag->tag_len] = '\0';
1640 len += itag->tag_len;
1642 (*tag)->obj_type = itag->obj_type;
1643 (*tag)->tagger_time = itag->tagger_time;
1644 (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1646 if (itag->tagger_len == 0) {
1647 (*tag)->tagger = strdup("");
1648 if ((*tag)->tagger == NULL) {
1649 err = got_error_from_errno("strdup");
1653 (*tag)->tagger = malloc(itag->tagger_len + 1);
1654 if ((*tag)->tagger == NULL) {
1655 err = got_error_from_errno("malloc");
1658 memcpy((*tag)->tagger, imsg.data + len,
1660 (*tag)->tagger[itag->tagger_len] = '\0';
1662 len += itag->tagger_len;
1664 if (itag->tagmsg_len == 0) {
1665 (*tag)->tagmsg = strdup("");
1666 if ((*tag)->tagmsg == NULL) {
1667 err = got_error_from_errno("strdup");
1671 size_t offset = 0, remain = itag->tagmsg_len;
1673 (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1674 if ((*tag)->tagmsg == NULL) {
1675 err = got_error_from_errno("malloc");
1678 while (remain > 0) {
1679 struct imsg imsg_log;
1680 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1683 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1687 if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1688 return got_error(GOT_ERR_PRIVSEP_MSG);
1690 memcpy((*tag)->tagmsg + offset, imsg_log.data,
1692 imsg_free(&imsg_log);
1696 (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1701 err = got_error(GOT_ERR_PRIVSEP_MSG);
1710 const struct got_error *
1711 got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1712 struct got_packidx *packidx)
1714 const struct got_error *err = NULL;
1715 struct got_imsg_packidx ipackidx;
1716 struct got_imsg_pack ipack;
1719 ipackidx.len = packidx->len;
1720 fd = dup(packidx->fd);
1722 return got_error_from_errno("dup");
1724 if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
1725 sizeof(ipackidx)) == -1) {
1726 err = got_error_from_errno("imsg_compose PACKIDX");
1731 if (strlcpy(ipack.path_packfile, pack->path_packfile,
1732 sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
1733 return got_error(GOT_ERR_NO_SPACE);
1734 ipack.filesize = pack->filesize;
1738 return got_error_from_errno("dup");
1740 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
1742 err = got_error_from_errno("imsg_compose PACK");
1747 return flush_imsg(ibuf);
1750 const struct got_error *
1751 got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
1752 struct got_object_id *id)
1754 struct got_imsg_packed_object iobj;
1757 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
1759 if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
1760 &iobj, sizeof(iobj)) == -1)
1761 return got_error_from_errno("imsg_compose "
1762 "PACKED_OBJECT_REQUEST");
1764 return flush_imsg(ibuf);
1767 const struct got_error *
1768 got_privsep_send_gitconfig_parse_req(struct imsgbuf *ibuf, int fd)
1770 const struct got_error *err = NULL;
1772 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_PARSE_REQUEST, 0, 0, fd,
1774 err = got_error_from_errno("imsg_compose "
1775 "GITCONFIG_PARSE_REQUEST");
1780 return flush_imsg(ibuf);
1783 const struct got_error *
1784 got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *ibuf)
1786 if (imsg_compose(ibuf,
1787 GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST, 0, 0, -1,
1789 return got_error_from_errno("imsg_compose "
1790 "GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST");
1792 return flush_imsg(ibuf);
1795 const struct got_error *
1796 got_privsep_send_gitconfig_author_name_req(struct imsgbuf *ibuf)
1798 if (imsg_compose(ibuf,
1799 GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST, 0, 0, -1, NULL, 0) == -1)
1800 return got_error_from_errno("imsg_compose "
1801 "GITCONFIG_AUTHOR_NAME_REQUEST");
1803 return flush_imsg(ibuf);
1806 const struct got_error *
1807 got_privsep_send_gitconfig_author_email_req(struct imsgbuf *ibuf)
1809 if (imsg_compose(ibuf,
1810 GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST, 0, 0, -1, NULL, 0) == -1)
1811 return got_error_from_errno("imsg_compose "
1812 "GITCONFIG_AUTHOR_EMAIL_REQUEST");
1814 return flush_imsg(ibuf);
1817 const struct got_error *
1818 got_privsep_send_gitconfig_remotes_req(struct imsgbuf *ibuf)
1820 if (imsg_compose(ibuf,
1821 GOT_IMSG_GITCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
1822 return got_error_from_errno("imsg_compose "
1823 "GITCONFIG_REMOTE_REQUEST");
1825 return flush_imsg(ibuf);
1828 const struct got_error *
1829 got_privsep_send_gitconfig_owner_req(struct imsgbuf *ibuf)
1831 if (imsg_compose(ibuf,
1832 GOT_IMSG_GITCONFIG_OWNER_REQUEST, 0, 0, -1, NULL, 0) == -1)
1833 return got_error_from_errno("imsg_compose "
1834 "GITCONFIG_OWNER_REQUEST");
1836 return flush_imsg(ibuf);
1839 const struct got_error *
1840 got_privsep_send_gitconfig_str(struct imsgbuf *ibuf, const char *value)
1842 size_t len = value ? strlen(value) + 1 : 0;
1844 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_STR_VAL, 0, 0, -1,
1846 return got_error_from_errno("imsg_compose GITCONFIG_STR_VAL");
1848 return flush_imsg(ibuf);
1851 const struct got_error *
1852 got_privsep_recv_gitconfig_str(char **str, struct imsgbuf *ibuf)
1854 const struct got_error *err = NULL;
1857 const size_t min_datalen = 0;
1861 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1864 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1866 switch (imsg.hdr.type) {
1867 case GOT_IMSG_GITCONFIG_STR_VAL:
1870 *str = malloc(datalen);
1872 err = got_error_from_errno("malloc");
1875 if (strlcpy(*str, imsg.data, datalen) >= datalen)
1876 err = got_error(GOT_ERR_NO_SPACE);
1879 err = got_error(GOT_ERR_PRIVSEP_MSG);
1887 const struct got_error *
1888 got_privsep_send_gitconfig_int(struct imsgbuf *ibuf, int value)
1890 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_INT_VAL, 0, 0, -1,
1891 &value, sizeof(value)) == -1)
1892 return got_error_from_errno("imsg_compose GITCONFIG_INT_VAL");
1894 return flush_imsg(ibuf);
1897 const struct got_error *
1898 got_privsep_recv_gitconfig_int(int *val, struct imsgbuf *ibuf)
1900 const struct got_error *err = NULL;
1903 const size_t min_datalen =
1904 MIN(sizeof(struct got_imsg_error), sizeof(int));
1908 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1911 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1913 switch (imsg.hdr.type) {
1914 case GOT_IMSG_GITCONFIG_INT_VAL:
1915 if (datalen != sizeof(*val)) {
1916 err = got_error(GOT_ERR_PRIVSEP_LEN);
1919 memcpy(val, imsg.data, sizeof(*val));
1922 err = got_error(GOT_ERR_PRIVSEP_MSG);
1930 const struct got_error *
1931 got_privsep_send_gitconfig_remotes(struct imsgbuf *ibuf,
1932 struct got_remote_repo *remotes, int nremotes)
1934 const struct got_error *err = NULL;
1935 struct got_imsg_remotes iremotes;
1938 iremotes.nremotes = nremotes;
1939 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_REMOTES, 0, 0, -1,
1940 &iremotes, sizeof(iremotes)) == -1)
1941 return got_error_from_errno("imsg_compose GITCONFIG_REMOTES");
1943 err = flush_imsg(ibuf);
1948 for (i = 0; i < nremotes; i++) {
1949 struct got_imsg_remote iremote;
1950 size_t len = sizeof(iremote);
1953 iremote.mirror_references = remotes[i].mirror_references;
1954 iremote.name_len = strlen(remotes[i].name);
1955 len += iremote.name_len;
1956 iremote.url_len = strlen(remotes[i].url);
1957 len += iremote.url_len;
1959 wbuf = imsg_create(ibuf, GOT_IMSG_GITCONFIG_REMOTE, 0, 0, len);
1961 return got_error_from_errno(
1962 "imsg_create GITCONFIG_REMOTE");
1964 if (imsg_add(wbuf, &iremote, sizeof(iremote)) == -1) {
1965 err = got_error_from_errno(
1966 "imsg_add GITCONFIG_REMOTE");
1971 if (imsg_add(wbuf, remotes[i].name, iremote.name_len) == -1) {
1972 err = got_error_from_errno(
1973 "imsg_add GITCONFIG_REMOTE");
1977 if (imsg_add(wbuf, remotes[i].url, iremote.url_len) == -1) {
1978 err = got_error_from_errno(
1979 "imsg_add GITCONFIG_REMOTE");
1985 imsg_close(ibuf, wbuf);
1986 err = flush_imsg(ibuf);
1994 const struct got_error *
1995 got_privsep_recv_gitconfig_remotes(struct got_remote_repo **remotes,
1996 int *nremotes, struct imsgbuf *ibuf)
1998 const struct got_error *err = NULL;
2001 struct got_imsg_remotes iremotes;
2002 struct got_imsg_remote iremote;
2006 iremotes.nremotes = 0;
2008 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremotes));
2011 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2013 switch (imsg.hdr.type) {
2014 case GOT_IMSG_GITCONFIG_REMOTES:
2015 if (datalen != sizeof(iremotes)) {
2016 err = got_error(GOT_ERR_PRIVSEP_LEN);
2019 memcpy(&iremotes, imsg.data, sizeof(iremotes));
2020 if (iremotes.nremotes == 0) {
2027 return got_error(GOT_ERR_PRIVSEP_MSG);
2032 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(**remotes));
2033 if (*remotes == NULL)
2034 return got_error_from_errno("recallocarray");
2036 while (*nremotes < iremotes.nremotes) {
2037 struct got_remote_repo *remote;
2039 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremote));
2042 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2044 switch (imsg.hdr.type) {
2045 case GOT_IMSG_GITCONFIG_REMOTE:
2046 remote = &(*remotes)[*nremotes];
2047 if (datalen < sizeof(iremote)) {
2048 err = got_error(GOT_ERR_PRIVSEP_LEN);
2051 memcpy(&iremote, imsg.data, sizeof(iremote));
2052 if (iremote.name_len == 0 || iremote.url_len == 0 ||
2053 (sizeof(iremote) + iremote.name_len +
2054 iremote.url_len) > datalen) {
2055 err = got_error(GOT_ERR_PRIVSEP_LEN);
2058 remote->name = strndup(imsg.data + sizeof(iremote),
2060 if (remote->name == NULL) {
2061 err = got_error_from_errno("strndup");
2064 remote->url = strndup(imsg.data + sizeof(iremote) +
2065 iremote.name_len, iremote.url_len);
2066 if (remote->url == NULL) {
2067 err = got_error_from_errno("strndup");
2071 remote->mirror_references = iremote.mirror_references;
2075 err = got_error(GOT_ERR_PRIVSEP_MSG);
2086 for (i = 0; i < *nremotes; i++) {
2087 free((*remotes)[i].name);
2088 free((*remotes)[i].url);
2097 const struct got_error *
2098 got_privsep_send_commit_traversal_request(struct imsgbuf *ibuf,
2099 struct got_object_id *id, int idx, const char *path)
2101 const struct got_error *err = NULL;
2103 size_t path_len = strlen(path) + 1;
2105 wbuf = imsg_create(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_REQUEST, 0, 0,
2106 sizeof(struct got_imsg_commit_traversal_request) + path_len);
2108 return got_error_from_errno(
2109 "imsg_create COMMIT_TRAVERSAL_REQUEST");
2110 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
2111 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
2115 if (imsg_add(wbuf, &idx, sizeof(idx)) == -1) {
2116 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
2120 if (imsg_add(wbuf, path, path_len) == -1) {
2121 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
2127 imsg_close(ibuf, wbuf);
2129 return flush_imsg(ibuf);
2132 const struct got_error *
2133 got_privsep_send_traversed_commits(struct got_object_id *commit_ids,
2134 size_t ncommits, struct imsgbuf *ibuf)
2136 const struct got_error *err;
2140 wbuf = imsg_create(ibuf, GOT_IMSG_TRAVERSED_COMMITS, 0, 0,
2141 sizeof(struct got_imsg_traversed_commits) +
2142 ncommits * SHA1_DIGEST_LENGTH);
2144 return got_error_from_errno("imsg_create TRAVERSED_COMMITS");
2146 if (imsg_add(wbuf, &ncommits, sizeof(ncommits)) == -1) {
2147 err = got_error_from_errno("imsg_add TRAVERSED_COMMITS");
2151 for (i = 0; i < ncommits; i++) {
2152 struct got_object_id *id = &commit_ids[i];
2153 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
2154 err = got_error_from_errno(
2155 "imsg_add TRAVERSED_COMMITS");
2162 imsg_close(ibuf, wbuf);
2164 return flush_imsg(ibuf);
2167 const struct got_error *
2168 got_privsep_recv_traversed_commits(struct got_commit_object **changed_commit,
2169 struct got_object_id **changed_commit_id,
2170 struct got_object_id_queue *commit_ids, struct imsgbuf *ibuf)
2172 const struct got_error *err = NULL;
2174 struct got_imsg_traversed_commits *icommits;
2178 *changed_commit = NULL;
2179 *changed_commit_id = NULL;
2182 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2186 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2187 switch (imsg.hdr.type) {
2188 case GOT_IMSG_TRAVERSED_COMMITS:
2189 icommits = imsg.data;
2190 if (datalen != sizeof(*icommits) +
2191 icommits->ncommits * SHA1_DIGEST_LENGTH) {
2192 err = got_error(GOT_ERR_PRIVSEP_LEN);
2195 for (i = 0; i < icommits->ncommits; i++) {
2196 struct got_object_qid *qid;
2197 uint8_t *sha1 = (uint8_t *)imsg.data +
2198 sizeof(*icommits) + i * SHA1_DIGEST_LENGTH;
2199 err = got_object_qid_alloc_partial(&qid);
2202 memcpy(qid->id->sha1, sha1, SHA1_DIGEST_LENGTH);
2203 SIMPLEQ_INSERT_TAIL(commit_ids, qid, entry);
2205 /* The last commit may contain a change. */
2206 if (i == icommits->ncommits - 1) {
2207 *changed_commit_id =
2208 got_object_id_dup(qid->id);
2209 if (*changed_commit_id == NULL) {
2210 err = got_error_from_errno(
2211 "got_object_id_dup");
2217 case GOT_IMSG_COMMIT:
2218 if (*changed_commit_id == NULL) {
2219 err = got_error(GOT_ERR_PRIVSEP_MSG);
2222 err = get_commit_from_imsg(changed_commit, &imsg,
2225 case GOT_IMSG_COMMIT_TRAVERSAL_DONE:
2229 err = got_error(GOT_ERR_PRIVSEP_MSG);
2239 got_object_id_queue_free(commit_ids);
2243 const struct got_error *
2244 got_privsep_send_commit_traversal_done(struct imsgbuf *ibuf)
2246 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_DONE, 0, 0, -1,
2248 return got_error_from_errno("imsg_compose TRAVERSAL_DONE");
2250 return flush_imsg(ibuf);
2253 const struct got_error *
2254 got_privsep_unveil_exec_helpers(void)
2256 const char *helpers[] = {
2257 GOT_PATH_PROG_READ_PACK,
2258 GOT_PATH_PROG_READ_OBJECT,
2259 GOT_PATH_PROG_READ_COMMIT,
2260 GOT_PATH_PROG_READ_TREE,
2261 GOT_PATH_PROG_READ_BLOB,
2262 GOT_PATH_PROG_READ_TAG,
2263 GOT_PATH_PROG_READ_GITCONFIG,
2264 GOT_PATH_PROG_FETCH_PACK,
2265 GOT_PATH_PROG_INDEX_PACK,
2269 for (i = 0; i < nitems(helpers); i++) {
2270 if (unveil(helpers[i], "x") == 0)
2272 return got_error_from_errno2("unveil", helpers[i]);
2279 got_privsep_exec_child(int imsg_fds[2], const char *path, const char *repo_path)
2281 if (close(imsg_fds[0]) != 0) {
2282 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2286 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
2287 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2290 if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
2291 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2295 if (execl(path, path, repo_path, (char *)NULL) == -1) {
2296 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,