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 const struct got_error *
110 got_privsep_recv_imsg(struct imsg *imsg, struct imsgbuf *ibuf, size_t min_datalen)
112 const struct got_error *err;
113 ssize_t n;
115 n = imsg_get(ibuf, imsg);
116 if (n == -1)
117 return got_error_from_errno();
119 while (n == 0) {
120 err = read_imsg(ibuf);
121 if (err)
122 return err;
123 n = imsg_get(ibuf, imsg);
126 if (imsg->hdr.len < IMSG_HEADER_SIZE + min_datalen)
127 return got_error(GOT_ERR_PRIVSEP_LEN);
129 return NULL;
132 static const struct got_error *
133 recv_imsg_error(struct imsg *imsg, size_t datalen)
135 struct got_imsg_error ierr;
137 if (datalen != sizeof(ierr))
138 return got_error(GOT_ERR_PRIVSEP_LEN);
140 memcpy(&ierr, imsg->data, sizeof(ierr));
141 if (ierr.code == GOT_ERR_ERRNO) {
142 static struct got_error serr;
143 serr.code = GOT_ERR_ERRNO;
144 serr.msg = strerror(ierr.errno_code);
145 return &serr;
148 return got_error(ierr.code);
151 /* Attempt to send an error in an imsg. Complain on stderr as a last resort. */
152 void
153 got_privsep_send_error(struct imsgbuf *ibuf, const struct got_error *err)
155 const struct got_error *poll_err;
156 struct got_imsg_error ierr;
157 int ret;
159 ierr.code = err->code;
160 if (err->code == GOT_ERR_ERRNO)
161 ierr.errno_code = errno;
162 else
163 ierr.errno_code = 0;
164 ret = imsg_compose(ibuf, GOT_IMSG_ERROR, 0, 0, -1, &ierr, sizeof(ierr));
165 if (ret != -1) {
166 fprintf(stderr, "%s: error %d \"%s\": imsg_compose: %s\n",
167 getprogname(), err->code, err->msg, strerror(errno));
168 return;
171 poll_err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
172 if (poll_err) {
173 fprintf(stderr, "%s: error %d \"%s\": poll: %s\n",
174 getprogname(), err->code, err->msg, poll_err->msg);
175 return;
178 ret = imsg_flush(ibuf);
179 if (ret == -1) {
180 fprintf(stderr, "%s: error %d \"%s\": imsg_flush: %s\n",
181 getprogname(), err->code, err->msg, strerror(errno));
182 return;
186 static const struct got_error *
187 flush_imsg(struct imsgbuf *ibuf)
189 const struct got_error *err;
191 err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
192 if (err)
193 return err;
195 if (imsg_flush(ibuf) == -1)
196 return got_error_from_errno();
198 return NULL;
201 const struct got_error *
202 got_privsep_send_stop(int fd)
204 const struct got_error *err = NULL;
205 struct imsgbuf ibuf;
207 imsg_init(&ibuf, fd);
209 if (imsg_compose(&ibuf, GOT_IMSG_STOP, 0, 0, -1, NULL, 0) == -1)
210 return got_error_from_errno();
212 err = flush_imsg(&ibuf);
213 imsg_clear(&ibuf);
214 return err;
217 static void
218 init_imsg_object(struct got_imsg_object *iobj, struct got_object *obj)
220 memcpy(iobj->id, obj->id.sha1, sizeof(iobj->id));
221 iobj->type = obj->type;
222 iobj->flags = obj->flags;
223 iobj->hdrlen = obj->hdrlen;
224 iobj->size = obj->size;
225 if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
226 iobj->pack_offset = obj->pack_offset;
227 iobj->pack_idx = obj->pack_idx;
231 const struct got_error *
232 got_privsep_send_obj_req(struct imsgbuf *ibuf, int fd, struct got_object *obj)
234 struct got_imsg_object iobj, *iobjp = NULL;
235 size_t iobj_size = 0;
236 int imsg_code = GOT_IMSG_OBJECT_REQUEST;
238 if (obj) {
239 switch (obj->type) {
240 case GOT_OBJ_TYPE_TREE:
241 imsg_code = GOT_IMSG_TREE_REQUEST;
242 break;
243 case GOT_OBJ_TYPE_COMMIT:
244 imsg_code = GOT_IMSG_COMMIT_REQUEST;
245 break;
246 case GOT_OBJ_TYPE_BLOB:
247 imsg_code = GOT_IMSG_BLOB_REQUEST;
248 break;
249 default:
250 return got_error(GOT_ERR_OBJ_TYPE);
253 init_imsg_object(&iobj, obj);
255 iobjp = &iobj;
256 iobj_size = sizeof(iobj);
259 if (imsg_compose(ibuf, imsg_code, 0, 0, fd, iobjp, iobj_size) == -1)
260 return got_error_from_errno();
262 return flush_imsg(ibuf);
265 const struct got_error *
266 got_privsep_send_mini_commit_req(struct imsgbuf *ibuf, int fd,
267 struct got_object *obj)
269 struct got_imsg_object iobj, *iobjp;
270 size_t iobj_size;
272 if (obj->type != GOT_OBJ_TYPE_COMMIT)
273 return got_error(GOT_ERR_OBJ_TYPE);
275 init_imsg_object(&iobj, obj);
277 iobjp = &iobj;
278 iobj_size = sizeof(iobj);
280 if (imsg_compose(ibuf, GOT_IMSG_MINI_COMMIT_REQUEST, 0, 0, fd,
281 iobjp, iobj_size) == -1)
282 return got_error_from_errno();
284 return flush_imsg(ibuf);
287 const struct got_error *
288 got_privsep_send_blob_req(struct imsgbuf *ibuf, int infd)
290 if (imsg_compose(ibuf, GOT_IMSG_BLOB_REQUEST, 0, 0, infd, NULL, 0)
291 == -1)
292 return got_error_from_errno();
294 return flush_imsg(ibuf);
297 const struct got_error *
298 got_privsep_send_blob_outfd(struct imsgbuf *ibuf, int outfd)
300 if (imsg_compose(ibuf, GOT_IMSG_BLOB_OUTFD, 0, 0, outfd, NULL, 0)
301 == -1)
302 return got_error_from_errno();
304 return flush_imsg(ibuf);
307 const struct got_error *
308 got_privsep_send_tmpfd(struct imsgbuf *ibuf, int fd)
310 if (imsg_compose(ibuf, GOT_IMSG_TMPFD, 0, 0, fd, NULL, 0)
311 == -1)
312 return got_error_from_errno();
314 return flush_imsg(ibuf);
317 const struct got_error *
318 got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj)
320 struct got_imsg_object iobj;
322 memcpy(iobj.id, obj->id.sha1, sizeof(iobj.id));
323 iobj.type = obj->type;
324 iobj.flags = obj->flags;
325 iobj.hdrlen = obj->hdrlen;
326 iobj.size = obj->size;
327 if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
328 iobj.pack_offset = obj->pack_offset;
329 iobj.pack_idx = obj->pack_idx;
332 if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
333 == -1)
334 return got_error_from_errno();
336 return flush_imsg(ibuf);
339 const struct got_error *
340 got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
341 struct imsgbuf *ibuf)
343 const struct got_error *err = NULL;
344 struct got_imsg_object iobj;
345 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
347 if (datalen != sizeof(iobj))
348 return got_error(GOT_ERR_PRIVSEP_LEN);
350 memcpy(&iobj, imsg->data, sizeof(iobj));
352 *obj = calloc(1, sizeof(**obj));
353 if (*obj == NULL)
354 return got_error_from_errno();
356 memcpy((*obj)->id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
357 (*obj)->type = iobj.type;
358 (*obj)->flags = iobj.flags;
359 (*obj)->hdrlen = iobj.hdrlen;
360 (*obj)->size = iobj.size;
361 /* path_packfile is handled by caller */
362 if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
363 (*obj)->pack_offset = iobj.pack_offset;
364 (*obj)->pack_idx = iobj.pack_idx;
367 return err;
370 const struct got_error *
371 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
373 const struct got_error *err = NULL;
374 struct imsg imsg;
375 size_t datalen;
376 const size_t min_datalen =
377 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
379 *obj = NULL;
381 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
382 if (err)
383 return err;
385 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
387 switch (imsg.hdr.type) {
388 case GOT_IMSG_ERROR:
389 err = recv_imsg_error(&imsg, datalen);
390 break;
391 case GOT_IMSG_OBJECT:
392 err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
393 break;
394 default:
395 err = got_error(GOT_ERR_PRIVSEP_MSG);
396 break;
399 imsg_free(&imsg);
401 return err;
404 static const struct got_error *
405 send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
406 size_t logmsg_len)
408 const struct got_error *err = NULL;
409 size_t offset, remain;
411 offset = 0;
412 remain = logmsg_len;
413 while (remain > 0) {
414 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
416 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
417 commit->logmsg + offset, n) == -1) {
418 err = got_error_from_errno();
419 break;
422 err = flush_imsg(ibuf);
423 if (err)
424 break;
426 offset += n;
427 remain -= n;
430 return err;
433 const struct got_error *
434 got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
436 const struct got_error *err = NULL;
437 struct got_imsg_commit_object icommit;
438 uint8_t *buf;
439 size_t len, total;
440 struct got_object_qid *qid;
441 size_t logmsg_len = strlen(commit->logmsg);
443 memcpy(icommit.tree_id, commit->tree_id->sha1, sizeof(icommit.tree_id));
444 icommit.author_len = strlen(commit->author);
445 memcpy(&icommit.tm_author, &commit->tm_author,
446 sizeof(icommit.tm_author));
447 icommit.committer_len = strlen(commit->committer);
448 memcpy(&icommit.tm_committer, &commit->tm_committer,
449 sizeof(icommit.tm_committer));
450 icommit.logmsg_len = logmsg_len;
451 icommit.nparents = commit->nparents;
453 total = sizeof(icommit) + icommit.author_len +
454 icommit.committer_len + icommit.nparents * SHA1_DIGEST_LENGTH;
456 buf = malloc(total);
457 if (buf == NULL)
458 return got_error_from_errno();
460 len = 0;
461 memcpy(buf + len, &icommit, sizeof(icommit));
462 len += sizeof(icommit);
463 memcpy(buf + len, commit->author, icommit.author_len);
464 len += icommit.author_len;
465 memcpy(buf + len, commit->committer, icommit.committer_len);
466 len += icommit.committer_len;
467 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
468 memcpy(buf + len, qid->id, SHA1_DIGEST_LENGTH);
469 len += SHA1_DIGEST_LENGTH;
472 if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
473 err = got_error_from_errno();
474 goto done;
477 if (logmsg_len == 0 ||
478 logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
479 err = flush_imsg(ibuf);
480 if (err)
481 goto done;
483 err = send_commit_logmsg(ibuf, commit, logmsg_len);
484 done:
485 free(buf);
486 return err;
489 const struct got_error *
490 got_privsep_send_mini_commit(struct imsgbuf *ibuf,
491 struct got_commit_object_mini *commit)
493 const struct got_error *err = NULL;
494 struct got_imsg_commit_object_mini icommit;
495 uint8_t *buf;
496 size_t len, total;
497 struct got_object_qid *qid;
499 memcpy(icommit.tree_id, commit->tree_id->sha1, sizeof(icommit.tree_id));
500 memcpy(&icommit.tm_committer, &commit->tm_committer,
501 sizeof(icommit.tm_committer));
502 icommit.nparents = commit->nparents;
504 total = sizeof(icommit) + icommit.nparents * SHA1_DIGEST_LENGTH;
506 buf = malloc(total);
507 if (buf == NULL)
508 return got_error_from_errno();
510 len = 0;
511 memcpy(buf + len, &icommit, sizeof(icommit));
512 len += sizeof(icommit);
513 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
514 memcpy(buf + len, qid->id, SHA1_DIGEST_LENGTH);
515 len += SHA1_DIGEST_LENGTH;
518 if (imsg_compose(ibuf, GOT_IMSG_MINI_COMMIT, 0, 0, -1,
519 buf, len) == -1) {
520 err = got_error_from_errno();
523 free(buf);
524 if (err)
525 return err;
526 return flush_imsg(ibuf);
529 const struct got_error *
530 got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
532 const struct got_error *err = NULL;
533 struct imsg imsg;
534 struct got_imsg_commit_object icommit;
535 size_t len, datalen;
536 int i;
537 const size_t min_datalen =
538 MIN(sizeof(struct got_imsg_error),
539 sizeof(struct got_imsg_commit_object));
540 uint8_t *data;
542 *commit = NULL;
544 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
545 if (err)
546 return err;
548 data = imsg.data;
549 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
550 len = 0;
552 switch (imsg.hdr.type) {
553 case GOT_IMSG_ERROR:
554 err = recv_imsg_error(&imsg, datalen);
555 break;
556 case GOT_IMSG_COMMIT:
557 if (datalen < sizeof(icommit)) {
558 err = got_error(GOT_ERR_PRIVSEP_LEN);
559 break;
562 memcpy(&icommit, data, sizeof(icommit));
563 if (datalen != sizeof(icommit) + icommit.author_len +
564 icommit.committer_len +
565 icommit.nparents * SHA1_DIGEST_LENGTH) {
566 err = got_error(GOT_ERR_PRIVSEP_LEN);
567 break;
569 if (icommit.nparents < 0) {
570 err = got_error(GOT_ERR_PRIVSEP_LEN);
571 break;
573 len += sizeof(icommit);
575 *commit = got_object_commit_alloc_partial();
576 if (*commit == NULL) {
577 err = got_error_from_errno();
578 break;
581 memcpy((*commit)->tree_id->sha1, icommit.tree_id,
582 SHA1_DIGEST_LENGTH);
583 memcpy(&(*commit)->tm_author, &icommit.tm_author,
584 sizeof((*commit)->tm_author));
585 memcpy(&(*commit)->tm_committer, &icommit.tm_committer,
586 sizeof((*commit)->tm_committer));
588 if (icommit.author_len == 0) {
589 (*commit)->author = strdup("");
590 if ((*commit)->author == NULL) {
591 err = got_error_from_errno();
592 break;
594 } else {
595 (*commit)->author = malloc(icommit.author_len + 1);
596 if ((*commit)->author == NULL) {
597 err = got_error_from_errno();
598 break;
600 memcpy((*commit)->author, data + len,
601 icommit.author_len);
602 (*commit)->author[icommit.author_len] = '\0';
604 len += icommit.author_len;
606 if (icommit.committer_len == 0) {
607 (*commit)->committer = strdup("");
608 if ((*commit)->committer == NULL) {
609 err = got_error_from_errno();
610 break;
612 } else {
613 (*commit)->committer =
614 malloc(icommit.committer_len + 1);
615 if ((*commit)->committer == NULL) {
616 err = got_error_from_errno();
617 break;
619 memcpy((*commit)->committer, data + len,
620 icommit.committer_len);
621 (*commit)->committer[icommit.committer_len] = '\0';
623 len += icommit.committer_len;
625 if (icommit.logmsg_len == 0) {
626 (*commit)->logmsg = strdup("");
627 if ((*commit)->logmsg == NULL) {
628 err = got_error_from_errno();
629 break;
631 } else {
632 size_t offset = 0, remain = icommit.logmsg_len;
634 (*commit)->logmsg = malloc(icommit.logmsg_len + 1);
635 if ((*commit)->logmsg == NULL) {
636 err = got_error_from_errno();
637 break;
639 while (remain > 0) {
640 struct imsg imsg_log;
641 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
642 remain);
644 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
645 if (err)
646 return err;
648 if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG)
649 return got_error(GOT_ERR_PRIVSEP_MSG);
651 memcpy((*commit)->logmsg + offset,
652 imsg_log.data, n);
653 imsg_free(&imsg_log);
654 offset += n;
655 remain -= n;
657 (*commit)->logmsg[icommit.logmsg_len] = '\0';
660 for (i = 0; i < icommit.nparents; i++) {
661 struct got_object_qid *qid;
663 err = got_object_qid_alloc_partial(&qid);
664 if (err)
665 break;
667 memcpy(qid->id, data + len + i * SHA1_DIGEST_LENGTH,
668 sizeof(*qid->id));
669 SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
670 (*commit)->nparents++;
672 break;
673 default:
674 err = got_error(GOT_ERR_PRIVSEP_MSG);
675 break;
678 imsg_free(&imsg);
680 return err;
683 const struct got_error *
684 got_privsep_recv_mini_commit(struct got_commit_object_mini **commit,
685 struct imsgbuf *ibuf)
687 const struct got_error *err = NULL;
688 struct imsg imsg;
689 struct got_imsg_commit_object_mini icommit;
690 size_t len, datalen;
691 int i;
692 const size_t min_datalen =
693 MIN(sizeof(struct got_imsg_error),
694 sizeof(struct got_imsg_commit_object_mini));
695 uint8_t *data;
697 *commit = NULL;
699 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
700 if (err)
701 return err;
703 data = imsg.data;
704 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
705 len = 0;
707 switch (imsg.hdr.type) {
708 case GOT_IMSG_ERROR:
709 err = recv_imsg_error(&imsg, datalen);
710 break;
711 case GOT_IMSG_MINI_COMMIT:
712 if (datalen < sizeof(icommit)) {
713 err = got_error(GOT_ERR_PRIVSEP_LEN);
714 break;
717 memcpy(&icommit, data, sizeof(icommit));
718 if (datalen != sizeof(icommit) +
719 icommit.nparents * SHA1_DIGEST_LENGTH) {
720 err = got_error(GOT_ERR_PRIVSEP_LEN);
721 break;
723 if (icommit.nparents < 0) {
724 err = got_error(GOT_ERR_PRIVSEP_LEN);
725 break;
727 len += sizeof(icommit);
729 *commit = got_object_mini_commit_alloc_partial();
730 if (*commit == NULL) {
731 err = got_error_from_errno();
732 break;
735 memcpy((*commit)->tree_id->sha1, icommit.tree_id,
736 SHA1_DIGEST_LENGTH);
737 memcpy(&(*commit)->tm_committer, &icommit.tm_committer,
738 sizeof((*commit)->tm_committer));
740 for (i = 0; i < icommit.nparents; i++) {
741 struct got_object_qid *qid;
743 err = got_object_qid_alloc_partial(&qid);
744 if (err)
745 break;
747 memcpy(qid->id, data + len + i * SHA1_DIGEST_LENGTH,
748 sizeof(*qid->id));
749 SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
750 (*commit)->nparents++;
752 break;
753 default:
754 err = got_error(GOT_ERR_PRIVSEP_MSG);
755 break;
758 imsg_free(&imsg);
760 return err;
763 const struct got_error *
764 got_privsep_send_tree(struct imsgbuf *ibuf, struct got_tree_object *tree)
766 const struct got_error *err = NULL;
767 struct got_imsg_tree_object itree;
768 struct got_tree_entry *te;
769 size_t totlen;
770 int nimsg; /* number of imsg queued in ibuf */
772 itree.nentries = tree->entries.nentries;
773 if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
774 == -1)
775 return got_error_from_errno();
777 totlen = sizeof(itree);
778 nimsg = 1;
779 SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
780 struct got_imsg_tree_entry ite;
781 uint8_t *buf = NULL;
782 size_t len = sizeof(ite) + strlen(te->name);
784 if (len > MAX_IMSGSIZE)
785 return got_error(GOT_ERR_NO_SPACE);
787 nimsg++;
788 if (totlen + len >= MAX_IMSGSIZE - (IMSG_HEADER_SIZE * nimsg)) {
789 err = flush_imsg(ibuf);
790 if (err)
791 return err;
792 nimsg = 0;
795 buf = malloc(len);
796 if (buf == NULL)
797 return got_error_from_errno();
799 memcpy(ite.id, te->id->sha1, sizeof(ite.id));
800 ite.mode = te->mode;
801 memcpy(buf, &ite, sizeof(ite));
802 memcpy(buf + sizeof(ite), te->name, strlen(te->name));
804 if (imsg_compose(ibuf, GOT_IMSG_TREE_ENTRY, 0, 0, -1,
805 buf, len) == -1)
806 err = got_error_from_errno();
807 free(buf);
808 if (err)
809 return err;
810 totlen += len;
813 return flush_imsg(ibuf);
816 const struct got_error *
817 got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
819 const struct got_error *err = NULL;
820 const size_t min_datalen =
821 MIN(sizeof(struct got_imsg_error),
822 sizeof(struct got_imsg_tree_object));
823 struct got_imsg_tree_object itree = { 0 };
824 int nentries = 0;
826 *tree = NULL;
827 get_more:
828 err = read_imsg(ibuf);
829 if (err)
830 goto done;
832 while (1) {
833 struct imsg imsg;
834 size_t n;
835 size_t datalen;
836 struct got_imsg_tree_entry ite;
837 struct got_tree_entry *te = NULL;
839 n = imsg_get(ibuf, &imsg);
840 if (n == 0) {
841 if (*tree && (*tree)->entries.nentries != nentries)
842 goto get_more;
843 break;
846 if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen)
847 return got_error(GOT_ERR_PRIVSEP_LEN);
849 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
851 switch (imsg.hdr.type) {
852 case GOT_IMSG_ERROR:
853 err = recv_imsg_error(&imsg, datalen);
854 break;
855 case GOT_IMSG_TREE:
856 /* This message should only appear once. */
857 if (*tree != NULL) {
858 err = got_error(GOT_ERR_PRIVSEP_MSG);
859 break;
861 if (datalen != sizeof(itree)) {
862 err = got_error(GOT_ERR_PRIVSEP_LEN);
863 break;
865 memcpy(&itree, imsg.data, sizeof(itree));
866 *tree = calloc(1, sizeof(**tree));
867 if (*tree == NULL) {
868 err = got_error_from_errno();
869 break;
871 (*tree)->entries.nentries = itree.nentries;
872 SIMPLEQ_INIT(&(*tree)->entries.head);
873 break;
874 case GOT_IMSG_TREE_ENTRY:
875 /* This message should be preceeded by GOT_IMSG_TREE. */
876 if (*tree == NULL) {
877 err = got_error(GOT_ERR_PRIVSEP_MSG);
878 break;
880 if (datalen < sizeof(ite) || datalen > MAX_IMSGSIZE) {
881 err = got_error(GOT_ERR_PRIVSEP_LEN);
882 break;
885 /* Remaining data contains the entry's name. */
886 datalen -= sizeof(ite);
887 memcpy(&ite, imsg.data, sizeof(ite));
888 if (datalen == 0 || datalen > MAX_IMSGSIZE) {
889 err = got_error(GOT_ERR_PRIVSEP_LEN);
890 break;
893 te = got_alloc_tree_entry_partial();
894 if (te == NULL) {
895 err = got_error_from_errno();
896 break;
898 te->name = malloc(datalen + 1);
899 if (te->name == NULL) {
900 free(te);
901 err = got_error_from_errno();
902 break;
904 memcpy(te->name, imsg.data + sizeof(ite), datalen);
905 te->name[datalen] = '\0';
907 memcpy(te->id->sha1, ite.id, SHA1_DIGEST_LENGTH);
908 te->mode = ite.mode;
909 SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
910 nentries++;
911 break;
912 default:
913 err = got_error(GOT_ERR_PRIVSEP_MSG);
914 break;
917 imsg_free(&imsg);
919 done:
920 if (*tree && (*tree)->entries.nentries != nentries) {
921 if (err == NULL)
922 err = got_error(GOT_ERR_PRIVSEP_LEN);
923 got_object_tree_close(*tree);
924 *tree = NULL;
927 return err;
930 const struct got_error *
931 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size)
933 struct got_imsg_blob iblob;
935 iblob.size = size;
936 /* Data has already been written to file descriptor. */
938 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob, sizeof(iblob))
939 == -1)
940 return got_error_from_errno();
942 return flush_imsg(ibuf);
945 const struct got_error *
946 got_privsep_recv_blob(size_t *size, struct imsgbuf *ibuf)
948 const struct got_error *err = NULL;
949 struct imsg imsg;
950 struct got_imsg_blob iblob;
951 size_t datalen;
953 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
954 if (err)
955 return err;
957 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
959 switch (imsg.hdr.type) {
960 case GOT_IMSG_ERROR:
961 err = recv_imsg_error(&imsg, datalen);
962 break;
963 case GOT_IMSG_BLOB:
964 if (datalen != sizeof(iblob)) {
965 err = got_error(GOT_ERR_PRIVSEP_LEN);
966 break;
968 memcpy(&iblob, imsg.data, sizeof(iblob));
969 *size = iblob.size;
970 /* Data has been written to file descriptor. */
971 break;
972 default:
973 err = got_error(GOT_ERR_PRIVSEP_MSG);
974 break;
977 imsg_free(&imsg);
979 return err;
982 const struct got_error *
983 got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
984 struct got_packidx *packidx)
986 struct got_imsg_packidx ipackidx;
987 struct got_imsg_pack ipack;
988 int fd;
990 ipackidx.len = packidx->len;
991 fd = dup(packidx->fd);
992 if (fd == -1)
993 return got_error_from_errno();
995 if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
996 sizeof(ipackidx)) == -1)
997 return got_error_from_errno();
999 if (strlcpy(ipack.path_packfile, pack->path_packfile,
1000 sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
1001 return got_error(GOT_ERR_NO_SPACE);
1002 ipack.filesize = pack->filesize;
1004 fd = dup(pack->fd);
1005 if (fd == -1)
1006 return got_error_from_errno();
1008 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
1009 == -1)
1010 return got_error_from_errno();
1012 return flush_imsg(ibuf);
1015 const struct got_error *
1016 got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
1017 struct got_object_id *id)
1019 struct got_imsg_packed_object iobj;
1021 iobj.idx = idx;
1022 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
1024 if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
1025 &iobj, sizeof(iobj)) == -1)
1026 return got_error_from_errno();
1028 return flush_imsg(ibuf);