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_blob_req(struct imsgbuf *ibuf, int infd)
295 if (imsg_compose(ibuf, GOT_IMSG_BLOB_REQUEST, 0, 0, infd, NULL, 0)
297 return got_error_from_errno();
299 return flush_imsg(ibuf);
302 const struct got_error *
303 got_privsep_send_blob_outfd(struct imsgbuf *ibuf, int outfd)
305 if (imsg_compose(ibuf, GOT_IMSG_BLOB_OUTFD, 0, 0, outfd, NULL, 0)
307 return got_error_from_errno();
309 return flush_imsg(ibuf);
312 const struct got_error *
313 got_privsep_send_tmpfd(struct imsgbuf *ibuf, int fd)
315 if (imsg_compose(ibuf, GOT_IMSG_TMPFD, 0, 0, fd, NULL, 0)
317 return got_error_from_errno();
319 return flush_imsg(ibuf);
322 const struct got_error *
323 got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj)
325 struct got_imsg_object iobj;
327 memcpy(iobj.id, obj->id.sha1, sizeof(iobj.id));
328 iobj.type = obj->type;
329 iobj.flags = obj->flags;
330 iobj.hdrlen = obj->hdrlen;
331 iobj.size = obj->size;
332 if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
333 iobj.pack_offset = obj->pack_offset;
334 iobj.pack_idx = obj->pack_idx;
337 if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
339 return got_error_from_errno();
341 return flush_imsg(ibuf);
344 const struct got_error *
345 got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
346 struct imsgbuf *ibuf)
348 const struct got_error *err = NULL;
349 struct got_imsg_object *iobj;
350 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
352 if (datalen != sizeof(*iobj))
353 return got_error(GOT_ERR_PRIVSEP_LEN);
356 *obj = calloc(1, sizeof(**obj));
358 return got_error_from_errno();
360 memcpy((*obj)->id.sha1, iobj->id, SHA1_DIGEST_LENGTH);
361 (*obj)->type = iobj->type;
362 (*obj)->flags = iobj->flags;
363 (*obj)->hdrlen = iobj->hdrlen;
364 (*obj)->size = iobj->size;
365 /* path_packfile is handled by caller */
366 if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
367 (*obj)->pack_offset = iobj->pack_offset;
368 (*obj)->pack_idx = iobj->pack_idx;
374 const struct got_error *
375 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
377 const struct got_error *err = NULL;
380 const size_t min_datalen =
381 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
385 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
389 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
391 switch (imsg.hdr.type) {
392 case GOT_IMSG_OBJECT:
393 err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
396 err = got_error(GOT_ERR_PRIVSEP_MSG);
405 static const struct got_error *
406 send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
409 const struct got_error *err = NULL;
410 size_t offset, remain;
415 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
417 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
418 commit->logmsg + offset, n) == -1) {
419 err = got_error_from_errno();
423 err = flush_imsg(ibuf);
434 const struct got_error *
435 got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
437 const struct got_error *err = NULL;
438 struct got_imsg_commit_object *icommit;
441 struct got_object_qid *qid;
442 size_t author_len = strlen(commit->author);
443 size_t committer_len = strlen(commit->committer);
444 size_t logmsg_len = strlen(commit->logmsg);
446 total = sizeof(*icommit) + author_len + committer_len +
447 commit->nparents * SHA1_DIGEST_LENGTH;
451 return got_error_from_errno();
453 icommit = (struct got_imsg_commit_object *)buf;
454 memcpy(icommit->tree_id, commit->tree_id->sha1, sizeof(icommit->tree_id));
455 icommit->author_len = author_len;
456 icommit->author_time = commit->author_time;
457 icommit->author_gmtoff = commit->author_gmtoff;
458 icommit->committer_len = committer_len;
459 icommit->committer_time = commit->committer_time;
460 icommit->committer_gmtoff = commit->committer_gmtoff;
461 icommit->logmsg_len = logmsg_len;
462 icommit->nparents = commit->nparents;
464 len = sizeof(*icommit);
465 memcpy(buf + len, commit->author, author_len);
467 memcpy(buf + len, commit->committer, committer_len);
468 len += committer_len;
469 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
470 memcpy(buf + len, qid->id, SHA1_DIGEST_LENGTH);
471 len += SHA1_DIGEST_LENGTH;
474 if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
475 err = got_error_from_errno();
479 if (logmsg_len == 0 ||
480 logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
481 err = flush_imsg(ibuf);
485 err = send_commit_logmsg(ibuf, commit, logmsg_len);
491 const struct got_error *
492 got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
494 const struct got_error *err = NULL;
496 struct got_imsg_commit_object *icommit;
499 const size_t min_datalen =
500 MIN(sizeof(struct got_imsg_error),
501 sizeof(struct got_imsg_commit_object));
505 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
509 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
512 switch (imsg.hdr.type) {
513 case GOT_IMSG_COMMIT:
514 if (datalen < sizeof(*icommit)) {
515 err = got_error(GOT_ERR_PRIVSEP_LEN);
519 if (datalen != sizeof(*icommit) + icommit->author_len +
520 icommit->committer_len +
521 icommit->nparents * SHA1_DIGEST_LENGTH) {
522 err = got_error(GOT_ERR_PRIVSEP_LEN);
525 if (icommit->nparents < 0) {
526 err = got_error(GOT_ERR_PRIVSEP_LEN);
529 len += sizeof(*icommit);
531 *commit = got_object_commit_alloc_partial();
532 if (*commit == NULL) {
533 err = got_error_from_errno();
537 memcpy((*commit)->tree_id->sha1, icommit->tree_id,
539 (*commit)->author_time = icommit->author_time;
540 (*commit)->author_gmtoff = icommit->author_gmtoff;
541 (*commit)->committer_time = icommit->committer_time;
542 (*commit)->committer_gmtoff = icommit->committer_gmtoff;
544 if (icommit->author_len == 0) {
545 (*commit)->author = strdup("");
546 if ((*commit)->author == NULL) {
547 err = got_error_from_errno();
551 (*commit)->author = malloc(icommit->author_len + 1);
552 if ((*commit)->author == NULL) {
553 err = got_error_from_errno();
556 memcpy((*commit)->author, imsg.data + len,
557 icommit->author_len);
558 (*commit)->author[icommit->author_len] = '\0';
560 len += icommit->author_len;
562 if (icommit->committer_len == 0) {
563 (*commit)->committer = strdup("");
564 if ((*commit)->committer == NULL) {
565 err = got_error_from_errno();
569 (*commit)->committer =
570 malloc(icommit->committer_len + 1);
571 if ((*commit)->committer == NULL) {
572 err = got_error_from_errno();
575 memcpy((*commit)->committer, imsg.data + len,
576 icommit->committer_len);
577 (*commit)->committer[icommit->committer_len] = '\0';
579 len += icommit->committer_len;
581 if (icommit->logmsg_len == 0) {
582 (*commit)->logmsg = strdup("");
583 if ((*commit)->logmsg == NULL) {
584 err = got_error_from_errno();
588 size_t offset = 0, remain = icommit->logmsg_len;
590 (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
591 if ((*commit)->logmsg == NULL) {
592 err = got_error_from_errno();
596 struct imsg imsg_log;
597 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
600 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
604 if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG)
605 return got_error(GOT_ERR_PRIVSEP_MSG);
607 memcpy((*commit)->logmsg + offset,
609 imsg_free(&imsg_log);
613 (*commit)->logmsg[icommit->logmsg_len] = '\0';
616 for (i = 0; i < icommit->nparents; i++) {
617 struct got_object_qid *qid;
619 err = got_object_qid_alloc_partial(&qid);
622 memcpy(qid->id, imsg.data + len +
623 i * SHA1_DIGEST_LENGTH, sizeof(*qid->id));
624 SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
625 (*commit)->nparents++;
629 err = got_error(GOT_ERR_PRIVSEP_MSG);
638 const struct got_error *
639 got_privsep_send_tree(struct imsgbuf *ibuf, struct got_tree_object *tree)
641 const struct got_error *err = NULL;
642 struct got_imsg_tree_object itree;
643 struct got_tree_entry *te;
645 int nimsg; /* number of imsg queued in ibuf */
647 itree.nentries = tree->entries.nentries;
648 if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
650 return got_error_from_errno();
652 totlen = sizeof(itree);
654 SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
655 struct got_imsg_tree_entry *ite;
657 size_t len = sizeof(*ite) + strlen(te->name);
659 if (len > MAX_IMSGSIZE)
660 return got_error(GOT_ERR_NO_SPACE);
663 if (totlen + len >= MAX_IMSGSIZE - (IMSG_HEADER_SIZE * nimsg)) {
664 err = flush_imsg(ibuf);
672 return got_error_from_errno();
674 ite = (struct got_imsg_tree_entry *)buf;
675 memcpy(ite->id, te->id->sha1, sizeof(ite->id));
676 ite->mode = te->mode;
677 memcpy(buf + sizeof(*ite), te->name, strlen(te->name));
679 if (imsg_compose(ibuf, GOT_IMSG_TREE_ENTRY, 0, 0, -1,
681 err = got_error_from_errno();
688 return flush_imsg(ibuf);
691 const struct got_error *
692 got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
694 const struct got_error *err = NULL;
695 const size_t min_datalen =
696 MIN(sizeof(struct got_imsg_error),
697 sizeof(struct got_imsg_tree_object));
698 struct got_imsg_tree_object *itree;
703 err = read_imsg(ibuf);
711 struct got_imsg_tree_entry *ite;
712 struct got_tree_entry *te = NULL;
714 n = imsg_get(ibuf, &imsg);
716 if (*tree && (*tree)->entries.nentries != nentries)
721 if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen)
722 return got_error(GOT_ERR_PRIVSEP_LEN);
724 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
726 switch (imsg.hdr.type) {
728 err = recv_imsg_error(&imsg, datalen);
731 /* This message should only appear once. */
733 err = got_error(GOT_ERR_PRIVSEP_MSG);
736 if (datalen != sizeof(*itree)) {
737 err = got_error(GOT_ERR_PRIVSEP_LEN);
741 *tree = malloc(sizeof(**tree));
743 err = got_error_from_errno();
746 (*tree)->entries.nentries = itree->nentries;
747 SIMPLEQ_INIT(&(*tree)->entries.head);
750 case GOT_IMSG_TREE_ENTRY:
751 /* This message should be preceeded by GOT_IMSG_TREE. */
753 err = got_error(GOT_ERR_PRIVSEP_MSG);
756 if (datalen < sizeof(*ite) || datalen > MAX_IMSGSIZE) {
757 err = got_error(GOT_ERR_PRIVSEP_LEN);
761 /* Remaining data contains the entry's name. */
762 datalen -= sizeof(*ite);
763 if (datalen == 0 || datalen > MAX_IMSGSIZE) {
764 err = got_error(GOT_ERR_PRIVSEP_LEN);
769 te = got_alloc_tree_entry_partial();
771 err = got_error_from_errno();
774 te->name = malloc(datalen + 1);
775 if (te->name == NULL) {
777 err = got_error_from_errno();
780 memcpy(te->name, imsg.data + sizeof(*ite), datalen);
781 te->name[datalen] = '\0';
783 memcpy(te->id->sha1, ite->id, SHA1_DIGEST_LENGTH);
784 te->mode = ite->mode;
785 SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
789 err = got_error(GOT_ERR_PRIVSEP_MSG);
796 if (*tree && (*tree)->entries.nentries != nentries) {
798 err = got_error(GOT_ERR_PRIVSEP_LEN);
799 got_object_tree_close(*tree);
806 const struct got_error *
807 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size)
809 struct got_imsg_blob iblob;
812 /* Data has already been written to file descriptor. */
814 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob, sizeof(iblob))
816 return got_error_from_errno();
818 return flush_imsg(ibuf);
821 const struct got_error *
822 got_privsep_recv_blob(size_t *size, struct imsgbuf *ibuf)
824 const struct got_error *err = NULL;
826 struct got_imsg_blob *iblob;
829 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
833 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
835 switch (imsg.hdr.type) {
837 if (datalen != sizeof(*iblob)) {
838 err = got_error(GOT_ERR_PRIVSEP_LEN);
843 /* Data has been written to file descriptor. */
846 err = got_error(GOT_ERR_PRIVSEP_MSG);
855 static const struct got_error *
856 send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
858 const struct got_error *err = NULL;
859 size_t offset, remain;
864 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
866 if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
867 tag->tagmsg + offset, n) == -1) {
868 err = got_error_from_errno();
872 err = flush_imsg(ibuf);
883 const struct got_error *
884 got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
886 const struct got_error *err = NULL;
887 struct got_imsg_tag_object *itag;
890 size_t tag_len = strlen(tag->tag);
891 size_t tagger_len = strlen(tag->tagger);
892 size_t tagmsg_len = strlen(tag->tagmsg);
894 total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
898 return got_error_from_errno();
900 itag = (struct got_imsg_tag_object *)buf;
901 memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
902 itag->obj_type = tag->obj_type;
903 itag->tag_len = tag_len;
904 itag->tagger_len = tagger_len;
905 itag->tagger_time = tag->tagger_time;
906 itag->tagger_gmtoff = tag->tagger_gmtoff;
907 itag->tagmsg_len = tagmsg_len;
910 memcpy(buf + len, tag->tag, tag_len);
912 memcpy(buf + len, tag->tagger, tagger_len);
915 if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
916 err = got_error_from_errno();
920 if (tagmsg_len == 0 ||
921 tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
922 err = flush_imsg(ibuf);
926 err = send_tagmsg(ibuf, tag, tagmsg_len);
932 const struct got_error *
933 got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
935 const struct got_error *err = NULL;
937 struct got_imsg_tag_object *itag;
939 const size_t min_datalen =
940 MIN(sizeof(struct got_imsg_error),
941 sizeof(struct got_imsg_tag_object));
945 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
949 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
952 switch (imsg.hdr.type) {
954 if (datalen < sizeof(*itag)) {
955 err = got_error(GOT_ERR_PRIVSEP_LEN);
959 if (datalen != sizeof(*itag) + itag->tag_len +
961 err = got_error(GOT_ERR_PRIVSEP_LEN);
964 len += sizeof(*itag);
966 *tag = calloc(1, sizeof(**tag));
968 err = got_error_from_errno();
972 memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
974 if (itag->tag_len == 0) {
975 (*tag)->tag = strdup("");
976 if ((*tag)->tag == NULL) {
977 err = got_error_from_errno();
981 (*tag)->tag = malloc(itag->tag_len + 1);
982 if ((*tag)->tag == NULL) {
983 err = got_error_from_errno();
986 memcpy((*tag)->tag, imsg.data + len,
988 (*tag)->tag[itag->tag_len] = '\0';
990 len += itag->tag_len;
992 (*tag)->obj_type = itag->obj_type;
993 (*tag)->tagger_time = itag->tagger_time;
994 (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
996 if (itag->tagger_len == 0) {
997 (*tag)->tagger = strdup("");
998 if ((*tag)->tagger == NULL) {
999 err = got_error_from_errno();
1003 (*tag)->tagger = malloc(itag->tagger_len + 1);
1004 if ((*tag)->tagger == NULL) {
1005 err = got_error_from_errno();
1008 memcpy((*tag)->tagger, imsg.data + len,
1010 (*tag)->tagger[itag->tagger_len] = '\0';
1012 len += itag->tagger_len;
1014 if (itag->tagmsg_len == 0) {
1015 (*tag)->tagmsg = strdup("");
1016 if ((*tag)->tagmsg == NULL) {
1017 err = got_error_from_errno();
1021 size_t offset = 0, remain = itag->tagmsg_len;
1023 (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1024 if ((*tag)->tagmsg == NULL) {
1025 err = got_error_from_errno();
1028 while (remain > 0) {
1029 struct imsg imsg_log;
1030 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1033 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1037 if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1038 return got_error(GOT_ERR_PRIVSEP_MSG);
1040 memcpy((*tag)->tagmsg + offset, imsg_log.data,
1042 imsg_free(&imsg_log);
1046 (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1051 err = got_error(GOT_ERR_PRIVSEP_MSG);
1060 const struct got_error *
1061 got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1062 struct got_packidx *packidx)
1064 struct got_imsg_packidx ipackidx;
1065 struct got_imsg_pack ipack;
1068 ipackidx.len = packidx->len;
1069 fd = dup(packidx->fd);
1071 return got_error_from_errno();
1073 if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
1074 sizeof(ipackidx)) == -1)
1075 return got_error_from_errno();
1077 if (strlcpy(ipack.path_packfile, pack->path_packfile,
1078 sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
1079 return got_error(GOT_ERR_NO_SPACE);
1080 ipack.filesize = pack->filesize;
1084 return got_error_from_errno();
1086 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
1088 return got_error_from_errno();
1090 return flush_imsg(ibuf);
1093 const struct got_error *
1094 got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
1095 struct got_object_id *id)
1097 struct got_imsg_packed_object iobj;
1100 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
1102 if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
1103 &iobj, sizeof(iobj)) == -1)
1104 return got_error_from_errno();
1106 return flush_imsg(ibuf);