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_entry) + 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");
742 if (imsg_add(wbuf, &pte->mode, sizeof(pte->mode)) == -1) {
743 err = got_error_from_errno("imsg_add TREE_ENTRY");
748 if (imsg_add(wbuf, name, namelen) == -1) {
749 err = got_error_from_errno("imsg_add TREE_ENTRY");
755 imsg_close(ibuf, wbuf);
760 return flush_imsg(ibuf);
763 const struct got_error *
764 got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
766 const struct got_error *err = NULL;
767 const size_t min_datalen =
768 MIN(sizeof(struct got_imsg_error),
769 sizeof(struct got_imsg_tree_object));
770 struct got_imsg_tree_object *itree;
775 err = read_imsg(ibuf);
783 struct got_imsg_tree_entry *ite;
784 struct got_tree_entry *te = NULL;
786 n = imsg_get(ibuf, &imsg);
788 if (*tree && (*tree)->nentries != nentries)
793 if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen)
794 return got_error(GOT_ERR_PRIVSEP_LEN);
796 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
798 switch (imsg.hdr.type) {
800 err = recv_imsg_error(&imsg, datalen);
803 /* This message should only appear once. */
805 err = got_error(GOT_ERR_PRIVSEP_MSG);
808 if (datalen != sizeof(*itree)) {
809 err = got_error(GOT_ERR_PRIVSEP_LEN);
813 *tree = malloc(sizeof(**tree));
815 err = got_error_from_errno("malloc");
818 (*tree)->entries = calloc(itree->nentries,
819 sizeof(struct got_tree_entry));
820 if ((*tree)->entries == NULL) {
821 err = got_error_from_errno("malloc");
824 (*tree)->nentries = itree->nentries;
827 case GOT_IMSG_TREE_ENTRY:
828 /* This message should be preceeded by GOT_IMSG_TREE. */
830 err = got_error(GOT_ERR_PRIVSEP_MSG);
833 if (datalen < sizeof(*ite) || datalen > MAX_IMSGSIZE) {
834 err = got_error(GOT_ERR_PRIVSEP_LEN);
838 /* Remaining data contains the entry's name. */
839 datalen -= sizeof(*ite);
840 if (datalen == 0 || datalen > MAX_IMSGSIZE) {
841 err = got_error(GOT_ERR_PRIVSEP_LEN);
846 if (datalen + 1 > sizeof(te->name)) {
847 err = got_error(GOT_ERR_NO_SPACE);
850 te = &(*tree)->entries[nentries];
851 memcpy(te->name, imsg.data + sizeof(*ite), datalen);
852 te->name[datalen] = '\0';
854 memcpy(te->id.sha1, ite->id, SHA1_DIGEST_LENGTH);
855 te->mode = ite->mode;
860 err = got_error(GOT_ERR_PRIVSEP_MSG);
867 if (*tree && (*tree)->nentries != nentries) {
869 err = got_error(GOT_ERR_PRIVSEP_LEN);
870 got_object_tree_close(*tree);
877 const struct got_error *
878 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
881 struct got_imsg_blob iblob;
884 iblob.hdrlen = hdrlen;
889 if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
890 return got_error(GOT_ERR_NO_SPACE);
892 buf = malloc(sizeof(iblob) + size);
894 return got_error_from_errno("malloc");
896 memcpy(buf, &iblob, sizeof(iblob));
897 memcpy(buf + sizeof(iblob), data, size);
898 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
899 sizeof(iblob) + size) == -1) {
901 return got_error_from_errno("imsg_compose BLOB");
905 /* Data has already been written to file descriptor. */
906 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
907 sizeof(iblob)) == -1)
908 return got_error_from_errno("imsg_compose BLOB");
912 return flush_imsg(ibuf);
915 const struct got_error *
916 got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
917 struct imsgbuf *ibuf)
919 const struct got_error *err = NULL;
921 struct got_imsg_blob *iblob;
926 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
930 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
932 switch (imsg.hdr.type) {
934 if (datalen < sizeof(*iblob)) {
935 err = got_error(GOT_ERR_PRIVSEP_LEN);
940 *hdrlen = iblob->hdrlen;
942 if (datalen == sizeof(*iblob)) {
943 /* Data has been written to file descriptor. */
947 if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX) {
948 err = got_error(GOT_ERR_PRIVSEP_LEN);
952 *outbuf = malloc(*size);
953 if (*outbuf == NULL) {
954 err = got_error_from_errno("malloc");
957 memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
960 err = got_error(GOT_ERR_PRIVSEP_MSG);
969 static const struct got_error *
970 send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
972 const struct got_error *err = NULL;
973 size_t offset, remain;
978 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
980 if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
981 tag->tagmsg + offset, n) == -1) {
982 err = got_error_from_errno("imsg_compose TAG_TAGMSG");
986 err = flush_imsg(ibuf);
997 const struct got_error *
998 got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
1000 const struct got_error *err = NULL;
1001 struct got_imsg_tag_object *itag;
1004 size_t tag_len = strlen(tag->tag);
1005 size_t tagger_len = strlen(tag->tagger);
1006 size_t tagmsg_len = strlen(tag->tagmsg);
1008 total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
1010 buf = malloc(total);
1012 return got_error_from_errno("malloc");
1014 itag = (struct got_imsg_tag_object *)buf;
1015 memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
1016 itag->obj_type = tag->obj_type;
1017 itag->tag_len = tag_len;
1018 itag->tagger_len = tagger_len;
1019 itag->tagger_time = tag->tagger_time;
1020 itag->tagger_gmtoff = tag->tagger_gmtoff;
1021 itag->tagmsg_len = tagmsg_len;
1023 len = sizeof(*itag);
1024 memcpy(buf + len, tag->tag, tag_len);
1026 memcpy(buf + len, tag->tagger, tagger_len);
1029 if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
1030 err = got_error_from_errno("imsg_compose TAG");
1034 if (tagmsg_len == 0 ||
1035 tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1036 err = flush_imsg(ibuf);
1040 err = send_tagmsg(ibuf, tag, tagmsg_len);
1046 const struct got_error *
1047 got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
1049 const struct got_error *err = NULL;
1051 struct got_imsg_tag_object *itag;
1052 size_t len, datalen;
1053 const size_t min_datalen =
1054 MIN(sizeof(struct got_imsg_error),
1055 sizeof(struct got_imsg_tag_object));
1059 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1063 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1066 switch (imsg.hdr.type) {
1068 if (datalen < sizeof(*itag)) {
1069 err = got_error(GOT_ERR_PRIVSEP_LEN);
1073 if (datalen != sizeof(*itag) + itag->tag_len +
1075 err = got_error(GOT_ERR_PRIVSEP_LEN);
1078 len += sizeof(*itag);
1080 *tag = calloc(1, sizeof(**tag));
1082 err = got_error_from_errno("calloc");
1086 memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
1088 if (itag->tag_len == 0) {
1089 (*tag)->tag = strdup("");
1090 if ((*tag)->tag == NULL) {
1091 err = got_error_from_errno("strdup");
1095 (*tag)->tag = malloc(itag->tag_len + 1);
1096 if ((*tag)->tag == NULL) {
1097 err = got_error_from_errno("malloc");
1100 memcpy((*tag)->tag, imsg.data + len,
1102 (*tag)->tag[itag->tag_len] = '\0';
1104 len += itag->tag_len;
1106 (*tag)->obj_type = itag->obj_type;
1107 (*tag)->tagger_time = itag->tagger_time;
1108 (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1110 if (itag->tagger_len == 0) {
1111 (*tag)->tagger = strdup("");
1112 if ((*tag)->tagger == NULL) {
1113 err = got_error_from_errno("strdup");
1117 (*tag)->tagger = malloc(itag->tagger_len + 1);
1118 if ((*tag)->tagger == NULL) {
1119 err = got_error_from_errno("malloc");
1122 memcpy((*tag)->tagger, imsg.data + len,
1124 (*tag)->tagger[itag->tagger_len] = '\0';
1126 len += itag->tagger_len;
1128 if (itag->tagmsg_len == 0) {
1129 (*tag)->tagmsg = strdup("");
1130 if ((*tag)->tagmsg == NULL) {
1131 err = got_error_from_errno("strdup");
1135 size_t offset = 0, remain = itag->tagmsg_len;
1137 (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1138 if ((*tag)->tagmsg == NULL) {
1139 err = got_error_from_errno("malloc");
1142 while (remain > 0) {
1143 struct imsg imsg_log;
1144 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1147 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1151 if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1152 return got_error(GOT_ERR_PRIVSEP_MSG);
1154 memcpy((*tag)->tagmsg + offset, imsg_log.data,
1156 imsg_free(&imsg_log);
1160 (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1165 err = got_error(GOT_ERR_PRIVSEP_MSG);
1174 const struct got_error *
1175 got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1176 struct got_packidx *packidx)
1178 const struct got_error *err = NULL;
1179 struct got_imsg_packidx ipackidx;
1180 struct got_imsg_pack ipack;
1183 ipackidx.len = packidx->len;
1184 fd = dup(packidx->fd);
1186 return got_error_from_errno("dup");
1188 if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
1189 sizeof(ipackidx)) == -1) {
1190 err = got_error_from_errno("imsg_compose PACKIDX");
1195 if (strlcpy(ipack.path_packfile, pack->path_packfile,
1196 sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
1197 return got_error(GOT_ERR_NO_SPACE);
1198 ipack.filesize = pack->filesize;
1202 return got_error_from_errno("dup");
1204 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
1206 err = got_error_from_errno("imsg_compose PACK");
1211 return flush_imsg(ibuf);
1214 const struct got_error *
1215 got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
1216 struct got_object_id *id)
1218 struct got_imsg_packed_object iobj;
1221 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
1223 if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
1224 &iobj, sizeof(iobj)) == -1)
1225 return got_error_from_errno("imsg_compose "
1226 "PACKED_OBJECT_REQUEST");
1228 return flush_imsg(ibuf);
1231 const struct got_error *
1232 got_privsep_send_gitconfig_parse_req(struct imsgbuf *ibuf, int fd)
1234 const struct got_error *err = NULL;
1236 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_PARSE_REQUEST, 0, 0, fd,
1238 err = got_error_from_errno("imsg_compose "
1239 "GITCONFIG_PARSE_REQUEST");
1244 return flush_imsg(ibuf);
1247 const struct got_error *
1248 got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *ibuf)
1250 if (imsg_compose(ibuf,
1251 GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST, 0, 0, -1,
1253 return got_error_from_errno("imsg_compose "
1254 "GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST");
1256 return flush_imsg(ibuf);
1259 const struct got_error *
1260 got_privsep_send_gitconfig_author_name_req(struct imsgbuf *ibuf)
1262 if (imsg_compose(ibuf,
1263 GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST, 0, 0, -1, NULL, 0) == -1)
1264 return got_error_from_errno("imsg_compose "
1265 "GITCONFIG_AUTHOR_NAME_REQUEST");
1267 return flush_imsg(ibuf);
1270 const struct got_error *
1271 got_privsep_send_gitconfig_author_email_req(struct imsgbuf *ibuf)
1273 if (imsg_compose(ibuf,
1274 GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST, 0, 0, -1, NULL, 0) == -1)
1275 return got_error_from_errno("imsg_compose "
1276 "GITCONFIG_AUTHOR_EMAIL_REQUEST");
1278 return flush_imsg(ibuf);
1281 const struct got_error *
1282 got_privsep_send_gitconfig_str(struct imsgbuf *ibuf, const char *value)
1284 size_t len = value ? strlen(value) + 1 : 0;
1286 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_STR_VAL, 0, 0, -1,
1288 return got_error_from_errno("imsg_compose GITCONFIG_STR_VAL");
1290 return flush_imsg(ibuf);
1293 const struct got_error *
1294 got_privsep_recv_gitconfig_str(char **str, struct imsgbuf *ibuf)
1296 const struct got_error *err = NULL;
1299 const size_t min_datalen = 0;
1303 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1306 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1308 switch (imsg.hdr.type) {
1309 case GOT_IMSG_GITCONFIG_STR_VAL:
1312 *str = malloc(datalen);
1314 err = got_error_from_errno("malloc");
1317 if (strlcpy(*str, imsg.data, datalen) >= datalen)
1318 err = got_error(GOT_ERR_NO_SPACE);
1321 err = got_error(GOT_ERR_PRIVSEP_MSG);
1329 const struct got_error *
1330 got_privsep_send_gitconfig_int(struct imsgbuf *ibuf, int value)
1332 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_INT_VAL, 0, 0, -1,
1333 &value, sizeof(value)) == -1)
1334 return got_error_from_errno("imsg_compose GITCONFIG_INT_VAL");
1336 return flush_imsg(ibuf);
1339 const struct got_error *
1340 got_privsep_recv_gitconfig_int(int *val, struct imsgbuf *ibuf)
1342 const struct got_error *err = NULL;
1345 const size_t min_datalen =
1346 MIN(sizeof(struct got_imsg_error), sizeof(int));
1350 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1353 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1355 switch (imsg.hdr.type) {
1356 case GOT_IMSG_GITCONFIG_INT_VAL:
1357 if (datalen != sizeof(*val)) {
1358 err = got_error(GOT_ERR_PRIVSEP_LEN);
1361 memcpy(val, imsg.data, sizeof(*val));
1364 err = got_error(GOT_ERR_PRIVSEP_MSG);
1372 const struct got_error *
1373 got_privsep_unveil_exec_helpers(void)
1375 const char *helpers[] = {
1376 GOT_PATH_PROG_READ_PACK,
1377 GOT_PATH_PROG_READ_OBJECT,
1378 GOT_PATH_PROG_READ_COMMIT,
1379 GOT_PATH_PROG_READ_TREE,
1380 GOT_PATH_PROG_READ_BLOB,
1381 GOT_PATH_PROG_READ_TAG,
1382 GOT_PATH_PROG_READ_GITCONFIG,
1386 for (i = 0; i < nitems(helpers); i++) {
1387 if (unveil(helpers[i], "x") == 0)
1389 return got_error_from_errno2("unveil", helpers[i]);
1396 got_privsep_exec_child(int imsg_fds[2], const char *path, const char *repo_path)
1398 if (close(imsg_fds[0]) != 0) {
1399 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
1403 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
1404 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
1407 if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
1408 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
1412 if (execl(path, path, repo_path, (char *)NULL) == -1) {
1413 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,