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,
417 struct got_pathlist_head *wanted_refs, int list_refs_only, int verbosity)
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 TAILQ_FOREACH(pe, wanted_refs, entry)
434 fetchreq.n_wanted_refs++;
435 len = sizeof(struct got_imsg_fetch_request);
436 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
438 return got_error(GOT_ERR_NO_SPACE);
441 if (imsg_compose(ibuf, GOT_IMSG_FETCH_REQUEST, 0, 0, fd,
442 &fetchreq, sizeof(fetchreq)) == -1)
443 return got_error_from_errno(
444 "imsg_compose FETCH_SERVER_PROGRESS");
446 err = flush_imsg(ibuf);
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 len = sizeof(struct got_imsg_fetch_have_ref) + name_len;
459 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_HAVE_REF, 0, 0, len);
461 return got_error_from_errno("imsg_create FETCH_HAVE_REF");
463 /* Keep in sync with struct got_imsg_fetch_have_ref! */
464 if (imsg_add(wbuf, id->sha1, sizeof(id->sha1)) == -1) {
465 err = got_error_from_errno("imsg_add FETCH_HAVE_REF");
469 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
470 err = got_error_from_errno("imsg_add FETCH_HAVE_REF");
474 if (imsg_add(wbuf, name, name_len) == -1) {
475 err = got_error_from_errno("imsg_add FETCH_HAVE_REF");
481 imsg_close(ibuf, wbuf);
482 err = flush_imsg(ibuf);
487 TAILQ_FOREACH(pe, wanted_branches, entry) {
488 const char *name = pe->path;
489 size_t name_len = pe->path_len;
491 len = sizeof(struct got_imsg_fetch_wanted_branch) + name_len;
492 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_WANTED_BRANCH, 0, 0,
495 return got_error_from_errno(
496 "imsg_create FETCH_WANTED_BRANCH");
498 /* Keep in sync with struct got_imsg_fetch_wanted_branch! */
499 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
500 err = got_error_from_errno(
501 "imsg_add FETCH_WANTED_BRANCH");
505 if (imsg_add(wbuf, name, name_len) == -1) {
506 err = got_error_from_errno(
507 "imsg_add FETCH_WANTED_BRANCH");
513 imsg_close(ibuf, wbuf);
514 err = flush_imsg(ibuf);
519 TAILQ_FOREACH(pe, wanted_refs, entry) {
520 const char *name = pe->path;
521 size_t name_len = pe->path_len;
523 len = sizeof(struct got_imsg_fetch_wanted_ref) + name_len;
524 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_WANTED_REF, 0, 0,
527 return got_error_from_errno(
528 "imsg_create FETCH_WANTED_REF");
530 /* Keep in sync with struct got_imsg_fetch_wanted_ref! */
531 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
532 err = got_error_from_errno(
533 "imsg_add FETCH_WANTED_REF");
537 if (imsg_add(wbuf, name, name_len) == -1) {
538 err = got_error_from_errno(
539 "imsg_add FETCH_WANTED_REF");
545 imsg_close(ibuf, wbuf);
546 err = flush_imsg(ibuf);
556 const struct got_error *
557 got_privsep_send_fetch_outfd(struct imsgbuf *ibuf, int fd)
559 return send_fd(ibuf, GOT_IMSG_FETCH_OUTFD, fd);
563 const struct got_error *
564 got_privsep_send_fetch_symrefs(struct imsgbuf *ibuf,
565 struct got_pathlist_head *symrefs)
567 const struct got_error *err = NULL;
569 size_t len, nsymrefs = 0;
570 struct got_pathlist_entry *pe;
572 len = sizeof(struct got_imsg_fetch_symrefs);
573 TAILQ_FOREACH(pe, symrefs, entry) {
574 const char *target = pe->data;
575 len += sizeof(struct got_imsg_fetch_symref) +
576 pe->path_len + strlen(target);
580 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
581 return got_error(GOT_ERR_NO_SPACE);
583 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_SYMREFS, 0, 0, len);
585 return got_error_from_errno("imsg_create FETCH_SYMREFS");
587 /* Keep in sync with struct got_imsg_fetch_symrefs definition! */
588 if (imsg_add(wbuf, &nsymrefs, sizeof(nsymrefs)) == -1) {
589 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
594 TAILQ_FOREACH(pe, symrefs, entry) {
595 const char *name = pe->path;
596 size_t name_len = pe->path_len;
597 const char *target = pe->data;
598 size_t target_len = strlen(target);
600 /* Keep in sync with struct got_imsg_fetch_symref definition! */
601 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
602 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
606 if (imsg_add(wbuf, &target_len, sizeof(target_len)) == -1) {
607 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
611 if (imsg_add(wbuf, name, name_len) == -1) {
612 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
616 if (imsg_add(wbuf, target, target_len) == -1) {
617 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
624 imsg_close(ibuf, wbuf);
625 return flush_imsg(ibuf);
628 const struct got_error *
629 got_privsep_send_fetch_ref(struct imsgbuf *ibuf,
630 struct got_object_id *refid, const char *refname)
632 const struct got_error *err = NULL;
634 size_t len, reflen = strlen(refname);
636 len = sizeof(struct got_imsg_fetch_ref) + reflen;
637 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
638 return got_error(GOT_ERR_NO_SPACE);
640 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_REF, 0, 0, len);
642 return got_error_from_errno("imsg_create FETCH_REF");
644 /* Keep in sync with struct got_imsg_fetch_ref definition! */
645 if (imsg_add(wbuf, refid->sha1, SHA1_DIGEST_LENGTH) == -1) {
646 err = got_error_from_errno("imsg_add FETCH_REF");
650 if (imsg_add(wbuf, refname, reflen) == -1) {
651 err = got_error_from_errno("imsg_add FETCH_REF");
657 imsg_close(ibuf, wbuf);
658 return flush_imsg(ibuf);
661 const struct got_error *
662 got_privsep_send_fetch_server_progress(struct imsgbuf *ibuf, const char *msg,
665 if (msglen > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
666 return got_error(GOT_ERR_NO_SPACE);
671 if (imsg_compose(ibuf, GOT_IMSG_FETCH_SERVER_PROGRESS, 0, 0, -1,
673 return got_error_from_errno(
674 "imsg_compose FETCH_SERVER_PROGRESS");
676 return flush_imsg(ibuf);
679 const struct got_error *
680 got_privsep_send_fetch_download_progress(struct imsgbuf *ibuf, off_t bytes)
682 if (imsg_compose(ibuf, GOT_IMSG_FETCH_DOWNLOAD_PROGRESS, 0, 0, -1,
683 &bytes, sizeof(bytes)) == -1)
684 return got_error_from_errno(
685 "imsg_compose FETCH_DOWNLOAD_PROGRESS");
687 return flush_imsg(ibuf);
690 const struct got_error *
691 got_privsep_send_fetch_done(struct imsgbuf *ibuf, struct got_object_id hash)
693 if (imsg_compose(ibuf, GOT_IMSG_FETCH_DONE, 0, 0, -1,
694 hash.sha1, SHA1_DIGEST_LENGTH) == -1)
695 return got_error_from_errno("imsg_compose FETCH");
696 return flush_imsg(ibuf);
700 const struct got_error *
701 got_privsep_recv_fetch_progress(int *done, struct got_object_id **id,
702 char **refname, struct got_pathlist_head *symrefs, char **server_progress,
703 off_t *packfile_size, struct imsgbuf *ibuf)
705 const struct got_error *err = NULL;
708 struct got_imsg_fetch_symrefs *isymrefs = NULL;
716 *server_progress = NULL;
719 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
723 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
724 switch (imsg.hdr.type) {
726 if (datalen < sizeof(struct got_imsg_error)) {
727 err = got_error(GOT_ERR_PRIVSEP_LEN);
730 err = recv_imsg_error(&imsg, datalen);
732 case GOT_IMSG_FETCH_SYMREFS:
733 if (datalen < sizeof(*isymrefs)) {
734 err = got_error(GOT_ERR_PRIVSEP_LEN);
737 if (isymrefs != NULL) {
738 err = got_error(GOT_ERR_PRIVSEP_MSG);
741 isymrefs = (struct got_imsg_fetch_symrefs *)imsg.data;
742 off = sizeof(*isymrefs);
743 remain = datalen - off;
744 for (n = 0; n < isymrefs->nsymrefs; n++) {
745 struct got_imsg_fetch_symref *s;
747 if (remain < sizeof(struct got_imsg_fetch_symref)) {
748 err = got_error(GOT_ERR_PRIVSEP_LEN);
751 s = (struct got_imsg_fetch_symref *)(imsg.data + off);
753 remain -= sizeof(*s);
754 if (remain < s->name_len) {
755 err = got_error(GOT_ERR_PRIVSEP_LEN);
758 name = strndup(imsg.data + off, s->name_len);
760 err = got_error_from_errno("strndup");
764 remain -= s->name_len;
765 if (remain < s->target_len) {
766 err = got_error(GOT_ERR_PRIVSEP_LEN);
770 target = strndup(imsg.data + off, s->target_len);
771 if (target == NULL) {
772 err = got_error_from_errno("strndup");
776 off += s->target_len;
777 remain -= s->target_len;
778 err = got_pathlist_append(symrefs, name, target);
786 case GOT_IMSG_FETCH_REF:
787 if (datalen <= SHA1_DIGEST_LENGTH) {
788 err = got_error(GOT_ERR_PRIVSEP_MSG);
791 *id = malloc(sizeof(**id));
793 err = got_error_from_errno("malloc");
796 memcpy((*id)->sha1, imsg.data, SHA1_DIGEST_LENGTH);
797 *refname = strndup(imsg.data + SHA1_DIGEST_LENGTH,
798 datalen - SHA1_DIGEST_LENGTH);
799 if (*refname == NULL) {
800 err = got_error_from_errno("strndup");
804 case GOT_IMSG_FETCH_SERVER_PROGRESS:
806 err = got_error(GOT_ERR_PRIVSEP_LEN);
809 *server_progress = strndup(imsg.data, datalen);
810 if (*server_progress == NULL) {
811 err = got_error_from_errno("strndup");
814 for (i = 0; i < datalen; i++) {
815 if (!isprint((unsigned char)(*server_progress)[i]) &&
816 !isspace((unsigned char)(*server_progress)[i])) {
817 err = got_error(GOT_ERR_PRIVSEP_MSG);
818 free(*server_progress);
819 *server_progress = NULL;
824 case GOT_IMSG_FETCH_DOWNLOAD_PROGRESS:
825 if (datalen < sizeof(*packfile_size)) {
826 err = got_error(GOT_ERR_PRIVSEP_MSG);
829 memcpy(packfile_size, imsg.data, sizeof(*packfile_size));
831 case GOT_IMSG_FETCH_DONE:
832 *id = malloc(sizeof(**id));
834 err = got_error_from_errno("malloc");
837 if (datalen != SHA1_DIGEST_LENGTH) {
838 err = got_error(GOT_ERR_PRIVSEP_MSG);
841 memcpy((*id)->sha1, imsg.data, SHA1_DIGEST_LENGTH);
845 err = got_error(GOT_ERR_PRIVSEP_MSG);
859 const struct got_error *
860 got_privsep_send_index_pack_req(struct imsgbuf *ibuf, uint8_t *pack_hash,
863 const struct got_error *err = NULL;
865 /* Keep in sync with struct got_imsg_index_pack_request */
866 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_REQUEST, 0, 0, fd,
867 pack_hash, SHA1_DIGEST_LENGTH) == -1) {
868 err = got_error_from_errno("imsg_compose INDEX_REQUEST");
872 return flush_imsg(ibuf);
875 const struct got_error *
876 got_privsep_send_index_pack_outfd(struct imsgbuf *ibuf, int fd)
878 return send_fd(ibuf, GOT_IMSG_IDXPACK_OUTFD, fd);
881 const struct got_error *
882 got_privsep_send_index_pack_progress(struct imsgbuf *ibuf, int nobj_total,
883 int nobj_indexed, int nobj_loose, int nobj_resolved)
885 struct got_imsg_index_pack_progress iprogress;
887 iprogress.nobj_total = nobj_total;
888 iprogress.nobj_indexed = nobj_indexed;
889 iprogress.nobj_loose = nobj_loose;
890 iprogress.nobj_resolved = nobj_resolved;
892 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_PROGRESS, 0, 0, -1,
893 &iprogress, sizeof(iprogress)) == -1)
894 return got_error_from_errno("imsg_compose IDXPACK_PROGRESS");
896 return flush_imsg(ibuf);
899 const struct got_error *
900 got_privsep_send_index_pack_done(struct imsgbuf *ibuf)
902 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_DONE, 0, 0, -1, NULL, 0) == -1)
903 return got_error_from_errno("imsg_compose FETCH");
904 return flush_imsg(ibuf);
907 const struct got_error *
908 got_privsep_recv_index_progress(int *done, int *nobj_total,
909 int *nobj_indexed, int *nobj_loose, int *nobj_resolved,
910 struct imsgbuf *ibuf)
912 const struct got_error *err = NULL;
914 struct got_imsg_index_pack_progress *iprogress;
922 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
926 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
927 switch (imsg.hdr.type) {
929 if (datalen < sizeof(struct got_imsg_error)) {
930 err = got_error(GOT_ERR_PRIVSEP_LEN);
933 err = recv_imsg_error(&imsg, datalen);
935 case GOT_IMSG_IDXPACK_PROGRESS:
936 if (datalen < sizeof(*iprogress)) {
937 err = got_error(GOT_ERR_PRIVSEP_LEN);
940 iprogress = (struct got_imsg_index_pack_progress *)imsg.data;
941 *nobj_total = iprogress->nobj_total;
942 *nobj_indexed = iprogress->nobj_indexed;
943 *nobj_loose = iprogress->nobj_loose;
944 *nobj_resolved = iprogress->nobj_resolved;
946 case GOT_IMSG_IDXPACK_DONE:
948 err = got_error(GOT_ERR_PRIVSEP_LEN);
954 err = got_error(GOT_ERR_PRIVSEP_MSG);
962 const struct got_error *
963 got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
964 struct imsgbuf *ibuf)
966 const struct got_error *err = NULL;
967 struct got_imsg_object *iobj;
968 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
970 if (datalen != sizeof(*iobj))
971 return got_error(GOT_ERR_PRIVSEP_LEN);
974 *obj = calloc(1, sizeof(**obj));
976 return got_error_from_errno("calloc");
978 memcpy((*obj)->id.sha1, iobj->id, SHA1_DIGEST_LENGTH);
979 (*obj)->type = iobj->type;
980 (*obj)->flags = iobj->flags;
981 (*obj)->hdrlen = iobj->hdrlen;
982 (*obj)->size = iobj->size;
983 /* path_packfile is handled by caller */
984 if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
985 (*obj)->pack_offset = iobj->pack_offset;
986 (*obj)->pack_idx = iobj->pack_idx;
992 const struct got_error *
993 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
995 const struct got_error *err = NULL;
997 const size_t min_datalen =
998 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
1002 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1006 switch (imsg.hdr.type) {
1007 case GOT_IMSG_OBJECT:
1008 err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
1011 err = got_error(GOT_ERR_PRIVSEP_MSG);
1020 static const struct got_error *
1021 send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
1024 const struct got_error *err = NULL;
1025 size_t offset, remain;
1028 remain = logmsg_len;
1029 while (remain > 0) {
1030 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1032 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
1033 commit->logmsg + offset, n) == -1) {
1034 err = got_error_from_errno("imsg_compose "
1039 err = flush_imsg(ibuf);
1050 const struct got_error *
1051 got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
1053 const struct got_error *err = NULL;
1054 struct got_imsg_commit_object *icommit;
1057 struct got_object_qid *qid;
1058 size_t author_len = strlen(commit->author);
1059 size_t committer_len = strlen(commit->committer);
1060 size_t logmsg_len = strlen(commit->logmsg);
1062 total = sizeof(*icommit) + author_len + committer_len +
1063 commit->nparents * SHA1_DIGEST_LENGTH;
1065 buf = malloc(total);
1067 return got_error_from_errno("malloc");
1069 icommit = (struct got_imsg_commit_object *)buf;
1070 memcpy(icommit->tree_id, commit->tree_id->sha1,
1071 sizeof(icommit->tree_id));
1072 icommit->author_len = author_len;
1073 icommit->author_time = commit->author_time;
1074 icommit->author_gmtoff = commit->author_gmtoff;
1075 icommit->committer_len = committer_len;
1076 icommit->committer_time = commit->committer_time;
1077 icommit->committer_gmtoff = commit->committer_gmtoff;
1078 icommit->logmsg_len = logmsg_len;
1079 icommit->nparents = commit->nparents;
1081 len = sizeof(*icommit);
1082 memcpy(buf + len, commit->author, author_len);
1084 memcpy(buf + len, commit->committer, committer_len);
1085 len += committer_len;
1086 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
1087 memcpy(buf + len, qid->id, SHA1_DIGEST_LENGTH);
1088 len += SHA1_DIGEST_LENGTH;
1091 if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
1092 err = got_error_from_errno("imsg_compose COMMIT");
1096 if (logmsg_len == 0 ||
1097 logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1098 err = flush_imsg(ibuf);
1102 err = send_commit_logmsg(ibuf, commit, logmsg_len);
1108 static const struct got_error *
1109 get_commit_from_imsg(struct got_commit_object **commit,
1110 struct imsg *imsg, size_t datalen, struct imsgbuf *ibuf)
1112 const struct got_error *err = NULL;
1113 struct got_imsg_commit_object *icommit;
1117 if (datalen < sizeof(*icommit))
1118 return got_error(GOT_ERR_PRIVSEP_LEN);
1120 icommit = imsg->data;
1121 if (datalen != sizeof(*icommit) + icommit->author_len +
1122 icommit->committer_len +
1123 icommit->nparents * SHA1_DIGEST_LENGTH)
1124 return got_error(GOT_ERR_PRIVSEP_LEN);
1126 if (icommit->nparents < 0)
1127 return got_error(GOT_ERR_PRIVSEP_LEN);
1129 len += sizeof(*icommit);
1131 *commit = got_object_commit_alloc_partial();
1132 if (*commit == NULL)
1133 return got_error_from_errno(
1134 "got_object_commit_alloc_partial");
1136 memcpy((*commit)->tree_id->sha1, icommit->tree_id,
1137 SHA1_DIGEST_LENGTH);
1138 (*commit)->author_time = icommit->author_time;
1139 (*commit)->author_gmtoff = icommit->author_gmtoff;
1140 (*commit)->committer_time = icommit->committer_time;
1141 (*commit)->committer_gmtoff = icommit->committer_gmtoff;
1143 if (icommit->author_len == 0) {
1144 (*commit)->author = strdup("");
1145 if ((*commit)->author == NULL) {
1146 err = got_error_from_errno("strdup");
1150 (*commit)->author = malloc(icommit->author_len + 1);
1151 if ((*commit)->author == NULL) {
1152 err = got_error_from_errno("malloc");
1155 memcpy((*commit)->author, imsg->data + len,
1156 icommit->author_len);
1157 (*commit)->author[icommit->author_len] = '\0';
1159 len += icommit->author_len;
1161 if (icommit->committer_len == 0) {
1162 (*commit)->committer = strdup("");
1163 if ((*commit)->committer == NULL) {
1164 err = got_error_from_errno("strdup");
1168 (*commit)->committer =
1169 malloc(icommit->committer_len + 1);
1170 if ((*commit)->committer == NULL) {
1171 err = got_error_from_errno("malloc");
1174 memcpy((*commit)->committer, imsg->data + len,
1175 icommit->committer_len);
1176 (*commit)->committer[icommit->committer_len] = '\0';
1178 len += icommit->committer_len;
1180 if (icommit->logmsg_len == 0) {
1181 (*commit)->logmsg = strdup("");
1182 if ((*commit)->logmsg == NULL) {
1183 err = got_error_from_errno("strdup");
1187 size_t offset = 0, remain = icommit->logmsg_len;
1189 (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
1190 if ((*commit)->logmsg == NULL) {
1191 err = got_error_from_errno("malloc");
1194 while (remain > 0) {
1195 struct imsg imsg_log;
1196 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1199 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1203 if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG) {
1204 err = got_error(GOT_ERR_PRIVSEP_MSG);
1208 memcpy((*commit)->logmsg + offset,
1210 imsg_free(&imsg_log);
1214 (*commit)->logmsg[icommit->logmsg_len] = '\0';
1217 for (i = 0; i < icommit->nparents; i++) {
1218 struct got_object_qid *qid;
1220 err = got_object_qid_alloc_partial(&qid);
1223 memcpy(qid->id, imsg->data + len +
1224 i * SHA1_DIGEST_LENGTH, sizeof(*qid->id));
1225 SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
1226 (*commit)->nparents++;
1230 got_object_commit_close(*commit);
1236 const struct got_error *
1237 got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
1239 const struct got_error *err = NULL;
1242 const size_t min_datalen =
1243 MIN(sizeof(struct got_imsg_error),
1244 sizeof(struct got_imsg_commit_object));
1248 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1252 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1254 switch (imsg.hdr.type) {
1255 case GOT_IMSG_COMMIT:
1256 err = get_commit_from_imsg(commit, &imsg, datalen, ibuf);
1259 err = got_error(GOT_ERR_PRIVSEP_MSG);
1268 const struct got_error *
1269 got_privsep_send_tree(struct imsgbuf *ibuf, struct got_pathlist_head *entries,
1272 const struct got_error *err = NULL;
1273 struct got_imsg_tree_object itree;
1274 struct got_pathlist_entry *pe;
1276 int nimsg; /* number of imsg queued in ibuf */
1278 itree.nentries = nentries;
1279 if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
1281 return got_error_from_errno("imsg_compose TREE");
1283 totlen = sizeof(itree);
1285 TAILQ_FOREACH(pe, entries, entry) {
1286 const char *name = pe->path;
1287 struct got_parsed_tree_entry *pte = pe->data;
1289 size_t namelen = strlen(name);
1290 size_t len = sizeof(struct got_imsg_tree_entry) + namelen;
1292 if (len > MAX_IMSGSIZE)
1293 return got_error(GOT_ERR_NO_SPACE);
1296 if (totlen + len >= MAX_IMSGSIZE - (IMSG_HEADER_SIZE * nimsg)) {
1297 err = flush_imsg(ibuf);
1303 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_ENTRY, 0, 0, len);
1305 return got_error_from_errno("imsg_create TREE_ENTRY");
1307 /* Keep in sync with struct got_imsg_tree_object definition! */
1308 if (imsg_add(wbuf, pte->id, SHA1_DIGEST_LENGTH) == -1) {
1309 err = got_error_from_errno("imsg_add TREE_ENTRY");
1313 if (imsg_add(wbuf, &pte->mode, sizeof(pte->mode)) == -1) {
1314 err = got_error_from_errno("imsg_add TREE_ENTRY");
1319 if (imsg_add(wbuf, name, namelen) == -1) {
1320 err = got_error_from_errno("imsg_add TREE_ENTRY");
1326 imsg_close(ibuf, wbuf);
1331 return flush_imsg(ibuf);
1334 const struct got_error *
1335 got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
1337 const struct got_error *err = NULL;
1338 const size_t min_datalen =
1339 MIN(sizeof(struct got_imsg_error),
1340 sizeof(struct got_imsg_tree_object));
1341 struct got_imsg_tree_object *itree;
1346 err = read_imsg(ibuf);
1354 struct got_imsg_tree_entry *ite;
1355 struct got_tree_entry *te = NULL;
1357 n = imsg_get(ibuf, &imsg);
1359 if (*tree && (*tree)->nentries != nentries)
1364 if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen)
1365 return got_error(GOT_ERR_PRIVSEP_LEN);
1367 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1369 switch (imsg.hdr.type) {
1370 case GOT_IMSG_ERROR:
1371 err = recv_imsg_error(&imsg, datalen);
1374 /* This message should only appear once. */
1375 if (*tree != NULL) {
1376 err = got_error(GOT_ERR_PRIVSEP_MSG);
1379 if (datalen != sizeof(*itree)) {
1380 err = got_error(GOT_ERR_PRIVSEP_LEN);
1384 *tree = malloc(sizeof(**tree));
1385 if (*tree == NULL) {
1386 err = got_error_from_errno("malloc");
1389 (*tree)->entries = calloc(itree->nentries,
1390 sizeof(struct got_tree_entry));
1391 if ((*tree)->entries == NULL) {
1392 err = got_error_from_errno("malloc");
1395 (*tree)->nentries = itree->nentries;
1396 (*tree)->refcnt = 0;
1398 case GOT_IMSG_TREE_ENTRY:
1399 /* This message should be preceeded by GOT_IMSG_TREE. */
1400 if (*tree == NULL) {
1401 err = got_error(GOT_ERR_PRIVSEP_MSG);
1404 if (datalen < sizeof(*ite) || datalen > MAX_IMSGSIZE) {
1405 err = got_error(GOT_ERR_PRIVSEP_LEN);
1409 /* Remaining data contains the entry's name. */
1410 datalen -= sizeof(*ite);
1411 if (datalen == 0 || datalen > MAX_IMSGSIZE) {
1412 err = got_error(GOT_ERR_PRIVSEP_LEN);
1417 if (datalen + 1 > sizeof(te->name)) {
1418 err = got_error(GOT_ERR_NO_SPACE);
1421 te = &(*tree)->entries[nentries];
1422 memcpy(te->name, imsg.data + sizeof(*ite), datalen);
1423 te->name[datalen] = '\0';
1425 memcpy(te->id.sha1, ite->id, SHA1_DIGEST_LENGTH);
1426 te->mode = ite->mode;
1431 err = got_error(GOT_ERR_PRIVSEP_MSG);
1438 if (*tree && (*tree)->nentries != nentries) {
1440 err = got_error(GOT_ERR_PRIVSEP_LEN);
1441 got_object_tree_close(*tree);
1448 const struct got_error *
1449 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
1450 const uint8_t *data)
1452 struct got_imsg_blob iblob;
1455 iblob.hdrlen = hdrlen;
1460 if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
1461 return got_error(GOT_ERR_NO_SPACE);
1463 buf = malloc(sizeof(iblob) + size);
1465 return got_error_from_errno("malloc");
1467 memcpy(buf, &iblob, sizeof(iblob));
1468 memcpy(buf + sizeof(iblob), data, size);
1469 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
1470 sizeof(iblob) + size) == -1) {
1472 return got_error_from_errno("imsg_compose BLOB");
1476 /* Data has already been written to file descriptor. */
1477 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
1478 sizeof(iblob)) == -1)
1479 return got_error_from_errno("imsg_compose BLOB");
1483 return flush_imsg(ibuf);
1486 const struct got_error *
1487 got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1488 struct imsgbuf *ibuf)
1490 const struct got_error *err = NULL;
1492 struct got_imsg_blob *iblob;
1497 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1501 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1503 switch (imsg.hdr.type) {
1505 if (datalen < sizeof(*iblob)) {
1506 err = got_error(GOT_ERR_PRIVSEP_LEN);
1510 *size = iblob->size;
1511 *hdrlen = iblob->hdrlen;
1513 if (datalen == sizeof(*iblob)) {
1514 /* Data has been written to file descriptor. */
1518 if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX) {
1519 err = got_error(GOT_ERR_PRIVSEP_LEN);
1523 *outbuf = malloc(*size);
1524 if (*outbuf == NULL) {
1525 err = got_error_from_errno("malloc");
1528 memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
1531 err = got_error(GOT_ERR_PRIVSEP_MSG);
1540 static const struct got_error *
1541 send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
1543 const struct got_error *err = NULL;
1544 size_t offset, remain;
1547 remain = tagmsg_len;
1548 while (remain > 0) {
1549 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1551 if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
1552 tag->tagmsg + offset, n) == -1) {
1553 err = got_error_from_errno("imsg_compose TAG_TAGMSG");
1557 err = flush_imsg(ibuf);
1568 const struct got_error *
1569 got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
1571 const struct got_error *err = NULL;
1572 struct got_imsg_tag_object *itag;
1575 size_t tag_len = strlen(tag->tag);
1576 size_t tagger_len = strlen(tag->tagger);
1577 size_t tagmsg_len = strlen(tag->tagmsg);
1579 total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
1581 buf = malloc(total);
1583 return got_error_from_errno("malloc");
1585 itag = (struct got_imsg_tag_object *)buf;
1586 memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
1587 itag->obj_type = tag->obj_type;
1588 itag->tag_len = tag_len;
1589 itag->tagger_len = tagger_len;
1590 itag->tagger_time = tag->tagger_time;
1591 itag->tagger_gmtoff = tag->tagger_gmtoff;
1592 itag->tagmsg_len = tagmsg_len;
1594 len = sizeof(*itag);
1595 memcpy(buf + len, tag->tag, tag_len);
1597 memcpy(buf + len, tag->tagger, tagger_len);
1600 if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
1601 err = got_error_from_errno("imsg_compose TAG");
1605 if (tagmsg_len == 0 ||
1606 tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1607 err = flush_imsg(ibuf);
1611 err = send_tagmsg(ibuf, tag, tagmsg_len);
1617 const struct got_error *
1618 got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
1620 const struct got_error *err = NULL;
1622 struct got_imsg_tag_object *itag;
1623 size_t len, datalen;
1624 const size_t min_datalen =
1625 MIN(sizeof(struct got_imsg_error),
1626 sizeof(struct got_imsg_tag_object));
1630 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1634 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1637 switch (imsg.hdr.type) {
1639 if (datalen < sizeof(*itag)) {
1640 err = got_error(GOT_ERR_PRIVSEP_LEN);
1644 if (datalen != sizeof(*itag) + itag->tag_len +
1646 err = got_error(GOT_ERR_PRIVSEP_LEN);
1649 len += sizeof(*itag);
1651 *tag = calloc(1, sizeof(**tag));
1653 err = got_error_from_errno("calloc");
1657 memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
1659 if (itag->tag_len == 0) {
1660 (*tag)->tag = strdup("");
1661 if ((*tag)->tag == NULL) {
1662 err = got_error_from_errno("strdup");
1666 (*tag)->tag = malloc(itag->tag_len + 1);
1667 if ((*tag)->tag == NULL) {
1668 err = got_error_from_errno("malloc");
1671 memcpy((*tag)->tag, imsg.data + len,
1673 (*tag)->tag[itag->tag_len] = '\0';
1675 len += itag->tag_len;
1677 (*tag)->obj_type = itag->obj_type;
1678 (*tag)->tagger_time = itag->tagger_time;
1679 (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1681 if (itag->tagger_len == 0) {
1682 (*tag)->tagger = strdup("");
1683 if ((*tag)->tagger == NULL) {
1684 err = got_error_from_errno("strdup");
1688 (*tag)->tagger = malloc(itag->tagger_len + 1);
1689 if ((*tag)->tagger == NULL) {
1690 err = got_error_from_errno("malloc");
1693 memcpy((*tag)->tagger, imsg.data + len,
1695 (*tag)->tagger[itag->tagger_len] = '\0';
1697 len += itag->tagger_len;
1699 if (itag->tagmsg_len == 0) {
1700 (*tag)->tagmsg = strdup("");
1701 if ((*tag)->tagmsg == NULL) {
1702 err = got_error_from_errno("strdup");
1706 size_t offset = 0, remain = itag->tagmsg_len;
1708 (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1709 if ((*tag)->tagmsg == NULL) {
1710 err = got_error_from_errno("malloc");
1713 while (remain > 0) {
1714 struct imsg imsg_log;
1715 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1718 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1722 if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1723 return got_error(GOT_ERR_PRIVSEP_MSG);
1725 memcpy((*tag)->tagmsg + offset, imsg_log.data,
1727 imsg_free(&imsg_log);
1731 (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1736 err = got_error(GOT_ERR_PRIVSEP_MSG);
1745 const struct got_error *
1746 got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1747 struct got_packidx *packidx)
1749 const struct got_error *err = NULL;
1750 struct got_imsg_packidx ipackidx;
1751 struct got_imsg_pack ipack;
1754 ipackidx.len = packidx->len;
1755 fd = dup(packidx->fd);
1757 return got_error_from_errno("dup");
1759 if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
1760 sizeof(ipackidx)) == -1) {
1761 err = got_error_from_errno("imsg_compose PACKIDX");
1766 if (strlcpy(ipack.path_packfile, pack->path_packfile,
1767 sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
1768 return got_error(GOT_ERR_NO_SPACE);
1769 ipack.filesize = pack->filesize;
1773 return got_error_from_errno("dup");
1775 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
1777 err = got_error_from_errno("imsg_compose PACK");
1782 return flush_imsg(ibuf);
1785 const struct got_error *
1786 got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
1787 struct got_object_id *id)
1789 struct got_imsg_packed_object iobj;
1792 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
1794 if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
1795 &iobj, sizeof(iobj)) == -1)
1796 return got_error_from_errno("imsg_compose "
1797 "PACKED_OBJECT_REQUEST");
1799 return flush_imsg(ibuf);
1802 const struct got_error *
1803 got_privsep_send_gitconfig_parse_req(struct imsgbuf *ibuf, int fd)
1805 const struct got_error *err = NULL;
1807 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_PARSE_REQUEST, 0, 0, fd,
1809 err = got_error_from_errno("imsg_compose "
1810 "GITCONFIG_PARSE_REQUEST");
1815 return flush_imsg(ibuf);
1818 const struct got_error *
1819 got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *ibuf)
1821 if (imsg_compose(ibuf,
1822 GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST, 0, 0, -1,
1824 return got_error_from_errno("imsg_compose "
1825 "GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST");
1827 return flush_imsg(ibuf);
1830 const struct got_error *
1831 got_privsep_send_gitconfig_author_name_req(struct imsgbuf *ibuf)
1833 if (imsg_compose(ibuf,
1834 GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST, 0, 0, -1, NULL, 0) == -1)
1835 return got_error_from_errno("imsg_compose "
1836 "GITCONFIG_AUTHOR_NAME_REQUEST");
1838 return flush_imsg(ibuf);
1841 const struct got_error *
1842 got_privsep_send_gitconfig_author_email_req(struct imsgbuf *ibuf)
1844 if (imsg_compose(ibuf,
1845 GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST, 0, 0, -1, NULL, 0) == -1)
1846 return got_error_from_errno("imsg_compose "
1847 "GITCONFIG_AUTHOR_EMAIL_REQUEST");
1849 return flush_imsg(ibuf);
1852 const struct got_error *
1853 got_privsep_send_gitconfig_remotes_req(struct imsgbuf *ibuf)
1855 if (imsg_compose(ibuf,
1856 GOT_IMSG_GITCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
1857 return got_error_from_errno("imsg_compose "
1858 "GITCONFIG_REMOTE_REQUEST");
1860 return flush_imsg(ibuf);
1863 const struct got_error *
1864 got_privsep_send_gitconfig_owner_req(struct imsgbuf *ibuf)
1866 if (imsg_compose(ibuf,
1867 GOT_IMSG_GITCONFIG_OWNER_REQUEST, 0, 0, -1, NULL, 0) == -1)
1868 return got_error_from_errno("imsg_compose "
1869 "GITCONFIG_OWNER_REQUEST");
1871 return flush_imsg(ibuf);
1874 const struct got_error *
1875 got_privsep_send_gitconfig_str(struct imsgbuf *ibuf, const char *value)
1877 size_t len = value ? strlen(value) + 1 : 0;
1879 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_STR_VAL, 0, 0, -1,
1881 return got_error_from_errno("imsg_compose GITCONFIG_STR_VAL");
1883 return flush_imsg(ibuf);
1886 const struct got_error *
1887 got_privsep_recv_gitconfig_str(char **str, struct imsgbuf *ibuf)
1889 const struct got_error *err = NULL;
1892 const size_t min_datalen = 0;
1896 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1899 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1901 switch (imsg.hdr.type) {
1902 case GOT_IMSG_GITCONFIG_STR_VAL:
1905 *str = malloc(datalen);
1907 err = got_error_from_errno("malloc");
1910 if (strlcpy(*str, imsg.data, datalen) >= datalen)
1911 err = got_error(GOT_ERR_NO_SPACE);
1914 err = got_error(GOT_ERR_PRIVSEP_MSG);
1922 const struct got_error *
1923 got_privsep_send_gitconfig_int(struct imsgbuf *ibuf, int value)
1925 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_INT_VAL, 0, 0, -1,
1926 &value, sizeof(value)) == -1)
1927 return got_error_from_errno("imsg_compose GITCONFIG_INT_VAL");
1929 return flush_imsg(ibuf);
1932 const struct got_error *
1933 got_privsep_recv_gitconfig_int(int *val, struct imsgbuf *ibuf)
1935 const struct got_error *err = NULL;
1938 const size_t min_datalen =
1939 MIN(sizeof(struct got_imsg_error), sizeof(int));
1943 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1946 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1948 switch (imsg.hdr.type) {
1949 case GOT_IMSG_GITCONFIG_INT_VAL:
1950 if (datalen != sizeof(*val)) {
1951 err = got_error(GOT_ERR_PRIVSEP_LEN);
1954 memcpy(val, imsg.data, sizeof(*val));
1957 err = got_error(GOT_ERR_PRIVSEP_MSG);
1965 const struct got_error *
1966 got_privsep_send_gitconfig_remotes(struct imsgbuf *ibuf,
1967 struct got_remote_repo *remotes, int nremotes)
1969 const struct got_error *err = NULL;
1970 struct got_imsg_remotes iremotes;
1973 iremotes.nremotes = nremotes;
1974 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_REMOTES, 0, 0, -1,
1975 &iremotes, sizeof(iremotes)) == -1)
1976 return got_error_from_errno("imsg_compose GITCONFIG_REMOTES");
1978 err = flush_imsg(ibuf);
1983 for (i = 0; i < nremotes; i++) {
1984 struct got_imsg_remote iremote;
1985 size_t len = sizeof(iremote);
1988 iremote.mirror_references = remotes[i].mirror_references;
1989 iremote.name_len = strlen(remotes[i].name);
1990 len += iremote.name_len;
1991 iremote.url_len = strlen(remotes[i].url);
1992 len += iremote.url_len;
1994 wbuf = imsg_create(ibuf, GOT_IMSG_GITCONFIG_REMOTE, 0, 0, len);
1996 return got_error_from_errno(
1997 "imsg_create GITCONFIG_REMOTE");
1999 if (imsg_add(wbuf, &iremote, sizeof(iremote)) == -1) {
2000 err = got_error_from_errno(
2001 "imsg_add GITCONFIG_REMOTE");
2006 if (imsg_add(wbuf, remotes[i].name, iremote.name_len) == -1) {
2007 err = got_error_from_errno(
2008 "imsg_add GITCONFIG_REMOTE");
2012 if (imsg_add(wbuf, remotes[i].url, iremote.url_len) == -1) {
2013 err = got_error_from_errno(
2014 "imsg_add GITCONFIG_REMOTE");
2020 imsg_close(ibuf, wbuf);
2021 err = flush_imsg(ibuf);
2029 const struct got_error *
2030 got_privsep_recv_gitconfig_remotes(struct got_remote_repo **remotes,
2031 int *nremotes, struct imsgbuf *ibuf)
2033 const struct got_error *err = NULL;
2036 struct got_imsg_remotes iremotes;
2037 struct got_imsg_remote iremote;
2041 iremotes.nremotes = 0;
2043 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremotes));
2046 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2048 switch (imsg.hdr.type) {
2049 case GOT_IMSG_GITCONFIG_REMOTES:
2050 if (datalen != sizeof(iremotes)) {
2051 err = got_error(GOT_ERR_PRIVSEP_LEN);
2054 memcpy(&iremotes, imsg.data, sizeof(iremotes));
2055 if (iremotes.nremotes == 0) {
2062 return got_error(GOT_ERR_PRIVSEP_MSG);
2067 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(**remotes));
2068 if (*remotes == NULL)
2069 return got_error_from_errno("recallocarray");
2071 while (*nremotes < iremotes.nremotes) {
2072 struct got_remote_repo *remote;
2074 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremote));
2077 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2079 switch (imsg.hdr.type) {
2080 case GOT_IMSG_GITCONFIG_REMOTE:
2081 remote = &(*remotes)[*nremotes];
2082 if (datalen < sizeof(iremote)) {
2083 err = got_error(GOT_ERR_PRIVSEP_LEN);
2086 memcpy(&iremote, imsg.data, sizeof(iremote));
2087 if (iremote.name_len == 0 || iremote.url_len == 0 ||
2088 (sizeof(iremote) + iremote.name_len +
2089 iremote.url_len) > datalen) {
2090 err = got_error(GOT_ERR_PRIVSEP_LEN);
2093 remote->name = strndup(imsg.data + sizeof(iremote),
2095 if (remote->name == NULL) {
2096 err = got_error_from_errno("strndup");
2099 remote->url = strndup(imsg.data + sizeof(iremote) +
2100 iremote.name_len, iremote.url_len);
2101 if (remote->url == NULL) {
2102 err = got_error_from_errno("strndup");
2106 remote->mirror_references = iremote.mirror_references;
2110 err = got_error(GOT_ERR_PRIVSEP_MSG);
2121 for (i = 0; i < *nremotes; i++) {
2122 free((*remotes)[i].name);
2123 free((*remotes)[i].url);
2132 const struct got_error *
2133 got_privsep_send_commit_traversal_request(struct imsgbuf *ibuf,
2134 struct got_object_id *id, int idx, const char *path)
2136 const struct got_error *err = NULL;
2138 size_t path_len = strlen(path) + 1;
2140 wbuf = imsg_create(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_REQUEST, 0, 0,
2141 sizeof(struct got_imsg_commit_traversal_request) + path_len);
2143 return got_error_from_errno(
2144 "imsg_create COMMIT_TRAVERSAL_REQUEST");
2145 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
2146 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
2150 if (imsg_add(wbuf, &idx, sizeof(idx)) == -1) {
2151 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
2155 if (imsg_add(wbuf, path, path_len) == -1) {
2156 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
2162 imsg_close(ibuf, wbuf);
2164 return flush_imsg(ibuf);
2167 const struct got_error *
2168 got_privsep_send_traversed_commits(struct got_object_id *commit_ids,
2169 size_t ncommits, struct imsgbuf *ibuf)
2171 const struct got_error *err;
2175 wbuf = imsg_create(ibuf, GOT_IMSG_TRAVERSED_COMMITS, 0, 0,
2176 sizeof(struct got_imsg_traversed_commits) +
2177 ncommits * SHA1_DIGEST_LENGTH);
2179 return got_error_from_errno("imsg_create TRAVERSED_COMMITS");
2181 if (imsg_add(wbuf, &ncommits, sizeof(ncommits)) == -1) {
2182 err = got_error_from_errno("imsg_add TRAVERSED_COMMITS");
2186 for (i = 0; i < ncommits; i++) {
2187 struct got_object_id *id = &commit_ids[i];
2188 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
2189 err = got_error_from_errno(
2190 "imsg_add TRAVERSED_COMMITS");
2197 imsg_close(ibuf, wbuf);
2199 return flush_imsg(ibuf);
2202 const struct got_error *
2203 got_privsep_recv_traversed_commits(struct got_commit_object **changed_commit,
2204 struct got_object_id **changed_commit_id,
2205 struct got_object_id_queue *commit_ids, struct imsgbuf *ibuf)
2207 const struct got_error *err = NULL;
2209 struct got_imsg_traversed_commits *icommits;
2213 *changed_commit = NULL;
2214 *changed_commit_id = NULL;
2217 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2221 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2222 switch (imsg.hdr.type) {
2223 case GOT_IMSG_TRAVERSED_COMMITS:
2224 icommits = imsg.data;
2225 if (datalen != sizeof(*icommits) +
2226 icommits->ncommits * SHA1_DIGEST_LENGTH) {
2227 err = got_error(GOT_ERR_PRIVSEP_LEN);
2230 for (i = 0; i < icommits->ncommits; i++) {
2231 struct got_object_qid *qid;
2232 uint8_t *sha1 = (uint8_t *)imsg.data +
2233 sizeof(*icommits) + i * SHA1_DIGEST_LENGTH;
2234 err = got_object_qid_alloc_partial(&qid);
2237 memcpy(qid->id->sha1, sha1, SHA1_DIGEST_LENGTH);
2238 SIMPLEQ_INSERT_TAIL(commit_ids, qid, entry);
2240 /* The last commit may contain a change. */
2241 if (i == icommits->ncommits - 1) {
2242 *changed_commit_id =
2243 got_object_id_dup(qid->id);
2244 if (*changed_commit_id == NULL) {
2245 err = got_error_from_errno(
2246 "got_object_id_dup");
2252 case GOT_IMSG_COMMIT:
2253 if (*changed_commit_id == NULL) {
2254 err = got_error(GOT_ERR_PRIVSEP_MSG);
2257 err = get_commit_from_imsg(changed_commit, &imsg,
2260 case GOT_IMSG_COMMIT_TRAVERSAL_DONE:
2264 err = got_error(GOT_ERR_PRIVSEP_MSG);
2274 got_object_id_queue_free(commit_ids);
2278 const struct got_error *
2279 got_privsep_send_commit_traversal_done(struct imsgbuf *ibuf)
2281 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_DONE, 0, 0, -1,
2283 return got_error_from_errno("imsg_compose TRAVERSAL_DONE");
2285 return flush_imsg(ibuf);
2288 const struct got_error *
2289 got_privsep_unveil_exec_helpers(void)
2291 const char *helpers[] = {
2292 GOT_PATH_PROG_READ_PACK,
2293 GOT_PATH_PROG_READ_OBJECT,
2294 GOT_PATH_PROG_READ_COMMIT,
2295 GOT_PATH_PROG_READ_TREE,
2296 GOT_PATH_PROG_READ_BLOB,
2297 GOT_PATH_PROG_READ_TAG,
2298 GOT_PATH_PROG_READ_GITCONFIG,
2299 GOT_PATH_PROG_FETCH_PACK,
2300 GOT_PATH_PROG_INDEX_PACK,
2304 for (i = 0; i < nitems(helpers); i++) {
2305 if (unveil(helpers[i], "x") == 0)
2307 return got_error_from_errno2("unveil", helpers[i]);
2314 got_privsep_exec_child(int imsg_fds[2], const char *path, const char *repo_path)
2316 if (close(imsg_fds[0]) != 0) {
2317 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2321 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
2322 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2325 if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
2326 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2330 if (execl(path, path, repo_path, (char *)NULL) == -1) {
2331 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,