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 struct got_imsg_packed_object iobj, *iobjp;
240 if (id) { /* commit is packed */
242 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
250 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_REQUEST, 0, 0, fd, iobjp, len)
252 return got_error_from_errno();
254 return flush_imsg(ibuf);
257 const struct got_error *
258 got_privsep_send_tree_req(struct imsgbuf *ibuf, int fd,
259 struct got_object_id *id, int pack_idx)
261 struct got_imsg_packed_object iobj, *iobjp;
264 if (id) { /* tree is packed */
266 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
274 if (imsg_compose(ibuf, GOT_IMSG_TREE_REQUEST, 0, 0, fd, iobjp, len)
276 return got_error_from_errno();
278 return flush_imsg(ibuf);
281 const struct got_error *
282 got_privsep_send_tag_req(struct imsgbuf *ibuf, int fd,
283 struct got_object_id *id, int pack_idx)
285 struct got_imsg_packed_object iobj, *iobjp;
288 if (id) { /* tag is packed */
290 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
298 if (imsg_compose(ibuf, GOT_IMSG_TAG_REQUEST, 0, 0, fd, iobjp, len)
300 return got_error_from_errno();
302 return flush_imsg(ibuf);
305 const struct got_error *
306 got_privsep_send_blob_req(struct imsgbuf *ibuf, int infd,
307 struct got_object_id *id, int pack_idx)
309 struct got_imsg_packed_object iobj, *iobjp;
312 if (id) { /* blob is packed */
314 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
322 if (imsg_compose(ibuf, GOT_IMSG_BLOB_REQUEST, 0, 0, infd, iobjp, len)
324 return got_error_from_errno();
326 return flush_imsg(ibuf);
329 const struct got_error *
330 got_privsep_send_blob_outfd(struct imsgbuf *ibuf, int outfd)
332 if (imsg_compose(ibuf, GOT_IMSG_BLOB_OUTFD, 0, 0, outfd, NULL, 0)
334 return got_error_from_errno();
336 return flush_imsg(ibuf);
339 const struct got_error *
340 got_privsep_send_tmpfd(struct imsgbuf *ibuf, int fd)
342 if (imsg_compose(ibuf, GOT_IMSG_TMPFD, 0, 0, fd, NULL, 0)
344 return got_error_from_errno();
346 return flush_imsg(ibuf);
349 const struct got_error *
350 got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj)
352 struct got_imsg_object iobj;
354 memcpy(iobj.id, obj->id.sha1, sizeof(iobj.id));
355 iobj.type = obj->type;
356 iobj.flags = obj->flags;
357 iobj.hdrlen = obj->hdrlen;
358 iobj.size = obj->size;
359 if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
360 iobj.pack_offset = obj->pack_offset;
361 iobj.pack_idx = obj->pack_idx;
364 if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
366 return got_error_from_errno();
368 return flush_imsg(ibuf);
371 const struct got_error *
372 got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
373 struct imsgbuf *ibuf)
375 const struct got_error *err = NULL;
376 struct got_imsg_object *iobj;
377 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
379 if (datalen != sizeof(*iobj))
380 return got_error(GOT_ERR_PRIVSEP_LEN);
383 *obj = calloc(1, sizeof(**obj));
385 return got_error_from_errno();
387 memcpy((*obj)->id.sha1, iobj->id, SHA1_DIGEST_LENGTH);
388 (*obj)->type = iobj->type;
389 (*obj)->flags = iobj->flags;
390 (*obj)->hdrlen = iobj->hdrlen;
391 (*obj)->size = iobj->size;
392 /* path_packfile is handled by caller */
393 if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
394 (*obj)->pack_offset = iobj->pack_offset;
395 (*obj)->pack_idx = iobj->pack_idx;
401 const struct got_error *
402 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
404 const struct got_error *err = NULL;
407 const size_t min_datalen =
408 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
412 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
416 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
418 switch (imsg.hdr.type) {
419 case GOT_IMSG_OBJECT:
420 err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
423 err = got_error(GOT_ERR_PRIVSEP_MSG);
432 static const struct got_error *
433 send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
436 const struct got_error *err = NULL;
437 size_t offset, remain;
442 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
444 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
445 commit->logmsg + offset, n) == -1) {
446 err = got_error_from_errno();
450 err = flush_imsg(ibuf);
461 const struct got_error *
462 got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
464 const struct got_error *err = NULL;
465 struct got_imsg_commit_object *icommit;
468 struct got_object_qid *qid;
469 size_t author_len = strlen(commit->author);
470 size_t committer_len = strlen(commit->committer);
471 size_t logmsg_len = strlen(commit->logmsg);
473 total = sizeof(*icommit) + author_len + committer_len +
474 commit->nparents * SHA1_DIGEST_LENGTH;
478 return got_error_from_errno();
480 icommit = (struct got_imsg_commit_object *)buf;
481 memcpy(icommit->tree_id, commit->tree_id->sha1,
482 sizeof(icommit->tree_id));
483 icommit->author_len = author_len;
484 icommit->author_time = commit->author_time;
485 icommit->author_gmtoff = commit->author_gmtoff;
486 icommit->committer_len = committer_len;
487 icommit->committer_time = commit->committer_time;
488 icommit->committer_gmtoff = commit->committer_gmtoff;
489 icommit->logmsg_len = logmsg_len;
490 icommit->nparents = commit->nparents;
492 len = sizeof(*icommit);
493 memcpy(buf + len, commit->author, author_len);
495 memcpy(buf + len, commit->committer, committer_len);
496 len += committer_len;
497 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
498 memcpy(buf + len, qid->id, SHA1_DIGEST_LENGTH);
499 len += SHA1_DIGEST_LENGTH;
502 if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
503 err = got_error_from_errno();
507 if (logmsg_len == 0 ||
508 logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
509 err = flush_imsg(ibuf);
513 err = send_commit_logmsg(ibuf, commit, logmsg_len);
519 const struct got_error *
520 got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
522 const struct got_error *err = NULL;
524 struct got_imsg_commit_object *icommit;
527 const size_t min_datalen =
528 MIN(sizeof(struct got_imsg_error),
529 sizeof(struct got_imsg_commit_object));
533 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
537 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
540 switch (imsg.hdr.type) {
541 case GOT_IMSG_COMMIT:
542 if (datalen < sizeof(*icommit)) {
543 err = got_error(GOT_ERR_PRIVSEP_LEN);
547 if (datalen != sizeof(*icommit) + icommit->author_len +
548 icommit->committer_len +
549 icommit->nparents * SHA1_DIGEST_LENGTH) {
550 err = got_error(GOT_ERR_PRIVSEP_LEN);
553 if (icommit->nparents < 0) {
554 err = got_error(GOT_ERR_PRIVSEP_LEN);
557 len += sizeof(*icommit);
559 *commit = got_object_commit_alloc_partial();
560 if (*commit == NULL) {
561 err = got_error_from_errno();
565 memcpy((*commit)->tree_id->sha1, icommit->tree_id,
567 (*commit)->author_time = icommit->author_time;
568 (*commit)->author_gmtoff = icommit->author_gmtoff;
569 (*commit)->committer_time = icommit->committer_time;
570 (*commit)->committer_gmtoff = icommit->committer_gmtoff;
572 if (icommit->author_len == 0) {
573 (*commit)->author = strdup("");
574 if ((*commit)->author == NULL) {
575 err = got_error_from_errno();
579 (*commit)->author = malloc(icommit->author_len + 1);
580 if ((*commit)->author == NULL) {
581 err = got_error_from_errno();
584 memcpy((*commit)->author, imsg.data + len,
585 icommit->author_len);
586 (*commit)->author[icommit->author_len] = '\0';
588 len += icommit->author_len;
590 if (icommit->committer_len == 0) {
591 (*commit)->committer = strdup("");
592 if ((*commit)->committer == NULL) {
593 err = got_error_from_errno();
597 (*commit)->committer =
598 malloc(icommit->committer_len + 1);
599 if ((*commit)->committer == NULL) {
600 err = got_error_from_errno();
603 memcpy((*commit)->committer, imsg.data + len,
604 icommit->committer_len);
605 (*commit)->committer[icommit->committer_len] = '\0';
607 len += icommit->committer_len;
609 if (icommit->logmsg_len == 0) {
610 (*commit)->logmsg = strdup("");
611 if ((*commit)->logmsg == NULL) {
612 err = got_error_from_errno();
616 size_t offset = 0, remain = icommit->logmsg_len;
618 (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
619 if ((*commit)->logmsg == NULL) {
620 err = got_error_from_errno();
624 struct imsg imsg_log;
625 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
628 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
632 if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG)
633 return got_error(GOT_ERR_PRIVSEP_MSG);
635 memcpy((*commit)->logmsg + offset,
637 imsg_free(&imsg_log);
641 (*commit)->logmsg[icommit->logmsg_len] = '\0';
644 for (i = 0; i < icommit->nparents; i++) {
645 struct got_object_qid *qid;
647 err = got_object_qid_alloc_partial(&qid);
650 memcpy(qid->id, imsg.data + len +
651 i * SHA1_DIGEST_LENGTH, sizeof(*qid->id));
652 SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
653 (*commit)->nparents++;
657 err = got_error(GOT_ERR_PRIVSEP_MSG);
666 const struct got_error *
667 got_privsep_send_tree(struct imsgbuf *ibuf, struct got_tree_object *tree)
669 const struct got_error *err = NULL;
670 struct got_imsg_tree_object itree;
671 struct got_tree_entry *te;
673 int nimsg; /* number of imsg queued in ibuf */
675 itree.nentries = tree->entries.nentries;
676 if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
678 return got_error_from_errno();
680 totlen = sizeof(itree);
682 SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
683 struct got_imsg_tree_entry *ite;
685 size_t len = sizeof(*ite) + strlen(te->name);
687 if (len > MAX_IMSGSIZE)
688 return got_error(GOT_ERR_NO_SPACE);
691 if (totlen + len >= MAX_IMSGSIZE - (IMSG_HEADER_SIZE * nimsg)) {
692 err = flush_imsg(ibuf);
700 return got_error_from_errno();
702 ite = (struct got_imsg_tree_entry *)buf;
703 memcpy(ite->id, te->id->sha1, sizeof(ite->id));
704 ite->mode = te->mode;
705 memcpy(buf + sizeof(*ite), te->name, strlen(te->name));
707 if (imsg_compose(ibuf, GOT_IMSG_TREE_ENTRY, 0, 0, -1,
709 err = got_error_from_errno();
716 return flush_imsg(ibuf);
719 const struct got_error *
720 got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
722 const struct got_error *err = NULL;
723 const size_t min_datalen =
724 MIN(sizeof(struct got_imsg_error),
725 sizeof(struct got_imsg_tree_object));
726 struct got_imsg_tree_object *itree;
731 err = read_imsg(ibuf);
739 struct got_imsg_tree_entry *ite;
740 struct got_tree_entry *te = NULL;
742 n = imsg_get(ibuf, &imsg);
744 if (*tree && (*tree)->entries.nentries != nentries)
749 if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen)
750 return got_error(GOT_ERR_PRIVSEP_LEN);
752 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
754 switch (imsg.hdr.type) {
756 err = recv_imsg_error(&imsg, datalen);
759 /* This message should only appear once. */
761 err = got_error(GOT_ERR_PRIVSEP_MSG);
764 if (datalen != sizeof(*itree)) {
765 err = got_error(GOT_ERR_PRIVSEP_LEN);
769 *tree = malloc(sizeof(**tree));
771 err = got_error_from_errno();
774 (*tree)->entries.nentries = itree->nentries;
775 SIMPLEQ_INIT(&(*tree)->entries.head);
778 case GOT_IMSG_TREE_ENTRY:
779 /* This message should be preceeded by GOT_IMSG_TREE. */
781 err = got_error(GOT_ERR_PRIVSEP_MSG);
784 if (datalen < sizeof(*ite) || datalen > MAX_IMSGSIZE) {
785 err = got_error(GOT_ERR_PRIVSEP_LEN);
789 /* Remaining data contains the entry's name. */
790 datalen -= sizeof(*ite);
791 if (datalen == 0 || datalen > MAX_IMSGSIZE) {
792 err = got_error(GOT_ERR_PRIVSEP_LEN);
797 te = got_alloc_tree_entry_partial();
799 err = got_error_from_errno();
802 te->name = malloc(datalen + 1);
803 if (te->name == NULL) {
805 err = got_error_from_errno();
808 memcpy(te->name, imsg.data + sizeof(*ite), datalen);
809 te->name[datalen] = '\0';
811 memcpy(te->id->sha1, ite->id, SHA1_DIGEST_LENGTH);
812 te->mode = ite->mode;
813 SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
817 err = got_error(GOT_ERR_PRIVSEP_MSG);
824 if (*tree && (*tree)->entries.nentries != nentries) {
826 err = got_error(GOT_ERR_PRIVSEP_LEN);
827 got_object_tree_close(*tree);
834 const struct got_error *
835 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
838 struct got_imsg_blob iblob;
841 iblob.hdrlen = hdrlen;
846 if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
847 return got_error(GOT_ERR_NO_SPACE);
849 buf = malloc(sizeof(iblob) + size);
851 return got_error_from_errno();
853 memcpy(buf, &iblob, sizeof(iblob));
854 memcpy(buf + sizeof(iblob), data, size);
855 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
856 sizeof(iblob) + size) == -1) {
858 return got_error_from_errno();
862 /* Data has already been written to file descriptor. */
863 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
864 sizeof(iblob)) == -1)
865 return got_error_from_errno();
869 return flush_imsg(ibuf);
872 const struct got_error *
873 got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
874 struct imsgbuf *ibuf)
876 const struct got_error *err = NULL;
878 struct got_imsg_blob *iblob;
883 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
887 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
889 switch (imsg.hdr.type) {
891 if (datalen < sizeof(*iblob)) {
892 err = got_error(GOT_ERR_PRIVSEP_LEN);
897 *hdrlen = iblob->hdrlen;
899 if (datalen == sizeof(*iblob)) {
900 /* Data has been written to file descriptor. */
904 if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX) {
905 err = got_error(GOT_ERR_PRIVSEP_LEN);
909 *outbuf = malloc(*size);
910 if (*outbuf == NULL) {
911 err = got_error_from_errno();
914 memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
917 err = got_error(GOT_ERR_PRIVSEP_MSG);
926 static const struct got_error *
927 send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
929 const struct got_error *err = NULL;
930 size_t offset, remain;
935 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
937 if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
938 tag->tagmsg + offset, n) == -1) {
939 err = got_error_from_errno();
943 err = flush_imsg(ibuf);
954 const struct got_error *
955 got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
957 const struct got_error *err = NULL;
958 struct got_imsg_tag_object *itag;
961 size_t tag_len = strlen(tag->tag);
962 size_t tagger_len = strlen(tag->tagger);
963 size_t tagmsg_len = strlen(tag->tagmsg);
965 total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
969 return got_error_from_errno();
971 itag = (struct got_imsg_tag_object *)buf;
972 memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
973 itag->obj_type = tag->obj_type;
974 itag->tag_len = tag_len;
975 itag->tagger_len = tagger_len;
976 itag->tagger_time = tag->tagger_time;
977 itag->tagger_gmtoff = tag->tagger_gmtoff;
978 itag->tagmsg_len = tagmsg_len;
981 memcpy(buf + len, tag->tag, tag_len);
983 memcpy(buf + len, tag->tagger, tagger_len);
986 if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
987 err = got_error_from_errno();
991 if (tagmsg_len == 0 ||
992 tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
993 err = flush_imsg(ibuf);
997 err = send_tagmsg(ibuf, tag, tagmsg_len);
1003 const struct got_error *
1004 got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
1006 const struct got_error *err = NULL;
1008 struct got_imsg_tag_object *itag;
1009 size_t len, datalen;
1010 const size_t min_datalen =
1011 MIN(sizeof(struct got_imsg_error),
1012 sizeof(struct got_imsg_tag_object));
1016 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1020 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1023 switch (imsg.hdr.type) {
1025 if (datalen < sizeof(*itag)) {
1026 err = got_error(GOT_ERR_PRIVSEP_LEN);
1030 if (datalen != sizeof(*itag) + itag->tag_len +
1032 err = got_error(GOT_ERR_PRIVSEP_LEN);
1035 len += sizeof(*itag);
1037 *tag = calloc(1, sizeof(**tag));
1039 err = got_error_from_errno();
1043 memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
1045 if (itag->tag_len == 0) {
1046 (*tag)->tag = strdup("");
1047 if ((*tag)->tag == NULL) {
1048 err = got_error_from_errno();
1052 (*tag)->tag = malloc(itag->tag_len + 1);
1053 if ((*tag)->tag == NULL) {
1054 err = got_error_from_errno();
1057 memcpy((*tag)->tag, imsg.data + len,
1059 (*tag)->tag[itag->tag_len] = '\0';
1061 len += itag->tag_len;
1063 (*tag)->obj_type = itag->obj_type;
1064 (*tag)->tagger_time = itag->tagger_time;
1065 (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1067 if (itag->tagger_len == 0) {
1068 (*tag)->tagger = strdup("");
1069 if ((*tag)->tagger == NULL) {
1070 err = got_error_from_errno();
1074 (*tag)->tagger = malloc(itag->tagger_len + 1);
1075 if ((*tag)->tagger == NULL) {
1076 err = got_error_from_errno();
1079 memcpy((*tag)->tagger, imsg.data + len,
1081 (*tag)->tagger[itag->tagger_len] = '\0';
1083 len += itag->tagger_len;
1085 if (itag->tagmsg_len == 0) {
1086 (*tag)->tagmsg = strdup("");
1087 if ((*tag)->tagmsg == NULL) {
1088 err = got_error_from_errno();
1092 size_t offset = 0, remain = itag->tagmsg_len;
1094 (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1095 if ((*tag)->tagmsg == NULL) {
1096 err = got_error_from_errno();
1099 while (remain > 0) {
1100 struct imsg imsg_log;
1101 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1104 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1108 if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1109 return got_error(GOT_ERR_PRIVSEP_MSG);
1111 memcpy((*tag)->tagmsg + offset, imsg_log.data,
1113 imsg_free(&imsg_log);
1117 (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1122 err = got_error(GOT_ERR_PRIVSEP_MSG);
1131 const struct got_error *
1132 got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1133 struct got_packidx *packidx)
1135 struct got_imsg_packidx ipackidx;
1136 struct got_imsg_pack ipack;
1139 ipackidx.len = packidx->len;
1140 fd = dup(packidx->fd);
1142 return got_error_from_errno();
1144 if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
1145 sizeof(ipackidx)) == -1)
1146 return got_error_from_errno();
1148 if (strlcpy(ipack.path_packfile, pack->path_packfile,
1149 sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
1150 return got_error(GOT_ERR_NO_SPACE);
1151 ipack.filesize = pack->filesize;
1155 return got_error_from_errno();
1157 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
1159 return got_error_from_errno();
1161 return flush_imsg(ibuf);
1164 const struct got_error *
1165 got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
1166 struct got_object_id *id)
1168 struct got_imsg_packed_object iobj;
1171 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
1173 if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
1174 &iobj, sizeof(iobj)) == -1)
1175 return got_error_from_errno();
1177 return flush_imsg(ibuf);
1180 const struct got_error *
1181 got_privsep_unveil_exec_helpers(void)
1183 if (unveil(GOT_PATH_PROG_READ_PACK, "x") != 0 ||
1184 unveil(GOT_PATH_PROG_READ_OBJECT, "x") != 0 ||
1185 unveil(GOT_PATH_PROG_READ_COMMIT, "x") != 0 ||
1186 unveil(GOT_PATH_PROG_READ_TREE, "x") != 0 ||
1187 unveil(GOT_PATH_PROG_READ_BLOB, "x") != 0 ||
1188 unveil(GOT_PATH_PROG_READ_TAG, "x") != 0)
1189 return got_error_from_errno();