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,
111 size_t min_datalen)
113 const struct got_error *err;
114 ssize_t n;
116 n = imsg_get(ibuf, imsg);
117 if (n == -1)
118 return got_error_from_errno();
120 while (n == 0) {
121 err = read_imsg(ibuf);
122 if (err)
123 return err;
124 n = imsg_get(ibuf, imsg);
127 if (imsg->hdr.len < IMSG_HEADER_SIZE + min_datalen)
128 return got_error(GOT_ERR_PRIVSEP_LEN);
130 return NULL;
133 static const struct got_error *
134 recv_imsg_error(struct imsg *imsg, size_t datalen)
136 struct got_imsg_error *ierr;
138 if (datalen != sizeof(*ierr))
139 return got_error(GOT_ERR_PRIVSEP_LEN);
141 ierr = imsg->data;
142 if (ierr->code == GOT_ERR_ERRNO) {
143 static struct got_error serr;
144 serr.code = GOT_ERR_ERRNO;
145 serr.msg = strerror(ierr->errno_code);
146 return &serr;
149 return got_error(ierr->code);
152 /* Attempt to send an error in an imsg. Complain on stderr as a last resort. */
153 void
154 got_privsep_send_error(struct imsgbuf *ibuf, const struct got_error *err)
156 const struct got_error *poll_err;
157 struct got_imsg_error ierr;
158 int ret;
160 ierr.code = err->code;
161 if (err->code == GOT_ERR_ERRNO)
162 ierr.errno_code = errno;
163 else
164 ierr.errno_code = 0;
165 ret = imsg_compose(ibuf, GOT_IMSG_ERROR, 0, 0, -1, &ierr, sizeof(ierr));
166 if (ret == -1) {
167 fprintf(stderr, "%s: error %d \"%s\": imsg_compose: %s\n",
168 getprogname(), err->code, err->msg, strerror(errno));
169 return;
172 poll_err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
173 if (poll_err) {
174 fprintf(stderr, "%s: error %d \"%s\": poll: %s\n",
175 getprogname(), err->code, err->msg, poll_err->msg);
176 return;
179 ret = imsg_flush(ibuf);
180 if (ret == -1) {
181 fprintf(stderr, "%s: error %d \"%s\": imsg_flush: %s\n",
182 getprogname(), err->code, err->msg, strerror(errno));
183 return;
187 static const struct got_error *
188 flush_imsg(struct imsgbuf *ibuf)
190 const struct got_error *err;
192 err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
193 if (err)
194 return err;
196 if (imsg_flush(ibuf) == -1)
197 return got_error_from_errno();
199 return NULL;
202 const struct got_error *
203 got_privsep_send_stop(int fd)
205 const struct got_error *err = NULL;
206 struct imsgbuf ibuf;
208 imsg_init(&ibuf, fd);
210 if (imsg_compose(&ibuf, GOT_IMSG_STOP, 0, 0, -1, NULL, 0) == -1)
211 return got_error_from_errno();
213 err = flush_imsg(&ibuf);
214 imsg_clear(&ibuf);
215 return err;
218 const struct got_error *
219 got_privsep_send_obj_req(struct imsgbuf *ibuf, int fd, struct got_object *obj)
221 struct got_imsg_object iobj, *iobjp = NULL;
222 size_t iobj_size = 0;
223 int imsg_code = GOT_IMSG_OBJECT_REQUEST;
225 if (obj) {
226 switch (obj->type) {
227 case GOT_OBJ_TYPE_TREE:
228 imsg_code = GOT_IMSG_TREE_REQUEST;
229 break;
230 case GOT_OBJ_TYPE_COMMIT:
231 imsg_code = GOT_IMSG_COMMIT_REQUEST;
232 break;
233 case GOT_OBJ_TYPE_BLOB:
234 imsg_code = GOT_IMSG_BLOB_REQUEST;
235 break;
236 default:
237 return got_error(GOT_ERR_OBJ_TYPE);
240 memcpy(iobj.id, obj->id.sha1, sizeof(iobj.id));
241 iobj.type = obj->type;
242 iobj.flags = obj->flags;
243 iobj.hdrlen = obj->hdrlen;
244 iobj.size = obj->size;
245 if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
246 iobj.pack_offset = obj->pack_offset;
247 iobj.pack_idx = obj->pack_idx;
250 iobjp = &iobj;
251 iobj_size = sizeof(iobj);
254 if (imsg_compose(ibuf, imsg_code, 0, 0, fd, iobjp, iobj_size) == -1)
255 return got_error_from_errno();
257 return flush_imsg(ibuf);
260 const struct got_error *
261 got_privsep_send_blob_req(struct imsgbuf *ibuf, int infd)
263 if (imsg_compose(ibuf, GOT_IMSG_BLOB_REQUEST, 0, 0, infd, NULL, 0)
264 == -1)
265 return got_error_from_errno();
267 return flush_imsg(ibuf);
270 const struct got_error *
271 got_privsep_send_blob_outfd(struct imsgbuf *ibuf, int outfd)
273 if (imsg_compose(ibuf, GOT_IMSG_BLOB_OUTFD, 0, 0, outfd, NULL, 0)
274 == -1)
275 return got_error_from_errno();
277 return flush_imsg(ibuf);
280 const struct got_error *
281 got_privsep_send_tmpfd(struct imsgbuf *ibuf, int fd)
283 if (imsg_compose(ibuf, GOT_IMSG_TMPFD, 0, 0, fd, NULL, 0)
284 == -1)
285 return got_error_from_errno();
287 return flush_imsg(ibuf);
290 const struct got_error *
291 got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj)
293 struct got_imsg_object iobj;
295 memcpy(iobj.id, obj->id.sha1, sizeof(iobj.id));
296 iobj.type = obj->type;
297 iobj.flags = obj->flags;
298 iobj.hdrlen = obj->hdrlen;
299 iobj.size = obj->size;
300 if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
301 iobj.pack_offset = obj->pack_offset;
302 iobj.pack_idx = obj->pack_idx;
305 if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
306 == -1)
307 return got_error_from_errno();
309 return flush_imsg(ibuf);
312 const struct got_error *
313 got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
314 struct imsgbuf *ibuf)
316 const struct got_error *err = NULL;
317 struct got_imsg_object *iobj;
318 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
320 if (datalen != sizeof(*iobj))
321 return got_error(GOT_ERR_PRIVSEP_LEN);
322 iobj = imsg->data;
324 *obj = calloc(1, sizeof(**obj));
325 if (*obj == NULL)
326 return got_error_from_errno();
328 memcpy((*obj)->id.sha1, iobj->id, SHA1_DIGEST_LENGTH);
329 (*obj)->type = iobj->type;
330 (*obj)->flags = iobj->flags;
331 (*obj)->hdrlen = iobj->hdrlen;
332 (*obj)->size = iobj->size;
333 /* path_packfile is handled by caller */
334 if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
335 (*obj)->pack_offset = iobj->pack_offset;
336 (*obj)->pack_idx = iobj->pack_idx;
339 return err;
342 const struct got_error *
343 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
345 const struct got_error *err = NULL;
346 struct imsg imsg;
347 size_t datalen;
348 const size_t min_datalen =
349 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
351 *obj = NULL;
353 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
354 if (err)
355 return err;
357 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
359 switch (imsg.hdr.type) {
360 case GOT_IMSG_ERROR:
361 err = recv_imsg_error(&imsg, datalen);
362 break;
363 case GOT_IMSG_OBJECT:
364 err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
365 break;
366 default:
367 err = got_error(GOT_ERR_PRIVSEP_MSG);
368 break;
371 imsg_free(&imsg);
373 return err;
376 static const struct got_error *
377 send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
378 size_t logmsg_len)
380 const struct got_error *err = NULL;
381 size_t offset, remain;
383 offset = 0;
384 remain = logmsg_len;
385 while (remain > 0) {
386 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
388 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
389 commit->logmsg + offset, n) == -1) {
390 err = got_error_from_errno();
391 break;
394 err = flush_imsg(ibuf);
395 if (err)
396 break;
398 offset += n;
399 remain -= n;
402 return err;
405 const struct got_error *
406 got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
408 const struct got_error *err = NULL;
409 struct got_imsg_commit_object *icommit;
410 uint8_t *buf;
411 size_t len, total;
412 struct got_object_qid *qid;
413 size_t author_len = strlen(commit->author);
414 size_t committer_len = strlen(commit->committer);
415 size_t logmsg_len = strlen(commit->logmsg);
417 total = sizeof(*icommit) + author_len + committer_len +
418 commit->nparents * SHA1_DIGEST_LENGTH;
420 buf = malloc(total);
421 if (buf == NULL)
422 return got_error_from_errno();
424 icommit = (struct got_imsg_commit_object *)buf;
425 memcpy(icommit->tree_id, commit->tree_id->sha1, sizeof(icommit->tree_id));
426 icommit->author_len = author_len;
427 icommit->author_time = commit->author_time;
428 icommit->author_gmtoff = commit->author_gmtoff;
429 icommit->committer_len = committer_len;
430 icommit->committer_time = commit->committer_time;
431 icommit->committer_gmtoff = commit->committer_gmtoff;
432 icommit->logmsg_len = logmsg_len;
433 icommit->nparents = commit->nparents;
435 len = sizeof(*icommit);
436 memcpy(buf + len, commit->author, author_len);
437 len += author_len;
438 memcpy(buf + len, commit->committer, committer_len);
439 len += committer_len;
440 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
441 memcpy(buf + len, qid->id, SHA1_DIGEST_LENGTH);
442 len += SHA1_DIGEST_LENGTH;
445 if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
446 err = got_error_from_errno();
447 goto done;
450 if (logmsg_len == 0 ||
451 logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
452 err = flush_imsg(ibuf);
453 if (err)
454 goto done;
456 err = send_commit_logmsg(ibuf, commit, logmsg_len);
457 done:
458 free(buf);
459 return err;
462 const struct got_error *
463 got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
465 const struct got_error *err = NULL;
466 struct imsg imsg;
467 struct got_imsg_commit_object *icommit;
468 size_t len, datalen;
469 int i;
470 const size_t min_datalen =
471 MIN(sizeof(struct got_imsg_error),
472 sizeof(struct got_imsg_commit_object));
474 *commit = NULL;
476 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
477 if (err)
478 return err;
480 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
481 len = 0;
483 switch (imsg.hdr.type) {
484 case GOT_IMSG_ERROR:
485 err = recv_imsg_error(&imsg, datalen);
486 break;
487 case GOT_IMSG_COMMIT:
488 if (datalen < sizeof(*icommit)) {
489 err = got_error(GOT_ERR_PRIVSEP_LEN);
490 break;
492 icommit = imsg.data;
493 if (datalen != sizeof(*icommit) + icommit->author_len +
494 icommit->committer_len +
495 icommit->nparents * SHA1_DIGEST_LENGTH) {
496 err = got_error(GOT_ERR_PRIVSEP_LEN);
497 break;
499 if (icommit->nparents < 0) {
500 err = got_error(GOT_ERR_PRIVSEP_LEN);
501 break;
503 len += sizeof(*icommit);
505 *commit = got_object_commit_alloc_partial();
506 if (*commit == NULL) {
507 err = got_error_from_errno();
508 break;
511 memcpy((*commit)->tree_id->sha1, icommit->tree_id,
512 SHA1_DIGEST_LENGTH);
513 (*commit)->author_time = icommit->author_time;
514 (*commit)->author_gmtoff = icommit->author_gmtoff;
515 (*commit)->committer_time = icommit->committer_time;
516 (*commit)->committer_gmtoff = icommit->committer_gmtoff;
518 if (icommit->author_len == 0) {
519 (*commit)->author = strdup("");
520 if ((*commit)->author == NULL) {
521 err = got_error_from_errno();
522 break;
524 } else {
525 (*commit)->author = malloc(icommit->author_len + 1);
526 if ((*commit)->author == NULL) {
527 err = got_error_from_errno();
528 break;
530 memcpy((*commit)->author, imsg.data + len,
531 icommit->author_len);
532 (*commit)->author[icommit->author_len] = '\0';
534 len += icommit->author_len;
536 if (icommit->committer_len == 0) {
537 (*commit)->committer = strdup("");
538 if ((*commit)->committer == NULL) {
539 err = got_error_from_errno();
540 break;
542 } else {
543 (*commit)->committer =
544 malloc(icommit->committer_len + 1);
545 if ((*commit)->committer == NULL) {
546 err = got_error_from_errno();
547 break;
549 memcpy((*commit)->committer, imsg.data + len,
550 icommit->committer_len);
551 (*commit)->committer[icommit->committer_len] = '\0';
553 len += icommit->committer_len;
555 if (icommit->logmsg_len == 0) {
556 (*commit)->logmsg = strdup("");
557 if ((*commit)->logmsg == NULL) {
558 err = got_error_from_errno();
559 break;
561 } else {
562 size_t offset = 0, remain = icommit->logmsg_len;
564 (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
565 if ((*commit)->logmsg == NULL) {
566 err = got_error_from_errno();
567 break;
569 while (remain > 0) {
570 struct imsg imsg_log;
571 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
572 remain);
574 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
575 if (err)
576 return err;
578 if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG)
579 return got_error(GOT_ERR_PRIVSEP_MSG);
581 memcpy((*commit)->logmsg + offset,
582 imsg_log.data, n);
583 imsg_free(&imsg_log);
584 offset += n;
585 remain -= n;
587 (*commit)->logmsg[icommit->logmsg_len] = '\0';
590 for (i = 0; i < icommit->nparents; i++) {
591 struct got_object_qid *qid;
593 err = got_object_qid_alloc_partial(&qid);
594 if (err)
595 break;
596 memcpy(qid->id, imsg.data + len +
597 i * SHA1_DIGEST_LENGTH, sizeof(*qid->id));
598 SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
599 (*commit)->nparents++;
601 break;
602 default:
603 err = got_error(GOT_ERR_PRIVSEP_MSG);
604 break;
607 imsg_free(&imsg);
609 return err;
612 const struct got_error *
613 got_privsep_send_tree(struct imsgbuf *ibuf, struct got_tree_object *tree)
615 const struct got_error *err = NULL;
616 struct got_imsg_tree_object itree;
617 struct got_tree_entry *te;
618 size_t totlen;
619 int nimsg; /* number of imsg queued in ibuf */
621 itree.nentries = tree->entries.nentries;
622 if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
623 == -1)
624 return got_error_from_errno();
626 totlen = sizeof(itree);
627 nimsg = 1;
628 SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
629 struct got_imsg_tree_entry *ite;
630 uint8_t *buf = NULL;
631 size_t len = sizeof(*ite) + strlen(te->name);
633 if (len > MAX_IMSGSIZE)
634 return got_error(GOT_ERR_NO_SPACE);
636 nimsg++;
637 if (totlen + len >= MAX_IMSGSIZE - (IMSG_HEADER_SIZE * nimsg)) {
638 err = flush_imsg(ibuf);
639 if (err)
640 return err;
641 nimsg = 0;
644 buf = malloc(len);
645 if (buf == NULL)
646 return got_error_from_errno();
648 ite = (struct got_imsg_tree_entry *)buf;
649 memcpy(ite->id, te->id->sha1, sizeof(ite->id));
650 ite->mode = te->mode;
651 memcpy(buf + sizeof(*ite), te->name, strlen(te->name));
653 if (imsg_compose(ibuf, GOT_IMSG_TREE_ENTRY, 0, 0, -1,
654 buf, len) == -1)
655 err = got_error_from_errno();
656 free(buf);
657 if (err)
658 return err;
659 totlen += len;
662 return flush_imsg(ibuf);
665 const struct got_error *
666 got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
668 const struct got_error *err = NULL;
669 const size_t min_datalen =
670 MIN(sizeof(struct got_imsg_error),
671 sizeof(struct got_imsg_tree_object));
672 struct got_imsg_tree_object *itree;
673 int nentries = 0;
675 *tree = NULL;
676 get_more:
677 err = read_imsg(ibuf);
678 if (err)
679 goto done;
681 while (1) {
682 struct imsg imsg;
683 size_t n;
684 size_t datalen;
685 struct got_imsg_tree_entry *ite;
686 struct got_tree_entry *te = NULL;
688 n = imsg_get(ibuf, &imsg);
689 if (n == 0) {
690 if (*tree && (*tree)->entries.nentries != nentries)
691 goto get_more;
692 break;
695 if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen)
696 return got_error(GOT_ERR_PRIVSEP_LEN);
698 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
700 switch (imsg.hdr.type) {
701 case GOT_IMSG_ERROR:
702 err = recv_imsg_error(&imsg, datalen);
703 break;
704 case GOT_IMSG_TREE:
705 /* This message should only appear once. */
706 if (*tree != NULL) {
707 err = got_error(GOT_ERR_PRIVSEP_MSG);
708 break;
710 if (datalen != sizeof(*itree)) {
711 err = got_error(GOT_ERR_PRIVSEP_LEN);
712 break;
714 itree = imsg.data;
715 *tree = malloc(sizeof(**tree));
716 if (*tree == NULL) {
717 err = got_error_from_errno();
718 break;
720 (*tree)->entries.nentries = itree->nentries;
721 SIMPLEQ_INIT(&(*tree)->entries.head);
722 (*tree)->refcnt = 0;
723 break;
724 case GOT_IMSG_TREE_ENTRY:
725 /* This message should be preceeded by GOT_IMSG_TREE. */
726 if (*tree == NULL) {
727 err = got_error(GOT_ERR_PRIVSEP_MSG);
728 break;
730 if (datalen < sizeof(*ite) || datalen > MAX_IMSGSIZE) {
731 err = got_error(GOT_ERR_PRIVSEP_LEN);
732 break;
735 /* Remaining data contains the entry's name. */
736 datalen -= sizeof(*ite);
737 if (datalen == 0 || datalen > MAX_IMSGSIZE) {
738 err = got_error(GOT_ERR_PRIVSEP_LEN);
739 break;
741 ite = imsg.data;
743 te = got_alloc_tree_entry_partial();
744 if (te == NULL) {
745 err = got_error_from_errno();
746 break;
748 te->name = malloc(datalen + 1);
749 if (te->name == NULL) {
750 free(te);
751 err = got_error_from_errno();
752 break;
754 memcpy(te->name, imsg.data + sizeof(*ite), datalen);
755 te->name[datalen] = '\0';
757 memcpy(te->id->sha1, ite->id, SHA1_DIGEST_LENGTH);
758 te->mode = ite->mode;
759 SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
760 nentries++;
761 break;
762 default:
763 err = got_error(GOT_ERR_PRIVSEP_MSG);
764 break;
767 imsg_free(&imsg);
769 done:
770 if (*tree && (*tree)->entries.nentries != nentries) {
771 if (err == NULL)
772 err = got_error(GOT_ERR_PRIVSEP_LEN);
773 got_object_tree_close(*tree);
774 *tree = NULL;
777 return err;
780 const struct got_error *
781 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size)
783 struct got_imsg_blob iblob;
785 iblob.size = size;
786 /* Data has already been written to file descriptor. */
788 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob, sizeof(iblob))
789 == -1)
790 return got_error_from_errno();
792 return flush_imsg(ibuf);
795 const struct got_error *
796 got_privsep_recv_blob(size_t *size, struct imsgbuf *ibuf)
798 const struct got_error *err = NULL;
799 struct imsg imsg;
800 struct got_imsg_blob *iblob;
801 size_t datalen;
803 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
804 if (err)
805 return err;
807 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
809 switch (imsg.hdr.type) {
810 case GOT_IMSG_ERROR:
811 err = recv_imsg_error(&imsg, datalen);
812 break;
813 case GOT_IMSG_BLOB:
814 if (datalen != sizeof(*iblob)) {
815 err = got_error(GOT_ERR_PRIVSEP_LEN);
816 break;
818 iblob = imsg.data;
819 *size = iblob->size;
820 /* Data has been written to file descriptor. */
821 break;
822 default:
823 err = got_error(GOT_ERR_PRIVSEP_MSG);
824 break;
827 imsg_free(&imsg);
829 return err;
832 const struct got_error *
833 got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
834 struct got_packidx *packidx)
836 struct got_imsg_packidx ipackidx;
837 struct got_imsg_pack ipack;
838 int fd;
840 ipackidx.len = packidx->len;
841 fd = dup(packidx->fd);
842 if (fd == -1)
843 return got_error_from_errno();
845 if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
846 sizeof(ipackidx)) == -1)
847 return got_error_from_errno();
849 if (strlcpy(ipack.path_packfile, pack->path_packfile,
850 sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
851 return got_error(GOT_ERR_NO_SPACE);
852 ipack.filesize = pack->filesize;
854 fd = dup(pack->fd);
855 if (fd == -1)
856 return got_error_from_errno();
858 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
859 == -1)
860 return got_error_from_errno();
862 return flush_imsg(ibuf);
865 const struct got_error *
866 got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
867 struct got_object_id *id)
869 struct got_imsg_packed_object iobj;
871 iobj.idx = idx;
872 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
874 if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
875 &iobj, sizeof(iobj)) == -1)
876 return got_error_from_errno();
878 return flush_imsg(ibuf);