Blob


1 /*
2 * Copyright (c) 2018, 2019 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"
36 #include "got_path.h"
38 #include "got_lib_sha1.h"
39 #include "got_lib_delta.h"
40 #include "got_lib_inflate.h"
41 #include "got_lib_object.h"
42 #include "got_lib_object_parse.h"
43 #include "got_lib_privsep.h"
44 #include "got_lib_pack.h"
46 #ifndef MIN
47 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
48 #endif
50 #ifndef nitems
51 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
52 #endif
54 static const struct got_error *
55 poll_fd(int fd, int events, int timeout)
56 {
57 struct pollfd pfd[1];
58 int n;
60 pfd[0].fd = fd;
61 pfd[0].events = events;
63 n = poll(pfd, 1, timeout);
64 if (n == -1)
65 return got_error_from_errno("poll");
66 if (n == 0)
67 return got_error(GOT_ERR_TIMEOUT);
68 if (pfd[0].revents & (POLLERR | POLLNVAL))
69 return got_error_from_errno("poll error");
70 if (pfd[0].revents & (events | POLLHUP))
71 return NULL;
73 return got_error(GOT_ERR_INTERRUPT);
74 }
76 static const struct got_error *
77 read_imsg(struct imsgbuf *ibuf)
78 {
79 const struct got_error *err;
80 size_t n;
82 err = poll_fd(ibuf->fd, POLLIN, INFTIM);
83 if (err)
84 return err;
86 n = imsg_read(ibuf);
87 if (n == -1) {
88 if (errno == EAGAIN) /* Could be a file-descriptor leak. */
89 return got_error(GOT_ERR_PRIVSEP_NO_FD);
90 return got_error(GOT_ERR_PRIVSEP_READ);
91 }
92 if (n == 0)
93 return got_error(GOT_ERR_PRIVSEP_PIPE);
95 return NULL;
96 }
98 const struct got_error *
99 got_privsep_wait_for_child(pid_t pid)
101 int child_status;
103 if (waitpid(pid, &child_status, 0) == -1)
104 return got_error_from_errno("waitpid");
106 if (!WIFEXITED(child_status))
107 return got_error(GOT_ERR_PRIVSEP_DIED);
109 if (WEXITSTATUS(child_status) != 0)
110 return got_error(GOT_ERR_PRIVSEP_EXIT);
112 return NULL;
115 static const struct got_error *
116 recv_imsg_error(struct imsg *imsg, size_t datalen)
118 struct got_imsg_error *ierr;
120 if (datalen != sizeof(*ierr))
121 return got_error(GOT_ERR_PRIVSEP_LEN);
123 ierr = imsg->data;
124 if (ierr->code == GOT_ERR_ERRNO) {
125 static struct got_error serr;
126 serr.code = GOT_ERR_ERRNO;
127 serr.msg = strerror(ierr->errno_code);
128 return &serr;
131 return got_error(ierr->code);
134 const struct got_error *
135 got_privsep_recv_imsg(struct imsg *imsg, struct imsgbuf *ibuf,
136 size_t min_datalen)
138 const struct got_error *err;
139 ssize_t n;
141 n = imsg_get(ibuf, imsg);
142 if (n == -1)
143 return got_error_from_errno("imsg_get");
145 while (n == 0) {
146 err = read_imsg(ibuf);
147 if (err)
148 return err;
149 n = imsg_get(ibuf, imsg);
152 if (imsg->hdr.len < IMSG_HEADER_SIZE + min_datalen)
153 return got_error(GOT_ERR_PRIVSEP_LEN);
155 if (imsg->hdr.type == GOT_IMSG_ERROR) {
156 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
157 return recv_imsg_error(imsg, datalen);
160 return NULL;
163 /* Attempt to send an error in an imsg. Complain on stderr as a last resort. */
164 void
165 got_privsep_send_error(struct imsgbuf *ibuf, const struct got_error *err)
167 const struct got_error *poll_err;
168 struct got_imsg_error ierr;
169 int ret;
171 ierr.code = err->code;
172 if (err->code == GOT_ERR_ERRNO)
173 ierr.errno_code = errno;
174 else
175 ierr.errno_code = 0;
176 ret = imsg_compose(ibuf, GOT_IMSG_ERROR, 0, 0, -1, &ierr, sizeof(ierr));
177 if (ret == -1) {
178 fprintf(stderr, "%s: error %d \"%s\": imsg_compose: %s\n",
179 getprogname(), err->code, err->msg, strerror(errno));
180 return;
183 poll_err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
184 if (poll_err) {
185 fprintf(stderr, "%s: error %d \"%s\": poll: %s\n",
186 getprogname(), err->code, err->msg, poll_err->msg);
187 return;
190 ret = imsg_flush(ibuf);
191 if (ret == -1) {
192 fprintf(stderr, "%s: error %d \"%s\": imsg_flush: %s\n",
193 getprogname(), err->code, err->msg, strerror(errno));
194 return;
198 static const struct got_error *
199 flush_imsg(struct imsgbuf *ibuf)
201 const struct got_error *err;
203 err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
204 if (err)
205 return err;
207 if (imsg_flush(ibuf) == -1)
208 return got_error_from_errno("imsg_flush");
210 return NULL;
213 const struct got_error *
214 got_privsep_send_stop(int fd)
216 const struct got_error *err = NULL;
217 struct imsgbuf ibuf;
219 imsg_init(&ibuf, fd);
221 if (imsg_compose(&ibuf, GOT_IMSG_STOP, 0, 0, -1, NULL, 0) == -1)
222 return got_error_from_errno("imsg_compose STOP");
224 err = flush_imsg(&ibuf);
225 imsg_clear(&ibuf);
226 return err;
229 const struct got_error *
230 got_privsep_send_obj_req(struct imsgbuf *ibuf, int fd)
232 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_REQUEST, 0, 0, fd, NULL, 0)
233 == -1)
234 return got_error_from_errno("imsg_compose OBJECT_REQUEST");
236 return flush_imsg(ibuf);
239 const struct got_error *
240 got_privsep_send_commit_req(struct imsgbuf *ibuf, int fd,
241 struct got_object_id *id, int pack_idx)
243 const struct got_error *err = NULL;
244 struct got_imsg_packed_object iobj, *iobjp;
245 size_t len;
247 if (id) { /* commit is packed */
248 iobj.idx = pack_idx;
249 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
250 iobjp = &iobj;
251 len = sizeof(iobj);
252 } else {
253 iobjp = NULL;
254 len = 0;
257 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_REQUEST, 0, 0, fd, iobjp, len)
258 == -1) {
259 err = got_error_from_errno("imsg_compose COMMIT_REQUEST");
260 close(fd);
261 return err;
264 return flush_imsg(ibuf);
267 const struct got_error *
268 got_privsep_send_tree_req(struct imsgbuf *ibuf, int fd,
269 struct got_object_id *id, int pack_idx)
271 const struct got_error *err = NULL;
272 struct ibuf *wbuf;
273 size_t len = id ? sizeof(struct got_imsg_packed_object) : 0;
275 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_REQUEST, 0, 0, len);
276 if (wbuf == NULL)
277 return got_error_from_errno("imsg_create TREE_REQUEST");
279 if (id) { /* tree is packed */
280 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
281 err = got_error_from_errno("imsg_add TREE_ENTRY");
282 ibuf_free(wbuf);
283 return err;
286 if (imsg_add(wbuf, &pack_idx, sizeof(pack_idx)) == -1) {
287 err = got_error_from_errno("imsg_add TREE_ENTRY");
288 ibuf_free(wbuf);
289 return err;
293 wbuf->fd = fd;
294 imsg_close(ibuf, wbuf);
296 return flush_imsg(ibuf);
299 const struct got_error *
300 got_privsep_send_tag_req(struct imsgbuf *ibuf, int fd,
301 struct got_object_id *id, int pack_idx)
303 struct got_imsg_packed_object iobj, *iobjp;
304 size_t len;
306 if (id) { /* tag is packed */
307 iobj.idx = pack_idx;
308 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
309 iobjp = &iobj;
310 len = sizeof(iobj);
311 } else {
312 iobjp = NULL;
313 len = 0;
316 if (imsg_compose(ibuf, GOT_IMSG_TAG_REQUEST, 0, 0, fd, iobjp, len)
317 == -1)
318 return got_error_from_errno("imsg_compose TAG_REQUEST");
320 return flush_imsg(ibuf);
323 const struct got_error *
324 got_privsep_send_blob_req(struct imsgbuf *ibuf, int infd,
325 struct got_object_id *id, int pack_idx)
327 const struct got_error *err = NULL;
328 struct got_imsg_packed_object iobj, *iobjp;
329 size_t len;
331 if (id) { /* blob is packed */
332 iobj.idx = pack_idx;
333 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
334 iobjp = &iobj;
335 len = sizeof(iobj);
336 } else {
337 iobjp = NULL;
338 len = 0;
341 if (imsg_compose(ibuf, GOT_IMSG_BLOB_REQUEST, 0, 0, infd, iobjp, len)
342 == -1) {
343 err = got_error_from_errno("imsg_compose BLOB_REQUEST");
344 close(infd);
345 return err;
348 return flush_imsg(ibuf);
351 const struct got_error *
352 got_privsep_send_blob_outfd(struct imsgbuf *ibuf, int outfd)
354 const struct got_error *err = NULL;
356 if (imsg_compose(ibuf, GOT_IMSG_BLOB_OUTFD, 0, 0, outfd, NULL, 0)
357 == -1) {
358 err = got_error_from_errno("imsg_compose BLOB_OUTFD");
359 close(outfd);
360 return err;
363 return flush_imsg(ibuf);
366 const struct got_error *
367 got_privsep_send_tmpfd(struct imsgbuf *ibuf, int fd)
369 const struct got_error *err = NULL;
371 if (imsg_compose(ibuf, GOT_IMSG_TMPFD, 0, 0, fd, NULL, 0)
372 == -1) {
373 err = got_error_from_errno("imsg_compose TMPFD");
374 close(fd);
375 return err;
378 return flush_imsg(ibuf);
381 const struct got_error *
382 got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj)
384 struct got_imsg_object iobj;
386 memcpy(iobj.id, obj->id.sha1, sizeof(iobj.id));
387 iobj.type = obj->type;
388 iobj.flags = obj->flags;
389 iobj.hdrlen = obj->hdrlen;
390 iobj.size = obj->size;
391 if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
392 iobj.pack_offset = obj->pack_offset;
393 iobj.pack_idx = obj->pack_idx;
396 if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
397 == -1)
398 return got_error_from_errno("imsg_compose OBJECT");
400 return flush_imsg(ibuf);
403 const struct got_error *
404 got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
405 struct imsgbuf *ibuf)
407 const struct got_error *err = NULL;
408 struct got_imsg_object *iobj;
409 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
411 if (datalen != sizeof(*iobj))
412 return got_error(GOT_ERR_PRIVSEP_LEN);
413 iobj = imsg->data;
415 *obj = calloc(1, sizeof(**obj));
416 if (*obj == NULL)
417 return got_error_from_errno("calloc");
419 memcpy((*obj)->id.sha1, iobj->id, SHA1_DIGEST_LENGTH);
420 (*obj)->type = iobj->type;
421 (*obj)->flags = iobj->flags;
422 (*obj)->hdrlen = iobj->hdrlen;
423 (*obj)->size = iobj->size;
424 /* path_packfile is handled by caller */
425 if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
426 (*obj)->pack_offset = iobj->pack_offset;
427 (*obj)->pack_idx = iobj->pack_idx;
430 return err;
433 const struct got_error *
434 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
436 const struct got_error *err = NULL;
437 struct imsg imsg;
438 const size_t min_datalen =
439 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
441 *obj = NULL;
443 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
444 if (err)
445 return err;
447 switch (imsg.hdr.type) {
448 case GOT_IMSG_OBJECT:
449 err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
450 break;
451 default:
452 err = got_error(GOT_ERR_PRIVSEP_MSG);
453 break;
456 imsg_free(&imsg);
458 return err;
461 static const struct got_error *
462 send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
463 size_t logmsg_len)
465 const struct got_error *err = NULL;
466 size_t offset, remain;
468 offset = 0;
469 remain = logmsg_len;
470 while (remain > 0) {
471 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
473 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
474 commit->logmsg + offset, n) == -1) {
475 err = got_error_from_errno("imsg_compose "
476 "COMMIT_LOGMSG");
477 break;
480 err = flush_imsg(ibuf);
481 if (err)
482 break;
484 offset += n;
485 remain -= n;
488 return err;
491 const struct got_error *
492 got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
494 const struct got_error *err = NULL;
495 struct got_imsg_commit_object *icommit;
496 uint8_t *buf;
497 size_t len, total;
498 struct got_object_qid *qid;
499 size_t author_len = strlen(commit->author);
500 size_t committer_len = strlen(commit->committer);
501 size_t logmsg_len = strlen(commit->logmsg);
503 total = sizeof(*icommit) + author_len + committer_len +
504 commit->nparents * SHA1_DIGEST_LENGTH;
506 buf = malloc(total);
507 if (buf == NULL)
508 return got_error_from_errno("malloc");
510 icommit = (struct got_imsg_commit_object *)buf;
511 memcpy(icommit->tree_id, commit->tree_id->sha1,
512 sizeof(icommit->tree_id));
513 icommit->author_len = author_len;
514 icommit->author_time = commit->author_time;
515 icommit->author_gmtoff = commit->author_gmtoff;
516 icommit->committer_len = committer_len;
517 icommit->committer_time = commit->committer_time;
518 icommit->committer_gmtoff = commit->committer_gmtoff;
519 icommit->logmsg_len = logmsg_len;
520 icommit->nparents = commit->nparents;
522 len = sizeof(*icommit);
523 memcpy(buf + len, commit->author, author_len);
524 len += author_len;
525 memcpy(buf + len, commit->committer, committer_len);
526 len += committer_len;
527 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
528 memcpy(buf + len, qid->id, SHA1_DIGEST_LENGTH);
529 len += SHA1_DIGEST_LENGTH;
532 if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
533 err = got_error_from_errno("imsg_compose COMMIT");
534 goto done;
537 if (logmsg_len == 0 ||
538 logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
539 err = flush_imsg(ibuf);
540 if (err)
541 goto done;
543 err = send_commit_logmsg(ibuf, commit, logmsg_len);
544 done:
545 free(buf);
546 return err;
549 const struct got_error *
550 got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
552 const struct got_error *err = NULL;
553 struct imsg imsg;
554 struct got_imsg_commit_object *icommit;
555 size_t len, datalen;
556 int i;
557 const size_t min_datalen =
558 MIN(sizeof(struct got_imsg_error),
559 sizeof(struct got_imsg_commit_object));
561 *commit = NULL;
563 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
564 if (err)
565 return err;
567 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
568 len = 0;
570 switch (imsg.hdr.type) {
571 case GOT_IMSG_COMMIT:
572 if (datalen < sizeof(*icommit)) {
573 err = got_error(GOT_ERR_PRIVSEP_LEN);
574 break;
576 icommit = imsg.data;
577 if (datalen != sizeof(*icommit) + icommit->author_len +
578 icommit->committer_len +
579 icommit->nparents * SHA1_DIGEST_LENGTH) {
580 err = got_error(GOT_ERR_PRIVSEP_LEN);
581 break;
583 if (icommit->nparents < 0) {
584 err = got_error(GOT_ERR_PRIVSEP_LEN);
585 break;
587 len += sizeof(*icommit);
589 *commit = got_object_commit_alloc_partial();
590 if (*commit == NULL) {
591 err = got_error_from_errno(
592 "got_object_commit_alloc_partial");
593 break;
596 memcpy((*commit)->tree_id->sha1, icommit->tree_id,
597 SHA1_DIGEST_LENGTH);
598 (*commit)->author_time = icommit->author_time;
599 (*commit)->author_gmtoff = icommit->author_gmtoff;
600 (*commit)->committer_time = icommit->committer_time;
601 (*commit)->committer_gmtoff = icommit->committer_gmtoff;
603 if (icommit->author_len == 0) {
604 (*commit)->author = strdup("");
605 if ((*commit)->author == NULL) {
606 err = got_error_from_errno("strdup");
607 break;
609 } else {
610 (*commit)->author = malloc(icommit->author_len + 1);
611 if ((*commit)->author == NULL) {
612 err = got_error_from_errno("malloc");
613 break;
615 memcpy((*commit)->author, imsg.data + len,
616 icommit->author_len);
617 (*commit)->author[icommit->author_len] = '\0';
619 len += icommit->author_len;
621 if (icommit->committer_len == 0) {
622 (*commit)->committer = strdup("");
623 if ((*commit)->committer == NULL) {
624 err = got_error_from_errno("strdup");
625 break;
627 } else {
628 (*commit)->committer =
629 malloc(icommit->committer_len + 1);
630 if ((*commit)->committer == NULL) {
631 err = got_error_from_errno("malloc");
632 break;
634 memcpy((*commit)->committer, imsg.data + len,
635 icommit->committer_len);
636 (*commit)->committer[icommit->committer_len] = '\0';
638 len += icommit->committer_len;
640 if (icommit->logmsg_len == 0) {
641 (*commit)->logmsg = strdup("");
642 if ((*commit)->logmsg == NULL) {
643 err = got_error_from_errno("strdup");
644 break;
646 } else {
647 size_t offset = 0, remain = icommit->logmsg_len;
649 (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
650 if ((*commit)->logmsg == NULL) {
651 err = got_error_from_errno("malloc");
652 break;
654 while (remain > 0) {
655 struct imsg imsg_log;
656 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
657 remain);
659 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
660 if (err)
661 return err;
663 if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG)
664 return got_error(GOT_ERR_PRIVSEP_MSG);
666 memcpy((*commit)->logmsg + offset,
667 imsg_log.data, n);
668 imsg_free(&imsg_log);
669 offset += n;
670 remain -= n;
672 (*commit)->logmsg[icommit->logmsg_len] = '\0';
675 for (i = 0; i < icommit->nparents; i++) {
676 struct got_object_qid *qid;
678 err = got_object_qid_alloc_partial(&qid);
679 if (err)
680 break;
681 memcpy(qid->id, imsg.data + len +
682 i * SHA1_DIGEST_LENGTH, sizeof(*qid->id));
683 SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
684 (*commit)->nparents++;
686 break;
687 default:
688 err = got_error(GOT_ERR_PRIVSEP_MSG);
689 break;
692 imsg_free(&imsg);
694 return err;
697 const struct got_error *
698 got_privsep_send_tree(struct imsgbuf *ibuf, struct got_pathlist_head *entries,
699 int nentries)
701 const struct got_error *err = NULL;
702 struct got_imsg_tree_object itree;
703 struct got_pathlist_entry *pe;
704 size_t totlen;
705 int nimsg; /* number of imsg queued in ibuf */
707 itree.nentries = nentries;
708 if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
709 == -1)
710 return got_error_from_errno("imsg_compose TREE");
712 totlen = sizeof(itree);
713 nimsg = 1;
714 TAILQ_FOREACH(pe, entries, entry) {
715 const char *name = pe->path;
716 struct got_parsed_tree_entry *pte = pe->data;
717 struct ibuf *wbuf;
718 size_t namelen = strlen(name);
719 size_t len = sizeof(struct got_imsg_tree_entry) + namelen;
721 if (len > MAX_IMSGSIZE)
722 return got_error(GOT_ERR_NO_SPACE);
724 nimsg++;
725 if (totlen + len >= MAX_IMSGSIZE - (IMSG_HEADER_SIZE * nimsg)) {
726 err = flush_imsg(ibuf);
727 if (err)
728 return err;
729 nimsg = 0;
732 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_ENTRY, 0, 0, len);
733 if (wbuf == NULL)
734 return got_error_from_errno("imsg_create TREE_ENTRY");
736 /* Keep in sync with struct got_imsg_tree_object definition! */
737 if (imsg_add(wbuf, pte->id, SHA1_DIGEST_LENGTH) == -1) {
738 err = got_error_from_errno("imsg_add TREE_ENTRY");
739 ibuf_free(wbuf);
740 return err;
742 if (imsg_add(wbuf, &pte->mode, sizeof(pte->mode)) == -1) {
743 err = got_error_from_errno("imsg_add TREE_ENTRY");
744 ibuf_free(wbuf);
745 return err;
748 if (imsg_add(wbuf, name, namelen) == -1) {
749 err = got_error_from_errno("imsg_add TREE_ENTRY");
750 ibuf_free(wbuf);
751 return err;
754 wbuf->fd = -1;
755 imsg_close(ibuf, wbuf);
757 totlen += len;
760 return flush_imsg(ibuf);
763 const struct got_error *
764 got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
766 const struct got_error *err = NULL;
767 const size_t min_datalen =
768 MIN(sizeof(struct got_imsg_error),
769 sizeof(struct got_imsg_tree_object));
770 struct got_imsg_tree_object *itree;
771 int nentries = 0;
773 *tree = NULL;
774 get_more:
775 err = read_imsg(ibuf);
776 if (err)
777 goto done;
779 for (;;) {
780 struct imsg imsg;
781 size_t n;
782 size_t datalen;
783 struct got_imsg_tree_entry *ite;
784 struct got_tree_entry *te = NULL;
786 n = imsg_get(ibuf, &imsg);
787 if (n == 0) {
788 if (*tree && (*tree)->nentries != nentries)
789 goto get_more;
790 break;
793 if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen)
794 return got_error(GOT_ERR_PRIVSEP_LEN);
796 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
798 switch (imsg.hdr.type) {
799 case GOT_IMSG_ERROR:
800 err = recv_imsg_error(&imsg, datalen);
801 break;
802 case GOT_IMSG_TREE:
803 /* This message should only appear once. */
804 if (*tree != NULL) {
805 err = got_error(GOT_ERR_PRIVSEP_MSG);
806 break;
808 if (datalen != sizeof(*itree)) {
809 err = got_error(GOT_ERR_PRIVSEP_LEN);
810 break;
812 itree = imsg.data;
813 *tree = malloc(sizeof(**tree));
814 if (*tree == NULL) {
815 err = got_error_from_errno("malloc");
816 break;
818 (*tree)->entries = calloc(itree->nentries,
819 sizeof(struct got_tree_entry));
820 if ((*tree)->entries == NULL) {
821 err = got_error_from_errno("malloc");
822 break;
824 (*tree)->nentries = itree->nentries;
825 (*tree)->refcnt = 0;
826 break;
827 case GOT_IMSG_TREE_ENTRY:
828 /* This message should be preceeded by GOT_IMSG_TREE. */
829 if (*tree == NULL) {
830 err = got_error(GOT_ERR_PRIVSEP_MSG);
831 break;
833 if (datalen < sizeof(*ite) || datalen > MAX_IMSGSIZE) {
834 err = got_error(GOT_ERR_PRIVSEP_LEN);
835 break;
838 /* Remaining data contains the entry's name. */
839 datalen -= sizeof(*ite);
840 if (datalen == 0 || datalen > MAX_IMSGSIZE) {
841 err = got_error(GOT_ERR_PRIVSEP_LEN);
842 break;
844 ite = imsg.data;
846 if (datalen + 1 > sizeof(te->name)) {
847 err = got_error(GOT_ERR_NO_SPACE);
848 break;
850 te = &(*tree)->entries[nentries];
851 memcpy(te->name, imsg.data + sizeof(*ite), datalen);
852 te->name[datalen] = '\0';
854 memcpy(te->id.sha1, ite->id, SHA1_DIGEST_LENGTH);
855 te->mode = ite->mode;
856 te->idx = nentries;
857 nentries++;
858 break;
859 default:
860 err = got_error(GOT_ERR_PRIVSEP_MSG);
861 break;
864 imsg_free(&imsg);
866 done:
867 if (*tree && (*tree)->nentries != nentries) {
868 if (err == NULL)
869 err = got_error(GOT_ERR_PRIVSEP_LEN);
870 got_object_tree_close(*tree);
871 *tree = NULL;
874 return err;
877 const struct got_error *
878 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
879 const uint8_t *data)
881 struct got_imsg_blob iblob;
883 iblob.size = size;
884 iblob.hdrlen = hdrlen;
886 if (data) {
887 uint8_t *buf;
889 if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
890 return got_error(GOT_ERR_NO_SPACE);
892 buf = malloc(sizeof(iblob) + size);
893 if (buf == NULL)
894 return got_error_from_errno("malloc");
896 memcpy(buf, &iblob, sizeof(iblob));
897 memcpy(buf + sizeof(iblob), data, size);
898 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
899 sizeof(iblob) + size) == -1) {
900 free(buf);
901 return got_error_from_errno("imsg_compose BLOB");
903 free(buf);
904 } else {
905 /* Data has already been written to file descriptor. */
906 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
907 sizeof(iblob)) == -1)
908 return got_error_from_errno("imsg_compose BLOB");
912 return flush_imsg(ibuf);
915 const struct got_error *
916 got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
917 struct imsgbuf *ibuf)
919 const struct got_error *err = NULL;
920 struct imsg imsg;
921 struct got_imsg_blob *iblob;
922 size_t datalen;
924 *outbuf = NULL;
926 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
927 if (err)
928 return err;
930 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
932 switch (imsg.hdr.type) {
933 case GOT_IMSG_BLOB:
934 if (datalen < sizeof(*iblob)) {
935 err = got_error(GOT_ERR_PRIVSEP_LEN);
936 break;
938 iblob = imsg.data;
939 *size = iblob->size;
940 *hdrlen = iblob->hdrlen;
942 if (datalen == sizeof(*iblob)) {
943 /* Data has been written to file descriptor. */
944 break;
947 if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX) {
948 err = got_error(GOT_ERR_PRIVSEP_LEN);
949 break;
952 *outbuf = malloc(*size);
953 if (*outbuf == NULL) {
954 err = got_error_from_errno("malloc");
955 break;
957 memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
958 break;
959 default:
960 err = got_error(GOT_ERR_PRIVSEP_MSG);
961 break;
964 imsg_free(&imsg);
966 return err;
969 static const struct got_error *
970 send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
972 const struct got_error *err = NULL;
973 size_t offset, remain;
975 offset = 0;
976 remain = tagmsg_len;
977 while (remain > 0) {
978 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
980 if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
981 tag->tagmsg + offset, n) == -1) {
982 err = got_error_from_errno("imsg_compose TAG_TAGMSG");
983 break;
986 err = flush_imsg(ibuf);
987 if (err)
988 break;
990 offset += n;
991 remain -= n;
994 return err;
997 const struct got_error *
998 got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
1000 const struct got_error *err = NULL;
1001 struct got_imsg_tag_object *itag;
1002 uint8_t *buf;
1003 size_t len, total;
1004 size_t tag_len = strlen(tag->tag);
1005 size_t tagger_len = strlen(tag->tagger);
1006 size_t tagmsg_len = strlen(tag->tagmsg);
1008 total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
1010 buf = malloc(total);
1011 if (buf == NULL)
1012 return got_error_from_errno("malloc");
1014 itag = (struct got_imsg_tag_object *)buf;
1015 memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
1016 itag->obj_type = tag->obj_type;
1017 itag->tag_len = tag_len;
1018 itag->tagger_len = tagger_len;
1019 itag->tagger_time = tag->tagger_time;
1020 itag->tagger_gmtoff = tag->tagger_gmtoff;
1021 itag->tagmsg_len = tagmsg_len;
1023 len = sizeof(*itag);
1024 memcpy(buf + len, tag->tag, tag_len);
1025 len += tag_len;
1026 memcpy(buf + len, tag->tagger, tagger_len);
1027 len += tagger_len;
1029 if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
1030 err = got_error_from_errno("imsg_compose TAG");
1031 goto done;
1034 if (tagmsg_len == 0 ||
1035 tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1036 err = flush_imsg(ibuf);
1037 if (err)
1038 goto done;
1040 err = send_tagmsg(ibuf, tag, tagmsg_len);
1041 done:
1042 free(buf);
1043 return err;
1046 const struct got_error *
1047 got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
1049 const struct got_error *err = NULL;
1050 struct imsg imsg;
1051 struct got_imsg_tag_object *itag;
1052 size_t len, datalen;
1053 const size_t min_datalen =
1054 MIN(sizeof(struct got_imsg_error),
1055 sizeof(struct got_imsg_tag_object));
1057 *tag = NULL;
1059 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1060 if (err)
1061 return err;
1063 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1064 len = 0;
1066 switch (imsg.hdr.type) {
1067 case GOT_IMSG_TAG:
1068 if (datalen < sizeof(*itag)) {
1069 err = got_error(GOT_ERR_PRIVSEP_LEN);
1070 break;
1072 itag = imsg.data;
1073 if (datalen != sizeof(*itag) + itag->tag_len +
1074 itag->tagger_len) {
1075 err = got_error(GOT_ERR_PRIVSEP_LEN);
1076 break;
1078 len += sizeof(*itag);
1080 *tag = calloc(1, sizeof(**tag));
1081 if (*tag == NULL) {
1082 err = got_error_from_errno("calloc");
1083 break;
1086 memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
1088 if (itag->tag_len == 0) {
1089 (*tag)->tag = strdup("");
1090 if ((*tag)->tag == NULL) {
1091 err = got_error_from_errno("strdup");
1092 break;
1094 } else {
1095 (*tag)->tag = malloc(itag->tag_len + 1);
1096 if ((*tag)->tag == NULL) {
1097 err = got_error_from_errno("malloc");
1098 break;
1100 memcpy((*tag)->tag, imsg.data + len,
1101 itag->tag_len);
1102 (*tag)->tag[itag->tag_len] = '\0';
1104 len += itag->tag_len;
1106 (*tag)->obj_type = itag->obj_type;
1107 (*tag)->tagger_time = itag->tagger_time;
1108 (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1110 if (itag->tagger_len == 0) {
1111 (*tag)->tagger = strdup("");
1112 if ((*tag)->tagger == NULL) {
1113 err = got_error_from_errno("strdup");
1114 break;
1116 } else {
1117 (*tag)->tagger = malloc(itag->tagger_len + 1);
1118 if ((*tag)->tagger == NULL) {
1119 err = got_error_from_errno("malloc");
1120 break;
1122 memcpy((*tag)->tagger, imsg.data + len,
1123 itag->tagger_len);
1124 (*tag)->tagger[itag->tagger_len] = '\0';
1126 len += itag->tagger_len;
1128 if (itag->tagmsg_len == 0) {
1129 (*tag)->tagmsg = strdup("");
1130 if ((*tag)->tagmsg == NULL) {
1131 err = got_error_from_errno("strdup");
1132 break;
1134 } else {
1135 size_t offset = 0, remain = itag->tagmsg_len;
1137 (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1138 if ((*tag)->tagmsg == NULL) {
1139 err = got_error_from_errno("malloc");
1140 break;
1142 while (remain > 0) {
1143 struct imsg imsg_log;
1144 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1145 remain);
1147 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1148 if (err)
1149 return err;
1151 if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1152 return got_error(GOT_ERR_PRIVSEP_MSG);
1154 memcpy((*tag)->tagmsg + offset, imsg_log.data,
1155 n);
1156 imsg_free(&imsg_log);
1157 offset += n;
1158 remain -= n;
1160 (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1163 break;
1164 default:
1165 err = got_error(GOT_ERR_PRIVSEP_MSG);
1166 break;
1169 imsg_free(&imsg);
1171 return err;
1174 const struct got_error *
1175 got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1176 struct got_packidx *packidx)
1178 const struct got_error *err = NULL;
1179 struct got_imsg_packidx ipackidx;
1180 struct got_imsg_pack ipack;
1181 int fd;
1183 ipackidx.len = packidx->len;
1184 fd = dup(packidx->fd);
1185 if (fd == -1)
1186 return got_error_from_errno("dup");
1188 if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
1189 sizeof(ipackidx)) == -1) {
1190 err = got_error_from_errno("imsg_compose PACKIDX");
1191 close(fd);
1192 return err;
1195 if (strlcpy(ipack.path_packfile, pack->path_packfile,
1196 sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
1197 return got_error(GOT_ERR_NO_SPACE);
1198 ipack.filesize = pack->filesize;
1200 fd = dup(pack->fd);
1201 if (fd == -1)
1202 return got_error_from_errno("dup");
1204 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
1205 == -1) {
1206 err = got_error_from_errno("imsg_compose PACK");
1207 close(fd);
1208 return err;
1211 return flush_imsg(ibuf);
1214 const struct got_error *
1215 got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
1216 struct got_object_id *id)
1218 struct got_imsg_packed_object iobj;
1220 iobj.idx = idx;
1221 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
1223 if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
1224 &iobj, sizeof(iobj)) == -1)
1225 return got_error_from_errno("imsg_compose "
1226 "PACKED_OBJECT_REQUEST");
1228 return flush_imsg(ibuf);
1231 const struct got_error *
1232 got_privsep_send_gitconfig_parse_req(struct imsgbuf *ibuf, int fd)
1234 const struct got_error *err = NULL;
1236 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_PARSE_REQUEST, 0, 0, fd,
1237 NULL, 0) == -1) {
1238 err = got_error_from_errno("imsg_compose "
1239 "GITCONFIG_PARSE_REQUEST");
1240 close(fd);
1241 return err;
1244 return flush_imsg(ibuf);
1247 const struct got_error *
1248 got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *ibuf)
1250 if (imsg_compose(ibuf,
1251 GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST, 0, 0, -1,
1252 NULL, 0) == -1)
1253 return got_error_from_errno("imsg_compose "
1254 "GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST");
1256 return flush_imsg(ibuf);
1259 const struct got_error *
1260 got_privsep_send_gitconfig_author_name_req(struct imsgbuf *ibuf)
1262 if (imsg_compose(ibuf,
1263 GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST, 0, 0, -1, NULL, 0) == -1)
1264 return got_error_from_errno("imsg_compose "
1265 "GITCONFIG_AUTHOR_NAME_REQUEST");
1267 return flush_imsg(ibuf);
1270 const struct got_error *
1271 got_privsep_send_gitconfig_author_email_req(struct imsgbuf *ibuf)
1273 if (imsg_compose(ibuf,
1274 GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST, 0, 0, -1, NULL, 0) == -1)
1275 return got_error_from_errno("imsg_compose "
1276 "GITCONFIG_AUTHOR_EMAIL_REQUEST");
1278 return flush_imsg(ibuf);
1281 const struct got_error *
1282 got_privsep_send_gitconfig_str(struct imsgbuf *ibuf, const char *value)
1284 size_t len = value ? strlen(value) + 1 : 0;
1286 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_STR_VAL, 0, 0, -1,
1287 value, len) == -1)
1288 return got_error_from_errno("imsg_compose GITCONFIG_STR_VAL");
1290 return flush_imsg(ibuf);
1293 const struct got_error *
1294 got_privsep_recv_gitconfig_str(char **str, struct imsgbuf *ibuf)
1296 const struct got_error *err = NULL;
1297 struct imsg imsg;
1298 size_t datalen;
1299 const size_t min_datalen = 0;
1301 *str = NULL;
1303 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1304 if (err)
1305 return err;
1306 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1308 switch (imsg.hdr.type) {
1309 case GOT_IMSG_GITCONFIG_STR_VAL:
1310 if (datalen == 0)
1311 break;
1312 *str = malloc(datalen);
1313 if (*str == NULL) {
1314 err = got_error_from_errno("malloc");
1315 break;
1317 if (strlcpy(*str, imsg.data, datalen) >= datalen)
1318 err = got_error(GOT_ERR_NO_SPACE);
1319 break;
1320 default:
1321 err = got_error(GOT_ERR_PRIVSEP_MSG);
1322 break;
1325 imsg_free(&imsg);
1326 return err;
1329 const struct got_error *
1330 got_privsep_send_gitconfig_int(struct imsgbuf *ibuf, int value)
1332 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_INT_VAL, 0, 0, -1,
1333 &value, sizeof(value)) == -1)
1334 return got_error_from_errno("imsg_compose GITCONFIG_INT_VAL");
1336 return flush_imsg(ibuf);
1339 const struct got_error *
1340 got_privsep_recv_gitconfig_int(int *val, struct imsgbuf *ibuf)
1342 const struct got_error *err = NULL;
1343 struct imsg imsg;
1344 size_t datalen;
1345 const size_t min_datalen =
1346 MIN(sizeof(struct got_imsg_error), sizeof(int));
1348 *val = 0;
1350 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1351 if (err)
1352 return err;
1353 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1355 switch (imsg.hdr.type) {
1356 case GOT_IMSG_GITCONFIG_INT_VAL:
1357 if (datalen != sizeof(*val)) {
1358 err = got_error(GOT_ERR_PRIVSEP_LEN);
1359 break;
1361 memcpy(val, imsg.data, sizeof(*val));
1362 break;
1363 default:
1364 err = got_error(GOT_ERR_PRIVSEP_MSG);
1365 break;
1368 imsg_free(&imsg);
1369 return err;
1372 const struct got_error *
1373 got_privsep_unveil_exec_helpers(void)
1375 const char *helpers[] = {
1376 GOT_PATH_PROG_READ_PACK,
1377 GOT_PATH_PROG_READ_OBJECT,
1378 GOT_PATH_PROG_READ_COMMIT,
1379 GOT_PATH_PROG_READ_TREE,
1380 GOT_PATH_PROG_READ_BLOB,
1381 GOT_PATH_PROG_READ_TAG,
1382 GOT_PATH_PROG_READ_GITCONFIG,
1384 int i;
1386 for (i = 0; i < nitems(helpers); i++) {
1387 if (unveil(helpers[i], "x") == 0)
1388 continue;
1389 return got_error_from_errno2("unveil", helpers[i]);
1392 return NULL;
1395 void
1396 got_privsep_exec_child(int imsg_fds[2], const char *path, const char *repo_path)
1398 if (close(imsg_fds[0]) != 0) {
1399 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
1400 _exit(1);
1403 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
1404 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
1405 _exit(1);
1407 if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
1408 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
1409 _exit(1);
1412 if (execl(path, path, repo_path, (char *)NULL) == -1) {
1413 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,
1414 strerror(errno));
1415 _exit(1);