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))
50 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
53 static const struct got_error *
54 poll_fd(int fd, int events, int timeout)
60 pfd[0].events = events;
62 n = poll(pfd, 1, timeout);
64 return got_error_from_errno("poll");
66 return got_error(GOT_ERR_TIMEOUT);
67 if (pfd[0].revents & (POLLERR | POLLNVAL))
68 return got_error_from_errno("poll error");
69 if (pfd[0].revents & (events | POLLHUP))
72 return got_error(GOT_ERR_INTERRUPT);
75 static const struct got_error *
76 read_imsg(struct imsgbuf *ibuf)
78 const struct got_error *err;
81 err = poll_fd(ibuf->fd, POLLIN, INFTIM);
87 if (errno == EAGAIN) /* Could be a file-descriptor leak. */
88 return got_error(GOT_ERR_PRIVSEP_NO_FD);
89 return got_error(GOT_ERR_PRIVSEP_READ);
92 return got_error(GOT_ERR_PRIVSEP_PIPE);
97 const struct got_error *
98 got_privsep_wait_for_child(pid_t pid)
102 if (waitpid(pid, &child_status, 0) == -1)
103 return got_error_from_errno("waitpid");
105 if (!WIFEXITED(child_status))
106 return got_error(GOT_ERR_PRIVSEP_DIED);
108 if (WEXITSTATUS(child_status) != 0)
109 return got_error(GOT_ERR_PRIVSEP_EXIT);
114 static const struct got_error *
115 recv_imsg_error(struct imsg *imsg, size_t datalen)
117 struct got_imsg_error *ierr;
119 if (datalen != sizeof(*ierr))
120 return got_error(GOT_ERR_PRIVSEP_LEN);
123 if (ierr->code == GOT_ERR_ERRNO) {
124 static struct got_error serr;
125 serr.code = GOT_ERR_ERRNO;
126 serr.msg = strerror(ierr->errno_code);
130 return got_error(ierr->code);
133 const struct got_error *
134 got_privsep_recv_imsg(struct imsg *imsg, struct imsgbuf *ibuf,
137 const struct got_error *err;
140 n = imsg_get(ibuf, imsg);
142 return got_error_from_errno("imsg_get");
145 err = read_imsg(ibuf);
148 n = imsg_get(ibuf, imsg);
151 if (imsg->hdr.len < IMSG_HEADER_SIZE + min_datalen)
152 return got_error(GOT_ERR_PRIVSEP_LEN);
154 if (imsg->hdr.type == GOT_IMSG_ERROR) {
155 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
156 return recv_imsg_error(imsg, datalen);
162 /* Attempt to send an error in an imsg. Complain on stderr as a last resort. */
164 got_privsep_send_error(struct imsgbuf *ibuf, const struct got_error *err)
166 const struct got_error *poll_err;
167 struct got_imsg_error ierr;
170 ierr.code = err->code;
171 if (err->code == GOT_ERR_ERRNO)
172 ierr.errno_code = errno;
175 ret = imsg_compose(ibuf, GOT_IMSG_ERROR, 0, 0, -1, &ierr, sizeof(ierr));
177 fprintf(stderr, "%s: error %d \"%s\": imsg_compose: %s\n",
178 getprogname(), err->code, err->msg, strerror(errno));
182 poll_err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
184 fprintf(stderr, "%s: error %d \"%s\": poll: %s\n",
185 getprogname(), err->code, err->msg, poll_err->msg);
189 ret = imsg_flush(ibuf);
191 fprintf(stderr, "%s: error %d \"%s\": imsg_flush: %s\n",
192 getprogname(), err->code, err->msg, strerror(errno));
197 static const struct got_error *
198 flush_imsg(struct imsgbuf *ibuf)
200 const struct got_error *err;
202 err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
206 if (imsg_flush(ibuf) == -1)
207 return got_error_from_errno("imsg_flush");
212 const struct got_error *
213 got_privsep_send_stop(int fd)
215 const struct got_error *err = NULL;
218 imsg_init(&ibuf, fd);
220 if (imsg_compose(&ibuf, GOT_IMSG_STOP, 0, 0, -1, NULL, 0) == -1)
221 return got_error_from_errno("imsg_compose STOP");
223 err = flush_imsg(&ibuf);
228 const struct got_error *
229 got_privsep_send_obj_req(struct imsgbuf *ibuf, int fd)
231 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_REQUEST, 0, 0, fd, NULL, 0)
233 return got_error_from_errno("imsg_compose OBJECT_REQUEST");
235 return flush_imsg(ibuf);
238 const struct got_error *
239 got_privsep_send_commit_req(struct imsgbuf *ibuf, int fd,
240 struct got_object_id *id, int pack_idx)
242 const struct got_error *err = NULL;
243 struct got_imsg_packed_object iobj, *iobjp;
246 if (id) { /* commit is packed */
248 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
256 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_REQUEST, 0, 0, fd, iobjp, len)
258 err = got_error_from_errno("imsg_compose COMMIT_REQUEST");
263 return flush_imsg(ibuf);
266 const struct got_error *
267 got_privsep_send_tree_req(struct imsgbuf *ibuf, int fd,
268 struct got_object_id *id, int pack_idx)
270 const struct got_error *err = NULL;
271 struct got_imsg_packed_object iobj, *iobjp;
274 if (id) { /* tree is packed */
276 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
284 if (imsg_compose(ibuf, GOT_IMSG_TREE_REQUEST, 0, 0, fd, iobjp, len)
286 err = got_error_from_errno("imsg_compose TREE_REQUEST");
291 return flush_imsg(ibuf);
294 const struct got_error *
295 got_privsep_send_tag_req(struct imsgbuf *ibuf, int fd,
296 struct got_object_id *id, int pack_idx)
298 struct got_imsg_packed_object iobj, *iobjp;
301 if (id) { /* tag is packed */
303 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
311 if (imsg_compose(ibuf, GOT_IMSG_TAG_REQUEST, 0, 0, fd, iobjp, len)
313 return got_error_from_errno("imsg_compose TAG_REQUEST");
315 return flush_imsg(ibuf);
318 const struct got_error *
319 got_privsep_send_blob_req(struct imsgbuf *ibuf, int infd,
320 struct got_object_id *id, int pack_idx)
322 const struct got_error *err = NULL;
323 struct got_imsg_packed_object iobj, *iobjp;
326 if (id) { /* blob is packed */
328 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
336 if (imsg_compose(ibuf, GOT_IMSG_BLOB_REQUEST, 0, 0, infd, iobjp, len)
338 err = got_error_from_errno("imsg_compose BLOB_REQUEST");
343 return flush_imsg(ibuf);
346 const struct got_error *
347 got_privsep_send_blob_outfd(struct imsgbuf *ibuf, int outfd)
349 const struct got_error *err = NULL;
351 if (imsg_compose(ibuf, GOT_IMSG_BLOB_OUTFD, 0, 0, outfd, NULL, 0)
353 err = got_error_from_errno("imsg_compose BLOB_OUTFD");
358 return flush_imsg(ibuf);
361 const struct got_error *
362 got_privsep_send_tmpfd(struct imsgbuf *ibuf, int fd)
364 const struct got_error *err = NULL;
366 if (imsg_compose(ibuf, GOT_IMSG_TMPFD, 0, 0, fd, NULL, 0)
368 err = got_error_from_errno("imsg_compose TMPFD");
373 return flush_imsg(ibuf);
376 const struct got_error *
377 got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj)
379 struct got_imsg_object iobj;
381 memcpy(iobj.id, obj->id.sha1, sizeof(iobj.id));
382 iobj.type = obj->type;
383 iobj.flags = obj->flags;
384 iobj.hdrlen = obj->hdrlen;
385 iobj.size = obj->size;
386 if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
387 iobj.pack_offset = obj->pack_offset;
388 iobj.pack_idx = obj->pack_idx;
391 if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
393 return got_error_from_errno("imsg_compose OBJECT");
395 return flush_imsg(ibuf);
398 const struct got_error *
399 got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
400 struct imsgbuf *ibuf)
402 const struct got_error *err = NULL;
403 struct got_imsg_object *iobj;
404 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
406 if (datalen != sizeof(*iobj))
407 return got_error(GOT_ERR_PRIVSEP_LEN);
410 *obj = calloc(1, sizeof(**obj));
412 return got_error_from_errno("calloc");
414 memcpy((*obj)->id.sha1, iobj->id, SHA1_DIGEST_LENGTH);
415 (*obj)->type = iobj->type;
416 (*obj)->flags = iobj->flags;
417 (*obj)->hdrlen = iobj->hdrlen;
418 (*obj)->size = iobj->size;
419 /* path_packfile is handled by caller */
420 if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
421 (*obj)->pack_offset = iobj->pack_offset;
422 (*obj)->pack_idx = iobj->pack_idx;
428 const struct got_error *
429 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
431 const struct got_error *err = NULL;
433 const size_t min_datalen =
434 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
438 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
442 switch (imsg.hdr.type) {
443 case GOT_IMSG_OBJECT:
444 err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
447 err = got_error(GOT_ERR_PRIVSEP_MSG);
456 static const struct got_error *
457 send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
460 const struct got_error *err = NULL;
461 size_t offset, remain;
466 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
468 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
469 commit->logmsg + offset, n) == -1) {
470 err = got_error_from_errno("imsg_compose "
475 err = flush_imsg(ibuf);
486 const struct got_error *
487 got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
489 const struct got_error *err = NULL;
490 struct got_imsg_commit_object *icommit;
493 struct got_object_qid *qid;
494 size_t author_len = strlen(commit->author);
495 size_t committer_len = strlen(commit->committer);
496 size_t logmsg_len = strlen(commit->logmsg);
498 total = sizeof(*icommit) + author_len + committer_len +
499 commit->nparents * SHA1_DIGEST_LENGTH;
503 return got_error_from_errno("malloc");
505 icommit = (struct got_imsg_commit_object *)buf;
506 memcpy(icommit->tree_id, commit->tree_id->sha1,
507 sizeof(icommit->tree_id));
508 icommit->author_len = author_len;
509 icommit->author_time = commit->author_time;
510 icommit->author_gmtoff = commit->author_gmtoff;
511 icommit->committer_len = committer_len;
512 icommit->committer_time = commit->committer_time;
513 icommit->committer_gmtoff = commit->committer_gmtoff;
514 icommit->logmsg_len = logmsg_len;
515 icommit->nparents = commit->nparents;
517 len = sizeof(*icommit);
518 memcpy(buf + len, commit->author, author_len);
520 memcpy(buf + len, commit->committer, committer_len);
521 len += committer_len;
522 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
523 memcpy(buf + len, qid->id, SHA1_DIGEST_LENGTH);
524 len += SHA1_DIGEST_LENGTH;
527 if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
528 err = got_error_from_errno("imsg_compose COMMIT");
532 if (logmsg_len == 0 ||
533 logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
534 err = flush_imsg(ibuf);
538 err = send_commit_logmsg(ibuf, commit, logmsg_len);
544 const struct got_error *
545 got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
547 const struct got_error *err = NULL;
549 struct got_imsg_commit_object *icommit;
552 const size_t min_datalen =
553 MIN(sizeof(struct got_imsg_error),
554 sizeof(struct got_imsg_commit_object));
558 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
562 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
565 switch (imsg.hdr.type) {
566 case GOT_IMSG_COMMIT:
567 if (datalen < sizeof(*icommit)) {
568 err = got_error(GOT_ERR_PRIVSEP_LEN);
572 if (datalen != sizeof(*icommit) + icommit->author_len +
573 icommit->committer_len +
574 icommit->nparents * SHA1_DIGEST_LENGTH) {
575 err = got_error(GOT_ERR_PRIVSEP_LEN);
578 if (icommit->nparents < 0) {
579 err = got_error(GOT_ERR_PRIVSEP_LEN);
582 len += sizeof(*icommit);
584 *commit = got_object_commit_alloc_partial();
585 if (*commit == NULL) {
586 err = got_error_from_errno(
587 "got_object_commit_alloc_partial");
591 memcpy((*commit)->tree_id->sha1, icommit->tree_id,
593 (*commit)->author_time = icommit->author_time;
594 (*commit)->author_gmtoff = icommit->author_gmtoff;
595 (*commit)->committer_time = icommit->committer_time;
596 (*commit)->committer_gmtoff = icommit->committer_gmtoff;
598 if (icommit->author_len == 0) {
599 (*commit)->author = strdup("");
600 if ((*commit)->author == NULL) {
601 err = got_error_from_errno("strdup");
605 (*commit)->author = malloc(icommit->author_len + 1);
606 if ((*commit)->author == NULL) {
607 err = got_error_from_errno("malloc");
610 memcpy((*commit)->author, imsg.data + len,
611 icommit->author_len);
612 (*commit)->author[icommit->author_len] = '\0';
614 len += icommit->author_len;
616 if (icommit->committer_len == 0) {
617 (*commit)->committer = strdup("");
618 if ((*commit)->committer == NULL) {
619 err = got_error_from_errno("strdup");
623 (*commit)->committer =
624 malloc(icommit->committer_len + 1);
625 if ((*commit)->committer == NULL) {
626 err = got_error_from_errno("malloc");
629 memcpy((*commit)->committer, imsg.data + len,
630 icommit->committer_len);
631 (*commit)->committer[icommit->committer_len] = '\0';
633 len += icommit->committer_len;
635 if (icommit->logmsg_len == 0) {
636 (*commit)->logmsg = strdup("");
637 if ((*commit)->logmsg == NULL) {
638 err = got_error_from_errno("strdup");
642 size_t offset = 0, remain = icommit->logmsg_len;
644 (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
645 if ((*commit)->logmsg == NULL) {
646 err = got_error_from_errno("malloc");
650 struct imsg imsg_log;
651 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
654 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
658 if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG)
659 return got_error(GOT_ERR_PRIVSEP_MSG);
661 memcpy((*commit)->logmsg + offset,
663 imsg_free(&imsg_log);
667 (*commit)->logmsg[icommit->logmsg_len] = '\0';
670 for (i = 0; i < icommit->nparents; i++) {
671 struct got_object_qid *qid;
673 err = got_object_qid_alloc_partial(&qid);
676 memcpy(qid->id, imsg.data + len +
677 i * SHA1_DIGEST_LENGTH, sizeof(*qid->id));
678 SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
679 (*commit)->nparents++;
683 err = got_error(GOT_ERR_PRIVSEP_MSG);
692 const struct got_error *
693 got_privsep_send_tree(struct imsgbuf *ibuf, struct got_tree_object *tree)
695 const struct got_error *err = NULL;
696 struct got_imsg_tree_object itree;
697 struct got_tree_entry *te;
699 int nimsg; /* number of imsg queued in ibuf */
701 itree.nentries = tree->entries.nentries;
702 if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
704 return got_error_from_errno("imsg_compose TREE");
706 totlen = sizeof(itree);
708 SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
709 struct got_imsg_tree_entry *ite;
711 size_t len = sizeof(*ite) + strlen(te->name);
713 if (len > MAX_IMSGSIZE)
714 return got_error(GOT_ERR_NO_SPACE);
717 if (totlen + len >= MAX_IMSGSIZE - (IMSG_HEADER_SIZE * nimsg)) {
718 err = flush_imsg(ibuf);
726 return got_error_from_errno("malloc");
728 ite = (struct got_imsg_tree_entry *)buf;
729 memcpy(ite->id, te->id->sha1, sizeof(ite->id));
730 ite->mode = te->mode;
731 memcpy(buf + sizeof(*ite), te->name, strlen(te->name));
733 if (imsg_compose(ibuf, GOT_IMSG_TREE_ENTRY, 0, 0, -1,
735 err = got_error_from_errno("imsg_compose TREE_ENTRY");
742 return flush_imsg(ibuf);
745 const struct got_error *
746 got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
748 const struct got_error *err = NULL;
749 const size_t min_datalen =
750 MIN(sizeof(struct got_imsg_error),
751 sizeof(struct got_imsg_tree_object));
752 struct got_imsg_tree_object *itree;
757 err = read_imsg(ibuf);
765 struct got_imsg_tree_entry *ite;
766 struct got_tree_entry *te = NULL;
768 n = imsg_get(ibuf, &imsg);
770 if (*tree && (*tree)->entries.nentries != nentries)
775 if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen)
776 return got_error(GOT_ERR_PRIVSEP_LEN);
778 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
780 switch (imsg.hdr.type) {
782 err = recv_imsg_error(&imsg, datalen);
785 /* This message should only appear once. */
787 err = got_error(GOT_ERR_PRIVSEP_MSG);
790 if (datalen != sizeof(*itree)) {
791 err = got_error(GOT_ERR_PRIVSEP_LEN);
795 *tree = malloc(sizeof(**tree));
797 err = got_error_from_errno("malloc");
800 (*tree)->entries.nentries = itree->nentries;
801 SIMPLEQ_INIT(&(*tree)->entries.head);
804 case GOT_IMSG_TREE_ENTRY:
805 /* This message should be preceeded by GOT_IMSG_TREE. */
807 err = got_error(GOT_ERR_PRIVSEP_MSG);
810 if (datalen < sizeof(*ite) || datalen > MAX_IMSGSIZE) {
811 err = got_error(GOT_ERR_PRIVSEP_LEN);
815 /* Remaining data contains the entry's name. */
816 datalen -= sizeof(*ite);
817 if (datalen == 0 || datalen > MAX_IMSGSIZE) {
818 err = got_error(GOT_ERR_PRIVSEP_LEN);
823 te = got_alloc_tree_entry_partial();
825 err = got_error_from_errno(
826 "got_alloc_tree_entry_partial");
829 te->name = malloc(datalen + 1);
830 if (te->name == NULL) {
832 err = got_error_from_errno("malloc");
835 memcpy(te->name, imsg.data + sizeof(*ite), datalen);
836 te->name[datalen] = '\0';
838 memcpy(te->id->sha1, ite->id, SHA1_DIGEST_LENGTH);
839 te->mode = ite->mode;
840 SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
844 err = got_error(GOT_ERR_PRIVSEP_MSG);
851 if (*tree && (*tree)->entries.nentries != nentries) {
853 err = got_error(GOT_ERR_PRIVSEP_LEN);
854 got_object_tree_close(*tree);
861 const struct got_error *
862 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
865 struct got_imsg_blob iblob;
868 iblob.hdrlen = hdrlen;
873 if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
874 return got_error(GOT_ERR_NO_SPACE);
876 buf = malloc(sizeof(iblob) + size);
878 return got_error_from_errno("malloc");
880 memcpy(buf, &iblob, sizeof(iblob));
881 memcpy(buf + sizeof(iblob), data, size);
882 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
883 sizeof(iblob) + size) == -1) {
885 return got_error_from_errno("imsg_compose BLOB");
889 /* Data has already been written to file descriptor. */
890 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
891 sizeof(iblob)) == -1)
892 return got_error_from_errno("imsg_compose BLOB");
896 return flush_imsg(ibuf);
899 const struct got_error *
900 got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
901 struct imsgbuf *ibuf)
903 const struct got_error *err = NULL;
905 struct got_imsg_blob *iblob;
910 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
914 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
916 switch (imsg.hdr.type) {
918 if (datalen < sizeof(*iblob)) {
919 err = got_error(GOT_ERR_PRIVSEP_LEN);
924 *hdrlen = iblob->hdrlen;
926 if (datalen == sizeof(*iblob)) {
927 /* Data has been written to file descriptor. */
931 if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX) {
932 err = got_error(GOT_ERR_PRIVSEP_LEN);
936 *outbuf = malloc(*size);
937 if (*outbuf == NULL) {
938 err = got_error_from_errno("malloc");
941 memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
944 err = got_error(GOT_ERR_PRIVSEP_MSG);
953 static const struct got_error *
954 send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
956 const struct got_error *err = NULL;
957 size_t offset, remain;
962 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
964 if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
965 tag->tagmsg + offset, n) == -1) {
966 err = got_error_from_errno("imsg_compose TAG_TAGMSG");
970 err = flush_imsg(ibuf);
981 const struct got_error *
982 got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
984 const struct got_error *err = NULL;
985 struct got_imsg_tag_object *itag;
988 size_t tag_len = strlen(tag->tag);
989 size_t tagger_len = strlen(tag->tagger);
990 size_t tagmsg_len = strlen(tag->tagmsg);
992 total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
996 return got_error_from_errno("malloc");
998 itag = (struct got_imsg_tag_object *)buf;
999 memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
1000 itag->obj_type = tag->obj_type;
1001 itag->tag_len = tag_len;
1002 itag->tagger_len = tagger_len;
1003 itag->tagger_time = tag->tagger_time;
1004 itag->tagger_gmtoff = tag->tagger_gmtoff;
1005 itag->tagmsg_len = tagmsg_len;
1007 len = sizeof(*itag);
1008 memcpy(buf + len, tag->tag, tag_len);
1010 memcpy(buf + len, tag->tagger, tagger_len);
1013 if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
1014 err = got_error_from_errno("imsg_compose TAG");
1018 if (tagmsg_len == 0 ||
1019 tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1020 err = flush_imsg(ibuf);
1024 err = send_tagmsg(ibuf, tag, tagmsg_len);
1030 const struct got_error *
1031 got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
1033 const struct got_error *err = NULL;
1035 struct got_imsg_tag_object *itag;
1036 size_t len, datalen;
1037 const size_t min_datalen =
1038 MIN(sizeof(struct got_imsg_error),
1039 sizeof(struct got_imsg_tag_object));
1043 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1047 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1050 switch (imsg.hdr.type) {
1052 if (datalen < sizeof(*itag)) {
1053 err = got_error(GOT_ERR_PRIVSEP_LEN);
1057 if (datalen != sizeof(*itag) + itag->tag_len +
1059 err = got_error(GOT_ERR_PRIVSEP_LEN);
1062 len += sizeof(*itag);
1064 *tag = calloc(1, sizeof(**tag));
1066 err = got_error_from_errno("calloc");
1070 memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
1072 if (itag->tag_len == 0) {
1073 (*tag)->tag = strdup("");
1074 if ((*tag)->tag == NULL) {
1075 err = got_error_from_errno("strdup");
1079 (*tag)->tag = malloc(itag->tag_len + 1);
1080 if ((*tag)->tag == NULL) {
1081 err = got_error_from_errno("malloc");
1084 memcpy((*tag)->tag, imsg.data + len,
1086 (*tag)->tag[itag->tag_len] = '\0';
1088 len += itag->tag_len;
1090 (*tag)->obj_type = itag->obj_type;
1091 (*tag)->tagger_time = itag->tagger_time;
1092 (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1094 if (itag->tagger_len == 0) {
1095 (*tag)->tagger = strdup("");
1096 if ((*tag)->tagger == NULL) {
1097 err = got_error_from_errno("strdup");
1101 (*tag)->tagger = malloc(itag->tagger_len + 1);
1102 if ((*tag)->tagger == NULL) {
1103 err = got_error_from_errno("malloc");
1106 memcpy((*tag)->tagger, imsg.data + len,
1108 (*tag)->tagger[itag->tagger_len] = '\0';
1110 len += itag->tagger_len;
1112 if (itag->tagmsg_len == 0) {
1113 (*tag)->tagmsg = strdup("");
1114 if ((*tag)->tagmsg == NULL) {
1115 err = got_error_from_errno("strdup");
1119 size_t offset = 0, remain = itag->tagmsg_len;
1121 (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1122 if ((*tag)->tagmsg == NULL) {
1123 err = got_error_from_errno("malloc");
1126 while (remain > 0) {
1127 struct imsg imsg_log;
1128 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1131 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1135 if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1136 return got_error(GOT_ERR_PRIVSEP_MSG);
1138 memcpy((*tag)->tagmsg + offset, imsg_log.data,
1140 imsg_free(&imsg_log);
1144 (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1149 err = got_error(GOT_ERR_PRIVSEP_MSG);
1158 const struct got_error *
1159 got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1160 struct got_packidx *packidx)
1162 const struct got_error *err = NULL;
1163 struct got_imsg_packidx ipackidx;
1164 struct got_imsg_pack ipack;
1167 ipackidx.len = packidx->len;
1168 fd = dup(packidx->fd);
1170 return got_error_from_errno("dup");
1172 if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
1173 sizeof(ipackidx)) == -1) {
1174 err = got_error_from_errno("imsg_compose PACKIDX");
1179 if (strlcpy(ipack.path_packfile, pack->path_packfile,
1180 sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
1181 return got_error(GOT_ERR_NO_SPACE);
1182 ipack.filesize = pack->filesize;
1186 return got_error_from_errno("dup");
1188 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
1190 err = got_error_from_errno("imsg_compose PACK");
1195 return flush_imsg(ibuf);
1198 const struct got_error *
1199 got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
1200 struct got_object_id *id)
1202 struct got_imsg_packed_object iobj;
1205 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
1207 if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
1208 &iobj, sizeof(iobj)) == -1)
1209 return got_error_from_errno("imsg_compose "
1210 "PACKED_OBJECT_REQUEST");
1212 return flush_imsg(ibuf);
1215 const struct got_error *
1216 got_privsep_send_gitconfig_parse_req(struct imsgbuf *ibuf, int fd)
1218 const struct got_error *err = NULL;
1220 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_PARSE_REQUEST, 0, 0, fd,
1222 err = got_error_from_errno("imsg_compose "
1223 "GITCONFIG_PARSE_REQUEST");
1228 return flush_imsg(ibuf);
1231 const struct got_error *
1232 got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *ibuf)
1234 if (imsg_compose(ibuf,
1235 GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST, 0, 0, -1,
1237 return got_error_from_errno("imsg_compose "
1238 "GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST");
1240 return flush_imsg(ibuf);
1243 const struct got_error *
1244 got_privsep_send_gitconfig_author_name_req(struct imsgbuf *ibuf)
1246 if (imsg_compose(ibuf,
1247 GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST, 0, 0, -1, NULL, 0) == -1)
1248 return got_error_from_errno("imsg_compose "
1249 "GITCONFIG_AUTHOR_NAME_REQUEST");
1251 return flush_imsg(ibuf);
1254 const struct got_error *
1255 got_privsep_send_gitconfig_author_email_req(struct imsgbuf *ibuf)
1257 if (imsg_compose(ibuf,
1258 GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST, 0, 0, -1, NULL, 0) == -1)
1259 return got_error_from_errno("imsg_compose "
1260 "GITCONFIG_AUTHOR_EMAIL_REQUEST");
1262 return flush_imsg(ibuf);
1265 const struct got_error *
1266 got_privsep_send_gitconfig_str(struct imsgbuf *ibuf, const char *value)
1268 size_t len = value ? strlen(value) + 1 : 0;
1270 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_STR_VAL, 0, 0, -1,
1272 return got_error_from_errno("imsg_compose GITCONFIG_STR_VAL");
1274 return flush_imsg(ibuf);
1277 const struct got_error *
1278 got_privsep_recv_gitconfig_str(char **str, struct imsgbuf *ibuf)
1280 const struct got_error *err = NULL;
1283 const size_t min_datalen = 0;
1287 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1290 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1292 switch (imsg.hdr.type) {
1293 case GOT_IMSG_GITCONFIG_STR_VAL:
1296 *str = malloc(datalen);
1298 err = got_error_from_errno("malloc");
1301 if (strlcpy(*str, imsg.data, datalen) >= datalen)
1302 err = got_error(GOT_ERR_NO_SPACE);
1305 err = got_error(GOT_ERR_PRIVSEP_MSG);
1313 const struct got_error *
1314 got_privsep_send_gitconfig_int(struct imsgbuf *ibuf, int value)
1316 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_INT_VAL, 0, 0, -1,
1317 &value, sizeof(value)) == -1)
1318 return got_error_from_errno("imsg_compose GITCONFIG_INT_VAL");
1320 return flush_imsg(ibuf);
1323 const struct got_error *
1324 got_privsep_recv_gitconfig_int(int *val, struct imsgbuf *ibuf)
1326 const struct got_error *err = NULL;
1329 const size_t min_datalen =
1330 MIN(sizeof(struct got_imsg_error), sizeof(int));
1334 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1337 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1339 switch (imsg.hdr.type) {
1340 case GOT_IMSG_GITCONFIG_INT_VAL:
1341 if (datalen != sizeof(*val)) {
1342 err = got_error(GOT_ERR_PRIVSEP_LEN);
1345 memcpy(val, imsg.data, sizeof(*val));
1348 err = got_error(GOT_ERR_PRIVSEP_MSG);
1356 const struct got_error *
1357 got_privsep_unveil_exec_helpers(void)
1359 const char *helpers[] = {
1360 GOT_PATH_PROG_READ_PACK,
1361 GOT_PATH_PROG_READ_OBJECT,
1362 GOT_PATH_PROG_READ_COMMIT,
1363 GOT_PATH_PROG_READ_TREE,
1364 GOT_PATH_PROG_READ_BLOB,
1365 GOT_PATH_PROG_READ_TAG,
1366 GOT_PATH_PROG_READ_GITCONFIG,
1370 for (i = 0; i < nitems(helpers); i++) {
1371 if (unveil(helpers[i], "x") == 0)
1373 return got_error_from_errno2("unveil", helpers[i]);
1380 got_privsep_exec_child(int imsg_fds[2], const char *path, const char *repo_path)
1382 if (close(imsg_fds[0]) != 0) {
1383 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
1387 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
1388 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
1391 if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
1392 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
1396 if (execl(path, path, repo_path, (char *)NULL) == -1) {
1397 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,