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)
417 const struct got_error *err = NULL;
419 size_t len, n_have_refs = 0;
420 struct got_pathlist_entry *pe;
422 len = sizeof(struct got_imsg_fetch_symrefs);
423 TAILQ_FOREACH(pe, have_refs, entry) {
424 struct got_object_id *id = pe->data;
425 len += sizeof(struct got_imsg_fetch_have_ref) +
426 pe->path_len + sizeof(id->sha1);
429 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
431 return got_error(GOT_ERR_NO_SPACE);
434 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_REQUEST, 0, 0, len);
437 return got_error_from_errno("imsg_create FETCH_REQUEST");
440 /* Keep in sync with struct got_imsg_fetch_have_refs definition! */
441 if (imsg_add(wbuf, &n_have_refs, sizeof(n_have_refs)) == -1) {
442 err = got_error_from_errno("imsg_add FETCH_REQUEST");
448 TAILQ_FOREACH(pe, have_refs, entry) {
449 const char *name = pe->path;
450 size_t name_len = pe->path_len;
451 struct got_object_id *id = pe->data;
453 /* Keep in sync with struct got_imsg_fetch_have_ref! */
454 if (imsg_add(wbuf, id->sha1, sizeof(id->sha1)) == -1) {
455 err = got_error_from_errno("imsg_add FETCH_REQUEST");
460 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
461 err = got_error_from_errno("imsg_add FETCH_REQUEST");
466 if (imsg_add(wbuf, name, name_len) == -1) {
467 err = got_error_from_errno("imsg_add FETCH_REQUEST");
475 imsg_close(ibuf, wbuf);
476 return flush_imsg(ibuf);
479 const struct got_error *
480 got_privsep_send_fetch_outfd(struct imsgbuf *ibuf, int fd)
482 return send_fd(ibuf, GOT_IMSG_FETCH_OUTFD, fd);
486 const struct got_error *
487 got_privsep_send_fetch_symrefs(struct imsgbuf *ibuf,
488 struct got_pathlist_head *symrefs)
490 const struct got_error *err = NULL;
492 size_t len, nsymrefs = 0;
493 struct got_pathlist_entry *pe;
495 len = sizeof(struct got_imsg_fetch_symrefs);
496 TAILQ_FOREACH(pe, symrefs, entry) {
497 const char *target = pe->data;
498 len += sizeof(struct got_imsg_fetch_symref) +
499 pe->path_len + strlen(target);
503 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
504 return got_error(GOT_ERR_NO_SPACE);
506 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_SYMREFS, 0, 0, len);
508 return got_error_from_errno("imsg_create FETCH_SYMREFS");
510 /* Keep in sync with struct got_imsg_fetch_symrefs definition! */
511 if (imsg_add(wbuf, &nsymrefs, sizeof(nsymrefs)) == -1) {
512 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
517 TAILQ_FOREACH(pe, symrefs, entry) {
518 const char *name = pe->path;
519 size_t name_len = pe->path_len;
520 const char *target = pe->data;
521 size_t target_len = strlen(target);
523 /* Keep in sync with struct got_imsg_fetch_symref definition! */
524 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
525 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
529 if (imsg_add(wbuf, &target_len, sizeof(target_len)) == -1) {
530 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
534 if (imsg_add(wbuf, name, name_len) == -1) {
535 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
539 if (imsg_add(wbuf, target, target_len) == -1) {
540 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
547 imsg_close(ibuf, wbuf);
548 return flush_imsg(ibuf);
551 const struct got_error *
552 got_privsep_send_fetch_ref(struct imsgbuf *ibuf,
553 struct got_object_id *refid, const char *refname)
555 const struct got_error *err = NULL;
557 size_t len, reflen = strlen(refname);
559 len = sizeof(struct got_imsg_fetch_ref) + reflen;
560 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
561 return got_error(GOT_ERR_NO_SPACE);
563 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_REF, 0, 0, len);
565 return got_error_from_errno("imsg_create FETCH_REF");
567 /* Keep in sync with struct got_imsg_fetch_ref definition! */
568 if (imsg_add(wbuf, refid->sha1, SHA1_DIGEST_LENGTH) == -1) {
569 err = got_error_from_errno("imsg_add FETCH_REF");
573 if (imsg_add(wbuf, refname, reflen) == -1) {
574 err = got_error_from_errno("imsg_add FETCH_REF");
580 imsg_close(ibuf, wbuf);
581 return flush_imsg(ibuf);
584 const struct got_error *
585 got_privsep_send_fetch_server_progress(struct imsgbuf *ibuf, const char *msg,
588 if (msglen > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
589 return got_error(GOT_ERR_NO_SPACE);
594 if (imsg_compose(ibuf, GOT_IMSG_FETCH_SERVER_PROGRESS, 0, 0, -1,
596 return got_error_from_errno(
597 "imsg_compose FETCH_SERVER_PROGRESS");
599 return flush_imsg(ibuf);
602 const struct got_error *
603 got_privsep_send_fetch_download_progress(struct imsgbuf *ibuf, off_t bytes)
605 if (imsg_compose(ibuf, GOT_IMSG_FETCH_DOWNLOAD_PROGRESS, 0, 0, -1,
606 &bytes, sizeof(bytes)) == -1)
607 return got_error_from_errno(
608 "imsg_compose FETCH_DOWNLOAD_PROGRESS");
610 return flush_imsg(ibuf);
613 const struct got_error *
614 got_privsep_send_fetch_done(struct imsgbuf *ibuf, struct got_object_id hash)
616 if (imsg_compose(ibuf, GOT_IMSG_FETCH_DONE, 0, 0, -1,
617 hash.sha1, SHA1_DIGEST_LENGTH) == -1)
618 return got_error_from_errno("imsg_compose FETCH");
619 return flush_imsg(ibuf);
623 const struct got_error *
624 got_privsep_recv_fetch_progress(int *done, struct got_object_id **id,
625 char **refname, struct got_pathlist_head *symrefs, char **server_progress,
626 off_t *packfile_size, struct imsgbuf *ibuf)
628 const struct got_error *err = NULL;
631 struct got_imsg_fetch_symrefs *isymrefs = NULL;
639 *server_progress = NULL;
642 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
646 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
647 switch (imsg.hdr.type) {
649 if (datalen < sizeof(struct got_imsg_error)) {
650 err = got_error(GOT_ERR_PRIVSEP_LEN);
653 err = recv_imsg_error(&imsg, datalen);
655 case GOT_IMSG_FETCH_SYMREFS:
656 if (datalen < sizeof(*isymrefs)) {
657 err = got_error(GOT_ERR_PRIVSEP_LEN);
660 if (isymrefs != NULL) {
661 err = got_error(GOT_ERR_PRIVSEP_MSG);
664 isymrefs = (struct got_imsg_fetch_symrefs *)imsg.data;
665 off = sizeof(*isymrefs);
666 remain = datalen - off;
667 for (n = 0; n < isymrefs->nsymrefs; n++) {
668 struct got_imsg_fetch_symref *s;
670 if (remain < sizeof(struct got_imsg_fetch_symref)) {
671 err = got_error(GOT_ERR_PRIVSEP_LEN);
674 s = (struct got_imsg_fetch_symref *)(imsg.data + off);
676 remain -= sizeof(*s);
677 if (remain < s->name_len) {
678 err = got_error(GOT_ERR_PRIVSEP_LEN);
681 name = strndup(imsg.data + off, s->name_len);
683 err = got_error_from_errno("strndup");
687 remain -= s->name_len;
688 if (remain < s->target_len) {
689 err = got_error(GOT_ERR_PRIVSEP_LEN);
693 target = strndup(imsg.data + off, s->target_len);
694 if (target == NULL) {
695 err = got_error_from_errno("strndup");
699 off += s->target_len;
700 remain -= s->target_len;
701 err = got_pathlist_append(symrefs, name, target);
709 case GOT_IMSG_FETCH_REF:
710 if (datalen <= SHA1_DIGEST_LENGTH) {
711 err = got_error(GOT_ERR_PRIVSEP_MSG);
714 *id = malloc(sizeof(**id));
716 err = got_error_from_errno("malloc");
719 memcpy((*id)->sha1, imsg.data, SHA1_DIGEST_LENGTH);
720 *refname = strndup(imsg.data + SHA1_DIGEST_LENGTH,
721 datalen - SHA1_DIGEST_LENGTH);
722 if (*refname == NULL) {
723 err = got_error_from_errno("strndup");
727 case GOT_IMSG_FETCH_SERVER_PROGRESS:
729 err = got_error(GOT_ERR_PRIVSEP_LEN);
732 *server_progress = strndup(imsg.data, datalen);
733 if (*server_progress == NULL) {
734 err = got_error_from_errno("strndup");
737 for (i = 0; i < datalen; i++) {
738 if (!isprint((unsigned char)(*server_progress)[i]) &&
739 !isspace((unsigned char)(*server_progress)[i])) {
740 err = got_error(GOT_ERR_PRIVSEP_MSG);
741 free(*server_progress);
742 *server_progress = NULL;
747 case GOT_IMSG_FETCH_DOWNLOAD_PROGRESS:
748 if (datalen < sizeof(*packfile_size)) {
749 err = got_error(GOT_ERR_PRIVSEP_MSG);
752 memcpy(packfile_size, imsg.data, sizeof(*packfile_size));
754 case GOT_IMSG_FETCH_DONE:
755 *id = malloc(sizeof(**id));
757 err = got_error_from_errno("malloc");
760 if (datalen != SHA1_DIGEST_LENGTH) {
761 err = got_error(GOT_ERR_PRIVSEP_MSG);
764 memcpy((*id)->sha1, imsg.data, SHA1_DIGEST_LENGTH);
768 err = got_error(GOT_ERR_PRIVSEP_MSG);
782 const struct got_error *
783 got_privsep_send_index_pack_req(struct imsgbuf *ibuf, uint8_t *pack_hash,
786 const struct got_error *err = NULL;
788 /* Keep in sync with struct got_imsg_index_pack_request */
789 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_REQUEST, 0, 0, fd,
790 pack_hash, SHA1_DIGEST_LENGTH) == -1) {
791 err = got_error_from_errno("imsg_compose INDEX_REQUEST");
795 return flush_imsg(ibuf);
798 const struct got_error *
799 got_privsep_send_index_pack_outfd(struct imsgbuf *ibuf, int fd)
801 return send_fd(ibuf, GOT_IMSG_IDXPACK_OUTFD, fd);
804 const struct got_error *
805 got_privsep_send_index_pack_progress(struct imsgbuf *ibuf, int nobj_total,
806 int nobj_indexed, int nobj_loose, int nobj_resolved)
808 struct got_imsg_index_pack_progress iprogress;
810 iprogress.nobj_total = nobj_total;
811 iprogress.nobj_indexed = nobj_indexed;
812 iprogress.nobj_loose = nobj_loose;
813 iprogress.nobj_resolved = nobj_resolved;
815 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_PROGRESS, 0, 0, -1,
816 &iprogress, sizeof(iprogress)) == -1)
817 return got_error_from_errno("imsg_compose IDXPACK_PROGRESS");
819 return flush_imsg(ibuf);
822 const struct got_error *
823 got_privsep_send_index_pack_done(struct imsgbuf *ibuf)
825 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_DONE, 0, 0, -1, NULL, 0) == -1)
826 return got_error_from_errno("imsg_compose FETCH");
827 return flush_imsg(ibuf);
830 const struct got_error *
831 got_privsep_recv_index_progress(int *done, int *nobj_total,
832 int *nobj_indexed, int *nobj_loose, int *nobj_resolved,
833 struct imsgbuf *ibuf)
835 const struct got_error *err = NULL;
837 struct got_imsg_index_pack_progress *iprogress;
845 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
849 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
850 switch (imsg.hdr.type) {
852 if (datalen < sizeof(struct got_imsg_error)) {
853 err = got_error(GOT_ERR_PRIVSEP_LEN);
856 err = recv_imsg_error(&imsg, datalen);
858 case GOT_IMSG_IDXPACK_PROGRESS:
859 if (datalen < sizeof(*iprogress)) {
860 err = got_error(GOT_ERR_PRIVSEP_LEN);
863 iprogress = (struct got_imsg_index_pack_progress *)imsg.data;
864 *nobj_total = iprogress->nobj_total;
865 *nobj_indexed = iprogress->nobj_indexed;
866 *nobj_loose = iprogress->nobj_loose;
867 *nobj_resolved = iprogress->nobj_resolved;
869 case GOT_IMSG_IDXPACK_DONE:
871 err = got_error(GOT_ERR_PRIVSEP_LEN);
877 err = got_error(GOT_ERR_PRIVSEP_MSG);
885 const struct got_error *
886 got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
887 struct imsgbuf *ibuf)
889 const struct got_error *err = NULL;
890 struct got_imsg_object *iobj;
891 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
893 if (datalen != sizeof(*iobj))
894 return got_error(GOT_ERR_PRIVSEP_LEN);
897 *obj = calloc(1, sizeof(**obj));
899 return got_error_from_errno("calloc");
901 memcpy((*obj)->id.sha1, iobj->id, SHA1_DIGEST_LENGTH);
902 (*obj)->type = iobj->type;
903 (*obj)->flags = iobj->flags;
904 (*obj)->hdrlen = iobj->hdrlen;
905 (*obj)->size = iobj->size;
906 /* path_packfile is handled by caller */
907 if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
908 (*obj)->pack_offset = iobj->pack_offset;
909 (*obj)->pack_idx = iobj->pack_idx;
915 const struct got_error *
916 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
918 const struct got_error *err = NULL;
920 const size_t min_datalen =
921 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
925 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
929 switch (imsg.hdr.type) {
930 case GOT_IMSG_OBJECT:
931 err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
934 err = got_error(GOT_ERR_PRIVSEP_MSG);
943 static const struct got_error *
944 send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
947 const struct got_error *err = NULL;
948 size_t offset, remain;
953 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
955 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
956 commit->logmsg + offset, n) == -1) {
957 err = got_error_from_errno("imsg_compose "
962 err = flush_imsg(ibuf);
973 const struct got_error *
974 got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
976 const struct got_error *err = NULL;
977 struct got_imsg_commit_object *icommit;
980 struct got_object_qid *qid;
981 size_t author_len = strlen(commit->author);
982 size_t committer_len = strlen(commit->committer);
983 size_t logmsg_len = strlen(commit->logmsg);
985 total = sizeof(*icommit) + author_len + committer_len +
986 commit->nparents * SHA1_DIGEST_LENGTH;
990 return got_error_from_errno("malloc");
992 icommit = (struct got_imsg_commit_object *)buf;
993 memcpy(icommit->tree_id, commit->tree_id->sha1,
994 sizeof(icommit->tree_id));
995 icommit->author_len = author_len;
996 icommit->author_time = commit->author_time;
997 icommit->author_gmtoff = commit->author_gmtoff;
998 icommit->committer_len = committer_len;
999 icommit->committer_time = commit->committer_time;
1000 icommit->committer_gmtoff = commit->committer_gmtoff;
1001 icommit->logmsg_len = logmsg_len;
1002 icommit->nparents = commit->nparents;
1004 len = sizeof(*icommit);
1005 memcpy(buf + len, commit->author, author_len);
1007 memcpy(buf + len, commit->committer, committer_len);
1008 len += committer_len;
1009 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
1010 memcpy(buf + len, qid->id, SHA1_DIGEST_LENGTH);
1011 len += SHA1_DIGEST_LENGTH;
1014 if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
1015 err = got_error_from_errno("imsg_compose COMMIT");
1019 if (logmsg_len == 0 ||
1020 logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1021 err = flush_imsg(ibuf);
1025 err = send_commit_logmsg(ibuf, commit, logmsg_len);
1031 static const struct got_error *
1032 get_commit_from_imsg(struct got_commit_object **commit,
1033 struct imsg *imsg, size_t datalen, struct imsgbuf *ibuf)
1035 const struct got_error *err = NULL;
1036 struct got_imsg_commit_object *icommit;
1040 if (datalen < sizeof(*icommit))
1041 return got_error(GOT_ERR_PRIVSEP_LEN);
1043 icommit = imsg->data;
1044 if (datalen != sizeof(*icommit) + icommit->author_len +
1045 icommit->committer_len +
1046 icommit->nparents * SHA1_DIGEST_LENGTH)
1047 return got_error(GOT_ERR_PRIVSEP_LEN);
1049 if (icommit->nparents < 0)
1050 return got_error(GOT_ERR_PRIVSEP_LEN);
1052 len += sizeof(*icommit);
1054 *commit = got_object_commit_alloc_partial();
1055 if (*commit == NULL)
1056 return got_error_from_errno(
1057 "got_object_commit_alloc_partial");
1059 memcpy((*commit)->tree_id->sha1, icommit->tree_id,
1060 SHA1_DIGEST_LENGTH);
1061 (*commit)->author_time = icommit->author_time;
1062 (*commit)->author_gmtoff = icommit->author_gmtoff;
1063 (*commit)->committer_time = icommit->committer_time;
1064 (*commit)->committer_gmtoff = icommit->committer_gmtoff;
1066 if (icommit->author_len == 0) {
1067 (*commit)->author = strdup("");
1068 if ((*commit)->author == NULL) {
1069 err = got_error_from_errno("strdup");
1073 (*commit)->author = malloc(icommit->author_len + 1);
1074 if ((*commit)->author == NULL) {
1075 err = got_error_from_errno("malloc");
1078 memcpy((*commit)->author, imsg->data + len,
1079 icommit->author_len);
1080 (*commit)->author[icommit->author_len] = '\0';
1082 len += icommit->author_len;
1084 if (icommit->committer_len == 0) {
1085 (*commit)->committer = strdup("");
1086 if ((*commit)->committer == NULL) {
1087 err = got_error_from_errno("strdup");
1091 (*commit)->committer =
1092 malloc(icommit->committer_len + 1);
1093 if ((*commit)->committer == NULL) {
1094 err = got_error_from_errno("malloc");
1097 memcpy((*commit)->committer, imsg->data + len,
1098 icommit->committer_len);
1099 (*commit)->committer[icommit->committer_len] = '\0';
1101 len += icommit->committer_len;
1103 if (icommit->logmsg_len == 0) {
1104 (*commit)->logmsg = strdup("");
1105 if ((*commit)->logmsg == NULL) {
1106 err = got_error_from_errno("strdup");
1110 size_t offset = 0, remain = icommit->logmsg_len;
1112 (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
1113 if ((*commit)->logmsg == NULL) {
1114 err = got_error_from_errno("malloc");
1117 while (remain > 0) {
1118 struct imsg imsg_log;
1119 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1122 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1126 if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG) {
1127 err = got_error(GOT_ERR_PRIVSEP_MSG);
1131 memcpy((*commit)->logmsg + offset,
1133 imsg_free(&imsg_log);
1137 (*commit)->logmsg[icommit->logmsg_len] = '\0';
1140 for (i = 0; i < icommit->nparents; i++) {
1141 struct got_object_qid *qid;
1143 err = got_object_qid_alloc_partial(&qid);
1146 memcpy(qid->id, imsg->data + len +
1147 i * SHA1_DIGEST_LENGTH, sizeof(*qid->id));
1148 SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
1149 (*commit)->nparents++;
1153 got_object_commit_close(*commit);
1159 const struct got_error *
1160 got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
1162 const struct got_error *err = NULL;
1165 const size_t min_datalen =
1166 MIN(sizeof(struct got_imsg_error),
1167 sizeof(struct got_imsg_commit_object));
1171 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1175 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1177 switch (imsg.hdr.type) {
1178 case GOT_IMSG_COMMIT:
1179 err = get_commit_from_imsg(commit, &imsg, datalen, ibuf);
1182 err = got_error(GOT_ERR_PRIVSEP_MSG);
1191 const struct got_error *
1192 got_privsep_send_tree(struct imsgbuf *ibuf, struct got_pathlist_head *entries,
1195 const struct got_error *err = NULL;
1196 struct got_imsg_tree_object itree;
1197 struct got_pathlist_entry *pe;
1199 int nimsg; /* number of imsg queued in ibuf */
1201 itree.nentries = nentries;
1202 if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
1204 return got_error_from_errno("imsg_compose TREE");
1206 totlen = sizeof(itree);
1208 TAILQ_FOREACH(pe, entries, entry) {
1209 const char *name = pe->path;
1210 struct got_parsed_tree_entry *pte = pe->data;
1212 size_t namelen = strlen(name);
1213 size_t len = sizeof(struct got_imsg_tree_entry) + namelen;
1215 if (len > MAX_IMSGSIZE)
1216 return got_error(GOT_ERR_NO_SPACE);
1219 if (totlen + len >= MAX_IMSGSIZE - (IMSG_HEADER_SIZE * nimsg)) {
1220 err = flush_imsg(ibuf);
1226 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_ENTRY, 0, 0, len);
1228 return got_error_from_errno("imsg_create TREE_ENTRY");
1230 /* Keep in sync with struct got_imsg_tree_object definition! */
1231 if (imsg_add(wbuf, pte->id, SHA1_DIGEST_LENGTH) == -1) {
1232 err = got_error_from_errno("imsg_add TREE_ENTRY");
1236 if (imsg_add(wbuf, &pte->mode, sizeof(pte->mode)) == -1) {
1237 err = got_error_from_errno("imsg_add TREE_ENTRY");
1242 if (imsg_add(wbuf, name, namelen) == -1) {
1243 err = got_error_from_errno("imsg_add TREE_ENTRY");
1249 imsg_close(ibuf, wbuf);
1254 return flush_imsg(ibuf);
1257 const struct got_error *
1258 got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
1260 const struct got_error *err = NULL;
1261 const size_t min_datalen =
1262 MIN(sizeof(struct got_imsg_error),
1263 sizeof(struct got_imsg_tree_object));
1264 struct got_imsg_tree_object *itree;
1269 err = read_imsg(ibuf);
1277 struct got_imsg_tree_entry *ite;
1278 struct got_tree_entry *te = NULL;
1280 n = imsg_get(ibuf, &imsg);
1282 if (*tree && (*tree)->nentries != nentries)
1287 if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen)
1288 return got_error(GOT_ERR_PRIVSEP_LEN);
1290 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1292 switch (imsg.hdr.type) {
1293 case GOT_IMSG_ERROR:
1294 err = recv_imsg_error(&imsg, datalen);
1297 /* This message should only appear once. */
1298 if (*tree != NULL) {
1299 err = got_error(GOT_ERR_PRIVSEP_MSG);
1302 if (datalen != sizeof(*itree)) {
1303 err = got_error(GOT_ERR_PRIVSEP_LEN);
1307 *tree = malloc(sizeof(**tree));
1308 if (*tree == NULL) {
1309 err = got_error_from_errno("malloc");
1312 (*tree)->entries = calloc(itree->nentries,
1313 sizeof(struct got_tree_entry));
1314 if ((*tree)->entries == NULL) {
1315 err = got_error_from_errno("malloc");
1318 (*tree)->nentries = itree->nentries;
1319 (*tree)->refcnt = 0;
1321 case GOT_IMSG_TREE_ENTRY:
1322 /* This message should be preceeded by GOT_IMSG_TREE. */
1323 if (*tree == NULL) {
1324 err = got_error(GOT_ERR_PRIVSEP_MSG);
1327 if (datalen < sizeof(*ite) || datalen > MAX_IMSGSIZE) {
1328 err = got_error(GOT_ERR_PRIVSEP_LEN);
1332 /* Remaining data contains the entry's name. */
1333 datalen -= sizeof(*ite);
1334 if (datalen == 0 || datalen > MAX_IMSGSIZE) {
1335 err = got_error(GOT_ERR_PRIVSEP_LEN);
1340 if (datalen + 1 > sizeof(te->name)) {
1341 err = got_error(GOT_ERR_NO_SPACE);
1344 te = &(*tree)->entries[nentries];
1345 memcpy(te->name, imsg.data + sizeof(*ite), datalen);
1346 te->name[datalen] = '\0';
1348 memcpy(te->id.sha1, ite->id, SHA1_DIGEST_LENGTH);
1349 te->mode = ite->mode;
1354 err = got_error(GOT_ERR_PRIVSEP_MSG);
1361 if (*tree && (*tree)->nentries != nentries) {
1363 err = got_error(GOT_ERR_PRIVSEP_LEN);
1364 got_object_tree_close(*tree);
1371 const struct got_error *
1372 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
1373 const uint8_t *data)
1375 struct got_imsg_blob iblob;
1378 iblob.hdrlen = hdrlen;
1383 if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
1384 return got_error(GOT_ERR_NO_SPACE);
1386 buf = malloc(sizeof(iblob) + size);
1388 return got_error_from_errno("malloc");
1390 memcpy(buf, &iblob, sizeof(iblob));
1391 memcpy(buf + sizeof(iblob), data, size);
1392 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
1393 sizeof(iblob) + size) == -1) {
1395 return got_error_from_errno("imsg_compose BLOB");
1399 /* Data has already been written to file descriptor. */
1400 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
1401 sizeof(iblob)) == -1)
1402 return got_error_from_errno("imsg_compose BLOB");
1406 return flush_imsg(ibuf);
1409 const struct got_error *
1410 got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1411 struct imsgbuf *ibuf)
1413 const struct got_error *err = NULL;
1415 struct got_imsg_blob *iblob;
1420 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1424 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1426 switch (imsg.hdr.type) {
1428 if (datalen < sizeof(*iblob)) {
1429 err = got_error(GOT_ERR_PRIVSEP_LEN);
1433 *size = iblob->size;
1434 *hdrlen = iblob->hdrlen;
1436 if (datalen == sizeof(*iblob)) {
1437 /* Data has been written to file descriptor. */
1441 if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX) {
1442 err = got_error(GOT_ERR_PRIVSEP_LEN);
1446 *outbuf = malloc(*size);
1447 if (*outbuf == NULL) {
1448 err = got_error_from_errno("malloc");
1451 memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
1454 err = got_error(GOT_ERR_PRIVSEP_MSG);
1463 static const struct got_error *
1464 send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
1466 const struct got_error *err = NULL;
1467 size_t offset, remain;
1470 remain = tagmsg_len;
1471 while (remain > 0) {
1472 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1474 if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
1475 tag->tagmsg + offset, n) == -1) {
1476 err = got_error_from_errno("imsg_compose TAG_TAGMSG");
1480 err = flush_imsg(ibuf);
1491 const struct got_error *
1492 got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
1494 const struct got_error *err = NULL;
1495 struct got_imsg_tag_object *itag;
1498 size_t tag_len = strlen(tag->tag);
1499 size_t tagger_len = strlen(tag->tagger);
1500 size_t tagmsg_len = strlen(tag->tagmsg);
1502 total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
1504 buf = malloc(total);
1506 return got_error_from_errno("malloc");
1508 itag = (struct got_imsg_tag_object *)buf;
1509 memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
1510 itag->obj_type = tag->obj_type;
1511 itag->tag_len = tag_len;
1512 itag->tagger_len = tagger_len;
1513 itag->tagger_time = tag->tagger_time;
1514 itag->tagger_gmtoff = tag->tagger_gmtoff;
1515 itag->tagmsg_len = tagmsg_len;
1517 len = sizeof(*itag);
1518 memcpy(buf + len, tag->tag, tag_len);
1520 memcpy(buf + len, tag->tagger, tagger_len);
1523 if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
1524 err = got_error_from_errno("imsg_compose TAG");
1528 if (tagmsg_len == 0 ||
1529 tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1530 err = flush_imsg(ibuf);
1534 err = send_tagmsg(ibuf, tag, tagmsg_len);
1540 const struct got_error *
1541 got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
1543 const struct got_error *err = NULL;
1545 struct got_imsg_tag_object *itag;
1546 size_t len, datalen;
1547 const size_t min_datalen =
1548 MIN(sizeof(struct got_imsg_error),
1549 sizeof(struct got_imsg_tag_object));
1553 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1557 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1560 switch (imsg.hdr.type) {
1562 if (datalen < sizeof(*itag)) {
1563 err = got_error(GOT_ERR_PRIVSEP_LEN);
1567 if (datalen != sizeof(*itag) + itag->tag_len +
1569 err = got_error(GOT_ERR_PRIVSEP_LEN);
1572 len += sizeof(*itag);
1574 *tag = calloc(1, sizeof(**tag));
1576 err = got_error_from_errno("calloc");
1580 memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
1582 if (itag->tag_len == 0) {
1583 (*tag)->tag = strdup("");
1584 if ((*tag)->tag == NULL) {
1585 err = got_error_from_errno("strdup");
1589 (*tag)->tag = malloc(itag->tag_len + 1);
1590 if ((*tag)->tag == NULL) {
1591 err = got_error_from_errno("malloc");
1594 memcpy((*tag)->tag, imsg.data + len,
1596 (*tag)->tag[itag->tag_len] = '\0';
1598 len += itag->tag_len;
1600 (*tag)->obj_type = itag->obj_type;
1601 (*tag)->tagger_time = itag->tagger_time;
1602 (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1604 if (itag->tagger_len == 0) {
1605 (*tag)->tagger = strdup("");
1606 if ((*tag)->tagger == NULL) {
1607 err = got_error_from_errno("strdup");
1611 (*tag)->tagger = malloc(itag->tagger_len + 1);
1612 if ((*tag)->tagger == NULL) {
1613 err = got_error_from_errno("malloc");
1616 memcpy((*tag)->tagger, imsg.data + len,
1618 (*tag)->tagger[itag->tagger_len] = '\0';
1620 len += itag->tagger_len;
1622 if (itag->tagmsg_len == 0) {
1623 (*tag)->tagmsg = strdup("");
1624 if ((*tag)->tagmsg == NULL) {
1625 err = got_error_from_errno("strdup");
1629 size_t offset = 0, remain = itag->tagmsg_len;
1631 (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1632 if ((*tag)->tagmsg == NULL) {
1633 err = got_error_from_errno("malloc");
1636 while (remain > 0) {
1637 struct imsg imsg_log;
1638 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1641 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1645 if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1646 return got_error(GOT_ERR_PRIVSEP_MSG);
1648 memcpy((*tag)->tagmsg + offset, imsg_log.data,
1650 imsg_free(&imsg_log);
1654 (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1659 err = got_error(GOT_ERR_PRIVSEP_MSG);
1668 const struct got_error *
1669 got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1670 struct got_packidx *packidx)
1672 const struct got_error *err = NULL;
1673 struct got_imsg_packidx ipackidx;
1674 struct got_imsg_pack ipack;
1677 ipackidx.len = packidx->len;
1678 fd = dup(packidx->fd);
1680 return got_error_from_errno("dup");
1682 if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
1683 sizeof(ipackidx)) == -1) {
1684 err = got_error_from_errno("imsg_compose PACKIDX");
1689 if (strlcpy(ipack.path_packfile, pack->path_packfile,
1690 sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
1691 return got_error(GOT_ERR_NO_SPACE);
1692 ipack.filesize = pack->filesize;
1696 return got_error_from_errno("dup");
1698 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
1700 err = got_error_from_errno("imsg_compose PACK");
1705 return flush_imsg(ibuf);
1708 const struct got_error *
1709 got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
1710 struct got_object_id *id)
1712 struct got_imsg_packed_object iobj;
1715 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
1717 if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
1718 &iobj, sizeof(iobj)) == -1)
1719 return got_error_from_errno("imsg_compose "
1720 "PACKED_OBJECT_REQUEST");
1722 return flush_imsg(ibuf);
1725 const struct got_error *
1726 got_privsep_send_gitconfig_parse_req(struct imsgbuf *ibuf, int fd)
1728 const struct got_error *err = NULL;
1730 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_PARSE_REQUEST, 0, 0, fd,
1732 err = got_error_from_errno("imsg_compose "
1733 "GITCONFIG_PARSE_REQUEST");
1738 return flush_imsg(ibuf);
1741 const struct got_error *
1742 got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *ibuf)
1744 if (imsg_compose(ibuf,
1745 GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST, 0, 0, -1,
1747 return got_error_from_errno("imsg_compose "
1748 "GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST");
1750 return flush_imsg(ibuf);
1753 const struct got_error *
1754 got_privsep_send_gitconfig_author_name_req(struct imsgbuf *ibuf)
1756 if (imsg_compose(ibuf,
1757 GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST, 0, 0, -1, NULL, 0) == -1)
1758 return got_error_from_errno("imsg_compose "
1759 "GITCONFIG_AUTHOR_NAME_REQUEST");
1761 return flush_imsg(ibuf);
1764 const struct got_error *
1765 got_privsep_send_gitconfig_author_email_req(struct imsgbuf *ibuf)
1767 if (imsg_compose(ibuf,
1768 GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST, 0, 0, -1, NULL, 0) == -1)
1769 return got_error_from_errno("imsg_compose "
1770 "GITCONFIG_AUTHOR_EMAIL_REQUEST");
1772 return flush_imsg(ibuf);
1775 const struct got_error *
1776 got_privsep_send_gitconfig_remotes_req(struct imsgbuf *ibuf)
1778 if (imsg_compose(ibuf,
1779 GOT_IMSG_GITCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
1780 return got_error_from_errno("imsg_compose "
1781 "GITCONFIG_REMOTE_REQUEST");
1783 return flush_imsg(ibuf);
1786 const struct got_error *
1787 got_privsep_send_gitconfig_owner_req(struct imsgbuf *ibuf)
1789 if (imsg_compose(ibuf,
1790 GOT_IMSG_GITCONFIG_OWNER_REQUEST, 0, 0, -1, NULL, 0) == -1)
1791 return got_error_from_errno("imsg_compose "
1792 "GITCONFIG_OWNER_REQUEST");
1794 return flush_imsg(ibuf);
1797 const struct got_error *
1798 got_privsep_send_gitconfig_str(struct imsgbuf *ibuf, const char *value)
1800 size_t len = value ? strlen(value) + 1 : 0;
1802 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_STR_VAL, 0, 0, -1,
1804 return got_error_from_errno("imsg_compose GITCONFIG_STR_VAL");
1806 return flush_imsg(ibuf);
1809 const struct got_error *
1810 got_privsep_recv_gitconfig_str(char **str, struct imsgbuf *ibuf)
1812 const struct got_error *err = NULL;
1815 const size_t min_datalen = 0;
1819 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1822 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1824 switch (imsg.hdr.type) {
1825 case GOT_IMSG_GITCONFIG_STR_VAL:
1828 *str = malloc(datalen);
1830 err = got_error_from_errno("malloc");
1833 if (strlcpy(*str, imsg.data, datalen) >= datalen)
1834 err = got_error(GOT_ERR_NO_SPACE);
1837 err = got_error(GOT_ERR_PRIVSEP_MSG);
1845 const struct got_error *
1846 got_privsep_send_gitconfig_int(struct imsgbuf *ibuf, int value)
1848 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_INT_VAL, 0, 0, -1,
1849 &value, sizeof(value)) == -1)
1850 return got_error_from_errno("imsg_compose GITCONFIG_INT_VAL");
1852 return flush_imsg(ibuf);
1855 const struct got_error *
1856 got_privsep_recv_gitconfig_int(int *val, struct imsgbuf *ibuf)
1858 const struct got_error *err = NULL;
1861 const size_t min_datalen =
1862 MIN(sizeof(struct got_imsg_error), sizeof(int));
1866 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1869 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1871 switch (imsg.hdr.type) {
1872 case GOT_IMSG_GITCONFIG_INT_VAL:
1873 if (datalen != sizeof(*val)) {
1874 err = got_error(GOT_ERR_PRIVSEP_LEN);
1877 memcpy(val, imsg.data, sizeof(*val));
1880 err = got_error(GOT_ERR_PRIVSEP_MSG);
1888 const struct got_error *
1889 got_privsep_send_gitconfig_remotes(struct imsgbuf *ibuf,
1890 struct got_remote_repo *remotes, int nremotes)
1892 const struct got_error *err = NULL;
1893 struct got_imsg_remotes iremotes;
1896 iremotes.nremotes = nremotes;
1897 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_REMOTES, 0, 0, -1,
1898 &iremotes, sizeof(iremotes)) == -1)
1899 return got_error_from_errno("imsg_compose GITCONFIG_REMOTES");
1901 err = flush_imsg(ibuf);
1906 for (i = 0; i < nremotes; i++) {
1907 struct got_imsg_remote iremote;
1908 size_t len = sizeof(iremote);
1911 iremote.name_len = strlen(remotes[i].name);
1912 len += iremote.name_len;
1913 iremote.url_len = strlen(remotes[i].url);
1914 len += iremote.url_len;
1916 wbuf = imsg_create(ibuf, GOT_IMSG_GITCONFIG_REMOTE, 0, 0, len);
1918 return got_error_from_errno(
1919 "imsg_create GITCONFIG_REMOTE");
1921 if (imsg_add(wbuf, &iremote, sizeof(iremote)) == -1) {
1922 err = got_error_from_errno(
1923 "imsg_add GIITCONFIG_REMOTE");
1928 if (imsg_add(wbuf, remotes[i].name, iremote.name_len) == -1) {
1929 err = got_error_from_errno(
1930 "imsg_add GIITCONFIG_REMOTE");
1934 if (imsg_add(wbuf, remotes[i].url, iremote.url_len) == -1) {
1935 err = got_error_from_errno(
1936 "imsg_add GIITCONFIG_REMOTE");
1942 imsg_close(ibuf, wbuf);
1943 err = flush_imsg(ibuf);
1951 const struct got_error *
1952 got_privsep_recv_gitconfig_remotes(struct got_remote_repo **remotes,
1953 int *nremotes, struct imsgbuf *ibuf)
1955 const struct got_error *err = NULL;
1958 struct got_imsg_remotes iremotes;
1959 struct got_imsg_remote iremote;
1963 iremotes.nremotes = 0;
1965 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremotes));
1968 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1970 switch (imsg.hdr.type) {
1971 case GOT_IMSG_GITCONFIG_REMOTES:
1972 if (datalen != sizeof(iremotes)) {
1973 err = got_error(GOT_ERR_PRIVSEP_LEN);
1976 memcpy(&iremotes, imsg.data, sizeof(iremotes));
1977 if (iremotes.nremotes == 0) {
1984 return got_error(GOT_ERR_PRIVSEP_MSG);
1989 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(iremote));
1990 if (*remotes == NULL)
1991 return got_error_from_errno("recallocarray");
1993 while (*nremotes < iremotes.nremotes) {
1994 struct got_remote_repo *remote;
1996 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremote));
1999 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2001 switch (imsg.hdr.type) {
2002 case GOT_IMSG_GITCONFIG_REMOTE:
2003 remote = &(*remotes)[*nremotes];
2004 if (datalen < sizeof(iremote)) {
2005 err = got_error(GOT_ERR_PRIVSEP_LEN);
2008 memcpy(&iremote, imsg.data, sizeof(iremote));
2009 if (iremote.name_len == 0 || iremote.url_len == 0 ||
2010 (sizeof(iremote) + iremote.name_len +
2011 iremote.url_len) > datalen) {
2012 err = got_error(GOT_ERR_PRIVSEP_LEN);
2015 remote->name = strndup(imsg.data + sizeof(iremote),
2017 if (remote->name == NULL) {
2018 err = got_error_from_errno("strndup");
2021 remote->url = strndup(imsg.data + sizeof(iremote) +
2022 iremote.name_len, iremote.url_len);
2023 if (remote->url == NULL) {
2024 err = got_error_from_errno("strndup");
2031 err = got_error(GOT_ERR_PRIVSEP_MSG);
2042 for (i = 0; i < *nremotes; i++) {
2043 free((*remotes)[i].name);
2044 free((*remotes)[i].url);
2053 const struct got_error *
2054 got_privsep_send_commit_traversal_request(struct imsgbuf *ibuf,
2055 struct got_object_id *id, int idx, const char *path)
2057 const struct got_error *err = NULL;
2059 size_t path_len = strlen(path) + 1;
2061 wbuf = imsg_create(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_REQUEST, 0, 0,
2062 sizeof(struct got_imsg_commit_traversal_request) + path_len);
2064 return got_error_from_errno(
2065 "imsg_create COMMIT_TRAVERSAL_REQUEST");
2066 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
2067 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
2071 if (imsg_add(wbuf, &idx, sizeof(idx)) == -1) {
2072 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
2076 if (imsg_add(wbuf, path, path_len) == -1) {
2077 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
2083 imsg_close(ibuf, wbuf);
2085 return flush_imsg(ibuf);
2088 const struct got_error *
2089 got_privsep_send_traversed_commits(struct got_object_id *commit_ids,
2090 size_t ncommits, struct imsgbuf *ibuf)
2092 const struct got_error *err;
2096 wbuf = imsg_create(ibuf, GOT_IMSG_TRAVERSED_COMMITS, 0, 0,
2097 sizeof(struct got_imsg_traversed_commits) +
2098 ncommits * SHA1_DIGEST_LENGTH);
2100 return got_error_from_errno("imsg_create TRAVERSED_COMMITS");
2102 if (imsg_add(wbuf, &ncommits, sizeof(ncommits)) == -1) {
2103 err = got_error_from_errno("imsg_add TRAVERSED_COMMITS");
2107 for (i = 0; i < ncommits; i++) {
2108 struct got_object_id *id = &commit_ids[i];
2109 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
2110 err = got_error_from_errno(
2111 "imsg_add TRAVERSED_COMMITS");
2118 imsg_close(ibuf, wbuf);
2120 return flush_imsg(ibuf);
2123 const struct got_error *
2124 got_privsep_recv_traversed_commits(struct got_commit_object **changed_commit,
2125 struct got_object_id **changed_commit_id,
2126 struct got_object_id_queue *commit_ids, struct imsgbuf *ibuf)
2128 const struct got_error *err = NULL;
2130 struct got_imsg_traversed_commits *icommits;
2134 *changed_commit = NULL;
2135 *changed_commit_id = NULL;
2138 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2142 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2143 switch (imsg.hdr.type) {
2144 case GOT_IMSG_TRAVERSED_COMMITS:
2145 icommits = imsg.data;
2146 if (datalen != sizeof(*icommits) +
2147 icommits->ncommits * SHA1_DIGEST_LENGTH) {
2148 err = got_error(GOT_ERR_PRIVSEP_LEN);
2151 for (i = 0; i < icommits->ncommits; i++) {
2152 struct got_object_qid *qid;
2153 uint8_t *sha1 = (uint8_t *)imsg.data +
2154 sizeof(*icommits) + i * SHA1_DIGEST_LENGTH;
2155 err = got_object_qid_alloc_partial(&qid);
2158 memcpy(qid->id->sha1, sha1, SHA1_DIGEST_LENGTH);
2159 SIMPLEQ_INSERT_TAIL(commit_ids, qid, entry);
2161 /* The last commit may contain a change. */
2162 if (i == icommits->ncommits - 1) {
2163 *changed_commit_id =
2164 got_object_id_dup(qid->id);
2165 if (*changed_commit_id == NULL) {
2166 err = got_error_from_errno(
2167 "got_object_id_dup");
2173 case GOT_IMSG_COMMIT:
2174 if (*changed_commit_id == NULL) {
2175 err = got_error(GOT_ERR_PRIVSEP_MSG);
2178 err = get_commit_from_imsg(changed_commit, &imsg,
2181 case GOT_IMSG_COMMIT_TRAVERSAL_DONE:
2185 err = got_error(GOT_ERR_PRIVSEP_MSG);
2195 got_object_id_queue_free(commit_ids);
2199 const struct got_error *
2200 got_privsep_send_commit_traversal_done(struct imsgbuf *ibuf)
2202 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_DONE, 0, 0, -1,
2204 return got_error_from_errno("imsg_compose TRAVERSAL_DONE");
2206 return flush_imsg(ibuf);
2209 const struct got_error *
2210 got_privsep_unveil_exec_helpers(void)
2212 const char *helpers[] = {
2213 GOT_PATH_PROG_READ_PACK,
2214 GOT_PATH_PROG_READ_OBJECT,
2215 GOT_PATH_PROG_READ_COMMIT,
2216 GOT_PATH_PROG_READ_TREE,
2217 GOT_PATH_PROG_READ_BLOB,
2218 GOT_PATH_PROG_READ_TAG,
2219 GOT_PATH_PROG_READ_GITCONFIG,
2220 GOT_PATH_PROG_FETCH_PACK,
2221 GOT_PATH_PROG_INDEX_PACK,
2225 for (i = 0; i < nitems(helpers); i++) {
2226 if (unveil(helpers[i], "x") == 0)
2228 return got_error_from_errno2("unveil", helpers[i]);
2235 got_privsep_exec_child(int imsg_fds[2], const char *path, const char *repo_path)
2237 if (close(imsg_fds[0]) != 0) {
2238 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2242 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
2243 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2246 if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
2247 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2251 if (execl(path, path, repo_path, (char *)NULL) == -1) {
2252 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,