2 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/types.h>
18 #include <sys/queue.h>
20 #include <sys/syslimits.h>
34 #include "got_object.h"
35 #include "got_error.h"
37 #include "got_lib_sha1.h"
38 #include "got_lib_delta.h"
39 #include "got_lib_inflate.h"
40 #include "got_lib_object.h"
41 #include "got_lib_object_parse.h"
42 #include "got_lib_privsep.h"
43 #include "got_lib_pack.h"
46 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
49 static const struct got_error *
50 poll_fd(int fd, int events, int timeout)
56 pfd[0].events = events;
58 n = poll(pfd, 1, timeout);
60 return got_error_from_errno();
62 return got_error(GOT_ERR_TIMEOUT);
63 if (pfd[0].revents & (POLLERR | POLLNVAL))
64 return got_error_from_errno();
65 if (pfd[0].revents & (events | POLLHUP))
68 return got_error(GOT_ERR_INTERRUPT);
71 static const struct got_error *
72 read_imsg(struct imsgbuf *ibuf)
74 const struct got_error *err;
77 err = poll_fd(ibuf->fd, POLLIN, INFTIM);
83 if (errno == EAGAIN) /* Could be a file-descriptor leak. */
84 return got_error(GOT_ERR_PRIVSEP_NO_FD);
85 return got_error(GOT_ERR_PRIVSEP_READ);
88 return got_error(GOT_ERR_PRIVSEP_PIPE);
93 const struct got_error *
94 got_privsep_wait_for_child(pid_t pid)
98 if (waitpid(pid, &child_status, WNOHANG) == 0)
101 if (!WIFEXITED(child_status))
102 return got_error(GOT_ERR_PRIVSEP_DIED);
104 if (WEXITSTATUS(child_status) != 0)
105 return got_error(GOT_ERR_PRIVSEP_EXIT);
110 static const struct got_error *
111 recv_imsg_error(struct imsg *imsg, size_t datalen)
113 struct got_imsg_error *ierr;
115 if (datalen != sizeof(*ierr))
116 return got_error(GOT_ERR_PRIVSEP_LEN);
119 if (ierr->code == GOT_ERR_ERRNO) {
120 static struct got_error serr;
121 serr.code = GOT_ERR_ERRNO;
122 serr.msg = strerror(ierr->errno_code);
126 return got_error(ierr->code);
129 const struct got_error *
130 got_privsep_recv_imsg(struct imsg *imsg, struct imsgbuf *ibuf,
133 const struct got_error *err;
136 n = imsg_get(ibuf, imsg);
138 return got_error_from_errno();
141 err = read_imsg(ibuf);
144 n = imsg_get(ibuf, imsg);
147 if (imsg->hdr.len < IMSG_HEADER_SIZE + min_datalen)
148 return got_error(GOT_ERR_PRIVSEP_LEN);
150 if (imsg->hdr.type == GOT_IMSG_ERROR) {
151 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
152 return recv_imsg_error(imsg, datalen);
158 /* Attempt to send an error in an imsg. Complain on stderr as a last resort. */
160 got_privsep_send_error(struct imsgbuf *ibuf, const struct got_error *err)
162 const struct got_error *poll_err;
163 struct got_imsg_error ierr;
166 ierr.code = err->code;
167 if (err->code == GOT_ERR_ERRNO)
168 ierr.errno_code = errno;
171 ret = imsg_compose(ibuf, GOT_IMSG_ERROR, 0, 0, -1, &ierr, sizeof(ierr));
173 fprintf(stderr, "%s: error %d \"%s\": imsg_compose: %s\n",
174 getprogname(), err->code, err->msg, strerror(errno));
178 poll_err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
180 fprintf(stderr, "%s: error %d \"%s\": poll: %s\n",
181 getprogname(), err->code, err->msg, poll_err->msg);
185 ret = imsg_flush(ibuf);
187 fprintf(stderr, "%s: error %d \"%s\": imsg_flush: %s\n",
188 getprogname(), err->code, err->msg, strerror(errno));
193 static const struct got_error *
194 flush_imsg(struct imsgbuf *ibuf)
196 const struct got_error *err;
198 err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
202 if (imsg_flush(ibuf) == -1)
203 return got_error_from_errno();
208 const struct got_error *
209 got_privsep_send_stop(int fd)
211 const struct got_error *err = NULL;
214 imsg_init(&ibuf, fd);
216 if (imsg_compose(&ibuf, GOT_IMSG_STOP, 0, 0, -1, NULL, 0) == -1)
217 return got_error_from_errno();
219 err = flush_imsg(&ibuf);
224 const struct got_error *
225 got_privsep_send_obj_req(struct imsgbuf *ibuf, int fd)
227 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_REQUEST, 0, 0, fd, NULL, 0)
229 return got_error_from_errno();
231 return flush_imsg(ibuf);
234 const struct got_error *
235 got_privsep_send_commit_req(struct imsgbuf *ibuf, int fd,
236 struct got_object_id *id, int pack_idx)
238 const struct got_error *err = NULL;
239 struct got_imsg_packed_object iobj, *iobjp;
242 if (id) { /* commit is packed */
244 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
252 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_REQUEST, 0, 0, fd, iobjp, len)
254 err = got_error_from_errno();
259 return flush_imsg(ibuf);
262 const struct got_error *
263 got_privsep_send_tree_req(struct imsgbuf *ibuf, int fd,
264 struct got_object_id *id, int pack_idx)
266 const struct got_error *err = NULL;
267 struct got_imsg_packed_object iobj, *iobjp;
270 if (id) { /* tree is packed */
272 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
280 if (imsg_compose(ibuf, GOT_IMSG_TREE_REQUEST, 0, 0, fd, iobjp, len)
282 err = got_error_from_errno();
287 return flush_imsg(ibuf);
290 const struct got_error *
291 got_privsep_send_tag_req(struct imsgbuf *ibuf, int fd,
292 struct got_object_id *id, int pack_idx)
294 struct got_imsg_packed_object iobj, *iobjp;
297 if (id) { /* tag is packed */
299 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
307 if (imsg_compose(ibuf, GOT_IMSG_TAG_REQUEST, 0, 0, fd, iobjp, len)
309 return got_error_from_errno();
311 return flush_imsg(ibuf);
314 const struct got_error *
315 got_privsep_send_blob_req(struct imsgbuf *ibuf, int infd,
316 struct got_object_id *id, int pack_idx)
318 const struct got_error *err = NULL;
319 struct got_imsg_packed_object iobj, *iobjp;
322 if (id) { /* blob is packed */
324 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
332 if (imsg_compose(ibuf, GOT_IMSG_BLOB_REQUEST, 0, 0, infd, iobjp, len)
334 err = got_error_from_errno();
339 return flush_imsg(ibuf);
342 const struct got_error *
343 got_privsep_send_blob_outfd(struct imsgbuf *ibuf, int outfd)
345 const struct got_error *err = NULL;
347 if (imsg_compose(ibuf, GOT_IMSG_BLOB_OUTFD, 0, 0, outfd, NULL, 0)
349 err = got_error_from_errno();
354 return flush_imsg(ibuf);
357 const struct got_error *
358 got_privsep_send_tmpfd(struct imsgbuf *ibuf, int fd)
360 const struct got_error *err = NULL;
362 if (imsg_compose(ibuf, GOT_IMSG_TMPFD, 0, 0, fd, NULL, 0)
364 err = got_error_from_errno();
369 return flush_imsg(ibuf);
372 const struct got_error *
373 got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj)
375 struct got_imsg_object iobj;
377 memcpy(iobj.id, obj->id.sha1, sizeof(iobj.id));
378 iobj.type = obj->type;
379 iobj.flags = obj->flags;
380 iobj.hdrlen = obj->hdrlen;
381 iobj.size = obj->size;
382 if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
383 iobj.pack_offset = obj->pack_offset;
384 iobj.pack_idx = obj->pack_idx;
387 if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
389 return got_error_from_errno();
391 return flush_imsg(ibuf);
394 const struct got_error *
395 got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
396 struct imsgbuf *ibuf)
398 const struct got_error *err = NULL;
399 struct got_imsg_object *iobj;
400 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
402 if (datalen != sizeof(*iobj))
403 return got_error(GOT_ERR_PRIVSEP_LEN);
406 *obj = calloc(1, sizeof(**obj));
408 return got_error_from_errno();
410 memcpy((*obj)->id.sha1, iobj->id, SHA1_DIGEST_LENGTH);
411 (*obj)->type = iobj->type;
412 (*obj)->flags = iobj->flags;
413 (*obj)->hdrlen = iobj->hdrlen;
414 (*obj)->size = iobj->size;
415 /* path_packfile is handled by caller */
416 if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
417 (*obj)->pack_offset = iobj->pack_offset;
418 (*obj)->pack_idx = iobj->pack_idx;
424 const struct got_error *
425 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
427 const struct got_error *err = NULL;
430 const size_t min_datalen =
431 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
435 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
439 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
441 switch (imsg.hdr.type) {
442 case GOT_IMSG_OBJECT:
443 err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
446 err = got_error(GOT_ERR_PRIVSEP_MSG);
455 static const struct got_error *
456 send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
459 const struct got_error *err = NULL;
460 size_t offset, remain;
465 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
467 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
468 commit->logmsg + offset, n) == -1) {
469 err = got_error_from_errno();
473 err = flush_imsg(ibuf);
484 const struct got_error *
485 got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
487 const struct got_error *err = NULL;
488 struct got_imsg_commit_object *icommit;
491 struct got_object_qid *qid;
492 size_t author_len = strlen(commit->author);
493 size_t committer_len = strlen(commit->committer);
494 size_t logmsg_len = strlen(commit->logmsg);
496 total = sizeof(*icommit) + author_len + committer_len +
497 commit->nparents * SHA1_DIGEST_LENGTH;
501 return got_error_from_errno();
503 icommit = (struct got_imsg_commit_object *)buf;
504 memcpy(icommit->tree_id, commit->tree_id->sha1,
505 sizeof(icommit->tree_id));
506 icommit->author_len = author_len;
507 icommit->author_time = commit->author_time;
508 icommit->author_gmtoff = commit->author_gmtoff;
509 icommit->committer_len = committer_len;
510 icommit->committer_time = commit->committer_time;
511 icommit->committer_gmtoff = commit->committer_gmtoff;
512 icommit->logmsg_len = logmsg_len;
513 icommit->nparents = commit->nparents;
515 len = sizeof(*icommit);
516 memcpy(buf + len, commit->author, author_len);
518 memcpy(buf + len, commit->committer, committer_len);
519 len += committer_len;
520 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
521 memcpy(buf + len, qid->id, SHA1_DIGEST_LENGTH);
522 len += SHA1_DIGEST_LENGTH;
525 if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
526 err = got_error_from_errno();
530 if (logmsg_len == 0 ||
531 logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
532 err = flush_imsg(ibuf);
536 err = send_commit_logmsg(ibuf, commit, logmsg_len);
542 const struct got_error *
543 got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
545 const struct got_error *err = NULL;
547 struct got_imsg_commit_object *icommit;
550 const size_t min_datalen =
551 MIN(sizeof(struct got_imsg_error),
552 sizeof(struct got_imsg_commit_object));
556 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
560 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
563 switch (imsg.hdr.type) {
564 case GOT_IMSG_COMMIT:
565 if (datalen < sizeof(*icommit)) {
566 err = got_error(GOT_ERR_PRIVSEP_LEN);
570 if (datalen != sizeof(*icommit) + icommit->author_len +
571 icommit->committer_len +
572 icommit->nparents * SHA1_DIGEST_LENGTH) {
573 err = got_error(GOT_ERR_PRIVSEP_LEN);
576 if (icommit->nparents < 0) {
577 err = got_error(GOT_ERR_PRIVSEP_LEN);
580 len += sizeof(*icommit);
582 *commit = got_object_commit_alloc_partial();
583 if (*commit == NULL) {
584 err = got_error_from_errno();
588 memcpy((*commit)->tree_id->sha1, icommit->tree_id,
590 (*commit)->author_time = icommit->author_time;
591 (*commit)->author_gmtoff = icommit->author_gmtoff;
592 (*commit)->committer_time = icommit->committer_time;
593 (*commit)->committer_gmtoff = icommit->committer_gmtoff;
595 if (icommit->author_len == 0) {
596 (*commit)->author = strdup("");
597 if ((*commit)->author == NULL) {
598 err = got_error_from_errno();
602 (*commit)->author = malloc(icommit->author_len + 1);
603 if ((*commit)->author == NULL) {
604 err = got_error_from_errno();
607 memcpy((*commit)->author, imsg.data + len,
608 icommit->author_len);
609 (*commit)->author[icommit->author_len] = '\0';
611 len += icommit->author_len;
613 if (icommit->committer_len == 0) {
614 (*commit)->committer = strdup("");
615 if ((*commit)->committer == NULL) {
616 err = got_error_from_errno();
620 (*commit)->committer =
621 malloc(icommit->committer_len + 1);
622 if ((*commit)->committer == NULL) {
623 err = got_error_from_errno();
626 memcpy((*commit)->committer, imsg.data + len,
627 icommit->committer_len);
628 (*commit)->committer[icommit->committer_len] = '\0';
630 len += icommit->committer_len;
632 if (icommit->logmsg_len == 0) {
633 (*commit)->logmsg = strdup("");
634 if ((*commit)->logmsg == NULL) {
635 err = got_error_from_errno();
639 size_t offset = 0, remain = icommit->logmsg_len;
641 (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
642 if ((*commit)->logmsg == NULL) {
643 err = got_error_from_errno();
647 struct imsg imsg_log;
648 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
651 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
655 if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG)
656 return got_error(GOT_ERR_PRIVSEP_MSG);
658 memcpy((*commit)->logmsg + offset,
660 imsg_free(&imsg_log);
664 (*commit)->logmsg[icommit->logmsg_len] = '\0';
667 for (i = 0; i < icommit->nparents; i++) {
668 struct got_object_qid *qid;
670 err = got_object_qid_alloc_partial(&qid);
673 memcpy(qid->id, imsg.data + len +
674 i * SHA1_DIGEST_LENGTH, sizeof(*qid->id));
675 SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
676 (*commit)->nparents++;
680 err = got_error(GOT_ERR_PRIVSEP_MSG);
689 const struct got_error *
690 got_privsep_send_tree(struct imsgbuf *ibuf, struct got_tree_object *tree)
692 const struct got_error *err = NULL;
693 struct got_imsg_tree_object itree;
694 struct got_tree_entry *te;
696 int nimsg; /* number of imsg queued in ibuf */
698 itree.nentries = tree->entries.nentries;
699 if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
701 return got_error_from_errno();
703 totlen = sizeof(itree);
705 SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
706 struct got_imsg_tree_entry *ite;
708 size_t len = sizeof(*ite) + strlen(te->name);
710 if (len > MAX_IMSGSIZE)
711 return got_error(GOT_ERR_NO_SPACE);
714 if (totlen + len >= MAX_IMSGSIZE - (IMSG_HEADER_SIZE * nimsg)) {
715 err = flush_imsg(ibuf);
723 return got_error_from_errno();
725 ite = (struct got_imsg_tree_entry *)buf;
726 memcpy(ite->id, te->id->sha1, sizeof(ite->id));
727 ite->mode = te->mode;
728 memcpy(buf + sizeof(*ite), te->name, strlen(te->name));
730 if (imsg_compose(ibuf, GOT_IMSG_TREE_ENTRY, 0, 0, -1,
732 err = got_error_from_errno();
739 return flush_imsg(ibuf);
742 const struct got_error *
743 got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
745 const struct got_error *err = NULL;
746 const size_t min_datalen =
747 MIN(sizeof(struct got_imsg_error),
748 sizeof(struct got_imsg_tree_object));
749 struct got_imsg_tree_object *itree;
754 err = read_imsg(ibuf);
762 struct got_imsg_tree_entry *ite;
763 struct got_tree_entry *te = NULL;
765 n = imsg_get(ibuf, &imsg);
767 if (*tree && (*tree)->entries.nentries != nentries)
772 if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen)
773 return got_error(GOT_ERR_PRIVSEP_LEN);
775 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
777 switch (imsg.hdr.type) {
779 err = recv_imsg_error(&imsg, datalen);
782 /* This message should only appear once. */
784 err = got_error(GOT_ERR_PRIVSEP_MSG);
787 if (datalen != sizeof(*itree)) {
788 err = got_error(GOT_ERR_PRIVSEP_LEN);
792 *tree = malloc(sizeof(**tree));
794 err = got_error_from_errno();
797 (*tree)->entries.nentries = itree->nentries;
798 SIMPLEQ_INIT(&(*tree)->entries.head);
801 case GOT_IMSG_TREE_ENTRY:
802 /* This message should be preceeded by GOT_IMSG_TREE. */
804 err = got_error(GOT_ERR_PRIVSEP_MSG);
807 if (datalen < sizeof(*ite) || datalen > MAX_IMSGSIZE) {
808 err = got_error(GOT_ERR_PRIVSEP_LEN);
812 /* Remaining data contains the entry's name. */
813 datalen -= sizeof(*ite);
814 if (datalen == 0 || datalen > MAX_IMSGSIZE) {
815 err = got_error(GOT_ERR_PRIVSEP_LEN);
820 te = got_alloc_tree_entry_partial();
822 err = got_error_from_errno();
825 te->name = malloc(datalen + 1);
826 if (te->name == NULL) {
828 err = got_error_from_errno();
831 memcpy(te->name, imsg.data + sizeof(*ite), datalen);
832 te->name[datalen] = '\0';
834 memcpy(te->id->sha1, ite->id, SHA1_DIGEST_LENGTH);
835 te->mode = ite->mode;
836 SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
840 err = got_error(GOT_ERR_PRIVSEP_MSG);
847 if (*tree && (*tree)->entries.nentries != nentries) {
849 err = got_error(GOT_ERR_PRIVSEP_LEN);
850 got_object_tree_close(*tree);
857 const struct got_error *
858 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
861 struct got_imsg_blob iblob;
864 iblob.hdrlen = hdrlen;
869 if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
870 return got_error(GOT_ERR_NO_SPACE);
872 buf = malloc(sizeof(iblob) + size);
874 return got_error_from_errno();
876 memcpy(buf, &iblob, sizeof(iblob));
877 memcpy(buf + sizeof(iblob), data, size);
878 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
879 sizeof(iblob) + size) == -1) {
881 return got_error_from_errno();
885 /* Data has already been written to file descriptor. */
886 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
887 sizeof(iblob)) == -1)
888 return got_error_from_errno();
892 return flush_imsg(ibuf);
895 const struct got_error *
896 got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
897 struct imsgbuf *ibuf)
899 const struct got_error *err = NULL;
901 struct got_imsg_blob *iblob;
906 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
910 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
912 switch (imsg.hdr.type) {
914 if (datalen < sizeof(*iblob)) {
915 err = got_error(GOT_ERR_PRIVSEP_LEN);
920 *hdrlen = iblob->hdrlen;
922 if (datalen == sizeof(*iblob)) {
923 /* Data has been written to file descriptor. */
927 if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX) {
928 err = got_error(GOT_ERR_PRIVSEP_LEN);
932 *outbuf = malloc(*size);
933 if (*outbuf == NULL) {
934 err = got_error_from_errno();
937 memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
940 err = got_error(GOT_ERR_PRIVSEP_MSG);
949 static const struct got_error *
950 send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
952 const struct got_error *err = NULL;
953 size_t offset, remain;
958 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
960 if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
961 tag->tagmsg + offset, n) == -1) {
962 err = got_error_from_errno();
966 err = flush_imsg(ibuf);
977 const struct got_error *
978 got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
980 const struct got_error *err = NULL;
981 struct got_imsg_tag_object *itag;
984 size_t tag_len = strlen(tag->tag);
985 size_t tagger_len = strlen(tag->tagger);
986 size_t tagmsg_len = strlen(tag->tagmsg);
988 total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
992 return got_error_from_errno();
994 itag = (struct got_imsg_tag_object *)buf;
995 memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
996 itag->obj_type = tag->obj_type;
997 itag->tag_len = tag_len;
998 itag->tagger_len = tagger_len;
999 itag->tagger_time = tag->tagger_time;
1000 itag->tagger_gmtoff = tag->tagger_gmtoff;
1001 itag->tagmsg_len = tagmsg_len;
1003 len = sizeof(*itag);
1004 memcpy(buf + len, tag->tag, tag_len);
1006 memcpy(buf + len, tag->tagger, tagger_len);
1009 if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
1010 err = got_error_from_errno();
1014 if (tagmsg_len == 0 ||
1015 tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1016 err = flush_imsg(ibuf);
1020 err = send_tagmsg(ibuf, tag, tagmsg_len);
1026 const struct got_error *
1027 got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
1029 const struct got_error *err = NULL;
1031 struct got_imsg_tag_object *itag;
1032 size_t len, datalen;
1033 const size_t min_datalen =
1034 MIN(sizeof(struct got_imsg_error),
1035 sizeof(struct got_imsg_tag_object));
1039 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1043 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1046 switch (imsg.hdr.type) {
1048 if (datalen < sizeof(*itag)) {
1049 err = got_error(GOT_ERR_PRIVSEP_LEN);
1053 if (datalen != sizeof(*itag) + itag->tag_len +
1055 err = got_error(GOT_ERR_PRIVSEP_LEN);
1058 len += sizeof(*itag);
1060 *tag = calloc(1, sizeof(**tag));
1062 err = got_error_from_errno();
1066 memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
1068 if (itag->tag_len == 0) {
1069 (*tag)->tag = strdup("");
1070 if ((*tag)->tag == NULL) {
1071 err = got_error_from_errno();
1075 (*tag)->tag = malloc(itag->tag_len + 1);
1076 if ((*tag)->tag == NULL) {
1077 err = got_error_from_errno();
1080 memcpy((*tag)->tag, imsg.data + len,
1082 (*tag)->tag[itag->tag_len] = '\0';
1084 len += itag->tag_len;
1086 (*tag)->obj_type = itag->obj_type;
1087 (*tag)->tagger_time = itag->tagger_time;
1088 (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1090 if (itag->tagger_len == 0) {
1091 (*tag)->tagger = strdup("");
1092 if ((*tag)->tagger == NULL) {
1093 err = got_error_from_errno();
1097 (*tag)->tagger = malloc(itag->tagger_len + 1);
1098 if ((*tag)->tagger == NULL) {
1099 err = got_error_from_errno();
1102 memcpy((*tag)->tagger, imsg.data + len,
1104 (*tag)->tagger[itag->tagger_len] = '\0';
1106 len += itag->tagger_len;
1108 if (itag->tagmsg_len == 0) {
1109 (*tag)->tagmsg = strdup("");
1110 if ((*tag)->tagmsg == NULL) {
1111 err = got_error_from_errno();
1115 size_t offset = 0, remain = itag->tagmsg_len;
1117 (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1118 if ((*tag)->tagmsg == NULL) {
1119 err = got_error_from_errno();
1122 while (remain > 0) {
1123 struct imsg imsg_log;
1124 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1127 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1131 if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1132 return got_error(GOT_ERR_PRIVSEP_MSG);
1134 memcpy((*tag)->tagmsg + offset, imsg_log.data,
1136 imsg_free(&imsg_log);
1140 (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1145 err = got_error(GOT_ERR_PRIVSEP_MSG);
1154 const struct got_error *
1155 got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1156 struct got_packidx *packidx)
1158 const struct got_error *err = NULL;
1159 struct got_imsg_packidx ipackidx;
1160 struct got_imsg_pack ipack;
1163 ipackidx.len = packidx->len;
1164 fd = dup(packidx->fd);
1166 return got_error_from_errno();
1168 if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
1169 sizeof(ipackidx)) == -1) {
1170 err = got_error_from_errno();
1175 if (strlcpy(ipack.path_packfile, pack->path_packfile,
1176 sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
1177 return got_error(GOT_ERR_NO_SPACE);
1178 ipack.filesize = pack->filesize;
1182 return got_error_from_errno();
1184 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
1186 err = got_error_from_errno();
1191 return flush_imsg(ibuf);
1194 const struct got_error *
1195 got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
1196 struct got_object_id *id)
1198 struct got_imsg_packed_object iobj;
1201 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
1203 if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
1204 &iobj, sizeof(iobj)) == -1)
1205 return got_error_from_errno();
1207 return flush_imsg(ibuf);
1210 const struct got_error *
1211 got_privsep_unveil_exec_helpers(void)
1213 if (unveil(GOT_PATH_PROG_READ_PACK, "x") != 0 ||
1214 unveil(GOT_PATH_PROG_READ_OBJECT, "x") != 0 ||
1215 unveil(GOT_PATH_PROG_READ_COMMIT, "x") != 0 ||
1216 unveil(GOT_PATH_PROG_READ_TREE, "x") != 0 ||
1217 unveil(GOT_PATH_PROG_READ_BLOB, "x") != 0 ||
1218 unveil(GOT_PATH_PROG_READ_TAG, "x") != 0)
1219 return got_error_from_errno();