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 imsg_code = GOT_IMSG_COMMIT_REQUEST;
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_blob_req(struct imsgbuf *ibuf, int infd)
271 if (imsg_compose(ibuf, GOT_IMSG_BLOB_REQUEST, 0, 0, infd, NULL, 0)
272 == -1)
273 return got_error_from_errno();
275 return flush_imsg(ibuf);
278 const struct got_error *
279 got_privsep_send_blob_outfd(struct imsgbuf *ibuf, int outfd)
281 if (imsg_compose(ibuf, GOT_IMSG_BLOB_OUTFD, 0, 0, outfd, NULL, 0)
282 == -1)
283 return got_error_from_errno();
285 return flush_imsg(ibuf);
288 const struct got_error *
289 got_privsep_send_tmpfd(struct imsgbuf *ibuf, int fd)
291 if (imsg_compose(ibuf, GOT_IMSG_TMPFD, 0, 0, fd, NULL, 0)
292 == -1)
293 return got_error_from_errno();
295 return flush_imsg(ibuf);
298 const struct got_error *
299 got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj)
301 struct got_imsg_object iobj;
303 memcpy(iobj.id, obj->id.sha1, sizeof(iobj.id));
304 iobj.type = obj->type;
305 iobj.flags = obj->flags;
306 iobj.hdrlen = obj->hdrlen;
307 iobj.size = obj->size;
308 if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
309 iobj.pack_offset = obj->pack_offset;
310 iobj.pack_idx = obj->pack_idx;
313 if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
314 == -1)
315 return got_error_from_errno();
317 return flush_imsg(ibuf);
320 const struct got_error *
321 got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
322 struct imsgbuf *ibuf)
324 const struct got_error *err = NULL;
325 struct got_imsg_object *iobj;
326 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
328 if (datalen != sizeof(*iobj))
329 return got_error(GOT_ERR_PRIVSEP_LEN);
330 iobj = imsg->data;
332 *obj = calloc(1, sizeof(**obj));
333 if (*obj == NULL)
334 return got_error_from_errno();
336 memcpy((*obj)->id.sha1, iobj->id, SHA1_DIGEST_LENGTH);
337 (*obj)->type = iobj->type;
338 (*obj)->flags = iobj->flags;
339 (*obj)->hdrlen = iobj->hdrlen;
340 (*obj)->size = iobj->size;
341 /* path_packfile is handled by caller */
342 if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
343 (*obj)->pack_offset = iobj->pack_offset;
344 (*obj)->pack_idx = iobj->pack_idx;
347 return err;
350 const struct got_error *
351 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
353 const struct got_error *err = NULL;
354 struct imsg imsg;
355 size_t datalen;
356 const size_t min_datalen =
357 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
359 *obj = NULL;
361 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
362 if (err)
363 return err;
365 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
367 switch (imsg.hdr.type) {
368 case GOT_IMSG_OBJECT:
369 err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
370 break;
371 default:
372 err = got_error(GOT_ERR_PRIVSEP_MSG);
373 break;
376 imsg_free(&imsg);
378 return err;
381 static const struct got_error *
382 send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
383 size_t logmsg_len)
385 const struct got_error *err = NULL;
386 size_t offset, remain;
388 offset = 0;
389 remain = logmsg_len;
390 while (remain > 0) {
391 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
393 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
394 commit->logmsg + offset, n) == -1) {
395 err = got_error_from_errno();
396 break;
399 err = flush_imsg(ibuf);
400 if (err)
401 break;
403 offset += n;
404 remain -= n;
407 return err;
410 const struct got_error *
411 got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
413 const struct got_error *err = NULL;
414 struct got_imsg_commit_object *icommit;
415 uint8_t *buf;
416 size_t len, total;
417 struct got_object_qid *qid;
418 size_t author_len = strlen(commit->author);
419 size_t committer_len = strlen(commit->committer);
420 size_t logmsg_len = strlen(commit->logmsg);
422 total = sizeof(*icommit) + author_len + committer_len +
423 commit->nparents * SHA1_DIGEST_LENGTH;
425 buf = malloc(total);
426 if (buf == NULL)
427 return got_error_from_errno();
429 icommit = (struct got_imsg_commit_object *)buf;
430 memcpy(icommit->tree_id, commit->tree_id->sha1, sizeof(icommit->tree_id));
431 icommit->author_len = author_len;
432 icommit->author_time = commit->author_time;
433 icommit->author_gmtoff = commit->author_gmtoff;
434 icommit->committer_len = committer_len;
435 icommit->committer_time = commit->committer_time;
436 icommit->committer_gmtoff = commit->committer_gmtoff;
437 icommit->logmsg_len = logmsg_len;
438 icommit->nparents = commit->nparents;
440 len = sizeof(*icommit);
441 memcpy(buf + len, commit->author, author_len);
442 len += author_len;
443 memcpy(buf + len, commit->committer, committer_len);
444 len += committer_len;
445 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
446 memcpy(buf + len, qid->id, SHA1_DIGEST_LENGTH);
447 len += SHA1_DIGEST_LENGTH;
450 if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
451 err = got_error_from_errno();
452 goto done;
455 if (logmsg_len == 0 ||
456 logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
457 err = flush_imsg(ibuf);
458 if (err)
459 goto done;
461 err = send_commit_logmsg(ibuf, commit, logmsg_len);
462 done:
463 free(buf);
464 return err;
467 const struct got_error *
468 got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
470 const struct got_error *err = NULL;
471 struct imsg imsg;
472 struct got_imsg_commit_object *icommit;
473 size_t len, datalen;
474 int i;
475 const size_t min_datalen =
476 MIN(sizeof(struct got_imsg_error),
477 sizeof(struct got_imsg_commit_object));
479 *commit = NULL;
481 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
482 if (err)
483 return err;
485 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
486 len = 0;
488 switch (imsg.hdr.type) {
489 case GOT_IMSG_COMMIT:
490 if (datalen < sizeof(*icommit)) {
491 err = got_error(GOT_ERR_PRIVSEP_LEN);
492 break;
494 icommit = imsg.data;
495 if (datalen != sizeof(*icommit) + icommit->author_len +
496 icommit->committer_len +
497 icommit->nparents * SHA1_DIGEST_LENGTH) {
498 err = got_error(GOT_ERR_PRIVSEP_LEN);
499 break;
501 if (icommit->nparents < 0) {
502 err = got_error(GOT_ERR_PRIVSEP_LEN);
503 break;
505 len += sizeof(*icommit);
507 *commit = got_object_commit_alloc_partial();
508 if (*commit == NULL) {
509 err = got_error_from_errno();
510 break;
513 memcpy((*commit)->tree_id->sha1, icommit->tree_id,
514 SHA1_DIGEST_LENGTH);
515 (*commit)->author_time = icommit->author_time;
516 (*commit)->author_gmtoff = icommit->author_gmtoff;
517 (*commit)->committer_time = icommit->committer_time;
518 (*commit)->committer_gmtoff = icommit->committer_gmtoff;
520 if (icommit->author_len == 0) {
521 (*commit)->author = strdup("");
522 if ((*commit)->author == NULL) {
523 err = got_error_from_errno();
524 break;
526 } else {
527 (*commit)->author = malloc(icommit->author_len + 1);
528 if ((*commit)->author == NULL) {
529 err = got_error_from_errno();
530 break;
532 memcpy((*commit)->author, imsg.data + len,
533 icommit->author_len);
534 (*commit)->author[icommit->author_len] = '\0';
536 len += icommit->author_len;
538 if (icommit->committer_len == 0) {
539 (*commit)->committer = strdup("");
540 if ((*commit)->committer == NULL) {
541 err = got_error_from_errno();
542 break;
544 } else {
545 (*commit)->committer =
546 malloc(icommit->committer_len + 1);
547 if ((*commit)->committer == NULL) {
548 err = got_error_from_errno();
549 break;
551 memcpy((*commit)->committer, imsg.data + len,
552 icommit->committer_len);
553 (*commit)->committer[icommit->committer_len] = '\0';
555 len += icommit->committer_len;
557 if (icommit->logmsg_len == 0) {
558 (*commit)->logmsg = strdup("");
559 if ((*commit)->logmsg == NULL) {
560 err = got_error_from_errno();
561 break;
563 } else {
564 size_t offset = 0, remain = icommit->logmsg_len;
566 (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
567 if ((*commit)->logmsg == NULL) {
568 err = got_error_from_errno();
569 break;
571 while (remain > 0) {
572 struct imsg imsg_log;
573 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
574 remain);
576 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
577 if (err)
578 return err;
580 if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG)
581 return got_error(GOT_ERR_PRIVSEP_MSG);
583 memcpy((*commit)->logmsg + offset,
584 imsg_log.data, n);
585 imsg_free(&imsg_log);
586 offset += n;
587 remain -= n;
589 (*commit)->logmsg[icommit->logmsg_len] = '\0';
592 for (i = 0; i < icommit->nparents; i++) {
593 struct got_object_qid *qid;
595 err = got_object_qid_alloc_partial(&qid);
596 if (err)
597 break;
598 memcpy(qid->id, imsg.data + len +
599 i * SHA1_DIGEST_LENGTH, sizeof(*qid->id));
600 SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
601 (*commit)->nparents++;
603 break;
604 default:
605 err = got_error(GOT_ERR_PRIVSEP_MSG);
606 break;
609 imsg_free(&imsg);
611 return err;
614 const struct got_error *
615 got_privsep_send_tree(struct imsgbuf *ibuf, struct got_tree_object *tree)
617 const struct got_error *err = NULL;
618 struct got_imsg_tree_object itree;
619 struct got_tree_entry *te;
620 size_t totlen;
621 int nimsg; /* number of imsg queued in ibuf */
623 itree.nentries = tree->entries.nentries;
624 if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
625 == -1)
626 return got_error_from_errno();
628 totlen = sizeof(itree);
629 nimsg = 1;
630 SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
631 struct got_imsg_tree_entry *ite;
632 uint8_t *buf = NULL;
633 size_t len = sizeof(*ite) + strlen(te->name);
635 if (len > MAX_IMSGSIZE)
636 return got_error(GOT_ERR_NO_SPACE);
638 nimsg++;
639 if (totlen + len >= MAX_IMSGSIZE - (IMSG_HEADER_SIZE * nimsg)) {
640 err = flush_imsg(ibuf);
641 if (err)
642 return err;
643 nimsg = 0;
646 buf = malloc(len);
647 if (buf == NULL)
648 return got_error_from_errno();
650 ite = (struct got_imsg_tree_entry *)buf;
651 memcpy(ite->id, te->id->sha1, sizeof(ite->id));
652 ite->mode = te->mode;
653 memcpy(buf + sizeof(*ite), te->name, strlen(te->name));
655 if (imsg_compose(ibuf, GOT_IMSG_TREE_ENTRY, 0, 0, -1,
656 buf, len) == -1)
657 err = got_error_from_errno();
658 free(buf);
659 if (err)
660 return err;
661 totlen += len;
664 return flush_imsg(ibuf);
667 const struct got_error *
668 got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
670 const struct got_error *err = NULL;
671 const size_t min_datalen =
672 MIN(sizeof(struct got_imsg_error),
673 sizeof(struct got_imsg_tree_object));
674 struct got_imsg_tree_object *itree;
675 int nentries = 0;
677 *tree = NULL;
678 get_more:
679 err = read_imsg(ibuf);
680 if (err)
681 goto done;
683 while (1) {
684 struct imsg imsg;
685 size_t n;
686 size_t datalen;
687 struct got_imsg_tree_entry *ite;
688 struct got_tree_entry *te = NULL;
690 n = imsg_get(ibuf, &imsg);
691 if (n == 0) {
692 if (*tree && (*tree)->entries.nentries != nentries)
693 goto get_more;
694 break;
697 if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen)
698 return got_error(GOT_ERR_PRIVSEP_LEN);
700 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
702 switch (imsg.hdr.type) {
703 case GOT_IMSG_ERROR:
704 err = recv_imsg_error(&imsg, datalen);
705 break;
706 case GOT_IMSG_TREE:
707 /* This message should only appear once. */
708 if (*tree != NULL) {
709 err = got_error(GOT_ERR_PRIVSEP_MSG);
710 break;
712 if (datalen != sizeof(*itree)) {
713 err = got_error(GOT_ERR_PRIVSEP_LEN);
714 break;
716 itree = imsg.data;
717 *tree = malloc(sizeof(**tree));
718 if (*tree == NULL) {
719 err = got_error_from_errno();
720 break;
722 (*tree)->entries.nentries = itree->nentries;
723 SIMPLEQ_INIT(&(*tree)->entries.head);
724 (*tree)->refcnt = 0;
725 break;
726 case GOT_IMSG_TREE_ENTRY:
727 /* This message should be preceeded by GOT_IMSG_TREE. */
728 if (*tree == NULL) {
729 err = got_error(GOT_ERR_PRIVSEP_MSG);
730 break;
732 if (datalen < sizeof(*ite) || datalen > MAX_IMSGSIZE) {
733 err = got_error(GOT_ERR_PRIVSEP_LEN);
734 break;
737 /* Remaining data contains the entry's name. */
738 datalen -= sizeof(*ite);
739 if (datalen == 0 || datalen > MAX_IMSGSIZE) {
740 err = got_error(GOT_ERR_PRIVSEP_LEN);
741 break;
743 ite = imsg.data;
745 te = got_alloc_tree_entry_partial();
746 if (te == NULL) {
747 err = got_error_from_errno();
748 break;
750 te->name = malloc(datalen + 1);
751 if (te->name == NULL) {
752 free(te);
753 err = got_error_from_errno();
754 break;
756 memcpy(te->name, imsg.data + sizeof(*ite), datalen);
757 te->name[datalen] = '\0';
759 memcpy(te->id->sha1, ite->id, SHA1_DIGEST_LENGTH);
760 te->mode = ite->mode;
761 SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
762 nentries++;
763 break;
764 default:
765 err = got_error(GOT_ERR_PRIVSEP_MSG);
766 break;
769 imsg_free(&imsg);
771 done:
772 if (*tree && (*tree)->entries.nentries != nentries) {
773 if (err == NULL)
774 err = got_error(GOT_ERR_PRIVSEP_LEN);
775 got_object_tree_close(*tree);
776 *tree = NULL;
779 return err;
782 const struct got_error *
783 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size)
785 struct got_imsg_blob iblob;
787 iblob.size = size;
788 /* Data has already been written to file descriptor. */
790 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob, sizeof(iblob))
791 == -1)
792 return got_error_from_errno();
794 return flush_imsg(ibuf);
797 const struct got_error *
798 got_privsep_recv_blob(size_t *size, struct imsgbuf *ibuf)
800 const struct got_error *err = NULL;
801 struct imsg imsg;
802 struct got_imsg_blob *iblob;
803 size_t datalen;
805 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
806 if (err)
807 return err;
809 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
811 switch (imsg.hdr.type) {
812 case GOT_IMSG_BLOB:
813 if (datalen != sizeof(*iblob)) {
814 err = got_error(GOT_ERR_PRIVSEP_LEN);
815 break;
817 iblob = imsg.data;
818 *size = iblob->size;
819 /* Data has been written to file descriptor. */
820 break;
821 default:
822 err = got_error(GOT_ERR_PRIVSEP_MSG);
823 break;
826 imsg_free(&imsg);
828 return err;
831 static const struct got_error *
832 send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
834 const struct got_error *err = NULL;
835 size_t offset, remain;
837 offset = 0;
838 remain = tagmsg_len;
839 while (remain > 0) {
840 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
842 if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
843 tag->tagmsg + offset, n) == -1) {
844 err = got_error_from_errno();
845 break;
848 err = flush_imsg(ibuf);
849 if (err)
850 break;
852 offset += n;
853 remain -= n;
856 return err;
859 const struct got_error *
860 got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
862 const struct got_error *err = NULL;
863 struct got_imsg_tag_object *itag;
864 uint8_t *buf;
865 size_t len, total;
866 size_t tag_len = strlen(tag->tag);
867 size_t tagger_len = strlen(tag->tagger);
868 size_t tagmsg_len = strlen(tag->tagmsg);
870 total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
872 buf = malloc(total);
873 if (buf == NULL)
874 return got_error_from_errno();
876 itag = (struct got_imsg_tag_object *)buf;
877 memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
878 itag->obj_type = tag->obj_type;
879 itag->tag_len = tag_len;
880 itag->tagger_len = tagger_len;
881 itag->tagger_time = tag->tagger_time;
882 itag->tagger_gmtoff = tag->tagger_gmtoff;
883 itag->tagmsg_len = tagmsg_len;
885 len = sizeof(*itag);
886 memcpy(buf + len, tag->tag, tag_len);
887 len += tag_len;
888 memcpy(buf + len, tag->tagger, tagger_len);
889 len += tagger_len;
891 if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
892 err = got_error_from_errno();
893 goto done;
896 if (tagmsg_len == 0 ||
897 tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
898 err = flush_imsg(ibuf);
899 if (err)
900 goto done;
902 err = send_tagmsg(ibuf, tag, tagmsg_len);
903 done:
904 free(buf);
905 return err;
908 const struct got_error *
909 got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
911 const struct got_error *err = NULL;
912 struct imsg imsg;
913 struct got_imsg_tag_object *itag;
914 size_t len, datalen;
915 const size_t min_datalen =
916 MIN(sizeof(struct got_imsg_error),
917 sizeof(struct got_imsg_tag_object));
919 *tag = NULL;
921 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
922 if (err)
923 return err;
925 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
926 len = 0;
928 switch (imsg.hdr.type) {
929 case GOT_IMSG_TAG:
930 if (datalen < sizeof(*itag)) {
931 err = got_error(GOT_ERR_PRIVSEP_LEN);
932 break;
934 itag = imsg.data;
935 if (datalen != sizeof(*itag) + itag->tag_len +
936 itag->tagger_len) {
937 err = got_error(GOT_ERR_PRIVSEP_LEN);
938 break;
940 len += sizeof(*itag);
942 *tag = calloc(1, sizeof(**tag));
943 if (*tag == NULL) {
944 err = got_error_from_errno();
945 break;
948 memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
950 if (itag->tag_len == 0) {
951 (*tag)->tag = strdup("");
952 if ((*tag)->tag == NULL) {
953 err = got_error_from_errno();
954 break;
956 } else {
957 (*tag)->tag = malloc(itag->tag_len + 1);
958 if ((*tag)->tag == NULL) {
959 err = got_error_from_errno();
960 break;
962 memcpy((*tag)->tag, imsg.data + len,
963 itag->tag_len);
964 (*tag)->tag[itag->tag_len] = '\0';
966 len += itag->tag_len;
968 (*tag)->obj_type = itag->obj_type;
969 (*tag)->tagger_time = itag->tagger_time;
970 (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
972 if (itag->tagger_len == 0) {
973 (*tag)->tagger = strdup("");
974 if ((*tag)->tagger == NULL) {
975 err = got_error_from_errno();
976 break;
978 } else {
979 (*tag)->tagger = malloc(itag->tagger_len + 1);
980 if ((*tag)->tagger == NULL) {
981 err = got_error_from_errno();
982 break;
984 memcpy((*tag)->tagger, imsg.data + len,
985 itag->tagger_len);
986 (*tag)->tagger[itag->tagger_len] = '\0';
988 len += itag->tagger_len;
990 if (itag->tagmsg_len == 0) {
991 (*tag)->tagmsg = strdup("");
992 if ((*tag)->tagmsg == NULL) {
993 err = got_error_from_errno();
994 break;
996 } else {
997 size_t offset = 0, remain = itag->tagmsg_len;
999 (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1000 if ((*tag)->tagmsg == NULL) {
1001 err = got_error_from_errno();
1002 break;
1004 while (remain > 0) {
1005 struct imsg imsg_log;
1006 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1007 remain);
1009 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1010 if (err)
1011 return err;
1013 if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1014 return got_error(GOT_ERR_PRIVSEP_MSG);
1016 memcpy((*tag)->tagmsg + offset, imsg_log.data,
1017 n);
1018 imsg_free(&imsg_log);
1019 offset += n;
1020 remain -= n;
1022 (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1025 break;
1026 default:
1027 err = got_error(GOT_ERR_PRIVSEP_MSG);
1028 break;
1031 imsg_free(&imsg);
1033 return err;
1036 const struct got_error *
1037 got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1038 struct got_packidx *packidx)
1040 struct got_imsg_packidx ipackidx;
1041 struct got_imsg_pack ipack;
1042 int fd;
1044 ipackidx.len = packidx->len;
1045 fd = dup(packidx->fd);
1046 if (fd == -1)
1047 return got_error_from_errno();
1049 if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
1050 sizeof(ipackidx)) == -1)
1051 return got_error_from_errno();
1053 if (strlcpy(ipack.path_packfile, pack->path_packfile,
1054 sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
1055 return got_error(GOT_ERR_NO_SPACE);
1056 ipack.filesize = pack->filesize;
1058 fd = dup(pack->fd);
1059 if (fd == -1)
1060 return got_error_from_errno();
1062 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
1063 == -1)
1064 return got_error_from_errno();
1066 return flush_imsg(ibuf);
1069 const struct got_error *
1070 got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
1071 struct got_object_id *id)
1073 struct got_imsg_packed_object iobj;
1075 iobj.idx = idx;
1076 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
1078 if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
1079 &iobj, sizeof(iobj)) == -1)
1080 return got_error_from_errno();
1082 return flush_imsg(ibuf);