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_object) + 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)->entries.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.nentries = itree->nentries;
819 SIMPLEQ_INIT(&(*tree)->entries.head);
820 (*tree)->refcnt = 0;
821 break;
822 case GOT_IMSG_TREE_ENTRY:
823 /* This message should be preceeded by GOT_IMSG_TREE. */
824 if (*tree == NULL) {
825 err = got_error(GOT_ERR_PRIVSEP_MSG);
826 break;
828 if (datalen < sizeof(*ite) || datalen > MAX_IMSGSIZE) {
829 err = got_error(GOT_ERR_PRIVSEP_LEN);
830 break;
833 /* Remaining data contains the entry's name. */
834 datalen -= sizeof(*ite);
835 if (datalen == 0 || datalen > MAX_IMSGSIZE) {
836 err = got_error(GOT_ERR_PRIVSEP_LEN);
837 break;
839 ite = imsg.data;
841 te = got_alloc_tree_entry_partial();
842 if (te == NULL) {
843 err = got_error_from_errno(
844 "got_alloc_tree_entry_partial");
845 break;
847 te->name = malloc(datalen + 1);
848 if (te->name == NULL) {
849 free(te);
850 err = got_error_from_errno("malloc");
851 break;
853 memcpy(te->name, imsg.data + sizeof(*ite), datalen);
854 te->name[datalen] = '\0';
856 memcpy(te->id->sha1, ite->id, SHA1_DIGEST_LENGTH);
857 te->mode = ite->mode;
858 SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
859 nentries++;
860 break;
861 default:
862 err = got_error(GOT_ERR_PRIVSEP_MSG);
863 break;
866 imsg_free(&imsg);
868 done:
869 if (*tree && (*tree)->entries.nentries != nentries) {
870 if (err == NULL)
871 err = got_error(GOT_ERR_PRIVSEP_LEN);
872 got_object_tree_close(*tree);
873 *tree = NULL;
876 return err;
879 const struct got_error *
880 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
881 const uint8_t *data)
883 struct got_imsg_blob iblob;
885 iblob.size = size;
886 iblob.hdrlen = hdrlen;
888 if (data) {
889 uint8_t *buf;
891 if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
892 return got_error(GOT_ERR_NO_SPACE);
894 buf = malloc(sizeof(iblob) + size);
895 if (buf == NULL)
896 return got_error_from_errno("malloc");
898 memcpy(buf, &iblob, sizeof(iblob));
899 memcpy(buf + sizeof(iblob), data, size);
900 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
901 sizeof(iblob) + size) == -1) {
902 free(buf);
903 return got_error_from_errno("imsg_compose BLOB");
905 free(buf);
906 } else {
907 /* Data has already been written to file descriptor. */
908 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
909 sizeof(iblob)) == -1)
910 return got_error_from_errno("imsg_compose BLOB");
914 return flush_imsg(ibuf);
917 const struct got_error *
918 got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
919 struct imsgbuf *ibuf)
921 const struct got_error *err = NULL;
922 struct imsg imsg;
923 struct got_imsg_blob *iblob;
924 size_t datalen;
926 *outbuf = NULL;
928 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
929 if (err)
930 return err;
932 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
934 switch (imsg.hdr.type) {
935 case GOT_IMSG_BLOB:
936 if (datalen < sizeof(*iblob)) {
937 err = got_error(GOT_ERR_PRIVSEP_LEN);
938 break;
940 iblob = imsg.data;
941 *size = iblob->size;
942 *hdrlen = iblob->hdrlen;
944 if (datalen == sizeof(*iblob)) {
945 /* Data has been written to file descriptor. */
946 break;
949 if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX) {
950 err = got_error(GOT_ERR_PRIVSEP_LEN);
951 break;
954 *outbuf = malloc(*size);
955 if (*outbuf == NULL) {
956 err = got_error_from_errno("malloc");
957 break;
959 memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
960 break;
961 default:
962 err = got_error(GOT_ERR_PRIVSEP_MSG);
963 break;
966 imsg_free(&imsg);
968 return err;
971 static const struct got_error *
972 send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
974 const struct got_error *err = NULL;
975 size_t offset, remain;
977 offset = 0;
978 remain = tagmsg_len;
979 while (remain > 0) {
980 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
982 if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
983 tag->tagmsg + offset, n) == -1) {
984 err = got_error_from_errno("imsg_compose TAG_TAGMSG");
985 break;
988 err = flush_imsg(ibuf);
989 if (err)
990 break;
992 offset += n;
993 remain -= n;
996 return err;
999 const struct got_error *
1000 got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
1002 const struct got_error *err = NULL;
1003 struct got_imsg_tag_object *itag;
1004 uint8_t *buf;
1005 size_t len, total;
1006 size_t tag_len = strlen(tag->tag);
1007 size_t tagger_len = strlen(tag->tagger);
1008 size_t tagmsg_len = strlen(tag->tagmsg);
1010 total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
1012 buf = malloc(total);
1013 if (buf == NULL)
1014 return got_error_from_errno("malloc");
1016 itag = (struct got_imsg_tag_object *)buf;
1017 memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
1018 itag->obj_type = tag->obj_type;
1019 itag->tag_len = tag_len;
1020 itag->tagger_len = tagger_len;
1021 itag->tagger_time = tag->tagger_time;
1022 itag->tagger_gmtoff = tag->tagger_gmtoff;
1023 itag->tagmsg_len = tagmsg_len;
1025 len = sizeof(*itag);
1026 memcpy(buf + len, tag->tag, tag_len);
1027 len += tag_len;
1028 memcpy(buf + len, tag->tagger, tagger_len);
1029 len += tagger_len;
1031 if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
1032 err = got_error_from_errno("imsg_compose TAG");
1033 goto done;
1036 if (tagmsg_len == 0 ||
1037 tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1038 err = flush_imsg(ibuf);
1039 if (err)
1040 goto done;
1042 err = send_tagmsg(ibuf, tag, tagmsg_len);
1043 done:
1044 free(buf);
1045 return err;
1048 const struct got_error *
1049 got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
1051 const struct got_error *err = NULL;
1052 struct imsg imsg;
1053 struct got_imsg_tag_object *itag;
1054 size_t len, datalen;
1055 const size_t min_datalen =
1056 MIN(sizeof(struct got_imsg_error),
1057 sizeof(struct got_imsg_tag_object));
1059 *tag = NULL;
1061 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1062 if (err)
1063 return err;
1065 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1066 len = 0;
1068 switch (imsg.hdr.type) {
1069 case GOT_IMSG_TAG:
1070 if (datalen < sizeof(*itag)) {
1071 err = got_error(GOT_ERR_PRIVSEP_LEN);
1072 break;
1074 itag = imsg.data;
1075 if (datalen != sizeof(*itag) + itag->tag_len +
1076 itag->tagger_len) {
1077 err = got_error(GOT_ERR_PRIVSEP_LEN);
1078 break;
1080 len += sizeof(*itag);
1082 *tag = calloc(1, sizeof(**tag));
1083 if (*tag == NULL) {
1084 err = got_error_from_errno("calloc");
1085 break;
1088 memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
1090 if (itag->tag_len == 0) {
1091 (*tag)->tag = strdup("");
1092 if ((*tag)->tag == NULL) {
1093 err = got_error_from_errno("strdup");
1094 break;
1096 } else {
1097 (*tag)->tag = malloc(itag->tag_len + 1);
1098 if ((*tag)->tag == NULL) {
1099 err = got_error_from_errno("malloc");
1100 break;
1102 memcpy((*tag)->tag, imsg.data + len,
1103 itag->tag_len);
1104 (*tag)->tag[itag->tag_len] = '\0';
1106 len += itag->tag_len;
1108 (*tag)->obj_type = itag->obj_type;
1109 (*tag)->tagger_time = itag->tagger_time;
1110 (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1112 if (itag->tagger_len == 0) {
1113 (*tag)->tagger = strdup("");
1114 if ((*tag)->tagger == NULL) {
1115 err = got_error_from_errno("strdup");
1116 break;
1118 } else {
1119 (*tag)->tagger = malloc(itag->tagger_len + 1);
1120 if ((*tag)->tagger == NULL) {
1121 err = got_error_from_errno("malloc");
1122 break;
1124 memcpy((*tag)->tagger, imsg.data + len,
1125 itag->tagger_len);
1126 (*tag)->tagger[itag->tagger_len] = '\0';
1128 len += itag->tagger_len;
1130 if (itag->tagmsg_len == 0) {
1131 (*tag)->tagmsg = strdup("");
1132 if ((*tag)->tagmsg == NULL) {
1133 err = got_error_from_errno("strdup");
1134 break;
1136 } else {
1137 size_t offset = 0, remain = itag->tagmsg_len;
1139 (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1140 if ((*tag)->tagmsg == NULL) {
1141 err = got_error_from_errno("malloc");
1142 break;
1144 while (remain > 0) {
1145 struct imsg imsg_log;
1146 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1147 remain);
1149 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1150 if (err)
1151 return err;
1153 if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1154 return got_error(GOT_ERR_PRIVSEP_MSG);
1156 memcpy((*tag)->tagmsg + offset, imsg_log.data,
1157 n);
1158 imsg_free(&imsg_log);
1159 offset += n;
1160 remain -= n;
1162 (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1165 break;
1166 default:
1167 err = got_error(GOT_ERR_PRIVSEP_MSG);
1168 break;
1171 imsg_free(&imsg);
1173 return err;
1176 const struct got_error *
1177 got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1178 struct got_packidx *packidx)
1180 const struct got_error *err = NULL;
1181 struct got_imsg_packidx ipackidx;
1182 struct got_imsg_pack ipack;
1183 int fd;
1185 ipackidx.len = packidx->len;
1186 fd = dup(packidx->fd);
1187 if (fd == -1)
1188 return got_error_from_errno("dup");
1190 if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
1191 sizeof(ipackidx)) == -1) {
1192 err = got_error_from_errno("imsg_compose PACKIDX");
1193 close(fd);
1194 return err;
1197 if (strlcpy(ipack.path_packfile, pack->path_packfile,
1198 sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
1199 return got_error(GOT_ERR_NO_SPACE);
1200 ipack.filesize = pack->filesize;
1202 fd = dup(pack->fd);
1203 if (fd == -1)
1204 return got_error_from_errno("dup");
1206 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
1207 == -1) {
1208 err = got_error_from_errno("imsg_compose PACK");
1209 close(fd);
1210 return err;
1213 return flush_imsg(ibuf);
1216 const struct got_error *
1217 got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
1218 struct got_object_id *id)
1220 struct got_imsg_packed_object iobj;
1222 iobj.idx = idx;
1223 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
1225 if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
1226 &iobj, sizeof(iobj)) == -1)
1227 return got_error_from_errno("imsg_compose "
1228 "PACKED_OBJECT_REQUEST");
1230 return flush_imsg(ibuf);
1233 const struct got_error *
1234 got_privsep_send_gitconfig_parse_req(struct imsgbuf *ibuf, int fd)
1236 const struct got_error *err = NULL;
1238 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_PARSE_REQUEST, 0, 0, fd,
1239 NULL, 0) == -1) {
1240 err = got_error_from_errno("imsg_compose "
1241 "GITCONFIG_PARSE_REQUEST");
1242 close(fd);
1243 return err;
1246 return flush_imsg(ibuf);
1249 const struct got_error *
1250 got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *ibuf)
1252 if (imsg_compose(ibuf,
1253 GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST, 0, 0, -1,
1254 NULL, 0) == -1)
1255 return got_error_from_errno("imsg_compose "
1256 "GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST");
1258 return flush_imsg(ibuf);
1261 const struct got_error *
1262 got_privsep_send_gitconfig_author_name_req(struct imsgbuf *ibuf)
1264 if (imsg_compose(ibuf,
1265 GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST, 0, 0, -1, NULL, 0) == -1)
1266 return got_error_from_errno("imsg_compose "
1267 "GITCONFIG_AUTHOR_NAME_REQUEST");
1269 return flush_imsg(ibuf);
1272 const struct got_error *
1273 got_privsep_send_gitconfig_author_email_req(struct imsgbuf *ibuf)
1275 if (imsg_compose(ibuf,
1276 GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST, 0, 0, -1, NULL, 0) == -1)
1277 return got_error_from_errno("imsg_compose "
1278 "GITCONFIG_AUTHOR_EMAIL_REQUEST");
1280 return flush_imsg(ibuf);
1283 const struct got_error *
1284 got_privsep_send_gitconfig_str(struct imsgbuf *ibuf, const char *value)
1286 size_t len = value ? strlen(value) + 1 : 0;
1288 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_STR_VAL, 0, 0, -1,
1289 value, len) == -1)
1290 return got_error_from_errno("imsg_compose GITCONFIG_STR_VAL");
1292 return flush_imsg(ibuf);
1295 const struct got_error *
1296 got_privsep_recv_gitconfig_str(char **str, struct imsgbuf *ibuf)
1298 const struct got_error *err = NULL;
1299 struct imsg imsg;
1300 size_t datalen;
1301 const size_t min_datalen = 0;
1303 *str = NULL;
1305 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1306 if (err)
1307 return err;
1308 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1310 switch (imsg.hdr.type) {
1311 case GOT_IMSG_GITCONFIG_STR_VAL:
1312 if (datalen == 0)
1313 break;
1314 *str = malloc(datalen);
1315 if (*str == NULL) {
1316 err = got_error_from_errno("malloc");
1317 break;
1319 if (strlcpy(*str, imsg.data, datalen) >= datalen)
1320 err = got_error(GOT_ERR_NO_SPACE);
1321 break;
1322 default:
1323 err = got_error(GOT_ERR_PRIVSEP_MSG);
1324 break;
1327 imsg_free(&imsg);
1328 return err;
1331 const struct got_error *
1332 got_privsep_send_gitconfig_int(struct imsgbuf *ibuf, int value)
1334 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_INT_VAL, 0, 0, -1,
1335 &value, sizeof(value)) == -1)
1336 return got_error_from_errno("imsg_compose GITCONFIG_INT_VAL");
1338 return flush_imsg(ibuf);
1341 const struct got_error *
1342 got_privsep_recv_gitconfig_int(int *val, struct imsgbuf *ibuf)
1344 const struct got_error *err = NULL;
1345 struct imsg imsg;
1346 size_t datalen;
1347 const size_t min_datalen =
1348 MIN(sizeof(struct got_imsg_error), sizeof(int));
1350 *val = 0;
1352 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1353 if (err)
1354 return err;
1355 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1357 switch (imsg.hdr.type) {
1358 case GOT_IMSG_GITCONFIG_INT_VAL:
1359 if (datalen != sizeof(*val)) {
1360 err = got_error(GOT_ERR_PRIVSEP_LEN);
1361 break;
1363 memcpy(val, imsg.data, sizeof(*val));
1364 break;
1365 default:
1366 err = got_error(GOT_ERR_PRIVSEP_MSG);
1367 break;
1370 imsg_free(&imsg);
1371 return err;
1374 const struct got_error *
1375 got_privsep_unveil_exec_helpers(void)
1377 const char *helpers[] = {
1378 GOT_PATH_PROG_READ_PACK,
1379 GOT_PATH_PROG_READ_OBJECT,
1380 GOT_PATH_PROG_READ_COMMIT,
1381 GOT_PATH_PROG_READ_TREE,
1382 GOT_PATH_PROG_READ_BLOB,
1383 GOT_PATH_PROG_READ_TAG,
1384 GOT_PATH_PROG_READ_GITCONFIG,
1386 int i;
1388 for (i = 0; i < nitems(helpers); i++) {
1389 if (unveil(helpers[i], "x") == 0)
1390 continue;
1391 return got_error_from_errno2("unveil", helpers[i]);
1394 return NULL;
1397 void
1398 got_privsep_exec_child(int imsg_fds[2], const char *path, const char *repo_path)
1400 if (close(imsg_fds[0]) != 0) {
1401 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
1402 _exit(1);
1405 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
1406 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
1407 _exit(1);
1409 if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
1410 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
1411 _exit(1);
1414 if (execl(path, path, repo_path, (char *)NULL) == -1) {
1415 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,
1416 strerror(errno));
1417 _exit(1);