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 const struct got_error *
372 got_privsep_send_tmpfd(struct imsgbuf *ibuf, int fd)
374 const struct got_error *err = NULL;
376 if (imsg_compose(ibuf, GOT_IMSG_TMPFD, 0, 0, fd, NULL, 0)
378 err = got_error_from_errno("imsg_compose TMPFD");
383 return flush_imsg(ibuf);
386 const struct got_error *
387 got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj)
389 struct got_imsg_object iobj;
391 memcpy(iobj.id, obj->id.sha1, sizeof(iobj.id));
392 iobj.type = obj->type;
393 iobj.flags = obj->flags;
394 iobj.hdrlen = obj->hdrlen;
395 iobj.size = obj->size;
396 if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
397 iobj.pack_offset = obj->pack_offset;
398 iobj.pack_idx = obj->pack_idx;
401 if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
403 return got_error_from_errno("imsg_compose OBJECT");
405 return flush_imsg(ibuf);
408 const struct got_error *
409 got_privsep_send_fetch_req(struct imsgbuf *ibuf, int fd,
410 struct got_pathlist_head *have_refs)
412 const struct got_error *err = NULL;
414 size_t len, n_have_refs = 0;
415 struct got_pathlist_entry *pe;
417 len = sizeof(struct got_imsg_fetch_symrefs);
418 TAILQ_FOREACH(pe, have_refs, entry) {
419 struct got_object_id *id = pe->data;
420 len += sizeof(struct got_imsg_fetch_have_ref) +
421 pe->path_len + sizeof(id->sha1);
424 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
426 return got_error(GOT_ERR_NO_SPACE);
429 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_REQUEST, 0, 0, len);
432 return got_error_from_errno("imsg_create FETCH_REQUEST");
435 /* Keep in sync with struct got_imsg_fetch_have_refs definition! */
436 if (imsg_add(wbuf, &n_have_refs, sizeof(n_have_refs)) == -1) {
437 err = got_error_from_errno("imsg_add FETCH_REQUEST");
443 TAILQ_FOREACH(pe, have_refs, entry) {
444 const char *name = pe->path;
445 size_t name_len = pe->path_len;
446 struct got_object_id *id = pe->data;
448 /* Keep in sync with struct got_imsg_fetch_have_ref! */
449 if (imsg_add(wbuf, id->sha1, sizeof(id->sha1)) == -1) {
450 err = got_error_from_errno("imsg_add FETCH_REQUEST");
455 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
456 err = got_error_from_errno("imsg_add FETCH_REQUEST");
461 if (imsg_add(wbuf, name, name_len) == -1) {
462 err = got_error_from_errno("imsg_add FETCH_REQUEST");
470 imsg_close(ibuf, wbuf);
471 return flush_imsg(ibuf);
474 const struct got_error *
475 got_privsep_send_fetch_symrefs(struct imsgbuf *ibuf,
476 struct got_pathlist_head *symrefs)
478 const struct got_error *err = NULL;
480 size_t len, nsymrefs = 0;
481 struct got_pathlist_entry *pe;
483 len = sizeof(struct got_imsg_fetch_symrefs);
484 TAILQ_FOREACH(pe, symrefs, entry) {
485 const char *target = pe->data;
486 len += sizeof(struct got_imsg_fetch_symref) +
487 pe->path_len + strlen(target);
491 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
492 return got_error(GOT_ERR_NO_SPACE);
494 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_SYMREFS, 0, 0, len);
496 return got_error_from_errno("imsg_create FETCH_SYMREFS");
498 /* Keep in sync with struct got_imsg_fetch_symrefs definition! */
499 if (imsg_add(wbuf, &nsymrefs, sizeof(nsymrefs)) == -1) {
500 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
505 TAILQ_FOREACH(pe, symrefs, entry) {
506 const char *name = pe->path;
507 size_t name_len = pe->path_len;
508 const char *target = pe->data;
509 size_t target_len = strlen(target);
511 /* Keep in sync with struct got_imsg_fetch_symref definition! */
512 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
513 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
517 if (imsg_add(wbuf, &target_len, sizeof(target_len)) == -1) {
518 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
522 if (imsg_add(wbuf, name, name_len) == -1) {
523 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
527 if (imsg_add(wbuf, target, target_len) == -1) {
528 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
535 imsg_close(ibuf, wbuf);
536 return flush_imsg(ibuf);
539 const struct got_error *
540 got_privsep_send_fetch_ref(struct imsgbuf *ibuf,
541 struct got_object_id *refid, const char *refname)
543 const struct got_error *err = NULL;
545 size_t len, reflen = strlen(refname);
547 len = sizeof(struct got_imsg_fetch_ref) + reflen;
548 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
549 return got_error(GOT_ERR_NO_SPACE);
551 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_REF, 0, 0, len);
553 return got_error_from_errno("imsg_create FETCH_REF");
555 /* Keep in sync with struct got_imsg_fetch_ref definition! */
556 if (imsg_add(wbuf, refid->sha1, SHA1_DIGEST_LENGTH) == -1) {
557 err = got_error_from_errno("imsg_add FETCH_REF");
561 if (imsg_add(wbuf, refname, reflen) == -1) {
562 err = got_error_from_errno("imsg_add FETCH_REF");
568 imsg_close(ibuf, wbuf);
569 return flush_imsg(ibuf);
572 const struct got_error *
573 got_privsep_send_fetch_server_progress(struct imsgbuf *ibuf, const char *msg,
576 if (msglen > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
577 return got_error(GOT_ERR_NO_SPACE);
582 if (imsg_compose(ibuf, GOT_IMSG_FETCH_SERVER_PROGRESS, 0, 0, -1,
584 return got_error_from_errno(
585 "imsg_compose FETCH_SERVER_PROGRESS");
587 return flush_imsg(ibuf);
590 const struct got_error *
591 got_privsep_send_fetch_download_progress(struct imsgbuf *ibuf, off_t bytes)
593 if (imsg_compose(ibuf, GOT_IMSG_FETCH_DOWNLOAD_PROGRESS, 0, 0, -1,
594 &bytes, sizeof(bytes)) == -1)
595 return got_error_from_errno(
596 "imsg_compose FETCH_DOWNLOAD_PROGRESS");
598 return flush_imsg(ibuf);
601 const struct got_error *
602 got_privsep_send_fetch_done(struct imsgbuf *ibuf, struct got_object_id hash)
604 if (imsg_compose(ibuf, GOT_IMSG_FETCH_DONE, 0, 0, -1,
605 hash.sha1, SHA1_DIGEST_LENGTH) == -1)
606 return got_error_from_errno("imsg_compose FETCH");
607 return flush_imsg(ibuf);
611 const struct got_error *
612 got_privsep_recv_fetch_progress(int *done, struct got_object_id **id,
613 char **refname, struct got_pathlist_head *symrefs, char **server_progress,
614 off_t *packfile_size, struct imsgbuf *ibuf)
616 const struct got_error *err = NULL;
619 struct got_imsg_fetch_symrefs *isymrefs = NULL;
627 *server_progress = NULL;
629 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
633 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
634 switch (imsg.hdr.type) {
636 if (datalen < sizeof(struct got_imsg_error)) {
637 err = got_error(GOT_ERR_PRIVSEP_LEN);
640 err = recv_imsg_error(&imsg, datalen);
642 case GOT_IMSG_FETCH_SYMREFS:
643 if (datalen < sizeof(*isymrefs)) {
644 err = got_error(GOT_ERR_PRIVSEP_LEN);
647 if (isymrefs != NULL) {
648 err = got_error(GOT_ERR_PRIVSEP_MSG);
651 isymrefs = (struct got_imsg_fetch_symrefs *)imsg.data;
652 off = sizeof(*isymrefs);
653 remain = datalen - off;
654 for (n = 0; n < isymrefs->nsymrefs; n++) {
655 struct got_imsg_fetch_symref *s;
657 if (remain < sizeof(struct got_imsg_fetch_symref)) {
658 err = got_error(GOT_ERR_PRIVSEP_LEN);
661 s = (struct got_imsg_fetch_symref *)(imsg.data + off);
663 remain -= sizeof(*s);
664 if (remain < s->name_len) {
665 err = got_error(GOT_ERR_PRIVSEP_LEN);
668 name = strndup(imsg.data + off, s->name_len);
670 err = got_error_from_errno("strndup");
674 remain -= s->name_len;
675 if (remain < s->target_len) {
676 err = got_error(GOT_ERR_PRIVSEP_LEN);
680 target = strndup(imsg.data + off, s->target_len);
681 if (target == NULL) {
682 err = got_error_from_errno("strndup");
686 off += s->target_len;
687 remain -= s->target_len;
688 err = got_pathlist_append(symrefs, name, target);
696 case GOT_IMSG_FETCH_REF:
697 if (datalen <= SHA1_DIGEST_LENGTH) {
698 err = got_error(GOT_ERR_PRIVSEP_MSG);
701 *id = malloc(sizeof(**id));
703 err = got_error_from_errno("malloc");
706 memcpy((*id)->sha1, imsg.data, SHA1_DIGEST_LENGTH);
707 *refname = strndup(imsg.data + SHA1_DIGEST_LENGTH,
708 datalen - SHA1_DIGEST_LENGTH);
709 if (*refname == NULL) {
710 err = got_error_from_errno("strndup");
714 case GOT_IMSG_FETCH_SERVER_PROGRESS:
716 err = got_error(GOT_ERR_PRIVSEP_LEN);
719 *server_progress = strndup(imsg.data, datalen);
720 if (*server_progress == NULL) {
721 err = got_error_from_errno("strndup");
724 for (i = 0; i < datalen; i++) {
725 if (!isprint((unsigned char)(*server_progress)[i]) &&
726 !isspace((unsigned char)(*server_progress)[i])) {
727 err = got_error(GOT_ERR_PRIVSEP_MSG);
728 free(*server_progress);
729 *server_progress = NULL;
734 case GOT_IMSG_FETCH_DOWNLOAD_PROGRESS:
735 if (datalen < sizeof(*packfile_size)) {
736 err = got_error(GOT_ERR_PRIVSEP_MSG);
739 memcpy(packfile_size, imsg.data, sizeof(*packfile_size));
741 case GOT_IMSG_FETCH_DONE:
742 *id = malloc(sizeof(**id));
744 err = got_error_from_errno("malloc");
747 if (datalen != SHA1_DIGEST_LENGTH) {
748 err = got_error(GOT_ERR_PRIVSEP_MSG);
751 memcpy((*id)->sha1, imsg.data, SHA1_DIGEST_LENGTH);
755 err = got_error(GOT_ERR_PRIVSEP_MSG);
769 const struct got_error *
770 got_privsep_send_index_pack_req(struct imsgbuf *ibuf, uint8_t *pack_hash,
773 const struct got_error *err = NULL;
775 /* Keep in sync with struct got_imsg_index_pack_request */
776 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_REQUEST, 0, 0, fd,
777 pack_hash, SHA1_DIGEST_LENGTH) == -1) {
778 err = got_error_from_errno("imsg_compose INDEX_REQUEST");
782 return flush_imsg(ibuf);
785 const struct got_error *
786 got_privsep_send_index_pack_progress(struct imsgbuf *ibuf, int nobj_total,
787 int nobj_indexed, int nobj_loose, int nobj_resolved)
789 struct got_imsg_index_pack_progress iprogress;
791 iprogress.nobj_total = nobj_total;
792 iprogress.nobj_indexed = nobj_indexed;
793 iprogress.nobj_loose = nobj_loose;
794 iprogress.nobj_resolved = nobj_resolved;
796 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_PROGRESS, 0, 0, -1,
797 &iprogress, sizeof(iprogress)) == -1)
798 return got_error_from_errno("imsg_compose IDXPACK_PROGRESS");
800 return flush_imsg(ibuf);
803 const struct got_error *
804 got_privsep_send_index_pack_done(struct imsgbuf *ibuf)
806 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_DONE, 0, 0, -1, NULL, 0) == -1)
807 return got_error_from_errno("imsg_compose FETCH");
808 return flush_imsg(ibuf);
811 const struct got_error *
812 got_privsep_recv_index_progress(int *done, int *nobj_total,
813 int *nobj_indexed, int *nobj_loose, int *nobj_resolved,
814 struct imsgbuf *ibuf)
816 const struct got_error *err = NULL;
818 struct got_imsg_index_pack_progress *iprogress;
826 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
830 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
831 switch (imsg.hdr.type) {
833 if (datalen < sizeof(struct got_imsg_error)) {
834 err = got_error(GOT_ERR_PRIVSEP_LEN);
837 err = recv_imsg_error(&imsg, datalen);
839 case GOT_IMSG_IDXPACK_PROGRESS:
840 if (datalen < sizeof(*iprogress)) {
841 err = got_error(GOT_ERR_PRIVSEP_LEN);
844 iprogress = (struct got_imsg_index_pack_progress *)imsg.data;
845 *nobj_total = iprogress->nobj_total;
846 *nobj_indexed = iprogress->nobj_indexed;
847 *nobj_loose = iprogress->nobj_loose;
848 *nobj_resolved = iprogress->nobj_resolved;
850 case GOT_IMSG_IDXPACK_DONE:
852 err = got_error(GOT_ERR_PRIVSEP_LEN);
858 err = got_error(GOT_ERR_PRIVSEP_MSG);
866 const struct got_error *
867 got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
868 struct imsgbuf *ibuf)
870 const struct got_error *err = NULL;
871 struct got_imsg_object *iobj;
872 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
874 if (datalen != sizeof(*iobj))
875 return got_error(GOT_ERR_PRIVSEP_LEN);
878 *obj = calloc(1, sizeof(**obj));
880 return got_error_from_errno("calloc");
882 memcpy((*obj)->id.sha1, iobj->id, SHA1_DIGEST_LENGTH);
883 (*obj)->type = iobj->type;
884 (*obj)->flags = iobj->flags;
885 (*obj)->hdrlen = iobj->hdrlen;
886 (*obj)->size = iobj->size;
887 /* path_packfile is handled by caller */
888 if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
889 (*obj)->pack_offset = iobj->pack_offset;
890 (*obj)->pack_idx = iobj->pack_idx;
896 const struct got_error *
897 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
899 const struct got_error *err = NULL;
901 const size_t min_datalen =
902 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
906 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
910 switch (imsg.hdr.type) {
911 case GOT_IMSG_OBJECT:
912 err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
915 err = got_error(GOT_ERR_PRIVSEP_MSG);
924 static const struct got_error *
925 send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
928 const struct got_error *err = NULL;
929 size_t offset, remain;
934 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
936 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
937 commit->logmsg + offset, n) == -1) {
938 err = got_error_from_errno("imsg_compose "
943 err = flush_imsg(ibuf);
954 const struct got_error *
955 got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
957 const struct got_error *err = NULL;
958 struct got_imsg_commit_object *icommit;
961 struct got_object_qid *qid;
962 size_t author_len = strlen(commit->author);
963 size_t committer_len = strlen(commit->committer);
964 size_t logmsg_len = strlen(commit->logmsg);
966 total = sizeof(*icommit) + author_len + committer_len +
967 commit->nparents * SHA1_DIGEST_LENGTH;
971 return got_error_from_errno("malloc");
973 icommit = (struct got_imsg_commit_object *)buf;
974 memcpy(icommit->tree_id, commit->tree_id->sha1,
975 sizeof(icommit->tree_id));
976 icommit->author_len = author_len;
977 icommit->author_time = commit->author_time;
978 icommit->author_gmtoff = commit->author_gmtoff;
979 icommit->committer_len = committer_len;
980 icommit->committer_time = commit->committer_time;
981 icommit->committer_gmtoff = commit->committer_gmtoff;
982 icommit->logmsg_len = logmsg_len;
983 icommit->nparents = commit->nparents;
985 len = sizeof(*icommit);
986 memcpy(buf + len, commit->author, author_len);
988 memcpy(buf + len, commit->committer, committer_len);
989 len += committer_len;
990 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
991 memcpy(buf + len, qid->id, SHA1_DIGEST_LENGTH);
992 len += SHA1_DIGEST_LENGTH;
995 if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
996 err = got_error_from_errno("imsg_compose COMMIT");
1000 if (logmsg_len == 0 ||
1001 logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1002 err = flush_imsg(ibuf);
1006 err = send_commit_logmsg(ibuf, commit, logmsg_len);
1012 static const struct got_error *
1013 get_commit_from_imsg(struct got_commit_object **commit,
1014 struct imsg *imsg, size_t datalen, struct imsgbuf *ibuf)
1016 const struct got_error *err = NULL;
1017 struct got_imsg_commit_object *icommit;
1021 if (datalen < sizeof(*icommit))
1022 return got_error(GOT_ERR_PRIVSEP_LEN);
1024 icommit = imsg->data;
1025 if (datalen != sizeof(*icommit) + icommit->author_len +
1026 icommit->committer_len +
1027 icommit->nparents * SHA1_DIGEST_LENGTH)
1028 return got_error(GOT_ERR_PRIVSEP_LEN);
1030 if (icommit->nparents < 0)
1031 return got_error(GOT_ERR_PRIVSEP_LEN);
1033 len += sizeof(*icommit);
1035 *commit = got_object_commit_alloc_partial();
1036 if (*commit == NULL)
1037 return got_error_from_errno(
1038 "got_object_commit_alloc_partial");
1040 memcpy((*commit)->tree_id->sha1, icommit->tree_id,
1041 SHA1_DIGEST_LENGTH);
1042 (*commit)->author_time = icommit->author_time;
1043 (*commit)->author_gmtoff = icommit->author_gmtoff;
1044 (*commit)->committer_time = icommit->committer_time;
1045 (*commit)->committer_gmtoff = icommit->committer_gmtoff;
1047 if (icommit->author_len == 0) {
1048 (*commit)->author = strdup("");
1049 if ((*commit)->author == NULL) {
1050 err = got_error_from_errno("strdup");
1054 (*commit)->author = malloc(icommit->author_len + 1);
1055 if ((*commit)->author == NULL) {
1056 err = got_error_from_errno("malloc");
1059 memcpy((*commit)->author, imsg->data + len,
1060 icommit->author_len);
1061 (*commit)->author[icommit->author_len] = '\0';
1063 len += icommit->author_len;
1065 if (icommit->committer_len == 0) {
1066 (*commit)->committer = strdup("");
1067 if ((*commit)->committer == NULL) {
1068 err = got_error_from_errno("strdup");
1072 (*commit)->committer =
1073 malloc(icommit->committer_len + 1);
1074 if ((*commit)->committer == NULL) {
1075 err = got_error_from_errno("malloc");
1078 memcpy((*commit)->committer, imsg->data + len,
1079 icommit->committer_len);
1080 (*commit)->committer[icommit->committer_len] = '\0';
1082 len += icommit->committer_len;
1084 if (icommit->logmsg_len == 0) {
1085 (*commit)->logmsg = strdup("");
1086 if ((*commit)->logmsg == NULL) {
1087 err = got_error_from_errno("strdup");
1091 size_t offset = 0, remain = icommit->logmsg_len;
1093 (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
1094 if ((*commit)->logmsg == NULL) {
1095 err = got_error_from_errno("malloc");
1098 while (remain > 0) {
1099 struct imsg imsg_log;
1100 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1103 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1107 if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG) {
1108 err = got_error(GOT_ERR_PRIVSEP_MSG);
1112 memcpy((*commit)->logmsg + offset,
1114 imsg_free(&imsg_log);
1118 (*commit)->logmsg[icommit->logmsg_len] = '\0';
1121 for (i = 0; i < icommit->nparents; i++) {
1122 struct got_object_qid *qid;
1124 err = got_object_qid_alloc_partial(&qid);
1127 memcpy(qid->id, imsg->data + len +
1128 i * SHA1_DIGEST_LENGTH, sizeof(*qid->id));
1129 SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
1130 (*commit)->nparents++;
1134 got_object_commit_close(*commit);
1140 const struct got_error *
1141 got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
1143 const struct got_error *err = NULL;
1146 const size_t min_datalen =
1147 MIN(sizeof(struct got_imsg_error),
1148 sizeof(struct got_imsg_commit_object));
1152 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1156 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1158 switch (imsg.hdr.type) {
1159 case GOT_IMSG_COMMIT:
1160 err = get_commit_from_imsg(commit, &imsg, datalen, ibuf);
1163 err = got_error(GOT_ERR_PRIVSEP_MSG);
1172 const struct got_error *
1173 got_privsep_send_tree(struct imsgbuf *ibuf, struct got_pathlist_head *entries,
1176 const struct got_error *err = NULL;
1177 struct got_imsg_tree_object itree;
1178 struct got_pathlist_entry *pe;
1180 int nimsg; /* number of imsg queued in ibuf */
1182 itree.nentries = nentries;
1183 if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
1185 return got_error_from_errno("imsg_compose TREE");
1187 totlen = sizeof(itree);
1189 TAILQ_FOREACH(pe, entries, entry) {
1190 const char *name = pe->path;
1191 struct got_parsed_tree_entry *pte = pe->data;
1193 size_t namelen = strlen(name);
1194 size_t len = sizeof(struct got_imsg_tree_entry) + namelen;
1196 if (len > MAX_IMSGSIZE)
1197 return got_error(GOT_ERR_NO_SPACE);
1200 if (totlen + len >= MAX_IMSGSIZE - (IMSG_HEADER_SIZE * nimsg)) {
1201 err = flush_imsg(ibuf);
1207 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_ENTRY, 0, 0, len);
1209 return got_error_from_errno("imsg_create TREE_ENTRY");
1211 /* Keep in sync with struct got_imsg_tree_object definition! */
1212 if (imsg_add(wbuf, pte->id, SHA1_DIGEST_LENGTH) == -1) {
1213 err = got_error_from_errno("imsg_add TREE_ENTRY");
1217 if (imsg_add(wbuf, &pte->mode, sizeof(pte->mode)) == -1) {
1218 err = got_error_from_errno("imsg_add TREE_ENTRY");
1223 if (imsg_add(wbuf, name, namelen) == -1) {
1224 err = got_error_from_errno("imsg_add TREE_ENTRY");
1230 imsg_close(ibuf, wbuf);
1235 return flush_imsg(ibuf);
1238 const struct got_error *
1239 got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
1241 const struct got_error *err = NULL;
1242 const size_t min_datalen =
1243 MIN(sizeof(struct got_imsg_error),
1244 sizeof(struct got_imsg_tree_object));
1245 struct got_imsg_tree_object *itree;
1250 err = read_imsg(ibuf);
1258 struct got_imsg_tree_entry *ite;
1259 struct got_tree_entry *te = NULL;
1261 n = imsg_get(ibuf, &imsg);
1263 if (*tree && (*tree)->nentries != nentries)
1268 if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen)
1269 return got_error(GOT_ERR_PRIVSEP_LEN);
1271 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1273 switch (imsg.hdr.type) {
1274 case GOT_IMSG_ERROR:
1275 err = recv_imsg_error(&imsg, datalen);
1278 /* This message should only appear once. */
1279 if (*tree != NULL) {
1280 err = got_error(GOT_ERR_PRIVSEP_MSG);
1283 if (datalen != sizeof(*itree)) {
1284 err = got_error(GOT_ERR_PRIVSEP_LEN);
1288 *tree = malloc(sizeof(**tree));
1289 if (*tree == NULL) {
1290 err = got_error_from_errno("malloc");
1293 (*tree)->entries = calloc(itree->nentries,
1294 sizeof(struct got_tree_entry));
1295 if ((*tree)->entries == NULL) {
1296 err = got_error_from_errno("malloc");
1299 (*tree)->nentries = itree->nentries;
1300 (*tree)->refcnt = 0;
1302 case GOT_IMSG_TREE_ENTRY:
1303 /* This message should be preceeded by GOT_IMSG_TREE. */
1304 if (*tree == NULL) {
1305 err = got_error(GOT_ERR_PRIVSEP_MSG);
1308 if (datalen < sizeof(*ite) || datalen > MAX_IMSGSIZE) {
1309 err = got_error(GOT_ERR_PRIVSEP_LEN);
1313 /* Remaining data contains the entry's name. */
1314 datalen -= sizeof(*ite);
1315 if (datalen == 0 || datalen > MAX_IMSGSIZE) {
1316 err = got_error(GOT_ERR_PRIVSEP_LEN);
1321 if (datalen + 1 > sizeof(te->name)) {
1322 err = got_error(GOT_ERR_NO_SPACE);
1325 te = &(*tree)->entries[nentries];
1326 memcpy(te->name, imsg.data + sizeof(*ite), datalen);
1327 te->name[datalen] = '\0';
1329 memcpy(te->id.sha1, ite->id, SHA1_DIGEST_LENGTH);
1330 te->mode = ite->mode;
1335 err = got_error(GOT_ERR_PRIVSEP_MSG);
1342 if (*tree && (*tree)->nentries != nentries) {
1344 err = got_error(GOT_ERR_PRIVSEP_LEN);
1345 got_object_tree_close(*tree);
1352 const struct got_error *
1353 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
1354 const uint8_t *data)
1356 struct got_imsg_blob iblob;
1359 iblob.hdrlen = hdrlen;
1364 if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
1365 return got_error(GOT_ERR_NO_SPACE);
1367 buf = malloc(sizeof(iblob) + size);
1369 return got_error_from_errno("malloc");
1371 memcpy(buf, &iblob, sizeof(iblob));
1372 memcpy(buf + sizeof(iblob), data, size);
1373 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
1374 sizeof(iblob) + size) == -1) {
1376 return got_error_from_errno("imsg_compose BLOB");
1380 /* Data has already been written to file descriptor. */
1381 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
1382 sizeof(iblob)) == -1)
1383 return got_error_from_errno("imsg_compose BLOB");
1387 return flush_imsg(ibuf);
1390 const struct got_error *
1391 got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1392 struct imsgbuf *ibuf)
1394 const struct got_error *err = NULL;
1396 struct got_imsg_blob *iblob;
1401 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1405 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1407 switch (imsg.hdr.type) {
1409 if (datalen < sizeof(*iblob)) {
1410 err = got_error(GOT_ERR_PRIVSEP_LEN);
1414 *size = iblob->size;
1415 *hdrlen = iblob->hdrlen;
1417 if (datalen == sizeof(*iblob)) {
1418 /* Data has been written to file descriptor. */
1422 if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX) {
1423 err = got_error(GOT_ERR_PRIVSEP_LEN);
1427 *outbuf = malloc(*size);
1428 if (*outbuf == NULL) {
1429 err = got_error_from_errno("malloc");
1432 memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
1435 err = got_error(GOT_ERR_PRIVSEP_MSG);
1444 static const struct got_error *
1445 send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
1447 const struct got_error *err = NULL;
1448 size_t offset, remain;
1451 remain = tagmsg_len;
1452 while (remain > 0) {
1453 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1455 if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
1456 tag->tagmsg + offset, n) == -1) {
1457 err = got_error_from_errno("imsg_compose TAG_TAGMSG");
1461 err = flush_imsg(ibuf);
1472 const struct got_error *
1473 got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
1475 const struct got_error *err = NULL;
1476 struct got_imsg_tag_object *itag;
1479 size_t tag_len = strlen(tag->tag);
1480 size_t tagger_len = strlen(tag->tagger);
1481 size_t tagmsg_len = strlen(tag->tagmsg);
1483 total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
1485 buf = malloc(total);
1487 return got_error_from_errno("malloc");
1489 itag = (struct got_imsg_tag_object *)buf;
1490 memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
1491 itag->obj_type = tag->obj_type;
1492 itag->tag_len = tag_len;
1493 itag->tagger_len = tagger_len;
1494 itag->tagger_time = tag->tagger_time;
1495 itag->tagger_gmtoff = tag->tagger_gmtoff;
1496 itag->tagmsg_len = tagmsg_len;
1498 len = sizeof(*itag);
1499 memcpy(buf + len, tag->tag, tag_len);
1501 memcpy(buf + len, tag->tagger, tagger_len);
1504 if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
1505 err = got_error_from_errno("imsg_compose TAG");
1509 if (tagmsg_len == 0 ||
1510 tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1511 err = flush_imsg(ibuf);
1515 err = send_tagmsg(ibuf, tag, tagmsg_len);
1521 const struct got_error *
1522 got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
1524 const struct got_error *err = NULL;
1526 struct got_imsg_tag_object *itag;
1527 size_t len, datalen;
1528 const size_t min_datalen =
1529 MIN(sizeof(struct got_imsg_error),
1530 sizeof(struct got_imsg_tag_object));
1534 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1538 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1541 switch (imsg.hdr.type) {
1543 if (datalen < sizeof(*itag)) {
1544 err = got_error(GOT_ERR_PRIVSEP_LEN);
1548 if (datalen != sizeof(*itag) + itag->tag_len +
1550 err = got_error(GOT_ERR_PRIVSEP_LEN);
1553 len += sizeof(*itag);
1555 *tag = calloc(1, sizeof(**tag));
1557 err = got_error_from_errno("calloc");
1561 memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
1563 if (itag->tag_len == 0) {
1564 (*tag)->tag = strdup("");
1565 if ((*tag)->tag == NULL) {
1566 err = got_error_from_errno("strdup");
1570 (*tag)->tag = malloc(itag->tag_len + 1);
1571 if ((*tag)->tag == NULL) {
1572 err = got_error_from_errno("malloc");
1575 memcpy((*tag)->tag, imsg.data + len,
1577 (*tag)->tag[itag->tag_len] = '\0';
1579 len += itag->tag_len;
1581 (*tag)->obj_type = itag->obj_type;
1582 (*tag)->tagger_time = itag->tagger_time;
1583 (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1585 if (itag->tagger_len == 0) {
1586 (*tag)->tagger = strdup("");
1587 if ((*tag)->tagger == NULL) {
1588 err = got_error_from_errno("strdup");
1592 (*tag)->tagger = malloc(itag->tagger_len + 1);
1593 if ((*tag)->tagger == NULL) {
1594 err = got_error_from_errno("malloc");
1597 memcpy((*tag)->tagger, imsg.data + len,
1599 (*tag)->tagger[itag->tagger_len] = '\0';
1601 len += itag->tagger_len;
1603 if (itag->tagmsg_len == 0) {
1604 (*tag)->tagmsg = strdup("");
1605 if ((*tag)->tagmsg == NULL) {
1606 err = got_error_from_errno("strdup");
1610 size_t offset = 0, remain = itag->tagmsg_len;
1612 (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1613 if ((*tag)->tagmsg == NULL) {
1614 err = got_error_from_errno("malloc");
1617 while (remain > 0) {
1618 struct imsg imsg_log;
1619 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1622 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1626 if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1627 return got_error(GOT_ERR_PRIVSEP_MSG);
1629 memcpy((*tag)->tagmsg + offset, imsg_log.data,
1631 imsg_free(&imsg_log);
1635 (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1640 err = got_error(GOT_ERR_PRIVSEP_MSG);
1649 const struct got_error *
1650 got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1651 struct got_packidx *packidx)
1653 const struct got_error *err = NULL;
1654 struct got_imsg_packidx ipackidx;
1655 struct got_imsg_pack ipack;
1658 ipackidx.len = packidx->len;
1659 fd = dup(packidx->fd);
1661 return got_error_from_errno("dup");
1663 if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
1664 sizeof(ipackidx)) == -1) {
1665 err = got_error_from_errno("imsg_compose PACKIDX");
1670 if (strlcpy(ipack.path_packfile, pack->path_packfile,
1671 sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
1672 return got_error(GOT_ERR_NO_SPACE);
1673 ipack.filesize = pack->filesize;
1677 return got_error_from_errno("dup");
1679 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
1681 err = got_error_from_errno("imsg_compose PACK");
1686 return flush_imsg(ibuf);
1689 const struct got_error *
1690 got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
1691 struct got_object_id *id)
1693 struct got_imsg_packed_object iobj;
1696 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
1698 if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
1699 &iobj, sizeof(iobj)) == -1)
1700 return got_error_from_errno("imsg_compose "
1701 "PACKED_OBJECT_REQUEST");
1703 return flush_imsg(ibuf);
1706 const struct got_error *
1707 got_privsep_send_gitconfig_parse_req(struct imsgbuf *ibuf, int fd)
1709 const struct got_error *err = NULL;
1711 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_PARSE_REQUEST, 0, 0, fd,
1713 err = got_error_from_errno("imsg_compose "
1714 "GITCONFIG_PARSE_REQUEST");
1719 return flush_imsg(ibuf);
1722 const struct got_error *
1723 got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *ibuf)
1725 if (imsg_compose(ibuf,
1726 GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST, 0, 0, -1,
1728 return got_error_from_errno("imsg_compose "
1729 "GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST");
1731 return flush_imsg(ibuf);
1734 const struct got_error *
1735 got_privsep_send_gitconfig_author_name_req(struct imsgbuf *ibuf)
1737 if (imsg_compose(ibuf,
1738 GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST, 0, 0, -1, NULL, 0) == -1)
1739 return got_error_from_errno("imsg_compose "
1740 "GITCONFIG_AUTHOR_NAME_REQUEST");
1742 return flush_imsg(ibuf);
1745 const struct got_error *
1746 got_privsep_send_gitconfig_author_email_req(struct imsgbuf *ibuf)
1748 if (imsg_compose(ibuf,
1749 GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST, 0, 0, -1, NULL, 0) == -1)
1750 return got_error_from_errno("imsg_compose "
1751 "GITCONFIG_AUTHOR_EMAIL_REQUEST");
1753 return flush_imsg(ibuf);
1756 const struct got_error *
1757 got_privsep_send_gitconfig_remotes_req(struct imsgbuf *ibuf)
1759 if (imsg_compose(ibuf,
1760 GOT_IMSG_GITCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
1761 return got_error_from_errno("imsg_compose "
1762 "GITCONFIG_REMOTE_REQUEST");
1764 return flush_imsg(ibuf);
1767 const struct got_error *
1768 got_privsep_send_gitconfig_owner_req(struct imsgbuf *ibuf)
1770 if (imsg_compose(ibuf,
1771 GOT_IMSG_GITCONFIG_OWNER_REQUEST, 0, 0, -1, NULL, 0) == -1)
1772 return got_error_from_errno("imsg_compose "
1773 "GITCONFIG_OWNER_REQUEST");
1775 return flush_imsg(ibuf);
1778 const struct got_error *
1779 got_privsep_send_gitconfig_str(struct imsgbuf *ibuf, const char *value)
1781 size_t len = value ? strlen(value) + 1 : 0;
1783 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_STR_VAL, 0, 0, -1,
1785 return got_error_from_errno("imsg_compose GITCONFIG_STR_VAL");
1787 return flush_imsg(ibuf);
1790 const struct got_error *
1791 got_privsep_recv_gitconfig_str(char **str, struct imsgbuf *ibuf)
1793 const struct got_error *err = NULL;
1796 const size_t min_datalen = 0;
1800 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1803 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1805 switch (imsg.hdr.type) {
1806 case GOT_IMSG_GITCONFIG_STR_VAL:
1809 *str = malloc(datalen);
1811 err = got_error_from_errno("malloc");
1814 if (strlcpy(*str, imsg.data, datalen) >= datalen)
1815 err = got_error(GOT_ERR_NO_SPACE);
1818 err = got_error(GOT_ERR_PRIVSEP_MSG);
1826 const struct got_error *
1827 got_privsep_send_gitconfig_int(struct imsgbuf *ibuf, int value)
1829 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_INT_VAL, 0, 0, -1,
1830 &value, sizeof(value)) == -1)
1831 return got_error_from_errno("imsg_compose GITCONFIG_INT_VAL");
1833 return flush_imsg(ibuf);
1836 const struct got_error *
1837 got_privsep_recv_gitconfig_int(int *val, struct imsgbuf *ibuf)
1839 const struct got_error *err = NULL;
1842 const size_t min_datalen =
1843 MIN(sizeof(struct got_imsg_error), sizeof(int));
1847 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1850 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1852 switch (imsg.hdr.type) {
1853 case GOT_IMSG_GITCONFIG_INT_VAL:
1854 if (datalen != sizeof(*val)) {
1855 err = got_error(GOT_ERR_PRIVSEP_LEN);
1858 memcpy(val, imsg.data, sizeof(*val));
1861 err = got_error(GOT_ERR_PRIVSEP_MSG);
1869 const struct got_error *
1870 got_privsep_send_gitconfig_remotes(struct imsgbuf *ibuf,
1871 struct got_remote_repo *remotes, int nremotes)
1873 const struct got_error *err = NULL;
1874 struct got_imsg_remotes iremotes;
1877 iremotes.nremotes = nremotes;
1878 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_REMOTES, 0, 0, -1,
1879 &iremotes, sizeof(iremotes)) == -1)
1880 return got_error_from_errno("imsg_compose GITCONFIG_REMOTES");
1882 err = flush_imsg(ibuf);
1887 for (i = 0; i < nremotes; i++) {
1888 struct got_imsg_remote iremote;
1889 size_t len = sizeof(iremote);
1892 iremote.name_len = strlen(remotes[i].name);
1893 len += iremote.name_len;
1894 iremote.url_len = strlen(remotes[i].url);
1895 len += iremote.url_len;
1897 wbuf = imsg_create(ibuf, GOT_IMSG_GITCONFIG_REMOTE, 0, 0, len);
1899 return got_error_from_errno(
1900 "imsg_create GITCONFIG_REMOTE");
1902 if (imsg_add(wbuf, &iremote, sizeof(iremote)) == -1) {
1903 err = got_error_from_errno(
1904 "imsg_add GIITCONFIG_REMOTE");
1909 if (imsg_add(wbuf, remotes[i].name, iremote.name_len) == -1) {
1910 err = got_error_from_errno(
1911 "imsg_add GIITCONFIG_REMOTE");
1915 if (imsg_add(wbuf, remotes[i].url, iremote.url_len) == -1) {
1916 err = got_error_from_errno(
1917 "imsg_add GIITCONFIG_REMOTE");
1923 imsg_close(ibuf, wbuf);
1924 err = flush_imsg(ibuf);
1932 const struct got_error *
1933 got_privsep_recv_gitconfig_remotes(struct got_remote_repo **remotes,
1934 int *nremotes, struct imsgbuf *ibuf)
1936 const struct got_error *err = NULL;
1939 struct got_imsg_remotes iremotes;
1940 struct got_imsg_remote iremote;
1944 iremotes.nremotes = 0;
1946 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremotes));
1949 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1951 switch (imsg.hdr.type) {
1952 case GOT_IMSG_GITCONFIG_REMOTES:
1953 if (datalen != sizeof(iremotes)) {
1954 err = got_error(GOT_ERR_PRIVSEP_LEN);
1957 memcpy(&iremotes, imsg.data, sizeof(iremotes));
1958 if (iremotes.nremotes == 0) {
1965 return got_error(GOT_ERR_PRIVSEP_MSG);
1970 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(iremote));
1971 if (*remotes == NULL)
1972 return got_error_from_errno("recallocarray");
1974 while (*nremotes < iremotes.nremotes) {
1975 struct got_remote_repo *remote;
1977 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremote));
1980 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1982 switch (imsg.hdr.type) {
1983 case GOT_IMSG_GITCONFIG_REMOTE:
1984 remote = &(*remotes)[*nremotes];
1985 if (datalen < sizeof(iremote)) {
1986 err = got_error(GOT_ERR_PRIVSEP_LEN);
1989 memcpy(&iremote, imsg.data, sizeof(iremote));
1990 if (iremote.name_len == 0 || iremote.url_len == 0 ||
1991 (sizeof(iremote) + iremote.name_len +
1992 iremote.url_len) > datalen) {
1993 err = got_error(GOT_ERR_PRIVSEP_LEN);
1996 remote->name = strndup(imsg.data + sizeof(iremote),
1998 if (remote->name == NULL) {
1999 err = got_error_from_errno("strndup");
2002 remote->url = strndup(imsg.data + sizeof(iremote) +
2003 iremote.name_len, iremote.url_len);
2004 if (remote->url == NULL) {
2005 err = got_error_from_errno("strndup");
2012 err = got_error(GOT_ERR_PRIVSEP_MSG);
2023 for (i = 0; i < *nremotes; i++) {
2024 free((*remotes)[i].name);
2025 free((*remotes)[i].url);
2034 const struct got_error *
2035 got_privsep_send_commit_traversal_request(struct imsgbuf *ibuf,
2036 struct got_object_id *id, int idx, const char *path)
2038 const struct got_error *err = NULL;
2040 size_t path_len = strlen(path) + 1;
2042 wbuf = imsg_create(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_REQUEST, 0, 0,
2043 sizeof(struct got_imsg_commit_traversal_request) + path_len);
2045 return got_error_from_errno(
2046 "imsg_create COMMIT_TRAVERSAL_REQUEST");
2047 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
2048 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
2052 if (imsg_add(wbuf, &idx, sizeof(idx)) == -1) {
2053 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
2057 if (imsg_add(wbuf, path, path_len) == -1) {
2058 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
2064 imsg_close(ibuf, wbuf);
2066 return flush_imsg(ibuf);
2069 const struct got_error *
2070 got_privsep_send_traversed_commits(struct got_object_id *commit_ids,
2071 size_t ncommits, struct imsgbuf *ibuf)
2073 const struct got_error *err;
2077 wbuf = imsg_create(ibuf, GOT_IMSG_TRAVERSED_COMMITS, 0, 0,
2078 sizeof(struct got_imsg_traversed_commits) +
2079 ncommits * SHA1_DIGEST_LENGTH);
2081 return got_error_from_errno("imsg_create TRAVERSED_COMMITS");
2083 if (imsg_add(wbuf, &ncommits, sizeof(ncommits)) == -1) {
2084 err = got_error_from_errno("imsg_add TRAVERSED_COMMITS");
2088 for (i = 0; i < ncommits; i++) {
2089 struct got_object_id *id = &commit_ids[i];
2090 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
2091 err = got_error_from_errno(
2092 "imsg_add TRAVERSED_COMMITS");
2099 imsg_close(ibuf, wbuf);
2101 return flush_imsg(ibuf);
2104 const struct got_error *
2105 got_privsep_recv_traversed_commits(struct got_commit_object **changed_commit,
2106 struct got_object_id **changed_commit_id,
2107 struct got_object_id_queue *commit_ids, struct imsgbuf *ibuf)
2109 const struct got_error *err = NULL;
2111 struct got_imsg_traversed_commits *icommits;
2115 *changed_commit = NULL;
2116 *changed_commit_id = NULL;
2119 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2123 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2124 switch (imsg.hdr.type) {
2125 case GOT_IMSG_TRAVERSED_COMMITS:
2126 icommits = imsg.data;
2127 if (datalen != sizeof(*icommits) +
2128 icommits->ncommits * SHA1_DIGEST_LENGTH) {
2129 err = got_error(GOT_ERR_PRIVSEP_LEN);
2132 for (i = 0; i < icommits->ncommits; i++) {
2133 struct got_object_qid *qid;
2134 uint8_t *sha1 = (uint8_t *)imsg.data +
2135 sizeof(*icommits) + i * SHA1_DIGEST_LENGTH;
2136 err = got_object_qid_alloc_partial(&qid);
2139 memcpy(qid->id->sha1, sha1, SHA1_DIGEST_LENGTH);
2140 SIMPLEQ_INSERT_TAIL(commit_ids, qid, entry);
2142 /* The last commit may contain a change. */
2143 if (i == icommits->ncommits - 1) {
2144 *changed_commit_id =
2145 got_object_id_dup(qid->id);
2146 if (*changed_commit_id == NULL) {
2147 err = got_error_from_errno(
2148 "got_object_id_dup");
2154 case GOT_IMSG_COMMIT:
2155 if (*changed_commit_id == NULL) {
2156 err = got_error(GOT_ERR_PRIVSEP_MSG);
2159 err = get_commit_from_imsg(changed_commit, &imsg,
2162 case GOT_IMSG_COMMIT_TRAVERSAL_DONE:
2166 err = got_error(GOT_ERR_PRIVSEP_MSG);
2176 got_object_id_queue_free(commit_ids);
2180 const struct got_error *
2181 got_privsep_send_commit_traversal_done(struct imsgbuf *ibuf)
2183 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_DONE, 0, 0, -1,
2185 return got_error_from_errno("imsg_compose TRAVERSAL_DONE");
2187 return flush_imsg(ibuf);
2190 const struct got_error *
2191 got_privsep_unveil_exec_helpers(void)
2193 const char *helpers[] = {
2194 GOT_PATH_PROG_READ_PACK,
2195 GOT_PATH_PROG_READ_OBJECT,
2196 GOT_PATH_PROG_READ_COMMIT,
2197 GOT_PATH_PROG_READ_TREE,
2198 GOT_PATH_PROG_READ_BLOB,
2199 GOT_PATH_PROG_READ_TAG,
2200 GOT_PATH_PROG_READ_GITCONFIG,
2204 for (i = 0; i < nitems(helpers); i++) {
2205 if (unveil(helpers[i], "x") == 0)
2207 return got_error_from_errno2("unveil", helpers[i]);
2214 got_privsep_exec_child(int imsg_fds[2], const char *path, const char *repo_path)
2216 if (close(imsg_fds[0]) != 0) {
2217 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2221 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
2222 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2225 if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
2226 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2230 if (execl(path, path, repo_path, (char *)NULL) == -1) {
2231 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,