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_done(struct imsgbuf *ibuf, struct got_object_id hash)
593 if (imsg_compose(ibuf, GOT_IMSG_FETCH_DONE, 0, 0, -1,
594 hash.sha1, SHA1_DIGEST_LENGTH) == -1)
595 return got_error_from_errno("imsg_compose FETCH");
596 return flush_imsg(ibuf);
600 const struct got_error *
601 got_privsep_recv_fetch_progress(int *done, struct got_object_id **id,
602 char **refname, struct got_pathlist_head *symrefs, char **server_progress,
603 struct imsgbuf *ibuf)
605 const struct got_error *err = NULL;
608 struct got_imsg_fetch_symrefs *isymrefs = NULL;
616 *server_progress = NULL;
618 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
622 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
623 switch (imsg.hdr.type) {
625 if (datalen < sizeof(struct got_imsg_error)) {
626 err = got_error(GOT_ERR_PRIVSEP_LEN);
629 err = recv_imsg_error(&imsg, datalen);
631 case GOT_IMSG_FETCH_SYMREFS:
632 if (datalen < sizeof(*isymrefs)) {
633 err = got_error(GOT_ERR_PRIVSEP_LEN);
636 if (isymrefs != NULL) {
637 err = got_error(GOT_ERR_PRIVSEP_MSG);
640 isymrefs = (struct got_imsg_fetch_symrefs *)imsg.data;
641 off = sizeof(*isymrefs);
642 remain = datalen - off;
643 for (n = 0; n < isymrefs->nsymrefs; n++) {
644 struct got_imsg_fetch_symref *s;
646 if (remain < sizeof(struct got_imsg_fetch_symref)) {
647 err = got_error(GOT_ERR_PRIVSEP_LEN);
650 s = (struct got_imsg_fetch_symref *)(imsg.data + off);
652 remain -= sizeof(*s);
653 if (remain < s->name_len) {
654 err = got_error(GOT_ERR_PRIVSEP_LEN);
657 name = strndup(imsg.data + off, s->name_len);
659 err = got_error_from_errno("strndup");
663 remain -= s->name_len;
664 if (remain < s->target_len) {
665 err = got_error(GOT_ERR_PRIVSEP_LEN);
669 target = strndup(imsg.data + off, s->target_len);
670 if (target == NULL) {
671 err = got_error_from_errno("strndup");
675 off += s->target_len;
676 remain -= s->target_len;
677 err = got_pathlist_append(symrefs, name, target);
685 case GOT_IMSG_FETCH_REF:
686 if (datalen <= SHA1_DIGEST_LENGTH) {
687 err = got_error(GOT_ERR_PRIVSEP_MSG);
690 *id = malloc(sizeof(**id));
692 err = got_error_from_errno("malloc");
695 memcpy((*id)->sha1, imsg.data, SHA1_DIGEST_LENGTH);
696 *refname = strndup(imsg.data + SHA1_DIGEST_LENGTH,
697 datalen - SHA1_DIGEST_LENGTH);
698 if (*refname == NULL) {
699 err = got_error_from_errno("strndup");
703 case GOT_IMSG_FETCH_SERVER_PROGRESS:
705 err = got_error(GOT_ERR_PRIVSEP_LEN);
708 *server_progress = strndup(imsg.data, datalen);
709 if (*server_progress == NULL) {
710 err = got_error_from_errno("strndup");
713 for (i = 0; i < datalen; i++) {
714 if (!isprint((unsigned char)(*server_progress)[i]) &&
715 !isspace((unsigned char)(*server_progress)[i])) {
716 err = got_error(GOT_ERR_PRIVSEP_MSG);
717 free(*server_progress);
718 *server_progress = NULL;
723 case GOT_IMSG_FETCH_DONE:
724 *id = malloc(sizeof(**id));
726 err = got_error_from_errno("malloc");
729 if (datalen != SHA1_DIGEST_LENGTH) {
730 err = got_error(GOT_ERR_PRIVSEP_MSG);
733 memcpy((*id)->sha1, imsg.data, SHA1_DIGEST_LENGTH);
737 err = got_error(GOT_ERR_PRIVSEP_MSG);
751 const struct got_error *
752 got_privsep_send_index_pack_req(struct imsgbuf *ibuf, int fd, struct got_object_id *hash)
754 const struct got_error *err = NULL;
756 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_REQUEST, 0, 0, fd,
757 hash->sha1, SHA1_DIGEST_LENGTH) == -1) {
758 err = got_error_from_errno("imsg_compose INDEX_REQUEST");
762 return flush_imsg(ibuf);
765 const struct got_error *
766 got_privsep_send_index_pack_done(struct imsgbuf *ibuf)
768 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_DONE, 0, 0, -1, NULL, 0) == -1)
769 return got_error_from_errno("imsg_compose FETCH");
770 return flush_imsg(ibuf);
773 const struct got_error *
774 got_privsep_wait_index_pack_done(struct imsgbuf *ibuf)
776 const struct got_error *err = NULL;
779 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
782 if (imsg.hdr.type == GOT_IMSG_IDXPACK_DONE)
785 return got_error(GOT_ERR_PRIVSEP_MSG);
789 const struct got_error *
790 got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
791 struct imsgbuf *ibuf)
793 const struct got_error *err = NULL;
794 struct got_imsg_object *iobj;
795 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
797 if (datalen != sizeof(*iobj))
798 return got_error(GOT_ERR_PRIVSEP_LEN);
801 *obj = calloc(1, sizeof(**obj));
803 return got_error_from_errno("calloc");
805 memcpy((*obj)->id.sha1, iobj->id, SHA1_DIGEST_LENGTH);
806 (*obj)->type = iobj->type;
807 (*obj)->flags = iobj->flags;
808 (*obj)->hdrlen = iobj->hdrlen;
809 (*obj)->size = iobj->size;
810 /* path_packfile is handled by caller */
811 if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
812 (*obj)->pack_offset = iobj->pack_offset;
813 (*obj)->pack_idx = iobj->pack_idx;
819 const struct got_error *
820 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
822 const struct got_error *err = NULL;
824 const size_t min_datalen =
825 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
829 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
833 switch (imsg.hdr.type) {
834 case GOT_IMSG_OBJECT:
835 err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
838 err = got_error(GOT_ERR_PRIVSEP_MSG);
847 static const struct got_error *
848 send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
851 const struct got_error *err = NULL;
852 size_t offset, remain;
857 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
859 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
860 commit->logmsg + offset, n) == -1) {
861 err = got_error_from_errno("imsg_compose "
866 err = flush_imsg(ibuf);
877 const struct got_error *
878 got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
880 const struct got_error *err = NULL;
881 struct got_imsg_commit_object *icommit;
884 struct got_object_qid *qid;
885 size_t author_len = strlen(commit->author);
886 size_t committer_len = strlen(commit->committer);
887 size_t logmsg_len = strlen(commit->logmsg);
889 total = sizeof(*icommit) + author_len + committer_len +
890 commit->nparents * SHA1_DIGEST_LENGTH;
894 return got_error_from_errno("malloc");
896 icommit = (struct got_imsg_commit_object *)buf;
897 memcpy(icommit->tree_id, commit->tree_id->sha1,
898 sizeof(icommit->tree_id));
899 icommit->author_len = author_len;
900 icommit->author_time = commit->author_time;
901 icommit->author_gmtoff = commit->author_gmtoff;
902 icommit->committer_len = committer_len;
903 icommit->committer_time = commit->committer_time;
904 icommit->committer_gmtoff = commit->committer_gmtoff;
905 icommit->logmsg_len = logmsg_len;
906 icommit->nparents = commit->nparents;
908 len = sizeof(*icommit);
909 memcpy(buf + len, commit->author, author_len);
911 memcpy(buf + len, commit->committer, committer_len);
912 len += committer_len;
913 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
914 memcpy(buf + len, qid->id, SHA1_DIGEST_LENGTH);
915 len += SHA1_DIGEST_LENGTH;
918 if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
919 err = got_error_from_errno("imsg_compose COMMIT");
923 if (logmsg_len == 0 ||
924 logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
925 err = flush_imsg(ibuf);
929 err = send_commit_logmsg(ibuf, commit, logmsg_len);
935 static const struct got_error *
936 get_commit_from_imsg(struct got_commit_object **commit,
937 struct imsg *imsg, size_t datalen, struct imsgbuf *ibuf)
939 const struct got_error *err = NULL;
940 struct got_imsg_commit_object *icommit;
944 if (datalen < sizeof(*icommit))
945 return got_error(GOT_ERR_PRIVSEP_LEN);
947 icommit = imsg->data;
948 if (datalen != sizeof(*icommit) + icommit->author_len +
949 icommit->committer_len +
950 icommit->nparents * SHA1_DIGEST_LENGTH)
951 return got_error(GOT_ERR_PRIVSEP_LEN);
953 if (icommit->nparents < 0)
954 return got_error(GOT_ERR_PRIVSEP_LEN);
956 len += sizeof(*icommit);
958 *commit = got_object_commit_alloc_partial();
960 return got_error_from_errno(
961 "got_object_commit_alloc_partial");
963 memcpy((*commit)->tree_id->sha1, icommit->tree_id,
965 (*commit)->author_time = icommit->author_time;
966 (*commit)->author_gmtoff = icommit->author_gmtoff;
967 (*commit)->committer_time = icommit->committer_time;
968 (*commit)->committer_gmtoff = icommit->committer_gmtoff;
970 if (icommit->author_len == 0) {
971 (*commit)->author = strdup("");
972 if ((*commit)->author == NULL) {
973 err = got_error_from_errno("strdup");
977 (*commit)->author = malloc(icommit->author_len + 1);
978 if ((*commit)->author == NULL) {
979 err = got_error_from_errno("malloc");
982 memcpy((*commit)->author, imsg->data + len,
983 icommit->author_len);
984 (*commit)->author[icommit->author_len] = '\0';
986 len += icommit->author_len;
988 if (icommit->committer_len == 0) {
989 (*commit)->committer = strdup("");
990 if ((*commit)->committer == NULL) {
991 err = got_error_from_errno("strdup");
995 (*commit)->committer =
996 malloc(icommit->committer_len + 1);
997 if ((*commit)->committer == NULL) {
998 err = got_error_from_errno("malloc");
1001 memcpy((*commit)->committer, imsg->data + len,
1002 icommit->committer_len);
1003 (*commit)->committer[icommit->committer_len] = '\0';
1005 len += icommit->committer_len;
1007 if (icommit->logmsg_len == 0) {
1008 (*commit)->logmsg = strdup("");
1009 if ((*commit)->logmsg == NULL) {
1010 err = got_error_from_errno("strdup");
1014 size_t offset = 0, remain = icommit->logmsg_len;
1016 (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
1017 if ((*commit)->logmsg == NULL) {
1018 err = got_error_from_errno("malloc");
1021 while (remain > 0) {
1022 struct imsg imsg_log;
1023 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1026 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1030 if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG) {
1031 err = got_error(GOT_ERR_PRIVSEP_MSG);
1035 memcpy((*commit)->logmsg + offset,
1037 imsg_free(&imsg_log);
1041 (*commit)->logmsg[icommit->logmsg_len] = '\0';
1044 for (i = 0; i < icommit->nparents; i++) {
1045 struct got_object_qid *qid;
1047 err = got_object_qid_alloc_partial(&qid);
1050 memcpy(qid->id, imsg->data + len +
1051 i * SHA1_DIGEST_LENGTH, sizeof(*qid->id));
1052 SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
1053 (*commit)->nparents++;
1057 got_object_commit_close(*commit);
1063 const struct got_error *
1064 got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
1066 const struct got_error *err = NULL;
1069 const size_t min_datalen =
1070 MIN(sizeof(struct got_imsg_error),
1071 sizeof(struct got_imsg_commit_object));
1075 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1079 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1081 switch (imsg.hdr.type) {
1082 case GOT_IMSG_COMMIT:
1083 err = get_commit_from_imsg(commit, &imsg, datalen, ibuf);
1086 err = got_error(GOT_ERR_PRIVSEP_MSG);
1095 const struct got_error *
1096 got_privsep_send_tree(struct imsgbuf *ibuf, struct got_pathlist_head *entries,
1099 const struct got_error *err = NULL;
1100 struct got_imsg_tree_object itree;
1101 struct got_pathlist_entry *pe;
1103 int nimsg; /* number of imsg queued in ibuf */
1105 itree.nentries = nentries;
1106 if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
1108 return got_error_from_errno("imsg_compose TREE");
1110 totlen = sizeof(itree);
1112 TAILQ_FOREACH(pe, entries, entry) {
1113 const char *name = pe->path;
1114 struct got_parsed_tree_entry *pte = pe->data;
1116 size_t namelen = strlen(name);
1117 size_t len = sizeof(struct got_imsg_tree_entry) + namelen;
1119 if (len > MAX_IMSGSIZE)
1120 return got_error(GOT_ERR_NO_SPACE);
1123 if (totlen + len >= MAX_IMSGSIZE - (IMSG_HEADER_SIZE * nimsg)) {
1124 err = flush_imsg(ibuf);
1130 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_ENTRY, 0, 0, len);
1132 return got_error_from_errno("imsg_create TREE_ENTRY");
1134 /* Keep in sync with struct got_imsg_tree_object definition! */
1135 if (imsg_add(wbuf, pte->id, SHA1_DIGEST_LENGTH) == -1) {
1136 err = got_error_from_errno("imsg_add TREE_ENTRY");
1140 if (imsg_add(wbuf, &pte->mode, sizeof(pte->mode)) == -1) {
1141 err = got_error_from_errno("imsg_add TREE_ENTRY");
1146 if (imsg_add(wbuf, name, namelen) == -1) {
1147 err = got_error_from_errno("imsg_add TREE_ENTRY");
1153 imsg_close(ibuf, wbuf);
1158 return flush_imsg(ibuf);
1161 const struct got_error *
1162 got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
1164 const struct got_error *err = NULL;
1165 const size_t min_datalen =
1166 MIN(sizeof(struct got_imsg_error),
1167 sizeof(struct got_imsg_tree_object));
1168 struct got_imsg_tree_object *itree;
1173 err = read_imsg(ibuf);
1181 struct got_imsg_tree_entry *ite;
1182 struct got_tree_entry *te = NULL;
1184 n = imsg_get(ibuf, &imsg);
1186 if (*tree && (*tree)->nentries != nentries)
1191 if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen)
1192 return got_error(GOT_ERR_PRIVSEP_LEN);
1194 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1196 switch (imsg.hdr.type) {
1197 case GOT_IMSG_ERROR:
1198 err = recv_imsg_error(&imsg, datalen);
1201 /* This message should only appear once. */
1202 if (*tree != NULL) {
1203 err = got_error(GOT_ERR_PRIVSEP_MSG);
1206 if (datalen != sizeof(*itree)) {
1207 err = got_error(GOT_ERR_PRIVSEP_LEN);
1211 *tree = malloc(sizeof(**tree));
1212 if (*tree == NULL) {
1213 err = got_error_from_errno("malloc");
1216 (*tree)->entries = calloc(itree->nentries,
1217 sizeof(struct got_tree_entry));
1218 if ((*tree)->entries == NULL) {
1219 err = got_error_from_errno("malloc");
1222 (*tree)->nentries = itree->nentries;
1223 (*tree)->refcnt = 0;
1225 case GOT_IMSG_TREE_ENTRY:
1226 /* This message should be preceeded by GOT_IMSG_TREE. */
1227 if (*tree == NULL) {
1228 err = got_error(GOT_ERR_PRIVSEP_MSG);
1231 if (datalen < sizeof(*ite) || datalen > MAX_IMSGSIZE) {
1232 err = got_error(GOT_ERR_PRIVSEP_LEN);
1236 /* Remaining data contains the entry's name. */
1237 datalen -= sizeof(*ite);
1238 if (datalen == 0 || datalen > MAX_IMSGSIZE) {
1239 err = got_error(GOT_ERR_PRIVSEP_LEN);
1244 if (datalen + 1 > sizeof(te->name)) {
1245 err = got_error(GOT_ERR_NO_SPACE);
1248 te = &(*tree)->entries[nentries];
1249 memcpy(te->name, imsg.data + sizeof(*ite), datalen);
1250 te->name[datalen] = '\0';
1252 memcpy(te->id.sha1, ite->id, SHA1_DIGEST_LENGTH);
1253 te->mode = ite->mode;
1258 err = got_error(GOT_ERR_PRIVSEP_MSG);
1265 if (*tree && (*tree)->nentries != nentries) {
1267 err = got_error(GOT_ERR_PRIVSEP_LEN);
1268 got_object_tree_close(*tree);
1275 const struct got_error *
1276 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
1277 const uint8_t *data)
1279 struct got_imsg_blob iblob;
1282 iblob.hdrlen = hdrlen;
1287 if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
1288 return got_error(GOT_ERR_NO_SPACE);
1290 buf = malloc(sizeof(iblob) + size);
1292 return got_error_from_errno("malloc");
1294 memcpy(buf, &iblob, sizeof(iblob));
1295 memcpy(buf + sizeof(iblob), data, size);
1296 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
1297 sizeof(iblob) + size) == -1) {
1299 return got_error_from_errno("imsg_compose BLOB");
1303 /* Data has already been written to file descriptor. */
1304 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
1305 sizeof(iblob)) == -1)
1306 return got_error_from_errno("imsg_compose BLOB");
1310 return flush_imsg(ibuf);
1313 const struct got_error *
1314 got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1315 struct imsgbuf *ibuf)
1317 const struct got_error *err = NULL;
1319 struct got_imsg_blob *iblob;
1324 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1328 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1330 switch (imsg.hdr.type) {
1332 if (datalen < sizeof(*iblob)) {
1333 err = got_error(GOT_ERR_PRIVSEP_LEN);
1337 *size = iblob->size;
1338 *hdrlen = iblob->hdrlen;
1340 if (datalen == sizeof(*iblob)) {
1341 /* Data has been written to file descriptor. */
1345 if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX) {
1346 err = got_error(GOT_ERR_PRIVSEP_LEN);
1350 *outbuf = malloc(*size);
1351 if (*outbuf == NULL) {
1352 err = got_error_from_errno("malloc");
1355 memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
1358 err = got_error(GOT_ERR_PRIVSEP_MSG);
1367 static const struct got_error *
1368 send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
1370 const struct got_error *err = NULL;
1371 size_t offset, remain;
1374 remain = tagmsg_len;
1375 while (remain > 0) {
1376 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1378 if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
1379 tag->tagmsg + offset, n) == -1) {
1380 err = got_error_from_errno("imsg_compose TAG_TAGMSG");
1384 err = flush_imsg(ibuf);
1395 const struct got_error *
1396 got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
1398 const struct got_error *err = NULL;
1399 struct got_imsg_tag_object *itag;
1402 size_t tag_len = strlen(tag->tag);
1403 size_t tagger_len = strlen(tag->tagger);
1404 size_t tagmsg_len = strlen(tag->tagmsg);
1406 total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
1408 buf = malloc(total);
1410 return got_error_from_errno("malloc");
1412 itag = (struct got_imsg_tag_object *)buf;
1413 memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
1414 itag->obj_type = tag->obj_type;
1415 itag->tag_len = tag_len;
1416 itag->tagger_len = tagger_len;
1417 itag->tagger_time = tag->tagger_time;
1418 itag->tagger_gmtoff = tag->tagger_gmtoff;
1419 itag->tagmsg_len = tagmsg_len;
1421 len = sizeof(*itag);
1422 memcpy(buf + len, tag->tag, tag_len);
1424 memcpy(buf + len, tag->tagger, tagger_len);
1427 if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
1428 err = got_error_from_errno("imsg_compose TAG");
1432 if (tagmsg_len == 0 ||
1433 tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1434 err = flush_imsg(ibuf);
1438 err = send_tagmsg(ibuf, tag, tagmsg_len);
1444 const struct got_error *
1445 got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
1447 const struct got_error *err = NULL;
1449 struct got_imsg_tag_object *itag;
1450 size_t len, datalen;
1451 const size_t min_datalen =
1452 MIN(sizeof(struct got_imsg_error),
1453 sizeof(struct got_imsg_tag_object));
1457 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1461 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1464 switch (imsg.hdr.type) {
1466 if (datalen < sizeof(*itag)) {
1467 err = got_error(GOT_ERR_PRIVSEP_LEN);
1471 if (datalen != sizeof(*itag) + itag->tag_len +
1473 err = got_error(GOT_ERR_PRIVSEP_LEN);
1476 len += sizeof(*itag);
1478 *tag = calloc(1, sizeof(**tag));
1480 err = got_error_from_errno("calloc");
1484 memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
1486 if (itag->tag_len == 0) {
1487 (*tag)->tag = strdup("");
1488 if ((*tag)->tag == NULL) {
1489 err = got_error_from_errno("strdup");
1493 (*tag)->tag = malloc(itag->tag_len + 1);
1494 if ((*tag)->tag == NULL) {
1495 err = got_error_from_errno("malloc");
1498 memcpy((*tag)->tag, imsg.data + len,
1500 (*tag)->tag[itag->tag_len] = '\0';
1502 len += itag->tag_len;
1504 (*tag)->obj_type = itag->obj_type;
1505 (*tag)->tagger_time = itag->tagger_time;
1506 (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1508 if (itag->tagger_len == 0) {
1509 (*tag)->tagger = strdup("");
1510 if ((*tag)->tagger == NULL) {
1511 err = got_error_from_errno("strdup");
1515 (*tag)->tagger = malloc(itag->tagger_len + 1);
1516 if ((*tag)->tagger == NULL) {
1517 err = got_error_from_errno("malloc");
1520 memcpy((*tag)->tagger, imsg.data + len,
1522 (*tag)->tagger[itag->tagger_len] = '\0';
1524 len += itag->tagger_len;
1526 if (itag->tagmsg_len == 0) {
1527 (*tag)->tagmsg = strdup("");
1528 if ((*tag)->tagmsg == NULL) {
1529 err = got_error_from_errno("strdup");
1533 size_t offset = 0, remain = itag->tagmsg_len;
1535 (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1536 if ((*tag)->tagmsg == NULL) {
1537 err = got_error_from_errno("malloc");
1540 while (remain > 0) {
1541 struct imsg imsg_log;
1542 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1545 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1549 if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1550 return got_error(GOT_ERR_PRIVSEP_MSG);
1552 memcpy((*tag)->tagmsg + offset, imsg_log.data,
1554 imsg_free(&imsg_log);
1558 (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1563 err = got_error(GOT_ERR_PRIVSEP_MSG);
1572 const struct got_error *
1573 got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1574 struct got_packidx *packidx)
1576 const struct got_error *err = NULL;
1577 struct got_imsg_packidx ipackidx;
1578 struct got_imsg_pack ipack;
1581 ipackidx.len = packidx->len;
1582 fd = dup(packidx->fd);
1584 return got_error_from_errno("dup");
1586 if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
1587 sizeof(ipackidx)) == -1) {
1588 err = got_error_from_errno("imsg_compose PACKIDX");
1593 if (strlcpy(ipack.path_packfile, pack->path_packfile,
1594 sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
1595 return got_error(GOT_ERR_NO_SPACE);
1596 ipack.filesize = pack->filesize;
1600 return got_error_from_errno("dup");
1602 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
1604 err = got_error_from_errno("imsg_compose PACK");
1609 return flush_imsg(ibuf);
1612 const struct got_error *
1613 got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
1614 struct got_object_id *id)
1616 struct got_imsg_packed_object iobj;
1619 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
1621 if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
1622 &iobj, sizeof(iobj)) == -1)
1623 return got_error_from_errno("imsg_compose "
1624 "PACKED_OBJECT_REQUEST");
1626 return flush_imsg(ibuf);
1629 const struct got_error *
1630 got_privsep_send_gitconfig_parse_req(struct imsgbuf *ibuf, int fd)
1632 const struct got_error *err = NULL;
1634 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_PARSE_REQUEST, 0, 0, fd,
1636 err = got_error_from_errno("imsg_compose "
1637 "GITCONFIG_PARSE_REQUEST");
1642 return flush_imsg(ibuf);
1645 const struct got_error *
1646 got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *ibuf)
1648 if (imsg_compose(ibuf,
1649 GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST, 0, 0, -1,
1651 return got_error_from_errno("imsg_compose "
1652 "GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST");
1654 return flush_imsg(ibuf);
1657 const struct got_error *
1658 got_privsep_send_gitconfig_author_name_req(struct imsgbuf *ibuf)
1660 if (imsg_compose(ibuf,
1661 GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST, 0, 0, -1, NULL, 0) == -1)
1662 return got_error_from_errno("imsg_compose "
1663 "GITCONFIG_AUTHOR_NAME_REQUEST");
1665 return flush_imsg(ibuf);
1668 const struct got_error *
1669 got_privsep_send_gitconfig_author_email_req(struct imsgbuf *ibuf)
1671 if (imsg_compose(ibuf,
1672 GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST, 0, 0, -1, NULL, 0) == -1)
1673 return got_error_from_errno("imsg_compose "
1674 "GITCONFIG_AUTHOR_EMAIL_REQUEST");
1676 return flush_imsg(ibuf);
1679 const struct got_error *
1680 got_privsep_send_gitconfig_remotes_req(struct imsgbuf *ibuf)
1682 if (imsg_compose(ibuf,
1683 GOT_IMSG_GITCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
1684 return got_error_from_errno("imsg_compose "
1685 "GITCONFIG_REMOTE_REQUEST");
1687 return flush_imsg(ibuf);
1690 const struct got_error *
1691 got_privsep_send_gitconfig_owner_req(struct imsgbuf *ibuf)
1693 if (imsg_compose(ibuf,
1694 GOT_IMSG_GITCONFIG_OWNER_REQUEST, 0, 0, -1, NULL, 0) == -1)
1695 return got_error_from_errno("imsg_compose "
1696 "GITCONFIG_OWNER_REQUEST");
1698 return flush_imsg(ibuf);
1701 const struct got_error *
1702 got_privsep_send_gitconfig_str(struct imsgbuf *ibuf, const char *value)
1704 size_t len = value ? strlen(value) + 1 : 0;
1706 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_STR_VAL, 0, 0, -1,
1708 return got_error_from_errno("imsg_compose GITCONFIG_STR_VAL");
1710 return flush_imsg(ibuf);
1713 const struct got_error *
1714 got_privsep_recv_gitconfig_str(char **str, struct imsgbuf *ibuf)
1716 const struct got_error *err = NULL;
1719 const size_t min_datalen = 0;
1723 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1726 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1728 switch (imsg.hdr.type) {
1729 case GOT_IMSG_GITCONFIG_STR_VAL:
1732 *str = malloc(datalen);
1734 err = got_error_from_errno("malloc");
1737 if (strlcpy(*str, imsg.data, datalen) >= datalen)
1738 err = got_error(GOT_ERR_NO_SPACE);
1741 err = got_error(GOT_ERR_PRIVSEP_MSG);
1749 const struct got_error *
1750 got_privsep_send_gitconfig_int(struct imsgbuf *ibuf, int value)
1752 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_INT_VAL, 0, 0, -1,
1753 &value, sizeof(value)) == -1)
1754 return got_error_from_errno("imsg_compose GITCONFIG_INT_VAL");
1756 return flush_imsg(ibuf);
1759 const struct got_error *
1760 got_privsep_recv_gitconfig_int(int *val, struct imsgbuf *ibuf)
1762 const struct got_error *err = NULL;
1765 const size_t min_datalen =
1766 MIN(sizeof(struct got_imsg_error), sizeof(int));
1770 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1773 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1775 switch (imsg.hdr.type) {
1776 case GOT_IMSG_GITCONFIG_INT_VAL:
1777 if (datalen != sizeof(*val)) {
1778 err = got_error(GOT_ERR_PRIVSEP_LEN);
1781 memcpy(val, imsg.data, sizeof(*val));
1784 err = got_error(GOT_ERR_PRIVSEP_MSG);
1792 const struct got_error *
1793 got_privsep_send_gitconfig_remotes(struct imsgbuf *ibuf,
1794 struct got_remote_repo *remotes, int nremotes)
1796 const struct got_error *err = NULL;
1797 struct got_imsg_remotes iremotes;
1800 iremotes.nremotes = nremotes;
1801 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_REMOTES, 0, 0, -1,
1802 &iremotes, sizeof(iremotes)) == -1)
1803 return got_error_from_errno("imsg_compose GITCONFIG_REMOTES");
1805 err = flush_imsg(ibuf);
1810 for (i = 0; i < nremotes; i++) {
1811 struct got_imsg_remote iremote;
1812 size_t len = sizeof(iremote);
1815 iremote.name_len = strlen(remotes[i].name);
1816 len += iremote.name_len;
1817 iremote.url_len = strlen(remotes[i].url);
1818 len += iremote.url_len;
1820 wbuf = imsg_create(ibuf, GOT_IMSG_GITCONFIG_REMOTE, 0, 0, len);
1822 return got_error_from_errno(
1823 "imsg_create GITCONFIG_REMOTE");
1825 if (imsg_add(wbuf, &iremote, sizeof(iremote)) == -1) {
1826 err = got_error_from_errno(
1827 "imsg_add GIITCONFIG_REMOTE");
1832 if (imsg_add(wbuf, remotes[i].name, iremote.name_len) == -1) {
1833 err = got_error_from_errno(
1834 "imsg_add GIITCONFIG_REMOTE");
1838 if (imsg_add(wbuf, remotes[i].url, iremote.url_len) == -1) {
1839 err = got_error_from_errno(
1840 "imsg_add GIITCONFIG_REMOTE");
1846 imsg_close(ibuf, wbuf);
1847 err = flush_imsg(ibuf);
1855 const struct got_error *
1856 got_privsep_recv_gitconfig_remotes(struct got_remote_repo **remotes,
1857 int *nremotes, struct imsgbuf *ibuf)
1859 const struct got_error *err = NULL;
1862 struct got_imsg_remotes iremotes;
1863 struct got_imsg_remote iremote;
1867 iremotes.nremotes = 0;
1869 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremotes));
1872 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1874 switch (imsg.hdr.type) {
1875 case GOT_IMSG_GITCONFIG_REMOTES:
1876 if (datalen != sizeof(iremotes)) {
1877 err = got_error(GOT_ERR_PRIVSEP_LEN);
1880 memcpy(&iremotes, imsg.data, sizeof(iremotes));
1881 if (iremotes.nremotes == 0) {
1888 return got_error(GOT_ERR_PRIVSEP_MSG);
1893 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(iremote));
1894 if (*remotes == NULL)
1895 return got_error_from_errno("recallocarray");
1897 while (*nremotes < iremotes.nremotes) {
1898 struct got_remote_repo *remote;
1900 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremote));
1903 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1905 switch (imsg.hdr.type) {
1906 case GOT_IMSG_GITCONFIG_REMOTE:
1907 remote = &(*remotes)[*nremotes];
1908 if (datalen < sizeof(iremote)) {
1909 err = got_error(GOT_ERR_PRIVSEP_LEN);
1912 memcpy(&iremote, imsg.data, sizeof(iremote));
1913 if (iremote.name_len == 0 || iremote.url_len == 0 ||
1914 (sizeof(iremote) + iremote.name_len +
1915 iremote.url_len) > datalen) {
1916 err = got_error(GOT_ERR_PRIVSEP_LEN);
1919 remote->name = strndup(imsg.data + sizeof(iremote),
1921 if (remote->name == NULL) {
1922 err = got_error_from_errno("strndup");
1925 remote->url = strndup(imsg.data + sizeof(iremote) +
1926 iremote.name_len, iremote.url_len);
1927 if (remote->url == NULL) {
1928 err = got_error_from_errno("strndup");
1935 err = got_error(GOT_ERR_PRIVSEP_MSG);
1946 for (i = 0; i < *nremotes; i++) {
1947 free((*remotes)[i].name);
1948 free((*remotes)[i].url);
1957 const struct got_error *
1958 got_privsep_send_commit_traversal_request(struct imsgbuf *ibuf,
1959 struct got_object_id *id, int idx, const char *path)
1961 const struct got_error *err = NULL;
1963 size_t path_len = strlen(path) + 1;
1965 wbuf = imsg_create(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_REQUEST, 0, 0,
1966 sizeof(struct got_imsg_commit_traversal_request) + path_len);
1968 return got_error_from_errno(
1969 "imsg_create COMMIT_TRAVERSAL_REQUEST");
1970 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
1971 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
1975 if (imsg_add(wbuf, &idx, sizeof(idx)) == -1) {
1976 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
1980 if (imsg_add(wbuf, path, path_len) == -1) {
1981 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
1987 imsg_close(ibuf, wbuf);
1989 return flush_imsg(ibuf);
1992 const struct got_error *
1993 got_privsep_send_traversed_commits(struct got_object_id *commit_ids,
1994 size_t ncommits, struct imsgbuf *ibuf)
1996 const struct got_error *err;
2000 wbuf = imsg_create(ibuf, GOT_IMSG_TRAVERSED_COMMITS, 0, 0,
2001 sizeof(struct got_imsg_traversed_commits) +
2002 ncommits * SHA1_DIGEST_LENGTH);
2004 return got_error_from_errno("imsg_create TRAVERSED_COMMITS");
2006 if (imsg_add(wbuf, &ncommits, sizeof(ncommits)) == -1) {
2007 err = got_error_from_errno("imsg_add TRAVERSED_COMMITS");
2011 for (i = 0; i < ncommits; i++) {
2012 struct got_object_id *id = &commit_ids[i];
2013 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
2014 err = got_error_from_errno(
2015 "imsg_add TRAVERSED_COMMITS");
2022 imsg_close(ibuf, wbuf);
2024 return flush_imsg(ibuf);
2027 const struct got_error *
2028 got_privsep_recv_traversed_commits(struct got_commit_object **changed_commit,
2029 struct got_object_id **changed_commit_id,
2030 struct got_object_id_queue *commit_ids, struct imsgbuf *ibuf)
2032 const struct got_error *err = NULL;
2034 struct got_imsg_traversed_commits *icommits;
2038 *changed_commit = NULL;
2039 *changed_commit_id = NULL;
2042 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2046 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2047 switch (imsg.hdr.type) {
2048 case GOT_IMSG_TRAVERSED_COMMITS:
2049 icommits = imsg.data;
2050 if (datalen != sizeof(*icommits) +
2051 icommits->ncommits * SHA1_DIGEST_LENGTH) {
2052 err = got_error(GOT_ERR_PRIVSEP_LEN);
2055 for (i = 0; i < icommits->ncommits; i++) {
2056 struct got_object_qid *qid;
2057 uint8_t *sha1 = (uint8_t *)imsg.data +
2058 sizeof(*icommits) + i * SHA1_DIGEST_LENGTH;
2059 err = got_object_qid_alloc_partial(&qid);
2062 memcpy(qid->id->sha1, sha1, SHA1_DIGEST_LENGTH);
2063 SIMPLEQ_INSERT_TAIL(commit_ids, qid, entry);
2065 /* The last commit may contain a change. */
2066 if (i == icommits->ncommits - 1) {
2067 *changed_commit_id =
2068 got_object_id_dup(qid->id);
2069 if (*changed_commit_id == NULL) {
2070 err = got_error_from_errno(
2071 "got_object_id_dup");
2077 case GOT_IMSG_COMMIT:
2078 if (*changed_commit_id == NULL) {
2079 err = got_error(GOT_ERR_PRIVSEP_MSG);
2082 err = get_commit_from_imsg(changed_commit, &imsg,
2085 case GOT_IMSG_COMMIT_TRAVERSAL_DONE:
2089 err = got_error(GOT_ERR_PRIVSEP_MSG);
2099 got_object_id_queue_free(commit_ids);
2103 const struct got_error *
2104 got_privsep_send_commit_traversal_done(struct imsgbuf *ibuf)
2106 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_DONE, 0, 0, -1,
2108 return got_error_from_errno("imsg_compose TRAVERSAL_DONE");
2110 return flush_imsg(ibuf);
2113 const struct got_error *
2114 got_privsep_unveil_exec_helpers(void)
2116 const char *helpers[] = {
2117 GOT_PATH_PROG_READ_PACK,
2118 GOT_PATH_PROG_READ_OBJECT,
2119 GOT_PATH_PROG_READ_COMMIT,
2120 GOT_PATH_PROG_READ_TREE,
2121 GOT_PATH_PROG_READ_BLOB,
2122 GOT_PATH_PROG_READ_TAG,
2123 GOT_PATH_PROG_READ_GITCONFIG,
2127 for (i = 0; i < nitems(helpers); i++) {
2128 if (unveil(helpers[i], "x") == 0)
2130 return got_error_from_errno2("unveil", helpers[i]);
2137 got_privsep_exec_child(int imsg_fds[2], const char *path, const char *repo_path)
2139 if (close(imsg_fds[0]) != 0) {
2140 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2144 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
2145 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2148 if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
2149 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2153 if (execl(path, path, repo_path, (char *)NULL) == -1) {
2154 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,