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_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);
153 if (imsg->hdr.len < IMSG_HEADER_SIZE + min_datalen)
154 return got_error(GOT_ERR_PRIVSEP_LEN);
156 if (imsg->hdr.type == GOT_IMSG_ERROR) {
157 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
158 return recv_imsg_error(imsg, datalen);
164 /* Attempt to send an error in an imsg. Complain on stderr as a last resort. */
166 got_privsep_send_error(struct imsgbuf *ibuf, const struct got_error *err)
168 const struct got_error *poll_err;
169 struct got_imsg_error ierr;
172 ierr.code = err->code;
173 if (err->code == GOT_ERR_ERRNO)
174 ierr.errno_code = errno;
177 ret = imsg_compose(ibuf, GOT_IMSG_ERROR, 0, 0, -1, &ierr, sizeof(ierr));
179 fprintf(stderr, "%s: error %d \"%s\": imsg_compose: %s\n",
180 getprogname(), err->code, err->msg, strerror(errno));
184 poll_err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
186 fprintf(stderr, "%s: error %d \"%s\": poll: %s\n",
187 getprogname(), err->code, err->msg, poll_err->msg);
191 ret = imsg_flush(ibuf);
193 fprintf(stderr, "%s: error %d \"%s\": imsg_flush: %s\n",
194 getprogname(), err->code, err->msg, strerror(errno));
199 static const struct got_error *
200 flush_imsg(struct imsgbuf *ibuf)
202 const struct got_error *err;
204 err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
208 if (imsg_flush(ibuf) == -1)
209 return got_error_from_errno("imsg_flush");
214 const struct got_error *
215 got_privsep_send_stop(int fd)
217 const struct got_error *err = NULL;
220 imsg_init(&ibuf, fd);
222 if (imsg_compose(&ibuf, GOT_IMSG_STOP, 0, 0, -1, NULL, 0) == -1)
223 return got_error_from_errno("imsg_compose STOP");
225 err = flush_imsg(&ibuf);
230 const struct got_error *
231 got_privsep_send_obj_req(struct imsgbuf *ibuf, int fd)
233 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_REQUEST, 0, 0, fd, NULL, 0)
235 return got_error_from_errno("imsg_compose OBJECT_REQUEST");
237 return flush_imsg(ibuf);
240 const struct got_error *
241 got_privsep_send_commit_req(struct imsgbuf *ibuf, int fd,
242 struct got_object_id *id, int pack_idx)
244 const struct got_error *err = NULL;
245 struct got_imsg_packed_object iobj, *iobjp;
248 if (id) { /* commit is packed */
250 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
258 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_REQUEST, 0, 0, fd, iobjp, len)
260 err = got_error_from_errno("imsg_compose COMMIT_REQUEST");
265 return flush_imsg(ibuf);
268 const struct got_error *
269 got_privsep_send_tree_req(struct imsgbuf *ibuf, int fd,
270 struct got_object_id *id, int pack_idx)
272 const struct got_error *err = NULL;
274 size_t len = id ? sizeof(struct got_imsg_packed_object) : 0;
276 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_REQUEST, 0, 0, len);
278 return got_error_from_errno("imsg_create TREE_REQUEST");
280 if (id) { /* tree is packed */
281 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
282 err = got_error_from_errno("imsg_add TREE_ENTRY");
287 if (imsg_add(wbuf, &pack_idx, sizeof(pack_idx)) == -1) {
288 err = got_error_from_errno("imsg_add TREE_ENTRY");
295 imsg_close(ibuf, wbuf);
297 return flush_imsg(ibuf);
300 const struct got_error *
301 got_privsep_send_tag_req(struct imsgbuf *ibuf, int fd,
302 struct got_object_id *id, int pack_idx)
304 struct got_imsg_packed_object iobj, *iobjp;
307 if (id) { /* tag is packed */
309 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
317 if (imsg_compose(ibuf, GOT_IMSG_TAG_REQUEST, 0, 0, fd, iobjp, len)
319 return got_error_from_errno("imsg_compose TAG_REQUEST");
321 return flush_imsg(ibuf);
324 const struct got_error *
325 got_privsep_send_blob_req(struct imsgbuf *ibuf, int infd,
326 struct got_object_id *id, int pack_idx)
328 const struct got_error *err = NULL;
329 struct got_imsg_packed_object iobj, *iobjp;
332 if (id) { /* blob is packed */
334 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
342 if (imsg_compose(ibuf, GOT_IMSG_BLOB_REQUEST, 0, 0, infd, iobjp, len)
344 err = got_error_from_errno("imsg_compose BLOB_REQUEST");
349 return flush_imsg(ibuf);
352 const struct got_error *
353 got_privsep_send_blob_outfd(struct imsgbuf *ibuf, int outfd)
355 const struct got_error *err = NULL;
357 if (imsg_compose(ibuf, GOT_IMSG_BLOB_OUTFD, 0, 0, outfd, NULL, 0)
359 err = got_error_from_errno("imsg_compose BLOB_OUTFD");
364 return flush_imsg(ibuf);
367 const struct got_error *
368 got_privsep_send_tmpfd(struct imsgbuf *ibuf, int fd)
370 const struct got_error *err = NULL;
372 if (imsg_compose(ibuf, GOT_IMSG_TMPFD, 0, 0, fd, NULL, 0)
374 err = got_error_from_errno("imsg_compose TMPFD");
379 return flush_imsg(ibuf);
382 const struct got_error *
383 got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj)
385 struct got_imsg_object iobj;
387 memcpy(iobj.id, obj->id.sha1, sizeof(iobj.id));
388 iobj.type = obj->type;
389 iobj.flags = obj->flags;
390 iobj.hdrlen = obj->hdrlen;
391 iobj.size = obj->size;
392 if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
393 iobj.pack_offset = obj->pack_offset;
394 iobj.pack_idx = obj->pack_idx;
397 if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
399 return got_error_from_errno("imsg_compose OBJECT");
401 return flush_imsg(ibuf);
404 const struct got_error *
405 got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
406 struct imsgbuf *ibuf)
408 const struct got_error *err = NULL;
409 struct got_imsg_object *iobj;
410 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
412 if (datalen != sizeof(*iobj))
413 return got_error(GOT_ERR_PRIVSEP_LEN);
416 *obj = calloc(1, sizeof(**obj));
418 return got_error_from_errno("calloc");
420 memcpy((*obj)->id.sha1, iobj->id, SHA1_DIGEST_LENGTH);
421 (*obj)->type = iobj->type;
422 (*obj)->flags = iobj->flags;
423 (*obj)->hdrlen = iobj->hdrlen;
424 (*obj)->size = iobj->size;
425 /* path_packfile is handled by caller */
426 if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
427 (*obj)->pack_offset = iobj->pack_offset;
428 (*obj)->pack_idx = iobj->pack_idx;
434 const struct got_error *
435 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
437 const struct got_error *err = NULL;
439 const size_t min_datalen =
440 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
444 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
448 switch (imsg.hdr.type) {
449 case GOT_IMSG_OBJECT:
450 err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
453 err = got_error(GOT_ERR_PRIVSEP_MSG);
462 static const struct got_error *
463 send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
466 const struct got_error *err = NULL;
467 size_t offset, remain;
472 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
474 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
475 commit->logmsg + offset, n) == -1) {
476 err = got_error_from_errno("imsg_compose "
481 err = flush_imsg(ibuf);
492 const struct got_error *
493 got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
495 const struct got_error *err = NULL;
496 struct got_imsg_commit_object *icommit;
499 struct got_object_qid *qid;
500 size_t author_len = strlen(commit->author);
501 size_t committer_len = strlen(commit->committer);
502 size_t logmsg_len = strlen(commit->logmsg);
504 total = sizeof(*icommit) + author_len + committer_len +
505 commit->nparents * SHA1_DIGEST_LENGTH;
509 return got_error_from_errno("malloc");
511 icommit = (struct got_imsg_commit_object *)buf;
512 memcpy(icommit->tree_id, commit->tree_id->sha1,
513 sizeof(icommit->tree_id));
514 icommit->author_len = author_len;
515 icommit->author_time = commit->author_time;
516 icommit->author_gmtoff = commit->author_gmtoff;
517 icommit->committer_len = committer_len;
518 icommit->committer_time = commit->committer_time;
519 icommit->committer_gmtoff = commit->committer_gmtoff;
520 icommit->logmsg_len = logmsg_len;
521 icommit->nparents = commit->nparents;
523 len = sizeof(*icommit);
524 memcpy(buf + len, commit->author, author_len);
526 memcpy(buf + len, commit->committer, committer_len);
527 len += committer_len;
528 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
529 memcpy(buf + len, qid->id, SHA1_DIGEST_LENGTH);
530 len += SHA1_DIGEST_LENGTH;
533 if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
534 err = got_error_from_errno("imsg_compose COMMIT");
538 if (logmsg_len == 0 ||
539 logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
540 err = flush_imsg(ibuf);
544 err = send_commit_logmsg(ibuf, commit, logmsg_len);
550 const struct got_error *
551 got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
553 const struct got_error *err = NULL;
555 struct got_imsg_commit_object *icommit;
558 const size_t min_datalen =
559 MIN(sizeof(struct got_imsg_error),
560 sizeof(struct got_imsg_commit_object));
564 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
568 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
571 switch (imsg.hdr.type) {
572 case GOT_IMSG_COMMIT:
573 if (datalen < sizeof(*icommit)) {
574 err = got_error(GOT_ERR_PRIVSEP_LEN);
578 if (datalen != sizeof(*icommit) + icommit->author_len +
579 icommit->committer_len +
580 icommit->nparents * SHA1_DIGEST_LENGTH) {
581 err = got_error(GOT_ERR_PRIVSEP_LEN);
584 if (icommit->nparents < 0) {
585 err = got_error(GOT_ERR_PRIVSEP_LEN);
588 len += sizeof(*icommit);
590 *commit = got_object_commit_alloc_partial();
591 if (*commit == NULL) {
592 err = got_error_from_errno(
593 "got_object_commit_alloc_partial");
597 memcpy((*commit)->tree_id->sha1, icommit->tree_id,
599 (*commit)->author_time = icommit->author_time;
600 (*commit)->author_gmtoff = icommit->author_gmtoff;
601 (*commit)->committer_time = icommit->committer_time;
602 (*commit)->committer_gmtoff = icommit->committer_gmtoff;
604 if (icommit->author_len == 0) {
605 (*commit)->author = strdup("");
606 if ((*commit)->author == NULL) {
607 err = got_error_from_errno("strdup");
611 (*commit)->author = malloc(icommit->author_len + 1);
612 if ((*commit)->author == NULL) {
613 err = got_error_from_errno("malloc");
616 memcpy((*commit)->author, imsg.data + len,
617 icommit->author_len);
618 (*commit)->author[icommit->author_len] = '\0';
620 len += icommit->author_len;
622 if (icommit->committer_len == 0) {
623 (*commit)->committer = strdup("");
624 if ((*commit)->committer == NULL) {
625 err = got_error_from_errno("strdup");
629 (*commit)->committer =
630 malloc(icommit->committer_len + 1);
631 if ((*commit)->committer == NULL) {
632 err = got_error_from_errno("malloc");
635 memcpy((*commit)->committer, imsg.data + len,
636 icommit->committer_len);
637 (*commit)->committer[icommit->committer_len] = '\0';
639 len += icommit->committer_len;
641 if (icommit->logmsg_len == 0) {
642 (*commit)->logmsg = strdup("");
643 if ((*commit)->logmsg == NULL) {
644 err = got_error_from_errno("strdup");
648 size_t offset = 0, remain = icommit->logmsg_len;
650 (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
651 if ((*commit)->logmsg == NULL) {
652 err = got_error_from_errno("malloc");
656 struct imsg imsg_log;
657 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
660 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
664 if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG)
665 return got_error(GOT_ERR_PRIVSEP_MSG);
667 memcpy((*commit)->logmsg + offset,
669 imsg_free(&imsg_log);
673 (*commit)->logmsg[icommit->logmsg_len] = '\0';
676 for (i = 0; i < icommit->nparents; i++) {
677 struct got_object_qid *qid;
679 err = got_object_qid_alloc_partial(&qid);
682 memcpy(qid->id, imsg.data + len +
683 i * SHA1_DIGEST_LENGTH, sizeof(*qid->id));
684 SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
685 (*commit)->nparents++;
689 err = got_error(GOT_ERR_PRIVSEP_MSG);
698 const struct got_error *
699 got_privsep_send_tree(struct imsgbuf *ibuf, struct got_pathlist_head *entries,
702 const struct got_error *err = NULL;
703 struct got_imsg_tree_object itree;
704 struct got_pathlist_entry *pe;
706 int nimsg; /* number of imsg queued in ibuf */
708 itree.nentries = nentries;
709 if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
711 return got_error_from_errno("imsg_compose TREE");
713 totlen = sizeof(itree);
715 TAILQ_FOREACH(pe, entries, entry) {
716 const char *name = pe->path;
717 struct got_parsed_tree_entry *pte = pe->data;
719 size_t namelen = strlen(name);
720 size_t len = sizeof(struct got_imsg_tree_entry) + namelen;
722 if (len > MAX_IMSGSIZE)
723 return got_error(GOT_ERR_NO_SPACE);
726 if (totlen + len >= MAX_IMSGSIZE - (IMSG_HEADER_SIZE * nimsg)) {
727 err = flush_imsg(ibuf);
733 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_ENTRY, 0, 0, len);
735 return got_error_from_errno("imsg_create TREE_ENTRY");
737 /* Keep in sync with struct got_imsg_tree_object definition! */
738 if (imsg_add(wbuf, pte->id, SHA1_DIGEST_LENGTH) == -1) {
739 err = got_error_from_errno("imsg_add TREE_ENTRY");
743 if (imsg_add(wbuf, &pte->mode, sizeof(pte->mode)) == -1) {
744 err = got_error_from_errno("imsg_add TREE_ENTRY");
749 if (imsg_add(wbuf, name, namelen) == -1) {
750 err = got_error_from_errno("imsg_add TREE_ENTRY");
756 imsg_close(ibuf, wbuf);
761 return flush_imsg(ibuf);
764 const struct got_error *
765 got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
767 const struct got_error *err = NULL;
768 const size_t min_datalen =
769 MIN(sizeof(struct got_imsg_error),
770 sizeof(struct got_imsg_tree_object));
771 struct got_imsg_tree_object *itree;
776 err = read_imsg(ibuf);
784 struct got_imsg_tree_entry *ite;
785 struct got_tree_entry *te = NULL;
787 n = imsg_get(ibuf, &imsg);
789 if (*tree && (*tree)->nentries != nentries)
794 if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen)
795 return got_error(GOT_ERR_PRIVSEP_LEN);
797 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
799 switch (imsg.hdr.type) {
801 err = recv_imsg_error(&imsg, datalen);
804 /* This message should only appear once. */
806 err = got_error(GOT_ERR_PRIVSEP_MSG);
809 if (datalen != sizeof(*itree)) {
810 err = got_error(GOT_ERR_PRIVSEP_LEN);
814 *tree = malloc(sizeof(**tree));
816 err = got_error_from_errno("malloc");
819 (*tree)->entries = calloc(itree->nentries,
820 sizeof(struct got_tree_entry));
821 if ((*tree)->entries == NULL) {
822 err = got_error_from_errno("malloc");
825 (*tree)->nentries = itree->nentries;
828 case GOT_IMSG_TREE_ENTRY:
829 /* This message should be preceeded by GOT_IMSG_TREE. */
831 err = got_error(GOT_ERR_PRIVSEP_MSG);
834 if (datalen < sizeof(*ite) || datalen > MAX_IMSGSIZE) {
835 err = got_error(GOT_ERR_PRIVSEP_LEN);
839 /* Remaining data contains the entry's name. */
840 datalen -= sizeof(*ite);
841 if (datalen == 0 || datalen > MAX_IMSGSIZE) {
842 err = got_error(GOT_ERR_PRIVSEP_LEN);
847 if (datalen + 1 > sizeof(te->name)) {
848 err = got_error(GOT_ERR_NO_SPACE);
851 te = &(*tree)->entries[nentries];
852 memcpy(te->name, imsg.data + sizeof(*ite), datalen);
853 te->name[datalen] = '\0';
855 memcpy(te->id.sha1, ite->id, SHA1_DIGEST_LENGTH);
856 te->mode = ite->mode;
861 err = got_error(GOT_ERR_PRIVSEP_MSG);
868 if (*tree && (*tree)->nentries != nentries) {
870 err = got_error(GOT_ERR_PRIVSEP_LEN);
871 got_object_tree_close(*tree);
878 const struct got_error *
879 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
882 struct got_imsg_blob iblob;
885 iblob.hdrlen = hdrlen;
890 if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
891 return got_error(GOT_ERR_NO_SPACE);
893 buf = malloc(sizeof(iblob) + size);
895 return got_error_from_errno("malloc");
897 memcpy(buf, &iblob, sizeof(iblob));
898 memcpy(buf + sizeof(iblob), data, size);
899 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
900 sizeof(iblob) + size) == -1) {
902 return got_error_from_errno("imsg_compose BLOB");
906 /* Data has already been written to file descriptor. */
907 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
908 sizeof(iblob)) == -1)
909 return got_error_from_errno("imsg_compose BLOB");
913 return flush_imsg(ibuf);
916 const struct got_error *
917 got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
918 struct imsgbuf *ibuf)
920 const struct got_error *err = NULL;
922 struct got_imsg_blob *iblob;
927 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
931 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
933 switch (imsg.hdr.type) {
935 if (datalen < sizeof(*iblob)) {
936 err = got_error(GOT_ERR_PRIVSEP_LEN);
941 *hdrlen = iblob->hdrlen;
943 if (datalen == sizeof(*iblob)) {
944 /* Data has been written to file descriptor. */
948 if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX) {
949 err = got_error(GOT_ERR_PRIVSEP_LEN);
953 *outbuf = malloc(*size);
954 if (*outbuf == NULL) {
955 err = got_error_from_errno("malloc");
958 memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
961 err = got_error(GOT_ERR_PRIVSEP_MSG);
970 static const struct got_error *
971 send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
973 const struct got_error *err = NULL;
974 size_t offset, remain;
979 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
981 if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
982 tag->tagmsg + offset, n) == -1) {
983 err = got_error_from_errno("imsg_compose TAG_TAGMSG");
987 err = flush_imsg(ibuf);
998 const struct got_error *
999 got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
1001 const struct got_error *err = NULL;
1002 struct got_imsg_tag_object *itag;
1005 size_t tag_len = strlen(tag->tag);
1006 size_t tagger_len = strlen(tag->tagger);
1007 size_t tagmsg_len = strlen(tag->tagmsg);
1009 total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
1011 buf = malloc(total);
1013 return got_error_from_errno("malloc");
1015 itag = (struct got_imsg_tag_object *)buf;
1016 memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
1017 itag->obj_type = tag->obj_type;
1018 itag->tag_len = tag_len;
1019 itag->tagger_len = tagger_len;
1020 itag->tagger_time = tag->tagger_time;
1021 itag->tagger_gmtoff = tag->tagger_gmtoff;
1022 itag->tagmsg_len = tagmsg_len;
1024 len = sizeof(*itag);
1025 memcpy(buf + len, tag->tag, tag_len);
1027 memcpy(buf + len, tag->tagger, tagger_len);
1030 if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
1031 err = got_error_from_errno("imsg_compose TAG");
1035 if (tagmsg_len == 0 ||
1036 tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1037 err = flush_imsg(ibuf);
1041 err = send_tagmsg(ibuf, tag, tagmsg_len);
1047 const struct got_error *
1048 got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
1050 const struct got_error *err = NULL;
1052 struct got_imsg_tag_object *itag;
1053 size_t len, datalen;
1054 const size_t min_datalen =
1055 MIN(sizeof(struct got_imsg_error),
1056 sizeof(struct got_imsg_tag_object));
1060 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1064 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1067 switch (imsg.hdr.type) {
1069 if (datalen < sizeof(*itag)) {
1070 err = got_error(GOT_ERR_PRIVSEP_LEN);
1074 if (datalen != sizeof(*itag) + itag->tag_len +
1076 err = got_error(GOT_ERR_PRIVSEP_LEN);
1079 len += sizeof(*itag);
1081 *tag = calloc(1, sizeof(**tag));
1083 err = got_error_from_errno("calloc");
1087 memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
1089 if (itag->tag_len == 0) {
1090 (*tag)->tag = strdup("");
1091 if ((*tag)->tag == NULL) {
1092 err = got_error_from_errno("strdup");
1096 (*tag)->tag = malloc(itag->tag_len + 1);
1097 if ((*tag)->tag == NULL) {
1098 err = got_error_from_errno("malloc");
1101 memcpy((*tag)->tag, imsg.data + len,
1103 (*tag)->tag[itag->tag_len] = '\0';
1105 len += itag->tag_len;
1107 (*tag)->obj_type = itag->obj_type;
1108 (*tag)->tagger_time = itag->tagger_time;
1109 (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1111 if (itag->tagger_len == 0) {
1112 (*tag)->tagger = strdup("");
1113 if ((*tag)->tagger == NULL) {
1114 err = got_error_from_errno("strdup");
1118 (*tag)->tagger = malloc(itag->tagger_len + 1);
1119 if ((*tag)->tagger == NULL) {
1120 err = got_error_from_errno("malloc");
1123 memcpy((*tag)->tagger, imsg.data + len,
1125 (*tag)->tagger[itag->tagger_len] = '\0';
1127 len += itag->tagger_len;
1129 if (itag->tagmsg_len == 0) {
1130 (*tag)->tagmsg = strdup("");
1131 if ((*tag)->tagmsg == NULL) {
1132 err = got_error_from_errno("strdup");
1136 size_t offset = 0, remain = itag->tagmsg_len;
1138 (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1139 if ((*tag)->tagmsg == NULL) {
1140 err = got_error_from_errno("malloc");
1143 while (remain > 0) {
1144 struct imsg imsg_log;
1145 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1148 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1152 if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1153 return got_error(GOT_ERR_PRIVSEP_MSG);
1155 memcpy((*tag)->tagmsg + offset, imsg_log.data,
1157 imsg_free(&imsg_log);
1161 (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1166 err = got_error(GOT_ERR_PRIVSEP_MSG);
1175 const struct got_error *
1176 got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1177 struct got_packidx *packidx)
1179 const struct got_error *err = NULL;
1180 struct got_imsg_packidx ipackidx;
1181 struct got_imsg_pack ipack;
1184 ipackidx.len = packidx->len;
1185 fd = dup(packidx->fd);
1187 return got_error_from_errno("dup");
1189 if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
1190 sizeof(ipackidx)) == -1) {
1191 err = got_error_from_errno("imsg_compose PACKIDX");
1196 if (strlcpy(ipack.path_packfile, pack->path_packfile,
1197 sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
1198 return got_error(GOT_ERR_NO_SPACE);
1199 ipack.filesize = pack->filesize;
1203 return got_error_from_errno("dup");
1205 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
1207 err = got_error_from_errno("imsg_compose PACK");
1212 return flush_imsg(ibuf);
1215 const struct got_error *
1216 got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
1217 struct got_object_id *id)
1219 struct got_imsg_packed_object iobj;
1222 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
1224 if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
1225 &iobj, sizeof(iobj)) == -1)
1226 return got_error_from_errno("imsg_compose "
1227 "PACKED_OBJECT_REQUEST");
1229 return flush_imsg(ibuf);
1232 const struct got_error *
1233 got_privsep_send_gitconfig_parse_req(struct imsgbuf *ibuf, int fd)
1235 const struct got_error *err = NULL;
1237 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_PARSE_REQUEST, 0, 0, fd,
1239 err = got_error_from_errno("imsg_compose "
1240 "GITCONFIG_PARSE_REQUEST");
1245 return flush_imsg(ibuf);
1248 const struct got_error *
1249 got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *ibuf)
1251 if (imsg_compose(ibuf,
1252 GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST, 0, 0, -1,
1254 return got_error_from_errno("imsg_compose "
1255 "GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST");
1257 return flush_imsg(ibuf);
1260 const struct got_error *
1261 got_privsep_send_gitconfig_author_name_req(struct imsgbuf *ibuf)
1263 if (imsg_compose(ibuf,
1264 GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST, 0, 0, -1, NULL, 0) == -1)
1265 return got_error_from_errno("imsg_compose "
1266 "GITCONFIG_AUTHOR_NAME_REQUEST");
1268 return flush_imsg(ibuf);
1271 const struct got_error *
1272 got_privsep_send_gitconfig_author_email_req(struct imsgbuf *ibuf)
1274 if (imsg_compose(ibuf,
1275 GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST, 0, 0, -1, NULL, 0) == -1)
1276 return got_error_from_errno("imsg_compose "
1277 "GITCONFIG_AUTHOR_EMAIL_REQUEST");
1279 return flush_imsg(ibuf);
1282 const struct got_error *
1283 got_privsep_send_gitconfig_remotes_req(struct imsgbuf *ibuf)
1285 if (imsg_compose(ibuf,
1286 GOT_IMSG_GITCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
1287 return got_error_from_errno("imsg_compose "
1288 "GITCONFIG_REMOTE_REQUEST");
1290 return flush_imsg(ibuf);
1293 const struct got_error *
1294 got_privsep_send_gitconfig_str(struct imsgbuf *ibuf, const char *value)
1296 size_t len = value ? strlen(value) + 1 : 0;
1298 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_STR_VAL, 0, 0, -1,
1300 return got_error_from_errno("imsg_compose GITCONFIG_STR_VAL");
1302 return flush_imsg(ibuf);
1305 const struct got_error *
1306 got_privsep_recv_gitconfig_str(char **str, struct imsgbuf *ibuf)
1308 const struct got_error *err = NULL;
1311 const size_t min_datalen = 0;
1315 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1318 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1320 switch (imsg.hdr.type) {
1321 case GOT_IMSG_GITCONFIG_STR_VAL:
1324 *str = malloc(datalen);
1326 err = got_error_from_errno("malloc");
1329 if (strlcpy(*str, imsg.data, datalen) >= datalen)
1330 err = got_error(GOT_ERR_NO_SPACE);
1333 err = got_error(GOT_ERR_PRIVSEP_MSG);
1341 const struct got_error *
1342 got_privsep_send_gitconfig_int(struct imsgbuf *ibuf, int value)
1344 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_INT_VAL, 0, 0, -1,
1345 &value, sizeof(value)) == -1)
1346 return got_error_from_errno("imsg_compose GITCONFIG_INT_VAL");
1348 return flush_imsg(ibuf);
1351 const struct got_error *
1352 got_privsep_recv_gitconfig_int(int *val, struct imsgbuf *ibuf)
1354 const struct got_error *err = NULL;
1357 const size_t min_datalen =
1358 MIN(sizeof(struct got_imsg_error), sizeof(int));
1362 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1365 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1367 switch (imsg.hdr.type) {
1368 case GOT_IMSG_GITCONFIG_INT_VAL:
1369 if (datalen != sizeof(*val)) {
1370 err = got_error(GOT_ERR_PRIVSEP_LEN);
1373 memcpy(val, imsg.data, sizeof(*val));
1376 err = got_error(GOT_ERR_PRIVSEP_MSG);
1384 const struct got_error *
1385 got_privsep_send_gitconfig_remotes(struct imsgbuf *ibuf,
1386 struct got_remote_repo *remotes, int nremotes)
1388 const struct got_error *err = NULL;
1389 struct got_imsg_remotes iremotes;
1392 iremotes.nremotes = nremotes;
1393 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_REMOTES, 0, 0, -1,
1394 &iremotes, sizeof(iremotes)) == -1)
1395 return got_error_from_errno("imsg_compose GITCONFIG_REMOTES");
1397 err = flush_imsg(ibuf);
1402 for (i = 0; i < nremotes; i++) {
1403 struct got_imsg_remote iremote;
1404 size_t len = sizeof(iremote);
1407 iremote.name_len = strlen(remotes[i].name);
1408 len += iremote.name_len;
1409 iremote.url_len = strlen(remotes[i].url);
1410 len += iremote.url_len;
1412 wbuf = imsg_create(ibuf, GOT_IMSG_GITCONFIG_REMOTE, 0, 0, len);
1414 return got_error_from_errno(
1415 "imsg_create GITCONFIG_REMOTE");
1417 if (imsg_add(wbuf, &iremote, sizeof(iremote)) == -1) {
1418 err = got_error_from_errno(
1419 "imsg_add GIITCONFIG_REMOTE");
1424 if (imsg_add(wbuf, remotes[i].name, iremote.name_len) == -1) {
1425 err = got_error_from_errno(
1426 "imsg_add GIITCONFIG_REMOTE");
1430 if (imsg_add(wbuf, remotes[i].url, iremote.url_len) == -1) {
1431 err = got_error_from_errno(
1432 "imsg_add GIITCONFIG_REMOTE");
1438 imsg_close(ibuf, wbuf);
1439 err = flush_imsg(ibuf);
1447 const struct got_error *
1448 got_privsep_recv_gitconfig_remotes(struct got_remote_repo **remotes,
1449 int *nremotes, struct imsgbuf *ibuf)
1451 const struct got_error *err = NULL;
1454 struct got_imsg_remotes iremotes;
1455 struct got_imsg_remote iremote;
1460 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremotes));
1463 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1465 switch (imsg.hdr.type) {
1466 case GOT_IMSG_GITCONFIG_REMOTES:
1467 if (datalen != sizeof(iremotes)) {
1468 err = got_error(GOT_ERR_PRIVSEP_LEN);
1471 memcpy(&iremotes, imsg.data, sizeof(iremotes));
1472 if (iremotes.nremotes == 0) {
1478 err = got_error(GOT_ERR_PRIVSEP_MSG);
1484 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(iremote));
1485 if (*remotes == NULL)
1486 return got_error_from_errno("recallocarray");
1488 while (*nremotes < iremotes.nremotes) {
1489 struct got_remote_repo *remote;
1491 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremote));
1494 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1496 switch (imsg.hdr.type) {
1497 case GOT_IMSG_GITCONFIG_REMOTE:
1498 remote = &(*remotes)[*nremotes];
1499 if (datalen < sizeof(iremote)) {
1500 err = got_error(GOT_ERR_PRIVSEP_LEN);
1503 memcpy(&iremote, imsg.data, sizeof(iremote));
1504 if (iremote.name_len == 0 || iremote.url_len == 0 ||
1505 (sizeof(iremote) + iremote.name_len +
1506 iremote.url_len) > datalen) {
1507 err = got_error(GOT_ERR_PRIVSEP_LEN);
1510 remote->name = strndup(imsg.data + sizeof(iremote),
1512 if (remote->name == NULL) {
1513 err = got_error_from_errno("strndup");
1516 remote->url = strndup(imsg.data + sizeof(iremote) +
1517 iremote.name_len, iremote.url_len);
1518 if (remote->url == NULL) {
1519 err = got_error_from_errno("strndup");
1526 err = got_error(GOT_ERR_PRIVSEP_MSG);
1537 for (i = 0; i < *nremotes; i++) {
1538 free((*remotes)[i].name);
1539 free((*remotes)[i].url);
1548 const struct got_error *
1549 got_privsep_unveil_exec_helpers(void)
1551 const char *helpers[] = {
1552 GOT_PATH_PROG_READ_PACK,
1553 GOT_PATH_PROG_READ_OBJECT,
1554 GOT_PATH_PROG_READ_COMMIT,
1555 GOT_PATH_PROG_READ_TREE,
1556 GOT_PATH_PROG_READ_BLOB,
1557 GOT_PATH_PROG_READ_TAG,
1558 GOT_PATH_PROG_READ_GITCONFIG,
1562 for (i = 0; i < nitems(helpers); i++) {
1563 if (unveil(helpers[i], "x") == 0)
1565 return got_error_from_errno2("unveil", helpers[i]);
1572 got_privsep_exec_child(int imsg_fds[2], const char *path, const char *repo_path)
1574 if (close(imsg_fds[0]) != 0) {
1575 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
1579 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
1580 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
1583 if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
1584 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
1588 if (execl(path, path, repo_path, (char *)NULL) == -1) {
1589 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,