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);
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 const struct got_error *
553 got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
555 const struct got_error *err = NULL;
557 struct got_imsg_commit_object *icommit;
560 const size_t min_datalen =
561 MIN(sizeof(struct got_imsg_error),
562 sizeof(struct got_imsg_commit_object));
566 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
570 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
573 switch (imsg.hdr.type) {
574 case GOT_IMSG_COMMIT:
575 if (datalen < sizeof(*icommit)) {
576 err = got_error(GOT_ERR_PRIVSEP_LEN);
580 if (datalen != sizeof(*icommit) + icommit->author_len +
581 icommit->committer_len +
582 icommit->nparents * SHA1_DIGEST_LENGTH) {
583 err = got_error(GOT_ERR_PRIVSEP_LEN);
586 if (icommit->nparents < 0) {
587 err = got_error(GOT_ERR_PRIVSEP_LEN);
590 len += sizeof(*icommit);
592 *commit = got_object_commit_alloc_partial();
593 if (*commit == NULL) {
594 err = got_error_from_errno(
595 "got_object_commit_alloc_partial");
599 memcpy((*commit)->tree_id->sha1, icommit->tree_id,
601 (*commit)->author_time = icommit->author_time;
602 (*commit)->author_gmtoff = icommit->author_gmtoff;
603 (*commit)->committer_time = icommit->committer_time;
604 (*commit)->committer_gmtoff = icommit->committer_gmtoff;
606 if (icommit->author_len == 0) {
607 (*commit)->author = strdup("");
608 if ((*commit)->author == NULL) {
609 err = got_error_from_errno("strdup");
613 (*commit)->author = malloc(icommit->author_len + 1);
614 if ((*commit)->author == NULL) {
615 err = got_error_from_errno("malloc");
618 memcpy((*commit)->author, imsg.data + len,
619 icommit->author_len);
620 (*commit)->author[icommit->author_len] = '\0';
622 len += icommit->author_len;
624 if (icommit->committer_len == 0) {
625 (*commit)->committer = strdup("");
626 if ((*commit)->committer == NULL) {
627 err = got_error_from_errno("strdup");
631 (*commit)->committer =
632 malloc(icommit->committer_len + 1);
633 if ((*commit)->committer == NULL) {
634 err = got_error_from_errno("malloc");
637 memcpy((*commit)->committer, imsg.data + len,
638 icommit->committer_len);
639 (*commit)->committer[icommit->committer_len] = '\0';
641 len += icommit->committer_len;
643 if (icommit->logmsg_len == 0) {
644 (*commit)->logmsg = strdup("");
645 if ((*commit)->logmsg == NULL) {
646 err = got_error_from_errno("strdup");
650 size_t offset = 0, remain = icommit->logmsg_len;
652 (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
653 if ((*commit)->logmsg == NULL) {
654 err = got_error_from_errno("malloc");
658 struct imsg imsg_log;
659 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
662 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
666 if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG)
667 return got_error(GOT_ERR_PRIVSEP_MSG);
669 memcpy((*commit)->logmsg + offset,
671 imsg_free(&imsg_log);
675 (*commit)->logmsg[icommit->logmsg_len] = '\0';
678 for (i = 0; i < icommit->nparents; i++) {
679 struct got_object_qid *qid;
681 err = got_object_qid_alloc_partial(&qid);
684 memcpy(qid->id, imsg.data + len +
685 i * SHA1_DIGEST_LENGTH, sizeof(*qid->id));
686 SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
687 (*commit)->nparents++;
691 err = got_error(GOT_ERR_PRIVSEP_MSG);
700 const struct got_error *
701 got_privsep_send_tree(struct imsgbuf *ibuf, struct got_pathlist_head *entries,
704 const struct got_error *err = NULL;
705 struct got_imsg_tree_object itree;
706 struct got_pathlist_entry *pe;
708 int nimsg; /* number of imsg queued in ibuf */
710 itree.nentries = nentries;
711 if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
713 return got_error_from_errno("imsg_compose TREE");
715 totlen = sizeof(itree);
717 TAILQ_FOREACH(pe, entries, entry) {
718 const char *name = pe->path;
719 struct got_parsed_tree_entry *pte = pe->data;
721 size_t namelen = strlen(name);
722 size_t len = sizeof(struct got_imsg_tree_entry) + namelen;
724 if (len > MAX_IMSGSIZE)
725 return got_error(GOT_ERR_NO_SPACE);
728 if (totlen + len >= MAX_IMSGSIZE - (IMSG_HEADER_SIZE * nimsg)) {
729 err = flush_imsg(ibuf);
735 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_ENTRY, 0, 0, len);
737 return got_error_from_errno("imsg_create TREE_ENTRY");
739 /* Keep in sync with struct got_imsg_tree_object definition! */
740 if (imsg_add(wbuf, pte->id, SHA1_DIGEST_LENGTH) == -1) {
741 err = got_error_from_errno("imsg_add TREE_ENTRY");
745 if (imsg_add(wbuf, &pte->mode, sizeof(pte->mode)) == -1) {
746 err = got_error_from_errno("imsg_add TREE_ENTRY");
751 if (imsg_add(wbuf, name, namelen) == -1) {
752 err = got_error_from_errno("imsg_add TREE_ENTRY");
758 imsg_close(ibuf, wbuf);
763 return flush_imsg(ibuf);
766 const struct got_error *
767 got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
769 const struct got_error *err = NULL;
770 const size_t min_datalen =
771 MIN(sizeof(struct got_imsg_error),
772 sizeof(struct got_imsg_tree_object));
773 struct got_imsg_tree_object *itree;
778 err = read_imsg(ibuf);
786 struct got_imsg_tree_entry *ite;
787 struct got_tree_entry *te = NULL;
789 n = imsg_get(ibuf, &imsg);
791 if (*tree && (*tree)->nentries != nentries)
796 if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen)
797 return got_error(GOT_ERR_PRIVSEP_LEN);
799 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
801 switch (imsg.hdr.type) {
803 err = recv_imsg_error(&imsg, datalen);
806 /* This message should only appear once. */
808 err = got_error(GOT_ERR_PRIVSEP_MSG);
811 if (datalen != sizeof(*itree)) {
812 err = got_error(GOT_ERR_PRIVSEP_LEN);
816 *tree = malloc(sizeof(**tree));
818 err = got_error_from_errno("malloc");
821 (*tree)->entries = calloc(itree->nentries,
822 sizeof(struct got_tree_entry));
823 if ((*tree)->entries == NULL) {
824 err = got_error_from_errno("malloc");
827 (*tree)->nentries = itree->nentries;
830 case GOT_IMSG_TREE_ENTRY:
831 /* This message should be preceeded by GOT_IMSG_TREE. */
833 err = got_error(GOT_ERR_PRIVSEP_MSG);
836 if (datalen < sizeof(*ite) || datalen > MAX_IMSGSIZE) {
837 err = got_error(GOT_ERR_PRIVSEP_LEN);
841 /* Remaining data contains the entry's name. */
842 datalen -= sizeof(*ite);
843 if (datalen == 0 || datalen > MAX_IMSGSIZE) {
844 err = got_error(GOT_ERR_PRIVSEP_LEN);
849 if (datalen + 1 > sizeof(te->name)) {
850 err = got_error(GOT_ERR_NO_SPACE);
853 te = &(*tree)->entries[nentries];
854 memcpy(te->name, imsg.data + sizeof(*ite), datalen);
855 te->name[datalen] = '\0';
857 memcpy(te->id.sha1, ite->id, SHA1_DIGEST_LENGTH);
858 te->mode = ite->mode;
863 err = got_error(GOT_ERR_PRIVSEP_MSG);
870 if (*tree && (*tree)->nentries != nentries) {
872 err = got_error(GOT_ERR_PRIVSEP_LEN);
873 got_object_tree_close(*tree);
880 const struct got_error *
881 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
884 struct got_imsg_blob iblob;
887 iblob.hdrlen = hdrlen;
892 if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
893 return got_error(GOT_ERR_NO_SPACE);
895 buf = malloc(sizeof(iblob) + size);
897 return got_error_from_errno("malloc");
899 memcpy(buf, &iblob, sizeof(iblob));
900 memcpy(buf + sizeof(iblob), data, size);
901 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
902 sizeof(iblob) + size) == -1) {
904 return got_error_from_errno("imsg_compose BLOB");
908 /* Data has already been written to file descriptor. */
909 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
910 sizeof(iblob)) == -1)
911 return got_error_from_errno("imsg_compose BLOB");
915 return flush_imsg(ibuf);
918 const struct got_error *
919 got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
920 struct imsgbuf *ibuf)
922 const struct got_error *err = NULL;
924 struct got_imsg_blob *iblob;
929 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
933 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
935 switch (imsg.hdr.type) {
937 if (datalen < sizeof(*iblob)) {
938 err = got_error(GOT_ERR_PRIVSEP_LEN);
943 *hdrlen = iblob->hdrlen;
945 if (datalen == sizeof(*iblob)) {
946 /* Data has been written to file descriptor. */
950 if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX) {
951 err = got_error(GOT_ERR_PRIVSEP_LEN);
955 *outbuf = malloc(*size);
956 if (*outbuf == NULL) {
957 err = got_error_from_errno("malloc");
960 memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
963 err = got_error(GOT_ERR_PRIVSEP_MSG);
972 static const struct got_error *
973 send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
975 const struct got_error *err = NULL;
976 size_t offset, remain;
981 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
983 if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
984 tag->tagmsg + offset, n) == -1) {
985 err = got_error_from_errno("imsg_compose TAG_TAGMSG");
989 err = flush_imsg(ibuf);
1000 const struct got_error *
1001 got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
1003 const struct got_error *err = NULL;
1004 struct got_imsg_tag_object *itag;
1007 size_t tag_len = strlen(tag->tag);
1008 size_t tagger_len = strlen(tag->tagger);
1009 size_t tagmsg_len = strlen(tag->tagmsg);
1011 total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
1013 buf = malloc(total);
1015 return got_error_from_errno("malloc");
1017 itag = (struct got_imsg_tag_object *)buf;
1018 memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
1019 itag->obj_type = tag->obj_type;
1020 itag->tag_len = tag_len;
1021 itag->tagger_len = tagger_len;
1022 itag->tagger_time = tag->tagger_time;
1023 itag->tagger_gmtoff = tag->tagger_gmtoff;
1024 itag->tagmsg_len = tagmsg_len;
1026 len = sizeof(*itag);
1027 memcpy(buf + len, tag->tag, tag_len);
1029 memcpy(buf + len, tag->tagger, tagger_len);
1032 if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
1033 err = got_error_from_errno("imsg_compose TAG");
1037 if (tagmsg_len == 0 ||
1038 tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1039 err = flush_imsg(ibuf);
1043 err = send_tagmsg(ibuf, tag, tagmsg_len);
1049 const struct got_error *
1050 got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
1052 const struct got_error *err = NULL;
1054 struct got_imsg_tag_object *itag;
1055 size_t len, datalen;
1056 const size_t min_datalen =
1057 MIN(sizeof(struct got_imsg_error),
1058 sizeof(struct got_imsg_tag_object));
1062 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1066 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1069 switch (imsg.hdr.type) {
1071 if (datalen < sizeof(*itag)) {
1072 err = got_error(GOT_ERR_PRIVSEP_LEN);
1076 if (datalen != sizeof(*itag) + itag->tag_len +
1078 err = got_error(GOT_ERR_PRIVSEP_LEN);
1081 len += sizeof(*itag);
1083 *tag = calloc(1, sizeof(**tag));
1085 err = got_error_from_errno("calloc");
1089 memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
1091 if (itag->tag_len == 0) {
1092 (*tag)->tag = strdup("");
1093 if ((*tag)->tag == NULL) {
1094 err = got_error_from_errno("strdup");
1098 (*tag)->tag = malloc(itag->tag_len + 1);
1099 if ((*tag)->tag == NULL) {
1100 err = got_error_from_errno("malloc");
1103 memcpy((*tag)->tag, imsg.data + len,
1105 (*tag)->tag[itag->tag_len] = '\0';
1107 len += itag->tag_len;
1109 (*tag)->obj_type = itag->obj_type;
1110 (*tag)->tagger_time = itag->tagger_time;
1111 (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1113 if (itag->tagger_len == 0) {
1114 (*tag)->tagger = strdup("");
1115 if ((*tag)->tagger == NULL) {
1116 err = got_error_from_errno("strdup");
1120 (*tag)->tagger = malloc(itag->tagger_len + 1);
1121 if ((*tag)->tagger == NULL) {
1122 err = got_error_from_errno("malloc");
1125 memcpy((*tag)->tagger, imsg.data + len,
1127 (*tag)->tagger[itag->tagger_len] = '\0';
1129 len += itag->tagger_len;
1131 if (itag->tagmsg_len == 0) {
1132 (*tag)->tagmsg = strdup("");
1133 if ((*tag)->tagmsg == NULL) {
1134 err = got_error_from_errno("strdup");
1138 size_t offset = 0, remain = itag->tagmsg_len;
1140 (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1141 if ((*tag)->tagmsg == NULL) {
1142 err = got_error_from_errno("malloc");
1145 while (remain > 0) {
1146 struct imsg imsg_log;
1147 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1150 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1154 if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1155 return got_error(GOT_ERR_PRIVSEP_MSG);
1157 memcpy((*tag)->tagmsg + offset, imsg_log.data,
1159 imsg_free(&imsg_log);
1163 (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1168 err = got_error(GOT_ERR_PRIVSEP_MSG);
1177 const struct got_error *
1178 got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1179 struct got_packidx *packidx)
1181 const struct got_error *err = NULL;
1182 struct got_imsg_packidx ipackidx;
1183 struct got_imsg_pack ipack;
1186 ipackidx.len = packidx->len;
1187 fd = dup(packidx->fd);
1189 return got_error_from_errno("dup");
1191 if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
1192 sizeof(ipackidx)) == -1) {
1193 err = got_error_from_errno("imsg_compose PACKIDX");
1198 if (strlcpy(ipack.path_packfile, pack->path_packfile,
1199 sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
1200 return got_error(GOT_ERR_NO_SPACE);
1201 ipack.filesize = pack->filesize;
1205 return got_error_from_errno("dup");
1207 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
1209 err = got_error_from_errno("imsg_compose PACK");
1214 return flush_imsg(ibuf);
1217 const struct got_error *
1218 got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
1219 struct got_object_id *id)
1221 struct got_imsg_packed_object iobj;
1224 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
1226 if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
1227 &iobj, sizeof(iobj)) == -1)
1228 return got_error_from_errno("imsg_compose "
1229 "PACKED_OBJECT_REQUEST");
1231 return flush_imsg(ibuf);
1234 const struct got_error *
1235 got_privsep_send_gitconfig_parse_req(struct imsgbuf *ibuf, int fd)
1237 const struct got_error *err = NULL;
1239 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_PARSE_REQUEST, 0, 0, fd,
1241 err = got_error_from_errno("imsg_compose "
1242 "GITCONFIG_PARSE_REQUEST");
1247 return flush_imsg(ibuf);
1250 const struct got_error *
1251 got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *ibuf)
1253 if (imsg_compose(ibuf,
1254 GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST, 0, 0, -1,
1256 return got_error_from_errno("imsg_compose "
1257 "GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST");
1259 return flush_imsg(ibuf);
1262 const struct got_error *
1263 got_privsep_send_gitconfig_author_name_req(struct imsgbuf *ibuf)
1265 if (imsg_compose(ibuf,
1266 GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST, 0, 0, -1, NULL, 0) == -1)
1267 return got_error_from_errno("imsg_compose "
1268 "GITCONFIG_AUTHOR_NAME_REQUEST");
1270 return flush_imsg(ibuf);
1273 const struct got_error *
1274 got_privsep_send_gitconfig_author_email_req(struct imsgbuf *ibuf)
1276 if (imsg_compose(ibuf,
1277 GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST, 0, 0, -1, NULL, 0) == -1)
1278 return got_error_from_errno("imsg_compose "
1279 "GITCONFIG_AUTHOR_EMAIL_REQUEST");
1281 return flush_imsg(ibuf);
1284 const struct got_error *
1285 got_privsep_send_gitconfig_remotes_req(struct imsgbuf *ibuf)
1287 if (imsg_compose(ibuf,
1288 GOT_IMSG_GITCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
1289 return got_error_from_errno("imsg_compose "
1290 "GITCONFIG_REMOTE_REQUEST");
1292 return flush_imsg(ibuf);
1295 const struct got_error *
1296 got_privsep_send_gitconfig_str(struct imsgbuf *ibuf, const char *value)
1298 size_t len = value ? strlen(value) + 1 : 0;
1300 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_STR_VAL, 0, 0, -1,
1302 return got_error_from_errno("imsg_compose GITCONFIG_STR_VAL");
1304 return flush_imsg(ibuf);
1307 const struct got_error *
1308 got_privsep_recv_gitconfig_str(char **str, struct imsgbuf *ibuf)
1310 const struct got_error *err = NULL;
1313 const size_t min_datalen = 0;
1317 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1320 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1322 switch (imsg.hdr.type) {
1323 case GOT_IMSG_GITCONFIG_STR_VAL:
1326 *str = malloc(datalen);
1328 err = got_error_from_errno("malloc");
1331 if (strlcpy(*str, imsg.data, datalen) >= datalen)
1332 err = got_error(GOT_ERR_NO_SPACE);
1335 err = got_error(GOT_ERR_PRIVSEP_MSG);
1343 const struct got_error *
1344 got_privsep_send_gitconfig_int(struct imsgbuf *ibuf, int value)
1346 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_INT_VAL, 0, 0, -1,
1347 &value, sizeof(value)) == -1)
1348 return got_error_from_errno("imsg_compose GITCONFIG_INT_VAL");
1350 return flush_imsg(ibuf);
1353 const struct got_error *
1354 got_privsep_recv_gitconfig_int(int *val, struct imsgbuf *ibuf)
1356 const struct got_error *err = NULL;
1359 const size_t min_datalen =
1360 MIN(sizeof(struct got_imsg_error), sizeof(int));
1364 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1367 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1369 switch (imsg.hdr.type) {
1370 case GOT_IMSG_GITCONFIG_INT_VAL:
1371 if (datalen != sizeof(*val)) {
1372 err = got_error(GOT_ERR_PRIVSEP_LEN);
1375 memcpy(val, imsg.data, sizeof(*val));
1378 err = got_error(GOT_ERR_PRIVSEP_MSG);
1386 const struct got_error *
1387 got_privsep_send_gitconfig_remotes(struct imsgbuf *ibuf,
1388 struct got_remote_repo *remotes, int nremotes)
1390 const struct got_error *err = NULL;
1391 struct got_imsg_remotes iremotes;
1394 iremotes.nremotes = nremotes;
1395 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_REMOTES, 0, 0, -1,
1396 &iremotes, sizeof(iremotes)) == -1)
1397 return got_error_from_errno("imsg_compose GITCONFIG_REMOTES");
1399 err = flush_imsg(ibuf);
1404 for (i = 0; i < nremotes; i++) {
1405 struct got_imsg_remote iremote;
1406 size_t len = sizeof(iremote);
1409 iremote.name_len = strlen(remotes[i].name);
1410 len += iremote.name_len;
1411 iremote.url_len = strlen(remotes[i].url);
1412 len += iremote.url_len;
1414 wbuf = imsg_create(ibuf, GOT_IMSG_GITCONFIG_REMOTE, 0, 0, len);
1416 return got_error_from_errno(
1417 "imsg_create GITCONFIG_REMOTE");
1419 if (imsg_add(wbuf, &iremote, sizeof(iremote)) == -1) {
1420 err = got_error_from_errno(
1421 "imsg_add GIITCONFIG_REMOTE");
1426 if (imsg_add(wbuf, remotes[i].name, iremote.name_len) == -1) {
1427 err = got_error_from_errno(
1428 "imsg_add GIITCONFIG_REMOTE");
1432 if (imsg_add(wbuf, remotes[i].url, iremote.url_len) == -1) {
1433 err = got_error_from_errno(
1434 "imsg_add GIITCONFIG_REMOTE");
1440 imsg_close(ibuf, wbuf);
1441 err = flush_imsg(ibuf);
1449 const struct got_error *
1450 got_privsep_recv_gitconfig_remotes(struct got_remote_repo **remotes,
1451 int *nremotes, struct imsgbuf *ibuf)
1453 const struct got_error *err = NULL;
1456 struct got_imsg_remotes iremotes;
1457 struct got_imsg_remote iremote;
1462 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremotes));
1465 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1467 switch (imsg.hdr.type) {
1468 case GOT_IMSG_GITCONFIG_REMOTES:
1469 if (datalen != sizeof(iremotes)) {
1470 err = got_error(GOT_ERR_PRIVSEP_LEN);
1473 memcpy(&iremotes, imsg.data, sizeof(iremotes));
1474 if (iremotes.nremotes == 0) {
1480 err = got_error(GOT_ERR_PRIVSEP_MSG);
1486 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(iremote));
1487 if (*remotes == NULL)
1488 return got_error_from_errno("recallocarray");
1490 while (*nremotes < iremotes.nremotes) {
1491 struct got_remote_repo *remote;
1493 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremote));
1496 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1498 switch (imsg.hdr.type) {
1499 case GOT_IMSG_GITCONFIG_REMOTE:
1500 remote = &(*remotes)[*nremotes];
1501 if (datalen < sizeof(iremote)) {
1502 err = got_error(GOT_ERR_PRIVSEP_LEN);
1505 memcpy(&iremote, imsg.data, sizeof(iremote));
1506 if (iremote.name_len == 0 || iremote.url_len == 0 ||
1507 (sizeof(iremote) + iremote.name_len +
1508 iremote.url_len) > datalen) {
1509 err = got_error(GOT_ERR_PRIVSEP_LEN);
1512 remote->name = strndup(imsg.data + sizeof(iremote),
1514 if (remote->name == NULL) {
1515 err = got_error_from_errno("strndup");
1518 remote->url = strndup(imsg.data + sizeof(iremote) +
1519 iremote.name_len, iremote.url_len);
1520 if (remote->url == NULL) {
1521 err = got_error_from_errno("strndup");
1528 err = got_error(GOT_ERR_PRIVSEP_MSG);
1539 for (i = 0; i < *nremotes; i++) {
1540 free((*remotes)[i].name);
1541 free((*remotes)[i].url);
1550 const struct got_error *
1551 got_privsep_unveil_exec_helpers(void)
1553 const char *helpers[] = {
1554 GOT_PATH_PROG_READ_PACK,
1555 GOT_PATH_PROG_READ_OBJECT,
1556 GOT_PATH_PROG_READ_COMMIT,
1557 GOT_PATH_PROG_READ_TREE,
1558 GOT_PATH_PROG_READ_BLOB,
1559 GOT_PATH_PROG_READ_TAG,
1560 GOT_PATH_PROG_READ_GITCONFIG,
1564 for (i = 0; i < nitems(helpers); i++) {
1565 if (unveil(helpers[i], "x") == 0)
1567 return got_error_from_errno2("unveil", helpers[i]);
1574 got_privsep_exec_child(int imsg_fds[2], const char *path, const char *repo_path)
1576 if (close(imsg_fds[0]) != 0) {
1577 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
1581 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
1582 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
1585 if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
1586 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
1590 if (execl(path, path, repo_path, (char *)NULL) == -1) {
1591 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,