2 * Copyright (c) 2018, 2019, 2020 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_repository.h"
39 #include "got_lib_sha1.h"
40 #include "got_lib_delta.h"
41 #include "got_lib_inflate.h"
42 #include "got_lib_object.h"
43 #include "got_lib_object_parse.h"
44 #include "got_lib_privsep.h"
45 #include "got_lib_pack.h"
48 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
52 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
55 static const struct got_error *
56 poll_fd(int fd, int events, int timeout)
62 pfd[0].events = events;
64 n = poll(pfd, 1, timeout);
66 return got_error_from_errno("poll");
68 return got_error(GOT_ERR_TIMEOUT);
69 if (pfd[0].revents & (POLLERR | POLLNVAL))
70 return got_error_from_errno("poll error");
71 if (pfd[0].revents & (events | POLLHUP))
74 return got_error(GOT_ERR_INTERRUPT);
77 static const struct got_error *
78 read_imsg(struct imsgbuf *ibuf)
80 const struct got_error *err;
83 err = poll_fd(ibuf->fd, POLLIN, INFTIM);
89 if (errno == EAGAIN) /* Could be a file-descriptor leak. */
90 return got_error(GOT_ERR_PRIVSEP_NO_FD);
91 return got_error(GOT_ERR_PRIVSEP_READ);
94 return got_error(GOT_ERR_PRIVSEP_PIPE);
99 const struct got_error *
100 got_privsep_wait_for_child(pid_t pid)
104 if (waitpid(pid, &child_status, 0) == -1)
105 return got_error_from_errno("waitpid");
107 if (!WIFEXITED(child_status))
108 return got_error(GOT_ERR_PRIVSEP_DIED);
110 if (WEXITSTATUS(child_status) != 0)
111 return got_error(GOT_ERR_PRIVSEP_EXIT);
116 static const struct got_error *
117 recv_imsg_error(struct imsg *imsg, size_t datalen)
119 struct got_imsg_error *ierr;
121 if (datalen != sizeof(*ierr))
122 return got_error(GOT_ERR_PRIVSEP_LEN);
125 if (ierr->code == GOT_ERR_ERRNO) {
126 static struct got_error serr;
127 serr.code = GOT_ERR_ERRNO;
128 serr.msg = strerror(ierr->errno_code);
132 return got_error(ierr->code);
135 const struct got_error *
136 got_privsep_recv_imsg(struct imsg *imsg, struct imsgbuf *ibuf,
139 const struct got_error *err;
142 n = imsg_get(ibuf, imsg);
144 return got_error_from_errno("imsg_get");
147 err = read_imsg(ibuf);
150 n = imsg_get(ibuf, imsg);
152 return got_error_from_errno("imsg_get");
155 if (imsg->hdr.len < IMSG_HEADER_SIZE + min_datalen)
156 return got_error(GOT_ERR_PRIVSEP_LEN);
158 if (imsg->hdr.type == GOT_IMSG_ERROR) {
159 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
160 return recv_imsg_error(imsg, datalen);
166 /* Attempt to send an error in an imsg. Complain on stderr as a last resort. */
168 got_privsep_send_error(struct imsgbuf *ibuf, const struct got_error *err)
170 const struct got_error *poll_err;
171 struct got_imsg_error ierr;
174 ierr.code = err->code;
175 if (err->code == GOT_ERR_ERRNO)
176 ierr.errno_code = errno;
179 ret = imsg_compose(ibuf, GOT_IMSG_ERROR, 0, 0, -1, &ierr, sizeof(ierr));
181 fprintf(stderr, "%s: error %d \"%s\": imsg_compose: %s\n",
182 getprogname(), err->code, err->msg, strerror(errno));
186 poll_err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
188 fprintf(stderr, "%s: error %d \"%s\": poll: %s\n",
189 getprogname(), err->code, err->msg, poll_err->msg);
193 ret = imsg_flush(ibuf);
195 fprintf(stderr, "%s: error %d \"%s\": imsg_flush: %s\n",
196 getprogname(), err->code, err->msg, strerror(errno));
201 static const struct got_error *
202 flush_imsg(struct imsgbuf *ibuf)
204 const struct got_error *err;
206 err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
210 if (imsg_flush(ibuf) == -1)
211 return got_error_from_errno("imsg_flush");
216 const struct got_error *
217 got_privsep_send_stop(int fd)
219 const struct got_error *err = NULL;
222 imsg_init(&ibuf, fd);
224 if (imsg_compose(&ibuf, GOT_IMSG_STOP, 0, 0, -1, NULL, 0) == -1)
225 return got_error_from_errno("imsg_compose STOP");
227 err = flush_imsg(&ibuf);
232 const struct got_error *
233 got_privsep_send_obj_req(struct imsgbuf *ibuf, int fd)
235 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_REQUEST, 0, 0, fd, NULL, 0)
237 return got_error_from_errno("imsg_compose OBJECT_REQUEST");
239 return flush_imsg(ibuf);
242 const struct got_error *
243 got_privsep_send_commit_req(struct imsgbuf *ibuf, int fd,
244 struct got_object_id *id, int pack_idx)
246 const struct got_error *err = NULL;
247 struct got_imsg_packed_object iobj, *iobjp;
250 if (id) { /* commit is packed */
252 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
260 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_REQUEST, 0, 0, fd, iobjp, len)
262 err = got_error_from_errno("imsg_compose COMMIT_REQUEST");
267 return flush_imsg(ibuf);
270 const struct got_error *
271 got_privsep_send_tree_req(struct imsgbuf *ibuf, int fd,
272 struct got_object_id *id, int pack_idx)
274 const struct got_error *err = NULL;
276 size_t len = id ? sizeof(struct got_imsg_packed_object) : 0;
278 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_REQUEST, 0, 0, len);
280 return got_error_from_errno("imsg_create TREE_REQUEST");
282 if (id) { /* tree is packed */
283 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
284 err = got_error_from_errno("imsg_add TREE_ENTRY");
289 if (imsg_add(wbuf, &pack_idx, sizeof(pack_idx)) == -1) {
290 err = got_error_from_errno("imsg_add TREE_ENTRY");
297 imsg_close(ibuf, wbuf);
299 return flush_imsg(ibuf);
302 const struct got_error *
303 got_privsep_send_tag_req(struct imsgbuf *ibuf, int fd,
304 struct got_object_id *id, int pack_idx)
306 struct got_imsg_packed_object iobj, *iobjp;
309 if (id) { /* tag is packed */
311 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
319 if (imsg_compose(ibuf, GOT_IMSG_TAG_REQUEST, 0, 0, fd, iobjp, len)
321 return got_error_from_errno("imsg_compose TAG_REQUEST");
323 return flush_imsg(ibuf);
326 const struct got_error *
327 got_privsep_send_blob_req(struct imsgbuf *ibuf, int infd,
328 struct got_object_id *id, int pack_idx)
330 const struct got_error *err = NULL;
331 struct got_imsg_packed_object iobj, *iobjp;
334 if (id) { /* blob is packed */
336 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
344 if (imsg_compose(ibuf, GOT_IMSG_BLOB_REQUEST, 0, 0, infd, iobjp, len)
346 err = got_error_from_errno("imsg_compose BLOB_REQUEST");
351 return flush_imsg(ibuf);
354 const struct got_error *
355 got_privsep_send_blob_outfd(struct imsgbuf *ibuf, int outfd)
357 const struct got_error *err = NULL;
359 if (imsg_compose(ibuf, GOT_IMSG_BLOB_OUTFD, 0, 0, outfd, NULL, 0)
361 err = got_error_from_errno("imsg_compose BLOB_OUTFD");
366 return flush_imsg(ibuf);
369 const struct got_error *
370 got_privsep_send_tmpfd(struct imsgbuf *ibuf, int fd)
372 const struct got_error *err = NULL;
374 if (imsg_compose(ibuf, GOT_IMSG_TMPFD, 0, 0, fd, NULL, 0)
376 err = got_error_from_errno("imsg_compose TMPFD");
381 return flush_imsg(ibuf);
384 const struct got_error *
385 got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj)
387 struct got_imsg_object iobj;
389 memcpy(iobj.id, obj->id.sha1, sizeof(iobj.id));
390 iobj.type = obj->type;
391 iobj.flags = obj->flags;
392 iobj.hdrlen = obj->hdrlen;
393 iobj.size = obj->size;
394 if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
395 iobj.pack_offset = obj->pack_offset;
396 iobj.pack_idx = obj->pack_idx;
399 if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
401 return got_error_from_errno("imsg_compose OBJECT");
403 return flush_imsg(ibuf);
406 const struct got_error *
407 got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
408 struct imsgbuf *ibuf)
410 const struct got_error *err = NULL;
411 struct got_imsg_object *iobj;
412 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
414 if (datalen != sizeof(*iobj))
415 return got_error(GOT_ERR_PRIVSEP_LEN);
418 *obj = calloc(1, sizeof(**obj));
420 return got_error_from_errno("calloc");
422 memcpy((*obj)->id.sha1, iobj->id, SHA1_DIGEST_LENGTH);
423 (*obj)->type = iobj->type;
424 (*obj)->flags = iobj->flags;
425 (*obj)->hdrlen = iobj->hdrlen;
426 (*obj)->size = iobj->size;
427 /* path_packfile is handled by caller */
428 if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
429 (*obj)->pack_offset = iobj->pack_offset;
430 (*obj)->pack_idx = iobj->pack_idx;
436 const struct got_error *
437 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
439 const struct got_error *err = NULL;
441 const size_t min_datalen =
442 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
446 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
450 switch (imsg.hdr.type) {
451 case GOT_IMSG_OBJECT:
452 err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
455 err = got_error(GOT_ERR_PRIVSEP_MSG);
464 static const struct got_error *
465 send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
468 const struct got_error *err = NULL;
469 size_t offset, remain;
474 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
476 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
477 commit->logmsg + offset, n) == -1) {
478 err = got_error_from_errno("imsg_compose "
483 err = flush_imsg(ibuf);
494 const struct got_error *
495 got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
497 const struct got_error *err = NULL;
498 struct got_imsg_commit_object *icommit;
501 struct got_object_qid *qid;
502 size_t author_len = strlen(commit->author);
503 size_t committer_len = strlen(commit->committer);
504 size_t logmsg_len = strlen(commit->logmsg);
506 total = sizeof(*icommit) + author_len + committer_len +
507 commit->nparents * SHA1_DIGEST_LENGTH;
511 return got_error_from_errno("malloc");
513 icommit = (struct got_imsg_commit_object *)buf;
514 memcpy(icommit->tree_id, commit->tree_id->sha1,
515 sizeof(icommit->tree_id));
516 icommit->author_len = author_len;
517 icommit->author_time = commit->author_time;
518 icommit->author_gmtoff = commit->author_gmtoff;
519 icommit->committer_len = committer_len;
520 icommit->committer_time = commit->committer_time;
521 icommit->committer_gmtoff = commit->committer_gmtoff;
522 icommit->logmsg_len = logmsg_len;
523 icommit->nparents = commit->nparents;
525 len = sizeof(*icommit);
526 memcpy(buf + len, commit->author, author_len);
528 memcpy(buf + len, commit->committer, committer_len);
529 len += committer_len;
530 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
531 memcpy(buf + len, qid->id, SHA1_DIGEST_LENGTH);
532 len += SHA1_DIGEST_LENGTH;
535 if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
536 err = got_error_from_errno("imsg_compose COMMIT");
540 if (logmsg_len == 0 ||
541 logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
542 err = flush_imsg(ibuf);
546 err = send_commit_logmsg(ibuf, commit, logmsg_len);
552 static const struct got_error *
553 get_commit_from_imsg(struct got_commit_object **commit,
554 struct imsg *imsg, size_t datalen, struct imsgbuf *ibuf)
556 const struct got_error *err = NULL;
557 struct got_imsg_commit_object *icommit;
561 if (datalen < sizeof(*icommit))
562 return got_error(GOT_ERR_PRIVSEP_LEN);
564 icommit = imsg->data;
565 if (datalen != sizeof(*icommit) + icommit->author_len +
566 icommit->committer_len +
567 icommit->nparents * SHA1_DIGEST_LENGTH)
568 return got_error(GOT_ERR_PRIVSEP_LEN);
570 if (icommit->nparents < 0)
571 return got_error(GOT_ERR_PRIVSEP_LEN);
573 len += sizeof(*icommit);
575 *commit = got_object_commit_alloc_partial();
577 return got_error_from_errno(
578 "got_object_commit_alloc_partial");
580 memcpy((*commit)->tree_id->sha1, icommit->tree_id,
582 (*commit)->author_time = icommit->author_time;
583 (*commit)->author_gmtoff = icommit->author_gmtoff;
584 (*commit)->committer_time = icommit->committer_time;
585 (*commit)->committer_gmtoff = icommit->committer_gmtoff;
587 if (icommit->author_len == 0) {
588 (*commit)->author = strdup("");
589 if ((*commit)->author == NULL) {
590 err = got_error_from_errno("strdup");
594 (*commit)->author = malloc(icommit->author_len + 1);
595 if ((*commit)->author == NULL) {
596 err = got_error_from_errno("malloc");
599 memcpy((*commit)->author, imsg->data + len,
600 icommit->author_len);
601 (*commit)->author[icommit->author_len] = '\0';
603 len += icommit->author_len;
605 if (icommit->committer_len == 0) {
606 (*commit)->committer = strdup("");
607 if ((*commit)->committer == NULL) {
608 err = got_error_from_errno("strdup");
612 (*commit)->committer =
613 malloc(icommit->committer_len + 1);
614 if ((*commit)->committer == NULL) {
615 err = got_error_from_errno("malloc");
618 memcpy((*commit)->committer, imsg->data + len,
619 icommit->committer_len);
620 (*commit)->committer[icommit->committer_len] = '\0';
622 len += icommit->committer_len;
624 if (icommit->logmsg_len == 0) {
625 (*commit)->logmsg = strdup("");
626 if ((*commit)->logmsg == NULL) {
627 err = got_error_from_errno("strdup");
631 size_t offset = 0, remain = icommit->logmsg_len;
633 (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
634 if ((*commit)->logmsg == NULL) {
635 err = got_error_from_errno("malloc");
639 struct imsg imsg_log;
640 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
643 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
647 if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG) {
648 err = got_error(GOT_ERR_PRIVSEP_MSG);
652 memcpy((*commit)->logmsg + offset,
654 imsg_free(&imsg_log);
658 (*commit)->logmsg[icommit->logmsg_len] = '\0';
661 for (i = 0; i < icommit->nparents; i++) {
662 struct got_object_qid *qid;
664 err = got_object_qid_alloc_partial(&qid);
667 memcpy(qid->id, imsg->data + len +
668 i * SHA1_DIGEST_LENGTH, sizeof(*qid->id));
669 SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
670 (*commit)->nparents++;
674 got_object_commit_close(*commit);
680 const struct got_error *
681 got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
683 const struct got_error *err = NULL;
686 const size_t min_datalen =
687 MIN(sizeof(struct got_imsg_error),
688 sizeof(struct got_imsg_commit_object));
692 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
696 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
698 switch (imsg.hdr.type) {
699 case GOT_IMSG_COMMIT:
700 err = get_commit_from_imsg(commit, &imsg, datalen, ibuf);
703 err = got_error(GOT_ERR_PRIVSEP_MSG);
712 const struct got_error *
713 got_privsep_send_tree(struct imsgbuf *ibuf, struct got_pathlist_head *entries,
716 const struct got_error *err = NULL;
717 struct got_imsg_tree_object itree;
718 struct got_pathlist_entry *pe;
720 int nimsg; /* number of imsg queued in ibuf */
722 itree.nentries = nentries;
723 if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
725 return got_error_from_errno("imsg_compose TREE");
727 totlen = sizeof(itree);
729 TAILQ_FOREACH(pe, entries, entry) {
730 const char *name = pe->path;
731 struct got_parsed_tree_entry *pte = pe->data;
733 size_t namelen = strlen(name);
734 size_t len = sizeof(struct got_imsg_tree_entry) + namelen;
736 if (len > MAX_IMSGSIZE)
737 return got_error(GOT_ERR_NO_SPACE);
740 if (totlen + len >= MAX_IMSGSIZE - (IMSG_HEADER_SIZE * nimsg)) {
741 err = flush_imsg(ibuf);
747 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_ENTRY, 0, 0, len);
749 return got_error_from_errno("imsg_create TREE_ENTRY");
751 /* Keep in sync with struct got_imsg_tree_object definition! */
752 if (imsg_add(wbuf, pte->id, SHA1_DIGEST_LENGTH) == -1) {
753 err = got_error_from_errno("imsg_add TREE_ENTRY");
757 if (imsg_add(wbuf, &pte->mode, sizeof(pte->mode)) == -1) {
758 err = got_error_from_errno("imsg_add TREE_ENTRY");
763 if (imsg_add(wbuf, name, namelen) == -1) {
764 err = got_error_from_errno("imsg_add TREE_ENTRY");
770 imsg_close(ibuf, wbuf);
775 return flush_imsg(ibuf);
778 const struct got_error *
779 got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
781 const struct got_error *err = NULL;
782 const size_t min_datalen =
783 MIN(sizeof(struct got_imsg_error),
784 sizeof(struct got_imsg_tree_object));
785 struct got_imsg_tree_object *itree;
790 err = read_imsg(ibuf);
798 struct got_imsg_tree_entry *ite;
799 struct got_tree_entry *te = NULL;
801 n = imsg_get(ibuf, &imsg);
803 if (*tree && (*tree)->nentries != nentries)
808 if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen)
809 return got_error(GOT_ERR_PRIVSEP_LEN);
811 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
813 switch (imsg.hdr.type) {
815 err = recv_imsg_error(&imsg, datalen);
818 /* This message should only appear once. */
820 err = got_error(GOT_ERR_PRIVSEP_MSG);
823 if (datalen != sizeof(*itree)) {
824 err = got_error(GOT_ERR_PRIVSEP_LEN);
828 *tree = malloc(sizeof(**tree));
830 err = got_error_from_errno("malloc");
833 (*tree)->entries = calloc(itree->nentries,
834 sizeof(struct got_tree_entry));
835 if ((*tree)->entries == NULL) {
836 err = got_error_from_errno("malloc");
839 (*tree)->nentries = itree->nentries;
842 case GOT_IMSG_TREE_ENTRY:
843 /* This message should be preceeded by GOT_IMSG_TREE. */
845 err = got_error(GOT_ERR_PRIVSEP_MSG);
848 if (datalen < sizeof(*ite) || datalen > MAX_IMSGSIZE) {
849 err = got_error(GOT_ERR_PRIVSEP_LEN);
853 /* Remaining data contains the entry's name. */
854 datalen -= sizeof(*ite);
855 if (datalen == 0 || datalen > MAX_IMSGSIZE) {
856 err = got_error(GOT_ERR_PRIVSEP_LEN);
861 if (datalen + 1 > sizeof(te->name)) {
862 err = got_error(GOT_ERR_NO_SPACE);
865 te = &(*tree)->entries[nentries];
866 memcpy(te->name, imsg.data + sizeof(*ite), datalen);
867 te->name[datalen] = '\0';
869 memcpy(te->id.sha1, ite->id, SHA1_DIGEST_LENGTH);
870 te->mode = ite->mode;
875 err = got_error(GOT_ERR_PRIVSEP_MSG);
882 if (*tree && (*tree)->nentries != nentries) {
884 err = got_error(GOT_ERR_PRIVSEP_LEN);
885 got_object_tree_close(*tree);
892 const struct got_error *
893 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
896 struct got_imsg_blob iblob;
899 iblob.hdrlen = hdrlen;
904 if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
905 return got_error(GOT_ERR_NO_SPACE);
907 buf = malloc(sizeof(iblob) + size);
909 return got_error_from_errno("malloc");
911 memcpy(buf, &iblob, sizeof(iblob));
912 memcpy(buf + sizeof(iblob), data, size);
913 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
914 sizeof(iblob) + size) == -1) {
916 return got_error_from_errno("imsg_compose BLOB");
920 /* Data has already been written to file descriptor. */
921 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
922 sizeof(iblob)) == -1)
923 return got_error_from_errno("imsg_compose BLOB");
927 return flush_imsg(ibuf);
930 const struct got_error *
931 got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
932 struct imsgbuf *ibuf)
934 const struct got_error *err = NULL;
936 struct got_imsg_blob *iblob;
941 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
945 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
947 switch (imsg.hdr.type) {
949 if (datalen < sizeof(*iblob)) {
950 err = got_error(GOT_ERR_PRIVSEP_LEN);
955 *hdrlen = iblob->hdrlen;
957 if (datalen == sizeof(*iblob)) {
958 /* Data has been written to file descriptor. */
962 if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX) {
963 err = got_error(GOT_ERR_PRIVSEP_LEN);
967 *outbuf = malloc(*size);
968 if (*outbuf == NULL) {
969 err = got_error_from_errno("malloc");
972 memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
975 err = got_error(GOT_ERR_PRIVSEP_MSG);
984 static const struct got_error *
985 send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
987 const struct got_error *err = NULL;
988 size_t offset, remain;
993 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
995 if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
996 tag->tagmsg + offset, n) == -1) {
997 err = got_error_from_errno("imsg_compose TAG_TAGMSG");
1001 err = flush_imsg(ibuf);
1012 const struct got_error *
1013 got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
1015 const struct got_error *err = NULL;
1016 struct got_imsg_tag_object *itag;
1019 size_t tag_len = strlen(tag->tag);
1020 size_t tagger_len = strlen(tag->tagger);
1021 size_t tagmsg_len = strlen(tag->tagmsg);
1023 total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
1025 buf = malloc(total);
1027 return got_error_from_errno("malloc");
1029 itag = (struct got_imsg_tag_object *)buf;
1030 memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
1031 itag->obj_type = tag->obj_type;
1032 itag->tag_len = tag_len;
1033 itag->tagger_len = tagger_len;
1034 itag->tagger_time = tag->tagger_time;
1035 itag->tagger_gmtoff = tag->tagger_gmtoff;
1036 itag->tagmsg_len = tagmsg_len;
1038 len = sizeof(*itag);
1039 memcpy(buf + len, tag->tag, tag_len);
1041 memcpy(buf + len, tag->tagger, tagger_len);
1044 if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
1045 err = got_error_from_errno("imsg_compose TAG");
1049 if (tagmsg_len == 0 ||
1050 tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1051 err = flush_imsg(ibuf);
1055 err = send_tagmsg(ibuf, tag, tagmsg_len);
1061 const struct got_error *
1062 got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
1064 const struct got_error *err = NULL;
1066 struct got_imsg_tag_object *itag;
1067 size_t len, datalen;
1068 const size_t min_datalen =
1069 MIN(sizeof(struct got_imsg_error),
1070 sizeof(struct got_imsg_tag_object));
1074 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1078 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1081 switch (imsg.hdr.type) {
1083 if (datalen < sizeof(*itag)) {
1084 err = got_error(GOT_ERR_PRIVSEP_LEN);
1088 if (datalen != sizeof(*itag) + itag->tag_len +
1090 err = got_error(GOT_ERR_PRIVSEP_LEN);
1093 len += sizeof(*itag);
1095 *tag = calloc(1, sizeof(**tag));
1097 err = got_error_from_errno("calloc");
1101 memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
1103 if (itag->tag_len == 0) {
1104 (*tag)->tag = strdup("");
1105 if ((*tag)->tag == NULL) {
1106 err = got_error_from_errno("strdup");
1110 (*tag)->tag = malloc(itag->tag_len + 1);
1111 if ((*tag)->tag == NULL) {
1112 err = got_error_from_errno("malloc");
1115 memcpy((*tag)->tag, imsg.data + len,
1117 (*tag)->tag[itag->tag_len] = '\0';
1119 len += itag->tag_len;
1121 (*tag)->obj_type = itag->obj_type;
1122 (*tag)->tagger_time = itag->tagger_time;
1123 (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1125 if (itag->tagger_len == 0) {
1126 (*tag)->tagger = strdup("");
1127 if ((*tag)->tagger == NULL) {
1128 err = got_error_from_errno("strdup");
1132 (*tag)->tagger = malloc(itag->tagger_len + 1);
1133 if ((*tag)->tagger == NULL) {
1134 err = got_error_from_errno("malloc");
1137 memcpy((*tag)->tagger, imsg.data + len,
1139 (*tag)->tagger[itag->tagger_len] = '\0';
1141 len += itag->tagger_len;
1143 if (itag->tagmsg_len == 0) {
1144 (*tag)->tagmsg = strdup("");
1145 if ((*tag)->tagmsg == NULL) {
1146 err = got_error_from_errno("strdup");
1150 size_t offset = 0, remain = itag->tagmsg_len;
1152 (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1153 if ((*tag)->tagmsg == NULL) {
1154 err = got_error_from_errno("malloc");
1157 while (remain > 0) {
1158 struct imsg imsg_log;
1159 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1162 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1166 if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1167 return got_error(GOT_ERR_PRIVSEP_MSG);
1169 memcpy((*tag)->tagmsg + offset, imsg_log.data,
1171 imsg_free(&imsg_log);
1175 (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1180 err = got_error(GOT_ERR_PRIVSEP_MSG);
1189 const struct got_error *
1190 got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1191 struct got_packidx *packidx)
1193 const struct got_error *err = NULL;
1194 struct got_imsg_packidx ipackidx;
1195 struct got_imsg_pack ipack;
1198 ipackidx.len = packidx->len;
1199 fd = dup(packidx->fd);
1201 return got_error_from_errno("dup");
1203 if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
1204 sizeof(ipackidx)) == -1) {
1205 err = got_error_from_errno("imsg_compose PACKIDX");
1210 if (strlcpy(ipack.path_packfile, pack->path_packfile,
1211 sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
1212 return got_error(GOT_ERR_NO_SPACE);
1213 ipack.filesize = pack->filesize;
1217 return got_error_from_errno("dup");
1219 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
1221 err = got_error_from_errno("imsg_compose PACK");
1226 return flush_imsg(ibuf);
1229 const struct got_error *
1230 got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
1231 struct got_object_id *id)
1233 struct got_imsg_packed_object iobj;
1236 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
1238 if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
1239 &iobj, sizeof(iobj)) == -1)
1240 return got_error_from_errno("imsg_compose "
1241 "PACKED_OBJECT_REQUEST");
1243 return flush_imsg(ibuf);
1246 const struct got_error *
1247 got_privsep_send_gitconfig_parse_req(struct imsgbuf *ibuf, int fd)
1249 const struct got_error *err = NULL;
1251 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_PARSE_REQUEST, 0, 0, fd,
1253 err = got_error_from_errno("imsg_compose "
1254 "GITCONFIG_PARSE_REQUEST");
1259 return flush_imsg(ibuf);
1262 const struct got_error *
1263 got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *ibuf)
1265 if (imsg_compose(ibuf,
1266 GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST, 0, 0, -1,
1268 return got_error_from_errno("imsg_compose "
1269 "GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST");
1271 return flush_imsg(ibuf);
1274 const struct got_error *
1275 got_privsep_send_gitconfig_author_name_req(struct imsgbuf *ibuf)
1277 if (imsg_compose(ibuf,
1278 GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST, 0, 0, -1, NULL, 0) == -1)
1279 return got_error_from_errno("imsg_compose "
1280 "GITCONFIG_AUTHOR_NAME_REQUEST");
1282 return flush_imsg(ibuf);
1285 const struct got_error *
1286 got_privsep_send_gitconfig_author_email_req(struct imsgbuf *ibuf)
1288 if (imsg_compose(ibuf,
1289 GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST, 0, 0, -1, NULL, 0) == -1)
1290 return got_error_from_errno("imsg_compose "
1291 "GITCONFIG_AUTHOR_EMAIL_REQUEST");
1293 return flush_imsg(ibuf);
1296 const struct got_error *
1297 got_privsep_send_gitconfig_remotes_req(struct imsgbuf *ibuf)
1299 if (imsg_compose(ibuf,
1300 GOT_IMSG_GITCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
1301 return got_error_from_errno("imsg_compose "
1302 "GITCONFIG_REMOTE_REQUEST");
1304 return flush_imsg(ibuf);
1307 const struct got_error *
1308 got_privsep_send_gitconfig_owner_req(struct imsgbuf *ibuf)
1310 if (imsg_compose(ibuf,
1311 GOT_IMSG_GITCONFIG_OWNER_REQUEST, 0, 0, -1, NULL, 0) == -1)
1312 return got_error_from_errno("imsg_compose "
1313 "GITCONFIG_OWNER_REQUEST");
1315 return flush_imsg(ibuf);
1318 const struct got_error *
1319 got_privsep_send_gitconfig_str(struct imsgbuf *ibuf, const char *value)
1321 size_t len = value ? strlen(value) + 1 : 0;
1323 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_STR_VAL, 0, 0, -1,
1325 return got_error_from_errno("imsg_compose GITCONFIG_STR_VAL");
1327 return flush_imsg(ibuf);
1330 const struct got_error *
1331 got_privsep_recv_gitconfig_str(char **str, struct imsgbuf *ibuf)
1333 const struct got_error *err = NULL;
1336 const size_t min_datalen = 0;
1340 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1343 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1345 switch (imsg.hdr.type) {
1346 case GOT_IMSG_GITCONFIG_STR_VAL:
1349 *str = malloc(datalen);
1351 err = got_error_from_errno("malloc");
1354 if (strlcpy(*str, imsg.data, datalen) >= datalen)
1355 err = got_error(GOT_ERR_NO_SPACE);
1358 err = got_error(GOT_ERR_PRIVSEP_MSG);
1366 const struct got_error *
1367 got_privsep_send_gitconfig_int(struct imsgbuf *ibuf, int value)
1369 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_INT_VAL, 0, 0, -1,
1370 &value, sizeof(value)) == -1)
1371 return got_error_from_errno("imsg_compose GITCONFIG_INT_VAL");
1373 return flush_imsg(ibuf);
1376 const struct got_error *
1377 got_privsep_recv_gitconfig_int(int *val, struct imsgbuf *ibuf)
1379 const struct got_error *err = NULL;
1382 const size_t min_datalen =
1383 MIN(sizeof(struct got_imsg_error), sizeof(int));
1387 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1390 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1392 switch (imsg.hdr.type) {
1393 case GOT_IMSG_GITCONFIG_INT_VAL:
1394 if (datalen != sizeof(*val)) {
1395 err = got_error(GOT_ERR_PRIVSEP_LEN);
1398 memcpy(val, imsg.data, sizeof(*val));
1401 err = got_error(GOT_ERR_PRIVSEP_MSG);
1409 const struct got_error *
1410 got_privsep_send_gitconfig_remotes(struct imsgbuf *ibuf,
1411 struct got_remote_repo *remotes, int nremotes)
1413 const struct got_error *err = NULL;
1414 struct got_imsg_remotes iremotes;
1417 iremotes.nremotes = nremotes;
1418 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_REMOTES, 0, 0, -1,
1419 &iremotes, sizeof(iremotes)) == -1)
1420 return got_error_from_errno("imsg_compose GITCONFIG_REMOTES");
1422 err = flush_imsg(ibuf);
1427 for (i = 0; i < nremotes; i++) {
1428 struct got_imsg_remote iremote;
1429 size_t len = sizeof(iremote);
1432 iremote.name_len = strlen(remotes[i].name);
1433 len += iremote.name_len;
1434 iremote.url_len = strlen(remotes[i].url);
1435 len += iremote.url_len;
1437 wbuf = imsg_create(ibuf, GOT_IMSG_GITCONFIG_REMOTE, 0, 0, len);
1439 return got_error_from_errno(
1440 "imsg_create GITCONFIG_REMOTE");
1442 if (imsg_add(wbuf, &iremote, sizeof(iremote)) == -1) {
1443 err = got_error_from_errno(
1444 "imsg_add GIITCONFIG_REMOTE");
1449 if (imsg_add(wbuf, remotes[i].name, iremote.name_len) == -1) {
1450 err = got_error_from_errno(
1451 "imsg_add GIITCONFIG_REMOTE");
1455 if (imsg_add(wbuf, remotes[i].url, iremote.url_len) == -1) {
1456 err = got_error_from_errno(
1457 "imsg_add GIITCONFIG_REMOTE");
1463 imsg_close(ibuf, wbuf);
1464 err = flush_imsg(ibuf);
1472 const struct got_error *
1473 got_privsep_recv_gitconfig_remotes(struct got_remote_repo **remotes,
1474 int *nremotes, struct imsgbuf *ibuf)
1476 const struct got_error *err = NULL;
1479 struct got_imsg_remotes iremotes;
1480 struct got_imsg_remote iremote;
1485 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremotes));
1488 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1490 switch (imsg.hdr.type) {
1491 case GOT_IMSG_GITCONFIG_REMOTES:
1492 if (datalen != sizeof(iremotes)) {
1493 err = got_error(GOT_ERR_PRIVSEP_LEN);
1496 memcpy(&iremotes, imsg.data, sizeof(iremotes));
1497 if (iremotes.nremotes == 0) {
1503 err = got_error(GOT_ERR_PRIVSEP_MSG);
1509 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(iremote));
1510 if (*remotes == NULL)
1511 return got_error_from_errno("recallocarray");
1513 while (*nremotes < iremotes.nremotes) {
1514 struct got_remote_repo *remote;
1516 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremote));
1519 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1521 switch (imsg.hdr.type) {
1522 case GOT_IMSG_GITCONFIG_REMOTE:
1523 remote = &(*remotes)[*nremotes];
1524 if (datalen < sizeof(iremote)) {
1525 err = got_error(GOT_ERR_PRIVSEP_LEN);
1528 memcpy(&iremote, imsg.data, sizeof(iremote));
1529 if (iremote.name_len == 0 || iremote.url_len == 0 ||
1530 (sizeof(iremote) + iremote.name_len +
1531 iremote.url_len) > datalen) {
1532 err = got_error(GOT_ERR_PRIVSEP_LEN);
1535 remote->name = strndup(imsg.data + sizeof(iremote),
1537 if (remote->name == NULL) {
1538 err = got_error_from_errno("strndup");
1541 remote->url = strndup(imsg.data + sizeof(iremote) +
1542 iremote.name_len, iremote.url_len);
1543 if (remote->url == NULL) {
1544 err = got_error_from_errno("strndup");
1551 err = got_error(GOT_ERR_PRIVSEP_MSG);
1562 for (i = 0; i < *nremotes; i++) {
1563 free((*remotes)[i].name);
1564 free((*remotes)[i].url);
1573 const struct got_error *
1574 got_privsep_send_commit_traversal_request(struct imsgbuf *ibuf,
1575 struct got_object_id *id, int idx, const char *path)
1577 const struct got_error *err = NULL;
1579 size_t path_len = strlen(path) + 1;
1581 wbuf = imsg_create(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_REQUEST, 0, 0,
1582 sizeof(struct got_imsg_commit_traversal_request) + path_len);
1584 return got_error_from_errno(
1585 "imsg_create COMMIT_TRAVERSAL_REQUEST");
1586 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
1587 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
1591 if (imsg_add(wbuf, &idx, sizeof(idx)) == -1) {
1592 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
1596 if (imsg_add(wbuf, path, path_len) == -1) {
1597 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
1603 imsg_close(ibuf, wbuf);
1605 return flush_imsg(ibuf);
1608 const struct got_error *
1609 got_privsep_send_traversed_commits(struct got_object_id *commit_ids,
1610 size_t ncommits, struct imsgbuf *ibuf)
1612 const struct got_error *err;
1616 wbuf = imsg_create(ibuf, GOT_IMSG_TRAVERSED_COMMITS, 0, 0,
1617 sizeof(struct got_imsg_traversed_commits) +
1618 ncommits * SHA1_DIGEST_LENGTH);
1620 return got_error_from_errno("imsg_create TRAVERSED_COMMITS");
1622 if (imsg_add(wbuf, &ncommits, sizeof(ncommits)) == -1) {
1623 err = got_error_from_errno("imsg_add TRAVERSED_COMMITS");
1627 for (i = 0; i < ncommits; i++) {
1628 struct got_object_id *id = &commit_ids[i];
1629 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
1630 err = got_error_from_errno(
1631 "imsg_add TRAVERSED_COMMITS");
1638 imsg_close(ibuf, wbuf);
1640 return flush_imsg(ibuf);
1643 const struct got_error *
1644 got_privsep_recv_traversed_commits(struct got_commit_object **changed_commit,
1645 struct got_object_id **changed_commit_id,
1646 struct got_object_id_queue *commit_ids, struct imsgbuf *ibuf)
1648 const struct got_error *err = NULL;
1650 struct got_imsg_traversed_commits *icommits;
1654 *changed_commit = NULL;
1655 *changed_commit_id = NULL;
1658 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1662 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1663 switch (imsg.hdr.type) {
1664 case GOT_IMSG_TRAVERSED_COMMITS:
1665 icommits = imsg.data;
1666 if (datalen != sizeof(*icommits) +
1667 icommits->ncommits * SHA1_DIGEST_LENGTH) {
1668 err = got_error(GOT_ERR_PRIVSEP_LEN);
1671 for (i = 0; i < icommits->ncommits; i++) {
1672 struct got_object_qid *qid;
1673 uint8_t *sha1 = (uint8_t *)imsg.data +
1674 sizeof(*icommits) + i * SHA1_DIGEST_LENGTH;
1675 err = got_object_qid_alloc_partial(&qid);
1678 memcpy(qid->id->sha1, sha1, SHA1_DIGEST_LENGTH);
1679 SIMPLEQ_INSERT_TAIL(commit_ids, qid, entry);
1681 /* The last commit may contain a change. */
1682 if (i == icommits->ncommits - 1) {
1683 *changed_commit_id =
1684 got_object_id_dup(qid->id);
1685 if (*changed_commit_id == NULL) {
1686 err = got_error_from_errno(
1687 "got_object_id_dup");
1693 case GOT_IMSG_COMMIT:
1694 if (*changed_commit_id == NULL) {
1695 err = got_error(GOT_ERR_PRIVSEP_MSG);
1698 err = get_commit_from_imsg(changed_commit, &imsg,
1701 case GOT_IMSG_COMMIT_TRAVERSAL_DONE:
1705 err = got_error(GOT_ERR_PRIVSEP_MSG);
1715 got_object_id_queue_free(commit_ids);
1719 const struct got_error *
1720 got_privsep_send_commit_traversal_done(struct imsgbuf *ibuf)
1722 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_DONE, 0, 0, -1,
1724 return got_error_from_errno("imsg_compose TRAVERSAL_DONE");
1726 return flush_imsg(ibuf);
1729 const struct got_error *
1730 got_privsep_unveil_exec_helpers(void)
1732 const char *helpers[] = {
1733 GOT_PATH_PROG_READ_PACK,
1734 GOT_PATH_PROG_READ_OBJECT,
1735 GOT_PATH_PROG_READ_COMMIT,
1736 GOT_PATH_PROG_READ_TREE,
1737 GOT_PATH_PROG_READ_BLOB,
1738 GOT_PATH_PROG_READ_TAG,
1739 GOT_PATH_PROG_READ_GITCONFIG,
1743 for (i = 0; i < nitems(helpers); i++) {
1744 if (unveil(helpers[i], "x") == 0)
1746 return got_error_from_errno2("unveil", helpers[i]);
1753 got_privsep_exec_child(int imsg_fds[2], const char *path, const char *repo_path)
1755 if (close(imsg_fds[0]) != 0) {
1756 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
1760 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
1761 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
1764 if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
1765 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
1769 if (execl(path, path, repo_path, (char *)NULL) == -1) {
1770 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,