Blob


1 /*
2 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 *
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.
7 *
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.
15 */
17 #include <sys/types.h>
18 #include <sys/queue.h>
19 #include <sys/uio.h>
20 #include <sys/syslimits.h>
21 #include <sys/wait.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <errno.h>
27 #include <stdint.h>
28 #include <poll.h>
29 #include <imsg.h>
30 #include <sha1.h>
31 #include <zlib.h>
32 #include <time.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"
45 #ifndef MIN
46 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
47 #endif
49 static const struct got_error *
50 poll_fd(int fd, int events, int timeout)
51 {
52 struct pollfd pfd[1];
53 int n;
55 pfd[0].fd = fd;
56 pfd[0].events = events;
58 n = poll(pfd, 1, timeout);
59 if (n == -1)
60 return got_error_from_errno();
61 if (n == 0)
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))
66 return NULL;
68 return got_error(GOT_ERR_INTERRUPT);
69 }
71 static const struct got_error *
72 read_imsg(struct imsgbuf *ibuf)
73 {
74 const struct got_error *err;
75 size_t n;
77 err = poll_fd(ibuf->fd, POLLIN, INFTIM);
78 if (err)
79 return err;
81 n = imsg_read(ibuf);
82 if (n == -1) {
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);
86 }
87 if (n == 0)
88 return got_error(GOT_ERR_PRIVSEP_PIPE);
90 return NULL;
91 }
93 const struct got_error *
94 got_privsep_wait_for_child(pid_t pid)
95 {
96 int child_status;
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);
106 return NULL;
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);
117 ierr = imsg->data;
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);
122 return &serr;
125 return got_error(ierr->code);
128 const struct got_error *
129 got_privsep_recv_imsg(struct imsg *imsg, struct imsgbuf *ibuf,
130 size_t min_datalen)
132 const struct got_error *err;
133 ssize_t n;
135 n = imsg_get(ibuf, imsg);
136 if (n == -1)
137 return got_error_from_errno();
139 while (n == 0) {
140 err = read_imsg(ibuf);
141 if (err)
142 return err;
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);
154 return NULL;
157 /* Attempt to send an error in an imsg. Complain on stderr as a last resort. */
158 void
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;
163 int ret;
165 ierr.code = err->code;
166 if (err->code == GOT_ERR_ERRNO)
167 ierr.errno_code = errno;
168 else
169 ierr.errno_code = 0;
170 ret = imsg_compose(ibuf, GOT_IMSG_ERROR, 0, 0, -1, &ierr, sizeof(ierr));
171 if (ret == -1) {
172 fprintf(stderr, "%s: error %d \"%s\": imsg_compose: %s\n",
173 getprogname(), err->code, err->msg, strerror(errno));
174 return;
177 poll_err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
178 if (poll_err) {
179 fprintf(stderr, "%s: error %d \"%s\": poll: %s\n",
180 getprogname(), err->code, err->msg, poll_err->msg);
181 return;
184 ret = imsg_flush(ibuf);
185 if (ret == -1) {
186 fprintf(stderr, "%s: error %d \"%s\": imsg_flush: %s\n",
187 getprogname(), err->code, err->msg, strerror(errno));
188 return;
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);
198 if (err)
199 return err;
201 if (imsg_flush(ibuf) == -1)
202 return got_error_from_errno();
204 return NULL;
207 const struct got_error *
208 got_privsep_send_stop(int fd)
210 const struct got_error *err = NULL;
211 struct imsgbuf ibuf;
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);
219 imsg_clear(&ibuf);
220 return err;
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;
230 if (obj) {
231 switch (obj->type) {
232 case GOT_OBJ_TYPE_TREE:
233 imsg_code = GOT_IMSG_TREE_REQUEST;
234 break;
235 case GOT_OBJ_TYPE_COMMIT:
236 abort(); /* should not get here */
237 break;
238 case GOT_OBJ_TYPE_BLOB:
239 imsg_code = GOT_IMSG_BLOB_REQUEST;
240 break;
241 case GOT_OBJ_TYPE_TAG:
242 imsg_code = GOT_IMSG_TAG_REQUEST;
243 break;
244 default:
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;
258 iobjp = &iobj;
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;
273 size_t len;
275 if (id) { /* commit is packed */
276 iobj.idx = pack_idx;
277 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
278 iobjp = &iobj;
279 len = sizeof(iobj);
280 } else {
281 iobjp = NULL;
282 len = 0;
285 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_REQUEST, 0, 0, fd, iobjp, len)
286 == -1)
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;
297 size_t len;
299 if (id) { /* tree is packed */
300 iobj.idx = pack_idx;
301 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
302 iobjp = &iobj;
303 len = sizeof(iobj);
304 } else {
305 iobjp = NULL;
306 len = 0;
309 if (imsg_compose(ibuf, GOT_IMSG_TREE_REQUEST, 0, 0, fd, iobjp, len)
310 == -1)
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)
320 == -1)
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)
330 == -1)
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)
340 == -1)
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))
362 == -1)
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);
378 iobj = imsg->data;
380 *obj = calloc(1, sizeof(**obj));
381 if (*obj == NULL)
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;
395 return err;
398 const struct got_error *
399 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
401 const struct got_error *err = NULL;
402 struct imsg imsg;
403 size_t datalen;
404 const size_t min_datalen =
405 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
407 *obj = NULL;
409 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
410 if (err)
411 return err;
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);
418 break;
419 default:
420 err = got_error(GOT_ERR_PRIVSEP_MSG);
421 break;
424 imsg_free(&imsg);
426 return err;
429 static const struct got_error *
430 send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
431 size_t logmsg_len)
433 const struct got_error *err = NULL;
434 size_t offset, remain;
436 offset = 0;
437 remain = logmsg_len;
438 while (remain > 0) {
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();
444 break;
447 err = flush_imsg(ibuf);
448 if (err)
449 break;
451 offset += n;
452 remain -= n;
455 return err;
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;
463 uint8_t *buf;
464 size_t len, total;
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;
473 buf = malloc(total);
474 if (buf == NULL)
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);
490 len += 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();
500 goto done;
503 if (logmsg_len == 0 ||
504 logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
505 err = flush_imsg(ibuf);
506 if (err)
507 goto done;
509 err = send_commit_logmsg(ibuf, commit, logmsg_len);
510 done:
511 free(buf);
512 return err;
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;
519 struct imsg imsg;
520 struct got_imsg_commit_object *icommit;
521 size_t len, datalen;
522 int i;
523 const size_t min_datalen =
524 MIN(sizeof(struct got_imsg_error),
525 sizeof(struct got_imsg_commit_object));
527 *commit = NULL;
529 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
530 if (err)
531 return err;
533 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
534 len = 0;
536 switch (imsg.hdr.type) {
537 case GOT_IMSG_COMMIT:
538 if (datalen < sizeof(*icommit)) {
539 err = got_error(GOT_ERR_PRIVSEP_LEN);
540 break;
542 icommit = imsg.data;
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);
547 break;
549 if (icommit->nparents < 0) {
550 err = got_error(GOT_ERR_PRIVSEP_LEN);
551 break;
553 len += sizeof(*icommit);
555 *commit = got_object_commit_alloc_partial();
556 if (*commit == NULL) {
557 err = got_error_from_errno();
558 break;
561 memcpy((*commit)->tree_id->sha1, icommit->tree_id,
562 SHA1_DIGEST_LENGTH);
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();
572 break;
574 } else {
575 (*commit)->author = malloc(icommit->author_len + 1);
576 if ((*commit)->author == NULL) {
577 err = got_error_from_errno();
578 break;
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();
590 break;
592 } else {
593 (*commit)->committer =
594 malloc(icommit->committer_len + 1);
595 if ((*commit)->committer == NULL) {
596 err = got_error_from_errno();
597 break;
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();
609 break;
611 } else {
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();
617 break;
619 while (remain > 0) {
620 struct imsg imsg_log;
621 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
622 remain);
624 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
625 if (err)
626 return err;
628 if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG)
629 return got_error(GOT_ERR_PRIVSEP_MSG);
631 memcpy((*commit)->logmsg + offset,
632 imsg_log.data, n);
633 imsg_free(&imsg_log);
634 offset += n;
635 remain -= n;
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);
644 if (err)
645 break;
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++;
651 break;
652 default:
653 err = got_error(GOT_ERR_PRIVSEP_MSG);
654 break;
657 imsg_free(&imsg);
659 return err;
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;
668 size_t totlen;
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))
673 == -1)
674 return got_error_from_errno();
676 totlen = sizeof(itree);
677 nimsg = 1;
678 SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
679 struct got_imsg_tree_entry *ite;
680 uint8_t *buf = NULL;
681 size_t len = sizeof(*ite) + strlen(te->name);
683 if (len > MAX_IMSGSIZE)
684 return got_error(GOT_ERR_NO_SPACE);
686 nimsg++;
687 if (totlen + len >= MAX_IMSGSIZE - (IMSG_HEADER_SIZE * nimsg)) {
688 err = flush_imsg(ibuf);
689 if (err)
690 return err;
691 nimsg = 0;
694 buf = malloc(len);
695 if (buf == NULL)
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,
704 buf, len) == -1)
705 err = got_error_from_errno();
706 free(buf);
707 if (err)
708 return err;
709 totlen += len;
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;
723 int nentries = 0;
725 *tree = NULL;
726 get_more:
727 err = read_imsg(ibuf);
728 if (err)
729 goto done;
731 while (1) {
732 struct imsg imsg;
733 size_t n;
734 size_t datalen;
735 struct got_imsg_tree_entry *ite;
736 struct got_tree_entry *te = NULL;
738 n = imsg_get(ibuf, &imsg);
739 if (n == 0) {
740 if (*tree && (*tree)->entries.nentries != nentries)
741 goto get_more;
742 break;
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) {
751 case GOT_IMSG_ERROR:
752 err = recv_imsg_error(&imsg, datalen);
753 break;
754 case GOT_IMSG_TREE:
755 /* This message should only appear once. */
756 if (*tree != NULL) {
757 err = got_error(GOT_ERR_PRIVSEP_MSG);
758 break;
760 if (datalen != sizeof(*itree)) {
761 err = got_error(GOT_ERR_PRIVSEP_LEN);
762 break;
764 itree = imsg.data;
765 *tree = malloc(sizeof(**tree));
766 if (*tree == NULL) {
767 err = got_error_from_errno();
768 break;
770 (*tree)->entries.nentries = itree->nentries;
771 SIMPLEQ_INIT(&(*tree)->entries.head);
772 (*tree)->refcnt = 0;
773 break;
774 case GOT_IMSG_TREE_ENTRY:
775 /* This message should be preceeded by GOT_IMSG_TREE. */
776 if (*tree == NULL) {
777 err = got_error(GOT_ERR_PRIVSEP_MSG);
778 break;
780 if (datalen < sizeof(*ite) || datalen > MAX_IMSGSIZE) {
781 err = got_error(GOT_ERR_PRIVSEP_LEN);
782 break;
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);
789 break;
791 ite = imsg.data;
793 te = got_alloc_tree_entry_partial();
794 if (te == NULL) {
795 err = got_error_from_errno();
796 break;
798 te->name = malloc(datalen + 1);
799 if (te->name == NULL) {
800 free(te);
801 err = got_error_from_errno();
802 break;
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);
810 nentries++;
811 break;
812 default:
813 err = got_error(GOT_ERR_PRIVSEP_MSG);
814 break;
817 imsg_free(&imsg);
819 done:
820 if (*tree && (*tree)->entries.nentries != nentries) {
821 if (err == NULL)
822 err = got_error(GOT_ERR_PRIVSEP_LEN);
823 got_object_tree_close(*tree);
824 *tree = NULL;
827 return err;
830 const struct got_error *
831 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size)
833 struct got_imsg_blob iblob;
835 iblob.size = size;
836 /* Data has already been written to file descriptor. */
838 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob, sizeof(iblob))
839 == -1)
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;
849 struct imsg imsg;
850 struct got_imsg_blob *iblob;
851 size_t datalen;
853 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
854 if (err)
855 return err;
857 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
859 switch (imsg.hdr.type) {
860 case GOT_IMSG_BLOB:
861 if (datalen != sizeof(*iblob)) {
862 err = got_error(GOT_ERR_PRIVSEP_LEN);
863 break;
865 iblob = imsg.data;
866 *size = iblob->size;
867 /* Data has been written to file descriptor. */
868 break;
869 default:
870 err = got_error(GOT_ERR_PRIVSEP_MSG);
871 break;
874 imsg_free(&imsg);
876 return err;
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;
885 offset = 0;
886 remain = tagmsg_len;
887 while (remain > 0) {
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();
893 break;
896 err = flush_imsg(ibuf);
897 if (err)
898 break;
900 offset += n;
901 remain -= n;
904 return err;
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;
912 uint8_t *buf;
913 size_t len, total;
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;
920 buf = malloc(total);
921 if (buf == NULL)
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;
933 len = sizeof(*itag);
934 memcpy(buf + len, tag->tag, tag_len);
935 len += tag_len;
936 memcpy(buf + len, tag->tagger, tagger_len);
937 len += tagger_len;
939 if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
940 err = got_error_from_errno();
941 goto done;
944 if (tagmsg_len == 0 ||
945 tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
946 err = flush_imsg(ibuf);
947 if (err)
948 goto done;
950 err = send_tagmsg(ibuf, tag, tagmsg_len);
951 done:
952 free(buf);
953 return err;
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;
960 struct imsg imsg;
961 struct got_imsg_tag_object *itag;
962 size_t len, datalen;
963 const size_t min_datalen =
964 MIN(sizeof(struct got_imsg_error),
965 sizeof(struct got_imsg_tag_object));
967 *tag = NULL;
969 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
970 if (err)
971 return err;
973 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
974 len = 0;
976 switch (imsg.hdr.type) {
977 case GOT_IMSG_TAG:
978 if (datalen < sizeof(*itag)) {
979 err = got_error(GOT_ERR_PRIVSEP_LEN);
980 break;
982 itag = imsg.data;
983 if (datalen != sizeof(*itag) + itag->tag_len +
984 itag->tagger_len) {
985 err = got_error(GOT_ERR_PRIVSEP_LEN);
986 break;
988 len += sizeof(*itag);
990 *tag = calloc(1, sizeof(**tag));
991 if (*tag == NULL) {
992 err = got_error_from_errno();
993 break;
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();
1002 break;
1004 } else {
1005 (*tag)->tag = malloc(itag->tag_len + 1);
1006 if ((*tag)->tag == NULL) {
1007 err = got_error_from_errno();
1008 break;
1010 memcpy((*tag)->tag, imsg.data + len,
1011 itag->tag_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();
1024 break;
1026 } else {
1027 (*tag)->tagger = malloc(itag->tagger_len + 1);
1028 if ((*tag)->tagger == NULL) {
1029 err = got_error_from_errno();
1030 break;
1032 memcpy((*tag)->tagger, imsg.data + len,
1033 itag->tagger_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();
1042 break;
1044 } else {
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();
1050 break;
1052 while (remain > 0) {
1053 struct imsg imsg_log;
1054 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1055 remain);
1057 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1058 if (err)
1059 return err;
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,
1065 n);
1066 imsg_free(&imsg_log);
1067 offset += n;
1068 remain -= n;
1070 (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1073 break;
1074 default:
1075 err = got_error(GOT_ERR_PRIVSEP_MSG);
1076 break;
1079 imsg_free(&imsg);
1081 return err;
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;
1090 int fd;
1092 ipackidx.len = packidx->len;
1093 fd = dup(packidx->fd);
1094 if (fd == -1)
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;
1106 fd = dup(pack->fd);
1107 if (fd == -1)
1108 return got_error_from_errno();
1110 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
1111 == -1)
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;
1123 iobj.idx = idx;
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);