2 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/types.h>
18 #include <sys/queue.h>
20 #include <sys/syslimits.h>
34 #include "got_object.h"
35 #include "got_error.h"
38 #include "got_lib_sha1.h"
39 #include "got_lib_delta.h"
40 #include "got_lib_inflate.h"
41 #include "got_lib_object.h"
42 #include "got_lib_object_parse.h"
43 #include "got_lib_privsep.h"
44 #include "got_lib_pack.h"
47 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
51 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
54 static const struct got_error *
55 poll_fd(int fd, int events, int timeout)
61 pfd[0].events = events;
63 n = poll(pfd, 1, timeout);
65 return got_error_from_errno("poll");
67 return got_error(GOT_ERR_TIMEOUT);
68 if (pfd[0].revents & (POLLERR | POLLNVAL))
69 return got_error_from_errno("poll error");
70 if (pfd[0].revents & (events | POLLHUP))
73 return got_error(GOT_ERR_INTERRUPT);
76 static const struct got_error *
77 read_imsg(struct imsgbuf *ibuf)
79 const struct got_error *err;
82 err = poll_fd(ibuf->fd, POLLIN, INFTIM);
88 if (errno == EAGAIN) /* Could be a file-descriptor leak. */
89 return got_error(GOT_ERR_PRIVSEP_NO_FD);
90 return got_error(GOT_ERR_PRIVSEP_READ);
93 return got_error(GOT_ERR_PRIVSEP_PIPE);
98 const struct got_error *
99 got_privsep_wait_for_child(pid_t pid)
103 if (waitpid(pid, &child_status, 0) == -1)
104 return got_error_from_errno("waitpid");
106 if (!WIFEXITED(child_status))
107 return got_error(GOT_ERR_PRIVSEP_DIED);
109 if (WEXITSTATUS(child_status) != 0)
110 return got_error(GOT_ERR_PRIVSEP_EXIT);
115 static const struct got_error *
116 recv_imsg_error(struct imsg *imsg, size_t datalen)
118 struct got_imsg_error *ierr;
120 if (datalen != sizeof(*ierr))
121 return got_error(GOT_ERR_PRIVSEP_LEN);
124 if (ierr->code == GOT_ERR_ERRNO) {
125 static struct got_error serr;
126 serr.code = GOT_ERR_ERRNO;
127 serr.msg = strerror(ierr->errno_code);
131 return got_error(ierr->code);
134 const struct got_error *
135 got_privsep_recv_imsg(struct imsg *imsg, struct imsgbuf *ibuf,
138 const struct got_error *err;
141 n = imsg_get(ibuf, imsg);
143 return got_error_from_errno("imsg_get");
146 err = read_imsg(ibuf);
149 n = imsg_get(ibuf, imsg);
152 if (imsg->hdr.len < IMSG_HEADER_SIZE + min_datalen)
153 return got_error(GOT_ERR_PRIVSEP_LEN);
155 if (imsg->hdr.type == GOT_IMSG_ERROR) {
156 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
157 return recv_imsg_error(imsg, datalen);
163 /* Attempt to send an error in an imsg. Complain on stderr as a last resort. */
165 got_privsep_send_error(struct imsgbuf *ibuf, const struct got_error *err)
167 const struct got_error *poll_err;
168 struct got_imsg_error ierr;
171 ierr.code = err->code;
172 if (err->code == GOT_ERR_ERRNO)
173 ierr.errno_code = errno;
176 ret = imsg_compose(ibuf, GOT_IMSG_ERROR, 0, 0, -1, &ierr, sizeof(ierr));
178 fprintf(stderr, "%s: error %d \"%s\": imsg_compose: %s\n",
179 getprogname(), err->code, err->msg, strerror(errno));
183 poll_err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
185 fprintf(stderr, "%s: error %d \"%s\": poll: %s\n",
186 getprogname(), err->code, err->msg, poll_err->msg);
190 ret = imsg_flush(ibuf);
192 fprintf(stderr, "%s: error %d \"%s\": imsg_flush: %s\n",
193 getprogname(), err->code, err->msg, strerror(errno));
198 static const struct got_error *
199 flush_imsg(struct imsgbuf *ibuf)
201 const struct got_error *err;
203 err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
207 if (imsg_flush(ibuf) == -1)
208 return got_error_from_errno("imsg_flush");
213 const struct got_error *
214 got_privsep_send_stop(int fd)
216 const struct got_error *err = NULL;
219 imsg_init(&ibuf, fd);
221 if (imsg_compose(&ibuf, GOT_IMSG_STOP, 0, 0, -1, NULL, 0) == -1)
222 return got_error_from_errno("imsg_compose STOP");
224 err = flush_imsg(&ibuf);
229 const struct got_error *
230 got_privsep_send_obj_req(struct imsgbuf *ibuf, int fd)
232 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_REQUEST, 0, 0, fd, NULL, 0)
234 return got_error_from_errno("imsg_compose OBJECT_REQUEST");
236 return flush_imsg(ibuf);
239 const struct got_error *
240 got_privsep_send_commit_req(struct imsgbuf *ibuf, int fd,
241 struct got_object_id *id, int pack_idx)
243 const struct got_error *err = NULL;
244 struct got_imsg_packed_object iobj, *iobjp;
247 if (id) { /* commit is packed */
249 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
257 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_REQUEST, 0, 0, fd, iobjp, len)
259 err = got_error_from_errno("imsg_compose COMMIT_REQUEST");
264 return flush_imsg(ibuf);
267 const struct got_error *
268 got_privsep_send_tree_req(struct imsgbuf *ibuf, int fd,
269 struct got_object_id *id, int pack_idx)
271 const struct got_error *err = NULL;
273 size_t len = id ? sizeof(struct got_imsg_packed_object) : 0;
275 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_REQUEST, 0, 0, len);
277 return got_error_from_errno("imsg_create TREE_REQUEST");
279 if (id) { /* tree is packed */
280 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
281 err = got_error_from_errno("imsg_add TREE_ENTRY");
286 if (imsg_add(wbuf, &pack_idx, sizeof(pack_idx)) == -1) {
287 err = got_error_from_errno("imsg_add TREE_ENTRY");
294 imsg_close(ibuf, wbuf);
296 return flush_imsg(ibuf);
299 const struct got_error *
300 got_privsep_send_tag_req(struct imsgbuf *ibuf, int fd,
301 struct got_object_id *id, int pack_idx)
303 struct got_imsg_packed_object iobj, *iobjp;
306 if (id) { /* tag is packed */
308 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
316 if (imsg_compose(ibuf, GOT_IMSG_TAG_REQUEST, 0, 0, fd, iobjp, len)
318 return got_error_from_errno("imsg_compose TAG_REQUEST");
320 return flush_imsg(ibuf);
323 const struct got_error *
324 got_privsep_send_blob_req(struct imsgbuf *ibuf, int infd,
325 struct got_object_id *id, int pack_idx)
327 const struct got_error *err = NULL;
328 struct got_imsg_packed_object iobj, *iobjp;
331 if (id) { /* blob is packed */
333 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
341 if (imsg_compose(ibuf, GOT_IMSG_BLOB_REQUEST, 0, 0, infd, iobjp, len)
343 err = got_error_from_errno("imsg_compose BLOB_REQUEST");
348 return flush_imsg(ibuf);
351 const struct got_error *
352 got_privsep_send_blob_outfd(struct imsgbuf *ibuf, int outfd)
354 const struct got_error *err = NULL;
356 if (imsg_compose(ibuf, GOT_IMSG_BLOB_OUTFD, 0, 0, outfd, NULL, 0)
358 err = got_error_from_errno("imsg_compose BLOB_OUTFD");
363 return flush_imsg(ibuf);
366 const struct got_error *
367 got_privsep_send_tmpfd(struct imsgbuf *ibuf, int fd)
369 const struct got_error *err = NULL;
371 if (imsg_compose(ibuf, GOT_IMSG_TMPFD, 0, 0, fd, NULL, 0)
373 err = got_error_from_errno("imsg_compose TMPFD");
378 return flush_imsg(ibuf);
381 const struct got_error *
382 got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj)
384 struct got_imsg_object iobj;
386 memcpy(iobj.id, obj->id.sha1, sizeof(iobj.id));
387 iobj.type = obj->type;
388 iobj.flags = obj->flags;
389 iobj.hdrlen = obj->hdrlen;
390 iobj.size = obj->size;
391 if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
392 iobj.pack_offset = obj->pack_offset;
393 iobj.pack_idx = obj->pack_idx;
396 if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
398 return got_error_from_errno("imsg_compose OBJECT");
400 return flush_imsg(ibuf);
403 const struct got_error *
404 got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
405 struct imsgbuf *ibuf)
407 const struct got_error *err = NULL;
408 struct got_imsg_object *iobj;
409 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
411 if (datalen != sizeof(*iobj))
412 return got_error(GOT_ERR_PRIVSEP_LEN);
415 *obj = calloc(1, sizeof(**obj));
417 return got_error_from_errno("calloc");
419 memcpy((*obj)->id.sha1, iobj->id, SHA1_DIGEST_LENGTH);
420 (*obj)->type = iobj->type;
421 (*obj)->flags = iobj->flags;
422 (*obj)->hdrlen = iobj->hdrlen;
423 (*obj)->size = iobj->size;
424 /* path_packfile is handled by caller */
425 if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
426 (*obj)->pack_offset = iobj->pack_offset;
427 (*obj)->pack_idx = iobj->pack_idx;
433 const struct got_error *
434 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
436 const struct got_error *err = NULL;
438 const size_t min_datalen =
439 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
443 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
447 switch (imsg.hdr.type) {
448 case GOT_IMSG_OBJECT:
449 err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
452 err = got_error(GOT_ERR_PRIVSEP_MSG);
461 static const struct got_error *
462 send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
465 const struct got_error *err = NULL;
466 size_t offset, remain;
471 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
473 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
474 commit->logmsg + offset, n) == -1) {
475 err = got_error_from_errno("imsg_compose "
480 err = flush_imsg(ibuf);
491 const struct got_error *
492 got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
494 const struct got_error *err = NULL;
495 struct got_imsg_commit_object *icommit;
498 struct got_object_qid *qid;
499 size_t author_len = strlen(commit->author);
500 size_t committer_len = strlen(commit->committer);
501 size_t logmsg_len = strlen(commit->logmsg);
503 total = sizeof(*icommit) + author_len + committer_len +
504 commit->nparents * SHA1_DIGEST_LENGTH;
508 return got_error_from_errno("malloc");
510 icommit = (struct got_imsg_commit_object *)buf;
511 memcpy(icommit->tree_id, commit->tree_id->sha1,
512 sizeof(icommit->tree_id));
513 icommit->author_len = author_len;
514 icommit->author_time = commit->author_time;
515 icommit->author_gmtoff = commit->author_gmtoff;
516 icommit->committer_len = committer_len;
517 icommit->committer_time = commit->committer_time;
518 icommit->committer_gmtoff = commit->committer_gmtoff;
519 icommit->logmsg_len = logmsg_len;
520 icommit->nparents = commit->nparents;
522 len = sizeof(*icommit);
523 memcpy(buf + len, commit->author, author_len);
525 memcpy(buf + len, commit->committer, committer_len);
526 len += committer_len;
527 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
528 memcpy(buf + len, qid->id, SHA1_DIGEST_LENGTH);
529 len += SHA1_DIGEST_LENGTH;
532 if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
533 err = got_error_from_errno("imsg_compose COMMIT");
537 if (logmsg_len == 0 ||
538 logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
539 err = flush_imsg(ibuf);
543 err = send_commit_logmsg(ibuf, commit, logmsg_len);
549 const struct got_error *
550 got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
552 const struct got_error *err = NULL;
554 struct got_imsg_commit_object *icommit;
557 const size_t min_datalen =
558 MIN(sizeof(struct got_imsg_error),
559 sizeof(struct got_imsg_commit_object));
563 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
567 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
570 switch (imsg.hdr.type) {
571 case GOT_IMSG_COMMIT:
572 if (datalen < sizeof(*icommit)) {
573 err = got_error(GOT_ERR_PRIVSEP_LEN);
577 if (datalen != sizeof(*icommit) + icommit->author_len +
578 icommit->committer_len +
579 icommit->nparents * SHA1_DIGEST_LENGTH) {
580 err = got_error(GOT_ERR_PRIVSEP_LEN);
583 if (icommit->nparents < 0) {
584 err = got_error(GOT_ERR_PRIVSEP_LEN);
587 len += sizeof(*icommit);
589 *commit = got_object_commit_alloc_partial();
590 if (*commit == NULL) {
591 err = got_error_from_errno(
592 "got_object_commit_alloc_partial");
596 memcpy((*commit)->tree_id->sha1, icommit->tree_id,
598 (*commit)->author_time = icommit->author_time;
599 (*commit)->author_gmtoff = icommit->author_gmtoff;
600 (*commit)->committer_time = icommit->committer_time;
601 (*commit)->committer_gmtoff = icommit->committer_gmtoff;
603 if (icommit->author_len == 0) {
604 (*commit)->author = strdup("");
605 if ((*commit)->author == NULL) {
606 err = got_error_from_errno("strdup");
610 (*commit)->author = malloc(icommit->author_len + 1);
611 if ((*commit)->author == NULL) {
612 err = got_error_from_errno("malloc");
615 memcpy((*commit)->author, imsg.data + len,
616 icommit->author_len);
617 (*commit)->author[icommit->author_len] = '\0';
619 len += icommit->author_len;
621 if (icommit->committer_len == 0) {
622 (*commit)->committer = strdup("");
623 if ((*commit)->committer == NULL) {
624 err = got_error_from_errno("strdup");
628 (*commit)->committer =
629 malloc(icommit->committer_len + 1);
630 if ((*commit)->committer == NULL) {
631 err = got_error_from_errno("malloc");
634 memcpy((*commit)->committer, imsg.data + len,
635 icommit->committer_len);
636 (*commit)->committer[icommit->committer_len] = '\0';
638 len += icommit->committer_len;
640 if (icommit->logmsg_len == 0) {
641 (*commit)->logmsg = strdup("");
642 if ((*commit)->logmsg == NULL) {
643 err = got_error_from_errno("strdup");
647 size_t offset = 0, remain = icommit->logmsg_len;
649 (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
650 if ((*commit)->logmsg == NULL) {
651 err = got_error_from_errno("malloc");
655 struct imsg imsg_log;
656 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
659 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
663 if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG)
664 return got_error(GOT_ERR_PRIVSEP_MSG);
666 memcpy((*commit)->logmsg + offset,
668 imsg_free(&imsg_log);
672 (*commit)->logmsg[icommit->logmsg_len] = '\0';
675 for (i = 0; i < icommit->nparents; i++) {
676 struct got_object_qid *qid;
678 err = got_object_qid_alloc_partial(&qid);
681 memcpy(qid->id, imsg.data + len +
682 i * SHA1_DIGEST_LENGTH, sizeof(*qid->id));
683 SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
684 (*commit)->nparents++;
688 err = got_error(GOT_ERR_PRIVSEP_MSG);
697 const struct got_error *
698 got_privsep_send_tree(struct imsgbuf *ibuf, struct got_pathlist_head *entries,
701 const struct got_error *err = NULL;
702 struct got_imsg_tree_object itree;
703 struct got_pathlist_entry *pe;
705 int nimsg; /* number of imsg queued in ibuf */
707 itree.nentries = nentries;
708 if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
710 return got_error_from_errno("imsg_compose TREE");
712 totlen = sizeof(itree);
714 TAILQ_FOREACH(pe, entries, entry) {
715 const char *name = pe->path;
716 struct got_parsed_tree_entry *pte = pe->data;
718 size_t namelen = strlen(name);
719 size_t len = sizeof(struct got_imsg_tree_object) + namelen;
721 if (len > MAX_IMSGSIZE)
722 return got_error(GOT_ERR_NO_SPACE);
725 if (totlen + len >= MAX_IMSGSIZE - (IMSG_HEADER_SIZE * nimsg)) {
726 err = flush_imsg(ibuf);
732 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_ENTRY, 0, 0, len);
734 return got_error_from_errno("imsg_create TREE_ENTRY");
736 /* Keep in sync with struct got_imsg_tree_object definition! */
737 if (imsg_add(wbuf, pte->id, SHA1_DIGEST_LENGTH) == -1)
738 err = got_error_from_errno("imsg_add TREE_ENTRY");
741 if (imsg_add(wbuf, &pte->mode, sizeof(pte->mode)) == -1)
742 err = got_error_from_errno("imsg_add TREE_ENTRY");
746 if (imsg_add(wbuf, name, namelen) == -1)
747 err = got_error_from_errno("imsg_add TREE_ENTRY");
752 imsg_close(ibuf, wbuf);
757 return flush_imsg(ibuf);
760 const struct got_error *
761 got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
763 const struct got_error *err = NULL;
764 const size_t min_datalen =
765 MIN(sizeof(struct got_imsg_error),
766 sizeof(struct got_imsg_tree_object));
767 struct got_imsg_tree_object *itree;
772 err = read_imsg(ibuf);
780 struct got_imsg_tree_entry *ite;
781 struct got_tree_entry *te = NULL;
783 n = imsg_get(ibuf, &imsg);
785 if (*tree && (*tree)->entries.nentries != nentries)
790 if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen)
791 return got_error(GOT_ERR_PRIVSEP_LEN);
793 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
795 switch (imsg.hdr.type) {
797 err = recv_imsg_error(&imsg, datalen);
800 /* This message should only appear once. */
802 err = got_error(GOT_ERR_PRIVSEP_MSG);
805 if (datalen != sizeof(*itree)) {
806 err = got_error(GOT_ERR_PRIVSEP_LEN);
810 *tree = malloc(sizeof(**tree));
812 err = got_error_from_errno("malloc");
815 (*tree)->entries.nentries = itree->nentries;
816 SIMPLEQ_INIT(&(*tree)->entries.head);
819 case GOT_IMSG_TREE_ENTRY:
820 /* This message should be preceeded by GOT_IMSG_TREE. */
822 err = got_error(GOT_ERR_PRIVSEP_MSG);
825 if (datalen < sizeof(*ite) || datalen > MAX_IMSGSIZE) {
826 err = got_error(GOT_ERR_PRIVSEP_LEN);
830 /* Remaining data contains the entry's name. */
831 datalen -= sizeof(*ite);
832 if (datalen == 0 || datalen > MAX_IMSGSIZE) {
833 err = got_error(GOT_ERR_PRIVSEP_LEN);
838 te = got_alloc_tree_entry_partial();
840 err = got_error_from_errno(
841 "got_alloc_tree_entry_partial");
844 te->name = malloc(datalen + 1);
845 if (te->name == NULL) {
847 err = got_error_from_errno("malloc");
850 memcpy(te->name, imsg.data + sizeof(*ite), datalen);
851 te->name[datalen] = '\0';
853 memcpy(te->id->sha1, ite->id, SHA1_DIGEST_LENGTH);
854 te->mode = ite->mode;
855 SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
859 err = got_error(GOT_ERR_PRIVSEP_MSG);
866 if (*tree && (*tree)->entries.nentries != nentries) {
868 err = got_error(GOT_ERR_PRIVSEP_LEN);
869 got_object_tree_close(*tree);
876 const struct got_error *
877 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
880 struct got_imsg_blob iblob;
883 iblob.hdrlen = hdrlen;
888 if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
889 return got_error(GOT_ERR_NO_SPACE);
891 buf = malloc(sizeof(iblob) + size);
893 return got_error_from_errno("malloc");
895 memcpy(buf, &iblob, sizeof(iblob));
896 memcpy(buf + sizeof(iblob), data, size);
897 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
898 sizeof(iblob) + size) == -1) {
900 return got_error_from_errno("imsg_compose BLOB");
904 /* Data has already been written to file descriptor. */
905 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
906 sizeof(iblob)) == -1)
907 return got_error_from_errno("imsg_compose BLOB");
911 return flush_imsg(ibuf);
914 const struct got_error *
915 got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
916 struct imsgbuf *ibuf)
918 const struct got_error *err = NULL;
920 struct got_imsg_blob *iblob;
925 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
929 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
931 switch (imsg.hdr.type) {
933 if (datalen < sizeof(*iblob)) {
934 err = got_error(GOT_ERR_PRIVSEP_LEN);
939 *hdrlen = iblob->hdrlen;
941 if (datalen == sizeof(*iblob)) {
942 /* Data has been written to file descriptor. */
946 if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX) {
947 err = got_error(GOT_ERR_PRIVSEP_LEN);
951 *outbuf = malloc(*size);
952 if (*outbuf == NULL) {
953 err = got_error_from_errno("malloc");
956 memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
959 err = got_error(GOT_ERR_PRIVSEP_MSG);
968 static const struct got_error *
969 send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
971 const struct got_error *err = NULL;
972 size_t offset, remain;
977 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
979 if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
980 tag->tagmsg + offset, n) == -1) {
981 err = got_error_from_errno("imsg_compose TAG_TAGMSG");
985 err = flush_imsg(ibuf);
996 const struct got_error *
997 got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
999 const struct got_error *err = NULL;
1000 struct got_imsg_tag_object *itag;
1003 size_t tag_len = strlen(tag->tag);
1004 size_t tagger_len = strlen(tag->tagger);
1005 size_t tagmsg_len = strlen(tag->tagmsg);
1007 total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
1009 buf = malloc(total);
1011 return got_error_from_errno("malloc");
1013 itag = (struct got_imsg_tag_object *)buf;
1014 memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
1015 itag->obj_type = tag->obj_type;
1016 itag->tag_len = tag_len;
1017 itag->tagger_len = tagger_len;
1018 itag->tagger_time = tag->tagger_time;
1019 itag->tagger_gmtoff = tag->tagger_gmtoff;
1020 itag->tagmsg_len = tagmsg_len;
1022 len = sizeof(*itag);
1023 memcpy(buf + len, tag->tag, tag_len);
1025 memcpy(buf + len, tag->tagger, tagger_len);
1028 if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
1029 err = got_error_from_errno("imsg_compose TAG");
1033 if (tagmsg_len == 0 ||
1034 tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1035 err = flush_imsg(ibuf);
1039 err = send_tagmsg(ibuf, tag, tagmsg_len);
1045 const struct got_error *
1046 got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
1048 const struct got_error *err = NULL;
1050 struct got_imsg_tag_object *itag;
1051 size_t len, datalen;
1052 const size_t min_datalen =
1053 MIN(sizeof(struct got_imsg_error),
1054 sizeof(struct got_imsg_tag_object));
1058 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1062 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1065 switch (imsg.hdr.type) {
1067 if (datalen < sizeof(*itag)) {
1068 err = got_error(GOT_ERR_PRIVSEP_LEN);
1072 if (datalen != sizeof(*itag) + itag->tag_len +
1074 err = got_error(GOT_ERR_PRIVSEP_LEN);
1077 len += sizeof(*itag);
1079 *tag = calloc(1, sizeof(**tag));
1081 err = got_error_from_errno("calloc");
1085 memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
1087 if (itag->tag_len == 0) {
1088 (*tag)->tag = strdup("");
1089 if ((*tag)->tag == NULL) {
1090 err = got_error_from_errno("strdup");
1094 (*tag)->tag = malloc(itag->tag_len + 1);
1095 if ((*tag)->tag == NULL) {
1096 err = got_error_from_errno("malloc");
1099 memcpy((*tag)->tag, imsg.data + len,
1101 (*tag)->tag[itag->tag_len] = '\0';
1103 len += itag->tag_len;
1105 (*tag)->obj_type = itag->obj_type;
1106 (*tag)->tagger_time = itag->tagger_time;
1107 (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1109 if (itag->tagger_len == 0) {
1110 (*tag)->tagger = strdup("");
1111 if ((*tag)->tagger == NULL) {
1112 err = got_error_from_errno("strdup");
1116 (*tag)->tagger = malloc(itag->tagger_len + 1);
1117 if ((*tag)->tagger == NULL) {
1118 err = got_error_from_errno("malloc");
1121 memcpy((*tag)->tagger, imsg.data + len,
1123 (*tag)->tagger[itag->tagger_len] = '\0';
1125 len += itag->tagger_len;
1127 if (itag->tagmsg_len == 0) {
1128 (*tag)->tagmsg = strdup("");
1129 if ((*tag)->tagmsg == NULL) {
1130 err = got_error_from_errno("strdup");
1134 size_t offset = 0, remain = itag->tagmsg_len;
1136 (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1137 if ((*tag)->tagmsg == NULL) {
1138 err = got_error_from_errno("malloc");
1141 while (remain > 0) {
1142 struct imsg imsg_log;
1143 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1146 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1150 if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1151 return got_error(GOT_ERR_PRIVSEP_MSG);
1153 memcpy((*tag)->tagmsg + offset, imsg_log.data,
1155 imsg_free(&imsg_log);
1159 (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1164 err = got_error(GOT_ERR_PRIVSEP_MSG);
1173 const struct got_error *
1174 got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1175 struct got_packidx *packidx)
1177 const struct got_error *err = NULL;
1178 struct got_imsg_packidx ipackidx;
1179 struct got_imsg_pack ipack;
1182 ipackidx.len = packidx->len;
1183 fd = dup(packidx->fd);
1185 return got_error_from_errno("dup");
1187 if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
1188 sizeof(ipackidx)) == -1) {
1189 err = got_error_from_errno("imsg_compose PACKIDX");
1194 if (strlcpy(ipack.path_packfile, pack->path_packfile,
1195 sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
1196 return got_error(GOT_ERR_NO_SPACE);
1197 ipack.filesize = pack->filesize;
1201 return got_error_from_errno("dup");
1203 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
1205 err = got_error_from_errno("imsg_compose PACK");
1210 return flush_imsg(ibuf);
1213 const struct got_error *
1214 got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
1215 struct got_object_id *id)
1217 struct got_imsg_packed_object iobj;
1220 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
1222 if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
1223 &iobj, sizeof(iobj)) == -1)
1224 return got_error_from_errno("imsg_compose "
1225 "PACKED_OBJECT_REQUEST");
1227 return flush_imsg(ibuf);
1230 const struct got_error *
1231 got_privsep_send_gitconfig_parse_req(struct imsgbuf *ibuf, int fd)
1233 const struct got_error *err = NULL;
1235 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_PARSE_REQUEST, 0, 0, fd,
1237 err = got_error_from_errno("imsg_compose "
1238 "GITCONFIG_PARSE_REQUEST");
1243 return flush_imsg(ibuf);
1246 const struct got_error *
1247 got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *ibuf)
1249 if (imsg_compose(ibuf,
1250 GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST, 0, 0, -1,
1252 return got_error_from_errno("imsg_compose "
1253 "GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST");
1255 return flush_imsg(ibuf);
1258 const struct got_error *
1259 got_privsep_send_gitconfig_author_name_req(struct imsgbuf *ibuf)
1261 if (imsg_compose(ibuf,
1262 GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST, 0, 0, -1, NULL, 0) == -1)
1263 return got_error_from_errno("imsg_compose "
1264 "GITCONFIG_AUTHOR_NAME_REQUEST");
1266 return flush_imsg(ibuf);
1269 const struct got_error *
1270 got_privsep_send_gitconfig_author_email_req(struct imsgbuf *ibuf)
1272 if (imsg_compose(ibuf,
1273 GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST, 0, 0, -1, NULL, 0) == -1)
1274 return got_error_from_errno("imsg_compose "
1275 "GITCONFIG_AUTHOR_EMAIL_REQUEST");
1277 return flush_imsg(ibuf);
1280 const struct got_error *
1281 got_privsep_send_gitconfig_str(struct imsgbuf *ibuf, const char *value)
1283 size_t len = value ? strlen(value) + 1 : 0;
1285 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_STR_VAL, 0, 0, -1,
1287 return got_error_from_errno("imsg_compose GITCONFIG_STR_VAL");
1289 return flush_imsg(ibuf);
1292 const struct got_error *
1293 got_privsep_recv_gitconfig_str(char **str, struct imsgbuf *ibuf)
1295 const struct got_error *err = NULL;
1298 const size_t min_datalen = 0;
1302 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1305 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1307 switch (imsg.hdr.type) {
1308 case GOT_IMSG_GITCONFIG_STR_VAL:
1311 *str = malloc(datalen);
1313 err = got_error_from_errno("malloc");
1316 if (strlcpy(*str, imsg.data, datalen) >= datalen)
1317 err = got_error(GOT_ERR_NO_SPACE);
1320 err = got_error(GOT_ERR_PRIVSEP_MSG);
1328 const struct got_error *
1329 got_privsep_send_gitconfig_int(struct imsgbuf *ibuf, int value)
1331 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_INT_VAL, 0, 0, -1,
1332 &value, sizeof(value)) == -1)
1333 return got_error_from_errno("imsg_compose GITCONFIG_INT_VAL");
1335 return flush_imsg(ibuf);
1338 const struct got_error *
1339 got_privsep_recv_gitconfig_int(int *val, struct imsgbuf *ibuf)
1341 const struct got_error *err = NULL;
1344 const size_t min_datalen =
1345 MIN(sizeof(struct got_imsg_error), sizeof(int));
1349 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1352 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1354 switch (imsg.hdr.type) {
1355 case GOT_IMSG_GITCONFIG_INT_VAL:
1356 if (datalen != sizeof(*val)) {
1357 err = got_error(GOT_ERR_PRIVSEP_LEN);
1360 memcpy(val, imsg.data, sizeof(*val));
1363 err = got_error(GOT_ERR_PRIVSEP_MSG);
1371 const struct got_error *
1372 got_privsep_unveil_exec_helpers(void)
1374 const char *helpers[] = {
1375 GOT_PATH_PROG_READ_PACK,
1376 GOT_PATH_PROG_READ_OBJECT,
1377 GOT_PATH_PROG_READ_COMMIT,
1378 GOT_PATH_PROG_READ_TREE,
1379 GOT_PATH_PROG_READ_BLOB,
1380 GOT_PATH_PROG_READ_TAG,
1381 GOT_PATH_PROG_READ_GITCONFIG,
1385 for (i = 0; i < nitems(helpers); i++) {
1386 if (unveil(helpers[i], "x") == 0)
1388 return got_error_from_errno2("unveil", helpers[i]);
1395 got_privsep_exec_child(int imsg_fds[2], const char *path, const char *repo_path)
1397 if (close(imsg_fds[0]) != 0) {
1398 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
1402 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
1403 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
1406 if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
1407 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
1411 if (execl(path, path, repo_path, (char *)NULL) == -1) {
1412 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,