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"
37 #include "got_lib_sha1.h"
38 #include "got_lib_delta.h"
39 #include "got_lib_inflate.h"
40 #include "got_lib_object.h"
41 #include "got_lib_object_parse.h"
42 #include "got_lib_privsep.h"
43 #include "got_lib_pack.h"
46 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
49 static const struct got_error *
50 poll_fd(int fd, int events, int timeout)
56 pfd[0].events = events;
58 n = poll(pfd, 1, timeout);
60 return got_error_from_errno("poll");
62 return got_error(GOT_ERR_TIMEOUT);
63 if (pfd[0].revents & (POLLERR | POLLNVAL))
64 return got_error_from_errno("poll error");
65 if (pfd[0].revents & (events | POLLHUP))
68 return got_error(GOT_ERR_INTERRUPT);
71 static const struct got_error *
72 read_imsg(struct imsgbuf *ibuf)
74 const struct got_error *err;
77 err = poll_fd(ibuf->fd, POLLIN, INFTIM);
83 if (errno == EAGAIN) /* Could be a file-descriptor leak. */
84 return got_error(GOT_ERR_PRIVSEP_NO_FD);
85 return got_error(GOT_ERR_PRIVSEP_READ);
88 return got_error(GOT_ERR_PRIVSEP_PIPE);
93 const struct got_error *
94 got_privsep_wait_for_child(pid_t pid)
98 if (waitpid(pid, &child_status, 0) == -1)
99 return got_error_from_errno("waitpid");
101 if (!WIFEXITED(child_status))
102 return got_error(GOT_ERR_PRIVSEP_DIED);
104 if (WEXITSTATUS(child_status) != 0)
105 return got_error(GOT_ERR_PRIVSEP_EXIT);
110 static const struct got_error *
111 recv_imsg_error(struct imsg *imsg, size_t datalen)
113 struct got_imsg_error *ierr;
115 if (datalen != sizeof(*ierr))
116 return got_error(GOT_ERR_PRIVSEP_LEN);
119 if (ierr->code == GOT_ERR_ERRNO) {
120 static struct got_error serr;
121 serr.code = GOT_ERR_ERRNO;
122 serr.msg = strerror(ierr->errno_code);
126 return got_error(ierr->code);
129 const struct got_error *
130 got_privsep_recv_imsg(struct imsg *imsg, struct imsgbuf *ibuf,
133 const struct got_error *err;
136 n = imsg_get(ibuf, imsg);
138 return got_error_from_errno("imsg_get");
141 err = read_imsg(ibuf);
144 n = imsg_get(ibuf, imsg);
147 if (imsg->hdr.len < IMSG_HEADER_SIZE + min_datalen)
148 return got_error(GOT_ERR_PRIVSEP_LEN);
150 if (imsg->hdr.type == GOT_IMSG_ERROR) {
151 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
152 return recv_imsg_error(imsg, datalen);
158 /* Attempt to send an error in an imsg. Complain on stderr as a last resort. */
160 got_privsep_send_error(struct imsgbuf *ibuf, const struct got_error *err)
162 const struct got_error *poll_err;
163 struct got_imsg_error ierr;
166 ierr.code = err->code;
167 if (err->code == GOT_ERR_ERRNO)
168 ierr.errno_code = errno;
171 ret = imsg_compose(ibuf, GOT_IMSG_ERROR, 0, 0, -1, &ierr, sizeof(ierr));
173 fprintf(stderr, "%s: error %d \"%s\": imsg_compose: %s\n",
174 getprogname(), err->code, err->msg, strerror(errno));
178 poll_err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
180 fprintf(stderr, "%s: error %d \"%s\": poll: %s\n",
181 getprogname(), err->code, err->msg, poll_err->msg);
185 ret = imsg_flush(ibuf);
187 fprintf(stderr, "%s: error %d \"%s\": imsg_flush: %s\n",
188 getprogname(), err->code, err->msg, strerror(errno));
193 static const struct got_error *
194 flush_imsg(struct imsgbuf *ibuf)
196 const struct got_error *err;
198 err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
202 if (imsg_flush(ibuf) == -1)
203 return got_error_from_errno("imsg_flush");
208 const struct got_error *
209 got_privsep_send_stop(int fd)
211 const struct got_error *err = NULL;
214 imsg_init(&ibuf, fd);
216 if (imsg_compose(&ibuf, GOT_IMSG_STOP, 0, 0, -1, NULL, 0) == -1)
217 return got_error_from_errno("imsg_compose STOP");
219 err = flush_imsg(&ibuf);
224 const struct got_error *
225 got_privsep_send_obj_req(struct imsgbuf *ibuf, int fd)
227 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_REQUEST, 0, 0, fd, NULL, 0)
229 return got_error_from_errno("imsg_compose OBJECT_REQUEST");
231 return flush_imsg(ibuf);
234 const struct got_error *
235 got_privsep_send_commit_req(struct imsgbuf *ibuf, int fd,
236 struct got_object_id *id, int pack_idx)
238 const struct got_error *err = NULL;
239 struct got_imsg_packed_object iobj, *iobjp;
242 if (id) { /* commit is packed */
244 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
252 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_REQUEST, 0, 0, fd, iobjp, len)
254 err = got_error_from_errno("imsg_compose COMMIT_REQUEST");
259 return flush_imsg(ibuf);
262 const struct got_error *
263 got_privsep_send_tree_req(struct imsgbuf *ibuf, int fd,
264 struct got_object_id *id, int pack_idx)
266 const struct got_error *err = NULL;
267 struct got_imsg_packed_object iobj, *iobjp;
270 if (id) { /* tree is packed */
272 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
280 if (imsg_compose(ibuf, GOT_IMSG_TREE_REQUEST, 0, 0, fd, iobjp, len)
282 err = got_error_from_errno("imsg_compose TREE_REQUEST");
287 return flush_imsg(ibuf);
290 const struct got_error *
291 got_privsep_send_tag_req(struct imsgbuf *ibuf, int fd,
292 struct got_object_id *id, int pack_idx)
294 struct got_imsg_packed_object iobj, *iobjp;
297 if (id) { /* tag is packed */
299 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
307 if (imsg_compose(ibuf, GOT_IMSG_TAG_REQUEST, 0, 0, fd, iobjp, len)
309 return got_error_from_errno("imsg_compose TAG_REQUEST");
311 return flush_imsg(ibuf);
314 const struct got_error *
315 got_privsep_send_blob_req(struct imsgbuf *ibuf, int infd,
316 struct got_object_id *id, int pack_idx)
318 const struct got_error *err = NULL;
319 struct got_imsg_packed_object iobj, *iobjp;
322 if (id) { /* blob is packed */
324 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
332 if (imsg_compose(ibuf, GOT_IMSG_BLOB_REQUEST, 0, 0, infd, iobjp, len)
334 err = got_error_from_errno("imsg_compose BLOB_REQUEST");
339 return flush_imsg(ibuf);
342 const struct got_error *
343 got_privsep_send_blob_outfd(struct imsgbuf *ibuf, int outfd)
345 const struct got_error *err = NULL;
347 if (imsg_compose(ibuf, GOT_IMSG_BLOB_OUTFD, 0, 0, outfd, NULL, 0)
349 err = got_error_from_errno("imsg_compose BLOB_OUTFD");
354 return flush_imsg(ibuf);
357 const struct got_error *
358 got_privsep_send_tmpfd(struct imsgbuf *ibuf, int fd)
360 const struct got_error *err = NULL;
362 if (imsg_compose(ibuf, GOT_IMSG_TMPFD, 0, 0, fd, NULL, 0)
364 err = got_error_from_errno("imsg_compose TMPFD");
369 return flush_imsg(ibuf);
372 const struct got_error *
373 got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj)
375 struct got_imsg_object iobj;
377 memcpy(iobj.id, obj->id.sha1, sizeof(iobj.id));
378 iobj.type = obj->type;
379 iobj.flags = obj->flags;
380 iobj.hdrlen = obj->hdrlen;
381 iobj.size = obj->size;
382 if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
383 iobj.pack_offset = obj->pack_offset;
384 iobj.pack_idx = obj->pack_idx;
387 if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
389 return got_error_from_errno("imsg_compose OBJECT");
391 return flush_imsg(ibuf);
394 const struct got_error *
395 got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
396 struct imsgbuf *ibuf)
398 const struct got_error *err = NULL;
399 struct got_imsg_object *iobj;
400 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
402 if (datalen != sizeof(*iobj))
403 return got_error(GOT_ERR_PRIVSEP_LEN);
406 *obj = calloc(1, sizeof(**obj));
408 return got_error_from_errno("calloc");
410 memcpy((*obj)->id.sha1, iobj->id, SHA1_DIGEST_LENGTH);
411 (*obj)->type = iobj->type;
412 (*obj)->flags = iobj->flags;
413 (*obj)->hdrlen = iobj->hdrlen;
414 (*obj)->size = iobj->size;
415 /* path_packfile is handled by caller */
416 if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
417 (*obj)->pack_offset = iobj->pack_offset;
418 (*obj)->pack_idx = iobj->pack_idx;
424 const struct got_error *
425 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
427 const struct got_error *err = NULL;
430 const size_t min_datalen =
431 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
435 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
439 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
441 switch (imsg.hdr.type) {
442 case GOT_IMSG_OBJECT:
443 err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
446 err = got_error(GOT_ERR_PRIVSEP_MSG);
455 static const struct got_error *
456 send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
459 const struct got_error *err = NULL;
460 size_t offset, remain;
465 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
467 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
468 commit->logmsg + offset, n) == -1) {
469 err = got_error_from_errno("imsg_compose "
474 err = flush_imsg(ibuf);
485 const struct got_error *
486 got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
488 const struct got_error *err = NULL;
489 struct got_imsg_commit_object *icommit;
492 struct got_object_qid *qid;
493 size_t author_len = strlen(commit->author);
494 size_t committer_len = strlen(commit->committer);
495 size_t logmsg_len = strlen(commit->logmsg);
497 total = sizeof(*icommit) + author_len + committer_len +
498 commit->nparents * SHA1_DIGEST_LENGTH;
502 return got_error_from_errno("malloc");
504 icommit = (struct got_imsg_commit_object *)buf;
505 memcpy(icommit->tree_id, commit->tree_id->sha1,
506 sizeof(icommit->tree_id));
507 icommit->author_len = author_len;
508 icommit->author_time = commit->author_time;
509 icommit->author_gmtoff = commit->author_gmtoff;
510 icommit->committer_len = committer_len;
511 icommit->committer_time = commit->committer_time;
512 icommit->committer_gmtoff = commit->committer_gmtoff;
513 icommit->logmsg_len = logmsg_len;
514 icommit->nparents = commit->nparents;
516 len = sizeof(*icommit);
517 memcpy(buf + len, commit->author, author_len);
519 memcpy(buf + len, commit->committer, committer_len);
520 len += committer_len;
521 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
522 memcpy(buf + len, qid->id, SHA1_DIGEST_LENGTH);
523 len += SHA1_DIGEST_LENGTH;
526 if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
527 err = got_error_from_errno("imsg_compose COMMIT");
531 if (logmsg_len == 0 ||
532 logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
533 err = flush_imsg(ibuf);
537 err = send_commit_logmsg(ibuf, commit, logmsg_len);
543 const struct got_error *
544 got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
546 const struct got_error *err = NULL;
548 struct got_imsg_commit_object *icommit;
551 const size_t min_datalen =
552 MIN(sizeof(struct got_imsg_error),
553 sizeof(struct got_imsg_commit_object));
557 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
561 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
564 switch (imsg.hdr.type) {
565 case GOT_IMSG_COMMIT:
566 if (datalen < sizeof(*icommit)) {
567 err = got_error(GOT_ERR_PRIVSEP_LEN);
571 if (datalen != sizeof(*icommit) + icommit->author_len +
572 icommit->committer_len +
573 icommit->nparents * SHA1_DIGEST_LENGTH) {
574 err = got_error(GOT_ERR_PRIVSEP_LEN);
577 if (icommit->nparents < 0) {
578 err = got_error(GOT_ERR_PRIVSEP_LEN);
581 len += sizeof(*icommit);
583 *commit = got_object_commit_alloc_partial();
584 if (*commit == NULL) {
585 err = got_error_from_errno(
586 "got_object_commit_alloc_partial");
590 memcpy((*commit)->tree_id->sha1, icommit->tree_id,
592 (*commit)->author_time = icommit->author_time;
593 (*commit)->author_gmtoff = icommit->author_gmtoff;
594 (*commit)->committer_time = icommit->committer_time;
595 (*commit)->committer_gmtoff = icommit->committer_gmtoff;
597 if (icommit->author_len == 0) {
598 (*commit)->author = strdup("");
599 if ((*commit)->author == NULL) {
600 err = got_error_from_errno("strdup");
604 (*commit)->author = malloc(icommit->author_len + 1);
605 if ((*commit)->author == NULL) {
606 err = got_error_from_errno("malloc");
609 memcpy((*commit)->author, imsg.data + len,
610 icommit->author_len);
611 (*commit)->author[icommit->author_len] = '\0';
613 len += icommit->author_len;
615 if (icommit->committer_len == 0) {
616 (*commit)->committer = strdup("");
617 if ((*commit)->committer == NULL) {
618 err = got_error_from_errno("strdup");
622 (*commit)->committer =
623 malloc(icommit->committer_len + 1);
624 if ((*commit)->committer == NULL) {
625 err = got_error_from_errno("malloc");
628 memcpy((*commit)->committer, imsg.data + len,
629 icommit->committer_len);
630 (*commit)->committer[icommit->committer_len] = '\0';
632 len += icommit->committer_len;
634 if (icommit->logmsg_len == 0) {
635 (*commit)->logmsg = strdup("");
636 if ((*commit)->logmsg == NULL) {
637 err = got_error_from_errno("strdup");
641 size_t offset = 0, remain = icommit->logmsg_len;
643 (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
644 if ((*commit)->logmsg == NULL) {
645 err = got_error_from_errno("malloc");
649 struct imsg imsg_log;
650 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
653 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
657 if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG)
658 return got_error(GOT_ERR_PRIVSEP_MSG);
660 memcpy((*commit)->logmsg + offset,
662 imsg_free(&imsg_log);
666 (*commit)->logmsg[icommit->logmsg_len] = '\0';
669 for (i = 0; i < icommit->nparents; i++) {
670 struct got_object_qid *qid;
672 err = got_object_qid_alloc_partial(&qid);
675 memcpy(qid->id, imsg.data + len +
676 i * SHA1_DIGEST_LENGTH, sizeof(*qid->id));
677 SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
678 (*commit)->nparents++;
682 err = got_error(GOT_ERR_PRIVSEP_MSG);
691 const struct got_error *
692 got_privsep_send_tree(struct imsgbuf *ibuf, struct got_tree_object *tree)
694 const struct got_error *err = NULL;
695 struct got_imsg_tree_object itree;
696 struct got_tree_entry *te;
698 int nimsg; /* number of imsg queued in ibuf */
700 itree.nentries = tree->entries.nentries;
701 if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
703 return got_error_from_errno("imsg_compose TREE");
705 totlen = sizeof(itree);
707 SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
708 struct got_imsg_tree_entry *ite;
710 size_t len = sizeof(*ite) + strlen(te->name);
712 if (len > MAX_IMSGSIZE)
713 return got_error(GOT_ERR_NO_SPACE);
716 if (totlen + len >= MAX_IMSGSIZE - (IMSG_HEADER_SIZE * nimsg)) {
717 err = flush_imsg(ibuf);
725 return got_error_from_errno("malloc");
727 ite = (struct got_imsg_tree_entry *)buf;
728 memcpy(ite->id, te->id->sha1, sizeof(ite->id));
729 ite->mode = te->mode;
730 memcpy(buf + sizeof(*ite), te->name, strlen(te->name));
732 if (imsg_compose(ibuf, GOT_IMSG_TREE_ENTRY, 0, 0, -1,
734 err = got_error_from_errno("imsg_compose TREE_ENTRY");
741 return flush_imsg(ibuf);
744 const struct got_error *
745 got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
747 const struct got_error *err = NULL;
748 const size_t min_datalen =
749 MIN(sizeof(struct got_imsg_error),
750 sizeof(struct got_imsg_tree_object));
751 struct got_imsg_tree_object *itree;
756 err = read_imsg(ibuf);
764 struct got_imsg_tree_entry *ite;
765 struct got_tree_entry *te = NULL;
767 n = imsg_get(ibuf, &imsg);
769 if (*tree && (*tree)->entries.nentries != nentries)
774 if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen)
775 return got_error(GOT_ERR_PRIVSEP_LEN);
777 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
779 switch (imsg.hdr.type) {
781 err = recv_imsg_error(&imsg, datalen);
784 /* This message should only appear once. */
786 err = got_error(GOT_ERR_PRIVSEP_MSG);
789 if (datalen != sizeof(*itree)) {
790 err = got_error(GOT_ERR_PRIVSEP_LEN);
794 *tree = malloc(sizeof(**tree));
796 err = got_error_from_errno("malloc");
799 (*tree)->entries.nentries = itree->nentries;
800 SIMPLEQ_INIT(&(*tree)->entries.head);
803 case GOT_IMSG_TREE_ENTRY:
804 /* This message should be preceeded by GOT_IMSG_TREE. */
806 err = got_error(GOT_ERR_PRIVSEP_MSG);
809 if (datalen < sizeof(*ite) || datalen > MAX_IMSGSIZE) {
810 err = got_error(GOT_ERR_PRIVSEP_LEN);
814 /* Remaining data contains the entry's name. */
815 datalen -= sizeof(*ite);
816 if (datalen == 0 || datalen > MAX_IMSGSIZE) {
817 err = got_error(GOT_ERR_PRIVSEP_LEN);
822 te = got_alloc_tree_entry_partial();
824 err = got_error_from_errno(
825 "got_alloc_tree_entry_partial");
828 te->name = malloc(datalen + 1);
829 if (te->name == NULL) {
831 err = got_error_from_errno("malloc");
834 memcpy(te->name, imsg.data + sizeof(*ite), datalen);
835 te->name[datalen] = '\0';
837 memcpy(te->id->sha1, ite->id, SHA1_DIGEST_LENGTH);
838 te->mode = ite->mode;
839 SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
843 err = got_error(GOT_ERR_PRIVSEP_MSG);
850 if (*tree && (*tree)->entries.nentries != nentries) {
852 err = got_error(GOT_ERR_PRIVSEP_LEN);
853 got_object_tree_close(*tree);
860 const struct got_error *
861 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
864 struct got_imsg_blob iblob;
867 iblob.hdrlen = hdrlen;
872 if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
873 return got_error(GOT_ERR_NO_SPACE);
875 buf = malloc(sizeof(iblob) + size);
877 return got_error_from_errno("malloc");
879 memcpy(buf, &iblob, sizeof(iblob));
880 memcpy(buf + sizeof(iblob), data, size);
881 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
882 sizeof(iblob) + size) == -1) {
884 return got_error_from_errno("imsg_compose BLOB");
888 /* Data has already been written to file descriptor. */
889 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
890 sizeof(iblob)) == -1)
891 return got_error_from_errno("imsg_compose BLOB");
895 return flush_imsg(ibuf);
898 const struct got_error *
899 got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
900 struct imsgbuf *ibuf)
902 const struct got_error *err = NULL;
904 struct got_imsg_blob *iblob;
909 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
913 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
915 switch (imsg.hdr.type) {
917 if (datalen < sizeof(*iblob)) {
918 err = got_error(GOT_ERR_PRIVSEP_LEN);
923 *hdrlen = iblob->hdrlen;
925 if (datalen == sizeof(*iblob)) {
926 /* Data has been written to file descriptor. */
930 if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX) {
931 err = got_error(GOT_ERR_PRIVSEP_LEN);
935 *outbuf = malloc(*size);
936 if (*outbuf == NULL) {
937 err = got_error_from_errno("malloc");
940 memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
943 err = got_error(GOT_ERR_PRIVSEP_MSG);
952 static const struct got_error *
953 send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
955 const struct got_error *err = NULL;
956 size_t offset, remain;
961 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
963 if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
964 tag->tagmsg + offset, n) == -1) {
965 err = got_error_from_errno("imsg_compose TAG_TAGMSG");
969 err = flush_imsg(ibuf);
980 const struct got_error *
981 got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
983 const struct got_error *err = NULL;
984 struct got_imsg_tag_object *itag;
987 size_t tag_len = strlen(tag->tag);
988 size_t tagger_len = strlen(tag->tagger);
989 size_t tagmsg_len = strlen(tag->tagmsg);
991 total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
995 return got_error_from_errno("malloc");
997 itag = (struct got_imsg_tag_object *)buf;
998 memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
999 itag->obj_type = tag->obj_type;
1000 itag->tag_len = tag_len;
1001 itag->tagger_len = tagger_len;
1002 itag->tagger_time = tag->tagger_time;
1003 itag->tagger_gmtoff = tag->tagger_gmtoff;
1004 itag->tagmsg_len = tagmsg_len;
1006 len = sizeof(*itag);
1007 memcpy(buf + len, tag->tag, tag_len);
1009 memcpy(buf + len, tag->tagger, tagger_len);
1012 if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
1013 err = got_error_from_errno("imsg_compose TAG");
1017 if (tagmsg_len == 0 ||
1018 tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1019 err = flush_imsg(ibuf);
1023 err = send_tagmsg(ibuf, tag, tagmsg_len);
1029 const struct got_error *
1030 got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
1032 const struct got_error *err = NULL;
1034 struct got_imsg_tag_object *itag;
1035 size_t len, datalen;
1036 const size_t min_datalen =
1037 MIN(sizeof(struct got_imsg_error),
1038 sizeof(struct got_imsg_tag_object));
1042 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1046 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1049 switch (imsg.hdr.type) {
1051 if (datalen < sizeof(*itag)) {
1052 err = got_error(GOT_ERR_PRIVSEP_LEN);
1056 if (datalen != sizeof(*itag) + itag->tag_len +
1058 err = got_error(GOT_ERR_PRIVSEP_LEN);
1061 len += sizeof(*itag);
1063 *tag = calloc(1, sizeof(**tag));
1065 err = got_error_from_errno("calloc");
1069 memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
1071 if (itag->tag_len == 0) {
1072 (*tag)->tag = strdup("");
1073 if ((*tag)->tag == NULL) {
1074 err = got_error_from_errno("strdup");
1078 (*tag)->tag = malloc(itag->tag_len + 1);
1079 if ((*tag)->tag == NULL) {
1080 err = got_error_from_errno("malloc");
1083 memcpy((*tag)->tag, imsg.data + len,
1085 (*tag)->tag[itag->tag_len] = '\0';
1087 len += itag->tag_len;
1089 (*tag)->obj_type = itag->obj_type;
1090 (*tag)->tagger_time = itag->tagger_time;
1091 (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1093 if (itag->tagger_len == 0) {
1094 (*tag)->tagger = strdup("");
1095 if ((*tag)->tagger == NULL) {
1096 err = got_error_from_errno("strdup");
1100 (*tag)->tagger = malloc(itag->tagger_len + 1);
1101 if ((*tag)->tagger == NULL) {
1102 err = got_error_from_errno("malloc");
1105 memcpy((*tag)->tagger, imsg.data + len,
1107 (*tag)->tagger[itag->tagger_len] = '\0';
1109 len += itag->tagger_len;
1111 if (itag->tagmsg_len == 0) {
1112 (*tag)->tagmsg = strdup("");
1113 if ((*tag)->tagmsg == NULL) {
1114 err = got_error_from_errno("strdup");
1118 size_t offset = 0, remain = itag->tagmsg_len;
1120 (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1121 if ((*tag)->tagmsg == NULL) {
1122 err = got_error_from_errno("malloc");
1125 while (remain > 0) {
1126 struct imsg imsg_log;
1127 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1130 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1134 if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1135 return got_error(GOT_ERR_PRIVSEP_MSG);
1137 memcpy((*tag)->tagmsg + offset, imsg_log.data,
1139 imsg_free(&imsg_log);
1143 (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1148 err = got_error(GOT_ERR_PRIVSEP_MSG);
1157 const struct got_error *
1158 got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1159 struct got_packidx *packidx)
1161 const struct got_error *err = NULL;
1162 struct got_imsg_packidx ipackidx;
1163 struct got_imsg_pack ipack;
1166 ipackidx.len = packidx->len;
1167 fd = dup(packidx->fd);
1169 return got_error_from_errno("dup");
1171 if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
1172 sizeof(ipackidx)) == -1) {
1173 err = got_error_from_errno("imsg_compose PACKIDX");
1178 if (strlcpy(ipack.path_packfile, pack->path_packfile,
1179 sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
1180 return got_error(GOT_ERR_NO_SPACE);
1181 ipack.filesize = pack->filesize;
1185 return got_error_from_errno("dup");
1187 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
1189 err = got_error_from_errno("imsg_compose PACK");
1194 return flush_imsg(ibuf);
1197 const struct got_error *
1198 got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
1199 struct got_object_id *id)
1201 struct got_imsg_packed_object iobj;
1204 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
1206 if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
1207 &iobj, sizeof(iobj)) == -1)
1208 return got_error_from_errno("imsg_compose "
1209 "PACKED_OBJECT_REQUEST");
1211 return flush_imsg(ibuf);
1214 const struct got_error *
1215 got_privsep_unveil_exec_helpers(void)
1217 if (unveil(GOT_PATH_PROG_READ_PACK, "x") != 0 ||
1218 unveil(GOT_PATH_PROG_READ_OBJECT, "x") != 0 ||
1219 unveil(GOT_PATH_PROG_READ_COMMIT, "x") != 0 ||
1220 unveil(GOT_PATH_PROG_READ_TREE, "x") != 0 ||
1221 unveil(GOT_PATH_PROG_READ_BLOB, "x") != 0 ||
1222 unveil(GOT_PATH_PROG_READ_TAG, "x") != 0)
1223 return got_error_from_errno("unveil");