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)
418 const struct got_error *err = NULL;
421 struct got_pathlist_entry *pe;
422 struct got_imsg_fetch_request fetchreq;
424 memset(&fetchreq, 0, sizeof(fetchreq));
425 fetchreq.fetch_all_branches = fetch_all_branches;
426 fetchreq.list_refs_only = list_refs_only;
427 TAILQ_FOREACH(pe, have_refs, entry)
428 fetchreq.n_have_refs++;
429 TAILQ_FOREACH(pe, wanted_branches, entry)
430 fetchreq.n_wanted_branches++;
431 len = sizeof(struct got_imsg_fetch_request);
432 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
434 return got_error(GOT_ERR_NO_SPACE);
437 if (imsg_compose(ibuf, GOT_IMSG_FETCH_REQUEST, 0, 0, fd,
438 &fetchreq, sizeof(fetchreq)) == -1)
439 return got_error_from_errno(
440 "imsg_compose FETCH_SERVER_PROGRESS");
442 err = flush_imsg(ibuf);
449 TAILQ_FOREACH(pe, have_refs, entry) {
450 const char *name = pe->path;
451 size_t name_len = pe->path_len;
452 struct got_object_id *id = pe->data;
454 len = sizeof(struct got_imsg_fetch_have_ref) + name_len;
455 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_HAVE_REF, 0, 0, len);
457 return got_error_from_errno("imsg_create FETCH_HAVE_REF");
459 /* Keep in sync with struct got_imsg_fetch_have_ref! */
460 if (imsg_add(wbuf, id->sha1, sizeof(id->sha1)) == -1) {
461 err = got_error_from_errno("imsg_add FETCH_HAVE_REF");
465 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
466 err = got_error_from_errno("imsg_add FETCH_HAVE_REF");
470 if (imsg_add(wbuf, name, name_len) == -1) {
471 err = got_error_from_errno("imsg_add FETCH_HAVE_REF");
477 imsg_close(ibuf, wbuf);
478 err = flush_imsg(ibuf);
483 TAILQ_FOREACH(pe, wanted_branches, entry) {
484 const char *name = pe->path;
485 size_t name_len = pe->path_len;
487 len = sizeof(struct got_imsg_fetch_wanted_branch) + name_len;
488 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_WANTED_BRANCH, 0, 0,
491 return got_error_from_errno(
492 "imsg_create FETCH_WANTED_BRANCH");
494 /* Keep in sync with struct got_imsg_fetch_wanted_branch! */
495 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
496 err = got_error_from_errno(
497 "imsg_add FETCH_WANTED_BRANCH");
501 if (imsg_add(wbuf, name, name_len) == -1) {
502 err = got_error_from_errno(
503 "imsg_add FETCH_WANTED_BRANCH");
509 imsg_close(ibuf, wbuf);
510 err = flush_imsg(ibuf);
519 const struct got_error *
520 got_privsep_send_fetch_outfd(struct imsgbuf *ibuf, int fd)
522 return send_fd(ibuf, GOT_IMSG_FETCH_OUTFD, fd);
526 const struct got_error *
527 got_privsep_send_fetch_symrefs(struct imsgbuf *ibuf,
528 struct got_pathlist_head *symrefs)
530 const struct got_error *err = NULL;
532 size_t len, nsymrefs = 0;
533 struct got_pathlist_entry *pe;
535 len = sizeof(struct got_imsg_fetch_symrefs);
536 TAILQ_FOREACH(pe, symrefs, entry) {
537 const char *target = pe->data;
538 len += sizeof(struct got_imsg_fetch_symref) +
539 pe->path_len + strlen(target);
543 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
544 return got_error(GOT_ERR_NO_SPACE);
546 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_SYMREFS, 0, 0, len);
548 return got_error_from_errno("imsg_create FETCH_SYMREFS");
550 /* Keep in sync with struct got_imsg_fetch_symrefs definition! */
551 if (imsg_add(wbuf, &nsymrefs, sizeof(nsymrefs)) == -1) {
552 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
557 TAILQ_FOREACH(pe, symrefs, entry) {
558 const char *name = pe->path;
559 size_t name_len = pe->path_len;
560 const char *target = pe->data;
561 size_t target_len = strlen(target);
563 /* Keep in sync with struct got_imsg_fetch_symref definition! */
564 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
565 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
569 if (imsg_add(wbuf, &target_len, sizeof(target_len)) == -1) {
570 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
574 if (imsg_add(wbuf, name, name_len) == -1) {
575 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
579 if (imsg_add(wbuf, target, target_len) == -1) {
580 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
587 imsg_close(ibuf, wbuf);
588 return flush_imsg(ibuf);
591 const struct got_error *
592 got_privsep_send_fetch_ref(struct imsgbuf *ibuf,
593 struct got_object_id *refid, const char *refname)
595 const struct got_error *err = NULL;
597 size_t len, reflen = strlen(refname);
599 len = sizeof(struct got_imsg_fetch_ref) + reflen;
600 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
601 return got_error(GOT_ERR_NO_SPACE);
603 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_REF, 0, 0, len);
605 return got_error_from_errno("imsg_create FETCH_REF");
607 /* Keep in sync with struct got_imsg_fetch_ref definition! */
608 if (imsg_add(wbuf, refid->sha1, SHA1_DIGEST_LENGTH) == -1) {
609 err = got_error_from_errno("imsg_add FETCH_REF");
613 if (imsg_add(wbuf, refname, reflen) == -1) {
614 err = got_error_from_errno("imsg_add FETCH_REF");
620 imsg_close(ibuf, wbuf);
621 return flush_imsg(ibuf);
624 const struct got_error *
625 got_privsep_send_fetch_server_progress(struct imsgbuf *ibuf, const char *msg,
628 if (msglen > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
629 return got_error(GOT_ERR_NO_SPACE);
634 if (imsg_compose(ibuf, GOT_IMSG_FETCH_SERVER_PROGRESS, 0, 0, -1,
636 return got_error_from_errno(
637 "imsg_compose FETCH_SERVER_PROGRESS");
639 return flush_imsg(ibuf);
642 const struct got_error *
643 got_privsep_send_fetch_download_progress(struct imsgbuf *ibuf, off_t bytes)
645 if (imsg_compose(ibuf, GOT_IMSG_FETCH_DOWNLOAD_PROGRESS, 0, 0, -1,
646 &bytes, sizeof(bytes)) == -1)
647 return got_error_from_errno(
648 "imsg_compose FETCH_DOWNLOAD_PROGRESS");
650 return flush_imsg(ibuf);
653 const struct got_error *
654 got_privsep_send_fetch_done(struct imsgbuf *ibuf, struct got_object_id hash)
656 if (imsg_compose(ibuf, GOT_IMSG_FETCH_DONE, 0, 0, -1,
657 hash.sha1, SHA1_DIGEST_LENGTH) == -1)
658 return got_error_from_errno("imsg_compose FETCH");
659 return flush_imsg(ibuf);
663 const struct got_error *
664 got_privsep_recv_fetch_progress(int *done, struct got_object_id **id,
665 char **refname, struct got_pathlist_head *symrefs, char **server_progress,
666 off_t *packfile_size, struct imsgbuf *ibuf)
668 const struct got_error *err = NULL;
671 struct got_imsg_fetch_symrefs *isymrefs = NULL;
679 *server_progress = NULL;
682 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
686 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
687 switch (imsg.hdr.type) {
689 if (datalen < sizeof(struct got_imsg_error)) {
690 err = got_error(GOT_ERR_PRIVSEP_LEN);
693 err = recv_imsg_error(&imsg, datalen);
695 case GOT_IMSG_FETCH_SYMREFS:
696 if (datalen < sizeof(*isymrefs)) {
697 err = got_error(GOT_ERR_PRIVSEP_LEN);
700 if (isymrefs != NULL) {
701 err = got_error(GOT_ERR_PRIVSEP_MSG);
704 isymrefs = (struct got_imsg_fetch_symrefs *)imsg.data;
705 off = sizeof(*isymrefs);
706 remain = datalen - off;
707 for (n = 0; n < isymrefs->nsymrefs; n++) {
708 struct got_imsg_fetch_symref *s;
710 if (remain < sizeof(struct got_imsg_fetch_symref)) {
711 err = got_error(GOT_ERR_PRIVSEP_LEN);
714 s = (struct got_imsg_fetch_symref *)(imsg.data + off);
716 remain -= sizeof(*s);
717 if (remain < s->name_len) {
718 err = got_error(GOT_ERR_PRIVSEP_LEN);
721 name = strndup(imsg.data + off, s->name_len);
723 err = got_error_from_errno("strndup");
727 remain -= s->name_len;
728 if (remain < s->target_len) {
729 err = got_error(GOT_ERR_PRIVSEP_LEN);
733 target = strndup(imsg.data + off, s->target_len);
734 if (target == NULL) {
735 err = got_error_from_errno("strndup");
739 off += s->target_len;
740 remain -= s->target_len;
741 err = got_pathlist_append(symrefs, name, target);
749 case GOT_IMSG_FETCH_REF:
750 if (datalen <= SHA1_DIGEST_LENGTH) {
751 err = got_error(GOT_ERR_PRIVSEP_MSG);
754 *id = malloc(sizeof(**id));
756 err = got_error_from_errno("malloc");
759 memcpy((*id)->sha1, imsg.data, SHA1_DIGEST_LENGTH);
760 *refname = strndup(imsg.data + SHA1_DIGEST_LENGTH,
761 datalen - SHA1_DIGEST_LENGTH);
762 if (*refname == NULL) {
763 err = got_error_from_errno("strndup");
767 case GOT_IMSG_FETCH_SERVER_PROGRESS:
769 err = got_error(GOT_ERR_PRIVSEP_LEN);
772 *server_progress = strndup(imsg.data, datalen);
773 if (*server_progress == NULL) {
774 err = got_error_from_errno("strndup");
777 for (i = 0; i < datalen; i++) {
778 if (!isprint((unsigned char)(*server_progress)[i]) &&
779 !isspace((unsigned char)(*server_progress)[i])) {
780 err = got_error(GOT_ERR_PRIVSEP_MSG);
781 free(*server_progress);
782 *server_progress = NULL;
787 case GOT_IMSG_FETCH_DOWNLOAD_PROGRESS:
788 if (datalen < sizeof(*packfile_size)) {
789 err = got_error(GOT_ERR_PRIVSEP_MSG);
792 memcpy(packfile_size, imsg.data, sizeof(*packfile_size));
794 case GOT_IMSG_FETCH_DONE:
795 *id = malloc(sizeof(**id));
797 err = got_error_from_errno("malloc");
800 if (datalen != SHA1_DIGEST_LENGTH) {
801 err = got_error(GOT_ERR_PRIVSEP_MSG);
804 memcpy((*id)->sha1, imsg.data, SHA1_DIGEST_LENGTH);
808 err = got_error(GOT_ERR_PRIVSEP_MSG);
822 const struct got_error *
823 got_privsep_send_index_pack_req(struct imsgbuf *ibuf, uint8_t *pack_hash,
826 const struct got_error *err = NULL;
828 /* Keep in sync with struct got_imsg_index_pack_request */
829 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_REQUEST, 0, 0, fd,
830 pack_hash, SHA1_DIGEST_LENGTH) == -1) {
831 err = got_error_from_errno("imsg_compose INDEX_REQUEST");
835 return flush_imsg(ibuf);
838 const struct got_error *
839 got_privsep_send_index_pack_outfd(struct imsgbuf *ibuf, int fd)
841 return send_fd(ibuf, GOT_IMSG_IDXPACK_OUTFD, fd);
844 const struct got_error *
845 got_privsep_send_index_pack_progress(struct imsgbuf *ibuf, int nobj_total,
846 int nobj_indexed, int nobj_loose, int nobj_resolved)
848 struct got_imsg_index_pack_progress iprogress;
850 iprogress.nobj_total = nobj_total;
851 iprogress.nobj_indexed = nobj_indexed;
852 iprogress.nobj_loose = nobj_loose;
853 iprogress.nobj_resolved = nobj_resolved;
855 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_PROGRESS, 0, 0, -1,
856 &iprogress, sizeof(iprogress)) == -1)
857 return got_error_from_errno("imsg_compose IDXPACK_PROGRESS");
859 return flush_imsg(ibuf);
862 const struct got_error *
863 got_privsep_send_index_pack_done(struct imsgbuf *ibuf)
865 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_DONE, 0, 0, -1, NULL, 0) == -1)
866 return got_error_from_errno("imsg_compose FETCH");
867 return flush_imsg(ibuf);
870 const struct got_error *
871 got_privsep_recv_index_progress(int *done, int *nobj_total,
872 int *nobj_indexed, int *nobj_loose, int *nobj_resolved,
873 struct imsgbuf *ibuf)
875 const struct got_error *err = NULL;
877 struct got_imsg_index_pack_progress *iprogress;
885 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
889 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
890 switch (imsg.hdr.type) {
892 if (datalen < sizeof(struct got_imsg_error)) {
893 err = got_error(GOT_ERR_PRIVSEP_LEN);
896 err = recv_imsg_error(&imsg, datalen);
898 case GOT_IMSG_IDXPACK_PROGRESS:
899 if (datalen < sizeof(*iprogress)) {
900 err = got_error(GOT_ERR_PRIVSEP_LEN);
903 iprogress = (struct got_imsg_index_pack_progress *)imsg.data;
904 *nobj_total = iprogress->nobj_total;
905 *nobj_indexed = iprogress->nobj_indexed;
906 *nobj_loose = iprogress->nobj_loose;
907 *nobj_resolved = iprogress->nobj_resolved;
909 case GOT_IMSG_IDXPACK_DONE:
911 err = got_error(GOT_ERR_PRIVSEP_LEN);
917 err = got_error(GOT_ERR_PRIVSEP_MSG);
925 const struct got_error *
926 got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
927 struct imsgbuf *ibuf)
929 const struct got_error *err = NULL;
930 struct got_imsg_object *iobj;
931 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
933 if (datalen != sizeof(*iobj))
934 return got_error(GOT_ERR_PRIVSEP_LEN);
937 *obj = calloc(1, sizeof(**obj));
939 return got_error_from_errno("calloc");
941 memcpy((*obj)->id.sha1, iobj->id, SHA1_DIGEST_LENGTH);
942 (*obj)->type = iobj->type;
943 (*obj)->flags = iobj->flags;
944 (*obj)->hdrlen = iobj->hdrlen;
945 (*obj)->size = iobj->size;
946 /* path_packfile is handled by caller */
947 if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
948 (*obj)->pack_offset = iobj->pack_offset;
949 (*obj)->pack_idx = iobj->pack_idx;
955 const struct got_error *
956 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
958 const struct got_error *err = NULL;
960 const size_t min_datalen =
961 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
965 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
969 switch (imsg.hdr.type) {
970 case GOT_IMSG_OBJECT:
971 err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
974 err = got_error(GOT_ERR_PRIVSEP_MSG);
983 static const struct got_error *
984 send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
987 const struct got_error *err = NULL;
988 size_t offset, remain;
993 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
995 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
996 commit->logmsg + offset, n) == -1) {
997 err = got_error_from_errno("imsg_compose "
1002 err = flush_imsg(ibuf);
1013 const struct got_error *
1014 got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
1016 const struct got_error *err = NULL;
1017 struct got_imsg_commit_object *icommit;
1020 struct got_object_qid *qid;
1021 size_t author_len = strlen(commit->author);
1022 size_t committer_len = strlen(commit->committer);
1023 size_t logmsg_len = strlen(commit->logmsg);
1025 total = sizeof(*icommit) + author_len + committer_len +
1026 commit->nparents * SHA1_DIGEST_LENGTH;
1028 buf = malloc(total);
1030 return got_error_from_errno("malloc");
1032 icommit = (struct got_imsg_commit_object *)buf;
1033 memcpy(icommit->tree_id, commit->tree_id->sha1,
1034 sizeof(icommit->tree_id));
1035 icommit->author_len = author_len;
1036 icommit->author_time = commit->author_time;
1037 icommit->author_gmtoff = commit->author_gmtoff;
1038 icommit->committer_len = committer_len;
1039 icommit->committer_time = commit->committer_time;
1040 icommit->committer_gmtoff = commit->committer_gmtoff;
1041 icommit->logmsg_len = logmsg_len;
1042 icommit->nparents = commit->nparents;
1044 len = sizeof(*icommit);
1045 memcpy(buf + len, commit->author, author_len);
1047 memcpy(buf + len, commit->committer, committer_len);
1048 len += committer_len;
1049 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
1050 memcpy(buf + len, qid->id, SHA1_DIGEST_LENGTH);
1051 len += SHA1_DIGEST_LENGTH;
1054 if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
1055 err = got_error_from_errno("imsg_compose COMMIT");
1059 if (logmsg_len == 0 ||
1060 logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1061 err = flush_imsg(ibuf);
1065 err = send_commit_logmsg(ibuf, commit, logmsg_len);
1071 static const struct got_error *
1072 get_commit_from_imsg(struct got_commit_object **commit,
1073 struct imsg *imsg, size_t datalen, struct imsgbuf *ibuf)
1075 const struct got_error *err = NULL;
1076 struct got_imsg_commit_object *icommit;
1080 if (datalen < sizeof(*icommit))
1081 return got_error(GOT_ERR_PRIVSEP_LEN);
1083 icommit = imsg->data;
1084 if (datalen != sizeof(*icommit) + icommit->author_len +
1085 icommit->committer_len +
1086 icommit->nparents * SHA1_DIGEST_LENGTH)
1087 return got_error(GOT_ERR_PRIVSEP_LEN);
1089 if (icommit->nparents < 0)
1090 return got_error(GOT_ERR_PRIVSEP_LEN);
1092 len += sizeof(*icommit);
1094 *commit = got_object_commit_alloc_partial();
1095 if (*commit == NULL)
1096 return got_error_from_errno(
1097 "got_object_commit_alloc_partial");
1099 memcpy((*commit)->tree_id->sha1, icommit->tree_id,
1100 SHA1_DIGEST_LENGTH);
1101 (*commit)->author_time = icommit->author_time;
1102 (*commit)->author_gmtoff = icommit->author_gmtoff;
1103 (*commit)->committer_time = icommit->committer_time;
1104 (*commit)->committer_gmtoff = icommit->committer_gmtoff;
1106 if (icommit->author_len == 0) {
1107 (*commit)->author = strdup("");
1108 if ((*commit)->author == NULL) {
1109 err = got_error_from_errno("strdup");
1113 (*commit)->author = malloc(icommit->author_len + 1);
1114 if ((*commit)->author == NULL) {
1115 err = got_error_from_errno("malloc");
1118 memcpy((*commit)->author, imsg->data + len,
1119 icommit->author_len);
1120 (*commit)->author[icommit->author_len] = '\0';
1122 len += icommit->author_len;
1124 if (icommit->committer_len == 0) {
1125 (*commit)->committer = strdup("");
1126 if ((*commit)->committer == NULL) {
1127 err = got_error_from_errno("strdup");
1131 (*commit)->committer =
1132 malloc(icommit->committer_len + 1);
1133 if ((*commit)->committer == NULL) {
1134 err = got_error_from_errno("malloc");
1137 memcpy((*commit)->committer, imsg->data + len,
1138 icommit->committer_len);
1139 (*commit)->committer[icommit->committer_len] = '\0';
1141 len += icommit->committer_len;
1143 if (icommit->logmsg_len == 0) {
1144 (*commit)->logmsg = strdup("");
1145 if ((*commit)->logmsg == NULL) {
1146 err = got_error_from_errno("strdup");
1150 size_t offset = 0, remain = icommit->logmsg_len;
1152 (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
1153 if ((*commit)->logmsg == NULL) {
1154 err = got_error_from_errno("malloc");
1157 while (remain > 0) {
1158 struct imsg imsg_log;
1159 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1162 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1166 if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG) {
1167 err = got_error(GOT_ERR_PRIVSEP_MSG);
1171 memcpy((*commit)->logmsg + offset,
1173 imsg_free(&imsg_log);
1177 (*commit)->logmsg[icommit->logmsg_len] = '\0';
1180 for (i = 0; i < icommit->nparents; i++) {
1181 struct got_object_qid *qid;
1183 err = got_object_qid_alloc_partial(&qid);
1186 memcpy(qid->id, imsg->data + len +
1187 i * SHA1_DIGEST_LENGTH, sizeof(*qid->id));
1188 SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
1189 (*commit)->nparents++;
1193 got_object_commit_close(*commit);
1199 const struct got_error *
1200 got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
1202 const struct got_error *err = NULL;
1205 const size_t min_datalen =
1206 MIN(sizeof(struct got_imsg_error),
1207 sizeof(struct got_imsg_commit_object));
1211 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1215 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1217 switch (imsg.hdr.type) {
1218 case GOT_IMSG_COMMIT:
1219 err = get_commit_from_imsg(commit, &imsg, datalen, ibuf);
1222 err = got_error(GOT_ERR_PRIVSEP_MSG);
1231 const struct got_error *
1232 got_privsep_send_tree(struct imsgbuf *ibuf, struct got_pathlist_head *entries,
1235 const struct got_error *err = NULL;
1236 struct got_imsg_tree_object itree;
1237 struct got_pathlist_entry *pe;
1239 int nimsg; /* number of imsg queued in ibuf */
1241 itree.nentries = nentries;
1242 if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
1244 return got_error_from_errno("imsg_compose TREE");
1246 totlen = sizeof(itree);
1248 TAILQ_FOREACH(pe, entries, entry) {
1249 const char *name = pe->path;
1250 struct got_parsed_tree_entry *pte = pe->data;
1252 size_t namelen = strlen(name);
1253 size_t len = sizeof(struct got_imsg_tree_entry) + namelen;
1255 if (len > MAX_IMSGSIZE)
1256 return got_error(GOT_ERR_NO_SPACE);
1259 if (totlen + len >= MAX_IMSGSIZE - (IMSG_HEADER_SIZE * nimsg)) {
1260 err = flush_imsg(ibuf);
1266 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_ENTRY, 0, 0, len);
1268 return got_error_from_errno("imsg_create TREE_ENTRY");
1270 /* Keep in sync with struct got_imsg_tree_object definition! */
1271 if (imsg_add(wbuf, pte->id, SHA1_DIGEST_LENGTH) == -1) {
1272 err = got_error_from_errno("imsg_add TREE_ENTRY");
1276 if (imsg_add(wbuf, &pte->mode, sizeof(pte->mode)) == -1) {
1277 err = got_error_from_errno("imsg_add TREE_ENTRY");
1282 if (imsg_add(wbuf, name, namelen) == -1) {
1283 err = got_error_from_errno("imsg_add TREE_ENTRY");
1289 imsg_close(ibuf, wbuf);
1294 return flush_imsg(ibuf);
1297 const struct got_error *
1298 got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
1300 const struct got_error *err = NULL;
1301 const size_t min_datalen =
1302 MIN(sizeof(struct got_imsg_error),
1303 sizeof(struct got_imsg_tree_object));
1304 struct got_imsg_tree_object *itree;
1309 err = read_imsg(ibuf);
1317 struct got_imsg_tree_entry *ite;
1318 struct got_tree_entry *te = NULL;
1320 n = imsg_get(ibuf, &imsg);
1322 if (*tree && (*tree)->nentries != nentries)
1327 if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen)
1328 return got_error(GOT_ERR_PRIVSEP_LEN);
1330 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1332 switch (imsg.hdr.type) {
1333 case GOT_IMSG_ERROR:
1334 err = recv_imsg_error(&imsg, datalen);
1337 /* This message should only appear once. */
1338 if (*tree != NULL) {
1339 err = got_error(GOT_ERR_PRIVSEP_MSG);
1342 if (datalen != sizeof(*itree)) {
1343 err = got_error(GOT_ERR_PRIVSEP_LEN);
1347 *tree = malloc(sizeof(**tree));
1348 if (*tree == NULL) {
1349 err = got_error_from_errno("malloc");
1352 (*tree)->entries = calloc(itree->nentries,
1353 sizeof(struct got_tree_entry));
1354 if ((*tree)->entries == NULL) {
1355 err = got_error_from_errno("malloc");
1358 (*tree)->nentries = itree->nentries;
1359 (*tree)->refcnt = 0;
1361 case GOT_IMSG_TREE_ENTRY:
1362 /* This message should be preceeded by GOT_IMSG_TREE. */
1363 if (*tree == NULL) {
1364 err = got_error(GOT_ERR_PRIVSEP_MSG);
1367 if (datalen < sizeof(*ite) || datalen > MAX_IMSGSIZE) {
1368 err = got_error(GOT_ERR_PRIVSEP_LEN);
1372 /* Remaining data contains the entry's name. */
1373 datalen -= sizeof(*ite);
1374 if (datalen == 0 || datalen > MAX_IMSGSIZE) {
1375 err = got_error(GOT_ERR_PRIVSEP_LEN);
1380 if (datalen + 1 > sizeof(te->name)) {
1381 err = got_error(GOT_ERR_NO_SPACE);
1384 te = &(*tree)->entries[nentries];
1385 memcpy(te->name, imsg.data + sizeof(*ite), datalen);
1386 te->name[datalen] = '\0';
1388 memcpy(te->id.sha1, ite->id, SHA1_DIGEST_LENGTH);
1389 te->mode = ite->mode;
1394 err = got_error(GOT_ERR_PRIVSEP_MSG);
1401 if (*tree && (*tree)->nentries != nentries) {
1403 err = got_error(GOT_ERR_PRIVSEP_LEN);
1404 got_object_tree_close(*tree);
1411 const struct got_error *
1412 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
1413 const uint8_t *data)
1415 struct got_imsg_blob iblob;
1418 iblob.hdrlen = hdrlen;
1423 if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
1424 return got_error(GOT_ERR_NO_SPACE);
1426 buf = malloc(sizeof(iblob) + size);
1428 return got_error_from_errno("malloc");
1430 memcpy(buf, &iblob, sizeof(iblob));
1431 memcpy(buf + sizeof(iblob), data, size);
1432 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
1433 sizeof(iblob) + size) == -1) {
1435 return got_error_from_errno("imsg_compose BLOB");
1439 /* Data has already been written to file descriptor. */
1440 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
1441 sizeof(iblob)) == -1)
1442 return got_error_from_errno("imsg_compose BLOB");
1446 return flush_imsg(ibuf);
1449 const struct got_error *
1450 got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1451 struct imsgbuf *ibuf)
1453 const struct got_error *err = NULL;
1455 struct got_imsg_blob *iblob;
1460 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1464 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1466 switch (imsg.hdr.type) {
1468 if (datalen < sizeof(*iblob)) {
1469 err = got_error(GOT_ERR_PRIVSEP_LEN);
1473 *size = iblob->size;
1474 *hdrlen = iblob->hdrlen;
1476 if (datalen == sizeof(*iblob)) {
1477 /* Data has been written to file descriptor. */
1481 if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX) {
1482 err = got_error(GOT_ERR_PRIVSEP_LEN);
1486 *outbuf = malloc(*size);
1487 if (*outbuf == NULL) {
1488 err = got_error_from_errno("malloc");
1491 memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
1494 err = got_error(GOT_ERR_PRIVSEP_MSG);
1503 static const struct got_error *
1504 send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
1506 const struct got_error *err = NULL;
1507 size_t offset, remain;
1510 remain = tagmsg_len;
1511 while (remain > 0) {
1512 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1514 if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
1515 tag->tagmsg + offset, n) == -1) {
1516 err = got_error_from_errno("imsg_compose TAG_TAGMSG");
1520 err = flush_imsg(ibuf);
1531 const struct got_error *
1532 got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
1534 const struct got_error *err = NULL;
1535 struct got_imsg_tag_object *itag;
1538 size_t tag_len = strlen(tag->tag);
1539 size_t tagger_len = strlen(tag->tagger);
1540 size_t tagmsg_len = strlen(tag->tagmsg);
1542 total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
1544 buf = malloc(total);
1546 return got_error_from_errno("malloc");
1548 itag = (struct got_imsg_tag_object *)buf;
1549 memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
1550 itag->obj_type = tag->obj_type;
1551 itag->tag_len = tag_len;
1552 itag->tagger_len = tagger_len;
1553 itag->tagger_time = tag->tagger_time;
1554 itag->tagger_gmtoff = tag->tagger_gmtoff;
1555 itag->tagmsg_len = tagmsg_len;
1557 len = sizeof(*itag);
1558 memcpy(buf + len, tag->tag, tag_len);
1560 memcpy(buf + len, tag->tagger, tagger_len);
1563 if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
1564 err = got_error_from_errno("imsg_compose TAG");
1568 if (tagmsg_len == 0 ||
1569 tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1570 err = flush_imsg(ibuf);
1574 err = send_tagmsg(ibuf, tag, tagmsg_len);
1580 const struct got_error *
1581 got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
1583 const struct got_error *err = NULL;
1585 struct got_imsg_tag_object *itag;
1586 size_t len, datalen;
1587 const size_t min_datalen =
1588 MIN(sizeof(struct got_imsg_error),
1589 sizeof(struct got_imsg_tag_object));
1593 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1597 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1600 switch (imsg.hdr.type) {
1602 if (datalen < sizeof(*itag)) {
1603 err = got_error(GOT_ERR_PRIVSEP_LEN);
1607 if (datalen != sizeof(*itag) + itag->tag_len +
1609 err = got_error(GOT_ERR_PRIVSEP_LEN);
1612 len += sizeof(*itag);
1614 *tag = calloc(1, sizeof(**tag));
1616 err = got_error_from_errno("calloc");
1620 memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
1622 if (itag->tag_len == 0) {
1623 (*tag)->tag = strdup("");
1624 if ((*tag)->tag == NULL) {
1625 err = got_error_from_errno("strdup");
1629 (*tag)->tag = malloc(itag->tag_len + 1);
1630 if ((*tag)->tag == NULL) {
1631 err = got_error_from_errno("malloc");
1634 memcpy((*tag)->tag, imsg.data + len,
1636 (*tag)->tag[itag->tag_len] = '\0';
1638 len += itag->tag_len;
1640 (*tag)->obj_type = itag->obj_type;
1641 (*tag)->tagger_time = itag->tagger_time;
1642 (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1644 if (itag->tagger_len == 0) {
1645 (*tag)->tagger = strdup("");
1646 if ((*tag)->tagger == NULL) {
1647 err = got_error_from_errno("strdup");
1651 (*tag)->tagger = malloc(itag->tagger_len + 1);
1652 if ((*tag)->tagger == NULL) {
1653 err = got_error_from_errno("malloc");
1656 memcpy((*tag)->tagger, imsg.data + len,
1658 (*tag)->tagger[itag->tagger_len] = '\0';
1660 len += itag->tagger_len;
1662 if (itag->tagmsg_len == 0) {
1663 (*tag)->tagmsg = strdup("");
1664 if ((*tag)->tagmsg == NULL) {
1665 err = got_error_from_errno("strdup");
1669 size_t offset = 0, remain = itag->tagmsg_len;
1671 (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1672 if ((*tag)->tagmsg == NULL) {
1673 err = got_error_from_errno("malloc");
1676 while (remain > 0) {
1677 struct imsg imsg_log;
1678 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1681 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1685 if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1686 return got_error(GOT_ERR_PRIVSEP_MSG);
1688 memcpy((*tag)->tagmsg + offset, imsg_log.data,
1690 imsg_free(&imsg_log);
1694 (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1699 err = got_error(GOT_ERR_PRIVSEP_MSG);
1708 const struct got_error *
1709 got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1710 struct got_packidx *packidx)
1712 const struct got_error *err = NULL;
1713 struct got_imsg_packidx ipackidx;
1714 struct got_imsg_pack ipack;
1717 ipackidx.len = packidx->len;
1718 fd = dup(packidx->fd);
1720 return got_error_from_errno("dup");
1722 if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
1723 sizeof(ipackidx)) == -1) {
1724 err = got_error_from_errno("imsg_compose PACKIDX");
1729 if (strlcpy(ipack.path_packfile, pack->path_packfile,
1730 sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
1731 return got_error(GOT_ERR_NO_SPACE);
1732 ipack.filesize = pack->filesize;
1736 return got_error_from_errno("dup");
1738 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
1740 err = got_error_from_errno("imsg_compose PACK");
1745 return flush_imsg(ibuf);
1748 const struct got_error *
1749 got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
1750 struct got_object_id *id)
1752 struct got_imsg_packed_object iobj;
1755 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
1757 if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
1758 &iobj, sizeof(iobj)) == -1)
1759 return got_error_from_errno("imsg_compose "
1760 "PACKED_OBJECT_REQUEST");
1762 return flush_imsg(ibuf);
1765 const struct got_error *
1766 got_privsep_send_gitconfig_parse_req(struct imsgbuf *ibuf, int fd)
1768 const struct got_error *err = NULL;
1770 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_PARSE_REQUEST, 0, 0, fd,
1772 err = got_error_from_errno("imsg_compose "
1773 "GITCONFIG_PARSE_REQUEST");
1778 return flush_imsg(ibuf);
1781 const struct got_error *
1782 got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *ibuf)
1784 if (imsg_compose(ibuf,
1785 GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST, 0, 0, -1,
1787 return got_error_from_errno("imsg_compose "
1788 "GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST");
1790 return flush_imsg(ibuf);
1793 const struct got_error *
1794 got_privsep_send_gitconfig_author_name_req(struct imsgbuf *ibuf)
1796 if (imsg_compose(ibuf,
1797 GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST, 0, 0, -1, NULL, 0) == -1)
1798 return got_error_from_errno("imsg_compose "
1799 "GITCONFIG_AUTHOR_NAME_REQUEST");
1801 return flush_imsg(ibuf);
1804 const struct got_error *
1805 got_privsep_send_gitconfig_author_email_req(struct imsgbuf *ibuf)
1807 if (imsg_compose(ibuf,
1808 GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST, 0, 0, -1, NULL, 0) == -1)
1809 return got_error_from_errno("imsg_compose "
1810 "GITCONFIG_AUTHOR_EMAIL_REQUEST");
1812 return flush_imsg(ibuf);
1815 const struct got_error *
1816 got_privsep_send_gitconfig_remotes_req(struct imsgbuf *ibuf)
1818 if (imsg_compose(ibuf,
1819 GOT_IMSG_GITCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
1820 return got_error_from_errno("imsg_compose "
1821 "GITCONFIG_REMOTE_REQUEST");
1823 return flush_imsg(ibuf);
1826 const struct got_error *
1827 got_privsep_send_gitconfig_owner_req(struct imsgbuf *ibuf)
1829 if (imsg_compose(ibuf,
1830 GOT_IMSG_GITCONFIG_OWNER_REQUEST, 0, 0, -1, NULL, 0) == -1)
1831 return got_error_from_errno("imsg_compose "
1832 "GITCONFIG_OWNER_REQUEST");
1834 return flush_imsg(ibuf);
1837 const struct got_error *
1838 got_privsep_send_gitconfig_str(struct imsgbuf *ibuf, const char *value)
1840 size_t len = value ? strlen(value) + 1 : 0;
1842 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_STR_VAL, 0, 0, -1,
1844 return got_error_from_errno("imsg_compose GITCONFIG_STR_VAL");
1846 return flush_imsg(ibuf);
1849 const struct got_error *
1850 got_privsep_recv_gitconfig_str(char **str, struct imsgbuf *ibuf)
1852 const struct got_error *err = NULL;
1855 const size_t min_datalen = 0;
1859 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1862 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1864 switch (imsg.hdr.type) {
1865 case GOT_IMSG_GITCONFIG_STR_VAL:
1868 *str = malloc(datalen);
1870 err = got_error_from_errno("malloc");
1873 if (strlcpy(*str, imsg.data, datalen) >= datalen)
1874 err = got_error(GOT_ERR_NO_SPACE);
1877 err = got_error(GOT_ERR_PRIVSEP_MSG);
1885 const struct got_error *
1886 got_privsep_send_gitconfig_int(struct imsgbuf *ibuf, int value)
1888 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_INT_VAL, 0, 0, -1,
1889 &value, sizeof(value)) == -1)
1890 return got_error_from_errno("imsg_compose GITCONFIG_INT_VAL");
1892 return flush_imsg(ibuf);
1895 const struct got_error *
1896 got_privsep_recv_gitconfig_int(int *val, struct imsgbuf *ibuf)
1898 const struct got_error *err = NULL;
1901 const size_t min_datalen =
1902 MIN(sizeof(struct got_imsg_error), sizeof(int));
1906 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1909 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1911 switch (imsg.hdr.type) {
1912 case GOT_IMSG_GITCONFIG_INT_VAL:
1913 if (datalen != sizeof(*val)) {
1914 err = got_error(GOT_ERR_PRIVSEP_LEN);
1917 memcpy(val, imsg.data, sizeof(*val));
1920 err = got_error(GOT_ERR_PRIVSEP_MSG);
1928 const struct got_error *
1929 got_privsep_send_gitconfig_remotes(struct imsgbuf *ibuf,
1930 struct got_remote_repo *remotes, int nremotes)
1932 const struct got_error *err = NULL;
1933 struct got_imsg_remotes iremotes;
1936 iremotes.nremotes = nremotes;
1937 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_REMOTES, 0, 0, -1,
1938 &iremotes, sizeof(iremotes)) == -1)
1939 return got_error_from_errno("imsg_compose GITCONFIG_REMOTES");
1941 err = flush_imsg(ibuf);
1946 for (i = 0; i < nremotes; i++) {
1947 struct got_imsg_remote iremote;
1948 size_t len = sizeof(iremote);
1951 iremote.mirror_references = remotes[i].mirror_references;
1952 iremote.name_len = strlen(remotes[i].name);
1953 len += iremote.name_len;
1954 iremote.url_len = strlen(remotes[i].url);
1955 len += iremote.url_len;
1957 wbuf = imsg_create(ibuf, GOT_IMSG_GITCONFIG_REMOTE, 0, 0, len);
1959 return got_error_from_errno(
1960 "imsg_create GITCONFIG_REMOTE");
1962 if (imsg_add(wbuf, &iremote, sizeof(iremote)) == -1) {
1963 err = got_error_from_errno(
1964 "imsg_add GITCONFIG_REMOTE");
1969 if (imsg_add(wbuf, remotes[i].name, iremote.name_len) == -1) {
1970 err = got_error_from_errno(
1971 "imsg_add GITCONFIG_REMOTE");
1975 if (imsg_add(wbuf, remotes[i].url, iremote.url_len) == -1) {
1976 err = got_error_from_errno(
1977 "imsg_add GITCONFIG_REMOTE");
1983 imsg_close(ibuf, wbuf);
1984 err = flush_imsg(ibuf);
1992 const struct got_error *
1993 got_privsep_recv_gitconfig_remotes(struct got_remote_repo **remotes,
1994 int *nremotes, struct imsgbuf *ibuf)
1996 const struct got_error *err = NULL;
1999 struct got_imsg_remotes iremotes;
2000 struct got_imsg_remote iremote;
2004 iremotes.nremotes = 0;
2006 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremotes));
2009 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2011 switch (imsg.hdr.type) {
2012 case GOT_IMSG_GITCONFIG_REMOTES:
2013 if (datalen != sizeof(iremotes)) {
2014 err = got_error(GOT_ERR_PRIVSEP_LEN);
2017 memcpy(&iremotes, imsg.data, sizeof(iremotes));
2018 if (iremotes.nremotes == 0) {
2025 return got_error(GOT_ERR_PRIVSEP_MSG);
2030 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(**remotes));
2031 if (*remotes == NULL)
2032 return got_error_from_errno("recallocarray");
2034 while (*nremotes < iremotes.nremotes) {
2035 struct got_remote_repo *remote;
2037 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremote));
2040 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2042 switch (imsg.hdr.type) {
2043 case GOT_IMSG_GITCONFIG_REMOTE:
2044 remote = &(*remotes)[*nremotes];
2045 if (datalen < sizeof(iremote)) {
2046 err = got_error(GOT_ERR_PRIVSEP_LEN);
2049 memcpy(&iremote, imsg.data, sizeof(iremote));
2050 if (iremote.name_len == 0 || iremote.url_len == 0 ||
2051 (sizeof(iremote) + iremote.name_len +
2052 iremote.url_len) > datalen) {
2053 err = got_error(GOT_ERR_PRIVSEP_LEN);
2056 remote->name = strndup(imsg.data + sizeof(iremote),
2058 if (remote->name == NULL) {
2059 err = got_error_from_errno("strndup");
2062 remote->url = strndup(imsg.data + sizeof(iremote) +
2063 iremote.name_len, iremote.url_len);
2064 if (remote->url == NULL) {
2065 err = got_error_from_errno("strndup");
2069 remote->mirror_references = iremote.mirror_references;
2073 err = got_error(GOT_ERR_PRIVSEP_MSG);
2084 for (i = 0; i < *nremotes; i++) {
2085 free((*remotes)[i].name);
2086 free((*remotes)[i].url);
2095 const struct got_error *
2096 got_privsep_send_commit_traversal_request(struct imsgbuf *ibuf,
2097 struct got_object_id *id, int idx, const char *path)
2099 const struct got_error *err = NULL;
2101 size_t path_len = strlen(path) + 1;
2103 wbuf = imsg_create(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_REQUEST, 0, 0,
2104 sizeof(struct got_imsg_commit_traversal_request) + path_len);
2106 return got_error_from_errno(
2107 "imsg_create COMMIT_TRAVERSAL_REQUEST");
2108 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
2109 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
2113 if (imsg_add(wbuf, &idx, sizeof(idx)) == -1) {
2114 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
2118 if (imsg_add(wbuf, path, path_len) == -1) {
2119 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
2125 imsg_close(ibuf, wbuf);
2127 return flush_imsg(ibuf);
2130 const struct got_error *
2131 got_privsep_send_traversed_commits(struct got_object_id *commit_ids,
2132 size_t ncommits, struct imsgbuf *ibuf)
2134 const struct got_error *err;
2138 wbuf = imsg_create(ibuf, GOT_IMSG_TRAVERSED_COMMITS, 0, 0,
2139 sizeof(struct got_imsg_traversed_commits) +
2140 ncommits * SHA1_DIGEST_LENGTH);
2142 return got_error_from_errno("imsg_create TRAVERSED_COMMITS");
2144 if (imsg_add(wbuf, &ncommits, sizeof(ncommits)) == -1) {
2145 err = got_error_from_errno("imsg_add TRAVERSED_COMMITS");
2149 for (i = 0; i < ncommits; i++) {
2150 struct got_object_id *id = &commit_ids[i];
2151 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
2152 err = got_error_from_errno(
2153 "imsg_add TRAVERSED_COMMITS");
2160 imsg_close(ibuf, wbuf);
2162 return flush_imsg(ibuf);
2165 const struct got_error *
2166 got_privsep_recv_traversed_commits(struct got_commit_object **changed_commit,
2167 struct got_object_id **changed_commit_id,
2168 struct got_object_id_queue *commit_ids, struct imsgbuf *ibuf)
2170 const struct got_error *err = NULL;
2172 struct got_imsg_traversed_commits *icommits;
2176 *changed_commit = NULL;
2177 *changed_commit_id = NULL;
2180 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2184 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2185 switch (imsg.hdr.type) {
2186 case GOT_IMSG_TRAVERSED_COMMITS:
2187 icommits = imsg.data;
2188 if (datalen != sizeof(*icommits) +
2189 icommits->ncommits * SHA1_DIGEST_LENGTH) {
2190 err = got_error(GOT_ERR_PRIVSEP_LEN);
2193 for (i = 0; i < icommits->ncommits; i++) {
2194 struct got_object_qid *qid;
2195 uint8_t *sha1 = (uint8_t *)imsg.data +
2196 sizeof(*icommits) + i * SHA1_DIGEST_LENGTH;
2197 err = got_object_qid_alloc_partial(&qid);
2200 memcpy(qid->id->sha1, sha1, SHA1_DIGEST_LENGTH);
2201 SIMPLEQ_INSERT_TAIL(commit_ids, qid, entry);
2203 /* The last commit may contain a change. */
2204 if (i == icommits->ncommits - 1) {
2205 *changed_commit_id =
2206 got_object_id_dup(qid->id);
2207 if (*changed_commit_id == NULL) {
2208 err = got_error_from_errno(
2209 "got_object_id_dup");
2215 case GOT_IMSG_COMMIT:
2216 if (*changed_commit_id == NULL) {
2217 err = got_error(GOT_ERR_PRIVSEP_MSG);
2220 err = get_commit_from_imsg(changed_commit, &imsg,
2223 case GOT_IMSG_COMMIT_TRAVERSAL_DONE:
2227 err = got_error(GOT_ERR_PRIVSEP_MSG);
2237 got_object_id_queue_free(commit_ids);
2241 const struct got_error *
2242 got_privsep_send_commit_traversal_done(struct imsgbuf *ibuf)
2244 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_DONE, 0, 0, -1,
2246 return got_error_from_errno("imsg_compose TRAVERSAL_DONE");
2248 return flush_imsg(ibuf);
2251 const struct got_error *
2252 got_privsep_unveil_exec_helpers(void)
2254 const char *helpers[] = {
2255 GOT_PATH_PROG_READ_PACK,
2256 GOT_PATH_PROG_READ_OBJECT,
2257 GOT_PATH_PROG_READ_COMMIT,
2258 GOT_PATH_PROG_READ_TREE,
2259 GOT_PATH_PROG_READ_BLOB,
2260 GOT_PATH_PROG_READ_TAG,
2261 GOT_PATH_PROG_READ_GITCONFIG,
2262 GOT_PATH_PROG_FETCH_PACK,
2263 GOT_PATH_PROG_INDEX_PACK,
2267 for (i = 0; i < nitems(helpers); i++) {
2268 if (unveil(helpers[i], "x") == 0)
2270 return got_error_from_errno2("unveil", helpers[i]);
2277 got_privsep_exec_child(int imsg_fds[2], const char *path, const char *repo_path)
2279 if (close(imsg_fds[0]) != 0) {
2280 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2284 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
2285 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2288 if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
2289 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2293 if (execl(path, path, repo_path, (char *)NULL) == -1) {
2294 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,