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 waitpid(pid, &child_status, 0);
100 if (!WIFEXITED(child_status))
101 return got_error(GOT_ERR_PRIVSEP_DIED);
103 if (WEXITSTATUS(child_status) != 0)
104 return got_error(GOT_ERR_PRIVSEP_EXIT);
109 static const struct got_error *
110 recv_imsg_error(struct imsg *imsg, size_t datalen)
112 struct got_imsg_error *ierr;
114 if (datalen != sizeof(*ierr))
115 return got_error(GOT_ERR_PRIVSEP_LEN);
118 if (ierr->code == GOT_ERR_ERRNO) {
119 static struct got_error serr;
120 serr.code = GOT_ERR_ERRNO;
121 serr.msg = strerror(ierr->errno_code);
125 return got_error(ierr->code);
128 const struct got_error *
129 got_privsep_recv_imsg(struct imsg *imsg, struct imsgbuf *ibuf,
132 const struct got_error *err;
135 n = imsg_get(ibuf, imsg);
137 return got_error_from_errno();
140 err = read_imsg(ibuf);
143 n = imsg_get(ibuf, imsg);
146 if (imsg->hdr.len < IMSG_HEADER_SIZE + min_datalen)
147 return got_error(GOT_ERR_PRIVSEP_LEN);
149 if (imsg->hdr.type == GOT_IMSG_ERROR) {
150 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
151 return recv_imsg_error(imsg, datalen);
157 /* Attempt to send an error in an imsg. Complain on stderr as a last resort. */
159 got_privsep_send_error(struct imsgbuf *ibuf, const struct got_error *err)
161 const struct got_error *poll_err;
162 struct got_imsg_error ierr;
165 ierr.code = err->code;
166 if (err->code == GOT_ERR_ERRNO)
167 ierr.errno_code = errno;
170 ret = imsg_compose(ibuf, GOT_IMSG_ERROR, 0, 0, -1, &ierr, sizeof(ierr));
172 fprintf(stderr, "%s: error %d \"%s\": imsg_compose: %s\n",
173 getprogname(), err->code, err->msg, strerror(errno));
177 poll_err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
179 fprintf(stderr, "%s: error %d \"%s\": poll: %s\n",
180 getprogname(), err->code, err->msg, poll_err->msg);
184 ret = imsg_flush(ibuf);
186 fprintf(stderr, "%s: error %d \"%s\": imsg_flush: %s\n",
187 getprogname(), err->code, err->msg, strerror(errno));
192 static const struct got_error *
193 flush_imsg(struct imsgbuf *ibuf)
195 const struct got_error *err;
197 err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
201 if (imsg_flush(ibuf) == -1)
202 return got_error_from_errno();
207 const struct got_error *
208 got_privsep_send_stop(int fd)
210 const struct got_error *err = NULL;
213 imsg_init(&ibuf, fd);
215 if (imsg_compose(&ibuf, GOT_IMSG_STOP, 0, 0, -1, NULL, 0) == -1)
216 return got_error_from_errno();
218 err = flush_imsg(&ibuf);
223 const struct got_error *
224 got_privsep_send_obj_req(struct imsgbuf *ibuf, int fd)
226 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_REQUEST, 0, 0, fd, NULL, 0)
228 return got_error_from_errno();
230 return flush_imsg(ibuf);
233 const struct got_error *
234 got_privsep_send_commit_req(struct imsgbuf *ibuf, int fd,
235 struct got_object_id *id, int pack_idx)
237 const struct got_error *err = NULL;
238 struct got_imsg_packed_object iobj, *iobjp;
241 if (id) { /* commit is packed */
243 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
251 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_REQUEST, 0, 0, fd, iobjp, len)
253 err = got_error_from_errno();
258 return flush_imsg(ibuf);
261 const struct got_error *
262 got_privsep_send_tree_req(struct imsgbuf *ibuf, int fd,
263 struct got_object_id *id, int pack_idx)
265 const struct got_error *err = NULL;
266 struct got_imsg_packed_object iobj, *iobjp;
269 if (id) { /* tree is packed */
271 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
279 if (imsg_compose(ibuf, GOT_IMSG_TREE_REQUEST, 0, 0, fd, iobjp, len)
281 err = got_error_from_errno();
286 return flush_imsg(ibuf);
289 const struct got_error *
290 got_privsep_send_tag_req(struct imsgbuf *ibuf, int fd,
291 struct got_object_id *id, int pack_idx)
293 struct got_imsg_packed_object iobj, *iobjp;
296 if (id) { /* tag is packed */
298 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
306 if (imsg_compose(ibuf, GOT_IMSG_TAG_REQUEST, 0, 0, fd, iobjp, len)
308 return got_error_from_errno();
310 return flush_imsg(ibuf);
313 const struct got_error *
314 got_privsep_send_blob_req(struct imsgbuf *ibuf, int infd,
315 struct got_object_id *id, int pack_idx)
317 const struct got_error *err = NULL;
318 struct got_imsg_packed_object iobj, *iobjp;
321 if (id) { /* blob is packed */
323 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
331 if (imsg_compose(ibuf, GOT_IMSG_BLOB_REQUEST, 0, 0, infd, iobjp, len)
333 err = got_error_from_errno();
338 return flush_imsg(ibuf);
341 const struct got_error *
342 got_privsep_send_blob_outfd(struct imsgbuf *ibuf, int outfd)
344 const struct got_error *err = NULL;
346 if (imsg_compose(ibuf, GOT_IMSG_BLOB_OUTFD, 0, 0, outfd, NULL, 0)
348 err = got_error_from_errno();
353 return flush_imsg(ibuf);
356 const struct got_error *
357 got_privsep_send_tmpfd(struct imsgbuf *ibuf, int fd)
359 const struct got_error *err = NULL;
361 if (imsg_compose(ibuf, GOT_IMSG_TMPFD, 0, 0, fd, NULL, 0)
363 err = got_error_from_errno();
368 return flush_imsg(ibuf);
371 const struct got_error *
372 got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj)
374 struct got_imsg_object iobj;
376 memcpy(iobj.id, obj->id.sha1, sizeof(iobj.id));
377 iobj.type = obj->type;
378 iobj.flags = obj->flags;
379 iobj.hdrlen = obj->hdrlen;
380 iobj.size = obj->size;
381 if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
382 iobj.pack_offset = obj->pack_offset;
383 iobj.pack_idx = obj->pack_idx;
386 if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
388 return got_error_from_errno();
390 return flush_imsg(ibuf);
393 const struct got_error *
394 got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
395 struct imsgbuf *ibuf)
397 const struct got_error *err = NULL;
398 struct got_imsg_object *iobj;
399 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
401 if (datalen != sizeof(*iobj))
402 return got_error(GOT_ERR_PRIVSEP_LEN);
405 *obj = calloc(1, sizeof(**obj));
407 return got_error_from_errno();
409 memcpy((*obj)->id.sha1, iobj->id, SHA1_DIGEST_LENGTH);
410 (*obj)->type = iobj->type;
411 (*obj)->flags = iobj->flags;
412 (*obj)->hdrlen = iobj->hdrlen;
413 (*obj)->size = iobj->size;
414 /* path_packfile is handled by caller */
415 if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
416 (*obj)->pack_offset = iobj->pack_offset;
417 (*obj)->pack_idx = iobj->pack_idx;
423 const struct got_error *
424 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
426 const struct got_error *err = NULL;
429 const size_t min_datalen =
430 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
434 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
438 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
440 switch (imsg.hdr.type) {
441 case GOT_IMSG_OBJECT:
442 err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
445 err = got_error(GOT_ERR_PRIVSEP_MSG);
454 static const struct got_error *
455 send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
458 const struct got_error *err = NULL;
459 size_t offset, remain;
464 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
466 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
467 commit->logmsg + offset, n) == -1) {
468 err = got_error_from_errno();
472 err = flush_imsg(ibuf);
483 const struct got_error *
484 got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
486 const struct got_error *err = NULL;
487 struct got_imsg_commit_object *icommit;
490 struct got_object_qid *qid;
491 size_t author_len = strlen(commit->author);
492 size_t committer_len = strlen(commit->committer);
493 size_t logmsg_len = strlen(commit->logmsg);
495 total = sizeof(*icommit) + author_len + committer_len +
496 commit->nparents * SHA1_DIGEST_LENGTH;
500 return got_error_from_errno();
502 icommit = (struct got_imsg_commit_object *)buf;
503 memcpy(icommit->tree_id, commit->tree_id->sha1,
504 sizeof(icommit->tree_id));
505 icommit->author_len = author_len;
506 icommit->author_time = commit->author_time;
507 icommit->author_gmtoff = commit->author_gmtoff;
508 icommit->committer_len = committer_len;
509 icommit->committer_time = commit->committer_time;
510 icommit->committer_gmtoff = commit->committer_gmtoff;
511 icommit->logmsg_len = logmsg_len;
512 icommit->nparents = commit->nparents;
514 len = sizeof(*icommit);
515 memcpy(buf + len, commit->author, author_len);
517 memcpy(buf + len, commit->committer, committer_len);
518 len += committer_len;
519 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
520 memcpy(buf + len, qid->id, SHA1_DIGEST_LENGTH);
521 len += SHA1_DIGEST_LENGTH;
524 if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
525 err = got_error_from_errno();
529 if (logmsg_len == 0 ||
530 logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
531 err = flush_imsg(ibuf);
535 err = send_commit_logmsg(ibuf, commit, logmsg_len);
541 const struct got_error *
542 got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
544 const struct got_error *err = NULL;
546 struct got_imsg_commit_object *icommit;
549 const size_t min_datalen =
550 MIN(sizeof(struct got_imsg_error),
551 sizeof(struct got_imsg_commit_object));
555 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
559 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
562 switch (imsg.hdr.type) {
563 case GOT_IMSG_COMMIT:
564 if (datalen < sizeof(*icommit)) {
565 err = got_error(GOT_ERR_PRIVSEP_LEN);
569 if (datalen != sizeof(*icommit) + icommit->author_len +
570 icommit->committer_len +
571 icommit->nparents * SHA1_DIGEST_LENGTH) {
572 err = got_error(GOT_ERR_PRIVSEP_LEN);
575 if (icommit->nparents < 0) {
576 err = got_error(GOT_ERR_PRIVSEP_LEN);
579 len += sizeof(*icommit);
581 *commit = got_object_commit_alloc_partial();
582 if (*commit == NULL) {
583 err = got_error_from_errno();
587 memcpy((*commit)->tree_id->sha1, icommit->tree_id,
589 (*commit)->author_time = icommit->author_time;
590 (*commit)->author_gmtoff = icommit->author_gmtoff;
591 (*commit)->committer_time = icommit->committer_time;
592 (*commit)->committer_gmtoff = icommit->committer_gmtoff;
594 if (icommit->author_len == 0) {
595 (*commit)->author = strdup("");
596 if ((*commit)->author == NULL) {
597 err = got_error_from_errno();
601 (*commit)->author = malloc(icommit->author_len + 1);
602 if ((*commit)->author == NULL) {
603 err = got_error_from_errno();
606 memcpy((*commit)->author, imsg.data + len,
607 icommit->author_len);
608 (*commit)->author[icommit->author_len] = '\0';
610 len += icommit->author_len;
612 if (icommit->committer_len == 0) {
613 (*commit)->committer = strdup("");
614 if ((*commit)->committer == NULL) {
615 err = got_error_from_errno();
619 (*commit)->committer =
620 malloc(icommit->committer_len + 1);
621 if ((*commit)->committer == NULL) {
622 err = got_error_from_errno();
625 memcpy((*commit)->committer, imsg.data + len,
626 icommit->committer_len);
627 (*commit)->committer[icommit->committer_len] = '\0';
629 len += icommit->committer_len;
631 if (icommit->logmsg_len == 0) {
632 (*commit)->logmsg = strdup("");
633 if ((*commit)->logmsg == NULL) {
634 err = got_error_from_errno();
638 size_t offset = 0, remain = icommit->logmsg_len;
640 (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
641 if ((*commit)->logmsg == NULL) {
642 err = got_error_from_errno();
646 struct imsg imsg_log;
647 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
650 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
654 if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG)
655 return got_error(GOT_ERR_PRIVSEP_MSG);
657 memcpy((*commit)->logmsg + offset,
659 imsg_free(&imsg_log);
663 (*commit)->logmsg[icommit->logmsg_len] = '\0';
666 for (i = 0; i < icommit->nparents; i++) {
667 struct got_object_qid *qid;
669 err = got_object_qid_alloc_partial(&qid);
672 memcpy(qid->id, imsg.data + len +
673 i * SHA1_DIGEST_LENGTH, sizeof(*qid->id));
674 SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
675 (*commit)->nparents++;
679 err = got_error(GOT_ERR_PRIVSEP_MSG);
688 const struct got_error *
689 got_privsep_send_tree(struct imsgbuf *ibuf, struct got_tree_object *tree)
691 const struct got_error *err = NULL;
692 struct got_imsg_tree_object itree;
693 struct got_tree_entry *te;
695 int nimsg; /* number of imsg queued in ibuf */
697 itree.nentries = tree->entries.nentries;
698 if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
700 return got_error_from_errno();
702 totlen = sizeof(itree);
704 SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
705 struct got_imsg_tree_entry *ite;
707 size_t len = sizeof(*ite) + strlen(te->name);
709 if (len > MAX_IMSGSIZE)
710 return got_error(GOT_ERR_NO_SPACE);
713 if (totlen + len >= MAX_IMSGSIZE - (IMSG_HEADER_SIZE * nimsg)) {
714 err = flush_imsg(ibuf);
722 return got_error_from_errno();
724 ite = (struct got_imsg_tree_entry *)buf;
725 memcpy(ite->id, te->id->sha1, sizeof(ite->id));
726 ite->mode = te->mode;
727 memcpy(buf + sizeof(*ite), te->name, strlen(te->name));
729 if (imsg_compose(ibuf, GOT_IMSG_TREE_ENTRY, 0, 0, -1,
731 err = got_error_from_errno();
738 return flush_imsg(ibuf);
741 const struct got_error *
742 got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
744 const struct got_error *err = NULL;
745 const size_t min_datalen =
746 MIN(sizeof(struct got_imsg_error),
747 sizeof(struct got_imsg_tree_object));
748 struct got_imsg_tree_object *itree;
753 err = read_imsg(ibuf);
761 struct got_imsg_tree_entry *ite;
762 struct got_tree_entry *te = NULL;
764 n = imsg_get(ibuf, &imsg);
766 if (*tree && (*tree)->entries.nentries != nentries)
771 if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen)
772 return got_error(GOT_ERR_PRIVSEP_LEN);
774 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
776 switch (imsg.hdr.type) {
778 err = recv_imsg_error(&imsg, datalen);
781 /* This message should only appear once. */
783 err = got_error(GOT_ERR_PRIVSEP_MSG);
786 if (datalen != sizeof(*itree)) {
787 err = got_error(GOT_ERR_PRIVSEP_LEN);
791 *tree = malloc(sizeof(**tree));
793 err = got_error_from_errno();
796 (*tree)->entries.nentries = itree->nentries;
797 SIMPLEQ_INIT(&(*tree)->entries.head);
800 case GOT_IMSG_TREE_ENTRY:
801 /* This message should be preceeded by GOT_IMSG_TREE. */
803 err = got_error(GOT_ERR_PRIVSEP_MSG);
806 if (datalen < sizeof(*ite) || datalen > MAX_IMSGSIZE) {
807 err = got_error(GOT_ERR_PRIVSEP_LEN);
811 /* Remaining data contains the entry's name. */
812 datalen -= sizeof(*ite);
813 if (datalen == 0 || datalen > MAX_IMSGSIZE) {
814 err = got_error(GOT_ERR_PRIVSEP_LEN);
819 te = got_alloc_tree_entry_partial();
821 err = got_error_from_errno();
824 te->name = malloc(datalen + 1);
825 if (te->name == NULL) {
827 err = got_error_from_errno();
830 memcpy(te->name, imsg.data + sizeof(*ite), datalen);
831 te->name[datalen] = '\0';
833 memcpy(te->id->sha1, ite->id, SHA1_DIGEST_LENGTH);
834 te->mode = ite->mode;
835 SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
839 err = got_error(GOT_ERR_PRIVSEP_MSG);
846 if (*tree && (*tree)->entries.nentries != nentries) {
848 err = got_error(GOT_ERR_PRIVSEP_LEN);
849 got_object_tree_close(*tree);
856 const struct got_error *
857 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
860 struct got_imsg_blob iblob;
863 iblob.hdrlen = hdrlen;
868 if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
869 return got_error(GOT_ERR_NO_SPACE);
871 buf = malloc(sizeof(iblob) + size);
873 return got_error_from_errno();
875 memcpy(buf, &iblob, sizeof(iblob));
876 memcpy(buf + sizeof(iblob), data, size);
877 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
878 sizeof(iblob) + size) == -1) {
880 return got_error_from_errno();
884 /* Data has already been written to file descriptor. */
885 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
886 sizeof(iblob)) == -1)
887 return got_error_from_errno();
891 return flush_imsg(ibuf);
894 const struct got_error *
895 got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
896 struct imsgbuf *ibuf)
898 const struct got_error *err = NULL;
900 struct got_imsg_blob *iblob;
905 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
909 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
911 switch (imsg.hdr.type) {
913 if (datalen < sizeof(*iblob)) {
914 err = got_error(GOT_ERR_PRIVSEP_LEN);
919 *hdrlen = iblob->hdrlen;
921 if (datalen == sizeof(*iblob)) {
922 /* Data has been written to file descriptor. */
926 if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX) {
927 err = got_error(GOT_ERR_PRIVSEP_LEN);
931 *outbuf = malloc(*size);
932 if (*outbuf == NULL) {
933 err = got_error_from_errno();
936 memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
939 err = got_error(GOT_ERR_PRIVSEP_MSG);
948 static const struct got_error *
949 send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
951 const struct got_error *err = NULL;
952 size_t offset, remain;
957 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
959 if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
960 tag->tagmsg + offset, n) == -1) {
961 err = got_error_from_errno();
965 err = flush_imsg(ibuf);
976 const struct got_error *
977 got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
979 const struct got_error *err = NULL;
980 struct got_imsg_tag_object *itag;
983 size_t tag_len = strlen(tag->tag);
984 size_t tagger_len = strlen(tag->tagger);
985 size_t tagmsg_len = strlen(tag->tagmsg);
987 total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
991 return got_error_from_errno();
993 itag = (struct got_imsg_tag_object *)buf;
994 memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
995 itag->obj_type = tag->obj_type;
996 itag->tag_len = tag_len;
997 itag->tagger_len = tagger_len;
998 itag->tagger_time = tag->tagger_time;
999 itag->tagger_gmtoff = tag->tagger_gmtoff;
1000 itag->tagmsg_len = tagmsg_len;
1002 len = sizeof(*itag);
1003 memcpy(buf + len, tag->tag, tag_len);
1005 memcpy(buf + len, tag->tagger, tagger_len);
1008 if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
1009 err = got_error_from_errno();
1013 if (tagmsg_len == 0 ||
1014 tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1015 err = flush_imsg(ibuf);
1019 err = send_tagmsg(ibuf, tag, tagmsg_len);
1025 const struct got_error *
1026 got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
1028 const struct got_error *err = NULL;
1030 struct got_imsg_tag_object *itag;
1031 size_t len, datalen;
1032 const size_t min_datalen =
1033 MIN(sizeof(struct got_imsg_error),
1034 sizeof(struct got_imsg_tag_object));
1038 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1042 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1045 switch (imsg.hdr.type) {
1047 if (datalen < sizeof(*itag)) {
1048 err = got_error(GOT_ERR_PRIVSEP_LEN);
1052 if (datalen != sizeof(*itag) + itag->tag_len +
1054 err = got_error(GOT_ERR_PRIVSEP_LEN);
1057 len += sizeof(*itag);
1059 *tag = calloc(1, sizeof(**tag));
1061 err = got_error_from_errno();
1065 memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
1067 if (itag->tag_len == 0) {
1068 (*tag)->tag = strdup("");
1069 if ((*tag)->tag == NULL) {
1070 err = got_error_from_errno();
1074 (*tag)->tag = malloc(itag->tag_len + 1);
1075 if ((*tag)->tag == NULL) {
1076 err = got_error_from_errno();
1079 memcpy((*tag)->tag, imsg.data + len,
1081 (*tag)->tag[itag->tag_len] = '\0';
1083 len += itag->tag_len;
1085 (*tag)->obj_type = itag->obj_type;
1086 (*tag)->tagger_time = itag->tagger_time;
1087 (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1089 if (itag->tagger_len == 0) {
1090 (*tag)->tagger = strdup("");
1091 if ((*tag)->tagger == NULL) {
1092 err = got_error_from_errno();
1096 (*tag)->tagger = malloc(itag->tagger_len + 1);
1097 if ((*tag)->tagger == NULL) {
1098 err = got_error_from_errno();
1101 memcpy((*tag)->tagger, imsg.data + len,
1103 (*tag)->tagger[itag->tagger_len] = '\0';
1105 len += itag->tagger_len;
1107 if (itag->tagmsg_len == 0) {
1108 (*tag)->tagmsg = strdup("");
1109 if ((*tag)->tagmsg == NULL) {
1110 err = got_error_from_errno();
1114 size_t offset = 0, remain = itag->tagmsg_len;
1116 (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1117 if ((*tag)->tagmsg == NULL) {
1118 err = got_error_from_errno();
1121 while (remain > 0) {
1122 struct imsg imsg_log;
1123 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1126 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1130 if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1131 return got_error(GOT_ERR_PRIVSEP_MSG);
1133 memcpy((*tag)->tagmsg + offset, imsg_log.data,
1135 imsg_free(&imsg_log);
1139 (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1144 err = got_error(GOT_ERR_PRIVSEP_MSG);
1153 const struct got_error *
1154 got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1155 struct got_packidx *packidx)
1157 const struct got_error *err = NULL;
1158 struct got_imsg_packidx ipackidx;
1159 struct got_imsg_pack ipack;
1162 ipackidx.len = packidx->len;
1163 fd = dup(packidx->fd);
1165 return got_error_from_errno();
1167 if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
1168 sizeof(ipackidx)) == -1) {
1169 err = got_error_from_errno();
1174 if (strlcpy(ipack.path_packfile, pack->path_packfile,
1175 sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
1176 return got_error(GOT_ERR_NO_SPACE);
1177 ipack.filesize = pack->filesize;
1181 return got_error_from_errno();
1183 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
1185 err = got_error_from_errno();
1190 return flush_imsg(ibuf);
1193 const struct got_error *
1194 got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
1195 struct got_object_id *id)
1197 struct got_imsg_packed_object iobj;
1200 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
1202 if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
1203 &iobj, sizeof(iobj)) == -1)
1204 return got_error_from_errno();
1206 return flush_imsg(ibuf);
1209 const struct got_error *
1210 got_privsep_unveil_exec_helpers(void)
1212 if (unveil(GOT_PATH_PROG_READ_PACK, "x") != 0 ||
1213 unveil(GOT_PATH_PROG_READ_OBJECT, "x") != 0 ||
1214 unveil(GOT_PATH_PROG_READ_COMMIT, "x") != 0 ||
1215 unveil(GOT_PATH_PROG_READ_TREE, "x") != 0 ||
1216 unveil(GOT_PATH_PROG_READ_BLOB, "x") != 0 ||
1217 unveil(GOT_PATH_PROG_READ_TAG, "x") != 0)
1218 return got_error_from_errno();