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_blob_req(struct imsgbuf *ibuf, int infd)
295 if (imsg_compose(ibuf, GOT_IMSG_BLOB_REQUEST, 0, 0, infd, NULL, 0)
296 == -1)
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)
306 == -1)
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)
316 == -1)
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))
338 == -1)
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);
354 iobj = imsg->data;
356 *obj = calloc(1, sizeof(**obj));
357 if (*obj == NULL)
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;
371 return err;
374 const struct got_error *
375 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
377 const struct got_error *err = NULL;
378 struct imsg imsg;
379 size_t datalen;
380 const size_t min_datalen =
381 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
383 *obj = NULL;
385 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
386 if (err)
387 return err;
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);
394 break;
395 default:
396 err = got_error(GOT_ERR_PRIVSEP_MSG);
397 break;
400 imsg_free(&imsg);
402 return err;
405 static const struct got_error *
406 send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
407 size_t logmsg_len)
409 const struct got_error *err = NULL;
410 size_t offset, remain;
412 offset = 0;
413 remain = logmsg_len;
414 while (remain > 0) {
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();
420 break;
423 err = flush_imsg(ibuf);
424 if (err)
425 break;
427 offset += n;
428 remain -= n;
431 return err;
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;
439 uint8_t *buf;
440 size_t len, total;
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;
449 buf = malloc(total);
450 if (buf == NULL)
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);
466 len += 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();
476 goto done;
479 if (logmsg_len == 0 ||
480 logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
481 err = flush_imsg(ibuf);
482 if (err)
483 goto done;
485 err = send_commit_logmsg(ibuf, commit, logmsg_len);
486 done:
487 free(buf);
488 return err;
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;
495 struct imsg imsg;
496 struct got_imsg_commit_object *icommit;
497 size_t len, datalen;
498 int i;
499 const size_t min_datalen =
500 MIN(sizeof(struct got_imsg_error),
501 sizeof(struct got_imsg_commit_object));
503 *commit = NULL;
505 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
506 if (err)
507 return err;
509 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
510 len = 0;
512 switch (imsg.hdr.type) {
513 case GOT_IMSG_COMMIT:
514 if (datalen < sizeof(*icommit)) {
515 err = got_error(GOT_ERR_PRIVSEP_LEN);
516 break;
518 icommit = imsg.data;
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);
523 break;
525 if (icommit->nparents < 0) {
526 err = got_error(GOT_ERR_PRIVSEP_LEN);
527 break;
529 len += sizeof(*icommit);
531 *commit = got_object_commit_alloc_partial();
532 if (*commit == NULL) {
533 err = got_error_from_errno();
534 break;
537 memcpy((*commit)->tree_id->sha1, icommit->tree_id,
538 SHA1_DIGEST_LENGTH);
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();
548 break;
550 } else {
551 (*commit)->author = malloc(icommit->author_len + 1);
552 if ((*commit)->author == NULL) {
553 err = got_error_from_errno();
554 break;
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();
566 break;
568 } else {
569 (*commit)->committer =
570 malloc(icommit->committer_len + 1);
571 if ((*commit)->committer == NULL) {
572 err = got_error_from_errno();
573 break;
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();
585 break;
587 } else {
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();
593 break;
595 while (remain > 0) {
596 struct imsg imsg_log;
597 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
598 remain);
600 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
601 if (err)
602 return err;
604 if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG)
605 return got_error(GOT_ERR_PRIVSEP_MSG);
607 memcpy((*commit)->logmsg + offset,
608 imsg_log.data, n);
609 imsg_free(&imsg_log);
610 offset += n;
611 remain -= n;
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);
620 if (err)
621 break;
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++;
627 break;
628 default:
629 err = got_error(GOT_ERR_PRIVSEP_MSG);
630 break;
633 imsg_free(&imsg);
635 return err;
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;
644 size_t totlen;
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))
649 == -1)
650 return got_error_from_errno();
652 totlen = sizeof(itree);
653 nimsg = 1;
654 SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
655 struct got_imsg_tree_entry *ite;
656 uint8_t *buf = NULL;
657 size_t len = sizeof(*ite) + strlen(te->name);
659 if (len > MAX_IMSGSIZE)
660 return got_error(GOT_ERR_NO_SPACE);
662 nimsg++;
663 if (totlen + len >= MAX_IMSGSIZE - (IMSG_HEADER_SIZE * nimsg)) {
664 err = flush_imsg(ibuf);
665 if (err)
666 return err;
667 nimsg = 0;
670 buf = malloc(len);
671 if (buf == NULL)
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,
680 buf, len) == -1)
681 err = got_error_from_errno();
682 free(buf);
683 if (err)
684 return err;
685 totlen += len;
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;
699 int nentries = 0;
701 *tree = NULL;
702 get_more:
703 err = read_imsg(ibuf);
704 if (err)
705 goto done;
707 while (1) {
708 struct imsg imsg;
709 size_t n;
710 size_t datalen;
711 struct got_imsg_tree_entry *ite;
712 struct got_tree_entry *te = NULL;
714 n = imsg_get(ibuf, &imsg);
715 if (n == 0) {
716 if (*tree && (*tree)->entries.nentries != nentries)
717 goto get_more;
718 break;
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) {
727 case GOT_IMSG_ERROR:
728 err = recv_imsg_error(&imsg, datalen);
729 break;
730 case GOT_IMSG_TREE:
731 /* This message should only appear once. */
732 if (*tree != NULL) {
733 err = got_error(GOT_ERR_PRIVSEP_MSG);
734 break;
736 if (datalen != sizeof(*itree)) {
737 err = got_error(GOT_ERR_PRIVSEP_LEN);
738 break;
740 itree = imsg.data;
741 *tree = malloc(sizeof(**tree));
742 if (*tree == NULL) {
743 err = got_error_from_errno();
744 break;
746 (*tree)->entries.nentries = itree->nentries;
747 SIMPLEQ_INIT(&(*tree)->entries.head);
748 (*tree)->refcnt = 0;
749 break;
750 case GOT_IMSG_TREE_ENTRY:
751 /* This message should be preceeded by GOT_IMSG_TREE. */
752 if (*tree == NULL) {
753 err = got_error(GOT_ERR_PRIVSEP_MSG);
754 break;
756 if (datalen < sizeof(*ite) || datalen > MAX_IMSGSIZE) {
757 err = got_error(GOT_ERR_PRIVSEP_LEN);
758 break;
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);
765 break;
767 ite = imsg.data;
769 te = got_alloc_tree_entry_partial();
770 if (te == NULL) {
771 err = got_error_from_errno();
772 break;
774 te->name = malloc(datalen + 1);
775 if (te->name == NULL) {
776 free(te);
777 err = got_error_from_errno();
778 break;
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);
786 nentries++;
787 break;
788 default:
789 err = got_error(GOT_ERR_PRIVSEP_MSG);
790 break;
793 imsg_free(&imsg);
795 done:
796 if (*tree && (*tree)->entries.nentries != nentries) {
797 if (err == NULL)
798 err = got_error(GOT_ERR_PRIVSEP_LEN);
799 got_object_tree_close(*tree);
800 *tree = NULL;
803 return err;
806 const struct got_error *
807 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size)
809 struct got_imsg_blob iblob;
811 iblob.size = size;
812 /* Data has already been written to file descriptor. */
814 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob, sizeof(iblob))
815 == -1)
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;
825 struct imsg imsg;
826 struct got_imsg_blob *iblob;
827 size_t datalen;
829 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
830 if (err)
831 return err;
833 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
835 switch (imsg.hdr.type) {
836 case GOT_IMSG_BLOB:
837 if (datalen != sizeof(*iblob)) {
838 err = got_error(GOT_ERR_PRIVSEP_LEN);
839 break;
841 iblob = imsg.data;
842 *size = iblob->size;
843 /* Data has been written to file descriptor. */
844 break;
845 default:
846 err = got_error(GOT_ERR_PRIVSEP_MSG);
847 break;
850 imsg_free(&imsg);
852 return err;
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;
861 offset = 0;
862 remain = tagmsg_len;
863 while (remain > 0) {
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();
869 break;
872 err = flush_imsg(ibuf);
873 if (err)
874 break;
876 offset += n;
877 remain -= n;
880 return err;
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;
888 uint8_t *buf;
889 size_t len, total;
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;
896 buf = malloc(total);
897 if (buf == NULL)
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;
909 len = sizeof(*itag);
910 memcpy(buf + len, tag->tag, tag_len);
911 len += tag_len;
912 memcpy(buf + len, tag->tagger, tagger_len);
913 len += tagger_len;
915 if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
916 err = got_error_from_errno();
917 goto done;
920 if (tagmsg_len == 0 ||
921 tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
922 err = flush_imsg(ibuf);
923 if (err)
924 goto done;
926 err = send_tagmsg(ibuf, tag, tagmsg_len);
927 done:
928 free(buf);
929 return err;
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;
936 struct imsg imsg;
937 struct got_imsg_tag_object *itag;
938 size_t len, datalen;
939 const size_t min_datalen =
940 MIN(sizeof(struct got_imsg_error),
941 sizeof(struct got_imsg_tag_object));
943 *tag = NULL;
945 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
946 if (err)
947 return err;
949 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
950 len = 0;
952 switch (imsg.hdr.type) {
953 case GOT_IMSG_TAG:
954 if (datalen < sizeof(*itag)) {
955 err = got_error(GOT_ERR_PRIVSEP_LEN);
956 break;
958 itag = imsg.data;
959 if (datalen != sizeof(*itag) + itag->tag_len +
960 itag->tagger_len) {
961 err = got_error(GOT_ERR_PRIVSEP_LEN);
962 break;
964 len += sizeof(*itag);
966 *tag = calloc(1, sizeof(**tag));
967 if (*tag == NULL) {
968 err = got_error_from_errno();
969 break;
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();
978 break;
980 } else {
981 (*tag)->tag = malloc(itag->tag_len + 1);
982 if ((*tag)->tag == NULL) {
983 err = got_error_from_errno();
984 break;
986 memcpy((*tag)->tag, imsg.data + len,
987 itag->tag_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();
1000 break;
1002 } else {
1003 (*tag)->tagger = malloc(itag->tagger_len + 1);
1004 if ((*tag)->tagger == NULL) {
1005 err = got_error_from_errno();
1006 break;
1008 memcpy((*tag)->tagger, imsg.data + len,
1009 itag->tagger_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();
1018 break;
1020 } else {
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();
1026 break;
1028 while (remain > 0) {
1029 struct imsg imsg_log;
1030 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1031 remain);
1033 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1034 if (err)
1035 return err;
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,
1041 n);
1042 imsg_free(&imsg_log);
1043 offset += n;
1044 remain -= n;
1046 (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1049 break;
1050 default:
1051 err = got_error(GOT_ERR_PRIVSEP_MSG);
1052 break;
1055 imsg_free(&imsg);
1057 return err;
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;
1066 int fd;
1068 ipackidx.len = packidx->len;
1069 fd = dup(packidx->fd);
1070 if (fd == -1)
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;
1082 fd = dup(pack->fd);
1083 if (fd == -1)
1084 return got_error_from_errno();
1086 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
1087 == -1)
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;
1099 iobj.idx = idx;
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);