2 * Copyright (c) 2018 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, struct got_object *obj)
226 struct got_imsg_object iobj, *iobjp = NULL;
227 size_t iobj_size = 0;
228 int imsg_code = GOT_IMSG_OBJECT_REQUEST;
232 case GOT_OBJ_TYPE_TREE:
233 imsg_code = GOT_IMSG_TREE_REQUEST;
235 case GOT_OBJ_TYPE_COMMIT:
236 abort(); /* should not get here */
238 case GOT_OBJ_TYPE_BLOB:
239 imsg_code = GOT_IMSG_BLOB_REQUEST;
241 case GOT_OBJ_TYPE_TAG:
242 imsg_code = GOT_IMSG_TAG_REQUEST;
245 return got_error(GOT_ERR_OBJ_TYPE);
248 memcpy(iobj.id, obj->id.sha1, sizeof(iobj.id));
249 iobj.type = obj->type;
250 iobj.flags = obj->flags;
251 iobj.hdrlen = obj->hdrlen;
252 iobj.size = obj->size;
253 if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
254 iobj.pack_offset = obj->pack_offset;
255 iobj.pack_idx = obj->pack_idx;
259 iobj_size = sizeof(iobj);
262 if (imsg_compose(ibuf, imsg_code, 0, 0, fd, iobjp, iobj_size) == -1)
263 return got_error_from_errno();
265 return flush_imsg(ibuf);
268 const struct got_error *
269 got_privsep_send_commit_req(struct imsgbuf *ibuf, int fd,
270 struct got_object_id *id, int pack_idx)
272 struct got_imsg_packed_object iobj, *iobjp;
275 if (id) { /* commit is packed */
277 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
285 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_REQUEST, 0, 0, fd, iobjp, len)
287 return got_error_from_errno();
289 return flush_imsg(ibuf);
292 const struct got_error *
293 got_privsep_send_tree_req(struct imsgbuf *ibuf, int fd,
294 struct got_object_id *id, int pack_idx)
296 struct got_imsg_packed_object iobj, *iobjp;
299 if (id) { /* tree is packed */
301 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
309 if (imsg_compose(ibuf, GOT_IMSG_TREE_REQUEST, 0, 0, fd, iobjp, len)
311 return got_error_from_errno();
313 return flush_imsg(ibuf);
316 const struct got_error *
317 got_privsep_send_blob_req(struct imsgbuf *ibuf, int infd)
319 if (imsg_compose(ibuf, GOT_IMSG_BLOB_REQUEST, 0, 0, infd, NULL, 0)
321 return got_error_from_errno();
323 return flush_imsg(ibuf);
326 const struct got_error *
327 got_privsep_send_blob_outfd(struct imsgbuf *ibuf, int outfd)
329 if (imsg_compose(ibuf, GOT_IMSG_BLOB_OUTFD, 0, 0, outfd, NULL, 0)
331 return got_error_from_errno();
333 return flush_imsg(ibuf);
336 const struct got_error *
337 got_privsep_send_tmpfd(struct imsgbuf *ibuf, int fd)
339 if (imsg_compose(ibuf, GOT_IMSG_TMPFD, 0, 0, fd, NULL, 0)
341 return got_error_from_errno();
343 return flush_imsg(ibuf);
346 const struct got_error *
347 got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj)
349 struct got_imsg_object iobj;
351 memcpy(iobj.id, obj->id.sha1, sizeof(iobj.id));
352 iobj.type = obj->type;
353 iobj.flags = obj->flags;
354 iobj.hdrlen = obj->hdrlen;
355 iobj.size = obj->size;
356 if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
357 iobj.pack_offset = obj->pack_offset;
358 iobj.pack_idx = obj->pack_idx;
361 if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
363 return got_error_from_errno();
365 return flush_imsg(ibuf);
368 const struct got_error *
369 got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
370 struct imsgbuf *ibuf)
372 const struct got_error *err = NULL;
373 struct got_imsg_object *iobj;
374 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
376 if (datalen != sizeof(*iobj))
377 return got_error(GOT_ERR_PRIVSEP_LEN);
380 *obj = calloc(1, sizeof(**obj));
382 return got_error_from_errno();
384 memcpy((*obj)->id.sha1, iobj->id, SHA1_DIGEST_LENGTH);
385 (*obj)->type = iobj->type;
386 (*obj)->flags = iobj->flags;
387 (*obj)->hdrlen = iobj->hdrlen;
388 (*obj)->size = iobj->size;
389 /* path_packfile is handled by caller */
390 if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
391 (*obj)->pack_offset = iobj->pack_offset;
392 (*obj)->pack_idx = iobj->pack_idx;
398 const struct got_error *
399 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
401 const struct got_error *err = NULL;
404 const size_t min_datalen =
405 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
409 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
413 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
415 switch (imsg.hdr.type) {
416 case GOT_IMSG_OBJECT:
417 err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
420 err = got_error(GOT_ERR_PRIVSEP_MSG);
429 static const struct got_error *
430 send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
433 const struct got_error *err = NULL;
434 size_t offset, remain;
439 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
441 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
442 commit->logmsg + offset, n) == -1) {
443 err = got_error_from_errno();
447 err = flush_imsg(ibuf);
458 const struct got_error *
459 got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
461 const struct got_error *err = NULL;
462 struct got_imsg_commit_object *icommit;
465 struct got_object_qid *qid;
466 size_t author_len = strlen(commit->author);
467 size_t committer_len = strlen(commit->committer);
468 size_t logmsg_len = strlen(commit->logmsg);
470 total = sizeof(*icommit) + author_len + committer_len +
471 commit->nparents * SHA1_DIGEST_LENGTH;
475 return got_error_from_errno();
477 icommit = (struct got_imsg_commit_object *)buf;
478 memcpy(icommit->tree_id, commit->tree_id->sha1, sizeof(icommit->tree_id));
479 icommit->author_len = author_len;
480 icommit->author_time = commit->author_time;
481 icommit->author_gmtoff = commit->author_gmtoff;
482 icommit->committer_len = committer_len;
483 icommit->committer_time = commit->committer_time;
484 icommit->committer_gmtoff = commit->committer_gmtoff;
485 icommit->logmsg_len = logmsg_len;
486 icommit->nparents = commit->nparents;
488 len = sizeof(*icommit);
489 memcpy(buf + len, commit->author, author_len);
491 memcpy(buf + len, commit->committer, committer_len);
492 len += committer_len;
493 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
494 memcpy(buf + len, qid->id, SHA1_DIGEST_LENGTH);
495 len += SHA1_DIGEST_LENGTH;
498 if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
499 err = got_error_from_errno();
503 if (logmsg_len == 0 ||
504 logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
505 err = flush_imsg(ibuf);
509 err = send_commit_logmsg(ibuf, commit, logmsg_len);
515 const struct got_error *
516 got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
518 const struct got_error *err = NULL;
520 struct got_imsg_commit_object *icommit;
523 const size_t min_datalen =
524 MIN(sizeof(struct got_imsg_error),
525 sizeof(struct got_imsg_commit_object));
529 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
533 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
536 switch (imsg.hdr.type) {
537 case GOT_IMSG_COMMIT:
538 if (datalen < sizeof(*icommit)) {
539 err = got_error(GOT_ERR_PRIVSEP_LEN);
543 if (datalen != sizeof(*icommit) + icommit->author_len +
544 icommit->committer_len +
545 icommit->nparents * SHA1_DIGEST_LENGTH) {
546 err = got_error(GOT_ERR_PRIVSEP_LEN);
549 if (icommit->nparents < 0) {
550 err = got_error(GOT_ERR_PRIVSEP_LEN);
553 len += sizeof(*icommit);
555 *commit = got_object_commit_alloc_partial();
556 if (*commit == NULL) {
557 err = got_error_from_errno();
561 memcpy((*commit)->tree_id->sha1, icommit->tree_id,
563 (*commit)->author_time = icommit->author_time;
564 (*commit)->author_gmtoff = icommit->author_gmtoff;
565 (*commit)->committer_time = icommit->committer_time;
566 (*commit)->committer_gmtoff = icommit->committer_gmtoff;
568 if (icommit->author_len == 0) {
569 (*commit)->author = strdup("");
570 if ((*commit)->author == NULL) {
571 err = got_error_from_errno();
575 (*commit)->author = malloc(icommit->author_len + 1);
576 if ((*commit)->author == NULL) {
577 err = got_error_from_errno();
580 memcpy((*commit)->author, imsg.data + len,
581 icommit->author_len);
582 (*commit)->author[icommit->author_len] = '\0';
584 len += icommit->author_len;
586 if (icommit->committer_len == 0) {
587 (*commit)->committer = strdup("");
588 if ((*commit)->committer == NULL) {
589 err = got_error_from_errno();
593 (*commit)->committer =
594 malloc(icommit->committer_len + 1);
595 if ((*commit)->committer == NULL) {
596 err = got_error_from_errno();
599 memcpy((*commit)->committer, imsg.data + len,
600 icommit->committer_len);
601 (*commit)->committer[icommit->committer_len] = '\0';
603 len += icommit->committer_len;
605 if (icommit->logmsg_len == 0) {
606 (*commit)->logmsg = strdup("");
607 if ((*commit)->logmsg == NULL) {
608 err = got_error_from_errno();
612 size_t offset = 0, remain = icommit->logmsg_len;
614 (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
615 if ((*commit)->logmsg == NULL) {
616 err = got_error_from_errno();
620 struct imsg imsg_log;
621 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
624 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
628 if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG)
629 return got_error(GOT_ERR_PRIVSEP_MSG);
631 memcpy((*commit)->logmsg + offset,
633 imsg_free(&imsg_log);
637 (*commit)->logmsg[icommit->logmsg_len] = '\0';
640 for (i = 0; i < icommit->nparents; i++) {
641 struct got_object_qid *qid;
643 err = got_object_qid_alloc_partial(&qid);
646 memcpy(qid->id, imsg.data + len +
647 i * SHA1_DIGEST_LENGTH, sizeof(*qid->id));
648 SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
649 (*commit)->nparents++;
653 err = got_error(GOT_ERR_PRIVSEP_MSG);
662 const struct got_error *
663 got_privsep_send_tree(struct imsgbuf *ibuf, struct got_tree_object *tree)
665 const struct got_error *err = NULL;
666 struct got_imsg_tree_object itree;
667 struct got_tree_entry *te;
669 int nimsg; /* number of imsg queued in ibuf */
671 itree.nentries = tree->entries.nentries;
672 if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
674 return got_error_from_errno();
676 totlen = sizeof(itree);
678 SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
679 struct got_imsg_tree_entry *ite;
681 size_t len = sizeof(*ite) + strlen(te->name);
683 if (len > MAX_IMSGSIZE)
684 return got_error(GOT_ERR_NO_SPACE);
687 if (totlen + len >= MAX_IMSGSIZE - (IMSG_HEADER_SIZE * nimsg)) {
688 err = flush_imsg(ibuf);
696 return got_error_from_errno();
698 ite = (struct got_imsg_tree_entry *)buf;
699 memcpy(ite->id, te->id->sha1, sizeof(ite->id));
700 ite->mode = te->mode;
701 memcpy(buf + sizeof(*ite), te->name, strlen(te->name));
703 if (imsg_compose(ibuf, GOT_IMSG_TREE_ENTRY, 0, 0, -1,
705 err = got_error_from_errno();
712 return flush_imsg(ibuf);
715 const struct got_error *
716 got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
718 const struct got_error *err = NULL;
719 const size_t min_datalen =
720 MIN(sizeof(struct got_imsg_error),
721 sizeof(struct got_imsg_tree_object));
722 struct got_imsg_tree_object *itree;
727 err = read_imsg(ibuf);
735 struct got_imsg_tree_entry *ite;
736 struct got_tree_entry *te = NULL;
738 n = imsg_get(ibuf, &imsg);
740 if (*tree && (*tree)->entries.nentries != nentries)
745 if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen)
746 return got_error(GOT_ERR_PRIVSEP_LEN);
748 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
750 switch (imsg.hdr.type) {
752 err = recv_imsg_error(&imsg, datalen);
755 /* This message should only appear once. */
757 err = got_error(GOT_ERR_PRIVSEP_MSG);
760 if (datalen != sizeof(*itree)) {
761 err = got_error(GOT_ERR_PRIVSEP_LEN);
765 *tree = malloc(sizeof(**tree));
767 err = got_error_from_errno();
770 (*tree)->entries.nentries = itree->nentries;
771 SIMPLEQ_INIT(&(*tree)->entries.head);
774 case GOT_IMSG_TREE_ENTRY:
775 /* This message should be preceeded by GOT_IMSG_TREE. */
777 err = got_error(GOT_ERR_PRIVSEP_MSG);
780 if (datalen < sizeof(*ite) || datalen > MAX_IMSGSIZE) {
781 err = got_error(GOT_ERR_PRIVSEP_LEN);
785 /* Remaining data contains the entry's name. */
786 datalen -= sizeof(*ite);
787 if (datalen == 0 || datalen > MAX_IMSGSIZE) {
788 err = got_error(GOT_ERR_PRIVSEP_LEN);
793 te = got_alloc_tree_entry_partial();
795 err = got_error_from_errno();
798 te->name = malloc(datalen + 1);
799 if (te->name == NULL) {
801 err = got_error_from_errno();
804 memcpy(te->name, imsg.data + sizeof(*ite), datalen);
805 te->name[datalen] = '\0';
807 memcpy(te->id->sha1, ite->id, SHA1_DIGEST_LENGTH);
808 te->mode = ite->mode;
809 SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
813 err = got_error(GOT_ERR_PRIVSEP_MSG);
820 if (*tree && (*tree)->entries.nentries != nentries) {
822 err = got_error(GOT_ERR_PRIVSEP_LEN);
823 got_object_tree_close(*tree);
830 const struct got_error *
831 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size)
833 struct got_imsg_blob iblob;
836 /* Data has already been written to file descriptor. */
838 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob, sizeof(iblob))
840 return got_error_from_errno();
842 return flush_imsg(ibuf);
845 const struct got_error *
846 got_privsep_recv_blob(size_t *size, struct imsgbuf *ibuf)
848 const struct got_error *err = NULL;
850 struct got_imsg_blob *iblob;
853 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
857 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
859 switch (imsg.hdr.type) {
861 if (datalen != sizeof(*iblob)) {
862 err = got_error(GOT_ERR_PRIVSEP_LEN);
867 /* Data has been written to file descriptor. */
870 err = got_error(GOT_ERR_PRIVSEP_MSG);
879 static const struct got_error *
880 send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
882 const struct got_error *err = NULL;
883 size_t offset, remain;
888 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
890 if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
891 tag->tagmsg + offset, n) == -1) {
892 err = got_error_from_errno();
896 err = flush_imsg(ibuf);
907 const struct got_error *
908 got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
910 const struct got_error *err = NULL;
911 struct got_imsg_tag_object *itag;
914 size_t tag_len = strlen(tag->tag);
915 size_t tagger_len = strlen(tag->tagger);
916 size_t tagmsg_len = strlen(tag->tagmsg);
918 total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
922 return got_error_from_errno();
924 itag = (struct got_imsg_tag_object *)buf;
925 memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
926 itag->obj_type = tag->obj_type;
927 itag->tag_len = tag_len;
928 itag->tagger_len = tagger_len;
929 itag->tagger_time = tag->tagger_time;
930 itag->tagger_gmtoff = tag->tagger_gmtoff;
931 itag->tagmsg_len = tagmsg_len;
934 memcpy(buf + len, tag->tag, tag_len);
936 memcpy(buf + len, tag->tagger, tagger_len);
939 if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
940 err = got_error_from_errno();
944 if (tagmsg_len == 0 ||
945 tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
946 err = flush_imsg(ibuf);
950 err = send_tagmsg(ibuf, tag, tagmsg_len);
956 const struct got_error *
957 got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
959 const struct got_error *err = NULL;
961 struct got_imsg_tag_object *itag;
963 const size_t min_datalen =
964 MIN(sizeof(struct got_imsg_error),
965 sizeof(struct got_imsg_tag_object));
969 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
973 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
976 switch (imsg.hdr.type) {
978 if (datalen < sizeof(*itag)) {
979 err = got_error(GOT_ERR_PRIVSEP_LEN);
983 if (datalen != sizeof(*itag) + itag->tag_len +
985 err = got_error(GOT_ERR_PRIVSEP_LEN);
988 len += sizeof(*itag);
990 *tag = calloc(1, sizeof(**tag));
992 err = got_error_from_errno();
996 memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
998 if (itag->tag_len == 0) {
999 (*tag)->tag = strdup("");
1000 if ((*tag)->tag == NULL) {
1001 err = got_error_from_errno();
1005 (*tag)->tag = malloc(itag->tag_len + 1);
1006 if ((*tag)->tag == NULL) {
1007 err = got_error_from_errno();
1010 memcpy((*tag)->tag, imsg.data + len,
1012 (*tag)->tag[itag->tag_len] = '\0';
1014 len += itag->tag_len;
1016 (*tag)->obj_type = itag->obj_type;
1017 (*tag)->tagger_time = itag->tagger_time;
1018 (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1020 if (itag->tagger_len == 0) {
1021 (*tag)->tagger = strdup("");
1022 if ((*tag)->tagger == NULL) {
1023 err = got_error_from_errno();
1027 (*tag)->tagger = malloc(itag->tagger_len + 1);
1028 if ((*tag)->tagger == NULL) {
1029 err = got_error_from_errno();
1032 memcpy((*tag)->tagger, imsg.data + len,
1034 (*tag)->tagger[itag->tagger_len] = '\0';
1036 len += itag->tagger_len;
1038 if (itag->tagmsg_len == 0) {
1039 (*tag)->tagmsg = strdup("");
1040 if ((*tag)->tagmsg == NULL) {
1041 err = got_error_from_errno();
1045 size_t offset = 0, remain = itag->tagmsg_len;
1047 (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1048 if ((*tag)->tagmsg == NULL) {
1049 err = got_error_from_errno();
1052 while (remain > 0) {
1053 struct imsg imsg_log;
1054 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1057 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1061 if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1062 return got_error(GOT_ERR_PRIVSEP_MSG);
1064 memcpy((*tag)->tagmsg + offset, imsg_log.data,
1066 imsg_free(&imsg_log);
1070 (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1075 err = got_error(GOT_ERR_PRIVSEP_MSG);
1084 const struct got_error *
1085 got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1086 struct got_packidx *packidx)
1088 struct got_imsg_packidx ipackidx;
1089 struct got_imsg_pack ipack;
1092 ipackidx.len = packidx->len;
1093 fd = dup(packidx->fd);
1095 return got_error_from_errno();
1097 if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
1098 sizeof(ipackidx)) == -1)
1099 return got_error_from_errno();
1101 if (strlcpy(ipack.path_packfile, pack->path_packfile,
1102 sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
1103 return got_error(GOT_ERR_NO_SPACE);
1104 ipack.filesize = pack->filesize;
1108 return got_error_from_errno();
1110 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
1112 return got_error_from_errno();
1114 return flush_imsg(ibuf);
1117 const struct got_error *
1118 got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
1119 struct got_object_id *id)
1121 struct got_imsg_packed_object iobj;
1124 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
1126 if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
1127 &iobj, sizeof(iobj)) == -1)
1128 return got_error_from_errno();
1130 return flush_imsg(ibuf);